blob: bd0b6c338155a6a9a2f909250bcdd92ba9aaf622 [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 Freyther24614ad2010-01-13 09:28:12 +010046#include <openbsc/linuxlist.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080047
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080048#include <sccp/sccp.h>
49
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080050static const char *config_file = "openbsc.cfg";
51static char *msc_address = "127.0.0.1";
52static struct in_addr local_addr;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010053static struct bsc_fd msc_connection;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010054static struct bsc_fd bsc_connection;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010055
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010056
57/*
58 * Per BSC data structure
59 */
60struct bsc_connection {
61 struct llist_head list_entry;
62
63 /* do we know anything about this BSC? */
64 int authenticated;
65
66 /* the fd we use to communicate */
67 struct bsc_fd bsc_fd;
68};
69
70static LLIST_HEAD(bsc_connections);
71
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080072
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010073/*
74 * below are stubs we need to link
75 */
76int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
77 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
78{
79 return -1;
80}
81
82void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
83{}
84
85int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
86{
87 return -1;
88}
89
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010090static int send_reset_ack(struct bsc_fd *bfd)
91{
92 static const u_int8_t gsm_reset_ack[] = {
93 0x00, 0x13, 0xfd,
94 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
95 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
96 0x00, 0x01, 0x31,
97 };
98
99 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
100}
101
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100102/*
103 * Below is the handling of messages coming
104 * from the MSC and need to be forwarded to
105 * a real BSC.
106 */
107static void initialize_msc_if_needed()
108{
109 static int init = 0;
110 init = 1;
111
112 /* do we need to send a GSM 08.08 message here? */
113}
114
115static void forward_sccp_to_bts(struct msgb *msg)
116{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100117 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800118 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100119 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100120
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100121 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800122 parsed = bsc_nat_parse(msg);
123 if (!parsed) {
124 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800125 return;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800126 }
127
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100128 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800129 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800130
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100131 /* currently send this to every BSC connected */
132 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100133 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
134
135 /* try the next one */
136 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100137 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100138 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800139
140exit:
141 talloc_free(parsed);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100142}
143
144static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
145{
146 int error;
147 struct msgb *msg = ipaccess_read_msg(bfd, &error);
148 struct ipaccess_head *hh;
149
150 if (!msg) {
151 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100152 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100153 exit(-2);
154 }
155
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100156 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100157 return -1;
158 }
159
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100160 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 +0100161
162 /* handle base message handling */
163 hh = (struct ipaccess_head *) msg->data;
164 ipaccess_rcvmsg_base(msg, bfd);
165
166 /* initialize the networking. This includes sending a GSM08.08 message */
167 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
168 initialize_msc_if_needed();
169 else if (hh->proto == IPAC_PROTO_SCCP)
170 forward_sccp_to_bts(msg);
171
172 return 0;
173}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800174
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100175/*
176 * Below is the handling of messages coming
177 * from the BSC and need to be forwarded to
178 * a real BSC.
179 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100180
181/*
182 * Remove the connection from the connections list,
183 * remove it from the patching of SCCP header lists
184 * as well. Maybe in the future even close connection..
185 */
186static void remove_bsc_connection(struct bsc_connection *connection)
187{
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100188 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100189 llist_del(&connection->list_entry);
190 talloc_free(connection);
191}
192
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100193static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100194{
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800195 struct bsc_nat_parsed *parsed;
196 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100197
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800198 /* Parse and filter messages */
199 parsed = bsc_nat_parse(msg);
200 if (!parsed) {
201 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
202 return -1;
203 }
204
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100205 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800206 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800207
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100208 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800209 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100210 talloc_free(parsed);
211 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800212
213exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100214 /* if we filter out the reset send an ack to the BSC */
215 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
216 send_reset_ack(bfd);
217 send_reset_ack(bfd);
218 }
219
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800220 talloc_free(parsed);
221 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100222}
223
224static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
225{
226 int error;
227 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100228
229 if (!msg) {
230 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100231 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100232 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100233 } else {
234 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100235 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100236 return -1;
237 }
238
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100239
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100240 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 +0100241
242 /* Handle messages from the BSC */
243 /* FIXME: Currently no PONG is sent to the BSC */
244 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100245 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100246
247 return 0;
248}
249
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100250static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
251{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100252 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100253 int ret;
254 struct sockaddr_in sa;
255 socklen_t sa_len = sizeof(sa);
256
257 if (!(what & BSC_FD_READ))
258 return 0;
259
260 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
261 if (ret < 0) {
262 perror("accept");
263 return ret;
264 }
265
266 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100267 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100268
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100269 /*
270 *
271 */
272 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
273 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100274 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100275 close(ret);
276 return -1;
277 }
278
279 bsc->bsc_fd.data = bsc;
280 bsc->bsc_fd.fd = ret;
281 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100282 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100283 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100284 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100285 close(ret);
286 talloc_free(bsc);
287 return -2;
288 }
289
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100290 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100291 llist_add(&bsc->list_entry, &bsc_connections);
292 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100293 return 0;
294}
295
296static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
297{
298 struct sockaddr_in addr;
299 int ret, on = 1;
300
301 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
302 bfd->cb = ipaccess_listen_bsc_cb;
303 bfd->when = BSC_FD_READ;
304
305 memset(&addr, 0, sizeof(addr));
306 addr.sin_family = AF_INET;
307 addr.sin_port = htons(port);
308 addr.sin_addr.s_addr = in_addr->s_addr;
309
310 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
311
312 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
313 if (ret < 0) {
314 fprintf(stderr, "Could not bind the BSC socket %s\n",
315 strerror(errno));
316 return -EIO;
317 }
318
319 ret = listen(bfd->fd, 1);
320 if (ret < 0) {
321 perror("listen");
322 return ret;
323 }
324
325 ret = bsc_register_fd(bfd);
326 if (ret < 0) {
327 perror("register_listen_fd");
328 return ret;
329 }
330 return 0;
331}
332
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800333static void print_usage()
334{
335 printf("Usage: bsc_nat\n");
336}
337
338static void print_help()
339{
340 printf(" Some useful help...\n");
341 printf(" -h --help this text\n");
342 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
343 printf(" -s --disable-color\n");
344 printf(" -c --config-file filename The config file to use.\n");
345 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100346 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800347}
348
349static void handle_options(int argc, char** argv)
350{
351 while (1) {
352 int option_index = 0, c;
353 static struct option long_options[] = {
354 {"help", 0, 0, 'h'},
355 {"debug", 1, 0, 'd'},
356 {"config-file", 1, 0, 'c'},
357 {"disable-color", 0, 0, 's'},
358 {"timestamp", 0, 0, 'T'},
359 {"msc", 1, 0, 'm'},
360 {"local", 1, 0, 'l'},
361 {0, 0, 0, 0}
362 };
363
364 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
365 long_options, &option_index);
366 if (c == -1)
367 break;
368
369 switch (c) {
370 case 'h':
371 print_usage();
372 print_help();
373 exit(0);
374 case 's':
375 debug_use_color(0);
376 break;
377 case 'd':
378 debug_parse_category_mask(optarg);
379 break;
380 case 'c':
381 config_file = strdup(optarg);
382 break;
383 case 'T':
384 debug_timestamp(1);
385 break;
386 case 'm':
387 msc_address = strdup(optarg);
388 break;
389 case 'l':
390 inet_aton(optarg, &local_addr);
391 break;
392 default:
393 /* ignore */
394 break;
395 }
396 }
397}
398
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100399static void signal_handler(int signal)
400{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100401 switch (signal) {
402 case SIGABRT:
403 /* in case of abort, we want to obtain a talloc report
404 * and then return to the caller, who will abort the process */
405 case SIGUSR1:
406 talloc_report_full(tall_bsc_ctx, stderr);
407 break;
408 default:
409 break;
410 }
411}
412
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800413int main(int argc, char** argv)
414{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100415 int rc;
416
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800417 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100418 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800419 handle_options(argc, argv);
420
421 /* seed the PRNG */
422 srand(time(NULL));
423
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100424 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100425 msc_connection.cb = ipaccess_msc_cb;
426 rc = connect_to_msc(&msc_connection, msc_address, 5000);
427 if (rc < 0) {
428 fprintf(stderr, "Opening the MSC connection failed.\n");
429 exit(1);
430 }
431
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100432 /* wait for the BSC */
433 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
434 fprintf(stderr, "Failed to listen for BSC.\n");
435 exit(1);
436 }
437
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100438 signal(SIGINT, &signal_handler);
439 signal(SIGABRT, &signal_handler);
440 signal(SIGUSR1, &signal_handler);
441 signal(SIGPIPE, SIG_IGN);
442
443 while (1) {
444 bsc_select_main(0);
445 }
446
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800447 return 0;
448}