blob: dc7798b1e09f2a632f93b4e636c64a628d57ecb6 [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 Freyther6ace5222010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080044#include <openbsc/vty.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080045
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +080046#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080047#include <osmocore/talloc.h>
48
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080049#include <osmocore/protocol/gsm_08_08.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080050
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080051#include <osmocom/vty/telnet_interface.h>
52#include <osmocom/vty/vty.h>
53
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080054#include <sccp/sccp.h>
55
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +080056#include "../../bscconfig.h"
57
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080058#define SCCP_CLOSE_TIME 20
59#define SCCP_CLOSE_TIME_TIMEOUT 19
60
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080061struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080062static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080063static struct in_addr local_addr;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010064static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080065static const char *msc_ip = NULL;
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +080066static struct timer_list sccp_close;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010067
Holger Hans Peter Freyther1398f132010-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 Freyther24614ad2010-01-13 09:28:12 +010073
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080074static struct bsc_nat *nat;
Holger Hans Peter Freyther2896df72010-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 Freytherb7527612010-04-07 11:20:36 +020076static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080077
Holger Hans Peter Freyther9a85ef32010-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 Freyther24614ad2010-01-13 09:28:12 +010088
Holger Hans Peter Freyther6ace5222010-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 Freyther2f2d3422010-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 Freyther6ace5222010-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 Freyther9d56d0c2010-04-22 19:11:37 +0800102static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
103{
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800104 if (write_queue_enqueue(&con->write_queue, msg) != 0) {
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800105 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
106 msgb_free(msg);
107 }
108}
109
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100110static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100111{
112 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100113 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
114 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
115 0x00, 0x01, 0x31,
116 };
117
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200118 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100119}
120
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800121static void send_ping(struct bsc_connection *bsc)
122{
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 Freyther50788712010-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 Freyther906c15e2010-05-02 19:28:59 +0800139static void bsc_pong_timeout(void *_bsc)
140{
141 struct bsc_connection *bsc = _bsc;
142
143 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
144 bsc_close_connection(bsc);
145}
146
147static void bsc_ping_timeout(void *_bsc)
148{
149 struct bsc_connection *bsc = _bsc;
150
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800151 if (bsc->nat->ping_timeout < 0)
152 return;
153
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800154 send_ping(bsc);
155
156 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800157 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800158
159 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800160 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800161}
162
163static void start_ping_pong(struct bsc_connection *bsc)
164{
165 bsc->pong_timeout.data = bsc;
166 bsc->pong_timeout.cb = bsc_pong_timeout;
167 bsc->ping_timeout.data = bsc;
168 bsc->ping_timeout.cb = bsc_ping_timeout;
169
170 bsc_ping_timeout(bsc);
171}
172
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100173static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100174{
175 static const u_int8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200176 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100177 };
178
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200179 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100180}
181
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100182static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100183{
184 static const u_int8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200185 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100186 0x01, IPAC_IDTAG_UNIT,
187 0x01, IPAC_IDTAG_MACADDR,
188 0x01, IPAC_IDTAG_LOCATION1,
189 0x01, IPAC_IDTAG_LOCATION2,
190 0x01, IPAC_IDTAG_EQUIPVERS,
191 0x01, IPAC_IDTAG_SWVERSION,
192 0x01, IPAC_IDTAG_UNITNAME,
193 0x01, IPAC_IDTAG_SERNR,
194 };
195
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200196 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100197}
198
Holger Hans Peter Freyther6b087d12010-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 Freytherdb65bbd2010-07-05 13:58:40 +0800219 queue_for_msc(conn->msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200220}
221
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800222static void nat_send_rlc(struct bsc_msc_connection *msc_con,
223 struct sccp_source_reference *src,
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200224 struct sccp_source_reference *dst)
225{
226 struct sccp_connection_release_complete *rlc;
227 struct msgb *msg;
228
229 msg = msgb_alloc_headroom(4096, 128, "rlc");
230 if (!msg) {
231 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
232 return;
233 }
234
235 msg->l2h = msgb_put(msg, sizeof(*rlc));
236 rlc = (struct sccp_connection_release_complete *) msg->l2h;
237 rlc->type = SCCP_MSG_TYPE_RLC;
238 rlc->destination_local_reference = *dst;
239 rlc->source_local_reference = *src;
240
241 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
242
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800243 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200244}
245
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200246static void send_mgcp_reset(struct bsc_connection *bsc)
247{
248 static const u_int8_t mgcp_reset[] = {
249 "RSIP 1 13@mgw MGCP 1.0\r\n"
250 };
251
252 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
253}
254
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100255/*
256 * Below is the handling of messages coming
257 * from the MSC and need to be forwarded to
258 * a real BSC.
259 */
260static void initialize_msc_if_needed()
261{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200262 if (nat->first_contact)
263 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100264
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200265 nat->first_contact = 1;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800266 msc_send_reset(nat->msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100267}
268
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800269static void send_id_get_response(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800270{
271 struct msgb *msg = bsc_msc_id_get_resp(nat->token);
272 if (!msg)
273 return;
274
275 ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800276 queue_for_msc(msc_con, msg);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800277}
278
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100279/*
280 * Currently we are lacking refcounting so we need to copy each message.
281 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200282static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int proto)
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100283{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100284 struct msgb *msg;
285
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200286 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100287 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
288 return;
289 }
290
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200291 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100292 if (!msg) {
293 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
294 return;
295 }
296
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200297 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100298 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200299
300 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100301}
302
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800303static void bsc_send_con_refuse(struct bsc_connection *bsc,
304 struct bsc_nat_parsed *parsed, int con_type)
305{
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800306 struct msgb *payload;
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800307 struct msgb *refuse;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800308
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800309 if (con_type == NAT_CON_TYPE_LU)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800310 payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800311 else if (con_type == NAT_CON_TYPE_CM_SERV_REQ)
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800312 payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800313
314 /*
315 * Some BSCs do not handle the payload inside a SCCP CREF msg
316 * so we will need to:
317 * 1.) Allocate a local connection and mark it as local..
318 * 2.) queue data for downstream.. and the RLC should delete everything
319 */
320 if (payload) {
321 struct msgb *cc, *udt, *rlsd;
322 struct sccp_connections *con;
323 con = create_sccp_src_ref(bsc, parsed);
324 if (!con)
325 goto send_refuse;
326
327 /* declare it local and assign a unique remote_ref */
328 con->con_type = NAT_CON_TYPE_LOCAL_REJECT;
329 con->con_local = 1;
330 con->has_remote_ref = 1;
331 con->remote_ref = con->patched_ref;
332
333 /* 1. create a confirmation */
334 cc = sccp_create_cc(&con->remote_ref, &con->real_ref);
335 if (!cc)
336 goto send_refuse;
337
338 /* 2. create the DT1 */
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800339 gsm0808_prepend_dtap_header(payload, 0);
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800340 udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len);
341 if (!udt) {
342 msgb_free(cc);
343 goto send_refuse;
344 }
345
346 /* 3. send a RLSD */
347 rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref,
348 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
349 if (!rlsd) {
350 msgb_free(cc);
351 msgb_free(udt);
352 goto send_refuse;
353 }
354
355 bsc_write(bsc, cc, IPAC_PROTO_SCCP);
356 bsc_write(bsc, udt, IPAC_PROTO_SCCP);
357 bsc_write(bsc, rlsd, IPAC_PROTO_SCCP);
358 msgb_free(payload);
359 return;
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800360 }
361
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800362
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800363send_refuse:
Holger Hans Peter Freythered775e42010-06-15 18:52:24 +0800364 if (payload)
365 msgb_free(payload);
366
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +0800367 refuse = sccp_create_refuse(parsed->src_local_ref,
368 SCCP_REFUSAL_SCCP_FAILURE, NULL, 0);
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800369 if (!refuse) {
370 LOGP(DNAT, LOGL_ERROR,
371 "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n",
372 sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr);
373 return;
374 }
375
376 bsc_write(bsc, refuse, IPAC_PROTO_SCCP);
377}
378
379
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800380static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100381{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800382 struct sccp_connections *con;
383 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800384 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200385 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100386
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100387 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800388 parsed = bsc_nat_parse(msg);
389 if (!parsed) {
390 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100391 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800392 }
393
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100394 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800395 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800396
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200397 proto = parsed->ipa_proto;
398
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100399 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200400 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100401 switch (parsed->sccp_type) {
402 case SCCP_MSG_TYPE_UDT:
403 /* forward UDT messages to every BSC */
404 goto send_to_all;
405 break;
406 case SCCP_MSG_TYPE_RLSD:
407 case SCCP_MSG_TYPE_CREF:
408 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200409 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800410 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800411 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200412 counter_inc(nat->stats.sccp.calls);
413
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800414 if (con) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800415 struct rate_ctr_group *ctrg;
416 ctrg = con->bsc->cfg->stats.ctrg;
417 rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800418 if (bsc_mgcp_assign(con, msg) != 0)
419 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
420 } else
421 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
422 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200423 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100424 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800425 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
426 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200427 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800428 break;
429 case SCCP_MSG_TYPE_RLC:
430 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
431 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100432 break;
433 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100434 /* MSC never opens a SCCP connection, fall through */
435 default:
436 goto exit;
437 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200438
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200439 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
440 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
441 /* Exchange src/dest for the reply */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800442 nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200443 } else if (!con)
Holger Hans Peter Freyther5f10c6d2010-05-15 19:13:52 +0800444 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 +0100445 }
446
447 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800448 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100449 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800450 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800451 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100452 return -1;
453 }
454
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200455 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100456 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100457
458send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800459 /*
460 * Filter Paging from the network. We do not want to send a PAGING
461 * Command to every BSC in our network. We will analys the PAGING
462 * message and then send it to the authenticated messages...
463 */
464 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200465 int lac;
466 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800467 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800468 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800469 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200470 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200471 else
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200472 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
473 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800474
475 goto exit;
476 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100477 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800478 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100479 if (!bsc->authenticated)
480 continue;
481
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200482 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100483 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800484
485exit:
486 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100487 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100488}
489
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800490static void msc_connection_was_lost(struct bsc_msc_connection *con)
491{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200492 struct bsc_connection *bsc, *tmp;
493
Holger Hans Peter Freythera4ed81c2010-04-12 12:35:02 +0200494 counter_inc(nat->stats.msc.reconn);
495
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200496 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
497 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800498 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200499
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200500 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200501 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200502 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800503}
504
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200505static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200506{
507 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200508 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200509 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
510 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
511 0x01, 0x20
512 };
513
514 struct msgb *msg;
515
516 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
517 if (!msg) {
518 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
519 return;
520 }
521
522 msg->l2h = msgb_put(msg, sizeof(reset));
523 memcpy(msg->l2h, reset, msgb_l2len(msg));
524
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800525 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200526
527 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
528}
529
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800530static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100531{
532 int error;
533 struct msgb *msg = ipaccess_read_msg(bfd, &error);
534 struct ipaccess_head *hh;
535
536 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800537 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100538 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800539 else
540 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100541
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800542 bsc_msc_lost(nat->msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100543 return -1;
544 }
545
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100546 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 +0100547
548 /* handle base message handling */
549 hh = (struct ipaccess_head *) msg->data;
550 ipaccess_rcvmsg_base(msg, bfd);
551
552 /* initialize the networking. This includes sending a GSM08.08 message */
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800553 if (hh->proto == IPAC_PROTO_IPACCESS) {
554 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
555 initialize_msc_if_needed();
556 else if (msg->l2h[0] == IPAC_MSGT_ID_GET)
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800557 send_id_get_response(nat->msc_con);
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800558 } else if (hh->proto == IPAC_PROTO_SCCP)
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800559 forward_sccp_to_bts(nat->msc_con, msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100560
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800561 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100562 return 0;
563}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800564
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800565static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
566{
567 int rc;
568 rc = write(bfd->fd, msg->data, msg->len);
569
570 if (rc != msg->len) {
571 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
572 return -1;
573 }
574
575 return rc;
576}
577
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100578/*
579 * Below is the handling of messages coming
580 * from the BSC and need to be forwarded to
581 * a real BSC.
582 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100583
584/*
585 * Remove the connection from the connections list,
586 * remove it from the patching of SCCP header lists
587 * as well. Maybe in the future even close connection..
588 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800589void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100590{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100591 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800592 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100593
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800594 /* stop the timeout timer */
595 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800596 bsc_del_timer(&connection->ping_timeout);
597 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800598
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800599 if (connection->cfg)
600 ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP];
601
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100602 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800603 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100604 if (sccp_patch->bsc != connection)
605 continue;
606
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800607 if (ctr)
608 rate_ctr_inc(ctr);
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800609 if (sccp_patch->has_remote_ref)
610 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200611 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100612 }
613
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200614 /* close endpoints allocated by this BSC */
615 bsc_mgcp_clear_endpoints_for(connection);
616
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800617 bsc_unregister_fd(&connection->write_queue.bfd);
618 close(connection->write_queue.bfd.fd);
619 write_queue_clear(&connection->write_queue);
620 llist_del(&connection->list_entry);
621
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100622 talloc_free(connection);
623}
624
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800625static void ipaccess_close_bsc(void *data)
626{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200627 struct sockaddr_in sock;
628 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800629 struct bsc_connection *conn = data;
630
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200631
632 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
633 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
634 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800635 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800636}
637
638static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
639{
640 struct bsc_config *conf;
641 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
642
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800643 if (bsc->cfg) {
644 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
645 bsc->write_queue.bfd.fd, bsc->cfg->nr);
646 return;
647 }
648
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800649 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
650 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800651 rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800652 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200653 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800654 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800655 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
656 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800657 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200658 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800659 }
660 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200661
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800662 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
663 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800664}
665
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100666static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100667{
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800668 int con_filter = 0;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800669 struct bsc_msc_connection *con_msc = NULL;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800670 struct bsc_connection *con_bsc = NULL;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800671 int con_type;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800672 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100673
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800674 /* Parse and filter messages */
675 parsed = bsc_nat_parse(msg);
676 if (!parsed) {
677 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200678 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800679 return -1;
680 }
681
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100682 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800683 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800684
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200685 /*
686 * check authentication after filtering to not reject auth
687 * responses coming from the BSC. We have to make sure that
688 * nothing from the exit path will forward things to the MSC
689 */
690 if (!bsc->authenticated) {
691 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
692 msgb_free(msg);
693 return -1;
694 }
695
696
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100697 /* modify the SCCP entries */
698 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800699 struct sccp_connections *con;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100700 switch (parsed->sccp_type) {
701 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800702 if (bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type) != 0)
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800703 goto exit3;
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800704 if (!create_sccp_src_ref(bsc, parsed))
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100705 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800706 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther3a77e612010-07-05 13:33:18 +0800707 con->msc_con = bsc->nat->msc_con;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800708 con_msc = con->msc_con;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800709 con->con_type = con_type;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800710 con_bsc = con->bsc;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100711 break;
712 case SCCP_MSG_TYPE_RLSD:
713 case SCCP_MSG_TYPE_CREF:
714 case SCCP_MSG_TYPE_DT1:
715 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200716 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800717 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800718 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800719 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800720 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800721 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800722 }
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100723 break;
724 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800725 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800726 if (con) {
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800727 con_bsc = con->bsc;
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800728 con_msc = con->msc_con;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800729 con_filter = con->con_local;
Holger Hans Peter Freyther605f62a2010-05-16 16:30:42 +0800730 }
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200731 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100732 break;
733 case SCCP_MSG_TYPE_UDT:
734 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800735 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100736 break;
737 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800738 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 +0800739 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100740 goto exit2;
741 break;
742 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200743 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
744 bsc_mgcp_forward(bsc, msg);
745 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800746 } else {
747 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
748 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100749 }
750
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800751 if (con_msc && con_bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800752 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 +0800753 bsc->cfg->nr, con_bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100754 goto exit2;
755 }
756
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800757 if (!con_msc) {
758 LOGP(DNAT, LOGL_ERROR, "No connection found, dropping data.\n");
759 goto exit2;
760 }
761
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800762 /* do not forward messages to the MSC */
763 if (con_filter)
764 goto exit2;
765
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100766 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freytherdb65bbd2010-07-05 13:58:40 +0800767 queue_for_msc(con_msc, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100768 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800769 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800770
771exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100772 /* if we filter out the reset send an ack to the BSC */
773 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100774 send_reset_ack(bsc);
775 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800776 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
777 /* do we know who is handling this? */
778 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
779 struct tlv_parsed tvp;
780 ipaccess_idtag_parse(&tvp,
781 (unsigned char *) msg->l2h + 2,
782 msgb_l2len(msg) - 2);
783 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
784 ipaccess_auth_bsc(&tvp, bsc);
785 }
786
787 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100788 }
789
Holger Hans Peter Freyther1498d2e2010-05-15 00:29:50 +0800790exit2:
791 talloc_free(parsed);
792 msgb_free(msg);
793 return -1;
794
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800795exit3:
796 /* send a SCCP Connection Refused */
Holger Hans Peter Freyther91246d72010-05-16 02:20:26 +0800797 bsc_send_con_refuse(bsc, parsed, con_type);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800798 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800799 msgb_free(msg);
800 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100801}
802
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800803static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100804{
805 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100806 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100807 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800808 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100809
810 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800811 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800812 LOGP(DNAT, LOGL_ERROR,
813 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
814 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800815 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800816 LOGP(DNAT, LOGL_ERROR,
817 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
818 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800819
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800820 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100821 return -1;
822 }
823
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100824
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100825 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 +0100826
827 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800828 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800829
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800830 /* stop the pong timeout */
831 if (hh->proto == IPAC_PROTO_IPACCESS) {
832 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800833 bsc_del_timer(&bsc->pong_timeout);
834 msgb_free(msg);
835 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800836 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
837 send_pong(bsc);
838 msgb_free(msg);
839 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800840 }
841 }
842
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100843 /* FIXME: Currently no PONG is sent to the BSC */
844 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100845 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100846
847 return 0;
848}
849
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100850static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
851{
852 int rc;
853
854 rc = write(bfd->fd, msg->data, msg->len);
855 if (rc != msg->len)
856 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
857
858 return rc;
859}
860
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100861static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
862{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100863 struct bsc_connection *bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800864 int fd, rc, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100865 struct sockaddr_in sa;
866 socklen_t sa_len = sizeof(sa);
867
868 if (!(what & BSC_FD_READ))
869 return 0;
870
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800871 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
872 if (fd < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100873 perror("accept");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800874 return fd;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100875 }
876
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200877 /* count the reconnect */
878 counter_inc(nat->stats.bsc.reconn);
879
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200880 /*
881 * if we are not connected to a msc... just close the socket
882 */
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800883 if (!nat->msc_con->is_connected) {
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200884 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800885 close(fd);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200886 return 0;
887 }
888
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800889 on = 1;
Holger Hans Peter Freyther569ee122010-05-05 20:42:14 +0800890 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800891 if (rc != 0)
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800892 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
893
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +0800894 rc = setsockopt(fd, IPPROTO_IP, IP_TOS,
895 &nat->bsc_ip_tos, sizeof(nat->bsc_ip_tos));
896 if (rc != 0)
897 LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
898
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100899 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100900 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100901
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100902 /*
903 *
904 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800905 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100906 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100907 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800908 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100909 return -1;
910 }
911
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800912 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800913 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800914 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100915 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800916 bsc->write_queue.bfd.when = BSC_FD_READ;
917 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100918 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800919 close(fd);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100920 talloc_free(bsc);
921 return -2;
922 }
923
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800924 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther872d7682010-05-05 20:33:34 +0800925 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800926 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100927 send_id_ack(bsc);
928 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200929 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800930
931 /*
932 * start the hangup timer
933 */
934 bsc->id_timeout.data = bsc;
935 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800936 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100937 return 0;
938}
939
940static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
941{
942 struct sockaddr_in addr;
943 int ret, on = 1;
944
945 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
946 bfd->cb = ipaccess_listen_bsc_cb;
947 bfd->when = BSC_FD_READ;
948
949 memset(&addr, 0, sizeof(addr));
950 addr.sin_family = AF_INET;
951 addr.sin_port = htons(port);
952 addr.sin_addr.s_addr = in_addr->s_addr;
953
954 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
955
956 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
957 if (ret < 0) {
958 fprintf(stderr, "Could not bind the BSC socket %s\n",
959 strerror(errno));
960 return -EIO;
961 }
962
963 ret = listen(bfd->fd, 1);
964 if (ret < 0) {
965 perror("listen");
966 return ret;
967 }
968
969 ret = bsc_register_fd(bfd);
970 if (ret < 0) {
971 perror("register_listen_fd");
972 return ret;
973 }
974 return 0;
975}
976
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800977static void print_usage()
978{
979 printf("Usage: bsc_nat\n");
980}
981
982static void print_help()
983{
984 printf(" Some useful help...\n");
985 printf(" -h --help this text\n");
986 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
987 printf(" -s --disable-color\n");
988 printf(" -c --config-file filename The config file to use.\n");
989 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100990 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800991}
992
993static void handle_options(int argc, char** argv)
994{
995 while (1) {
996 int option_index = 0, c;
997 static struct option long_options[] = {
998 {"help", 0, 0, 'h'},
999 {"debug", 1, 0, 'd'},
1000 {"config-file", 1, 0, 'c'},
1001 {"disable-color", 0, 0, 's'},
1002 {"timestamp", 0, 0, 'T'},
1003 {"msc", 1, 0, 'm'},
1004 {"local", 1, 0, 'l'},
1005 {0, 0, 0, 0}
1006 };
1007
1008 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
1009 long_options, &option_index);
1010 if (c == -1)
1011 break;
1012
1013 switch (c) {
1014 case 'h':
1015 print_usage();
1016 print_help();
1017 exit(0);
1018 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001019 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001020 break;
1021 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001022 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001023 break;
1024 case 'c':
1025 config_file = strdup(optarg);
1026 break;
1027 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001028 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001029 break;
1030 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001031 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001032 break;
1033 case 'l':
1034 inet_aton(optarg, &local_addr);
1035 break;
1036 default:
1037 /* ignore */
1038 break;
1039 }
1040 }
1041}
1042
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001043static void signal_handler(int signal)
1044{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001045 switch (signal) {
1046 case SIGABRT:
1047 /* in case of abort, we want to obtain a talloc report
1048 * and then return to the caller, who will abort the process */
1049 case SIGUSR1:
1050 talloc_report_full(tall_bsc_ctx, stderr);
1051 break;
1052 default:
1053 break;
1054 }
1055}
1056
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001057static void sccp_close_unconfirmed(void *_data)
1058{
1059 struct sccp_connections *conn, *tmp1;
1060 struct timespec now;
1061 clock_gettime(CLOCK_MONOTONIC, &now);
1062
1063 llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) {
1064 if (conn->has_remote_ref)
1065 continue;
1066
1067 int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60;
1068 if (diff < SCCP_CLOSE_TIME_TIMEOUT)
1069 continue;
1070
1071 LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n",
1072 sccp_src_ref_to_int(&conn->real_ref),
1073 sccp_src_ref_to_int(&conn->patched_ref));
1074 sccp_connection_destroy(conn);
1075 }
1076
1077 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1078}
1079
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001080extern void *tall_msgb_ctx;
1081extern void *tall_ctr_ctx;
1082static void talloc_init_ctx()
1083{
1084 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
1085 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
1086 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
1087}
1088
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001089extern enum node_type bsc_vty_go_parent(struct vty *vty);
1090
1091static struct vty_app_info vty_info = {
1092 .name = "BSC NAT",
1093 .version = PACKAGE_VERSION,
1094 .go_parent_cb = bsc_vty_go_parent,
1095};
1096
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001097int main(int argc, char** argv)
1098{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +08001099 talloc_init_ctx();
1100
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +08001101
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +08001102 log_init(&log_info);
1103 stderr_target = log_target_create_stderr();
1104 log_add_target(stderr_target);
1105 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +08001106
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001107 nat = bsc_nat_alloc();
1108 if (!nat) {
1109 fprintf(stderr, "Failed to allocate the BSC nat.\n");
1110 return -4;
1111 }
1112
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001113 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001114 if (!nat->mgcp_cfg) {
1115 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
1116 return -5;
1117 }
1118
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001119 vty_info.copyright = openbsc_copyright;
1120 vty_init(&vty_info);
1121 logging_vty_add_cmds();
1122 bsc_nat_vty_init(nat);
1123
1124
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001125 /* parse options */
1126 local_addr.s_addr = INADDR_ANY;
1127 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001128
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +08001129 rate_ctr_init(tall_bsc_ctx);
1130
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001131 /* init vty and parse */
Holger Hans Peter Freyther1398f132010-06-15 20:14:08 +08001132 telnet_init(tall_bsc_ctx, NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +08001133 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001134 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1135 return -3;
1136 }
1137
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +08001138 /* over rule the VTY config */
1139 if (msc_ip)
1140 bsc_nat_set_msc_ip(nat, msc_ip);
1141
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001142 /* seed the PRNG */
1143 srand(time(NULL));
1144
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001145 /*
1146 * Setup the MGCP code..
1147 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +08001148 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +02001149 return -4;
1150
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001151 /* connect to the MSC */
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +08001152 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0);
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001153 if (!nat->msc_con) {
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001154 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001155 exit(1);
1156 }
1157
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +08001158 nat->msc_con->connection_loss = msc_connection_was_lost;
1159 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
1160 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
1161 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +08001162
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001163 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +01001164 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +01001165 fprintf(stderr, "Failed to listen for BSC.\n");
1166 exit(1);
1167 }
1168
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001169 signal(SIGABRT, &signal_handler);
1170 signal(SIGUSR1, &signal_handler);
1171 signal(SIGPIPE, SIG_IGN);
1172
Holger Hans Peter Freyther6b6ecba2010-06-15 18:51:56 +08001173 /* recycle timer */
1174 sccp_close.cb = sccp_close_unconfirmed;
1175 sccp_close.data = NULL;
1176 bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0);
1177
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +01001178 while (1) {
1179 bsc_select_main(0);
1180 }
1181
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +08001182 return 0;
1183}