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