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