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