blob: c14eecf5708685f56c11f9e35aee051110dbd387 [file] [log] [blame]
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08001/* BSC Multiplexer/NAT */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther98e49d42010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +01006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +08007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24#include <sys/socket.h>
25#include <netinet/in.h>
Holger Hans Peter Freyther150fa582010-05-05 18:58:13 +080026#include <netinet/tcp.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080027#include <arpa/inet.h>
28
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +010029#include <errno.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010030#include <signal.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080031#include <stdio.h>
32#include <stdlib.h>
Holger Hans Peter Freytherfd012d52010-01-12 21:36:08 +010033#include <time.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080034#include <unistd.h>
35
36#define _GNU_SOURCE
37#include <getopt.h>
38
39#include <openbsc/debug.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010040#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080041#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +010042#include <openbsc/bssap.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010043#include <openbsc/ipaccess.h>
44#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080045#include <openbsc/telnet_interface.h>
46
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080047#include <osmocore/talloc.h>
48
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080049#include <vty/vty.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080050
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080051#include <sccp/sccp.h>
52
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +080053struct log_target *stderr_target;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080054static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080055static struct in_addr local_addr;
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +010056static struct bsc_fd bsc_listen;
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +080057static const char *msc_ip = NULL;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010058
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010059
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080060static struct bsc_nat *nat;
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +020061static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int);
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +020062static void msc_send_reset(struct bsc_msc_connection *con);
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080063
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080064struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
65{
66 struct bsc_config *conf;
67
68 llist_for_each_entry(conf, &nat->bsc_configs, entry)
69 if (conf->nr == num)
70 return conf;
71
72 return NULL;
73}
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +010074
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010075/*
76 * below are stubs we need to link
77 */
78int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
79 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
80{
81 return -1;
82}
83
84void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
85{}
86
87int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
88{
89 return -1;
90}
91
Holger Hans Peter Freyther754787c2010-04-22 19:11:37 +080092static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg)
93{
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +080094 if (write_queue_enqueue(&nat->msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther754787c2010-04-22 19:11:37 +080095 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
96 msgb_free(msg);
97 }
98}
99
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100100static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100101{
102 static const u_int8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100103 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
104 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
105 0x00, 0x01, 0x31,
106 };
107
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200108 bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100109}
110
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800111static void send_ping(struct bsc_connection *bsc)
112{
113 static const u_int8_t id_ping[] = {
114 IPAC_MSGT_PING,
115 };
116
117 bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS);
118}
119
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800120static void send_pong(struct bsc_connection *bsc)
121{
122 static const u_int8_t id_pong[] = {
123 IPAC_MSGT_PONG,
124 };
125
126 bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS);
127}
128
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800129static void bsc_pong_timeout(void *_bsc)
130{
131 struct bsc_connection *bsc = _bsc;
132
133 LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr);
134 bsc_close_connection(bsc);
135}
136
137static void bsc_ping_timeout(void *_bsc)
138{
139 struct bsc_connection *bsc = _bsc;
140
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800141 if (bsc->nat->ping_timeout < 0)
142 return;
143
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800144 send_ping(bsc);
145
146 /* send another ping in 20 seconds */
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800147 bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800148
149 /* also start a pong timer */
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800150 bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800151}
152
153static void start_ping_pong(struct bsc_connection *bsc)
154{
155 bsc->pong_timeout.data = bsc;
156 bsc->pong_timeout.cb = bsc_pong_timeout;
157 bsc->ping_timeout.data = bsc;
158 bsc->ping_timeout.cb = bsc_ping_timeout;
159
160 bsc_ping_timeout(bsc);
161}
162
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100163static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100164{
165 static const u_int8_t id_ack[] = {
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200166 IPAC_MSGT_ID_ACK
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100167 };
168
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200169 bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100170}
171
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100172static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100173{
174 static const u_int8_t id_req[] = {
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200175 IPAC_MSGT_ID_GET,
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100176 0x01, IPAC_IDTAG_UNIT,
177 0x01, IPAC_IDTAG_MACADDR,
178 0x01, IPAC_IDTAG_LOCATION1,
179 0x01, IPAC_IDTAG_LOCATION2,
180 0x01, IPAC_IDTAG_EQUIPVERS,
181 0x01, IPAC_IDTAG_SWVERSION,
182 0x01, IPAC_IDTAG_UNITNAME,
183 0x01, IPAC_IDTAG_SERNR,
184 };
185
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200186 bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100187}
188
Holger Hans Peter Freyther7746bd82010-04-06 17:32:58 +0200189static void nat_send_rlsd(struct sccp_connections *conn)
190{
191 struct sccp_connection_released *rel;
192 struct msgb *msg;
193
194 msg = msgb_alloc_headroom(4096, 128, "rlsd");
195 if (!msg) {
196 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
197 return;
198 }
199
200 msg->l2h = msgb_put(msg, sizeof(*rel));
201 rel = (struct sccp_connection_released *) msg->l2h;
202 rel->type = SCCP_MSG_TYPE_RLSD;
203 rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
204 rel->destination_local_reference = conn->remote_ref;
205 rel->source_local_reference = conn->patched_ref;
206
207 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
208
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800209 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther7746bd82010-04-06 17:32:58 +0200210}
211
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200212static void nat_send_rlc(struct sccp_source_reference *src,
213 struct sccp_source_reference *dst)
214{
215 struct sccp_connection_release_complete *rlc;
216 struct msgb *msg;
217
218 msg = msgb_alloc_headroom(4096, 128, "rlc");
219 if (!msg) {
220 LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
221 return;
222 }
223
224 msg->l2h = msgb_put(msg, sizeof(*rlc));
225 rlc = (struct sccp_connection_release_complete *) msg->l2h;
226 rlc->type = SCCP_MSG_TYPE_RLC;
227 rlc->destination_local_reference = *dst;
228 rlc->source_local_reference = *src;
229
230 ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
231
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800232 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200233}
234
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200235static void send_mgcp_reset(struct bsc_connection *bsc)
236{
237 static const u_int8_t mgcp_reset[] = {
238 "RSIP 1 13@mgw MGCP 1.0\r\n"
239 };
240
241 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
242}
243
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100244/*
245 * Below is the handling of messages coming
246 * from the MSC and need to be forwarded to
247 * a real BSC.
248 */
249static void initialize_msc_if_needed()
250{
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200251 if (nat->first_contact)
252 return;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100253
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200254 nat->first_contact = 1;
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800255 msc_send_reset(nat->msc_con);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100256}
257
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100258/*
259 * Currently we are lacking refcounting so we need to copy each message.
260 */
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200261static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int proto)
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100262{
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100263 struct msgb *msg;
264
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200265 if (length > 4096 - 128) {
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100266 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
267 return;
268 }
269
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200270 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100271 if (!msg) {
272 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
273 return;
274 }
275
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200276 msg->l2h = msgb_put(msg, length);
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100277 memcpy(msg->data, data, length);
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200278
279 bsc_write(bsc, msg, proto);
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100280}
281
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100282static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100283{
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800284 struct sccp_connections *con;
285 struct bsc_connection *bsc;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800286 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200287 int proto;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100288
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100289 /* filter, drop, patch the message? */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800290 parsed = bsc_nat_parse(msg);
291 if (!parsed) {
292 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100293 return -1;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800294 }
295
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100296 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800297 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800298
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200299 proto = parsed->ipa_proto;
300
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100301 /* Route and modify the SCCP packet */
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200302 if (proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100303 switch (parsed->sccp_type) {
304 case SCCP_MSG_TYPE_UDT:
305 /* forward UDT messages to every BSC */
306 goto send_to_all;
307 break;
308 case SCCP_MSG_TYPE_RLSD:
309 case SCCP_MSG_TYPE_CREF:
310 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther567e8402010-04-06 10:22:34 +0200311 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800312 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800313 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200314 counter_inc(nat->stats.sccp.calls);
315
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800316 if (con) {
Holger Hans Peter Freytherdc71cf42010-04-18 01:35:41 +0800317 counter_inc(con->bsc->cfg->stats.sccp.calls);
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800318 if (bsc_mgcp_assign(con, msg) != 0)
319 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
320 } else
321 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
322 }
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200323 break;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100324 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800325 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
326 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200327 goto exit;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800328 break;
329 case SCCP_MSG_TYPE_RLC:
330 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
331 goto exit;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100332 break;
333 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100334 /* MSC never opens a SCCP connection, fall through */
335 default:
336 goto exit;
337 }
Holger Hans Peter Freyther8d298282010-04-06 16:07:44 +0200338
Holger Hans Peter Freythercd922a12010-04-08 11:28:12 +0200339 if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
340 LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
341 /* Exchange src/dest for the reply */
342 nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
343 } else if (!con)
Holger Hans Peter Freyther8d298282010-04-06 16:07:44 +0200344 LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100345 }
346
347 talloc_free(parsed);
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800348 if (!con)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100349 return -1;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800350 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800351 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100352 return -1;
353 }
354
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200355 bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100356 return 0;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100357
358send_to_all:
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800359 /*
360 * Filter Paging from the network. We do not want to send a PAGING
361 * Command to every BSC in our network. We will analys the PAGING
362 * message and then send it to the authenticated messages...
363 */
364 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200365 int lac;
366 bsc = bsc_nat_find_bsc(nat, msg, &lac);
Holger Hans Peter Freyther7acf4622010-04-21 19:05:14 +0800367 if (bsc && bsc->cfg->forbid_paging)
Holger Hans Peter Freyther19ccb482010-04-21 20:17:18 +0800368 LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr);
Holger Hans Peter Freyther7acf4622010-04-21 19:05:14 +0800369 else if (bsc)
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200370 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200371 else
Holger Hans Peter Freyther935b2df2010-04-17 08:07:19 +0200372 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n",
373 lac, lac);
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800374
375 goto exit;
376 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100377 /* currently send this to every BSC connected */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800378 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100379 if (!bsc->authenticated)
380 continue;
381
Holger Hans Peter Freythereb4edde2010-04-08 10:24:57 +0200382 bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100383 }
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800384
385exit:
386 talloc_free(parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100387 return 0;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100388}
389
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800390static void msc_connection_was_lost(struct bsc_msc_connection *con)
391{
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200392 struct bsc_connection *bsc, *tmp;
393
Holger Hans Peter Freyther5b870612010-04-12 12:35:02 +0200394 counter_inc(nat->stats.msc.reconn);
395
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200396 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
397 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800398 bsc_close_connection(bsc);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200399
Holger Hans Peter Freytherdc813932010-04-07 11:20:36 +0200400 nat->first_contact = 0;
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +0200401 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200402 bsc_msc_schedule_connect(con);
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800403}
404
Holger Hans Peter Freyther8f515a02010-04-08 10:31:07 +0200405static void msc_send_reset(struct bsc_msc_connection *msc_con)
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200406{
407 static const u_int8_t reset[] = {
Holger Hans Peter Freytherc48a5d92010-04-07 11:11:11 +0200408 0x00, 0x12, 0xfd,
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200409 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
410 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
411 0x01, 0x20
412 };
413
414 struct msgb *msg;
415
416 msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
417 if (!msg) {
418 LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
419 return;
420 }
421
422 msg->l2h = msgb_put(msg, sizeof(reset));
423 memcpy(msg->l2h, reset, msgb_l2len(msg));
424
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800425 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther2b3505b2010-04-07 10:46:30 +0200426
427 LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
428}
429
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800430static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100431{
432 int error;
433 struct msgb *msg = ipaccess_read_msg(bfd, &error);
434 struct ipaccess_head *hh;
435
436 if (!msg) {
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800437 if (error == 0)
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100438 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800439 else
440 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100441
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800442 bsc_msc_lost(nat->msc_con);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100443 return -1;
444 }
445
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100446 LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100447
448 /* handle base message handling */
449 hh = (struct ipaccess_head *) msg->data;
450 ipaccess_rcvmsg_base(msg, bfd);
451
452 /* initialize the networking. This includes sending a GSM08.08 message */
453 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
454 initialize_msc_if_needed();
455 else if (hh->proto == IPAC_PROTO_SCCP)
456 forward_sccp_to_bts(msg);
457
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800458 msgb_free(msg);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100459 return 0;
460}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800461
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800462static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
463{
464 int rc;
465 rc = write(bfd->fd, msg->data, msg->len);
466
467 if (rc != msg->len) {
468 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
469 return -1;
470 }
471
472 return rc;
473}
474
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100475/*
476 * Below is the handling of messages coming
477 * from the BSC and need to be forwarded to
478 * a real BSC.
479 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100480
481/*
482 * Remove the connection from the connections list,
483 * remove it from the patching of SCCP header lists
484 * as well. Maybe in the future even close connection..
485 */
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800486void bsc_close_connection(struct bsc_connection *connection)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100487{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100488 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100489
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800490 /* stop the timeout timer */
491 bsc_del_timer(&connection->id_timeout);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800492 bsc_del_timer(&connection->ping_timeout);
493 bsc_del_timer(&connection->pong_timeout);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800494
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100495 /* remove all SCCP connections */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800496 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100497 if (sccp_patch->bsc != connection)
498 continue;
499
Holger Hans Peter Freyther5975c682010-04-23 14:13:27 +0800500 if (sccp_patch->has_remote_ref)
501 nat_send_rlsd(sccp_patch);
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200502 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100503 }
504
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +0200505 /* close endpoints allocated by this BSC */
506 bsc_mgcp_clear_endpoints_for(connection);
507
Holger Hans Peter Freyther53cd9852010-04-22 12:04:36 +0800508 bsc_unregister_fd(&connection->write_queue.bfd);
509 close(connection->write_queue.bfd.fd);
510 write_queue_clear(&connection->write_queue);
511 llist_del(&connection->list_entry);
512
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100513 talloc_free(connection);
514}
515
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800516static void ipaccess_close_bsc(void *data)
517{
Holger Hans Peter Freyther3626e972010-04-17 07:58:17 +0200518 struct sockaddr_in sock;
519 socklen_t len = sizeof(sock);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800520 struct bsc_connection *conn = data;
521
Holger Hans Peter Freyther3626e972010-04-17 07:58:17 +0200522
523 getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
524 LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n",
525 inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800526 bsc_close_connection(conn);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800527}
528
529static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
530{
531 struct bsc_config *conf;
532 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
533
Holger Hans Peter Freyther4dc44ea2010-05-02 18:58:10 +0800534 if (bsc->cfg) {
535 LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n",
536 bsc->write_queue.bfd.fd, bsc->cfg->nr);
537 return;
538 }
539
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800540 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
541 if (strcmp(conf->token, token) == 0) {
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200542 counter_inc(conf->stats.net.reconn);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800543 bsc->authenticated = 1;
Holger Hans Peter Freytherbfcf5192010-04-06 15:11:34 +0200544 bsc->cfg = conf;
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800545 bsc_del_timer(&bsc->id_timeout);
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800546 LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n",
547 conf->nr, conf->lac, bsc->write_queue.bfd.fd);
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800548 start_ping_pong(bsc);
Holger Hans Peter Freyther8b2302f2010-04-17 07:59:57 +0200549 return;
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800550 }
551 }
Holger Hans Peter Freyther8b2302f2010-04-17 07:59:57 +0200552
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800553 LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token,
554 bsc->write_queue.bfd.fd);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800555}
556
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100557static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100558{
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800559 struct sccp_connections *con;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800560 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100561
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800562 /* Parse and filter messages */
563 parsed = bsc_nat_parse(msg);
564 if (!parsed) {
565 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther45f62952010-03-30 07:35:46 +0200566 msgb_free(msg);
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800567 return -1;
568 }
569
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100570 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800571 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800572
Holger Hans Peter Freyther5a723b62010-04-02 03:42:44 +0200573 /*
574 * check authentication after filtering to not reject auth
575 * responses coming from the BSC. We have to make sure that
576 * nothing from the exit path will forward things to the MSC
577 */
578 if (!bsc->authenticated) {
579 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
580 msgb_free(msg);
581 return -1;
582 }
583
584
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100585 /* modify the SCCP entries */
586 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
587 switch (parsed->sccp_type) {
588 case SCCP_MSG_TYPE_CR:
589 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
590 goto exit2;
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800591 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100592 break;
593 case SCCP_MSG_TYPE_RLSD:
594 case SCCP_MSG_TYPE_CREF:
595 case SCCP_MSG_TYPE_DT1:
596 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther567e8402010-04-06 10:22:34 +0200597 case SCCP_MSG_TYPE_IT:
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800598 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100599 break;
600 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800601 con = patch_sccp_src_ref_to_msc(msg, parsed, bsc);
Holger Hans Peter Freyther38712d02010-03-31 09:15:05 +0200602 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100603 break;
604 case SCCP_MSG_TYPE_UDT:
605 /* simply forward everything */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800606 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100607 break;
608 default:
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800609 LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type);
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800610 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100611 goto exit2;
612 break;
613 }
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200614 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
615 bsc_mgcp_forward(bsc, msg);
616 goto exit2;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800617 } else {
618 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
619 goto exit2;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100620 }
621
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800622 if (con && con->bsc != bsc) {
Holger Hans Peter Freyther4107f982010-04-21 18:49:55 +0800623 LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n",
624 bsc->cfg->nr, con->bsc->cfg->nr);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100625 goto exit2;
626 }
627
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100628 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800629 queue_for_msc(nat->msc_con, msg);
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100630 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800631 return 0;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800632
633exit:
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100634 /* if we filter out the reset send an ack to the BSC */
635 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100636 send_reset_ack(bsc);
637 send_reset_ack(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800638 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
639 /* do we know who is handling this? */
640 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
641 struct tlv_parsed tvp;
642 ipaccess_idtag_parse(&tvp,
643 (unsigned char *) msg->l2h + 2,
644 msgb_l2len(msg) - 2);
645 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
646 ipaccess_auth_bsc(&tvp, bsc);
647 }
648
649 goto exit2;
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100650 }
651
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100652exit2:
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800653 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800654 msgb_free(msg);
655 return -1;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100656}
657
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800658static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100659{
660 int error;
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100661 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100662 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800663 struct ipaccess_head *hh;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100664
665 if (!msg) {
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800666 if (error == 0)
Holger Hans Peter Freyther3c8c8152010-05-01 10:37:15 +0800667 LOGP(DNAT, LOGL_ERROR,
668 "The connection to the BSC Nr: %d was lost. Cleaning it\n",
669 bsc->cfg ? bsc->cfg->nr : -1);
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800670 else
Holger Hans Peter Freyther3c8c8152010-05-01 10:37:15 +0800671 LOGP(DNAT, LOGL_ERROR,
672 "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n",
673 bsc->cfg ? bsc->cfg->nr : -1, error);
Holger Hans Peter Freytherbae6b8b2010-04-23 00:23:03 +0800674
Holger Hans Peter Freyther1512dc22010-04-27 13:21:39 +0800675 bsc_close_connection(bsc);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100676 return -1;
677 }
678
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100679
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100680 LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100681
682 /* Handle messages from the BSC */
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800683 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800684
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800685 /* stop the pong timeout */
686 if (hh->proto == IPAC_PROTO_IPACCESS) {
687 if (msg->l2h[0] == IPAC_MSGT_PONG) {
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800688 bsc_del_timer(&bsc->pong_timeout);
689 msgb_free(msg);
690 return 0;
Holger Hans Peter Freyther453235a2010-06-15 18:51:33 +0800691 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
692 send_pong(bsc);
693 msgb_free(msg);
694 return 0;
Holger Hans Peter Freyther582511a2010-05-02 19:28:59 +0800695 }
696 }
697
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100698 /* FIXME: Currently no PONG is sent to the BSC */
699 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100700 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100701
702 return 0;
703}
704
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100705static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
706{
707 int rc;
708
709 rc = write(bfd->fd, msg->data, msg->len);
710 if (rc != msg->len)
711 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
712
713 return rc;
714}
715
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100716static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
717{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100718 struct bsc_connection *bsc;
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800719 int fd, rc, on;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100720 struct sockaddr_in sa;
721 socklen_t sa_len = sizeof(sa);
722
723 if (!(what & BSC_FD_READ))
724 return 0;
725
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800726 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
727 if (fd < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100728 perror("accept");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800729 return fd;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100730 }
731
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200732 /* count the reconnect */
733 counter_inc(nat->stats.bsc.reconn);
734
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200735 /*
736 * if we are not connected to a msc... just close the socket
737 */
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800738 if (!nat->msc_con->is_connected) {
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200739 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800740 close(fd);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200741 return 0;
742 }
743
Holger Hans Peter Freyther150fa582010-05-05 18:58:13 +0800744 on = 1;
Holger Hans Peter Freytherd58afeb2010-05-05 20:42:14 +0800745 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800746 if (rc != 0)
Holger Hans Peter Freyther150fa582010-05-05 18:58:13 +0800747 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
748
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100749 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100750 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100751
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100752 /*
753 *
754 */
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800755 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100756 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100757 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800758 close(fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100759 return -1;
760 }
761
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800762 bsc->write_queue.bfd.data = bsc;
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800763 bsc->write_queue.bfd.fd = fd;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800764 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100765 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800766 bsc->write_queue.bfd.when = BSC_FD_READ;
767 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100768 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800769 close(fd);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100770 talloc_free(bsc);
771 return -2;
772 }
773
Holger Hans Peter Freyther4cc58092010-05-05 17:03:44 +0800774 LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n",
Holger Hans Peter Freyther74d00422010-05-05 20:33:34 +0800775 fd, inet_ntoa(sa.sin_addr));
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800776 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100777 send_id_ack(bsc);
778 send_id_req(bsc);
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200779 send_mgcp_reset(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800780
781 /*
782 * start the hangup timer
783 */
784 bsc->id_timeout.data = bsc;
785 bsc->id_timeout.cb = ipaccess_close_bsc;
Holger Hans Peter Freytheracd30782010-05-05 16:57:38 +0800786 bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100787 return 0;
788}
789
790static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
791{
792 struct sockaddr_in addr;
793 int ret, on = 1;
794
795 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
796 bfd->cb = ipaccess_listen_bsc_cb;
797 bfd->when = BSC_FD_READ;
798
799 memset(&addr, 0, sizeof(addr));
800 addr.sin_family = AF_INET;
801 addr.sin_port = htons(port);
802 addr.sin_addr.s_addr = in_addr->s_addr;
803
804 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
805
806 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
807 if (ret < 0) {
808 fprintf(stderr, "Could not bind the BSC socket %s\n",
809 strerror(errno));
810 return -EIO;
811 }
812
813 ret = listen(bfd->fd, 1);
814 if (ret < 0) {
815 perror("listen");
816 return ret;
817 }
818
819 ret = bsc_register_fd(bfd);
820 if (ret < 0) {
821 perror("register_listen_fd");
822 return ret;
823 }
824 return 0;
825}
826
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800827static void print_usage()
828{
829 printf("Usage: bsc_nat\n");
830}
831
832static void print_help()
833{
834 printf(" Some useful help...\n");
835 printf(" -h --help this text\n");
836 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
837 printf(" -s --disable-color\n");
838 printf(" -c --config-file filename The config file to use.\n");
839 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100840 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800841}
842
843static void handle_options(int argc, char** argv)
844{
845 while (1) {
846 int option_index = 0, c;
847 static struct option long_options[] = {
848 {"help", 0, 0, 'h'},
849 {"debug", 1, 0, 'd'},
850 {"config-file", 1, 0, 'c'},
851 {"disable-color", 0, 0, 's'},
852 {"timestamp", 0, 0, 'T'},
853 {"msc", 1, 0, 'm'},
854 {"local", 1, 0, 'l'},
855 {0, 0, 0, 0}
856 };
857
858 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
859 long_options, &option_index);
860 if (c == -1)
861 break;
862
863 switch (c) {
864 case 'h':
865 print_usage();
866 print_help();
867 exit(0);
868 case 's':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +0800869 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800870 break;
871 case 'd':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +0800872 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800873 break;
874 case 'c':
875 config_file = strdup(optarg);
876 break;
877 case 'T':
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +0800878 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800879 break;
880 case 'm':
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +0800881 msc_ip = optarg;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800882 break;
883 case 'l':
884 inet_aton(optarg, &local_addr);
885 break;
886 default:
887 /* ignore */
888 break;
889 }
890 }
891}
892
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100893static void signal_handler(int signal)
894{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100895 switch (signal) {
896 case SIGABRT:
897 /* in case of abort, we want to obtain a talloc report
898 * and then return to the caller, who will abort the process */
899 case SIGUSR1:
900 talloc_report_full(tall_bsc_ctx, stderr);
901 break;
902 default:
903 break;
904 }
905}
906
Holger Hans Peter Freyther023a5502010-06-15 18:51:25 +0800907extern void *tall_msgb_ctx;
908extern void *tall_ctr_ctx;
909static void talloc_init_ctx()
910{
911 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
912 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
913 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
914}
915
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800916int main(int argc, char** argv)
917{
Holger Hans Peter Freyther023a5502010-06-15 18:51:25 +0800918 talloc_init_ctx();
919
Holger Hans Peter Freyther09371382010-06-15 18:51:18 +0800920
Holger Hans Peter Freyther4acca1a2010-06-15 19:14:12 +0800921 log_init(&log_info);
922 stderr_target = log_target_create_stderr();
923 log_add_target(stderr_target);
924 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800925
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800926 nat = bsc_nat_alloc();
927 if (!nat) {
928 fprintf(stderr, "Failed to allocate the BSC nat.\n");
929 return -4;
930 }
931
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +0800932 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +0800933 if (!nat->mgcp_cfg) {
934 fprintf(stderr, "Failed to allocate MGCP cfg.\n");
935 return -5;
936 }
937
938 /* parse options */
939 local_addr.s_addr = INADDR_ANY;
940 handle_options(argc, argv);
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +0800941
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800942 /* init vty and parse */
943 bsc_nat_vty_init(nat);
944 telnet_init(NULL, 4244);
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +0800945 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800946 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
947 return -3;
948 }
949
Holger Hans Peter Freyther9226d702010-06-15 18:51:04 +0800950 /* over rule the VTY config */
951 if (msc_ip)
952 bsc_nat_set_msc_ip(nat, msc_ip);
953
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800954 /* seed the PRNG */
955 srand(time(NULL));
956
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200957 /*
958 * Setup the MGCP code..
959 */
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800960 if (bsc_mgcp_nat_init(nat) != 0)
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200961 return -4;
962
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100963 /* connect to the MSC */
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800964 nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port);
965 if (!nat->msc_con) {
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800966 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100967 exit(1);
968 }
969
Holger Hans Peter Freyther8e2e0ac2010-05-11 19:07:39 +0800970 nat->msc_con->connection_loss = msc_connection_was_lost;
971 nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
972 nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
973 bsc_msc_connect(nat->msc_con);
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800974
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100975 /* wait for the BSC */
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +0100976 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100977 fprintf(stderr, "Failed to listen for BSC.\n");
978 exit(1);
979 }
980
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100981 signal(SIGABRT, &signal_handler);
982 signal(SIGUSR1, &signal_handler);
983 signal(SIGPIPE, SIG_IGN);
984
985 while (1) {
986 bsc_select_main(0);
987 }
988
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800989 return 0;
990}