blob: 445f45915a86bce8afdfc2ea56a6ee3b620d71cc [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>
Daniel Willmanna86bc392011-02-18 14:32:56 +010042#include <openbsc/control_cmd.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +020045#include <openbsc/socket.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080046#include <openbsc/vty.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080047
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020048#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010049#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080050
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020051#include <osmocom/gsm/gsm0808.h>
Harald Welted36ff762011-03-23 18:26:56 +010052#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080053
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080054#include <osmocom/vty/telnet_interface.h>
55#include <osmocom/vty/vty.h>
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +010056#include <osmocom/vty/logging.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080057
Harald Welted5db12c2010-08-03 15:11:51 +020058#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080059
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080060#include "../../bscconfig.h"
61
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080062#define SCCP_CLOSE_TIME 20
63#define SCCP_CLOSE_TIME_TIMEOUT 19
64
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;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020067static struct osmo_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080068static const char *msc_ip = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020069static struct osmo_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);
Daniel Willmanna86bc392011-02-18 14:32:56 +010082static void bsc_del_pending(struct bsc_cmd_list *pending);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080083
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080084struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
85{
86 struct bsc_config *conf;
87
88 llist_for_each_entry(conf, &nat->bsc_configs, entry)
89 if (conf->nr == num)
90 return conf;
91
92 return NULL;
93}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010094
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080095static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
96{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +080097 if (!con) {
98 LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
99 msgb_free(msg);
100 return;
101 }
102
103
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200104 if (osmo_wqueue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800105 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
106 msgb_free(msg);
107 }
108}
109
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100110static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100111{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800112 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100113 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
114 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
115 0x00, 0x01, 0x31,
116 };
117
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200118 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100119}
120
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800121static void send_ping(struct bsc_connection *bsc)
122{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800123 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800124 IPAC_MSGT_PING,
125 };
126
127 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
128}
129
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800130static void send_pong(struct bsc_connection *bsc)
131{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800132 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800133 IPAC_MSGT_PONG,
134 };
135
136 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
137}
138
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800139static void bsc_pong_timeout(void *_bsc)
140{
141 struct bsc_connection *bsc = _bsc;
142
143 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
144 bsc_close_connection(bsc);
145}
146
147static void bsc_ping_timeout(void *_bsc)
148{
149 struct bsc_connection *bsc = _bsc;
150
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800151 if (bsc->nat->ping_timeout < 0)
152 return;
153
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800154 send_ping(bsc);
155
156 /* send another ping in 20 seconds */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200157 osmo_timer_schedule(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800158
159 /* also start a pong timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200160 osmo_timer_schedule(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800161}
162
163static void start_ping_pong(struct bsc_connection *bsc)
164{
165 bsc->pong_timeout.data = bsc;
166 bsc->pong_timeout.cb = bsc_pong_timeout;
167 bsc->ping_timeout.data = bsc;
168 bsc->ping_timeout.cb = bsc_ping_timeout;
169
170 bsc_ping_timeout(bsc);
171}
172
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100173static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100174{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800175 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200176 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100177 };
178
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200179 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100180}
181
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100182static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100183{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800184 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200185 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100186 0x01, IPAC_IDTAG_UNIT,
187 0x01, IPAC_IDTAG_MACADDR,
188 0x01, IPAC_IDTAG_LOCATION1,
189 0x01, IPAC_IDTAG_LOCATION2,
190 0x01, IPAC_IDTAG_EQUIPVERS,
191 0x01, IPAC_IDTAG_SWVERSION,
192 0x01, IPAC_IDTAG_UNITNAME,
193 0x01, IPAC_IDTAG_SERNR,
194 };
195
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200196 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100197}
198
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200199static struct msgb *nat_create_rlsd(struct sccp_connections *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200200{
201 struct sccp_connection_released *rel;
202 struct msgb *msg;
203
204 msg = msgb_alloc_headroom(4096, 128, "rlsd");
205 if (!msg) {
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200206 LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n");
207 return NULL;
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200208 }
209
210 msg->l2h = msgb_put(msg, sizeof(*rel));
211 rel = (struct sccp_connection_released *) msg->l2h;
212 rel->type = SCCP_MSG_TYPE_RLSD;
213 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
214 rel->destination_local_reference = conn->remote_ref;
215 rel->source_local_reference = conn->patched_ref;
216
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200217 return msg;
218}
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200219
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200220static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct sccp_connections *conn)
221{
222 struct msgb *msg;
223
224 if (!nat->ussd_con)
225 return;
226
227 msg = nat_create_rlsd(conn);
228 if (!msg)
229 return;
230
231 bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP);
232}
233
234static void nat_send_rlsd_msc(struct sccp_connections *conn)
235{
236 struct msgb *msg;
237
238 msg = nat_create_rlsd(conn);
239 if (!msg)
240 return;
241
242 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800243 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200244}
245
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200246static void nat_send_rlsd_bsc(struct sccp_connections *conn)
247{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200248 struct msgb *msg;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200249 struct sccp_connection_released *rel;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200250
251 msg = msgb_alloc_headroom(4096, 128, "rlsd");
252 if (!msg) {
253 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
254 return;
255 }
256
257 msg->l2h = msgb_put(msg, sizeof(*rel));
258 rel = (struct sccp_connection_released *) msg->l2h;
259 rel->type = SCCP_MSG_TYPE_RLSD;
260 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
261 rel->destination_local_reference = conn->real_ref;
262 rel->source_local_reference = conn->remote_ref;
263
264 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
265}
266
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200267static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200268{
269 struct msgb *msg;
270 struct msgb *sccp;
271
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200272 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200273 if (!msg) {
274 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200275 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200276 }
277
278 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
279 if (!sccp) {
280 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
281 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200282 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200283 }
284
285 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200286 return sccp;
287}
288
289static int nat_send_clrc_bsc(struct sccp_connections *conn)
290{
291 struct msgb *sccp;
292
293 sccp = nat_creat_clrc(conn, 0x20);
294 if (!sccp)
295 return -1;
296 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200297}
298
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800299static void nat_send_rlc(struct bsc_msc_connection *msc_con,
300 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200301 struct sccp_source_reference *dst)
302{
303 struct sccp_connection_release_complete *rlc;
304 struct msgb *msg;
305
306 msg = msgb_alloc_headroom(4096, 128, "rlc");
307 if (!msg) {
308 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
309 return;
310 }
311
312 msg->l2h = msgb_put(msg, sizeof(*rlc));
313 rlc = (struct sccp_connection_release_complete *) msg->l2h;
314 rlc->type = SCCP_MSG_TYPE_RLC;
315 rlc->destination_local_reference = *dst;
316 rlc->source_local_reference = *src;
317
318 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
319
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800320 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200321}
322
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200323static void send_mgcp_reset(struct bsc_connection *bsc)
324{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800325 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200326 "RSIP 1 13@mgw MGCP 1.0\r\n"
327 };
328
329 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
330}
331
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100332/*
333 * Below is the handling of messages coming
334 * from the MSC and need to be forwarded to
335 * a real BSC.
336 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800337static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100338{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800339 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200340 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100341
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800342 msc_con->first_contact = 1;
343 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100344}
345
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800346static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800347{
348 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
349 if (!msg)
350 return;
351
352 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800353 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800354}
355
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100356/*
357 * Currently we are lacking refcounting so we need to copy each message.
358 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800359static 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 +0100360{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100361 struct msgb *msg;
362
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200363 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100364 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
365 return;
366 }
367
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200368 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100369 if (!msg) {
370 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
371 return;
372 }
373
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200374 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100375 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200376
377 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100378}
379
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800380/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800381 * Update the release statistics
382 */
383static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
384{
385 if (!bsc->cfg) {
386 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
387 return;
388 }
389
390 if (filter >= 0) {
391 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
392 return;
393 }
394
395 if (filter == -1)
396 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
397 else if (normal)
398 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
399 else
400 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
401}
402
403/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800404 * Release an established connection. We will have to release it to the BSC
405 * and to the network and we do it the following way.
406 * 1.) Give up on the MSC side
407 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
408 * ignore the RLC... we might complain about it. Other options would
409 * be to send a Release Request, handle the Release Complete..
410 * 1.2) Mark the data structure to be con_local and wait for 2nd
411 *
412 * 2.) Give up on the BSC side
413 * 2.1) Depending on the con type reject the service, or just close it
414 */
415static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
416{
417 struct msgb *rlsd;
418 /* 1. release the network */
419 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
420 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
421 if (!rlsd)
422 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
423 else {
424 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
425 queue_for_msc(con->msc_con, rlsd);
426 }
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200427 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800428 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800429
430 /* 2. release the BSC side */
431 if (con->con_type == NAT_CON_TYPE_LU) {
432 struct msgb *payload, *udt;
433 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
434
435 if (payload) {
436 gsm0808_prepend_dtap_header(payload, 0);
437 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
438 if (udt)
439 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
440 else
441 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
442
443 msgb_free(payload);
444 } else {
445 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
446 }
447 }
448
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200449 nat_send_clrc_bsc(con);
450
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800451 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
452 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
453 if (!rlsd) {
454 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
455 sccp_connection_destroy(con);
456 return;
457 }
458
459 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
460 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
461}
462
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800463static void bsc_send_con_refuse(struct bsc_connection *bsc,
464 struct bsc_nat_parsed *parsed, int con_type)
465{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800466 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800467 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800468
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800469 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800470 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800471 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800472 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800473 else {
474 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
475 payload = NULL;
476 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800477
478 /*
479 * Some BSCs do not handle the payload inside a SCCP CREF msg
480 * so we will need to:
481 * 1.) Allocate a local connection and mark it as local..
482 * 2.) queue data for downstream.. and the RLC should delete everything
483 */
484 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200485 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800486 struct sccp_connections *con;
487 con = create_sccp_src_ref(bsc, parsed);
488 if (!con)
489 goto send_refuse;
490
491 /* declare it local and assign a unique remote_ref */
492 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +0200493 con->con_local = NAT_CON_END_LOCAL;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800494 con->has_remote_ref = 1;
495 con->remote_ref = con->patched_ref;
496
497 /* 1. create a confirmation */
498 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
499 if (!cc)
500 goto send_refuse;
501
502 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800503 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800504 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
505 if (!udt) {
506 msgb_free(cc);
507 goto send_refuse;
508 }
509
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200510 /* 3. send a Clear Command */
511 clear = nat_creat_clrc(con, 0x20);
512 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800513 msgb_free(cc);
514 msgb_free(udt);
515 goto send_refuse;
516 }
517
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200518 /* 4. send a RLSD */
519 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
520 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
521 if (!rlsd) {
522 msgb_free(cc);
523 msgb_free(udt);
524 msgb_free(clear);
525 goto send_refuse;
526 }
527
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800528 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
529 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200530 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800531 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
532 msgb_free(payload);
533 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800534 }
535
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800536
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800537send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800538 if (payload)
539 msgb_free(payload);
540
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800541 refuse = sccp_create_refuse(parsed->src_local_ref,
542 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800543 if (!refuse) {
544 LOGP(DNAT, LOGL_ERROR,
545 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
546 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
547 return;
548 }
549
550 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
551}
552
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200553static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg)
554{
555 if (bsc->cfg->forbid_paging) {
556 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
557 return;
558 }
559
560 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP);
561}
562
563static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg)
564{
565 struct bsc_connection *bsc;
566 const uint8_t *paging_start;
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200567 int paging_length, i, ret;
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200568
Holger Hans Peter Freyther0dc36f12011-05-02 20:03:17 +0200569 ret = bsc_nat_find_paging(msg, &paging_start, &paging_length);
570 if (ret != 0) {
571 LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200572 return;
573 }
574
575 /* This is quite expensive now */
576 for (i = 0; i < paging_length; i += 2) {
577 unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]);
578 unsigned int paged = 0;
579 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
580 if (!bsc->cfg)
581 continue;
582 if (!bsc->authenticated)
583 continue;
584 if (!bsc_config_handles_lac(bsc->cfg, _lac))
585 continue;
586 bsc_nat_send_paging(bsc, msg);
587 paged += 1;
588 }
589
590 /* highlight a possible config issue */
591 if (paged == 0)
592 LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac);
593
594 }
595}
596
597
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200598/*
599 * Update the auth status. This can be either a CIPHER MODE COMAMND or
600 * a CM Serivce Accept. Maybe also LU Accept or such in the future.
601 */
602static void update_con_authorize(struct sccp_connections *con,
603 struct bsc_nat_parsed *parsed,
604 struct msgb *msg)
605{
606 if (!con)
607 return;
608 if (con->authorized)
609 return;
610
611 if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT &&
612 parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) {
613 con->authorized = 1;
614 } else if (parsed->bssap == BSSAP_MSG_DTAP) {
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200615 uint8_t msg_type, proto;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200616 uint32_t len;
617 struct gsm48_hdr *hdr48;
618 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
619 if (!hdr48)
620 return;
621
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200622 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200623 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200624 if (proto == GSM48_PDISC_MM &&
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200625 msg_type == GSM48_MT_MM_CM_SERV_ACC)
626 con->authorized = 1;
627 }
628}
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800629
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800630static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100631{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800632 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800633 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800634 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200635 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100636
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100637 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800638 parsed = bsc_nat_parse(msg);
639 if (!parsed) {
640 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100641 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800642 }
643
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100644 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800645 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800646
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200647 proto = parsed->ipa_proto;
648
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100649 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200650 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100651 switch (parsed->sccp_type) {
652 case SCCP_MSG_TYPE_UDT:
653 /* forward UDT messages to every BSC */
654 goto send_to_all;
655 break;
656 case SCCP_MSG_TYPE_RLSD:
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200657 if (con && con->con_local == NAT_CON_END_USSD) {
658 LOGP(DNAT, LOGL_NOTICE, "RLSD for a USSD connection. Ignoring.\n");
659 con = NULL;
660 }
661 /* fall through */
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100662 case SCCP_MSG_TYPE_CREF:
663 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200664 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800665 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800666 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200667 osmo_counter_inc(nat->stats.sccp.calls);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200668
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800669 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800670 struct rate_ctr_group *ctrg;
671 ctrg = con->bsc->cfg->stats.ctrg;
672 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800673 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800674 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
675 } else
676 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
Holger Hans Peter Freyther0b03f162011-04-21 17:13:51 +0200677 } else if (con && con->con_local == NAT_CON_END_USSD &&
678 parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) {
679 LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n");
680 con = NULL;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800681 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200682 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100683 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800684 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
685 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200686 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800687 break;
688 case SCCP_MSG_TYPE_RLC:
689 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
690 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100691 break;
692 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100693 /* MSC never opens a SCCP connection, fall through */
694 default:
695 goto exit;
696 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200697
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200698 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
699 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
700 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800701 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200702 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800703 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 +0100704 }
705
706 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800707 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100708 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800709 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800710 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100711 return -1;
712 }
713
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200714 update_con_authorize(con, parsed, msg);
715
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200716 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100717 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100718
719send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800720 /*
721 * Filter Paging from the network. We do not want to send a PAGING
722 * Command to every BSC in our network. We will analys the PAGING
723 * message and then send it to the authenticated messages...
724 */
725 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200726 bsc_nat_handle_paging(nat, msg);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800727 goto exit;
728 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100729 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800730 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100731 if (!bsc->authenticated)
732 continue;
733
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200734 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100735 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800736
737exit:
738 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100739 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100740}
741
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800742static void msc_connection_was_lost(struct bsc_msc_connection *con)
743{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200744 struct bsc_connection *bsc, *tmp;
745
746 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
747 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800748 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200749
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200750 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200751 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800752}
753
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800754static void msc_connection_connected(struct bsc_msc_connection *con)
755{
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200756 osmo_counter_inc(nat->stats.msc.reconn);
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800757}
758
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200759static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200760{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800761 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200762 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200763 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
764 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
765 0x01, 0x20
766 };
767
768 struct msgb *msg;
769
770 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
771 if (!msg) {
772 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
773 return;
774 }
775
776 msg->l2h = msgb_put(msg, sizeof(reset));
777 memcpy(msg->l2h, reset, msgb_l2len(msg));
778
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800779 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200780
781 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
782}
783
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200784static int ipaccess_msc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100785{
786 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800787 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100788 struct msgb *msg = ipaccess_read_msg(bfd, &error);
789 struct ipaccess_head *hh;
790
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800791 msc_con = (struct bsc_msc_connection *) bfd->data;
792
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100793 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800794 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100795 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800796 else
797 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100798
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800799 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100800 return -1;
801 }
802
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200803 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100804
805 /* handle base message handling */
806 hh = (struct ipaccess_head *) msg->data;
807 ipaccess_rcvmsg_base(msg, bfd);
808
809 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800810 if (hh->proto == IPAC_PROTO_IPACCESS) {
811 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800812 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800813 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800814 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800815 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800816 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100817
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800818 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100819 return 0;
820}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800821
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200822static int ipaccess_msc_write_cb(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800823{
824 int rc;
825 rc = write(bfd->fd, msg->data, msg->len);
826
827 if (rc != msg->len) {
828 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
829 return -1;
830 }
831
832 return rc;
833}
834
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100835/*
836 * Below is the handling of messages coming
837 * from the BSC and need to be forwarded to
838 * a real BSC.
839 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100840
841/*
842 * Remove the connection from the connections list,
843 * remove it from the patching of SCCP header lists
844 * as well. Maybe in the future even close connection..
845 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800846void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100847{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100848 struct sccp_connections *sccp_patch, *tmp;
Daniel Willmanna86bc392011-02-18 14:32:56 +0100849 struct bsc_cmd_list *cmd_entry, *cmd_tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800850 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100851
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800852 /* stop the timeout timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200853 osmo_timer_del(&connection->id_timeout);
854 osmo_timer_del(&connection->ping_timeout);
855 osmo_timer_del(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800856
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800857 if (connection->cfg)
858 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
859
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100860 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800861 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100862 if (sccp_patch->bsc != connection)
863 continue;
864
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800865 if (ctr)
866 rate_ctr_inc(ctr);
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200867 if (sccp_patch->has_remote_ref) {
868 if (sccp_patch->con_local == NAT_CON_END_MSC)
869 nat_send_rlsd_msc(sccp_patch);
870 else if (sccp_patch->con_local == NAT_CON_END_USSD)
871 nat_send_rlsd_ussd(nat, sccp_patch);
872 }
873
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200874 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100875 }
876
Daniel Willmanna86bc392011-02-18 14:32:56 +0100877 /* Reply to all outstanding commands */
878 llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) {
879 cmd_entry->cmd->type = CTRL_TYPE_ERROR;
880 cmd_entry->cmd->reply = "BSC closed the connection";
881 ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd);
882 bsc_del_pending(cmd_entry);
883 }
884
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200885 /* close endpoints allocated by this BSC */
886 bsc_mgcp_clear_endpoints_for(connection);
887
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200888 osmo_fd_unregister(&connection->write_queue.bfd);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800889 close(connection->write_queue.bfd.fd);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200890 osmo_wqueue_clear(&connection->write_queue);
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800891 llist_del(&connection->list_entry);
892
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100893 talloc_free(connection);
894}
895
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200896static void bsc_maybe_close(struct bsc_connection *bsc)
897{
898 struct sccp_connections *sccp;
899 if (!bsc->nat->blocked)
900 return;
901
902 /* are there any connections left */
903 llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry)
904 if (sccp->bsc == bsc)
905 return;
906
907 /* nothing left, close the BSC */
908 LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n",
909 bsc->cfg ? bsc->cfg->nr : -1);
910 bsc_close_connection(bsc);
911}
912
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800913static void ipaccess_close_bsc(void *data)
914{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200915 struct sockaddr_in sock;
916 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800917 struct bsc_connection *conn = data;
918
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200919
920 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
921 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
922 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800923 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800924}
925
926static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
927{
928 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800929 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200930 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800931
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800932 if (bsc->cfg) {
933 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
934 bsc->write_queue.bfd.fd, bsc->cfg->nr);
935 return;
936 }
937
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800938 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200939 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800940 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800941 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200942 bsc->cfg = conf;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200943 osmo_timer_del(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800944 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
945 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800946 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200947 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800948 }
949 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200950
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800951 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
952 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800953}
954
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800955static void handle_con_stats(struct sccp_connections *con)
956{
957 struct rate_ctr_group *ctrg;
958 int id = bsc_conn_type_to_ctr(con);
959
960 if (id == -1)
961 return;
962
963 if (!con->bsc || !con->bsc->cfg)
964 return;
965
966 ctrg = con->bsc->cfg->stats.ctrg;
967 rate_ctr_inc(&ctrg->ctr[id]);
968}
969
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100970static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100971{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800972 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800973 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800974 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800975 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800976 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800977 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100978
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800979 /* Parse and filter messages */
980 parsed = bsc_nat_parse(msg);
981 if (!parsed) {
982 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200983 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800984 return -1;
985 }
986
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100987 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800988 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800989
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200990 /*
991 * check authentication after filtering to not reject auth
992 * responses coming from the BSC. We have to make sure that
993 * nothing from the exit path will forward things to the MSC
994 */
995 if (!bsc->authenticated) {
996 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
997 msgb_free(msg);
998 return -1;
999 }
1000
1001
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001002 /* modify the SCCP entries */
1003 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +08001004 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001005 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001006 switch (parsed->sccp_type) {
1007 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001008 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001009 if (filter < 0) {
1010 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001011 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +08001012 }
1013
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +08001014 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001015 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001016 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +08001017 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001018 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +08001019 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +08001020 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001021 if (imsi)
1022 con->imsi = talloc_steal(con, imsi);
1023 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001024 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +08001025 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001026 break;
1027 case SCCP_MSG_TYPE_RLSD:
1028 case SCCP_MSG_TYPE_CREF:
1029 case SCCP_MSG_TYPE_DT1:
1030 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +02001031 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001032 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001033 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001034 /* only filter non local connections */
1035 if (!con->con_local) {
1036 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
1037 if (filter < 0) {
1038 bsc_stat_reject(filter, bsc, 1);
1039 bsc_send_con_release(bsc, con);
1040 con = NULL;
1041 goto exit2;
1042 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001043
1044 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +02001045 if (bsc_check_ussd(con, parsed, msg) == 1)
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001046 con->con_local = NAT_CON_END_USSD;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001047
1048 /*
1049 * Optionally rewrite setup message. This can
1050 * replace the msg and the parsed structure becomes
1051 * invalid.
1052 */
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001053 msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001054 talloc_free(parsed);
1055 parsed = NULL;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +02001056 } else if (con->con_local == NAT_CON_END_USSD) {
1057 bsc_check_ussd(con, parsed, msg);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +08001058 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +08001059
1060 con_bsc = con->bsc;
1061 con_msc = con->msc_con;
1062 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001063 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001064
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001065 break;
1066 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +08001067 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001068 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001069 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001070 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +08001071 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +08001072 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +02001073 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001074 bsc_maybe_close(bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001075 break;
1076 case SCCP_MSG_TYPE_UDT:
1077 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001078 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001079 break;
1080 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +08001081 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 +08001082 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001083 goto exit2;
1084 break;
1085 }
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +01001086 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001087 bsc_mgcp_forward(bsc, msg);
1088 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +08001089 } else {
1090 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
1091 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001092 }
1093
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001094 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +08001095 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 +08001096 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +01001097 goto exit2;
1098 }
1099
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +08001100 /* do not forward messages to the MSC */
1101 if (con_filter)
1102 goto exit2;
1103
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001104 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +08001105 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
1106 bsc->cfg->nr,
1107 parsed ? parsed->ipa_proto : -1,
1108 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001109 goto exit2;
1110 }
1111
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001112 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +08001113 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +02001114 if (parsed)
1115 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001116 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001117
1118exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001119 /* if we filter out the reset send an ack to the BSC */
1120 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001121 send_reset_ack(bsc);
1122 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001123 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
1124 /* do we know who is handling this? */
1125 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1126 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001127 int ret;
1128 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001129 (unsigned char *) msg->l2h + 2,
1130 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +02001131 if (ret < 0) {
1132 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
1133 "message with malformed TLVs\n");
1134 return ret;
1135 }
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001136 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1137 ipaccess_auth_bsc(&tvp, bsc);
1138 }
1139
1140 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001141 }
1142
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001143exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001144 if (imsi)
1145 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001146 talloc_free(parsed);
1147 msgb_free(msg);
1148 return -1;
1149
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001150exit3:
1151 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001152 if (imsi)
1153 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001154 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001155 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001156 msgb_free(msg);
1157 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001158}
1159
Daniel Willmanna86bc392011-02-18 14:32:56 +01001160static struct bsc_cmd_list *bsc_get_pending(struct bsc_connection *bsc, char *id_str)
1161{
1162 struct bsc_cmd_list *cmd_entry;
1163 int id = atoi(id_str);
1164 if (id == 0)
1165 return NULL;
1166
1167 llist_for_each_entry(cmd_entry, &bsc->cmd_pending, list_entry) {
1168 if (cmd_entry->nat_id == id) {
1169 return cmd_entry;
1170 }
1171 }
1172 return NULL;
1173}
1174
1175static void bsc_del_pending(struct bsc_cmd_list *pending)
1176{
1177 llist_del(&pending->list_entry);
1178 osmo_timer_del(&pending->timeout);
1179 talloc_free(pending->cmd);
1180 talloc_free(pending);
1181}
1182
Harald Weltef8e49dd2011-07-13 14:43:37 +02001183
1184static int handle_ctrlif_msg(struct bsc_connection *bsc, struct msgb *msg)
1185{
1186 struct ctrl_cmd *cmd;
1187 struct bsc_cmd_list *pending;
1188 char *var, *id;
1189
1190 cmd = ctrl_cmd_parse(bsc, msg);
1191 msgb_free(msg);
1192
1193 if (!cmd) {
1194 cmd = talloc_zero(bsc, struct ctrl_cmd);
1195 if (!cmd) {
1196 LOGP(DNAT, LOGL_ERROR, "OOM!\n");
Harald Welte65520472011-07-13 14:45:21 +02001197 return -ENOMEM;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001198 }
1199 cmd->type = CTRL_TYPE_ERROR;
1200 cmd->id = "err";
1201 cmd->reply = "Failed to parse command.";
Harald Welte65520472011-07-13 14:45:21 +02001202 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001203 }
1204
Harald Weltef071e162011-07-13 14:47:34 +02001205 if (bsc->cfg && !llist_empty(&bsc->cfg->lac_list)) {
1206 if (cmd->variable) {
Harald Welte7017fa72011-07-13 14:50:46 +02001207 struct bsc_lac_entry *bsc_lac;
1208 bsc_lac = llist_entry(bsc->cfg->lac_list.next,
1209 struct bsc_lac_entry, entry);
1210 var = talloc_asprintf(cmd, "bsc.%i.%s", bsc_lac->lac,
1211 cmd->variable);
Harald Weltef071e162011-07-13 14:47:34 +02001212 if (!var) {
1213 cmd->type = CTRL_TYPE_ERROR;
1214 cmd->reply = "OOM";
1215 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001216 }
Harald Weltef071e162011-07-13 14:47:34 +02001217 talloc_free(cmd->variable);
1218 cmd->variable = var;
1219 }
Harald Weltef8e49dd2011-07-13 14:43:37 +02001220
Harald Weltef071e162011-07-13 14:47:34 +02001221 /* Find the pending command */
1222 pending = bsc_get_pending(bsc, cmd->id);
1223 if (pending) {
1224 id = talloc_strdup(cmd, pending->cmd->id);
1225 if (!id) {
1226 cmd->type = CTRL_TYPE_ERROR;
1227 cmd->reply = "OOM";
1228 goto err;
1229 }
1230 cmd->id = id;
1231 ctrl_cmd_send(&pending->ccon->write_queue, cmd);
1232 bsc_del_pending(pending);
1233 } else {
1234 /* We need to handle TRAPS here */
Harald Welte7017fa72011-07-13 14:50:46 +02001235 if ((cmd->type != CTRL_TYPE_ERROR) &&
1236 (cmd->type != CTRL_TYPE_TRAP)) {
1237 LOGP(DNAT, LOGL_NOTICE, "Got control message "
1238 "from BSC without pending entry\n");
Harald Weltef071e162011-07-13 14:47:34 +02001239 cmd->type = CTRL_TYPE_ERROR;
1240 cmd->reply = "No request outstanding";
1241 goto err;
Harald Weltef8e49dd2011-07-13 14:43:37 +02001242 }
1243 }
1244 }
1245 talloc_free(cmd);
1246 return 0;
1247err:
1248 ctrl_cmd_send(&bsc->write_queue, cmd);
1249 talloc_free(cmd);
1250 return 0;
1251}
1252
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001253static int ipaccess_bsc_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001254{
1255 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001256 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001257 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001258 struct ipaccess_head *hh;
Daniel Willmanna86bc392011-02-18 14:32:56 +01001259 struct ipaccess_head_ext *hh_ext;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001260
1261 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001262 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001263 LOGP(DNAT, LOGL_ERROR,
1264 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1265 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001266 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001267 LOGP(DNAT, LOGL_ERROR,
1268 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
1269 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001270
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001271 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001272 return -1;
1273 }
1274
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001275
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001276 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001277
1278 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001279 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001280
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001281 /* stop the pong timeout */
1282 if (hh->proto == IPAC_PROTO_IPACCESS) {
1283 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001284 osmo_timer_del(&bsc->pong_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001285 msgb_free(msg);
1286 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001287 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1288 send_pong(bsc);
1289 msgb_free(msg);
1290 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001291 }
Daniel Willmanna86bc392011-02-18 14:32:56 +01001292 /* Message contains the ipaccess_head_ext header, investigate further */
1293 } else if (hh->proto == IPAC_PROTO_OSMO &&
1294 msg->len > sizeof(*hh) + sizeof(*hh_ext)) {
1295
1296 hh_ext = (struct ipaccess_head_ext *) hh->data;
1297 /* l2h is where the actual command data is expected */
1298 msg->l2h = hh_ext->data;
1299
Harald Weltef8e49dd2011-07-13 14:43:37 +02001300 if (hh_ext->proto == IPAC_PROTO_EXT_CTRL)
1301 return handle_ctrlif_msg(bsc, msg);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001302 }
1303
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001304 /* FIXME: Currently no PONG is sent to the BSC */
1305 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001306 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001307
1308 return 0;
1309}
1310
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001311static int ipaccess_listen_bsc_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001312{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001313 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001314 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001315 struct sockaddr_in sa;
1316 socklen_t sa_len = sizeof(sa);
1317
1318 if (!(what & BSC_FD_READ))
1319 return 0;
1320
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001321 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1322 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001323 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001324 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001325 }
1326
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001327 /* count the reconnect */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001328 osmo_counter_inc(nat->stats.bsc.reconn);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001329
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001330 /*
1331 * if we are not connected to a msc... just close the socket
1332 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001333 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001334 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001335 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001336 return 0;
1337 }
1338
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001339 if (nat->blocked) {
1340 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n");
1341 close(fd);
1342 return 0;
1343 }
1344
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001345 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001346 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001347 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001348 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1349
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001350 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001351 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001352 if (rc != 0)
1353 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1354
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001355 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001356 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001357
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001358 /*
1359 *
1360 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001361 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001362 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001363 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001364 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001365 return -1;
1366 }
1367
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001368 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001369 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001370 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001371 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001372 bsc->write_queue.bfd.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001373 if (osmo_fd_register(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001374 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001375 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001376 talloc_free(bsc);
1377 return -2;
1378 }
1379
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001380 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001381 fd, inet_ntoa(sa.sin_addr));
Daniel Willmanna86bc392011-02-18 14:32:56 +01001382
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001383 llist_add(&bsc->list_entry, &nat->bsc_connections);
Daniel Willmanna86bc392011-02-18 14:32:56 +01001384 bsc->last_id = 0;
1385
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001386 send_id_ack(bsc);
1387 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001388 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001389
1390 /*
1391 * start the hangup timer
1392 */
1393 bsc->id_timeout.data = bsc;
1394 bsc->id_timeout.cb = ipaccess_close_bsc;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001395 osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001396 return 0;
1397}
1398
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001399static void print_usage()
1400{
1401 printf("Usage: bsc_nat\n");
1402}
1403
1404static void print_help()
1405{
1406 printf(" Some useful help...\n");
1407 printf(" -h --help this text\n");
1408 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001409 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001410 printf(" -s --disable-color\n");
1411 printf(" -c --config-file filename The config file to use.\n");
1412 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001413 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001414}
1415
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001416static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001417{
1418 while (1) {
1419 int option_index = 0, c;
1420 static struct option long_options[] = {
1421 {"help", 0, 0, 'h'},
1422 {"debug", 1, 0, 'd'},
1423 {"config-file", 1, 0, 'c'},
1424 {"disable-color", 0, 0, 's'},
1425 {"timestamp", 0, 0, 'T'},
1426 {"msc", 1, 0, 'm'},
1427 {"local", 1, 0, 'l'},
1428 {0, 0, 0, 0}
1429 };
1430
1431 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1432 long_options, &option_index);
1433 if (c == -1)
1434 break;
1435
1436 switch (c) {
1437 case 'h':
1438 print_usage();
1439 print_help();
1440 exit(0);
1441 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001442 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001443 break;
1444 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001445 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001446 break;
1447 case 'c':
1448 config_file = strdup(optarg);
1449 break;
1450 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001451 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001452 break;
1453 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001454 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001455 break;
1456 case 'l':
1457 inet_aton(optarg, &local_addr);
1458 break;
1459 default:
1460 /* ignore */
1461 break;
1462 }
1463 }
1464}
1465
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001466static void signal_handler(int signal)
1467{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001468 switch (signal) {
1469 case SIGABRT:
1470 /* in case of abort, we want to obtain a talloc report
1471 * and then return to the caller, who will abort the process */
1472 case SIGUSR1:
1473 talloc_report_full(tall_bsc_ctx, stderr);
1474 break;
1475 default:
1476 break;
1477 }
1478}
1479
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001480static void sccp_close_unconfirmed(void *_data)
1481{
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001482 int destroyed = 0;
1483 struct bsc_connection *bsc, *bsc_tmp;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001484 struct sccp_connections *conn, *tmp1;
1485 struct timespec now;
1486 clock_gettime(CLOCK_MONOTONIC, &now);
1487
1488 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1489 if (conn->has_remote_ref)
1490 continue;
1491
1492 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1493 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1494 continue;
1495
1496 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1497 sccp_src_ref_to_int(&conn->real_ref),
1498 sccp_src_ref_to_int(&conn->patched_ref));
1499 sccp_connection_destroy(conn);
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001500 destroyed = 1;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001501 }
1502
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +02001503 if (!destroyed)
1504 goto out;
1505
1506 /* now close out any BSC */
1507 llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry)
1508 bsc_maybe_close(bsc);
1509
1510out:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001511 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001512}
1513
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001514extern void *tall_msgb_ctx;
1515extern void *tall_ctr_ctx;
1516static void talloc_init_ctx()
1517{
1518 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1519 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1520 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1521}
1522
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001523extern enum node_type bsc_vty_go_parent(struct vty *vty);
1524
1525static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +01001526 .name = "OsmoBSCNAT",
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001527 .version = PACKAGE_VERSION,
1528 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001529 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001530};
1531
Daniel Willmanna86bc392011-02-18 14:32:56 +01001532static int get_next_free_bsc_id(struct bsc_connection *bsc)
1533{
1534 int new_id, overflow = 0;
1535 struct bsc_cmd_list *pending;
1536
1537 new_id = bsc->last_id;
1538 do {
1539 new_id++;
1540 if (new_id <= 0) {
1541 new_id = 1;
1542 overflow++;
1543 }
1544
1545 llist_for_each_entry(pending, &bsc->cmd_pending, list_entry) {
1546 if (pending->nat_id == new_id)
1547 continue;
1548 }
1549
1550 /* ID is not in use */
1551 break;
1552 } while ((new_id != bsc->last_id) && (overflow < 2));
1553
1554 if ((new_id == bsc->last_id) || (overflow == 2)) {
1555 return -1;
1556 } else {
1557 bsc->last_id = new_id;
1558 return new_id;
1559 }
1560}
1561
1562static void pending_timeout_cb(void *data)
1563{
1564 struct bsc_cmd_list *pending = data;
1565 LOGP(DNAT, LOGL_ERROR, "Command timed out\n");
1566 pending->cmd->type = CTRL_TYPE_ERROR;
1567 pending->cmd->reply = "Command timed out";
1568 ctrl_cmd_send(&pending->ccon->write_queue, pending->cmd);
1569
1570 bsc_del_pending(pending);
1571}
1572
1573static void ctrl_conn_closed_cb(struct ctrl_connection *connection)
1574{
1575 struct bsc_connection *bsc;
1576 struct bsc_cmd_list *pending, *tmp;
1577
1578 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
1579 llist_for_each_entry_safe(pending, tmp, &bsc->cmd_pending, list_entry) {
1580 if (pending->ccon == connection)
1581 bsc_del_pending(pending);
1582 }
1583 }
1584}
1585
1586static int forward_to_bsc(struct ctrl_cmd *cmd)
1587{
1588 int ret = CTRL_CMD_HANDLED;
1589 struct ctrl_cmd *bsc_cmd = NULL;
1590 struct bsc_connection *bsc;
1591 struct bsc_cmd_list *pending;
1592 unsigned int lac;
1593 char *lac_str, *tmp, *saveptr;
1594
1595 /* Skip over the beginning (bsc.) */
1596 tmp = strtok_r(cmd->variable, ".", &saveptr);
1597 lac_str = strtok_r(NULL, ".", &saveptr);
1598 if (!lac_str) {
1599 cmd->reply = "command incomplete";
1600 goto err;
1601 }
1602 lac = atoi(lac_str);
1603
1604 tmp = strtok_r(NULL, "\0", &saveptr);
1605 if (!tmp) {
1606 cmd->reply = "command incomplete";
1607 goto err;
1608 }
1609
1610 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
1611 if (!bsc->cfg)
1612 continue;
1613 if (!bsc->authenticated)
1614 continue;
1615 if (bsc_config_handles_lac(bsc->cfg, lac)) {
1616 /* Add pending command to list */
1617 pending = talloc_zero(bsc, struct bsc_cmd_list);
1618 if (!pending) {
1619 cmd->reply = "OOM";
1620 goto err;
1621 }
1622
1623 pending->nat_id = get_next_free_bsc_id(bsc);
1624 if (pending->nat_id < 0) {
1625 cmd->reply = "No free ID found";
1626 goto err;
1627 }
1628
1629 bsc_cmd = ctrl_cmd_cpy(bsc, cmd);
1630 if (!bsc_cmd) {
1631 cmd->reply = "Could not forward command";
1632 goto err;
1633 }
1634
1635 talloc_free(bsc_cmd->id);
1636 bsc_cmd->id = talloc_asprintf(bsc_cmd, "%i", pending->nat_id);
1637 if (!bsc_cmd->id) {
1638 cmd->reply = "OOM";
1639 goto err;
1640 }
1641
1642 talloc_free(bsc_cmd->variable);
1643 bsc_cmd->variable = talloc_strdup(bsc_cmd, tmp);
1644 if (!bsc_cmd->variable) {
1645 cmd->reply = "OOM";
1646 goto err;
1647 }
1648
1649 if (ctrl_cmd_send(&bsc->write_queue, bsc_cmd)) {
1650 cmd->reply = "Sending failed";
1651 goto err;
1652 }
1653 pending->ccon = cmd->ccon;
1654 pending->ccon->closed_cb = ctrl_conn_closed_cb;
1655 pending->cmd = cmd;
1656
1657 /* Setup the timeout */
1658 pending->timeout.data = pending;
1659 pending->timeout.cb = pending_timeout_cb;
1660 /* TODO: Make timeout configurable */
1661 osmo_timer_schedule(&pending->timeout, 10, 0);
1662 llist_add_tail(&pending->list_entry, &bsc->cmd_pending);
1663
1664 goto done;
1665 }
1666 }
1667 /* We end up here if there's no bsc to handle our LAC */
1668 cmd->reply = "no BSC with this LAC";
1669err:
1670 ret = CTRL_CMD_ERROR;
1671done:
1672 if (bsc_cmd)
1673 talloc_free(bsc_cmd);
1674 return ret;
1675
1676}
1677
1678CTRL_CMD_DEFINE(fwd_cmd, "bsc *");
1679int get_fwd_cmd(struct ctrl_cmd *cmd, void *data)
1680{
1681 return forward_to_bsc(cmd);
1682}
1683
1684int set_fwd_cmd(struct ctrl_cmd *cmd, void *data)
1685{
1686 return forward_to_bsc(cmd);
1687}
1688
1689int verify_fwd_cmd(struct ctrl_cmd *cmd, const char *value, void *data)
1690{
1691 return 0;
1692}
1693
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001694int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001695{
Harald Welte2c869ef2010-08-25 19:43:54 +02001696 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001697
Harald Welte2c869ef2010-08-25 19:43:54 +02001698 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001699
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001700 osmo_init_logging(&log_info);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001701
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001702 nat = bsc_nat_alloc();
1703 if (!nat) {
1704 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1705 return -4;
1706 }
1707
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001708 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001709 if (!nat->mgcp_cfg) {
1710 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1711 return -5;
1712 }
1713
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001714 vty_info.copyright = openbsc_copyright;
1715 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +01001716 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001717 bsc_nat_vty_init(nat);
1718
1719
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001720 /* parse options */
1721 local_addr.s_addr = INADDR_ANY;
1722 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001723
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001724 rate_ctr_init(tall_bsc_ctx);
1725
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001726 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001727 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001728 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001729 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1730 return -3;
1731 }
1732
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001733 /* over rule the VTY config */
1734 if (msc_ip)
1735 bsc_nat_set_msc_ip(nat, msc_ip);
1736
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001737 /* seed the PRNG */
1738 srand(time(NULL));
1739
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001740 /*
1741 * Setup the MGCP code..
1742 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001743 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001744 return -4;
1745
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001746 /* connect to the MSC */
Holger Hans Peter Freythere1880102011-04-23 23:31:31 +02001747 nat->msc_con = bsc_msc_create(nat, &nat->dests);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001748 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001749 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001750 exit(1);
1751 }
1752
Daniel Willmanna86bc392011-02-18 14:32:56 +01001753 controlif_setup(NULL, 4250);
1754 ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_fwd_cmd);
1755
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001756 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001757 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001758 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1759 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001760 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001761 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001762
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001763 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001764 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freyther7d736422011-04-07 22:14:58 +02001765 5000, 0, ipaccess_listen_bsc_cb, nat);
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001766 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001767 fprintf(stderr, "Failed to listen for BSC.\n");
1768 exit(1);
1769 }
1770
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001771 rc = bsc_ussd_init(nat);
1772 if (rc != 0) {
1773 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1774 exit(1);
1775 }
1776
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001777 signal(SIGABRT, &signal_handler);
1778 signal(SIGUSR1, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001779 osmo_init_ignore_signals();
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001780
Harald Welte2c869ef2010-08-25 19:43:54 +02001781 if (daemonize) {
1782 rc = osmo_daemonize();
1783 if (rc < 0) {
1784 perror("Error during daemonize");
1785 exit(1);
1786 }
1787 }
1788
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001789 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001790 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001791 sccp_close.cb = sccp_close_unconfirmed;
1792 sccp_close.data = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001793 osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001794
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001795 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001796 osmo_select_main(0);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001797 }
1798
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001799 return 0;
1800}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001801
1802/* Close all connections handed out to the USSD module */
1803int bsc_close_ussd_connections(struct bsc_nat *nat)
1804{
1805 struct sccp_connections *con;
1806 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +02001807 if (con->con_local != NAT_CON_END_USSD)
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001808 continue;
1809 if (!con->bsc)
1810 continue;
1811
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001812 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001813 nat_send_rlsd_bsc(con);
1814 }
1815
1816 return 0;
1817}