blob: 8dd42dc317f26036a2abe22df30a13c1b8b7877f [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{
153 llist_del(&connection->list_entry);
154 talloc_free(connection);
155}
156
157static int forward_sccp_to_msc(struct msgb *msg)
158{
159 /* FIXME: We need to filter out certain messages */
160
161 /* send the non-filtered but maybe modified msg */
162 return write(msc_connection.fd, msg->data, msg->data_len);
163}
164
165static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
166{
167 int error;
168 struct msgb *msg = ipaccess_read_msg(bfd, &error);
169 struct ipaccess_head *hh;
170
171 if (!msg) {
172 if (error == 0) {
173 fprintf(stderr, "The connection to the BSC was lost. Cleaning it\n");
174 remove_bsc_connection((struct bsc_connection *) bfd->data);
175 }
176
177 fprintf(stderr, "Failed to parse ip access message: %d\n", error);
178 return -1;
179 }
180
181 DEBUGP(DMSC, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
182
183 /* handle base message handling */
184 hh = (struct ipaccess_head *) msg->data;
185
186 /* Handle messages from the BSC */
187 /* FIXME: Currently no PONG is sent to the BSC */
188 /* FIXME: Currently no ID ACK is sent to the BSC */
189 if (hh->proto == IPAC_PROTO_SCCP)
190 forward_sccp_to_msc(msg);
191
192 return 0;
193}
194
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100195static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
196{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100197 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100198 int ret;
199 struct sockaddr_in sa;
200 socklen_t sa_len = sizeof(sa);
201
202 if (!(what & BSC_FD_READ))
203 return 0;
204
205 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
206 if (ret < 0) {
207 perror("accept");
208 return ret;
209 }
210
211 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100212 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100213
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100214 /*
215 *
216 */
217 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
218 if (!bsc) {
219 DEBUGP(DMSC, "Failed to allocate BSC struct.\n");
220 close(ret);
221 return -1;
222 }
223
224 bsc->bsc_fd.data = bsc;
225 bsc->bsc_fd.fd = ret;
226 bsc->bsc_fd.cb = ipaccess_bsc_cb;
227 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
228 DEBUGP(DMSC, "Failed to register BSC fd.\n");
229 close(ret);
230 talloc_free(bsc);
231 return -2;
232 }
233
234 DEBUGP(DMSC, "Registered new BSC\n");
235 llist_add(&bsc->list_entry, &bsc_connections);
236 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100237 return 0;
238}
239
240static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
241{
242 struct sockaddr_in addr;
243 int ret, on = 1;
244
245 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
246 bfd->cb = ipaccess_listen_bsc_cb;
247 bfd->when = BSC_FD_READ;
248
249 memset(&addr, 0, sizeof(addr));
250 addr.sin_family = AF_INET;
251 addr.sin_port = htons(port);
252 addr.sin_addr.s_addr = in_addr->s_addr;
253
254 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
255
256 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
257 if (ret < 0) {
258 fprintf(stderr, "Could not bind the BSC socket %s\n",
259 strerror(errno));
260 return -EIO;
261 }
262
263 ret = listen(bfd->fd, 1);
264 if (ret < 0) {
265 perror("listen");
266 return ret;
267 }
268
269 ret = bsc_register_fd(bfd);
270 if (ret < 0) {
271 perror("register_listen_fd");
272 return ret;
273 }
274 return 0;
275}
276
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800277static void print_usage()
278{
279 printf("Usage: bsc_nat\n");
280}
281
282static void print_help()
283{
284 printf(" Some useful help...\n");
285 printf(" -h --help this text\n");
286 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
287 printf(" -s --disable-color\n");
288 printf(" -c --config-file filename The config file to use.\n");
289 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100290 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800291}
292
293static void handle_options(int argc, char** argv)
294{
295 while (1) {
296 int option_index = 0, c;
297 static struct option long_options[] = {
298 {"help", 0, 0, 'h'},
299 {"debug", 1, 0, 'd'},
300 {"config-file", 1, 0, 'c'},
301 {"disable-color", 0, 0, 's'},
302 {"timestamp", 0, 0, 'T'},
303 {"msc", 1, 0, 'm'},
304 {"local", 1, 0, 'l'},
305 {0, 0, 0, 0}
306 };
307
308 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
309 long_options, &option_index);
310 if (c == -1)
311 break;
312
313 switch (c) {
314 case 'h':
315 print_usage();
316 print_help();
317 exit(0);
318 case 's':
319 debug_use_color(0);
320 break;
321 case 'd':
322 debug_parse_category_mask(optarg);
323 break;
324 case 'c':
325 config_file = strdup(optarg);
326 break;
327 case 'T':
328 debug_timestamp(1);
329 break;
330 case 'm':
331 msc_address = strdup(optarg);
332 break;
333 case 'l':
334 inet_aton(optarg, &local_addr);
335 break;
336 default:
337 /* ignore */
338 break;
339 }
340 }
341}
342
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100343static void signal_handler(int signal)
344{
345 fprintf(stdout, "signal %u received\n", signal);
346
347 switch (signal) {
348 case SIGABRT:
349 /* in case of abort, we want to obtain a talloc report
350 * and then return to the caller, who will abort the process */
351 case SIGUSR1:
352 talloc_report_full(tall_bsc_ctx, stderr);
353 break;
354 default:
355 break;
356 }
357}
358
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800359int main(int argc, char** argv)
360{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100361 int rc;
362
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800363 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100364 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800365 handle_options(argc, argv);
366
367 /* seed the PRNG */
368 srand(time(NULL));
369
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100370 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100371 msc_connection.cb = ipaccess_msc_cb;
372 rc = connect_to_msc(&msc_connection, msc_address, 5000);
373 if (rc < 0) {
374 fprintf(stderr, "Opening the MSC connection failed.\n");
375 exit(1);
376 }
377
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100378 /* wait for the BSC */
379 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
380 fprintf(stderr, "Failed to listen for BSC.\n");
381 exit(1);
382 }
383
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100384 signal(SIGINT, &signal_handler);
385 signal(SIGABRT, &signal_handler);
386 signal(SIGUSR1, &signal_handler);
387 signal(SIGPIPE, SIG_IGN);
388
389 while (1) {
390 bsc_select_main(0);
391 }
392
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800393 return 0;
394}