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