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