blob: fe82fb510a099c2ea7d5fb9e790a7526b9786c0a [file] [log] [blame]
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009 by on-waves.com
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 General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <ctype.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
30#include <limits.h>
31#include <unistd.h>
32
33#include <sys/socket.h>
34#include <arpa/inet.h>
35
36#include <openbsc/debug.h>
37#include <openbsc/msgb.h>
38#include <openbsc/talloc.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/select.h>
41#include <openbsc/mgcp.h>
42#include <openbsc/telnet_interface.h>
43
44#include <vty/command.h>
45#include <vty/vty.h>
46
47#warning "Make use of the rtp proxy code"
48
49static int source_port = 2427;
50static const char *local_ip = NULL;
51static const char *source_addr = "0.0.0.0";
52static unsigned int number_endpoints = 0;
53static const char *bts_ip = NULL;
54static struct in_addr bts_in;
55static const char *audio_name = "GSM-EFR/8000";
56static int audio_payload = 97;
57static int audio_loop = 0;
58static int early_bind = 0;
59
60static char *forward_ip = NULL;
61static int forward_port = 0;
62
63enum mgcp_connection_mode {
64 MGCP_CONN_NONE = 0,
65 MGCP_CONN_RECV_ONLY = 1,
66 MGCP_CONN_SEND_ONLY = 2,
67 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
68};
69
70enum {
71 DEST_NETWORK = 0,
72 DEST_BTS = 1,
73};
74
75enum {
76 PROTO_RTP,
77 PROTO_RTCP,
78};
79
80#define CI_UNUSED 0
81static unsigned int last_call_id = 0;
82
83struct mgcp_endpoint {
84 int ci;
85 char *callid;
86 char *local_options;
87 int conn_mode;
88
89 /* the local rtp port we are binding to */
90 int rtp_port;
91
92 /*
93 * RTP mangling:
94 * - we get RTP and RTCP to us and need to forward to the BTS
95 * - we get RTP and RTCP from the BTS and forward to the network
96 */
97 struct bsc_fd local_rtp;
98 struct bsc_fd local_rtcp;
99
100 struct in_addr remote;
101 struct in_addr bts;
102
103 /* in network byte order */
104 int net_rtp, net_rtcp;
105 int bts_rtp, bts_rtcp;
106};
107
108static struct mgcp_endpoint *endpoints = NULL;
109#define ENDPOINT_NUMBER(endp) abs(endp - endpoints)
110
111/**
112 * Macro for tokenizing MGCP messages and SDP in one go.
113 *
114 */
115#define MSG_TOKENIZE_START \
116 line_start = 0; \
117 for (i = 0; i < msgb_l3len(msg); ++i) { \
118 /* we have a line end */ \
119 if (msg->l3h[i] == '\n') { \
120 /* skip the first line */ \
121 if (line_start == 0) { \
122 line_start = i + 1; \
123 continue; \
124 } \
125 \
126 /* check if we have a proper param */ \
127 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
128 } else if (i - line_start > 2 \
129 && islower(msg->l3h[line_start]) \
130 && msg->l3h[line_start + 1] == '=') { \
131 } else if (i - line_start < 3 \
132 || msg->l3h[line_start + 1] != ':' \
133 || msg->l3h[line_start + 2] != ' ') \
134 goto error; \
135 \
136 msg->l3h[i] = '\0'; \
137 if (msg->l3h[i-1] == '\r') \
138 msg->l3h[i-1] = '\0';
139
140#define MSG_TOKENIZE_END \
141 line_start = i + 1; \
142 } \
143 }
144
145
146struct mgcp_msg_ptr {
147 unsigned int start;
148 unsigned int length;
149};
150
151struct mgcp_request {
152 char *name;
153 int (*handle_request) (int fd, struct msgb *msg, struct sockaddr_in *source);
154 char *debug_name;
155};
156
157#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
158 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
159
160static int handle_audit_endpoint(int fd, struct msgb *msg, struct sockaddr_in *source);
161static int handle_create_con(int fd, struct msgb *msg, struct sockaddr_in *source);
162static int handle_delete_con(int fd, struct msgb *msg, struct sockaddr_in *source);
163static int handle_modify_con(int fd, struct msgb *msg, struct sockaddr_in *source);
164
165static int generate_call_id()
166{
167 int i;
168
169 /* use the call id */
170 ++last_call_id;
171
172 /* handle wrap around */
173 if (last_call_id == CI_UNUSED)
174 ++last_call_id;
175
176 /* callstack can only be of size number_of_endpoints */
177 /* verify that the call id is free, e.g. in case of overrun */
178 for (i = 1; i < number_endpoints; ++i)
179 if (endpoints[i].ci == last_call_id)
180 return generate_call_id();
181
182 return last_call_id;
183}
184
185/* FIXIME/TODO: need to have a list of pending transactions and check that */
186static unsigned int generate_transaction_id()
187{
188 return abs(rand());
189}
190
191static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
192{
193 struct sockaddr_in out;
194 out.sin_family = AF_INET;
195 out.sin_port = port;
196 memcpy(&out.sin_addr, addr, sizeof(*addr));
197
198 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
199}
200
201/*
202 * There is data coming. We will have to figure out if it
203 * came from the BTS or the MediaGateway of the MSC. On top
204 * of that we need to figure out if it was RTP or RTCP.
205 *
206 * Currently we do not communicate with the BSC so we have
207 * no idea where the BTS is listening for RTP and need to
208 * do the classic routing trick. Wait for the first packet
209 * from the BTS and then go ahead.
210 */
211static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
212{
213 char buf[4096];
214 struct sockaddr_in addr;
215 socklen_t slen = sizeof(addr);
216 struct mgcp_endpoint *endp;
217 int rc, dest, proto;
218
219 endp = (struct mgcp_endpoint *) fd->data;
220
221 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
222 (struct sockaddr *) &addr, &slen);
223 if (rc < 0) {
224 DEBUGP(DMGCP, "Failed to receive message on: 0x%x\n",
225 ENDPOINT_NUMBER(endp));
226 return -1;
227 }
228
229 /* do not forward aynthing... maybe there is a packet from the bts */
230 if (endp->ci == CI_UNUSED) {
231 DEBUGP(DMGCP, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
232 return -1;
233 }
234
235 /*
236 * Figure out where to forward it to. This code assumes that we
237 * have received the Connection Modify and know who is a legitimate
238 * partner. According to the spec we could attempt to forward even
239 * after the Create Connection but we will not as we are not really
240 * able to tell if this is legitimate.
241 */
242 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
243 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
244 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
245 ? DEST_BTS : DEST_NETWORK;
246 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
247
248 /* We have no idea who called us, maybe it is the BTS. */
249 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || forward_ip)) {
250 /* it was the BTS... */
251 if (!bts_ip || memcmp(&addr.sin_addr, &bts_in, sizeof(bts_in)) == 0) {
252 if (fd == &endp->local_rtp) {
253 endp->bts_rtp = addr.sin_port;
254 } else {
255 endp->bts_rtcp = addr.sin_port;
256 }
257
258 endp->bts = addr.sin_addr;
259 DEBUGP(DMGCP, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
260 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
261 }
262 }
263
264 /* dispatch */
265 if (audio_loop)
266 dest = !dest;
267
268 if (dest == DEST_NETWORK) {
269 return udp_send(fd->fd, &endp->remote,
270 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
271 buf, rc);
272 } else {
273 return udp_send(fd->fd, &endp->bts,
274 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
275 buf, rc);
276 }
277}
278
279static int create_bind(struct bsc_fd *fd, int port)
280{
281 struct sockaddr_in addr;
282 int on = 1;
283
284 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
285 if (fd->fd < 0)
286 return -1;
287
288 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
289 memset(&addr, 0, sizeof(addr));
290 addr.sin_family = AF_INET;
291 addr.sin_port = htons(port);
292 inet_aton(source_addr, &addr.sin_addr);
293
294 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
295 return -1;
296
297 return 0;
298}
299
300static int bind_rtp(struct mgcp_endpoint *endp)
301{
302 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
303 DEBUGP(DMGCP, "Failed to create RTP port: %d on 0x%x\n",
304 endp->rtp_port, ENDPOINT_NUMBER(endp));
305 goto cleanup0;
306 }
307
308 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
309 DEBUGP(DMGCP, "Failed to create RTCP port: %d on 0x%x\n",
310 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
311 goto cleanup1;
312 }
313
314 endp->local_rtp.cb = rtp_data_cb;
315 endp->local_rtp.data = endp;
316 endp->local_rtp.when = BSC_FD_READ;
317 if (bsc_register_fd(&endp->local_rtp) != 0) {
318 DEBUGP(DMGCP, "Failed to register RTP port %d on 0x%x\n",
319 endp->rtp_port, ENDPOINT_NUMBER(endp));
320 goto cleanup2;
321 }
322
323 endp->local_rtcp.cb = rtp_data_cb;
324 endp->local_rtcp.data = endp;
325 endp->local_rtcp.when = BSC_FD_READ;
326 if (bsc_register_fd(&endp->local_rtcp) != 0) {
327 DEBUGP(DMGCP, "Failed to register RTCP port %d on 0x%x\n",
328 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
329 goto cleanup3;
330 }
331
332 return 0;
333
334cleanup3:
335 bsc_unregister_fd(&endp->local_rtp);
336cleanup2:
337 close(endp->local_rtcp.fd);
338 endp->local_rtcp.fd = -1;
339cleanup1:
340 close(endp->local_rtp.fd);
341 endp->local_rtp.fd = -1;
342cleanup0:
343 return -1;
344}
345
346/*
347 * array of function pointers for handling various
348 * messages. In the future this might be binary sorted
349 * for performance reasons.
350 */
351static const struct mgcp_request mgcp_requests [] = {
352 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
353 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
354 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
355 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
356};
357
358static int send_response_with_data(int fd, int code, const char *msg, const char *trans,
359 const char *data, struct sockaddr_in *source)
360{
361 char buf[4096];
362 int len;
363
364 if (data) {
365 len = snprintf(buf, sizeof(buf), "%d %s\n%s", code, trans, data);
366 } else {
367 len = snprintf(buf, sizeof(buf), "%d %s\n", code, trans);
368 }
369 DEBUGP(DMGCP, "Sending response: code: %d for '%s'\n", code, msg);
370
371 if (source)
372 return sendto(fd, buf, len, 0, (struct sockaddr *)source, sizeof(*source));
373 else
374 return write(fd, buf, len);
375}
376
377static int send_response(int fd, int code, const char *msg,
378 const char *trans, struct sockaddr_in *source)
379{
380 return send_response_with_data(fd, code, msg, trans, NULL, source);
381}
382
383static int send_with_sdp(int fd, struct mgcp_endpoint *endp, const char *msg, const char *trans_id, struct sockaddr_in *source)
384{
385 const char *addr = local_ip;
386 char sdp_record[4096];
387
388 if (!addr)
389 addr = source_addr;
390
391 snprintf(sdp_record, sizeof(sdp_record) - 1,
392 "I: %d\n\n"
393 "v=0\r\n"
394 "c=IN IP4 %s\r\n"
395 "m=audio %d RTP/AVP %d\r\n"
396 "a=rtpmap:%d %s\r\n",
397 endp->ci, addr, endp->rtp_port,
398 audio_payload, audio_payload, audio_name);
399 return send_response_with_data(fd, 200, msg, trans_id, sdp_record, source);
400}
401
402/* send a static record */
403int mgcp_send_rsip(int fd, struct sockaddr_in *source)
404{
405 char reset[4096];
406 int len, rc;
407
408 len = snprintf(reset, sizeof(reset) - 1,
409 "RSIP %u *@mgw MGCP 1.0\n"
410 "RM: restart\n", generate_transaction_id());
411 if (source)
412 rc = sendto(fd, reset, len, 0, (struct sockaddr *) source, sizeof(*source));
413 else
414 rc = write(fd, reset, len);
415
416 if (rc < 0) {
417 DEBUGP(DMGCP, "Failed to send RSIP: %d\n", rc);
418 }
419
420 return rc;
421}
422
423/*
424 * handle incoming messages:
425 * - this can be a command (four letters, space, transaction id)
426 * - or a response (three numbers, space, transaction id)
427 */
428int mgcp_handle_message(int fd, struct msgb *msg, struct sockaddr_in *source)
429{
430 int code;
431
432 if (msg->len < 4) {
433 DEBUGP(DMGCP, "mgs too short: %d\n", msg->len);
434 return -1;
435 }
436
437 /* attempt to treat it as a response */
438 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
439 DEBUGP(DMGCP, "Response: Code: %d\n", code);
440 } else {
441 int i, handled = 0;
442 msg->l3h = &msg->l2h[4];
443 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
444 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
445 handled = 1;
446 mgcp_requests[i].handle_request(fd, msg, source);
447 }
448 if (!handled) {
449 DEBUGP(DMGCP, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
450 }
451 }
452
453 return 0;
454}
455
456/* string tokenizer for the poor */
457static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
458{
459 int i, found = 0;
460
461 int whitespace = 1;
462 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
463 /* if we have a space we found an end */
464 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
465 if (!whitespace) {
466 ++found;
467 whitespace = 1;
468 ptrs->length = i - ptrs->start - 1;
469 ++ptrs;
470 --ptrs_length;
471 } else {
472 /* skip any number of whitespace */
473 }
474
475 /* line end... stop */
476 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
477 break;
478 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
479 /* line end, be done */
480 break;
481 } else if (whitespace) {
482 whitespace = 0;
483 ptrs->start = i;
484 }
485 }
486
487 if (ptrs_length == 0)
488 return -1;
489 return found;
490}
491
492static struct mgcp_endpoint *find_endpoint(const char *mgcp)
493{
494 char *endptr = NULL;
495 unsigned int gw = INT_MAX;
496
497 gw = strtoul(mgcp, &endptr, 16);
498 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
499 DEBUGP(DMGCP, "Not able to find endpoint: '%s'\n", mgcp);
500 return NULL;
501 }
502
503 return &endpoints[gw];
504}
505
506static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
507 const char **transaction_id, struct mgcp_endpoint **endp)
508{
509 int found;
510
511 if (size < 3) {
512 DEBUGP(DMGCP, "Not enough space in ptr\n");
513 return -1;
514 }
515
516 found = find_msg_pointers(msg, ptr, size);
517
518 if (found < 3) {
519 DEBUGP(DMGCP, "Gateway: Not enough params. Found: %d\n", found);
520 return -1;
521 }
522
523 /*
524 * replace the space with \0. the main method gurantess that
525 * we still have + 1 for null termination
526 */
527 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
528 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
529 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
530 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
531
532 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
533 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
534 DEBUGP(DMGCP, "Wrong MGCP version. Not handling: '%s' '%s'\n",
535 (const char *)&msg->l3h[ptr[3].start],
536 (const char *)&msg->l3h[ptr[2].start]);
537 return -1;
538 }
539
540 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
541 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
542 return *endp == NULL;
543}
544
545static int verify_call_id(const struct mgcp_endpoint *endp,
546 const char *callid)
547{
548 if (strcmp(endp->callid, callid) != 0) {
549 DEBUGP(DMGCP, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
550 ENDPOINT_NUMBER(endp), endp->callid, callid);
551 return -1;
552 }
553
554 return 0;
555}
556
557static int verify_ci(const struct mgcp_endpoint *endp,
558 const char *ci)
559{
560 if (atoi(ci) != endp->ci) {
561 DEBUGP(DMGCP, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
562 ENDPOINT_NUMBER(endp), endp->ci, ci);
563 return -1;
564 }
565
566 return 0;
567}
568
569static int handle_audit_endpoint(int fd, struct msgb *msg, struct sockaddr_in *source)
570{
571 struct mgcp_msg_ptr data_ptrs[6];
572 int found, response;
573 const char *trans_id;
574 struct mgcp_endpoint *endp;
575
576 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
577 if (found != 0)
578 response = 500;
579 else
580 response = 200;
581
582 return send_response(fd, response, "AUEP", trans_id, source);
583}
584
585static int parse_conn_mode(const char* msg, int *conn_mode)
586{
587 int ret = 0;
588 if (strcmp(msg, "recvonly") == 0)
589 *conn_mode = MGCP_CONN_RECV_ONLY;
590 else if (strcmp(msg, "sendrecv") == 0)
591 *conn_mode = MGCP_CONN_RECV_SEND;
592 else {
593 DEBUGP(DMGCP, "Unknown connection mode: '%s'\n", msg);
594 ret = -1;
595 }
596
597 return ret;
598}
599
600static int handle_create_con(int fd, struct msgb *msg, struct sockaddr_in *source)
601{
602 struct mgcp_msg_ptr data_ptrs[6];
603 int found, i, line_start;
604 const char *trans_id;
605 struct mgcp_endpoint *endp;
606 int error_code = 500;
607
608 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
609 if (found != 0)
610 return send_response(fd, 500, "CRCX", trans_id, source);
611
612 if (endp->ci != CI_UNUSED) {
613 DEBUGP(DMGCP, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
614 return send_response(fd, 500, "CRCX", trans_id, source);
615 }
616
617 /* parse CallID C: and LocalParameters L: */
618 MSG_TOKENIZE_START
619 switch (msg->l3h[line_start]) {
620 case 'L':
621 endp->local_options = talloc_strdup(endpoints,
622 (const char *)&msg->l3h[line_start + 3]);
623 break;
624 case 'C':
625 endp->callid = talloc_strdup(endpoints,
626 (const char *)&msg->l3h[line_start + 3]);
627 break;
628 case 'M':
629 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
630 &endp->conn_mode) != 0) {
631 error_code = 517;
632 goto error2;
633 }
634 break;
635 default:
636 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
637 msg->l3h[line_start], msg->l3h[line_start],
638 ENDPOINT_NUMBER(endp));
639 break;
640 }
641 MSG_TOKENIZE_END
642
643 /* initialize */
644 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
645
646 /* set to zero until we get the info */
647 memset(&endp->remote, 0, sizeof(endp->remote));
648
649 /* bind to the port now */
650 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
651 if (!early_bind && bind_rtp(endp) != 0)
652 goto error2;
653
654 /* assign a local call identifier or fail */
655 endp->ci = generate_call_id();
656 if (endp->ci == CI_UNUSED)
657 goto error2;
658
659 DEBUGP(DMGCP, "Creating endpoint on: 0x%x CI: %u port: %u\n",
660 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
661 return send_with_sdp(fd, endp, "CRCX", trans_id, source);
662error:
663 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
664 hexdump(msg->l3h, msgb_l3len(msg)),
665 ENDPOINT_NUMBER(endp), line_start, i);
666 return send_response(fd, error_code, "CRCX", trans_id, source);
667
668error2:
669 DEBUGP(DMGCP, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
670 return send_response(fd, error_code, "CRCX", trans_id, source);
671}
672
673static int handle_modify_con(int fd, struct msgb *msg, struct sockaddr_in *source)
674{
675 struct mgcp_msg_ptr data_ptrs[6];
676 int found, i, line_start;
677 const char *trans_id;
678 struct mgcp_endpoint *endp;
679 int error_code = 500;
680
681 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
682 if (found != 0)
683 return send_response(fd, error_code, "MDCX", trans_id, source);
684
685 if (endp->ci == CI_UNUSED) {
686 DEBUGP(DMGCP, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
687 return send_response(fd, error_code, "MDCX", trans_id, source);
688 }
689
690 MSG_TOKENIZE_START
691 switch (msg->l3h[line_start]) {
692 case 'C': {
693 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
694 goto error3;
695 break;
696 }
697 case 'I': {
698 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
699 goto error3;
700 break;
701 }
702 case 'L':
703 /* skip */
704 break;
705 case 'M':
706 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
707 &endp->conn_mode) != 0) {
708 error_code = 517;
709 goto error3;
710 }
711 break;
712 case '\0':
713 /* SDP file begins */
714 break;
715 case 'a':
716 case 'o':
717 case 's':
718 case 't':
719 case 'v':
720 /* skip these SDP attributes */
721 break;
722 case 'm': {
723 int port;
724 const char *param = (const char *)&msg->l3h[line_start];
725
726 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
727 endp->net_rtp = htons(port);
728 endp->net_rtcp = htons(port + 1);
729 }
730 break;
731 }
732 case 'c': {
733 char ipv4[16];
734 const char *param = (const char *)&msg->l3h[line_start];
735
736 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
737 inet_aton(ipv4, &endp->remote);
738 }
739 break;
740 }
741 default:
742 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
743 msg->l3h[line_start], msg->l3h[line_start],
744 ENDPOINT_NUMBER(endp));
745 break;
746 }
747 MSG_TOKENIZE_END
748
749 /* modify */
750 DEBUGP(DMGCP, "Modified endpoint on: 0x%x Server: %s:%u\n",
751 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->net_rtp);
752 return send_with_sdp(fd, endp, "MDCX", trans_id, source);
753
754error:
755 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
756 hexdump(msg->l3h, msgb_l3len(msg)),
757 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
758 return send_response(fd, error_code, "MDCX", trans_id, source);
759
760error3:
761 return send_response(fd, error_code, "MDCX", trans_id, source);
762}
763
764static int handle_delete_con(int fd, struct msgb *msg, struct sockaddr_in *source)
765{
766 struct mgcp_msg_ptr data_ptrs[6];
767 int found, i, line_start;
768 const char *trans_id;
769 struct mgcp_endpoint *endp;
770 int error_code = 500;
771
772 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
773 if (found != 0)
774 return send_response(fd, error_code, "DLCX", trans_id, source);
775
776 if (endp->ci == CI_UNUSED) {
777 DEBUGP(DMGCP, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
778 return send_response(fd, error_code, "DLCX", trans_id, source);
779 }
780
781 MSG_TOKENIZE_START
782 switch (msg->l3h[line_start]) {
783 case 'C': {
784 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
785 goto error3;
786 break;
787 }
788 case 'I': {
789 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
790 goto error3;
791 break;
792 }
793 default:
794 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
795 msg->l3h[line_start], msg->l3h[line_start],
796 ENDPOINT_NUMBER(endp));
797 break;
798 }
799 MSG_TOKENIZE_END
800
801
802 /* free the connection */
803 DEBUGP(DMGCP, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
804 endp->ci= CI_UNUSED;
805 talloc_free(endp->callid);
806 talloc_free(endp->local_options);
807
808 if (!early_bind) {
809 bsc_unregister_fd(&endp->local_rtp);
810 bsc_unregister_fd(&endp->local_rtcp);
811 }
812
813 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
814
815 return send_response(fd, 250, "DLCX", trans_id, source);
816
817error:
818 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
819 hexdump(msg->l3h, msgb_l3len(msg)),
820 ENDPOINT_NUMBER(endp), line_start, i);
821 return send_response(fd, error_code, "DLCX", trans_id, source);
822
823error3:
824 return send_response(fd, error_code, "DLCX", trans_id, source);
825}
826
827/*
828 * vty code for mgcp below
829 */
830struct cmd_node mgcp_node = {
831 MGCP_NODE,
832 "%s(mgcp)#",
833 1,
834};
835
836static int config_write_mgcp(struct vty *vty)
837{
838 vty_out(vty, "mgcp%s", VTY_NEWLINE);
839 if (local_ip)
840 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
841 if (bts_ip)
842 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
843 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
844 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
845 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
846 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
847 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
848 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
849 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
850 vty_out(vty, " endpoints %u%s", number_endpoints, VTY_NEWLINE);
851 if (forward_ip)
852 vty_out(vty, " forward audio ip %s%s", forward_ip, VTY_NEWLINE);
853 if (forward_port != 0)
854 vty_out(vty, " forward audio port %d%s", forward_port, VTY_NEWLINE);
855
856 return CMD_SUCCESS;
857}
858
859DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
860 SHOW_STR "Display information about the MGCP Media Gateway")
861{
862 int i;
863
864 vty_out(vty, "MGCP is up and running with %u endpoints:%s", number_endpoints - 1, VTY_NEWLINE);
865 for (i = 1; i < number_endpoints; ++i) {
866 struct mgcp_endpoint *endp = &endpoints[i];
867 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
868 i, endp->ci,
869 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
870 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
871 }
872
873 return CMD_SUCCESS;
874}
875
876DEFUN(cfg_mgcp,
877 cfg_mgcp_cmd,
878 "mgcp",
879 "Configure the MGCP")
880{
881 vty->node = MGCP_NODE;
882 return CMD_SUCCESS;
883}
884
885DEFUN(cfg_mgcp_local_ip,
886 cfg_mgcp_local_ip_cmd,
887 "local ip IP",
888 "Set the IP to be used in SDP records")
889{
890 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
891 return CMD_SUCCESS;
892}
893
894DEFUN(cfg_mgcp_bts_ip,
895 cfg_mgcp_bts_ip_cmd,
896 "bts ip IP",
897 "Set the IP of the BTS for RTP forwarding")
898{
899 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
900 inet_aton(bts_ip, &bts_in);
901 return CMD_SUCCESS;
902}
903
904DEFUN(cfg_mgcp_bind_ip,
905 cfg_mgcp_bind_ip_cmd,
906 "bind ip IP",
907 "Bind the MGCP to this local addr")
908{
909 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
910 return CMD_SUCCESS;
911}
912
913DEFUN(cfg_mgcp_bind_port,
914 cfg_mgcp_bind_port_cmd,
915 "bind port <0-65534>",
916 "Bind the MGCP to this port")
917{
918 unsigned int port = atoi(argv[0]);
919 if (port > 65534) {
920 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
921 return CMD_WARNING;
922 }
923
924 source_port = port;
925 return CMD_SUCCESS;
926}
927
928DEFUN(cfg_mgcp_bind_early,
929 cfg_mgcp_bind_early_cmd,
930 "bind early (0|1)",
931 "Bind all RTP ports early")
932{
933 unsigned int bind = atoi(argv[0]);
934 if (bind != 0 && bind != 1) {
935 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
936 return CMD_WARNING;
937 }
938
939 early_bind = bind == 1;
940 return CMD_SUCCESS;
941}
942
943DEFUN(cfg_mgcp_rtp_base_port,
944 cfg_mgcp_rtp_base_port_cmd,
945 "rtp base <0-65534>",
946 "Base port to use")
947{
948 unsigned int port = atoi(argv[0]);
949 if (port > 65534) {
950 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
951 return CMD_WARNING;
952 }
953
954 rtp_base_port = port;
955 return CMD_SUCCESS;
956}
957
958DEFUN(cfg_mgcp_sdp_payload_number,
959 cfg_mgcp_sdp_payload_number_cmd,
960 "sdp audio payload number <1-255>",
961 "Set the audio codec to use")
962{
963 unsigned int payload = atoi(argv[0]);
964 if (payload > 255) {
965 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
966 return CMD_WARNING;
967 }
968
969 audio_payload = payload;
970 return CMD_SUCCESS;
971}
972
973DEFUN(cfg_mgcp_sdp_payload_name,
974 cfg_mgcp_sdp_payload_name_cmd,
975 "sdp audio payload name NAME",
976 "Set the audio name to use")
977{
978 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
979 return CMD_SUCCESS;
980}
981
982DEFUN(cfg_mgcp_loop,
983 cfg_mgcp_loop_cmd,
984 "loop (0|1)",
985 "Loop the audio")
986{
987 audio_loop = atoi(argv[0]);
988 return CMD_SUCCESS;
989}
990
991DEFUN(cfg_mgcp_number_endp,
992 cfg_mgcp_number_endp_cmd,
993 "number endpoints <0-65534>",
994 "The number of endpoints to allocate. This is not dynamic.")
995{
996 /* + 1 as we start counting at one */
997 number_endpoints = atoi(argv[0]) + 1;
998 return CMD_SUCCESS;
999}
1000
1001DEFUN(cfg_mgcp_forward_ip,
1002 cfg_mgcp_forward_ip_cmd,
1003 "forward audio ip IP",
1004 "Forward packets from and to the IP. This disables most of the MGCP feature.")
1005{
1006 if (forward_ip)
1007 talloc_free(forward_ip);
1008 forward_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
1009 return CMD_SUCCESS;
1010}
1011
1012DEFUN(cfg_mgcp_forward_port,
1013 cfg_mgcp_forward_port_cmd,
1014 "forward audio port <1-15000>",
1015 "Forward packets from and to the port. This disables most of the MGCP feature.")
1016{
1017 forward_port = atoi(argv[0]);
1018 return CMD_SUCCESS;
1019}
1020
1021int mgcp_vty_init(void)
1022{
1023 install_element(VIEW_NODE, &show_mgcp_cmd);
1024
1025 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1026 install_node(&mgcp_node, config_write_mgcp);
1027 install_default(MGCP_NODE);
1028 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1029 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1030 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1031 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1032 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1033 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1034 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1035 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
1036 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
1037 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
1038 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
1039 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
1040 return 0;
1041}
1042
1043int mgcp_parse_config(const char *config_file, struct gsm_network *dummy_network)
1044{
1045 int i, rc;
1046
1047 rc = vty_read_config_file(config_file);
1048 if (rc < 0) {
1049 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1050 return rc;
1051 }
1052
1053
1054 if (!bts_ip)
1055 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
1056
1057 endpoints = _talloc_zero_array(tall_bsc_ctx,
1058 sizeof(struct mgcp_endpoint),
1059 number_endpoints, "endpoints");
1060 if (!endpoints) {
1061 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1062 return -1;
1063 }
1064
1065 /* Initialize all endpoints */
1066 for (i = 0; i < number_endpoints; ++i) {
1067 endpoints[i].local_rtp.fd = -1;
1068 endpoints[i].local_rtcp.fd = -1;
1069 endpoints[i].ci = CI_UNUSED;
1070 }
1071
1072 /*
1073 * This application supports two modes.
1074 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
1075 * 2.) plain forwarding of RTP packets on the endpoints.
1076 * both modes are mutual exclusive
1077 */
1078 if (forward_ip) {
1079 int port = rtp_base_port;
1080 if (forward_port != 0)
1081 port = forward_port;
1082
1083 if (!early_bind) {
1084 DEBUGP(DMGCP, "Forwarding requires early bind.\n");
1085 return -1;
1086 }
1087
1088 /*
1089 * Store the forward IP and assign a ci. For early bind
1090 * the sockets will be created after this.
1091 */
1092 for (i = 1; i < number_endpoints; ++i) {
1093 struct mgcp_endpoint *endp = &endpoints[i];
1094 inet_aton(forward_ip, &endp->remote);
1095 endp->ci = CI_UNUSED + 23;
1096 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
1097 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
1098 }
1099
1100 DEBUGP(DMGCP, "Configured for Audio Forwarding.\n");
1101 }
1102
1103 /* early bind */
1104 if (early_bind) {
1105 for (i = 1; i < number_endpoints; ++i) {
1106 struct mgcp_endpoint *endp = &endpoints[i];
1107 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
1108 if (bind_rtp(endp) != 0)
1109 return -1;
1110 }
1111 }
1112
1113 return !!forward_ip;
1114}