blob: f69d383102a02de0f79e6c461e930a88659abca4 [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 Freyther74cfab72010-05-05 17:03:44 +0800546 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
547 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800548 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200549 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800550 }
551 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200552
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800553 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
554 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800555}
556
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100557static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100558{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800559 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800560 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100561
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800562 /* Parse and filter messages */
563 parsed = bsc_nat_parse(msg);
564 if (!parsed) {
565 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200566 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800567 return -1;
568 }
569
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100570 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800571 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800572
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200573 /*
574 * check authentication after filtering to not reject auth
575 * responses coming from the BSC. We have to make sure that
576 * nothing from the exit path will forward things to the MSC
577 */
578 if (!bsc->authenticated) {
579 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
580 msgb_free(msg);
581 return -1;
582 }
583
584
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100585 /* modify the SCCP entries */
586 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
587 switch (parsed->sccp_type) {
588 case SCCP_MSG_TYPE_CR:
589 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
590 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800591 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100592 break;
593 case SCCP_MSG_TYPE_RLSD:
594 case SCCP_MSG_TYPE_CREF:
595 case SCCP_MSG_TYPE_DT1:
596 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200597 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800598 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100599 break;
600 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800601 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200602 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100603 break;
604 case SCCP_MSG_TYPE_UDT:
605 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800606 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100607 break;
608 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800609 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 +0800610 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100611 goto exit2;
612 break;
613 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200614 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
615 bsc_mgcp_forward(bsc, msg);
616 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800617 } else {
618 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
619 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100620 }
621
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800622 if (con && con->bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800623 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
624 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100625 goto exit2;
626 }
627
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100628 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800629 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100630 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800631 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800632
633exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100634 /* if we filter out the reset send an ack to the BSC */
635 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100636 send_reset_ack(bsc);
637 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800638 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
639 /* do we know who is handling this? */
640 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
641 struct tlv_parsed tvp;
642 ipaccess_idtag_parse(&tvp,
643 (unsigned char *) msg->l2h + 2,
644 msgb_l2len(msg) - 2);
645 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
646 ipaccess_auth_bsc(&tvp, bsc);
647 }
648
649 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100650 }
651
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100652exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800653 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800654 msgb_free(msg);
655 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100656}
657
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800658static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100659{
660 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100661 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100662 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800663 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100664
665 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800666 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800667 LOGP(DNAT, LOGL_ERROR,
668 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
669 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800670 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800671 LOGP(DNAT, LOGL_ERROR,
672 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
673 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800674
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800675 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100676 return -1;
677 }
678
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100679
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100680 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 +0100681
682 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800683 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800684
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800685 /* stop the pong timeout */
686 if (hh->proto == IPAC_PROTO_IPACCESS) {
687 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800688 bsc_del_timer(&bsc->pong_timeout);
689 msgb_free(msg);
690 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800691 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
692 send_pong(bsc);
693 msgb_free(msg);
694 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800695 }
696 }
697
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100698 /* FIXME: Currently no PONG is sent to the BSC */
699 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100700 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100701
702 return 0;
703}
704
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100705static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
706{
707 int rc;
708
709 rc = write(bfd->fd, msg->data, msg->len);
710 if (rc != msg->len)
711 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
712
713 return rc;
714}
715
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100716static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
717{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100718 struct bsc_connection *bsc;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100719 int ret;
720 struct sockaddr_in sa;
721 socklen_t sa_len = sizeof(sa);
722
723 if (!(what & BSC_FD_READ))
724 return 0;
725
726 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
727 if (ret < 0) {
728 perror("accept");
729 return ret;
730 }
731
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200732 /* count the reconnect */
733 counter_inc(nat->stats.bsc.reconn);
734
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200735 /*
736 * if we are not connected to a msc... just close the socket
737 */
738 if (!msc_con->is_connected) {
739 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freythera7c377d2010-04-06 12:42:35 +0200740 close(ret);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200741 return 0;
742 }
743
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100744 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100745 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100746
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100747 /*
748 *
749 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800750 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100751 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100752 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100753 close(ret);
754 return -1;
755 }
756
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800757 bsc->write_queue.bfd.data = bsc;
758 bsc->write_queue.bfd.fd = ret;
759 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100760 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800761 bsc->write_queue.bfd.when = BSC_FD_READ;
762 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100763 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100764 close(ret);
765 talloc_free(bsc);
766 return -2;
767 }
768
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800769 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
770 ret, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800771 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100772 send_id_ack(bsc);
773 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200774 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800775
776 /*
777 * start the hangup timer
778 */
779 bsc->id_timeout.data = bsc;
780 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800781 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100782 return 0;
783}
784
785static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
786{
787 struct sockaddr_in addr;
788 int ret, on = 1;
789
790 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
791 bfd->cb = ipaccess_listen_bsc_cb;
792 bfd->when = BSC_FD_READ;
793
794 memset(&addr, 0, sizeof(addr));
795 addr.sin_family = AF_INET;
796 addr.sin_port = htons(port);
797 addr.sin_addr.s_addr = in_addr->s_addr;
798
799 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
800
801 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
802 if (ret < 0) {
803 fprintf(stderr, "Could not bind the BSC socket %s\n",
804 strerror(errno));
805 return -EIO;
806 }
807
808 ret = listen(bfd->fd, 1);
809 if (ret < 0) {
810 perror("listen");
811 return ret;
812 }
813
814 ret = bsc_register_fd(bfd);
815 if (ret < 0) {
816 perror("register_listen_fd");
817 return ret;
818 }
819 return 0;
820}
821
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800822static void print_usage()
823{
824 printf("Usage: bsc_nat\n");
825}
826
827static void print_help()
828{
829 printf(" Some useful help...\n");
830 printf(" -h --help this text\n");
831 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
832 printf(" -s --disable-color\n");
833 printf(" -c --config-file filename The config file to use.\n");
834 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100835 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800836}
837
838static void handle_options(int argc, char** argv)
839{
840 while (1) {
841 int option_index = 0, c;
842 static struct option long_options[] = {
843 {"help", 0, 0, 'h'},
844 {"debug", 1, 0, 'd'},
845 {"config-file", 1, 0, 'c'},
846 {"disable-color", 0, 0, 's'},
847 {"timestamp", 0, 0, 'T'},
848 {"msc", 1, 0, 'm'},
849 {"local", 1, 0, 'l'},
850 {0, 0, 0, 0}
851 };
852
853 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
854 long_options, &option_index);
855 if (c == -1)
856 break;
857
858 switch (c) {
859 case 'h':
860 print_usage();
861 print_help();
862 exit(0);
863 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800864 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800865 break;
866 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800867 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800868 break;
869 case 'c':
870 config_file = strdup(optarg);
871 break;
872 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800873 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800874 break;
875 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800876 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800877 break;
878 case 'l':
879 inet_aton(optarg, &local_addr);
880 break;
881 default:
882 /* ignore */
883 break;
884 }
885 }
886}
887
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100888static void signal_handler(int signal)
889{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100890 switch (signal) {
891 case SIGABRT:
892 /* in case of abort, we want to obtain a talloc report
893 * and then return to the caller, who will abort the process */
894 case SIGUSR1:
895 talloc_report_full(tall_bsc_ctx, stderr);
896 break;
897 default:
898 break;
899 }
900}
901
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800902extern void *tall_msgb_ctx;
903extern void *tall_ctr_ctx;
904static void talloc_init_ctx()
905{
906 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
907 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
908 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
909}
910
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800911int main(int argc, char** argv)
912{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800913 talloc_init_ctx();
914
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +0800915
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800916 log_init(&log_info);
917 stderr_target = log_target_create_stderr();
918 log_add_target(stderr_target);
919 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800920
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800921 nat = bsc_nat_alloc();
922 if (!nat) {
923 fprintf(stderr, "Failed to allocate the BSC nat.\n");
924 return -4;
925 }
926
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800927 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800928 if (!nat->mgcp_cfg) {
929 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
930 return -5;
931 }
932
933 /* parse options */
934 local_addr.s_addr = INADDR_ANY;
935 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800936
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800937 /* init vty and parse */
938 bsc_nat_vty_init(nat);
939 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800940 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800941 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
942 return -3;
943 }
944
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800945 /* over rule the VTY config */
946 if (msc_ip)
947 bsc_nat_set_msc_ip(nat, msc_ip);
948
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800949 /* seed the PRNG */
950 srand(time(NULL));
951
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200952 /*
953 * Setup the MGCP code..
954 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800955 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200956 return -4;
957
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100958 /* connect to the MSC */
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200959 msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800960 if (!msc_con) {
961 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100962 exit(1);
963 }
964
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800965 msc_con->connection_loss = msc_connection_was_lost;
966 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
967 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
968 bsc_msc_connect(msc_con);
969
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100970 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100971 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100972 fprintf(stderr, "Failed to listen for BSC.\n");
973 exit(1);
974 }
975
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100976 signal(SIGABRT, &signal_handler);
977 signal(SIGUSR1, &signal_handler);
978 signal(SIGPIPE, SIG_IGN);
979
980 while (1) {
981 bsc_select_main(0);
982 }
983
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800984 return 0;
985}