blob: 3e44193a6e61923bccc342b23394ddf41a4c5612 [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);
82 llist_del(&conn->list_entry);
83 talloc_free(conn);
84}
85
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020086struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
87{
88 struct bsc_connection *bsc;
89 int data_length;
90 const u_int8_t *data;
91 struct tlv_parsed tp;
92 int i = 0;
93
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +020094 if (!msg->l3h || msgb_l3len(msg) < 3) {
95 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
96 return NULL;
97 }
98
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020099 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
100 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
101 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
102 return NULL;
103 }
104
105 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
106 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
107 if (data[0] != CELL_IDENT_LAC) {
108 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
109 return NULL;
110 }
111
112 /* Currently we only handle one BSC */
113 for (i = 1; i < data_length - 1; i += 2) {
114 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
115 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
116 if (!bsc->authenticated || _lac != bsc->lac)
117 continue;
118
119 return bsc;
120 }
121 }
122
123 return NULL;
124}