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