blob: e3e63d13d3ea47d60ebf1a95de33773c3bcbfca4 [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
40static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
41{
42 struct bsc_nat_acc_lst_entry *entry;
43
44 llist_for_each_entry(entry, &lst->fltr_list, list) {
45 if (!entry->imsi_deny)
46 continue;
47 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
48 return 0;
49 }
50
51 return 1;
52}
53
54/* apply white/black list */
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +010055static int auth_imsi(struct bsc_connection *bsc, const char *mi_string,
56 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +010057{
58 /*
59 * Now apply blacklist/whitelist of the BSC and the NAT.
60 * 1.) Allow directly if the IMSI is allowed at the BSC
61 * 2.) Reject if the IMSI is not allowed at the BSC
62 * 3.) Reject if the IMSI not allowed at the global level.
63 * 4.) Allow directly if the IMSI is allowed at the global level
64 */
65 struct bsc_nat_acc_lst *nat_lst = NULL;
66 struct bsc_nat_acc_lst *bsc_lst = NULL;
67
68 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
69 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
70
71
72 if (bsc_lst) {
73 /* 1. BSC allow */
74 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
75 return 1;
76
77 /* 2. BSC deny */
78 if (lst_check_deny(bsc_lst, mi_string) == 0) {
79 LOGP(DNAT, LOGL_ERROR,
80 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
81 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
82 return -2;
83 }
84
85 }
86
87 /* 3. NAT deny */
88 if (nat_lst) {
89 if (lst_check_deny(nat_lst, mi_string) == 0) {
90 LOGP(DNAT, LOGL_ERROR,
91 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
92 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
93 return -3;
94 }
95 }
96
97 return 1;
98}
99
100static int _cr_check_loc_upd(struct bsc_connection *bsc,
101 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100102 char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100103{
104 uint8_t mi_type;
105 struct gsm48_loc_upd_req *lu;
106 char mi_string[GSM48_MI_SIZE];
107
108 if (length < sizeof(*lu)) {
109 LOGP(DNAT, LOGL_ERROR,
110 "LU does not fit. Length is %d \n", length);
111 return -1;
112 }
113
114 lu = (struct gsm48_loc_upd_req *) data;
115 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
116
117 /*
118 * We can only deal with the IMSI. This will fail for a phone that
119 * will send the TMSI of a previous network to us.
120 */
121 if (mi_type != GSM_MI_TYPE_IMSI)
122 return 0;
123
124 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
125 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100126 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100127}
128
129static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
130 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100131 int *con_type, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100132{
133 static const uint32_t classmark_offset =
134 offsetof(struct gsm48_service_request, classmark);
135
136 char mi_string[GSM48_MI_SIZE];
137 uint8_t mi_type;
138 int rc;
139 struct gsm48_service_request *req;
140
141 /* unfortunately in Phase1 the classmark2 length is variable */
142
143 if (length < sizeof(*req)) {
144 LOGP(DNAT, LOGL_ERROR,
145 "CM Serv Req does not fit. Length is %d\n", length);
146 return -1;
147 }
148
149 req = (struct gsm48_service_request *) data;
150 if (req->cm_service_type == 0x8)
151 *con_type = NAT_CON_TYPE_SSA;
152 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
153 length - classmark_offset, mi_string, &mi_type);
154 if (rc < 0) {
155 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
156 return -1;
157 }
158
159 /* we have to let the TMSI or such pass */
160 if (mi_type != GSM_MI_TYPE_IMSI)
161 return 0;
162
163 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100164 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100165}
166
167static int _cr_check_pag_resp(struct bsc_connection *bsc,
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100168 uint8_t *data, unsigned int length, char **imsi)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100169{
170 struct gsm48_pag_resp *resp;
171 char mi_string[GSM48_MI_SIZE];
172 uint8_t mi_type;
173
174 if (length < sizeof(*resp)) {
175 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
176 return -1;
177 }
178
179 resp = (struct gsm48_pag_resp *) data;
180 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
181 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
182 return -1;
183 }
184
185 /* we need to let it pass for now */
186 if (mi_type != GSM_MI_TYPE_IMSI)
187 return 0;
188
189 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100190 return 1;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100191}
192
193static int _dt_check_id_resp(struct bsc_connection *bsc,
194 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100195 struct sccp_connections *con,
196 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100197{
198 char mi_string[GSM48_MI_SIZE];
199 uint8_t mi_type;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100200
201 if (length < 2) {
202 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
203 return -1;
204 }
205
206 if (data[0] < length - 1) {
207 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
208 return -2;
209 }
210
211 mi_type = data[1] & GSM_MI_TYPE_MASK;
212 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
213
214 if (mi_type != GSM_MI_TYPE_IMSI)
215 return 0;
216
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100217 con->imsi_checked = 1;
218 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100219 return auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100220}
221
222
223/* Filter out CR data... */
224int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100225 struct bsc_nat_parsed *parsed, int *con_type,
226 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100227{
228 struct tlv_parsed tp;
229 struct gsm48_hdr *hdr48;
230 int hdr48_len;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100231 int len, ret = 0;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100232 uint8_t msg_type, proto;
233
234 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100235 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
236 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100237 *imsi = NULL;
238
239 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
240 LOGP(DNAT, LOGL_ERROR,
241 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
242 return -1;
243 }
244
245 /* the parsed has had some basic l3 length check */
246 len = msg->l3h[1];
247 if (msgb_l3len(msg) - 3 < len) {
248 LOGP(DNAT, LOGL_ERROR,
249 "The CR Data has not enough space...\n");
250 return -1;
251 }
252
253 msg->l4h = &msg->l3h[3];
254 len -= 1;
255
256 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
257
258 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
259 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
260 return -1;
261 }
262
263 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
264
265 if (hdr48_len < sizeof(*hdr48)) {
266 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
267 return -1;
268 }
269
270 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
271
272 proto = hdr48->proto_discr & 0x0f;
273 msg_type = hdr48->msg_type & 0xbf;
274 if (proto == GSM48_PDISC_MM &&
275 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
276 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100277 ret = _cr_check_loc_upd(bsc, &hdr48->data[0],
278 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100279 } else if (proto == GSM48_PDISC_MM &&
280 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
281 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100282 ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0],
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100283 hdr48_len - sizeof(*hdr48),
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100284 con_type, imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100285 } else if (proto == GSM48_PDISC_RR &&
286 msg_type == GSM48_MT_RR_PAG_RESP) {
287 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100288 ret = _cr_check_pag_resp(bsc, &hdr48->data[0],
289 hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100290 } else {
291 /* We only want to filter the above, let other things pass */
292 *con_type = NAT_CON_TYPE_OTHER;
293 return 0;
294 }
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100295
296 /* check if we are done */
297 if (ret != 1)
298 return ret;
299
300 /* the memory allocation failed */
301 if (!*imsi)
302 return -1;
303
304 /* now check the imsi */
305 return auth_imsi(bsc, *imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100306}
307
308int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100309 struct sccp_connections *con, struct bsc_nat_parsed *parsed,
310 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100311{
312 uint32_t len;
313 uint8_t msg_type, proto;
314 struct gsm48_hdr *hdr48;
315
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100316 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
317 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
318
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100319 if (con->imsi_checked)
320 return 0;
321
322 /* only care about DTAP messages */
323 if (parsed->bssap != BSSAP_MSG_DTAP)
324 return 0;
325
326 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
327 if (!hdr48)
328 return -1;
329
330 proto = hdr48->proto_discr & 0x0f;
331 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100332 if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100333 return 0;
Holger Hans Peter Freyther0434fae2012-12-17 15:22:47 +0100334
335 return _dt_check_id_resp(bsc, &hdr48->data[0],
336 len - sizeof(*hdr48), con, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100337}