blob: c7c6c41350d04c522794ff56021a521be137d889 [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 Freyther49c7fb52010-06-15 18:48:55 +0800171 struct sccp_connections *con;
172 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800173 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100174
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100175 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800176 parsed = bsc_nat_parse(msg);
177 if (!parsed) {
178 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100179 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800180 }
181
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100182 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800183 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800184
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100185 /* Route and modify the SCCP packet */
186 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
187 switch (parsed->sccp_type) {
188 case SCCP_MSG_TYPE_UDT:
189 /* forward UDT messages to every BSC */
190 goto send_to_all;
191 break;
192 case SCCP_MSG_TYPE_RLSD:
193 case SCCP_MSG_TYPE_CREF:
194 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800195 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200196 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100197 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800198 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
199 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200200 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800201 break;
202 case SCCP_MSG_TYPE_RLC:
203 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
204 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100205 break;
206 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100207 /* MSC never opens a SCCP connection, fall through */
208 default:
209 goto exit;
210 }
211 }
212
213 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800214 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100215 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800216 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800217 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100218 return -1;
219 }
220
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800221 bsc_write(con->bsc, msg->data, msg->len);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100222 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100223
224send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800225 /*
226 * Filter Paging from the network. We do not want to send a PAGING
227 * Command to every BSC in our network. We will analys the PAGING
228 * message and then send it to the authenticated messages...
229 */
230 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200231 bsc = bsc_nat_find_bsc(nat, msg);
232 if (bsc)
233 bsc_write(bsc, msg->data, msg->len);
234 else
235 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n");
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800236
237 goto exit;
238 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100239 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800240 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100241 if (!bsc->authenticated)
242 continue;
243
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100244 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100245 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800246
247exit:
248 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100249 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100250}
251
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800252static void msc_connection_was_lost(struct bsc_msc_connection *con)
253{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200254 struct bsc_connection *bsc, *tmp;
255
256 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
257 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
258 remove_bsc_connection(bsc);
259
260 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800261}
262
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800263static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100264{
265 int error;
266 struct msgb *msg = ipaccess_read_msg(bfd, &error);
267 struct ipaccess_head *hh;
268
269 if (!msg) {
270 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100271 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800272 bsc_msc_lost(msc_con);
273 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100274 }
275
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100276 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100277 return -1;
278 }
279
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100280 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 +0100281
282 /* handle base message handling */
283 hh = (struct ipaccess_head *) msg->data;
284 ipaccess_rcvmsg_base(msg, bfd);
285
286 /* initialize the networking. This includes sending a GSM08.08 message */
287 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
288 initialize_msc_if_needed();
289 else if (hh->proto == IPAC_PROTO_SCCP)
290 forward_sccp_to_bts(msg);
291
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800292 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100293 return 0;
294}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800295
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800296static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
297{
298 int rc;
299 rc = write(bfd->fd, msg->data, msg->len);
300
301 if (rc != msg->len) {
302 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
303 return -1;
304 }
305
306 return rc;
307}
308
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100309/*
310 * Below is the handling of messages coming
311 * from the BSC and need to be forwarded to
312 * a real BSC.
313 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100314
315/*
316 * Remove the connection from the connections list,
317 * remove it from the patching of SCCP header lists
318 * as well. Maybe in the future even close connection..
319 */
320static void remove_bsc_connection(struct bsc_connection *connection)
321{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100322 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800323 bsc_unregister_fd(&connection->write_queue.bfd);
324 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100325 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100326 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100327
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800328 /* stop the timeout timer */
329 bsc_del_timer(&connection->id_timeout);
330
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100331 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800332 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100333 if (sccp_patch->bsc != connection)
334 continue;
335
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100336#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200337 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100338 }
339
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100340 talloc_free(connection);
341}
342
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800343static void ipaccess_close_bsc(void *data)
344{
345 struct bsc_connection *conn = data;
346
347 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
348 remove_bsc_connection(conn);
349}
350
351static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
352{
353 struct bsc_config *conf;
354 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
355
356 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
357 if (strcmp(conf->token, token) == 0) {
358 bsc->authenticated = 1;
359 bsc->lac = conf->lac;
360 bsc_del_timer(&bsc->id_timeout);
361 break;
362 }
363 }
364}
365
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100366static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100367{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800368 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800369 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100370
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200371 if (!bsc->authenticated) {
372 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
373 msgb_free(msg);
374 return -1;
375 }
376
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800377 /* Parse and filter messages */
378 parsed = bsc_nat_parse(msg);
379 if (!parsed) {
380 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200381 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800382 return -1;
383 }
384
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100385 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800386 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800387
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100388 /* modify the SCCP entries */
389 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
390 switch (parsed->sccp_type) {
391 case SCCP_MSG_TYPE_CR:
392 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
393 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800394 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100395 break;
396 case SCCP_MSG_TYPE_RLSD:
397 case SCCP_MSG_TYPE_CREF:
398 case SCCP_MSG_TYPE_DT1:
399 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800400 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100401 break;
402 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800403 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100404 remove_sccp_src_ref(bsc, msg, parsed);
405 break;
406 case SCCP_MSG_TYPE_UDT:
407 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800408 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100409 break;
410 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800411 LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800412 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100413 goto exit2;
414 break;
415 }
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800416 } else {
417 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
418 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100419 }
420
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800421 if (con && con->bsc != bsc) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100422 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
423 goto exit2;
424 }
425
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100426 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800427 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800428 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
429 msgb_free(msg);
430 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100431 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800432 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800433
434exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100435 /* if we filter out the reset send an ack to the BSC */
436 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100437 send_reset_ack(bsc);
438 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800439 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
440 /* do we know who is handling this? */
441 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
442 struct tlv_parsed tvp;
443 ipaccess_idtag_parse(&tvp,
444 (unsigned char *) msg->l2h + 2,
445 msgb_l2len(msg) - 2);
446 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
447 ipaccess_auth_bsc(&tvp, bsc);
448 }
449
450 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100451 }
452
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100453exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800454 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800455 msgb_free(msg);
456 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100457}
458
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800459static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100460{
461 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100462 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100463 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100464
465 if (!msg) {
466 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100467 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100468 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100469 } else {
470 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100471 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100472 return -1;
473 }
474
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100475
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100476 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 +0100477
478 /* Handle messages from the BSC */
479 /* FIXME: Currently no PONG is sent to the BSC */
480 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100481 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100482
483 return 0;
484}
485
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100486static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
487{
488 int rc;
489
490 rc = write(bfd->fd, msg->data, msg->len);
491 if (rc != msg->len)
492 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
493
494 return rc;
495}
496
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100497static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
498{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100499 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100500 int ret;
501 struct sockaddr_in sa;
502 socklen_t sa_len = sizeof(sa);
503
504 if (!(what & BSC_FD_READ))
505 return 0;
506
507 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
508 if (ret < 0) {
509 perror("accept");
510 return ret;
511 }
512
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200513 /*
514 * if we are not connected to a msc... just close the socket
515 */
516 if (!msc_con->is_connected) {
517 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
518 return 0;
519 }
520
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100521 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100522 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100523
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100524 /*
525 *
526 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800527 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100528 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100529 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100530 close(ret);
531 return -1;
532 }
533
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800534 write_queue_init(&bsc->write_queue, 100);
535 bsc->write_queue.bfd.data = bsc;
536 bsc->write_queue.bfd.fd = ret;
537 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100538 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800539 bsc->write_queue.bfd.when = BSC_FD_READ;
540 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100541 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100542 close(ret);
543 talloc_free(bsc);
544 return -2;
545 }
546
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100547 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800548 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100549 send_id_ack(bsc);
550 send_id_req(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800551
552 /*
553 * start the hangup timer
554 */
555 bsc->id_timeout.data = bsc;
556 bsc->id_timeout.cb = ipaccess_close_bsc;
557 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100558 return 0;
559}
560
561static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
562{
563 struct sockaddr_in addr;
564 int ret, on = 1;
565
566 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
567 bfd->cb = ipaccess_listen_bsc_cb;
568 bfd->when = BSC_FD_READ;
569
570 memset(&addr, 0, sizeof(addr));
571 addr.sin_family = AF_INET;
572 addr.sin_port = htons(port);
573 addr.sin_addr.s_addr = in_addr->s_addr;
574
575 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
576
577 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
578 if (ret < 0) {
579 fprintf(stderr, "Could not bind the BSC socket %s\n",
580 strerror(errno));
581 return -EIO;
582 }
583
584 ret = listen(bfd->fd, 1);
585 if (ret < 0) {
586 perror("listen");
587 return ret;
588 }
589
590 ret = bsc_register_fd(bfd);
591 if (ret < 0) {
592 perror("register_listen_fd");
593 return ret;
594 }
595 return 0;
596}
597
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800598static void print_usage()
599{
600 printf("Usage: bsc_nat\n");
601}
602
603static void print_help()
604{
605 printf(" Some useful help...\n");
606 printf(" -h --help this text\n");
607 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
608 printf(" -s --disable-color\n");
609 printf(" -c --config-file filename The config file to use.\n");
610 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100611 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800612}
613
614static void handle_options(int argc, char** argv)
615{
616 while (1) {
617 int option_index = 0, c;
618 static struct option long_options[] = {
619 {"help", 0, 0, 'h'},
620 {"debug", 1, 0, 'd'},
621 {"config-file", 1, 0, 'c'},
622 {"disable-color", 0, 0, 's'},
623 {"timestamp", 0, 0, 'T'},
624 {"msc", 1, 0, 'm'},
625 {"local", 1, 0, 'l'},
626 {0, 0, 0, 0}
627 };
628
629 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
630 long_options, &option_index);
631 if (c == -1)
632 break;
633
634 switch (c) {
635 case 'h':
636 print_usage();
637 print_help();
638 exit(0);
639 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800640 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800641 break;
642 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800643 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800644 break;
645 case 'c':
646 config_file = strdup(optarg);
647 break;
648 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800649 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800650 break;
651 case 'm':
652 msc_address = strdup(optarg);
653 break;
654 case 'l':
655 inet_aton(optarg, &local_addr);
656 break;
657 default:
658 /* ignore */
659 break;
660 }
661 }
662}
663
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100664static void signal_handler(int signal)
665{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100666 switch (signal) {
667 case SIGABRT:
668 /* in case of abort, we want to obtain a talloc report
669 * and then return to the caller, who will abort the process */
670 case SIGUSR1:
671 talloc_report_full(tall_bsc_ctx, stderr);
672 break;
673 default:
674 break;
675 }
676}
677
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800678int main(int argc, char** argv)
679{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100680
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800681 debug_init();
682 stderr_target = debug_target_create_stderr();
683 debug_add_target(stderr_target);
684 debug_set_all_filter(stderr_target, 1);
685
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800686 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100687 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800688 handle_options(argc, argv);
689
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800690 nat = bsc_nat_alloc();
691 if (!nat) {
692 fprintf(stderr, "Failed to allocate the BSC nat.\n");
693 return -4;
694 }
695
696 /* init vty and parse */
697 bsc_nat_vty_init(nat);
698 telnet_init(NULL, 4244);
699 if (vty_read_config_file(config_file) < 0) {
700 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
701 return -3;
702 }
703
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800704 /* seed the PRNG */
705 srand(time(NULL));
706
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100707 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800708 msc_con = bsc_msc_create(msc_address, 5000);
709 if (!msc_con) {
710 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100711 exit(1);
712 }
713
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800714 msc_con->connection_loss = msc_connection_was_lost;
715 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
716 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
717 bsc_msc_connect(msc_con);
718
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100719 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100720 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100721 fprintf(stderr, "Failed to listen for BSC.\n");
722 exit(1);
723 }
724
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100725 signal(SIGABRT, &signal_handler);
726 signal(SIGUSR1, &signal_handler);
727 signal(SIGPIPE, SIG_IGN);
728
729 while (1) {
730 bsc_select_main(0);
731 }
732
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800733 return 0;
734}