blob: d4798c71fa0576784cfe60cd4575679487d91cbf [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
Holger Hans Peter Freythere18801052011-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 Freythere18801052011-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 Freythere18801052011-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 Freythere18801052011-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 Freythere18801052011-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);
Daniel Willmanna86bc392011-02-18 14:32:56 +0100132 INIT_LLIST_HEAD(&con->cmd_pending);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800133 return con;
134}
135
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800136struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800137{
138 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
139 if (!conf)
140 return NULL;
141
142 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800143 conf->nr = nat->num_bsc;
144 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100145 conf->max_endpoints = 32;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200146 conf->paging_group = PAGIN_GROUP_UNASSIGNED;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800147
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800148 INIT_LLIST_HEAD(&conf->lac_list);
149
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200150 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800151 ++nat->num_bsc;
152
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800153 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800154 if (!conf->stats.ctrg) {
155 talloc_free(conf);
156 return NULL;
157 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200158
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800159 return conf;
160}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200161
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100162void bsc_config_free(struct bsc_config *cfg)
163{
164 rate_ctr_group_free(cfg->stats.ctrg);
165}
166
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200167static void _add_lac(void *ctx, struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800168{
169 struct bsc_lac_entry *lac;
170
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200171 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800172 if (lac->lac == _lac)
173 return;
174
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200175 lac = talloc_zero(ctx, struct bsc_lac_entry);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800176 if (!lac) {
177 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
178 return;
179 }
180
181 lac->lac = _lac;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200182 llist_add_tail(&lac->entry, list);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800183}
184
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200185static void _del_lac(struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800186{
187 struct bsc_lac_entry *lac;
188
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200189 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800190 if (lac->lac == _lac) {
191 llist_del(&lac->entry);
192 talloc_free(lac);
193 return;
194 }
195}
196
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200197void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
198{
199 _add_lac(cfg, &cfg->lac_list, _lac);
200}
201
202void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
203{
204 _del_lac(&cfg->lac_list, _lac);
205}
206
207struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group)
208{
209 struct bsc_nat_paging_group *pgroup;
210
211 pgroup = talloc_zero(nat, struct bsc_nat_paging_group);
212 if (!pgroup) {
213 LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n");
214 return NULL;
215 }
216
217 pgroup->nr = group;
218 INIT_LLIST_HEAD(&pgroup->lists);
219 llist_add_tail(&pgroup->entry, &nat->paging_groups);
220 return pgroup;
221}
222
223void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup)
224{
225 llist_del(&pgroup->entry);
226 talloc_free(pgroup);
227}
228
229struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group)
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200230{
231 struct bsc_nat_paging_group *pgroup;
232
233 llist_for_each_entry(pgroup, &nat->paging_groups, entry)
234 if (pgroup->nr == group)
235 return pgroup;
236
237 return NULL;
238}
239
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200240void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac)
241{
242 _add_lac(pgroup, &pgroup->lists, lac);
243}
244
245void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac)
246{
247 _del_lac(&pgroup->lists, lac);
248}
249
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800250int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
251{
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200252 struct bsc_nat_paging_group *pgroup;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800253 struct bsc_lac_entry *entry;
254
255 llist_for_each_entry(entry, &cfg->lac_list, entry)
256 if (entry->lac == lac_nr)
257 return 1;
258
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200259 /* now lookup the paging group */
260 pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group);
261 if (!pgroup)
262 return 0;
263
264 llist_for_each_entry(entry, &pgroup->lists, entry)
265 if (entry->lac == lac_nr)
266 return 1;
267
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800268 return 0;
269}
270
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200271void sccp_connection_destroy(struct sccp_connections *conn)
272{
273 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
274 sccp_src_ref_to_int(&conn->real_ref),
275 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800276 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200277 llist_del(&conn->list_entry);
278 talloc_free(conn);
279}
280
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200281
282int bsc_nat_find_paging(struct msgb *msg,
283 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200284{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200285 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800286 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200287 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800288
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200289 if (!msg->l3h || msgb_l3len(msg) < 3) {
290 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200291 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200292 }
293
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200294 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
295 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
296 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200297 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200298 }
299
300 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
301 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800302
303 /* No need to try a different BSS */
304 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200305 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800306 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200307 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200308 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200309 }
310
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200311 *out_data = &data[1];
312 *out_leng = data_length - 1;
313 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200314}
315
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800316int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200317{
318 struct msgb *msg;
319
320 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200321 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200322 return -1;
323 }
324
325 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
326 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200327 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200328 return -1;
329 }
330
331 /* copy the data */
332 msg->l3h = msgb_put(msg, length);
333 memcpy(msg->l3h, data, length);
334
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100335 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200336}
337
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200338int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200339{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200340 return bsc_do_write(&bsc->write_queue, msg, proto);
341}
342
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200343int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto)
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200344{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200345 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200346 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200347 return bsc_write_msg(queue, msg);
348}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200349
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200350int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg)
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200351{
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200352 if (osmo_wqueue_enqueue(queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200353 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200354 msgb_free(msg);
355 return -1;
356 }
357
358 return 0;
359}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800360
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200361int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800362{
363 struct bsc_nat_acc_lst_entry *entry;
364
365 llist_for_each_entry(entry, &lst->fltr_list, list) {
366 if (!entry->imsi_allow)
367 continue;
368 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
369 return 0;
370 }
371
372 return 1;
373}
374
375static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
376{
377 struct bsc_nat_acc_lst_entry *entry;
378
379 llist_for_each_entry(entry, &lst->fltr_list, list) {
380 if (!entry->imsi_deny)
381 continue;
382 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
383 return 0;
384 }
385
386 return 1;
387}
388
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800389/* apply white/black list */
390static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
391{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800392 /*
393 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200394 * 1.) Allow directly if the IMSI is allowed at the BSC
395 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800396 * 3.) Reject if the IMSI not allowed at the global level.
397 * 4.) Allow directly if the IMSI is allowed at the global level
398 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800399 struct bsc_nat_acc_lst *nat_lst = NULL;
400 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800401
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800402 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
403 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800404
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800405
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800406 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200407 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200408 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200409 return 1;
410
411 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800412 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800413 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800414 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800415 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800416 return -2;
417 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800418
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800419 }
420
421 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800422 if (nat_lst) {
423 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800424 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800425 "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 +0800426 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800427 return -3;
428 }
429 }
430
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800431 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800432}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800433
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800434static int _cr_check_loc_upd(struct bsc_connection *bsc,
435 uint8_t *data, unsigned int length,
436 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800437{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800438 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800439 struct gsm48_loc_upd_req *lu;
440 char mi_string[GSM48_MI_SIZE];
441
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800442 if (length < sizeof(*lu)) {
443 LOGP(DNAT, LOGL_ERROR,
444 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800445 return -1;
446 }
447
448 lu = (struct gsm48_loc_upd_req *) data;
449 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
450
451 /*
452 * We can only deal with the IMSI. This will fail for a phone that
453 * will send the TMSI of a previous network to us.
454 */
455 if (mi_type != GSM_MI_TYPE_IMSI)
456 return 0;
457
458 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800459 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800460 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800461}
462
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800463static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
464 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800465 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800466{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800467 static const uint32_t classmark_offset =
468 offsetof(struct gsm48_service_request, classmark);
469
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800470 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800471 uint8_t mi_type;
472 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800473 struct gsm48_service_request *req;
474
475 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800476
477 if (length < sizeof(*req)) {
478 LOGP(DNAT, LOGL_ERROR,
479 "CM Serv Req does not fit. Length is %d\n", length);
480 return -1;
481 }
482
483 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800484 if (req->cm_service_type == 0x8)
485 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800486 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
487 length - classmark_offset, mi_string, &mi_type);
488 if (rc < 0) {
489 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800490 return -1;
491 }
492
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800493 /* we have to let the TMSI or such pass */
494 if (mi_type != GSM_MI_TYPE_IMSI)
495 return 0;
496
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800497 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800498 return auth_imsi(bsc, mi_string);
499}
500
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800501static int _cr_check_pag_resp(struct bsc_connection *bsc,
502 uint8_t *data, unsigned int length,
503 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800504{
505 struct gsm48_pag_resp *resp;
506 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800507 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800508
509 if (length < sizeof(*resp)) {
510 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
511 return -1;
512 }
513
514 resp = (struct gsm48_pag_resp *) data;
515 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
516 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
517 return -1;
518 }
519
520 /* we need to let it pass for now */
521 if (mi_type != GSM_MI_TYPE_IMSI)
522 return 0;
523
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800524 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800525 return auth_imsi(bsc, mi_string);
526}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800527
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800528static int _dt_check_id_resp(struct bsc_connection *bsc,
529 uint8_t *data, unsigned int length,
530 struct sccp_connections *con)
531{
532 char mi_string[GSM48_MI_SIZE];
533 uint8_t mi_type;
534 int ret;
535
536 if (length < 2) {
537 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
538 return -1;
539 }
540
541 if (data[0] < length - 1) {
542 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
543 return -2;
544 }
545
546 mi_type = data[1] & GSM_MI_TYPE_MASK;
547 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
548
549 if (mi_type != GSM_MI_TYPE_IMSI)
550 return 0;
551
552 ret = auth_imsi(bsc, mi_string);
553 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800554 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800555 return ret;
556}
557
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800558/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800559int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
560 struct bsc_nat_parsed *parsed, int *con_type,
561 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800562{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800563 struct tlv_parsed tp;
564 struct gsm48_hdr *hdr48;
565 int hdr48_len;
566 int len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200567 uint8_t msg_type, proto;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800568
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800569 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800570 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800571
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800572 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
573 LOGP(DNAT, LOGL_ERROR,
574 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
575 return -1;
576 }
577
578 /* the parsed has had some basic l3 length check */
579 len = msg->l3h[1];
580 if (msgb_l3len(msg) - 3 < len) {
581 LOGP(DNAT, LOGL_ERROR,
582 "The CR Data has not enough space...\n");
583 return -1;
584 }
585
586 msg->l4h = &msg->l3h[3];
587 len -= 1;
588
589 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
590
591 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
592 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
593 return -1;
594 }
595
596 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
597
598 if (hdr48_len < sizeof(*hdr48)) {
599 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
600 return -1;
601 }
602
603 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
604
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200605 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800606 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200607 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800608 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800609 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800610 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200611 } else if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800612 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800613 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800614 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
615 hdr48_len - sizeof(*hdr48),
616 con_type, imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200617 } else if (proto == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800618 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800619 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800620 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800621 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800622 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800623 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800624 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800625 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800626}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800627
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800628struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
629 struct msgb *msg, uint32_t *len)
630{
631 /* gsm_type is actually the size of the dtap */
632 *len = parsed->gsm_type;
633 if (*len < msgb_l3len(msg) - 3) {
634 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
635 return NULL;
636 }
637
638 if (*len < sizeof(struct gsm48_hdr)) {
639 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
640 return NULL;
641 }
642
643 msg->l4h = &msg->l3h[3];
644 return (struct gsm48_hdr *) msg->l4h;
645}
646
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800647int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
648 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
649{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800650 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200651 uint8_t msg_type, proto;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800652 struct gsm48_hdr *hdr48;
653
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800654 if (con->imsi_checked)
655 return 0;
656
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800657 /* only care about DTAP messages */
658 if (parsed->bssap != BSSAP_MSG_DTAP)
659 return 0;
660
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800661 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
662 if (!hdr48)
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800663 return -1;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800664
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200665 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800666 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200667 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800668 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800669 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
670 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800671 return 0;
672 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800673}
674
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800675static const char *con_types [] = {
676 [NAT_CON_TYPE_NONE] = "n/a",
677 [NAT_CON_TYPE_LU] = "Location Update",
678 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
679 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800680 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800681 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800682 [NAT_CON_TYPE_OTHER] = "Other",
683};
684
685const char *bsc_con_type_to_string(int type)
686{
687 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800688}
689
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800690struct 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 +0800691{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800692 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800693
694 if (!name)
695 return NULL;
696
697 llist_for_each_entry(lst, &nat->access_lists, list)
698 if (strcmp(lst->name, name) == 0)
699 return lst;
700
701 return NULL;
702}
703
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800704struct 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 +0800705{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800706 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800707
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800708 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800709 if (lst)
710 return lst;
711
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800712 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800713 if (!lst) {
714 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
715 return NULL;
716 }
717
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800718 /* TODO: get the index right */
719 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
720 if (!lst->stats) {
721 talloc_free(lst);
722 return NULL;
723 }
724
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800725 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800726 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800727 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800728 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800729}
730
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800731void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800732{
733 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800734 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800735 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800736}
737
738struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
739{
740 struct bsc_nat_acc_lst_entry *entry;
741
742 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
743 if (!entry)
744 return NULL;
745
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800746 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800747 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800748}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800749
750int bsc_nat_msc_is_connected(struct bsc_nat *nat)
751{
752 return nat->msc_con->is_connected;
753}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800754
755static const int con_to_ctr[] = {
756 [NAT_CON_TYPE_NONE] = -1,
757 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
758 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
759 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800760 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800761 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
762 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
763};
764
765int bsc_conn_type_to_ctr(struct sccp_connections *conn)
766{
767 return con_to_ctr[conn->con_type];
768}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200769
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200770int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200771{
772 int rc;
773
774 rc = write(bfd->fd, msg->data, msg->len);
775 if (rc != msg->len)
776 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
777
778 return rc;
779}
780
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200781static char *rewrite_non_international(struct bsc_nat *nat, void *ctx, const char *imsi,
782 struct gsm_mncc_number *called)
783{
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200784 struct bsc_nat_num_rewr_entry *entry;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200785 char *new_number = NULL;
786
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200787 if (llist_empty(&nat->num_rewr))
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200788 return NULL;
789
790 if (called->plan != 1)
791 return NULL;
792 if (called->type == 1)
793 return NULL;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200794
795 /* need to find a replacement and then fix it */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200796 llist_for_each_entry(entry, &nat->num_rewr, list) {
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200797 regmatch_t matches[2];
798
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200799 /* check the IMSI match */
800 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200801 continue;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200802
803 /* this regexp matches... */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200804 if (regexec(&entry->num_reg, called->number, 2, matches, 0) == 0 &&
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200805 matches[1].rm_eo != -1)
806 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200807 entry->replace,
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200808 &called->number[matches[1].rm_so]);
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200809 if (new_number)
810 break;
811 }
812
813 return new_number;
814}
815
816
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200817/**
818 * Rewrite non global numbers... according to rules based on the IMSI
819 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200820static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
821 struct bsc_nat_parsed *parsed, const char *imsi,
822 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200823{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200824 struct tlv_parsed tp;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200825 unsigned int payload_len;
826 struct gsm_mncc_number called;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200827 struct msgb *out;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200828 char *new_number = NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200829 uint8_t *outptr;
830 const uint8_t *msgptr;
831 int sec_len;
832
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200833 /* decode and rewrite the message */
834 payload_len = len - sizeof(*hdr48);
835 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
836
837 /* no number, well let us ignore it */
838 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200839 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200840
841 memset(&called, 0, sizeof(called));
842 gsm48_decode_called(&called,
843 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
844
845 /* check if it looks international and stop */
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200846 new_number = rewrite_non_international(nat, msg, imsi, &called);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200847
848 if (!new_number) {
849 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200850 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200851 }
852
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100853 if (strlen(new_number) > sizeof(called.number)) {
854 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
855 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200856 return NULL;
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100857 }
858
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200859 /*
860 * Need to create a new message now based on the old onew
861 * with a new number. We can sadly not patch this in place
862 * so we will need to regenerate it.
863 */
864
865 out = msgb_alloc_headroom(4096, 128, "changed-setup");
866 if (!out) {
867 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
868 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200869 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200870 }
871
872 /* copy the header */
873 outptr = msgb_put(out, sizeof(*hdr48));
874 memcpy(outptr, hdr48, sizeof(*hdr48));
875
876 /* copy everything up to the number */
877 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
878 outptr = msgb_put(out, sec_len);
879 memcpy(outptr, &hdr48->data[0], sec_len);
880
881 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100882 if (strncmp(new_number, "00", 2) == 0) {
883 called.type = 1;
884 strncpy(called.number, new_number + 2, sizeof(called.number));
885 } else {
886 strncpy(called.number, new_number, sizeof(called.number));
887 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200888 gsm48_encode_called(out, &called);
889
890 /* copy thre rest */
891 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
892 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
893 sec_len = payload_len - (msgptr - &hdr48->data[0]);
894 outptr = msgb_put(out, sec_len);
895 memcpy(outptr, msgptr, sec_len);
896
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200897 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200898 return out;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200899}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200900
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +0100901/**
902 * Find a new SMSC address, returns an allocated string that needs to be
903 * freed or is NULL.
904 */
905static char *find_new_smsc(struct bsc_nat *nat, void *ctx, const char *imsi,
906 const char *smsc_addr, const char *dest_nr)
907{
908 struct bsc_nat_num_rewr_entry *entry;
909 char *new_number = NULL;
910 uint8_t dest_match = 0;
911
912 /* We will find a new number now */
913 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
914 regmatch_t matches[2];
915
916 /* check the IMSI match */
917 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
918 continue;
919
920 /* this regexp matches... */
921 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
922 matches[1].rm_eo != -1)
923 new_number = talloc_asprintf(ctx, "%s%s",
924 entry->replace,
925 &smsc_addr[matches[1].rm_so]);
926 if (new_number)
927 break;
928 }
929
930 if (!new_number)
931 return NULL;
932
933 /*
934 * now match the number against another list
935 */
936 llist_for_each_entry(entry, &nat->tpdest_match, list) {
937 /* check the IMSI match */
938 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
939 continue;
940
941 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
942 dest_match = 1;
943 break;
944 }
945 }
946
947 if (!dest_match) {
948 talloc_free(new_number);
949 return NULL;
950 }
951
952 return new_number;
953}
954
955/**
956 * Parse the SMS and check if it needs to be rewritten
957 */
958static struct msgb *rewrite_sms(struct bsc_nat *nat, struct msgb *msg,
959 struct bsc_nat_parsed *parsed, const char *imsi,
960 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200961{
962 unsigned int payload_len;
963 unsigned int cp_len;
964
965 uint8_t ref;
966 uint8_t orig_addr_len, *orig_addr_ptr;
967 uint8_t dest_addr_len, *dest_addr_ptr;
968 uint8_t data_len, *data_ptr;
969 char smsc_addr[30];
970 uint8_t new_addr[12];
971
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200972
973 uint8_t dest_len;
974 char _dest_nr[30];
975 char *dest_nr;
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200976
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200977 char *new_number = NULL;
978 uint8_t new_addr_len;
979 struct gsm48_hdr *new_hdr48;
980 struct msgb *out;
981
982 payload_len = len - sizeof(*hdr48);
983 if (payload_len < 1) {
984 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
985 return NULL;
986 }
987
988 cp_len = hdr48->data[0];
989 if (payload_len + 1 < cp_len) {
990 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
991 return NULL;
992 }
993
994 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
995 return NULL;
996
997 if (cp_len < 5) {
998 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
999 return NULL;
1000 }
1001
1002 ref = hdr48->data[2];
1003 orig_addr_len = hdr48->data[3];
1004 orig_addr_ptr = &hdr48->data[4];
1005
1006 /* the +1 is for checking if the following element has some space */
1007 if (cp_len < 3 + orig_addr_len + 1) {
1008 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
1009 return NULL;
1010 }
1011
1012 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
1013 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
1014
1015 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
1016 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
1017 return NULL;
1018 }
1019 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
1020
1021 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
1022 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
1023
1024 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
1025 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
1026 return NULL;
1027 }
1028
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001029 /* look into the phone number */
1030 if ((data_ptr[0] & 0x01) != 1)
1031 return NULL;
1032
1033 if (data_len < 3) {
1034 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
1035 return NULL;
1036 }
1037
1038 dest_len = data_ptr[2];
1039 if (data_len < dest_len + 3 || dest_len < 2) {
1040 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
1041 return NULL;
1042 }
1043
1044 if ((data_ptr[3] & 0x80) == 0) {
1045 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
1046 return NULL;
1047 }
1048
1049 if ((data_ptr[3] & 0x0F) == 0) {
1050 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is not a ISDN number.\n");
1051 return NULL;
1052 }
1053
1054 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
1055 &data_ptr[2], 1);
1056 if ((data_ptr[3] & 0x70) == 0x10) {
1057 _dest_nr[0] = _dest_nr[1] = '0';
1058 dest_nr = &_dest_nr[0];
1059 } else {
1060 dest_nr = &_dest_nr[2];
1061 }
1062
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +01001063 new_number = find_new_smsc(nat, msg, imsi, smsc_addr, dest_nr);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001064 if (!new_number)
1065 return NULL;
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001066 /*
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001067 * We need to re-create the patched structure. This is why we have
1068 * saved the above pointers.
1069 */
1070 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
1071 if (!out) {
1072 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1073 return NULL;
1074 }
1075
1076 out->l3h = out->data;
1077 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
1078 msgb_v_put(out, ref);
1079 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
1080
1081 /*
1082 * Copy the new number. We let libosmocore encode it, then set
1083 * the extension followed after the length. For our convenience
1084 * we let the TLV code re-add the length so we start copying
1085 * from &new_addr[1].
1086 */
1087 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
1088 1, new_number);
1089 new_addr[1] = 0x91;
1090 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
1091
1092 msgb_lv_put(out, data_len, data_ptr);
1093
1094 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*hdr48) + 1);
1095 memcpy(new_hdr48, hdr48, sizeof(*hdr48));
1096 new_hdr48->data[0] = msgb_l3len(out);
1097
1098 talloc_free(new_number);
1099 return out;
1100}
1101
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001102struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
1103{
1104 struct gsm48_hdr *hdr48;
1105 uint32_t len;
1106 uint8_t msg_type, proto;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001107 struct msgb *new_msg = NULL, *sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001108
1109 if (!imsi || strlen(imsi) < 5)
1110 return msg;
1111
1112 /* only care about DTAP messages */
1113 if (parsed->bssap != BSSAP_MSG_DTAP)
1114 return msg;
1115 if (!parsed->dest_local_ref)
1116 return msg;
1117
1118 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
1119 if (!hdr48)
1120 return msg;
1121
1122 proto = hdr48->proto_discr & 0x0f;
1123 msg_type = hdr48->msg_type & 0xbf;
1124
1125 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001126 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001127 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +01001128 new_msg = rewrite_sms(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001129
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001130 if (!new_msg)
1131 return msg;
1132
1133 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
1134 gsm0808_prepend_dtap_header(new_msg, 0);
1135 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
1136 talloc_free(new_msg);
1137
1138 if (!sccp) {
1139 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1140 return msg;
1141 }
1142
1143 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
1144
1145 /* the parsed hangs off from msg but it needs to survive */
1146 talloc_steal(sccp, parsed);
1147 msgb_free(msg);
1148 return sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001149}
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001150
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001151static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
1152{
1153 regfree(&entry->msisdn_reg);
1154 regfree(&entry->num_reg);
1155 talloc_free(entry->replace);
1156}
1157
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001158void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
1159 const struct osmo_config_list *list)
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001160{
1161 struct bsc_nat_num_rewr_entry *entry, *tmp;
1162 struct osmo_config_entry *cfg_entry;
1163
1164 /* free the old data */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001165 llist_for_each_entry_safe(entry, tmp, head, list) {
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001166 num_rewr_free_data(entry);
1167 llist_del(&entry->list);
1168 talloc_free(entry);
1169 }
1170
1171
1172 if (!list)
1173 return;
1174
1175 llist_for_each_entry(cfg_entry, &list->entry, list) {
1176 char *regexp;
1177 if (cfg_entry->text[0] == '+') {
1178 LOGP(DNAT, LOGL_ERROR,
1179 "Plus is not allowed in the number\n");
1180 continue;
1181 }
1182
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001183 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001184 if (!entry) {
1185 LOGP(DNAT, LOGL_ERROR,
1186 "Allication of the num_rewr entry failed.\n");
1187 continue;
1188 }
1189
1190 entry->replace = talloc_strdup(entry, cfg_entry->text);
1191 if (!entry->replace) {
1192 LOGP(DNAT, LOGL_ERROR,
1193 "Failed to copy the replacement text.\n");
1194 talloc_free(entry);
1195 continue;
1196 }
1197
1198 /* we will now build a regexp string */
1199 if (cfg_entry->mcc[0] == '^') {
1200 regexp = talloc_strdup(entry, cfg_entry->mcc);
1201 } else {
1202 regexp = talloc_asprintf(entry, "^%s%s",
1203 cfg_entry->mcc[0] == '*' ?
1204 "[0-9][0-9][0-9]" : cfg_entry->mcc,
1205 cfg_entry->mnc[0] == '*' ?
1206 "[0-9][0-9]" : cfg_entry->mnc);
1207 }
1208
1209 if (!regexp) {
1210 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
1211 talloc_free(entry);
1212 continue;
1213 }
1214
1215 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
1216 LOGP(DNAT, LOGL_ERROR,
1217 "Failed to compile regexp '%s'\n", regexp);
1218 talloc_free(regexp);
1219 talloc_free(entry);
1220 continue;
1221 }
1222
1223 talloc_free(regexp);
1224 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
1225 LOGP(DNAT, LOGL_ERROR,
1226 "Failed to compile regexp '%s\n'", cfg_entry->option);
1227 regfree(&entry->msisdn_reg);
1228 talloc_free(entry);
1229 continue;
1230 }
1231
1232 /* we have copied the number */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001233 llist_add_tail(&entry->list, head);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001234 }
1235}