1 /* $FreeBSD: src/lib/libipsec/pfkey.c,v 1.1.2.2 2001/07/03 11:01:14 ume Exp $ */
2 /* $KAME: pfkey.c,v 1.39 2001/03/05 18:22:17 thorpej Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <net/pfkeyv2.h>
38 #include <netkey/key_var.h>
39 #include <netinet/in.h>
40 #include <netinet6/ipsec.h>
48 #include "ipsec_strerror.h"
53 #define CALLOC(size, cast) (cast)calloc(1, (size))
55 static int findsupportedmap
__P((int));
56 static int setsupportedmap
__P((struct sadb_supported
*));
57 static struct sadb_alg
*findsupportedalg
__P((u_int
, u_int
));
58 static int pfkey_send_x1
__P((int, u_int
, u_int
, u_int
, struct sockaddr
*,
59 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
60 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
61 u_int32_t
, u_int32_t
, u_int32_t
, u_int16_t
));
62 static int pfkey_send_x2
__P((int, u_int
, u_int
, u_int
,
63 struct sockaddr
*, struct sockaddr
*, u_int32_t
));
64 static int pfkey_send_x3
__P((int, u_int
, u_int
));
65 static int pfkey_send_x4
__P((int, u_int
, struct sockaddr
*, u_int
,
66 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
67 char *, int, u_int32_t
));
68 static int pfkey_send_x5
__P((int, u_int
, u_int32_t
));
70 static caddr_t pfkey_setsadbmsg
__P((caddr_t
, caddr_t
, u_int
, u_int
,
71 u_int
, u_int32_t
, pid_t
));
72 static caddr_t pfkey_setsadbsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int
,
73 u_int
, u_int
, u_int32_t
, u_int16_t
));
74 static caddr_t pfkey_setsadbaddr
__P((caddr_t
, caddr_t
, u_int
,
75 struct sockaddr
*, u_int
, u_int
));
76 static caddr_t pfkey_setsadbkey
__P((caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
));
77 static caddr_t pfkey_setsadblifetime
__P((caddr_t
, caddr_t
, u_int
, u_int32_t
,
78 u_int32_t
, u_int32_t
, u_int32_t
));
79 static caddr_t pfkey_setsadbxsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int32_t
));
82 * make and search supported algorithm structure.
84 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
86 static int supported_map
[] = {
93 findsupportedmap(satype
)
98 for (i
= 0; i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
99 if (supported_map
[i
] == satype
)
104 static struct sadb_alg
*
105 findsupportedalg(satype
, alg_id
)
106 u_int satype
, alg_id
;
113 algno
= findsupportedmap(satype
);
115 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
118 if (ipsec_supported
[algno
] == NULL
) {
119 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
123 tlen
= ipsec_supported
[algno
]->sadb_supported_len
124 - sizeof(struct sadb_supported
);
125 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
127 if (tlen
< sizeof(struct sadb_alg
)) {
131 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
132 return (struct sadb_alg
*)p
;
134 tlen
-= sizeof(struct sadb_alg
);
135 p
+= sizeof(struct sadb_alg
);
138 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
144 struct sadb_supported
*sup
;
146 struct sadb_supported
**ipsup
;
148 switch (sup
->sadb_supported_exttype
) {
149 case SADB_EXT_SUPPORTED_AUTH
:
150 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
152 case SADB_EXT_SUPPORTED_ENCRYPT
:
153 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
156 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
163 *ipsup
= malloc(sup
->sadb_supported_len
);
165 __ipsec_set_strerror(strerror(errno
));
168 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
174 * check key length against algorithm specified.
175 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
176 * augument, and only calls to ipsec_check_keylen2();
177 * keylen is the unit of bit.
183 ipsec_check_keylen(supported
, alg_id
, keylen
)
192 case SADB_EXT_SUPPORTED_AUTH
:
193 satype
= SADB_SATYPE_AH
;
195 case SADB_EXT_SUPPORTED_ENCRYPT
:
196 satype
= SADB_SATYPE_ESP
;
199 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
203 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
207 * check key length against algorithm specified.
208 * satype is one of satype defined at pfkeyv2.h.
209 * keylen is the unit of bit.
215 ipsec_check_keylen2(satype
, alg_id
, keylen
)
220 struct sadb_alg
*alg
;
222 alg
= findsupportedalg(satype
, alg_id
);
226 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
227 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
231 __ipsec_errcode
= EIPSEC_NO_ERROR
;
236 * get max/min key length against algorithm specified.
237 * satype is one of satype defined at pfkeyv2.h.
238 * keylen is the unit of bit.
244 ipsec_get_keylen(supported
, alg_id
, alg0
)
245 u_int supported
, alg_id
;
246 struct sadb_alg
*alg0
;
248 struct sadb_alg
*alg
;
253 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
258 case SADB_EXT_SUPPORTED_AUTH
:
259 satype
= SADB_SATYPE_AH
;
261 case SADB_EXT_SUPPORTED_ENCRYPT
:
262 satype
= SADB_SATYPE_ESP
;
265 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
269 alg
= findsupportedalg(satype
, alg_id
);
273 memcpy(alg0
, alg
, sizeof(*alg0
));
275 __ipsec_errcode
= EIPSEC_NO_ERROR
;
280 * set the rate for SOFT lifetime against HARD one.
281 * If rate is more than 100 or equal to zero, then set to 100.
283 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
284 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
285 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
286 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
289 pfkey_set_softrate(type
, rate
)
292 __ipsec_errcode
= EIPSEC_NO_ERROR
;
294 if (rate
> 100 || rate
== 0)
298 case SADB_X_LIFETIME_ALLOCATIONS
:
299 soft_lifetime_allocations_rate
= rate
;
301 case SADB_X_LIFETIME_BYTES
:
302 soft_lifetime_bytes_rate
= rate
;
304 case SADB_X_LIFETIME_ADDTIME
:
305 soft_lifetime_addtime_rate
= rate
;
307 case SADB_X_LIFETIME_USETIME
:
308 soft_lifetime_usetime_rate
= rate
;
312 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
317 * get current rate for SOFT lifetime against HARD one.
318 * ATTENTION: ~0 is returned if invalid type was passed.
321 pfkey_get_softrate(type
)
325 case SADB_X_LIFETIME_ALLOCATIONS
:
326 return soft_lifetime_allocations_rate
;
327 case SADB_X_LIFETIME_BYTES
:
328 return soft_lifetime_bytes_rate
;
329 case SADB_X_LIFETIME_ADDTIME
:
330 return soft_lifetime_addtime_rate
;
331 case SADB_X_LIFETIME_USETIME
:
332 return soft_lifetime_usetime_rate
;
339 * sending SADB_GETSPI message to the kernel.
341 * positive: success and return length sent.
342 * -1 : error occured, and set errno.
345 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
348 struct sockaddr
*src
, *dst
;
349 u_int32_t min
, max
, reqid
, seq
;
351 struct sadb_msg
*newmsg
;
354 int need_spirange
= 0;
359 if (src
== NULL
|| dst
== NULL
) {
360 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
363 if (src
->sa_family
!= dst
->sa_family
) {
364 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
367 if (min
> max
|| (min
> 0 && min
<= 255)) {
368 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
371 switch (src
->sa_family
) {
373 plen
= sizeof(struct in_addr
) << 3;
376 plen
= sizeof(struct in6_addr
) << 3;
379 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
383 /* create new sadb_msg to send. */
384 len
= sizeof(struct sadb_msg
)
385 + sizeof(struct sadb_x_sa2
)
386 + sizeof(struct sadb_address
)
387 + PFKEY_ALIGN8(src
->sa_len
)
388 + sizeof(struct sadb_address
)
389 + PFKEY_ALIGN8(dst
->sa_len
);
391 if (min
> 255 && max
< ~0) {
393 len
+= sizeof(struct sadb_spirange
);
396 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
397 __ipsec_set_strerror(strerror(errno
));
400 ep
= ((caddr_t
)newmsg
) + len
;
402 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
403 len
, satype
, seq
, getpid());
409 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
415 /* set sadb_address for source */
416 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
423 /* set sadb_address for destination */
424 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
431 /* proccessing spi range */
433 struct sadb_spirange spirange
;
435 if (p
+ sizeof(spirange
) > ep
) {
440 memset(&spirange
, 0, sizeof(spirange
));
441 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
442 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
443 spirange
.sadb_spirange_min
= min
;
444 spirange
.sadb_spirange_max
= max
;
446 memcpy(p
, &spirange
, sizeof(spirange
));
448 p
+= sizeof(spirange
);
456 len
= pfkey_send(so
, newmsg
, len
);
462 __ipsec_errcode
= EIPSEC_NO_ERROR
;
467 * sending SADB_UPDATE message to the kernel.
468 * The length of key material is a_keylen + e_keylen.
470 * positive: success and return length sent.
471 * -1 : error occured, and set errno.
474 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
475 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
476 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
, port
)
478 u_int satype
, mode
, wsize
;
479 struct sockaddr
*src
, *dst
;
480 u_int32_t spi
, reqid
;
482 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
484 u_int64_t l_bytes
, l_addtime
, l_usetime
;
489 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
491 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
492 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
, port
)) < 0)
499 * sending SADB_ADD message to the kernel.
500 * The length of key material is a_keylen + e_keylen.
502 * positive: success and return length sent.
503 * -1 : error occured, and set errno.
506 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
507 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
508 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
, port
)
510 u_int satype
, mode
, wsize
;
511 struct sockaddr
*src
, *dst
;
512 u_int32_t spi
, reqid
;
514 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
516 u_int64_t l_bytes
, l_addtime
, l_usetime
;
521 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
523 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
524 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
, port
)) < 0)
531 * sending SADB_DELETE message to the kernel.
533 * positive: success and return length sent.
534 * -1 : error occured, and set errno.
537 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
540 struct sockaddr
*src
, *dst
;
544 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
551 * sending SADB_DELETE without spi to the kernel. This is
552 * the "delete all" request (an extension also present in
556 * positive: success and return length sent
557 * -1 : error occured, and set errno
560 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
563 struct sockaddr
*src
, *dst
;
565 struct sadb_msg
*newmsg
;
572 if (src
== NULL
|| dst
== NULL
) {
573 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
576 if (src
->sa_family
!= dst
->sa_family
) {
577 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
580 switch (src
->sa_family
) {
582 plen
= sizeof(struct in_addr
) << 3;
585 plen
= sizeof(struct in6_addr
) << 3;
588 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
592 /* create new sadb_msg to reply. */
593 len
= sizeof(struct sadb_msg
)
594 + sizeof(struct sadb_address
)
595 + PFKEY_ALIGN8(src
->sa_len
)
596 + sizeof(struct sadb_address
)
597 + PFKEY_ALIGN8(dst
->sa_len
);
599 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
600 __ipsec_set_strerror(strerror(errno
));
603 ep
= ((caddr_t
)newmsg
) + len
;
605 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
611 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
617 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
625 len
= pfkey_send(so
, newmsg
, len
);
631 __ipsec_errcode
= EIPSEC_NO_ERROR
;
636 * sending SADB_GET message to the kernel.
638 * positive: success and return length sent.
639 * -1 : error occured, and set errno.
642 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
645 struct sockaddr
*src
, *dst
;
649 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
656 * sending SADB_REGISTER message to the kernel.
658 * positive: success and return length sent.
659 * -1 : error occured, and set errno.
662 pfkey_send_register(so
, satype
)
668 if (satype
== PF_UNSPEC
) {
670 algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
672 if (ipsec_supported
[algno
]) {
673 free(ipsec_supported
[algno
]);
674 ipsec_supported
[algno
] = NULL
;
678 algno
= findsupportedmap(satype
);
680 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
684 if (ipsec_supported
[algno
]) {
685 free(ipsec_supported
[algno
]);
686 ipsec_supported
[algno
] = NULL
;
690 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
697 * receiving SADB_REGISTER message from the kernel, and copy buffer for
698 * sadb_supported returned into ipsec_supported.
700 * 0: success and return length sent.
701 * -1: error occured, and set errno.
704 pfkey_recv_register(so
)
707 pid_t pid
= getpid();
708 struct sadb_msg
*newmsg
;
711 /* receive message */
713 if ((newmsg
= pfkey_recv(so
)) == NULL
)
715 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
716 || newmsg
->sadb_msg_pid
!= pid
);
719 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
721 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
725 __ipsec_errcode
= EIPSEC_NO_ERROR
;
731 * receiving SADB_REGISTER message from the kernel, and copy buffer for
732 * sadb_supported returned into ipsec_supported.
733 * NOTE: sadb_msg_len must be host order.
735 * tlen: msg length, it's to makeing sure.
737 * 0: success and return length sent.
738 * -1: error occured, and set errno.
741 pfkey_set_supported(msg
, tlen
)
742 struct sadb_msg
*msg
;
745 struct sadb_supported
*sup
;
750 if (msg
->sadb_msg_len
!= tlen
) {
751 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
758 p
+= sizeof(struct sadb_msg
);
761 sup
= (struct sadb_supported
*)p
;
762 if (ep
< p
+ sizeof(*sup
) ||
763 PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
764 ep
< p
+ sup
->sadb_supported_len
) {
769 switch (sup
->sadb_supported_exttype
) {
770 case SADB_EXT_SUPPORTED_AUTH
:
771 case SADB_EXT_SUPPORTED_ENCRYPT
:
774 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
779 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
781 /* set supported map */
782 if (setsupportedmap(sup
) != 0)
785 p
+= sup
->sadb_supported_len
;
789 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
793 __ipsec_errcode
= EIPSEC_NO_ERROR
;
799 * sending SADB_FLUSH message to the kernel.
801 * positive: success and return length sent.
802 * -1 : error occured, and set errno.
805 pfkey_send_flush(so
, satype
)
811 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
818 * sending SADB_DUMP message to the kernel.
820 * positive: success and return length sent.
821 * -1 : error occured, and set errno.
824 pfkey_send_dump(so
, satype
)
830 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
837 * sending SADB_X_PROMISC message to the kernel.
838 * NOTE that this function handles promisc mode toggle only.
840 * flag: set promisc off if zero, set promisc on if non-zero.
842 * positive: success and return length sent.
843 * -1 : error occured, and set errno.
844 * 0 : error occured, and set errno.
845 * others: a pointer to new allocated buffer in which supported
849 pfkey_send_promisc_toggle(so
, flag
)
855 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
862 * sending SADB_X_SPDADD message to the kernel.
864 * positive: success and return length sent.
865 * -1 : error occured, and set errno.
868 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
870 struct sockaddr
*src
, *dst
;
871 u_int prefs
, prefd
, proto
;
878 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
879 src
, prefs
, dst
, prefd
, proto
,
881 policy
, policylen
, seq
)) < 0)
888 * sending SADB_X_SPDADD message to the kernel.
890 * positive: success and return length sent.
891 * -1 : error occured, and set errno.
894 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
895 policy
, policylen
, seq
)
897 struct sockaddr
*src
, *dst
;
898 u_int prefs
, prefd
, proto
;
899 u_int64_t ltime
, vtime
;
906 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
907 src
, prefs
, dst
, prefd
, proto
,
909 policy
, policylen
, seq
)) < 0)
916 * sending SADB_X_SPDUPDATE message to the kernel.
918 * positive: success and return length sent.
919 * -1 : error occured, and set errno.
922 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
924 struct sockaddr
*src
, *dst
;
925 u_int prefs
, prefd
, proto
;
932 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
933 src
, prefs
, dst
, prefd
, proto
,
935 policy
, policylen
, seq
)) < 0)
942 * sending SADB_X_SPDUPDATE message to the kernel.
944 * positive: success and return length sent.
945 * -1 : error occured, and set errno.
948 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
949 policy
, policylen
, seq
)
951 struct sockaddr
*src
, *dst
;
952 u_int prefs
, prefd
, proto
;
953 u_int64_t ltime
, vtime
;
960 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
961 src
, prefs
, dst
, prefd
, proto
,
963 policy
, policylen
, seq
)) < 0)
970 * sending SADB_X_SPDDELETE message to the kernel.
972 * positive: success and return length sent.
973 * -1 : error occured, and set errno.
976 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
978 struct sockaddr
*src
, *dst
;
979 u_int prefs
, prefd
, proto
;
986 if (policylen
!= sizeof(struct sadb_x_policy
)) {
987 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
991 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
992 src
, prefs
, dst
, prefd
, proto
,
994 policy
, policylen
, seq
)) < 0)
1001 * sending SADB_X_SPDDELETE message to the kernel.
1003 * positive: success and return length sent.
1004 * -1 : error occured, and set errno.
1007 pfkey_send_spddelete2(so
, spid
)
1013 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1020 * sending SADB_X_SPDGET message to the kernel.
1022 * positive: success and return length sent.
1023 * -1 : error occured, and set errno.
1026 pfkey_send_spdget(so
, spid
)
1032 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1039 * sending SADB_X_SPDSETIDX message to the kernel.
1041 * positive: success and return length sent.
1042 * -1 : error occured, and set errno.
1045 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1047 struct sockaddr
*src
, *dst
;
1048 u_int prefs
, prefd
, proto
;
1055 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1056 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1060 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1061 src
, prefs
, dst
, prefd
, proto
,
1063 policy
, policylen
, seq
)) < 0)
1070 * sending SADB_SPDFLUSH message to the kernel.
1072 * positive: success and return length sent.
1073 * -1 : error occured, and set errno.
1076 pfkey_send_spdflush(so
)
1081 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1088 * sending SADB_SPDDUMP message to the kernel.
1090 * positive: success and return length sent.
1091 * -1 : error occured, and set errno.
1094 pfkey_send_spddump(so
)
1099 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1105 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1107 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1108 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1109 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
, port
)
1111 u_int type
, satype
, mode
;
1112 struct sockaddr
*src
, *dst
;
1113 u_int32_t spi
, reqid
;
1116 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1117 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1120 struct sadb_msg
*newmsg
;
1126 /* validity check */
1127 if (src
== NULL
|| dst
== NULL
) {
1128 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1131 if (src
->sa_family
!= dst
->sa_family
) {
1132 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1135 switch (src
->sa_family
) {
1137 plen
= sizeof(struct in_addr
) << 3;
1140 plen
= sizeof(struct in6_addr
) << 3;
1143 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1148 case SADB_SATYPE_ESP
:
1149 if (e_type
== SADB_EALG_NONE
) {
1150 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1154 case SADB_SATYPE_AH
:
1155 if (e_type
!= SADB_EALG_NONE
) {
1156 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1159 if (a_type
== SADB_AALG_NONE
) {
1160 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1164 case SADB_X_SATYPE_IPCOMP
:
1165 if (e_type
== SADB_X_CALG_NONE
) {
1166 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1169 if (a_type
!= SADB_AALG_NONE
) {
1170 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1175 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1179 /* create new sadb_msg to reply. */
1180 len
= sizeof(struct sadb_msg
)
1181 + sizeof(struct sadb_sa_2
)
1182 + sizeof(struct sadb_x_sa2
)
1183 + sizeof(struct sadb_address
)
1184 + PFKEY_ALIGN8(src
->sa_len
)
1185 + sizeof(struct sadb_address
)
1186 + PFKEY_ALIGN8(dst
->sa_len
)
1187 + sizeof(struct sadb_lifetime
)
1188 + sizeof(struct sadb_lifetime
);
1190 if (e_type
!= SADB_EALG_NONE
)
1191 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1192 if (a_type
!= SADB_AALG_NONE
)
1193 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1195 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1196 __ipsec_set_strerror(strerror(errno
));
1199 ep
= ((caddr_t
)newmsg
) + len
;
1201 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1202 satype
, seq
, getpid());
1207 p
= pfkey_setsadbsa2(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
, port
);
1212 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1217 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1223 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1230 if (e_type
!= SADB_EALG_NONE
) {
1231 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1238 if (a_type
!= SADB_AALG_NONE
) {
1239 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1240 keymat
+ e_keylen
, a_keylen
);
1247 /* set sadb_lifetime for destination */
1248 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1249 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1254 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1255 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1256 if (!p
|| p
!= ep
) {
1262 len
= pfkey_send(so
, newmsg
, len
);
1268 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1272 /* sending SADB_DELETE or SADB_GET message to the kernel */
1274 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1276 u_int type
, satype
, mode
;
1277 struct sockaddr
*src
, *dst
;
1280 struct sadb_msg
*newmsg
;
1286 /* validity check */
1287 if (src
== NULL
|| dst
== NULL
) {
1288 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1291 if (src
->sa_family
!= dst
->sa_family
) {
1292 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1295 switch (src
->sa_family
) {
1297 plen
= sizeof(struct in_addr
) << 3;
1300 plen
= sizeof(struct in6_addr
) << 3;
1303 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1307 /* create new sadb_msg to reply. */
1308 len
= sizeof(struct sadb_msg
)
1309 + sizeof(struct sadb_sa_2
)
1310 + sizeof(struct sadb_address
)
1311 + PFKEY_ALIGN8(src
->sa_len
)
1312 + sizeof(struct sadb_address
)
1313 + PFKEY_ALIGN8(dst
->sa_len
);
1315 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1316 __ipsec_set_strerror(strerror(errno
));
1319 ep
= ((caddr_t
)newmsg
) + len
;
1321 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1327 p
= pfkey_setsadbsa2(p
, ep
, spi
, 0, 0, 0, 0, 0);
1332 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1338 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1340 if (!p
|| p
!= ep
) {
1346 len
= pfkey_send(so
, newmsg
, len
);
1352 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1357 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1361 pfkey_send_x3(so
, type
, satype
)
1365 struct sadb_msg
*newmsg
;
1370 /* validity check */
1372 case SADB_X_PROMISC
:
1373 if (satype
!= 0 && satype
!= 1) {
1374 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1380 case SADB_SATYPE_UNSPEC
:
1381 case SADB_SATYPE_AH
:
1382 case SADB_SATYPE_ESP
:
1383 case SADB_X_SATYPE_IPCOMP
:
1386 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1391 /* create new sadb_msg to send. */
1392 len
= sizeof(struct sadb_msg
);
1394 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1395 __ipsec_set_strerror(strerror(errno
));
1398 ep
= ((caddr_t
)newmsg
) + len
;
1400 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1402 if (!p
|| p
!= ep
) {
1408 len
= pfkey_send(so
, newmsg
, len
);
1414 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1418 /* sending SADB_X_SPDADD message to the kernel */
1420 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1421 ltime
, vtime
, policy
, policylen
, seq
)
1423 struct sockaddr
*src
, *dst
;
1424 u_int type
, prefs
, prefd
, proto
;
1425 u_int64_t ltime
, vtime
;
1430 struct sadb_msg
*newmsg
;
1436 /* validity check */
1437 if (src
== NULL
|| dst
== NULL
) {
1438 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1441 if (src
->sa_family
!= dst
->sa_family
) {
1442 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1446 switch (src
->sa_family
) {
1448 plen
= sizeof(struct in_addr
) << 3;
1451 plen
= sizeof(struct in6_addr
) << 3;
1454 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1457 if (prefs
> plen
|| prefd
> plen
) {
1458 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1462 /* create new sadb_msg to reply. */
1463 len
= sizeof(struct sadb_msg
)
1464 + sizeof(struct sadb_address
)
1465 + PFKEY_ALIGN8(src
->sa_len
)
1466 + sizeof(struct sadb_address
)
1467 + PFKEY_ALIGN8(src
->sa_len
)
1468 + sizeof(struct sadb_lifetime
)
1471 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1472 __ipsec_set_strerror(strerror(errno
));
1475 ep
= ((caddr_t
)newmsg
) + len
;
1477 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1478 SADB_SATYPE_UNSPEC
, seq
, getpid());
1483 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1488 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1493 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1494 0, 0, ltime
, vtime
);
1495 if (!p
|| p
+ policylen
!= ep
) {
1499 memcpy(p
, policy
, policylen
);
1502 len
= pfkey_send(so
, newmsg
, len
);
1508 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1512 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1514 pfkey_send_x5(so
, type
, spid
)
1519 struct sadb_msg
*newmsg
;
1520 struct sadb_x_policy xpl
;
1525 /* create new sadb_msg to reply. */
1526 len
= sizeof(struct sadb_msg
)
1529 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1530 __ipsec_set_strerror(strerror(errno
));
1533 ep
= ((caddr_t
)newmsg
) + len
;
1535 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1536 SADB_SATYPE_UNSPEC
, 0, getpid());
1542 if (p
+ sizeof(xpl
) != ep
) {
1546 memset(&xpl
, 0, sizeof(xpl
));
1547 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1548 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1549 xpl
.sadb_x_policy_id
= spid
;
1550 memcpy(p
, &xpl
, sizeof(xpl
));
1553 len
= pfkey_send(so
, newmsg
, len
);
1559 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1567 * others : success and return value of socket.
1573 int bufsiz
= 0; /* Max allowed by default */
1574 const unsigned long newbufk
= 512;
1575 unsigned long oldmax
;
1576 size_t oldmaxsize
= sizeof(oldmax
);
1577 unsigned long newmax
= newbufk
* (1024 + 128);
1579 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1580 __ipsec_set_strerror(strerror(errno
));
1585 * This is a temporary workaround for KAME PR 154.
1586 * Don't really care even if it fails.
1588 if (sysctlbyname("kern.ipc.maxsockbuf", &oldmax
, &oldmaxsize
, &newmax
, sizeof(newmax
)) != 0) {
1589 plog(LLV_WARNING
, LOCATION
, NULL
,
1590 "sysctlbyname kern.ipc.maxsockbuf failed: %s\n", strerror(errno
));
1591 bufsiz
= 233016; /* Max allowed by default */
1595 bufsiz
= newbufk
* 1024;
1597 if (setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
)) != 0)
1598 plog(LLV_ERROR
, LOCATION
, NULL
,
1599 "setsockopt SOL_SOCKET SO_SNDBUF failed: %s\n", strerror(errno
));
1600 if (setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
)) != 0)
1601 plog(LLV_ERROR
, LOCATION
, NULL
,
1602 "setsockopt SOL_SOCKET SO_RCVBUF failed: %s\n", strerror(errno
));
1604 if (bufsiz
== newbufk
* 1024)
1605 if (sysctlbyname("kern.ipc.maxsockbuf", NULL
, NULL
, &oldmax
, oldmaxsize
) != 0)
1606 plog(LLV_WARNING
, LOCATION
, NULL
,
1607 "sysctlbyname kern.ipc.maxsockbuf (restore) failed: %s\n", strerror(errno
));
1609 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1625 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1630 * receive sadb_msg data, and return pointer to new buffer allocated.
1631 * Must free this buffer later.
1633 * NULL : error occured.
1634 * others : a pointer to sadb_msg structure.
1636 * XXX should be rewritten to pass length explicitly
1642 struct sadb_msg buf
, *newmsg
;
1645 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1648 __ipsec_set_strerror(strerror(errno
));
1652 if (len
< sizeof(buf
)) {
1653 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1654 __ipsec_errcode
= EIPSEC_MAX
;
1658 /* read real message */
1659 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1660 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1661 __ipsec_set_strerror(strerror(errno
));
1665 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1668 __ipsec_set_strerror(strerror(errno
));
1673 if (len
!= reallen
) {
1674 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1679 /* don't trust what the kernel says, validate! */
1680 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1681 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1686 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1691 * send message to a socket.
1693 * others: success and return length sent.
1697 pfkey_send(so
, msg
, len
)
1699 struct sadb_msg
*msg
;
1702 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1703 __ipsec_set_strerror(strerror(errno
));
1707 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1713 * NOTE: These functions are derived from netkey/key.c in KAME.
1716 * set the pointer to each header in this message buffer.
1717 * IN: msg: pointer to message buffer.
1718 * mhp: pointer to the buffer initialized like below:
1719 * caddr_t mhp[SADB_EXT_MAX + 1];
1723 * XXX should be rewritten to obtain length explicitly
1726 pfkey_align(msg
, mhp
)
1727 struct sadb_msg
*msg
;
1730 struct sadb_ext
*ext
;
1733 caddr_t ep
; /* XXX should be passed from upper layer */
1735 /* validity check */
1736 if (msg
== NULL
|| mhp
== NULL
) {
1737 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1742 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1745 mhp
[0] = (caddr_t
)msg
;
1749 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1751 /* skip base header */
1752 p
+= sizeof(struct sadb_msg
);
1755 ext
= (struct sadb_ext
*)p
;
1756 if (ep
< p
+ sizeof(*ext
) || PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1757 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1758 /* invalid format */
1762 /* duplicate check */
1763 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1764 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1765 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1770 switch (ext
->sadb_ext_type
) {
1772 case SADB_EXT_LIFETIME_CURRENT
:
1773 case SADB_EXT_LIFETIME_HARD
:
1774 case SADB_EXT_LIFETIME_SOFT
:
1775 case SADB_EXT_ADDRESS_SRC
:
1776 case SADB_EXT_ADDRESS_DST
:
1777 case SADB_EXT_ADDRESS_PROXY
:
1778 case SADB_EXT_KEY_AUTH
:
1779 /* XXX should to be check weak keys. */
1780 case SADB_EXT_KEY_ENCRYPT
:
1781 /* XXX should to be check weak keys. */
1782 case SADB_EXT_IDENTITY_SRC
:
1783 case SADB_EXT_IDENTITY_DST
:
1784 case SADB_EXT_SENSITIVITY
:
1785 case SADB_EXT_PROPOSAL
:
1786 case SADB_EXT_SUPPORTED_AUTH
:
1787 case SADB_EXT_SUPPORTED_ENCRYPT
:
1788 case SADB_EXT_SPIRANGE
:
1789 case SADB_X_EXT_POLICY
:
1790 case SADB_X_EXT_SA2
:
1791 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1794 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1798 p
+= PFKEY_EXTLEN(ext
);
1802 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1806 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1811 * check basic usage for sadb_msg,
1812 * NOTE: This routine is derived from netkey/key.c in KAME.
1813 * IN: msg: pointer to message buffer.
1814 * mhp: pointer to the buffer initialized like below:
1816 * caddr_t mhp[SADB_EXT_MAX + 1];
1825 struct sadb_msg
*msg
;
1827 /* validity check */
1828 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1829 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1833 msg
= (struct sadb_msg
*)mhp
[0];
1836 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1837 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1842 if (msg
->sadb_msg_type
> SADB_MAX
) {
1843 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1848 switch (msg
->sadb_msg_satype
) {
1849 case SADB_SATYPE_UNSPEC
:
1850 switch (msg
->sadb_msg_type
) {
1858 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1862 case SADB_SATYPE_ESP
:
1863 case SADB_SATYPE_AH
:
1864 case SADB_X_SATYPE_IPCOMP
:
1865 switch (msg
->sadb_msg_type
) {
1867 case SADB_X_SPDDELETE
:
1869 case SADB_X_SPDDUMP
:
1870 case SADB_X_SPDFLUSH
:
1871 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1875 case SADB_SATYPE_RSVP
:
1876 case SADB_SATYPE_OSPFV2
:
1877 case SADB_SATYPE_RIPV2
:
1878 case SADB_SATYPE_MIP
:
1879 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1881 case 1: /* XXX: What does it do ? */
1882 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1886 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1890 /* check field of upper layer protocol and address family */
1891 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1892 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1893 struct sadb_address
*src0
, *dst0
;
1895 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1896 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1898 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1899 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1903 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1904 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1905 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1909 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1914 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1919 * prefixlen == 0 is valid because there must be the case
1920 * all addresses are matched.
1924 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1929 * set data into sadb_msg.
1930 * `buf' must has been allocated sufficiently.
1933 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1944 p
= (struct sadb_msg
*)buf
;
1945 len
= sizeof(struct sadb_msg
);
1947 if (buf
+ len
> lim
)
1951 p
->sadb_msg_version
= PF_KEY_V2
;
1952 p
->sadb_msg_type
= type
;
1953 p
->sadb_msg_errno
= 0;
1954 p
->sadb_msg_satype
= satype
;
1955 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1956 p
->sadb_msg_reserved
= 0;
1957 p
->sadb_msg_seq
= seq
;
1958 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1964 * copy secasvar data into sadb_address.
1965 * `buf' must has been allocated sufficiently.
1968 pfkey_setsadbsa2(buf
, lim
, spi
, wsize
, auth
, enc
, flags
, port
)
1971 u_int32_t spi
, flags
;
1972 u_int wsize
, auth
, enc
;
1975 struct sadb_sa_2
*p
;
1978 p
= (struct sadb_sa_2
*)buf
;
1979 len
= sizeof(struct sadb_sa_2
);
1981 if (buf
+ len
> lim
)
1985 p
->sa
.sadb_sa_len
= PFKEY_UNIT64(len
);
1986 p
->sa
.sadb_sa_exttype
= SADB_EXT_SA
;
1987 p
->sa
.sadb_sa_spi
= spi
;
1988 p
->sa
.sadb_sa_replay
= wsize
;
1989 p
->sa
.sadb_sa_state
= SADB_SASTATE_LARVAL
;
1990 p
->sa
.sadb_sa_auth
= auth
;
1991 p
->sa
.sadb_sa_encrypt
= enc
;
1992 p
->sa
.sadb_sa_flags
= flags
;
1993 p
->sadb_sa_natt_port
= port
;
1995 printf("pfkey_setsadbsa2: flags = 0x%X, port = %u.\n", flags
, ntohs(port
));
2001 * set data into sadb_address.
2002 * `buf' must has been allocated sufficiently.
2003 * prefixlen is in bits.
2006 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
2010 struct sockaddr
*saddr
;
2014 struct sadb_address
*p
;
2017 p
= (struct sadb_address
*)buf
;
2018 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
2020 if (buf
+ len
> lim
)
2024 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2025 p
->sadb_address_exttype
= exttype
& 0xffff;
2026 p
->sadb_address_proto
= ul_proto
& 0xff;
2027 p
->sadb_address_prefixlen
= prefixlen
;
2028 p
->sadb_address_reserved
= 0;
2030 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2036 * set sadb_key structure after clearing buffer with zero.
2037 * OUT: the pointer of buf + len.
2040 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2049 p
= (struct sadb_key
*)buf
;
2050 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2052 if (buf
+ len
> lim
)
2056 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2057 p
->sadb_key_exttype
= type
;
2058 p
->sadb_key_bits
= keylen
<< 3;
2059 p
->sadb_key_reserved
= 0;
2061 memcpy(p
+ 1, key
, keylen
);
2067 * set sadb_lifetime structure after clearing buffer with zero.
2068 * OUT: the pointer of buf + len.
2071 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2075 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2077 struct sadb_lifetime
*p
;
2080 p
= (struct sadb_lifetime
*)buf
;
2081 len
= sizeof(struct sadb_lifetime
);
2083 if (buf
+ len
> lim
)
2087 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2088 p
->sadb_lifetime_exttype
= type
;
2091 case SADB_EXT_LIFETIME_SOFT
:
2092 p
->sadb_lifetime_allocations
2093 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2094 p
->sadb_lifetime_bytes
2095 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2096 p
->sadb_lifetime_addtime
2097 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2098 p
->sadb_lifetime_usetime
2099 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2101 case SADB_EXT_LIFETIME_HARD
:
2102 p
->sadb_lifetime_allocations
= l_alloc
;
2103 p
->sadb_lifetime_bytes
= l_bytes
;
2104 p
->sadb_lifetime_addtime
= l_addtime
;
2105 p
->sadb_lifetime_usetime
= l_usetime
;
2113 * copy secasvar data into sadb_address.
2114 * `buf' must has been allocated sufficiently.
2117 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2123 struct sadb_x_sa2
*p
;
2124 u_int8_t mode
= mode0
& 0xff;
2127 p
= (struct sadb_x_sa2
*)buf
;
2128 len
= sizeof(struct sadb_x_sa2
);
2130 if (buf
+ len
> lim
)
2134 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2135 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2136 p
->sadb_x_sa2_mode
= mode
;
2137 p
->sadb_x_sa2_reqid
= reqid
;