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