blob: 2146da1c49082a73efcdd83a9a96b1ddc8f5ee5e [file] [log] [blame]
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +02004 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080021 *
22 */
23#include <sys/socket.h>
24#include <netinet/in.h>
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +080025#include <netinet/tcp.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080026#include <arpa/inet.h>
27
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freyther5aa25ae2010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080033#include <unistd.h>
34
35#define _GNU_SOURCE
36#include <getopt.h>
37
38#include <openbsc/debug.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010039#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080041#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +020044#include <openbsc/socket.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080045#include <openbsc/vty.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080046
Harald Welted36ff762011-03-23 18:26:56 +010047#include <osmocom/gsm/gsm0808.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010048#include <osmocom/core/talloc.h>
49#include <osmocom/core/process.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080050
Harald Welted36ff762011-03-23 18:26:56 +010051#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080052
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080053#include <osmocom/vty/telnet_interface.h>
54#include <osmocom/vty/vty.h>
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +010055#include <osmocom/vty/logging.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080056
Harald Welted5db12c2010-08-03 15:11:51 +020057#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080058
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080059#include "../../bscconfig.h"
60
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080061#define SCCP_CLOSE_TIME 20
62#define SCCP_CLOSE_TIME_TIMEOUT 19
63
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080064struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080065static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080066static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010067static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080068static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080069static struct timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020070static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010071
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080072const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080073 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010074 "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 +080075 "This is free software: you are free to change and redistribute it.\r\n"
76 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010077
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080078static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080079static 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 +020080static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080081static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080082
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080083struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
84{
85 struct bsc_config *conf;
86
87 llist_for_each_entry(conf, &nat->bsc_configs, entry)
88 if (conf->nr == num)
89 return conf;
90
91 return NULL;
92}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010093
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080094static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
95{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +080096 if (!con) {
97 LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
98 msgb_free(msg);
99 return;
100 }
101
102
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800103 if (write_queue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800104 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
105 msgb_free(msg);
106 }
107}
108
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100109static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100110{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800111 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100112 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
113 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
114 0x00, 0x01, 0x31,
115 };
116
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200117 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100118}
119
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800120static void send_ping(struct bsc_connection *bsc)
121{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800122 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800123 IPAC_MSGT_PING,
124 };
125
126 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
127}
128
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800129static void send_pong(struct bsc_connection *bsc)
130{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800131 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800132 IPAC_MSGT_PONG,
133 };
134
135 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
136}
137
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800138static void bsc_pong_timeout(void *_bsc)
139{
140 struct bsc_connection *bsc = _bsc;
141
142 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
143 bsc_close_connection(bsc);
144}
145
146static void bsc_ping_timeout(void *_bsc)
147{
148 struct bsc_connection *bsc = _bsc;
149
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800150 if (bsc->nat->ping_timeout < 0)
151 return;
152
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800153 send_ping(bsc);
154
155 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800156 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800157
158 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800159 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800160}
161
162static void start_ping_pong(struct bsc_connection *bsc)
163{
164 bsc->pong_timeout.data = bsc;
165 bsc->pong_timeout.cb = bsc_pong_timeout;
166 bsc->ping_timeout.data = bsc;
167 bsc->ping_timeout.cb = bsc_ping_timeout;
168
169 bsc_ping_timeout(bsc);
170}
171
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100172static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100173{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800174 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200175 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100176 };
177
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200178 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100179}
180
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100181static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100182{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800183 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200184 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100185 0x01, IPAC_IDTAG_UNIT,
186 0x01, IPAC_IDTAG_MACADDR,
187 0x01, IPAC_IDTAG_LOCATION1,
188 0x01, IPAC_IDTAG_LOCATION2,
189 0x01, IPAC_IDTAG_EQUIPVERS,
190 0x01, IPAC_IDTAG_SWVERSION,
191 0x01, IPAC_IDTAG_UNITNAME,
192 0x01, IPAC_IDTAG_SERNR,
193 };
194
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200195 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100196}
197
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200198static struct msgb *nat_create_rlsd(struct sccp_connections *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200199{
200 struct sccp_connection_released *rel;
201 struct msgb *msg;
202
203 msg = msgb_alloc_headroom(4096, 128, "rlsd");
204 if (!msg) {
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200205 LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n");
206 return NULL;
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200207 }
208
209 msg->l2h = msgb_put(msg, sizeof(*rel));
210 rel = (struct sccp_connection_released *) msg->l2h;
211 rel->type = SCCP_MSG_TYPE_RLSD;
212 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
213 rel->destination_local_reference = conn->remote_ref;
214 rel->source_local_reference = conn->patched_ref;
215
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200216 return msg;
217}
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200218
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200219static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct sccp_connections *conn)
220{
221 struct msgb *msg;
222
223 if (!nat->ussd_con)
224 return;
225
226 msg = nat_create_rlsd(conn);
227 if (!msg)
228 return;
229
230 bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP);
231}
232
233static void nat_send_rlsd_msc(struct sccp_connections *conn)
234{
235 struct msgb *msg;
236
237 msg = nat_create_rlsd(conn);
238 if (!msg)
239 return;
240
241 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800242 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200243}
244
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200245static void nat_send_rlsd_bsc(struct sccp_connections *conn)
246{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200247 struct msgb *msg;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200248 struct sccp_connection_released *rel;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200249
250 msg = msgb_alloc_headroom(4096, 128, "rlsd");
251 if (!msg) {
252 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
253 return;
254 }
255
256 msg->l2h = msgb_put(msg, sizeof(*rel));
257 rel = (struct sccp_connection_released *) msg->l2h;
258 rel->type = SCCP_MSG_TYPE_RLSD;
259 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
260 rel->destination_local_reference = conn->real_ref;
261 rel->source_local_reference = conn->remote_ref;
262
263 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
264}
265
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200266static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200267{
268 struct msgb *msg;
269 struct msgb *sccp;
270
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200271 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200272 if (!msg) {
273 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200274 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200275 }
276
277 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
278 if (!sccp) {
279 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
280 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200281 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200282 }
283
284 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200285 return sccp;
286}
287
288static int nat_send_clrc_bsc(struct sccp_connections *conn)
289{
290 struct msgb *sccp;
291
292 sccp = nat_creat_clrc(conn, 0x20);
293 if (!sccp)
294 return -1;
295 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200296}
297
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800298static void nat_send_rlc(struct bsc_msc_connection *msc_con,
299 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200300 struct sccp_source_reference *dst)
301{
302 struct sccp_connection_release_complete *rlc;
303 struct msgb *msg;
304
305 msg = msgb_alloc_headroom(4096, 128, "rlc");
306 if (!msg) {
307 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
308 return;
309 }
310
311 msg->l2h = msgb_put(msg, sizeof(*rlc));
312 rlc = (struct sccp_connection_release_complete *) msg->l2h;
313 rlc->type = SCCP_MSG_TYPE_RLC;
314 rlc->destination_local_reference = *dst;
315 rlc->source_local_reference = *src;
316
317 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
318
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800319 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200320}
321
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200322static void send_mgcp_reset(struct bsc_connection *bsc)
323{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800324 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200325 "RSIP 1 13@mgw MGCP 1.0\r\n"
326 };
327
328 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
329}
330
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100331/*
332 * Below is the handling of messages coming
333 * from the MSC and need to be forwarded to
334 * a real BSC.
335 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800336static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100337{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800338 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200339 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100340
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800341 msc_con->first_contact = 1;
342 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100343}
344
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800345static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800346{
347 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
348 if (!msg)
349 return;
350
351 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800352 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800353}
354
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100355/*
356 * Currently we are lacking refcounting so we need to copy each message.
357 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800358static 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 +0100359{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100360 struct msgb *msg;
361
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200362 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100363 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
364 return;
365 }
366
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200367 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100368 if (!msg) {
369 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
370 return;
371 }
372
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200373 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100374 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200375
376 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100377}
378
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800379/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800380 * Update the release statistics
381 */
382static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
383{
384 if (!bsc->cfg) {
385 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
386 return;
387 }
388
389 if (filter >= 0) {
390 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
391 return;
392 }
393
394 if (filter == -1)
395 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
396 else if (normal)
397 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
398 else
399 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
400}
401
402/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800403 * Release an established connection. We will have to release it to the BSC
404 * and to the network and we do it the following way.
405 * 1.) Give up on the MSC side
406 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
407 * ignore the RLC... we might complain about it. Other options would
408 * be to send a Release Request, handle the Release Complete..
409 * 1.2) Mark the data structure to be con_local and wait for 2nd
410 *
411 * 2.) Give up on the BSC side
412 * 2.1) Depending on the con type reject the service, or just close it
413 */
414static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
415{
416 struct msgb *rlsd;
417 /* 1. release the network */
418 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
419 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
420 if (!rlsd)
421 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
422 else {
423 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
424 queue_for_msc(con->msc_con, rlsd);
425 }
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200426 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800427 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800428
429 /* 2. release the BSC side */
430 if (con->con_type == NAT_CON_TYPE_LU) {
431 struct msgb *payload, *udt;
432 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
433
434 if (payload) {
435 gsm0808_prepend_dtap_header(payload, 0);
436 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
437 if (udt)
438 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
439 else
440 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
441
442 msgb_free(payload);
443 } else {
444 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
445 }
446 }
447
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200448 nat_send_clrc_bsc(con);
449
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800450 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
451 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
452 if (!rlsd) {
453 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
454 sccp_connection_destroy(con);
455 return;
456 }
457
458 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
459 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
460}
461
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800462static void bsc_send_con_refuse(struct bsc_connection *bsc,
463 struct bsc_nat_parsed *parsed, int con_type)
464{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800465 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800466 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800467
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800468 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800469 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800470 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800471 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800472 else {
473 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
474 payload = NULL;
475 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800476
477 /*
478 * Some BSCs do not handle the payload inside a SCCP CREF msg
479 * so we will need to:
480 * 1.) Allocate a local connection and mark it as local..
481 * 2.) queue data for downstream.. and the RLC should delete everything
482 */
483 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200484 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800485 struct sccp_connections *con;
486 con = create_sccp_src_ref(bsc, parsed);
487 if (!con)
488 goto send_refuse;
489
490 /* declare it local and assign a unique remote_ref */
491 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200492 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800493 con->has_remote_ref = 1;
494 con->remote_ref = con->patched_ref;
495
496 /* 1. create a confirmation */
497 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
498 if (!cc)
499 goto send_refuse;
500
501 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800502 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800503 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
504 if (!udt) {
505 msgb_free(cc);
506 goto send_refuse;
507 }
508
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200509 /* 3. send a Clear Command */
510 clear = nat_creat_clrc(con, 0x20);
511 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800512 msgb_free(cc);
513 msgb_free(udt);
514 goto send_refuse;
515 }
516
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200517 /* 4. send a RLSD */
518 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
519 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
520 if (!rlsd) {
521 msgb_free(cc);
522 msgb_free(udt);
523 msgb_free(clear);
524 goto send_refuse;
525 }
526
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800527 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
528 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200529 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800530 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
531 msgb_free(payload);
532 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800533 }
534
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800535
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800536send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800537 if (payload)
538 msgb_free(payload);
539
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800540 refuse = sccp_create_refuse(parsed->src_local_ref,
541 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800542 if (!refuse) {
543 LOGP(DNAT, LOGL_ERROR,
544 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
545 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
546 return;
547 }
548
549 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
550}
551
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200552/*
553 * Update the auth status. This can be either a CIPHER MODE COMAMND or
554 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
555 */
556static void update_con_authorize(struct sccp_connections *con,
557 struct bsc_nat_parsed *parsed,
558 struct msgb *msg)
559{
560 if (!con)
561 return;
562 if (con->authorized)
563 return;
564
565 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
566 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
567 con->authorized = 1;
568 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200569 uint8_t msg_type, proto;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200570 uint32_t len;
571 struct gsm48_hdr *hdr48;
572 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
573 if (!hdr48)
574 return;
575
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200576 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200577 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200578 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200579 msg_type == GSM48_MT_MM_CM_SERV_ACC)
580 con->authorized = 1;
581 }
582}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800583
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800584static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100585{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800586 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800587 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800588 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200589 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100590
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100591 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800592 parsed = bsc_nat_parse(msg);
593 if (!parsed) {
594 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100595 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800596 }
597
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100598 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800599 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800600
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200601 proto = parsed->ipa_proto;
602
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100603 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200604 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100605 switch (parsed->sccp_type) {
606 case SCCP_MSG_TYPE_UDT:
607 /* forward UDT messages to every BSC */
608 goto send_to_all;
609 break;
610 case SCCP_MSG_TYPE_RLSD:
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200611 if (con && con->con_local == NAT_CON_END_USSD) {
612 LOGP(DNAT, LOGL_NOTICE, "RLSD for a USSD connection. Ignoring.\n");
613 con = NULL;
614 }
615 /* fall through */
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100616 case SCCP_MSG_TYPE_CREF:
617 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200618 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800619 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800620 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200621 counter_inc(nat->stats.sccp.calls);
622
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800623 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800624 struct rate_ctr_group *ctrg;
625 ctrg = con->bsc->cfg->stats.ctrg;
626 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800627 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800628 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
629 } else
630 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200631 } else if (con && con->con_local == NAT_CON_END_USSD &&
632 parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) {
633 LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n");
634 con = NULL;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800635 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200636 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100637 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800638 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
639 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200640 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800641 break;
642 case SCCP_MSG_TYPE_RLC:
643 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
644 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100645 break;
646 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100647 /* MSC never opens a SCCP connection, fall through */
648 default:
649 goto exit;
650 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200651
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200652 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
653 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
654 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800655 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200656 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800657 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 +0100658 }
659
660 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800661 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100662 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800663 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800664 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100665 return -1;
666 }
667
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200668 update_con_authorize(con, parsed, msg);
669
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200670 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100671 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100672
673send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800674 /*
675 * Filter Paging from the network. We do not want to send a PAGING
676 * Command to every BSC in our network. We will analys the PAGING
677 * message and then send it to the authenticated messages...
678 */
679 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200680 int lac;
681 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800682 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800683 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800684 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200685 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherda340632010-08-06 20:15:40 +0800686 else if (lac != -1)
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200687 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
688 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800689
690 goto exit;
691 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100692 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800693 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100694 if (!bsc->authenticated)
695 continue;
696
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200697 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100698 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800699
700exit:
701 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100702 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100703}
704
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800705static void msc_connection_was_lost(struct bsc_msc_connection *con)
706{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200707 struct bsc_connection *bsc, *tmp;
708
709 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
710 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800711 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200712
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200713 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200714 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800715}
716
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800717static void msc_connection_connected(struct bsc_msc_connection *con)
718{
719 counter_inc(nat->stats.msc.reconn);
720}
721
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200722static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200723{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800724 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200725 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200726 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
727 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
728 0x01, 0x20
729 };
730
731 struct msgb *msg;
732
733 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
734 if (!msg) {
735 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
736 return;
737 }
738
739 msg->l2h = msgb_put(msg, sizeof(reset));
740 memcpy(msg->l2h, reset, msgb_l2len(msg));
741
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800742 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200743
744 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
745}
746
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800747static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100748{
749 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800750 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100751 struct msgb *msg = ipaccess_read_msg(bfd, &error);
752 struct ipaccess_head *hh;
753
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800754 msc_con = (struct bsc_msc_connection *) bfd->data;
755
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100756 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800757 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100758 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800759 else
760 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100761
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800762 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100763 return -1;
764 }
765
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100766 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100767
768 /* handle base message handling */
769 hh = (struct ipaccess_head *) msg->data;
770 ipaccess_rcvmsg_base(msg, bfd);
771
772 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800773 if (hh->proto == IPAC_PROTO_IPACCESS) {
774 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800775 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800776 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800777 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800778 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800779 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100780
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800781 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100782 return 0;
783}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800784
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800785static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
786{
787 int rc;
788 rc = write(bfd->fd, msg->data, msg->len);
789
790 if (rc != msg->len) {
791 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
792 return -1;
793 }
794
795 return rc;
796}
797
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100798/*
799 * Below is the handling of messages coming
800 * from the BSC and need to be forwarded to
801 * a real BSC.
802 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100803
804/*
805 * Remove the connection from the connections list,
806 * remove it from the patching of SCCP header lists
807 * as well. Maybe in the future even close connection..
808 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800809void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100810{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100811 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800812 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100813
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800814 /* stop the timeout timer */
815 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800816 bsc_del_timer(&connection->ping_timeout);
817 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800818
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800819 if (connection->cfg)
820 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
821
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100822 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800823 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100824 if (sccp_patch->bsc != connection)
825 continue;
826
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800827 if (ctr)
828 rate_ctr_inc(ctr);
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200829 if (sccp_patch->has_remote_ref) {
830 if (sccp_patch->con_local == NAT_CON_END_MSC)
831 nat_send_rlsd_msc(sccp_patch);
832 else if (sccp_patch->con_local == NAT_CON_END_USSD)
833 nat_send_rlsd_ussd(nat, sccp_patch);
834 }
835
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200836 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100837 }
838
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200839 /* close endpoints allocated by this BSC */
840 bsc_mgcp_clear_endpoints_for(connection);
841
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800842 bsc_unregister_fd(&connection->write_queue.bfd);
843 close(connection->write_queue.bfd.fd);
844 write_queue_clear(&connection->write_queue);
845 llist_del(&connection->list_entry);
846
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100847 talloc_free(connection);
848}
849
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200850static void bsc_maybe_close(struct bsc_connection *bsc)
851{
852 struct sccp_connections *sccp;
853 if (!bsc->nat->blocked)
854 return;
855
856 /* are there any connections left */
857 llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry)
858 if (sccp->bsc == bsc)
859 return;
860
861 /* nothing left, close the BSC */
862 LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n",
863 bsc->cfg ? bsc->cfg->nr : -1);
864 bsc_close_connection(bsc);
865}
866
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800867static void ipaccess_close_bsc(void *data)
868{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200869 struct sockaddr_in sock;
870 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800871 struct bsc_connection *conn = data;
872
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200873
874 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
875 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
876 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800877 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800878}
879
880static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
881{
882 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800883 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200884 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800885
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800886 if (bsc->cfg) {
887 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
888 bsc->write_queue.bfd.fd, bsc->cfg->nr);
889 return;
890 }
891
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800892 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200893 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800894 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800895 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200896 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800897 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800898 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
899 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800900 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200901 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800902 }
903 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200904
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800905 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
906 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800907}
908
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800909static void handle_con_stats(struct sccp_connections *con)
910{
911 struct rate_ctr_group *ctrg;
912 int id = bsc_conn_type_to_ctr(con);
913
914 if (id == -1)
915 return;
916
917 if (!con->bsc || !con->bsc->cfg)
918 return;
919
920 ctrg = con->bsc->cfg->stats.ctrg;
921 rate_ctr_inc(&ctrg->ctr[id]);
922}
923
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100924static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100925{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800926 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800927 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800928 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800929 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800930 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800931 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100932
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800933 /* Parse and filter messages */
934 parsed = bsc_nat_parse(msg);
935 if (!parsed) {
936 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200937 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800938 return -1;
939 }
940
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100941 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800942 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800943
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200944 /*
945 * check authentication after filtering to not reject auth
946 * responses coming from the BSC. We have to make sure that
947 * nothing from the exit path will forward things to the MSC
948 */
949 if (!bsc->authenticated) {
950 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
951 msgb_free(msg);
952 return -1;
953 }
954
955
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100956 /* modify the SCCP entries */
957 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800958 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800959 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100960 switch (parsed->sccp_type) {
961 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800962 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800963 if (filter < 0) {
964 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800965 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800966 }
967
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800968 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100969 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800970 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800971 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800972 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800973 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800974 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800975 if (imsi)
976 con->imsi = talloc_steal(con, imsi);
977 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800978 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800979 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100980 break;
981 case SCCP_MSG_TYPE_RLSD:
982 case SCCP_MSG_TYPE_CREF:
983 case SCCP_MSG_TYPE_DT1:
984 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200985 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800986 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800987 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800988 /* only filter non local connections */
989 if (!con->con_local) {
990 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
991 if (filter < 0) {
992 bsc_stat_reject(filter, bsc, 1);
993 bsc_send_con_release(bsc, con);
994 con = NULL;
995 goto exit2;
996 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800997
998 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200999 if (bsc_check_ussd(con, parsed, msg) == 1)
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001000 con->con_local = NAT_CON_END_USSD;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001001
1002 /*
1003 * Optionally rewrite setup message. This can
1004 * replace the msg and the parsed structure becomes
1005 * invalid.
1006 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001007 msg = bsc_nat_rewrite_setup(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001008 talloc_free(parsed);
1009 parsed = NULL;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +02001010 } else if (con->con_local == NAT_CON_END_USSD) {
1011 bsc_check_ussd(con, parsed, msg);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +08001012 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001013
1014 con_bsc = con->bsc;
1015 con_msc = con->msc_con;
1016 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001017 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001018
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001019 break;
1020 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001021 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001022 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001023 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001024 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001025 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001026 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +02001027 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001028 bsc_maybe_close(bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001029 break;
1030 case SCCP_MSG_TYPE_UDT:
1031 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001032 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001033 break;
1034 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +08001035 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 +08001036 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001037 goto exit2;
1038 break;
1039 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +01001040 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001041 bsc_mgcp_forward(bsc, msg);
1042 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001043 } else {
1044 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
1045 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001046 }
1047
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001048 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +08001049 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 +08001050 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001051 goto exit2;
1052 }
1053
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +08001054 /* do not forward messages to the MSC */
1055 if (con_filter)
1056 goto exit2;
1057
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001058 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001059 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1060 bsc->cfg->nr,
1061 parsed ? parsed->ipa_proto : -1,
1062 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001063 goto exit2;
1064 }
1065
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001066 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001067 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001068 if (parsed)
1069 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001070 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001071
1072exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001073 /* if we filter out the reset send an ack to the BSC */
1074 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001075 send_reset_ack(bsc);
1076 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001077 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1078 /* do we know who is handling this? */
1079 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1080 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001081 int ret;
1082 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001083 (unsigned char *) msg->l2h + 2,
1084 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001085 if (ret < 0) {
1086 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
1087 "message with malformed TLVs\n");
1088 return ret;
1089 }
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001090 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1091 ipaccess_auth_bsc(&tvp, bsc);
1092 }
1093
1094 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001095 }
1096
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001097exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001098 if (imsi)
1099 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001100 talloc_free(parsed);
1101 msgb_free(msg);
1102 return -1;
1103
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001104exit3:
1105 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001106 if (imsi)
1107 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001108 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001109 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001110 msgb_free(msg);
1111 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001112}
1113
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001114static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001115{
1116 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001117 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001118 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001119 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001120
1121 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001122 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001123 LOGP(DNAT, LOGL_ERROR,
1124 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1125 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001126 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001127 LOGP(DNAT, LOGL_ERROR,
1128 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
1129 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001130
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001131 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001132 return -1;
1133 }
1134
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001135
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001136 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001137
1138 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001139 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001140
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001141 /* stop the pong timeout */
1142 if (hh->proto == IPAC_PROTO_IPACCESS) {
1143 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001144 bsc_del_timer(&bsc->pong_timeout);
1145 msgb_free(msg);
1146 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001147 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1148 send_pong(bsc);
1149 msgb_free(msg);
1150 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001151 }
1152 }
1153
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001154 /* FIXME: Currently no PONG is sent to the BSC */
1155 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001156 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001157
1158 return 0;
1159}
1160
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001161static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
1162{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001163 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001164 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001165 struct sockaddr_in sa;
1166 socklen_t sa_len = sizeof(sa);
1167
1168 if (!(what & BSC_FD_READ))
1169 return 0;
1170
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001171 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1172 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001173 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001174 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001175 }
1176
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001177 /* count the reconnect */
1178 counter_inc(nat->stats.bsc.reconn);
1179
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001180 /*
1181 * if we are not connected to a msc... just close the socket
1182 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001183 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001184 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001185 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001186 return 0;
1187 }
1188
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001189 if (nat->blocked) {
1190 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n");
1191 close(fd);
1192 return 0;
1193 }
1194
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001195 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001196 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001197 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001198 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1199
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001200 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001201 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001202 if (rc != 0)
1203 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1204
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001205 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001206 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001207
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001208 /*
1209 *
1210 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001211 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001212 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001213 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001214 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001215 return -1;
1216 }
1217
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001218 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001219 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001220 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001221 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001222 bsc->write_queue.bfd.when = BSC_FD_READ;
1223 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001224 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001225 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001226 talloc_free(bsc);
1227 return -2;
1228 }
1229
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001230 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001231 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001232 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001233 send_id_ack(bsc);
1234 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001235 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001236
1237 /*
1238 * start the hangup timer
1239 */
1240 bsc->id_timeout.data = bsc;
1241 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +08001242 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001243 return 0;
1244}
1245
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001246static void print_usage()
1247{
1248 printf("Usage: bsc_nat\n");
1249}
1250
1251static void print_help()
1252{
1253 printf(" Some useful help...\n");
1254 printf(" -h --help this text\n");
1255 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001256 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001257 printf(" -s --disable-color\n");
1258 printf(" -c --config-file filename The config file to use.\n");
1259 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001260 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001261}
1262
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001263static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001264{
1265 while (1) {
1266 int option_index = 0, c;
1267 static struct option long_options[] = {
1268 {"help", 0, 0, 'h'},
1269 {"debug", 1, 0, 'd'},
1270 {"config-file", 1, 0, 'c'},
1271 {"disable-color", 0, 0, 's'},
1272 {"timestamp", 0, 0, 'T'},
1273 {"msc", 1, 0, 'm'},
1274 {"local", 1, 0, 'l'},
1275 {0, 0, 0, 0}
1276 };
1277
1278 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1279 long_options, &option_index);
1280 if (c == -1)
1281 break;
1282
1283 switch (c) {
1284 case 'h':
1285 print_usage();
1286 print_help();
1287 exit(0);
1288 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001289 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001290 break;
1291 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001292 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001293 break;
1294 case 'c':
1295 config_file = strdup(optarg);
1296 break;
1297 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001298 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001299 break;
1300 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001301 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001302 break;
1303 case 'l':
1304 inet_aton(optarg, &local_addr);
1305 break;
1306 default:
1307 /* ignore */
1308 break;
1309 }
1310 }
1311}
1312
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001313static void signal_handler(int signal)
1314{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001315 switch (signal) {
1316 case SIGABRT:
1317 /* in case of abort, we want to obtain a talloc report
1318 * and then return to the caller, who will abort the process */
1319 case SIGUSR1:
1320 talloc_report_full(tall_bsc_ctx, stderr);
1321 break;
1322 default:
1323 break;
1324 }
1325}
1326
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001327static void sccp_close_unconfirmed(void *_data)
1328{
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001329 int destroyed = 0;
1330 struct bsc_connection *bsc, *bsc_tmp;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001331 struct sccp_connections *conn, *tmp1;
1332 struct timespec now;
1333 clock_gettime(CLOCK_MONOTONIC, &now);
1334
1335 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1336 if (conn->has_remote_ref)
1337 continue;
1338
1339 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1340 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1341 continue;
1342
1343 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1344 sccp_src_ref_to_int(&conn->real_ref),
1345 sccp_src_ref_to_int(&conn->patched_ref));
1346 sccp_connection_destroy(conn);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001347 destroyed = 1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001348 }
1349
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001350 if (!destroyed)
1351 goto out;
1352
1353 /* now close out any BSC */
1354 llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry)
1355 bsc_maybe_close(bsc);
1356
1357out:
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001358 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1359}
1360
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001361extern void *tall_msgb_ctx;
1362extern void *tall_ctr_ctx;
1363static void talloc_init_ctx()
1364{
1365 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1366 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1367 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1368}
1369
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001370extern enum node_type bsc_vty_go_parent(struct vty *vty);
1371
1372static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001373 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001374 .version = PACKAGE_VERSION,
1375 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001376 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001377};
1378
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001379int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001380{
Harald Welte2c869ef2010-08-25 19:43:54 +02001381 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001382
Harald Welte2c869ef2010-08-25 19:43:54 +02001383 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001384
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001385 log_init(&log_info);
1386 stderr_target = log_target_create_stderr();
1387 log_add_target(stderr_target);
1388 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001389
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001390 nat = bsc_nat_alloc();
1391 if (!nat) {
1392 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1393 return -4;
1394 }
1395
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001396 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001397 if (!nat->mgcp_cfg) {
1398 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1399 return -5;
1400 }
1401
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001402 vty_info.copyright = openbsc_copyright;
1403 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001404 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001405 bsc_nat_vty_init(nat);
1406
1407
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001408 /* parse options */
1409 local_addr.s_addr = INADDR_ANY;
1410 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001411
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001412 rate_ctr_init(tall_bsc_ctx);
1413
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001414 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001415 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001416 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001417 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1418 return -3;
1419 }
1420
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001421 /* over rule the VTY config */
1422 if (msc_ip)
1423 bsc_nat_set_msc_ip(nat, msc_ip);
1424
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001425 /* seed the PRNG */
1426 srand(time(NULL));
1427
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001428 /*
1429 * Setup the MGCP code..
1430 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001431 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001432 return -4;
1433
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001434 /* connect to the MSC */
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +02001435 nat->msc_con = bsc_msc_create(nat, &nat->dests);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001436 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001437 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001438 exit(1);
1439 }
1440
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001441 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001442 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001443 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1444 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001445 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001446 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001447
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001448 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001449 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001450 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001451 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001452 fprintf(stderr, "Failed to listen for BSC.\n");
1453 exit(1);
1454 }
1455
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001456 rc = bsc_ussd_init(nat);
1457 if (rc != 0) {
1458 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1459 exit(1);
1460 }
1461
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001462 signal(SIGABRT, &signal_handler);
1463 signal(SIGUSR1, &signal_handler);
1464 signal(SIGPIPE, SIG_IGN);
1465
Harald Welte2c869ef2010-08-25 19:43:54 +02001466 if (daemonize) {
1467 rc = osmo_daemonize();
1468 if (rc < 0) {
1469 perror("Error during daemonize");
1470 exit(1);
1471 }
1472 }
1473
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001474 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001475 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001476 sccp_close.cb = sccp_close_unconfirmed;
1477 sccp_close.data = NULL;
1478 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1479
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001480 while (1) {
1481 bsc_select_main(0);
1482 }
1483
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001484 return 0;
1485}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001486
1487/* Close all connections handed out to the USSD module */
1488int bsc_close_ussd_connections(struct bsc_nat *nat)
1489{
1490 struct sccp_connections *con;
1491 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001492 if (con->con_local != NAT_CON_END_USSD)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001493 continue;
1494 if (!con->bsc)
1495 continue;
1496
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001497 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001498 nat_send_rlsd_bsc(con);
1499 }
1500
1501 return 0;
1502}