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