Detect TCP connection closure; OpenSSL doesn't.
[platform/upstream/openconnect.git] / mainloop.c
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008 Intel Corporation.
5  *
6  * Author: David Woodhouse <dwmw2@infradead.org>
7  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to:
19  *
20  *   Free Software Foundation, Inc.
21  *   51 Franklin Street, Fifth Floor,
22  *   Boston, MA 02110-1301 USA
23  */
24
25 #include <errno.h>
26 #include <poll.h>
27 #include <limits.h>
28 #include <sys/select.h>
29 #include <signal.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32
33 #include "openconnect.h"
34
35 void queue_packet(struct pkt **q, struct pkt *new)
36 {
37         while (*q)
38                 q = &(*q)->next;
39
40         new->next = NULL;
41         *q = new;
42 }
43
44 int queue_new_packet(struct pkt **q, int type, void *buf, int len)
45 {
46         struct pkt *new = malloc(sizeof(struct pkt) + len);
47         if (!new)
48                 return -ENOMEM;
49
50         new->type = type;
51         new->len = len;
52         new->next = NULL;
53         memcpy(new->data, buf, len);
54         queue_packet(q, new);
55         return 0;
56 }
57
58 int killed;
59
60 static void handle_sigint(int sig)
61 {
62         killed = sig;
63 }
64
65 int vpn_mainloop(struct openconnect_info *vpninfo)
66 {
67         struct sigaction sa;
68         memset(&sa, 0, sizeof(sa));
69         sa.sa_handler = handle_sigint;
70
71         sigaction(SIGINT, &sa, NULL);
72         sigaction(SIGHUP, &sa, NULL);
73
74         while (!vpninfo->quit_reason) {
75                 int did_work = 0;
76                 int timeout = INT_MAX;
77                 struct timeval tv;
78                 fd_set rfds, wfds, efds;
79
80                 if (vpninfo->new_dtls_ssl)
81                         dtls_try_handshake(vpninfo);
82
83                 if (vpninfo->dtls_attempt_period && !vpninfo->dtls_ssl && !vpninfo->new_dtls_ssl &&
84                     vpninfo->new_dtls_started + vpninfo->dtls_attempt_period < time(NULL)) {
85                         vpninfo->progress(vpninfo, PRG_TRACE, "Attempt new DTLS connection\n");
86                         connect_dtls_socket(vpninfo);
87                 }
88                 if (vpninfo->dtls_ssl)
89                         did_work += dtls_mainloop(vpninfo, &timeout);
90
91                 if (vpninfo->quit_reason)
92                         break;
93
94                 did_work += cstp_mainloop(vpninfo, &timeout);
95                 if (vpninfo->quit_reason)
96                         break;
97
98                 /* Tun must be last because it will set/clear its bit
99                    in the select_rfds according to the queue length */
100                 did_work += tun_mainloop(vpninfo, &timeout);
101                 if (vpninfo->quit_reason)
102                         break;
103
104                 if (killed) {
105                         if (killed == SIGHUP)
106                                 vpninfo->quit_reason = "Client received SIGHUP";
107                         else if (killed == SIGINT)
108                                 vpninfo->quit_reason = "Client received SIGINT";
109                         else
110                                 vpninfo->quit_reason = "Client killed";
111                         break;
112                 }
113
114                 if (did_work)
115                         continue;
116
117                 vpninfo->progress(vpninfo, PRG_TRACE,
118                                   "Did no work; sleeping for %d ms...\n", timeout);
119                 memcpy(&rfds, &vpninfo->select_rfds, sizeof(rfds));
120                 memcpy(&wfds, &vpninfo->select_wfds, sizeof(wfds));
121                 memcpy(&efds, &vpninfo->select_efds, sizeof(efds));
122
123                 tv.tv_sec = timeout / 1000;
124                 tv.tv_usec = (timeout % 1000) * 1000;
125                 if (select(vpninfo->select_nfds, &rfds, &wfds, &efds, &tv) >= 0
126                     && FD_ISSET(vpninfo->ssl_fd, &rfds)) {
127                         char b;
128                         if (recv(vpninfo->ssl_fd, &b, 1, MSG_PEEK) == 0) {
129                                 /* Zero indicates EOF, which OpenSSL won't handle on read() */
130                                 vpninfo->progress(vpninfo, PRG_ERR, "Server closed connection!\n");
131                                 exit(1);
132                         }
133                 }
134         }
135
136         cstp_bye(vpninfo, vpninfo->quit_reason);
137
138         if (vpninfo->vpnc_script) {
139                 if (vpninfo->script_tun) {
140                         kill(vpninfo->script_tun, SIGHUP);
141                         close(vpninfo->tun_fd);
142                 } else {
143                         setenv("TUNDEV", vpninfo->ifname, 1);
144                         setenv("reason", "disconnect", 1);
145                         system(vpninfo->vpnc_script);
146                 }
147         }
148
149         return 0;
150 }
151
152 /* Called when the socket is unwritable, to get the deadline for DPD.
153    Returns 1 if DPD deadline has already arrived. */
154 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout)
155 {
156         time_t now, due;
157
158         if (!ka->dpd)
159                 return 0;
160
161         time(&now);
162         due = ka->last_rx + (2 * ka->dpd);
163
164         if (now > due)
165                 return 1;
166
167         if (*timeout > (due - now) * 1000)
168                 *timeout = (due - now) * 1000;
169
170         return 0;
171 }
172
173
174 int keepalive_action(struct keepalive_info *ka, int *timeout)
175 {
176         time_t now = time(NULL);
177
178         if (ka->rekey) {
179                 time_t due = ka->last_rekey + ka->rekey;
180
181                 if (now >= due)
182                         return KA_REKEY;
183
184                 if (*timeout > (due - now) * 1000)
185                         *timeout = (due - now) * 1000;
186         }
187
188         /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
189         if (ka->dpd) {
190                 time_t due = ka->last_rx + ka->dpd;
191                 time_t overdue = ka->last_rx + (2 * ka->dpd);
192
193                 /* Peer didn't respond */
194                 if (now > overdue)
195                         return KA_DPD_DEAD;
196
197                 /* If we already have DPD outstanding, don't flood. Repeat by
198                    all means, but only after half the DPD period. */
199                 if (ka->last_dpd > ka->last_rx)
200                         due = ka->last_dpd + ka->dpd / 2;
201
202                 /* We haven't seen a packet from this host for $DPD seconds.
203                    Prod it to see if it's still alive */
204                 if (now >= due) {
205                         ka->last_dpd = now;
206                         return KA_DPD;
207                 }
208                 if (*timeout > (due - now) * 1000)
209                         *timeout = (due - now) * 1000;
210         }
211
212         /* Keepalive is just client -> server */
213         if (ka->keepalive) {
214                 time_t due = ka->last_tx + ka->keepalive;
215
216                 /* If we haven't sent anything for $KEEPALIVE seconds, send a
217                    dummy packet (which the server will discard) */
218                 if (now >= due)
219                         return KA_KEEPALIVE;
220
221                 if (*timeout > (due - now) * 1000)
222                         *timeout = (due - now) * 1000;
223         }
224
225         return KA_NONE;
226 }