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