1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2011 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /* $FreeBSD: src/lib/libipsec/pfkey.c,v 1.1.2.2 2001/07/03 11:01:14 ume Exp $ */
19 /* $KAME: pfkey.c,v 1.39 2001/03/05 18:22:17 thorpej Exp $ */
22 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
23 * All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. Neither the name of the project nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <net/pfkeyv2.h>
54 #include <netinet/in.h>
55 #include <netinet6/ipsec.h>
62 #include <TargetConditionals.h>
64 #include "ipsec_strerror.h"
66 #include "ipsec_options.h"
68 #if TARGET_OS_EMBEDDED
70 #define MDNS_NO_IPSEC 1
76 #define CALLOC(size, cast) (cast)calloc(1, (size))
78 static int findsupportedmap
__P((int));
79 static int setsupportedmap
__P((struct sadb_supported
*));
80 static struct sadb_alg
*findsupportedalg
__P((u_int
, u_int
));
81 static int pfkey_send_x1
__P((int, u_int
, u_int
, u_int
, struct sockaddr
*,
82 struct sockaddr
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
83 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
84 u_int32_t
, u_int32_t
, u_int32_t
));
85 static int pfkey_send_x2
__P((int, u_int
, u_int
, u_int
,
86 struct sockaddr
*, struct sockaddr
*, u_int32_t
));
87 static int pfkey_send_x3
__P((int, u_int
, u_int
));
88 static int pfkey_send_x4
__P((int, u_int
, struct sockaddr
*, u_int
,
89 struct sockaddr
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
90 char *, int, u_int32_t
));
91 static int pfkey_send_x5
__P((int, u_int
, u_int32_t
));
93 static caddr_t pfkey_setsadbmsg
__P((caddr_t
, caddr_t
, u_int
, u_int
,
94 u_int
, u_int32_t
, pid_t
));
95 static caddr_t pfkey_setsadbsa
__P((caddr_t
, caddr_t
, u_int32_t
, u_int
,
96 u_int
, u_int
, u_int32_t
));
97 static caddr_t pfkey_setsadbaddr
__P((caddr_t
, caddr_t
, u_int
,
98 struct sockaddr
*, u_int
, u_int
));
99 static caddr_t pfkey_setsadbkey
__P((caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
));
100 static caddr_t pfkey_setsadblifetime
__P((caddr_t
, caddr_t
, u_int
, u_int32_t
,
101 u_int32_t
, u_int32_t
, u_int32_t
));
102 static caddr_t pfkey_setsadbxsa2
__P((caddr_t
, caddr_t
, u_int32_t
, u_int32_t
));
105 * make and search supported algorithm structure.
107 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
, };
109 static int supported_map
[] = {
112 SADB_X_SATYPE_IPCOMP
,
116 findsupportedmap(satype
)
121 for (i
= 0; (unsigned int)i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
122 if (supported_map
[i
] == satype
)
127 static struct sadb_alg
*
128 findsupportedalg(satype
, alg_id
)
129 u_int satype
, alg_id
;
136 algno
= findsupportedmap(satype
);
138 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
141 if (ipsec_supported
[algno
] == NULL
) {
142 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
146 tlen
= ipsec_supported
[algno
]->sadb_supported_len
147 - sizeof(struct sadb_supported
);
148 p
= (caddr_t
)(ipsec_supported
[algno
] + 1);
150 if ((unsigned int)tlen
< sizeof(struct sadb_alg
)) {
154 if (((struct sadb_alg
*)p
)->sadb_alg_id
== alg_id
)
155 return (struct sadb_alg
*)p
;
157 tlen
-= sizeof(struct sadb_alg
);
158 p
+= sizeof(struct sadb_alg
);
161 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
167 struct sadb_supported
*sup
;
169 struct sadb_supported
**ipsup
;
171 switch (sup
->sadb_supported_exttype
) {
172 case SADB_EXT_SUPPORTED_AUTH
:
173 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
175 case SADB_EXT_SUPPORTED_ENCRYPT
:
176 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
179 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
186 *ipsup
= malloc(sup
->sadb_supported_len
);
188 __ipsec_set_strerror(strerror(errno
));
191 memcpy(*ipsup
, sup
, sup
->sadb_supported_len
);
197 * check key length against algorithm specified.
198 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
199 * augument, and only calls to ipsec_check_keylen2();
200 * keylen is the unit of bit.
206 ipsec_check_keylen(supported
, alg_id
, keylen
)
215 case SADB_EXT_SUPPORTED_AUTH
:
216 satype
= SADB_SATYPE_AH
;
218 case SADB_EXT_SUPPORTED_ENCRYPT
:
219 satype
= SADB_SATYPE_ESP
;
222 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
226 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
230 * check key length against algorithm specified.
231 * satype is one of satype defined at pfkeyv2.h.
232 * keylen is the unit of bit.
238 ipsec_check_keylen2(satype
, alg_id
, keylen
)
243 struct sadb_alg
*alg
;
245 alg
= findsupportedalg(satype
, alg_id
);
249 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
250 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
254 __ipsec_errcode
= EIPSEC_NO_ERROR
;
259 * get max/min key length against algorithm specified.
260 * satype is one of satype defined at pfkeyv2.h.
261 * keylen is the unit of bit.
267 ipsec_get_keylen(supported
, alg_id
, alg0
)
268 u_int supported
, alg_id
;
269 struct sadb_alg
*alg0
;
271 struct sadb_alg
*alg
;
276 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
281 case SADB_EXT_SUPPORTED_AUTH
:
282 satype
= SADB_SATYPE_AH
;
284 case SADB_EXT_SUPPORTED_ENCRYPT
:
285 satype
= SADB_SATYPE_ESP
;
288 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
292 alg
= findsupportedalg(satype
, alg_id
);
296 memcpy(alg0
, alg
, sizeof(*alg0
));
298 __ipsec_errcode
= EIPSEC_NO_ERROR
;
303 * set the rate for SOFT lifetime against HARD one.
304 * If rate is more than 100 or equal to zero, then set to 100.
306 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
307 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
308 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
309 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
312 pfkey_set_softrate(type
, rate
)
315 __ipsec_errcode
= EIPSEC_NO_ERROR
;
317 if (rate
> 100 || rate
== 0)
321 case SADB_X_LIFETIME_ALLOCATIONS
:
322 soft_lifetime_allocations_rate
= rate
;
324 case SADB_X_LIFETIME_BYTES
:
325 soft_lifetime_bytes_rate
= rate
;
327 case SADB_X_LIFETIME_ADDTIME
:
328 soft_lifetime_addtime_rate
= rate
;
330 case SADB_X_LIFETIME_USETIME
:
331 soft_lifetime_usetime_rate
= rate
;
335 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
340 * get current rate for SOFT lifetime against HARD one.
341 * ATTENTION: ~0 is returned if invalid type was passed.
344 pfkey_get_softrate(type
)
348 case SADB_X_LIFETIME_ALLOCATIONS
:
349 return soft_lifetime_allocations_rate
;
350 case SADB_X_LIFETIME_BYTES
:
351 return soft_lifetime_bytes_rate
;
352 case SADB_X_LIFETIME_ADDTIME
:
353 return soft_lifetime_addtime_rate
;
354 case SADB_X_LIFETIME_USETIME
:
355 return soft_lifetime_usetime_rate
;
362 * sending SADB_GETSPI message to the kernel.
364 * positive: success and return length sent.
365 * -1 : error occured, and set errno.
368 pfkey_send_getspi(so
, satype
, mode
, src
, dst
, min
, max
, reqid
, seq
)
371 struct sockaddr
*src
, *dst
;
372 u_int32_t min
, max
, reqid
, seq
;
374 struct sadb_msg
*newmsg
;
377 int need_spirange
= 0;
382 if (src
== NULL
|| dst
== NULL
) {
383 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
386 if (src
->sa_family
!= dst
->sa_family
) {
387 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
390 if (min
> max
|| (min
> 0 && min
<= 255)) {
391 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
394 switch (src
->sa_family
) {
396 plen
= sizeof(struct in_addr
) << 3;
399 plen
= sizeof(struct in6_addr
) << 3;
402 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
406 /* create new sadb_msg to send. */
407 len
= sizeof(struct sadb_msg
)
408 + sizeof(struct sadb_x_sa2
)
409 + sizeof(struct sadb_address
)
410 + PFKEY_ALIGN8(src
->sa_len
)
411 + sizeof(struct sadb_address
)
412 + PFKEY_ALIGN8(dst
->sa_len
);
414 if (min
> (u_int32_t
)255 && max
< (u_int32_t
) ~0) {
416 len
+= sizeof(struct sadb_spirange
);
419 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
420 __ipsec_set_strerror(strerror(errno
));
423 ep
= ((caddr_t
)newmsg
) + len
;
425 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_GETSPI
,
426 len
, satype
, seq
, getpid());
432 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
438 /* set sadb_address for source */
439 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
446 /* set sadb_address for destination */
447 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
454 /* proccessing spi range */
456 struct sadb_spirange spirange
;
458 if (p
+ sizeof(spirange
) > ep
) {
463 memset(&spirange
, 0, sizeof(spirange
));
464 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
465 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
466 spirange
.sadb_spirange_min
= min
;
467 spirange
.sadb_spirange_max
= max
;
469 memcpy(p
, &spirange
, sizeof(spirange
));
471 p
+= sizeof(spirange
);
479 len
= pfkey_send(so
, newmsg
, len
);
485 __ipsec_errcode
= EIPSEC_NO_ERROR
;
490 * sending SADB_UPDATE message to the kernel.
491 * The length of key material is a_keylen + e_keylen.
493 * positive: success and return length sent.
494 * -1 : error occured, and set errno.
497 pfkey_send_update(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
498 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
499 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
501 u_int satype
, mode
, wsize
;
502 struct sockaddr
*src
, *dst
;
503 u_int32_t spi
, reqid
;
505 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
507 u_int64_t l_bytes
, l_addtime
, l_usetime
;
511 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
513 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
514 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
521 * sending SADB_ADD message to the kernel.
522 * The length of key material is a_keylen + e_keylen.
524 * positive: success and return length sent.
525 * -1 : error occured, and set errno.
528 pfkey_send_add(so
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
529 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
530 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
532 u_int satype
, mode
, wsize
;
533 struct sockaddr
*src
, *dst
;
534 u_int32_t spi
, reqid
;
536 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
538 u_int64_t l_bytes
, l_addtime
, l_usetime
;
542 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
544 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
545 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)) < 0)
552 * sending SADB_DELETE message to the kernel.
554 * positive: success and return length sent.
555 * -1 : error occured, and set errno.
558 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
561 struct sockaddr
*src
, *dst
;
565 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
572 * sending SADB_DELETE without spi to the kernel. This is
573 * the "delete all" request (an extension also present in
577 * positive: success and return length sent
578 * -1 : error occured, and set errno
581 pfkey_send_delete_all(so
, satype
, mode
, src
, dst
)
584 struct sockaddr
*src
, *dst
;
586 struct sadb_msg
*newmsg
;
595 if (src
== NULL
|| dst
== NULL
) {
596 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
599 if (src
->sa_family
!= dst
->sa_family
) {
600 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
603 switch (src
->sa_family
) {
605 plen
= sizeof(struct in_addr
) << 3;
608 plen
= sizeof(struct in6_addr
) << 3;
611 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
615 /* create new sadb_msg to reply. */
616 len
= sizeof(struct sadb_msg
)
617 + sizeof(struct sadb_address
)
618 + PFKEY_ALIGN8(src
->sa_len
)
619 + sizeof(struct sadb_address
)
620 + PFKEY_ALIGN8(dst
->sa_len
);
622 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
623 __ipsec_set_strerror(strerror(errno
));
626 ep
= ((caddr_t
)newmsg
) + len
;
628 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, SADB_DELETE
, len
, satype
, 0,
634 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
640 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
648 len
= pfkey_send(so
, newmsg
, len
);
654 __ipsec_errcode
= EIPSEC_NO_ERROR
;
659 * sending SADB_GET message to the kernel.
661 * positive: success and return length sent.
662 * -1 : error occured, and set errno.
665 pfkey_send_get(so
, satype
, mode
, src
, dst
, spi
)
668 struct sockaddr
*src
, *dst
;
672 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
679 * sending SADB_REGISTER message to the kernel.
681 * positive: success and return length sent.
682 * -1 : error occured, and set errno.
685 pfkey_send_register(so
, satype
)
691 if (satype
== PF_UNSPEC
) {
693 (unsigned int)algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
695 if (ipsec_supported
[algno
]) {
696 free(ipsec_supported
[algno
]);
697 ipsec_supported
[algno
] = NULL
;
701 algno
= findsupportedmap(satype
);
703 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
707 if (ipsec_supported
[algno
]) {
708 free(ipsec_supported
[algno
]);
709 ipsec_supported
[algno
] = NULL
;
713 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
720 * receiving SADB_REGISTER message from the kernel, and copy buffer for
721 * sadb_supported returned into ipsec_supported.
723 * 0: success and return length sent.
724 * -1: error occured, and set errno.
727 pfkey_recv_register(so
)
730 pid_t pid
= getpid();
731 struct sadb_msg
*newmsg
;
734 /* receive message */
736 if ((newmsg
= pfkey_recv(so
)) == NULL
)
738 } while (newmsg
->sadb_msg_type
!= SADB_REGISTER
739 || (pid_t
)newmsg
->sadb_msg_pid
!= pid
);
742 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
744 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
748 __ipsec_errcode
= EIPSEC_NO_ERROR
;
754 * receiving SADB_REGISTER message from the kernel, and copy buffer for
755 * sadb_supported returned into ipsec_supported.
756 * NOTE: sadb_msg_len must be host order.
758 * tlen: msg length, it's to makeing sure.
760 * 0: success and return length sent.
761 * -1: error occured, and set errno.
764 pfkey_set_supported(msg
, tlen
)
765 struct sadb_msg
*msg
;
768 struct sadb_supported
*sup
;
773 if (msg
->sadb_msg_len
!= tlen
) {
774 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
781 p
+= sizeof(struct sadb_msg
);
784 sup
= (struct sadb_supported
*)p
;
785 if (ep
< p
+ sizeof(*sup
) ||
786 (size_t)PFKEY_EXTLEN(sup
) < sizeof(*sup
) ||
787 ep
< p
+ sup
->sadb_supported_len
) {
792 switch (sup
->sadb_supported_exttype
) {
793 case SADB_EXT_SUPPORTED_AUTH
:
794 case SADB_EXT_SUPPORTED_ENCRYPT
:
797 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
802 sup
->sadb_supported_len
= PFKEY_EXTLEN(sup
);
804 /* set supported map */
805 if (setsupportedmap(sup
) != 0)
808 p
+= sup
->sadb_supported_len
;
812 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
816 __ipsec_errcode
= EIPSEC_NO_ERROR
;
822 * sending SADB_FLUSH message to the kernel.
824 * positive: success and return length sent.
825 * -1 : error occured, and set errno.
828 pfkey_send_flush(so
, satype
)
834 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
841 * sending SADB_DUMP message to the kernel.
843 * positive: success and return length sent.
844 * -1 : error occured, and set errno.
847 pfkey_send_dump(so
, satype
)
853 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
860 * sending SADB_X_PROMISC message to the kernel.
861 * NOTE that this function handles promisc mode toggle only.
863 * flag: set promisc off if zero, set promisc on if non-zero.
865 * positive: success and return length sent.
866 * -1 : error occured, and set errno.
867 * 0 : error occured, and set errno.
868 * others: a pointer to new allocated buffer in which supported
872 pfkey_send_promisc_toggle(so
, flag
)
878 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
, (flag
? 1 : 0))) < 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_spdadd(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
893 struct sockaddr
*src
, *dst
;
894 u_int prefs
, prefd
, proto
;
901 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
902 src
, prefs
, dst
, prefd
, proto
,
904 policy
, policylen
, seq
)) < 0)
911 * sending SADB_X_SPDADD message to the kernel.
913 * positive: success and return length sent.
914 * -1 : error occured, and set errno.
917 pfkey_send_spdadd2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
918 policy
, policylen
, seq
)
920 struct sockaddr
*src
, *dst
;
921 u_int prefs
, prefd
, proto
;
922 u_int64_t ltime
, vtime
;
929 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
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_spdupdate(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
947 struct sockaddr
*src
, *dst
;
948 u_int prefs
, prefd
, proto
;
955 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
956 src
, prefs
, dst
, prefd
, proto
,
958 policy
, policylen
, seq
)) < 0)
965 * sending SADB_X_SPDUPDATE message to the kernel.
967 * positive: success and return length sent.
968 * -1 : error occured, and set errno.
971 pfkey_send_spdupdate2(so
, src
, prefs
, dst
, prefd
, proto
, ltime
, vtime
,
972 policy
, policylen
, seq
)
974 struct sockaddr
*src
, *dst
;
975 u_int prefs
, prefd
, proto
;
976 u_int64_t ltime
, vtime
;
983 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
984 src
, prefs
, dst
, prefd
, proto
,
986 policy
, policylen
, seq
)) < 0)
993 * sending SADB_X_SPDDELETE message to the kernel.
995 * positive: success and return length sent.
996 * -1 : error occured, and set errno.
999 pfkey_send_spddelete(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1001 struct sockaddr
*src
, *dst
;
1002 u_int prefs
, prefd
, proto
;
1009 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1010 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1014 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
1015 src
, prefs
, dst
, prefd
, proto
,
1017 policy
, policylen
, seq
)) < 0)
1024 * sending SADB_X_SPDDELETE message to the kernel.
1026 * positive: success and return length sent.
1027 * -1 : error occured, and set errno.
1030 pfkey_send_spddelete2(so
, spid
)
1036 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1043 * sending SADB_X_SPDGET message to the kernel.
1045 * positive: success and return length sent.
1046 * -1 : error occured, and set errno.
1049 pfkey_send_spdget(so
, spid
)
1055 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1062 * sending SADB_X_SPDSETIDX message to the kernel.
1064 * positive: success and return length sent.
1065 * -1 : error occured, and set errno.
1068 pfkey_send_spdsetidx(so
, src
, prefs
, dst
, prefd
, proto
, policy
, policylen
, seq
)
1070 struct sockaddr
*src
, *dst
;
1071 u_int prefs
, prefd
, proto
;
1078 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1079 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1083 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1084 src
, prefs
, dst
, prefd
, proto
,
1086 policy
, policylen
, seq
)) < 0)
1093 * sending SADB_SPDFLUSH message to the kernel.
1095 * positive: success and return length sent.
1096 * -1 : error occured, and set errno.
1099 pfkey_send_spdflush(so
)
1104 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1111 * sending SADB_SPDDUMP message to the kernel.
1113 * positive: success and return length sent.
1114 * -1 : error occured, and set errno.
1117 pfkey_send_spddump(so
)
1122 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1128 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1130 pfkey_send_x1(so
, type
, satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
1131 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1132 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
1134 u_int type
, satype
, mode
;
1135 struct sockaddr
*src
, *dst
;
1136 u_int32_t spi
, reqid
;
1139 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1140 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
;
1142 struct sadb_msg
*newmsg
;
1148 /* validity check */
1149 if (src
== NULL
|| dst
== NULL
) {
1150 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1153 if (src
->sa_family
!= dst
->sa_family
) {
1154 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1157 switch (src
->sa_family
) {
1159 plen
= sizeof(struct in_addr
) << 3;
1162 plen
= sizeof(struct in6_addr
) << 3;
1165 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1170 case SADB_SATYPE_ESP
:
1171 if (e_type
== SADB_EALG_NONE
) {
1172 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1176 case SADB_SATYPE_AH
:
1177 if (e_type
!= SADB_EALG_NONE
) {
1178 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1181 if (a_type
== SADB_AALG_NONE
) {
1182 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1186 case SADB_X_SATYPE_IPCOMP
:
1187 if (e_type
== SADB_X_CALG_NONE
) {
1188 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1191 if (a_type
!= SADB_AALG_NONE
) {
1192 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1197 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1201 /* create new sadb_msg to reply. */
1202 len
= sizeof(struct sadb_msg
)
1203 + sizeof(struct sadb_sa
)
1204 + sizeof(struct sadb_x_sa2
)
1205 + sizeof(struct sadb_address
)
1206 + PFKEY_ALIGN8(src
->sa_len
)
1207 + sizeof(struct sadb_address
)
1208 + PFKEY_ALIGN8(dst
->sa_len
)
1209 + sizeof(struct sadb_lifetime
)
1210 + sizeof(struct sadb_lifetime
);
1212 if (e_type
!= SADB_EALG_NONE
)
1213 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1214 if (a_type
!= SADB_AALG_NONE
)
1215 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1217 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1218 __ipsec_set_strerror(strerror(errno
));
1221 ep
= ((caddr_t
)newmsg
) + len
;
1223 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1224 satype
, seq
, getpid());
1229 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
);
1234 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
);
1239 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1245 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1252 if (e_type
!= SADB_EALG_NONE
) {
1253 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1260 if (a_type
!= SADB_AALG_NONE
) {
1261 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1262 keymat
+ e_keylen
, a_keylen
);
1269 /* set sadb_lifetime for destination */
1270 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1271 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1276 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1277 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1278 if (!p
|| p
!= ep
) {
1284 len
= pfkey_send(so
, newmsg
, len
);
1290 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1294 /* sending SADB_DELETE or SADB_GET message to the kernel */
1296 pfkey_send_x2(so
, type
, satype
, mode
, src
, dst
, spi
)
1298 u_int type
, satype
, mode
;
1299 struct sockaddr
*src
, *dst
;
1302 struct sadb_msg
*newmsg
;
1310 /* validity check */
1311 if (src
== NULL
|| dst
== NULL
) {
1312 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1315 if (src
->sa_family
!= dst
->sa_family
) {
1316 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1319 switch (src
->sa_family
) {
1321 plen
= sizeof(struct in_addr
) << 3;
1324 plen
= sizeof(struct in6_addr
) << 3;
1327 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1331 /* create new sadb_msg to reply. */
1332 len
= sizeof(struct sadb_msg
)
1333 + sizeof(struct sadb_sa
)
1334 + sizeof(struct sadb_address
)
1335 + PFKEY_ALIGN8(src
->sa_len
)
1336 + sizeof(struct sadb_address
)
1337 + PFKEY_ALIGN8(dst
->sa_len
);
1339 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1340 __ipsec_set_strerror(strerror(errno
));
1343 ep
= ((caddr_t
)newmsg
) + len
;
1345 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1351 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0);
1356 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, plen
,
1362 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, plen
,
1364 if (!p
|| p
!= ep
) {
1370 len
= pfkey_send(so
, newmsg
, len
);
1376 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1381 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1385 pfkey_send_x3(so
, type
, satype
)
1389 struct sadb_msg
*newmsg
;
1394 /* validity check */
1396 case SADB_X_PROMISC
:
1397 if (satype
!= 0 && satype
!= 1) {
1398 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1404 case SADB_SATYPE_UNSPEC
:
1405 case SADB_SATYPE_AH
:
1406 case SADB_SATYPE_ESP
:
1407 case SADB_X_SATYPE_IPCOMP
:
1410 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1415 /* create new sadb_msg to send. */
1416 len
= sizeof(struct sadb_msg
);
1418 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1419 __ipsec_set_strerror(strerror(errno
));
1422 ep
= ((caddr_t
)newmsg
) + len
;
1424 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
, satype
, 0,
1426 if (!p
|| p
!= ep
) {
1432 len
= pfkey_send(so
, newmsg
, len
);
1438 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1442 /* sending SADB_X_SPDADD message to the kernel */
1444 pfkey_send_x4(so
, type
, src
, prefs
, dst
, prefd
, proto
,
1445 ltime
, vtime
, policy
, policylen
, seq
)
1447 struct sockaddr
*src
, *dst
;
1448 u_int type
, prefs
, prefd
, proto
;
1449 u_int64_t ltime
, vtime
;
1454 struct sadb_msg
*newmsg
;
1460 /* validity check */
1461 if (src
== NULL
|| dst
== NULL
) {
1462 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1465 if (src
->sa_family
!= dst
->sa_family
) {
1466 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1470 switch (src
->sa_family
) {
1472 plen
= sizeof(struct in_addr
) << 3;
1475 plen
= sizeof(struct in6_addr
) << 3;
1478 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1481 if (prefs
> (u_int
)plen
|| prefd
> (u_int
)plen
) {
1482 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1486 /* create new sadb_msg to reply. */
1487 len
= sizeof(struct sadb_msg
)
1488 + sizeof(struct sadb_address
)
1489 + PFKEY_ALIGN8(src
->sa_len
)
1490 + sizeof(struct sadb_address
)
1491 + PFKEY_ALIGN8(src
->sa_len
)
1492 + sizeof(struct sadb_lifetime
)
1495 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1496 __ipsec_set_strerror(strerror(errno
));
1499 ep
= ((caddr_t
)newmsg
) + len
;
1501 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1502 SADB_SATYPE_UNSPEC
, seq
, getpid());
1507 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1512 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1517 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1518 0, 0, ltime
, vtime
);
1519 if (!p
|| p
+ policylen
!= ep
) {
1523 memcpy(p
, policy
, policylen
);
1526 len
= pfkey_send(so
, newmsg
, len
);
1532 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1536 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1538 pfkey_send_x5(so
, type
, spid
)
1543 struct sadb_msg
*newmsg
;
1544 struct sadb_x_policy xpl
;
1549 /* create new sadb_msg to reply. */
1550 len
= sizeof(struct sadb_msg
)
1553 if ((newmsg
= CALLOC(len
, struct sadb_msg
*)) == NULL
) {
1554 __ipsec_set_strerror(strerror(errno
));
1557 ep
= ((caddr_t
)newmsg
) + len
;
1559 p
= pfkey_setsadbmsg((caddr_t
)newmsg
, ep
, type
, len
,
1560 SADB_SATYPE_UNSPEC
, 0, getpid());
1566 if (p
+ sizeof(xpl
) != ep
) {
1570 memset(&xpl
, 0, sizeof(xpl
));
1571 xpl
.sadb_x_policy_len
= PFKEY_UNUNIT64(sizeof(xpl
));
1572 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1573 xpl
.sadb_x_policy_id
= spid
;
1574 memcpy(p
, &xpl
, sizeof(xpl
));
1577 len
= pfkey_send(so
, newmsg
, len
);
1583 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1591 * others : success and return value of socket.
1597 const int bufsiz
= 128 * 1024; /*is 128K enough?*/
1599 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1600 __ipsec_set_strerror(strerror(errno
));
1605 * This is a temporary workaround for KAME PR 154.
1606 * Don't really care even if it fails.
1608 (void)setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1609 (void)setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1611 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1627 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1632 * receive sadb_msg data, and return pointer to new buffer allocated.
1633 * Must free this buffer later.
1635 * NULL : error occured.
1636 * others : a pointer to sadb_msg structure.
1638 * XXX should be rewritten to pass length explicitly
1644 struct sadb_msg buf
, *newmsg
;
1647 while ((len
= recv(so
, (caddr_t
)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1650 __ipsec_set_strerror(strerror(errno
));
1654 if ((size_t)len
< sizeof(buf
)) {
1655 recv(so
, (caddr_t
)&buf
, sizeof(buf
), 0);
1656 __ipsec_errcode
= EIPSEC_MAX
;
1660 /* read real message */
1661 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1662 if ((newmsg
= CALLOC(reallen
, struct sadb_msg
*)) == 0) {
1663 __ipsec_set_strerror(strerror(errno
));
1667 while ((len
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
1670 __ipsec_set_strerror(strerror(errno
));
1675 if (len
!= reallen
) {
1676 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1681 /* don't trust what the kernel says, validate! */
1682 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1683 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1688 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1693 * send message to a socket.
1695 * others: success and return length sent.
1699 pfkey_send(so
, msg
, len
)
1701 struct sadb_msg
*msg
;
1704 if ((len
= send(so
, (caddr_t
)msg
, len
, 0)) < 0) {
1705 __ipsec_set_strerror(strerror(errno
));
1709 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1715 * NOTE: These functions are derived from netkey/key.c in KAME.
1718 * set the pointer to each header in this message buffer.
1719 * IN: msg: pointer to message buffer.
1720 * mhp: pointer to the buffer initialized like below:
1721 * caddr_t mhp[SADB_EXT_MAX + 1];
1725 * XXX should be rewritten to obtain length explicitly
1728 pfkey_align(msg
, mhp
)
1729 struct sadb_msg
*msg
;
1732 struct sadb_ext
*ext
;
1735 caddr_t ep
; /* XXX should be passed from upper layer */
1737 /* validity check */
1738 if (msg
== NULL
|| mhp
== NULL
) {
1739 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1744 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1747 mhp
[0] = (caddr_t
)msg
;
1751 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1753 /* skip base header */
1754 p
+= sizeof(struct sadb_msg
);
1757 ext
= (struct sadb_ext
*)p
;
1758 if (ep
< p
+ sizeof(*ext
) || (size_t)PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1759 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1760 /* invalid format */
1764 /* duplicate check */
1765 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1766 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1767 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1772 switch (ext
->sadb_ext_type
) {
1774 case SADB_EXT_LIFETIME_CURRENT
:
1775 case SADB_EXT_LIFETIME_HARD
:
1776 case SADB_EXT_LIFETIME_SOFT
:
1777 case SADB_EXT_ADDRESS_SRC
:
1778 case SADB_EXT_ADDRESS_DST
:
1779 case SADB_EXT_ADDRESS_PROXY
:
1780 case SADB_EXT_KEY_AUTH
:
1781 /* XXX should to be check weak keys. */
1782 case SADB_EXT_KEY_ENCRYPT
:
1783 /* XXX should to be check weak keys. */
1784 case SADB_EXT_IDENTITY_SRC
:
1785 case SADB_EXT_IDENTITY_DST
:
1786 case SADB_EXT_SENSITIVITY
:
1787 case SADB_EXT_PROPOSAL
:
1788 case SADB_EXT_SUPPORTED_AUTH
:
1789 case SADB_EXT_SUPPORTED_ENCRYPT
:
1790 case SADB_EXT_SPIRANGE
:
1791 case SADB_X_EXT_POLICY
:
1792 case SADB_X_EXT_SA2
:
1793 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
1796 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1800 p
+= PFKEY_EXTLEN(ext
);
1804 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1808 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1813 * check basic usage for sadb_msg,
1814 * NOTE: This routine is derived from netkey/key.c in KAME.
1815 * IN: msg: pointer to message buffer.
1816 * mhp: pointer to the buffer initialized like below:
1818 * caddr_t mhp[SADB_EXT_MAX + 1];
1827 struct sadb_msg
*msg
;
1829 /* validity check */
1830 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1831 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1835 msg
= (struct sadb_msg
*)mhp
[0];
1838 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1839 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1844 if (msg
->sadb_msg_type
> SADB_MAX
) {
1845 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1850 switch (msg
->sadb_msg_satype
) {
1851 case SADB_SATYPE_UNSPEC
:
1852 switch (msg
->sadb_msg_type
) {
1860 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1864 case SADB_SATYPE_ESP
:
1865 case SADB_SATYPE_AH
:
1866 case SADB_X_SATYPE_IPCOMP
:
1867 switch (msg
->sadb_msg_type
) {
1869 case SADB_X_SPDDELETE
:
1871 case SADB_X_SPDDUMP
:
1872 case SADB_X_SPDFLUSH
:
1873 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1877 case SADB_SATYPE_RSVP
:
1878 case SADB_SATYPE_OSPFV2
:
1879 case SADB_SATYPE_RIPV2
:
1880 case SADB_SATYPE_MIP
:
1881 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1883 case 1: /* XXX: What does it do ? */
1884 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1888 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1892 /* check field of upper layer protocol and address family */
1893 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1894 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1895 struct sadb_address
*src0
, *dst0
;
1897 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1898 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1900 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1901 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1905 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1906 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1907 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1911 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
1916 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1921 * prefixlen == 0 is valid because there must be the case
1922 * all addresses are matched.
1926 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1931 * set data into sadb_msg.
1932 * `buf' must has been allocated sufficiently.
1935 pfkey_setsadbmsg(buf
, lim
, type
, tlen
, satype
, seq
, pid
)
1946 p
= (struct sadb_msg
*)buf
;
1947 len
= sizeof(struct sadb_msg
);
1949 if (buf
+ len
> lim
)
1953 p
->sadb_msg_version
= PF_KEY_V2
;
1954 p
->sadb_msg_type
= type
;
1955 p
->sadb_msg_errno
= 0;
1956 p
->sadb_msg_satype
= satype
;
1957 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
1958 p
->sadb_msg_reserved
= 0;
1959 p
->sadb_msg_seq
= seq
;
1960 p
->sadb_msg_pid
= (u_int32_t
)pid
;
1966 * copy secasvar data into sadb_address.
1967 * `buf' must has been allocated sufficiently.
1970 pfkey_setsadbsa(buf
, lim
, spi
, wsize
, auth
, enc
, flags
)
1973 u_int32_t spi
, flags
;
1974 u_int wsize
, auth
, enc
;
1979 p
= (struct sadb_sa
*)buf
;
1980 len
= sizeof(struct sadb_sa
);
1982 if (buf
+ len
> lim
)
1986 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
1987 p
->sadb_sa_exttype
= SADB_EXT_SA
;
1988 p
->sadb_sa_spi
= spi
;
1989 p
->sadb_sa_replay
= wsize
;
1990 p
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
1991 p
->sadb_sa_auth
= auth
;
1992 p
->sadb_sa_encrypt
= enc
;
1993 p
->sadb_sa_flags
= flags
;
1999 * set data into sadb_address.
2000 * `buf' must has been allocated sufficiently.
2001 * prefixlen is in bits.
2004 pfkey_setsadbaddr(buf
, lim
, exttype
, saddr
, prefixlen
, ul_proto
)
2008 struct sockaddr
*saddr
;
2012 struct sadb_address
*p
;
2015 p
= (struct sadb_address
*)buf
;
2016 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
2018 if (buf
+ len
> lim
)
2022 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2023 p
->sadb_address_exttype
= exttype
& 0xffff;
2024 p
->sadb_address_proto
= ul_proto
& 0xff;
2025 p
->sadb_address_prefixlen
= prefixlen
;
2026 p
->sadb_address_reserved
= 0;
2028 memcpy(p
+ 1, saddr
, saddr
->sa_len
);
2034 * set sadb_key structure after clearing buffer with zero.
2035 * OUT: the pointer of buf + len.
2038 pfkey_setsadbkey(buf
, lim
, type
, key
, keylen
)
2047 p
= (struct sadb_key
*)buf
;
2048 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2050 if (buf
+ len
> lim
)
2054 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2055 p
->sadb_key_exttype
= type
;
2056 p
->sadb_key_bits
= keylen
<< 3;
2057 p
->sadb_key_reserved
= 0;
2059 memcpy(p
+ 1, key
, keylen
);
2065 * set sadb_lifetime structure after clearing buffer with zero.
2066 * OUT: the pointer of buf + len.
2069 pfkey_setsadblifetime(buf
, lim
, type
, l_alloc
, l_bytes
, l_addtime
, l_usetime
)
2073 u_int32_t l_alloc
, l_bytes
, l_addtime
, l_usetime
;
2075 struct sadb_lifetime
*p
;
2078 p
= (struct sadb_lifetime
*)buf
;
2079 len
= sizeof(struct sadb_lifetime
);
2081 if (buf
+ len
> lim
)
2085 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2086 p
->sadb_lifetime_exttype
= type
;
2089 case SADB_EXT_LIFETIME_SOFT
:
2090 p
->sadb_lifetime_allocations
2091 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2092 p
->sadb_lifetime_bytes
2093 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2094 p
->sadb_lifetime_addtime
2095 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2096 p
->sadb_lifetime_usetime
2097 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2099 case SADB_EXT_LIFETIME_HARD
:
2100 p
->sadb_lifetime_allocations
= l_alloc
;
2101 p
->sadb_lifetime_bytes
= l_bytes
;
2102 p
->sadb_lifetime_addtime
= l_addtime
;
2103 p
->sadb_lifetime_usetime
= l_usetime
;
2111 * copy secasvar data into sadb_address.
2112 * `buf' must has been allocated sufficiently.
2115 pfkey_setsadbxsa2(buf
, lim
, mode0
, reqid
)
2121 struct sadb_x_sa2
*p
;
2122 u_int8_t mode
= mode0
& 0xff;
2125 p
= (struct sadb_x_sa2
*)buf
;
2126 len
= sizeof(struct sadb_x_sa2
);
2128 if (buf
+ len
> lim
)
2132 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2133 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2134 p
->sadb_x_sa2_mode
= mode
;
2135 p
->sadb_x_sa2_reqid
= reqid
;
2140 #endif /* ndef MDNS_NO_IPSEC */