1 /* timer event handling
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * RCSID $Id: timer.c,v 1.5 2004/09/17 21:36:57 as Exp $
20 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/queue.h>
29 #include "constants.h"
31 #include "connections.h"
34 #include "ipsec_doi.h" /* needs demux.h and state.h */
43 #include "nat_traversal.h"
46 /* monotonic version of time(3) */
50 static time_t delta
= 0
52 time_t n
= time((time_t)NULL
);
54 passert(n
!= (time_t)-1);
57 plog("time moved backwards %ld seconds", (long)(last_time
- n
));
58 delta
+= last_time
- n
;
64 /* This file has the event handling routines. Events are
65 * kept as a linked list of event structures. These structures
66 * have information like event type, expiration time and a pointer
67 * to event specific data (for example, to a state structure).
70 static struct event
*evlist
= (struct event
*) NULL
;
73 * This routine places an event in the event list.
76 event_schedule(enum event_type type
, time_t tm
, struct state
*st
)
78 struct event
*ev
= alloc_thing(struct event
, "struct event in event_schedule()");
81 ev
->ev_time
= tm
+ now();
84 /* If the event is associated with a state, put a backpointer to the
85 * event in the state object, so we can find and delete the event
86 * if we need to (for example, if we receive a reply).
90 if (type
== EVENT_DPD
|| type
== EVENT_DPD_TIMEOUT
)
92 passert(st
->st_dpd_event
== NULL
);
93 st
->st_dpd_event
= ev
;
97 passert(st
->st_event
== NULL
);
104 DBG_log("inserting event %s, timeout in %lu seconds"
105 , enum_show(&timer_event_names
, type
), (unsigned long)tm
);
107 DBG_log("inserting event %s, timeout in %lu seconds for #%lu"
108 , enum_show(&timer_event_names
, type
), (unsigned long)tm
109 , ev
->ev_state
->st_serialno
));
111 if (evlist
== (struct event
*) NULL
112 || evlist
->ev_time
>= ev
->ev_time
)
114 ev
->ev_next
= evlist
;
121 for (evt
= evlist
; evt
->ev_next
!= NULL
; evt
= evt
->ev_next
)
122 if (evt
->ev_next
->ev_time
>= ev
->ev_time
)
125 #ifdef NEVER /* this seems to be overkill */
127 if (evt
->ev_state
== NULL
)
128 DBG_log("event added after event %s"
129 , enum_show(&timer_event_names
, evt
->ev_type
));
131 DBG_log("event added after event %s for #%lu"
132 , enum_show(&timer_event_names
, evt
->ev_type
)
133 , evt
->ev_state
->st_serialno
));
136 ev
->ev_next
= evt
->ev_next
;
142 * Handle the first event on the list.
145 handle_timer_event(void)
148 struct event
*ev
= evlist
;
151 struct connection
*c
= NULL
;
154 if (ev
== (struct event
*) NULL
) /* Just paranoid */
156 DBG(DBG_CONTROL
, DBG_log("empty event list, yet we're called"));
165 if (tm
< ev
->ev_time
)
167 DBG(DBG_CONTROL
, DBG_log("called while no event expired (%lu/%lu, %s)"
168 , (unsigned long)tm
, (unsigned long)ev
->ev_time
169 , enum_show(&timer_event_names
, type
)));
171 /* This will happen if the most close-to-expire event was
172 * a retransmission or cleanup, and we received a packet
173 * at the same time as the event expired. Due to the processing
174 * order in call_server(), the packet processing will happen first,
175 * and the event will be removed.
180 evlist
= evlist
->ev_next
; /* Ok, we'll handle this event */
183 if (evlist
!= (struct event
*) NULL
)
184 DBG_log("event after this is %s in %ld seconds"
185 , enum_show(&timer_event_names
, evlist
->ev_type
)
186 , (long) (evlist
->ev_time
- tm
)));
188 /* for state-associated events, pick up the state pointer
189 * and remove the backpointer from the state object.
190 * We'll eventually either schedule a new event, or delete the state.
192 passert(GLOBALS_ARE_RESET());
195 c
= st
->st_connection
;
196 if (type
== EVENT_DPD
|| type
== EVENT_DPD_TIMEOUT
)
198 passert(st
->st_dpd_event
== ev
);
199 st
->st_dpd_event
= NULL
;
203 passert(st
->st_event
== ev
);
206 peer
= c
->spd
.that
.host_addr
;
212 case EVENT_REINIT_SECRET
:
214 DBG(DBG_CONTROL
, DBG_log("event EVENT_REINIT_SECRET handled"));
219 case EVENT_SHUNT_SCAN
:
225 case EVENT_LOG_DAILY
:
229 case EVENT_RETRANSMIT
:
230 /* Time to retransmit, or give up.
232 * Generally, we'll only try to send the message
233 * MAXIMUM_RETRANSMISSIONS times. Each time we double
236 * As a special case, if this is the first initiating message
237 * of a Main Mode exchange, and we have been directed to try
238 * forever, we'll extend the number of retransmissions to
239 * MAXIMUM_RETRANSMISSIONS_INITIAL times, with all these
240 * extended attempts having the same patience. The intention
241 * is to reduce the bother when nobody is home.
246 DBG(DBG_CONTROL
, DBG_log(
247 "handling event EVENT_RETRANSMIT for %s \"%s\" #%lu"
248 , ip_str(&peer
), c
->name
, st
->st_serialno
));
250 if (st
->st_retransmit
< MAXIMUM_RETRANSMISSIONS
)
251 delay
= EVENT_RETRANSMIT_DELAY_0
<< (st
->st_retransmit
+ 1);
252 else if (st
->st_state
== STATE_MAIN_I1
253 && c
->sa_keying_tries
== 0
254 && st
->st_retransmit
< MAXIMUM_RETRANSMISSIONS_INITIAL
)
255 delay
= EVENT_RETRANSMIT_DELAY_0
<< MAXIMUM_RETRANSMISSIONS
;
260 whack_log(RC_RETRANSMISSION
261 , "%s: retransmission; will wait %lus for response"
262 , enum_name(&state_names
, st
->st_state
)
263 , (unsigned long)delay
);
264 send_packet(st
, "EVENT_RETRANSMIT");
265 event_schedule(EVENT_RETRANSMIT
, delay
, st
);
269 /* check if we've tried rekeying enough times.
270 * st->st_try == 0 means that this should be the only try.
271 * c->sa_keying_tries == 0 means that there is no limit.
273 unsigned long try = st
->st_try
;
274 unsigned long try_limit
= c
->sa_keying_tries
;
275 const char *details
= "";
277 switch (st
->st_state
)
280 details
= ". Possible authentication failure:"
281 " no acceptable response to our"
282 " first encrypted message";
285 details
= ". No response (or no acceptable response) to our"
286 " first IKE message";
289 if (c
->newest_ipsec_sa
== SOS_NOBODY
)
290 details
= ". No acceptable response to our"
291 " first Quick Mode message:"
292 " perhaps peer likes no proposal";
297 loglog(RC_NORETRANSMISSION
298 , "max number of retransmissions (%d) reached %s%s"
300 , enum_show(&state_names
, st
->st_state
), details
);
301 if (try != 0 && try != try_limit
)
303 /* A lot like EVENT_SA_REPLACE, but over again.
304 * Since we know that st cannot be in use,
305 * we can delete it right away.
307 char story
[80]; /* arbitrary limit */
310 snprintf(story
, sizeof(story
), try_limit
== 0
311 ? "starting keying attempt %ld of an unlimited number"
312 : "starting keying attempt %ld of at most %ld"
315 if (st
->st_whack_sock
!= NULL_FD
)
317 /* Release whack because the observer will get bored. */
318 loglog(RC_COMMENT
, "%s, but releasing whack"
320 release_pending_whacks(st
, story
);
324 /* no whack: just log to syslog */
327 ipsecdoi_replace(st
, try);
334 case EVENT_SA_REPLACE
:
335 case EVENT_SA_REPLACE_IF_USED
:
337 so_serial_t newest
= IS_PHASE1(st
->st_state
)
338 ? c
->newest_isakmp_sa
: c
->newest_ipsec_sa
;
340 if (newest
!= st
->st_serialno
341 && newest
!= SOS_NOBODY
)
343 /* not very interesting: no need to replace */
345 , plog("not replacing stale %s SA: #%lu will do"
346 , IS_PHASE1(st
->st_state
)? "ISAKMP" : "IPsec"
349 else if (type
== EVENT_SA_REPLACE_IF_USED
350 && st
->st_outbound_time
<= tm
- c
->sa_rekey_margin
)
352 /* we observed no recent use: no need to replace
354 * The sampling effects mean that st_outbound_time
355 * could be up to SHUNT_SCAN_INTERVAL more recent
356 * than actual traffic because the sampler looks at change
357 * over that interval.
358 * st_outbound_time could also not yet reflect traffic
359 * in the last SHUNT_SCAN_INTERVAL.
360 * We expect that SHUNT_SCAN_INTERVAL is smaller than
361 * c->sa_rekey_margin so that the effects of this will
363 * This is just an optimization: correctness is not
366 * Note: we are abusing the DBG mechanism to control
370 , plog("not replacing stale %s SA: inactive for %lus"
371 , IS_PHASE1(st
->st_state
)? "ISAKMP" : "IPsec"
372 , (unsigned long)(tm
- st
->st_outbound_time
)));
377 , plog("replacing stale %s SA"
378 , IS_PHASE1(st
->st_state
)? "ISAKMP" : "IPsec"));
379 ipsecdoi_replace(st
, 1);
381 delete_dpd_event(st
);
382 event_schedule(EVENT_SA_EXPIRE
, st
->st_margin
, st
);
386 case EVENT_SA_EXPIRE
:
391 if (IS_PHASE1(st
->st_state
))
394 latest
= c
->newest_isakmp_sa
;
399 latest
= c
->newest_ipsec_sa
;
402 if (st
->st_serialno
!= latest
)
404 /* not very interesting: already superseded */
406 , plog("%s SA expired (superseded by #%lu)"
411 plog("%s SA expired (%s)", satype
412 , (c
->policy
& POLICY_DONT_REKEY
)
419 case EVENT_SO_DISCARD
:
420 /* Delete this state object. It must be in the hash table. */
427 case EVENT_DPD_TIMEOUT
:
431 case EVENT_NAT_T_KEEPALIVE
:
432 nat_traversal_ka_event();
436 loglog(RC_LOG_SERIOUS
, "INTERNAL ERROR: ignoring unknown expiring event %s"
437 , enum_show(&timer_event_names
, type
));
445 * Return the time until the next event in the queue
446 * expires (never negative), or -1 if no jobs in queue.
453 if (evlist
== (struct event
*) NULL
)
459 if (evlist
->ev_state
== NULL
)
460 DBG_log("next event %s in %ld seconds"
461 , enum_show(&timer_event_names
, evlist
->ev_type
)
462 , (long)evlist
->ev_time
- (long)tm
);
464 DBG_log("next event %s in %ld seconds for #%lu"
465 , enum_show(&timer_event_names
, evlist
->ev_type
)
466 , (long)evlist
->ev_time
- (long)tm
467 , evlist
->ev_state
->st_serialno
));
469 if (evlist
->ev_time
- tm
<= 0)
472 return evlist
->ev_time
- tm
;
479 delete_event(struct state
*st
)
481 if (st
->st_event
!= (struct event
*) NULL
)
485 for (ev
= &evlist
; ; ev
= &(*ev
)->ev_next
)
489 DBG(DBG_CONTROL
, DBG_log("event %s to be deleted not found",
490 enum_show(&timer_event_names
, st
->st_event
->ev_type
)));
493 if ((*ev
) == st
->st_event
)
495 *ev
= (*ev
)->ev_next
;
497 if (st
->st_event
->ev_type
== EVENT_RETRANSMIT
)
498 st
->st_retransmit
= 0;
500 st
->st_event
= (struct event
*) NULL
;
509 * Delete a DPD event.
512 delete_dpd_event(struct state
*st
)
514 if (st
->st_dpd_event
!= (struct event
*) NULL
)
518 for (ev
= &evlist
; ; ev
= &(*ev
)->ev_next
)
522 DBG(DBG_CONTROL
, DBG_log("event %s to be deleted not found",
523 enum_show(&timer_event_names
, st
->st_dpd_event
->ev_type
)));
526 if ((*ev
) == st
->st_dpd_event
)
528 *ev
= (*ev
)->ev_next
;
529 pfree(st
->st_dpd_event
);
530 st
->st_dpd_event
= (struct event
*) NULL
;