blob: a458afdaabb944ec7042a43422fae72ee5bb9e26 [file] [log] [blame]
Holger Hans Peter Freyther090a4d82010-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 Freyther4a9dd3b2010-07-31 05:17:17 +080026#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther4d0fbda2010-07-05 14:39:44 +080027#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +080028#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +020029#include <openbsc/debug.h>
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +020030#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther7c831f62010-10-12 23:21:54 +020031#include <openbsc/vty.h>
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +080032
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +020033#include <osmocore/linuxlist.h>
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +080034#include <osmocore/talloc.h>
Holger Hans Peter Freythera7070612010-06-15 18:50:57 +080035#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +080036
Holger Hans Peter Freythere36be812010-06-15 20:13:33 +080037#include <osmocore/protocol/gsm_08_08.h>
38
Harald Welte90e5eae2010-08-03 15:11:51 +020039#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +020040
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +020041#include <netinet/in.h>
42#include <arpa/inet.h>
Holger Hans Peter Freyther6c1ae962010-10-13 20:37:13 +020043#include <unistd.h>
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +020044
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +080045static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther275f27a2010-06-17 18:31:18 +080046 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
47 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
48 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
49 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
50 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freythera67fc6f2010-09-25 17:58:22 +080051 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
52 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
53 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freyther2e728072010-10-03 19:41:42 +080054 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
55 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
56 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
57 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +080058};
59
60static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
61 .group_name_prefix = "nat.bsc",
62 .group_description = "NAT BSC Statistics",
63 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
64 .ctr_desc = bsc_cfg_ctr_description,
65};
66
Holger Hans Peter Freyther39f18802010-09-25 06:14:52 +080067static const struct rate_ctr_desc acc_list_ctr_description[] = {
68 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
69 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
70};
71
72static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
73 .group_name_prefix = "nat.filter",
74 .group_description = "NAT Access-List Statistics",
75 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
76 .ctr_desc = acc_list_ctr_description,
77};
78
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +080079struct bsc_nat *bsc_nat_alloc(void)
80{
81 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
82 if (!nat)
83 return NULL;
84
85 INIT_LLIST_HEAD(&nat->sccp_connections);
86 INIT_LLIST_HEAD(&nat->bsc_connections);
87 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +080088 INIT_LLIST_HEAD(&nat->access_lists);
89
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +020090 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
91 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
92 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
93 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
94 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +080095 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freytherbace2022010-04-17 07:48:45 +020096 nat->msc_port = 5000;
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +080097 nat->auth_timeout = 2;
98 nat->ping_timeout = 20;
99 nat->pong_timeout = 5;
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800100 return nat;
101}
102
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +0800103void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
104{
Holger Hans Peter Freyther7c831f62010-10-12 23:21:54 +0200105 bsc_replace_string(nat, &nat->msc_ip, ip);
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +0800106}
107
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800108struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
109{
110 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
111 if (!con)
112 return NULL;
113
Holger Hans Peter Freyther72deaea2010-03-29 15:14:15 +0200114 con->nat = nat;
Holger Hans Peter Freyther0952b2f2010-04-22 12:05:23 +0800115 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800116 return con;
117}
118
Holger Hans Peter Freyther1f040832010-10-08 22:08:29 +0800119struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800120{
121 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
122 if (!conf)
123 return NULL;
124
125 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800126 conf->nr = nat->num_bsc;
127 conf->nat = nat;
128
Holger Hans Peter Freyther1f040832010-10-08 22:08:29 +0800129 INIT_LLIST_HEAD(&conf->lac_list);
130
Holger Hans Peter Freyther085ea302010-04-16 16:52:20 +0200131 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800132 ++nat->num_bsc;
133
Holger Hans Peter Freyther1f040832010-10-08 22:08:29 +0800134 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +0800135 if (!conf->stats.ctrg) {
136 talloc_free(conf);
137 return NULL;
138 }
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200139
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800140 return conf;
141}
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200142
Holger Hans Peter Freyther1f040832010-10-08 22:08:29 +0800143void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
144{
145 struct bsc_lac_entry *lac;
146
147 llist_for_each_entry(lac, &cfg->lac_list, entry)
148 if (lac->lac == _lac)
149 return;
150
151 lac = talloc_zero(cfg, struct bsc_lac_entry);
152 if (!lac) {
153 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
154 return;
155 }
156
157 lac->lac = _lac;
158 llist_add_tail(&lac->entry, &cfg->lac_list);
159}
160
161void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
162{
163 struct bsc_lac_entry *lac;
164
165 llist_for_each_entry(lac, &cfg->lac_list, entry)
166 if (lac->lac == _lac) {
167 llist_del(&lac->entry);
168 talloc_free(lac);
169 return;
170 }
171}
172
173int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
174{
175 struct bsc_lac_entry *entry;
176
177 llist_for_each_entry(entry, &cfg->lac_list, entry)
178 if (entry->lac == lac_nr)
179 return 1;
180
181 return 0;
182}
183
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200184void sccp_connection_destroy(struct sccp_connections *conn)
185{
186 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
187 sccp_src_ref_to_int(&conn->real_ref),
188 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800189 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200190 llist_del(&conn->list_entry);
191 talloc_free(conn);
192}
193
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200194struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out)
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200195{
196 struct bsc_connection *bsc;
197 int data_length;
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800198 const uint8_t *data;
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200199 struct tlv_parsed tp;
200 int i = 0;
201
Holger Hans Peter Freyther05c77ab2010-04-18 02:41:20 +0800202 *lac_out = -1;
203
Holger Hans Peter Freyther4b66bff2010-03-30 06:51:23 +0200204 if (!msg->l3h || msgb_l3len(msg) < 3) {
205 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
206 return NULL;
207 }
208
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200209 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
210 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
211 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
212 return NULL;
213 }
214
215 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
216 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freyther69a365e2010-04-21 18:47:24 +0800217
218 /* No need to try a different BSS */
219 if (data[0] == CELL_IDENT_BSS) {
220 return NULL;
221 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther60ebf142010-04-06 10:25:40 +0200222 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200223 return NULL;
224 }
225
226 /* Currently we only handle one BSC */
227 for (i = 1; i < data_length - 1; i += 2) {
228 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200229 *lac_out = _lac;
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200230 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherbfcf5192010-04-06 15:11:34 +0200231 if (!bsc->cfg)
232 continue;
Holger Hans Peter Freyther1f040832010-10-08 22:08:29 +0800233 if (!bsc->authenticated)
234 continue;
235 if (!bsc_config_handles_lac(bsc->cfg, _lac))
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200236 continue;
237
238 return bsc;
239 }
240 }
241
242 return NULL;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200243}
244
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800245int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200246{
247 struct msgb *msg;
248
249 if (length > 4096 - 128) {
250 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
251 return -1;
252 }
253
254 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
255 if (!msg) {
256 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
257 return -1;
258 }
259
260 /* copy the data */
261 msg->l3h = msgb_put(msg, length);
262 memcpy(msg->l3h, data, length);
263
Holger Hans Peter Freytherd5cd3042010-10-13 23:52:01 +0200264 return bsc_write(bsc, msg, IPAC_PROTO_MGCP);
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200265}
266
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200267int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200268{
Holger Hans Peter Freytherf232b272010-10-13 17:56:22 +0200269 return bsc_do_write(&bsc->write_queue, msg, proto);
270}
271
272int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto)
273{
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200274 /* prepend the header */
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200275 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200276
Holger Hans Peter Freytherf232b272010-10-13 17:56:22 +0200277 if (write_queue_enqueue(queue, msg) != 0) {
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200278 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
279 msgb_free(msg);
280 return -1;
281 }
282
283 return 0;
284}
Holger Hans Peter Freyther6d357162010-05-14 03:39:56 +0800285
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800286static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
287{
288 struct bsc_nat_acc_lst_entry *entry;
289
290 llist_for_each_entry(entry, &lst->fltr_list, list) {
291 if (!entry->imsi_allow)
292 continue;
293 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
294 return 0;
295 }
296
297 return 1;
298}
299
300static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
301{
302 struct bsc_nat_acc_lst_entry *entry;
303
304 llist_for_each_entry(entry, &lst->fltr_list, list) {
305 if (!entry->imsi_deny)
306 continue;
307 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
308 return 0;
309 }
310
311 return 1;
312}
313
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800314/* apply white/black list */
315static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
316{
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800317 /*
318 * Now apply blacklist/whitelist of the BSC and the NAT.
319 * 1.) Reject if the IMSI is not allowed at the BSC
320 * 2.) Allow directly if the IMSI is allowed at the BSC
321 * 3.) Reject if the IMSI not allowed at the global level.
322 * 4.) Allow directly if the IMSI is allowed at the global level
323 */
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800324 struct bsc_nat_acc_lst *nat_lst = NULL;
325 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800326
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800327 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
328 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800329
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800330
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800331 if (bsc_lst) {
332 /* 1. BSC deny */
333 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800334 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther801efa12010-05-16 00:45:07 +0800335 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther39f18802010-09-25 06:14:52 +0800336 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800337 return -2;
338 }
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800339
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800340 /* 2. BSC allow */
341 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther531d3e32010-09-15 00:41:19 +0800342 return 1;
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800343 }
344
345 /* 3. NAT deny */
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800346 if (nat_lst) {
347 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800348 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther801efa12010-05-16 00:45:07 +0800349 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther3cc2e2f2010-10-06 00:18:20 +0800350 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800351 return -3;
352 }
353 }
354
Holger Hans Peter Freyther531d3e32010-09-15 00:41:19 +0800355 return 1;
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800356}
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800357
358static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
359{
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800360 uint8_t mi_type;
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800361 struct gsm48_loc_upd_req *lu;
362 char mi_string[GSM48_MI_SIZE];
363
Holger Hans Peter Freyther540f4c82010-05-14 19:24:06 +0800364 if (length < sizeof(*lu)) {
365 LOGP(DNAT, LOGL_ERROR,
366 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800367 return -1;
368 }
369
370 lu = (struct gsm48_loc_upd_req *) data;
371 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
372
373 /*
374 * We can only deal with the IMSI. This will fail for a phone that
375 * will send the TMSI of a previous network to us.
376 */
377 if (mi_type != GSM_MI_TYPE_IMSI)
378 return 0;
379
380 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther13abb822010-05-14 19:49:35 +0800381 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800382}
383
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800384static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
385{
Holger Hans Peter Freyther2ed35b32010-05-16 01:13:28 +0800386 static const uint32_t classmark_offset =
387 offsetof(struct gsm48_service_request, classmark);
388
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800389 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther2ed35b32010-05-16 01:13:28 +0800390 uint8_t mi_type;
391 int rc;
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800392 struct gsm48_service_request *req;
393
394 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800395
396 if (length < sizeof(*req)) {
397 LOGP(DNAT, LOGL_ERROR,
398 "CM Serv Req does not fit. Length is %d\n", length);
399 return -1;
400 }
401
402 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther2ed35b32010-05-16 01:13:28 +0800403 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
404 length - classmark_offset, mi_string, &mi_type);
405 if (rc < 0) {
406 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800407 return -1;
408 }
409
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800410 /* we have to let the TMSI or such pass */
411 if (mi_type != GSM_MI_TYPE_IMSI)
412 return 0;
413
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800414 return auth_imsi(bsc, mi_string);
415}
416
Holger Hans Peter Freytherecaa0b12010-05-15 23:54:04 +0800417static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
418{
419 struct gsm48_pag_resp *resp;
420 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800421 uint8_t mi_type;
Holger Hans Peter Freytherecaa0b12010-05-15 23:54:04 +0800422
423 if (length < sizeof(*resp)) {
424 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
425 return -1;
426 }
427
428 resp = (struct gsm48_pag_resp *) data;
429 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
430 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
431 return -1;
432 }
433
434 /* we need to let it pass for now */
435 if (mi_type != GSM_MI_TYPE_IMSI)
436 return 0;
437
438 return auth_imsi(bsc, mi_string);
439}
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800440
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800441static int _dt_check_id_resp(struct bsc_connection *bsc,
442 uint8_t *data, unsigned int length,
443 struct sccp_connections *con)
444{
445 char mi_string[GSM48_MI_SIZE];
446 uint8_t mi_type;
447 int ret;
448
449 if (length < 2) {
450 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
451 return -1;
452 }
453
454 if (data[0] < length - 1) {
455 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
456 return -2;
457 }
458
459 mi_type = data[1] & GSM_MI_TYPE_MASK;
460 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
461
462 if (mi_type != GSM_MI_TYPE_IMSI)
463 return 0;
464
465 ret = auth_imsi(bsc, mi_string);
466 con->imsi_checked = 1;
467 return ret;
468}
469
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800470/* Filter out CR data... */
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800471int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed, int *con_type)
Holger Hans Peter Freyther6d357162010-05-14 03:39:56 +0800472{
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800473 struct tlv_parsed tp;
474 struct gsm48_hdr *hdr48;
475 int hdr48_len;
476 int len;
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800477 uint8_t msg_type;
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800478
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800479 *con_type = NAT_CON_TYPE_NONE;
480
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800481 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
482 LOGP(DNAT, LOGL_ERROR,
483 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
484 return -1;
485 }
486
487 /* the parsed has had some basic l3 length check */
488 len = msg->l3h[1];
489 if (msgb_l3len(msg) - 3 < len) {
490 LOGP(DNAT, LOGL_ERROR,
491 "The CR Data has not enough space...\n");
492 return -1;
493 }
494
495 msg->l4h = &msg->l3h[3];
496 len -= 1;
497
498 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
499
500 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
501 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
502 return -1;
503 }
504
505 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
506
507 if (hdr48_len < sizeof(*hdr48)) {
508 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
509 return -1;
510 }
511
512 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
513
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800514 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther6c1610f2010-05-15 22:09:39 +0800515 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800516 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800517 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800518 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther6c1610f2010-05-15 22:09:39 +0800519 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800520 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800521 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther2ebd52a2010-05-15 21:58:33 +0800522 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherecaa0b12010-05-15 23:54:04 +0800523 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800524 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800525 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherecaa0b12010-05-15 23:54:04 +0800526 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800527 } else {
Holger Hans Peter Freyther49c0c522010-05-16 01:05:47 +0800528 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800529 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther49c0c522010-05-16 01:05:47 +0800530 return 0;
Holger Hans Peter Freyther4ebb30e2010-05-14 08:14:09 +0800531 }
Holger Hans Peter Freyther6d357162010-05-14 03:39:56 +0800532}
Holger Hans Peter Freytherdac2dee2010-05-14 18:38:29 +0800533
Holger Hans Peter Freyther1b4bde62010-09-15 01:11:08 +0800534int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
535 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
536{
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800537 uint32_t len;
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800538 uint8_t msg_type;
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800539 struct gsm48_hdr *hdr48;
540
Holger Hans Peter Freyther1b4bde62010-09-15 01:11:08 +0800541 if (con->imsi_checked)
542 return 0;
543
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800544 /* only care about DTAP messages */
545 if (parsed->bssap != BSSAP_MSG_DTAP)
546 return 0;
547
548 /* gsm_type is actually the size of the dtap */
549 len = parsed->gsm_type;
550 if (len < msgb_l3len(msg) - 3) {
551 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
552 return -1;
553 }
554
555 if (len < sizeof(*hdr48)) {
556 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
557 return -1;
558 }
559
560 msg->l4h = &msg->l3h[3];
561 hdr48 = (struct gsm48_hdr *) msg->l4h;
562
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800563 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800564 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freytherefd367d2010-09-15 05:24:25 +0800565 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800566 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
567 } else {
Holger Hans Peter Freythera15e2572010-09-15 05:20:40 +0800568 return 0;
569 }
Holger Hans Peter Freyther1b4bde62010-09-15 01:11:08 +0800570}
571
Holger Hans Peter Freytherdac2dee2010-05-14 18:38:29 +0800572void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
573{
574 if (*imsi) {
575 talloc_free(*imsi);
576 *imsi = NULL;
577 }
578 regfree(reg);
579
580 if (argc > 0) {
581 *imsi = talloc_strdup(ctx, argv[0]);
582 regcomp(reg, argv[0], 0);
583 }
584}
Holger Hans Peter Freyther8e69f0c2010-05-16 02:06:11 +0800585
586static const char *con_types [] = {
587 [NAT_CON_TYPE_NONE] = "n/a",
588 [NAT_CON_TYPE_LU] = "Location Update",
589 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
590 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800591 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther8e69f0c2010-05-16 02:06:11 +0800592 [NAT_CON_TYPE_OTHER] = "Other",
593};
594
595const char *bsc_con_type_to_string(int type)
596{
597 return con_types[type];
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800598}
599
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800600struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name)
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800601{
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800602 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800603
604 if (!name)
605 return NULL;
606
607 llist_for_each_entry(lst, &nat->access_lists, list)
608 if (strcmp(lst->name, name) == 0)
609 return lst;
610
611 return NULL;
612}
613
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800614struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name)
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800615{
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800616 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800617
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800618 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800619 if (lst)
620 return lst;
621
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800622 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800623 if (!lst) {
624 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
625 return NULL;
626 }
627
Holger Hans Peter Freyther39f18802010-09-25 06:14:52 +0800628 /* TODO: get the index right */
629 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
630 if (!lst->stats) {
631 talloc_free(lst);
632 return NULL;
633 }
634
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800635 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800636 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther6b762502010-06-08 11:00:09 +0800637 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther67b6d522010-06-01 01:03:13 +0800638 return lst;
Holger Hans Peter Freyther704b3502010-06-03 01:44:05 +0800639}
640
Holger Hans Peter Freytherb6044712010-06-08 10:14:44 +0800641void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freyther704b3502010-06-03 01:44:05 +0800642{
643 llist_del(&lst->list);
Holger Hans Peter Freyther39f18802010-09-25 06:14:52 +0800644 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freyther704b3502010-06-03 01:44:05 +0800645 talloc_free(lst);
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800646}
647
648struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
649{
650 struct bsc_nat_acc_lst_entry *entry;
651
652 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
653 if (!entry)
654 return NULL;
655
Holger Hans Peter Freyther6b762502010-06-08 11:00:09 +0800656 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freyther8eb45892010-06-08 10:53:39 +0800657 return entry;
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +0800658}
Holger Hans Peter Freyther4d0fbda2010-07-05 14:39:44 +0800659
660int bsc_nat_msc_is_connected(struct bsc_nat *nat)
661{
662 return nat->msc_con->is_connected;
663}
Holger Hans Peter Freyther2e728072010-10-03 19:41:42 +0800664
665static const int con_to_ctr[] = {
666 [NAT_CON_TYPE_NONE] = -1,
667 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
668 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
669 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
670 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
671 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
672};
673
674int bsc_conn_type_to_ctr(struct sccp_connections *conn)
675{
676 return con_to_ctr[conn->con_type];
677}
Holger Hans Peter Freyther6c1ae962010-10-13 20:37:13 +0200678
679int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
680{
681 int rc;
682
683 rc = write(bfd->fd, msg->data, msg->len);
684 if (rc != msg->len)
685 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
686
687 return rc;
688}
689