blob: f50e47f3596c3c62aac91d4cfb4400a61a2a0b23 [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
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100495#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100496 llist_del(&sccp_patch->list_entry);
497 talloc_free(sccp_patch);
498 }
499
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100500 talloc_free(connection);
501}
502
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800503static void ipaccess_close_bsc(void *data)
504{
505 struct bsc_connection *conn = data;
506
507 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
508 remove_bsc_connection(conn);
509}
510
511static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
512{
513 struct bsc_config *conf;
514 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
515
516 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
517 if (strcmp(conf->token, token) == 0) {
518 bsc->authenticated = 1;
519 bsc->lac = conf->lac;
520 bsc_del_timer(&bsc->id_timeout);
521 break;
522 }
523 }
524}
525
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100526static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100527{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100528 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800529 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100530
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800531 /* Parse and filter messages */
532 parsed = bsc_nat_parse(msg);
533 if (!parsed) {
534 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
535 return -1;
536 }
537
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100538 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800539 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800540
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100541 /* modify the SCCP entries */
542 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
543 switch (parsed->sccp_type) {
544 case SCCP_MSG_TYPE_CR:
545 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
546 goto exit2;
547 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
548 break;
549 case SCCP_MSG_TYPE_RLSD:
550 case SCCP_MSG_TYPE_CREF:
551 case SCCP_MSG_TYPE_DT1:
552 case SCCP_MSG_TYPE_CC:
553 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
554 break;
555 case SCCP_MSG_TYPE_RLC:
556 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
557 remove_sccp_src_ref(bsc, msg, parsed);
558 break;
559 case SCCP_MSG_TYPE_UDT:
560 /* simply forward everything */
561 break;
562 default:
563 goto exit2;
564 break;
565 }
566 }
567
568 if (found_bsc != bsc) {
569 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
570 goto exit2;
571 }
572
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100573 if (!bsc->authenticated) {
574 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
575 goto exit2;
576 }
577
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100578 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800579 if (write_queue_enqueue(&msc_queue, msg) != 0) {
580 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
581 msgb_free(msg);
582 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100583 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800584 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800585
586exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100587 /* if we filter out the reset send an ack to the BSC */
588 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100589 send_reset_ack(bsc);
590 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800591 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
592 /* do we know who is handling this? */
593 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
594 struct tlv_parsed tvp;
595 ipaccess_idtag_parse(&tvp,
596 (unsigned char *) msg->l2h + 2,
597 msgb_l2len(msg) - 2);
598 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
599 ipaccess_auth_bsc(&tvp, bsc);
600 }
601
602 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100603 }
604
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100605exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800606 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800607 msgb_free(msg);
608 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100609}
610
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800611static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100612{
613 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100614 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100615 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100616
617 if (!msg) {
618 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100619 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100620 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100621 } else {
622 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100623 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100624 return -1;
625 }
626
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100627
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100628 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 +0100629
630 /* Handle messages from the BSC */
631 /* FIXME: Currently no PONG is sent to the BSC */
632 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100633 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100634
635 return 0;
636}
637
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100638static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
639{
640 int rc;
641
642 rc = write(bfd->fd, msg->data, msg->len);
643 if (rc != msg->len)
644 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
645
646 return rc;
647}
648
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100649static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
650{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100651 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100652 int ret;
653 struct sockaddr_in sa;
654 socklen_t sa_len = sizeof(sa);
655
656 if (!(what & BSC_FD_READ))
657 return 0;
658
659 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
660 if (ret < 0) {
661 perror("accept");
662 return ret;
663 }
664
665 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100666 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100667
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100668 /*
669 *
670 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800671 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100672 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100673 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100674 close(ret);
675 return -1;
676 }
677
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800678 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800679 write_queue_init(&bsc->write_queue, 100);
680 bsc->write_queue.bfd.data = bsc;
681 bsc->write_queue.bfd.fd = ret;
682 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100683 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800684 bsc->write_queue.bfd.when = BSC_FD_READ;
685 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100686 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100687 close(ret);
688 talloc_free(bsc);
689 return -2;
690 }
691
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100692 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800693 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100694 send_id_ack(bsc);
695 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800696
697 /*
698 * start the hangup timer
699 */
700 bsc->id_timeout.data = bsc;
701 bsc->id_timeout.cb = ipaccess_close_bsc;
702 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100703 return 0;
704}
705
706static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
707{
708 struct sockaddr_in addr;
709 int ret, on = 1;
710
711 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
712 bfd->cb = ipaccess_listen_bsc_cb;
713 bfd->when = BSC_FD_READ;
714
715 memset(&addr, 0, sizeof(addr));
716 addr.sin_family = AF_INET;
717 addr.sin_port = htons(port);
718 addr.sin_addr.s_addr = in_addr->s_addr;
719
720 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
721
722 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
723 if (ret < 0) {
724 fprintf(stderr, "Could not bind the BSC socket %s\n",
725 strerror(errno));
726 return -EIO;
727 }
728
729 ret = listen(bfd->fd, 1);
730 if (ret < 0) {
731 perror("listen");
732 return ret;
733 }
734
735 ret = bsc_register_fd(bfd);
736 if (ret < 0) {
737 perror("register_listen_fd");
738 return ret;
739 }
740 return 0;
741}
742
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800743static void print_usage()
744{
745 printf("Usage: bsc_nat\n");
746}
747
748static void print_help()
749{
750 printf(" Some useful help...\n");
751 printf(" -h --help this text\n");
752 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
753 printf(" -s --disable-color\n");
754 printf(" -c --config-file filename The config file to use.\n");
755 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100756 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800757}
758
759static void handle_options(int argc, char** argv)
760{
761 while (1) {
762 int option_index = 0, c;
763 static struct option long_options[] = {
764 {"help", 0, 0, 'h'},
765 {"debug", 1, 0, 'd'},
766 {"config-file", 1, 0, 'c'},
767 {"disable-color", 0, 0, 's'},
768 {"timestamp", 0, 0, 'T'},
769 {"msc", 1, 0, 'm'},
770 {"local", 1, 0, 'l'},
771 {0, 0, 0, 0}
772 };
773
774 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
775 long_options, &option_index);
776 if (c == -1)
777 break;
778
779 switch (c) {
780 case 'h':
781 print_usage();
782 print_help();
783 exit(0);
784 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800785 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800786 break;
787 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800788 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800789 break;
790 case 'c':
791 config_file = strdup(optarg);
792 break;
793 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800794 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800795 break;
796 case 'm':
797 msc_address = strdup(optarg);
798 break;
799 case 'l':
800 inet_aton(optarg, &local_addr);
801 break;
802 default:
803 /* ignore */
804 break;
805 }
806 }
807}
808
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100809static void signal_handler(int signal)
810{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100811 switch (signal) {
812 case SIGABRT:
813 /* in case of abort, we want to obtain a talloc report
814 * and then return to the caller, who will abort the process */
815 case SIGUSR1:
816 talloc_report_full(tall_bsc_ctx, stderr);
817 break;
818 default:
819 break;
820 }
821}
822
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800823int main(int argc, char** argv)
824{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100825 int rc;
826
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800827 debug_init();
828 stderr_target = debug_target_create_stderr();
829 debug_add_target(stderr_target);
830 debug_set_all_filter(stderr_target, 1);
831
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800832 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100833 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800834 handle_options(argc, argv);
835
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800836 nat = bsc_nat_alloc();
837 if (!nat) {
838 fprintf(stderr, "Failed to allocate the BSC nat.\n");
839 return -4;
840 }
841
842 /* init vty and parse */
843 bsc_nat_vty_init(nat);
844 telnet_init(NULL, 4244);
845 if (vty_read_config_file(config_file) < 0) {
846 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
847 return -3;
848 }
849
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800850 /* seed the PRNG */
851 srand(time(NULL));
852
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100853 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800854 write_queue_init(&msc_queue, 100);
855 msc_queue.read_cb = ipaccess_msc_read_cb;
856 msc_queue.write_cb = ipaccess_msc_write_cb;
857 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100858 if (rc < 0) {
859 fprintf(stderr, "Opening the MSC connection failed.\n");
860 exit(1);
861 }
862
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100863 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100864 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100865 fprintf(stderr, "Failed to listen for BSC.\n");
866 exit(1);
867 }
868
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100869 signal(SIGABRT, &signal_handler);
870 signal(SIGUSR1, &signal_handler);
871 signal(SIGPIPE, SIG_IGN);
872
873 while (1) {
874 bsc_select_main(0);
875 }
876
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800877 return 0;
878}