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
, NULL
};
84 static int supported_map
[] = {
88 SADB_X_SATYPE_TCPSIGNATURE
,
92 findsupportedmap(satype
)
97 for (i
= 0; i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
98 if (supported_map
[i
] == satype
)
103 static struct sadb_alg
*
104 findsupportedalg(satype
, alg_id
)
105 u_int satype
, alg_id
;
112 algno
= findsupportedmap(satype
);
114 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
117 if (ipsec_supported
[algno
] == NULL
) {
118 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
122 tlen
= ipsec_supported
[algno
]->sadb_supported_len
123 - sizeof(struct sadb_supported
);
124 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
126 if (tlen
< sizeof(struct sadb_alg
)) {
130 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
131 return (struct sadb_alg
*)p
;
133 tlen
-= sizeof(struct sadb_alg
);
134 p
+= sizeof(struct sadb_alg
);
137 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
143 struct sadb_supported
*sup
;
145 struct sadb_supported
**ipsup
;
147 switch (sup
->sadb_supported_exttype
) {
148 case SADB_EXT_SUPPORTED_AUTH
:
149 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
151 case SADB_EXT_SUPPORTED_ENCRYPT
:
152 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
155 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
162 *ipsup
= malloc(sup
->sadb_supported_len
);
164 __ipsec_set_strerror(strerror(errno
));
167 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
173 * check key length against algorithm specified.
174 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
175 * augument, and only calls to ipsec_check_keylen2();
176 * keylen is the unit of bit.
182 ipsec_check_keylen(supported
, alg_id
, keylen
)
191 case SADB_EXT_SUPPORTED_AUTH
:
192 satype
= SADB_SATYPE_AH
;
194 case SADB_EXT_SUPPORTED_ENCRYPT
:
195 satype
= SADB_SATYPE_ESP
;
198 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
202 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
206 * check key length against algorithm specified.
207 * satype is one of satype defined at pfkeyv2.h.
208 * keylen is the unit of bit.
214 ipsec_check_keylen2(satype
, alg_id
, keylen
)
219 struct sadb_alg
*alg
;
221 alg
= findsupportedalg(satype
, alg_id
);
225 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
226 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
230 __ipsec_errcode
= EIPSEC_NO_ERROR
;
235 * get max/min key length against algorithm specified.
236 * satype is one of satype defined at pfkeyv2.h.
237 * keylen is the unit of bit.
243 ipsec_get_keylen(supported
, alg_id
, alg0
)
244 u_int supported
, alg_id
;
245 struct sadb_alg
*alg0
;
247 struct sadb_alg
*alg
;
252 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
257 case SADB_EXT_SUPPORTED_AUTH
:
258 satype
= SADB_SATYPE_AH
;
260 case SADB_EXT_SUPPORTED_ENCRYPT
:
261 satype
= SADB_SATYPE_ESP
;
264 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
268 alg
= findsupportedalg(satype
, alg_id
);
272 memcpy(alg0
, alg
, sizeof(*alg0
));
274 __ipsec_errcode
= EIPSEC_NO_ERROR
;
279 * set the rate for SOFT lifetime against HARD one.
280 * If rate is more than 100 or equal to zero, then set to 100.
282 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
283 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
284 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
285 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
288 pfkey_set_softrate(type
, rate
)
291 __ipsec_errcode
= EIPSEC_NO_ERROR
;
293 if (rate
> 100 || rate
== 0)
297 case SADB_X_LIFETIME_ALLOCATIONS
:
298 soft_lifetime_allocations_rate
= rate
;
300 case SADB_X_LIFETIME_BYTES
:
301 soft_lifetime_bytes_rate
= rate
;
303 case SADB_X_LIFETIME_ADDTIME
:
304 soft_lifetime_addtime_rate
= rate
;
306 case SADB_X_LIFETIME_USETIME
:
307 soft_lifetime_usetime_rate
= rate
;
311 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
316 * get current rate for SOFT lifetime against HARD one.
317 * ATTENTION: ~0 is returned if invalid type was passed.
320 pfkey_get_softrate(type
)
324 case SADB_X_LIFETIME_ALLOCATIONS
:
325 return soft_lifetime_allocations_rate
;
326 case SADB_X_LIFETIME_BYTES
:
327 return soft_lifetime_bytes_rate
;
328 case SADB_X_LIFETIME_ADDTIME
:
329 return soft_lifetime_addtime_rate
;
330 case SADB_X_LIFETIME_USETIME
:
331 return soft_lifetime_usetime_rate
;
338 * sending SADB_GETSPI message to the kernel.
340 * positive: success and return length sent.
341 * -1 : error occured, and set errno.
344 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
347 struct sockaddr
*src
, *dst
;
348 u_int32_t min
, max
, reqid
, seq
;
350 struct sadb_msg
*newmsg
;
353 int need_spirange
= 0;
358 if (src
== NULL
|| dst
== NULL
) {
359 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
362 if (src
->sa_family
!= dst
->sa_family
) {
363 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
366 if (min
> max
|| (min
> 0 && min
<= 255)) {
367 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
370 switch (src
->sa_family
) {
372 plen
= sizeof(struct in_addr
) << 3;
375 plen
= sizeof(struct in6_addr
) << 3;
378 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
382 /* create new sadb_msg to send. */
383 len
= sizeof(struct sadb_msg
)
384 + sizeof(struct sadb_x_sa2
)
385 + sizeof(struct sadb_address
)
386 + PFKEY_ALIGN8(src
->sa_len
)
387 + sizeof(struct sadb_address
)
388 + PFKEY_ALIGN8(dst
->sa_len
);
390 if (min
> 255 && max
< ~0) {
392 len
+= sizeof(struct sadb_spirange
);
395 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
396 __ipsec_set_strerror(strerror(errno
));
399 ep
= ((caddr_t
)newmsg
) + len
;
401 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
402 len
, satype
, seq
, getpid());
408 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
414 /* set sadb_address for source */
415 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
422 /* set sadb_address for destination */
423 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
430 /* proccessing spi range */
432 struct sadb_spirange spirange
;
434 if (p
+ sizeof(spirange
) > ep
) {
439 memset(&spirange
, 0, sizeof(spirange
));
440 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
441 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
442 spirange
.sadb_spirange_min
= min
;
443 spirange
.sadb_spirange_max
= max
;
445 memcpy(p
, &spirange
, sizeof(spirange
));
447 p
+= sizeof(spirange
);
455 len
= pfkey_send(so
, newmsg
, len
);
461 __ipsec_errcode
= EIPSEC_NO_ERROR
;
466 * sending SADB_UPDATE message to the kernel.
467 * The length of key material is a_keylen + e_keylen.
469 * positive: success and return length sent.
470 * -1 : error occured, and set errno.
473 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
474 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
475 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
477 u_int satype
, mode
, wsize
;
478 struct sockaddr
*src
, *dst
;
479 u_int32_t spi
, reqid
;
481 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
483 u_int64_t l_bytes
, l_addtime
, l_usetime
;
487 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
489 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
490 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
497 * sending SADB_ADD message to the kernel.
498 * The length of key material is a_keylen + e_keylen.
500 * positive: success and return length sent.
501 * -1 : error occured, and set errno.
504 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
505 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
506 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
508 u_int satype
, mode
, wsize
;
509 struct sockaddr
*src
, *dst
;
510 u_int32_t spi
, reqid
;
512 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
514 u_int64_t l_bytes
, l_addtime
, l_usetime
;
518 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
520 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
521 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
528 * sending SADB_DELETE message to the kernel.
530 * positive: success and return length sent.
531 * -1 : error occured, and set errno.
534 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
537 struct sockaddr
*src
, *dst
;
541 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
548 * sending SADB_DELETE without spi to the kernel. This is
549 * the "delete all" request (an extension also present in
553 * positive: success and return length sent
554 * -1 : error occured, and set errno
557 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
560 struct sockaddr
*src
, *dst
;
562 struct sadb_msg
*newmsg
;
569 if (src
== NULL
|| dst
== NULL
) {
570 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
573 if (src
->sa_family
!= dst
->sa_family
) {
574 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
577 switch (src
->sa_family
) {
579 plen
= sizeof(struct in_addr
) << 3;
582 plen
= sizeof(struct in6_addr
) << 3;
585 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
589 /* create new sadb_msg to reply. */
590 len
= sizeof(struct sadb_msg
)
591 + sizeof(struct sadb_address
)
592 + PFKEY_ALIGN8(src
->sa_len
)
593 + sizeof(struct sadb_address
)
594 + PFKEY_ALIGN8(dst
->sa_len
);
596 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
597 __ipsec_set_strerror(strerror(errno
));
600 ep
= ((caddr_t
)newmsg
) + len
;
602 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
608 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
614 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
622 len
= pfkey_send(so
, newmsg
, len
);
628 __ipsec_errcode
= EIPSEC_NO_ERROR
;
633 * sending SADB_GET message to the kernel.
635 * positive: success and return length sent.
636 * -1 : error occured, and set errno.
639 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
642 struct sockaddr
*src
, *dst
;
646 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
653 * sending SADB_REGISTER message to the kernel.
655 * positive: success and return length sent.
656 * -1 : error occured, and set errno.
659 pfkey_send_register(so
, satype
)
665 if (satype
== PF_UNSPEC
) {
667 algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
669 if (ipsec_supported
[algno
]) {
670 free(ipsec_supported
[algno
]);
671 ipsec_supported
[algno
] = NULL
;
675 algno
= findsupportedmap(satype
);
677 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
681 if (ipsec_supported
[algno
]) {
682 free(ipsec_supported
[algno
]);
683 ipsec_supported
[algno
] = NULL
;
687 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
694 * receiving SADB_REGISTER message from the kernel, and copy buffer for
695 * sadb_supported returned into ipsec_supported.
697 * 0: success and return length sent.
698 * -1: error occured, and set errno.
701 pfkey_recv_register(so
)
704 pid_t pid
= getpid();
705 struct sadb_msg
*newmsg
;
708 /* receive message */
710 if ((newmsg
= pfkey_recv(so
)) == NULL
)
712 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
713 || newmsg
->sadb_msg_pid
!= pid
);
716 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
718 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
722 __ipsec_errcode
= EIPSEC_NO_ERROR
;
728 * receiving SADB_REGISTER message from the kernel, and copy buffer for
729 * sadb_supported returned into ipsec_supported.
730 * NOTE: sadb_msg_len must be host order.
732 * tlen: msg length, it's to makeing sure.
734 * 0: success and return length sent.
735 * -1: error occured, and set errno.
738 pfkey_set_supported(msg
, tlen
)
739 struct sadb_msg
*msg
;
742 struct sadb_supported
*sup
;
747 if (msg
->sadb_msg_len
!= tlen
) {
748 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
755 p
+= sizeof(struct sadb_msg
);
758 sup
= (struct sadb_supported
*)p
;
759 if (ep
< p
+ sizeof(*sup
) ||
760 PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
761 ep
< p
+ sup
->sadb_supported_len
) {
766 switch (sup
->sadb_supported_exttype
) {
767 case SADB_EXT_SUPPORTED_AUTH
:
768 case SADB_EXT_SUPPORTED_ENCRYPT
:
771 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
776 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
778 /* set supported map */
779 if (setsupportedmap(sup
) != 0)
782 p
+= sup
->sadb_supported_len
;
786 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
790 __ipsec_errcode
= EIPSEC_NO_ERROR
;
796 * sending SADB_FLUSH message to the kernel.
798 * positive: success and return length sent.
799 * -1 : error occured, and set errno.
802 pfkey_send_flush(so
, satype
)
808 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
815 * sending SADB_DUMP message to the kernel.
817 * positive: success and return length sent.
818 * -1 : error occured, and set errno.
821 pfkey_send_dump(so
, satype
)
827 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
834 * sending SADB_X_PROMISC message to the kernel.
835 * NOTE that this function handles promisc mode toggle only.
837 * flag: set promisc off if zero, set promisc on if non-zero.
839 * positive: success and return length sent.
840 * -1 : error occured, and set errno.
841 * 0 : error occured, and set errno.
842 * others: a pointer to new allocated buffer in which supported
846 pfkey_send_promisc_toggle(so
, flag
)
852 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
859 * sending SADB_X_SPDADD message to the kernel.
861 * positive: success and return length sent.
862 * -1 : error occured, and set errno.
865 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
867 struct sockaddr
*src
, *dst
;
868 u_int prefs
, prefd
, proto
;
875 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
876 src
, prefs
, dst
, prefd
, proto
,
878 policy
, policylen
, seq
)) < 0)
885 * sending SADB_X_SPDADD message to the kernel.
887 * positive: success and return length sent.
888 * -1 : error occured, and set errno.
891 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
892 policy
, policylen
, seq
)
894 struct sockaddr
*src
, *dst
;
895 u_int prefs
, prefd
, proto
;
896 u_int64_t ltime
, vtime
;
903 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
904 src
, prefs
, dst
, prefd
, proto
,
906 policy
, policylen
, seq
)) < 0)
913 * sending SADB_X_SPDUPDATE message to the kernel.
915 * positive: success and return length sent.
916 * -1 : error occured, and set errno.
919 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
921 struct sockaddr
*src
, *dst
;
922 u_int prefs
, prefd
, proto
;
929 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
930 src
, prefs
, dst
, prefd
, proto
,
932 policy
, policylen
, seq
)) < 0)
939 * sending SADB_X_SPDUPDATE message to the kernel.
941 * positive: success and return length sent.
942 * -1 : error occured, and set errno.
945 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
946 policy
, policylen
, seq
)
948 struct sockaddr
*src
, *dst
;
949 u_int prefs
, prefd
, proto
;
950 u_int64_t ltime
, vtime
;
957 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
958 src
, prefs
, dst
, prefd
, proto
,
960 policy
, policylen
, seq
)) < 0)
967 * sending SADB_X_SPDDELETE message to the kernel.
969 * positive: success and return length sent.
970 * -1 : error occured, and set errno.
973 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
975 struct sockaddr
*src
, *dst
;
976 u_int prefs
, prefd
, proto
;
983 if (policylen
!= sizeof(struct sadb_x_policy
)) {
984 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
988 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
989 src
, prefs
, dst
, prefd
, proto
,
991 policy
, policylen
, seq
)) < 0)
998 * sending SADB_X_SPDDELETE message to the kernel.
1000 * positive: success and return length sent.
1001 * -1 : error occured, and set errno.
1004 pfkey_send_spddelete2(so
, spid
)
1010 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1017 * sending SADB_X_SPDGET message to the kernel.
1019 * positive: success and return length sent.
1020 * -1 : error occured, and set errno.
1023 pfkey_send_spdget(so
, spid
)
1029 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1036 * sending SADB_X_SPDSETIDX message to the kernel.
1038 * positive: success and return length sent.
1039 * -1 : error occured, and set errno.
1042 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1044 struct sockaddr
*src
, *dst
;
1045 u_int prefs
, prefd
, proto
;
1052 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1053 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1057 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1058 src
, prefs
, dst
, prefd
, proto
,
1060 policy
, policylen
, seq
)) < 0)
1067 * sending SADB_SPDFLUSH message to the kernel.
1069 * positive: success and return length sent.
1070 * -1 : error occured, and set errno.
1073 pfkey_send_spdflush(so
)
1078 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1085 * sending SADB_SPDDUMP message to the kernel.
1087 * positive: success and return length sent.
1088 * -1 : error occured, and set errno.
1091 pfkey_send_spddump(so
)
1096 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1102 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1104 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1105 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1106 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1108 u_int type
, satype
, mode
;
1109 struct sockaddr
*src
, *dst
;
1110 u_int32_t spi
, reqid
;
1113 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1114 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1116 struct sadb_msg
*newmsg
;
1122 /* validity check */
1123 if (src
== NULL
|| dst
== NULL
) {
1124 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1127 if (src
->sa_family
!= dst
->sa_family
) {
1128 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1131 switch (src
->sa_family
) {
1133 plen
= sizeof(struct in_addr
) << 3;
1136 plen
= sizeof(struct in6_addr
) << 3;
1139 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1144 case SADB_SATYPE_ESP
:
1145 if (e_type
== SADB_EALG_NONE
) {
1146 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1150 case SADB_SATYPE_AH
:
1151 if (e_type
!= SADB_EALG_NONE
) {
1152 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1155 if (a_type
== SADB_AALG_NONE
) {
1156 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1160 case SADB_X_SATYPE_IPCOMP
:
1161 if (e_type
== SADB_X_CALG_NONE
) {
1162 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1165 if (a_type
!= SADB_AALG_NONE
) {
1166 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1170 case SADB_X_SATYPE_TCPSIGNATURE
:
1171 if (e_type
!= SADB_EALG_NONE
) {
1172 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1175 if (a_type
!= SADB_X_AALG_TCP_MD5
) {
1176 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1181 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1185 /* create new sadb_msg to reply. */
1186 len
= sizeof(struct sadb_msg
)
1187 + sizeof(struct sadb_sa
)
1188 + sizeof(struct sadb_x_sa2
)
1189 + sizeof(struct sadb_address
)
1190 + PFKEY_ALIGN8(src
->sa_len
)
1191 + sizeof(struct sadb_address
)
1192 + PFKEY_ALIGN8(dst
->sa_len
)
1193 + sizeof(struct sadb_lifetime
)
1194 + sizeof(struct sadb_lifetime
);
1196 if (e_type
!= SADB_EALG_NONE
)
1197 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1198 if (a_type
!= SADB_AALG_NONE
)
1199 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1201 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1202 __ipsec_set_strerror(strerror(errno
));
1205 ep
= ((caddr_t
)newmsg
) + len
;
1207 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1208 satype
, seq
, getpid());
1213 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1218 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1223 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1229 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1236 if (e_type
!= SADB_EALG_NONE
) {
1237 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1244 if (a_type
!= SADB_AALG_NONE
) {
1245 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1246 keymat
+ e_keylen
, a_keylen
);
1253 /* set sadb_lifetime for destination */
1254 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1255 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1260 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1261 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1262 if (!p
|| p
!= ep
) {
1268 len
= pfkey_send(so
, newmsg
, len
);
1274 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1278 /* sending SADB_DELETE or SADB_GET message to the kernel */
1280 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1282 u_int type
, satype
, mode
;
1283 struct sockaddr
*src
, *dst
;
1286 struct sadb_msg
*newmsg
;
1292 /* validity check */
1293 if (src
== NULL
|| dst
== NULL
) {
1294 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1297 if (src
->sa_family
!= dst
->sa_family
) {
1298 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1301 switch (src
->sa_family
) {
1303 plen
= sizeof(struct in_addr
) << 3;
1306 plen
= sizeof(struct in6_addr
) << 3;
1309 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1313 /* create new sadb_msg to reply. */
1314 len
= sizeof(struct sadb_msg
)
1315 + sizeof(struct sadb_sa
)
1316 + sizeof(struct sadb_address
)
1317 + PFKEY_ALIGN8(src
->sa_len
)
1318 + sizeof(struct sadb_address
)
1319 + PFKEY_ALIGN8(dst
->sa_len
);
1321 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1322 __ipsec_set_strerror(strerror(errno
));
1325 ep
= ((caddr_t
)newmsg
) + len
;
1327 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1333 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1338 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1344 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1346 if (!p
|| p
!= ep
) {
1352 len
= pfkey_send(so
, newmsg
, len
);
1358 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1363 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1367 pfkey_send_x3(so
, type
, satype
)
1371 struct sadb_msg
*newmsg
;
1376 /* validity check */
1378 case SADB_X_PROMISC
:
1379 if (satype
!= 0 && satype
!= 1) {
1380 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1386 case SADB_SATYPE_UNSPEC
:
1387 case SADB_SATYPE_AH
:
1388 case SADB_SATYPE_ESP
:
1389 case SADB_X_SATYPE_IPCOMP
:
1390 case SADB_X_SATYPE_TCPSIGNATURE
:
1393 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1398 /* create new sadb_msg to send. */
1399 len
= sizeof(struct sadb_msg
);
1401 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1402 __ipsec_set_strerror(strerror(errno
));
1405 ep
= ((caddr_t
)newmsg
) + len
;
1407 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1409 if (!p
|| p
!= ep
) {
1415 len
= pfkey_send(so
, newmsg
, len
);
1421 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1425 /* sending SADB_X_SPDADD message to the kernel */
1427 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1428 ltime
, vtime
, policy
, policylen
, seq
)
1430 struct sockaddr
*src
, *dst
;
1431 u_int type
, prefs
, prefd
, proto
;
1432 u_int64_t ltime
, vtime
;
1437 struct sadb_msg
*newmsg
;
1443 /* validity check */
1444 if (src
== NULL
|| dst
== NULL
) {
1445 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1448 if (src
->sa_family
!= dst
->sa_family
) {
1449 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1453 switch (src
->sa_family
) {
1455 plen
= sizeof(struct in_addr
) << 3;
1458 plen
= sizeof(struct in6_addr
) << 3;
1461 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1464 if (prefs
> plen
|| prefd
> plen
) {
1465 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1469 /* create new sadb_msg to reply. */
1470 len
= sizeof(struct sadb_msg
)
1471 + sizeof(struct sadb_address
)
1472 + PFKEY_ALIGN8(src
->sa_len
)
1473 + sizeof(struct sadb_address
)
1474 + PFKEY_ALIGN8(src
->sa_len
)
1475 + sizeof(struct sadb_lifetime
)
1478 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1479 __ipsec_set_strerror(strerror(errno
));
1482 ep
= ((caddr_t
)newmsg
) + len
;
1484 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1485 SADB_SATYPE_UNSPEC
, seq
, getpid());
1490 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1495 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1500 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1501 0, 0, ltime
, vtime
);
1502 if (!p
|| p
+ policylen
!= ep
) {
1506 memcpy(p
, policy
, policylen
);
1509 len
= pfkey_send(so
, newmsg
, len
);
1515 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1519 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1521 pfkey_send_x5(so
, type
, spid
)
1526 struct sadb_msg
*newmsg
;
1527 struct sadb_x_policy xpl
;
1532 /* create new sadb_msg to reply. */
1533 len
= sizeof(struct sadb_msg
)
1536 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1537 __ipsec_set_strerror(strerror(errno
));
1540 ep
= ((caddr_t
)newmsg
) + len
;
1542 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1543 SADB_SATYPE_UNSPEC
, 0, getpid());
1549 if (p
+ sizeof(xpl
) != ep
) {
1553 memset(&xpl
, 0, sizeof(xpl
));
1554 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1555 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1556 xpl
.sadb_x_policy_id
= spid
;
1557 memcpy(p
, &xpl
, sizeof(xpl
));
1560 len
= pfkey_send(so
, newmsg
, len
);
1566 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1574 * others : success and return value of socket.
1580 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1582 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1583 __ipsec_set_strerror(strerror(errno
));
1588 * This is a temporary workaround for KAME PR 154.
1589 * Don't really care even if it fails.
1591 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1592 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1594 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1610 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1615 * receive sadb_msg data, and return pointer to new buffer allocated.
1616 * Must free this buffer later.
1618 * NULL : error occured.
1619 * others : a pointer to sadb_msg structure.
1621 * XXX should be rewritten to pass length explicitly
1627 struct sadb_msg buf
, *newmsg
;
1630 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1633 __ipsec_set_strerror(strerror(errno
));
1637 if (len
< sizeof(buf
)) {
1638 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1639 __ipsec_errcode
= EIPSEC_MAX
;
1643 /* read real message */
1644 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1645 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1646 __ipsec_set_strerror(strerror(errno
));
1650 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1653 __ipsec_set_strerror(strerror(errno
));
1658 if (len
!= reallen
) {
1659 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1664 /* don't trust what the kernel says, validate! */
1665 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1666 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1671 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1676 * send message to a socket.
1678 * others: success and return length sent.
1682 pfkey_send(so
, msg
, len
)
1684 struct sadb_msg
*msg
;
1687 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1688 __ipsec_set_strerror(strerror(errno
));
1692 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1698 * NOTE: These functions are derived from netkey/key.c in KAME.
1701 * set the pointer to each header in this message buffer.
1702 * IN: msg: pointer to message buffer.
1703 * mhp: pointer to the buffer initialized like below:
1704 * caddr_t mhp[SADB_EXT_MAX + 1];
1708 * XXX should be rewritten to obtain length explicitly
1711 pfkey_align(msg
, mhp
)
1712 struct sadb_msg
*msg
;
1715 struct sadb_ext
*ext
;
1718 caddr_t ep
; /* XXX should be passed from upper layer */
1720 /* validity check */
1721 if (msg
== NULL
|| mhp
== NULL
) {
1722 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1727 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1730 mhp
[0] = (caddr_t
)msg
;
1734 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1736 /* skip base header */
1737 p
+= sizeof(struct sadb_msg
);
1740 ext
= (struct sadb_ext
*)p
;
1741 if (ep
< p
+ sizeof(*ext
) || PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1742 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1743 /* invalid format */
1747 /* duplicate check */
1748 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1749 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1750 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1755 switch (ext
->sadb_ext_type
) {
1757 case SADB_EXT_LIFETIME_CURRENT
:
1758 case SADB_EXT_LIFETIME_HARD
:
1759 case SADB_EXT_LIFETIME_SOFT
:
1760 case SADB_EXT_ADDRESS_SRC
:
1761 case SADB_EXT_ADDRESS_DST
:
1762 case SADB_EXT_ADDRESS_PROXY
:
1763 case SADB_EXT_KEY_AUTH
:
1764 /* XXX should to be check weak keys. */
1765 case SADB_EXT_KEY_ENCRYPT
:
1766 /* XXX should to be check weak keys. */
1767 case SADB_EXT_IDENTITY_SRC
:
1768 case SADB_EXT_IDENTITY_DST
:
1769 case SADB_EXT_SENSITIVITY
:
1770 case SADB_EXT_PROPOSAL
:
1771 case SADB_EXT_SUPPORTED_AUTH
:
1772 case SADB_EXT_SUPPORTED_ENCRYPT
:
1773 case SADB_EXT_SPIRANGE
:
1774 case SADB_X_EXT_POLICY
:
1775 case SADB_X_EXT_SA2
:
1776 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1779 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1783 p
+= PFKEY_EXTLEN(ext
);
1787 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1791 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1796 * check basic usage for sadb_msg,
1797 * NOTE: This routine is derived from netkey/key.c in KAME.
1798 * IN: msg: pointer to message buffer.
1799 * mhp: pointer to the buffer initialized like below:
1801 * caddr_t mhp[SADB_EXT_MAX + 1];
1810 struct sadb_msg
*msg
;
1812 /* validity check */
1813 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1814 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1818 msg
= (struct sadb_msg
*)mhp
[0];
1821 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1822 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1827 if (msg
->sadb_msg_type
> SADB_MAX
) {
1828 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1833 switch (msg
->sadb_msg_satype
) {
1834 case SADB_SATYPE_UNSPEC
:
1835 switch (msg
->sadb_msg_type
) {
1843 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1847 case SADB_SATYPE_ESP
:
1848 case SADB_SATYPE_AH
:
1849 case SADB_X_SATYPE_IPCOMP
:
1850 case SADB_X_SATYPE_TCPSIGNATURE
:
1851 switch (msg
->sadb_msg_type
) {
1853 case SADB_X_SPDDELETE
:
1855 case SADB_X_SPDDUMP
:
1856 case SADB_X_SPDFLUSH
:
1857 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1861 case SADB_SATYPE_RSVP
:
1862 case SADB_SATYPE_OSPFV2
:
1863 case SADB_SATYPE_RIPV2
:
1864 case SADB_SATYPE_MIP
:
1865 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1867 case 1: /* XXX: What does it do ? */
1868 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1872 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1876 /* check field of upper layer protocol and address family */
1877 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1878 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1879 struct sadb_address
*src0
, *dst0
;
1881 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1882 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1884 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1885 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1889 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1890 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1891 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1895 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1900 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1905 * prefixlen == 0 is valid because there must be the case
1906 * all addresses are matched.
1910 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1915 * set data into sadb_msg.
1916 * `buf' must has been allocated sufficiently.
1919 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1930 p
= (struct sadb_msg
*)buf
;
1931 len
= sizeof(struct sadb_msg
);
1933 if (buf
+ len
> lim
)
1937 p
->sadb_msg_version
= PF_KEY_V2
;
1938 p
->sadb_msg_type
= type
;
1939 p
->sadb_msg_errno
= 0;
1940 p
->sadb_msg_satype
= satype
;
1941 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1942 p
->sadb_msg_reserved
= 0;
1943 p
->sadb_msg_seq
= seq
;
1944 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1950 * copy secasvar data into sadb_address.
1951 * `buf' must has been allocated sufficiently.
1954 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1957 u_int32_t spi
, flags
;
1958 u_int wsize
, auth
, enc
;
1963 p
= (struct sadb_sa
*)buf
;
1964 len
= sizeof(struct sadb_sa
);
1966 if (buf
+ len
> lim
)
1970 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1971 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1972 p
->sadb_sa_spi
= spi
;
1973 p
->sadb_sa_replay
= wsize
;
1974 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1975 p
->sadb_sa_auth
= auth
;
1976 p
->sadb_sa_encrypt
= enc
;
1977 p
->sadb_sa_flags
= flags
;
1983 * set data into sadb_address.
1984 * `buf' must has been allocated sufficiently.
1985 * prefixlen is in bits.
1988 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
1992 struct sockaddr
*saddr
;
1996 struct sadb_address
*p
;
1999 p
= (struct sadb_address
*)buf
;
2000 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
2002 if (buf
+ len
> lim
)
2006 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2007 p
->sadb_address_exttype
= exttype
& 0xffff;
2008 p
->sadb_address_proto
= ul_proto
& 0xff;
2009 p
->sadb_address_prefixlen
= prefixlen
;
2010 p
->sadb_address_reserved
= 0;
2012 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2018 * set sadb_key structure after clearing buffer with zero.
2019 * OUT: the pointer of buf + len.
2022 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2031 p
= (struct sadb_key
*)buf
;
2032 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2034 if (buf
+ len
> lim
)
2038 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2039 p
->sadb_key_exttype
= type
;
2040 p
->sadb_key_bits
= keylen
<< 3;
2041 p
->sadb_key_reserved
= 0;
2043 memcpy(p
+ 1, key
, keylen
);
2049 * set sadb_lifetime structure after clearing buffer with zero.
2050 * OUT: the pointer of buf + len.
2053 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2057 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2059 struct sadb_lifetime
*p
;
2062 p
= (struct sadb_lifetime
*)buf
;
2063 len
= sizeof(struct sadb_lifetime
);
2065 if (buf
+ len
> lim
)
2069 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2070 p
->sadb_lifetime_exttype
= type
;
2073 case SADB_EXT_LIFETIME_SOFT
:
2074 p
->sadb_lifetime_allocations
2075 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2076 p
->sadb_lifetime_bytes
2077 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2078 p
->sadb_lifetime_addtime
2079 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2080 p
->sadb_lifetime_usetime
2081 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2083 case SADB_EXT_LIFETIME_HARD
:
2084 p
->sadb_lifetime_allocations
= l_alloc
;
2085 p
->sadb_lifetime_bytes
= l_bytes
;
2086 p
->sadb_lifetime_addtime
= l_addtime
;
2087 p
->sadb_lifetime_usetime
= l_usetime
;
2095 * copy secasvar data into sadb_address.
2096 * `buf' must has been allocated sufficiently.
2099 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2105 struct sadb_x_sa2
*p
;
2106 u_int8_t mode
= mode0
& 0xff;
2109 p
= (struct sadb_x_sa2
*)buf
;
2110 len
= sizeof(struct sadb_x_sa2
);
2112 if (buf
+ len
> lim
)
2116 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2117 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2118 p
->sadb_x_sa2_mode
= mode
;
2119 p
->sadb_x_sa2_reqid
= reqid
;