blob: e2552c4f3953f1100a5614b5470f7890647ccb48 [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>
Holger Hans Peter Freyther98e49d42010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
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/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +010041#include <openbsc/bssap.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080044#include <openbsc/telnet_interface.h>
45
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080046#include <osmocore/talloc.h>
47
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080048#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 Freyther3b960892010-06-15 19:06:18 +080052struct debug_target *stderr_target;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080053static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080054static char *msc_address = "127.0.0.1";
55static struct in_addr local_addr;
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +080056static struct write_queue msc_queue;
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +010057static struct bsc_fd bsc_listen;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010058
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010059
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080060static struct bsc_nat *nat;
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +010061static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length);
Holger Hans Peter Freyther5e547882010-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 Freyther4a629602010-01-13 09:28:12 +0100111
Holger Hans Peter Freythere907cb22010-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 Freyther4ce32702010-03-26 07:24:34 +0100129static int send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther722ead82010-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 Freyther4ce32702010-03-26 07:24:34 +0100138 return bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100139}
140
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100141static int send_id_ack(struct bsc_connection *bsc)
142{
143 static const u_int8_t id_ack[] = {
144 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
145 };
146
147 return bsc_write(bsc, id_ack, sizeof(id_ack));
148}
149
150static int send_id_req(struct bsc_connection *bsc)
151{
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
164 return bsc_write(bsc, id_req, sizeof(id_req));
165}
166
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100167/*
Holger Hans Peter Freythere83917d2010-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 Freyther5e547882010-06-15 18:46:11 +0800176 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-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 Freyther0792bb52010-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 Freythere83917d2010-01-31 09:46:21 +0100215{
216 struct sccp_connections *conn;
217
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800218 conn = talloc_zero(nat, struct sccp_connections);
Holger Hans Peter Freythere83917d2010-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 Freyther0792bb52010-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 Freythere83917d2010-01-31 09:46:21 +0100235{
236 struct sccp_connections *conn;
237
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800238 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-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 Freyther0792bb52010-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 Freythere83917d2010-01-31 09:46:21 +0100257{
258 struct sccp_connections *conn;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800259 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-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 Freyther0792bb52010-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 Freythere83917d2010-01-31 09:46:21 +0100272{
273 struct sccp_connections *conn;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800274 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-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 Freythere907cb22010-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 Freyther4ce32702010-03-26 07:24:34 +0100299static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100300{
301 return write(bsc->write_queue.bfd.fd, data, length);
302}
303
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100304static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100305{
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800306 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800307 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100308 int rc;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100309
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100310 /* filter, drop, patch the message? */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800311 parsed = bsc_nat_parse(msg);
312 if (!parsed) {
313 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100314 return -1;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800315 }
316
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100317 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800318 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800319
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100320 /* Route and modify the SCCP packet */
321 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
322 switch (parsed->sccp_type) {
323 case SCCP_MSG_TYPE_UDT:
324 /* forward UDT messages to every BSC */
325 goto send_to_all;
326 break;
327 case SCCP_MSG_TYPE_RLSD:
328 case SCCP_MSG_TYPE_CREF:
329 case SCCP_MSG_TYPE_DT1:
330 case SCCP_MSG_TYPE_CC:
331 bsc = patch_sccp_src_ref_to_bsc(msg, parsed);
332 break;
333 case SCCP_MSG_TYPE_CR:
334 case SCCP_MSG_TYPE_RLC:
335 /* MSC never opens a SCCP connection, fall through */
336 default:
337 goto exit;
338 }
339 }
340
341 talloc_free(parsed);
342 if (!bsc)
343 return -1;
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100344 if (!bsc->authenticated) {
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800345 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100346 return -1;
347 }
348
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100349 return bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100350
351send_to_all:
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800352 /*
353 * Filter Paging from the network. We do not want to send a PAGING
354 * Command to every BSC in our network. We will analys the PAGING
355 * message and then send it to the authenticated messages...
356 */
357 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
358 int data_length;
359 const u_int8_t *data;
360 struct tlv_parsed tp;
361 int i = 0;
362
363 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
364 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
365 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
366 goto exit;
367 }
368
369 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
370 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
371 if (data[0] != CELL_IDENT_LAC) {
372 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
373 goto exit;
374 }
375
376 /* go through each LAC and forward the message */
377 for (i = 1; i < data_length - 1; i += 2) {
378 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
379 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
380 if (!bsc->authenticated || _lac != bsc->lac)
381 continue;
382
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100383 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800384 if (rc < msg->len)
385 LOGP(DNAT, LOGL_ERROR,
386 "Failed to write message to BTS: %d\n", rc);
387 }
388 }
389
390 goto exit;
391 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100392 /* currently send this to every BSC connected */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800393 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100394 if (!bsc->authenticated)
395 continue;
396
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100397 rc = bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freytherac0dc7f2010-01-25 10:01:30 +0100398
399 /* try the next one */
400 if (rc < msg->len)
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100401 LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100402 }
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800403
404exit:
405 talloc_free(parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100406 return 0;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100407}
408
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800409static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100410{
411 int error;
412 struct msgb *msg = ipaccess_read_msg(bfd, &error);
413 struct ipaccess_head *hh;
414
415 if (!msg) {
416 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100417 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100418 exit(-2);
419 }
420
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100421 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100422 return -1;
423 }
424
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100425 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 +0100426
427 /* handle base message handling */
428 hh = (struct ipaccess_head *) msg->data;
429 ipaccess_rcvmsg_base(msg, bfd);
430
431 /* initialize the networking. This includes sending a GSM08.08 message */
432 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
433 initialize_msc_if_needed();
434 else if (hh->proto == IPAC_PROTO_SCCP)
435 forward_sccp_to_bts(msg);
436
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800437 msgb_free(msg);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100438 return 0;
439}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800440
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800441static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
442{
443 int rc;
444 rc = write(bfd->fd, msg->data, msg->len);
445
446 if (rc != msg->len) {
447 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
448 return -1;
449 }
450
451 return rc;
452}
453
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100454/*
455 * Below is the handling of messages coming
456 * from the BSC and need to be forwarded to
457 * a real BSC.
458 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100459
460/*
461 * Remove the connection from the connections list,
462 * remove it from the patching of SCCP header lists
463 * as well. Maybe in the future even close connection..
464 */
465static void remove_bsc_connection(struct bsc_connection *connection)
466{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100467 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800468 bsc_unregister_fd(&connection->write_queue.bfd);
469 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100470 llist_del(&connection->list_entry);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100471
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800472 /* stop the timeout timer */
473 bsc_del_timer(&connection->id_timeout);
474
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100475 /* remove all SCCP connections */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800476 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100477 if (sccp_patch->bsc != connection)
478 continue;
479
480 llist_del(&sccp_patch->list_entry);
481 talloc_free(sccp_patch);
482 }
483
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100484 talloc_free(connection);
485}
486
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800487static void ipaccess_close_bsc(void *data)
488{
489 struct bsc_connection *conn = data;
490
491 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
492 remove_bsc_connection(conn);
493}
494
495static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
496{
497 struct bsc_config *conf;
498 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
499
500 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
501 if (strcmp(conf->token, token) == 0) {
502 bsc->authenticated = 1;
503 bsc->lac = conf->lac;
504 bsc_del_timer(&bsc->id_timeout);
505 break;
506 }
507 }
508}
509
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100510static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100511{
Holger Hans Peter Freyther87fcac22010-02-09 16:30:53 +0100512 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800513 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100514
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800515 /* Parse and filter messages */
516 parsed = bsc_nat_parse(msg);
517 if (!parsed) {
518 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
519 return -1;
520 }
521
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100522 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800523 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800524
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100525 /* modify the SCCP entries */
526 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
527 switch (parsed->sccp_type) {
528 case SCCP_MSG_TYPE_CR:
529 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
530 goto exit2;
531 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
532 break;
533 case SCCP_MSG_TYPE_RLSD:
534 case SCCP_MSG_TYPE_CREF:
535 case SCCP_MSG_TYPE_DT1:
536 case SCCP_MSG_TYPE_CC:
537 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
538 break;
539 case SCCP_MSG_TYPE_RLC:
540 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed);
541 remove_sccp_src_ref(bsc, msg, parsed);
542 break;
543 case SCCP_MSG_TYPE_UDT:
544 /* simply forward everything */
545 break;
546 default:
547 goto exit2;
548 break;
549 }
550 }
551
552 if (found_bsc != bsc) {
553 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
554 goto exit2;
555 }
556
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100557 if (!bsc->authenticated) {
558 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
559 goto exit2;
560 }
561
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100562 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800563 if (write_queue_enqueue(&msc_queue, msg) != 0) {
564 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
565 msgb_free(msg);
566 }
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100567 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800568 return 0;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800569
570exit:
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100571 /* if we filter out the reset send an ack to the BSC */
572 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100573 send_reset_ack(bsc);
574 send_reset_ack(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800575 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
576 /* do we know who is handling this? */
577 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
578 struct tlv_parsed tvp;
579 ipaccess_idtag_parse(&tvp,
580 (unsigned char *) msg->l2h + 2,
581 msgb_l2len(msg) - 2);
582 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
583 ipaccess_auth_bsc(&tvp, bsc);
584 }
585
586 goto exit2;
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100587 }
588
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100589exit2:
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800590 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800591 msgb_free(msg);
592 return -1;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100593}
594
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800595static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100596{
597 int error;
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100598 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100599 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100600
601 if (!msg) {
602 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100603 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100604 remove_bsc_connection(bsc);
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100605 } else {
606 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100607 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100608 return -1;
609 }
610
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100611
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100612 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 +0100613
614 /* Handle messages from the BSC */
615 /* FIXME: Currently no PONG is sent to the BSC */
616 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100617 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100618
619 return 0;
620}
621
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100622static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
623{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100624 struct bsc_connection *bsc;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100625 int ret;
626 struct sockaddr_in sa;
627 socklen_t sa_len = sizeof(sa);
628
629 if (!(what & BSC_FD_READ))
630 return 0;
631
632 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
633 if (ret < 0) {
634 perror("accept");
635 return ret;
636 }
637
638 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100639 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100640
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100641 /*
642 *
643 */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800644 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100645 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100646 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100647 close(ret);
648 return -1;
649 }
650
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800651 bsc->nat = nat;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800652 write_queue_init(&bsc->write_queue, 100);
653 bsc->write_queue.bfd.data = bsc;
654 bsc->write_queue.bfd.fd = ret;
655 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
656 bsc->write_queue.bfd.when = BSC_FD_READ;
657 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100658 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100659 close(ret);
660 talloc_free(bsc);
661 return -2;
662 }
663
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100664 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800665 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100666 send_id_ack(bsc);
667 send_id_req(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800668
669 /*
670 * start the hangup timer
671 */
672 bsc->id_timeout.data = bsc;
673 bsc->id_timeout.cb = ipaccess_close_bsc;
674 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100675 return 0;
676}
677
678static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
679{
680 struct sockaddr_in addr;
681 int ret, on = 1;
682
683 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
684 bfd->cb = ipaccess_listen_bsc_cb;
685 bfd->when = BSC_FD_READ;
686
687 memset(&addr, 0, sizeof(addr));
688 addr.sin_family = AF_INET;
689 addr.sin_port = htons(port);
690 addr.sin_addr.s_addr = in_addr->s_addr;
691
692 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
693
694 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
695 if (ret < 0) {
696 fprintf(stderr, "Could not bind the BSC socket %s\n",
697 strerror(errno));
698 return -EIO;
699 }
700
701 ret = listen(bfd->fd, 1);
702 if (ret < 0) {
703 perror("listen");
704 return ret;
705 }
706
707 ret = bsc_register_fd(bfd);
708 if (ret < 0) {
709 perror("register_listen_fd");
710 return ret;
711 }
712 return 0;
713}
714
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800715static void print_usage()
716{
717 printf("Usage: bsc_nat\n");
718}
719
720static void print_help()
721{
722 printf(" Some useful help...\n");
723 printf(" -h --help this text\n");
724 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
725 printf(" -s --disable-color\n");
726 printf(" -c --config-file filename The config file to use.\n");
727 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100728 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800729}
730
731static void handle_options(int argc, char** argv)
732{
733 while (1) {
734 int option_index = 0, c;
735 static struct option long_options[] = {
736 {"help", 0, 0, 'h'},
737 {"debug", 1, 0, 'd'},
738 {"config-file", 1, 0, 'c'},
739 {"disable-color", 0, 0, 's'},
740 {"timestamp", 0, 0, 'T'},
741 {"msc", 1, 0, 'm'},
742 {"local", 1, 0, 'l'},
743 {0, 0, 0, 0}
744 };
745
746 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
747 long_options, &option_index);
748 if (c == -1)
749 break;
750
751 switch (c) {
752 case 'h':
753 print_usage();
754 print_help();
755 exit(0);
756 case 's':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800757 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800758 break;
759 case 'd':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800760 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800761 break;
762 case 'c':
763 config_file = strdup(optarg);
764 break;
765 case 'T':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800766 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800767 break;
768 case 'm':
769 msc_address = strdup(optarg);
770 break;
771 case 'l':
772 inet_aton(optarg, &local_addr);
773 break;
774 default:
775 /* ignore */
776 break;
777 }
778 }
779}
780
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100781static void signal_handler(int signal)
782{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100783 switch (signal) {
784 case SIGABRT:
785 /* in case of abort, we want to obtain a talloc report
786 * and then return to the caller, who will abort the process */
787 case SIGUSR1:
788 talloc_report_full(tall_bsc_ctx, stderr);
789 break;
790 default:
791 break;
792 }
793}
794
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800795int main(int argc, char** argv)
796{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100797 int rc;
798
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800799 debug_init();
800 stderr_target = debug_target_create_stderr();
801 debug_add_target(stderr_target);
802 debug_set_all_filter(stderr_target, 1);
803
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800804 /* parse options */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100805 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800806 handle_options(argc, argv);
807
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800808 nat = bsc_nat_alloc();
809 if (!nat) {
810 fprintf(stderr, "Failed to allocate the BSC nat.\n");
811 return -4;
812 }
813
814 /* init vty and parse */
815 bsc_nat_vty_init(nat);
816 telnet_init(NULL, 4244);
817 if (vty_read_config_file(config_file) < 0) {
818 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
819 return -3;
820 }
821
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800822 /* seed the PRNG */
823 srand(time(NULL));
824
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100825 /* connect to the MSC */
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800826 write_queue_init(&msc_queue, 100);
827 msc_queue.read_cb = ipaccess_msc_read_cb;
828 msc_queue.write_cb = ipaccess_msc_write_cb;
829 rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100830 if (rc < 0) {
831 fprintf(stderr, "Opening the MSC connection failed.\n");
832 exit(1);
833 }
834
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100835 /* wait for the BSC */
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +0100836 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100837 fprintf(stderr, "Failed to listen for BSC.\n");
838 exit(1);
839 }
840
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100841 signal(SIGABRT, &signal_handler);
842 signal(SIGUSR1, &signal_handler);
843 signal(SIGPIPE, SIG_IGN);
844
845 while (1) {
846 bsc_select_main(0);
847 }
848
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800849 return 0;
850}