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> |
Holger Hans Peter Freyther | 98e49d4 | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
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/bsc_msc.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 40 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 41 | #include <openbsc/bssap.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/ipaccess.h> |
| 43 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 44 | #include <openbsc/telnet_interface.h> |
| 45 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 46 | #include <osmocore/talloc.h> |
| 47 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 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 | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 52 | struct debug_target *stderr_target; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 53 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 89d9fd9 | 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 | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 56 | static struct write_queue msc_queue; |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 57 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 59 | |
Holger Hans Peter Freyther | 5e54788 | 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 | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 110 | |
Holger Hans Peter Freyther | e907cb2 | 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 | 722ead8 | 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 | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 140 | /* |
Holger Hans Peter Freyther | e83917d | 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 | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 149 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 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 | 0792bb5 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 188 | { |
| 189 | struct sccp_connections *conn; |
| 190 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 191 | conn = talloc_zero(nat, struct sccp_connections); |
Holger Hans Peter Freyther | e83917d | 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 | 0792bb5 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 208 | { |
| 209 | struct sccp_connections *conn; |
| 210 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 211 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 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 | 0792bb5 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 230 | { |
| 231 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 232 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 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 | 0792bb5 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 245 | { |
| 246 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 247 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 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 | e907cb2 | 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 | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame^] | 272 | static int bsc_write(struct bsc_connection *bsc, u_int8_t *data, unsigned int length) |
| 273 | { |
| 274 | return write(bsc->write_queue.bfd.fd, data, length); |
| 275 | } |
| 276 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 277 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 278 | { |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 279 | struct bsc_connection *bsc = NULL; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 280 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | ac0dc7f | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 281 | int rc; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 282 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 283 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 284 | parsed = bsc_nat_parse(msg); |
| 285 | if (!parsed) { |
| 286 | 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] | 287 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 290 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 291 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 292 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 293 | /* Route and modify the SCCP packet */ |
| 294 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 295 | switch (parsed->sccp_type) { |
| 296 | case SCCP_MSG_TYPE_UDT: |
| 297 | /* forward UDT messages to every BSC */ |
| 298 | goto send_to_all; |
| 299 | break; |
| 300 | case SCCP_MSG_TYPE_RLSD: |
| 301 | case SCCP_MSG_TYPE_CREF: |
| 302 | case SCCP_MSG_TYPE_DT1: |
| 303 | case SCCP_MSG_TYPE_CC: |
| 304 | bsc = patch_sccp_src_ref_to_bsc(msg, parsed); |
| 305 | break; |
| 306 | case SCCP_MSG_TYPE_CR: |
| 307 | case SCCP_MSG_TYPE_RLC: |
| 308 | /* MSC never opens a SCCP connection, fall through */ |
| 309 | default: |
| 310 | goto exit; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | talloc_free(parsed); |
| 315 | if (!bsc) |
| 316 | return -1; |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 317 | if (!bsc->authenticated) { |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 318 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 319 | return -1; |
| 320 | } |
| 321 | |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame^] | 322 | return bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 323 | |
| 324 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 325 | /* |
| 326 | * Filter Paging from the network. We do not want to send a PAGING |
| 327 | * Command to every BSC in our network. We will analys the PAGING |
| 328 | * message and then send it to the authenticated messages... |
| 329 | */ |
| 330 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
| 331 | int data_length; |
| 332 | const u_int8_t *data; |
| 333 | struct tlv_parsed tp; |
| 334 | int i = 0; |
| 335 | |
| 336 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 337 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 338 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 339 | goto exit; |
| 340 | } |
| 341 | |
| 342 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 343 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 344 | if (data[0] != CELL_IDENT_LAC) { |
| 345 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]); |
| 346 | goto exit; |
| 347 | } |
| 348 | |
| 349 | /* go through each LAC and forward the message */ |
| 350 | for (i = 1; i < data_length - 1; i += 2) { |
| 351 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
| 352 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 353 | if (!bsc->authenticated || _lac != bsc->lac) |
| 354 | continue; |
| 355 | |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame^] | 356 | rc = bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 357 | if (rc < msg->len) |
| 358 | LOGP(DNAT, LOGL_ERROR, |
| 359 | "Failed to write message to BTS: %d\n", rc); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | goto exit; |
| 364 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 365 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 366 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 367 | if (!bsc->authenticated) |
| 368 | continue; |
| 369 | |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame^] | 370 | rc = bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | ac0dc7f | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 371 | |
| 372 | /* try the next one */ |
| 373 | if (rc < msg->len) |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 374 | 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] | 375 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 376 | |
| 377 | exit: |
| 378 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 379 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 382 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 383 | { |
| 384 | int error; |
| 385 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 386 | struct ipaccess_head *hh; |
| 387 | |
| 388 | if (!msg) { |
| 389 | if (error == 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 390 | 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] | 391 | exit(-2); |
| 392 | } |
| 393 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 394 | 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] | 395 | return -1; |
| 396 | } |
| 397 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 398 | 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] | 399 | |
| 400 | /* handle base message handling */ |
| 401 | hh = (struct ipaccess_head *) msg->data; |
| 402 | ipaccess_rcvmsg_base(msg, bfd); |
| 403 | |
| 404 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 405 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 406 | initialize_msc_if_needed(); |
| 407 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 408 | forward_sccp_to_bts(msg); |
| 409 | |
Holger Hans Peter Freyther | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 410 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 411 | return 0; |
| 412 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 413 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 414 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 415 | { |
| 416 | int rc; |
| 417 | rc = write(bfd->fd, msg->data, msg->len); |
| 418 | |
| 419 | if (rc != msg->len) { |
| 420 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 421 | return -1; |
| 422 | } |
| 423 | |
| 424 | return rc; |
| 425 | } |
| 426 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 427 | /* |
| 428 | * Below is the handling of messages coming |
| 429 | * from the BSC and need to be forwarded to |
| 430 | * a real BSC. |
| 431 | */ |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Remove the connection from the connections list, |
| 435 | * remove it from the patching of SCCP header lists |
| 436 | * as well. Maybe in the future even close connection.. |
| 437 | */ |
| 438 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 439 | { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 440 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 441 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 442 | close(connection->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 443 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 444 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 445 | /* stop the timeout timer */ |
| 446 | bsc_del_timer(&connection->id_timeout); |
| 447 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 448 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 449 | 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] | 450 | if (sccp_patch->bsc != connection) |
| 451 | continue; |
| 452 | |
| 453 | llist_del(&sccp_patch->list_entry); |
| 454 | talloc_free(sccp_patch); |
| 455 | } |
| 456 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 457 | talloc_free(connection); |
| 458 | } |
| 459 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 460 | static void ipaccess_close_bsc(void *data) |
| 461 | { |
| 462 | struct bsc_connection *conn = data; |
| 463 | |
| 464 | LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n"); |
| 465 | remove_bsc_connection(conn); |
| 466 | } |
| 467 | |
| 468 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 469 | { |
| 470 | struct bsc_config *conf; |
| 471 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 472 | |
| 473 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 474 | if (strcmp(conf->token, token) == 0) { |
| 475 | bsc->authenticated = 1; |
| 476 | bsc->lac = conf->lac; |
| 477 | bsc_del_timer(&bsc->id_timeout); |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 483 | 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] | 484 | { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 485 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 87fcac2 | 2010-02-09 16:30:53 +0100 | [diff] [blame] | 486 | struct bsc_connection *found_bsc = NULL; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 487 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 488 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 489 | bsc = bfd->data; |
| 490 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 491 | /* Parse and filter messages */ |
| 492 | parsed = bsc_nat_parse(msg); |
| 493 | if (!parsed) { |
| 494 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
| 495 | return -1; |
| 496 | } |
| 497 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 498 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 499 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 500 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 501 | /* modify the SCCP entries */ |
| 502 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 503 | switch (parsed->sccp_type) { |
| 504 | case SCCP_MSG_TYPE_CR: |
| 505 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 506 | goto exit2; |
| 507 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 508 | break; |
| 509 | case SCCP_MSG_TYPE_RLSD: |
| 510 | case SCCP_MSG_TYPE_CREF: |
| 511 | case SCCP_MSG_TYPE_DT1: |
| 512 | case SCCP_MSG_TYPE_CC: |
| 513 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 514 | break; |
| 515 | case SCCP_MSG_TYPE_RLC: |
| 516 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 517 | remove_sccp_src_ref(bsc, msg, parsed); |
| 518 | break; |
| 519 | case SCCP_MSG_TYPE_UDT: |
| 520 | /* simply forward everything */ |
| 521 | break; |
| 522 | default: |
| 523 | goto exit2; |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (found_bsc != bsc) { |
| 529 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 530 | goto exit2; |
| 531 | } |
| 532 | |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 533 | if (!bsc->authenticated) { |
| 534 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 535 | goto exit2; |
| 536 | } |
| 537 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 538 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 539 | if (write_queue_enqueue(&msc_queue, msg) != 0) { |
| 540 | LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n"); |
| 541 | msgb_free(msg); |
| 542 | } |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 543 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 544 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 545 | |
| 546 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 547 | /* if we filter out the reset send an ack to the BSC */ |
| 548 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
| 549 | send_reset_ack(bfd); |
| 550 | send_reset_ack(bfd); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 551 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 552 | /* do we know who is handling this? */ |
| 553 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 554 | struct tlv_parsed tvp; |
| 555 | ipaccess_idtag_parse(&tvp, |
| 556 | (unsigned char *) msg->l2h + 2, |
| 557 | msgb_l2len(msg) - 2); |
| 558 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 559 | ipaccess_auth_bsc(&tvp, bsc); |
| 560 | } |
| 561 | |
| 562 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 565 | exit2: |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 566 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 567 | msgb_free(msg); |
| 568 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 571 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 572 | { |
| 573 | int error; |
| 574 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 575 | |
| 576 | if (!msg) { |
| 577 | if (error == 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 578 | 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] | 579 | remove_bsc_connection((struct bsc_connection *) bfd->data); |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 580 | } else { |
| 581 | 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] | 582 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 586 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 587 | 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] | 588 | |
| 589 | /* Handle messages from the BSC */ |
| 590 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 591 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 592 | forward_sccp_to_msc(bfd, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 597 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 598 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 599 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 600 | int ret; |
| 601 | struct sockaddr_in sa; |
| 602 | socklen_t sa_len = sizeof(sa); |
| 603 | |
| 604 | if (!(what & BSC_FD_READ)) |
| 605 | return 0; |
| 606 | |
| 607 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 608 | if (ret < 0) { |
| 609 | perror("accept"); |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 614 | /* 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] | 615 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 616 | /* |
| 617 | * |
| 618 | */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 619 | bsc = bsc_connection_alloc(); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 620 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 621 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 622 | close(ret); |
| 623 | return -1; |
| 624 | } |
| 625 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 626 | bsc->nat = nat; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 627 | write_queue_init(&bsc->write_queue, 100); |
| 628 | bsc->write_queue.bfd.data = bsc; |
| 629 | bsc->write_queue.bfd.fd = ret; |
| 630 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
| 631 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 632 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 633 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 634 | close(ret); |
| 635 | talloc_free(bsc); |
| 636 | return -2; |
| 637 | } |
| 638 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 639 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 640 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 641 | ipaccess_send_id_ack(bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 642 | ipaccess_send_id_req(ret); |
| 643 | |
| 644 | /* |
| 645 | * start the hangup timer |
| 646 | */ |
| 647 | bsc->id_timeout.data = bsc; |
| 648 | bsc->id_timeout.cb = ipaccess_close_bsc; |
| 649 | bsc_schedule_timer(&bsc->id_timeout, 2, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 654 | { |
| 655 | struct sockaddr_in addr; |
| 656 | int ret, on = 1; |
| 657 | |
| 658 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 659 | bfd->cb = ipaccess_listen_bsc_cb; |
| 660 | bfd->when = BSC_FD_READ; |
| 661 | |
| 662 | memset(&addr, 0, sizeof(addr)); |
| 663 | addr.sin_family = AF_INET; |
| 664 | addr.sin_port = htons(port); |
| 665 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 666 | |
| 667 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 668 | |
| 669 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 670 | if (ret < 0) { |
| 671 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 672 | strerror(errno)); |
| 673 | return -EIO; |
| 674 | } |
| 675 | |
| 676 | ret = listen(bfd->fd, 1); |
| 677 | if (ret < 0) { |
| 678 | perror("listen"); |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | ret = bsc_register_fd(bfd); |
| 683 | if (ret < 0) { |
| 684 | perror("register_listen_fd"); |
| 685 | return ret; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 690 | static void print_usage() |
| 691 | { |
| 692 | printf("Usage: bsc_nat\n"); |
| 693 | } |
| 694 | |
| 695 | static void print_help() |
| 696 | { |
| 697 | printf(" Some useful help...\n"); |
| 698 | printf(" -h --help this text\n"); |
| 699 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 700 | printf(" -s --disable-color\n"); |
| 701 | printf(" -c --config-file filename The config file to use.\n"); |
| 702 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 703 | 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] | 704 | } |
| 705 | |
| 706 | static void handle_options(int argc, char** argv) |
| 707 | { |
| 708 | while (1) { |
| 709 | int option_index = 0, c; |
| 710 | static struct option long_options[] = { |
| 711 | {"help", 0, 0, 'h'}, |
| 712 | {"debug", 1, 0, 'd'}, |
| 713 | {"config-file", 1, 0, 'c'}, |
| 714 | {"disable-color", 0, 0, 's'}, |
| 715 | {"timestamp", 0, 0, 'T'}, |
| 716 | {"msc", 1, 0, 'm'}, |
| 717 | {"local", 1, 0, 'l'}, |
| 718 | {0, 0, 0, 0} |
| 719 | }; |
| 720 | |
| 721 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 722 | long_options, &option_index); |
| 723 | if (c == -1) |
| 724 | break; |
| 725 | |
| 726 | switch (c) { |
| 727 | case 'h': |
| 728 | print_usage(); |
| 729 | print_help(); |
| 730 | exit(0); |
| 731 | case 's': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 732 | debug_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 733 | break; |
| 734 | case 'd': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 735 | debug_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 736 | break; |
| 737 | case 'c': |
| 738 | config_file = strdup(optarg); |
| 739 | break; |
| 740 | case 'T': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 741 | debug_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 742 | break; |
| 743 | case 'm': |
| 744 | msc_address = strdup(optarg); |
| 745 | break; |
| 746 | case 'l': |
| 747 | inet_aton(optarg, &local_addr); |
| 748 | break; |
| 749 | default: |
| 750 | /* ignore */ |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 756 | static void signal_handler(int signal) |
| 757 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 758 | switch (signal) { |
| 759 | case SIGABRT: |
| 760 | /* in case of abort, we want to obtain a talloc report |
| 761 | * and then return to the caller, who will abort the process */ |
| 762 | case SIGUSR1: |
| 763 | talloc_report_full(tall_bsc_ctx, stderr); |
| 764 | break; |
| 765 | default: |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 770 | int main(int argc, char** argv) |
| 771 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 772 | int rc; |
| 773 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 774 | debug_init(); |
| 775 | stderr_target = debug_target_create_stderr(); |
| 776 | debug_add_target(stderr_target); |
| 777 | debug_set_all_filter(stderr_target, 1); |
| 778 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 779 | /* parse options */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 780 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 781 | handle_options(argc, argv); |
| 782 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 783 | nat = bsc_nat_alloc(); |
| 784 | if (!nat) { |
| 785 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 786 | return -4; |
| 787 | } |
| 788 | |
| 789 | /* init vty and parse */ |
| 790 | bsc_nat_vty_init(nat); |
| 791 | telnet_init(NULL, 4244); |
| 792 | if (vty_read_config_file(config_file) < 0) { |
| 793 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 794 | return -3; |
| 795 | } |
| 796 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 797 | /* seed the PRNG */ |
| 798 | srand(time(NULL)); |
| 799 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 800 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 801 | write_queue_init(&msc_queue, 100); |
| 802 | msc_queue.read_cb = ipaccess_msc_read_cb; |
| 803 | msc_queue.write_cb = ipaccess_msc_write_cb; |
| 804 | rc = connect_to_msc(&msc_queue.bfd, msc_address, 5000); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 805 | if (rc < 0) { |
| 806 | fprintf(stderr, "Opening the MSC connection failed.\n"); |
| 807 | exit(1); |
| 808 | } |
| 809 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 810 | /* wait for the BSC */ |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 811 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 812 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 813 | exit(1); |
| 814 | } |
| 815 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 816 | signal(SIGABRT, &signal_handler); |
| 817 | signal(SIGUSR1, &signal_handler); |
| 818 | signal(SIGPIPE, SIG_IGN); |
| 819 | |
| 820 | while (1) { |
| 821 | bsc_select_main(0); |
| 822 | } |
| 823 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 824 | return 0; |
| 825 | } |