2 * Open AnyConnect (SSL + DTLS) client
4 * © 2008 David Woodhouse <dwmw2@infradead.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to:
19 * Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA
27 #include <sys/select.h>
29 #include <arpa/inet.h>
32 #include "openconnect.h"
34 void queue_packet(struct pkt **q, struct pkt *new)
43 int queue_new_packet(struct pkt **q, int type, void *buf, int len)
45 struct pkt *new = malloc(sizeof(struct pkt) + len);
52 memcpy(new->data, buf, len);
59 static void handle_sigint(int sig)
64 int vpn_mainloop(struct openconnect_info *vpninfo)
67 memset(&sa, 0, sizeof(sa));
68 sa.sa_handler = handle_sigint;
70 sigaction(SIGINT, &sa, NULL);
71 sigaction(SIGHUP, &sa, NULL);
73 while (!vpninfo->quit_reason) {
75 int timeout = INT_MAX;
77 fd_set rfds, wfds, efds;
79 if (vpninfo->new_dtls_ssl)
80 dtls_try_handshake(vpninfo);
82 if (vpninfo->dtls_attempt_period && !vpninfo->dtls_ssl && !vpninfo->new_dtls_ssl &&
83 vpninfo->new_dtls_started + vpninfo->dtls_attempt_period < time(NULL)) {
84 vpninfo->progress(vpninfo, PRG_TRACE, "Attempt new DTLS connection\n");
85 connect_dtls_socket(vpninfo);
87 if (vpninfo->dtls_ssl)
88 did_work += dtls_mainloop(vpninfo, &timeout);
90 if (vpninfo->quit_reason)
93 did_work += cstp_mainloop(vpninfo, &timeout);
94 if (vpninfo->quit_reason)
97 /* Tun must be last because it will set/clear its bit
98 in the select_rfds according to the queue length */
99 did_work += tun_mainloop(vpninfo, &timeout);
100 if (vpninfo->quit_reason)
104 if (killed == SIGHUP)
105 vpninfo->quit_reason = "Client received SIGHUP";
106 else if (killed == SIGINT)
107 vpninfo->quit_reason = "Client received SIGINT";
109 vpninfo->quit_reason = "Client killed";
116 vpninfo->progress(vpninfo, PRG_TRACE,
117 "Did no work; sleeping for %d ms...\n", timeout);
118 memcpy(&rfds, &vpninfo->select_rfds, sizeof(rfds));
119 memcpy(&wfds, &vpninfo->select_wfds, sizeof(wfds));
120 memcpy(&efds, &vpninfo->select_efds, sizeof(efds));
122 tv.tv_sec = timeout / 1000;
123 tv.tv_usec = (timeout * 1000) % 1000000;
124 if (select(vpninfo->select_nfds, &rfds, &wfds, &efds, &tv) >= 0
125 && FD_ISSET(vpninfo->ssl_fd, &efds)) {
126 /* OpenSSL doesn't seem to cope properly with this... */
127 vpninfo->progress(vpninfo, PRG_ERR, "Server closed connection!\n");
132 cstp_bye(vpninfo, vpninfo->quit_reason);
134 if (vpninfo->vpnc_script) {
135 if (vpninfo->script_tun) {
136 kill(vpninfo->script_tun, SIGHUP);
137 close(vpninfo->tun_fd);
139 setenv("TUNDEV", vpninfo->ifname, 1);
140 setenv("reason", "disconnect", 1);
141 system(vpninfo->vpnc_script);
148 /* Called when the socket is unwritable, to get the deadline for DPD.
149 Returns 1 if DPD deadline has already arrived. */
150 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout)
158 due = ka->last_rx + (2 * ka->dpd);
163 if (*timeout > (due - now) * 1000)
164 *timeout = (due - now) * 1000;
170 int keepalive_action(struct keepalive_info *ka, int *timeout)
172 time_t now = time(NULL);
175 time_t due = ka->last_rekey + ka->rekey;
180 if (*timeout > (due - now) * 1000)
181 *timeout = (due - now) * 1000;
184 /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
186 time_t due = ka->last_rx + ka->dpd;
187 time_t overdue = ka->last_rx + (2 * ka->dpd);
189 /* Peer didn't respond */
193 /* If we already have DPD outstanding, don't flood. Repeat by
194 all means, but only after half the DPD period. */
195 if (ka->last_dpd > ka->last_rx)
196 due = ka->last_dpd + ka->dpd / 2;
198 /* We haven't seen a packet from this host for $DPD seconds.
199 Prod it to see if it's still alive */
204 if (*timeout > (due - now) * 1000)
205 *timeout = (due - now) * 1000;
208 /* Keepalive is just client -> server */
210 time_t due = ka->last_tx + ka->keepalive;
212 /* If we haven't sent anything for $KEEPALIVE seconds, send a
213 dummy packet (which the server will discard) */
217 if (*timeout > (due - now) * 1000)
218 *timeout = (due - now) * 1000;