blob: ffd0af1c403e9f62d8ba9fa6c947ab4318b93771 [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>
41#include <openbsc/ipaccess.h>
42#include <openbsc/abis_nm.h>
43#include <openbsc/talloc.h>
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010044#include <openbsc/linuxlist.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080045
46static const char *config_file = "openbsc.cfg";
47static char *msc_address = "127.0.0.1";
48static struct in_addr local_addr;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010049static struct bsc_fd msc_connection;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010050static struct bsc_fd bsc_connection;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010051
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010052
53/*
54 * Per BSC data structure
55 */
56struct bsc_connection {
57 struct llist_head list_entry;
58
59 /* do we know anything about this BSC? */
60 int authenticated;
61
62 /* the fd we use to communicate */
63 struct bsc_fd bsc_fd;
64};
65
66static LLIST_HEAD(bsc_connections);
67
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010068/*
69 * below are stubs we need to link
70 */
71int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
72 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
73{
74 return -1;
75}
76
77void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
78{}
79
80int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
81{
82 return -1;
83}
84
85/*
86 * Below is the handling of messages coming
87 * from the MSC and need to be forwarded to
88 * a real BSC.
89 */
90static void initialize_msc_if_needed()
91{
92 static int init = 0;
93 init = 1;
94
95 /* do we need to send a GSM 08.08 message here? */
96}
97
98static void forward_sccp_to_bts(struct msgb *msg)
99{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100100 struct bsc_connection *bsc;
101
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100102 /* filter, drop, patch the message? */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100103
104 /* currently send this to every BSC connected */
105 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
106 }
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100107}
108
109static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
110{
111 int error;
112 struct msgb *msg = ipaccess_read_msg(bfd, &error);
113 struct ipaccess_head *hh;
114
115 if (!msg) {
116 if (error == 0) {
117 fprintf(stderr, "The connection to the MSC was lost, exiting\n");
118 exit(-2);
119 }
120
121 fprintf(stderr, "Failed to parse ip access message: %d\n", error);
122 return -1;
123 }
124
125 DEBUGP(DMSC, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
126
127 /* handle base message handling */
128 hh = (struct ipaccess_head *) msg->data;
129 ipaccess_rcvmsg_base(msg, bfd);
130
131 /* initialize the networking. This includes sending a GSM08.08 message */
132 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
133 initialize_msc_if_needed();
134 else if (hh->proto == IPAC_PROTO_SCCP)
135 forward_sccp_to_bts(msg);
136
137 return 0;
138}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800139
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100140/*
141 * Below is the handling of messages coming
142 * from the BSC and need to be forwarded to
143 * a real BSC.
144 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100145
146/*
147 * Remove the connection from the connections list,
148 * remove it from the patching of SCCP header lists
149 * as well. Maybe in the future even close connection..
150 */
151static void remove_bsc_connection(struct bsc_connection *connection)
152{
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100153 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100154 llist_del(&connection->list_entry);
155 talloc_free(connection);
156}
157
158static int forward_sccp_to_msc(struct msgb *msg)
159{
160 /* FIXME: We need to filter out certain messages */
161
162 /* send the non-filtered but maybe modified msg */
163 return write(msc_connection.fd, msg->data, msg->data_len);
164}
165
166static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
167{
168 int error;
169 struct msgb *msg = ipaccess_read_msg(bfd, &error);
170 struct ipaccess_head *hh;
171
172 if (!msg) {
173 if (error == 0) {
174 fprintf(stderr, "The connection to the BSC was lost. Cleaning it\n");
175 remove_bsc_connection((struct bsc_connection *) bfd->data);
176 }
177
178 fprintf(stderr, "Failed to parse ip access message: %d\n", error);
179 return -1;
180 }
181
182 DEBUGP(DMSC, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
183
184 /* handle base message handling */
185 hh = (struct ipaccess_head *) msg->data;
186
187 /* Handle messages from the BSC */
188 /* FIXME: Currently no PONG is sent to the BSC */
189 /* FIXME: Currently no ID ACK is sent to the BSC */
190 if (hh->proto == IPAC_PROTO_SCCP)
191 forward_sccp_to_msc(msg);
192
193 return 0;
194}
195
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100196static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
197{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100198 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100199 int ret;
200 struct sockaddr_in sa;
201 socklen_t sa_len = sizeof(sa);
202
203 if (!(what & BSC_FD_READ))
204 return 0;
205
206 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
207 if (ret < 0) {
208 perror("accept");
209 return ret;
210 }
211
212 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100213 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100214
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100215 /*
216 *
217 */
218 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
219 if (!bsc) {
220 DEBUGP(DMSC, "Failed to allocate BSC struct.\n");
221 close(ret);
222 return -1;
223 }
224
225 bsc->bsc_fd.data = bsc;
226 bsc->bsc_fd.fd = ret;
227 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100228 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100229 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
230 DEBUGP(DMSC, "Failed to register BSC fd.\n");
231 close(ret);
232 talloc_free(bsc);
233 return -2;
234 }
235
236 DEBUGP(DMSC, "Registered new BSC\n");
237 llist_add(&bsc->list_entry, &bsc_connections);
238 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100239 return 0;
240}
241
242static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
243{
244 struct sockaddr_in addr;
245 int ret, on = 1;
246
247 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
248 bfd->cb = ipaccess_listen_bsc_cb;
249 bfd->when = BSC_FD_READ;
250
251 memset(&addr, 0, sizeof(addr));
252 addr.sin_family = AF_INET;
253 addr.sin_port = htons(port);
254 addr.sin_addr.s_addr = in_addr->s_addr;
255
256 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
257
258 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
259 if (ret < 0) {
260 fprintf(stderr, "Could not bind the BSC socket %s\n",
261 strerror(errno));
262 return -EIO;
263 }
264
265 ret = listen(bfd->fd, 1);
266 if (ret < 0) {
267 perror("listen");
268 return ret;
269 }
270
271 ret = bsc_register_fd(bfd);
272 if (ret < 0) {
273 perror("register_listen_fd");
274 return ret;
275 }
276 return 0;
277}
278
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800279static void print_usage()
280{
281 printf("Usage: bsc_nat\n");
282}
283
284static void print_help()
285{
286 printf(" Some useful help...\n");
287 printf(" -h --help this text\n");
288 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
289 printf(" -s --disable-color\n");
290 printf(" -c --config-file filename The config file to use.\n");
291 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100292 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800293}
294
295static void handle_options(int argc, char** argv)
296{
297 while (1) {
298 int option_index = 0, c;
299 static struct option long_options[] = {
300 {"help", 0, 0, 'h'},
301 {"debug", 1, 0, 'd'},
302 {"config-file", 1, 0, 'c'},
303 {"disable-color", 0, 0, 's'},
304 {"timestamp", 0, 0, 'T'},
305 {"msc", 1, 0, 'm'},
306 {"local", 1, 0, 'l'},
307 {0, 0, 0, 0}
308 };
309
310 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
311 long_options, &option_index);
312 if (c == -1)
313 break;
314
315 switch (c) {
316 case 'h':
317 print_usage();
318 print_help();
319 exit(0);
320 case 's':
321 debug_use_color(0);
322 break;
323 case 'd':
324 debug_parse_category_mask(optarg);
325 break;
326 case 'c':
327 config_file = strdup(optarg);
328 break;
329 case 'T':
330 debug_timestamp(1);
331 break;
332 case 'm':
333 msc_address = strdup(optarg);
334 break;
335 case 'l':
336 inet_aton(optarg, &local_addr);
337 break;
338 default:
339 /* ignore */
340 break;
341 }
342 }
343}
344
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100345static void signal_handler(int signal)
346{
347 fprintf(stdout, "signal %u received\n", signal);
348
349 switch (signal) {
350 case SIGABRT:
351 /* in case of abort, we want to obtain a talloc report
352 * and then return to the caller, who will abort the process */
353 case SIGUSR1:
354 talloc_report_full(tall_bsc_ctx, stderr);
355 break;
356 default:
357 break;
358 }
359}
360
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800361int main(int argc, char** argv)
362{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100363 int rc;
364
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800365 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100366 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800367 handle_options(argc, argv);
368
369 /* seed the PRNG */
370 srand(time(NULL));
371
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100372 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100373 msc_connection.cb = ipaccess_msc_cb;
374 rc = connect_to_msc(&msc_connection, msc_address, 5000);
375 if (rc < 0) {
376 fprintf(stderr, "Opening the MSC connection failed.\n");
377 exit(1);
378 }
379
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100380 /* wait for the BSC */
381 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
382 fprintf(stderr, "Failed to listen for BSC.\n");
383 exit(1);
384 }
385
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100386 signal(SIGINT, &signal_handler);
387 signal(SIGABRT, &signal_handler);
388 signal(SIGUSR1, &signal_handler);
389 signal(SIGPIPE, SIG_IGN);
390
391 while (1) {
392 bsc_select_main(0);
393 }
394
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800395 return 0;
396}