Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2013 by On-Waves |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 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 | 9e2c5f5 | 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 | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 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 | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | #include <sys/socket.h> |
Holger Hans Peter Freyther | fce6971 | 2015-06-08 11:56:59 +0200 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 26 | #include <netinet/in.h> |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 27 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 30 | #include <errno.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 31 | #include <signal.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5aa25ae | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 34 | #include <time.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 35 | #include <unistd.h> |
Holger Hans Peter Freyther | fce6971 | 2015-06-08 11:56:59 +0200 | [diff] [blame] | 36 | #include <fcntl.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 37 | |
| 38 | #define _GNU_SOURCE |
| 39 | #include <getopt.h> |
| 40 | |
| 41 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 42 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 43 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 44 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 973dbae | 2015-04-04 20:47:03 +0200 | [diff] [blame] | 45 | #include <openbsc/bsc_msg_filter.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 46 | #include <openbsc/ipaccess.h> |
| 47 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | f961de1 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 48 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 49 | #include <openbsc/vty.h> |
Neels Hofmeyr | d48f057 | 2015-10-12 11:57:33 +0200 | [diff] [blame] | 50 | #include <openbsc/utils.h> |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 51 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 52 | #include <osmocom/ctrl/control_cmd.h> |
| 53 | #include <osmocom/ctrl/control_if.h> |
Neels Hofmeyr | fd1187e | 2016-02-24 00:28:13 +0100 | [diff] [blame] | 54 | #include <osmocom/ctrl/ports.h> |
Daniel Willmann | 6fc4a98 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 55 | |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 56 | #include <osmocom/crypt/auth.h> |
| 57 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 58 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 59 | #include <osmocom/core/talloc.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 60 | #include <osmocom/core/stats.h> |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 61 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 62 | #include <osmocom/gsm/tlv.h> |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 63 | #include <osmocom/gsm/gsm0808.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 64 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 65 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 66 | #include <osmocom/vty/telnet_interface.h> |
| 67 | #include <osmocom/vty/vty.h> |
Pablo Neira Ayuso | 739a566 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 68 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 69 | #include <osmocom/vty/stats.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 70 | #include <osmocom/vty/ports.h> |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 71 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 72 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 73 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 74 | #include <osmocom/abis/ipa.h> |
| 75 | |
Holger Hans Peter Freyther | 9f95ae8 | 2015-07-01 08:34:16 +0200 | [diff] [blame] | 76 | #include <openssl/rand.h> |
| 77 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 78 | #include "../../bscconfig.h" |
| 79 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 80 | #define SCCP_CLOSE_TIME 20 |
| 81 | #define SCCP_CLOSE_TIME_TIMEOUT 19 |
| 82 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 83 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 84 | static struct in_addr local_addr; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 85 | static struct osmo_fd bsc_listen; |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 86 | static const char *msc_ip = NULL; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 87 | static struct osmo_timer_list sccp_close; |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 88 | static int daemonize = 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 89 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 90 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 91 | "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n" |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 92 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 93 | "This is free software: you are free to change and redistribute it.\r\n" |
| 94 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 95 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 96 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 97 | static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int); |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 98 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 99 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 100 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 101 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 102 | { |
| 103 | struct bsc_config *conf; |
| 104 | |
| 105 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 106 | if (conf->nr == num) |
| 107 | return conf; |
| 108 | |
| 109 | return NULL; |
| 110 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 111 | |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 112 | static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) |
| 113 | { |
Holger Hans Peter Freyther | e8e41e6 | 2010-10-06 00:24:28 +0800 | [diff] [blame] | 114 | if (!con) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 115 | LOGP(DLINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n"); |
Holger Hans Peter Freyther | e8e41e6 | 2010-10-06 00:24:28 +0800 | [diff] [blame] | 116 | msgb_free(msg); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 121 | if (osmo_wqueue_enqueue(&con->write_queue, msg) != 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 122 | LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 123 | msgb_free(msg); |
| 124 | } |
| 125 | } |
| 126 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 127 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 128 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 129 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 130 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 131 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 132 | 0x00, 0x01, 0x31, |
| 133 | }; |
| 134 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 135 | bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 138 | static void send_ping(struct bsc_connection *bsc) |
| 139 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 140 | static const uint8_t id_ping[] = { |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 141 | IPAC_MSGT_PING, |
| 142 | }; |
| 143 | |
| 144 | bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS); |
| 145 | } |
| 146 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 147 | static void send_pong(struct bsc_connection *bsc) |
| 148 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 149 | static const uint8_t id_pong[] = { |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 150 | IPAC_MSGT_PONG, |
| 151 | }; |
| 152 | |
| 153 | bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS); |
| 154 | } |
| 155 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 156 | static void bsc_pong_timeout(void *_bsc) |
| 157 | { |
| 158 | struct bsc_connection *bsc = _bsc; |
| 159 | |
| 160 | LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr); |
| 161 | bsc_close_connection(bsc); |
| 162 | } |
| 163 | |
| 164 | static void bsc_ping_timeout(void *_bsc) |
| 165 | { |
| 166 | struct bsc_connection *bsc = _bsc; |
| 167 | |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 168 | if (bsc->nat->ping_timeout < 0) |
| 169 | return; |
| 170 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 171 | send_ping(bsc); |
| 172 | |
| 173 | /* send another ping in 20 seconds */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 174 | osmo_timer_schedule(&bsc->ping_timeout, bsc->nat->ping_timeout, 0); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 175 | |
| 176 | /* also start a pong timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 177 | osmo_timer_schedule(&bsc->pong_timeout, bsc->nat->pong_timeout, 0); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void start_ping_pong(struct bsc_connection *bsc) |
| 181 | { |
| 182 | bsc->pong_timeout.data = bsc; |
| 183 | bsc->pong_timeout.cb = bsc_pong_timeout; |
| 184 | bsc->ping_timeout.data = bsc; |
| 185 | bsc->ping_timeout.cb = bsc_ping_timeout; |
| 186 | |
| 187 | bsc_ping_timeout(bsc); |
| 188 | } |
| 189 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 190 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 191 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 192 | static const uint8_t id_ack[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 193 | IPAC_MSGT_ID_ACK |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 196 | bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 199 | static void send_id_req(struct bsc_nat *nat, struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 200 | { |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 201 | static const uint8_t s_id_req[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 202 | IPAC_MSGT_ID_GET, |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 203 | 0x01, IPAC_IDTAG_UNIT, |
| 204 | 0x01, IPAC_IDTAG_MACADDR, |
| 205 | 0x01, IPAC_IDTAG_LOCATION1, |
| 206 | 0x01, IPAC_IDTAG_LOCATION2, |
| 207 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 208 | 0x01, IPAC_IDTAG_SWVERSION, |
| 209 | 0x01, IPAC_IDTAG_UNITNAME, |
| 210 | 0x01, IPAC_IDTAG_SERNR, |
| 211 | }; |
| 212 | |
Holger Hans Peter Freyther | 9f95ae8 | 2015-07-01 08:34:16 +0200 | [diff] [blame] | 213 | uint8_t *mrand; |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 214 | uint8_t id_req[sizeof(s_id_req) + (2+16)]; |
| 215 | uint8_t *buf = &id_req[sizeof(s_id_req)]; |
| 216 | |
| 217 | /* copy the static data */ |
| 218 | memcpy(id_req, s_id_req, sizeof(s_id_req)); |
| 219 | |
| 220 | /* put the RAND with length, tag, value */ |
| 221 | buf = v_put(buf, 0x11); |
| 222 | buf = v_put(buf, 0x23); |
| 223 | mrand = bsc->last_rand; |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 224 | |
Holger Hans Peter Freyther | 9f95ae8 | 2015-07-01 08:34:16 +0200 | [diff] [blame] | 225 | if (RAND_bytes(mrand, 16) != 1) |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 226 | goto failed_random; |
Holger Hans Peter Freyther | 9f95ae8 | 2015-07-01 08:34:16 +0200 | [diff] [blame] | 227 | |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 228 | memcpy(buf, mrand, 16); |
| 229 | buf += 16; |
| 230 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 231 | bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 232 | return; |
| 233 | |
| 234 | failed_random: |
| 235 | /* the timeout will trigger and close this connection */ |
| 236 | LOGP(DNAT, LOGL_ERROR, "Failed to read from urandom.\n"); |
| 237 | return; |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 240 | static struct msgb *nat_create_rlsd(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 241 | { |
| 242 | struct sccp_connection_released *rel; |
| 243 | struct msgb *msg; |
| 244 | |
| 245 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 246 | if (!msg) { |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 247 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate released.\n"); |
| 248 | return NULL; |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 252 | rel = (struct sccp_connection_released *) msg->l2h; |
| 253 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 254 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 255 | rel->destination_local_reference = conn->remote_ref; |
| 256 | rel->source_local_reference = conn->patched_ref; |
| 257 | |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 258 | return msg; |
| 259 | } |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 260 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 261 | static void nat_send_rlsd_ussd(struct bsc_nat *nat, struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 262 | { |
| 263 | struct msgb *msg; |
| 264 | |
| 265 | if (!nat->ussd_con) |
| 266 | return; |
| 267 | |
| 268 | msg = nat_create_rlsd(conn); |
| 269 | if (!msg) |
| 270 | return; |
| 271 | |
| 272 | bsc_do_write(&nat->ussd_con->queue, msg, IPAC_PROTO_SCCP); |
| 273 | } |
| 274 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 275 | static void nat_send_rlsd_msc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 276 | { |
| 277 | struct msgb *msg; |
| 278 | |
| 279 | msg = nat_create_rlsd(conn); |
| 280 | if (!msg) |
| 281 | return; |
| 282 | |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 283 | ipa_prepend_header(msg, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 284 | queue_for_msc(conn->msc_con, msg); |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 287 | static void nat_send_rlsd_bsc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 288 | { |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 289 | struct msgb *msg; |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 290 | struct sccp_connection_released *rel; |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 291 | |
| 292 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 293 | if (!msg) { |
| 294 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 299 | rel = (struct sccp_connection_released *) msg->l2h; |
| 300 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 301 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 302 | rel->destination_local_reference = conn->real_ref; |
| 303 | rel->source_local_reference = conn->remote_ref; |
| 304 | |
| 305 | bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP); |
| 306 | } |
| 307 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 308 | static struct msgb *nat_creat_clrc(struct nat_sccp_connection *conn, uint8_t cause) |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 309 | { |
| 310 | struct msgb *msg; |
| 311 | struct msgb *sccp; |
| 312 | |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 313 | msg = gsm0808_create_clear_command(cause); |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 314 | if (!msg) { |
| 315 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 316 | return NULL; |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len); |
| 320 | if (!sccp) { |
| 321 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n"); |
| 322 | msgb_free(msg); |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 323 | return NULL; |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | msgb_free(msg); |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 327 | return sccp; |
| 328 | } |
| 329 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 330 | static int nat_send_clrc_bsc(struct nat_sccp_connection *conn) |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 331 | { |
| 332 | struct msgb *sccp; |
| 333 | |
| 334 | sccp = nat_creat_clrc(conn, 0x20); |
| 335 | if (!sccp) |
| 336 | return -1; |
| 337 | return bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 340 | static void nat_send_rlc(struct bsc_msc_connection *msc_con, |
| 341 | struct sccp_source_reference *src, |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 342 | struct sccp_source_reference *dst) |
| 343 | { |
| 344 | struct sccp_connection_release_complete *rlc; |
| 345 | struct msgb *msg; |
| 346 | |
| 347 | msg = msgb_alloc_headroom(4096, 128, "rlc"); |
| 348 | if (!msg) { |
Holger Hans Peter Freyther | ff98b3c | 2013-09-03 11:25:16 +0200 | [diff] [blame] | 349 | LOGP(DNAT, LOGL_ERROR, "Failed to sccp rlc.\n"); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
| 353 | msg->l2h = msgb_put(msg, sizeof(*rlc)); |
| 354 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 355 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 356 | rlc->destination_local_reference = *dst; |
| 357 | rlc->source_local_reference = *src; |
| 358 | |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 359 | ipa_prepend_header(msg, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 360 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 361 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 364 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 365 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 366 | static const uint8_t mgcp_reset[] = { |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 367 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 368 | }; |
| 369 | |
| 370 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 371 | } |
| 372 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 373 | void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg) |
| 374 | { |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 375 | ipa_prepend_header(msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 376 | queue_for_msc(nat->msc_con, msg); |
| 377 | } |
| 378 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 379 | /* |
| 380 | * Below is the handling of messages coming |
| 381 | * from the MSC and need to be forwarded to |
| 382 | * a real BSC. |
| 383 | */ |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 384 | static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 385 | { |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 386 | if (msc_con->first_contact) |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 387 | return; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 388 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 389 | msc_con->first_contact = 1; |
| 390 | msc_send_reset(msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 393 | static void send_id_get_response(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 394 | { |
Holger Hans Peter Freyther | e2ac6b7 | 2015-06-08 18:02:10 +0200 | [diff] [blame] | 395 | struct msgb *msg = bsc_msc_id_get_resp(0, nat->token, NULL, 0); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 396 | if (!msg) |
| 397 | return; |
| 398 | |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 399 | ipa_prepend_header(msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 400 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Currently we are lacking refcounting so we need to copy each message. |
| 405 | */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 406 | static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int proto) |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 407 | { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 408 | struct msgb *msg; |
| 409 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 410 | if (length > 4096 - 128) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 411 | LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n"); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 415 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 416 | if (!msg) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 417 | LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 421 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 422 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 423 | |
| 424 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 427 | /* |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 428 | * Update the release statistics |
| 429 | */ |
| 430 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal) |
| 431 | { |
| 432 | if (!bsc->cfg) { |
| 433 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated."); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | if (filter >= 0) { |
| 438 | LOGP(DNAT, LOGL_ERROR, "Connection was not rejected"); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | if (filter == -1) |
| 443 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]); |
| 444 | else if (normal) |
| 445 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]); |
| 446 | else |
| 447 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]); |
| 448 | } |
| 449 | |
| 450 | /* |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 451 | * Release an established connection. We will have to release it to the BSC |
| 452 | * and to the network and we do it the following way. |
| 453 | * 1.) Give up on the MSC side |
| 454 | * 1.1) Send a RLSD message, it is a bit non standard but should work, we |
| 455 | * ignore the RLC... we might complain about it. Other options would |
| 456 | * be to send a Release Request, handle the Release Complete.. |
| 457 | * 1.2) Mark the data structure to be con_local and wait for 2nd |
| 458 | * |
| 459 | * 2.) Give up on the BSC side |
| 460 | * 2.1) Depending on the con type reject the service, or just close it |
| 461 | */ |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 462 | static void bsc_send_con_release(struct bsc_connection *bsc, |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 463 | struct nat_sccp_connection *con, |
Holger Hans Peter Freyther | c36a6d5 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 464 | struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 465 | { |
| 466 | struct msgb *rlsd; |
| 467 | /* 1. release the network */ |
| 468 | rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref, |
| 469 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 470 | if (!rlsd) |
| 471 | LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n"); |
| 472 | else { |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 473 | ipa_prepend_header(rlsd, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 474 | queue_for_msc(con->msc_con, rlsd); |
| 475 | } |
Holger Hans Peter Freyther | e5d3249 | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 476 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | eea5a1b | 2010-09-16 06:41:09 +0800 | [diff] [blame] | 477 | con->msc_con = NULL; |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 478 | |
| 479 | /* 2. release the BSC side */ |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 480 | if (con->filter_state.con_type == FLT_CON_TYPE_LU) { |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 481 | struct msgb *payload, *udt; |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 482 | payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 483 | |
| 484 | if (payload) { |
| 485 | gsm0808_prepend_dtap_header(payload, 0); |
| 486 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 487 | if (udt) |
| 488 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 489 | else |
| 490 | LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n"); |
| 491 | |
| 492 | msgb_free(payload); |
| 493 | } else { |
| 494 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n"); |
| 495 | } |
| 496 | } |
| 497 | |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 498 | nat_send_clrc_bsc(con); |
| 499 | |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 500 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 501 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 502 | if (!rlsd) { |
| 503 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n"); |
| 504 | sccp_connection_destroy(con); |
| 505 | return; |
| 506 | } |
| 507 | |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 508 | con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT; |
Holger Hans Peter Freyther | ac2763b | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 509 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 510 | } |
| 511 | |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 512 | static void bsc_send_con_refuse(struct bsc_connection *bsc, |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 513 | struct bsc_nat_parsed *parsed, int con_type, |
Holger Hans Peter Freyther | c36a6d5 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 514 | struct bsc_filter_reject_cause *cause) |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 515 | { |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 516 | struct msgb *payload; |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 517 | struct msgb *refuse; |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 518 | |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 519 | if (con_type == FLT_CON_TYPE_LU) |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 520 | payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 521 | else if (con_type == FLT_CON_TYPE_CM_SERV_REQ || con_type == FLT_CON_TYPE_SSA) |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 522 | payload = gsm48_create_mm_serv_rej(cause->cm_reject_cause); |
Holger Hans Peter Freyther | 8d7b10e | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 523 | else { |
| 524 | LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); |
| 525 | payload = NULL; |
| 526 | } |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 527 | |
| 528 | /* |
| 529 | * Some BSCs do not handle the payload inside a SCCP CREF msg |
| 530 | * so we will need to: |
| 531 | * 1.) Allocate a local connection and mark it as local.. |
| 532 | * 2.) queue data for downstream.. and the RLC should delete everything |
| 533 | */ |
| 534 | if (payload) { |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 535 | struct msgb *cc, *udt, *clear, *rlsd; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 536 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 537 | con = create_sccp_src_ref(bsc, parsed); |
| 538 | if (!con) |
| 539 | goto send_refuse; |
| 540 | |
| 541 | /* declare it local and assign a unique remote_ref */ |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 542 | con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT; |
Holger Hans Peter Freyther | e5d3249 | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 543 | con->con_local = NAT_CON_END_LOCAL; |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 544 | con->has_remote_ref = 1; |
| 545 | con->remote_ref = con->patched_ref; |
| 546 | |
| 547 | /* 1. create a confirmation */ |
| 548 | cc = sccp_create_cc(&con->remote_ref, &con->real_ref); |
| 549 | if (!cc) |
| 550 | goto send_refuse; |
| 551 | |
| 552 | /* 2. create the DT1 */ |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 553 | gsm0808_prepend_dtap_header(payload, 0); |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 554 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 555 | if (!udt) { |
| 556 | msgb_free(cc); |
| 557 | goto send_refuse; |
| 558 | } |
| 559 | |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 560 | /* 3. send a Clear Command */ |
| 561 | clear = nat_creat_clrc(con, 0x20); |
| 562 | if (!clear) { |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 563 | msgb_free(cc); |
| 564 | msgb_free(udt); |
| 565 | goto send_refuse; |
| 566 | } |
| 567 | |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 568 | /* 4. send a RLSD */ |
| 569 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 570 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 571 | if (!rlsd) { |
| 572 | msgb_free(cc); |
| 573 | msgb_free(udt); |
| 574 | msgb_free(clear); |
| 575 | goto send_refuse; |
| 576 | } |
| 577 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 578 | bsc_write(bsc, cc, IPAC_PROTO_SCCP); |
| 579 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 43eb1a3 | 2010-10-29 17:05:28 +0200 | [diff] [blame] | 580 | bsc_write(bsc, clear, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 581 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 582 | msgb_free(payload); |
| 583 | return; |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 584 | } |
| 585 | |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 586 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 587 | send_refuse: |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 588 | if (payload) |
| 589 | msgb_free(payload); |
| 590 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 591 | refuse = sccp_create_refuse(parsed->src_local_ref, |
| 592 | SCCP_REFUSAL_SCCP_FAILURE, NULL, 0); |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 593 | if (!refuse) { |
| 594 | LOGP(DNAT, LOGL_ERROR, |
| 595 | "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n", |
| 596 | sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | bsc_write(bsc, refuse, IPAC_PROTO_SCCP); |
| 601 | } |
| 602 | |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 603 | static void bsc_nat_send_paging(struct bsc_connection *bsc, struct msgb *msg) |
| 604 | { |
| 605 | if (bsc->cfg->forbid_paging) { |
| 606 | LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), IPAC_PROTO_SCCP); |
| 611 | } |
| 612 | |
| 613 | static void bsc_nat_handle_paging(struct bsc_nat *nat, struct msgb *msg) |
| 614 | { |
| 615 | struct bsc_connection *bsc; |
| 616 | const uint8_t *paging_start; |
Holger Hans Peter Freyther | 0dc36f1 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 617 | int paging_length, i, ret; |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 618 | |
Holger Hans Peter Freyther | 0dc36f1 | 2011-05-02 20:03:17 +0200 | [diff] [blame] | 619 | ret = bsc_nat_find_paging(msg, &paging_start, &paging_length); |
| 620 | if (ret != 0) { |
| 621 | LOGP(DNAT, LOGL_ERROR, "Could not parse paging message: %d\n", ret); |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 622 | return; |
| 623 | } |
| 624 | |
| 625 | /* This is quite expensive now */ |
| 626 | for (i = 0; i < paging_length; i += 2) { |
| 627 | unsigned int _lac = ntohs(*(unsigned int *) &paging_start[i]); |
| 628 | unsigned int paged = 0; |
| 629 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 630 | if (!bsc->cfg) |
| 631 | continue; |
| 632 | if (!bsc->authenticated) |
| 633 | continue; |
| 634 | if (!bsc_config_handles_lac(bsc->cfg, _lac)) |
| 635 | continue; |
| 636 | bsc_nat_send_paging(bsc, msg); |
| 637 | paged += 1; |
| 638 | } |
| 639 | |
| 640 | /* highlight a possible config issue */ |
| 641 | if (paged == 0) |
| 642 | LOGP(DNAT, LOGL_ERROR, "No BSC for LAC %d/0x%d\n", _lac, _lac); |
| 643 | |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 648 | /* |
Neels Hofmeyr | fd1187e | 2016-02-24 00:28:13 +0100 | [diff] [blame] | 649 | * Update the auth status. This can be either a CIPHER MODE COMMAND or |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 650 | * a CM Serivce Accept. Maybe also LU Accept or such in the future. |
| 651 | */ |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 652 | static void update_con_authorize(struct nat_sccp_connection *con, |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 653 | struct bsc_nat_parsed *parsed, |
| 654 | struct msgb *msg) |
| 655 | { |
| 656 | if (!con) |
| 657 | return; |
| 658 | if (con->authorized) |
| 659 | return; |
| 660 | |
| 661 | if (parsed->bssap == BSSAP_MSG_BSS_MANAGEMENT && |
| 662 | parsed->gsm_type == BSS_MAP_MSG_CIPHER_MODE_CMD) { |
| 663 | con->authorized = 1; |
| 664 | } else if (parsed->bssap == BSSAP_MSG_DTAP) { |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 665 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 666 | uint32_t len; |
| 667 | struct gsm48_hdr *hdr48; |
| 668 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 669 | if (!hdr48) |
| 670 | return; |
| 671 | |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 672 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 673 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 674 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 675 | msg_type == GSM48_MT_MM_CM_SERV_ACC) |
| 676 | con->authorized = 1; |
| 677 | } |
| 678 | } |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 679 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 680 | static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 681 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 682 | struct nat_sccp_connection *con = NULL; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 683 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 684 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 685 | int proto; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 686 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 687 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 688 | parsed = bsc_nat_parse(msg); |
| 689 | if (!parsed) { |
| 690 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 691 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 692 | } |
| 693 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 694 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 695 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 696 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 697 | proto = parsed->ipa_proto; |
| 698 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 699 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 700 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 701 | switch (parsed->sccp_type) { |
| 702 | case SCCP_MSG_TYPE_UDT: |
| 703 | /* forward UDT messages to every BSC */ |
| 704 | goto send_to_all; |
| 705 | break; |
| 706 | case SCCP_MSG_TYPE_RLSD: |
| 707 | case SCCP_MSG_TYPE_CREF: |
| 708 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 709 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 710 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 711 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 712 | osmo_counter_inc(nat->stats.sccp.calls); |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 713 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 714 | if (con) { |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 715 | struct rate_ctr_group *ctrg; |
| 716 | ctrg = con->bsc->cfg->stats.ctrg; |
| 717 | rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]); |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 718 | if (bsc_mgcp_assign_patch(con, msg) != 0) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 719 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 720 | } else |
| 721 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
Holger Hans Peter Freyther | 0b03f16 | 2011-04-21 17:13:51 +0200 | [diff] [blame] | 722 | } else if (con && con->con_local == NAT_CON_END_USSD && |
| 723 | parsed->gsm_type == BSS_MAP_MSG_CLEAR_CMD) { |
| 724 | LOGP(DNAT, LOGL_NOTICE, "Clear Command for USSD Connection. Ignoring.\n"); |
| 725 | con = NULL; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 726 | } |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 727 | break; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 728 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 729 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 730 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 731 | goto exit; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 732 | break; |
| 733 | case SCCP_MSG_TYPE_RLC: |
| 734 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 735 | goto exit; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 736 | break; |
| 737 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 738 | /* MSC never opens a SCCP connection, fall through */ |
| 739 | default: |
| 740 | goto exit; |
| 741 | } |
Holger Hans Peter Freyther | f464ea5 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 742 | |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 743 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 744 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 745 | /* Exchange src/dest for the reply */ |
Holger Hans Peter Freyther | 1b9902c | 2013-09-03 14:35:33 +0200 | [diff] [blame] | 746 | nat_send_rlc(msc_con, &parsed->original_dest_ref, |
| 747 | parsed->src_local_ref); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 748 | } else if (!con) |
Holger Hans Peter Freyther | 5f10c6d | 2010-05-15 19:13:52 +0800 | [diff] [blame] | 749 | LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x from the MSC.\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Pablo Neira Ayuso | 0a244b4 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 752 | if (!con) { |
| 753 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 754 | return -1; |
Pablo Neira Ayuso | 0a244b4 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 755 | } |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 756 | if (!con->bsc->authenticated) { |
Pablo Neira Ayuso | 0a244b4 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 757 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 758 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 759 | return -1; |
| 760 | } |
| 761 | |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 762 | update_con_authorize(con, parsed, msg); |
Pablo Neira Ayuso | 0a244b4 | 2013-05-12 20:50:28 +0200 | [diff] [blame] | 763 | talloc_free(parsed); |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 764 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 765 | bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 766 | return 0; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 767 | |
| 768 | send_to_all: |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 769 | /* |
| 770 | * Filter Paging from the network. We do not want to send a PAGING |
| 771 | * Command to every BSC in our network. We will analys the PAGING |
| 772 | * message and then send it to the authenticated messages... |
| 773 | */ |
| 774 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 775 | bsc_nat_handle_paging(nat, msg); |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 776 | goto exit; |
| 777 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 778 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 779 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 780 | if (!bsc->authenticated) |
| 781 | continue; |
| 782 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 783 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 784 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 785 | |
| 786 | exit: |
| 787 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 788 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 791 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 792 | { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 793 | struct bsc_connection *bsc, *tmp; |
| 794 | |
| 795 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 796 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 797 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 798 | |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 799 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 800 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 801 | } |
| 802 | |
Holger Hans Peter Freyther | a99c5b9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 803 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 804 | { |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 805 | osmo_counter_inc(nat->stats.msc.reconn); |
Holger Hans Peter Freyther | a99c5b9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 806 | } |
| 807 | |
Holger Hans Peter Freyther | 02aee14 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 808 | static void msc_send_reset(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 809 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 810 | static const uint8_t reset[] = { |
Holger Hans Peter Freyther | 7cab166 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 811 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 812 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 813 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 814 | 0x01, 0x20 |
| 815 | }; |
| 816 | |
| 817 | struct msgb *msg; |
| 818 | |
| 819 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 820 | if (!msg) { |
| 821 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 826 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 827 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 828 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 829 | |
| 830 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 831 | } |
| 832 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 833 | static int ipaccess_msc_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 834 | { |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 835 | struct bsc_msc_connection *msc_con; |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 836 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 837 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 838 | int ret; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 839 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 840 | msc_con = (struct bsc_msc_connection *) bfd->data; |
| 841 | |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 842 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &msc_con->pending_msg); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 843 | if (ret <= 0) { |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 844 | if (ret == -EAGAIN) |
| 845 | return 0; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 846 | if (ret == 0) |
Holger Hans Peter Freyther | c1a8687 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 847 | LOGP(DNAT, LOGL_FATAL, |
| 848 | "The connection the MSC(%s) was lost, exiting\n", |
| 849 | msc_con->name); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 850 | else |
Holger Hans Peter Freyther | c1a8687 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 851 | LOGP(DNAT, LOGL_ERROR, |
| 852 | "Failed to parse ip access message on %s: %d\n", |
| 853 | msc_con->name, ret); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 854 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 855 | bsc_msc_lost(msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 856 | return -1; |
| 857 | } |
| 858 | |
Holger Hans Peter Freyther | c1a8687 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 859 | LOGP(DNAT, LOGL_DEBUG, |
| 860 | "MSG from MSC(%s): %s proto: %d\n", msc_con->name, |
| 861 | osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 862 | |
| 863 | /* handle base message handling */ |
| 864 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 865 | |
| 866 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 867 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 868 | ipa_ccm_rcvmsg_base(msg, bfd); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 869 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 870 | initialize_msc_if_needed(msc_con); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 871 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 872 | send_id_get_response(msc_con); |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 873 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 874 | forward_sccp_to_bts(msc_con, msg); |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 875 | } else if (hh->proto == IPAC_PROTO_MGCP_OLD) { |
| 876 | bsc_nat_handle_mgcp(nat, msg); |
| 877 | } |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 878 | |
Holger Hans Peter Freyther | aad68b5 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 879 | msgb_free(msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 880 | return 0; |
| 881 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 882 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 883 | static int ipaccess_msc_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 884 | { |
| 885 | int rc; |
| 886 | rc = write(bfd->fd, msg->data, msg->len); |
| 887 | |
| 888 | if (rc != msg->len) { |
| 889 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 890 | return -1; |
| 891 | } |
| 892 | |
| 893 | return rc; |
| 894 | } |
| 895 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 896 | /* |
| 897 | * Below is the handling of messages coming |
| 898 | * from the BSC and need to be forwarded to |
| 899 | * a real BSC. |
| 900 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * Remove the connection from the connections list, |
| 904 | * remove it from the patching of SCCP header lists |
| 905 | * as well. Maybe in the future even close connection.. |
| 906 | */ |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 907 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 908 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 909 | struct nat_sccp_connection *sccp_patch, *tmp; |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 910 | struct bsc_cmd_list *cmd_entry, *cmd_tmp; |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 911 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 912 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 913 | /* stop the timeout timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 914 | osmo_timer_del(&connection->id_timeout); |
| 915 | osmo_timer_del(&connection->ping_timeout); |
| 916 | osmo_timer_del(&connection->pong_timeout); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 917 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 918 | if (connection->cfg) |
| 919 | ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP]; |
| 920 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 921 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 922 | llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 923 | if (sccp_patch->bsc != connection) |
| 924 | continue; |
| 925 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 926 | if (ctr) |
| 927 | rate_ctr_inc(ctr); |
Holger Hans Peter Freyther | 91b9a45 | 2011-04-16 17:01:47 +0200 | [diff] [blame] | 928 | if (sccp_patch->has_remote_ref) { |
| 929 | if (sccp_patch->con_local == NAT_CON_END_MSC) |
| 930 | nat_send_rlsd_msc(sccp_patch); |
| 931 | else if (sccp_patch->con_local == NAT_CON_END_USSD) |
| 932 | nat_send_rlsd_ussd(nat, sccp_patch); |
| 933 | } |
| 934 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 935 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 938 | /* Reply to all outstanding commands */ |
| 939 | llist_for_each_entry_safe(cmd_entry, cmd_tmp, &connection->cmd_pending, list_entry) { |
| 940 | cmd_entry->cmd->type = CTRL_TYPE_ERROR; |
| 941 | cmd_entry->cmd->reply = "BSC closed the connection"; |
| 942 | ctrl_cmd_send(&cmd_entry->ccon->write_queue, cmd_entry->cmd); |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 943 | bsc_nat_ctrl_del_pending(cmd_entry); |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 944 | } |
| 945 | |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 946 | /* close endpoints allocated by this BSC */ |
| 947 | bsc_mgcp_clear_endpoints_for(connection); |
| 948 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 949 | osmo_fd_unregister(&connection->write_queue.bfd); |
Holger Hans Peter Freyther | e464ed4 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 950 | close(connection->write_queue.bfd.fd); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 951 | osmo_wqueue_clear(&connection->write_queue); |
Holger Hans Peter Freyther | e464ed4 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 952 | llist_del(&connection->list_entry); |
| 953 | |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 954 | if (connection->pending_msg) { |
| 955 | LOGP(DNAT, LOGL_ERROR, "Dropping partial message on connection %d.\n", |
Jacob Erlbeck | 4ace424 | 2014-04-03 12:10:24 +0200 | [diff] [blame] | 956 | connection->cfg ? connection->cfg->nr : -1); |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 957 | msgb_free(connection->pending_msg); |
| 958 | connection->pending_msg = NULL; |
| 959 | } |
| 960 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 961 | talloc_free(connection); |
| 962 | } |
| 963 | |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 964 | static void bsc_maybe_close(struct bsc_connection *bsc) |
| 965 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 966 | struct nat_sccp_connection *sccp; |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 967 | if (!bsc->nat->blocked) |
| 968 | return; |
| 969 | |
| 970 | /* are there any connections left */ |
| 971 | llist_for_each_entry(sccp, &bsc->nat->sccp_connections, list_entry) |
| 972 | if (sccp->bsc == bsc) |
| 973 | return; |
| 974 | |
| 975 | /* nothing left, close the BSC */ |
| 976 | LOGP(DNAT, LOGL_NOTICE, "Cleaning up BSC %d in blocking mode.\n", |
| 977 | bsc->cfg ? bsc->cfg->nr : -1); |
| 978 | bsc_close_connection(bsc); |
| 979 | } |
| 980 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 981 | static void ipaccess_close_bsc(void *data) |
| 982 | { |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 983 | struct sockaddr_in sock; |
| 984 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 985 | struct bsc_connection *conn = data; |
| 986 | |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 987 | |
| 988 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 989 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 990 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 991 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 992 | } |
| 993 | |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 994 | static int verify_key(struct bsc_connection *conn, struct bsc_config *conf, const uint8_t *key, const int keylen) |
| 995 | { |
| 996 | struct osmo_auth_vector vec; |
| 997 | |
| 998 | struct osmo_sub_auth_data auth = { |
| 999 | .type = OSMO_AUTH_TYPE_GSM, |
| 1000 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 1001 | }; |
| 1002 | |
| 1003 | /* expect a specific keylen */ |
| 1004 | if (keylen != 8) { |
| 1005 | LOGP(DNAT, LOGL_ERROR, "Key length is wrong: %d for bsc nr %d\n", |
| 1006 | keylen, conf->nr); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | memcpy(auth.u.umts.opc, conf->key, 16); |
| 1011 | memcpy(auth.u.umts.k, conf->key, 16); |
| 1012 | memset(auth.u.umts.amf, 0, 2); |
| 1013 | auth.u.umts.sqn = 0; |
| 1014 | |
| 1015 | memset(&vec, 0, sizeof(vec)); |
| 1016 | osmo_auth_gen_vec(&vec, &auth, conn->last_rand); |
| 1017 | |
| 1018 | if (vec.res_len != 8) { |
| 1019 | LOGP(DNAT, LOGL_ERROR, "Res length is wrong: %d for bsc nr %d\n", |
Neels Hofmeyr | e57e0fa | 2015-09-23 19:02:36 +0200 | [diff] [blame] | 1020 | vec.res_len, conf->nr); |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | return constant_time_cmp(vec.res, key, 8) == 0; |
| 1025 | } |
| 1026 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1027 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 1028 | { |
| 1029 | struct bsc_config *conf; |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1030 | const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | 8a8df80 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 1031 | int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 1032 | const uint8_t *xres = TLVP_VAL(tvp, 0x24); |
| 1033 | const int xlen = TLVP_LEN(tvp, 0x24); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1034 | |
Holger Hans Peter Freyther | 5cdcfa6 | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 1035 | if (bsc->cfg) { |
| 1036 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 1037 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
Holger Hans Peter Freyther | d34adb2 | 2013-03-19 20:50:36 +0100 | [diff] [blame] | 1041 | if (len <= 0) { |
| 1042 | LOGP(DNAT, LOGL_ERROR, "Token with length zero on fd: %d\n", |
| 1043 | bsc->write_queue.bfd.fd); |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | if (token[len - 1] != '\0') { |
| 1048 | LOGP(DNAT, LOGL_ERROR, "Token not null terminated on fd: %d\n", |
| 1049 | bsc->write_queue.bfd.fd); |
| 1050 | return; |
| 1051 | } |
| 1052 | |
Holger Hans Peter Freyther | 8a8df80 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 1053 | /* |
| 1054 | * New systems have fixed the structure of the message but |
| 1055 | * we need to support old ones too. |
| 1056 | */ |
| 1057 | if (len >= 2 && token[len - 2] == '\0') |
| 1058 | len -= 1; |
| 1059 | |
Holger Hans Peter Freyther | aa0cecd | 2015-06-08 09:54:45 +0200 | [diff] [blame] | 1060 | conf = bsc_config_by_token(bsc->nat, token, len); |
| 1061 | if (!conf) { |
| 1062 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 8a8df80 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 1063 | "No bsc found for token '%s' len %d on fd: %d.\n", token, |
| 1064 | bsc->write_queue.bfd.fd, len); |
Holger Hans Peter Freyther | 0363d1b | 2015-06-08 11:55:02 +0200 | [diff] [blame] | 1065 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | aa0cecd | 2015-06-08 09:54:45 +0200 | [diff] [blame] | 1066 | return; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1067 | } |
Holger Hans Peter Freyther | c615c26 | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 1068 | |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 1069 | /* We have set a key and expect it to be present */ |
| 1070 | if (conf->key_present && !verify_key(bsc, conf, xres, xlen - 1)) { |
| 1071 | LOGP(DNAT, LOGL_ERROR, |
| 1072 | "Wrong key for bsc nr %d fd: %d.\n", conf->nr, |
| 1073 | bsc->write_queue.bfd.fd); |
| 1074 | bsc_close_connection(bsc); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
Holger Hans Peter Freyther | aa0cecd | 2015-06-08 09:54:45 +0200 | [diff] [blame] | 1078 | rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]); |
| 1079 | bsc->authenticated = 1; |
| 1080 | bsc->cfg = conf; |
| 1081 | osmo_timer_del(&bsc->id_timeout); |
| 1082 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n", |
| 1083 | conf->nr, bsc->write_queue.bfd.fd); |
| 1084 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1087 | static void handle_con_stats(struct nat_sccp_connection *con) |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 1088 | { |
| 1089 | struct rate_ctr_group *ctrg; |
| 1090 | int id = bsc_conn_type_to_ctr(con); |
| 1091 | |
| 1092 | if (id == -1) |
| 1093 | return; |
| 1094 | |
| 1095 | if (!con->bsc || !con->bsc->cfg) |
| 1096 | return; |
| 1097 | |
| 1098 | ctrg = con->bsc->cfg->stats.ctrg; |
| 1099 | rate_ctr_inc(&ctrg->ctr[id]); |
| 1100 | } |
| 1101 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1102 | static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1103 | { |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 1104 | int con_filter = 0; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1105 | char *imsi = NULL; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1106 | struct bsc_msc_connection *con_msc = NULL; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1107 | struct bsc_connection *con_bsc = NULL; |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 1108 | int con_type; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1109 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | c36a6d5 | 2015-04-05 16:55:28 +0200 | [diff] [blame] | 1110 | struct bsc_filter_reject_cause cause; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1111 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1112 | /* Parse and filter messages */ |
| 1113 | parsed = bsc_nat_parse(msg); |
| 1114 | if (!parsed) { |
| 1115 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | d7657ff | 2010-03-30 07:35:46 +0200 | [diff] [blame] | 1116 | msgb_free(msg); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
| 1119 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 1120 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1121 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 1122 | |
Holger Hans Peter Freyther | bbb9d39 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 1123 | /* |
| 1124 | * check authentication after filtering to not reject auth |
| 1125 | * responses coming from the BSC. We have to make sure that |
| 1126 | * nothing from the exit path will forward things to the MSC |
| 1127 | */ |
| 1128 | if (!bsc->authenticated) { |
| 1129 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 1130 | msgb_free(msg); |
| 1131 | return -1; |
| 1132 | } |
| 1133 | |
| 1134 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1135 | /* modify the SCCP entries */ |
| 1136 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 3837f99 | 2010-09-15 00:38:54 +0800 | [diff] [blame] | 1137 | int filter; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1138 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1139 | switch (parsed->sccp_type) { |
| 1140 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1141 | memset(&cause, 0, sizeof(cause)); |
| 1142 | filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, |
| 1143 | &con_type, &imsi, &cause); |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1144 | if (filter < 0) { |
Holger Hans Peter Freyther | 7c00983 | 2015-04-05 13:45:53 +0200 | [diff] [blame] | 1145 | if (imsi) |
| 1146 | bsc_nat_inform_reject(bsc, imsi); |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1147 | bsc_stat_reject(filter, bsc, 0); |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1148 | goto exit3; |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Holger Hans Peter Freyther | fa20c94 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 1151 | if (!create_sccp_src_ref(bsc, parsed)) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1152 | goto exit2; |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1153 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 3a77e61 | 2010-07-05 13:33:18 +0800 | [diff] [blame] | 1154 | con->msc_con = bsc->nat->msc_con; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1155 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 1156 | con->filter_state.con_type = con_type; |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 1157 | con->filter_state.imsi_checked = filter; |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 1158 | bsc_nat_extract_lac(bsc, con, parsed, msg); |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1159 | if (imsi) |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 1160 | con->filter_state.imsi = talloc_steal(con, imsi); |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1161 | imsi = NULL; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1162 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 1163 | handle_con_stats(con); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1164 | break; |
| 1165 | case SCCP_MSG_TYPE_RLSD: |
| 1166 | case SCCP_MSG_TYPE_CREF: |
| 1167 | case SCCP_MSG_TYPE_DT1: |
| 1168 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 1169 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1170 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1171 | if (con) { |
Holger Hans Peter Freyther | 0c41b69 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1172 | /* only filter non local connections */ |
| 1173 | if (!con->con_local) { |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1174 | memset(&cause, 0, sizeof(cause)); |
| 1175 | filter = bsc_nat_filter_dt(bsc, msg, |
| 1176 | con, parsed, &cause); |
Holger Hans Peter Freyther | 0c41b69 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1177 | if (filter < 0) { |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 1178 | if (con->filter_state.imsi) |
| 1179 | bsc_nat_inform_reject(bsc, |
| 1180 | con->filter_state.imsi); |
Holger Hans Peter Freyther | 0c41b69 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1181 | bsc_stat_reject(filter, bsc, 1); |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1182 | bsc_send_con_release(bsc, con, &cause); |
Holger Hans Peter Freyther | 0c41b69 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1183 | con = NULL; |
| 1184 | goto exit2; |
| 1185 | } |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1186 | |
| 1187 | /* hand data to a side channel */ |
Holger Hans Peter Freyther | 6fcc3a9 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1188 | if (bsc_ussd_check(con, parsed, msg) == 1) |
Holger Hans Peter Freyther | e5d3249 | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1189 | con->con_local = NAT_CON_END_USSD; |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1190 | |
| 1191 | /* |
| 1192 | * Optionally rewrite setup message. This can |
| 1193 | * replace the msg and the parsed structure becomes |
| 1194 | * invalid. |
| 1195 | */ |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 1196 | msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, |
| 1197 | con->filter_state.imsi); |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1198 | talloc_free(parsed); |
| 1199 | parsed = NULL; |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 1200 | } else if (con->con_local == NAT_CON_END_USSD) { |
Holger Hans Peter Freyther | 6fcc3a9 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1201 | bsc_ussd_check(con, parsed, msg); |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 1202 | } |
Holger Hans Peter Freyther | 0c41b69 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 1203 | |
| 1204 | con_bsc = con->bsc; |
| 1205 | con_msc = con->msc_con; |
| 1206 | con_filter = con->con_local; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1207 | } |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1208 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1209 | break; |
| 1210 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 1211 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1212 | if (con) { |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1213 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1214 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 1215 | con_filter = con->con_local; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1216 | } |
Holger Hans Peter Freyther | f4cfc4f | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 1217 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1218 | bsc_maybe_close(bsc); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1219 | break; |
| 1220 | case SCCP_MSG_TYPE_UDT: |
| 1221 | /* simply forward everything */ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1222 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1223 | break; |
| 1224 | default: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 1225 | LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1226 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1227 | goto exit2; |
| 1228 | break; |
| 1229 | } |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 1230 | } else if (parsed->ipa_proto == IPAC_PROTO_MGCP_OLD) { |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 1231 | bsc_mgcp_forward(bsc, msg); |
| 1232 | goto exit2; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 1233 | } else { |
| 1234 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 1235 | goto exit2; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1238 | if (con_msc && con_bsc != bsc) { |
Holger Hans Peter Freyther | ca0c2f9 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 1239 | LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n", |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 1240 | bsc->cfg->nr, con_bsc->cfg->nr); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 1241 | goto exit2; |
| 1242 | } |
| 1243 | |
Holger Hans Peter Freyther | 09ecda4 | 2010-09-15 17:39:44 +0800 | [diff] [blame] | 1244 | /* do not forward messages to the MSC */ |
| 1245 | if (con_filter) |
| 1246 | goto exit2; |
| 1247 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1248 | if (!con_msc) { |
Holger Hans Peter Freyther | 27640fc | 2010-09-15 00:14:48 +0800 | [diff] [blame] | 1249 | LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n", |
| 1250 | bsc->cfg->nr, |
| 1251 | parsed ? parsed->ipa_proto : -1, |
| 1252 | parsed ? parsed->sccp_type : -1); |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1253 | goto exit2; |
| 1254 | } |
| 1255 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1256 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 1257 | queue_for_msc(con_msc, msg); |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 1258 | if (parsed) |
| 1259 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1260 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1261 | |
| 1262 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1263 | /* if we filter out the reset send an ack to the BSC */ |
| 1264 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1265 | send_reset_ack(bsc); |
| 1266 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1267 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 1268 | /* do we know who is handling this? */ |
Holger Hans Peter Freyther | 2dd18bd | 2015-06-08 18:31:02 +0200 | [diff] [blame] | 1269 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP && msgb_l2len(msg) > 2) { |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1270 | struct tlv_parsed tvp; |
Pablo Neira Ayuso | ca05d43 | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1271 | int ret; |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 1272 | ret = ipa_ccm_idtag_parse_off(&tvp, |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1273 | (unsigned char *) msg->l2h + 2, |
Holger Hans Peter Freyther | 694d980 | 2015-06-08 18:33:28 +0200 | [diff] [blame] | 1274 | msgb_l2len(msg) - 2, 0); |
Pablo Neira Ayuso | ca05d43 | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 1275 | if (ret < 0) { |
| 1276 | LOGP(DNAT, LOGL_ERROR, "ignoring IPA response " |
| 1277 | "message with malformed TLVs\n"); |
| 1278 | return ret; |
| 1279 | } |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1280 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 1281 | ipaccess_auth_bsc(&tvp, bsc); |
| 1282 | } |
| 1283 | |
| 1284 | goto exit2; |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
Holger Hans Peter Freyther | 1498d2e | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1287 | exit2: |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1288 | if (imsi) |
| 1289 | talloc_free(imsi); |
Holger Hans Peter Freyther | 1498d2e | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 1290 | talloc_free(parsed); |
| 1291 | msgb_free(msg); |
| 1292 | return -1; |
| 1293 | |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 1294 | exit3: |
| 1295 | /* send a SCCP Connection Refused */ |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1296 | if (imsi) |
| 1297 | talloc_free(imsi); |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 1298 | bsc_send_con_refuse(bsc, parsed, con_type, &cause); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1299 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1300 | msgb_free(msg); |
| 1301 | return -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1304 | static int ipaccess_bsc_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1305 | { |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1306 | struct bsc_connection *bsc = bfd->data; |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1307 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1308 | struct ipaccess_head *hh; |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1309 | struct ipaccess_head_ext *hh_ext; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1310 | int ret; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1311 | |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1312 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &bsc->pending_msg); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1313 | if (ret <= 0) { |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 1314 | if (ret == -EAGAIN) |
| 1315 | return 0; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1316 | if (ret == 0) |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1317 | LOGP(DNAT, LOGL_ERROR, |
| 1318 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 1319 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1320 | else |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1321 | LOGP(DNAT, LOGL_ERROR, |
| 1322 | "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n", |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1323 | bsc->cfg ? bsc->cfg->nr : -1, ret); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1324 | |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 1325 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1326 | return -1; |
| 1327 | } |
| 1328 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1329 | |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 1330 | LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1331 | |
| 1332 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1333 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1334 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1335 | /* stop the pong timeout */ |
| 1336 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 1337 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1338 | osmo_timer_del(&bsc->pong_timeout); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1339 | msgb_free(msg); |
| 1340 | return 0; |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1341 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 1342 | send_pong(bsc); |
| 1343 | msgb_free(msg); |
| 1344 | return 0; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1345 | } |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1346 | /* Message contains the ipaccess_head_ext header, investigate further */ |
| 1347 | } else if (hh->proto == IPAC_PROTO_OSMO && |
| 1348 | msg->len > sizeof(*hh) + sizeof(*hh_ext)) { |
| 1349 | |
| 1350 | hh_ext = (struct ipaccess_head_ext *) hh->data; |
| 1351 | /* l2h is where the actual command data is expected */ |
| 1352 | msg->l2h = hh_ext->data; |
| 1353 | |
Harald Welte | f8e49dd | 2011-07-13 14:43:37 +0200 | [diff] [blame] | 1354 | if (hh_ext->proto == IPAC_PROTO_EXT_CTRL) |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 1355 | return bsc_nat_handle_ctrlif_msg(bsc, msg); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1358 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 1359 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1360 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1361 | |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1365 | static int ipaccess_listen_bsc_cb(struct osmo_fd *bfd, unsigned int what) |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1366 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1367 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1368 | int fd, rc, on; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1369 | struct sockaddr_in sa; |
| 1370 | socklen_t sa_len = sizeof(sa); |
| 1371 | |
| 1372 | if (!(what & BSC_FD_READ)) |
| 1373 | return 0; |
| 1374 | |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1375 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 1376 | if (fd < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1377 | perror("accept"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1378 | return fd; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1381 | /* count the reconnect */ |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 1382 | osmo_counter_inc(nat->stats.bsc.reconn); |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1383 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1384 | /* |
| 1385 | * if we are not connected to a msc... just close the socket |
| 1386 | */ |
Holger Hans Peter Freyther | 20ee312 | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 1387 | if (!bsc_nat_msc_is_connected(nat)) { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1388 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1389 | close(fd); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1390 | return 0; |
| 1391 | } |
| 1392 | |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1393 | if (nat->blocked) { |
| 1394 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due NAT being blocked.\n"); |
| 1395 | close(fd); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1399 | on = 1; |
Holger Hans Peter Freyther | 569ee12 | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 1400 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1401 | if (rc != 0) |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1402 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 1403 | |
Holger Hans Peter Freyther | 078321a | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1404 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | 6b77107 | 2010-07-27 19:21:53 +0800 | [diff] [blame] | 1405 | &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp)); |
Holger Hans Peter Freyther | 078321a | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1406 | if (rc != 0) |
| 1407 | LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 1408 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1409 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 1410 | /* todo... use GNUtls to see if we want to trust this as a BTS */ |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1411 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1412 | /* |
| 1413 | * |
| 1414 | */ |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1415 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1416 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1417 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1418 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1419 | return -1; |
| 1420 | } |
| 1421 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1422 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1423 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1424 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 1425 | bsc->write_queue.write_cb = bsc_write_cb; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1426 | bsc->write_queue.bfd.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1427 | if (osmo_fd_register(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1428 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1429 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1430 | talloc_free(bsc); |
| 1431 | return -2; |
| 1432 | } |
| 1433 | |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 1434 | LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n", |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1435 | fd, inet_ntoa(sa.sin_addr)); |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1436 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1437 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1438 | bsc->last_id = 0; |
| 1439 | |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 1440 | send_id_ack(bsc); |
Holger Hans Peter Freyther | 9705671 | 2015-06-08 16:28:15 +0200 | [diff] [blame] | 1441 | send_id_req(nat, bsc); |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 1442 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * start the hangup timer |
| 1446 | */ |
| 1447 | bsc->id_timeout.data = bsc; |
| 1448 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1449 | osmo_timer_schedule(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
| 1452 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1453 | static void print_usage() |
| 1454 | { |
| 1455 | printf("Usage: bsc_nat\n"); |
| 1456 | } |
| 1457 | |
| 1458 | static void print_help() |
| 1459 | { |
| 1460 | printf(" Some useful help...\n"); |
| 1461 | printf(" -h --help this text\n"); |
| 1462 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1463 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1464 | printf(" -s --disable-color\n"); |
| 1465 | printf(" -c --config-file filename The config file to use.\n"); |
| 1466 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1467 | printf(" -l --local=IP. The local address of this BSC.\n"); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1470 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1471 | { |
| 1472 | while (1) { |
| 1473 | int option_index = 0, c; |
| 1474 | static struct option long_options[] = { |
| 1475 | {"help", 0, 0, 'h'}, |
| 1476 | {"debug", 1, 0, 'd'}, |
Holger Hans Peter Freyther | 27b6184 | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1477 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1478 | {"config-file", 1, 0, 'c'}, |
| 1479 | {"disable-color", 0, 0, 's'}, |
| 1480 | {"timestamp", 0, 0, 'T'}, |
| 1481 | {"msc", 1, 0, 'm'}, |
| 1482 | {"local", 1, 0, 'l'}, |
| 1483 | {0, 0, 0, 0} |
| 1484 | }; |
| 1485 | |
Holger Hans Peter Freyther | 27b6184 | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1486 | c = getopt_long(argc, argv, "hd:sTPc:m:l:D", |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1487 | long_options, &option_index); |
| 1488 | if (c == -1) |
| 1489 | break; |
| 1490 | |
| 1491 | switch (c) { |
| 1492 | case 'h': |
| 1493 | print_usage(); |
| 1494 | print_help(); |
| 1495 | exit(0); |
| 1496 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1497 | log_set_use_color(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1498 | break; |
| 1499 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1500 | log_parse_category_mask(osmo_stderr_target, optarg); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1501 | break; |
Holger Hans Peter Freyther | 27b6184 | 2013-01-15 14:37:51 +0100 | [diff] [blame] | 1502 | case 'D': |
| 1503 | daemonize = 1; |
| 1504 | break; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1505 | case 'c': |
Holger Hans Peter Freyther | 60e073e | 2015-02-08 09:25:38 +0100 | [diff] [blame] | 1506 | config_file = optarg; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1507 | break; |
| 1508 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1509 | log_set_print_timestamp(osmo_stderr_target, 1); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1510 | break; |
| 1511 | case 'm': |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1512 | msc_ip = optarg; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1513 | break; |
| 1514 | case 'l': |
| 1515 | inet_aton(optarg, &local_addr); |
| 1516 | break; |
| 1517 | default: |
| 1518 | /* ignore */ |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1524 | static void signal_handler(int signal) |
| 1525 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1526 | switch (signal) { |
| 1527 | case SIGABRT: |
| 1528 | /* in case of abort, we want to obtain a talloc report |
| 1529 | * and then return to the caller, who will abort the process */ |
| 1530 | case SIGUSR1: |
| 1531 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1532 | break; |
| 1533 | default: |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1538 | static void sccp_close_unconfirmed(void *_data) |
| 1539 | { |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1540 | int destroyed = 0; |
| 1541 | struct bsc_connection *bsc, *bsc_tmp; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1542 | struct nat_sccp_connection *conn, *tmp1; |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1543 | struct timespec now; |
| 1544 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 1545 | |
| 1546 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 1547 | if (conn->has_remote_ref) |
| 1548 | continue; |
| 1549 | |
| 1550 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 1551 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 1552 | continue; |
| 1553 | |
Holger Hans Peter Freyther | c8b2c2f | 2014-04-03 12:28:14 +0200 | [diff] [blame] | 1554 | LOGP(DNAT, LOGL_ERROR, |
| 1555 | "SCCP connection 0x%x/0x%x was never confirmed on bsc nr. %d\n", |
| 1556 | sccp_src_ref_to_int(&conn->real_ref), |
| 1557 | sccp_src_ref_to_int(&conn->patched_ref), |
| 1558 | conn->bsc->cfg->nr); |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1559 | sccp_connection_destroy(conn); |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1560 | destroyed = 1; |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1561 | } |
| 1562 | |
Holger Hans Peter Freyther | de2b860 | 2011-04-23 23:55:14 +0200 | [diff] [blame] | 1563 | if (!destroyed) |
| 1564 | goto out; |
| 1565 | |
| 1566 | /* now close out any BSC */ |
| 1567 | llist_for_each_entry_safe(bsc, bsc_tmp, &nat->bsc_connections, list_entry) |
| 1568 | bsc_maybe_close(bsc); |
| 1569 | |
| 1570 | out: |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1571 | osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0); |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1574 | extern void *tall_msgb_ctx; |
| 1575 | extern void *tall_ctr_ctx; |
| 1576 | static void talloc_init_ctx() |
| 1577 | { |
| 1578 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 1579 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1580 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 1581 | } |
| 1582 | |
Holger Hans Peter Freyther | 3e4e8cf | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 1583 | extern int bsc_vty_go_parent(struct vty *vty); |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1584 | |
| 1585 | static struct vty_app_info vty_info = { |
Harald Welte | ec1921d | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 1586 | .name = "OsmoBSCNAT", |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1587 | .version = PACKAGE_VERSION, |
| 1588 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 81506b4 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 1589 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1590 | }; |
| 1591 | |
Holger Hans Peter Freyther | 85d3b34 | 2013-06-14 19:10:28 +0200 | [diff] [blame] | 1592 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1593 | int main(int argc, char **argv) |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1594 | { |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1595 | int rc; |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1596 | |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1597 | talloc_init_ctx(); |
Holger Hans Peter Freyther | a1597f1 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 1598 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1599 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 1600 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1601 | nat = bsc_nat_alloc(); |
| 1602 | if (!nat) { |
| 1603 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 1604 | return -4; |
| 1605 | } |
| 1606 | |
Holger Hans Peter Freyther | d5e6c23 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1607 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1608 | if (!nat->mgcp_cfg) { |
| 1609 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 1610 | return -5; |
| 1611 | } |
| 1612 | |
Holger Hans Peter Freyther | 7f100c9 | 2015-04-23 20:25:17 -0400 | [diff] [blame] | 1613 | /* We need to add mode-set for amr codecs */ |
| 1614 | nat->sdp_ensure_amr_mode_set = 1; |
| 1615 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1616 | vty_info.copyright = openbsc_copyright; |
| 1617 | vty_init(&vty_info); |
Pablo Neira Ayuso | 739a566 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 1618 | logging_vty_add_cmds(&log_info); |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 1619 | osmo_stats_vty_add_cmds(&log_info); |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1620 | bsc_nat_vty_init(nat); |
| 1621 | |
| 1622 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1623 | /* parse options */ |
| 1624 | local_addr.s_addr = INADDR_ANY; |
| 1625 | handle_options(argc, argv); |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1626 | |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1627 | rate_ctr_init(tall_bsc_ctx); |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 1628 | osmo_stats_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1629 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1630 | /* init vty and parse */ |
Neels Hofmeyr | 547f048 | 2016-02-24 03:12:44 +0100 | [diff] [blame] | 1631 | if (telnet_init(tall_bsc_ctx, NULL, OSMO_VTY_PORT_BSC_NAT)) { |
| 1632 | fprintf(stderr, "Creating VTY telnet line failed\n"); |
| 1633 | return -5; |
| 1634 | } |
Pablo Neira Ayuso | 326b5d8 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 1635 | if (mgcp_parse_config(config_file, nat->mgcp_cfg, MGCP_BSC_NAT) < 0) { |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1636 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1637 | return -3; |
| 1638 | } |
| 1639 | |
Neels Hofmeyr | 547f048 | 2016-02-24 03:12:44 +0100 | [diff] [blame] | 1640 | /* over rule the VTY config for MSC IP */ |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1641 | if (msc_ip) |
| 1642 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1643 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1644 | /* seed the PRNG */ |
| 1645 | srand(time(NULL)); |
| 1646 | |
Holger Hans Peter Freyther | 85d3b34 | 2013-06-14 19:10:28 +0200 | [diff] [blame] | 1647 | |
| 1648 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1649 | /* |
| 1650 | * Setup the MGCP code.. |
| 1651 | */ |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1652 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1653 | return -4; |
| 1654 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1655 | /* connect to the MSC */ |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 1656 | nat->msc_con = bsc_msc_create(nat, &nat->dests); |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1657 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1658 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1659 | exit(1); |
| 1660 | } |
| 1661 | |
Neels Hofmeyr | fd1187e | 2016-02-24 00:28:13 +0100 | [diff] [blame] | 1662 | nat->ctrl = bsc_nat_controlif_setup(nat, OSMO_CTRL_PORT_BSC_NAT); |
Holger Hans Peter Freyther | 036b25f | 2013-01-15 13:48:18 +0100 | [diff] [blame] | 1663 | if (!nat->ctrl) { |
| 1664 | fprintf(stderr, "Creating the control interface failed.\n"); |
| 1665 | exit(1); |
| 1666 | } |
Daniel Willmann | a86bc39 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 1667 | |
Holger Hans Peter Freyther | c1a8687 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 1668 | nat->msc_con->name = "main MSC"; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1669 | nat->msc_con->connection_loss = msc_connection_was_lost; |
Holger Hans Peter Freyther | a99c5b9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 1670 | nat->msc_con->connected = msc_connection_connected; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1671 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1672 | nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 1673 | nat->msc_con->write_queue.bfd.data = nat->msc_con; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1674 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1675 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1676 | /* wait for the BSC */ |
Holger Hans Peter Freyther | a25d579 | 2010-10-19 17:48:13 +0200 | [diff] [blame] | 1677 | rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr), |
Holger Hans Peter Freyther | 7d73642 | 2011-04-07 22:14:58 +0200 | [diff] [blame] | 1678 | 5000, 0, ipaccess_listen_bsc_cb, nat); |
Holger Hans Peter Freyther | f961de1 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 1679 | if (rc != 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1680 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1681 | exit(1); |
| 1682 | } |
| 1683 | |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 1684 | rc = bsc_ussd_init(nat); |
| 1685 | if (rc != 0) { |
| 1686 | LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n"); |
| 1687 | exit(1); |
| 1688 | } |
| 1689 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1690 | signal(SIGABRT, &signal_handler); |
| 1691 | signal(SIGUSR1, &signal_handler); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1692 | osmo_init_ignore_signals(); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1693 | |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1694 | if (daemonize) { |
| 1695 | rc = osmo_daemonize(); |
| 1696 | if (rc < 0) { |
| 1697 | perror("Error during daemonize"); |
| 1698 | exit(1); |
| 1699 | } |
| 1700 | } |
| 1701 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1702 | /* recycle timer */ |
Holger Hans Peter Freyther | 30e1ae9 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1703 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1704 | sccp_close.cb = sccp_close_unconfirmed; |
| 1705 | sccp_close.data = NULL; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1706 | osmo_timer_schedule(&sccp_close, SCCP_CLOSE_TIME, 0); |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1707 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1708 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1709 | osmo_select_main(0); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1710 | } |
| 1711 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* Close all connections handed out to the USSD module */ |
Holger Hans Peter Freyther | 6fcc3a9 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 1716 | int bsc_ussd_close_connections(struct bsc_nat *nat) |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1717 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 1718 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1719 | llist_for_each_entry(con, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e5d3249 | 2011-04-16 16:02:59 +0200 | [diff] [blame] | 1720 | if (con->con_local != NAT_CON_END_USSD) |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1721 | continue; |
| 1722 | if (!con->bsc) |
| 1723 | continue; |
| 1724 | |
Holger Hans Peter Freyther | a8a50a8 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 1725 | nat_send_clrc_bsc(con); |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1726 | nat_send_rlsd_bsc(con); |
| 1727 | } |
| 1728 | |
| 1729 | return 0; |
| 1730 | } |