blob: a86895774decbf7578d971805ae9ad6a87dfc217 [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 Freytherd131b792010-03-31 07:30:58 +0200130static void send_mgcp_reset(struct bsc_connection *bsc)
131{
132 static const u_int8_t mgcp_reset[] = {
133 "RSIP 1 13@mgw MGCP 1.0\r\n"
134 };
135
136 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
137}
138
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100139/*
140 * Below is the handling of messages coming
141 * from the MSC and need to be forwarded to
142 * a real BSC.
143 */
144static void initialize_msc_if_needed()
145{
146 static int init = 0;
147 init = 1;
148
149 /* do we need to send a GSM 08.08 message here? */
150}
151
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100152/*
153 * Currently we are lacking refcounting so we need to copy each message.
154 */
155static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100156{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100157 struct msgb *msg;
158
159 if (length > 4096) {
160 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
161 return;
162 }
163
164 msg = msgb_alloc(4096, "to-bsc");
165 if (!msg) {
166 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
167 return;
168 }
169
170 msgb_put(msg, length);
171 memcpy(msg->data, data, length);
172 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
173 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
174 msgb_free(msg);
175 }
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100176}
177
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200178int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
179{
180 struct msgb *msg;
181
182 if (length > 4096 - 128) {
183 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
184 return -1;
185 }
186
187 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
188 if (!msg) {
189 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
190 return -1;
191 }
192
193 /* copy the data */
194 msg->l3h = msgb_put(msg, length);
195 memcpy(msg->l3h, data, length);
196
197 /* prepend the header */
198 ipaccess_prepend_header(msg, NAT_IPAC_PROTO_MGCP);
199
200 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
201 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
202 msgb_free(msg);
203 return -1;
204 }
205
206 return 0;
207}
208
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100209static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100210{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800211 struct sccp_connections *con;
212 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800213 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100214
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100215 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800216 parsed = bsc_nat_parse(msg);
217 if (!parsed) {
218 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100219 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800220 }
221
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100222 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800223 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800224
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100225 /* Route and modify the SCCP packet */
226 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
227 switch (parsed->sccp_type) {
228 case SCCP_MSG_TYPE_UDT:
229 /* forward UDT messages to every BSC */
230 goto send_to_all;
231 break;
232 case SCCP_MSG_TYPE_RLSD:
233 case SCCP_MSG_TYPE_CREF:
234 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800235 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800236 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
237 if (con) {
238 if (bsc_mgcp_assign(con, msg) != 0)
239 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
240 } else
241 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
242 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200243 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100244 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800245 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
246 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200247 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800248 break;
249 case SCCP_MSG_TYPE_RLC:
250 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
251 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100252 break;
253 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100254 /* MSC never opens a SCCP connection, fall through */
255 default:
256 goto exit;
257 }
258 }
259
260 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800261 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100262 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800263 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800264 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100265 return -1;
266 }
267
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800268 bsc_write(con->bsc, msg->data, msg->len);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100269 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100270
271send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800272 /*
273 * Filter Paging from the network. We do not want to send a PAGING
274 * Command to every BSC in our network. We will analys the PAGING
275 * message and then send it to the authenticated messages...
276 */
277 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200278 bsc = bsc_nat_find_bsc(nat, msg);
279 if (bsc)
280 bsc_write(bsc, msg->data, msg->len);
281 else
282 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n");
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800283
284 goto exit;
285 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100286 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800287 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100288 if (!bsc->authenticated)
289 continue;
290
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100291 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100292 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800293
294exit:
295 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100296 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100297}
298
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800299static void msc_connection_was_lost(struct bsc_msc_connection *con)
300{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200301 struct bsc_connection *bsc, *tmp;
302
303 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
304 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
305 remove_bsc_connection(bsc);
306
307 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800308}
309
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800310static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100311{
312 int error;
313 struct msgb *msg = ipaccess_read_msg(bfd, &error);
314 struct ipaccess_head *hh;
315
316 if (!msg) {
317 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100318 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800319 bsc_msc_lost(msc_con);
320 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100321 }
322
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100323 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100324 return -1;
325 }
326
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100327 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 +0100328
329 /* handle base message handling */
330 hh = (struct ipaccess_head *) msg->data;
331 ipaccess_rcvmsg_base(msg, bfd);
332
333 /* initialize the networking. This includes sending a GSM08.08 message */
334 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
335 initialize_msc_if_needed();
336 else if (hh->proto == IPAC_PROTO_SCCP)
337 forward_sccp_to_bts(msg);
338
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800339 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100340 return 0;
341}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800342
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800343static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
344{
345 int rc;
346 rc = write(bfd->fd, msg->data, msg->len);
347
348 if (rc != msg->len) {
349 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
350 return -1;
351 }
352
353 return rc;
354}
355
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100356/*
357 * Below is the handling of messages coming
358 * from the BSC and need to be forwarded to
359 * a real BSC.
360 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100361
362/*
363 * Remove the connection from the connections list,
364 * remove it from the patching of SCCP header lists
365 * as well. Maybe in the future even close connection..
366 */
367static void remove_bsc_connection(struct bsc_connection *connection)
368{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100369 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800370 bsc_unregister_fd(&connection->write_queue.bfd);
371 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100372 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100373 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100374
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800375 /* stop the timeout timer */
376 bsc_del_timer(&connection->id_timeout);
377
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100378 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800379 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100380 if (sccp_patch->bsc != connection)
381 continue;
382
Holger Hans Peter Freyther7c99d4f2010-03-26 09:28:40 +0100383#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200384 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100385 }
386
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100387 talloc_free(connection);
388}
389
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800390static void ipaccess_close_bsc(void *data)
391{
392 struct bsc_connection *conn = data;
393
394 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
395 remove_bsc_connection(conn);
396}
397
398static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
399{
400 struct bsc_config *conf;
401 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
402
403 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
404 if (strcmp(conf->token, token) == 0) {
405 bsc->authenticated = 1;
406 bsc->lac = conf->lac;
407 bsc_del_timer(&bsc->id_timeout);
408 break;
409 }
410 }
411}
412
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100413static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100414{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800415 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800416 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100417
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200418 if (!bsc->authenticated) {
419 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
420 msgb_free(msg);
421 return -1;
422 }
423
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800424 /* Parse and filter messages */
425 parsed = bsc_nat_parse(msg);
426 if (!parsed) {
427 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200428 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800429 return -1;
430 }
431
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100432 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800433 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800434
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100435 /* modify the SCCP entries */
436 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
437 switch (parsed->sccp_type) {
438 case SCCP_MSG_TYPE_CR:
439 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
440 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800441 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100442 break;
443 case SCCP_MSG_TYPE_RLSD:
444 case SCCP_MSG_TYPE_CREF:
445 case SCCP_MSG_TYPE_DT1:
446 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800447 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100448 break;
449 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800450 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200451 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100452 break;
453 case SCCP_MSG_TYPE_UDT:
454 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800455 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100456 break;
457 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800458 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 +0800459 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100460 goto exit2;
461 break;
462 }
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800463 } else {
464 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
465 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100466 }
467
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800468 if (con && con->bsc != bsc) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100469 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
470 goto exit2;
471 }
472
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100473 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800474 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800475 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
476 msgb_free(msg);
477 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100478 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800479 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800480
481exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100482 /* if we filter out the reset send an ack to the BSC */
483 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100484 send_reset_ack(bsc);
485 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800486 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
487 /* do we know who is handling this? */
488 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
489 struct tlv_parsed tvp;
490 ipaccess_idtag_parse(&tvp,
491 (unsigned char *) msg->l2h + 2,
492 msgb_l2len(msg) - 2);
493 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
494 ipaccess_auth_bsc(&tvp, bsc);
495 }
496
497 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100498 }
499
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100500exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800501 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800502 msgb_free(msg);
503 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100504}
505
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800506static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100507{
508 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100509 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100510 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100511
512 if (!msg) {
513 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100514 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100515 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100516 } else {
517 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100518 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100519 return -1;
520 }
521
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100522
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100523 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 +0100524
525 /* Handle messages from the BSC */
526 /* FIXME: Currently no PONG is sent to the BSC */
527 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100528 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100529
530 return 0;
531}
532
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100533static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
534{
535 int rc;
536
537 rc = write(bfd->fd, msg->data, msg->len);
538 if (rc != msg->len)
539 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
540
541 return rc;
542}
543
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100544static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
545{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100546 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100547 int ret;
548 struct sockaddr_in sa;
549 socklen_t sa_len = sizeof(sa);
550
551 if (!(what & BSC_FD_READ))
552 return 0;
553
554 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
555 if (ret < 0) {
556 perror("accept");
557 return ret;
558 }
559
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200560 /*
561 * if we are not connected to a msc... just close the socket
562 */
563 if (!msc_con->is_connected) {
564 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
565 return 0;
566 }
567
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100568 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100569 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100570
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100571 /*
572 *
573 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800574 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100575 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100576 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100577 close(ret);
578 return -1;
579 }
580
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800581 write_queue_init(&bsc->write_queue, 100);
582 bsc->write_queue.bfd.data = bsc;
583 bsc->write_queue.bfd.fd = ret;
584 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100585 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800586 bsc->write_queue.bfd.when = BSC_FD_READ;
587 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100588 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100589 close(ret);
590 talloc_free(bsc);
591 return -2;
592 }
593
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100594 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800595 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100596 send_id_ack(bsc);
597 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200598 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800599
600 /*
601 * start the hangup timer
602 */
603 bsc->id_timeout.data = bsc;
604 bsc->id_timeout.cb = ipaccess_close_bsc;
605 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100606 return 0;
607}
608
609static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
610{
611 struct sockaddr_in addr;
612 int ret, on = 1;
613
614 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
615 bfd->cb = ipaccess_listen_bsc_cb;
616 bfd->when = BSC_FD_READ;
617
618 memset(&addr, 0, sizeof(addr));
619 addr.sin_family = AF_INET;
620 addr.sin_port = htons(port);
621 addr.sin_addr.s_addr = in_addr->s_addr;
622
623 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
624
625 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
626 if (ret < 0) {
627 fprintf(stderr, "Could not bind the BSC socket %s\n",
628 strerror(errno));
629 return -EIO;
630 }
631
632 ret = listen(bfd->fd, 1);
633 if (ret < 0) {
634 perror("listen");
635 return ret;
636 }
637
638 ret = bsc_register_fd(bfd);
639 if (ret < 0) {
640 perror("register_listen_fd");
641 return ret;
642 }
643 return 0;
644}
645
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800646static void print_usage()
647{
648 printf("Usage: bsc_nat\n");
649}
650
651static void print_help()
652{
653 printf(" Some useful help...\n");
654 printf(" -h --help this text\n");
655 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
656 printf(" -s --disable-color\n");
657 printf(" -c --config-file filename The config file to use.\n");
658 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100659 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800660}
661
662static void handle_options(int argc, char** argv)
663{
664 while (1) {
665 int option_index = 0, c;
666 static struct option long_options[] = {
667 {"help", 0, 0, 'h'},
668 {"debug", 1, 0, 'd'},
669 {"config-file", 1, 0, 'c'},
670 {"disable-color", 0, 0, 's'},
671 {"timestamp", 0, 0, 'T'},
672 {"msc", 1, 0, 'm'},
673 {"local", 1, 0, 'l'},
674 {0, 0, 0, 0}
675 };
676
677 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
678 long_options, &option_index);
679 if (c == -1)
680 break;
681
682 switch (c) {
683 case 'h':
684 print_usage();
685 print_help();
686 exit(0);
687 case 's':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800688 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800689 break;
690 case 'd':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800691 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800692 break;
693 case 'c':
694 config_file = strdup(optarg);
695 break;
696 case 'T':
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800697 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800698 break;
699 case 'm':
700 msc_address = strdup(optarg);
701 break;
702 case 'l':
703 inet_aton(optarg, &local_addr);
704 break;
705 default:
706 /* ignore */
707 break;
708 }
709 }
710}
711
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100712static void signal_handler(int signal)
713{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100714 switch (signal) {
715 case SIGABRT:
716 /* in case of abort, we want to obtain a talloc report
717 * and then return to the caller, who will abort the process */
718 case SIGUSR1:
719 talloc_report_full(tall_bsc_ctx, stderr);
720 break;
721 default:
722 break;
723 }
724}
725
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800726int main(int argc, char** argv)
727{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100728
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800729 debug_init();
730 stderr_target = debug_target_create_stderr();
731 debug_add_target(stderr_target);
732 debug_set_all_filter(stderr_target, 1);
733
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800734 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100735 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800736 handle_options(argc, argv);
737
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800738 nat = bsc_nat_alloc();
739 if (!nat) {
740 fprintf(stderr, "Failed to allocate the BSC nat.\n");
741 return -4;
742 }
743
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800744 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
745
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800746 /* init vty and parse */
747 bsc_nat_vty_init(nat);
748 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800749 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800750 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
751 return -3;
752 }
753
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800754 /* seed the PRNG */
755 srand(time(NULL));
756
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100757 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800758 msc_con = bsc_msc_create(msc_address, 5000);
759 if (!msc_con) {
760 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100761 exit(1);
762 }
763
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800764 msc_con->connection_loss = msc_connection_was_lost;
765 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
766 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
767 bsc_msc_connect(msc_con);
768
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100769 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100770 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100771 fprintf(stderr, "Failed to listen for BSC.\n");
772 exit(1);
773 }
774
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100775 signal(SIGABRT, &signal_handler);
776 signal(SIGUSR1, &signal_handler);
777 signal(SIGPIPE, SIG_IGN);
778
779 while (1) {
780 bsc_select_main(0);
781 }
782
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800783 return 0;
784}