blob: 763dadbcc502ff30b439e036410a1ba5835e1c3f [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) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100224 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100225 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) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100231 LOGP(DMGCP, LOGL_ERROR, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100232 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;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100259 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100260 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);
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100285 if (fd->fd < 0) {
286 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100287 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100288 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100289
290 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
291 memset(&addr, 0, sizeof(addr));
292 addr.sin_family = AF_INET;
293 addr.sin_port = htons(port);
294 inet_aton(source_addr, &addr.sin_addr);
295
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100296 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100297 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100298 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100299
300 return 0;
301}
302
303static int bind_rtp(struct mgcp_endpoint *endp)
304{
305 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100306 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
307 source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100308 goto cleanup0;
309 }
310
311 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100312 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
313 source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100314 goto cleanup1;
315 }
316
317 endp->local_rtp.cb = rtp_data_cb;
318 endp->local_rtp.data = endp;
319 endp->local_rtp.when = BSC_FD_READ;
320 if (bsc_register_fd(&endp->local_rtp) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100321 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100322 endp->rtp_port, ENDPOINT_NUMBER(endp));
323 goto cleanup2;
324 }
325
326 endp->local_rtcp.cb = rtp_data_cb;
327 endp->local_rtcp.data = endp;
328 endp->local_rtcp.when = BSC_FD_READ;
329 if (bsc_register_fd(&endp->local_rtcp) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100330 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100331 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
332 goto cleanup3;
333 }
334
335 return 0;
336
337cleanup3:
338 bsc_unregister_fd(&endp->local_rtp);
339cleanup2:
340 close(endp->local_rtcp.fd);
341 endp->local_rtcp.fd = -1;
342cleanup1:
343 close(endp->local_rtp.fd);
344 endp->local_rtp.fd = -1;
345cleanup0:
346 return -1;
347}
348
349/*
350 * array of function pointers for handling various
351 * messages. In the future this might be binary sorted
352 * for performance reasons.
353 */
354static const struct mgcp_request mgcp_requests [] = {
355 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
356 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
357 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
358 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
359};
360
361static int send_response_with_data(int fd, int code, const char *msg, const char *trans,
362 const char *data, struct sockaddr_in *source)
363{
364 char buf[4096];
365 int len;
366
367 if (data) {
368 len = snprintf(buf, sizeof(buf), "%d %s\n%s", code, trans, data);
369 } else {
370 len = snprintf(buf, sizeof(buf), "%d %s\n", code, trans);
371 }
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100372 LOGP(DMGCP, LOGL_NOTICE, "Sending response: code: %d for '%s'\n", code, msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100373
374 if (source)
375 return sendto(fd, buf, len, 0, (struct sockaddr *)source, sizeof(*source));
376 else
377 return write(fd, buf, len);
378}
379
380static int send_response(int fd, int code, const char *msg,
381 const char *trans, struct sockaddr_in *source)
382{
383 return send_response_with_data(fd, code, msg, trans, NULL, source);
384}
385
386static int send_with_sdp(int fd, struct mgcp_endpoint *endp, const char *msg, const char *trans_id, struct sockaddr_in *source)
387{
388 const char *addr = local_ip;
389 char sdp_record[4096];
390
391 if (!addr)
392 addr = source_addr;
393
394 snprintf(sdp_record, sizeof(sdp_record) - 1,
395 "I: %d\n\n"
396 "v=0\r\n"
397 "c=IN IP4 %s\r\n"
398 "m=audio %d RTP/AVP %d\r\n"
399 "a=rtpmap:%d %s\r\n",
400 endp->ci, addr, endp->rtp_port,
401 audio_payload, audio_payload, audio_name);
402 return send_response_with_data(fd, 200, msg, trans_id, sdp_record, source);
403}
404
405/* send a static record */
406int mgcp_send_rsip(int fd, struct sockaddr_in *source)
407{
408 char reset[4096];
409 int len, rc;
410
411 len = snprintf(reset, sizeof(reset) - 1,
412 "RSIP %u *@mgw MGCP 1.0\n"
413 "RM: restart\n", generate_transaction_id());
414 if (source)
415 rc = sendto(fd, reset, len, 0, (struct sockaddr *) source, sizeof(*source));
416 else
417 rc = write(fd, reset, len);
418
419 if (rc < 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100420 LOGP(DMGCP, LOGL_ERROR, "Failed to send RSIP: %d\n", rc);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100421 }
422
423 return rc;
424}
425
426/*
427 * handle incoming messages:
428 * - this can be a command (four letters, space, transaction id)
429 * - or a response (three numbers, space, transaction id)
430 */
431int mgcp_handle_message(int fd, struct msgb *msg, struct sockaddr_in *source)
432{
433 int code;
434
435 if (msg->len < 4) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100436 LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100437 return -1;
438 }
439
440 /* attempt to treat it as a response */
441 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100442 LOGP(DMGCP, LOGL_NOTICE, "Response: Code: %d\n", code);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100443 } else {
444 int i, handled = 0;
445 msg->l3h = &msg->l2h[4];
446 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
447 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
448 handled = 1;
449 mgcp_requests[i].handle_request(fd, msg, source);
450 }
451 if (!handled) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100452 LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100453 }
454 }
455
456 return 0;
457}
458
459/* string tokenizer for the poor */
460static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
461{
462 int i, found = 0;
463
464 int whitespace = 1;
465 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
466 /* if we have a space we found an end */
467 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
468 if (!whitespace) {
469 ++found;
470 whitespace = 1;
471 ptrs->length = i - ptrs->start - 1;
472 ++ptrs;
473 --ptrs_length;
474 } else {
475 /* skip any number of whitespace */
476 }
477
478 /* line end... stop */
479 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
480 break;
481 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
482 /* line end, be done */
483 break;
484 } else if (whitespace) {
485 whitespace = 0;
486 ptrs->start = i;
487 }
488 }
489
490 if (ptrs_length == 0)
491 return -1;
492 return found;
493}
494
495static struct mgcp_endpoint *find_endpoint(const char *mgcp)
496{
497 char *endptr = NULL;
498 unsigned int gw = INT_MAX;
499
500 gw = strtoul(mgcp, &endptr, 16);
501 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100502 LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100503 return NULL;
504 }
505
506 return &endpoints[gw];
507}
508
509static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
510 const char **transaction_id, struct mgcp_endpoint **endp)
511{
512 int found;
513
514 if (size < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100515 LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100516 return -1;
517 }
518
519 found = find_msg_pointers(msg, ptr, size);
520
521 if (found < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100522 LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100523 return -1;
524 }
525
526 /*
527 * replace the space with \0. the main method gurantess that
528 * we still have + 1 for null termination
529 */
530 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
531 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
532 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
533 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
534
535 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
536 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100537 LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100538 (const char *)&msg->l3h[ptr[3].start],
539 (const char *)&msg->l3h[ptr[2].start]);
540 return -1;
541 }
542
543 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
544 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
545 return *endp == NULL;
546}
547
548static int verify_call_id(const struct mgcp_endpoint *endp,
549 const char *callid)
550{
551 if (strcmp(endp->callid, callid) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100552 LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100553 ENDPOINT_NUMBER(endp), endp->callid, callid);
554 return -1;
555 }
556
557 return 0;
558}
559
560static int verify_ci(const struct mgcp_endpoint *endp,
561 const char *ci)
562{
563 if (atoi(ci) != endp->ci) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100564 LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100565 ENDPOINT_NUMBER(endp), endp->ci, ci);
566 return -1;
567 }
568
569 return 0;
570}
571
572static int handle_audit_endpoint(int fd, struct msgb *msg, struct sockaddr_in *source)
573{
574 struct mgcp_msg_ptr data_ptrs[6];
575 int found, response;
576 const char *trans_id;
577 struct mgcp_endpoint *endp;
578
579 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
580 if (found != 0)
581 response = 500;
582 else
583 response = 200;
584
585 return send_response(fd, response, "AUEP", trans_id, source);
586}
587
588static int parse_conn_mode(const char* msg, int *conn_mode)
589{
590 int ret = 0;
591 if (strcmp(msg, "recvonly") == 0)
592 *conn_mode = MGCP_CONN_RECV_ONLY;
593 else if (strcmp(msg, "sendrecv") == 0)
594 *conn_mode = MGCP_CONN_RECV_SEND;
595 else {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100596 LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100597 ret = -1;
598 }
599
600 return ret;
601}
602
603static int handle_create_con(int fd, struct msgb *msg, struct sockaddr_in *source)
604{
605 struct mgcp_msg_ptr data_ptrs[6];
606 int found, i, line_start;
607 const char *trans_id;
608 struct mgcp_endpoint *endp;
609 int error_code = 500;
610
611 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
612 if (found != 0)
613 return send_response(fd, 500, "CRCX", trans_id, source);
614
615 if (endp->ci != CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100616 LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100617 return send_response(fd, 500, "CRCX", trans_id, source);
618 }
619
620 /* parse CallID C: and LocalParameters L: */
621 MSG_TOKENIZE_START
622 switch (msg->l3h[line_start]) {
623 case 'L':
624 endp->local_options = talloc_strdup(endpoints,
625 (const char *)&msg->l3h[line_start + 3]);
626 break;
627 case 'C':
628 endp->callid = talloc_strdup(endpoints,
629 (const char *)&msg->l3h[line_start + 3]);
630 break;
631 case 'M':
632 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
633 &endp->conn_mode) != 0) {
634 error_code = 517;
635 goto error2;
636 }
637 break;
638 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100639 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100640 msg->l3h[line_start], msg->l3h[line_start],
641 ENDPOINT_NUMBER(endp));
642 break;
643 }
644 MSG_TOKENIZE_END
645
646 /* initialize */
647 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
648
649 /* set to zero until we get the info */
650 memset(&endp->remote, 0, sizeof(endp->remote));
651
652 /* bind to the port now */
653 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
654 if (!early_bind && bind_rtp(endp) != 0)
655 goto error2;
656
657 /* assign a local call identifier or fail */
658 endp->ci = generate_call_id();
659 if (endp->ci == CI_UNUSED)
660 goto error2;
661
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100662 LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100663 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
664 return send_with_sdp(fd, endp, "CRCX", trans_id, source);
665error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100666 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100667 hexdump(msg->l3h, msgb_l3len(msg)),
668 ENDPOINT_NUMBER(endp), line_start, i);
669 return send_response(fd, error_code, "CRCX", trans_id, source);
670
671error2:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100672 LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100673 return send_response(fd, error_code, "CRCX", trans_id, source);
674}
675
676static int handle_modify_con(int fd, struct msgb *msg, struct sockaddr_in *source)
677{
678 struct mgcp_msg_ptr data_ptrs[6];
679 int found, i, line_start;
680 const char *trans_id;
681 struct mgcp_endpoint *endp;
682 int error_code = 500;
683
684 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
685 if (found != 0)
686 return send_response(fd, error_code, "MDCX", trans_id, source);
687
688 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100689 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100690 return send_response(fd, error_code, "MDCX", trans_id, source);
691 }
692
693 MSG_TOKENIZE_START
694 switch (msg->l3h[line_start]) {
695 case 'C': {
696 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
697 goto error3;
698 break;
699 }
700 case 'I': {
701 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
702 goto error3;
703 break;
704 }
705 case 'L':
706 /* skip */
707 break;
708 case 'M':
709 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
710 &endp->conn_mode) != 0) {
711 error_code = 517;
712 goto error3;
713 }
714 break;
715 case '\0':
716 /* SDP file begins */
717 break;
718 case 'a':
719 case 'o':
720 case 's':
721 case 't':
722 case 'v':
723 /* skip these SDP attributes */
724 break;
725 case 'm': {
726 int port;
727 const char *param = (const char *)&msg->l3h[line_start];
728
729 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
730 endp->net_rtp = htons(port);
731 endp->net_rtcp = htons(port + 1);
732 }
733 break;
734 }
735 case 'c': {
736 char ipv4[16];
737 const char *param = (const char *)&msg->l3h[line_start];
738
739 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
740 inet_aton(ipv4, &endp->remote);
741 }
742 break;
743 }
744 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100745 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100746 msg->l3h[line_start], msg->l3h[line_start],
747 ENDPOINT_NUMBER(endp));
748 break;
749 }
750 MSG_TOKENIZE_END
751
752 /* modify */
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100753 LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100754 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->net_rtp);
755 return send_with_sdp(fd, endp, "MDCX", trans_id, source);
756
757error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100758 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100759 hexdump(msg->l3h, msgb_l3len(msg)),
760 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
761 return send_response(fd, error_code, "MDCX", trans_id, source);
762
763error3:
764 return send_response(fd, error_code, "MDCX", trans_id, source);
765}
766
767static int handle_delete_con(int fd, struct msgb *msg, struct sockaddr_in *source)
768{
769 struct mgcp_msg_ptr data_ptrs[6];
770 int found, i, line_start;
771 const char *trans_id;
772 struct mgcp_endpoint *endp;
773 int error_code = 500;
774
775 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
776 if (found != 0)
777 return send_response(fd, error_code, "DLCX", trans_id, source);
778
779 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100780 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100781 return send_response(fd, error_code, "DLCX", trans_id, source);
782 }
783
784 MSG_TOKENIZE_START
785 switch (msg->l3h[line_start]) {
786 case 'C': {
787 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
788 goto error3;
789 break;
790 }
791 case 'I': {
792 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
793 goto error3;
794 break;
795 }
796 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100797 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100798 msg->l3h[line_start], msg->l3h[line_start],
799 ENDPOINT_NUMBER(endp));
800 break;
801 }
802 MSG_TOKENIZE_END
803
804
805 /* free the connection */
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100806 LOGP(DMGCP, LOGL_NOTICE, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100807 endp->ci= CI_UNUSED;
808 talloc_free(endp->callid);
809 talloc_free(endp->local_options);
810
811 if (!early_bind) {
812 bsc_unregister_fd(&endp->local_rtp);
813 bsc_unregister_fd(&endp->local_rtcp);
814 }
815
816 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
817
818 return send_response(fd, 250, "DLCX", trans_id, source);
819
820error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100821 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100822 hexdump(msg->l3h, msgb_l3len(msg)),
823 ENDPOINT_NUMBER(endp), line_start, i);
824 return send_response(fd, error_code, "DLCX", trans_id, source);
825
826error3:
827 return send_response(fd, error_code, "DLCX", trans_id, source);
828}
829
830/*
831 * vty code for mgcp below
832 */
833struct cmd_node mgcp_node = {
834 MGCP_NODE,
835 "%s(mgcp)#",
836 1,
837};
838
839static int config_write_mgcp(struct vty *vty)
840{
841 vty_out(vty, "mgcp%s", VTY_NEWLINE);
842 if (local_ip)
843 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
844 if (bts_ip)
845 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
846 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
847 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
848 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
849 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
850 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
851 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
852 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
853 vty_out(vty, " endpoints %u%s", number_endpoints, VTY_NEWLINE);
854 if (forward_ip)
855 vty_out(vty, " forward audio ip %s%s", forward_ip, VTY_NEWLINE);
856 if (forward_port != 0)
857 vty_out(vty, " forward audio port %d%s", forward_port, VTY_NEWLINE);
858
859 return CMD_SUCCESS;
860}
861
862DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
863 SHOW_STR "Display information about the MGCP Media Gateway")
864{
865 int i;
866
867 vty_out(vty, "MGCP is up and running with %u endpoints:%s", number_endpoints - 1, VTY_NEWLINE);
868 for (i = 1; i < number_endpoints; ++i) {
869 struct mgcp_endpoint *endp = &endpoints[i];
870 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
871 i, endp->ci,
872 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
873 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
874 }
875
876 return CMD_SUCCESS;
877}
878
879DEFUN(cfg_mgcp,
880 cfg_mgcp_cmd,
881 "mgcp",
882 "Configure the MGCP")
883{
884 vty->node = MGCP_NODE;
885 return CMD_SUCCESS;
886}
887
888DEFUN(cfg_mgcp_local_ip,
889 cfg_mgcp_local_ip_cmd,
890 "local ip IP",
891 "Set the IP to be used in SDP records")
892{
893 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
894 return CMD_SUCCESS;
895}
896
897DEFUN(cfg_mgcp_bts_ip,
898 cfg_mgcp_bts_ip_cmd,
899 "bts ip IP",
900 "Set the IP of the BTS for RTP forwarding")
901{
902 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
903 inet_aton(bts_ip, &bts_in);
904 return CMD_SUCCESS;
905}
906
907DEFUN(cfg_mgcp_bind_ip,
908 cfg_mgcp_bind_ip_cmd,
909 "bind ip IP",
910 "Bind the MGCP to this local addr")
911{
912 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
913 return CMD_SUCCESS;
914}
915
916DEFUN(cfg_mgcp_bind_port,
917 cfg_mgcp_bind_port_cmd,
918 "bind port <0-65534>",
919 "Bind the MGCP to this port")
920{
921 unsigned int port = atoi(argv[0]);
922 if (port > 65534) {
923 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
924 return CMD_WARNING;
925 }
926
927 source_port = port;
928 return CMD_SUCCESS;
929}
930
931DEFUN(cfg_mgcp_bind_early,
932 cfg_mgcp_bind_early_cmd,
933 "bind early (0|1)",
934 "Bind all RTP ports early")
935{
936 unsigned int bind = atoi(argv[0]);
937 if (bind != 0 && bind != 1) {
938 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
939 return CMD_WARNING;
940 }
941
942 early_bind = bind == 1;
943 return CMD_SUCCESS;
944}
945
946DEFUN(cfg_mgcp_rtp_base_port,
947 cfg_mgcp_rtp_base_port_cmd,
948 "rtp base <0-65534>",
949 "Base port to use")
950{
951 unsigned int port = atoi(argv[0]);
952 if (port > 65534) {
953 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
954 return CMD_WARNING;
955 }
956
957 rtp_base_port = port;
958 return CMD_SUCCESS;
959}
960
961DEFUN(cfg_mgcp_sdp_payload_number,
962 cfg_mgcp_sdp_payload_number_cmd,
963 "sdp audio payload number <1-255>",
964 "Set the audio codec to use")
965{
966 unsigned int payload = atoi(argv[0]);
967 if (payload > 255) {
968 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
969 return CMD_WARNING;
970 }
971
972 audio_payload = payload;
973 return CMD_SUCCESS;
974}
975
976DEFUN(cfg_mgcp_sdp_payload_name,
977 cfg_mgcp_sdp_payload_name_cmd,
978 "sdp audio payload name NAME",
979 "Set the audio name to use")
980{
981 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
982 return CMD_SUCCESS;
983}
984
985DEFUN(cfg_mgcp_loop,
986 cfg_mgcp_loop_cmd,
987 "loop (0|1)",
988 "Loop the audio")
989{
990 audio_loop = atoi(argv[0]);
991 return CMD_SUCCESS;
992}
993
994DEFUN(cfg_mgcp_number_endp,
995 cfg_mgcp_number_endp_cmd,
996 "number endpoints <0-65534>",
997 "The number of endpoints to allocate. This is not dynamic.")
998{
999 /* + 1 as we start counting at one */
1000 number_endpoints = atoi(argv[0]) + 1;
1001 return CMD_SUCCESS;
1002}
1003
1004DEFUN(cfg_mgcp_forward_ip,
1005 cfg_mgcp_forward_ip_cmd,
1006 "forward audio ip IP",
1007 "Forward packets from and to the IP. This disables most of the MGCP feature.")
1008{
1009 if (forward_ip)
1010 talloc_free(forward_ip);
1011 forward_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
1012 return CMD_SUCCESS;
1013}
1014
1015DEFUN(cfg_mgcp_forward_port,
1016 cfg_mgcp_forward_port_cmd,
1017 "forward audio port <1-15000>",
1018 "Forward packets from and to the port. This disables most of the MGCP feature.")
1019{
1020 forward_port = atoi(argv[0]);
1021 return CMD_SUCCESS;
1022}
1023
1024int mgcp_vty_init(void)
1025{
1026 install_element(VIEW_NODE, &show_mgcp_cmd);
1027
1028 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1029 install_node(&mgcp_node, config_write_mgcp);
1030 install_default(MGCP_NODE);
1031 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1032 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1033 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1034 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1035 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1036 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1037 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1038 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
1039 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
1040 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
1041 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
1042 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
1043 return 0;
1044}
1045
1046int mgcp_parse_config(const char *config_file, struct gsm_network *dummy_network)
1047{
1048 int i, rc;
1049
1050 rc = vty_read_config_file(config_file);
1051 if (rc < 0) {
1052 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1053 return rc;
1054 }
1055
1056
1057 if (!bts_ip)
1058 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
1059
1060 endpoints = _talloc_zero_array(tall_bsc_ctx,
1061 sizeof(struct mgcp_endpoint),
1062 number_endpoints, "endpoints");
1063 if (!endpoints) {
1064 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1065 return -1;
1066 }
1067
1068 /* Initialize all endpoints */
1069 for (i = 0; i < number_endpoints; ++i) {
1070 endpoints[i].local_rtp.fd = -1;
1071 endpoints[i].local_rtcp.fd = -1;
1072 endpoints[i].ci = CI_UNUSED;
1073 }
1074
1075 /*
1076 * This application supports two modes.
1077 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
1078 * 2.) plain forwarding of RTP packets on the endpoints.
1079 * both modes are mutual exclusive
1080 */
1081 if (forward_ip) {
1082 int port = rtp_base_port;
1083 if (forward_port != 0)
1084 port = forward_port;
1085
1086 if (!early_bind) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001087 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001088 return -1;
1089 }
1090
1091 /*
1092 * Store the forward IP and assign a ci. For early bind
1093 * the sockets will be created after this.
1094 */
1095 for (i = 1; i < number_endpoints; ++i) {
1096 struct mgcp_endpoint *endp = &endpoints[i];
1097 inet_aton(forward_ip, &endp->remote);
1098 endp->ci = CI_UNUSED + 23;
1099 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
1100 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
1101 }
1102
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001103 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001104 }
1105
1106 /* early bind */
1107 if (early_bind) {
1108 for (i = 1; i < number_endpoints; ++i) {
1109 struct mgcp_endpoint *endp = &endpoints[i];
1110 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001111 if (bind_rtp(endp) != 0) {
1112 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", endp->rtp_port);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001113 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001114 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001115 }
1116 }
1117
1118 return !!forward_ip;
1119}