/
key.c
5876 lines (5052 loc) · 136 KB
/
key.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* 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.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``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 PROJECT OR CONTRIBUTORS 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.
*/
/* KAME $Id: key.c,v 1.30 1999/12/02 05:32:13 sakane Exp $ */
/*
* This code is referd to RFC 2367
*/
#if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
#include "opt_inet.h"
#ifdef __NetBSD__
#include "opt_ipsec.h"
#endif
#endif
#ifdef __NetBSD__
# ifdef _KERNEL
# define KERNEL
# endif
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#ifdef __FreeBSD__
#include <sys/sysctl.h>
#endif
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/route.h>
#include <net/raw_cb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#ifdef INET6
#include <netinet6/ip6.h>
#include <netinet6/in6_var.h>
#include <netinet6/in6_pcb.h>
#endif /* INET6 */
#include <netkey/keyv2.h>
#include <netkey/keydb.h>
#include <netkey/key.h>
#include <netkey/keysock.h>
#include <netkey/key_debug.h>
#include <netinet6/ipsec.h>
#include <netinet6/ah.h>
#ifdef IPSEC_ESP
#include <netinet6/esp.h>
#endif
#include <netinet6/ipcomp.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
#endif
#if defined(IPSEC_DEBUG)
u_int32_t key_debug_level = 0;
#endif /* defined(IPSEC_DEBUG) */
static u_int key_spi_trycnt = 1000;
static u_int32_t key_spi_minval = 0x100;
static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
static u_int32_t acq_seq = 0;
static int key_tick_init_random = 0;
static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
/* registed list */
#ifndef IPSEC_NONBLOCK_ACQUIRE
static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
#endif
struct key_cb key_cb;
/* search order for SAs */
static u_int saorder_state_valid[] = {
SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
/*
* This order is important because we must select a oldest SA
* for outbound processing. For inbound, This is not important.
*/
};
static u_int saorder_state_alive[] = {
/* except DEAD */
SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
};
static u_int saorder_state_any[] = {
SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
};
#ifdef __FreeBSD__
#if defined(IPSEC_DEBUG)
SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
&key_debug_level, 0, "");
#endif /* defined(IPSEC_DEBUG) */
/* max count of trial for the decision of spi value */
SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
&key_spi_trycnt, 0, "");
/* minimum spi value to allocate automatically. */
SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
&key_spi_minval, 0, "");
/* maximun spi value to allocate automatically. */
SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
&key_spi_maxval, 0, "");
/* interval to initialize randseed */
SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
&key_int_random, 0, "");
/* lifetime for larval SA */
SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
&key_larval_lifetime, 0, "");
/* counter for blocking to send SADB_ACQUIRE to IKEd */
SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
&key_blockacq_count, 0, "");
/* lifetime for blocking to send SADB_ACQUIRE to IKEd */
SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
&key_blockacq_lifetime, 0, "");
#endif /* __FreeBSD__ */
#if (defined(__bsdi__) && !defined(ALTQ)) || defined(__NetBSD__)
typedef void (timeout_t)(void *);
#endif
#define __LIST_FOREACH(elm, head, field) \
for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
#define __LIST_CHAINED(elm) \
(!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
#define LIST_INSERT_TAIL(head, elm, type, field) do {\
struct type *curelm = LIST_FIRST(head); \
if (curelm == NULL) {\
LIST_INSERT_HEAD(head, elm, field); \
} else { \
while (LIST_NEXT(curelm, field)) \
curelm = LIST_NEXT(curelm, field);\
LIST_INSERT_AFTER(curelm, elm, field);\
}\
} while (0)
#define KEY_CHKSASTATE(head, sav, name) { \
if ((head) != (sav)) { \
printf("%s: state mismatched (TREE=%d SA=%d)\n", \
(name), (head), (sav)); \
continue; \
} \
}
#define KEY_CHKSPDIR(head, sp, name) { \
if ((head) != (sp)) { \
printf("%s: direction mismatched (TREE=%d SP=%d), " \
"anyway continue.\n", \
(name), (head), (sp)); \
} \
}
#if 1
#define KMALLOC(p, t, n) \
((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
#define KFREE(p) \
free((caddr_t)(p), M_SECA);
#else
#define KMALLOC(p, t, n) do { \
((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
__FILE__, __LINE__, (p), #t, n); \
} while (0)
#define KFREE(p) \
do { \
printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
free((caddr_t)(p), M_SECA); \
} while (0)
#endif
#define KEY_NEWBUF(dst, t, src, len) \
((dst) = (t)key_newbuf((src), (len)))
/*
* set parameters into secpolicyindex buffer.
* Must allocate secpolicyindex buffer passed to this function.
*/
#define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) do { \
bzero((idx), sizeof(struct secpolicyindex)); \
(idx)->dir = (_dir); \
(idx)->prefs = (ps); \
(idx)->prefd = (pd); \
(idx)->ul_proto = (ulp); \
bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
} while (0)
/*
* set parameters into secasindex buffer.
* Must allocate secasindex buffer before calling this function.
*/
#define KEY_SETSECASIDX(p, m, s, d, idx) do { \
bzero((idx), sizeof(struct secasindex)); \
(idx)->proto = (p); \
(idx)->mode = (m)->sadb_msg_mode; \
(idx)->reqid = (m)->sadb_msg_reqid; \
bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
} while (0)
/* key statistics */
struct _keystat {
u_long getspi_count; /* the avarage of count to try to get new SPI */
} keystat;
static struct secasvar *key_allocsa_policy __P((struct secasindex *saidx));
static void key_freesp_so __P((struct secpolicy **sp));
static struct secasvar *key_do_allocsa_policy __P((struct secashead *sah,
u_int state));
static void key_delsp __P((struct secpolicy *sp));
static struct secpolicy *key_getsp __P((struct secpolicyindex *spidx));
static u_int32_t key_newreqid __P((void));
static struct sadb_msg *key_spdadd __P((caddr_t *mhp));
static struct sadb_msg *key_spddelete __P((caddr_t *mhp));
static struct sadb_msg *key_spdflush __P((caddr_t *mhp));
static int key_spddump __P((caddr_t *mhp, struct socket *so, int target));
static u_int key_setdumpsp __P((struct sadb_msg *newmsg, struct secpolicy *sp,
u_int8_t type, u_int32_t seq, u_int32_t pid));
static u_int key_getspmsglen __P((struct secpolicy *sp));
static u_int key_getspreqmsglen __P((struct secpolicy *sp));
static struct secashead *key_newsah __P((struct secasindex *saidx));
static void key_delsah __P((struct secashead *sah));
static struct secasvar *key_newsav __P((caddr_t *mhp, struct secashead *sah));
static void key_delsav __P((struct secasvar *sav));
static struct secashead *key_getsah __P((struct secasindex *saidx));
static struct secasvar *key_checkspidup __P((struct secasindex *saidx,
u_int32_t spi));
static struct secasvar *key_getsavbyspi __P((struct secashead *sah,
u_int32_t spi));
static int key_setsaval __P((struct secasvar *sav, caddr_t *mhp));
static u_int key_getmsglen __P((struct secasvar *sav));
static int key_mature __P((struct secasvar *sav));
static u_int key_setdumpsa __P((struct sadb_msg *newmsg, struct secasvar *sav,
u_int8_t type, u_int8_t satype,
u_int32_t seq, u_int32_t pid));
static caddr_t key_setsadbmsg __P((caddr_t buf, u_int8_t type, int tlen,
u_int8_t satype, u_int32_t seq, pid_t pid,
u_int8_t mode, u_int32_t reqid,
u_int8_t reserved1, u_int32_t reserved2));
static caddr_t key_setsadbsa __P((caddr_t buf, struct secasvar *sav));
static caddr_t key_setsadbaddr __P((caddr_t buf, u_int16_t exttype,
struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto));
static caddr_t key_setsadbident
__P((caddr_t buf, u_int16_t exttype, u_int16_t idtype,
caddr_t string, int stringlen, u_int64_t id));
static caddr_t key_setsadbext __P((caddr_t p, caddr_t ext));
static void *key_newbuf __P((void *src, u_int len));
#ifdef INET6
static int key_ismyaddr6 __P((caddr_t addr));
#endif
#if 0
static int key_isloopback __P((u_int family, caddr_t addr));
#endif
static int key_cmpsaidx_exactly
__P((struct secasindex *saidx0, struct secasindex *saidx1));
static int key_cmpsaidx_withmode
__P((struct secasindex *saidx0, struct secasindex *saidx1));
static int key_cmpspidx_exactly
__P((struct secpolicyindex *spidx0, struct secpolicyindex *spidx1));
static int key_cmpspidx_withmask
__P((struct secpolicyindex *spidx0, struct secpolicyindex *spidx1));
static int key_bbcmp __P((caddr_t p1, caddr_t p2, u_int bits));
static u_int16_t key_satype2proto __P((u_int8_t satype));
static u_int8_t key_proto2satype __P((u_int16_t proto));
static struct sadb_msg *key_getspi __P((caddr_t *mhp));
static u_int32_t key_do_getnewspi __P((struct sadb_spirange *spirange,
struct secasindex *saidx));
static struct sadb_msg *key_update __P((caddr_t *mhp));
static struct secasvar *key_getsavbyseq __P((struct secashead *sah,
u_int32_t seq));
static struct sadb_msg *key_add __P((caddr_t *mhp));
static int key_setident __P((struct secashead *sah, caddr_t *mhp));
static struct sadb_msg *key_getmsgbuf_x1 __P((caddr_t *mhp));
static struct sadb_msg *key_delete __P((caddr_t *mhp));
static struct sadb_msg *key_get __P((caddr_t *mhp));
static int key_acquire __P((struct secasindex *saidx,
struct secpolicyindex *spidx));
static struct secacq *key_newacq __P((struct secasindex *saidx));
static struct secacq *key_getacq __P((struct secasindex *saidx));
static struct secacq *key_getacqbyseq __P((u_int32_t seq));
static struct sadb_msg *key_acquire2 __P((caddr_t *mhp));
static struct sadb_msg *key_register __P((caddr_t *mhp, struct socket *so));
static int key_expire __P((struct secasvar *sav));
static struct sadb_msg *key_flush __P((caddr_t *mhp));
static int key_dump __P((caddr_t *mhp, struct socket *so, int target));
static void key_promisc __P((caddr_t *mhp, struct socket *so));
static int key_sendall __P((struct sadb_msg *msg, u_int len));
static int key_align __P((struct sadb_msg *msg, caddr_t *mhp));
#if 0
static const char *key_getfqdn __P((void));
static const char *key_getuserfqdn __P((void));
#endif
static void key_sa_chgstate __P((struct secasvar *sav, u_int8_t state));
/* %%% IPsec policy management */
/*
* allocating a SP for OUTBOUND or INBOUND packet.
* Must call key_freesp() later.
* OUT: NULL: not found
* others: found and return the pointer.
*/
struct secpolicy *
key_allocsp(spidx, dir)
struct secpolicyindex *spidx;
u_int dir;
{
struct secpolicy *sp;
int s;
/* sanity check */
if (spidx == NULL)
panic("key_allocsp: NULL pointer is passed.\n");
/* check direction */
switch (dir) {
case IPSEC_DIR_INBOUND:
case IPSEC_DIR_OUTBOUND:
break;
default:
panic("key_allocsp: Invalid direction is passed.\n");
}
/* get a SP entry */
#ifdef __NetBSD__
s = splsoftnet(); /*called from softclock()*/
#else
s = splnet(); /*called from softclock()*/
#endif
KEYDEBUG(KEYDEBUG_IPSEC_DATA,
printf("*** objects\n");
kdebug_secpolicyindex(spidx));
__LIST_FOREACH(sp, &sptree[dir], chain) {
KEYDEBUG(KEYDEBUG_IPSEC_DATA,
printf("*** in SPD\n");
kdebug_secpolicyindex(&sp->spidx));
if (sp->state == IPSEC_SPSTATE_DEAD)
continue;
if (key_cmpspidx_withmask(&sp->spidx, spidx))
goto found;
}
splx(s);
return NULL;
found:
/* sanity check */
KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
/* found a SPD entry */
sp->refcnt++;
splx(s);
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
sp->refcnt, sp));
return sp;
}
/*
* allocating a SA entry for a *OUTBOUND* packet.
* checking each request entries in SP, and acquire SA if need.
* OUT: 0: there are valid requests.
* ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
*/
int
key_checkrequest(isr, saidx)
struct ipsecrequest *isr;
struct secasindex *saidx;
{
u_int level;
int error;
/* sanity check */
if (isr == NULL || saidx == NULL)
panic("key_checkrequest: NULL pointer is passed.\n");
/* check mode */
switch (saidx->mode) {
case IPSEC_MODE_TRANSPORT:
case IPSEC_MODE_TUNNEL:
break;
case IPSEC_MODE_ANY:
default:
panic("key_checkrequest: Invalid policy defined.\n");
}
/* get current level */
level = ipsec_get_reqlevel(isr);
/*
* We do allocate new SA only if the state of SA in the holder is
* SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
*/
if (isr->sav != NULL) {
if (isr->sav->sah == NULL)
panic("key_checkrequest: sah is null.\n");
if (isr->sav == (struct secasvar *)LIST_FIRST(
&isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP checkrequest calls free SA:%p\n",
isr->sav));
key_freesav(isr->sav);
isr->sav = NULL;
}
}
/* new SA allocation if no SA found. */
if (isr->sav == NULL)
isr->sav = key_allocsa_policy(saidx);
/* When there is SA. */
if (isr->sav != NULL)
return 0;
/* there is no SA */
if ((error = key_acquire(saidx, &isr->sp->spidx)) != 0) {
/* XXX What I do ? */
printf("key_checkrequest: error %d returned "
"from key_acquire.\n", error);
return error;
}
return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
}
/*
* allocating a SA for policy entry from SAD.
* NOTE: searching SAD of aliving state.
* OUT: NULL: not found.
* others: found and return the pointer.
*/
static struct secasvar *
key_allocsa_policy(saidx)
struct secasindex *saidx;
{
struct secashead *sah;
struct secasvar *sav;
u_int stateidx, state;
__LIST_FOREACH(sah, &sahtree, chain) {
if (sah->state == SADB_SASTATE_DEAD)
continue;
if (key_cmpsaidx_withmode(&sah->saidx, saidx))
goto found;
}
return NULL;
found:
/* search valid state */
for (stateidx = 0;
stateidx < _ARRAYLEN(saorder_state_valid);
stateidx++) {
state = saorder_state_valid[stateidx];
sav = key_do_allocsa_policy(sah, state);
if (sav != NULL)
return sav;
}
return NULL;
}
/*
* searching SAD with direction, protocol, mode and state.
* called by key_allocsa_policy().
* OUT:
* NULL : not found
* others : found, pointer to a SA.
*/
static struct secasvar *
key_do_allocsa_policy(sah, state)
struct secashead *sah;
u_int state;
{
struct secasvar *sav, *candidate;
/* initilize */
candidate = NULL;
__LIST_FOREACH(sav, &sah->savtree[state], chain) {
/* sanity check */
KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
/* initialize */
if (candidate == NULL) {
candidate = sav;
continue;
}
/* Which SA is the better ? */
/* sanity check 2 */
if (candidate->lft_c == NULL || sav->lft_c == NULL)
panic("key_do_allocsa_policy: "
"lifetime_current is NULL.\n");
/* XXX What the best method is to compare ? */
if (candidate->lft_c->sadb_lifetime_addtime >
sav->lft_c->sadb_lifetime_addtime) {
candidate = sav;
continue;
}
}
if (candidate) {
candidate->refcnt++;
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP allocsa_policy cause "
"refcnt++:%d SA:%p\n",
candidate->refcnt, candidate));
}
return candidate;
}
/*
* allocating a SA entry for a *INBOUND* packet.
* Must call key_freesav() later.
* OUT: positive: pointer to a sav.
* NULL: not found, or error occured.
*/
struct secasvar *
key_allocsa(family, src, dst, proto, spi)
u_int family, proto;
caddr_t src, dst;
u_int32_t spi;
{
struct secashead *sah;
struct secasvar *sav;
u_int stateidx, state;
int s;
/* sanity check */
if (src == NULL || dst == NULL)
panic("key_allocsa: NULL pointer is passed.\n");
/*
* searching SAD.
* XXX: to be checked internal IP header somewhere. Also when
* IPsec tunnel packet is received. But ESP tunnel mode is
* encrypted so we can't check internal IP header.
*/
#ifdef __NetBSD__
s = splsoftnet(); /*called from softclock()*/
#else
s = splnet(); /*called from softclock()*/
#endif
__LIST_FOREACH(sah, &sahtree, chain) {
/* search valid state */
for (stateidx = 0;
stateidx < _ARRAYLEN(saorder_state_valid);
stateidx++) {
state = saorder_state_valid[stateidx];
__LIST_FOREACH(sav, &sah->savtree[state], chain) {
/* sanity check */
KEY_CHKSASTATE(sav->state, state, "key_allocsav");
if (proto != sav->sah->saidx.proto)
continue;
if (spi != sav->spi)
continue;
if (key_bbcmp(src,
_INADDRBYSA(&sav->sah->saidx.src),
_INALENBYAF(sav->sah->saidx.src.__ss_family) << 3)
&& key_bbcmp(dst,
_INADDRBYSA(&sav->sah->saidx.dst),
_INALENBYAF(sav->sah->saidx.dst.__ss_family) << 3))
goto found;
}
}
}
/* not found */
splx(s);
return NULL;
found:
sav->refcnt++;
splx(s);
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP allocsa cause refcnt++:%d SA:%p\n",
sav->refcnt, sav));
return sav;
}
/*
* Must be called after calling key_allocsp().
* For both the packet without socket and key_freeso().
*/
void
key_freesp(sp)
struct secpolicy *sp;
{
/* sanity check */
if (sp == NULL)
panic("key_freesp: NULL pointer is passed.\n");
sp->refcnt--;
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP freesp cause refcnt--:%d SP:%p\n",
sp->refcnt, sp));
if (sp->refcnt == 0)
key_delsp(sp);
return;
}
/*
* Must be called after calling key_allocsp().
* For the packet with socket.
*/
void
key_freeso(so)
struct socket *so;
{
/* sanity check */
if (so == NULL)
panic("key_freeso: NULL pointer is passed.\n");
switch (so->so_proto->pr_domain->dom_family) {
#ifdef INET
case PF_INET:
{
struct inpcb *pcb = sotoinpcb(so);
/* Does it have a PCB ? */
if (pcb == NULL)
return;
key_freesp_so(&pcb->inp_sp->sp_in);
key_freesp_so(&pcb->inp_sp->sp_out);
}
break;
#endif
#ifdef INET6
case PF_INET6:
{
struct in6pcb *pcb = sotoin6pcb(so);
/* Does it have a PCB ? */
if (pcb == NULL)
return;
key_freesp_so(&pcb->in6p_sp->sp_in);
key_freesp_so(&pcb->in6p_sp->sp_out);
}
break;
#endif /* INET6 */
default:
printf("key_freeso: unknown address family=%d.\n",
so->so_proto->pr_domain->dom_family);
return;
}
return;
}
static void
key_freesp_so(sp)
struct secpolicy **sp;
{
/* sanity check */
if (sp == NULL || *sp == NULL)
panic("key_freesp_so: sp == NULL\n");
switch ((*sp)->policy) {
case IPSEC_POLICY_IPSEC:
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP freeso calls free SP:%p\n", *sp));
key_freesp(*sp);
*sp = NULL;
break;
case IPSEC_POLICY_ENTRUST:
case IPSEC_POLICY_BYPASS:
return;
default:
panic("key_freesp_so: Invalid policy found %d", (*sp)->policy);
}
return;
}
/*
* Must be called after calling key_allocsa().
* This function is called by key_freesp() to free some SA allocated
* for a policy.
*/
void
key_freesav(sav)
struct secasvar *sav;
{
/* sanity check */
if (sav == NULL)
panic("key_freesav: NULL pointer is passed.\n");
sav->refcnt--;
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP freesav cause refcnt--:%d SA:%p SPI %d\n",
sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
if (sav->refcnt == 0)
key_delsav(sav);
return;
}
/* %%% SPD management */
/*
* free security policy entry.
*/
static void
key_delsp(sp)
struct secpolicy *sp;
{
int s;
/* sanity check */
if (sp == NULL)
panic("key_delsp: NULL pointer is passed.\n");
sp->state = IPSEC_SPSTATE_DEAD;
if (sp->refcnt > 0)
return; /* can't free */
#ifdef __NetBSD__
s = splsoftnet(); /*called from softclock()*/
#else
s = splnet(); /*called from softclock()*/
#endif
/* remove from SP index */
if (__LIST_CHAINED(sp))
LIST_REMOVE(sp, chain);
{
struct ipsecrequest *isr = sp->req, *nextisr;
while (isr != NULL) {
if (isr->sav != NULL) {
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
printf("DP delsp calls free SA:%p\n",
isr->sav));
key_freesav(isr->sav);
isr->sav = NULL;
}
nextisr = isr->next;
KFREE(isr);
isr = nextisr;
}
}
KFREE(sp);
splx(s);
return;
}
/*
* search SPD
* OUT: NULL : not found
* others : found, pointer to a SP.
*/
static struct secpolicy *
key_getsp(spidx)
struct secpolicyindex *spidx;
{
struct secpolicy *sp;
/* sanity check */
if (spidx == NULL)
panic("key_getsp: NULL pointer is passed.\n");
__LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
if (sp->state == IPSEC_SPSTATE_DEAD)
continue;
if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
sp->refcnt++;
return sp;
}
}
return NULL;
}
struct secpolicy *
key_newsp()
{
struct secpolicy *newsp = NULL;
KMALLOC(newsp, struct secpolicy *, sizeof(*newsp));
if (newsp == NULL) {
printf("key_newsp: No more memory.\n");
return NULL;
}
bzero(newsp, sizeof(*newsp));
newsp->refcnt = 1;
newsp->req = NULL;
return newsp;
}
/*
* create secpolicy structure from sadb_x_policy structure.
* NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
* so must be set properly later.
*/
struct secpolicy *
key_msg2sp(xpl0, error)
struct sadb_x_policy *xpl0;
int *error;
{
struct secpolicy *newsp;
/* sanity check */
if (xpl0 == NULL)
panic("key_msg2sp: NULL pointer was passed.\n");
if ((newsp = key_newsp()) == NULL) {
*error = ENOBUFS;
return NULL;
}
newsp->spidx.dir = xpl0->sadb_x_policy_dir;
newsp->policy = xpl0->sadb_x_policy_type;
/* check policy */
switch (xpl0->sadb_x_policy_type) {
case IPSEC_POLICY_DISCARD:
case IPSEC_POLICY_NONE:
case IPSEC_POLICY_ENTRUST:
case IPSEC_POLICY_BYPASS:
newsp->req = NULL;
break;
case IPSEC_POLICY_IPSEC:
{
int tlen;
struct sadb_x_ipsecrequest *xisr;
struct ipsecrequest **p_isr = &newsp->req;
/* validity check */
if (PFKEY_EXTLEN(xpl0) <= sizeof(*xpl0)) {
printf("key_msg2sp: Invalid msg length.\n");
key_freesp(newsp);
*error = EINVAL;
return NULL;
}
tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
while (tlen > 0) {
/* length check */
if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
printf("key_msg2sp: "
"invalid ipsecrequest length.\n");
key_freesp(newsp);
*error = EINVAL;
return NULL;
}
/* allocate request buffer */
KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
if ((*p_isr) == NULL) {
printf("key_msg2sp: No more memory.\n");
key_freesp(newsp);
*error = ENOBUFS;
return NULL;
}
bzero(*p_isr, sizeof(**p_isr));
/* set values */
(*p_isr)->next = NULL;
switch (xisr->sadb_x_ipsecrequest_proto) {
case IPPROTO_ESP:
case IPPROTO_AH:
#if 1 /*nonstandard*/
case IPPROTO_IPCOMP:
#endif
break;
default:
printf("key_msg2sp: invalid proto type=%u\n",
xisr->sadb_x_ipsecrequest_proto);
key_freesp(newsp);
*error = EPROTONOSUPPORT;
return NULL;
}
(*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
switch (xisr->sadb_x_ipsecrequest_mode) {
case IPSEC_MODE_TRANSPORT:
case IPSEC_MODE_TUNNEL:
break;
case IPSEC_MODE_ANY:
default:
printf("key_msg2sp: invalid mode=%u\n",
xisr->sadb_x_ipsecrequest_mode);
key_freesp(newsp);
*error = EINVAL;
return NULL;
}
(*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
switch (xisr->sadb_x_ipsecrequest_level) {
case IPSEC_LEVEL_DEFAULT:
case IPSEC_LEVEL_USE:
case IPSEC_LEVEL_REQUIRE:
break;
case IPSEC_LEVEL_UNIQUE:
/* validity check */
/*
* If range violation of reqid, kernel will
* update it, don't refuse it.
*/
if (xisr->sadb_x_ipsecrequest_reqid
> IPSEC_MANUAL_REQID_MAX) {
printf("key_msg2sp: reqid=%d "
"range violation, "
"updated by kernel.\n",
xisr->sadb_x_ipsecrequest_reqid);
xisr->sadb_x_ipsecrequest_reqid = 0;
}
/* allocate new reqid id if reqid is zero. */
if (xisr->sadb_x_ipsecrequest_reqid == 0) {
u_int32_t reqid;
if ((reqid = key_newreqid()) == 0) {
key_freesp(newsp);
*error = ENOBUFS;