2 * Copyright (c) 2003-2007 Apple Computer, Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 /* $FreeBSD: src/lib/libipsec/pfkey.c,v 1.1.2.2 2001/07/03 11:01:14 ume Exp $ */
17 /* $KAME: pfkey.c,v 1.39 2001/03/05 18:22:17 thorpej Exp $ */
20 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the project nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <net/pfkeyv2.h>
52 #include <netinet/in.h>
53 #include <netinet6/ipsec.h>
61 #include "ipsec_strerror.h"
63 #include "ipsec_options.h"
67 #define CALLOC(size, cast) (cast)calloc(1, (size))
69 static int findsupportedmap
__P((int));
70 static int setsupportedmap
__P((struct sadb_supported
*));
71 static struct sadb_alg
*findsupportedalg
__P((u_int
, u_int
));
72 static int pfkey_send_x1
__P((int, u_int
, u_int
, u_int
, struct sockaddr
*,
73 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
74 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
75 u_int32_t
, u_int32_t
, u_int32_t
));
76 static int pfkey_send_x2
__P((int, u_int
, u_int
, u_int
,
77 struct sockaddr
*, struct sockaddr
*, u_int32_t
));
78 static int pfkey_send_x3
__P((int, u_int
, u_int
));
79 static int pfkey_send_x4
__P((int, u_int
, struct sockaddr
*, u_int
,
80 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
81 char *, int, u_int32_t
));
82 static int pfkey_send_x5
__P((int, u_int
, u_int32_t
));
84 static caddr_t pfkey_setsadbmsg
__P((caddr_t
, caddr_t
, u_int
, u_int
,
85 u_int
, u_int32_t
, pid_t
));
86 static caddr_t pfkey_setsadbsa
__P((caddr_t
, caddr_t
, u_int32_t
, u_int
,
87 u_int
, u_int
, u_int32_t
));
88 static caddr_t pfkey_setsadbaddr
__P((caddr_t
, caddr_t
, u_int
,
89 struct sockaddr
*, u_int
, u_int
));
90 static caddr_t pfkey_setsadbkey
__P((caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
));
91 static caddr_t pfkey_setsadblifetime
__P((caddr_t
, caddr_t
, u_int
, u_int32_t
,
92 u_int32_t
, u_int32_t
, u_int32_t
));
93 static caddr_t pfkey_setsadbxsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int32_t
));
96 * make and search supported algorithm structure.
98 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
100 static int supported_map
[] = {
103 SADB_X_SATYPE_IPCOMP
,
107 findsupportedmap(satype
)
112 for (i
= 0; (unsigned int)i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
113 if (supported_map
[i
] == satype
)
118 static struct sadb_alg
*
119 findsupportedalg(satype
, alg_id
)
120 u_int satype
, alg_id
;
127 algno
= findsupportedmap(satype
);
129 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
132 if (ipsec_supported
[algno
] == NULL
) {
133 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
137 tlen
= ipsec_supported
[algno
]->sadb_supported_len
138 - sizeof(struct sadb_supported
);
139 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
141 if ((unsigned int)tlen
< sizeof(struct sadb_alg
)) {
145 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
146 return (struct sadb_alg
*)p
;
148 tlen
-= sizeof(struct sadb_alg
);
149 p
+= sizeof(struct sadb_alg
);
152 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
158 struct sadb_supported
*sup
;
160 struct sadb_supported
**ipsup
;
162 switch (sup
->sadb_supported_exttype
) {
163 case SADB_EXT_SUPPORTED_AUTH
:
164 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
166 case SADB_EXT_SUPPORTED_ENCRYPT
:
167 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
170 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
177 *ipsup
= malloc(sup
->sadb_supported_len
);
179 __ipsec_set_strerror(strerror(errno
));
182 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
188 * check key length against algorithm specified.
189 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
190 * augument, and only calls to ipsec_check_keylen2();
191 * keylen is the unit of bit.
197 ipsec_check_keylen(supported
, alg_id
, keylen
)
206 case SADB_EXT_SUPPORTED_AUTH
:
207 satype
= SADB_SATYPE_AH
;
209 case SADB_EXT_SUPPORTED_ENCRYPT
:
210 satype
= SADB_SATYPE_ESP
;
213 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
217 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
221 * check key length against algorithm specified.
222 * satype is one of satype defined at pfkeyv2.h.
223 * keylen is the unit of bit.
229 ipsec_check_keylen2(satype
, alg_id
, keylen
)
234 struct sadb_alg
*alg
;
236 alg
= findsupportedalg(satype
, alg_id
);
240 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
241 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
245 __ipsec_errcode
= EIPSEC_NO_ERROR
;
250 * get max/min key length against algorithm specified.
251 * satype is one of satype defined at pfkeyv2.h.
252 * keylen is the unit of bit.
258 ipsec_get_keylen(supported
, alg_id
, alg0
)
259 u_int supported
, alg_id
;
260 struct sadb_alg
*alg0
;
262 struct sadb_alg
*alg
;
267 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
272 case SADB_EXT_SUPPORTED_AUTH
:
273 satype
= SADB_SATYPE_AH
;
275 case SADB_EXT_SUPPORTED_ENCRYPT
:
276 satype
= SADB_SATYPE_ESP
;
279 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
283 alg
= findsupportedalg(satype
, alg_id
);
287 memcpy(alg0
, alg
, sizeof(*alg0
));
289 __ipsec_errcode
= EIPSEC_NO_ERROR
;
294 * set the rate for SOFT lifetime against HARD one.
295 * If rate is more than 100 or equal to zero, then set to 100.
297 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
298 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
299 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
300 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
303 pfkey_set_softrate(type
, rate
)
306 __ipsec_errcode
= EIPSEC_NO_ERROR
;
308 if (rate
> 100 || rate
== 0)
312 case SADB_X_LIFETIME_ALLOCATIONS
:
313 soft_lifetime_allocations_rate
= rate
;
315 case SADB_X_LIFETIME_BYTES
:
316 soft_lifetime_bytes_rate
= rate
;
318 case SADB_X_LIFETIME_ADDTIME
:
319 soft_lifetime_addtime_rate
= rate
;
321 case SADB_X_LIFETIME_USETIME
:
322 soft_lifetime_usetime_rate
= rate
;
326 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
331 * get current rate for SOFT lifetime against HARD one.
332 * ATTENTION: ~0 is returned if invalid type was passed.
335 pfkey_get_softrate(type
)
339 case SADB_X_LIFETIME_ALLOCATIONS
:
340 return soft_lifetime_allocations_rate
;
341 case SADB_X_LIFETIME_BYTES
:
342 return soft_lifetime_bytes_rate
;
343 case SADB_X_LIFETIME_ADDTIME
:
344 return soft_lifetime_addtime_rate
;
345 case SADB_X_LIFETIME_USETIME
:
346 return soft_lifetime_usetime_rate
;
353 * sending SADB_GETSPI message to the kernel.
355 * positive: success and return length sent.
356 * -1 : error occured, and set errno.
359 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
362 struct sockaddr
*src
, *dst
;
363 u_int32_t min
, max
, reqid
, seq
;
365 struct sadb_msg
*newmsg
;
368 int need_spirange
= 0;
373 if (src
== NULL
|| dst
== NULL
) {
374 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
377 if (src
->sa_family
!= dst
->sa_family
) {
378 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
381 if (min
> max
|| (min
> 0 && min
<= 255)) {
382 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
385 switch (src
->sa_family
) {
387 plen
= sizeof(struct in_addr
) << 3;
390 plen
= sizeof(struct in6_addr
) << 3;
393 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
397 /* create new sadb_msg to send. */
398 len
= sizeof(struct sadb_msg
)
399 + sizeof(struct sadb_x_sa2
)
400 + sizeof(struct sadb_address
)
401 + PFKEY_ALIGN8(src
->sa_len
)
402 + sizeof(struct sadb_address
)
403 + PFKEY_ALIGN8(dst
->sa_len
);
405 if (min
> (u_int32_t
)255 && max
< (u_int32_t
)~0) {
407 len
+= sizeof(struct sadb_spirange
);
410 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
411 __ipsec_set_strerror(strerror(errno
));
414 ep
= ((caddr_t
)newmsg
) + len
;
416 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
417 len
, satype
, seq
, getpid());
423 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
429 /* set sadb_address for source */
430 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
437 /* set sadb_address for destination */
438 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
445 /* proccessing spi range */
447 struct sadb_spirange spirange
;
449 if (p
+ sizeof(spirange
) > ep
) {
454 memset(&spirange
, 0, sizeof(spirange
));
455 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
456 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
457 spirange
.sadb_spirange_min
= min
;
458 spirange
.sadb_spirange_max
= max
;
460 memcpy(p
, &spirange
, sizeof(spirange
));
462 p
+= sizeof(spirange
);
470 len
= pfkey_send(so
, newmsg
, len
);
476 __ipsec_errcode
= EIPSEC_NO_ERROR
;
481 * sending SADB_UPDATE message to the kernel.
482 * The length of key material is a_keylen + e_keylen.
484 * positive: success and return length sent.
485 * -1 : error occured, and set errno.
488 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
489 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
490 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
492 u_int satype
, mode
, wsize
;
493 struct sockaddr
*src
, *dst
;
494 u_int32_t spi
, reqid
;
496 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
498 u_int64_t l_bytes
, l_addtime
, l_usetime
;
502 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
504 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
505 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
512 * sending SADB_ADD message to the kernel.
513 * The length of key material is a_keylen + e_keylen.
515 * positive: success and return length sent.
516 * -1 : error occured, and set errno.
519 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
520 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
521 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
523 u_int satype
, mode
, wsize
;
524 struct sockaddr
*src
, *dst
;
525 u_int32_t spi
, reqid
;
527 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
529 u_int64_t l_bytes
, l_addtime
, l_usetime
;
533 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
535 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
536 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
543 * sending SADB_DELETE message to the kernel.
545 * positive: success and return length sent.
546 * -1 : error occured, and set errno.
549 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
552 struct sockaddr
*src
, *dst
;
556 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
563 * sending SADB_DELETE without spi to the kernel. This is
564 * the "delete all" request (an extension also present in
568 * positive: success and return length sent
569 * -1 : error occured, and set errno
572 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
575 struct sockaddr
*src
, *dst
;
577 struct sadb_msg
*newmsg
;
586 if (src
== NULL
|| dst
== NULL
) {
587 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
590 if (src
->sa_family
!= dst
->sa_family
) {
591 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
594 switch (src
->sa_family
) {
596 plen
= sizeof(struct in_addr
) << 3;
599 plen
= sizeof(struct in6_addr
) << 3;
602 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
606 /* create new sadb_msg to reply. */
607 len
= sizeof(struct sadb_msg
)
608 + sizeof(struct sadb_address
)
609 + PFKEY_ALIGN8(src
->sa_len
)
610 + sizeof(struct sadb_address
)
611 + PFKEY_ALIGN8(dst
->sa_len
);
613 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
614 __ipsec_set_strerror(strerror(errno
));
617 ep
= ((caddr_t
)newmsg
) + len
;
619 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
625 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
631 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
639 len
= pfkey_send(so
, newmsg
, len
);
645 __ipsec_errcode
= EIPSEC_NO_ERROR
;
650 * sending SADB_GET message to the kernel.
652 * positive: success and return length sent.
653 * -1 : error occured, and set errno.
656 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
659 struct sockaddr
*src
, *dst
;
663 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
670 * sending SADB_REGISTER message to the kernel.
672 * positive: success and return length sent.
673 * -1 : error occured, and set errno.
676 pfkey_send_register(so
, satype
)
682 if (satype
== PF_UNSPEC
) {
684 (unsigned int)algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
686 if (ipsec_supported
[algno
]) {
687 free(ipsec_supported
[algno
]);
688 ipsec_supported
[algno
] = NULL
;
692 algno
= findsupportedmap(satype
);
694 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
698 if (ipsec_supported
[algno
]) {
699 free(ipsec_supported
[algno
]);
700 ipsec_supported
[algno
] = NULL
;
704 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
711 * receiving SADB_REGISTER message from the kernel, and copy buffer for
712 * sadb_supported returned into ipsec_supported.
714 * 0: success and return length sent.
715 * -1: error occured, and set errno.
718 pfkey_recv_register(so
)
721 pid_t pid
= getpid();
722 struct sadb_msg
*newmsg
;
725 /* receive message */
727 if ((newmsg
= pfkey_recv(so
)) == NULL
)
729 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
730 || (pid_t
)newmsg
->sadb_msg_pid
!= pid
);
733 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
735 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
739 __ipsec_errcode
= EIPSEC_NO_ERROR
;
745 * receiving SADB_REGISTER message from the kernel, and copy buffer for
746 * sadb_supported returned into ipsec_supported.
747 * NOTE: sadb_msg_len must be host order.
749 * tlen: msg length, it's to makeing sure.
751 * 0: success and return length sent.
752 * -1: error occured, and set errno.
755 pfkey_set_supported(msg
, tlen
)
756 struct sadb_msg
*msg
;
759 struct sadb_supported
*sup
;
764 if (msg
->sadb_msg_len
!= tlen
) {
765 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
772 p
+= sizeof(struct sadb_msg
);
775 sup
= (struct sadb_supported
*)p
;
776 if (ep
< p
+ sizeof(*sup
) ||
777 (size_t)PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
778 ep
< p
+ sup
->sadb_supported_len
) {
783 switch (sup
->sadb_supported_exttype
) {
784 case SADB_EXT_SUPPORTED_AUTH
:
785 case SADB_EXT_SUPPORTED_ENCRYPT
:
788 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
793 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
795 /* set supported map */
796 if (setsupportedmap(sup
) != 0)
799 p
+= sup
->sadb_supported_len
;
803 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
807 __ipsec_errcode
= EIPSEC_NO_ERROR
;
813 * sending SADB_FLUSH message to the kernel.
815 * positive: success and return length sent.
816 * -1 : error occured, and set errno.
819 pfkey_send_flush(so
, satype
)
825 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
832 * sending SADB_DUMP message to the kernel.
834 * positive: success and return length sent.
835 * -1 : error occured, and set errno.
838 pfkey_send_dump(so
, satype
)
844 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
851 * sending SADB_X_PROMISC message to the kernel.
852 * NOTE that this function handles promisc mode toggle only.
854 * flag: set promisc off if zero, set promisc on if non-zero.
856 * positive: success and return length sent.
857 * -1 : error occured, and set errno.
858 * 0 : error occured, and set errno.
859 * others: a pointer to new allocated buffer in which supported
863 pfkey_send_promisc_toggle(so
, flag
)
869 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
876 * sending SADB_X_SPDADD message to the kernel.
878 * positive: success and return length sent.
879 * -1 : error occured, and set errno.
882 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
884 struct sockaddr
*src
, *dst
;
885 u_int prefs
, prefd
, proto
;
892 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
893 src
, prefs
, dst
, prefd
, proto
,
895 policy
, policylen
, seq
)) < 0)
902 * sending SADB_X_SPDADD message to the kernel.
904 * positive: success and return length sent.
905 * -1 : error occured, and set errno.
908 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
909 policy
, policylen
, seq
)
911 struct sockaddr
*src
, *dst
;
912 u_int prefs
, prefd
, proto
;
913 u_int64_t ltime
, vtime
;
920 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
921 src
, prefs
, dst
, prefd
, proto
,
923 policy
, policylen
, seq
)) < 0)
930 * sending SADB_X_SPDUPDATE message to the kernel.
932 * positive: success and return length sent.
933 * -1 : error occured, and set errno.
936 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
938 struct sockaddr
*src
, *dst
;
939 u_int prefs
, prefd
, proto
;
946 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
947 src
, prefs
, dst
, prefd
, proto
,
949 policy
, policylen
, seq
)) < 0)
956 * sending SADB_X_SPDUPDATE message to the kernel.
958 * positive: success and return length sent.
959 * -1 : error occured, and set errno.
962 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
963 policy
, policylen
, seq
)
965 struct sockaddr
*src
, *dst
;
966 u_int prefs
, prefd
, proto
;
967 u_int64_t ltime
, vtime
;
974 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
975 src
, prefs
, dst
, prefd
, proto
,
977 policy
, policylen
, seq
)) < 0)
984 * sending SADB_X_SPDDELETE message to the kernel.
986 * positive: success and return length sent.
987 * -1 : error occured, and set errno.
990 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
992 struct sockaddr
*src
, *dst
;
993 u_int prefs
, prefd
, proto
;
1000 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1001 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1005 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
1006 src
, prefs
, dst
, prefd
, proto
,
1008 policy
, policylen
, seq
)) < 0)
1015 * sending SADB_X_SPDDELETE message to the kernel.
1017 * positive: success and return length sent.
1018 * -1 : error occured, and set errno.
1021 pfkey_send_spddelete2(so
, spid
)
1027 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1034 * sending SADB_X_SPDGET message to the kernel.
1036 * positive: success and return length sent.
1037 * -1 : error occured, and set errno.
1040 pfkey_send_spdget(so
, spid
)
1046 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1053 * sending SADB_X_SPDSETIDX message to the kernel.
1055 * positive: success and return length sent.
1056 * -1 : error occured, and set errno.
1059 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1061 struct sockaddr
*src
, *dst
;
1062 u_int prefs
, prefd
, proto
;
1069 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1070 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1074 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1075 src
, prefs
, dst
, prefd
, proto
,
1077 policy
, policylen
, seq
)) < 0)
1084 * sending SADB_SPDFLUSH message to the kernel.
1086 * positive: success and return length sent.
1087 * -1 : error occured, and set errno.
1090 pfkey_send_spdflush(so
)
1095 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1102 * sending SADB_SPDDUMP message to the kernel.
1104 * positive: success and return length sent.
1105 * -1 : error occured, and set errno.
1108 pfkey_send_spddump(so
)
1113 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1119 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1121 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1122 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1123 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1125 u_int type
, satype
, mode
;
1126 struct sockaddr
*src
, *dst
;
1127 u_int32_t spi
, reqid
;
1130 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1131 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1133 struct sadb_msg
*newmsg
;
1139 /* validity check */
1140 if (src
== NULL
|| dst
== NULL
) {
1141 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1144 if (src
->sa_family
!= dst
->sa_family
) {
1145 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1148 switch (src
->sa_family
) {
1150 plen
= sizeof(struct in_addr
) << 3;
1153 plen
= sizeof(struct in6_addr
) << 3;
1156 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1161 case SADB_SATYPE_ESP
:
1162 if (e_type
== SADB_EALG_NONE
) {
1163 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1167 case SADB_SATYPE_AH
:
1168 if (e_type
!= SADB_EALG_NONE
) {
1169 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1172 if (a_type
== SADB_AALG_NONE
) {
1173 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1177 case SADB_X_SATYPE_IPCOMP
:
1178 if (e_type
== SADB_X_CALG_NONE
) {
1179 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1182 if (a_type
!= SADB_AALG_NONE
) {
1183 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1188 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1192 /* create new sadb_msg to reply. */
1193 len
= sizeof(struct sadb_msg
)
1194 + sizeof(struct sadb_sa
)
1195 + sizeof(struct sadb_x_sa2
)
1196 + sizeof(struct sadb_address
)
1197 + PFKEY_ALIGN8(src
->sa_len
)
1198 + sizeof(struct sadb_address
)
1199 + PFKEY_ALIGN8(dst
->sa_len
)
1200 + sizeof(struct sadb_lifetime
)
1201 + sizeof(struct sadb_lifetime
);
1203 if (e_type
!= SADB_EALG_NONE
)
1204 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1205 if (a_type
!= SADB_AALG_NONE
)
1206 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1208 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1209 __ipsec_set_strerror(strerror(errno
));
1212 ep
= ((caddr_t
)newmsg
) + len
;
1214 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1215 satype
, seq
, getpid());
1220 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1225 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1230 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1236 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1243 if (e_type
!= SADB_EALG_NONE
) {
1244 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1251 if (a_type
!= SADB_AALG_NONE
) {
1252 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1253 keymat
+ e_keylen
, a_keylen
);
1260 /* set sadb_lifetime for destination */
1261 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1262 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1267 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1268 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1269 if (!p
|| p
!= ep
) {
1275 len
= pfkey_send(so
, newmsg
, len
);
1281 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1285 /* sending SADB_DELETE or SADB_GET message to the kernel */
1287 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1289 u_int type
, satype
, mode
;
1290 struct sockaddr
*src
, *dst
;
1293 struct sadb_msg
*newmsg
;
1301 /* validity check */
1302 if (src
== NULL
|| dst
== NULL
) {
1303 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1306 if (src
->sa_family
!= dst
->sa_family
) {
1307 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1310 switch (src
->sa_family
) {
1312 plen
= sizeof(struct in_addr
) << 3;
1315 plen
= sizeof(struct in6_addr
) << 3;
1318 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1322 /* create new sadb_msg to reply. */
1323 len
= sizeof(struct sadb_msg
)
1324 + sizeof(struct sadb_sa
)
1325 + sizeof(struct sadb_address
)
1326 + PFKEY_ALIGN8(src
->sa_len
)
1327 + sizeof(struct sadb_address
)
1328 + PFKEY_ALIGN8(dst
->sa_len
);
1330 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1331 __ipsec_set_strerror(strerror(errno
));
1334 ep
= ((caddr_t
)newmsg
) + len
;
1336 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1342 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1347 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1353 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1355 if (!p
|| p
!= ep
) {
1361 len
= pfkey_send(so
, newmsg
, len
);
1367 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1372 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1376 pfkey_send_x3(so
, type
, satype
)
1380 struct sadb_msg
*newmsg
;
1385 /* validity check */
1387 case SADB_X_PROMISC
:
1388 if (satype
!= 0 && satype
!= 1) {
1389 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1395 case SADB_SATYPE_UNSPEC
:
1396 case SADB_SATYPE_AH
:
1397 case SADB_SATYPE_ESP
:
1398 case SADB_X_SATYPE_IPCOMP
:
1401 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1406 /* create new sadb_msg to send. */
1407 len
= sizeof(struct sadb_msg
);
1409 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1410 __ipsec_set_strerror(strerror(errno
));
1413 ep
= ((caddr_t
)newmsg
) + len
;
1415 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1417 if (!p
|| p
!= ep
) {
1423 len
= pfkey_send(so
, newmsg
, len
);
1429 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1433 /* sending SADB_X_SPDADD message to the kernel */
1435 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1436 ltime
, vtime
, policy
, policylen
, seq
)
1438 struct sockaddr
*src
, *dst
;
1439 u_int type
, prefs
, prefd
, proto
;
1440 u_int64_t ltime
, vtime
;
1445 struct sadb_msg
*newmsg
;
1451 /* validity check */
1452 if (src
== NULL
|| dst
== NULL
) {
1453 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1456 if (src
->sa_family
!= dst
->sa_family
) {
1457 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1461 switch (src
->sa_family
) {
1463 plen
= sizeof(struct in_addr
) << 3;
1466 plen
= sizeof(struct in6_addr
) << 3;
1469 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1472 if (prefs
> (u_int
)plen
|| prefd
> (u_int
)plen
) {
1473 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1477 /* create new sadb_msg to reply. */
1478 len
= sizeof(struct sadb_msg
)
1479 + sizeof(struct sadb_address
)
1480 + PFKEY_ALIGN8(src
->sa_len
)
1481 + sizeof(struct sadb_address
)
1482 + PFKEY_ALIGN8(src
->sa_len
)
1483 + sizeof(struct sadb_lifetime
)
1486 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1487 __ipsec_set_strerror(strerror(errno
));
1490 ep
= ((caddr_t
)newmsg
) + len
;
1492 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1493 SADB_SATYPE_UNSPEC
, seq
, getpid());
1498 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1503 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1508 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1509 0, 0, ltime
, vtime
);
1510 if (!p
|| p
+ policylen
!= ep
) {
1514 memcpy(p
, policy
, policylen
);
1517 len
= pfkey_send(so
, newmsg
, len
);
1523 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1527 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1529 pfkey_send_x5(so
, type
, spid
)
1534 struct sadb_msg
*newmsg
;
1535 struct sadb_x_policy xpl
;
1540 /* create new sadb_msg to reply. */
1541 len
= sizeof(struct sadb_msg
)
1544 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1545 __ipsec_set_strerror(strerror(errno
));
1548 ep
= ((caddr_t
)newmsg
) + len
;
1550 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1551 SADB_SATYPE_UNSPEC
, 0, getpid());
1557 if (p
+ sizeof(xpl
) != ep
) {
1561 memset(&xpl
, 0, sizeof(xpl
));
1562 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1563 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1564 xpl
.sadb_x_policy_id
= spid
;
1565 memcpy(p
, &xpl
, sizeof(xpl
));
1568 len
= pfkey_send(so
, newmsg
, len
);
1574 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1582 * others : success and return value of socket.
1588 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1590 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1591 __ipsec_set_strerror(strerror(errno
));
1596 * This is a temporary workaround for KAME PR 154.
1597 * Don't really care even if it fails.
1599 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1600 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1602 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1618 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1623 * receive sadb_msg data, and return pointer to new buffer allocated.
1624 * Must free this buffer later.
1626 * NULL : error occured.
1627 * others : a pointer to sadb_msg structure.
1629 * XXX should be rewritten to pass length explicitly
1635 struct sadb_msg buf
, *newmsg
;
1638 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1641 __ipsec_set_strerror(strerror(errno
));
1645 if ((size_t)len
< sizeof(buf
)) {
1646 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1647 __ipsec_errcode
= EIPSEC_MAX
;
1651 /* read real message */
1652 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1653 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1654 __ipsec_set_strerror(strerror(errno
));
1658 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1661 __ipsec_set_strerror(strerror(errno
));
1666 if (len
!= reallen
) {
1667 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1672 /* don't trust what the kernel says, validate! */
1673 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1674 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1679 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1684 * send message to a socket.
1686 * others: success and return length sent.
1690 pfkey_send(so
, msg
, len
)
1692 struct sadb_msg
*msg
;
1695 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1696 __ipsec_set_strerror(strerror(errno
));
1700 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1706 * NOTE: These functions are derived from netkey/key.c in KAME.
1709 * set the pointer to each header in this message buffer.
1710 * IN: msg: pointer to message buffer.
1711 * mhp: pointer to the buffer initialized like below:
1712 * caddr_t mhp[SADB_EXT_MAX + 1];
1716 * XXX should be rewritten to obtain length explicitly
1719 pfkey_align(msg
, mhp
)
1720 struct sadb_msg
*msg
;
1723 struct sadb_ext
*ext
;
1726 caddr_t ep
; /* XXX should be passed from upper layer */
1728 /* validity check */
1729 if (msg
== NULL
|| mhp
== NULL
) {
1730 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1735 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1738 mhp
[0] = (caddr_t
)msg
;
1742 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1744 /* skip base header */
1745 p
+= sizeof(struct sadb_msg
);
1748 ext
= (struct sadb_ext
*)p
;
1749 if (ep
< p
+ sizeof(*ext
) || (size_t)PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1750 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1751 /* invalid format */
1755 /* duplicate check */
1756 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1757 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1758 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1763 switch (ext
->sadb_ext_type
) {
1765 case SADB_EXT_LIFETIME_CURRENT
:
1766 case SADB_EXT_LIFETIME_HARD
:
1767 case SADB_EXT_LIFETIME_SOFT
:
1768 case SADB_EXT_ADDRESS_SRC
:
1769 case SADB_EXT_ADDRESS_DST
:
1770 case SADB_EXT_ADDRESS_PROXY
:
1771 case SADB_EXT_KEY_AUTH
:
1772 /* XXX should to be check weak keys. */
1773 case SADB_EXT_KEY_ENCRYPT
:
1774 /* XXX should to be check weak keys. */
1775 case SADB_EXT_IDENTITY_SRC
:
1776 case SADB_EXT_IDENTITY_DST
:
1777 case SADB_EXT_SENSITIVITY
:
1778 case SADB_EXT_PROPOSAL
:
1779 case SADB_EXT_SUPPORTED_AUTH
:
1780 case SADB_EXT_SUPPORTED_ENCRYPT
:
1781 case SADB_EXT_SPIRANGE
:
1782 case SADB_X_EXT_POLICY
:
1783 case SADB_X_EXT_SA2
:
1784 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1787 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1791 p
+= PFKEY_EXTLEN(ext
);
1795 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1799 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1804 * check basic usage for sadb_msg,
1805 * NOTE: This routine is derived from netkey/key.c in KAME.
1806 * IN: msg: pointer to message buffer.
1807 * mhp: pointer to the buffer initialized like below:
1809 * caddr_t mhp[SADB_EXT_MAX + 1];
1818 struct sadb_msg
*msg
;
1820 /* validity check */
1821 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1822 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1826 msg
= (struct sadb_msg
*)mhp
[0];
1829 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1830 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1835 if (msg
->sadb_msg_type
> SADB_MAX
) {
1836 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1841 switch (msg
->sadb_msg_satype
) {
1842 case SADB_SATYPE_UNSPEC
:
1843 switch (msg
->sadb_msg_type
) {
1851 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1855 case SADB_SATYPE_ESP
:
1856 case SADB_SATYPE_AH
:
1857 case SADB_X_SATYPE_IPCOMP
:
1858 switch (msg
->sadb_msg_type
) {
1860 case SADB_X_SPDDELETE
:
1862 case SADB_X_SPDDUMP
:
1863 case SADB_X_SPDFLUSH
:
1864 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1868 case SADB_SATYPE_RSVP
:
1869 case SADB_SATYPE_OSPFV2
:
1870 case SADB_SATYPE_RIPV2
:
1871 case SADB_SATYPE_MIP
:
1872 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1874 case 1: /* XXX: What does it do ? */
1875 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1879 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1883 /* check field of upper layer protocol and address family */
1884 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1885 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1886 struct sadb_address
*src0
, *dst0
;
1888 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1889 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1891 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1892 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1896 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1897 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1898 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1902 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1907 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1912 * prefixlen == 0 is valid because there must be the case
1913 * all addresses are matched.
1917 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1922 * set data into sadb_msg.
1923 * `buf' must has been allocated sufficiently.
1926 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1937 p
= (struct sadb_msg
*)buf
;
1938 len
= sizeof(struct sadb_msg
);
1940 if (buf
+ len
> lim
)
1944 p
->sadb_msg_version
= PF_KEY_V2
;
1945 p
->sadb_msg_type
= type
;
1946 p
->sadb_msg_errno
= 0;
1947 p
->sadb_msg_satype
= satype
;
1948 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1949 p
->sadb_msg_reserved
= 0;
1950 p
->sadb_msg_seq
= seq
;
1951 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1957 * copy secasvar data into sadb_address.
1958 * `buf' must has been allocated sufficiently.
1961 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1964 u_int32_t spi
, flags
;
1965 u_int wsize
, auth
, enc
;
1970 p
= (struct sadb_sa
*)buf
;
1971 len
= sizeof(struct sadb_sa
);
1973 if (buf
+ len
> lim
)
1977 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1978 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1979 p
->sadb_sa_spi
= spi
;
1980 p
->sadb_sa_replay
= wsize
;
1981 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1982 p
->sadb_sa_auth
= auth
;
1983 p
->sadb_sa_encrypt
= enc
;
1984 p
->sadb_sa_flags
= flags
;
1990 * set data into sadb_address.
1991 * `buf' must has been allocated sufficiently.
1992 * prefixlen is in bits.
1995 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
1999 struct sockaddr
*saddr
;
2003 struct sadb_address
*p
;
2006 p
= (struct sadb_address
*)buf
;
2007 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
2009 if (buf
+ len
> lim
)
2013 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2014 p
->sadb_address_exttype
= exttype
& 0xffff;
2015 p
->sadb_address_proto
= ul_proto
& 0xff;
2016 p
->sadb_address_prefixlen
= prefixlen
;
2017 p
->sadb_address_reserved
= 0;
2019 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2025 * set sadb_key structure after clearing buffer with zero.
2026 * OUT: the pointer of buf + len.
2029 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2038 p
= (struct sadb_key
*)buf
;
2039 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2041 if (buf
+ len
> lim
)
2045 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2046 p
->sadb_key_exttype
= type
;
2047 p
->sadb_key_bits
= keylen
<< 3;
2048 p
->sadb_key_reserved
= 0;
2050 memcpy(p
+ 1, key
, keylen
);
2056 * set sadb_lifetime structure after clearing buffer with zero.
2057 * OUT: the pointer of buf + len.
2060 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2064 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2066 struct sadb_lifetime
*p
;
2069 p
= (struct sadb_lifetime
*)buf
;
2070 len
= sizeof(struct sadb_lifetime
);
2072 if (buf
+ len
> lim
)
2076 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2077 p
->sadb_lifetime_exttype
= type
;
2080 case SADB_EXT_LIFETIME_SOFT
:
2081 p
->sadb_lifetime_allocations
2082 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2083 p
->sadb_lifetime_bytes
2084 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2085 p
->sadb_lifetime_addtime
2086 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2087 p
->sadb_lifetime_usetime
2088 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2090 case SADB_EXT_LIFETIME_HARD
:
2091 p
->sadb_lifetime_allocations
= l_alloc
;
2092 p
->sadb_lifetime_bytes
= l_bytes
;
2093 p
->sadb_lifetime_addtime
= l_addtime
;
2094 p
->sadb_lifetime_usetime
= l_usetime
;
2102 * copy secasvar data into sadb_address.
2103 * `buf' must has been allocated sufficiently.
2106 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2112 struct sadb_x_sa2
*p
;
2113 u_int8_t mode
= mode0
& 0xff;
2116 p
= (struct sadb_x_sa2
*)buf
;
2117 len
= sizeof(struct sadb_x_sa2
);
2119 if (buf
+ len
> lim
)
2123 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2124 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2125 p
->sadb_x_sa2_mode
= mode
;
2126 p
->sadb_x_sa2_reqid
= reqid
;
2131 #endif /* ndef MDNS_NO_IPSEC */