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