blob: 96444884cff1bfb721c6e4afa6d70274e3f8021b [file] [log] [blame]
Holger Hans Peter Freyther89d9fd92010-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 Freythere8fa0f12010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther89d9fd92010-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 Freythere8fa0f12010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freytherfd012d52010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther89d9fd92010-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 Freythere907cb22010-01-12 21:15:08 +010039#include <openbsc/msgb.h>
40#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
44#include <openbsc/talloc.h>
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010045#include <openbsc/linuxlist.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080046
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080047#include <sccp/sccp.h>
48
Holger Hans Peter Freyther89d9fd92010-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 Freythere907cb22010-01-12 21:15:08 +010052static struct bsc_fd msc_connection;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +010053static struct bsc_fd bsc_connection;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010054
Holger Hans Peter Freyther4a629602010-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 Freyther57adba52010-06-15 18:45:26 +080071
Holger Hans Peter Freythere907cb22010-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 Freyther4a629602010-01-13 09:28:12 +0100104 struct bsc_connection *bsc;
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100105 int rc;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100106
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100107 /* filter, drop, patch the message? */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100108
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800109 /* drop packets with the wrong IPA header */
110 if (bsc_nat_filter_ipa(msg))
111 return;
112
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100113 /* currently send this to every BSC connected */
114 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100115 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
116
117 /* try the next one */
118 if (rc < msg->len)
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100119 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100120 }
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100121}
122
123static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
124{
125 int error;
126 struct msgb *msg = ipaccess_read_msg(bfd, &error);
127 struct ipaccess_head *hh;
128
129 if (!msg) {
130 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100131 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100132 exit(-2);
133 }
134
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100135 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100136 return -1;
137 }
138
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100139 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100140
141 /* handle base message handling */
142 hh = (struct ipaccess_head *) msg->data;
143 ipaccess_rcvmsg_base(msg, bfd);
144
145 /* initialize the networking. This includes sending a GSM08.08 message */
146 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
147 initialize_msc_if_needed();
148 else if (hh->proto == IPAC_PROTO_SCCP)
149 forward_sccp_to_bts(msg);
150
151 return 0;
152}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800153
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100154/*
155 * Below is the handling of messages coming
156 * from the BSC and need to be forwarded to
157 * a real BSC.
158 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100159
160/*
161 * Remove the connection from the connections list,
162 * remove it from the patching of SCCP header lists
163 * as well. Maybe in the future even close connection..
164 */
165static void remove_bsc_connection(struct bsc_connection *connection)
166{
Holger Hans Peter Freytherb8567c82010-01-13 09:51:23 +0100167 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100168 llist_del(&connection->list_entry);
169 talloc_free(connection);
170}
171
172static int forward_sccp_to_msc(struct msgb *msg)
173{
174 /* FIXME: We need to filter out certain messages */
175
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800176 /* drop packets with the wrong IPA header */
177 if (bsc_nat_filter_ipa(msg))
178 return 0;
179
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100180 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther57a6ac82010-01-13 15:22:20 +0100181 return write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100182}
183
184static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
185{
186 int error;
187 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100188
189 if (!msg) {
190 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100191 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100192 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100193 } else {
194 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100195 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100196 return -1;
197 }
198
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100199
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100200 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100201
202 /* Handle messages from the BSC */
203 /* FIXME: Currently no PONG is sent to the BSC */
204 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800205 forward_sccp_to_msc(msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100206
207 return 0;
208}
209
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100210static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
211{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100212 struct bsc_connection *bsc;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100213 int ret;
214 struct sockaddr_in sa;
215 socklen_t sa_len = sizeof(sa);
216
217 if (!(what & BSC_FD_READ))
218 return 0;
219
220 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
221 if (ret < 0) {
222 perror("accept");
223 return ret;
224 }
225
226 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100227 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100228
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100229 /*
230 *
231 */
232 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
233 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100234 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100235 close(ret);
236 return -1;
237 }
238
239 bsc->bsc_fd.data = bsc;
240 bsc->bsc_fd.fd = ret;
241 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freyther058519d2010-01-13 09:52:29 +0100242 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100243 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100244 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100245 close(ret);
246 talloc_free(bsc);
247 return -2;
248 }
249
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100250 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100251 llist_add(&bsc->list_entry, &bsc_connections);
252 ipaccess_send_id_ack(ret);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100253 return 0;
254}
255
256static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
257{
258 struct sockaddr_in addr;
259 int ret, on = 1;
260
261 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
262 bfd->cb = ipaccess_listen_bsc_cb;
263 bfd->when = BSC_FD_READ;
264
265 memset(&addr, 0, sizeof(addr));
266 addr.sin_family = AF_INET;
267 addr.sin_port = htons(port);
268 addr.sin_addr.s_addr = in_addr->s_addr;
269
270 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
271
272 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
273 if (ret < 0) {
274 fprintf(stderr, "Could not bind the BSC socket %s\n",
275 strerror(errno));
276 return -EIO;
277 }
278
279 ret = listen(bfd->fd, 1);
280 if (ret < 0) {
281 perror("listen");
282 return ret;
283 }
284
285 ret = bsc_register_fd(bfd);
286 if (ret < 0) {
287 perror("register_listen_fd");
288 return ret;
289 }
290 return 0;
291}
292
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800293static void print_usage()
294{
295 printf("Usage: bsc_nat\n");
296}
297
298static void print_help()
299{
300 printf(" Some useful help...\n");
301 printf(" -h --help this text\n");
302 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
303 printf(" -s --disable-color\n");
304 printf(" -c --config-file filename The config file to use.\n");
305 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100306 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800307}
308
309static void handle_options(int argc, char** argv)
310{
311 while (1) {
312 int option_index = 0, c;
313 static struct option long_options[] = {
314 {"help", 0, 0, 'h'},
315 {"debug", 1, 0, 'd'},
316 {"config-file", 1, 0, 'c'},
317 {"disable-color", 0, 0, 's'},
318 {"timestamp", 0, 0, 'T'},
319 {"msc", 1, 0, 'm'},
320 {"local", 1, 0, 'l'},
321 {0, 0, 0, 0}
322 };
323
324 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
325 long_options, &option_index);
326 if (c == -1)
327 break;
328
329 switch (c) {
330 case 'h':
331 print_usage();
332 print_help();
333 exit(0);
334 case 's':
335 debug_use_color(0);
336 break;
337 case 'd':
338 debug_parse_category_mask(optarg);
339 break;
340 case 'c':
341 config_file = strdup(optarg);
342 break;
343 case 'T':
344 debug_timestamp(1);
345 break;
346 case 'm':
347 msc_address = strdup(optarg);
348 break;
349 case 'l':
350 inet_aton(optarg, &local_addr);
351 break;
352 default:
353 /* ignore */
354 break;
355 }
356 }
357}
358
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100359static void signal_handler(int signal)
360{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100361 switch (signal) {
362 case SIGABRT:
363 /* in case of abort, we want to obtain a talloc report
364 * and then return to the caller, who will abort the process */
365 case SIGUSR1:
366 talloc_report_full(tall_bsc_ctx, stderr);
367 break;
368 default:
369 break;
370 }
371}
372
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800373int main(int argc, char** argv)
374{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100375 int rc;
376
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800377 /* parse options */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100378 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800379 handle_options(argc, argv);
380
381 /* seed the PRNG */
382 srand(time(NULL));
383
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100384 /* connect to the MSC */
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100385 msc_connection.cb = ipaccess_msc_cb;
386 rc = connect_to_msc(&msc_connection, msc_address, 5000);
387 if (rc < 0) {
388 fprintf(stderr, "Opening the MSC connection failed.\n");
389 exit(1);
390 }
391
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100392 /* wait for the BSC */
393 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
394 fprintf(stderr, "Failed to listen for BSC.\n");
395 exit(1);
396 }
397
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100398 signal(SIGINT, &signal_handler);
399 signal(SIGABRT, &signal_handler);
400 signal(SIGUSR1, &signal_handler);
401 signal(SIGPIPE, SIG_IGN);
402
403 while (1) {
404 bsc_select_main(0);
405 }
406
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800407 return 0;
408}