1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* $OpenBSD: schedule.c,v 1.8 2002/06/09 08:13:08 todd Exp $ */
/*
* Copyright 1997-2000 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Parts derived from code by Angelos D. Keromytis, kermit@forthnet.gr
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* schedule.c:
* SCHEDULE handling functions
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: schedule.c,v 1.8 2002/06/09 08:13:08 todd Exp $";
#endif
#define _SCHEDULE_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "state.h"
#include "spi.h"
#include "photuris.h"
#include "buffer.h"
#include "schedule.h"
#include "log.h"
#include "cookie.h"
#include "modulus.h"
#include "api.h"
#ifdef IPSEC
#include "attributes.h"
#include "kernel.h"
#endif
#ifdef DEBUG
#include "config.h"
#endif
static struct schedule *schedob = NULL;
void
schedule_insert(int type, int off, u_int8_t *cookie, u_int16_t cookie_size)
{
struct schedule *tmp;
#ifdef DEBUG
{
if( cookie != NULL) {
int i = BUFFER_SIZE;
bin2hex(buffer, &i, cookie, cookie_size);
}
printf("Adding event type %d, due in %d seconds, cookie %s\n",
type, off, cookie == NULL ? "None" : (char *)buffer);
}
#endif
if ((tmp = calloc(1, sizeof(struct schedule))) == NULL) {
log_error("calloc() in schedule_insert()");
return;
}
tmp->event = type;
tmp->offset = off;
tmp->tm = time(NULL) + off;
if (cookie != NULL) {
tmp->cookie = calloc(cookie_size, sizeof(u_int8_t));
if (tmp->cookie == NULL) {
log_error("calloc() in schedule_insert()");
free(tmp);
return;
}
bcopy(cookie, tmp->cookie, cookie_size);
tmp->cookie_size = cookie_size;
}
tmp->next = NULL;
if (schedob == NULL)
schedob = tmp;
else {
tmp->next = schedob;
schedob = tmp;
}
}
int
schedule_next(void)
{
struct schedule *tmp;
time_t tm;
if (schedob == NULL)
return -1;
tm = schedob->tm;
tmp = schedob->next;
while (tmp != NULL) {
if (tmp->tm < tm)
tm = tmp->tm;
tmp = tmp->next;
}
if ((tm -= time(NULL)) < 0)
return 0;
return((int) tm);
}
int
schedule_offset(int type, u_int8_t *cookie)
{
struct schedule *tmp = schedob;
while (tmp != NULL) {
if (tmp->event == type &&
((tmp->cookie == NULL && cookie == NULL) ||
!bcmp(tmp->cookie, cookie, tmp->cookie_size)))
return tmp->offset;
tmp = tmp->next;
}
return -1;
}
void
schedule_remove(int type, u_int8_t *cookie)
{
struct schedule *tmp, *otmp = NULL;
tmp = schedob;
while (tmp != NULL) {
if (tmp->event == type &&
((tmp->cookie == NULL && cookie == NULL) ||
!bcmp(tmp->cookie, cookie, tmp->cookie_size))) {
if (tmp == schedob)
schedob = tmp->next;
else
otmp->next = tmp->next;
if (tmp->cookie != NULL)
free(tmp->cookie);
free(tmp);
return;
}
otmp = tmp;
tmp = tmp->next;
}
}
void
schedule_process(int sock)
{
struct schedule *tmp, *tmp2;
struct sockaddr_in sin;
struct stateob *st;
time_t tm;
int remove;
tm = time(NULL);
tmp = schedob;
while (tmp != NULL) {
if (tmp->tm > tm) {
tmp = tmp->next;
continue;
}
remove = 0;
switch(tmp->event) {
case REKEY:
reset_secret();
tmp->tm = time(NULL) + REKEY_TIMEOUT;
break;
case MODULUS:
#ifdef DEBUG2
printf("Checking moduli\n");
#endif
mod_check_prime(MOD_PRIME_ITER, MOD_PRIME_TIME);
tmp->tm = time(NULL) + MODULUS_TIMEOUT;
break;
case CLEANUP:
#ifdef DEBUG2
printf("Cleaning up states\n");
#endif
state_expire();
#ifdef DEBUG2
printf("Cleaning up SPI's\n");
#endif
spi_expire();
tmp->tm = time(NULL) + CLEANUP_TIMEOUT;
break;
case TIMEOUT:
st = state_find_cookies(NULL, tmp->cookie, NULL);
if (st == NULL) {
remove = 1;
break;
} else if (st->retries >= max_retries) {
remove = 1;
if (st->phase == COOKIE_REQUEST && st->resource == 0) {
log_print("no anwser for cookie request to %s:%d",
st->address, st->port);
#ifdef IPSEC
if (st->flags & IPSEC_NOTIFY)
kernel_notify_result(st, NULL, 0);
#endif
break;
} else if(st->phase == COOKIE_REQUEST) {
/* Try again with updated counters */
struct stateob *newst;
if ((newst = state_new()) == NULL) {
log_error("state_new() in schedule_process()");
break;
}
state_copy_flags(st, newst);
#ifdef DEBUG
printf("Starting a new exchange to %s:%d with updated rcookie and"
" counter.\n", newst->address, newst->port);
#endif /* DEBUG */
start_exchange(sock, newst, st->address, st->port);
state_insert(newst);
break;
} else {
log_print("exchange terminated, phase %d to %s:%d",
st->phase, st->address, st->port);
break;
}
}
if (st->packet == NULL || st->packetlen == 0) {
log_print("no packet in schedule_process()");
remove = 1;
break;
}
/* Only send the packet when no error occurred */
if (!remove) {
st->retries++;
sin.sin_port = htons(st->port);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(st->address);
if (sendto(sock, st->packet, st->packetlen, 0,
(struct sockaddr *) &sin, sizeof(sin))
!= st->packetlen) {
log_error("sendto() in schedule_process()");
remove = 1;
break;
}
#ifdef DEBUG
printf("Resending packet to %s type %d, length %d.\n",
st->address, st->phase, st->packetlen);
#endif
tmp->tm = tm + retrans_timeout;
}
break;
case UPDATE:
spi_update(sock, tmp->cookie);
remove = 1;
break;
default:
remove = 1;
log_print("Unknown event in schedule_process()");
break;
}
if (remove) {
tmp2 = tmp;
tmp = tmp->next;
schedule_remove(tmp2->event, tmp2->cookie);
} else
tmp = tmp->next;
}
}
void
init_schedule(void)
{
schedule_insert(REKEY, REKEY_TIMEOUT, NULL, 0);
schedule_insert(CLEANUP, CLEANUP_TIMEOUT, NULL, 0);
schedule_insert(MODULUS, MODULUS_TIMEOUT, NULL, 0);
}
|