blob: faba7a548c9588beeb1f772c0563c2abe4c76e5e [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 +080043static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-06-17 18:31:18 +080044 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
45 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
46 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
47 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
48 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080049 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
50 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
51 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080052 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
53 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
54 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
55 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080056};
57
58static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
59 .group_name_prefix = "nat.bsc",
60 .group_description = "NAT BSC Statistics",
61 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
62 .ctr_desc = bsc_cfg_ctr_description,
63};
64
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080065static const struct rate_ctr_desc acc_list_ctr_description[] = {
66 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
67 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
68};
69
70static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
71 .group_name_prefix = "nat.filter",
72 .group_description = "NAT Access-List Statistics",
73 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
74 .ctr_desc = acc_list_ctr_description,
75};
76
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080077struct bsc_nat *bsc_nat_alloc(void)
78{
79 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
80 if (!nat)
81 return NULL;
82
83 INIT_LLIST_HEAD(&nat->sccp_connections);
84 INIT_LLIST_HEAD(&nat->bsc_connections);
85 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080086 INIT_LLIST_HEAD(&nat->access_lists);
87
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020088 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
89 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
90 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
91 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
92 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080093 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020094 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080095 nat->auth_timeout = 2;
96 nat->ping_timeout = 20;
97 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080098 return nat;
99}
100
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800101void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
102{
103 if (nat->msc_ip)
104 talloc_free(nat->msc_ip);
105 nat->msc_ip = talloc_strdup(nat, ip);
106}
107
Holger Hans Peter Freytherdcf8a7d2010-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 Freytherf8048d92010-03-29 15:14:15 +0200114 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800115 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800116 return con;
117}
118
119struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
120{
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);
126 conf->lac = lac;
127 conf->nr = nat->num_bsc;
128 conf->nat = nat;
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 Freytherb2c38eb2010-06-17 18:16:00 +0800133 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->lac);
134 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 Freyther23fe7be2010-03-30 10:45:48 +0200142void sccp_connection_destroy(struct sccp_connections *conn)
143{
144 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
145 sccp_src_ref_to_int(&conn->real_ref),
146 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800147 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200148 llist_del(&conn->list_entry);
149 talloc_free(conn);
150}
151
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200152struct 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 +0200153{
154 struct bsc_connection *bsc;
155 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800156 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200157 struct tlv_parsed tp;
158 int i = 0;
159
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800160 *lac_out = -1;
161
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200162 if (!msg->l3h || msgb_l3len(msg) < 3) {
163 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
164 return NULL;
165 }
166
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200167 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
168 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
169 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
170 return NULL;
171 }
172
173 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
174 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800175
176 /* No need to try a different BSS */
177 if (data[0] == CELL_IDENT_BSS) {
178 return NULL;
179 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200180 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200181 return NULL;
182 }
183
184 /* Currently we only handle one BSC */
185 for (i = 1; i < data_length - 1; i += 2) {
186 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200187 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200188 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200189 if (!bsc->cfg)
190 continue;
191 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200192 continue;
193
194 return bsc;
195 }
196 }
197
198 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200199}
200
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800201int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200202{
203 struct msgb *msg;
204
205 if (length > 4096 - 128) {
206 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
207 return -1;
208 }
209
210 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
211 if (!msg) {
212 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
213 return -1;
214 }
215
216 /* copy the data */
217 msg->l3h = msgb_put(msg, length);
218 memcpy(msg->l3h, data, length);
219
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200220 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200221}
222
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200223int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200224{
225 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200226 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200227
228 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
229 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
230 msgb_free(msg);
231 return -1;
232 }
233
234 return 0;
235}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800236
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800237static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
238{
239 struct bsc_nat_acc_lst_entry *entry;
240
241 llist_for_each_entry(entry, &lst->fltr_list, list) {
242 if (!entry->imsi_allow)
243 continue;
244 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
245 return 0;
246 }
247
248 return 1;
249}
250
251static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
252{
253 struct bsc_nat_acc_lst_entry *entry;
254
255 llist_for_each_entry(entry, &lst->fltr_list, list) {
256 if (!entry->imsi_deny)
257 continue;
258 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
259 return 0;
260 }
261
262 return 1;
263}
264
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800265/* apply white/black list */
266static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
267{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800268 /*
269 * Now apply blacklist/whitelist of the BSC and the NAT.
270 * 1.) Reject if the IMSI is not allowed at the BSC
271 * 2.) Allow directly if the IMSI is allowed at the BSC
272 * 3.) Reject if the IMSI not allowed at the global level.
273 * 4.) Allow directly if the IMSI is allowed at the global level
274 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800275 struct bsc_nat_acc_lst *nat_lst = NULL;
276 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800277
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800278 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
279 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800280
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800281
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800282 if (bsc_lst) {
283 /* 1. BSC deny */
284 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800285 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800286 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800287 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800288 return -2;
289 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800290
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800291 /* 2. BSC allow */
292 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800293 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800294 }
295
296 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800297 if (nat_lst) {
298 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800299 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800300 "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 +0800301 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800302 return -3;
303 }
304 }
305
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800306 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800307}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800308
309static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
310{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800311 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800312 struct gsm48_loc_upd_req *lu;
313 char mi_string[GSM48_MI_SIZE];
314
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800315 if (length < sizeof(*lu)) {
316 LOGP(DNAT, LOGL_ERROR,
317 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800318 return -1;
319 }
320
321 lu = (struct gsm48_loc_upd_req *) data;
322 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
323
324 /*
325 * We can only deal with the IMSI. This will fail for a phone that
326 * will send the TMSI of a previous network to us.
327 */
328 if (mi_type != GSM_MI_TYPE_IMSI)
329 return 0;
330
331 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800332 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800333}
334
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800335static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
336{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800337 static const uint32_t classmark_offset =
338 offsetof(struct gsm48_service_request, classmark);
339
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800340 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800341 uint8_t mi_type;
342 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800343 struct gsm48_service_request *req;
344
345 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800346
347 if (length < sizeof(*req)) {
348 LOGP(DNAT, LOGL_ERROR,
349 "CM Serv Req does not fit. Length is %d\n", length);
350 return -1;
351 }
352
353 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800354 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
355 length - classmark_offset, mi_string, &mi_type);
356 if (rc < 0) {
357 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800358 return -1;
359 }
360
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800361 /* we have to let the TMSI or such pass */
362 if (mi_type != GSM_MI_TYPE_IMSI)
363 return 0;
364
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800365 return auth_imsi(bsc, mi_string);
366}
367
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800368static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
369{
370 struct gsm48_pag_resp *resp;
371 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800372 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800373
374 if (length < sizeof(*resp)) {
375 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
376 return -1;
377 }
378
379 resp = (struct gsm48_pag_resp *) data;
380 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
381 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
382 return -1;
383 }
384
385 /* we need to let it pass for now */
386 if (mi_type != GSM_MI_TYPE_IMSI)
387 return 0;
388
389 return auth_imsi(bsc, mi_string);
390}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800391
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800392static int _dt_check_id_resp(struct bsc_connection *bsc,
393 uint8_t *data, unsigned int length,
394 struct sccp_connections *con)
395{
396 char mi_string[GSM48_MI_SIZE];
397 uint8_t mi_type;
398 int ret;
399
400 if (length < 2) {
401 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
402 return -1;
403 }
404
405 if (data[0] < length - 1) {
406 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
407 return -2;
408 }
409
410 mi_type = data[1] & GSM_MI_TYPE_MASK;
411 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
412
413 if (mi_type != GSM_MI_TYPE_IMSI)
414 return 0;
415
416 ret = auth_imsi(bsc, mi_string);
417 con->imsi_checked = 1;
418 return ret;
419}
420
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800421/* Filter out CR data... */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800422int 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 +0800423{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800424 struct tlv_parsed tp;
425 struct gsm48_hdr *hdr48;
426 int hdr48_len;
427 int len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800428 uint8_t msg_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800429
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800430 *con_type = NAT_CON_TYPE_NONE;
431
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800432 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
433 LOGP(DNAT, LOGL_ERROR,
434 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
435 return -1;
436 }
437
438 /* the parsed has had some basic l3 length check */
439 len = msg->l3h[1];
440 if (msgb_l3len(msg) - 3 < len) {
441 LOGP(DNAT, LOGL_ERROR,
442 "The CR Data has not enough space...\n");
443 return -1;
444 }
445
446 msg->l4h = &msg->l3h[3];
447 len -= 1;
448
449 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
450
451 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
452 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
453 return -1;
454 }
455
456 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
457
458 if (hdr48_len < sizeof(*hdr48)) {
459 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
460 return -1;
461 }
462
463 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
464
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800465 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800466 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800467 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800468 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800469 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800470 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800471 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800472 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800473 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800474 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800475 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800476 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800477 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800478 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800479 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800480 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800481 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800482 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800483}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800484
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800485int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
486 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
487{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800488 uint32_t len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800489 uint8_t msg_type;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800490 struct gsm48_hdr *hdr48;
491
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800492 if (con->imsi_checked)
493 return 0;
494
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800495 /* only care about DTAP messages */
496 if (parsed->bssap != BSSAP_MSG_DTAP)
497 return 0;
498
499 /* gsm_type is actually the size of the dtap */
500 len = parsed->gsm_type;
501 if (len < msgb_l3len(msg) - 3) {
502 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
503 return -1;
504 }
505
506 if (len < sizeof(*hdr48)) {
507 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
508 return -1;
509 }
510
511 msg->l4h = &msg->l3h[3];
512 hdr48 = (struct gsm48_hdr *) msg->l4h;
513
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800514 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800515 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800516 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800517 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
518 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800519 return 0;
520 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800521}
522
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800523void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
524{
525 if (*imsi) {
526 talloc_free(*imsi);
527 *imsi = NULL;
528 }
529 regfree(reg);
530
531 if (argc > 0) {
532 *imsi = talloc_strdup(ctx, argv[0]);
533 regcomp(reg, argv[0], 0);
534 }
535}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800536
537static const char *con_types [] = {
538 [NAT_CON_TYPE_NONE] = "n/a",
539 [NAT_CON_TYPE_LU] = "Location Update",
540 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
541 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800542 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800543 [NAT_CON_TYPE_OTHER] = "Other",
544};
545
546const char *bsc_con_type_to_string(int type)
547{
548 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800549}
550
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800551struct 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 +0800552{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800553 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800554
555 if (!name)
556 return NULL;
557
558 llist_for_each_entry(lst, &nat->access_lists, list)
559 if (strcmp(lst->name, name) == 0)
560 return lst;
561
562 return NULL;
563}
564
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800565struct 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 +0800566{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800567 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800568
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800569 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800570 if (lst)
571 return lst;
572
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800573 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800574 if (!lst) {
575 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
576 return NULL;
577 }
578
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800579 /* TODO: get the index right */
580 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
581 if (!lst->stats) {
582 talloc_free(lst);
583 return NULL;
584 }
585
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800586 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800587 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800588 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800589 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800590}
591
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800592void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800593{
594 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800595 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800596 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800597}
598
599struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
600{
601 struct bsc_nat_acc_lst_entry *entry;
602
603 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
604 if (!entry)
605 return NULL;
606
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800607 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800608 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800609}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800610
611int bsc_nat_msc_is_connected(struct bsc_nat *nat)
612{
613 return nat->msc_con->is_connected;
614}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800615
616static const int con_to_ctr[] = {
617 [NAT_CON_TYPE_NONE] = -1,
618 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
619 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
620 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
621 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
622 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
623};
624
625int bsc_conn_type_to_ctr(struct sccp_connections *conn)
626{
627 return con_to_ctr[conn->con_type];
628}