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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* $OpenBSD: dpd.c,v 1.2 2004/06/20 17:17:34 ho Exp $ */
/*
* Copyright (c) 2004 Håkan Olsson. All rights reserved.
*
* 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.
*
* 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.
*/
#include <sys/types.h>
#include <stdlib.h>
#include "sysdep.h"
#include "dpd.h"
#include "exchange.h"
#include "ipsec.h"
#include "isakmp_fld.h"
#include "log.h"
#include "message.h"
#include "sa.h"
#include "timer.h"
#include "util.h"
/* From RFC 3706. */
#define DPD_MAJOR 0x01
#define DPD_MINOR 0x00
#define DPD_SEQNO_SZ 4
static const char dpd_vendor_id[] = {
0xAF, 0xCA, 0xD7, 0x13, 0x68, 0xA1, 0xF1, /* RFC 3706 */
0xC9, 0x6B, 0x86, 0x96, 0xFC, 0x77, 0x57,
DPD_MAJOR,
DPD_MINOR
};
int16_t script_dpd[] = {
ISAKMP_PAYLOAD_NOTIFY, /* Initiator -> responder. */
ISAKMP_PAYLOAD_HASH,
EXCHANGE_SCRIPT_SWITCH,
ISAKMP_PAYLOAD_NOTIFY, /* Responder -> initiator. */
ISAKMP_PAYLOAD_HASH,
EXCHANGE_SCRIPT_END
};
static int dpd_initiator_send_notify(struct message *);
static int dpd_initiator_recv_ack(struct message *);
static int dpd_responder_recv_notify(struct message *);
static int dpd_responder_send_ack(struct message *);
static void dpd_event(void *);
int (*isakmp_dpd_initiator[])(struct message *) = {
dpd_initiator_send_notify,
dpd_initiator_recv_ack
};
int (*isakmp_dpd_responder[])(struct message *) = {
dpd_responder_recv_notify,
dpd_responder_send_ack
};
/* Add the DPD VENDOR ID payload. */
int
dpd_add_vendor_payload(struct message *msg)
{
u_int8_t *buf;
size_t buflen = sizeof dpd_vendor_id + ISAKMP_GEN_SZ;
buf = malloc(buflen);
if (!buf) {
log_error("dpd_add_vendor_payload: malloc(%lu) failed",
(unsigned long)buflen);
return -1;
}
SET_ISAKMP_GEN_LENGTH(buf, buflen);
memcpy(buf + ISAKMP_VENDOR_ID_OFF, dpd_vendor_id,
sizeof dpd_vendor_id);
if (message_add_payload(msg, ISAKMP_PAYLOAD_VENDOR, buf, buflen, 1)) {
free(buf);
return -1;
}
return 0;
}
/*
* Check an incoming message for DPD capability markers.
*/
void
dpd_check_vendor_payload(struct message *msg, struct payload *p)
{
u_int8_t *pbuf = p->p;
size_t vlen;
/* Already checked? */
if (msg->exchange->flags & EXCHANGE_FLAG_DPD_CAP_PEER) {
/* Just mark it as handled and return. */
p->flags |= PL_MARK;
return;
}
vlen = GET_ISAKMP_GEN_LENGTH(pbuf) - ISAKMP_GEN_SZ;
if (vlen != sizeof dpd_vendor_id) {
LOG_DBG((LOG_EXCHANGE, 90,
"dpd_check_vendor_payload: bad size %d != %d", vlen,
sizeof dpd_vendor_id));
return;
}
if (memcmp(dpd_vendor_id, pbuf + ISAKMP_GEN_SZ, vlen) == 0) {
/* This peer is DPD capable. */
msg->exchange->flags |= EXCHANGE_FLAG_DPD_CAP_PEER;
LOG_DBG((LOG_EXCHANGE, 10, "dpd_check_vendor_payload: "
"DPD capable peer detected"));
p->flags |= PL_MARK;
return;
}
return;
}
static int
dpd_add_notify(struct message *msg, u_int16_t type, u_int32_t seqno)
{
struct sa *isakmp_sa = msg->isakmp_sa;
char *buf;
u_int32_t buflen;
if (!isakmp_sa) {
log_print("dpd_add_notify: no isakmp_sa");
return -1;
}
buflen = ISAKMP_NOTIFY_SZ + ISAKMP_HDR_COOKIES_LEN + DPD_SEQNO_SZ;
buf = malloc(buflen);
if (!buf) {
log_error("dpd_add_notify: malloc(%d) failed",
ISAKMP_NOTIFY_SZ + DPD_SEQNO_SZ);
return -1;
}
SET_ISAKMP_NOTIFY_DOI(buf, IPSEC_DOI_IPSEC);
SET_ISAKMP_NOTIFY_PROTO(buf, ISAKMP_PROTO_ISAKMP);
SET_ISAKMP_NOTIFY_SPI_SZ(buf, ISAKMP_HDR_COOKIES_LEN);
SET_ISAKMP_NOTIFY_MSG_TYPE(buf, type);
memcpy(buf + ISAKMP_NOTIFY_SPI_OFF, isakmp_sa->cookies,
ISAKMP_HDR_COOKIES_LEN);
memcpy(buf + ISAKMP_NOTIFY_SPI_OFF + ISAKMP_HDR_COOKIES_LEN, &seqno,
sizeof (u_int32_t));
if (message_add_payload(msg, ISAKMP_PAYLOAD_NOTIFY, buf, buflen, 1)) {
free(buf);
return -1;
}
return 0;
}
static int
dpd_initiator_send_notify(struct message *msg)
{
if (!msg->isakmp_sa) {
log_print("dpd_initiator_send_notify: no isakmp_sa");
return -1;
}
if (msg->isakmp_sa->dpd_seq == 0) {
/* RFC 3706: first seq# should be random, with MSB zero. */
getrandom((u_int8_t *)&msg->isakmp_sa->seq,
sizeof msg->isakmp_sa->seq);
msg->isakmp_sa->dpd_seq &= 0x7FFF;
} else
msg->isakmp_sa->dpd_seq++;
return dpd_add_notify(msg, ISAKMP_NOTIFY_STATUS_DPD_R_U_THERE,
msg->isakmp_sa->dpd_seq);
}
static int
dpd_initiator_recv_ack(struct message *msg)
{
struct payload *p = payload_first(msg, ISAKMP_PAYLOAD_NOTIFY);
struct sa *isakmp_sa = msg->isakmp_sa;
struct timeval tv;
u_int32_t rseq;
if (msg->exchange->phase != 2) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_EXCHANGE_TYPE, 0, 1,
0);
return -1;
}
if (GET_ISAKMP_NOTIFY_MSG_TYPE(p->p)
!= ISAKMP_NOTIFY_STATUS_DPD_R_U_THERE_ACK) {
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 0);
return -1;
}
/* Presumably, we've been through message_validate_notify(). */
/* Validate the SPI. Perhaps move to message_validate_notify(). */
if (memcmp(p->p + ISAKMP_NOTIFY_SPI_OFF, isakmp_sa->cookies,
ISAKMP_HDR_COOKIES_LEN) != 0) {
log_print("dpd_initiator_recv_ack: bad cookies");
message_drop(msg, ISAKMP_NOTIFY_INVALID_SPI, 0, 1, 0);
return -1;
}
/* Check the seqno. */
memcpy(p->p + ISAKMP_NOTIFY_SPI_OFF + ISAKMP_HDR_COOKIES_LEN, &rseq,
sizeof rseq);
rseq = ntohl(rseq);
if (isakmp_sa->seq != rseq) {
log_print("dpd_initiator_recv_ack: bad seqno %u, expected %u",
rseq, isakmp_sa->seq);
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 0);
return -1;
}
/* Peer is alive. Reset timer. */
gettimeofday(&tv, 0);
tv.tv_sec += DPD_DEFAULT_WORRY_METRIC; /* XXX Configurable */
isakmp_sa->dpd_nextev = timer_add_event("dpd_event", dpd_event,
isakmp_sa, &tv);
if (!isakmp_sa->dpd_nextev)
log_print("dpd_initiator_recv_ack: timer_add_event "
"failed");
else
sa_reference(isakmp_sa);
/* Mark handled. */
p->flags |= PL_MARK;
return 0;
}
static int
dpd_responder_recv_notify(struct message *msg)
{
struct payload *p = payload_first(msg, ISAKMP_PAYLOAD_NOTIFY);
struct sa *isakmp_sa = msg->isakmp_sa;
struct timeval tv;
u_int32_t rseq;
if (msg->exchange->phase != 2) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_EXCHANGE_TYPE, 0, 1,
0);
return -1;
}
if (GET_ISAKMP_NOTIFY_MSG_TYPE(p->p) !=
ISAKMP_NOTIFY_STATUS_DPD_R_U_THERE) {
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 0);
return -1;
}
/* Presumably, we've gone through message_validate_notify(). */
/* XXX */
/* Validate the SPI. Perhaps move to message_validate_notify(). */
if (memcmp(p->p + ISAKMP_NOTIFY_SPI_OFF, isakmp_sa->cookies,
ISAKMP_HDR_COOKIES_LEN) != 0) {
log_print("dpd_initiator_recv_notify: bad cookies");
message_drop(msg, ISAKMP_NOTIFY_INVALID_SPI, 0, 1, 0);
return -1;
}
/* Get the seqno. */
memcpy(p->p + ISAKMP_NOTIFY_SPI_OFF + ISAKMP_HDR_COOKIES_LEN, &rseq,
sizeof rseq);
rseq = ntohl(rseq);
/* Check increasing seqno. */
if (rseq <= isakmp_sa->dpd_rseq) {
log_print("dpd_initiator_recv_notify: bad seqno (%u <= %u)",
rseq, isakmp_sa->dpd_rseq);
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 0);
return -1;
}
isakmp_sa->dpd_rseq = rseq;
/*
* Ok, now we know the peer is alive, in case we're wondering.
* If so, reset timers, etc... here.
*/
if (isakmp_sa->dpd_nextev) {
timer_remove_event(isakmp_sa->dpd_nextev);
sa_release(isakmp_sa);
gettimeofday(&tv, 0);
tv.tv_sec += DPD_DEFAULT_WORRY_METRIC; /* XXX Configurable */
isakmp_sa->dpd_nextev = timer_add_event("dpd_event", dpd_event,
isakmp_sa, &tv);
if (!isakmp_sa->dpd_nextev)
log_print("dpd_responder_recv_notify: timer_add_event "
"failed");
else
sa_reference(isakmp_sa);
}
/* Mark handled. */
p->flags |= PL_MARK;
return 0;
}
static int
dpd_responder_send_ack(struct message *msg)
{
if (!msg->isakmp_sa)
return -1;
return dpd_add_notify(msg, ISAKMP_NOTIFY_STATUS_DPD_R_U_THERE_ACK,
msg->isakmp_sa->dpd_rseq);
}
static void
dpd_event(void *v_sa)
{
struct sa *sa = v_sa;
sa->dpd_nextev = 0;
sa_release(sa);
if ((sa->flags & SA_FLAG_DPD) == 0)
return;
/* Create a new DPD exchange. XXX */
}
|