blob: f1bb4b516626bec93d6130dbd5b03963084c549c [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 Freyther23fe7be2010-03-30 10:45:48 +020033#include <sccp/sccp.h>
34
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020035#include <netinet/in.h>
36#include <arpa/inet.h>
37
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080038struct bsc_nat *bsc_nat_alloc(void)
39{
40 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
41 if (!nat)
42 return NULL;
43
44 INIT_LLIST_HEAD(&nat->sccp_connections);
45 INIT_LLIST_HEAD(&nat->bsc_connections);
46 INIT_LLIST_HEAD(&nat->bsc_configs);
47 return nat;
48}
49
50struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
51{
52 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
53 if (!con)
54 return NULL;
55
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020056 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080057 return con;
58}
59
60struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
61{
62 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
63 if (!conf)
64 return NULL;
65
66 conf->token = talloc_strdup(conf, token);
67 conf->lac = lac;
68 conf->nr = nat->num_bsc;
69 conf->nat = nat;
70
71 llist_add(&conf->entry, &nat->bsc_configs);
72 ++nat->num_bsc;
73
74 return conf;
75}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020076
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020077void sccp_connection_destroy(struct sccp_connections *conn)
78{
79 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
80 sccp_src_ref_to_int(&conn->real_ref),
81 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080082 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020083 llist_del(&conn->list_entry);
84 talloc_free(conn);
85}
86
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020087struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
88{
89 struct bsc_connection *bsc;
90 int data_length;
91 const u_int8_t *data;
92 struct tlv_parsed tp;
93 int i = 0;
94
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +020095 if (!msg->l3h || msgb_l3len(msg) < 3) {
96 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
97 return NULL;
98 }
99
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200100 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
101 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
102 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
103 return NULL;
104 }
105
106 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
107 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
108 if (data[0] != CELL_IDENT_LAC) {
109 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
110 return NULL;
111 }
112
113 /* Currently we only handle one BSC */
114 for (i = 1; i < data_length - 1; i += 2) {
115 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
116 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
117 if (!bsc->authenticated || _lac != bsc->lac)
118 continue;
119
120 return bsc;
121 }
122 }
123
124 return NULL;
125}