Holger Hans Peter Freyther | 89d9fd9 | 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 | 98e49d4 | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 30 | #include <signal.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Holger Hans Peter Freyther | fd012d5 | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 33 | #include <time.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #define _GNU_SOURCE |
| 37 | #include <getopt.h> |
| 38 | |
| 39 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 40 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 42 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 43 | #include <openbsc/ipaccess.h> |
| 44 | #include <openbsc/abis_nm.h> |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 45 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 46 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 47 | |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 48 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 49 | #include <osmocore/talloc.h> |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 50 | #include <osmocore/process.h> |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 51 | |
Holger Hans Peter Freyther | e36be81 | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 52 | #include <osmocore/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 53 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 54 | #include <osmocom/vty/telnet_interface.h> |
| 55 | #include <osmocom/vty/vty.h> |
| 56 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 57 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 59 | #include "../../bscconfig.h" |
| 60 | |
Holger Hans Peter Freyther | 6088cc7 | 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 | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 64 | struct log_target *stderr_target; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 65 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 66 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | bea0ac6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 67 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 68 | static const char *msc_ip = NULL; |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 69 | static struct timer_list sccp_close; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 70 | static int daemonize = 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 71 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 72 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 73 | "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\r\n" |
| 74 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\r\n" |
| 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 | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 77 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 78 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | 0b77635 | 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 | dc81393 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 80 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | a67fc6f | 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 | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 82 | |
Holger Hans Peter Freyther | 5e54788 | 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 | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 94 | /* |
| 95 | * below are stubs we need to link |
| 96 | */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 97 | int nm_state_event(enum nm_evt evt, uint8_t obj_class, void *obj, |
Holger Hans Peter Freyther | c4aa270 | 2010-05-12 16:55:12 +0800 | [diff] [blame] | 98 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state, |
| 99 | struct abis_om_obj_inst *obj_ins) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 100 | { |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 105 | {} |
| 106 | |
Holger Hans Peter Freyther | 754787c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 107 | static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) |
| 108 | { |
Holger Hans Peter Freyther | 1e64b32 | 2010-10-06 00:24:28 +0800 | [diff] [blame] | 109 | if (!con) { |
| 110 | LOGP(DINP, LOGL_ERROR, "No MSC Connection assigned. Check your code.\n"); |
| 111 | msgb_free(msg); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 116 | if (write_queue_enqueue(&con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 754787c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 117 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 118 | msgb_free(msg); |
| 119 | } |
| 120 | } |
| 121 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 122 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 123 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 124 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 125 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 126 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 127 | 0x00, 0x01, 0x31, |
| 128 | }; |
| 129 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 130 | bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 133 | static void send_ping(struct bsc_connection *bsc) |
| 134 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 135 | static const uint8_t id_ping[] = { |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 136 | IPAC_MSGT_PING, |
| 137 | }; |
| 138 | |
| 139 | bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS); |
| 140 | } |
| 141 | |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 142 | static void send_pong(struct bsc_connection *bsc) |
| 143 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 144 | static const uint8_t id_pong[] = { |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 145 | IPAC_MSGT_PONG, |
| 146 | }; |
| 147 | |
| 148 | bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS); |
| 149 | } |
| 150 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 151 | static void bsc_pong_timeout(void *_bsc) |
| 152 | { |
| 153 | struct bsc_connection *bsc = _bsc; |
| 154 | |
| 155 | LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr); |
| 156 | bsc_close_connection(bsc); |
| 157 | } |
| 158 | |
| 159 | static void bsc_ping_timeout(void *_bsc) |
| 160 | { |
| 161 | struct bsc_connection *bsc = _bsc; |
| 162 | |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 163 | if (bsc->nat->ping_timeout < 0) |
| 164 | return; |
| 165 | |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 166 | send_ping(bsc); |
| 167 | |
| 168 | /* send another ping in 20 seconds */ |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 169 | bsc_schedule_timer(&bsc->ping_timeout, bsc->nat->ping_timeout, 0); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 170 | |
| 171 | /* also start a pong timer */ |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 172 | bsc_schedule_timer(&bsc->pong_timeout, bsc->nat->pong_timeout, 0); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void start_ping_pong(struct bsc_connection *bsc) |
| 176 | { |
| 177 | bsc->pong_timeout.data = bsc; |
| 178 | bsc->pong_timeout.cb = bsc_pong_timeout; |
| 179 | bsc->ping_timeout.data = bsc; |
| 180 | bsc->ping_timeout.cb = bsc_ping_timeout; |
| 181 | |
| 182 | bsc_ping_timeout(bsc); |
| 183 | } |
| 184 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 185 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 186 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 187 | static const uint8_t id_ack[] = { |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 188 | IPAC_MSGT_ID_ACK |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 191 | bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 194 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 195 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 196 | static const uint8_t id_req[] = { |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 197 | IPAC_MSGT_ID_GET, |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 198 | 0x01, IPAC_IDTAG_UNIT, |
| 199 | 0x01, IPAC_IDTAG_MACADDR, |
| 200 | 0x01, IPAC_IDTAG_LOCATION1, |
| 201 | 0x01, IPAC_IDTAG_LOCATION2, |
| 202 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 203 | 0x01, IPAC_IDTAG_SWVERSION, |
| 204 | 0x01, IPAC_IDTAG_UNITNAME, |
| 205 | 0x01, IPAC_IDTAG_SERNR, |
| 206 | }; |
| 207 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 208 | bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Holger Hans Peter Freyther | 0a891d0 | 2010-10-27 10:54:11 +0200 | [diff] [blame] | 211 | static void nat_send_rlsd_msc(struct sccp_connections *conn) |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 212 | { |
| 213 | struct sccp_connection_released *rel; |
| 214 | struct msgb *msg; |
| 215 | |
| 216 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 217 | if (!msg) { |
| 218 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 223 | rel = (struct sccp_connection_released *) msg->l2h; |
| 224 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 225 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 226 | rel->destination_local_reference = conn->remote_ref; |
| 227 | rel->source_local_reference = conn->patched_ref; |
| 228 | |
| 229 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 230 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 231 | queue_for_msc(conn->msc_con, msg); |
Holger Hans Peter Freyther | 7746bd8 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 234 | static void nat_send_rlsd_bsc(struct sccp_connections *conn) |
| 235 | { |
| 236 | struct sccp_connection_released *rel; |
| 237 | struct msgb *msg; |
| 238 | |
| 239 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 240 | if (!msg) { |
| 241 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 246 | rel = (struct sccp_connection_released *) msg->l2h; |
| 247 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 248 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 249 | rel->destination_local_reference = conn->real_ref; |
| 250 | rel->source_local_reference = conn->remote_ref; |
| 251 | |
| 252 | bsc_write(conn->bsc, msg, IPAC_PROTO_SCCP); |
| 253 | } |
| 254 | |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 255 | static void nat_send_clrc_bsc(struct sccp_connections *conn) |
| 256 | { |
| 257 | struct msgb *msg; |
| 258 | struct msgb *sccp; |
| 259 | |
| 260 | msg = gsm0808_create_clear_command(0x20); |
| 261 | if (!msg) { |
| 262 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | sccp = sccp_create_dt1(&conn->real_ref, msg->data, msg->len); |
| 267 | if (!sccp) { |
| 268 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate SCCP msg.\n"); |
| 269 | msgb_free(msg); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | msgb_free(msg); |
| 274 | bsc_write(conn->bsc, sccp, IPAC_PROTO_SCCP); |
| 275 | } |
| 276 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 277 | static void nat_send_rlc(struct bsc_msc_connection *msc_con, |
| 278 | struct sccp_source_reference *src, |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 279 | struct sccp_source_reference *dst) |
| 280 | { |
| 281 | struct sccp_connection_release_complete *rlc; |
| 282 | struct msgb *msg; |
| 283 | |
| 284 | msg = msgb_alloc_headroom(4096, 128, "rlc"); |
| 285 | if (!msg) { |
| 286 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | msg->l2h = msgb_put(msg, sizeof(*rlc)); |
| 291 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 292 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 293 | rlc->destination_local_reference = *dst; |
| 294 | rlc->source_local_reference = *src; |
| 295 | |
| 296 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 297 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 298 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 301 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 302 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 303 | static const uint8_t mgcp_reset[] = { |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 304 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 305 | }; |
| 306 | |
| 307 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 308 | } |
| 309 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 310 | /* |
| 311 | * Below is the handling of messages coming |
| 312 | * from the MSC and need to be forwarded to |
| 313 | * a real BSC. |
| 314 | */ |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 315 | static void initialize_msc_if_needed(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 316 | { |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 317 | if (msc_con->first_contact) |
Holger Hans Peter Freyther | dc81393 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 318 | return; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 319 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 320 | msc_con->first_contact = 1; |
| 321 | msc_send_reset(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 324 | static void send_id_get_response(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 325 | { |
| 326 | struct msgb *msg = bsc_msc_id_get_resp(nat->token); |
| 327 | if (!msg) |
| 328 | return; |
| 329 | |
| 330 | ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 331 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 334 | /* |
| 335 | * Currently we are lacking refcounting so we need to copy each message. |
| 336 | */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 337 | static void bsc_send_data(struct bsc_connection *bsc, const uint8_t *data, unsigned int length, int proto) |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 338 | { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 339 | struct msgb *msg; |
| 340 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 341 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 342 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 343 | return; |
| 344 | } |
| 345 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 346 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 347 | if (!msg) { |
| 348 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 349 | return; |
| 350 | } |
| 351 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 352 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 353 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 354 | |
| 355 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | ad2136b | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 358 | /* |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 359 | * Update the release statistics |
| 360 | */ |
| 361 | static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal) |
| 362 | { |
| 363 | if (!bsc->cfg) { |
| 364 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated."); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | if (filter >= 0) { |
| 369 | LOGP(DNAT, LOGL_ERROR, "Connection was not rejected"); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (filter == -1) |
| 374 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_ILL_PACKET]); |
| 375 | else if (normal) |
| 376 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_MSG]); |
| 377 | else |
| 378 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_REJECTED_CR]); |
| 379 | } |
| 380 | |
| 381 | /* |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 382 | * Release an established connection. We will have to release it to the BSC |
| 383 | * and to the network and we do it the following way. |
| 384 | * 1.) Give up on the MSC side |
| 385 | * 1.1) Send a RLSD message, it is a bit non standard but should work, we |
| 386 | * ignore the RLC... we might complain about it. Other options would |
| 387 | * be to send a Release Request, handle the Release Complete.. |
| 388 | * 1.2) Mark the data structure to be con_local and wait for 2nd |
| 389 | * |
| 390 | * 2.) Give up on the BSC side |
| 391 | * 2.1) Depending on the con type reject the service, or just close it |
| 392 | */ |
| 393 | static void bsc_send_con_release(struct bsc_connection *bsc, struct sccp_connections *con) |
| 394 | { |
| 395 | struct msgb *rlsd; |
| 396 | /* 1. release the network */ |
| 397 | rlsd = sccp_create_rlsd(&con->patched_ref, &con->remote_ref, |
| 398 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 399 | if (!rlsd) |
| 400 | LOGP(DNAT, LOGL_ERROR, "Failed to create RLSD message.\n"); |
| 401 | else { |
| 402 | ipaccess_prepend_header(rlsd, IPAC_PROTO_SCCP); |
| 403 | queue_for_msc(con->msc_con, rlsd); |
| 404 | } |
| 405 | con->con_local = 1; |
Holger Hans Peter Freyther | b2b66c2 | 2010-09-16 06:41:09 +0800 | [diff] [blame] | 406 | con->msc_con = NULL; |
Holger Hans Peter Freyther | a8f092d | 2010-09-15 07:43:59 +0800 | [diff] [blame] | 407 | |
| 408 | /* 2. release the BSC side */ |
| 409 | if (con->con_type == NAT_CON_TYPE_LU) { |
| 410 | struct msgb *payload, *udt; |
| 411 | payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
| 412 | |
| 413 | if (payload) { |
| 414 | gsm0808_prepend_dtap_header(payload, 0); |
| 415 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 416 | if (udt) |
| 417 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 418 | else |
| 419 | LOGP(DNAT, LOGL_ERROR, "Failed to create DT1\n"); |
| 420 | |
| 421 | msgb_free(payload); |
| 422 | } else { |
| 423 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate LU Reject.\n"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 428 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 429 | if (!rlsd) { |
| 430 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate RLSD for the BSC.\n"); |
| 431 | sccp_connection_destroy(con); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
| 436 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 437 | } |
| 438 | |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 439 | static void bsc_send_con_refuse(struct bsc_connection *bsc, |
| 440 | struct bsc_nat_parsed *parsed, int con_type) |
| 441 | { |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 442 | struct msgb *payload; |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 443 | struct msgb *refuse; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 444 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 445 | if (con_type == NAT_CON_TYPE_LU) |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 446 | payload = gsm48_create_loc_upd_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 447 | else if (con_type == NAT_CON_TYPE_CM_SERV_REQ) |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 448 | payload = gsm48_create_mm_serv_rej(GSM48_REJECT_PLMN_NOT_ALLOWED); |
Holger Hans Peter Freyther | a624c53 | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 449 | else { |
| 450 | LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); |
| 451 | payload = NULL; |
| 452 | } |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * Some BSCs do not handle the payload inside a SCCP CREF msg |
| 456 | * so we will need to: |
| 457 | * 1.) Allocate a local connection and mark it as local.. |
| 458 | * 2.) queue data for downstream.. and the RLC should delete everything |
| 459 | */ |
| 460 | if (payload) { |
| 461 | struct msgb *cc, *udt, *rlsd; |
| 462 | struct sccp_connections *con; |
| 463 | con = create_sccp_src_ref(bsc, parsed); |
| 464 | if (!con) |
| 465 | goto send_refuse; |
| 466 | |
| 467 | /* declare it local and assign a unique remote_ref */ |
| 468 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
| 469 | con->con_local = 1; |
| 470 | con->has_remote_ref = 1; |
| 471 | con->remote_ref = con->patched_ref; |
| 472 | |
| 473 | /* 1. create a confirmation */ |
| 474 | cc = sccp_create_cc(&con->remote_ref, &con->real_ref); |
| 475 | if (!cc) |
| 476 | goto send_refuse; |
| 477 | |
| 478 | /* 2. create the DT1 */ |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 479 | gsm0808_prepend_dtap_header(payload, 0); |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 480 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 481 | if (!udt) { |
| 482 | msgb_free(cc); |
| 483 | goto send_refuse; |
| 484 | } |
| 485 | |
| 486 | /* 3. send a RLSD */ |
| 487 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 488 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 489 | if (!rlsd) { |
| 490 | msgb_free(cc); |
| 491 | msgb_free(udt); |
| 492 | goto send_refuse; |
| 493 | } |
| 494 | |
| 495 | bsc_write(bsc, cc, IPAC_PROTO_SCCP); |
| 496 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 497 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 498 | msgb_free(payload); |
| 499 | return; |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 500 | } |
| 501 | |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 502 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 503 | send_refuse: |
Holger Hans Peter Freyther | 605eba7 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 504 | if (payload) |
| 505 | msgb_free(payload); |
| 506 | |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 507 | refuse = sccp_create_refuse(parsed->src_local_ref, |
| 508 | SCCP_REFUSAL_SCCP_FAILURE, NULL, 0); |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 509 | if (!refuse) { |
| 510 | LOGP(DNAT, LOGL_ERROR, |
| 511 | "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n", |
| 512 | sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | bsc_write(bsc, refuse, IPAC_PROTO_SCCP); |
| 517 | } |
| 518 | |
| 519 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 520 | static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *msg) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 521 | { |
Holger Hans Peter Freyther | a624c53 | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 522 | struct sccp_connections *con = NULL; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 523 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 524 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 525 | int proto; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 526 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 527 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 528 | parsed = bsc_nat_parse(msg); |
| 529 | if (!parsed) { |
| 530 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 531 | return -1; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 532 | } |
| 533 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 534 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 535 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 536 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 537 | proto = parsed->ipa_proto; |
| 538 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 539 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 540 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 541 | switch (parsed->sccp_type) { |
| 542 | case SCCP_MSG_TYPE_UDT: |
| 543 | /* forward UDT messages to every BSC */ |
| 544 | goto send_to_all; |
| 545 | break; |
| 546 | case SCCP_MSG_TYPE_RLSD: |
| 547 | case SCCP_MSG_TYPE_CREF: |
| 548 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 549 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 550 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 551 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 552 | counter_inc(nat->stats.sccp.calls); |
| 553 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 554 | if (con) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 555 | struct rate_ctr_group *ctrg; |
| 556 | ctrg = con->bsc->cfg->stats.ctrg; |
| 557 | rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]); |
Holger Hans Peter Freyther | 926c5fd | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 558 | if (bsc_mgcp_assign_patch(con, msg) != 0) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 559 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 560 | } else |
| 561 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
| 562 | } |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 563 | break; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 564 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 565 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 566 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 567 | goto exit; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 568 | break; |
| 569 | case SCCP_MSG_TYPE_RLC: |
| 570 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 571 | goto exit; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 572 | break; |
| 573 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 574 | /* MSC never opens a SCCP connection, fall through */ |
| 575 | default: |
| 576 | goto exit; |
| 577 | } |
Holger Hans Peter Freyther | 8d29828 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 578 | |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 579 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 580 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 581 | /* Exchange src/dest for the reply */ |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 582 | nat_send_rlc(msc_con, parsed->dest_local_ref, parsed->src_local_ref); |
Holger Hans Peter Freyther | cd922a1 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 583 | } else if (!con) |
Holger Hans Peter Freyther | 117e7a7 | 2010-05-15 19:13:52 +0800 | [diff] [blame] | 584 | LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x from the MSC.\n", parsed->sccp_type); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | talloc_free(parsed); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 588 | if (!con) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 589 | return -1; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 590 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 591 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 592 | return -1; |
| 593 | } |
| 594 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 595 | bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto); |
Holger Hans Peter Freyther | 2ec5517 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 596 | return 0; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 597 | |
| 598 | send_to_all: |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 599 | /* |
| 600 | * Filter Paging from the network. We do not want to send a PAGING |
| 601 | * Command to every BSC in our network. We will analys the PAGING |
| 602 | * message and then send it to the authenticated messages... |
| 603 | */ |
| 604 | if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) { |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 605 | int lac; |
| 606 | bsc = bsc_nat_find_bsc(nat, msg, &lac); |
Holger Hans Peter Freyther | 7acf462 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 607 | if (bsc && bsc->cfg->forbid_paging) |
Holger Hans Peter Freyther | 19ccb48 | 2010-04-21 20:17:18 +0800 | [diff] [blame] | 608 | LOGP(DNAT, LOGL_DEBUG, "Paging forbidden for BTS: %d\n", bsc->cfg->nr); |
Holger Hans Peter Freyther | 7acf462 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 609 | else if (bsc) |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 610 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto); |
Holger Hans Peter Freyther | e0ca9d2 | 2010-08-06 20:15:40 +0800 | [diff] [blame] | 611 | else if (lac != -1) |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 612 | LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n", |
| 613 | lac, lac); |
Holger Hans Peter Freyther | fd2c757 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 614 | |
| 615 | goto exit; |
| 616 | } |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 617 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 618 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | fb5a487 | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 619 | if (!bsc->authenticated) |
| 620 | continue; |
| 621 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 622 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 623 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 624 | |
| 625 | exit: |
| 626 | talloc_free(parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 627 | return 0; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 630 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 631 | { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 632 | struct bsc_connection *bsc, *tmp; |
| 633 | |
| 634 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 635 | llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry) |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 636 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 637 | |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 638 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 639 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 640 | } |
| 641 | |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 642 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 643 | { |
| 644 | counter_inc(nat->stats.msc.reconn); |
| 645 | } |
| 646 | |
Holger Hans Peter Freyther | 8f515a0 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 647 | static void msc_send_reset(struct bsc_msc_connection *msc_con) |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 648 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 649 | static const uint8_t reset[] = { |
Holger Hans Peter Freyther | c48a5d9 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 650 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 651 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 652 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 653 | 0x01, 0x20 |
| 654 | }; |
| 655 | |
| 656 | struct msgb *msg; |
| 657 | |
| 658 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 659 | if (!msg) { |
| 660 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 665 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 666 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 667 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | 2b3505b | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 668 | |
| 669 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 670 | } |
| 671 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 672 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 673 | { |
| 674 | int error; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 675 | struct bsc_msc_connection *msc_con; |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 676 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 677 | struct ipaccess_head *hh; |
| 678 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 679 | msc_con = (struct bsc_msc_connection *) bfd->data; |
| 680 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 681 | if (!msg) { |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 682 | if (error == 0) |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 683 | LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n"); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 684 | else |
| 685 | LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 686 | |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 687 | bsc_msc_lost(msc_con); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 688 | return -1; |
| 689 | } |
| 690 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 691 | LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 692 | |
| 693 | /* handle base message handling */ |
| 694 | hh = (struct ipaccess_head *) msg->data; |
| 695 | ipaccess_rcvmsg_base(msg, bfd); |
| 696 | |
| 697 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 698 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 699 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 700 | initialize_msc_if_needed(msc_con); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 701 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 702 | send_id_get_response(msc_con); |
Holger Hans Peter Freyther | 6e88b17 | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 703 | } else if (hh->proto == IPAC_PROTO_SCCP) |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 704 | forward_sccp_to_bts(msc_con, msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 705 | |
Holger Hans Peter Freyther | 418fe11 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 706 | msgb_free(msg); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 707 | return 0; |
| 708 | } |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 709 | |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 710 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 711 | { |
| 712 | int rc; |
| 713 | rc = write(bfd->fd, msg->data, msg->len); |
| 714 | |
| 715 | if (rc != msg->len) { |
| 716 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | return rc; |
| 721 | } |
| 722 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 723 | /* |
| 724 | * Below is the handling of messages coming |
| 725 | * from the BSC and need to be forwarded to |
| 726 | * a real BSC. |
| 727 | */ |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * Remove the connection from the connections list, |
| 731 | * remove it from the patching of SCCP header lists |
| 732 | * as well. Maybe in the future even close connection.. |
| 733 | */ |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 734 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 735 | { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 736 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 737 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 738 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 739 | /* stop the timeout timer */ |
| 740 | bsc_del_timer(&connection->id_timeout); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 741 | bsc_del_timer(&connection->ping_timeout); |
| 742 | bsc_del_timer(&connection->pong_timeout); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 743 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 744 | if (connection->cfg) |
| 745 | ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP]; |
| 746 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 747 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 748 | llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 749 | if (sccp_patch->bsc != connection) |
| 750 | continue; |
| 751 | |
Holger Hans Peter Freyther | 3357a7b | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 752 | if (ctr) |
| 753 | rate_ctr_inc(ctr); |
Holger Hans Peter Freyther | b2b66c2 | 2010-09-16 06:41:09 +0800 | [diff] [blame] | 754 | if (sccp_patch->has_remote_ref && !sccp_patch->con_local) |
Holger Hans Peter Freyther | 0a891d0 | 2010-10-27 10:54:11 +0200 | [diff] [blame] | 755 | nat_send_rlsd_msc(sccp_patch); |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 756 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Holger Hans Peter Freyther | c0ff1d0 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 759 | /* close endpoints allocated by this BSC */ |
| 760 | bsc_mgcp_clear_endpoints_for(connection); |
| 761 | |
Holger Hans Peter Freyther | 53cd985 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 762 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 763 | close(connection->write_queue.bfd.fd); |
| 764 | write_queue_clear(&connection->write_queue); |
| 765 | llist_del(&connection->list_entry); |
| 766 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 767 | talloc_free(connection); |
| 768 | } |
| 769 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 770 | static void ipaccess_close_bsc(void *data) |
| 771 | { |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 772 | struct sockaddr_in sock; |
| 773 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 774 | struct bsc_connection *conn = data; |
| 775 | |
Holger Hans Peter Freyther | 3626e97 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 776 | |
| 777 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 778 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 779 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 780 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 784 | { |
| 785 | struct bsc_config *conf; |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 786 | const char *token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | 4884096 | 2010-10-14 17:19:58 +0200 | [diff] [blame] | 787 | const int len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 788 | |
Holger Hans Peter Freyther | 4dc44ea | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 789 | if (bsc->cfg) { |
| 790 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 791 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 792 | return; |
| 793 | } |
| 794 | |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 795 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
Holger Hans Peter Freyther | 4884096 | 2010-10-14 17:19:58 +0200 | [diff] [blame] | 796 | if (strncmp(conf->token, token, len) == 0) { |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 797 | rate_ctr_inc(&conf->stats.ctrg->ctr[BCFG_CTR_NET_RECONN]); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 798 | bsc->authenticated = 1; |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 799 | bsc->cfg = conf; |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 800 | bsc_del_timer(&bsc->id_timeout); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 801 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d on fd %d\n", |
| 802 | conf->nr, bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 803 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 804 | return; |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 805 | } |
| 806 | } |
Holger Hans Peter Freyther | 8b2302f | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 807 | |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 808 | LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token, |
| 809 | bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 810 | } |
| 811 | |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 812 | static void handle_con_stats(struct sccp_connections *con) |
| 813 | { |
| 814 | struct rate_ctr_group *ctrg; |
| 815 | int id = bsc_conn_type_to_ctr(con); |
| 816 | |
| 817 | if (id == -1) |
| 818 | return; |
| 819 | |
| 820 | if (!con->bsc || !con->bsc->cfg) |
| 821 | return; |
| 822 | |
| 823 | ctrg = con->bsc->cfg->stats.ctrg; |
| 824 | rate_ctr_inc(&ctrg->ctr[id]); |
| 825 | } |
| 826 | |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 827 | static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 828 | { |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 829 | int con_filter = 0; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 830 | char *imsi = NULL; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 831 | struct bsc_msc_connection *con_msc = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 832 | struct bsc_connection *con_bsc = NULL; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 833 | int con_type; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 834 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 835 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 836 | /* Parse and filter messages */ |
| 837 | parsed = bsc_nat_parse(msg); |
| 838 | if (!parsed) { |
| 839 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n"); |
Holger Hans Peter Freyther | 45f6295 | 2010-03-30 07:35:46 +0200 | [diff] [blame] | 840 | msgb_free(msg); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 841 | return -1; |
| 842 | } |
| 843 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 844 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 845 | goto exit; |
Holger Hans Peter Freyther | 57adba5 | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 846 | |
Holger Hans Peter Freyther | 5a723b6 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 847 | /* |
| 848 | * check authentication after filtering to not reject auth |
| 849 | * responses coming from the BSC. We have to make sure that |
| 850 | * nothing from the exit path will forward things to the MSC |
| 851 | */ |
| 852 | if (!bsc->authenticated) { |
| 853 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 854 | msgb_free(msg); |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 859 | /* modify the SCCP entries */ |
| 860 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 8582bb9 | 2010-09-15 00:38:54 +0800 | [diff] [blame] | 861 | int filter; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 862 | struct sccp_connections *con; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 863 | switch (parsed->sccp_type) { |
| 864 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 865 | filter = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type, &imsi); |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 866 | if (filter < 0) { |
| 867 | bsc_stat_reject(filter, bsc, 0); |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 868 | goto exit3; |
Holger Hans Peter Freyther | a67fc6f | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 869 | } |
| 870 | |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 871 | if (!create_sccp_src_ref(bsc, parsed)) |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 872 | goto exit2; |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 873 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 6200fd9 | 2010-07-05 13:33:18 +0800 | [diff] [blame] | 874 | con->msc_con = bsc->nat->msc_con; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 875 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 876 | con->con_type = con_type; |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 877 | con->imsi_checked = filter; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 878 | if (imsi) |
| 879 | con->imsi = talloc_steal(con, imsi); |
| 880 | imsi = NULL; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 881 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 882 | handle_con_stats(con); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 883 | break; |
| 884 | case SCCP_MSG_TYPE_RLSD: |
| 885 | case SCCP_MSG_TYPE_CREF: |
| 886 | case SCCP_MSG_TYPE_DT1: |
| 887 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 567e840 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 888 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 889 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 890 | if (con) { |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 891 | /* only filter non local connections */ |
| 892 | if (!con->con_local) { |
| 893 | filter = bsc_nat_filter_dt(bsc, msg, con, parsed); |
| 894 | if (filter < 0) { |
| 895 | bsc_stat_reject(filter, bsc, 1); |
| 896 | bsc_send_con_release(bsc, con); |
| 897 | con = NULL; |
| 898 | goto exit2; |
| 899 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 900 | |
| 901 | /* hand data to a side channel */ |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 902 | if (bsc_check_ussd(con, parsed, msg) == 1) |
| 903 | con->con_local = 2; |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * Optionally rewrite setup message. This can |
| 907 | * replace the msg and the parsed structure becomes |
| 908 | * invalid. |
| 909 | */ |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 910 | msg = bsc_nat_rewrite_setup(bsc->nat, msg, parsed, con->imsi); |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 911 | talloc_free(parsed); |
| 912 | parsed = NULL; |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 913 | } |
Holger Hans Peter Freyther | 655e975 | 2010-10-06 00:48:36 +0800 | [diff] [blame] | 914 | |
| 915 | con_bsc = con->bsc; |
| 916 | con_msc = con->msc_con; |
| 917 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 918 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 919 | |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 920 | break; |
| 921 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 922 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 923 | if (con) { |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 924 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 925 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c3dccc7 | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 926 | con_filter = con->con_local; |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 927 | } |
Holger Hans Peter Freyther | 38712d0 | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 928 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 929 | break; |
| 930 | case SCCP_MSG_TYPE_UDT: |
| 931 | /* simply forward everything */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 932 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 933 | break; |
| 934 | default: |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 935 | LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 936 | con = NULL; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 937 | goto exit2; |
| 938 | break; |
| 939 | } |
Holger Hans Peter Freyther | d5cd304 | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 940 | } else if (parsed->ipa_proto == IPAC_PROTO_MGCP) { |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 941 | bsc_mgcp_forward(bsc, msg); |
| 942 | goto exit2; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 943 | } else { |
| 944 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 945 | goto exit2; |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 948 | if (con_msc && con_bsc != bsc) { |
Holger Hans Peter Freyther | 4107f98 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 949 | LOGP(DNAT, LOGL_ERROR, "The connection belongs to a different BTS: input: %d con: %d\n", |
Holger Hans Peter Freyther | df96153 | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 950 | bsc->cfg->nr, con_bsc->cfg->nr); |
Holger Hans Peter Freyther | e83917d | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 951 | goto exit2; |
| 952 | } |
| 953 | |
Holger Hans Peter Freyther | 79a6baf | 2010-09-15 17:39:44 +0800 | [diff] [blame] | 954 | /* do not forward messages to the MSC */ |
| 955 | if (con_filter) |
| 956 | goto exit2; |
| 957 | |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 958 | if (!con_msc) { |
Holger Hans Peter Freyther | 43bd9d1 | 2010-09-15 00:14:48 +0800 | [diff] [blame] | 959 | LOGP(DNAT, LOGL_ERROR, "Not forwarding data bsc_nr: %d ipa: %d type: 0x%x\n", |
| 960 | bsc->cfg->nr, |
| 961 | parsed ? parsed->ipa_proto : -1, |
| 962 | parsed ? parsed->sccp_type : -1); |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 963 | goto exit2; |
| 964 | } |
| 965 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 966 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | 6a2fe9a | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 967 | queue_for_msc(con_msc, msg); |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 968 | if (parsed) |
| 969 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 970 | return 0; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 971 | |
| 972 | exit: |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 973 | /* if we filter out the reset send an ack to the BSC */ |
| 974 | if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) { |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 975 | send_reset_ack(bsc); |
| 976 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 977 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 978 | /* do we know who is handling this? */ |
| 979 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 980 | struct tlv_parsed tvp; |
| 981 | ipaccess_idtag_parse(&tvp, |
| 982 | (unsigned char *) msg->l2h + 2, |
| 983 | msgb_l2len(msg) - 2); |
| 984 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 985 | ipaccess_auth_bsc(&tvp, bsc); |
| 986 | } |
| 987 | |
| 988 | goto exit2; |
Holger Hans Peter Freyther | 722ead8 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 991 | exit2: |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 992 | if (imsi) |
| 993 | talloc_free(imsi); |
Holger Hans Peter Freyther | 171d930 | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 994 | talloc_free(parsed); |
| 995 | msgb_free(msg); |
| 996 | return -1; |
| 997 | |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 998 | exit3: |
| 999 | /* send a SCCP Connection Refused */ |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 1000 | if (imsi) |
| 1001 | talloc_free(imsi); |
Holger Hans Peter Freyther | fa412e5 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 1002 | bsc_send_con_refuse(bsc, parsed, con_type); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1003 | talloc_free(parsed); |
Holger Hans Peter Freyther | 257a8cc | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 1004 | msgb_free(msg); |
| 1005 | return -1; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1008 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1009 | { |
| 1010 | int error; |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1011 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1012 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1013 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1014 | |
| 1015 | if (!msg) { |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1016 | if (error == 0) |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1017 | LOGP(DNAT, LOGL_ERROR, |
| 1018 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 1019 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1020 | else |
Holger Hans Peter Freyther | 3c8c815 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 1021 | LOGP(DNAT, LOGL_ERROR, |
| 1022 | "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n", |
| 1023 | bsc->cfg ? bsc->cfg->nr : -1, error); |
Holger Hans Peter Freyther | bae6b8b | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 1024 | |
Holger Hans Peter Freyther | 1512dc2 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 1025 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1026 | return -1; |
| 1027 | } |
| 1028 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1029 | |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1030 | LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1031 | |
| 1032 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1033 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1034 | |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1035 | /* stop the pong timeout */ |
| 1036 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 1037 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1038 | bsc_del_timer(&bsc->pong_timeout); |
| 1039 | msgb_free(msg); |
| 1040 | return 0; |
Holger Hans Peter Freyther | 453235a | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 1041 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 1042 | send_pong(bsc); |
| 1043 | msgb_free(msg); |
| 1044 | return 0; |
Holger Hans Peter Freyther | 582511a | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1048 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 1049 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 4ce3270 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 1050 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1055 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 1056 | { |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1057 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1058 | int fd, rc, on; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1059 | struct sockaddr_in sa; |
| 1060 | socklen_t sa_len = sizeof(sa); |
| 1061 | |
| 1062 | if (!(what & BSC_FD_READ)) |
| 1063 | return 0; |
| 1064 | |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1065 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 1066 | if (fd < 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1067 | perror("accept"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1068 | return fd; |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 1071 | /* count the reconnect */ |
| 1072 | counter_inc(nat->stats.bsc.reconn); |
| 1073 | |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1074 | /* |
| 1075 | * if we are not connected to a msc... just close the socket |
| 1076 | */ |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 1077 | if (!bsc_nat_msc_is_connected(nat)) { |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1078 | LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1079 | close(fd); |
Holger Hans Peter Freyther | 3399df3 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1083 | on = 1; |
Holger Hans Peter Freyther | d58afeb | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 1084 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1085 | if (rc != 0) |
Holger Hans Peter Freyther | 150fa58 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 1086 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 1087 | |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1088 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | f0374a5 | 2010-07-27 19:21:53 +0800 | [diff] [blame] | 1089 | &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp)); |
Holger Hans Peter Freyther | 9d42d32 | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 1090 | if (rc != 0) |
| 1091 | LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 1092 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1093 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | 738dbdf | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 1094 | /* todo... use GNUtls to see if we want to trust this as a BTS */ |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1095 | |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1096 | /* |
| 1097 | * |
| 1098 | */ |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1099 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1100 | if (!bsc) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1101 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1102 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1103 | return -1; |
| 1104 | } |
| 1105 | |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1106 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1107 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1108 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 1109 | bsc->write_queue.write_cb = bsc_write_cb; |
Holger Hans Peter Freyther | 8401054 | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 1110 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 1111 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 8f99b82 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 1112 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1113 | close(fd); |
Holger Hans Peter Freyther | 4a62960 | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 1114 | talloc_free(bsc); |
| 1115 | return -2; |
| 1116 | } |
| 1117 | |
Holger Hans Peter Freyther | 4cc5809 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 1118 | LOGP(DNAT, LOGL_NOTICE, "BSC connection on %d with IP: %s\n", |
Holger Hans Peter Freyther | 74d0042 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 1119 | fd, inet_ntoa(sa.sin_addr)); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1120 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 809d6fa | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 1121 | send_id_ack(bsc); |
| 1122 | send_id_req(bsc); |
Holger Hans Peter Freyther | 8c61cdb | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 1123 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | de55766 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | * start the hangup timer |
| 1127 | */ |
| 1128 | bsc->id_timeout.data = bsc; |
| 1129 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 1130 | bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1131 | return 0; |
| 1132 | } |
| 1133 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1134 | static void print_usage() |
| 1135 | { |
| 1136 | printf("Usage: bsc_nat\n"); |
| 1137 | } |
| 1138 | |
| 1139 | static void print_help() |
| 1140 | { |
| 1141 | printf(" Some useful help...\n"); |
| 1142 | printf(" -h --help this text\n"); |
| 1143 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1144 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1145 | printf(" -s --disable-color\n"); |
| 1146 | printf(" -c --config-file filename The config file to use.\n"); |
| 1147 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1148 | printf(" -l --local=IP. The local address of this BSC.\n"); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1151 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1152 | { |
| 1153 | while (1) { |
| 1154 | int option_index = 0, c; |
| 1155 | static struct option long_options[] = { |
| 1156 | {"help", 0, 0, 'h'}, |
| 1157 | {"debug", 1, 0, 'd'}, |
| 1158 | {"config-file", 1, 0, 'c'}, |
| 1159 | {"disable-color", 0, 0, 's'}, |
| 1160 | {"timestamp", 0, 0, 'T'}, |
| 1161 | {"msc", 1, 0, 'm'}, |
| 1162 | {"local", 1, 0, 'l'}, |
| 1163 | {0, 0, 0, 0} |
| 1164 | }; |
| 1165 | |
| 1166 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 1167 | long_options, &option_index); |
| 1168 | if (c == -1) |
| 1169 | break; |
| 1170 | |
| 1171 | switch (c) { |
| 1172 | case 'h': |
| 1173 | print_usage(); |
| 1174 | print_help(); |
| 1175 | exit(0); |
| 1176 | case 's': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1177 | log_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1178 | break; |
| 1179 | case 'd': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1180 | log_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1181 | break; |
| 1182 | case 'c': |
| 1183 | config_file = strdup(optarg); |
| 1184 | break; |
| 1185 | case 'T': |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1186 | log_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1187 | break; |
| 1188 | case 'm': |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1189 | msc_ip = optarg; |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1190 | break; |
| 1191 | case 'l': |
| 1192 | inet_aton(optarg, &local_addr); |
| 1193 | break; |
| 1194 | default: |
| 1195 | /* ignore */ |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1201 | static void signal_handler(int signal) |
| 1202 | { |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1203 | switch (signal) { |
| 1204 | case SIGABRT: |
| 1205 | /* in case of abort, we want to obtain a talloc report |
| 1206 | * and then return to the caller, who will abort the process */ |
| 1207 | case SIGUSR1: |
| 1208 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1209 | break; |
| 1210 | default: |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1215 | static void sccp_close_unconfirmed(void *_data) |
| 1216 | { |
| 1217 | struct sccp_connections *conn, *tmp1; |
| 1218 | struct timespec now; |
| 1219 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 1220 | |
| 1221 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 1222 | if (conn->has_remote_ref) |
| 1223 | continue; |
| 1224 | |
| 1225 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 1226 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 1227 | continue; |
| 1228 | |
| 1229 | LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n", |
| 1230 | sccp_src_ref_to_int(&conn->real_ref), |
| 1231 | sccp_src_ref_to_int(&conn->patched_ref)); |
| 1232 | sccp_connection_destroy(conn); |
| 1233 | } |
| 1234 | |
| 1235 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1236 | } |
| 1237 | |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1238 | extern void *tall_msgb_ctx; |
| 1239 | extern void *tall_ctr_ctx; |
| 1240 | static void talloc_init_ctx() |
| 1241 | { |
| 1242 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 1243 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1244 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 1245 | } |
| 1246 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1247 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 1248 | |
| 1249 | static struct vty_app_info vty_info = { |
| 1250 | .name = "BSC NAT", |
| 1251 | .version = PACKAGE_VERSION, |
| 1252 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 1253 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1254 | }; |
| 1255 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1256 | int main(int argc, char **argv) |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1257 | { |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1258 | int rc; |
Holger Hans Peter Freyther | 023a550 | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1259 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1260 | talloc_init_ctx(); |
Holger Hans Peter Freyther | 0937138 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 1261 | |
Holger Hans Peter Freyther | 4acca1a | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1262 | log_init(&log_info); |
| 1263 | stderr_target = log_target_create_stderr(); |
| 1264 | log_add_target(stderr_target); |
| 1265 | log_set_all_filter(stderr_target, 1); |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 1266 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1267 | nat = bsc_nat_alloc(); |
| 1268 | if (!nat) { |
| 1269 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 1270 | return -4; |
| 1271 | } |
| 1272 | |
Holger Hans Peter Freyther | 774d1d3 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1273 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1274 | if (!nat->mgcp_cfg) { |
| 1275 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 1276 | return -5; |
| 1277 | } |
| 1278 | |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1279 | vty_info.copyright = openbsc_copyright; |
| 1280 | vty_init(&vty_info); |
| 1281 | logging_vty_add_cmds(); |
| 1282 | bsc_nat_vty_init(nat); |
| 1283 | |
| 1284 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1285 | /* parse options */ |
| 1286 | local_addr.s_addr = INADDR_ANY; |
| 1287 | handle_options(argc, argv); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1288 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1289 | rate_ctr_init(tall_bsc_ctx); |
| 1290 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1291 | /* init vty and parse */ |
Holger Hans Peter Freyther | 4fcedba | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1292 | telnet_init(tall_bsc_ctx, NULL, 4244); |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1293 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1294 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1295 | return -3; |
| 1296 | } |
| 1297 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1298 | /* over rule the VTY config */ |
| 1299 | if (msc_ip) |
| 1300 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1301 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1302 | /* seed the PRNG */ |
| 1303 | srand(time(NULL)); |
| 1304 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1305 | /* |
| 1306 | * Setup the MGCP code.. |
| 1307 | */ |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1308 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1309 | return -4; |
| 1310 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1311 | /* connect to the MSC */ |
Holger Hans Peter Freyther | 7ba0c54 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 1312 | nat->msc_con = bsc_msc_create(nat->msc_ip, nat->msc_port, 0); |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1313 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1314 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1315 | exit(1); |
| 1316 | } |
| 1317 | |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1318 | nat->msc_con->connection_loss = msc_connection_was_lost; |
Holger Hans Peter Freyther | 0cb4fd9 | 2010-08-04 02:31:55 +0800 | [diff] [blame] | 1319 | nat->msc_con->connected = msc_connection_connected; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1320 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1321 | nat->msc_con->write_queue.write_cb = ipaccess_msc_write_cb;; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 1322 | nat->msc_con->write_queue.bfd.data = nat->msc_con; |
Holger Hans Peter Freyther | 8e2e0ac | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1323 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1324 | |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1325 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 5d0a027 | 2010-10-19 17:48:13 +0200 | [diff] [blame] | 1326 | rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr), |
Holger Hans Peter Freyther | 492a006 | 2010-10-12 23:28:28 +0200 | [diff] [blame] | 1327 | 5000, ipaccess_listen_bsc_cb); |
| 1328 | if (rc != 0) { |
Holger Hans Peter Freyther | e8fa0f1 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1329 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1330 | exit(1); |
| 1331 | } |
| 1332 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 1333 | rc = bsc_ussd_init(nat); |
| 1334 | if (rc != 0) { |
| 1335 | LOGP(DNAT, LOGL_ERROR, "Failed to bind the USSD socket.\n"); |
| 1336 | exit(1); |
| 1337 | } |
| 1338 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1339 | signal(SIGABRT, &signal_handler); |
| 1340 | signal(SIGUSR1, &signal_handler); |
| 1341 | signal(SIGPIPE, SIG_IGN); |
| 1342 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 1343 | if (daemonize) { |
| 1344 | rc = osmo_daemonize(); |
| 1345 | if (rc < 0) { |
| 1346 | perror("Error during daemonize"); |
| 1347 | exit(1); |
| 1348 | } |
| 1349 | } |
| 1350 | |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1351 | /* recycle timer */ |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1352 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 6088cc7 | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1353 | sccp_close.cb = sccp_close_unconfirmed; |
| 1354 | sccp_close.data = NULL; |
| 1355 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1356 | |
Holger Hans Peter Freyther | e907cb2 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1357 | while (1) { |
| 1358 | bsc_select_main(0); |
| 1359 | } |
| 1360 | |
Holger Hans Peter Freyther | 89d9fd9 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* Close all connections handed out to the USSD module */ |
| 1365 | int bsc_close_ussd_connections(struct bsc_nat *nat) |
| 1366 | { |
| 1367 | struct sccp_connections *con; |
| 1368 | llist_for_each_entry(con, &nat->sccp_connections, list_entry) { |
| 1369 | if (con->con_local != 2) |
| 1370 | continue; |
| 1371 | if (!con->bsc) |
| 1372 | continue; |
| 1373 | |
Holger Hans Peter Freyther | 2c7f274 | 2010-10-27 11:58:04 +0200 | [diff] [blame] | 1374 | nat_send_clrc_bsc(con); |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 1375 | nat_send_rlsd_bsc(con); |
| 1376 | } |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |