Remove all _xxx_SOURCE macros from source, do it in configure.
[platform/upstream/openconnect.git] / mainloop.c
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008-2010 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 <openssl/ssl.h>
35
36 #include "openconnect-internal.h"
37
38 void queue_packet(struct pkt **q, struct pkt *new)
39 {
40         while (*q)
41                 q = &(*q)->next;
42
43         new->next = NULL;
44         *q = new;
45 }
46
47 int queue_new_packet(struct pkt **q, void *buf, int len)
48 {
49         struct pkt *new = malloc(sizeof(struct pkt) + len);
50         if (!new)
51                 return -ENOMEM;
52
53         new->len = len;
54         new->next = NULL;
55         memcpy(new->data, buf, len);
56         queue_packet(q, new);
57         return 0;
58 }
59
60 int killed;
61
62 static void handle_sigint(int sig)
63 {
64         killed = sig;
65 }
66
67 int vpn_mainloop(struct openconnect_info *vpninfo)
68 {
69         struct sigaction sa;
70         memset(&sa, 0, sizeof(sa));
71         sa.sa_handler = handle_sigint;
72
73         sigaction(SIGTERM, &sa, NULL);
74         sigaction(SIGINT, &sa, NULL);
75         sigaction(SIGHUP, &sa, NULL);
76
77         while (!vpninfo->quit_reason) {
78                 int did_work = 0;
79                 int timeout = INT_MAX;
80                 struct timeval tv;
81                 fd_set rfds, wfds, efds;
82
83 #ifdef SSL_OP_CISCO_ANYCONNECT
84                 if (vpninfo->new_dtls_ssl)
85                         dtls_try_handshake(vpninfo);
86
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                         vpn_progress(vpninfo, PRG_TRACE, _("Attempt new DTLS connection\n"));
90                         connect_dtls_socket(vpninfo);
91                 }
92                 if (vpninfo->dtls_ssl)
93                         did_work += dtls_mainloop(vpninfo, &timeout);
94 #endif
95                 if (vpninfo->quit_reason)
96                         break;
97
98                 did_work += cstp_mainloop(vpninfo, &timeout);
99                 if (vpninfo->quit_reason)
100                         break;
101
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)
106                         break;
107
108                 if (killed) {
109                         if (killed == SIGHUP)
110                                 vpninfo->quit_reason = "Client received SIGHUP";
111                         else if (killed == SIGINT)
112                                 vpninfo->quit_reason = "Client received SIGINT";
113                         else
114                                 vpninfo->quit_reason = "Client killed";
115                         break;
116                 }
117
118                 if (did_work)
119                         continue;
120
121                 vpn_progress(vpninfo, PRG_TRACE,
122                              _("No work to do; 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));
126
127                 tv.tv_sec = timeout / 1000;
128                 tv.tv_usec = (timeout % 1000) * 1000;
129
130                 select(vpninfo->select_nfds, &rfds, &wfds, &efds, &tv);
131         }
132
133         cstp_bye(vpninfo, vpninfo->quit_reason);
134
135         shutdown_tun(vpninfo);
136         return 0;
137 }
138
139 /* Called when the socket is unwritable, to get the deadline for DPD.
140    Returns 1 if DPD deadline has already arrived. */
141 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout)
142 {
143         time_t now, due;
144
145         if (!ka->dpd)
146                 return 0;
147
148         time(&now);
149         due = ka->last_rx + (2 * ka->dpd);
150
151         if (now > due)
152                 return 1;
153
154         if (*timeout > (due - now) * 1000)
155                 *timeout = (due - now) * 1000;
156
157         return 0;
158 }
159
160
161 int keepalive_action(struct keepalive_info *ka, int *timeout)
162 {
163         time_t now = time(NULL);
164
165         if (ka->rekey) {
166                 time_t due = ka->last_rekey + ka->rekey;
167
168                 if (now >= due)
169                         return KA_REKEY;
170
171                 if (*timeout > (due - now) * 1000)
172                         *timeout = (due - now) * 1000;
173         }
174
175         /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
176         if (ka->dpd) {
177                 time_t due = ka->last_rx + ka->dpd;
178                 time_t overdue = ka->last_rx + (2 * ka->dpd);
179
180                 /* Peer didn't respond */
181                 if (now > overdue)
182                         return KA_DPD_DEAD;
183
184                 /* If we already have DPD outstanding, don't flood. Repeat by
185                    all means, but only after half the DPD period. */
186                 if (ka->last_dpd > ka->last_rx)
187                         due = ka->last_dpd + ka->dpd / 2;
188
189                 /* We haven't seen a packet from this host for $DPD seconds.
190                    Prod it to see if it's still alive */
191                 if (now >= due) {
192                         ka->last_dpd = now;
193                         return KA_DPD;
194                 }
195                 if (*timeout > (due - now) * 1000)
196                         *timeout = (due - now) * 1000;
197         }
198
199         /* Keepalive is just client -> server */
200         if (ka->keepalive) {
201                 time_t due = ka->last_tx + ka->keepalive;
202
203                 /* If we haven't sent anything for $KEEPALIVE seconds, send a
204                    dummy packet (which the server will discard) */
205                 if (now >= due)
206                         return KA_KEEPALIVE;
207
208                 if (*timeout > (due - now) * 1000)
209                         *timeout = (due - now) * 1000;
210         }
211
212         return KA_NONE;
213 }