blob: bbbc6e2aa7a8c94f48a6f4680e885c048ef88dce [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
5 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <openbsc/bsc_nat.h>
26#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020027#include <openbsc/bssap.h>
28#include <openbsc/debug.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080029
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020030#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080031#include <osmocore/talloc.h>
32
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020033#include <netinet/in.h>
34#include <arpa/inet.h>
35
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080036struct bsc_nat *bsc_nat_alloc(void)
37{
38 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
39 if (!nat)
40 return NULL;
41
42 INIT_LLIST_HEAD(&nat->sccp_connections);
43 INIT_LLIST_HEAD(&nat->bsc_connections);
44 INIT_LLIST_HEAD(&nat->bsc_configs);
45 return nat;
46}
47
48struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
49{
50 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
51 if (!con)
52 return NULL;
53
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020054 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080055 return con;
56}
57
58struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
59{
60 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
61 if (!conf)
62 return NULL;
63
64 conf->token = talloc_strdup(conf, token);
65 conf->lac = lac;
66 conf->nr = nat->num_bsc;
67 conf->nat = nat;
68
69 llist_add(&conf->entry, &nat->bsc_configs);
70 ++nat->num_bsc;
71
72 return conf;
73}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020074
75struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
76{
77 struct bsc_connection *bsc;
78 int data_length;
79 const u_int8_t *data;
80 struct tlv_parsed tp;
81 int i = 0;
82
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +020083 if (!msg->l3h || msgb_l3len(msg) < 3) {
84 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
85 return NULL;
86 }
87
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020088 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
89 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
90 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
91 return NULL;
92 }
93
94 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
95 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
96 if (data[0] != CELL_IDENT_LAC) {
97 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
98 return NULL;
99 }
100
101 /* Currently we only handle one BSC */
102 for (i = 1; i < data_length - 1; i += 2) {
103 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
104 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
105 if (!bsc->authenticated || _lac != bsc->lac)
106 continue;
107
108 return bsc;
109 }
110 }
111
112 return NULL;
113}