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