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 |
| 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 | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 30 | #include <signal.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5aa25ae | 2010-01-12 21:36:08 +0100 | [diff] [blame] | 33 | #include <time.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 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 | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 40 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 41 | #include <openbsc/bsc_nat.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 | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 44 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 45 | |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 46 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 47 | #include <osmocore/talloc.h> |
| 48 | |
Holger Hans Peter Freyther | 69d801e | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 49 | #include <osmocore/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 50 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 51 | #include <osmocom/vty/telnet_interface.h> |
| 52 | #include <osmocom/vty/vty.h> |
| 53 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 54 | #include <sccp/sccp.h> |
| 55 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 56 | #include "../../bscconfig.h" |
| 57 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 58 | #define SCCP_CLOSE_TIME 20 |
| 59 | #define SCCP_CLOSE_TIME_TIMEOUT 19 |
| 60 | |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 61 | struct log_target *stderr_target; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 62 | static const char *config_file = "bsc-nat.cfg"; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 63 | static struct in_addr local_addr; |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 64 | static struct bsc_fd bsc_listen; |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 65 | static const char *msc_ip = NULL; |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 66 | static struct timer_list sccp_close; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 67 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 68 | const char *openbsc_copyright = |
| 69 | "Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\n" |
| 70 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 71 | "This is free software: you are free to change and redistribute it.\n" |
| 72 | "There is NO WARRANTY, to the extent permitted by law.\n"; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 73 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 74 | static struct bsc_nat *nat; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 75 | 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] | 76 | static void msc_send_reset(struct bsc_msc_connection *con); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 77 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 78 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num) |
| 79 | { |
| 80 | struct bsc_config *conf; |
| 81 | |
| 82 | llist_for_each_entry(conf, &nat->bsc_configs, entry) |
| 83 | if (conf->nr == num) |
| 84 | return conf; |
| 85 | |
| 86 | return NULL; |
| 87 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 88 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 89 | /* |
| 90 | * below are stubs we need to link |
| 91 | */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 92 | int nm_state_event(enum nm_evt evt, uint8_t obj_class, void *obj, |
Holger Hans Peter Freyther | 2f2d342 | 2010-05-12 16:55:12 +0800 | [diff] [blame] | 93 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state, |
| 94 | struct abis_om_obj_inst *obj_ins) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 95 | { |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 100 | {} |
| 101 | |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 102 | static void queue_for_msc(struct bsc_msc_connection *con, struct msgb *msg) |
| 103 | { |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 104 | if (write_queue_enqueue(&con->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 9d56d0c | 2010-04-22 19:11:37 +0800 | [diff] [blame] | 105 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 106 | msgb_free(msg); |
| 107 | } |
| 108 | } |
| 109 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 110 | static void send_reset_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 111 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 112 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 113 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 114 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 115 | 0x00, 0x01, 0x31, |
| 116 | }; |
| 117 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 118 | 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] | 119 | } |
| 120 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 121 | static void send_ping(struct bsc_connection *bsc) |
| 122 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 123 | static const uint8_t id_ping[] = { |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 124 | IPAC_MSGT_PING, |
| 125 | }; |
| 126 | |
| 127 | bsc_send_data(bsc, id_ping, sizeof(id_ping), IPAC_PROTO_IPACCESS); |
| 128 | } |
| 129 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 130 | static void send_pong(struct bsc_connection *bsc) |
| 131 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 132 | static const uint8_t id_pong[] = { |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 133 | IPAC_MSGT_PONG, |
| 134 | }; |
| 135 | |
| 136 | bsc_send_data(bsc, id_pong, sizeof(id_pong), IPAC_PROTO_IPACCESS); |
| 137 | } |
| 138 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 139 | static void bsc_pong_timeout(void *_bsc) |
| 140 | { |
| 141 | struct bsc_connection *bsc = _bsc; |
| 142 | |
| 143 | LOGP(DNAT, LOGL_ERROR, "BSC Nr: %d PONG timeout.\n", bsc->cfg->nr); |
| 144 | bsc_close_connection(bsc); |
| 145 | } |
| 146 | |
| 147 | static void bsc_ping_timeout(void *_bsc) |
| 148 | { |
| 149 | struct bsc_connection *bsc = _bsc; |
| 150 | |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 151 | if (bsc->nat->ping_timeout < 0) |
| 152 | return; |
| 153 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 154 | send_ping(bsc); |
| 155 | |
| 156 | /* send another ping in 20 seconds */ |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | /* also start a pong timer */ |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 160 | 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] | 161 | } |
| 162 | |
| 163 | static void start_ping_pong(struct bsc_connection *bsc) |
| 164 | { |
| 165 | bsc->pong_timeout.data = bsc; |
| 166 | bsc->pong_timeout.cb = bsc_pong_timeout; |
| 167 | bsc->ping_timeout.data = bsc; |
| 168 | bsc->ping_timeout.cb = bsc_ping_timeout; |
| 169 | |
| 170 | bsc_ping_timeout(bsc); |
| 171 | } |
| 172 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 173 | static void send_id_ack(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 174 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 175 | static const uint8_t id_ack[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 176 | IPAC_MSGT_ID_ACK |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 179 | 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] | 180 | } |
| 181 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 182 | static void send_id_req(struct bsc_connection *bsc) |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 183 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 184 | static const uint8_t id_req[] = { |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 185 | IPAC_MSGT_ID_GET, |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 186 | 0x01, IPAC_IDTAG_UNIT, |
| 187 | 0x01, IPAC_IDTAG_MACADDR, |
| 188 | 0x01, IPAC_IDTAG_LOCATION1, |
| 189 | 0x01, IPAC_IDTAG_LOCATION2, |
| 190 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 191 | 0x01, IPAC_IDTAG_SWVERSION, |
| 192 | 0x01, IPAC_IDTAG_UNITNAME, |
| 193 | 0x01, IPAC_IDTAG_SERNR, |
| 194 | }; |
| 195 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 196 | 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] | 197 | } |
| 198 | |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 199 | static void nat_send_rlsd(struct sccp_connections *conn) |
| 200 | { |
| 201 | struct sccp_connection_released *rel; |
| 202 | struct msgb *msg; |
| 203 | |
| 204 | msg = msgb_alloc_headroom(4096, 128, "rlsd"); |
| 205 | if (!msg) { |
| 206 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | msg->l2h = msgb_put(msg, sizeof(*rel)); |
| 211 | rel = (struct sccp_connection_released *) msg->l2h; |
| 212 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 213 | rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE; |
| 214 | rel->destination_local_reference = conn->remote_ref; |
| 215 | rel->source_local_reference = conn->patched_ref; |
| 216 | |
| 217 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 218 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 219 | queue_for_msc(conn->msc_con, msg); |
Holger Hans Peter Freyther | 6b087d1 | 2010-04-06 17:32:58 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 222 | static void nat_send_rlc(struct bsc_msc_connection *msc_con, |
| 223 | struct sccp_source_reference *src, |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 224 | struct sccp_source_reference *dst) |
| 225 | { |
| 226 | struct sccp_connection_release_complete *rlc; |
| 227 | struct msgb *msg; |
| 228 | |
| 229 | msg = msgb_alloc_headroom(4096, 128, "rlc"); |
| 230 | if (!msg) { |
| 231 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n"); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | msg->l2h = msgb_put(msg, sizeof(*rlc)); |
| 236 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 237 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 238 | rlc->destination_local_reference = *dst; |
| 239 | rlc->source_local_reference = *src; |
| 240 | |
| 241 | ipaccess_prepend_header(msg, IPAC_PROTO_SCCP); |
| 242 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 243 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 246 | static void send_mgcp_reset(struct bsc_connection *bsc) |
| 247 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 248 | static const uint8_t mgcp_reset[] = { |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 249 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 250 | }; |
| 251 | |
| 252 | bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1); |
| 253 | } |
| 254 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 255 | /* |
| 256 | * Below is the handling of messages coming |
| 257 | * from the MSC and need to be forwarded to |
| 258 | * a real BSC. |
| 259 | */ |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 260 | 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] | 261 | { |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 262 | if (msc_con->first_contact) |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 263 | return; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 264 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 265 | msc_con->first_contact = 1; |
| 266 | msc_send_reset(msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 269 | 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] | 270 | { |
| 271 | struct msgb *msg = bsc_msc_id_get_resp(nat->token); |
| 272 | if (!msg) |
| 273 | return; |
| 274 | |
| 275 | ipaccess_prepend_header(msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 276 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 279 | /* |
| 280 | * Currently we are lacking refcounting so we need to copy each message. |
| 281 | */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 282 | 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] | 283 | { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 284 | struct msgb *msg; |
| 285 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 286 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 287 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 291 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 292 | if (!msg) { |
| 293 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 294 | return; |
| 295 | } |
| 296 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 297 | msg->l2h = msgb_put(msg, length); |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 298 | memcpy(msg->data, data, length); |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 299 | |
| 300 | bsc_write(bsc, msg, proto); |
Holger Hans Peter Freyther | f7cb33c | 2010-03-26 07:20:59 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 303 | static void bsc_send_con_refuse(struct bsc_connection *bsc, |
| 304 | struct bsc_nat_parsed *parsed, int con_type) |
| 305 | { |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 306 | struct msgb *payload; |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 307 | struct msgb *refuse; |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 308 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 309 | if (con_type == NAT_CON_TYPE_LU) |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 310 | 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] | 311 | else if (con_type == NAT_CON_TYPE_CM_SERV_REQ) |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 312 | 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] | 313 | else { |
| 314 | LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); |
| 315 | payload = NULL; |
| 316 | } |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Some BSCs do not handle the payload inside a SCCP CREF msg |
| 320 | * so we will need to: |
| 321 | * 1.) Allocate a local connection and mark it as local.. |
| 322 | * 2.) queue data for downstream.. and the RLC should delete everything |
| 323 | */ |
| 324 | if (payload) { |
| 325 | struct msgb *cc, *udt, *rlsd; |
| 326 | struct sccp_connections *con; |
| 327 | con = create_sccp_src_ref(bsc, parsed); |
| 328 | if (!con) |
| 329 | goto send_refuse; |
| 330 | |
| 331 | /* declare it local and assign a unique remote_ref */ |
| 332 | con->con_type = NAT_CON_TYPE_LOCAL_REJECT; |
| 333 | con->con_local = 1; |
| 334 | con->has_remote_ref = 1; |
| 335 | con->remote_ref = con->patched_ref; |
| 336 | |
| 337 | /* 1. create a confirmation */ |
| 338 | cc = sccp_create_cc(&con->remote_ref, &con->real_ref); |
| 339 | if (!cc) |
| 340 | goto send_refuse; |
| 341 | |
| 342 | /* 2. create the DT1 */ |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 343 | gsm0808_prepend_dtap_header(payload, 0); |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 344 | udt = sccp_create_dt1(&con->real_ref, payload->data, payload->len); |
| 345 | if (!udt) { |
| 346 | msgb_free(cc); |
| 347 | goto send_refuse; |
| 348 | } |
| 349 | |
| 350 | /* 3. send a RLSD */ |
| 351 | rlsd = sccp_create_rlsd(&con->remote_ref, &con->real_ref, |
| 352 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 353 | if (!rlsd) { |
| 354 | msgb_free(cc); |
| 355 | msgb_free(udt); |
| 356 | goto send_refuse; |
| 357 | } |
| 358 | |
| 359 | bsc_write(bsc, cc, IPAC_PROTO_SCCP); |
| 360 | bsc_write(bsc, udt, IPAC_PROTO_SCCP); |
| 361 | bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); |
| 362 | msgb_free(payload); |
| 363 | return; |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 364 | } |
| 365 | |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 366 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 367 | send_refuse: |
Holger Hans Peter Freyther | ed775e4 | 2010-06-15 18:52:24 +0800 | [diff] [blame] | 368 | if (payload) |
| 369 | msgb_free(payload); |
| 370 | |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 371 | refuse = sccp_create_refuse(parsed->src_local_ref, |
| 372 | SCCP_REFUSAL_SCCP_FAILURE, NULL, 0); |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 373 | if (!refuse) { |
| 374 | LOGP(DNAT, LOGL_ERROR, |
| 375 | "Creating refuse msg failed for SCCP 0x%x on BSC Nr: %d.\n", |
| 376 | sccp_src_ref_to_int(parsed->src_local_ref), bsc->cfg->nr); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | bsc_write(bsc, refuse, IPAC_PROTO_SCCP); |
| 381 | } |
| 382 | |
| 383 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 384 | 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] | 385 | { |
Holger Hans Peter Freyther | 8d7b10e | 2010-07-23 19:43:12 +0800 | [diff] [blame] | 386 | struct sccp_connections *con = NULL; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 387 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 388 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 389 | int proto; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 390 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 391 | /* filter, drop, patch the message? */ |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 392 | parsed = bsc_nat_parse(msg); |
| 393 | if (!parsed) { |
| 394 | 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] | 395 | return -1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 398 | if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 399 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 400 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 401 | proto = parsed->ipa_proto; |
| 402 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 403 | /* Route and modify the SCCP packet */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 404 | if (proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 405 | switch (parsed->sccp_type) { |
| 406 | case SCCP_MSG_TYPE_UDT: |
| 407 | /* forward UDT messages to every BSC */ |
| 408 | goto send_to_all; |
| 409 | break; |
| 410 | case SCCP_MSG_TYPE_RLSD: |
| 411 | case SCCP_MSG_TYPE_CREF: |
| 412 | case SCCP_MSG_TYPE_DT1: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 413 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 414 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 415 | if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) { |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 416 | counter_inc(nat->stats.sccp.calls); |
| 417 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 418 | if (con) { |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 419 | struct rate_ctr_group *ctrg; |
| 420 | ctrg = con->bsc->cfg->stats.ctrg; |
| 421 | rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 422 | if (bsc_mgcp_assign(con, msg) != 0) |
| 423 | LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n"); |
| 424 | } else |
| 425 | LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n"); |
| 426 | } |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 427 | break; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 428 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 429 | con = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 430 | if (!con || update_sccp_src_ref(con, parsed) != 0) |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 431 | goto exit; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 432 | break; |
| 433 | case SCCP_MSG_TYPE_RLC: |
| 434 | LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n"); |
| 435 | goto exit; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 436 | break; |
| 437 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 438 | /* MSC never opens a SCCP connection, fall through */ |
| 439 | default: |
| 440 | goto exit; |
| 441 | } |
Holger Hans Peter Freyther | f464ea5 | 2010-04-06 16:07:44 +0200 | [diff] [blame] | 442 | |
Holger Hans Peter Freyther | b8a3373 | 2010-04-08 11:28:12 +0200 | [diff] [blame] | 443 | if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) { |
| 444 | LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n"); |
| 445 | /* Exchange src/dest for the reply */ |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 446 | 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] | 447 | } else if (!con) |
Holger Hans Peter Freyther | 5f10c6d | 2010-05-15 19:13:52 +0800 | [diff] [blame] | 448 | 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] | 449 | } |
| 450 | |
| 451 | talloc_free(parsed); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 452 | if (!con) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 453 | return -1; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 454 | if (!con->bsc->authenticated) { |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 455 | LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n"); |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 456 | return -1; |
| 457 | } |
| 458 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 459 | 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] | 460 | return 0; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 461 | |
| 462 | send_to_all: |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 463 | /* |
| 464 | * Filter Paging from the network. We do not want to send a PAGING |
| 465 | * Command to every BSC in our network. We will analys the PAGING |
| 466 | * message and then send it to the authenticated messages... |
| 467 | */ |
| 468 | 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] | 469 | int lac; |
| 470 | bsc = bsc_nat_find_bsc(nat, msg, &lac); |
Holger Hans Peter Freyther | 62e5843 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 471 | if (bsc && bsc->cfg->forbid_paging) |
Holger Hans Peter Freyther | a34585e | 2010-04-21 20:17:18 +0800 | [diff] [blame] | 472 | 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] | 473 | else if (bsc) |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 474 | bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto); |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 475 | else |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 476 | LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging on lac: %d/0x%x\n", |
| 477 | lac, lac); |
Holger Hans Peter Freyther | 45d1181 | 2010-06-15 18:46:36 +0800 | [diff] [blame] | 478 | |
| 479 | goto exit; |
| 480 | } |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 481 | /* currently send this to every BSC connected */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 482 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 3f37b8f | 2010-02-08 23:24:32 +0100 | [diff] [blame] | 483 | if (!bsc->authenticated) |
| 484 | continue; |
| 485 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 486 | 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] | 487 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 488 | |
| 489 | exit: |
| 490 | talloc_free(parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 491 | return 0; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 494 | static void msc_connection_was_lost(struct bsc_msc_connection *con) |
| 495 | { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 496 | struct bsc_connection *bsc, *tmp; |
| 497 | |
Holger Hans Peter Freyther | a4ed81c | 2010-04-12 12:35:02 +0200 | [diff] [blame] | 498 | counter_inc(nat->stats.msc.reconn); |
| 499 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 500 | LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n"); |
| 501 | 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] | 502 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 503 | |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 504 | bsc_mgcp_free_endpoints(nat); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 505 | bsc_msc_schedule_connect(con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 506 | } |
| 507 | |
Holger Hans Peter Freyther | 02aee14 | 2010-04-08 10:31:07 +0200 | [diff] [blame] | 508 | 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] | 509 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 510 | static const uint8_t reset[] = { |
Holger Hans Peter Freyther | 7cab166 | 2010-04-07 11:11:11 +0200 | [diff] [blame] | 511 | 0x00, 0x12, 0xfd, |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 512 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 513 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 514 | 0x01, 0x20 |
| 515 | }; |
| 516 | |
| 517 | struct msgb *msg; |
| 518 | |
| 519 | msg = msgb_alloc_headroom(4096, 128, "08.08 reset"); |
| 520 | if (!msg) { |
| 521 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n"); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | msg->l2h = msgb_put(msg, sizeof(reset)); |
| 526 | memcpy(msg->l2h, reset, msgb_l2len(msg)); |
| 527 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 528 | queue_for_msc(msc_con, msg); |
Holger Hans Peter Freyther | af0ff6c | 2010-04-07 10:46:30 +0200 | [diff] [blame] | 529 | |
| 530 | LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n"); |
| 531 | } |
| 532 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 533 | static int ipaccess_msc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 534 | { |
| 535 | int error; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 536 | struct bsc_msc_connection *msc_con; |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 537 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 538 | struct ipaccess_head *hh; |
| 539 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 540 | msc_con = (struct bsc_msc_connection *) bfd->data; |
| 541 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 542 | if (!msg) { |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 543 | if (error == 0) |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 544 | 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] | 545 | else |
| 546 | 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] | 547 | |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 548 | bsc_msc_lost(msc_con); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 552 | 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] | 553 | |
| 554 | /* handle base message handling */ |
| 555 | hh = (struct ipaccess_head *) msg->data; |
| 556 | ipaccess_rcvmsg_base(msg, bfd); |
| 557 | |
| 558 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 559 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 560 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 561 | initialize_msc_if_needed(msc_con); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 562 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 563 | send_id_get_response(msc_con); |
Holger Hans Peter Freyther | e635dab | 2010-05-15 00:14:58 +0800 | [diff] [blame] | 564 | } else if (hh->proto == IPAC_PROTO_SCCP) |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 565 | forward_sccp_to_bts(msc_con, msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 566 | |
Holger Hans Peter Freyther | aad68b5 | 2010-06-15 18:46:48 +0800 | [diff] [blame] | 567 | msgb_free(msg); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 568 | return 0; |
| 569 | } |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 570 | |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 571 | static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 572 | { |
| 573 | int rc; |
| 574 | rc = write(bfd->fd, msg->data, msg->len); |
| 575 | |
| 576 | if (rc != msg->len) { |
| 577 | LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | return rc; |
| 582 | } |
| 583 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 584 | /* |
| 585 | * Below is the handling of messages coming |
| 586 | * from the BSC and need to be forwarded to |
| 587 | * a real BSC. |
| 588 | */ |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * Remove the connection from the connections list, |
| 592 | * remove it from the patching of SCCP header lists |
| 593 | * as well. Maybe in the future even close connection.. |
| 594 | */ |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 595 | void bsc_close_connection(struct bsc_connection *connection) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 596 | { |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 597 | struct sccp_connections *sccp_patch, *tmp; |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 598 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 599 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 600 | /* stop the timeout timer */ |
| 601 | bsc_del_timer(&connection->id_timeout); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 602 | bsc_del_timer(&connection->ping_timeout); |
| 603 | bsc_del_timer(&connection->pong_timeout); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 604 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 605 | if (connection->cfg) |
| 606 | ctr = &connection->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_SCCP]; |
| 607 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 608 | /* remove all SCCP connections */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 609 | 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] | 610 | if (sccp_patch->bsc != connection) |
| 611 | continue; |
| 612 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 613 | if (ctr) |
| 614 | rate_ctr_inc(ctr); |
Holger Hans Peter Freyther | 4c683d1 | 2010-04-23 14:13:27 +0800 | [diff] [blame] | 615 | if (sccp_patch->has_remote_ref) |
| 616 | nat_send_rlsd(sccp_patch); |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 617 | sccp_connection_destroy(sccp_patch); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 620 | /* close endpoints allocated by this BSC */ |
| 621 | bsc_mgcp_clear_endpoints_for(connection); |
| 622 | |
Holger Hans Peter Freyther | e464ed4 | 2010-04-22 12:04:36 +0800 | [diff] [blame] | 623 | bsc_unregister_fd(&connection->write_queue.bfd); |
| 624 | close(connection->write_queue.bfd.fd); |
| 625 | write_queue_clear(&connection->write_queue); |
| 626 | llist_del(&connection->list_entry); |
| 627 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 628 | talloc_free(connection); |
| 629 | } |
| 630 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 631 | static void ipaccess_close_bsc(void *data) |
| 632 | { |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 633 | struct sockaddr_in sock; |
| 634 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 635 | struct bsc_connection *conn = data; |
| 636 | |
Holger Hans Peter Freyther | 6d5a600 | 2010-04-17 07:58:17 +0200 | [diff] [blame] | 637 | |
| 638 | getpeername(conn->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 639 | LOGP(DNAT, LOGL_ERROR, "BSC on %s didn't respond to identity request. Closing.\n", |
| 640 | inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 641 | bsc_close_connection(conn); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc) |
| 645 | { |
| 646 | struct bsc_config *conf; |
| 647 | const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 648 | |
Holger Hans Peter Freyther | 5cdcfa6 | 2010-05-02 18:58:10 +0800 | [diff] [blame] | 649 | if (bsc->cfg) { |
| 650 | LOGP(DNAT, LOGL_ERROR, "Reauth on fd %d bsc nr %d\n", |
| 651 | bsc->write_queue.bfd.fd, bsc->cfg->nr); |
| 652 | return; |
| 653 | } |
| 654 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 655 | llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) { |
| 656 | if (strcmp(conf->token, token) == 0) { |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 657 | 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] | 658 | bsc->authenticated = 1; |
Holger Hans Peter Freyther | 47dd494 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 659 | bsc->cfg = conf; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 660 | bsc_del_timer(&bsc->id_timeout); |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 661 | LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d on fd %d\n", |
| 662 | conf->nr, conf->lac, bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 663 | start_ping_pong(bsc); |
Holger Hans Peter Freyther | c615c26 | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 664 | return; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 665 | } |
| 666 | } |
Holger Hans Peter Freyther | c615c26 | 2010-04-17 07:59:57 +0200 | [diff] [blame] | 667 | |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 668 | LOGP(DNAT, LOGL_ERROR, "No bsc found for token %s on fd: %d.\n", token, |
| 669 | bsc->write_queue.bfd.fd); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 670 | } |
| 671 | |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 672 | 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] | 673 | { |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 674 | int con_filter = 0; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 675 | struct bsc_msc_connection *con_msc = NULL; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 676 | struct bsc_connection *con_bsc = NULL; |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 677 | int con_type; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 678 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 679 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 680 | /* Parse and filter messages */ |
| 681 | parsed = bsc_nat_parse(msg); |
| 682 | if (!parsed) { |
| 683 | 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] | 684 | msgb_free(msg); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 688 | if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed)) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 689 | goto exit; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 690 | |
Holger Hans Peter Freyther | bbb9d39 | 2010-04-02 03:42:44 +0200 | [diff] [blame] | 691 | /* |
| 692 | * check authentication after filtering to not reject auth |
| 693 | * responses coming from the BSC. We have to make sure that |
| 694 | * nothing from the exit path will forward things to the MSC |
| 695 | */ |
| 696 | if (!bsc->authenticated) { |
| 697 | LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n"); |
| 698 | msgb_free(msg); |
| 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 703 | /* modify the SCCP entries */ |
| 704 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 705 | struct sccp_connections *con; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 706 | switch (parsed->sccp_type) { |
| 707 | case SCCP_MSG_TYPE_CR: |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 708 | if (bsc_nat_filter_sccp_cr(bsc, msg, parsed, &con_type) != 0) |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 709 | goto exit3; |
Holger Hans Peter Freyther | fa20c94 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 710 | if (!create_sccp_src_ref(bsc, parsed)) |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 711 | goto exit2; |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 712 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 3a77e61 | 2010-07-05 13:33:18 +0800 | [diff] [blame] | 713 | con->msc_con = bsc->nat->msc_con; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 714 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | 234d312 | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 715 | con->con_type = con_type; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 716 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 717 | break; |
| 718 | case SCCP_MSG_TYPE_RLSD: |
| 719 | case SCCP_MSG_TYPE_CREF: |
| 720 | case SCCP_MSG_TYPE_DT1: |
| 721 | case SCCP_MSG_TYPE_CC: |
Holger Hans Peter Freyther | f46ce53 | 2010-04-06 10:22:34 +0200 | [diff] [blame] | 722 | case SCCP_MSG_TYPE_IT: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 723 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 724 | if (con) { |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 725 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 726 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 727 | con_filter = con->con_local; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 728 | } |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 729 | break; |
| 730 | case SCCP_MSG_TYPE_RLC: |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 731 | con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 732 | if (con) { |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 733 | con_bsc = con->bsc; |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 734 | con_msc = con->msc_con; |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 735 | con_filter = con->con_local; |
Holger Hans Peter Freyther | 605f62a | 2010-05-16 16:30:42 +0800 | [diff] [blame] | 736 | } |
Holger Hans Peter Freyther | f4cfc4f | 2010-03-31 09:15:05 +0200 | [diff] [blame] | 737 | remove_sccp_src_ref(bsc, msg, parsed); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 738 | break; |
| 739 | case SCCP_MSG_TYPE_UDT: |
| 740 | /* simply forward everything */ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 741 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 742 | break; |
| 743 | default: |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 744 | 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] | 745 | con = NULL; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 746 | goto exit2; |
| 747 | break; |
| 748 | } |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 749 | } else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) { |
| 750 | bsc_mgcp_forward(bsc, msg); |
| 751 | goto exit2; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 752 | } else { |
| 753 | LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto); |
| 754 | goto exit2; |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 757 | if (con_msc && con_bsc != bsc) { |
Holger Hans Peter Freyther | ca0c2f9 | 2010-04-21 18:49:55 +0800 | [diff] [blame] | 758 | 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] | 759 | bsc->cfg->nr, con_bsc->cfg->nr); |
Holger Hans Peter Freyther | 058eeb7 | 2010-01-31 09:46:21 +0100 | [diff] [blame] | 760 | goto exit2; |
| 761 | } |
| 762 | |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 763 | if (!con_msc) { |
| 764 | LOGP(DNAT, LOGL_ERROR, "No connection found, dropping data.\n"); |
| 765 | goto exit2; |
| 766 | } |
| 767 | |
Holger Hans Peter Freyther | c58da4b | 2010-05-16 16:36:36 +0800 | [diff] [blame] | 768 | /* do not forward messages to the MSC */ |
| 769 | if (con_filter) |
| 770 | goto exit2; |
| 771 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 772 | /* send the non-filtered but maybe modified msg */ |
Holger Hans Peter Freyther | db65bbd | 2010-07-05 13:58:40 +0800 | [diff] [blame] | 773 | queue_for_msc(con_msc, msg); |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 774 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 775 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 776 | |
| 777 | exit: |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 778 | /* if we filter out the reset send an ack to the BSC */ |
| 779 | 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] | 780 | send_reset_ack(bsc); |
| 781 | send_reset_ack(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 782 | } else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) { |
| 783 | /* do we know who is handling this? */ |
| 784 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 785 | struct tlv_parsed tvp; |
| 786 | ipaccess_idtag_parse(&tvp, |
| 787 | (unsigned char *) msg->l2h + 2, |
| 788 | msgb_l2len(msg) - 2); |
| 789 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 790 | ipaccess_auth_bsc(&tvp, bsc); |
| 791 | } |
| 792 | |
| 793 | goto exit2; |
Holger Hans Peter Freyther | 38a77d0 | 2010-01-30 12:45:10 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Holger Hans Peter Freyther | 1498d2e | 2010-05-15 00:29:50 +0800 | [diff] [blame] | 796 | exit2: |
| 797 | talloc_free(parsed); |
| 798 | msgb_free(msg); |
| 799 | return -1; |
| 800 | |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 801 | exit3: |
| 802 | /* send a SCCP Connection Refused */ |
Holger Hans Peter Freyther | 91246d7 | 2010-05-16 02:20:26 +0800 | [diff] [blame] | 803 | bsc_send_con_refuse(bsc, parsed, con_type); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 804 | talloc_free(parsed); |
Holger Hans Peter Freyther | 6f5fbfd | 2010-06-15 18:47:02 +0800 | [diff] [blame] | 805 | msgb_free(msg); |
| 806 | return -1; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 809 | static int ipaccess_bsc_read_cb(struct bsc_fd *bfd) |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 810 | { |
| 811 | int error; |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 812 | struct bsc_connection *bsc = bfd->data; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 813 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 814 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 815 | |
| 816 | if (!msg) { |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 817 | if (error == 0) |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 818 | LOGP(DNAT, LOGL_ERROR, |
| 819 | "The connection to the BSC Nr: %d was lost. Cleaning it\n", |
| 820 | bsc->cfg ? bsc->cfg->nr : -1); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 821 | else |
Holger Hans Peter Freyther | 19c3544 | 2010-05-01 10:37:15 +0800 | [diff] [blame] | 822 | LOGP(DNAT, LOGL_ERROR, |
| 823 | "Stream error on BSC Nr: %d. Failed to parse ip access message: %d\n", |
| 824 | bsc->cfg ? bsc->cfg->nr : -1, error); |
Holger Hans Peter Freyther | 9db7843 | 2010-04-23 00:23:03 +0800 | [diff] [blame] | 825 | |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 826 | bsc_close_connection(bsc); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 827 | return -1; |
| 828 | } |
| 829 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 830 | |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 831 | 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] | 832 | |
| 833 | /* Handle messages from the BSC */ |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 834 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 835 | |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 836 | /* stop the pong timeout */ |
| 837 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 838 | if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 839 | bsc_del_timer(&bsc->pong_timeout); |
| 840 | msgb_free(msg); |
| 841 | return 0; |
Holger Hans Peter Freyther | 5078871 | 2010-06-15 18:51:33 +0800 | [diff] [blame] | 842 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 843 | send_pong(bsc); |
| 844 | msgb_free(msg); |
| 845 | return 0; |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 849 | /* FIXME: Currently no PONG is sent to the BSC */ |
| 850 | /* FIXME: Currently no ID ACK is sent to the BSC */ |
Holger Hans Peter Freyther | 747d654 | 2010-03-26 07:24:34 +0100 | [diff] [blame] | 851 | forward_sccp_to_msc(bsc, msg); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 856 | static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 857 | { |
| 858 | int rc; |
| 859 | |
| 860 | rc = write(bfd->fd, msg->data, msg->len); |
| 861 | if (rc != msg->len) |
| 862 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 863 | |
| 864 | return rc; |
| 865 | } |
| 866 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 867 | static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what) |
| 868 | { |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 869 | struct bsc_connection *bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 870 | int fd, rc, on; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 871 | struct sockaddr_in sa; |
| 872 | socklen_t sa_len = sizeof(sa); |
| 873 | |
| 874 | if (!(what & BSC_FD_READ)) |
| 875 | return 0; |
| 876 | |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 877 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 878 | if (fd < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 879 | perror("accept"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 880 | return fd; |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 883 | /* count the reconnect */ |
| 884 | counter_inc(nat->stats.bsc.reconn); |
| 885 | |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 886 | /* |
| 887 | * if we are not connected to a msc... just close the socket |
| 888 | */ |
Holger Hans Peter Freyther | 20ee312 | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 889 | if (!bsc_nat_msc_is_connected(nat)) { |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 890 | 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] | 891 | close(fd); |
Holger Hans Peter Freyther | cd89537 | 2010-03-29 08:04:09 +0200 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 895 | on = 1; |
Holger Hans Peter Freyther | 569ee12 | 2010-05-05 20:42:14 +0800 | [diff] [blame] | 896 | rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 897 | if (rc != 0) |
Holger Hans Peter Freyther | 9e938c6 | 2010-05-05 18:58:13 +0800 | [diff] [blame] | 898 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 899 | |
Holger Hans Peter Freyther | 078321a | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 900 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | 6b77107 | 2010-07-27 19:21:53 +0800 | [diff] [blame] | 901 | &nat->bsc_ip_dscp, sizeof(nat->bsc_ip_dscp)); |
Holger Hans Peter Freyther | 078321a | 2010-05-31 10:36:35 +0800 | [diff] [blame] | 902 | if (rc != 0) |
| 903 | LOGP(DNAT, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 904 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 905 | /* todo... do something with the connection */ |
Holger Hans Peter Freyther | da86c0a | 2010-01-12 21:35:32 +0100 | [diff] [blame] | 906 | /* 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] | 907 | |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 908 | /* |
| 909 | * |
| 910 | */ |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 911 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 912 | if (!bsc) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 913 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 914 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 915 | return -1; |
| 916 | } |
| 917 | |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 918 | bsc->write_queue.bfd.data = bsc; |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 919 | bsc->write_queue.bfd.fd = fd; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 920 | bsc->write_queue.read_cb = ipaccess_bsc_read_cb; |
Holger Hans Peter Freyther | 3025e19 | 2010-03-26 09:18:02 +0100 | [diff] [blame] | 921 | bsc->write_queue.write_cb = ipaccess_bsc_write_cb; |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 922 | bsc->write_queue.bfd.when = BSC_FD_READ; |
| 923 | if (bsc_register_fd(&bsc->write_queue.bfd) < 0) { |
Holger Hans Peter Freyther | 418f394 | 2010-01-29 05:58:43 +0100 | [diff] [blame] | 924 | LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n"); |
Holger Hans Peter Freyther | 872d768 | 2010-05-05 20:33:34 +0800 | [diff] [blame] | 925 | close(fd); |
Holger Hans Peter Freyther | 24614ad | 2010-01-13 09:28:12 +0100 | [diff] [blame] | 926 | talloc_free(bsc); |
| 927 | return -2; |
| 928 | } |
| 929 | |
Holger Hans Peter Freyther | 74cfab7 | 2010-05-05 17:03:44 +0800 | [diff] [blame] | 930 | 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] | 931 | fd, inet_ntoa(sa.sin_addr)); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 932 | llist_add(&bsc->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | db7ba7d | 2010-03-26 07:41:54 +0100 | [diff] [blame] | 933 | send_id_ack(bsc); |
| 934 | send_id_req(bsc); |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 935 | send_mgcp_reset(bsc); |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 936 | |
| 937 | /* |
| 938 | * start the hangup timer |
| 939 | */ |
| 940 | bsc->id_timeout.data = bsc; |
| 941 | bsc->id_timeout.cb = ipaccess_close_bsc; |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 942 | bsc_schedule_timer(&bsc->id_timeout, nat->auth_timeout, 0); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port) |
| 947 | { |
| 948 | struct sockaddr_in addr; |
| 949 | int ret, on = 1; |
| 950 | |
| 951 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 952 | bfd->cb = ipaccess_listen_bsc_cb; |
| 953 | bfd->when = BSC_FD_READ; |
| 954 | |
| 955 | memset(&addr, 0, sizeof(addr)); |
| 956 | addr.sin_family = AF_INET; |
| 957 | addr.sin_port = htons(port); |
| 958 | addr.sin_addr.s_addr = in_addr->s_addr; |
| 959 | |
| 960 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 961 | |
| 962 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 963 | if (ret < 0) { |
| 964 | fprintf(stderr, "Could not bind the BSC socket %s\n", |
| 965 | strerror(errno)); |
| 966 | return -EIO; |
| 967 | } |
| 968 | |
| 969 | ret = listen(bfd->fd, 1); |
| 970 | if (ret < 0) { |
| 971 | perror("listen"); |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | ret = bsc_register_fd(bfd); |
| 976 | if (ret < 0) { |
| 977 | perror("register_listen_fd"); |
| 978 | return ret; |
| 979 | } |
| 980 | return 0; |
| 981 | } |
| 982 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 983 | static void print_usage() |
| 984 | { |
| 985 | printf("Usage: bsc_nat\n"); |
| 986 | } |
| 987 | |
| 988 | static void print_help() |
| 989 | { |
| 990 | printf(" Some useful help...\n"); |
| 991 | printf(" -h --help this text\n"); |
| 992 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 993 | printf(" -s --disable-color\n"); |
| 994 | printf(" -c --config-file filename The config file to use.\n"); |
| 995 | printf(" -m --msc=IP. The address of the MSC.\n"); |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 996 | 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] | 997 | } |
| 998 | |
| 999 | static void handle_options(int argc, char** argv) |
| 1000 | { |
| 1001 | while (1) { |
| 1002 | int option_index = 0, c; |
| 1003 | static struct option long_options[] = { |
| 1004 | {"help", 0, 0, 'h'}, |
| 1005 | {"debug", 1, 0, 'd'}, |
| 1006 | {"config-file", 1, 0, 'c'}, |
| 1007 | {"disable-color", 0, 0, 's'}, |
| 1008 | {"timestamp", 0, 0, 'T'}, |
| 1009 | {"msc", 1, 0, 'm'}, |
| 1010 | {"local", 1, 0, 'l'}, |
| 1011 | {0, 0, 0, 0} |
| 1012 | }; |
| 1013 | |
| 1014 | c = getopt_long(argc, argv, "hd:sTPc:m:l:", |
| 1015 | long_options, &option_index); |
| 1016 | if (c == -1) |
| 1017 | break; |
| 1018 | |
| 1019 | switch (c) { |
| 1020 | case 'h': |
| 1021 | print_usage(); |
| 1022 | print_help(); |
| 1023 | exit(0); |
| 1024 | case 's': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1025 | log_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1026 | break; |
| 1027 | case 'd': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1028 | log_parse_category_mask(stderr_target, optarg); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1029 | break; |
| 1030 | case 'c': |
| 1031 | config_file = strdup(optarg); |
| 1032 | break; |
| 1033 | case 'T': |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1034 | log_set_print_timestamp(stderr_target, 1); |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1035 | break; |
| 1036 | case 'm': |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1037 | msc_ip = optarg; |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1038 | break; |
| 1039 | case 'l': |
| 1040 | inet_aton(optarg, &local_addr); |
| 1041 | break; |
| 1042 | default: |
| 1043 | /* ignore */ |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1049 | static void signal_handler(int signal) |
| 1050 | { |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1051 | switch (signal) { |
| 1052 | case SIGABRT: |
| 1053 | /* in case of abort, we want to obtain a talloc report |
| 1054 | * and then return to the caller, who will abort the process */ |
| 1055 | case SIGUSR1: |
| 1056 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1057 | break; |
| 1058 | default: |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1063 | static void sccp_close_unconfirmed(void *_data) |
| 1064 | { |
| 1065 | struct sccp_connections *conn, *tmp1; |
| 1066 | struct timespec now; |
| 1067 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 1068 | |
| 1069 | llist_for_each_entry_safe(conn, tmp1, &nat->sccp_connections, list_entry) { |
| 1070 | if (conn->has_remote_ref) |
| 1071 | continue; |
| 1072 | |
| 1073 | int diff = (now.tv_sec - conn->creation_time.tv_sec) / 60; |
| 1074 | if (diff < SCCP_CLOSE_TIME_TIMEOUT) |
| 1075 | continue; |
| 1076 | |
| 1077 | LOGP(DNAT, LOGL_ERROR, "SCCP connection 0x%x/0x%x was never confirmed.\n", |
| 1078 | sccp_src_ref_to_int(&conn->real_ref), |
| 1079 | sccp_src_ref_to_int(&conn->patched_ref)); |
| 1080 | sccp_connection_destroy(conn); |
| 1081 | } |
| 1082 | |
| 1083 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1084 | } |
| 1085 | |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1086 | extern void *tall_msgb_ctx; |
| 1087 | extern void *tall_ctr_ctx; |
| 1088 | static void talloc_init_ctx() |
| 1089 | { |
| 1090 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nat"); |
| 1091 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1092 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 1093 | } |
| 1094 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1095 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 1096 | |
| 1097 | static struct vty_app_info vty_info = { |
| 1098 | .name = "BSC NAT", |
| 1099 | .version = PACKAGE_VERSION, |
| 1100 | .go_parent_cb = bsc_vty_go_parent, |
| 1101 | }; |
| 1102 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1103 | int main(int argc, char** argv) |
| 1104 | { |
Holger Hans Peter Freyther | 0b0b31c | 2010-06-15 18:51:25 +0800 | [diff] [blame] | 1105 | talloc_init_ctx(); |
| 1106 | |
Holger Hans Peter Freyther | a1597f1 | 2010-06-15 18:51:18 +0800 | [diff] [blame] | 1107 | |
Holger Hans Peter Freyther | fb7a934 | 2010-06-15 19:14:12 +0800 | [diff] [blame] | 1108 | log_init(&log_info); |
| 1109 | stderr_target = log_target_create_stderr(); |
| 1110 | log_add_target(stderr_target); |
| 1111 | log_set_all_filter(stderr_target, 1); |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 1112 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1113 | nat = bsc_nat_alloc(); |
| 1114 | if (!nat) { |
| 1115 | fprintf(stderr, "Failed to allocate the BSC nat.\n"); |
| 1116 | return -4; |
| 1117 | } |
| 1118 | |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1119 | nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1120 | if (!nat->mgcp_cfg) { |
| 1121 | fprintf(stderr, "Failed to allocate MGCP cfg.\n"); |
| 1122 | return -5; |
| 1123 | } |
| 1124 | |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1125 | vty_info.copyright = openbsc_copyright; |
| 1126 | vty_init(&vty_info); |
| 1127 | logging_vty_add_cmds(); |
| 1128 | bsc_nat_vty_init(nat); |
| 1129 | |
| 1130 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1131 | /* parse options */ |
| 1132 | local_addr.s_addr = INADDR_ANY; |
| 1133 | handle_options(argc, argv); |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1134 | |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 1135 | rate_ctr_init(tall_bsc_ctx); |
| 1136 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1137 | /* init vty and parse */ |
Holger Hans Peter Freyther | 1398f13 | 2010-06-15 20:14:08 +0800 | [diff] [blame] | 1138 | telnet_init(tall_bsc_ctx, NULL, 4244); |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 1139 | if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) { |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1140 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 1141 | return -3; |
| 1142 | } |
| 1143 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 1144 | /* over rule the VTY config */ |
| 1145 | if (msc_ip) |
| 1146 | bsc_nat_set_msc_ip(nat, msc_ip); |
| 1147 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1148 | /* seed the PRNG */ |
| 1149 | srand(time(NULL)); |
| 1150 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1151 | /* |
| 1152 | * Setup the MGCP code.. |
| 1153 | */ |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1154 | if (bsc_mgcp_nat_init(nat) != 0) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1155 | return -4; |
| 1156 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1157 | /* connect to the MSC */ |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 1158 | 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] | 1159 | if (!nat->msc_con) { |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1160 | fprintf(stderr, "Creating a bsc_msc_connection failed.\n"); |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1161 | exit(1); |
| 1162 | } |
| 1163 | |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1164 | nat->msc_con->connection_loss = msc_connection_was_lost; |
| 1165 | nat->msc_con->write_queue.read_cb = ipaccess_msc_read_cb; |
| 1166 | 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] | 1167 | nat->msc_con->write_queue.bfd.data = nat->msc_con; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 1168 | bsc_msc_connect(nat->msc_con); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 1169 | |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1170 | /* wait for the BSC */ |
Holger Hans Peter Freyther | 2d677c6 | 2010-03-26 06:51:04 +0100 | [diff] [blame] | 1171 | if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) { |
Holger Hans Peter Freyther | 49d8068 | 2010-01-12 21:34:54 +0100 | [diff] [blame] | 1172 | fprintf(stderr, "Failed to listen for BSC.\n"); |
| 1173 | exit(1); |
| 1174 | } |
| 1175 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1176 | signal(SIGABRT, &signal_handler); |
| 1177 | signal(SIGUSR1, &signal_handler); |
| 1178 | signal(SIGPIPE, SIG_IGN); |
| 1179 | |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1180 | /* recycle timer */ |
Holger Hans Peter Freyther | 30e1ae9 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1181 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 6b6ecba | 2010-06-15 18:51:56 +0800 | [diff] [blame] | 1182 | sccp_close.cb = sccp_close_unconfirmed; |
| 1183 | sccp_close.data = NULL; |
| 1184 | bsc_schedule_timer(&sccp_close, SCCP_CLOSE_TIME, 0); |
| 1185 | |
Holger Hans Peter Freyther | 6ace522 | 2010-01-12 21:15:08 +0100 | [diff] [blame] | 1186 | while (1) { |
| 1187 | bsc_select_main(0); |
| 1188 | } |
| 1189 | |
Holger Hans Peter Freyther | 9e2c5f5 | 2010-06-15 18:44:42 +0800 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |