blob: 3bc602e79fba2c185e9201a1735a6f5aeb5d3f46 [file] [log] [blame]
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +01001/*
2 * Message rewriting functionality
3 */
4/*
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +02005 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010-2013 by On-Waves
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +01007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <openbsc/bsc_nat.h>
25#include <openbsc/bsc_nat_sccp.h>
26#include <openbsc/bsc_msc.h>
27#include <openbsc/gsm_data.h>
28#include <openbsc/debug.h>
29#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020030#include <openbsc/nat_rewrite_trie.h>
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010031
32#include <osmocom/core/linuxlist.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/gsm/gsm0808.h>
35
36#include <osmocom/gsm/protocol/gsm_08_08.h>
37#include <osmocom/gsm/protocol/gsm_04_11.h>
38
39#include <osmocom/sccp/sccp.h>
40
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020041static char *trie_lookup(struct nat_rewrite *trie, const char *number,
42 regoff_t off, void *ctx)
43{
44 struct nat_rewrite_rule *rule;
45
46 if (!trie) {
47 LOGP(DNAT, LOGL_ERROR,
48 "Asked to do a table lookup but no table.\n");
49 return NULL;
50 }
51
52 rule = nat_rewrite_lookup(trie, number);
53 if (!rule) {
54 LOGP(DNAT, LOGL_DEBUG,
55 "Couldn't find a prefix rule for %s\n", number);
56 return NULL;
57 }
58
59 return talloc_asprintf(ctx, "%s%s", rule->rewrite, &number[off]);
60}
61
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010062static char *match_and_rewrite_number(void *ctx, const char *number,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020063 const char *imsi, struct llist_head *list,
64 struct nat_rewrite *trie)
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010065{
66 struct bsc_nat_num_rewr_entry *entry;
67 char *new_number = NULL;
68
69 /* need to find a replacement and then fix it */
70 llist_for_each_entry(entry, list, list) {
71 regmatch_t matches[2];
72
73 /* check the IMSI match */
74 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
75 continue;
76
77 /* this regexp matches... */
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020078 if (regexec(&entry->num_reg, number, 2, matches, 0) == 0
79 && matches[1].rm_eo != -1) {
80 if (entry->is_prefix_lookup)
81 new_number = trie_lookup(trie, number,
82 matches[1].rm_so, ctx);
83 else
84 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010085 entry->replace,
86 &number[matches[1].rm_so]);
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020087 }
88
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010089 if (new_number)
90 break;
91 }
92
93 return new_number;
94}
95
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +020096static char *rewrite_isdn_number(struct bsc_nat *nat, struct llist_head *rewr_list,
97 void *ctx, const char *imsi,
98 struct gsm_mncc_number *called)
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010099{
Holger Hans Peter Freyther73460812013-07-05 07:50:30 +0200100 char int_number[sizeof(called->number) + 2];
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200101 char *number = called->number;
102
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100103 if (llist_empty(&nat->num_rewr))
104 return NULL;
105
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200106 /* only ISDN plan */
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100107 if (called->plan != 1)
108 return NULL;
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100109
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200110 /* international, prepend */
111 if (called->type == 1) {
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200112 int_number[0] = '+';
113 memcpy(&int_number[1], number, strlen(number) + 1);
114 number = int_number;
115 }
116
117 return match_and_rewrite_number(ctx, number,
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200118 imsi, rewr_list, nat->num_rewr_trie);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100119}
120
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200121static void update_called_number(struct gsm_mncc_number *called,
122 const char *chosen_number)
123{
124 if (strncmp(chosen_number, "00", 2) == 0) {
125 called->type = 1;
126 strncpy(called->number, chosen_number + 2, sizeof(called->number));
127 } else {
128 /* rewrite international to unknown */
129 if (called->type == 1)
130 called->type = 0;
131 strncpy(called->number, chosen_number, sizeof(called->number));
132 }
133}
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100134
135/**
136 * Rewrite non global numbers... according to rules based on the IMSI
137 */
138static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
139 struct bsc_nat_parsed *parsed, const char *imsi,
140 struct gsm48_hdr *hdr48, const uint32_t len)
141{
142 struct tlv_parsed tp;
143 unsigned int payload_len;
144 struct gsm_mncc_number called;
145 struct msgb *out;
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200146 char *new_number_pre = NULL, *new_number_post = NULL, *chosen_number;
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100147 uint8_t *outptr;
148 const uint8_t *msgptr;
149 int sec_len;
150
151 /* decode and rewrite the message */
152 payload_len = len - sizeof(*hdr48);
153 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
154
155 /* no number, well let us ignore it */
156 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
157 return NULL;
158
159 memset(&called, 0, sizeof(called));
160 gsm48_decode_called(&called,
161 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
162
163 /* check if it looks international and stop */
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200164 new_number_pre = rewrite_isdn_number(nat, &nat->num_rewr, msg, imsi, &called);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100165
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200166 if (!new_number_pre) {
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100167 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
168 return NULL;
169 }
170
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200171 if (strlen(new_number_pre) > sizeof(called.number)) {
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100172 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200173 talloc_free(new_number_pre);
174 return NULL;
175 }
176 update_called_number(&called, new_number_pre);
177
178 /* another run through the re-write engine with other rules */
179 new_number_post = rewrite_isdn_number(nat, &nat->num_rewr_post, msg,
180 imsi, &called);
181 chosen_number = new_number_post ? new_number_post : new_number_pre;
182
183
184 if (strlen(chosen_number) > sizeof(called.number)) {
185 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
186 talloc_free(new_number_pre);
187 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100188 return NULL;
189 }
190
191 /*
192 * Need to create a new message now based on the old onew
193 * with a new number. We can sadly not patch this in place
194 * so we will need to regenerate it.
195 */
196
197 out = msgb_alloc_headroom(4096, 128, "changed-setup");
198 if (!out) {
199 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200200 talloc_free(new_number_pre);
201 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100202 return NULL;
203 }
204
205 /* copy the header */
206 outptr = msgb_put(out, sizeof(*hdr48));
207 memcpy(outptr, hdr48, sizeof(*hdr48));
208
209 /* copy everything up to the number */
210 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
211 outptr = msgb_put(out, sec_len);
212 memcpy(outptr, &hdr48->data[0], sec_len);
213
214 /* create the new number */
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200215 update_called_number(&called, chosen_number);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100216 gsm48_encode_called(out, &called);
217
218 /* copy thre rest */
219 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
220 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
221 sec_len = payload_len - (msgptr - &hdr48->data[0]);
222 outptr = msgb_put(out, sec_len);
223 memcpy(outptr, msgptr, sec_len);
224
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200225 talloc_free(new_number_pre);
226 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100227 return out;
228}
229
230/**
231 * Find a new SMSC address, returns an allocated string that needs to be
232 * freed or is NULL.
233 */
234static char *find_new_smsc(struct bsc_nat *nat, void *ctx, const char *imsi,
235 const char *smsc_addr, const char *dest_nr)
236{
237 struct bsc_nat_num_rewr_entry *entry;
238 char *new_number = NULL;
239 uint8_t dest_match = llist_empty(&nat->tpdest_match);
240
241 /* We will find a new number now */
242 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
243 regmatch_t matches[2];
244
245 /* check the IMSI match */
246 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
247 continue;
248
249 /* this regexp matches... */
250 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
251 matches[1].rm_eo != -1)
252 new_number = talloc_asprintf(ctx, "%s%s",
253 entry->replace,
254 &smsc_addr[matches[1].rm_so]);
255 if (new_number)
256 break;
257 }
258
259 if (!new_number)
260 return NULL;
261
262 /*
263 * now match the number against another list
264 */
265 llist_for_each_entry(entry, &nat->tpdest_match, list) {
266 /* check the IMSI match */
267 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
268 continue;
269
270 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
271 dest_match = 1;
272 break;
273 }
274 }
275
276 if (!dest_match) {
277 talloc_free(new_number);
278 return NULL;
279 }
280
281 return new_number;
282}
283
284/**
285 * Clear the TP-SRR from the TPDU header
286 */
287static uint8_t sms_new_tpdu_hdr(struct bsc_nat *nat, const char *imsi,
288 const char *dest_nr, uint8_t hdr)
289{
290 struct bsc_nat_num_rewr_entry *entry;
291
292 /* We will find a new number now */
293 llist_for_each_entry(entry, &nat->sms_clear_tp_srr, list) {
294 /* check the IMSI match */
295 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
296 continue;
297 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) != 0)
298 continue;
299
300 /* matched phone number and imsi */
301 return hdr & ~0x20;
302 }
303
304 return hdr;
305}
306
307/**
308 * Check if we need to rewrite the number. For this SMS.
309 */
310static char *sms_new_dest_nr(struct bsc_nat *nat, void *ctx,
311 const char *imsi, const char *dest_nr)
312{
313 return match_and_rewrite_number(ctx, dest_nr, imsi,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200314 &nat->sms_num_rewr, NULL);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100315}
316
317/**
318 * This is a helper for GSM 04.11 8.2.5.2 Destination address element
319 */
320void sms_encode_addr_element(struct msgb *out, const char *new_number,
321 int format, int tp_data)
322{
323 uint8_t new_addr_len;
324 uint8_t new_addr[26];
325
326 /*
327 * Copy the new number. We let libosmocore encode it, then set
328 * the extension followed after the length. Depending on if
329 * we want to write RP we will let the TLV code add the
330 * length for us or we need to use strlen... This is not very clear
331 * as of 03.40 and 04.11.
332 */
333 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
334 1, new_number);
335 new_addr[1] = format;
336 if (tp_data) {
337 uint8_t *data = msgb_put(out, new_addr_len);
338 memcpy(data, new_addr, new_addr_len);
339 data[0] = strlen(new_number);
340 } else {
341 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
342 }
343}
344
345static struct msgb *sms_create_new(uint8_t type, uint8_t ref,
346 struct gsm48_hdr *old_hdr48,
347 const uint8_t *orig_addr_ptr,
348 int orig_addr_len, const char *new_number,
349 const uint8_t *data_ptr, int data_len,
350 uint8_t tpdu_first_byte,
351 const int old_dest_len, const char *new_dest_nr)
352{
353 struct gsm48_hdr *new_hdr48;
354 struct msgb *out;
355
356 /*
357 * We need to re-create the patched structure. This is why we have
358 * saved the above pointers.
359 */
360 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
361 if (!out) {
362 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
363 return NULL;
364 }
365
366 out->l2h = out->data;
367 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
368 msgb_v_put(out, ref);
369 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
370
371 sms_encode_addr_element(out, new_number, 0x91, 0);
372
373
374 /* Patch the TPDU from here on */
375
376 /**
377 * Do we need to put a new TP-Destination-Address (TP-DA) here or
378 * can we copy the old thing? For the TP-DA we need to find out the
379 * new size.
380 */
381 if (new_dest_nr) {
382 uint8_t *data, *new_size;
383
384 /* reserve the size and write the header */
385 new_size = msgb_put(out, 1);
386 out->l3h = new_size + 1;
387 msgb_v_put(out, tpdu_first_byte);
388 msgb_v_put(out, data_ptr[1]);
389
390 /* encode the new number and put it */
391 if (strncmp(new_dest_nr, "00", 2) == 0)
392 sms_encode_addr_element(out, new_dest_nr + 2, 0x91, 1);
393 else
394 sms_encode_addr_element(out, new_dest_nr, 0x81, 1);
395
396 /* Copy the rest after the TP-DS */
397 data = msgb_put(out, data_len - 2 - 1 - old_dest_len);
398 memcpy(data, &data_ptr[2 + 1 + old_dest_len], data_len - 2 - 1 - old_dest_len);
399
400 /* fill in the new size */
401 new_size[0] = msgb_l3len(out);
402 } else {
403 msgb_v_put(out, data_len);
404 msgb_tv_fixed_put(out, tpdu_first_byte, data_len - 1, &data_ptr[1]);
405 }
406
407 /* prepend GSM 04.08 header */
408 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*new_hdr48) + 1);
409 memcpy(new_hdr48, old_hdr48, sizeof(*old_hdr48));
410 new_hdr48->data[0] = msgb_l2len(out);
411
412 return out;
413}
414
415/**
416 * Parse the SMS and check if it needs to be rewritten
417 */
418static struct msgb *rewrite_sms(struct bsc_nat *nat, struct msgb *msg,
419 struct bsc_nat_parsed *parsed, const char *imsi,
420 struct gsm48_hdr *hdr48, const uint32_t len)
421{
422 unsigned int payload_len;
423 unsigned int cp_len;
424
425 uint8_t ref;
426 uint8_t orig_addr_len, *orig_addr_ptr;
427 uint8_t dest_addr_len, *dest_addr_ptr;
428 uint8_t data_len, *data_ptr;
429 char smsc_addr[30];
430
431
432 uint8_t dest_len, orig_dest_len;
433 char _dest_nr[30];
434 char *dest_nr;
435 char *new_dest_nr;
436
437 char *new_number = NULL;
438 uint8_t tpdu_hdr;
439 struct msgb *out;
440
441 payload_len = len - sizeof(*hdr48);
442 if (payload_len < 1) {
443 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
444 return NULL;
445 }
446
447 cp_len = hdr48->data[0];
448 if (payload_len + 1 < cp_len) {
449 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
450 return NULL;
451 }
452
453 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
454 return NULL;
455
456 if (cp_len < 5) {
457 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
458 return NULL;
459 }
460
461 /* RP */
462 ref = hdr48->data[2];
463 orig_addr_len = hdr48->data[3];
464 orig_addr_ptr = &hdr48->data[4];
465
466 /* the +1 is for checking if the following element has some space */
467 if (cp_len < 3 + orig_addr_len + 1) {
468 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
469 return NULL;
470 }
471
472 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
473 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
474
475 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
476 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
477 return NULL;
478 }
479 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
480
481 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
482 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
483
484 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
485 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
486 return NULL;
487 }
488
489 if (data_len < 3) {
490 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
491 return NULL;
492 }
493
494 /* TP-PDU starts here */
495 if ((data_ptr[0] & 0x03) != GSM340_SMS_SUBMIT_MS2SC)
496 return NULL;
497
498 /*
499 * look into the phone number. The length is in semi-octets, we will
500 * need to add the byte for the number type as well.
501 */
502 orig_dest_len = data_ptr[2];
503 dest_len = ((orig_dest_len + 1) / 2) + 1;
504 if (data_len < dest_len + 3 || dest_len < 2) {
505 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
506 return NULL;
507 }
508
509 if ((data_ptr[3] & 0x80) == 0) {
510 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
511 return NULL;
512 }
513
514 if ((data_ptr[3] & 0x0F) == 0) {
515 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is of unknown type.\n");
516 return NULL;
517 }
518
519 /**
520 * Besides of what I think I read in GSM 03.40 and 04.11 the TP-DA
521 * contains the semi-octets as length (strlen), change it to the
522 * the number of bytes, but then change it back.
523 */
524 data_ptr[2] = dest_len;
525 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
526 &data_ptr[2], 1);
527 data_ptr[2] = orig_dest_len;
528 if ((data_ptr[3] & 0x70) == 0x10) {
529 _dest_nr[0] = _dest_nr[1] = '0';
530 dest_nr = &_dest_nr[0];
531 } else {
532 dest_nr = &_dest_nr[2];
533 }
534
535 /**
536 * Call functions to rewrite the data
537 */
538 tpdu_hdr = sms_new_tpdu_hdr(nat, imsi, dest_nr, data_ptr[0]);
539 new_number = find_new_smsc(nat, msg, imsi, smsc_addr, dest_nr);
540 new_dest_nr = sms_new_dest_nr(nat, msg, imsi, dest_nr);
541
542 if (tpdu_hdr == data_ptr[0] && !new_number && !new_dest_nr)
543 return NULL;
544
545 out = sms_create_new(GSM411_MT_RP_DATA_MO, ref, hdr48,
546 orig_addr_ptr, orig_addr_len,
547 new_number ? new_number : smsc_addr,
548 data_ptr, data_len, tpdu_hdr,
549 dest_len, new_dest_nr);
550 talloc_free(new_number);
551 talloc_free(new_dest_nr);
552 return out;
553}
554
555struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
556{
557 struct gsm48_hdr *hdr48;
558 uint32_t len;
559 uint8_t msg_type, proto;
560 struct msgb *new_msg = NULL, *sccp;
561 uint8_t link_id;
562
563 if (!imsi || strlen(imsi) < 5)
564 return msg;
565
566 /* only care about DTAP messages */
567 if (parsed->bssap != BSSAP_MSG_DTAP)
568 return msg;
569 if (!parsed->dest_local_ref)
570 return msg;
571
572 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
573 if (!hdr48)
574 return msg;
575
576 link_id = msg->l3h[1];
577 proto = hdr48->proto_discr & 0x0f;
578 msg_type = hdr48->msg_type & 0xbf;
579
580 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
581 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
582 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
583 new_msg = rewrite_sms(nat, msg, parsed, imsi, hdr48, len);
584
585 if (!new_msg)
586 return msg;
587
588 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
589 gsm0808_prepend_dtap_header(new_msg, link_id);
590 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
591 talloc_free(new_msg);
592
593 if (!sccp) {
594 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
595 return msg;
596 }
597
598 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
599
600 /* the parsed hangs off from msg but it needs to survive */
601 talloc_steal(sccp, parsed);
602 msgb_free(msg);
603 return sccp;
604}
605
606static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
607{
608 regfree(&entry->msisdn_reg);
609 regfree(&entry->num_reg);
610 talloc_free(entry->replace);
611}
612
613void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
614 const struct osmo_config_list *list)
615{
616 struct bsc_nat_num_rewr_entry *entry, *tmp;
617 struct osmo_config_entry *cfg_entry;
618
619 /* free the old data */
620 llist_for_each_entry_safe(entry, tmp, head, list) {
621 num_rewr_free_data(entry);
622 llist_del(&entry->list);
623 talloc_free(entry);
624 }
625
626
627 if (!list)
628 return;
629
630 llist_for_each_entry(cfg_entry, &list->entry, list) {
631 char *regexp;
632 if (cfg_entry->text[0] == '+') {
633 LOGP(DNAT, LOGL_ERROR,
634 "Plus is not allowed in the number\n");
635 continue;
636 }
637
638 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
639 if (!entry) {
640 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther6fbd8642013-01-01 11:25:16 +0100641 "Allocation of the num_rewr entry failed.\n");
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100642 continue;
643 }
644
645 entry->replace = talloc_strdup(entry, cfg_entry->text);
646 if (!entry->replace) {
647 LOGP(DNAT, LOGL_ERROR,
648 "Failed to copy the replacement text.\n");
649 talloc_free(entry);
650 continue;
651 }
652
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200653 if (strcmp("prefix_lookup", entry->replace) == 0)
654 entry->is_prefix_lookup = 1;
655
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100656 /* we will now build a regexp string */
657 if (cfg_entry->mcc[0] == '^') {
658 regexp = talloc_strdup(entry, cfg_entry->mcc);
659 } else {
660 regexp = talloc_asprintf(entry, "^%s%s",
661 cfg_entry->mcc[0] == '*' ?
662 "[0-9][0-9][0-9]" : cfg_entry->mcc,
663 cfg_entry->mnc[0] == '*' ?
664 "[0-9][0-9]" : cfg_entry->mnc);
665 }
666
667 if (!regexp) {
668 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
669 talloc_free(entry);
670 continue;
671 }
672
673 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
674 LOGP(DNAT, LOGL_ERROR,
675 "Failed to compile regexp '%s'\n", regexp);
676 talloc_free(regexp);
677 talloc_free(entry);
678 continue;
679 }
680
681 talloc_free(regexp);
682 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
683 LOGP(DNAT, LOGL_ERROR,
684 "Failed to compile regexp '%s'\n", cfg_entry->option);
685 regfree(&entry->msisdn_reg);
686 talloc_free(entry);
687 continue;
688 }
689
690 /* we have copied the number */
691 llist_add_tail(&entry->list, head);
692 }
693}