blob: 3d0c30d2f6e71e837310fc27b3e5574f7b382984 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +02005 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080021 *
22 */
23
24#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080025#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +080026#include <openbsc/bsc_msc.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080027#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020028#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020029#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +020030#include <openbsc/vty.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080031
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/linuxlist.h>
33#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010034#include <osmocom/gsm/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080035
Harald Welted36ff762011-03-23 18:26:56 +010036#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +020037#include <osmocom/gsm/protocol/gsm_04_11.h>
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080038
Harald Welted5db12c2010-08-03 15:11:51 +020039#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020040
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020041#include <netinet/in.h>
42#include <arpa/inet.h>
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +020043#include <unistd.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020044
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080045static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-06-17 18:31:18 +080046 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
47 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
48 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
49 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
50 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080051 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
52 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
53 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080054 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
55 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
56 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +080057 [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080058 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080059};
60
61static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
62 .group_name_prefix = "nat.bsc",
63 .group_description = "NAT BSC Statistics",
64 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
65 .ctr_desc = bsc_cfg_ctr_description,
66};
67
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080068static const struct rate_ctr_desc acc_list_ctr_description[] = {
69 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
70 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
71};
72
73static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
74 .group_name_prefix = "nat.filter",
75 .group_description = "NAT Access-List Statistics",
76 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
77 .ctr_desc = acc_list_ctr_description,
78};
79
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080080struct bsc_nat *bsc_nat_alloc(void)
81{
82 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
83 if (!nat)
84 return NULL;
85
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +020086 nat->main_dest = talloc_zero(nat, struct bsc_msc_dest);
87 if (!nat->main_dest) {
88 talloc_free(nat);
89 return NULL;
90 }
91
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080092 INIT_LLIST_HEAD(&nat->sccp_connections);
93 INIT_LLIST_HEAD(&nat->bsc_connections);
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +020094 INIT_LLIST_HEAD(&nat->paging_groups);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080095 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080096 INIT_LLIST_HEAD(&nat->access_lists);
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +020097 INIT_LLIST_HEAD(&nat->dests);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +020098 INIT_LLIST_HEAD(&nat->num_rewr);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +020099 INIT_LLIST_HEAD(&nat->smsc_rewr);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200100 INIT_LLIST_HEAD(&nat->tpdest_match);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +0100101 INIT_LLIST_HEAD(&nat->sms_clear_tp_srr);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800102
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200103 nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn");
104 nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls");
105 nat->stats.bsc.reconn = osmo_counter_alloc("nat.bsc.conn");
106 nat->stats.bsc.auth_fail = osmo_counter_alloc("nat.bsc.auth_fail");
107 nat->stats.msc.reconn = osmo_counter_alloc("nat.msc.conn");
108 nat->stats.ussd.reconn = osmo_counter_alloc("nat.ussd.conn");
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800109 nat->auth_timeout = 2;
110 nat->ping_timeout = 20;
111 nat->pong_timeout = 5;
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +0200112
113 llist_add(&nat->main_dest->list, &nat->dests);
114 nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1");
115 nat->main_dest->port = 5000;
116
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800117 return nat;
118}
119
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800120void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
121{
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +0200122 bsc_replace_string(nat, &nat->main_dest->ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800123}
124
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800125struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
126{
127 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
128 if (!con)
129 return NULL;
130
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200131 con->nat = nat;
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200132 osmo_wqueue_init(&con->write_queue, 100);
Daniel Willmanna86bc392011-02-18 14:32:56 +0100133 INIT_LLIST_HEAD(&con->cmd_pending);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800134 return con;
135}
136
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800137struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800138{
139 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
140 if (!conf)
141 return NULL;
142
143 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800144 conf->nr = nat->num_bsc;
145 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100146 conf->max_endpoints = 32;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200147 conf->paging_group = PAGIN_GROUP_UNASSIGNED;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800148
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800149 INIT_LLIST_HEAD(&conf->lac_list);
150
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200151 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800152 ++nat->num_bsc;
153
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800154 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800155 if (!conf->stats.ctrg) {
156 talloc_free(conf);
157 return NULL;
158 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200159
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800160 return conf;
161}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200162
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100163void bsc_config_free(struct bsc_config *cfg)
164{
165 rate_ctr_group_free(cfg->stats.ctrg);
166}
167
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200168static void _add_lac(void *ctx, struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800169{
170 struct bsc_lac_entry *lac;
171
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200172 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800173 if (lac->lac == _lac)
174 return;
175
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200176 lac = talloc_zero(ctx, struct bsc_lac_entry);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800177 if (!lac) {
178 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
179 return;
180 }
181
182 lac->lac = _lac;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200183 llist_add_tail(&lac->entry, list);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800184}
185
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200186static void _del_lac(struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800187{
188 struct bsc_lac_entry *lac;
189
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200190 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800191 if (lac->lac == _lac) {
192 llist_del(&lac->entry);
193 talloc_free(lac);
194 return;
195 }
196}
197
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200198void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
199{
200 _add_lac(cfg, &cfg->lac_list, _lac);
201}
202
203void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
204{
205 _del_lac(&cfg->lac_list, _lac);
206}
207
208struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group)
209{
210 struct bsc_nat_paging_group *pgroup;
211
212 pgroup = talloc_zero(nat, struct bsc_nat_paging_group);
213 if (!pgroup) {
214 LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n");
215 return NULL;
216 }
217
218 pgroup->nr = group;
219 INIT_LLIST_HEAD(&pgroup->lists);
220 llist_add_tail(&pgroup->entry, &nat->paging_groups);
221 return pgroup;
222}
223
224void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup)
225{
226 llist_del(&pgroup->entry);
227 talloc_free(pgroup);
228}
229
230struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group)
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200231{
232 struct bsc_nat_paging_group *pgroup;
233
234 llist_for_each_entry(pgroup, &nat->paging_groups, entry)
235 if (pgroup->nr == group)
236 return pgroup;
237
238 return NULL;
239}
240
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200241void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac)
242{
243 _add_lac(pgroup, &pgroup->lists, lac);
244}
245
246void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac)
247{
248 _del_lac(&pgroup->lists, lac);
249}
250
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800251int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
252{
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200253 struct bsc_nat_paging_group *pgroup;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800254 struct bsc_lac_entry *entry;
255
256 llist_for_each_entry(entry, &cfg->lac_list, entry)
257 if (entry->lac == lac_nr)
258 return 1;
259
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200260 /* now lookup the paging group */
261 pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group);
262 if (!pgroup)
263 return 0;
264
265 llist_for_each_entry(entry, &pgroup->lists, entry)
266 if (entry->lac == lac_nr)
267 return 1;
268
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800269 return 0;
270}
271
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200272void sccp_connection_destroy(struct sccp_connections *conn)
273{
274 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
275 sccp_src_ref_to_int(&conn->real_ref),
276 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800277 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200278 llist_del(&conn->list_entry);
279 talloc_free(conn);
280}
281
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200282
283int bsc_nat_find_paging(struct msgb *msg,
284 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200285{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200286 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800287 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200288 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800289
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200290 if (!msg->l3h || msgb_l3len(msg) < 3) {
291 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200292 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200293 }
294
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200295 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
296 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
297 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200298 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200299 }
300
301 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
302 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800303
304 /* No need to try a different BSS */
305 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200306 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800307 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200308 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200309 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200310 }
311
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200312 *out_data = &data[1];
313 *out_leng = data_length - 1;
314 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200315}
316
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800317int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200318{
319 struct msgb *msg;
320
321 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200322 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200323 return -1;
324 }
325
326 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
327 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200328 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200329 return -1;
330 }
331
332 /* copy the data */
333 msg->l3h = msgb_put(msg, length);
334 memcpy(msg->l3h, data, length);
335
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100336 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200337}
338
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200339int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200340{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200341 return bsc_do_write(&bsc->write_queue, msg, proto);
342}
343
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200344int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto)
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200345{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200346 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200347 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200348 return bsc_write_msg(queue, msg);
349}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200350
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200351int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg)
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200352{
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200353 if (osmo_wqueue_enqueue(queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200354 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200355 msgb_free(msg);
356 return -1;
357 }
358
359 return 0;
360}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800361
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200362int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800363{
364 struct bsc_nat_acc_lst_entry *entry;
365
366 llist_for_each_entry(entry, &lst->fltr_list, list) {
367 if (!entry->imsi_allow)
368 continue;
369 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
370 return 0;
371 }
372
373 return 1;
374}
375
376static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string)
377{
378 struct bsc_nat_acc_lst_entry *entry;
379
380 llist_for_each_entry(entry, &lst->fltr_list, list) {
381 if (!entry->imsi_deny)
382 continue;
383 if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0)
384 return 0;
385 }
386
387 return 1;
388}
389
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800390/* apply white/black list */
391static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
392{
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800393 /*
394 * Now apply blacklist/whitelist of the BSC and the NAT.
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200395 * 1.) Allow directly if the IMSI is allowed at the BSC
396 * 2.) Reject if the IMSI is not allowed at the BSC
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800397 * 3.) Reject if the IMSI not allowed at the global level.
398 * 4.) Allow directly if the IMSI is allowed at the global level
399 */
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800400 struct bsc_nat_acc_lst *nat_lst = NULL;
401 struct bsc_nat_acc_lst *bsc_lst = NULL;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800402
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800403 bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name);
404 nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800405
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800406
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800407 if (bsc_lst) {
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200408 /* 1. BSC allow */
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200409 if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0)
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200410 return 1;
411
412 /* 2. BSC deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800413 if (lst_check_deny(bsc_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800414 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800415 "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800416 rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800417 return -2;
418 }
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800419
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800420 }
421
422 /* 3. NAT deny */
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800423 if (nat_lst) {
424 if (lst_check_deny(nat_lst, mi_string) == 0) {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800425 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther48945b12010-05-16 00:45:07 +0800426 "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 +0800427 rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800428 return -3;
429 }
430 }
431
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800432 return 1;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800433}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800434
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800435static int _cr_check_loc_upd(struct bsc_connection *bsc,
436 uint8_t *data, unsigned int length,
437 char **imsi)
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800438{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800439 uint8_t mi_type;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800440 struct gsm48_loc_upd_req *lu;
441 char mi_string[GSM48_MI_SIZE];
442
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800443 if (length < sizeof(*lu)) {
444 LOGP(DNAT, LOGL_ERROR,
445 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800446 return -1;
447 }
448
449 lu = (struct gsm48_loc_upd_req *) data;
450 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
451
452 /*
453 * We can only deal with the IMSI. This will fail for a phone that
454 * will send the TMSI of a previous network to us.
455 */
456 if (mi_type != GSM_MI_TYPE_IMSI)
457 return 0;
458
459 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800460 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800461 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800462}
463
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800464static int _cr_check_cm_serv_req(struct bsc_connection *bsc,
465 uint8_t *data, unsigned int length,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800466 int *con_type, char **imsi)
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800467{
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800468 static const uint32_t classmark_offset =
469 offsetof(struct gsm48_service_request, classmark);
470
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800471 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800472 uint8_t mi_type;
473 int rc;
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800474 struct gsm48_service_request *req;
475
476 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800477
478 if (length < sizeof(*req)) {
479 LOGP(DNAT, LOGL_ERROR,
480 "CM Serv Req does not fit. Length is %d\n", length);
481 return -1;
482 }
483
484 req = (struct gsm48_service_request *) data;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800485 if (req->cm_service_type == 0x8)
486 *con_type = NAT_CON_TYPE_SSA;
Holger Hans Peter Freyther66e1ef72010-05-16 01:13:28 +0800487 rc = gsm48_extract_mi((uint8_t *) &req->classmark,
488 length - classmark_offset, mi_string, &mi_type);
489 if (rc < 0) {
490 LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800491 return -1;
492 }
493
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800494 /* we have to let the TMSI or such pass */
495 if (mi_type != GSM_MI_TYPE_IMSI)
496 return 0;
497
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800498 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherbcb32a42010-05-15 21:58:33 +0800499 return auth_imsi(bsc, mi_string);
500}
501
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800502static int _cr_check_pag_resp(struct bsc_connection *bsc,
503 uint8_t *data, unsigned int length,
504 char **imsi)
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800505{
506 struct gsm48_pag_resp *resp;
507 char mi_string[GSM48_MI_SIZE];
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800508 uint8_t mi_type;
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800509
510 if (length < sizeof(*resp)) {
511 LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length);
512 return -1;
513 }
514
515 resp = (struct gsm48_pag_resp *) data;
516 if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) {
517 LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n");
518 return -1;
519 }
520
521 /* we need to let it pass for now */
522 if (mi_type != GSM_MI_TYPE_IMSI)
523 return 0;
524
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800525 *imsi = talloc_strdup(bsc, mi_string);
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800526 return auth_imsi(bsc, mi_string);
527}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800528
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800529static int _dt_check_id_resp(struct bsc_connection *bsc,
530 uint8_t *data, unsigned int length,
531 struct sccp_connections *con)
532{
533 char mi_string[GSM48_MI_SIZE];
534 uint8_t mi_type;
535 int ret;
536
537 if (length < 2) {
538 LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n");
539 return -1;
540 }
541
542 if (data[0] < length - 1) {
543 LOGP(DNAT, LOGL_ERROR, "mi length too big.\n");
544 return -2;
545 }
546
547 mi_type = data[1] & GSM_MI_TYPE_MASK;
548 gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]);
549
550 if (mi_type != GSM_MI_TYPE_IMSI)
551 return 0;
552
553 ret = auth_imsi(bsc, mi_string);
554 con->imsi_checked = 1;
Holger Hans Peter Freyther8c78b482010-09-29 01:00:46 +0800555 con->imsi = talloc_strdup(con, mi_string);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800556 return ret;
557}
558
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800559/* Filter out CR data... */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800560int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
561 struct bsc_nat_parsed *parsed, int *con_type,
562 char **imsi)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800563{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800564 struct tlv_parsed tp;
565 struct gsm48_hdr *hdr48;
566 int hdr48_len;
567 int len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200568 uint8_t msg_type, proto;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800569
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800570 *con_type = NAT_CON_TYPE_NONE;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800571 *imsi = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800572
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800573 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
574 LOGP(DNAT, LOGL_ERROR,
575 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
576 return -1;
577 }
578
579 /* the parsed has had some basic l3 length check */
580 len = msg->l3h[1];
581 if (msgb_l3len(msg) - 3 < len) {
582 LOGP(DNAT, LOGL_ERROR,
583 "The CR Data has not enough space...\n");
584 return -1;
585 }
586
587 msg->l4h = &msg->l3h[3];
588 len -= 1;
589
590 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
591
592 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
593 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
594 return -1;
595 }
596
597 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
598
599 if (hdr48_len < sizeof(*hdr48)) {
600 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
601 return -1;
602 }
603
604 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
605
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200606 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800607 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200608 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800609 msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800610 *con_type = NAT_CON_TYPE_LU;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800611 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200612 } else if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800613 msg_type == GSM48_MT_MM_CM_SERV_REQ) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800614 *con_type = NAT_CON_TYPE_CM_SERV_REQ;
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800615 return _cr_check_cm_serv_req(bsc, &hdr48->data[0],
616 hdr48_len - sizeof(*hdr48),
617 con_type, imsi);
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200618 } else if (proto == GSM48_PDISC_RR &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800619 msg_type == GSM48_MT_RR_PAG_RESP) {
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800620 *con_type = NAT_CON_TYPE_PAG_RESP;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800621 return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800622 } else {
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800623 /* We only want to filter the above, let other things pass */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800624 *con_type = NAT_CON_TYPE_OTHER;
Holger Hans Peter Freyther1f387472010-05-16 01:05:47 +0800625 return 0;
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800626 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800627}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800628
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800629struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
630 struct msgb *msg, uint32_t *len)
631{
632 /* gsm_type is actually the size of the dtap */
633 *len = parsed->gsm_type;
634 if (*len < msgb_l3len(msg) - 3) {
635 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
636 return NULL;
637 }
638
Holger Hans Peter Freytherc2c0ea72012-01-10 22:27:43 +0100639 if (msgb_l3len(msg) - 3 < msg->l3h[2]) {
640 LOGP(DNAT, LOGL_ERROR,
641 "GSM48 payload does not fit: %d %d\n",
642 msg->l3h[2], msgb_l3len(msg) - 3);
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800643 return NULL;
644 }
645
646 msg->l4h = &msg->l3h[3];
647 return (struct gsm48_hdr *) msg->l4h;
648}
649
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800650int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
651 struct sccp_connections *con, struct bsc_nat_parsed *parsed)
652{
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800653 uint32_t len;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200654 uint8_t msg_type, proto;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800655 struct gsm48_hdr *hdr48;
656
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800657 if (con->imsi_checked)
658 return 0;
659
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800660 /* only care about DTAP messages */
661 if (parsed->bssap != BSSAP_MSG_DTAP)
662 return 0;
663
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800664 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
665 if (!hdr48)
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800666 return -1;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800667
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200668 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800669 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200670 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freyther11ebe1b2010-09-15 05:24:25 +0800671 msg_type == GSM48_MT_MM_ID_RESP) {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800672 return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con);
673 } else {
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800674 return 0;
675 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800676}
677
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800678static const char *con_types [] = {
679 [NAT_CON_TYPE_NONE] = "n/a",
680 [NAT_CON_TYPE_LU] = "Location Update",
681 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
682 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800683 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800684 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800685 [NAT_CON_TYPE_OTHER] = "Other",
686};
687
688const char *bsc_con_type_to_string(int type)
689{
690 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800691}
692
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800693struct 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 +0800694{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800695 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800696
697 if (!name)
698 return NULL;
699
700 llist_for_each_entry(lst, &nat->access_lists, list)
701 if (strcmp(lst->name, name) == 0)
702 return lst;
703
704 return NULL;
705}
706
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800707struct 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 +0800708{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800709 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800710
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800711 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800712 if (lst)
713 return lst;
714
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800715 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800716 if (!lst) {
717 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
718 return NULL;
719 }
720
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800721 /* TODO: get the index right */
722 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
723 if (!lst->stats) {
724 talloc_free(lst);
725 return NULL;
726 }
727
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800728 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800729 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800730 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800731 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800732}
733
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800734void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800735{
736 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800737 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800738 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800739}
740
741struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
742{
743 struct bsc_nat_acc_lst_entry *entry;
744
745 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
746 if (!entry)
747 return NULL;
748
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800749 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800750 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800751}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800752
753int bsc_nat_msc_is_connected(struct bsc_nat *nat)
754{
755 return nat->msc_con->is_connected;
756}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800757
758static const int con_to_ctr[] = {
759 [NAT_CON_TYPE_NONE] = -1,
760 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
761 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
762 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800763 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800764 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
765 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
766};
767
768int bsc_conn_type_to_ctr(struct sccp_connections *conn)
769{
770 return con_to_ctr[conn->con_type];
771}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200772
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200773int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200774{
775 int rc;
776
777 rc = write(bfd->fd, msg->data, msg->len);
778 if (rc != msg->len)
779 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
780
781 return rc;
782}
783
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200784static char *rewrite_non_international(struct bsc_nat *nat, void *ctx, const char *imsi,
785 struct gsm_mncc_number *called)
786{
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200787 struct bsc_nat_num_rewr_entry *entry;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200788 char *new_number = NULL;
789
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200790 if (llist_empty(&nat->num_rewr))
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200791 return NULL;
792
793 if (called->plan != 1)
794 return NULL;
795 if (called->type == 1)
796 return NULL;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200797
798 /* need to find a replacement and then fix it */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200799 llist_for_each_entry(entry, &nat->num_rewr, list) {
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200800 regmatch_t matches[2];
801
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200802 /* check the IMSI match */
803 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200804 continue;
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200805
806 /* this regexp matches... */
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200807 if (regexec(&entry->num_reg, called->number, 2, matches, 0) == 0 &&
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200808 matches[1].rm_eo != -1)
809 new_number = talloc_asprintf(ctx, "%s%s",
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200810 entry->replace,
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200811 &called->number[matches[1].rm_so]);
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200812 if (new_number)
813 break;
814 }
815
816 return new_number;
817}
818
819
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200820/**
821 * Rewrite non global numbers... according to rules based on the IMSI
822 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200823static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg,
824 struct bsc_nat_parsed *parsed, const char *imsi,
825 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200826{
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200827 struct tlv_parsed tp;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200828 unsigned int payload_len;
829 struct gsm_mncc_number called;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200830 struct msgb *out;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200831 char *new_number = NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200832 uint8_t *outptr;
833 const uint8_t *msgptr;
834 int sec_len;
835
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200836 /* decode and rewrite the message */
837 payload_len = len - sizeof(*hdr48);
838 tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0);
839
840 /* no number, well let us ignore it */
841 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200842 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200843
844 memset(&called, 0, sizeof(called));
845 gsm48_decode_called(&called,
846 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
847
848 /* check if it looks international and stop */
Holger Hans Peter Freyther20102b02011-05-18 18:41:14 +0200849 new_number = rewrite_non_international(nat, msg, imsi, &called);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200850
851 if (!new_number) {
852 LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n");
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200853 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200854 }
855
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100856 if (strlen(new_number) > sizeof(called.number)) {
857 LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n");
858 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200859 return NULL;
Holger Hans Peter Freyther4446b3b2011-02-10 11:41:49 +0100860 }
861
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200862 /*
863 * Need to create a new message now based on the old onew
864 * with a new number. We can sadly not patch this in place
865 * so we will need to regenerate it.
866 */
867
868 out = msgb_alloc_headroom(4096, 128, "changed-setup");
869 if (!out) {
870 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
871 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200872 return NULL;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200873 }
874
875 /* copy the header */
876 outptr = msgb_put(out, sizeof(*hdr48));
877 memcpy(outptr, hdr48, sizeof(*hdr48));
878
879 /* copy everything up to the number */
880 sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0];
881 outptr = msgb_put(out, sec_len);
882 memcpy(outptr, &hdr48->data[0], sec_len);
883
884 /* create the new number */
Holger Hans Peter Freyther91fa8502011-02-10 11:46:51 +0100885 if (strncmp(new_number, "00", 2) == 0) {
886 called.type = 1;
887 strncpy(called.number, new_number + 2, sizeof(called.number));
888 } else {
889 strncpy(called.number, new_number, sizeof(called.number));
890 }
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200891 gsm48_encode_called(out, &called);
892
893 /* copy thre rest */
894 msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) +
895 TLVP_LEN(&tp, GSM48_IE_CALLED_BCD);
896 sec_len = payload_len - (msgptr - &hdr48->data[0]);
897 outptr = msgb_put(out, sec_len);
898 memcpy(outptr, msgptr, sec_len);
899
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200900 talloc_free(new_number);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +0200901 return out;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200902}
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200903
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +0100904/**
905 * Find a new SMSC address, returns an allocated string that needs to be
906 * freed or is NULL.
907 */
908static char *find_new_smsc(struct bsc_nat *nat, void *ctx, const char *imsi,
909 const char *smsc_addr, const char *dest_nr)
910{
911 struct bsc_nat_num_rewr_entry *entry;
912 char *new_number = NULL;
Holger Hans Peter Freyther55466192012-01-10 14:22:55 +0100913 uint8_t dest_match = llist_empty(&nat->tpdest_match);
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +0100914
915 /* We will find a new number now */
916 llist_for_each_entry(entry, &nat->smsc_rewr, list) {
917 regmatch_t matches[2];
918
919 /* check the IMSI match */
920 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
921 continue;
922
923 /* this regexp matches... */
924 if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 &&
925 matches[1].rm_eo != -1)
926 new_number = talloc_asprintf(ctx, "%s%s",
927 entry->replace,
928 &smsc_addr[matches[1].rm_so]);
929 if (new_number)
930 break;
931 }
932
933 if (!new_number)
934 return NULL;
935
936 /*
937 * now match the number against another list
938 */
939 llist_for_each_entry(entry, &nat->tpdest_match, list) {
940 /* check the IMSI match */
941 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
942 continue;
943
944 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) {
945 dest_match = 1;
946 break;
947 }
948 }
949
950 if (!dest_match) {
951 talloc_free(new_number);
952 return NULL;
953 }
954
955 return new_number;
956}
957
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +0100958/**
959 * Clear the TP-SRR from the TPDU header
960 */
961static uint8_t sms_new_tpdu_hdr(struct bsc_nat *nat, const char *imsi,
962 const char *dest_nr, uint8_t hdr)
963{
964 struct bsc_nat_num_rewr_entry *entry;
965
966 /* We will find a new number now */
967 llist_for_each_entry(entry, &nat->sms_clear_tp_srr, list) {
968 /* check the IMSI match */
969 if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0)
970 continue;
971 if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) != 0)
972 continue;
973
974 /* matched phone number and imsi */
975 return hdr & ~0x20;
976 }
977
978 return hdr;
979}
980
Holger Hans Peter Freyther998a9812012-01-10 13:44:05 +0100981static struct msgb *sms_create_new(uint8_t type, uint8_t ref,
982 struct gsm48_hdr *old_hdr48,
983 const uint8_t *orig_addr_ptr,
984 int orig_addr_len, const char *new_number,
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +0100985 const uint8_t *data_ptr, int data_len,
986 uint8_t tpdu_first_byte)
Holger Hans Peter Freyther998a9812012-01-10 13:44:05 +0100987{
988 uint8_t new_addr_len;
989 struct gsm48_hdr *new_hdr48;
990 uint8_t new_addr[12];
991 struct msgb *out;
992
993 /*
994 * We need to re-create the patched structure. This is why we have
995 * saved the above pointers.
996 */
997 out = msgb_alloc_headroom(4096, 128, "changed-smsc");
998 if (!out) {
999 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1000 return NULL;
1001 }
1002
1003 out->l3h = out->data;
1004 msgb_v_put(out, GSM411_MT_RP_DATA_MO);
1005 msgb_v_put(out, ref);
1006 msgb_lv_put(out, orig_addr_len, orig_addr_ptr);
1007
1008 /*
1009 * Copy the new number. We let libosmocore encode it, then set
1010 * the extension followed after the length. For our convenience
1011 * we let the TLV code re-add the length so we start copying
1012 * from &new_addr[1].
1013 */
1014 new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr),
1015 1, new_number);
1016 new_addr[1] = 0x91;
1017 msgb_lv_put(out, new_addr_len - 1, new_addr + 1);
1018
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001019 /* patch in the new TPDU header value */
1020 msgb_v_put(out, data_len);
1021 msgb_tv_fixed_put(out, tpdu_first_byte, data_len - 1, &data_ptr[1]);
Holger Hans Peter Freyther998a9812012-01-10 13:44:05 +01001022
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001023 /* prepend GSM 04.08 header */
Holger Hans Peter Freyther998a9812012-01-10 13:44:05 +01001024 new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*new_hdr48) + 1);
1025 memcpy(new_hdr48, old_hdr48, sizeof(*old_hdr48));
1026 new_hdr48->data[0] = msgb_l3len(out);
1027
1028 return out;
1029}
1030
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +01001031/**
1032 * Parse the SMS and check if it needs to be rewritten
1033 */
1034static struct msgb *rewrite_sms(struct bsc_nat *nat, struct msgb *msg,
1035 struct bsc_nat_parsed *parsed, const char *imsi,
1036 struct gsm48_hdr *hdr48, const uint32_t len)
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001037{
1038 unsigned int payload_len;
1039 unsigned int cp_len;
1040
1041 uint8_t ref;
1042 uint8_t orig_addr_len, *orig_addr_ptr;
1043 uint8_t dest_addr_len, *dest_addr_ptr;
1044 uint8_t data_len, *data_ptr;
1045 char smsc_addr[30];
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001046
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001047
1048 uint8_t dest_len;
1049 char _dest_nr[30];
1050 char *dest_nr;
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001051
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001052 char *new_number = NULL;
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001053 uint8_t tpdu_hdr;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001054 struct msgb *out;
1055
1056 payload_len = len - sizeof(*hdr48);
1057 if (payload_len < 1) {
1058 LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len);
1059 return NULL;
1060 }
1061
1062 cp_len = hdr48->data[0];
1063 if (payload_len + 1 < cp_len) {
1064 LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len);
1065 return NULL;
1066 }
1067
1068 if (hdr48->data[1] != GSM411_MT_RP_DATA_MO)
1069 return NULL;
1070
1071 if (cp_len < 5) {
1072 LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len);
1073 return NULL;
1074 }
1075
Holger Hans Peter Freytherdec73872012-01-10 14:13:43 +01001076 /* RP */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001077 ref = hdr48->data[2];
1078 orig_addr_len = hdr48->data[3];
1079 orig_addr_ptr = &hdr48->data[4];
1080
1081 /* the +1 is for checking if the following element has some space */
1082 if (cp_len < 3 + orig_addr_len + 1) {
1083 LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len);
1084 return NULL;
1085 }
1086
1087 dest_addr_len = hdr48->data[3 + orig_addr_len + 1];
1088 dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2];
1089
1090 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) {
1091 LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len);
1092 return NULL;
1093 }
1094 gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1);
1095
1096 data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1];
1097 data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2];
1098
1099 if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) {
1100 LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len);
1101 return NULL;
1102 }
1103
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001104 if (data_len < 3) {
1105 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n");
1106 return NULL;
1107 }
1108
Holger Hans Peter Freyther8c36fdb2012-01-10 14:33:27 +01001109 /* TP-PDU starts here */
1110 if ((data_ptr[0] & 0x03) != GSM340_SMS_SUBMIT_MS2SC)
Holger Hans Peter Freyther5d2a30a2012-01-10 13:04:40 +01001111 return NULL;
1112
Holger Hans Peter Freyther8c36fdb2012-01-10 14:33:27 +01001113 /* look into the phone number */
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001114 dest_len = data_ptr[2];
1115 if (data_len < dest_len + 3 || dest_len < 2) {
1116 LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n");
1117 return NULL;
1118 }
1119
1120 if ((data_ptr[3] & 0x80) == 0) {
1121 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n");
1122 return NULL;
1123 }
1124
1125 if ((data_ptr[3] & 0x0F) == 0) {
Holger Hans Peter Freytherdec73872012-01-10 14:13:43 +01001126 LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is of unknown type.\n");
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001127 return NULL;
1128 }
1129
1130 gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2,
1131 &data_ptr[2], 1);
1132 if ((data_ptr[3] & 0x70) == 0x10) {
1133 _dest_nr[0] = _dest_nr[1] = '0';
1134 dest_nr = &_dest_nr[0];
1135 } else {
1136 dest_nr = &_dest_nr[2];
1137 }
1138
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001139 /**
1140 * Call functions to rewrite the data
1141 */
1142 tpdu_hdr = sms_new_tpdu_hdr(nat, imsi, dest_nr, data_ptr[0]);
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +01001143 new_number = find_new_smsc(nat, msg, imsi, smsc_addr, dest_nr);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001144
1145 if (tpdu_hdr == data_ptr[0] && !new_number)
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001146 return NULL;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001147
Holger Hans Peter Freyther998a9812012-01-10 13:44:05 +01001148 out = sms_create_new(GSM411_MT_RP_DATA_MO, ref, hdr48,
1149 orig_addr_ptr, orig_addr_len,
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001150 new_number ? new_number : smsc_addr,
1151 data_ptr, data_len, tpdu_hdr);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001152 talloc_free(new_number);
1153 return out;
1154}
1155
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001156struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi)
1157{
1158 struct gsm48_hdr *hdr48;
1159 uint32_t len;
1160 uint8_t msg_type, proto;
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001161 struct msgb *new_msg = NULL, *sccp;
Holger Hans Peter Freyther951f34b2012-01-10 22:29:35 +01001162 uint8_t link_id;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001163
1164 if (!imsi || strlen(imsi) < 5)
1165 return msg;
1166
1167 /* only care about DTAP messages */
1168 if (parsed->bssap != BSSAP_MSG_DTAP)
1169 return msg;
1170 if (!parsed->dest_local_ref)
1171 return msg;
1172
1173 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
1174 if (!hdr48)
1175 return msg;
1176
Holger Hans Peter Freyther951f34b2012-01-10 22:29:35 +01001177 link_id = msg->l3h[1];
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001178 proto = hdr48->proto_discr & 0x0f;
1179 msg_type = hdr48->msg_type & 0xbf;
1180
1181 if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP)
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001182 new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001183 else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA)
Holger Hans Peter Freytherc5df08b2012-01-10 12:05:52 +01001184 new_msg = rewrite_sms(nat, msg, parsed, imsi, hdr48, len);
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001185
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001186 if (!new_msg)
1187 return msg;
1188
1189 /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */
Holger Hans Peter Freyther951f34b2012-01-10 22:29:35 +01001190 gsm0808_prepend_dtap_header(new_msg, link_id);
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001191 sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len);
1192 talloc_free(new_msg);
1193
1194 if (!sccp) {
1195 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
1196 return msg;
1197 }
1198
1199 ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP);
1200
1201 /* the parsed hangs off from msg but it needs to survive */
1202 talloc_steal(sccp, parsed);
1203 msgb_free(msg);
1204 return sccp;
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001205}
Holger Hans Peter Freyther2e2ff342011-05-27 14:22:58 +02001206
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001207static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry)
1208{
1209 regfree(&entry->msisdn_reg);
1210 regfree(&entry->num_reg);
1211 talloc_free(entry->replace);
1212}
1213
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001214void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head,
1215 const struct osmo_config_list *list)
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001216{
1217 struct bsc_nat_num_rewr_entry *entry, *tmp;
1218 struct osmo_config_entry *cfg_entry;
1219
1220 /* free the old data */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001221 llist_for_each_entry_safe(entry, tmp, head, list) {
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001222 num_rewr_free_data(entry);
1223 llist_del(&entry->list);
1224 talloc_free(entry);
1225 }
1226
1227
1228 if (!list)
1229 return;
1230
1231 llist_for_each_entry(cfg_entry, &list->entry, list) {
1232 char *regexp;
1233 if (cfg_entry->text[0] == '+') {
1234 LOGP(DNAT, LOGL_ERROR,
1235 "Plus is not allowed in the number\n");
1236 continue;
1237 }
1238
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001239 entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001240 if (!entry) {
1241 LOGP(DNAT, LOGL_ERROR,
1242 "Allication of the num_rewr entry failed.\n");
1243 continue;
1244 }
1245
1246 entry->replace = talloc_strdup(entry, cfg_entry->text);
1247 if (!entry->replace) {
1248 LOGP(DNAT, LOGL_ERROR,
1249 "Failed to copy the replacement text.\n");
1250 talloc_free(entry);
1251 continue;
1252 }
1253
1254 /* we will now build a regexp string */
1255 if (cfg_entry->mcc[0] == '^') {
1256 regexp = talloc_strdup(entry, cfg_entry->mcc);
1257 } else {
1258 regexp = talloc_asprintf(entry, "^%s%s",
1259 cfg_entry->mcc[0] == '*' ?
1260 "[0-9][0-9][0-9]" : cfg_entry->mcc,
1261 cfg_entry->mnc[0] == '*' ?
1262 "[0-9][0-9]" : cfg_entry->mnc);
1263 }
1264
1265 if (!regexp) {
1266 LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n");
1267 talloc_free(entry);
1268 continue;
1269 }
1270
1271 if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) {
1272 LOGP(DNAT, LOGL_ERROR,
1273 "Failed to compile regexp '%s'\n", regexp);
1274 talloc_free(regexp);
1275 talloc_free(entry);
1276 continue;
1277 }
1278
1279 talloc_free(regexp);
1280 if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) {
1281 LOGP(DNAT, LOGL_ERROR,
1282 "Failed to compile regexp '%s\n'", cfg_entry->option);
1283 regfree(&entry->msisdn_reg);
1284 talloc_free(entry);
1285 continue;
1286 }
1287
1288 /* we have copied the number */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001289 llist_add_tail(&entry->list, head);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +02001290 }
1291}