blob: 7d618848abdef2f1e6bdabd472d79bb83cc79460 [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>
5 * (C) 2010 by on-waves.com
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/msgb.h>
40#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010042#include <openbsc/bssap.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
45#include <openbsc/talloc.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080046#include <openbsc/telnet_interface.h>
47
48#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 Freyther9a85ef32010-06-15 18:46:11 +080052static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080053static char *msc_address = "127.0.0.1";
54static struct in_addr local_addr;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010055static struct bsc_fd msc_connection;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010056static struct bsc_fd bsc_connection;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010057
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010058
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080059static struct bsc_nat *nat;
60
61static struct bsc_nat *bsc_nat_alloc(void)
62{
63 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
64 if (!nat)
65 return NULL;
66
67 INIT_LLIST_HEAD(&nat->sccp_connections);
68 INIT_LLIST_HEAD(&nat->bsc_connections);
69 INIT_LLIST_HEAD(&nat->bsc_configs);
70 return nat;
71}
72
73static struct bsc_connection *bsc_connection_alloc(void)
74{
75 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
76 if (!con)
77 return NULL;
78
79 return con;
80}
81
82struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
83{
84 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
85 if (!conf)
86 return NULL;
87
88 conf->token = talloc_strdup(conf, token);
89 conf->lac = lac;
90 conf->nr = nat->num_bsc;
91 conf->nat = nat;
92
93 llist_add(&conf->entry, &nat->bsc_configs);
94 ++nat->num_bsc;
95
96 return conf;
97}
98
99struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
100{
101 struct bsc_config *conf;
102
103 llist_for_each_entry(conf, &nat->bsc_configs, entry)
104 if (conf->nr == num)
105 return conf;
106
107 return NULL;
108}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100109
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100110/*
111 * below are stubs we need to link
112 */
113int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
114 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
115{
116 return -1;
117}
118
119void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
120{}
121
122int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
123{
124 return -1;
125}
126
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100127static int send_reset_ack(struct bsc_fd *bfd)
128{
129 static const u_int8_t gsm_reset_ack[] = {
130 0x00, 0x13, 0xfd,
131 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
132 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
133 0x00, 0x01, 0x31,
134 };
135
136 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
137}
138
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100139/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100140 * SCCP patching below
141 */
142
143/* check if we are using this ref for patched already */
144static int sccp_ref_is_free(struct sccp_source_reference *ref)
145{
146 struct sccp_connections *conn;
147
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800148 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100149 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
150 return -1;
151 }
152
153 return 0;
154}
155
156/* copied from sccp.c */
157static int assign_src_local_reference(struct sccp_source_reference *ref)
158{
159 static u_int32_t last_ref = 0x50000;
160 int wrapped = 0;
161
162 do {
163 struct sccp_source_reference reference;
164 reference.octet1 = (last_ref >> 0) & 0xff;
165 reference.octet2 = (last_ref >> 8) & 0xff;
166 reference.octet3 = (last_ref >> 16) & 0xff;
167
168 ++last_ref;
169 /* do not use the reversed word and wrap around */
170 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
171 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
172 last_ref = 0;
173 ++wrapped;
174 }
175
176 if (sccp_ref_is_free(&reference) == 0) {
177 *ref = reference;
178 return 0;
179 }
180 } while (wrapped != 2);
181
182 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
183 return -1;
184}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100185
186static 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 +0100187{
188 struct sccp_connections *conn;
189
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800190 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100191 if (!conn) {
192 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
193 return -1;
194 }
195
196 conn->real_ref = *parsed->src_local_ref;
197 if (assign_src_local_reference(&conn->patched_ref) != 0) {
198 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
199 talloc_free(conn);
200 return -1;
201 }
202
203 return 0;
204}
205
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100206static 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 +0100207{
208 struct sccp_connections *conn;
209
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800210 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100211 if (memcmp(parsed->src_local_ref,
212 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
213 if (bsc != conn->bsc) {
214 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
215 continue;
216 }
217
218
219 llist_del(&conn->list_entry);
220 talloc_free(conn);
221 return;
222 }
223 }
224
225 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
226}
227
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100228static 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 +0100229{
230 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800231 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100232 if (memcmp(parsed->dest_local_ref,
233 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
234 memcpy(parsed->dest_local_ref,
235 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
236 return conn->bsc;
237 }
238 }
239
240 return NULL;
241}
242
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100243static 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 +0100244{
245 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800246 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100247 if (memcmp(parsed->src_local_ref,
248 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
249 memcpy(parsed->src_local_ref,
250 &conn->patched_ref, sizeof(*parsed->src_local_ref));
251 return conn->bsc;
252 }
253 }
254
255 return NULL;
256}
257
258/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100259 * Below is the handling of messages coming
260 * from the MSC and need to be forwarded to
261 * a real BSC.
262 */
263static void initialize_msc_if_needed()
264{
265 static int init = 0;
266 init = 1;
267
268 /* do we need to send a GSM 08.08 message here? */
269}
270
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100271static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100272{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100273 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800274 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100275 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100276
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100277 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800278 parsed = bsc_nat_parse(msg);
279 if (!parsed) {
280 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100281 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800282 }
283
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100284 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800285 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800286
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100287 /* Route and modify the SCCP packet */
288 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
289 switch (parsed->sccp_type) {
290 case SCCP_MSG_TYPE_UDT:
291 /* forward UDT messages to every BSC */
292 goto send_to_all;
293 break;
294 case SCCP_MSG_TYPE_RLSD:
295 case SCCP_MSG_TYPE_CREF:
296 case SCCP_MSG_TYPE_DT1:
297 case SCCP_MSG_TYPE_CC:
298 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
299 break;
300 case SCCP_MSG_TYPE_CR:
301 case SCCP_MSG_TYPE_RLC:
302 /* MSC never opens a SCCP connection, fall through */
303 default:
304 goto exit;
305 }
306 }
307
308 talloc_free(parsed);
309 if (!bsc)
310 return -1;
311 return write(bsc->bsc_fd.fd, msg->data, msg->len);
312
313send_to_all:
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100314 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800315 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100316 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
317
318 /* try the next one */
319 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100320 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100321 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800322
323exit:
324 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100325 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100326}
327
328static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
329{
330 int error;
331 struct msgb *msg = ipaccess_read_msg(bfd, &error);
332 struct ipaccess_head *hh;
333
334 if (!msg) {
335 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100336 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100337 exit(-2);
338 }
339
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100340 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100341 return -1;
342 }
343
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100344 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 +0100345
346 /* handle base message handling */
347 hh = (struct ipaccess_head *) msg->data;
348 ipaccess_rcvmsg_base(msg, bfd);
349
350 /* initialize the networking. This includes sending a GSM08.08 message */
351 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
352 initialize_msc_if_needed();
353 else if (hh->proto == IPAC_PROTO_SCCP)
354 forward_sccp_to_bts(msg);
355
356 return 0;
357}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800358
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100359/*
360 * Below is the handling of messages coming
361 * from the BSC and need to be forwarded to
362 * a real BSC.
363 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100364
365/*
366 * Remove the connection from the connections list,
367 * remove it from the patching of SCCP header lists
368 * as well. Maybe in the future even close connection..
369 */
370static void remove_bsc_connection(struct bsc_connection *connection)
371{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100372 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100373 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100374 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100375
376 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800377 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100378 if (sccp_patch->bsc != connection)
379 continue;
380
381 llist_del(&sccp_patch->list_entry);
382 talloc_free(sccp_patch);
383 }
384
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100385 talloc_free(connection);
386}
387
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100388static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100389{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100390 struct bsc_connection *bsc;
391 struct bsc_connection *found_bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800392 struct bsc_nat_parsed *parsed;
393 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100394
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100395 bsc = bfd->data;
396
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800397 /* Parse and filter messages */
398 parsed = bsc_nat_parse(msg);
399 if (!parsed) {
400 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
401 return -1;
402 }
403
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100404 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800405 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800406
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100407 /* modify the SCCP entries */
408 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
409 switch (parsed->sccp_type) {
410 case SCCP_MSG_TYPE_CR:
411 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
412 goto exit2;
413 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
414 break;
415 case SCCP_MSG_TYPE_RLSD:
416 case SCCP_MSG_TYPE_CREF:
417 case SCCP_MSG_TYPE_DT1:
418 case SCCP_MSG_TYPE_CC:
419 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
420 break;
421 case SCCP_MSG_TYPE_RLC:
422 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
423 remove_sccp_src_ref(bsc, msg, parsed);
424 break;
425 case SCCP_MSG_TYPE_UDT:
426 /* simply forward everything */
427 break;
428 default:
429 goto exit2;
430 break;
431 }
432 }
433
434 if (found_bsc != bsc) {
435 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
436 goto exit2;
437 }
438
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100439 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800440 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100441 talloc_free(parsed);
442 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800443
444exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100445 /* if we filter out the reset send an ack to the BSC */
446 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
447 send_reset_ack(bfd);
448 send_reset_ack(bfd);
449 }
450
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100451exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800452 talloc_free(parsed);
453 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100454}
455
456static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
457{
458 int error;
459 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100460
461 if (!msg) {
462 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100463 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100464 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100465 } else {
466 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100467 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100468 return -1;
469 }
470
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100471
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100472 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 +0100473
474 /* Handle messages from the BSC */
475 /* FIXME: Currently no PONG is sent to the BSC */
476 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100477 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100478
479 return 0;
480}
481
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100482static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
483{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100484 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100485 int ret;
486 struct sockaddr_in sa;
487 socklen_t sa_len = sizeof(sa);
488
489 if (!(what & BSC_FD_READ))
490 return 0;
491
492 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
493 if (ret < 0) {
494 perror("accept");
495 return ret;
496 }
497
498 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100499 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100500
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100501 /*
502 *
503 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800504 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100505 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100506 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100507 close(ret);
508 return -1;
509 }
510
511 bsc->bsc_fd.data = bsc;
512 bsc->bsc_fd.fd = ret;
513 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100514 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100515 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100516 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100517 close(ret);
518 talloc_free(bsc);
519 return -2;
520 }
521
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100522 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800523 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100524 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100525 return 0;
526}
527
528static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
529{
530 struct sockaddr_in addr;
531 int ret, on = 1;
532
533 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
534 bfd->cb = ipaccess_listen_bsc_cb;
535 bfd->when = BSC_FD_READ;
536
537 memset(&addr, 0, sizeof(addr));
538 addr.sin_family = AF_INET;
539 addr.sin_port = htons(port);
540 addr.sin_addr.s_addr = in_addr->s_addr;
541
542 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
543
544 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
545 if (ret < 0) {
546 fprintf(stderr, "Could not bind the BSC socket %s\n",
547 strerror(errno));
548 return -EIO;
549 }
550
551 ret = listen(bfd->fd, 1);
552 if (ret < 0) {
553 perror("listen");
554 return ret;
555 }
556
557 ret = bsc_register_fd(bfd);
558 if (ret < 0) {
559 perror("register_listen_fd");
560 return ret;
561 }
562 return 0;
563}
564
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800565static void print_usage()
566{
567 printf("Usage: bsc_nat\n");
568}
569
570static void print_help()
571{
572 printf(" Some useful help...\n");
573 printf(" -h --help this text\n");
574 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
575 printf(" -s --disable-color\n");
576 printf(" -c --config-file filename The config file to use.\n");
577 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100578 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800579}
580
581static void handle_options(int argc, char** argv)
582{
583 while (1) {
584 int option_index = 0, c;
585 static struct option long_options[] = {
586 {"help", 0, 0, 'h'},
587 {"debug", 1, 0, 'd'},
588 {"config-file", 1, 0, 'c'},
589 {"disable-color", 0, 0, 's'},
590 {"timestamp", 0, 0, 'T'},
591 {"msc", 1, 0, 'm'},
592 {"local", 1, 0, 'l'},
593 {0, 0, 0, 0}
594 };
595
596 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
597 long_options, &option_index);
598 if (c == -1)
599 break;
600
601 switch (c) {
602 case 'h':
603 print_usage();
604 print_help();
605 exit(0);
606 case 's':
607 debug_use_color(0);
608 break;
609 case 'd':
610 debug_parse_category_mask(optarg);
611 break;
612 case 'c':
613 config_file = strdup(optarg);
614 break;
615 case 'T':
616 debug_timestamp(1);
617 break;
618 case 'm':
619 msc_address = strdup(optarg);
620 break;
621 case 'l':
622 inet_aton(optarg, &local_addr);
623 break;
624 default:
625 /* ignore */
626 break;
627 }
628 }
629}
630
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100631static void signal_handler(int signal)
632{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100633 switch (signal) {
634 case SIGABRT:
635 /* in case of abort, we want to obtain a talloc report
636 * and then return to the caller, who will abort the process */
637 case SIGUSR1:
638 talloc_report_full(tall_bsc_ctx, stderr);
639 break;
640 default:
641 break;
642 }
643}
644
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800645int main(int argc, char** argv)
646{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100647 int rc;
648
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800649 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100650 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800651 handle_options(argc, argv);
652
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800653 nat = bsc_nat_alloc();
654 if (!nat) {
655 fprintf(stderr, "Failed to allocate the BSC nat.\n");
656 return -4;
657 }
658
659 /* init vty and parse */
660 bsc_nat_vty_init(nat);
661 telnet_init(NULL, 4244);
662 if (vty_read_config_file(config_file) < 0) {
663 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
664 return -3;
665 }
666
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800667 /* seed the PRNG */
668 srand(time(NULL));
669
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100670 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100671 msc_connection.cb = ipaccess_msc_cb;
672 rc = connect_to_msc(&msc_connection, msc_address, 5000);
673 if (rc < 0) {
674 fprintf(stderr, "Opening the MSC connection failed.\n");
675 exit(1);
676 }
677
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100678 /* wait for the BSC */
679 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
680 fprintf(stderr, "Failed to listen for BSC.\n");
681 exit(1);
682 }
683
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100684 signal(SIGINT, &signal_handler);
685 signal(SIGABRT, &signal_handler);
686 signal(SIGUSR1, &signal_handler);
687 signal(SIGPIPE, SIG_IGN);
688
689 while (1) {
690 bsc_select_main(0);
691 }
692
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800693 return 0;
694}