blob: f290e2d524fd46905a32d8ca65b2bc28fda124cf [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 Freyther184950e2012-12-17 14:51:42 +0100102 char **imsi, struct bsc_nat_reject_cause *cause)
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 Freyther184950e2012-12-17 14:51:42 +0100126 return auth_imsi(bsc, mi_string, cause);
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 Freyther184950e2012-12-17 14:51:42 +0100131 int *con_type, char **imsi,
132 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100133{
134 static const uint32_t classmark_offset =
135 offsetof(struct gsm48_service_request, classmark);
136
137 char mi_string[GSM48_MI_SIZE];
138 uint8_t mi_type;
139 int rc;
140 struct gsm48_service_request *req;
141
142 /* unfortunately in Phase1 the classmark2 length is variable */
143
144 if (length < sizeof(*req)) {
145 LOGP(DNAT, LOGL_ERROR,
146 "CM Serv Req does not fit. Length is %d\n", length);
147 return -1;
148 }
149
150 req = (struct gsm48_service_request *) data;
151 if (req->cm_service_type == 0x8)
152 *con_type = NAT_CON_TYPE_SSA;
153 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
154 length - classmark_offset, mi_string, &mi_type);
155 if (rc < 0) {
156 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
157 return -1;
158 }
159
160 /* we have to let the TMSI or such pass */
161 if (mi_type != GSM_MI_TYPE_IMSI)
162 return 0;
163
164 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100165 return auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100166}
167
168static int _cr_check_pag_resp(struct bsc_connection *bsc,
169 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100170 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100171{
172 struct gsm48_pag_resp *resp;
173 char mi_string[GSM48_MI_SIZE];
174 uint8_t mi_type;
175
176 if (length < sizeof(*resp)) {
177 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
178 return -1;
179 }
180
181 resp = (struct gsm48_pag_resp *) data;
182 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
183 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
184 return -1;
185 }
186
187 /* we need to let it pass for now */
188 if (mi_type != GSM_MI_TYPE_IMSI)
189 return 0;
190
191 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100192 return auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100193}
194
195static int _dt_check_id_resp(struct bsc_connection *bsc,
196 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100197 struct sccp_connections *con,
198 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100199{
200 char mi_string[GSM48_MI_SIZE];
201 uint8_t mi_type;
202 int ret;
203
204 if (length < 2) {
205 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
206 return -1;
207 }
208
209 if (data[0] < length - 1) {
210 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
211 return -2;
212 }
213
214 mi_type = data[1] & GSM_MI_TYPE_MASK;
215 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
216
217 if (mi_type != GSM_MI_TYPE_IMSI)
218 return 0;
219
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100220 ret = auth_imsi(bsc, mi_string, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100221 con->imsi_checked = 1;
222 con->imsi = talloc_strdup(con, mi_string);
223 return ret;
224}
225
226
227/* Filter out CR data... */
228int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100229 struct bsc_nat_parsed *parsed, int *con_type,
230 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100231{
232 struct tlv_parsed tp;
233 struct gsm48_hdr *hdr48;
234 int hdr48_len;
235 int len;
236 uint8_t msg_type, proto;
237
238 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100239 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
240 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100241 *imsi = NULL;
242
243 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
244 LOGP(DNAT, LOGL_ERROR,
245 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
246 return -1;
247 }
248
249 /* the parsed has had some basic l3 length check */
250 len = msg->l3h[1];
251 if (msgb_l3len(msg) - 3 < len) {
252 LOGP(DNAT, LOGL_ERROR,
253 "The CR Data has not enough space...\n");
254 return -1;
255 }
256
257 msg->l4h = &msg->l3h[3];
258 len -= 1;
259
260 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
261
262 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
263 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
264 return -1;
265 }
266
267 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
268
269 if (hdr48_len < sizeof(*hdr48)) {
270 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
271 return -1;
272 }
273
274 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
275
276 proto = hdr48->proto_discr & 0x0f;
277 msg_type = hdr48->msg_type & 0xbf;
278 if (proto == GSM48_PDISC_MM &&
279 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
280 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100281 return _cr_check_loc_upd(bsc, &hdr48->data[0],
282 hdr48_len - sizeof(*hdr48), imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100283 } else if (proto == GSM48_PDISC_MM &&
284 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
285 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
286 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
287 hdr48_len - sizeof(*hdr48),
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100288 con_type, imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100289 } else if (proto == GSM48_PDISC_RR &&
290 msg_type == GSM48_MT_RR_PAG_RESP) {
291 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100292 return _cr_check_pag_resp(bsc, &hdr48->data[0],
293 hdr48_len - sizeof(*hdr48), imsi, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100294 } else {
295 /* We only want to filter the above, let other things pass */
296 *con_type = NAT_CON_TYPE_OTHER;
297 return 0;
298 }
299}
300
301int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100302 struct sccp_connections *con, struct bsc_nat_parsed *parsed,
303 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100304{
305 uint32_t len;
306 uint8_t msg_type, proto;
307 struct gsm48_hdr *hdr48;
308
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100309 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
310 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
311
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100312 if (con->imsi_checked)
313 return 0;
314
315 /* only care about DTAP messages */
316 if (parsed->bssap != BSSAP_MSG_DTAP)
317 return 0;
318
319 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
320 if (!hdr48)
321 return -1;
322
323 proto = hdr48->proto_discr & 0x0f;
324 msg_type = hdr48->msg_type & 0xbf;
325 if (proto == GSM48_PDISC_MM &&
326 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther184950e2012-12-17 14:51:42 +0100327 return _dt_check_id_resp(bsc, &hdr48->data[0],
328 len - sizeof(*hdr48), con, cause);
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100329 } else {
330 return 0;
331 }
332}