blob: e7c387c22ed5b0c5548cc5e4d65dcb66ae654ac3 [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>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010034#include <osmocom/core/utils.h>
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010035#include <osmocom/gsm/gsm0808.h>
Harald Welte4a88a492014-08-20 23:46:40 +020036#include <osmocom/gsm/ipa.h>
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010037
38#include <osmocom/gsm/protocol/gsm_08_08.h>
39#include <osmocom/gsm/protocol/gsm_04_11.h>
40
41#include <osmocom/sccp/sccp.h>
42
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020043static char *trie_lookup(struct nat_rewrite *trie, const char *number,
44 regoff_t off, void *ctx)
45{
46 struct nat_rewrite_rule *rule;
47
48 if (!trie) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +020049 LOGP(DCC, LOGL_ERROR,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020050 "Asked to do a table lookup but no table.\n");
51 return NULL;
52 }
53
54 rule = nat_rewrite_lookup(trie, number);
55 if (!rule) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +020056 LOGP(DCC, LOGL_DEBUG,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020057 "Couldn't find a prefix rule for %s\n", number);
58 return NULL;
59 }
60
61 return talloc_asprintf(ctx, "%s%s", rule->rewrite, &number[off]);
62}
63
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010064static char *match_and_rewrite_number(void *ctx, const char *number,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020065 const char *imsi, struct llist_head *list,
66 struct nat_rewrite *trie)
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010067{
68 struct bsc_nat_num_rewr_entry *entry;
69 char *new_number = NULL;
70
71 /* need to find a replacement and then fix it */
72 llist_for_each_entry(entry, list, list) {
73 regmatch_t matches[2];
74
75 /* check the IMSI match */
76 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
77 continue;
78
79 /* this regexp matches... */
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020080 if (regexec(&entry->num_reg, number, 2, matches, 0) == 0
81 && matches[1].rm_eo != -1) {
82 if (entry->is_prefix_lookup)
83 new_number = trie_lookup(trie, number,
84 matches[1].rm_so, ctx);
85 else
86 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010087 entry->replace,
88 &number[matches[1].rm_so]);
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020089 }
90
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +010091 if (new_number)
92 break;
93 }
94
95 return new_number;
96}
97
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +020098static char *rewrite_isdn_number(struct bsc_nat *nat, struct llist_head *rewr_list,
99 void *ctx, const char *imsi,
100 struct gsm_mncc_number *called)
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100101{
Holger Hans Peter Freyther73460812013-07-05 07:50:30 +0200102 char int_number[sizeof(called->number) + 2];
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200103 char *number = called->number;
104
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200105 if (llist_empty(&nat->num_rewr)) {
106 LOGP(DCC, LOGL_DEBUG, "Rewrite rules empty.\n");
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100107 return NULL;
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200108 }
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100109
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200110 /* only ISDN plan */
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200111 if (called->plan != 1) {
112 LOGP(DCC, LOGL_DEBUG, "Called plan is not 1 it was %d\n",
113 called->plan);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100114 return NULL;
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200115 }
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100116
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200117 /* international, prepend */
118 if (called->type == 1) {
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +0200119 int_number[0] = '+';
120 memcpy(&int_number[1], number, strlen(number) + 1);
121 number = int_number;
122 }
123
124 return match_and_rewrite_number(ctx, number,
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200125 imsi, rewr_list, nat->num_rewr_trie);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100126}
127
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200128static void update_called_number(struct gsm_mncc_number *called,
129 const char *chosen_number)
130{
131 if (strncmp(chosen_number, "00", 2) == 0) {
132 called->type = 1;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100133 osmo_strlcpy(called->number, chosen_number + 2,
134 sizeof(called->number));
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200135 } else {
136 /* rewrite international to unknown */
137 if (called->type == 1)
138 called->type = 0;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100139 osmo_strlcpy(called->number, chosen_number,
140 sizeof(called->number));
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200141 }
142}
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100143
144/**
145 * Rewrite non global numbers... according to rules based on the IMSI
146 */
147static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
148 struct bsc_nat_parsed *parsed, const char *imsi,
149 struct gsm48_hdr *hdr48, const uint32_t len)
150{
151 struct tlv_parsed tp;
152 unsigned int payload_len;
153 struct gsm_mncc_number called;
154 struct msgb *out;
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200155 char *new_number_pre = NULL, *new_number_post = NULL, *chosen_number;
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100156 uint8_t *outptr;
157 const uint8_t *msgptr;
158 int sec_len;
159
160 /* decode and rewrite the message */
161 payload_len = len - sizeof(*hdr48);
162 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
163
164 /* no number, well let us ignore it */
165 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
166 return NULL;
167
168 memset(&called, 0, sizeof(called));
169 gsm48_decode_called(&called,
170 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
171
172 /* check if it looks international and stop */
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200173 LOGP(DCC, LOGL_DEBUG,
174 "Pre-Rewrite for IMSI(%s) Plan(%d) Type(%d) Number(%s)\n",
175 imsi, called.plan, called.type, called.number);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200176 new_number_pre = rewrite_isdn_number(nat, &nat->num_rewr, msg, imsi, &called);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100177
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200178 if (!new_number_pre) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200179 LOGP(DCC, LOGL_DEBUG, "No IMSI(%s) match found, returning message.\n",
180 imsi);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100181 return NULL;
182 }
183
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200184 if (strlen(new_number_pre) > sizeof(called.number)) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200185 LOGP(DCC, LOGL_ERROR, "Number %s is too long for structure.\n",
186 new_number_pre);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200187 talloc_free(new_number_pre);
188 return NULL;
189 }
190 update_called_number(&called, new_number_pre);
191
192 /* another run through the re-write engine with other rules */
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200193 LOGP(DCC, LOGL_DEBUG,
194 "Post-Rewrite for IMSI(%s) Plan(%d) Type(%d) Number(%s)\n",
195 imsi, called.plan, called.type, called.number);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200196 new_number_post = rewrite_isdn_number(nat, &nat->num_rewr_post, msg,
197 imsi, &called);
198 chosen_number = new_number_post ? new_number_post : new_number_pre;
199
200
201 if (strlen(chosen_number) > sizeof(called.number)) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200202 LOGP(DCC, LOGL_ERROR, "Number %s is too long for structure.\n",
203 chosen_number);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200204 talloc_free(new_number_pre);
205 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100206 return NULL;
207 }
208
209 /*
210 * Need to create a new message now based on the old onew
211 * with a new number. We can sadly not patch this in place
212 * so we will need to regenerate it.
213 */
214
215 out = msgb_alloc_headroom(4096, 128, "changed-setup");
216 if (!out) {
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200217 LOGP(DCC, LOGL_ERROR, "Failed to allocate.\n");
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200218 talloc_free(new_number_pre);
219 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100220 return NULL;
221 }
222
223 /* copy the header */
224 outptr = msgb_put(out, sizeof(*hdr48));
225 memcpy(outptr, hdr48, sizeof(*hdr48));
226
227 /* copy everything up to the number */
228 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
229 outptr = msgb_put(out, sec_len);
230 memcpy(outptr, &hdr48->data[0], sec_len);
231
232 /* create the new number */
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200233 update_called_number(&called, chosen_number);
Holger Hans Peter Freyther52f705e2013-07-24 07:46:06 +0200234 LOGP(DCC, LOGL_DEBUG,
235 "Chosen number for IMSI(%s) is Plan(%d) Type(%d) Number(%s)\n",
236 imsi, called.plan, called.type, called.number);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100237 gsm48_encode_called(out, &called);
238
239 /* copy thre rest */
240 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
241 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
242 sec_len = payload_len - (msgptr - &hdr48->data[0]);
243 outptr = msgb_put(out, sec_len);
244 memcpy(outptr, msgptr, sec_len);
245
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200246 talloc_free(new_number_pre);
247 talloc_free(new_number_post);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100248 return out;
249}
250
251/**
252 * Find a new SMSC address, returns an allocated string that needs to be
253 * freed or is NULL.
254 */
255static char *find_new_smsc(struct bsc_nat *nat, void *ctx, const char *imsi,
256 const char *smsc_addr, const char *dest_nr)
257{
258 struct bsc_nat_num_rewr_entry *entry;
259 char *new_number = NULL;
260 uint8_t dest_match = llist_empty(&nat->tpdest_match);
261
262 /* We will find a new number now */
263 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
264 regmatch_t matches[2];
265
266 /* check the IMSI match */
267 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
268 continue;
269
270 /* this regexp matches... */
271 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
272 matches[1].rm_eo != -1)
273 new_number = talloc_asprintf(ctx, "%s%s",
274 entry->replace,
275 &smsc_addr[matches[1].rm_so]);
276 if (new_number)
277 break;
278 }
279
280 if (!new_number)
281 return NULL;
282
283 /*
284 * now match the number against another list
285 */
286 llist_for_each_entry(entry, &nat->tpdest_match, list) {
287 /* check the IMSI match */
288 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
289 continue;
290
291 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
292 dest_match = 1;
293 break;
294 }
295 }
296
297 if (!dest_match) {
298 talloc_free(new_number);
299 return NULL;
300 }
301
302 return new_number;
303}
304
305/**
306 * Clear the TP-SRR from the TPDU header
307 */
308static uint8_t sms_new_tpdu_hdr(struct bsc_nat *nat, const char *imsi,
309 const char *dest_nr, uint8_t hdr)
310{
311 struct bsc_nat_num_rewr_entry *entry;
312
313 /* We will find a new number now */
314 llist_for_each_entry(entry, &nat->sms_clear_tp_srr, list) {
315 /* check the IMSI match */
316 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
317 continue;
318 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) != 0)
319 continue;
320
321 /* matched phone number and imsi */
322 return hdr & ~0x20;
323 }
324
325 return hdr;
326}
327
328/**
329 * Check if we need to rewrite the number. For this SMS.
330 */
331static char *sms_new_dest_nr(struct bsc_nat *nat, void *ctx,
332 const char *imsi, const char *dest_nr)
333{
334 return match_and_rewrite_number(ctx, dest_nr, imsi,
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200335 &nat->sms_num_rewr, NULL);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100336}
337
338/**
339 * This is a helper for GSM 04.11 8.2.5.2 Destination address element
340 */
341void sms_encode_addr_element(struct msgb *out, const char *new_number,
342 int format, int tp_data)
343{
344 uint8_t new_addr_len;
345 uint8_t new_addr[26];
346
347 /*
348 * Copy the new number. We let libosmocore encode it, then set
349 * the extension followed after the length. Depending on if
350 * we want to write RP we will let the TLV code add the
351 * length for us or we need to use strlen... This is not very clear
352 * as of 03.40 and 04.11.
353 */
354 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
355 1, new_number);
356 new_addr[1] = format;
357 if (tp_data) {
358 uint8_t *data = msgb_put(out, new_addr_len);
359 memcpy(data, new_addr, new_addr_len);
360 data[0] = strlen(new_number);
361 } else {
362 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
363 }
364}
365
366static struct msgb *sms_create_new(uint8_t type, uint8_t ref,
367 struct gsm48_hdr *old_hdr48,
368 const uint8_t *orig_addr_ptr,
369 int orig_addr_len, const char *new_number,
370 const uint8_t *data_ptr, int data_len,
371 uint8_t tpdu_first_byte,
372 const int old_dest_len, const char *new_dest_nr)
373{
374 struct gsm48_hdr *new_hdr48;
375 struct msgb *out;
376
377 /*
378 * We need to re-create the patched structure. This is why we have
379 * saved the above pointers.
380 */
381 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
382 if (!out) {
383 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
384 return NULL;
385 }
386
387 out->l2h = out->data;
388 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
389 msgb_v_put(out, ref);
390 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
391
392 sms_encode_addr_element(out, new_number, 0x91, 0);
393
394
395 /* Patch the TPDU from here on */
396
397 /**
398 * Do we need to put a new TP-Destination-Address (TP-DA) here or
399 * can we copy the old thing? For the TP-DA we need to find out the
400 * new size.
401 */
402 if (new_dest_nr) {
403 uint8_t *data, *new_size;
404
405 /* reserve the size and write the header */
406 new_size = msgb_put(out, 1);
407 out->l3h = new_size + 1;
408 msgb_v_put(out, tpdu_first_byte);
409 msgb_v_put(out, data_ptr[1]);
410
411 /* encode the new number and put it */
412 if (strncmp(new_dest_nr, "00", 2) == 0)
413 sms_encode_addr_element(out, new_dest_nr + 2, 0x91, 1);
414 else
415 sms_encode_addr_element(out, new_dest_nr, 0x81, 1);
416
417 /* Copy the rest after the TP-DS */
418 data = msgb_put(out, data_len - 2 - 1 - old_dest_len);
419 memcpy(data, &data_ptr[2 + 1 + old_dest_len], data_len - 2 - 1 - old_dest_len);
420
421 /* fill in the new size */
422 new_size[0] = msgb_l3len(out);
423 } else {
424 msgb_v_put(out, data_len);
425 msgb_tv_fixed_put(out, tpdu_first_byte, data_len - 1, &data_ptr[1]);
426 }
427
428 /* prepend GSM 04.08 header */
429 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*new_hdr48) + 1);
430 memcpy(new_hdr48, old_hdr48, sizeof(*old_hdr48));
431 new_hdr48->data[0] = msgb_l2len(out);
432
433 return out;
434}
435
436/**
437 * Parse the SMS and check if it needs to be rewritten
438 */
439static struct msgb *rewrite_sms(struct bsc_nat *nat, struct msgb *msg,
440 struct bsc_nat_parsed *parsed, const char *imsi,
441 struct gsm48_hdr *hdr48, const uint32_t len)
442{
443 unsigned int payload_len;
444 unsigned int cp_len;
445
446 uint8_t ref;
447 uint8_t orig_addr_len, *orig_addr_ptr;
448 uint8_t dest_addr_len, *dest_addr_ptr;
449 uint8_t data_len, *data_ptr;
450 char smsc_addr[30];
451
452
453 uint8_t dest_len, orig_dest_len;
454 char _dest_nr[30];
455 char *dest_nr;
456 char *new_dest_nr;
457
458 char *new_number = NULL;
459 uint8_t tpdu_hdr;
460 struct msgb *out;
461
462 payload_len = len - sizeof(*hdr48);
463 if (payload_len < 1) {
464 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
465 return NULL;
466 }
467
468 cp_len = hdr48->data[0];
469 if (payload_len + 1 < cp_len) {
470 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
471 return NULL;
472 }
473
474 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
475 return NULL;
476
477 if (cp_len < 5) {
478 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
479 return NULL;
480 }
481
482 /* RP */
483 ref = hdr48->data[2];
484 orig_addr_len = hdr48->data[3];
485 orig_addr_ptr = &hdr48->data[4];
486
487 /* the +1 is for checking if the following element has some space */
488 if (cp_len < 3 + orig_addr_len + 1) {
489 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
490 return NULL;
491 }
492
493 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
494 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
495
496 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
497 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
498 return NULL;
499 }
500 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
501
502 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
503 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
504
505 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
506 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
507 return NULL;
508 }
509
510 if (data_len < 3) {
511 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
512 return NULL;
513 }
514
515 /* TP-PDU starts here */
516 if ((data_ptr[0] & 0x03) != GSM340_SMS_SUBMIT_MS2SC)
517 return NULL;
518
519 /*
520 * look into the phone number. The length is in semi-octets, we will
521 * need to add the byte for the number type as well.
522 */
523 orig_dest_len = data_ptr[2];
524 dest_len = ((orig_dest_len + 1) / 2) + 1;
525 if (data_len < dest_len + 3 || dest_len < 2) {
526 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
527 return NULL;
528 }
529
530 if ((data_ptr[3] & 0x80) == 0) {
531 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
532 return NULL;
533 }
534
535 if ((data_ptr[3] & 0x0F) == 0) {
536 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is of unknown type.\n");
537 return NULL;
538 }
539
540 /**
541 * Besides of what I think I read in GSM 03.40 and 04.11 the TP-DA
542 * contains the semi-octets as length (strlen), change it to the
543 * the number of bytes, but then change it back.
544 */
545 data_ptr[2] = dest_len;
546 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
547 &data_ptr[2], 1);
548 data_ptr[2] = orig_dest_len;
549 if ((data_ptr[3] & 0x70) == 0x10) {
550 _dest_nr[0] = _dest_nr[1] = '0';
551 dest_nr = &_dest_nr[0];
552 } else {
553 dest_nr = &_dest_nr[2];
554 }
555
556 /**
557 * Call functions to rewrite the data
558 */
559 tpdu_hdr = sms_new_tpdu_hdr(nat, imsi, dest_nr, data_ptr[0]);
560 new_number = find_new_smsc(nat, msg, imsi, smsc_addr, dest_nr);
561 new_dest_nr = sms_new_dest_nr(nat, msg, imsi, dest_nr);
562
563 if (tpdu_hdr == data_ptr[0] && !new_number && !new_dest_nr)
564 return NULL;
565
566 out = sms_create_new(GSM411_MT_RP_DATA_MO, ref, hdr48,
567 orig_addr_ptr, orig_addr_len,
568 new_number ? new_number : smsc_addr,
569 data_ptr, data_len, tpdu_hdr,
570 dest_len, new_dest_nr);
571 talloc_free(new_number);
572 talloc_free(new_dest_nr);
573 return out;
574}
575
576struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
577{
578 struct gsm48_hdr *hdr48;
579 uint32_t len;
580 uint8_t msg_type, proto;
581 struct msgb *new_msg = NULL, *sccp;
582 uint8_t link_id;
583
584 if (!imsi || strlen(imsi) < 5)
585 return msg;
586
587 /* only care about DTAP messages */
588 if (parsed->bssap != BSSAP_MSG_DTAP)
589 return msg;
590 if (!parsed->dest_local_ref)
591 return msg;
592
593 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
594 if (!hdr48)
595 return msg;
596
597 link_id = msg->l3h[1];
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100598 proto = gsm48_hdr_pdisc(hdr48);
599 msg_type = gsm48_hdr_msg_type(hdr48);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100600
601 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
602 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
603 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
604 new_msg = rewrite_sms(nat, msg, parsed, imsi, hdr48, len);
605
606 if (!new_msg)
607 return msg;
608
609 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
610 gsm0808_prepend_dtap_header(new_msg, link_id);
611 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
612 talloc_free(new_msg);
613
614 if (!sccp) {
615 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
616 return msg;
617 }
618
Harald Welte4a88a492014-08-20 23:46:40 +0200619 ipa_prepend_header(sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100620
621 /* the parsed hangs off from msg but it needs to survive */
622 talloc_steal(sccp, parsed);
623 msgb_free(msg);
624 return sccp;
625}
626
627static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
628{
629 regfree(&entry->msisdn_reg);
630 regfree(&entry->num_reg);
631 talloc_free(entry->replace);
632}
633
634void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
635 const struct osmo_config_list *list)
636{
637 struct bsc_nat_num_rewr_entry *entry, *tmp;
638 struct osmo_config_entry *cfg_entry;
639
640 /* free the old data */
641 llist_for_each_entry_safe(entry, tmp, head, list) {
642 num_rewr_free_data(entry);
643 llist_del(&entry->list);
644 talloc_free(entry);
645 }
646
647
648 if (!list)
649 return;
650
651 llist_for_each_entry(cfg_entry, &list->entry, list) {
652 char *regexp;
653 if (cfg_entry->text[0] == '+') {
654 LOGP(DNAT, LOGL_ERROR,
655 "Plus is not allowed in the number\n");
656 continue;
657 }
658
659 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
660 if (!entry) {
661 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther6fbd8642013-01-01 11:25:16 +0100662 "Allocation of the num_rewr entry failed.\n");
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100663 continue;
664 }
665
666 entry->replace = talloc_strdup(entry, cfg_entry->text);
667 if (!entry->replace) {
668 LOGP(DNAT, LOGL_ERROR,
669 "Failed to copy the replacement text.\n");
670 talloc_free(entry);
671 continue;
672 }
673
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200674 if (strcmp("prefix_lookup", entry->replace) == 0)
675 entry->is_prefix_lookup = 1;
676
Holger Hans Peter Freyther6ecb3cb2012-12-17 13:25:36 +0100677 /* we will now build a regexp string */
678 if (cfg_entry->mcc[0] == '^') {
679 regexp = talloc_strdup(entry, cfg_entry->mcc);
680 } else {
681 regexp = talloc_asprintf(entry, "^%s%s",
682 cfg_entry->mcc[0] == '*' ?
683 "[0-9][0-9][0-9]" : cfg_entry->mcc,
684 cfg_entry->mnc[0] == '*' ?
685 "[0-9][0-9]" : cfg_entry->mnc);
686 }
687
688 if (!regexp) {
689 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
690 talloc_free(entry);
691 continue;
692 }
693
694 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
695 LOGP(DNAT, LOGL_ERROR,
696 "Failed to compile regexp '%s'\n", regexp);
697 talloc_free(regexp);
698 talloc_free(entry);
699 continue;
700 }
701
702 talloc_free(regexp);
703 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
704 LOGP(DNAT, LOGL_ERROR,
705 "Failed to compile regexp '%s'\n", cfg_entry->option);
706 regfree(&entry->msisdn_reg);
707 talloc_free(entry);
708 continue;
709 }
710
711 /* we have copied the number */
712 llist_add_tail(&entry->list, head);
713 }
714}