blob: b82c4ef2532de78518e73a54ff57218782aa19a8 [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24#include <sys/socket.h>
25#include <netinet/in.h>
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +080026#include <netinet/tcp.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080027#include <arpa/inet.h>
28
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +010029#include <errno.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010030#include <signal.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080031#include <stdio.h>
32#include <stdlib.h>
Holger Hans Peter Freyther5aa25ae2010-01-12 21:36:08 +010033#include <time.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080034#include <unistd.h>
35
36#define _GNU_SOURCE
37#include <getopt.h>
38
39#include <openbsc/debug.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010040#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080042#include <openbsc/bsc_nat_sccp.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 Freythered775e42010-06-15 18:52:24 +080048#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080049#include <osmocore/talloc.h>
Harald Welte2c869ef2010-08-25 19:43:54 +020050#include <osmocore/process.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080051
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080052#include <osmocore/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>
56
Harald Welted5db12c2010-08-03 15:11:51 +020057#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080058
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080059#include "../../bscconfig.h"
60
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080061#define SCCP_CLOSE_TIME 20
62#define SCCP_CLOSE_TIME_TIMEOUT 19
63
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080064struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080065static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080066static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010067static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080068static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080069static struct timer_list sccp_close;
Harald Welte2c869ef2010-08-25 19:43:54 +020070static int daemonize = 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010071
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080072const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080073 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n"
74 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\r\n"
75 "This is free software: you are free to change and redistribute it.\r\n"
76 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010077
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080078static struct bsc_nat *nat;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080079static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int);
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +020080static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +080081static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080082
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080083struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
84{
85 struct bsc_config *conf;
86
87 llist_for_each_entry(conf, &nat->bsc_configs, entry)
88 if (conf->nr == num)
89 return conf;
90
91 return NULL;
92}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010093
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010094/*
95 * below are stubs we need to link
96 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080097int nm_state_event(enum nm_evt evt, uint8_t obj_class, void *obj,
Holger Hans Peter Freyther2f2d3422010-05-12 16:55:12 +080098 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state,
99 struct abis_om_obj_inst *obj_ins)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100100{
101 return -1;
102}
103
104void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
105{}
106
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800107static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
108{
Holger Hans Peter Freythere8e41e62010-10-06 00:24:28 +0800109 if (!con) {
110 LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n");
111 msgb_free(msg);
112 return;
113 }
114
115
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800116 if (write_queue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800117 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
118 msgb_free(msg);
119 }
120}
121
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100122static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100123{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800124 static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100125 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
126 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
127 0x00, 0x01, 0x31,
128 };
129
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200130 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100131}
132
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800133static void send_ping(struct bsc_connection *bsc)
134{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800135 static const uint8_t id_ping[] = {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800136 IPAC_MSGT_PING,
137 };
138
139 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
140}
141
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800142static void send_pong(struct bsc_connection *bsc)
143{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800144 static const uint8_t id_pong[] = {
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800145 IPAC_MSGT_PONG,
146 };
147
148 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
149}
150
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800151static void bsc_pong_timeout(void *_bsc)
152{
153 struct bsc_connection *bsc = _bsc;
154
155 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
156 bsc_close_connection(bsc);
157}
158
159static void bsc_ping_timeout(void *_bsc)
160{
161 struct bsc_connection *bsc = _bsc;
162
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800163 if (bsc->nat->ping_timeout < 0)
164 return;
165
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800166 send_ping(bsc);
167
168 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800169 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800170
171 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800172 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800173}
174
175static void start_ping_pong(struct bsc_connection *bsc)
176{
177 bsc->pong_timeout.data = bsc;
178 bsc->pong_timeout.cb = bsc_pong_timeout;
179 bsc->ping_timeout.data = bsc;
180 bsc->ping_timeout.cb = bsc_ping_timeout;
181
182 bsc_ping_timeout(bsc);
183}
184
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100185static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100186{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800187 static const uint8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200188 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100189 };
190
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200191 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100192}
193
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100194static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100195{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800196 static const uint8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200197 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100198 0x01, IPAC_IDTAG_UNIT,
199 0x01, IPAC_IDTAG_MACADDR,
200 0x01, IPAC_IDTAG_LOCATION1,
201 0x01, IPAC_IDTAG_LOCATION2,
202 0x01, IPAC_IDTAG_EQUIPVERS,
203 0x01, IPAC_IDTAG_SWVERSION,
204 0x01, IPAC_IDTAG_UNITNAME,
205 0x01, IPAC_IDTAG_SERNR,
206 };
207
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200208 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100209}
210
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200211static void nat_send_rlsd(struct sccp_connections *conn)
212{
213 struct sccp_connection_released *rel;
214 struct msgb *msg;
215
216 msg = msgb_alloc_headroom(4096, 128, "rlsd");
217 if (!msg) {
218 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
219 return;
220 }
221
222 msg->l2h = msgb_put(msg, sizeof(*rel));
223 rel = (struct sccp_connection_released *) msg->l2h;
224 rel->type = SCCP_MSG_TYPE_RLSD;
225 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
226 rel->destination_local_reference = conn->remote_ref;
227 rel->source_local_reference = conn->patched_ref;
228
229 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
230
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800231 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200232}
233
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800234static void nat_send_rlc(struct bsc_msc_connection *msc_con,
235 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200236 struct sccp_source_reference *dst)
237{
238 struct sccp_connection_release_complete *rlc;
239 struct msgb *msg;
240
241 msg = msgb_alloc_headroom(4096, 128, "rlc");
242 if (!msg) {
243 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
244 return;
245 }
246
247 msg->l2h = msgb_put(msg, sizeof(*rlc));
248 rlc = (struct sccp_connection_release_complete *) msg->l2h;
249 rlc->type = SCCP_MSG_TYPE_RLC;
250 rlc->destination_local_reference = *dst;
251 rlc->source_local_reference = *src;
252
253 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
254
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800255 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200256}
257
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200258static void send_mgcp_reset(struct bsc_connection *bsc)
259{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800260 static const uint8_t mgcp_reset[] = {
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200261 "RSIP 1 13@mgw MGCP 1.0\r\n"
262 };
263
264 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
265}
266
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100267/*
268 * Below is the handling of messages coming
269 * from the MSC and need to be forwarded to
270 * a real BSC.
271 */
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800272static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100273{
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800274 if (msc_con->first_contact)
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200275 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100276
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800277 msc_con->first_contact = 1;
278 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100279}
280
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800281static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800282{
283 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
284 if (!msg)
285 return;
286
287 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800288 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800289}
290
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100291/*
292 * Currently we are lacking refcounting so we need to copy each message.
293 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800294static 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 +0100295{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100296 struct msgb *msg;
297
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200298 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100299 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
300 return;
301 }
302
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200303 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100304 if (!msg) {
305 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
306 return;
307 }
308
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200309 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100310 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200311
312 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100313}
314
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800315/*
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800316 * Update the release statistics
317 */
318static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal)
319{
320 if (!bsc->cfg) {
321 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.");
322 return;
323 }
324
325 if (filter >= 0) {
326 LOGP(DNAT, LOGL_ERROR, "Connection was not rejected");
327 return;
328 }
329
330 if (filter == -1)
331 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]);
332 else if (normal)
333 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]);
334 else
335 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]);
336}
337
338/*
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800339 * Release an established connection. We will have to release it to the BSC
340 * and to the network and we do it the following way.
341 * 1.) Give up on the MSC side
342 * 1.1) Send a RLSD message, it is a bit non standard but should work, we
343 * ignore the RLC... we might complain about it. Other options would
344 * be to send a Release Request, handle the Release Complete..
345 * 1.2) Mark the data structure to be con_local and wait for 2nd
346 *
347 * 2.) Give up on the BSC side
348 * 2.1) Depending on the con type reject the service, or just close it
349 */
350static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con)
351{
352 struct msgb *rlsd;
353 /* 1. release the network */
354 rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref,
355 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
356 if (!rlsd)
357 LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n");
358 else {
359 ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP);
360 queue_for_msc(con->msc_con, rlsd);
361 }
362 con->con_local = 1;
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800363 con->msc_con = NULL;
Holger Hans Peter Freytherac2763b2010-09-15 07:43:59 +0800364
365 /* 2. release the BSC side */
366 if (con->con_type == NAT_CON_TYPE_LU) {
367 struct msgb *payload, *udt;
368 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
369
370 if (payload) {
371 gsm0808_prepend_dtap_header(payload, 0);
372 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
373 if (udt)
374 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
375 else
376 LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n");
377
378 msgb_free(payload);
379 } else {
380 LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n");
381 }
382 }
383
384 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
385 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
386 if (!rlsd) {
387 LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n");
388 sccp_connection_destroy(con);
389 return;
390 }
391
392 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
393 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
394}
395
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800396static void bsc_send_con_refuse(struct bsc_connection *bsc,
397 struct bsc_nat_parsed *parsed, int con_type)
398{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800399 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800400 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800401
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800402 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800403 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800404 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800405 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800406 else {
407 LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type);
408 payload = NULL;
409 }
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800410
411 /*
412 * Some BSCs do not handle the payload inside a SCCP CREF msg
413 * so we will need to:
414 * 1.) Allocate a local connection and mark it as local..
415 * 2.) queue data for downstream.. and the RLC should delete everything
416 */
417 if (payload) {
418 struct msgb *cc, *udt, *rlsd;
419 struct sccp_connections *con;
420 con = create_sccp_src_ref(bsc, parsed);
421 if (!con)
422 goto send_refuse;
423
424 /* declare it local and assign a unique remote_ref */
425 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
426 con->con_local = 1;
427 con->has_remote_ref = 1;
428 con->remote_ref = con->patched_ref;
429
430 /* 1. create a confirmation */
431 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
432 if (!cc)
433 goto send_refuse;
434
435 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800436 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800437 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
438 if (!udt) {
439 msgb_free(cc);
440 goto send_refuse;
441 }
442
443 /* 3. send a RLSD */
444 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
445 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
446 if (!rlsd) {
447 msgb_free(cc);
448 msgb_free(udt);
449 goto send_refuse;
450 }
451
452 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
453 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
454 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
455 msgb_free(payload);
456 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800457 }
458
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800459
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800460send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800461 if (payload)
462 msgb_free(payload);
463
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800464 refuse = sccp_create_refuse(parsed->src_local_ref,
465 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800466 if (!refuse) {
467 LOGP(DNAT, LOGL_ERROR,
468 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
469 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
470 return;
471 }
472
473 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
474}
475
476
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800477static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100478{
Holger Hans Peter Freyther8d7b10e2010-07-23 19:43:12 +0800479 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800480 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800481 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200482 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100483
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100484 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800485 parsed = bsc_nat_parse(msg);
486 if (!parsed) {
487 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100488 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800489 }
490
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100491 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800492 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800493
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200494 proto = parsed->ipa_proto;
495
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100496 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200497 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100498 switch (parsed->sccp_type) {
499 case SCCP_MSG_TYPE_UDT:
500 /* forward UDT messages to every BSC */
501 goto send_to_all;
502 break;
503 case SCCP_MSG_TYPE_RLSD:
504 case SCCP_MSG_TYPE_CREF:
505 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200506 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800507 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800508 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200509 counter_inc(nat->stats.sccp.calls);
510
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800511 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800512 struct rate_ctr_group *ctrg;
513 ctrg = con->bsc->cfg->stats.ctrg;
514 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800515 if (bsc_mgcp_assign_patch(con, msg) != 0)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800516 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
517 } else
518 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
519 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200520 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100521 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800522 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
523 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200524 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800525 break;
526 case SCCP_MSG_TYPE_RLC:
527 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
528 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100529 break;
530 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100531 /* MSC never opens a SCCP connection, fall through */
532 default:
533 goto exit;
534 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200535
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200536 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
537 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
538 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800539 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200540 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800541 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 +0100542 }
543
544 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800545 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100546 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800547 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800548 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100549 return -1;
550 }
551
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200552 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100553 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100554
555send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800556 /*
557 * Filter Paging from the network. We do not want to send a PAGING
558 * Command to every BSC in our network. We will analys the PAGING
559 * message and then send it to the authenticated messages...
560 */
561 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200562 int lac;
563 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800564 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800565 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800566 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200567 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherda340632010-08-06 20:15:40 +0800568 else if (lac != -1)
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200569 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
570 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800571
572 goto exit;
573 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100574 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800575 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100576 if (!bsc->authenticated)
577 continue;
578
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200579 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100580 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800581
582exit:
583 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100584 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100585}
586
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800587static void msc_connection_was_lost(struct bsc_msc_connection *con)
588{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200589 struct bsc_connection *bsc, *tmp;
590
591 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
592 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800593 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200594
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200595 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200596 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800597}
598
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +0800599static void msc_connection_connected(struct bsc_msc_connection *con)
600{
601 counter_inc(nat->stats.msc.reconn);
602}
603
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200604static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200605{
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800606 static const uint8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200607 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200608 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
609 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
610 0x01, 0x20
611 };
612
613 struct msgb *msg;
614
615 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
616 if (!msg) {
617 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
618 return;
619 }
620
621 msg->l2h = msgb_put(msg, sizeof(reset));
622 memcpy(msg->l2h, reset, msgb_l2len(msg));
623
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800624 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200625
626 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
627}
628
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800629static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100630{
631 int error;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800632 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100633 struct msgb *msg = ipaccess_read_msg(bfd, &error);
634 struct ipaccess_head *hh;
635
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800636 msc_con = (struct bsc_msc_connection *) bfd->data;
637
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100638 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800639 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100640 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800641 else
642 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100643
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800644 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100645 return -1;
646 }
647
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100648 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 +0100649
650 /* handle base message handling */
651 hh = (struct ipaccess_head *) msg->data;
652 ipaccess_rcvmsg_base(msg, bfd);
653
654 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800655 if (hh->proto == IPAC_PROTO_IPACCESS) {
656 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800657 initialize_msc_if_needed(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800658 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800659 send_id_get_response(msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800660 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800661 forward_sccp_to_bts(msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100662
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800663 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100664 return 0;
665}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800666
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800667static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
668{
669 int rc;
670 rc = write(bfd->fd, msg->data, msg->len);
671
672 if (rc != msg->len) {
673 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
674 return -1;
675 }
676
677 return rc;
678}
679
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100680/*
681 * Below is the handling of messages coming
682 * from the BSC and need to be forwarded to
683 * a real BSC.
684 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100685
686/*
687 * Remove the connection from the connections list,
688 * remove it from the patching of SCCP header lists
689 * as well. Maybe in the future even close connection..
690 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800691void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100692{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100693 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800694 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100695
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800696 /* stop the timeout timer */
697 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800698 bsc_del_timer(&connection->ping_timeout);
699 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800700
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800701 if (connection->cfg)
702 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
703
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100704 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800705 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100706 if (sccp_patch->bsc != connection)
707 continue;
708
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800709 if (ctr)
710 rate_ctr_inc(ctr);
Holger Hans Peter Freythereea5a1b2010-09-16 06:41:09 +0800711 if (sccp_patch->has_remote_ref && !sccp_patch->con_local)
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800712 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200713 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100714 }
715
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200716 /* close endpoints allocated by this BSC */
717 bsc_mgcp_clear_endpoints_for(connection);
718
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800719 bsc_unregister_fd(&connection->write_queue.bfd);
720 close(connection->write_queue.bfd.fd);
721 write_queue_clear(&connection->write_queue);
722 llist_del(&connection->list_entry);
723
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100724 talloc_free(connection);
725}
726
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800727static void ipaccess_close_bsc(void *data)
728{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200729 struct sockaddr_in sock;
730 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800731 struct bsc_connection *conn = data;
732
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200733
734 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
735 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
736 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800737 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800738}
739
740static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
741{
742 struct bsc_config *conf;
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800743 const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200744 const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800745
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800746 if (bsc->cfg) {
747 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
748 bsc->write_queue.bfd.fd, bsc->cfg->nr);
749 return;
750 }
751
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800752 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
Holger Hans Peter Freythera09b9662010-10-14 17:19:58 +0200753 if (strncmp(conf->token, token, len) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800754 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800755 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200756 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800757 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800758 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n",
759 conf->nr, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800760 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200761 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800762 }
763 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200764
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800765 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
766 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800767}
768
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800769static void handle_con_stats(struct sccp_connections *con)
770{
771 struct rate_ctr_group *ctrg;
772 int id = bsc_conn_type_to_ctr(con);
773
774 if (id == -1)
775 return;
776
777 if (!con->bsc || !con->bsc->cfg)
778 return;
779
780 ctrg = con->bsc->cfg->stats.ctrg;
781 rate_ctr_inc(&ctrg->ctr[id]);
782}
783
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100784static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100785{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800786 int con_filter = 0;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800787 char *imsi = NULL;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800788 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800789 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800790 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800791 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100792
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800793 /* Parse and filter messages */
794 parsed = bsc_nat_parse(msg);
795 if (!parsed) {
796 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200797 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800798 return -1;
799 }
800
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100801 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800802 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800803
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200804 /*
805 * check authentication after filtering to not reject auth
806 * responses coming from the BSC. We have to make sure that
807 * nothing from the exit path will forward things to the MSC
808 */
809 if (!bsc->authenticated) {
810 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
811 msgb_free(msg);
812 return -1;
813 }
814
815
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100816 /* modify the SCCP entries */
817 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther3837f992010-09-15 00:38:54 +0800818 int filter;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800819 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100820 switch (parsed->sccp_type) {
821 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800822 filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi);
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800823 if (filter < 0) {
824 bsc_stat_reject(filter, bsc, 0);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800825 goto exit3;
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800826 }
827
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800828 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100829 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800830 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800831 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800832 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800833 con->con_type = con_type;
Holger Hans Peter Freyther909e61f2010-09-15 00:41:19 +0800834 con->imsi_checked = filter;
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800835 if (imsi)
836 con->imsi = talloc_steal(con, imsi);
837 imsi = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800838 con_bsc = con->bsc;
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800839 handle_con_stats(con);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100840 break;
841 case SCCP_MSG_TYPE_RLSD:
842 case SCCP_MSG_TYPE_CREF:
843 case SCCP_MSG_TYPE_DT1:
844 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200845 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800846 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800847 if (con) {
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800848 /* only filter non local connections */
849 if (!con->con_local) {
850 filter = bsc_nat_filter_dt(bsc, msg, con, parsed);
851 if (filter < 0) {
852 bsc_stat_reject(filter, bsc, 1);
853 bsc_send_con_release(bsc, con);
854 con = NULL;
855 goto exit2;
856 }
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800857 }
Holger Hans Peter Freyther0c41b692010-10-06 00:48:36 +0800858
859 con_bsc = con->bsc;
860 con_msc = con->msc_con;
861 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800862 }
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100863 break;
864 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800865 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800866 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800867 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800868 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800869 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800870 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200871 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100872 break;
873 case SCCP_MSG_TYPE_UDT:
874 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800875 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100876 break;
877 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800878 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 +0800879 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100880 goto exit2;
881 break;
882 }
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +0200883 } else if (parsed->ipa_proto == IPAC_PROTO_MGCP) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200884 bsc_mgcp_forward(bsc, msg);
885 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800886 } else {
887 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
888 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100889 }
890
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800891 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800892 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 +0800893 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100894 goto exit2;
895 }
896
Holger Hans Peter Freyther09ecda42010-09-15 17:39:44 +0800897 /* do not forward messages to the MSC */
898 if (con_filter)
899 goto exit2;
900
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800901 if (!con_msc) {
Holger Hans Peter Freyther27640fc2010-09-15 00:14:48 +0800902 LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n",
903 bsc->cfg->nr,
904 parsed ? parsed->ipa_proto : -1,
905 parsed ? parsed->sccp_type : -1);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800906 goto exit2;
907 }
908
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100909 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800910 queue_for_msc(con_msc, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100911 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800912 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800913
914exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100915 /* if we filter out the reset send an ack to the BSC */
916 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100917 send_reset_ack(bsc);
918 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800919 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
920 /* do we know who is handling this? */
921 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
922 struct tlv_parsed tvp;
923 ipaccess_idtag_parse(&tvp,
924 (unsigned char *) msg->l2h + 2,
925 msgb_l2len(msg) - 2);
926 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
927 ipaccess_auth_bsc(&tvp, bsc);
928 }
929
930 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100931 }
932
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +0800933exit2:
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800934 if (imsi)
935 talloc_free(imsi);
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +0800936 talloc_free(parsed);
937 msgb_free(msg);
938 return -1;
939
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800940exit3:
941 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800942 if (imsi)
943 talloc_free(imsi);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800944 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800945 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800946 msgb_free(msg);
947 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100948}
949
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800950static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100951{
952 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100953 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100954 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800955 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100956
957 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800958 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800959 LOGP(DNAT, LOGL_ERROR,
960 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
961 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800962 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800963 LOGP(DNAT, LOGL_ERROR,
964 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
965 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800966
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800967 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100968 return -1;
969 }
970
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100971
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100972 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 +0100973
974 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800975 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800976
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800977 /* stop the pong timeout */
978 if (hh->proto == IPAC_PROTO_IPACCESS) {
979 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800980 bsc_del_timer(&bsc->pong_timeout);
981 msgb_free(msg);
982 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800983 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
984 send_pong(bsc);
985 msgb_free(msg);
986 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800987 }
988 }
989
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100990 /* FIXME: Currently no PONG is sent to the BSC */
991 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100992 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100993
994 return 0;
995}
996
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100997static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
998{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100999 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001000 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001001 struct sockaddr_in sa;
1002 socklen_t sa_len = sizeof(sa);
1003
1004 if (!(what & BSC_FD_READ))
1005 return 0;
1006
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001007 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
1008 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001009 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001010 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001011 }
1012
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +02001013 /* count the reconnect */
1014 counter_inc(nat->stats.bsc.reconn);
1015
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001016 /*
1017 * if we are not connected to a msc... just close the socket
1018 */
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +08001019 if (!bsc_nat_msc_is_connected(nat)) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001020 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001021 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +02001022 return 0;
1023 }
1024
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001025 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +08001026 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001027 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +08001028 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
1029
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001030 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +08001031 &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp));
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +08001032 if (rc != 0)
1033 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
1034
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001035 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +01001036 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001037
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001038 /*
1039 *
1040 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001041 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001042 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001043 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001044 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001045 return -1;
1046 }
1047
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001048 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001049 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001050 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +02001051 bsc->write_queue.write_cb = bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +08001052 bsc->write_queue.bfd.when = BSC_FD_READ;
1053 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +01001054 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001055 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +01001056 talloc_free(bsc);
1057 return -2;
1058 }
1059
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +08001060 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +08001061 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001062 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +01001063 send_id_ack(bsc);
1064 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +02001065 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +08001066
1067 /*
1068 * start the hangup timer
1069 */
1070 bsc->id_timeout.data = bsc;
1071 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +08001072 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001073 return 0;
1074}
1075
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001076static void print_usage()
1077{
1078 printf("Usage: bsc_nat\n");
1079}
1080
1081static void print_help()
1082{
1083 printf(" Some useful help...\n");
1084 printf(" -h --help this text\n");
1085 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +02001086 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001087 printf(" -s --disable-color\n");
1088 printf(" -c --config-file filename The config file to use.\n");
1089 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001090 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001091}
1092
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001093static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001094{
1095 while (1) {
1096 int option_index = 0, c;
1097 static struct option long_options[] = {
1098 {"help", 0, 0, 'h'},
1099 {"debug", 1, 0, 'd'},
1100 {"config-file", 1, 0, 'c'},
1101 {"disable-color", 0, 0, 's'},
1102 {"timestamp", 0, 0, 'T'},
1103 {"msc", 1, 0, 'm'},
1104 {"local", 1, 0, 'l'},
1105 {0, 0, 0, 0}
1106 };
1107
1108 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1109 long_options, &option_index);
1110 if (c == -1)
1111 break;
1112
1113 switch (c) {
1114 case 'h':
1115 print_usage();
1116 print_help();
1117 exit(0);
1118 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001119 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001120 break;
1121 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001122 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001123 break;
1124 case 'c':
1125 config_file = strdup(optarg);
1126 break;
1127 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001128 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001129 break;
1130 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001131 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001132 break;
1133 case 'l':
1134 inet_aton(optarg, &local_addr);
1135 break;
1136 default:
1137 /* ignore */
1138 break;
1139 }
1140 }
1141}
1142
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001143static void signal_handler(int signal)
1144{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001145 switch (signal) {
1146 case SIGABRT:
1147 /* in case of abort, we want to obtain a talloc report
1148 * and then return to the caller, who will abort the process */
1149 case SIGUSR1:
1150 talloc_report_full(tall_bsc_ctx, stderr);
1151 break;
1152 default:
1153 break;
1154 }
1155}
1156
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001157static void sccp_close_unconfirmed(void *_data)
1158{
1159 struct sccp_connections *conn, *tmp1;
1160 struct timespec now;
1161 clock_gettime(CLOCK_MONOTONIC, &now);
1162
1163 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1164 if (conn->has_remote_ref)
1165 continue;
1166
1167 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1168 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1169 continue;
1170
1171 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1172 sccp_src_ref_to_int(&conn->real_ref),
1173 sccp_src_ref_to_int(&conn->patched_ref));
1174 sccp_connection_destroy(conn);
1175 }
1176
1177 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1178}
1179
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001180extern void *tall_msgb_ctx;
1181extern void *tall_ctr_ctx;
1182static void talloc_init_ctx()
1183{
1184 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1185 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1186 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1187}
1188
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001189extern enum node_type bsc_vty_go_parent(struct vty *vty);
1190
1191static struct vty_app_info vty_info = {
1192 .name = "BSC NAT",
1193 .version = PACKAGE_VERSION,
1194 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +08001195 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001196};
1197
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001198int main(int argc, char **argv)
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001199{
Harald Welte2c869ef2010-08-25 19:43:54 +02001200 int rc;
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001201
Harald Welte2c869ef2010-08-25 19:43:54 +02001202 talloc_init_ctx();
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001203
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001204 log_init(&log_info);
1205 stderr_target = log_target_create_stderr();
1206 log_add_target(stderr_target);
1207 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001208
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001209 nat = bsc_nat_alloc();
1210 if (!nat) {
1211 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1212 return -4;
1213 }
1214
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +00001215 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001216 if (!nat->mgcp_cfg) {
1217 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1218 return -5;
1219 }
1220
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001221 vty_info.copyright = openbsc_copyright;
1222 vty_init(&vty_info);
1223 logging_vty_add_cmds();
1224 bsc_nat_vty_init(nat);
1225
1226
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001227 /* parse options */
1228 local_addr.s_addr = INADDR_ANY;
1229 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001230
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001231 rate_ctr_init(tall_bsc_ctx);
1232
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001233 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001234 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001235 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001236 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1237 return -3;
1238 }
1239
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001240 /* over rule the VTY config */
1241 if (msc_ip)
1242 bsc_nat_set_msc_ip(nat, msc_ip);
1243
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001244 /* seed the PRNG */
1245 srand(time(NULL));
1246
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001247 /*
1248 * Setup the MGCP code..
1249 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001250 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001251 return -4;
1252
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001253 /* connect to the MSC */
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +08001254 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001255 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001256 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001257 exit(1);
1258 }
1259
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001260 nat->msc_con->connection_loss = msc_connection_was_lost;
Holger Hans Peter Freythera99c5b92010-08-04 02:31:55 +08001261 nat->msc_con->connected = msc_connection_connected;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001262 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1263 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +08001264 nat->msc_con->write_queue.bfd.data = nat->msc_con;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001265 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001266
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001267 /* wait for the BSC */
Holger Hans Peter Freythera25d5792010-10-19 17:48:13 +02001268 rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
Holger Hans Peter Freytherf961de12010-10-12 23:28:28 +02001269 5000, ipaccess_listen_bsc_cb);
1270 if (rc != 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001271 fprintf(stderr, "Failed to listen for BSC.\n");
1272 exit(1);
1273 }
1274
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001275 signal(SIGABRT, &signal_handler);
1276 signal(SIGUSR1, &signal_handler);
1277 signal(SIGPIPE, SIG_IGN);
1278
Harald Welte2c869ef2010-08-25 19:43:54 +02001279 if (daemonize) {
1280 rc = osmo_daemonize();
1281 if (rc < 0) {
1282 perror("Error during daemonize");
1283 exit(1);
1284 }
1285 }
1286
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001287 /* recycle timer */
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001288 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001289 sccp_close.cb = sccp_close_unconfirmed;
1290 sccp_close.data = NULL;
1291 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1292
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001293 while (1) {
1294 bsc_select_main(0);
1295 }
1296
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001297 return 0;
1298}