blob: 356025c9816ce19256a33ed87735b1b007fd9ac3 [file] [log] [blame]
Holger Hans Peter Freyther89d9fd92010-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 Freyther98e49d42010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther89d9fd92010-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 Freyther150fa582010-05-05 18:58:13 +080026#include <netinet/tcp.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080027#include <arpa/inet.h>
28
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +010029#include <errno.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010030#include <signal.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080031#include <stdio.h>
32#include <stdlib.h>
Holger Hans Peter Freytherfd012d52010-01-12 21:36:08 +010033#include <time.h>
Holger Hans Peter Freyther89d9fd92010-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 Freythere907cb22010-01-12 21:15:08 +010040#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +080044#include <openbsc/vty.h>
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080045
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +080046#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080047#include <osmocore/talloc.h>
48
Holger Hans Peter Freythere36be812010-06-15 20:13:33 +080049#include <osmocore/protocol/gsm_08_08.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080050
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +080051#include <osmocom/vty/telnet_interface.h>
52#include <osmocom/vty/vty.h>
53
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080054#include <sccp/sccp.h>
55
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +080056#include "../../bscconfig.h"
57
Holger Hans Peter Freyther6088cc72010-06-15 18:51:56 +080058#define SCCP_CLOSE_TIME 20
59#define SCCP_CLOSE_TIME_TIMEOUT 19
60
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +080061struct log_target *stderr_target;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080062static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080063static struct in_addr local_addr;
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +010064static struct bsc_fd bsc_listen;
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +080065static const char *msc_ip = NULL;
Holger Hans Peter Freyther6088cc72010-06-15 18:51:56 +080066static struct timer_list sccp_close;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010067
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +080068const char *openbsc_copyright =
69 "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\n"
70 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
71 "This is free software: you are free to change and redistribute it.\n"
72 "There is NO WARRANTY, to the extent permitted by law.\n";
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010073
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080074static struct bsc_nat *nat;
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +020075static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int);
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +020076static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080077
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080078struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
79{
80 struct bsc_config *conf;
81
82 llist_for_each_entry(conf, &nat->bsc_configs, entry)
83 if (conf->nr == num)
84 return conf;
85
86 return NULL;
87}
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010088
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010089/*
90 * below are stubs we need to link
91 */
92int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
Holger Hans Peter Freytherc4aa2702010-05-12 16:55:12 +080093 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state,
94 struct abis_om_obj_inst *obj_ins)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010095{
96 return -1;
97}
98
99void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
100{}
101
Holger Hans Peter Freyther754787c2010-04-22 19:11:37 +0800102static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
103{
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800104 if (write_queue_enqueue(&nat->msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther754787c2010-04-22 19:11:37 +0800105 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
106 msgb_free(msg);
107 }
108}
109
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100110static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100111{
112 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100113 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
114 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
115 0x00, 0x01, 0x31,
116 };
117
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200118 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100119}
120
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800121static void send_ping(struct bsc_connection *bsc)
122{
123 static const u_int8_t id_ping[] = {
124 IPAC_MSGT_PING,
125 };
126
127 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
128}
129
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800130static void send_pong(struct bsc_connection *bsc)
131{
132 static const u_int8_t id_pong[] = {
133 IPAC_MSGT_PONG,
134 };
135
136 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
137}
138
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800139static void bsc_pong_timeout(void *_bsc)
140{
141 struct bsc_connection *bsc = _bsc;
142
143 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
144 bsc_close_connection(bsc);
145}
146
147static void bsc_ping_timeout(void *_bsc)
148{
149 struct bsc_connection *bsc = _bsc;
150
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800151 if (bsc->nat->ping_timeout < 0)
152 return;
153
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800154 send_ping(bsc);
155
156 /* send another ping in 20 seconds */
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800157 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800158
159 /* also start a pong timer */
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800160 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800161}
162
163static void start_ping_pong(struct bsc_connection *bsc)
164{
165 bsc->pong_timeout.data = bsc;
166 bsc->pong_timeout.cb = bsc_pong_timeout;
167 bsc->ping_timeout.data = bsc;
168 bsc->ping_timeout.cb = bsc_ping_timeout;
169
170 bsc_ping_timeout(bsc);
171}
172
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100173static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100174{
175 static const u_int8_t id_ack[] = {
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200176 IPAC_MSGT_ID_ACK
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100177 };
178
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200179 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100180}
181
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100182static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100183{
184 static const u_int8_t id_req[] = {
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200185 IPAC_MSGT_ID_GET,
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100186 0x01, IPAC_IDTAG_UNIT,
187 0x01, IPAC_IDTAG_MACADDR,
188 0x01, IPAC_IDTAG_LOCATION1,
189 0x01, IPAC_IDTAG_LOCATION2,
190 0x01, IPAC_IDTAG_EQUIPVERS,
191 0x01, IPAC_IDTAG_SWVERSION,
192 0x01, IPAC_IDTAG_UNITNAME,
193 0x01, IPAC_IDTAG_SERNR,
194 };
195
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200196 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100197}
198
Holger Hans Peter Freyther7746bd82010-04-06 17:32:58 +0200199static void nat_send_rlsd(struct sccp_connections *conn)
200{
201 struct sccp_connection_released *rel;
202 struct msgb *msg;
203
204 msg = msgb_alloc_headroom(4096, 128, "rlsd");
205 if (!msg) {
206 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
207 return;
208 }
209
210 msg->l2h = msgb_put(msg, sizeof(*rel));
211 rel = (struct sccp_connection_released *) msg->l2h;
212 rel->type = SCCP_MSG_TYPE_RLSD;
213 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
214 rel->destination_local_reference = conn->remote_ref;
215 rel->source_local_reference = conn->patched_ref;
216
217 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
218
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800219 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther7746bd82010-04-06 17:32:58 +0200220}
221
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200222static void nat_send_rlc(struct sccp_source_reference *src,
223 struct sccp_source_reference *dst)
224{
225 struct sccp_connection_release_complete *rlc;
226 struct msgb *msg;
227
228 msg = msgb_alloc_headroom(4096, 128, "rlc");
229 if (!msg) {
230 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
231 return;
232 }
233
234 msg->l2h = msgb_put(msg, sizeof(*rlc));
235 rlc = (struct sccp_connection_release_complete *) msg->l2h;
236 rlc->type = SCCP_MSG_TYPE_RLC;
237 rlc->destination_local_reference = *dst;
238 rlc->source_local_reference = *src;
239
240 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
241
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800242 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200243}
244
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200245static void send_mgcp_reset(struct bsc_connection *bsc)
246{
247 static const u_int8_t mgcp_reset[] = {
248 "RSIP 1 13@mgw MGCP 1.0\r\n"
249 };
250
251 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
252}
253
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100254/*
255 * Below is the handling of messages coming
256 * from the MSC and need to be forwarded to
257 * a real BSC.
258 */
259static void initialize_msc_if_needed()
260{
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200261 if (nat->first_contact)
262 return;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100263
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200264 nat->first_contact = 1;
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800265 msc_send_reset(nat->msc_con);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100266}
267
Holger Hans Peter Freyther6e88b172010-05-15 00:14:58 +0800268static void send_id_get_response()
269{
270 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
271 if (!msg)
272 return;
273
274 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
275 queue_for_msc(nat->msc_con, msg);
276}
277
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100278/*
279 * Currently we are lacking refcounting so we need to copy each message.
280 */
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200281static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int proto)
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100282{
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100283 struct msgb *msg;
284
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200285 if (length > 4096 - 128) {
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100286 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
287 return;
288 }
289
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200290 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100291 if (!msg) {
292 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
293 return;
294 }
295
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200296 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100297 memcpy(msg->data, data, length);
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200298
299 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100300}
301
Holger Hans Peter Freytherfa412e52010-05-16 02:20:26 +0800302static void bsc_send_con_refuse(struct bsc_connection *bsc,
303 struct bsc_nat_parsed *parsed, int con_type)
304{
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800305 struct msgb *payload;
Holger Hans Peter Freytherfa412e52010-05-16 02:20:26 +0800306 struct msgb *refuse;
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800307
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800308 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800309 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800310 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800311 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800312
313 /*
314 * Some BSCs do not handle the payload inside a SCCP CREF msg
315 * so we will need to:
316 * 1.) Allocate a local connection and mark it as local..
317 * 2.) queue data for downstream.. and the RLC should delete everything
318 */
319 if (payload) {
320 struct msgb *cc, *udt, *rlsd;
321 struct sccp_connections *con;
322 con = create_sccp_src_ref(bsc, parsed);
323 if (!con)
324 goto send_refuse;
325
326 /* declare it local and assign a unique remote_ref */
327 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
328 con->con_local = 1;
329 con->has_remote_ref = 1;
330 con->remote_ref = con->patched_ref;
331
332 /* 1. create a confirmation */
333 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
334 if (!cc)
335 goto send_refuse;
336
337 /* 2. create the DT1 */
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800338 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800339 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
340 if (!udt) {
341 msgb_free(cc);
342 goto send_refuse;
343 }
344
345 /* 3. send a RLSD */
346 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
347 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
348 if (!rlsd) {
349 msgb_free(cc);
350 msgb_free(udt);
351 goto send_refuse;
352 }
353
354 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
355 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
356 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
357 msgb_free(payload);
358 return;
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800359 }
360
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800361
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800362send_refuse:
Holger Hans Peter Freyther605eba72010-06-15 18:52:24 +0800363 if (payload)
364 msgb_free(payload);
365
Holger Hans Peter Freyther15a1f992010-05-16 20:43:52 +0800366 refuse = sccp_create_refuse(parsed->src_local_ref,
367 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freytherfa412e52010-05-16 02:20:26 +0800368 if (!refuse) {
369 LOGP(DNAT, LOGL_ERROR,
370 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
371 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
372 return;
373 }
374
375 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
376}
377
378
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100379static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100380{
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800381 struct sccp_connections *con;
382 struct bsc_connection *bsc;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800383 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200384 int proto;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100385
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100386 /* filter, drop, patch the message? */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800387 parsed = bsc_nat_parse(msg);
388 if (!parsed) {
389 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100390 return -1;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800391 }
392
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100393 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800394 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800395
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200396 proto = parsed->ipa_proto;
397
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100398 /* Route and modify the SCCP packet */
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200399 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100400 switch (parsed->sccp_type) {
401 case SCCP_MSG_TYPE_UDT:
402 /* forward UDT messages to every BSC */
403 goto send_to_all;
404 break;
405 case SCCP_MSG_TYPE_RLSD:
406 case SCCP_MSG_TYPE_CREF:
407 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther567e8402010-04-06 10:22:34 +0200408 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800409 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800410 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200411 counter_inc(nat->stats.sccp.calls);
412
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800413 if (con) {
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +0800414 struct rate_ctr_group *ctrg;
415 ctrg = con->bsc->cfg->stats.ctrg;
416 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800417 if (bsc_mgcp_assign(con, msg) != 0)
418 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
419 } else
420 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
421 }
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200422 break;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100423 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800424 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
425 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200426 goto exit;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800427 break;
428 case SCCP_MSG_TYPE_RLC:
429 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
430 goto exit;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100431 break;
432 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100433 /* MSC never opens a SCCP connection, fall through */
434 default:
435 goto exit;
436 }
Holger Hans Peter Freyther8d298282010-04-06 16:07:44 +0200437
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200438 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
439 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
440 /* Exchange src/dest for the reply */
441 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
442 } else if (!con)
Holger Hans Peter Freyther117e7a72010-05-15 19:13:52 +0800443 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x from the MSC.\n", parsed->sccp_type);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100444 }
445
446 talloc_free(parsed);
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800447 if (!con)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100448 return -1;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800449 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800450 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100451 return -1;
452 }
453
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200454 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100455 return 0;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100456
457send_to_all:
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800458 /*
459 * Filter Paging from the network. We do not want to send a PAGING
460 * Command to every BSC in our network. We will analys the PAGING
461 * message and then send it to the authenticated messages...
462 */
463 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200464 int lac;
465 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther7acf4622010-04-21 19:05:14 +0800466 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freyther19ccb482010-04-21 20:17:18 +0800467 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther7acf4622010-04-21 19:05:14 +0800468 else if (bsc)
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200469 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200470 else
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200471 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
472 lac, lac);
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800473
474 goto exit;
475 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100476 /* currently send this to every BSC connected */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800477 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100478 if (!bsc->authenticated)
479 continue;
480
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200481 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100482 }
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800483
484exit:
485 talloc_free(parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100486 return 0;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100487}
488
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800489static void msc_connection_was_lost(struct bsc_msc_connection *con)
490{
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200491 struct bsc_connection *bsc, *tmp;
492
Holger Hans Peter Freyther5b870612010-04-12 12:35:02 +0200493 counter_inc(nat->stats.msc.reconn);
494
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200495 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
496 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800497 bsc_close_connection(bsc);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200498
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200499 nat->first_contact = 0;
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +0200500 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200501 bsc_msc_schedule_connect(con);
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800502}
503
Holger Hans Peter Freyther8f515a02010-04-08 10:31:07 +0200504static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200505{
506 static const u_int8_t reset[] = {
Holger Hans Peter Freytherc48a5d92010-04-07 11:11:11 +0200507 0x00, 0x12, 0xfd,
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200508 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
509 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
510 0x01, 0x20
511 };
512
513 struct msgb *msg;
514
515 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
516 if (!msg) {
517 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
518 return;
519 }
520
521 msg->l2h = msgb_put(msg, sizeof(reset));
522 memcpy(msg->l2h, reset, msgb_l2len(msg));
523
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800524 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200525
526 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
527}
528
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800529static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100530{
531 int error;
532 struct msgb *msg = ipaccess_read_msg(bfd, &error);
533 struct ipaccess_head *hh;
534
535 if (!msg) {
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800536 if (error == 0)
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100537 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800538 else
539 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100540
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800541 bsc_msc_lost(nat->msc_con);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100542 return -1;
543 }
544
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100545 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100546
547 /* handle base message handling */
548 hh = (struct ipaccess_head *) msg->data;
549 ipaccess_rcvmsg_base(msg, bfd);
550
551 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freyther6e88b172010-05-15 00:14:58 +0800552 if (hh->proto == IPAC_PROTO_IPACCESS) {
553 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
554 initialize_msc_if_needed();
555 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
556 send_id_get_response();
557 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100558 forward_sccp_to_bts(msg);
559
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800560 msgb_free(msg);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100561 return 0;
562}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800563
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800564static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
565{
566 int rc;
567 rc = write(bfd->fd, msg->data, msg->len);
568
569 if (rc != msg->len) {
570 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
571 return -1;
572 }
573
574 return rc;
575}
576
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100577/*
578 * Below is the handling of messages coming
579 * from the BSC and need to be forwarded to
580 * a real BSC.
581 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100582
583/*
584 * Remove the connection from the connections list,
585 * remove it from the patching of SCCP header lists
586 * as well. Maybe in the future even close connection..
587 */
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800588void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100589{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100590 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +0800591 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100592
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800593 /* stop the timeout timer */
594 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800595 bsc_del_timer(&connection->ping_timeout);
596 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800597
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +0800598 if (connection->cfg)
599 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
600
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100601 /* remove all SCCP connections */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800602 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100603 if (sccp_patch->bsc != connection)
604 continue;
605
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +0800606 if (ctr)
607 rate_ctr_inc(ctr);
Holger Hans Peter Freyther5975c682010-04-23 14:13:27 +0800608 if (sccp_patch->has_remote_ref)
609 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200610 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100611 }
612
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +0200613 /* close endpoints allocated by this BSC */
614 bsc_mgcp_clear_endpoints_for(connection);
615
Holger Hans Peter Freyther53cd9852010-04-22 12:04:36 +0800616 bsc_unregister_fd(&connection->write_queue.bfd);
617 close(connection->write_queue.bfd.fd);
618 write_queue_clear(&connection->write_queue);
619 llist_del(&connection->list_entry);
620
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100621 talloc_free(connection);
622}
623
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800624static void ipaccess_close_bsc(void *data)
625{
Holger Hans Peter Freyther3626e972010-04-17 07:58:17 +0200626 struct sockaddr_in sock;
627 socklen_t len = sizeof(sock);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800628 struct bsc_connection *conn = data;
629
Holger Hans Peter Freyther3626e972010-04-17 07:58:17 +0200630
631 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
632 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
633 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800634 bsc_close_connection(conn);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800635}
636
637static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
638{
639 struct bsc_config *conf;
640 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
641
Holger Hans Peter Freyther4dc44ea2010-05-02 18:58:10 +0800642 if (bsc->cfg) {
643 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
644 bsc->write_queue.bfd.fd, bsc->cfg->nr);
645 return;
646 }
647
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800648 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
649 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +0800650 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800651 bsc->authenticated = 1;
Holger Hans Peter Freytherbfcf5192010-04-06 15:11:34 +0200652 bsc->cfg = conf;
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800653 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800654 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
655 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800656 start_ping_pong(bsc);
Holger Hans Peter Freyther8b2302f2010-04-17 07:59:57 +0200657 return;
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800658 }
659 }
Holger Hans Peter Freyther8b2302f2010-04-17 07:59:57 +0200660
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800661 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
662 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800663}
664
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100665static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100666{
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800667 int con_found = 0;
Holger Hans Peter Freytherc3dccc72010-05-16 16:36:36 +0800668 int con_filter = 0;
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800669 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800670 int con_type;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800671 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100672
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800673 /* Parse and filter messages */
674 parsed = bsc_nat_parse(msg);
675 if (!parsed) {
676 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther45f62952010-03-30 07:35:46 +0200677 msgb_free(msg);
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800678 return -1;
679 }
680
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100681 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800682 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800683
Holger Hans Peter Freyther5a723b62010-04-02 03:42:44 +0200684 /*
685 * check authentication after filtering to not reject auth
686 * responses coming from the BSC. We have to make sure that
687 * nothing from the exit path will forward things to the MSC
688 */
689 if (!bsc->authenticated) {
690 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
691 msgb_free(msg);
692 return -1;
693 }
694
695
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100696 /* modify the SCCP entries */
697 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800698 struct sccp_connections *con;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100699 switch (parsed->sccp_type) {
700 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freytherd5a2bc42010-05-16 02:00:40 +0800701 if (bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type) != 0)
Holger Hans Peter Freyther6d357162010-05-14 03:39:56 +0800702 goto exit3;
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800703 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100704 goto exit2;
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800705 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther8e69f0c2010-05-16 02:06:11 +0800706 con->con_type = con_type;
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800707 con_found = 1;
708 con_bsc = con->bsc;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100709 break;
710 case SCCP_MSG_TYPE_RLSD:
711 case SCCP_MSG_TYPE_CREF:
712 case SCCP_MSG_TYPE_DT1:
713 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther567e8402010-04-06 10:22:34 +0200714 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800715 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800716 if (con) {
717 con_found = 1;
718 con_bsc = con->bsc;
Holger Hans Peter Freytherc3dccc72010-05-16 16:36:36 +0800719 con_filter = con->con_local;
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800720 }
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100721 break;
722 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800723 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800724 if (con) {
725 con_found = 1;
726 con_bsc = con->bsc;
Holger Hans Peter Freytherc3dccc72010-05-16 16:36:36 +0800727 con_filter = con->con_local;
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800728 }
Holger Hans Peter Freyther38712d02010-03-31 09:15:05 +0200729 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100730 break;
731 case SCCP_MSG_TYPE_UDT:
732 /* simply forward everything */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800733 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100734 break;
735 default:
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800736 LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type);
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800737 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100738 goto exit2;
739 break;
740 }
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200741 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
742 bsc_mgcp_forward(bsc, msg);
743 goto exit2;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800744 } else {
745 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
746 goto exit2;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100747 }
748
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800749 if (con_found && con_bsc != bsc) {
Holger Hans Peter Freyther4107f982010-04-21 18:49:55 +0800750 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
Holger Hans Peter Freytherdf961532010-05-16 16:30:42 +0800751 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100752 goto exit2;
753 }
754
Holger Hans Peter Freytherc3dccc72010-05-16 16:36:36 +0800755 /* do not forward messages to the MSC */
756 if (con_filter)
757 goto exit2;
758
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100759 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800760 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100761 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800762 return 0;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800763
764exit:
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100765 /* if we filter out the reset send an ack to the BSC */
766 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100767 send_reset_ack(bsc);
768 send_reset_ack(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800769 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
770 /* do we know who is handling this? */
771 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
772 struct tlv_parsed tvp;
773 ipaccess_idtag_parse(&tvp,
774 (unsigned char *) msg->l2h + 2,
775 msgb_l2len(msg) - 2);
776 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
777 ipaccess_auth_bsc(&tvp, bsc);
778 }
779
780 goto exit2;
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100781 }
782
Holger Hans Peter Freyther171d9302010-05-15 00:29:50 +0800783exit2:
784 talloc_free(parsed);
785 msgb_free(msg);
786 return -1;
787
Holger Hans Peter Freyther6d357162010-05-14 03:39:56 +0800788exit3:
789 /* send a SCCP Connection Refused */
Holger Hans Peter Freytherfa412e52010-05-16 02:20:26 +0800790 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800791 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800792 msgb_free(msg);
793 return -1;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100794}
795
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800796static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100797{
798 int error;
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100799 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100800 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800801 struct ipaccess_head *hh;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100802
803 if (!msg) {
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800804 if (error == 0)
Holger Hans Peter Freyther3c8c8152010-05-01 10:37:15 +0800805 LOGP(DNAT, LOGL_ERROR,
806 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
807 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800808 else
Holger Hans Peter Freyther3c8c8152010-05-01 10:37:15 +0800809 LOGP(DNAT, LOGL_ERROR,
810 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
811 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800812
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800813 bsc_close_connection(bsc);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100814 return -1;
815 }
816
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100817
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100818 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100819
820 /* Handle messages from the BSC */
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800821 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800822
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800823 /* stop the pong timeout */
824 if (hh->proto == IPAC_PROTO_IPACCESS) {
825 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800826 bsc_del_timer(&bsc->pong_timeout);
827 msgb_free(msg);
828 return 0;
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800829 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
830 send_pong(bsc);
831 msgb_free(msg);
832 return 0;
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800833 }
834 }
835
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100836 /* FIXME: Currently no PONG is sent to the BSC */
837 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100838 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100839
840 return 0;
841}
842
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100843static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
844{
845 int rc;
846
847 rc = write(bfd->fd, msg->data, msg->len);
848 if (rc != msg->len)
849 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
850
851 return rc;
852}
853
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100854static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
855{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100856 struct bsc_connection *bsc;
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800857 int fd, rc, on;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100858 struct sockaddr_in sa;
859 socklen_t sa_len = sizeof(sa);
860
861 if (!(what & BSC_FD_READ))
862 return 0;
863
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800864 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
865 if (fd < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100866 perror("accept");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800867 return fd;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100868 }
869
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200870 /* count the reconnect */
871 counter_inc(nat->stats.bsc.reconn);
872
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200873 /*
874 * if we are not connected to a msc... just close the socket
875 */
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800876 if (!nat->msc_con->is_connected) {
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200877 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800878 close(fd);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200879 return 0;
880 }
881
Holger Hans Peter Freyther150fa582010-05-05 18:58:13 +0800882 on = 1;
Holger Hans Peter Freytherd58afeb2010-05-05 20:42:14 +0800883 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800884 if (rc != 0)
Holger Hans Peter Freyther150fa582010-05-05 18:58:13 +0800885 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
886
Holger Hans Peter Freyther9d42d322010-05-31 10:36:35 +0800887 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
888 &nat->bsc_ip_tos, sizeof(nat->bsc_ip_tos));
889 if (rc != 0)
890 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
891
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100892 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100893 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100894
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100895 /*
896 *
897 */
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800898 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100899 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100900 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800901 close(fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100902 return -1;
903 }
904
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800905 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800906 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800907 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100908 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800909 bsc->write_queue.bfd.when = BSC_FD_READ;
910 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100911 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800912 close(fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100913 talloc_free(bsc);
914 return -2;
915 }
916
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800917 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800918 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800919 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100920 send_id_ack(bsc);
921 send_id_req(bsc);
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200922 send_mgcp_reset(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800923
924 /*
925 * start the hangup timer
926 */
927 bsc->id_timeout.data = bsc;
928 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800929 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100930 return 0;
931}
932
933static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
934{
935 struct sockaddr_in addr;
936 int ret, on = 1;
937
938 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
939 bfd->cb = ipaccess_listen_bsc_cb;
940 bfd->when = BSC_FD_READ;
941
942 memset(&addr, 0, sizeof(addr));
943 addr.sin_family = AF_INET;
944 addr.sin_port = htons(port);
945 addr.sin_addr.s_addr = in_addr->s_addr;
946
947 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
948
949 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
950 if (ret < 0) {
951 fprintf(stderr, "Could not bind the BSC socket %s\n",
952 strerror(errno));
953 return -EIO;
954 }
955
956 ret = listen(bfd->fd, 1);
957 if (ret < 0) {
958 perror("listen");
959 return ret;
960 }
961
962 ret = bsc_register_fd(bfd);
963 if (ret < 0) {
964 perror("register_listen_fd");
965 return ret;
966 }
967 return 0;
968}
969
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800970static void print_usage()
971{
972 printf("Usage: bsc_nat\n");
973}
974
975static void print_help()
976{
977 printf(" Some useful help...\n");
978 printf(" -h --help this text\n");
979 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
980 printf(" -s --disable-color\n");
981 printf(" -c --config-file filename The config file to use.\n");
982 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100983 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800984}
985
986static void handle_options(int argc, char** argv)
987{
988 while (1) {
989 int option_index = 0, c;
990 static struct option long_options[] = {
991 {"help", 0, 0, 'h'},
992 {"debug", 1, 0, 'd'},
993 {"config-file", 1, 0, 'c'},
994 {"disable-color", 0, 0, 's'},
995 {"timestamp", 0, 0, 'T'},
996 {"msc", 1, 0, 'm'},
997 {"local", 1, 0, 'l'},
998 {0, 0, 0, 0}
999 };
1000
1001 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1002 long_options, &option_index);
1003 if (c == -1)
1004 break;
1005
1006 switch (c) {
1007 case 'h':
1008 print_usage();
1009 print_help();
1010 exit(0);
1011 case 's':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +08001012 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001013 break;
1014 case 'd':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +08001015 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001016 break;
1017 case 'c':
1018 config_file = strdup(optarg);
1019 break;
1020 case 'T':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +08001021 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001022 break;
1023 case 'm':
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +08001024 msc_ip = optarg;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001025 break;
1026 case 'l':
1027 inet_aton(optarg, &local_addr);
1028 break;
1029 default:
1030 /* ignore */
1031 break;
1032 }
1033 }
1034}
1035
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +01001036static void signal_handler(int signal)
1037{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +01001038 switch (signal) {
1039 case SIGABRT:
1040 /* in case of abort, we want to obtain a talloc report
1041 * and then return to the caller, who will abort the process */
1042 case SIGUSR1:
1043 talloc_report_full(tall_bsc_ctx, stderr);
1044 break;
1045 default:
1046 break;
1047 }
1048}
1049
Holger Hans Peter Freyther6088cc72010-06-15 18:51:56 +08001050static void sccp_close_unconfirmed(void *_data)
1051{
1052 struct sccp_connections *conn, *tmp1;
1053 struct timespec now;
1054 clock_gettime(CLOCK_MONOTONIC, &now);
1055
1056 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1057 if (conn->has_remote_ref)
1058 continue;
1059
1060 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1061 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1062 continue;
1063
1064 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1065 sccp_src_ref_to_int(&conn->real_ref),
1066 sccp_src_ref_to_int(&conn->patched_ref));
1067 sccp_connection_destroy(conn);
1068 }
1069
1070 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1071}
1072
Holger Hans Peter Freyther023a5502010-06-15 18:51:25 +08001073extern void *tall_msgb_ctx;
1074extern void *tall_ctr_ctx;
1075static void talloc_init_ctx()
1076{
1077 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1078 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1079 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1080}
1081
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +08001082extern enum node_type bsc_vty_go_parent(struct vty *vty);
1083
1084static struct vty_app_info vty_info = {
1085 .name = "BSC NAT",
1086 .version = PACKAGE_VERSION,
1087 .go_parent_cb = bsc_vty_go_parent,
1088};
1089
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001090int main(int argc, char** argv)
1091{
Holger Hans Peter Freyther023a5502010-06-15 18:51:25 +08001092 talloc_init_ctx();
1093
Holger Hans Peter Freyther09371382010-06-15 18:51:18 +08001094
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +08001095 log_init(&log_info);
1096 stderr_target = log_target_create_stderr();
1097 log_add_target(stderr_target);
1098 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +08001099
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +08001100 nat = bsc_nat_alloc();
1101 if (!nat) {
1102 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1103 return -4;
1104 }
1105
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +08001106 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +08001107 if (!nat->mgcp_cfg) {
1108 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1109 return -5;
1110 }
1111
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +08001112 vty_info.copyright = openbsc_copyright;
1113 vty_init(&vty_info);
1114 logging_vty_add_cmds();
1115 bsc_nat_vty_init(nat);
1116
1117
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +08001118 /* parse options */
1119 local_addr.s_addr = INADDR_ANY;
1120 handle_options(argc, argv);
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +08001121
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +08001122 rate_ctr_init(tall_bsc_ctx);
1123
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +08001124 /* init vty and parse */
Holger Hans Peter Freyther4fcedba2010-06-15 20:14:08 +08001125 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +08001126 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +08001127 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1128 return -3;
1129 }
1130
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +08001131 /* over rule the VTY config */
1132 if (msc_ip)
1133 bsc_nat_set_msc_ip(nat, msc_ip);
1134
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001135 /* seed the PRNG */
1136 srand(time(NULL));
1137
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +02001138 /*
1139 * Setup the MGCP code..
1140 */
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +08001141 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +02001142 return -4;
1143
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01001144 /* connect to the MSC */
Holger Hans Peter Freyther7ba0c542010-06-15 18:52:38 +08001145 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +08001146 if (!nat->msc_con) {
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +08001147 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +01001148 exit(1);
1149 }
1150
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +08001151 nat->msc_con->connection_loss = msc_connection_was_lost;
1152 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1153 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
1154 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +08001155
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01001156 /* wait for the BSC */
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +01001157 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01001158 fprintf(stderr, "Failed to listen for BSC.\n");
1159 exit(1);
1160 }
1161
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +01001162 signal(SIGABRT, &signal_handler);
1163 signal(SIGUSR1, &signal_handler);
1164 signal(SIGPIPE, SIG_IGN);
1165
Holger Hans Peter Freyther6088cc72010-06-15 18:51:56 +08001166 /* recycle timer */
1167 sccp_close.cb = sccp_close_unconfirmed;
1168 sccp_close.data = NULL;
1169 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1170
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +01001171 while (1) {
1172 bsc_select_main(0);
1173 }
1174
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001175 return 0;
1176}