blob: 49d791d0924e5d04df05c6a718e4b63da56adfb7 [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
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010057static LLIST_HEAD(bsc_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +010058static LLIST_HEAD(sccp_connections);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010059
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010060/*
61 * below are stubs we need to link
62 */
63int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
64 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
65{
66 return -1;
67}
68
69void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
70{}
71
72int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
73{
74 return -1;
75}
76
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010077static int send_reset_ack(struct bsc_fd *bfd)
78{
79 static const u_int8_t gsm_reset_ack[] = {
80 0x00, 0x13, 0xfd,
81 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
82 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
83 0x00, 0x01, 0x31,
84 };
85
86 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
87}
88
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010089/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +010090 * SCCP patching below
91 */
92
93/* check if we are using this ref for patched already */
94static int sccp_ref_is_free(struct sccp_source_reference *ref)
95{
96 struct sccp_connections *conn;
97
98 llist_for_each_entry(conn, &sccp_connections, list_entry) {
99 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
100 return -1;
101 }
102
103 return 0;
104}
105
106/* copied from sccp.c */
107static int assign_src_local_reference(struct sccp_source_reference *ref)
108{
109 static u_int32_t last_ref = 0x50000;
110 int wrapped = 0;
111
112 do {
113 struct sccp_source_reference reference;
114 reference.octet1 = (last_ref >> 0) & 0xff;
115 reference.octet2 = (last_ref >> 8) & 0xff;
116 reference.octet3 = (last_ref >> 16) & 0xff;
117
118 ++last_ref;
119 /* do not use the reversed word and wrap around */
120 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
121 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
122 last_ref = 0;
123 ++wrapped;
124 }
125
126 if (sccp_ref_is_free(&reference) == 0) {
127 *ref = reference;
128 return 0;
129 }
130 } while (wrapped != 2);
131
132 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
133 return -1;
134}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100135
136static int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100137{
138 struct sccp_connections *conn;
139
140 conn = talloc_zero(tall_bsc_ctx, struct sccp_connections);
141 if (!conn) {
142 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
143 return -1;
144 }
145
146 conn->real_ref = *parsed->src_local_ref;
147 if (assign_src_local_reference(&conn->patched_ref) != 0) {
148 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
149 talloc_free(conn);
150 return -1;
151 }
152
153 return 0;
154}
155
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100156static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100157{
158 struct sccp_connections *conn;
159
160 llist_for_each_entry(conn, &sccp_connections, list_entry) {
161 if (memcmp(parsed->src_local_ref,
162 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
163 if (bsc != conn->bsc) {
164 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
165 continue;
166 }
167
168
169 llist_del(&conn->list_entry);
170 talloc_free(conn);
171 return;
172 }
173 }
174
175 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
176}
177
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100178static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100179{
180 struct sccp_connections *conn;
181 llist_for_each_entry(conn, &sccp_connections, list_entry) {
182 if (memcmp(parsed->dest_local_ref,
183 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
184 memcpy(parsed->dest_local_ref,
185 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
186 return conn->bsc;
187 }
188 }
189
190 return NULL;
191}
192
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100193static struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100194{
195 struct sccp_connections *conn;
196 llist_for_each_entry(conn, &sccp_connections, list_entry) {
197 if (memcmp(parsed->src_local_ref,
198 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
199 memcpy(parsed->src_local_ref,
200 &conn->patched_ref, sizeof(*parsed->src_local_ref));
201 return conn->bsc;
202 }
203 }
204
205 return NULL;
206}
207
208/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100209 * Below is the handling of messages coming
210 * from the MSC and need to be forwarded to
211 * a real BSC.
212 */
213static void initialize_msc_if_needed()
214{
215 static int init = 0;
216 init = 1;
217
218 /* do we need to send a GSM 08.08 message here? */
219}
220
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100221static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100222{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100223 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800224 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100225 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100226
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100227 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800228 parsed = bsc_nat_parse(msg);
229 if (!parsed) {
230 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100231 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800232 }
233
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100234 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800235 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800236
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100237 /* Route and modify the SCCP packet */
238 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
239 switch (parsed->sccp_type) {
240 case SCCP_MSG_TYPE_UDT:
241 /* forward UDT messages to every BSC */
242 goto send_to_all;
243 break;
244 case SCCP_MSG_TYPE_RLSD:
245 case SCCP_MSG_TYPE_CREF:
246 case SCCP_MSG_TYPE_DT1:
247 case SCCP_MSG_TYPE_CC:
248 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
249 break;
250 case SCCP_MSG_TYPE_CR:
251 case SCCP_MSG_TYPE_RLC:
252 /* MSC never opens a SCCP connection, fall through */
253 default:
254 goto exit;
255 }
256 }
257
258 talloc_free(parsed);
259 if (!bsc)
260 return -1;
261 return write(bsc->bsc_fd.fd, msg->data, msg->len);
262
263send_to_all:
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100264 /* currently send this to every BSC connected */
265 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100266 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
267
268 /* try the next one */
269 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100270 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100271 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800272
273exit:
274 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100275 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100276}
277
278static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
279{
280 int error;
281 struct msgb *msg = ipaccess_read_msg(bfd, &error);
282 struct ipaccess_head *hh;
283
284 if (!msg) {
285 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100286 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100287 exit(-2);
288 }
289
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100290 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100291 return -1;
292 }
293
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100294 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 +0100295
296 /* handle base message handling */
297 hh = (struct ipaccess_head *) msg->data;
298 ipaccess_rcvmsg_base(msg, bfd);
299
300 /* initialize the networking. This includes sending a GSM08.08 message */
301 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
302 initialize_msc_if_needed();
303 else if (hh->proto == IPAC_PROTO_SCCP)
304 forward_sccp_to_bts(msg);
305
306 return 0;
307}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800308
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100309/*
310 * Below is the handling of messages coming
311 * from the BSC and need to be forwarded to
312 * a real BSC.
313 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100314
315/*
316 * Remove the connection from the connections list,
317 * remove it from the patching of SCCP header lists
318 * as well. Maybe in the future even close connection..
319 */
320static void remove_bsc_connection(struct bsc_connection *connection)
321{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100322 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100323 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100324 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100325
326 /* remove all SCCP connections */
327 llist_for_each_entry_safe(sccp_patch, tmp, &sccp_connections, list_entry) {
328 if (sccp_patch->bsc != connection)
329 continue;
330
331 llist_del(&sccp_patch->list_entry);
332 talloc_free(sccp_patch);
333 }
334
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100335 talloc_free(connection);
336}
337
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100338static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100339{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100340 struct bsc_connection *bsc;
341 struct bsc_connection *found_bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800342 struct bsc_nat_parsed *parsed;
343 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100344
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100345 bsc = bfd->data;
346
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800347 /* Parse and filter messages */
348 parsed = bsc_nat_parse(msg);
349 if (!parsed) {
350 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
351 return -1;
352 }
353
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100354 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800355 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800356
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100357 /* modify the SCCP entries */
358 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
359 switch (parsed->sccp_type) {
360 case SCCP_MSG_TYPE_CR:
361 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
362 goto exit2;
363 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
364 break;
365 case SCCP_MSG_TYPE_RLSD:
366 case SCCP_MSG_TYPE_CREF:
367 case SCCP_MSG_TYPE_DT1:
368 case SCCP_MSG_TYPE_CC:
369 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
370 break;
371 case SCCP_MSG_TYPE_RLC:
372 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
373 remove_sccp_src_ref(bsc, msg, parsed);
374 break;
375 case SCCP_MSG_TYPE_UDT:
376 /* simply forward everything */
377 break;
378 default:
379 goto exit2;
380 break;
381 }
382 }
383
384 if (found_bsc != bsc) {
385 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
386 goto exit2;
387 }
388
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100389 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800390 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100391 talloc_free(parsed);
392 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800393
394exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100395 /* if we filter out the reset send an ack to the BSC */
396 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
397 send_reset_ack(bfd);
398 send_reset_ack(bfd);
399 }
400
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100401exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800402 talloc_free(parsed);
403 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100404}
405
406static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
407{
408 int error;
409 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100410
411 if (!msg) {
412 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100413 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100414 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100415 } else {
416 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100417 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100418 return -1;
419 }
420
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100421
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100422 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 +0100423
424 /* Handle messages from the BSC */
425 /* FIXME: Currently no PONG is sent to the BSC */
426 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100427 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100428
429 return 0;
430}
431
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100432static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
433{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100434 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100435 int ret;
436 struct sockaddr_in sa;
437 socklen_t sa_len = sizeof(sa);
438
439 if (!(what & BSC_FD_READ))
440 return 0;
441
442 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
443 if (ret < 0) {
444 perror("accept");
445 return ret;
446 }
447
448 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100449 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100450
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100451 /*
452 *
453 */
454 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
455 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100456 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100457 close(ret);
458 return -1;
459 }
460
461 bsc->bsc_fd.data = bsc;
462 bsc->bsc_fd.fd = ret;
463 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100464 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100465 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100466 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100467 close(ret);
468 talloc_free(bsc);
469 return -2;
470 }
471
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100472 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100473 llist_add(&bsc->list_entry, &bsc_connections);
474 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100475 return 0;
476}
477
478static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
479{
480 struct sockaddr_in addr;
481 int ret, on = 1;
482
483 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
484 bfd->cb = ipaccess_listen_bsc_cb;
485 bfd->when = BSC_FD_READ;
486
487 memset(&addr, 0, sizeof(addr));
488 addr.sin_family = AF_INET;
489 addr.sin_port = htons(port);
490 addr.sin_addr.s_addr = in_addr->s_addr;
491
492 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
493
494 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
495 if (ret < 0) {
496 fprintf(stderr, "Could not bind the BSC socket %s\n",
497 strerror(errno));
498 return -EIO;
499 }
500
501 ret = listen(bfd->fd, 1);
502 if (ret < 0) {
503 perror("listen");
504 return ret;
505 }
506
507 ret = bsc_register_fd(bfd);
508 if (ret < 0) {
509 perror("register_listen_fd");
510 return ret;
511 }
512 return 0;
513}
514
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800515static void print_usage()
516{
517 printf("Usage: bsc_nat\n");
518}
519
520static void print_help()
521{
522 printf(" Some useful help...\n");
523 printf(" -h --help this text\n");
524 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
525 printf(" -s --disable-color\n");
526 printf(" -c --config-file filename The config file to use.\n");
527 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100528 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800529}
530
531static void handle_options(int argc, char** argv)
532{
533 while (1) {
534 int option_index = 0, c;
535 static struct option long_options[] = {
536 {"help", 0, 0, 'h'},
537 {"debug", 1, 0, 'd'},
538 {"config-file", 1, 0, 'c'},
539 {"disable-color", 0, 0, 's'},
540 {"timestamp", 0, 0, 'T'},
541 {"msc", 1, 0, 'm'},
542 {"local", 1, 0, 'l'},
543 {0, 0, 0, 0}
544 };
545
546 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
547 long_options, &option_index);
548 if (c == -1)
549 break;
550
551 switch (c) {
552 case 'h':
553 print_usage();
554 print_help();
555 exit(0);
556 case 's':
557 debug_use_color(0);
558 break;
559 case 'd':
560 debug_parse_category_mask(optarg);
561 break;
562 case 'c':
563 config_file = strdup(optarg);
564 break;
565 case 'T':
566 debug_timestamp(1);
567 break;
568 case 'm':
569 msc_address = strdup(optarg);
570 break;
571 case 'l':
572 inet_aton(optarg, &local_addr);
573 break;
574 default:
575 /* ignore */
576 break;
577 }
578 }
579}
580
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100581static void signal_handler(int signal)
582{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100583 switch (signal) {
584 case SIGABRT:
585 /* in case of abort, we want to obtain a talloc report
586 * and then return to the caller, who will abort the process */
587 case SIGUSR1:
588 talloc_report_full(tall_bsc_ctx, stderr);
589 break;
590 default:
591 break;
592 }
593}
594
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800595int main(int argc, char** argv)
596{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100597 int rc;
598
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800599 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100600 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800601 handle_options(argc, argv);
602
603 /* seed the PRNG */
604 srand(time(NULL));
605
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100606 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100607 msc_connection.cb = ipaccess_msc_cb;
608 rc = connect_to_msc(&msc_connection, msc_address, 5000);
609 if (rc < 0) {
610 fprintf(stderr, "Opening the MSC connection failed.\n");
611 exit(1);
612 }
613
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100614 /* wait for the BSC */
615 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
616 fprintf(stderr, "Failed to listen for BSC.\n");
617 exit(1);
618 }
619
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100620 signal(SIGINT, &signal_handler);
621 signal(SIGABRT, &signal_handler);
622 signal(SIGUSR1, &signal_handler);
623 signal(SIGPIPE, SIG_IGN);
624
625 while (1) {
626 bsc_select_main(0);
627 }
628
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800629 return 0;
630}