blob: b9a158424a64ac792604f5cd7ab0b071313583a6 [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 Freyther38a77d02010-01-30 12:45:10 +010042#include <openbsc/bssap.h>
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080045#include <openbsc/telnet_interface.h>
46
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080047#include <osmocore/talloc.h>
48
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080049#include <vty/vty.h>
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080050
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080051#include <sccp/sccp.h>
52
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +080053struct log_target *stderr_target;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080054static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +080055static struct in_addr local_addr;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080056static struct bsc_msc_connection *msc_con;
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +010057static struct bsc_fd bsc_listen;
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080058static const char *msc_ip = NULL;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010059
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010060
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080061static struct bsc_nat *nat;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +020062static 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 +020063static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080064
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080065struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
66{
67 struct bsc_config *conf;
68
69 llist_for_each_entry(conf, &nat->bsc_configs, entry)
70 if (conf->nr == num)
71 return conf;
72
73 return NULL;
74}
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +010075
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +010076/*
77 * below are stubs we need to link
78 */
79int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
80 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
81{
82 return -1;
83}
84
85void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
86{}
87
88int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
89{
90 return -1;
91}
92
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +080093static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
94{
95 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
96 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
97 msgb_free(msg);
98 }
99}
100
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100101static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100102{
103 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100104 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
105 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
106 0x00, 0x01, 0x31,
107 };
108
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200109 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100110}
111
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800112static void send_ping(struct bsc_connection *bsc)
113{
114 static const u_int8_t id_ping[] = {
115 IPAC_MSGT_PING,
116 };
117
118 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
119}
120
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800121static void send_pong(struct bsc_connection *bsc)
122{
123 static const u_int8_t id_pong[] = {
124 IPAC_MSGT_PONG,
125 };
126
127 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
128}
129
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800130static void bsc_pong_timeout(void *_bsc)
131{
132 struct bsc_connection *bsc = _bsc;
133
134 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
135 bsc_close_connection(bsc);
136}
137
138static void bsc_ping_timeout(void *_bsc)
139{
140 struct bsc_connection *bsc = _bsc;
141
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800142 if (bsc->nat->ping_timeout < 0)
143 return;
144
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800145 send_ping(bsc);
146
147 /* send another ping in 20 seconds */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800148 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800149
150 /* also start a pong timer */
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800151 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800152}
153
154static void start_ping_pong(struct bsc_connection *bsc)
155{
156 bsc->pong_timeout.data = bsc;
157 bsc->pong_timeout.cb = bsc_pong_timeout;
158 bsc->ping_timeout.data = bsc;
159 bsc->ping_timeout.cb = bsc_ping_timeout;
160
161 bsc_ping_timeout(bsc);
162}
163
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100164static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100165{
166 static const u_int8_t id_ack[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200167 IPAC_MSGT_ID_ACK
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100168 };
169
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200170 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100171}
172
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100173static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100174{
175 static const u_int8_t id_req[] = {
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200176 IPAC_MSGT_ID_GET,
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100177 0x01, IPAC_IDTAG_UNIT,
178 0x01, IPAC_IDTAG_MACADDR,
179 0x01, IPAC_IDTAG_LOCATION1,
180 0x01, IPAC_IDTAG_LOCATION2,
181 0x01, IPAC_IDTAG_EQUIPVERS,
182 0x01, IPAC_IDTAG_SWVERSION,
183 0x01, IPAC_IDTAG_UNITNAME,
184 0x01, IPAC_IDTAG_SERNR,
185 };
186
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200187 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100188}
189
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200190static void nat_send_rlsd(struct sccp_connections *conn)
191{
192 struct sccp_connection_released *rel;
193 struct msgb *msg;
194
195 msg = msgb_alloc_headroom(4096, 128, "rlsd");
196 if (!msg) {
197 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
198 return;
199 }
200
201 msg->l2h = msgb_put(msg, sizeof(*rel));
202 rel = (struct sccp_connection_released *) msg->l2h;
203 rel->type = SCCP_MSG_TYPE_RLSD;
204 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
205 rel->destination_local_reference = conn->remote_ref;
206 rel->source_local_reference = conn->patched_ref;
207
208 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
209
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800210 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther6b087d12010-04-06 17:32:58 +0200211}
212
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200213static void nat_send_rlc(struct sccp_source_reference *src,
214 struct sccp_source_reference *dst)
215{
216 struct sccp_connection_release_complete *rlc;
217 struct msgb *msg;
218
219 msg = msgb_alloc_headroom(4096, 128, "rlc");
220 if (!msg) {
221 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
222 return;
223 }
224
225 msg->l2h = msgb_put(msg, sizeof(*rlc));
226 rlc = (struct sccp_connection_release_complete *) msg->l2h;
227 rlc->type = SCCP_MSG_TYPE_RLC;
228 rlc->destination_local_reference = *dst;
229 rlc->source_local_reference = *src;
230
231 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
232
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800233 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200234}
235
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200236static void send_mgcp_reset(struct bsc_connection *bsc)
237{
238 static const u_int8_t mgcp_reset[] = {
239 "RSIP 1 13@mgw MGCP 1.0\r\n"
240 };
241
242 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
243}
244
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100245/*
246 * Below is the handling of messages coming
247 * from the MSC and need to be forwarded to
248 * a real BSC.
249 */
250static void initialize_msc_if_needed()
251{
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200252 if (nat->first_contact)
253 return;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100254
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200255 nat->first_contact = 1;
256 msc_send_reset(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100257}
258
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100259/*
260 * Currently we are lacking refcounting so we need to copy each message.
261 */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200262static 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 +0100263{
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100264 struct msgb *msg;
265
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200266 if (length > 4096 - 128) {
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100267 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
268 return;
269 }
270
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200271 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100272 if (!msg) {
273 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
274 return;
275 }
276
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200277 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100278 memcpy(msg->data, data, length);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200279
280 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherf7cb33c2010-03-26 07:20:59 +0100281}
282
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100283static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100284{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800285 struct sccp_connections *con;
286 struct bsc_connection *bsc;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800287 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200288 int proto;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100289
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100290 /* filter, drop, patch the message? */
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800291 parsed = bsc_nat_parse(msg);
292 if (!parsed) {
293 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100294 return -1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800295 }
296
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100297 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800298 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800299
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200300 proto = parsed->ipa_proto;
301
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100302 /* Route and modify the SCCP packet */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200303 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100304 switch (parsed->sccp_type) {
305 case SCCP_MSG_TYPE_UDT:
306 /* forward UDT messages to every BSC */
307 goto send_to_all;
308 break;
309 case SCCP_MSG_TYPE_RLSD:
310 case SCCP_MSG_TYPE_CREF:
311 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200312 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800313 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800314 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200315 counter_inc(nat->stats.sccp.calls);
316
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800317 if (con) {
Holger Hans Peter Freyther3d0049f2010-04-18 01:35:41 +0800318 counter_inc(con->bsc->cfg->stats.sccp.calls);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800319 if (bsc_mgcp_assign(con, msg) != 0)
320 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
321 } else
322 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
323 }
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200324 break;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100325 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800326 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
327 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200328 goto exit;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800329 break;
330 case SCCP_MSG_TYPE_RLC:
331 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
332 goto exit;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100333 break;
334 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100335 /* MSC never opens a SCCP connection, fall through */
336 default:
337 goto exit;
338 }
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200339
Holger Hans Peter Freytherb8a33732010-04-08 11:28:12 +0200340 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
341 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
342 /* Exchange src/dest for the reply */
343 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
344 } else if (!con)
Holger Hans Peter Freytherf464ea52010-04-06 16:07:44 +0200345 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100346 }
347
348 talloc_free(parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800349 if (!con)
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100350 return -1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800351 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800352 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100353 return -1;
354 }
355
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200356 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100357 return 0;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100358
359send_to_all:
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800360 /*
361 * Filter Paging from the network. We do not want to send a PAGING
362 * Command to every BSC in our network. We will analys the PAGING
363 * message and then send it to the authenticated messages...
364 */
365 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200366 int lac;
367 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800368 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freythera34585e2010-04-21 20:17:18 +0800369 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800370 else if (bsc)
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200371 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200372 else
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200373 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
374 lac, lac);
Holger Hans Peter Freyther45d11812010-06-15 18:46:36 +0800375
376 goto exit;
377 }
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100378 /* currently send this to every BSC connected */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800379 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther3f37b8f2010-02-08 23:24:32 +0100380 if (!bsc->authenticated)
381 continue;
382
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200383 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100384 }
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800385
386exit:
387 talloc_free(parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100388 return 0;
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100389}
390
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800391static void msc_connection_was_lost(struct bsc_msc_connection *con)
392{
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200393 struct bsc_connection *bsc, *tmp;
394
Holger Hans Peter Freythera4ed81c2010-04-12 12:35:02 +0200395 counter_inc(nat->stats.msc.reconn);
396
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200397 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
398 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800399 bsc_close_connection(bsc);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200400
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200401 nat->first_contact = 0;
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200402 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200403 bsc_msc_schedule_connect(con);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800404}
405
Holger Hans Peter Freyther02aee142010-04-08 10:31:07 +0200406static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200407{
408 static const u_int8_t reset[] = {
Holger Hans Peter Freyther7cab1662010-04-07 11:11:11 +0200409 0x00, 0x12, 0xfd,
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200410 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
411 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
412 0x01, 0x20
413 };
414
415 struct msgb *msg;
416
417 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
418 if (!msg) {
419 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
420 return;
421 }
422
423 msg->l2h = msgb_put(msg, sizeof(reset));
424 memcpy(msg->l2h, reset, msgb_l2len(msg));
425
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800426 queue_for_msc(msc_con, msg);
Holger Hans Peter Freytheraf0ff6c2010-04-07 10:46:30 +0200427
428 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
429}
430
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800431static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100432{
433 int error;
434 struct msgb *msg = ipaccess_read_msg(bfd, &error);
435 struct ipaccess_head *hh;
436
437 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800438 if (error == 0)
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100439 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800440 else
441 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100442
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800443 bsc_msc_lost(msc_con);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100444 return -1;
445 }
446
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100447 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 +0100448
449 /* handle base message handling */
450 hh = (struct ipaccess_head *) msg->data;
451 ipaccess_rcvmsg_base(msg, bfd);
452
453 /* initialize the networking. This includes sending a GSM08.08 message */
454 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
455 initialize_msc_if_needed();
456 else if (hh->proto == IPAC_PROTO_SCCP)
457 forward_sccp_to_bts(msg);
458
Holger Hans Peter Freytheraad68b52010-06-15 18:46:48 +0800459 msgb_free(msg);
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100460 return 0;
461}
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800462
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800463static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
464{
465 int rc;
466 rc = write(bfd->fd, msg->data, msg->len);
467
468 if (rc != msg->len) {
469 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
470 return -1;
471 }
472
473 return rc;
474}
475
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100476/*
477 * Below is the handling of messages coming
478 * from the BSC and need to be forwarded to
479 * a real BSC.
480 */
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100481
482/*
483 * Remove the connection from the connections list,
484 * remove it from the patching of SCCP header lists
485 * as well. Maybe in the future even close connection..
486 */
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800487void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100488{
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100489 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100490
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800491 /* stop the timeout timer */
492 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800493 bsc_del_timer(&connection->ping_timeout);
494 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800495
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100496 /* remove all SCCP connections */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800497 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100498 if (sccp_patch->bsc != connection)
499 continue;
500
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800501 if (sccp_patch->has_remote_ref)
502 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200503 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100504 }
505
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200506 /* close endpoints allocated by this BSC */
507 bsc_mgcp_clear_endpoints_for(connection);
508
Holger Hans Peter Freythere464ed42010-04-22 12:04:36 +0800509 bsc_unregister_fd(&connection->write_queue.bfd);
510 close(connection->write_queue.bfd.fd);
511 write_queue_clear(&connection->write_queue);
512 llist_del(&connection->list_entry);
513
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100514 talloc_free(connection);
515}
516
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800517static void ipaccess_close_bsc(void *data)
518{
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200519 struct sockaddr_in sock;
520 socklen_t len = sizeof(sock);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800521 struct bsc_connection *conn = data;
522
Holger Hans Peter Freyther6d5a6002010-04-17 07:58:17 +0200523
524 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
525 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
526 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800527 bsc_close_connection(conn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800528}
529
530static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
531{
532 struct bsc_config *conf;
533 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
534
Holger Hans Peter Freyther5cdcfa62010-05-02 18:58:10 +0800535 if (bsc->cfg) {
536 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
537 bsc->write_queue.bfd.fd, bsc->cfg->nr);
538 return;
539 }
540
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800541 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
542 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200543 counter_inc(conf->stats.net.reconn);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800544 bsc->authenticated = 1;
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200545 bsc->cfg = conf;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800546 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800547 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
548 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800549 start_ping_pong(bsc);
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200550 return;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800551 }
552 }
Holger Hans Peter Freytherc615c262010-04-17 07:59:57 +0200553
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800554 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
555 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800556}
557
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100558static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100559{
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800560 struct sccp_connections *con;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800561 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100562
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800563 /* Parse and filter messages */
564 parsed = bsc_nat_parse(msg);
565 if (!parsed) {
566 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freytherd7657ff2010-03-30 07:35:46 +0200567 msgb_free(msg);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800568 return -1;
569 }
570
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100571 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800572 goto exit;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800573
Holger Hans Peter Freytherbbb9d392010-04-02 03:42:44 +0200574 /*
575 * check authentication after filtering to not reject auth
576 * responses coming from the BSC. We have to make sure that
577 * nothing from the exit path will forward things to the MSC
578 */
579 if (!bsc->authenticated) {
580 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
581 msgb_free(msg);
582 return -1;
583 }
584
585
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100586 /* modify the SCCP entries */
587 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
588 switch (parsed->sccp_type) {
589 case SCCP_MSG_TYPE_CR:
590 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
591 goto exit2;
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800592 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100593 break;
594 case SCCP_MSG_TYPE_RLSD:
595 case SCCP_MSG_TYPE_CREF:
596 case SCCP_MSG_TYPE_DT1:
597 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freytherf46ce532010-04-06 10:22:34 +0200598 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800599 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100600 break;
601 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800602 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freytherf4cfc4f2010-03-31 09:15:05 +0200603 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100604 break;
605 case SCCP_MSG_TYPE_UDT:
606 /* simply forward everything */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800607 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100608 break;
609 default:
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800610 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 +0800611 con = NULL;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100612 goto exit2;
613 break;
614 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200615 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
616 bsc_mgcp_forward(bsc, msg);
617 goto exit2;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800618 } else {
619 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
620 goto exit2;
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100621 }
622
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800623 if (con && con->bsc != bsc) {
Holger Hans Peter Freytherca0c2f92010-04-21 18:49:55 +0800624 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
625 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100626 goto exit2;
627 }
628
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100629 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther9d56d0c2010-04-22 19:11:37 +0800630 queue_for_msc(msc_con, msg);
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100631 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800632 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800633
634exit:
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100635 /* if we filter out the reset send an ack to the BSC */
636 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100637 send_reset_ack(bsc);
638 send_reset_ack(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800639 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
640 /* do we know who is handling this? */
641 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
642 struct tlv_parsed tvp;
643 ipaccess_idtag_parse(&tvp,
644 (unsigned char *) msg->l2h + 2,
645 msgb_l2len(msg) - 2);
646 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
647 ipaccess_auth_bsc(&tvp, bsc);
648 }
649
650 goto exit2;
Holger Hans Peter Freyther38a77d02010-01-30 12:45:10 +0100651 }
652
Holger Hans Peter Freyther058eeb72010-01-31 09:46:21 +0100653exit2:
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800654 talloc_free(parsed);
Holger Hans Peter Freyther6f5fbfd2010-06-15 18:47:02 +0800655 msgb_free(msg);
656 return -1;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100657}
658
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800659static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100660{
661 int error;
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100662 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100663 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800664 struct ipaccess_head *hh;
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100665
666 if (!msg) {
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800667 if (error == 0)
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800668 LOGP(DNAT, LOGL_ERROR,
669 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
670 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800671 else
Holger Hans Peter Freyther19c35442010-05-01 10:37:15 +0800672 LOGP(DNAT, LOGL_ERROR,
673 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
674 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freyther9db78432010-04-23 00:23:03 +0800675
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800676 bsc_close_connection(bsc);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100677 return -1;
678 }
679
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100680
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100681 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 +0100682
683 /* Handle messages from the BSC */
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800684 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800685
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800686 /* stop the pong timeout */
687 if (hh->proto == IPAC_PROTO_IPACCESS) {
688 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800689 bsc_del_timer(&bsc->pong_timeout);
690 msgb_free(msg);
691 return 0;
Holger Hans Peter Freyther50788712010-06-15 18:51:33 +0800692 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
693 send_pong(bsc);
694 msgb_free(msg);
695 return 0;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800696 }
697 }
698
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100699 /* FIXME: Currently no PONG is sent to the BSC */
700 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther747d6542010-03-26 07:24:34 +0100701 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100702
703 return 0;
704}
705
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100706static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
707{
708 int rc;
709
710 rc = write(bfd->fd, msg->data, msg->len);
711 if (rc != msg->len)
712 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
713
714 return rc;
715}
716
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100717static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
718{
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100719 struct bsc_connection *bsc;
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800720 int ret, on;
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100721 struct sockaddr_in sa;
722 socklen_t sa_len = sizeof(sa);
723
724 if (!(what & BSC_FD_READ))
725 return 0;
726
727 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
728 if (ret < 0) {
729 perror("accept");
730 return ret;
731 }
732
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200733 /* count the reconnect */
734 counter_inc(nat->stats.bsc.reconn);
735
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200736 /*
737 * if we are not connected to a msc... just close the socket
738 */
739 if (!msc_con->is_connected) {
740 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freythera7c377d2010-04-06 12:42:35 +0200741 close(ret);
Holger Hans Peter Freythercd895372010-03-29 08:04:09 +0200742 return 0;
743 }
744
Holger Hans Peter Freyther9e938c62010-05-05 18:58:13 +0800745 on = 1;
746 ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
747 if (ret != 0)
748 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
749
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100750 /* todo... do something with the connection */
Holger Hans Peter Freytherda86c0a2010-01-12 21:35:32 +0100751 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100752
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100753 /*
754 *
755 */
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800756 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100757 if (!bsc) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100758 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100759 close(ret);
760 return -1;
761 }
762
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800763 bsc->write_queue.bfd.data = bsc;
764 bsc->write_queue.bfd.fd = ret;
765 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther3025e192010-03-26 09:18:02 +0100766 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +0800767 bsc->write_queue.bfd.when = BSC_FD_READ;
768 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther418f3942010-01-29 05:58:43 +0100769 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther24614ad2010-01-13 09:28:12 +0100770 close(ret);
771 talloc_free(bsc);
772 return -2;
773 }
774
Holger Hans Peter Freyther74cfab72010-05-05 17:03:44 +0800775 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
776 ret, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800777 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freytherdb7ba7d2010-03-26 07:41:54 +0100778 send_id_ack(bsc);
779 send_id_req(bsc);
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200780 send_mgcp_reset(bsc);
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800781
782 /*
783 * start the hangup timer
784 */
785 bsc->id_timeout.data = bsc;
786 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800787 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100788 return 0;
789}
790
791static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
792{
793 struct sockaddr_in addr;
794 int ret, on = 1;
795
796 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
797 bfd->cb = ipaccess_listen_bsc_cb;
798 bfd->when = BSC_FD_READ;
799
800 memset(&addr, 0, sizeof(addr));
801 addr.sin_family = AF_INET;
802 addr.sin_port = htons(port);
803 addr.sin_addr.s_addr = in_addr->s_addr;
804
805 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
806
807 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
808 if (ret < 0) {
809 fprintf(stderr, "Could not bind the BSC socket %s\n",
810 strerror(errno));
811 return -EIO;
812 }
813
814 ret = listen(bfd->fd, 1);
815 if (ret < 0) {
816 perror("listen");
817 return ret;
818 }
819
820 ret = bsc_register_fd(bfd);
821 if (ret < 0) {
822 perror("register_listen_fd");
823 return ret;
824 }
825 return 0;
826}
827
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800828static void print_usage()
829{
830 printf("Usage: bsc_nat\n");
831}
832
833static void print_help()
834{
835 printf(" Some useful help...\n");
836 printf(" -h --help this text\n");
837 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
838 printf(" -s --disable-color\n");
839 printf(" -c --config-file filename The config file to use.\n");
840 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100841 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800842}
843
844static void handle_options(int argc, char** argv)
845{
846 while (1) {
847 int option_index = 0, c;
848 static struct option long_options[] = {
849 {"help", 0, 0, 'h'},
850 {"debug", 1, 0, 'd'},
851 {"config-file", 1, 0, 'c'},
852 {"disable-color", 0, 0, 's'},
853 {"timestamp", 0, 0, 'T'},
854 {"msc", 1, 0, 'm'},
855 {"local", 1, 0, 'l'},
856 {0, 0, 0, 0}
857 };
858
859 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
860 long_options, &option_index);
861 if (c == -1)
862 break;
863
864 switch (c) {
865 case 'h':
866 print_usage();
867 print_help();
868 exit(0);
869 case 's':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800870 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800871 break;
872 case 'd':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800873 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800874 break;
875 case 'c':
876 config_file = strdup(optarg);
877 break;
878 case 'T':
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800879 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800880 break;
881 case 'm':
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800882 msc_ip = optarg;
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800883 break;
884 case 'l':
885 inet_aton(optarg, &local_addr);
886 break;
887 default:
888 /* ignore */
889 break;
890 }
891 }
892}
893
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100894static void signal_handler(int signal)
895{
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100896 switch (signal) {
897 case SIGABRT:
898 /* in case of abort, we want to obtain a talloc report
899 * and then return to the caller, who will abort the process */
900 case SIGUSR1:
901 talloc_report_full(tall_bsc_ctx, stderr);
902 break;
903 default:
904 break;
905 }
906}
907
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800908extern void *tall_msgb_ctx;
909extern void *tall_ctr_ctx;
910static void talloc_init_ctx()
911{
912 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
913 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
914 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
915}
916
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800917int main(int argc, char** argv)
918{
Holger Hans Peter Freyther0b0b31c2010-06-15 18:51:25 +0800919 talloc_init_ctx();
920
Holger Hans Peter Freythera1597f12010-06-15 18:51:18 +0800921
Holger Hans Peter Freytherfb7a9342010-06-15 19:14:12 +0800922 log_init(&log_info);
923 stderr_target = log_target_create_stderr();
924 log_add_target(stderr_target);
925 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +0800926
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800927 nat = bsc_nat_alloc();
928 if (!nat) {
929 fprintf(stderr, "Failed to allocate the BSC nat.\n");
930 return -4;
931 }
932
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800933 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800934 if (!nat->mgcp_cfg) {
935 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
936 return -5;
937 }
938
939 /* parse options */
940 local_addr.s_addr = INADDR_ANY;
941 handle_options(argc, argv);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800942
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800943 /* init vty and parse */
944 bsc_nat_vty_init(nat);
945 telnet_init(NULL, 4244);
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800946 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800947 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
948 return -3;
949 }
950
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800951 /* over rule the VTY config */
952 if (msc_ip)
953 bsc_nat_set_msc_ip(nat, msc_ip);
954
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800955 /* seed the PRNG */
956 srand(time(NULL));
957
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200958 /*
959 * Setup the MGCP code..
960 */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800961 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200962 return -4;
963
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100964 /* connect to the MSC */
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200965 msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800966 if (!msc_con) {
967 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100968 exit(1);
969 }
970
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800971 msc_con->connection_loss = msc_connection_was_lost;
972 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
973 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
974 bsc_msc_connect(msc_con);
975
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100976 /* wait for the BSC */
Holger Hans Peter Freyther2d677c62010-03-26 06:51:04 +0100977 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freyther49d80682010-01-12 21:34:54 +0100978 fprintf(stderr, "Failed to listen for BSC.\n");
979 exit(1);
980 }
981
Holger Hans Peter Freyther6ace5222010-01-12 21:15:08 +0100982 signal(SIGABRT, &signal_handler);
983 signal(SIGUSR1, &signal_handler);
984 signal(SIGPIPE, SIG_IGN);
985
986 while (1) {
987 bsc_select_main(0);
988 }
989
Holger Hans Peter Freyther9e2c5f52010-06-15 18:44:42 +0800990 return 0;
991}