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