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