Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 4 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 25 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 26 | #include <arpa/inet.h> |
| 27 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 29 | #include <signal.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Holger Hans Peter Freyther | fd012d5 | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #define _GNU_SOURCE |
| 36 | #include <getopt.h> |
| 37 | |
| 38 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 39 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 40 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/ipaccess.h> |
| 43 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 44 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 45 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 46 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 47 | #include <osmocom/gsm/gsm0808.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 48 | #include <osmocom/core/talloc.h> |
| 49 | #include <osmocom/core/process.h> |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 50 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 51 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 52 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 53 | #include <osmocom/vty/telnet_interface.h> |
| 54 | #include <osmocom/vty/vty.h> |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 55 | #include <osmocom/vty/logging.h> |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 56 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 57 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 59 | #include "../../bscconfig.h" |
| 60 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 61 | #define SCCP_CLOSE_TIME 20 |
| 62 | #define SCCP_CLOSE_TIME_TIMEOUT 19 |
| 63 | |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 64 | struct log_target *stderr_target; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 65 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 66 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 67 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 68 | static const char *msc_ip = NULL; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 69 | static struct timer_list sccp_close; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 70 | static int daemonize = 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 71 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 72 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 73 | "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n" |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 74 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 75 | "This is free software: you are free to change and redistribute it.\r\n" |
| 76 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 77 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 78 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 79 | static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int); |
Holger Hans Peter Freyther | dc81393 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 80 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 81 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 82 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 83 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 84 | { |
| 85 | struct bsc_config *conf; |
| 86 | |
| 87 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 88 | if (conf->nr == num) |
| 89 | return conf; |
| 90 | |
| 91 | return NULL; |
| 92 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 754787c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 94 | static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) |
| 95 | { |
Holger Hans Peter Freyther | 1e64b32 | 2010-10-06 00:24:28 +0800 | [diff] [blame] | 96 | if (!con) { |
| 97 | LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n"); |
| 98 | msgb_free(msg); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 103 | if (write_queue_enqueue(&con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 754787c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 104 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 105 | msgb_free(msg); |
| 106 | } |
| 107 | } |
| 108 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 109 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 110 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 111 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 112 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 113 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 114 | 0x00, 0x01, 0x31, |
| 115 | }; |
| 116 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 117 | bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 120 | static void send_ping(struct bsc_connection *bsc) |
| 121 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 122 | static const uint8_t id_ping[] = { |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 123 | IPAC_MSGT_PING, |
| 124 | }; |
| 125 | |
| 126 | bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS); |
| 127 | } |
| 128 | |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 129 | static void send_pong(struct bsc_connection *bsc) |
| 130 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 131 | static const uint8_t id_pong[] = { |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 132 | IPAC_MSGT_PONG, |
| 133 | }; |
| 134 | |
| 135 | bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS); |
| 136 | } |
| 137 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 138 | static void bsc_pong_timeout(void *_bsc) |
| 139 | { |
| 140 | struct bsc_connection *bsc = _bsc; |
| 141 | |
| 142 | LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr); |
| 143 | bsc_close_connection(bsc); |
| 144 | } |
| 145 | |
| 146 | static void bsc_ping_timeout(void *_bsc) |
| 147 | { |
| 148 | struct bsc_connection *bsc = _bsc; |
| 149 | |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 150 | if (bsc->nat->ping_timeout < 0) |
| 151 | return; |
| 152 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 153 | send_ping(bsc); |
| 154 | |
| 155 | /* send another ping in 20 seconds */ |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 156 | bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 157 | |
| 158 | /* also start a pong timer */ |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 159 | bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void start_ping_pong(struct bsc_connection *bsc) |
| 163 | { |
| 164 | bsc->pong_timeout.data = bsc; |
| 165 | bsc->pong_timeout.cb = bsc_pong_timeout; |
| 166 | bsc->ping_timeout.data = bsc; |
| 167 | bsc->ping_timeout.cb = bsc_ping_timeout; |
| 168 | |
| 169 | bsc_ping_timeout(bsc); |
| 170 | } |
| 171 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 172 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 173 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 174 | static const uint8_t id_ack[] = { |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 175 | IPAC_MSGT_ID_ACK |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 178 | bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 181 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 182 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 183 | static const uint8_t id_req[] = { |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 184 | IPAC_MSGT_ID_GET, |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 185 | 0x01, IPAC_IDTAG_UNIT, |
| 186 | 0x01, IPAC_IDTAG_MACADDR, |
| 187 | 0x01, IPAC_IDTAG_LOCATION1, |
| 188 | 0x01, IPAC_IDTAG_LOCATION2, |
| 189 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 190 | 0x01, IPAC_IDTAG_SWVERSION, |
| 191 | 0x01, IPAC_IDTAG_UNITNAME, |
| 192 | 0x01, IPAC_IDTAG_SERNR, |
| 193 | }; |
| 194 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 195 | bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 198 | static struct msgb *nat_create_rlsd(struct sccp_connections *conn) |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 199 | { |
| 200 | struct sccp_connection_released *rel; |
| 201 | struct msgb *msg; |
| 202 | |
| 203 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 204 | if (!msg) { |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 205 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n"); |
| 206 | return NULL; |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 210 | rel = (struct sccp_connection_released *) msg->l2h; |
| 211 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 212 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 213 | rel->destination_local_reference = conn->remote_ref; |
| 214 | rel->source_local_reference = conn->patched_ref; |
| 215 | |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 216 | return msg; |
| 217 | } |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 218 | |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 219 | static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct sccp_connections *conn) |
| 220 | { |
| 221 | struct msgb *msg; |
| 222 | |
| 223 | if (!nat->ussd_con) |
| 224 | return; |
| 225 | |
| 226 | msg = nat_create_rlsd(conn); |
| 227 | if (!msg) |
| 228 | return; |
| 229 | |
| 230 | bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP); |
| 231 | } |
| 232 | |
| 233 | static void nat_send_rlsd_msc(struct sccp_connections *conn) |
| 234 | { |
| 235 | struct msgb *msg; |
| 236 | |
| 237 | msg = nat_create_rlsd(conn); |
| 238 | if (!msg) |
| 239 | return; |
| 240 | |
| 241 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 242 | queue_for_msc(conn->msc_con, msg); |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 245 | static void nat_send_rlsd_bsc(struct sccp_connections *conn) |
| 246 | { |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 247 | struct msgb *msg; |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 248 | struct sccp_connection_released *rel; |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 249 | |
| 250 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 251 | if (!msg) { |
| 252 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 257 | rel = (struct sccp_connection_released *) msg->l2h; |
| 258 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 259 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 260 | rel->destination_local_reference = conn->real_ref; |
| 261 | rel->source_local_reference = conn->remote_ref; |
| 262 | |
| 263 | bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP); |
| 264 | } |
| 265 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 266 | static struct msgb *nat_creat_clrc(struct sccp_connections *conn, uint8_t cause) |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 267 | { |
| 268 | struct msgb *msg; |
| 269 | struct msgb *sccp; |
| 270 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 271 | msg = gsm0808_create_clear_command(cause); |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 272 | if (!msg) { |
| 273 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 274 | return NULL; |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len); |
| 278 | if (!sccp) { |
| 279 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n"); |
| 280 | msgb_free(msg); |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 281 | return NULL; |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | msgb_free(msg); |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 285 | return sccp; |
| 286 | } |
| 287 | |
| 288 | static int nat_send_clrc_bsc(struct sccp_connections *conn) |
| 289 | { |
| 290 | struct msgb *sccp; |
| 291 | |
| 292 | sccp = nat_creat_clrc(conn, 0x20); |
| 293 | if (!sccp) |
| 294 | return -1; |
| 295 | return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 298 | static void nat_send_rlc(struct bsc_msc_connection *msc_con, |
| 299 | struct sccp_source_reference *src, |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 300 | struct sccp_source_reference *dst) |
| 301 | { |
| 302 | struct sccp_connection_release_complete *rlc; |
| 303 | struct msgb *msg; |
| 304 | |
| 305 | msg = msgb_alloc_headroom(4096, 128, "rlc"); |
| 306 | if (!msg) { |
| 307 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | msg->l2h = msgb_put(msg, sizeof(*rlc)); |
| 312 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 313 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 314 | rlc->destination_local_reference = *dst; |
| 315 | rlc->source_local_reference = *src; |
| 316 | |
| 317 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 318 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 319 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 322 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 323 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 324 | static const uint8_t mgcp_reset[] = { |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 325 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 326 | }; |
| 327 | |
| 328 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 329 | } |
| 330 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Below is the handling of messages coming |
| 333 | * from the MSC and need to be forwarded to |
| 334 | * a real BSC. |
| 335 | */ |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 336 | static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 337 | { |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 338 | if (msc_con->first_contact) |
Holger Hans Peter Freyther | dc81393 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 339 | return; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 340 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 341 | msc_con->first_contact = 1; |
| 342 | msc_send_reset(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 345 | static void send_id_get_response(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 346 | { |
| 347 | struct msgb *msg = bsc_msc_id_get_resp(nat->token); |
| 348 | if (!msg) |
| 349 | return; |
| 350 | |
| 351 | ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 352 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 353 | } |
| 354 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 355 | /* |
| 356 | * Currently we are lacking refcounting so we need to copy each message. |
| 357 | */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 358 | static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int proto) |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 359 | { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 360 | struct msgb *msg; |
| 361 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 362 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 363 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 364 | return; |
| 365 | } |
| 366 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 367 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 368 | if (!msg) { |
| 369 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 370 | return; |
| 371 | } |
| 372 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 373 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 374 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 375 | |
| 376 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 379 | /* |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 380 | * Update the release statistics |
| 381 | */ |
| 382 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal) |
| 383 | { |
| 384 | if (!bsc->cfg) { |
| 385 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated."); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (filter >= 0) { |
| 390 | LOGP(DNAT, LOGL_ERROR, "Connection was not rejected"); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | if (filter == -1) |
| 395 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]); |
| 396 | else if (normal) |
| 397 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]); |
| 398 | else |
| 399 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]); |
| 400 | } |
| 401 | |
| 402 | /* |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 403 | * Release an established connection. We will have to release it to the BSC |
| 404 | * and to the network and we do it the following way. |
| 405 | * 1.) Give up on the MSC side |
| 406 | * 1.1) Send a RLSD message, it is a bit non standard but should work, we |
| 407 | * ignore the RLC... we might complain about it. Other options would |
| 408 | * be to send a Release Request, handle the Release Complete.. |
| 409 | * 1.2) Mark the data structure to be con_local and wait for 2nd |
| 410 | * |
| 411 | * 2.) Give up on the BSC side |
| 412 | * 2.1) Depending on the con type reject the service, or just close it |
| 413 | */ |
| 414 | static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con) |
| 415 | { |
| 416 | struct msgb *rlsd; |
| 417 | /* 1. release the network */ |
| 418 | rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref, |
| 419 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 420 | if (!rlsd) |
| 421 | LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n"); |
| 422 | else { |
| 423 | ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP); |
| 424 | queue_for_msc(con->msc_con, rlsd); |
| 425 | } |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 426 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | b2b66c2 | 2010-09-16 06:41:09 +0800 | [diff] [blame] | 427 | con->msc_con = NULL; |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 428 | |
| 429 | /* 2. release the BSC side */ |
| 430 | if (con->con_type == NAT_CON_TYPE_LU) { |
| 431 | struct msgb *payload, *udt; |
| 432 | payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
| 433 | |
| 434 | if (payload) { |
| 435 | gsm0808_prepend_dtap_header(payload, 0); |
| 436 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 437 | if (udt) |
| 438 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 439 | else |
| 440 | LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n"); |
| 441 | |
| 442 | msgb_free(payload); |
| 443 | } else { |
| 444 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n"); |
| 445 | } |
| 446 | } |
| 447 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 448 | nat_send_clrc_bsc(con); |
| 449 | |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 450 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 451 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 452 | if (!rlsd) { |
| 453 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n"); |
| 454 | sccp_connection_destroy(con); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
| 459 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 460 | } |
| 461 | |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 462 | static void bsc_send_con_refuse(struct bsc_connection *bsc, |
| 463 | struct bsc_nat_parsed *parsed, int con_type) |
| 464 | { |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 465 | struct msgb *payload; |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 466 | struct msgb *refuse; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 467 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 468 | if (con_type == NAT_CON_TYPE_LU) |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 469 | payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 470 | else if (con_type == NAT_CON_TYPE_CM_SERV_REQ) |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 471 | payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
Holger Hans Peter Freyther | a624c53 | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 472 | else { |
| 473 | LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); |
| 474 | payload = NULL; |
| 475 | } |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Some BSCs do not handle the payload inside a SCCP CREF msg |
| 479 | * so we will need to: |
| 480 | * 1.) Allocate a local connection and mark it as local.. |
| 481 | * 2.) queue data for downstream.. and the RLC should delete everything |
| 482 | */ |
| 483 | if (payload) { |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 484 | struct msgb *cc, *udt, *clear, *rlsd; |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 485 | struct sccp_connections *con; |
| 486 | con = create_sccp_src_ref(bsc, parsed); |
| 487 | if (!con) |
| 488 | goto send_refuse; |
| 489 | |
| 490 | /* declare it local and assign a unique remote_ref */ |
| 491 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 492 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 493 | con->has_remote_ref = 1; |
| 494 | con->remote_ref = con->patched_ref; |
| 495 | |
| 496 | /* 1. create a confirmation */ |
| 497 | cc = sccp_create_cc(&con->remote_ref, &con->real_ref); |
| 498 | if (!cc) |
| 499 | goto send_refuse; |
| 500 | |
| 501 | /* 2. create the DT1 */ |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 502 | gsm0808_prepend_dtap_header(payload, 0); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 503 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 504 | if (!udt) { |
| 505 | msgb_free(cc); |
| 506 | goto send_refuse; |
| 507 | } |
| 508 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 509 | /* 3. send a Clear Command */ |
| 510 | clear = nat_creat_clrc(con, 0x20); |
| 511 | if (!clear) { |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 512 | msgb_free(cc); |
| 513 | msgb_free(udt); |
| 514 | goto send_refuse; |
| 515 | } |
| 516 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 517 | /* 4. send a RLSD */ |
| 518 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 519 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 520 | if (!rlsd) { |
| 521 | msgb_free(cc); |
| 522 | msgb_free(udt); |
| 523 | msgb_free(clear); |
| 524 | goto send_refuse; |
| 525 | } |
| 526 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 527 | bsc_write(bsc, cc, IPAC_PROTO_SCCP); |
| 528 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 529 | bsc_write(bsc, clear, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 530 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 531 | msgb_free(payload); |
| 532 | return; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 533 | } |
| 534 | |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 535 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 536 | send_refuse: |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 537 | if (payload) |
| 538 | msgb_free(payload); |
| 539 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 540 | refuse = sccp_create_refuse(parsed->src_local_ref, |
| 541 | SCCP_REFUSAL_SCCP_FAILURE, NULL, 0); |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 542 | if (!refuse) { |
| 543 | LOGP(DNAT, LOGL_ERROR, |
| 544 | "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n", |
| 545 | sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | bsc_write(bsc, refuse, IPAC_PROTO_SCCP); |
| 550 | } |
| 551 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 552 | static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg) |
| 553 | { |
| 554 | if (bsc->cfg->forbid_paging) { |
| 555 | LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP); |
| 560 | } |
| 561 | |
| 562 | static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg) |
| 563 | { |
| 564 | struct bsc_connection *bsc; |
| 565 | const uint8_t *paging_start; |
Holger Hans Peter Freyther | f77bca7 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 566 | int paging_length, i, ret; |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 567 | |
Holger Hans Peter Freyther | f77bca7 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 568 | ret = bsc_nat_find_paging(msg, &paging_start, &paging_length); |
| 569 | if (ret != 0) { |
| 570 | LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| 574 | /* This is quite expensive now */ |
| 575 | for (i = 0; i < paging_length; i += 2) { |
| 576 | unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]); |
| 577 | unsigned int paged = 0; |
| 578 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 579 | if (!bsc->cfg) |
| 580 | continue; |
| 581 | if (!bsc->authenticated) |
| 582 | continue; |
| 583 | if (!bsc_config_handles_lac(bsc->cfg, _lac)) |
| 584 | continue; |
| 585 | bsc_nat_send_paging(bsc, msg); |
| 586 | paged += 1; |
| 587 | } |
| 588 | |
| 589 | /* highlight a possible config issue */ |
| 590 | if (paged == 0) |
| 591 | LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac); |
| 592 | |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 597 | /* |
| 598 | * Update the auth status. This can be either a CIPHER MODE COMAMND or |
| 599 | * a CM Serivce Accept. Maybe also LU Accept or such in the future. |
| 600 | */ |
| 601 | static void update_con_authorize(struct sccp_connections *con, |
| 602 | struct bsc_nat_parsed *parsed, |
| 603 | struct msgb *msg) |
| 604 | { |
| 605 | if (!con) |
| 606 | return; |
| 607 | if (con->authorized) |
| 608 | return; |
| 609 | |
| 610 | if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT && |
| 611 | parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) { |
| 612 | con->authorized = 1; |
| 613 | } else if (parsed->bssap == BSSAP_MSG_DTAP) { |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 614 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 615 | uint32_t len; |
| 616 | struct gsm48_hdr *hdr48; |
| 617 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 618 | if (!hdr48) |
| 619 | return; |
| 620 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 621 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 622 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 623 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 624 | msg_type == GSM48_MT_MM_CM_SERV_ACC) |
| 625 | con->authorized = 1; |
| 626 | } |
| 627 | } |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 628 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 629 | static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 630 | { |
Holger Hans Peter Freyther | a624c53 | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 631 | struct sccp_connections *con = NULL; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 632 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 633 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 634 | int proto; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 635 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 636 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 637 | parsed = bsc_nat_parse(msg); |
| 638 | if (!parsed) { |
| 639 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 640 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 641 | } |
| 642 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 643 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 644 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 645 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 646 | proto = parsed->ipa_proto; |
| 647 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 648 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 649 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 650 | switch (parsed->sccp_type) { |
| 651 | case SCCP_MSG_TYPE_UDT: |
| 652 | /* forward UDT messages to every BSC */ |
| 653 | goto send_to_all; |
| 654 | break; |
| 655 | case SCCP_MSG_TYPE_RLSD: |
Holger Hans Peter Freyther | 1b71492 | 2011-04-21 17:13:51 +0200 | [diff] [blame] | 656 | if (con && con->con_local == NAT_CON_END_USSD) { |
| 657 | LOGP(DNAT, LOGL_NOTICE, "RLSD for a USSD connection. Ignoring.\n"); |
| 658 | con = NULL; |
| 659 | } |
| 660 | /* fall through */ |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 661 | case SCCP_MSG_TYPE_CREF: |
| 662 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 663 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 664 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 665 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 666 | counter_inc(nat->stats.sccp.calls); |
| 667 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 668 | if (con) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 669 | struct rate_ctr_group *ctrg; |
| 670 | ctrg = con->bsc->cfg->stats.ctrg; |
| 671 | rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]); |
Holger Hans Peter Freyther | 926c5fd | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 672 | if (bsc_mgcp_assign_patch(con, msg) != 0) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 673 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 674 | } else |
| 675 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
Holger Hans Peter Freyther | 1b71492 | 2011-04-21 17:13:51 +0200 | [diff] [blame] | 676 | } else if (con && con->con_local == NAT_CON_END_USSD && |
| 677 | parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) { |
| 678 | LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n"); |
| 679 | con = NULL; |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 680 | } |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 681 | break; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 682 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 683 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 684 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 685 | goto exit; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 686 | break; |
| 687 | case SCCP_MSG_TYPE_RLC: |
| 688 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 689 | goto exit; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 690 | break; |
| 691 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 692 | /* MSC never opens a SCCP connection, fall through */ |
| 693 | default: |
| 694 | goto exit; |
| 695 | } |
Holger Hans Peter Freyther | 8d29828 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 696 | |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 697 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 698 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 699 | /* Exchange src/dest for the reply */ |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 700 | nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 701 | } else if (!con) |
Holger Hans Peter Freyther | 117e7a7 | 2010-05-15 19:13:52 +0800 | [diff] [blame] | 702 | LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x from the MSC.\n", parsed->sccp_type); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | talloc_free(parsed); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 706 | if (!con) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 707 | return -1; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 708 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 709 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 713 | update_con_authorize(con, parsed, msg); |
| 714 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 715 | bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 716 | return 0; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 717 | |
| 718 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 719 | /* |
| 720 | * Filter Paging from the network. We do not want to send a PAGING |
| 721 | * Command to every BSC in our network. We will analys the PAGING |
| 722 | * message and then send it to the authenticated messages... |
| 723 | */ |
| 724 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 725 | bsc_nat_handle_paging(nat, msg); |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 726 | goto exit; |
| 727 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 728 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 729 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 730 | if (!bsc->authenticated) |
| 731 | continue; |
| 732 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 733 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 734 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 735 | |
| 736 | exit: |
| 737 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 738 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 741 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 742 | { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 743 | struct bsc_connection *bsc, *tmp; |
| 744 | |
| 745 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 746 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 747 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 748 | |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 749 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 750 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 751 | } |
| 752 | |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 753 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 754 | { |
| 755 | counter_inc(nat->stats.msc.reconn); |
| 756 | } |
| 757 | |
Holger Hans Peter Freyther | 8f515a0 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 758 | static void msc_send_reset(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 759 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 760 | static const uint8_t reset[] = { |
Holger Hans Peter Freyther | c48a5d9 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 761 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 762 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 763 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 764 | 0x01, 0x20 |
| 765 | }; |
| 766 | |
| 767 | struct msgb *msg; |
| 768 | |
| 769 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 770 | if (!msg) { |
| 771 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 776 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 777 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 778 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 779 | |
| 780 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 781 | } |
| 782 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 783 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 784 | { |
| 785 | int error; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 786 | struct bsc_msc_connection *msc_con; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 787 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 788 | struct ipaccess_head *hh; |
| 789 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 790 | msc_con = (struct bsc_msc_connection *) bfd->data; |
| 791 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 792 | if (!msg) { |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 793 | if (error == 0) |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 794 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 795 | else |
| 796 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 797 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 798 | bsc_msc_lost(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 799 | return -1; |
| 800 | } |
| 801 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 802 | LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 803 | |
| 804 | /* handle base message handling */ |
| 805 | hh = (struct ipaccess_head *) msg->data; |
| 806 | ipaccess_rcvmsg_base(msg, bfd); |
| 807 | |
| 808 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 809 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 810 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 811 | initialize_msc_if_needed(msc_con); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 812 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 813 | send_id_get_response(msc_con); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 814 | } else if (hh->proto == IPAC_PROTO_SCCP) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 815 | forward_sccp_to_bts(msc_con, msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 816 | |
Holger Hans Peter Freyther | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 817 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 818 | return 0; |
| 819 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 820 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 821 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 822 | { |
| 823 | int rc; |
| 824 | rc = write(bfd->fd, msg->data, msg->len); |
| 825 | |
| 826 | if (rc != msg->len) { |
| 827 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | return rc; |
| 832 | } |
| 833 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 834 | /* |
| 835 | * Below is the handling of messages coming |
| 836 | * from the BSC and need to be forwarded to |
| 837 | * a real BSC. |
| 838 | */ |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * Remove the connection from the connections list, |
| 842 | * remove it from the patching of SCCP header lists |
| 843 | * as well. Maybe in the future even close connection.. |
| 844 | */ |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 845 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 846 | { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 847 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 848 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 849 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 850 | /* stop the timeout timer */ |
| 851 | bsc_del_timer(&connection->id_timeout); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 852 | bsc_del_timer(&connection->ping_timeout); |
| 853 | bsc_del_timer(&connection->pong_timeout); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 854 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 855 | if (connection->cfg) |
| 856 | ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP]; |
| 857 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 858 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 859 | llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 860 | if (sccp_patch->bsc != connection) |
| 861 | continue; |
| 862 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 863 | if (ctr) |
| 864 | rate_ctr_inc(ctr); |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 865 | if (sccp_patch->has_remote_ref) { |
| 866 | if (sccp_patch->con_local == NAT_CON_END_MSC) |
| 867 | nat_send_rlsd_msc(sccp_patch); |
| 868 | else if (sccp_patch->con_local == NAT_CON_END_USSD) |
| 869 | nat_send_rlsd_ussd(nat, sccp_patch); |
| 870 | } |
| 871 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 872 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Holger Hans Peter Freyther | c0ff1d0 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 875 | /* close endpoints allocated by this BSC */ |
| 876 | bsc_mgcp_clear_endpoints_for(connection); |
| 877 | |
Holger Hans Peter Freyther | 53cd985 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 878 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 879 | close(connection->write_queue.bfd.fd); |
| 880 | write_queue_clear(&connection->write_queue); |
| 881 | llist_del(&connection->list_entry); |
| 882 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 883 | talloc_free(connection); |
| 884 | } |
| 885 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 886 | static void bsc_maybe_close(struct bsc_connection *bsc) |
| 887 | { |
| 888 | struct sccp_connections *sccp; |
| 889 | if (!bsc->nat->blocked) |
| 890 | return; |
| 891 | |
| 892 | /* are there any connections left */ |
| 893 | llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry) |
| 894 | if (sccp->bsc == bsc) |
| 895 | return; |
| 896 | |
| 897 | /* nothing left, close the BSC */ |
| 898 | LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n", |
| 899 | bsc->cfg ? bsc->cfg->nr : -1); |
| 900 | bsc_close_connection(bsc); |
| 901 | } |
| 902 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 903 | static void ipaccess_close_bsc(void *data) |
| 904 | { |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 905 | struct sockaddr_in sock; |
| 906 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 907 | struct bsc_connection *conn = data; |
| 908 | |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 909 | |
| 910 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 911 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 912 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 913 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 917 | { |
| 918 | struct bsc_config *conf; |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 919 | const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | 4884096 | 2010-10-14 17:19:58 +0200 | [diff] [blame] | 920 | const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 921 | |
Holger Hans Peter Freyther | 4dc44ea | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 922 | if (bsc->cfg) { |
| 923 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 924 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 925 | return; |
| 926 | } |
| 927 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 928 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
Holger Hans Peter Freyther | 4884096 | 2010-10-14 17:19:58 +0200 | [diff] [blame] | 929 | if (strncmp(conf->token, token, len) == 0) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 930 | rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 931 | bsc->authenticated = 1; |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 932 | bsc->cfg = conf; |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 933 | bsc_del_timer(&bsc->id_timeout); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 934 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n", |
| 935 | conf->nr, bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 936 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 937 | return; |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 938 | } |
| 939 | } |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 940 | |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 941 | LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token, |
| 942 | bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 943 | } |
| 944 | |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 945 | static void handle_con_stats(struct sccp_connections *con) |
| 946 | { |
| 947 | struct rate_ctr_group *ctrg; |
| 948 | int id = bsc_conn_type_to_ctr(con); |
| 949 | |
| 950 | if (id == -1) |
| 951 | return; |
| 952 | |
| 953 | if (!con->bsc || !con->bsc->cfg) |
| 954 | return; |
| 955 | |
| 956 | ctrg = con->bsc->cfg->stats.ctrg; |
| 957 | rate_ctr_inc(&ctrg->ctr[id]); |
| 958 | } |
| 959 | |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 960 | static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 961 | { |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 962 | int con_filter = 0; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 963 | char *imsi = NULL; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 964 | struct bsc_msc_connection *con_msc = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 965 | struct bsc_connection *con_bsc = NULL; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 966 | int con_type; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 967 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 968 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 969 | /* Parse and filter messages */ |
| 970 | parsed = bsc_nat_parse(msg); |
| 971 | if (!parsed) { |
| 972 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 45f6295 | 2010-03-30 07:35:46 +0200 | [diff] [blame] | 973 | msgb_free(msg); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 974 | return -1; |
| 975 | } |
| 976 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 977 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 978 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 979 | |
Holger Hans Peter Freyther | 5a723b6 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 980 | /* |
| 981 | * check authentication after filtering to not reject auth |
| 982 | * responses coming from the BSC. We have to make sure that |
| 983 | * nothing from the exit path will forward things to the MSC |
| 984 | */ |
| 985 | if (!bsc->authenticated) { |
| 986 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 987 | msgb_free(msg); |
| 988 | return -1; |
| 989 | } |
| 990 | |
| 991 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 992 | /* modify the SCCP entries */ |
| 993 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 8582bb9 | 2010-09-15 00:38:54 +0800 | [diff] [blame] | 994 | int filter; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 995 | struct sccp_connections *con; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 996 | switch (parsed->sccp_type) { |
| 997 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 998 | filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 999 | if (filter < 0) { |
| 1000 | bsc_stat_reject(filter, bsc, 0); |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1001 | goto exit3; |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 1004 | if (!create_sccp_src_ref(bsc, parsed)) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1005 | goto exit2; |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1006 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 6200fd9 | 2010-07-05 13:33:18 +0800 | [diff] [blame] | 1007 | con->msc_con = bsc->nat->msc_con; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1008 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 1009 | con->con_type = con_type; |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 1010 | con->imsi_checked = filter; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1011 | if (imsi) |
| 1012 | con->imsi = talloc_steal(con, imsi); |
| 1013 | imsi = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1014 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 1015 | handle_con_stats(con); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1016 | break; |
| 1017 | case SCCP_MSG_TYPE_RLSD: |
| 1018 | case SCCP_MSG_TYPE_CREF: |
| 1019 | case SCCP_MSG_TYPE_DT1: |
| 1020 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 1021 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1022 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1023 | if (con) { |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1024 | /* only filter non local connections */ |
| 1025 | if (!con->con_local) { |
| 1026 | filter = bsc_nat_filter_dt(bsc, msg, con, parsed); |
| 1027 | if (filter < 0) { |
| 1028 | bsc_stat_reject(filter, bsc, 1); |
| 1029 | bsc_send_con_release(bsc, con); |
| 1030 | con = NULL; |
| 1031 | goto exit2; |
| 1032 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1033 | |
| 1034 | /* hand data to a side channel */ |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 1035 | if (bsc_check_ussd(con, parsed, msg) == 1) |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1036 | con->con_local = NAT_CON_END_USSD; |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* |
| 1039 | * Optionally rewrite setup message. This can |
| 1040 | * replace the msg and the parsed structure becomes |
| 1041 | * invalid. |
| 1042 | */ |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1043 | msg = bsc_nat_rewrite_setup(bsc->nat, msg, parsed, con->imsi); |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1044 | talloc_free(parsed); |
| 1045 | parsed = NULL; |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 1046 | } else if (con->con_local == NAT_CON_END_USSD) { |
| 1047 | bsc_check_ussd(con, parsed, msg); |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 1048 | } |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1049 | |
| 1050 | con_bsc = con->bsc; |
| 1051 | con_msc = con->msc_con; |
| 1052 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1053 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1054 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1055 | break; |
| 1056 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1057 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1058 | if (con) { |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1059 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1060 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 1061 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1062 | } |
Holger Hans Peter Freyther | 38712d0 | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 1063 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1064 | bsc_maybe_close(bsc); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1065 | break; |
| 1066 | case SCCP_MSG_TYPE_UDT: |
| 1067 | /* simply forward everything */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1068 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1069 | break; |
| 1070 | default: |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 1071 | LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1072 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1073 | goto exit2; |
| 1074 | break; |
| 1075 | } |
Holger Hans Peter Freyther | a63d35c | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 1076 | } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) { |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 1077 | bsc_mgcp_forward(bsc, msg); |
| 1078 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1079 | } else { |
| 1080 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 1081 | goto exit2; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1084 | if (con_msc && con_bsc != bsc) { |
Holger Hans Peter Freyther | 4107f98 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 1085 | LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n", |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1086 | bsc->cfg->nr, con_bsc->cfg->nr); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1087 | goto exit2; |
| 1088 | } |
| 1089 | |
Holger Hans Peter Freyther | 79a6baf | 2010-09-15 17:39:44 +0800 | [diff] [blame] | 1090 | /* do not forward messages to the MSC */ |
| 1091 | if (con_filter) |
| 1092 | goto exit2; |
| 1093 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1094 | if (!con_msc) { |
Holger Hans Peter Freyther | 43bd9d1 | 2010-09-15 00:14:48 +0800 | [diff] [blame] | 1095 | LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n", |
| 1096 | bsc->cfg->nr, |
| 1097 | parsed ? parsed->ipa_proto : -1, |
| 1098 | parsed ? parsed->sccp_type : -1); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1099 | goto exit2; |
| 1100 | } |
| 1101 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1102 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1103 | queue_for_msc(con_msc, msg); |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1104 | if (parsed) |
| 1105 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1106 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1107 | |
| 1108 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1109 | /* if we filter out the reset send an ack to the BSC */ |
| 1110 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1111 | send_reset_ack(bsc); |
| 1112 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1113 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 1114 | /* do we know who is handling this? */ |
| 1115 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 1116 | struct tlv_parsed tvp; |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1117 | int ret; |
| 1118 | ret = ipaccess_idtag_parse(&tvp, |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1119 | (unsigned char *) msg->l2h + 2, |
| 1120 | msgb_l2len(msg) - 2); |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1121 | if (ret < 0) { |
| 1122 | LOGP(DNAT, LOGL_ERROR, "ignoring IPA response " |
| 1123 | "message with malformed TLVs\n"); |
| 1124 | return ret; |
| 1125 | } |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1126 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 1127 | ipaccess_auth_bsc(&tvp, bsc); |
| 1128 | } |
| 1129 | |
| 1130 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1133 | exit2: |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1134 | if (imsi) |
| 1135 | talloc_free(imsi); |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1136 | talloc_free(parsed); |
| 1137 | msgb_free(msg); |
| 1138 | return -1; |
| 1139 | |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1140 | exit3: |
| 1141 | /* send a SCCP Connection Refused */ |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1142 | if (imsi) |
| 1143 | talloc_free(imsi); |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 1144 | bsc_send_con_refuse(bsc, parsed, con_type); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1145 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1146 | msgb_free(msg); |
| 1147 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1150 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1151 | { |
| 1152 | int error; |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1153 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1154 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1155 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1156 | |
| 1157 | if (!msg) { |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1158 | if (error == 0) |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1159 | LOGP(DNAT, LOGL_ERROR, |
| 1160 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 1161 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1162 | else |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1163 | LOGP(DNAT, LOGL_ERROR, |
| 1164 | "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n", |
| 1165 | bsc->cfg ? bsc->cfg->nr : -1, error); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1166 | |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 1167 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1168 | return -1; |
| 1169 | } |
| 1170 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1171 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1172 | LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1173 | |
| 1174 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1175 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1176 | |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1177 | /* stop the pong timeout */ |
| 1178 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 1179 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1180 | bsc_del_timer(&bsc->pong_timeout); |
| 1181 | msgb_free(msg); |
| 1182 | return 0; |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1183 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 1184 | send_pong(bsc); |
| 1185 | msgb_free(msg); |
| 1186 | return 0; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1190 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 1191 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1192 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1197 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 1198 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1199 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1200 | int fd, rc, on; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1201 | struct sockaddr_in sa; |
| 1202 | socklen_t sa_len = sizeof(sa); |
| 1203 | |
| 1204 | if (!(what & BSC_FD_READ)) |
| 1205 | return 0; |
| 1206 | |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1207 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 1208 | if (fd < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1209 | perror("accept"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1210 | return fd; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1213 | /* count the reconnect */ |
| 1214 | counter_inc(nat->stats.bsc.reconn); |
| 1215 | |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1216 | /* |
| 1217 | * if we are not connected to a msc... just close the socket |
| 1218 | */ |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 1219 | if (!bsc_nat_msc_is_connected(nat)) { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1220 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1221 | close(fd); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1225 | if (nat->blocked) { |
| 1226 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n"); |
| 1227 | close(fd); |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1231 | on = 1; |
Holger Hans Peter Freyther | d58afeb | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 1232 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1233 | if (rc != 0) |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1234 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 1235 | |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1236 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | f0374a5 | 2010-07-27 19:21:53 +0800 | [diff] [blame] | 1237 | &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp)); |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1238 | if (rc != 0) |
| 1239 | LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 1240 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1241 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 1242 | /* todo... use GNUtls to see if we want to trust this as a BTS */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1243 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1244 | /* |
| 1245 | * |
| 1246 | */ |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1247 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1248 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1249 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1250 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1251 | return -1; |
| 1252 | } |
| 1253 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1254 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1255 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1256 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 1257 | bsc->write_queue.write_cb = bsc_write_cb; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1258 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 1259 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1260 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1261 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1262 | talloc_free(bsc); |
| 1263 | return -2; |
| 1264 | } |
| 1265 | |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 1266 | LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n", |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1267 | fd, inet_ntoa(sa.sin_addr)); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1268 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 1269 | send_id_ack(bsc); |
| 1270 | send_id_req(bsc); |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 1271 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1272 | |
| 1273 | /* |
| 1274 | * start the hangup timer |
| 1275 | */ |
| 1276 | bsc->id_timeout.data = bsc; |
| 1277 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 1278 | bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1279 | return 0; |
| 1280 | } |
| 1281 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1282 | static void print_usage() |
| 1283 | { |
| 1284 | printf("Usage: bsc_nat\n"); |
| 1285 | } |
| 1286 | |
| 1287 | static void print_help() |
| 1288 | { |
| 1289 | printf(" Some useful help...\n"); |
| 1290 | printf(" -h --help this text\n"); |
| 1291 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1292 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1293 | printf(" -s --disable-color\n"); |
| 1294 | printf(" -c --config-file filename The config file to use.\n"); |
| 1295 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1296 | printf(" -l --local=IP. The local address of this BSC.\n"); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1299 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1300 | { |
| 1301 | while (1) { |
| 1302 | int option_index = 0, c; |
| 1303 | static struct option long_options[] = { |
| 1304 | {"help", 0, 0, 'h'}, |
| 1305 | {"debug", 1, 0, 'd'}, |
| 1306 | {"config-file", 1, 0, 'c'}, |
| 1307 | {"disable-color", 0, 0, 's'}, |
| 1308 | {"timestamp", 0, 0, 'T'}, |
| 1309 | {"msc", 1, 0, 'm'}, |
| 1310 | {"local", 1, 0, 'l'}, |
| 1311 | {0, 0, 0, 0} |
| 1312 | }; |
| 1313 | |
| 1314 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 1315 | long_options, &option_index); |
| 1316 | if (c == -1) |
| 1317 | break; |
| 1318 | |
| 1319 | switch (c) { |
| 1320 | case 'h': |
| 1321 | print_usage(); |
| 1322 | print_help(); |
| 1323 | exit(0); |
| 1324 | case 's': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1325 | log_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1326 | break; |
| 1327 | case 'd': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1328 | log_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1329 | break; |
| 1330 | case 'c': |
| 1331 | config_file = strdup(optarg); |
| 1332 | break; |
| 1333 | case 'T': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1334 | log_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1335 | break; |
| 1336 | case 'm': |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1337 | msc_ip = optarg; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1338 | break; |
| 1339 | case 'l': |
| 1340 | inet_aton(optarg, &local_addr); |
| 1341 | break; |
| 1342 | default: |
| 1343 | /* ignore */ |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1349 | static void signal_handler(int signal) |
| 1350 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1351 | switch (signal) { |
| 1352 | case SIGABRT: |
| 1353 | /* in case of abort, we want to obtain a talloc report |
| 1354 | * and then return to the caller, who will abort the process */ |
| 1355 | case SIGUSR1: |
| 1356 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1357 | break; |
| 1358 | default: |
| 1359 | break; |
| 1360 | } |
| 1361 | } |
| 1362 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1363 | static void sccp_close_unconfirmed(void *_data) |
| 1364 | { |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1365 | int destroyed = 0; |
| 1366 | struct bsc_connection *bsc, *bsc_tmp; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1367 | struct sccp_connections *conn, *tmp1; |
| 1368 | struct timespec now; |
| 1369 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 1370 | |
| 1371 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 1372 | if (conn->has_remote_ref) |
| 1373 | continue; |
| 1374 | |
| 1375 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 1376 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 1377 | continue; |
| 1378 | |
| 1379 | LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n", |
| 1380 | sccp_src_ref_to_int(&conn->real_ref), |
| 1381 | sccp_src_ref_to_int(&conn->patched_ref)); |
| 1382 | sccp_connection_destroy(conn); |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1383 | destroyed = 1; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1384 | } |
| 1385 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1386 | if (!destroyed) |
| 1387 | goto out; |
| 1388 | |
| 1389 | /* now close out any BSC */ |
| 1390 | llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry) |
| 1391 | bsc_maybe_close(bsc); |
| 1392 | |
| 1393 | out: |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1394 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1395 | } |
| 1396 | |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1397 | extern void *tall_msgb_ctx; |
| 1398 | extern void *tall_ctr_ctx; |
| 1399 | static void talloc_init_ctx() |
| 1400 | { |
| 1401 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 1402 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1403 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 1404 | } |
| 1405 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1406 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 1407 | |
| 1408 | static struct vty_app_info vty_info = { |
Harald Welte | 9af42c3 | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 1409 | .name = "OsmoBSCNAT", |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1410 | .version = PACKAGE_VERSION, |
| 1411 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 1412 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1413 | }; |
| 1414 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1415 | int main(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1416 | { |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1417 | int rc; |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1418 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1419 | talloc_init_ctx(); |
Holger Hans Peter Freyther | 0937138 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 1420 | |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1421 | log_init(&log_info); |
| 1422 | stderr_target = log_target_create_stderr(); |
| 1423 | log_add_target(stderr_target); |
| 1424 | log_set_all_filter(stderr_target, 1); |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 1425 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1426 | nat = bsc_nat_alloc(); |
| 1427 | if (!nat) { |
| 1428 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 1429 | return -4; |
| 1430 | } |
| 1431 | |
Holger Hans Peter Freyther | 774d1d3 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1432 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1433 | if (!nat->mgcp_cfg) { |
| 1434 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 1435 | return -5; |
| 1436 | } |
| 1437 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1438 | vty_info.copyright = openbsc_copyright; |
| 1439 | vty_init(&vty_info); |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 1440 | logging_vty_add_cmds(&log_info); |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1441 | bsc_nat_vty_init(nat); |
| 1442 | |
| 1443 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1444 | /* parse options */ |
| 1445 | local_addr.s_addr = INADDR_ANY; |
| 1446 | handle_options(argc, argv); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1447 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1448 | rate_ctr_init(tall_bsc_ctx); |
| 1449 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1450 | /* init vty and parse */ |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1451 | telnet_init(tall_bsc_ctx, NULL, 4244); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1452 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1453 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1454 | return -3; |
| 1455 | } |
| 1456 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1457 | /* over rule the VTY config */ |
| 1458 | if (msc_ip) |
| 1459 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1460 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1461 | /* seed the PRNG */ |
| 1462 | srand(time(NULL)); |
| 1463 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1464 | /* |
| 1465 | * Setup the MGCP code.. |
| 1466 | */ |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1467 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1468 | return -4; |
| 1469 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1470 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 1471 | nat->msc_con = bsc_msc_create(nat, &nat->dests); |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1472 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1473 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1474 | exit(1); |
| 1475 | } |
| 1476 | |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1477 | nat->msc_con->connection_loss = msc_connection_was_lost; |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 1478 | nat->msc_con->connected = msc_connection_connected; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1479 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1480 | nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 1481 | nat->msc_con->write_queue.bfd.data = nat->msc_con; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1482 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1483 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1484 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 5d0a027 | 2010-10-19 17:48:13 +0200 | [diff] [blame] | 1485 | rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr), |
Holger Hans Peter Freyther | 60de20c | 2011-04-07 22:14:58 +0200 | [diff] [blame] | 1486 | 5000, 0, ipaccess_listen_bsc_cb, nat); |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 1487 | if (rc != 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1488 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1489 | exit(1); |
| 1490 | } |
| 1491 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 1492 | rc = bsc_ussd_init(nat); |
| 1493 | if (rc != 0) { |
| 1494 | LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n"); |
| 1495 | exit(1); |
| 1496 | } |
| 1497 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1498 | signal(SIGABRT, &signal_handler); |
| 1499 | signal(SIGUSR1, &signal_handler); |
| 1500 | signal(SIGPIPE, SIG_IGN); |
| 1501 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1502 | if (daemonize) { |
| 1503 | rc = osmo_daemonize(); |
| 1504 | if (rc < 0) { |
| 1505 | perror("Error during daemonize"); |
| 1506 | exit(1); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1510 | /* recycle timer */ |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1511 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1512 | sccp_close.cb = sccp_close_unconfirmed; |
| 1513 | sccp_close.data = NULL; |
| 1514 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1515 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1516 | while (1) { |
| 1517 | bsc_select_main(0); |
| 1518 | } |
| 1519 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1520 | return 0; |
| 1521 | } |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1522 | |
| 1523 | /* Close all connections handed out to the USSD module */ |
| 1524 | int bsc_close_ussd_connections(struct bsc_nat *nat) |
| 1525 | { |
| 1526 | struct sccp_connections *con; |
| 1527 | llist_for_each_entry(con, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1528 | if (con->con_local != NAT_CON_END_USSD) |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1529 | continue; |
| 1530 | if (!con->bsc) |
| 1531 | continue; |
| 1532 | |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 1533 | nat_send_clrc_bsc(con); |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1534 | nat_send_rlsd_bsc(con); |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |