blob: e4db47f0b2a3710d11f016401f77ab31f824dcc5 [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 Freyther45d11812010-06-15 18:46:36 +0800273 struct bsc_connection *bsc = NULL;
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 Freyther45d11812010-06-15 18:46:36 +0800319 /*
320 * Filter Paging from the network. We do not want to send a PAGING
321 * Command to every BSC in our network. We will analys the PAGING
322 * message and then send it to the authenticated messages...
323 */
324 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
325 int data_length;
326 const u_int8_t *data;
327 struct tlv_parsed tp;
328 int i = 0;
329
330 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
331 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
332 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
333 goto exit;
334 }
335
336 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
337 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
338 if (data[0] != CELL_IDENT_LAC) {
339 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
340 goto exit;
341 }
342
343 /* go through each LAC and forward the message */
344 for (i = 1; i < data_length - 1; i += 2) {
345 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
346 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
347 if (!bsc->authenticated || _lac != bsc->lac)
348 continue;
349
350 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
351 if (rc < msg->len)
352 LOGP(DNAT, LOGL_ERROR,
353 "Failed to write message to BTS: %d\n", rc);
354 }
355 }
356
357 goto exit;
358 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100359 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800360 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100361 if (!bsc->authenticated)
362 continue;
363
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100364 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
365
366 /* try the next one */
367 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100368 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100369 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800370
371exit:
372 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100373 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100374}
375
376static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
377{
378 int error;
379 struct msgb *msg = ipaccess_read_msg(bfd, &error);
380 struct ipaccess_head *hh;
381
382 if (!msg) {
383 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100384 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100385 exit(-2);
386 }
387
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100388 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100389 return -1;
390 }
391
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100392 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 +0100393
394 /* handle base message handling */
395 hh = (struct ipaccess_head *) msg->data;
396 ipaccess_rcvmsg_base(msg, bfd);
397
398 /* initialize the networking. This includes sending a GSM08.08 message */
399 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
400 initialize_msc_if_needed();
401 else if (hh->proto == IPAC_PROTO_SCCP)
402 forward_sccp_to_bts(msg);
403
404 return 0;
405}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800406
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100407/*
408 * Below is the handling of messages coming
409 * from the BSC and need to be forwarded to
410 * a real BSC.
411 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100412
413/*
414 * Remove the connection from the connections list,
415 * remove it from the patching of SCCP header lists
416 * as well. Maybe in the future even close connection..
417 */
418static void remove_bsc_connection(struct bsc_connection *connection)
419{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100420 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100421 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800422 close(connection->bsc_fd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100423 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100424
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800425 /* stop the timeout timer */
426 bsc_del_timer(&connection->id_timeout);
427
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100428 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800429 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100430 if (sccp_patch->bsc != connection)
431 continue;
432
433 llist_del(&sccp_patch->list_entry);
434 talloc_free(sccp_patch);
435 }
436
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100437 talloc_free(connection);
438}
439
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800440static void ipaccess_close_bsc(void *data)
441{
442 struct bsc_connection *conn = data;
443
444 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
445 remove_bsc_connection(conn);
446}
447
448static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
449{
450 struct bsc_config *conf;
451 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
452
453 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
454 if (strcmp(conf->token, token) == 0) {
455 bsc->authenticated = 1;
456 bsc->lac = conf->lac;
457 bsc_del_timer(&bsc->id_timeout);
458 break;
459 }
460 }
461}
462
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100463static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100464{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100465 struct bsc_connection *bsc;
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100466 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800467 struct bsc_nat_parsed *parsed;
468 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100469
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100470 bsc = bfd->data;
471
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800472 /* Parse and filter messages */
473 parsed = bsc_nat_parse(msg);
474 if (!parsed) {
475 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
476 return -1;
477 }
478
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100479 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800480 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800481
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100482 /* modify the SCCP entries */
483 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
484 switch (parsed->sccp_type) {
485 case SCCP_MSG_TYPE_CR:
486 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
487 goto exit2;
488 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
489 break;
490 case SCCP_MSG_TYPE_RLSD:
491 case SCCP_MSG_TYPE_CREF:
492 case SCCP_MSG_TYPE_DT1:
493 case SCCP_MSG_TYPE_CC:
494 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
495 break;
496 case SCCP_MSG_TYPE_RLC:
497 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
498 remove_sccp_src_ref(bsc, msg, parsed);
499 break;
500 case SCCP_MSG_TYPE_UDT:
501 /* simply forward everything */
502 break;
503 default:
504 goto exit2;
505 break;
506 }
507 }
508
509 if (found_bsc != bsc) {
510 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
511 goto exit2;
512 }
513
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100514 if (!bsc->authenticated) {
515 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
516 goto exit2;
517 }
518
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100519 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800520 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100521 talloc_free(parsed);
522 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800523
524exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100525 /* if we filter out the reset send an ack to the BSC */
526 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
527 send_reset_ack(bfd);
528 send_reset_ack(bfd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800529 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
530 /* do we know who is handling this? */
531 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
532 struct tlv_parsed tvp;
533 ipaccess_idtag_parse(&tvp,
534 (unsigned char *) msg->l2h + 2,
535 msgb_l2len(msg) - 2);
536 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
537 ipaccess_auth_bsc(&tvp, bsc);
538 }
539
540 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100541 }
542
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100543exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800544 talloc_free(parsed);
545 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100546}
547
548static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
549{
550 int error;
551 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100552
553 if (!msg) {
554 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100555 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100556 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100557 } else {
558 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100559 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100560 return -1;
561 }
562
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100563
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100564 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 +0100565
566 /* Handle messages from the BSC */
567 /* FIXME: Currently no PONG is sent to the BSC */
568 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100569 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100570
571 return 0;
572}
573
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100574static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
575{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100576 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100577 int ret;
578 struct sockaddr_in sa;
579 socklen_t sa_len = sizeof(sa);
580
581 if (!(what & BSC_FD_READ))
582 return 0;
583
584 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
585 if (ret < 0) {
586 perror("accept");
587 return ret;
588 }
589
590 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100591 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100592
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100593 /*
594 *
595 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800596 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100597 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100598 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100599 close(ret);
600 return -1;
601 }
602
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800603 bsc->nat = nat;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100604 bsc->bsc_fd.data = bsc;
605 bsc->bsc_fd.fd = ret;
606 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100607 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100608 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100609 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100610 close(ret);
611 talloc_free(bsc);
612 return -2;
613 }
614
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100615 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800616 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800617 ipaccess_send_id_ack(bsc->bsc_fd.fd);
618 ipaccess_send_id_req(ret);
619
620 /*
621 * start the hangup timer
622 */
623 bsc->id_timeout.data = bsc;
624 bsc->id_timeout.cb = ipaccess_close_bsc;
625 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100626 return 0;
627}
628
629static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
630{
631 struct sockaddr_in addr;
632 int ret, on = 1;
633
634 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
635 bfd->cb = ipaccess_listen_bsc_cb;
636 bfd->when = BSC_FD_READ;
637
638 memset(&addr, 0, sizeof(addr));
639 addr.sin_family = AF_INET;
640 addr.sin_port = htons(port);
641 addr.sin_addr.s_addr = in_addr->s_addr;
642
643 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
644
645 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
646 if (ret < 0) {
647 fprintf(stderr, "Could not bind the BSC socket %s\n",
648 strerror(errno));
649 return -EIO;
650 }
651
652 ret = listen(bfd->fd, 1);
653 if (ret < 0) {
654 perror("listen");
655 return ret;
656 }
657
658 ret = bsc_register_fd(bfd);
659 if (ret < 0) {
660 perror("register_listen_fd");
661 return ret;
662 }
663 return 0;
664}
665
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800666static void print_usage()
667{
668 printf("Usage: bsc_nat\n");
669}
670
671static void print_help()
672{
673 printf(" Some useful help...\n");
674 printf(" -h --help this text\n");
675 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
676 printf(" -s --disable-color\n");
677 printf(" -c --config-file filename The config file to use.\n");
678 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100679 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800680}
681
682static void handle_options(int argc, char** argv)
683{
684 while (1) {
685 int option_index = 0, c;
686 static struct option long_options[] = {
687 {"help", 0, 0, 'h'},
688 {"debug", 1, 0, 'd'},
689 {"config-file", 1, 0, 'c'},
690 {"disable-color", 0, 0, 's'},
691 {"timestamp", 0, 0, 'T'},
692 {"msc", 1, 0, 'm'},
693 {"local", 1, 0, 'l'},
694 {0, 0, 0, 0}
695 };
696
697 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
698 long_options, &option_index);
699 if (c == -1)
700 break;
701
702 switch (c) {
703 case 'h':
704 print_usage();
705 print_help();
706 exit(0);
707 case 's':
708 debug_use_color(0);
709 break;
710 case 'd':
711 debug_parse_category_mask(optarg);
712 break;
713 case 'c':
714 config_file = strdup(optarg);
715 break;
716 case 'T':
717 debug_timestamp(1);
718 break;
719 case 'm':
720 msc_address = strdup(optarg);
721 break;
722 case 'l':
723 inet_aton(optarg, &local_addr);
724 break;
725 default:
726 /* ignore */
727 break;
728 }
729 }
730}
731
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100732static void signal_handler(int signal)
733{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100734 switch (signal) {
735 case SIGABRT:
736 /* in case of abort, we want to obtain a talloc report
737 * and then return to the caller, who will abort the process */
738 case SIGUSR1:
739 talloc_report_full(tall_bsc_ctx, stderr);
740 break;
741 default:
742 break;
743 }
744}
745
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800746int main(int argc, char** argv)
747{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100748 int rc;
749
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800750 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100751 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800752 handle_options(argc, argv);
753
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800754 nat = bsc_nat_alloc();
755 if (!nat) {
756 fprintf(stderr, "Failed to allocate the BSC nat.\n");
757 return -4;
758 }
759
760 /* init vty and parse */
761 bsc_nat_vty_init(nat);
762 telnet_init(NULL, 4244);
763 if (vty_read_config_file(config_file) < 0) {
764 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
765 return -3;
766 }
767
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800768 /* seed the PRNG */
769 srand(time(NULL));
770
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100771 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100772 msc_connection.cb = ipaccess_msc_cb;
773 rc = connect_to_msc(&msc_connection, msc_address, 5000);
774 if (rc < 0) {
775 fprintf(stderr, "Opening the MSC connection failed.\n");
776 exit(1);
777 }
778
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100779 /* wait for the BSC */
780 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
781 fprintf(stderr, "Failed to listen for BSC.\n");
782 exit(1);
783 }
784
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100785 signal(SIGABRT, &signal_handler);
786 signal(SIGUSR1, &signal_handler);
787 signal(SIGPIPE, SIG_IGN);
788
789 while (1) {
790 bsc_select_main(0);
791 }
792
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800793 return 0;
794}