blob: 4fda517c218bbba34a36ecb27234b7680872412c [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 Freyther3e9a7f82010-10-12 23:21:54 +020031#include <openbsc/vty.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020033#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080034#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080035#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080036
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080037#include <osmocore/protocol/gsm_08_08.h>
38
Harald Welted5db12c2010-08-03 15:11:51 +020039#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020040
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020041#include <netinet/in.h>
42#include <arpa/inet.h>
43
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080044static 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 Freyther463dc622010-10-03 19:41:42 +080053 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
54 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
55 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
56 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080057};
58
59static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
60 .group_name_prefix = "nat.bsc",
61 .group_description = "NAT BSC Statistics",
62 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
63 .ctr_desc = bsc_cfg_ctr_description,
64};
65
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080066static const struct rate_ctr_desc acc_list_ctr_description[] = {
67 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
68 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
69};
70
71static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
72 .group_name_prefix = "nat.filter",
73 .group_description = "NAT Access-List Statistics",
74 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
75 .ctr_desc = acc_list_ctr_description,
76};
77
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080078struct bsc_nat *bsc_nat_alloc(void)
79{
80 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
81 if (!nat)
82 return NULL;
83
84 INIT_LLIST_HEAD(&nat->sccp_connections);
85 INIT_LLIST_HEAD(&nat->bsc_connections);
86 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080087 INIT_LLIST_HEAD(&nat->access_lists);
88
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020089 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
90 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
91 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
92 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
93 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080094 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020095 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080096 nat->auth_timeout = 2;
97 nat->ping_timeout = 20;
98 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080099 return nat;
100}
101
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800102void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
103{
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +0200104 bsc_replace_string(nat, &nat->msc_ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800105}
106
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800107struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
108{
109 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
110 if (!con)
111 return NULL;
112
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200113 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800114 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800115 return con;
116}
117
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800118struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800119{
120 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
121 if (!conf)
122 return NULL;
123
124 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800125 conf->nr = nat->num_bsc;
126 conf->nat = nat;
127
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800128 INIT_LLIST_HEAD(&conf->lac_list);
129
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200130 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800131 ++nat->num_bsc;
132
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800133 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800134 if (!conf->stats.ctrg) {
135 talloc_free(conf);
136 return NULL;
137 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200138
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800139 return conf;
140}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200141
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800142void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
143{
144 struct bsc_lac_entry *lac;
145
146 llist_for_each_entry(lac, &cfg->lac_list, entry)
147 if (lac->lac == _lac)
148 return;
149
150 lac = talloc_zero(cfg, struct bsc_lac_entry);
151 if (!lac) {
152 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
153 return;
154 }
155
156 lac->lac = _lac;
157 llist_add_tail(&lac->entry, &cfg->lac_list);
158}
159
160void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
161{
162 struct bsc_lac_entry *lac;
163
164 llist_for_each_entry(lac, &cfg->lac_list, entry)
165 if (lac->lac == _lac) {
166 llist_del(&lac->entry);
167 talloc_free(lac);
168 return;
169 }
170}
171
172int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
173{
174 struct bsc_lac_entry *entry;
175
176 llist_for_each_entry(entry, &cfg->lac_list, entry)
177 if (entry->lac == lac_nr)
178 return 1;
179
180 return 0;
181}
182
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200183void sccp_connection_destroy(struct sccp_connections *conn)
184{
185 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
186 sccp_src_ref_to_int(&conn->real_ref),
187 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800188 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200189 llist_del(&conn->list_entry);
190 talloc_free(conn);
191}
192
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200193struct 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 +0200194{
195 struct bsc_connection *bsc;
196 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800197 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200198 struct tlv_parsed tp;
199 int i = 0;
200
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800201 *lac_out = -1;
202
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200203 if (!msg->l3h || msgb_l3len(msg) < 3) {
204 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
205 return NULL;
206 }
207
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200208 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
209 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
210 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
211 return NULL;
212 }
213
214 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
215 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800216
217 /* No need to try a different BSS */
218 if (data[0] == CELL_IDENT_BSS) {
219 return NULL;
220 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200221 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200222 return NULL;
223 }
224
225 /* Currently we only handle one BSC */
226 for (i = 1; i < data_length - 1; i += 2) {
227 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200228 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200229 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200230 if (!bsc->cfg)
231 continue;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800232 if (!bsc->authenticated)
233 continue;
234 if (!bsc_config_handles_lac(bsc->cfg, _lac))
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200235 continue;
236
237 return bsc;
238 }
239 }
240
241 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200242}
243
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800244int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200245{
246 struct msgb *msg;
247
248 if (length > 4096 - 128) {
249 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
250 return -1;
251 }
252
253 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
254 if (!msg) {
255 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
256 return -1;
257 }
258
259 /* copy the data */
260 msg->l3h = msgb_put(msg, length);
261 memcpy(msg->l3h, data, length);
262
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +0200263 return bsc_write(bsc, msg, IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200264}
265
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200266int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200267{
268 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200269 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200270
271 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
272 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
273 msgb_free(msg);
274 return -1;
275 }
276
277 return 0;
278}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800279
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800280static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
281{
282 struct bsc_nat_acc_lst_entry *entry;
283
284 llist_for_each_entry(entry, &lst->fltr_list, list) {
285 if (!entry->imsi_allow)
286 continue;
287 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
288 return 0;
289 }
290
291 return 1;
292}
293
294static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
295{
296 struct bsc_nat_acc_lst_entry *entry;
297
298 llist_for_each_entry(entry, &lst->fltr_list, list) {
299 if (!entry->imsi_deny)
300 continue;
301 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
302 return 0;
303 }
304
305 return 1;
306}
307
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800308/* apply white/black list */
309static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
310{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800311 /*
312 * Now apply blacklist/whitelist of the BSC and the NAT.
313 * 1.) Reject if the IMSI is not allowed at the BSC
314 * 2.) Allow directly if the IMSI is allowed at the BSC
315 * 3.) Reject if the IMSI not allowed at the global level.
316 * 4.) Allow directly if the IMSI is allowed at the global level
317 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800318 struct bsc_nat_acc_lst *nat_lst = NULL;
319 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800320
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800321 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
322 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800323
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800324
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800325 if (bsc_lst) {
326 /* 1. BSC deny */
327 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800328 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800329 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800330 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800331 return -2;
332 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800333
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800334 /* 2. BSC allow */
335 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800336 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800337 }
338
339 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800340 if (nat_lst) {
341 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800342 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800343 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther0c35b5b2010-10-06 00:18:20 +0800344 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800345 return -3;
346 }
347 }
348
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800349 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800350}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800351
352static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
353{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800354 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800355 struct gsm48_loc_upd_req *lu;
356 char mi_string[GSM48_MI_SIZE];
357
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800358 if (length < sizeof(*lu)) {
359 LOGP(DNAT, LOGL_ERROR,
360 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800361 return -1;
362 }
363
364 lu = (struct gsm48_loc_upd_req *) data;
365 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
366
367 /*
368 * We can only deal with the IMSI. This will fail for a phone that
369 * will send the TMSI of a previous network to us.
370 */
371 if (mi_type != GSM_MI_TYPE_IMSI)
372 return 0;
373
374 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800375 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800376}
377
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800378static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
379{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800380 static const uint32_t classmark_offset =
381 offsetof(struct gsm48_service_request, classmark);
382
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800383 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800384 uint8_t mi_type;
385 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800386 struct gsm48_service_request *req;
387
388 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800389
390 if (length < sizeof(*req)) {
391 LOGP(DNAT, LOGL_ERROR,
392 "CM Serv Req does not fit. Length is %d\n", length);
393 return -1;
394 }
395
396 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800397 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
398 length - classmark_offset, mi_string, &mi_type);
399 if (rc < 0) {
400 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800401 return -1;
402 }
403
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800404 /* we have to let the TMSI or such pass */
405 if (mi_type != GSM_MI_TYPE_IMSI)
406 return 0;
407
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800408 return auth_imsi(bsc, mi_string);
409}
410
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800411static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
412{
413 struct gsm48_pag_resp *resp;
414 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800415 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800416
417 if (length < sizeof(*resp)) {
418 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
419 return -1;
420 }
421
422 resp = (struct gsm48_pag_resp *) data;
423 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
424 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
425 return -1;
426 }
427
428 /* we need to let it pass for now */
429 if (mi_type != GSM_MI_TYPE_IMSI)
430 return 0;
431
432 return auth_imsi(bsc, mi_string);
433}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800434
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800435static int _dt_check_id_resp(struct bsc_connection *bsc,
436 uint8_t *data, unsigned int length,
437 struct sccp_connections *con)
438{
439 char mi_string[GSM48_MI_SIZE];
440 uint8_t mi_type;
441 int ret;
442
443 if (length < 2) {
444 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
445 return -1;
446 }
447
448 if (data[0] < length - 1) {
449 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
450 return -2;
451 }
452
453 mi_type = data[1] & GSM_MI_TYPE_MASK;
454 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
455
456 if (mi_type != GSM_MI_TYPE_IMSI)
457 return 0;
458
459 ret = auth_imsi(bsc, mi_string);
460 con->imsi_checked = 1;
461 return ret;
462}
463
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800464/* Filter out CR data... */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800465int 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 +0800466{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800467 struct tlv_parsed tp;
468 struct gsm48_hdr *hdr48;
469 int hdr48_len;
470 int len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800471 uint8_t msg_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800472
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800473 *con_type = NAT_CON_TYPE_NONE;
474
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800475 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
476 LOGP(DNAT, LOGL_ERROR,
477 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
478 return -1;
479 }
480
481 /* the parsed has had some basic l3 length check */
482 len = msg->l3h[1];
483 if (msgb_l3len(msg) - 3 < len) {
484 LOGP(DNAT, LOGL_ERROR,
485 "The CR Data has not enough space...\n");
486 return -1;
487 }
488
489 msg->l4h = &msg->l3h[3];
490 len -= 1;
491
492 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
493
494 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
495 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
496 return -1;
497 }
498
499 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
500
501 if (hdr48_len < sizeof(*hdr48)) {
502 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
503 return -1;
504 }
505
506 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
507
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800508 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800509 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800510 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800511 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800512 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800513 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800514 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800515 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800516 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800517 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800518 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800519 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800520 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800521 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800522 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800523 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800524 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800525 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800526}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800527
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800528int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
529 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
530{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800531 uint32_t len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800532 uint8_t msg_type;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800533 struct gsm48_hdr *hdr48;
534
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800535 if (con->imsi_checked)
536 return 0;
537
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800538 /* only care about DTAP messages */
539 if (parsed->bssap != BSSAP_MSG_DTAP)
540 return 0;
541
542 /* gsm_type is actually the size of the dtap */
543 len = parsed->gsm_type;
544 if (len < msgb_l3len(msg) - 3) {
545 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
546 return -1;
547 }
548
549 if (len < sizeof(*hdr48)) {
550 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
551 return -1;
552 }
553
554 msg->l4h = &msg->l3h[3];
555 hdr48 = (struct gsm48_hdr *) msg->l4h;
556
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800557 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800558 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800559 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800560 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
561 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800562 return 0;
563 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800564}
565
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800566void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
567{
568 if (*imsi) {
569 talloc_free(*imsi);
570 *imsi = NULL;
571 }
572 regfree(reg);
573
574 if (argc > 0) {
575 *imsi = talloc_strdup(ctx, argv[0]);
576 regcomp(reg, argv[0], 0);
577 }
578}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800579
580static const char *con_types [] = {
581 [NAT_CON_TYPE_NONE] = "n/a",
582 [NAT_CON_TYPE_LU] = "Location Update",
583 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
584 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800585 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800586 [NAT_CON_TYPE_OTHER] = "Other",
587};
588
589const char *bsc_con_type_to_string(int type)
590{
591 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800592}
593
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800594struct 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 +0800595{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800596 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800597
598 if (!name)
599 return NULL;
600
601 llist_for_each_entry(lst, &nat->access_lists, list)
602 if (strcmp(lst->name, name) == 0)
603 return lst;
604
605 return NULL;
606}
607
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800608struct 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 +0800609{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800610 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800611
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800612 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800613 if (lst)
614 return lst;
615
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800616 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800617 if (!lst) {
618 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
619 return NULL;
620 }
621
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800622 /* TODO: get the index right */
623 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
624 if (!lst->stats) {
625 talloc_free(lst);
626 return NULL;
627 }
628
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800629 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800630 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800631 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800632 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800633}
634
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800635void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800636{
637 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800638 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800639 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800640}
641
642struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
643{
644 struct bsc_nat_acc_lst_entry *entry;
645
646 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
647 if (!entry)
648 return NULL;
649
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800650 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800651 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800652}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800653
654int bsc_nat_msc_is_connected(struct bsc_nat *nat)
655{
656 return nat->msc_con->is_connected;
657}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800658
659static const int con_to_ctr[] = {
660 [NAT_CON_TYPE_NONE] = -1,
661 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
662 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
663 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
664 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
665 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
666};
667
668int bsc_conn_type_to_ctr(struct sccp_connections *conn)
669{
670 return con_to_ctr[conn->con_type];
671}