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