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