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