blob: 512656a6b8de408e19db7e4c5bafef8caaa6fa6b [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);
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +020093 INIT_LLIST_HEAD(&nat->paging_groups);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080094 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080095 INIT_LLIST_HEAD(&nat->access_lists);
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020096 INIT_LLIST_HEAD(&nat->dests);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080097
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020098 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
99 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
100 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
101 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
102 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200103 nat->stats.ussd.reconn = counter_alloc("nat.ussd.conn");
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800104 nat->auth_timeout = 2;
105 nat->ping_timeout = 20;
106 nat->pong_timeout = 5;
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200107
108 llist_add(&nat->main_dest->list, &nat->dests);
109 nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1");
110 nat->main_dest->port = 5000;
111
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800112 return nat;
113}
114
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800115void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
116{
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200117 bsc_replace_string(nat, &nat->main_dest->ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800118}
119
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800120struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
121{
122 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
123 if (!con)
124 return NULL;
125
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200126 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +0800127 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800128 return con;
129}
130
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800131struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800132{
133 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
134 if (!conf)
135 return NULL;
136
137 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800138 conf->nr = nat->num_bsc;
139 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100140 conf->max_endpoints = 32;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200141 conf->paging_group = PAGIN_GROUP_UNASSIGNED;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800142
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800143 INIT_LLIST_HEAD(&conf->lac_list);
144
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200145 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800146 ++nat->num_bsc;
147
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800148 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800149 if (!conf->stats.ctrg) {
150 talloc_free(conf);
151 return NULL;
152 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200153
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800154 return conf;
155}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200156
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100157void bsc_config_free(struct bsc_config *cfg)
158{
159 rate_ctr_group_free(cfg->stats.ctrg);
160}
161
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800162void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
163{
164 struct bsc_lac_entry *lac;
165
166 llist_for_each_entry(lac, &cfg->lac_list, entry)
167 if (lac->lac == _lac)
168 return;
169
170 lac = talloc_zero(cfg, struct bsc_lac_entry);
171 if (!lac) {
172 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
173 return;
174 }
175
176 lac->lac = _lac;
177 llist_add_tail(&lac->entry, &cfg->lac_list);
178}
179
180void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
181{
182 struct bsc_lac_entry *lac;
183
184 llist_for_each_entry(lac, &cfg->lac_list, entry)
185 if (lac->lac == _lac) {
186 llist_del(&lac->entry);
187 talloc_free(lac);
188 return;
189 }
190}
191
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200192static struct bsc_nat_paging_group *bsc_nat_paging_group_num(
193 struct bsc_nat *nat, int group)
194{
195 struct bsc_nat_paging_group *pgroup;
196
197 llist_for_each_entry(pgroup, &nat->paging_groups, entry)
198 if (pgroup->nr == group)
199 return pgroup;
200
201 return NULL;
202}
203
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800204int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
205{
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200206 struct bsc_nat_paging_group *pgroup;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800207 struct bsc_lac_entry *entry;
208
209 llist_for_each_entry(entry, &cfg->lac_list, entry)
210 if (entry->lac == lac_nr)
211 return 1;
212
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200213 /* now lookup the paging group */
214 pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group);
215 if (!pgroup)
216 return 0;
217
218 llist_for_each_entry(entry, &pgroup->lists, entry)
219 if (entry->lac == lac_nr)
220 return 1;
221
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800222 return 0;
223}
224
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200225void sccp_connection_destroy(struct sccp_connections *conn)
226{
227 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
228 sccp_src_ref_to_int(&conn->real_ref),
229 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800230 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200231 llist_del(&conn->list_entry);
232 talloc_free(conn);
233}
234
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200235
236int bsc_nat_find_paging(struct msgb *msg,
237 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200238{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200239 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800240 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200241 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800242
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200243 if (!msg->l3h || msgb_l3len(msg) < 3) {
244 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200245 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200246 }
247
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200248 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
249 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
250 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200251 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200252 }
253
254 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
255 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800256
257 /* No need to try a different BSS */
258 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200259 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800260 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200261 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200262 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200263 }
264
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200265 *out_data = &data[1];
266 *out_leng = data_length - 1;
267 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200268}
269
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800270int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200271{
272 struct msgb *msg;
273
274 if (length > 4096 - 128) {
275 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
276 return -1;
277 }
278
279 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
280 if (!msg) {
281 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
282 return -1;
283 }
284
285 /* copy the data */
286 msg->l3h = msgb_put(msg, length);
287 memcpy(msg->l3h, data, length);
288
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100289 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200290}
291
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200292int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200293{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200294 return bsc_do_write(&bsc->write_queue, msg, proto);
295}
296
297int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto)
298{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200299 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200300 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200301 return bsc_write_msg(queue, msg);
302}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200303
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200304int bsc_write_msg(struct write_queue *queue, struct msgb *msg)
305{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200306 if (write_queue_enqueue(queue, msg) != 0) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200307 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
308 msgb_free(msg);
309 return -1;
310 }
311
312 return 0;
313}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800314
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200315int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800316{
317 struct bsc_nat_acc_lst_entry *entry;
318
319 llist_for_each_entry(entry, &lst->fltr_list, list) {
320 if (!entry->imsi_allow)
321 continue;
322 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
323 return 0;
324 }
325
326 return 1;
327}
328
329static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
330{
331 struct bsc_nat_acc_lst_entry *entry;
332
333 llist_for_each_entry(entry, &lst->fltr_list, list) {
334 if (!entry->imsi_deny)
335 continue;
336 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
337 return 0;
338 }
339
340 return 1;
341}
342
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800343/* apply white/black list */
344static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
345{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800346 /*
347 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200348 * 1.) Allow directly if the IMSI is allowed at the BSC
349 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800350 * 3.) Reject if the IMSI not allowed at the global level.
351 * 4.) Allow directly if the IMSI is allowed at the global level
352 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800353 struct bsc_nat_acc_lst *nat_lst = NULL;
354 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800355
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800356 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
357 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800358
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800359
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800360 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200361 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200362 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200363 return 1;
364
365 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800366 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800367 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800368 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800369 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800370 return -2;
371 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800372
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800373 }
374
375 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800376 if (nat_lst) {
377 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800378 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800379 "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 +0800380 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800381 return -3;
382 }
383 }
384
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800385 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800386}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800387
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800388static int _cr_check_loc_upd(struct bsc_connection *bsc,
389 uint8_t *data, unsigned int length,
390 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800391{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800392 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800393 struct gsm48_loc_upd_req *lu;
394 char mi_string[GSM48_MI_SIZE];
395
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800396 if (length < sizeof(*lu)) {
397 LOGP(DNAT, LOGL_ERROR,
398 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800399 return -1;
400 }
401
402 lu = (struct gsm48_loc_upd_req *) data;
403 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
404
405 /*
406 * We can only deal with the IMSI. This will fail for a phone that
407 * will send the TMSI of a previous network to us.
408 */
409 if (mi_type != GSM_MI_TYPE_IMSI)
410 return 0;
411
412 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800413 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800414 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800415}
416
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800417static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
418 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800419 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800420{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800421 static const uint32_t classmark_offset =
422 offsetof(struct gsm48_service_request, classmark);
423
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800424 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800425 uint8_t mi_type;
426 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800427 struct gsm48_service_request *req;
428
429 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800430
431 if (length < sizeof(*req)) {
432 LOGP(DNAT, LOGL_ERROR,
433 "CM Serv Req does not fit. Length is %d\n", length);
434 return -1;
435 }
436
437 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800438 if (req->cm_service_type == 0x8)
439 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800440 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
441 length - classmark_offset, mi_string, &mi_type);
442 if (rc < 0) {
443 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800444 return -1;
445 }
446
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800447 /* we have to let the TMSI or such pass */
448 if (mi_type != GSM_MI_TYPE_IMSI)
449 return 0;
450
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800451 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800452 return auth_imsi(bsc, mi_string);
453}
454
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800455static int _cr_check_pag_resp(struct bsc_connection *bsc,
456 uint8_t *data, unsigned int length,
457 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800458{
459 struct gsm48_pag_resp *resp;
460 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800461 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800462
463 if (length < sizeof(*resp)) {
464 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
465 return -1;
466 }
467
468 resp = (struct gsm48_pag_resp *) data;
469 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
470 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
471 return -1;
472 }
473
474 /* we need to let it pass for now */
475 if (mi_type != GSM_MI_TYPE_IMSI)
476 return 0;
477
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800478 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800479 return auth_imsi(bsc, mi_string);
480}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800481
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800482static int _dt_check_id_resp(struct bsc_connection *bsc,
483 uint8_t *data, unsigned int length,
484 struct sccp_connections *con)
485{
486 char mi_string[GSM48_MI_SIZE];
487 uint8_t mi_type;
488 int ret;
489
490 if (length < 2) {
491 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
492 return -1;
493 }
494
495 if (data[0] < length - 1) {
496 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
497 return -2;
498 }
499
500 mi_type = data[1] & GSM_MI_TYPE_MASK;
501 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
502
503 if (mi_type != GSM_MI_TYPE_IMSI)
504 return 0;
505
506 ret = auth_imsi(bsc, mi_string);
507 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800508 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800509 return ret;
510}
511
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800512/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800513int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
514 struct bsc_nat_parsed *parsed, int *con_type,
515 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800516{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800517 struct tlv_parsed tp;
518 struct gsm48_hdr *hdr48;
519 int hdr48_len;
520 int len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200521 uint8_t msg_type, proto;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800522
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800523 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800524 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800525
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800526 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
527 LOGP(DNAT, LOGL_ERROR,
528 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
529 return -1;
530 }
531
532 /* the parsed has had some basic l3 length check */
533 len = msg->l3h[1];
534 if (msgb_l3len(msg) - 3 < len) {
535 LOGP(DNAT, LOGL_ERROR,
536 "The CR Data has not enough space...\n");
537 return -1;
538 }
539
540 msg->l4h = &msg->l3h[3];
541 len -= 1;
542
543 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
544
545 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
546 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
547 return -1;
548 }
549
550 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
551
552 if (hdr48_len < sizeof(*hdr48)) {
553 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
554 return -1;
555 }
556
557 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
558
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200559 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800560 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200561 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800562 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800563 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800564 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200565 } else if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800566 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800567 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800568 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
569 hdr48_len - sizeof(*hdr48),
570 con_type, imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200571 } else if (proto == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800572 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800573 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800574 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800575 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800576 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800577 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800578 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800579 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800580}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800581
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800582struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
583 struct msgb *msg, uint32_t *len)
584{
585 /* gsm_type is actually the size of the dtap */
586 *len = parsed->gsm_type;
587 if (*len < msgb_l3len(msg) - 3) {
588 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
589 return NULL;
590 }
591
592 if (*len < sizeof(struct gsm48_hdr)) {
593 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
594 return NULL;
595 }
596
597 msg->l4h = &msg->l3h[3];
598 return (struct gsm48_hdr *) msg->l4h;
599}
600
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800601int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
602 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
603{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800604 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200605 uint8_t msg_type, proto;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800606 struct gsm48_hdr *hdr48;
607
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800608 if (con->imsi_checked)
609 return 0;
610
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800611 /* only care about DTAP messages */
612 if (parsed->bssap != BSSAP_MSG_DTAP)
613 return 0;
614
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800615 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
616 if (!hdr48)
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800617 return -1;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800618
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200619 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800620 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200621 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800622 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800623 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
624 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800625 return 0;
626 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800627}
628
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200629int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800630{
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200631 int ret;
632
633 ret = 0;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800634 if (*imsi) {
635 talloc_free(*imsi);
636 *imsi = NULL;
637 }
638 regfree(reg);
639
640 if (argc > 0) {
641 *imsi = talloc_strdup(ctx, argv[0]);
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200642 ret = regcomp(reg, argv[0], 0);
643
644 /* handle compilation failures */
645 if (ret != 0) {
646 talloc_free(*imsi);
647 *imsi = NULL;
648 }
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800649 }
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200650
651 return ret;
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800652}
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800653
654static const char *con_types [] = {
655 [NAT_CON_TYPE_NONE] = "n/a",
656 [NAT_CON_TYPE_LU] = "Location Update",
657 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
658 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800659 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800660 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800661 [NAT_CON_TYPE_OTHER] = "Other",
662};
663
664const char *bsc_con_type_to_string(int type)
665{
666 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800667}
668
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800669struct 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 +0800670{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800671 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800672
673 if (!name)
674 return NULL;
675
676 llist_for_each_entry(lst, &nat->access_lists, list)
677 if (strcmp(lst->name, name) == 0)
678 return lst;
679
680 return NULL;
681}
682
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800683struct 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 +0800684{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800685 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800686
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800687 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800688 if (lst)
689 return lst;
690
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800691 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800692 if (!lst) {
693 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
694 return NULL;
695 }
696
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800697 /* TODO: get the index right */
698 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
699 if (!lst->stats) {
700 talloc_free(lst);
701 return NULL;
702 }
703
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800704 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800705 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800706 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800707 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800708}
709
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800710void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800711{
712 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800713 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800714 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800715}
716
717struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
718{
719 struct bsc_nat_acc_lst_entry *entry;
720
721 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
722 if (!entry)
723 return NULL;
724
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800725 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800726 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800727}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800728
729int bsc_nat_msc_is_connected(struct bsc_nat *nat)
730{
731 return nat->msc_con->is_connected;
732}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800733
734static const int con_to_ctr[] = {
735 [NAT_CON_TYPE_NONE] = -1,
736 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
737 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
738 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800739 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800740 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
741 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
742};
743
744int bsc_conn_type_to_ctr(struct sccp_connections *conn)
745{
746 return con_to_ctr[conn->con_type];
747}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200748
749int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
750{
751 int rc;
752
753 rc = write(bfd->fd, msg->data, msg->len);
754 if (rc != msg->len)
755 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
756
757 return rc;
758}
759
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200760/**
761 * Rewrite non global numbers... according to rules based on the IMSI
762 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200763struct 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 +0200764{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200765 struct tlv_parsed tp;
766 struct gsm48_hdr *hdr48;
767 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200768 uint8_t msg_type, proto;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200769 unsigned int payload_len;
770 struct gsm_mncc_number called;
771 struct msg_entry *entry;
772 char *new_number = NULL;
773 struct msgb *out, *sccp;
774 uint8_t *outptr;
775 const uint8_t *msgptr;
776 int sec_len;
777
778 if (!imsi || strlen(imsi) < 5)
779 return msg;
780
781 if (!nat->num_rewr)
782 return msg;
783
784 /* only care about DTAP messages */
785 if (parsed->bssap != BSSAP_MSG_DTAP)
786 return msg;
787 if (!parsed->dest_local_ref)
788 return msg;
789
790 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
791 if (!hdr48)
792 return msg;
793
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200794 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200795 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200796 if (proto != GSM48_PDISC_CC ||
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200797 msg_type != GSM48_MT_CC_SETUP)
798 return msg;
799
800 /* decode and rewrite the message */
801 payload_len = len - sizeof(*hdr48);
802 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
803
804 /* no number, well let us ignore it */
805 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
806 return msg;
807
808 memset(&called, 0, sizeof(called));
809 gsm48_decode_called(&called,
810 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
811
812 /* check if it looks international and stop */
Holger Hans Peter Freyther643931d2010-10-27 10:24:34 +0200813 if (called.plan != 1)
814 return msg;
815 if (called.type == 1)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200816 return msg;
817 if (strncmp(called.number, "00", 2) == 0)
818 return msg;
819
820 /* need to find a replacement and then fix it */
821 llist_for_each_entry(entry, &nat->num_rewr->entry, list) {
822 regex_t reg;
823 regmatch_t matches[2];
824
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200825 if (entry->mcc[0] != '*' && strncmp(entry->mcc, imsi, 3) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200826 continue;
Holger Hans Peter Freytherd414c062010-10-28 13:51:28 +0200827 if (entry->mnc[0] != '*' && strncmp(entry->mnc, imsi + 3, 2) != 0)
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200828 continue;
829
Holger Hans Peter Freyther14003102010-10-27 10:24:12 +0200830 if (entry->text[0] == '+') {
831 LOGP(DNAT, LOGL_ERROR,
832 "Plus is not allowed in the number");
833 continue;
834 }
835
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200836 /* We have an entry for the IMSI. Need to match now */
837 if (regcomp(&reg, entry->option, REG_EXTENDED) != 0) {
838 LOGP(DNAT, LOGL_ERROR,
839 "Regexp '%s' is not valid.\n", entry->option);
840 continue;
841 }
842
843 /* this regexp matches... */
844 if (regexec(&reg, called.number, 2, matches, 0) == 0 &&
845 matches[1].rm_eo != -1)
846 new_number = talloc_asprintf(msg, "%s%s",
847 entry->text,
848 &called.number[matches[1].rm_so]);
849 regfree(&reg);
850
851 if (new_number)
852 break;
853 }
854
855 if (!new_number) {
856 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
857 return msg;
858 }
859
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100860 if (strlen(new_number) > sizeof(called.number)) {
861 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
862 talloc_free(new_number);
863 return msg;
864 }
865
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200866 /*
867 * Need to create a new message now based on the old onew
868 * with a new number. We can sadly not patch this in place
869 * so we will need to regenerate it.
870 */
871
872 out = msgb_alloc_headroom(4096, 128, "changed-setup");
873 if (!out) {
874 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
875 talloc_free(new_number);
876 return msg;
877 }
878
879 /* copy the header */
880 outptr = msgb_put(out, sizeof(*hdr48));
881 memcpy(outptr, hdr48, sizeof(*hdr48));
882
883 /* copy everything up to the number */
884 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
885 outptr = msgb_put(out, sec_len);
886 memcpy(outptr, &hdr48->data[0], sec_len);
887
888 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100889 if (strncmp(new_number, "00", 2) == 0) {
890 called.type = 1;
891 strncpy(called.number, new_number + 2, sizeof(called.number));
892 } else {
893 strncpy(called.number, new_number, sizeof(called.number));
894 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200895 gsm48_encode_called(out, &called);
896
897 /* copy thre rest */
898 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
899 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
900 sec_len = payload_len - (msgptr - &hdr48->data[0]);
901 outptr = msgb_put(out, sec_len);
902 memcpy(outptr, msgptr, sec_len);
903
904 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
905 gsm0808_prepend_dtap_header(out, 0);
906 sccp = sccp_create_dt1(parsed->dest_local_ref, out->data, out->len);
907 if (!sccp) {
908 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
909 talloc_free(new_number);
910 talloc_free(out);
911 return msg;
912 }
913
914 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
915
916 /* give up memory, we are done */
917 talloc_free(new_number);
918 /* the parsed hangs off from msg but it needs to survive */
919 talloc_steal(sccp, parsed);
920 msgb_free(msg);
921 msgb_free(out);
922 out = NULL;
923 return sccp;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200924}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200925