blob: 8897cc39f59a7b42e61953f972a1f5c259d8a1db [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 Freytherfb7a9342010-06-15 19:14:12 +080052struct log_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 Freyther2896df72010-04-08 10:24:57 +020061static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +020062static void remove_bsc_connection(struct bsc_connection *connection);
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +020063static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080065struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
66{
67 struct bsc_config *conf;
68
69 llist_for_each_entry(conf, &nat->bsc_configs, entry)
70 if (conf->nr == num)
71 return conf;
72
73 return NULL;
74}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010075
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010076/*
77 * below are stubs we need to link
78 */
79int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
80 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
81{
82 return -1;
83}
84
85void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
86{}
87
88int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
89{
90 return -1;
91}
92
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +010093static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010094{
95 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010096 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 Freyther2896df72010-04-08 10:24:57 +0200101 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
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[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200107 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100108 };
109
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200110 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
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[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200116 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100117 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 Freyther2896df72010-04-08 10:24:57 +0200127 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100128}
129
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200130static void nat_send_rlsd(struct sccp_connections *conn)
131{
132 struct sccp_connection_released *rel;
133 struct msgb *msg;
134
135 msg = msgb_alloc_headroom(4096, 128, "rlsd");
136 if (!msg) {
137 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
138 return;
139 }
140
141 msg->l2h = msgb_put(msg, sizeof(*rel));
142 rel = (struct sccp_connection_released *) msg->l2h;
143 rel->type = SCCP_MSG_TYPE_RLSD;
144 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
145 rel->destination_local_reference = conn->remote_ref;
146 rel->source_local_reference = conn->patched_ref;
147
148 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
149
150 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
151 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
152 msgb_free(msg);
153 }
154}
155
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200156static void nat_send_rlc(struct sccp_source_reference *src,
157 struct sccp_source_reference *dst)
158{
159 struct sccp_connection_release_complete *rlc;
160 struct msgb *msg;
161
162 msg = msgb_alloc_headroom(4096, 128, "rlc");
163 if (!msg) {
164 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
165 return;
166 }
167
168 msg->l2h = msgb_put(msg, sizeof(*rlc));
169 rlc = (struct sccp_connection_release_complete *) msg->l2h;
170 rlc->type = SCCP_MSG_TYPE_RLC;
171 rlc->destination_local_reference = *dst;
172 rlc->source_local_reference = *src;
173
174 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
175
176 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
177 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
178 msgb_free(msg);
179 }
180}
181
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200182static void send_mgcp_reset(struct bsc_connection *bsc)
183{
184 static const u_int8_t mgcp_reset[] = {
185 "RSIP 1 13@mgw MGCP 1.0\r\n"
186 };
187
188 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
189}
190
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100191/*
192 * Below is the handling of messages coming
193 * from the MSC and need to be forwarded to
194 * a real BSC.
195 */
196static void initialize_msc_if_needed()
197{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200198 if (nat->first_contact)
199 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100200
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200201 nat->first_contact = 1;
202 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100203}
204
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100205/*
206 * Currently we are lacking refcounting so we need to copy each message.
207 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200208static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int proto)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100209{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100210 struct msgb *msg;
211
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200212 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100213 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
214 return;
215 }
216
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200217 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100218 if (!msg) {
219 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
220 return;
221 }
222
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200223 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100224 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200225
226 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100227}
228
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100229static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100230{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800231 struct sccp_connections *con;
232 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800233 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200234 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100235
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100236 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800237 parsed = bsc_nat_parse(msg);
238 if (!parsed) {
239 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100240 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800241 }
242
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100243 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800244 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800245
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200246 proto = parsed->ipa_proto;
247
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100248 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200249 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100250 switch (parsed->sccp_type) {
251 case SCCP_MSG_TYPE_UDT:
252 /* forward UDT messages to every BSC */
253 goto send_to_all;
254 break;
255 case SCCP_MSG_TYPE_RLSD:
256 case SCCP_MSG_TYPE_CREF:
257 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200258 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800259 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800260 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
261 if (con) {
262 if (bsc_mgcp_assign(con, msg) != 0)
263 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
264 } else
265 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
266 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200267 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100268 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800269 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
270 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200271 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800272 break;
273 case SCCP_MSG_TYPE_RLC:
274 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
275 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100276 break;
277 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100278 /* MSC never opens a SCCP connection, fall through */
279 default:
280 goto exit;
281 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200282
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200283 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
284 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
285 /* Exchange src/dest for the reply */
286 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
287 } else if (!con)
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200288 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100289 }
290
291 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800292 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100293 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800294 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800295 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100296 return -1;
297 }
298
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200299 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100300 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100301
302send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800303 /*
304 * Filter Paging from the network. We do not want to send a PAGING
305 * Command to every BSC in our network. We will analys the PAGING
306 * message and then send it to the authenticated messages...
307 */
308 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200309 bsc = bsc_nat_find_bsc(nat, msg);
310 if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200311 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200312 else
313 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n");
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800314
315 goto exit;
316 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100317 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800318 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100319 if (!bsc->authenticated)
320 continue;
321
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200322 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100323 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800324
325exit:
326 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100327 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100328}
329
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800330static void msc_connection_was_lost(struct bsc_msc_connection *con)
331{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200332 struct bsc_connection *bsc, *tmp;
333
334 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
335 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
336 remove_bsc_connection(bsc);
337
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200338 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200339 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200340 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800341}
342
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200343static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200344{
345 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200346 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200347 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
348 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
349 0x01, 0x20
350 };
351
352 struct msgb *msg;
353
354 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
355 if (!msg) {
356 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
357 return;
358 }
359
360 msg->l2h = msgb_put(msg, sizeof(reset));
361 memcpy(msg->l2h, reset, msgb_l2len(msg));
362
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200363 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200364 LOGP(DMSC, LOGL_ERROR, "Failed to enqueue reset msg.\n");
365 msgb_free(msg);
366 }
367
368 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
369}
370
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800371static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100372{
373 int error;
374 struct msgb *msg = ipaccess_read_msg(bfd, &error);
375 struct ipaccess_head *hh;
376
377 if (!msg) {
378 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100379 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800380 bsc_msc_lost(msc_con);
381 return -1;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100382 }
383
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100384 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100385 return -1;
386 }
387
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100388 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 +0100389
390 /* handle base message handling */
391 hh = (struct ipaccess_head *) msg->data;
392 ipaccess_rcvmsg_base(msg, bfd);
393
394 /* initialize the networking. This includes sending a GSM08.08 message */
395 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
396 initialize_msc_if_needed();
397 else if (hh->proto == IPAC_PROTO_SCCP)
398 forward_sccp_to_bts(msg);
399
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800400 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100401 return 0;
402}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800403
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800404static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
405{
406 int rc;
407 rc = write(bfd->fd, msg->data, msg->len);
408
409 if (rc != msg->len) {
410 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
411 return -1;
412 }
413
414 return rc;
415}
416
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100417/*
418 * Below is the handling of messages coming
419 * from the BSC and need to be forwarded to
420 * a real BSC.
421 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100422
423/*
424 * Remove the connection from the connections list,
425 * remove it from the patching of SCCP header lists
426 * as well. Maybe in the future even close connection..
427 */
428static void remove_bsc_connection(struct bsc_connection *connection)
429{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100430 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800431 bsc_unregister_fd(&connection->write_queue.bfd);
432 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherf38e8792010-03-26 09:27:08 +0100433 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100434 llist_del(&connection->list_entry);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100435
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800436 /* stop the timeout timer */
437 bsc_del_timer(&connection->id_timeout);
438
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100439 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800440 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100441 if (sccp_patch->bsc != connection)
442 continue;
443
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200444 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200445 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100446 }
447
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200448 /* close endpoints allocated by this BSC */
449 bsc_mgcp_clear_endpoints_for(connection);
450
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100451 talloc_free(connection);
452}
453
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800454static void ipaccess_close_bsc(void *data)
455{
456 struct bsc_connection *conn = data;
457
458 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
459 remove_bsc_connection(conn);
460}
461
462static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
463{
464 struct bsc_config *conf;
465 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
466
467 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
468 if (strcmp(conf->token, token) == 0) {
469 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200470 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800471 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200472 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d\n", conf->nr, conf->lac);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800473 break;
474 }
475 }
476}
477
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100478static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100479{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800480 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800481 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100482
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800483 /* Parse and filter messages */
484 parsed = bsc_nat_parse(msg);
485 if (!parsed) {
486 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200487 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800488 return -1;
489 }
490
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100491 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800492 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800493
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200494 /*
495 * check authentication after filtering to not reject auth
496 * responses coming from the BSC. We have to make sure that
497 * nothing from the exit path will forward things to the MSC
498 */
499 if (!bsc->authenticated) {
500 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
501 msgb_free(msg);
502 return -1;
503 }
504
505
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100506 /* modify the SCCP entries */
507 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
508 switch (parsed->sccp_type) {
509 case SCCP_MSG_TYPE_CR:
510 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
511 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800512 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100513 break;
514 case SCCP_MSG_TYPE_RLSD:
515 case SCCP_MSG_TYPE_CREF:
516 case SCCP_MSG_TYPE_DT1:
517 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200518 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800519 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100520 break;
521 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800522 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200523 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100524 break;
525 case SCCP_MSG_TYPE_UDT:
526 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800527 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100528 break;
529 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800530 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 +0800531 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100532 goto exit2;
533 break;
534 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200535 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
536 bsc_mgcp_forward(bsc, msg);
537 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800538 } else {
539 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
540 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100541 }
542
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800543 if (con && con->bsc != bsc) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100544 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
545 goto exit2;
546 }
547
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100548 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800549 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800550 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
551 msgb_free(msg);
552 }
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100553 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800554 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800555
556exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100557 /* if we filter out the reset send an ack to the BSC */
558 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100559 send_reset_ack(bsc);
560 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800561 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
562 /* do we know who is handling this? */
563 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
564 struct tlv_parsed tvp;
565 ipaccess_idtag_parse(&tvp,
566 (unsigned char *) msg->l2h + 2,
567 msgb_l2len(msg) - 2);
568 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
569 ipaccess_auth_bsc(&tvp, bsc);
570 }
571
572 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100573 }
574
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100575exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800576 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800577 msgb_free(msg);
578 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100579}
580
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800581static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100582{
583 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100584 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100585 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100586
587 if (!msg) {
588 if (error == 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100589 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100590 remove_bsc_connection(bsc);
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100591 } else {
592 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100593 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100594 return -1;
595 }
596
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100597
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100598 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 +0100599
600 /* Handle messages from the BSC */
601 /* FIXME: Currently no PONG is sent to the BSC */
602 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100603 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100604
605 return 0;
606}
607
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100608static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
609{
610 int rc;
611
612 rc = write(bfd->fd, msg->data, msg->len);
613 if (rc != msg->len)
614 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
615
616 return rc;
617}
618
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100619static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
620{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100621 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100622 int ret;
623 struct sockaddr_in sa;
624 socklen_t sa_len = sizeof(sa);
625
626 if (!(what & BSC_FD_READ))
627 return 0;
628
629 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
630 if (ret < 0) {
631 perror("accept");
632 return ret;
633 }
634
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200635 /*
636 * if we are not connected to a msc... just close the socket
637 */
638 if (!msc_con->is_connected) {
639 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freythera7c377d2010-04-06 12:42:35 +0200640 close(ret);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200641 return 0;
642 }
643
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100644 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100645 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100646
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100647 /*
648 *
649 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800650 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100651 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100652 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100653 close(ret);
654 return -1;
655 }
656
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800657 write_queue_init(&bsc->write_queue, 100);
658 bsc->write_queue.bfd.data = bsc;
659 bsc->write_queue.bfd.fd = ret;
660 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100661 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800662 bsc->write_queue.bfd.when = BSC_FD_READ;
663 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100664 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100665 close(ret);
666 talloc_free(bsc);
667 return -2;
668 }
669
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200670 LOGP(DNAT, LOGL_NOTICE, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800671 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100672 send_id_ack(bsc);
673 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200674 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800675
676 /*
677 * start the hangup timer
678 */
679 bsc->id_timeout.data = bsc;
680 bsc->id_timeout.cb = ipaccess_close_bsc;
681 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100682 return 0;
683}
684
685static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
686{
687 struct sockaddr_in addr;
688 int ret, on = 1;
689
690 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
691 bfd->cb = ipaccess_listen_bsc_cb;
692 bfd->when = BSC_FD_READ;
693
694 memset(&addr, 0, sizeof(addr));
695 addr.sin_family = AF_INET;
696 addr.sin_port = htons(port);
697 addr.sin_addr.s_addr = in_addr->s_addr;
698
699 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
700
701 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
702 if (ret < 0) {
703 fprintf(stderr, "Could not bind the BSC socket %s\n",
704 strerror(errno));
705 return -EIO;
706 }
707
708 ret = listen(bfd->fd, 1);
709 if (ret < 0) {
710 perror("listen");
711 return ret;
712 }
713
714 ret = bsc_register_fd(bfd);
715 if (ret < 0) {
716 perror("register_listen_fd");
717 return ret;
718 }
719 return 0;
720}
721
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800722static void print_usage()
723{
724 printf("Usage: bsc_nat\n");
725}
726
727static void print_help()
728{
729 printf(" Some useful help...\n");
730 printf(" -h --help this text\n");
731 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
732 printf(" -s --disable-color\n");
733 printf(" -c --config-file filename The config file to use.\n");
734 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100735 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800736}
737
738static void handle_options(int argc, char** argv)
739{
740 while (1) {
741 int option_index = 0, c;
742 static struct option long_options[] = {
743 {"help", 0, 0, 'h'},
744 {"debug", 1, 0, 'd'},
745 {"config-file", 1, 0, 'c'},
746 {"disable-color", 0, 0, 's'},
747 {"timestamp", 0, 0, 'T'},
748 {"msc", 1, 0, 'm'},
749 {"local", 1, 0, 'l'},
750 {0, 0, 0, 0}
751 };
752
753 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
754 long_options, &option_index);
755 if (c == -1)
756 break;
757
758 switch (c) {
759 case 'h':
760 print_usage();
761 print_help();
762 exit(0);
763 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800764 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800765 break;
766 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800767 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800768 break;
769 case 'c':
770 config_file = strdup(optarg);
771 break;
772 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800773 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800774 break;
775 case 'm':
776 msc_address = strdup(optarg);
777 break;
778 case 'l':
779 inet_aton(optarg, &local_addr);
780 break;
781 default:
782 /* ignore */
783 break;
784 }
785 }
786}
787
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100788static void signal_handler(int signal)
789{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100790 switch (signal) {
791 case SIGABRT:
792 /* in case of abort, we want to obtain a talloc report
793 * and then return to the caller, who will abort the process */
794 case SIGUSR1:
795 talloc_report_full(tall_bsc_ctx, stderr);
796 break;
797 default:
798 break;
799 }
800}
801
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800802int main(int argc, char** argv)
803{
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800804 log_init(&log_info);
805 stderr_target = log_target_create_stderr();
806 log_add_target(stderr_target);
807 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800808
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800809 /* parse options */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100810 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800811 handle_options(argc, argv);
812
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800813 nat = bsc_nat_alloc();
814 if (!nat) {
815 fprintf(stderr, "Failed to allocate the BSC nat.\n");
816 return -4;
817 }
818
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800819 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
820
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800821 /* init vty and parse */
822 bsc_nat_vty_init(nat);
823 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800824 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800825 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
826 return -3;
827 }
828
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800829 /* seed the PRNG */
830 srand(time(NULL));
831
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200832 /*
833 * Setup the MGCP code..
834 */
835 if (bsc_mgcp_init(nat) != 0)
836 return -4;
837
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100838 /* connect to the MSC */
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800839 msc_con = bsc_msc_create(msc_address, 5000);
840 if (!msc_con) {
841 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100842 exit(1);
843 }
844
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800845 msc_con->connection_loss = msc_connection_was_lost;
846 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
847 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
848 bsc_msc_connect(msc_con);
849
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100850 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100851 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100852 fprintf(stderr, "Failed to listen for BSC.\n");
853 exit(1);
854 }
855
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100856 signal(SIGABRT, &signal_handler);
857 signal(SIGUSR1, &signal_handler);
858 signal(SIGPIPE, SIG_IGN);
859
860 while (1) {
861 bsc_select_main(0);
862 }
863
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800864 return 0;
865}