blob: b45f447ed3c52fb08bd3ad83f88914873c095975 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
5 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080026#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +080027#include <openbsc/bsc_msc.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080028#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020029#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020030#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +020031#include <openbsc/vty.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020033#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080034#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080035#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080036
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080037#include <osmocore/protocol/gsm_08_08.h>
38
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
86 INIT_LLIST_HEAD(&nat->sccp_connections);
87 INIT_LLIST_HEAD(&nat->bsc_connections);
88 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080089 INIT_LLIST_HEAD(&nat->access_lists);
90
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020091 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
92 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
93 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
94 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
95 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020096 nat->stats.ussd.reconn = counter_alloc("nat.ussd.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080097 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020098 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080099 nat->auth_timeout = 2;
100 nat->ping_timeout = 20;
101 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800102 return nat;
103}
104
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800105void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
106{
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +0200107 bsc_replace_string(nat, &nat->msc_ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800108}
109
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800110struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
111{
112 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
113 if (!con)
114 return NULL;
115
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200116 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800117 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800118 return con;
119}
120
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800121struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800122{
123 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
124 if (!conf)
125 return NULL;
126
127 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800128 conf->nr = nat->num_bsc;
129 conf->nat = nat;
130
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800131 INIT_LLIST_HEAD(&conf->lac_list);
132
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200133 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800134 ++nat->num_bsc;
135
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800136 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800137 if (!conf->stats.ctrg) {
138 talloc_free(conf);
139 return NULL;
140 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200141
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800142 return conf;
143}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200144
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800145void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
146{
147 struct bsc_lac_entry *lac;
148
149 llist_for_each_entry(lac, &cfg->lac_list, entry)
150 if (lac->lac == _lac)
151 return;
152
153 lac = talloc_zero(cfg, struct bsc_lac_entry);
154 if (!lac) {
155 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
156 return;
157 }
158
159 lac->lac = _lac;
160 llist_add_tail(&lac->entry, &cfg->lac_list);
161}
162
163void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
164{
165 struct bsc_lac_entry *lac;
166
167 llist_for_each_entry(lac, &cfg->lac_list, entry)
168 if (lac->lac == _lac) {
169 llist_del(&lac->entry);
170 talloc_free(lac);
171 return;
172 }
173}
174
175int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
176{
177 struct bsc_lac_entry *entry;
178
179 llist_for_each_entry(entry, &cfg->lac_list, entry)
180 if (entry->lac == lac_nr)
181 return 1;
182
183 return 0;
184}
185
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200186void sccp_connection_destroy(struct sccp_connections *conn)
187{
188 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
189 sccp_src_ref_to_int(&conn->real_ref),
190 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800191 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200192 llist_del(&conn->list_entry);
193 talloc_free(conn);
194}
195
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200196struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200197{
198 struct bsc_connection *bsc;
199 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800200 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200201 struct tlv_parsed tp;
202 int i = 0;
203
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800204 *lac_out = -1;
205
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200206 if (!msg->l3h || msgb_l3len(msg) < 3) {
207 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
208 return NULL;
209 }
210
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200211 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
212 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
213 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
214 return NULL;
215 }
216
217 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
218 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800219
220 /* No need to try a different BSS */
221 if (data[0] == CELL_IDENT_BSS) {
222 return NULL;
223 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200224 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200225 return NULL;
226 }
227
228 /* Currently we only handle one BSC */
229 for (i = 1; i < data_length - 1; i += 2) {
230 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200231 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200232 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200233 if (!bsc->cfg)
234 continue;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800235 if (!bsc->authenticated)
236 continue;
237 if (!bsc_config_handles_lac(bsc->cfg, _lac))
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200238 continue;
239
240 return bsc;
241 }
242 }
243
244 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200245}
246
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800247int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200248{
249 struct msgb *msg;
250
251 if (length > 4096 - 128) {
252 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
253 return -1;
254 }
255
256 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
257 if (!msg) {
258 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
259 return -1;
260 }
261
262 /* copy the data */
263 msg->l3h = msgb_put(msg, length);
264 memcpy(msg->l3h, data, length);
265
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +0200266 return bsc_write(bsc, msg, IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200267}
268
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200269int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200270{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200271 return bsc_do_write(&bsc->write_queue, msg, proto);
272}
273
274int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto)
275{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200276 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200277 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200278 return bsc_write_msg(queue, msg);
279}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200280
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200281int bsc_write_msg(struct write_queue *queue, struct msgb *msg)
282{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200283 if (write_queue_enqueue(queue, msg) != 0) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200284 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
285 msgb_free(msg);
286 return -1;
287 }
288
289 return 0;
290}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800291
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200292int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800293{
294 struct bsc_nat_acc_lst_entry *entry;
295
296 llist_for_each_entry(entry, &lst->fltr_list, list) {
297 if (!entry->imsi_allow)
298 continue;
299 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
300 return 0;
301 }
302
303 return 1;
304}
305
306static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
307{
308 struct bsc_nat_acc_lst_entry *entry;
309
310 llist_for_each_entry(entry, &lst->fltr_list, list) {
311 if (!entry->imsi_deny)
312 continue;
313 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
314 return 0;
315 }
316
317 return 1;
318}
319
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800320/* apply white/black list */
321static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
322{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800323 /*
324 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200325 * 1.) Allow directly if the IMSI is allowed at the BSC
326 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800327 * 3.) Reject if the IMSI not allowed at the global level.
328 * 4.) Allow directly if the IMSI is allowed at the global level
329 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800330 struct bsc_nat_acc_lst *nat_lst = NULL;
331 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800332
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800333 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
334 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800335
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800336
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800337 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200338 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200339 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200340 return 1;
341
342 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800343 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800344 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800345 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800346 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800347 return -2;
348 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800349
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800350 }
351
352 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800353 if (nat_lst) {
354 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800355 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800356 "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 +0800357 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800358 return -3;
359 }
360 }
361
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800362 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800363}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800364
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800365static int _cr_check_loc_upd(struct bsc_connection *bsc,
366 uint8_t *data, unsigned int length,
367 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800368{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800369 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800370 struct gsm48_loc_upd_req *lu;
371 char mi_string[GSM48_MI_SIZE];
372
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800373 if (length < sizeof(*lu)) {
374 LOGP(DNAT, LOGL_ERROR,
375 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800376 return -1;
377 }
378
379 lu = (struct gsm48_loc_upd_req *) data;
380 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
381
382 /*
383 * We can only deal with the IMSI. This will fail for a phone that
384 * will send the TMSI of a previous network to us.
385 */
386 if (mi_type != GSM_MI_TYPE_IMSI)
387 return 0;
388
389 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800390 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800391 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800392}
393
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800394static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
395 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800396 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800397{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800398 static const uint32_t classmark_offset =
399 offsetof(struct gsm48_service_request, classmark);
400
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800401 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800402 uint8_t mi_type;
403 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800404 struct gsm48_service_request *req;
405
406 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800407
408 if (length < sizeof(*req)) {
409 LOGP(DNAT, LOGL_ERROR,
410 "CM Serv Req does not fit. Length is %d\n", length);
411 return -1;
412 }
413
414 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800415 if (req->cm_service_type == 0x8)
416 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800417 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
418 length - classmark_offset, mi_string, &mi_type);
419 if (rc < 0) {
420 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800421 return -1;
422 }
423
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800424 /* we have to let the TMSI or such pass */
425 if (mi_type != GSM_MI_TYPE_IMSI)
426 return 0;
427
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800428 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800429 return auth_imsi(bsc, mi_string);
430}
431
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800432static int _cr_check_pag_resp(struct bsc_connection *bsc,
433 uint8_t *data, unsigned int length,
434 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800435{
436 struct gsm48_pag_resp *resp;
437 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800438 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800439
440 if (length < sizeof(*resp)) {
441 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
442 return -1;
443 }
444
445 resp = (struct gsm48_pag_resp *) data;
446 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
447 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
448 return -1;
449 }
450
451 /* we need to let it pass for now */
452 if (mi_type != GSM_MI_TYPE_IMSI)
453 return 0;
454
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800455 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800456 return auth_imsi(bsc, mi_string);
457}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800458
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800459static int _dt_check_id_resp(struct bsc_connection *bsc,
460 uint8_t *data, unsigned int length,
461 struct sccp_connections *con)
462{
463 char mi_string[GSM48_MI_SIZE];
464 uint8_t mi_type;
465 int ret;
466
467 if (length < 2) {
468 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
469 return -1;
470 }
471
472 if (data[0] < length - 1) {
473 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
474 return -2;
475 }
476
477 mi_type = data[1] & GSM_MI_TYPE_MASK;
478 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
479
480 if (mi_type != GSM_MI_TYPE_IMSI)
481 return 0;
482
483 ret = auth_imsi(bsc, mi_string);
484 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800485 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800486 return ret;
487}
488
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800489/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800490int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
491 struct bsc_nat_parsed *parsed, int *con_type,
492 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800493{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800494 struct tlv_parsed tp;
495 struct gsm48_hdr *hdr48;
496 int hdr48_len;
497 int len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800498 uint8_t msg_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800499
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800500 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800501 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800502
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800503 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
504 LOGP(DNAT, LOGL_ERROR,
505 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
506 return -1;
507 }
508
509 /* the parsed has had some basic l3 length check */
510 len = msg->l3h[1];
511 if (msgb_l3len(msg) - 3 < len) {
512 LOGP(DNAT, LOGL_ERROR,
513 "The CR Data has not enough space...\n");
514 return -1;
515 }
516
517 msg->l4h = &msg->l3h[3];
518 len -= 1;
519
520 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
521
522 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
523 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
524 return -1;
525 }
526
527 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
528
529 if (hdr48_len < sizeof(*hdr48)) {
530 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
531 return -1;
532 }
533
534 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
535
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800536 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800537 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800538 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800539 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800540 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther87ef2f22010-05-15 22:09:39 +0800541 } else if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800542 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800543 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800544 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
545 hdr48_len - sizeof(*hdr48),
546 con_type, imsi);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800547 } else if (hdr48->proto_discr == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800548 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800549 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800550 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800551 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800552 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800553 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800554 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800555 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800556}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800557
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800558struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
559 struct msgb *msg, uint32_t *len)
560{
561 /* gsm_type is actually the size of the dtap */
562 *len = parsed->gsm_type;
563 if (*len < msgb_l3len(msg) - 3) {
564 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
565 return NULL;
566 }
567
568 if (*len < sizeof(struct gsm48_hdr)) {
569 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
570 return NULL;
571 }
572
573 msg->l4h = &msg->l3h[3];
574 return (struct gsm48_hdr *) msg->l4h;
575}
576
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800577int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
578 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
579{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800580 uint32_t len;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800581 uint8_t msg_type;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800582 struct gsm48_hdr *hdr48;
583
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800584 if (con->imsi_checked)
585 return 0;
586
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800587 /* only care about DTAP messages */
588 if (parsed->bssap != BSSAP_MSG_DTAP)
589 return 0;
590
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800591 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
592 if (!hdr48)
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800593 return -1;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800594
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800595 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800596 if (hdr48->proto_discr == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800597 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800598 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
599 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800600 return 0;
601 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800602}
603
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800604void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
605{
606 if (*imsi) {
607 talloc_free(*imsi);
608 *imsi = NULL;
609 }
610 regfree(reg);
611
612 if (argc > 0) {
613 *imsi = talloc_strdup(ctx, argv[0]);
614 regcomp(reg, argv[0], 0);
615 }
616}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800617
618static const char *con_types [] = {
619 [NAT_CON_TYPE_NONE] = "n/a",
620 [NAT_CON_TYPE_LU] = "Location Update",
621 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
622 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800623 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800624 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800625 [NAT_CON_TYPE_OTHER] = "Other",
626};
627
628const char *bsc_con_type_to_string(int type)
629{
630 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800631}
632
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800633struct 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 +0800634{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800635 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800636
637 if (!name)
638 return NULL;
639
640 llist_for_each_entry(lst, &nat->access_lists, list)
641 if (strcmp(lst->name, name) == 0)
642 return lst;
643
644 return NULL;
645}
646
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800647struct 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 +0800648{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800649 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800650
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800651 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800652 if (lst)
653 return lst;
654
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800655 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800656 if (!lst) {
657 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
658 return NULL;
659 }
660
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800661 /* TODO: get the index right */
662 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
663 if (!lst->stats) {
664 talloc_free(lst);
665 return NULL;
666 }
667
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800668 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800669 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800670 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800671 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800672}
673
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800674void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800675{
676 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800677 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800678 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800679}
680
681struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
682{
683 struct bsc_nat_acc_lst_entry *entry;
684
685 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
686 if (!entry)
687 return NULL;
688
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800689 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800690 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800691}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800692
693int bsc_nat_msc_is_connected(struct bsc_nat *nat)
694{
695 return nat->msc_con->is_connected;
696}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800697
698static const int con_to_ctr[] = {
699 [NAT_CON_TYPE_NONE] = -1,
700 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
701 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
702 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800703 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800704 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
705 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
706};
707
708int bsc_conn_type_to_ctr(struct sccp_connections *conn)
709{
710 return con_to_ctr[conn->con_type];
711}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200712
713int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
714{
715 int rc;
716
717 rc = write(bfd->fd, msg->data, msg->len);
718 if (rc != msg->len)
719 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
720
721 return rc;
722}
723
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200724/**
725 * Rewrite non global numbers... according to rules based on the IMSI
726 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200727struct msgb *bsc_nat_rewrite_setup(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200728{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200729 struct tlv_parsed tp;
730 struct gsm48_hdr *hdr48;
731 uint32_t len;
732 uint8_t msg_type;
733 unsigned int payload_len;
734 struct gsm_mncc_number called;
735 struct msg_entry *entry;
736 char *new_number = NULL;
737 struct msgb *out, *sccp;
738 uint8_t *outptr;
739 const uint8_t *msgptr;
740 int sec_len;
741
742 if (!imsi || strlen(imsi) < 5)
743 return msg;
744
745 if (!nat->num_rewr)
746 return msg;
747
748 /* only care about DTAP messages */
749 if (parsed->bssap != BSSAP_MSG_DTAP)
750 return msg;
751 if (!parsed->dest_local_ref)
752 return msg;
753
754 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
755 if (!hdr48)
756 return msg;
757
758 msg_type = hdr48->msg_type & 0xbf;
759 if (hdr48->proto_discr != GSM48_PDISC_CC ||
760 msg_type != GSM48_MT_CC_SETUP)
761 return msg;
762
763 /* decode and rewrite the message */
764 payload_len = len - sizeof(*hdr48);
765 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
766
767 /* no number, well let us ignore it */
768 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
769 return msg;
770
771 memset(&called, 0, sizeof(called));
772 gsm48_decode_called(&called,
773 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
774
775 /* check if it looks international and stop */
Holger Hans Peter Freyther643931d2010-10-27 10:24:34 +0200776 if (called.plan != 1)
777 return msg;
778 if (called.type == 1)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200779 return msg;
780 if (strncmp(called.number, "00", 2) == 0)
781 return msg;
782
783 /* need to find a replacement and then fix it */
784 llist_for_each_entry(entry, &nat->num_rewr->entry, list) {
785 regex_t reg;
786 regmatch_t matches[2];
787
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200788 if (entry->mcc[0] != '*' && strncmp(entry->mcc, imsi, 3) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200789 continue;
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200790 if (entry->mnc[0] != '*' && strncmp(entry->mnc, imsi + 3, 2) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200791 continue;
792
Holger Hans Peter Freyther14003102010-10-27 10:24:12 +0200793 if (entry->text[0] == '+') {
794 LOGP(DNAT, LOGL_ERROR,
795 "Plus is not allowed in the number");
796 continue;
797 }
798
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200799 /* We have an entry for the IMSI. Need to match now */
800 if (regcomp(&reg, entry->option, REG_EXTENDED) != 0) {
801 LOGP(DNAT, LOGL_ERROR,
802 "Regexp '%s' is not valid.\n", entry->option);
803 continue;
804 }
805
806 /* this regexp matches... */
807 if (regexec(&reg, called.number, 2, matches, 0) == 0 &&
808 matches[1].rm_eo != -1)
809 new_number = talloc_asprintf(msg, "%s%s",
810 entry->text,
811 &called.number[matches[1].rm_so]);
812 regfree(&reg);
813
814 if (new_number)
815 break;
816 }
817
818 if (!new_number) {
819 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
820 return msg;
821 }
822
823 /*
824 * Need to create a new message now based on the old onew
825 * with a new number. We can sadly not patch this in place
826 * so we will need to regenerate it.
827 */
828
829 out = msgb_alloc_headroom(4096, 128, "changed-setup");
830 if (!out) {
831 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
832 talloc_free(new_number);
833 return msg;
834 }
835
836 /* copy the header */
837 outptr = msgb_put(out, sizeof(*hdr48));
838 memcpy(outptr, hdr48, sizeof(*hdr48));
839
840 /* copy everything up to the number */
841 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
842 outptr = msgb_put(out, sec_len);
843 memcpy(outptr, &hdr48->data[0], sec_len);
844
845 /* create the new number */
846 strncpy(called.number, new_number, sizeof(called.number));
847 gsm48_encode_called(out, &called);
848
849 /* copy thre rest */
850 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
851 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
852 sec_len = payload_len - (msgptr - &hdr48->data[0]);
853 outptr = msgb_put(out, sec_len);
854 memcpy(outptr, msgptr, sec_len);
855
856 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
857 gsm0808_prepend_dtap_header(out, 0);
858 sccp = sccp_create_dt1(parsed->dest_local_ref, out->data, out->len);
859 if (!sccp) {
860 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
861 talloc_free(new_number);
862 talloc_free(out);
863 return msg;
864 }
865
866 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
867
868 /* give up memory, we are done */
869 talloc_free(new_number);
870 /* the parsed hangs off from msg but it needs to survive */
871 talloc_steal(sccp, parsed);
872 msgb_free(msg);
873 msgb_free(out);
874 out = NULL;
875 return sccp;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200876}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200877