blob: 9558e5a574af03048610f303afa1bba422efe99c [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 Freyther6ace5222010-01-12 21:15:08 +010056static struct bsc_fd msc_connection;
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 Freyther058eeb72010-01-31 09:46:21 +0100272static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100273{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800274 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800275 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100276 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100277
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100278 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800279 parsed = bsc_nat_parse(msg);
280 if (!parsed) {
281 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100282 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800283 }
284
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100285 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800286 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800287
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100288 /* Route and modify the SCCP packet */
289 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
290 switch (parsed->sccp_type) {
291 case SCCP_MSG_TYPE_UDT:
292 /* forward UDT messages to every BSC */
293 goto send_to_all;
294 break;
295 case SCCP_MSG_TYPE_RLSD:
296 case SCCP_MSG_TYPE_CREF:
297 case SCCP_MSG_TYPE_DT1:
298 case SCCP_MSG_TYPE_CC:
299 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
300 break;
301 case SCCP_MSG_TYPE_CR:
302 case SCCP_MSG_TYPE_RLC:
303 /* MSC never opens a SCCP connection, fall through */
304 default:
305 goto exit;
306 }
307 }
308
309 talloc_free(parsed);
310 if (!bsc)
311 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100312 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800313 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100314 return -1;
315 }
316
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100317 return write(bsc->bsc_fd.fd, msg->data, msg->len);
318
319send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800320 /*
321 * Filter Paging from the network. We do not want to send a PAGING
322 * Command to every BSC in our network. We will analys the PAGING
323 * message and then send it to the authenticated messages...
324 */
325 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
326 int data_length;
327 const u_int8_t *data;
328 struct tlv_parsed tp;
329 int i = 0;
330
331 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
332 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
333 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
334 goto exit;
335 }
336
337 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
338 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
339 if (data[0] != CELL_IDENT_LAC) {
340 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
341 goto exit;
342 }
343
344 /* go through each LAC and forward the message */
345 for (i = 1; i < data_length - 1; i += 2) {
346 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
347 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
348 if (!bsc->authenticated || _lac != bsc->lac)
349 continue;
350
351 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
352 if (rc < msg->len)
353 LOGP(DNAT, LOGL_ERROR,
354 "Failed to write message to BTS: %d\n", rc);
355 }
356 }
357
358 goto exit;
359 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100360 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800361 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100362 if (!bsc->authenticated)
363 continue;
364
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100365 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
366
367 /* try the next one */
368 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100369 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100370 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800371
372exit:
373 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100374 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100375}
376
377static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
378{
379 int error;
380 struct msgb *msg = ipaccess_read_msg(bfd, &error);
381 struct ipaccess_head *hh;
382
383 if (!msg) {
384 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100385 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100386 exit(-2);
387 }
388
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100389 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100390 return -1;
391 }
392
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100393 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 +0100394
395 /* handle base message handling */
396 hh = (struct ipaccess_head *) msg->data;
397 ipaccess_rcvmsg_base(msg, bfd);
398
399 /* initialize the networking. This includes sending a GSM08.08 message */
400 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
401 initialize_msc_if_needed();
402 else if (hh->proto == IPAC_PROTO_SCCP)
403 forward_sccp_to_bts(msg);
404
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800405 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100406 return 0;
407}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800408
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100409/*
410 * Below is the handling of messages coming
411 * from the BSC and need to be forwarded to
412 * a real BSC.
413 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100414
415/*
416 * Remove the connection from the connections list,
417 * remove it from the patching of SCCP header lists
418 * as well. Maybe in the future even close connection..
419 */
420static void remove_bsc_connection(struct bsc_connection *connection)
421{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100422 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100423 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800424 close(connection->bsc_fd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100425 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100426
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800427 /* stop the timeout timer */
428 bsc_del_timer(&connection->id_timeout);
429
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100430 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800431 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100432 if (sccp_patch->bsc != connection)
433 continue;
434
435 llist_del(&sccp_patch->list_entry);
436 talloc_free(sccp_patch);
437 }
438
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100439 talloc_free(connection);
440}
441
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800442static void ipaccess_close_bsc(void *data)
443{
444 struct bsc_connection *conn = data;
445
446 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
447 remove_bsc_connection(conn);
448}
449
450static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
451{
452 struct bsc_config *conf;
453 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
454
455 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
456 if (strcmp(conf->token, token) == 0) {
457 bsc->authenticated = 1;
458 bsc->lac = conf->lac;
459 bsc_del_timer(&bsc->id_timeout);
460 break;
461 }
462 }
463}
464
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100465static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100466{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100467 struct bsc_connection *bsc;
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100468 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800469 struct bsc_nat_parsed *parsed;
470 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100471
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100472 bsc = bfd->data;
473
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800474 /* Parse and filter messages */
475 parsed = bsc_nat_parse(msg);
476 if (!parsed) {
477 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
478 return -1;
479 }
480
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100481 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800482 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800483
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100484 /* modify the SCCP entries */
485 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
486 switch (parsed->sccp_type) {
487 case SCCP_MSG_TYPE_CR:
488 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
489 goto exit2;
490 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
491 break;
492 case SCCP_MSG_TYPE_RLSD:
493 case SCCP_MSG_TYPE_CREF:
494 case SCCP_MSG_TYPE_DT1:
495 case SCCP_MSG_TYPE_CC:
496 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
497 break;
498 case SCCP_MSG_TYPE_RLC:
499 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
500 remove_sccp_src_ref(bsc, msg, parsed);
501 break;
502 case SCCP_MSG_TYPE_UDT:
503 /* simply forward everything */
504 break;
505 default:
506 goto exit2;
507 break;
508 }
509 }
510
511 if (found_bsc != bsc) {
512 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
513 goto exit2;
514 }
515
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100516 if (!bsc->authenticated) {
517 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
518 goto exit2;
519 }
520
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100521 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800522 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100523 talloc_free(parsed);
524 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800525
526exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100527 /* if we filter out the reset send an ack to the BSC */
528 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
529 send_reset_ack(bfd);
530 send_reset_ack(bfd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800531 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
532 /* do we know who is handling this? */
533 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
534 struct tlv_parsed tvp;
535 ipaccess_idtag_parse(&tvp,
536 (unsigned char *) msg->l2h + 2,
537 msgb_l2len(msg) - 2);
538 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
539 ipaccess_auth_bsc(&tvp, bsc);
540 }
541
542 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100543 }
544
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100545exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800546 talloc_free(parsed);
547 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100548}
549
550static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
551{
552 int error;
553 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100554
555 if (!msg) {
556 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100557 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100558 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100559 } else {
560 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100561 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100562 return -1;
563 }
564
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100565
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100566 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 +0100567
568 /* Handle messages from the BSC */
569 /* FIXME: Currently no PONG is sent to the BSC */
570 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100571 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800572 msgb_free(msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100573
574 return 0;
575}
576
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100577static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
578{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100579 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100580 int ret;
581 struct sockaddr_in sa;
582 socklen_t sa_len = sizeof(sa);
583
584 if (!(what & BSC_FD_READ))
585 return 0;
586
587 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
588 if (ret < 0) {
589 perror("accept");
590 return ret;
591 }
592
593 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100594 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100595
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100596 /*
597 *
598 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800599 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100600 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100601 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100602 close(ret);
603 return -1;
604 }
605
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800606 bsc->nat = nat;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100607 bsc->bsc_fd.data = bsc;
608 bsc->bsc_fd.fd = ret;
609 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100610 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100611 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100612 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100613 close(ret);
614 talloc_free(bsc);
615 return -2;
616 }
617
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100618 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800619 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800620 ipaccess_send_id_ack(bsc->bsc_fd.fd);
621 ipaccess_send_id_req(ret);
622
623 /*
624 * start the hangup timer
625 */
626 bsc->id_timeout.data = bsc;
627 bsc->id_timeout.cb = ipaccess_close_bsc;
628 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100629 return 0;
630}
631
632static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
633{
634 struct sockaddr_in addr;
635 int ret, on = 1;
636
637 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
638 bfd->cb = ipaccess_listen_bsc_cb;
639 bfd->when = BSC_FD_READ;
640
641 memset(&addr, 0, sizeof(addr));
642 addr.sin_family = AF_INET;
643 addr.sin_port = htons(port);
644 addr.sin_addr.s_addr = in_addr->s_addr;
645
646 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
647
648 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
649 if (ret < 0) {
650 fprintf(stderr, "Could not bind the BSC socket %s\n",
651 strerror(errno));
652 return -EIO;
653 }
654
655 ret = listen(bfd->fd, 1);
656 if (ret < 0) {
657 perror("listen");
658 return ret;
659 }
660
661 ret = bsc_register_fd(bfd);
662 if (ret < 0) {
663 perror("register_listen_fd");
664 return ret;
665 }
666 return 0;
667}
668
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800669static void print_usage()
670{
671 printf("Usage: bsc_nat\n");
672}
673
674static void print_help()
675{
676 printf(" Some useful help...\n");
677 printf(" -h --help this text\n");
678 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
679 printf(" -s --disable-color\n");
680 printf(" -c --config-file filename The config file to use.\n");
681 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100682 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800683}
684
685static void handle_options(int argc, char** argv)
686{
687 while (1) {
688 int option_index = 0, c;
689 static struct option long_options[] = {
690 {"help", 0, 0, 'h'},
691 {"debug", 1, 0, 'd'},
692 {"config-file", 1, 0, 'c'},
693 {"disable-color", 0, 0, 's'},
694 {"timestamp", 0, 0, 'T'},
695 {"msc", 1, 0, 'm'},
696 {"local", 1, 0, 'l'},
697 {0, 0, 0, 0}
698 };
699
700 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
701 long_options, &option_index);
702 if (c == -1)
703 break;
704
705 switch (c) {
706 case 'h':
707 print_usage();
708 print_help();
709 exit(0);
710 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800711 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800712 break;
713 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800714 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800715 break;
716 case 'c':
717 config_file = strdup(optarg);
718 break;
719 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800720 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800721 break;
722 case 'm':
723 msc_address = strdup(optarg);
724 break;
725 case 'l':
726 inet_aton(optarg, &local_addr);
727 break;
728 default:
729 /* ignore */
730 break;
731 }
732 }
733}
734
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100735static void signal_handler(int signal)
736{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100737 switch (signal) {
738 case SIGABRT:
739 /* in case of abort, we want to obtain a talloc report
740 * and then return to the caller, who will abort the process */
741 case SIGUSR1:
742 talloc_report_full(tall_bsc_ctx, stderr);
743 break;
744 default:
745 break;
746 }
747}
748
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800749int main(int argc, char** argv)
750{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100751 int rc;
752
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800753 debug_init();
754 stderr_target = debug_target_create_stderr();
755 debug_add_target(stderr_target);
756 debug_set_all_filter(stderr_target, 1);
757
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800758 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100759 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800760 handle_options(argc, argv);
761
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800762 nat = bsc_nat_alloc();
763 if (!nat) {
764 fprintf(stderr, "Failed to allocate the BSC nat.\n");
765 return -4;
766 }
767
768 /* init vty and parse */
769 bsc_nat_vty_init(nat);
770 telnet_init(NULL, 4244);
771 if (vty_read_config_file(config_file) < 0) {
772 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
773 return -3;
774 }
775
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800776 /* seed the PRNG */
777 srand(time(NULL));
778
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100779 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100780 msc_connection.cb = ipaccess_msc_cb;
781 rc = connect_to_msc(&msc_connection, msc_address, 5000);
782 if (rc < 0) {
783 fprintf(stderr, "Opening the MSC connection failed.\n");
784 exit(1);
785 }
786
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100787 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100788 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100789 fprintf(stderr, "Failed to listen for BSC.\n");
790 exit(1);
791 }
792
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100793 signal(SIGABRT, &signal_handler);
794 signal(SIGUSR1, &signal_handler);
795 signal(SIGPIPE, SIG_IGN);
796
797 while (1) {
798 bsc_select_main(0);
799 }
800
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800801 return 0;
802}