blob: 98eaa979423fb48f138d64e8afb5042ebbae90cb [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 Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
44#include <openbsc/talloc.h>
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010045#include <openbsc/linuxlist.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080046
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080047#include <sccp/sccp.h>
48
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080049static const char *config_file = "openbsc.cfg";
50static char *msc_address = "127.0.0.1";
51static struct in_addr local_addr;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010052static struct bsc_fd msc_connection;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010053static struct bsc_fd bsc_connection;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010054
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010055
56/*
57 * Per BSC data structure
58 */
59struct bsc_connection {
60 struct llist_head list_entry;
61
62 /* do we know anything about this BSC? */
63 int authenticated;
64
65 /* the fd we use to communicate */
66 struct bsc_fd bsc_fd;
67};
68
69static LLIST_HEAD(bsc_connections);
70
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080071
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010072/*
73 * below are stubs we need to link
74 */
75int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
76 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
77{
78 return -1;
79}
80
81void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
82{}
83
84int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
85{
86 return -1;
87}
88
89/*
90 * Below is the handling of messages coming
91 * from the MSC and need to be forwarded to
92 * a real BSC.
93 */
94static void initialize_msc_if_needed()
95{
96 static int init = 0;
97 init = 1;
98
99 /* do we need to send a GSM 08.08 message here? */
100}
101
102static void forward_sccp_to_bts(struct msgb *msg)
103{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100104 struct bsc_connection *bsc;
105
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100106 /* filter, drop, patch the message? */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100107
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800108 /* drop packets with the wrong IPA header */
109 if (bsc_nat_filter_ipa(msg))
110 return;
111
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100112 /* currently send this to every BSC connected */
113 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
Holger Hans Peter Freytherd7fb9542010-01-13 15:22:20 +0100114 write(bsc->bsc_fd.fd, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100115 }
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100116}
117
118static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
119{
120 int error;
121 struct msgb *msg = ipaccess_read_msg(bfd, &error);
122 struct ipaccess_head *hh;
123
124 if (!msg) {
125 if (error == 0) {
126 fprintf(stderr, "The connection to the MSC was lost, exiting\n");
127 exit(-2);
128 }
129
130 fprintf(stderr, "Failed to parse ip access message: %d\n", error);
131 return -1;
132 }
133
134 DEBUGP(DMSC, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
135
136 /* handle base message handling */
137 hh = (struct ipaccess_head *) msg->data;
138 ipaccess_rcvmsg_base(msg, bfd);
139
140 /* initialize the networking. This includes sending a GSM08.08 message */
141 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
142 initialize_msc_if_needed();
143 else if (hh->proto == IPAC_PROTO_SCCP)
144 forward_sccp_to_bts(msg);
145
146 return 0;
147}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800148
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100149/*
150 * Below is the handling of messages coming
151 * from the BSC and need to be forwarded to
152 * a real BSC.
153 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100154
155/*
156 * Remove the connection from the connections list,
157 * remove it from the patching of SCCP header lists
158 * as well. Maybe in the future even close connection..
159 */
160static void remove_bsc_connection(struct bsc_connection *connection)
161{
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100162 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100163 llist_del(&connection->list_entry);
164 talloc_free(connection);
165}
166
167static int forward_sccp_to_msc(struct msgb *msg)
168{
169 /* FIXME: We need to filter out certain messages */
170
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800171 /* drop packets with the wrong IPA header */
172 if (bsc_nat_filter_ipa(msg))
173 return 0;
174
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100175 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherd7fb9542010-01-13 15:22:20 +0100176 return write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100177}
178
179static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
180{
181 int error;
182 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100183
184 if (!msg) {
185 if (error == 0) {
186 fprintf(stderr, "The connection to the BSC was lost. Cleaning it\n");
187 remove_bsc_connection((struct bsc_connection *) bfd->data);
188 }
189
190 fprintf(stderr, "Failed to parse ip access message: %d\n", error);
191 return -1;
192 }
193
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100194
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800195 DEBUGP(DMSC, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100196
197 /* Handle messages from the BSC */
198 /* FIXME: Currently no PONG is sent to the BSC */
199 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800200 forward_sccp_to_msc(msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100201
202 return 0;
203}
204
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100205static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
206{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100207 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100208 int ret;
209 struct sockaddr_in sa;
210 socklen_t sa_len = sizeof(sa);
211
212 if (!(what & BSC_FD_READ))
213 return 0;
214
215 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
216 if (ret < 0) {
217 perror("accept");
218 return ret;
219 }
220
221 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100222 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100223
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100224 /*
225 *
226 */
227 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
228 if (!bsc) {
229 DEBUGP(DMSC, "Failed to allocate BSC struct.\n");
230 close(ret);
231 return -1;
232 }
233
234 bsc->bsc_fd.data = bsc;
235 bsc->bsc_fd.fd = ret;
236 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100237 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100238 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
239 DEBUGP(DMSC, "Failed to register BSC fd.\n");
240 close(ret);
241 talloc_free(bsc);
242 return -2;
243 }
244
245 DEBUGP(DMSC, "Registered new BSC\n");
246 llist_add(&bsc->list_entry, &bsc_connections);
247 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100248 return 0;
249}
250
251static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
252{
253 struct sockaddr_in addr;
254 int ret, on = 1;
255
256 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
257 bfd->cb = ipaccess_listen_bsc_cb;
258 bfd->when = BSC_FD_READ;
259
260 memset(&addr, 0, sizeof(addr));
261 addr.sin_family = AF_INET;
262 addr.sin_port = htons(port);
263 addr.sin_addr.s_addr = in_addr->s_addr;
264
265 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
266
267 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
268 if (ret < 0) {
269 fprintf(stderr, "Could not bind the BSC socket %s\n",
270 strerror(errno));
271 return -EIO;
272 }
273
274 ret = listen(bfd->fd, 1);
275 if (ret < 0) {
276 perror("listen");
277 return ret;
278 }
279
280 ret = bsc_register_fd(bfd);
281 if (ret < 0) {
282 perror("register_listen_fd");
283 return ret;
284 }
285 return 0;
286}
287
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800288static void print_usage()
289{
290 printf("Usage: bsc_nat\n");
291}
292
293static void print_help()
294{
295 printf(" Some useful help...\n");
296 printf(" -h --help this text\n");
297 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
298 printf(" -s --disable-color\n");
299 printf(" -c --config-file filename The config file to use.\n");
300 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100301 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800302}
303
304static void handle_options(int argc, char** argv)
305{
306 while (1) {
307 int option_index = 0, c;
308 static struct option long_options[] = {
309 {"help", 0, 0, 'h'},
310 {"debug", 1, 0, 'd'},
311 {"config-file", 1, 0, 'c'},
312 {"disable-color", 0, 0, 's'},
313 {"timestamp", 0, 0, 'T'},
314 {"msc", 1, 0, 'm'},
315 {"local", 1, 0, 'l'},
316 {0, 0, 0, 0}
317 };
318
319 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
320 long_options, &option_index);
321 if (c == -1)
322 break;
323
324 switch (c) {
325 case 'h':
326 print_usage();
327 print_help();
328 exit(0);
329 case 's':
330 debug_use_color(0);
331 break;
332 case 'd':
333 debug_parse_category_mask(optarg);
334 break;
335 case 'c':
336 config_file = strdup(optarg);
337 break;
338 case 'T':
339 debug_timestamp(1);
340 break;
341 case 'm':
342 msc_address = strdup(optarg);
343 break;
344 case 'l':
345 inet_aton(optarg, &local_addr);
346 break;
347 default:
348 /* ignore */
349 break;
350 }
351 }
352}
353
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100354static void signal_handler(int signal)
355{
356 fprintf(stdout, "signal %u received\n", signal);
357
358 switch (signal) {
359 case SIGABRT:
360 /* in case of abort, we want to obtain a talloc report
361 * and then return to the caller, who will abort the process */
362 case SIGUSR1:
363 talloc_report_full(tall_bsc_ctx, stderr);
364 break;
365 default:
366 break;
367 }
368}
369
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800370int main(int argc, char** argv)
371{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100372 int rc;
373
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800374 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100375 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800376 handle_options(argc, argv);
377
378 /* seed the PRNG */
379 srand(time(NULL));
380
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100381 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100382 msc_connection.cb = ipaccess_msc_cb;
383 rc = connect_to_msc(&msc_connection, msc_address, 5000);
384 if (rc < 0) {
385 fprintf(stderr, "Opening the MSC connection failed.\n");
386 exit(1);
387 }
388
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100389 /* wait for the BSC */
390 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
391 fprintf(stderr, "Failed to listen for BSC.\n");
392 exit(1);
393 }
394
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100395 signal(SIGINT, &signal_handler);
396 signal(SIGABRT, &signal_handler);
397 signal(SIGUSR1, &signal_handler);
398 signal(SIGPIPE, SIG_IGN);
399
400 while (1) {
401 bsc_select_main(0);
402 }
403
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800404 return 0;
405}