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