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