blob: 8466876bf287d722d0f64c8ee823624d8ce0b912 [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;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100311 if (!bsc->authenticated) {
312 LOGP(DNAT, LOGL_ERRO, "Selected BSC not authenticated.\n");
313 return -1;
314 }
315
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100316 return write(bsc->bsc_fd.fd, msg->data, msg->len);
317
318send_to_all:
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100319 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800320 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100321 if (!bsc->authenticated)
322 continue;
323
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100324 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
325
326 /* try the next one */
327 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100328 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100329 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800330
331exit:
332 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100333 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100334}
335
336static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
337{
338 int error;
339 struct msgb *msg = ipaccess_read_msg(bfd, &error);
340 struct ipaccess_head *hh;
341
342 if (!msg) {
343 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100344 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100345 exit(-2);
346 }
347
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100348 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100349 return -1;
350 }
351
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100352 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 +0100353
354 /* handle base message handling */
355 hh = (struct ipaccess_head *) msg->data;
356 ipaccess_rcvmsg_base(msg, bfd);
357
358 /* initialize the networking. This includes sending a GSM08.08 message */
359 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
360 initialize_msc_if_needed();
361 else if (hh->proto == IPAC_PROTO_SCCP)
362 forward_sccp_to_bts(msg);
363
364 return 0;
365}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800366
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100367/*
368 * Below is the handling of messages coming
369 * from the BSC and need to be forwarded to
370 * a real BSC.
371 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100372
373/*
374 * Remove the connection from the connections list,
375 * remove it from the patching of SCCP header lists
376 * as well. Maybe in the future even close connection..
377 */
378static void remove_bsc_connection(struct bsc_connection *connection)
379{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100380 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100381 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800382 close(connection->bsc_fd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100383 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100384
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800385 /* stop the timeout timer */
386 bsc_del_timer(&connection->id_timeout);
387
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100388 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800389 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100390 if (sccp_patch->bsc != connection)
391 continue;
392
393 llist_del(&sccp_patch->list_entry);
394 talloc_free(sccp_patch);
395 }
396
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100397 talloc_free(connection);
398}
399
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800400static void ipaccess_close_bsc(void *data)
401{
402 struct bsc_connection *conn = data;
403
404 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
405 remove_bsc_connection(conn);
406}
407
408static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
409{
410 struct bsc_config *conf;
411 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
412
413 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
414 if (strcmp(conf->token, token) == 0) {
415 bsc->authenticated = 1;
416 bsc->lac = conf->lac;
417 bsc_del_timer(&bsc->id_timeout);
418 break;
419 }
420 }
421}
422
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100423static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100424{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100425 struct bsc_connection *bsc;
426 struct bsc_connection *found_bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800427 struct bsc_nat_parsed *parsed;
428 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100429
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100430 bsc = bfd->data;
431
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800432 /* Parse and filter messages */
433 parsed = bsc_nat_parse(msg);
434 if (!parsed) {
435 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
436 return -1;
437 }
438
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100439 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800440 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800441
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100442 /* modify the SCCP entries */
443 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
444 switch (parsed->sccp_type) {
445 case SCCP_MSG_TYPE_CR:
446 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
447 goto exit2;
448 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
449 break;
450 case SCCP_MSG_TYPE_RLSD:
451 case SCCP_MSG_TYPE_CREF:
452 case SCCP_MSG_TYPE_DT1:
453 case SCCP_MSG_TYPE_CC:
454 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
455 break;
456 case SCCP_MSG_TYPE_RLC:
457 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
458 remove_sccp_src_ref(bsc, msg, parsed);
459 break;
460 case SCCP_MSG_TYPE_UDT:
461 /* simply forward everything */
462 break;
463 default:
464 goto exit2;
465 break;
466 }
467 }
468
469 if (found_bsc != bsc) {
470 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
471 goto exit2;
472 }
473
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100474 if (!bsc->authenticated) {
475 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
476 goto exit2;
477 }
478
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100479 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800480 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100481 talloc_free(parsed);
482 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800483
484exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100485 /* if we filter out the reset send an ack to the BSC */
486 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
487 send_reset_ack(bfd);
488 send_reset_ack(bfd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800489 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
490 /* do we know who is handling this? */
491 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
492 struct tlv_parsed tvp;
493 ipaccess_idtag_parse(&tvp,
494 (unsigned char *) msg->l2h + 2,
495 msgb_l2len(msg) - 2);
496 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
497 ipaccess_auth_bsc(&tvp, bsc);
498 }
499
500 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100501 }
502
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100503exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800504 talloc_free(parsed);
505 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100506}
507
508static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
509{
510 int error;
511 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100512
513 if (!msg) {
514 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100515 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100516 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100517 } else {
518 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100519 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100520 return -1;
521 }
522
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100523
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100524 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 +0100525
526 /* Handle messages from the BSC */
527 /* FIXME: Currently no PONG is sent to the BSC */
528 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100529 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100530
531 return 0;
532}
533
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100534static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
535{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100536 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100537 int ret;
538 struct sockaddr_in sa;
539 socklen_t sa_len = sizeof(sa);
540
541 if (!(what & BSC_FD_READ))
542 return 0;
543
544 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
545 if (ret < 0) {
546 perror("accept");
547 return ret;
548 }
549
550 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100551 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100552
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100553 /*
554 *
555 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800556 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100557 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100558 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100559 close(ret);
560 return -1;
561 }
562
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800563 bsc->nat = nat;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100564 bsc->bsc_fd.data = bsc;
565 bsc->bsc_fd.fd = ret;
566 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100567 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100568 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100569 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100570 close(ret);
571 talloc_free(bsc);
572 return -2;
573 }
574
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100575 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800576 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800577 ipaccess_send_id_ack(bsc->bsc_fd.fd);
578 ipaccess_send_id_req(ret);
579
580 /*
581 * start the hangup timer
582 */
583 bsc->id_timeout.data = bsc;
584 bsc->id_timeout.cb = ipaccess_close_bsc;
585 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100586 return 0;
587}
588
589static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
590{
591 struct sockaddr_in addr;
592 int ret, on = 1;
593
594 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
595 bfd->cb = ipaccess_listen_bsc_cb;
596 bfd->when = BSC_FD_READ;
597
598 memset(&addr, 0, sizeof(addr));
599 addr.sin_family = AF_INET;
600 addr.sin_port = htons(port);
601 addr.sin_addr.s_addr = in_addr->s_addr;
602
603 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
604
605 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
606 if (ret < 0) {
607 fprintf(stderr, "Could not bind the BSC socket %s\n",
608 strerror(errno));
609 return -EIO;
610 }
611
612 ret = listen(bfd->fd, 1);
613 if (ret < 0) {
614 perror("listen");
615 return ret;
616 }
617
618 ret = bsc_register_fd(bfd);
619 if (ret < 0) {
620 perror("register_listen_fd");
621 return ret;
622 }
623 return 0;
624}
625
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800626static void print_usage()
627{
628 printf("Usage: bsc_nat\n");
629}
630
631static void print_help()
632{
633 printf(" Some useful help...\n");
634 printf(" -h --help this text\n");
635 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
636 printf(" -s --disable-color\n");
637 printf(" -c --config-file filename The config file to use.\n");
638 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100639 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800640}
641
642static void handle_options(int argc, char** argv)
643{
644 while (1) {
645 int option_index = 0, c;
646 static struct option long_options[] = {
647 {"help", 0, 0, 'h'},
648 {"debug", 1, 0, 'd'},
649 {"config-file", 1, 0, 'c'},
650 {"disable-color", 0, 0, 's'},
651 {"timestamp", 0, 0, 'T'},
652 {"msc", 1, 0, 'm'},
653 {"local", 1, 0, 'l'},
654 {0, 0, 0, 0}
655 };
656
657 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
658 long_options, &option_index);
659 if (c == -1)
660 break;
661
662 switch (c) {
663 case 'h':
664 print_usage();
665 print_help();
666 exit(0);
667 case 's':
668 debug_use_color(0);
669 break;
670 case 'd':
671 debug_parse_category_mask(optarg);
672 break;
673 case 'c':
674 config_file = strdup(optarg);
675 break;
676 case 'T':
677 debug_timestamp(1);
678 break;
679 case 'm':
680 msc_address = strdup(optarg);
681 break;
682 case 'l':
683 inet_aton(optarg, &local_addr);
684 break;
685 default:
686 /* ignore */
687 break;
688 }
689 }
690}
691
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100692static void signal_handler(int signal)
693{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100694 switch (signal) {
695 case SIGABRT:
696 /* in case of abort, we want to obtain a talloc report
697 * and then return to the caller, who will abort the process */
698 case SIGUSR1:
699 talloc_report_full(tall_bsc_ctx, stderr);
700 break;
701 default:
702 break;
703 }
704}
705
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800706int main(int argc, char** argv)
707{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100708 int rc;
709
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800710 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100711 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800712 handle_options(argc, argv);
713
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800714 nat = bsc_nat_alloc();
715 if (!nat) {
716 fprintf(stderr, "Failed to allocate the BSC nat.\n");
717 return -4;
718 }
719
720 /* init vty and parse */
721 bsc_nat_vty_init(nat);
722 telnet_init(NULL, 4244);
723 if (vty_read_config_file(config_file) < 0) {
724 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
725 return -3;
726 }
727
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800728 /* seed the PRNG */
729 srand(time(NULL));
730
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100731 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100732 msc_connection.cb = ipaccess_msc_cb;
733 rc = connect_to_msc(&msc_connection, msc_address, 5000);
734 if (rc < 0) {
735 fprintf(stderr, "Opening the MSC connection failed.\n");
736 exit(1);
737 }
738
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100739 /* wait for the BSC */
740 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
741 fprintf(stderr, "Failed to listen for BSC.\n");
742 exit(1);
743 }
744
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100745 signal(SIGABRT, &signal_handler);
746 signal(SIGUSR1, &signal_handler);
747 signal(SIGPIPE, SIG_IGN);
748
749 while (1) {
750 bsc_select_main(0);
751 }
752
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800753 return 0;
754}