blob: 841262c5abfd18815f47b49d2a625489d4f6e856 [file] [log] [blame]
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02004 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2013 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 Freyther973dbae2015-04-04 20:47:03 +020042#include <openbsc/bsc_msg_filter.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +020045#include <openbsc/socket.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080046#include <openbsc/vty.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080047
Harald Welteba874b82014-08-20 23:47:15 +020048#include <osmocom/ctrl/control_cmd.h>
49#include <osmocom/ctrl/control_if.h>
Daniel Willmann6fc4a982011-07-22 17:55:42 +020050
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020051#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080053
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020054#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020055#include <osmocom/gsm/gsm0808.h>
Harald Welted36ff762011-03-23 18:26:56 +010056#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080057
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080058#include <osmocom/vty/telnet_interface.h>
59#include <osmocom/vty/vty.h>
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +010060#include <osmocom/vty/logging.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020061#include <osmocom/vty/ports.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080062
Harald Welted5db12c2010-08-03 15:11:51 +020063#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080064
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020065#include <osmocom/abis/ipa.h>
66
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080067#include "../../bscconfig.h"
68
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080069#define SCCP_CLOSE_TIME 20
70#define SCCP_CLOSE_TIME_TIMEOUT 19
71
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080072static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080073static struct in_addr local_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020074static struct osmo_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080075static const char *msc_ip = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020076static struct osmo_timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020077static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010078
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080079const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080080 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010081 "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 +080082 "This is free software: you are free to change and redistribute it.\r\n"
83 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010084
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080085static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080086static 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 +020087static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080088static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080089
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080090struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
91{
92 struct bsc_config *conf;
93
94 llist_for_each_entry(conf, &nat->bsc_configs, entry)
95 if (conf->nr == num)
96 return conf;
97
98 return NULL;
99}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100100
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800101static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
102{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800103 if (!con) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200104 LOGP(DLINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800105 msgb_free(msg);
106 return;
107 }
108
109
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200110 if (osmo_wqueue_enqueue(&con->write_queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200111 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800112 msgb_free(msg);
113 }
114}
115
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100116static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100117{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800118 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100119 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
120 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
121 0x00, 0x01, 0x31,
122 };
123
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200124 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100125}
126
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800127static void send_ping(struct bsc_connection *bsc)
128{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800129 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800130 IPAC_MSGT_PING,
131 };
132
133 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
134}
135
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800136static void send_pong(struct bsc_connection *bsc)
137{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800138 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800139 IPAC_MSGT_PONG,
140 };
141
142 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
143}
144
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800145static void bsc_pong_timeout(void *_bsc)
146{
147 struct bsc_connection *bsc = _bsc;
148
149 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
150 bsc_close_connection(bsc);
151}
152
153static void bsc_ping_timeout(void *_bsc)
154{
155 struct bsc_connection *bsc = _bsc;
156
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800157 if (bsc->nat->ping_timeout < 0)
158 return;
159
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800160 send_ping(bsc);
161
162 /* send another ping in 20 seconds */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200163 osmo_timer_schedule(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800164
165 /* also start a pong timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200166 osmo_timer_schedule(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800167}
168
169static void start_ping_pong(struct bsc_connection *bsc)
170{
171 bsc->pong_timeout.data = bsc;
172 bsc->pong_timeout.cb = bsc_pong_timeout;
173 bsc->ping_timeout.data = bsc;
174 bsc->ping_timeout.cb = bsc_ping_timeout;
175
176 bsc_ping_timeout(bsc);
177}
178
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100179static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100180{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800181 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200182 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100183 };
184
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200185 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100186}
187
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100188static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100189{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800190 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200191 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100192 0x01, IPAC_IDTAG_UNIT,
193 0x01, IPAC_IDTAG_MACADDR,
194 0x01, IPAC_IDTAG_LOCATION1,
195 0x01, IPAC_IDTAG_LOCATION2,
196 0x01, IPAC_IDTAG_EQUIPVERS,
197 0x01, IPAC_IDTAG_SWVERSION,
198 0x01, IPAC_IDTAG_UNITNAME,
199 0x01, IPAC_IDTAG_SERNR,
200 };
201
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200202 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100203}
204
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200205static struct msgb *nat_create_rlsd(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200206{
207 struct sccp_connection_released *rel;
208 struct msgb *msg;
209
210 msg = msgb_alloc_headroom(4096, 128, "rlsd");
211 if (!msg) {
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200212 LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n");
213 return NULL;
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200214 }
215
216 msg->l2h = msgb_put(msg, sizeof(*rel));
217 rel = (struct sccp_connection_released *) msg->l2h;
218 rel->type = SCCP_MSG_TYPE_RLSD;
219 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
220 rel->destination_local_reference = conn->remote_ref;
221 rel->source_local_reference = conn->patched_ref;
222
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200223 return msg;
224}
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200225
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200226static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct nat_sccp_connection *conn)
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200227{
228 struct msgb *msg;
229
230 if (!nat->ussd_con)
231 return;
232
233 msg = nat_create_rlsd(conn);
234 if (!msg)
235 return;
236
237 bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP);
238}
239
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200240static void nat_send_rlsd_msc(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200241{
242 struct msgb *msg;
243
244 msg = nat_create_rlsd(conn);
245 if (!msg)
246 return;
247
Harald Welte4a88a492014-08-20 23:46:40 +0200248 ipa_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800249 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200250}
251
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200252static void nat_send_rlsd_bsc(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200253{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200254 struct msgb *msg;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200255 struct sccp_connection_released *rel;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200256
257 msg = msgb_alloc_headroom(4096, 128, "rlsd");
258 if (!msg) {
259 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
260 return;
261 }
262
263 msg->l2h = msgb_put(msg, sizeof(*rel));
264 rel = (struct sccp_connection_released *) msg->l2h;
265 rel->type = SCCP_MSG_TYPE_RLSD;
266 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
267 rel->destination_local_reference = conn->real_ref;
268 rel->source_local_reference = conn->remote_ref;
269
270 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
271}
272
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200273static struct msgb *nat_creat_clrc(struct nat_sccp_connection *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200274{
275 struct msgb *msg;
276 struct msgb *sccp;
277
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200278 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200279 if (!msg) {
280 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200281 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200282 }
283
284 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
285 if (!sccp) {
286 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
287 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200288 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200289 }
290
291 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200292 return sccp;
293}
294
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200295static int nat_send_clrc_bsc(struct nat_sccp_connection *conn)
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200296{
297 struct msgb *sccp;
298
299 sccp = nat_creat_clrc(conn, 0x20);
300 if (!sccp)
301 return -1;
302 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200303}
304
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800305static void nat_send_rlc(struct bsc_msc_connection *msc_con,
306 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200307 struct sccp_source_reference *dst)
308{
309 struct sccp_connection_release_complete *rlc;
310 struct msgb *msg;
311
312 msg = msgb_alloc_headroom(4096, 128, "rlc");
313 if (!msg) {
Holger Hans Peter Freytherff98b3c2013-09-03 11:25:16 +0200314 LOGP(DNAT, LOGL_ERROR, "Failed to sccp rlc.\n");
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200315 return;
316 }
317
318 msg->l2h = msgb_put(msg, sizeof(*rlc));
319 rlc = (struct sccp_connection_release_complete *) msg->l2h;
320 rlc->type = SCCP_MSG_TYPE_RLC;
321 rlc->destination_local_reference = *dst;
322 rlc->source_local_reference = *src;
323
Harald Welte4a88a492014-08-20 23:46:40 +0200324 ipa_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200325
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800326 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200327}
328
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200329static void send_mgcp_reset(struct bsc_connection *bsc)
330{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800331 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200332 "RSIP 1 13@mgw MGCP 1.0\r\n"
333 };
334
335 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
336}
337
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100338void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg)
339{
Harald Welte4a88a492014-08-20 23:46:40 +0200340 ipa_prepend_header(msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100341 queue_for_msc(nat->msc_con, msg);
342}
343
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100344/*
345 * Below is the handling of messages coming
346 * from the MSC and need to be forwarded to
347 * a real BSC.
348 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800349static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100350{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800351 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200352 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100353
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800354 msc_con->first_contact = 1;
355 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100356}
357
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800358static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800359{
Holger Hans Peter Freyther8a8df802015-06-10 11:51:16 +0200360 struct msgb *msg = bsc_msc_id_get_resp(0, nat->token);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800361 if (!msg)
362 return;
363
Harald Welte4a88a492014-08-20 23:46:40 +0200364 ipa_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800365 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800366}
367
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100368/*
369 * Currently we are lacking refcounting so we need to copy each message.
370 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800371static 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 +0100372{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100373 struct msgb *msg;
374
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200375 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200376 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100377 return;
378 }
379
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200380 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100381 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200382 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100383 return;
384 }
385
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200386 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100387 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200388
389 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100390}
391
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800392/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800393 * Update the release statistics
394 */
395static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
396{
397 if (!bsc->cfg) {
398 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
399 return;
400 }
401
402 if (filter >= 0) {
403 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
404 return;
405 }
406
407 if (filter == -1)
408 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
409 else if (normal)
410 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
411 else
412 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
413}
414
415/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800416 * Release an established connection. We will have to release it to the BSC
417 * and to the network and we do it the following way.
418 * 1.) Give up on the MSC side
419 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
420 * ignore the RLC... we might complain about it. Other options would
421 * be to send a Release Request, handle the Release Complete..
422 * 1.2) Mark the data structure to be con_local and wait for 2nd
423 *
424 * 2.) Give up on the BSC side
425 * 2.1) Depending on the con type reject the service, or just close it
426 */
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100427static void bsc_send_con_release(struct bsc_connection *bsc,
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200428 struct nat_sccp_connection *con,
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +0200429 struct bsc_filter_reject_cause *cause)
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800430{
431 struct msgb *rlsd;
432 /* 1. release the network */
433 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
434 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
435 if (!rlsd)
436 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
437 else {
Harald Welte4a88a492014-08-20 23:46:40 +0200438 ipa_prepend_header(rlsd, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800439 queue_for_msc(con->msc_con, rlsd);
440 }
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200441 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800442 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800443
444 /* 2. release the BSC side */
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200445 if (con->filter_state.con_type == FLT_CON_TYPE_LU) {
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800446 struct msgb *payload, *udt;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100447 payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause);
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800448
449 if (payload) {
450 gsm0808_prepend_dtap_header(payload, 0);
451 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
452 if (udt)
453 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
454 else
455 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
456
457 msgb_free(payload);
458 } else {
459 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
460 }
461 }
462
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200463 nat_send_clrc_bsc(con);
464
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800465 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
466 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
467 if (!rlsd) {
468 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
469 sccp_connection_destroy(con);
470 return;
471 }
472
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200473 con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800474 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
475}
476
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800477static void bsc_send_con_refuse(struct bsc_connection *bsc,
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100478 struct bsc_nat_parsed *parsed, int con_type,
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +0200479 struct bsc_filter_reject_cause *cause)
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800480{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800481 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800482 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800483
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200484 if (con_type == FLT_CON_TYPE_LU)
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100485 payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause);
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200486 else if (con_type == FLT_CON_TYPE_CM_SERV_REQ || con_type == FLT_CON_TYPE_SSA)
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100487 payload = gsm48_create_mm_serv_rej(cause->cm_reject_cause);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800488 else {
489 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
490 payload = NULL;
491 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800492
493 /*
494 * Some BSCs do not handle the payload inside a SCCP CREF msg
495 * so we will need to:
496 * 1.) Allocate a local connection and mark it as local..
497 * 2.) queue data for downstream.. and the RLC should delete everything
498 */
499 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200500 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200501 struct nat_sccp_connection *con;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800502 con = create_sccp_src_ref(bsc, parsed);
503 if (!con)
504 goto send_refuse;
505
506 /* declare it local and assign a unique remote_ref */
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200507 con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200508 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800509 con->has_remote_ref = 1;
510 con->remote_ref = con->patched_ref;
511
512 /* 1. create a confirmation */
513 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
514 if (!cc)
515 goto send_refuse;
516
517 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800518 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800519 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
520 if (!udt) {
521 msgb_free(cc);
522 goto send_refuse;
523 }
524
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200525 /* 3. send a Clear Command */
526 clear = nat_creat_clrc(con, 0x20);
527 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800528 msgb_free(cc);
529 msgb_free(udt);
530 goto send_refuse;
531 }
532
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200533 /* 4. send a RLSD */
534 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
535 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
536 if (!rlsd) {
537 msgb_free(cc);
538 msgb_free(udt);
539 msgb_free(clear);
540 goto send_refuse;
541 }
542
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800543 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
544 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200545 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800546 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
547 msgb_free(payload);
548 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800549 }
550
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800551
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800552send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800553 if (payload)
554 msgb_free(payload);
555
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800556 refuse = sccp_create_refuse(parsed->src_local_ref,
557 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800558 if (!refuse) {
559 LOGP(DNAT, LOGL_ERROR,
560 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
561 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
562 return;
563 }
564
565 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
566}
567
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200568static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg)
569{
570 if (bsc->cfg->forbid_paging) {
571 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
572 return;
573 }
574
575 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP);
576}
577
578static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg)
579{
580 struct bsc_connection *bsc;
581 const uint8_t *paging_start;
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200582 int paging_length, i, ret;
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200583
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200584 ret = bsc_nat_find_paging(msg, &paging_start, &paging_length);
585 if (ret != 0) {
586 LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200587 return;
588 }
589
590 /* This is quite expensive now */
591 for (i = 0; i < paging_length; i += 2) {
592 unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]);
593 unsigned int paged = 0;
594 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
595 if (!bsc->cfg)
596 continue;
597 if (!bsc->authenticated)
598 continue;
599 if (!bsc_config_handles_lac(bsc->cfg, _lac))
600 continue;
601 bsc_nat_send_paging(bsc, msg);
602 paged += 1;
603 }
604
605 /* highlight a possible config issue */
606 if (paged == 0)
607 LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac);
608
609 }
610}
611
612
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200613/*
614 * Update the auth status. This can be either a CIPHER MODE COMAMND or
615 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
616 */
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200617static void update_con_authorize(struct nat_sccp_connection *con,
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200618 struct bsc_nat_parsed *parsed,
619 struct msgb *msg)
620{
621 if (!con)
622 return;
623 if (con->authorized)
624 return;
625
626 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
627 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
628 con->authorized = 1;
629 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200630 uint8_t msg_type, proto;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200631 uint32_t len;
632 struct gsm48_hdr *hdr48;
633 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
634 if (!hdr48)
635 return;
636
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200637 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200638 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200639 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200640 msg_type == GSM48_MT_MM_CM_SERV_ACC)
641 con->authorized = 1;
642 }
643}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800644
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800645static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100646{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200647 struct nat_sccp_connection *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800648 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800649 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200650 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100651
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100652 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800653 parsed = bsc_nat_parse(msg);
654 if (!parsed) {
655 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100656 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800657 }
658
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100659 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800660 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800661
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200662 proto = parsed->ipa_proto;
663
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100664 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200665 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100666 switch (parsed->sccp_type) {
667 case SCCP_MSG_TYPE_UDT:
668 /* forward UDT messages to every BSC */
669 goto send_to_all;
670 break;
671 case SCCP_MSG_TYPE_RLSD:
672 case SCCP_MSG_TYPE_CREF:
673 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200674 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800675 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800676 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200677 osmo_counter_inc(nat->stats.sccp.calls);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200678
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800679 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800680 struct rate_ctr_group *ctrg;
681 ctrg = con->bsc->cfg->stats.ctrg;
682 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800683 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800684 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
685 } else
686 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200687 } else if (con && con->con_local == NAT_CON_END_USSD &&
688 parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) {
689 LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n");
690 con = NULL;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800691 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200692 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100693 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800694 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
695 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200696 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800697 break;
698 case SCCP_MSG_TYPE_RLC:
699 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
700 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100701 break;
702 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100703 /* MSC never opens a SCCP connection, fall through */
704 default:
705 goto exit;
706 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200707
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200708 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
709 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
710 /* Exchange src/dest for the reply */
Holger Hans Peter Freyther1b9902c2013-09-03 14:35:33 +0200711 nat_send_rlc(msc_con, &parsed->original_dest_ref,
712 parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200713 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800714 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 +0100715 }
716
Pablo Neira Ayuso0a244b42013-05-12 20:50:28 +0200717 if (!con) {
718 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100719 return -1;
Pablo Neira Ayuso0a244b42013-05-12 20:50:28 +0200720 }
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800721 if (!con->bsc->authenticated) {
Pablo Neira Ayuso0a244b42013-05-12 20:50:28 +0200722 talloc_free(parsed);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800723 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100724 return -1;
725 }
726
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200727 update_con_authorize(con, parsed, msg);
Pablo Neira Ayuso0a244b42013-05-12 20:50:28 +0200728 talloc_free(parsed);
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200729
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200730 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100731 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100732
733send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800734 /*
735 * Filter Paging from the network. We do not want to send a PAGING
736 * Command to every BSC in our network. We will analys the PAGING
737 * message and then send it to the authenticated messages...
738 */
739 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200740 bsc_nat_handle_paging(nat, msg);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800741 goto exit;
742 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100743 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800744 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100745 if (!bsc->authenticated)
746 continue;
747
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200748 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100749 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800750
751exit:
752 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100753 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100754}
755
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800756static void msc_connection_was_lost(struct bsc_msc_connection *con)
757{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200758 struct bsc_connection *bsc, *tmp;
759
760 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
761 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800762 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200763
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200764 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200765 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800766}
767
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800768static void msc_connection_connected(struct bsc_msc_connection *con)
769{
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200770 osmo_counter_inc(nat->stats.msc.reconn);
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800771}
772
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200773static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200774{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800775 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200776 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200777 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
778 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
779 0x01, 0x20
780 };
781
782 struct msgb *msg;
783
784 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
785 if (!msg) {
786 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
787 return;
788 }
789
790 msg->l2h = msgb_put(msg, sizeof(reset));
791 memcpy(msg->l2h, reset, msgb_l2len(msg));
792
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800793 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200794
795 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
796}
797
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200798static int ipaccess_msc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100799{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800800 struct bsc_msc_connection *msc_con;
Jacob Erlbecke8278122014-03-31 13:42:11 +0200801 struct msgb *msg = NULL;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100802 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200803 int ret;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100804
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800805 msc_con = (struct bsc_msc_connection *) bfd->data;
806
Jacob Erlbecke8278122014-03-31 13:42:11 +0200807 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &msc_con->pending_msg);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200808 if (ret <= 0) {
Jacob Erlbecke8278122014-03-31 13:42:11 +0200809 if (ret == -EAGAIN)
810 return 0;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200811 if (ret == 0)
Holger Hans Peter Freytherc1a86872013-09-02 13:07:45 +0200812 LOGP(DNAT, LOGL_FATAL,
813 "The connection the MSC(%s) was lost, exiting\n",
814 msc_con->name);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800815 else
Holger Hans Peter Freytherc1a86872013-09-02 13:07:45 +0200816 LOGP(DNAT, LOGL_ERROR,
817 "Failed to parse ip access message on %s: %d\n",
818 msc_con->name, ret);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100819
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800820 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100821 return -1;
822 }
823
Holger Hans Peter Freytherc1a86872013-09-02 13:07:45 +0200824 LOGP(DNAT, LOGL_DEBUG,
825 "MSG from MSC(%s): %s proto: %d\n", msc_con->name,
826 osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100827
828 /* handle base message handling */
829 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100830
831 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800832 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte4a88a492014-08-20 23:46:40 +0200833 ipa_ccm_rcvmsg_base(msg, bfd);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800834 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800835 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800836 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800837 send_id_get_response(msc_con);
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100838 } else if (hh->proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800839 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100840 } else if (hh->proto == IPAC_PROTO_MGCP_OLD) {
841 bsc_nat_handle_mgcp(nat, msg);
842 }
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100843
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800844 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100845 return 0;
846}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800847
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200848static int ipaccess_msc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800849{
850 int rc;
851 rc = write(bfd->fd, msg->data, msg->len);
852
853 if (rc != msg->len) {
854 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
855 return -1;
856 }
857
858 return rc;
859}
860
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100861/*
862 * Below is the handling of messages coming
863 * from the BSC and need to be forwarded to
864 * a real BSC.
865 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100866
867/*
868 * Remove the connection from the connections list,
869 * remove it from the patching of SCCP header lists
870 * as well. Maybe in the future even close connection..
871 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800872void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100873{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200874 struct nat_sccp_connection *sccp_patch, *tmp;
Daniel Willmanna86bc392011-02-18 14:32:56 +0100875 struct bsc_cmd_list *cmd_entry, *cmd_tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800876 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100877
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800878 /* stop the timeout timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200879 osmo_timer_del(&connection->id_timeout);
880 osmo_timer_del(&connection->ping_timeout);
881 osmo_timer_del(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800882
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800883 if (connection->cfg)
884 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
885
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100886 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800887 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100888 if (sccp_patch->bsc != connection)
889 continue;
890
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800891 if (ctr)
892 rate_ctr_inc(ctr);
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200893 if (sccp_patch->has_remote_ref) {
894 if (sccp_patch->con_local == NAT_CON_END_MSC)
895 nat_send_rlsd_msc(sccp_patch);
896 else if (sccp_patch->con_local == NAT_CON_END_USSD)
897 nat_send_rlsd_ussd(nat, sccp_patch);
898 }
899
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200900 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100901 }
902
Daniel Willmanna86bc392011-02-18 14:32:56 +0100903 /* Reply to all outstanding commands */
904 llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) {
905 cmd_entry->cmd->type = CTRL_TYPE_ERROR;
906 cmd_entry->cmd->reply = "BSC closed the connection";
907 ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd);
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +0100908 bsc_nat_ctrl_del_pending(cmd_entry);
Daniel Willmanna86bc392011-02-18 14:32:56 +0100909 }
910
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200911 /* close endpoints allocated by this BSC */
912 bsc_mgcp_clear_endpoints_for(connection);
913
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200914 osmo_fd_unregister(&connection->write_queue.bfd);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800915 close(connection->write_queue.bfd.fd);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200916 osmo_wqueue_clear(&connection->write_queue);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800917 llist_del(&connection->list_entry);
918
Jacob Erlbecke8278122014-03-31 13:42:11 +0200919 if (connection->pending_msg) {
920 LOGP(DNAT, LOGL_ERROR, "Dropping partial message on connection %d.\n",
Jacob Erlbeck4ace4242014-04-03 12:10:24 +0200921 connection->cfg ? connection->cfg->nr : -1);
Jacob Erlbecke8278122014-03-31 13:42:11 +0200922 msgb_free(connection->pending_msg);
923 connection->pending_msg = NULL;
924 }
925
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100926 talloc_free(connection);
927}
928
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200929static void bsc_maybe_close(struct bsc_connection *bsc)
930{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200931 struct nat_sccp_connection *sccp;
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200932 if (!bsc->nat->blocked)
933 return;
934
935 /* are there any connections left */
936 llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry)
937 if (sccp->bsc == bsc)
938 return;
939
940 /* nothing left, close the BSC */
941 LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n",
942 bsc->cfg ? bsc->cfg->nr : -1);
943 bsc_close_connection(bsc);
944}
945
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800946static void ipaccess_close_bsc(void *data)
947{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200948 struct sockaddr_in sock;
949 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800950 struct bsc_connection *conn = data;
951
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200952
953 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
954 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
955 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800956 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800957}
958
959static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
960{
961 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800962 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freyther8a8df802015-06-10 11:51:16 +0200963 int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800964
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800965 if (bsc->cfg) {
966 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
967 bsc->write_queue.bfd.fd, bsc->cfg->nr);
968 return;
969 }
970
Holger Hans Peter Freytherd34adb22013-03-19 20:50:36 +0100971 if (len <= 0) {
972 LOGP(DNAT, LOGL_ERROR, "Token with length zero on fd: %d\n",
973 bsc->write_queue.bfd.fd);
974 return;
975 }
976
977 if (token[len - 1] != '\0') {
978 LOGP(DNAT, LOGL_ERROR, "Token not null terminated on fd: %d\n",
979 bsc->write_queue.bfd.fd);
980 return;
981 }
982
Holger Hans Peter Freyther8a8df802015-06-10 11:51:16 +0200983 /*
984 * New systems have fixed the structure of the message but
985 * we need to support old ones too.
986 */
987 if (len >= 2 && token[len - 2] == '\0')
988 len -= 1;
989
Holger Hans Peter Freytheraa0cecd2015-06-08 09:54:45 +0200990 conf = bsc_config_by_token(bsc->nat, token, len);
991 if (!conf) {
992 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther8a8df802015-06-10 11:51:16 +0200993 "No bsc found for token '%s' len %d on fd: %d.\n", token,
994 bsc->write_queue.bfd.fd, len);
Holger Hans Peter Freyther0363d1b2015-06-08 11:55:02 +0200995 bsc_close_connection(bsc);
Holger Hans Peter Freytheraa0cecd2015-06-08 09:54:45 +0200996 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800997 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200998
Holger Hans Peter Freytheraa0cecd2015-06-08 09:54:45 +0200999 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
1000 bsc->authenticated = 1;
1001 bsc->cfg = conf;
1002 osmo_timer_del(&bsc->id_timeout);
1003 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
1004 conf->nr, bsc->write_queue.bfd.fd);
1005 start_ping_pong(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001006}
1007
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +02001008static void handle_con_stats(struct nat_sccp_connection *con)
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +08001009{
1010 struct rate_ctr_group *ctrg;
1011 int id = bsc_conn_type_to_ctr(con);
1012
1013 if (id == -1)
1014 return;
1015
1016 if (!con->bsc || !con->bsc->cfg)
1017 return;
1018
1019 ctrg = con->bsc->cfg->stats.ctrg;
1020 rate_ctr_inc(&ctrg->ctr[id]);
1021}
1022
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001023static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001024{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001025 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001026 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001027 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001028 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +08001029 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001030 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001031 struct bsc_filter_reject_cause cause;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001032
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001033 /* Parse and filter messages */
1034 parsed = bsc_nat_parse(msg);
1035 if (!parsed) {
1036 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +02001037 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001038 return -1;
1039 }
1040
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +01001041 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001042 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08001043
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +02001044 /*
1045 * check authentication after filtering to not reject auth
1046 * responses coming from the BSC. We have to make sure that
1047 * nothing from the exit path will forward things to the MSC
1048 */
1049 if (!bsc->authenticated) {
1050 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
1051 msgb_free(msg);
1052 return -1;
1053 }
1054
1055
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001056 /* modify the SCCP entries */
1057 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +08001058 int filter;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +02001059 struct nat_sccp_connection *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001060 switch (parsed->sccp_type) {
1061 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +01001062 memset(&cause, 0, sizeof(cause));
1063 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed,
1064 &con_type, &imsi, &cause);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001065 if (filter < 0) {
Holger Hans Peter Freyther7c009832015-04-05 13:45:53 +02001066 if (imsi)
1067 bsc_nat_inform_reject(bsc, imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001068 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001069 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001070 }
1071
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +08001072 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001073 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001074 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +08001075 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001076 con_msc = con->msc_con;
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +02001077 con->filter_state.con_type = con_type;
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +02001078 con->filter_state.imsi_checked = filter;
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001079 bsc_nat_extract_lac(bsc, con, parsed, msg);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001080 if (imsi)
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +02001081 con->filter_state.imsi = talloc_steal(con, imsi);
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001082 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001083 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +08001084 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001085 break;
1086 case SCCP_MSG_TYPE_RLSD:
1087 case SCCP_MSG_TYPE_CREF:
1088 case SCCP_MSG_TYPE_DT1:
1089 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +02001090 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001091 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001092 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001093 /* only filter non local connections */
1094 if (!con->con_local) {
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +01001095 memset(&cause, 0, sizeof(cause));
1096 filter = bsc_nat_filter_dt(bsc, msg,
1097 con, parsed, &cause);
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001098 if (filter < 0) {
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +02001099 if (con->filter_state.imsi)
1100 bsc_nat_inform_reject(bsc,
1101 con->filter_state.imsi);
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001102 bsc_stat_reject(filter, bsc, 1);
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +01001103 bsc_send_con_release(bsc, con, &cause);
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001104 con = NULL;
1105 goto exit2;
1106 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001107
1108 /* hand data to a side channel */
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +02001109 if (bsc_ussd_check(con, parsed, msg) == 1)
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001110 con->con_local = NAT_CON_END_USSD;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001111
1112 /*
1113 * Optionally rewrite setup message. This can
1114 * replace the msg and the parsed structure becomes
1115 * invalid.
1116 */
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +02001117 msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed,
1118 con->filter_state.imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001119 talloc_free(parsed);
1120 parsed = NULL;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +02001121 } else if (con->con_local == NAT_CON_END_USSD) {
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +02001122 bsc_ussd_check(con, parsed, msg);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +08001123 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001124
1125 con_bsc = con->bsc;
1126 con_msc = con->msc_con;
1127 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001128 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001129
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001130 break;
1131 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001132 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001133 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001134 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001135 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001136 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001137 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +02001138 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001139 bsc_maybe_close(bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001140 break;
1141 case SCCP_MSG_TYPE_UDT:
1142 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001143 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001144 break;
1145 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +08001146 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 +08001147 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001148 goto exit2;
1149 break;
1150 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +01001151 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001152 bsc_mgcp_forward(bsc, msg);
1153 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001154 } else {
1155 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
1156 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001157 }
1158
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001159 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +08001160 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 +08001161 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001162 goto exit2;
1163 }
1164
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +08001165 /* do not forward messages to the MSC */
1166 if (con_filter)
1167 goto exit2;
1168
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001169 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001170 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1171 bsc->cfg->nr,
1172 parsed ? parsed->ipa_proto : -1,
1173 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001174 goto exit2;
1175 }
1176
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001177 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001178 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001179 if (parsed)
1180 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001181 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001182
1183exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001184 /* if we filter out the reset send an ack to the BSC */
1185 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001186 send_reset_ack(bsc);
1187 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001188 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1189 /* do we know who is handling this? */
Holger Hans Peter Freyther2dd18bd2015-06-08 18:31:02 +02001190 if (msg->l2h[0] == IPAC_MSGT_ID_RESP && msgb_l2len(msg) > 2) {
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001191 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001192 int ret;
Harald Welte4a88a492014-08-20 23:46:40 +02001193 ret = ipa_ccm_idtag_parse(&tvp,
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001194 (unsigned char *) msg->l2h + 2,
1195 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001196 if (ret < 0) {
1197 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
1198 "message with malformed TLVs\n");
1199 return ret;
1200 }
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001201 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1202 ipaccess_auth_bsc(&tvp, bsc);
1203 }
1204
1205 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001206 }
1207
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001208exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001209 if (imsi)
1210 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001211 talloc_free(parsed);
1212 msgb_free(msg);
1213 return -1;
1214
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001215exit3:
1216 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001217 if (imsi)
1218 talloc_free(imsi);
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +01001219 bsc_send_con_refuse(bsc, parsed, con_type, &cause);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001220 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001221 msgb_free(msg);
1222 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001223}
1224
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001225static int ipaccess_bsc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001226{
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001227 struct bsc_connection *bsc = bfd->data;
Jacob Erlbecke8278122014-03-31 13:42:11 +02001228 struct msgb *msg = NULL;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001229 struct ipaccess_head *hh;
Daniel Willmanna86bc392011-02-18 14:32:56 +01001230 struct ipaccess_head_ext *hh_ext;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001231 int ret;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001232
Jacob Erlbecke8278122014-03-31 13:42:11 +02001233 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &bsc->pending_msg);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001234 if (ret <= 0) {
Jacob Erlbecke8278122014-03-31 13:42:11 +02001235 if (ret == -EAGAIN)
1236 return 0;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001237 if (ret == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001238 LOGP(DNAT, LOGL_ERROR,
1239 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1240 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001241 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001242 LOGP(DNAT, LOGL_ERROR,
1243 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001244 bsc->cfg ? bsc->cfg->nr : -1, ret);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001245
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001246 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001247 return -1;
1248 }
1249
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001250
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001251 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 +01001252
1253 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001254 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001255
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001256 /* stop the pong timeout */
1257 if (hh->proto == IPAC_PROTO_IPACCESS) {
1258 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001259 osmo_timer_del(&bsc->pong_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001260 msgb_free(msg);
1261 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001262 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1263 send_pong(bsc);
1264 msgb_free(msg);
1265 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001266 }
Daniel Willmanna86bc392011-02-18 14:32:56 +01001267 /* Message contains the ipaccess_head_ext header, investigate further */
1268 } else if (hh->proto == IPAC_PROTO_OSMO &&
1269 msg->len > sizeof(*hh) + sizeof(*hh_ext)) {
1270
1271 hh_ext = (struct ipaccess_head_ext *) hh->data;
1272 /* l2h is where the actual command data is expected */
1273 msg->l2h = hh_ext->data;
1274
Harald Weltef8e49dd2011-07-13 14:43:37 +02001275 if (hh_ext->proto == IPAC_PROTO_EXT_CTRL)
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +01001276 return bsc_nat_handle_ctrlif_msg(bsc, msg);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001277 }
1278
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001279 /* FIXME: Currently no PONG is sent to the BSC */
1280 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001281 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001282
1283 return 0;
1284}
1285
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001286static int ipaccess_listen_bsc_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001287{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001288 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001289 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001290 struct sockaddr_in sa;
1291 socklen_t sa_len = sizeof(sa);
1292
1293 if (!(what & BSC_FD_READ))
1294 return 0;
1295
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001296 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1297 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001298 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001299 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001300 }
1301
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001302 /* count the reconnect */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001303 osmo_counter_inc(nat->stats.bsc.reconn);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001304
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001305 /*
1306 * if we are not connected to a msc... just close the socket
1307 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001308 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001309 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001310 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001311 return 0;
1312 }
1313
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001314 if (nat->blocked) {
1315 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n");
1316 close(fd);
1317 return 0;
1318 }
1319
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001320 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001321 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001322 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001323 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1324
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001325 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001326 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001327 if (rc != 0)
1328 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1329
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001330 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001331 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001332
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001333 /*
1334 *
1335 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001336 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001337 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001338 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001339 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001340 return -1;
1341 }
1342
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001343 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001344 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001345 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001346 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001347 bsc->write_queue.bfd.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001348 if (osmo_fd_register(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001349 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001350 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001351 talloc_free(bsc);
1352 return -2;
1353 }
1354
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001355 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001356 fd, inet_ntoa(sa.sin_addr));
Daniel Willmanna86bc392011-02-18 14:32:56 +01001357
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001358 llist_add(&bsc->list_entry, &nat->bsc_connections);
Daniel Willmanna86bc392011-02-18 14:32:56 +01001359 bsc->last_id = 0;
1360
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001361 send_id_ack(bsc);
1362 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001363 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001364
1365 /*
1366 * start the hangup timer
1367 */
1368 bsc->id_timeout.data = bsc;
1369 bsc->id_timeout.cb = ipaccess_close_bsc;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001370 osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001371 return 0;
1372}
1373
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001374static void print_usage()
1375{
1376 printf("Usage: bsc_nat\n");
1377}
1378
1379static void print_help()
1380{
1381 printf(" Some useful help...\n");
1382 printf(" -h --help this text\n");
1383 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001384 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001385 printf(" -s --disable-color\n");
1386 printf(" -c --config-file filename The config file to use.\n");
1387 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001388 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001389}
1390
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001391static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001392{
1393 while (1) {
1394 int option_index = 0, c;
1395 static struct option long_options[] = {
1396 {"help", 0, 0, 'h'},
1397 {"debug", 1, 0, 'd'},
Holger Hans Peter Freyther27b61842013-01-15 14:37:51 +01001398 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001399 {"config-file", 1, 0, 'c'},
1400 {"disable-color", 0, 0, 's'},
1401 {"timestamp", 0, 0, 'T'},
1402 {"msc", 1, 0, 'm'},
1403 {"local", 1, 0, 'l'},
1404 {0, 0, 0, 0}
1405 };
1406
Holger Hans Peter Freyther27b61842013-01-15 14:37:51 +01001407 c = getopt_long(argc, argv, "hd:sTPc:m:l:D",
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001408 long_options, &option_index);
1409 if (c == -1)
1410 break;
1411
1412 switch (c) {
1413 case 'h':
1414 print_usage();
1415 print_help();
1416 exit(0);
1417 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001418 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001419 break;
1420 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001421 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001422 break;
Holger Hans Peter Freyther27b61842013-01-15 14:37:51 +01001423 case 'D':
1424 daemonize = 1;
1425 break;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001426 case 'c':
Holger Hans Peter Freyther60e073e2015-02-08 09:25:38 +01001427 config_file = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001428 break;
1429 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001430 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001431 break;
1432 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001433 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001434 break;
1435 case 'l':
1436 inet_aton(optarg, &local_addr);
1437 break;
1438 default:
1439 /* ignore */
1440 break;
1441 }
1442 }
1443}
1444
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001445static void signal_handler(int signal)
1446{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001447 switch (signal) {
1448 case SIGABRT:
1449 /* in case of abort, we want to obtain a talloc report
1450 * and then return to the caller, who will abort the process */
1451 case SIGUSR1:
1452 talloc_report_full(tall_bsc_ctx, stderr);
1453 break;
1454 default:
1455 break;
1456 }
1457}
1458
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001459static void sccp_close_unconfirmed(void *_data)
1460{
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001461 int destroyed = 0;
1462 struct bsc_connection *bsc, *bsc_tmp;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +02001463 struct nat_sccp_connection *conn, *tmp1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001464 struct timespec now;
1465 clock_gettime(CLOCK_MONOTONIC, &now);
1466
1467 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1468 if (conn->has_remote_ref)
1469 continue;
1470
1471 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1472 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1473 continue;
1474
Holger Hans Peter Freytherc8b2c2f2014-04-03 12:28:14 +02001475 LOGP(DNAT, LOGL_ERROR,
1476 "SCCP connection 0x%x/0x%x was never confirmed on bsc nr. %d\n",
1477 sccp_src_ref_to_int(&conn->real_ref),
1478 sccp_src_ref_to_int(&conn->patched_ref),
1479 conn->bsc->cfg->nr);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001480 sccp_connection_destroy(conn);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001481 destroyed = 1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001482 }
1483
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001484 if (!destroyed)
1485 goto out;
1486
1487 /* now close out any BSC */
1488 llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry)
1489 bsc_maybe_close(bsc);
1490
1491out:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001492 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001493}
1494
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001495extern void *tall_msgb_ctx;
1496extern void *tall_ctr_ctx;
1497static void talloc_init_ctx()
1498{
1499 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1500 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1501 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1502}
1503
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001504extern enum node_type bsc_vty_go_parent(struct vty *vty);
1505
1506static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001507 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001508 .version = PACKAGE_VERSION,
1509 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001510 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001511};
1512
Holger Hans Peter Freyther85d3b342013-06-14 19:10:28 +02001513
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001514int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001515{
Harald Welte2c869ef2010-08-25 19:43:54 +02001516 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001517
Harald Welte2c869ef2010-08-25 19:43:54 +02001518 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001519
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001520 osmo_init_logging(&log_info);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001521
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001522 nat = bsc_nat_alloc();
1523 if (!nat) {
1524 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1525 return -4;
1526 }
1527
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001528 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001529 if (!nat->mgcp_cfg) {
1530 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1531 return -5;
1532 }
1533
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -04001534 /* We need to add mode-set for amr codecs */
1535 nat->sdp_ensure_amr_mode_set = 1;
1536
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001537 vty_info.copyright = openbsc_copyright;
1538 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001539 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001540 bsc_nat_vty_init(nat);
1541
1542
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001543 /* parse options */
1544 local_addr.s_addr = INADDR_ANY;
1545 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001546
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001547 rate_ctr_init(tall_bsc_ctx);
1548
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001549 /* init vty and parse */
Harald Welte55dc31e2014-08-24 17:54:49 +02001550 telnet_init(tall_bsc_ctx, NULL, OSMO_VTY_PORT_BSC_NAT);
Pablo Neira Ayuso326b5d82013-08-02 21:14:14 +02001551 if (mgcp_parse_config(config_file, nat->mgcp_cfg, MGCP_BSC_NAT) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001552 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1553 return -3;
1554 }
1555
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001556 /* over rule the VTY config */
1557 if (msc_ip)
1558 bsc_nat_set_msc_ip(nat, msc_ip);
1559
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001560 /* seed the PRNG */
1561 srand(time(NULL));
1562
Holger Hans Peter Freyther85d3b342013-06-14 19:10:28 +02001563
1564
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001565 /*
1566 * Setup the MGCP code..
1567 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001568 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001569 return -4;
1570
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001571 /* connect to the MSC */
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +02001572 nat->msc_con = bsc_msc_create(nat, &nat->dests);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001573 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001574 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001575 exit(1);
1576 }
1577
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +01001578 nat->ctrl = bsc_nat_controlif_setup(nat, 4250);
Holger Hans Peter Freyther036b25f2013-01-15 13:48:18 +01001579 if (!nat->ctrl) {
1580 fprintf(stderr, "Creating the control interface failed.\n");
1581 exit(1);
1582 }
Daniel Willmanna86bc392011-02-18 14:32:56 +01001583
Holger Hans Peter Freytherc1a86872013-09-02 13:07:45 +02001584 nat->msc_con->name = "main MSC";
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001585 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001586 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001587 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1588 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001589 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001590 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001591
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001592 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001593 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001594 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001595 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001596 fprintf(stderr, "Failed to listen for BSC.\n");
1597 exit(1);
1598 }
1599
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001600 rc = bsc_ussd_init(nat);
1601 if (rc != 0) {
1602 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1603 exit(1);
1604 }
1605
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001606 signal(SIGABRT, &signal_handler);
1607 signal(SIGUSR1, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001608 osmo_init_ignore_signals();
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001609
Harald Welte2c869ef2010-08-25 19:43:54 +02001610 if (daemonize) {
1611 rc = osmo_daemonize();
1612 if (rc < 0) {
1613 perror("Error during daemonize");
1614 exit(1);
1615 }
1616 }
1617
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001618 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001619 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001620 sccp_close.cb = sccp_close_unconfirmed;
1621 sccp_close.data = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001622 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001623
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001624 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001625 osmo_select_main(0);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001626 }
1627
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001628 return 0;
1629}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001630
1631/* Close all connections handed out to the USSD module */
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +02001632int bsc_ussd_close_connections(struct bsc_nat *nat)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001633{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +02001634 struct nat_sccp_connection *con;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001635 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001636 if (con->con_local != NAT_CON_END_USSD)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001637 continue;
1638 if (!con->bsc)
1639 continue;
1640
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001641 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001642 nat_send_rlsd_bsc(con);
1643 }
1644
1645 return 0;
1646}