blob: 9fc057d9ab72da087daa4428d8f7bac13d97d3ea [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>
26#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020027#include <openbsc/bssap.h>
28#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 Freyther23fe7be2010-03-30 10:45:48 +020035#include <sccp/sccp.h>
36
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020037#include <netinet/in.h>
38#include <arpa/inet.h>
39
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080040struct bsc_nat *bsc_nat_alloc(void)
41{
42 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
43 if (!nat)
44 return NULL;
45
46 INIT_LLIST_HEAD(&nat->sccp_connections);
47 INIT_LLIST_HEAD(&nat->bsc_connections);
48 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080049 INIT_LLIST_HEAD(&nat->access_lists);
50
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020051 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
52 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
53 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
54 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
55 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080056 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020057 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080058 nat->auth_timeout = 2;
59 nat->ping_timeout = 20;
60 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080061 return nat;
62}
63
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080064void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
65{
66 if (nat->msc_ip)
67 talloc_free(nat->msc_ip);
68 nat->msc_ip = talloc_strdup(nat, ip);
69}
70
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080071struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
72{
73 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
74 if (!con)
75 return NULL;
76
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020077 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +080078 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080079 return con;
80}
81
82struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
83{
84 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
85 if (!conf)
86 return NULL;
87
88 conf->token = talloc_strdup(conf, token);
89 conf->lac = lac;
90 conf->nr = nat->num_bsc;
91 conf->nat = nat;
92
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020093 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080094 ++nat->num_bsc;
95
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020096 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
97 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
98 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
99
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800100 return conf;
101}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200102
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200103void sccp_connection_destroy(struct sccp_connections *conn)
104{
105 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
106 sccp_src_ref_to_int(&conn->real_ref),
107 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800108 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200109 llist_del(&conn->list_entry);
110 talloc_free(conn);
111}
112
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200113struct 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 +0200114{
115 struct bsc_connection *bsc;
116 int data_length;
117 const u_int8_t *data;
118 struct tlv_parsed tp;
119 int i = 0;
120
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800121 *lac_out = -1;
122
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200123 if (!msg->l3h || msgb_l3len(msg) < 3) {
124 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
125 return NULL;
126 }
127
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200128 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
129 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
130 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
131 return NULL;
132 }
133
134 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
135 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800136
137 /* No need to try a different BSS */
138 if (data[0] == CELL_IDENT_BSS) {
139 return NULL;
140 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200141 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200142 return NULL;
143 }
144
145 /* Currently we only handle one BSC */
146 for (i = 1; i < data_length - 1; i += 2) {
147 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200148 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200149 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200150 if (!bsc->cfg)
151 continue;
152 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200153 continue;
154
155 return bsc;
156 }
157 }
158
159 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200160}
161
162int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
163{
164 struct msgb *msg;
165
166 if (length > 4096 - 128) {
167 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
168 return -1;
169 }
170
171 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
172 if (!msg) {
173 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
174 return -1;
175 }
176
177 /* copy the data */
178 msg->l3h = msgb_put(msg, length);
179 memcpy(msg->l3h, data, length);
180
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200181 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200182}
183
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200184int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200185{
186 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200187 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200188
189 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
190 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
191 msgb_free(msg);
192 return -1;
193 }
194
195 return 0;
196}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800197
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800198static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
199{
200 struct bsc_nat_acc_lst_entry *entry;
201
202 llist_for_each_entry(entry, &lst->fltr_list, list) {
203 if (!entry->imsi_allow)
204 continue;
205 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
206 return 0;
207 }
208
209 return 1;
210}
211
212static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
213{
214 struct bsc_nat_acc_lst_entry *entry;
215
216 llist_for_each_entry(entry, &lst->fltr_list, list) {
217 if (!entry->imsi_deny)
218 continue;
219 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
220 return 0;
221 }
222
223 return 1;
224}
225
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800226/* apply white/black list */
227static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
228{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800229 /*
230 * Now apply blacklist/whitelist of the BSC and the NAT.
231 * 1.) Reject if the IMSI is not allowed at the BSC
232 * 2.) Allow directly if the IMSI is allowed at the BSC
233 * 3.) Reject if the IMSI not allowed at the global level.
234 * 4.) Allow directly if the IMSI is allowed at the global level
235 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800236 struct bsc_nat_acc_lst *nat_lst = NULL;
237 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800238
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800239 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
240 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800241
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800242
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800243 if (bsc_lst) {
244 /* 1. BSC deny */
245 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800246 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800247 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800248 return -2;
249 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800250
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800251 /* 2. BSC allow */
252 if (lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800253 return 0;
254 }
255
256 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800257 if (nat_lst) {
258 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800259 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800260 "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 +0800261 return -3;
262 }
263 }
264
Holger Hans Peter Freytherf1012a42010-05-15 00:36:54 +0800265 return 0;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800266}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800267
268static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
269{
270 u_int8_t mi_type;
271 struct gsm48_loc_upd_req *lu;
272 char mi_string[GSM48_MI_SIZE];
273
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800274 if (length < sizeof(*lu)) {
275 LOGP(DNAT, LOGL_ERROR,
276 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800277 return -1;
278 }
279
280 lu = (struct gsm48_loc_upd_req *) data;
281 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
282
283 /*
284 * We can only deal with the IMSI. This will fail for a phone that
285 * will send the TMSI of a previous network to us.
286 */
287 if (mi_type != GSM_MI_TYPE_IMSI)
288 return 0;
289
290 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800291 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800292}
293
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800294static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
295{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800296 static const uint32_t classmark_offset =
297 offsetof(struct gsm48_service_request, classmark);
298
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800299 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800300 uint8_t mi_type;
301 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800302 struct gsm48_service_request *req;
303
304 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800305
306 if (length < sizeof(*req)) {
307 LOGP(DNAT, LOGL_ERROR,
308 "CM Serv Req does not fit. Length is %d\n", length);
309 return -1;
310 }
311
312 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800313 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
314 length - classmark_offset, mi_string, &mi_type);
315 if (rc < 0) {
316 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800317 return -1;
318 }
319
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800320 /* we have to let the TMSI or such pass */
321 if (mi_type != GSM_MI_TYPE_IMSI)
322 return 0;
323
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800324 return auth_imsi(bsc, mi_string);
325}
326
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800327static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
328{
329 struct gsm48_pag_resp *resp;
330 char mi_string[GSM48_MI_SIZE];
331 u_int8_t mi_type;
332
333 if (length < sizeof(*resp)) {
334 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
335 return -1;
336 }
337
338 resp = (struct gsm48_pag_resp *) data;
339 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
340 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
341 return -1;
342 }
343
344 /* we need to let it pass for now */
345 if (mi_type != GSM_MI_TYPE_IMSI)
346 return 0;
347
348 return auth_imsi(bsc, mi_string);
349}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800350
351/* Filter out CR data... */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800352int 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 +0800353{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800354 struct tlv_parsed tp;
355 struct gsm48_hdr *hdr48;
356 int hdr48_len;
357 int len;
358
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800359 *con_type = NAT_CON_TYPE_NONE;
360
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800361 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
362 LOGP(DNAT, LOGL_ERROR,
363 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
364 return -1;
365 }
366
367 /* the parsed has had some basic l3 length check */
368 len = msg->l3h[1];
369 if (msgb_l3len(msg) - 3 < len) {
370 LOGP(DNAT, LOGL_ERROR,
371 "The CR Data has not enough space...\n");
372 return -1;
373 }
374
375 msg->l4h = &msg->l3h[3];
376 len -= 1;
377
378 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
379
380 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
381 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
382 return -1;
383 }
384
385 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
386
387 if (hdr48_len < sizeof(*hdr48)) {
388 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
389 return -1;
390 }
391
392 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
393
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800394 if (hdr48->proto_discr == GSM48_PDISC_MM &&
395 hdr48->msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800396 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800397 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800398 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
399 hdr48->msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800400 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800401 return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800402 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
403 hdr48->msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800404 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800405 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800406 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800407 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800408 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800409 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800410 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800411}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800412
413void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
414{
415 if (*imsi) {
416 talloc_free(*imsi);
417 *imsi = NULL;
418 }
419 regfree(reg);
420
421 if (argc > 0) {
422 *imsi = talloc_strdup(ctx, argv[0]);
423 regcomp(reg, argv[0], 0);
424 }
425}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800426
427static const char *con_types [] = {
428 [NAT_CON_TYPE_NONE] = "n/a",
429 [NAT_CON_TYPE_LU] = "Location Update",
430 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
431 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800432 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800433 [NAT_CON_TYPE_OTHER] = "Other",
434};
435
436const char *bsc_con_type_to_string(int type)
437{
438 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800439}
440
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800441struct 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 +0800442{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800443 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800444
445 if (!name)
446 return NULL;
447
448 llist_for_each_entry(lst, &nat->access_lists, list)
449 if (strcmp(lst->name, name) == 0)
450 return lst;
451
452 return NULL;
453}
454
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800455struct 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 +0800456{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800457 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800458
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800459 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800460 if (lst)
461 return lst;
462
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800463 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800464 if (!lst) {
465 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
466 return NULL;
467 }
468
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800469 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800470 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800471 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800472 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800473}
474
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800475void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800476{
477 llist_del(&lst->list);
478 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800479}
480
481struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
482{
483 struct bsc_nat_acc_lst_entry *entry;
484
485 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
486 if (!entry)
487 return NULL;
488
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800489 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800490 return entry;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800491}