2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2011 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>
33 #include <openssl/ssl.h>
35 #include "openconnect-internal.h"
37 void queue_packet(struct pkt **q, struct pkt *new)
46 int queue_new_packet(struct pkt **q, void *buf, int len)
48 struct pkt *new = malloc(sizeof(struct pkt) + len);
54 memcpy(new->data, buf, len);
61 static void handle_sigint(int sig)
66 int vpn_mainloop(struct openconnect_info *vpninfo)
69 memset(&sa, 0, sizeof(sa));
70 sa.sa_handler = handle_sigint;
72 sigaction(SIGTERM, &sa, NULL);
73 sigaction(SIGINT, &sa, NULL);
74 sigaction(SIGHUP, &sa, NULL);
76 while (!vpninfo->quit_reason) {
78 int timeout = INT_MAX;
80 fd_set rfds, wfds, efds;
82 #if defined(OPENCONNECT_OPENSSL) && defined(SSL_OP_CISCO_ANYCONNECT)
83 if (vpninfo->new_dtls_ssl)
84 dtls_try_handshake(vpninfo);
86 if (vpninfo->dtls_attempt_period && !vpninfo->dtls_ssl && !vpninfo->new_dtls_ssl &&
87 vpninfo->new_dtls_started + vpninfo->dtls_attempt_period < time(NULL)) {
88 vpn_progress(vpninfo, PRG_TRACE, _("Attempt new DTLS connection\n"));
89 connect_dtls_socket(vpninfo);
91 if (vpninfo->dtls_ssl)
92 did_work += dtls_mainloop(vpninfo, &timeout);
94 if (vpninfo->quit_reason)
97 did_work += cstp_mainloop(vpninfo, &timeout);
98 if (vpninfo->quit_reason)
101 /* Tun must be last because it will set/clear its bit
102 in the select_rfds according to the queue length */
103 did_work += tun_mainloop(vpninfo, &timeout);
104 if (vpninfo->quit_reason)
108 if (killed == SIGHUP)
109 vpninfo->quit_reason = "Client received SIGHUP";
110 else if (killed == SIGINT)
111 vpninfo->quit_reason = "Client received SIGINT";
113 vpninfo->quit_reason = "Client killed";
120 vpn_progress(vpninfo, PRG_TRACE,
121 _("No work to do; sleeping for %d ms...\n"), timeout);
122 memcpy(&rfds, &vpninfo->select_rfds, sizeof(rfds));
123 memcpy(&wfds, &vpninfo->select_wfds, sizeof(wfds));
124 memcpy(&efds, &vpninfo->select_efds, sizeof(efds));
126 tv.tv_sec = timeout / 1000;
127 tv.tv_usec = (timeout % 1000) * 1000;
129 select(vpninfo->select_nfds, &rfds, &wfds, &efds, &tv);
132 cstp_bye(vpninfo, vpninfo->quit_reason);
134 shutdown_tun(vpninfo);
138 /* Called when the socket is unwritable, to get the deadline for DPD.
139 Returns 1 if DPD deadline has already arrived. */
140 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout)
148 due = ka->last_rx + (2 * ka->dpd);
153 if (*timeout > (due - now) * 1000)
154 *timeout = (due - now) * 1000;
160 int keepalive_action(struct keepalive_info *ka, int *timeout)
162 time_t now = time(NULL);
165 time_t due = ka->last_rekey + ka->rekey;
170 if (*timeout > (due - now) * 1000)
171 *timeout = (due - now) * 1000;
174 /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
176 time_t due = ka->last_rx + ka->dpd;
177 time_t overdue = ka->last_rx + (2 * ka->dpd);
179 /* Peer didn't respond */
183 /* If we already have DPD outstanding, don't flood. Repeat by
184 all means, but only after half the DPD period. */
185 if (ka->last_dpd > ka->last_rx)
186 due = ka->last_dpd + ka->dpd / 2;
188 /* We haven't seen a packet from this host for $DPD seconds.
189 Prod it to see if it's still alive */
194 if (*timeout > (due - now) * 1000)
195 *timeout = (due - now) * 1000;
198 /* Keepalive is just client -> server */
200 time_t due = ka->last_tx + ka->keepalive;
202 /* If we haven't sent anything for $KEEPALIVE seconds, send a
203 dummy packet (which the server will discard) */
207 if (*timeout > (due - now) * 1000)
208 *timeout = (due - now) * 1000;