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