blob: 0899cec8eef4cc451e5297d78f261e61401e3269 [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
151 bsc_endp->transaction_id = talloc_strdup(bsc_endp, transaction_id);
152 bsc_endp->bsc = bsc_con;
153
154 /* we need to update some bits */
155 if (state == MGCP_ENDP_CRCX) {
156 struct sockaddr_in sock;
157 socklen_t len = sizeof(sock);
158 if (getpeername(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
159 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...\n");
160 } else {
161 mgcp_endp->bts = sock.sin_addr;
162 }
163 }
164
165 /* we need to generate a new and patched message */
166 bsc_msg = bsc_mgcp_rewrite(nat->mgcp_msg, nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port);
167 if (!bsc_msg) {
168 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
169 return MGCP_POLICY_CONT;
170 }
171
172 bsc_write_mgcp_msg(bsc_con, bsc_msg);
173 return MGCP_POLICY_DEFER;
174}
175
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200176/*
177 * We have received a msg from the BSC. We will see if we know
178 * this transaction and if it belongs to the BSC. Then we will
179 * need to patch the content to point to the local network and we
180 * need to update the I: that was assigned by the BSS.
181 */
182void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
183{
184 struct msgb *output;
185 struct bsc_endpoint *bsc_endp = NULL;
186 struct mgcp_endpoint *endp = NULL;
187 int i, code;
188 char transaction_id[60];
189
190 /* Some assumption that our buffer is big enough.. and null terminate */
191 if (msgb_l2len(msg) > 2000) {
192 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
193 return;
194 }
195
196 msg->l2h[msgb_l2len(msg)] = '\0';
197
198 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
199 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
200 return;
201 }
202
203 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
204 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
205 continue;
206 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
207 continue;
208
209 endp = &bsc->nat->mgcp_cfg->endpoints[i];
210 bsc_endp = &bsc->nat->bsc_endpoints[i];
211 break;
212 }
213
214 if (!bsc_endp) {
215 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s\n", transaction_id);
216 return;
217 }
218
219 /* free some stuff */
220 talloc_free(bsc_endp->transaction_id);
221 bsc_endp->transaction_id = NULL;
222
223 /* make it point to our endpoint */
224 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
225 output = bsc_mgcp_rewrite(msg, bsc->nat->mgcp_cfg->source_addr, endp->rtp_port);
226 if (!output) {
227 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
228 return;
229 }
230
231 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
232 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
233 msgb_free(output);
234 }
235}
236
237int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
238{
239 /* we want to parse two strings */
240 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
241}
242
243int bsc_mgcp_extract_ci(const char *str)
244{
245 int ci;
246 char *res = strstr(str, "I: ");
247 if (!res)
248 return CI_UNUSED;
249
250 if (sscanf(res, "I: %d\r\n", &ci) != 1)
251 return CI_UNUSED;
252 return ci;
253}
254
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200255/* we need to replace some strings... */
256struct msgb *bsc_mgcp_rewrite(struct msgb *input, const char *ip, int port)
257{
258 static const char *ip_str = "c=IN IP4 ";
259 static const char *aud_str = "m=audio ";
260
261 char buf[128];
262 char *running, *token;
263 struct msgb *output;
264
265 if (msgb_l2len(input) > 4096 - 128) {
266 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
267 return NULL;
268 }
269
270 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
271 if (!output) {
272 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
273 return NULL;
274 }
275
276 running = (char *) input->l2h;
277 output->l2h = output->data;
278 for (token = strsep(&running, "\n"); token; token = strsep(&running, "\n")) {
279 int len = strlen(token);
280
281 /* ignore completely empty lines for now */
282 if (len == 0)
283 continue;
284
285 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
286 output->l3h = msgb_put(output, strlen(ip_str));
287 memcpy(output->l3h, ip_str, strlen(ip_str));
288 output->l3h = msgb_put(output, strlen(ip));
289 memcpy(output->l3h, ip, strlen(ip));
290 output->l3h = msgb_put(output, 2);
291 output->l3h[0] = '\r';
292 output->l3h[1] = '\n';
293 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
294 int payload;
295 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
296 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
297 msgb_free(output);
298 return NULL;
299 }
300
301 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d\r\n", port, payload);
302 buf[sizeof(buf)-1] = '\0';
303
304 output->l3h = msgb_put(output, strlen(buf));
305 memcpy(output->l3h, buf, strlen(buf));
306 } else {
307 output->l3h = msgb_put(output, len + 1);
308 memcpy(output->l3h, token, len);
309 output->l3h[len] = '\n';
310 }
311 }
312
313 return output;
314}
315
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200316static int mgcp_do_read(struct bsc_fd *fd)
317{
318 struct bsc_nat *nat;
319 struct msgb *msg, *resp;
320 int rc;
321
322 msg = msgb_alloc(4096, "MGCP GW Read");
323 if (!msg) {
324 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
325 return -1;
326 }
327
328
329 rc = read(fd->fd, msg->data, msg->data_len);
330 if (rc <= 0) {
331 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
332 msgb_free(msg);
333 return -1;
334 }
335
336 nat = fd->data;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200337 nat->mgcp_msg = msg;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200338 msg->l2h = msgb_put(msg, rc);
339 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
340 msgb_free(msg);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200341 nat->mgcp_msg = NULL;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200342
343 /* we do have a direct answer... e.g. AUEP */
344 if (resp) {
345 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
346 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
347 msgb_free(resp);
348 }
349 }
350
351 return 0;
352}
353
354static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
355{
356 int rc;
357
358 rc = write(bfd->fd, msg->data, msg->len);
359
360 if (rc != msg->len) {
361 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
362 return -1;
363 }
364
365 return rc;
366}
367
368int bsc_mgcp_init(struct bsc_nat *nat)
369{
370 int on;
371 struct sockaddr_in addr;
372
373 if (!nat->mgcp_cfg->call_agent_addr) {
374 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
375 return -1;
376 }
377
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200378 if (nat->mgcp_cfg->bts_ip) {
379 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
380 return -1;
381 }
382
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200383 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
384 if (nat->mgcp_queue.bfd.fd < 0) {
385 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
386 return -1;
387 }
388
389 on = 1;
390 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
391
392 addr.sin_family = AF_INET;
393 addr.sin_port = htons(nat->mgcp_cfg->source_port);
394 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
395
396 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
397 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
398 close(nat->mgcp_queue.bfd.fd);
399 nat->mgcp_queue.bfd.fd = -1;
400 return -1;
401 }
402
403 addr.sin_port = htons(2727);
404 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
405 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
406 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
407 nat->mgcp_cfg->call_agent_addr, errno);
408 close(nat->mgcp_queue.bfd.fd);
409 nat->mgcp_queue.bfd.fd = -1;
410 return -1;
411 }
412
413 write_queue_init(&nat->mgcp_queue, 10);
414 nat->mgcp_queue.bfd.when = BSC_FD_READ;
415 nat->mgcp_queue.bfd.data = nat;
416 nat->mgcp_queue.read_cb = mgcp_do_read;
417 nat->mgcp_queue.write_cb = mgcp_do_write;
418
419 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
420 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
421 close(nat->mgcp_queue.bfd.fd);
422 nat->mgcp_queue.bfd.fd = -1;
423 return -1;
424 }
425
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200426 /* some more MGCP config handling */
427 nat->mgcp_cfg->data = nat;
428 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
429 nat->bsc_endpoints = talloc_zero_array(nat,
430 struct bsc_endpoint,
431 nat->mgcp_cfg->number_endpoints + 1);
432
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200433 return 0;
434}