blob: 8658c3d9b1ec1fcedf7670e9ebcb480fc95039b8 [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);
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) {
321 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
322 return -1;
323 }
324
325 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
326 if (!msg) {
327 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
328 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) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200353 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
354 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 Freyther4c9557e2011-04-04 19:19:26 +0200675int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800676{
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200677 int ret;
678
679 ret = 0;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800680 if (*imsi) {
681 talloc_free(*imsi);
682 *imsi = NULL;
683 }
684 regfree(reg);
685
686 if (argc > 0) {
687 *imsi = talloc_strdup(ctx, argv[0]);
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200688 ret = regcomp(reg, argv[0], 0);
689
690 /* handle compilation failures */
691 if (ret != 0) {
692 talloc_free(*imsi);
693 *imsi = NULL;
694 }
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800695 }
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200696
697 return ret;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800698}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800699
700static const char *con_types [] = {
701 [NAT_CON_TYPE_NONE] = "n/a",
702 [NAT_CON_TYPE_LU] = "Location Update",
703 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
704 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800705 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800706 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800707 [NAT_CON_TYPE_OTHER] = "Other",
708};
709
710const char *bsc_con_type_to_string(int type)
711{
712 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800713}
714
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800715struct 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 +0800716{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800717 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800718
719 if (!name)
720 return NULL;
721
722 llist_for_each_entry(lst, &nat->access_lists, list)
723 if (strcmp(lst->name, name) == 0)
724 return lst;
725
726 return NULL;
727}
728
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800729struct 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 +0800730{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800731 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800732
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800733 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800734 if (lst)
735 return lst;
736
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800737 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800738 if (!lst) {
739 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
740 return NULL;
741 }
742
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800743 /* TODO: get the index right */
744 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
745 if (!lst->stats) {
746 talloc_free(lst);
747 return NULL;
748 }
749
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800750 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800751 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800752 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800753 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800754}
755
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800756void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800757{
758 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800759 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800760 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800761}
762
763struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
764{
765 struct bsc_nat_acc_lst_entry *entry;
766
767 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
768 if (!entry)
769 return NULL;
770
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800771 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800772 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800773}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800774
775int bsc_nat_msc_is_connected(struct bsc_nat *nat)
776{
777 return nat->msc_con->is_connected;
778}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800779
780static const int con_to_ctr[] = {
781 [NAT_CON_TYPE_NONE] = -1,
782 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
783 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
784 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800785 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800786 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
787 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
788};
789
790int bsc_conn_type_to_ctr(struct sccp_connections *conn)
791{
792 return con_to_ctr[conn->con_type];
793}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200794
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200795int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200796{
797 int rc;
798
799 rc = write(bfd->fd, msg->data, msg->len);
800 if (rc != msg->len)
801 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
802
803 return rc;
804}
805
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200806static char *rewrite_non_international(struct bsc_nat *nat, void *ctx, const char *imsi,
807 struct gsm_mncc_number *called)
808{
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200809 struct bsc_nat_num_rewr_entry *entry;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200810 char *new_number = NULL;
811
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200812 if (llist_empty(&nat->num_rewr))
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200813 return NULL;
814
815 if (called->plan != 1)
816 return NULL;
817 if (called->type == 1)
818 return NULL;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200819
820 /* need to find a replacement and then fix it */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200821 llist_for_each_entry(entry, &nat->num_rewr, list) {
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200822 regmatch_t matches[2];
823
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200824 /* check the IMSI match */
825 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200826 continue;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200827
828 /* this regexp matches... */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200829 if (regexec(&entry->num_reg, called->number, 2, matches, 0) == 0 &&
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200830 matches[1].rm_eo != -1)
831 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200832 entry->replace,
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200833 &called->number[matches[1].rm_so]);
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200834 if (new_number)
835 break;
836 }
837
838 return new_number;
839}
840
841
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200842/**
843 * Rewrite non global numbers... according to rules based on the IMSI
844 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200845static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
846 struct bsc_nat_parsed *parsed, const char *imsi,
847 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200848{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200849 struct tlv_parsed tp;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200850 unsigned int payload_len;
851 struct gsm_mncc_number called;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200852 struct msgb *out;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200853 char *new_number = NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200854 uint8_t *outptr;
855 const uint8_t *msgptr;
856 int sec_len;
857
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200858 /* decode and rewrite the message */
859 payload_len = len - sizeof(*hdr48);
860 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
861
862 /* no number, well let us ignore it */
863 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200864 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200865
866 memset(&called, 0, sizeof(called));
867 gsm48_decode_called(&called,
868 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
869
870 /* check if it looks international and stop */
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200871 new_number = rewrite_non_international(nat, msg, imsi, &called);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200872
873 if (!new_number) {
874 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200875 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200876 }
877
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100878 if (strlen(new_number) > sizeof(called.number)) {
879 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
880 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200881 return NULL;
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100882 }
883
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200884 /*
885 * Need to create a new message now based on the old onew
886 * with a new number. We can sadly not patch this in place
887 * so we will need to regenerate it.
888 */
889
890 out = msgb_alloc_headroom(4096, 128, "changed-setup");
891 if (!out) {
892 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
893 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200894 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200895 }
896
897 /* copy the header */
898 outptr = msgb_put(out, sizeof(*hdr48));
899 memcpy(outptr, hdr48, sizeof(*hdr48));
900
901 /* copy everything up to the number */
902 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
903 outptr = msgb_put(out, sec_len);
904 memcpy(outptr, &hdr48->data[0], sec_len);
905
906 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100907 if (strncmp(new_number, "00", 2) == 0) {
908 called.type = 1;
909 strncpy(called.number, new_number + 2, sizeof(called.number));
910 } else {
911 strncpy(called.number, new_number, sizeof(called.number));
912 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200913 gsm48_encode_called(out, &called);
914
915 /* copy thre rest */
916 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
917 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
918 sec_len = payload_len - (msgptr - &hdr48->data[0]);
919 outptr = msgb_put(out, sec_len);
920 memcpy(outptr, msgptr, sec_len);
921
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200922 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200923 return out;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200924}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200925
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200926static struct msgb *rewrite_smsc(struct bsc_nat *nat, struct msgb *msg,
927 struct bsc_nat_parsed *parsed, const char *imsi,
928 struct gsm48_hdr *hdr48, const uint32_t len)
929{
930 unsigned int payload_len;
931 unsigned int cp_len;
932
933 uint8_t ref;
934 uint8_t orig_addr_len, *orig_addr_ptr;
935 uint8_t dest_addr_len, *dest_addr_ptr;
936 uint8_t data_len, *data_ptr;
937 char smsc_addr[30];
938 uint8_t new_addr[12];
939
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200940
941 uint8_t dest_len;
942 char _dest_nr[30];
943 char *dest_nr;
944 uint8_t dest_match = 0;
945
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200946 struct bsc_nat_num_rewr_entry *entry;
947 char *new_number = NULL;
948 uint8_t new_addr_len;
949 struct gsm48_hdr *new_hdr48;
950 struct msgb *out;
951
952 payload_len = len - sizeof(*hdr48);
953 if (payload_len < 1) {
954 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
955 return NULL;
956 }
957
958 cp_len = hdr48->data[0];
959 if (payload_len + 1 < cp_len) {
960 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
961 return NULL;
962 }
963
964 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
965 return NULL;
966
967 if (cp_len < 5) {
968 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
969 return NULL;
970 }
971
972 ref = hdr48->data[2];
973 orig_addr_len = hdr48->data[3];
974 orig_addr_ptr = &hdr48->data[4];
975
976 /* the +1 is for checking if the following element has some space */
977 if (cp_len < 3 + orig_addr_len + 1) {
978 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
979 return NULL;
980 }
981
982 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
983 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
984
985 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
986 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
987 return NULL;
988 }
989 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
990
991 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
992 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
993
994 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
995 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
996 return NULL;
997 }
998
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200999 /* look into the phone number */
1000 if ((data_ptr[0] & 0x01) != 1)
1001 return NULL;
1002
1003 if (data_len < 3) {
1004 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
1005 return NULL;
1006 }
1007
1008 dest_len = data_ptr[2];
1009 if (data_len < dest_len + 3 || dest_len < 2) {
1010 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
1011 return NULL;
1012 }
1013
1014 if ((data_ptr[3] & 0x80) == 0) {
1015 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
1016 return NULL;
1017 }
1018
1019 if ((data_ptr[3] & 0x0F) == 0) {
1020 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is not a ISDN number.\n");
1021 return NULL;
1022 }
1023
1024 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
1025 &data_ptr[2], 1);
1026 if ((data_ptr[3] & 0x70) == 0x10) {
1027 _dest_nr[0] = _dest_nr[1] = '0';
1028 dest_nr = &_dest_nr[0];
1029 } else {
1030 dest_nr = &_dest_nr[2];
1031 }
1032
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001033 /* We will find a new number now */
1034 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
1035 regmatch_t matches[2];
1036
1037 /* check the IMSI match */
1038 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
1039 continue;
1040
1041 /* this regexp matches... */
1042 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
1043 matches[1].rm_eo != -1)
1044 new_number = talloc_asprintf(msg, "%s%s",
1045 entry->replace,
1046 &smsc_addr[matches[1].rm_so]);
1047 if (new_number)
1048 break;
1049 }
1050
1051 if (!new_number)
1052 return NULL;
1053
1054 /*
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001055 * now match the number against another list
1056 */
1057 llist_for_each_entry(entry, &nat->tpdest_match, list) {
1058 /* check the IMSI match */
1059 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
1060 continue;
1061
1062 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
1063 dest_match =1;
1064 break;
1065 }
1066 }
1067
1068 if (!dest_match) {
1069 talloc_free(new_number);
1070 return NULL;
1071 }
1072
1073 /*
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001074 * We need to re-create the patched structure. This is why we have
1075 * saved the above pointers.
1076 */
1077 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
1078 if (!out) {
1079 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1080 return NULL;
1081 }
1082
1083 out->l3h = out->data;
1084 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
1085 msgb_v_put(out, ref);
1086 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
1087
1088 /*
1089 * Copy the new number. We let libosmocore encode it, then set
1090 * the extension followed after the length. For our convenience
1091 * we let the TLV code re-add the length so we start copying
1092 * from &new_addr[1].
1093 */
1094 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
1095 1, new_number);
1096 new_addr[1] = 0x91;
1097 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
1098
1099 msgb_lv_put(out, data_len, data_ptr);
1100
1101 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*hdr48) + 1);
1102 memcpy(new_hdr48, hdr48, sizeof(*hdr48));
1103 new_hdr48->data[0] = msgb_l3len(out);
1104
1105 talloc_free(new_number);
1106 return out;
1107}
1108
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001109struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
1110{
1111 struct gsm48_hdr *hdr48;
1112 uint32_t len;
1113 uint8_t msg_type, proto;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001114 struct msgb *new_msg = NULL, *sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001115
1116 if (!imsi || strlen(imsi) < 5)
1117 return msg;
1118
1119 /* only care about DTAP messages */
1120 if (parsed->bssap != BSSAP_MSG_DTAP)
1121 return msg;
1122 if (!parsed->dest_local_ref)
1123 return msg;
1124
1125 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
1126 if (!hdr48)
1127 return msg;
1128
1129 proto = hdr48->proto_discr & 0x0f;
1130 msg_type = hdr48->msg_type & 0xbf;
1131
1132 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001133 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001134 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
1135 new_msg = rewrite_smsc(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001136
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001137 if (!new_msg)
1138 return msg;
1139
1140 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
1141 gsm0808_prepend_dtap_header(new_msg, 0);
1142 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
1143 talloc_free(new_msg);
1144
1145 if (!sccp) {
1146 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1147 return msg;
1148 }
1149
1150 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
1151
1152 /* the parsed hangs off from msg but it needs to survive */
1153 talloc_steal(sccp, parsed);
1154 msgb_free(msg);
1155 return sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001156}
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001157
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001158static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
1159{
1160 regfree(&entry->msisdn_reg);
1161 regfree(&entry->num_reg);
1162 talloc_free(entry->replace);
1163}
1164
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001165void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
1166 const struct osmo_config_list *list)
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001167{
1168 struct bsc_nat_num_rewr_entry *entry, *tmp;
1169 struct osmo_config_entry *cfg_entry;
1170
1171 /* free the old data */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001172 llist_for_each_entry_safe(entry, tmp, head, list) {
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001173 num_rewr_free_data(entry);
1174 llist_del(&entry->list);
1175 talloc_free(entry);
1176 }
1177
1178
1179 if (!list)
1180 return;
1181
1182 llist_for_each_entry(cfg_entry, &list->entry, list) {
1183 char *regexp;
1184 if (cfg_entry->text[0] == '+') {
1185 LOGP(DNAT, LOGL_ERROR,
1186 "Plus is not allowed in the number\n");
1187 continue;
1188 }
1189
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001190 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001191 if (!entry) {
1192 LOGP(DNAT, LOGL_ERROR,
1193 "Allication of the num_rewr entry failed.\n");
1194 continue;
1195 }
1196
1197 entry->replace = talloc_strdup(entry, cfg_entry->text);
1198 if (!entry->replace) {
1199 LOGP(DNAT, LOGL_ERROR,
1200 "Failed to copy the replacement text.\n");
1201 talloc_free(entry);
1202 continue;
1203 }
1204
1205 /* we will now build a regexp string */
1206 if (cfg_entry->mcc[0] == '^') {
1207 regexp = talloc_strdup(entry, cfg_entry->mcc);
1208 } else {
1209 regexp = talloc_asprintf(entry, "^%s%s",
1210 cfg_entry->mcc[0] == '*' ?
1211 "[0-9][0-9][0-9]" : cfg_entry->mcc,
1212 cfg_entry->mnc[0] == '*' ?
1213 "[0-9][0-9]" : cfg_entry->mnc);
1214 }
1215
1216 if (!regexp) {
1217 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
1218 talloc_free(entry);
1219 continue;
1220 }
1221
1222 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
1223 LOGP(DNAT, LOGL_ERROR,
1224 "Failed to compile regexp '%s'\n", regexp);
1225 talloc_free(regexp);
1226 talloc_free(entry);
1227 continue;
1228 }
1229
1230 talloc_free(regexp);
1231 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
1232 LOGP(DNAT, LOGL_ERROR,
1233 "Failed to compile regexp '%s\n'", cfg_entry->option);
1234 regfree(&entry->msisdn_reg);
1235 talloc_free(entry);
1236 continue;
1237 }
1238
1239 /* we have copied the number */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001240 llist_add_tail(&entry->list, head);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001241 }
1242}