blob: 6fc5eaca2701b46ebbb86a988d121fba3409ac4d [file] [log] [blame]
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <openbsc/bsc_nat.h>
23#include <openbsc/gsm_data.h>
24#include <openbsc/bssap.h>
25#include <openbsc/debug.h>
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020026#include <openbsc/mgcp.h>
27#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080028
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020029#include <osmocore/talloc.h>
30
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080031#include <netinet/in.h>
32#include <arpa/inet.h>
33
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +020034#include <errno.h>
35#include <unistd.h>
36
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080037int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
38{
39 struct tlv_parsed tp;
40 u_int16_t cic;
41 u_int8_t timeslot;
42 u_int8_t multiplex;
43
44 if (!msg->l3h) {
45 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
46 return -1;
47 }
48
49 if (msgb_l3len(msg) < 3) {
50 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
51 return -1;
52 }
53
54 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
55 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
56 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
57 return -1;
58 }
59
60 cic = ntohs(*(u_int16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
61 timeslot = cic & 0x1f;
62 multiplex = (cic & ~0x1f) >> 5;
63
64 con->msc_timeslot = (32 * multiplex) + timeslot;
65 con->bsc_timeslot = con->msc_timeslot;
66 return 0;
67}
68
69void bsc_mgcp_clear(struct sccp_connections *con)
70{
71 con->msc_timeslot = -1;
72 con->bsc_timeslot = -1;
73}
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020074
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020075void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
76{
77 if (nat->bsc_endpoints[i].transaction_id) {
78 talloc_free(nat->bsc_endpoints[i].transaction_id);
79 nat->bsc_endpoints[i].transaction_id = NULL;
80 }
81
82 nat->bsc_endpoints[i].bsc = NULL;
83 mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
84}
85
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020086void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
87{
88 int i;
89
90 for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020091 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020092}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +020093
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +020094struct bsc_connection *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
95{
96 struct sccp_connections *sccp;
97
98 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
99 if (sccp->msc_timeslot == -1)
100 continue;
101 if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
102 continue;
103
104 return sccp->bsc;
105 }
106
107 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
108 return NULL;
109}
110
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200111int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id)
112{
113 struct bsc_nat *nat;
114 struct bsc_endpoint *bsc_endp;
115 struct bsc_connection *bsc_con;
116 struct mgcp_endpoint *mgcp_endp;
117 struct msgb *bsc_msg;
118
119 nat = cfg->data;
120 bsc_endp = &nat->bsc_endpoints[endpoint];
121 mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint];
122
123 bsc_con = bsc_mgcp_find_con(nat, endpoint);
124
125 if (!bsc_con) {
126 LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for a new connection on 0x%x for %d\n", endpoint, state);
127
128 switch (state) {
129 case MGCP_ENDP_CRCX:
130 return MGCP_POLICY_REJECT;
131 break;
132 case MGCP_ENDP_DLCX:
133 return MGCP_POLICY_CONT;
134 break;
135 case MGCP_ENDP_MDCX:
136 return MGCP_POLICY_CONT;
137 break;
138 default:
139 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
140 return MGCP_POLICY_CONT;
141 break;
142 }
143 }
144
145 if (bsc_endp->transaction_id) {
146 LOGP(DMGCP, LOGL_ERROR, "One transaction with id '%s' on 0x%x\n",
147 bsc_endp->transaction_id, endpoint);
148 talloc_free(bsc_endp->transaction_id);
149 }
150
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200151 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200152 bsc_endp->bsc = bsc_con;
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200153 bsc_endp->pending_delete = state == MGCP_ENDP_DLCX;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200154
155 /* we need to update some bits */
156 if (state == MGCP_ENDP_CRCX) {
157 struct sockaddr_in sock;
158 socklen_t len = sizeof(sock);
159 if (getpeername(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
160 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...\n");
161 } else {
162 mgcp_endp->bts = sock.sin_addr;
163 }
164 }
165
166 /* we need to generate a new and patched message */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200167 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
168 nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200169 if (!bsc_msg) {
170 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
171 return MGCP_POLICY_CONT;
172 }
173
174 bsc_write_mgcp_msg(bsc_con, bsc_msg);
175 return MGCP_POLICY_DEFER;
176}
177
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200178/*
179 * We have received a msg from the BSC. We will see if we know
180 * this transaction and if it belongs to the BSC. Then we will
181 * need to patch the content to point to the local network and we
182 * need to update the I: that was assigned by the BSS.
183 */
184void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
185{
186 struct msgb *output;
187 struct bsc_endpoint *bsc_endp = NULL;
188 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherf05506e2010-04-05 09:10:30 +0200189 int i, code, port;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200190 char transaction_id[60];
191
192 /* Some assumption that our buffer is big enough.. and null terminate */
193 if (msgb_l2len(msg) > 2000) {
194 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
195 return;
196 }
197
198 msg->l2h[msgb_l2len(msg)] = '\0';
199
200 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
201 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
202 return;
203 }
204
205 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
206 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
207 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200208 /* no one listening? a bug? */
209 if (!bsc->nat->bsc_endpoints[i].transaction_id)
210 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200211 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
212 continue;
213
214 endp = &bsc->nat->mgcp_cfg->endpoints[i];
215 bsc_endp = &bsc->nat->bsc_endpoints[i];
216 break;
217 }
218
219 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200220 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
221 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200222 return;
223 }
224
225 /* free some stuff */
226 talloc_free(bsc_endp->transaction_id);
227 bsc_endp->transaction_id = NULL;
228
229 /* make it point to our endpoint */
230 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherf05506e2010-04-05 09:10:30 +0200231 port = bsc_mgcp_extract_port((const char *) msg->l2h);
232 endp->bts_rtp = htons(port);
233 endp->bts_rtcp = htons(port + 1);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200234 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
235 bsc->nat->mgcp_cfg->source_addr, endp->rtp_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200236
237 if (bsc_endp->pending_delete) {
238 mgcp_free_endp(endp);
239 bsc_endp->bsc = NULL;
240 bsc_endp->pending_delete = 0;
241 }
242
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200243 if (!output) {
244 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
245 return;
246 }
247
248 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
249 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
250 msgb_free(output);
251 }
252}
253
254int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
255{
256 /* we want to parse two strings */
257 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
258}
259
260int bsc_mgcp_extract_ci(const char *str)
261{
262 int ci;
263 char *res = strstr(str, "I: ");
264 if (!res)
265 return CI_UNUSED;
266
Holger Hans Peter Freyther806aca92010-04-05 09:07:39 +0200267 if (sscanf(res, "I: %d", &ci) != 1)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200268 return CI_UNUSED;
269 return ci;
270}
271
Holger Hans Peter Freytherf05506e2010-04-05 09:10:30 +0200272int bsc_mgcp_extract_port(const char *str)
273{
274 int port;
275 char *res = strstr(str, "m=audio ");
276 if (!res)
277 return 0;
278
279 if (sscanf(res, "m=audio %d RTP/AVP %*d", &port) != 1)
280 return 0;
281 return port;
282}
283
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200284/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200285struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200286{
287 static const char *ip_str = "c=IN IP4 ";
288 static const char *aud_str = "m=audio ";
289
290 char buf[128];
291 char *running, *token;
292 struct msgb *output;
293
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200294 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200295 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
296 return NULL;
297 }
298
299 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
300 if (!output) {
301 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
302 return NULL;
303 }
304
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200305 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200306 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200307 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200308 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200309 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200310
311 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
312 output->l3h = msgb_put(output, strlen(ip_str));
313 memcpy(output->l3h, ip_str, strlen(ip_str));
314 output->l3h = msgb_put(output, strlen(ip));
315 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200316
317 if (cr) {
318 output->l3h = msgb_put(output, 2);
319 output->l3h[0] = '\r';
320 output->l3h[1] = '\n';
321 } else {
322 output->l3h = msgb_put(output, 1);
323 output->l3h[0] = '\n';
324 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200325 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
326 int payload;
327 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
328 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
329 msgb_free(output);
330 return NULL;
331 }
332
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200333 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
334 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200335 buf[sizeof(buf)-1] = '\0';
336
337 output->l3h = msgb_put(output, strlen(buf));
338 memcpy(output->l3h, buf, strlen(buf));
339 } else {
340 output->l3h = msgb_put(output, len + 1);
341 memcpy(output->l3h, token, len);
342 output->l3h[len] = '\n';
343 }
344 }
345
346 return output;
347}
348
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200349static int mgcp_do_read(struct bsc_fd *fd)
350{
351 struct bsc_nat *nat;
352 struct msgb *msg, *resp;
353 int rc;
354
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200355 nat = fd->data;
356
357 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
358 if (rc <= 0) {
359 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
360 return -1;
361 }
362
363 nat->mgcp_msg[rc] = '\0';
364 nat->mgcp_length = rc;
365
366 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200367 if (!msg) {
368 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
369 return -1;
370 }
371
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200372 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200373 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200374 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
375 msgb_free(msg);
376
377 /* we do have a direct answer... e.g. AUEP */
378 if (resp) {
379 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
380 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
381 msgb_free(resp);
382 }
383 }
384
385 return 0;
386}
387
388static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
389{
390 int rc;
391
392 rc = write(bfd->fd, msg->data, msg->len);
393
394 if (rc != msg->len) {
395 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
396 return -1;
397 }
398
399 return rc;
400}
401
402int bsc_mgcp_init(struct bsc_nat *nat)
403{
404 int on;
405 struct sockaddr_in addr;
406
407 if (!nat->mgcp_cfg->call_agent_addr) {
408 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
409 return -1;
410 }
411
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200412 if (nat->mgcp_cfg->bts_ip) {
413 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
414 return -1;
415 }
416
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200417 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
418 if (nat->mgcp_queue.bfd.fd < 0) {
419 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
420 return -1;
421 }
422
423 on = 1;
424 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
425
426 addr.sin_family = AF_INET;
427 addr.sin_port = htons(nat->mgcp_cfg->source_port);
428 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
429
430 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
431 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
432 close(nat->mgcp_queue.bfd.fd);
433 nat->mgcp_queue.bfd.fd = -1;
434 return -1;
435 }
436
437 addr.sin_port = htons(2727);
438 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
439 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
440 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
441 nat->mgcp_cfg->call_agent_addr, errno);
442 close(nat->mgcp_queue.bfd.fd);
443 nat->mgcp_queue.bfd.fd = -1;
444 return -1;
445 }
446
447 write_queue_init(&nat->mgcp_queue, 10);
448 nat->mgcp_queue.bfd.when = BSC_FD_READ;
449 nat->mgcp_queue.bfd.data = nat;
450 nat->mgcp_queue.read_cb = mgcp_do_read;
451 nat->mgcp_queue.write_cb = mgcp_do_write;
452
453 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
454 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
455 close(nat->mgcp_queue.bfd.fd);
456 nat->mgcp_queue.bfd.fd = -1;
457 return -1;
458 }
459
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200460 /* some more MGCP config handling */
461 nat->mgcp_cfg->data = nat;
462 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
463 nat->bsc_endpoints = talloc_zero_array(nat,
464 struct bsc_endpoint,
465 nat->mgcp_cfg->number_endpoints + 1);
466
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200467 return 0;
468}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200469
470void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
471{
472 int i;
473 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
474 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
475
476 if (bsc_endp->bsc != bsc)
477 continue;
478
479 bsc_endp->bsc = NULL;
480 bsc_endp->pending_delete = 0;
481 if (bsc_endp->transaction_id)
482 talloc_free(bsc_endp->transaction_id);
483 bsc_endp->transaction_id = NULL;
484 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
485 }
486}