blob: 5984d962d19074ef90de71e850c8bdeb295dd6b0 [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 Freytherdbd94492013-04-02 12:34:11 +020096static char *rewrite_isdn_number(struct bsc_nat *nat, void *ctx, const char *imsi,
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010097 struct gsm_mncc_number *called)
98{
Holger Hans Peter Freyther73460812013-07-05 07:50:30 +020099 char int_number[sizeof(called->number) + 2];
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200100 char *number = called->number;
101
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100102 if (llist_empty(&nat->num_rewr))
103 return NULL;
104
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200105 /* only ISDN plan */
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100106 if (called->plan != 1)
107 return NULL;
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100108
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200109 /* international, prepend */
110 if (called->type == 1) {
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200111 int_number[0] = '+';
112 memcpy(&int_number[1], number, strlen(number) + 1);
113 number = int_number;
114 }
115
116 return match_and_rewrite_number(ctx, number,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200117 imsi, &nat->num_rewr, nat->num_rewr_trie);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100118}
119
120
121/**
122 * Rewrite non global numbers... according to rules based on the IMSI
123 */
124static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
125 struct bsc_nat_parsed *parsed, const char *imsi,
126 struct gsm48_hdr *hdr48, const uint32_t len)
127{
128 struct tlv_parsed tp;
129 unsigned int payload_len;
130 struct gsm_mncc_number called;
131 struct msgb *out;
132 char *new_number = NULL;
133 uint8_t *outptr;
134 const uint8_t *msgptr;
135 int sec_len;
136
137 /* decode and rewrite the message */
138 payload_len = len - sizeof(*hdr48);
139 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
140
141 /* no number, well let us ignore it */
142 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
143 return NULL;
144
145 memset(&called, 0, sizeof(called));
146 gsm48_decode_called(&called,
147 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
148
149 /* check if it looks international and stop */
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200150 new_number = rewrite_isdn_number(nat, msg, imsi, &called);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100151
152 if (!new_number) {
153 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
154 return NULL;
155 }
156
157 if (strlen(new_number) > sizeof(called.number)) {
158 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
159 talloc_free(new_number);
160 return NULL;
161 }
162
163 /*
164 * Need to create a new message now based on the old onew
165 * with a new number. We can sadly not patch this in place
166 * so we will need to regenerate it.
167 */
168
169 out = msgb_alloc_headroom(4096, 128, "changed-setup");
170 if (!out) {
171 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
172 talloc_free(new_number);
173 return NULL;
174 }
175
176 /* copy the header */
177 outptr = msgb_put(out, sizeof(*hdr48));
178 memcpy(outptr, hdr48, sizeof(*hdr48));
179
180 /* copy everything up to the number */
181 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
182 outptr = msgb_put(out, sec_len);
183 memcpy(outptr, &hdr48->data[0], sec_len);
184
185 /* create the new number */
186 if (strncmp(new_number, "00", 2) == 0) {
187 called.type = 1;
188 strncpy(called.number, new_number + 2, sizeof(called.number));
189 } else {
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200190 /* rewrite international to unknown */
191 if (called.type == 1)
192 called.type = 0;
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100193 strncpy(called.number, new_number, sizeof(called.number));
194 }
195 gsm48_encode_called(out, &called);
196
197 /* copy thre rest */
198 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
199 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
200 sec_len = payload_len - (msgptr - &hdr48->data[0]);
201 outptr = msgb_put(out, sec_len);
202 memcpy(outptr, msgptr, sec_len);
203
204 talloc_free(new_number);
205 return out;
206}
207
208/**
209 * Find a new SMSC address, returns an allocated string that needs to be
210 * freed or is NULL.
211 */
212static char *find_new_smsc(struct bsc_nat *nat, void *ctx, const char *imsi,
213 const char *smsc_addr, const char *dest_nr)
214{
215 struct bsc_nat_num_rewr_entry *entry;
216 char *new_number = NULL;
217 uint8_t dest_match = llist_empty(&nat->tpdest_match);
218
219 /* We will find a new number now */
220 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
221 regmatch_t matches[2];
222
223 /* check the IMSI match */
224 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
225 continue;
226
227 /* this regexp matches... */
228 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
229 matches[1].rm_eo != -1)
230 new_number = talloc_asprintf(ctx, "%s%s",
231 entry->replace,
232 &smsc_addr[matches[1].rm_so]);
233 if (new_number)
234 break;
235 }
236
237 if (!new_number)
238 return NULL;
239
240 /*
241 * now match the number against another list
242 */
243 llist_for_each_entry(entry, &nat->tpdest_match, list) {
244 /* check the IMSI match */
245 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
246 continue;
247
248 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
249 dest_match = 1;
250 break;
251 }
252 }
253
254 if (!dest_match) {
255 talloc_free(new_number);
256 return NULL;
257 }
258
259 return new_number;
260}
261
262/**
263 * Clear the TP-SRR from the TPDU header
264 */
265static uint8_t sms_new_tpdu_hdr(struct bsc_nat *nat, const char *imsi,
266 const char *dest_nr, uint8_t hdr)
267{
268 struct bsc_nat_num_rewr_entry *entry;
269
270 /* We will find a new number now */
271 llist_for_each_entry(entry, &nat->sms_clear_tp_srr, list) {
272 /* check the IMSI match */
273 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
274 continue;
275 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) != 0)
276 continue;
277
278 /* matched phone number and imsi */
279 return hdr & ~0x20;
280 }
281
282 return hdr;
283}
284
285/**
286 * Check if we need to rewrite the number. For this SMS.
287 */
288static char *sms_new_dest_nr(struct bsc_nat *nat, void *ctx,
289 const char *imsi, const char *dest_nr)
290{
291 return match_and_rewrite_number(ctx, dest_nr, imsi,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200292 &nat->sms_num_rewr, NULL);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100293}
294
295/**
296 * This is a helper for GSM 04.11 8.2.5.2 Destination address element
297 */
298void sms_encode_addr_element(struct msgb *out, const char *new_number,
299 int format, int tp_data)
300{
301 uint8_t new_addr_len;
302 uint8_t new_addr[26];
303
304 /*
305 * Copy the new number. We let libosmocore encode it, then set
306 * the extension followed after the length. Depending on if
307 * we want to write RP we will let the TLV code add the
308 * length for us or we need to use strlen... This is not very clear
309 * as of 03.40 and 04.11.
310 */
311 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
312 1, new_number);
313 new_addr[1] = format;
314 if (tp_data) {
315 uint8_t *data = msgb_put(out, new_addr_len);
316 memcpy(data, new_addr, new_addr_len);
317 data[0] = strlen(new_number);
318 } else {
319 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
320 }
321}
322
323static struct msgb *sms_create_new(uint8_t type, uint8_t ref,
324 struct gsm48_hdr *old_hdr48,
325 const uint8_t *orig_addr_ptr,
326 int orig_addr_len, const char *new_number,
327 const uint8_t *data_ptr, int data_len,
328 uint8_t tpdu_first_byte,
329 const int old_dest_len, const char *new_dest_nr)
330{
331 struct gsm48_hdr *new_hdr48;
332 struct msgb *out;
333
334 /*
335 * We need to re-create the patched structure. This is why we have
336 * saved the above pointers.
337 */
338 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
339 if (!out) {
340 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
341 return NULL;
342 }
343
344 out->l2h = out->data;
345 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
346 msgb_v_put(out, ref);
347 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
348
349 sms_encode_addr_element(out, new_number, 0x91, 0);
350
351
352 /* Patch the TPDU from here on */
353
354 /**
355 * Do we need to put a new TP-Destination-Address (TP-DA) here or
356 * can we copy the old thing? For the TP-DA we need to find out the
357 * new size.
358 */
359 if (new_dest_nr) {
360 uint8_t *data, *new_size;
361
362 /* reserve the size and write the header */
363 new_size = msgb_put(out, 1);
364 out->l3h = new_size + 1;
365 msgb_v_put(out, tpdu_first_byte);
366 msgb_v_put(out, data_ptr[1]);
367
368 /* encode the new number and put it */
369 if (strncmp(new_dest_nr, "00", 2) == 0)
370 sms_encode_addr_element(out, new_dest_nr + 2, 0x91, 1);
371 else
372 sms_encode_addr_element(out, new_dest_nr, 0x81, 1);
373
374 /* Copy the rest after the TP-DS */
375 data = msgb_put(out, data_len - 2 - 1 - old_dest_len);
376 memcpy(data, &data_ptr[2 + 1 + old_dest_len], data_len - 2 - 1 - old_dest_len);
377
378 /* fill in the new size */
379 new_size[0] = msgb_l3len(out);
380 } else {
381 msgb_v_put(out, data_len);
382 msgb_tv_fixed_put(out, tpdu_first_byte, data_len - 1, &data_ptr[1]);
383 }
384
385 /* prepend GSM 04.08 header */
386 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*new_hdr48) + 1);
387 memcpy(new_hdr48, old_hdr48, sizeof(*old_hdr48));
388 new_hdr48->data[0] = msgb_l2len(out);
389
390 return out;
391}
392
393/**
394 * Parse the SMS and check if it needs to be rewritten
395 */
396static struct msgb *rewrite_sms(struct bsc_nat *nat, struct msgb *msg,
397 struct bsc_nat_parsed *parsed, const char *imsi,
398 struct gsm48_hdr *hdr48, const uint32_t len)
399{
400 unsigned int payload_len;
401 unsigned int cp_len;
402
403 uint8_t ref;
404 uint8_t orig_addr_len, *orig_addr_ptr;
405 uint8_t dest_addr_len, *dest_addr_ptr;
406 uint8_t data_len, *data_ptr;
407 char smsc_addr[30];
408
409
410 uint8_t dest_len, orig_dest_len;
411 char _dest_nr[30];
412 char *dest_nr;
413 char *new_dest_nr;
414
415 char *new_number = NULL;
416 uint8_t tpdu_hdr;
417 struct msgb *out;
418
419 payload_len = len - sizeof(*hdr48);
420 if (payload_len < 1) {
421 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
422 return NULL;
423 }
424
425 cp_len = hdr48->data[0];
426 if (payload_len + 1 < cp_len) {
427 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
428 return NULL;
429 }
430
431 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
432 return NULL;
433
434 if (cp_len < 5) {
435 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
436 return NULL;
437 }
438
439 /* RP */
440 ref = hdr48->data[2];
441 orig_addr_len = hdr48->data[3];
442 orig_addr_ptr = &hdr48->data[4];
443
444 /* the +1 is for checking if the following element has some space */
445 if (cp_len < 3 + orig_addr_len + 1) {
446 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
447 return NULL;
448 }
449
450 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
451 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
452
453 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
454 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
455 return NULL;
456 }
457 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
458
459 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
460 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
461
462 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
463 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
464 return NULL;
465 }
466
467 if (data_len < 3) {
468 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
469 return NULL;
470 }
471
472 /* TP-PDU starts here */
473 if ((data_ptr[0] & 0x03) != GSM340_SMS_SUBMIT_MS2SC)
474 return NULL;
475
476 /*
477 * look into the phone number. The length is in semi-octets, we will
478 * need to add the byte for the number type as well.
479 */
480 orig_dest_len = data_ptr[2];
481 dest_len = ((orig_dest_len + 1) / 2) + 1;
482 if (data_len < dest_len + 3 || dest_len < 2) {
483 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
484 return NULL;
485 }
486
487 if ((data_ptr[3] & 0x80) == 0) {
488 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
489 return NULL;
490 }
491
492 if ((data_ptr[3] & 0x0F) == 0) {
493 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is of unknown type.\n");
494 return NULL;
495 }
496
497 /**
498 * Besides of what I think I read in GSM 03.40 and 04.11 the TP-DA
499 * contains the semi-octets as length (strlen), change it to the
500 * the number of bytes, but then change it back.
501 */
502 data_ptr[2] = dest_len;
503 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
504 &data_ptr[2], 1);
505 data_ptr[2] = orig_dest_len;
506 if ((data_ptr[3] & 0x70) == 0x10) {
507 _dest_nr[0] = _dest_nr[1] = '0';
508 dest_nr = &_dest_nr[0];
509 } else {
510 dest_nr = &_dest_nr[2];
511 }
512
513 /**
514 * Call functions to rewrite the data
515 */
516 tpdu_hdr = sms_new_tpdu_hdr(nat, imsi, dest_nr, data_ptr[0]);
517 new_number = find_new_smsc(nat, msg, imsi, smsc_addr, dest_nr);
518 new_dest_nr = sms_new_dest_nr(nat, msg, imsi, dest_nr);
519
520 if (tpdu_hdr == data_ptr[0] && !new_number && !new_dest_nr)
521 return NULL;
522
523 out = sms_create_new(GSM411_MT_RP_DATA_MO, ref, hdr48,
524 orig_addr_ptr, orig_addr_len,
525 new_number ? new_number : smsc_addr,
526 data_ptr, data_len, tpdu_hdr,
527 dest_len, new_dest_nr);
528 talloc_free(new_number);
529 talloc_free(new_dest_nr);
530 return out;
531}
532
533struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
534{
535 struct gsm48_hdr *hdr48;
536 uint32_t len;
537 uint8_t msg_type, proto;
538 struct msgb *new_msg = NULL, *sccp;
539 uint8_t link_id;
540
541 if (!imsi || strlen(imsi) < 5)
542 return msg;
543
544 /* only care about DTAP messages */
545 if (parsed->bssap != BSSAP_MSG_DTAP)
546 return msg;
547 if (!parsed->dest_local_ref)
548 return msg;
549
550 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
551 if (!hdr48)
552 return msg;
553
554 link_id = msg->l3h[1];
555 proto = hdr48->proto_discr & 0x0f;
556 msg_type = hdr48->msg_type & 0xbf;
557
558 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
559 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
560 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
561 new_msg = rewrite_sms(nat, msg, parsed, imsi, hdr48, len);
562
563 if (!new_msg)
564 return msg;
565
566 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
567 gsm0808_prepend_dtap_header(new_msg, link_id);
568 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
569 talloc_free(new_msg);
570
571 if (!sccp) {
572 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
573 return msg;
574 }
575
576 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
577
578 /* the parsed hangs off from msg but it needs to survive */
579 talloc_steal(sccp, parsed);
580 msgb_free(msg);
581 return sccp;
582}
583
584static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
585{
586 regfree(&entry->msisdn_reg);
587 regfree(&entry->num_reg);
588 talloc_free(entry->replace);
589}
590
591void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
592 const struct osmo_config_list *list)
593{
594 struct bsc_nat_num_rewr_entry *entry, *tmp;
595 struct osmo_config_entry *cfg_entry;
596
597 /* free the old data */
598 llist_for_each_entry_safe(entry, tmp, head, list) {
599 num_rewr_free_data(entry);
600 llist_del(&entry->list);
601 talloc_free(entry);
602 }
603
604
605 if (!list)
606 return;
607
608 llist_for_each_entry(cfg_entry, &list->entry, list) {
609 char *regexp;
610 if (cfg_entry->text[0] == '+') {
611 LOGP(DNAT, LOGL_ERROR,
612 "Plus is not allowed in the number\n");
613 continue;
614 }
615
616 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
617 if (!entry) {
618 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther6fbd8642013-01-01 11:25:16 +0100619 "Allocation of the num_rewr entry failed.\n");
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100620 continue;
621 }
622
623 entry->replace = talloc_strdup(entry, cfg_entry->text);
624 if (!entry->replace) {
625 LOGP(DNAT, LOGL_ERROR,
626 "Failed to copy the replacement text.\n");
627 talloc_free(entry);
628 continue;
629 }
630
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200631 if (strcmp("prefix_lookup", entry->replace) == 0)
632 entry->is_prefix_lookup = 1;
633
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100634 /* we will now build a regexp string */
635 if (cfg_entry->mcc[0] == '^') {
636 regexp = talloc_strdup(entry, cfg_entry->mcc);
637 } else {
638 regexp = talloc_asprintf(entry, "^%s%s",
639 cfg_entry->mcc[0] == '*' ?
640 "[0-9][0-9][0-9]" : cfg_entry->mcc,
641 cfg_entry->mnc[0] == '*' ?
642 "[0-9][0-9]" : cfg_entry->mnc);
643 }
644
645 if (!regexp) {
646 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
647 talloc_free(entry);
648 continue;
649 }
650
651 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
652 LOGP(DNAT, LOGL_ERROR,
653 "Failed to compile regexp '%s'\n", regexp);
654 talloc_free(regexp);
655 talloc_free(entry);
656 continue;
657 }
658
659 talloc_free(regexp);
660 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
661 LOGP(DNAT, LOGL_ERROR,
662 "Failed to compile regexp '%s'\n", cfg_entry->option);
663 regfree(&entry->msisdn_reg);
664 talloc_free(entry);
665 continue;
666 }
667
668 /* we have copied the number */
669 llist_add_tail(&entry->list, head);
670 }
671}