Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Access filtering |
| 3 | */ |
| 4 | /* |
Holger Hans Peter Freyther | d25039a | 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 | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 6 | * (C) 2010-2012 by On-Waves |
Holger Hans Peter Freyther | a673ce7 | 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 | d25039a | 2015-04-04 20:47:03 +0200 | [diff] [blame] | 24 | #include <openbsc/bsc_msg_filter.h> |
| 25 | |
Holger Hans Peter Freyther | a673ce7 | 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 | a673ce7 | 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 | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 41 | int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 42 | { |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 43 | struct bsc_filter_barr_entry *n; |
| 44 | n = rb_entry(root->rb_node, struct bsc_filter_barr_entry, node); |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 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, |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 56 | struct bsc_filter_barr_entry, node); |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 62 | static int insert_barr_node(struct bsc_filter_barr_entry *entry, struct rb_root *root) |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 63 | { |
| 64 | struct rb_node **new = &root->rb_node, *parent = NULL; |
| 65 | |
| 66 | while (*new) { |
| 67 | int rc; |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 68 | struct bsc_filter_barr_entry *this; |
| 69 | this = rb_entry(*new, struct bsc_filter_barr_entry, node); |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 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 { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 78 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 90 | int bsc_filter_barr_adapt(void *ctx, struct rb_root *root, |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 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) { |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 108 | struct bsc_filter_barr_entry *entry; |
| 109 | entry = talloc_zero(ctx, struct bsc_filter_barr_entry); |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 110 | if (!entry) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 111 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 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 | 955f53c | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 126 | static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 127 | int *cm_cause, int *lu_cause) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 128 | { |
Holger Hans Peter Freyther | 955f53c | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 129 | struct bsc_msg_acc_lst_entry *entry; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 130 | |
| 131 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 132 | if (!entry->imsi_deny) |
| 133 | continue; |
Holger Hans Peter Freyther | 802cb5e | 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 | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 137 | return 0; |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 138 | } |
Holger Hans Peter Freyther | a673ce7 | 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 | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 145 | static int auth_imsi(struct bsc_filter_request *req, |
Holger Hans Peter Freyther | 0a6ddc8 | 2015-04-05 18:48:27 +0200 | [diff] [blame] | 146 | const char *imsi, |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 147 | struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 148 | { |
| 149 | /* |
| 150 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 151 | * 1.) Check the global IMSI barr list |
| 152 | * 2.) Allow directly if the IMSI is allowed at the BSC |
| 153 | * 3.) Reject if the IMSI is not allowed at the BSC |
| 154 | * 4.) Reject if the IMSI not allowed at the global level. |
| 155 | * 5.) Allow directly if the IMSI is allowed at the global level |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 156 | */ |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 157 | int cm, lu; |
Holger Hans Peter Freyther | 955f53c | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 158 | struct bsc_msg_acc_lst *nat_lst = NULL; |
| 159 | struct bsc_msg_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 160 | |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 161 | /* 1. global check for barred imsis */ |
Holger Hans Peter Freyther | 48f9a4e | 2015-04-05 20:53:42 +0200 | [diff] [blame^] | 162 | if (req->black_list && bsc_filter_barr_find(req->black_list, imsi, &cm, &lu)) { |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 163 | cause->cm_reject_cause = cm; |
| 164 | cause->lu_reject_cause = lu; |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 165 | LOGP(DFILTER, LOGL_DEBUG, |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 166 | "Blocking subscriber IMSI %s with CM: %d LU: %d\n", |
| 167 | imsi, cm, lu); |
Holger Hans Peter Freyther | cb13bb3 | 2015-04-05 11:44:47 +0200 | [diff] [blame] | 168 | return -4; |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Holger Hans Peter Freyther | 0a6ddc8 | 2015-04-05 18:48:27 +0200 | [diff] [blame] | 172 | bsc_lst = bsc_msg_acc_lst_find(req->access_lists, req->local_lst_name); |
| 173 | nat_lst = bsc_msg_acc_lst_find(req->access_lists, req->global_lst_name); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 174 | |
| 175 | |
| 176 | if (bsc_lst) { |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 177 | /* 2. BSC allow */ |
Holger Hans Peter Freyther | 955f53c | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 178 | if (bsc_msg_acc_lst_check_allow(bsc_lst, imsi) == 0) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 179 | return 1; |
| 180 | |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 181 | /* 3. BSC deny */ |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 182 | if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 183 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | 0a6ddc8 | 2015-04-05 18:48:27 +0200 | [diff] [blame] | 184 | "Filtering %s by imsi_deny on config nr: %d.\n", imsi, req->bsc_nr); |
Holger Hans Peter Freyther | 80c7e25 | 2015-04-05 16:50:34 +0200 | [diff] [blame] | 185 | rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_LOCAL_FILTER]); |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 186 | cause->cm_reject_cause = cm; |
| 187 | cause->lu_reject_cause = lu; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 188 | return -2; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 193 | /* 4. NAT deny */ |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 194 | if (nat_lst) { |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 195 | if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 196 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | 0a6ddc8 | 2015-04-05 18:48:27 +0200 | [diff] [blame] | 197 | "Filtering %s global imsi_deny on bsc nr: %d.\n", imsi, req->bsc_nr); |
Holger Hans Peter Freyther | 80c7e25 | 2015-04-05 16:50:34 +0200 | [diff] [blame] | 198 | rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_GLOBAL_FILTER]); |
Holger Hans Peter Freyther | 802cb5e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 199 | cause->cm_reject_cause = cm; |
| 200 | cause->lu_reject_cause = lu; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 201 | return -3; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return 1; |
| 206 | } |
| 207 | |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 208 | static int _cr_check_loc_upd(void *ctx, |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 209 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 210 | char **imsi) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 211 | { |
| 212 | uint8_t mi_type; |
| 213 | struct gsm48_loc_upd_req *lu; |
| 214 | char mi_string[GSM48_MI_SIZE]; |
| 215 | |
| 216 | if (length < sizeof(*lu)) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 217 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 218 | "LU does not fit. Length is %d \n", length); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | lu = (struct gsm48_loc_upd_req *) data; |
| 223 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 224 | |
| 225 | /* |
| 226 | * We can only deal with the IMSI. This will fail for a phone that |
| 227 | * will send the TMSI of a previous network to us. |
| 228 | */ |
| 229 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 230 | return 0; |
| 231 | |
| 232 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 233 | *imsi = talloc_strdup(ctx, mi_string); |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 234 | return 1; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 237 | static int _cr_check_cm_serv_req(void *ctx, |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 238 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 239 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 240 | { |
| 241 | static const uint32_t classmark_offset = |
| 242 | offsetof(struct gsm48_service_request, classmark); |
| 243 | |
| 244 | char mi_string[GSM48_MI_SIZE]; |
| 245 | uint8_t mi_type; |
| 246 | int rc; |
| 247 | struct gsm48_service_request *req; |
| 248 | |
| 249 | /* unfortunately in Phase1 the classmark2 length is variable */ |
| 250 | |
| 251 | if (length < sizeof(*req)) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 252 | LOGP(DFILTER, LOGL_ERROR, |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 253 | "CM Serv Req does not fit. Length is %d\n", length); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | req = (struct gsm48_service_request *) data; |
| 258 | if (req->cm_service_type == 0x8) |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 259 | *con_type = FLT_CON_TYPE_SSA; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 260 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 261 | length - classmark_offset, mi_string, &mi_type); |
| 262 | if (rc < 0) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 263 | LOGP(DFILTER, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | /* we have to let the TMSI or such pass */ |
| 268 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 269 | return 0; |
| 270 | |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 271 | *imsi = talloc_strdup(ctx, mi_string); |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 272 | return 1; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 275 | static int _cr_check_pag_resp(void *ctx, |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 276 | uint8_t *data, unsigned int length, char **imsi) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 277 | { |
| 278 | struct gsm48_pag_resp *resp; |
| 279 | char mi_string[GSM48_MI_SIZE]; |
| 280 | uint8_t mi_type; |
| 281 | |
| 282 | if (length < sizeof(*resp)) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 283 | LOGP(DFILTER, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | resp = (struct gsm48_pag_resp *) data; |
| 288 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 289 | LOGP(DFILTER, LOGL_ERROR, "Failed to extract the MI.\n"); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | /* we need to let it pass for now */ |
| 294 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 295 | return 0; |
| 296 | |
Holger Hans Peter Freyther | d4fedc5 | 2015-04-05 18:58:55 +0200 | [diff] [blame] | 297 | *imsi = talloc_strdup(ctx, mi_string); |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 298 | return 1; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 301 | static int _dt_check_id_resp(struct bsc_filter_request *req, |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 302 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | d308480 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 303 | struct bsc_filter_state *state, |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 304 | struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 305 | { |
| 306 | char mi_string[GSM48_MI_SIZE]; |
| 307 | uint8_t mi_type; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 308 | |
| 309 | if (length < 2) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 310 | LOGP(DFILTER, LOGL_ERROR, "mi does not fit.\n"); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | if (data[0] < length - 1) { |
Holger Hans Peter Freyther | b0023f5 | 2015-04-05 19:34:12 +0200 | [diff] [blame] | 315 | LOGP(DFILTER, LOGL_ERROR, "mi length too big.\n"); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 316 | return -2; |
| 317 | } |
| 318 | |
| 319 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 320 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 321 | |
| 322 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 323 | return 0; |
| 324 | |
Holger Hans Peter Freyther | d308480 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 325 | state->imsi_checked = 1; |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 326 | state->imsi = talloc_strdup(req->ctx, mi_string); |
Holger Hans Peter Freyther | 0a6ddc8 | 2015-04-05 18:48:27 +0200 | [diff] [blame] | 327 | return auth_imsi(req, mi_string, cause); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
| 331 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | a6f229b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 332 | int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 333 | struct bsc_filter_request *req, |
Holger Hans Peter Freyther | a6f229b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 334 | int *con_type, |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 335 | char **imsi, struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 336 | { |
Holger Hans Peter Freyther | a6f229b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 337 | int ret = 0; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 338 | uint8_t msg_type, proto; |
| 339 | |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 340 | *con_type = FLT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 341 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 342 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 343 | *imsi = NULL; |
| 344 | |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 345 | proto = hdr48->proto_discr & 0x0f; |
| 346 | msg_type = hdr48->msg_type & 0xbf; |
| 347 | if (proto == GSM48_PDISC_MM && |
| 348 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 349 | *con_type = FLT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 350 | ret = _cr_check_loc_upd(req->ctx, &hdr48->data[0], |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 351 | hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 352 | } else if (proto == GSM48_PDISC_MM && |
| 353 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 354 | *con_type = FLT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 355 | ret = _cr_check_cm_serv_req(req->ctx, &hdr48->data[0], |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 356 | hdr48_len - sizeof(*hdr48), |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 357 | con_type, imsi); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 358 | } else if (proto == GSM48_PDISC_RR && |
| 359 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 360 | *con_type = FLT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 361 | ret = _cr_check_pag_resp(req->ctx, &hdr48->data[0], |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 362 | hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 363 | } else { |
| 364 | /* We only want to filter the above, let other things pass */ |
Holger Hans Peter Freyther | e192c7b | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 365 | *con_type = FLT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 366 | return 0; |
| 367 | } |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 368 | |
| 369 | /* check if we are done */ |
| 370 | if (ret != 1) |
| 371 | return ret; |
| 372 | |
| 373 | /* the memory allocation failed */ |
| 374 | if (!*imsi) |
| 375 | return -1; |
| 376 | |
| 377 | /* now check the imsi */ |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 378 | return auth_imsi(req, *imsi, cause); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Holger Hans Peter Freyther | a6f229b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 381 | int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 382 | struct bsc_filter_request *req, |
Holger Hans Peter Freyther | d308480 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 383 | struct bsc_filter_state *state, |
Holger Hans Peter Freyther | cc0d656 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 384 | struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 385 | { |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 386 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 387 | |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 388 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 389 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 390 | |
Holger Hans Peter Freyther | d308480 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 391 | if (state->imsi_checked) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 392 | return 0; |
| 393 | |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 394 | proto = hdr48->proto_discr & 0x0f; |
| 395 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 396 | if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP) |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 397 | return 0; |
Holger Hans Peter Freyther | 073e1f2 | 2012-12-17 15:22:47 +0100 | [diff] [blame] | 398 | |
Holger Hans Peter Freyther | 99cc91e | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 399 | return _dt_check_id_resp(req, &hdr48->data[0], |
Holger Hans Peter Freyther | d308480 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 400 | len - sizeof(*hdr48), state, cause); |
Holger Hans Peter Freyther | a673ce7 | 2012-12-17 13:32:53 +0100 | [diff] [blame] | 401 | } |