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