blob: 5ab3a971df897856a662c5dd6cbd0014ae1283d1 [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 Freytheradc2e872012-12-17 13:32:53 +0100125static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
126{
127 struct bsc_nat_acc_lst_entry *entry;
128
129 llist_for_each_entry(entry, &lst->fltr_list, list) {
130 if (!entry->imsi_deny)
131 continue;
132 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
133 return 0;
134 }
135
136 return 1;
137}
138
139/* apply white/black list */
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100140static int auth_imsi(struct bsc_connection *bsc, const char *imsi,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100141 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100142{
143 /*
144 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100145 * 1.) Check the global IMSI barr list
146 * 2.) Allow directly if the IMSI is allowed at the BSC
147 * 3.) Reject if the IMSI is not allowed at the BSC
148 * 4.) Reject if the IMSI not allowed at the global level.
149 * 5.) Allow directly if the IMSI is allowed at the global level
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100150 */
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100151 int cm, lu;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100152 struct bsc_nat_acc_lst *nat_lst = NULL;
153 struct bsc_nat_acc_lst *bsc_lst = NULL;
154
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100155 /* 1. global check for barred imsis */
156 if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) {
157 cause->cm_reject_cause = cm;
158 cause->lu_reject_cause = lu;
159 LOGP(DNAT, LOGL_DEBUG,
160 "Blocking subscriber IMSI %s with CM: %d LU: %d\n",
161 imsi, cm, lu);
162 return -1;
163 }
164
165
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100166 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
167 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
168
169
170 if (bsc_lst) {
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100171 /* 2. BSC allow */
172 if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100173 return 1;
174
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100175 /* 3. BSC deny */
176 if (lst_check_deny(bsc_lst, imsi) == 0) {
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100177 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100178 "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100179 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
180 return -2;
181 }
182
183 }
184
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100185 /* 4. NAT deny */
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100186 if (nat_lst) {
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100187 if (lst_check_deny(nat_lst, imsi) == 0) {
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100188 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100189 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100190 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
191 return -3;
192 }
193 }
194
195 return 1;
196}
197
198static int _cr_check_loc_upd(struct bsc_connection *bsc,
199 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100200 char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100201{
202 uint8_t mi_type;
203 struct gsm48_loc_upd_req *lu;
204 char mi_string[GSM48_MI_SIZE];
205
206 if (length < sizeof(*lu)) {
207 LOGP(DNAT, LOGL_ERROR,
208 "LU does not fit. Length is %d \n", length);
209 return -1;
210 }
211
212 lu = (struct gsm48_loc_upd_req *) data;
213 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
214
215 /*
216 * We can only deal with the IMSI. This will fail for a phone that
217 * will send the TMSI of a previous network to us.
218 */
219 if (mi_type != GSM_MI_TYPE_IMSI)
220 return 0;
221
222 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
223 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100224 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100225}
226
227static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
228 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100229 int *con_type, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100230{
231 static const uint32_t classmark_offset =
232 offsetof(struct gsm48_service_request, classmark);
233
234 char mi_string[GSM48_MI_SIZE];
235 uint8_t mi_type;
236 int rc;
237 struct gsm48_service_request *req;
238
239 /* unfortunately in Phase1 the classmark2 length is variable */
240
241 if (length < sizeof(*req)) {
242 LOGP(DNAT, LOGL_ERROR,
243 "CM Serv Req does not fit. Length is %d\n", length);
244 return -1;
245 }
246
247 req = (struct gsm48_service_request *) data;
248 if (req->cm_service_type == 0x8)
249 *con_type = NAT_CON_TYPE_SSA;
250 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
251 length - classmark_offset, mi_string, &mi_type);
252 if (rc < 0) {
253 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
254 return -1;
255 }
256
257 /* we have to let the TMSI or such pass */
258 if (mi_type != GSM_MI_TYPE_IMSI)
259 return 0;
260
261 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100262 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100263}
264
265static int _cr_check_pag_resp(struct bsc_connection *bsc,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100266 uint8_t *data, unsigned int length, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100267{
268 struct gsm48_pag_resp *resp;
269 char mi_string[GSM48_MI_SIZE];
270 uint8_t mi_type;
271
272 if (length < sizeof(*resp)) {
273 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
274 return -1;
275 }
276
277 resp = (struct gsm48_pag_resp *) data;
278 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
279 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
280 return -1;
281 }
282
283 /* we need to let it pass for now */
284 if (mi_type != GSM_MI_TYPE_IMSI)
285 return 0;
286
287 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100288 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100289}
290
291static int _dt_check_id_resp(struct bsc_connection *bsc,
292 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100293 struct sccp_connections *con,
294 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100295{
296 char mi_string[GSM48_MI_SIZE];
297 uint8_t mi_type;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100298
299 if (length < 2) {
300 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
301 return -1;
302 }
303
304 if (data[0] < length - 1) {
305 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
306 return -2;
307 }
308
309 mi_type = data[1] & GSM_MI_TYPE_MASK;
310 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
311
312 if (mi_type != GSM_MI_TYPE_IMSI)
313 return 0;
314
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100315 con->imsi_checked = 1;
316 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100317 return auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100318}
319
320
321/* Filter out CR data... */
322int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100323 struct bsc_nat_parsed *parsed, int *con_type,
324 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100325{
326 struct tlv_parsed tp;
327 struct gsm48_hdr *hdr48;
328 int hdr48_len;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100329 int len, ret = 0;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100330 uint8_t msg_type, proto;
331
332 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100333 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
334 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100335 *imsi = NULL;
336
337 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
338 LOGP(DNAT, LOGL_ERROR,
339 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
340 return -1;
341 }
342
343 /* the parsed has had some basic l3 length check */
344 len = msg->l3h[1];
345 if (msgb_l3len(msg) - 3 < len) {
346 LOGP(DNAT, LOGL_ERROR,
347 "The CR Data has not enough space...\n");
348 return -1;
349 }
350
351 msg->l4h = &msg->l3h[3];
352 len -= 1;
353
354 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
355
356 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
357 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
358 return -1;
359 }
360
361 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
362
363 if (hdr48_len < sizeof(*hdr48)) {
364 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
365 return -1;
366 }
367
368 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
369
370 proto = hdr48->proto_discr & 0x0f;
371 msg_type = hdr48->msg_type & 0xbf;
372 if (proto == GSM48_PDISC_MM &&
373 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
374 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100375 ret = _cr_check_loc_upd(bsc, &hdr48->data[0],
376 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100377 } else if (proto == GSM48_PDISC_MM &&
378 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
379 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100380 ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0],
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100381 hdr48_len - sizeof(*hdr48),
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100382 con_type, imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100383 } else if (proto == GSM48_PDISC_RR &&
384 msg_type == GSM48_MT_RR_PAG_RESP) {
385 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100386 ret = _cr_check_pag_resp(bsc, &hdr48->data[0],
387 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100388 } else {
389 /* We only want to filter the above, let other things pass */
390 *con_type = NAT_CON_TYPE_OTHER;
391 return 0;
392 }
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100393
394 /* check if we are done */
395 if (ret != 1)
396 return ret;
397
398 /* the memory allocation failed */
399 if (!*imsi)
400 return -1;
401
402 /* now check the imsi */
403 return auth_imsi(bsc, *imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100404}
405
406int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100407 struct sccp_connections *con, struct bsc_nat_parsed *parsed,
408 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100409{
410 uint32_t len;
411 uint8_t msg_type, proto;
412 struct gsm48_hdr *hdr48;
413
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100414 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
415 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
416
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100417 if (con->imsi_checked)
418 return 0;
419
420 /* only care about DTAP messages */
421 if (parsed->bssap != BSSAP_MSG_DTAP)
422 return 0;
423
424 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
425 if (!hdr48)
426 return -1;
427
428 proto = hdr48->proto_discr & 0x0f;
429 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100430 if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100431 return 0;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100432
433 return _dt_check_id_resp(bsc, &hdr48->data[0],
434 len - sizeof(*hdr48), con, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100435}