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