Invoke vpnc-script with "pre-init" to load tun module if necessary
[platform/upstream/openconnect.git] / openconnect-internal.h
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008-2011 Intel Corporation.
5  * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
6  *
7  * Author: David Woodhouse <dwmw2@infradead.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to:
20  *
21  *   Free Software Foundation, Inc.
22  *   51 Franklin Street, Fifth Floor,
23  *   Boston, MA 02110-1301 USA
24  */
25
26 #ifndef __OPENCONNECT_INTERNAL_H__
27 #define __OPENCONNECT_INTERNAL_H__
28
29 #include "openconnect.h"
30
31 #include <openssl/ssl.h>
32 #include <zlib.h>
33 #include <stdint.h>
34 #include <sys/socket.h>
35 #include <sys/select.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #ifdef LIBPROXY_HDR
40 #include LIBPROXY_HDR
41 #endif
42 #ifdef ENABLE_NLS
43 #include <locale.h>
44 #include <libintl.h>
45 #define _(s) dgettext("openconnect", s)
46 #else
47 #define _(s) s
48 #endif
49 #define N_(s) s
50
51 /****************************************************************************/
52
53 struct pkt {
54         int len;
55         struct pkt *next;
56         unsigned char hdr[8];
57         unsigned char data[];
58 };
59
60 struct vpn_option {
61         char *option;
62         char *value;
63         struct vpn_option *next;
64 };
65
66 #define KA_NONE         0
67 #define KA_DPD          1
68 #define KA_DPD_DEAD     2
69 #define KA_KEEPALIVE    3
70 #define KA_REKEY        4
71
72 struct keepalive_info {
73         int dpd;
74         int keepalive;
75         int rekey;
76         time_t last_rekey;
77         time_t last_tx;
78         time_t last_rx;
79         time_t last_dpd;
80 };
81
82 struct split_include {
83         char *route;
84         struct split_include *next;
85 };
86
87 #define RECONNECT_INTERVAL_MIN  10
88 #define RECONNECT_INTERVAL_MAX  100
89
90 #define CERT_TYPE_UNKNOWN       0
91 #define CERT_TYPE_PEM           1
92 #define CERT_TYPE_PKCS12        2
93 #define CERT_TYPE_TPM           3
94
95 struct openconnect_info {
96         char *redirect_url;
97
98         char *csd_token;
99         char *csd_ticket;
100         char *csd_stuburl;
101         char *csd_starturl;
102         char *csd_waiturl;
103         char *csd_preurl;
104
105         char *csd_scriptname;
106
107 #ifdef LIBPROXY_HDR
108         pxProxyFactory *proxy_factory;
109 #endif
110         char *proxy_type;
111         char *proxy;
112         int proxy_port;
113
114         const char *localname;
115         char *hostname;
116         int port;
117         char *urlpath;
118         int cert_expire_warning;
119         const char *cert;
120         const char *sslkey;
121         X509 *cert_x509;
122         int cert_type;
123         char *cert_password;
124         const char *cafile;
125         const char *servercert;
126         const char *xmlconfig;
127         char xmlsha1[(SHA_DIGEST_LENGTH * 2) + 1];
128         char *username;
129         char *password;
130         char *authgroup;
131         int nopasswd;
132         char *dtls_ciphers;
133         uid_t uid_csd;
134         char *csd_wrapper;
135         int uid_csd_given;
136         int no_http_keepalive;
137
138         char *cookie; /* Pointer to within cookies list */
139         struct vpn_option *cookies;
140         struct vpn_option *cstp_options;
141         struct vpn_option *dtls_options;
142
143         SSL_CTX *https_ctx;
144         SSL *https_ssl;
145         struct keepalive_info ssl_times;
146         int owe_ssl_dpd_response;
147         struct pkt *deflate_pkt;
148         struct pkt *current_ssl_pkt;
149         struct pkt *pending_deflated_pkt;
150
151         z_stream inflate_strm;
152         uint32_t inflate_adler32;
153         z_stream deflate_strm;
154         uint32_t deflate_adler32;
155
156         int disable_ipv6;
157         int reconnect_timeout;
158         int reconnect_interval;
159         int dtls_attempt_period;
160         time_t new_dtls_started;
161         SSL_CTX *dtls_ctx;
162         SSL *dtls_ssl;
163         SSL *new_dtls_ssl;
164         SSL_SESSION *dtls_session;
165         struct keepalive_info dtls_times;
166         unsigned char dtls_session_id[32];
167         unsigned char dtls_secret[48];
168
169         char *dtls_cipher;
170         char *vpnc_script;
171         int script_tun;
172         char *ifname;
173
174         int mtu;
175         const char *banner;
176         const char *vpn_addr;
177         const char *vpn_netmask;
178         const char *vpn_addr6;
179         const char *vpn_netmask6;
180         const char *vpn_dns[3];
181         const char *vpn_nbns[3];
182         const char *vpn_domain;
183         const char *vpn_proxy_pac;
184         struct split_include *split_includes;
185         struct split_include *split_excludes;
186
187         int select_nfds;
188         fd_set select_rfds;
189         fd_set select_wfds;
190         fd_set select_efds;
191
192 #ifdef __sun__
193         int ip_fd;
194         int ip6_fd;
195 #endif
196         int tun_fd;
197         int ssl_fd;
198         int dtls_fd;
199         int new_dtls_fd;
200
201         struct pkt *incoming_queue;
202         struct pkt *outgoing_queue;
203         int outgoing_qlen;
204         int max_qlen;
205
206         socklen_t peer_addrlen;
207         struct sockaddr *peer_addr;
208         struct sockaddr *dtls_addr;
209
210         int deflate;
211         char *useragent;
212
213         const char *quit_reason;
214
215         void *cbdata;
216         openconnect_validate_peer_cert_vfn validate_peer_cert;
217         openconnect_write_new_config_vfn write_new_config;
218         openconnect_process_auth_form_vfn process_auth_form;
219         openconnect_progress_vfn progress;
220 };
221
222 /* Packet types */
223
224 #define AC_PKT_DATA             0       /* Uncompressed data */
225 #define AC_PKT_DPD_OUT          3       /* Dead Peer Detection */
226 #define AC_PKT_DPD_RESP         4       /* DPD response */
227 #define AC_PKT_DISCONN          5       /* Client disconnection notice */
228 #define AC_PKT_KEEPALIVE        7       /* Keepalive */
229 #define AC_PKT_COMPRESSED       8       /* Compressed data */
230 #define AC_PKT_TERM_SERVER      9       /* Server kick */
231
232 /* Ick */
233 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
234 #define method_const const
235 #else
236 #define method_const
237 #endif
238
239 #define vpn_progress(vpninfo, ...) (vpninfo)->progress ((vpninfo)->cbdata, __VA_ARGS__)
240
241 /****************************************************************************/
242
243 /* tun.c */
244 int setup_tun(struct openconnect_info *vpninfo);
245 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout);
246 void shutdown_tun(struct openconnect_info *vpninfo);
247 int script_config_tun (struct openconnect_info *vpninfo, const char *reason);
248
249 /* dtls.c */
250 unsigned char unhex(const char *data);
251 int setup_dtls(struct openconnect_info *vpninfo);
252 int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout);
253 int dtls_try_handshake(struct openconnect_info *vpninfo);
254 int connect_dtls_socket(struct openconnect_info *vpninfo);
255
256 /* cstp.c */
257 int make_cstp_connection(struct openconnect_info *vpninfo);
258 int cstp_mainloop(struct openconnect_info *vpninfo, int *timeout);
259 int cstp_bye(struct openconnect_info *vpninfo, const char *reason);
260 int cstp_reconnect(struct openconnect_info *vpninfo);
261
262 /* ssl.c */
263 int  __attribute__ ((format (printf, 2, 3)))
264                 openconnect_SSL_printf(SSL *ssl, const char *fmt, ...);
265 int openconnect_SSL_gets(SSL *ssl, char *buf, size_t len);
266 int openconnect_open_https(struct openconnect_info *vpninfo);
267 void openconnect_close_https(struct openconnect_info *vpninfo);
268 int get_cert_md5_fingerprint(struct openconnect_info *vpninfo, X509 *cert,
269                              char *buf);
270 void report_ssl_errors(struct openconnect_info *vpninfo);
271
272 /* mainloop.c */
273 int vpn_add_pollfd(struct openconnect_info *vpninfo, int fd, short events);
274 int vpn_mainloop(struct openconnect_info *vpninfo);
275 int queue_new_packet(struct pkt **q, void *buf, int len);
276 void queue_packet(struct pkt **q, struct pkt *new);
277 int keepalive_action(struct keepalive_info *ka, int *timeout);
278 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout);
279
280 extern int killed;
281
282 /* xml.c */
283 int config_lookup_host(struct openconnect_info *vpninfo, const char *host);
284
285 /* auth.c */
286 int parse_xml_response(struct openconnect_info *vpninfo, char *response,
287                        char *request_body, int req_len, const char **method,
288                        const char **request_body_type);
289
290 /* http.c */
291 char *openconnect_create_useragent(const char *base);
292 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock);
293 int internal_parse_url(char *url, char **res_proto, char **res_host,
294                        int *res_port, char **res_path, int default_port);
295
296 /* ssl_ui.c */
297 int set_openssl_ui(void);
298
299 /* securid.c */
300 int generate_securid_tokencodes(struct openconnect_info *vpninfo);
301 int add_securid_pin(char *token, char *pin);
302
303 /* version.c */
304 extern char openconnect_version[];
305
306 #endif /* __OPENCONNECT_INTERNAL_H__ */