Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Access filtering |
| 3 | */ |
| 4 | /* |
Holger Hans Peter Freyther | 973dbae | 2015-04-04 20:47:03 +0200 | [diff] [blame] | 5 | * (C) 2010-2015 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 6 | * (C) 2010-2012 by On-Waves |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +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 | |
Holger Hans Peter Freyther | 973dbae | 2015-04-04 20:47:03 +0200 | [diff] [blame] | 24 | #include <openbsc/bsc_msg_filter.h> |
| 25 | |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 26 | #include <openbsc/bsc_nat.h> |
| 27 | #include <openbsc/bsc_nat_sccp.h> |
| 28 | #include <openbsc/bsc_msc.h> |
| 29 | #include <openbsc/gsm_data.h> |
| 30 | #include <openbsc/debug.h> |
| 31 | #include <openbsc/ipaccess.h> |
| 32 | |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/gsm/gsm0808.h> |
| 35 | |
| 36 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 37 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
| 38 | |
| 39 | #include <osmocom/sccp/sccp.h> |
| 40 | |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 41 | int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) |
| 42 | { |
| 43 | struct bsc_nat_barr_entry *n; |
| 44 | n = rb_entry(root->rb_node, struct bsc_nat_barr_entry, node); |
| 45 | |
| 46 | while (n) { |
| 47 | int rc = strcmp(imsi, n->imsi); |
| 48 | if (rc == 0) { |
| 49 | *cm = n->cm_reject_cause; |
| 50 | *lu = n->lu_reject_cause; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | n = rb_entry( |
| 55 | (rc < 0) ? n->node.rb_left : n->node.rb_right, |
| 56 | struct bsc_nat_barr_entry, node); |
| 57 | }; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *root) |
| 63 | { |
| 64 | struct rb_node **new = &root->rb_node, *parent = NULL; |
| 65 | |
| 66 | while (*new) { |
| 67 | int rc; |
| 68 | struct bsc_nat_barr_entry *this; |
| 69 | this = rb_entry(*new, struct bsc_nat_barr_entry, node); |
| 70 | parent = *new; |
| 71 | |
| 72 | rc = strcmp(entry->imsi, this->imsi); |
| 73 | if (rc < 0) |
| 74 | new = &((*new)->rb_left); |
| 75 | else if (rc > 0) |
| 76 | new = &((*new)->rb_right); |
| 77 | else { |
| 78 | LOGP(DNAT, LOGL_ERROR, |
| 79 | "Duplicate entry for IMSI(%s)\n", entry->imsi); |
| 80 | talloc_free(entry); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | rb_link_node(&entry->node, parent, new); |
| 86 | rb_insert_color(&entry->node, root); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, |
| 91 | const struct osmo_config_list *list) |
| 92 | { |
| 93 | struct osmo_config_entry *cfg_entry; |
| 94 | int err = 0; |
| 95 | |
| 96 | /* free the old data */ |
| 97 | while (!RB_EMPTY_ROOT(root)) { |
| 98 | struct rb_node *node = rb_first(root); |
| 99 | rb_erase(node, root); |
| 100 | talloc_free(node); |
| 101 | } |
| 102 | |
| 103 | if (!list) |
| 104 | return 0; |
| 105 | |
| 106 | /* now adapt the new list */ |
| 107 | llist_for_each_entry(cfg_entry, &list->entry, list) { |
| 108 | struct bsc_nat_barr_entry *entry; |
| 109 | entry = talloc_zero(ctx, struct bsc_nat_barr_entry); |
| 110 | if (!entry) { |
| 111 | LOGP(DNAT, LOGL_ERROR, |
| 112 | "Allocation of the barr entry failed.\n"); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | entry->imsi = talloc_strdup(entry, cfg_entry->mcc); |
| 117 | entry->cm_reject_cause = atoi(cfg_entry->mnc); |
| 118 | entry->lu_reject_cause = atoi(cfg_entry->option); |
| 119 | err |= insert_barr_node(entry, root); |
| 120 | } |
| 121 | |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 126 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string, |
| 127 | int *cm_cause, int *lu_cause) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 128 | { |
| 129 | struct bsc_nat_acc_lst_entry *entry; |
| 130 | |
| 131 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 132 | if (!entry->imsi_deny) |
| 133 | continue; |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 134 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) { |
| 135 | *cm_cause = entry->cm_reject_cause; |
| 136 | *lu_cause = entry->lu_reject_cause; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 137 | return 0; |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 138 | } |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | /* apply white/black list */ |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 145 | static int auth_imsi(struct bsc_connection *bsc, const char *imsi, |
Holger Hans Peter Freyther | 184950e | 2012-12-17 14:51:42 +0100 | [diff] [blame] | 146 | struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 147 | { |
| 148 | /* |
| 149 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 150 | * 1.) Check the global IMSI barr list |
| 151 | * 2.) Allow directly if the IMSI is allowed at the BSC |
| 152 | * 3.) Reject if the IMSI is not allowed at the BSC |
| 153 | * 4.) Reject if the IMSI not allowed at the global level. |
| 154 | * 5.) Allow directly if the IMSI is allowed at the global level |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 155 | */ |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 156 | int cm, lu; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 157 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 158 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
| 159 | |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 160 | /* 1. global check for barred imsis */ |
| 161 | if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { |
| 162 | cause->cm_reject_cause = cm; |
| 163 | cause->lu_reject_cause = lu; |
| 164 | LOGP(DNAT, LOGL_DEBUG, |
| 165 | "Blocking subscriber IMSI %s with CM: %d LU: %d\n", |
| 166 | imsi, cm, lu); |
Holger Hans Peter Freyther | fa1cba9 | 2015-04-05 11:44:47 +0200 | [diff] [blame] | 167 | return -4; |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 171 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 172 | nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); |
| 173 | |
| 174 | |
| 175 | if (bsc_lst) { |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 176 | /* 2. BSC allow */ |
| 177 | if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 178 | return 1; |
| 179 | |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 180 | /* 3. BSC deny */ |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 181 | if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 182 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 183 | "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 184 | rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 185 | cause->cm_reject_cause = cm; |
| 186 | cause->lu_reject_cause = lu; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 187 | return -2; |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 192 | /* 4. NAT deny */ |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 193 | if (nat_lst) { |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 194 | if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 195 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 196 | "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 197 | rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 198 | cause->cm_reject_cause = cm; |
| 199 | cause->lu_reject_cause = lu; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 200 | return -3; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return 1; |
| 205 | } |
| 206 | |
| 207 | static int _cr_check_loc_upd(struct bsc_connection *bsc, |
| 208 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 209 | char **imsi) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 210 | { |
| 211 | uint8_t mi_type; |
| 212 | struct gsm48_loc_upd_req *lu; |
| 213 | char mi_string[GSM48_MI_SIZE]; |
| 214 | |
| 215 | if (length < sizeof(*lu)) { |
| 216 | LOGP(DNAT, LOGL_ERROR, |
| 217 | "LU does not fit. Length is %d \n", length); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | lu = (struct gsm48_loc_upd_req *) data; |
| 222 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 223 | |
| 224 | /* |
| 225 | * We can only deal with the IMSI. This will fail for a phone that |
| 226 | * will send the TMSI of a previous network to us. |
| 227 | */ |
| 228 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 229 | return 0; |
| 230 | |
| 231 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 232 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 233 | return 1; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, |
| 237 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 238 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 239 | { |
| 240 | static const uint32_t classmark_offset = |
| 241 | offsetof(struct gsm48_service_request, classmark); |
| 242 | |
| 243 | char mi_string[GSM48_MI_SIZE]; |
| 244 | uint8_t mi_type; |
| 245 | int rc; |
| 246 | struct gsm48_service_request *req; |
| 247 | |
| 248 | /* unfortunately in Phase1 the classmark2 length is variable */ |
| 249 | |
| 250 | if (length < sizeof(*req)) { |
| 251 | LOGP(DNAT, LOGL_ERROR, |
| 252 | "CM Serv Req does not fit. Length is %d\n", length); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | req = (struct gsm48_service_request *) data; |
| 257 | if (req->cm_service_type == 0x8) |
| 258 | *con_type = NAT_CON_TYPE_SSA; |
| 259 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 260 | length - classmark_offset, mi_string, &mi_type); |
| 261 | if (rc < 0) { |
| 262 | LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | /* we have to let the TMSI or such pass */ |
| 267 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 268 | return 0; |
| 269 | |
| 270 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 271 | return 1; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static int _cr_check_pag_resp(struct bsc_connection *bsc, |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 275 | uint8_t *data, unsigned int length, char **imsi) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 276 | { |
| 277 | struct gsm48_pag_resp *resp; |
| 278 | char mi_string[GSM48_MI_SIZE]; |
| 279 | uint8_t mi_type; |
| 280 | |
| 281 | if (length < sizeof(*resp)) { |
| 282 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | resp = (struct gsm48_pag_resp *) data; |
| 287 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 288 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | /* we need to let it pass for now */ |
| 293 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 294 | return 0; |
| 295 | |
| 296 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 297 | return 1; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 301 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 302 | struct nat_sccp_connection *con, |
Holger Hans Peter Freyther | 184950e | 2012-12-17 14:51:42 +0100 | [diff] [blame] | 303 | struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 304 | { |
| 305 | char mi_string[GSM48_MI_SIZE]; |
| 306 | uint8_t mi_type; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 307 | |
| 308 | if (length < 2) { |
| 309 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | if (data[0] < length - 1) { |
| 314 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 315 | return -2; |
| 316 | } |
| 317 | |
| 318 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 319 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 320 | |
| 321 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 322 | return 0; |
| 323 | |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 324 | con->imsi_checked = 1; |
| 325 | con->imsi = talloc_strdup(con, mi_string); |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 326 | return auth_imsi(bsc, mi_string, cause); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | /* Filter out CR data... */ |
| 331 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 332 | struct bsc_nat_parsed *parsed, int *con_type, |
| 333 | char **imsi, struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 334 | { |
| 335 | struct tlv_parsed tp; |
| 336 | struct gsm48_hdr *hdr48; |
| 337 | int hdr48_len; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 338 | int len, ret = 0; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 339 | uint8_t msg_type, proto; |
| 340 | |
| 341 | *con_type = NAT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 342 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 343 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 344 | *imsi = NULL; |
| 345 | |
| 346 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 347 | LOGP(DNAT, LOGL_ERROR, |
| 348 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | /* the parsed has had some basic l3 length check */ |
| 353 | len = msg->l3h[1]; |
| 354 | if (msgb_l3len(msg) - 3 < len) { |
| 355 | LOGP(DNAT, LOGL_ERROR, |
| 356 | "The CR Data has not enough space...\n"); |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | msg->l4h = &msg->l3h[3]; |
| 361 | len -= 1; |
| 362 | |
| 363 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 364 | |
| 365 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 366 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 371 | |
| 372 | if (hdr48_len < sizeof(*hdr48)) { |
| 373 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 378 | |
| 379 | proto = hdr48->proto_discr & 0x0f; |
| 380 | msg_type = hdr48->msg_type & 0xbf; |
| 381 | if (proto == GSM48_PDISC_MM && |
| 382 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
| 383 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 384 | ret = _cr_check_loc_upd(bsc, &hdr48->data[0], |
| 385 | hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 386 | } else if (proto == GSM48_PDISC_MM && |
| 387 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
| 388 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 389 | ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0], |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 390 | hdr48_len - sizeof(*hdr48), |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 391 | con_type, imsi); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 392 | } else if (proto == GSM48_PDISC_RR && |
| 393 | msg_type == GSM48_MT_RR_PAG_RESP) { |
| 394 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 395 | ret = _cr_check_pag_resp(bsc, &hdr48->data[0], |
| 396 | hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 397 | } else { |
| 398 | /* We only want to filter the above, let other things pass */ |
| 399 | *con_type = NAT_CON_TYPE_OTHER; |
| 400 | return 0; |
| 401 | } |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 402 | |
| 403 | /* check if we are done */ |
| 404 | if (ret != 1) |
| 405 | return ret; |
| 406 | |
| 407 | /* the memory allocation failed */ |
| 408 | if (!*imsi) |
| 409 | return -1; |
| 410 | |
| 411 | /* now check the imsi */ |
| 412 | return auth_imsi(bsc, *imsi, cause); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 416 | struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 417 | struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 418 | { |
| 419 | uint32_t len; |
| 420 | uint8_t msg_type, proto; |
| 421 | struct gsm48_hdr *hdr48; |
| 422 | |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 423 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 424 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 425 | |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 426 | if (con->imsi_checked) |
| 427 | return 0; |
| 428 | |
| 429 | /* only care about DTAP messages */ |
| 430 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 431 | return 0; |
| 432 | |
| 433 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 434 | if (!hdr48) |
| 435 | return -1; |
| 436 | |
| 437 | proto = hdr48->proto_discr & 0x0f; |
| 438 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 439 | if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP) |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 440 | return 0; |
Holger Hans Peter Freyther | 0434fae | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 441 | |
| 442 | return _dt_check_id_resp(bsc, &hdr48->data[0], |
| 443 | len - sizeof(*hdr48), con, cause); |
Holger Hans Peter Freyther | adc2e87 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 444 | } |