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 | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 130 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 131 | { |
| 132 | static const u_int8_t mgcp_reset[] = { |
| 133 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 134 | }; |
| 135 | |
| 136 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 137 | } |
| 138 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Below is the handling of messages coming |
| 141 | * from the MSC and need to be forwarded to |
| 142 | * a real BSC. |
| 143 | */ |
| 144 | static void initialize_msc_if_needed() |
| 145 | { |
| 146 | static int init = 0; |
| 147 | init = 1; |
| 148 | |
| 149 | /* do we need to send a GSM 08.08 message here? */ |
| 150 | } |
| 151 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 152 | /* |
| 153 | * Currently we are lacking refcounting so we need to copy each message. |
| 154 | */ |
| 155 | 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] | 156 | { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 157 | struct msgb *msg; |
| 158 | |
| 159 | if (length > 4096) { |
| 160 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | msg = msgb_alloc(4096, "to-bsc"); |
| 165 | if (!msg) { |
| 166 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | msgb_put(msg, length); |
| 171 | memcpy(msg->data, data, length); |
| 172 | if (write_queue_enqueue(&bsc->write_queue, msg) != 0) { |
| 173 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 174 | msgb_free(msg); |
| 175 | } |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 178 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 179 | { |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 180 | struct sccp_connections *con; |
| 181 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 182 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 183 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 184 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 185 | parsed = bsc_nat_parse(msg); |
| 186 | if (!parsed) { |
| 187 | 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] | 188 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 191 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 192 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 193 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 194 | /* Route and modify the SCCP packet */ |
| 195 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 196 | switch (parsed->sccp_type) { |
| 197 | case SCCP_MSG_TYPE_UDT: |
| 198 | /* forward UDT messages to every BSC */ |
| 199 | goto send_to_all; |
| 200 | break; |
| 201 | case SCCP_MSG_TYPE_RLSD: |
| 202 | case SCCP_MSG_TYPE_CREF: |
| 203 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 204 | case SCCP_MSG_TYPE_IT: |
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); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 206 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
| 207 | if (con) { |
| 208 | if (bsc_mgcp_assign(con, msg) != 0) |
| 209 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 210 | } else |
| 211 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
| 212 | } |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 213 | break; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 214 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 215 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 216 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 217 | goto exit; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 218 | break; |
| 219 | case SCCP_MSG_TYPE_RLC: |
| 220 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 221 | goto exit; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 222 | break; |
| 223 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 224 | /* MSC never opens a SCCP connection, fall through */ |
| 225 | default: |
| 226 | goto exit; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | talloc_free(parsed); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 231 | if (!con) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 232 | return -1; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 233 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 234 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 235 | return -1; |
| 236 | } |
| 237 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 238 | bsc_write(con->bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 239 | return 0; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 240 | |
| 241 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 242 | /* |
| 243 | * Filter Paging from the network. We do not want to send a PAGING |
| 244 | * Command to every BSC in our network. We will analys the PAGING |
| 245 | * message and then send it to the authenticated messages... |
| 246 | */ |
| 247 | 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] | 248 | bsc = bsc_nat_find_bsc(nat, msg); |
| 249 | if (bsc) |
| 250 | bsc_write(bsc, msg->data, msg->len); |
| 251 | else |
| 252 | 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] | 253 | |
| 254 | goto exit; |
| 255 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 256 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 257 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 258 | if (!bsc->authenticated) |
| 259 | continue; |
| 260 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 261 | bsc_write(bsc, msg->data, msg->len); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 262 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 263 | |
| 264 | exit: |
| 265 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 266 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 269 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 270 | { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 271 | struct bsc_connection *bsc, *tmp; |
| 272 | |
| 273 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 274 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
| 275 | remove_bsc_connection(bsc); |
| 276 | |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 277 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 278 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 281 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 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 | 8f99b82 | 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 | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 290 | bsc_msc_lost(msc_con); |
| 291 | return -1; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Holger Hans Peter Freyther | 8f99b82 | 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 | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 295 | return -1; |
| 296 | } |
| 297 | |
Holger Hans Peter Freyther | 8f99b82 | 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 | e907cb2 | 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 | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 310 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 311 | return 0; |
| 312 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | 257a8cc | 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 | e8fa0f1 | 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 | 4a62960 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 340 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 8401054 | 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 | ffcf79c | 2010-03-26 09:27:08 +0100 | [diff] [blame] | 343 | write_queue_clear(&connection->write_queue); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 344 | llist_del(&connection->list_entry); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 345 | |
Holger Hans Peter Freyther | de55766 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 349 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 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 | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 351 | if (sccp_patch->bsc != connection) |
| 352 | continue; |
| 353 | |
Holger Hans Peter Freyther | a207db2 | 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 | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 355 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Holger Hans Peter Freyther | c0ff1d0 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 358 | /* close endpoints allocated by this BSC */ |
| 359 | bsc_mgcp_clear_endpoints_for(connection); |
| 360 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 361 | talloc_free(connection); |
| 362 | } |
| 363 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 364 | static void ipaccess_close_bsc(void *data) |
| 365 | { |
| 366 | struct bsc_connection *conn = data; |
| 367 | |
| 368 | LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n"); |
| 369 | remove_bsc_connection(conn); |
| 370 | } |
| 371 | |
| 372 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 373 | { |
| 374 | struct bsc_config *conf; |
| 375 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 376 | |
| 377 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 378 | if (strcmp(conf->token, token) == 0) { |
| 379 | bsc->authenticated = 1; |
| 380 | bsc->lac = conf->lac; |
| 381 | bsc_del_timer(&bsc->id_timeout); |
Holger Hans Peter Freyther | c12e193 | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 382 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc %d\n", bsc->lac); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 388 | 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] | 389 | { |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 390 | struct sccp_connections *con; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 391 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 392 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 393 | /* Parse and filter messages */ |
| 394 | parsed = bsc_nat_parse(msg); |
| 395 | if (!parsed) { |
| 396 | 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] | 397 | msgb_free(msg); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 401 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 402 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 403 | |
Holger Hans Peter Freyther | 5a723b6 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 404 | /* |
| 405 | * check authentication after filtering to not reject auth |
| 406 | * responses coming from the BSC. We have to make sure that |
| 407 | * nothing from the exit path will forward things to the MSC |
| 408 | */ |
| 409 | if (!bsc->authenticated) { |
| 410 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 411 | msgb_free(msg); |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 416 | /* modify the SCCP entries */ |
| 417 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 418 | switch (parsed->sccp_type) { |
| 419 | case SCCP_MSG_TYPE_CR: |
| 420 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 421 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 422 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 423 | break; |
| 424 | case SCCP_MSG_TYPE_RLSD: |
| 425 | case SCCP_MSG_TYPE_CREF: |
| 426 | case SCCP_MSG_TYPE_DT1: |
| 427 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 428 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 429 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 430 | break; |
| 431 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 432 | con = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 38712d0 | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 433 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 434 | break; |
| 435 | case SCCP_MSG_TYPE_UDT: |
| 436 | /* simply forward everything */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 437 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 438 | break; |
| 439 | default: |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 440 | 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] | 441 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 442 | goto exit2; |
| 443 | break; |
| 444 | } |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 445 | } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) { |
| 446 | bsc_mgcp_forward(bsc, msg); |
| 447 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 448 | } else { |
| 449 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 450 | goto exit2; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 453 | if (con && con->bsc != bsc) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 454 | LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n"); |
| 455 | goto exit2; |
| 456 | } |
| 457 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 458 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 459 | if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 460 | LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n"); |
| 461 | msgb_free(msg); |
| 462 | } |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 463 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 464 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 465 | |
| 466 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 467 | /* if we filter out the reset send an ack to the BSC */ |
| 468 | 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] | 469 | send_reset_ack(bsc); |
| 470 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 471 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 472 | /* do we know who is handling this? */ |
| 473 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 474 | struct tlv_parsed tvp; |
| 475 | ipaccess_idtag_parse(&tvp, |
| 476 | (unsigned char *) msg->l2h + 2, |
| 477 | msgb_l2len(msg) - 2); |
| 478 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 479 | ipaccess_auth_bsc(&tvp, bsc); |
| 480 | } |
| 481 | |
| 482 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 485 | exit2: |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 486 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 487 | msgb_free(msg); |
| 488 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 491 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 492 | { |
| 493 | int error; |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 494 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 495 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 496 | |
| 497 | if (!msg) { |
| 498 | if (error == 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 499 | 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] | 500 | remove_bsc_connection(bsc); |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 501 | } else { |
| 502 | 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] | 503 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 504 | return -1; |
| 505 | } |
| 506 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 507 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 508 | 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] | 509 | |
| 510 | /* Handle messages from the BSC */ |
| 511 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 512 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 513 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 518 | static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 519 | { |
| 520 | int rc; |
| 521 | |
| 522 | rc = write(bfd->fd, msg->data, msg->len); |
| 523 | if (rc != msg->len) |
| 524 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 525 | |
| 526 | return rc; |
| 527 | } |
| 528 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 529 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 530 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 531 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 532 | int ret; |
| 533 | struct sockaddr_in sa; |
| 534 | socklen_t sa_len = sizeof(sa); |
| 535 | |
| 536 | if (!(what & BSC_FD_READ)) |
| 537 | return 0; |
| 538 | |
| 539 | ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 540 | if (ret < 0) { |
| 541 | perror("accept"); |
| 542 | return ret; |
| 543 | } |
| 544 | |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 545 | /* |
| 546 | * if we are not connected to a msc... just close the socket |
| 547 | */ |
| 548 | if (!msc_con->is_connected) { |
| 549 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
Holger Hans Peter Freyther | a746697 | 2010-04-06 12:42:35 +0200 | [diff] [blame^] | 550 | close(ret); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 554 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 555 | /* 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] | 556 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 557 | /* |
| 558 | * |
| 559 | */ |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 560 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 561 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 562 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 563 | close(ret); |
| 564 | return -1; |
| 565 | } |
| 566 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 567 | write_queue_init(&bsc->write_queue, 100); |
| 568 | bsc->write_queue.bfd.data = bsc; |
| 569 | bsc->write_queue.bfd.fd = ret; |
| 570 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 571 | bsc->write_queue.write_cb = ipaccess_bsc_write_cb; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 572 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 573 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 574 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 575 | close(ret); |
| 576 | talloc_free(bsc); |
| 577 | return -2; |
| 578 | } |
| 579 | |
Holger Hans Peter Freyther | c12e193 | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 580 | LOGP(DNAT, LOGL_NOTICE, "Registered new BSC\n"); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 581 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 582 | send_id_ack(bsc); |
| 583 | send_id_req(bsc); |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 584 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * start the hangup timer |
| 588 | */ |
| 589 | bsc->id_timeout.data = bsc; |
| 590 | bsc->id_timeout.cb = ipaccess_close_bsc; |
| 591 | bsc_schedule_timer(&bsc->id_timeout, 2, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 596 | { |
| 597 | struct sockaddr_in addr; |
| 598 | int ret, on = 1; |
| 599 | |
| 600 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 601 | bfd->cb = ipaccess_listen_bsc_cb; |
| 602 | bfd->when = BSC_FD_READ; |
| 603 | |
| 604 | memset(&addr, 0, sizeof(addr)); |
| 605 | addr.sin_family = AF_INET; |
| 606 | addr.sin_port = htons(port); |
| 607 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 608 | |
| 609 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 610 | |
| 611 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 612 | if (ret < 0) { |
| 613 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 614 | strerror(errno)); |
| 615 | return -EIO; |
| 616 | } |
| 617 | |
| 618 | ret = listen(bfd->fd, 1); |
| 619 | if (ret < 0) { |
| 620 | perror("listen"); |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | ret = bsc_register_fd(bfd); |
| 625 | if (ret < 0) { |
| 626 | perror("register_listen_fd"); |
| 627 | return ret; |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 632 | static void print_usage() |
| 633 | { |
| 634 | printf("Usage: bsc_nat\n"); |
| 635 | } |
| 636 | |
| 637 | static void print_help() |
| 638 | { |
| 639 | printf(" Some useful help...\n"); |
| 640 | printf(" -h --help this text\n"); |
| 641 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 642 | printf(" -s --disable-color\n"); |
| 643 | printf(" -c --config-file filename The config file to use.\n"); |
| 644 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 645 | 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] | 646 | } |
| 647 | |
| 648 | static void handle_options(int argc, char** argv) |
| 649 | { |
| 650 | while (1) { |
| 651 | int option_index = 0, c; |
| 652 | static struct option long_options[] = { |
| 653 | {"help", 0, 0, 'h'}, |
| 654 | {"debug", 1, 0, 'd'}, |
| 655 | {"config-file", 1, 0, 'c'}, |
| 656 | {"disable-color", 0, 0, 's'}, |
| 657 | {"timestamp", 0, 0, 'T'}, |
| 658 | {"msc", 1, 0, 'm'}, |
| 659 | {"local", 1, 0, 'l'}, |
| 660 | {0, 0, 0, 0} |
| 661 | }; |
| 662 | |
| 663 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 664 | long_options, &option_index); |
| 665 | if (c == -1) |
| 666 | break; |
| 667 | |
| 668 | switch (c) { |
| 669 | case 'h': |
| 670 | print_usage(); |
| 671 | print_help(); |
| 672 | exit(0); |
| 673 | case 's': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 674 | debug_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 675 | break; |
| 676 | case 'd': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 677 | debug_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 678 | break; |
| 679 | case 'c': |
| 680 | config_file = strdup(optarg); |
| 681 | break; |
| 682 | case 'T': |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 683 | debug_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 684 | break; |
| 685 | case 'm': |
| 686 | msc_address = strdup(optarg); |
| 687 | break; |
| 688 | case 'l': |
| 689 | inet_aton(optarg, &local_addr); |
| 690 | break; |
| 691 | default: |
| 692 | /* ignore */ |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 698 | static void signal_handler(int signal) |
| 699 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 700 | switch (signal) { |
| 701 | case SIGABRT: |
| 702 | /* in case of abort, we want to obtain a talloc report |
| 703 | * and then return to the caller, who will abort the process */ |
| 704 | case SIGUSR1: |
| 705 | talloc_report_full(tall_bsc_ctx, stderr); |
| 706 | break; |
| 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 712 | int main(int argc, char** argv) |
| 713 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 714 | |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 715 | debug_init(); |
| 716 | stderr_target = debug_target_create_stderr(); |
| 717 | debug_add_target(stderr_target); |
| 718 | debug_set_all_filter(stderr_target, 1); |
| 719 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 720 | /* parse options */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 721 | local_addr.s_addr = INADDR_ANY; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 722 | handle_options(argc, argv); |
| 723 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 724 | nat = bsc_nat_alloc(); |
| 725 | if (!nat) { |
| 726 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 727 | return -4; |
| 728 | } |
| 729 | |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 730 | nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config); |
| 731 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 732 | /* init vty and parse */ |
| 733 | bsc_nat_vty_init(nat); |
| 734 | telnet_init(NULL, 4244); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 735 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 736 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 737 | return -3; |
| 738 | } |
| 739 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 740 | /* seed the PRNG */ |
| 741 | srand(time(NULL)); |
| 742 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 743 | /* |
| 744 | * Setup the MGCP code.. |
| 745 | */ |
| 746 | if (bsc_mgcp_init(nat) != 0) |
| 747 | return -4; |
| 748 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 749 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 750 | msc_con = bsc_msc_create(msc_address, 5000); |
| 751 | if (!msc_con) { |
| 752 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 753 | exit(1); |
| 754 | } |
| 755 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 756 | msc_con->connection_loss = msc_connection_was_lost; |
| 757 | msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 758 | msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
| 759 | bsc_msc_connect(msc_con); |
| 760 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 761 | /* wait for the BSC */ |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 762 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 763 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 764 | exit(1); |
| 765 | } |
| 766 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 767 | signal(SIGABRT, &signal_handler); |
| 768 | signal(SIGUSR1, &signal_handler); |
| 769 | signal(SIGPIPE, SIG_IGN); |
| 770 | |
| 771 | while (1) { |
| 772 | bsc_select_main(0); |
| 773 | } |
| 774 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 775 | return 0; |
| 776 | } |