2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008 Intel Corporation.
6 * Author: David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
28 #include <sys/select.h>
30 #include <arpa/inet.h>
34 #include <openssl/ssl.h>
36 #include "openconnect.h"
38 void queue_packet(struct pkt **q, struct pkt *new)
47 int queue_new_packet(struct pkt **q, int type, void *buf, int len)
49 struct pkt *new = malloc(sizeof(struct pkt) + len);
56 memcpy(new->data, buf, len);
63 static void handle_sigint(int sig)
68 int vpn_mainloop(struct openconnect_info *vpninfo)
71 memset(&sa, 0, sizeof(sa));
72 sa.sa_handler = handle_sigint;
74 sigaction(SIGINT, &sa, NULL);
75 sigaction(SIGHUP, &sa, NULL);
77 while (!vpninfo->quit_reason) {
79 int timeout = INT_MAX;
81 fd_set rfds, wfds, efds;
83 #ifdef SSL_F_DTLS1_CONNECT
84 if (vpninfo->new_dtls_ssl)
85 dtls_try_handshake(vpninfo);
87 if (vpninfo->dtls_attempt_period && !vpninfo->dtls_ssl && !vpninfo->new_dtls_ssl &&
88 vpninfo->new_dtls_started + vpninfo->dtls_attempt_period < time(NULL)) {
89 vpninfo->progress(vpninfo, PRG_TRACE, "Attempt new DTLS connection\n");
90 connect_dtls_socket(vpninfo);
92 if (vpninfo->dtls_ssl)
93 did_work += dtls_mainloop(vpninfo, &timeout);
95 if (vpninfo->quit_reason)
98 did_work += cstp_mainloop(vpninfo, &timeout);
99 if (vpninfo->quit_reason)
102 /* Tun must be last because it will set/clear its bit
103 in the select_rfds according to the queue length */
104 did_work += tun_mainloop(vpninfo, &timeout);
105 if (vpninfo->quit_reason)
109 if (killed == SIGHUP)
110 vpninfo->quit_reason = "Client received SIGHUP";
111 else if (killed == SIGINT)
112 vpninfo->quit_reason = "Client received SIGINT";
114 vpninfo->quit_reason = "Client killed";
121 vpninfo->progress(vpninfo, PRG_TRACE,
122 "Did no work; sleeping for %d ms...\n", timeout);
123 memcpy(&rfds, &vpninfo->select_rfds, sizeof(rfds));
124 memcpy(&wfds, &vpninfo->select_wfds, sizeof(wfds));
125 memcpy(&efds, &vpninfo->select_efds, sizeof(efds));
127 tv.tv_sec = timeout / 1000;
128 tv.tv_usec = (timeout % 1000) * 1000;
129 if (select(vpninfo->select_nfds, &rfds, &wfds, &efds, &tv) >= 0
130 && FD_ISSET(vpninfo->ssl_fd, &rfds)) {
132 if (recv(vpninfo->ssl_fd, &b, 1, MSG_PEEK) == 0) {
133 /* Zero indicates EOF, which OpenSSL won't handle on read() */
134 vpninfo->progress(vpninfo, PRG_ERR, "Server closed connection!\n");
140 cstp_bye(vpninfo, vpninfo->quit_reason);
142 if (vpninfo->vpnc_script) {
143 if (vpninfo->script_tun) {
144 kill(vpninfo->script_tun, SIGHUP);
145 close(vpninfo->tun_fd);
147 setenv("TUNDEV", vpninfo->ifname, 1);
148 setenv("reason", "disconnect", 1);
149 system(vpninfo->vpnc_script);
156 /* Called when the socket is unwritable, to get the deadline for DPD.
157 Returns 1 if DPD deadline has already arrived. */
158 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout)
166 due = ka->last_rx + (2 * ka->dpd);
171 if (*timeout > (due - now) * 1000)
172 *timeout = (due - now) * 1000;
178 int keepalive_action(struct keepalive_info *ka, int *timeout)
180 time_t now = time(NULL);
183 time_t due = ka->last_rekey + ka->rekey;
188 if (*timeout > (due - now) * 1000)
189 *timeout = (due - now) * 1000;
192 /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
194 time_t due = ka->last_rx + ka->dpd;
195 time_t overdue = ka->last_rx + (2 * ka->dpd);
197 /* Peer didn't respond */
201 /* If we already have DPD outstanding, don't flood. Repeat by
202 all means, but only after half the DPD period. */
203 if (ka->last_dpd > ka->last_rx)
204 due = ka->last_dpd + ka->dpd / 2;
206 /* We haven't seen a packet from this host for $DPD seconds.
207 Prod it to see if it's still alive */
212 if (*timeout > (due - now) * 1000)
213 *timeout = (due - now) * 1000;
216 /* Keepalive is just client -> server */
218 time_t due = ka->last_tx + ka->keepalive;
220 /* If we haven't sent anything for $KEEPALIVE seconds, send a
221 dummy packet (which the server will discard) */
225 if (*timeout > (due - now) * 1000)
226 *timeout = (due - now) * 1000;