blob: 93831c4175b90c78f6f12525baa62997f16d94ad [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>
Harald Welte4a88a492014-08-20 23:46:40 +020035#include <osmocom/gsm/ipa.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080036
Harald Welted36ff762011-03-23 18:26:56 +010037#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +020038#include <osmocom/gsm/protocol/gsm_04_11.h>
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080039
Harald Welted5db12c2010-08-03 15:11:51 +020040#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020041
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020042#include <netinet/in.h>
43#include <arpa/inet.h>
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +020044#include <unistd.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020045
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080046static const struct rate_ctr_desc bsc_cfg_ctr_description[] = {
Holger Hans Peter Freyther71d36b32010-06-17 18:31:18 +080047 [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "},
48 [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "},
49 [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "},
50 [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."},
51 [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "},
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080052 [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "},
53 [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "},
54 [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080055 [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "},
56 [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "},
57 [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "},
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +080058 [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "},
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +080059 [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "},
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080060};
61
62static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = {
63 .group_name_prefix = "nat.bsc",
64 .group_description = "NAT BSC Statistics",
65 .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description),
66 .ctr_desc = bsc_cfg_ctr_description,
67};
68
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +080069static const struct rate_ctr_desc acc_list_ctr_description[] = {
70 [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"},
71 [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"},
72};
73
74static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = {
75 .group_name_prefix = "nat.filter",
76 .group_description = "NAT Access-List Statistics",
77 .num_ctr = ARRAY_SIZE(acc_list_ctr_description),
78 .ctr_desc = acc_list_ctr_description,
79};
80
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080081struct bsc_nat *bsc_nat_alloc(void)
82{
83 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
84 if (!nat)
85 return NULL;
86
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020087 nat->main_dest = talloc_zero(nat, struct bsc_msc_dest);
88 if (!nat->main_dest) {
89 talloc_free(nat);
90 return NULL;
91 }
92
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080093 INIT_LLIST_HEAD(&nat->sccp_connections);
94 INIT_LLIST_HEAD(&nat->bsc_connections);
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +020095 INIT_LLIST_HEAD(&nat->paging_groups);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080096 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +080097 INIT_LLIST_HEAD(&nat->access_lists);
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +020098 INIT_LLIST_HEAD(&nat->dests);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +020099 INIT_LLIST_HEAD(&nat->num_rewr);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200100 INIT_LLIST_HEAD(&nat->num_rewr_post);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200101 INIT_LLIST_HEAD(&nat->smsc_rewr);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200102 INIT_LLIST_HEAD(&nat->tpdest_match);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +0100103 INIT_LLIST_HEAD(&nat->sms_clear_tp_srr);
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +0100104 INIT_LLIST_HEAD(&nat->sms_num_rewr);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800105
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200106 nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn");
107 nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls");
108 nat->stats.bsc.reconn = osmo_counter_alloc("nat.bsc.conn");
109 nat->stats.bsc.auth_fail = osmo_counter_alloc("nat.bsc.auth_fail");
110 nat->stats.msc.reconn = osmo_counter_alloc("nat.msc.conn");
111 nat->stats.ussd.reconn = osmo_counter_alloc("nat.ussd.conn");
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800112 nat->auth_timeout = 2;
113 nat->ping_timeout = 20;
114 nat->pong_timeout = 5;
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200115
116 llist_add(&nat->main_dest->list, &nat->dests);
117 nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1");
118 nat->main_dest->port = 5000;
119
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800120 return nat;
121}
122
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800123void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
124{
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +0200125 bsc_replace_string(nat, &nat->main_dest->ip, ip);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800126}
127
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800128struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
129{
130 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
131 if (!con)
132 return NULL;
133
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +0200134 con->nat = nat;
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200135 osmo_wqueue_init(&con->write_queue, 100);
Daniel Willmanna86bc392011-02-18 14:32:56 +0100136 INIT_LLIST_HEAD(&con->cmd_pending);
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200137 INIT_LLIST_HEAD(&con->pending_dlcx);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800138 return con;
139}
140
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800141struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token)
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800142{
143 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
144 if (!conf)
145 return NULL;
146
147 conf->token = talloc_strdup(conf, token);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800148 conf->nr = nat->num_bsc;
149 conf->nat = nat;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100150 conf->max_endpoints = 32;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200151 conf->paging_group = PAGIN_GROUP_UNASSIGNED;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800152
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800153 INIT_LLIST_HEAD(&conf->lac_list);
154
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +0200155 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800156 ++nat->num_bsc;
157
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800158 conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800159 if (!conf->stats.ctrg) {
Holger Hans Peter Freytherdc16bdd2014-08-29 09:49:30 +0200160 llist_del(&conf->entry);
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800161 talloc_free(conf);
162 return NULL;
163 }
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200164
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800165 return conf;
166}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200167
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100168void bsc_config_free(struct bsc_config *cfg)
169{
170 rate_ctr_group_free(cfg->stats.ctrg);
171}
172
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200173static void _add_lac(void *ctx, struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800174{
175 struct bsc_lac_entry *lac;
176
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200177 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800178 if (lac->lac == _lac)
179 return;
180
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200181 lac = talloc_zero(ctx, struct bsc_lac_entry);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800182 if (!lac) {
183 LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n");
184 return;
185 }
186
187 lac->lac = _lac;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200188 llist_add_tail(&lac->entry, list);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800189}
190
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200191static void _del_lac(struct llist_head *list, int _lac)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800192{
193 struct bsc_lac_entry *lac;
194
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200195 llist_for_each_entry(lac, list, entry)
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800196 if (lac->lac == _lac) {
197 llist_del(&lac->entry);
198 talloc_free(lac);
199 return;
200 }
201}
202
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200203void bsc_config_add_lac(struct bsc_config *cfg, int _lac)
204{
205 _add_lac(cfg, &cfg->lac_list, _lac);
206}
207
208void bsc_config_del_lac(struct bsc_config *cfg, int _lac)
209{
210 _del_lac(&cfg->lac_list, _lac);
211}
212
213struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group)
214{
215 struct bsc_nat_paging_group *pgroup;
216
217 pgroup = talloc_zero(nat, struct bsc_nat_paging_group);
218 if (!pgroup) {
219 LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n");
220 return NULL;
221 }
222
223 pgroup->nr = group;
224 INIT_LLIST_HEAD(&pgroup->lists);
225 llist_add_tail(&pgroup->entry, &nat->paging_groups);
226 return pgroup;
227}
228
229void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup)
230{
231 llist_del(&pgroup->entry);
232 talloc_free(pgroup);
233}
234
235struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group)
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200236{
237 struct bsc_nat_paging_group *pgroup;
238
239 llist_for_each_entry(pgroup, &nat->paging_groups, entry)
240 if (pgroup->nr == group)
241 return pgroup;
242
243 return NULL;
244}
245
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200246void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac)
247{
248 _add_lac(pgroup, &pgroup->lists, lac);
249}
250
251void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac)
252{
253 _del_lac(&pgroup->lists, lac);
254}
255
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800256int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr)
257{
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200258 struct bsc_nat_paging_group *pgroup;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800259 struct bsc_lac_entry *entry;
260
261 llist_for_each_entry(entry, &cfg->lac_list, entry)
262 if (entry->lac == lac_nr)
263 return 1;
264
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200265 /* now lookup the paging group */
266 pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group);
267 if (!pgroup)
268 return 0;
269
270 llist_for_each_entry(entry, &pgroup->lists, entry)
271 if (entry->lac == lac_nr)
272 return 1;
273
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800274 return 0;
275}
276
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200277void sccp_connection_destroy(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200278{
279 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
280 sccp_src_ref_to_int(&conn->real_ref),
281 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800282 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200283 llist_del(&conn->list_entry);
284 talloc_free(conn);
285}
286
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200287
288int bsc_nat_find_paging(struct msgb *msg,
289 const uint8_t **out_data, int *out_leng)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200290{
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200291 int data_length;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800292 const uint8_t *data;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200293 struct tlv_parsed tp;
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800294
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200295 if (!msg->l3h || msgb_l3len(msg) < 3) {
296 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200297 return -1;
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200298 }
299
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200300 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
301 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
302 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200303 return -2;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200304 }
305
306 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
307 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800308
309 /* No need to try a different BSS */
310 if (data[0] == CELL_IDENT_BSS) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200311 return -3;
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800312 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200313 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200314 return -4;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200315 }
316
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200317 *out_data = &data[1];
318 *out_leng = data_length - 1;
319 return 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200320}
321
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800322int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200323{
324 struct msgb *msg;
325
326 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200327 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200328 return -1;
329 }
330
331 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
332 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200333 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200334 return -1;
335 }
336
337 /* copy the data */
338 msg->l3h = msgb_put(msg, length);
339 memcpy(msg->l3h, data, length);
340
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100341 return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200342}
343
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200344int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200345{
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200346 return bsc_do_write(&bsc->write_queue, msg, proto);
347}
348
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200349int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto)
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200350{
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200351 /* prepend the header */
Harald Welte4a88a492014-08-20 23:46:40 +0200352 ipa_prepend_header(msg, proto);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200353 return bsc_write_msg(queue, msg);
354}
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200355
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200356int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg)
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200357{
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200358 if (osmo_wqueue_enqueue(queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200359 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200360 msgb_free(msg);
361 return -1;
362 }
363
364 return 0;
365}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800366
Holger Hans Peter Freytherc1cac1e2010-10-11 09:50:31 +0200367int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800368{
369 struct bsc_nat_acc_lst_entry *entry;
370
371 llist_for_each_entry(entry, &lst->fltr_list, list) {
372 if (!entry->imsi_allow)
373 continue;
374 if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
375 return 0;
376 }
377
378 return 1;
379}
380
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800381struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed,
382 struct msgb *msg, uint32_t *len)
383{
384 /* gsm_type is actually the size of the dtap */
385 *len = parsed->gsm_type;
386 if (*len < msgb_l3len(msg) - 3) {
387 LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n");
388 return NULL;
389 }
390
Holger Hans Peter Freytherc2c0ea72012-01-10 22:27:43 +0100391 if (msgb_l3len(msg) - 3 < msg->l3h[2]) {
392 LOGP(DNAT, LOGL_ERROR,
393 "GSM48 payload does not fit: %d %d\n",
394 msg->l3h[2], msgb_l3len(msg) - 3);
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800395 return NULL;
396 }
397
398 msg->l4h = &msg->l3h[3];
399 return (struct gsm48_hdr *) msg->l4h;
400}
401
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800402static const char *con_types [] = {
403 [NAT_CON_TYPE_NONE] = "n/a",
404 [NAT_CON_TYPE_LU] = "Location Update",
405 [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req",
406 [NAT_CON_TYPE_PAG_RESP] = "Paging Response",
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800407 [NAT_CON_TYPE_SSA] = "Supplementar Service Activation",
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800408 [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject",
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800409 [NAT_CON_TYPE_OTHER] = "Other",
410};
411
412const char *bsc_con_type_to_string(int type)
413{
414 return con_types[type];
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800415}
416
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800417struct 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 +0800418{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800419 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800420
421 if (!name)
422 return NULL;
423
424 llist_for_each_entry(lst, &nat->access_lists, list)
425 if (strcmp(lst->name, name) == 0)
426 return lst;
427
428 return NULL;
429}
430
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800431struct 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 +0800432{
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800433 struct bsc_nat_acc_lst *lst;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800434
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800435 lst = bsc_nat_acc_lst_find(nat, name);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800436 if (lst)
437 return lst;
438
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800439 lst = talloc_zero(nat, struct bsc_nat_acc_lst);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800440 if (!lst) {
441 LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list");
442 return NULL;
443 }
444
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800445 /* TODO: get the index right */
446 lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0);
447 if (!lst->stats) {
448 talloc_free(lst);
449 return NULL;
450 }
451
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800452 INIT_LLIST_HEAD(&lst->fltr_list);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800453 lst->name = talloc_strdup(lst, name);
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800454 llist_add_tail(&lst->list, &nat->access_lists);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800455 return lst;
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800456}
457
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800458void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst)
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800459{
460 llist_del(&lst->list);
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800461 rate_ctr_group_free(lst->stats);
Holger Hans Peter Freythere4900a02010-06-03 01:44:05 +0800462 talloc_free(lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800463}
464
465struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst)
466{
467 struct bsc_nat_acc_lst_entry *entry;
468
469 entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry);
470 if (!entry)
471 return NULL;
472
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100473 entry->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
474 entry->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED;
Holger Hans Peter Freyther26c3a352010-06-08 11:00:09 +0800475 llist_add_tail(&entry->list, &lst->fltr_list);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800476 return entry;
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800477}
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800478
479int bsc_nat_msc_is_connected(struct bsc_nat *nat)
480{
481 return nat->msc_con->is_connected;
482}
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800483
484static const int con_to_ctr[] = {
485 [NAT_CON_TYPE_NONE] = -1,
486 [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU,
487 [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ,
488 [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800489 [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800490 [NAT_CON_TYPE_LOCAL_REJECT] = -1,
491 [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER,
492};
493
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200494int bsc_conn_type_to_ctr(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800495{
496 return con_to_ctr[conn->con_type];
497}
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200498
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200499int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200500{
501 int rc;
502
503 rc = write(bfd->fd, msg->data, msg->len);
504 if (rc != msg->len)
505 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
506
507 return rc;
508}
509
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +0200510static void extract_lac(const uint8_t *data, uint16_t *lac, uint16_t *ci)
511{
512 memcpy(lac, &data[0], sizeof(*lac));
513 memcpy(ci, &data[2], sizeof(*ci));
514
515 *lac = ntohs(*lac);
516 *ci = ntohs(*ci);
517}
518
519int bsc_nat_extract_lac(struct bsc_connection *bsc,
520 struct nat_sccp_connection *con,
521 struct bsc_nat_parsed *parsed, struct msgb *msg)
522{
523 int data_length;
524 const uint8_t *data;
525 struct tlv_parsed tp;
526 uint16_t lac, ci;
527
528 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
529 LOGP(DNAT, LOGL_ERROR, "Can only extract LAC from Complete Layer3\n");
530 return -1;
531 }
532
533 if (!msg->l3h || msgb_l3len(msg) < 3) {
534 LOGP(DNAT, LOGL_ERROR, "Complete Layer3 mssage is too short.\n");
535 return -1;
536 }
537
538 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
539 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER)) {
540 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
541 return -2;
542 }
543
544 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER);
545 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER);
546
547 /* Attemt to get the LAC/CI from it */
548 if (data[0] == CELL_IDENT_WHOLE_GLOBAL) {
549 if (data_length != 8) {
550 LOGP(DNAT, LOGL_ERROR,
551 "Ident too short: %d\n", data_length);
552 return -3;
553 }
554 extract_lac(&data[1 + 3], &lac, &ci);
555 } else if (data[0] == CELL_IDENT_LAC_AND_CI) {
556 if (data_length != 5) {
557 LOGP(DNAT, LOGL_ERROR,
558 "Ident too short: %d\n", data_length);
559 return -3;
560 }
561 extract_lac(&data[1], &lac, &ci);
562 } else {
563 LOGP(DNAT, LOGL_ERROR,
564 "Unhandled cell identifier: %d\n", data[0]);
565 return -1;
566 }
567
568 con->lac = lac;
569 con->ci = ci;
570 return 0;
571}