blob: 23b811117e05880e0bd4fba912c51c0f8e713908 [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 Freytherc2b31ed2010-07-31 05:17:17 +080042#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080045#include <openbsc/vty.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080046
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +080047#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080048#include <osmocore/talloc.h>
Harald Welte2c869ef2010-08-25 19:43:54 +020049#include <osmocore/process.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080050
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080051#include <osmocore/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080052
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080053#include <osmocom/vty/telnet_interface.h>
54#include <osmocom/vty/vty.h>
55
Harald Welted5db12c2010-08-03 15:11:51 +020056#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080057
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080058#include "../../bscconfig.h"
59
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080060#define SCCP_CLOSE_TIME 20
61#define SCCP_CLOSE_TIME_TIMEOUT 19
62
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080063struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080065static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010066static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080067static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080068static struct timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020069static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010070
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080071const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080072 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
73 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\r\n"
74 "This is free software: you are free to change and redistribute it.\r\n"
75 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010076
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080077static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080078static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int);
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +020079static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080080
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080081struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
82{
83 struct bsc_config *conf;
84
85 llist_for_each_entry(conf, &nat->bsc_configs, entry)
86 if (conf->nr == num)
87 return conf;
88
89 return NULL;
90}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010091
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010092/*
93 * below are stubs we need to link
94 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080095int nm_state_event(enum nm_evt evt, uint8_t obj_class, void *obj,
Holger Hans Peter Freyther2f2d3422010-05-12 16:55:12 +080096 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state,
97 struct abis_om_obj_inst *obj_ins)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010098{
99 return -1;
100}
101
102void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
103{}
104
Holger Hans Peter Freyther4fcce9e2010-09-16 06:33:27 +0800105static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) __attribute__((nonnull (1, 2)));
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800106static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
107{
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800108 if (write_queue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800109 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
110 msgb_free(msg);
111 }
112}
113
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100114static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100115{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800116 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100117 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
118 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
119 0x00, 0x01, 0x31,
120 };
121
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200122 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100123}
124
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800125static void send_ping(struct bsc_connection *bsc)
126{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800127 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800128 IPAC_MSGT_PING,
129 };
130
131 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
132}
133
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800134static void send_pong(struct bsc_connection *bsc)
135{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800136 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800137 IPAC_MSGT_PONG,
138 };
139
140 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
141}
142
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800143static void bsc_pong_timeout(void *_bsc)
144{
145 struct bsc_connection *bsc = _bsc;
146
147 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
148 bsc_close_connection(bsc);
149}
150
151static void bsc_ping_timeout(void *_bsc)
152{
153 struct bsc_connection *bsc = _bsc;
154
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800155 if (bsc->nat->ping_timeout < 0)
156 return;
157
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800158 send_ping(bsc);
159
160 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800161 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800162
163 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800164 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800165}
166
167static void start_ping_pong(struct bsc_connection *bsc)
168{
169 bsc->pong_timeout.data = bsc;
170 bsc->pong_timeout.cb = bsc_pong_timeout;
171 bsc->ping_timeout.data = bsc;
172 bsc->ping_timeout.cb = bsc_ping_timeout;
173
174 bsc_ping_timeout(bsc);
175}
176
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100177static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100178{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800179 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200180 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100181 };
182
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200183 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100184}
185
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100186static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100187{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800188 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200189 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100190 0x01, IPAC_IDTAG_UNIT,
191 0x01, IPAC_IDTAG_MACADDR,
192 0x01, IPAC_IDTAG_LOCATION1,
193 0x01, IPAC_IDTAG_LOCATION2,
194 0x01, IPAC_IDTAG_EQUIPVERS,
195 0x01, IPAC_IDTAG_SWVERSION,
196 0x01, IPAC_IDTAG_UNITNAME,
197 0x01, IPAC_IDTAG_SERNR,
198 };
199
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200200 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100201}
202
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200203static void nat_send_rlsd(struct sccp_connections *conn)
204{
205 struct sccp_connection_released *rel;
206 struct msgb *msg;
207
208 msg = msgb_alloc_headroom(4096, 128, "rlsd");
209 if (!msg) {
210 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
211 return;
212 }
213
214 msg->l2h = msgb_put(msg, sizeof(*rel));
215 rel = (struct sccp_connection_released *) msg->l2h;
216 rel->type = SCCP_MSG_TYPE_RLSD;
217 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
218 rel->destination_local_reference = conn->remote_ref;
219 rel->source_local_reference = conn->patched_ref;
220
221 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
222
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800223 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200224}
225
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800226static void nat_send_rlc(struct bsc_msc_connection *msc_con,
227 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200228 struct sccp_source_reference *dst)
229{
230 struct sccp_connection_release_complete *rlc;
231 struct msgb *msg;
232
233 msg = msgb_alloc_headroom(4096, 128, "rlc");
234 if (!msg) {
235 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
236 return;
237 }
238
239 msg->l2h = msgb_put(msg, sizeof(*rlc));
240 rlc = (struct sccp_connection_release_complete *) msg->l2h;
241 rlc->type = SCCP_MSG_TYPE_RLC;
242 rlc->destination_local_reference = *dst;
243 rlc->source_local_reference = *src;
244
245 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
246
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800247 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200248}
249
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200250static void send_mgcp_reset(struct bsc_connection *bsc)
251{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800252 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200253 "RSIP 1 13@mgw MGCP 1.0\r\n"
254 };
255
256 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
257}
258
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100259/*
260 * Below is the handling of messages coming
261 * from the MSC and need to be forwarded to
262 * a real BSC.
263 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800264static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100265{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800266 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200267 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100268
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800269 msc_con->first_contact = 1;
270 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100271}
272
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800273static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800274{
275 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
276 if (!msg)
277 return;
278
279 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800280 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800281}
282
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100283/*
284 * Currently we are lacking refcounting so we need to copy each message.
285 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800286static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int proto)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100287{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100288 struct msgb *msg;
289
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200290 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100291 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
292 return;
293 }
294
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200295 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100296 if (!msg) {
297 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
298 return;
299 }
300
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200301 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100302 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200303
304 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100305}
306
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800307/*
308 * Release an established connection. We will have to release it to the BSC
309 * and to the network and we do it the following way.
310 * 1.) Give up on the MSC side
311 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
312 * ignore the RLC... we might complain about it. Other options would
313 * be to send a Release Request, handle the Release Complete..
314 * 1.2) Mark the data structure to be con_local and wait for 2nd
315 *
316 * 2.) Give up on the BSC side
317 * 2.1) Depending on the con type reject the service, or just close it
318 */
319static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
320{
321 struct msgb *rlsd;
322 /* 1. release the network */
323 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
324 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
325 if (!rlsd)
326 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
327 else {
328 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
329 queue_for_msc(con->msc_con, rlsd);
330 }
331 con->con_local = 1;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800332 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800333
334 /* 2. release the BSC side */
335 if (con->con_type == NAT_CON_TYPE_LU) {
336 struct msgb *payload, *udt;
337 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
338
339 if (payload) {
340 gsm0808_prepend_dtap_header(payload, 0);
341 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
342 if (udt)
343 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
344 else
345 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
346
347 msgb_free(payload);
348 } else {
349 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
350 }
351 }
352
353 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
354 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
355 if (!rlsd) {
356 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
357 sccp_connection_destroy(con);
358 return;
359 }
360
361 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
362 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
363}
364
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800365static void bsc_send_con_refuse(struct bsc_connection *bsc,
366 struct bsc_nat_parsed *parsed, int con_type)
367{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800368 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800369 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800370
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800371 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800372 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800373 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800374 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800375 else {
376 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
377 payload = NULL;
378 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800379
380 /*
381 * Some BSCs do not handle the payload inside a SCCP CREF msg
382 * so we will need to:
383 * 1.) Allocate a local connection and mark it as local..
384 * 2.) queue data for downstream.. and the RLC should delete everything
385 */
386 if (payload) {
387 struct msgb *cc, *udt, *rlsd;
388 struct sccp_connections *con;
389 con = create_sccp_src_ref(bsc, parsed);
390 if (!con)
391 goto send_refuse;
392
393 /* declare it local and assign a unique remote_ref */
394 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
395 con->con_local = 1;
396 con->has_remote_ref = 1;
397 con->remote_ref = con->patched_ref;
398
399 /* 1. create a confirmation */
400 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
401 if (!cc)
402 goto send_refuse;
403
404 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800405 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800406 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
407 if (!udt) {
408 msgb_free(cc);
409 goto send_refuse;
410 }
411
412 /* 3. send a RLSD */
413 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
414 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
415 if (!rlsd) {
416 msgb_free(cc);
417 msgb_free(udt);
418 goto send_refuse;
419 }
420
421 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
422 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
423 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
424 msgb_free(payload);
425 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800426 }
427
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800428
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800429send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800430 if (payload)
431 msgb_free(payload);
432
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800433 refuse = sccp_create_refuse(parsed->src_local_ref,
434 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800435 if (!refuse) {
436 LOGP(DNAT, LOGL_ERROR,
437 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
438 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
439 return;
440 }
441
442 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
443}
444
445
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800446static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100447{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800448 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800449 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800450 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200451 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100452
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100453 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800454 parsed = bsc_nat_parse(msg);
455 if (!parsed) {
456 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100457 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800458 }
459
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100460 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800461 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800462
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200463 proto = parsed->ipa_proto;
464
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100465 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200466 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100467 switch (parsed->sccp_type) {
468 case SCCP_MSG_TYPE_UDT:
469 /* forward UDT messages to every BSC */
470 goto send_to_all;
471 break;
472 case SCCP_MSG_TYPE_RLSD:
473 case SCCP_MSG_TYPE_CREF:
474 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200475 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800476 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800477 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200478 counter_inc(nat->stats.sccp.calls);
479
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800480 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800481 struct rate_ctr_group *ctrg;
482 ctrg = con->bsc->cfg->stats.ctrg;
483 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800484 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800485 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
486 } else
487 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
488 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200489 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100490 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800491 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
492 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200493 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800494 break;
495 case SCCP_MSG_TYPE_RLC:
496 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
497 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100498 break;
499 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100500 /* MSC never opens a SCCP connection, fall through */
501 default:
502 goto exit;
503 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200504
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200505 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
506 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
507 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800508 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200509 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800510 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x from the MSC.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100511 }
512
513 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800514 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100515 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800516 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800517 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100518 return -1;
519 }
520
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200521 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100522 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100523
524send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800525 /*
526 * Filter Paging from the network. We do not want to send a PAGING
527 * Command to every BSC in our network. We will analys the PAGING
528 * message and then send it to the authenticated messages...
529 */
530 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200531 int lac;
532 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800533 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800534 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800535 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200536 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherda340632010-08-06 20:15:40 +0800537 else if (lac != -1)
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200538 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
539 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800540
541 goto exit;
542 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100543 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800544 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100545 if (!bsc->authenticated)
546 continue;
547
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200548 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100549 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800550
551exit:
552 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100553 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100554}
555
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800556static void msc_connection_was_lost(struct bsc_msc_connection *con)
557{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200558 struct bsc_connection *bsc, *tmp;
559
560 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
561 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800562 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200563
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200564 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200565 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800566}
567
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800568static void msc_connection_connected(struct bsc_msc_connection *con)
569{
570 counter_inc(nat->stats.msc.reconn);
571}
572
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200573static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200574{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800575 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200576 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200577 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
578 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
579 0x01, 0x20
580 };
581
582 struct msgb *msg;
583
584 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
585 if (!msg) {
586 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
587 return;
588 }
589
590 msg->l2h = msgb_put(msg, sizeof(reset));
591 memcpy(msg->l2h, reset, msgb_l2len(msg));
592
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800593 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200594
595 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
596}
597
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800598static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100599{
600 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800601 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100602 struct msgb *msg = ipaccess_read_msg(bfd, &error);
603 struct ipaccess_head *hh;
604
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800605 msc_con = (struct bsc_msc_connection *) bfd->data;
606
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100607 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800608 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100609 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800610 else
611 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100612
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800613 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100614 return -1;
615 }
616
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100617 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 +0100618
619 /* handle base message handling */
620 hh = (struct ipaccess_head *) msg->data;
621 ipaccess_rcvmsg_base(msg, bfd);
622
623 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800624 if (hh->proto == IPAC_PROTO_IPACCESS) {
625 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800626 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800627 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800628 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800629 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800630 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100631
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800632 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100633 return 0;
634}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800635
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800636static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
637{
638 int rc;
639 rc = write(bfd->fd, msg->data, msg->len);
640
641 if (rc != msg->len) {
642 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
643 return -1;
644 }
645
646 return rc;
647}
648
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100649/*
650 * Below is the handling of messages coming
651 * from the BSC and need to be forwarded to
652 * a real BSC.
653 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100654
655/*
656 * Remove the connection from the connections list,
657 * remove it from the patching of SCCP header lists
658 * as well. Maybe in the future even close connection..
659 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800660void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100661{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100662 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800663 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100664
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800665 /* stop the timeout timer */
666 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800667 bsc_del_timer(&connection->ping_timeout);
668 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800669
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800670 if (connection->cfg)
671 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
672
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100673 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800674 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100675 if (sccp_patch->bsc != connection)
676 continue;
677
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800678 if (ctr)
679 rate_ctr_inc(ctr);
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800680 if (sccp_patch->has_remote_ref && !sccp_patch->con_local)
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800681 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200682 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100683 }
684
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200685 /* close endpoints allocated by this BSC */
686 bsc_mgcp_clear_endpoints_for(connection);
687
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800688 bsc_unregister_fd(&connection->write_queue.bfd);
689 close(connection->write_queue.bfd.fd);
690 write_queue_clear(&connection->write_queue);
691 llist_del(&connection->list_entry);
692
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100693 talloc_free(connection);
694}
695
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800696static void ipaccess_close_bsc(void *data)
697{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200698 struct sockaddr_in sock;
699 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800700 struct bsc_connection *conn = data;
701
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200702
703 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
704 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
705 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800706 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800707}
708
709static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
710{
711 struct bsc_config *conf;
712 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
713
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800714 if (bsc->cfg) {
715 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
716 bsc->write_queue.bfd.fd, bsc->cfg->nr);
717 return;
718 }
719
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800720 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
721 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800722 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800723 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200724 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800725 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800726 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
727 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800728 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200729 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800730 }
731 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200732
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800733 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
734 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800735}
736
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100737static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100738{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800739 int con_filter = 0;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800740 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800741 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800742 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800743 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100744
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800745 /* Parse and filter messages */
746 parsed = bsc_nat_parse(msg);
747 if (!parsed) {
748 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200749 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800750 return -1;
751 }
752
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100753 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800754 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800755
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200756 /*
757 * check authentication after filtering to not reject auth
758 * responses coming from the BSC. We have to make sure that
759 * nothing from the exit path will forward things to the MSC
760 */
761 if (!bsc->authenticated) {
762 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
763 msgb_free(msg);
764 return -1;
765 }
766
767
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100768 /* modify the SCCP entries */
769 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800770 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800771 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100772 switch (parsed->sccp_type) {
773 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800774 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type);
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800775 if (filter < 0)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800776 goto exit3;
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800777 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100778 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800779 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800780 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800781 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800782 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800783 con->imsi_checked = filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800784 con_bsc = con->bsc;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100785 break;
786 case SCCP_MSG_TYPE_RLSD:
787 case SCCP_MSG_TYPE_CREF:
788 case SCCP_MSG_TYPE_DT1:
789 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200790 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800791 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800792 if (con) {
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800793 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
794 if (filter == 0) {
795 con_bsc = con->bsc;
796 con_msc = con->msc_con;
797 con_filter = con->con_local;
798 } else {
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800799 bsc_send_con_release(bsc, con);
800 con = NULL;
801 goto exit2;
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800802 }
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800803 }
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100804 break;
805 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800806 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800807 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800808 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800809 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800810 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800811 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200812 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100813 break;
814 case SCCP_MSG_TYPE_UDT:
815 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800816 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100817 break;
818 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800819 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 +0800820 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100821 goto exit2;
822 break;
823 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200824 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
825 bsc_mgcp_forward(bsc, msg);
826 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800827 } else {
828 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
829 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100830 }
831
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800832 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800833 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800834 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100835 goto exit2;
836 }
837
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +0800838 /* do not forward messages to the MSC */
839 if (con_filter)
840 goto exit2;
841
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800842 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +0800843 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
844 bsc->cfg->nr,
845 parsed ? parsed->ipa_proto : -1,
846 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800847 goto exit2;
848 }
849
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100850 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800851 queue_for_msc(con_msc, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100852 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800853 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800854
855exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100856 /* if we filter out the reset send an ack to the BSC */
857 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100858 send_reset_ack(bsc);
859 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800860 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
861 /* do we know who is handling this? */
862 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
863 struct tlv_parsed tvp;
864 ipaccess_idtag_parse(&tvp,
865 (unsigned char *) msg->l2h + 2,
866 msgb_l2len(msg) - 2);
867 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
868 ipaccess_auth_bsc(&tvp, bsc);
869 }
870
871 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100872 }
873
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +0800874exit2:
875 talloc_free(parsed);
876 msgb_free(msg);
877 return -1;
878
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800879exit3:
880 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800881 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800882 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800883 msgb_free(msg);
884 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100885}
886
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800887static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100888{
889 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100890 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100891 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800892 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100893
894 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800895 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800896 LOGP(DNAT, LOGL_ERROR,
897 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
898 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800899 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800900 LOGP(DNAT, LOGL_ERROR,
901 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
902 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800903
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800904 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100905 return -1;
906 }
907
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100908
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100909 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 +0100910
911 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800912 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800913
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800914 /* stop the pong timeout */
915 if (hh->proto == IPAC_PROTO_IPACCESS) {
916 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800917 bsc_del_timer(&bsc->pong_timeout);
918 msgb_free(msg);
919 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800920 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
921 send_pong(bsc);
922 msgb_free(msg);
923 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800924 }
925 }
926
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100927 /* FIXME: Currently no PONG is sent to the BSC */
928 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100929 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100930
931 return 0;
932}
933
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100934static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
935{
936 int rc;
937
938 rc = write(bfd->fd, msg->data, msg->len);
939 if (rc != msg->len)
940 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
941
942 return rc;
943}
944
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100945static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
946{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100947 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800948 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100949 struct sockaddr_in sa;
950 socklen_t sa_len = sizeof(sa);
951
952 if (!(what & BSC_FD_READ))
953 return 0;
954
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800955 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
956 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100957 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800958 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100959 }
960
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200961 /* count the reconnect */
962 counter_inc(nat->stats.bsc.reconn);
963
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200964 /*
965 * if we are not connected to a msc... just close the socket
966 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800967 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200968 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800969 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200970 return 0;
971 }
972
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800973 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +0800974 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800975 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800976 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
977
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +0800978 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +0800979 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +0800980 if (rc != 0)
981 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
982
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100983 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100984 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100985
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100986 /*
987 *
988 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800989 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100990 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100991 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800992 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100993 return -1;
994 }
995
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800996 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800997 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800998 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100999 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001000 bsc->write_queue.bfd.when = BSC_FD_READ;
1001 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001002 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001003 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001004 talloc_free(bsc);
1005 return -2;
1006 }
1007
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001008 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001009 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001010 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001011 send_id_ack(bsc);
1012 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001013 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001014
1015 /*
1016 * start the hangup timer
1017 */
1018 bsc->id_timeout.data = bsc;
1019 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +08001020 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001021 return 0;
1022}
1023
1024static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
1025{
1026 struct sockaddr_in addr;
1027 int ret, on = 1;
1028
1029 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1030 bfd->cb = ipaccess_listen_bsc_cb;
1031 bfd->when = BSC_FD_READ;
1032
1033 memset(&addr, 0, sizeof(addr));
1034 addr.sin_family = AF_INET;
1035 addr.sin_port = htons(port);
1036 addr.sin_addr.s_addr = in_addr->s_addr;
1037
1038 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1039
1040 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
1041 if (ret < 0) {
Holger Hans Peter Freytherd368a712010-09-05 08:51:12 +08001042 fprintf(stderr, "Could not bind the BSC socket: %s\n",
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001043 strerror(errno));
1044 return -EIO;
1045 }
1046
1047 ret = listen(bfd->fd, 1);
1048 if (ret < 0) {
1049 perror("listen");
1050 return ret;
1051 }
1052
1053 ret = bsc_register_fd(bfd);
1054 if (ret < 0) {
1055 perror("register_listen_fd");
1056 return ret;
1057 }
1058 return 0;
1059}
1060
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001061static void print_usage()
1062{
1063 printf("Usage: bsc_nat\n");
1064}
1065
1066static void print_help()
1067{
1068 printf(" Some useful help...\n");
1069 printf(" -h --help this text\n");
1070 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001071 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001072 printf(" -s --disable-color\n");
1073 printf(" -c --config-file filename The config file to use.\n");
1074 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001075 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001076}
1077
1078static void handle_options(int argc, char** argv)
1079{
1080 while (1) {
1081 int option_index = 0, c;
1082 static struct option long_options[] = {
1083 {"help", 0, 0, 'h'},
1084 {"debug", 1, 0, 'd'},
1085 {"config-file", 1, 0, 'c'},
1086 {"disable-color", 0, 0, 's'},
1087 {"timestamp", 0, 0, 'T'},
1088 {"msc", 1, 0, 'm'},
1089 {"local", 1, 0, 'l'},
1090 {0, 0, 0, 0}
1091 };
1092
1093 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1094 long_options, &option_index);
1095 if (c == -1)
1096 break;
1097
1098 switch (c) {
1099 case 'h':
1100 print_usage();
1101 print_help();
1102 exit(0);
1103 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001104 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001105 break;
1106 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001107 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001108 break;
1109 case 'c':
1110 config_file = strdup(optarg);
1111 break;
1112 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001113 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001114 break;
1115 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001116 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001117 break;
1118 case 'l':
1119 inet_aton(optarg, &local_addr);
1120 break;
1121 default:
1122 /* ignore */
1123 break;
1124 }
1125 }
1126}
1127
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001128static void signal_handler(int signal)
1129{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001130 switch (signal) {
1131 case SIGABRT:
1132 /* in case of abort, we want to obtain a talloc report
1133 * and then return to the caller, who will abort the process */
1134 case SIGUSR1:
1135 talloc_report_full(tall_bsc_ctx, stderr);
1136 break;
1137 default:
1138 break;
1139 }
1140}
1141
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001142static void sccp_close_unconfirmed(void *_data)
1143{
1144 struct sccp_connections *conn, *tmp1;
1145 struct timespec now;
1146 clock_gettime(CLOCK_MONOTONIC, &now);
1147
1148 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1149 if (conn->has_remote_ref)
1150 continue;
1151
1152 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1153 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1154 continue;
1155
1156 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1157 sccp_src_ref_to_int(&conn->real_ref),
1158 sccp_src_ref_to_int(&conn->patched_ref));
1159 sccp_connection_destroy(conn);
1160 }
1161
1162 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1163}
1164
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001165extern void *tall_msgb_ctx;
1166extern void *tall_ctr_ctx;
1167static void talloc_init_ctx()
1168{
1169 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1170 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1171 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1172}
1173
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001174extern enum node_type bsc_vty_go_parent(struct vty *vty);
1175
1176static struct vty_app_info vty_info = {
1177 .name = "BSC NAT",
1178 .version = PACKAGE_VERSION,
1179 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001180 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001181};
1182
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001183int main(int argc, char** argv)
1184{
Harald Welte2c869ef2010-08-25 19:43:54 +02001185 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001186
Harald Welte2c869ef2010-08-25 19:43:54 +02001187 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001188
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001189 log_init(&log_info);
1190 stderr_target = log_target_create_stderr();
1191 log_add_target(stderr_target);
1192 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001193
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001194 nat = bsc_nat_alloc();
1195 if (!nat) {
1196 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1197 return -4;
1198 }
1199
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001200 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001201 if (!nat->mgcp_cfg) {
1202 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1203 return -5;
1204 }
1205
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001206 vty_info.copyright = openbsc_copyright;
1207 vty_init(&vty_info);
1208 logging_vty_add_cmds();
1209 bsc_nat_vty_init(nat);
1210
1211
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001212 /* parse options */
1213 local_addr.s_addr = INADDR_ANY;
1214 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001215
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001216 rate_ctr_init(tall_bsc_ctx);
1217
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001218 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001219 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001220 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001221 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1222 return -3;
1223 }
1224
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001225 /* over rule the VTY config */
1226 if (msc_ip)
1227 bsc_nat_set_msc_ip(nat, msc_ip);
1228
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001229 /* seed the PRNG */
1230 srand(time(NULL));
1231
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001232 /*
1233 * Setup the MGCP code..
1234 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001235 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001236 return -4;
1237
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001238 /* connect to the MSC */
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +08001239 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001240 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001241 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001242 exit(1);
1243 }
1244
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001245 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001246 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001247 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1248 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001249 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001250 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001251
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001252 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +01001253 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001254 fprintf(stderr, "Failed to listen for BSC.\n");
1255 exit(1);
1256 }
1257
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001258 signal(SIGABRT, &signal_handler);
1259 signal(SIGUSR1, &signal_handler);
1260 signal(SIGPIPE, SIG_IGN);
1261
Harald Welte2c869ef2010-08-25 19:43:54 +02001262 if (daemonize) {
1263 rc = osmo_daemonize();
1264 if (rc < 0) {
1265 perror("Error during daemonize");
1266 exit(1);
1267 }
1268 }
1269
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001270 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001271 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001272 sccp_close.cb = sccp_close_unconfirmed;
1273 sccp_close.data = NULL;
1274 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1275
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001276 while (1) {
1277 bsc_select_main(0);
1278 }
1279
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001280 return 0;
1281}