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