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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/*
%%% copyright-nrl-97
This software is Copyright 1997-1998 by Randall Atkinson, Ronald Lee,
Daniel McDonald, Bao Phan, and Chris Winters. All Rights Reserved. All
rights under this copyright have been assigned to the US Naval Research
Laboratory (NRL). The NRL Copyright Notice and License Agreement Version
1.1 (January 17, 1995) applies to this software.
You should have received a copy of the license with this software. If you
didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
%%% copyright-cmetz-97
This software is Copyright 1997-1998 by Craig Metz, All Rights Reserved.
The Inner Net License Version 2 applies to this software.
You should have received a copy of the license with this software. If
you didn't get a copy, you may request one from <license@inner.net>.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/socketvar.h>
#include <sys/proc.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/ip_ipsp.h>
#include <net/pfkeyv2.h>
#define BITMAP_SA (1 << SADB_EXT_SA)
#define BITMAP_LIFETIME_CURRENT (1 << SADB_EXT_LIFETIME_CURRENT)
#define BITMAP_LIFETIME_HARD (1 << SADB_EXT_LIFETIME_HARD)
#define BITMAP_LIFETIME_SOFT (1 << SADB_EXT_LIFETIME_SOFT)
#define BITMAP_ADDRESS_SRC (1 << SADB_EXT_ADDRESS_SRC)
#define BITMAP_ADDRESS_DST (1 << SADB_EXT_ADDRESS_DST)
#define BITMAP_ADDRESS_PROXY (1 << SADB_EXT_ADDRESS_PROXY)
#define BITMAP_KEY_AUTH (1 << SADB_EXT_KEY_AUTH)
#define BITMAP_KEY_ENCRYPT (1 << SADB_EXT_KEY_ENCRYPT)
#define BITMAP_IDENTITY_SRC (1 << SADB_EXT_IDENTITY_SRC)
#define BITMAP_IDENTITY_DST (1 << SADB_EXT_IDENTITY_DST)
#define BITMAP_SENSITIVITY (1 << SADB_EXT_SENSITIVITY)
#define BITMAP_PROPOSAL (1 << SADB_EXT_PROPOSAL)
#define BITMAP_SUPPORTED (1 << SADB_EXT_SUPPORTED)
#define BITMAP_SPIRANGE (1 << SADB_EXT_SPIRANGE)
#define BITMAP_LIFETIME (BITMAP_LIFETIME_CURRENT | BITMAP_LIFETIME_HARD | BITMAP_LIFETIME_SOFT)
#define BITMAP_ADDRESS (BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_ADDRESS_PROXY)
#define BITMAP_KEY (BITMAP_KEY_AUTH | BITMAP_KEY_ENCRYPT)
#define BITMAP_IDENTITY (BITMAP_IDENTITY_SRC | BITMAP_IDENTITY_DST)
#define BITMAP_MSG 1
#define BITMAP_X_SRC_MASK (1 << SADB_X_EXT_SRC_MASK)
#define BITMAP_X_DST_MASK (1 << SADB_X_EXT_DST_MASK)
#define BITMAP_X_PROTOCOL (1 << SADB_X_EXT_PROTOCOL)
#define BITMAP_X_SRC_FLOW (1 << SADB_X_EXT_SRC_FLOW)
#define BITMAP_X_DST_FLOW (1 << SADB_X_EXT_DST_FLOW)
#define BITMAP_X_FLOW_TYPE (1 << SADB_X_EXT_FLOW_TYPE)
uint32_t sadb_exts_allowed_in[SADB_MAX+1] =
{
/* RESERVED */
~0,
/* GETSPI */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SPIRANGE,
/* UPDATE */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY,
/* ADD */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY,
/* DELETE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* GET */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* ACQUIRE */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_IDENTITY | BITMAP_PROPOSAL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK,
/* REGISTER */
0,
/* EXPIRE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* FLUSH */
0,
/* DUMP */
0,
/* X_PROMISC */
0,
/* X_ADDFLOW */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SA | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
/* X_DELFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_SA | BITMAP_ADDRESS_DST | BITMAP_X_FLOW_TYPE,
};
uint32_t sadb_exts_required_in[SADB_MAX+1] =
{
/* RESERVED */
0,
/* GETSPI */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SPIRANGE,
/* UPDATE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* ADD */
BITMAP_SA | BITMAP_ADDRESS_DST,
/* DELETE */
BITMAP_SA | BITMAP_ADDRESS_DST,
/* GET */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* ACQUIRE */
0,
/* REGISTER */
0,
/* EXPIRE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* FLUSH */
0,
/* DUMP */
0,
/* X_PROMISC */
0,
/* X_ADDFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
/* X_DELFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
};
uint32_t sadb_exts_allowed_out[SADB_MAX+1] =
{
/* RESERVED */
~0,
/* GETSPI */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* UPDATE */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY,
/* ADD */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY,
/* DELETE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* GET */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY,
/* ACQUIRE */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_IDENTITY | BITMAP_PROPOSAL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK,
/* REGISTER */
BITMAP_SUPPORTED,
/* EXPIRE */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS,
/* FLUSH */
0,
/* DUMP */
BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY,
/* X_PROMISC */
0,
/* X_ADDFLOW */
BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SA | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
/* X_DELFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_SA | BITMAP_ADDRESS_DST | BITMAP_X_FLOW_TYPE,
};
uint32_t sadb_exts_required_out[SADB_MAX+1] =
{
/* RESERVED */
0,
/* GETSPI */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* UPDATE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* ADD */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* DELETE */
BITMAP_SA | BITMAP_ADDRESS_DST,
/* GET */
BITMAP_SA | BITMAP_LIFETIME_CURRENT | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* ACQUIRE */
0,
/* REGISTER */
BITMAP_SUPPORTED,
/* EXPIRE */
BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST,
/* FLUSH */
0,
/* DUMP */
0,
/* X_PROMISC */
0,
/* X_ADDFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
/* X_DELFLOW */
BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE,
};
int pfkeyv2_parsemessage(void *, int, void **);
#define RETURN_EINVAL(line) goto einval;
int
pfkeyv2_parsemessage(void *p, int len, void **headers)
{
struct sadb_ext *sadb_ext;
int i, left = len;
uint32_t allow, seen = 1;
struct sadb_msg *sadb_msg = (struct sadb_msg *) p;
bzero(headers, (SADB_EXT_MAX + 1) * sizeof(void *));
if (left < sizeof(struct sadb_msg))
return EINVAL;
headers[0] = p;
if (sadb_msg->sadb_msg_len * sizeof(uint64_t) != left)
return EINVAL;
p += sizeof(struct sadb_msg);
left -= sizeof(struct sadb_msg);
if (sadb_msg->sadb_msg_reserved)
return EINVAL;
if (sadb_msg->sadb_msg_type > SADB_MAX)
return EINVAL;
if (!sadb_msg->sadb_msg_type)
return EINVAL;
if (sadb_msg->sadb_msg_pid != curproc->p_pid)
return EINVAL;
if (sadb_msg->sadb_msg_errno) {
if (left)
return EINVAL;
return 0;
}
if (sadb_msg->sadb_msg_type == SADB_X_PROMISC)
return 0;
allow = sadb_exts_allowed_in[sadb_msg->sadb_msg_type];
while (left > 0) {
sadb_ext = (struct sadb_ext *)p;
if (left < sizeof(struct sadb_ext))
return EINVAL;
i = sadb_ext->sadb_ext_len * sizeof(uint64_t);
if (left < i)
return EINVAL;
if (sadb_ext->sadb_ext_type > SADB_EXT_MAX)
return EINVAL;
if (!sadb_ext->sadb_ext_type)
return EINVAL;
if (!(allow & (1 << sadb_ext->sadb_ext_type)))
return EINVAL;
if (headers[sadb_ext->sadb_ext_type])
return EINVAL;
seen |= (1 << sadb_ext->sadb_ext_type);
switch (sadb_ext->sadb_ext_type) {
case SADB_EXT_SA:
{
struct sadb_sa *sadb_sa = (struct sadb_sa *)p;
if (i != sizeof(struct sadb_sa))
return EINVAL;
if (sadb_sa->sadb_sa_state > SADB_SASTATE_MAX)
return EINVAL;
if (sadb_sa->sadb_sa_state == SADB_SASTATE_DEAD)
return EINVAL;
if (sadb_sa->sadb_sa_encrypt > SADB_EALG_MAX)
return EINVAL;
if (sadb_sa->sadb_sa_auth > SADB_AALG_MAX)
return EINVAL;
if (sadb_sa->sadb_sa_replay > 32)
return EINVAL;
}
break;
case SADB_X_EXT_PROTOCOL:
case SADB_X_EXT_FLOW_TYPE:
if (i != sizeof(struct sadb_protocol))
return EINVAL;
break;
case SADB_EXT_LIFETIME_CURRENT:
case SADB_EXT_LIFETIME_HARD:
case SADB_EXT_LIFETIME_SOFT:
{
if (i != sizeof(struct sadb_lifetime))
return EINVAL;
}
break;
case SADB_EXT_ADDRESS_SRC:
case SADB_EXT_ADDRESS_DST:
case SADB_X_EXT_SRC_MASK:
case SADB_X_EXT_DST_MASK:
case SADB_X_EXT_SRC_FLOW:
case SADB_X_EXT_DST_FLOW:
case SADB_EXT_ADDRESS_PROXY:
{
struct sadb_address *sadb_address = (struct sadb_address *)p;
struct sockaddr *sa = (struct sockaddr *)(p + sizeof(struct sadb_address));
if (i < sizeof(struct sadb_address) + sizeof(struct sockaddr))
return EINVAL;
if (sadb_address->sadb_address_reserved)
return EINVAL;
#if SALEN
if (sa->sa_len && (i != sizeof(struct sadb_address) + sa->sa_len))
return EINVAL;
#endif /* SALEN */
switch(sa->sa_family) {
case AF_INET:
if (sizeof(struct sadb_address) + sizeof(struct sockaddr_in)
!= i)
return EINVAL;
#if SALEN
if (sa->sa_len != sizeof(struct sockaddr_in))
return EINVAL;
#endif /* SALEN */
/* Only check the right pieces */
switch (sadb_ext->sadb_ext_type)
{
case SADB_X_EXT_SRC_MASK:
case SADB_X_EXT_DST_MASK:
case SADB_X_EXT_SRC_FLOW:
case SADB_X_EXT_DST_FLOW:
break;
default:
if (((struct sockaddr_in *)sa)->sin_port)
return EINVAL;
break;
}
{
char zero[sizeof(((struct sockaddr_in *)sa)->sin_zero)];
bzero(zero, sizeof(zero));
if (bcmp(&((struct sockaddr_in *)sa)->sin_zero, zero,
sizeof(zero)))
return EINVAL;
}
break;
#if INET6
case AF_INET6:
if (i != sizeof(struct sadb_address) +
sizeof(struct sockaddr_in6) + 4)
return EINVAL;
if (sa->sa_len != sizeof(struct sockaddr_in6))
return EINVAL;
if (((struct sockaddr_in6 *)sa)->sin6_port)
return EINVAL;
if (((struct sockaddr_in6 *)sa)->sin6_flowinfo)
return EINVAL;
break;
#endif /* INET6 */
default:
return EINVAL;
}
}
break;
case SADB_EXT_KEY_AUTH:
case SADB_EXT_KEY_ENCRYPT:
{
struct sadb_key *sadb_key = (struct sadb_key *)p;
if (i < sizeof(struct sadb_key))
return EINVAL;
if (!sadb_key->sadb_key_bits)
return EINVAL;
if (((sadb_key->sadb_key_bits + 63) / 64) * sizeof(uint64_t) !=
i - sizeof(struct sadb_key))
return EINVAL;
if (sadb_key->sadb_key_reserved)
return EINVAL;
}
break;
case SADB_EXT_IDENTITY_SRC:
case SADB_EXT_IDENTITY_DST:
{
struct sadb_ident *sadb_ident = (struct sadb_ident *)p;
if (i < sizeof(struct sadb_ident))
return EINVAL;
if (sadb_ident->sadb_ident_type > SADB_IDENTTYPE_MAX)
return EINVAL;
if (sadb_ident->sadb_ident_reserved)
return EINVAL;
if (i > sizeof(struct sadb_ident)) {
char *c = (char *)(p + sizeof(struct sadb_ident));
int j;
if (*(char *)(p + i - 1))
return EINVAL;
j = ((strlen(c) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t)-1)) +
sizeof(struct sadb_ident);
if (i != j)
return EINVAL;
}
}
break;
case SADB_EXT_SENSITIVITY:
{
struct sadb_sens *sadb_sens = (struct sadb_sens *)p;
if (i < sizeof(struct sadb_sens))
return EINVAL;
if (i != (sadb_sens->sadb_sens_sens_len +
sadb_sens->sadb_sens_integ_len) * sizeof(uint64_t) +
sizeof(struct sadb_sens))
return EINVAL;
}
break;
case SADB_EXT_PROPOSAL:
{
struct sadb_prop *sadb_prop = (struct sadb_prop *)p;
if (i < sizeof(struct sadb_prop))
return EINVAL;
if (sadb_prop->sadb_prop_reserved)
return EINVAL;
if ((i - sizeof(struct sadb_prop)) % sizeof(struct sadb_comb))
return EINVAL;
{
struct sadb_comb *sadb_comb = (struct sadb_comb *)(p + sizeof(struct sadb_prop));
int j;
for (j = 0;
j < (i - sizeof(struct sadb_prop))/sizeof(struct sadb_comb);
j++) {
if (sadb_comb->sadb_comb_auth > SADB_AALG_MAX)
return EINVAL;
if (sadb_comb->sadb_comb_encrypt > SADB_EALG_MAX)
return EINVAL;
if (sadb_comb->sadb_comb_reserved)
return EINVAL;
}
}
}
break;
case SADB_EXT_SUPPORTED:
{
struct sadb_supported *sadb_supported = (struct sadb_supported *)p;
int j;
if (i < sizeof(struct sadb_supported))
return EINVAL;
if (sadb_supported->sadb_supported_reserved)
return EINVAL;
if (i != ((sadb_supported->sadb_supported_nauth +
sadb_supported->sadb_supported_nencrypt) *
sizeof(struct sadb_alg)) + sizeof(struct sadb_supported))
return EINVAL;
{
struct sadb_alg *sadb_alg = (struct sadb_alg *)(p + sizeof(struct sadb_supported));
for (j = 0; j < sadb_supported->sadb_supported_nauth; j++) {
if (sadb_alg->sadb_alg_type > SADB_AALG_MAX)
return EINVAL;
if (sadb_alg->sadb_alg_reserved)
return EINVAL;
sadb_alg++;
}
for (j = 0; j < sadb_supported->sadb_supported_nencrypt; j++) {
if (sadb_alg->sadb_alg_type > SADB_EALG_MAX)
return EINVAL;
if (sadb_alg->sadb_alg_reserved)
return EINVAL;
sadb_alg++;
}
}
}
break;
case SADB_EXT_SPIRANGE:
{
struct sadb_spirange *sadb_spirange = (struct sadb_spirange *)p;
if (i != sizeof(struct sadb_spirange))
return EINVAL;
if (sadb_spirange->sadb_spirange_min >
sadb_spirange->sadb_spirange_max)
return EINVAL;
}
break;
default:
return EINVAL;
}
headers[sadb_ext->sadb_ext_type] = p;
p += i;
left -= i;
}
if (left)
return EINVAL;
{
uint32_t required;
required = sadb_exts_required_in[sadb_msg->sadb_msg_type];
if ((seen & required) != required)
return EINVAL;
}
switch(((struct sadb_msg *)headers[0])->sadb_msg_type) {
case SADB_UPDATE:
if (((struct sadb_sa *)headers[SADB_EXT_SA])->sadb_sa_state !=
SADB_SASTATE_MATURE)
return EINVAL;
break;
case SADB_ADD:
if (((struct sadb_sa *)headers[SADB_EXT_SA])->sadb_sa_state !=
SADB_SASTATE_MATURE)
return EINVAL;
break;
}
return 0;
}
|