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 | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 60 | static int rx_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *payload, |
| 61 | enum rtp_proto proto, struct sockaddr_in *from_addr); |
| 62 | static int tx_rtp(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, |
| 63 | enum rtp_proto proto, struct sockaddr_in *from_addr, struct msgb *payload); |
| 64 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 65 | /*! Determine the local rtp bind IP-address. |
| 66 | * \param[out] addr caller provided memory to store the resulting IP-Address |
| 67 | * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters |
| 68 | * |
| 69 | * The local bind IP-address is automatically selected by probing the |
| 70 | * IP-Address of the interface that is pointing towards the remote IP-Address, |
| 71 | * if no remote IP-Address is known yet, the statically configured |
| 72 | * IP-Addresses are used as fallback. */ |
| 73 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn) |
| 74 | { |
| 75 | |
| 76 | struct mgcp_endpoint *endp; |
| 77 | int rc; |
| 78 | endp = conn->conn->endp; |
| 79 | |
| 80 | /* Try probing the local IP-Address */ |
| 81 | if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) { |
| 82 | rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr)); |
| 83 | if (rc < 0) |
| 84 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 85 | "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] | 86 | ENDPOINT_NUMBER(endp), conn->conn->id); |
| 87 | else { |
| 88 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 89 | "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] | 90 | ENDPOINT_NUMBER(endp), conn->conn->id, addr, |
| 91 | inet_ntoa(conn->end.addr)); |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* Select from preconfigured IP-Addresses */ |
| 97 | if (endp->cfg->net_ports.bind_addr) { |
| 98 | /* Check there is a bind IP for the RTP traffic configured, |
| 99 | * if so, use that IP-Address */ |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 100 | osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 101 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 102 | "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] | 103 | ENDPOINT_NUMBER(endp), conn->conn->id, addr); |
| 104 | } else { |
| 105 | /* No specific bind IP is configured for the RTP traffic, so |
| 106 | * assume the IP where we listen for incoming MGCP messages |
| 107 | * as bind IP */ |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 108 | osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 109 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 110 | "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] | 111 | ENDPOINT_NUMBER(endp), conn->conn->id, addr); |
| 112 | } |
| 113 | } |
| 114 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 115 | /* This does not need to be a precision timestamp and |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 116 | * is allowed to wrap quite fast. The returned value is |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 117 | * 1/codec_rate seconds. */ |
| 118 | static uint32_t get_current_ts(unsigned codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 119 | { |
| 120 | struct timespec tp; |
| 121 | uint64_t ret; |
| 122 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 123 | if (!codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 124 | return 0; |
| 125 | |
| 126 | memset(&tp, 0, sizeof(tp)); |
| 127 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 128 | LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 129 | |
| 130 | /* convert it to 1/unit seconds */ |
| 131 | ret = tp.tv_sec; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 132 | ret *= codec_rate; |
| 133 | ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 138 | /*! send udp packet. |
| 139 | * \param[in] fd associated file descriptor |
| 140 | * \param[in] addr destination ip-address |
| 141 | * \param[in] port destination UDP port |
| 142 | * \param[in] buf buffer that holds the data to be send |
| 143 | * \param[in] len length of the data to be sent |
| 144 | * \returns bytes sent, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 145 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
| 146 | { |
| 147 | struct sockaddr_in out; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 148 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 149 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 150 | "sending %i bytes length packet to %s:%u ...\n", |
| 151 | len, inet_ntoa(*addr), ntohs(port)); |
| 152 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 153 | out.sin_family = AF_INET; |
| 154 | out.sin_port = port; |
| 155 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 156 | |
| 157 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 158 | } |
| 159 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 160 | /*! send RTP dummy packet (to keep NAT connection open). |
| 161 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 162 | * \param[in] conn associated RTP connection |
| 163 | * \returns bytes sent, -1 on error */ |
| 164 | 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] | 165 | { |
| 166 | static char buf[] = { MGCP_DUMMY_LOAD }; |
| 167 | int rc; |
| 168 | int was_rtcp = 0; |
| 169 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 170 | OSMO_ASSERT(endp); |
| 171 | OSMO_ASSERT(conn); |
| 172 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 173 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 174 | "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp)); |
| 175 | LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 176 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 177 | |
| 178 | rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, |
| 179 | conn->end.rtp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 180 | |
| 181 | if (rc == -1) |
| 182 | goto failed; |
| 183 | |
| 184 | if (endp->tcfg->omit_rtcp) |
| 185 | return rc; |
| 186 | |
| 187 | was_rtcp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 188 | rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr, |
| 189 | conn->end.rtcp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 190 | |
| 191 | if (rc >= 0) |
| 192 | return rc; |
| 193 | |
| 194 | failed: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 195 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 196 | "endpoint:0x%x Failed to send dummy %s packet.\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 197 | ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 198 | |
| 199 | return -1; |
| 200 | } |
| 201 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 202 | /* Compute timestamp alignment error */ |
| 203 | static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate, |
| 204 | int ptime, uint32_t timestamp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | { |
| 206 | int32_t timestamp_delta; |
| 207 | |
| 208 | if (ptime == 0) |
| 209 | return 0; |
| 210 | |
| 211 | /* Align according to: T - Tlast = k * Tptime */ |
| 212 | timestamp_delta = timestamp - sstate->last_timestamp; |
| 213 | |
| 214 | return timestamp_delta % ptime; |
| 215 | } |
| 216 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 217 | /* Check timestamp and sequence number for plausibility */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 218 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
| 219 | struct mgcp_rtp_state *state, |
| 220 | struct mgcp_rtp_stream_state *sstate, |
| 221 | struct mgcp_rtp_end *rtp_end, |
| 222 | struct sockaddr_in *addr, |
| 223 | uint16_t seq, uint32_t timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 224 | const char *text, int32_t * tsdelta_out) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 225 | { |
| 226 | int32_t tsdelta; |
| 227 | int32_t timestamp_error; |
| 228 | |
| 229 | /* Not fully intialized, skip */ |
| 230 | if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp) |
| 231 | return 0; |
| 232 | |
| 233 | if (seq == sstate->last_seq) { |
| 234 | if (timestamp != sstate->last_timestamp) { |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 235 | rate_ctr_inc(sstate->err_ts_ctr); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 236 | LOGP(DRTP, LOGL_ERROR, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 237 | "The %s timestamp delta is != 0 but the sequence " |
| 238 | "number %d is the same, " |
| 239 | "TS offset: %d, SeqNo offset: %d " |
| 240 | "on 0x%x SSRC: %u timestamp: %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 241 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 242 | text, seq, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 243 | state->patch.timestamp_offset, state->patch.seq_offset, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 244 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 245 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | tsdelta = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 251 | (int32_t)(timestamp - sstate->last_timestamp) / |
| 252 | (int16_t)(seq - sstate->last_seq); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 253 | |
| 254 | if (tsdelta == 0) { |
| 255 | /* Don't update *tsdelta_out */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 256 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 257 | "The %s timestamp delta is %d " |
| 258 | "on 0x%x SSRC: %u timestamp: %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 259 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 260 | text, tsdelta, |
| 261 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 262 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | if (sstate->last_tsdelta != tsdelta) { |
| 268 | if (sstate->last_tsdelta) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 269 | LOGP(DRTP, LOGL_INFO, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 270 | "The %s timestamp delta changes from %d to %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 271 | "on 0x%x SSRC: %u timestamp: %u from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 272 | text, sstate->last_tsdelta, tsdelta, |
| 273 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 274 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | if (tsdelta_out) |
| 279 | *tsdelta_out = tsdelta; |
| 280 | |
| 281 | timestamp_error = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 282 | ts_alignment_error(sstate, state->packet_duration, timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 283 | |
| 284 | if (timestamp_error) { |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 285 | rate_ctr_inc(sstate->err_ts_ctr); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 286 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 287 | "The %s timestamp has an alignment error of %d " |
| 288 | "on 0x%x SSRC: %u " |
| 289 | "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 290 | "from %s:%d. ptime: %d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 291 | text, timestamp_error, |
| 292 | ENDPOINT_NUMBER(endp), sstate->ssrc, |
| 293 | (int16_t)(seq - sstate->last_seq), |
| 294 | (int32_t)(timestamp - sstate->last_timestamp), |
| 295 | tsdelta, |
| 296 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 297 | state->packet_duration); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 298 | } |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | /* Set the timestamp offset according to the packet duration. */ |
| 303 | static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 304 | struct mgcp_rtp_state *state, |
| 305 | struct mgcp_rtp_end *rtp_end, |
| 306 | struct sockaddr_in *addr, |
| 307 | int16_t delta_seq, uint32_t in_timestamp) |
| 308 | { |
| 309 | int32_t tsdelta = state->packet_duration; |
| 310 | int timestamp_offset; |
| 311 | uint32_t out_timestamp; |
| 312 | |
| 313 | if (tsdelta == 0) { |
| 314 | tsdelta = state->out_stream.last_tsdelta; |
| 315 | if (tsdelta != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 316 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 317 | "A fixed packet duration is not available on 0x%x, " |
| 318 | "using last output timestamp delta instead: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 319 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 320 | ENDPOINT_NUMBER(endp), tsdelta, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 321 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | } else { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 323 | tsdelta = rtp_end->codec->rate * 20 / 1000; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 324 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 325 | "Fixed packet duration and last timestamp delta " |
| 326 | "are not available on 0x%x, " |
| 327 | "using fixed 20ms instead: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 328 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 329 | ENDPOINT_NUMBER(endp), tsdelta, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 330 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta; |
| 335 | timestamp_offset = out_timestamp - in_timestamp; |
| 336 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 337 | if (state->patch.timestamp_offset != timestamp_offset) { |
| 338 | state->patch.timestamp_offset = timestamp_offset; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 339 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 340 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 341 | "Timestamp offset change on 0x%x SSRC: %u " |
| 342 | "SeqNo delta: %d, TS offset: %d, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 343 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 345 | delta_seq, state->patch.timestamp_offset, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 346 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return timestamp_offset; |
| 350 | } |
| 351 | |
| 352 | /* Set the timestamp offset according to the packet duration. */ |
| 353 | static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 354 | struct mgcp_rtp_state *state, |
| 355 | struct mgcp_rtp_end *rtp_end, |
| 356 | struct sockaddr_in *addr, |
| 357 | uint32_t timestamp) |
| 358 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 359 | int ts_error = 0; |
| 360 | int ts_check = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 361 | int ptime = state->packet_duration; |
| 362 | |
| 363 | /* Align according to: T + Toffs - Tlast = k * Tptime */ |
| 364 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 365 | ts_error = ts_alignment_error(&state->out_stream, ptime, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 366 | timestamp + state->patch.timestamp_offset); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 367 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 368 | /* If there is an alignment error, we have to compensate it */ |
| 369 | if (ts_error) { |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 370 | state->patch.timestamp_offset += ptime - ts_error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 371 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 372 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 373 | "Corrected timestamp alignment error of %d on 0x%x SSRC: %u " |
| 374 | "new TS offset: %d, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 375 | "from %s:%d\n", |
| 376 | ts_error, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 377 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 378 | state->patch.timestamp_offset, inet_ntoa(addr->sin_addr), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 379 | ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 382 | /* Check we really managed to compensate the timestamp |
| 383 | * offset. There should not be any remaining error, failing |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 384 | * here would point to a serous problem with the alignment |
| 385 | * error computation function */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 386 | ts_check = ts_alignment_error(&state->out_stream, ptime, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 387 | timestamp + state->patch.timestamp_offset); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 388 | OSMO_ASSERT(ts_check == 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 389 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 390 | /* Return alignment error before compensation */ |
| 391 | return ts_error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 394 | /*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb). |
| 395 | * \param[in] associated endpoint |
| 396 | * \param[in] destination RTP end |
| 397 | * \param[in,out] pointer to buffer with voice data |
| 398 | * \param[in] voice data length |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 399 | * \param[in] maximum size of caller provided voice data buffer |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 400 | * \returns ignores input parameters, return always 0 */ |
| 401 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, |
| 402 | struct mgcp_rtp_end *dst_end, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 403 | char *data, int *len, int buf_size) |
| 404 | { |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 405 | LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 406 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 410 | /*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb). |
| 411 | * \param[in] associated endpoint |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 412 | * \param[in] destination RTP connnection |
| 413 | * \param[in] source RTP connection |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 414 | * \returns ignores input parameters, return always 0 */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 415 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 416 | struct mgcp_conn_rtp *conn_dst, |
| 417 | struct mgcp_conn_rtp *conn_src) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 418 | { |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 419 | LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 420 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 425 | int *payload_type, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 426 | const char **audio_name, |
| 427 | const char **fmtp_extra, |
| 428 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 429 | { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 430 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 431 | "endpoint:0x%x conn:%s using format defaults\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 432 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 433 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 434 | *payload_type = conn->end.codec->payload_type; |
| 435 | *audio_name = conn->end.codec->audio_name; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 436 | *fmtp_extra = conn->end.fmtp_extra; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 439 | void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, |
| 440 | struct mgcp_rtp_state *state, const uint16_t seq, |
| 441 | const int32_t transit, const uint32_t ssrc) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 442 | { |
| 443 | int32_t d; |
| 444 | |
| 445 | /* initialize or re-initialize */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 446 | if (!state->stats.initialized || state->stats.ssrc != ssrc) { |
| 447 | state->stats.initialized = 1; |
| 448 | state->stats.base_seq = seq; |
| 449 | state->stats.max_seq = seq - 1; |
| 450 | state->stats.ssrc = ssrc; |
| 451 | state->stats.jitter = 0; |
| 452 | state->stats.transit = transit; |
| 453 | state->stats.cycles = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 454 | } else { |
| 455 | uint16_t udelta; |
| 456 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 457 | /* The below takes the shape of the validation of |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 458 | * Appendix A. Check if there is something weird with |
| 459 | * the sequence number, otherwise check for a wrap |
| 460 | * around in the sequence number. |
| 461 | * It can't wrap during the initialization so let's |
| 462 | * skip it here. The Appendix A probably doesn't have |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 463 | * this issue because of the probation. */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 464 | udelta = seq - state->stats.max_seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 465 | if (udelta < RTP_MAX_DROPOUT) { |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 466 | if (seq < state->stats.max_seq) |
| 467 | state->stats.cycles += RTP_SEQ_MOD; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 468 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 469 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 470 | "RTP seqno made a very large jump on 0x%x delta: %u\n", |
| 471 | ENDPOINT_NUMBER(endp), udelta); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 475 | /* Calculate the jitter between the two packages. The TS should be |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 476 | * taken closer to the read function. This was taken from the |
| 477 | * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 478 | * resolution. */ |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 479 | d = transit - state->stats.transit; |
| 480 | state->stats.transit = transit; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 481 | if (d < 0) |
| 482 | d = -d; |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 483 | state->stats.jitter += d - ((state->stats.jitter + 8) >> 4); |
| 484 | state->stats.max_seq = seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 487 | /* There may be different payload type numbers negotiated for two connections. |
| 488 | * Patch the payload type of an RTP packet so that it uses the payload type |
| 489 | * that is valid for the destination connection (conn_dst) */ |
| 490 | static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 491 | struct mgcp_conn_rtp *conn_dst, struct msgb *msg) |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 492 | { |
| 493 | struct rtp_hdr *rtp_hdr; |
| 494 | uint8_t pt_in; |
| 495 | int pt_out; |
| 496 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 497 | OSMO_ASSERT(msgb_l3len(msg) >= sizeof(struct rtp_hdr)); |
| 498 | rtp_hdr = (struct rtp_hdr *)msgb_l3(msg); |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 499 | |
| 500 | pt_in = rtp_hdr->payload_type; |
| 501 | pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in); |
| 502 | if (pt_out < 0) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | rtp_hdr->payload_type = (uint8_t) pt_out; |
| 506 | return 0; |
| 507 | } |
| 508 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 509 | /* The RFC 3550 Appendix A assumes there are multiple sources but |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 510 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
| 511 | * one source and this code will patch RTP header to appear as if there |
| 512 | * is only one source. |
| 513 | * There is also no probation period for new sources. Every RTP header |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 514 | * we receive will be seen as a switch in streams. */ |
| 515 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, |
| 516 | struct mgcp_rtp_state *state, |
| 517 | struct mgcp_rtp_end *rtp_end, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 518 | struct sockaddr_in *addr, struct msgb *msg) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 519 | { |
| 520 | uint32_t arrival_time; |
| 521 | int32_t transit; |
| 522 | uint16_t seq; |
| 523 | uint32_t timestamp, ssrc; |
| 524 | struct rtp_hdr *rtp_hdr; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 525 | int payload = rtp_end->codec->payload_type; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 526 | unsigned int len = msgb_l3len(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 527 | |
| 528 | if (len < sizeof(*rtp_hdr)) |
| 529 | return; |
| 530 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 531 | rtp_hdr = (struct rtp_hdr *)msgb_l3(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 532 | seq = ntohs(rtp_hdr->sequence); |
| 533 | timestamp = ntohl(rtp_hdr->timestamp); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 534 | arrival_time = get_current_ts(rtp_end->codec->rate); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 535 | ssrc = ntohl(rtp_hdr->ssrc); |
| 536 | transit = arrival_time - timestamp; |
| 537 | |
| 538 | mgcp_rtp_annex_count(endp, state, seq, transit, ssrc); |
| 539 | |
| 540 | if (!state->initialized) { |
| 541 | state->initialized = 1; |
| 542 | state->in_stream.last_seq = seq - 1; |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 543 | state->in_stream.ssrc = state->patch.orig_ssrc = ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 544 | state->in_stream.last_tsdelta = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 545 | state->packet_duration = |
| 546 | mgcp_rtp_packet_duration(endp, rtp_end); |
Philipp Maier | 0ec1d4e | 2018-05-16 11:09:42 +0200 | [diff] [blame] | 547 | state->out_stream.last_seq = seq - 1; |
| 548 | state->out_stream.ssrc = state->patch.orig_ssrc = ssrc; |
| 549 | state->out_stream.last_tsdelta = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 550 | state->out_stream.last_timestamp = timestamp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 551 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 552 | LOGP(DRTP, LOGL_INFO, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 553 | "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 554 | "pkt-duration: %d, from %s:%d\n", |
| 555 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 556 | state->patch.seq_offset, state->packet_duration, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 557 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 558 | if (state->packet_duration == 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 559 | state->packet_duration = |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 560 | rtp_end->codec->rate * 20 / 1000; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 561 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 562 | "endpoint:0x%x fixed packet duration is not available, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 563 | "using fixed 20ms instead: %d from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 564 | ENDPOINT_NUMBER(endp), state->packet_duration, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 565 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 566 | } |
| 567 | } else if (state->in_stream.ssrc != ssrc) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 568 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 569 | "endpoint:0x%x SSRC changed: %u -> %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 570 | "from %s:%d\n", |
| 571 | ENDPOINT_NUMBER(endp), |
| 572 | state->in_stream.ssrc, rtp_hdr->ssrc, |
| 573 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 574 | |
| 575 | state->in_stream.ssrc = ssrc; |
| 576 | if (rtp_end->force_constant_ssrc) { |
| 577 | int16_t delta_seq; |
| 578 | |
| 579 | /* Always increment seqno by 1 */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 580 | state->patch.seq_offset = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 581 | (state->out_stream.last_seq + 1) - seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 582 | |
| 583 | /* Estimate number of packets that would have been sent */ |
| 584 | delta_seq = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 585 | (arrival_time - state->in_stream.last_arrival_time |
| 586 | + state->packet_duration / 2) / |
| 587 | state->packet_duration; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 588 | |
| 589 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 590 | delta_seq, timestamp); |
| 591 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 592 | state->patch.patch_ssrc = 1; |
| 593 | ssrc = state->patch.orig_ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 594 | if (rtp_end->force_constant_ssrc != -1) |
| 595 | rtp_end->force_constant_ssrc -= 1; |
| 596 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 597 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 598 | "endpoint:0x%x SSRC patching enabled, SSRC: %u " |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 599 | "SeqNo offset: %d, TS offset: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 600 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 601 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 602 | state->patch.seq_offset, state->patch.timestamp_offset, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 603 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | state->in_stream.last_tsdelta = 0; |
| 607 | } else { |
| 608 | /* Compute current per-packet timestamp delta */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 609 | check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, |
| 610 | addr, seq, timestamp, "input", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 611 | &state->in_stream.last_tsdelta); |
| 612 | |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 613 | if (state->patch.patch_ssrc) |
| 614 | ssrc = state->patch.orig_ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* Save before patching */ |
| 618 | state->in_stream.last_timestamp = timestamp; |
| 619 | state->in_stream.last_seq = seq; |
| 620 | state->in_stream.last_arrival_time = arrival_time; |
| 621 | |
| 622 | if (rtp_end->force_aligned_timing && |
| 623 | state->out_stream.ssrc == ssrc && state->packet_duration) |
| 624 | /* Align the timestamp offset */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 625 | align_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 626 | timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 627 | |
| 628 | /* Store the updated SSRC back to the packet */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 629 | if (state->patch.patch_ssrc) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 630 | rtp_hdr->ssrc = htonl(ssrc); |
| 631 | |
| 632 | /* Apply the offset and store it back to the packet. |
| 633 | * This won't change anything if the offset is 0, so the conditional is |
| 634 | * omitted. */ |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 635 | seq += state->patch.seq_offset; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 636 | rtp_hdr->sequence = htons(seq); |
Harald Welte | 3338135 | 2017-12-25 09:44:26 +0100 | [diff] [blame] | 637 | timestamp += state->patch.timestamp_offset; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 638 | rtp_hdr->timestamp = htonl(timestamp); |
| 639 | |
| 640 | /* Check again, whether the timestamps are still valid */ |
| 641 | if (state->out_stream.ssrc == ssrc) |
| 642 | check_rtp_timestamp(endp, state, &state->out_stream, rtp_end, |
| 643 | addr, seq, timestamp, "output", |
| 644 | &state->out_stream.last_tsdelta); |
| 645 | |
| 646 | /* Save output values */ |
| 647 | state->out_stream.last_seq = seq; |
| 648 | state->out_stream.last_timestamp = timestamp; |
| 649 | state->out_stream.ssrc = ssrc; |
| 650 | |
| 651 | if (payload < 0) |
| 652 | return; |
| 653 | |
| 654 | #if 0 |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 655 | DEBUGP(DRTP, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 656 | "endpoint:0x%x payload hdr payload %u -> endp payload %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 657 | ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 658 | rtp_hdr->payload_type = payload; |
| 659 | #endif |
| 660 | } |
| 661 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 662 | /* Forward data to a debug tap. This is debug function that is intended for |
| 663 | * debugging the voice traffic with tools like gstreamer */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 664 | 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] | 665 | { |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 666 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 667 | |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 668 | if (!tap->enabled) |
| 669 | return; |
| 670 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 671 | rc = sendto(fd, msgb_l3(msg), msgb_l3len(msg), 0, (struct sockaddr *)&tap->forward, |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 672 | sizeof(tap->forward)); |
| 673 | |
| 674 | if (rc < 0) |
| 675 | LOGP(DRTP, LOGL_ERROR, |
| 676 | "Forwarding tapped (debug) voice data failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 679 | /*! Send RTP/RTCP data to a specified destination connection. |
| 680 | * \param[in] endp associated endpoint (for configuration, logging) |
| 681 | * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP |
| 682 | * \param[in] spoofed source address (set to NULL to disable) |
| 683 | * \param[in] buf buffer that contains the RTP/RTCP data |
| 684 | * \param[in] len length of the buffer that contains the RTP/RTCP data |
| 685 | * \param[in] conn_src associated source connection |
| 686 | * \param[in] conn_dst associated destination connection |
| 687 | * \returns 0 on success, -1 on ERROR */ |
| 688 | 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^] | 689 | struct msgb *msg, struct mgcp_conn_rtp *conn_src, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 690 | struct mgcp_conn_rtp *conn_dst) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 691 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 692 | /*! When no destination connection is available (e.g. when only one |
| 693 | * connection in loopback mode exists), then the source connection |
| 694 | * shall be specified as destination connection */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 695 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 696 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 697 | struct mgcp_rtp_end *rtp_end; |
| 698 | struct mgcp_rtp_state *rtp_state; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 699 | char *dest_name; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 700 | int rc; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 701 | int len; |
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 | OSMO_ASSERT(conn_src); |
| 704 | OSMO_ASSERT(conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 705 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 706 | if (is_rtp) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 707 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 708 | "endpoint:0x%x delivering RTP packet...\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 709 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 710 | } else { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 711 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 712 | "endpoint:0x%x delivering RTCP packet...\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 713 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 714 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 715 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 716 | LOGP(DRTP, LOGL_DEBUG, |
Neels Hofmeyr | 7066af8 | 2018-07-23 18:28:36 +0200 | [diff] [blame] | 717 | "endpoint:0x%x loop:%d, mode:%d%s\n", |
| 718 | ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode, |
| 719 | conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 720 | |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 721 | /* FIXME: It is legal that the payload type on the egress connection is |
| 722 | * different from the payload type that has been negotiated on the |
| 723 | * ingress connection. Essentially the codecs are the same so we can |
| 724 | * match them and patch the payload type. However, if we can not find |
| 725 | * the codec pendant (everything ist equal except the PT), we are of |
| 726 | * course unable to patch the payload type. A situation like this |
| 727 | * should not occur if transcoding is consequently avoided. Until |
| 728 | * we have transcoding support in osmo-mgw we can not resolve this. */ |
Philipp Maier | da895b1 | 2018-08-03 12:16:37 +0200 | [diff] [blame] | 729 | if (is_rtp) { |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 730 | rc = mgcp_patch_pt(conn_src, conn_dst, msg); |
Philipp Maier | da895b1 | 2018-08-03 12:16:37 +0200 | [diff] [blame] | 731 | if (rc < 0) { |
| 732 | LOGP(DRTP, LOGL_ERROR, |
| 733 | "endpoint:0x%x can not patch PT because no suitable egress codec was found.\n", |
| 734 | ENDPOINT_NUMBER(endp)); |
| 735 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 738 | /* Note: In case of loopback configuration, both, the source and the |
| 739 | * destination will point to the same connection. */ |
| 740 | rtp_end = &conn_dst->end; |
| 741 | rtp_state = &conn_src->state; |
| 742 | dest_name = conn_dst->conn->name; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 743 | |
| 744 | if (!rtp_end->output_enabled) { |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 745 | 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] | 746 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 747 | "endpoint:0x%x output disabled, drop to %s %s " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 748 | "rtp_port:%u rtcp_port:%u\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 749 | ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 750 | dest_name, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 751 | inet_ntoa(rtp_end->addr), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 752 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 753 | ); |
| 754 | } else if (is_rtp) { |
| 755 | int cont; |
| 756 | int nbytes = 0; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 757 | int buflen = msgb_l3len(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 758 | do { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 759 | /* Run transcoder */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 760 | cont = endp->cfg->rtp_processing_cb(endp, rtp_end, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 761 | msgb_l3(msg), &buflen, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 762 | RTP_BUF_SIZE); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 763 | if (cont < 0) |
| 764 | break; |
| 765 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 766 | if (addr) |
| 767 | mgcp_patch_and_count(endp, rtp_state, rtp_end, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 768 | addr, msg); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 769 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 770 | "endpoint:0x%x process/send to %s %s " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 771 | "rtp_port:%u rtcp_port:%u\n", |
| 772 | ENDPOINT_NUMBER(endp), dest_name, |
| 773 | inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port), |
| 774 | ntohs(rtp_end->rtcp_port) |
| 775 | ); |
| 776 | |
| 777 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 778 | forward_data(rtp_end->rtp.fd, &conn_src->tap_out, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 779 | msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 780 | |
| 781 | /* FIXME: HACK HACK HACK. See OS#2459. |
| 782 | * The ip.access nano3G needs the first RTP payload's first two bytes to read hex |
| 783 | * 'e400', or it will reject the RAB assignment. It seems to not harm other femto |
| 784 | * cells (as long as we patch only the first RTP payload in each stream). |
| 785 | */ |
Neels Hofmeyr | 35a3829 | 2018-07-23 18:29:04 +0200 | [diff] [blame] | 786 | if (!rtp_state->patched_first_rtp_payload |
| 787 | && conn_src->conn->mode == MGCP_CONN_LOOPBACK) { |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 788 | uint8_t *data = ((uint8_t *)msgb_l3(msg)) + 12; |
Neels Hofmeyr | 35a3829 | 2018-07-23 18:29:04 +0200 | [diff] [blame] | 789 | if (data[0] == 0xe0) { |
| 790 | data[0] = 0xe4; |
| 791 | data[1] = 0x00; |
| 792 | rtp_state->patched_first_rtp_payload = true; |
| 793 | LOGP(DRTP, LOGL_DEBUG, |
| 794 | "endpoint:0x%x Patching over first two bytes" |
| 795 | " to fake an IuUP Initialization Ack\n", |
| 796 | ENDPOINT_NUMBER(endp)); |
| 797 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 800 | len = mgcp_udp_send(rtp_end->rtp.fd, |
| 801 | &rtp_end->addr, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 802 | rtp_end->rtp_port, |
| 803 | msgb_l3(msg), msgb_l3len(msg)); |
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 | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 826 | rtp_end->rtcp_port, msgb_l3(msg), msgb_l3len(msg)); |
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, |
| 917 | "endpoint:0x%x destination IP-address and rtp port is (not yet) known\n", |
| 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; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 946 | unsigned int buf_size = msgb_l3len(msg); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 947 | |
| 948 | /* RTPC packets that are just a header without data do not make |
| 949 | * any sense. */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 950 | if (buf_size < sizeof(struct rtcp_hdr)) { |
| 951 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n", |
| 952 | buf_size, sizeof(struct rtcp_hdr)); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 953 | return -EINVAL; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 954 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 955 | |
| 956 | /* Make sure that the length of the received packet does not exceed |
| 957 | * the available buffer size */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 958 | hdr = (struct rtcp_hdr *)msgb_l3(msg); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 959 | len = (osmo_ntohs(hdr->length) + 1) * 4; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 960 | if (len > buf_size) { |
| 961 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n", |
| 962 | len, buf_size); |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 963 | return -EINVAL; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 964 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 965 | |
| 966 | /* Make sure we accept only packets that have a proper packet type set |
| 967 | * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
| 968 | type = hdr->type; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 969 | if ((type < 192 || type > 195) && (type < 200 || type > 213)) { |
| 970 | 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] | 971 | return -EINVAL; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 972 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* Do some basic checks to make sure that the RTP packets we are going to |
| 978 | * process are not complete garbage */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 979 | 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] | 980 | { |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 981 | size_t min_size = sizeof(struct rtp_hdr); |
| 982 | if (conn_src->iuup) |
| 983 | min_size += sizeof(struct osmo_iuup_hdr_data); |
| 984 | if (msgb_l3len(msg) < min_size) { |
| 985 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n", |
| 986 | msgb_l3len(msg), min_size); |
| 987 | return -1; |
| 988 | } |
Philipp Maier | 4dba769 | 2018-08-03 12:20:52 +0200 | [diff] [blame] | 989 | |
| 990 | /* FIXME: Add more checks, the reason why we do not check more than |
| 991 | * the length is because we currently handle IUUP packets as RTP |
| 992 | * packets, so they must pass this check, if we weould be more |
| 993 | * strict here, we would possibly break 3G. (see also FIXME note |
| 994 | * below */ |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 999 | /* Send RTP data. Possible options are standard RTP packet |
| 1000 | * transmission or trsmission via an osmux connection */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1001 | static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, |
| 1002 | struct msgb *payload, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1003 | struct mgcp_conn_rtp *conn_src, |
| 1004 | struct mgcp_conn_rtp *conn_dst) |
| 1005 | { |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1006 | struct mgcp_endpoint *endp = conn_src->conn->endp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1007 | |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1008 | LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1009 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn)); |
| 1010 | |
| 1011 | /* Before we try to deliver the packet, we check if the destination |
| 1012 | * port and IP-Address make sense at all. If not, we will be unable |
| 1013 | * to deliver the packet. */ |
| 1014 | if (check_rtp_destin(conn_dst) != 0) |
| 1015 | return -1; |
| 1016 | |
| 1017 | /* Depending on the RTP connection type, deliver the RTP packet to the |
| 1018 | * destination connection. */ |
| 1019 | switch (conn_dst->type) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1020 | case MGCP_RTP_DEFAULT: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1021 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1022 | "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1023 | "using mgcp_send() to forward data directly\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1024 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1025 | return mgcp_send(endp, proto == MGCP_PROTO_RTP, |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1026 | addr, payload, conn_src, conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1027 | case MGCP_OSMUX_BSC_NAT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1028 | case MGCP_OSMUX_BSC: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1029 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1030 | "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1031 | "using osmux_xfrm_to_osmux() to forward data through OSMUX\n", |
| 1032 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1033 | return osmux_xfrm_to_osmux(msgb_l3(payload), msgb_l3len(payload), conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1036 | /* If the data has not been handled/forwarded until here, it will |
| 1037 | * be discarded, this should not happen, normally the MGCP type |
| 1038 | * should be properly set */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1039 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1040 | "endpoint:0x%x bad MGCP type -- data discarded!\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1041 | ENDPOINT_NUMBER(endp)); |
| 1042 | |
| 1043 | return -1; |
| 1044 | } |
| 1045 | |
| 1046 | /*! dispatch incoming RTP packet to opposite RTP connection. |
| 1047 | * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP) |
| 1048 | * \param[in] addr socket address where the RTP packet has been received from |
| 1049 | * \param[in] buf buffer that hold the RTP payload |
| 1050 | * \param[in] buf_size size data length of buf |
| 1051 | * \param[in] conn originating connection |
| 1052 | * \returns 0 on success, -1 on ERROR */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1053 | int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, |
| 1054 | struct msgb *payload, struct mgcp_conn *conn) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1055 | { |
| 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 | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1099 | return tx_rtp(&conn->u.rtp, &conn_dst->u.rtp, proto, addr, payload); |
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 | { |
| 1123 | return msgb_l3len(msg) == 1 && ((char*)msgb_l3(msg))[0] == MGCP_DUMMY_LOAD; |
| 1124 | } |
| 1125 | |
| 1126 | int rx_rtp_from_iuup(struct msgb *msg, void *node_priv, void *pdu_priv) |
| 1127 | { |
| 1128 | struct mgcp_conn_rtp *conn_src = node_priv; |
| 1129 | struct sockaddr_in *from_addr = pdu_priv; |
| 1130 | return rx_rtp(conn_src, msg, MGCP_PROTO_RTP, from_addr); |
| 1131 | } |
| 1132 | |
| 1133 | struct pdu_ctx { |
| 1134 | struct sockaddr_in *from_addr; |
| 1135 | struct mgcp_conn_rtp *conn_src; |
| 1136 | }; |
| 1137 | |
| 1138 | int tx_pdu_from_iuup(struct msgb *msg, void *node_priv, void *pdu_priv) |
| 1139 | { |
| 1140 | struct mgcp_conn_rtp *conn_dst = node_priv; |
| 1141 | struct pdu_ctx *p = pdu_priv; |
| 1142 | return mgcp_send_rtp(MGCP_PROTO_RTP, p->from_addr, msg, p->conn_src, conn_dst); |
| 1143 | } |
| 1144 | |
| 1145 | static void init_iuup(struct mgcp_conn_rtp *conn_src) |
| 1146 | { |
| 1147 | struct osmo_iuup_cn_cfg cfg = { |
| 1148 | .node_priv = conn_src, |
| 1149 | .rx_payload = rx_rtp_from_iuup, |
| 1150 | .tx_msg = tx_pdu_from_iuup, |
| 1151 | }; |
| 1152 | |
| 1153 | osmo_iuup_cn_init(conn_src, &cfg, "%d@ I:%s", |
| 1154 | ENDPOINT_NUMBER(conn_src->conn->endp), conn_src->conn->id); |
| 1155 | } |
| 1156 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1157 | /* Handle incoming RTP data from NET */ |
| 1158 | static int rtp_data_net(struct osmo_fd *fd, unsigned int what) |
| 1159 | { |
| 1160 | /* NOTE: This is a generic implementation. RTP data is received. In |
| 1161 | * case of loopback the data is just sent back to its origin. All |
| 1162 | * other cases implement endpoint specific behaviour (e.g. how is the |
| 1163 | * destination connection determined?). That specific behaviour is |
| 1164 | * implemented by the callback function that is called at the end of |
| 1165 | * the function */ |
| 1166 | |
| 1167 | struct mgcp_conn_rtp *conn_src; |
| 1168 | struct mgcp_endpoint *endp; |
| 1169 | struct sockaddr_in addr; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1170 | socklen_t slen = sizeof(addr); |
| 1171 | int ret; |
| 1172 | enum rtp_proto proto; |
| 1173 | struct msgb *msg = msgb_alloc_headroom(RTP_BUF_SIZE + OSMO_IUUP_HEADROOM, |
| 1174 | OSMO_IUUP_HEADROOM, "RTP-rx"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1175 | |
| 1176 | conn_src = (struct mgcp_conn_rtp *)fd->data; |
| 1177 | OSMO_ASSERT(conn_src); |
| 1178 | endp = conn_src->conn->endp; |
| 1179 | OSMO_ASSERT(endp); |
| 1180 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1181 | proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1182 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1183 | ret = recvfrom(fd->fd, msg->data, msg->data_len, 0, (struct sockaddr *)&addr, &slen); |
| 1184 | |
| 1185 | if (ret <= 0) { |
| 1186 | LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno)); |
| 1187 | msgb_free(msg); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1188 | return -1; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1189 | } |
| 1190 | |
| 1191 | /* By default, indicate that RTP payload starts right from the buffer's beginning. */ |
| 1192 | msg->l3h = msgb_put(msg, ret); |
| 1193 | |
| 1194 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n", |
| 1195 | proto == MGCP_PROTO_RTP ? "RTP" : "RTPC", |
| 1196 | msgb_l3len(msg), inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); |
| 1197 | |
| 1198 | if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg)) |
| 1199 | || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) { |
| 1200 | /* Logging happened in the two check_ functions */ |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
| 1204 | if (is_dummy_msg(proto, msg)) { |
| 1205 | LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n"); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | /* Increment RX statistics */ |
| 1210 | rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]); |
| 1211 | rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_l3len(msg)); |
| 1212 | /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */ |
| 1213 | |
| 1214 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 1215 | forward_data(fd->fd, &conn_src->tap_in, msg); |
| 1216 | |
| 1217 | if (proto == MGCP_PROTO_RTP && osmo_iuup_is_init(msg)) |
| 1218 | init_iuup(conn_src); |
| 1219 | |
| 1220 | if (conn_src->iuup && proto == MGCP_PROTO_RTP) |
| 1221 | return osmo_iuup_cn_rx_pdu(conn_src->iuup, msg, &addr); |
| 1222 | else |
| 1223 | return rx_rtp(conn_src, msg, proto, &addr); |
| 1224 | } |
| 1225 | |
| 1226 | static int rx_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *payload, |
| 1227 | enum rtp_proto proto, struct sockaddr_in *from_addr) |
| 1228 | { |
| 1229 | struct mgcp_endpoint *endp = conn_src->conn->endp; |
| 1230 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1231 | |
| 1232 | /* Check if the connection is in loopback mode, if yes, just send the |
| 1233 | * incoming data back to the origin */ |
| 1234 | if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) { |
Philipp Maier | 5dbfc78 | 2017-12-12 16:20:25 +0100 | [diff] [blame] | 1235 | /* When we are in loopback mode, we loop back all incoming |
| 1236 | * packets back to their origin. We will use the originating |
| 1237 | * address data from the UDP packet header to patch the |
| 1238 | * outgoing address in connection on the fly */ |
| 1239 | if (conn_src->end.rtp_port == 0) { |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1240 | conn_src->end.addr = from_addr->sin_addr; |
| 1241 | conn_src->end.rtp_port = from_addr->sin_port; |
Philipp Maier | 5dbfc78 | 2017-12-12 16:20:25 +0100 | [diff] [blame] | 1242 | } |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1243 | return tx_rtp(conn_src, conn_src, proto, from_addr, payload); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1246 | /* Check if the origin of the RTP packet seems plausible */ |
| 1247 | if (!tcfg->rtp_accept_all && check_rtp_origin(conn_src, from_addr)) |
| 1248 | return -1; |
| 1249 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1250 | /* Execute endpoint specific implementation that handles the |
| 1251 | * dispatching of the RTP data */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1252 | return conn_src->conn->endp->type->dispatch_rtp_cb(proto, from_addr, payload, conn_src->conn); |
| 1253 | } |
| 1254 | |
| 1255 | static int tx_rtp(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, |
| 1256 | enum rtp_proto proto, struct sockaddr_in *from_addr, struct msgb *payload) |
| 1257 | { |
| 1258 | if (conn_dst->iuup && proto == MGCP_PROTO_RTP) |
| 1259 | return osmo_iuup_cn_tx_payload(conn_dst->iuup, payload, from_addr); |
| 1260 | else |
| 1261 | return mgcp_send_rtp(proto, from_addr, payload, conn_src, conn_dst); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | /*! set IP Type of Service parameter. |
| 1265 | * \param[in] fd associated file descriptor |
| 1266 | * \param[in] tos dscp value |
| 1267 | * \returns 0 on success, -1 on ERROR */ |
| 1268 | int mgcp_set_ip_tos(int fd, int tos) |
| 1269 | { |
| 1270 | int ret; |
| 1271 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1272 | |
| 1273 | if (ret < 0) |
| 1274 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
| 1277 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1278 | /*! bind RTP port to osmo_fd. |
| 1279 | * \param[in] source_addr source (local) address to bind on |
| 1280 | * \param[in] fd associated file descriptor |
| 1281 | * \param[in] port to bind on |
| 1282 | * \returns 0 on success, -1 on ERROR */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1283 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port) |
| 1284 | { |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1285 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1286 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1287 | rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port, |
| 1288 | NULL, 0, OSMO_SOCK_F_BIND); |
| 1289 | if (rc < 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1290 | LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1291 | source_addr, port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1292 | return -1; |
| 1293 | } |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 1294 | fd->fd = rc; |
| 1295 | 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] | 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1300 | /* 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] | 1301 | static int bind_rtp(struct mgcp_config *cfg, const char *source_addr, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1302 | struct mgcp_rtp_end *rtp_end, int endpno) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1303 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1304 | /* NOTE: The port that is used for RTCP is the RTP port incremented by one |
| 1305 | * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */ |
| 1306 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1307 | if (mgcp_create_bind(source_addr, &rtp_end->rtp, |
| 1308 | rtp_end->local_port) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1309 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1310 | "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1311 | source_addr, rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1312 | goto cleanup0; |
| 1313 | } |
| 1314 | |
| 1315 | if (mgcp_create_bind(source_addr, &rtp_end->rtcp, |
| 1316 | rtp_end->local_port + 1) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1317 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1318 | "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1319 | source_addr, rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1320 | goto cleanup1; |
| 1321 | } |
| 1322 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1323 | /* Set Type of Service (DSCP-Value) as configured via VTY */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1324 | mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 1325 | mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
| 1326 | |
| 1327 | rtp_end->rtp.when = BSC_FD_READ; |
| 1328 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1329 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1330 | "endpoint:0x%x failed to register RTP port %d\n", endpno, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1331 | rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1332 | goto cleanup2; |
| 1333 | } |
| 1334 | |
| 1335 | rtp_end->rtcp.when = BSC_FD_READ; |
| 1336 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1337 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1338 | "endpoint:0x%x failed to register RTCP port %d\n", endpno, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1339 | rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1340 | goto cleanup3; |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | |
| 1345 | cleanup3: |
| 1346 | osmo_fd_unregister(&rtp_end->rtp); |
| 1347 | cleanup2: |
| 1348 | close(rtp_end->rtcp.fd); |
| 1349 | rtp_end->rtcp.fd = -1; |
| 1350 | cleanup1: |
| 1351 | close(rtp_end->rtp.fd); |
| 1352 | rtp_end->rtp.fd = -1; |
| 1353 | cleanup0: |
| 1354 | return -1; |
| 1355 | } |
| 1356 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1357 | /*! bind RTP port to endpoint/connection. |
| 1358 | * \param[in] endp endpoint that holds the RTP connection |
| 1359 | * \param[in] rtp_port port number to bind on |
| 1360 | * \param[in] conn associated RTP connection |
| 1361 | * \returns 0 on success, -1 on ERROR */ |
| 1362 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 1363 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1364 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1365 | char name[512]; |
| 1366 | struct mgcp_rtp_end *end; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1367 | char local_ip_addr[INET_ADDRSTRLEN]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1368 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1369 | snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1370 | end = &conn->end; |
| 1371 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1372 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1373 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1374 | "endpoint:0x%x %u was already bound on conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1375 | ENDPOINT_NUMBER(endp), rtp_port, |
| 1376 | mgcp_conn_dump(conn->conn)); |
| 1377 | |
| 1378 | /* Double bindings should never occour! Since we always allocate |
| 1379 | * connections dynamically and free them when they are not |
| 1380 | * needed anymore, there must be no previous binding leftover. |
| 1381 | * Should there be a connection bound twice, we have a serious |
| 1382 | * problem and must exit immediately! */ |
| 1383 | OSMO_ASSERT(false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | end->local_port = rtp_port; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1387 | end->rtp.cb = rtp_data_net; |
| 1388 | end->rtp.data = conn; |
| 1389 | end->rtcp.data = conn; |
| 1390 | end->rtcp.cb = rtp_data_net; |
| 1391 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1392 | mgcp_get_local_addr(local_ip_addr, conn); |
| 1393 | |
| 1394 | return bind_rtp(endp->cfg, local_ip_addr, end, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1395 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1398 | /*! free allocated RTP and RTCP ports. |
| 1399 | * \param[in] end RTP end */ |
| 1400 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1401 | { |
| 1402 | if (end->rtp.fd != -1) { |
| 1403 | close(end->rtp.fd); |
| 1404 | end->rtp.fd = -1; |
| 1405 | osmo_fd_unregister(&end->rtp); |
| 1406 | } |
| 1407 | |
| 1408 | if (end->rtcp.fd != -1) { |
| 1409 | close(end->rtcp.fd); |
| 1410 | end->rtcp.fd = -1; |
| 1411 | osmo_fd_unregister(&end->rtcp); |
| 1412 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1413 | } |