blob: 4694788e42dc750e17b90a50f9a5ccb293c94a0c [file] [log] [blame]
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08007 * 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#include <sys/socket.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freyther5aa25ae2010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080033#include <unistd.h>
34
35#define _GNU_SOURCE
36#include <getopt.h>
37
38#include <openbsc/debug.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010039#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010041#include <openbsc/bssap.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080044#include <openbsc/telnet_interface.h>
45
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080046#include <osmocore/talloc.h>
47
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080048#include <vty/vty.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080049
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080050#include <sccp/sccp.h>
51
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080052struct debug_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080053static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080054static char *msc_address = "127.0.0.1";
55static struct in_addr local_addr;
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +080056static struct write_queue msc_queue;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010057static struct bsc_fd bsc_listen;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010058
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010059
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080060static struct bsc_nat *nat;
61
62static struct bsc_nat *bsc_nat_alloc(void)
63{
64 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
65 if (!nat)
66 return NULL;
67
68 INIT_LLIST_HEAD(&nat->sccp_connections);
69 INIT_LLIST_HEAD(&nat->bsc_connections);
70 INIT_LLIST_HEAD(&nat->bsc_configs);
71 return nat;
72}
73
74static struct bsc_connection *bsc_connection_alloc(void)
75{
76 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
77 if (!con)
78 return NULL;
79
80 return con;
81}
82
83struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
84{
85 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
86 if (!conf)
87 return NULL;
88
89 conf->token = talloc_strdup(conf, token);
90 conf->lac = lac;
91 conf->nr = nat->num_bsc;
92 conf->nat = nat;
93
94 llist_add(&conf->entry, &nat->bsc_configs);
95 ++nat->num_bsc;
96
97 return conf;
98}
99
100struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
101{
102 struct bsc_config *conf;
103
104 llist_for_each_entry(conf, &nat->bsc_configs, entry)
105 if (conf->nr == num)
106 return conf;
107
108 return NULL;
109}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100110
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100111/*
112 * below are stubs we need to link
113 */
114int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
115 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
116{
117 return -1;
118}
119
120void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
121{}
122
123int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
124{
125 return -1;
126}
127
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100128static int send_reset_ack(struct bsc_fd *bfd)
129{
130 static const u_int8_t gsm_reset_ack[] = {
131 0x00, 0x13, 0xfd,
132 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
133 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
134 0x00, 0x01, 0x31,
135 };
136
137 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
138}
139
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100140/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100141 * SCCP patching below
142 */
143
144/* check if we are using this ref for patched already */
145static int sccp_ref_is_free(struct sccp_source_reference *ref)
146{
147 struct sccp_connections *conn;
148
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800149 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100150 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
151 return -1;
152 }
153
154 return 0;
155}
156
157/* copied from sccp.c */
158static int assign_src_local_reference(struct sccp_source_reference *ref)
159{
160 static u_int32_t last_ref = 0x50000;
161 int wrapped = 0;
162
163 do {
164 struct sccp_source_reference reference;
165 reference.octet1 = (last_ref >> 0) & 0xff;
166 reference.octet2 = (last_ref >> 8) & 0xff;
167 reference.octet3 = (last_ref >> 16) & 0xff;
168
169 ++last_ref;
170 /* do not use the reversed word and wrap around */
171 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
172 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
173 last_ref = 0;
174 ++wrapped;
175 }
176
177 if (sccp_ref_is_free(&reference) == 0) {
178 *ref = reference;
179 return 0;
180 }
181 } while (wrapped != 2);
182
183 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
184 return -1;
185}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100186
187static int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100188{
189 struct sccp_connections *conn;
190
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800191 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100192 if (!conn) {
193 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
194 return -1;
195 }
196
197 conn->real_ref = *parsed->src_local_ref;
198 if (assign_src_local_reference(&conn->patched_ref) != 0) {
199 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
200 talloc_free(conn);
201 return -1;
202 }
203
204 return 0;
205}
206
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100207static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100208{
209 struct sccp_connections *conn;
210
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800211 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100212 if (memcmp(parsed->src_local_ref,
213 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
214 if (bsc != conn->bsc) {
215 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
216 continue;
217 }
218
219
220 llist_del(&conn->list_entry);
221 talloc_free(conn);
222 return;
223 }
224 }
225
226 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
227}
228
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100229static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100230{
231 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800232 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100233 if (memcmp(parsed->dest_local_ref,
234 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
235 memcpy(parsed->dest_local_ref,
236 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
237 return conn->bsc;
238 }
239 }
240
241 return NULL;
242}
243
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100244static struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100245{
246 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800247 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100248 if (memcmp(parsed->src_local_ref,
249 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
250 memcpy(parsed->src_local_ref,
251 &conn->patched_ref, sizeof(*parsed->src_local_ref));
252 return conn->bsc;
253 }
254 }
255
256 return NULL;
257}
258
259/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100260 * Below is the handling of messages coming
261 * from the MSC and need to be forwarded to
262 * a real BSC.
263 */
264static void initialize_msc_if_needed()
265{
266 static int init = 0;
267 init = 1;
268
269 /* do we need to send a GSM 08.08 message here? */
270}
271
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100272static int bsc_write(struct bsc_connection *bsc, u_int8_t *data, unsigned int length)
273{
274 return write(bsc->write_queue.bfd.fd, data, length);
275}
276
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100277static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100278{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800279 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800280 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100281 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100282
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100283 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800284 parsed = bsc_nat_parse(msg);
285 if (!parsed) {
286 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100287 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800288 }
289
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100290 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800291 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800292
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100293 /* Route and modify the SCCP packet */
294 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
295 switch (parsed->sccp_type) {
296 case SCCP_MSG_TYPE_UDT:
297 /* forward UDT messages to every BSC */
298 goto send_to_all;
299 break;
300 case SCCP_MSG_TYPE_RLSD:
301 case SCCP_MSG_TYPE_CREF:
302 case SCCP_MSG_TYPE_DT1:
303 case SCCP_MSG_TYPE_CC:
304 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
305 break;
306 case SCCP_MSG_TYPE_CR:
307 case SCCP_MSG_TYPE_RLC:
308 /* MSC never opens a SCCP connection, fall through */
309 default:
310 goto exit;
311 }
312 }
313
314 talloc_free(parsed);
315 if (!bsc)
316 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100317 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800318 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100319 return -1;
320 }
321
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100322 return bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100323
324send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800325 /*
326 * Filter Paging from the network. We do not want to send a PAGING
327 * Command to every BSC in our network. We will analys the PAGING
328 * message and then send it to the authenticated messages...
329 */
330 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
331 int data_length;
332 const u_int8_t *data;
333 struct tlv_parsed tp;
334 int i = 0;
335
336 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
337 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
338 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
339 goto exit;
340 }
341
342 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
343 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
344 if (data[0] != CELL_IDENT_LAC) {
345 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
346 goto exit;
347 }
348
349 /* go through each LAC and forward the message */
350 for (i = 1; i < data_length - 1; i += 2) {
351 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
352 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
353 if (!bsc->authenticated || _lac != bsc->lac)
354 continue;
355
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100356 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800357 if (rc < msg->len)
358 LOGP(DNAT, LOGL_ERROR,
359 "Failed to write message to BTS: %d\n", rc);
360 }
361 }
362
363 goto exit;
364 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100365 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800366 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100367 if (!bsc->authenticated)
368 continue;
369
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100370 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100371
372 /* try the next one */
373 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100374 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100375 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800376
377exit:
378 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100379 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100380}
381
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800382static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100383{
384 int error;
385 struct msgb *msg = ipaccess_read_msg(bfd, &error);
386 struct ipaccess_head *hh;
387
388 if (!msg) {
389 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100390 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100391 exit(-2);
392 }
393
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100394 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100395 return -1;
396 }
397
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100398 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100399
400 /* handle base message handling */
401 hh = (struct ipaccess_head *) msg->data;
402 ipaccess_rcvmsg_base(msg, bfd);
403
404 /* initialize the networking. This includes sending a GSM08.08 message */
405 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
406 initialize_msc_if_needed();
407 else if (hh->proto == IPAC_PROTO_SCCP)
408 forward_sccp_to_bts(msg);
409
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800410 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100411 return 0;
412}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800413
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800414static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
415{
416 int rc;
417 rc = write(bfd->fd, msg->data, msg->len);
418
419 if (rc != msg->len) {
420 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
421 return -1;
422 }
423
424 return rc;
425}
426
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100427/*
428 * Below is the handling of messages coming
429 * from the BSC and need to be forwarded to
430 * a real BSC.
431 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100432
433/*
434 * Remove the connection from the connections list,
435 * remove it from the patching of SCCP header lists
436 * as well. Maybe in the future even close connection..
437 */
438static void remove_bsc_connection(struct bsc_connection *connection)
439{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100440 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800441 bsc_unregister_fd(&connection->write_queue.bfd);
442 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100443 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100444
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800445 /* stop the timeout timer */
446 bsc_del_timer(&connection->id_timeout);
447
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100448 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800449 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100450 if (sccp_patch->bsc != connection)
451 continue;
452
453 llist_del(&sccp_patch->list_entry);
454 talloc_free(sccp_patch);
455 }
456
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100457 talloc_free(connection);
458}
459
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800460static void ipaccess_close_bsc(void *data)
461{
462 struct bsc_connection *conn = data;
463
464 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
465 remove_bsc_connection(conn);
466}
467
468static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
469{
470 struct bsc_config *conf;
471 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
472
473 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
474 if (strcmp(conf->token, token) == 0) {
475 bsc->authenticated = 1;
476 bsc->lac = conf->lac;
477 bsc_del_timer(&bsc->id_timeout);
478 break;
479 }
480 }
481}
482
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100483static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100484{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100485 struct bsc_connection *bsc;
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100486 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800487 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100488
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100489 bsc = bfd->data;
490
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800491 /* Parse and filter messages */
492 parsed = bsc_nat_parse(msg);
493 if (!parsed) {
494 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
495 return -1;
496 }
497
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100498 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800499 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800500
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100501 /* modify the SCCP entries */
502 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
503 switch (parsed->sccp_type) {
504 case SCCP_MSG_TYPE_CR:
505 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
506 goto exit2;
507 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
508 break;
509 case SCCP_MSG_TYPE_RLSD:
510 case SCCP_MSG_TYPE_CREF:
511 case SCCP_MSG_TYPE_DT1:
512 case SCCP_MSG_TYPE_CC:
513 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
514 break;
515 case SCCP_MSG_TYPE_RLC:
516 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
517 remove_sccp_src_ref(bsc, msg, parsed);
518 break;
519 case SCCP_MSG_TYPE_UDT:
520 /* simply forward everything */
521 break;
522 default:
523 goto exit2;
524 break;
525 }
526 }
527
528 if (found_bsc != bsc) {
529 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
530 goto exit2;
531 }
532
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100533 if (!bsc->authenticated) {
534 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
535 goto exit2;
536 }
537
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100538 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800539 if (write_queue_enqueue(&msc_queue, msg) != 0) {
540 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
541 msgb_free(msg);
542 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100543 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800544 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800545
546exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100547 /* if we filter out the reset send an ack to the BSC */
548 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
549 send_reset_ack(bfd);
550 send_reset_ack(bfd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800551 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
552 /* do we know who is handling this? */
553 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
554 struct tlv_parsed tvp;
555 ipaccess_idtag_parse(&tvp,
556 (unsigned char *) msg->l2h + 2,
557 msgb_l2len(msg) - 2);
558 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
559 ipaccess_auth_bsc(&tvp, bsc);
560 }
561
562 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100563 }
564
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100565exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800566 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800567 msgb_free(msg);
568 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100569}
570
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800571static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100572{
573 int error;
574 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100575
576 if (!msg) {
577 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100578 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100579 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100580 } else {
581 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100582 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100583 return -1;
584 }
585
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100586
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100587 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100588
589 /* Handle messages from the BSC */
590 /* FIXME: Currently no PONG is sent to the BSC */
591 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100592 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100593
594 return 0;
595}
596
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100597static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
598{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100599 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100600 int ret;
601 struct sockaddr_in sa;
602 socklen_t sa_len = sizeof(sa);
603
604 if (!(what & BSC_FD_READ))
605 return 0;
606
607 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
608 if (ret < 0) {
609 perror("accept");
610 return ret;
611 }
612
613 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100614 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100615
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100616 /*
617 *
618 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800619 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100620 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100621 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100622 close(ret);
623 return -1;
624 }
625
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800626 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800627 write_queue_init(&bsc->write_queue, 100);
628 bsc->write_queue.bfd.data = bsc;
629 bsc->write_queue.bfd.fd = ret;
630 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
631 bsc->write_queue.bfd.when = BSC_FD_READ;
632 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100633 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100634 close(ret);
635 talloc_free(bsc);
636 return -2;
637 }
638
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100639 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800640 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800641 ipaccess_send_id_ack(bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800642 ipaccess_send_id_req(ret);
643
644 /*
645 * start the hangup timer
646 */
647 bsc->id_timeout.data = bsc;
648 bsc->id_timeout.cb = ipaccess_close_bsc;
649 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100650 return 0;
651}
652
653static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
654{
655 struct sockaddr_in addr;
656 int ret, on = 1;
657
658 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
659 bfd->cb = ipaccess_listen_bsc_cb;
660 bfd->when = BSC_FD_READ;
661
662 memset(&addr, 0, sizeof(addr));
663 addr.sin_family = AF_INET;
664 addr.sin_port = htons(port);
665 addr.sin_addr.s_addr = in_addr->s_addr;
666
667 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
668
669 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
670 if (ret < 0) {
671 fprintf(stderr, "Could not bind the BSC socket %s\n",
672 strerror(errno));
673 return -EIO;
674 }
675
676 ret = listen(bfd->fd, 1);
677 if (ret < 0) {
678 perror("listen");
679 return ret;
680 }
681
682 ret = bsc_register_fd(bfd);
683 if (ret < 0) {
684 perror("register_listen_fd");
685 return ret;
686 }
687 return 0;
688}
689
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800690static void print_usage()
691{
692 printf("Usage: bsc_nat\n");
693}
694
695static void print_help()
696{
697 printf(" Some useful help...\n");
698 printf(" -h --help this text\n");
699 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
700 printf(" -s --disable-color\n");
701 printf(" -c --config-file filename The config file to use.\n");
702 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100703 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800704}
705
706static void handle_options(int argc, char** argv)
707{
708 while (1) {
709 int option_index = 0, c;
710 static struct option long_options[] = {
711 {"help", 0, 0, 'h'},
712 {"debug", 1, 0, 'd'},
713 {"config-file", 1, 0, 'c'},
714 {"disable-color", 0, 0, 's'},
715 {"timestamp", 0, 0, 'T'},
716 {"msc", 1, 0, 'm'},
717 {"local", 1, 0, 'l'},
718 {0, 0, 0, 0}
719 };
720
721 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
722 long_options, &option_index);
723 if (c == -1)
724 break;
725
726 switch (c) {
727 case 'h':
728 print_usage();
729 print_help();
730 exit(0);
731 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800732 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800733 break;
734 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800735 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800736 break;
737 case 'c':
738 config_file = strdup(optarg);
739 break;
740 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800741 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800742 break;
743 case 'm':
744 msc_address = strdup(optarg);
745 break;
746 case 'l':
747 inet_aton(optarg, &local_addr);
748 break;
749 default:
750 /* ignore */
751 break;
752 }
753 }
754}
755
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100756static void signal_handler(int signal)
757{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100758 switch (signal) {
759 case SIGABRT:
760 /* in case of abort, we want to obtain a talloc report
761 * and then return to the caller, who will abort the process */
762 case SIGUSR1:
763 talloc_report_full(tall_bsc_ctx, stderr);
764 break;
765 default:
766 break;
767 }
768}
769
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800770int main(int argc, char** argv)
771{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100772 int rc;
773
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800774 debug_init();
775 stderr_target = debug_target_create_stderr();
776 debug_add_target(stderr_target);
777 debug_set_all_filter(stderr_target, 1);
778
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800779 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100780 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800781 handle_options(argc, argv);
782
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800783 nat = bsc_nat_alloc();
784 if (!nat) {
785 fprintf(stderr, "Failed to allocate the BSC nat.\n");
786 return -4;
787 }
788
789 /* init vty and parse */
790 bsc_nat_vty_init(nat);
791 telnet_init(NULL, 4244);
792 if (vty_read_config_file(config_file) < 0) {
793 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
794 return -3;
795 }
796
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800797 /* seed the PRNG */
798 srand(time(NULL));
799
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100800 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800801 write_queue_init(&msc_queue, 100);
802 msc_queue.read_cb = ipaccess_msc_read_cb;
803 msc_queue.write_cb = ipaccess_msc_write_cb;
804 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100805 if (rc < 0) {
806 fprintf(stderr, "Opening the MSC connection failed.\n");
807 exit(1);
808 }
809
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100810 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100811 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100812 fprintf(stderr, "Failed to listen for BSC.\n");
813 exit(1);
814 }
815
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100816 signal(SIGABRT, &signal_handler);
817 signal(SIGUSR1, &signal_handler);
818 signal(SIGPIPE, SIG_IGN);
819
820 while (1) {
821 bsc_select_main(0);
822 }
823
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800824 return 0;
825}