Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010 by on-waves.com |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
| 27 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 29 | #include <signal.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5aa25ae | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #define _GNU_SOURCE |
| 36 | #include <getopt.h> |
| 37 | |
| 38 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 39 | #include <openbsc/msgb.h> |
| 40 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 42 | #include <openbsc/bssap.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 43 | #include <openbsc/ipaccess.h> |
| 44 | #include <openbsc/abis_nm.h> |
| 45 | #include <openbsc/talloc.h> |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 46 | #include <openbsc/linuxlist.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 47 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 48 | #include <sccp/sccp.h> |
| 49 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 50 | static const char *config_file = "openbsc.cfg"; |
| 51 | static char *msc_address = "127.0.0.1"; |
| 52 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 53 | static struct bsc_fd msc_connection; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 54 | static struct bsc_fd bsc_connection; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 55 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Per BSC data structure |
| 59 | */ |
| 60 | struct bsc_connection { |
| 61 | struct llist_head list_entry; |
| 62 | |
| 63 | /* do we know anything about this BSC? */ |
| 64 | int authenticated; |
| 65 | |
| 66 | /* the fd we use to communicate */ |
| 67 | struct bsc_fd bsc_fd; |
| 68 | }; |
| 69 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 70 | /* |
| 71 | * Per SCCP source local reference patch table. It needs to |
| 72 | * be updated on new SCCP connections, connection confirm and reject, |
| 73 | * and on the loss of the BSC connection. |
| 74 | */ |
| 75 | struct sccp_connections { |
| 76 | struct llist_head list_entry; |
| 77 | |
| 78 | struct bsc_connection *bsc; |
| 79 | |
| 80 | struct sccp_source_reference real_ref; |
| 81 | struct sccp_source_reference patched_ref; |
| 82 | }; |
| 83 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 84 | static LLIST_HEAD(bsc_connections); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 85 | static LLIST_HEAD(sccp_connections); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 86 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 87 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 88 | /* |
| 89 | * below are stubs we need to link |
| 90 | */ |
| 91 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
| 92 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state) |
| 93 | { |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 98 | {} |
| 99 | |
| 100 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 101 | { |
| 102 | return -1; |
| 103 | } |
| 104 | |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 105 | static int send_reset_ack(struct bsc_fd *bfd) |
| 106 | { |
| 107 | static const u_int8_t gsm_reset_ack[] = { |
| 108 | 0x00, 0x13, 0xfd, |
| 109 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 110 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 111 | 0x00, 0x01, 0x31, |
| 112 | }; |
| 113 | |
| 114 | return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack)); |
| 115 | } |
| 116 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 117 | /* |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 118 | * SCCP patching below |
| 119 | */ |
| 120 | |
| 121 | /* check if we are using this ref for patched already */ |
| 122 | static int sccp_ref_is_free(struct sccp_source_reference *ref) |
| 123 | { |
| 124 | struct sccp_connections *conn; |
| 125 | |
| 126 | llist_for_each_entry(conn, &sccp_connections, list_entry) { |
| 127 | if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0) |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* copied from sccp.c */ |
| 135 | static int assign_src_local_reference(struct sccp_source_reference *ref) |
| 136 | { |
| 137 | static u_int32_t last_ref = 0x50000; |
| 138 | int wrapped = 0; |
| 139 | |
| 140 | do { |
| 141 | struct sccp_source_reference reference; |
| 142 | reference.octet1 = (last_ref >> 0) & 0xff; |
| 143 | reference.octet2 = (last_ref >> 8) & 0xff; |
| 144 | reference.octet3 = (last_ref >> 16) & 0xff; |
| 145 | |
| 146 | ++last_ref; |
| 147 | /* do not use the reversed word and wrap around */ |
| 148 | if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) { |
| 149 | LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n"); |
| 150 | last_ref = 0; |
| 151 | ++wrapped; |
| 152 | } |
| 153 | |
| 154 | if (sccp_ref_is_free(&reference) == 0) { |
| 155 | *ref = reference; |
| 156 | return 0; |
| 157 | } |
| 158 | } while (wrapped != 2); |
| 159 | |
| 160 | LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n"); |
| 161 | return -1; |
| 162 | } |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame^] | 163 | |
| 164 | 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] | 165 | { |
| 166 | struct sccp_connections *conn; |
| 167 | |
| 168 | conn = talloc_zero(tall_bsc_ctx, struct sccp_connections); |
| 169 | if (!conn) { |
| 170 | LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | conn->real_ref = *parsed->src_local_ref; |
| 175 | if (assign_src_local_reference(&conn->patched_ref) != 0) { |
| 176 | LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n"); |
| 177 | talloc_free(conn); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame^] | 184 | 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] | 185 | { |
| 186 | struct sccp_connections *conn; |
| 187 | |
| 188 | llist_for_each_entry(conn, &sccp_connections, list_entry) { |
| 189 | if (memcmp(parsed->src_local_ref, |
| 190 | &conn->real_ref, sizeof(conn->real_ref)) == 0) { |
| 191 | if (bsc != conn->bsc) { |
| 192 | LOGP(DNAT, LOGL_ERROR, "Someone else...\n"); |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | llist_del(&conn->list_entry); |
| 198 | talloc_free(conn); |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n"); |
| 204 | } |
| 205 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame^] | 206 | 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] | 207 | { |
| 208 | struct sccp_connections *conn; |
| 209 | llist_for_each_entry(conn, &sccp_connections, list_entry) { |
| 210 | if (memcmp(parsed->dest_local_ref, |
| 211 | &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) { |
| 212 | memcpy(parsed->dest_local_ref, |
| 213 | &conn->patched_ref, sizeof(*parsed->dest_local_ref)); |
| 214 | return conn->bsc; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Holger Hans Peter Freyther | 45f7dcd | 2010-01-31 13:52:32 +0100 | [diff] [blame^] | 221 | 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] | 222 | { |
| 223 | struct sccp_connections *conn; |
| 224 | llist_for_each_entry(conn, &sccp_connections, list_entry) { |
| 225 | if (memcmp(parsed->src_local_ref, |
| 226 | &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) { |
| 227 | memcpy(parsed->src_local_ref, |
| 228 | &conn->patched_ref, sizeof(*parsed->src_local_ref)); |
| 229 | return conn->bsc; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | /* |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 237 | * Below is the handling of messages coming |
| 238 | * from the MSC and need to be forwarded to |
| 239 | * a real BSC. |
| 240 | */ |
| 241 | static void initialize_msc_if_needed() |
| 242 | { |
| 243 | static int init = 0; |
| 244 | init = 1; |
| 245 | |
| 246 | /* do we need to send a GSM 08.08 message here? */ |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 249 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 250 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 251 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 252 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 253 | int rc; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 254 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 255 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 256 | parsed = bsc_nat_parse(msg); |
| 257 | if (!parsed) { |
| 258 | 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] | 259 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 260 | } |
| 261 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 262 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 263 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 264 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 265 | /* Route and modify the SCCP packet */ |
| 266 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 267 | switch (parsed->sccp_type) { |
| 268 | case SCCP_MSG_TYPE_UDT: |
| 269 | /* forward UDT messages to every BSC */ |
| 270 | goto send_to_all; |
| 271 | break; |
| 272 | case SCCP_MSG_TYPE_RLSD: |
| 273 | case SCCP_MSG_TYPE_CREF: |
| 274 | case SCCP_MSG_TYPE_DT1: |
| 275 | case SCCP_MSG_TYPE_CC: |
| 276 | bsc = patch_sccp_src_ref_to_bsc(msg, parsed); |
| 277 | break; |
| 278 | case SCCP_MSG_TYPE_CR: |
| 279 | case SCCP_MSG_TYPE_RLC: |
| 280 | /* MSC never opens a SCCP connection, fall through */ |
| 281 | default: |
| 282 | goto exit; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | talloc_free(parsed); |
| 287 | if (!bsc) |
| 288 | return -1; |
| 289 | return write(bsc->bsc_fd.fd, msg->data, msg->len); |
| 290 | |
| 291 | send_to_all: |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 292 | /* currently send this to every BSC connected */ |
| 293 | llist_for_each_entry(bsc, &bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 294 | rc = write(bsc->bsc_fd.fd, msg->data, msg->len); |
| 295 | |
| 296 | /* try the next one */ |
| 297 | if (rc < msg->len) |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 298 | 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] | 299 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 300 | |
| 301 | exit: |
| 302 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 303 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what) |
| 307 | { |
| 308 | int error; |
| 309 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 310 | struct ipaccess_head *hh; |
| 311 | |
| 312 | if (!msg) { |
| 313 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 314 | 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] | 315 | exit(-2); |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 318 | 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] | 319 | return -1; |
| 320 | } |
| 321 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 322 | 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] | 323 | |
| 324 | /* handle base message handling */ |
| 325 | hh = (struct ipaccess_head *) msg->data; |
| 326 | ipaccess_rcvmsg_base(msg, bfd); |
| 327 | |
| 328 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 329 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 330 | initialize_msc_if_needed(); |
| 331 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 332 | forward_sccp_to_bts(msg); |
| 333 | |
| 334 | return 0; |
| 335 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 336 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 337 | /* |
| 338 | * Below is the handling of messages coming |
| 339 | * from the BSC and need to be forwarded to |
| 340 | * a real BSC. |
| 341 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Remove the connection from the connections list, |
| 345 | * remove it from the patching of SCCP header lists |
| 346 | * as well. Maybe in the future even close connection.. |
| 347 | */ |
| 348 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 349 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 350 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 7625506 | 2010-01-13 09:51:23 +0100 | [diff] [blame] | 351 | bsc_unregister_fd(&connection->bsc_fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 352 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 353 | |
| 354 | /* remove all SCCP connections */ |
| 355 | llist_for_each_entry_safe(sccp_patch, tmp, &sccp_connections, list_entry) { |
| 356 | if (sccp_patch->bsc != connection) |
| 357 | continue; |
| 358 | |
| 359 | llist_del(&sccp_patch->list_entry); |
| 360 | talloc_free(sccp_patch); |
| 361 | } |
| 362 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 363 | talloc_free(connection); |
| 364 | } |
| 365 | |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 366 | 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] | 367 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 368 | struct bsc_connection *bsc; |
| 369 | struct bsc_connection *found_bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 370 | struct bsc_nat_parsed *parsed; |
| 371 | int rc = -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 372 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 373 | bsc = bfd->data; |
| 374 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 375 | /* Parse and filter messages */ |
| 376 | parsed = bsc_nat_parse(msg); |
| 377 | if (!parsed) { |
| 378 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
| 379 | return -1; |
| 380 | } |
| 381 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 382 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 383 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 384 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 385 | /* modify the SCCP entries */ |
| 386 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 387 | switch (parsed->sccp_type) { |
| 388 | case SCCP_MSG_TYPE_CR: |
| 389 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 390 | goto exit2; |
| 391 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 392 | break; |
| 393 | case SCCP_MSG_TYPE_RLSD: |
| 394 | case SCCP_MSG_TYPE_CREF: |
| 395 | case SCCP_MSG_TYPE_DT1: |
| 396 | case SCCP_MSG_TYPE_CC: |
| 397 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 398 | break; |
| 399 | case SCCP_MSG_TYPE_RLC: |
| 400 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed); |
| 401 | remove_sccp_src_ref(bsc, msg, parsed); |
| 402 | break; |
| 403 | case SCCP_MSG_TYPE_UDT: |
| 404 | /* simply forward everything */ |
| 405 | break; |
| 406 | default: |
| 407 | goto exit2; |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (found_bsc != bsc) { |
| 413 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 414 | goto exit2; |
| 415 | } |
| 416 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 417 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 418 | rc = write(msc_connection.fd, msg->data, msg->len); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 419 | talloc_free(parsed); |
| 420 | return rc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 421 | |
| 422 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 423 | /* if we filter out the reset send an ack to the BSC */ |
| 424 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
| 425 | send_reset_ack(bfd); |
| 426 | send_reset_ack(bfd); |
| 427 | } |
| 428 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 429 | exit2: |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 430 | talloc_free(parsed); |
| 431 | return rc; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 435 | { |
| 436 | int error; |
| 437 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 438 | |
| 439 | if (!msg) { |
| 440 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 441 | 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] | 442 | remove_bsc_connection((struct bsc_connection *) bfd->data); |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 443 | } else { |
| 444 | 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] | 445 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 449 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 450 | 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] | 451 | |
| 452 | /* Handle messages from the BSC */ |
| 453 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 454 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 455 | forward_sccp_to_msc(bfd, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 460 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 461 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 462 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 463 | int ret; |
| 464 | struct sockaddr_in sa; |
| 465 | socklen_t sa_len = sizeof(sa); |
| 466 | |
| 467 | if (!(what & BSC_FD_READ)) |
| 468 | return 0; |
| 469 | |
| 470 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 471 | if (ret < 0) { |
| 472 | perror("accept"); |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 477 | /* 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] | 478 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 479 | /* |
| 480 | * |
| 481 | */ |
| 482 | bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection); |
| 483 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 484 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 485 | close(ret); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | bsc->bsc_fd.data = bsc; |
| 490 | bsc->bsc_fd.fd = ret; |
| 491 | bsc->bsc_fd.cb = ipaccess_bsc_cb; |
Holger Hans Peter Freyther | c7641c9 | 2010-01-13 09:52:29 +0100 | [diff] [blame] | 492 | bsc->bsc_fd.when = BSC_FD_READ; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 493 | if (bsc_register_fd(&bsc->bsc_fd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 494 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 495 | close(ret); |
| 496 | talloc_free(bsc); |
| 497 | return -2; |
| 498 | } |
| 499 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 500 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 501 | llist_add(&bsc->list_entry, &bsc_connections); |
| 502 | ipaccess_send_id_ack(ret); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 507 | { |
| 508 | struct sockaddr_in addr; |
| 509 | int ret, on = 1; |
| 510 | |
| 511 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 512 | bfd->cb = ipaccess_listen_bsc_cb; |
| 513 | bfd->when = BSC_FD_READ; |
| 514 | |
| 515 | memset(&addr, 0, sizeof(addr)); |
| 516 | addr.sin_family = AF_INET; |
| 517 | addr.sin_port = htons(port); |
| 518 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 519 | |
| 520 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 521 | |
| 522 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 523 | if (ret < 0) { |
| 524 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 525 | strerror(errno)); |
| 526 | return -EIO; |
| 527 | } |
| 528 | |
| 529 | ret = listen(bfd->fd, 1); |
| 530 | if (ret < 0) { |
| 531 | perror("listen"); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | ret = bsc_register_fd(bfd); |
| 536 | if (ret < 0) { |
| 537 | perror("register_listen_fd"); |
| 538 | return ret; |
| 539 | } |
| 540 | return 0; |
| 541 | } |
| 542 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 543 | static void print_usage() |
| 544 | { |
| 545 | printf("Usage: bsc_nat\n"); |
| 546 | } |
| 547 | |
| 548 | static void print_help() |
| 549 | { |
| 550 | printf(" Some useful help...\n"); |
| 551 | printf(" -h --help this text\n"); |
| 552 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 553 | printf(" -s --disable-color\n"); |
| 554 | printf(" -c --config-file filename The config file to use.\n"); |
| 555 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 556 | 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] | 557 | } |
| 558 | |
| 559 | static void handle_options(int argc, char** argv) |
| 560 | { |
| 561 | while (1) { |
| 562 | int option_index = 0, c; |
| 563 | static struct option long_options[] = { |
| 564 | {"help", 0, 0, 'h'}, |
| 565 | {"debug", 1, 0, 'd'}, |
| 566 | {"config-file", 1, 0, 'c'}, |
| 567 | {"disable-color", 0, 0, 's'}, |
| 568 | {"timestamp", 0, 0, 'T'}, |
| 569 | {"msc", 1, 0, 'm'}, |
| 570 | {"local", 1, 0, 'l'}, |
| 571 | {0, 0, 0, 0} |
| 572 | }; |
| 573 | |
| 574 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 575 | long_options, &option_index); |
| 576 | if (c == -1) |
| 577 | break; |
| 578 | |
| 579 | switch (c) { |
| 580 | case 'h': |
| 581 | print_usage(); |
| 582 | print_help(); |
| 583 | exit(0); |
| 584 | case 's': |
| 585 | debug_use_color(0); |
| 586 | break; |
| 587 | case 'd': |
| 588 | debug_parse_category_mask(optarg); |
| 589 | break; |
| 590 | case 'c': |
| 591 | config_file = strdup(optarg); |
| 592 | break; |
| 593 | case 'T': |
| 594 | debug_timestamp(1); |
| 595 | break; |
| 596 | case 'm': |
| 597 | msc_address = strdup(optarg); |
| 598 | break; |
| 599 | case 'l': |
| 600 | inet_aton(optarg, &local_addr); |
| 601 | break; |
| 602 | default: |
| 603 | /* ignore */ |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 609 | static void signal_handler(int signal) |
| 610 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 611 | switch (signal) { |
| 612 | case SIGABRT: |
| 613 | /* in case of abort, we want to obtain a talloc report |
| 614 | * and then return to the caller, who will abort the process */ |
| 615 | case SIGUSR1: |
| 616 | talloc_report_full(tall_bsc_ctx, stderr); |
| 617 | break; |
| 618 | default: |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 623 | int main(int argc, char** argv) |
| 624 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 625 | int rc; |
| 626 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 627 | /* parse options */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 628 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 629 | handle_options(argc, argv); |
| 630 | |
| 631 | /* seed the PRNG */ |
| 632 | srand(time(NULL)); |
| 633 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 634 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 635 | msc_connection.cb = ipaccess_msc_cb; |
| 636 | rc = connect_to_msc(&msc_connection, msc_address, 5000); |
| 637 | if (rc < 0) { |
| 638 | fprintf(stderr, "Opening the MSC connection failed.\n"); |
| 639 | exit(1); |
| 640 | } |
| 641 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 642 | /* wait for the BSC */ |
| 643 | if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) { |
| 644 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 645 | exit(1); |
| 646 | } |
| 647 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 648 | signal(SIGINT, &signal_handler); |
| 649 | signal(SIGABRT, &signal_handler); |
| 650 | signal(SIGUSR1, &signal_handler); |
| 651 | signal(SIGPIPE, SIG_IGN); |
| 652 | |
| 653 | while (1) { |
| 654 | bsc_select_main(0); |
| 655 | } |
| 656 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 657 | return 0; |
| 658 | } |