blob: c42061b98b2d3b447704edf0f1ab297c86149312 [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>
Daniel Willmanna86bc392011-02-18 14:32:56 +010042#include <openbsc/control_cmd.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
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020048#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010049#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080050
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020052#include <osmocom/gsm/gsm0808.h>
Harald Welted36ff762011-03-23 18:26:56 +010053#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080054
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080055#include <osmocom/vty/telnet_interface.h>
56#include <osmocom/vty/vty.h>
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +010057#include <osmocom/vty/logging.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080058
Harald Welted5db12c2010-08-03 15:11:51 +020059#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080060
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020061#include <osmocom/abis/ipa.h>
62
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080063#include "../../bscconfig.h"
64
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080065#define SCCP_CLOSE_TIME 20
66#define SCCP_CLOSE_TIME_TIMEOUT 19
67
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080068static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080069static struct in_addr local_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020070static struct osmo_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080071static const char *msc_ip = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020072static struct osmo_timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020073static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010074
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080075const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080076 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010077 "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 +080078 "This is free software: you are free to change and redistribute it.\r\n"
79 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010080
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080081static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080082static 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 +020083static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080084static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Daniel Willmanna86bc392011-02-18 14:32:56 +010085static void bsc_del_pending(struct bsc_cmd_list *pending);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080086
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080087struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
88{
89 struct bsc_config *conf;
90
91 llist_for_each_entry(conf, &nat->bsc_configs, entry)
92 if (conf->nr == num)
93 return conf;
94
95 return NULL;
96}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010097
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080098static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
99{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800100 if (!con) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200101 LOGP(DLINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800102 msgb_free(msg);
103 return;
104 }
105
106
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200107 if (osmo_wqueue_enqueue(&con->write_queue, msg) != 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200108 LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n");
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800109 msgb_free(msg);
110 }
111}
112
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100113static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100114{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800115 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100116 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
117 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
118 0x00, 0x01, 0x31,
119 };
120
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200121 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100122}
123
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800124static void send_ping(struct bsc_connection *bsc)
125{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800126 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800127 IPAC_MSGT_PING,
128 };
129
130 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
131}
132
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800133static void send_pong(struct bsc_connection *bsc)
134{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800135 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800136 IPAC_MSGT_PONG,
137 };
138
139 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
140}
141
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800142static void bsc_pong_timeout(void *_bsc)
143{
144 struct bsc_connection *bsc = _bsc;
145
146 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
147 bsc_close_connection(bsc);
148}
149
150static void bsc_ping_timeout(void *_bsc)
151{
152 struct bsc_connection *bsc = _bsc;
153
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800154 if (bsc->nat->ping_timeout < 0)
155 return;
156
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800157 send_ping(bsc);
158
159 /* send another ping in 20 seconds */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200160 osmo_timer_schedule(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800161
162 /* also start a pong timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200163 osmo_timer_schedule(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800164}
165
166static void start_ping_pong(struct bsc_connection *bsc)
167{
168 bsc->pong_timeout.data = bsc;
169 bsc->pong_timeout.cb = bsc_pong_timeout;
170 bsc->ping_timeout.data = bsc;
171 bsc->ping_timeout.cb = bsc_ping_timeout;
172
173 bsc_ping_timeout(bsc);
174}
175
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100176static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100177{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800178 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200179 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100180 };
181
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200182 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100183}
184
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100185static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100186{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800187 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200188 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100189 0x01, IPAC_IDTAG_UNIT,
190 0x01, IPAC_IDTAG_MACADDR,
191 0x01, IPAC_IDTAG_LOCATION1,
192 0x01, IPAC_IDTAG_LOCATION2,
193 0x01, IPAC_IDTAG_EQUIPVERS,
194 0x01, IPAC_IDTAG_SWVERSION,
195 0x01, IPAC_IDTAG_UNITNAME,
196 0x01, IPAC_IDTAG_SERNR,
197 };
198
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200199 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100200}
201
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200202static struct msgb *nat_create_rlsd(struct sccp_connections *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200203{
204 struct sccp_connection_released *rel;
205 struct msgb *msg;
206
207 msg = msgb_alloc_headroom(4096, 128, "rlsd");
208 if (!msg) {
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200209 LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n");
210 return NULL;
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200211 }
212
213 msg->l2h = msgb_put(msg, sizeof(*rel));
214 rel = (struct sccp_connection_released *) msg->l2h;
215 rel->type = SCCP_MSG_TYPE_RLSD;
216 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
217 rel->destination_local_reference = conn->remote_ref;
218 rel->source_local_reference = conn->patched_ref;
219
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200220 return msg;
221}
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200222
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200223static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct sccp_connections *conn)
224{
225 struct msgb *msg;
226
227 if (!nat->ussd_con)
228 return;
229
230 msg = nat_create_rlsd(conn);
231 if (!msg)
232 return;
233
234 bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP);
235}
236
237static void nat_send_rlsd_msc(struct sccp_connections *conn)
238{
239 struct msgb *msg;
240
241 msg = nat_create_rlsd(conn);
242 if (!msg)
243 return;
244
245 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800246 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200247}
248
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200249static void nat_send_rlsd_bsc(struct sccp_connections *conn)
250{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200251 struct msgb *msg;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200252 struct sccp_connection_released *rel;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200253
254 msg = msgb_alloc_headroom(4096, 128, "rlsd");
255 if (!msg) {
256 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
257 return;
258 }
259
260 msg->l2h = msgb_put(msg, sizeof(*rel));
261 rel = (struct sccp_connection_released *) msg->l2h;
262 rel->type = SCCP_MSG_TYPE_RLSD;
263 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
264 rel->destination_local_reference = conn->real_ref;
265 rel->source_local_reference = conn->remote_ref;
266
267 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
268}
269
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200270static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200271{
272 struct msgb *msg;
273 struct msgb *sccp;
274
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200275 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200276 if (!msg) {
277 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200278 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200279 }
280
281 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
282 if (!sccp) {
283 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
284 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200285 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200286 }
287
288 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200289 return sccp;
290}
291
292static int nat_send_clrc_bsc(struct sccp_connections *conn)
293{
294 struct msgb *sccp;
295
296 sccp = nat_creat_clrc(conn, 0x20);
297 if (!sccp)
298 return -1;
299 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200300}
301
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800302static void nat_send_rlc(struct bsc_msc_connection *msc_con,
303 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200304 struct sccp_source_reference *dst)
305{
306 struct sccp_connection_release_complete *rlc;
307 struct msgb *msg;
308
309 msg = msgb_alloc_headroom(4096, 128, "rlc");
310 if (!msg) {
311 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
312 return;
313 }
314
315 msg->l2h = msgb_put(msg, sizeof(*rlc));
316 rlc = (struct sccp_connection_release_complete *) msg->l2h;
317 rlc->type = SCCP_MSG_TYPE_RLC;
318 rlc->destination_local_reference = *dst;
319 rlc->source_local_reference = *src;
320
321 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
322
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800323 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200324}
325
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200326static void send_mgcp_reset(struct bsc_connection *bsc)
327{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800328 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200329 "RSIP 1 13@mgw MGCP 1.0\r\n"
330 };
331
332 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
333}
334
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100335/*
336 * Below is the handling of messages coming
337 * from the MSC and need to be forwarded to
338 * a real BSC.
339 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800340static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100341{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800342 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200343 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100344
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800345 msc_con->first_contact = 1;
346 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100347}
348
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800349static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800350{
351 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
352 if (!msg)
353 return;
354
355 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800356 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800357}
358
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100359/*
360 * Currently we are lacking refcounting so we need to copy each message.
361 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800362static 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 +0100363{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100364 struct msgb *msg;
365
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200366 if (length > 4096 - 128) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200367 LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100368 return;
369 }
370
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200371 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100372 if (!msg) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200373 LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100374 return;
375 }
376
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200377 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100378 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200379
380 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100381}
382
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800383/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800384 * Update the release statistics
385 */
386static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
387{
388 if (!bsc->cfg) {
389 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
390 return;
391 }
392
393 if (filter >= 0) {
394 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
395 return;
396 }
397
398 if (filter == -1)
399 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
400 else if (normal)
401 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
402 else
403 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
404}
405
406/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800407 * Release an established connection. We will have to release it to the BSC
408 * and to the network and we do it the following way.
409 * 1.) Give up on the MSC side
410 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
411 * ignore the RLC... we might complain about it. Other options would
412 * be to send a Release Request, handle the Release Complete..
413 * 1.2) Mark the data structure to be con_local and wait for 2nd
414 *
415 * 2.) Give up on the BSC side
416 * 2.1) Depending on the con type reject the service, or just close it
417 */
418static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
419{
420 struct msgb *rlsd;
421 /* 1. release the network */
422 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
423 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
424 if (!rlsd)
425 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
426 else {
427 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
428 queue_for_msc(con->msc_con, rlsd);
429 }
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200430 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800431 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800432
433 /* 2. release the BSC side */
434 if (con->con_type == NAT_CON_TYPE_LU) {
435 struct msgb *payload, *udt;
436 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
437
438 if (payload) {
439 gsm0808_prepend_dtap_header(payload, 0);
440 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
441 if (udt)
442 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
443 else
444 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
445
446 msgb_free(payload);
447 } else {
448 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
449 }
450 }
451
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200452 nat_send_clrc_bsc(con);
453
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800454 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
455 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
456 if (!rlsd) {
457 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
458 sccp_connection_destroy(con);
459 return;
460 }
461
462 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
463 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
464}
465
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800466static void bsc_send_con_refuse(struct bsc_connection *bsc,
467 struct bsc_nat_parsed *parsed, int con_type)
468{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800469 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800470 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800471
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800472 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800473 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800474 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800475 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800476 else {
477 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
478 payload = NULL;
479 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800480
481 /*
482 * Some BSCs do not handle the payload inside a SCCP CREF msg
483 * so we will need to:
484 * 1.) Allocate a local connection and mark it as local..
485 * 2.) queue data for downstream.. and the RLC should delete everything
486 */
487 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200488 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800489 struct sccp_connections *con;
490 con = create_sccp_src_ref(bsc, parsed);
491 if (!con)
492 goto send_refuse;
493
494 /* declare it local and assign a unique remote_ref */
495 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200496 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800497 con->has_remote_ref = 1;
498 con->remote_ref = con->patched_ref;
499
500 /* 1. create a confirmation */
501 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
502 if (!cc)
503 goto send_refuse;
504
505 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800506 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800507 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
508 if (!udt) {
509 msgb_free(cc);
510 goto send_refuse;
511 }
512
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200513 /* 3. send a Clear Command */
514 clear = nat_creat_clrc(con, 0x20);
515 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800516 msgb_free(cc);
517 msgb_free(udt);
518 goto send_refuse;
519 }
520
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200521 /* 4. send a RLSD */
522 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
523 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
524 if (!rlsd) {
525 msgb_free(cc);
526 msgb_free(udt);
527 msgb_free(clear);
528 goto send_refuse;
529 }
530
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800531 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
532 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200533 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800534 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
535 msgb_free(payload);
536 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800537 }
538
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800539
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800540send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800541 if (payload)
542 msgb_free(payload);
543
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800544 refuse = sccp_create_refuse(parsed->src_local_ref,
545 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800546 if (!refuse) {
547 LOGP(DNAT, LOGL_ERROR,
548 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
549 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
550 return;
551 }
552
553 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
554}
555
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200556static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg)
557{
558 if (bsc->cfg->forbid_paging) {
559 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
560 return;
561 }
562
563 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP);
564}
565
566static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg)
567{
568 struct bsc_connection *bsc;
569 const uint8_t *paging_start;
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200570 int paging_length, i, ret;
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200571
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200572 ret = bsc_nat_find_paging(msg, &paging_start, &paging_length);
573 if (ret != 0) {
574 LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200575 return;
576 }
577
578 /* This is quite expensive now */
579 for (i = 0; i < paging_length; i += 2) {
580 unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]);
581 unsigned int paged = 0;
582 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
583 if (!bsc->cfg)
584 continue;
585 if (!bsc->authenticated)
586 continue;
587 if (!bsc_config_handles_lac(bsc->cfg, _lac))
588 continue;
589 bsc_nat_send_paging(bsc, msg);
590 paged += 1;
591 }
592
593 /* highlight a possible config issue */
594 if (paged == 0)
595 LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac);
596
597 }
598}
599
600
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200601/*
602 * Update the auth status. This can be either a CIPHER MODE COMAMND or
603 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
604 */
605static void update_con_authorize(struct sccp_connections *con,
606 struct bsc_nat_parsed *parsed,
607 struct msgb *msg)
608{
609 if (!con)
610 return;
611 if (con->authorized)
612 return;
613
614 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
615 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
616 con->authorized = 1;
617 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200618 uint8_t msg_type, proto;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200619 uint32_t len;
620 struct gsm48_hdr *hdr48;
621 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
622 if (!hdr48)
623 return;
624
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200625 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200626 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200627 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200628 msg_type == GSM48_MT_MM_CM_SERV_ACC)
629 con->authorized = 1;
630 }
631}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800632
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800633static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100634{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800635 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800636 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800637 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200638 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100639
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100640 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800641 parsed = bsc_nat_parse(msg);
642 if (!parsed) {
643 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100644 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800645 }
646
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100647 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800648 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800649
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200650 proto = parsed->ipa_proto;
651
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100652 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200653 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100654 switch (parsed->sccp_type) {
655 case SCCP_MSG_TYPE_UDT:
656 /* forward UDT messages to every BSC */
657 goto send_to_all;
658 break;
659 case SCCP_MSG_TYPE_RLSD:
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200660 if (con && con->con_local == NAT_CON_END_USSD) {
661 LOGP(DNAT, LOGL_NOTICE, "RLSD for a USSD connection. Ignoring.\n");
662 con = NULL;
663 }
664 /* fall through */
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100665 case SCCP_MSG_TYPE_CREF:
666 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200667 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800668 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800669 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200670 osmo_counter_inc(nat->stats.sccp.calls);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200671
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800672 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800673 struct rate_ctr_group *ctrg;
674 ctrg = con->bsc->cfg->stats.ctrg;
675 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800676 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800677 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
678 } else
679 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200680 } else if (con && con->con_local == NAT_CON_END_USSD &&
681 parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) {
682 LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n");
683 con = NULL;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800684 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200685 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100686 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800687 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
688 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200689 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800690 break;
691 case SCCP_MSG_TYPE_RLC:
692 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
693 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100694 break;
695 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100696 /* MSC never opens a SCCP connection, fall through */
697 default:
698 goto exit;
699 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200700
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200701 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
702 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
703 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800704 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200705 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800706 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 +0100707 }
708
709 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800710 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100711 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800712 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800713 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100714 return -1;
715 }
716
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200717 update_con_authorize(con, parsed, msg);
718
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200719 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100720 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100721
722send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800723 /*
724 * Filter Paging from the network. We do not want to send a PAGING
725 * Command to every BSC in our network. We will analys the PAGING
726 * message and then send it to the authenticated messages...
727 */
728 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200729 bsc_nat_handle_paging(nat, msg);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800730 goto exit;
731 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100732 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800733 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100734 if (!bsc->authenticated)
735 continue;
736
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200737 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100738 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800739
740exit:
741 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100742 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100743}
744
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800745static void msc_connection_was_lost(struct bsc_msc_connection *con)
746{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200747 struct bsc_connection *bsc, *tmp;
748
749 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
750 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800751 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200752
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200753 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200754 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800755}
756
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800757static void msc_connection_connected(struct bsc_msc_connection *con)
758{
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200759 osmo_counter_inc(nat->stats.msc.reconn);
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800760}
761
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200762static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200763{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800764 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200765 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200766 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
767 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
768 0x01, 0x20
769 };
770
771 struct msgb *msg;
772
773 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
774 if (!msg) {
775 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
776 return;
777 }
778
779 msg->l2h = msgb_put(msg, sizeof(reset));
780 memcpy(msg->l2h, reset, msgb_l2len(msg));
781
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800782 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200783
784 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
785}
786
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200787static int ipaccess_msc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100788{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800789 struct bsc_msc_connection *msc_con;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200790 struct msgb *msg;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100791 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200792 int ret;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100793
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800794 msc_con = (struct bsc_msc_connection *) bfd->data;
795
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200796 ret = ipa_msg_recv(bfd->fd, &msg);
797 if (ret <= 0) {
798 if (ret == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100799 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800800 else
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200801 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100802
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800803 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100804 return -1;
805 }
806
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200807 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 +0100808
809 /* handle base message handling */
810 hh = (struct ipaccess_head *) msg->data;
811 ipaccess_rcvmsg_base(msg, bfd);
812
813 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800814 if (hh->proto == IPAC_PROTO_IPACCESS) {
815 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800816 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800817 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800818 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800819 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800820 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100821
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800822 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100823 return 0;
824}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800825
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200826static int ipaccess_msc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800827{
828 int rc;
829 rc = write(bfd->fd, msg->data, msg->len);
830
831 if (rc != msg->len) {
832 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
833 return -1;
834 }
835
836 return rc;
837}
838
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100839/*
840 * Below is the handling of messages coming
841 * from the BSC and need to be forwarded to
842 * a real BSC.
843 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100844
845/*
846 * Remove the connection from the connections list,
847 * remove it from the patching of SCCP header lists
848 * as well. Maybe in the future even close connection..
849 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800850void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100851{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100852 struct sccp_connections *sccp_patch, *tmp;
Daniel Willmanna86bc392011-02-18 14:32:56 +0100853 struct bsc_cmd_list *cmd_entry, *cmd_tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800854 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100855
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800856 /* stop the timeout timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200857 osmo_timer_del(&connection->id_timeout);
858 osmo_timer_del(&connection->ping_timeout);
859 osmo_timer_del(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800860
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800861 if (connection->cfg)
862 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
863
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100864 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800865 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100866 if (sccp_patch->bsc != connection)
867 continue;
868
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800869 if (ctr)
870 rate_ctr_inc(ctr);
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200871 if (sccp_patch->has_remote_ref) {
872 if (sccp_patch->con_local == NAT_CON_END_MSC)
873 nat_send_rlsd_msc(sccp_patch);
874 else if (sccp_patch->con_local == NAT_CON_END_USSD)
875 nat_send_rlsd_ussd(nat, sccp_patch);
876 }
877
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200878 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100879 }
880
Daniel Willmanna86bc392011-02-18 14:32:56 +0100881 /* Reply to all outstanding commands */
882 llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) {
883 cmd_entry->cmd->type = CTRL_TYPE_ERROR;
884 cmd_entry->cmd->reply = "BSC closed the connection";
885 ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd);
886 bsc_del_pending(cmd_entry);
887 }
888
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200889 /* close endpoints allocated by this BSC */
890 bsc_mgcp_clear_endpoints_for(connection);
891
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200892 osmo_fd_unregister(&connection->write_queue.bfd);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800893 close(connection->write_queue.bfd.fd);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200894 osmo_wqueue_clear(&connection->write_queue);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800895 llist_del(&connection->list_entry);
896
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100897 talloc_free(connection);
898}
899
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200900static void bsc_maybe_close(struct bsc_connection *bsc)
901{
902 struct sccp_connections *sccp;
903 if (!bsc->nat->blocked)
904 return;
905
906 /* are there any connections left */
907 llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry)
908 if (sccp->bsc == bsc)
909 return;
910
911 /* nothing left, close the BSC */
912 LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n",
913 bsc->cfg ? bsc->cfg->nr : -1);
914 bsc_close_connection(bsc);
915}
916
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800917static void ipaccess_close_bsc(void *data)
918{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200919 struct sockaddr_in sock;
920 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800921 struct bsc_connection *conn = data;
922
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200923
924 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
925 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
926 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800927 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800928}
929
930static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
931{
932 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800933 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200934 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800935
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800936 if (bsc->cfg) {
937 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
938 bsc->write_queue.bfd.fd, bsc->cfg->nr);
939 return;
940 }
941
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800942 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200943 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800944 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800945 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200946 bsc->cfg = conf;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200947 osmo_timer_del(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800948 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
949 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800950 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200951 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800952 }
953 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200954
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800955 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
956 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800957}
958
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800959static void handle_con_stats(struct sccp_connections *con)
960{
961 struct rate_ctr_group *ctrg;
962 int id = bsc_conn_type_to_ctr(con);
963
964 if (id == -1)
965 return;
966
967 if (!con->bsc || !con->bsc->cfg)
968 return;
969
970 ctrg = con->bsc->cfg->stats.ctrg;
971 rate_ctr_inc(&ctrg->ctr[id]);
972}
973
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100974static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100975{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800976 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800977 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800978 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800979 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800980 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800981 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100982
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800983 /* Parse and filter messages */
984 parsed = bsc_nat_parse(msg);
985 if (!parsed) {
986 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200987 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800988 return -1;
989 }
990
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100991 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800992 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800993
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200994 /*
995 * check authentication after filtering to not reject auth
996 * responses coming from the BSC. We have to make sure that
997 * nothing from the exit path will forward things to the MSC
998 */
999 if (!bsc->authenticated) {
1000 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
1001 msgb_free(msg);
1002 return -1;
1003 }
1004
1005
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001006 /* modify the SCCP entries */
1007 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +08001008 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001009 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001010 switch (parsed->sccp_type) {
1011 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001012 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001013 if (filter < 0) {
1014 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001015 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001016 }
1017
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +08001018 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001019 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001020 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +08001021 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001022 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +08001023 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +08001024 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001025 if (imsi)
1026 con->imsi = talloc_steal(con, imsi);
1027 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001028 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +08001029 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001030 break;
1031 case SCCP_MSG_TYPE_RLSD:
1032 case SCCP_MSG_TYPE_CREF:
1033 case SCCP_MSG_TYPE_DT1:
1034 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +02001035 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001036 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001037 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001038 /* only filter non local connections */
1039 if (!con->con_local) {
1040 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
1041 if (filter < 0) {
1042 bsc_stat_reject(filter, bsc, 1);
1043 bsc_send_con_release(bsc, con);
1044 con = NULL;
1045 goto exit2;
1046 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001047
1048 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +02001049 if (bsc_check_ussd(con, parsed, msg) == 1)
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001050 con->con_local = NAT_CON_END_USSD;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001051
1052 /*
1053 * Optionally rewrite setup message. This can
1054 * replace the msg and the parsed structure becomes
1055 * invalid.
1056 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001057 msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001058 talloc_free(parsed);
1059 parsed = NULL;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +02001060 } else if (con->con_local == NAT_CON_END_USSD) {
1061 bsc_check_ussd(con, parsed, msg);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +08001062 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001063
1064 con_bsc = con->bsc;
1065 con_msc = con->msc_con;
1066 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001067 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001068
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001069 break;
1070 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001071 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001072 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001073 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001074 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001075 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001076 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +02001077 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001078 bsc_maybe_close(bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001079 break;
1080 case SCCP_MSG_TYPE_UDT:
1081 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001082 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001083 break;
1084 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +08001085 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 +08001086 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001087 goto exit2;
1088 break;
1089 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +01001090 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001091 bsc_mgcp_forward(bsc, msg);
1092 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001093 } else {
1094 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
1095 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001096 }
1097
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001098 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +08001099 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 +08001100 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001101 goto exit2;
1102 }
1103
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +08001104 /* do not forward messages to the MSC */
1105 if (con_filter)
1106 goto exit2;
1107
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001108 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001109 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1110 bsc->cfg->nr,
1111 parsed ? parsed->ipa_proto : -1,
1112 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001113 goto exit2;
1114 }
1115
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001116 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001117 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001118 if (parsed)
1119 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001120 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001121
1122exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001123 /* if we filter out the reset send an ack to the BSC */
1124 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001125 send_reset_ack(bsc);
1126 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001127 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1128 /* do we know who is handling this? */
1129 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1130 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001131 int ret;
1132 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001133 (unsigned char *) msg->l2h + 2,
1134 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001135 if (ret < 0) {
1136 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
1137 "message with malformed TLVs\n");
1138 return ret;
1139 }
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001140 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1141 ipaccess_auth_bsc(&tvp, bsc);
1142 }
1143
1144 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001145 }
1146
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001147exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001148 if (imsi)
1149 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001150 talloc_free(parsed);
1151 msgb_free(msg);
1152 return -1;
1153
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001154exit3:
1155 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001156 if (imsi)
1157 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001158 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001159 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001160 msgb_free(msg);
1161 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001162}
1163
Daniel Willmanna86bc392011-02-18 14:32:56 +01001164static struct bsc_cmd_list *bsc_get_pending(struct bsc_connection *bsc, char *id_str)
1165{
1166 struct bsc_cmd_list *cmd_entry;
1167 int id = atoi(id_str);
1168 if (id == 0)
1169 return NULL;
1170
1171 llist_for_each_entry(cmd_entry, &bsc->cmd_pending, list_entry) {
1172 if (cmd_entry->nat_id == id) {
1173 return cmd_entry;
1174 }
1175 }
1176 return NULL;
1177}
1178
1179static void bsc_del_pending(struct bsc_cmd_list *pending)
1180{
1181 llist_del(&pending->list_entry);
1182 osmo_timer_del(&pending->timeout);
1183 talloc_free(pending->cmd);
1184 talloc_free(pending);
1185}
1186
Harald Weltef8e49dd2011-07-13 14:43:37 +02001187
1188static int handle_ctrlif_msg(struct bsc_connection *bsc, struct msgb *msg)
1189{
1190 struct ctrl_cmd *cmd;
1191 struct bsc_cmd_list *pending;
1192 char *var, *id;
1193
1194 cmd = ctrl_cmd_parse(bsc, msg);
1195 msgb_free(msg);
1196
1197 if (!cmd) {
1198 cmd = talloc_zero(bsc, struct ctrl_cmd);
1199 if (!cmd) {
1200 LOGP(DNAT, LOGL_ERROR, "OOM!\n");
Harald Welte65520472011-07-13 14:45:21 +02001201 return -ENOMEM;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001202 }
1203 cmd->type = CTRL_TYPE_ERROR;
1204 cmd->id = "err";
1205 cmd->reply = "Failed to parse command.";
Harald Welte65520472011-07-13 14:45:21 +02001206 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001207 }
1208
Harald Weltef071e162011-07-13 14:47:34 +02001209 if (bsc->cfg && !llist_empty(&bsc->cfg->lac_list)) {
1210 if (cmd->variable) {
Harald Welte7017fa72011-07-13 14:50:46 +02001211 struct bsc_lac_entry *bsc_lac;
1212 bsc_lac = llist_entry(bsc->cfg->lac_list.next,
1213 struct bsc_lac_entry, entry);
1214 var = talloc_asprintf(cmd, "bsc.%i.%s", bsc_lac->lac,
1215 cmd->variable);
Harald Weltef071e162011-07-13 14:47:34 +02001216 if (!var) {
1217 cmd->type = CTRL_TYPE_ERROR;
1218 cmd->reply = "OOM";
1219 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001220 }
Harald Weltef071e162011-07-13 14:47:34 +02001221 talloc_free(cmd->variable);
1222 cmd->variable = var;
1223 }
Harald Weltef8e49dd2011-07-13 14:43:37 +02001224
Harald Weltef071e162011-07-13 14:47:34 +02001225 /* Find the pending command */
1226 pending = bsc_get_pending(bsc, cmd->id);
1227 if (pending) {
1228 id = talloc_strdup(cmd, pending->cmd->id);
1229 if (!id) {
1230 cmd->type = CTRL_TYPE_ERROR;
1231 cmd->reply = "OOM";
1232 goto err;
1233 }
1234 cmd->id = id;
1235 ctrl_cmd_send(&pending->ccon->write_queue, cmd);
1236 bsc_del_pending(pending);
1237 } else {
1238 /* We need to handle TRAPS here */
Harald Welte7017fa72011-07-13 14:50:46 +02001239 if ((cmd->type != CTRL_TYPE_ERROR) &&
1240 (cmd->type != CTRL_TYPE_TRAP)) {
1241 LOGP(DNAT, LOGL_NOTICE, "Got control message "
1242 "from BSC without pending entry\n");
Harald Weltef071e162011-07-13 14:47:34 +02001243 cmd->type = CTRL_TYPE_ERROR;
1244 cmd->reply = "No request outstanding";
1245 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001246 }
1247 }
1248 }
1249 talloc_free(cmd);
1250 return 0;
1251err:
1252 ctrl_cmd_send(&bsc->write_queue, cmd);
1253 talloc_free(cmd);
1254 return 0;
1255}
1256
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001257static int ipaccess_bsc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001258{
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001259 struct bsc_connection *bsc = bfd->data;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001260 struct msgb *msg;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001261 struct ipaccess_head *hh;
Daniel Willmanna86bc392011-02-18 14:32:56 +01001262 struct ipaccess_head_ext *hh_ext;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001263 int ret;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001264
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001265 ret = ipa_msg_recv(bfd->fd, &msg);
1266 if (ret <= 0) {
1267 if (ret == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001268 LOGP(DNAT, LOGL_ERROR,
1269 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1270 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001271 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001272 LOGP(DNAT, LOGL_ERROR,
1273 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001274 bsc->cfg ? bsc->cfg->nr : -1, ret);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001275
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001276 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001277 return -1;
1278 }
1279
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001280
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001281 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 +01001282
1283 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001284 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001285
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001286 /* stop the pong timeout */
1287 if (hh->proto == IPAC_PROTO_IPACCESS) {
1288 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001289 osmo_timer_del(&bsc->pong_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001290 msgb_free(msg);
1291 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001292 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1293 send_pong(bsc);
1294 msgb_free(msg);
1295 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001296 }
Daniel Willmanna86bc392011-02-18 14:32:56 +01001297 /* Message contains the ipaccess_head_ext header, investigate further */
1298 } else if (hh->proto == IPAC_PROTO_OSMO &&
1299 msg->len > sizeof(*hh) + sizeof(*hh_ext)) {
1300
1301 hh_ext = (struct ipaccess_head_ext *) hh->data;
1302 /* l2h is where the actual command data is expected */
1303 msg->l2h = hh_ext->data;
1304
Harald Weltef8e49dd2011-07-13 14:43:37 +02001305 if (hh_ext->proto == IPAC_PROTO_EXT_CTRL)
1306 return handle_ctrlif_msg(bsc, msg);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001307 }
1308
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001309 /* FIXME: Currently no PONG is sent to the BSC */
1310 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001311 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001312
1313 return 0;
1314}
1315
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001316static int ipaccess_listen_bsc_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001317{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001318 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001319 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001320 struct sockaddr_in sa;
1321 socklen_t sa_len = sizeof(sa);
1322
1323 if (!(what & BSC_FD_READ))
1324 return 0;
1325
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001326 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1327 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001328 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001329 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001330 }
1331
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001332 /* count the reconnect */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001333 osmo_counter_inc(nat->stats.bsc.reconn);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001334
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001335 /*
1336 * if we are not connected to a msc... just close the socket
1337 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001338 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001339 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001340 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001341 return 0;
1342 }
1343
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001344 if (nat->blocked) {
1345 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n");
1346 close(fd);
1347 return 0;
1348 }
1349
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001350 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001351 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001352 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001353 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1354
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001355 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001356 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001357 if (rc != 0)
1358 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1359
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001360 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001361 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001362
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001363 /*
1364 *
1365 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001366 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001367 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001368 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001369 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001370 return -1;
1371 }
1372
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001373 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001374 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001375 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001376 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001377 bsc->write_queue.bfd.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001378 if (osmo_fd_register(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001379 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001380 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001381 talloc_free(bsc);
1382 return -2;
1383 }
1384
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001385 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001386 fd, inet_ntoa(sa.sin_addr));
Daniel Willmanna86bc392011-02-18 14:32:56 +01001387
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001388 llist_add(&bsc->list_entry, &nat->bsc_connections);
Daniel Willmanna86bc392011-02-18 14:32:56 +01001389 bsc->last_id = 0;
1390
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001391 send_id_ack(bsc);
1392 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001393 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001394
1395 /*
1396 * start the hangup timer
1397 */
1398 bsc->id_timeout.data = bsc;
1399 bsc->id_timeout.cb = ipaccess_close_bsc;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001400 osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001401 return 0;
1402}
1403
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001404static void print_usage()
1405{
1406 printf("Usage: bsc_nat\n");
1407}
1408
1409static void print_help()
1410{
1411 printf(" Some useful help...\n");
1412 printf(" -h --help this text\n");
1413 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001414 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001415 printf(" -s --disable-color\n");
1416 printf(" -c --config-file filename The config file to use.\n");
1417 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001418 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001419}
1420
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001421static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001422{
1423 while (1) {
1424 int option_index = 0, c;
1425 static struct option long_options[] = {
1426 {"help", 0, 0, 'h'},
1427 {"debug", 1, 0, 'd'},
1428 {"config-file", 1, 0, 'c'},
1429 {"disable-color", 0, 0, 's'},
1430 {"timestamp", 0, 0, 'T'},
1431 {"msc", 1, 0, 'm'},
1432 {"local", 1, 0, 'l'},
1433 {0, 0, 0, 0}
1434 };
1435
1436 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1437 long_options, &option_index);
1438 if (c == -1)
1439 break;
1440
1441 switch (c) {
1442 case 'h':
1443 print_usage();
1444 print_help();
1445 exit(0);
1446 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001447 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001448 break;
1449 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001450 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001451 break;
1452 case 'c':
1453 config_file = strdup(optarg);
1454 break;
1455 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001456 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001457 break;
1458 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001459 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001460 break;
1461 case 'l':
1462 inet_aton(optarg, &local_addr);
1463 break;
1464 default:
1465 /* ignore */
1466 break;
1467 }
1468 }
1469}
1470
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001471static void signal_handler(int signal)
1472{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001473 switch (signal) {
1474 case SIGABRT:
1475 /* in case of abort, we want to obtain a talloc report
1476 * and then return to the caller, who will abort the process */
1477 case SIGUSR1:
1478 talloc_report_full(tall_bsc_ctx, stderr);
1479 break;
1480 default:
1481 break;
1482 }
1483}
1484
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001485static void sccp_close_unconfirmed(void *_data)
1486{
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001487 int destroyed = 0;
1488 struct bsc_connection *bsc, *bsc_tmp;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001489 struct sccp_connections *conn, *tmp1;
1490 struct timespec now;
1491 clock_gettime(CLOCK_MONOTONIC, &now);
1492
1493 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1494 if (conn->has_remote_ref)
1495 continue;
1496
1497 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1498 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1499 continue;
1500
1501 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1502 sccp_src_ref_to_int(&conn->real_ref),
1503 sccp_src_ref_to_int(&conn->patched_ref));
1504 sccp_connection_destroy(conn);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001505 destroyed = 1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001506 }
1507
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001508 if (!destroyed)
1509 goto out;
1510
1511 /* now close out any BSC */
1512 llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry)
1513 bsc_maybe_close(bsc);
1514
1515out:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001516 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001517}
1518
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001519extern void *tall_msgb_ctx;
1520extern void *tall_ctr_ctx;
1521static void talloc_init_ctx()
1522{
1523 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1524 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1525 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1526}
1527
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001528extern enum node_type bsc_vty_go_parent(struct vty *vty);
1529
1530static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001531 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001532 .version = PACKAGE_VERSION,
1533 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001534 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001535};
1536
Daniel Willmanna86bc392011-02-18 14:32:56 +01001537static int get_next_free_bsc_id(struct bsc_connection *bsc)
1538{
1539 int new_id, overflow = 0;
1540 struct bsc_cmd_list *pending;
1541
1542 new_id = bsc->last_id;
1543 do {
1544 new_id++;
1545 if (new_id <= 0) {
1546 new_id = 1;
1547 overflow++;
1548 }
1549
1550 llist_for_each_entry(pending, &bsc->cmd_pending, list_entry) {
1551 if (pending->nat_id == new_id)
1552 continue;
1553 }
1554
1555 /* ID is not in use */
1556 break;
1557 } while ((new_id != bsc->last_id) && (overflow < 2));
1558
1559 if ((new_id == bsc->last_id) || (overflow == 2)) {
1560 return -1;
1561 } else {
1562 bsc->last_id = new_id;
1563 return new_id;
1564 }
1565}
1566
1567static void pending_timeout_cb(void *data)
1568{
1569 struct bsc_cmd_list *pending = data;
1570 LOGP(DNAT, LOGL_ERROR, "Command timed out\n");
1571 pending->cmd->type = CTRL_TYPE_ERROR;
1572 pending->cmd->reply = "Command timed out";
1573 ctrl_cmd_send(&pending->ccon->write_queue, pending->cmd);
1574
1575 bsc_del_pending(pending);
1576}
1577
1578static void ctrl_conn_closed_cb(struct ctrl_connection *connection)
1579{
1580 struct bsc_connection *bsc;
1581 struct bsc_cmd_list *pending, *tmp;
1582
1583 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
1584 llist_for_each_entry_safe(pending, tmp, &bsc->cmd_pending, list_entry) {
1585 if (pending->ccon == connection)
1586 bsc_del_pending(pending);
1587 }
1588 }
1589}
1590
1591static int forward_to_bsc(struct ctrl_cmd *cmd)
1592{
1593 int ret = CTRL_CMD_HANDLED;
1594 struct ctrl_cmd *bsc_cmd = NULL;
1595 struct bsc_connection *bsc;
1596 struct bsc_cmd_list *pending;
1597 unsigned int lac;
1598 char *lac_str, *tmp, *saveptr;
1599
1600 /* Skip over the beginning (bsc.) */
1601 tmp = strtok_r(cmd->variable, ".", &saveptr);
1602 lac_str = strtok_r(NULL, ".", &saveptr);
1603 if (!lac_str) {
1604 cmd->reply = "command incomplete";
1605 goto err;
1606 }
1607 lac = atoi(lac_str);
1608
1609 tmp = strtok_r(NULL, "\0", &saveptr);
1610 if (!tmp) {
1611 cmd->reply = "command incomplete";
1612 goto err;
1613 }
1614
1615 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
1616 if (!bsc->cfg)
1617 continue;
1618 if (!bsc->authenticated)
1619 continue;
1620 if (bsc_config_handles_lac(bsc->cfg, lac)) {
1621 /* Add pending command to list */
1622 pending = talloc_zero(bsc, struct bsc_cmd_list);
1623 if (!pending) {
1624 cmd->reply = "OOM";
1625 goto err;
1626 }
1627
1628 pending->nat_id = get_next_free_bsc_id(bsc);
1629 if (pending->nat_id < 0) {
1630 cmd->reply = "No free ID found";
1631 goto err;
1632 }
1633
1634 bsc_cmd = ctrl_cmd_cpy(bsc, cmd);
1635 if (!bsc_cmd) {
1636 cmd->reply = "Could not forward command";
1637 goto err;
1638 }
1639
1640 talloc_free(bsc_cmd->id);
1641 bsc_cmd->id = talloc_asprintf(bsc_cmd, "%i", pending->nat_id);
1642 if (!bsc_cmd->id) {
1643 cmd->reply = "OOM";
1644 goto err;
1645 }
1646
1647 talloc_free(bsc_cmd->variable);
1648 bsc_cmd->variable = talloc_strdup(bsc_cmd, tmp);
1649 if (!bsc_cmd->variable) {
1650 cmd->reply = "OOM";
1651 goto err;
1652 }
1653
1654 if (ctrl_cmd_send(&bsc->write_queue, bsc_cmd)) {
1655 cmd->reply = "Sending failed";
1656 goto err;
1657 }
1658 pending->ccon = cmd->ccon;
1659 pending->ccon->closed_cb = ctrl_conn_closed_cb;
1660 pending->cmd = cmd;
1661
1662 /* Setup the timeout */
1663 pending->timeout.data = pending;
1664 pending->timeout.cb = pending_timeout_cb;
1665 /* TODO: Make timeout configurable */
1666 osmo_timer_schedule(&pending->timeout, 10, 0);
1667 llist_add_tail(&pending->list_entry, &bsc->cmd_pending);
1668
1669 goto done;
1670 }
1671 }
1672 /* We end up here if there's no bsc to handle our LAC */
1673 cmd->reply = "no BSC with this LAC";
1674err:
1675 ret = CTRL_CMD_ERROR;
1676done:
1677 if (bsc_cmd)
1678 talloc_free(bsc_cmd);
1679 return ret;
1680
1681}
1682
1683CTRL_CMD_DEFINE(fwd_cmd, "bsc *");
1684int get_fwd_cmd(struct ctrl_cmd *cmd, void *data)
1685{
1686 return forward_to_bsc(cmd);
1687}
1688
1689int set_fwd_cmd(struct ctrl_cmd *cmd, void *data)
1690{
1691 return forward_to_bsc(cmd);
1692}
1693
1694int verify_fwd_cmd(struct ctrl_cmd *cmd, const char *value, void *data)
1695{
1696 return 0;
1697}
1698
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001699int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001700{
Harald Welte2c869ef2010-08-25 19:43:54 +02001701 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001702
Harald Welte2c869ef2010-08-25 19:43:54 +02001703 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001704
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001705 osmo_init_logging(&log_info);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001706
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001707 nat = bsc_nat_alloc();
1708 if (!nat) {
1709 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1710 return -4;
1711 }
1712
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001713 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001714 if (!nat->mgcp_cfg) {
1715 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1716 return -5;
1717 }
1718
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001719 vty_info.copyright = openbsc_copyright;
1720 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001721 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001722 bsc_nat_vty_init(nat);
1723
1724
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001725 /* parse options */
1726 local_addr.s_addr = INADDR_ANY;
1727 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001728
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001729 rate_ctr_init(tall_bsc_ctx);
1730
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001731 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001732 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001733 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001734 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1735 return -3;
1736 }
1737
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001738 /* over rule the VTY config */
1739 if (msc_ip)
1740 bsc_nat_set_msc_ip(nat, msc_ip);
1741
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001742 /* seed the PRNG */
1743 srand(time(NULL));
1744
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001745 /*
1746 * Setup the MGCP code..
1747 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001748 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001749 return -4;
1750
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001751 /* connect to the MSC */
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +02001752 nat->msc_con = bsc_msc_create(nat, &nat->dests);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001753 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001754 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001755 exit(1);
1756 }
1757
Daniel Willmanna86bc392011-02-18 14:32:56 +01001758 controlif_setup(NULL, 4250);
1759 ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_fwd_cmd);
1760
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001761 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001762 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001763 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1764 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001765 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001766 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001767
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001768 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001769 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001770 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001771 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001772 fprintf(stderr, "Failed to listen for BSC.\n");
1773 exit(1);
1774 }
1775
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001776 rc = bsc_ussd_init(nat);
1777 if (rc != 0) {
1778 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1779 exit(1);
1780 }
1781
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001782 signal(SIGABRT, &signal_handler);
1783 signal(SIGUSR1, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001784 osmo_init_ignore_signals();
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001785
Harald Welte2c869ef2010-08-25 19:43:54 +02001786 if (daemonize) {
1787 rc = osmo_daemonize();
1788 if (rc < 0) {
1789 perror("Error during daemonize");
1790 exit(1);
1791 }
1792 }
1793
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001794 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001795 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001796 sccp_close.cb = sccp_close_unconfirmed;
1797 sccp_close.data = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001798 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001799
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001800 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001801 osmo_select_main(0);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001802 }
1803
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001804 return 0;
1805}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001806
1807/* Close all connections handed out to the USSD module */
1808int bsc_close_ussd_connections(struct bsc_nat *nat)
1809{
1810 struct sccp_connections *con;
1811 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001812 if (con->con_local != NAT_CON_END_USSD)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001813 continue;
1814 if (!con->bsc)
1815 continue;
1816
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001817 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001818 nat_send_rlsd_bsc(con);
1819 }
1820
1821 return 0;
1822}