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