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"
64 #define CALLOC(size, cast) (cast)calloc(1, (size))
66 static int findsupportedmap
__P((int));
67 static int setsupportedmap
__P((struct sadb_supported
*));
68 static struct sadb_alg
*findsupportedalg
__P((u_int
, u_int
));
69 static int pfkey_send_x1
__P((int, u_int
, u_int
, u_int
, struct sockaddr
*,
70 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
71 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
72 u_int32_t
, u_int32_t
, u_int32_t
));
73 static int pfkey_send_x2
__P((int, u_int
, u_int
, u_int
,
74 struct sockaddr
*, struct sockaddr
*, u_int32_t
));
75 static int pfkey_send_x3
__P((int, u_int
, u_int
));
76 static int pfkey_send_x4
__P((int, u_int
, struct sockaddr
*, u_int
,
77 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
78 char *, int, u_int32_t
));
79 static int pfkey_send_x5
__P((int, u_int
, u_int32_t
));
81 static caddr_t pfkey_setsadbmsg
__P((caddr_t
, caddr_t
, u_int
, u_int
,
82 u_int
, u_int32_t
, pid_t
));
83 static caddr_t pfkey_setsadbsa
__P((caddr_t
, caddr_t
, u_int32_t
, u_int
,
84 u_int
, u_int
, u_int32_t
));
85 static caddr_t pfkey_setsadbaddr
__P((caddr_t
, caddr_t
, u_int
,
86 struct sockaddr
*, u_int
, u_int
));
87 static caddr_t pfkey_setsadbkey
__P((caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
));
88 static caddr_t pfkey_setsadblifetime
__P((caddr_t
, caddr_t
, u_int
, u_int32_t
,
89 u_int32_t
, u_int32_t
, u_int32_t
));
90 static caddr_t pfkey_setsadbxsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int32_t
));
93 * make and search supported algorithm structure.
95 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
97 static int supported_map
[] = {
100 SADB_X_SATYPE_IPCOMP
,
104 findsupportedmap(satype
)
109 for (i
= 0; (unsigned int)i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
110 if (supported_map
[i
] == satype
)
115 static struct sadb_alg
*
116 findsupportedalg(satype
, alg_id
)
117 u_int satype
, alg_id
;
124 algno
= findsupportedmap(satype
);
126 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
129 if (ipsec_supported
[algno
] == NULL
) {
130 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
134 tlen
= ipsec_supported
[algno
]->sadb_supported_len
135 - sizeof(struct sadb_supported
);
136 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
138 if ((unsigned int)tlen
< sizeof(struct sadb_alg
)) {
142 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
143 return (struct sadb_alg
*)p
;
145 tlen
-= sizeof(struct sadb_alg
);
146 p
+= sizeof(struct sadb_alg
);
149 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
155 struct sadb_supported
*sup
;
157 struct sadb_supported
**ipsup
;
159 switch (sup
->sadb_supported_exttype
) {
160 case SADB_EXT_SUPPORTED_AUTH
:
161 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
163 case SADB_EXT_SUPPORTED_ENCRYPT
:
164 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
167 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
174 *ipsup
= malloc(sup
->sadb_supported_len
);
176 __ipsec_set_strerror(strerror(errno
));
179 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
185 * check key length against algorithm specified.
186 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
187 * augument, and only calls to ipsec_check_keylen2();
188 * keylen is the unit of bit.
194 ipsec_check_keylen(supported
, alg_id
, keylen
)
203 case SADB_EXT_SUPPORTED_AUTH
:
204 satype
= SADB_SATYPE_AH
;
206 case SADB_EXT_SUPPORTED_ENCRYPT
:
207 satype
= SADB_SATYPE_ESP
;
210 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
214 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
218 * check key length against algorithm specified.
219 * satype is one of satype defined at pfkeyv2.h.
220 * keylen is the unit of bit.
226 ipsec_check_keylen2(satype
, alg_id
, keylen
)
231 struct sadb_alg
*alg
;
233 alg
= findsupportedalg(satype
, alg_id
);
237 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
238 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
242 __ipsec_errcode
= EIPSEC_NO_ERROR
;
247 * get max/min key length against algorithm specified.
248 * satype is one of satype defined at pfkeyv2.h.
249 * keylen is the unit of bit.
255 ipsec_get_keylen(supported
, alg_id
, alg0
)
256 u_int supported
, alg_id
;
257 struct sadb_alg
*alg0
;
259 struct sadb_alg
*alg
;
264 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
269 case SADB_EXT_SUPPORTED_AUTH
:
270 satype
= SADB_SATYPE_AH
;
272 case SADB_EXT_SUPPORTED_ENCRYPT
:
273 satype
= SADB_SATYPE_ESP
;
276 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
280 alg
= findsupportedalg(satype
, alg_id
);
284 memcpy(alg0
, alg
, sizeof(*alg0
));
286 __ipsec_errcode
= EIPSEC_NO_ERROR
;
291 * set the rate for SOFT lifetime against HARD one.
292 * If rate is more than 100 or equal to zero, then set to 100.
294 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
295 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
296 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
297 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
300 pfkey_set_softrate(type
, rate
)
303 __ipsec_errcode
= EIPSEC_NO_ERROR
;
305 if (rate
> 100 || rate
== 0)
309 case SADB_X_LIFETIME_ALLOCATIONS
:
310 soft_lifetime_allocations_rate
= rate
;
312 case SADB_X_LIFETIME_BYTES
:
313 soft_lifetime_bytes_rate
= rate
;
315 case SADB_X_LIFETIME_ADDTIME
:
316 soft_lifetime_addtime_rate
= rate
;
318 case SADB_X_LIFETIME_USETIME
:
319 soft_lifetime_usetime_rate
= rate
;
323 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
328 * get current rate for SOFT lifetime against HARD one.
329 * ATTENTION: ~0 is returned if invalid type was passed.
332 pfkey_get_softrate(type
)
336 case SADB_X_LIFETIME_ALLOCATIONS
:
337 return soft_lifetime_allocations_rate
;
338 case SADB_X_LIFETIME_BYTES
:
339 return soft_lifetime_bytes_rate
;
340 case SADB_X_LIFETIME_ADDTIME
:
341 return soft_lifetime_addtime_rate
;
342 case SADB_X_LIFETIME_USETIME
:
343 return soft_lifetime_usetime_rate
;
350 * sending SADB_GETSPI message to the kernel.
352 * positive: success and return length sent.
353 * -1 : error occured, and set errno.
356 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
359 struct sockaddr
*src
, *dst
;
360 u_int32_t min
, max
, reqid
, seq
;
362 struct sadb_msg
*newmsg
;
365 int need_spirange
= 0;
370 if (src
== NULL
|| dst
== NULL
) {
371 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
374 if (src
->sa_family
!= dst
->sa_family
) {
375 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
378 if (min
> max
|| (min
> 0 && min
<= 255)) {
379 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
382 switch (src
->sa_family
) {
384 plen
= sizeof(struct in_addr
) << 3;
387 plen
= sizeof(struct in6_addr
) << 3;
390 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
394 /* create new sadb_msg to send. */
395 len
= sizeof(struct sadb_msg
)
396 + sizeof(struct sadb_x_sa2
)
397 + sizeof(struct sadb_address
)
398 + PFKEY_ALIGN8(src
->sa_len
)
399 + sizeof(struct sadb_address
)
400 + PFKEY_ALIGN8(dst
->sa_len
);
402 if (min
> (u_int32_t
)255 && max
< (u_int32_t
)~0) {
404 len
+= sizeof(struct sadb_spirange
);
407 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
408 __ipsec_set_strerror(strerror(errno
));
411 ep
= ((caddr_t
)newmsg
) + len
;
413 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
414 len
, satype
, seq
, getpid());
420 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
426 /* set sadb_address for source */
427 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
434 /* set sadb_address for destination */
435 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
442 /* proccessing spi range */
444 struct sadb_spirange spirange
;
446 if (p
+ sizeof(spirange
) > ep
) {
451 memset(&spirange
, 0, sizeof(spirange
));
452 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
453 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
454 spirange
.sadb_spirange_min
= min
;
455 spirange
.sadb_spirange_max
= max
;
457 memcpy(p
, &spirange
, sizeof(spirange
));
459 p
+= sizeof(spirange
);
467 len
= pfkey_send(so
, newmsg
, len
);
473 __ipsec_errcode
= EIPSEC_NO_ERROR
;
478 * sending SADB_UPDATE message to the kernel.
479 * The length of key material is a_keylen + e_keylen.
481 * positive: success and return length sent.
482 * -1 : error occured, and set errno.
485 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
486 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
487 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
489 u_int satype
, mode
, wsize
;
490 struct sockaddr
*src
, *dst
;
491 u_int32_t spi
, reqid
;
493 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
495 u_int64_t l_bytes
, l_addtime
, l_usetime
;
499 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
501 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
502 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
509 * sending SADB_ADD message to the kernel.
510 * The length of key material is a_keylen + e_keylen.
512 * positive: success and return length sent.
513 * -1 : error occured, and set errno.
516 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
517 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
518 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
520 u_int satype
, mode
, wsize
;
521 struct sockaddr
*src
, *dst
;
522 u_int32_t spi
, reqid
;
524 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
526 u_int64_t l_bytes
, l_addtime
, l_usetime
;
530 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
532 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
533 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
540 * sending SADB_DELETE message to the kernel.
542 * positive: success and return length sent.
543 * -1 : error occured, and set errno.
546 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
549 struct sockaddr
*src
, *dst
;
553 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
560 * sending SADB_DELETE without spi to the kernel. This is
561 * the "delete all" request (an extension also present in
565 * positive: success and return length sent
566 * -1 : error occured, and set errno
569 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
572 struct sockaddr
*src
, *dst
;
574 struct sadb_msg
*newmsg
;
583 if (src
== NULL
|| dst
== NULL
) {
584 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
587 if (src
->sa_family
!= dst
->sa_family
) {
588 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
591 switch (src
->sa_family
) {
593 plen
= sizeof(struct in_addr
) << 3;
596 plen
= sizeof(struct in6_addr
) << 3;
599 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
603 /* create new sadb_msg to reply. */
604 len
= sizeof(struct sadb_msg
)
605 + sizeof(struct sadb_address
)
606 + PFKEY_ALIGN8(src
->sa_len
)
607 + sizeof(struct sadb_address
)
608 + PFKEY_ALIGN8(dst
->sa_len
);
610 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
611 __ipsec_set_strerror(strerror(errno
));
614 ep
= ((caddr_t
)newmsg
) + len
;
616 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
622 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
628 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
636 len
= pfkey_send(so
, newmsg
, len
);
642 __ipsec_errcode
= EIPSEC_NO_ERROR
;
647 * sending SADB_GET message to the kernel.
649 * positive: success and return length sent.
650 * -1 : error occured, and set errno.
653 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
656 struct sockaddr
*src
, *dst
;
660 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
667 * sending SADB_REGISTER message to the kernel.
669 * positive: success and return length sent.
670 * -1 : error occured, and set errno.
673 pfkey_send_register(so
, satype
)
679 if (satype
== PF_UNSPEC
) {
681 (unsigned int)algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
683 if (ipsec_supported
[algno
]) {
684 free(ipsec_supported
[algno
]);
685 ipsec_supported
[algno
] = NULL
;
689 algno
= findsupportedmap(satype
);
691 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
695 if (ipsec_supported
[algno
]) {
696 free(ipsec_supported
[algno
]);
697 ipsec_supported
[algno
] = NULL
;
701 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
708 * receiving SADB_REGISTER message from the kernel, and copy buffer for
709 * sadb_supported returned into ipsec_supported.
711 * 0: success and return length sent.
712 * -1: error occured, and set errno.
715 pfkey_recv_register(so
)
718 pid_t pid
= getpid();
719 struct sadb_msg
*newmsg
;
722 /* receive message */
724 if ((newmsg
= pfkey_recv(so
)) == NULL
)
726 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
727 || (pid_t
)newmsg
->sadb_msg_pid
!= pid
);
730 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
732 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
736 __ipsec_errcode
= EIPSEC_NO_ERROR
;
742 * receiving SADB_REGISTER message from the kernel, and copy buffer for
743 * sadb_supported returned into ipsec_supported.
744 * NOTE: sadb_msg_len must be host order.
746 * tlen: msg length, it's to makeing sure.
748 * 0: success and return length sent.
749 * -1: error occured, and set errno.
752 pfkey_set_supported(msg
, tlen
)
753 struct sadb_msg
*msg
;
756 struct sadb_supported
*sup
;
761 if (msg
->sadb_msg_len
!= tlen
) {
762 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
769 p
+= sizeof(struct sadb_msg
);
772 sup
= (struct sadb_supported
*)p
;
773 if (ep
< p
+ sizeof(*sup
) ||
774 (size_t)PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
775 ep
< p
+ sup
->sadb_supported_len
) {
780 switch (sup
->sadb_supported_exttype
) {
781 case SADB_EXT_SUPPORTED_AUTH
:
782 case SADB_EXT_SUPPORTED_ENCRYPT
:
785 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
790 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
792 /* set supported map */
793 if (setsupportedmap(sup
) != 0)
796 p
+= sup
->sadb_supported_len
;
800 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
804 __ipsec_errcode
= EIPSEC_NO_ERROR
;
810 * sending SADB_FLUSH message to the kernel.
812 * positive: success and return length sent.
813 * -1 : error occured, and set errno.
816 pfkey_send_flush(so
, satype
)
822 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
829 * sending SADB_DUMP message to the kernel.
831 * positive: success and return length sent.
832 * -1 : error occured, and set errno.
835 pfkey_send_dump(so
, satype
)
841 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
848 * sending SADB_X_PROMISC message to the kernel.
849 * NOTE that this function handles promisc mode toggle only.
851 * flag: set promisc off if zero, set promisc on if non-zero.
853 * positive: success and return length sent.
854 * -1 : error occured, and set errno.
855 * 0 : error occured, and set errno.
856 * others: a pointer to new allocated buffer in which supported
860 pfkey_send_promisc_toggle(so
, flag
)
866 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 0)
873 * sending SADB_X_SPDADD message to the kernel.
875 * positive: success and return length sent.
876 * -1 : error occured, and set errno.
879 pfkey_send_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
881 struct sockaddr
*src
, *dst
;
882 u_int prefs
, prefd
, proto
;
889 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
890 src
, prefs
, dst
, prefd
, proto
,
892 policy
, policylen
, seq
)) < 0)
899 * sending SADB_X_SPDADD message to the kernel.
901 * positive: success and return length sent.
902 * -1 : error occured, and set errno.
905 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
906 policy
, policylen
, seq
)
908 struct sockaddr
*src
, *dst
;
909 u_int prefs
, prefd
, proto
;
910 u_int64_t ltime
, vtime
;
917 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
918 src
, prefs
, dst
, prefd
, proto
,
920 policy
, policylen
, seq
)) < 0)
927 * sending SADB_X_SPDUPDATE message to the kernel.
929 * positive: success and return length sent.
930 * -1 : error occured, and set errno.
933 pfkey_send_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
935 struct sockaddr
*src
, *dst
;
936 u_int prefs
, prefd
, proto
;
943 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
944 src
, prefs
, dst
, prefd
, proto
,
946 policy
, policylen
, seq
)) < 0)
953 * sending SADB_X_SPDUPDATE message to the kernel.
955 * positive: success and return length sent.
956 * -1 : error occured, and set errno.
959 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
960 policy
, policylen
, seq
)
962 struct sockaddr
*src
, *dst
;
963 u_int prefs
, prefd
, proto
;
964 u_int64_t ltime
, vtime
;
971 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
972 src
, prefs
, dst
, prefd
, proto
,
974 policy
, policylen
, seq
)) < 0)
981 * sending SADB_X_SPDDELETE message to the kernel.
983 * positive: success and return length sent.
984 * -1 : error occured, and set errno.
987 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
989 struct sockaddr
*src
, *dst
;
990 u_int prefs
, prefd
, proto
;
997 if (policylen
!= sizeof(struct sadb_x_policy
)) {
998 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1002 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
1003 src
, prefs
, dst
, prefd
, proto
,
1005 policy
, policylen
, seq
)) < 0)
1012 * sending SADB_X_SPDDELETE message to the kernel.
1014 * positive: success and return length sent.
1015 * -1 : error occured, and set errno.
1018 pfkey_send_spddelete2(so
, spid
)
1024 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1031 * sending SADB_X_SPDGET message to the kernel.
1033 * positive: success and return length sent.
1034 * -1 : error occured, and set errno.
1037 pfkey_send_spdget(so
, spid
)
1043 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1050 * sending SADB_X_SPDSETIDX message to the kernel.
1052 * positive: success and return length sent.
1053 * -1 : error occured, and set errno.
1056 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1058 struct sockaddr
*src
, *dst
;
1059 u_int prefs
, prefd
, proto
;
1066 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1067 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1071 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1072 src
, prefs
, dst
, prefd
, proto
,
1074 policy
, policylen
, seq
)) < 0)
1081 * sending SADB_SPDFLUSH message to the kernel.
1083 * positive: success and return length sent.
1084 * -1 : error occured, and set errno.
1087 pfkey_send_spdflush(so
)
1092 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1099 * sending SADB_SPDDUMP message to the kernel.
1101 * positive: success and return length sent.
1102 * -1 : error occured, and set errno.
1105 pfkey_send_spddump(so
)
1110 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1116 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1118 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1119 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1120 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1122 u_int type
, satype
, mode
;
1123 struct sockaddr
*src
, *dst
;
1124 u_int32_t spi
, reqid
;
1127 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1128 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1130 struct sadb_msg
*newmsg
;
1136 /* validity check */
1137 if (src
== NULL
|| dst
== NULL
) {
1138 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1141 if (src
->sa_family
!= dst
->sa_family
) {
1142 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1145 switch (src
->sa_family
) {
1147 plen
= sizeof(struct in_addr
) << 3;
1150 plen
= sizeof(struct in6_addr
) << 3;
1153 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1158 case SADB_SATYPE_ESP
:
1159 if (e_type
== SADB_EALG_NONE
) {
1160 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1164 case SADB_SATYPE_AH
:
1165 if (e_type
!= SADB_EALG_NONE
) {
1166 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1169 if (a_type
== SADB_AALG_NONE
) {
1170 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1174 case SADB_X_SATYPE_IPCOMP
:
1175 if (e_type
== SADB_X_CALG_NONE
) {
1176 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1179 if (a_type
!= SADB_AALG_NONE
) {
1180 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1185 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1189 /* create new sadb_msg to reply. */
1190 len
= sizeof(struct sadb_msg
)
1191 + sizeof(struct sadb_sa
)
1192 + sizeof(struct sadb_x_sa2
)
1193 + sizeof(struct sadb_address
)
1194 + PFKEY_ALIGN8(src
->sa_len
)
1195 + sizeof(struct sadb_address
)
1196 + PFKEY_ALIGN8(dst
->sa_len
)
1197 + sizeof(struct sadb_lifetime
)
1198 + sizeof(struct sadb_lifetime
);
1200 if (e_type
!= SADB_EALG_NONE
)
1201 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1202 if (a_type
!= SADB_AALG_NONE
)
1203 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1205 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1206 __ipsec_set_strerror(strerror(errno
));
1209 ep
= ((caddr_t
)newmsg
) + len
;
1211 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1212 satype
, seq
, getpid());
1217 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1222 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1227 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1233 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1240 if (e_type
!= SADB_EALG_NONE
) {
1241 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1248 if (a_type
!= SADB_AALG_NONE
) {
1249 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1250 keymat
+ e_keylen
, a_keylen
);
1257 /* set sadb_lifetime for destination */
1258 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1259 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1264 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1265 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1266 if (!p
|| p
!= ep
) {
1272 len
= pfkey_send(so
, newmsg
, len
);
1278 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1282 /* sending SADB_DELETE or SADB_GET message to the kernel */
1284 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1286 u_int type
, satype
, mode
;
1287 struct sockaddr
*src
, *dst
;
1290 struct sadb_msg
*newmsg
;
1298 /* validity check */
1299 if (src
== NULL
|| dst
== NULL
) {
1300 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1303 if (src
->sa_family
!= dst
->sa_family
) {
1304 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1307 switch (src
->sa_family
) {
1309 plen
= sizeof(struct in_addr
) << 3;
1312 plen
= sizeof(struct in6_addr
) << 3;
1315 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1319 /* create new sadb_msg to reply. */
1320 len
= sizeof(struct sadb_msg
)
1321 + sizeof(struct sadb_sa
)
1322 + sizeof(struct sadb_address
)
1323 + PFKEY_ALIGN8(src
->sa_len
)
1324 + sizeof(struct sadb_address
)
1325 + PFKEY_ALIGN8(dst
->sa_len
);
1327 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1328 __ipsec_set_strerror(strerror(errno
));
1331 ep
= ((caddr_t
)newmsg
) + len
;
1333 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1339 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1344 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1350 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1352 if (!p
|| p
!= ep
) {
1358 len
= pfkey_send(so
, newmsg
, len
);
1364 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1369 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1373 pfkey_send_x3(so
, type
, satype
)
1377 struct sadb_msg
*newmsg
;
1382 /* validity check */
1384 case SADB_X_PROMISC
:
1385 if (satype
!= 0 && satype
!= 1) {
1386 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1392 case SADB_SATYPE_UNSPEC
:
1393 case SADB_SATYPE_AH
:
1394 case SADB_SATYPE_ESP
:
1395 case SADB_X_SATYPE_IPCOMP
:
1398 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1403 /* create new sadb_msg to send. */
1404 len
= sizeof(struct sadb_msg
);
1406 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1407 __ipsec_set_strerror(strerror(errno
));
1410 ep
= ((caddr_t
)newmsg
) + len
;
1412 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1414 if (!p
|| p
!= ep
) {
1420 len
= pfkey_send(so
, newmsg
, len
);
1426 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1430 /* sending SADB_X_SPDADD message to the kernel */
1432 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1433 ltime
, vtime
, policy
, policylen
, seq
)
1435 struct sockaddr
*src
, *dst
;
1436 u_int type
, prefs
, prefd
, proto
;
1437 u_int64_t ltime
, vtime
;
1442 struct sadb_msg
*newmsg
;
1448 /* validity check */
1449 if (src
== NULL
|| dst
== NULL
) {
1450 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1453 if (src
->sa_family
!= dst
->sa_family
) {
1454 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1458 switch (src
->sa_family
) {
1460 plen
= sizeof(struct in_addr
) << 3;
1463 plen
= sizeof(struct in6_addr
) << 3;
1466 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1469 if (prefs
> (u_int
)plen
|| prefd
> (u_int
)plen
) {
1470 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1474 /* create new sadb_msg to reply. */
1475 len
= sizeof(struct sadb_msg
)
1476 + sizeof(struct sadb_address
)
1477 + PFKEY_ALIGN8(src
->sa_len
)
1478 + sizeof(struct sadb_address
)
1479 + PFKEY_ALIGN8(src
->sa_len
)
1480 + sizeof(struct sadb_lifetime
)
1483 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1484 __ipsec_set_strerror(strerror(errno
));
1487 ep
= ((caddr_t
)newmsg
) + len
;
1489 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1490 SADB_SATYPE_UNSPEC
, seq
, getpid());
1495 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1500 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1505 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1506 0, 0, ltime
, vtime
);
1507 if (!p
|| p
+ policylen
!= ep
) {
1511 memcpy(p
, policy
, policylen
);
1514 len
= pfkey_send(so
, newmsg
, len
);
1520 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1524 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1526 pfkey_send_x5(so
, type
, spid
)
1531 struct sadb_msg
*newmsg
;
1532 struct sadb_x_policy xpl
;
1537 /* create new sadb_msg to reply. */
1538 len
= sizeof(struct sadb_msg
)
1541 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1542 __ipsec_set_strerror(strerror(errno
));
1545 ep
= ((caddr_t
)newmsg
) + len
;
1547 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1548 SADB_SATYPE_UNSPEC
, 0, getpid());
1554 if (p
+ sizeof(xpl
) != ep
) {
1558 memset(&xpl
, 0, sizeof(xpl
));
1559 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1560 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1561 xpl
.sadb_x_policy_id
= spid
;
1562 memcpy(p
, &xpl
, sizeof(xpl
));
1565 len
= pfkey_send(so
, newmsg
, len
);
1571 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1579 * others : success and return value of socket.
1585 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1587 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1588 __ipsec_set_strerror(strerror(errno
));
1593 * This is a temporary workaround for KAME PR 154.
1594 * Don't really care even if it fails.
1596 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1597 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1599 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1615 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1620 * receive sadb_msg data, and return pointer to new buffer allocated.
1621 * Must free this buffer later.
1623 * NULL : error occured.
1624 * others : a pointer to sadb_msg structure.
1626 * XXX should be rewritten to pass length explicitly
1632 struct sadb_msg buf
, *newmsg
;
1635 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1638 __ipsec_set_strerror(strerror(errno
));
1642 if ((size_t)len
< sizeof(buf
)) {
1643 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1644 __ipsec_errcode
= EIPSEC_MAX
;
1648 /* read real message */
1649 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1650 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1651 __ipsec_set_strerror(strerror(errno
));
1655 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1658 __ipsec_set_strerror(strerror(errno
));
1663 if (len
!= reallen
) {
1664 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1669 /* don't trust what the kernel says, validate! */
1670 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1671 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1676 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1681 * send message to a socket.
1683 * others: success and return length sent.
1687 pfkey_send(so
, msg
, len
)
1689 struct sadb_msg
*msg
;
1692 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1693 __ipsec_set_strerror(strerror(errno
));
1697 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1703 * NOTE: These functions are derived from netkey/key.c in KAME.
1706 * set the pointer to each header in this message buffer.
1707 * IN: msg: pointer to message buffer.
1708 * mhp: pointer to the buffer initialized like below:
1709 * caddr_t mhp[SADB_EXT_MAX + 1];
1713 * XXX should be rewritten to obtain length explicitly
1716 pfkey_align(msg
, mhp
)
1717 struct sadb_msg
*msg
;
1720 struct sadb_ext
*ext
;
1723 caddr_t ep
; /* XXX should be passed from upper layer */
1725 /* validity check */
1726 if (msg
== NULL
|| mhp
== NULL
) {
1727 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1732 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1735 mhp
[0] = (caddr_t
)msg
;
1739 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1741 /* skip base header */
1742 p
+= sizeof(struct sadb_msg
);
1745 ext
= (struct sadb_ext
*)p
;
1746 if (ep
< p
+ sizeof(*ext
) || (size_t)PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1747 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1748 /* invalid format */
1752 /* duplicate check */
1753 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1754 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1755 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1760 switch (ext
->sadb_ext_type
) {
1762 case SADB_EXT_LIFETIME_CURRENT
:
1763 case SADB_EXT_LIFETIME_HARD
:
1764 case SADB_EXT_LIFETIME_SOFT
:
1765 case SADB_EXT_ADDRESS_SRC
:
1766 case SADB_EXT_ADDRESS_DST
:
1767 case SADB_EXT_ADDRESS_PROXY
:
1768 case SADB_EXT_KEY_AUTH
:
1769 /* XXX should to be check weak keys. */
1770 case SADB_EXT_KEY_ENCRYPT
:
1771 /* XXX should to be check weak keys. */
1772 case SADB_EXT_IDENTITY_SRC
:
1773 case SADB_EXT_IDENTITY_DST
:
1774 case SADB_EXT_SENSITIVITY
:
1775 case SADB_EXT_PROPOSAL
:
1776 case SADB_EXT_SUPPORTED_AUTH
:
1777 case SADB_EXT_SUPPORTED_ENCRYPT
:
1778 case SADB_EXT_SPIRANGE
:
1779 case SADB_X_EXT_POLICY
:
1780 case SADB_X_EXT_SA2
:
1781 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1784 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1788 p
+= PFKEY_EXTLEN(ext
);
1792 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1796 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1801 * check basic usage for sadb_msg,
1802 * NOTE: This routine is derived from netkey/key.c in KAME.
1803 * IN: msg: pointer to message buffer.
1804 * mhp: pointer to the buffer initialized like below:
1806 * caddr_t mhp[SADB_EXT_MAX + 1];
1815 struct sadb_msg
*msg
;
1817 /* validity check */
1818 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1819 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1823 msg
= (struct sadb_msg
*)mhp
[0];
1826 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1827 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1832 if (msg
->sadb_msg_type
> SADB_MAX
) {
1833 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1838 switch (msg
->sadb_msg_satype
) {
1839 case SADB_SATYPE_UNSPEC
:
1840 switch (msg
->sadb_msg_type
) {
1848 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1852 case SADB_SATYPE_ESP
:
1853 case SADB_SATYPE_AH
:
1854 case SADB_X_SATYPE_IPCOMP
:
1855 switch (msg
->sadb_msg_type
) {
1857 case SADB_X_SPDDELETE
:
1859 case SADB_X_SPDDUMP
:
1860 case SADB_X_SPDFLUSH
:
1861 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1865 case SADB_SATYPE_RSVP
:
1866 case SADB_SATYPE_OSPFV2
:
1867 case SADB_SATYPE_RIPV2
:
1868 case SADB_SATYPE_MIP
:
1869 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1871 case 1: /* XXX: What does it do ? */
1872 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1876 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1880 /* check field of upper layer protocol and address family */
1881 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1882 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1883 struct sadb_address
*src0
, *dst0
;
1885 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1886 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1888 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1889 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1893 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1894 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1895 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1899 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1904 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1909 * prefixlen == 0 is valid because there must be the case
1910 * all addresses are matched.
1914 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1919 * set data into sadb_msg.
1920 * `buf' must has been allocated sufficiently.
1923 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1934 p
= (struct sadb_msg
*)buf
;
1935 len
= sizeof(struct sadb_msg
);
1937 if (buf
+ len
> lim
)
1941 p
->sadb_msg_version
= PF_KEY_V2
;
1942 p
->sadb_msg_type
= type
;
1943 p
->sadb_msg_errno
= 0;
1944 p
->sadb_msg_satype
= satype
;
1945 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1946 p
->sadb_msg_reserved
= 0;
1947 p
->sadb_msg_seq
= seq
;
1948 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1954 * copy secasvar data into sadb_address.
1955 * `buf' must has been allocated sufficiently.
1958 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1961 u_int32_t spi
, flags
;
1962 u_int wsize
, auth
, enc
;
1967 p
= (struct sadb_sa
*)buf
;
1968 len
= sizeof(struct sadb_sa
);
1970 if (buf
+ len
> lim
)
1974 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1975 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1976 p
->sadb_sa_spi
= spi
;
1977 p
->sadb_sa_replay
= wsize
;
1978 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1979 p
->sadb_sa_auth
= auth
;
1980 p
->sadb_sa_encrypt
= enc
;
1981 p
->sadb_sa_flags
= flags
;
1987 * set data into sadb_address.
1988 * `buf' must has been allocated sufficiently.
1989 * prefixlen is in bits.
1992 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
1996 struct sockaddr
*saddr
;
2000 struct sadb_address
*p
;
2003 p
= (struct sadb_address
*)buf
;
2004 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
2006 if (buf
+ len
> lim
)
2010 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2011 p
->sadb_address_exttype
= exttype
& 0xffff;
2012 p
->sadb_address_proto
= ul_proto
& 0xff;
2013 p
->sadb_address_prefixlen
= prefixlen
;
2014 p
->sadb_address_reserved
= 0;
2016 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2022 * set sadb_key structure after clearing buffer with zero.
2023 * OUT: the pointer of buf + len.
2026 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2035 p
= (struct sadb_key
*)buf
;
2036 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2038 if (buf
+ len
> lim
)
2042 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2043 p
->sadb_key_exttype
= type
;
2044 p
->sadb_key_bits
= keylen
<< 3;
2045 p
->sadb_key_reserved
= 0;
2047 memcpy(p
+ 1, key
, keylen
);
2053 * set sadb_lifetime structure after clearing buffer with zero.
2054 * OUT: the pointer of buf + len.
2057 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2061 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2063 struct sadb_lifetime
*p
;
2066 p
= (struct sadb_lifetime
*)buf
;
2067 len
= sizeof(struct sadb_lifetime
);
2069 if (buf
+ len
> lim
)
2073 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2074 p
->sadb_lifetime_exttype
= type
;
2077 case SADB_EXT_LIFETIME_SOFT
:
2078 p
->sadb_lifetime_allocations
2079 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2080 p
->sadb_lifetime_bytes
2081 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2082 p
->sadb_lifetime_addtime
2083 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2084 p
->sadb_lifetime_usetime
2085 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2087 case SADB_EXT_LIFETIME_HARD
:
2088 p
->sadb_lifetime_allocations
= l_alloc
;
2089 p
->sadb_lifetime_bytes
= l_bytes
;
2090 p
->sadb_lifetime_addtime
= l_addtime
;
2091 p
->sadb_lifetime_usetime
= l_usetime
;
2099 * copy secasvar data into sadb_address.
2100 * `buf' must has been allocated sufficiently.
2103 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2109 struct sadb_x_sa2
*p
;
2110 u_int8_t mode
= mode0
& 0xff;
2113 p
= (struct sadb_x_sa2
*)buf
;
2114 len
= sizeof(struct sadb_x_sa2
);
2116 if (buf
+ len
> lim
)
2120 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2121 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2122 p
->sadb_x_sa2_mode
= mode
;
2123 p
->sadb_x_sa2_reqid
= reqid
;