blob: 046f38c1100b4d897646607b87ca01e7ccb4c3a6 [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
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +080047#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080048#include <osmocore/talloc.h>
Harald Welte2c869ef2010-08-25 19:43:54 +020049#include <osmocore/process.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080050
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080051#include <osmocore/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>
55
Harald Welted5db12c2010-08-03 15:11:51 +020056#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080057
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080058#include "../../bscconfig.h"
59
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080060#define SCCP_CLOSE_TIME 20
61#define SCCP_CLOSE_TIME_TIMEOUT 19
62
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080063struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080065static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010066static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080067static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080068static struct timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020069static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010070
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080071const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080072 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010073 "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 +080074 "This is free software: you are free to change and redistribute it.\r\n"
75 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010076
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080077static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080078static 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 +020079static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080080static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080081
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080082struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
83{
84 struct bsc_config *conf;
85
86 llist_for_each_entry(conf, &nat->bsc_configs, entry)
87 if (conf->nr == num)
88 return conf;
89
90 return NULL;
91}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010092
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010093/*
94 * below are stubs we need to link
95 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080096int nm_state_event(enum nm_evt evt, uint8_t obj_class, void *obj,
Holger Hans Peter Freyther2f2d3422010-05-12 16:55:12 +080097 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state,
98 struct abis_om_obj_inst *obj_ins)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010099{
100 return -1;
101}
102
103void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
104{}
105
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800106static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
107{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800108 if (!con) {
109 LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
110 msgb_free(msg);
111 return;
112 }
113
114
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800115 if (write_queue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800116 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
117 msgb_free(msg);
118 }
119}
120
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100121static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100122{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800123 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100124 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
125 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
126 0x00, 0x01, 0x31,
127 };
128
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200129 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100130}
131
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800132static void send_ping(struct bsc_connection *bsc)
133{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800134 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800135 IPAC_MSGT_PING,
136 };
137
138 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
139}
140
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800141static void send_pong(struct bsc_connection *bsc)
142{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800143 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800144 IPAC_MSGT_PONG,
145 };
146
147 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
148}
149
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800150static void bsc_pong_timeout(void *_bsc)
151{
152 struct bsc_connection *bsc = _bsc;
153
154 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
155 bsc_close_connection(bsc);
156}
157
158static void bsc_ping_timeout(void *_bsc)
159{
160 struct bsc_connection *bsc = _bsc;
161
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800162 if (bsc->nat->ping_timeout < 0)
163 return;
164
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800165 send_ping(bsc);
166
167 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800168 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800169
170 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800171 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800172}
173
174static void start_ping_pong(struct bsc_connection *bsc)
175{
176 bsc->pong_timeout.data = bsc;
177 bsc->pong_timeout.cb = bsc_pong_timeout;
178 bsc->ping_timeout.data = bsc;
179 bsc->ping_timeout.cb = bsc_ping_timeout;
180
181 bsc_ping_timeout(bsc);
182}
183
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100184static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100185{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800186 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200187 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100188 };
189
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200190 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100191}
192
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100193static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100194{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800195 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200196 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100197 0x01, IPAC_IDTAG_UNIT,
198 0x01, IPAC_IDTAG_MACADDR,
199 0x01, IPAC_IDTAG_LOCATION1,
200 0x01, IPAC_IDTAG_LOCATION2,
201 0x01, IPAC_IDTAG_EQUIPVERS,
202 0x01, IPAC_IDTAG_SWVERSION,
203 0x01, IPAC_IDTAG_UNITNAME,
204 0x01, IPAC_IDTAG_SERNR,
205 };
206
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200207 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100208}
209
Holger Hans Peter Freyther07dfc702010-10-27 10:54:11 +0200210static void nat_send_rlsd_msc(struct sccp_connections *conn)
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200211{
212 struct sccp_connection_released *rel;
213 struct msgb *msg;
214
215 msg = msgb_alloc_headroom(4096, 128, "rlsd");
216 if (!msg) {
217 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
218 return;
219 }
220
221 msg->l2h = msgb_put(msg, sizeof(*rel));
222 rel = (struct sccp_connection_released *) msg->l2h;
223 rel->type = SCCP_MSG_TYPE_RLSD;
224 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
225 rel->destination_local_reference = conn->remote_ref;
226 rel->source_local_reference = conn->patched_ref;
227
228 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
229
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800230 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200231}
232
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +0200233static void nat_send_rlsd_bsc(struct sccp_connections *conn)
234{
235 struct sccp_connection_released *rel;
236 struct msgb *msg;
237
238 msg = msgb_alloc_headroom(4096, 128, "rlsd");
239 if (!msg) {
240 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
241 return;
242 }
243
244 msg->l2h = msgb_put(msg, sizeof(*rel));
245 rel = (struct sccp_connection_released *) msg->l2h;
246 rel->type = SCCP_MSG_TYPE_RLSD;
247 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
248 rel->destination_local_reference = conn->real_ref;
249 rel->source_local_reference = conn->remote_ref;
250
251 bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP);
252}
253
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200254static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause)
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200255{
256 struct msgb *msg;
257 struct msgb *sccp;
258
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200259 msg = gsm0808_create_clear_command(cause);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200260 if (!msg) {
261 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200262 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200263 }
264
265 sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len);
266 if (!sccp) {
267 LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n");
268 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200269 return NULL;
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200270 }
271
272 msgb_free(msg);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200273 return sccp;
274}
275
276static int nat_send_clrc_bsc(struct sccp_connections *conn)
277{
278 struct msgb *sccp;
279
280 sccp = nat_creat_clrc(conn, 0x20);
281 if (!sccp)
282 return -1;
283 return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP);
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +0200284}
285
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800286static void nat_send_rlc(struct bsc_msc_connection *msc_con,
287 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200288 struct sccp_source_reference *dst)
289{
290 struct sccp_connection_release_complete *rlc;
291 struct msgb *msg;
292
293 msg = msgb_alloc_headroom(4096, 128, "rlc");
294 if (!msg) {
295 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
296 return;
297 }
298
299 msg->l2h = msgb_put(msg, sizeof(*rlc));
300 rlc = (struct sccp_connection_release_complete *) msg->l2h;
301 rlc->type = SCCP_MSG_TYPE_RLC;
302 rlc->destination_local_reference = *dst;
303 rlc->source_local_reference = *src;
304
305 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
306
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800307 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200308}
309
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200310static void send_mgcp_reset(struct bsc_connection *bsc)
311{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800312 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200313 "RSIP 1 13@mgw MGCP 1.0\r\n"
314 };
315
316 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
317}
318
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100319/*
320 * Below is the handling of messages coming
321 * from the MSC and need to be forwarded to
322 * a real BSC.
323 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800324static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100325{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800326 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200327 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100328
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800329 msc_con->first_contact = 1;
330 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100331}
332
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800333static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800334{
335 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
336 if (!msg)
337 return;
338
339 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800340 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800341}
342
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100343/*
344 * Currently we are lacking refcounting so we need to copy each message.
345 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800346static 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 +0100347{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100348 struct msgb *msg;
349
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200350 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100351 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
352 return;
353 }
354
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200355 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100356 if (!msg) {
357 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
358 return;
359 }
360
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200361 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100362 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200363
364 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100365}
366
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800367/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800368 * Update the release statistics
369 */
370static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
371{
372 if (!bsc->cfg) {
373 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
374 return;
375 }
376
377 if (filter >= 0) {
378 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
379 return;
380 }
381
382 if (filter == -1)
383 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
384 else if (normal)
385 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
386 else
387 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
388}
389
390/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800391 * Release an established connection. We will have to release it to the BSC
392 * and to the network and we do it the following way.
393 * 1.) Give up on the MSC side
394 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
395 * ignore the RLC... we might complain about it. Other options would
396 * be to send a Release Request, handle the Release Complete..
397 * 1.2) Mark the data structure to be con_local and wait for 2nd
398 *
399 * 2.) Give up on the BSC side
400 * 2.1) Depending on the con type reject the service, or just close it
401 */
402static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
403{
404 struct msgb *rlsd;
405 /* 1. release the network */
406 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
407 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
408 if (!rlsd)
409 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
410 else {
411 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
412 queue_for_msc(con->msc_con, rlsd);
413 }
414 con->con_local = 1;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800415 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800416
417 /* 2. release the BSC side */
418 if (con->con_type == NAT_CON_TYPE_LU) {
419 struct msgb *payload, *udt;
420 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
421
422 if (payload) {
423 gsm0808_prepend_dtap_header(payload, 0);
424 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
425 if (udt)
426 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
427 else
428 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
429
430 msgb_free(payload);
431 } else {
432 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
433 }
434 }
435
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200436 nat_send_clrc_bsc(con);
437
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800438 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
439 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
440 if (!rlsd) {
441 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
442 sccp_connection_destroy(con);
443 return;
444 }
445
446 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
447 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
448}
449
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800450static void bsc_send_con_refuse(struct bsc_connection *bsc,
451 struct bsc_nat_parsed *parsed, int con_type)
452{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800453 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800454 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800455
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800456 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800457 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800458 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800459 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800460 else {
461 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
462 payload = NULL;
463 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800464
465 /*
466 * Some BSCs do not handle the payload inside a SCCP CREF msg
467 * so we will need to:
468 * 1.) Allocate a local connection and mark it as local..
469 * 2.) queue data for downstream.. and the RLC should delete everything
470 */
471 if (payload) {
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200472 struct msgb *cc, *udt, *clear, *rlsd;
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800473 struct sccp_connections *con;
474 con = create_sccp_src_ref(bsc, parsed);
475 if (!con)
476 goto send_refuse;
477
478 /* declare it local and assign a unique remote_ref */
479 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
480 con->con_local = 1;
481 con->has_remote_ref = 1;
482 con->remote_ref = con->patched_ref;
483
484 /* 1. create a confirmation */
485 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
486 if (!cc)
487 goto send_refuse;
488
489 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800490 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800491 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
492 if (!udt) {
493 msgb_free(cc);
494 goto send_refuse;
495 }
496
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200497 /* 3. send a Clear Command */
498 clear = nat_creat_clrc(con, 0x20);
499 if (!clear) {
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800500 msgb_free(cc);
501 msgb_free(udt);
502 goto send_refuse;
503 }
504
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200505 /* 4. send a RLSD */
506 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
507 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
508 if (!rlsd) {
509 msgb_free(cc);
510 msgb_free(udt);
511 msgb_free(clear);
512 goto send_refuse;
513 }
514
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800515 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
516 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther43eb1a32010-10-29 17:05:28 +0200517 bsc_write(bsc, clear, IPAC_PROTO_SCCP);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800518 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
519 msgb_free(payload);
520 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800521 }
522
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800523
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800524send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800525 if (payload)
526 msgb_free(payload);
527
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800528 refuse = sccp_create_refuse(parsed->src_local_ref,
529 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800530 if (!refuse) {
531 LOGP(DNAT, LOGL_ERROR,
532 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
533 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
534 return;
535 }
536
537 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
538}
539
540
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800541static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100542{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800543 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800544 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800545 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200546 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100547
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100548 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800549 parsed = bsc_nat_parse(msg);
550 if (!parsed) {
551 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100552 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800553 }
554
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100555 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800556 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800557
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200558 proto = parsed->ipa_proto;
559
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100560 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200561 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100562 switch (parsed->sccp_type) {
563 case SCCP_MSG_TYPE_UDT:
564 /* forward UDT messages to every BSC */
565 goto send_to_all;
566 break;
567 case SCCP_MSG_TYPE_RLSD:
568 case SCCP_MSG_TYPE_CREF:
569 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200570 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800571 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800572 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200573 counter_inc(nat->stats.sccp.calls);
574
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800575 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800576 struct rate_ctr_group *ctrg;
577 ctrg = con->bsc->cfg->stats.ctrg;
578 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800579 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800580 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
581 } else
582 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
583 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200584 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100585 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800586 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
587 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200588 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800589 break;
590 case SCCP_MSG_TYPE_RLC:
591 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
592 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100593 break;
594 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100595 /* MSC never opens a SCCP connection, fall through */
596 default:
597 goto exit;
598 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200599
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200600 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
601 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
602 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800603 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200604 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800605 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 +0100606 }
607
608 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800609 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100610 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800611 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800612 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100613 return -1;
614 }
615
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200616 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100617 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100618
619send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800620 /*
621 * Filter Paging from the network. We do not want to send a PAGING
622 * Command to every BSC in our network. We will analys the PAGING
623 * message and then send it to the authenticated messages...
624 */
625 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200626 int lac;
627 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800628 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800629 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800630 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200631 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherda340632010-08-06 20:15:40 +0800632 else if (lac != -1)
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200633 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
634 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800635
636 goto exit;
637 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100638 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800639 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100640 if (!bsc->authenticated)
641 continue;
642
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200643 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100644 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800645
646exit:
647 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100648 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100649}
650
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800651static void msc_connection_was_lost(struct bsc_msc_connection *con)
652{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200653 struct bsc_connection *bsc, *tmp;
654
655 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
656 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800657 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200658
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200659 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200660 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800661}
662
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800663static void msc_connection_connected(struct bsc_msc_connection *con)
664{
665 counter_inc(nat->stats.msc.reconn);
666}
667
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200668static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200669{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800670 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200671 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200672 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
673 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
674 0x01, 0x20
675 };
676
677 struct msgb *msg;
678
679 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
680 if (!msg) {
681 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
682 return;
683 }
684
685 msg->l2h = msgb_put(msg, sizeof(reset));
686 memcpy(msg->l2h, reset, msgb_l2len(msg));
687
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800688 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200689
690 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
691}
692
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800693static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100694{
695 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800696 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100697 struct msgb *msg = ipaccess_read_msg(bfd, &error);
698 struct ipaccess_head *hh;
699
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800700 msc_con = (struct bsc_msc_connection *) bfd->data;
701
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100702 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800703 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100704 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800705 else
706 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100707
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800708 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100709 return -1;
710 }
711
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100712 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 +0100713
714 /* handle base message handling */
715 hh = (struct ipaccess_head *) msg->data;
716 ipaccess_rcvmsg_base(msg, bfd);
717
718 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800719 if (hh->proto == IPAC_PROTO_IPACCESS) {
720 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800721 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800722 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800723 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800724 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800725 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100726
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800727 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100728 return 0;
729}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800730
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800731static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
732{
733 int rc;
734 rc = write(bfd->fd, msg->data, msg->len);
735
736 if (rc != msg->len) {
737 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
738 return -1;
739 }
740
741 return rc;
742}
743
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100744/*
745 * Below is the handling of messages coming
746 * from the BSC and need to be forwarded to
747 * a real BSC.
748 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100749
750/*
751 * Remove the connection from the connections list,
752 * remove it from the patching of SCCP header lists
753 * as well. Maybe in the future even close connection..
754 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800755void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100756{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100757 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800758 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100759
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800760 /* stop the timeout timer */
761 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800762 bsc_del_timer(&connection->ping_timeout);
763 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800764
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800765 if (connection->cfg)
766 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
767
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100768 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800769 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100770 if (sccp_patch->bsc != connection)
771 continue;
772
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800773 if (ctr)
774 rate_ctr_inc(ctr);
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800775 if (sccp_patch->has_remote_ref && !sccp_patch->con_local)
Holger Hans Peter Freyther07dfc702010-10-27 10:54:11 +0200776 nat_send_rlsd_msc(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200777 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100778 }
779
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200780 /* close endpoints allocated by this BSC */
781 bsc_mgcp_clear_endpoints_for(connection);
782
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800783 bsc_unregister_fd(&connection->write_queue.bfd);
784 close(connection->write_queue.bfd.fd);
785 write_queue_clear(&connection->write_queue);
786 llist_del(&connection->list_entry);
787
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100788 talloc_free(connection);
789}
790
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800791static void ipaccess_close_bsc(void *data)
792{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200793 struct sockaddr_in sock;
794 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800795 struct bsc_connection *conn = data;
796
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200797
798 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
799 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
800 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800801 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800802}
803
804static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
805{
806 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800807 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200808 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800809
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800810 if (bsc->cfg) {
811 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
812 bsc->write_queue.bfd.fd, bsc->cfg->nr);
813 return;
814 }
815
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800816 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200817 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800818 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800819 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200820 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800821 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800822 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
823 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800824 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200825 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800826 }
827 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200828
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800829 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
830 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800831}
832
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800833static void handle_con_stats(struct sccp_connections *con)
834{
835 struct rate_ctr_group *ctrg;
836 int id = bsc_conn_type_to_ctr(con);
837
838 if (id == -1)
839 return;
840
841 if (!con->bsc || !con->bsc->cfg)
842 return;
843
844 ctrg = con->bsc->cfg->stats.ctrg;
845 rate_ctr_inc(&ctrg->ctr[id]);
846}
847
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100848static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100849{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800850 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800851 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800852 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800853 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800854 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800855 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100856
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800857 /* Parse and filter messages */
858 parsed = bsc_nat_parse(msg);
859 if (!parsed) {
860 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200861 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800862 return -1;
863 }
864
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100865 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800866 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800867
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200868 /*
869 * check authentication after filtering to not reject auth
870 * responses coming from the BSC. We have to make sure that
871 * nothing from the exit path will forward things to the MSC
872 */
873 if (!bsc->authenticated) {
874 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
875 msgb_free(msg);
876 return -1;
877 }
878
879
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100880 /* modify the SCCP entries */
881 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800882 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800883 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100884 switch (parsed->sccp_type) {
885 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800886 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800887 if (filter < 0) {
888 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800889 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800890 }
891
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800892 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100893 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800894 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800895 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800896 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800897 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800898 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800899 if (imsi)
900 con->imsi = talloc_steal(con, imsi);
901 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800902 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800903 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100904 break;
905 case SCCP_MSG_TYPE_RLSD:
906 case SCCP_MSG_TYPE_CREF:
907 case SCCP_MSG_TYPE_DT1:
908 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200909 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800910 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800911 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800912 /* only filter non local connections */
913 if (!con->con_local) {
914 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
915 if (filter < 0) {
916 bsc_stat_reject(filter, bsc, 1);
917 bsc_send_con_release(bsc, con);
918 con = NULL;
919 goto exit2;
920 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800921
922 /* hand data to a side channel */
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200923 if (bsc_check_ussd(con, parsed, msg) == 1)
924 con->con_local = 2;
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200925
926 /*
927 * Optionally rewrite setup message. This can
928 * replace the msg and the parsed structure becomes
929 * invalid.
930 */
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200931 msg = bsc_nat_rewrite_setup(bsc->nat, msg, parsed, con->imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200932 talloc_free(parsed);
933 parsed = NULL;
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800934 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800935
936 con_bsc = con->bsc;
937 con_msc = con->msc_con;
938 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800939 }
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800940
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100941 break;
942 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800943 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800944 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800945 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800946 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800947 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800948 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200949 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100950 break;
951 case SCCP_MSG_TYPE_UDT:
952 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800953 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100954 break;
955 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800956 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 +0800957 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100958 goto exit2;
959 break;
960 }
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +0200961 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200962 bsc_mgcp_forward(bsc, msg);
963 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800964 } else {
965 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
966 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100967 }
968
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800969 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800970 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 +0800971 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100972 goto exit2;
973 }
974
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +0800975 /* do not forward messages to the MSC */
976 if (con_filter)
977 goto exit2;
978
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800979 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +0800980 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
981 bsc->cfg->nr,
982 parsed ? parsed->ipa_proto : -1,
983 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800984 goto exit2;
985 }
986
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100987 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800988 queue_for_msc(con_msc, msg);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200989 if (parsed)
990 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800991 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800992
993exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100994 /* if we filter out the reset send an ack to the BSC */
995 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100996 send_reset_ack(bsc);
997 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800998 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
999 /* do we know who is handling this? */
1000 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
1001 struct tlv_parsed tvp;
1002 ipaccess_idtag_parse(&tvp,
1003 (unsigned char *) msg->l2h + 2,
1004 msgb_l2len(msg) - 2);
1005 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
1006 ipaccess_auth_bsc(&tvp, bsc);
1007 }
1008
1009 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +01001010 }
1011
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001012exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001013 if (imsi)
1014 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +08001015 talloc_free(parsed);
1016 msgb_free(msg);
1017 return -1;
1018
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +08001019exit3:
1020 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +08001021 if (imsi)
1022 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +08001023 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001024 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +08001025 msgb_free(msg);
1026 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001027}
1028
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001029static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001030{
1031 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001032 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001033 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001034 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001035
1036 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001037 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001038 LOGP(DNAT, LOGL_ERROR,
1039 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
1040 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001041 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +08001042 LOGP(DNAT, LOGL_ERROR,
1043 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
1044 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +08001045
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +08001046 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001047 return -1;
1048 }
1049
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001050
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001051 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 +01001052
1053 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001054 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001055
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001056 /* stop the pong timeout */
1057 if (hh->proto == IPAC_PROTO_IPACCESS) {
1058 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001059 bsc_del_timer(&bsc->pong_timeout);
1060 msgb_free(msg);
1061 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +08001062 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
1063 send_pong(bsc);
1064 msgb_free(msg);
1065 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +08001066 }
1067 }
1068
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001069 /* FIXME: Currently no PONG is sent to the BSC */
1070 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +01001071 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001072
1073 return 0;
1074}
1075
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001076static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
1077{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001078 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001079 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001080 struct sockaddr_in sa;
1081 socklen_t sa_len = sizeof(sa);
1082
1083 if (!(what & BSC_FD_READ))
1084 return 0;
1085
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001086 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1087 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001088 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001089 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001090 }
1091
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001092 /* count the reconnect */
1093 counter_inc(nat->stats.bsc.reconn);
1094
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001095 /*
1096 * if we are not connected to a msc... just close the socket
1097 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001098 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001099 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001100 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001101 return 0;
1102 }
1103
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001104 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001105 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001106 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001107 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1108
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001109 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001110 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001111 if (rc != 0)
1112 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1113
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001114 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001115 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001116
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001117 /*
1118 *
1119 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001120 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001121 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001122 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001123 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001124 return -1;
1125 }
1126
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001127 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001128 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001129 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001130 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001131 bsc->write_queue.bfd.when = BSC_FD_READ;
1132 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001133 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001134 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001135 talloc_free(bsc);
1136 return -2;
1137 }
1138
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001139 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001140 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001141 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001142 send_id_ack(bsc);
1143 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001144 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001145
1146 /*
1147 * start the hangup timer
1148 */
1149 bsc->id_timeout.data = bsc;
1150 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +08001151 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001152 return 0;
1153}
1154
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001155static void print_usage()
1156{
1157 printf("Usage: bsc_nat\n");
1158}
1159
1160static void print_help()
1161{
1162 printf(" Some useful help...\n");
1163 printf(" -h --help this text\n");
1164 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001165 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001166 printf(" -s --disable-color\n");
1167 printf(" -c --config-file filename The config file to use.\n");
1168 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001169 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001170}
1171
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001172static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001173{
1174 while (1) {
1175 int option_index = 0, c;
1176 static struct option long_options[] = {
1177 {"help", 0, 0, 'h'},
1178 {"debug", 1, 0, 'd'},
1179 {"config-file", 1, 0, 'c'},
1180 {"disable-color", 0, 0, 's'},
1181 {"timestamp", 0, 0, 'T'},
1182 {"msc", 1, 0, 'm'},
1183 {"local", 1, 0, 'l'},
1184 {0, 0, 0, 0}
1185 };
1186
1187 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1188 long_options, &option_index);
1189 if (c == -1)
1190 break;
1191
1192 switch (c) {
1193 case 'h':
1194 print_usage();
1195 print_help();
1196 exit(0);
1197 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001198 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001199 break;
1200 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001201 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001202 break;
1203 case 'c':
1204 config_file = strdup(optarg);
1205 break;
1206 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001207 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001208 break;
1209 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001210 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001211 break;
1212 case 'l':
1213 inet_aton(optarg, &local_addr);
1214 break;
1215 default:
1216 /* ignore */
1217 break;
1218 }
1219 }
1220}
1221
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001222static void signal_handler(int signal)
1223{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001224 switch (signal) {
1225 case SIGABRT:
1226 /* in case of abort, we want to obtain a talloc report
1227 * and then return to the caller, who will abort the process */
1228 case SIGUSR1:
1229 talloc_report_full(tall_bsc_ctx, stderr);
1230 break;
1231 default:
1232 break;
1233 }
1234}
1235
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001236static void sccp_close_unconfirmed(void *_data)
1237{
1238 struct sccp_connections *conn, *tmp1;
1239 struct timespec now;
1240 clock_gettime(CLOCK_MONOTONIC, &now);
1241
1242 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1243 if (conn->has_remote_ref)
1244 continue;
1245
1246 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1247 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1248 continue;
1249
1250 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1251 sccp_src_ref_to_int(&conn->real_ref),
1252 sccp_src_ref_to_int(&conn->patched_ref));
1253 sccp_connection_destroy(conn);
1254 }
1255
1256 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1257}
1258
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001259extern void *tall_msgb_ctx;
1260extern void *tall_ctr_ctx;
1261static void talloc_init_ctx()
1262{
1263 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1264 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1265 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1266}
1267
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001268extern enum node_type bsc_vty_go_parent(struct vty *vty);
1269
1270static struct vty_app_info vty_info = {
1271 .name = "BSC NAT",
1272 .version = PACKAGE_VERSION,
1273 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001274 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001275};
1276
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001277int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001278{
Harald Welte2c869ef2010-08-25 19:43:54 +02001279 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001280
Harald Welte2c869ef2010-08-25 19:43:54 +02001281 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001282
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001283 log_init(&log_info);
1284 stderr_target = log_target_create_stderr();
1285 log_add_target(stderr_target);
1286 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001287
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001288 nat = bsc_nat_alloc();
1289 if (!nat) {
1290 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1291 return -4;
1292 }
1293
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001294 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001295 if (!nat->mgcp_cfg) {
1296 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1297 return -5;
1298 }
1299
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001300 vty_info.copyright = openbsc_copyright;
1301 vty_init(&vty_info);
1302 logging_vty_add_cmds();
1303 bsc_nat_vty_init(nat);
1304
1305
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001306 /* parse options */
1307 local_addr.s_addr = INADDR_ANY;
1308 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001309
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001310 rate_ctr_init(tall_bsc_ctx);
1311
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001312 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001313 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001314 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001315 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1316 return -3;
1317 }
1318
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001319 /* over rule the VTY config */
1320 if (msc_ip)
1321 bsc_nat_set_msc_ip(nat, msc_ip);
1322
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001323 /* seed the PRNG */
1324 srand(time(NULL));
1325
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001326 /*
1327 * Setup the MGCP code..
1328 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001329 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001330 return -4;
1331
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001332 /* connect to the MSC */
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +08001333 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001334 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001335 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001336 exit(1);
1337 }
1338
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001339 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001340 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001341 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1342 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001343 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001344 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001345
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001346 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001347 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001348 5000, ipaccess_listen_bsc_cb);
1349 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001350 fprintf(stderr, "Failed to listen for BSC.\n");
1351 exit(1);
1352 }
1353
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +02001354 rc = bsc_ussd_init(nat);
1355 if (rc != 0) {
1356 LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n");
1357 exit(1);
1358 }
1359
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001360 signal(SIGABRT, &signal_handler);
1361 signal(SIGUSR1, &signal_handler);
1362 signal(SIGPIPE, SIG_IGN);
1363
Harald Welte2c869ef2010-08-25 19:43:54 +02001364 if (daemonize) {
1365 rc = osmo_daemonize();
1366 if (rc < 0) {
1367 perror("Error during daemonize");
1368 exit(1);
1369 }
1370 }
1371
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001372 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001373 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001374 sccp_close.cb = sccp_close_unconfirmed;
1375 sccp_close.data = NULL;
1376 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1377
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001378 while (1) {
1379 bsc_select_main(0);
1380 }
1381
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001382 return 0;
1383}
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001384
1385/* Close all connections handed out to the USSD module */
1386int bsc_close_ussd_connections(struct bsc_nat *nat)
1387{
1388 struct sccp_connections *con;
1389 llist_for_each_entry(con, &nat->sccp_connections, list_entry) {
1390 if (con->con_local != 2)
1391 continue;
1392 if (!con->bsc)
1393 continue;
1394
Holger Hans Peter Freythera8a50a82010-10-27 11:58:04 +02001395 nat_send_clrc_bsc(con);
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +02001396 nat_send_rlsd_bsc(con);
1397 }
1398
1399 return 0;
1400}