blob: 7effe484bdfe61c88c9c3feb1f5c66bec16e7423 [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 Freythera0df82d2010-04-01 08:21:33 +020029#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080030
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020031#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032#include <osmocore/talloc.h>
33
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020034#include <sccp/sccp.h>
35
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020036#include <netinet/in.h>
37#include <arpa/inet.h>
38
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080039struct bsc_nat *bsc_nat_alloc(void)
40{
41 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
42 if (!nat)
43 return NULL;
44
45 INIT_LLIST_HEAD(&nat->sccp_connections);
46 INIT_LLIST_HEAD(&nat->bsc_connections);
47 INIT_LLIST_HEAD(&nat->bsc_configs);
48 return nat;
49}
50
51struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
52{
53 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
54 if (!con)
55 return NULL;
56
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020057 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080058 return con;
59}
60
61struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
62{
63 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
64 if (!conf)
65 return NULL;
66
67 conf->token = talloc_strdup(conf, token);
68 conf->lac = lac;
69 conf->nr = nat->num_bsc;
70 conf->nat = nat;
71
72 llist_add(&conf->entry, &nat->bsc_configs);
73 ++nat->num_bsc;
74
75 return conf;
76}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020077
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020078void sccp_connection_destroy(struct sccp_connections *conn)
79{
80 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
81 sccp_src_ref_to_int(&conn->real_ref),
82 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080083 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020084 llist_del(&conn->list_entry);
85 talloc_free(conn);
86}
87
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020088struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
89{
90 struct bsc_connection *bsc;
91 int data_length;
92 const u_int8_t *data;
93 struct tlv_parsed tp;
94 int i = 0;
95
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +020096 if (!msg->l3h || msgb_l3len(msg) < 3) {
97 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
98 return NULL;
99 }
100
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200101 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
102 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
103 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
104 return NULL;
105 }
106
107 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
108 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
109 if (data[0] != CELL_IDENT_LAC) {
110 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
111 return NULL;
112 }
113
114 /* Currently we only handle one BSC */
115 for (i = 1; i < data_length - 1; i += 2) {
116 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
117 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
118 if (!bsc->authenticated || _lac != bsc->lac)
119 continue;
120
121 return bsc;
122 }
123 }
124
125 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200126}
127
128int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
129{
130 struct msgb *msg;
131
132 if (length > 4096 - 128) {
133 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
134 return -1;
135 }
136
137 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
138 if (!msg) {
139 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
140 return -1;
141 }
142
143 /* copy the data */
144 msg->l3h = msgb_put(msg, length);
145 memcpy(msg->l3h, data, length);
146
147 return bsc_write_mgcp_msg(bsc, msg);
148}
149
150int bsc_write_mgcp_msg(struct bsc_connection *bsc, struct msgb *msg)
151{
152 /* prepend the header */
153 ipaccess_prepend_header(msg, NAT_IPAC_PROTO_MGCP);
154
155 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
156 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
157 msgb_free(msg);
158 return -1;
159 }
160
161 return 0;
162}