blob: 466dc72dc391bc277dfdaddece22917ec41ceabc [file] [log] [blame]
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +02004 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2011 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
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080021 *
22 */
23#include <sys/socket.h>
24#include <netinet/in.h>
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +080025#include <netinet/tcp.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080026#include <arpa/inet.h>
27
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freyther5aa25ae2010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080033#include <unistd.h>
34
35#define _GNU_SOURCE
36#include <getopt.h>
37
38#include <openbsc/debug.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010039#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080041#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +020044#include <openbsc/socket.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
Daniel Willmann6fc4a982011-07-22 17:55:42 +020047#include <openbsc/control_cmd.h>
48#include <openbsc/control_if.h>
49
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020050#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010051#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080052
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020053#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020054#include <osmocom/gsm/gsm0808.h>
Harald Welted36ff762011-03-23 18:26:56 +010055#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080056
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080057#include <osmocom/vty/telnet_interface.h>
58#include <osmocom/vty/vty.h>
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +010059#include <osmocom/vty/logging.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080060
Harald Welted5db12c2010-08-03 15:11:51 +020061#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080062
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020063#include <osmocom/abis/ipa.h>
64
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080065#include "../../bscconfig.h"
66
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080067#define SCCP_CLOSE_TIME 20
68#define SCCP_CLOSE_TIME_TIMEOUT 19
69
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080070static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080071static struct in_addr local_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020072static struct osmo_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080073static const char *msc_ip = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020074static struct osmo_timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020075static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010076
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080077const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080078 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010079 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080080 "This is free software: you are free to change and redistribute it.\r\n"
81 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010082
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080083static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080084static 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 +020085static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080086static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080087
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080088struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
89{
90 struct bsc_config *conf;
91
92 llist_for_each_entry(conf, &nat->bsc_configs, entry)
93 if (conf->nr == num)
94 return conf;
95
96 return NULL;
97}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010098
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080099static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
100{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800101 if (!con) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200102 LOGP(DLINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800103 msgb_free(msg);
104 return;
105 }
106
107
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200108 if (osmo_wqueue_enqueue(&con->write_queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200109 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800110 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 */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200161 osmo_timer_schedule(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800162
163 /* also start a pong timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200164 osmo_timer_schedule(&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 Freyther91b9a452011-04-16 17:01:47 +0200203static struct msgb *nat_create_rlsd(struct sccp_connections *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200204{
205 struct sccp_connection_released *rel;
206 struct msgb *msg;
207
208 msg = msgb_alloc_headroom(4096, 128, "rlsd");
209 if (!msg) {
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200210 LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n");
211 return NULL;
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200212 }
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
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200221 return msg;
222}
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200223
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200224static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct sccp_connections *conn)
225{
226 struct msgb *msg;
227
228 if (!nat->ussd_con)
229 return;
230
231 msg = nat_create_rlsd(conn);
232 if (!msg)
233 return;
234
235 bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP);
236}
237
238static void nat_send_rlsd_msc(struct sccp_connections *conn)
239{
240 struct msgb *msg;
241
242 msg = nat_create_rlsd(conn);
243 if (!msg)
244 return;
245
246 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800247 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200248}
249
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200250static void nat_send_rlsd_bsc(struct sccp_connections *conn)
251{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200252 struct msgb *msg;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200253 struct sccp_connection_released *rel;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200254
255 msg = msgb_alloc_headroom(4096, 128, "rlsd");
256 if (!msg) {
257 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
258 return;
259 }
260
261 msg->l2h = msgb_put(msg, sizeof(*rel));
262 rel = (struct sccp_connection_released *) msg->l2h;
263 rel->type = SCCP_MSG_TYPE_RLSD;
264 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
265 rel->destination_local_reference = conn->real_ref;
266 rel->source_local_reference = conn->remote_ref;
267
268 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
269}
270
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200271static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200272{
273 struct msgb *msg;
274 struct msgb *sccp;
275
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200276 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200277 if (!msg) {
278 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200279 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200280 }
281
282 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
283 if (!sccp) {
284 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
285 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200286 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200287 }
288
289 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200290 return sccp;
291}
292
293static int nat_send_clrc_bsc(struct sccp_connections *conn)
294{
295 struct msgb *sccp;
296
297 sccp = nat_creat_clrc(conn, 0x20);
298 if (!sccp)
299 return -1;
300 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200301}
302
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800303static void nat_send_rlc(struct bsc_msc_connection *msc_con,
304 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200305 struct sccp_source_reference *dst)
306{
307 struct sccp_connection_release_complete *rlc;
308 struct msgb *msg;
309
310 msg = msgb_alloc_headroom(4096, 128, "rlc");
311 if (!msg) {
312 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
313 return;
314 }
315
316 msg->l2h = msgb_put(msg, sizeof(*rlc));
317 rlc = (struct sccp_connection_release_complete *) msg->l2h;
318 rlc->type = SCCP_MSG_TYPE_RLC;
319 rlc->destination_local_reference = *dst;
320 rlc->source_local_reference = *src;
321
322 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
323
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800324 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200325}
326
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200327static void send_mgcp_reset(struct bsc_connection *bsc)
328{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800329 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200330 "RSIP 1 13@mgw MGCP 1.0\r\n"
331 };
332
333 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
334}
335
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100336/*
337 * Below is the handling of messages coming
338 * from the MSC and need to be forwarded to
339 * a real BSC.
340 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800341static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100342{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800343 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200344 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100345
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800346 msc_con->first_contact = 1;
347 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100348}
349
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800350static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800351{
352 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
353 if (!msg)
354 return;
355
356 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800357 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800358}
359
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100360/*
361 * Currently we are lacking refcounting so we need to copy each message.
362 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800363static 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 +0100364{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100365 struct msgb *msg;
366
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200367 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200368 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100369 return;
370 }
371
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200372 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100373 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200374 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100375 return;
376 }
377
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200378 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100379 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200380
381 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100382}
383
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800384/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800385 * Update the release statistics
386 */
387static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
388{
389 if (!bsc->cfg) {
390 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
391 return;
392 }
393
394 if (filter >= 0) {
395 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
396 return;
397 }
398
399 if (filter == -1)
400 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
401 else if (normal)
402 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
403 else
404 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
405}
406
407/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800408 * Release an established connection. We will have to release it to the BSC
409 * and to the network and we do it the following way.
410 * 1.) Give up on the MSC side
411 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
412 * ignore the RLC... we might complain about it. Other options would
413 * be to send a Release Request, handle the Release Complete..
414 * 1.2) Mark the data structure to be con_local and wait for 2nd
415 *
416 * 2.) Give up on the BSC side
417 * 2.1) Depending on the con type reject the service, or just close it
418 */
419static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
420{
421 struct msgb *rlsd;
422 /* 1. release the network */
423 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
424 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
425 if (!rlsd)
426 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
427 else {
428 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
429 queue_for_msc(con->msc_con, rlsd);
430 }
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200431 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800432 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800433
434 /* 2. release the BSC side */
435 if (con->con_type == NAT_CON_TYPE_LU) {
436 struct msgb *payload, *udt;
437 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
438
439 if (payload) {
440 gsm0808_prepend_dtap_header(payload, 0);
441 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
442 if (udt)
443 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
444 else
445 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
446
447 msgb_free(payload);
448 } else {
449 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
450 }
451 }
452
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200453 nat_send_clrc_bsc(con);
454
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800455 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
456 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
457 if (!rlsd) {
458 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
459 sccp_connection_destroy(con);
460 return;
461 }
462
463 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
464 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
465}
466
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800467static void bsc_send_con_refuse(struct bsc_connection *bsc,
468 struct bsc_nat_parsed *parsed, int con_type)
469{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800470 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800471 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800472
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800473 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800474 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800475 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800476 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800477 else {
478 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
479 payload = NULL;
480 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800481
482 /*
483 * Some BSCs do not handle the payload inside a SCCP CREF msg
484 * so we will need to:
485 * 1.) Allocate a local connection and mark it as local..
486 * 2.) queue data for downstream.. and the RLC should delete everything
487 */
488 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200489 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800490 struct sccp_connections *con;
491 con = create_sccp_src_ref(bsc, parsed);
492 if (!con)
493 goto send_refuse;
494
495 /* declare it local and assign a unique remote_ref */
496 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200497 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800498 con->has_remote_ref = 1;
499 con->remote_ref = con->patched_ref;
500
501 /* 1. create a confirmation */
502 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
503 if (!cc)
504 goto send_refuse;
505
506 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800507 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800508 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
509 if (!udt) {
510 msgb_free(cc);
511 goto send_refuse;
512 }
513
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200514 /* 3. send a Clear Command */
515 clear = nat_creat_clrc(con, 0x20);
516 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800517 msgb_free(cc);
518 msgb_free(udt);
519 goto send_refuse;
520 }
521
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200522 /* 4. send a RLSD */
523 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
524 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
525 if (!rlsd) {
526 msgb_free(cc);
527 msgb_free(udt);
528 msgb_free(clear);
529 goto send_refuse;
530 }
531
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800532 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
533 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200534 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800535 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
536 msgb_free(payload);
537 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800538 }
539
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800540
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800541send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800542 if (payload)
543 msgb_free(payload);
544
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800545 refuse = sccp_create_refuse(parsed->src_local_ref,
546 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800547 if (!refuse) {
548 LOGP(DNAT, LOGL_ERROR,
549 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
550 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
551 return;
552 }
553
554 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
555}
556
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200557static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg)
558{
559 if (bsc->cfg->forbid_paging) {
560 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
561 return;
562 }
563
564 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP);
565}
566
567static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg)
568{
569 struct bsc_connection *bsc;
570 const uint8_t *paging_start;
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200571 int paging_length, i, ret;
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200572
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200573 ret = bsc_nat_find_paging(msg, &paging_start, &paging_length);
574 if (ret != 0) {
575 LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200576 return;
577 }
578
579 /* This is quite expensive now */
580 for (i = 0; i < paging_length; i += 2) {
581 unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]);
582 unsigned int paged = 0;
583 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
584 if (!bsc->cfg)
585 continue;
586 if (!bsc->authenticated)
587 continue;
588 if (!bsc_config_handles_lac(bsc->cfg, _lac))
589 continue;
590 bsc_nat_send_paging(bsc, msg);
591 paged += 1;
592 }
593
594 /* highlight a possible config issue */
595 if (paged == 0)
596 LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac);
597
598 }
599}
600
601
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200602/*
603 * Update the auth status. This can be either a CIPHER MODE COMAMND or
604 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
605 */
606static void update_con_authorize(struct sccp_connections *con,
607 struct bsc_nat_parsed *parsed,
608 struct msgb *msg)
609{
610 if (!con)
611 return;
612 if (con->authorized)
613 return;
614
615 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
616 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
617 con->authorized = 1;
618 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200619 uint8_t msg_type, proto;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200620 uint32_t len;
621 struct gsm48_hdr *hdr48;
622 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
623 if (!hdr48)
624 return;
625
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200626 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200627 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200628 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200629 msg_type == GSM48_MT_MM_CM_SERV_ACC)
630 con->authorized = 1;
631 }
632}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800633
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800634static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100635{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800636 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800637 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800638 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200639 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100640
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100641 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800642 parsed = bsc_nat_parse(msg);
643 if (!parsed) {
644 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100645 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800646 }
647
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100648 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800649 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800650
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200651 proto = parsed->ipa_proto;
652
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100653 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200654 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100655 switch (parsed->sccp_type) {
656 case SCCP_MSG_TYPE_UDT:
657 /* forward UDT messages to every BSC */
658 goto send_to_all;
659 break;
660 case SCCP_MSG_TYPE_RLSD:
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200661 if (con && con->con_local == NAT_CON_END_USSD) {
662 LOGP(DNAT, LOGL_NOTICE, "RLSD for a USSD connection. Ignoring.\n");
663 con = NULL;
664 }
665 /* fall through */
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100666 case SCCP_MSG_TYPE_CREF:
667 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200668 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800669 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800670 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200671 osmo_counter_inc(nat->stats.sccp.calls);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200672
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800673 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800674 struct rate_ctr_group *ctrg;
675 ctrg = con->bsc->cfg->stats.ctrg;
676 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800677 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800678 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
679 } else
680 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200681 } else if (con && con->con_local == NAT_CON_END_USSD &&
682 parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) {
683 LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n");
684 con = NULL;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800685 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200686 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100687 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800688 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
689 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200690 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800691 break;
692 case SCCP_MSG_TYPE_RLC:
693 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
694 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100695 break;
696 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100697 /* MSC never opens a SCCP connection, fall through */
698 default:
699 goto exit;
700 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200701
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200702 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
703 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
704 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800705 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200706 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800707 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 +0100708 }
709
710 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800711 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100712 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800713 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800714 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100715 return -1;
716 }
717
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200718 update_con_authorize(con, parsed, msg);
719
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200720 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100721 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100722
723send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800724 /*
725 * Filter Paging from the network. We do not want to send a PAGING
726 * Command to every BSC in our network. We will analys the PAGING
727 * message and then send it to the authenticated messages...
728 */
729 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200730 bsc_nat_handle_paging(nat, msg);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800731 goto exit;
732 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100733 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800734 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100735 if (!bsc->authenticated)
736 continue;
737
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200738 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100739 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800740
741exit:
742 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100743 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100744}
745
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800746static void msc_connection_was_lost(struct bsc_msc_connection *con)
747{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200748 struct bsc_connection *bsc, *tmp;
749
750 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
751 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800752 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200753
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200754 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200755 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800756}
757
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800758static void msc_connection_connected(struct bsc_msc_connection *con)
759{
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200760 osmo_counter_inc(nat->stats.msc.reconn);
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800761}
762
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200763static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200764{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800765 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200766 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200767 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
768 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
769 0x01, 0x20
770 };
771
772 struct msgb *msg;
773
774 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
775 if (!msg) {
776 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
777 return;
778 }
779
780 msg->l2h = msgb_put(msg, sizeof(reset));
781 memcpy(msg->l2h, reset, msgb_l2len(msg));
782
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800783 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200784
785 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
786}
787
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200788static int ipaccess_msc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100789{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800790 struct bsc_msc_connection *msc_con;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200791 struct msgb *msg;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100792 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200793 int ret;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100794
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800795 msc_con = (struct bsc_msc_connection *) bfd->data;
796
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200797 ret = ipa_msg_recv(bfd->fd, &msg);
798 if (ret <= 0) {
799 if (ret == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100800 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800801 else
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200802 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100803
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800804 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100805 return -1;
806 }
807
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200808 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100809
810 /* handle base message handling */
811 hh = (struct ipaccess_head *) msg->data;
812 ipaccess_rcvmsg_base(msg, bfd);
813
814 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800815 if (hh->proto == IPAC_PROTO_IPACCESS) {
816 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800817 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800818 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800819 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800820 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800821 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100822
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800823 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100824 return 0;
825}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800826
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200827static int ipaccess_msc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800828{
829 int rc;
830 rc = write(bfd->fd, msg->data, msg->len);
831
832 if (rc != msg->len) {
833 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
834 return -1;
835 }
836
837 return rc;
838}
839
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100840/*
841 * Below is the handling of messages coming
842 * from the BSC and need to be forwarded to
843 * a real BSC.
844 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100845
846/*
847 * Remove the connection from the connections list,
848 * remove it from the patching of SCCP header lists
849 * as well. Maybe in the future even close connection..
850 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800851void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100852{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100853 struct sccp_connections *sccp_patch, *tmp;
Daniel Willmanna86bc392011-02-18 14:32:56 +0100854 struct bsc_cmd_list *cmd_entry, *cmd_tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800855 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100856
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800857 /* stop the timeout timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200858 osmo_timer_del(&connection->id_timeout);
859 osmo_timer_del(&connection->ping_timeout);
860 osmo_timer_del(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800861
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800862 if (connection->cfg)
863 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
864
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100865 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800866 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100867 if (sccp_patch->bsc != connection)
868 continue;
869
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800870 if (ctr)
871 rate_ctr_inc(ctr);
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200872 if (sccp_patch->has_remote_ref) {
873 if (sccp_patch->con_local == NAT_CON_END_MSC)
874 nat_send_rlsd_msc(sccp_patch);
875 else if (sccp_patch->con_local == NAT_CON_END_USSD)
876 nat_send_rlsd_ussd(nat, sccp_patch);
877 }
878
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200879 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100880 }
881
Daniel Willmanna86bc392011-02-18 14:32:56 +0100882 /* Reply to all outstanding commands */
883 llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) {
884 cmd_entry->cmd->type = CTRL_TYPE_ERROR;
885 cmd_entry->cmd->reply = "BSC closed the connection";
886 ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd);
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +0100887 bsc_nat_ctrl_del_pending(cmd_entry);
Daniel Willmanna86bc392011-02-18 14:32:56 +0100888 }
889
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200890 /* close endpoints allocated by this BSC */
891 bsc_mgcp_clear_endpoints_for(connection);
892
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200893 osmo_fd_unregister(&connection->write_queue.bfd);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800894 close(connection->write_queue.bfd.fd);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200895 osmo_wqueue_clear(&connection->write_queue);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800896 llist_del(&connection->list_entry);
897
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100898 talloc_free(connection);
899}
900
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200901static void bsc_maybe_close(struct bsc_connection *bsc)
902{
903 struct sccp_connections *sccp;
904 if (!bsc->nat->blocked)
905 return;
906
907 /* are there any connections left */
908 llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry)
909 if (sccp->bsc == bsc)
910 return;
911
912 /* nothing left, close the BSC */
913 LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n",
914 bsc->cfg ? bsc->cfg->nr : -1);
915 bsc_close_connection(bsc);
916}
917
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800918static void ipaccess_close_bsc(void *data)
919{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200920 struct sockaddr_in sock;
921 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800922 struct bsc_connection *conn = data;
923
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200924
925 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
926 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
927 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800928 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800929}
930
931static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
932{
933 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800934 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200935 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800936
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800937 if (bsc->cfg) {
938 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
939 bsc->write_queue.bfd.fd, bsc->cfg->nr);
940 return;
941 }
942
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800943 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200944 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800945 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800946 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200947 bsc->cfg = conf;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200948 osmo_timer_del(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800949 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
950 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800951 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200952 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800953 }
954 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200955
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800956 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
957 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800958}
959
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800960static void handle_con_stats(struct sccp_connections *con)
961{
962 struct rate_ctr_group *ctrg;
963 int id = bsc_conn_type_to_ctr(con);
964
965 if (id == -1)
966 return;
967
968 if (!con->bsc || !con->bsc->cfg)
969 return;
970
971 ctrg = con->bsc->cfg->stats.ctrg;
972 rate_ctr_inc(&ctrg->ctr[id]);
973}
974
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100975static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100976{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800977 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800978 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800979 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800980 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800981 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800982 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100983
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800984 /* Parse and filter messages */
985 parsed = bsc_nat_parse(msg);
986 if (!parsed) {
987 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200988 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800989 return -1;
990 }
991
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100992 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800993 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800994
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200995 /*
996 * check authentication after filtering to not reject auth
997 * responses coming from the BSC. We have to make sure that
998 * nothing from the exit path will forward things to the MSC
999 */
1000 if (!bsc->authenticated) {
1001 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
1002 msgb_free(msg);
1003 return -1;
1004 }
1005
1006
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001007 /* modify the SCCP entries */
1008 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +08001009 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001010 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001011 switch (parsed->sccp_type) {
1012 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001013 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001014 if (filter < 0) {
1015 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001016 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001017 }
1018
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +08001019 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001020 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001021 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +08001022 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001023 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +08001024 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +08001025 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001026 if (imsi)
1027 con->imsi = talloc_steal(con, imsi);
1028 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001029 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +08001030 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001031 break;
1032 case SCCP_MSG_TYPE_RLSD:
1033 case SCCP_MSG_TYPE_CREF:
1034 case SCCP_MSG_TYPE_DT1:
1035 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +02001036 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001037 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001038 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001039 /* only filter non local connections */
1040 if (!con->con_local) {
1041 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
1042 if (filter < 0) {
1043 bsc_stat_reject(filter, bsc, 1);
1044 bsc_send_con_release(bsc, con);
1045 con = NULL;
1046 goto exit2;
1047 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001048
1049 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +02001050 if (bsc_check_ussd(con, parsed, msg) == 1)
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001051 con->con_local = NAT_CON_END_USSD;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001052
1053 /*
1054 * Optionally rewrite setup message. This can
1055 * replace the msg and the parsed structure becomes
1056 * invalid.
1057 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001058 msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001059 talloc_free(parsed);
1060 parsed = NULL;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +02001061 } else if (con->con_local == NAT_CON_END_USSD) {
1062 bsc_check_ussd(con, parsed, msg);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +08001063 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001064
1065 con_bsc = con->bsc;
1066 con_msc = con->msc_con;
1067 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001068 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001069
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001070 break;
1071 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001072 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001073 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001074 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001075 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001076 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001077 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +02001078 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001079 bsc_maybe_close(bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001080 break;
1081 case SCCP_MSG_TYPE_UDT:
1082 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001083 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001084 break;
1085 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +08001086 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 +08001087 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001088 goto exit2;
1089 break;
1090 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +01001091 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001092 bsc_mgcp_forward(bsc, msg);
1093 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001094 } else {
1095 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
1096 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001097 }
1098
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001099 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +08001100 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 +08001101 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001102 goto exit2;
1103 }
1104
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +08001105 /* do not forward messages to the MSC */
1106 if (con_filter)
1107 goto exit2;
1108
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001109 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001110 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1111 bsc->cfg->nr,
1112 parsed ? parsed->ipa_proto : -1,
1113 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001114 goto exit2;
1115 }
1116
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001117 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001118 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001119 if (parsed)
1120 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001121 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001122
1123exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001124 /* if we filter out the reset send an ack to the BSC */
1125 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001126 send_reset_ack(bsc);
1127 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001128 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1129 /* do we know who is handling this? */
1130 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1131 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001132 int ret;
1133 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001134 (unsigned char *) msg->l2h + 2,
1135 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001136 if (ret < 0) {
1137 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
1138 "message with malformed TLVs\n");
1139 return ret;
1140 }
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001141 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1142 ipaccess_auth_bsc(&tvp, bsc);
1143 }
1144
1145 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001146 }
1147
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001148exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001149 if (imsi)
1150 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001151 talloc_free(parsed);
1152 msgb_free(msg);
1153 return -1;
1154
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001155exit3:
1156 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001157 if (imsi)
1158 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001159 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001160 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001161 msgb_free(msg);
1162 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001163}
1164
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001165static int ipaccess_bsc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001166{
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001167 struct bsc_connection *bsc = bfd->data;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001168 struct msgb *msg;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001169 struct ipaccess_head *hh;
Daniel Willmanna86bc392011-02-18 14:32:56 +01001170 struct ipaccess_head_ext *hh_ext;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001171 int ret;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001172
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001173 ret = ipa_msg_recv(bfd->fd, &msg);
1174 if (ret <= 0) {
1175 if (ret == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001176 LOGP(DNAT, LOGL_ERROR,
1177 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1178 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001179 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001180 LOGP(DNAT, LOGL_ERROR,
1181 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001182 bsc->cfg ? bsc->cfg->nr : -1, ret);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001183
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001184 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001185 return -1;
1186 }
1187
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001188
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001189 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001190
1191 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001192 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001193
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001194 /* stop the pong timeout */
1195 if (hh->proto == IPAC_PROTO_IPACCESS) {
1196 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001197 osmo_timer_del(&bsc->pong_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001198 msgb_free(msg);
1199 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001200 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1201 send_pong(bsc);
1202 msgb_free(msg);
1203 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001204 }
Daniel Willmanna86bc392011-02-18 14:32:56 +01001205 /* Message contains the ipaccess_head_ext header, investigate further */
1206 } else if (hh->proto == IPAC_PROTO_OSMO &&
1207 msg->len > sizeof(*hh) + sizeof(*hh_ext)) {
1208
1209 hh_ext = (struct ipaccess_head_ext *) hh->data;
1210 /* l2h is where the actual command data is expected */
1211 msg->l2h = hh_ext->data;
1212
Harald Weltef8e49dd2011-07-13 14:43:37 +02001213 if (hh_ext->proto == IPAC_PROTO_EXT_CTRL)
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +01001214 return bsc_nat_handle_ctrlif_msg(bsc, msg);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001215 }
1216
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001217 /* FIXME: Currently no PONG is sent to the BSC */
1218 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001219 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001220
1221 return 0;
1222}
1223
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001224static int ipaccess_listen_bsc_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001225{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001226 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001227 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001228 struct sockaddr_in sa;
1229 socklen_t sa_len = sizeof(sa);
1230
1231 if (!(what & BSC_FD_READ))
1232 return 0;
1233
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001234 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1235 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001236 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001237 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001238 }
1239
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001240 /* count the reconnect */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001241 osmo_counter_inc(nat->stats.bsc.reconn);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001242
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001243 /*
1244 * if we are not connected to a msc... just close the socket
1245 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001246 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001247 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001248 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001249 return 0;
1250 }
1251
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001252 if (nat->blocked) {
1253 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n");
1254 close(fd);
1255 return 0;
1256 }
1257
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001258 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001259 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001260 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001261 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1262
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001263 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001264 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001265 if (rc != 0)
1266 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1267
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001268 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001269 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001270
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001271 /*
1272 *
1273 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001274 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001275 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001276 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001277 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001278 return -1;
1279 }
1280
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001281 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001282 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001283 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001284 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001285 bsc->write_queue.bfd.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001286 if (osmo_fd_register(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001287 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001288 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001289 talloc_free(bsc);
1290 return -2;
1291 }
1292
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001293 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001294 fd, inet_ntoa(sa.sin_addr));
Daniel Willmanna86bc392011-02-18 14:32:56 +01001295
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001296 llist_add(&bsc->list_entry, &nat->bsc_connections);
Daniel Willmanna86bc392011-02-18 14:32:56 +01001297 bsc->last_id = 0;
1298
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001299 send_id_ack(bsc);
1300 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001301 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001302
1303 /*
1304 * start the hangup timer
1305 */
1306 bsc->id_timeout.data = bsc;
1307 bsc->id_timeout.cb = ipaccess_close_bsc;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001308 osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001309 return 0;
1310}
1311
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001312static void print_usage()
1313{
1314 printf("Usage: bsc_nat\n");
1315}
1316
1317static void print_help()
1318{
1319 printf(" Some useful help...\n");
1320 printf(" -h --help this text\n");
1321 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001322 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001323 printf(" -s --disable-color\n");
1324 printf(" -c --config-file filename The config file to use.\n");
1325 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001326 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001327}
1328
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001329static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001330{
1331 while (1) {
1332 int option_index = 0, c;
1333 static struct option long_options[] = {
1334 {"help", 0, 0, 'h'},
1335 {"debug", 1, 0, 'd'},
1336 {"config-file", 1, 0, 'c'},
1337 {"disable-color", 0, 0, 's'},
1338 {"timestamp", 0, 0, 'T'},
1339 {"msc", 1, 0, 'm'},
1340 {"local", 1, 0, 'l'},
1341 {0, 0, 0, 0}
1342 };
1343
1344 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1345 long_options, &option_index);
1346 if (c == -1)
1347 break;
1348
1349 switch (c) {
1350 case 'h':
1351 print_usage();
1352 print_help();
1353 exit(0);
1354 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001355 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001356 break;
1357 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001358 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001359 break;
1360 case 'c':
1361 config_file = strdup(optarg);
1362 break;
1363 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001364 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001365 break;
1366 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001367 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001368 break;
1369 case 'l':
1370 inet_aton(optarg, &local_addr);
1371 break;
1372 default:
1373 /* ignore */
1374 break;
1375 }
1376 }
1377}
1378
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001379static void signal_handler(int signal)
1380{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001381 switch (signal) {
1382 case SIGABRT:
1383 /* in case of abort, we want to obtain a talloc report
1384 * and then return to the caller, who will abort the process */
1385 case SIGUSR1:
1386 talloc_report_full(tall_bsc_ctx, stderr);
1387 break;
1388 default:
1389 break;
1390 }
1391}
1392
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001393static void sccp_close_unconfirmed(void *_data)
1394{
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001395 int destroyed = 0;
1396 struct bsc_connection *bsc, *bsc_tmp;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001397 struct sccp_connections *conn, *tmp1;
1398 struct timespec now;
1399 clock_gettime(CLOCK_MONOTONIC, &now);
1400
1401 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1402 if (conn->has_remote_ref)
1403 continue;
1404
1405 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1406 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1407 continue;
1408
1409 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1410 sccp_src_ref_to_int(&conn->real_ref),
1411 sccp_src_ref_to_int(&conn->patched_ref));
1412 sccp_connection_destroy(conn);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001413 destroyed = 1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001414 }
1415
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001416 if (!destroyed)
1417 goto out;
1418
1419 /* now close out any BSC */
1420 llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry)
1421 bsc_maybe_close(bsc);
1422
1423out:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001424 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001425}
1426
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001427extern void *tall_msgb_ctx;
1428extern void *tall_ctr_ctx;
1429static void talloc_init_ctx()
1430{
1431 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1432 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1433 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1434}
1435
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001436extern enum node_type bsc_vty_go_parent(struct vty *vty);
1437
1438static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001439 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001440 .version = PACKAGE_VERSION,
1441 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001442 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001443};
1444
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001445int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001446{
Harald Welte2c869ef2010-08-25 19:43:54 +02001447 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001448
Harald Welte2c869ef2010-08-25 19:43:54 +02001449 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001450
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001451 osmo_init_logging(&log_info);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001452
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001453 nat = bsc_nat_alloc();
1454 if (!nat) {
1455 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1456 return -4;
1457 }
1458
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001459 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001460 if (!nat->mgcp_cfg) {
1461 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1462 return -5;
1463 }
1464
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001465 vty_info.copyright = openbsc_copyright;
1466 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001467 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001468 bsc_nat_vty_init(nat);
1469
1470
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001471 /* parse options */
1472 local_addr.s_addr = INADDR_ANY;
1473 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001474
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001475 rate_ctr_init(tall_bsc_ctx);
1476
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001477 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001478 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001479 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001480 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1481 return -3;
1482 }
1483
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001484 /* over rule the VTY config */
1485 if (msc_ip)
1486 bsc_nat_set_msc_ip(nat, msc_ip);
1487
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001488 /* seed the PRNG */
1489 srand(time(NULL));
1490
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001491 /*
1492 * Setup the MGCP code..
1493 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001494 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001495 return -4;
1496
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001497 /* connect to the MSC */
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +02001498 nat->msc_con = bsc_msc_create(nat, &nat->dests);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001499 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001500 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001501 exit(1);
1502 }
1503
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +01001504 nat->ctrl = bsc_nat_controlif_setup(nat, 4250);
Daniel Willmanna86bc392011-02-18 14:32:56 +01001505
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001506 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001507 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001508 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1509 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001510 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001511 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001512
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001513 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001514 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001515 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001516 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001517 fprintf(stderr, "Failed to listen for BSC.\n");
1518 exit(1);
1519 }
1520
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001521 rc = bsc_ussd_init(nat);
1522 if (rc != 0) {
1523 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1524 exit(1);
1525 }
1526
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001527 signal(SIGABRT, &signal_handler);
1528 signal(SIGUSR1, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001529 osmo_init_ignore_signals();
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001530
Harald Welte2c869ef2010-08-25 19:43:54 +02001531 if (daemonize) {
1532 rc = osmo_daemonize();
1533 if (rc < 0) {
1534 perror("Error during daemonize");
1535 exit(1);
1536 }
1537 }
1538
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001539 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001540 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001541 sccp_close.cb = sccp_close_unconfirmed;
1542 sccp_close.data = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001543 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001544
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001545 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001546 osmo_select_main(0);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001547 }
1548
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001549 return 0;
1550}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001551
1552/* Close all connections handed out to the USSD module */
1553int bsc_close_ussd_connections(struct bsc_nat *nat)
1554{
1555 struct sccp_connections *con;
1556 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001557 if (con->con_local != NAT_CON_END_USSD)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001558 continue;
1559 if (!con->bsc)
1560 continue;
1561
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001562 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001563 nat_send_rlsd_bsc(con);
1564 }
1565
1566 return 0;
1567}