blob: ece58231b152dc8f5a2e38457142b18379b87109 [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 Freyther24614ad2010-01-13 09:28:12 +0100484 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100485
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800486 /* stop the timeout timer */
487 bsc_del_timer(&connection->id_timeout);
488
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100489 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800490 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100491 if (sccp_patch->bsc != connection)
492 continue;
493
494 llist_del(&sccp_patch->list_entry);
495 talloc_free(sccp_patch);
496 }
497
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100498 talloc_free(connection);
499}
500
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800501static void ipaccess_close_bsc(void *data)
502{
503 struct bsc_connection *conn = data;
504
505 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
506 remove_bsc_connection(conn);
507}
508
509static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
510{
511 struct bsc_config *conf;
512 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
513
514 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
515 if (strcmp(conf->token, token) == 0) {
516 bsc->authenticated = 1;
517 bsc->lac = conf->lac;
518 bsc_del_timer(&bsc->id_timeout);
519 break;
520 }
521 }
522}
523
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100524static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100525{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100526 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800527 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100528
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800529 /* Parse and filter messages */
530 parsed = bsc_nat_parse(msg);
531 if (!parsed) {
532 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
533 return -1;
534 }
535
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100536 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800537 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800538
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100539 /* modify the SCCP entries */
540 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
541 switch (parsed->sccp_type) {
542 case SCCP_MSG_TYPE_CR:
543 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
544 goto exit2;
545 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
546 break;
547 case SCCP_MSG_TYPE_RLSD:
548 case SCCP_MSG_TYPE_CREF:
549 case SCCP_MSG_TYPE_DT1:
550 case SCCP_MSG_TYPE_CC:
551 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
552 break;
553 case SCCP_MSG_TYPE_RLC:
554 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
555 remove_sccp_src_ref(bsc, msg, parsed);
556 break;
557 case SCCP_MSG_TYPE_UDT:
558 /* simply forward everything */
559 break;
560 default:
561 goto exit2;
562 break;
563 }
564 }
565
566 if (found_bsc != bsc) {
567 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
568 goto exit2;
569 }
570
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100571 if (!bsc->authenticated) {
572 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
573 goto exit2;
574 }
575
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100576 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800577 if (write_queue_enqueue(&msc_queue, msg) != 0) {
578 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
579 msgb_free(msg);
580 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100581 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800582 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800583
584exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100585 /* if we filter out the reset send an ack to the BSC */
586 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100587 send_reset_ack(bsc);
588 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800589 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
590 /* do we know who is handling this? */
591 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
592 struct tlv_parsed tvp;
593 ipaccess_idtag_parse(&tvp,
594 (unsigned char *) msg->l2h + 2,
595 msgb_l2len(msg) - 2);
596 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
597 ipaccess_auth_bsc(&tvp, bsc);
598 }
599
600 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100601 }
602
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100603exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800604 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800605 msgb_free(msg);
606 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100607}
608
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800609static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100610{
611 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100612 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100613 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100614
615 if (!msg) {
616 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100617 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100618 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100619 } else {
620 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100621 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100622 return -1;
623 }
624
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100625
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100626 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 +0100627
628 /* Handle messages from the BSC */
629 /* FIXME: Currently no PONG is sent to the BSC */
630 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100631 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100632
633 return 0;
634}
635
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100636static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
637{
638 int rc;
639
640 rc = write(bfd->fd, msg->data, msg->len);
641 if (rc != msg->len)
642 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
643
644 return rc;
645}
646
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100647static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
648{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100649 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100650 int ret;
651 struct sockaddr_in sa;
652 socklen_t sa_len = sizeof(sa);
653
654 if (!(what & BSC_FD_READ))
655 return 0;
656
657 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
658 if (ret < 0) {
659 perror("accept");
660 return ret;
661 }
662
663 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100664 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100665
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100666 /*
667 *
668 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800669 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100670 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100671 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100672 close(ret);
673 return -1;
674 }
675
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800676 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800677 write_queue_init(&bsc->write_queue, 100);
678 bsc->write_queue.bfd.data = bsc;
679 bsc->write_queue.bfd.fd = ret;
680 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100681 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800682 bsc->write_queue.bfd.when = BSC_FD_READ;
683 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100684 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100685 close(ret);
686 talloc_free(bsc);
687 return -2;
688 }
689
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100690 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800691 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100692 send_id_ack(bsc);
693 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800694
695 /*
696 * start the hangup timer
697 */
698 bsc->id_timeout.data = bsc;
699 bsc->id_timeout.cb = ipaccess_close_bsc;
700 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100701 return 0;
702}
703
704static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
705{
706 struct sockaddr_in addr;
707 int ret, on = 1;
708
709 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
710 bfd->cb = ipaccess_listen_bsc_cb;
711 bfd->when = BSC_FD_READ;
712
713 memset(&addr, 0, sizeof(addr));
714 addr.sin_family = AF_INET;
715 addr.sin_port = htons(port);
716 addr.sin_addr.s_addr = in_addr->s_addr;
717
718 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
719
720 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
721 if (ret < 0) {
722 fprintf(stderr, "Could not bind the BSC socket %s\n",
723 strerror(errno));
724 return -EIO;
725 }
726
727 ret = listen(bfd->fd, 1);
728 if (ret < 0) {
729 perror("listen");
730 return ret;
731 }
732
733 ret = bsc_register_fd(bfd);
734 if (ret < 0) {
735 perror("register_listen_fd");
736 return ret;
737 }
738 return 0;
739}
740
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800741static void print_usage()
742{
743 printf("Usage: bsc_nat\n");
744}
745
746static void print_help()
747{
748 printf(" Some useful help...\n");
749 printf(" -h --help this text\n");
750 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
751 printf(" -s --disable-color\n");
752 printf(" -c --config-file filename The config file to use.\n");
753 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100754 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800755}
756
757static void handle_options(int argc, char** argv)
758{
759 while (1) {
760 int option_index = 0, c;
761 static struct option long_options[] = {
762 {"help", 0, 0, 'h'},
763 {"debug", 1, 0, 'd'},
764 {"config-file", 1, 0, 'c'},
765 {"disable-color", 0, 0, 's'},
766 {"timestamp", 0, 0, 'T'},
767 {"msc", 1, 0, 'm'},
768 {"local", 1, 0, 'l'},
769 {0, 0, 0, 0}
770 };
771
772 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
773 long_options, &option_index);
774 if (c == -1)
775 break;
776
777 switch (c) {
778 case 'h':
779 print_usage();
780 print_help();
781 exit(0);
782 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800783 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800784 break;
785 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800786 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800787 break;
788 case 'c':
789 config_file = strdup(optarg);
790 break;
791 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800792 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800793 break;
794 case 'm':
795 msc_address = strdup(optarg);
796 break;
797 case 'l':
798 inet_aton(optarg, &local_addr);
799 break;
800 default:
801 /* ignore */
802 break;
803 }
804 }
805}
806
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100807static void signal_handler(int signal)
808{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100809 switch (signal) {
810 case SIGABRT:
811 /* in case of abort, we want to obtain a talloc report
812 * and then return to the caller, who will abort the process */
813 case SIGUSR1:
814 talloc_report_full(tall_bsc_ctx, stderr);
815 break;
816 default:
817 break;
818 }
819}
820
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800821int main(int argc, char** argv)
822{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100823 int rc;
824
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800825 debug_init();
826 stderr_target = debug_target_create_stderr();
827 debug_add_target(stderr_target);
828 debug_set_all_filter(stderr_target, 1);
829
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800830 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100831 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800832 handle_options(argc, argv);
833
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800834 nat = bsc_nat_alloc();
835 if (!nat) {
836 fprintf(stderr, "Failed to allocate the BSC nat.\n");
837 return -4;
838 }
839
840 /* init vty and parse */
841 bsc_nat_vty_init(nat);
842 telnet_init(NULL, 4244);
843 if (vty_read_config_file(config_file) < 0) {
844 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
845 return -3;
846 }
847
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800848 /* seed the PRNG */
849 srand(time(NULL));
850
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100851 /* connect to the MSC */
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800852 write_queue_init(&msc_queue, 100);
853 msc_queue.read_cb = ipaccess_msc_read_cb;
854 msc_queue.write_cb = ipaccess_msc_write_cb;
855 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100856 if (rc < 0) {
857 fprintf(stderr, "Opening the MSC connection failed.\n");
858 exit(1);
859 }
860
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100861 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100862 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100863 fprintf(stderr, "Failed to listen for BSC.\n");
864 exit(1);
865 }
866
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100867 signal(SIGABRT, &signal_handler);
868 signal(SIGUSR1, &signal_handler);
869 signal(SIGPIPE, SIG_IGN);
870
871 while (1) {
872 bsc_select_main(0);
873 }
874
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800875 return 0;
876}