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