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