Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <time.h> |
| 29 | #include <limits.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 30 | #include <arpa/inet.h> |
| 31 | |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/select.h> |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 34 | #include <osmocom/core/socket.h> |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 35 | #include <osmocom/core/byteswap.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 36 | #include <osmocom/netif/rtp.h> |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 37 | #include <osmocom/netif/amr.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 39 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 40 | #include <osmocom/mgcp/mgcp_network.h> |
| 41 | #include <osmocom/mgcp/mgcp_protocol.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_stat.h> |
| 43 | #include <osmocom/mgcp/osmux.h> |
| 44 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 45 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 46 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 47 | #include <osmocom/mgcp/mgcp_codec.h> |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 48 | #include <osmocom/mgcp/debug.h> |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 49 | #include <osmocom/codec/codec.h> |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 50 | #include <osmocom/mgcp/mgcp_e1.h> |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 51 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 52 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 53 | #define RTP_SEQ_MOD (1 << 16) |
| 54 | #define RTP_MAX_DROPOUT 3000 |
| 55 | #define RTP_MAX_MISORDER 100 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 56 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 57 | enum rtp_proto { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 58 | MGCP_PROTO_RTP, |
| 59 | MGCP_PROTO_RTCP, |
| 60 | }; |
| 61 | |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 62 | static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, |
| 63 | int id, int inc) |
| 64 | { |
| 65 | struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 66 | struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats; |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 67 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 68 | /* add to both the per-connection and the global stats */ |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 69 | rate_ctr_add(&conn_stats->ctr[id], inc); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 70 | rate_ctr_add(&mgw_stats->ctr[id], inc); |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id) |
| 74 | { |
| 75 | rtpconn_rate_ctr_add(conn_rtp, endp, id, 1); |
| 76 | } |
| 77 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 78 | static int rx_rtp(struct msgb *msg); |
| 79 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 80 | static bool addr_is_any(struct osmo_sockaddr *osa) { |
| 81 | if (osa->u.sa.sa_family == AF_INET6) { |
| 82 | struct in6_addr ip6_any = IN6ADDR_ANY_INIT; |
| 83 | return memcmp(&osa->u.sin6.sin6_addr, |
| 84 | &ip6_any, sizeof(ip6_any)) == 0; |
| 85 | } else { |
| 86 | return osa->u.sin.sin_addr.s_addr == 0; |
| 87 | } |
| 88 | } |
| 89 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 90 | /*! Determine the local rtp bind IP-address. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 91 | * \param[out] addr caller provided memory to store the resulting IP-Address. |
| 92 | * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters. |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 93 | * |
| 94 | * The local bind IP-address is automatically selected by probing the |
| 95 | * IP-Address of the interface that is pointing towards the remote IP-Address, |
| 96 | * if no remote IP-Address is known yet, the statically configured |
| 97 | * IP-Addresses are used as fallback. */ |
| 98 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn) |
| 99 | { |
| 100 | |
| 101 | struct mgcp_endpoint *endp; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 102 | char ipbuf[INET6_ADDRSTRLEN]; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 103 | int rc; |
| 104 | endp = conn->conn->endp; |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 105 | bool rem_addr_set = !addr_is_any(&conn->end.addr); |
| 106 | char *bind_addr; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 107 | |
| 108 | /* Try probing the local IP-Address */ |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 109 | if (endp->cfg->net_ports.bind_addr_probe && rem_addr_set) { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 110 | rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf)); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 111 | if (rc < 0) |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 112 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
| 113 | "local interface auto detection failed, using configured addresses...\n"); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 114 | else { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 115 | LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, |
| 116 | "selected local rtp bind ip %s by probing using remote ip %s\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 117 | addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf)); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | } |
| 121 | |
Pau Espin Pedrol | a93c6e9 | 2019-05-06 15:23:57 +0200 | [diff] [blame] | 122 | /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */ |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 123 | if (rem_addr_set) { |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 124 | /* Check there is a bind IP for the RTP traffic configured, |
| 125 | * if so, use that IP-Address */ |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 126 | bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ? |
| 127 | endp->cfg->net_ports.bind_addr_v6 : |
| 128 | endp->cfg->net_ports.bind_addr_v4; |
| 129 | } else { |
| 130 | /* Choose any of the bind addresses, preferring v6 over v4 */ |
| 131 | bind_addr = endp->cfg->net_ports.bind_addr_v6; |
| 132 | if (!bind_addr) |
| 133 | bind_addr = endp->cfg->net_ports.bind_addr_v4; |
| 134 | } |
| 135 | if (bind_addr) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 136 | LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, |
| 137 | "using configured rtp bind ip as local bind ip %s\n", |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 138 | bind_addr); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 139 | } else { |
| 140 | /* No specific bind IP is configured for the RTP traffic, so |
| 141 | * assume the IP where we listen for incoming MGCP messages |
| 142 | * as bind IP */ |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 143 | bind_addr = endp->cfg->source_addr; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 144 | LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 145 | "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 146 | } |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 147 | osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 150 | /* This does not need to be a precision timestamp and |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 151 | * is allowed to wrap quite fast. The returned value is |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 152 | * 1/codec_rate seconds. */ |
| 153 | static uint32_t get_current_ts(unsigned codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 154 | { |
| 155 | struct timespec tp; |
| 156 | uint64_t ret; |
| 157 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 158 | if (!codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | |
| 161 | memset(&tp, 0, sizeof(tp)); |
| 162 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 163 | LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 164 | |
| 165 | /* convert it to 1/unit seconds */ |
| 166 | ret = tp.tv_sec; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 167 | ret *= codec_rate; |
| 168 | ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 173 | /*! send udp packet. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 174 | * \param[in] fd associated file descriptor. |
| 175 | * \param[in] addr destination ip-address. |
| 176 | * \param[in] port destination UDP port (network byte order). |
| 177 | * \param[in] buf buffer that holds the data to be send. |
| 178 | * \param[in] len length of the data to be sent. |
| 179 | * \returns bytes sent, -1 on error. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 180 | int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, char *buf, int len) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 181 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 182 | char ipbuf[INET6_ADDRSTRLEN]; |
| 183 | size_t addr_len; |
| 184 | bool is_ipv6 = addr->u.sa.sa_family == AF_INET6; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 185 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 186 | LOGP(DRTP, LOGL_DEBUG, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 187 | "sending %i bytes length packet to %s:%u ...\n", len, |
| 188 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 189 | ntohs(port)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 190 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 191 | if (is_ipv6) { |
| 192 | addr->u.sin6.sin6_port = port; |
| 193 | addr_len = sizeof(addr->u.sin6); |
| 194 | } else { |
| 195 | addr->u.sin.sin_port = port; |
| 196 | addr_len = sizeof(addr->u.sin); |
| 197 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 198 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 199 | return sendto(fd, buf, len, 0, &addr->u.sa, addr_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 202 | /*! send RTP dummy packet (to keep NAT connection open). |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 203 | * \param[in] endp mcgp endpoint that holds the RTP connection. |
| 204 | * \param[in] conn associated RTP connection. |
| 205 | * \returns bytes sent, -1 on error. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 206 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 207 | { |
| 208 | static char buf[] = { MGCP_DUMMY_LOAD }; |
| 209 | int rc; |
| 210 | int was_rtcp = 0; |
| 211 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 212 | OSMO_ASSERT(endp); |
| 213 | OSMO_ASSERT(conn); |
| 214 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 215 | LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n", |
| 216 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 217 | |
| 218 | rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, |
| 219 | conn->end.rtp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 220 | |
| 221 | if (rc == -1) |
| 222 | goto failed; |
| 223 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 224 | if (endp->trunk->omit_rtcp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 225 | return rc; |
| 226 | |
| 227 | was_rtcp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 228 | rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr, |
| 229 | conn->end.rtcp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 230 | |
| 231 | if (rc >= 0) |
| 232 | return rc; |
| 233 | |
| 234 | failed: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 235 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
| 236 | "Failed to send dummy %s packet.\n", |
| 237 | was_rtcp ? "RTCP" : "RTP"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 238 | |
| 239 | return -1; |
| 240 | } |
| 241 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 242 | /* Compute timestamp alignment error */ |
| 243 | static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate, |
| 244 | int ptime, uint32_t timestamp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 245 | { |
| 246 | int32_t timestamp_delta; |
| 247 | |
| 248 | if (ptime == 0) |
| 249 | return 0; |
| 250 | |
| 251 | /* Align according to: T - Tlast = k * Tptime */ |
| 252 | timestamp_delta = timestamp - sstate->last_timestamp; |
| 253 | |
| 254 | return timestamp_delta % ptime; |
| 255 | } |
| 256 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 257 | /* Check timestamp and sequence number for plausibility */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 258 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
| 259 | struct mgcp_rtp_state *state, |
| 260 | struct mgcp_rtp_stream_state *sstate, |
| 261 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 262 | struct osmo_sockaddr *addr, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 263 | uint16_t seq, uint32_t timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 264 | const char *text, int32_t * tsdelta_out) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 265 | { |
| 266 | int32_t tsdelta; |
| 267 | int32_t timestamp_error; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 268 | char ipbuf[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 269 | |
| 270 | /* Not fully intialized, skip */ |
| 271 | if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp) |
| 272 | return 0; |
| 273 | |
| 274 | if (seq == sstate->last_seq) { |
| 275 | if (timestamp != sstate->last_timestamp) { |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 276 | rate_ctr_inc(sstate->err_ts_ctr); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 277 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 278 | "The %s timestamp delta is != 0 but the sequence " |
| 279 | "number %d is the same, " |
| 280 | "TS offset: %d, SeqNo offset: %d " |
| 281 | "on SSRC: %u timestamp: %u " |
| 282 | "from %s:%d\n", |
| 283 | text, seq, |
| 284 | state->patch.timestamp_offset, state->patch.seq_offset, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 285 | sstate->ssrc, timestamp, |
| 286 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 287 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | tsdelta = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 293 | (int32_t)(timestamp - sstate->last_timestamp) / |
| 294 | (int16_t)(seq - sstate->last_seq); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 295 | |
| 296 | if (tsdelta == 0) { |
| 297 | /* Don't update *tsdelta_out */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 298 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 299 | "The %s timestamp delta is %d " |
| 300 | "on SSRC: %u timestamp: %u " |
| 301 | "from %s:%d\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 302 | text, tsdelta, sstate->ssrc, timestamp, |
| 303 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 304 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | if (sstate->last_tsdelta != tsdelta) { |
| 310 | if (sstate->last_tsdelta) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 311 | LOGPENDP(endp, DRTP, LOGL_INFO, |
| 312 | "The %s timestamp delta changes from %d to %d " |
| 313 | "on SSRC: %u timestamp: %u from %s:%d\n", |
| 314 | text, sstate->last_tsdelta, tsdelta, |
| 315 | sstate->ssrc, timestamp, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 316 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 317 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | if (tsdelta_out) |
| 322 | *tsdelta_out = tsdelta; |
| 323 | |
| 324 | timestamp_error = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 325 | ts_alignment_error(sstate, state->packet_duration, timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 326 | |
| 327 | if (timestamp_error) { |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 328 | rate_ctr_inc(sstate->err_ts_ctr); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 329 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 330 | "The %s timestamp has an alignment error of %d " |
| 331 | "on SSRC: %u " |
| 332 | "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d " |
| 333 | "from %s:%d. ptime: %d\n", |
| 334 | text, timestamp_error, |
| 335 | sstate->ssrc, |
| 336 | (int16_t)(seq - sstate->last_seq), |
| 337 | (int32_t)(timestamp - sstate->last_timestamp), |
| 338 | tsdelta, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 339 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 340 | osmo_sockaddr_port(&addr->u.sa), |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 341 | state->packet_duration); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 342 | } |
| 343 | return 1; |
| 344 | } |
| 345 | |
| 346 | /* Set the timestamp offset according to the packet duration. */ |
| 347 | static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 348 | struct mgcp_rtp_state *state, |
| 349 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 350 | struct osmo_sockaddr *addr, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 351 | int16_t delta_seq, uint32_t in_timestamp) |
| 352 | { |
| 353 | int32_t tsdelta = state->packet_duration; |
| 354 | int timestamp_offset; |
| 355 | uint32_t out_timestamp; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 356 | char ipbuf[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 357 | |
| 358 | if (tsdelta == 0) { |
| 359 | tsdelta = state->out_stream.last_tsdelta; |
| 360 | if (tsdelta != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 361 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 362 | "A fixed packet duration is not available, " |
| 363 | "using last output timestamp delta instead: %d " |
| 364 | "from %s:%d\n", tsdelta, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 365 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 366 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 367 | } else { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 368 | tsdelta = rtp_end->codec->rate * 20 / 1000; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 369 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 370 | "Fixed packet duration and last timestamp delta " |
| 371 | "are not available, " |
| 372 | "using fixed 20ms instead: %d " |
| 373 | "from %s:%d\n", tsdelta, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 374 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 375 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta; |
| 380 | timestamp_offset = out_timestamp - in_timestamp; |
| 381 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 382 | if (state->patch.timestamp_offset != timestamp_offset) { |
| 383 | state->patch.timestamp_offset = timestamp_offset; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 384 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 385 | "Timestamp offset change on SSRC: %u " |
| 386 | "SeqNo delta: %d, TS offset: %d, " |
| 387 | "from %s:%d\n", state->in_stream.ssrc, |
| 388 | delta_seq, state->patch.timestamp_offset, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 389 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 390 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return timestamp_offset; |
| 394 | } |
| 395 | |
| 396 | /* Set the timestamp offset according to the packet duration. */ |
| 397 | static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 398 | struct mgcp_rtp_state *state, |
| 399 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 400 | struct osmo_sockaddr *addr, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 401 | uint32_t timestamp) |
| 402 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 403 | char ipbuf[INET6_ADDRSTRLEN]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 404 | int ts_error = 0; |
| 405 | int ts_check = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 406 | int ptime = state->packet_duration; |
| 407 | |
| 408 | /* Align according to: T + Toffs - Tlast = k * Tptime */ |
| 409 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 410 | ts_error = ts_alignment_error(&state->out_stream, ptime, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 411 | timestamp + state->patch.timestamp_offset); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 412 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 413 | /* If there is an alignment error, we have to compensate it */ |
| 414 | if (ts_error) { |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 415 | state->patch.timestamp_offset += ptime - ts_error; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 416 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 417 | "Corrected timestamp alignment error of %d on SSRC: %u " |
| 418 | "new TS offset: %d, " |
| 419 | "from %s:%d\n", |
| 420 | ts_error, state->in_stream.ssrc, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 421 | state->patch.timestamp_offset, |
| 422 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 423 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 426 | /* Check we really managed to compensate the timestamp |
| 427 | * offset. There should not be any remaining error, failing |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 428 | * here would point to a serous problem with the alignment |
| 429 | * error computation function */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 430 | ts_check = ts_alignment_error(&state->out_stream, ptime, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 431 | timestamp + state->patch.timestamp_offset); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 432 | OSMO_ASSERT(ts_check == 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 433 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 434 | /* Return alignment error before compensation */ |
| 435 | return ts_error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 438 | /*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb). |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 439 | * \param[in] associated endpoint. |
| 440 | * \param[in] destination RTP end. |
| 441 | * \param[in,out] pointer to buffer with voice data. |
| 442 | * \param[in] voice data length. |
| 443 | * \param[in] maximum size of caller provided voice data buffer. |
| 444 | * \returns ignores input parameters, return always 0. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 445 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, |
| 446 | struct mgcp_rtp_end *dst_end, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 447 | char *data, int *len, int buf_size) |
| 448 | { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 449 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 453 | /*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb). |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 454 | * \param[in] associated endpoint. |
| 455 | * \param[in] destination RTP connnection. |
| 456 | * \param[in] source RTP connection. |
| 457 | * \returns ignores input parameters, return always 0. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 458 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 459 | struct mgcp_conn_rtp *conn_dst, |
| 460 | struct mgcp_conn_rtp *conn_src) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 461 | { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 462 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 467 | const struct mgcp_rtp_codec **codec, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 468 | const char **fmtp_extra, |
| 469 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 470 | { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 471 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n", |
| 472 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 473 | |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 474 | *codec = conn->end.codec; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 475 | *fmtp_extra = conn->end.fmtp_extra; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 478 | void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, |
| 479 | struct mgcp_rtp_state *state, const uint16_t seq, |
| 480 | const int32_t transit, const uint32_t ssrc) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 481 | { |
| 482 | int32_t d; |
| 483 | |
| 484 | /* initialize or re-initialize */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 485 | if (!state->stats.initialized || state->stats.ssrc != ssrc) { |
| 486 | state->stats.initialized = 1; |
| 487 | state->stats.base_seq = seq; |
| 488 | state->stats.max_seq = seq - 1; |
| 489 | state->stats.ssrc = ssrc; |
| 490 | state->stats.jitter = 0; |
| 491 | state->stats.transit = transit; |
| 492 | state->stats.cycles = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 493 | } else { |
| 494 | uint16_t udelta; |
| 495 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 496 | /* The below takes the shape of the validation of |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 497 | * Appendix A. Check if there is something weird with |
| 498 | * the sequence number, otherwise check for a wrap |
| 499 | * around in the sequence number. |
| 500 | * It can't wrap during the initialization so let's |
| 501 | * skip it here. The Appendix A probably doesn't have |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 502 | * this issue because of the probation. */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 503 | udelta = seq - state->stats.max_seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 504 | if (udelta < RTP_MAX_DROPOUT) { |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 505 | if (seq < state->stats.max_seq) |
| 506 | state->stats.cycles += RTP_SEQ_MOD; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 507 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 508 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 509 | "RTP seqno made a very large jump on delta: %u\n", |
| 510 | udelta); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 514 | /* Calculate the jitter between the two packages. The TS should be |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 515 | * taken closer to the read function. This was taken from the |
| 516 | * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 517 | * resolution. */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 518 | d = transit - state->stats.transit; |
| 519 | state->stats.transit = transit; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 520 | if (d < 0) |
| 521 | d = -d; |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 522 | state->stats.jitter += d - ((state->stats.jitter + 8) >> 4); |
| 523 | state->stats.max_seq = seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 526 | /* There may be different payload type numbers negotiated for two connections. |
| 527 | * Patch the payload type of an RTP packet so that it uses the payload type |
| 528 | * that is valid for the destination connection (conn_dst) */ |
| 529 | static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 530 | struct mgcp_conn_rtp *conn_dst, struct msgb *msg) |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 531 | { |
| 532 | struct rtp_hdr *rtp_hdr; |
| 533 | uint8_t pt_in; |
| 534 | int pt_out; |
| 535 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 536 | if (msgb_length(msg) < sizeof(struct rtp_hdr)) { |
| 537 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n", |
| 538 | msgb_length(msg), sizeof(struct rtp_hdr)); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 539 | return -EINVAL; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 540 | } |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 541 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 542 | rtp_hdr = (struct rtp_hdr *)msgb_data(msg); |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 543 | |
| 544 | pt_in = rtp_hdr->payload_type; |
| 545 | pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in); |
| 546 | if (pt_out < 0) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | rtp_hdr->payload_type = (uint8_t) pt_out; |
| 550 | return 0; |
| 551 | } |
| 552 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 553 | /* The RFC 3550 Appendix A assumes there are multiple sources but |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 554 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
| 555 | * one source and this code will patch RTP header to appear as if there |
| 556 | * is only one source. |
| 557 | * There is also no probation period for new sources. Every RTP header |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 558 | * we receive will be seen as a switch in streams. */ |
| 559 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, |
| 560 | struct mgcp_rtp_state *state, |
| 561 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 562 | struct osmo_sockaddr *addr, struct msgb *msg) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 563 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 564 | char ipbuf[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 565 | uint32_t arrival_time; |
| 566 | int32_t transit; |
| 567 | uint16_t seq; |
| 568 | uint32_t timestamp, ssrc; |
| 569 | struct rtp_hdr *rtp_hdr; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 570 | int payload = rtp_end->codec->payload_type; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 571 | unsigned int len = msgb_length(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 572 | |
| 573 | if (len < sizeof(*rtp_hdr)) |
| 574 | return; |
| 575 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 576 | rtp_hdr = (struct rtp_hdr *)msgb_data(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 577 | seq = ntohs(rtp_hdr->sequence); |
| 578 | timestamp = ntohl(rtp_hdr->timestamp); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 579 | arrival_time = get_current_ts(rtp_end->codec->rate); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 580 | ssrc = ntohl(rtp_hdr->ssrc); |
| 581 | transit = arrival_time - timestamp; |
| 582 | |
| 583 | mgcp_rtp_annex_count(endp, state, seq, transit, ssrc); |
| 584 | |
| 585 | if (!state->initialized) { |
| 586 | state->initialized = 1; |
| 587 | state->in_stream.last_seq = seq - 1; |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 588 | state->in_stream.ssrc = state->patch.orig_ssrc = ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 589 | state->in_stream.last_tsdelta = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 590 | state->packet_duration = |
| 591 | mgcp_rtp_packet_duration(endp, rtp_end); |
Philipp Maier | 0ec1d4e | 2018-05-16 11:09:42 +0200 | [diff] [blame] | 592 | state->out_stream.last_seq = seq - 1; |
| 593 | state->out_stream.ssrc = state->patch.orig_ssrc = ssrc; |
| 594 | state->out_stream.last_tsdelta = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 595 | state->out_stream.last_timestamp = timestamp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 596 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 597 | LOGPENDP(endp, DRTP, LOGL_INFO, |
| 598 | "initializing stream, SSRC: %u timestamp: %u " |
| 599 | "pkt-duration: %d, from %s:%d\n", |
| 600 | state->in_stream.ssrc, |
| 601 | state->patch.seq_offset, state->packet_duration, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 602 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 603 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 604 | if (state->packet_duration == 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 605 | state->packet_duration = |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 606 | rtp_end->codec->rate * 20 / 1000; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 607 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 608 | "fixed packet duration is not available, " |
| 609 | "using fixed 20ms instead: %d from %s:%d\n", |
| 610 | state->packet_duration, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 611 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 612 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 613 | } |
| 614 | } else if (state->in_stream.ssrc != ssrc) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 615 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 616 | "SSRC changed: %u -> %u " |
| 617 | "from %s:%d\n", |
| 618 | state->in_stream.ssrc, rtp_hdr->ssrc, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 619 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 620 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 621 | |
| 622 | state->in_stream.ssrc = ssrc; |
| 623 | if (rtp_end->force_constant_ssrc) { |
| 624 | int16_t delta_seq; |
| 625 | |
| 626 | /* Always increment seqno by 1 */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 627 | state->patch.seq_offset = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 628 | (state->out_stream.last_seq + 1) - seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 629 | |
| 630 | /* Estimate number of packets that would have been sent */ |
| 631 | delta_seq = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 632 | (arrival_time - state->in_stream.last_arrival_time |
| 633 | + state->packet_duration / 2) / |
| 634 | state->packet_duration; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 635 | |
| 636 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 637 | delta_seq, timestamp); |
| 638 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 639 | state->patch.patch_ssrc = 1; |
| 640 | ssrc = state->patch.orig_ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 641 | if (rtp_end->force_constant_ssrc != -1) |
| 642 | rtp_end->force_constant_ssrc -= 1; |
| 643 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 644 | LOGPENDP(endp, DRTP, LOGL_NOTICE, |
| 645 | "SSRC patching enabled, SSRC: %u " |
| 646 | "SeqNo offset: %d, TS offset: %d " |
| 647 | "from %s:%d\n", state->in_stream.ssrc, |
| 648 | state->patch.seq_offset, state->patch.timestamp_offset, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 649 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf), |
| 650 | osmo_sockaddr_port(&addr->u.sa)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | state->in_stream.last_tsdelta = 0; |
| 654 | } else { |
| 655 | /* Compute current per-packet timestamp delta */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 656 | check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, |
| 657 | addr, seq, timestamp, "input", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 658 | &state->in_stream.last_tsdelta); |
| 659 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 660 | if (state->patch.patch_ssrc) |
| 661 | ssrc = state->patch.orig_ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* Save before patching */ |
| 665 | state->in_stream.last_timestamp = timestamp; |
| 666 | state->in_stream.last_seq = seq; |
| 667 | state->in_stream.last_arrival_time = arrival_time; |
| 668 | |
| 669 | if (rtp_end->force_aligned_timing && |
| 670 | state->out_stream.ssrc == ssrc && state->packet_duration) |
| 671 | /* Align the timestamp offset */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 672 | align_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 673 | timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 674 | |
| 675 | /* Store the updated SSRC back to the packet */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 676 | if (state->patch.patch_ssrc) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 677 | rtp_hdr->ssrc = htonl(ssrc); |
| 678 | |
| 679 | /* Apply the offset and store it back to the packet. |
| 680 | * This won't change anything if the offset is 0, so the conditional is |
| 681 | * omitted. */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 682 | seq += state->patch.seq_offset; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 683 | rtp_hdr->sequence = htons(seq); |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 684 | timestamp += state->patch.timestamp_offset; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 685 | rtp_hdr->timestamp = htonl(timestamp); |
| 686 | |
| 687 | /* Check again, whether the timestamps are still valid */ |
| 688 | if (state->out_stream.ssrc == ssrc) |
| 689 | check_rtp_timestamp(endp, state, &state->out_stream, rtp_end, |
| 690 | addr, seq, timestamp, "output", |
| 691 | &state->out_stream.last_tsdelta); |
| 692 | |
| 693 | /* Save output values */ |
| 694 | state->out_stream.last_seq = seq; |
| 695 | state->out_stream.last_timestamp = timestamp; |
| 696 | state->out_stream.ssrc = ssrc; |
| 697 | |
| 698 | if (payload < 0) |
| 699 | return; |
| 700 | |
| 701 | #if 0 |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 702 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n", |
| 703 | rtp_hdr->payload_type, payload); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 704 | rtp_hdr->payload_type = payload; |
| 705 | #endif |
| 706 | } |
| 707 | |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 708 | /* There are different dialects used to format and transfer voice data. When |
| 709 | * the receiving end expects GSM-HR data to be formated after RFC 5993, this |
| 710 | * function is used to convert between RFC 5993 and TS 101318, which we normally |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 711 | * use. |
| 712 | * Return 0 on sucess, negative on errors like invalid data length. */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 713 | static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg) |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 714 | { |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 715 | struct rtp_hdr *rtp_hdr; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 716 | if (msgb_length(msg) < sizeof(struct rtp_hdr)) { |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 717 | LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n", |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 718 | msgb_length(msg), sizeof(struct rtp_hdr)); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 719 | return -EINVAL; |
| 720 | } |
| 721 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 722 | rtp_hdr = (struct rtp_hdr *)msgb_data(msg); |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 723 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 724 | if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) { |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 725 | /* TS 101318 encoding => RFC 5993 encoding */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 726 | msgb_put(msg, 1); |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 727 | memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES); |
| 728 | rtp_hdr->data[0] = 0x00; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 729 | } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) { |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 730 | /* RFC 5993 encoding => TS 101318 encoding */ |
| 731 | memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 732 | msgb_trim(msg, msgb_length(msg) - 1); |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 733 | } else { |
| 734 | /* It is possible that multiple payloads occur in one RTP |
| 735 | * packet. This is not supported yet. */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 736 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 737 | "cannot figure out how to convert RTP packet\n"); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 738 | return -ENOTSUP; |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 739 | } |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 740 | return 0; |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 743 | /* For AMR RTP two framing modes are defined RFC3267. There is a bandwith |
| 744 | * efficient encoding scheme where all fields are packed together one after |
| 745 | * another and an octet aligned mode where all fields are aligned to octet |
| 746 | * boundaries. This function is used to convert between the two modes */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 747 | static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg, |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 748 | bool target_is_oa) |
| 749 | { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 750 | /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 751 | * plenty of space available to store the slightly larger, converted |
| 752 | * data */ |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 753 | struct rtp_hdr *rtp_hdr; |
| 754 | unsigned int payload_len; |
| 755 | int rc; |
| 756 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 757 | if (msgb_length(msg) < sizeof(struct rtp_hdr)) { |
| 758 | LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n", msgb_length(msg), sizeof(struct rtp_hdr)); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 759 | return -EINVAL; |
| 760 | } |
| 761 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 762 | rtp_hdr = (struct rtp_hdr *)msgb_data(msg); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 763 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 764 | payload_len = msgb_length(msg) - sizeof(struct rtp_hdr); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 765 | |
| 766 | if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) { |
| 767 | if (!target_is_oa) |
| 768 | /* Input data is oa an target format is bwe |
| 769 | * ==> convert */ |
| 770 | rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len); |
| 771 | else |
| 772 | /* Input data is already bew, but we accept it anyway |
| 773 | * ==> no conversion needed */ |
| 774 | rc = payload_len; |
| 775 | } else { |
| 776 | if (target_is_oa) |
| 777 | /* Input data is bwe an target format is oa |
| 778 | * ==> convert */ |
| 779 | rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len, |
| 780 | RTP_BUF_SIZE); |
| 781 | else |
| 782 | /* Input data is already oa, but we accept it anyway |
| 783 | * ==> no conversion needed */ |
| 784 | rc = payload_len; |
| 785 | } |
| 786 | if (rc < 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 787 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 788 | "AMR RTP packet conversion failed\n"); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 789 | return -EINVAL; |
| 790 | } |
| 791 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 792 | return msgb_trim(msg, rc + sizeof(struct rtp_hdr)); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* Check if a conversion between octet-aligned and bandwith-efficient mode is |
| 796 | * indicated. */ |
| 797 | static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec) |
| 798 | { |
| 799 | if (codec->param_present == false) |
| 800 | return false; |
| 801 | if (!codec->param.amr_octet_aligned_present) |
| 802 | return false; |
| 803 | if (strcmp(codec->subtype_name, "AMR") != 0) |
| 804 | return false; |
| 805 | return true; |
| 806 | } |
| 807 | |
| 808 | |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 809 | /* Return whether an RTP packet with AMR payload is in octet-aligned mode. |
| 810 | * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */ |
| 811 | static int amr_oa_check(char *data, int len) |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 812 | { |
| 813 | struct rtp_hdr *rtp_hdr; |
| 814 | unsigned int payload_len; |
| 815 | |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 816 | if (len < sizeof(struct rtp_hdr)) |
| 817 | return -EINVAL; |
| 818 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 819 | rtp_hdr = (struct rtp_hdr *)data; |
| 820 | |
| 821 | payload_len = len - sizeof(struct rtp_hdr); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 822 | if (payload_len < sizeof(struct amr_hdr)) |
| 823 | return -EINVAL; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 824 | |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 825 | return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 828 | /* Forward data to a debug tap. This is debug function that is intended for |
| 829 | * debugging the voice traffic with tools like gstreamer */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 830 | static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 831 | { |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 832 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 833 | |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 834 | if (!tap->enabled) |
| 835 | return; |
| 836 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 837 | rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward, |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 838 | sizeof(tap->forward)); |
| 839 | |
| 840 | if (rc < 0) |
| 841 | LOGP(DRTP, LOGL_ERROR, |
| 842 | "Forwarding tapped (debug) voice data failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 845 | /* Generate an RTP header if it is missing */ |
| 846 | static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end, |
| 847 | struct mgcp_rtp_state *state) |
| 848 | { |
| 849 | struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg); |
| 850 | |
| 851 | if (hdr->version > 0) |
| 852 | return; |
| 853 | |
| 854 | hdr->version = 2; |
| 855 | hdr->payload_type = rtp_end->codec->payload_type; |
| 856 | hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate)); |
| 857 | hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence); |
| 858 | hdr->ssrc = state->alt_rtp_tx_ssrc; |
| 859 | } |
| 860 | |
| 861 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 862 | /*! Send RTP/RTCP data to a specified destination connection. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 863 | * \param[in] endp associated endpoint (for configuration, logging). |
| 864 | * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP. |
| 865 | * \param[in] spoofed source address (set to NULL to disable). |
| 866 | * \param[in] buf buffer that contains the RTP/RTCP data. |
| 867 | * \param[in] len length of the buffer that contains the RTP/RTCP data. |
| 868 | * \param[in] conn_src associated source connection. |
| 869 | * \param[in] conn_dst associated destination connection. |
| 870 | * \returns 0 on success, -1 on ERROR. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 871 | int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 872 | struct msgb *msg, struct mgcp_conn_rtp *conn_src, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 873 | struct mgcp_conn_rtp *conn_dst) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 874 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 875 | /*! When no destination connection is available (e.g. when only one |
| 876 | * connection in loopback mode exists), then the source connection |
| 877 | * shall be specified as destination connection */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 878 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 879 | struct mgcp_trunk *trunk = endp->trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 880 | struct mgcp_rtp_end *rtp_end; |
| 881 | struct mgcp_rtp_state *rtp_state; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 882 | char ipbuf[INET6_ADDRSTRLEN]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 883 | char *dest_name; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 884 | int rc; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 885 | int len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 886 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 887 | OSMO_ASSERT(conn_src); |
| 888 | OSMO_ASSERT(conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 889 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 890 | if (is_rtp) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 891 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 892 | } else { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 893 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 894 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 895 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 896 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n", |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 897 | trunk->audio_loop, conn_src->conn->mode, |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 898 | conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 899 | |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 900 | /* FIXME: It is legal that the payload type on the egress connection is |
| 901 | * different from the payload type that has been negotiated on the |
| 902 | * ingress connection. Essentially the codecs are the same so we can |
| 903 | * match them and patch the payload type. However, if we can not find |
| 904 | * the codec pendant (everything ist equal except the PT), we are of |
| 905 | * course unable to patch the payload type. A situation like this |
| 906 | * should not occur if transcoding is consequently avoided. Until |
| 907 | * we have transcoding support in osmo-mgw we can not resolve this. */ |
Philipp Maier | da895b1 | 2018-08-03 12:16:37 +0200 | [diff] [blame] | 908 | if (is_rtp) { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 909 | rc = mgcp_patch_pt(conn_src, conn_dst, msg); |
Philipp Maier | da895b1 | 2018-08-03 12:16:37 +0200 | [diff] [blame] | 910 | if (rc < 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 911 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 912 | "can not patch PT because no suitable egress codec was found.\n"); |
Philipp Maier | da895b1 | 2018-08-03 12:16:37 +0200 | [diff] [blame] | 913 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 916 | /* Note: In case of loopback configuration, both, the source and the |
| 917 | * destination will point to the same connection. */ |
| 918 | rtp_end = &conn_dst->end; |
| 919 | rtp_state = &conn_src->state; |
| 920 | dest_name = conn_dst->conn->name; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 921 | |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 922 | /* Ensure we have an alternative SSRC in case we need it, see also |
| 923 | * gen_rtp_header() */ |
| 924 | if (rtp_state->alt_rtp_tx_ssrc == 0) |
| 925 | rtp_state->alt_rtp_tx_ssrc = rand(); |
| 926 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 927 | if (!rtp_end->output_enabled) { |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 928 | rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 929 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 930 | "output disabled, drop to %s %s " |
| 931 | "rtp_port:%u rtcp_port:%u\n", |
| 932 | dest_name, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 933 | osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf), |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 934 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 935 | ); |
| 936 | } else if (is_rtp) { |
| 937 | int cont; |
| 938 | int nbytes = 0; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 939 | int buflen = msgb_length(msg); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 940 | |
| 941 | /* Make sure we have a valid RTP header, in cases where no RTP |
| 942 | * header is present, we will generate one. */ |
| 943 | gen_rtp_header(msg, rtp_end, rtp_state); |
| 944 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 945 | do { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 946 | /* Run transcoder */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 947 | cont = endp->cfg->rtp_processing_cb(endp, rtp_end, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 948 | (char*)msgb_data(msg), &buflen, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 949 | RTP_BUF_SIZE); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 950 | if (cont < 0) |
| 951 | break; |
| 952 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 953 | if (addr) |
| 954 | mgcp_patch_and_count(endp, rtp_state, rtp_end, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 955 | addr, msg); |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 956 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 957 | if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 958 | rc = amr_oa_bwe_convert(endp, msg, |
Neels Hofmeyr | 5a6220f | 2019-08-20 03:09:04 +0200 | [diff] [blame] | 959 | conn_dst->end.codec->param.amr_octet_aligned); |
| 960 | if (rc < 0) { |
| 961 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 962 | "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n"); |
| 963 | break; |
| 964 | } |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 965 | } |
| 966 | else if (rtp_end->rfc5993_hr_convert |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 967 | && strcmp(conn_src->end.codec->subtype_name, |
Neels Hofmeyr | 5a6220f | 2019-08-20 03:09:04 +0200 | [diff] [blame] | 968 | "GSM-HR-08") == 0) { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 969 | rc = rfc5993_hr_convert(endp, msg); |
Neels Hofmeyr | 5a6220f | 2019-08-20 03:09:04 +0200 | [diff] [blame] | 970 | if (rc < 0) { |
| 971 | LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n"); |
| 972 | break; |
| 973 | } |
| 974 | } |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 975 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 976 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 977 | "process/send to %s %s " |
| 978 | "rtp_port:%u rtcp_port:%u\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 979 | dest_name, |
| 980 | osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf), |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 981 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
| 982 | ); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 983 | |
| 984 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 985 | forward_data(rtp_end->rtp.fd, &conn_src->tap_out, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 986 | msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 987 | |
| 988 | /* FIXME: HACK HACK HACK. See OS#2459. |
| 989 | * The ip.access nano3G needs the first RTP payload's first two bytes to read hex |
| 990 | * 'e400', or it will reject the RAB assignment. It seems to not harm other femto |
| 991 | * cells (as long as we patch only the first RTP payload in each stream). |
| 992 | */ |
Neels Hofmeyr | 35a3829 | 2018-07-23 18:29:04 +0200 | [diff] [blame] | 993 | if (!rtp_state->patched_first_rtp_payload |
| 994 | && conn_src->conn->mode == MGCP_CONN_LOOPBACK) { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 995 | uint8_t *data = msgb_data(msg) + 12; |
Neels Hofmeyr | 35a3829 | 2018-07-23 18:29:04 +0200 | [diff] [blame] | 996 | if (data[0] == 0xe0) { |
| 997 | data[0] = 0xe4; |
| 998 | data[1] = 0x00; |
| 999 | rtp_state->patched_first_rtp_payload = true; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1000 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 1001 | "Patching over first two bytes" |
| 1002 | " to fake an IuUP Initialization Ack\n"); |
Neels Hofmeyr | 35a3829 | 2018-07-23 18:29:04 +0200 | [diff] [blame] | 1003 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1006 | len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port, |
| 1007 | (char*)msgb_data(msg), msgb_length(msg)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1008 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1009 | if (len <= 0) |
| 1010 | return len; |
| 1011 | |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 1012 | rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR); |
| 1013 | rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1014 | rtp_state->alt_rtp_tx_sequence++; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1015 | |
| 1016 | nbytes += len; |
| 1017 | buflen = cont; |
| 1018 | } while (buflen > 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1019 | return nbytes; |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1020 | } else if (!trunk->omit_rtcp) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1021 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 1022 | "send to %s %s rtp_port:%u rtcp_port:%u\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1023 | dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf), |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1024 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
| 1025 | ); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1026 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1027 | len = mgcp_udp_send(rtp_end->rtcp.fd, |
| 1028 | &rtp_end->addr, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1029 | rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1030 | |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 1031 | rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR); |
| 1032 | rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1033 | rtp_state->alt_rtp_tx_sequence++; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1034 | |
| 1035 | return len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1041 | /* Check if the origin (addr) matches the address/port data of the RTP |
| 1042 | * connections. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1043 | static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1044 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1045 | char ipbuf[INET6_ADDRSTRLEN]; |
| 1046 | |
| 1047 | if (addr_is_any(&conn->end.addr)) { |
Neels Hofmeyr | efd645e | 2018-09-10 13:14:50 +0200 | [diff] [blame] | 1048 | switch (conn->conn->mode) { |
| 1049 | case MGCP_CONN_LOOPBACK: |
| 1050 | /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP |
| 1051 | * message received. We currently hackishly accomplish that by putting the endpoint in |
| 1052 | * loopback mode and patching over the looped back RTP message to make it look like an |
| 1053 | * ack. We don't know the femto cell's IP address and port until the RAB Assignment |
| 1054 | * Response is received, but the nano3G expects an IuUP Initialization Ack before it even |
| 1055 | * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the |
| 1056 | * MGCP port is in loopback mode, allow looping back the packet to any source. */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1057 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
| 1058 | "In loopback mode and remote address not set:" |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1059 | " allowing data from address: %s\n", |
| 1060 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf)); |
Neels Hofmeyr | efd645e | 2018-09-10 13:14:50 +0200 | [diff] [blame] | 1061 | return 0; |
| 1062 | |
| 1063 | default: |
| 1064 | /* Receiving early media before the endpoint is configured. Instead of logging |
| 1065 | * this as an error that occurs on every call, keep it more low profile to not |
| 1066 | * confuse humans with expected errors. */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1067 | LOGPCONN(conn->conn, DRTP, LOGL_INFO, |
| 1068 | "Rx RTP from %s, but remote address not set:" |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1069 | " dropping early media\n", |
| 1070 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf)); |
Neels Hofmeyr | efd645e | 2018-09-10 13:14:50 +0200 | [diff] [blame] | 1071 | return -1; |
| 1072 | } |
Neels Hofmeyr | 0063ca2 | 2018-07-23 18:12:16 +0200 | [diff] [blame] | 1073 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1074 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1075 | /* Note: Check if the inbound RTP data comes from the same host to |
| 1076 | * which we send our outgoing RTP traffic. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1077 | if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family || |
| 1078 | (conn->end.addr.u.sa.sa_family == AF_INET && |
| 1079 | conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) || |
| 1080 | (conn->end.addr.u.sa.sa_family == AF_INET6 && |
| 1081 | memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr, |
| 1082 | sizeof(struct in6_addr)))) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1083 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1084 | "data from wrong address: %s, ", |
| 1085 | osmo_sockaddr_ntop(&addr->u.sa, ipbuf)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1086 | LOGPC(DRTP, LOGL_ERROR, "expected: %s\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1087 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf)); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1088 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1089 | return -1; |
| 1090 | } |
| 1091 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1092 | /* Note: Usually the remote remote port of the data we receive will be |
| 1093 | * the same as the remote port where we transmit outgoing RTP traffic |
| 1094 | * to (set by MDCX). We use this to check the origin of the data for |
| 1095 | * plausibility. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1096 | if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) && |
| 1097 | ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1098 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1099 | "data from wrong source port: %d, ", |
| 1100 | osmo_sockaddr_port(&addr->u.sa)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1101 | LOGPC(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1102 | "expected: %d for RTP or %d for RTCP\n", |
| 1103 | ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port)); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1104 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1105 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1111 | /* Check the if the destination address configuration of an RTP connection |
| 1112 | * makes sense */ |
| 1113 | static int check_rtp_destin(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1114 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1115 | char ipbuf[INET6_ADDRSTRLEN]; |
| 1116 | bool ip_is_any = addr_is_any(&conn->end.addr); |
| 1117 | |
Philipp Maier | e6df0e4 | 2018-05-29 14:03:06 +0200 | [diff] [blame] | 1118 | /* Note: it is legal to create a connection but never setting a port |
| 1119 | * and IP-address for outgoing data. */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1120 | if (ip_is_any && conn->end.rtp_port == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1121 | LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, |
| 1122 | "destination IP-address and rtp port is (not yet) known (%s:%u)\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1123 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port); |
Philipp Maier | e6df0e4 | 2018-05-29 14:03:06 +0200 | [diff] [blame] | 1124 | return -1; |
| 1125 | } |
| 1126 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1127 | if (ip_is_any) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1128 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
| 1129 | "destination IP-address is invalid (%s:%u)\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1130 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1131 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1132 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1133 | |
| 1134 | if (conn->end.rtp_port == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1135 | LOGPCONN(conn->conn, DRTP, LOGL_ERROR, |
| 1136 | "destination rtp port is invalid (%s:%u)\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1137 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1138 | return -1; |
| 1139 | } |
| 1140 | |
| 1141 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1144 | /* Do some basic checks to make sure that the RTCP packets we are going to |
| 1145 | * process are not complete garbage */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1146 | static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg) |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1147 | { |
| 1148 | struct rtcp_hdr *hdr; |
| 1149 | unsigned int len; |
| 1150 | uint8_t type; |
| 1151 | |
| 1152 | /* RTPC packets that are just a header without data do not make |
| 1153 | * any sense. */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1154 | if (msgb_length(msg) < sizeof(struct rtcp_hdr)) { |
| 1155 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n", |
| 1156 | msgb_length(msg), sizeof(struct rtcp_hdr)); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1157 | return -EINVAL; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1158 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1159 | |
| 1160 | /* Make sure that the length of the received packet does not exceed |
| 1161 | * the available buffer size */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1162 | hdr = (struct rtcp_hdr *)msgb_data(msg); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1163 | len = (osmo_ntohs(hdr->length) + 1) * 4; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1164 | if (len > msgb_length(msg)) { |
| 1165 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n", |
| 1166 | len, msgb_length(msg)); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1167 | return -EINVAL; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1168 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1169 | |
| 1170 | /* Make sure we accept only packets that have a proper packet type set |
| 1171 | * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
| 1172 | type = hdr->type; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1173 | if ((type < 192 || type > 195) && (type < 200 || type > 213)) { |
| 1174 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1175 | return -EINVAL; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1176 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | /* Do some basic checks to make sure that the RTP packets we are going to |
| 1182 | * process are not complete garbage */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1183 | static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg) |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1184 | { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1185 | size_t min_size = sizeof(struct rtp_hdr); |
| 1186 | if (msgb_length(msg) < min_size) { |
| 1187 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n", |
| 1188 | msgb_length(msg), min_size); |
| 1189 | return -1; |
| 1190 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 1191 | |
| 1192 | /* FIXME: Add more checks, the reason why we do not check more than |
| 1193 | * the length is because we currently handle IUUP packets as RTP |
| 1194 | * packets, so they must pass this check, if we weould be more |
| 1195 | * strict here, we would possibly break 3G. (see also FIXME note |
| 1196 | * below */ |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1201 | /* Send RTP data. Possible options are standard RTP packet |
| 1202 | * transmission or trsmission via an osmux connection */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1203 | static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1204 | { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1205 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 1206 | enum rtp_proto proto = mc->proto; |
| 1207 | struct mgcp_conn_rtp *conn_src = mc->conn_src; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1208 | struct mgcp_endpoint *endp = conn_src->conn->endp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1209 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1210 | LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n", |
| 1211 | mgcp_conn_dump(conn_dst->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* Before we try to deliver the packet, we check if the destination |
| 1214 | * port and IP-Address make sense at all. If not, we will be unable |
| 1215 | * to deliver the packet. */ |
| 1216 | if (check_rtp_destin(conn_dst) != 0) |
| 1217 | return -1; |
| 1218 | |
| 1219 | /* Depending on the RTP connection type, deliver the RTP packet to the |
| 1220 | * destination connection. */ |
| 1221 | switch (conn_dst->type) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1222 | case MGCP_RTP_DEFAULT: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1223 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 1224 | "endpoint type is MGCP_RTP_DEFAULT, " |
| 1225 | "using mgcp_send() to forward data directly\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1226 | return mgcp_send(endp, proto == MGCP_PROTO_RTP, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1227 | mc->from_addr, msg, conn_src, conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1228 | case MGCP_OSMUX_BSC_NAT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1229 | case MGCP_OSMUX_BSC: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1230 | LOGPENDP(endp, DRTP, LOGL_DEBUG, |
| 1231 | "endpoint type is MGCP_OSMUX_BSC_NAT, " |
| 1232 | "using osmux_xfrm_to_osmux() to forward data through OSMUX\n"); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1233 | return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1236 | /* If the data has not been handled/forwarded until here, it will |
| 1237 | * be discarded, this should not happen, normally the MGCP type |
| 1238 | * should be properly set */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1239 | LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1240 | |
| 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | /*! dispatch incoming RTP packet to opposite RTP connection. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 1245 | * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP). |
| 1246 | * \param[in] addr socket address where the RTP packet has been received from. |
| 1247 | * \param[in] buf buffer that hold the RTP payload. |
| 1248 | * \param[in] buf_size size data length of buf. |
| 1249 | * \param[in] conn originating connection. |
| 1250 | * \returns 0 on success, -1 on ERROR. */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1251 | int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1252 | { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1253 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 1254 | struct mgcp_conn_rtp *conn_src = mc->conn_src; |
| 1255 | struct mgcp_conn *conn = conn_src->conn; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1256 | struct mgcp_conn *conn_dst; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1257 | struct osmo_sockaddr *from_addr = mc->from_addr; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1258 | |
| 1259 | /*! NOTE: This callback function implements the endpoint specific |
Alexander Chemeris | 61cf9bb | 2020-05-11 18:13:53 +0300 | [diff] [blame] | 1260 | * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1261 | * that the endpoint will hold only two connections. This premise |
| 1262 | * is used to determine the opposite connection (it is always the |
| 1263 | * connection that is not the originating connection). Once the |
| 1264 | * destination connection is known the RTP packet is sent via |
| 1265 | * the destination connection. */ |
| 1266 | |
Pau Espin Pedrol | 9aaaab6 | 2019-05-15 23:23:27 +0200 | [diff] [blame] | 1267 | |
| 1268 | /* Check if the connection is in loopback mode, if yes, just send the |
| 1269 | * incoming data back to the origin */ |
| 1270 | if (conn->mode == MGCP_CONN_LOOPBACK) { |
| 1271 | /* When we are in loopback mode, we loop back all incoming |
| 1272 | * packets back to their origin. We will use the originating |
| 1273 | * address data from the UDP packet header to patch the |
| 1274 | * outgoing address in connection on the fly */ |
| 1275 | if (conn->u.rtp.end.rtp_port == 0) { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1276 | OSMO_ASSERT(conn->u.rtp.end.addr.u.sa.sa_family == from_addr->u.sa.sa_family); |
| 1277 | switch (from_addr->u.sa.sa_family) { |
| 1278 | case AF_INET: |
| 1279 | conn->u.rtp.end.addr.u.sin.sin_addr = from_addr->u.sin.sin_addr; |
| 1280 | conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port; |
| 1281 | break; |
| 1282 | case AF_INET6: |
| 1283 | conn->u.rtp.end.addr.u.sin6.sin6_addr = from_addr->u.sin6.sin6_addr; |
| 1284 | conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port; |
| 1285 | break; |
| 1286 | default: |
| 1287 | OSMO_ASSERT(false); |
| 1288 | } |
Pau Espin Pedrol | 9aaaab6 | 2019-05-15 23:23:27 +0200 | [diff] [blame] | 1289 | } |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1290 | return mgcp_send_rtp(conn_src, msg); |
Pau Espin Pedrol | 9aaaab6 | 2019-05-15 23:23:27 +0200 | [diff] [blame] | 1291 | } |
| 1292 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1293 | /* Find a destination connection. */ |
| 1294 | /* NOTE: This code path runs every time an RTP packet is received. The |
| 1295 | * function mgcp_find_dst_conn() we use to determine the detination |
| 1296 | * connection will iterate the connection list inside the endpoint. |
| 1297 | * Since list iterations are quite costly, we will figure out the |
| 1298 | * destination only once and use the optional private data pointer of |
| 1299 | * the connection to cache the destination connection pointer. */ |
| 1300 | if (!conn->priv) { |
| 1301 | conn_dst = mgcp_find_dst_conn(conn); |
| 1302 | conn->priv = conn_dst; |
| 1303 | } else { |
| 1304 | conn_dst = (struct mgcp_conn *)conn->priv; |
| 1305 | } |
| 1306 | |
| 1307 | /* There is no destination conn, stop here */ |
| 1308 | if (!conn_dst) { |
Alexander Chemeris | ebb9bf3 | 2020-05-11 18:14:31 +0300 | [diff] [blame] | 1309 | LOGPCONN(conn, DRTP, LOGL_DEBUG, |
| 1310 | "no connection to forward an incoming RTP packet to\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | /* The destination conn is not an RTP connection */ |
| 1315 | if (conn_dst->type != MGCP_CONN_TYPE_RTP) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1316 | LOGPCONN(conn, DRTP, LOGL_ERROR, |
| 1317 | "unable to find suitable destination conn\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1318 | return -1; |
| 1319 | } |
| 1320 | |
| 1321 | /* Dispatch RTP packet to destination RTP connection */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1322 | return mgcp_send_rtp(&conn_dst->u.rtp, msg); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 1325 | /*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally. |
| 1326 | * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP). |
| 1327 | * \param[in] addr socket address where the RTP packet has been received from. |
| 1328 | * \param[in] buf buffer that hold the RTP payload. |
| 1329 | * \param[in] buf_size size data length of buf. |
| 1330 | * \param[in] conn originating connection. |
| 1331 | * \returns 0 on success, -1 on ERROR. */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1332 | int mgcp_dispatch_e1_bridge_cb(struct msgb *msg) |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 1333 | { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1334 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 1335 | struct mgcp_conn_rtp *conn_src = mc->conn_src; |
| 1336 | struct mgcp_conn *conn = conn_src->conn; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1337 | struct osmo_sockaddr *from_addr = mc->from_addr; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1338 | |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1339 | /* Check if the connection is in loopback mode, if yes, just send the |
| 1340 | * incoming data back to the origin */ |
| 1341 | if (conn->mode == MGCP_CONN_LOOPBACK) { |
| 1342 | /* When we are in loopback mode, we loop back all incoming |
| 1343 | * packets back to their origin. We will use the originating |
| 1344 | * address data from the UDP packet header to patch the |
| 1345 | * outgoing address in connection on the fly */ |
| 1346 | if (conn->u.rtp.end.rtp_port == 0) { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1347 | OSMO_ASSERT(conn->u.rtp.end.addr.u.sa.sa_family == from_addr->u.sa.sa_family); |
| 1348 | switch (from_addr->u.sa.sa_family) { |
| 1349 | case AF_INET: |
| 1350 | conn->u.rtp.end.addr.u.sin.sin_addr = from_addr->u.sin.sin_addr; |
| 1351 | conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port; |
| 1352 | break; |
| 1353 | case AF_INET6: |
| 1354 | conn->u.rtp.end.addr.u.sin6.sin6_addr = from_addr->u.sin6.sin6_addr; |
| 1355 | conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port; |
| 1356 | break; |
| 1357 | default: |
| 1358 | OSMO_ASSERT(false); |
| 1359 | } |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1360 | } |
| 1361 | return mgcp_send_rtp(conn_src, msg); |
| 1362 | } |
| 1363 | |
| 1364 | /* Forward to E1 */ |
| 1365 | return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg); |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Philipp Maier | df5d219 | 2018-01-24 11:39:32 +0100 | [diff] [blame] | 1368 | /*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed. |
| 1369 | * \param[in] endp Endpoint on which the connection resides. |
Philipp Maier | 08eb935 | 2020-06-18 11:55:35 +0200 | [diff] [blame] | 1370 | * \param[in] conn Connection that is about to be removed (ignored). */ |
Philipp Maier | df5d219 | 2018-01-24 11:39:32 +0100 | [diff] [blame] | 1371 | void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn) |
| 1372 | { |
| 1373 | struct mgcp_conn *conn_cleanup; |
| 1374 | |
| 1375 | /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the |
| 1376 | * pointer to the destination connection, so that we do not have |
| 1377 | * to go through the list every time an RTP packet arrives. To prevent |
| 1378 | * a use-after-free situation we invalidate this information for all |
| 1379 | * connections present when one connection is removed from the |
| 1380 | * endpoint. */ |
| 1381 | llist_for_each_entry(conn_cleanup, &endp->conns, entry) { |
| 1382 | conn_cleanup->priv = NULL; |
| 1383 | } |
| 1384 | } |
| 1385 | |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 1386 | /*! cleanup an endpoint when a connection on an E1 endpoint is removed. |
| 1387 | * \param[in] endp Endpoint on which the connection resides. |
| 1388 | * \param[in] conn Connection that is about to be removed (ignored). */ |
| 1389 | void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn) |
| 1390 | { |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1391 | /* Cleanup tasks for E1 are the same as for regular endpoint. The |
| 1392 | * shut down of the E1 part is handled separately. */ |
| 1393 | mgcp_cleanup_rtp_bridge_cb(endp, conn); |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1396 | static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg) |
| 1397 | { |
| 1398 | return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD; |
| 1399 | } |
| 1400 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1401 | /* Handle incoming RTP data from NET */ |
| 1402 | static int rtp_data_net(struct osmo_fd *fd, unsigned int what) |
| 1403 | { |
| 1404 | /* NOTE: This is a generic implementation. RTP data is received. In |
| 1405 | * case of loopback the data is just sent back to its origin. All |
| 1406 | * other cases implement endpoint specific behaviour (e.g. how is the |
| 1407 | * destination connection determined?). That specific behaviour is |
| 1408 | * implemented by the callback function that is called at the end of |
| 1409 | * the function */ |
| 1410 | |
| 1411 | struct mgcp_conn_rtp *conn_src; |
| 1412 | struct mgcp_endpoint *endp; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1413 | struct osmo_sockaddr addr; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1414 | socklen_t slen = sizeof(addr); |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1415 | char ipbuf[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1416 | int ret; |
| 1417 | enum rtp_proto proto; |
| 1418 | struct osmo_rtp_msg_ctx *mc; |
| 1419 | struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx"); |
| 1420 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1421 | |
| 1422 | conn_src = (struct mgcp_conn_rtp *)fd->data; |
| 1423 | OSMO_ASSERT(conn_src); |
| 1424 | endp = conn_src->conn->endp; |
| 1425 | OSMO_ASSERT(endp); |
| 1426 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1427 | proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1428 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1429 | ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1430 | |
| 1431 | if (ret <= 0) { |
| 1432 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno)); |
| 1433 | rc = -1; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | |
| 1437 | msgb_put(msg, ret); |
| 1438 | |
| 1439 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n", |
| 1440 | proto == MGCP_PROTO_RTP ? "RTP" : "RTPC", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1441 | msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf), |
| 1442 | osmo_sockaddr_port(&addr.u.sa)); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1443 | |
| 1444 | if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg)) |
| 1445 | || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) { |
| 1446 | /* Logging happened in the two check_ functions */ |
| 1447 | rc = -1; |
| 1448 | goto out; |
| 1449 | } |
| 1450 | |
| 1451 | if (is_dummy_msg(proto, msg)) { |
| 1452 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n"); |
| 1453 | rc = 0; |
| 1454 | goto out; |
| 1455 | } |
| 1456 | |
| 1457 | /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and |
| 1458 | * needs not be allocated with the msgb. */ |
| 1459 | mc = OSMO_RTP_MSG_CTX(msg); |
| 1460 | *mc = (struct osmo_rtp_msg_ctx){ |
| 1461 | .proto = proto, |
| 1462 | .conn_src = conn_src, |
| 1463 | .from_addr = &addr, |
| 1464 | }; |
| 1465 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n", |
| 1466 | mc->proto, mc->conn_src, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1467 | osmo_hexdump((void*)mc->from_addr, |
| 1468 | mc->from_addr->u.sa.sa_family == AF_INET6 ? |
| 1469 | sizeof(struct sockaddr_in6) : |
| 1470 | sizeof(struct sockaddr_in))); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1471 | |
| 1472 | /* Increment RX statistics */ |
| 1473 | rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]); |
| 1474 | rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_length(msg)); |
| 1475 | /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */ |
| 1476 | |
| 1477 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 1478 | forward_data(fd->fd, &conn_src->tap_in, msg); |
| 1479 | |
| 1480 | rc = rx_rtp(msg); |
| 1481 | |
| 1482 | out: |
| 1483 | msgb_free(msg); |
| 1484 | return rc; |
| 1485 | } |
| 1486 | |
| 1487 | static int rx_rtp(struct msgb *msg) |
| 1488 | { |
| 1489 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 1490 | struct mgcp_conn_rtp *conn_src = mc->conn_src; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1491 | struct osmo_sockaddr *from_addr = mc->from_addr; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1492 | struct mgcp_conn *conn = conn_src->conn; |
| 1493 | struct mgcp_trunk *trunk = conn->endp->trunk; |
| 1494 | |
| 1495 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1496 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 1497 | mgcp_conn_watchdog_kick(conn_src->conn); |
| 1498 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 1499 | /* If AMR is configured for the ingress connection a conversion of the |
| 1500 | * framing mode (octet-aligned vs. bandwith-efficient is explicitly |
| 1501 | * define, then we check if the incoming payload matches that |
| 1502 | * expectation. */ |
| 1503 | if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) { |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1504 | int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg)); |
Neels Hofmeyr | 7c6dd3c | 2019-08-20 03:06:17 +0200 | [diff] [blame] | 1505 | if (oa < 0) |
| 1506 | return -1; |
| 1507 | if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned) |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 1508 | return -1; |
| 1509 | } |
| 1510 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1511 | /* Check if the origin of the RTP packet seems plausible */ |
| 1512 | if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr)) |
| 1513 | return -1; |
| 1514 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1515 | /* Execute endpoint specific implementation that handles the |
| 1516 | * dispatching of the RTP data */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 1517 | return conn->endp->type->dispatch_rtp_cb(msg); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /*! set IP Type of Service parameter. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 1521 | * \param[in] fd associated file descriptor. |
| 1522 | * \param[in] tos dscp value. |
| 1523 | * \returns 0 on success, -1 on ERROR. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1524 | int mgcp_set_ip_tos(int fd, int tos) |
| 1525 | { |
| 1526 | int ret; |
| 1527 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1528 | |
| 1529 | if (ret < 0) |
| 1530 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1531 | return 0; |
| 1532 | } |
| 1533 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1534 | /*! bind RTP port to osmo_fd. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 1535 | * \param[in] source_addr source (local) address to bind on. |
| 1536 | * \param[in] fd associated file descriptor. |
| 1537 | * \param[in] port to bind on. |
| 1538 | * \returns 0 on success, -1 on ERROR. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1539 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port) |
| 1540 | { |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1541 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1542 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 1543 | rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1544 | NULL, 0, OSMO_SOCK_F_BIND); |
| 1545 | if (rc < 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1546 | LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1547 | source_addr, port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1548 | return -1; |
| 1549 | } |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1550 | fd->fd = rc; |
| 1551 | LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1556 | /* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1557 | static int bind_rtp(struct mgcp_config *cfg, const char *source_addr, |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1558 | struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1559 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1560 | /* NOTE: The port that is used for RTCP is the RTP port incremented by one |
| 1561 | * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */ |
| 1562 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1563 | if (mgcp_create_bind(source_addr, &rtp_end->rtp, |
| 1564 | rtp_end->local_port) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1565 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 1566 | "failed to create RTP port: %s:%d\n", |
| 1567 | source_addr, rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1568 | goto cleanup0; |
| 1569 | } |
| 1570 | |
| 1571 | if (mgcp_create_bind(source_addr, &rtp_end->rtcp, |
| 1572 | rtp_end->local_port + 1) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1573 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 1574 | "failed to create RTCP port: %s:%d\n", |
| 1575 | source_addr, rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1576 | goto cleanup1; |
| 1577 | } |
| 1578 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1579 | /* Set Type of Service (DSCP-Value) as configured via VTY */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1580 | mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 1581 | mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
| 1582 | |
Pau Espin Pedrol | a7152e0 | 2020-05-09 19:15:50 +0200 | [diff] [blame] | 1583 | rtp_end->rtp.when = OSMO_FD_READ; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1584 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1585 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 1586 | "failed to register RTP port %d\n", |
| 1587 | rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1588 | goto cleanup2; |
| 1589 | } |
| 1590 | |
Pau Espin Pedrol | a7152e0 | 2020-05-09 19:15:50 +0200 | [diff] [blame] | 1591 | rtp_end->rtcp.when = OSMO_FD_READ; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1592 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1593 | LOGPENDP(endp, DRTP, LOGL_ERROR, |
| 1594 | "failed to register RTCP port %d\n", |
| 1595 | rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1596 | goto cleanup3; |
| 1597 | } |
| 1598 | |
| 1599 | return 0; |
| 1600 | |
| 1601 | cleanup3: |
| 1602 | osmo_fd_unregister(&rtp_end->rtp); |
| 1603 | cleanup2: |
| 1604 | close(rtp_end->rtcp.fd); |
| 1605 | rtp_end->rtcp.fd = -1; |
| 1606 | cleanup1: |
| 1607 | close(rtp_end->rtp.fd); |
| 1608 | rtp_end->rtp.fd = -1; |
| 1609 | cleanup0: |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1613 | /*! bind RTP port to endpoint/connection. |
Philipp Maier | 0b79d21 | 2020-06-18 12:02:49 +0200 | [diff] [blame] | 1614 | * \param[in] endp endpoint that holds the RTP connection. |
| 1615 | * \param[in] rtp_port port number to bind on. |
| 1616 | * \param[in] conn associated RTP connection. |
| 1617 | * \returns 0 on success, -1 on ERROR. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1618 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 1619 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1620 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1621 | char name[512]; |
| 1622 | struct mgcp_rtp_end *end; |
| 1623 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1624 | snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1625 | end = &conn->end; |
| 1626 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1627 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1628 | LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n", |
| 1629 | rtp_port, mgcp_conn_dump(conn->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* Double bindings should never occour! Since we always allocate |
| 1632 | * connections dynamically and free them when they are not |
| 1633 | * needed anymore, there must be no previous binding leftover. |
| 1634 | * Should there be a connection bound twice, we have a serious |
| 1635 | * problem and must exit immediately! */ |
| 1636 | OSMO_ASSERT(false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | end->local_port = rtp_port; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1640 | end->rtp.cb = rtp_data_net; |
| 1641 | end->rtp.data = conn; |
| 1642 | end->rtcp.data = conn; |
| 1643 | end->rtcp.cb = rtp_data_net; |
| 1644 | |
Pau Espin Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 1645 | return bind_rtp(endp->cfg, conn->end.local_addr, end, endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1648 | /*! free allocated RTP and RTCP ports. |
| 1649 | * \param[in] end RTP end */ |
| 1650 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1651 | { |
| 1652 | if (end->rtp.fd != -1) { |
| 1653 | close(end->rtp.fd); |
| 1654 | end->rtp.fd = -1; |
| 1655 | osmo_fd_unregister(&end->rtp); |
| 1656 | } |
| 1657 | |
| 1658 | if (end->rtcp.fd != -1) { |
| 1659 | close(end->rtcp.fd); |
| 1660 | end->rtcp.fd = -1; |
| 1661 | osmo_fd_unregister(&end->rtcp); |
| 1662 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1663 | } |