blob: 75862941a8559e92f8d7cd5568b9427444772a97 [file] [log] [blame]
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
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 Freyther07dfc702010-10-27 10:54:11 +0200198static void nat_send_rlsd_msc(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) {
205 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
206 return;
207 }
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
216 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
217
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800218 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200219}
220
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200221static void nat_send_rlsd_bsc(struct sccp_connections *conn)
222{
223 struct sccp_connection_released *rel;
224 struct msgb *msg;
225
226 msg = msgb_alloc_headroom(4096, 128, "rlsd");
227 if (!msg) {
228 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
229 return;
230 }
231
232 msg->l2h = msgb_put(msg, sizeof(*rel));
233 rel = (struct sccp_connection_released *) msg->l2h;
234 rel->type = SCCP_MSG_TYPE_RLSD;
235 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
236 rel->destination_local_reference = conn->real_ref;
237 rel->source_local_reference = conn->remote_ref;
238
239 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
240}
241
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200242static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200243{
244 struct msgb *msg;
245 struct msgb *sccp;
246
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200247 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200248 if (!msg) {
249 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200250 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200251 }
252
253 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
254 if (!sccp) {
255 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
256 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200257 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200258 }
259
260 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200261 return sccp;
262}
263
264static int nat_send_clrc_bsc(struct sccp_connections *conn)
265{
266 struct msgb *sccp;
267
268 sccp = nat_creat_clrc(conn, 0x20);
269 if (!sccp)
270 return -1;
271 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200272}
273
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800274static void nat_send_rlc(struct bsc_msc_connection *msc_con,
275 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200276 struct sccp_source_reference *dst)
277{
278 struct sccp_connection_release_complete *rlc;
279 struct msgb *msg;
280
281 msg = msgb_alloc_headroom(4096, 128, "rlc");
282 if (!msg) {
283 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
284 return;
285 }
286
287 msg->l2h = msgb_put(msg, sizeof(*rlc));
288 rlc = (struct sccp_connection_release_complete *) msg->l2h;
289 rlc->type = SCCP_MSG_TYPE_RLC;
290 rlc->destination_local_reference = *dst;
291 rlc->source_local_reference = *src;
292
293 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
294
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800295 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200296}
297
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200298static void send_mgcp_reset(struct bsc_connection *bsc)
299{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800300 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200301 "RSIP 1 13@mgw MGCP 1.0\r\n"
302 };
303
304 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
305}
306
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100307/*
308 * Below is the handling of messages coming
309 * from the MSC and need to be forwarded to
310 * a real BSC.
311 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800312static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100313{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800314 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200315 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100316
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800317 msc_con->first_contact = 1;
318 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100319}
320
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800321static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800322{
323 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
324 if (!msg)
325 return;
326
327 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800328 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800329}
330
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100331/*
332 * Currently we are lacking refcounting so we need to copy each message.
333 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800334static 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 +0100335{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100336 struct msgb *msg;
337
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200338 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100339 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
340 return;
341 }
342
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200343 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100344 if (!msg) {
345 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
346 return;
347 }
348
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200349 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100350 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200351
352 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100353}
354
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800355/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800356 * Update the release statistics
357 */
358static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
359{
360 if (!bsc->cfg) {
361 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
362 return;
363 }
364
365 if (filter >= 0) {
366 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
367 return;
368 }
369
370 if (filter == -1)
371 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
372 else if (normal)
373 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
374 else
375 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
376}
377
378/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800379 * Release an established connection. We will have to release it to the BSC
380 * and to the network and we do it the following way.
381 * 1.) Give up on the MSC side
382 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
383 * ignore the RLC... we might complain about it. Other options would
384 * be to send a Release Request, handle the Release Complete..
385 * 1.2) Mark the data structure to be con_local and wait for 2nd
386 *
387 * 2.) Give up on the BSC side
388 * 2.1) Depending on the con type reject the service, or just close it
389 */
390static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
391{
392 struct msgb *rlsd;
393 /* 1. release the network */
394 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
395 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
396 if (!rlsd)
397 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
398 else {
399 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
400 queue_for_msc(con->msc_con, rlsd);
401 }
402 con->con_local = 1;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800403 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800404
405 /* 2. release the BSC side */
406 if (con->con_type == NAT_CON_TYPE_LU) {
407 struct msgb *payload, *udt;
408 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
409
410 if (payload) {
411 gsm0808_prepend_dtap_header(payload, 0);
412 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
413 if (udt)
414 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
415 else
416 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
417
418 msgb_free(payload);
419 } else {
420 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
421 }
422 }
423
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200424 nat_send_clrc_bsc(con);
425
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800426 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
427 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
428 if (!rlsd) {
429 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
430 sccp_connection_destroy(con);
431 return;
432 }
433
434 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
435 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
436}
437
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800438static void bsc_send_con_refuse(struct bsc_connection *bsc,
439 struct bsc_nat_parsed *parsed, int con_type)
440{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800441 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800442 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800443
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800444 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800445 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800446 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800447 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800448 else {
449 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
450 payload = NULL;
451 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800452
453 /*
454 * Some BSCs do not handle the payload inside a SCCP CREF msg
455 * so we will need to:
456 * 1.) Allocate a local connection and mark it as local..
457 * 2.) queue data for downstream.. and the RLC should delete everything
458 */
459 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200460 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800461 struct sccp_connections *con;
462 con = create_sccp_src_ref(bsc, parsed);
463 if (!con)
464 goto send_refuse;
465
466 /* declare it local and assign a unique remote_ref */
467 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
468 con->con_local = 1;
469 con->has_remote_ref = 1;
470 con->remote_ref = con->patched_ref;
471
472 /* 1. create a confirmation */
473 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
474 if (!cc)
475 goto send_refuse;
476
477 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800478 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800479 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
480 if (!udt) {
481 msgb_free(cc);
482 goto send_refuse;
483 }
484
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200485 /* 3. send a Clear Command */
486 clear = nat_creat_clrc(con, 0x20);
487 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800488 msgb_free(cc);
489 msgb_free(udt);
490 goto send_refuse;
491 }
492
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200493 /* 4. send a RLSD */
494 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
495 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
496 if (!rlsd) {
497 msgb_free(cc);
498 msgb_free(udt);
499 msgb_free(clear);
500 goto send_refuse;
501 }
502
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800503 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
504 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200505 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800506 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
507 msgb_free(payload);
508 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800509 }
510
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800511
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800512send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800513 if (payload)
514 msgb_free(payload);
515
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800516 refuse = sccp_create_refuse(parsed->src_local_ref,
517 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800518 if (!refuse) {
519 LOGP(DNAT, LOGL_ERROR,
520 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
521 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
522 return;
523 }
524
525 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
526}
527
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200528/*
529 * Update the auth status. This can be either a CIPHER MODE COMAMND or
530 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
531 */
532static void update_con_authorize(struct sccp_connections *con,
533 struct bsc_nat_parsed *parsed,
534 struct msgb *msg)
535{
536 if (!con)
537 return;
538 if (con->authorized)
539 return;
540
541 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
542 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
543 con->authorized = 1;
544 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
545 uint8_t msg_type;
546 uint32_t len;
547 struct gsm48_hdr *hdr48;
548 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
549 if (!hdr48)
550 return;
551
552 msg_type = hdr48->msg_type & 0xbf;
553 if (hdr48->proto_discr == GSM48_PDISC_MM &&
554 msg_type == GSM48_MT_MM_CM_SERV_ACC)
555 con->authorized = 1;
556 }
557}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800558
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800559static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100560{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800561 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800562 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800563 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200564 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100565
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100566 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800567 parsed = bsc_nat_parse(msg);
568 if (!parsed) {
569 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100570 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800571 }
572
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100573 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800574 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800575
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200576 proto = parsed->ipa_proto;
577
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100578 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200579 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100580 switch (parsed->sccp_type) {
581 case SCCP_MSG_TYPE_UDT:
582 /* forward UDT messages to every BSC */
583 goto send_to_all;
584 break;
585 case SCCP_MSG_TYPE_RLSD:
586 case SCCP_MSG_TYPE_CREF:
587 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200588 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800589 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800590 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200591 counter_inc(nat->stats.sccp.calls);
592
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800593 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800594 struct rate_ctr_group *ctrg;
595 ctrg = con->bsc->cfg->stats.ctrg;
596 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800597 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800598 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
599 } else
600 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
601 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200602 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100603 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800604 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
605 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200606 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800607 break;
608 case SCCP_MSG_TYPE_RLC:
609 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
610 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100611 break;
612 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100613 /* MSC never opens a SCCP connection, fall through */
614 default:
615 goto exit;
616 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200617
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200618 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
619 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
620 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800621 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200622 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800623 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 +0100624 }
625
626 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800627 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100628 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800629 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800630 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100631 return -1;
632 }
633
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200634 update_con_authorize(con, parsed, msg);
635
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200636 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100637 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100638
639send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800640 /*
641 * Filter Paging from the network. We do not want to send a PAGING
642 * Command to every BSC in our network. We will analys the PAGING
643 * message and then send it to the authenticated messages...
644 */
645 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200646 int lac;
647 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800648 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800649 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800650 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200651 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherda340632010-08-06 20:15:40 +0800652 else if (lac != -1)
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200653 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
654 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800655
656 goto exit;
657 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100658 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800659 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100660 if (!bsc->authenticated)
661 continue;
662
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200663 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100664 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800665
666exit:
667 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100668 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100669}
670
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800671static void msc_connection_was_lost(struct bsc_msc_connection *con)
672{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200673 struct bsc_connection *bsc, *tmp;
674
675 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
676 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800677 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200678
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200679 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200680 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800681}
682
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800683static void msc_connection_connected(struct bsc_msc_connection *con)
684{
685 counter_inc(nat->stats.msc.reconn);
686}
687
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200688static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200689{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800690 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200691 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200692 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
693 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
694 0x01, 0x20
695 };
696
697 struct msgb *msg;
698
699 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
700 if (!msg) {
701 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
702 return;
703 }
704
705 msg->l2h = msgb_put(msg, sizeof(reset));
706 memcpy(msg->l2h, reset, msgb_l2len(msg));
707
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800708 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200709
710 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
711}
712
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800713static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100714{
715 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800716 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100717 struct msgb *msg = ipaccess_read_msg(bfd, &error);
718 struct ipaccess_head *hh;
719
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800720 msc_con = (struct bsc_msc_connection *) bfd->data;
721
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100722 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800723 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100724 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800725 else
726 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100727
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800728 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100729 return -1;
730 }
731
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100732 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 +0100733
734 /* handle base message handling */
735 hh = (struct ipaccess_head *) msg->data;
736 ipaccess_rcvmsg_base(msg, bfd);
737
738 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800739 if (hh->proto == IPAC_PROTO_IPACCESS) {
740 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800741 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800742 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800743 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800744 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800745 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100746
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800747 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100748 return 0;
749}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800750
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800751static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
752{
753 int rc;
754 rc = write(bfd->fd, msg->data, msg->len);
755
756 if (rc != msg->len) {
757 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
758 return -1;
759 }
760
761 return rc;
762}
763
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100764/*
765 * Below is the handling of messages coming
766 * from the BSC and need to be forwarded to
767 * a real BSC.
768 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100769
770/*
771 * Remove the connection from the connections list,
772 * remove it from the patching of SCCP header lists
773 * as well. Maybe in the future even close connection..
774 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800775void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100776{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100777 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800778 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100779
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800780 /* stop the timeout timer */
781 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800782 bsc_del_timer(&connection->ping_timeout);
783 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800784
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800785 if (connection->cfg)
786 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
787
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100788 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800789 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100790 if (sccp_patch->bsc != connection)
791 continue;
792
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800793 if (ctr)
794 rate_ctr_inc(ctr);
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800795 if (sccp_patch->has_remote_ref && !sccp_patch->con_local)
Holger Hans Peter Freyther07dfc702010-10-27 10:54:11 +0200796 nat_send_rlsd_msc(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200797 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100798 }
799
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200800 /* close endpoints allocated by this BSC */
801 bsc_mgcp_clear_endpoints_for(connection);
802
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800803 bsc_unregister_fd(&connection->write_queue.bfd);
804 close(connection->write_queue.bfd.fd);
805 write_queue_clear(&connection->write_queue);
806 llist_del(&connection->list_entry);
807
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100808 talloc_free(connection);
809}
810
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800811static void ipaccess_close_bsc(void *data)
812{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200813 struct sockaddr_in sock;
814 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800815 struct bsc_connection *conn = data;
816
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200817
818 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
819 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
820 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800821 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800822}
823
824static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
825{
826 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800827 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200828 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800829
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800830 if (bsc->cfg) {
831 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
832 bsc->write_queue.bfd.fd, bsc->cfg->nr);
833 return;
834 }
835
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800836 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200837 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800838 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800839 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200840 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800841 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800842 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
843 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800844 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200845 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800846 }
847 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200848
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800849 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
850 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800851}
852
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800853static void handle_con_stats(struct sccp_connections *con)
854{
855 struct rate_ctr_group *ctrg;
856 int id = bsc_conn_type_to_ctr(con);
857
858 if (id == -1)
859 return;
860
861 if (!con->bsc || !con->bsc->cfg)
862 return;
863
864 ctrg = con->bsc->cfg->stats.ctrg;
865 rate_ctr_inc(&ctrg->ctr[id]);
866}
867
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100868static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100869{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800870 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800871 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800872 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800873 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800874 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800875 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100876
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800877 /* Parse and filter messages */
878 parsed = bsc_nat_parse(msg);
879 if (!parsed) {
880 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200881 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800882 return -1;
883 }
884
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100885 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800886 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800887
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200888 /*
889 * check authentication after filtering to not reject auth
890 * responses coming from the BSC. We have to make sure that
891 * nothing from the exit path will forward things to the MSC
892 */
893 if (!bsc->authenticated) {
894 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
895 msgb_free(msg);
896 return -1;
897 }
898
899
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100900 /* modify the SCCP entries */
901 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800902 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800903 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100904 switch (parsed->sccp_type) {
905 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800906 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800907 if (filter < 0) {
908 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800909 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800910 }
911
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800912 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100913 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800914 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800915 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800916 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800917 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800918 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800919 if (imsi)
920 con->imsi = talloc_steal(con, imsi);
921 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800922 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800923 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100924 break;
925 case SCCP_MSG_TYPE_RLSD:
926 case SCCP_MSG_TYPE_CREF:
927 case SCCP_MSG_TYPE_DT1:
928 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200929 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800930 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800931 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800932 /* only filter non local connections */
933 if (!con->con_local) {
934 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
935 if (filter < 0) {
936 bsc_stat_reject(filter, bsc, 1);
937 bsc_send_con_release(bsc, con);
938 con = NULL;
939 goto exit2;
940 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800941
942 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200943 if (bsc_check_ussd(con, parsed, msg) == 1)
944 con->con_local = 2;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200945
946 /*
947 * Optionally rewrite setup message. This can
948 * replace the msg and the parsed structure becomes
949 * invalid.
950 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200951 msg = bsc_nat_rewrite_setup(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200952 talloc_free(parsed);
953 parsed = NULL;
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800954 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800955
956 con_bsc = con->bsc;
957 con_msc = con->msc_con;
958 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800959 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800960
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100961 break;
962 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800963 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800964 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800965 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800966 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800967 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800968 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200969 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100970 break;
971 case SCCP_MSG_TYPE_UDT:
972 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800973 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100974 break;
975 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800976 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 +0800977 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100978 goto exit2;
979 break;
980 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100981 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200982 bsc_mgcp_forward(bsc, msg);
983 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800984 } else {
985 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
986 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100987 }
988
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800989 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800990 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 +0800991 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100992 goto exit2;
993 }
994
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +0800995 /* do not forward messages to the MSC */
996 if (con_filter)
997 goto exit2;
998
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800999 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001000 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1001 bsc->cfg->nr,
1002 parsed ? parsed->ipa_proto : -1,
1003 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001004 goto exit2;
1005 }
1006
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001007 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001008 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001009 if (parsed)
1010 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001011 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001012
1013exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001014 /* if we filter out the reset send an ack to the BSC */
1015 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001016 send_reset_ack(bsc);
1017 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001018 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1019 /* do we know who is handling this? */
1020 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1021 struct tlv_parsed tvp;
1022 ipaccess_idtag_parse(&tvp,
1023 (unsigned char *) msg->l2h + 2,
1024 msgb_l2len(msg) - 2);
1025 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1026 ipaccess_auth_bsc(&tvp, bsc);
1027 }
1028
1029 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001030 }
1031
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001032exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001033 if (imsi)
1034 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001035 talloc_free(parsed);
1036 msgb_free(msg);
1037 return -1;
1038
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001039exit3:
1040 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001041 if (imsi)
1042 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001043 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001044 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001045 msgb_free(msg);
1046 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001047}
1048
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001049static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001050{
1051 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001052 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001053 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001054 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001055
1056 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001057 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001058 LOGP(DNAT, LOGL_ERROR,
1059 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1060 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001061 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001062 LOGP(DNAT, LOGL_ERROR,
1063 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
1064 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001065
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001066 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001067 return -1;
1068 }
1069
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001070
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001071 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 +01001072
1073 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001074 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001075
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001076 /* stop the pong timeout */
1077 if (hh->proto == IPAC_PROTO_IPACCESS) {
1078 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001079 bsc_del_timer(&bsc->pong_timeout);
1080 msgb_free(msg);
1081 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001082 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1083 send_pong(bsc);
1084 msgb_free(msg);
1085 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001086 }
1087 }
1088
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001089 /* FIXME: Currently no PONG is sent to the BSC */
1090 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001091 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001092
1093 return 0;
1094}
1095
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001096static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
1097{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001098 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001099 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001100 struct sockaddr_in sa;
1101 socklen_t sa_len = sizeof(sa);
1102
1103 if (!(what & BSC_FD_READ))
1104 return 0;
1105
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001106 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1107 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001108 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001109 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001110 }
1111
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001112 /* count the reconnect */
1113 counter_inc(nat->stats.bsc.reconn);
1114
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001115 /*
1116 * if we are not connected to a msc... just close the socket
1117 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001118 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001119 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001120 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001121 return 0;
1122 }
1123
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001124 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001125 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001126 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001127 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1128
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001129 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001130 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001131 if (rc != 0)
1132 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1133
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001134 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001135 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001136
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001137 /*
1138 *
1139 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001140 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001141 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001142 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001143 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001144 return -1;
1145 }
1146
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001147 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001148 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001149 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001150 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001151 bsc->write_queue.bfd.when = BSC_FD_READ;
1152 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001153 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001154 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001155 talloc_free(bsc);
1156 return -2;
1157 }
1158
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001159 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001160 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001161 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001162 send_id_ack(bsc);
1163 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001164 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001165
1166 /*
1167 * start the hangup timer
1168 */
1169 bsc->id_timeout.data = bsc;
1170 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +08001171 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001172 return 0;
1173}
1174
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001175static void print_usage()
1176{
1177 printf("Usage: bsc_nat\n");
1178}
1179
1180static void print_help()
1181{
1182 printf(" Some useful help...\n");
1183 printf(" -h --help this text\n");
1184 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001185 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001186 printf(" -s --disable-color\n");
1187 printf(" -c --config-file filename The config file to use.\n");
1188 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001189 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001190}
1191
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001192static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001193{
1194 while (1) {
1195 int option_index = 0, c;
1196 static struct option long_options[] = {
1197 {"help", 0, 0, 'h'},
1198 {"debug", 1, 0, 'd'},
1199 {"config-file", 1, 0, 'c'},
1200 {"disable-color", 0, 0, 's'},
1201 {"timestamp", 0, 0, 'T'},
1202 {"msc", 1, 0, 'm'},
1203 {"local", 1, 0, 'l'},
1204 {0, 0, 0, 0}
1205 };
1206
1207 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1208 long_options, &option_index);
1209 if (c == -1)
1210 break;
1211
1212 switch (c) {
1213 case 'h':
1214 print_usage();
1215 print_help();
1216 exit(0);
1217 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001218 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001219 break;
1220 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001221 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001222 break;
1223 case 'c':
1224 config_file = strdup(optarg);
1225 break;
1226 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001227 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001228 break;
1229 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001230 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001231 break;
1232 case 'l':
1233 inet_aton(optarg, &local_addr);
1234 break;
1235 default:
1236 /* ignore */
1237 break;
1238 }
1239 }
1240}
1241
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001242static void signal_handler(int signal)
1243{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001244 switch (signal) {
1245 case SIGABRT:
1246 /* in case of abort, we want to obtain a talloc report
1247 * and then return to the caller, who will abort the process */
1248 case SIGUSR1:
1249 talloc_report_full(tall_bsc_ctx, stderr);
1250 break;
1251 default:
1252 break;
1253 }
1254}
1255
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001256static void sccp_close_unconfirmed(void *_data)
1257{
1258 struct sccp_connections *conn, *tmp1;
1259 struct timespec now;
1260 clock_gettime(CLOCK_MONOTONIC, &now);
1261
1262 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1263 if (conn->has_remote_ref)
1264 continue;
1265
1266 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1267 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1268 continue;
1269
1270 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1271 sccp_src_ref_to_int(&conn->real_ref),
1272 sccp_src_ref_to_int(&conn->patched_ref));
1273 sccp_connection_destroy(conn);
1274 }
1275
1276 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1277}
1278
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001279extern void *tall_msgb_ctx;
1280extern void *tall_ctr_ctx;
1281static void talloc_init_ctx()
1282{
1283 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1284 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1285 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1286}
1287
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001288extern enum node_type bsc_vty_go_parent(struct vty *vty);
1289
1290static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001291 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001292 .version = PACKAGE_VERSION,
1293 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001294 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001295};
1296
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001297int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001298{
Harald Welte2c869ef2010-08-25 19:43:54 +02001299 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001300
Harald Welte2c869ef2010-08-25 19:43:54 +02001301 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001302
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001303 log_init(&log_info);
1304 stderr_target = log_target_create_stderr();
1305 log_add_target(stderr_target);
1306 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001307
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001308 nat = bsc_nat_alloc();
1309 if (!nat) {
1310 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1311 return -4;
1312 }
1313
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001314 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001315 if (!nat->mgcp_cfg) {
1316 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1317 return -5;
1318 }
1319
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001320 vty_info.copyright = openbsc_copyright;
1321 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001322 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001323 bsc_nat_vty_init(nat);
1324
1325
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001326 /* parse options */
1327 local_addr.s_addr = INADDR_ANY;
1328 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001329
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001330 rate_ctr_init(tall_bsc_ctx);
1331
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001332 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001333 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001334 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001335 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1336 return -3;
1337 }
1338
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001339 /* over rule the VTY config */
1340 if (msc_ip)
1341 bsc_nat_set_msc_ip(nat, msc_ip);
1342
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001343 /* seed the PRNG */
1344 srand(time(NULL));
1345
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001346 /*
1347 * Setup the MGCP code..
1348 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001349 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001350 return -4;
1351
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001352 /* connect to the MSC */
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +08001353 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001354 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001355 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001356 exit(1);
1357 }
1358
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001359 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001360 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001361 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1362 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001363 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001364 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001365
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001366 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001367 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001368 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001369 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001370 fprintf(stderr, "Failed to listen for BSC.\n");
1371 exit(1);
1372 }
1373
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001374 rc = bsc_ussd_init(nat);
1375 if (rc != 0) {
1376 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1377 exit(1);
1378 }
1379
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001380 signal(SIGABRT, &signal_handler);
1381 signal(SIGUSR1, &signal_handler);
1382 signal(SIGPIPE, SIG_IGN);
1383
Harald Welte2c869ef2010-08-25 19:43:54 +02001384 if (daemonize) {
1385 rc = osmo_daemonize();
1386 if (rc < 0) {
1387 perror("Error during daemonize");
1388 exit(1);
1389 }
1390 }
1391
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001392 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001393 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001394 sccp_close.cb = sccp_close_unconfirmed;
1395 sccp_close.data = NULL;
1396 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1397
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001398 while (1) {
1399 bsc_select_main(0);
1400 }
1401
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001402 return 0;
1403}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001404
1405/* Close all connections handed out to the USSD module */
1406int bsc_close_ussd_connections(struct bsc_nat *nat)
1407{
1408 struct sccp_connections *con;
1409 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
1410 if (con->con_local != 2)
1411 continue;
1412 if (!con->bsc)
1413 continue;
1414
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001415 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001416 nat_send_rlsd_bsc(con);
1417 }
1418
1419 return 0;
1420}