blob: 15afb9a4ab6711042849102f3bbde082bb990958 [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 */
55static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
56{
57 /*
58 * Now apply blacklist/whitelist of the BSC and the NAT.
59 * 1.) Allow directly if the IMSI is allowed at the BSC
60 * 2.) Reject if the IMSI is not allowed at the BSC
61 * 3.) Reject if the IMSI not allowed at the global level.
62 * 4.) Allow directly if the IMSI is allowed at the global level
63 */
64 struct bsc_nat_acc_lst *nat_lst = NULL;
65 struct bsc_nat_acc_lst *bsc_lst = NULL;
66
67 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
68 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
69
70
71 if (bsc_lst) {
72 /* 1. BSC allow */
73 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
74 return 1;
75
76 /* 2. BSC deny */
77 if (lst_check_deny(bsc_lst, mi_string) == 0) {
78 LOGP(DNAT, LOGL_ERROR,
79 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
80 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
81 return -2;
82 }
83
84 }
85
86 /* 3. NAT deny */
87 if (nat_lst) {
88 if (lst_check_deny(nat_lst, mi_string) == 0) {
89 LOGP(DNAT, LOGL_ERROR,
90 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
91 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
92 return -3;
93 }
94 }
95
96 return 1;
97}
98
99static int _cr_check_loc_upd(struct bsc_connection *bsc,
100 uint8_t *data, unsigned int length,
101 char **imsi)
102{
103 uint8_t mi_type;
104 struct gsm48_loc_upd_req *lu;
105 char mi_string[GSM48_MI_SIZE];
106
107 if (length < sizeof(*lu)) {
108 LOGP(DNAT, LOGL_ERROR,
109 "LU does not fit. Length is %d \n", length);
110 return -1;
111 }
112
113 lu = (struct gsm48_loc_upd_req *) data;
114 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
115
116 /*
117 * We can only deal with the IMSI. This will fail for a phone that
118 * will send the TMSI of a previous network to us.
119 */
120 if (mi_type != GSM_MI_TYPE_IMSI)
121 return 0;
122
123 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
124 *imsi = talloc_strdup(bsc, mi_string);
125 return auth_imsi(bsc, mi_string);
126}
127
128static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
129 uint8_t *data, unsigned int length,
130 int *con_type, char **imsi)
131{
132 static const uint32_t classmark_offset =
133 offsetof(struct gsm48_service_request, classmark);
134
135 char mi_string[GSM48_MI_SIZE];
136 uint8_t mi_type;
137 int rc;
138 struct gsm48_service_request *req;
139
140 /* unfortunately in Phase1 the classmark2 length is variable */
141
142 if (length < sizeof(*req)) {
143 LOGP(DNAT, LOGL_ERROR,
144 "CM Serv Req does not fit. Length is %d\n", length);
145 return -1;
146 }
147
148 req = (struct gsm48_service_request *) data;
149 if (req->cm_service_type == 0x8)
150 *con_type = NAT_CON_TYPE_SSA;
151 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
152 length - classmark_offset, mi_string, &mi_type);
153 if (rc < 0) {
154 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
155 return -1;
156 }
157
158 /* we have to let the TMSI or such pass */
159 if (mi_type != GSM_MI_TYPE_IMSI)
160 return 0;
161
162 *imsi = talloc_strdup(bsc, mi_string);
163 return auth_imsi(bsc, mi_string);
164}
165
166static int _cr_check_pag_resp(struct bsc_connection *bsc,
167 uint8_t *data, unsigned int length,
168 char **imsi)
169{
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);
190 return auth_imsi(bsc, mi_string);
191}
192
193static int _dt_check_id_resp(struct bsc_connection *bsc,
194 uint8_t *data, unsigned int length,
195 struct sccp_connections *con)
196{
197 char mi_string[GSM48_MI_SIZE];
198 uint8_t mi_type;
199 int ret;
200
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
217 ret = auth_imsi(bsc, mi_string);
218 con->imsi_checked = 1;
219 con->imsi = talloc_strdup(con, mi_string);
220 return ret;
221}
222
223
224/* Filter out CR data... */
225int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100226 struct bsc_nat_parsed *parsed, int *con_type,
227 char **imsi, struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100228{
229 struct tlv_parsed tp;
230 struct gsm48_hdr *hdr48;
231 int hdr48_len;
232 int len;
233 uint8_t msg_type, proto;
234
235 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100236 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
237 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100238 *imsi = NULL;
239
240 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
241 LOGP(DNAT, LOGL_ERROR,
242 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
243 return -1;
244 }
245
246 /* the parsed has had some basic l3 length check */
247 len = msg->l3h[1];
248 if (msgb_l3len(msg) - 3 < len) {
249 LOGP(DNAT, LOGL_ERROR,
250 "The CR Data has not enough space...\n");
251 return -1;
252 }
253
254 msg->l4h = &msg->l3h[3];
255 len -= 1;
256
257 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
258
259 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
260 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
261 return -1;
262 }
263
264 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
265
266 if (hdr48_len < sizeof(*hdr48)) {
267 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
268 return -1;
269 }
270
271 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
272
273 proto = hdr48->proto_discr & 0x0f;
274 msg_type = hdr48->msg_type & 0xbf;
275 if (proto == GSM48_PDISC_MM &&
276 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
277 *con_type = NAT_CON_TYPE_LU;
278 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
279 } else if (proto == GSM48_PDISC_MM &&
280 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
281 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
282 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
283 hdr48_len - sizeof(*hdr48),
284 con_type, imsi);
285 } else if (proto == GSM48_PDISC_RR &&
286 msg_type == GSM48_MT_RR_PAG_RESP) {
287 *con_type = NAT_CON_TYPE_PAG_RESP;
288 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
289 } else {
290 /* We only want to filter the above, let other things pass */
291 *con_type = NAT_CON_TYPE_OTHER;
292 return 0;
293 }
294}
295
296int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100297 struct sccp_connections *con, struct bsc_nat_parsed *parsed,
298 struct bsc_nat_reject_cause *cause)
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100299{
300 uint32_t len;
301 uint8_t msg_type, proto;
302 struct gsm48_hdr *hdr48;
303
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100304 cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
305 cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
306
Holger Hans Peter Freytheradc2e872012-12-17 13:32:53 +0100307 if (con->imsi_checked)
308 return 0;
309
310 /* only care about DTAP messages */
311 if (parsed->bssap != BSSAP_MSG_DTAP)
312 return 0;
313
314 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
315 if (!hdr48)
316 return -1;
317
318 proto = hdr48->proto_discr & 0x0f;
319 msg_type = hdr48->msg_type & 0xbf;
320 if (proto == GSM48_PDISC_MM &&
321 msg_type == GSM48_MT_MM_ID_RESP) {
322 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
323 } else {
324 return 0;
325 }
326}