blob: 63263fa9d05797548d39a7bdb1a3f3fa53cd4e7c [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>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
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/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010041#include <openbsc/bssap.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080044#include <openbsc/telnet_interface.h>
45
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080046#include <osmocore/talloc.h>
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +080047#include <osmocore/write_queue.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080048
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080049#include <vty/vty.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080050
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080051#include <sccp/sccp.h>
52
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080053struct debug_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080054static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080055static char *msc_address = "127.0.0.1";
56static struct in_addr local_addr;
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +080057static struct write_queue msc_queue;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010058static struct bsc_fd bsc_listen;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010059
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010060
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080061static struct bsc_nat *nat;
62
63static struct bsc_nat *bsc_nat_alloc(void)
64{
65 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
66 if (!nat)
67 return NULL;
68
69 INIT_LLIST_HEAD(&nat->sccp_connections);
70 INIT_LLIST_HEAD(&nat->bsc_connections);
71 INIT_LLIST_HEAD(&nat->bsc_configs);
72 return nat;
73}
74
75static struct bsc_connection *bsc_connection_alloc(void)
76{
77 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
78 if (!con)
79 return NULL;
80
81 return con;
82}
83
84struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
85{
86 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
87 if (!conf)
88 return NULL;
89
90 conf->token = talloc_strdup(conf, token);
91 conf->lac = lac;
92 conf->nr = nat->num_bsc;
93 conf->nat = nat;
94
95 llist_add(&conf->entry, &nat->bsc_configs);
96 ++nat->num_bsc;
97
98 return conf;
99}
100
101struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
102{
103 struct bsc_config *conf;
104
105 llist_for_each_entry(conf, &nat->bsc_configs, entry)
106 if (conf->nr == num)
107 return conf;
108
109 return NULL;
110}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100111
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100112/*
113 * below are stubs we need to link
114 */
115int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
116 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
117{
118 return -1;
119}
120
121void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
122{}
123
124int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
125{
126 return -1;
127}
128
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100129static int send_reset_ack(struct bsc_fd *bfd)
130{
131 static const u_int8_t gsm_reset_ack[] = {
132 0x00, 0x13, 0xfd,
133 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
134 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
135 0x00, 0x01, 0x31,
136 };
137
138 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
139}
140
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100141/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100142 * SCCP patching below
143 */
144
145/* check if we are using this ref for patched already */
146static int sccp_ref_is_free(struct sccp_source_reference *ref)
147{
148 struct sccp_connections *conn;
149
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800150 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100151 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
152 return -1;
153 }
154
155 return 0;
156}
157
158/* copied from sccp.c */
159static int assign_src_local_reference(struct sccp_source_reference *ref)
160{
161 static u_int32_t last_ref = 0x50000;
162 int wrapped = 0;
163
164 do {
165 struct sccp_source_reference reference;
166 reference.octet1 = (last_ref >> 0) & 0xff;
167 reference.octet2 = (last_ref >> 8) & 0xff;
168 reference.octet3 = (last_ref >> 16) & 0xff;
169
170 ++last_ref;
171 /* do not use the reversed word and wrap around */
172 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
173 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
174 last_ref = 0;
175 ++wrapped;
176 }
177
178 if (sccp_ref_is_free(&reference) == 0) {
179 *ref = reference;
180 return 0;
181 }
182 } while (wrapped != 2);
183
184 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
185 return -1;
186}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100187
188static 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 +0100189{
190 struct sccp_connections *conn;
191
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800192 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100193 if (!conn) {
194 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
195 return -1;
196 }
197
198 conn->real_ref = *parsed->src_local_ref;
199 if (assign_src_local_reference(&conn->patched_ref) != 0) {
200 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
201 talloc_free(conn);
202 return -1;
203 }
204
205 return 0;
206}
207
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100208static 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 +0100209{
210 struct sccp_connections *conn;
211
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800212 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100213 if (memcmp(parsed->src_local_ref,
214 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
215 if (bsc != conn->bsc) {
216 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
217 continue;
218 }
219
220
221 llist_del(&conn->list_entry);
222 talloc_free(conn);
223 return;
224 }
225 }
226
227 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
228}
229
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100230static 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 +0100231{
232 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800233 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100234 if (memcmp(parsed->dest_local_ref,
235 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
236 memcpy(parsed->dest_local_ref,
237 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
238 return conn->bsc;
239 }
240 }
241
242 return NULL;
243}
244
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100245static 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 +0100246{
247 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800248 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100249 if (memcmp(parsed->src_local_ref,
250 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
251 memcpy(parsed->src_local_ref,
252 &conn->patched_ref, sizeof(*parsed->src_local_ref));
253 return conn->bsc;
254 }
255 }
256
257 return NULL;
258}
259
260/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100261 * Below is the handling of messages coming
262 * from the MSC and need to be forwarded to
263 * a real BSC.
264 */
265static void initialize_msc_if_needed()
266{
267 static int init = 0;
268 init = 1;
269
270 /* do we need to send a GSM 08.08 message here? */
271}
272
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100273static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100274{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800275 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800276 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100277 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100278
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100279 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800280 parsed = bsc_nat_parse(msg);
281 if (!parsed) {
282 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100283 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800284 }
285
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100286 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800287 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800288
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100289 /* Route and modify the SCCP packet */
290 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
291 switch (parsed->sccp_type) {
292 case SCCP_MSG_TYPE_UDT:
293 /* forward UDT messages to every BSC */
294 goto send_to_all;
295 break;
296 case SCCP_MSG_TYPE_RLSD:
297 case SCCP_MSG_TYPE_CREF:
298 case SCCP_MSG_TYPE_DT1:
299 case SCCP_MSG_TYPE_CC:
300 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
301 break;
302 case SCCP_MSG_TYPE_CR:
303 case SCCP_MSG_TYPE_RLC:
304 /* MSC never opens a SCCP connection, fall through */
305 default:
306 goto exit;
307 }
308 }
309
310 talloc_free(parsed);
311 if (!bsc)
312 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100313 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800314 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100315 return -1;
316 }
317
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100318 return write(bsc->bsc_fd.fd, msg->data, msg->len);
319
320send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800321 /*
322 * Filter Paging from the network. We do not want to send a PAGING
323 * Command to every BSC in our network. We will analys the PAGING
324 * message and then send it to the authenticated messages...
325 */
326 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
327 int data_length;
328 const u_int8_t *data;
329 struct tlv_parsed tp;
330 int i = 0;
331
332 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
333 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
334 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
335 goto exit;
336 }
337
338 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
339 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
340 if (data[0] != CELL_IDENT_LAC) {
341 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
342 goto exit;
343 }
344
345 /* go through each LAC and forward the message */
346 for (i = 1; i < data_length - 1; i += 2) {
347 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
348 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
349 if (!bsc->authenticated || _lac != bsc->lac)
350 continue;
351
352 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
353 if (rc < msg->len)
354 LOGP(DNAT, LOGL_ERROR,
355 "Failed to write message to BTS: %d\n", rc);
356 }
357 }
358
359 goto exit;
360 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100361 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800362 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100363 if (!bsc->authenticated)
364 continue;
365
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100366 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
367
368 /* try the next one */
369 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100370 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100371 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800372
373exit:
374 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100375 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100376}
377
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800378static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100379{
380 int error;
381 struct msgb *msg = ipaccess_read_msg(bfd, &error);
382 struct ipaccess_head *hh;
383
384 if (!msg) {
385 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100386 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100387 exit(-2);
388 }
389
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100390 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100391 return -1;
392 }
393
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100394 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 +0100395
396 /* handle base message handling */
397 hh = (struct ipaccess_head *) msg->data;
398 ipaccess_rcvmsg_base(msg, bfd);
399
400 /* initialize the networking. This includes sending a GSM08.08 message */
401 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
402 initialize_msc_if_needed();
403 else if (hh->proto == IPAC_PROTO_SCCP)
404 forward_sccp_to_bts(msg);
405
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800406 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100407 return 0;
408}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800409
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800410static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
411{
412 int rc;
413 rc = write(bfd->fd, msg->data, msg->len);
414
415 if (rc != msg->len) {
416 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
417 return -1;
418 }
419
420 return rc;
421}
422
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100423/*
424 * Below is the handling of messages coming
425 * from the BSC and need to be forwarded to
426 * a real BSC.
427 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100428
429/*
430 * Remove the connection from the connections list,
431 * remove it from the patching of SCCP header lists
432 * as well. Maybe in the future even close connection..
433 */
434static void remove_bsc_connection(struct bsc_connection *connection)
435{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100436 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther76255062010-01-13 09:51:23 +0100437 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800438 close(connection->bsc_fd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100439 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100440
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800441 /* stop the timeout timer */
442 bsc_del_timer(&connection->id_timeout);
443
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100444 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800445 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100446 if (sccp_patch->bsc != connection)
447 continue;
448
449 llist_del(&sccp_patch->list_entry);
450 talloc_free(sccp_patch);
451 }
452
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100453 talloc_free(connection);
454}
455
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800456static void ipaccess_close_bsc(void *data)
457{
458 struct bsc_connection *conn = data;
459
460 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
461 remove_bsc_connection(conn);
462}
463
464static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
465{
466 struct bsc_config *conf;
467 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
468
469 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
470 if (strcmp(conf->token, token) == 0) {
471 bsc->authenticated = 1;
472 bsc->lac = conf->lac;
473 bsc_del_timer(&bsc->id_timeout);
474 break;
475 }
476 }
477}
478
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100479static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100480{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100481 struct bsc_connection *bsc;
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100482 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800483 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100484
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100485 bsc = bfd->data;
486
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800487 /* Parse and filter messages */
488 parsed = bsc_nat_parse(msg);
489 if (!parsed) {
490 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
491 return -1;
492 }
493
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100494 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800495 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800496
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100497 /* modify the SCCP entries */
498 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
499 switch (parsed->sccp_type) {
500 case SCCP_MSG_TYPE_CR:
501 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
502 goto exit2;
503 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
504 break;
505 case SCCP_MSG_TYPE_RLSD:
506 case SCCP_MSG_TYPE_CREF:
507 case SCCP_MSG_TYPE_DT1:
508 case SCCP_MSG_TYPE_CC:
509 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
510 break;
511 case SCCP_MSG_TYPE_RLC:
512 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
513 remove_sccp_src_ref(bsc, msg, parsed);
514 break;
515 case SCCP_MSG_TYPE_UDT:
516 /* simply forward everything */
517 break;
518 default:
519 goto exit2;
520 break;
521 }
522 }
523
524 if (found_bsc != bsc) {
525 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
526 goto exit2;
527 }
528
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100529 if (!bsc->authenticated) {
530 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
531 goto exit2;
532 }
533
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100534 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800535 if (write_queue_enqueue(&msc_queue, msg) != 0) {
536 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
537 msgb_free(msg);
538 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100539 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800540 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800541
542exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100543 /* if we filter out the reset send an ack to the BSC */
544 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
545 send_reset_ack(bfd);
546 send_reset_ack(bfd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800547 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
548 /* do we know who is handling this? */
549 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
550 struct tlv_parsed tvp;
551 ipaccess_idtag_parse(&tvp,
552 (unsigned char *) msg->l2h + 2,
553 msgb_l2len(msg) - 2);
554 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
555 ipaccess_auth_bsc(&tvp, bsc);
556 }
557
558 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100559 }
560
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100561exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800562 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800563 msgb_free(msg);
564 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100565}
566
567static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
568{
569 int error;
570 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100571
572 if (!msg) {
573 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100574 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100575 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100576 } else {
577 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100578 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100579 return -1;
580 }
581
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100582
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100583 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 +0100584
585 /* Handle messages from the BSC */
586 /* FIXME: Currently no PONG is sent to the BSC */
587 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100588 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100589
590 return 0;
591}
592
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100593static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
594{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100595 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100596 int ret;
597 struct sockaddr_in sa;
598 socklen_t sa_len = sizeof(sa);
599
600 if (!(what & BSC_FD_READ))
601 return 0;
602
603 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
604 if (ret < 0) {
605 perror("accept");
606 return ret;
607 }
608
609 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100610 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100611
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100612 /*
613 *
614 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800615 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100616 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100617 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100618 close(ret);
619 return -1;
620 }
621
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800622 bsc->nat = nat;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100623 bsc->bsc_fd.data = bsc;
624 bsc->bsc_fd.fd = ret;
625 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freytherc7641c92010-01-13 09:52:29 +0100626 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100627 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100628 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100629 close(ret);
630 talloc_free(bsc);
631 return -2;
632 }
633
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100634 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800635 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800636 ipaccess_send_id_ack(bsc->bsc_fd.fd);
637 ipaccess_send_id_req(ret);
638
639 /*
640 * start the hangup timer
641 */
642 bsc->id_timeout.data = bsc;
643 bsc->id_timeout.cb = ipaccess_close_bsc;
644 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100645 return 0;
646}
647
648static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
649{
650 struct sockaddr_in addr;
651 int ret, on = 1;
652
653 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
654 bfd->cb = ipaccess_listen_bsc_cb;
655 bfd->when = BSC_FD_READ;
656
657 memset(&addr, 0, sizeof(addr));
658 addr.sin_family = AF_INET;
659 addr.sin_port = htons(port);
660 addr.sin_addr.s_addr = in_addr->s_addr;
661
662 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
663
664 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
665 if (ret < 0) {
666 fprintf(stderr, "Could not bind the BSC socket %s\n",
667 strerror(errno));
668 return -EIO;
669 }
670
671 ret = listen(bfd->fd, 1);
672 if (ret < 0) {
673 perror("listen");
674 return ret;
675 }
676
677 ret = bsc_register_fd(bfd);
678 if (ret < 0) {
679 perror("register_listen_fd");
680 return ret;
681 }
682 return 0;
683}
684
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800685static void print_usage()
686{
687 printf("Usage: bsc_nat\n");
688}
689
690static void print_help()
691{
692 printf(" Some useful help...\n");
693 printf(" -h --help this text\n");
694 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
695 printf(" -s --disable-color\n");
696 printf(" -c --config-file filename The config file to use.\n");
697 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100698 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800699}
700
701static void handle_options(int argc, char** argv)
702{
703 while (1) {
704 int option_index = 0, c;
705 static struct option long_options[] = {
706 {"help", 0, 0, 'h'},
707 {"debug", 1, 0, 'd'},
708 {"config-file", 1, 0, 'c'},
709 {"disable-color", 0, 0, 's'},
710 {"timestamp", 0, 0, 'T'},
711 {"msc", 1, 0, 'm'},
712 {"local", 1, 0, 'l'},
713 {0, 0, 0, 0}
714 };
715
716 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
717 long_options, &option_index);
718 if (c == -1)
719 break;
720
721 switch (c) {
722 case 'h':
723 print_usage();
724 print_help();
725 exit(0);
726 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800727 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800728 break;
729 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800730 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800731 break;
732 case 'c':
733 config_file = strdup(optarg);
734 break;
735 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800736 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800737 break;
738 case 'm':
739 msc_address = strdup(optarg);
740 break;
741 case 'l':
742 inet_aton(optarg, &local_addr);
743 break;
744 default:
745 /* ignore */
746 break;
747 }
748 }
749}
750
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100751static void signal_handler(int signal)
752{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100753 switch (signal) {
754 case SIGABRT:
755 /* in case of abort, we want to obtain a talloc report
756 * and then return to the caller, who will abort the process */
757 case SIGUSR1:
758 talloc_report_full(tall_bsc_ctx, stderr);
759 break;
760 default:
761 break;
762 }
763}
764
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800765int main(int argc, char** argv)
766{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100767 int rc;
768
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800769 debug_init();
770 stderr_target = debug_target_create_stderr();
771 debug_add_target(stderr_target);
772 debug_set_all_filter(stderr_target, 1);
773
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800774 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100775 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800776 handle_options(argc, argv);
777
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800778 nat = bsc_nat_alloc();
779 if (!nat) {
780 fprintf(stderr, "Failed to allocate the BSC nat.\n");
781 return -4;
782 }
783
784 /* init vty and parse */
785 bsc_nat_vty_init(nat);
786 telnet_init(NULL, 4244);
787 if (vty_read_config_file(config_file) < 0) {
788 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
789 return -3;
790 }
791
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800792 /* seed the PRNG */
793 srand(time(NULL));
794
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100795 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800796 write_queue_init(&msc_queue, 100);
797 msc_queue.read_cb = ipaccess_msc_read_cb;
798 msc_queue.write_cb = ipaccess_msc_write_cb;
799 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100800 if (rc < 0) {
801 fprintf(stderr, "Opening the MSC connection failed.\n");
802 exit(1);
803 }
804
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100805 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100806 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100807 fprintf(stderr, "Failed to listen for BSC.\n");
808 exit(1);
809 }
810
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100811 signal(SIGABRT, &signal_handler);
812 signal(SIGUSR1, &signal_handler);
813 signal(SIGPIPE, SIG_IGN);
814
815 while (1) {
816 bsc_select_main(0);
817 }
818
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800819 return 0;
820}