blob: 77deef4242d8ff17e6f84f5f18145ab166e373b3 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
5 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010 by On-Waves
7 * 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 General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080026#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +080027#include <openbsc/bsc_msc.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080028#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020029#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020030#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080031
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020032#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080033#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080034#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080035
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080036#include <osmocore/protocol/gsm_08_08.h>
37
Harald Welted5db12c2010-08-03 15:11:51 +020038#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020039
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020040#include <netinet/in.h>
41#include <arpa/inet.h>
42
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080043
44static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-06-17 18:31:18 +080045 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
46 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
47 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
48 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
49 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080050 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
51 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
52 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080053};
54
55static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
56 .group_name_prefix = "nat.bsc",
57 .group_description = "NAT BSC Statistics",
58 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
59 .ctr_desc = bsc_cfg_ctr_description,
60};
61
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080062static const struct rate_ctr_desc acc_list_ctr_description[] = {
63 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
64 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
65};
66
67static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
68 .group_name_prefix = "nat.filter",
69 .group_description = "NAT Access-List Statistics",
70 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
71 .ctr_desc = acc_list_ctr_description,
72};
73
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080074struct bsc_nat *bsc_nat_alloc(void)
75{
76 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
77 if (!nat)
78 return NULL;
79
80 INIT_LLIST_HEAD(&nat->sccp_connections);
81 INIT_LLIST_HEAD(&nat->bsc_connections);
82 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080083 INIT_LLIST_HEAD(&nat->access_lists);
84
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020085 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
86 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
87 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
88 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
89 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080090 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020091 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080092 nat->auth_timeout = 2;
93 nat->ping_timeout = 20;
94 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080095 return nat;
96}
97
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080098void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
99{
100 if (nat->msc_ip)
101 talloc_free(nat->msc_ip);
102 nat->msc_ip = talloc_strdup(nat, ip);
103}
104
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800105struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
106{
107 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
108 if (!con)
109 return NULL;
110
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200111 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800112 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800113 return con;
114}
115
116struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
117{
118 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
119 if (!conf)
120 return NULL;
121
122 conf->token = talloc_strdup(conf, token);
123 conf->lac = lac;
124 conf->nr = nat->num_bsc;
125 conf->nat = nat;
126
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200127 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800128 ++nat->num_bsc;
129
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800130 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->lac);
131 if (!conf->stats.ctrg) {
132 talloc_free(conf);
133 return NULL;
134 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200135
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800136 return conf;
137}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200138
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200139void sccp_connection_destroy(struct sccp_connections *conn)
140{
141 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
142 sccp_src_ref_to_int(&conn->real_ref),
143 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800144 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200145 llist_del(&conn->list_entry);
146 talloc_free(conn);
147}
148
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200149struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200150{
151 struct bsc_connection *bsc;
152 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800153 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200154 struct tlv_parsed tp;
155 int i = 0;
156
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800157 *lac_out = -1;
158
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200159 if (!msg->l3h || msgb_l3len(msg) < 3) {
160 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
161 return NULL;
162 }
163
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200164 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
165 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
166 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
167 return NULL;
168 }
169
170 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
171 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800172
173 /* No need to try a different BSS */
174 if (data[0] == CELL_IDENT_BSS) {
175 return NULL;
176 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200177 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200178 return NULL;
179 }
180
181 /* Currently we only handle one BSC */
182 for (i = 1; i < data_length - 1; i += 2) {
183 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200184 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200185 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200186 if (!bsc->cfg)
187 continue;
188 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200189 continue;
190
191 return bsc;
192 }
193 }
194
195 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200196}
197
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800198int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200199{
200 struct msgb *msg;
201
202 if (length > 4096 - 128) {
203 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
204 return -1;
205 }
206
207 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
208 if (!msg) {
209 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
210 return -1;
211 }
212
213 /* copy the data */
214 msg->l3h = msgb_put(msg, length);
215 memcpy(msg->l3h, data, length);
216
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200217 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200218}
219
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200220int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200221{
222 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200223 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200224
225 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
226 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
227 msgb_free(msg);
228 return -1;
229 }
230
231 return 0;
232}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800233
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800234static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
235{
236 struct bsc_nat_acc_lst_entry *entry;
237
238 llist_for_each_entry(entry, &lst->fltr_list, list) {
239 if (!entry->imsi_allow)
240 continue;
241 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
242 return 0;
243 }
244
245 return 1;
246}
247
248static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
249{
250 struct bsc_nat_acc_lst_entry *entry;
251
252 llist_for_each_entry(entry, &lst->fltr_list, list) {
253 if (!entry->imsi_deny)
254 continue;
255 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
256 return 0;
257 }
258
259 return 1;
260}
261
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800262/* apply white/black list */
263static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
264{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800265 /*
266 * Now apply blacklist/whitelist of the BSC and the NAT.
267 * 1.) Reject if the IMSI is not allowed at the BSC
268 * 2.) Allow directly if the IMSI is allowed at the BSC
269 * 3.) Reject if the IMSI not allowed at the global level.
270 * 4.) Allow directly if the IMSI is allowed at the global level
271 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800272 struct bsc_nat_acc_lst *nat_lst = NULL;
273 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800274
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800275 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
276 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800277
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800278
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800279 if (bsc_lst) {
280 /* 1. BSC deny */
281 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800282 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800283 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800284 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800285 return -2;
286 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800287
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800288 /* 2. BSC allow */
289 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800290 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800291 }
292
293 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800294 if (nat_lst) {
295 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800296 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800297 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800298 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800299 return -3;
300 }
301 }
302
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800303 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800304}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800305
306static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
307{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800308 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800309 struct gsm48_loc_upd_req *lu;
310 char mi_string[GSM48_MI_SIZE];
311
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800312 if (length < sizeof(*lu)) {
313 LOGP(DNAT, LOGL_ERROR,
314 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800315 return -1;
316 }
317
318 lu = (struct gsm48_loc_upd_req *) data;
319 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
320
321 /*
322 * We can only deal with the IMSI. This will fail for a phone that
323 * will send the TMSI of a previous network to us.
324 */
325 if (mi_type != GSM_MI_TYPE_IMSI)
326 return 0;
327
328 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800329 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800330}
331
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800332static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
333{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800334 static const uint32_t classmark_offset =
335 offsetof(struct gsm48_service_request, classmark);
336
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800337 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800338 uint8_t mi_type;
339 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800340 struct gsm48_service_request *req;
341
342 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800343
344 if (length < sizeof(*req)) {
345 LOGP(DNAT, LOGL_ERROR,
346 "CM Serv Req does not fit. Length is %d\n", length);
347 return -1;
348 }
349
350 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800351 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
352 length - classmark_offset, mi_string, &mi_type);
353 if (rc < 0) {
354 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800355 return -1;
356 }
357
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800358 /* we have to let the TMSI or such pass */
359 if (mi_type != GSM_MI_TYPE_IMSI)
360 return 0;
361
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800362 return auth_imsi(bsc, mi_string);
363}
364
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800365static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
366{
367 struct gsm48_pag_resp *resp;
368 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800369 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800370
371 if (length < sizeof(*resp)) {
372 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
373 return -1;
374 }
375
376 resp = (struct gsm48_pag_resp *) data;
377 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
378 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
379 return -1;
380 }
381
382 /* we need to let it pass for now */
383 if (mi_type != GSM_MI_TYPE_IMSI)
384 return 0;
385
386 return auth_imsi(bsc, mi_string);
387}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800388
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800389static int _dt_check_id_resp(struct bsc_connection *bsc,
390 uint8_t *data, unsigned int length,
391 struct sccp_connections *con)
392{
393 char mi_string[GSM48_MI_SIZE];
394 uint8_t mi_type;
395 int ret;
396
397 if (length < 2) {
398 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
399 return -1;
400 }
401
402 if (data[0] < length - 1) {
403 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
404 return -2;
405 }
406
407 mi_type = data[1] & GSM_MI_TYPE_MASK;
408 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
409
410 if (mi_type != GSM_MI_TYPE_IMSI)
411 return 0;
412
413 ret = auth_imsi(bsc, mi_string);
414 con->imsi_checked = 1;
415 return ret;
416}
417
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800418/* Filter out CR data... */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800419int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed, int *con_type)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800420{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800421 struct tlv_parsed tp;
422 struct gsm48_hdr *hdr48;
423 int hdr48_len;
424 int len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800425 uint8_t msg_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800426
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800427 *con_type = NAT_CON_TYPE_NONE;
428
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800429 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
430 LOGP(DNAT, LOGL_ERROR,
431 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
432 return -1;
433 }
434
435 /* the parsed has had some basic l3 length check */
436 len = msg->l3h[1];
437 if (msgb_l3len(msg) - 3 < len) {
438 LOGP(DNAT, LOGL_ERROR,
439 "The CR Data has not enough space...\n");
440 return -1;
441 }
442
443 msg->l4h = &msg->l3h[3];
444 len -= 1;
445
446 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
447
448 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
449 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
450 return -1;
451 }
452
453 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
454
455 if (hdr48_len < sizeof(*hdr48)) {
456 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
457 return -1;
458 }
459
460 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
461
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800462 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800463 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800464 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800465 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800466 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800467 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800468 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800469 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800470 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800471 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800472 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800473 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800474 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800475 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800476 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800477 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800478 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800479 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800480}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800481
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800482int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
483 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
484{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800485 uint32_t len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800486 uint8_t msg_type;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800487 struct gsm48_hdr *hdr48;
488
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800489 if (con->imsi_checked)
490 return 0;
491
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800492 /* only care about DTAP messages */
493 if (parsed->bssap != BSSAP_MSG_DTAP)
494 return 0;
495
496 /* gsm_type is actually the size of the dtap */
497 len = parsed->gsm_type;
498 if (len < msgb_l3len(msg) - 3) {
499 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
500 return -1;
501 }
502
503 if (len < sizeof(*hdr48)) {
504 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
505 return -1;
506 }
507
508 msg->l4h = &msg->l3h[3];
509 hdr48 = (struct gsm48_hdr *) msg->l4h;
510
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800511 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800512 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800513 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800514 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
515 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800516 return 0;
517 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800518}
519
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800520void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
521{
522 if (*imsi) {
523 talloc_free(*imsi);
524 *imsi = NULL;
525 }
526 regfree(reg);
527
528 if (argc > 0) {
529 *imsi = talloc_strdup(ctx, argv[0]);
530 regcomp(reg, argv[0], 0);
531 }
532}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800533
534static const char *con_types [] = {
535 [NAT_CON_TYPE_NONE] = "n/a",
536 [NAT_CON_TYPE_LU] = "Location Update",
537 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
538 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800539 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800540 [NAT_CON_TYPE_OTHER] = "Other",
541};
542
543const char *bsc_con_type_to_string(int type)
544{
545 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800546}
547
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800548struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name)
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800549{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800550 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800551
552 if (!name)
553 return NULL;
554
555 llist_for_each_entry(lst, &nat->access_lists, list)
556 if (strcmp(lst->name, name) == 0)
557 return lst;
558
559 return NULL;
560}
561
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800562struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name)
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800563{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800564 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800565
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800566 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800567 if (lst)
568 return lst;
569
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800570 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800571 if (!lst) {
572 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
573 return NULL;
574 }
575
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800576 /* TODO: get the index right */
577 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
578 if (!lst->stats) {
579 talloc_free(lst);
580 return NULL;
581 }
582
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800583 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800584 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800585 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800586 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800587}
588
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800589void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800590{
591 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800592 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800593 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800594}
595
596struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
597{
598 struct bsc_nat_acc_lst_entry *entry;
599
600 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
601 if (!entry)
602 return NULL;
603
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800604 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800605 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800606}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800607
608int bsc_nat_msc_is_connected(struct bsc_nat *nat)
609{
610 return nat->msc_con->is_connected;
611}