blob: e8a6b199be1b7a4eec8fc99edc3bcf1726db1e45 [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>
26#include <arpa/inet.h>
27
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +010028#include <errno.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010029#include <signal.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080030#include <stdio.h>
31#include <stdlib.h>
Holger Hans Peter Freytherfd012d52010-01-12 21:36:08 +010032#include <time.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080033#include <unistd.h>
34
35#define _GNU_SOURCE
36#include <getopt.h>
37
38#include <openbsc/debug.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010039#include <openbsc/bsc_msc.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080040#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +010041#include <openbsc/bssap.h>
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +010042#include <openbsc/ipaccess.h>
43#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080044#include <openbsc/telnet_interface.h>
45
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080046#include <osmocore/talloc.h>
47
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080048#include <vty/vty.h>
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080049
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080050#include <sccp/sccp.h>
51
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080052struct debug_target *stderr_target;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080053static const char *config_file = "bsc-nat.cfg";
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +080054static char *msc_address = "127.0.0.1";
55static struct in_addr local_addr;
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +080056static struct bsc_msc_connection *msc_con;
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +010057static struct bsc_fd bsc_listen;
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 Freyther2ec55172010-03-26 09:18:02 +010061static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +020062static void remove_bsc_connection(struct bsc_connection *connection);
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 Freyther2ec55172010-03-26 09:18:02 +010092static void send_reset_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +010093{
94 static const u_int8_t gsm_reset_ack[] = {
95 0x00, 0x13, 0xfd,
96 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
97 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
98 0x00, 0x01, 0x31,
99 };
100
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100101 bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack));
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100102}
103
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100104static void send_id_ack(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100105{
106 static const u_int8_t id_ack[] = {
107 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
108 };
109
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100110 bsc_write(bsc, id_ack, sizeof(id_ack));
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100111}
112
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100113static void send_id_req(struct bsc_connection *bsc)
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100114{
115 static const u_int8_t id_req[] = {
116 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
117 0x01, IPAC_IDTAG_UNIT,
118 0x01, IPAC_IDTAG_MACADDR,
119 0x01, IPAC_IDTAG_LOCATION1,
120 0x01, IPAC_IDTAG_LOCATION2,
121 0x01, IPAC_IDTAG_EQUIPVERS,
122 0x01, IPAC_IDTAG_SWVERSION,
123 0x01, IPAC_IDTAG_UNITNAME,
124 0x01, IPAC_IDTAG_SERNR,
125 };
126
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100127 bsc_write(bsc, id_req, sizeof(id_req));
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100128}
129
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200130static void send_mgcp_reset(struct bsc_connection *bsc)
131{
132 static const u_int8_t mgcp_reset[] = {
133 "RSIP 1 13@mgw MGCP 1.0\r\n"
134 };
135
136 bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
137}
138
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100139/*
140 * Below is the handling of messages coming
141 * from the MSC and need to be forwarded to
142 * a real BSC.
143 */
144static void initialize_msc_if_needed()
145{
146 static int init = 0;
147 init = 1;
148
149 /* do we need to send a GSM 08.08 message here? */
150}
151
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100152/*
153 * Currently we are lacking refcounting so we need to copy each message.
154 */
155static void bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100156{
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100157 struct msgb *msg;
158
159 if (length > 4096) {
160 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
161 return;
162 }
163
164 msg = msgb_alloc(4096, "to-bsc");
165 if (!msg) {
166 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
167 return;
168 }
169
170 msgb_put(msg, length);
171 memcpy(msg->data, data, length);
172 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
173 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
174 msgb_free(msg);
175 }
Holger Hans Peter Freytherad2136b2010-03-26 07:20:59 +0100176}
177
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100178static int forward_sccp_to_bts(struct msgb *msg)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100179{
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800180 struct sccp_connections *con;
181 struct bsc_connection *bsc;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800182 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100183
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100184 /* filter, drop, patch the message? */
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800185 parsed = bsc_nat_parse(msg);
186 if (!parsed) {
187 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100188 return -1;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800189 }
190
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100191 if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800192 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800193
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100194 /* Route and modify the SCCP packet */
195 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
196 switch (parsed->sccp_type) {
197 case SCCP_MSG_TYPE_UDT:
198 /* forward UDT messages to every BSC */
199 goto send_to_all;
200 break;
201 case SCCP_MSG_TYPE_RLSD:
202 case SCCP_MSG_TYPE_CREF:
203 case SCCP_MSG_TYPE_DT1:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800204 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800205 if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
206 if (con) {
207 if (bsc_mgcp_assign(con, msg) != 0)
208 LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
209 } else
210 LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
211 }
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200212 break;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100213 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800214 con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
215 if (!con || update_sccp_src_ref(con, parsed) != 0)
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200216 goto exit;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800217 break;
218 case SCCP_MSG_TYPE_RLC:
219 LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
220 goto exit;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100221 break;
222 case SCCP_MSG_TYPE_CR:
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100223 /* MSC never opens a SCCP connection, fall through */
224 default:
225 goto exit;
226 }
227 }
228
229 talloc_free(parsed);
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800230 if (!con)
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100231 return -1;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800232 if (!con->bsc->authenticated) {
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800233 LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100234 return -1;
235 }
236
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800237 bsc_write(con->bsc, msg->data, msg->len);
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100238 return 0;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100239
240send_to_all:
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800241 /*
242 * Filter Paging from the network. We do not want to send a PAGING
243 * Command to every BSC in our network. We will analys the PAGING
244 * message and then send it to the authenticated messages...
245 */
246 if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
Holger Hans Peter Freyther21c4d9e2010-03-30 05:57:42 +0200247 bsc = bsc_nat_find_bsc(nat, msg);
248 if (bsc)
249 bsc_write(bsc, msg->data, msg->len);
250 else
251 LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n");
Holger Hans Peter Freytherfd2c7572010-06-15 18:46:36 +0800252
253 goto exit;
254 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100255 /* currently send this to every BSC connected */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800256 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freytherfb5a4872010-02-08 23:24:32 +0100257 if (!bsc->authenticated)
258 continue;
259
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100260 bsc_write(bsc, msg->data, msg->len);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100261 }
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800262
263exit:
264 talloc_free(parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100265 return 0;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100266}
267
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800268static void msc_connection_was_lost(struct bsc_msc_connection *con)
269{
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200270 struct bsc_connection *bsc, *tmp;
271
272 LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
273 llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
274 remove_bsc_connection(bsc);
275
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +0200276 bsc_mgcp_free_endpoints(nat);
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200277 bsc_msc_schedule_connect(con);
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800278}
279
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800280static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100281{
282 int error;
283 struct msgb *msg = ipaccess_read_msg(bfd, &error);
284 struct ipaccess_head *hh;
285
286 if (!msg) {
287 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100288 LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800289 bsc_msc_lost(msc_con);
290 return -1;
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100291 }
292
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100293 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100294 return -1;
295 }
296
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100297 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 +0100298
299 /* handle base message handling */
300 hh = (struct ipaccess_head *) msg->data;
301 ipaccess_rcvmsg_base(msg, bfd);
302
303 /* initialize the networking. This includes sending a GSM08.08 message */
304 if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK)
305 initialize_msc_if_needed();
306 else if (hh->proto == IPAC_PROTO_SCCP)
307 forward_sccp_to_bts(msg);
308
Holger Hans Peter Freyther418fe112010-06-15 18:46:48 +0800309 msgb_free(msg);
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100310 return 0;
311}
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800312
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800313static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
314{
315 int rc;
316 rc = write(bfd->fd, msg->data, msg->len);
317
318 if (rc != msg->len) {
319 LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
320 return -1;
321 }
322
323 return rc;
324}
325
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100326/*
327 * Below is the handling of messages coming
328 * from the BSC and need to be forwarded to
329 * a real BSC.
330 */
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100331
332/*
333 * Remove the connection from the connections list,
334 * remove it from the patching of SCCP header lists
335 * as well. Maybe in the future even close connection..
336 */
337static void remove_bsc_connection(struct bsc_connection *connection)
338{
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100339 struct sccp_connections *sccp_patch, *tmp;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800340 bsc_unregister_fd(&connection->write_queue.bfd);
341 close(connection->write_queue.bfd.fd);
Holger Hans Peter Freytherffcf79c2010-03-26 09:27:08 +0100342 write_queue_clear(&connection->write_queue);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100343 llist_del(&connection->list_entry);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100344
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800345 /* stop the timeout timer */
346 bsc_del_timer(&connection->id_timeout);
347
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100348 /* remove all SCCP connections */
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800349 llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100350 if (sccp_patch->bsc != connection)
351 continue;
352
Holger Hans Peter Freythera207db22010-03-26 09:28:40 +0100353#warning "TODO: Send a RLSD to the MSC. Or at least a clear command."
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200354 sccp_connection_destroy(sccp_patch);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100355 }
356
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100357 talloc_free(connection);
358}
359
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800360static void ipaccess_close_bsc(void *data)
361{
362 struct bsc_connection *conn = data;
363
364 LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
365 remove_bsc_connection(conn);
366}
367
368static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
369{
370 struct bsc_config *conf;
371 const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
372
373 llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
374 if (strcmp(conf->token, token) == 0) {
375 bsc->authenticated = 1;
376 bsc->lac = conf->lac;
377 bsc_del_timer(&bsc->id_timeout);
378 break;
379 }
380 }
381}
382
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100383static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100384{
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800385 struct sccp_connections *con;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800386 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100387
Holger Hans Peter Freyther45f62952010-03-30 07:35:46 +0200388 if (!bsc->authenticated) {
389 LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
390 msgb_free(msg);
391 return -1;
392 }
393
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800394 /* Parse and filter messages */
395 parsed = bsc_nat_parse(msg);
396 if (!parsed) {
397 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
Holger Hans Peter Freyther45f62952010-03-30 07:35:46 +0200398 msgb_free(msg);
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800399 return -1;
400 }
401
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100402 if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800403 goto exit;
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800404
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100405 /* modify the SCCP entries */
406 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
407 switch (parsed->sccp_type) {
408 case SCCP_MSG_TYPE_CR:
409 if (create_sccp_src_ref(bsc, msg, parsed) != 0)
410 goto exit2;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800411 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100412 break;
413 case SCCP_MSG_TYPE_RLSD:
414 case SCCP_MSG_TYPE_CREF:
415 case SCCP_MSG_TYPE_DT1:
416 case SCCP_MSG_TYPE_CC:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800417 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100418 break;
419 case SCCP_MSG_TYPE_RLC:
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800420 con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
Holger Hans Peter Freyther38712d02010-03-31 09:15:05 +0200421 remove_sccp_src_ref(bsc, msg, parsed);
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100422 break;
423 case SCCP_MSG_TYPE_UDT:
424 /* simply forward everything */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800425 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100426 break;
427 default:
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800428 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 +0800429 con = NULL;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100430 goto exit2;
431 break;
432 }
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200433 } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
434 bsc_mgcp_forward(bsc, msg);
435 goto exit2;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800436 } else {
437 LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
438 goto exit2;
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100439 }
440
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800441 if (con && con->bsc != bsc) {
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100442 LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
443 goto exit2;
444 }
445
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100446 /* send the non-filtered but maybe modified msg */
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800447 if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800448 LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
449 msgb_free(msg);
450 }
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100451 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800452 return 0;
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800453
454exit:
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100455 /* if we filter out the reset send an ack to the BSC */
456 if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100457 send_reset_ack(bsc);
458 send_reset_ack(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800459 } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
460 /* do we know who is handling this? */
461 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
462 struct tlv_parsed tvp;
463 ipaccess_idtag_parse(&tvp,
464 (unsigned char *) msg->l2h + 2,
465 msgb_l2len(msg) - 2);
466 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
467 ipaccess_auth_bsc(&tvp, bsc);
468 }
469
470 goto exit2;
Holger Hans Peter Freyther722ead82010-01-30 12:45:10 +0100471 }
472
Holger Hans Peter Freythere83917d2010-01-31 09:46:21 +0100473exit2:
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800474 talloc_free(parsed);
Holger Hans Peter Freyther257a8cc2010-06-15 18:47:02 +0800475 msgb_free(msg);
476 return -1;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100477}
478
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800479static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100480{
481 int error;
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100482 struct bsc_connection *bsc = bfd->data;
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100483 struct msgb *msg = ipaccess_read_msg(bfd, &error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100484
485 if (!msg) {
486 if (error == 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100487 LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100488 remove_bsc_connection(bsc);
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100489 } else {
490 LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100491 }
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100492 return -1;
493 }
494
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100495
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100496 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 +0100497
498 /* Handle messages from the BSC */
499 /* FIXME: Currently no PONG is sent to the BSC */
500 /* FIXME: Currently no ID ACK is sent to the BSC */
Holger Hans Peter Freyther4ce32702010-03-26 07:24:34 +0100501 forward_sccp_to_msc(bsc, msg);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100502
503 return 0;
504}
505
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100506static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
507{
508 int rc;
509
510 rc = write(bfd->fd, msg->data, msg->len);
511 if (rc != msg->len)
512 LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
513
514 return rc;
515}
516
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100517static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
518{
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100519 struct bsc_connection *bsc;
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100520 int ret;
521 struct sockaddr_in sa;
522 socklen_t sa_len = sizeof(sa);
523
524 if (!(what & BSC_FD_READ))
525 return 0;
526
527 ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
528 if (ret < 0) {
529 perror("accept");
530 return ret;
531 }
532
Holger Hans Peter Freyther3399df32010-03-29 08:04:09 +0200533 /*
534 * if we are not connected to a msc... just close the socket
535 */
536 if (!msc_con->is_connected) {
537 LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
538 return 0;
539 }
540
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100541 /* todo... do something with the connection */
Holger Hans Peter Freyther738dbdf2010-01-12 21:35:32 +0100542 /* todo... use GNUtls to see if we want to trust this as a BTS */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100543
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100544 /*
545 *
546 */
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800547 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100548 if (!bsc) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100549 LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100550 close(ret);
551 return -1;
552 }
553
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800554 write_queue_init(&bsc->write_queue, 100);
555 bsc->write_queue.bfd.data = bsc;
556 bsc->write_queue.bfd.fd = ret;
557 bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
Holger Hans Peter Freyther2ec55172010-03-26 09:18:02 +0100558 bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +0800559 bsc->write_queue.bfd.when = BSC_FD_READ;
560 if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100561 LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
Holger Hans Peter Freyther4a629602010-01-13 09:28:12 +0100562 close(ret);
563 talloc_free(bsc);
564 return -2;
565 }
566
Holger Hans Peter Freyther8f99b822010-01-29 05:58:43 +0100567 LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800568 llist_add(&bsc->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther809d6fa2010-03-26 07:41:54 +0100569 send_id_ack(bsc);
570 send_id_req(bsc);
Holger Hans Peter Freyther8c61cdb2010-03-31 07:30:58 +0200571 send_mgcp_reset(bsc);
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +0800572
573 /*
574 * start the hangup timer
575 */
576 bsc->id_timeout.data = bsc;
577 bsc->id_timeout.cb = ipaccess_close_bsc;
578 bsc_schedule_timer(&bsc->id_timeout, 2, 0);
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100579 return 0;
580}
581
582static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
583{
584 struct sockaddr_in addr;
585 int ret, on = 1;
586
587 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
588 bfd->cb = ipaccess_listen_bsc_cb;
589 bfd->when = BSC_FD_READ;
590
591 memset(&addr, 0, sizeof(addr));
592 addr.sin_family = AF_INET;
593 addr.sin_port = htons(port);
594 addr.sin_addr.s_addr = in_addr->s_addr;
595
596 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
597
598 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
599 if (ret < 0) {
600 fprintf(stderr, "Could not bind the BSC socket %s\n",
601 strerror(errno));
602 return -EIO;
603 }
604
605 ret = listen(bfd->fd, 1);
606 if (ret < 0) {
607 perror("listen");
608 return ret;
609 }
610
611 ret = bsc_register_fd(bfd);
612 if (ret < 0) {
613 perror("register_listen_fd");
614 return ret;
615 }
616 return 0;
617}
618
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800619static void print_usage()
620{
621 printf("Usage: bsc_nat\n");
622}
623
624static void print_help()
625{
626 printf(" Some useful help...\n");
627 printf(" -h --help this text\n");
628 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
629 printf(" -s --disable-color\n");
630 printf(" -c --config-file filename The config file to use.\n");
631 printf(" -m --msc=IP. The address of the MSC.\n");
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100632 printf(" -l --local=IP. The local address of this BSC.\n");
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800633}
634
635static void handle_options(int argc, char** argv)
636{
637 while (1) {
638 int option_index = 0, c;
639 static struct option long_options[] = {
640 {"help", 0, 0, 'h'},
641 {"debug", 1, 0, 'd'},
642 {"config-file", 1, 0, 'c'},
643 {"disable-color", 0, 0, 's'},
644 {"timestamp", 0, 0, 'T'},
645 {"msc", 1, 0, 'm'},
646 {"local", 1, 0, 'l'},
647 {0, 0, 0, 0}
648 };
649
650 c = getopt_long(argc, argv, "hd:sTPc:m:l:",
651 long_options, &option_index);
652 if (c == -1)
653 break;
654
655 switch (c) {
656 case 'h':
657 print_usage();
658 print_help();
659 exit(0);
660 case 's':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800661 debug_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800662 break;
663 case 'd':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800664 debug_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800665 break;
666 case 'c':
667 config_file = strdup(optarg);
668 break;
669 case 'T':
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800670 debug_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800671 break;
672 case 'm':
673 msc_address = strdup(optarg);
674 break;
675 case 'l':
676 inet_aton(optarg, &local_addr);
677 break;
678 default:
679 /* ignore */
680 break;
681 }
682 }
683}
684
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100685static void signal_handler(int signal)
686{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100687 switch (signal) {
688 case SIGABRT:
689 /* in case of abort, we want to obtain a talloc report
690 * and then return to the caller, who will abort the process */
691 case SIGUSR1:
692 talloc_report_full(tall_bsc_ctx, stderr);
693 break;
694 default:
695 break;
696 }
697}
698
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800699int main(int argc, char** argv)
700{
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100701
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +0800702 debug_init();
703 stderr_target = debug_target_create_stderr();
704 debug_add_target(stderr_target);
705 debug_set_all_filter(stderr_target, 1);
706
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800707 /* parse options */
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100708 local_addr.s_addr = INADDR_ANY;
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800709 handle_options(argc, argv);
710
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800711 nat = bsc_nat_alloc();
712 if (!nat) {
713 fprintf(stderr, "Failed to allocate the BSC nat.\n");
714 return -4;
715 }
716
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +0800717 nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
718
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800719 /* init vty and parse */
720 bsc_nat_vty_init(nat);
721 telnet_init(NULL, 4244);
Holger Hans Peter Freyther3660e4d2010-06-15 18:50:26 +0800722 if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800723 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
724 return -3;
725 }
726
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800727 /* seed the PRNG */
728 srand(time(NULL));
729
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200730 /*
731 * Setup the MGCP code..
732 */
733 if (bsc_mgcp_init(nat) != 0)
734 return -4;
735
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100736 /* connect to the MSC */
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800737 msc_con = bsc_msc_create(msc_address, 5000);
738 if (!msc_con) {
739 fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100740 exit(1);
741 }
742
Holger Hans Peter Freyther4a860a02010-06-15 18:47:29 +0800743 msc_con->connection_loss = msc_connection_was_lost;
744 msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
745 msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
746 bsc_msc_connect(msc_con);
747
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100748 /* wait for the BSC */
Holger Hans Peter Freytherbea0ac62010-03-26 06:51:04 +0100749 if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
Holger Hans Peter Freythere8fa0f12010-01-12 21:34:54 +0100750 fprintf(stderr, "Failed to listen for BSC.\n");
751 exit(1);
752 }
753
Holger Hans Peter Freythere907cb22010-01-12 21:15:08 +0100754 signal(SIGABRT, &signal_handler);
755 signal(SIGUSR1, &signal_handler);
756 signal(SIGPIPE, SIG_IGN);
757
758 while (1) {
759 bsc_select_main(0);
760 }
761
Holger Hans Peter Freyther89d9fd92010-06-15 18:44:42 +0800762 return 0;
763}