Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | cc49469 | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2013 by On-Waves |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 25 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 26 | #include <arpa/inet.h> |
| 27 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 29 | #include <signal.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Holger Hans Peter Freyther | fd012d5 | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #define _GNU_SOURCE |
| 36 | #include <getopt.h> |
| 37 | |
| 38 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 39 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 40 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/ipaccess.h> |
| 43 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 44 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 45 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 46 | |
Harald Welte | 5919b6a | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 47 | #include <osmocom/ctrl/control_cmd.h> |
| 48 | #include <osmocom/ctrl/control_if.h> |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 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> |
Harald Welte | 6bcd303 | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 60 | #include <osmocom/vty/ports.h> |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 61 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 62 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 63 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 64 | #include <osmocom/abis/ipa.h> |
| 65 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 66 | #include "../../bscconfig.h" |
| 67 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 68 | #define SCCP_CLOSE_TIME 20 |
| 69 | #define SCCP_CLOSE_TIME_TIMEOUT 19 |
| 70 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 71 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 72 | static struct in_addr local_addr; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 73 | static struct osmo_fd bsc_listen; |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 74 | static const char *msc_ip = NULL; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 75 | static struct osmo_timer_list sccp_close; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 76 | static int daemonize = 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 77 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 78 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 79 | "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n" |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 80 | "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] | 81 | "This is free software: you are free to change and redistribute it.\r\n" |
| 82 | "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] | 83 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 84 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 85 | 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] | 86 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 87 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 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 | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 204 | static struct msgb *nat_create_rlsd(struct nat_sccp_connection *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 | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 225 | static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 239 | static void nat_send_rlsd_msc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 240 | { |
| 241 | struct msgb *msg; |
| 242 | |
| 243 | msg = nat_create_rlsd(conn); |
| 244 | if (!msg) |
| 245 | return; |
| 246 | |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 247 | ipa_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 | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 251 | static void nat_send_rlsd_bsc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 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 | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 272 | static struct msgb *nat_creat_clrc(struct nat_sccp_connection *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 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 294 | static int nat_send_clrc_bsc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 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) { |
Holger Hans Peter Freyther | 0cc1498 | 2013-09-03 11:25:16 +0200 | [diff] [blame] | 313 | LOGP(DNAT, LOGL_ERROR, "Failed to sccp rlc.\n"); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 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 | |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 323 | ipa_prepend_header(msg, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 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 | cd8bea9 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 337 | void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg) |
| 338 | { |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 339 | ipa_prepend_header(msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | cd8bea9 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 340 | queue_for_msc(nat->msc_con, msg); |
| 341 | } |
| 342 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 343 | /* |
| 344 | * Below is the handling of messages coming |
| 345 | * from the MSC and need to be forwarded to |
| 346 | * a real BSC. |
| 347 | */ |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 348 | 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] | 349 | { |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 350 | if (msc_con->first_contact) |
Holger Hans Peter Freyther | dc81393 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 351 | return; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 352 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 353 | msc_con->first_contact = 1; |
| 354 | msc_send_reset(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | struct msgb *msg = bsc_msc_id_get_resp(nat->token); |
| 360 | if (!msg) |
| 361 | return; |
| 362 | |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 363 | ipa_prepend_header(msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 364 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 367 | /* |
| 368 | * Currently we are lacking refcounting so we need to copy each message. |
| 369 | */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 370 | 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] | 371 | { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 372 | struct msgb *msg; |
| 373 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 374 | if (length > 4096 - 128) { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 375 | 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] | 376 | return; |
| 377 | } |
| 378 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 379 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 380 | if (!msg) { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 381 | 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] | 382 | return; |
| 383 | } |
| 384 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 385 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 386 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 387 | |
| 388 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 391 | /* |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 392 | * Update the release statistics |
| 393 | */ |
| 394 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal) |
| 395 | { |
| 396 | if (!bsc->cfg) { |
| 397 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated."); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (filter >= 0) { |
| 402 | LOGP(DNAT, LOGL_ERROR, "Connection was not rejected"); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | if (filter == -1) |
| 407 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]); |
| 408 | else if (normal) |
| 409 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]); |
| 410 | else |
| 411 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]); |
| 412 | } |
| 413 | |
| 414 | /* |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 415 | * Release an established connection. We will have to release it to the BSC |
| 416 | * and to the network and we do it the following way. |
| 417 | * 1.) Give up on the MSC side |
| 418 | * 1.1) Send a RLSD message, it is a bit non standard but should work, we |
| 419 | * ignore the RLC... we might complain about it. Other options would |
| 420 | * be to send a Release Request, handle the Release Complete.. |
| 421 | * 1.2) Mark the data structure to be con_local and wait for 2nd |
| 422 | * |
| 423 | * 2.) Give up on the BSC side |
| 424 | * 2.1) Depending on the con type reject the service, or just close it |
| 425 | */ |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 426 | static void bsc_send_con_release(struct bsc_connection *bsc, |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 427 | struct nat_sccp_connection *con, |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 428 | struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 429 | { |
| 430 | struct msgb *rlsd; |
| 431 | /* 1. release the network */ |
| 432 | rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref, |
| 433 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 434 | if (!rlsd) |
| 435 | LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n"); |
| 436 | else { |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 437 | ipa_prepend_header(rlsd, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 438 | queue_for_msc(con->msc_con, rlsd); |
| 439 | } |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 440 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | b2b66c2 | 2010-09-16 06:41:09 +0800 | [diff] [blame] | 441 | con->msc_con = NULL; |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 442 | |
| 443 | /* 2. release the BSC side */ |
| 444 | if (con->con_type == NAT_CON_TYPE_LU) { |
| 445 | struct msgb *payload, *udt; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 446 | payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 447 | |
| 448 | if (payload) { |
| 449 | gsm0808_prepend_dtap_header(payload, 0); |
| 450 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 451 | if (udt) |
| 452 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 453 | else |
| 454 | LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n"); |
| 455 | |
| 456 | msgb_free(payload); |
| 457 | } else { |
| 458 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n"); |
| 459 | } |
| 460 | } |
| 461 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 462 | nat_send_clrc_bsc(con); |
| 463 | |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 464 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 465 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 466 | if (!rlsd) { |
| 467 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n"); |
| 468 | sccp_connection_destroy(con); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
| 473 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 474 | } |
| 475 | |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 476 | static void bsc_send_con_refuse(struct bsc_connection *bsc, |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 477 | struct bsc_nat_parsed *parsed, int con_type, |
| 478 | struct bsc_nat_reject_cause *cause) |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 479 | { |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 480 | struct msgb *payload; |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 481 | struct msgb *refuse; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 482 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 483 | if (con_type == NAT_CON_TYPE_LU) |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 484 | payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); |
Holger Hans Peter Freyther | 7544d8b | 2013-01-01 16:50:40 +0100 | [diff] [blame] | 485 | else if (con_type == NAT_CON_TYPE_CM_SERV_REQ || con_type == NAT_CON_TYPE_SSA) |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 486 | payload = gsm48_create_mm_serv_rej(cause->cm_reject_cause); |
Holger Hans Peter Freyther | a624c53 | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 487 | else { |
| 488 | LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); |
| 489 | payload = NULL; |
| 490 | } |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * Some BSCs do not handle the payload inside a SCCP CREF msg |
| 494 | * so we will need to: |
| 495 | * 1.) Allocate a local connection and mark it as local.. |
| 496 | * 2.) queue data for downstream.. and the RLC should delete everything |
| 497 | */ |
| 498 | if (payload) { |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 499 | struct msgb *cc, *udt, *clear, *rlsd; |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 500 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 501 | con = create_sccp_src_ref(bsc, parsed); |
| 502 | if (!con) |
| 503 | goto send_refuse; |
| 504 | |
| 505 | /* declare it local and assign a unique remote_ref */ |
| 506 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 507 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 508 | con->has_remote_ref = 1; |
| 509 | con->remote_ref = con->patched_ref; |
| 510 | |
| 511 | /* 1. create a confirmation */ |
| 512 | cc = sccp_create_cc(&con->remote_ref, &con->real_ref); |
| 513 | if (!cc) |
| 514 | goto send_refuse; |
| 515 | |
| 516 | /* 2. create the DT1 */ |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 517 | gsm0808_prepend_dtap_header(payload, 0); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 518 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 519 | if (!udt) { |
| 520 | msgb_free(cc); |
| 521 | goto send_refuse; |
| 522 | } |
| 523 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 524 | /* 3. send a Clear Command */ |
| 525 | clear = nat_creat_clrc(con, 0x20); |
| 526 | if (!clear) { |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 527 | msgb_free(cc); |
| 528 | msgb_free(udt); |
| 529 | goto send_refuse; |
| 530 | } |
| 531 | |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 532 | /* 4. send a RLSD */ |
| 533 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 534 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 535 | if (!rlsd) { |
| 536 | msgb_free(cc); |
| 537 | msgb_free(udt); |
| 538 | msgb_free(clear); |
| 539 | goto send_refuse; |
| 540 | } |
| 541 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 542 | bsc_write(bsc, cc, IPAC_PROTO_SCCP); |
| 543 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 38f640f | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 544 | bsc_write(bsc, clear, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 545 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 546 | msgb_free(payload); |
| 547 | return; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 548 | } |
| 549 | |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 550 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 551 | send_refuse: |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 552 | if (payload) |
| 553 | msgb_free(payload); |
| 554 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 555 | refuse = sccp_create_refuse(parsed->src_local_ref, |
| 556 | SCCP_REFUSAL_SCCP_FAILURE, NULL, 0); |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 557 | if (!refuse) { |
| 558 | LOGP(DNAT, LOGL_ERROR, |
| 559 | "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n", |
| 560 | sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | bsc_write(bsc, refuse, IPAC_PROTO_SCCP); |
| 565 | } |
| 566 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 567 | static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg) |
| 568 | { |
| 569 | if (bsc->cfg->forbid_paging) { |
| 570 | LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr); |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP); |
| 575 | } |
| 576 | |
| 577 | static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg) |
| 578 | { |
| 579 | struct bsc_connection *bsc; |
| 580 | const uint8_t *paging_start; |
Holger Hans Peter Freyther | f77bca7 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 581 | int paging_length, i, ret; |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 582 | |
Holger Hans Peter Freyther | f77bca7 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 583 | ret = bsc_nat_find_paging(msg, &paging_start, &paging_length); |
| 584 | if (ret != 0) { |
| 585 | 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] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | /* This is quite expensive now */ |
| 590 | for (i = 0; i < paging_length; i += 2) { |
| 591 | unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]); |
| 592 | unsigned int paged = 0; |
| 593 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 594 | if (!bsc->cfg) |
| 595 | continue; |
| 596 | if (!bsc->authenticated) |
| 597 | continue; |
| 598 | if (!bsc_config_handles_lac(bsc->cfg, _lac)) |
| 599 | continue; |
| 600 | bsc_nat_send_paging(bsc, msg); |
| 601 | paged += 1; |
| 602 | } |
| 603 | |
| 604 | /* highlight a possible config issue */ |
| 605 | if (paged == 0) |
| 606 | LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac); |
| 607 | |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 612 | /* |
| 613 | * Update the auth status. This can be either a CIPHER MODE COMAMND or |
| 614 | * a CM Serivce Accept. Maybe also LU Accept or such in the future. |
| 615 | */ |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 616 | static void update_con_authorize(struct nat_sccp_connection *con, |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 617 | struct bsc_nat_parsed *parsed, |
| 618 | struct msgb *msg) |
| 619 | { |
| 620 | if (!con) |
| 621 | return; |
| 622 | if (con->authorized) |
| 623 | return; |
| 624 | |
| 625 | if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT && |
| 626 | parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) { |
| 627 | con->authorized = 1; |
| 628 | } else if (parsed->bssap == BSSAP_MSG_DTAP) { |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 629 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 630 | uint32_t len; |
| 631 | struct gsm48_hdr *hdr48; |
| 632 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 633 | if (!hdr48) |
| 634 | return; |
| 635 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 636 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 637 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 638 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 639 | msg_type == GSM48_MT_MM_CM_SERV_ACC) |
| 640 | con->authorized = 1; |
| 641 | } |
| 642 | } |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 643 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 644 | 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] | 645 | { |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 646 | struct nat_sccp_connection *con = NULL; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 647 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 648 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 649 | int proto; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 650 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 651 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 652 | parsed = bsc_nat_parse(msg); |
| 653 | if (!parsed) { |
| 654 | 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] | 655 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 656 | } |
| 657 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 658 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 659 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 660 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 661 | proto = parsed->ipa_proto; |
| 662 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 663 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 664 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 665 | switch (parsed->sccp_type) { |
| 666 | case SCCP_MSG_TYPE_UDT: |
| 667 | /* forward UDT messages to every BSC */ |
| 668 | goto send_to_all; |
| 669 | break; |
| 670 | case SCCP_MSG_TYPE_RLSD: |
| 671 | case SCCP_MSG_TYPE_CREF: |
| 672 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 673 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 674 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 675 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 676 | osmo_counter_inc(nat->stats.sccp.calls); |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 677 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 678 | if (con) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 679 | struct rate_ctr_group *ctrg; |
| 680 | ctrg = con->bsc->cfg->stats.ctrg; |
| 681 | rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]); |
Holger Hans Peter Freyther | 926c5fd | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 682 | if (bsc_mgcp_assign_patch(con, msg) != 0) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 683 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 684 | } else |
| 685 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
Holger Hans Peter Freyther | 1b71492 | 2011-04-21 17:13:51 +0200 | [diff] [blame] | 686 | } else if (con && con->con_local == NAT_CON_END_USSD && |
| 687 | parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) { |
| 688 | LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n"); |
| 689 | con = NULL; |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 690 | } |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 691 | break; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 692 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 693 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 694 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 695 | goto exit; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 696 | break; |
| 697 | case SCCP_MSG_TYPE_RLC: |
| 698 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 699 | goto exit; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 700 | break; |
| 701 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 702 | /* MSC never opens a SCCP connection, fall through */ |
| 703 | default: |
| 704 | goto exit; |
| 705 | } |
Holger Hans Peter Freyther | 8d29828 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 706 | |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 707 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 708 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 709 | /* Exchange src/dest for the reply */ |
Holger Hans Peter Freyther | 85dee84 | 2013-09-03 14:35:33 +0200 | [diff] [blame] | 710 | nat_send_rlc(msc_con, &parsed->original_dest_ref, |
| 711 | parsed->src_local_ref); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 712 | } else if (!con) |
Holger Hans Peter Freyther | 117e7a7 | 2010-05-15 19:13:52 +0800 | [diff] [blame] | 713 | 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] | 714 | } |
| 715 | |
Pablo Neira Ayuso | 8cf2fc2 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 716 | if (!con) { |
| 717 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 718 | return -1; |
Pablo Neira Ayuso | 8cf2fc2 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 719 | } |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 720 | if (!con->bsc->authenticated) { |
Pablo Neira Ayuso | 8cf2fc2 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 721 | talloc_free(parsed); |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 722 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 723 | return -1; |
| 724 | } |
| 725 | |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 726 | update_con_authorize(con, parsed, msg); |
Pablo Neira Ayuso | 8cf2fc2 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 727 | talloc_free(parsed); |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 728 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 729 | 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] | 730 | return 0; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 731 | |
| 732 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 733 | /* |
| 734 | * Filter Paging from the network. We do not want to send a PAGING |
| 735 | * Command to every BSC in our network. We will analys the PAGING |
| 736 | * message and then send it to the authenticated messages... |
| 737 | */ |
| 738 | 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] | 739 | bsc_nat_handle_paging(nat, msg); |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 740 | goto exit; |
| 741 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 742 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 743 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 744 | if (!bsc->authenticated) |
| 745 | continue; |
| 746 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 747 | 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] | 748 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 749 | |
| 750 | exit: |
| 751 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 752 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 755 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 756 | { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 757 | struct bsc_connection *bsc, *tmp; |
| 758 | |
| 759 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 760 | 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] | 761 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 762 | |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 763 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 764 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 765 | } |
| 766 | |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 767 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 768 | { |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 769 | osmo_counter_inc(nat->stats.msc.reconn); |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 770 | } |
| 771 | |
Holger Hans Peter Freyther | 8f515a0 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 772 | 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] | 773 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 774 | static const uint8_t reset[] = { |
Holger Hans Peter Freyther | c48a5d9 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 775 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 776 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 777 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 778 | 0x01, 0x20 |
| 779 | }; |
| 780 | |
| 781 | struct msgb *msg; |
| 782 | |
| 783 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 784 | if (!msg) { |
| 785 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 790 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 791 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 792 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 793 | |
| 794 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 795 | } |
| 796 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 797 | static int ipaccess_msc_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 798 | { |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 799 | struct bsc_msc_connection *msc_con; |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 800 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 801 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 802 | int ret; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 803 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 804 | msc_con = (struct bsc_msc_connection *) bfd->data; |
| 805 | |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 806 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &msc_con->pending_msg); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 807 | if (ret <= 0) { |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 808 | if (ret == -EAGAIN) |
| 809 | return 0; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 810 | if (ret == 0) |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 811 | LOGP(DNAT, LOGL_FATAL, |
| 812 | "The connection the MSC(%s) was lost, exiting\n", |
| 813 | msc_con->name); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 814 | else |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 815 | LOGP(DNAT, LOGL_ERROR, |
| 816 | "Failed to parse ip access message on %s: %d\n", |
| 817 | msc_con->name, ret); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 818 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 819 | bsc_msc_lost(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 823 | LOGP(DNAT, LOGL_DEBUG, |
| 824 | "MSG from MSC(%s): %s proto: %d\n", msc_con->name, |
| 825 | osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 826 | |
| 827 | /* handle base message handling */ |
| 828 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 829 | |
| 830 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 831 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 832 | ipa_ccm_rcvmsg_base(msg, bfd); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 833 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 834 | initialize_msc_if_needed(msc_con); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 835 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 836 | send_id_get_response(msc_con); |
Holger Hans Peter Freyther | cd8bea9 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 837 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 838 | forward_sccp_to_bts(msc_con, msg); |
Holger Hans Peter Freyther | cd8bea9 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 839 | } else if (hh->proto == IPAC_PROTO_MGCP_OLD) { |
| 840 | bsc_nat_handle_mgcp(nat, msg); |
| 841 | } |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 842 | |
Holger Hans Peter Freyther | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 843 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 844 | return 0; |
| 845 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 846 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 847 | 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] | 848 | { |
| 849 | int rc; |
| 850 | rc = write(bfd->fd, msg->data, msg->len); |
| 851 | |
| 852 | if (rc != msg->len) { |
| 853 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | return rc; |
| 858 | } |
| 859 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 860 | /* |
| 861 | * Below is the handling of messages coming |
| 862 | * from the BSC and need to be forwarded to |
| 863 | * a real BSC. |
| 864 | */ |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 865 | |
| 866 | /* |
| 867 | * Remove the connection from the connections list, |
| 868 | * remove it from the patching of SCCP header lists |
| 869 | * as well. Maybe in the future even close connection.. |
| 870 | */ |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 871 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 872 | { |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 873 | struct nat_sccp_connection *sccp_patch, *tmp; |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 874 | struct bsc_cmd_list *cmd_entry, *cmd_tmp; |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 875 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 876 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 877 | /* stop the timeout timer */ |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 878 | osmo_timer_del(&connection->id_timeout); |
| 879 | osmo_timer_del(&connection->ping_timeout); |
| 880 | osmo_timer_del(&connection->pong_timeout); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 881 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 882 | if (connection->cfg) |
| 883 | ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP]; |
| 884 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 885 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 886 | 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] | 887 | if (sccp_patch->bsc != connection) |
| 888 | continue; |
| 889 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 890 | if (ctr) |
| 891 | rate_ctr_inc(ctr); |
Holger Hans Peter Freyther | 38bc276 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 892 | if (sccp_patch->has_remote_ref) { |
| 893 | if (sccp_patch->con_local == NAT_CON_END_MSC) |
| 894 | nat_send_rlsd_msc(sccp_patch); |
| 895 | else if (sccp_patch->con_local == NAT_CON_END_USSD) |
| 896 | nat_send_rlsd_ussd(nat, sccp_patch); |
| 897 | } |
| 898 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 899 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 902 | /* Reply to all outstanding commands */ |
| 903 | llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) { |
| 904 | cmd_entry->cmd->type = CTRL_TYPE_ERROR; |
| 905 | cmd_entry->cmd->reply = "BSC closed the connection"; |
| 906 | ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd); |
Holger Hans Peter Freyther | 9a01062 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 907 | bsc_nat_ctrl_del_pending(cmd_entry); |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Holger Hans Peter Freyther | c0ff1d0 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 910 | /* close endpoints allocated by this BSC */ |
| 911 | bsc_mgcp_clear_endpoints_for(connection); |
| 912 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 913 | osmo_fd_unregister(&connection->write_queue.bfd); |
Holger Hans Peter Freyther | 53cd985 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 914 | close(connection->write_queue.bfd.fd); |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 915 | osmo_wqueue_clear(&connection->write_queue); |
Holger Hans Peter Freyther | 53cd985 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 916 | llist_del(&connection->list_entry); |
| 917 | |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 918 | if (connection->pending_msg) { |
| 919 | LOGP(DNAT, LOGL_ERROR, "Dropping partial message on connection %d.\n", |
Jacob Erlbeck | d0a941f | 2014-04-03 12:10:24 +0200 | [diff] [blame] | 920 | connection->cfg ? connection->cfg->nr : -1); |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 921 | msgb_free(connection->pending_msg); |
| 922 | connection->pending_msg = NULL; |
| 923 | } |
| 924 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 925 | talloc_free(connection); |
| 926 | } |
| 927 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 928 | static void bsc_maybe_close(struct bsc_connection *bsc) |
| 929 | { |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 930 | struct nat_sccp_connection *sccp; |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 931 | if (!bsc->nat->blocked) |
| 932 | return; |
| 933 | |
| 934 | /* are there any connections left */ |
| 935 | llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry) |
| 936 | if (sccp->bsc == bsc) |
| 937 | return; |
| 938 | |
| 939 | /* nothing left, close the BSC */ |
| 940 | LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n", |
| 941 | bsc->cfg ? bsc->cfg->nr : -1); |
| 942 | bsc_close_connection(bsc); |
| 943 | } |
| 944 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 945 | static void ipaccess_close_bsc(void *data) |
| 946 | { |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 947 | struct sockaddr_in sock; |
| 948 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 949 | struct bsc_connection *conn = data; |
| 950 | |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 951 | |
| 952 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 953 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 954 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 955 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 959 | { |
| 960 | struct bsc_config *conf; |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 961 | 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] | 962 | const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 963 | |
Holger Hans Peter Freyther | 4dc44ea | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 964 | if (bsc->cfg) { |
| 965 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 966 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 967 | return; |
| 968 | } |
| 969 | |
Holger Hans Peter Freyther | 8e9649c | 2013-03-19 20:50:36 +0100 | [diff] [blame] | 970 | if (len <= 0) { |
| 971 | LOGP(DNAT, LOGL_ERROR, "Token with length zero on fd: %d\n", |
| 972 | bsc->write_queue.bfd.fd); |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (token[len - 1] != '\0') { |
| 977 | LOGP(DNAT, LOGL_ERROR, "Token not null terminated on fd: %d\n", |
| 978 | bsc->write_queue.bfd.fd); |
| 979 | return; |
| 980 | } |
| 981 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 982 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
Holger Hans Peter Freyther | 8e9649c | 2013-03-19 20:50:36 +0100 | [diff] [blame] | 983 | /* |
| 984 | * Add the '\0' of the token for the memcmp, the IPA messages |
| 985 | * for some reason added null termination. |
| 986 | */ |
| 987 | const int token_len = strlen(conf->token) + 1; |
| 988 | |
| 989 | if (token_len == len && memcmp(conf->token, token, token_len) == 0) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 990 | 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] | 991 | bsc->authenticated = 1; |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 992 | bsc->cfg = conf; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 993 | osmo_timer_del(&bsc->id_timeout); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 994 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n", |
| 995 | conf->nr, bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 996 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 997 | return; |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 998 | } |
| 999 | } |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 1000 | |
Holger Hans Peter Freyther | 8e9649c | 2013-03-19 20:50:36 +0100 | [diff] [blame] | 1001 | LOGP(DNAT, LOGL_ERROR, "No bsc found for token '%s' on fd: %d.\n", token, |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 1002 | bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1005 | static void handle_con_stats(struct nat_sccp_connection *con) |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 1006 | { |
| 1007 | struct rate_ctr_group *ctrg; |
| 1008 | int id = bsc_conn_type_to_ctr(con); |
| 1009 | |
| 1010 | if (id == -1) |
| 1011 | return; |
| 1012 | |
| 1013 | if (!con->bsc || !con->bsc->cfg) |
| 1014 | return; |
| 1015 | |
| 1016 | ctrg = con->bsc->cfg->stats.ctrg; |
| 1017 | rate_ctr_inc(&ctrg->ctr[id]); |
| 1018 | } |
| 1019 | |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1020 | 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] | 1021 | { |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 1022 | int con_filter = 0; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1023 | char *imsi = NULL; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1024 | struct bsc_msc_connection *con_msc = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1025 | struct bsc_connection *con_bsc = NULL; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 1026 | int con_type; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1027 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1028 | struct bsc_nat_reject_cause cause; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1029 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1030 | /* Parse and filter messages */ |
| 1031 | parsed = bsc_nat_parse(msg); |
| 1032 | if (!parsed) { |
| 1033 | 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] | 1034 | msgb_free(msg); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1035 | return -1; |
| 1036 | } |
| 1037 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 1038 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1039 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 1040 | |
Holger Hans Peter Freyther | 5a723b6 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 1041 | /* |
| 1042 | * check authentication after filtering to not reject auth |
| 1043 | * responses coming from the BSC. We have to make sure that |
| 1044 | * nothing from the exit path will forward things to the MSC |
| 1045 | */ |
| 1046 | if (!bsc->authenticated) { |
| 1047 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 1048 | msgb_free(msg); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1053 | /* modify the SCCP entries */ |
| 1054 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 8582bb9 | 2010-09-15 00:38:54 +0800 | [diff] [blame] | 1055 | int filter; |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1056 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1057 | switch (parsed->sccp_type) { |
| 1058 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1059 | memset(&cause, 0, sizeof(cause)); |
| 1060 | filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, |
| 1061 | &con_type, &imsi, &cause); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1062 | if (filter < 0) { |
Holger Hans Peter Freyther | 22d77aa | 2015-04-05 13:45:53 +0200 | [diff] [blame] | 1063 | if (imsi) |
| 1064 | bsc_nat_inform_reject(bsc, imsi); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1065 | bsc_stat_reject(filter, bsc, 0); |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1066 | goto exit3; |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 1069 | if (!create_sccp_src_ref(bsc, parsed)) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1070 | goto exit2; |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1071 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 6200fd9 | 2010-07-05 13:33:18 +0800 | [diff] [blame] | 1072 | con->msc_con = bsc->nat->msc_con; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1073 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 1074 | con->con_type = con_type; |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 1075 | con->imsi_checked = filter; |
Holger Hans Peter Freyther | cc49469 | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 1076 | bsc_nat_extract_lac(bsc, con, parsed, msg); |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1077 | if (imsi) |
| 1078 | con->imsi = talloc_steal(con, imsi); |
| 1079 | imsi = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1080 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 1081 | handle_con_stats(con); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1082 | break; |
| 1083 | case SCCP_MSG_TYPE_RLSD: |
| 1084 | case SCCP_MSG_TYPE_CREF: |
| 1085 | case SCCP_MSG_TYPE_DT1: |
| 1086 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 1087 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1088 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1089 | if (con) { |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1090 | /* only filter non local connections */ |
| 1091 | if (!con->con_local) { |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1092 | memset(&cause, 0, sizeof(cause)); |
| 1093 | filter = bsc_nat_filter_dt(bsc, msg, |
| 1094 | con, parsed, &cause); |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1095 | if (filter < 0) { |
Holger Hans Peter Freyther | 22d77aa | 2015-04-05 13:45:53 +0200 | [diff] [blame] | 1096 | if (imsi) |
| 1097 | bsc_nat_inform_reject(bsc, imsi); |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1098 | bsc_stat_reject(filter, bsc, 1); |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1099 | bsc_send_con_release(bsc, con, &cause); |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1100 | con = NULL; |
| 1101 | goto exit2; |
| 1102 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1103 | |
| 1104 | /* hand data to a side channel */ |
Holger Hans Peter Freyther | 30f1702 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1105 | if (bsc_ussd_check(con, parsed, msg) == 1) |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1106 | con->con_local = NAT_CON_END_USSD; |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* |
| 1109 | * Optionally rewrite setup message. This can |
| 1110 | * replace the msg and the parsed structure becomes |
| 1111 | * invalid. |
| 1112 | */ |
Holger Hans Peter Freyther | 1f58606 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1113 | 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] | 1114 | talloc_free(parsed); |
| 1115 | parsed = NULL; |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 1116 | } else if (con->con_local == NAT_CON_END_USSD) { |
Holger Hans Peter Freyther | 30f1702 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1117 | bsc_ussd_check(con, parsed, msg); |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 1118 | } |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1119 | |
| 1120 | con_bsc = con->bsc; |
| 1121 | con_msc = con->msc_con; |
| 1122 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1123 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1124 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1125 | break; |
| 1126 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1127 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1128 | if (con) { |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1129 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1130 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 1131 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1132 | } |
Holger Hans Peter Freyther | 38712d0 | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 1133 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1134 | bsc_maybe_close(bsc); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1135 | break; |
| 1136 | case SCCP_MSG_TYPE_UDT: |
| 1137 | /* simply forward everything */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1138 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1139 | break; |
| 1140 | default: |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 1141 | 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] | 1142 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1143 | goto exit2; |
| 1144 | break; |
| 1145 | } |
Holger Hans Peter Freyther | a63d35c | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 1146 | } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) { |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 1147 | bsc_mgcp_forward(bsc, msg); |
| 1148 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1149 | } else { |
| 1150 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 1151 | goto exit2; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1154 | if (con_msc && con_bsc != bsc) { |
Holger Hans Peter Freyther | 4107f98 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 1155 | 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] | 1156 | bsc->cfg->nr, con_bsc->cfg->nr); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1157 | goto exit2; |
| 1158 | } |
| 1159 | |
Holger Hans Peter Freyther | 79a6baf | 2010-09-15 17:39:44 +0800 | [diff] [blame] | 1160 | /* do not forward messages to the MSC */ |
| 1161 | if (con_filter) |
| 1162 | goto exit2; |
| 1163 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1164 | if (!con_msc) { |
Holger Hans Peter Freyther | 43bd9d1 | 2010-09-15 00:14:48 +0800 | [diff] [blame] | 1165 | LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n", |
| 1166 | bsc->cfg->nr, |
| 1167 | parsed ? parsed->ipa_proto : -1, |
| 1168 | parsed ? parsed->sccp_type : -1); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1169 | goto exit2; |
| 1170 | } |
| 1171 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1172 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1173 | queue_for_msc(con_msc, msg); |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1174 | if (parsed) |
| 1175 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1176 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1177 | |
| 1178 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1179 | /* if we filter out the reset send an ack to the BSC */ |
| 1180 | 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] | 1181 | send_reset_ack(bsc); |
| 1182 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1183 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 1184 | /* do we know who is handling this? */ |
| 1185 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 1186 | struct tlv_parsed tvp; |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1187 | int ret; |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 1188 | ret = ipa_ccm_idtag_parse(&tvp, |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1189 | (unsigned char *) msg->l2h + 2, |
| 1190 | msgb_l2len(msg) - 2); |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1191 | if (ret < 0) { |
| 1192 | LOGP(DNAT, LOGL_ERROR, "ignoring IPA response " |
| 1193 | "message with malformed TLVs\n"); |
| 1194 | return ret; |
| 1195 | } |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1196 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 1197 | ipaccess_auth_bsc(&tvp, bsc); |
| 1198 | } |
| 1199 | |
| 1200 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1203 | exit2: |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1204 | if (imsi) |
| 1205 | talloc_free(imsi); |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1206 | talloc_free(parsed); |
| 1207 | msgb_free(msg); |
| 1208 | return -1; |
| 1209 | |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1210 | exit3: |
| 1211 | /* send a SCCP Connection Refused */ |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1212 | if (imsi) |
| 1213 | talloc_free(imsi); |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1214 | bsc_send_con_refuse(bsc, parsed, con_type, &cause); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1215 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1216 | msgb_free(msg); |
| 1217 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1220 | static int ipaccess_bsc_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1221 | { |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1222 | struct bsc_connection *bsc = bfd->data; |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1223 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1224 | struct ipaccess_head *hh; |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1225 | struct ipaccess_head_ext *hh_ext; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1226 | int ret; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1227 | |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1228 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &bsc->pending_msg); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1229 | if (ret <= 0) { |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1230 | if (ret == -EAGAIN) |
| 1231 | return 0; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1232 | if (ret == 0) |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1233 | LOGP(DNAT, LOGL_ERROR, |
| 1234 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 1235 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1236 | else |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1237 | LOGP(DNAT, LOGL_ERROR, |
| 1238 | "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] | 1239 | bsc->cfg ? bsc->cfg->nr : -1, ret); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1240 | |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 1241 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
| 1244 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1245 | |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 1246 | 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] | 1247 | |
| 1248 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1249 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1250 | |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1251 | /* stop the pong timeout */ |
| 1252 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 1253 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1254 | osmo_timer_del(&bsc->pong_timeout); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1255 | msgb_free(msg); |
| 1256 | return 0; |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1257 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 1258 | send_pong(bsc); |
| 1259 | msgb_free(msg); |
| 1260 | return 0; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1261 | } |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1262 | /* Message contains the ipaccess_head_ext header, investigate further */ |
| 1263 | } else if (hh->proto == IPAC_PROTO_OSMO && |
| 1264 | msg->len > sizeof(*hh) + sizeof(*hh_ext)) { |
| 1265 | |
| 1266 | hh_ext = (struct ipaccess_head_ext *) hh->data; |
| 1267 | /* l2h is where the actual command data is expected */ |
| 1268 | msg->l2h = hh_ext->data; |
| 1269 | |
Harald Welte | 8ba399e | 2011-07-13 14:43:37 +0200 | [diff] [blame] | 1270 | if (hh_ext->proto == IPAC_PROTO_EXT_CTRL) |
Holger Hans Peter Freyther | 9a01062 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 1271 | return bsc_nat_handle_ctrlif_msg(bsc, msg); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1274 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 1275 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1276 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1281 | 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] | 1282 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1283 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1284 | int fd, rc, on; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1285 | struct sockaddr_in sa; |
| 1286 | socklen_t sa_len = sizeof(sa); |
| 1287 | |
| 1288 | if (!(what & BSC_FD_READ)) |
| 1289 | return 0; |
| 1290 | |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1291 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 1292 | if (fd < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1293 | perror("accept"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1294 | return fd; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1297 | /* count the reconnect */ |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 1298 | osmo_counter_inc(nat->stats.bsc.reconn); |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1299 | |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1300 | /* |
| 1301 | * if we are not connected to a msc... just close the socket |
| 1302 | */ |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 1303 | if (!bsc_nat_msc_is_connected(nat)) { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1304 | 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] | 1305 | close(fd); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1306 | return 0; |
| 1307 | } |
| 1308 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1309 | if (nat->blocked) { |
| 1310 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n"); |
| 1311 | close(fd); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1315 | on = 1; |
Holger Hans Peter Freyther | d58afeb | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 1316 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1317 | if (rc != 0) |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1318 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 1319 | |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1320 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | f0374a5 | 2010-07-27 19:21:53 +0800 | [diff] [blame] | 1321 | &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp)); |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1322 | if (rc != 0) |
| 1323 | LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 1324 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1325 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 1326 | /* 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] | 1327 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1328 | /* |
| 1329 | * |
| 1330 | */ |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1331 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1332 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1333 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1334 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1335 | return -1; |
| 1336 | } |
| 1337 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1338 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1339 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1340 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 1341 | bsc->write_queue.write_cb = bsc_write_cb; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1342 | bsc->write_queue.bfd.when = BSC_FD_READ; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1343 | if (osmo_fd_register(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1344 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1345 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1346 | talloc_free(bsc); |
| 1347 | return -2; |
| 1348 | } |
| 1349 | |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 1350 | 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] | 1351 | fd, inet_ntoa(sa.sin_addr)); |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1352 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1353 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1354 | bsc->last_id = 0; |
| 1355 | |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 1356 | send_id_ack(bsc); |
| 1357 | send_id_req(bsc); |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 1358 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * start the hangup timer |
| 1362 | */ |
| 1363 | bsc->id_timeout.data = bsc; |
| 1364 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1365 | osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1366 | return 0; |
| 1367 | } |
| 1368 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1369 | static void print_usage() |
| 1370 | { |
| 1371 | printf("Usage: bsc_nat\n"); |
| 1372 | } |
| 1373 | |
| 1374 | static void print_help() |
| 1375 | { |
| 1376 | printf(" Some useful help...\n"); |
| 1377 | printf(" -h --help this text\n"); |
| 1378 | 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] | 1379 | 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] | 1380 | printf(" -s --disable-color\n"); |
| 1381 | printf(" -c --config-file filename The config file to use.\n"); |
| 1382 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1383 | 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] | 1384 | } |
| 1385 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1386 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1387 | { |
| 1388 | while (1) { |
| 1389 | int option_index = 0, c; |
| 1390 | static struct option long_options[] = { |
| 1391 | {"help", 0, 0, 'h'}, |
| 1392 | {"debug", 1, 0, 'd'}, |
Holger Hans Peter Freyther | 961775e | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1393 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1394 | {"config-file", 1, 0, 'c'}, |
| 1395 | {"disable-color", 0, 0, 's'}, |
| 1396 | {"timestamp", 0, 0, 'T'}, |
| 1397 | {"msc", 1, 0, 'm'}, |
| 1398 | {"local", 1, 0, 'l'}, |
| 1399 | {0, 0, 0, 0} |
| 1400 | }; |
| 1401 | |
Holger Hans Peter Freyther | 961775e | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1402 | c = getopt_long(argc, argv, "hd:sTPc:m:l:D", |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1403 | long_options, &option_index); |
| 1404 | if (c == -1) |
| 1405 | break; |
| 1406 | |
| 1407 | switch (c) { |
| 1408 | case 'h': |
| 1409 | print_usage(); |
| 1410 | print_help(); |
| 1411 | exit(0); |
| 1412 | case 's': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1413 | log_set_use_color(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1414 | break; |
| 1415 | case 'd': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1416 | log_parse_category_mask(osmo_stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1417 | break; |
Holger Hans Peter Freyther | 961775e | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1418 | case 'D': |
| 1419 | daemonize = 1; |
| 1420 | break; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1421 | case 'c': |
Holger Hans Peter Freyther | 4c6b4b9 | 2015-02-08 09:25:38 +0100 | [diff] [blame] | 1422 | config_file = optarg; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1423 | break; |
| 1424 | case 'T': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1425 | log_set_print_timestamp(osmo_stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1426 | break; |
| 1427 | case 'm': |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1428 | msc_ip = optarg; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1429 | break; |
| 1430 | case 'l': |
| 1431 | inet_aton(optarg, &local_addr); |
| 1432 | break; |
| 1433 | default: |
| 1434 | /* ignore */ |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1440 | static void signal_handler(int signal) |
| 1441 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1442 | switch (signal) { |
| 1443 | case SIGABRT: |
| 1444 | /* in case of abort, we want to obtain a talloc report |
| 1445 | * and then return to the caller, who will abort the process */ |
| 1446 | case SIGUSR1: |
| 1447 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1448 | break; |
| 1449 | default: |
| 1450 | break; |
| 1451 | } |
| 1452 | } |
| 1453 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1454 | static void sccp_close_unconfirmed(void *_data) |
| 1455 | { |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1456 | int destroyed = 0; |
| 1457 | struct bsc_connection *bsc, *bsc_tmp; |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1458 | struct nat_sccp_connection *conn, *tmp1; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1459 | struct timespec now; |
| 1460 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 1461 | |
| 1462 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 1463 | if (conn->has_remote_ref) |
| 1464 | continue; |
| 1465 | |
| 1466 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 1467 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 1468 | continue; |
| 1469 | |
Holger Hans Peter Freyther | 38a7ef1 | 2014-04-03 12:28:14 +0200 | [diff] [blame] | 1470 | LOGP(DNAT, LOGL_ERROR, |
| 1471 | "SCCP connection 0x%x/0x%x was never confirmed on bsc nr. %d\n", |
| 1472 | sccp_src_ref_to_int(&conn->real_ref), |
| 1473 | sccp_src_ref_to_int(&conn->patched_ref), |
| 1474 | conn->bsc->cfg->nr); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1475 | sccp_connection_destroy(conn); |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1476 | destroyed = 1; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Holger Hans Peter Freyther | 013dd14 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1479 | if (!destroyed) |
| 1480 | goto out; |
| 1481 | |
| 1482 | /* now close out any BSC */ |
| 1483 | llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry) |
| 1484 | bsc_maybe_close(bsc); |
| 1485 | |
| 1486 | out: |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1487 | osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1488 | } |
| 1489 | |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1490 | extern void *tall_msgb_ctx; |
| 1491 | extern void *tall_ctr_ctx; |
| 1492 | static void talloc_init_ctx() |
| 1493 | { |
| 1494 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 1495 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1496 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 1497 | } |
| 1498 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1499 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 1500 | |
| 1501 | static struct vty_app_info vty_info = { |
Harald Welte | 9af42c3 | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 1502 | .name = "OsmoBSCNAT", |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1503 | .version = PACKAGE_VERSION, |
| 1504 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 1505 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1506 | }; |
| 1507 | |
Holger Hans Peter Freyther | aaa3d08 | 2013-06-14 19:10:28 +0200 | [diff] [blame] | 1508 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1509 | int main(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1510 | { |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1511 | int rc; |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1512 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1513 | talloc_init_ctx(); |
Holger Hans Peter Freyther | 0937138 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 1514 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1515 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 1516 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1517 | nat = bsc_nat_alloc(); |
| 1518 | if (!nat) { |
| 1519 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 1520 | return -4; |
| 1521 | } |
| 1522 | |
Holger Hans Peter Freyther | 774d1d3 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1523 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1524 | if (!nat->mgcp_cfg) { |
| 1525 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 1526 | return -5; |
| 1527 | } |
| 1528 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1529 | vty_info.copyright = openbsc_copyright; |
| 1530 | vty_init(&vty_info); |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 1531 | logging_vty_add_cmds(&log_info); |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1532 | bsc_nat_vty_init(nat); |
| 1533 | |
| 1534 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1535 | /* parse options */ |
| 1536 | local_addr.s_addr = INADDR_ANY; |
| 1537 | handle_options(argc, argv); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1538 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1539 | rate_ctr_init(tall_bsc_ctx); |
| 1540 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1541 | /* init vty and parse */ |
Harald Welte | 6bcd303 | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 1542 | telnet_init(tall_bsc_ctx, NULL, OSMO_VTY_PORT_BSC_NAT); |
Pablo Neira Ayuso | a4e3694 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 1543 | if (mgcp_parse_config(config_file, nat->mgcp_cfg, MGCP_BSC_NAT) < 0) { |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1544 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1545 | return -3; |
| 1546 | } |
| 1547 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1548 | /* over rule the VTY config */ |
| 1549 | if (msc_ip) |
| 1550 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1551 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1552 | /* seed the PRNG */ |
| 1553 | srand(time(NULL)); |
| 1554 | |
Holger Hans Peter Freyther | aaa3d08 | 2013-06-14 19:10:28 +0200 | [diff] [blame] | 1555 | |
| 1556 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1557 | /* |
| 1558 | * Setup the MGCP code.. |
| 1559 | */ |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1560 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1561 | return -4; |
| 1562 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1563 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 1564 | nat->msc_con = bsc_msc_create(nat, &nat->dests); |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1565 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1566 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1567 | exit(1); |
| 1568 | } |
| 1569 | |
Holger Hans Peter Freyther | 9a01062 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 1570 | nat->ctrl = bsc_nat_controlif_setup(nat, 4250); |
Holger Hans Peter Freyther | 1427a20 | 2013-01-15 13:48:18 +0100 | [diff] [blame] | 1571 | if (!nat->ctrl) { |
| 1572 | fprintf(stderr, "Creating the control interface failed.\n"); |
| 1573 | exit(1); |
| 1574 | } |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1575 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 1576 | nat->msc_con->name = "main MSC"; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1577 | nat->msc_con->connection_loss = msc_connection_was_lost; |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 1578 | nat->msc_con->connected = msc_connection_connected; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1579 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1580 | 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] | 1581 | nat->msc_con->write_queue.bfd.data = nat->msc_con; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1582 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1583 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1584 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 5d0a027 | 2010-10-19 17:48:13 +0200 | [diff] [blame] | 1585 | 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] | 1586 | 5000, 0, ipaccess_listen_bsc_cb, nat); |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 1587 | if (rc != 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1588 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1589 | exit(1); |
| 1590 | } |
| 1591 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 1592 | rc = bsc_ussd_init(nat); |
| 1593 | if (rc != 0) { |
| 1594 | LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n"); |
| 1595 | exit(1); |
| 1596 | } |
| 1597 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1598 | signal(SIGABRT, &signal_handler); |
| 1599 | signal(SIGUSR1, &signal_handler); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1600 | osmo_init_ignore_signals(); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1601 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1602 | if (daemonize) { |
| 1603 | rc = osmo_daemonize(); |
| 1604 | if (rc < 0) { |
| 1605 | perror("Error during daemonize"); |
| 1606 | exit(1); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1610 | /* recycle timer */ |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1611 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1612 | sccp_close.cb = sccp_close_unconfirmed; |
| 1613 | sccp_close.data = NULL; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1614 | osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1615 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1616 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1617 | osmo_select_main(0); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1622 | |
| 1623 | /* Close all connections handed out to the USSD module */ |
Holger Hans Peter Freyther | 30f1702 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1624 | int bsc_ussd_close_connections(struct bsc_nat *nat) |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1625 | { |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1626 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1627 | llist_for_each_entry(con, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 99aaece | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1628 | if (con->con_local != NAT_CON_END_USSD) |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1629 | continue; |
| 1630 | if (!con->bsc) |
| 1631 | continue; |
| 1632 | |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 1633 | nat_send_clrc_bsc(con); |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1634 | nat_send_rlsd_bsc(con); |
| 1635 | } |
| 1636 | |
| 1637 | return 0; |
| 1638 | } |