blob: 7c9f6c73a556c85a8eb9b0838555a78a1bbd10e1 [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>
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +080026#include <netinet/tcp.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080027#include <arpa/inet.h>
28
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010029#include <errno.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010030#include <signal.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080031#include <stdio.h>
32#include <stdlib.h>
Holger Hans Peter Freyther5aa25ae2010-01-12 21:36:08 +010033#include <time.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080034#include <unistd.h>
35
36#define _GNU_SOURCE
37#include <getopt.h>
38
39#include <openbsc/debug.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010040#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +010042#include <openbsc/bssap.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080045#include <openbsc/telnet_interface.h>
46
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080047#include <osmocore/talloc.h>
48
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080049#include <vty/vty.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080050
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080051#include <sccp/sccp.h>
52
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080053#define SCCP_CLOSE_TIME 20
54#define SCCP_CLOSE_TIME_TIMEOUT 19
55
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080056struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080057static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080058static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010059static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080060static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080061static struct timer_list sccp_close;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010062
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010063
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064static struct bsc_nat *nat;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +020065static 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 +020066static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080067
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080068struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
69{
70 struct bsc_config *conf;
71
72 llist_for_each_entry(conf, &nat->bsc_configs, entry)
73 if (conf->nr == num)
74 return conf;
75
76 return NULL;
77}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010078
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010079/*
80 * below are stubs we need to link
81 */
82int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
Holger Hans Peter Freyther2f2d3422010-05-12 16:55:12 +080083 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state,
84 struct abis_om_obj_inst *obj_ins)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010085{
86 return -1;
87}
88
89void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
90{}
91
92int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
93{
94 return -1;
95}
96
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080097static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
98{
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +080099 if (write_queue_enqueue(&nat->msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800100 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
101 msgb_free(msg);
102 }
103}
104
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100105static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100106{
107 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100108 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
109 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
110 0x00, 0x01, 0x31,
111 };
112
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200113 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100114}
115
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800116static void send_ping(struct bsc_connection *bsc)
117{
118 static const u_int8_t id_ping[] = {
119 IPAC_MSGT_PING,
120 };
121
122 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
123}
124
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800125static void send_pong(struct bsc_connection *bsc)
126{
127 static const u_int8_t id_pong[] = {
128 IPAC_MSGT_PONG,
129 };
130
131 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
132}
133
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800134static void bsc_pong_timeout(void *_bsc)
135{
136 struct bsc_connection *bsc = _bsc;
137
138 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
139 bsc_close_connection(bsc);
140}
141
142static void bsc_ping_timeout(void *_bsc)
143{
144 struct bsc_connection *bsc = _bsc;
145
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800146 if (bsc->nat->ping_timeout < 0)
147 return;
148
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800149 send_ping(bsc);
150
151 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800152 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800153
154 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800155 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800156}
157
158static void start_ping_pong(struct bsc_connection *bsc)
159{
160 bsc->pong_timeout.data = bsc;
161 bsc->pong_timeout.cb = bsc_pong_timeout;
162 bsc->ping_timeout.data = bsc;
163 bsc->ping_timeout.cb = bsc_ping_timeout;
164
165 bsc_ping_timeout(bsc);
166}
167
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100168static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100169{
170 static const u_int8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200171 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100172 };
173
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200174 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100175}
176
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100177static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100178{
179 static const u_int8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200180 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100181 0x01, IPAC_IDTAG_UNIT,
182 0x01, IPAC_IDTAG_MACADDR,
183 0x01, IPAC_IDTAG_LOCATION1,
184 0x01, IPAC_IDTAG_LOCATION2,
185 0x01, IPAC_IDTAG_EQUIPVERS,
186 0x01, IPAC_IDTAG_SWVERSION,
187 0x01, IPAC_IDTAG_UNITNAME,
188 0x01, IPAC_IDTAG_SERNR,
189 };
190
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200191 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100192}
193
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200194static void nat_send_rlsd(struct sccp_connections *conn)
195{
196 struct sccp_connection_released *rel;
197 struct msgb *msg;
198
199 msg = msgb_alloc_headroom(4096, 128, "rlsd");
200 if (!msg) {
201 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
202 return;
203 }
204
205 msg->l2h = msgb_put(msg, sizeof(*rel));
206 rel = (struct sccp_connection_released *) msg->l2h;
207 rel->type = SCCP_MSG_TYPE_RLSD;
208 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
209 rel->destination_local_reference = conn->remote_ref;
210 rel->source_local_reference = conn->patched_ref;
211
212 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
213
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800214 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200215}
216
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200217static void nat_send_rlc(struct sccp_source_reference *src,
218 struct sccp_source_reference *dst)
219{
220 struct sccp_connection_release_complete *rlc;
221 struct msgb *msg;
222
223 msg = msgb_alloc_headroom(4096, 128, "rlc");
224 if (!msg) {
225 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
226 return;
227 }
228
229 msg->l2h = msgb_put(msg, sizeof(*rlc));
230 rlc = (struct sccp_connection_release_complete *) msg->l2h;
231 rlc->type = SCCP_MSG_TYPE_RLC;
232 rlc->destination_local_reference = *dst;
233 rlc->source_local_reference = *src;
234
235 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
236
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800237 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200238}
239
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200240static void send_mgcp_reset(struct bsc_connection *bsc)
241{
242 static const u_int8_t mgcp_reset[] = {
243 "RSIP 1 13@mgw MGCP 1.0\r\n"
244 };
245
246 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
247}
248
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100249/*
250 * Below is the handling of messages coming
251 * from the MSC and need to be forwarded to
252 * a real BSC.
253 */
254static void initialize_msc_if_needed()
255{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200256 if (nat->first_contact)
257 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100258
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200259 nat->first_contact = 1;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800260 msc_send_reset(nat->msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100261}
262
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100263/*
264 * Currently we are lacking refcounting so we need to copy each message.
265 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200266static 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 +0100267{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100268 struct msgb *msg;
269
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200270 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100271 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
272 return;
273 }
274
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200275 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100276 if (!msg) {
277 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
278 return;
279 }
280
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200281 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100282 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200283
284 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100285}
286
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100287static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100288{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800289 struct sccp_connections *con;
290 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800291 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200292 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100293
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100294 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800295 parsed = bsc_nat_parse(msg);
296 if (!parsed) {
297 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100298 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800299 }
300
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100301 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800302 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800303
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200304 proto = parsed->ipa_proto;
305
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100306 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200307 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100308 switch (parsed->sccp_type) {
309 case SCCP_MSG_TYPE_UDT:
310 /* forward UDT messages to every BSC */
311 goto send_to_all;
312 break;
313 case SCCP_MSG_TYPE_RLSD:
314 case SCCP_MSG_TYPE_CREF:
315 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200316 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800317 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800318 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200319 counter_inc(nat->stats.sccp.calls);
320
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800321 if (con) {
Holger Hans Peter Freyther3d0049f2010-04-18 01:35:41 +0800322 counter_inc(con->bsc->cfg->stats.sccp.calls);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800323 if (bsc_mgcp_assign(con, msg) != 0)
324 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
325 } else
326 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
327 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200328 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100329 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800330 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
331 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200332 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800333 break;
334 case SCCP_MSG_TYPE_RLC:
335 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
336 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100337 break;
338 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100339 /* MSC never opens a SCCP connection, fall through */
340 default:
341 goto exit;
342 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200343
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200344 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
345 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
346 /* Exchange src/dest for the reply */
347 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
348 } else if (!con)
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200349 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100350 }
351
352 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800353 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100354 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800355 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800356 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100357 return -1;
358 }
359
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200360 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100361 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100362
363send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800364 /*
365 * Filter Paging from the network. We do not want to send a PAGING
366 * Command to every BSC in our network. We will analys the PAGING
367 * message and then send it to the authenticated messages...
368 */
369 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200370 int lac;
371 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800372 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800373 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800374 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200375 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200376 else
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200377 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
378 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800379
380 goto exit;
381 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100382 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800383 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100384 if (!bsc->authenticated)
385 continue;
386
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200387 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100388 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800389
390exit:
391 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100392 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100393}
394
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800395static void msc_connection_was_lost(struct bsc_msc_connection *con)
396{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200397 struct bsc_connection *bsc, *tmp;
398
Holger Hans Peter Freythera4ed81c2010-04-12 12:35:02 +0200399 counter_inc(nat->stats.msc.reconn);
400
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200401 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
402 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800403 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200404
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200405 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200406 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200407 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800408}
409
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200410static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200411{
412 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200413 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200414 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
415 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
416 0x01, 0x20
417 };
418
419 struct msgb *msg;
420
421 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
422 if (!msg) {
423 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
424 return;
425 }
426
427 msg->l2h = msgb_put(msg, sizeof(reset));
428 memcpy(msg->l2h, reset, msgb_l2len(msg));
429
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800430 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200431
432 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
433}
434
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800435static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100436{
437 int error;
438 struct msgb *msg = ipaccess_read_msg(bfd, &error);
439 struct ipaccess_head *hh;
440
441 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800442 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100443 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800444 else
445 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100446
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800447 bsc_msc_lost(nat->msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100448 return -1;
449 }
450
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100451 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 +0100452
453 /* handle base message handling */
454 hh = (struct ipaccess_head *) msg->data;
455 ipaccess_rcvmsg_base(msg, bfd);
456
457 /* initialize the networking. This includes sending a GSM08.08 message */
458 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
459 initialize_msc_if_needed();
460 else if (hh->proto == IPAC_PROTO_SCCP)
461 forward_sccp_to_bts(msg);
462
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800463 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100464 return 0;
465}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800466
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800467static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
468{
469 int rc;
470 rc = write(bfd->fd, msg->data, msg->len);
471
472 if (rc != msg->len) {
473 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
474 return -1;
475 }
476
477 return rc;
478}
479
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100480/*
481 * Below is the handling of messages coming
482 * from the BSC and need to be forwarded to
483 * a real BSC.
484 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100485
486/*
487 * Remove the connection from the connections list,
488 * remove it from the patching of SCCP header lists
489 * as well. Maybe in the future even close connection..
490 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800491void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100492{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100493 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100494
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800495 /* stop the timeout timer */
496 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800497 bsc_del_timer(&connection->ping_timeout);
498 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800499
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100500 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800501 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100502 if (sccp_patch->bsc != connection)
503 continue;
504
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800505 if (sccp_patch->has_remote_ref)
506 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200507 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100508 }
509
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200510 /* close endpoints allocated by this BSC */
511 bsc_mgcp_clear_endpoints_for(connection);
512
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800513 bsc_unregister_fd(&connection->write_queue.bfd);
514 close(connection->write_queue.bfd.fd);
515 write_queue_clear(&connection->write_queue);
516 llist_del(&connection->list_entry);
517
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100518 talloc_free(connection);
519}
520
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800521static void ipaccess_close_bsc(void *data)
522{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200523 struct sockaddr_in sock;
524 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800525 struct bsc_connection *conn = data;
526
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200527
528 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
529 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
530 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800531 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800532}
533
534static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
535{
536 struct bsc_config *conf;
537 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
538
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800539 if (bsc->cfg) {
540 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
541 bsc->write_queue.bfd.fd, bsc->cfg->nr);
542 return;
543 }
544
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800545 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
546 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200547 counter_inc(conf->stats.net.reconn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800548 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200549 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800550 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800551 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
552 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800553 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200554 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800555 }
556 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200557
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800558 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
559 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800560}
561
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100562static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100563{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800564 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800565 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100566
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800567 /* Parse and filter messages */
568 parsed = bsc_nat_parse(msg);
569 if (!parsed) {
570 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200571 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800572 return -1;
573 }
574
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100575 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800576 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800577
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200578 /*
579 * check authentication after filtering to not reject auth
580 * responses coming from the BSC. We have to make sure that
581 * nothing from the exit path will forward things to the MSC
582 */
583 if (!bsc->authenticated) {
584 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
585 msgb_free(msg);
586 return -1;
587 }
588
589
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100590 /* modify the SCCP entries */
591 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
592 switch (parsed->sccp_type) {
593 case SCCP_MSG_TYPE_CR:
594 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
595 goto exit2;
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_RLSD:
599 case SCCP_MSG_TYPE_CREF:
600 case SCCP_MSG_TYPE_DT1:
601 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200602 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800603 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100604 break;
605 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800606 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200607 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100608 break;
609 case SCCP_MSG_TYPE_UDT:
610 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800611 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100612 break;
613 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800614 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 +0800615 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100616 goto exit2;
617 break;
618 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200619 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
620 bsc_mgcp_forward(bsc, msg);
621 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800622 } else {
623 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
624 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100625 }
626
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800627 if (con && con->bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800628 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
629 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100630 goto exit2;
631 }
632
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100633 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800634 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100635 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800636 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800637
638exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100639 /* if we filter out the reset send an ack to the BSC */
640 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100641 send_reset_ack(bsc);
642 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800643 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
644 /* do we know who is handling this? */
645 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
646 struct tlv_parsed tvp;
647 ipaccess_idtag_parse(&tvp,
648 (unsigned char *) msg->l2h + 2,
649 msgb_l2len(msg) - 2);
650 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
651 ipaccess_auth_bsc(&tvp, bsc);
652 }
653
654 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100655 }
656
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100657exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800658 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800659 msgb_free(msg);
660 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100661}
662
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800663static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100664{
665 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100666 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100667 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800668 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100669
670 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800671 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800672 LOGP(DNAT, LOGL_ERROR,
673 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
674 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800675 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800676 LOGP(DNAT, LOGL_ERROR,
677 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
678 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800679
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800680 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100681 return -1;
682 }
683
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100684
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100685 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 +0100686
687 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800688 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800689
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800690 /* stop the pong timeout */
691 if (hh->proto == IPAC_PROTO_IPACCESS) {
692 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800693 bsc_del_timer(&bsc->pong_timeout);
694 msgb_free(msg);
695 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800696 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
697 send_pong(bsc);
698 msgb_free(msg);
699 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800700 }
701 }
702
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100703 /* FIXME: Currently no PONG is sent to the BSC */
704 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100705 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100706
707 return 0;
708}
709
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100710static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
711{
712 int rc;
713
714 rc = write(bfd->fd, msg->data, msg->len);
715 if (rc != msg->len)
716 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
717
718 return rc;
719}
720
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100721static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
722{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100723 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800724 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100725 struct sockaddr_in sa;
726 socklen_t sa_len = sizeof(sa);
727
728 if (!(what & BSC_FD_READ))
729 return 0;
730
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800731 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
732 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100733 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800734 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100735 }
736
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200737 /* count the reconnect */
738 counter_inc(nat->stats.bsc.reconn);
739
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200740 /*
741 * if we are not connected to a msc... just close the socket
742 */
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800743 if (!nat->msc_con->is_connected) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200744 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800745 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200746 return 0;
747 }
748
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800749 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +0800750 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800751 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800752 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
753
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100754 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100755 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100756
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100757 /*
758 *
759 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800760 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100761 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100762 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800763 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100764 return -1;
765 }
766
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800767 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800768 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800769 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100770 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800771 bsc->write_queue.bfd.when = BSC_FD_READ;
772 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100773 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800774 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100775 talloc_free(bsc);
776 return -2;
777 }
778
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800779 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800780 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800781 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100782 send_id_ack(bsc);
783 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200784 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800785
786 /*
787 * start the hangup timer
788 */
789 bsc->id_timeout.data = bsc;
790 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800791 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100792 return 0;
793}
794
795static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
796{
797 struct sockaddr_in addr;
798 int ret, on = 1;
799
800 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
801 bfd->cb = ipaccess_listen_bsc_cb;
802 bfd->when = BSC_FD_READ;
803
804 memset(&addr, 0, sizeof(addr));
805 addr.sin_family = AF_INET;
806 addr.sin_port = htons(port);
807 addr.sin_addr.s_addr = in_addr->s_addr;
808
809 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
810
811 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
812 if (ret < 0) {
813 fprintf(stderr, "Could not bind the BSC socket %s\n",
814 strerror(errno));
815 return -EIO;
816 }
817
818 ret = listen(bfd->fd, 1);
819 if (ret < 0) {
820 perror("listen");
821 return ret;
822 }
823
824 ret = bsc_register_fd(bfd);
825 if (ret < 0) {
826 perror("register_listen_fd");
827 return ret;
828 }
829 return 0;
830}
831
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800832static void print_usage()
833{
834 printf("Usage: bsc_nat\n");
835}
836
837static void print_help()
838{
839 printf(" Some useful help...\n");
840 printf(" -h --help this text\n");
841 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
842 printf(" -s --disable-color\n");
843 printf(" -c --config-file filename The config file to use.\n");
844 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100845 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800846}
847
848static void handle_options(int argc, char** argv)
849{
850 while (1) {
851 int option_index = 0, c;
852 static struct option long_options[] = {
853 {"help", 0, 0, 'h'},
854 {"debug", 1, 0, 'd'},
855 {"config-file", 1, 0, 'c'},
856 {"disable-color", 0, 0, 's'},
857 {"timestamp", 0, 0, 'T'},
858 {"msc", 1, 0, 'm'},
859 {"local", 1, 0, 'l'},
860 {0, 0, 0, 0}
861 };
862
863 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
864 long_options, &option_index);
865 if (c == -1)
866 break;
867
868 switch (c) {
869 case 'h':
870 print_usage();
871 print_help();
872 exit(0);
873 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800874 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800875 break;
876 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800877 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800878 break;
879 case 'c':
880 config_file = strdup(optarg);
881 break;
882 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800883 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800884 break;
885 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800886 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800887 break;
888 case 'l':
889 inet_aton(optarg, &local_addr);
890 break;
891 default:
892 /* ignore */
893 break;
894 }
895 }
896}
897
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100898static void signal_handler(int signal)
899{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100900 switch (signal) {
901 case SIGABRT:
902 /* in case of abort, we want to obtain a talloc report
903 * and then return to the caller, who will abort the process */
904 case SIGUSR1:
905 talloc_report_full(tall_bsc_ctx, stderr);
906 break;
907 default:
908 break;
909 }
910}
911
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +0800912static void sccp_close_unconfirmed(void *_data)
913{
914 struct sccp_connections *conn, *tmp1;
915 struct timespec now;
916 clock_gettime(CLOCK_MONOTONIC, &now);
917
918 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
919 if (conn->has_remote_ref)
920 continue;
921
922 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
923 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
924 continue;
925
926 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
927 sccp_src_ref_to_int(&conn->real_ref),
928 sccp_src_ref_to_int(&conn->patched_ref));
929 sccp_connection_destroy(conn);
930 }
931
932 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
933}
934
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800935extern void *tall_msgb_ctx;
936extern void *tall_ctr_ctx;
937static void talloc_init_ctx()
938{
939 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
940 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
941 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
942}
943
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800944int main(int argc, char** argv)
945{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800946 talloc_init_ctx();
947
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +0800948
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800949 log_init(&log_info);
950 stderr_target = log_target_create_stderr();
951 log_add_target(stderr_target);
952 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800953
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800954 nat = bsc_nat_alloc();
955 if (!nat) {
956 fprintf(stderr, "Failed to allocate the BSC nat.\n");
957 return -4;
958 }
959
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800960 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800961 if (!nat->mgcp_cfg) {
962 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
963 return -5;
964 }
965
966 /* parse options */
967 local_addr.s_addr = INADDR_ANY;
968 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800969
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800970 /* init vty and parse */
971 bsc_nat_vty_init(nat);
972 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800973 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800974 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
975 return -3;
976 }
977
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800978 /* over rule the VTY config */
979 if (msc_ip)
980 bsc_nat_set_msc_ip(nat, msc_ip);
981
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800982 /* seed the PRNG */
983 srand(time(NULL));
984
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200985 /*
986 * Setup the MGCP code..
987 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800988 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200989 return -4;
990
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100991 /* connect to the MSC */
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800992 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
993 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800994 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100995 exit(1);
996 }
997
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800998 nat->msc_con->connection_loss = msc_connection_was_lost;
999 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1000 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
1001 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001002
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001003 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +01001004 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001005 fprintf(stderr, "Failed to listen for BSC.\n");
1006 exit(1);
1007 }
1008
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001009 signal(SIGABRT, &signal_handler);
1010 signal(SIGUSR1, &signal_handler);
1011 signal(SIGPIPE, SIG_IGN);
1012
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001013 /* recycle timer */
1014 sccp_close.cb = sccp_close_unconfirmed;
1015 sccp_close.data = NULL;
1016 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1017
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001018 while (1) {
1019 bsc_select_main(0);
1020 }
1021
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001022 return 0;
1023}