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 | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 56 | static struct bsc_msc_connection *msc_con; |
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 | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 62 | static void remove_bsc_connection(struct bsc_connection *connection); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 63 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 64 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 65 | { |
| 66 | struct bsc_config *conf; |
| 67 | |
| 68 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 69 | if (conf->nr == num) |
| 70 | return conf; |
| 71 | |
| 72 | return NULL; |
| 73 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 74 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 75 | /* |
| 76 | * below are stubs we need to link |
| 77 | */ |
| 78 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
| 79 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state) |
| 80 | { |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 85 | {} |
| 86 | |
| 87 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 88 | { |
| 89 | return -1; |
| 90 | } |
| 91 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 92 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 93 | { |
| 94 | static const u_int8_t gsm_reset_ack[] = { |
| 95 | 0x00, 0x13, 0xfd, |
| 96 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 97 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 98 | 0x00, 0x01, 0x31, |
| 99 | }; |
| 100 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 101 | bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack)); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 104 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 105 | { |
| 106 | static const u_int8_t id_ack[] = { |
| 107 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK |
| 108 | }; |
| 109 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 110 | bsc_write(bsc, id_ack, sizeof(id_ack)); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 113 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 114 | { |
| 115 | static const u_int8_t id_req[] = { |
| 116 | 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
| 117 | 0x01, IPAC_IDTAG_UNIT, |
| 118 | 0x01, IPAC_IDTAG_MACADDR, |
| 119 | 0x01, IPAC_IDTAG_LOCATION1, |
| 120 | 0x01, IPAC_IDTAG_LOCATION2, |
| 121 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 122 | 0x01, IPAC_IDTAG_SWVERSION, |
| 123 | 0x01, IPAC_IDTAG_UNITNAME, |
| 124 | 0x01, IPAC_IDTAG_SERNR, |
| 125 | }; |
| 126 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 127 | bsc_write(bsc, id_req, sizeof(id_req)); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 130 | /* |
| 131 | * Below is the handling of messages coming |
| 132 | * from the MSC and need to be forwarded to |
| 133 | * a real BSC. |
| 134 | */ |
| 135 | static void initialize_msc_if_needed() |
| 136 | { |
| 137 | static int init = 0; |
| 138 | init = 1; |
| 139 | |
| 140 | /* do we need to send a GSM 08.08 message here? */ |
| 141 | } |
| 142 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 143 | /* |
| 144 | * Currently we are lacking refcounting so we need to copy each message. |
| 145 | */ |
| 146 | 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] | 147 | { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 148 | struct msgb *msg; |
| 149 | |
| 150 | if (length > 4096) { |
| 151 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | msg = msgb_alloc(4096, "to-bsc"); |
| 156 | if (!msg) { |
| 157 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | msgb_put(msg, length); |
| 162 | memcpy(msg->data, data, length); |
| 163 | if (write_queue_enqueue(&bsc->write_queue, msg) != 0) { |
| 164 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 165 | msgb_free(msg); |
| 166 | } |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 169 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 170 | { |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 171 | struct bsc_connection *bsc = NULL; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 172 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 173 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 174 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 175 | parsed = bsc_nat_parse(msg); |
| 176 | if (!parsed) { |
| 177 | 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] | 178 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 181 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 182 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 183 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 184 | /* Route and modify the SCCP packet */ |
| 185 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 186 | switch (parsed->sccp_type) { |
| 187 | case SCCP_MSG_TYPE_UDT: |
| 188 | /* forward UDT messages to every BSC */ |
| 189 | goto send_to_all; |
| 190 | break; |
| 191 | case SCCP_MSG_TYPE_RLSD: |
| 192 | case SCCP_MSG_TYPE_CREF: |
| 193 | case SCCP_MSG_TYPE_DT1: |
| 194 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 195 | bsc = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 196 | break; |
| 197 | case SCCP_MSG_TYPE_RLC: |
| 198 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 199 | goto exit; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 200 | break; |
| 201 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 202 | /* MSC never opens a SCCP connection, fall through */ |
| 203 | default: |
| 204 | goto exit; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | talloc_free(parsed); |
| 209 | if (!bsc) |
| 210 | return -1; |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 211 | if (!bsc->authenticated) { |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 212 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 213 | return -1; |
| 214 | } |
| 215 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 216 | bsc_write(bsc, msg->data, msg->len); |
| 217 | return 0; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 218 | |
| 219 | send_to_all: |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 220 | /* |
| 221 | * Filter Paging from the network. We do not want to send a PAGING |
| 222 | * Command to every BSC in our network. We will analys the PAGING |
| 223 | * message and then send it to the authenticated messages... |
| 224 | */ |
| 225 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
| 226 | int data_length; |
| 227 | const u_int8_t *data; |
| 228 | struct tlv_parsed tp; |
| 229 | int i = 0; |
| 230 | |
| 231 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 232 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 233 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 234 | goto exit; |
| 235 | } |
| 236 | |
| 237 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 238 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 239 | if (data[0] != CELL_IDENT_LAC) { |
| 240 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]); |
| 241 | goto exit; |
| 242 | } |
| 243 | |
| 244 | /* go through each LAC and forward the message */ |
| 245 | for (i = 1; i < data_length - 1; i += 2) { |
| 246 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
| 247 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 248 | if (!bsc->authenticated || _lac != bsc->lac) |
| 249 | continue; |
| 250 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 251 | bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | goto exit; |
| 256 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 257 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 258 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 259 | if (!bsc->authenticated) |
| 260 | continue; |
| 261 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 262 | bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 263 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 264 | |
| 265 | exit: |
| 266 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 267 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 270 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 271 | { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 272 | struct bsc_connection *bsc, *tmp; |
| 273 | |
| 274 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 275 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
| 276 | remove_bsc_connection(bsc); |
| 277 | |
| 278 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 281 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 282 | { |
| 283 | int error; |
| 284 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 285 | struct ipaccess_head *hh; |
| 286 | |
| 287 | if (!msg) { |
| 288 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 289 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 290 | bsc_msc_lost(msc_con); |
| 291 | return -1; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 294 | 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] | 295 | return -1; |
| 296 | } |
| 297 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 298 | 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] | 299 | |
| 300 | /* handle base message handling */ |
| 301 | hh = (struct ipaccess_head *) msg->data; |
| 302 | ipaccess_rcvmsg_base(msg, bfd); |
| 303 | |
| 304 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 305 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 306 | initialize_msc_if_needed(); |
| 307 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 308 | forward_sccp_to_bts(msg); |
| 309 | |
Holger Hans Peter Freyther | aad68b5 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 310 | msgb_free(msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 311 | return 0; |
| 312 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 314 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 315 | { |
| 316 | int rc; |
| 317 | rc = write(bfd->fd, msg->data, msg->len); |
| 318 | |
| 319 | if (rc != msg->len) { |
| 320 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | return rc; |
| 325 | } |
| 326 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Below is the handling of messages coming |
| 329 | * from the BSC and need to be forwarded to |
| 330 | * a real BSC. |
| 331 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * Remove the connection from the connections list, |
| 335 | * remove it from the patching of SCCP header lists |
| 336 | * as well. Maybe in the future even close connection.. |
| 337 | */ |
| 338 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 339 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 340 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 341 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 342 | close(connection->write_queue.bfd.fd); |
Holger Hans Peter Freyther | f38e879 | 2010-03-26 09:27:08 +0100 | [diff] [blame] | 343 | write_queue_clear(&connection->write_queue); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 344 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 345 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 346 | /* stop the timeout timer */ |
| 347 | bsc_del_timer(&connection->id_timeout); |
| 348 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 349 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 350 | 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] | 351 | if (sccp_patch->bsc != connection) |
| 352 | continue; |
| 353 | |
Holger Hans Peter Freyther | 7c99d4f | 2010-03-26 09:28:40 +0100 | [diff] [blame] | 354 | #warning "TODO: Send a RLSD to the MSC. Or at least a clear command." |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 355 | llist_del(&sccp_patch->list_entry); |
| 356 | talloc_free(sccp_patch); |
| 357 | } |
| 358 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 359 | talloc_free(connection); |
| 360 | } |
| 361 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 362 | static void ipaccess_close_bsc(void *data) |
| 363 | { |
| 364 | struct bsc_connection *conn = data; |
| 365 | |
| 366 | LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n"); |
| 367 | remove_bsc_connection(conn); |
| 368 | } |
| 369 | |
| 370 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 371 | { |
| 372 | struct bsc_config *conf; |
| 373 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 374 | |
| 375 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 376 | if (strcmp(conf->token, token) == 0) { |
| 377 | bsc->authenticated = 1; |
| 378 | bsc->lac = conf->lac; |
| 379 | bsc_del_timer(&bsc->id_timeout); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 385 | 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] | 386 | { |
Holger Hans Peter Freyther | 7c11d1d | 2010-02-09 16:30:53 +0100 | [diff] [blame] | 387 | struct bsc_connection *found_bsc = NULL; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 388 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 389 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 390 | /* Parse and filter messages */ |
| 391 | parsed = bsc_nat_parse(msg); |
| 392 | if (!parsed) { |
| 393 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
| 394 | return -1; |
| 395 | } |
| 396 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 397 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 398 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 399 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 400 | /* modify the SCCP entries */ |
| 401 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 402 | switch (parsed->sccp_type) { |
| 403 | case SCCP_MSG_TYPE_CR: |
| 404 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 405 | goto exit2; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 406 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 407 | break; |
| 408 | case SCCP_MSG_TYPE_RLSD: |
| 409 | case SCCP_MSG_TYPE_CREF: |
| 410 | case SCCP_MSG_TYPE_DT1: |
| 411 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 412 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 413 | break; |
| 414 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 415 | found_bsc = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 416 | remove_sccp_src_ref(bsc, msg, parsed); |
| 417 | break; |
| 418 | case SCCP_MSG_TYPE_UDT: |
| 419 | /* simply forward everything */ |
| 420 | break; |
| 421 | default: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 422 | LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 423 | goto exit2; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (found_bsc != bsc) { |
| 429 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 430 | goto exit2; |
| 431 | } |
| 432 | |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 433 | if (!bsc->authenticated) { |
| 434 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 435 | goto exit2; |
| 436 | } |
| 437 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 438 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 439 | if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 440 | LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n"); |
| 441 | msgb_free(msg); |
| 442 | } |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 443 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 444 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 445 | |
| 446 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 447 | /* if we filter out the reset send an ack to the BSC */ |
| 448 | 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] | 449 | send_reset_ack(bsc); |
| 450 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 451 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 452 | /* do we know who is handling this? */ |
| 453 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 454 | struct tlv_parsed tvp; |
| 455 | ipaccess_idtag_parse(&tvp, |
| 456 | (unsigned char *) msg->l2h + 2, |
| 457 | msgb_l2len(msg) - 2); |
| 458 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 459 | ipaccess_auth_bsc(&tvp, bsc); |
| 460 | } |
| 461 | |
| 462 | goto exit2; |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 465 | exit2: |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 466 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 467 | msgb_free(msg); |
| 468 | return -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 471 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 472 | { |
| 473 | int error; |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 474 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 475 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 476 | |
| 477 | if (!msg) { |
| 478 | if (error == 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 479 | 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] | 480 | remove_bsc_connection(bsc); |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 481 | } else { |
| 482 | 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] | 483 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 484 | return -1; |
| 485 | } |
| 486 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 487 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 488 | 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] | 489 | |
| 490 | /* Handle messages from the BSC */ |
| 491 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 492 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 493 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 498 | static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 499 | { |
| 500 | int rc; |
| 501 | |
| 502 | rc = write(bfd->fd, msg->data, msg->len); |
| 503 | if (rc != msg->len) |
| 504 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 505 | |
| 506 | return rc; |
| 507 | } |
| 508 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 509 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 510 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 511 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 512 | int ret; |
| 513 | struct sockaddr_in sa; |
| 514 | socklen_t sa_len = sizeof(sa); |
| 515 | |
| 516 | if (!(what & BSC_FD_READ)) |
| 517 | return 0; |
| 518 | |
| 519 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 520 | if (ret < 0) { |
| 521 | perror("accept"); |
| 522 | return ret; |
| 523 | } |
| 524 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 525 | /* |
| 526 | * if we are not connected to a msc... just close the socket |
| 527 | */ |
| 528 | if (!msc_con->is_connected) { |
| 529 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
| 530 | return 0; |
| 531 | } |
| 532 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 533 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 534 | /* 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] | 535 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 536 | /* |
| 537 | * |
| 538 | */ |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 539 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 540 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 541 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 542 | close(ret); |
| 543 | return -1; |
| 544 | } |
| 545 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 546 | write_queue_init(&bsc->write_queue, 100); |
| 547 | bsc->write_queue.bfd.data = bsc; |
| 548 | bsc->write_queue.bfd.fd = ret; |
| 549 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 550 | bsc->write_queue.write_cb = ipaccess_bsc_write_cb; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 551 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 552 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 553 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 554 | close(ret); |
| 555 | talloc_free(bsc); |
| 556 | return -2; |
| 557 | } |
| 558 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 559 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 560 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 561 | send_id_ack(bsc); |
| 562 | send_id_req(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * start the hangup timer |
| 566 | */ |
| 567 | bsc->id_timeout.data = bsc; |
| 568 | bsc->id_timeout.cb = ipaccess_close_bsc; |
| 569 | bsc_schedule_timer(&bsc->id_timeout, 2, 0); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 574 | { |
| 575 | struct sockaddr_in addr; |
| 576 | int ret, on = 1; |
| 577 | |
| 578 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 579 | bfd->cb = ipaccess_listen_bsc_cb; |
| 580 | bfd->when = BSC_FD_READ; |
| 581 | |
| 582 | memset(&addr, 0, sizeof(addr)); |
| 583 | addr.sin_family = AF_INET; |
| 584 | addr.sin_port = htons(port); |
| 585 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 586 | |
| 587 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 588 | |
| 589 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 590 | if (ret < 0) { |
| 591 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 592 | strerror(errno)); |
| 593 | return -EIO; |
| 594 | } |
| 595 | |
| 596 | ret = listen(bfd->fd, 1); |
| 597 | if (ret < 0) { |
| 598 | perror("listen"); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | ret = bsc_register_fd(bfd); |
| 603 | if (ret < 0) { |
| 604 | perror("register_listen_fd"); |
| 605 | return ret; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 610 | static void print_usage() |
| 611 | { |
| 612 | printf("Usage: bsc_nat\n"); |
| 613 | } |
| 614 | |
| 615 | static void print_help() |
| 616 | { |
| 617 | printf(" Some useful help...\n"); |
| 618 | printf(" -h --help this text\n"); |
| 619 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 620 | printf(" -s --disable-color\n"); |
| 621 | printf(" -c --config-file filename The config file to use.\n"); |
| 622 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 623 | 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] | 624 | } |
| 625 | |
| 626 | static void handle_options(int argc, char** argv) |
| 627 | { |
| 628 | while (1) { |
| 629 | int option_index = 0, c; |
| 630 | static struct option long_options[] = { |
| 631 | {"help", 0, 0, 'h'}, |
| 632 | {"debug", 1, 0, 'd'}, |
| 633 | {"config-file", 1, 0, 'c'}, |
| 634 | {"disable-color", 0, 0, 's'}, |
| 635 | {"timestamp", 0, 0, 'T'}, |
| 636 | {"msc", 1, 0, 'm'}, |
| 637 | {"local", 1, 0, 'l'}, |
| 638 | {0, 0, 0, 0} |
| 639 | }; |
| 640 | |
| 641 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 642 | long_options, &option_index); |
| 643 | if (c == -1) |
| 644 | break; |
| 645 | |
| 646 | switch (c) { |
| 647 | case 'h': |
| 648 | print_usage(); |
| 649 | print_help(); |
| 650 | exit(0); |
| 651 | case 's': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 652 | debug_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 653 | break; |
| 654 | case 'd': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 655 | debug_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 656 | break; |
| 657 | case 'c': |
| 658 | config_file = strdup(optarg); |
| 659 | break; |
| 660 | case 'T': |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 661 | debug_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 662 | break; |
| 663 | case 'm': |
| 664 | msc_address = strdup(optarg); |
| 665 | break; |
| 666 | case 'l': |
| 667 | inet_aton(optarg, &local_addr); |
| 668 | break; |
| 669 | default: |
| 670 | /* ignore */ |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 676 | static void signal_handler(int signal) |
| 677 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 678 | switch (signal) { |
| 679 | case SIGABRT: |
| 680 | /* in case of abort, we want to obtain a talloc report |
| 681 | * and then return to the caller, who will abort the process */ |
| 682 | case SIGUSR1: |
| 683 | talloc_report_full(tall_bsc_ctx, stderr); |
| 684 | break; |
| 685 | default: |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 690 | int main(int argc, char** argv) |
| 691 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 692 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 693 | debug_init(); |
| 694 | stderr_target = debug_target_create_stderr(); |
| 695 | debug_add_target(stderr_target); |
| 696 | debug_set_all_filter(stderr_target, 1); |
| 697 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 698 | /* parse options */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 699 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 700 | handle_options(argc, argv); |
| 701 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 702 | nat = bsc_nat_alloc(); |
| 703 | if (!nat) { |
| 704 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 705 | return -4; |
| 706 | } |
| 707 | |
| 708 | /* init vty and parse */ |
| 709 | bsc_nat_vty_init(nat); |
| 710 | telnet_init(NULL, 4244); |
| 711 | if (vty_read_config_file(config_file) < 0) { |
| 712 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 713 | return -3; |
| 714 | } |
| 715 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 716 | /* seed the PRNG */ |
| 717 | srand(time(NULL)); |
| 718 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 719 | /* connect to the MSC */ |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 720 | msc_con = bsc_msc_create(msc_address, 5000); |
| 721 | if (!msc_con) { |
| 722 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 723 | exit(1); |
| 724 | } |
| 725 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 726 | msc_con->connection_loss = msc_connection_was_lost; |
| 727 | msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 728 | msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
| 729 | bsc_msc_connect(msc_con); |
| 730 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 731 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 732 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 733 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 734 | exit(1); |
| 735 | } |
| 736 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 737 | signal(SIGABRT, &signal_handler); |
| 738 | signal(SIGUSR1, &signal_handler); |
| 739 | signal(SIGPIPE, SIG_IGN); |
| 740 | |
| 741 | while (1) { |
| 742 | bsc_select_main(0); |
| 743 | } |
| 744 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 745 | return 0; |
| 746 | } |