blob: 0a565a9b73dc14d4e88e01df1f1e062158ae11b7 [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 Freyther69d801e2010-06-15 20:13:33 +080037
Harald Welted5db12c2010-08-03 15:11:51 +020038#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020039
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020040#include <netinet/in.h>
41#include <arpa/inet.h>
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +020042#include <unistd.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020043
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080044static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-06-17 18:31:18 +080045 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
46 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
47 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
48 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
49 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080050 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
51 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
52 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080053 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
54 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
55 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +080056 [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080057 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080058};
59
60static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
61 .group_name_prefix = "nat.bsc",
62 .group_description = "NAT BSC Statistics",
63 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
64 .ctr_desc = bsc_cfg_ctr_description,
65};
66
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080067static const struct rate_ctr_desc acc_list_ctr_description[] = {
68 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
69 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
70};
71
72static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
73 .group_name_prefix = "nat.filter",
74 .group_description = "NAT Access-List Statistics",
75 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
76 .ctr_desc = acc_list_ctr_description,
77};
78
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080079struct bsc_nat *bsc_nat_alloc(void)
80{
81 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
82 if (!nat)
83 return NULL;
84
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020085 nat->main_dest = talloc_zero(nat, struct bsc_msc_dest);
86 if (!nat->main_dest) {
87 talloc_free(nat);
88 return NULL;
89 }
90
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080091 INIT_LLIST_HEAD(&nat->sccp_connections);
92 INIT_LLIST_HEAD(&nat->bsc_connections);
93 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080094 INIT_LLIST_HEAD(&nat->access_lists);
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020095 INIT_LLIST_HEAD(&nat->dests);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080096
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020097 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
98 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
99 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
100 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
101 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200102 nat->stats.ussd.reconn = counter_alloc("nat.ussd.conn");
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800103 nat->auth_timeout = 2;
104 nat->ping_timeout = 20;
105 nat->pong_timeout = 5;
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200106
107 llist_add(&nat->main_dest->list, &nat->dests);
108 nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1");
109 nat->main_dest->port = 5000;
110
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800111 return nat;
112}
113
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800114void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
115{
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200116 bsc_replace_string(nat, &nat->main_dest->ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800117}
118
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800119struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
120{
121 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
122 if (!con)
123 return NULL;
124
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200125 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800126 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800127 return con;
128}
129
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800130struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800131{
132 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
133 if (!conf)
134 return NULL;
135
136 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800137 conf->nr = nat->num_bsc;
138 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100139 conf->max_endpoints = 32;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800140
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800141 INIT_LLIST_HEAD(&conf->lac_list);
142
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200143 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800144 ++nat->num_bsc;
145
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800146 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800147 if (!conf->stats.ctrg) {
148 talloc_free(conf);
149 return NULL;
150 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200151
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800152 return conf;
153}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200154
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100155void bsc_config_free(struct bsc_config *cfg)
156{
157 rate_ctr_group_free(cfg->stats.ctrg);
158}
159
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800160void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
161{
162 struct bsc_lac_entry *lac;
163
164 llist_for_each_entry(lac, &cfg->lac_list, entry)
165 if (lac->lac == _lac)
166 return;
167
168 lac = talloc_zero(cfg, struct bsc_lac_entry);
169 if (!lac) {
170 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
171 return;
172 }
173
174 lac->lac = _lac;
175 llist_add_tail(&lac->entry, &cfg->lac_list);
176}
177
178void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
179{
180 struct bsc_lac_entry *lac;
181
182 llist_for_each_entry(lac, &cfg->lac_list, entry)
183 if (lac->lac == _lac) {
184 llist_del(&lac->entry);
185 talloc_free(lac);
186 return;
187 }
188}
189
190int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
191{
192 struct bsc_lac_entry *entry;
193
194 llist_for_each_entry(entry, &cfg->lac_list, entry)
195 if (entry->lac == lac_nr)
196 return 1;
197
198 return 0;
199}
200
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200201void sccp_connection_destroy(struct sccp_connections *conn)
202{
203 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
204 sccp_src_ref_to_int(&conn->real_ref),
205 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800206 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200207 llist_del(&conn->list_entry);
208 talloc_free(conn);
209}
210
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200211
212int bsc_nat_find_paging(struct msgb *msg,
213 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200214{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200215 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800216 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200217 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800218
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200219 if (!msg->l3h || msgb_l3len(msg) < 3) {
220 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200221 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200222 }
223
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200224 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
225 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
226 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200227 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200228 }
229
230 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
231 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800232
233 /* No need to try a different BSS */
234 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200235 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800236 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200237 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200238 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200239 }
240
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200241 *out_data = &data[1];
242 *out_leng = data_length - 1;
243 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200244}
245
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800246int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200247{
248 struct msgb *msg;
249
250 if (length > 4096 - 128) {
251 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
252 return -1;
253 }
254
255 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
256 if (!msg) {
257 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
258 return -1;
259 }
260
261 /* copy the data */
262 msg->l3h = msgb_put(msg, length);
263 memcpy(msg->l3h, data, length);
264
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100265 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200266}
267
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200268int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200269{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200270 return bsc_do_write(&bsc->write_queue, msg, proto);
271}
272
273int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto)
274{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200275 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200276 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200277 return bsc_write_msg(queue, msg);
278}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200279
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200280int bsc_write_msg(struct write_queue *queue, struct msgb *msg)
281{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200282 if (write_queue_enqueue(queue, msg) != 0) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200283 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
284 msgb_free(msg);
285 return -1;
286 }
287
288 return 0;
289}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800290
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200291int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800292{
293 struct bsc_nat_acc_lst_entry *entry;
294
295 llist_for_each_entry(entry, &lst->fltr_list, list) {
296 if (!entry->imsi_allow)
297 continue;
298 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
299 return 0;
300 }
301
302 return 1;
303}
304
305static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
306{
307 struct bsc_nat_acc_lst_entry *entry;
308
309 llist_for_each_entry(entry, &lst->fltr_list, list) {
310 if (!entry->imsi_deny)
311 continue;
312 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
313 return 0;
314 }
315
316 return 1;
317}
318
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800319/* apply white/black list */
320static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
321{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800322 /*
323 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200324 * 1.) Allow directly if the IMSI is allowed at the BSC
325 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800326 * 3.) Reject if the IMSI not allowed at the global level.
327 * 4.) Allow directly if the IMSI is allowed at the global level
328 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800329 struct bsc_nat_acc_lst *nat_lst = NULL;
330 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800331
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800332 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
333 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800334
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800335
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800336 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200337 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200338 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200339 return 1;
340
341 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800342 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800343 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800344 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800345 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800346 return -2;
347 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800348
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800349 }
350
351 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800352 if (nat_lst) {
353 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800354 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800355 "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 +0800356 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800357 return -3;
358 }
359 }
360
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800361 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800362}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800363
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800364static int _cr_check_loc_upd(struct bsc_connection *bsc,
365 uint8_t *data, unsigned int length,
366 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800367{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800368 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800369 struct gsm48_loc_upd_req *lu;
370 char mi_string[GSM48_MI_SIZE];
371
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800372 if (length < sizeof(*lu)) {
373 LOGP(DNAT, LOGL_ERROR,
374 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800375 return -1;
376 }
377
378 lu = (struct gsm48_loc_upd_req *) data;
379 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
380
381 /*
382 * We can only deal with the IMSI. This will fail for a phone that
383 * will send the TMSI of a previous network to us.
384 */
385 if (mi_type != GSM_MI_TYPE_IMSI)
386 return 0;
387
388 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800389 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800390 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800391}
392
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800393static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
394 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800395 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800396{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800397 static const uint32_t classmark_offset =
398 offsetof(struct gsm48_service_request, classmark);
399
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800400 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800401 uint8_t mi_type;
402 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800403 struct gsm48_service_request *req;
404
405 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800406
407 if (length < sizeof(*req)) {
408 LOGP(DNAT, LOGL_ERROR,
409 "CM Serv Req does not fit. Length is %d\n", length);
410 return -1;
411 }
412
413 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800414 if (req->cm_service_type == 0x8)
415 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800416 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
417 length - classmark_offset, mi_string, &mi_type);
418 if (rc < 0) {
419 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800420 return -1;
421 }
422
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800423 /* we have to let the TMSI or such pass */
424 if (mi_type != GSM_MI_TYPE_IMSI)
425 return 0;
426
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800427 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800428 return auth_imsi(bsc, mi_string);
429}
430
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800431static int _cr_check_pag_resp(struct bsc_connection *bsc,
432 uint8_t *data, unsigned int length,
433 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800434{
435 struct gsm48_pag_resp *resp;
436 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800437 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800438
439 if (length < sizeof(*resp)) {
440 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
441 return -1;
442 }
443
444 resp = (struct gsm48_pag_resp *) data;
445 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
446 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
447 return -1;
448 }
449
450 /* we need to let it pass for now */
451 if (mi_type != GSM_MI_TYPE_IMSI)
452 return 0;
453
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800454 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800455 return auth_imsi(bsc, mi_string);
456}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800457
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800458static int _dt_check_id_resp(struct bsc_connection *bsc,
459 uint8_t *data, unsigned int length,
460 struct sccp_connections *con)
461{
462 char mi_string[GSM48_MI_SIZE];
463 uint8_t mi_type;
464 int ret;
465
466 if (length < 2) {
467 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
468 return -1;
469 }
470
471 if (data[0] < length - 1) {
472 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
473 return -2;
474 }
475
476 mi_type = data[1] & GSM_MI_TYPE_MASK;
477 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
478
479 if (mi_type != GSM_MI_TYPE_IMSI)
480 return 0;
481
482 ret = auth_imsi(bsc, mi_string);
483 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800484 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800485 return ret;
486}
487
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800488/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800489int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
490 struct bsc_nat_parsed *parsed, int *con_type,
491 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800492{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800493 struct tlv_parsed tp;
494 struct gsm48_hdr *hdr48;
495 int hdr48_len;
496 int len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200497 uint8_t msg_type, proto;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800498
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800499 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800500 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800501
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800502 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
503 LOGP(DNAT, LOGL_ERROR,
504 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
505 return -1;
506 }
507
508 /* the parsed has had some basic l3 length check */
509 len = msg->l3h[1];
510 if (msgb_l3len(msg) - 3 < len) {
511 LOGP(DNAT, LOGL_ERROR,
512 "The CR Data has not enough space...\n");
513 return -1;
514 }
515
516 msg->l4h = &msg->l3h[3];
517 len -= 1;
518
519 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
520
521 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
522 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
523 return -1;
524 }
525
526 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
527
528 if (hdr48_len < sizeof(*hdr48)) {
529 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
530 return -1;
531 }
532
533 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
534
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200535 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800536 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200537 if (proto == 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 Freyther5cde92c2011-04-13 18:56:13 +0200541 } else if (proto == 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 Freyther5cde92c2011-04-13 18:56:13 +0200547 } else if (proto == 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 Freyther5cde92c2011-04-13 18:56:13 +0200581 uint8_t msg_type, proto;
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 Freyther5cde92c2011-04-13 18:56:13 +0200595 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800596 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200597 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800598 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800599 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
600 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800601 return 0;
602 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800603}
604
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200605int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800606{
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200607 int ret;
608
609 ret = 0;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800610 if (*imsi) {
611 talloc_free(*imsi);
612 *imsi = NULL;
613 }
614 regfree(reg);
615
616 if (argc > 0) {
617 *imsi = talloc_strdup(ctx, argv[0]);
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200618 ret = regcomp(reg, argv[0], 0);
619
620 /* handle compilation failures */
621 if (ret != 0) {
622 talloc_free(*imsi);
623 *imsi = NULL;
624 }
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800625 }
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200626
627 return ret;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800628}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800629
630static const char *con_types [] = {
631 [NAT_CON_TYPE_NONE] = "n/a",
632 [NAT_CON_TYPE_LU] = "Location Update",
633 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
634 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800635 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800636 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800637 [NAT_CON_TYPE_OTHER] = "Other",
638};
639
640const char *bsc_con_type_to_string(int type)
641{
642 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800643}
644
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800645struct 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 +0800646{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800647 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800648
649 if (!name)
650 return NULL;
651
652 llist_for_each_entry(lst, &nat->access_lists, list)
653 if (strcmp(lst->name, name) == 0)
654 return lst;
655
656 return NULL;
657}
658
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800659struct 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 +0800660{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800661 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800662
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800663 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800664 if (lst)
665 return lst;
666
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800667 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800668 if (!lst) {
669 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
670 return NULL;
671 }
672
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800673 /* TODO: get the index right */
674 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
675 if (!lst->stats) {
676 talloc_free(lst);
677 return NULL;
678 }
679
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800680 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800681 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800682 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800683 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800684}
685
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800686void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800687{
688 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800689 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800690 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800691}
692
693struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
694{
695 struct bsc_nat_acc_lst_entry *entry;
696
697 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
698 if (!entry)
699 return NULL;
700
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800701 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800702 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800703}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800704
705int bsc_nat_msc_is_connected(struct bsc_nat *nat)
706{
707 return nat->msc_con->is_connected;
708}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800709
710static const int con_to_ctr[] = {
711 [NAT_CON_TYPE_NONE] = -1,
712 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
713 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
714 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800715 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800716 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
717 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
718};
719
720int bsc_conn_type_to_ctr(struct sccp_connections *conn)
721{
722 return con_to_ctr[conn->con_type];
723}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200724
725int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
726{
727 int rc;
728
729 rc = write(bfd->fd, msg->data, msg->len);
730 if (rc != msg->len)
731 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
732
733 return rc;
734}
735
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200736/**
737 * Rewrite non global numbers... according to rules based on the IMSI
738 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200739struct 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 +0200740{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200741 struct tlv_parsed tp;
742 struct gsm48_hdr *hdr48;
743 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200744 uint8_t msg_type, proto;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200745 unsigned int payload_len;
746 struct gsm_mncc_number called;
747 struct msg_entry *entry;
748 char *new_number = NULL;
749 struct msgb *out, *sccp;
750 uint8_t *outptr;
751 const uint8_t *msgptr;
752 int sec_len;
753
754 if (!imsi || strlen(imsi) < 5)
755 return msg;
756
757 if (!nat->num_rewr)
758 return msg;
759
760 /* only care about DTAP messages */
761 if (parsed->bssap != BSSAP_MSG_DTAP)
762 return msg;
763 if (!parsed->dest_local_ref)
764 return msg;
765
766 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
767 if (!hdr48)
768 return msg;
769
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200770 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200771 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200772 if (proto != GSM48_PDISC_CC ||
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200773 msg_type != GSM48_MT_CC_SETUP)
774 return msg;
775
776 /* decode and rewrite the message */
777 payload_len = len - sizeof(*hdr48);
778 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
779
780 /* no number, well let us ignore it */
781 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
782 return msg;
783
784 memset(&called, 0, sizeof(called));
785 gsm48_decode_called(&called,
786 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
787
788 /* check if it looks international and stop */
Holger Hans Peter Freyther643931d2010-10-27 10:24:34 +0200789 if (called.plan != 1)
790 return msg;
791 if (called.type == 1)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200792 return msg;
793 if (strncmp(called.number, "00", 2) == 0)
794 return msg;
795
796 /* need to find a replacement and then fix it */
797 llist_for_each_entry(entry, &nat->num_rewr->entry, list) {
798 regex_t reg;
799 regmatch_t matches[2];
800
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200801 if (entry->mcc[0] != '*' && strncmp(entry->mcc, imsi, 3) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200802 continue;
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200803 if (entry->mnc[0] != '*' && strncmp(entry->mnc, imsi + 3, 2) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200804 continue;
805
Holger Hans Peter Freyther14003102010-10-27 10:24:12 +0200806 if (entry->text[0] == '+') {
807 LOGP(DNAT, LOGL_ERROR,
808 "Plus is not allowed in the number");
809 continue;
810 }
811
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200812 /* We have an entry for the IMSI. Need to match now */
813 if (regcomp(&reg, entry->option, REG_EXTENDED) != 0) {
814 LOGP(DNAT, LOGL_ERROR,
815 "Regexp '%s' is not valid.\n", entry->option);
816 continue;
817 }
818
819 /* this regexp matches... */
820 if (regexec(&reg, called.number, 2, matches, 0) == 0 &&
821 matches[1].rm_eo != -1)
822 new_number = talloc_asprintf(msg, "%s%s",
823 entry->text,
824 &called.number[matches[1].rm_so]);
825 regfree(&reg);
826
827 if (new_number)
828 break;
829 }
830
831 if (!new_number) {
832 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
833 return msg;
834 }
835
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100836 if (strlen(new_number) > sizeof(called.number)) {
837 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
838 talloc_free(new_number);
839 return msg;
840 }
841
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200842 /*
843 * Need to create a new message now based on the old onew
844 * with a new number. We can sadly not patch this in place
845 * so we will need to regenerate it.
846 */
847
848 out = msgb_alloc_headroom(4096, 128, "changed-setup");
849 if (!out) {
850 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
851 talloc_free(new_number);
852 return msg;
853 }
854
855 /* copy the header */
856 outptr = msgb_put(out, sizeof(*hdr48));
857 memcpy(outptr, hdr48, sizeof(*hdr48));
858
859 /* copy everything up to the number */
860 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
861 outptr = msgb_put(out, sec_len);
862 memcpy(outptr, &hdr48->data[0], sec_len);
863
864 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100865 if (strncmp(new_number, "00", 2) == 0) {
866 called.type = 1;
867 strncpy(called.number, new_number + 2, sizeof(called.number));
868 } else {
869 strncpy(called.number, new_number, sizeof(called.number));
870 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200871 gsm48_encode_called(out, &called);
872
873 /* copy thre rest */
874 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
875 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
876 sec_len = payload_len - (msgptr - &hdr48->data[0]);
877 outptr = msgb_put(out, sec_len);
878 memcpy(outptr, msgptr, sec_len);
879
880 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
881 gsm0808_prepend_dtap_header(out, 0);
882 sccp = sccp_create_dt1(parsed->dest_local_ref, out->data, out->len);
883 if (!sccp) {
884 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
885 talloc_free(new_number);
886 talloc_free(out);
887 return msg;
888 }
889
890 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
891
892 /* give up memory, we are done */
893 talloc_free(new_number);
894 /* the parsed hangs off from msg but it needs to survive */
895 talloc_steal(sccp, parsed);
896 msgb_free(msg);
897 msgb_free(out);
898 out = NULL;
899 return sccp;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200900}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200901