blob: 309b6dbe393a7ce65ec0010799ce1d1dd8fdef34 [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 Freyther3025e192010-03-26 09:18:02 +010061static void 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 Freyther3025e192010-03-26 09:18:02 +0100129static void 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 Freyther3025e192010-03-26 09:18:02 +0100138 bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100139}
140
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100141static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100142{
143 static const u_int8_t id_ack[] = {
144 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
145 };
146
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100147 bsc_write(bsc, id_ack, sizeof(id_ack));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100148}
149
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100150static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100151{
152 static const u_int8_t id_req[] = {
153 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
154 0x01, IPAC_IDTAG_UNIT,
155 0x01, IPAC_IDTAG_MACADDR,
156 0x01, IPAC_IDTAG_LOCATION1,
157 0x01, IPAC_IDTAG_LOCATION2,
158 0x01, IPAC_IDTAG_EQUIPVERS,
159 0x01, IPAC_IDTAG_SWVERSION,
160 0x01, IPAC_IDTAG_UNITNAME,
161 0x01, IPAC_IDTAG_SERNR,
162 };
163
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100164 bsc_write(bsc, id_req, sizeof(id_req));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100165}
166
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100167/*
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100168 * SCCP patching below
169 */
170
171/* check if we are using this ref for patched already */
172static int sccp_ref_is_free(struct sccp_source_reference *ref)
173{
174 struct sccp_connections *conn;
175
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800176 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100177 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
178 return -1;
179 }
180
181 return 0;
182}
183
184/* copied from sccp.c */
185static int assign_src_local_reference(struct sccp_source_reference *ref)
186{
187 static u_int32_t last_ref = 0x50000;
188 int wrapped = 0;
189
190 do {
191 struct sccp_source_reference reference;
192 reference.octet1 = (last_ref >> 0) & 0xff;
193 reference.octet2 = (last_ref >> 8) & 0xff;
194 reference.octet3 = (last_ref >> 16) & 0xff;
195
196 ++last_ref;
197 /* do not use the reversed word and wrap around */
198 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
199 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
200 last_ref = 0;
201 ++wrapped;
202 }
203
204 if (sccp_ref_is_free(&reference) == 0) {
205 *ref = reference;
206 return 0;
207 }
208 } while (wrapped != 2);
209
210 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
211 return -1;
212}
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100213
214static 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 +0100215{
216 struct sccp_connections *conn;
217
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800218 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100219 if (!conn) {
220 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
221 return -1;
222 }
223
224 conn->real_ref = *parsed->src_local_ref;
225 if (assign_src_local_reference(&conn->patched_ref) != 0) {
226 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
227 talloc_free(conn);
228 return -1;
229 }
230
231 return 0;
232}
233
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100234static 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 +0100235{
236 struct sccp_connections *conn;
237
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800238 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100239 if (memcmp(parsed->src_local_ref,
240 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
241 if (bsc != conn->bsc) {
242 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
243 continue;
244 }
245
246
247 llist_del(&conn->list_entry);
248 talloc_free(conn);
249 return;
250 }
251 }
252
253 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
254}
255
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100256static 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 +0100257{
258 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800259 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100260 if (memcmp(parsed->dest_local_ref,
261 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
262 memcpy(parsed->dest_local_ref,
263 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
264 return conn->bsc;
265 }
266 }
267
268 return NULL;
269}
270
Holger Hans Peter Freyther45f7dcd2010-01-31 13:52:32 +0100271static 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 +0100272{
273 struct sccp_connections *conn;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800274 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100275 if (memcmp(parsed->src_local_ref,
276 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
277 memcpy(parsed->src_local_ref,
278 &conn->patched_ref, sizeof(*parsed->src_local_ref));
279 return conn->bsc;
280 }
281 }
282
283 return NULL;
284}
285
286/*
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100287 * Below is the handling of messages coming
288 * from the MSC and need to be forwarded to
289 * a real BSC.
290 */
291static void initialize_msc_if_needed()
292{
293 static int init = 0;
294 init = 1;
295
296 /* do we need to send a GSM 08.08 message here? */
297}
298
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100299/*
300 * Currently we are lacking refcounting so we need to copy each message.
301 */
302static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100303{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100304 struct msgb *msg;
305
306 if (length > 4096) {
307 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
308 return;
309 }
310
311 msg = msgb_alloc(4096, "to-bsc");
312 if (!msg) {
313 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
314 return;
315 }
316
317 msgb_put(msg, length);
318 memcpy(msg->data, data, length);
319 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
320 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
321 msgb_free(msg);
322 }
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100323}
324
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100325static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100326{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800327 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800328 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100329
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100330 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800331 parsed = bsc_nat_parse(msg);
332 if (!parsed) {
333 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100334 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800335 }
336
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100337 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800338 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800339
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100340 /* Route and modify the SCCP packet */
341 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
342 switch (parsed->sccp_type) {
343 case SCCP_MSG_TYPE_UDT:
344 /* forward UDT messages to every BSC */
345 goto send_to_all;
346 break;
347 case SCCP_MSG_TYPE_RLSD:
348 case SCCP_MSG_TYPE_CREF:
349 case SCCP_MSG_TYPE_DT1:
350 case SCCP_MSG_TYPE_CC:
351 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
352 break;
353 case SCCP_MSG_TYPE_CR:
354 case SCCP_MSG_TYPE_RLC:
355 /* MSC never opens a SCCP connection, fall through */
356 default:
357 goto exit;
358 }
359 }
360
361 talloc_free(parsed);
362 if (!bsc)
363 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100364 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800365 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100366 return -1;
367 }
368
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100369 bsc_write(bsc, msg->data, msg->len);
370 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100371
372send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800373 /*
374 * Filter Paging from the network. We do not want to send a PAGING
375 * Command to every BSC in our network. We will analys the PAGING
376 * message and then send it to the authenticated messages...
377 */
378 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
379 int data_length;
380 const u_int8_t *data;
381 struct tlv_parsed tp;
382 int i = 0;
383
384 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
385 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
386 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
387 goto exit;
388 }
389
390 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
391 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
392 if (data[0] != CELL_IDENT_LAC) {
393 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
394 goto exit;
395 }
396
397 /* go through each LAC and forward the message */
398 for (i = 1; i < data_length - 1; i += 2) {
399 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
400 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
401 if (!bsc->authenticated || _lac != bsc->lac)
402 continue;
403
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100404 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800405 }
406 }
407
408 goto exit;
409 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100410 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800411 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100412 if (!bsc->authenticated)
413 continue;
414
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100415 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100416 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800417
418exit:
419 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100420 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100421}
422
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800423static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100424{
425 int error;
426 struct msgb *msg = ipaccess_read_msg(bfd, &error);
427 struct ipaccess_head *hh;
428
429 if (!msg) {
430 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100431 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100432 exit(-2);
433 }
434
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100435 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100436 return -1;
437 }
438
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100439 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 +0100440
441 /* handle base message handling */
442 hh = (struct ipaccess_head *) msg->data;
443 ipaccess_rcvmsg_base(msg, bfd);
444
445 /* initialize the networking. This includes sending a GSM08.08 message */
446 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
447 initialize_msc_if_needed();
448 else if (hh->proto == IPAC_PROTO_SCCP)
449 forward_sccp_to_bts(msg);
450
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800451 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100452 return 0;
453}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800454
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800455static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
456{
457 int rc;
458 rc = write(bfd->fd, msg->data, msg->len);
459
460 if (rc != msg->len) {
461 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
462 return -1;
463 }
464
465 return rc;
466}
467
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100468/*
469 * Below is the handling of messages coming
470 * from the BSC and need to be forwarded to
471 * a real BSC.
472 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100473
474/*
475 * Remove the connection from the connections list,
476 * remove it from the patching of SCCP header lists
477 * as well. Maybe in the future even close connection..
478 */
479static void remove_bsc_connection(struct bsc_connection *connection)
480{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100481 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800482 bsc_unregister_fd(&connection->write_queue.bfd);
483 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100484 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100485 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100486
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800487 /* stop the timeout timer */
488 bsc_del_timer(&connection->id_timeout);
489
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100490 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800491 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100492 if (sccp_patch->bsc != connection)
493 continue;
494
495 llist_del(&sccp_patch->list_entry);
496 talloc_free(sccp_patch);
497 }
498
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100499 talloc_free(connection);
500}
501
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800502static void ipaccess_close_bsc(void *data)
503{
504 struct bsc_connection *conn = data;
505
506 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
507 remove_bsc_connection(conn);
508}
509
510static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
511{
512 struct bsc_config *conf;
513 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
514
515 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
516 if (strcmp(conf->token, token) == 0) {
517 bsc->authenticated = 1;
518 bsc->lac = conf->lac;
519 bsc_del_timer(&bsc->id_timeout);
520 break;
521 }
522 }
523}
524
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100525static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100526{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100527 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800528 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100529
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800530 /* Parse and filter messages */
531 parsed = bsc_nat_parse(msg);
532 if (!parsed) {
533 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
534 return -1;
535 }
536
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100537 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800538 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800539
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100540 /* modify the SCCP entries */
541 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
542 switch (parsed->sccp_type) {
543 case SCCP_MSG_TYPE_CR:
544 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
545 goto exit2;
546 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
547 break;
548 case SCCP_MSG_TYPE_RLSD:
549 case SCCP_MSG_TYPE_CREF:
550 case SCCP_MSG_TYPE_DT1:
551 case SCCP_MSG_TYPE_CC:
552 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
553 break;
554 case SCCP_MSG_TYPE_RLC:
555 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
556 remove_sccp_src_ref(bsc, msg, parsed);
557 break;
558 case SCCP_MSG_TYPE_UDT:
559 /* simply forward everything */
560 break;
561 default:
562 goto exit2;
563 break;
564 }
565 }
566
567 if (found_bsc != bsc) {
568 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
569 goto exit2;
570 }
571
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100572 if (!bsc->authenticated) {
573 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
574 goto exit2;
575 }
576
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100577 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800578 if (write_queue_enqueue(&msc_queue, msg) != 0) {
579 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
580 msgb_free(msg);
581 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100582 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800583 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800584
585exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100586 /* if we filter out the reset send an ack to the BSC */
587 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100588 send_reset_ack(bsc);
589 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800590 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
591 /* do we know who is handling this? */
592 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
593 struct tlv_parsed tvp;
594 ipaccess_idtag_parse(&tvp,
595 (unsigned char *) msg->l2h + 2,
596 msgb_l2len(msg) - 2);
597 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
598 ipaccess_auth_bsc(&tvp, bsc);
599 }
600
601 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100602 }
603
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100604exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800605 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800606 msgb_free(msg);
607 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100608}
609
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800610static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100611{
612 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100613 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100614 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100615
616 if (!msg) {
617 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100618 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100619 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100620 } else {
621 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100622 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100623 return -1;
624 }
625
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100626
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100627 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 +0100628
629 /* Handle messages from the BSC */
630 /* FIXME: Currently no PONG is sent to the BSC */
631 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100632 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100633
634 return 0;
635}
636
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100637static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
638{
639 int rc;
640
641 rc = write(bfd->fd, msg->data, msg->len);
642 if (rc != msg->len)
643 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
644
645 return rc;
646}
647
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100648static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
649{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100650 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100651 int ret;
652 struct sockaddr_in sa;
653 socklen_t sa_len = sizeof(sa);
654
655 if (!(what & BSC_FD_READ))
656 return 0;
657
658 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
659 if (ret < 0) {
660 perror("accept");
661 return ret;
662 }
663
664 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100665 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100666
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100667 /*
668 *
669 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800670 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100671 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100672 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100673 close(ret);
674 return -1;
675 }
676
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800677 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800678 write_queue_init(&bsc->write_queue, 100);
679 bsc->write_queue.bfd.data = bsc;
680 bsc->write_queue.bfd.fd = ret;
681 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100682 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800683 bsc->write_queue.bfd.when = BSC_FD_READ;
684 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100685 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100686 close(ret);
687 talloc_free(bsc);
688 return -2;
689 }
690
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100691 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800692 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100693 send_id_ack(bsc);
694 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800695
696 /*
697 * start the hangup timer
698 */
699 bsc->id_timeout.data = bsc;
700 bsc->id_timeout.cb = ipaccess_close_bsc;
701 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100702 return 0;
703}
704
705static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
706{
707 struct sockaddr_in addr;
708 int ret, on = 1;
709
710 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
711 bfd->cb = ipaccess_listen_bsc_cb;
712 bfd->when = BSC_FD_READ;
713
714 memset(&addr, 0, sizeof(addr));
715 addr.sin_family = AF_INET;
716 addr.sin_port = htons(port);
717 addr.sin_addr.s_addr = in_addr->s_addr;
718
719 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
720
721 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
722 if (ret < 0) {
723 fprintf(stderr, "Could not bind the BSC socket %s\n",
724 strerror(errno));
725 return -EIO;
726 }
727
728 ret = listen(bfd->fd, 1);
729 if (ret < 0) {
730 perror("listen");
731 return ret;
732 }
733
734 ret = bsc_register_fd(bfd);
735 if (ret < 0) {
736 perror("register_listen_fd");
737 return ret;
738 }
739 return 0;
740}
741
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800742static void print_usage()
743{
744 printf("Usage: bsc_nat\n");
745}
746
747static void print_help()
748{
749 printf(" Some useful help...\n");
750 printf(" -h --help this text\n");
751 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
752 printf(" -s --disable-color\n");
753 printf(" -c --config-file filename The config file to use.\n");
754 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100755 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800756}
757
758static void handle_options(int argc, char** argv)
759{
760 while (1) {
761 int option_index = 0, c;
762 static struct option long_options[] = {
763 {"help", 0, 0, 'h'},
764 {"debug", 1, 0, 'd'},
765 {"config-file", 1, 0, 'c'},
766 {"disable-color", 0, 0, 's'},
767 {"timestamp", 0, 0, 'T'},
768 {"msc", 1, 0, 'm'},
769 {"local", 1, 0, 'l'},
770 {0, 0, 0, 0}
771 };
772
773 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
774 long_options, &option_index);
775 if (c == -1)
776 break;
777
778 switch (c) {
779 case 'h':
780 print_usage();
781 print_help();
782 exit(0);
783 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800784 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800785 break;
786 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800787 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800788 break;
789 case 'c':
790 config_file = strdup(optarg);
791 break;
792 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800793 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800794 break;
795 case 'm':
796 msc_address = strdup(optarg);
797 break;
798 case 'l':
799 inet_aton(optarg, &local_addr);
800 break;
801 default:
802 /* ignore */
803 break;
804 }
805 }
806}
807
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100808static void signal_handler(int signal)
809{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100810 switch (signal) {
811 case SIGABRT:
812 /* in case of abort, we want to obtain a talloc report
813 * and then return to the caller, who will abort the process */
814 case SIGUSR1:
815 talloc_report_full(tall_bsc_ctx, stderr);
816 break;
817 default:
818 break;
819 }
820}
821
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800822int main(int argc, char** argv)
823{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100824 int rc;
825
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800826 debug_init();
827 stderr_target = debug_target_create_stderr();
828 debug_add_target(stderr_target);
829 debug_set_all_filter(stderr_target, 1);
830
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800831 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100832 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800833 handle_options(argc, argv);
834
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800835 nat = bsc_nat_alloc();
836 if (!nat) {
837 fprintf(stderr, "Failed to allocate the BSC nat.\n");
838 return -4;
839 }
840
841 /* init vty and parse */
842 bsc_nat_vty_init(nat);
843 telnet_init(NULL, 4244);
844 if (vty_read_config_file(config_file) < 0) {
845 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
846 return -3;
847 }
848
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800849 /* seed the PRNG */
850 srand(time(NULL));
851
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100852 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800853 write_queue_init(&msc_queue, 100);
854 msc_queue.read_cb = ipaccess_msc_read_cb;
855 msc_queue.write_cb = ipaccess_msc_write_cb;
856 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100857 if (rc < 0) {
858 fprintf(stderr, "Opening the MSC connection failed.\n");
859 exit(1);
860 }
861
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100862 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100863 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100864 fprintf(stderr, "Failed to listen for BSC.\n");
865 exit(1);
866 }
867
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100868 signal(SIGABRT, &signal_handler);
869 signal(SIGUSR1, &signal_handler);
870 signal(SIGPIPE, SIG_IGN);
871
872 while (1) {
873 bsc_select_main(0);
874 }
875
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800876 return 0;
877}