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