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