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