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 <net/pfkeyv2.h>
37 #include <netkey/key_var.h>
38 #include <netinet/in.h>
39 #include <netinet6/ipsec.h>
47 #include "ipsec_strerror.h"
50 #define CALLOC(size, cast) (cast)calloc(1, (size))
52 static int findsupportedmap
__P((int));
53 static int setsupportedmap
__P((struct sadb_supported
*));
54 static struct sadb_alg
*findsupportedalg
__P((u_int
, u_int
));
55 static int pfkey_send_x1
__P((int, u_int
, u_int
, u_int
, struct sockaddr
*,
56 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
57 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
58 u_int32_t
, u_int32_t
, u_int32_t
));
59 static int pfkey_send_x2
__P((int, u_int
, u_int
, u_int
,
60 struct sockaddr
*, struct sockaddr
*, u_int32_t
));
61 static int pfkey_send_x3
__P((int, u_int
, u_int
));
62 static int pfkey_send_x4
__P((int, u_int
, struct sockaddr
*, u_int
,
63 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
64 char *, int, u_int32_t
));
65 static int pfkey_send_x5
__P((int, u_int
, u_int32_t
));
67 static caddr_t pfkey_setsadbmsg
__P((caddr_t
, caddr_t
, u_int
, u_int
,
68 u_int
, u_int32_t
, pid_t
));
69 static caddr_t pfkey_setsadbsa
__P((caddr_t
, caddr_t
, u_int32_t
, u_int
,
70 u_int
, u_int
, u_int32_t
));
71 static caddr_t pfkey_setsadbaddr
__P((caddr_t
, caddr_t
, u_int
,
72 struct sockaddr
*, u_int
, u_int
));
73 static caddr_t pfkey_setsadbkey
__P((caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
));
74 static caddr_t pfkey_setsadblifetime
__P((caddr_t
, caddr_t
, u_int
, u_int32_t
,
75 u_int32_t
, u_int32_t
, u_int32_t
));
76 static caddr_t pfkey_setsadbxsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int32_t
));
79 * make and search supported algorithm structure.
81 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
83 static int supported_map
[] = {
90 findsupportedmap(satype
)
95 for (i
= 0; i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
96 if (supported_map
[i
] == satype
)
101 static struct sadb_alg
*
102 findsupportedalg(satype
, alg_id
)
103 u_int satype
, alg_id
;
110 algno
= findsupportedmap(satype
);
112 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
115 if (ipsec_supported
[algno
] == NULL
) {
116 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
120 tlen
= ipsec_supported
[algno
]->sadb_supported_len
121 - sizeof(struct sadb_supported
);
122 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
124 if (tlen
< sizeof(struct sadb_alg
)) {
128 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
129 return (struct sadb_alg
*)p
;
131 tlen
-= sizeof(struct sadb_alg
);
132 p
+= sizeof(struct sadb_alg
);
135 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
141 struct sadb_supported
*sup
;
143 struct sadb_supported
**ipsup
;
145 switch (sup
->sadb_supported_exttype
) {
146 case SADB_EXT_SUPPORTED_AUTH
:
147 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
149 case SADB_EXT_SUPPORTED_ENCRYPT
:
150 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
153 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
160 *ipsup
= malloc(sup
->sadb_supported_len
);
162 __ipsec_set_strerror(strerror(errno
));
165 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
171 * check key length against algorithm specified.
172 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
173 * augument, and only calls to ipsec_check_keylen2();
174 * keylen is the unit of bit.
180 ipsec_check_keylen(supported
, alg_id
, keylen
)
189 case SADB_EXT_SUPPORTED_AUTH
:
190 satype
= SADB_SATYPE_AH
;
192 case SADB_EXT_SUPPORTED_ENCRYPT
:
193 satype
= SADB_SATYPE_ESP
;
196 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
200 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
204 * check key length against algorithm specified.
205 * satype is one of satype defined at pfkeyv2.h.
206 * keylen is the unit of bit.
212 ipsec_check_keylen2(satype
, alg_id
, keylen
)
217 struct sadb_alg
*alg
;
219 alg
= findsupportedalg(satype
, alg_id
);
223 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
224 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
228 __ipsec_errcode
= EIPSEC_NO_ERROR
;
233 * get max/min key length against algorithm specified.
234 * satype is one of satype defined at pfkeyv2.h.
235 * keylen is the unit of bit.
241 ipsec_get_keylen(supported
, alg_id
, alg0
)
242 u_int supported
, alg_id
;
243 struct sadb_alg
*alg0
;
245 struct sadb_alg
*alg
;
250 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
255 case SADB_EXT_SUPPORTED_AUTH
:
256 satype
= SADB_SATYPE_AH
;
258 case SADB_EXT_SUPPORTED_ENCRYPT
:
259 satype
= SADB_SATYPE_ESP
;
262 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
266 alg
= findsupportedalg(satype
, alg_id
);
270 memcpy(alg0
, alg
, sizeof(*alg0
));
272 __ipsec_errcode
= EIPSEC_NO_ERROR
;
277 * set the rate for SOFT lifetime against HARD one.
278 * If rate is more than 100 or equal to zero, then set to 100.
280 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
281 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
282 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
283 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
286 pfkey_set_softrate(type
, rate
)
289 __ipsec_errcode
= EIPSEC_NO_ERROR
;
291 if (rate
> 100 || rate
== 0)
295 case SADB_X_LIFETIME_ALLOCATIONS
:
296 soft_lifetime_allocations_rate
= rate
;
298 case SADB_X_LIFETIME_BYTES
:
299 soft_lifetime_bytes_rate
= rate
;
301 case SADB_X_LIFETIME_ADDTIME
:
302 soft_lifetime_addtime_rate
= rate
;
304 case SADB_X_LIFETIME_USETIME
:
305 soft_lifetime_usetime_rate
= rate
;
309 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
314 * get current rate for SOFT lifetime against HARD one.
315 * ATTENTION: ~0 is returned if invalid type was passed.
318 pfkey_get_softrate(type
)
322 case SADB_X_LIFETIME_ALLOCATIONS
:
323 return soft_lifetime_allocations_rate
;
324 case SADB_X_LIFETIME_BYTES
:
325 return soft_lifetime_bytes_rate
;
326 case SADB_X_LIFETIME_ADDTIME
:
327 return soft_lifetime_addtime_rate
;
328 case SADB_X_LIFETIME_USETIME
:
329 return soft_lifetime_usetime_rate
;
336 * sending SADB_GETSPI message to the kernel.
338 * positive: success and return length sent.
339 * -1 : error occured, and set errno.
342 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
345 struct sockaddr
*src
, *dst
;
346 u_int32_t min
, max
, reqid
, seq
;
348 struct sadb_msg
*newmsg
;
351 int need_spirange
= 0;
356 if (src
== NULL
|| dst
== NULL
) {
357 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
360 if (src
->sa_family
!= dst
->sa_family
) {
361 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
364 if (min
> max
|| (min
> 0 && min
<= 255)) {
365 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
368 switch (src
->sa_family
) {
370 plen
= sizeof(struct in_addr
) << 3;
373 plen
= sizeof(struct in6_addr
) << 3;
376 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
380 /* create new sadb_msg to send. */
381 len
= sizeof(struct sadb_msg
)
382 + sizeof(struct sadb_x_sa2
)
383 + sizeof(struct sadb_address
)
384 + PFKEY_ALIGN8(src
->sa_len
)
385 + sizeof(struct sadb_address
)
386 + PFKEY_ALIGN8(dst
->sa_len
);
388 if (min
> 255 && max
< ~0) {
390 len
+= sizeof(struct sadb_spirange
);
393 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
394 __ipsec_set_strerror(strerror(errno
));
397 ep
= ((caddr_t
)newmsg
) + len
;
399 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
400 len
, satype
, seq
, getpid());
406 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
412 /* set sadb_address for source */
413 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
420 /* set sadb_address for destination */
421 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
428 /* proccessing spi range */
430 struct sadb_spirange spirange
;
432 if (p
+ sizeof(spirange
) > ep
) {
437 memset(&spirange
, 0, sizeof(spirange
));
438 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
439 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
440 spirange
.sadb_spirange_min
= min
;
441 spirange
.sadb_spirange_max
= max
;
443 memcpy(p
, &spirange
, sizeof(spirange
));
445 p
+= sizeof(spirange
);
453 len
= pfkey_send(so
, newmsg
, len
);
459 __ipsec_errcode
= EIPSEC_NO_ERROR
;
464 * sending SADB_UPDATE message to the kernel.
465 * The length of key material is a_keylen + e_keylen.
467 * positive: success and return length sent.
468 * -1 : error occured, and set errno.
471 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
472 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
473 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
475 u_int satype
, mode
, wsize
;
476 struct sockaddr
*src
, *dst
;
477 u_int32_t spi
, reqid
;
479 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
481 u_int64_t l_bytes
, l_addtime
, l_usetime
;
485 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
487 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
488 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
495 * sending SADB_ADD message to the kernel.
496 * The length of key material is a_keylen + e_keylen.
498 * positive: success and return length sent.
499 * -1 : error occured, and set errno.
502 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
503 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
504 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
506 u_int satype
, mode
, wsize
;
507 struct sockaddr
*src
, *dst
;
508 u_int32_t spi
, reqid
;
510 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
512 u_int64_t l_bytes
, l_addtime
, l_usetime
;
516 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
518 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
519 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
526 * sending SADB_DELETE message to the kernel.
528 * positive: success and return length sent.
529 * -1 : error occured, and set errno.
532 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
535 struct sockaddr
*src
, *dst
;
539 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
546 * sending SADB_DELETE without spi to the kernel. This is
547 * the "delete all" request (an extension also present in
551 * positive: success and return length sent
552 * -1 : error occured, and set errno
555 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
558 struct sockaddr
*src
, *dst
;
560 struct sadb_msg
*newmsg
;
567 if (src
== NULL
|| dst
== NULL
) {
568 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
571 if (src
->sa_family
!= dst
->sa_family
) {
572 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
575 switch (src
->sa_family
) {
577 plen
= sizeof(struct in_addr
) << 3;
580 plen
= sizeof(struct in6_addr
) << 3;
583 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
587 /* create new sadb_msg to reply. */
588 len
= sizeof(struct sadb_msg
)
589 + sizeof(struct sadb_address
)
590 + PFKEY_ALIGN8(src
->sa_len
)
591 + sizeof(struct sadb_address
)
592 + PFKEY_ALIGN8(dst
->sa_len
);
594 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
595 __ipsec_set_strerror(strerror(errno
));
598 ep
= ((caddr_t
)newmsg
) + len
;
600 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
606 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
612 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
620 len
= pfkey_send(so
, newmsg
, len
);
626 __ipsec_errcode
= EIPSEC_NO_ERROR
;
631 * sending SADB_GET message to the kernel.
633 * positive: success and return length sent.
634 * -1 : error occured, and set errno.
637 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
640 struct sockaddr
*src
, *dst
;
644 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
651 * sending SADB_REGISTER message to the kernel.
653 * positive: success and return length sent.
654 * -1 : error occured, and set errno.
657 pfkey_send_register(so
, satype
)
663 if (satype
== PF_UNSPEC
) {
665 algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
667 if (ipsec_supported
[algno
]) {
668 free(ipsec_supported
[algno
]);
669 ipsec_supported
[algno
] = NULL
;
673 algno
= findsupportedmap(satype
);
675 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
679 if (ipsec_supported
[algno
]) {
680 free(ipsec_supported
[algno
]);
681 ipsec_supported
[algno
] = NULL
;
685 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
692 * receiving SADB_REGISTER message from the kernel, and copy buffer for
693 * sadb_supported returned into ipsec_supported.
695 * 0: success and return length sent.
696 * -1: error occured, and set errno.
699 pfkey_recv_register(so
)
702 pid_t pid
= getpid();
703 struct sadb_msg
*newmsg
;
706 /* receive message */
708 if ((newmsg
= pfkey_recv(so
)) == NULL
)
710 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
711 || newmsg
->sadb_msg_pid
!= pid
);
714 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
716 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
720 __ipsec_errcode
= EIPSEC_NO_ERROR
;
726 * receiving SADB_REGISTER message from the kernel, and copy buffer for
727 * sadb_supported returned into ipsec_supported.
728 * NOTE: sadb_msg_len must be host order.
730 * tlen: msg length, it's to makeing sure.
732 * 0: success and return length sent.
733 * -1: error occured, and set errno.
736 pfkey_set_supported(msg
, tlen
)
737 struct sadb_msg
*msg
;
740 struct sadb_supported
*sup
;
745 if (msg
->sadb_msg_len
!= tlen
) {
746 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
753 p
+= sizeof(struct sadb_msg
);
756 sup
= (struct sadb_supported
*)p
;
757 if (ep
< p
+ sizeof(*sup
) ||
758 PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
759 ep
< p
+ sup
->sadb_supported_len
) {
764 switch (sup
->sadb_supported_exttype
) {
765 case SADB_EXT_SUPPORTED_AUTH
:
766 case SADB_EXT_SUPPORTED_ENCRYPT
:
769 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
774 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
776 /* set supported map */
777 if (setsupportedmap(sup
) != 0)
780 p
+= sup
->sadb_supported_len
;
784 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
788 __ipsec_errcode
= EIPSEC_NO_ERROR
;
794 * sending SADB_FLUSH message to the kernel.
796 * positive: success and return length sent.
797 * -1 : error occured, and set errno.
800 pfkey_send_flush(so
, satype
)
806 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
813 * sending SADB_DUMP message to the kernel.
815 * positive: success and return length sent.
816 * -1 : error occured, and set errno.
819 pfkey_send_dump(so
, satype
)
825 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
832 * sending SADB_X_PROMISC message to the kernel.
833 * NOTE that this function handles promisc mode toggle only.
835 * flag: set promisc off if zero, set promisc on if non-zero.
837 * positive: success and return length sent.
838 * -1 : error occured, and set errno.
839 * 0 : error occured, and set errno.
840 * others: a pointer to new allocated buffer in which supported
844 pfkey_send_promisc_toggle(so
, flag
)
850 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
857 * sending SADB_X_SPDADD message to the kernel.
859 * positive: success and return length sent.
860 * -1 : error occured, and set errno.
863 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
865 struct sockaddr
*src
, *dst
;
866 u_int prefs
, prefd
, proto
;
873 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
874 src
, prefs
, dst
, prefd
, proto
,
876 policy
, policylen
, seq
)) < 0)
883 * sending SADB_X_SPDADD message to the kernel.
885 * positive: success and return length sent.
886 * -1 : error occured, and set errno.
889 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
890 policy
, policylen
, seq
)
892 struct sockaddr
*src
, *dst
;
893 u_int prefs
, prefd
, proto
;
894 u_int64_t ltime
, vtime
;
901 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
902 src
, prefs
, dst
, prefd
, proto
,
904 policy
, policylen
, seq
)) < 0)
911 * sending SADB_X_SPDUPDATE message to the kernel.
913 * positive: success and return length sent.
914 * -1 : error occured, and set errno.
917 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
919 struct sockaddr
*src
, *dst
;
920 u_int prefs
, prefd
, proto
;
927 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
928 src
, prefs
, dst
, prefd
, proto
,
930 policy
, policylen
, seq
)) < 0)
937 * sending SADB_X_SPDUPDATE message to the kernel.
939 * positive: success and return length sent.
940 * -1 : error occured, and set errno.
943 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
944 policy
, policylen
, seq
)
946 struct sockaddr
*src
, *dst
;
947 u_int prefs
, prefd
, proto
;
948 u_int64_t ltime
, vtime
;
955 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
956 src
, prefs
, dst
, prefd
, proto
,
958 policy
, policylen
, seq
)) < 0)
965 * sending SADB_X_SPDDELETE message to the kernel.
967 * positive: success and return length sent.
968 * -1 : error occured, and set errno.
971 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
973 struct sockaddr
*src
, *dst
;
974 u_int prefs
, prefd
, proto
;
981 if (policylen
!= sizeof(struct sadb_x_policy
)) {
982 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
986 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
987 src
, prefs
, dst
, prefd
, proto
,
989 policy
, policylen
, seq
)) < 0)
996 * sending SADB_X_SPDDELETE message to the kernel.
998 * positive: success and return length sent.
999 * -1 : error occured, and set errno.
1002 pfkey_send_spddelete2(so
, spid
)
1008 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1015 * sending SADB_X_SPDGET message to the kernel.
1017 * positive: success and return length sent.
1018 * -1 : error occured, and set errno.
1021 pfkey_send_spdget(so
, spid
)
1027 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1034 * sending SADB_X_SPDSETIDX message to the kernel.
1036 * positive: success and return length sent.
1037 * -1 : error occured, and set errno.
1040 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1042 struct sockaddr
*src
, *dst
;
1043 u_int prefs
, prefd
, proto
;
1050 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1051 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1055 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1056 src
, prefs
, dst
, prefd
, proto
,
1058 policy
, policylen
, seq
)) < 0)
1065 * sending SADB_SPDFLUSH message to the kernel.
1067 * positive: success and return length sent.
1068 * -1 : error occured, and set errno.
1071 pfkey_send_spdflush(so
)
1076 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1083 * sending SADB_SPDDUMP message to the kernel.
1085 * positive: success and return length sent.
1086 * -1 : error occured, and set errno.
1089 pfkey_send_spddump(so
)
1094 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1100 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1102 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1103 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1104 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1106 u_int type
, satype
, mode
;
1107 struct sockaddr
*src
, *dst
;
1108 u_int32_t spi
, reqid
;
1111 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1112 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1114 struct sadb_msg
*newmsg
;
1120 /* validity check */
1121 if (src
== NULL
|| dst
== NULL
) {
1122 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1125 if (src
->sa_family
!= dst
->sa_family
) {
1126 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1129 switch (src
->sa_family
) {
1131 plen
= sizeof(struct in_addr
) << 3;
1134 plen
= sizeof(struct in6_addr
) << 3;
1137 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1142 case SADB_SATYPE_ESP
:
1143 if (e_type
== SADB_EALG_NONE
) {
1144 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1148 case SADB_SATYPE_AH
:
1149 if (e_type
!= SADB_EALG_NONE
) {
1150 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1153 if (a_type
== SADB_AALG_NONE
) {
1154 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1158 case SADB_X_SATYPE_IPCOMP
:
1159 if (e_type
== SADB_X_CALG_NONE
) {
1160 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1163 if (a_type
!= SADB_AALG_NONE
) {
1164 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1169 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1173 /* create new sadb_msg to reply. */
1174 len
= sizeof(struct sadb_msg
)
1175 + sizeof(struct sadb_sa
)
1176 + sizeof(struct sadb_x_sa2
)
1177 + sizeof(struct sadb_address
)
1178 + PFKEY_ALIGN8(src
->sa_len
)
1179 + sizeof(struct sadb_address
)
1180 + PFKEY_ALIGN8(dst
->sa_len
)
1181 + sizeof(struct sadb_lifetime
)
1182 + sizeof(struct sadb_lifetime
);
1184 if (e_type
!= SADB_EALG_NONE
)
1185 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1186 if (a_type
!= SADB_AALG_NONE
)
1187 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1189 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1190 __ipsec_set_strerror(strerror(errno
));
1193 ep
= ((caddr_t
)newmsg
) + len
;
1195 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1196 satype
, seq
, getpid());
1201 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1206 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1211 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1217 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1224 if (e_type
!= SADB_EALG_NONE
) {
1225 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1232 if (a_type
!= SADB_AALG_NONE
) {
1233 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1234 keymat
+ e_keylen
, a_keylen
);
1241 /* set sadb_lifetime for destination */
1242 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1243 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1248 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1249 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1250 if (!p
|| p
!= ep
) {
1256 len
= pfkey_send(so
, newmsg
, len
);
1262 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1266 /* sending SADB_DELETE or SADB_GET message to the kernel */
1268 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1270 u_int type
, satype
, mode
;
1271 struct sockaddr
*src
, *dst
;
1274 struct sadb_msg
*newmsg
;
1280 /* validity check */
1281 if (src
== NULL
|| dst
== NULL
) {
1282 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1285 if (src
->sa_family
!= dst
->sa_family
) {
1286 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1289 switch (src
->sa_family
) {
1291 plen
= sizeof(struct in_addr
) << 3;
1294 plen
= sizeof(struct in6_addr
) << 3;
1297 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1301 /* create new sadb_msg to reply. */
1302 len
= sizeof(struct sadb_msg
)
1303 + sizeof(struct sadb_sa
)
1304 + sizeof(struct sadb_address
)
1305 + PFKEY_ALIGN8(src
->sa_len
)
1306 + sizeof(struct sadb_address
)
1307 + PFKEY_ALIGN8(dst
->sa_len
);
1309 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1310 __ipsec_set_strerror(strerror(errno
));
1313 ep
= ((caddr_t
)newmsg
) + len
;
1315 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1321 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1326 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1332 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1334 if (!p
|| p
!= ep
) {
1340 len
= pfkey_send(so
, newmsg
, len
);
1346 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1351 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1355 pfkey_send_x3(so
, type
, satype
)
1359 struct sadb_msg
*newmsg
;
1364 /* validity check */
1366 case SADB_X_PROMISC
:
1367 if (satype
!= 0 && satype
!= 1) {
1368 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1374 case SADB_SATYPE_UNSPEC
:
1375 case SADB_SATYPE_AH
:
1376 case SADB_SATYPE_ESP
:
1377 case SADB_X_SATYPE_IPCOMP
:
1380 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1385 /* create new sadb_msg to send. */
1386 len
= sizeof(struct sadb_msg
);
1388 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1389 __ipsec_set_strerror(strerror(errno
));
1392 ep
= ((caddr_t
)newmsg
) + len
;
1394 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1396 if (!p
|| p
!= ep
) {
1402 len
= pfkey_send(so
, newmsg
, len
);
1408 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1412 /* sending SADB_X_SPDADD message to the kernel */
1414 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1415 ltime
, vtime
, policy
, policylen
, seq
)
1417 struct sockaddr
*src
, *dst
;
1418 u_int type
, prefs
, prefd
, proto
;
1419 u_int64_t ltime
, vtime
;
1424 struct sadb_msg
*newmsg
;
1430 /* validity check */
1431 if (src
== NULL
|| dst
== NULL
) {
1432 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1435 if (src
->sa_family
!= dst
->sa_family
) {
1436 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1440 switch (src
->sa_family
) {
1442 plen
= sizeof(struct in_addr
) << 3;
1445 plen
= sizeof(struct in6_addr
) << 3;
1448 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1451 if (prefs
> plen
|| prefd
> plen
) {
1452 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1456 /* create new sadb_msg to reply. */
1457 len
= sizeof(struct sadb_msg
)
1458 + sizeof(struct sadb_address
)
1459 + PFKEY_ALIGN8(src
->sa_len
)
1460 + sizeof(struct sadb_address
)
1461 + PFKEY_ALIGN8(src
->sa_len
)
1462 + sizeof(struct sadb_lifetime
)
1465 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1466 __ipsec_set_strerror(strerror(errno
));
1469 ep
= ((caddr_t
)newmsg
) + len
;
1471 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1472 SADB_SATYPE_UNSPEC
, seq
, getpid());
1477 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1482 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1487 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1488 0, 0, ltime
, vtime
);
1489 if (!p
|| p
+ policylen
!= ep
) {
1493 memcpy(p
, policy
, policylen
);
1496 len
= pfkey_send(so
, newmsg
, len
);
1502 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1506 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1508 pfkey_send_x5(so
, type
, spid
)
1513 struct sadb_msg
*newmsg
;
1514 struct sadb_x_policy xpl
;
1519 /* create new sadb_msg to reply. */
1520 len
= sizeof(struct sadb_msg
)
1523 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1524 __ipsec_set_strerror(strerror(errno
));
1527 ep
= ((caddr_t
)newmsg
) + len
;
1529 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1530 SADB_SATYPE_UNSPEC
, 0, getpid());
1536 if (p
+ sizeof(xpl
) != ep
) {
1540 memset(&xpl
, 0, sizeof(xpl
));
1541 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1542 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1543 xpl
.sadb_x_policy_id
= spid
;
1544 memcpy(p
, &xpl
, sizeof(xpl
));
1547 len
= pfkey_send(so
, newmsg
, len
);
1553 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1561 * others : success and return value of socket.
1567 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1569 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1570 __ipsec_set_strerror(strerror(errno
));
1575 * This is a temporary workaround for KAME PR 154.
1576 * Don't really care even if it fails.
1578 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1579 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1581 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1597 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1602 * receive sadb_msg data, and return pointer to new buffer allocated.
1603 * Must free this buffer later.
1605 * NULL : error occured.
1606 * others : a pointer to sadb_msg structure.
1608 * XXX should be rewritten to pass length explicitly
1614 struct sadb_msg buf
, *newmsg
;
1617 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1620 __ipsec_set_strerror(strerror(errno
));
1624 if (len
< sizeof(buf
)) {
1625 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1626 __ipsec_errcode
= EIPSEC_MAX
;
1630 /* read real message */
1631 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1632 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1633 __ipsec_set_strerror(strerror(errno
));
1637 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1640 __ipsec_set_strerror(strerror(errno
));
1645 if (len
!= reallen
) {
1646 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1651 /* don't trust what the kernel says, validate! */
1652 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1653 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1658 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1663 * send message to a socket.
1665 * others: success and return length sent.
1669 pfkey_send(so
, msg
, len
)
1671 struct sadb_msg
*msg
;
1674 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1675 __ipsec_set_strerror(strerror(errno
));
1679 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1685 * NOTE: These functions are derived from netkey/key.c in KAME.
1688 * set the pointer to each header in this message buffer.
1689 * IN: msg: pointer to message buffer.
1690 * mhp: pointer to the buffer initialized like below:
1691 * caddr_t mhp[SADB_EXT_MAX + 1];
1695 * XXX should be rewritten to obtain length explicitly
1698 pfkey_align(msg
, mhp
)
1699 struct sadb_msg
*msg
;
1702 struct sadb_ext
*ext
;
1705 caddr_t ep
; /* XXX should be passed from upper layer */
1707 /* validity check */
1708 if (msg
== NULL
|| mhp
== NULL
) {
1709 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1714 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1717 mhp
[0] = (caddr_t
)msg
;
1721 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1723 /* skip base header */
1724 p
+= sizeof(struct sadb_msg
);
1727 ext
= (struct sadb_ext
*)p
;
1728 if (ep
< p
+ sizeof(*ext
) || PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1729 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1730 /* invalid format */
1734 /* duplicate check */
1735 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1736 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1737 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1742 switch (ext
->sadb_ext_type
) {
1744 case SADB_EXT_LIFETIME_CURRENT
:
1745 case SADB_EXT_LIFETIME_HARD
:
1746 case SADB_EXT_LIFETIME_SOFT
:
1747 case SADB_EXT_ADDRESS_SRC
:
1748 case SADB_EXT_ADDRESS_DST
:
1749 case SADB_EXT_ADDRESS_PROXY
:
1750 case SADB_EXT_KEY_AUTH
:
1751 /* XXX should to be check weak keys. */
1752 case SADB_EXT_KEY_ENCRYPT
:
1753 /* XXX should to be check weak keys. */
1754 case SADB_EXT_IDENTITY_SRC
:
1755 case SADB_EXT_IDENTITY_DST
:
1756 case SADB_EXT_SENSITIVITY
:
1757 case SADB_EXT_PROPOSAL
:
1758 case SADB_EXT_SUPPORTED_AUTH
:
1759 case SADB_EXT_SUPPORTED_ENCRYPT
:
1760 case SADB_EXT_SPIRANGE
:
1761 case SADB_X_EXT_POLICY
:
1762 case SADB_X_EXT_SA2
:
1763 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1766 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1770 p
+= PFKEY_EXTLEN(ext
);
1774 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1778 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1783 * check basic usage for sadb_msg,
1784 * NOTE: This routine is derived from netkey/key.c in KAME.
1785 * IN: msg: pointer to message buffer.
1786 * mhp: pointer to the buffer initialized like below:
1788 * caddr_t mhp[SADB_EXT_MAX + 1];
1797 struct sadb_msg
*msg
;
1799 /* validity check */
1800 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1801 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1805 msg
= (struct sadb_msg
*)mhp
[0];
1808 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1809 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1814 if (msg
->sadb_msg_type
> SADB_MAX
) {
1815 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1820 switch (msg
->sadb_msg_satype
) {
1821 case SADB_SATYPE_UNSPEC
:
1822 switch (msg
->sadb_msg_type
) {
1830 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1834 case SADB_SATYPE_ESP
:
1835 case SADB_SATYPE_AH
:
1836 case SADB_X_SATYPE_IPCOMP
:
1837 switch (msg
->sadb_msg_type
) {
1839 case SADB_X_SPDDELETE
:
1841 case SADB_X_SPDDUMP
:
1842 case SADB_X_SPDFLUSH
:
1843 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1847 case SADB_SATYPE_RSVP
:
1848 case SADB_SATYPE_OSPFV2
:
1849 case SADB_SATYPE_RIPV2
:
1850 case SADB_SATYPE_MIP
:
1851 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1853 case 1: /* XXX: What does it do ? */
1854 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1858 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1862 /* check field of upper layer protocol and address family */
1863 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1864 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1865 struct sadb_address
*src0
, *dst0
;
1867 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1868 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1870 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1871 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1875 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1876 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1877 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1881 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1886 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1891 * prefixlen == 0 is valid because there must be the case
1892 * all addresses are matched.
1896 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1901 * set data into sadb_msg.
1902 * `buf' must has been allocated sufficiently.
1905 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1916 p
= (struct sadb_msg
*)buf
;
1917 len
= sizeof(struct sadb_msg
);
1919 if (buf
+ len
> lim
)
1923 p
->sadb_msg_version
= PF_KEY_V2
;
1924 p
->sadb_msg_type
= type
;
1925 p
->sadb_msg_errno
= 0;
1926 p
->sadb_msg_satype
= satype
;
1927 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1928 p
->sadb_msg_reserved
= 0;
1929 p
->sadb_msg_seq
= seq
;
1930 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1936 * copy secasvar data into sadb_address.
1937 * `buf' must has been allocated sufficiently.
1940 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1943 u_int32_t spi
, flags
;
1944 u_int wsize
, auth
, enc
;
1949 p
= (struct sadb_sa
*)buf
;
1950 len
= sizeof(struct sadb_sa
);
1952 if (buf
+ len
> lim
)
1956 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1957 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1958 p
->sadb_sa_spi
= spi
;
1959 p
->sadb_sa_replay
= wsize
;
1960 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1961 p
->sadb_sa_auth
= auth
;
1962 p
->sadb_sa_encrypt
= enc
;
1963 p
->sadb_sa_flags
= flags
;
1969 * set data into sadb_address.
1970 * `buf' must has been allocated sufficiently.
1971 * prefixlen is in bits.
1974 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
1978 struct sockaddr
*saddr
;
1982 struct sadb_address
*p
;
1985 p
= (struct sadb_address
*)buf
;
1986 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
1988 if (buf
+ len
> lim
)
1992 p
->sadb_address_len
= PFKEY_UNIT64(len
);
1993 p
->sadb_address_exttype
= exttype
& 0xffff;
1994 p
->sadb_address_proto
= ul_proto
& 0xff;
1995 p
->sadb_address_prefixlen
= prefixlen
;
1996 p
->sadb_address_reserved
= 0;
1998 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2004 * set sadb_key structure after clearing buffer with zero.
2005 * OUT: the pointer of buf + len.
2008 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2017 p
= (struct sadb_key
*)buf
;
2018 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2020 if (buf
+ len
> lim
)
2024 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2025 p
->sadb_key_exttype
= type
;
2026 p
->sadb_key_bits
= keylen
<< 3;
2027 p
->sadb_key_reserved
= 0;
2029 memcpy(p
+ 1, key
, keylen
);
2035 * set sadb_lifetime structure after clearing buffer with zero.
2036 * OUT: the pointer of buf + len.
2039 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2043 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2045 struct sadb_lifetime
*p
;
2048 p
= (struct sadb_lifetime
*)buf
;
2049 len
= sizeof(struct sadb_lifetime
);
2051 if (buf
+ len
> lim
)
2055 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2056 p
->sadb_lifetime_exttype
= type
;
2059 case SADB_EXT_LIFETIME_SOFT
:
2060 p
->sadb_lifetime_allocations
2061 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2062 p
->sadb_lifetime_bytes
2063 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2064 p
->sadb_lifetime_addtime
2065 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2066 p
->sadb_lifetime_usetime
2067 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2069 case SADB_EXT_LIFETIME_HARD
:
2070 p
->sadb_lifetime_allocations
= l_alloc
;
2071 p
->sadb_lifetime_bytes
= l_bytes
;
2072 p
->sadb_lifetime_addtime
= l_addtime
;
2073 p
->sadb_lifetime_usetime
= l_usetime
;
2081 * copy secasvar data into sadb_address.
2082 * `buf' must has been allocated sufficiently.
2085 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2091 struct sadb_x_sa2
*p
;
2092 u_int8_t mode
= mode0
& 0xff;
2095 p
= (struct sadb_x_sa2
*)buf
;
2096 len
= sizeof(struct sadb_x_sa2
);
2098 if (buf
+ len
> lim
)
2102 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2103 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2104 p
->sadb_x_sa2_mode
= mode
;
2105 p
->sadb_x_sa2_reqid
= reqid
;