blob: 9242b55be761b5ce3ab950c53fc28659f15d3bed [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
57/*
58 * Per BSC data structure
59 */
60struct bsc_connection {
61 struct llist_head list_entry;
62
63 /* do we know anything about this BSC? */
64 int authenticated;
65
66 /* the fd we use to communicate */
67 struct bsc_fd bsc_fd;
68};
69
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +010070/*
71 * Per SCCP source local reference patch table. It needs to
72 * be updated on new SCCP connections, connection confirm and reject,
73 * and on the loss of the BSC connection.
74 */
75struct sccp_connections {
76 struct llist_head list_entry;
77
78 struct bsc_connection *bsc;
79
80 struct sccp_source_reference real_ref;
81 struct sccp_source_reference patched_ref;
82};
83
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010084static LLIST_HEAD(bsc_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +010085static LLIST_HEAD(sccp_connections);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010086
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080087
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010088/*
89 * below are stubs we need to link
90 */
91int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
92 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
93{
94 return -1;
95}
96
97void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
98{}
99
100int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
101{
102 return -1;
103}
104
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100105static int send_reset_ack(struct bsc_fd *bfd)
106{
107 static const u_int8_t gsm_reset_ack[] = {
108 0x00, 0x13, 0xfd,
109 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
110 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
111 0x00, 0x01, 0x31,
112 };
113
114 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
115}
116
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100117/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100118 * SCCP patching below
119 */
120
121/* check if we are using this ref for patched already */
122static int sccp_ref_is_free(struct sccp_source_reference *ref)
123{
124 struct sccp_connections *conn;
125
126 llist_for_each_entry(conn, &sccp_connections, list_entry) {
127 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
128 return -1;
129 }
130
131 return 0;
132}
133
134/* copied from sccp.c */
135static int assign_src_local_reference(struct sccp_source_reference *ref)
136{
137 static u_int32_t last_ref = 0x50000;
138 int wrapped = 0;
139
140 do {
141 struct sccp_source_reference reference;
142 reference.octet1 = (last_ref >> 0) & 0xff;
143 reference.octet2 = (last_ref >> 8) & 0xff;
144 reference.octet3 = (last_ref >> 16) & 0xff;
145
146 ++last_ref;
147 /* do not use the reversed word and wrap around */
148 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
149 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
150 last_ref = 0;
151 ++wrapped;
152 }
153
154 if (sccp_ref_is_free(&reference) == 0) {
155 *ref = reference;
156 return 0;
157 }
158 } while (wrapped != 2);
159
160 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
161 return -1;
162}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100163
164static 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 +0100165{
166 struct sccp_connections *conn;
167
168 conn = talloc_zero(tall_bsc_ctx, struct sccp_connections);
169 if (!conn) {
170 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
171 return -1;
172 }
173
174 conn->real_ref = *parsed->src_local_ref;
175 if (assign_src_local_reference(&conn->patched_ref) != 0) {
176 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
177 talloc_free(conn);
178 return -1;
179 }
180
181 return 0;
182}
183
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100184static 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 +0100185{
186 struct sccp_connections *conn;
187
188 llist_for_each_entry(conn, &sccp_connections, list_entry) {
189 if (memcmp(parsed->src_local_ref,
190 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
191 if (bsc != conn->bsc) {
192 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
193 continue;
194 }
195
196
197 llist_del(&conn->list_entry);
198 talloc_free(conn);
199 return;
200 }
201 }
202
203 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
204}
205
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100206static 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 +0100207{
208 struct sccp_connections *conn;
209 llist_for_each_entry(conn, &sccp_connections, list_entry) {
210 if (memcmp(parsed->dest_local_ref,
211 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
212 memcpy(parsed->dest_local_ref,
213 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
214 return conn->bsc;
215 }
216 }
217
218 return NULL;
219}
220
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100221static 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 +0100222{
223 struct sccp_connections *conn;
224 llist_for_each_entry(conn, &sccp_connections, list_entry) {
225 if (memcmp(parsed->src_local_ref,
226 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
227 memcpy(parsed->src_local_ref,
228 &conn->patched_ref, sizeof(*parsed->src_local_ref));
229 return conn->bsc;
230 }
231 }
232
233 return NULL;
234}
235
236/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100237 * Below is the handling of messages coming
238 * from the MSC and need to be forwarded to
239 * a real BSC.
240 */
241static void initialize_msc_if_needed()
242{
243 static int init = 0;
244 init = 1;
245
246 /* do we need to send a GSM 08.08 message here? */
247}
248
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100249static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100250{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100251 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800252 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100253 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100254
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100255 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800256 parsed = bsc_nat_parse(msg);
257 if (!parsed) {
258 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100259 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800260 }
261
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100262 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800263 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800264
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100265 /* Route and modify the SCCP packet */
266 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
267 switch (parsed->sccp_type) {
268 case SCCP_MSG_TYPE_UDT:
269 /* forward UDT messages to every BSC */
270 goto send_to_all;
271 break;
272 case SCCP_MSG_TYPE_RLSD:
273 case SCCP_MSG_TYPE_CREF:
274 case SCCP_MSG_TYPE_DT1:
275 case SCCP_MSG_TYPE_CC:
276 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
277 break;
278 case SCCP_MSG_TYPE_CR:
279 case SCCP_MSG_TYPE_RLC:
280 /* MSC never opens a SCCP connection, fall through */
281 default:
282 goto exit;
283 }
284 }
285
286 talloc_free(parsed);
287 if (!bsc)
288 return -1;
289 return write(bsc->bsc_fd.fd, msg->data, msg->len);
290
291send_to_all:
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100292 /* currently send this to every BSC connected */
293 llist_for_each_entry(bsc, &bsc_connections, list_entry) {
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100294 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
295
296 /* try the next one */
297 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100298 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100299 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800300
301exit:
302 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100303 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100304}
305
306static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
307{
308 int error;
309 struct msgb *msg = ipaccess_read_msg(bfd, &error);
310 struct ipaccess_head *hh;
311
312 if (!msg) {
313 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100314 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100315 exit(-2);
316 }
317
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100318 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100319 return -1;
320 }
321
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100322 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 +0100323
324 /* handle base message handling */
325 hh = (struct ipaccess_head *) msg->data;
326 ipaccess_rcvmsg_base(msg, bfd);
327
328 /* initialize the networking. This includes sending a GSM08.08 message */
329 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
330 initialize_msc_if_needed();
331 else if (hh->proto == IPAC_PROTO_SCCP)
332 forward_sccp_to_bts(msg);
333
334 return 0;
335}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800336
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100337/*
338 * Below is the handling of messages coming
339 * from the BSC and need to be forwarded to
340 * a real BSC.
341 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100342
343/*
344 * Remove the connection from the connections list,
345 * remove it from the patching of SCCP header lists
346 * as well. Maybe in the future even close connection..
347 */
348static void remove_bsc_connection(struct bsc_connection *connection)
349{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100350 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100351 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100352 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100353
354 /* remove all SCCP connections */
355 llist_for_each_entry_safe(sccp_patch, tmp, &sccp_connections, list_entry) {
356 if (sccp_patch->bsc != connection)
357 continue;
358
359 llist_del(&sccp_patch->list_entry);
360 talloc_free(sccp_patch);
361 }
362
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100363 talloc_free(connection);
364}
365
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100366static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100367{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100368 struct bsc_connection *bsc;
369 struct bsc_connection *found_bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800370 struct bsc_nat_parsed *parsed;
371 int rc = -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100372
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100373 bsc = bfd->data;
374
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800375 /* Parse and filter messages */
376 parsed = bsc_nat_parse(msg);
377 if (!parsed) {
378 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
379 return -1;
380 }
381
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100382 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800383 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800384
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100385 /* modify the SCCP entries */
386 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
387 switch (parsed->sccp_type) {
388 case SCCP_MSG_TYPE_CR:
389 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
390 goto exit2;
391 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
392 break;
393 case SCCP_MSG_TYPE_RLSD:
394 case SCCP_MSG_TYPE_CREF:
395 case SCCP_MSG_TYPE_DT1:
396 case SCCP_MSG_TYPE_CC:
397 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
398 break;
399 case SCCP_MSG_TYPE_RLC:
400 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
401 remove_sccp_src_ref(bsc, msg, parsed);
402 break;
403 case SCCP_MSG_TYPE_UDT:
404 /* simply forward everything */
405 break;
406 default:
407 goto exit2;
408 break;
409 }
410 }
411
412 if (found_bsc != bsc) {
413 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
414 goto exit2;
415 }
416
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100417 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800418 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100419 talloc_free(parsed);
420 return rc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800421
422exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100423 /* if we filter out the reset send an ack to the BSC */
424 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
425 send_reset_ack(bfd);
426 send_reset_ack(bfd);
427 }
428
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100429exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800430 talloc_free(parsed);
431 return rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100432}
433
434static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
435{
436 int error;
437 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100438
439 if (!msg) {
440 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100441 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100442 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100443 } else {
444 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100445 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100446 return -1;
447 }
448
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100449
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100450 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 +0100451
452 /* Handle messages from the BSC */
453 /* FIXME: Currently no PONG is sent to the BSC */
454 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100455 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100456
457 return 0;
458}
459
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100460static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
461{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100462 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100463 int ret;
464 struct sockaddr_in sa;
465 socklen_t sa_len = sizeof(sa);
466
467 if (!(what & BSC_FD_READ))
468 return 0;
469
470 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
471 if (ret < 0) {
472 perror("accept");
473 return ret;
474 }
475
476 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100477 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100478
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100479 /*
480 *
481 */
482 bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
483 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100484 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100485 close(ret);
486 return -1;
487 }
488
489 bsc->bsc_fd.data = bsc;
490 bsc->bsc_fd.fd = ret;
491 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100492 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100493 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100494 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100495 close(ret);
496 talloc_free(bsc);
497 return -2;
498 }
499
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100500 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100501 llist_add(&bsc->list_entry, &bsc_connections);
502 ipaccess_send_id_ack(ret);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100503 return 0;
504}
505
506static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
507{
508 struct sockaddr_in addr;
509 int ret, on = 1;
510
511 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
512 bfd->cb = ipaccess_listen_bsc_cb;
513 bfd->when = BSC_FD_READ;
514
515 memset(&addr, 0, sizeof(addr));
516 addr.sin_family = AF_INET;
517 addr.sin_port = htons(port);
518 addr.sin_addr.s_addr = in_addr->s_addr;
519
520 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
521
522 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
523 if (ret < 0) {
524 fprintf(stderr, "Could not bind the BSC socket %s\n",
525 strerror(errno));
526 return -EIO;
527 }
528
529 ret = listen(bfd->fd, 1);
530 if (ret < 0) {
531 perror("listen");
532 return ret;
533 }
534
535 ret = bsc_register_fd(bfd);
536 if (ret < 0) {
537 perror("register_listen_fd");
538 return ret;
539 }
540 return 0;
541}
542
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800543static void print_usage()
544{
545 printf("Usage: bsc_nat\n");
546}
547
548static void print_help()
549{
550 printf(" Some useful help...\n");
551 printf(" -h --help this text\n");
552 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
553 printf(" -s --disable-color\n");
554 printf(" -c --config-file filename The config file to use.\n");
555 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100556 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800557}
558
559static void handle_options(int argc, char** argv)
560{
561 while (1) {
562 int option_index = 0, c;
563 static struct option long_options[] = {
564 {"help", 0, 0, 'h'},
565 {"debug", 1, 0, 'd'},
566 {"config-file", 1, 0, 'c'},
567 {"disable-color", 0, 0, 's'},
568 {"timestamp", 0, 0, 'T'},
569 {"msc", 1, 0, 'm'},
570 {"local", 1, 0, 'l'},
571 {0, 0, 0, 0}
572 };
573
574 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
575 long_options, &option_index);
576 if (c == -1)
577 break;
578
579 switch (c) {
580 case 'h':
581 print_usage();
582 print_help();
583 exit(0);
584 case 's':
585 debug_use_color(0);
586 break;
587 case 'd':
588 debug_parse_category_mask(optarg);
589 break;
590 case 'c':
591 config_file = strdup(optarg);
592 break;
593 case 'T':
594 debug_timestamp(1);
595 break;
596 case 'm':
597 msc_address = strdup(optarg);
598 break;
599 case 'l':
600 inet_aton(optarg, &local_addr);
601 break;
602 default:
603 /* ignore */
604 break;
605 }
606 }
607}
608
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100609static void signal_handler(int signal)
610{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100611 switch (signal) {
612 case SIGABRT:
613 /* in case of abort, we want to obtain a talloc report
614 * and then return to the caller, who will abort the process */
615 case SIGUSR1:
616 talloc_report_full(tall_bsc_ctx, stderr);
617 break;
618 default:
619 break;
620 }
621}
622
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800623int main(int argc, char** argv)
624{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100625 int rc;
626
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800627 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100628 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800629 handle_options(argc, argv);
630
631 /* seed the PRNG */
632 srand(time(NULL));
633
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100634 /* connect to the MSC */
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100635 msc_connection.cb = ipaccess_msc_cb;
636 rc = connect_to_msc(&msc_connection, msc_address, 5000);
637 if (rc < 0) {
638 fprintf(stderr, "Opening the MSC connection failed.\n");
639 exit(1);
640 }
641
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100642 /* wait for the BSC */
643 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
644 fprintf(stderr, "Failed to listen for BSC.\n");
645 exit(1);
646 }
647
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100648 signal(SIGINT, &signal_handler);
649 signal(SIGABRT, &signal_handler);
650 signal(SIGUSR1, &signal_handler);
651 signal(SIGPIPE, SIG_IGN);
652
653 while (1) {
654 bsc_select_main(0);
655 }
656
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800657 return 0;
658}