1 /* $KAME: pfkey.c,v 1.46 2003/08/26 03:37:06 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <net/pfkeyv2.h>
39 #include <netipsec/key_var.h>
40 #include <netinet/in.h>
41 #include <netipsec/ipsec.h>
48 #include "ipsec_strerror.h"
51 #define CALLOC(size, cast) (cast)calloc(1, (size))
53 static int findsupportedmap(int);
54 static int setsupportedmap(struct sadb_supported *);
55 static struct sadb_alg *findsupportedalg(u_int, u_int);
56 static int pfkey_send_x1(int, u_int, u_int, u_int, struct sockaddr *,
57 struct sockaddr *, u_int32_t, u_int32_t, u_int, caddr_t,
58 u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int32_t,
59 u_int32_t, u_int32_t, u_int32_t);
60 static int pfkey_send_x2(int, u_int, u_int, u_int,
61 struct sockaddr *, struct sockaddr *, u_int32_t);
62 static int pfkey_send_x3(int, u_int, u_int);
63 static int pfkey_send_x4(int, u_int, struct sockaddr *, u_int,
64 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
65 char *, int, u_int32_t);
66 static int pfkey_send_x5(int, u_int, u_int32_t);
68 static caddr_t pfkey_setsadbmsg(caddr_t, caddr_t, u_int, u_int,
69 u_int, u_int32_t, pid_t);
70 static caddr_t pfkey_setsadbsa(caddr_t, caddr_t, u_int32_t, u_int,
71 u_int, u_int, u_int32_t);
72 static caddr_t pfkey_setsadbaddr(caddr_t, caddr_t, u_int,
73 struct sockaddr *, u_int, u_int);
74 static caddr_t pfkey_setsadbkey(caddr_t, caddr_t, u_int, caddr_t, u_int);
75 static caddr_t pfkey_setsadblifetime(caddr_t, caddr_t, u_int, u_int32_t,
76 u_int32_t, u_int32_t, u_int32_t);
77 static caddr_t pfkey_setsadbxsa2(caddr_t, caddr_t, u_int32_t, u_int32_t);
80 * make and search supported algorithm structure.
82 static struct sadb_supported *ipsec_supported[] = { NULL, NULL, NULL, NULL };
84 static int supported_map[] = {
88 SADB_X_SATYPE_TCPSIGNATURE
92 findsupportedmap(satype)
97 for (i = 0; i < sizeof(supported_map)/sizeof(supported_map[0]); i++)
98 if (supported_map[i] == satype)
103 static struct sadb_alg *
104 findsupportedalg(satype, alg_id)
105 u_int satype, alg_id;
112 algno = findsupportedmap(satype);
114 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
117 if (ipsec_supported[algno] == NULL) {
118 __ipsec_errcode = EIPSEC_DO_GET_SUPP_LIST;
122 tlen = ipsec_supported[algno]->sadb_supported_len
123 - sizeof(struct sadb_supported);
124 p = (caddr_t)(ipsec_supported[algno] + 1);
126 if (tlen < sizeof(struct sadb_alg)) {
130 if (((struct sadb_alg *)p)->sadb_alg_id == alg_id)
131 return (struct sadb_alg *)p;
133 tlen -= sizeof(struct sadb_alg);
134 p += sizeof(struct sadb_alg);
137 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
143 struct sadb_supported *sup;
145 struct sadb_supported **ipsup;
147 switch (sup->sadb_supported_exttype) {
148 case SADB_EXT_SUPPORTED_AUTH:
149 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_AH)];
151 case SADB_EXT_SUPPORTED_ENCRYPT:
152 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_ESP)];
155 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
162 *ipsup = malloc(sup->sadb_supported_len);
164 __ipsec_set_strerror(strerror(errno));
167 memcpy(*ipsup, sup, sup->sadb_supported_len);
173 * check key length against algorithm specified.
174 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
175 * augument, and only calls to ipsec_check_keylen2();
176 * keylen is the unit of bit.
182 ipsec_check_keylen(supported, alg_id, keylen)
191 case SADB_EXT_SUPPORTED_AUTH:
192 satype = SADB_SATYPE_AH;
194 case SADB_EXT_SUPPORTED_ENCRYPT:
195 satype = SADB_SATYPE_ESP;
198 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
202 return ipsec_check_keylen2(satype, alg_id, keylen);
206 * check key length against algorithm specified.
207 * satype is one of satype defined at pfkeyv2.h.
208 * keylen is the unit of bit.
214 ipsec_check_keylen2(satype, alg_id, keylen)
219 struct sadb_alg *alg;
221 alg = findsupportedalg(satype, alg_id);
225 if (keylen < alg->sadb_alg_minbits || keylen > alg->sadb_alg_maxbits) {
226 __ipsec_errcode = EIPSEC_INVAL_KEYLEN;
230 __ipsec_errcode = EIPSEC_NO_ERROR;
235 * get max/min key length against algorithm specified.
236 * satype is one of satype defined at pfkeyv2.h.
237 * keylen is the unit of bit.
243 ipsec_get_keylen(supported, alg_id, alg0)
244 u_int supported, alg_id;
245 struct sadb_alg *alg0;
247 struct sadb_alg *alg;
252 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
257 case SADB_EXT_SUPPORTED_AUTH:
258 satype = SADB_SATYPE_AH;
260 case SADB_EXT_SUPPORTED_ENCRYPT:
261 satype = SADB_SATYPE_ESP;
264 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
268 alg = findsupportedalg(satype, alg_id);
272 memcpy(alg0, alg, sizeof(*alg0));
274 __ipsec_errcode = EIPSEC_NO_ERROR;
279 * set the rate for SOFT lifetime against HARD one.
280 * If rate is more than 100 or equal to zero, then set to 100.
282 static u_int soft_lifetime_allocations_rate = PFKEY_SOFT_LIFETIME_RATE;
283 static u_int soft_lifetime_bytes_rate = PFKEY_SOFT_LIFETIME_RATE;
284 static u_int soft_lifetime_addtime_rate = PFKEY_SOFT_LIFETIME_RATE;
285 static u_int soft_lifetime_usetime_rate = PFKEY_SOFT_LIFETIME_RATE;
288 pfkey_set_softrate(type, rate)
291 __ipsec_errcode = EIPSEC_NO_ERROR;
293 if (rate > 100 || rate == 0)
297 case SADB_X_LIFETIME_ALLOCATIONS:
298 soft_lifetime_allocations_rate = rate;
300 case SADB_X_LIFETIME_BYTES:
301 soft_lifetime_bytes_rate = rate;
303 case SADB_X_LIFETIME_ADDTIME:
304 soft_lifetime_addtime_rate = rate;
306 case SADB_X_LIFETIME_USETIME:
307 soft_lifetime_usetime_rate = rate;
311 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
316 * get current rate for SOFT lifetime against HARD one.
317 * ATTENTION: ~0 is returned if invalid type was passed.
320 pfkey_get_softrate(type)
324 case SADB_X_LIFETIME_ALLOCATIONS:
325 return soft_lifetime_allocations_rate;
326 case SADB_X_LIFETIME_BYTES:
327 return soft_lifetime_bytes_rate;
328 case SADB_X_LIFETIME_ADDTIME:
329 return soft_lifetime_addtime_rate;
330 case SADB_X_LIFETIME_USETIME:
331 return soft_lifetime_usetime_rate;
338 * sending SADB_GETSPI message to the kernel.
340 * positive: success and return length sent.
341 * -1 : error occured, and set errno.
344 pfkey_send_getspi(so, satype, mode, src, dst, min, max, reqid, seq)
347 struct sockaddr *src, *dst;
348 u_int32_t min, max, reqid, seq;
350 struct sadb_msg *newmsg;
353 int need_spirange = 0;
358 if (src == NULL || dst == NULL) {
359 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
362 if (src->sa_family != dst->sa_family) {
363 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
366 if (min > max || (min > 0 && min <= 255)) {
367 __ipsec_errcode = EIPSEC_INVAL_SPI;
370 switch (src->sa_family) {
372 plen = sizeof(struct in_addr) << 3;
375 plen = sizeof(struct in6_addr) << 3;
378 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
382 /* create new sadb_msg to send. */
383 len = sizeof(struct sadb_msg)
384 + sizeof(struct sadb_x_sa2)
385 + sizeof(struct sadb_address)
386 + PFKEY_ALIGN8(src->sa_len)
387 + sizeof(struct sadb_address)
388 + PFKEY_ALIGN8(dst->sa_len);
390 if (min > 255 && max < ~0) {
392 len += sizeof(struct sadb_spirange);
395 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
396 __ipsec_set_strerror(strerror(errno));
399 ep = ((caddr_t)newmsg) + len;
401 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_GETSPI,
402 len, satype, seq, getpid());
408 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
414 /* set sadb_address for source */
415 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
422 /* set sadb_address for destination */
423 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
430 /* proccessing spi range */
432 struct sadb_spirange spirange;
434 if (p + sizeof(spirange) > ep) {
439 memset(&spirange, 0, sizeof(spirange));
440 spirange.sadb_spirange_len = PFKEY_UNIT64(sizeof(spirange));
441 spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
442 spirange.sadb_spirange_min = min;
443 spirange.sadb_spirange_max = max;
445 memcpy(p, &spirange, sizeof(spirange));
447 p += sizeof(spirange);
455 len = pfkey_send(so, newmsg, len);
461 __ipsec_errcode = EIPSEC_NO_ERROR;
466 * sending SADB_UPDATE message to the kernel.
467 * The length of key material is a_keylen + e_keylen.
469 * positive: success and return length sent.
470 * -1 : error occured, and set errno.
473 pfkey_send_update(so, satype, mode, src, dst, spi, reqid, wsize,
474 keymat, e_type, e_keylen, a_type, a_keylen, flags,
475 l_alloc, l_bytes, l_addtime, l_usetime, seq)
477 u_int satype, mode, wsize;
478 struct sockaddr *src, *dst;
479 u_int32_t spi, reqid;
481 u_int e_type, e_keylen, a_type, a_keylen, flags;
483 u_int64_t l_bytes, l_addtime, l_usetime;
487 if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
489 keymat, e_type, e_keylen, a_type, a_keylen, flags,
490 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0)
497 * sending SADB_ADD message to the kernel.
498 * The length of key material is a_keylen + e_keylen.
500 * positive: success and return length sent.
501 * -1 : error occured, and set errno.
504 pfkey_send_add(so, satype, mode, src, dst, spi, reqid, wsize,
505 keymat, e_type, e_keylen, a_type, a_keylen, flags,
506 l_alloc, l_bytes, l_addtime, l_usetime, seq)
508 u_int satype, mode, wsize;
509 struct sockaddr *src, *dst;
510 u_int32_t spi, reqid;
512 u_int e_type, e_keylen, a_type, a_keylen, flags;
514 u_int64_t l_bytes, l_addtime, l_usetime;
518 if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
520 keymat, e_type, e_keylen, a_type, a_keylen, flags,
521 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0)
528 * sending SADB_DELETE message to the kernel.
530 * positive: success and return length sent.
531 * -1 : error occured, and set errno.
534 pfkey_send_delete(so, satype, mode, src, dst, spi)
537 struct sockaddr *src, *dst;
541 if ((len = pfkey_send_x2(so, SADB_DELETE, satype, mode, src, dst, spi)) < 0)
548 * sending SADB_DELETE without spi to the kernel. This is
549 * the "delete all" request (an extension also present in
553 * positive: success and return length sent
554 * -1 : error occured, and set errno
557 pfkey_send_delete_all(so, satype, mode, src, dst)
560 struct sockaddr *src, *dst;
562 struct sadb_msg *newmsg;
569 if (src == NULL || dst == NULL) {
570 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
573 if (src->sa_family != dst->sa_family) {
574 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
577 switch (src->sa_family) {
579 plen = sizeof(struct in_addr) << 3;
582 plen = sizeof(struct in6_addr) << 3;
585 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
589 /* create new sadb_msg to reply. */
590 len = sizeof(struct sadb_msg)
591 + sizeof(struct sadb_address)
592 + PFKEY_ALIGN8(src->sa_len)
593 + sizeof(struct sadb_address)
594 + PFKEY_ALIGN8(dst->sa_len);
596 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
597 __ipsec_set_strerror(strerror(errno));
600 ep = ((caddr_t)newmsg) + len;
602 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_DELETE, len, satype, 0,
608 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
614 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
622 len = pfkey_send(so, newmsg, len);
628 __ipsec_errcode = EIPSEC_NO_ERROR;
633 * sending SADB_GET message to the kernel.
635 * positive: success and return length sent.
636 * -1 : error occured, and set errno.
639 pfkey_send_get(so, satype, mode, src, dst, spi)
642 struct sockaddr *src, *dst;
646 if ((len = pfkey_send_x2(so, SADB_GET, satype, mode, src, dst, spi)) < 0)
653 * sending SADB_REGISTER message to the kernel.
655 * positive: success and return length sent.
656 * -1 : error occured, and set errno.
659 pfkey_send_register(so, satype)
665 if (satype == PF_UNSPEC) {
667 algno < sizeof(supported_map)/sizeof(supported_map[0]);
669 if (ipsec_supported[algno]) {
670 free(ipsec_supported[algno]);
671 ipsec_supported[algno] = NULL;
675 algno = findsupportedmap(satype);
677 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
681 if (ipsec_supported[algno]) {
682 free(ipsec_supported[algno]);
683 ipsec_supported[algno] = NULL;
687 if ((len = pfkey_send_x3(so, SADB_REGISTER, satype)) < 0)
694 * receiving SADB_REGISTER message from the kernel, and copy buffer for
695 * sadb_supported returned into ipsec_supported.
697 * 0: success and return length sent.
698 * -1: error occured, and set errno.
701 pfkey_recv_register(so)
704 pid_t pid = getpid();
705 struct sadb_msg *newmsg;
708 /* receive message */
710 if ((newmsg = pfkey_recv(so)) == NULL)
712 if (newmsg->sadb_msg_type == SADB_REGISTER &&
713 newmsg->sadb_msg_pid == pid)
719 newmsg->sadb_msg_len = PFKEY_UNUNIT64(newmsg->sadb_msg_len);
721 error = pfkey_set_supported(newmsg, newmsg->sadb_msg_len);
725 __ipsec_errcode = EIPSEC_NO_ERROR;
731 * receiving SADB_REGISTER message from the kernel, and copy buffer for
732 * sadb_supported returned into ipsec_supported.
733 * NOTE: sadb_msg_len must be host order.
735 * tlen: msg length, it's to makeing sure.
737 * 0: success and return length sent.
738 * -1: error occured, and set errno.
741 pfkey_set_supported(msg, tlen)
742 struct sadb_msg *msg;
745 struct sadb_supported *sup;
750 if (msg->sadb_msg_len != tlen) {
751 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
758 p += sizeof(struct sadb_msg);
761 sup = (struct sadb_supported *)p;
762 if (ep < p + sizeof(*sup) ||
763 PFKEY_EXTLEN(sup) < sizeof(*sup) ||
764 ep < p + sup->sadb_supported_len) {
769 switch (sup->sadb_supported_exttype) {
770 case SADB_EXT_SUPPORTED_AUTH:
771 case SADB_EXT_SUPPORTED_ENCRYPT:
774 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
779 sup->sadb_supported_len = PFKEY_EXTLEN(sup);
781 /* set supported map */
782 if (setsupportedmap(sup) != 0)
785 p += sup->sadb_supported_len;
789 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
793 __ipsec_errcode = EIPSEC_NO_ERROR;
799 * sending SADB_FLUSH message to the kernel.
801 * positive: success and return length sent.
802 * -1 : error occured, and set errno.
805 pfkey_send_flush(so, satype)
811 if ((len = pfkey_send_x3(so, SADB_FLUSH, satype)) < 0)
818 * sending SADB_DUMP message to the kernel.
820 * positive: success and return length sent.
821 * -1 : error occured, and set errno.
824 pfkey_send_dump(so, satype)
830 if ((len = pfkey_send_x3(so, SADB_DUMP, satype)) < 0)
837 * sending SADB_X_PROMISC message to the kernel.
838 * NOTE that this function handles promisc mode toggle only.
840 * flag: set promisc off if zero, set promisc on if non-zero.
842 * positive: success and return length sent.
843 * -1 : error occured, and set errno.
844 * 0 : error occured, and set errno.
845 * others: a pointer to new allocated buffer in which supported
849 pfkey_send_promisc_toggle(so, flag)
855 if ((len = pfkey_send_x3(so, SADB_X_PROMISC, (flag ? 1 : 0))) < 0)
862 * sending SADB_X_SPDADD message to the kernel.
864 * positive: success and return length sent.
865 * -1 : error occured, and set errno.
868 pfkey_send_spdadd(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
870 struct sockaddr *src, *dst;
871 u_int prefs, prefd, proto;
878 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
879 src, prefs, dst, prefd, proto,
881 policy, policylen, seq)) < 0)
888 * sending SADB_X_SPDADD message to the kernel.
890 * positive: success and return length sent.
891 * -1 : error occured, and set errno.
894 pfkey_send_spdadd2(so, src, prefs, dst, prefd, proto, ltime, vtime,
895 policy, policylen, seq)
897 struct sockaddr *src, *dst;
898 u_int prefs, prefd, proto;
899 u_int64_t ltime, vtime;
906 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
907 src, prefs, dst, prefd, proto,
909 policy, policylen, seq)) < 0)
916 * sending SADB_X_SPDUPDATE message to the kernel.
918 * positive: success and return length sent.
919 * -1 : error occured, and set errno.
922 pfkey_send_spdupdate(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
924 struct sockaddr *src, *dst;
925 u_int prefs, prefd, proto;
932 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
933 src, prefs, dst, prefd, proto,
935 policy, policylen, seq)) < 0)
942 * sending SADB_X_SPDUPDATE message to the kernel.
944 * positive: success and return length sent.
945 * -1 : error occured, and set errno.
948 pfkey_send_spdupdate2(so, src, prefs, dst, prefd, proto, ltime, vtime,
949 policy, policylen, seq)
951 struct sockaddr *src, *dst;
952 u_int prefs, prefd, proto;
953 u_int64_t ltime, vtime;
960 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
961 src, prefs, dst, prefd, proto,
963 policy, policylen, seq)) < 0)
970 * sending SADB_X_SPDDELETE message to the kernel.
972 * positive: success and return length sent.
973 * -1 : error occured, and set errno.
976 pfkey_send_spddelete(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
978 struct sockaddr *src, *dst;
979 u_int prefs, prefd, proto;
986 if (policylen != sizeof(struct sadb_x_policy)) {
987 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
991 if ((len = pfkey_send_x4(so, SADB_X_SPDDELETE,
992 src, prefs, dst, prefd, proto,
994 policy, policylen, seq)) < 0)
1001 * sending SADB_X_SPDDELETE message to the kernel.
1003 * positive: success and return length sent.
1004 * -1 : error occured, and set errno.
1007 pfkey_send_spddelete2(so, spid)
1013 if ((len = pfkey_send_x5(so, SADB_X_SPDDELETE2, spid)) < 0)
1020 * sending SADB_X_SPDGET message to the kernel.
1022 * positive: success and return length sent.
1023 * -1 : error occured, and set errno.
1026 pfkey_send_spdget(so, spid)
1032 if ((len = pfkey_send_x5(so, SADB_X_SPDGET, spid)) < 0)
1039 * sending SADB_X_SPDSETIDX message to the kernel.
1041 * positive: success and return length sent.
1042 * -1 : error occured, and set errno.
1045 pfkey_send_spdsetidx(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1047 struct sockaddr *src, *dst;
1048 u_int prefs, prefd, proto;
1055 if (policylen != sizeof(struct sadb_x_policy)) {
1056 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1060 if ((len = pfkey_send_x4(so, SADB_X_SPDSETIDX,
1061 src, prefs, dst, prefd, proto,
1063 policy, policylen, seq)) < 0)
1070 * sending SADB_SPDFLUSH message to the kernel.
1072 * positive: success and return length sent.
1073 * -1 : error occured, and set errno.
1076 pfkey_send_spdflush(so)
1081 if ((len = pfkey_send_x3(so, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC)) < 0)
1088 * sending SADB_SPDDUMP message to the kernel.
1090 * positive: success and return length sent.
1091 * -1 : error occured, and set errno.
1094 pfkey_send_spddump(so)
1099 if ((len = pfkey_send_x3(so, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC)) < 0)
1105 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1107 pfkey_send_x1(so, type, satype, mode, src, dst, spi, reqid, wsize,
1108 keymat, e_type, e_keylen, a_type, a_keylen, flags,
1109 l_alloc, l_bytes, l_addtime, l_usetime, seq)
1111 u_int type, satype, mode;
1112 struct sockaddr *src, *dst;
1113 u_int32_t spi, reqid;
1116 u_int e_type, e_keylen, a_type, a_keylen, flags;
1117 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime, seq;
1119 struct sadb_msg *newmsg;
1125 /* validity check */
1126 if (src == NULL || dst == NULL) {
1127 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1130 if (src->sa_family != dst->sa_family) {
1131 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1134 switch (src->sa_family) {
1136 plen = sizeof(struct in_addr) << 3;
1139 plen = sizeof(struct in6_addr) << 3;
1142 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1147 case SADB_SATYPE_ESP:
1148 if (e_type == SADB_EALG_NONE) {
1149 __ipsec_errcode = EIPSEC_NO_ALGS;
1153 case SADB_SATYPE_AH:
1154 if (e_type != SADB_EALG_NONE) {
1155 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1158 if (a_type == SADB_AALG_NONE) {
1159 __ipsec_errcode = EIPSEC_NO_ALGS;
1163 case SADB_X_SATYPE_IPCOMP:
1164 if (e_type == SADB_X_CALG_NONE) {
1165 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1168 if (a_type != SADB_AALG_NONE) {
1169 __ipsec_errcode = EIPSEC_NO_ALGS;
1173 case SADB_X_SATYPE_TCPSIGNATURE:
1174 if (e_type != SADB_EALG_NONE) {
1175 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1178 if (a_type != SADB_X_AALG_TCP_MD5) {
1179 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1184 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1188 /* create new sadb_msg to reply. */
1189 len = sizeof(struct sadb_msg)
1190 + sizeof(struct sadb_sa)
1191 + sizeof(struct sadb_x_sa2)
1192 + sizeof(struct sadb_address)
1193 + PFKEY_ALIGN8(src->sa_len)
1194 + sizeof(struct sadb_address)
1195 + PFKEY_ALIGN8(dst->sa_len)
1196 + sizeof(struct sadb_lifetime)
1197 + sizeof(struct sadb_lifetime);
1199 if (e_type != SADB_EALG_NONE)
1200 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(e_keylen));
1201 if (a_type != SADB_AALG_NONE)
1202 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(a_keylen));
1204 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1205 __ipsec_set_strerror(strerror(errno));
1208 ep = ((caddr_t)newmsg) + len;
1210 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1211 satype, seq, getpid());
1216 p = pfkey_setsadbsa(p, ep, spi, wsize, a_type, e_type, flags);
1221 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
1226 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1232 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1239 if (e_type != SADB_EALG_NONE) {
1240 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_ENCRYPT,
1247 if (a_type != SADB_AALG_NONE) {
1248 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_AUTH,
1249 keymat + e_keylen, a_keylen);
1256 /* set sadb_lifetime for destination */
1257 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1258 l_alloc, l_bytes, l_addtime, l_usetime);
1263 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_SOFT,
1264 l_alloc, l_bytes, l_addtime, l_usetime);
1265 if (!p || p != ep) {
1271 len = pfkey_send(so, newmsg, len);
1277 __ipsec_errcode = EIPSEC_NO_ERROR;
1281 /* sending SADB_DELETE or SADB_GET message to the kernel */
1283 pfkey_send_x2(so, type, satype, mode, src, dst, spi)
1285 u_int type, satype, mode;
1286 struct sockaddr *src, *dst;
1289 struct sadb_msg *newmsg;
1295 /* validity check */
1296 if (src == NULL || dst == NULL) {
1297 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1300 if (src->sa_family != dst->sa_family) {
1301 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1304 switch (src->sa_family) {
1306 plen = sizeof(struct in_addr) << 3;
1309 plen = sizeof(struct in6_addr) << 3;
1312 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1316 /* create new sadb_msg to reply. */
1317 len = sizeof(struct sadb_msg)
1318 + sizeof(struct sadb_sa)
1319 + sizeof(struct sadb_address)
1320 + PFKEY_ALIGN8(src->sa_len)
1321 + sizeof(struct sadb_address)
1322 + PFKEY_ALIGN8(dst->sa_len);
1324 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1325 __ipsec_set_strerror(strerror(errno));
1328 ep = ((caddr_t)newmsg) + len;
1330 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1336 p = pfkey_setsadbsa(p, ep, spi, 0, 0, 0, 0);
1341 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1347 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1349 if (!p || p != ep) {
1355 len = pfkey_send(so, newmsg, len);
1361 __ipsec_errcode = EIPSEC_NO_ERROR;
1366 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1370 pfkey_send_x3(so, type, satype)
1374 struct sadb_msg *newmsg;
1379 /* validity check */
1381 case SADB_X_PROMISC:
1382 if (satype != 0 && satype != 1) {
1383 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1389 case SADB_SATYPE_UNSPEC:
1390 case SADB_SATYPE_AH:
1391 case SADB_SATYPE_ESP:
1392 case SADB_X_SATYPE_IPCOMP:
1393 case SADB_X_SATYPE_TCPSIGNATURE:
1396 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1401 /* create new sadb_msg to send. */
1402 len = sizeof(struct sadb_msg);
1404 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1405 __ipsec_set_strerror(strerror(errno));
1408 ep = ((caddr_t)newmsg) + len;
1410 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1412 if (!p || p != ep) {
1418 len = pfkey_send(so, newmsg, len);
1424 __ipsec_errcode = EIPSEC_NO_ERROR;
1428 /* sending SADB_X_SPDADD message to the kernel */
1430 pfkey_send_x4(so, type, src, prefs, dst, prefd, proto,
1431 ltime, vtime, policy, policylen, seq)
1433 struct sockaddr *src, *dst;
1434 u_int type, prefs, prefd, proto;
1435 u_int64_t ltime, vtime;
1440 struct sadb_msg *newmsg;
1446 /* validity check */
1447 if (src == NULL || dst == NULL) {
1448 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1451 if (src->sa_family != dst->sa_family) {
1452 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1456 switch (src->sa_family) {
1458 plen = sizeof(struct in_addr) << 3;
1461 plen = sizeof(struct in6_addr) << 3;
1464 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1467 if (prefs > plen || prefd > plen) {
1468 __ipsec_errcode = EIPSEC_INVAL_PREFIXLEN;
1472 /* create new sadb_msg to reply. */
1473 len = sizeof(struct sadb_msg)
1474 + sizeof(struct sadb_address)
1475 + PFKEY_ALIGN8(src->sa_len)
1476 + sizeof(struct sadb_address)
1477 + PFKEY_ALIGN8(src->sa_len)
1478 + sizeof(struct sadb_lifetime)
1481 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1482 __ipsec_set_strerror(strerror(errno));
1485 ep = ((caddr_t)newmsg) + len;
1487 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1488 SADB_SATYPE_UNSPEC, seq, getpid());
1493 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, prefs, proto);
1498 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, prefd, proto);
1503 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1504 0, 0, ltime, vtime);
1505 if (!p || p + policylen != ep) {
1509 memcpy(p, policy, policylen);
1512 len = pfkey_send(so, newmsg, len);
1518 __ipsec_errcode = EIPSEC_NO_ERROR;
1522 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1524 pfkey_send_x5(so, type, spid)
1529 struct sadb_msg *newmsg;
1530 struct sadb_x_policy xpl;
1535 /* create new sadb_msg to reply. */
1536 len = sizeof(struct sadb_msg)
1539 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1540 __ipsec_set_strerror(strerror(errno));
1543 ep = ((caddr_t)newmsg) + len;
1545 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1546 SADB_SATYPE_UNSPEC, 0, getpid());
1552 if (p + sizeof(xpl) != ep) {
1556 memset(&xpl, 0, sizeof(xpl));
1557 xpl.sadb_x_policy_len = PFKEY_UNIT64(sizeof(xpl));
1558 xpl.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1559 xpl.sadb_x_policy_id = spid;
1560 memcpy(p, &xpl, sizeof(xpl));
1563 len = pfkey_send(so, newmsg, len);
1569 __ipsec_errcode = EIPSEC_NO_ERROR;
1577 * others : success and return value of socket.
1583 const int bufsiz = 128 * 1024; /*is 128K enough?*/
1585 if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
1586 __ipsec_set_strerror(strerror(errno));
1591 * This is a temporary workaround for KAME PR 154.
1592 * Don't really care even if it fails.
1594 (void)setsockopt(so, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz));
1595 (void)setsockopt(so, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz));
1597 __ipsec_errcode = EIPSEC_NO_ERROR;
1613 __ipsec_errcode = EIPSEC_NO_ERROR;
1618 * receive sadb_msg data, and return pointer to new buffer allocated.
1619 * Must free this buffer later.
1621 * NULL : error occured.
1622 * others : a pointer to sadb_msg structure.
1624 * XXX should be rewritten to pass length explicitly
1630 struct sadb_msg buf, *newmsg;
1633 while ((len = recv(so, (caddr_t)&buf, sizeof(buf), MSG_PEEK)) < 0) {
1636 __ipsec_set_strerror(strerror(errno));
1640 if (len < sizeof(buf)) {
1641 recv(so, (caddr_t)&buf, sizeof(buf), 0);
1642 __ipsec_errcode = EIPSEC_MAX;
1646 /* read real message */
1647 reallen = PFKEY_UNUNIT64(buf.sadb_msg_len);
1648 if ((newmsg = CALLOC(reallen, struct sadb_msg *)) == 0) {
1649 __ipsec_set_strerror(strerror(errno));
1653 while ((len = recv(so, (caddr_t)newmsg, reallen, 0)) < 0) {
1656 __ipsec_set_strerror(strerror(errno));
1661 if (len != reallen) {
1662 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1667 /* don't trust what the kernel says, validate! */
1668 if (PFKEY_UNUNIT64(newmsg->sadb_msg_len) != len) {
1669 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1674 __ipsec_errcode = EIPSEC_NO_ERROR;
1679 * send message to a socket.
1681 * others: success and return length sent.
1685 pfkey_send(so, msg, len)
1687 struct sadb_msg *msg;
1690 if ((len = send(so, (caddr_t)msg, len, 0)) < 0) {
1691 __ipsec_set_strerror(strerror(errno));
1695 __ipsec_errcode = EIPSEC_NO_ERROR;
1701 * NOTE: These functions are derived from netkey/key.c in KAME.
1704 * set the pointer to each header in this message buffer.
1705 * IN: msg: pointer to message buffer.
1706 * mhp: pointer to the buffer initialized like below:
1707 * caddr_t mhp[SADB_EXT_MAX + 1];
1711 * XXX should be rewritten to obtain length explicitly
1714 pfkey_align(msg, mhp)
1715 struct sadb_msg *msg;
1718 struct sadb_ext *ext;
1721 caddr_t ep; /* XXX should be passed from upper layer */
1723 /* validity check */
1724 if (msg == NULL || mhp == NULL) {
1725 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1730 for (i = 0; i < SADB_EXT_MAX + 1; i++)
1733 mhp[0] = (caddr_t)msg;
1737 ep = p + PFKEY_UNUNIT64(msg->sadb_msg_len);
1739 /* skip base header */
1740 p += sizeof(struct sadb_msg);
1743 ext = (struct sadb_ext *)p;
1744 if (ep < p + sizeof(*ext) || PFKEY_EXTLEN(ext) < sizeof(*ext) ||
1745 ep < p + PFKEY_EXTLEN(ext)) {
1746 /* invalid format */
1750 /* duplicate check */
1751 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1752 if (mhp[ext->sadb_ext_type] != NULL) {
1753 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1758 switch (ext->sadb_ext_type) {
1760 case SADB_EXT_LIFETIME_CURRENT:
1761 case SADB_EXT_LIFETIME_HARD:
1762 case SADB_EXT_LIFETIME_SOFT:
1763 case SADB_EXT_ADDRESS_SRC:
1764 case SADB_EXT_ADDRESS_DST:
1765 case SADB_EXT_ADDRESS_PROXY:
1766 case SADB_EXT_KEY_AUTH:
1767 /* XXX should to be check weak keys. */
1768 case SADB_EXT_KEY_ENCRYPT:
1769 /* XXX should to be check weak keys. */
1770 case SADB_EXT_IDENTITY_SRC:
1771 case SADB_EXT_IDENTITY_DST:
1772 case SADB_EXT_SENSITIVITY:
1773 case SADB_EXT_PROPOSAL:
1774 case SADB_EXT_SUPPORTED_AUTH:
1775 case SADB_EXT_SUPPORTED_ENCRYPT:
1776 case SADB_EXT_SPIRANGE:
1777 case SADB_X_EXT_POLICY:
1778 case SADB_X_EXT_SA2:
1779 mhp[ext->sadb_ext_type] = (caddr_t)ext;
1782 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1786 p += PFKEY_EXTLEN(ext);
1790 __ipsec_errcode = EIPSEC_INVAL_SADBMSG;
1794 __ipsec_errcode = EIPSEC_NO_ERROR;
1799 * check basic usage for sadb_msg,
1800 * NOTE: This routine is derived from netkey/key.c in KAME.
1801 * IN: msg: pointer to message buffer.
1802 * mhp: pointer to the buffer initialized like below:
1804 * caddr_t mhp[SADB_EXT_MAX + 1];
1813 struct sadb_msg *msg;
1815 /* validity check */
1816 if (mhp == NULL || mhp[0] == NULL) {
1817 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1821 msg = (struct sadb_msg *)mhp[0];
1824 if (msg->sadb_msg_version != PF_KEY_V2) {
1825 __ipsec_errcode = EIPSEC_INVAL_VERSION;
1830 if (msg->sadb_msg_type > SADB_MAX) {
1831 __ipsec_errcode = EIPSEC_INVAL_MSGTYPE;
1836 switch (msg->sadb_msg_satype) {
1837 case SADB_SATYPE_UNSPEC:
1838 switch (msg->sadb_msg_type) {
1846 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1850 case SADB_SATYPE_ESP:
1851 case SADB_SATYPE_AH:
1852 case SADB_X_SATYPE_IPCOMP:
1853 case SADB_X_SATYPE_TCPSIGNATURE:
1854 switch (msg->sadb_msg_type) {
1856 case SADB_X_SPDDELETE:
1858 case SADB_X_SPDDUMP:
1859 case SADB_X_SPDFLUSH:
1860 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1864 case SADB_SATYPE_RSVP:
1865 case SADB_SATYPE_OSPFV2:
1866 case SADB_SATYPE_RIPV2:
1867 case SADB_SATYPE_MIP:
1868 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
1870 case 1: /* XXX: What does it do ? */
1871 if (msg->sadb_msg_type == SADB_X_PROMISC)
1875 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1879 /* check field of upper layer protocol and address family */
1880 if (mhp[SADB_EXT_ADDRESS_SRC] != NULL
1881 && mhp[SADB_EXT_ADDRESS_DST] != NULL) {
1882 struct sadb_address *src0, *dst0;
1884 src0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_SRC]);
1885 dst0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_DST]);
1887 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
1888 __ipsec_errcode = EIPSEC_PROTO_MISMATCH;
1892 if (PFKEY_ADDR_SADDR(src0)->sa_family
1893 != PFKEY_ADDR_SADDR(dst0)->sa_family) {
1894 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1898 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
1903 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1908 * prefixlen == 0 is valid because there must be the case
1909 * all addresses are matched.
1913 __ipsec_errcode = EIPSEC_NO_ERROR;
1918 * set data into sadb_msg.
1919 * `buf' must has been allocated sufficiently.
1922 pfkey_setsadbmsg(buf, lim, type, tlen, satype, seq, pid)
1933 p = (struct sadb_msg *)buf;
1934 len = sizeof(struct sadb_msg);
1936 if (buf + len > lim)
1940 p->sadb_msg_version = PF_KEY_V2;
1941 p->sadb_msg_type = type;
1942 p->sadb_msg_errno = 0;
1943 p->sadb_msg_satype = satype;
1944 p->sadb_msg_len = PFKEY_UNIT64(tlen);
1945 p->sadb_msg_reserved = 0;
1946 p->sadb_msg_seq = seq;
1947 p->sadb_msg_pid = (u_int32_t)pid;
1953 * copy secasvar data into sadb_address.
1954 * `buf' must has been allocated sufficiently.
1957 pfkey_setsadbsa(buf, lim, spi, wsize, auth, enc, flags)
1960 u_int32_t spi, flags;
1961 u_int wsize, auth, enc;
1966 p = (struct sadb_sa *)buf;
1967 len = sizeof(struct sadb_sa);
1969 if (buf + len > lim)
1973 p->sadb_sa_len = PFKEY_UNIT64(len);
1974 p->sadb_sa_exttype = SADB_EXT_SA;
1975 p->sadb_sa_spi = spi;
1976 p->sadb_sa_replay = wsize;
1977 p->sadb_sa_state = SADB_SASTATE_LARVAL;
1978 p->sadb_sa_auth = auth;
1979 p->sadb_sa_encrypt = enc;
1980 p->sadb_sa_flags = flags;
1986 * set data into sadb_address.
1987 * `buf' must has been allocated sufficiently.
1988 * prefixlen is in bits.
1991 pfkey_setsadbaddr(buf, lim, exttype, saddr, prefixlen, ul_proto)
1995 struct sockaddr *saddr;
1999 struct sadb_address *p;
2002 p = (struct sadb_address *)buf;
2003 len = sizeof(struct sadb_address) + PFKEY_ALIGN8(saddr->sa_len);
2005 if (buf + len > lim)
2009 p->sadb_address_len = PFKEY_UNIT64(len);
2010 p->sadb_address_exttype = exttype & 0xffff;
2011 p->sadb_address_proto = ul_proto & 0xff;
2012 p->sadb_address_prefixlen = prefixlen;
2013 p->sadb_address_reserved = 0;
2015 memcpy(p + 1, saddr, saddr->sa_len);
2021 * set sadb_key structure after clearing buffer with zero.
2022 * OUT: the pointer of buf + len.
2025 pfkey_setsadbkey(buf, lim, type, key, keylen)
2034 p = (struct sadb_key *)buf;
2035 len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
2037 if (buf + len > lim)
2041 p->sadb_key_len = PFKEY_UNIT64(len);
2042 p->sadb_key_exttype = type;
2043 p->sadb_key_bits = keylen << 3;
2044 p->sadb_key_reserved = 0;
2046 memcpy(p + 1, key, keylen);
2052 * set sadb_lifetime structure after clearing buffer with zero.
2053 * OUT: the pointer of buf + len.
2056 pfkey_setsadblifetime(buf, lim, type, l_alloc, l_bytes, l_addtime, l_usetime)
2060 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime;
2062 struct sadb_lifetime *p;
2065 p = (struct sadb_lifetime *)buf;
2066 len = sizeof(struct sadb_lifetime);
2068 if (buf + len > lim)
2072 p->sadb_lifetime_len = PFKEY_UNIT64(len);
2073 p->sadb_lifetime_exttype = type;
2076 case SADB_EXT_LIFETIME_SOFT:
2077 p->sadb_lifetime_allocations
2078 = (l_alloc * soft_lifetime_allocations_rate) /100;
2079 p->sadb_lifetime_bytes
2080 = (l_bytes * soft_lifetime_bytes_rate) /100;
2081 p->sadb_lifetime_addtime
2082 = (l_addtime * soft_lifetime_addtime_rate) /100;
2083 p->sadb_lifetime_usetime
2084 = (l_usetime * soft_lifetime_usetime_rate) /100;
2086 case SADB_EXT_LIFETIME_HARD:
2087 p->sadb_lifetime_allocations = l_alloc;
2088 p->sadb_lifetime_bytes = l_bytes;
2089 p->sadb_lifetime_addtime = l_addtime;
2090 p->sadb_lifetime_usetime = l_usetime;
2098 * copy secasvar data into sadb_address.
2099 * `buf' must has been allocated sufficiently.
2102 pfkey_setsadbxsa2(buf, lim, mode0, reqid)
2108 struct sadb_x_sa2 *p;
2109 u_int8_t mode = mode0 & 0xff;
2112 p = (struct sadb_x_sa2 *)buf;
2113 len = sizeof(struct sadb_x_sa2);
2115 if (buf + len > lim)
2119 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
2120 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
2121 p->sadb_x_sa2_mode = mode;
2122 p->sadb_x_sa2_reqid = reqid;