blob: 4a8fca7d481fe1872c01317b2a1279eb3a48bdf7 [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 struct in_addr local_addr;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080055static struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010056static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080057static const char *msc_ip = NULL;
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 Freytherb7527612010-04-07 11:20:36 +020062static void msc_send_reset(struct bsc_msc_connection *con);
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 Freyther9d56d0c2010-04-22 19:11:37 +080092static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
93{
94 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
95 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
96 msgb_free(msg);
97 }
98}
99
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100100static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100101{
102 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100103 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
104 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
105 0x00, 0x01, 0x31,
106 };
107
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200108 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100109}
110
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800111static void send_ping(struct bsc_connection *bsc)
112{
113 static const u_int8_t id_ping[] = {
114 IPAC_MSGT_PING,
115 };
116
117 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
118}
119
120static void bsc_pong_timeout(void *_bsc)
121{
122 struct bsc_connection *bsc = _bsc;
123
124 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
125 bsc_close_connection(bsc);
126}
127
128static void bsc_ping_timeout(void *_bsc)
129{
130 struct bsc_connection *bsc = _bsc;
131
132 send_ping(bsc);
133
134 /* send another ping in 20 seconds */
135 bsc_schedule_timer(&bsc->ping_timeout, 20, 0);
136
137 /* also start a pong timer */
138 bsc_schedule_timer(&bsc->pong_timeout, 5, 0);
139}
140
141static void start_ping_pong(struct bsc_connection *bsc)
142{
143 bsc->pong_timeout.data = bsc;
144 bsc->pong_timeout.cb = bsc_pong_timeout;
145 bsc->ping_timeout.data = bsc;
146 bsc->ping_timeout.cb = bsc_ping_timeout;
147
148 bsc_ping_timeout(bsc);
149}
150
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100151static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100152{
153 static const u_int8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200154 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100155 };
156
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200157 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100158}
159
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100160static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100161{
162 static const u_int8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200163 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100164 0x01, IPAC_IDTAG_UNIT,
165 0x01, IPAC_IDTAG_MACADDR,
166 0x01, IPAC_IDTAG_LOCATION1,
167 0x01, IPAC_IDTAG_LOCATION2,
168 0x01, IPAC_IDTAG_EQUIPVERS,
169 0x01, IPAC_IDTAG_SWVERSION,
170 0x01, IPAC_IDTAG_UNITNAME,
171 0x01, IPAC_IDTAG_SERNR,
172 };
173
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200174 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100175}
176
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200177static void nat_send_rlsd(struct sccp_connections *conn)
178{
179 struct sccp_connection_released *rel;
180 struct msgb *msg;
181
182 msg = msgb_alloc_headroom(4096, 128, "rlsd");
183 if (!msg) {
184 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
185 return;
186 }
187
188 msg->l2h = msgb_put(msg, sizeof(*rel));
189 rel = (struct sccp_connection_released *) msg->l2h;
190 rel->type = SCCP_MSG_TYPE_RLSD;
191 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
192 rel->destination_local_reference = conn->remote_ref;
193 rel->source_local_reference = conn->patched_ref;
194
195 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
196
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800197 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200198}
199
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200200static void nat_send_rlc(struct sccp_source_reference *src,
201 struct sccp_source_reference *dst)
202{
203 struct sccp_connection_release_complete *rlc;
204 struct msgb *msg;
205
206 msg = msgb_alloc_headroom(4096, 128, "rlc");
207 if (!msg) {
208 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
209 return;
210 }
211
212 msg->l2h = msgb_put(msg, sizeof(*rlc));
213 rlc = (struct sccp_connection_release_complete *) msg->l2h;
214 rlc->type = SCCP_MSG_TYPE_RLC;
215 rlc->destination_local_reference = *dst;
216 rlc->source_local_reference = *src;
217
218 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
219
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800220 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200221}
222
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200223static void send_mgcp_reset(struct bsc_connection *bsc)
224{
225 static const u_int8_t mgcp_reset[] = {
226 "RSIP 1 13@mgw MGCP 1.0\r\n"
227 };
228
229 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
230}
231
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100232/*
233 * Below is the handling of messages coming
234 * from the MSC and need to be forwarded to
235 * a real BSC.
236 */
237static void initialize_msc_if_needed()
238{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200239 if (nat->first_contact)
240 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100241
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200242 nat->first_contact = 1;
243 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100244}
245
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100246/*
247 * Currently we are lacking refcounting so we need to copy each message.
248 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200249static 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 +0100250{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100251 struct msgb *msg;
252
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200253 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100254 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
255 return;
256 }
257
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200258 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100259 if (!msg) {
260 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
261 return;
262 }
263
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200264 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100265 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200266
267 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100268}
269
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100270static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100271{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800272 struct sccp_connections *con;
273 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800274 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200275 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100276
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100277 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800278 parsed = bsc_nat_parse(msg);
279 if (!parsed) {
280 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100281 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800282 }
283
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100284 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800285 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800286
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200287 proto = parsed->ipa_proto;
288
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100289 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200290 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100291 switch (parsed->sccp_type) {
292 case SCCP_MSG_TYPE_UDT:
293 /* forward UDT messages to every BSC */
294 goto send_to_all;
295 break;
296 case SCCP_MSG_TYPE_RLSD:
297 case SCCP_MSG_TYPE_CREF:
298 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200299 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800300 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800301 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200302 counter_inc(nat->stats.sccp.calls);
303
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800304 if (con) {
Holger Hans Peter Freyther3d0049f2010-04-18 01:35:41 +0800305 counter_inc(con->bsc->cfg->stats.sccp.calls);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800306 if (bsc_mgcp_assign(con, msg) != 0)
307 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
308 } else
309 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
310 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200311 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100312 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800313 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
314 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200315 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800316 break;
317 case SCCP_MSG_TYPE_RLC:
318 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
319 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100320 break;
321 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100322 /* MSC never opens a SCCP connection, fall through */
323 default:
324 goto exit;
325 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200326
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200327 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
328 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
329 /* Exchange src/dest for the reply */
330 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
331 } else if (!con)
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200332 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100333 }
334
335 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800336 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100337 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800338 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800339 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100340 return -1;
341 }
342
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200343 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100344 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100345
346send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800347 /*
348 * Filter Paging from the network. We do not want to send a PAGING
349 * Command to every BSC in our network. We will analys the PAGING
350 * message and then send it to the authenticated messages...
351 */
352 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200353 int lac;
354 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800355 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800356 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800357 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200358 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200359 else
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200360 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
361 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800362
363 goto exit;
364 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100365 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800366 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100367 if (!bsc->authenticated)
368 continue;
369
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200370 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100371 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800372
373exit:
374 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100375 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100376}
377
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800378static void msc_connection_was_lost(struct bsc_msc_connection *con)
379{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200380 struct bsc_connection *bsc, *tmp;
381
Holger Hans Peter Freythera4ed81c2010-04-12 12:35:02 +0200382 counter_inc(nat->stats.msc.reconn);
383
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200384 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
385 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800386 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200387
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200388 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200389 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200390 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800391}
392
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200393static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200394{
395 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200396 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200397 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
398 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
399 0x01, 0x20
400 };
401
402 struct msgb *msg;
403
404 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
405 if (!msg) {
406 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
407 return;
408 }
409
410 msg->l2h = msgb_put(msg, sizeof(reset));
411 memcpy(msg->l2h, reset, msgb_l2len(msg));
412
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800413 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200414
415 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
416}
417
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800418static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100419{
420 int error;
421 struct msgb *msg = ipaccess_read_msg(bfd, &error);
422 struct ipaccess_head *hh;
423
424 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800425 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100426 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800427 else
428 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100429
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800430 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100431 return -1;
432 }
433
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100434 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 +0100435
436 /* handle base message handling */
437 hh = (struct ipaccess_head *) msg->data;
438 ipaccess_rcvmsg_base(msg, bfd);
439
440 /* initialize the networking. This includes sending a GSM08.08 message */
441 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
442 initialize_msc_if_needed();
443 else if (hh->proto == IPAC_PROTO_SCCP)
444 forward_sccp_to_bts(msg);
445
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800446 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100447 return 0;
448}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800449
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800450static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
451{
452 int rc;
453 rc = write(bfd->fd, msg->data, msg->len);
454
455 if (rc != msg->len) {
456 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
457 return -1;
458 }
459
460 return rc;
461}
462
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100463/*
464 * Below is the handling of messages coming
465 * from the BSC and need to be forwarded to
466 * a real BSC.
467 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100468
469/*
470 * Remove the connection from the connections list,
471 * remove it from the patching of SCCP header lists
472 * as well. Maybe in the future even close connection..
473 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800474void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100475{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100476 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100477
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800478 /* stop the timeout timer */
479 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800480 bsc_del_timer(&connection->ping_timeout);
481 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800482
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100483 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800484 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100485 if (sccp_patch->bsc != connection)
486 continue;
487
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800488 if (sccp_patch->has_remote_ref)
489 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200490 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100491 }
492
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200493 /* close endpoints allocated by this BSC */
494 bsc_mgcp_clear_endpoints_for(connection);
495
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800496 bsc_unregister_fd(&connection->write_queue.bfd);
497 close(connection->write_queue.bfd.fd);
498 write_queue_clear(&connection->write_queue);
499 llist_del(&connection->list_entry);
500
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100501 talloc_free(connection);
502}
503
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800504static void ipaccess_close_bsc(void *data)
505{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200506 struct sockaddr_in sock;
507 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800508 struct bsc_connection *conn = data;
509
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200510
511 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
512 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
513 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800514 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800515}
516
517static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
518{
519 struct bsc_config *conf;
520 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
521
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800522 if (bsc->cfg) {
523 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
524 bsc->write_queue.bfd.fd, bsc->cfg->nr);
525 return;
526 }
527
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800528 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
529 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200530 counter_inc(conf->stats.net.reconn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800531 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200532 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800533 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200534 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d\n", conf->nr, conf->lac);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800535 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200536 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800537 }
538 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200539
540 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s.\n", token);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800541}
542
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100543static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100544{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800545 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800546 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100547
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800548 /* Parse and filter messages */
549 parsed = bsc_nat_parse(msg);
550 if (!parsed) {
551 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200552 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800553 return -1;
554 }
555
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100556 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800557 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800558
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200559 /*
560 * check authentication after filtering to not reject auth
561 * responses coming from the BSC. We have to make sure that
562 * nothing from the exit path will forward things to the MSC
563 */
564 if (!bsc->authenticated) {
565 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
566 msgb_free(msg);
567 return -1;
568 }
569
570
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100571 /* modify the SCCP entries */
572 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
573 switch (parsed->sccp_type) {
574 case SCCP_MSG_TYPE_CR:
575 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
576 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800577 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100578 break;
579 case SCCP_MSG_TYPE_RLSD:
580 case SCCP_MSG_TYPE_CREF:
581 case SCCP_MSG_TYPE_DT1:
582 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200583 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800584 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100585 break;
586 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800587 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200588 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100589 break;
590 case SCCP_MSG_TYPE_UDT:
591 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800592 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100593 break;
594 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800595 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 +0800596 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100597 goto exit2;
598 break;
599 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200600 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
601 bsc_mgcp_forward(bsc, msg);
602 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800603 } else {
604 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
605 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100606 }
607
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800608 if (con && con->bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800609 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
610 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100611 goto exit2;
612 }
613
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100614 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800615 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100616 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800617 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800618
619exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100620 /* if we filter out the reset send an ack to the BSC */
621 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100622 send_reset_ack(bsc);
623 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800624 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
625 /* do we know who is handling this? */
626 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
627 struct tlv_parsed tvp;
628 ipaccess_idtag_parse(&tvp,
629 (unsigned char *) msg->l2h + 2,
630 msgb_l2len(msg) - 2);
631 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
632 ipaccess_auth_bsc(&tvp, bsc);
633 }
634
635 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100636 }
637
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100638exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800639 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800640 msgb_free(msg);
641 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100642}
643
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800644static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100645{
646 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100647 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100648 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100649
650 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800651 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800652 LOGP(DNAT, LOGL_ERROR,
653 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
654 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800655 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800656 LOGP(DNAT, LOGL_ERROR,
657 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
658 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800659
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800660 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100661 return -1;
662 }
663
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100664
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100665 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 +0100666
667 /* Handle messages from the BSC */
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800668 if (bsc->authenticated) {
669 struct ipaccess_head *hh;
670 hh = (struct ipaccess_head *) msg->data;
671
672 /* stop the pong timeout */
673 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_PONG) {
674 bsc_del_timer(&bsc->pong_timeout);
675 msgb_free(msg);
676 return 0;
677 }
678 }
679
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100680 /* FIXME: Currently no PONG is sent to the BSC */
681 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100682 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100683
684 return 0;
685}
686
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100687static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
688{
689 int rc;
690
691 rc = write(bfd->fd, msg->data, msg->len);
692 if (rc != msg->len)
693 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
694
695 return rc;
696}
697
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100698static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
699{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100700 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100701 int ret;
702 struct sockaddr_in sa;
703 socklen_t sa_len = sizeof(sa);
704
705 if (!(what & BSC_FD_READ))
706 return 0;
707
708 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
709 if (ret < 0) {
710 perror("accept");
711 return ret;
712 }
713
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200714 /* count the reconnect */
715 counter_inc(nat->stats.bsc.reconn);
716
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200717 /*
718 * if we are not connected to a msc... just close the socket
719 */
720 if (!msc_con->is_connected) {
721 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freythera7c377d2010-04-06 12:42:35 +0200722 close(ret);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200723 return 0;
724 }
725
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100726 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100727 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100728
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100729 /*
730 *
731 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800732 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100733 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100734 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100735 close(ret);
736 return -1;
737 }
738
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800739 bsc->write_queue.bfd.data = bsc;
740 bsc->write_queue.bfd.fd = ret;
741 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100742 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800743 bsc->write_queue.bfd.when = BSC_FD_READ;
744 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100745 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100746 close(ret);
747 talloc_free(bsc);
748 return -2;
749 }
750
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200751 LOGP(DNAT, LOGL_NOTICE, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800752 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100753 send_id_ack(bsc);
754 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200755 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800756
757 /*
758 * start the hangup timer
759 */
760 bsc->id_timeout.data = bsc;
761 bsc->id_timeout.cb = ipaccess_close_bsc;
762 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100763 return 0;
764}
765
766static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
767{
768 struct sockaddr_in addr;
769 int ret, on = 1;
770
771 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
772 bfd->cb = ipaccess_listen_bsc_cb;
773 bfd->when = BSC_FD_READ;
774
775 memset(&addr, 0, sizeof(addr));
776 addr.sin_family = AF_INET;
777 addr.sin_port = htons(port);
778 addr.sin_addr.s_addr = in_addr->s_addr;
779
780 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
781
782 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
783 if (ret < 0) {
784 fprintf(stderr, "Could not bind the BSC socket %s\n",
785 strerror(errno));
786 return -EIO;
787 }
788
789 ret = listen(bfd->fd, 1);
790 if (ret < 0) {
791 perror("listen");
792 return ret;
793 }
794
795 ret = bsc_register_fd(bfd);
796 if (ret < 0) {
797 perror("register_listen_fd");
798 return ret;
799 }
800 return 0;
801}
802
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800803static void print_usage()
804{
805 printf("Usage: bsc_nat\n");
806}
807
808static void print_help()
809{
810 printf(" Some useful help...\n");
811 printf(" -h --help this text\n");
812 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
813 printf(" -s --disable-color\n");
814 printf(" -c --config-file filename The config file to use.\n");
815 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100816 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800817}
818
819static void handle_options(int argc, char** argv)
820{
821 while (1) {
822 int option_index = 0, c;
823 static struct option long_options[] = {
824 {"help", 0, 0, 'h'},
825 {"debug", 1, 0, 'd'},
826 {"config-file", 1, 0, 'c'},
827 {"disable-color", 0, 0, 's'},
828 {"timestamp", 0, 0, 'T'},
829 {"msc", 1, 0, 'm'},
830 {"local", 1, 0, 'l'},
831 {0, 0, 0, 0}
832 };
833
834 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
835 long_options, &option_index);
836 if (c == -1)
837 break;
838
839 switch (c) {
840 case 'h':
841 print_usage();
842 print_help();
843 exit(0);
844 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800845 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800846 break;
847 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800848 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800849 break;
850 case 'c':
851 config_file = strdup(optarg);
852 break;
853 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800854 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800855 break;
856 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800857 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800858 break;
859 case 'l':
860 inet_aton(optarg, &local_addr);
861 break;
862 default:
863 /* ignore */
864 break;
865 }
866 }
867}
868
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100869static void signal_handler(int signal)
870{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100871 switch (signal) {
872 case SIGABRT:
873 /* in case of abort, we want to obtain a talloc report
874 * and then return to the caller, who will abort the process */
875 case SIGUSR1:
876 talloc_report_full(tall_bsc_ctx, stderr);
877 break;
878 default:
879 break;
880 }
881}
882
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800883extern void *tall_msgb_ctx;
884extern void *tall_ctr_ctx;
885static void talloc_init_ctx()
886{
887 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
888 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
889 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
890}
891
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800892int main(int argc, char** argv)
893{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800894 talloc_init_ctx();
895
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +0800896
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800897 log_init(&log_info);
898 stderr_target = log_target_create_stderr();
899 log_add_target(stderr_target);
900 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800901
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800902 nat = bsc_nat_alloc();
903 if (!nat) {
904 fprintf(stderr, "Failed to allocate the BSC nat.\n");
905 return -4;
906 }
907
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800908 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800909 if (!nat->mgcp_cfg) {
910 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
911 return -5;
912 }
913
914 /* parse options */
915 local_addr.s_addr = INADDR_ANY;
916 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800917
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800918 /* init vty and parse */
919 bsc_nat_vty_init(nat);
920 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800921 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800922 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
923 return -3;
924 }
925
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800926 /* over rule the VTY config */
927 if (msc_ip)
928 bsc_nat_set_msc_ip(nat, msc_ip);
929
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800930 /* seed the PRNG */
931 srand(time(NULL));
932
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200933 /*
934 * Setup the MGCP code..
935 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800936 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200937 return -4;
938
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100939 /* connect to the MSC */
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200940 msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800941 if (!msc_con) {
942 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100943 exit(1);
944 }
945
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800946 msc_con->connection_loss = msc_connection_was_lost;
947 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
948 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
949 bsc_msc_connect(msc_con);
950
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100951 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100952 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100953 fprintf(stderr, "Failed to listen for BSC.\n");
954 exit(1);
955 }
956
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100957 signal(SIGABRT, &signal_handler);
958 signal(SIGUSR1, &signal_handler);
959 signal(SIGPIPE, SIG_IGN);
960
961 while (1) {
962 bsc_select_main(0);
963 }
964
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800965 return 0;
966}