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