Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * 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 Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 29 | #include <signal.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5aa25ae | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #define _GNU_SOURCE |
| 36 | #include <getopt.h> |
| 37 | |
| 38 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 39 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 40 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 41 | #include <openbsc/bssap.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/ipaccess.h> |
| 43 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 44 | #include <openbsc/telnet_interface.h> |
| 45 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 46 | #include <osmocore/talloc.h> |
| 47 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 48 | #include <vty/vty.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 49 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 50 | #include <sccp/sccp.h> |
| 51 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 52 | struct debug_target *stderr_target; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 53 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 54 | static char *msc_address = "127.0.0.1"; |
| 55 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 56 | static struct write_queue msc_queue; |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 57 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 59 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 60 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 61 | static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 62 | |
| 63 | static struct bsc_nat *bsc_nat_alloc(void) |
| 64 | { |
| 65 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 66 | if (!nat) |
| 67 | return NULL; |
| 68 | |
| 69 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 70 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 71 | INIT_LLIST_HEAD(&nat->bsc_configs); |
| 72 | return nat; |
| 73 | } |
| 74 | |
| 75 | static struct bsc_connection *bsc_connection_alloc(void) |
| 76 | { |
| 77 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 78 | if (!con) |
| 79 | return NULL; |
| 80 | |
| 81 | return con; |
| 82 | } |
| 83 | |
| 84 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac) |
| 85 | { |
| 86 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 87 | if (!conf) |
| 88 | return NULL; |
| 89 | |
| 90 | conf->token = talloc_strdup(conf, token); |
| 91 | conf->lac = lac; |
| 92 | conf->nr = nat->num_bsc; |
| 93 | conf->nat = nat; |
| 94 | |
| 95 | llist_add(&conf->entry, &nat->bsc_configs); |
| 96 | ++nat->num_bsc; |
| 97 | |
| 98 | return conf; |
| 99 | } |
| 100 | |
| 101 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 102 | { |
| 103 | struct bsc_config *conf; |
| 104 | |
| 105 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 106 | if (conf->nr == num) |
| 107 | return conf; |
| 108 | |
| 109 | return NULL; |
| 110 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 111 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 112 | /* |
| 113 | * below are stubs we need to link |
| 114 | */ |
| 115 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
| 116 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state) |
| 117 | { |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 122 | {} |
| 123 | |
| 124 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 125 | { |
| 126 | return -1; |
| 127 | } |
| 128 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 129 | static int send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 130 | { |
| 131 | static const u_int8_t gsm_reset_ack[] = { |
| 132 | 0x00, 0x13, 0xfd, |
| 133 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 134 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 135 | 0x00, 0x01, 0x31, |
| 136 | }; |
| 137 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 138 | return bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack)); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 141 | /* |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 142 | * SCCP patching below |
| 143 | */ |
| 144 | |
| 145 | /* check if we are using this ref for patched already */ |
| 146 | static int sccp_ref_is_free(struct sccp_source_reference *ref) |
| 147 | { |
| 148 | struct sccp_connections *conn; |
| 149 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 150 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 151 | if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0) |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* copied from sccp.c */ |
| 159 | static int assign_src_local_reference(struct sccp_source_reference *ref) |
| 160 | { |
| 161 | static u_int32_t last_ref = 0x50000; |
| 162 | int wrapped = 0; |
| 163 | |
| 164 | do { |
| 165 | struct sccp_source_reference reference; |
| 166 | reference.octet1 = (last_ref >> 0) & 0xff; |
| 167 | reference.octet2 = (last_ref >> 8) & 0xff; |
| 168 | reference.octet3 = (last_ref >> 16) & 0xff; |
| 169 | |
| 170 | ++last_ref; |
| 171 | /* do not use the reversed word and wrap around */ |
| 172 | if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) { |
| 173 | LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n"); |
| 174 | last_ref = 0; |
| 175 | ++wrapped; |
| 176 | } |
| 177 | |
| 178 | if (sccp_ref_is_free(&reference) == 0) { |
| 179 | *ref = reference; |
| 180 | return 0; |
| 181 | } |
| 182 | } while (wrapped != 2); |
| 183 | |
| 184 | LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n"); |
| 185 | return -1; |
| 186 | } |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame] | 187 | |
| 188 | static int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 189 | { |
| 190 | struct sccp_connections *conn; |
| 191 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 192 | conn = talloc_zero(nat, struct sccp_connections); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 193 | if (!conn) { |
| 194 | LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | conn->real_ref = *parsed->src_local_ref; |
| 199 | if (assign_src_local_reference(&conn->patched_ref) != 0) { |
| 200 | LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n"); |
| 201 | talloc_free(conn); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame] | 208 | static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 209 | { |
| 210 | struct sccp_connections *conn; |
| 211 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 212 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 213 | if (memcmp(parsed->src_local_ref, |
| 214 | &conn->real_ref, sizeof(conn->real_ref)) == 0) { |
| 215 | if (bsc != conn->bsc) { |
| 216 | LOGP(DNAT, LOGL_ERROR, "Someone else...\n"); |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | llist_del(&conn->list_entry); |
| 222 | talloc_free(conn); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n"); |
| 228 | } |
| 229 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame] | 230 | static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 231 | { |
| 232 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 233 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 234 | if (memcmp(parsed->dest_local_ref, |
| 235 | &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) { |
| 236 | memcpy(parsed->dest_local_ref, |
| 237 | &conn->patched_ref, sizeof(*parsed->dest_local_ref)); |
| 238 | return conn->bsc; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame] | 245 | static struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 246 | { |
| 247 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 248 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 249 | if (memcmp(parsed->src_local_ref, |
| 250 | &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) { |
| 251 | memcpy(parsed->src_local_ref, |
| 252 | &conn->patched_ref, sizeof(*parsed->src_local_ref)); |
| 253 | return conn->bsc; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | /* |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 261 | * Below is the handling of messages coming |
| 262 | * from the MSC and need to be forwarded to |
| 263 | * a real BSC. |
| 264 | */ |
| 265 | static void initialize_msc_if_needed() |
| 266 | { |
| 267 | static int init = 0; |
| 268 | init = 1; |
| 269 | |
| 270 | /* do we need to send a GSM 08.08 message here? */ |
| 271 | } |
| 272 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 273 | static int bsc_write(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length) |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 274 | { |
| 275 | return write(bsc->write_queue.bfd.fd, data, length); |
| 276 | } |
| 277 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 278 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 279 | { |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 280 | struct bsc_connection *bsc = NULL; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 281 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 282 | int rc; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 283 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 284 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 285 | parsed = bsc_nat_parse(msg); |
| 286 | if (!parsed) { |
| 287 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 288 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 289 | } |
| 290 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 291 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 292 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 293 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 294 | /* Route and modify the SCCP packet */ |
| 295 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 296 | switch (parsed->sccp_type) { |
| 297 | case SCCP_MSG_TYPE_UDT: |
| 298 | /* forward UDT messages to every BSC */ |
| 299 | goto send_to_all; |
| 300 | break; |
| 301 | case SCCP_MSG_TYPE_RLSD: |
| 302 | case SCCP_MSG_TYPE_CREF: |
| 303 | case SCCP_MSG_TYPE_DT1: |
| 304 | case SCCP_MSG_TYPE_CC: |
| 305 | bsc = patch_sccp_src_ref_to_bsc(msg, parsed); |
| 306 | break; |
| 307 | case SCCP_MSG_TYPE_CR: |
| 308 | case SCCP_MSG_TYPE_RLC: |
| 309 | /* MSC never opens a SCCP connection, fall through */ |
| 310 | default: |
| 311 | goto exit; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | talloc_free(parsed); |
| 316 | if (!bsc) |
| 317 | return -1; |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 318 | if (!bsc->authenticated) { |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 319 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 323 | return bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 324 | |
| 325 | send_to_all: |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 326 | /* |
| 327 | * Filter Paging from the network. We do not want to send a PAGING |
| 328 | * Command to every BSC in our network. We will analys the PAGING |
| 329 | * message and then send it to the authenticated messages... |
| 330 | */ |
| 331 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
| 332 | int data_length; |
| 333 | const u_int8_t *data; |
| 334 | struct tlv_parsed tp; |
| 335 | int i = 0; |
| 336 | |
| 337 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 338 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 339 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 340 | goto exit; |
| 341 | } |
| 342 | |
| 343 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 344 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 345 | if (data[0] != CELL_IDENT_LAC) { |
| 346 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]); |
| 347 | goto exit; |
| 348 | } |
| 349 | |
| 350 | /* go through each LAC and forward the message */ |
| 351 | for (i = 1; i < data_length - 1; i += 2) { |
| 352 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
| 353 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 354 | if (!bsc->authenticated || _lac != bsc->lac) |
| 355 | continue; |
| 356 | |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 357 | rc = bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 358 | if (rc < msg->len) |
| 359 | LOGP(DNAT, LOGL_ERROR, |
| 360 | "Failed to write message to BTS: %d\n", rc); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | goto exit; |
| 365 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 366 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 367 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 368 | if (!bsc->authenticated) |
| 369 | continue; |
| 370 | |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 371 | rc = bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 372 | |
| 373 | /* try the next one */ |
| 374 | if (rc < msg->len) |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 375 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to BTS: %d\n", rc); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 376 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 377 | |
| 378 | exit: |
| 379 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 380 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 383 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 384 | { |
| 385 | int error; |
| 386 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 387 | struct ipaccess_head *hh; |
| 388 | |
| 389 | if (!msg) { |
| 390 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 391 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 392 | exit(-2); |
| 393 | } |
| 394 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 395 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 396 | return -1; |
| 397 | } |
| 398 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 399 | LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 400 | |
| 401 | /* handle base message handling */ |
| 402 | hh = (struct ipaccess_head *) msg->data; |
| 403 | ipaccess_rcvmsg_base(msg, bfd); |
| 404 | |
| 405 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 406 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 407 | initialize_msc_if_needed(); |
| 408 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 409 | forward_sccp_to_bts(msg); |
| 410 | |
Holger Hans Peter Freyther | aad68b5 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 411 | msgb_free(msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 414 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 415 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 416 | { |
| 417 | int rc; |
| 418 | rc = write(bfd->fd, msg->data, msg->len); |
| 419 | |
| 420 | if (rc != msg->len) { |
| 421 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | return rc; |
| 426 | } |
| 427 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 428 | /* |
| 429 | * Below is the handling of messages coming |
| 430 | * from the BSC and need to be forwarded to |
| 431 | * a real BSC. |
| 432 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * Remove the connection from the connections list, |
| 436 | * remove it from the patching of SCCP header lists |
| 437 | * as well. Maybe in the future even close connection.. |
| 438 | */ |
| 439 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 440 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 441 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 442 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 443 | close(connection->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 444 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 445 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 446 | /* stop the timeout timer */ |
| 447 | bsc_del_timer(&connection->id_timeout); |
| 448 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 449 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 450 | llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 451 | if (sccp_patch->bsc != connection) |
| 452 | continue; |
| 453 | |
| 454 | llist_del(&sccp_patch->list_entry); |
| 455 | talloc_free(sccp_patch); |
| 456 | } |
| 457 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 458 | talloc_free(connection); |
| 459 | } |
| 460 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 461 | static void ipaccess_close_bsc(void *data) |
| 462 | { |
| 463 | struct bsc_connection *conn = data; |
| 464 | |
| 465 | LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n"); |
| 466 | remove_bsc_connection(conn); |
| 467 | } |
| 468 | |
| 469 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 470 | { |
| 471 | struct bsc_config *conf; |
| 472 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 473 | |
| 474 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 475 | if (strcmp(conf->token, token) == 0) { |
| 476 | bsc->authenticated = 1; |
| 477 | bsc->lac = conf->lac; |
| 478 | bsc_del_timer(&bsc->id_timeout); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 484 | static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 485 | { |
Holger Hans Peter Freyther | 7c11d1d | 2010-02-09 16:30:53 +0100 | [diff] [blame] | 486 | struct bsc_connection *found_bsc = NULL; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 487 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 488 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 489 | /* Parse and filter messages */ |
| 490 | parsed = bsc_nat_parse(msg); |
| 491 | if (!parsed) { |
| 492 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
| 493 | return -1; |
| 494 | } |
| 495 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 496 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 497 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 498 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 499 | /* modify the SCCP entries */ |
| 500 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 501 | switch (parsed->sccp_type) { |
| 502 | case SCCP_MSG_TYPE_CR: |
| 503 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 504 | goto exit2; |
| 505 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 506 | break; |
| 507 | case SCCP_MSG_TYPE_RLSD: |
| 508 | case SCCP_MSG_TYPE_CREF: |
| 509 | case SCCP_MSG_TYPE_DT1: |
| 510 | case SCCP_MSG_TYPE_CC: |
| 511 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 512 | break; |
| 513 | case SCCP_MSG_TYPE_RLC: |
| 514 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 515 | remove_sccp_src_ref(bsc, msg, parsed); |
| 516 | break; |
| 517 | case SCCP_MSG_TYPE_UDT: |
| 518 | /* simply forward everything */ |
| 519 | break; |
| 520 | default: |
| 521 | goto exit2; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (found_bsc != bsc) { |
| 527 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 528 | goto exit2; |
| 529 | } |
| 530 | |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 531 | if (!bsc->authenticated) { |
| 532 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 533 | goto exit2; |
| 534 | } |
| 535 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 536 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 537 | if (write_queue_enqueue(&msc_queue, msg) != 0) { |
| 538 | LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n"); |
| 539 | msgb_free(msg); |
| 540 | } |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 541 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 542 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 543 | |
| 544 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 545 | /* if we filter out the reset send an ack to the BSC */ |
| 546 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 547 | send_reset_ack(bsc); |
| 548 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 549 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 550 | /* do we know who is handling this? */ |
| 551 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 552 | struct tlv_parsed tvp; |
| 553 | ipaccess_idtag_parse(&tvp, |
| 554 | (unsigned char *) msg->l2h + 2, |
| 555 | msgb_l2len(msg) - 2); |
| 556 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 557 | ipaccess_auth_bsc(&tvp, bsc); |
| 558 | } |
| 559 | |
| 560 | goto exit2; |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 563 | exit2: |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 564 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 565 | msgb_free(msg); |
| 566 | return -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 569 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 570 | { |
| 571 | int error; |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 572 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 573 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 574 | |
| 575 | if (!msg) { |
| 576 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 577 | LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n"); |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 578 | remove_bsc_connection(bsc); |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 579 | } else { |
| 580 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 581 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 582 | return -1; |
| 583 | } |
| 584 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 585 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 586 | LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 587 | |
| 588 | /* Handle messages from the BSC */ |
| 589 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 590 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 591 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 596 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 597 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 598 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 599 | int ret; |
| 600 | struct sockaddr_in sa; |
| 601 | socklen_t sa_len = sizeof(sa); |
| 602 | |
| 603 | if (!(what & BSC_FD_READ)) |
| 604 | return 0; |
| 605 | |
| 606 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 607 | if (ret < 0) { |
| 608 | perror("accept"); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 613 | /* todo... use GNUtls to see if we want to trust this as a BTS */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 614 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 615 | /* |
| 616 | * |
| 617 | */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 618 | bsc = bsc_connection_alloc(); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 619 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 620 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 621 | close(ret); |
| 622 | return -1; |
| 623 | } |
| 624 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 625 | bsc->nat = nat; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 626 | write_queue_init(&bsc->write_queue, 100); |
| 627 | bsc->write_queue.bfd.data = bsc; |
| 628 | bsc->write_queue.bfd.fd = ret; |
| 629 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
| 630 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 631 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 632 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 633 | close(ret); |
| 634 | talloc_free(bsc); |
| 635 | return -2; |
| 636 | } |
| 637 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 638 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 639 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 640 | ipaccess_send_id_ack(bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 0fb47e0 | 2010-03-26 07:26:18 +0100 | [diff] [blame^] | 641 | ipaccess_send_id_req(bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * start the hangup timer |
| 645 | */ |
| 646 | bsc->id_timeout.data = bsc; |
| 647 | bsc->id_timeout.cb = ipaccess_close_bsc; |
| 648 | bsc_schedule_timer(&bsc->id_timeout, 2, 0); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 653 | { |
| 654 | struct sockaddr_in addr; |
| 655 | int ret, on = 1; |
| 656 | |
| 657 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 658 | bfd->cb = ipaccess_listen_bsc_cb; |
| 659 | bfd->when = BSC_FD_READ; |
| 660 | |
| 661 | memset(&addr, 0, sizeof(addr)); |
| 662 | addr.sin_family = AF_INET; |
| 663 | addr.sin_port = htons(port); |
| 664 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 665 | |
| 666 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 667 | |
| 668 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 669 | if (ret < 0) { |
| 670 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 671 | strerror(errno)); |
| 672 | return -EIO; |
| 673 | } |
| 674 | |
| 675 | ret = listen(bfd->fd, 1); |
| 676 | if (ret < 0) { |
| 677 | perror("listen"); |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | ret = bsc_register_fd(bfd); |
| 682 | if (ret < 0) { |
| 683 | perror("register_listen_fd"); |
| 684 | return ret; |
| 685 | } |
| 686 | return 0; |
| 687 | } |
| 688 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 689 | static void print_usage() |
| 690 | { |
| 691 | printf("Usage: bsc_nat\n"); |
| 692 | } |
| 693 | |
| 694 | static void print_help() |
| 695 | { |
| 696 | printf(" Some useful help...\n"); |
| 697 | printf(" -h --help this text\n"); |
| 698 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 699 | printf(" -s --disable-color\n"); |
| 700 | printf(" -c --config-file filename The config file to use.\n"); |
| 701 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 702 | printf(" -l --local=IP. The local address of this BSC.\n"); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void handle_options(int argc, char** argv) |
| 706 | { |
| 707 | while (1) { |
| 708 | int option_index = 0, c; |
| 709 | static struct option long_options[] = { |
| 710 | {"help", 0, 0, 'h'}, |
| 711 | {"debug", 1, 0, 'd'}, |
| 712 | {"config-file", 1, 0, 'c'}, |
| 713 | {"disable-color", 0, 0, 's'}, |
| 714 | {"timestamp", 0, 0, 'T'}, |
| 715 | {"msc", 1, 0, 'm'}, |
| 716 | {"local", 1, 0, 'l'}, |
| 717 | {0, 0, 0, 0} |
| 718 | }; |
| 719 | |
| 720 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 721 | long_options, &option_index); |
| 722 | if (c == -1) |
| 723 | break; |
| 724 | |
| 725 | switch (c) { |
| 726 | case 'h': |
| 727 | print_usage(); |
| 728 | print_help(); |
| 729 | exit(0); |
| 730 | case 's': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 731 | debug_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 732 | break; |
| 733 | case 'd': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 734 | debug_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 735 | break; |
| 736 | case 'c': |
| 737 | config_file = strdup(optarg); |
| 738 | break; |
| 739 | case 'T': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 740 | debug_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 741 | break; |
| 742 | case 'm': |
| 743 | msc_address = strdup(optarg); |
| 744 | break; |
| 745 | case 'l': |
| 746 | inet_aton(optarg, &local_addr); |
| 747 | break; |
| 748 | default: |
| 749 | /* ignore */ |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 755 | static void signal_handler(int signal) |
| 756 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 757 | switch (signal) { |
| 758 | case SIGABRT: |
| 759 | /* in case of abort, we want to obtain a talloc report |
| 760 | * and then return to the caller, who will abort the process */ |
| 761 | case SIGUSR1: |
| 762 | talloc_report_full(tall_bsc_ctx, stderr); |
| 763 | break; |
| 764 | default: |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 769 | int main(int argc, char** argv) |
| 770 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 771 | int rc; |
| 772 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 773 | debug_init(); |
| 774 | stderr_target = debug_target_create_stderr(); |
| 775 | debug_add_target(stderr_target); |
| 776 | debug_set_all_filter(stderr_target, 1); |
| 777 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 778 | /* parse options */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 779 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 780 | handle_options(argc, argv); |
| 781 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 782 | nat = bsc_nat_alloc(); |
| 783 | if (!nat) { |
| 784 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 785 | return -4; |
| 786 | } |
| 787 | |
| 788 | /* init vty and parse */ |
| 789 | bsc_nat_vty_init(nat); |
| 790 | telnet_init(NULL, 4244); |
| 791 | if (vty_read_config_file(config_file) < 0) { |
| 792 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 793 | return -3; |
| 794 | } |
| 795 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 796 | /* seed the PRNG */ |
| 797 | srand(time(NULL)); |
| 798 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 799 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 800 | write_queue_init(&msc_queue, 100); |
| 801 | msc_queue.read_cb = ipaccess_msc_read_cb; |
| 802 | msc_queue.write_cb = ipaccess_msc_write_cb; |
| 803 | rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 804 | if (rc < 0) { |
| 805 | fprintf(stderr, "Opening the MSC connection failed.\n"); |
| 806 | exit(1); |
| 807 | } |
| 808 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 809 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 810 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 811 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 812 | exit(1); |
| 813 | } |
| 814 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 815 | signal(SIGABRT, &signal_handler); |
| 816 | signal(SIGUSR1, &signal_handler); |
| 817 | signal(SIGPIPE, SIG_IGN); |
| 818 | |
| 819 | while (1) { |
| 820 | bsc_select_main(0); |
| 821 | } |
| 822 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 823 | return 0; |
| 824 | } |