blob: 58462324e526755c2f5699f95e5048a51d7fe173 [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 Freythercd895372010-03-29 08:04:09 +020062static void remove_bsc_connection(struct bsc_connection *connection);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080063
64static struct bsc_nat *bsc_nat_alloc(void)
65{
66 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
67 if (!nat)
68 return NULL;
69
70 INIT_LLIST_HEAD(&nat->sccp_connections);
71 INIT_LLIST_HEAD(&nat->bsc_connections);
72 INIT_LLIST_HEAD(&nat->bsc_configs);
73 return nat;
74}
75
76static struct bsc_connection *bsc_connection_alloc(void)
77{
78 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
79 if (!con)
80 return NULL;
81
82 return con;
83}
84
85struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
86{
87 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
88 if (!conf)
89 return NULL;
90
91 conf->token = talloc_strdup(conf, token);
92 conf->lac = lac;
93 conf->nr = nat->num_bsc;
94 conf->nat = nat;
95
96 llist_add(&conf->entry, &nat->bsc_configs);
97 ++nat->num_bsc;
98
99 return conf;
100}
101
102struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
103{
104 struct bsc_config *conf;
105
106 llist_for_each_entry(conf, &nat->bsc_configs, entry)
107 if (conf->nr == num)
108 return conf;
109
110 return NULL;
111}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100112
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100113/*
114 * below are stubs we need to link
115 */
116int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
117 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
118{
119 return -1;
120}
121
122void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
123{}
124
125int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
126{
127 return -1;
128}
129
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100130static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100131{
132 static const u_int8_t gsm_reset_ack[] = {
133 0x00, 0x13, 0xfd,
134 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
135 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
136 0x00, 0x01, 0x31,
137 };
138
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100139 bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100140}
141
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100142static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100143{
144 static const u_int8_t id_ack[] = {
145 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
146 };
147
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100148 bsc_write(bsc, id_ack, sizeof(id_ack));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100149}
150
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100151static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100152{
153 static const u_int8_t id_req[] = {
154 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
155 0x01, IPAC_IDTAG_UNIT,
156 0x01, IPAC_IDTAG_MACADDR,
157 0x01, IPAC_IDTAG_LOCATION1,
158 0x01, IPAC_IDTAG_LOCATION2,
159 0x01, IPAC_IDTAG_EQUIPVERS,
160 0x01, IPAC_IDTAG_SWVERSION,
161 0x01, IPAC_IDTAG_UNITNAME,
162 0x01, IPAC_IDTAG_SERNR,
163 };
164
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100165 bsc_write(bsc, id_req, sizeof(id_req));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100166}
167
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100168/*
169 * Below is the handling of messages coming
170 * from the MSC and need to be forwarded to
171 * a real BSC.
172 */
173static void initialize_msc_if_needed()
174{
175 static int init = 0;
176 init = 1;
177
178 /* do we need to send a GSM 08.08 message here? */
179}
180
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100181/*
182 * Currently we are lacking refcounting so we need to copy each message.
183 */
184static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100185{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100186 struct msgb *msg;
187
188 if (length > 4096) {
189 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
190 return;
191 }
192
193 msg = msgb_alloc(4096, "to-bsc");
194 if (!msg) {
195 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
196 return;
197 }
198
199 msgb_put(msg, length);
200 memcpy(msg->data, data, length);
201 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
202 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
203 msgb_free(msg);
204 }
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100205}
206
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100207static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100208{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800209 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800210 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100211
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100212 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800213 parsed = bsc_nat_parse(msg);
214 if (!parsed) {
215 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100216 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800217 }
218
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100219 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800220 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800221
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100222 /* Route and modify the SCCP packet */
223 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
224 switch (parsed->sccp_type) {
225 case SCCP_MSG_TYPE_UDT:
226 /* forward UDT messages to every BSC */
227 goto send_to_all;
228 break;
229 case SCCP_MSG_TYPE_RLSD:
230 case SCCP_MSG_TYPE_CREF:
231 case SCCP_MSG_TYPE_DT1:
232 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800233 bsc = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
234 break;
235 case SCCP_MSG_TYPE_RLC:
236 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
237 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100238 break;
239 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100240 /* MSC never opens a SCCP connection, fall through */
241 default:
242 goto exit;
243 }
244 }
245
246 talloc_free(parsed);
247 if (!bsc)
248 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100249 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800250 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100251 return -1;
252 }
253
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100254 bsc_write(bsc, msg->data, msg->len);
255 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100256
257send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800258 /*
259 * Filter Paging from the network. We do not want to send a PAGING
260 * Command to every BSC in our network. We will analys the PAGING
261 * message and then send it to the authenticated messages...
262 */
263 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
264 int data_length;
265 const u_int8_t *data;
266 struct tlv_parsed tp;
267 int i = 0;
268
269 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
270 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
271 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
272 goto exit;
273 }
274
275 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
276 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
277 if (data[0] != CELL_IDENT_LAC) {
278 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
279 goto exit;
280 }
281
282 /* go through each LAC and forward the message */
283 for (i = 1; i < data_length - 1; i += 2) {
284 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
285 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
286 if (!bsc->authenticated || _lac != bsc->lac)
287 continue;
288
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100289 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800290 }
291 }
292
293 goto exit;
294 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100295 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800296 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100297 if (!bsc->authenticated)
298 continue;
299
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100300 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100301 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800302
303exit:
304 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100305 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100306}
307
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800308static void msc_connection_was_lost(struct bsc_msc_connection *con)
309{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200310 struct bsc_connection *bsc, *tmp;
311
312 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
313 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
314 remove_bsc_connection(bsc);
315
316 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800317}
318
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800319static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100320{
321 int error;
322 struct msgb *msg = ipaccess_read_msg(bfd, &error);
323 struct ipaccess_head *hh;
324
325 if (!msg) {
326 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100327 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800328 bsc_msc_lost(msc_con);
329 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100330 }
331
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100332 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100333 return -1;
334 }
335
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100336 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 +0100337
338 /* handle base message handling */
339 hh = (struct ipaccess_head *) msg->data;
340 ipaccess_rcvmsg_base(msg, bfd);
341
342 /* initialize the networking. This includes sending a GSM08.08 message */
343 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
344 initialize_msc_if_needed();
345 else if (hh->proto == IPAC_PROTO_SCCP)
346 forward_sccp_to_bts(msg);
347
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800348 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100349 return 0;
350}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800351
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800352static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
353{
354 int rc;
355 rc = write(bfd->fd, msg->data, msg->len);
356
357 if (rc != msg->len) {
358 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
359 return -1;
360 }
361
362 return rc;
363}
364
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100365/*
366 * Below is the handling of messages coming
367 * from the BSC and need to be forwarded to
368 * a real BSC.
369 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100370
371/*
372 * Remove the connection from the connections list,
373 * remove it from the patching of SCCP header lists
374 * as well. Maybe in the future even close connection..
375 */
376static void remove_bsc_connection(struct bsc_connection *connection)
377{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100378 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800379 bsc_unregister_fd(&connection->write_queue.bfd);
380 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100381 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100382 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100383
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800384 /* stop the timeout timer */
385 bsc_del_timer(&connection->id_timeout);
386
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100387 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800388 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100389 if (sccp_patch->bsc != connection)
390 continue;
391
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100392#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100393 llist_del(&sccp_patch->list_entry);
394 talloc_free(sccp_patch);
395 }
396
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100397 talloc_free(connection);
398}
399
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800400static void ipaccess_close_bsc(void *data)
401{
402 struct bsc_connection *conn = data;
403
404 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
405 remove_bsc_connection(conn);
406}
407
408static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
409{
410 struct bsc_config *conf;
411 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
412
413 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
414 if (strcmp(conf->token, token) == 0) {
415 bsc->authenticated = 1;
416 bsc->lac = conf->lac;
417 bsc_del_timer(&bsc->id_timeout);
418 break;
419 }
420 }
421}
422
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100423static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100424{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100425 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800426 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100427
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800428 /* Parse and filter messages */
429 parsed = bsc_nat_parse(msg);
430 if (!parsed) {
431 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
432 return -1;
433 }
434
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100435 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800436 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800437
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100438 /* modify the SCCP entries */
439 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
440 switch (parsed->sccp_type) {
441 case SCCP_MSG_TYPE_CR:
442 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
443 goto exit2;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800444 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100445 break;
446 case SCCP_MSG_TYPE_RLSD:
447 case SCCP_MSG_TYPE_CREF:
448 case SCCP_MSG_TYPE_DT1:
449 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800450 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100451 break;
452 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800453 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100454 remove_sccp_src_ref(bsc, msg, parsed);
455 break;
456 case SCCP_MSG_TYPE_UDT:
457 /* simply forward everything */
458 break;
459 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800460 LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100461 goto exit2;
462 break;
463 }
464 }
465
466 if (found_bsc != bsc) {
467 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
468 goto exit2;
469 }
470
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100471 if (!bsc->authenticated) {
472 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
473 goto exit2;
474 }
475
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100476 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800477 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800478 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
479 msgb_free(msg);
480 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100481 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800482 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800483
484exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100485 /* if we filter out the reset send an ack to the BSC */
486 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100487 send_reset_ack(bsc);
488 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800489 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
490 /* do we know who is handling this? */
491 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
492 struct tlv_parsed tvp;
493 ipaccess_idtag_parse(&tvp,
494 (unsigned char *) msg->l2h + 2,
495 msgb_l2len(msg) - 2);
496 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
497 ipaccess_auth_bsc(&tvp, bsc);
498 }
499
500 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100501 }
502
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100503exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800504 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800505 msgb_free(msg);
506 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100507}
508
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800509static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100510{
511 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100512 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100513 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100514
515 if (!msg) {
516 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100517 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100518 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100519 } else {
520 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100521 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100522 return -1;
523 }
524
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100525
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100526 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 +0100527
528 /* Handle messages from the BSC */
529 /* FIXME: Currently no PONG is sent to the BSC */
530 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100531 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100532
533 return 0;
534}
535
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100536static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
537{
538 int rc;
539
540 rc = write(bfd->fd, msg->data, msg->len);
541 if (rc != msg->len)
542 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
543
544 return rc;
545}
546
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100547static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
548{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100549 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100550 int ret;
551 struct sockaddr_in sa;
552 socklen_t sa_len = sizeof(sa);
553
554 if (!(what & BSC_FD_READ))
555 return 0;
556
557 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
558 if (ret < 0) {
559 perror("accept");
560 return ret;
561 }
562
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200563 /*
564 * if we are not connected to a msc... just close the socket
565 */
566 if (!msc_con->is_connected) {
567 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
568 return 0;
569 }
570
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100571 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100572 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100573
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100574 /*
575 *
576 */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800577 bsc = bsc_connection_alloc();
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100578 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100579 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100580 close(ret);
581 return -1;
582 }
583
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800584 bsc->nat = nat;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800585 write_queue_init(&bsc->write_queue, 100);
586 bsc->write_queue.bfd.data = bsc;
587 bsc->write_queue.bfd.fd = ret;
588 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100589 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800590 bsc->write_queue.bfd.when = BSC_FD_READ;
591 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100592 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100593 close(ret);
594 talloc_free(bsc);
595 return -2;
596 }
597
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100598 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800599 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100600 send_id_ack(bsc);
601 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800602
603 /*
604 * start the hangup timer
605 */
606 bsc->id_timeout.data = bsc;
607 bsc->id_timeout.cb = ipaccess_close_bsc;
608 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100609 return 0;
610}
611
612static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
613{
614 struct sockaddr_in addr;
615 int ret, on = 1;
616
617 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
618 bfd->cb = ipaccess_listen_bsc_cb;
619 bfd->when = BSC_FD_READ;
620
621 memset(&addr, 0, sizeof(addr));
622 addr.sin_family = AF_INET;
623 addr.sin_port = htons(port);
624 addr.sin_addr.s_addr = in_addr->s_addr;
625
626 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
627
628 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
629 if (ret < 0) {
630 fprintf(stderr, "Could not bind the BSC socket %s\n",
631 strerror(errno));
632 return -EIO;
633 }
634
635 ret = listen(bfd->fd, 1);
636 if (ret < 0) {
637 perror("listen");
638 return ret;
639 }
640
641 ret = bsc_register_fd(bfd);
642 if (ret < 0) {
643 perror("register_listen_fd");
644 return ret;
645 }
646 return 0;
647}
648
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800649static void print_usage()
650{
651 printf("Usage: bsc_nat\n");
652}
653
654static void print_help()
655{
656 printf(" Some useful help...\n");
657 printf(" -h --help this text\n");
658 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
659 printf(" -s --disable-color\n");
660 printf(" -c --config-file filename The config file to use.\n");
661 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100662 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800663}
664
665static void handle_options(int argc, char** argv)
666{
667 while (1) {
668 int option_index = 0, c;
669 static struct option long_options[] = {
670 {"help", 0, 0, 'h'},
671 {"debug", 1, 0, 'd'},
672 {"config-file", 1, 0, 'c'},
673 {"disable-color", 0, 0, 's'},
674 {"timestamp", 0, 0, 'T'},
675 {"msc", 1, 0, 'm'},
676 {"local", 1, 0, 'l'},
677 {0, 0, 0, 0}
678 };
679
680 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
681 long_options, &option_index);
682 if (c == -1)
683 break;
684
685 switch (c) {
686 case 'h':
687 print_usage();
688 print_help();
689 exit(0);
690 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800691 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800692 break;
693 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800694 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800695 break;
696 case 'c':
697 config_file = strdup(optarg);
698 break;
699 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800700 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800701 break;
702 case 'm':
703 msc_address = strdup(optarg);
704 break;
705 case 'l':
706 inet_aton(optarg, &local_addr);
707 break;
708 default:
709 /* ignore */
710 break;
711 }
712 }
713}
714
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100715static void signal_handler(int signal)
716{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100717 switch (signal) {
718 case SIGABRT:
719 /* in case of abort, we want to obtain a talloc report
720 * and then return to the caller, who will abort the process */
721 case SIGUSR1:
722 talloc_report_full(tall_bsc_ctx, stderr);
723 break;
724 default:
725 break;
726 }
727}
728
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800729int main(int argc, char** argv)
730{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100731
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800732 debug_init();
733 stderr_target = debug_target_create_stderr();
734 debug_add_target(stderr_target);
735 debug_set_all_filter(stderr_target, 1);
736
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800737 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100738 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800739 handle_options(argc, argv);
740
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800741 nat = bsc_nat_alloc();
742 if (!nat) {
743 fprintf(stderr, "Failed to allocate the BSC nat.\n");
744 return -4;
745 }
746
747 /* init vty and parse */
748 bsc_nat_vty_init(nat);
749 telnet_init(NULL, 4244);
750 if (vty_read_config_file(config_file) < 0) {
751 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
752 return -3;
753 }
754
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800755 /* seed the PRNG */
756 srand(time(NULL));
757
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100758 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800759 msc_con = bsc_msc_create(msc_address, 5000);
760 if (!msc_con) {
761 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100762 exit(1);
763 }
764
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800765 msc_con->connection_loss = msc_connection_was_lost;
766 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
767 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
768 bsc_msc_connect(msc_con);
769
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100770 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100771 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100772 fprintf(stderr, "Failed to listen for BSC.\n");
773 exit(1);
774 }
775
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100776 signal(SIGABRT, &signal_handler);
777 signal(SIGUSR1, &signal_handler);
778 signal(SIGPIPE, SIG_IGN);
779
780 while (1) {
781 bsc_select_main(0);
782 }
783
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800784 return 0;
785}