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