1 /* $FreeBSD: src/lib/libipsec/pfkey.c,v 1.1.2.2 2001/07/03 11:01:14 ume Exp $ */
2 /* $DragonFly: src/lib/libipsec/pfkey.c,v 1.4 2003/11/13 02:39:42 drhodus Exp $ */
3 /* $KAME: pfkey.c,v 1.39 2001/03/05 18:22:17 thorpej Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.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"
51 #define CALLOC(size, cast) (cast)calloc(1, (size))
53 static int findsupportedmap (int);
54 static int setsupportedmap (struct sadb_supported
*);
55 static struct sadb_alg
*findsupportedalg (u_int
, u_int
);
56 static int pfkey_send_x1 (int, u_int
, u_int
, u_int
, struct sockaddr
*,
57 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
58 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
59 u_int32_t
, u_int32_t
, u_int32_t
);
60 static int pfkey_send_x2 (int, u_int
, u_int
, u_int
,
61 struct sockaddr
*, struct sockaddr
*, u_int32_t
);
62 static int pfkey_send_x3 (int, u_int
, u_int
);
63 static int pfkey_send_x4 (int, u_int
, struct sockaddr
*, u_int
,
64 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
65 char *, int, u_int32_t
);
66 static int pfkey_send_x5 (int, u_int
, u_int32_t
);
68 static caddr_t
pfkey_setsadbmsg (caddr_t
, caddr_t
, u_int
, u_int
,
69 u_int
, u_int32_t
, pid_t
);
70 static caddr_t
pfkey_setsadbsa (caddr_t
, caddr_t
, u_int32_t
, u_int
,
71 u_int
, u_int
, u_int32_t
);
72 static caddr_t
pfkey_setsadbaddr (caddr_t
, caddr_t
, u_int
,
73 struct sockaddr
*, u_int
, u_int
);
74 static caddr_t
pfkey_setsadbkey (caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
);
75 static caddr_t
pfkey_setsadblifetime (caddr_t
, caddr_t
, u_int
, u_int32_t
,
76 u_int32_t
, u_int32_t
, u_int32_t
);
77 static caddr_t
pfkey_setsadbxsa2 (caddr_t
, caddr_t
, u_int32_t
, u_int32_t
);
80 * make and search supported algorithm structure.
82 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
84 static int supported_map
[] = {
91 findsupportedmap(satype
)
96 for (i
= 0; i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
97 if (supported_map
[i
] == satype
)
102 static struct sadb_alg
*
103 findsupportedalg(satype
, alg_id
)
104 u_int satype
, alg_id
;
111 algno
= findsupportedmap(satype
);
113 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
116 if (ipsec_supported
[algno
] == NULL
) {
117 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
121 tlen
= ipsec_supported
[algno
]->sadb_supported_len
122 - sizeof(struct sadb_supported
);
123 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
125 if (tlen
< sizeof(struct sadb_alg
)) {
129 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
130 return (struct sadb_alg
*)p
;
132 tlen
-= sizeof(struct sadb_alg
);
133 p
+= sizeof(struct sadb_alg
);
136 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
142 struct sadb_supported
*sup
;
144 struct sadb_supported
**ipsup
;
146 switch (sup
->sadb_supported_exttype
) {
147 case SADB_EXT_SUPPORTED_AUTH
:
148 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
150 case SADB_EXT_SUPPORTED_ENCRYPT
:
151 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
154 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
161 *ipsup
= malloc(sup
->sadb_supported_len
);
163 __ipsec_set_strerror(strerror(errno
));
166 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
172 * check key length against algorithm specified.
173 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
174 * augument, and only calls to ipsec_check_keylen2();
175 * keylen is the unit of bit.
181 ipsec_check_keylen(supported
, alg_id
, keylen
)
190 case SADB_EXT_SUPPORTED_AUTH
:
191 satype
= SADB_SATYPE_AH
;
193 case SADB_EXT_SUPPORTED_ENCRYPT
:
194 satype
= SADB_SATYPE_ESP
;
197 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
201 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
205 * check key length against algorithm specified.
206 * satype is one of satype defined at pfkeyv2.h.
207 * keylen is the unit of bit.
213 ipsec_check_keylen2(satype
, alg_id
, keylen
)
218 struct sadb_alg
*alg
;
220 alg
= findsupportedalg(satype
, alg_id
);
224 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
225 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
229 __ipsec_errcode
= EIPSEC_NO_ERROR
;
234 * get max/min key length against algorithm specified.
235 * satype is one of satype defined at pfkeyv2.h.
236 * keylen is the unit of bit.
242 ipsec_get_keylen(supported
, alg_id
, alg0
)
243 u_int supported
, alg_id
;
244 struct sadb_alg
*alg0
;
246 struct sadb_alg
*alg
;
251 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
256 case SADB_EXT_SUPPORTED_AUTH
:
257 satype
= SADB_SATYPE_AH
;
259 case SADB_EXT_SUPPORTED_ENCRYPT
:
260 satype
= SADB_SATYPE_ESP
;
263 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
267 alg
= findsupportedalg(satype
, alg_id
);
271 memcpy(alg0
, alg
, sizeof(*alg0
));
273 __ipsec_errcode
= EIPSEC_NO_ERROR
;
278 * set the rate for SOFT lifetime against HARD one.
279 * If rate is more than 100 or equal to zero, then set to 100.
281 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
282 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
283 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
284 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
287 pfkey_set_softrate(type
, rate
)
290 __ipsec_errcode
= EIPSEC_NO_ERROR
;
292 if (rate
> 100 || rate
== 0)
296 case SADB_X_LIFETIME_ALLOCATIONS
:
297 soft_lifetime_allocations_rate
= rate
;
299 case SADB_X_LIFETIME_BYTES
:
300 soft_lifetime_bytes_rate
= rate
;
302 case SADB_X_LIFETIME_ADDTIME
:
303 soft_lifetime_addtime_rate
= rate
;
305 case SADB_X_LIFETIME_USETIME
:
306 soft_lifetime_usetime_rate
= rate
;
310 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
315 * get current rate for SOFT lifetime against HARD one.
316 * ATTENTION: ~0 is returned if invalid type was passed.
319 pfkey_get_softrate(type
)
323 case SADB_X_LIFETIME_ALLOCATIONS
:
324 return soft_lifetime_allocations_rate
;
325 case SADB_X_LIFETIME_BYTES
:
326 return soft_lifetime_bytes_rate
;
327 case SADB_X_LIFETIME_ADDTIME
:
328 return soft_lifetime_addtime_rate
;
329 case SADB_X_LIFETIME_USETIME
:
330 return soft_lifetime_usetime_rate
;
337 * sending SADB_GETSPI message to the kernel.
339 * positive: success and return length sent.
340 * -1 : error occured, and set errno.
343 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
346 struct sockaddr
*src
, *dst
;
347 u_int32_t min
, max
, reqid
, seq
;
349 struct sadb_msg
*newmsg
;
352 int need_spirange
= 0;
357 if (src
== NULL
|| dst
== NULL
) {
358 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
361 if (src
->sa_family
!= dst
->sa_family
) {
362 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
365 if (min
> max
|| (min
> 0 && min
<= 255)) {
366 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
369 switch (src
->sa_family
) {
371 plen
= sizeof(struct in_addr
) << 3;
374 plen
= sizeof(struct in6_addr
) << 3;
377 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
381 /* create new sadb_msg to send. */
382 len
= sizeof(struct sadb_msg
)
383 + sizeof(struct sadb_x_sa2
)
384 + sizeof(struct sadb_address
)
385 + PFKEY_ALIGN8(src
->sa_len
)
386 + sizeof(struct sadb_address
)
387 + PFKEY_ALIGN8(dst
->sa_len
);
389 if (min
> 255 && max
< ~0) {
391 len
+= sizeof(struct sadb_spirange
);
394 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
395 __ipsec_set_strerror(strerror(errno
));
398 ep
= ((caddr_t
)newmsg
) + len
;
400 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
401 len
, satype
, seq
, getpid());
407 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
413 /* set sadb_address for source */
414 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
421 /* set sadb_address for destination */
422 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
429 /* proccessing spi range */
431 struct sadb_spirange spirange
;
433 if (p
+ sizeof(spirange
) > ep
) {
438 memset(&spirange
, 0, sizeof(spirange
));
439 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
440 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
441 spirange
.sadb_spirange_min
= min
;
442 spirange
.sadb_spirange_max
= max
;
444 memcpy(p
, &spirange
, sizeof(spirange
));
446 p
+= sizeof(spirange
);
454 len
= pfkey_send(so
, newmsg
, len
);
460 __ipsec_errcode
= EIPSEC_NO_ERROR
;
465 * sending SADB_UPDATE message to the kernel.
466 * The length of key material is a_keylen + e_keylen.
468 * positive: success and return length sent.
469 * -1 : error occured, and set errno.
472 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
473 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
474 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
476 u_int satype
, mode
, wsize
;
477 struct sockaddr
*src
, *dst
;
478 u_int32_t spi
, reqid
;
480 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
482 u_int64_t l_bytes
, l_addtime
, l_usetime
;
486 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
488 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
489 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
496 * sending SADB_ADD message to the kernel.
497 * The length of key material is a_keylen + e_keylen.
499 * positive: success and return length sent.
500 * -1 : error occured, and set errno.
503 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
504 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
505 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
507 u_int satype
, mode
, wsize
;
508 struct sockaddr
*src
, *dst
;
509 u_int32_t spi
, reqid
;
511 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
513 u_int64_t l_bytes
, l_addtime
, l_usetime
;
517 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
519 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
520 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
527 * sending SADB_DELETE message to the kernel.
529 * positive: success and return length sent.
530 * -1 : error occured, and set errno.
533 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
536 struct sockaddr
*src
, *dst
;
540 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
547 * sending SADB_DELETE without spi to the kernel. This is
548 * the "delete all" request (an extension also present in
552 * positive: success and return length sent
553 * -1 : error occured, and set errno
556 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
559 struct sockaddr
*src
, *dst
;
561 struct sadb_msg
*newmsg
;
568 if (src
== NULL
|| dst
== NULL
) {
569 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
572 if (src
->sa_family
!= dst
->sa_family
) {
573 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
576 switch (src
->sa_family
) {
578 plen
= sizeof(struct in_addr
) << 3;
581 plen
= sizeof(struct in6_addr
) << 3;
584 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
588 /* create new sadb_msg to reply. */
589 len
= sizeof(struct sadb_msg
)
590 + sizeof(struct sadb_address
)
591 + PFKEY_ALIGN8(src
->sa_len
)
592 + sizeof(struct sadb_address
)
593 + PFKEY_ALIGN8(dst
->sa_len
);
595 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
596 __ipsec_set_strerror(strerror(errno
));
599 ep
= ((caddr_t
)newmsg
) + len
;
601 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
607 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
613 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
621 len
= pfkey_send(so
, newmsg
, len
);
627 __ipsec_errcode
= EIPSEC_NO_ERROR
;
632 * sending SADB_GET message to the kernel.
634 * positive: success and return length sent.
635 * -1 : error occured, and set errno.
638 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
641 struct sockaddr
*src
, *dst
;
645 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
652 * sending SADB_REGISTER message to the kernel.
654 * positive: success and return length sent.
655 * -1 : error occured, and set errno.
658 pfkey_send_register(so
, satype
)
664 if (satype
== PF_UNSPEC
) {
666 algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
668 if (ipsec_supported
[algno
]) {
669 free(ipsec_supported
[algno
]);
670 ipsec_supported
[algno
] = NULL
;
674 algno
= findsupportedmap(satype
);
676 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
680 if (ipsec_supported
[algno
]) {
681 free(ipsec_supported
[algno
]);
682 ipsec_supported
[algno
] = NULL
;
686 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
693 * receiving SADB_REGISTER message from the kernel, and copy buffer for
694 * sadb_supported returned into ipsec_supported.
696 * 0: success and return length sent.
697 * -1: error occured, and set errno.
700 pfkey_recv_register(so
)
703 pid_t pid
= getpid();
704 struct sadb_msg
*newmsg
;
707 /* receive message */
709 if ((newmsg
= pfkey_recv(so
)) == NULL
)
711 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
712 || newmsg
->sadb_msg_pid
!= pid
);
715 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
717 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
721 __ipsec_errcode
= EIPSEC_NO_ERROR
;
727 * receiving SADB_REGISTER message from the kernel, and copy buffer for
728 * sadb_supported returned into ipsec_supported.
729 * NOTE: sadb_msg_len must be host order.
731 * tlen: msg length, it's to makeing sure.
733 * 0: success and return length sent.
734 * -1: error occured, and set errno.
737 pfkey_set_supported(msg
, tlen
)
738 struct sadb_msg
*msg
;
741 struct sadb_supported
*sup
;
746 if (msg
->sadb_msg_len
!= tlen
) {
747 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
754 p
+= sizeof(struct sadb_msg
);
757 sup
= (struct sadb_supported
*)p
;
758 if (ep
< p
+ sizeof(*sup
) ||
759 PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
760 ep
< p
+ sup
->sadb_supported_len
) {
765 switch (sup
->sadb_supported_exttype
) {
766 case SADB_EXT_SUPPORTED_AUTH
:
767 case SADB_EXT_SUPPORTED_ENCRYPT
:
770 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
775 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
777 /* set supported map */
778 if (setsupportedmap(sup
) != 0)
781 p
+= sup
->sadb_supported_len
;
785 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
789 __ipsec_errcode
= EIPSEC_NO_ERROR
;
795 * sending SADB_FLUSH message to the kernel.
797 * positive: success and return length sent.
798 * -1 : error occured, and set errno.
801 pfkey_send_flush(so
, satype
)
807 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
814 * sending SADB_DUMP message to the kernel.
816 * positive: success and return length sent.
817 * -1 : error occured, and set errno.
820 pfkey_send_dump(so
, satype
)
826 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
833 * sending SADB_X_PROMISC message to the kernel.
834 * NOTE that this function handles promisc mode toggle only.
836 * flag: set promisc off if zero, set promisc on if non-zero.
838 * positive: success and return length sent.
839 * -1 : error occured, and set errno.
840 * 0 : error occured, and set errno.
841 * others: a pointer to new allocated buffer in which supported
845 pfkey_send_promisc_toggle(so
, flag
)
851 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
858 * sending SADB_X_SPDADD message to the kernel.
860 * positive: success and return length sent.
861 * -1 : error occured, and set errno.
864 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
866 struct sockaddr
*src
, *dst
;
867 u_int prefs
, prefd
, proto
;
874 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
875 src
, prefs
, dst
, prefd
, proto
,
877 policy
, policylen
, seq
)) < 0)
884 * sending SADB_X_SPDADD message to the kernel.
886 * positive: success and return length sent.
887 * -1 : error occured, and set errno.
890 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
891 policy
, policylen
, seq
)
893 struct sockaddr
*src
, *dst
;
894 u_int prefs
, prefd
, proto
;
895 u_int64_t ltime
, vtime
;
902 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
903 src
, prefs
, dst
, prefd
, proto
,
905 policy
, policylen
, seq
)) < 0)
912 * sending SADB_X_SPDUPDATE message to the kernel.
914 * positive: success and return length sent.
915 * -1 : error occured, and set errno.
918 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
920 struct sockaddr
*src
, *dst
;
921 u_int prefs
, prefd
, proto
;
928 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
929 src
, prefs
, dst
, prefd
, proto
,
931 policy
, policylen
, seq
)) < 0)
938 * sending SADB_X_SPDUPDATE message to the kernel.
940 * positive: success and return length sent.
941 * -1 : error occured, and set errno.
944 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
945 policy
, policylen
, seq
)
947 struct sockaddr
*src
, *dst
;
948 u_int prefs
, prefd
, proto
;
949 u_int64_t ltime
, vtime
;
956 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
957 src
, prefs
, dst
, prefd
, proto
,
959 policy
, policylen
, seq
)) < 0)
966 * sending SADB_X_SPDDELETE message to the kernel.
968 * positive: success and return length sent.
969 * -1 : error occured, and set errno.
972 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
974 struct sockaddr
*src
, *dst
;
975 u_int prefs
, prefd
, proto
;
982 if (policylen
!= sizeof(struct sadb_x_policy
)) {
983 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
987 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
988 src
, prefs
, dst
, prefd
, proto
,
990 policy
, policylen
, seq
)) < 0)
997 * sending SADB_X_SPDDELETE message to the kernel.
999 * positive: success and return length sent.
1000 * -1 : error occured, and set errno.
1003 pfkey_send_spddelete2(so
, spid
)
1009 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1016 * sending SADB_X_SPDGET message to the kernel.
1018 * positive: success and return length sent.
1019 * -1 : error occured, and set errno.
1022 pfkey_send_spdget(so
, spid
)
1028 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1035 * sending SADB_X_SPDSETIDX message to the kernel.
1037 * positive: success and return length sent.
1038 * -1 : error occured, and set errno.
1041 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1043 struct sockaddr
*src
, *dst
;
1044 u_int prefs
, prefd
, proto
;
1051 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1052 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1056 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1057 src
, prefs
, dst
, prefd
, proto
,
1059 policy
, policylen
, seq
)) < 0)
1066 * sending SADB_SPDFLUSH message to the kernel.
1068 * positive: success and return length sent.
1069 * -1 : error occured, and set errno.
1072 pfkey_send_spdflush(so
)
1077 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1084 * sending SADB_SPDDUMP message to the kernel.
1086 * positive: success and return length sent.
1087 * -1 : error occured, and set errno.
1090 pfkey_send_spddump(so
)
1095 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1101 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1103 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1104 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1105 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1107 u_int type
, satype
, mode
;
1108 struct sockaddr
*src
, *dst
;
1109 u_int32_t spi
, reqid
;
1112 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1113 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1115 struct sadb_msg
*newmsg
;
1121 /* validity check */
1122 if (src
== NULL
|| dst
== NULL
) {
1123 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1126 if (src
->sa_family
!= dst
->sa_family
) {
1127 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1130 switch (src
->sa_family
) {
1132 plen
= sizeof(struct in_addr
) << 3;
1135 plen
= sizeof(struct in6_addr
) << 3;
1138 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1143 case SADB_SATYPE_ESP
:
1144 if (e_type
== SADB_EALG_NONE
) {
1145 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1149 case SADB_SATYPE_AH
:
1150 if (e_type
!= SADB_EALG_NONE
) {
1151 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1154 if (a_type
== SADB_AALG_NONE
) {
1155 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1159 case SADB_X_SATYPE_IPCOMP
:
1160 if (e_type
== SADB_X_CALG_NONE
) {
1161 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1164 if (a_type
!= SADB_AALG_NONE
) {
1165 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1170 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1174 /* create new sadb_msg to reply. */
1175 len
= sizeof(struct sadb_msg
)
1176 + sizeof(struct sadb_sa
)
1177 + sizeof(struct sadb_x_sa2
)
1178 + sizeof(struct sadb_address
)
1179 + PFKEY_ALIGN8(src
->sa_len
)
1180 + sizeof(struct sadb_address
)
1181 + PFKEY_ALIGN8(dst
->sa_len
)
1182 + sizeof(struct sadb_lifetime
)
1183 + sizeof(struct sadb_lifetime
);
1185 if (e_type
!= SADB_EALG_NONE
)
1186 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1187 if (a_type
!= SADB_AALG_NONE
)
1188 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1190 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1191 __ipsec_set_strerror(strerror(errno
));
1194 ep
= ((caddr_t
)newmsg
) + len
;
1196 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1197 satype
, seq
, getpid());
1202 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1207 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1212 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1218 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1225 if (e_type
!= SADB_EALG_NONE
) {
1226 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1233 if (a_type
!= SADB_AALG_NONE
) {
1234 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1235 keymat
+ e_keylen
, a_keylen
);
1242 /* set sadb_lifetime for destination */
1243 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1244 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1249 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1250 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1251 if (!p
|| p
!= ep
) {
1257 len
= pfkey_send(so
, newmsg
, len
);
1263 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1267 /* sending SADB_DELETE or SADB_GET message to the kernel */
1269 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1271 u_int type
, satype
, mode
;
1272 struct sockaddr
*src
, *dst
;
1275 struct sadb_msg
*newmsg
;
1281 /* validity check */
1282 if (src
== NULL
|| dst
== NULL
) {
1283 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1286 if (src
->sa_family
!= dst
->sa_family
) {
1287 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1290 switch (src
->sa_family
) {
1292 plen
= sizeof(struct in_addr
) << 3;
1295 plen
= sizeof(struct in6_addr
) << 3;
1298 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1302 /* create new sadb_msg to reply. */
1303 len
= sizeof(struct sadb_msg
)
1304 + sizeof(struct sadb_sa
)
1305 + sizeof(struct sadb_address
)
1306 + PFKEY_ALIGN8(src
->sa_len
)
1307 + sizeof(struct sadb_address
)
1308 + PFKEY_ALIGN8(dst
->sa_len
);
1310 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1311 __ipsec_set_strerror(strerror(errno
));
1314 ep
= ((caddr_t
)newmsg
) + len
;
1316 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1322 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1327 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1333 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1335 if (!p
|| p
!= ep
) {
1341 len
= pfkey_send(so
, newmsg
, len
);
1347 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1352 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1356 pfkey_send_x3(so
, type
, satype
)
1360 struct sadb_msg
*newmsg
;
1365 /* validity check */
1367 case SADB_X_PROMISC
:
1368 if (satype
!= 0 && satype
!= 1) {
1369 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1375 case SADB_SATYPE_UNSPEC
:
1376 case SADB_SATYPE_AH
:
1377 case SADB_SATYPE_ESP
:
1378 case SADB_X_SATYPE_IPCOMP
:
1381 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1386 /* create new sadb_msg to send. */
1387 len
= sizeof(struct sadb_msg
);
1389 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1390 __ipsec_set_strerror(strerror(errno
));
1393 ep
= ((caddr_t
)newmsg
) + len
;
1395 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1397 if (!p
|| p
!= ep
) {
1403 len
= pfkey_send(so
, newmsg
, len
);
1409 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1413 /* sending SADB_X_SPDADD message to the kernel */
1415 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1416 ltime
, vtime
, policy
, policylen
, seq
)
1418 struct sockaddr
*src
, *dst
;
1419 u_int type
, prefs
, prefd
, proto
;
1420 u_int64_t ltime
, vtime
;
1425 struct sadb_msg
*newmsg
;
1431 /* validity check */
1432 if (src
== NULL
|| dst
== NULL
) {
1433 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1436 if (src
->sa_family
!= dst
->sa_family
) {
1437 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1441 switch (src
->sa_family
) {
1443 plen
= sizeof(struct in_addr
) << 3;
1446 plen
= sizeof(struct in6_addr
) << 3;
1449 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1452 if (prefs
> plen
|| prefd
> plen
) {
1453 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1457 /* create new sadb_msg to reply. */
1458 len
= sizeof(struct sadb_msg
)
1459 + sizeof(struct sadb_address
)
1460 + PFKEY_ALIGN8(src
->sa_len
)
1461 + sizeof(struct sadb_address
)
1462 + PFKEY_ALIGN8(src
->sa_len
)
1463 + sizeof(struct sadb_lifetime
)
1466 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1467 __ipsec_set_strerror(strerror(errno
));
1470 ep
= ((caddr_t
)newmsg
) + len
;
1472 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1473 SADB_SATYPE_UNSPEC
, seq
, getpid());
1478 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1483 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1488 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1489 0, 0, ltime
, vtime
);
1490 if (!p
|| p
+ policylen
!= ep
) {
1494 memcpy(p
, policy
, policylen
);
1497 len
= pfkey_send(so
, newmsg
, len
);
1503 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1507 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1509 pfkey_send_x5(so
, type
, spid
)
1514 struct sadb_msg
*newmsg
;
1515 struct sadb_x_policy xpl
;
1520 /* create new sadb_msg to reply. */
1521 len
= sizeof(struct sadb_msg
)
1524 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1525 __ipsec_set_strerror(strerror(errno
));
1528 ep
= ((caddr_t
)newmsg
) + len
;
1530 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1531 SADB_SATYPE_UNSPEC
, 0, getpid());
1537 if (p
+ sizeof(xpl
) != ep
) {
1541 memset(&xpl
, 0, sizeof(xpl
));
1542 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1543 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1544 xpl
.sadb_x_policy_id
= spid
;
1545 memcpy(p
, &xpl
, sizeof(xpl
));
1548 len
= pfkey_send(so
, newmsg
, len
);
1554 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1562 * others : success and return value of socket.
1568 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1570 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1571 __ipsec_set_strerror(strerror(errno
));
1576 * This is a temporary workaround for KAME PR 154.
1577 * Don't really care even if it fails.
1579 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1580 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1582 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1598 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1603 * receive sadb_msg data, and return pointer to new buffer allocated.
1604 * Must free this buffer later.
1606 * NULL : error occured.
1607 * others : a pointer to sadb_msg structure.
1609 * XXX should be rewritten to pass length explicitly
1615 struct sadb_msg buf
, *newmsg
;
1618 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1621 __ipsec_set_strerror(strerror(errno
));
1625 if (len
< sizeof(buf
)) {
1626 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1627 __ipsec_errcode
= EIPSEC_MAX
;
1631 /* read real message */
1632 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1633 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1634 __ipsec_set_strerror(strerror(errno
));
1638 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1641 __ipsec_set_strerror(strerror(errno
));
1646 if (len
!= reallen
) {
1647 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1652 /* don't trust what the kernel says, validate! */
1653 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1654 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1659 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1664 * send message to a socket.
1666 * others: success and return length sent.
1670 pfkey_send(so
, msg
, len
)
1672 struct sadb_msg
*msg
;
1675 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1676 __ipsec_set_strerror(strerror(errno
));
1680 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1686 * NOTE: These functions are derived from netkey/key.c in KAME.
1689 * set the pointer to each header in this message buffer.
1690 * IN: msg: pointer to message buffer.
1691 * mhp: pointer to the buffer initialized like below:
1692 * caddr_t mhp[SADB_EXT_MAX + 1];
1696 * XXX should be rewritten to obtain length explicitly
1699 pfkey_align(msg
, mhp
)
1700 struct sadb_msg
*msg
;
1703 struct sadb_ext
*ext
;
1706 caddr_t ep
; /* XXX should be passed from upper layer */
1708 /* validity check */
1709 if (msg
== NULL
|| mhp
== NULL
) {
1710 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1715 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1718 mhp
[0] = (caddr_t
)msg
;
1722 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1724 /* skip base header */
1725 p
+= sizeof(struct sadb_msg
);
1728 ext
= (struct sadb_ext
*)p
;
1729 if (ep
< p
+ sizeof(*ext
) || PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1730 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1731 /* invalid format */
1735 /* duplicate check */
1736 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1737 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1738 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1743 switch (ext
->sadb_ext_type
) {
1745 case SADB_EXT_LIFETIME_CURRENT
:
1746 case SADB_EXT_LIFETIME_HARD
:
1747 case SADB_EXT_LIFETIME_SOFT
:
1748 case SADB_EXT_ADDRESS_SRC
:
1749 case SADB_EXT_ADDRESS_DST
:
1750 case SADB_EXT_ADDRESS_PROXY
:
1751 case SADB_EXT_KEY_AUTH
:
1752 /* XXX should to be check weak keys. */
1753 case SADB_EXT_KEY_ENCRYPT
:
1754 /* XXX should to be check weak keys. */
1755 case SADB_EXT_IDENTITY_SRC
:
1756 case SADB_EXT_IDENTITY_DST
:
1757 case SADB_EXT_SENSITIVITY
:
1758 case SADB_EXT_PROPOSAL
:
1759 case SADB_EXT_SUPPORTED_AUTH
:
1760 case SADB_EXT_SUPPORTED_ENCRYPT
:
1761 case SADB_EXT_SPIRANGE
:
1762 case SADB_X_EXT_POLICY
:
1763 case SADB_X_EXT_SA2
:
1764 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1767 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1771 p
+= PFKEY_EXTLEN(ext
);
1775 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1779 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1784 * check basic usage for sadb_msg,
1785 * NOTE: This routine is derived from netkey/key.c in KAME.
1786 * IN: msg: pointer to message buffer.
1787 * mhp: pointer to the buffer initialized like below:
1789 * caddr_t mhp[SADB_EXT_MAX + 1];
1798 struct sadb_msg
*msg
;
1800 /* validity check */
1801 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1802 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1806 msg
= (struct sadb_msg
*)mhp
[0];
1809 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1810 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1815 if (msg
->sadb_msg_type
> SADB_MAX
) {
1816 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1821 switch (msg
->sadb_msg_satype
) {
1822 case SADB_SATYPE_UNSPEC
:
1823 switch (msg
->sadb_msg_type
) {
1831 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1835 case SADB_SATYPE_ESP
:
1836 case SADB_SATYPE_AH
:
1837 case SADB_X_SATYPE_IPCOMP
:
1838 switch (msg
->sadb_msg_type
) {
1840 case SADB_X_SPDDELETE
:
1842 case SADB_X_SPDDUMP
:
1843 case SADB_X_SPDFLUSH
:
1844 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1848 case SADB_SATYPE_RSVP
:
1849 case SADB_SATYPE_OSPFV2
:
1850 case SADB_SATYPE_RIPV2
:
1851 case SADB_SATYPE_MIP
:
1852 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1854 case 1: /* XXX: What does it do ? */
1855 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1859 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1863 /* check field of upper layer protocol and address family */
1864 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1865 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1866 struct sadb_address
*src0
, *dst0
;
1868 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1869 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1871 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1872 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1876 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1877 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1878 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1882 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1887 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1892 * prefixlen == 0 is valid because there must be the case
1893 * all addresses are matched.
1897 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1902 * set data into sadb_msg.
1903 * `buf' must has been allocated sufficiently.
1906 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1917 p
= (struct sadb_msg
*)buf
;
1918 len
= sizeof(struct sadb_msg
);
1920 if (buf
+ len
> lim
)
1924 p
->sadb_msg_version
= PF_KEY_V2
;
1925 p
->sadb_msg_type
= type
;
1926 p
->sadb_msg_errno
= 0;
1927 p
->sadb_msg_satype
= satype
;
1928 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1929 p
->sadb_msg_reserved
= 0;
1930 p
->sadb_msg_seq
= seq
;
1931 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1937 * copy secasvar data into sadb_address.
1938 * `buf' must has been allocated sufficiently.
1941 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1944 u_int32_t spi
, flags
;
1945 u_int wsize
, auth
, enc
;
1950 p
= (struct sadb_sa
*)buf
;
1951 len
= sizeof(struct sadb_sa
);
1953 if (buf
+ len
> lim
)
1957 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1958 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1959 p
->sadb_sa_spi
= spi
;
1960 p
->sadb_sa_replay
= wsize
;
1961 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1962 p
->sadb_sa_auth
= auth
;
1963 p
->sadb_sa_encrypt
= enc
;
1964 p
->sadb_sa_flags
= flags
;
1970 * set data into sadb_address.
1971 * `buf' must has been allocated sufficiently.
1972 * prefixlen is in bits.
1975 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
1979 struct sockaddr
*saddr
;
1983 struct sadb_address
*p
;
1986 p
= (struct sadb_address
*)buf
;
1987 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
1989 if (buf
+ len
> lim
)
1993 p
->sadb_address_len
= PFKEY_UNIT64(len
);
1994 p
->sadb_address_exttype
= exttype
& 0xffff;
1995 p
->sadb_address_proto
= ul_proto
& 0xff;
1996 p
->sadb_address_prefixlen
= prefixlen
;
1997 p
->sadb_address_reserved
= 0;
1999 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2005 * set sadb_key structure after clearing buffer with zero.
2006 * OUT: the pointer of buf + len.
2009 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2018 p
= (struct sadb_key
*)buf
;
2019 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2021 if (buf
+ len
> lim
)
2025 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2026 p
->sadb_key_exttype
= type
;
2027 p
->sadb_key_bits
= keylen
<< 3;
2028 p
->sadb_key_reserved
= 0;
2030 memcpy(p
+ 1, key
, keylen
);
2036 * set sadb_lifetime structure after clearing buffer with zero.
2037 * OUT: the pointer of buf + len.
2040 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2044 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2046 struct sadb_lifetime
*p
;
2049 p
= (struct sadb_lifetime
*)buf
;
2050 len
= sizeof(struct sadb_lifetime
);
2052 if (buf
+ len
> lim
)
2056 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2057 p
->sadb_lifetime_exttype
= type
;
2060 case SADB_EXT_LIFETIME_SOFT
:
2061 p
->sadb_lifetime_allocations
2062 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2063 p
->sadb_lifetime_bytes
2064 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2065 p
->sadb_lifetime_addtime
2066 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2067 p
->sadb_lifetime_usetime
2068 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2070 case SADB_EXT_LIFETIME_HARD
:
2071 p
->sadb_lifetime_allocations
= l_alloc
;
2072 p
->sadb_lifetime_bytes
= l_bytes
;
2073 p
->sadb_lifetime_addtime
= l_addtime
;
2074 p
->sadb_lifetime_usetime
= l_usetime
;
2082 * copy secasvar data into sadb_address.
2083 * `buf' must has been allocated sufficiently.
2086 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2092 struct sadb_x_sa2
*p
;
2093 u_int8_t mode
= mode0
& 0xff;
2096 p
= (struct sadb_x_sa2
*)buf
;
2097 len
= sizeof(struct sadb_x_sa2
);
2099 if (buf
+ len
> lim
)
2103 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2104 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2105 p
->sadb_x_sa2_mode
= mode
;
2106 p
->sadb_x_sa2_reqid
= reqid
;