blob: d29ea9c1671fa6ed09d68b5412b7d22fdcbf28fd [file] [log] [blame]
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +01001/*
2 * Access filtering
3 */
4/*
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +01005 * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010-2012 by On-Waves
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +01007 * 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
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +010040int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu)
41{
42 struct bsc_nat_barr_entry *n;
43 n = rb_entry(root->rb_node, struct bsc_nat_barr_entry, node);
44
45 while (n) {
46 int rc = strcmp(imsi, n->imsi);
47 if (rc == 0) {
48 *cm = n->cm_reject_cause;
49 *lu = n->lu_reject_cause;
50 return 1;
51 }
52
53 n = rb_entry(
54 (rc < 0) ? n->node.rb_left : n->node.rb_right,
55 struct bsc_nat_barr_entry, node);
56 };
57
58 return 0;
59}
60
61static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *root)
62{
63 struct rb_node **new = &root->rb_node, *parent = NULL;
64
65 while (*new) {
66 int rc;
67 struct bsc_nat_barr_entry *this;
68 this = rb_entry(*new, struct bsc_nat_barr_entry, node);
69 parent = *new;
70
71 rc = strcmp(entry->imsi, this->imsi);
72 if (rc < 0)
73 new = &((*new)->rb_left);
74 else if (rc > 0)
75 new = &((*new)->rb_right);
76 else {
77 LOGP(DNAT, LOGL_ERROR,
78 "Duplicate entry for IMSI(%s)\n", entry->imsi);
79 talloc_free(entry);
80 return -1;
81 }
82 }
83
84 rb_link_node(&entry->node, parent, new);
85 rb_insert_color(&entry->node, root);
86 return 0;
87}
88
89int bsc_nat_barr_adapt(void *ctx, struct rb_root *root,
90 const struct osmo_config_list *list)
91{
92 struct osmo_config_entry *cfg_entry;
93 int err = 0;
94
95 /* free the old data */
96 while (!RB_EMPTY_ROOT(root)) {
97 struct rb_node *node = rb_first(root);
98 rb_erase(node, root);
99 talloc_free(node);
100 }
101
102 if (!list)
103 return 0;
104
105 /* now adapt the new list */
106 llist_for_each_entry(cfg_entry, &list->entry, list) {
107 struct bsc_nat_barr_entry *entry;
108 entry = talloc_zero(ctx, struct bsc_nat_barr_entry);
109 if (!entry) {
110 LOGP(DNAT, LOGL_ERROR,
111 "Allocation of the barr entry failed.\n");
112 continue;
113 }
114
115 entry->imsi = talloc_strdup(entry, cfg_entry->mcc);
116 entry->cm_reject_cause = atoi(cfg_entry->mnc);
117 entry->lu_reject_cause = atoi(cfg_entry->option);
118 err |= insert_barr_node(entry, root);
119 }
120
121 return err;
122}
123
124
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100125static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string,
126 int *cm_cause, int *lu_cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100127{
128 struct bsc_nat_acc_lst_entry *entry;
129
130 llist_for_each_entry(entry, &lst->fltr_list, list) {
131 if (!entry->imsi_deny)
132 continue;
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100133 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) {
134 *cm_cause = entry->cm_reject_cause;
135 *lu_cause = entry->lu_reject_cause;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100136 return 0;
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100137 }
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100138 }
139
140 return 1;
141}
142
143/* apply white/black list */
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100144static int auth_imsi(struct bsc_connection *bsc, const char *imsi,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100145 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100146{
147 /*
148 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100149 * 1.) Check the global IMSI barr list
150 * 2.) Allow directly if the IMSI is allowed at the BSC
151 * 3.) Reject if the IMSI is not allowed at the BSC
152 * 4.) Reject if the IMSI not allowed at the global level.
153 * 5.) Allow directly if the IMSI is allowed at the global level
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100154 */
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100155 int cm, lu;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100156 struct bsc_nat_acc_lst *nat_lst = NULL;
157 struct bsc_nat_acc_lst *bsc_lst = NULL;
158
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100159 /* 1. global check for barred imsis */
160 if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) {
161 cause->cm_reject_cause = cm;
162 cause->lu_reject_cause = lu;
163 LOGP(DNAT, LOGL_DEBUG,
164 "Blocking subscriber IMSI %s with CM: %d LU: %d\n",
165 imsi, cm, lu);
166 return -1;
167 }
168
169
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100170 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
171 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
172
173
174 if (bsc_lst) {
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100175 /* 2. BSC allow */
176 if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100177 return 1;
178
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100179 /* 3. BSC deny */
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100180 if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) {
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100181 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100182 "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100183 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100184 cause->cm_reject_cause = cm;
185 cause->lu_reject_cause = lu;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100186 return -2;
187 }
188
189 }
190
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100191 /* 4. NAT deny */
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100192 if (nat_lst) {
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100193 if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) {
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100194 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100195 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100196 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100197 cause->cm_reject_cause = cm;
198 cause->lu_reject_cause = lu;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100199 return -3;
200 }
201 }
202
203 return 1;
204}
205
206static int _cr_check_loc_upd(struct bsc_connection *bsc,
207 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100208 char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100209{
210 uint8_t mi_type;
211 struct gsm48_loc_upd_req *lu;
212 char mi_string[GSM48_MI_SIZE];
213
214 if (length < sizeof(*lu)) {
215 LOGP(DNAT, LOGL_ERROR,
216 "LU does not fit. Length is %d \n", length);
217 return -1;
218 }
219
220 lu = (struct gsm48_loc_upd_req *) data;
221 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
222
223 /*
224 * We can only deal with the IMSI. This will fail for a phone that
225 * will send the TMSI of a previous network to us.
226 */
227 if (mi_type != GSM_MI_TYPE_IMSI)
228 return 0;
229
230 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
231 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100232 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100233}
234
235static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
236 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100237 int *con_type, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100238{
239 static const uint32_t classmark_offset =
240 offsetof(struct gsm48_service_request, classmark);
241
242 char mi_string[GSM48_MI_SIZE];
243 uint8_t mi_type;
244 int rc;
245 struct gsm48_service_request *req;
246
247 /* unfortunately in Phase1 the classmark2 length is variable */
248
249 if (length < sizeof(*req)) {
250 LOGP(DNAT, LOGL_ERROR,
251 "CM Serv Req does not fit. Length is %d\n", length);
252 return -1;
253 }
254
255 req = (struct gsm48_service_request *) data;
256 if (req->cm_service_type == 0x8)
257 *con_type = NAT_CON_TYPE_SSA;
258 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
259 length - classmark_offset, mi_string, &mi_type);
260 if (rc < 0) {
261 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
262 return -1;
263 }
264
265 /* we have to let the TMSI or such pass */
266 if (mi_type != GSM_MI_TYPE_IMSI)
267 return 0;
268
269 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100270 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100271}
272
273static int _cr_check_pag_resp(struct bsc_connection *bsc,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100274 uint8_t *data, unsigned int length, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100275{
276 struct gsm48_pag_resp *resp;
277 char mi_string[GSM48_MI_SIZE];
278 uint8_t mi_type;
279
280 if (length < sizeof(*resp)) {
281 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
282 return -1;
283 }
284
285 resp = (struct gsm48_pag_resp *) data;
286 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
287 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
288 return -1;
289 }
290
291 /* we need to let it pass for now */
292 if (mi_type != GSM_MI_TYPE_IMSI)
293 return 0;
294
295 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100296 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100297}
298
299static int _dt_check_id_resp(struct bsc_connection *bsc,
300 uint8_t *data, unsigned int length,
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200301 struct nat_sccp_connection *con,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100302 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100303{
304 char mi_string[GSM48_MI_SIZE];
305 uint8_t mi_type;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100306
307 if (length < 2) {
308 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
309 return -1;
310 }
311
312 if (data[0] < length - 1) {
313 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
314 return -2;
315 }
316
317 mi_type = data[1] & GSM_MI_TYPE_MASK;
318 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
319
320 if (mi_type != GSM_MI_TYPE_IMSI)
321 return 0;
322
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100323 con->imsi_checked = 1;
324 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100325 return auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100326}
327
328
329/* Filter out CR data... */
330int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100331 struct bsc_nat_parsed *parsed, int *con_type,
332 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100333{
334 struct tlv_parsed tp;
335 struct gsm48_hdr *hdr48;
336 int hdr48_len;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100337 int len, ret = 0;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100338 uint8_t msg_type, proto;
339
340 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100341 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
342 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100343 *imsi = NULL;
344
345 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
346 LOGP(DNAT, LOGL_ERROR,
347 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
348 return -1;
349 }
350
351 /* the parsed has had some basic l3 length check */
352 len = msg->l3h[1];
353 if (msgb_l3len(msg) - 3 < len) {
354 LOGP(DNAT, LOGL_ERROR,
355 "The CR Data has not enough space...\n");
356 return -1;
357 }
358
359 msg->l4h = &msg->l3h[3];
360 len -= 1;
361
362 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
363
364 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
365 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
366 return -1;
367 }
368
369 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
370
371 if (hdr48_len < sizeof(*hdr48)) {
372 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
373 return -1;
374 }
375
376 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
377
378 proto = hdr48->proto_discr & 0x0f;
379 msg_type = hdr48->msg_type & 0xbf;
380 if (proto == GSM48_PDISC_MM &&
381 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
382 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100383 ret = _cr_check_loc_upd(bsc, &hdr48->data[0],
384 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100385 } else if (proto == GSM48_PDISC_MM &&
386 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
387 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100388 ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0],
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100389 hdr48_len - sizeof(*hdr48),
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100390 con_type, imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100391 } else if (proto == GSM48_PDISC_RR &&
392 msg_type == GSM48_MT_RR_PAG_RESP) {
393 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100394 ret = _cr_check_pag_resp(bsc, &hdr48->data[0],
395 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100396 } else {
397 /* We only want to filter the above, let other things pass */
398 *con_type = NAT_CON_TYPE_OTHER;
399 return 0;
400 }
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100401
402 /* check if we are done */
403 if (ret != 1)
404 return ret;
405
406 /* the memory allocation failed */
407 if (!*imsi)
408 return -1;
409
410 /* now check the imsi */
411 return auth_imsi(bsc, *imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100412}
413
414int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200415 struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100416 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100417{
418 uint32_t len;
419 uint8_t msg_type, proto;
420 struct gsm48_hdr *hdr48;
421
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100422 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
423 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
424
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100425 if (con->imsi_checked)
426 return 0;
427
428 /* only care about DTAP messages */
429 if (parsed->bssap != BSSAP_MSG_DTAP)
430 return 0;
431
432 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
433 if (!hdr48)
434 return -1;
435
436 proto = hdr48->proto_discr & 0x0f;
437 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100438 if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100439 return 0;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100440
441 return _dt_check_id_resp(bsc, &hdr48->data[0],
442 len - sizeof(*hdr48), con, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100443}