blob: db2d60532a9ebd6a4ad5425c7d059045c7a6f9f1 [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 Freytherbaf2abe2010-06-15 18:47:29 +080056static struct bsc_msc_connection *msc_con;
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 Freytherbaf2abe2010-06-15 18:47:29 +0800423static void msc_connection_was_lost(struct bsc_msc_connection *con)
424{
425 LOGP(DMSC, LOGL_FATAL, "Lost the connection.\n");
426 exit(0);
427}
428
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800429static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100430{
431 int error;
432 struct msgb *msg = ipaccess_read_msg(bfd, &error);
433 struct ipaccess_head *hh;
434
435 if (!msg) {
436 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100437 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800438 bsc_msc_lost(msc_con);
439 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100440 }
441
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100442 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100443 return -1;
444 }
445
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100446 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 +0100447
448 /* handle base message handling */
449 hh = (struct ipaccess_head *) msg->data;
450 ipaccess_rcvmsg_base(msg, bfd);
451
452 /* initialize the networking. This includes sending a GSM08.08 message */
453 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
454 initialize_msc_if_needed();
455 else if (hh->proto == IPAC_PROTO_SCCP)
456 forward_sccp_to_bts(msg);
457
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800458 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100459 return 0;
460}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800461
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800462static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
463{
464 int rc;
465 rc = write(bfd->fd, msg->data, msg->len);
466
467 if (rc != msg->len) {
468 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
469 return -1;
470 }
471
472 return rc;
473}
474
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100475/*
476 * Below is the handling of messages coming
477 * from the BSC and need to be forwarded to
478 * a real BSC.
479 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100480
481/*
482 * Remove the connection from the connections list,
483 * remove it from the patching of SCCP header lists
484 * as well. Maybe in the future even close connection..
485 */
486static void remove_bsc_connection(struct bsc_connection *connection)
487{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100488 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800489 bsc_unregister_fd(&connection->write_queue.bfd);
490 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100491 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100492 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100493
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800494 /* stop the timeout timer */
495 bsc_del_timer(&connection->id_timeout);
496
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100497 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800498 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100499 if (sccp_patch->bsc != connection)
500 continue;
501
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100502#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100503 llist_del(&sccp_patch->list_entry);
504 talloc_free(sccp_patch);
505 }
506
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100507 talloc_free(connection);
508}
509
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800510static void ipaccess_close_bsc(void *data)
511{
512 struct bsc_connection *conn = data;
513
514 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
515 remove_bsc_connection(conn);
516}
517
518static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
519{
520 struct bsc_config *conf;
521 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
522
523 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
524 if (strcmp(conf->token, token) == 0) {
525 bsc->authenticated = 1;
526 bsc->lac = conf->lac;
527 bsc_del_timer(&bsc->id_timeout);
528 break;
529 }
530 }
531}
532
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100533static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100534{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100535 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800536 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100537
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800538 /* Parse and filter messages */
539 parsed = bsc_nat_parse(msg);
540 if (!parsed) {
541 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
542 return -1;
543 }
544
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100545 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800546 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800547
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100548 /* modify the SCCP entries */
549 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
550 switch (parsed->sccp_type) {
551 case SCCP_MSG_TYPE_CR:
552 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
553 goto exit2;
554 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
555 break;
556 case SCCP_MSG_TYPE_RLSD:
557 case SCCP_MSG_TYPE_CREF:
558 case SCCP_MSG_TYPE_DT1:
559 case SCCP_MSG_TYPE_CC:
560 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
561 break;
562 case SCCP_MSG_TYPE_RLC:
563 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
564 remove_sccp_src_ref(bsc, msg, parsed);
565 break;
566 case SCCP_MSG_TYPE_UDT:
567 /* simply forward everything */
568 break;
569 default:
570 goto exit2;
571 break;
572 }
573 }
574
575 if (found_bsc != bsc) {
576 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
577 goto exit2;
578 }
579
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100580 if (!bsc->authenticated) {
581 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
582 goto exit2;
583 }
584
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100585 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800586 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800587 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
588 msgb_free(msg);
589 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100590 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800591 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800592
593exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100594 /* if we filter out the reset send an ack to the BSC */
595 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100596 send_reset_ack(bsc);
597 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800598 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
599 /* do we know who is handling this? */
600 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
601 struct tlv_parsed tvp;
602 ipaccess_idtag_parse(&tvp,
603 (unsigned char *) msg->l2h + 2,
604 msgb_l2len(msg) - 2);
605 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
606 ipaccess_auth_bsc(&tvp, bsc);
607 }
608
609 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100610 }
611
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100612exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800613 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800614 msgb_free(msg);
615 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100616}
617
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800618static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100619{
620 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100621 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100622 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100623
624 if (!msg) {
625 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100626 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100627 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100628 } else {
629 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100630 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100631 return -1;
632 }
633
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100634
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100635 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 +0100636
637 /* Handle messages from the BSC */
638 /* FIXME: Currently no PONG is sent to the BSC */
639 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100640 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100641
642 return 0;
643}
644
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100645static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
646{
647 int rc;
648
649 rc = write(bfd->fd, msg->data, msg->len);
650 if (rc != msg->len)
651 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
652
653 return rc;
654}
655
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100656static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
657{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100658 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100659 int ret;
660 struct sockaddr_in sa;
661 socklen_t sa_len = sizeof(sa);
662
663 if (!(what & BSC_FD_READ))
664 return 0;
665
666 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
667 if (ret < 0) {
668 perror("accept");
669 return ret;
670 }
671
672 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100673 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100674
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100675 /*
676 *
677 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800678 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100679 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100680 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100681 close(ret);
682 return -1;
683 }
684
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800685 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800686 write_queue_init(&bsc->write_queue, 100);
687 bsc->write_queue.bfd.data = bsc;
688 bsc->write_queue.bfd.fd = ret;
689 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100690 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800691 bsc->write_queue.bfd.when = BSC_FD_READ;
692 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100693 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100694 close(ret);
695 talloc_free(bsc);
696 return -2;
697 }
698
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100699 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800700 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100701 send_id_ack(bsc);
702 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800703
704 /*
705 * start the hangup timer
706 */
707 bsc->id_timeout.data = bsc;
708 bsc->id_timeout.cb = ipaccess_close_bsc;
709 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100710 return 0;
711}
712
713static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
714{
715 struct sockaddr_in addr;
716 int ret, on = 1;
717
718 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
719 bfd->cb = ipaccess_listen_bsc_cb;
720 bfd->when = BSC_FD_READ;
721
722 memset(&addr, 0, sizeof(addr));
723 addr.sin_family = AF_INET;
724 addr.sin_port = htons(port);
725 addr.sin_addr.s_addr = in_addr->s_addr;
726
727 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
728
729 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
730 if (ret < 0) {
731 fprintf(stderr, "Could not bind the BSC socket %s\n",
732 strerror(errno));
733 return -EIO;
734 }
735
736 ret = listen(bfd->fd, 1);
737 if (ret < 0) {
738 perror("listen");
739 return ret;
740 }
741
742 ret = bsc_register_fd(bfd);
743 if (ret < 0) {
744 perror("register_listen_fd");
745 return ret;
746 }
747 return 0;
748}
749
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800750static void print_usage()
751{
752 printf("Usage: bsc_nat\n");
753}
754
755static void print_help()
756{
757 printf(" Some useful help...\n");
758 printf(" -h --help this text\n");
759 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
760 printf(" -s --disable-color\n");
761 printf(" -c --config-file filename The config file to use.\n");
762 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100763 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800764}
765
766static void handle_options(int argc, char** argv)
767{
768 while (1) {
769 int option_index = 0, c;
770 static struct option long_options[] = {
771 {"help", 0, 0, 'h'},
772 {"debug", 1, 0, 'd'},
773 {"config-file", 1, 0, 'c'},
774 {"disable-color", 0, 0, 's'},
775 {"timestamp", 0, 0, 'T'},
776 {"msc", 1, 0, 'm'},
777 {"local", 1, 0, 'l'},
778 {0, 0, 0, 0}
779 };
780
781 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
782 long_options, &option_index);
783 if (c == -1)
784 break;
785
786 switch (c) {
787 case 'h':
788 print_usage();
789 print_help();
790 exit(0);
791 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800792 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800793 break;
794 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800795 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800796 break;
797 case 'c':
798 config_file = strdup(optarg);
799 break;
800 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800801 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800802 break;
803 case 'm':
804 msc_address = strdup(optarg);
805 break;
806 case 'l':
807 inet_aton(optarg, &local_addr);
808 break;
809 default:
810 /* ignore */
811 break;
812 }
813 }
814}
815
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100816static void signal_handler(int signal)
817{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100818 switch (signal) {
819 case SIGABRT:
820 /* in case of abort, we want to obtain a talloc report
821 * and then return to the caller, who will abort the process */
822 case SIGUSR1:
823 talloc_report_full(tall_bsc_ctx, stderr);
824 break;
825 default:
826 break;
827 }
828}
829
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800830int main(int argc, char** argv)
831{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100832
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800833 debug_init();
834 stderr_target = debug_target_create_stderr();
835 debug_add_target(stderr_target);
836 debug_set_all_filter(stderr_target, 1);
837
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800838 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100839 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800840 handle_options(argc, argv);
841
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800842 nat = bsc_nat_alloc();
843 if (!nat) {
844 fprintf(stderr, "Failed to allocate the BSC nat.\n");
845 return -4;
846 }
847
848 /* init vty and parse */
849 bsc_nat_vty_init(nat);
850 telnet_init(NULL, 4244);
851 if (vty_read_config_file(config_file) < 0) {
852 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
853 return -3;
854 }
855
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800856 /* seed the PRNG */
857 srand(time(NULL));
858
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100859 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800860 msc_con = bsc_msc_create(msc_address, 5000);
861 if (!msc_con) {
862 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100863 exit(1);
864 }
865
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800866 msc_con->connection_loss = msc_connection_was_lost;
867 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
868 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
869 bsc_msc_connect(msc_con);
870
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100871 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100872 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100873 fprintf(stderr, "Failed to listen for BSC.\n");
874 exit(1);
875 }
876
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100877 signal(SIGABRT, &signal_handler);
878 signal(SIGUSR1, &signal_handler);
879 signal(SIGPIPE, SIG_IGN);
880
881 while (1) {
882 bsc_select_main(0);
883 }
884
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800885 return 0;
886}