Holger Hans Peter Freyther | 89d9fd9 | 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 | 98e49d4 | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 89d9fd9 | 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 | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 29 | #include <signal.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Holger Hans Peter Freyther | fd012d5 | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 89d9fd9 | 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 | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 39 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 40 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 41 | #include <openbsc/bssap.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/ipaccess.h> |
| 43 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 44 | #include <openbsc/telnet_interface.h> |
| 45 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 46 | #include <osmocore/talloc.h> |
| 47 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 48 | #include <vty/vty.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 49 | |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 50 | #include <sccp/sccp.h> |
| 51 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 52 | struct debug_target *stderr_target; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 53 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 89d9fd9 | 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 | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 56 | static struct bsc_msc_connection *msc_con; |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 57 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 59 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 60 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 2ec5517 | 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 | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 62 | static void remove_bsc_connection(struct bsc_connection *connection); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 63 | |
Holger Hans Peter Freyther | 5e54788 | 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 | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 74 | |
Holger Hans Peter Freyther | e907cb2 | 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 | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 92 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 722ead8 | 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 | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 101 | bsc_write(bsc, gsm_reset_ack, sizeof(gsm_reset_ack)); |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 104 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 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 | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 110 | bsc_write(bsc, id_ack, sizeof(id_ack)); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 113 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 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 | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 127 | bsc_write(bsc, id_req, sizeof(id_req)); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Holger Hans Peter Freyther | e907cb2 | 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 | 2ec5517 | 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 | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 147 | { |
Holger Hans Peter Freyther | 2ec5517 | 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 | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 169 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 170 | { |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 171 | struct sccp_connections *con; |
| 172 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 173 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 174 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 175 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 176 | parsed = bsc_nat_parse(msg); |
| 177 | if (!parsed) { |
| 178 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 179 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 182 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 183 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 184 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 185 | /* Route and modify the SCCP packet */ |
| 186 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 187 | switch (parsed->sccp_type) { |
| 188 | case SCCP_MSG_TYPE_UDT: |
| 189 | /* forward UDT messages to every BSC */ |
| 190 | goto send_to_all; |
| 191 | break; |
| 192 | case SCCP_MSG_TYPE_RLSD: |
| 193 | case SCCP_MSG_TYPE_CREF: |
| 194 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 195 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 196 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
| 197 | if (con) { |
| 198 | if (bsc_mgcp_assign(con, msg) != 0) |
| 199 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 200 | } else |
| 201 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
| 202 | } |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 203 | break; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 204 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 205 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 206 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 207 | goto exit; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 208 | break; |
| 209 | case SCCP_MSG_TYPE_RLC: |
| 210 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 211 | goto exit; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 212 | break; |
| 213 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 214 | /* MSC never opens a SCCP connection, fall through */ |
| 215 | default: |
| 216 | goto exit; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | talloc_free(parsed); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 221 | if (!con) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 222 | return -1; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 223 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 224 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 228 | bsc_write(con->bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 229 | return 0; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 230 | |
| 231 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 232 | /* |
| 233 | * Filter Paging from the network. We do not want to send a PAGING |
| 234 | * Command to every BSC in our network. We will analys the PAGING |
| 235 | * message and then send it to the authenticated messages... |
| 236 | */ |
| 237 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 238 | bsc = bsc_nat_find_bsc(nat, msg); |
| 239 | if (bsc) |
| 240 | bsc_write(bsc, msg->data, msg->len); |
| 241 | else |
| 242 | LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n"); |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 243 | |
| 244 | goto exit; |
| 245 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 246 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 247 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 248 | if (!bsc->authenticated) |
| 249 | continue; |
| 250 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 251 | bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 252 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 253 | |
| 254 | exit: |
| 255 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 256 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 259 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 260 | { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 261 | struct bsc_connection *bsc, *tmp; |
| 262 | |
| 263 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 264 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
| 265 | remove_bsc_connection(bsc); |
| 266 | |
| 267 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 270 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 271 | { |
| 272 | int error; |
| 273 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 274 | struct ipaccess_head *hh; |
| 275 | |
| 276 | if (!msg) { |
| 277 | if (error == 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 278 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 279 | bsc_msc_lost(msc_con); |
| 280 | return -1; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 283 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 287 | LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 288 | |
| 289 | /* handle base message handling */ |
| 290 | hh = (struct ipaccess_head *) msg->data; |
| 291 | ipaccess_rcvmsg_base(msg, bfd); |
| 292 | |
| 293 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 294 | if (hh->proto == IPAC_PROTO_IPACCESS && msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 295 | initialize_msc_if_needed(); |
| 296 | else if (hh->proto == IPAC_PROTO_SCCP) |
| 297 | forward_sccp_to_bts(msg); |
| 298 | |
Holger Hans Peter Freyther | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 299 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 300 | return 0; |
| 301 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 302 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 303 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 304 | { |
| 305 | int rc; |
| 306 | rc = write(bfd->fd, msg->data, msg->len); |
| 307 | |
| 308 | if (rc != msg->len) { |
| 309 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | return rc; |
| 314 | } |
| 315 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 316 | /* |
| 317 | * Below is the handling of messages coming |
| 318 | * from the BSC and need to be forwarded to |
| 319 | * a real BSC. |
| 320 | */ |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * Remove the connection from the connections list, |
| 324 | * remove it from the patching of SCCP header lists |
| 325 | * as well. Maybe in the future even close connection.. |
| 326 | */ |
| 327 | static void remove_bsc_connection(struct bsc_connection *connection) |
| 328 | { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 329 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 330 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 331 | close(connection->write_queue.bfd.fd); |
Holger Hans Peter Freyther | ffcf79c | 2010-03-26 09:27:08 +0100 | [diff] [blame] | 332 | write_queue_clear(&connection->write_queue); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 333 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 334 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 335 | /* stop the timeout timer */ |
| 336 | bsc_del_timer(&connection->id_timeout); |
| 337 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 338 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 339 | llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 340 | if (sccp_patch->bsc != connection) |
| 341 | continue; |
| 342 | |
Holger Hans Peter Freyther | a207db2 | 2010-03-26 09:28:40 +0100 | [diff] [blame] | 343 | #warning "TODO: Send a RLSD to the MSC. Or at least a clear command." |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 344 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 347 | talloc_free(connection); |
| 348 | } |
| 349 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 350 | static void ipaccess_close_bsc(void *data) |
| 351 | { |
| 352 | struct bsc_connection *conn = data; |
| 353 | |
| 354 | LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n"); |
| 355 | remove_bsc_connection(conn); |
| 356 | } |
| 357 | |
| 358 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 359 | { |
| 360 | struct bsc_config *conf; |
| 361 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 362 | |
| 363 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 364 | if (strcmp(conf->token, token) == 0) { |
| 365 | bsc->authenticated = 1; |
| 366 | bsc->lac = conf->lac; |
| 367 | bsc_del_timer(&bsc->id_timeout); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 373 | static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 374 | { |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 375 | struct sccp_connections *con; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 376 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 377 | |
Holger Hans Peter Freyther | 45f6295 | 2010-03-30 07:35:46 +0200 | [diff] [blame] | 378 | if (!bsc->authenticated) { |
| 379 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 380 | msgb_free(msg); |
| 381 | return -1; |
| 382 | } |
| 383 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 384 | /* Parse and filter messages */ |
| 385 | parsed = bsc_nat_parse(msg); |
| 386 | if (!parsed) { |
| 387 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 45f6295 | 2010-03-30 07:35:46 +0200 | [diff] [blame] | 388 | msgb_free(msg); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 389 | return -1; |
| 390 | } |
| 391 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 392 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 393 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 394 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 395 | /* modify the SCCP entries */ |
| 396 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 397 | switch (parsed->sccp_type) { |
| 398 | case SCCP_MSG_TYPE_CR: |
| 399 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 400 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 401 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 402 | break; |
| 403 | case SCCP_MSG_TYPE_RLSD: |
| 404 | case SCCP_MSG_TYPE_CREF: |
| 405 | case SCCP_MSG_TYPE_DT1: |
| 406 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 407 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 408 | break; |
| 409 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 410 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 411 | if (con) { |
| 412 | remove_sccp_src_ref(bsc, msg, parsed); |
| 413 | bsc_mgcp_clear(con); |
| 414 | } |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 415 | break; |
| 416 | case SCCP_MSG_TYPE_UDT: |
| 417 | /* simply forward everything */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 418 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 419 | break; |
| 420 | default: |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 421 | LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 422 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 423 | goto exit2; |
| 424 | break; |
| 425 | } |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 426 | } else { |
| 427 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 428 | goto exit2; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 431 | if (con && con->bsc != bsc) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 432 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 433 | goto exit2; |
| 434 | } |
| 435 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 436 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 437 | if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 438 | LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n"); |
| 439 | msgb_free(msg); |
| 440 | } |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 441 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 442 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 443 | |
| 444 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 445 | /* if we filter out the reset send an ack to the BSC */ |
| 446 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 447 | send_reset_ack(bsc); |
| 448 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 449 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 450 | /* do we know who is handling this? */ |
| 451 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 452 | struct tlv_parsed tvp; |
| 453 | ipaccess_idtag_parse(&tvp, |
| 454 | (unsigned char *) msg->l2h + 2, |
| 455 | msgb_l2len(msg) - 2); |
| 456 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 457 | ipaccess_auth_bsc(&tvp, bsc); |
| 458 | } |
| 459 | |
| 460 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 463 | exit2: |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 464 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 465 | msgb_free(msg); |
| 466 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 469 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 470 | { |
| 471 | int error; |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 472 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 473 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 474 | |
| 475 | if (!msg) { |
| 476 | if (error == 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 477 | LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n"); |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 478 | remove_bsc_connection(bsc); |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 479 | } else { |
| 480 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 481 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 482 | return -1; |
| 483 | } |
| 484 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 485 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 486 | LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 487 | |
| 488 | /* Handle messages from the BSC */ |
| 489 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 490 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 491 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 496 | static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 497 | { |
| 498 | int rc; |
| 499 | |
| 500 | rc = write(bfd->fd, msg->data, msg->len); |
| 501 | if (rc != msg->len) |
| 502 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 503 | |
| 504 | return rc; |
| 505 | } |
| 506 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 507 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 508 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 509 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 510 | int ret; |
| 511 | struct sockaddr_in sa; |
| 512 | socklen_t sa_len = sizeof(sa); |
| 513 | |
| 514 | if (!(what & BSC_FD_READ)) |
| 515 | return 0; |
| 516 | |
| 517 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 518 | if (ret < 0) { |
| 519 | perror("accept"); |
| 520 | return ret; |
| 521 | } |
| 522 | |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 523 | /* |
| 524 | * if we are not connected to a msc... just close the socket |
| 525 | */ |
| 526 | if (!msc_con->is_connected) { |
| 527 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 531 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 532 | /* todo... use GNUtls to see if we want to trust this as a BTS */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 533 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 534 | /* |
| 535 | * |
| 536 | */ |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 537 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 538 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 539 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 540 | close(ret); |
| 541 | return -1; |
| 542 | } |
| 543 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 544 | write_queue_init(&bsc->write_queue, 100); |
| 545 | bsc->write_queue.bfd.data = bsc; |
| 546 | bsc->write_queue.bfd.fd = ret; |
| 547 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 548 | bsc->write_queue.write_cb = ipaccess_bsc_write_cb; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 549 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 550 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 551 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 552 | close(ret); |
| 553 | talloc_free(bsc); |
| 554 | return -2; |
| 555 | } |
| 556 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 557 | LOGP(DNAT, LOGL_INFO, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 558 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 559 | send_id_ack(bsc); |
| 560 | send_id_req(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * start the hangup timer |
| 564 | */ |
| 565 | bsc->id_timeout.data = bsc; |
| 566 | bsc->id_timeout.cb = ipaccess_close_bsc; |
| 567 | bsc_schedule_timer(&bsc->id_timeout, 2, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 572 | { |
| 573 | struct sockaddr_in addr; |
| 574 | int ret, on = 1; |
| 575 | |
| 576 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 577 | bfd->cb = ipaccess_listen_bsc_cb; |
| 578 | bfd->when = BSC_FD_READ; |
| 579 | |
| 580 | memset(&addr, 0, sizeof(addr)); |
| 581 | addr.sin_family = AF_INET; |
| 582 | addr.sin_port = htons(port); |
| 583 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 584 | |
| 585 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 586 | |
| 587 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 588 | if (ret < 0) { |
| 589 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 590 | strerror(errno)); |
| 591 | return -EIO; |
| 592 | } |
| 593 | |
| 594 | ret = listen(bfd->fd, 1); |
| 595 | if (ret < 0) { |
| 596 | perror("listen"); |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | ret = bsc_register_fd(bfd); |
| 601 | if (ret < 0) { |
| 602 | perror("register_listen_fd"); |
| 603 | return ret; |
| 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 608 | static void print_usage() |
| 609 | { |
| 610 | printf("Usage: bsc_nat\n"); |
| 611 | } |
| 612 | |
| 613 | static void print_help() |
| 614 | { |
| 615 | printf(" Some useful help...\n"); |
| 616 | printf(" -h --help this text\n"); |
| 617 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 618 | printf(" -s --disable-color\n"); |
| 619 | printf(" -c --config-file filename The config file to use.\n"); |
| 620 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 621 | printf(" -l --local=IP. The local address of this BSC.\n"); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static void handle_options(int argc, char** argv) |
| 625 | { |
| 626 | while (1) { |
| 627 | int option_index = 0, c; |
| 628 | static struct option long_options[] = { |
| 629 | {"help", 0, 0, 'h'}, |
| 630 | {"debug", 1, 0, 'd'}, |
| 631 | {"config-file", 1, 0, 'c'}, |
| 632 | {"disable-color", 0, 0, 's'}, |
| 633 | {"timestamp", 0, 0, 'T'}, |
| 634 | {"msc", 1, 0, 'm'}, |
| 635 | {"local", 1, 0, 'l'}, |
| 636 | {0, 0, 0, 0} |
| 637 | }; |
| 638 | |
| 639 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 640 | long_options, &option_index); |
| 641 | if (c == -1) |
| 642 | break; |
| 643 | |
| 644 | switch (c) { |
| 645 | case 'h': |
| 646 | print_usage(); |
| 647 | print_help(); |
| 648 | exit(0); |
| 649 | case 's': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 650 | debug_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 651 | break; |
| 652 | case 'd': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 653 | debug_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 654 | break; |
| 655 | case 'c': |
| 656 | config_file = strdup(optarg); |
| 657 | break; |
| 658 | case 'T': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 659 | debug_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 660 | break; |
| 661 | case 'm': |
| 662 | msc_address = strdup(optarg); |
| 663 | break; |
| 664 | case 'l': |
| 665 | inet_aton(optarg, &local_addr); |
| 666 | break; |
| 667 | default: |
| 668 | /* ignore */ |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 674 | static void signal_handler(int signal) |
| 675 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 676 | switch (signal) { |
| 677 | case SIGABRT: |
| 678 | /* in case of abort, we want to obtain a talloc report |
| 679 | * and then return to the caller, who will abort the process */ |
| 680 | case SIGUSR1: |
| 681 | talloc_report_full(tall_bsc_ctx, stderr); |
| 682 | break; |
| 683 | default: |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 688 | int main(int argc, char** argv) |
| 689 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 690 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 691 | debug_init(); |
| 692 | stderr_target = debug_target_create_stderr(); |
| 693 | debug_add_target(stderr_target); |
| 694 | debug_set_all_filter(stderr_target, 1); |
| 695 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 696 | /* parse options */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 697 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 698 | handle_options(argc, argv); |
| 699 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 700 | nat = bsc_nat_alloc(); |
| 701 | if (!nat) { |
| 702 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 703 | return -4; |
| 704 | } |
| 705 | |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame^] | 706 | nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config); |
| 707 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 708 | /* init vty and parse */ |
| 709 | bsc_nat_vty_init(nat); |
| 710 | telnet_init(NULL, 4244); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame^] | 711 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 712 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 713 | return -3; |
| 714 | } |
| 715 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 716 | /* seed the PRNG */ |
| 717 | srand(time(NULL)); |
| 718 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 719 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 4a860a0 | 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 | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 723 | exit(1); |
| 724 | } |
| 725 | |
Holger Hans Peter Freyther | 4a860a0 | 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 | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 731 | /* wait for the BSC */ |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 732 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 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 | e907cb2 | 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 | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 745 | return 0; |
| 746 | } |