blob: 51c08d1514c23c22ac90f68256a167ac4ff1f5fe [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2011 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
29#include <sys/socket.h>
30#include <arpa/inet.h>
31
32#include <osmocore/msgb.h>
33#include <osmocore/select.h>
34
35#include <openbsc/debug.h>
36#include <openbsc/mgcp.h>
37#include <openbsc/mgcp_internal.h>
38
39#warning "Make use of the rtp proxy code"
40
41/* attempt to determine byte order */
42#include <sys/types.h>
43#include <sys/param.h>
44#include <limits.h>
45
46#ifndef __BYTE_ORDER
47#error "__BYTE_ORDER should be defined by someone"
48#endif
49
50/* according to rtp_proxy.c RFC 3550 */
51struct rtp_hdr {
52#if __BYTE_ORDER == __LITTLE_ENDIAN
53 uint8_t csrc_count:4,
54 extension:1,
55 padding:1,
56 version:2;
57 uint8_t payload_type:7,
58 marker:1;
59#elif __BYTE_ORDER == __BIG_ENDIAN
60 uint8_t version:2,
61 padding:1,
62 extension:1,
63 csrc_count:4;
64 uint8_t marker:1,
65 payload_type:7;
66#endif
67 uint16_t sequence;
68 uint32_t timestamp;
69 uint32_t ssrc;
70} __attribute__((packed));
71
72
73enum {
74 DEST_NETWORK = 0,
75 DEST_BTS = 1,
76};
77
78enum {
79 PROTO_RTP,
80 PROTO_RTCP,
81};
82
83#define DUMMY_LOAD 0x23
84
85
86static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
87{
88 struct sockaddr_in out;
89 out.sin_family = AF_INET;
90 out.sin_port = port;
91 memcpy(&out.sin_addr, addr, sizeof(*addr));
92
93 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
94}
95
96int mgcp_send_dummy(struct mgcp_endpoint *endp)
97{
98 static char buf[] = { DUMMY_LOAD };
99
100 return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
101 endp->net_end.rtp_port, buf, 1);
102}
103
104static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
105 int payload, struct sockaddr_in *addr, char *data, int len)
106{
107 uint16_t seq;
108 uint32_t timestamp;
109 struct rtp_hdr *rtp_hdr;
110
111 if (len < sizeof(*rtp_hdr))
112 return;
113
114 rtp_hdr = (struct rtp_hdr *) data;
115 seq = ntohs(rtp_hdr->sequence);
116 timestamp = ntohl(rtp_hdr->timestamp);
117
118 if (!state->initialized) {
119 state->seq_no = seq - 1;
120 state->ssrc = state->orig_ssrc = rtp_hdr->ssrc;
121 state->initialized = 1;
122 state->last_timestamp = timestamp;
123 } else if (state->ssrc != rtp_hdr->ssrc) {
124 state->ssrc = rtp_hdr->ssrc;
125 state->seq_offset = (state->seq_no + 1) - seq;
126 state->timestamp_offset = state->last_timestamp - timestamp;
127 state->patch = endp->allow_patch;
128 LOGP(DMGCP, LOGL_NOTICE,
129 "The SSRC changed on 0x%x SSRC: %u offset: %d from %s:%d in %d\n",
130 ENDPOINT_NUMBER(endp), state->ssrc, state->seq_offset,
131 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), endp->conn_mode);
132 }
133
134 /* apply the offset and store it back to the packet */
135 if (state->patch) {
136 seq += state->seq_offset;
137 rtp_hdr->sequence = htons(seq);
138 rtp_hdr->ssrc = state->orig_ssrc;
139
140 timestamp += state->timestamp_offset;
141 rtp_hdr->timestamp = htonl(timestamp);
142 }
143
144 /* seq changed, now compare if we have lost something */
145 if (state->seq_no + 1u != seq)
146 state->lost_no = abs(seq - (state->seq_no + 1));
147 state->seq_no = seq;
148
149 state->last_timestamp = timestamp;
150
151 if (payload < 0)
152 return;
153
154 rtp_hdr->payload_type = payload;
155}
156
157/*
158 * The below code is for dispatching. We have a dedicated port for
159 * the data coming from the net and one to discover the BTS.
160 */
161static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
162{
163 if (!tap->enabled)
164 return 0;
165
166 return sendto(fd, buf, len, 0,
167 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
168}
169
170static int send_transcoder(struct mgcp_rtp_end *end, struct mgcp_config *cfg,
171 int is_rtp, const char *buf, int len)
172{
173 int rc;
174 int port;
175 struct sockaddr_in addr;
176
177 port = is_rtp ? end->rtp_port : end->rtcp_port;
178
179 addr.sin_family = AF_INET;
180 addr.sin_addr = cfg->transcoder_in;
181 addr.sin_port = port;
182
183 rc = sendto(is_rtp ?
184 end->rtp.fd :
185 end->rtcp.fd, buf, len, 0,
186 (struct sockaddr *) &addr, sizeof(addr));
187
188 if (rc != len)
189 LOGP(DMGCP, LOGL_ERROR,
190 "Failed to send data to the transcoder: %s\n",
191 strerror(errno));
192
193 return rc;
194}
195
196static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp,
197 struct sockaddr_in *addr, char *buf, int rc)
198{
199 struct mgcp_trunk_config *tcfg = endp->tcfg;
200 /* For loop toggle the destination and then dispatch. */
201 if (tcfg->audio_loop)
202 dest = !dest;
203
204 /* Loop based on the conn_mode, maybe undoing the above */
205 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
206 dest = !dest;
207
208 if (dest == DEST_NETWORK) {
209 if (is_rtp) {
210 patch_and_count(endp, &endp->bts_state,
211 endp->net_end.payload_type,
212 addr, buf, rc);
213 forward_data(endp->net_end.rtp.fd,
214 &endp->taps[MGCP_TAP_NET_OUT], buf, rc);
215 return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
216 endp->net_end.rtp_port, buf, rc);
217 } else {
218 return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr,
219 endp->net_end.rtcp_port, buf, rc);
220 }
221 } else {
222 if (is_rtp) {
223 patch_and_count(endp, &endp->net_state,
224 endp->bts_end.payload_type,
225 addr, buf, rc);
226 forward_data(endp->bts_end.rtp.fd,
227 &endp->taps[MGCP_TAP_BTS_OUT], buf, rc);
228 return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr,
229 endp->bts_end.rtp_port, buf, rc);
230 } else {
231 return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr,
232 endp->bts_end.rtcp_port, buf, rc);
233 }
234 }
235}
236
237static int recevice_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
238 char *buf, int bufsize)
239{
240 int rc;
241 socklen_t slen = sizeof(*addr);
242
243 rc = recvfrom(fd, buf, bufsize, 0,
244 (struct sockaddr *) addr, &slen);
245 if (rc < 0) {
246 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
247 ENDPOINT_NUMBER(endp), errno, strerror(errno));
248 return -1;
249 }
250
251 /* do not forward aynthing... maybe there is a packet from the bts */
252 if (!endp->allocated)
253 return -1;
254
255 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
256
257 return rc;
258}
259
260static int rtp_data_net(struct bsc_fd *fd, unsigned int what)
261{
262 char buf[4096];
263 struct sockaddr_in addr;
264 struct mgcp_endpoint *endp;
265 int rc, proto;
266
267 endp = (struct mgcp_endpoint *) fd->data;
268
269 rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf));
270 if (rc <= 0)
271 return -1;
272
273 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
274 LOGP(DMGCP, LOGL_ERROR,
275 "Data from wrong address %s on 0x%x\n",
276 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
277 return -1;
278 }
279
280 if (endp->net_end.rtp_port != addr.sin_port &&
281 endp->net_end.rtcp_port != addr.sin_port) {
282 LOGP(DMGCP, LOGL_ERROR,
283 "Data from wrong source port %d on 0x%x\n",
284 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
285 return -1;
286 }
287
288 /* throw away the dummy message */
289 if (rc == 1 && buf[0] == DUMMY_LOAD) {
290 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
291 ENDPOINT_NUMBER(endp));
292 return 0;
293 }
294
295 proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP;
296 endp->net_end.packets += 1;
297
298 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
299 if (endp->is_transcoded)
300 return send_transcoder(&endp->trans_net, endp->cfg, proto == PROTO_RTP, &buf[0], rc);
301 else
302 return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr, &buf[0], rc);
303}
304
305static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
306{
307 struct mgcp_config *cfg = endp->cfg;
308
309 if (proto == PROTO_RTP && endp->bts_end.rtp_port == 0) {
310 if (!cfg->bts_ip ||
311 memcmp(&addr->sin_addr,
312 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
313 memcmp(&addr->sin_addr,
314 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
315
316 endp->bts_end.rtp_port = addr->sin_port;
317 endp->bts_end.addr = addr->sin_addr;
318
319 LOGP(DMGCP, LOGL_NOTICE,
320 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
321 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
322 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
323 }
324 } else if (proto == PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
325 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
326 sizeof(endp->bts_end.addr)) == 0) {
327 endp->bts_end.rtcp_port = addr->sin_port;
328 }
329 }
330}
331
332static int rtp_data_bts(struct bsc_fd *fd, unsigned int what)
333{
334 char buf[4096];
335 struct sockaddr_in addr;
336 struct mgcp_endpoint *endp;
337 struct mgcp_config *cfg;
338 int rc, proto;
339
340 endp = (struct mgcp_endpoint *) fd->data;
341 cfg = endp->cfg;
342
343 rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf));
344 if (rc <= 0)
345 return -1;
346
347 proto = fd == &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP;
348
349 /* We have no idea who called us, maybe it is the BTS. */
350 /* it was the BTS... */
351 discover_bts(endp, proto, &addr);
352
353 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
354 LOGP(DMGCP, LOGL_ERROR,
355 "Data from wrong bts %s on 0x%x\n",
356 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
357 return -1;
358 }
359
360 if (endp->bts_end.rtp_port != addr.sin_port &&
361 endp->bts_end.rtcp_port != addr.sin_port) {
362 LOGP(DMGCP, LOGL_ERROR,
363 "Data from wrong bts source port %d on 0x%x\n",
364 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
365 return -1;
366 }
367
368 /* throw away the dummy message */
369 if (rc == 1 && buf[0] == DUMMY_LOAD) {
370 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
371 ENDPOINT_NUMBER(endp));
372 return 0;
373 }
374
375 /* do this before the loop handling */
376 endp->bts_end.packets += 1;
377
378 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
379 if (endp->is_transcoded)
380 return send_transcoder(&endp->trans_bts, endp->cfg, proto == PROTO_RTP, &buf[0], rc);
381 else
382 return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr, &buf[0], rc);
383}
384
385static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
386 int dest, struct bsc_fd *fd)
387{
388 char buf[4096];
389 struct sockaddr_in addr;
390 struct mgcp_config *cfg;
391 int rc, proto;
392
393 cfg = _endp->cfg;
394 rc = recevice_from(_endp, fd->fd, &addr, buf, sizeof(buf));
395 if (rc <= 0)
396 return -1;
397
398 proto = fd == &end->rtp ? PROTO_RTP : PROTO_RTCP;
399
400 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
401 LOGP(DMGCP, LOGL_ERROR,
402 "Data not coming from transcoder dest: %d %s on 0x%x\n",
403 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
404 return -1;
405 }
406
407 if (end->rtp_port != addr.sin_port &&
408 end->rtcp_port != addr.sin_port) {
409 LOGP(DMGCP, LOGL_ERROR,
410 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
411 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
412 return -1;
413 }
414
415 /* throw away the dummy message */
416 if (rc == 1 && buf[0] == DUMMY_LOAD) {
417 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
418 dest, ENDPOINT_NUMBER(_endp));
419 return 0;
420 }
421
422 end->packets += 1;
423 return send_to(_endp, dest, proto == PROTO_RTP, &addr, &buf[0], rc);
424}
425
426static int rtp_data_trans_net(struct bsc_fd *fd, unsigned int what)
427{
428 struct mgcp_endpoint *endp;
429 endp = (struct mgcp_endpoint *) fd->data;
430
431 return rtp_data_transcoder(&endp->trans_net, endp, DEST_NETWORK, fd);
432}
433
434static int rtp_data_trans_bts(struct bsc_fd *fd, unsigned int what)
435{
436 struct mgcp_endpoint *endp;
437 endp = (struct mgcp_endpoint *) fd->data;
438
439 return rtp_data_transcoder(&endp->trans_bts, endp, DEST_BTS, fd);
440}
441
442static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
443{
444 struct sockaddr_in addr;
445 int on = 1;
446
447 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
448 if (fd->fd < 0) {
449 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
450 return -1;
451 }
452
453 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
454 memset(&addr, 0, sizeof(addr));
455 addr.sin_family = AF_INET;
456 addr.sin_port = htons(port);
457 inet_aton(source_addr, &addr.sin_addr);
458
459 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
460 close(fd->fd);
461 fd->fd = -1;
462 return -1;
463 }
464
465 return 0;
466}
467
468static int set_ip_tos(int fd, int tos)
469{
470 int ret;
471 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
472 &tos, sizeof(tos));
473 return ret != 0;
474}
475
476static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
477{
478 if (create_bind(cfg->source_addr, &rtp_end->rtp, rtp_end->local_port) != 0) {
479 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
480 cfg->source_addr, rtp_end->local_port, endpno);
481 goto cleanup0;
482 }
483
484 if (create_bind(cfg->source_addr, &rtp_end->rtcp, rtp_end->local_port + 1) != 0) {
485 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
486 cfg->source_addr, rtp_end->local_port + 1, endpno);
487 goto cleanup1;
488 }
489
490 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
491 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
492
493 rtp_end->rtp.when = BSC_FD_READ;
494 if (bsc_register_fd(&rtp_end->rtp) != 0) {
495 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
496 rtp_end->local_port, endpno);
497 goto cleanup2;
498 }
499
500 rtp_end->rtcp.when = BSC_FD_READ;
501 if (bsc_register_fd(&rtp_end->rtcp) != 0) {
502 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
503 rtp_end->local_port + 1, endpno);
504 goto cleanup3;
505 }
506
507 return 0;
508
509cleanup3:
510 bsc_unregister_fd(&rtp_end->rtp);
511cleanup2:
512 close(rtp_end->rtcp.fd);
513 rtp_end->rtcp.fd = -1;
514cleanup1:
515 close(rtp_end->rtp.fd);
516 rtp_end->rtp.fd = -1;
517cleanup0:
518 return -1;
519}
520
521static int int_bind(const char *port,
522 struct mgcp_rtp_end *end, int (*cb)(struct bsc_fd *, unsigned),
523 struct mgcp_endpoint *_endp, int rtp_port)
524{
525 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
526 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
527 port, ENDPOINT_NUMBER(_endp));
528 mgcp_free_rtp_port(end);
529 }
530
531 end->local_port = rtp_port;
532 end->rtp.cb = cb;
533 end->rtp.data = _endp;
534 end->rtcp.data = _endp;
535 end->rtcp.cb = cb;
536 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
537}
538
539
540int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
541{
542 return int_bind("bts-port", &endp->bts_end,
543 rtp_data_bts, endp, rtp_port);
544}
545
546int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
547{
548 return int_bind("net-port", &endp->net_end,
549 rtp_data_net, endp, rtp_port);
550}
551
552int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
553{
554 return int_bind("trans-net", &endp->trans_net,
555 rtp_data_trans_net, endp, rtp_port);
556}
557
558int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
559{
560 return int_bind("trans-bts", &endp->trans_bts,
561 rtp_data_trans_bts, endp, rtp_port);
562}
563
564int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
565{
566 if (end->rtp.fd != -1) {
567 close(end->rtp.fd);
568 end->rtp.fd = -1;
569 bsc_unregister_fd(&end->rtp);
570 }
571
572 if (end->rtcp.fd != -1) {
573 close(end->rtcp.fd);
574 end->rtcp.fd = -1;
575 bsc_unregister_fd(&end->rtcp);
576 }
577
578 return 0;
579}