blob: 58ee6af30260e0e77a2c2e0615d10e5fc65e4013 [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
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800120static void send_pong(struct bsc_connection *bsc)
121{
122 static const u_int8_t id_pong[] = {
123 IPAC_MSGT_PONG,
124 };
125
126 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
127}
128
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800129static void bsc_pong_timeout(void *_bsc)
130{
131 struct bsc_connection *bsc = _bsc;
132
133 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
134 bsc_close_connection(bsc);
135}
136
137static void bsc_ping_timeout(void *_bsc)
138{
139 struct bsc_connection *bsc = _bsc;
140
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800141 if (bsc->nat->ping_timeout < 0)
142 return;
143
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800144 send_ping(bsc);
145
146 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800147 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800148
149 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800150 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800151}
152
153static void start_ping_pong(struct bsc_connection *bsc)
154{
155 bsc->pong_timeout.data = bsc;
156 bsc->pong_timeout.cb = bsc_pong_timeout;
157 bsc->ping_timeout.data = bsc;
158 bsc->ping_timeout.cb = bsc_ping_timeout;
159
160 bsc_ping_timeout(bsc);
161}
162
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100163static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100164{
165 static const u_int8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200166 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100167 };
168
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200169 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100170}
171
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100172static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100173{
174 static const u_int8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200175 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100176 0x01, IPAC_IDTAG_UNIT,
177 0x01, IPAC_IDTAG_MACADDR,
178 0x01, IPAC_IDTAG_LOCATION1,
179 0x01, IPAC_IDTAG_LOCATION2,
180 0x01, IPAC_IDTAG_EQUIPVERS,
181 0x01, IPAC_IDTAG_SWVERSION,
182 0x01, IPAC_IDTAG_UNITNAME,
183 0x01, IPAC_IDTAG_SERNR,
184 };
185
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200186 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100187}
188
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200189static void nat_send_rlsd(struct sccp_connections *conn)
190{
191 struct sccp_connection_released *rel;
192 struct msgb *msg;
193
194 msg = msgb_alloc_headroom(4096, 128, "rlsd");
195 if (!msg) {
196 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
197 return;
198 }
199
200 msg->l2h = msgb_put(msg, sizeof(*rel));
201 rel = (struct sccp_connection_released *) msg->l2h;
202 rel->type = SCCP_MSG_TYPE_RLSD;
203 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
204 rel->destination_local_reference = conn->remote_ref;
205 rel->source_local_reference = conn->patched_ref;
206
207 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
208
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800209 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200210}
211
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200212static void nat_send_rlc(struct sccp_source_reference *src,
213 struct sccp_source_reference *dst)
214{
215 struct sccp_connection_release_complete *rlc;
216 struct msgb *msg;
217
218 msg = msgb_alloc_headroom(4096, 128, "rlc");
219 if (!msg) {
220 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
221 return;
222 }
223
224 msg->l2h = msgb_put(msg, sizeof(*rlc));
225 rlc = (struct sccp_connection_release_complete *) msg->l2h;
226 rlc->type = SCCP_MSG_TYPE_RLC;
227 rlc->destination_local_reference = *dst;
228 rlc->source_local_reference = *src;
229
230 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
231
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800232 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200233}
234
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200235static void send_mgcp_reset(struct bsc_connection *bsc)
236{
237 static const u_int8_t mgcp_reset[] = {
238 "RSIP 1 13@mgw MGCP 1.0\r\n"
239 };
240
241 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
242}
243
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100244/*
245 * Below is the handling of messages coming
246 * from the MSC and need to be forwarded to
247 * a real BSC.
248 */
249static void initialize_msc_if_needed()
250{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200251 if (nat->first_contact)
252 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100253
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200254 nat->first_contact = 1;
255 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100256}
257
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100258/*
259 * Currently we are lacking refcounting so we need to copy each message.
260 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200261static 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 +0100262{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100263 struct msgb *msg;
264
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200265 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100266 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
267 return;
268 }
269
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200270 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100271 if (!msg) {
272 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
273 return;
274 }
275
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200276 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100277 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200278
279 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100280}
281
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100282static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100283{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800284 struct sccp_connections *con;
285 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800286 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200287 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100288
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100289 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800290 parsed = bsc_nat_parse(msg);
291 if (!parsed) {
292 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100293 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800294 }
295
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100296 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800297 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800298
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200299 proto = parsed->ipa_proto;
300
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100301 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200302 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100303 switch (parsed->sccp_type) {
304 case SCCP_MSG_TYPE_UDT:
305 /* forward UDT messages to every BSC */
306 goto send_to_all;
307 break;
308 case SCCP_MSG_TYPE_RLSD:
309 case SCCP_MSG_TYPE_CREF:
310 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200311 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800312 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800313 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200314 counter_inc(nat->stats.sccp.calls);
315
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800316 if (con) {
Holger Hans Peter Freyther3d0049f2010-04-18 01:35:41 +0800317 counter_inc(con->bsc->cfg->stats.sccp.calls);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800318 if (bsc_mgcp_assign(con, msg) != 0)
319 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
320 } else
321 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
322 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200323 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100324 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800325 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
326 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200327 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800328 break;
329 case SCCP_MSG_TYPE_RLC:
330 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
331 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100332 break;
333 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100334 /* MSC never opens a SCCP connection, fall through */
335 default:
336 goto exit;
337 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200338
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200339 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
340 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
341 /* Exchange src/dest for the reply */
342 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
343 } else if (!con)
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200344 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100345 }
346
347 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800348 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100349 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800350 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800351 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100352 return -1;
353 }
354
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200355 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100356 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100357
358send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800359 /*
360 * Filter Paging from the network. We do not want to send a PAGING
361 * Command to every BSC in our network. We will analys the PAGING
362 * message and then send it to the authenticated messages...
363 */
364 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200365 int lac;
366 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800367 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800368 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800369 else if (bsc)
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 Freytherbae9da42010-03-30 05:57:42 +0200371 else
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200372 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
373 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800374
375 goto exit;
376 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100377 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800378 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100379 if (!bsc->authenticated)
380 continue;
381
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200382 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100383 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800384
385exit:
386 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100387 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100388}
389
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800390static void msc_connection_was_lost(struct bsc_msc_connection *con)
391{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200392 struct bsc_connection *bsc, *tmp;
393
Holger Hans Peter Freythera4ed81c2010-04-12 12:35:02 +0200394 counter_inc(nat->stats.msc.reconn);
395
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200396 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
397 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800398 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200399
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200400 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200401 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200402 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800403}
404
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200405static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200406{
407 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200408 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200409 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
410 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
411 0x01, 0x20
412 };
413
414 struct msgb *msg;
415
416 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
417 if (!msg) {
418 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
419 return;
420 }
421
422 msg->l2h = msgb_put(msg, sizeof(reset));
423 memcpy(msg->l2h, reset, msgb_l2len(msg));
424
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800425 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200426
427 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
428}
429
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800430static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100431{
432 int error;
433 struct msgb *msg = ipaccess_read_msg(bfd, &error);
434 struct ipaccess_head *hh;
435
436 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800437 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100438 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800439 else
440 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100441
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800442 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100443 return -1;
444 }
445
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100446 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 +0100447
448 /* handle base message handling */
449 hh = (struct ipaccess_head *) msg->data;
450 ipaccess_rcvmsg_base(msg, bfd);
451
452 /* initialize the networking. This includes sending a GSM08.08 message */
453 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
454 initialize_msc_if_needed();
455 else if (hh->proto == IPAC_PROTO_SCCP)
456 forward_sccp_to_bts(msg);
457
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800458 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100459 return 0;
460}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800461
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800462static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
463{
464 int rc;
465 rc = write(bfd->fd, msg->data, msg->len);
466
467 if (rc != msg->len) {
468 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
469 return -1;
470 }
471
472 return rc;
473}
474
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100475/*
476 * Below is the handling of messages coming
477 * from the BSC and need to be forwarded to
478 * a real BSC.
479 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100480
481/*
482 * Remove the connection from the connections list,
483 * remove it from the patching of SCCP header lists
484 * as well. Maybe in the future even close connection..
485 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800486void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100487{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100488 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100489
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800490 /* stop the timeout timer */
491 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800492 bsc_del_timer(&connection->ping_timeout);
493 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800494
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100495 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800496 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100497 if (sccp_patch->bsc != connection)
498 continue;
499
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800500 if (sccp_patch->has_remote_ref)
501 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200502 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100503 }
504
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200505 /* close endpoints allocated by this BSC */
506 bsc_mgcp_clear_endpoints_for(connection);
507
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800508 bsc_unregister_fd(&connection->write_queue.bfd);
509 close(connection->write_queue.bfd.fd);
510 write_queue_clear(&connection->write_queue);
511 llist_del(&connection->list_entry);
512
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100513 talloc_free(connection);
514}
515
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800516static void ipaccess_close_bsc(void *data)
517{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200518 struct sockaddr_in sock;
519 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800520 struct bsc_connection *conn = data;
521
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200522
523 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
524 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
525 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800526 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800527}
528
529static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
530{
531 struct bsc_config *conf;
532 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
533
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800534 if (bsc->cfg) {
535 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
536 bsc->write_queue.bfd.fd, bsc->cfg->nr);
537 return;
538 }
539
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800540 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
541 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200542 counter_inc(conf->stats.net.reconn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800543 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200544 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800545 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200546 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d\n", conf->nr, conf->lac);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800547 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200548 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800549 }
550 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200551
552 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s.\n", token);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800553}
554
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100555static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100556{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800557 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800558 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100559
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800560 /* Parse and filter messages */
561 parsed = bsc_nat_parse(msg);
562 if (!parsed) {
563 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200564 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800565 return -1;
566 }
567
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100568 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800569 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800570
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200571 /*
572 * check authentication after filtering to not reject auth
573 * responses coming from the BSC. We have to make sure that
574 * nothing from the exit path will forward things to the MSC
575 */
576 if (!bsc->authenticated) {
577 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
578 msgb_free(msg);
579 return -1;
580 }
581
582
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100583 /* modify the SCCP entries */
584 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
585 switch (parsed->sccp_type) {
586 case SCCP_MSG_TYPE_CR:
587 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
588 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800589 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100590 break;
591 case SCCP_MSG_TYPE_RLSD:
592 case SCCP_MSG_TYPE_CREF:
593 case SCCP_MSG_TYPE_DT1:
594 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200595 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800596 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100597 break;
598 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800599 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200600 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100601 break;
602 case SCCP_MSG_TYPE_UDT:
603 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800604 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100605 break;
606 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800607 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 +0800608 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100609 goto exit2;
610 break;
611 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200612 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
613 bsc_mgcp_forward(bsc, msg);
614 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800615 } else {
616 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
617 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100618 }
619
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800620 if (con && con->bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800621 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
622 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100623 goto exit2;
624 }
625
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100626 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800627 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100628 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800629 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800630
631exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100632 /* if we filter out the reset send an ack to the BSC */
633 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100634 send_reset_ack(bsc);
635 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800636 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
637 /* do we know who is handling this? */
638 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
639 struct tlv_parsed tvp;
640 ipaccess_idtag_parse(&tvp,
641 (unsigned char *) msg->l2h + 2,
642 msgb_l2len(msg) - 2);
643 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
644 ipaccess_auth_bsc(&tvp, bsc);
645 }
646
647 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100648 }
649
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100650exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800651 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800652 msgb_free(msg);
653 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100654}
655
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800656static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100657{
658 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100659 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100660 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800661 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100662
663 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800664 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800665 LOGP(DNAT, LOGL_ERROR,
666 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
667 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800668 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800669 LOGP(DNAT, LOGL_ERROR,
670 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
671 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800672
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800673 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100674 return -1;
675 }
676
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100677
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100678 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 +0100679
680 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800681 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800682
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800683 /* stop the pong timeout */
684 if (hh->proto == IPAC_PROTO_IPACCESS) {
685 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800686 bsc_del_timer(&bsc->pong_timeout);
687 msgb_free(msg);
688 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800689 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
690 send_pong(bsc);
691 msgb_free(msg);
692 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800693 }
694 }
695
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100696 /* FIXME: Currently no PONG is sent to the BSC */
697 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100698 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100699
700 return 0;
701}
702
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100703static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
704{
705 int rc;
706
707 rc = write(bfd->fd, msg->data, msg->len);
708 if (rc != msg->len)
709 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
710
711 return rc;
712}
713
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100714static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
715{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100716 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100717 int ret;
718 struct sockaddr_in sa;
719 socklen_t sa_len = sizeof(sa);
720
721 if (!(what & BSC_FD_READ))
722 return 0;
723
724 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
725 if (ret < 0) {
726 perror("accept");
727 return ret;
728 }
729
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200730 /* count the reconnect */
731 counter_inc(nat->stats.bsc.reconn);
732
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200733 /*
734 * if we are not connected to a msc... just close the socket
735 */
736 if (!msc_con->is_connected) {
737 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freythera7c377d2010-04-06 12:42:35 +0200738 close(ret);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200739 return 0;
740 }
741
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100742 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100743 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100744
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100745 /*
746 *
747 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800748 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100749 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100750 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100751 close(ret);
752 return -1;
753 }
754
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800755 bsc->write_queue.bfd.data = bsc;
756 bsc->write_queue.bfd.fd = ret;
757 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100758 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800759 bsc->write_queue.bfd.when = BSC_FD_READ;
760 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100761 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100762 close(ret);
763 talloc_free(bsc);
764 return -2;
765 }
766
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200767 LOGP(DNAT, LOGL_NOTICE, "Registered new BSC\n");
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800768 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100769 send_id_ack(bsc);
770 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200771 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800772
773 /*
774 * start the hangup timer
775 */
776 bsc->id_timeout.data = bsc;
777 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800778 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100779 return 0;
780}
781
782static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
783{
784 struct sockaddr_in addr;
785 int ret, on = 1;
786
787 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
788 bfd->cb = ipaccess_listen_bsc_cb;
789 bfd->when = BSC_FD_READ;
790
791 memset(&addr, 0, sizeof(addr));
792 addr.sin_family = AF_INET;
793 addr.sin_port = htons(port);
794 addr.sin_addr.s_addr = in_addr->s_addr;
795
796 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
797
798 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
799 if (ret < 0) {
800 fprintf(stderr, "Could not bind the BSC socket %s\n",
801 strerror(errno));
802 return -EIO;
803 }
804
805 ret = listen(bfd->fd, 1);
806 if (ret < 0) {
807 perror("listen");
808 return ret;
809 }
810
811 ret = bsc_register_fd(bfd);
812 if (ret < 0) {
813 perror("register_listen_fd");
814 return ret;
815 }
816 return 0;
817}
818
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800819static void print_usage()
820{
821 printf("Usage: bsc_nat\n");
822}
823
824static void print_help()
825{
826 printf(" Some useful help...\n");
827 printf(" -h --help this text\n");
828 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
829 printf(" -s --disable-color\n");
830 printf(" -c --config-file filename The config file to use.\n");
831 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100832 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800833}
834
835static void handle_options(int argc, char** argv)
836{
837 while (1) {
838 int option_index = 0, c;
839 static struct option long_options[] = {
840 {"help", 0, 0, 'h'},
841 {"debug", 1, 0, 'd'},
842 {"config-file", 1, 0, 'c'},
843 {"disable-color", 0, 0, 's'},
844 {"timestamp", 0, 0, 'T'},
845 {"msc", 1, 0, 'm'},
846 {"local", 1, 0, 'l'},
847 {0, 0, 0, 0}
848 };
849
850 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
851 long_options, &option_index);
852 if (c == -1)
853 break;
854
855 switch (c) {
856 case 'h':
857 print_usage();
858 print_help();
859 exit(0);
860 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800861 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800862 break;
863 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800864 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800865 break;
866 case 'c':
867 config_file = strdup(optarg);
868 break;
869 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800870 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800871 break;
872 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800873 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800874 break;
875 case 'l':
876 inet_aton(optarg, &local_addr);
877 break;
878 default:
879 /* ignore */
880 break;
881 }
882 }
883}
884
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100885static void signal_handler(int signal)
886{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100887 switch (signal) {
888 case SIGABRT:
889 /* in case of abort, we want to obtain a talloc report
890 * and then return to the caller, who will abort the process */
891 case SIGUSR1:
892 talloc_report_full(tall_bsc_ctx, stderr);
893 break;
894 default:
895 break;
896 }
897}
898
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800899extern void *tall_msgb_ctx;
900extern void *tall_ctr_ctx;
901static void talloc_init_ctx()
902{
903 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
904 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
905 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
906}
907
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800908int main(int argc, char** argv)
909{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800910 talloc_init_ctx();
911
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +0800912
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800913 log_init(&log_info);
914 stderr_target = log_target_create_stderr();
915 log_add_target(stderr_target);
916 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800917
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800918 nat = bsc_nat_alloc();
919 if (!nat) {
920 fprintf(stderr, "Failed to allocate the BSC nat.\n");
921 return -4;
922 }
923
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800924 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800925 if (!nat->mgcp_cfg) {
926 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
927 return -5;
928 }
929
930 /* parse options */
931 local_addr.s_addr = INADDR_ANY;
932 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800933
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800934 /* init vty and parse */
935 bsc_nat_vty_init(nat);
936 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800937 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800938 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
939 return -3;
940 }
941
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800942 /* over rule the VTY config */
943 if (msc_ip)
944 bsc_nat_set_msc_ip(nat, msc_ip);
945
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800946 /* seed the PRNG */
947 srand(time(NULL));
948
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200949 /*
950 * Setup the MGCP code..
951 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800952 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200953 return -4;
954
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100955 /* connect to the MSC */
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200956 msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800957 if (!msc_con) {
958 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100959 exit(1);
960 }
961
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800962 msc_con->connection_loss = msc_connection_was_lost;
963 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
964 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
965 bsc_msc_connect(msc_con);
966
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100967 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100968 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100969 fprintf(stderr, "Failed to listen for BSC.\n");
970 exit(1);
971 }
972
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100973 signal(SIGABRT, &signal_handler);
974 signal(SIGUSR1, &signal_handler);
975 signal(SIGPIPE, SIG_IGN);
976
977 while (1) {
978 bsc_select_main(0);
979 }
980
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800981 return 0;
982}