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