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