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> |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 30 | #include <signal.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5aa25ae | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 33 | #include <time.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #define _GNU_SOURCE |
| 37 | #include <getopt.h> |
| 38 | |
| 39 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 40 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 42 | #include <openbsc/bssap.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 43 | #include <openbsc/ipaccess.h> |
| 44 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 45 | #include <openbsc/telnet_interface.h> |
| 46 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 47 | #include <osmocore/talloc.h> |
| 48 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 49 | #include <vty/vty.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 50 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 51 | #include <sccp/sccp.h> |
| 52 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 53 | #define SCCP_CLOSE_TIME 20 |
| 54 | #define SCCP_CLOSE_TIME_TIMEOUT 19 |
| 55 | |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 56 | struct log_target *stderr_target; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 57 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 58 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 59 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 60 | static const char *msc_ip = NULL; |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 61 | static struct timer_list sccp_close; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 62 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 63 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 64 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 65 | static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int); |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 66 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 67 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 68 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 69 | { |
| 70 | struct bsc_config *conf; |
| 71 | |
| 72 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 73 | if (conf->nr == num) |
| 74 | return conf; |
| 75 | |
| 76 | return NULL; |
| 77 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 78 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 79 | /* |
| 80 | * below are stubs we need to link |
| 81 | */ |
| 82 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
Holger Hans Peter Freyther | 2f2d342 | 2010-05-12 16:55:12 +0800 | [diff] [blame] | 83 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state, |
| 84 | struct abis_om_obj_inst *obj_ins) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 85 | { |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 90 | {} |
| 91 | |
| 92 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 93 | { |
| 94 | return -1; |
| 95 | } |
| 96 | |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 97 | static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) |
| 98 | { |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 99 | if (write_queue_enqueue(&nat->msc_con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 100 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 101 | msgb_free(msg); |
| 102 | } |
| 103 | } |
| 104 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 105 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 106 | { |
| 107 | static const u_int8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 108 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 109 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 110 | 0x00, 0x01, 0x31, |
| 111 | }; |
| 112 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 113 | 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] | 114 | } |
| 115 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 116 | static void send_ping(struct bsc_connection *bsc) |
| 117 | { |
| 118 | static const u_int8_t id_ping[] = { |
| 119 | IPAC_MSGT_PING, |
| 120 | }; |
| 121 | |
| 122 | bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS); |
| 123 | } |
| 124 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 125 | static void send_pong(struct bsc_connection *bsc) |
| 126 | { |
| 127 | static const u_int8_t id_pong[] = { |
| 128 | IPAC_MSGT_PONG, |
| 129 | }; |
| 130 | |
| 131 | bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS); |
| 132 | } |
| 133 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 134 | static void bsc_pong_timeout(void *_bsc) |
| 135 | { |
| 136 | struct bsc_connection *bsc = _bsc; |
| 137 | |
| 138 | LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr); |
| 139 | bsc_close_connection(bsc); |
| 140 | } |
| 141 | |
| 142 | static void bsc_ping_timeout(void *_bsc) |
| 143 | { |
| 144 | struct bsc_connection *bsc = _bsc; |
| 145 | |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 146 | if (bsc->nat->ping_timeout < 0) |
| 147 | return; |
| 148 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 149 | send_ping(bsc); |
| 150 | |
| 151 | /* send another ping in 20 seconds */ |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 152 | bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 153 | |
| 154 | /* also start a pong timer */ |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 155 | bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void start_ping_pong(struct bsc_connection *bsc) |
| 159 | { |
| 160 | bsc->pong_timeout.data = bsc; |
| 161 | bsc->pong_timeout.cb = bsc_pong_timeout; |
| 162 | bsc->ping_timeout.data = bsc; |
| 163 | bsc->ping_timeout.cb = bsc_ping_timeout; |
| 164 | |
| 165 | bsc_ping_timeout(bsc); |
| 166 | } |
| 167 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 168 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 169 | { |
| 170 | static const u_int8_t id_ack[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 171 | IPAC_MSGT_ID_ACK |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 172 | }; |
| 173 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 174 | 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] | 175 | } |
| 176 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 177 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 178 | { |
| 179 | static const u_int8_t id_req[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 180 | IPAC_MSGT_ID_GET, |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 181 | 0x01, IPAC_IDTAG_UNIT, |
| 182 | 0x01, IPAC_IDTAG_MACADDR, |
| 183 | 0x01, IPAC_IDTAG_LOCATION1, |
| 184 | 0x01, IPAC_IDTAG_LOCATION2, |
| 185 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 186 | 0x01, IPAC_IDTAG_SWVERSION, |
| 187 | 0x01, IPAC_IDTAG_UNITNAME, |
| 188 | 0x01, IPAC_IDTAG_SERNR, |
| 189 | }; |
| 190 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 191 | 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] | 192 | } |
| 193 | |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 194 | static void nat_send_rlsd(struct sccp_connections *conn) |
| 195 | { |
| 196 | struct sccp_connection_released *rel; |
| 197 | struct msgb *msg; |
| 198 | |
| 199 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 200 | if (!msg) { |
| 201 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 206 | rel = (struct sccp_connection_released *) msg->l2h; |
| 207 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 208 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 209 | rel->destination_local_reference = conn->remote_ref; |
| 210 | rel->source_local_reference = conn->patched_ref; |
| 211 | |
| 212 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 213 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 214 | queue_for_msc(nat->msc_con, msg); |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 217 | static void nat_send_rlc(struct sccp_source_reference *src, |
| 218 | struct sccp_source_reference *dst) |
| 219 | { |
| 220 | struct sccp_connection_release_complete *rlc; |
| 221 | struct msgb *msg; |
| 222 | |
| 223 | msg = msgb_alloc_headroom(4096, 128, "rlc"); |
| 224 | if (!msg) { |
| 225 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | msg->l2h = msgb_put(msg, sizeof(*rlc)); |
| 230 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 231 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 232 | rlc->destination_local_reference = *dst; |
| 233 | rlc->source_local_reference = *src; |
| 234 | |
| 235 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 236 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 237 | queue_for_msc(nat->msc_con, msg); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 240 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 241 | { |
| 242 | static const u_int8_t mgcp_reset[] = { |
| 243 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 244 | }; |
| 245 | |
| 246 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 249 | /* |
| 250 | * Below is the handling of messages coming |
| 251 | * from the MSC and need to be forwarded to |
| 252 | * a real BSC. |
| 253 | */ |
| 254 | static void initialize_msc_if_needed() |
| 255 | { |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 256 | if (nat->first_contact) |
| 257 | return; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 258 | |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 259 | nat->first_contact = 1; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 260 | msc_send_reset(nat->msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 263 | static void send_id_get_response() |
| 264 | { |
| 265 | struct msgb *msg = bsc_msc_id_get_resp(nat->token); |
| 266 | if (!msg) |
| 267 | return; |
| 268 | |
| 269 | ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS); |
| 270 | queue_for_msc(nat->msc_con, msg); |
| 271 | } |
| 272 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 273 | /* |
| 274 | * Currently we are lacking refcounting so we need to copy each message. |
| 275 | */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 276 | 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] | 277 | { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 278 | struct msgb *msg; |
| 279 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 280 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 281 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 282 | return; |
| 283 | } |
| 284 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 285 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 286 | if (!msg) { |
| 287 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 291 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 292 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 293 | |
| 294 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 297 | static int forward_sccp_to_bts(struct msgb *msg) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 298 | { |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 299 | struct sccp_connections *con; |
| 300 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 301 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 302 | int proto; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 303 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 304 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 305 | parsed = bsc_nat_parse(msg); |
| 306 | if (!parsed) { |
| 307 | 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] | 308 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 309 | } |
| 310 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 311 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 312 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 314 | proto = parsed->ipa_proto; |
| 315 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 316 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 317 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 318 | switch (parsed->sccp_type) { |
| 319 | case SCCP_MSG_TYPE_UDT: |
| 320 | /* forward UDT messages to every BSC */ |
| 321 | goto send_to_all; |
| 322 | break; |
| 323 | case SCCP_MSG_TYPE_RLSD: |
| 324 | case SCCP_MSG_TYPE_CREF: |
| 325 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 326 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 327 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 328 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 329 | counter_inc(nat->stats.sccp.calls); |
| 330 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 331 | if (con) { |
Holger Hans Peter Freyther | 3d0049f | 2010-04-18 01:35:41 +0800 | [diff] [blame] | 332 | counter_inc(con->bsc->cfg->stats.sccp.calls); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 333 | if (bsc_mgcp_assign(con, msg) != 0) |
| 334 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 335 | } else |
| 336 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
| 337 | } |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 338 | break; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 339 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 340 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 341 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 342 | goto exit; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 343 | break; |
| 344 | case SCCP_MSG_TYPE_RLC: |
| 345 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 346 | goto exit; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 347 | break; |
| 348 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 349 | /* MSC never opens a SCCP connection, fall through */ |
| 350 | default: |
| 351 | goto exit; |
| 352 | } |
Holger Hans Peter Freyther | f464ea5 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 353 | |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 354 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 355 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 356 | /* Exchange src/dest for the reply */ |
| 357 | nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref); |
| 358 | } else if (!con) |
Holger Hans Peter Freyther | f464ea5 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 359 | 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] | 360 | } |
| 361 | |
| 362 | talloc_free(parsed); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 363 | if (!con) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 364 | return -1; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 365 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 366 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 367 | return -1; |
| 368 | } |
| 369 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 370 | 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] | 371 | return 0; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 372 | |
| 373 | send_to_all: |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 374 | /* |
| 375 | * Filter Paging from the network. We do not want to send a PAGING |
| 376 | * Command to every BSC in our network. We will analys the PAGING |
| 377 | * message and then send it to the authenticated messages... |
| 378 | */ |
| 379 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 380 | int lac; |
| 381 | bsc = bsc_nat_find_bsc(nat, msg, &lac); |
Holger Hans Peter Freyther | 62e5843 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 382 | if (bsc && bsc->cfg->forbid_paging) |
Holger Hans Peter Freyther | a34585e | 2010-04-21 20:17:18 +0800 | [diff] [blame] | 383 | LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr); |
Holger Hans Peter Freyther | 62e5843 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 384 | else if (bsc) |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 385 | 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] | 386 | else |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 387 | LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n", |
| 388 | lac, lac); |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 389 | |
| 390 | goto exit; |
| 391 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 392 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 393 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 394 | if (!bsc->authenticated) |
| 395 | continue; |
| 396 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 397 | 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] | 398 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 399 | |
| 400 | exit: |
| 401 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 402 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 405 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 406 | { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 407 | struct bsc_connection *bsc, *tmp; |
| 408 | |
Holger Hans Peter Freyther | a4ed81c | 2010-04-12 12:35:02 +0200 | [diff] [blame] | 409 | counter_inc(nat->stats.msc.reconn); |
| 410 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 411 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 412 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 413 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 414 | |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 415 | nat->first_contact = 0; |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 416 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 417 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Holger Hans Peter Freyther | 02aee14 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 420 | 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] | 421 | { |
| 422 | static const u_int8_t reset[] = { |
Holger Hans Peter Freyther | 7cab166 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 423 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 424 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 425 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 426 | 0x01, 0x20 |
| 427 | }; |
| 428 | |
| 429 | struct msgb *msg; |
| 430 | |
| 431 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 432 | if (!msg) { |
| 433 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 438 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 439 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 440 | queue_for_msc(nat->msc_con, msg); |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 441 | |
| 442 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 443 | } |
| 444 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 445 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 446 | { |
| 447 | int error; |
| 448 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 449 | struct ipaccess_head *hh; |
| 450 | |
| 451 | if (!msg) { |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 452 | if (error == 0) |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 453 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 454 | else |
| 455 | 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] | 456 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 457 | bsc_msc_lost(nat->msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 458 | return -1; |
| 459 | } |
| 460 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 461 | 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] | 462 | |
| 463 | /* handle base message handling */ |
| 464 | hh = (struct ipaccess_head *) msg->data; |
| 465 | ipaccess_rcvmsg_base(msg, bfd); |
| 466 | |
| 467 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 468 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 469 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 470 | initialize_msc_if_needed(); |
| 471 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
| 472 | send_id_get_response(); |
| 473 | } else if (hh->proto == IPAC_PROTO_SCCP) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 474 | forward_sccp_to_bts(msg); |
| 475 | |
Holger Hans Peter Freyther | aad68b5 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 476 | msgb_free(msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 477 | return 0; |
| 478 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 479 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 480 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 481 | { |
| 482 | int rc; |
| 483 | rc = write(bfd->fd, msg->data, msg->len); |
| 484 | |
| 485 | if (rc != msg->len) { |
| 486 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | return rc; |
| 491 | } |
| 492 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 493 | /* |
| 494 | * Below is the handling of messages coming |
| 495 | * from the BSC and need to be forwarded to |
| 496 | * a real BSC. |
| 497 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * Remove the connection from the connections list, |
| 501 | * remove it from the patching of SCCP header lists |
| 502 | * as well. Maybe in the future even close connection.. |
| 503 | */ |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 504 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 505 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 506 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 507 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 508 | /* stop the timeout timer */ |
| 509 | bsc_del_timer(&connection->id_timeout); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 510 | bsc_del_timer(&connection->ping_timeout); |
| 511 | bsc_del_timer(&connection->pong_timeout); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 512 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 513 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 514 | 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] | 515 | if (sccp_patch->bsc != connection) |
| 516 | continue; |
| 517 | |
Holger Hans Peter Freyther | 4c683d1 | 2010-04-23 14:13:27 +0800 | [diff] [blame] | 518 | if (sccp_patch->has_remote_ref) |
| 519 | nat_send_rlsd(sccp_patch); |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 520 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 523 | /* close endpoints allocated by this BSC */ |
| 524 | bsc_mgcp_clear_endpoints_for(connection); |
| 525 | |
Holger Hans Peter Freyther | e464ed4 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 526 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 527 | close(connection->write_queue.bfd.fd); |
| 528 | write_queue_clear(&connection->write_queue); |
| 529 | llist_del(&connection->list_entry); |
| 530 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 531 | talloc_free(connection); |
| 532 | } |
| 533 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 534 | static void ipaccess_close_bsc(void *data) |
| 535 | { |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 536 | struct sockaddr_in sock; |
| 537 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 538 | struct bsc_connection *conn = data; |
| 539 | |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 540 | |
| 541 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 542 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 543 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 544 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 548 | { |
| 549 | struct bsc_config *conf; |
| 550 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 551 | |
Holger Hans Peter Freyther | 5cdcfa6 | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 552 | if (bsc->cfg) { |
| 553 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 554 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 555 | return; |
| 556 | } |
| 557 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 558 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 559 | if (strcmp(conf->token, token) == 0) { |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 560 | counter_inc(conf->stats.net.reconn); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 561 | bsc->authenticated = 1; |
Holger Hans Peter Freyther | 47dd494 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 562 | bsc->cfg = conf; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 563 | bsc_del_timer(&bsc->id_timeout); |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 564 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n", |
| 565 | conf->nr, conf->lac, bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 566 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | c615c26 | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 567 | return; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 568 | } |
| 569 | } |
Holger Hans Peter Freyther | c615c26 | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 570 | |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 571 | LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token, |
| 572 | bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 573 | } |
| 574 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 575 | 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] | 576 | { |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 577 | struct msgb *refuse; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 578 | struct sccp_connections *con; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 579 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 580 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 581 | /* Parse and filter messages */ |
| 582 | parsed = bsc_nat_parse(msg); |
| 583 | if (!parsed) { |
| 584 | 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] | 585 | msgb_free(msg); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 586 | return -1; |
| 587 | } |
| 588 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 589 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 590 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 591 | |
Holger Hans Peter Freyther | bbb9d39 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 592 | /* |
| 593 | * check authentication after filtering to not reject auth |
| 594 | * responses coming from the BSC. We have to make sure that |
| 595 | * nothing from the exit path will forward things to the MSC |
| 596 | */ |
| 597 | if (!bsc->authenticated) { |
| 598 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 599 | msgb_free(msg); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 604 | /* modify the SCCP entries */ |
| 605 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 606 | switch (parsed->sccp_type) { |
| 607 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 608 | if (bsc_nat_filter_sccp_cr(bsc, msg, parsed) != 0) |
| 609 | goto exit3; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 610 | if (create_sccp_src_ref(bsc, msg, parsed) != 0) |
| 611 | goto exit2; |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 612 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 613 | break; |
| 614 | case SCCP_MSG_TYPE_RLSD: |
| 615 | case SCCP_MSG_TYPE_CREF: |
| 616 | case SCCP_MSG_TYPE_DT1: |
| 617 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 618 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 619 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 620 | break; |
| 621 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 622 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | f4cfc4f | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 623 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 624 | break; |
| 625 | case SCCP_MSG_TYPE_UDT: |
| 626 | /* simply forward everything */ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 627 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 628 | break; |
| 629 | default: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 630 | 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] | 631 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 632 | goto exit2; |
| 633 | break; |
| 634 | } |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 635 | } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) { |
| 636 | bsc_mgcp_forward(bsc, msg); |
| 637 | goto exit2; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 638 | } else { |
| 639 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 640 | goto exit2; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 643 | if (con && con->bsc != bsc) { |
Holger Hans Peter Freyther | ca0c2f9 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 644 | LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n", |
| 645 | bsc->cfg->nr, con->bsc->cfg->nr); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 646 | goto exit2; |
| 647 | } |
| 648 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 649 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 650 | queue_for_msc(nat->msc_con, msg); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 651 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 652 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 653 | |
| 654 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 655 | /* if we filter out the reset send an ack to the BSC */ |
| 656 | 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] | 657 | send_reset_ack(bsc); |
| 658 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 659 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 660 | /* do we know who is handling this? */ |
| 661 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 662 | struct tlv_parsed tvp; |
| 663 | ipaccess_idtag_parse(&tvp, |
| 664 | (unsigned char *) msg->l2h + 2, |
| 665 | msgb_l2len(msg) - 2); |
| 666 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 667 | ipaccess_auth_bsc(&tvp, bsc); |
| 668 | } |
| 669 | |
| 670 | goto exit2; |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Holger Hans Peter Freyther | 1498d2e | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 673 | exit2: |
| 674 | talloc_free(parsed); |
| 675 | msgb_free(msg); |
| 676 | return -1; |
| 677 | |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 678 | exit3: |
| 679 | /* send a SCCP Connection Refused */ |
| 680 | refuse = sccp_create_refuse(parsed->src_local_ref, SCCP_REFUSAL_SCCP_FAILURE); |
| 681 | if (refuse) { |
| 682 | bsc_send_data(bsc, refuse->l2h, msgb_l2len(refuse), IPAC_PROTO_SCCP); |
| 683 | msgb_free(refuse); |
| 684 | } |
Holger Hans Peter Freyther | 1498d2e | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 685 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 686 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 687 | msgb_free(msg); |
| 688 | return -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 691 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 692 | { |
| 693 | int error; |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 694 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 695 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 696 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 697 | |
| 698 | if (!msg) { |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 699 | if (error == 0) |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 700 | LOGP(DNAT, LOGL_ERROR, |
| 701 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 702 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 703 | else |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 704 | LOGP(DNAT, LOGL_ERROR, |
| 705 | "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n", |
| 706 | bsc->cfg ? bsc->cfg->nr : -1, error); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 707 | |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 708 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 709 | return -1; |
| 710 | } |
| 711 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 712 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 713 | 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] | 714 | |
| 715 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 716 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 717 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 718 | /* stop the pong timeout */ |
| 719 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 720 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 721 | bsc_del_timer(&bsc->pong_timeout); |
| 722 | msgb_free(msg); |
| 723 | return 0; |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 724 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 725 | send_pong(bsc); |
| 726 | msgb_free(msg); |
| 727 | return 0; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 731 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 732 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 733 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 738 | static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 739 | { |
| 740 | int rc; |
| 741 | |
| 742 | rc = write(bfd->fd, msg->data, msg->len); |
| 743 | if (rc != msg->len) |
| 744 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 745 | |
| 746 | return rc; |
| 747 | } |
| 748 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 749 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 750 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 751 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 752 | int fd, rc, on; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 753 | struct sockaddr_in sa; |
| 754 | socklen_t sa_len = sizeof(sa); |
| 755 | |
| 756 | if (!(what & BSC_FD_READ)) |
| 757 | return 0; |
| 758 | |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 759 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 760 | if (fd < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 761 | perror("accept"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 762 | return fd; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 765 | /* count the reconnect */ |
| 766 | counter_inc(nat->stats.bsc.reconn); |
| 767 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 768 | /* |
| 769 | * if we are not connected to a msc... just close the socket |
| 770 | */ |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 771 | if (!nat->msc_con->is_connected) { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 772 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 773 | close(fd); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 777 | on = 1; |
Holger Hans Peter Freyther | 569ee12 | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 778 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 779 | if (rc != 0) |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 780 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 781 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 782 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 783 | /* 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] | 784 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 785 | /* |
| 786 | * |
| 787 | */ |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 788 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 789 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 790 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 791 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 792 | return -1; |
| 793 | } |
| 794 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 795 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 796 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 797 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 798 | bsc->write_queue.write_cb = ipaccess_bsc_write_cb; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 799 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 800 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 801 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 802 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 803 | talloc_free(bsc); |
| 804 | return -2; |
| 805 | } |
| 806 | |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 807 | LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n", |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 808 | fd, inet_ntoa(sa.sin_addr)); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 809 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 810 | send_id_ack(bsc); |
| 811 | send_id_req(bsc); |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 812 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 813 | |
| 814 | /* |
| 815 | * start the hangup timer |
| 816 | */ |
| 817 | bsc->id_timeout.data = bsc; |
| 818 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 819 | bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 824 | { |
| 825 | struct sockaddr_in addr; |
| 826 | int ret, on = 1; |
| 827 | |
| 828 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 829 | bfd->cb = ipaccess_listen_bsc_cb; |
| 830 | bfd->when = BSC_FD_READ; |
| 831 | |
| 832 | memset(&addr, 0, sizeof(addr)); |
| 833 | addr.sin_family = AF_INET; |
| 834 | addr.sin_port = htons(port); |
| 835 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 836 | |
| 837 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 838 | |
| 839 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 840 | if (ret < 0) { |
| 841 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 842 | strerror(errno)); |
| 843 | return -EIO; |
| 844 | } |
| 845 | |
| 846 | ret = listen(bfd->fd, 1); |
| 847 | if (ret < 0) { |
| 848 | perror("listen"); |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | ret = bsc_register_fd(bfd); |
| 853 | if (ret < 0) { |
| 854 | perror("register_listen_fd"); |
| 855 | return ret; |
| 856 | } |
| 857 | return 0; |
| 858 | } |
| 859 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 860 | static void print_usage() |
| 861 | { |
| 862 | printf("Usage: bsc_nat\n"); |
| 863 | } |
| 864 | |
| 865 | static void print_help() |
| 866 | { |
| 867 | printf(" Some useful help...\n"); |
| 868 | printf(" -h --help this text\n"); |
| 869 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 870 | printf(" -s --disable-color\n"); |
| 871 | printf(" -c --config-file filename The config file to use.\n"); |
| 872 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 873 | 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] | 874 | } |
| 875 | |
| 876 | static void handle_options(int argc, char** argv) |
| 877 | { |
| 878 | while (1) { |
| 879 | int option_index = 0, c; |
| 880 | static struct option long_options[] = { |
| 881 | {"help", 0, 0, 'h'}, |
| 882 | {"debug", 1, 0, 'd'}, |
| 883 | {"config-file", 1, 0, 'c'}, |
| 884 | {"disable-color", 0, 0, 's'}, |
| 885 | {"timestamp", 0, 0, 'T'}, |
| 886 | {"msc", 1, 0, 'm'}, |
| 887 | {"local", 1, 0, 'l'}, |
| 888 | {0, 0, 0, 0} |
| 889 | }; |
| 890 | |
| 891 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 892 | long_options, &option_index); |
| 893 | if (c == -1) |
| 894 | break; |
| 895 | |
| 896 | switch (c) { |
| 897 | case 'h': |
| 898 | print_usage(); |
| 899 | print_help(); |
| 900 | exit(0); |
| 901 | case 's': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 902 | log_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 903 | break; |
| 904 | case 'd': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 905 | log_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 906 | break; |
| 907 | case 'c': |
| 908 | config_file = strdup(optarg); |
| 909 | break; |
| 910 | case 'T': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 911 | log_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 912 | break; |
| 913 | case 'm': |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 914 | msc_ip = optarg; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 915 | break; |
| 916 | case 'l': |
| 917 | inet_aton(optarg, &local_addr); |
| 918 | break; |
| 919 | default: |
| 920 | /* ignore */ |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 926 | static void signal_handler(int signal) |
| 927 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 928 | switch (signal) { |
| 929 | case SIGABRT: |
| 930 | /* in case of abort, we want to obtain a talloc report |
| 931 | * and then return to the caller, who will abort the process */ |
| 932 | case SIGUSR1: |
| 933 | talloc_report_full(tall_bsc_ctx, stderr); |
| 934 | break; |
| 935 | default: |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 940 | static void sccp_close_unconfirmed(void *_data) |
| 941 | { |
| 942 | struct sccp_connections *conn, *tmp1; |
| 943 | struct timespec now; |
| 944 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 945 | |
| 946 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 947 | if (conn->has_remote_ref) |
| 948 | continue; |
| 949 | |
| 950 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 951 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 952 | continue; |
| 953 | |
| 954 | LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n", |
| 955 | sccp_src_ref_to_int(&conn->real_ref), |
| 956 | sccp_src_ref_to_int(&conn->patched_ref)); |
| 957 | sccp_connection_destroy(conn); |
| 958 | } |
| 959 | |
| 960 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 961 | } |
| 962 | |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 963 | extern void *tall_msgb_ctx; |
| 964 | extern void *tall_ctr_ctx; |
| 965 | static void talloc_init_ctx() |
| 966 | { |
| 967 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 968 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 969 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 970 | } |
| 971 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 972 | int main(int argc, char** argv) |
| 973 | { |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 974 | talloc_init_ctx(); |
| 975 | |
Holger Hans Peter Freyther | a1597f1 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 976 | |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 977 | log_init(&log_info); |
| 978 | stderr_target = log_target_create_stderr(); |
| 979 | log_add_target(stderr_target); |
| 980 | log_set_all_filter(stderr_target, 1); |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 981 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 982 | nat = bsc_nat_alloc(); |
| 983 | if (!nat) { |
| 984 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 985 | return -4; |
| 986 | } |
| 987 | |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 988 | nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 989 | if (!nat->mgcp_cfg) { |
| 990 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 991 | return -5; |
| 992 | } |
| 993 | |
| 994 | /* parse options */ |
| 995 | local_addr.s_addr = INADDR_ANY; |
| 996 | handle_options(argc, argv); |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 997 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 998 | /* init vty and parse */ |
| 999 | bsc_nat_vty_init(nat); |
| 1000 | telnet_init(NULL, 4244); |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1001 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1002 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1003 | return -3; |
| 1004 | } |
| 1005 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1006 | /* over rule the VTY config */ |
| 1007 | if (msc_ip) |
| 1008 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1009 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1010 | /* seed the PRNG */ |
| 1011 | srand(time(NULL)); |
| 1012 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1013 | /* |
| 1014 | * Setup the MGCP code.. |
| 1015 | */ |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1016 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1017 | return -4; |
| 1018 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1019 | /* connect to the MSC */ |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1020 | nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port); |
| 1021 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1022 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1023 | exit(1); |
| 1024 | } |
| 1025 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1026 | nat->msc_con->connection_loss = msc_connection_was_lost; |
| 1027 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1028 | nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
| 1029 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1030 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1031 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 1032 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1033 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1034 | exit(1); |
| 1035 | } |
| 1036 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1037 | signal(SIGABRT, &signal_handler); |
| 1038 | signal(SIGUSR1, &signal_handler); |
| 1039 | signal(SIGPIPE, SIG_IGN); |
| 1040 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1041 | /* recycle timer */ |
| 1042 | sccp_close.cb = sccp_close_unconfirmed; |
| 1043 | sccp_close.data = NULL; |
| 1044 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1045 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1046 | while (1) { |
| 1047 | bsc_select_main(0); |
| 1048 | } |
| 1049 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |