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