blob: cafca8352ba9878a9b89b5215d0737cd9052727b [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
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
65{
66 struct bsc_config *conf;
67
68 llist_for_each_entry(conf, &nat->bsc_configs, entry)
69 if (conf->nr == num)
70 return conf;
71
72 return NULL;
73}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010074
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010075/*
76 * below are stubs we need to link
77 */
78int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
79 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
80{
81 return -1;
82}
83
84void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
85{}
86
87int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
88{
89 return -1;
90}
91
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +010092static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010093{
94 static const u_int8_t gsm_reset_ack[] = {
95 0x00, 0x13, 0xfd,
96 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
97 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
98 0x00, 0x01, 0x31,
99 };
100
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100101 bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100102}
103
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100104static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100105{
106 static const u_int8_t id_ack[] = {
107 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
108 };
109
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100110 bsc_write(bsc, id_ack, sizeof(id_ack));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100111}
112
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100113static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100114{
115 static const u_int8_t id_req[] = {
116 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
117 0x01, IPAC_IDTAG_UNIT,
118 0x01, IPAC_IDTAG_MACADDR,
119 0x01, IPAC_IDTAG_LOCATION1,
120 0x01, IPAC_IDTAG_LOCATION2,
121 0x01, IPAC_IDTAG_EQUIPVERS,
122 0x01, IPAC_IDTAG_SWVERSION,
123 0x01, IPAC_IDTAG_UNITNAME,
124 0x01, IPAC_IDTAG_SERNR,
125 };
126
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100127 bsc_write(bsc, id_req, sizeof(id_req));
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100128}
129
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100130/*
131 * Below is the handling of messages coming
132 * from the MSC and need to be forwarded to
133 * a real BSC.
134 */
135static void initialize_msc_if_needed()
136{
137 static int init = 0;
138 init = 1;
139
140 /* do we need to send a GSM 08.08 message here? */
141}
142
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100143/*
144 * Currently we are lacking refcounting so we need to copy each message.
145 */
146static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100147{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100148 struct msgb *msg;
149
150 if (length > 4096) {
151 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
152 return;
153 }
154
155 msg = msgb_alloc(4096, "to-bsc");
156 if (!msg) {
157 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
158 return;
159 }
160
161 msgb_put(msg, length);
162 memcpy(msg->data, data, length);
163 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
164 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
165 msgb_free(msg);
166 }
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100167}
168
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100169static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100170{
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800171 struct bsc_connection *bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800172 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100173
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100174 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800175 parsed = bsc_nat_parse(msg);
176 if (!parsed) {
177 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100178 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800179 }
180
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100181 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800182 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800183
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100184 /* Route and modify the SCCP packet */
185 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
186 switch (parsed->sccp_type) {
187 case SCCP_MSG_TYPE_UDT:
188 /* forward UDT messages to every BSC */
189 goto send_to_all;
190 break;
191 case SCCP_MSG_TYPE_RLSD:
192 case SCCP_MSG_TYPE_CREF:
193 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200194 bsc = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
195 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100196 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200197 if (update_sccp_src_ref(bsc, msg, parsed) != 0)
198 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800199 bsc = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
200 break;
201 case SCCP_MSG_TYPE_RLC:
202 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
203 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100204 break;
205 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100206 /* MSC never opens a SCCP connection, fall through */
207 default:
208 goto exit;
209 }
210 }
211
212 talloc_free(parsed);
213 if (!bsc)
214 return -1;
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100215 if (!bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800216 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100217 return -1;
218 }
219
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100220 bsc_write(bsc, msg->data, msg->len);
221 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100222
223send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800224 /*
225 * Filter Paging from the network. We do not want to send a PAGING
226 * Command to every BSC in our network. We will analys the PAGING
227 * message and then send it to the authenticated messages...
228 */
229 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
230 int data_length;
231 const u_int8_t *data;
232 struct tlv_parsed tp;
233 int i = 0;
234
235 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
236 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
237 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
238 goto exit;
239 }
240
241 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
242 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
243 if (data[0] != CELL_IDENT_LAC) {
244 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]);
245 goto exit;
246 }
247
248 /* go through each LAC and forward the message */
249 for (i = 1; i < data_length - 1; i += 2) {
250 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
251 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
252 if (!bsc->authenticated || _lac != bsc->lac)
253 continue;
254
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100255 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800256 }
257 }
258
259 goto exit;
260 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100261 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800262 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100263 if (!bsc->authenticated)
264 continue;
265
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100266 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100267 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800268
269exit:
270 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100271 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100272}
273
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800274static void msc_connection_was_lost(struct bsc_msc_connection *con)
275{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200276 struct bsc_connection *bsc, *tmp;
277
278 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
279 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
280 remove_bsc_connection(bsc);
281
282 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800283}
284
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800285static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100286{
287 int error;
288 struct msgb *msg = ipaccess_read_msg(bfd, &error);
289 struct ipaccess_head *hh;
290
291 if (!msg) {
292 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100293 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800294 bsc_msc_lost(msc_con);
295 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100296 }
297
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100298 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100299 return -1;
300 }
301
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100302 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 +0100303
304 /* handle base message handling */
305 hh = (struct ipaccess_head *) msg->data;
306 ipaccess_rcvmsg_base(msg, bfd);
307
308 /* initialize the networking. This includes sending a GSM08.08 message */
309 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
310 initialize_msc_if_needed();
311 else if (hh->proto == IPAC_PROTO_SCCP)
312 forward_sccp_to_bts(msg);
313
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800314 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100315 return 0;
316}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800317
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800318static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
319{
320 int rc;
321 rc = write(bfd->fd, msg->data, msg->len);
322
323 if (rc != msg->len) {
324 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
325 return -1;
326 }
327
328 return rc;
329}
330
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100331/*
332 * Below is the handling of messages coming
333 * from the BSC and need to be forwarded to
334 * a real BSC.
335 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100336
337/*
338 * Remove the connection from the connections list,
339 * remove it from the patching of SCCP header lists
340 * as well. Maybe in the future even close connection..
341 */
342static void remove_bsc_connection(struct bsc_connection *connection)
343{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100344 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800345 bsc_unregister_fd(&connection->write_queue.bfd);
346 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100347 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100348 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100349
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800350 /* stop the timeout timer */
351 bsc_del_timer(&connection->id_timeout);
352
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100353 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800354 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100355 if (sccp_patch->bsc != connection)
356 continue;
357
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100358#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100359 llist_del(&sccp_patch->list_entry);
360 talloc_free(sccp_patch);
361 }
362
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100363 talloc_free(connection);
364}
365
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800366static void ipaccess_close_bsc(void *data)
367{
368 struct bsc_connection *conn = data;
369
370 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
371 remove_bsc_connection(conn);
372}
373
374static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
375{
376 struct bsc_config *conf;
377 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
378
379 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
380 if (strcmp(conf->token, token) == 0) {
381 bsc->authenticated = 1;
382 bsc->lac = conf->lac;
383 bsc_del_timer(&bsc->id_timeout);
384 break;
385 }
386 }
387}
388
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100389static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100390{
Holger Hans Peter Freyther7c11d1d2010-02-09 16:30:53 +0100391 struct bsc_connection *found_bsc = NULL;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800392 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100393
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800394 /* Parse and filter messages */
395 parsed = bsc_nat_parse(msg);
396 if (!parsed) {
397 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
398 return -1;
399 }
400
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100401 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800402 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800403
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100404 /* modify the SCCP entries */
405 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
406 switch (parsed->sccp_type) {
407 case SCCP_MSG_TYPE_CR:
408 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
409 goto exit2;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800410 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100411 break;
412 case SCCP_MSG_TYPE_RLSD:
413 case SCCP_MSG_TYPE_CREF:
414 case SCCP_MSG_TYPE_DT1:
415 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800416 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100417 break;
418 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800419 found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100420 remove_sccp_src_ref(bsc, msg, parsed);
421 break;
422 case SCCP_MSG_TYPE_UDT:
423 /* simply forward everything */
424 break;
425 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800426 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 +0100427 goto exit2;
428 break;
429 }
430 }
431
432 if (found_bsc != bsc) {
433 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
434 goto exit2;
435 }
436
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100437 if (!bsc->authenticated) {
438 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
439 goto exit2;
440 }
441
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100442 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800443 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800444 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
445 msgb_free(msg);
446 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100447 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800448 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800449
450exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100451 /* if we filter out the reset send an ack to the BSC */
452 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100453 send_reset_ack(bsc);
454 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800455 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
456 /* do we know who is handling this? */
457 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
458 struct tlv_parsed tvp;
459 ipaccess_idtag_parse(&tvp,
460 (unsigned char *) msg->l2h + 2,
461 msgb_l2len(msg) - 2);
462 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
463 ipaccess_auth_bsc(&tvp, bsc);
464 }
465
466 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100467 }
468
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100469exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800470 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800471 msgb_free(msg);
472 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100473}
474
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800475static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100476{
477 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100478 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100479 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100480
481 if (!msg) {
482 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100483 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100484 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100485 } else {
486 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100487 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100488 return -1;
489 }
490
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100491
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100492 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 +0100493
494 /* Handle messages from the BSC */
495 /* FIXME: Currently no PONG is sent to the BSC */
496 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100497 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100498
499 return 0;
500}
501
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100502static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
503{
504 int rc;
505
506 rc = write(bfd->fd, msg->data, msg->len);
507 if (rc != msg->len)
508 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
509
510 return rc;
511}
512
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100513static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
514{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100515 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100516 int ret;
517 struct sockaddr_in sa;
518 socklen_t sa_len = sizeof(sa);
519
520 if (!(what & BSC_FD_READ))
521 return 0;
522
523 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
524 if (ret < 0) {
525 perror("accept");
526 return ret;
527 }
528
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200529 /*
530 * if we are not connected to a msc... just close the socket
531 */
532 if (!msc_con->is_connected) {
533 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
534 return 0;
535 }
536
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100537 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100538 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100539
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100540 /*
541 *
542 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800543 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100544 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100545 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100546 close(ret);
547 return -1;
548 }
549
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800550 write_queue_init(&bsc->write_queue, 100);
551 bsc->write_queue.bfd.data = bsc;
552 bsc->write_queue.bfd.fd = ret;
553 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100554 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800555 bsc->write_queue.bfd.when = BSC_FD_READ;
556 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100557 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100558 close(ret);
559 talloc_free(bsc);
560 return -2;
561 }
562
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100563 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800564 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100565 send_id_ack(bsc);
566 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800567
568 /*
569 * start the hangup timer
570 */
571 bsc->id_timeout.data = bsc;
572 bsc->id_timeout.cb = ipaccess_close_bsc;
573 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100574 return 0;
575}
576
577static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
578{
579 struct sockaddr_in addr;
580 int ret, on = 1;
581
582 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
583 bfd->cb = ipaccess_listen_bsc_cb;
584 bfd->when = BSC_FD_READ;
585
586 memset(&addr, 0, sizeof(addr));
587 addr.sin_family = AF_INET;
588 addr.sin_port = htons(port);
589 addr.sin_addr.s_addr = in_addr->s_addr;
590
591 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
592
593 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
594 if (ret < 0) {
595 fprintf(stderr, "Could not bind the BSC socket %s\n",
596 strerror(errno));
597 return -EIO;
598 }
599
600 ret = listen(bfd->fd, 1);
601 if (ret < 0) {
602 perror("listen");
603 return ret;
604 }
605
606 ret = bsc_register_fd(bfd);
607 if (ret < 0) {
608 perror("register_listen_fd");
609 return ret;
610 }
611 return 0;
612}
613
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800614static void print_usage()
615{
616 printf("Usage: bsc_nat\n");
617}
618
619static void print_help()
620{
621 printf(" Some useful help...\n");
622 printf(" -h --help this text\n");
623 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
624 printf(" -s --disable-color\n");
625 printf(" -c --config-file filename The config file to use.\n");
626 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100627 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800628}
629
630static void handle_options(int argc, char** argv)
631{
632 while (1) {
633 int option_index = 0, c;
634 static struct option long_options[] = {
635 {"help", 0, 0, 'h'},
636 {"debug", 1, 0, 'd'},
637 {"config-file", 1, 0, 'c'},
638 {"disable-color", 0, 0, 's'},
639 {"timestamp", 0, 0, 'T'},
640 {"msc", 1, 0, 'm'},
641 {"local", 1, 0, 'l'},
642 {0, 0, 0, 0}
643 };
644
645 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
646 long_options, &option_index);
647 if (c == -1)
648 break;
649
650 switch (c) {
651 case 'h':
652 print_usage();
653 print_help();
654 exit(0);
655 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800656 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800657 break;
658 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800659 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800660 break;
661 case 'c':
662 config_file = strdup(optarg);
663 break;
664 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800665 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800666 break;
667 case 'm':
668 msc_address = strdup(optarg);
669 break;
670 case 'l':
671 inet_aton(optarg, &local_addr);
672 break;
673 default:
674 /* ignore */
675 break;
676 }
677 }
678}
679
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100680static void signal_handler(int signal)
681{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100682 switch (signal) {
683 case SIGABRT:
684 /* in case of abort, we want to obtain a talloc report
685 * and then return to the caller, who will abort the process */
686 case SIGUSR1:
687 talloc_report_full(tall_bsc_ctx, stderr);
688 break;
689 default:
690 break;
691 }
692}
693
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800694int main(int argc, char** argv)
695{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100696
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800697 debug_init();
698 stderr_target = debug_target_create_stderr();
699 debug_add_target(stderr_target);
700 debug_set_all_filter(stderr_target, 1);
701
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800702 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100703 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800704 handle_options(argc, argv);
705
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800706 nat = bsc_nat_alloc();
707 if (!nat) {
708 fprintf(stderr, "Failed to allocate the BSC nat.\n");
709 return -4;
710 }
711
712 /* init vty and parse */
713 bsc_nat_vty_init(nat);
714 telnet_init(NULL, 4244);
715 if (vty_read_config_file(config_file) < 0) {
716 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
717 return -3;
718 }
719
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800720 /* seed the PRNG */
721 srand(time(NULL));
722
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100723 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800724 msc_con = bsc_msc_create(msc_address, 5000);
725 if (!msc_con) {
726 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100727 exit(1);
728 }
729
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800730 msc_con->connection_loss = msc_connection_was_lost;
731 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
732 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
733 bsc_msc_connect(msc_con);
734
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100735 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100736 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100737 fprintf(stderr, "Failed to listen for BSC.\n");
738 exit(1);
739 }
740
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100741 signal(SIGABRT, &signal_handler);
742 signal(SIGUSR1, &signal_handler);
743 signal(SIGPIPE, SIG_IGN);
744
745 while (1) {
746 bsc_select_main(0);
747 }
748
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800749 return 0;
750}