blob: 39de817387ce3f9b927abca120f4541478920abc [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 Freyther20ee3122010-07-05 14:39:44 +080026#include <openbsc/bsc_msc.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080027#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020028#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020029#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080030
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020031#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080033#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080034
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080035#include <osmocore/protocol/gsm_08_08.h>
36
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020037#include <sccp/sccp.h>
38
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020039#include <netinet/in.h>
40#include <arpa/inet.h>
41
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080042
43static 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 Freytherb2c38eb2010-06-17 18:16:00 +080049};
50
51static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
52 .group_name_prefix = "nat.bsc",
53 .group_description = "NAT BSC Statistics",
54 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
55 .ctr_desc = bsc_cfg_ctr_description,
56};
57
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080058struct bsc_nat *bsc_nat_alloc(void)
59{
60 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
61 if (!nat)
62 return NULL;
63
64 INIT_LLIST_HEAD(&nat->sccp_connections);
65 INIT_LLIST_HEAD(&nat->bsc_connections);
66 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080067 INIT_LLIST_HEAD(&nat->access_lists);
68
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020069 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
70 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
71 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
72 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
73 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080074 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020075 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080076 nat->auth_timeout = 2;
77 nat->ping_timeout = 20;
78 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080079 return nat;
80}
81
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080082void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
83{
84 if (nat->msc_ip)
85 talloc_free(nat->msc_ip);
86 nat->msc_ip = talloc_strdup(nat, ip);
87}
88
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080089struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
90{
91 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
92 if (!con)
93 return NULL;
94
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020095 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +080096 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080097 return con;
98}
99
100struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
101{
102 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
103 if (!conf)
104 return NULL;
105
106 conf->token = talloc_strdup(conf, token);
107 conf->lac = lac;
108 conf->nr = nat->num_bsc;
109 conf->nat = nat;
110
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200111 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800112 ++nat->num_bsc;
113
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800114 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->lac);
115 if (!conf->stats.ctrg) {
116 talloc_free(conf);
117 return NULL;
118 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200119
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800120 return conf;
121}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200122
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200123void sccp_connection_destroy(struct sccp_connections *conn)
124{
125 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
126 sccp_src_ref_to_int(&conn->real_ref),
127 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800128 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200129 llist_del(&conn->list_entry);
130 talloc_free(conn);
131}
132
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200133struct 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 +0200134{
135 struct bsc_connection *bsc;
136 int data_length;
137 const u_int8_t *data;
138 struct tlv_parsed tp;
139 int i = 0;
140
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800141 *lac_out = -1;
142
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200143 if (!msg->l3h || msgb_l3len(msg) < 3) {
144 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
145 return NULL;
146 }
147
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200148 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
149 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
150 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
151 return NULL;
152 }
153
154 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
155 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800156
157 /* No need to try a different BSS */
158 if (data[0] == CELL_IDENT_BSS) {
159 return NULL;
160 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200161 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200162 return NULL;
163 }
164
165 /* Currently we only handle one BSC */
166 for (i = 1; i < data_length - 1; i += 2) {
167 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200168 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200169 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200170 if (!bsc->cfg)
171 continue;
172 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200173 continue;
174
175 return bsc;
176 }
177 }
178
179 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200180}
181
182int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
183{
184 struct msgb *msg;
185
186 if (length > 4096 - 128) {
187 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
188 return -1;
189 }
190
191 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
192 if (!msg) {
193 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
194 return -1;
195 }
196
197 /* copy the data */
198 msg->l3h = msgb_put(msg, length);
199 memcpy(msg->l3h, data, length);
200
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200201 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200202}
203
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200204int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200205{
206 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200207 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200208
209 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
210 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
211 msgb_free(msg);
212 return -1;
213 }
214
215 return 0;
216}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800217
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800218static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
219{
220 struct bsc_nat_acc_lst_entry *entry;
221
222 llist_for_each_entry(entry, &lst->fltr_list, list) {
223 if (!entry->imsi_allow)
224 continue;
225 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
226 return 0;
227 }
228
229 return 1;
230}
231
232static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
233{
234 struct bsc_nat_acc_lst_entry *entry;
235
236 llist_for_each_entry(entry, &lst->fltr_list, list) {
237 if (!entry->imsi_deny)
238 continue;
239 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
240 return 0;
241 }
242
243 return 1;
244}
245
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800246/* apply white/black list */
247static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
248{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800249 /*
250 * Now apply blacklist/whitelist of the BSC and the NAT.
251 * 1.) Reject if the IMSI is not allowed at the BSC
252 * 2.) Allow directly if the IMSI is allowed at the BSC
253 * 3.) Reject if the IMSI not allowed at the global level.
254 * 4.) Allow directly if the IMSI is allowed at the global level
255 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800256 struct bsc_nat_acc_lst *nat_lst = NULL;
257 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800258
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800259 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
260 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800261
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800262
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800263 if (bsc_lst) {
264 /* 1. BSC deny */
265 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800266 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800267 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800268 return -2;
269 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800270
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800271 /* 2. BSC allow */
272 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800273 return 0;
274 }
275
276 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800277 if (nat_lst) {
278 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800279 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800280 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800281 return -3;
282 }
283 }
284
Holger Hans Peter Freytherf1012a42010-05-15 00:36:54 +0800285 return 0;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800286}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800287
288static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
289{
290 u_int8_t mi_type;
291 struct gsm48_loc_upd_req *lu;
292 char mi_string[GSM48_MI_SIZE];
293
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800294 if (length < sizeof(*lu)) {
295 LOGP(DNAT, LOGL_ERROR,
296 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800297 return -1;
298 }
299
300 lu = (struct gsm48_loc_upd_req *) data;
301 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
302
303 /*
304 * We can only deal with the IMSI. This will fail for a phone that
305 * will send the TMSI of a previous network to us.
306 */
307 if (mi_type != GSM_MI_TYPE_IMSI)
308 return 0;
309
310 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800311 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800312}
313
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800314static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
315{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800316 static const uint32_t classmark_offset =
317 offsetof(struct gsm48_service_request, classmark);
318
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800319 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800320 uint8_t mi_type;
321 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800322 struct gsm48_service_request *req;
323
324 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800325
326 if (length < sizeof(*req)) {
327 LOGP(DNAT, LOGL_ERROR,
328 "CM Serv Req does not fit. Length is %d\n", length);
329 return -1;
330 }
331
332 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800333 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
334 length - classmark_offset, mi_string, &mi_type);
335 if (rc < 0) {
336 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800337 return -1;
338 }
339
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800340 /* we have to let the TMSI or such pass */
341 if (mi_type != GSM_MI_TYPE_IMSI)
342 return 0;
343
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800344 return auth_imsi(bsc, mi_string);
345}
346
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800347static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
348{
349 struct gsm48_pag_resp *resp;
350 char mi_string[GSM48_MI_SIZE];
351 u_int8_t mi_type;
352
353 if (length < sizeof(*resp)) {
354 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
355 return -1;
356 }
357
358 resp = (struct gsm48_pag_resp *) data;
359 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
360 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
361 return -1;
362 }
363
364 /* we need to let it pass for now */
365 if (mi_type != GSM_MI_TYPE_IMSI)
366 return 0;
367
368 return auth_imsi(bsc, mi_string);
369}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800370
371/* Filter out CR data... */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800372int 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 +0800373{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800374 struct tlv_parsed tp;
375 struct gsm48_hdr *hdr48;
376 int hdr48_len;
377 int len;
378
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800379 *con_type = NAT_CON_TYPE_NONE;
380
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800381 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
382 LOGP(DNAT, LOGL_ERROR,
383 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
384 return -1;
385 }
386
387 /* the parsed has had some basic l3 length check */
388 len = msg->l3h[1];
389 if (msgb_l3len(msg) - 3 < len) {
390 LOGP(DNAT, LOGL_ERROR,
391 "The CR Data has not enough space...\n");
392 return -1;
393 }
394
395 msg->l4h = &msg->l3h[3];
396 len -= 1;
397
398 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
399
400 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
401 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
402 return -1;
403 }
404
405 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
406
407 if (hdr48_len < sizeof(*hdr48)) {
408 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
409 return -1;
410 }
411
412 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
413
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800414 if (hdr48->proto_discr == GSM48_PDISC_MM &&
415 hdr48->msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800416 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800417 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800418 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
419 hdr48->msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800420 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800421 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800422 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
423 hdr48->msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800424 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800425 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800426 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800427 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800428 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800429 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800430 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800431}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800432
433void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
434{
435 if (*imsi) {
436 talloc_free(*imsi);
437 *imsi = NULL;
438 }
439 regfree(reg);
440
441 if (argc > 0) {
442 *imsi = talloc_strdup(ctx, argv[0]);
443 regcomp(reg, argv[0], 0);
444 }
445}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800446
447static const char *con_types [] = {
448 [NAT_CON_TYPE_NONE] = "n/a",
449 [NAT_CON_TYPE_LU] = "Location Update",
450 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
451 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800452 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800453 [NAT_CON_TYPE_OTHER] = "Other",
454};
455
456const char *bsc_con_type_to_string(int type)
457{
458 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800459}
460
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800461struct 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 +0800462{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800463 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800464
465 if (!name)
466 return NULL;
467
468 llist_for_each_entry(lst, &nat->access_lists, list)
469 if (strcmp(lst->name, name) == 0)
470 return lst;
471
472 return NULL;
473}
474
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800475struct 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 +0800476{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800477 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800478
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800479 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800480 if (lst)
481 return lst;
482
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800483 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800484 if (!lst) {
485 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
486 return NULL;
487 }
488
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800489 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800490 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800491 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800492 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800493}
494
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800495void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800496{
497 llist_del(&lst->list);
498 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800499}
500
501struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
502{
503 struct bsc_nat_acc_lst_entry *entry;
504
505 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
506 if (!entry)
507 return NULL;
508
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800509 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800510 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800511}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800512
513int bsc_nat_msc_is_connected(struct bsc_nat *nat)
514{
515 return nat->msc_con->is_connected;
516}