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