blob: 1efc888349a18d94d5865b295843bd0347213f0c [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>
47
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080048#include <vty/vty.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080049
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080050#include <sccp/sccp.h>
51
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080052struct debug_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080053static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080054static char *msc_address = "127.0.0.1";
55static struct in_addr local_addr;
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +080056static struct write_queue msc_queue;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010057static struct bsc_fd bsc_listen;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010058
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010059
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080060static struct bsc_nat *nat;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +010061static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080062
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 Freyther747d6542010-03-26 07:24:34 +0100129static int send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100130{
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
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100138 return bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100139}
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 Freyther747d6542010-03-26 07:24:34 +0100273static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100274{
275 return write(bsc->write_queue.bfd.fd, data, length);
276}
277
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100278static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100279{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800280 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800281 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100282 int rc;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100283
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100284 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800285 parsed = bsc_nat_parse(msg);
286 if (!parsed) {
287 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100288 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800289 }
290
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100291 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800292 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800293
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100294 /* Route and modify the SCCP packet */
295 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
296 switch (parsed->sccp_type) {
297 case SCCP_MSG_TYPE_UDT:
298 /* forward UDT messages to every BSC */
299 goto send_to_all;
300 break;
301 case SCCP_MSG_TYPE_RLSD:
302 case SCCP_MSG_TYPE_CREF:
303 case SCCP_MSG_TYPE_DT1:
304 case SCCP_MSG_TYPE_CC:
305 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
306 break;
307 case SCCP_MSG_TYPE_CR:
308 case SCCP_MSG_TYPE_RLC:
309 /* MSC never opens a SCCP connection, fall through */
310 default:
311 goto exit;
312 }
313 }
314
315 talloc_free(parsed);
316 if (!bsc)
317 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100318 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800319 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100320 return -1;
321 }
322
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100323 return bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100324
325send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800326 /*
327 * Filter Paging from the network. We do not want to send a PAGING
328 * Command to every BSC in our network. We will analys the PAGING
329 * message and then send it to the authenticated messages...
330 */
331 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
332 int data_length;
333 const u_int8_t *data;
334 struct tlv_parsed tp;
335 int i = 0;
336
337 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
338 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
339 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
340 goto exit;
341 }
342
343 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
344 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
345 if (data[0] != CELL_IDENT_LAC) {
346 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
347 goto exit;
348 }
349
350 /* go through each LAC and forward the message */
351 for (i = 1; i < data_length - 1; i += 2) {
352 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
353 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
354 if (!bsc->authenticated || _lac != bsc->lac)
355 continue;
356
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100357 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800358 if (rc < msg->len)
359 LOGP(DNAT, LOGL_ERROR,
360 "Failed to write message to BTS: %d\n", rc);
361 }
362 }
363
364 goto exit;
365 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100366 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800367 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100368 if (!bsc->authenticated)
369 continue;
370
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100371 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther60046642010-01-25 10:01:30 +0100372
373 /* try the next one */
374 if (rc < msg->len)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100375 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100376 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800377
378exit:
379 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100380 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100381}
382
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800383static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100384{
385 int error;
386 struct msgb *msg = ipaccess_read_msg(bfd, &error);
387 struct ipaccess_head *hh;
388
389 if (!msg) {
390 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100391 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100392 exit(-2);
393 }
394
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100395 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100396 return -1;
397 }
398
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100399 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 +0100400
401 /* handle base message handling */
402 hh = (struct ipaccess_head *) msg->data;
403 ipaccess_rcvmsg_base(msg, bfd);
404
405 /* initialize the networking. This includes sending a GSM08.08 message */
406 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
407 initialize_msc_if_needed();
408 else if (hh->proto == IPAC_PROTO_SCCP)
409 forward_sccp_to_bts(msg);
410
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800411 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100412 return 0;
413}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800414
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800415static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
416{
417 int rc;
418 rc = write(bfd->fd, msg->data, msg->len);
419
420 if (rc != msg->len) {
421 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
422 return -1;
423 }
424
425 return rc;
426}
427
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100428/*
429 * Below is the handling of messages coming
430 * from the BSC and need to be forwarded to
431 * a real BSC.
432 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100433
434/*
435 * Remove the connection from the connections list,
436 * remove it from the patching of SCCP header lists
437 * as well. Maybe in the future even close connection..
438 */
439static void remove_bsc_connection(struct bsc_connection *connection)
440{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100441 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800442 bsc_unregister_fd(&connection->write_queue.bfd);
443 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100444 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100445
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800446 /* stop the timeout timer */
447 bsc_del_timer(&connection->id_timeout);
448
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100449 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800450 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100451 if (sccp_patch->bsc != connection)
452 continue;
453
454 llist_del(&sccp_patch->list_entry);
455 talloc_free(sccp_patch);
456 }
457
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100458 talloc_free(connection);
459}
460
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800461static void ipaccess_close_bsc(void *data)
462{
463 struct bsc_connection *conn = data;
464
465 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
466 remove_bsc_connection(conn);
467}
468
469static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
470{
471 struct bsc_config *conf;
472 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
473
474 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
475 if (strcmp(conf->token, token) == 0) {
476 bsc->authenticated = 1;
477 bsc->lac = conf->lac;
478 bsc_del_timer(&bsc->id_timeout);
479 break;
480 }
481 }
482}
483
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100484static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100485{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100486 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800487 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100488
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800489 /* Parse and filter messages */
490 parsed = bsc_nat_parse(msg);
491 if (!parsed) {
492 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
493 return -1;
494 }
495
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100496 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800497 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800498
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100499 /* modify the SCCP entries */
500 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
501 switch (parsed->sccp_type) {
502 case SCCP_MSG_TYPE_CR:
503 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
504 goto exit2;
505 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
506 break;
507 case SCCP_MSG_TYPE_RLSD:
508 case SCCP_MSG_TYPE_CREF:
509 case SCCP_MSG_TYPE_DT1:
510 case SCCP_MSG_TYPE_CC:
511 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
512 break;
513 case SCCP_MSG_TYPE_RLC:
514 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
515 remove_sccp_src_ref(bsc, msg, parsed);
516 break;
517 case SCCP_MSG_TYPE_UDT:
518 /* simply forward everything */
519 break;
520 default:
521 goto exit2;
522 break;
523 }
524 }
525
526 if (found_bsc != bsc) {
527 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
528 goto exit2;
529 }
530
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100531 if (!bsc->authenticated) {
532 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
533 goto exit2;
534 }
535
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100536 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800537 if (write_queue_enqueue(&msc_queue, msg) != 0) {
538 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
539 msgb_free(msg);
540 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100541 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800542 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800543
544exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100545 /* if we filter out the reset send an ack to the BSC */
546 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100547 send_reset_ack(bsc);
548 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800549 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
550 /* do we know who is handling this? */
551 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
552 struct tlv_parsed tvp;
553 ipaccess_idtag_parse(&tvp,
554 (unsigned char *) msg->l2h + 2,
555 msgb_l2len(msg) - 2);
556 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
557 ipaccess_auth_bsc(&tvp, bsc);
558 }
559
560 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100561 }
562
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100563exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800564 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800565 msgb_free(msg);
566 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100567}
568
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800569static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100570{
571 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100572 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100573 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100574
575 if (!msg) {
576 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100577 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100578 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100579 } else {
580 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100581 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100582 return -1;
583 }
584
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100585
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100586 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 +0100587
588 /* Handle messages from the BSC */
589 /* FIXME: Currently no PONG is sent to the BSC */
590 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100591 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100592
593 return 0;
594}
595
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100596static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
597{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100598 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100599 int ret;
600 struct sockaddr_in sa;
601 socklen_t sa_len = sizeof(sa);
602
603 if (!(what & BSC_FD_READ))
604 return 0;
605
606 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
607 if (ret < 0) {
608 perror("accept");
609 return ret;
610 }
611
612 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100613 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100614
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100615 /*
616 *
617 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800618 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100619 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100620 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100621 close(ret);
622 return -1;
623 }
624
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800625 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800626 write_queue_init(&bsc->write_queue, 100);
627 bsc->write_queue.bfd.data = bsc;
628 bsc->write_queue.bfd.fd = ret;
629 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
630 bsc->write_queue.bfd.when = BSC_FD_READ;
631 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100632 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100633 close(ret);
634 talloc_free(bsc);
635 return -2;
636 }
637
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100638 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800639 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800640 ipaccess_send_id_ack(bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800641 ipaccess_send_id_req(ret);
642
643 /*
644 * start the hangup timer
645 */
646 bsc->id_timeout.data = bsc;
647 bsc->id_timeout.cb = ipaccess_close_bsc;
648 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100649 return 0;
650}
651
652static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
653{
654 struct sockaddr_in addr;
655 int ret, on = 1;
656
657 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
658 bfd->cb = ipaccess_listen_bsc_cb;
659 bfd->when = BSC_FD_READ;
660
661 memset(&addr, 0, sizeof(addr));
662 addr.sin_family = AF_INET;
663 addr.sin_port = htons(port);
664 addr.sin_addr.s_addr = in_addr->s_addr;
665
666 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
667
668 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
669 if (ret < 0) {
670 fprintf(stderr, "Could not bind the BSC socket %s\n",
671 strerror(errno));
672 return -EIO;
673 }
674
675 ret = listen(bfd->fd, 1);
676 if (ret < 0) {
677 perror("listen");
678 return ret;
679 }
680
681 ret = bsc_register_fd(bfd);
682 if (ret < 0) {
683 perror("register_listen_fd");
684 return ret;
685 }
686 return 0;
687}
688
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800689static void print_usage()
690{
691 printf("Usage: bsc_nat\n");
692}
693
694static void print_help()
695{
696 printf(" Some useful help...\n");
697 printf(" -h --help this text\n");
698 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
699 printf(" -s --disable-color\n");
700 printf(" -c --config-file filename The config file to use.\n");
701 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100702 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800703}
704
705static void handle_options(int argc, char** argv)
706{
707 while (1) {
708 int option_index = 0, c;
709 static struct option long_options[] = {
710 {"help", 0, 0, 'h'},
711 {"debug", 1, 0, 'd'},
712 {"config-file", 1, 0, 'c'},
713 {"disable-color", 0, 0, 's'},
714 {"timestamp", 0, 0, 'T'},
715 {"msc", 1, 0, 'm'},
716 {"local", 1, 0, 'l'},
717 {0, 0, 0, 0}
718 };
719
720 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
721 long_options, &option_index);
722 if (c == -1)
723 break;
724
725 switch (c) {
726 case 'h':
727 print_usage();
728 print_help();
729 exit(0);
730 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800731 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800732 break;
733 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800734 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800735 break;
736 case 'c':
737 config_file = strdup(optarg);
738 break;
739 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800740 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800741 break;
742 case 'm':
743 msc_address = strdup(optarg);
744 break;
745 case 'l':
746 inet_aton(optarg, &local_addr);
747 break;
748 default:
749 /* ignore */
750 break;
751 }
752 }
753}
754
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100755static void signal_handler(int signal)
756{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100757 switch (signal) {
758 case SIGABRT:
759 /* in case of abort, we want to obtain a talloc report
760 * and then return to the caller, who will abort the process */
761 case SIGUSR1:
762 talloc_report_full(tall_bsc_ctx, stderr);
763 break;
764 default:
765 break;
766 }
767}
768
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800769int main(int argc, char** argv)
770{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100771 int rc;
772
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800773 debug_init();
774 stderr_target = debug_target_create_stderr();
775 debug_add_target(stderr_target);
776 debug_set_all_filter(stderr_target, 1);
777
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800778 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100779 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800780 handle_options(argc, argv);
781
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800782 nat = bsc_nat_alloc();
783 if (!nat) {
784 fprintf(stderr, "Failed to allocate the BSC nat.\n");
785 return -4;
786 }
787
788 /* init vty and parse */
789 bsc_nat_vty_init(nat);
790 telnet_init(NULL, 4244);
791 if (vty_read_config_file(config_file) < 0) {
792 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
793 return -3;
794 }
795
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800796 /* seed the PRNG */
797 srand(time(NULL));
798
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100799 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800800 write_queue_init(&msc_queue, 100);
801 msc_queue.read_cb = ipaccess_msc_read_cb;
802 msc_queue.write_cb = ipaccess_msc_write_cb;
803 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100804 if (rc < 0) {
805 fprintf(stderr, "Opening the MSC connection failed.\n");
806 exit(1);
807 }
808
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100809 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100810 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100811 fprintf(stderr, "Failed to listen for BSC.\n");
812 exit(1);
813 }
814
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100815 signal(SIGABRT, &signal_handler);
816 signal(SIGUSR1, &signal_handler);
817 signal(SIGPIPE, SIG_IGN);
818
819 while (1) {
820 bsc_select_main(0);
821 }
822
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800823 return 0;
824}