blob: 6ce78be7f14ba702cd51600932db543c2e8e94c7 [file] [log] [blame]
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by on-waves.com
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther89d9fd92010-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 Freythere8fa0f12010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freytherfd012d52010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther89d9fd92010-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 Freythere907cb22010-01-12 21:15:08 +010039#include <openbsc/msgb.h>
40#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +010042#include <openbsc/bssap.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
45#include <openbsc/talloc.h>
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080046#include <openbsc/telnet_interface.h>
47
48#include <vty/vty.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080049
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080050#include <sccp/sccp.h>
51
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080052static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080053static char *msc_address = "127.0.0.1";
54static struct in_addr local_addr;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010055static struct bsc_fd msc_connection;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +010056static struct bsc_fd bsc_connection;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010057
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010058
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080059static struct bsc_nat *nat;
60
61static struct bsc_nat *bsc_nat_alloc(void)
62{
63 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
64 if (!nat)
65 return NULL;
66
67 INIT_LLIST_HEAD(&nat->sccp_connections);
68 INIT_LLIST_HEAD(&nat->bsc_connections);
69 INIT_LLIST_HEAD(&nat->bsc_configs);
70 return nat;
71}
72
73static struct bsc_connection *bsc_connection_alloc(void)
74{
75 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
76 if (!con)
77 return NULL;
78
79 return con;
80}
81
82struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
83{
84 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
85 if (!conf)
86 return NULL;
87
88 conf->token = talloc_strdup(conf, token);
89 conf->lac = lac;
90 conf->nr = nat->num_bsc;
91 conf->nat = nat;
92
93 llist_add(&conf->entry, &nat->bsc_configs);
94 ++nat->num_bsc;
95
96 return conf;
97}
98
99struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
100{
101 struct bsc_config *conf;
102
103 llist_for_each_entry(conf, &nat->bsc_configs, entry)
104 if (conf->nr == num)
105 return conf;
106
107 return NULL;
108}
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100109
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100110/*
111 * below are stubs we need to link
112 */
113int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
114 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
115{
116 return -1;
117}
118
119void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
120{}
121
122int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
123{
124 return -1;
125}
126
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100127static int send_reset_ack(struct bsc_fd *bfd)
128{
129 static const u_int8_t gsm_reset_ack[] = {
130 0x00, 0x13, 0xfd,
131 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
132 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
133 0x00, 0x01, 0x31,
134 };
135
136 return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack));
137}
138
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100139/*
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100140 * SCCP patching below
141 */
142
143/* check if we are using this ref for patched already */
144static int sccp_ref_is_free(struct sccp_source_reference *ref)
145{
146 struct sccp_connections *conn;
147
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800148 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100149 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
150 return -1;
151 }
152
153 return 0;
154}
155
156/* copied from sccp.c */
157static int assign_src_local_reference(struct sccp_source_reference *ref)
158{
159 static u_int32_t last_ref = 0x50000;
160 int wrapped = 0;
161
162 do {
163 struct sccp_source_reference reference;
164 reference.octet1 = (last_ref >> 0) & 0xff;
165 reference.octet2 = (last_ref >> 8) & 0xff;
166 reference.octet3 = (last_ref >> 16) & 0xff;
167
168 ++last_ref;
169 /* do not use the reversed word and wrap around */
170 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
171 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
172 last_ref = 0;
173 ++wrapped;
174 }
175
176 if (sccp_ref_is_free(&reference) == 0) {
177 *ref = reference;
178 return 0;
179 }
180 } while (wrapped != 2);
181
182 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
183 return -1;
184}
Holger Hans Peter Freyther0792bb52010-01-31 13:52:32 +0100185
186static int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100187{
188 struct sccp_connections *conn;
189
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800190 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100191 if (!conn) {
192 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
193 return -1;
194 }
195
196 conn->real_ref = *parsed->src_local_ref;
197 if (assign_src_local_reference(&conn->patched_ref) != 0) {
198 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
199 talloc_free(conn);
200 return -1;
201 }
202
203 return 0;
204}
205
Holger Hans Peter Freyther0792bb52010-01-31 13:52:32 +0100206static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100207{
208 struct sccp_connections *conn;
209
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800210 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100211 if (memcmp(parsed->src_local_ref,
212 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
213 if (bsc != conn->bsc) {
214 LOGP(DNAT, LOGL_ERROR, "Someone else...\n");
215 continue;
216 }
217
218
219 llist_del(&conn->list_entry);
220 talloc_free(conn);
221 return;
222 }
223 }
224
225 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
226}
227
Holger Hans Peter Freyther0792bb52010-01-31 13:52:32 +0100228static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100229{
230 struct sccp_connections *conn;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800231 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100232 if (memcmp(parsed->dest_local_ref,
233 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
234 memcpy(parsed->dest_local_ref,
235 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
236 return conn->bsc;
237 }
238 }
239
240 return NULL;
241}
242
Holger Hans Peter Freyther0792bb52010-01-31 13:52:32 +0100243static struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100244{
245 struct sccp_connections *conn;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800246 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100247 if (memcmp(parsed->src_local_ref,
248 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
249 memcpy(parsed->src_local_ref,
250 &conn->patched_ref, sizeof(*parsed->src_local_ref));
251 return conn->bsc;
252 }
253 }
254
255 return NULL;
256}
257
258/*
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100259 * Below is the handling of messages coming
260 * from the MSC and need to be forwarded to
261 * a real BSC.
262 */
263static void initialize_msc_if_needed()
264{
265 static int init = 0;
266 init = 1;
267
268 /* do we need to send a GSM 08.08 message here? */
269}
270
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100271static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100272{
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800273 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800274 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100275 int rc;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100276
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100277 /* filter, drop, patch the message? */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800278 parsed = bsc_nat_parse(msg);
279 if (!parsed) {
280 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100281 return -1;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800282 }
283
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100284 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800285 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800286
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100287 /* Route and modify the SCCP packet */
288 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
289 switch (parsed->sccp_type) {
290 case SCCP_MSG_TYPE_UDT:
291 /* forward UDT messages to every BSC */
292 goto send_to_all;
293 break;
294 case SCCP_MSG_TYPE_RLSD:
295 case SCCP_MSG_TYPE_CREF:
296 case SCCP_MSG_TYPE_DT1:
297 case SCCP_MSG_TYPE_CC:
298 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
299 break;
300 case SCCP_MSG_TYPE_CR:
301 case SCCP_MSG_TYPE_RLC:
302 /* MSC never opens a SCCP connection, fall through */
303 default:
304 goto exit;
305 }
306 }
307
308 talloc_free(parsed);
309 if (!bsc)
310 return -1;
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100311 if (!bsc->authenticated) {
312 LOGP(DNAT, LOGL_ERRO, "Selected BSC not authenticated.\n");
313 return -1;
314 }
315
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100316 return write(bsc->bsc_fd.fd, msg->data, msg->len);
317
318send_to_all:
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800319 /*
320 * Filter Paging from the network. We do not want to send a PAGING
321 * Command to every BSC in our network. We will analys the PAGING
322 * message and then send it to the authenticated messages...
323 */
324 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
325 int data_length;
326 const u_int8_t *data;
327 struct tlv_parsed tp;
328 int i = 0;
329
330 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
331 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
332 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
333 goto exit;
334 }
335
336 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
337 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
338 if (data[0] != CELL_IDENT_LAC) {
339 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
340 goto exit;
341 }
342
343 /* go through each LAC and forward the message */
344 for (i = 1; i < data_length - 1; i += 2) {
345 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
346 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
347 if (!bsc->authenticated || _lac != bsc->lac)
348 continue;
349
350 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
351 if (rc < msg->len)
352 LOGP(DNAT, LOGL_ERROR,
353 "Failed to write message to BTS: %d\n", rc);
354 }
355 }
356
357 goto exit;
358 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100359 /* currently send this to every BSC connected */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800360 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100361 if (!bsc->authenticated)
362 continue;
363
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100364 rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
365
366 /* try the next one */
367 if (rc < msg->len)
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100368 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100369 }
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800370
371exit:
372 talloc_free(parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100373 return 0;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100374}
375
376static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what)
377{
378 int error;
379 struct msgb *msg = ipaccess_read_msg(bfd, &error);
380 struct ipaccess_head *hh;
381
382 if (!msg) {
383 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100384 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100385 exit(-2);
386 }
387
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100388 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100389 return -1;
390 }
391
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100392 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100393
394 /* handle base message handling */
395 hh = (struct ipaccess_head *) msg->data;
396 ipaccess_rcvmsg_base(msg, bfd);
397
398 /* initialize the networking. This includes sending a GSM08.08 message */
399 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
400 initialize_msc_if_needed();
401 else if (hh->proto == IPAC_PROTO_SCCP)
402 forward_sccp_to_bts(msg);
403
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800404 msgb_free(msg);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100405 return 0;
406}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800407
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100408/*
409 * Below is the handling of messages coming
410 * from the BSC and need to be forwarded to
411 * a real BSC.
412 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100413
414/*
415 * Remove the connection from the connections list,
416 * remove it from the patching of SCCP header lists
417 * as well. Maybe in the future even close connection..
418 */
419static void remove_bsc_connection(struct bsc_connection *connection)
420{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100421 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freytherb8567c82010-01-13 09:51:23 +0100422 bsc_unregister_fd(&connection->bsc_fd);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800423 close(connection->bsc_fd.fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100424 llist_del(&connection->list_entry);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100425
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800426 /* stop the timeout timer */
427 bsc_del_timer(&connection->id_timeout);
428
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100429 /* remove all SCCP connections */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800430 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100431 if (sccp_patch->bsc != connection)
432 continue;
433
434 llist_del(&sccp_patch->list_entry);
435 talloc_free(sccp_patch);
436 }
437
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100438 talloc_free(connection);
439}
440
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800441static void ipaccess_close_bsc(void *data)
442{
443 struct bsc_connection *conn = data;
444
445 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
446 remove_bsc_connection(conn);
447}
448
449static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
450{
451 struct bsc_config *conf;
452 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
453
454 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
455 if (strcmp(conf->token, token) == 0) {
456 bsc->authenticated = 1;
457 bsc->lac = conf->lac;
458 bsc_del_timer(&bsc->id_timeout);
459 break;
460 }
461 }
462}
463
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100464static int forward_sccp_to_msc(struct bsc_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100465{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100466 struct bsc_connection *bsc;
Holger Hans Peter Freyther87fcac22010-02-09 16:30:53 +0100467 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800468 struct bsc_nat_parsed *parsed;
469 int rc = -1;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100470
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100471 bsc = bfd->data;
472
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800473 /* Parse and filter messages */
474 parsed = bsc_nat_parse(msg);
475 if (!parsed) {
476 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
477 return -1;
478 }
479
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100480 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800481 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800482
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100483 /* modify the SCCP entries */
484 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
485 switch (parsed->sccp_type) {
486 case SCCP_MSG_TYPE_CR:
487 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
488 goto exit2;
489 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
490 break;
491 case SCCP_MSG_TYPE_RLSD:
492 case SCCP_MSG_TYPE_CREF:
493 case SCCP_MSG_TYPE_DT1:
494 case SCCP_MSG_TYPE_CC:
495 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
496 break;
497 case SCCP_MSG_TYPE_RLC:
498 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
499 remove_sccp_src_ref(bsc, msg, parsed);
500 break;
501 case SCCP_MSG_TYPE_UDT:
502 /* simply forward everything */
503 break;
504 default:
505 goto exit2;
506 break;
507 }
508 }
509
510 if (found_bsc != bsc) {
511 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
512 goto exit2;
513 }
514
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100515 if (!bsc->authenticated) {
516 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
517 goto exit2;
518 }
519
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100520 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800521 rc = write(msc_connection.fd, msg->data, msg->len);
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100522 talloc_free(parsed);
523 return rc;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800524
525exit:
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100526 /* if we filter out the reset send an ack to the BSC */
527 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
528 send_reset_ack(bfd);
529 send_reset_ack(bfd);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800530 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
531 /* do we know who is handling this? */
532 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
533 struct tlv_parsed tvp;
534 ipaccess_idtag_parse(&tvp,
535 (unsigned char *) msg->l2h + 2,
536 msgb_l2len(msg) - 2);
537 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
538 ipaccess_auth_bsc(&tvp, bsc);
539 }
540
541 goto exit2;
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100542 }
543
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100544exit2:
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800545 talloc_free(parsed);
546 return rc;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100547}
548
549static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
550{
551 int error;
552 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100553
554 if (!msg) {
555 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100556 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100557 remove_bsc_connection((struct bsc_connection *) bfd->data);
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100558 } else {
559 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100560 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100561 return -1;
562 }
563
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100564
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100565 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100566
567 /* Handle messages from the BSC */
568 /* FIXME: Currently no PONG is sent to the BSC */
569 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100570 forward_sccp_to_msc(bfd, msg);
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800571 msgb_free(msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100572
573 return 0;
574}
575
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100576static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
577{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100578 struct bsc_connection *bsc;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100579 int ret;
580 struct sockaddr_in sa;
581 socklen_t sa_len = sizeof(sa);
582
583 if (!(what & BSC_FD_READ))
584 return 0;
585
586 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
587 if (ret < 0) {
588 perror("accept");
589 return ret;
590 }
591
592 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100593 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100594
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100595 /*
596 *
597 */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800598 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100599 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100600 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100601 close(ret);
602 return -1;
603 }
604
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800605 bsc->nat = nat;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100606 bsc->bsc_fd.data = bsc;
607 bsc->bsc_fd.fd = ret;
608 bsc->bsc_fd.cb = ipaccess_bsc_cb;
Holger Hans Peter Freyther058519d2010-01-13 09:52:29 +0100609 bsc->bsc_fd.when = BSC_FD_READ;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100610 if (bsc_register_fd(&bsc->bsc_fd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100611 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100612 close(ret);
613 talloc_free(bsc);
614 return -2;
615 }
616
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100617 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800618 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800619 ipaccess_send_id_ack(bsc->bsc_fd.fd);
620 ipaccess_send_id_req(ret);
621
622 /*
623 * start the hangup timer
624 */
625 bsc->id_timeout.data = bsc;
626 bsc->id_timeout.cb = ipaccess_close_bsc;
627 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100628 return 0;
629}
630
631static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
632{
633 struct sockaddr_in addr;
634 int ret, on = 1;
635
636 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
637 bfd->cb = ipaccess_listen_bsc_cb;
638 bfd->when = BSC_FD_READ;
639
640 memset(&addr, 0, sizeof(addr));
641 addr.sin_family = AF_INET;
642 addr.sin_port = htons(port);
643 addr.sin_addr.s_addr = in_addr->s_addr;
644
645 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
646
647 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
648 if (ret < 0) {
649 fprintf(stderr, "Could not bind the BSC socket %s\n",
650 strerror(errno));
651 return -EIO;
652 }
653
654 ret = listen(bfd->fd, 1);
655 if (ret < 0) {
656 perror("listen");
657 return ret;
658 }
659
660 ret = bsc_register_fd(bfd);
661 if (ret < 0) {
662 perror("register_listen_fd");
663 return ret;
664 }
665 return 0;
666}
667
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800668static void print_usage()
669{
670 printf("Usage: bsc_nat\n");
671}
672
673static void print_help()
674{
675 printf(" Some useful help...\n");
676 printf(" -h --help this text\n");
677 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
678 printf(" -s --disable-color\n");
679 printf(" -c --config-file filename The config file to use.\n");
680 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100681 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800682}
683
684static void handle_options(int argc, char** argv)
685{
686 while (1) {
687 int option_index = 0, c;
688 static struct option long_options[] = {
689 {"help", 0, 0, 'h'},
690 {"debug", 1, 0, 'd'},
691 {"config-file", 1, 0, 'c'},
692 {"disable-color", 0, 0, 's'},
693 {"timestamp", 0, 0, 'T'},
694 {"msc", 1, 0, 'm'},
695 {"local", 1, 0, 'l'},
696 {0, 0, 0, 0}
697 };
698
699 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
700 long_options, &option_index);
701 if (c == -1)
702 break;
703
704 switch (c) {
705 case 'h':
706 print_usage();
707 print_help();
708 exit(0);
709 case 's':
710 debug_use_color(0);
711 break;
712 case 'd':
713 debug_parse_category_mask(optarg);
714 break;
715 case 'c':
716 config_file = strdup(optarg);
717 break;
718 case 'T':
719 debug_timestamp(1);
720 break;
721 case 'm':
722 msc_address = strdup(optarg);
723 break;
724 case 'l':
725 inet_aton(optarg, &local_addr);
726 break;
727 default:
728 /* ignore */
729 break;
730 }
731 }
732}
733
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100734static void signal_handler(int signal)
735{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100736 switch (signal) {
737 case SIGABRT:
738 /* in case of abort, we want to obtain a talloc report
739 * and then return to the caller, who will abort the process */
740 case SIGUSR1:
741 talloc_report_full(tall_bsc_ctx, stderr);
742 break;
743 default:
744 break;
745 }
746}
747
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800748int main(int argc, char** argv)
749{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100750 int rc;
751
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800752 /* parse options */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100753 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800754 handle_options(argc, argv);
755
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800756 nat = bsc_nat_alloc();
757 if (!nat) {
758 fprintf(stderr, "Failed to allocate the BSC nat.\n");
759 return -4;
760 }
761
762 /* init vty and parse */
763 bsc_nat_vty_init(nat);
764 telnet_init(NULL, 4244);
765 if (vty_read_config_file(config_file) < 0) {
766 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
767 return -3;
768 }
769
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800770 /* seed the PRNG */
771 srand(time(NULL));
772
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100773 /* connect to the MSC */
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100774 msc_connection.cb = ipaccess_msc_cb;
775 rc = connect_to_msc(&msc_connection, msc_address, 5000);
776 if (rc < 0) {
777 fprintf(stderr, "Opening the MSC connection failed.\n");
778 exit(1);
779 }
780
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100781 /* wait for the BSC */
782 if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) {
783 fprintf(stderr, "Failed to listen for BSC.\n");
784 exit(1);
785 }
786
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100787 signal(SIGABRT, &signal_handler);
788 signal(SIGUSR1, &signal_handler);
789 signal(SIGPIPE, SIG_IGN);
790
791 while (1) {
792 bsc_select_main(0);
793 }
794
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800795 return 0;
796}