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 | |
| 70 | static LLIST_HEAD(bsc_connections); |
| 71 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 72 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 73 | /* |
| 74 | * below are stubs we need to link |
| 75 | */ |
| 76 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
| 77 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state) |
| 78 | { |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 83 | {} |
| 84 | |
| 85 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame^] | 90 | static int send_reset_ack(struct bsc_fd *bfd) |
| 91 | { |
| 92 | static const u_int8_t gsm_reset_ack[] = { |
| 93 | 0x00, 0x13, 0xfd, |
| 94 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 95 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 96 | 0x00, 0x01, 0x31, |
| 97 | }; |
| 98 | |
| 99 | return write(bfd->fd, gsm_reset_ack, sizeof(gsm_reset_ack)); |
| 100 | } |
| 101 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Below is the handling of messages coming |
| 104 | * from the MSC and need to be forwarded to |
| 105 | * a real BSC. |
| 106 | */ |
| 107 | static void initialize_msc_if_needed() |
| 108 | { |
| 109 | static int init = 0; |
| 110 | init = 1; |
| 111 | |
| 112 | /* do we need to send a GSM 08.08 message here? */ |
| 113 | } |
| 114 | |
| 115 | static void forward_sccp_to_bts(struct msgb *msg) |
| 116 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 117 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 118 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 119 | int rc; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 120 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 121 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 122 | parsed = bsc_nat_parse(msg); |
| 123 | if (!parsed) { |
| 124 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 125 | return; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 128 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 129 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 130 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 131 | /* currently send this to every BSC connected */ |
| 132 | llist_for_each_entry(bsc, &bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 6004664 | 2010-01-25 10:01:30 +0100 | [diff] [blame] | 133 | rc = write(bsc->bsc_fd.fd, msg->data, msg->len); |
| 134 | |
| 135 | /* try the next one */ |
| 136 | if (rc < msg->len) |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 137 | 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] | 138 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 139 | |
| 140 | exit: |
| 141 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int ipaccess_msc_cb(struct bsc_fd *bfd, unsigned int what) |
| 145 | { |
| 146 | int error; |
| 147 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 148 | struct ipaccess_head *hh; |
| 149 | |
| 150 | if (!msg) { |
| 151 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 152 | 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] | 153 | exit(-2); |
| 154 | } |
| 155 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 156 | 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] | 157 | return -1; |
| 158 | } |
| 159 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 160 | 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] | 161 | |
| 162 | /* handle base message handling */ |
| 163 | hh = (struct ipaccess_head *) msg->data; |
| 164 | ipaccess_rcvmsg_base(msg, bfd); |
| 165 | |
| 166 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 167 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 168 | initialize_msc_if_needed(); |
| 169 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 170 | forward_sccp_to_bts(msg); |
| 171 | |
| 172 | return 0; |
| 173 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 174 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 175 | /* |
| 176 | * Below is the handling of messages coming |
| 177 | * from the BSC and need to be forwarded to |
| 178 | * a real BSC. |
| 179 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Remove the connection from the connections list, |
| 183 | * remove it from the patching of SCCP header lists |
| 184 | * as well. Maybe in the future even close connection.. |
| 185 | */ |
| 186 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 187 | { |
Holger Hans Peter Freyther | 7625506 | 2010-01-13 09:51:23 +0100 | [diff] [blame] | 188 | bsc_unregister_fd(&connection->bsc_fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 189 | llist_del(&connection->list_entry); |
| 190 | talloc_free(connection); |
| 191 | } |
| 192 | |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame^] | 193 | 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] | 194 | { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 195 | struct bsc_nat_parsed *parsed; |
| 196 | int rc = -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 197 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 198 | /* Parse and filter messages */ |
| 199 | parsed = bsc_nat_parse(msg); |
| 200 | if (!parsed) { |
| 201 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 205 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 206 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 207 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 208 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 209 | rc = write(msc_connection.fd, msg->data, msg->len); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame^] | 210 | talloc_free(parsed); |
| 211 | return rc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 212 | |
| 213 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame^] | 214 | /* if we filter out the reset send an ack to the BSC */ |
| 215 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
| 216 | send_reset_ack(bfd); |
| 217 | send_reset_ack(bfd); |
| 218 | } |
| 219 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 220 | talloc_free(parsed); |
| 221 | return rc; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 225 | { |
| 226 | int error; |
| 227 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 228 | |
| 229 | if (!msg) { |
| 230 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 231 | 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] | 232 | remove_bsc_connection((struct bsc_connection *) bfd->data); |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 233 | } else { |
| 234 | 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] | 235 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 239 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 240 | 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] | 241 | |
| 242 | /* Handle messages from the BSC */ |
| 243 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 244 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame^] | 245 | forward_sccp_to_msc(bfd, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 250 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 251 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 252 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 253 | int ret; |
| 254 | struct sockaddr_in sa; |
| 255 | socklen_t sa_len = sizeof(sa); |
| 256 | |
| 257 | if (!(what & BSC_FD_READ)) |
| 258 | return 0; |
| 259 | |
| 260 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 261 | if (ret < 0) { |
| 262 | perror("accept"); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 267 | /* 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] | 268 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 269 | /* |
| 270 | * |
| 271 | */ |
| 272 | bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection); |
| 273 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 274 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 275 | close(ret); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | bsc->bsc_fd.data = bsc; |
| 280 | bsc->bsc_fd.fd = ret; |
| 281 | bsc->bsc_fd.cb = ipaccess_bsc_cb; |
Holger Hans Peter Freyther | c7641c9 | 2010-01-13 09:52:29 +0100 | [diff] [blame] | 282 | bsc->bsc_fd.when = BSC_FD_READ; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 283 | if (bsc_register_fd(&bsc->bsc_fd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 284 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 285 | close(ret); |
| 286 | talloc_free(bsc); |
| 287 | return -2; |
| 288 | } |
| 289 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 290 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 291 | llist_add(&bsc->list_entry, &bsc_connections); |
| 292 | ipaccess_send_id_ack(ret); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 297 | { |
| 298 | struct sockaddr_in addr; |
| 299 | int ret, on = 1; |
| 300 | |
| 301 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 302 | bfd->cb = ipaccess_listen_bsc_cb; |
| 303 | bfd->when = BSC_FD_READ; |
| 304 | |
| 305 | memset(&addr, 0, sizeof(addr)); |
| 306 | addr.sin_family = AF_INET; |
| 307 | addr.sin_port = htons(port); |
| 308 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 309 | |
| 310 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 311 | |
| 312 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 313 | if (ret < 0) { |
| 314 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 315 | strerror(errno)); |
| 316 | return -EIO; |
| 317 | } |
| 318 | |
| 319 | ret = listen(bfd->fd, 1); |
| 320 | if (ret < 0) { |
| 321 | perror("listen"); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | ret = bsc_register_fd(bfd); |
| 326 | if (ret < 0) { |
| 327 | perror("register_listen_fd"); |
| 328 | return ret; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 333 | static void print_usage() |
| 334 | { |
| 335 | printf("Usage: bsc_nat\n"); |
| 336 | } |
| 337 | |
| 338 | static void print_help() |
| 339 | { |
| 340 | printf(" Some useful help...\n"); |
| 341 | printf(" -h --help this text\n"); |
| 342 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 343 | printf(" -s --disable-color\n"); |
| 344 | printf(" -c --config-file filename The config file to use.\n"); |
| 345 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 346 | 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] | 347 | } |
| 348 | |
| 349 | static void handle_options(int argc, char** argv) |
| 350 | { |
| 351 | while (1) { |
| 352 | int option_index = 0, c; |
| 353 | static struct option long_options[] = { |
| 354 | {"help", 0, 0, 'h'}, |
| 355 | {"debug", 1, 0, 'd'}, |
| 356 | {"config-file", 1, 0, 'c'}, |
| 357 | {"disable-color", 0, 0, 's'}, |
| 358 | {"timestamp", 0, 0, 'T'}, |
| 359 | {"msc", 1, 0, 'm'}, |
| 360 | {"local", 1, 0, 'l'}, |
| 361 | {0, 0, 0, 0} |
| 362 | }; |
| 363 | |
| 364 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 365 | long_options, &option_index); |
| 366 | if (c == -1) |
| 367 | break; |
| 368 | |
| 369 | switch (c) { |
| 370 | case 'h': |
| 371 | print_usage(); |
| 372 | print_help(); |
| 373 | exit(0); |
| 374 | case 's': |
| 375 | debug_use_color(0); |
| 376 | break; |
| 377 | case 'd': |
| 378 | debug_parse_category_mask(optarg); |
| 379 | break; |
| 380 | case 'c': |
| 381 | config_file = strdup(optarg); |
| 382 | break; |
| 383 | case 'T': |
| 384 | debug_timestamp(1); |
| 385 | break; |
| 386 | case 'm': |
| 387 | msc_address = strdup(optarg); |
| 388 | break; |
| 389 | case 'l': |
| 390 | inet_aton(optarg, &local_addr); |
| 391 | break; |
| 392 | default: |
| 393 | /* ignore */ |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 399 | static void signal_handler(int signal) |
| 400 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 401 | switch (signal) { |
| 402 | case SIGABRT: |
| 403 | /* in case of abort, we want to obtain a talloc report |
| 404 | * and then return to the caller, who will abort the process */ |
| 405 | case SIGUSR1: |
| 406 | talloc_report_full(tall_bsc_ctx, stderr); |
| 407 | break; |
| 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 413 | int main(int argc, char** argv) |
| 414 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 415 | int rc; |
| 416 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 417 | /* parse options */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 418 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 419 | handle_options(argc, argv); |
| 420 | |
| 421 | /* seed the PRNG */ |
| 422 | srand(time(NULL)); |
| 423 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 424 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 425 | msc_connection.cb = ipaccess_msc_cb; |
| 426 | rc = connect_to_msc(&msc_connection, msc_address, 5000); |
| 427 | if (rc < 0) { |
| 428 | fprintf(stderr, "Opening the MSC connection failed.\n"); |
| 429 | exit(1); |
| 430 | } |
| 431 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 432 | /* wait for the BSC */ |
| 433 | if (listen_for_bsc(&bsc_connection, &local_addr, 5000) < 0) { |
| 434 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 435 | exit(1); |
| 436 | } |
| 437 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 438 | signal(SIGINT, &signal_handler); |
| 439 | signal(SIGABRT, &signal_handler); |
| 440 | signal(SIGUSR1, &signal_handler); |
| 441 | signal(SIGPIPE, SIG_IGN); |
| 442 | |
| 443 | while (1) { |
| 444 | bsc_select_main(0); |
| 445 | } |
| 446 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 447 | return 0; |
| 448 | } |