1 /* $KAME: pfkey.c,v 1.46 2003/08/26 03:37:06 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <net/pfkeyv2.h>
41 #include <netipsec/key_var.h>
42 #include <netinet/in.h>
43 #include <netipsec/ipsec.h>
51 #include "ipsec_strerror.h"
54 #define CALLOC(size, cast) (cast)calloc(1, (size))
56 static int findsupportedmap(int);
57 static int setsupportedmap(struct sadb_supported *);
58 static struct sadb_alg *findsupportedalg(u_int, u_int);
59 static int pfkey_send_x1(int, u_int, u_int, u_int, struct sockaddr *,
60 struct sockaddr *, u_int32_t, u_int32_t, u_int, caddr_t,
61 u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int32_t,
62 u_int32_t, u_int32_t, u_int32_t);
63 static int pfkey_send_x2(int, u_int, u_int, u_int,
64 struct sockaddr *, struct sockaddr *, u_int32_t);
65 static int pfkey_send_x3(int, u_int, u_int);
66 static int pfkey_send_x4(int, u_int, struct sockaddr *, u_int,
67 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
68 char *, int, u_int32_t);
69 static int pfkey_send_x5(int, u_int, u_int32_t);
71 static caddr_t pfkey_setsadbmsg(caddr_t, caddr_t, u_int, u_int,
72 u_int, u_int32_t, pid_t);
73 static caddr_t pfkey_setsadbsa(caddr_t, caddr_t, u_int32_t, u_int,
74 u_int, u_int, u_int32_t);
75 static caddr_t pfkey_setsadbxreplay(caddr_t, caddr_t, uint32_t);
76 static caddr_t pfkey_setsadbaddr(caddr_t, caddr_t, u_int,
77 struct sockaddr *, u_int, u_int);
78 static caddr_t pfkey_setsadbkey(caddr_t, caddr_t, u_int, caddr_t, u_int);
79 static caddr_t pfkey_setsadblifetime(caddr_t, caddr_t, u_int, u_int32_t,
80 u_int32_t, u_int32_t, u_int32_t);
81 static caddr_t pfkey_setsadbxsa2(caddr_t, caddr_t, u_int32_t, u_int32_t);
84 * make and search supported algorithm structure.
86 static struct sadb_supported *ipsec_supported[] = { NULL, NULL, NULL, NULL };
88 static int supported_map[] = {
92 SADB_X_SATYPE_TCPSIGNATURE
96 findsupportedmap(satype)
101 for (i = 0; i < sizeof(supported_map)/sizeof(supported_map[0]); i++)
102 if (supported_map[i] == satype)
107 static struct sadb_alg *
108 findsupportedalg(satype, alg_id)
109 u_int satype, alg_id;
116 algno = findsupportedmap(satype);
118 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
121 if (ipsec_supported[algno] == NULL) {
122 __ipsec_errcode = EIPSEC_DO_GET_SUPP_LIST;
126 tlen = ipsec_supported[algno]->sadb_supported_len
127 - sizeof(struct sadb_supported);
128 p = (caddr_t)(ipsec_supported[algno] + 1);
130 if (tlen < sizeof(struct sadb_alg)) {
134 if (((struct sadb_alg *)p)->sadb_alg_id == alg_id)
135 return (struct sadb_alg *)p;
137 tlen -= sizeof(struct sadb_alg);
138 p += sizeof(struct sadb_alg);
141 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
147 struct sadb_supported *sup;
149 struct sadb_supported **ipsup;
151 switch (sup->sadb_supported_exttype) {
152 case SADB_EXT_SUPPORTED_AUTH:
153 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_AH)];
155 case SADB_EXT_SUPPORTED_ENCRYPT:
156 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_ESP)];
159 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
166 *ipsup = malloc(sup->sadb_supported_len);
168 __ipsec_set_strerror(strerror(errno));
171 memcpy(*ipsup, sup, sup->sadb_supported_len);
177 * check key length against algorithm specified.
178 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
179 * augument, and only calls to ipsec_check_keylen2();
180 * keylen is the unit of bit.
186 ipsec_check_keylen(supported, alg_id, keylen)
195 case SADB_EXT_SUPPORTED_AUTH:
196 satype = SADB_SATYPE_AH;
198 case SADB_EXT_SUPPORTED_ENCRYPT:
199 satype = SADB_SATYPE_ESP;
202 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
206 return ipsec_check_keylen2(satype, alg_id, keylen);
210 * check key length against algorithm specified.
211 * satype is one of satype defined at pfkeyv2.h.
212 * keylen is the unit of bit.
218 ipsec_check_keylen2(satype, alg_id, keylen)
223 struct sadb_alg *alg;
225 alg = findsupportedalg(satype, alg_id);
229 if (keylen < alg->sadb_alg_minbits || keylen > alg->sadb_alg_maxbits) {
230 __ipsec_errcode = EIPSEC_INVAL_KEYLEN;
234 __ipsec_errcode = EIPSEC_NO_ERROR;
239 * get max/min key length against algorithm specified.
240 * satype is one of satype defined at pfkeyv2.h.
241 * keylen is the unit of bit.
247 ipsec_get_keylen(supported, alg_id, alg0)
248 u_int supported, alg_id;
249 struct sadb_alg *alg0;
251 struct sadb_alg *alg;
256 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
261 case SADB_EXT_SUPPORTED_AUTH:
262 satype = SADB_SATYPE_AH;
264 case SADB_EXT_SUPPORTED_ENCRYPT:
265 satype = SADB_SATYPE_ESP;
268 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
272 alg = findsupportedalg(satype, alg_id);
276 memcpy(alg0, alg, sizeof(*alg0));
278 __ipsec_errcode = EIPSEC_NO_ERROR;
283 * set the rate for SOFT lifetime against HARD one.
284 * If rate is more than 100 or equal to zero, then set to 100.
286 static u_int soft_lifetime_allocations_rate = PFKEY_SOFT_LIFETIME_RATE;
287 static u_int soft_lifetime_bytes_rate = PFKEY_SOFT_LIFETIME_RATE;
288 static u_int soft_lifetime_addtime_rate = PFKEY_SOFT_LIFETIME_RATE;
289 static u_int soft_lifetime_usetime_rate = PFKEY_SOFT_LIFETIME_RATE;
292 pfkey_set_softrate(type, rate)
295 __ipsec_errcode = EIPSEC_NO_ERROR;
297 if (rate > 100 || rate == 0)
301 case SADB_X_LIFETIME_ALLOCATIONS:
302 soft_lifetime_allocations_rate = rate;
304 case SADB_X_LIFETIME_BYTES:
305 soft_lifetime_bytes_rate = rate;
307 case SADB_X_LIFETIME_ADDTIME:
308 soft_lifetime_addtime_rate = rate;
310 case SADB_X_LIFETIME_USETIME:
311 soft_lifetime_usetime_rate = rate;
315 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
320 * get current rate for SOFT lifetime against HARD one.
321 * ATTENTION: ~0 is returned if invalid type was passed.
324 pfkey_get_softrate(type)
328 case SADB_X_LIFETIME_ALLOCATIONS:
329 return soft_lifetime_allocations_rate;
330 case SADB_X_LIFETIME_BYTES:
331 return soft_lifetime_bytes_rate;
332 case SADB_X_LIFETIME_ADDTIME:
333 return soft_lifetime_addtime_rate;
334 case SADB_X_LIFETIME_USETIME:
335 return soft_lifetime_usetime_rate;
342 * sending SADB_GETSPI message to the kernel.
344 * positive: success and return length sent.
345 * -1 : error occured, and set errno.
348 pfkey_send_getspi(so, satype, mode, src, dst, min, max, reqid, seq)
351 struct sockaddr *src, *dst;
352 u_int32_t min, max, reqid, seq;
354 struct sadb_msg *newmsg;
357 int need_spirange = 0;
362 if (src == NULL || dst == NULL) {
363 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
366 if (src->sa_family != dst->sa_family) {
367 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
370 if (min > max || (min > 0 && min <= 255)) {
371 __ipsec_errcode = EIPSEC_INVAL_SPI;
374 switch (src->sa_family) {
376 plen = sizeof(struct in_addr) << 3;
379 plen = sizeof(struct in6_addr) << 3;
382 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
386 /* create new sadb_msg to send. */
387 len = sizeof(struct sadb_msg)
388 + sizeof(struct sadb_x_sa2)
389 + sizeof(struct sadb_address)
390 + PFKEY_ALIGN8(src->sa_len)
391 + sizeof(struct sadb_address)
392 + PFKEY_ALIGN8(dst->sa_len);
394 if (min > 255 && max < ~0) {
396 len += sizeof(struct sadb_spirange);
399 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
400 __ipsec_set_strerror(strerror(errno));
403 ep = ((caddr_t)newmsg) + len;
405 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_GETSPI,
406 len, satype, seq, getpid());
412 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
418 /* set sadb_address for source */
419 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
426 /* set sadb_address for destination */
427 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
434 /* proccessing spi range */
436 struct sadb_spirange spirange;
438 if (p + sizeof(spirange) > ep) {
443 memset(&spirange, 0, sizeof(spirange));
444 spirange.sadb_spirange_len = PFKEY_UNIT64(sizeof(spirange));
445 spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
446 spirange.sadb_spirange_min = min;
447 spirange.sadb_spirange_max = max;
449 memcpy(p, &spirange, sizeof(spirange));
451 p += sizeof(spirange);
459 len = pfkey_send(so, newmsg, len);
465 __ipsec_errcode = EIPSEC_NO_ERROR;
470 * sending SADB_UPDATE message to the kernel.
471 * The length of key material is a_keylen + e_keylen.
473 * positive: success and return length sent.
474 * -1 : error occured, and set errno.
477 pfkey_send_update(so, satype, mode, src, dst, spi, reqid, wsize,
478 keymat, e_type, e_keylen, a_type, a_keylen, flags,
479 l_alloc, l_bytes, l_addtime, l_usetime, seq)
481 u_int satype, mode, wsize;
482 struct sockaddr *src, *dst;
483 u_int32_t spi, reqid;
485 u_int e_type, e_keylen, a_type, a_keylen, flags;
487 u_int64_t l_bytes, l_addtime, l_usetime;
491 if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
493 keymat, e_type, e_keylen, a_type, a_keylen, flags,
494 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0)
501 * sending SADB_ADD message to the kernel.
502 * The length of key material is a_keylen + e_keylen.
504 * positive: success and return length sent.
505 * -1 : error occured, and set errno.
508 pfkey_send_add(so, satype, mode, src, dst, spi, reqid, wsize,
509 keymat, e_type, e_keylen, a_type, a_keylen, flags,
510 l_alloc, l_bytes, l_addtime, l_usetime, seq)
512 u_int satype, mode, wsize;
513 struct sockaddr *src, *dst;
514 u_int32_t spi, reqid;
516 u_int e_type, e_keylen, a_type, a_keylen, flags;
518 u_int64_t l_bytes, l_addtime, l_usetime;
522 if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
524 keymat, e_type, e_keylen, a_type, a_keylen, flags,
525 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0)
532 * sending SADB_DELETE message to the kernel.
534 * positive: success and return length sent.
535 * -1 : error occured, and set errno.
538 pfkey_send_delete(so, satype, mode, src, dst, spi)
541 struct sockaddr *src, *dst;
545 if ((len = pfkey_send_x2(so, SADB_DELETE, satype, mode, src, dst, spi)) < 0)
552 * sending SADB_DELETE without spi to the kernel. This is
553 * the "delete all" request (an extension also present in
557 * positive: success and return length sent
558 * -1 : error occured, and set errno
561 pfkey_send_delete_all(so, satype, mode, src, dst)
564 struct sockaddr *src, *dst;
566 struct sadb_msg *newmsg;
573 if (src == NULL || dst == NULL) {
574 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
577 if (src->sa_family != dst->sa_family) {
578 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
581 switch (src->sa_family) {
583 plen = sizeof(struct in_addr) << 3;
586 plen = sizeof(struct in6_addr) << 3;
589 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
593 /* create new sadb_msg to reply. */
594 len = sizeof(struct sadb_msg)
595 + sizeof(struct sadb_address)
596 + PFKEY_ALIGN8(src->sa_len)
597 + sizeof(struct sadb_address)
598 + PFKEY_ALIGN8(dst->sa_len);
600 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
601 __ipsec_set_strerror(strerror(errno));
604 ep = ((caddr_t)newmsg) + len;
606 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_DELETE, len, satype, 0,
612 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
618 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
626 len = pfkey_send(so, newmsg, len);
632 __ipsec_errcode = EIPSEC_NO_ERROR;
637 * sending SADB_GET message to the kernel.
639 * positive: success and return length sent.
640 * -1 : error occured, and set errno.
643 pfkey_send_get(so, satype, mode, src, dst, spi)
646 struct sockaddr *src, *dst;
650 if ((len = pfkey_send_x2(so, SADB_GET, satype, mode, src, dst, spi)) < 0)
657 * sending SADB_REGISTER message to the kernel.
659 * positive: success and return length sent.
660 * -1 : error occured, and set errno.
663 pfkey_send_register(so, satype)
669 if (satype == SADB_SATYPE_UNSPEC) {
671 algno < sizeof(supported_map)/sizeof(supported_map[0]);
673 if (ipsec_supported[algno]) {
674 free(ipsec_supported[algno]);
675 ipsec_supported[algno] = NULL;
679 algno = findsupportedmap(satype);
681 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
685 if (ipsec_supported[algno]) {
686 free(ipsec_supported[algno]);
687 ipsec_supported[algno] = NULL;
691 if ((len = pfkey_send_x3(so, SADB_REGISTER, satype)) < 0)
698 * receiving SADB_REGISTER message from the kernel, and copy buffer for
699 * sadb_supported returned into ipsec_supported.
701 * 0: success and return length sent.
702 * -1: error occured, and set errno.
705 pfkey_recv_register(so)
708 pid_t pid = getpid();
709 struct sadb_msg *newmsg;
712 /* receive message */
714 if ((newmsg = pfkey_recv(so)) == NULL)
716 if (newmsg->sadb_msg_type == SADB_REGISTER &&
717 newmsg->sadb_msg_pid == pid)
723 newmsg->sadb_msg_len = PFKEY_UNUNIT64(newmsg->sadb_msg_len);
725 error = pfkey_set_supported(newmsg, newmsg->sadb_msg_len);
729 __ipsec_errcode = EIPSEC_NO_ERROR;
735 * receiving SADB_REGISTER message from the kernel, and copy buffer for
736 * sadb_supported returned into ipsec_supported.
737 * NOTE: sadb_msg_len must be host order.
739 * tlen: msg length, it's to makeing sure.
741 * 0: success and return length sent.
742 * -1: error occured, and set errno.
745 pfkey_set_supported(msg, tlen)
746 struct sadb_msg *msg;
749 struct sadb_supported *sup;
754 if (msg->sadb_msg_len != tlen) {
755 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
762 p += sizeof(struct sadb_msg);
765 sup = (struct sadb_supported *)p;
766 if (ep < p + sizeof(*sup) ||
767 PFKEY_EXTLEN(sup) < sizeof(*sup) ||
768 ep < p + sup->sadb_supported_len) {
773 switch (sup->sadb_supported_exttype) {
774 case SADB_EXT_SUPPORTED_AUTH:
775 case SADB_EXT_SUPPORTED_ENCRYPT:
778 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
783 sup->sadb_supported_len = PFKEY_EXTLEN(sup);
785 /* set supported map */
786 if (setsupportedmap(sup) != 0)
789 p += sup->sadb_supported_len;
793 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
797 __ipsec_errcode = EIPSEC_NO_ERROR;
803 * sending SADB_FLUSH message to the kernel.
805 * positive: success and return length sent.
806 * -1 : error occured, and set errno.
809 pfkey_send_flush(so, satype)
815 if ((len = pfkey_send_x3(so, SADB_FLUSH, satype)) < 0)
822 * sending SADB_DUMP message to the kernel.
824 * positive: success and return length sent.
825 * -1 : error occured, and set errno.
828 pfkey_send_dump(so, satype)
834 if ((len = pfkey_send_x3(so, SADB_DUMP, satype)) < 0)
841 * sending SADB_X_PROMISC message to the kernel.
842 * NOTE that this function handles promisc mode toggle only.
844 * flag: set promisc off if zero, set promisc on if non-zero.
846 * positive: success and return length sent.
847 * -1 : error occured, and set errno.
848 * 0 : error occured, and set errno.
849 * others: a pointer to new allocated buffer in which supported
853 pfkey_send_promisc_toggle(so, flag)
859 if ((len = pfkey_send_x3(so, SADB_X_PROMISC, (flag ? 1 : 0))) < 0)
866 * sending SADB_X_SPDADD message to the kernel.
868 * positive: success and return length sent.
869 * -1 : error occured, and set errno.
872 pfkey_send_spdadd(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
874 struct sockaddr *src, *dst;
875 u_int prefs, prefd, proto;
882 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
883 src, prefs, dst, prefd, proto,
885 policy, policylen, seq)) < 0)
892 * sending SADB_X_SPDADD message to the kernel.
894 * positive: success and return length sent.
895 * -1 : error occured, and set errno.
898 pfkey_send_spdadd2(so, src, prefs, dst, prefd, proto, ltime, vtime,
899 policy, policylen, seq)
901 struct sockaddr *src, *dst;
902 u_int prefs, prefd, proto;
903 u_int64_t ltime, vtime;
910 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
911 src, prefs, dst, prefd, proto,
913 policy, policylen, seq)) < 0)
920 * sending SADB_X_SPDUPDATE message to the kernel.
922 * positive: success and return length sent.
923 * -1 : error occured, and set errno.
926 pfkey_send_spdupdate(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
928 struct sockaddr *src, *dst;
929 u_int prefs, prefd, proto;
936 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
937 src, prefs, dst, prefd, proto,
939 policy, policylen, seq)) < 0)
946 * sending SADB_X_SPDUPDATE message to the kernel.
948 * positive: success and return length sent.
949 * -1 : error occured, and set errno.
952 pfkey_send_spdupdate2(so, src, prefs, dst, prefd, proto, ltime, vtime,
953 policy, policylen, seq)
955 struct sockaddr *src, *dst;
956 u_int prefs, prefd, proto;
957 u_int64_t ltime, vtime;
964 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
965 src, prefs, dst, prefd, proto,
967 policy, policylen, seq)) < 0)
974 * sending SADB_X_SPDDELETE message to the kernel.
976 * positive: success and return length sent.
977 * -1 : error occured, and set errno.
980 pfkey_send_spddelete(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
982 struct sockaddr *src, *dst;
983 u_int prefs, prefd, proto;
990 if (policylen != sizeof(struct sadb_x_policy)) {
991 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
995 if ((len = pfkey_send_x4(so, SADB_X_SPDDELETE,
996 src, prefs, dst, prefd, proto,
998 policy, policylen, seq)) < 0)
1005 * sending SADB_X_SPDDELETE message to the kernel.
1007 * positive: success and return length sent.
1008 * -1 : error occured, and set errno.
1011 pfkey_send_spddelete2(so, spid)
1017 if ((len = pfkey_send_x5(so, SADB_X_SPDDELETE2, spid)) < 0)
1024 * sending SADB_X_SPDGET message to the kernel.
1026 * positive: success and return length sent.
1027 * -1 : error occured, and set errno.
1030 pfkey_send_spdget(so, spid)
1036 if ((len = pfkey_send_x5(so, SADB_X_SPDGET, spid)) < 0)
1043 * sending SADB_X_SPDSETIDX message to the kernel.
1045 * positive: success and return length sent.
1046 * -1 : error occured, and set errno.
1049 pfkey_send_spdsetidx(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1051 struct sockaddr *src, *dst;
1052 u_int prefs, prefd, proto;
1059 if (policylen != sizeof(struct sadb_x_policy)) {
1060 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1064 if ((len = pfkey_send_x4(so, SADB_X_SPDSETIDX,
1065 src, prefs, dst, prefd, proto,
1067 policy, policylen, seq)) < 0)
1074 * sending SADB_SPDFLUSH message to the kernel.
1076 * positive: success and return length sent.
1077 * -1 : error occured, and set errno.
1080 pfkey_send_spdflush(so)
1085 if ((len = pfkey_send_x3(so, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC)) < 0)
1092 * sending SADB_SPDDUMP message to the kernel.
1094 * positive: success and return length sent.
1095 * -1 : error occured, and set errno.
1098 pfkey_send_spddump(so)
1103 if ((len = pfkey_send_x3(so, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC)) < 0)
1109 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1111 pfkey_send_x1(so, type, satype, mode, src, dst, spi, reqid, wsize,
1112 keymat, e_type, e_keylen, a_type, a_keylen, flags,
1113 l_alloc, l_bytes, l_addtime, l_usetime, seq)
1115 u_int type, satype, mode;
1116 struct sockaddr *src, *dst;
1117 u_int32_t spi, reqid;
1120 u_int e_type, e_keylen, a_type, a_keylen, flags;
1121 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime, seq;
1123 struct sadb_msg *newmsg;
1129 /* validity check */
1130 if (src == NULL || dst == NULL) {
1131 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1134 if (src->sa_family != dst->sa_family) {
1135 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1138 switch (src->sa_family) {
1140 plen = sizeof(struct in_addr) << 3;
1143 plen = sizeof(struct in6_addr) << 3;
1146 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1151 case SADB_SATYPE_ESP:
1152 if (e_type == SADB_EALG_NONE) {
1153 __ipsec_errcode = EIPSEC_NO_ALGS;
1157 case SADB_SATYPE_AH:
1158 if (e_type != SADB_EALG_NONE) {
1159 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1162 if (a_type == SADB_AALG_NONE) {
1163 __ipsec_errcode = EIPSEC_NO_ALGS;
1167 case SADB_X_SATYPE_IPCOMP:
1168 if (e_type == SADB_X_CALG_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_TCPSIGNATURE:
1178 if (e_type != SADB_EALG_NONE) {
1179 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1182 if (a_type != SADB_X_AALG_TCP_MD5) {
1183 __ipsec_errcode = EIPSEC_INVAL_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 (wsize > UINT8_MAX) {
1204 if (wsize > (UINT32_MAX - 32) >> 3) {
1205 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1208 len += sizeof(struct sadb_x_sa_replay);
1210 if (e_type != SADB_EALG_NONE)
1211 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(e_keylen));
1212 if (a_type != SADB_AALG_NONE)
1213 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(a_keylen));
1215 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1216 __ipsec_set_strerror(strerror(errno));
1219 ep = ((caddr_t)newmsg) + len;
1221 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1222 satype, seq, getpid());
1227 p = pfkey_setsadbsa(p, ep, spi, wsize, a_type, e_type, flags);
1232 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
1237 if (wsize > UINT8_MAX) {
1238 p = pfkey_setsadbxreplay(p, ep, wsize);
1244 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1250 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1257 if (e_type != SADB_EALG_NONE) {
1258 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_ENCRYPT,
1265 if (a_type != SADB_AALG_NONE) {
1266 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_AUTH,
1267 keymat + e_keylen, a_keylen);
1274 /* set sadb_lifetime for destination */
1275 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1276 l_alloc, l_bytes, l_addtime, l_usetime);
1281 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_SOFT,
1282 l_alloc, l_bytes, l_addtime, l_usetime);
1283 if (!p || p != ep) {
1289 len = pfkey_send(so, newmsg, len);
1295 __ipsec_errcode = EIPSEC_NO_ERROR;
1299 /* sending SADB_DELETE or SADB_GET message to the kernel */
1301 pfkey_send_x2(so, type, satype, mode, src, dst, spi)
1303 u_int type, satype, mode;
1304 struct sockaddr *src, *dst;
1307 struct sadb_msg *newmsg;
1313 /* validity check */
1314 if (src == NULL || dst == NULL) {
1315 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1318 if (src->sa_family != dst->sa_family) {
1319 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1322 switch (src->sa_family) {
1324 plen = sizeof(struct in_addr) << 3;
1327 plen = sizeof(struct in6_addr) << 3;
1330 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1334 /* create new sadb_msg to reply. */
1335 len = sizeof(struct sadb_msg)
1336 + sizeof(struct sadb_sa)
1337 + sizeof(struct sadb_address)
1338 + PFKEY_ALIGN8(src->sa_len)
1339 + sizeof(struct sadb_address)
1340 + PFKEY_ALIGN8(dst->sa_len);
1342 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1343 __ipsec_set_strerror(strerror(errno));
1346 ep = ((caddr_t)newmsg) + len;
1348 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1354 p = pfkey_setsadbsa(p, ep, spi, 0, 0, 0, 0);
1359 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1365 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1367 if (!p || p != ep) {
1373 len = pfkey_send(so, newmsg, len);
1379 __ipsec_errcode = EIPSEC_NO_ERROR;
1384 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1388 pfkey_send_x3(so, type, satype)
1392 struct sadb_msg *newmsg;
1397 /* validity check */
1399 case SADB_X_PROMISC:
1400 if (satype != 0 && satype != 1) {
1401 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1407 case SADB_SATYPE_UNSPEC:
1408 case SADB_SATYPE_AH:
1409 case SADB_SATYPE_ESP:
1410 case SADB_X_SATYPE_IPCOMP:
1411 case SADB_X_SATYPE_TCPSIGNATURE:
1414 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1419 /* create new sadb_msg to send. */
1420 len = sizeof(struct sadb_msg);
1422 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1423 __ipsec_set_strerror(strerror(errno));
1426 ep = ((caddr_t)newmsg) + len;
1428 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1430 if (!p || p != ep) {
1436 len = pfkey_send(so, newmsg, len);
1442 __ipsec_errcode = EIPSEC_NO_ERROR;
1446 /* sending SADB_X_SPDADD message to the kernel */
1448 pfkey_send_x4(so, type, src, prefs, dst, prefd, proto,
1449 ltime, vtime, policy, policylen, seq)
1451 struct sockaddr *src, *dst;
1452 u_int type, prefs, prefd, proto;
1453 u_int64_t ltime, vtime;
1458 struct sadb_msg *newmsg;
1464 /* validity check */
1465 if (src == NULL || dst == NULL) {
1466 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1469 if (src->sa_family != dst->sa_family) {
1470 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1474 switch (src->sa_family) {
1476 plen = sizeof(struct in_addr) << 3;
1479 plen = sizeof(struct in6_addr) << 3;
1482 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1485 if (prefs > plen || prefd > plen) {
1486 __ipsec_errcode = EIPSEC_INVAL_PREFIXLEN;
1490 /* create new sadb_msg to reply. */
1491 len = sizeof(struct sadb_msg)
1492 + sizeof(struct sadb_address)
1493 + PFKEY_ALIGN8(src->sa_len)
1494 + sizeof(struct sadb_address)
1495 + PFKEY_ALIGN8(src->sa_len)
1496 + sizeof(struct sadb_lifetime)
1499 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1500 __ipsec_set_strerror(strerror(errno));
1503 ep = ((caddr_t)newmsg) + len;
1505 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1506 SADB_SATYPE_UNSPEC, seq, getpid());
1511 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, prefs, proto);
1516 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, prefd, proto);
1521 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1522 0, 0, ltime, vtime);
1523 if (!p || p + policylen != ep) {
1527 memcpy(p, policy, policylen);
1530 len = pfkey_send(so, newmsg, len);
1536 __ipsec_errcode = EIPSEC_NO_ERROR;
1540 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1542 pfkey_send_x5(so, type, spid)
1547 struct sadb_msg *newmsg;
1548 struct sadb_x_policy xpl;
1553 /* create new sadb_msg to reply. */
1554 len = sizeof(struct sadb_msg)
1557 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1558 __ipsec_set_strerror(strerror(errno));
1561 ep = ((caddr_t)newmsg) + len;
1563 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1564 SADB_SATYPE_UNSPEC, 0, getpid());
1570 if (p + sizeof(xpl) != ep) {
1574 memset(&xpl, 0, sizeof(xpl));
1575 xpl.sadb_x_policy_len = PFKEY_UNIT64(sizeof(xpl));
1576 xpl.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1577 xpl.sadb_x_policy_id = spid;
1578 memcpy(p, &xpl, sizeof(xpl));
1581 len = pfkey_send(so, newmsg, len);
1587 __ipsec_errcode = EIPSEC_NO_ERROR;
1595 * others : success and return value of socket.
1601 int bufsiz_current, bufsiz_wanted;
1605 if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
1606 __ipsec_set_strerror(strerror(errno));
1611 * This is a temporary workaround for KAME PR 154.
1612 * Don't really care even if it fails.
1614 /* Try to have 128k. If we have more, do not lower it. */
1615 bufsiz_wanted = 128 * 1024;
1616 len = sizeof(bufsiz_current);
1617 ret = getsockopt(so, SOL_SOCKET, SO_SNDBUF,
1618 &bufsiz_current, &len);
1619 if ((ret < 0) || (bufsiz_current < bufsiz_wanted))
1620 (void)setsockopt(so, SOL_SOCKET, SO_SNDBUF,
1621 &bufsiz_wanted, sizeof(bufsiz_wanted));
1623 /* Try to have have at least 2MB. If we have more, do not lower it. */
1624 bufsiz_wanted = 2 * 1024 * 1024;
1625 len = sizeof(bufsiz_current);
1626 ret = getsockopt(so, SOL_SOCKET, SO_RCVBUF,
1627 &bufsiz_current, &len);
1629 bufsiz_current = 128 * 1024;
1631 for (; bufsiz_wanted > bufsiz_current; bufsiz_wanted /= 2) {
1632 if (setsockopt(so, SOL_SOCKET, SO_RCVBUF,
1633 &bufsiz_wanted, sizeof(bufsiz_wanted)) == 0)
1637 __ipsec_errcode = EIPSEC_NO_ERROR;
1653 __ipsec_errcode = EIPSEC_NO_ERROR;
1658 * receive sadb_msg data, and return pointer to new buffer allocated.
1659 * Must free this buffer later.
1661 * NULL : error occured.
1662 * others : a pointer to sadb_msg structure.
1664 * XXX should be rewritten to pass length explicitly
1670 struct sadb_msg buf, *newmsg;
1673 while ((len = recv(so, (caddr_t)&buf, sizeof(buf), MSG_PEEK)) < 0) {
1676 __ipsec_set_strerror(strerror(errno));
1680 if (len < sizeof(buf)) {
1681 recv(so, (caddr_t)&buf, sizeof(buf), 0);
1682 __ipsec_errcode = EIPSEC_MAX;
1686 /* read real message */
1687 reallen = PFKEY_UNUNIT64(buf.sadb_msg_len);
1688 if ((newmsg = CALLOC(reallen, struct sadb_msg *)) == NULL) {
1689 __ipsec_set_strerror(strerror(errno));
1693 while ((len = recv(so, (caddr_t)newmsg, reallen, 0)) < 0) {
1696 __ipsec_set_strerror(strerror(errno));
1701 if (len != reallen) {
1702 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1707 /* don't trust what the kernel says, validate! */
1708 if (PFKEY_UNUNIT64(newmsg->sadb_msg_len) != len) {
1709 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1714 __ipsec_errcode = EIPSEC_NO_ERROR;
1719 * send message to a socket.
1721 * others: success and return length sent.
1725 pfkey_send(so, msg, len)
1727 struct sadb_msg *msg;
1730 if ((len = send(so, (caddr_t)msg, len, 0)) < 0) {
1731 __ipsec_set_strerror(strerror(errno));
1735 __ipsec_errcode = EIPSEC_NO_ERROR;
1741 * NOTE: These functions are derived from netkey/key.c in KAME.
1744 * set the pointer to each header in this message buffer.
1745 * IN: msg: pointer to message buffer.
1746 * mhp: pointer to the buffer initialized like below:
1747 * caddr_t mhp[SADB_EXT_MAX + 1];
1751 * XXX should be rewritten to obtain length explicitly
1754 pfkey_align(msg, mhp)
1755 struct sadb_msg *msg;
1758 struct sadb_ext *ext;
1761 caddr_t ep; /* XXX should be passed from upper layer */
1763 /* validity check */
1764 if (msg == NULL || mhp == NULL) {
1765 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1770 for (i = 0; i < SADB_EXT_MAX + 1; i++)
1773 mhp[0] = (caddr_t)msg;
1777 ep = p + PFKEY_UNUNIT64(msg->sadb_msg_len);
1779 /* skip base header */
1780 p += sizeof(struct sadb_msg);
1783 ext = (struct sadb_ext *)p;
1784 if (ep < p + sizeof(*ext) || PFKEY_EXTLEN(ext) < sizeof(*ext) ||
1785 ep < p + PFKEY_EXTLEN(ext)) {
1786 /* invalid format */
1790 /* duplicate check */
1791 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1792 if (mhp[ext->sadb_ext_type] != NULL) {
1793 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1798 switch (ext->sadb_ext_type) {
1800 case SADB_EXT_LIFETIME_CURRENT:
1801 case SADB_EXT_LIFETIME_HARD:
1802 case SADB_EXT_LIFETIME_SOFT:
1803 case SADB_EXT_ADDRESS_SRC:
1804 case SADB_EXT_ADDRESS_DST:
1805 case SADB_EXT_ADDRESS_PROXY:
1806 case SADB_EXT_KEY_AUTH:
1807 /* XXX should to be check weak keys. */
1808 case SADB_EXT_KEY_ENCRYPT:
1809 /* XXX should to be check weak keys. */
1810 case SADB_EXT_IDENTITY_SRC:
1811 case SADB_EXT_IDENTITY_DST:
1812 case SADB_EXT_SENSITIVITY:
1813 case SADB_EXT_PROPOSAL:
1814 case SADB_EXT_SUPPORTED_AUTH:
1815 case SADB_EXT_SUPPORTED_ENCRYPT:
1816 case SADB_EXT_SPIRANGE:
1817 case SADB_X_EXT_POLICY:
1818 case SADB_X_EXT_SA2:
1819 case SADB_X_EXT_NAT_T_TYPE:
1820 case SADB_X_EXT_NAT_T_SPORT:
1821 case SADB_X_EXT_NAT_T_DPORT:
1822 case SADB_X_EXT_NAT_T_OAI:
1823 case SADB_X_EXT_NAT_T_OAR:
1824 case SADB_X_EXT_NAT_T_FRAG:
1825 case SADB_X_EXT_SA_REPLAY:
1826 case SADB_X_EXT_NEW_ADDRESS_SRC:
1827 case SADB_X_EXT_NEW_ADDRESS_DST:
1828 mhp[ext->sadb_ext_type] = (caddr_t)ext;
1831 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1835 p += PFKEY_EXTLEN(ext);
1839 __ipsec_errcode = EIPSEC_INVAL_SADBMSG;
1843 __ipsec_errcode = EIPSEC_NO_ERROR;
1848 * check basic usage for sadb_msg,
1849 * NOTE: This routine is derived from netkey/key.c in KAME.
1850 * IN: msg: pointer to message buffer.
1851 * mhp: pointer to the buffer initialized like below:
1853 * caddr_t mhp[SADB_EXT_MAX + 1];
1862 struct sadb_msg *msg;
1864 /* validity check */
1865 if (mhp == NULL || mhp[0] == NULL) {
1866 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1870 msg = (struct sadb_msg *)mhp[0];
1873 if (msg->sadb_msg_version != PF_KEY_V2) {
1874 __ipsec_errcode = EIPSEC_INVAL_VERSION;
1879 if (msg->sadb_msg_type > SADB_MAX) {
1880 __ipsec_errcode = EIPSEC_INVAL_MSGTYPE;
1885 switch (msg->sadb_msg_satype) {
1886 case SADB_SATYPE_UNSPEC:
1887 switch (msg->sadb_msg_type) {
1895 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1899 case SADB_SATYPE_ESP:
1900 case SADB_SATYPE_AH:
1901 case SADB_X_SATYPE_IPCOMP:
1902 case SADB_X_SATYPE_TCPSIGNATURE:
1903 switch (msg->sadb_msg_type) {
1905 case SADB_X_SPDDELETE:
1907 case SADB_X_SPDDUMP:
1908 case SADB_X_SPDFLUSH:
1909 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1913 case SADB_SATYPE_RSVP:
1914 case SADB_SATYPE_OSPFV2:
1915 case SADB_SATYPE_RIPV2:
1916 case SADB_SATYPE_MIP:
1917 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
1919 case 1: /* XXX: What does it do ? */
1920 if (msg->sadb_msg_type == SADB_X_PROMISC)
1924 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1928 /* check field of upper layer protocol and address family */
1929 if (mhp[SADB_EXT_ADDRESS_SRC] != NULL
1930 && mhp[SADB_EXT_ADDRESS_DST] != NULL) {
1931 struct sadb_address *src0, *dst0;
1933 src0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_SRC]);
1934 dst0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_DST]);
1936 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
1937 __ipsec_errcode = EIPSEC_PROTO_MISMATCH;
1941 if (PFKEY_ADDR_SADDR(src0)->sa_family
1942 != PFKEY_ADDR_SADDR(dst0)->sa_family) {
1943 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1947 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
1952 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1957 * prefixlen == 0 is valid because there must be the case
1958 * all addresses are matched.
1962 __ipsec_errcode = EIPSEC_NO_ERROR;
1967 * set data into sadb_msg.
1968 * `buf' must has been allocated sufficiently.
1971 pfkey_setsadbmsg(buf, lim, type, tlen, satype, seq, pid)
1982 p = (struct sadb_msg *)buf;
1983 len = sizeof(struct sadb_msg);
1985 if (buf + len > lim)
1989 p->sadb_msg_version = PF_KEY_V2;
1990 p->sadb_msg_type = type;
1991 p->sadb_msg_errno = 0;
1992 p->sadb_msg_satype = satype;
1993 p->sadb_msg_len = PFKEY_UNIT64(tlen);
1994 p->sadb_msg_reserved = 0;
1995 p->sadb_msg_seq = seq;
1996 p->sadb_msg_pid = (u_int32_t)pid;
2002 * copy secasvar data into sadb_address.
2003 * `buf' must has been allocated sufficiently.
2006 pfkey_setsadbsa(buf, lim, spi, wsize, auth, enc, flags)
2009 u_int32_t spi, flags;
2010 u_int wsize, auth, enc;
2015 p = (struct sadb_sa *)buf;
2016 len = sizeof(struct sadb_sa);
2018 if (buf + len > lim)
2022 p->sadb_sa_len = PFKEY_UNIT64(len);
2023 p->sadb_sa_exttype = SADB_EXT_SA;
2024 p->sadb_sa_spi = spi;
2025 p->sadb_sa_replay = wsize > UINT8_MAX ? UINT8_MAX: wsize;
2026 p->sadb_sa_state = SADB_SASTATE_LARVAL;
2027 p->sadb_sa_auth = auth;
2028 p->sadb_sa_encrypt = enc;
2029 p->sadb_sa_flags = flags;
2035 * Set data into sadb_x_sa_replay.
2036 * `buf' must has been allocated sufficiently.
2039 pfkey_setsadbxreplay(caddr_t buf, caddr_t lim, uint32_t wsize)
2041 struct sadb_x_sa_replay *p;
2044 p = (struct sadb_x_sa_replay *)buf;
2045 len = sizeof(struct sadb_x_sa_replay);
2047 if (buf + len > lim)
2051 p->sadb_x_sa_replay_len = PFKEY_UNIT64(len);
2052 p->sadb_x_sa_replay_exttype = SADB_X_EXT_SA_REPLAY;
2053 /* Convert wsize from bytes to number of packets. */
2054 p->sadb_x_sa_replay_replay = wsize << 3;
2060 * set data into sadb_address.
2061 * `buf' must has been allocated sufficiently.
2062 * prefixlen is in bits.
2065 pfkey_setsadbaddr(buf, lim, exttype, saddr, prefixlen, ul_proto)
2069 struct sockaddr *saddr;
2073 struct sadb_address *p;
2076 p = (struct sadb_address *)buf;
2077 len = sizeof(struct sadb_address) + PFKEY_ALIGN8(saddr->sa_len);
2079 if (buf + len > lim)
2083 p->sadb_address_len = PFKEY_UNIT64(len);
2084 p->sadb_address_exttype = exttype & 0xffff;
2085 p->sadb_address_proto = ul_proto & 0xff;
2086 p->sadb_address_prefixlen = prefixlen;
2087 p->sadb_address_reserved = 0;
2089 memcpy(p + 1, saddr, saddr->sa_len);
2095 * set sadb_key structure after clearing buffer with zero.
2096 * OUT: the pointer of buf + len.
2099 pfkey_setsadbkey(buf, lim, type, key, keylen)
2108 p = (struct sadb_key *)buf;
2109 len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
2111 if (buf + len > lim)
2115 p->sadb_key_len = PFKEY_UNIT64(len);
2116 p->sadb_key_exttype = type;
2117 p->sadb_key_bits = keylen << 3;
2118 p->sadb_key_reserved = 0;
2120 memcpy(p + 1, key, keylen);
2126 * set sadb_lifetime structure after clearing buffer with zero.
2127 * OUT: the pointer of buf + len.
2130 pfkey_setsadblifetime(buf, lim, type, l_alloc, l_bytes, l_addtime, l_usetime)
2134 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime;
2136 struct sadb_lifetime *p;
2139 p = (struct sadb_lifetime *)buf;
2140 len = sizeof(struct sadb_lifetime);
2142 if (buf + len > lim)
2146 p->sadb_lifetime_len = PFKEY_UNIT64(len);
2147 p->sadb_lifetime_exttype = type;
2150 case SADB_EXT_LIFETIME_SOFT:
2151 p->sadb_lifetime_allocations
2152 = (l_alloc * soft_lifetime_allocations_rate) /100;
2153 p->sadb_lifetime_bytes
2154 = (l_bytes * soft_lifetime_bytes_rate) /100;
2155 p->sadb_lifetime_addtime
2156 = (l_addtime * soft_lifetime_addtime_rate) /100;
2157 p->sadb_lifetime_usetime
2158 = (l_usetime * soft_lifetime_usetime_rate) /100;
2160 case SADB_EXT_LIFETIME_HARD:
2161 p->sadb_lifetime_allocations = l_alloc;
2162 p->sadb_lifetime_bytes = l_bytes;
2163 p->sadb_lifetime_addtime = l_addtime;
2164 p->sadb_lifetime_usetime = l_usetime;
2172 * copy secasvar data into sadb_address.
2173 * `buf' must has been allocated sufficiently.
2176 pfkey_setsadbxsa2(buf, lim, mode0, reqid)
2182 struct sadb_x_sa2 *p;
2183 u_int8_t mode = mode0 & 0xff;
2186 p = (struct sadb_x_sa2 *)buf;
2187 len = sizeof(struct sadb_x_sa2);
2189 if (buf + len > lim)
2193 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
2194 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
2195 p->sadb_x_sa2_mode = mode;
2196 p->sadb_x_sa2_reqid = reqid;