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