blob: 4834340c8c7e488a601c91681bd689f3373a7de4 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +02005 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080021 *
22 */
23
24#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080025#include <openbsc/bsc_nat_sccp.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 Freyther3e9a7f82010-10-12 23:21:54 +020030#include <openbsc/vty.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080031
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/linuxlist.h>
33#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010034#include <osmocom/gsm/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080035
Harald Welted36ff762011-03-23 18:26:56 +010036#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +020037#include <osmocom/gsm/protocol/gsm_04_11.h>
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080038
Harald Welted5db12c2010-08-03 15:11:51 +020039#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020040
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020041#include <netinet/in.h>
42#include <arpa/inet.h>
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +020043#include <unistd.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020044
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080045static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-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 Freytheree884962010-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 Freyther463dc622010-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 "},
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +080057 [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080058 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080059};
60
61static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
62 .group_name_prefix = "nat.bsc",
63 .group_description = "NAT BSC Statistics",
64 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
65 .ctr_desc = bsc_cfg_ctr_description,
66};
67
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080068static const struct rate_ctr_desc acc_list_ctr_description[] = {
69 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
70 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
71};
72
73static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
74 .group_name_prefix = "nat.filter",
75 .group_description = "NAT Access-List Statistics",
76 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
77 .ctr_desc = acc_list_ctr_description,
78};
79
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080080struct bsc_nat *bsc_nat_alloc(void)
81{
82 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
83 if (!nat)
84 return NULL;
85
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020086 nat->main_dest = talloc_zero(nat, struct bsc_msc_dest);
87 if (!nat->main_dest) {
88 talloc_free(nat);
89 return NULL;
90 }
91
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080092 INIT_LLIST_HEAD(&nat->sccp_connections);
93 INIT_LLIST_HEAD(&nat->bsc_connections);
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +020094 INIT_LLIST_HEAD(&nat->paging_groups);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080095 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080096 INIT_LLIST_HEAD(&nat->access_lists);
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020097 INIT_LLIST_HEAD(&nat->dests);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +020098 INIT_LLIST_HEAD(&nat->num_rewr);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +020099 INIT_LLIST_HEAD(&nat->smsc_rewr);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200100 INIT_LLIST_HEAD(&nat->tpdest_match);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800101
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200102 nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn");
103 nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls");
104 nat->stats.bsc.reconn = osmo_counter_alloc("nat.bsc.conn");
105 nat->stats.bsc.auth_fail = osmo_counter_alloc("nat.bsc.auth_fail");
106 nat->stats.msc.reconn = osmo_counter_alloc("nat.msc.conn");
107 nat->stats.ussd.reconn = osmo_counter_alloc("nat.ussd.conn");
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800108 nat->auth_timeout = 2;
109 nat->ping_timeout = 20;
110 nat->pong_timeout = 5;
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200111
112 llist_add(&nat->main_dest->list, &nat->dests);
113 nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1");
114 nat->main_dest->port = 5000;
115
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800116 return nat;
117}
118
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800119void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
120{
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200121 bsc_replace_string(nat, &nat->main_dest->ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800122}
123
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800124struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
125{
126 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
127 if (!con)
128 return NULL;
129
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200130 con->nat = nat;
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200131 osmo_wqueue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800132 return con;
133}
134
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800135struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800136{
137 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
138 if (!conf)
139 return NULL;
140
141 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800142 conf->nr = nat->num_bsc;
143 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100144 conf->max_endpoints = 32;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200145 conf->paging_group = PAGIN_GROUP_UNASSIGNED;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800146
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800147 INIT_LLIST_HEAD(&conf->lac_list);
148
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200149 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800150 ++nat->num_bsc;
151
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800152 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800153 if (!conf->stats.ctrg) {
154 talloc_free(conf);
155 return NULL;
156 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200157
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800158 return conf;
159}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200160
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100161void bsc_config_free(struct bsc_config *cfg)
162{
163 rate_ctr_group_free(cfg->stats.ctrg);
164}
165
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200166static void _add_lac(void *ctx, struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800167{
168 struct bsc_lac_entry *lac;
169
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200170 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800171 if (lac->lac == _lac)
172 return;
173
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200174 lac = talloc_zero(ctx, struct bsc_lac_entry);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800175 if (!lac) {
176 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
177 return;
178 }
179
180 lac->lac = _lac;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200181 llist_add_tail(&lac->entry, list);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800182}
183
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200184static void _del_lac(struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800185{
186 struct bsc_lac_entry *lac;
187
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200188 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800189 if (lac->lac == _lac) {
190 llist_del(&lac->entry);
191 talloc_free(lac);
192 return;
193 }
194}
195
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200196void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
197{
198 _add_lac(cfg, &cfg->lac_list, _lac);
199}
200
201void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
202{
203 _del_lac(&cfg->lac_list, _lac);
204}
205
206struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group)
207{
208 struct bsc_nat_paging_group *pgroup;
209
210 pgroup = talloc_zero(nat, struct bsc_nat_paging_group);
211 if (!pgroup) {
212 LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n");
213 return NULL;
214 }
215
216 pgroup->nr = group;
217 INIT_LLIST_HEAD(&pgroup->lists);
218 llist_add_tail(&pgroup->entry, &nat->paging_groups);
219 return pgroup;
220}
221
222void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup)
223{
224 llist_del(&pgroup->entry);
225 talloc_free(pgroup);
226}
227
228struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group)
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200229{
230 struct bsc_nat_paging_group *pgroup;
231
232 llist_for_each_entry(pgroup, &nat->paging_groups, entry)
233 if (pgroup->nr == group)
234 return pgroup;
235
236 return NULL;
237}
238
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200239void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac)
240{
241 _add_lac(pgroup, &pgroup->lists, lac);
242}
243
244void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac)
245{
246 _del_lac(&pgroup->lists, lac);
247}
248
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800249int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
250{
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200251 struct bsc_nat_paging_group *pgroup;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800252 struct bsc_lac_entry *entry;
253
254 llist_for_each_entry(entry, &cfg->lac_list, entry)
255 if (entry->lac == lac_nr)
256 return 1;
257
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200258 /* now lookup the paging group */
259 pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group);
260 if (!pgroup)
261 return 0;
262
263 llist_for_each_entry(entry, &pgroup->lists, entry)
264 if (entry->lac == lac_nr)
265 return 1;
266
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800267 return 0;
268}
269
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200270void sccp_connection_destroy(struct sccp_connections *conn)
271{
272 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
273 sccp_src_ref_to_int(&conn->real_ref),
274 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800275 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200276 llist_del(&conn->list_entry);
277 talloc_free(conn);
278}
279
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200280
281int bsc_nat_find_paging(struct msgb *msg,
282 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200283{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200284 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800285 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200286 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800287
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200288 if (!msg->l3h || msgb_l3len(msg) < 3) {
289 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200290 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200291 }
292
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200293 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
294 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
295 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200296 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200297 }
298
299 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
300 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800301
302 /* No need to try a different BSS */
303 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200304 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800305 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200306 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200307 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200308 }
309
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200310 *out_data = &data[1];
311 *out_leng = data_length - 1;
312 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200313}
314
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800315int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200316{
317 struct msgb *msg;
318
319 if (length > 4096 - 128) {
320 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
321 return -1;
322 }
323
324 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
325 if (!msg) {
326 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
327 return -1;
328 }
329
330 /* copy the data */
331 msg->l3h = msgb_put(msg, length);
332 memcpy(msg->l3h, data, length);
333
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100334 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200335}
336
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200337int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200338{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200339 return bsc_do_write(&bsc->write_queue, msg, proto);
340}
341
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200342int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto)
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200343{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200344 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200345 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200346 return bsc_write_msg(queue, msg);
347}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200348
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200349int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg)
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200350{
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200351 if (osmo_wqueue_enqueue(queue, msg) != 0) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200352 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
353 msgb_free(msg);
354 return -1;
355 }
356
357 return 0;
358}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800359
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200360int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800361{
362 struct bsc_nat_acc_lst_entry *entry;
363
364 llist_for_each_entry(entry, &lst->fltr_list, list) {
365 if (!entry->imsi_allow)
366 continue;
367 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
368 return 0;
369 }
370
371 return 1;
372}
373
374static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
375{
376 struct bsc_nat_acc_lst_entry *entry;
377
378 llist_for_each_entry(entry, &lst->fltr_list, list) {
379 if (!entry->imsi_deny)
380 continue;
381 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
382 return 0;
383 }
384
385 return 1;
386}
387
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800388/* apply white/black list */
389static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
390{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800391 /*
392 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200393 * 1.) Allow directly if the IMSI is allowed at the BSC
394 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800395 * 3.) Reject if the IMSI not allowed at the global level.
396 * 4.) Allow directly if the IMSI is allowed at the global level
397 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800398 struct bsc_nat_acc_lst *nat_lst = NULL;
399 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800400
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800401 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
402 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800403
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800404
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800405 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200406 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200407 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200408 return 1;
409
410 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800411 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800412 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800413 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800414 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800415 return -2;
416 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800417
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800418 }
419
420 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800421 if (nat_lst) {
422 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800423 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800424 "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther0c35b5b2010-10-06 00:18:20 +0800425 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800426 return -3;
427 }
428 }
429
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800430 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800431}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800432
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800433static int _cr_check_loc_upd(struct bsc_connection *bsc,
434 uint8_t *data, unsigned int length,
435 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800436{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800437 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800438 struct gsm48_loc_upd_req *lu;
439 char mi_string[GSM48_MI_SIZE];
440
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800441 if (length < sizeof(*lu)) {
442 LOGP(DNAT, LOGL_ERROR,
443 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800444 return -1;
445 }
446
447 lu = (struct gsm48_loc_upd_req *) data;
448 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
449
450 /*
451 * We can only deal with the IMSI. This will fail for a phone that
452 * will send the TMSI of a previous network to us.
453 */
454 if (mi_type != GSM_MI_TYPE_IMSI)
455 return 0;
456
457 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800458 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800459 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800460}
461
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800462static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
463 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800464 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800465{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800466 static const uint32_t classmark_offset =
467 offsetof(struct gsm48_service_request, classmark);
468
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800469 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800470 uint8_t mi_type;
471 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800472 struct gsm48_service_request *req;
473
474 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800475
476 if (length < sizeof(*req)) {
477 LOGP(DNAT, LOGL_ERROR,
478 "CM Serv Req does not fit. Length is %d\n", length);
479 return -1;
480 }
481
482 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800483 if (req->cm_service_type == 0x8)
484 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800485 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
486 length - classmark_offset, mi_string, &mi_type);
487 if (rc < 0) {
488 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800489 return -1;
490 }
491
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800492 /* we have to let the TMSI or such pass */
493 if (mi_type != GSM_MI_TYPE_IMSI)
494 return 0;
495
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800496 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800497 return auth_imsi(bsc, mi_string);
498}
499
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800500static int _cr_check_pag_resp(struct bsc_connection *bsc,
501 uint8_t *data, unsigned int length,
502 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800503{
504 struct gsm48_pag_resp *resp;
505 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800506 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800507
508 if (length < sizeof(*resp)) {
509 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
510 return -1;
511 }
512
513 resp = (struct gsm48_pag_resp *) data;
514 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
515 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
516 return -1;
517 }
518
519 /* we need to let it pass for now */
520 if (mi_type != GSM_MI_TYPE_IMSI)
521 return 0;
522
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800523 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800524 return auth_imsi(bsc, mi_string);
525}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800526
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800527static int _dt_check_id_resp(struct bsc_connection *bsc,
528 uint8_t *data, unsigned int length,
529 struct sccp_connections *con)
530{
531 char mi_string[GSM48_MI_SIZE];
532 uint8_t mi_type;
533 int ret;
534
535 if (length < 2) {
536 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
537 return -1;
538 }
539
540 if (data[0] < length - 1) {
541 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
542 return -2;
543 }
544
545 mi_type = data[1] & GSM_MI_TYPE_MASK;
546 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
547
548 if (mi_type != GSM_MI_TYPE_IMSI)
549 return 0;
550
551 ret = auth_imsi(bsc, mi_string);
552 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800553 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800554 return ret;
555}
556
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800557/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800558int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
559 struct bsc_nat_parsed *parsed, int *con_type,
560 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800561{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800562 struct tlv_parsed tp;
563 struct gsm48_hdr *hdr48;
564 int hdr48_len;
565 int len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200566 uint8_t msg_type, proto;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800567
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800568 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800569 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800570
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800571 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
572 LOGP(DNAT, LOGL_ERROR,
573 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
574 return -1;
575 }
576
577 /* the parsed has had some basic l3 length check */
578 len = msg->l3h[1];
579 if (msgb_l3len(msg) - 3 < len) {
580 LOGP(DNAT, LOGL_ERROR,
581 "The CR Data has not enough space...\n");
582 return -1;
583 }
584
585 msg->l4h = &msg->l3h[3];
586 len -= 1;
587
588 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
589
590 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
591 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
592 return -1;
593 }
594
595 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
596
597 if (hdr48_len < sizeof(*hdr48)) {
598 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
599 return -1;
600 }
601
602 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
603
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200604 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800605 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200606 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800607 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800608 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800609 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200610 } else if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800611 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800612 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800613 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
614 hdr48_len - sizeof(*hdr48),
615 con_type, imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200616 } else if (proto == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800617 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800618 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800619 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800620 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800621 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800622 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800623 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800624 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800625}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800626
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800627struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
628 struct msgb *msg, uint32_t *len)
629{
630 /* gsm_type is actually the size of the dtap */
631 *len = parsed->gsm_type;
632 if (*len < msgb_l3len(msg) - 3) {
633 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
634 return NULL;
635 }
636
637 if (*len < sizeof(struct gsm48_hdr)) {
638 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
639 return NULL;
640 }
641
642 msg->l4h = &msg->l3h[3];
643 return (struct gsm48_hdr *) msg->l4h;
644}
645
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800646int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
647 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
648{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800649 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200650 uint8_t msg_type, proto;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800651 struct gsm48_hdr *hdr48;
652
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800653 if (con->imsi_checked)
654 return 0;
655
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800656 /* only care about DTAP messages */
657 if (parsed->bssap != BSSAP_MSG_DTAP)
658 return 0;
659
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800660 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
661 if (!hdr48)
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800662 return -1;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800663
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200664 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800665 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200666 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800667 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800668 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
669 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800670 return 0;
671 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800672}
673
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200674int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800675{
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200676 int ret;
677
678 ret = 0;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800679 if (*imsi) {
680 talloc_free(*imsi);
681 *imsi = NULL;
682 }
683 regfree(reg);
684
685 if (argc > 0) {
686 *imsi = talloc_strdup(ctx, argv[0]);
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200687 ret = regcomp(reg, argv[0], 0);
688
689 /* handle compilation failures */
690 if (ret != 0) {
691 talloc_free(*imsi);
692 *imsi = NULL;
693 }
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800694 }
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200695
696 return ret;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800697}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800698
699static const char *con_types [] = {
700 [NAT_CON_TYPE_NONE] = "n/a",
701 [NAT_CON_TYPE_LU] = "Location Update",
702 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
703 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800704 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800705 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800706 [NAT_CON_TYPE_OTHER] = "Other",
707};
708
709const char *bsc_con_type_to_string(int type)
710{
711 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800712}
713
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800714struct 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 +0800715{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800716 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800717
718 if (!name)
719 return NULL;
720
721 llist_for_each_entry(lst, &nat->access_lists, list)
722 if (strcmp(lst->name, name) == 0)
723 return lst;
724
725 return NULL;
726}
727
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800728struct 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 +0800729{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800730 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800731
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800732 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800733 if (lst)
734 return lst;
735
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800736 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800737 if (!lst) {
738 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
739 return NULL;
740 }
741
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800742 /* TODO: get the index right */
743 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
744 if (!lst->stats) {
745 talloc_free(lst);
746 return NULL;
747 }
748
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800749 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800750 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800751 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800752 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800753}
754
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800755void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800756{
757 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800758 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800759 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800760}
761
762struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
763{
764 struct bsc_nat_acc_lst_entry *entry;
765
766 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
767 if (!entry)
768 return NULL;
769
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800770 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800771 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800772}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800773
774int bsc_nat_msc_is_connected(struct bsc_nat *nat)
775{
776 return nat->msc_con->is_connected;
777}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800778
779static const int con_to_ctr[] = {
780 [NAT_CON_TYPE_NONE] = -1,
781 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
782 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
783 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800784 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800785 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
786 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
787};
788
789int bsc_conn_type_to_ctr(struct sccp_connections *conn)
790{
791 return con_to_ctr[conn->con_type];
792}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200793
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200794int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200795{
796 int rc;
797
798 rc = write(bfd->fd, msg->data, msg->len);
799 if (rc != msg->len)
800 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
801
802 return rc;
803}
804
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200805static char *rewrite_non_international(struct bsc_nat *nat, void *ctx, const char *imsi,
806 struct gsm_mncc_number *called)
807{
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200808 struct bsc_nat_num_rewr_entry *entry;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200809 char *new_number = NULL;
810
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200811 if (llist_empty(&nat->num_rewr))
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200812 return NULL;
813
814 if (called->plan != 1)
815 return NULL;
816 if (called->type == 1)
817 return NULL;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200818
819 /* need to find a replacement and then fix it */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200820 llist_for_each_entry(entry, &nat->num_rewr, list) {
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200821 regmatch_t matches[2];
822
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200823 /* check the IMSI match */
824 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200825 continue;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200826
827 /* this regexp matches... */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200828 if (regexec(&entry->num_reg, called->number, 2, matches, 0) == 0 &&
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200829 matches[1].rm_eo != -1)
830 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200831 entry->replace,
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200832 &called->number[matches[1].rm_so]);
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200833 if (new_number)
834 break;
835 }
836
837 return new_number;
838}
839
840
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200841/**
842 * Rewrite non global numbers... according to rules based on the IMSI
843 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200844static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
845 struct bsc_nat_parsed *parsed, const char *imsi,
846 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200847{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200848 struct tlv_parsed tp;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200849 unsigned int payload_len;
850 struct gsm_mncc_number called;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200851 struct msgb *out;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200852 char *new_number = NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200853 uint8_t *outptr;
854 const uint8_t *msgptr;
855 int sec_len;
856
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200857 /* decode and rewrite the message */
858 payload_len = len - sizeof(*hdr48);
859 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
860
861 /* no number, well let us ignore it */
862 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200863 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200864
865 memset(&called, 0, sizeof(called));
866 gsm48_decode_called(&called,
867 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
868
869 /* check if it looks international and stop */
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200870 new_number = rewrite_non_international(nat, msg, imsi, &called);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200871
872 if (!new_number) {
873 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200874 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200875 }
876
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100877 if (strlen(new_number) > sizeof(called.number)) {
878 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
879 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200880 return NULL;
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100881 }
882
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200883 /*
884 * Need to create a new message now based on the old onew
885 * with a new number. We can sadly not patch this in place
886 * so we will need to regenerate it.
887 */
888
889 out = msgb_alloc_headroom(4096, 128, "changed-setup");
890 if (!out) {
891 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
892 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200893 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200894 }
895
896 /* copy the header */
897 outptr = msgb_put(out, sizeof(*hdr48));
898 memcpy(outptr, hdr48, sizeof(*hdr48));
899
900 /* copy everything up to the number */
901 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
902 outptr = msgb_put(out, sec_len);
903 memcpy(outptr, &hdr48->data[0], sec_len);
904
905 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100906 if (strncmp(new_number, "00", 2) == 0) {
907 called.type = 1;
908 strncpy(called.number, new_number + 2, sizeof(called.number));
909 } else {
910 strncpy(called.number, new_number, sizeof(called.number));
911 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200912 gsm48_encode_called(out, &called);
913
914 /* copy thre rest */
915 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
916 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
917 sec_len = payload_len - (msgptr - &hdr48->data[0]);
918 outptr = msgb_put(out, sec_len);
919 memcpy(outptr, msgptr, sec_len);
920
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200921 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200922 return out;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200923}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200924
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200925static struct msgb *rewrite_smsc(struct bsc_nat *nat, struct msgb *msg,
926 struct bsc_nat_parsed *parsed, const char *imsi,
927 struct gsm48_hdr *hdr48, const uint32_t len)
928{
929 unsigned int payload_len;
930 unsigned int cp_len;
931
932 uint8_t ref;
933 uint8_t orig_addr_len, *orig_addr_ptr;
934 uint8_t dest_addr_len, *dest_addr_ptr;
935 uint8_t data_len, *data_ptr;
936 char smsc_addr[30];
937 uint8_t new_addr[12];
938
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200939
940 uint8_t dest_len;
941 char _dest_nr[30];
942 char *dest_nr;
943 uint8_t dest_match = 0;
944
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200945 struct bsc_nat_num_rewr_entry *entry;
946 char *new_number = NULL;
947 uint8_t new_addr_len;
948 struct gsm48_hdr *new_hdr48;
949 struct msgb *out;
950
951 payload_len = len - sizeof(*hdr48);
952 if (payload_len < 1) {
953 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
954 return NULL;
955 }
956
957 cp_len = hdr48->data[0];
958 if (payload_len + 1 < cp_len) {
959 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
960 return NULL;
961 }
962
963 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
964 return NULL;
965
966 if (cp_len < 5) {
967 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
968 return NULL;
969 }
970
971 ref = hdr48->data[2];
972 orig_addr_len = hdr48->data[3];
973 orig_addr_ptr = &hdr48->data[4];
974
975 /* the +1 is for checking if the following element has some space */
976 if (cp_len < 3 + orig_addr_len + 1) {
977 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
978 return NULL;
979 }
980
981 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
982 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
983
984 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
985 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
986 return NULL;
987 }
988 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
989
990 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
991 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
992
993 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
994 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
995 return NULL;
996 }
997
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200998 /* look into the phone number */
999 if ((data_ptr[0] & 0x01) != 1)
1000 return NULL;
1001
1002 if (data_len < 3) {
1003 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
1004 return NULL;
1005 }
1006
1007 dest_len = data_ptr[2];
1008 if (data_len < dest_len + 3 || dest_len < 2) {
1009 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
1010 return NULL;
1011 }
1012
1013 if ((data_ptr[3] & 0x80) == 0) {
1014 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
1015 return NULL;
1016 }
1017
1018 if ((data_ptr[3] & 0x0F) == 0) {
1019 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is not a ISDN number.\n");
1020 return NULL;
1021 }
1022
1023 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
1024 &data_ptr[2], 1);
1025 if ((data_ptr[3] & 0x70) == 0x10) {
1026 _dest_nr[0] = _dest_nr[1] = '0';
1027 dest_nr = &_dest_nr[0];
1028 } else {
1029 dest_nr = &_dest_nr[2];
1030 }
1031
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001032 /* We will find a new number now */
1033 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
1034 regmatch_t matches[2];
1035
1036 /* check the IMSI match */
1037 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
1038 continue;
1039
1040 /* this regexp matches... */
1041 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
1042 matches[1].rm_eo != -1)
1043 new_number = talloc_asprintf(msg, "%s%s",
1044 entry->replace,
1045 &smsc_addr[matches[1].rm_so]);
1046 if (new_number)
1047 break;
1048 }
1049
1050 if (!new_number)
1051 return NULL;
1052
1053 /*
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001054 * now match the number against another list
1055 */
1056 llist_for_each_entry(entry, &nat->tpdest_match, list) {
1057 /* check the IMSI match */
1058 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
1059 continue;
1060
1061 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
1062 dest_match =1;
1063 break;
1064 }
1065 }
1066
1067 if (!dest_match) {
1068 talloc_free(new_number);
1069 return NULL;
1070 }
1071
1072 /*
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001073 * We need to re-create the patched structure. This is why we have
1074 * saved the above pointers.
1075 */
1076 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
1077 if (!out) {
1078 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1079 return NULL;
1080 }
1081
1082 out->l3h = out->data;
1083 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
1084 msgb_v_put(out, ref);
1085 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
1086
1087 /*
1088 * Copy the new number. We let libosmocore encode it, then set
1089 * the extension followed after the length. For our convenience
1090 * we let the TLV code re-add the length so we start copying
1091 * from &new_addr[1].
1092 */
1093 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
1094 1, new_number);
1095 new_addr[1] = 0x91;
1096 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
1097
1098 msgb_lv_put(out, data_len, data_ptr);
1099
1100 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*hdr48) + 1);
1101 memcpy(new_hdr48, hdr48, sizeof(*hdr48));
1102 new_hdr48->data[0] = msgb_l3len(out);
1103
1104 talloc_free(new_number);
1105 return out;
1106}
1107
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001108struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
1109{
1110 struct gsm48_hdr *hdr48;
1111 uint32_t len;
1112 uint8_t msg_type, proto;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001113 struct msgb *new_msg = NULL, *sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001114
1115 if (!imsi || strlen(imsi) < 5)
1116 return msg;
1117
1118 /* only care about DTAP messages */
1119 if (parsed->bssap != BSSAP_MSG_DTAP)
1120 return msg;
1121 if (!parsed->dest_local_ref)
1122 return msg;
1123
1124 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
1125 if (!hdr48)
1126 return msg;
1127
1128 proto = hdr48->proto_discr & 0x0f;
1129 msg_type = hdr48->msg_type & 0xbf;
1130
1131 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001132 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001133 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
1134 new_msg = rewrite_smsc(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001135
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001136 if (!new_msg)
1137 return msg;
1138
1139 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
1140 gsm0808_prepend_dtap_header(new_msg, 0);
1141 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
1142 talloc_free(new_msg);
1143
1144 if (!sccp) {
1145 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1146 return msg;
1147 }
1148
1149 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
1150
1151 /* the parsed hangs off from msg but it needs to survive */
1152 talloc_steal(sccp, parsed);
1153 msgb_free(msg);
1154 return sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001155}
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001156
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001157static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
1158{
1159 regfree(&entry->msisdn_reg);
1160 regfree(&entry->num_reg);
1161 talloc_free(entry->replace);
1162}
1163
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001164void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
1165 const struct osmo_config_list *list)
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001166{
1167 struct bsc_nat_num_rewr_entry *entry, *tmp;
1168 struct osmo_config_entry *cfg_entry;
1169
1170 /* free the old data */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001171 llist_for_each_entry_safe(entry, tmp, head, list) {
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001172 num_rewr_free_data(entry);
1173 llist_del(&entry->list);
1174 talloc_free(entry);
1175 }
1176
1177
1178 if (!list)
1179 return;
1180
1181 llist_for_each_entry(cfg_entry, &list->entry, list) {
1182 char *regexp;
1183 if (cfg_entry->text[0] == '+') {
1184 LOGP(DNAT, LOGL_ERROR,
1185 "Plus is not allowed in the number\n");
1186 continue;
1187 }
1188
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001189 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001190 if (!entry) {
1191 LOGP(DNAT, LOGL_ERROR,
1192 "Allication of the num_rewr entry failed.\n");
1193 continue;
1194 }
1195
1196 entry->replace = talloc_strdup(entry, cfg_entry->text);
1197 if (!entry->replace) {
1198 LOGP(DNAT, LOGL_ERROR,
1199 "Failed to copy the replacement text.\n");
1200 talloc_free(entry);
1201 continue;
1202 }
1203
1204 /* we will now build a regexp string */
1205 if (cfg_entry->mcc[0] == '^') {
1206 regexp = talloc_strdup(entry, cfg_entry->mcc);
1207 } else {
1208 regexp = talloc_asprintf(entry, "^%s%s",
1209 cfg_entry->mcc[0] == '*' ?
1210 "[0-9][0-9][0-9]" : cfg_entry->mcc,
1211 cfg_entry->mnc[0] == '*' ?
1212 "[0-9][0-9]" : cfg_entry->mnc);
1213 }
1214
1215 if (!regexp) {
1216 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
1217 talloc_free(entry);
1218 continue;
1219 }
1220
1221 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
1222 LOGP(DNAT, LOGL_ERROR,
1223 "Failed to compile regexp '%s'\n", regexp);
1224 talloc_free(regexp);
1225 talloc_free(entry);
1226 continue;
1227 }
1228
1229 talloc_free(regexp);
1230 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
1231 LOGP(DNAT, LOGL_ERROR,
1232 "Failed to compile regexp '%s\n'", cfg_entry->option);
1233 regfree(&entry->msisdn_reg);
1234 talloc_free(entry);
1235 continue;
1236 }
1237
1238 /* we have copied the number */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001239 llist_add_tail(&entry->list, head);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001240 }
1241}