1 /* $KAME: pfkey.c,v 1.47 2003/10/02 19:52:12 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
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <net/pfkeyv2.h>
40 #include <netinet/in.h>
41 #ifdef HAVE_NETINET6_IPSEC
42 # include <netinet6/ipsec.h>
44 # include <netinet/ipsec.h>
53 #include "ipsec_strerror.h"
56 #define CALLOC(size, cast) (cast)calloc(1, (size))
58 static int findsupportedmap __P((int));
59 static int setsupportedmap __P((struct sadb_supported *));
60 static struct sadb_alg *findsupportedalg __P((u_int, u_int));
61 static int pfkey_send_x1 __P((int, u_int, u_int, u_int, struct sockaddr *,
62 struct sockaddr *, u_int32_t, u_int32_t, u_int, caddr_t,
63 u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int32_t,
64 u_int32_t, u_int32_t, u_int32_t,
65 u_int8_t, u_int16_t, u_int16_t, struct sockaddr *));
66 static int pfkey_send_x2 __P((int, u_int, u_int, u_int,
67 struct sockaddr *, struct sockaddr *, u_int32_t));
68 static int pfkey_send_x3 __P((int, u_int, u_int));
69 static int pfkey_send_x4 __P((int, u_int, struct sockaddr *, u_int,
70 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
71 char *, int, u_int32_t));
72 static int pfkey_send_x5 __P((int, u_int, u_int32_t));
74 static caddr_t pfkey_setsadbmsg __P((caddr_t, caddr_t, u_int, u_int,
75 u_int, u_int32_t, pid_t));
76 static caddr_t pfkey_setsadbsa __P((caddr_t, caddr_t, u_int32_t, u_int,
77 u_int, u_int, u_int32_t));
78 static caddr_t pfkey_setsadbaddr __P((caddr_t, caddr_t, u_int,
79 struct sockaddr *, u_int, u_int));
80 static caddr_t pfkey_setsadbkey __P((caddr_t, caddr_t, u_int, caddr_t, u_int));
81 static caddr_t pfkey_setsadblifetime __P((caddr_t, caddr_t, u_int, u_int32_t,
82 u_int32_t, u_int32_t, u_int32_t));
83 static caddr_t pfkey_setsadbxsa2 __P((caddr_t, caddr_t, u_int32_t, u_int32_t));
85 #ifdef SADB_X_EXT_NAT_T_TYPE
86 static caddr_t pfkey_set_natt_type __P((caddr_t, caddr_t, u_int, u_int8_t));
87 static caddr_t pfkey_set_natt_port __P((caddr_t, caddr_t, u_int, u_int16_t));
91 * make and search supported algorithm structure.
93 static struct sadb_supported *ipsec_supported[] = { NULL, NULL, NULL, };
95 static int supported_map[] = {
102 findsupportedmap(satype)
107 for (i = 0; i < sizeof(supported_map)/sizeof(supported_map[0]); i++)
108 if (supported_map[i] == satype)
113 static struct sadb_alg *
114 findsupportedalg(satype, alg_id)
115 u_int satype, alg_id;
122 algno = findsupportedmap(satype);
124 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
127 if (ipsec_supported[algno] == NULL) {
128 __ipsec_errcode = EIPSEC_DO_GET_SUPP_LIST;
132 tlen = ipsec_supported[algno]->sadb_supported_len
133 - sizeof(struct sadb_supported);
134 p = (caddr_t)(ipsec_supported[algno] + 1);
136 if (tlen < sizeof(struct sadb_alg)) {
140 if (((struct sadb_alg *)p)->sadb_alg_id == alg_id)
141 return (struct sadb_alg *)p;
143 tlen -= sizeof(struct sadb_alg);
144 p += sizeof(struct sadb_alg);
147 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
153 struct sadb_supported *sup;
155 struct sadb_supported **ipsup;
157 switch (sup->sadb_supported_exttype) {
158 case SADB_EXT_SUPPORTED_AUTH:
159 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_AH)];
161 case SADB_EXT_SUPPORTED_ENCRYPT:
162 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_ESP)];
165 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
172 *ipsup = malloc(sup->sadb_supported_len);
174 __ipsec_set_strerror(strerror(errno));
177 memcpy(*ipsup, sup, sup->sadb_supported_len);
183 * check key length against algorithm specified.
184 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
185 * augument, and only calls to ipsec_check_keylen2();
186 * keylen is the unit of bit.
192 ipsec_check_keylen(supported, alg_id, keylen)
201 case SADB_EXT_SUPPORTED_AUTH:
202 satype = SADB_SATYPE_AH;
204 case SADB_EXT_SUPPORTED_ENCRYPT:
205 satype = SADB_SATYPE_ESP;
208 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
212 return ipsec_check_keylen2(satype, alg_id, keylen);
216 * check key length against algorithm specified.
217 * satype is one of satype defined at pfkeyv2.h.
218 * keylen is the unit of bit.
224 ipsec_check_keylen2(satype, alg_id, keylen)
229 struct sadb_alg *alg;
231 alg = findsupportedalg(satype, alg_id);
235 if (keylen < alg->sadb_alg_minbits || keylen > alg->sadb_alg_maxbits) {
236 fprintf(stderr, "%d %d %d\n", keylen, alg->sadb_alg_minbits,
237 alg->sadb_alg_maxbits);
238 __ipsec_errcode = EIPSEC_INVAL_KEYLEN;
242 __ipsec_errcode = EIPSEC_NO_ERROR;
247 * get max/min key length against algorithm specified.
248 * satype is one of satype defined at pfkeyv2.h.
249 * keylen is the unit of bit.
255 ipsec_get_keylen(supported, alg_id, alg0)
256 u_int supported, alg_id;
257 struct sadb_alg *alg0;
259 struct sadb_alg *alg;
264 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
269 case SADB_EXT_SUPPORTED_AUTH:
270 satype = SADB_SATYPE_AH;
272 case SADB_EXT_SUPPORTED_ENCRYPT:
273 satype = SADB_SATYPE_ESP;
276 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
280 alg = findsupportedalg(satype, alg_id);
284 memcpy(alg0, alg, sizeof(*alg0));
286 __ipsec_errcode = EIPSEC_NO_ERROR;
291 * set the rate for SOFT lifetime against HARD one.
292 * If rate is more than 100 or equal to zero, then set to 100.
294 static u_int soft_lifetime_allocations_rate = PFKEY_SOFT_LIFETIME_RATE;
295 static u_int soft_lifetime_bytes_rate = PFKEY_SOFT_LIFETIME_RATE;
296 static u_int soft_lifetime_addtime_rate = PFKEY_SOFT_LIFETIME_RATE;
297 static u_int soft_lifetime_usetime_rate = PFKEY_SOFT_LIFETIME_RATE;
300 pfkey_set_softrate(type, rate)
303 __ipsec_errcode = EIPSEC_NO_ERROR;
305 if (rate > 100 || rate == 0)
309 case SADB_X_LIFETIME_ALLOCATIONS:
310 soft_lifetime_allocations_rate = rate;
312 case SADB_X_LIFETIME_BYTES:
313 soft_lifetime_bytes_rate = rate;
315 case SADB_X_LIFETIME_ADDTIME:
316 soft_lifetime_addtime_rate = rate;
318 case SADB_X_LIFETIME_USETIME:
319 soft_lifetime_usetime_rate = rate;
323 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
328 * get current rate for SOFT lifetime against HARD one.
329 * ATTENTION: ~0 is returned if invalid type was passed.
332 pfkey_get_softrate(type)
336 case SADB_X_LIFETIME_ALLOCATIONS:
337 return soft_lifetime_allocations_rate;
338 case SADB_X_LIFETIME_BYTES:
339 return soft_lifetime_bytes_rate;
340 case SADB_X_LIFETIME_ADDTIME:
341 return soft_lifetime_addtime_rate;
342 case SADB_X_LIFETIME_USETIME:
343 return soft_lifetime_usetime_rate;
350 * sending SADB_GETSPI message to the kernel.
352 * positive: success and return length sent.
353 * -1 : error occured, and set errno.
356 pfkey_send_getspi(so, satype, mode, src, dst, min, max, reqid, seq)
359 struct sockaddr *src, *dst;
360 u_int32_t min, max, reqid, seq;
362 struct sadb_msg *newmsg;
365 int need_spirange = 0;
370 if (src == NULL || dst == NULL) {
371 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
374 if (src->sa_family != dst->sa_family) {
375 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
378 if (min > max || (min > 0 && min <= 255)) {
379 __ipsec_errcode = EIPSEC_INVAL_SPI;
382 switch (src->sa_family) {
384 plen = sizeof(struct in_addr) << 3;
387 plen = sizeof(struct in6_addr) << 3;
390 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
394 /* create new sadb_msg to send. */
395 len = sizeof(struct sadb_msg)
396 + sizeof(struct sadb_x_sa2)
397 + sizeof(struct sadb_address)
398 + PFKEY_ALIGN8(sysdep_sa_len(src))
399 + sizeof(struct sadb_address)
400 + PFKEY_ALIGN8(sysdep_sa_len(dst));
402 if (min > 255 && max < ~0) {
404 len += sizeof(struct sadb_spirange);
407 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
408 __ipsec_set_strerror(strerror(errno));
411 ep = ((caddr_t)newmsg) + len;
413 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_GETSPI,
414 len, satype, seq, getpid());
420 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
426 /* set sadb_address for source */
427 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
434 /* set sadb_address for destination */
435 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
442 /* proccessing spi range */
444 struct sadb_spirange spirange;
446 if (p + sizeof(spirange) > ep) {
451 memset(&spirange, 0, sizeof(spirange));
452 spirange.sadb_spirange_len = PFKEY_UNIT64(sizeof(spirange));
453 spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
454 spirange.sadb_spirange_min = min;
455 spirange.sadb_spirange_max = max;
457 memcpy(p, &spirange, sizeof(spirange));
459 p += sizeof(spirange);
467 len = pfkey_send(so, newmsg, len);
473 __ipsec_errcode = EIPSEC_NO_ERROR;
478 * sending SADB_UPDATE message to the kernel.
479 * The length of key material is a_keylen + e_keylen.
481 * positive: success and return length sent.
482 * -1 : error occured, and set errno.
485 pfkey_send_update(so, satype, mode, src, dst, spi, reqid, wsize,
486 keymat, e_type, e_keylen, a_type, a_keylen, flags,
487 l_alloc, l_bytes, l_addtime, l_usetime, seq)
489 u_int satype, mode, wsize;
490 struct sockaddr *src, *dst;
491 u_int32_t spi, reqid;
493 u_int e_type, e_keylen, a_type, a_keylen, flags;
495 u_int64_t l_bytes, l_addtime, l_usetime;
499 if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
501 keymat, e_type, e_keylen, a_type, a_keylen, flags,
502 l_alloc, l_bytes, l_addtime, l_usetime, seq,
509 #ifdef SADB_X_EXT_NAT_T_TYPE
511 pfkey_send_update_nat(so, satype, mode, src, dst, spi, reqid, wsize,
512 keymat, e_type, e_keylen, a_type, a_keylen, flags,
513 l_alloc, l_bytes, l_addtime, l_usetime, seq,
514 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
516 u_int satype, mode, wsize;
517 struct sockaddr *src, *dst;
518 u_int32_t spi, reqid;
520 u_int e_type, e_keylen, a_type, a_keylen, flags;
522 u_int64_t l_bytes, l_addtime, l_usetime;
524 u_int8_t l_natt_type;
525 u_int16_t l_natt_sport, l_natt_dport;
526 struct sockaddr *l_natt_oa;
529 if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
531 keymat, e_type, e_keylen, a_type, a_keylen, flags,
532 l_alloc, l_bytes, l_addtime, l_usetime, seq,
533 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)) < 0)
541 * sending SADB_ADD message to the kernel.
542 * The length of key material is a_keylen + e_keylen.
544 * positive: success and return length sent.
545 * -1 : error occured, and set errno.
548 pfkey_send_add(so, satype, mode, src, dst, spi, reqid, wsize,
549 keymat, e_type, e_keylen, a_type, a_keylen, flags,
550 l_alloc, l_bytes, l_addtime, l_usetime, seq)
552 u_int satype, mode, wsize;
553 struct sockaddr *src, *dst;
554 u_int32_t spi, reqid;
556 u_int e_type, e_keylen, a_type, a_keylen, flags;
558 u_int64_t l_bytes, l_addtime, l_usetime;
562 if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
564 keymat, e_type, e_keylen, a_type, a_keylen, flags,
565 l_alloc, l_bytes, l_addtime, l_usetime, seq,
572 #ifdef SADB_X_EXT_NAT_T_TYPE
574 pfkey_send_add_nat(so, satype, mode, src, dst, spi, reqid, wsize,
575 keymat, e_type, e_keylen, a_type, a_keylen, flags,
576 l_alloc, l_bytes, l_addtime, l_usetime, seq,
577 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
579 u_int satype, mode, wsize;
580 struct sockaddr *src, *dst;
581 u_int32_t spi, reqid;
583 u_int e_type, e_keylen, a_type, a_keylen, flags;
585 u_int64_t l_bytes, l_addtime, l_usetime;
587 u_int8_t l_natt_type;
588 u_int16_t l_natt_sport, l_natt_dport;
589 struct sockaddr *l_natt_oa;
592 if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
594 keymat, e_type, e_keylen, a_type, a_keylen, flags,
595 l_alloc, l_bytes, l_addtime, l_usetime, seq,
596 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)) < 0)
604 * sending SADB_DELETE message to the kernel.
606 * positive: success and return length sent.
607 * -1 : error occured, and set errno.
610 pfkey_send_delete(so, satype, mode, src, dst, spi)
613 struct sockaddr *src, *dst;
617 if ((len = pfkey_send_x2(so, SADB_DELETE, satype, mode, src, dst, spi)) < 0)
624 * sending SADB_DELETE without spi to the kernel. This is
625 * the "delete all" request (an extension also present in
629 * positive: success and return length sent
630 * -1 : error occured, and set errno
633 pfkey_send_delete_all(so, satype, mode, src, dst)
636 struct sockaddr *src, *dst;
638 struct sadb_msg *newmsg;
645 if (src == NULL || dst == NULL) {
646 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
649 if (src->sa_family != dst->sa_family) {
650 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
653 switch (src->sa_family) {
655 plen = sizeof(struct in_addr) << 3;
658 plen = sizeof(struct in6_addr) << 3;
661 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
665 /* create new sadb_msg to reply. */
666 len = sizeof(struct sadb_msg)
667 + sizeof(struct sadb_address)
668 + PFKEY_ALIGN8(sysdep_sa_len(src))
669 + sizeof(struct sadb_address)
670 + PFKEY_ALIGN8(sysdep_sa_len(dst));
672 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
673 __ipsec_set_strerror(strerror(errno));
676 ep = ((caddr_t)newmsg) + len;
678 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_DELETE, len, satype, 0,
684 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
690 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
698 len = pfkey_send(so, newmsg, len);
704 __ipsec_errcode = EIPSEC_NO_ERROR;
709 * sending SADB_GET message to the kernel.
711 * positive: success and return length sent.
712 * -1 : error occured, and set errno.
715 pfkey_send_get(so, satype, mode, src, dst, spi)
718 struct sockaddr *src, *dst;
722 if ((len = pfkey_send_x2(so, SADB_GET, satype, mode, src, dst, spi)) < 0)
729 * sending SADB_REGISTER message to the kernel.
731 * positive: success and return length sent.
732 * -1 : error occured, and set errno.
735 pfkey_send_register(so, satype)
741 if (satype == PF_UNSPEC) {
743 algno < sizeof(supported_map)/sizeof(supported_map[0]);
745 if (ipsec_supported[algno]) {
746 free(ipsec_supported[algno]);
747 ipsec_supported[algno] = NULL;
751 algno = findsupportedmap(satype);
753 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
757 if (ipsec_supported[algno]) {
758 free(ipsec_supported[algno]);
759 ipsec_supported[algno] = NULL;
763 if ((len = pfkey_send_x3(so, SADB_REGISTER, satype)) < 0)
770 * receiving SADB_REGISTER message from the kernel, and copy buffer for
771 * sadb_supported returned into ipsec_supported.
773 * 0: success and return length sent.
774 * -1: error occured, and set errno.
777 pfkey_recv_register(so)
780 pid_t pid = getpid();
781 struct sadb_msg *newmsg;
784 /* receive message */
786 if ((newmsg = pfkey_recv(so)) == NULL)
788 if (newmsg->sadb_msg_type == SADB_REGISTER &&
789 newmsg->sadb_msg_pid == pid)
795 newmsg->sadb_msg_len = PFKEY_UNUNIT64(newmsg->sadb_msg_len);
797 error = pfkey_set_supported(newmsg, newmsg->sadb_msg_len);
801 __ipsec_errcode = EIPSEC_NO_ERROR;
807 * receiving SADB_REGISTER message from the kernel, and copy buffer for
808 * sadb_supported returned into ipsec_supported.
809 * NOTE: sadb_msg_len must be host order.
811 * tlen: msg length, it's to makeing sure.
813 * 0: success and return length sent.
814 * -1: error occured, and set errno.
817 pfkey_set_supported(msg, tlen)
818 struct sadb_msg *msg;
821 struct sadb_supported *sup;
826 if (msg->sadb_msg_len != tlen) {
827 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
834 p += sizeof(struct sadb_msg);
837 sup = (struct sadb_supported *)p;
838 if (ep < p + sizeof(*sup) ||
839 PFKEY_EXTLEN(sup) < sizeof(*sup) ||
840 ep < p + sup->sadb_supported_len) {
845 switch (sup->sadb_supported_exttype) {
846 case SADB_EXT_SUPPORTED_AUTH:
847 case SADB_EXT_SUPPORTED_ENCRYPT:
850 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
855 sup->sadb_supported_len = PFKEY_EXTLEN(sup);
857 /* set supported map */
858 if (setsupportedmap(sup) != 0)
861 p += sup->sadb_supported_len;
865 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
869 __ipsec_errcode = EIPSEC_NO_ERROR;
875 * sending SADB_FLUSH message to the kernel.
877 * positive: success and return length sent.
878 * -1 : error occured, and set errno.
881 pfkey_send_flush(so, satype)
887 if ((len = pfkey_send_x3(so, SADB_FLUSH, satype)) < 0)
894 * sending SADB_DUMP message to the kernel.
896 * positive: success and return length sent.
897 * -1 : error occured, and set errno.
900 pfkey_send_dump(so, satype)
906 if ((len = pfkey_send_x3(so, SADB_DUMP, satype)) < 0)
913 * sending SADB_X_PROMISC message to the kernel.
914 * NOTE that this function handles promisc mode toggle only.
916 * flag: set promisc off if zero, set promisc on if non-zero.
918 * positive: success and return length sent.
919 * -1 : error occured, and set errno.
920 * 0 : error occured, and set errno.
921 * others: a pointer to new allocated buffer in which supported
925 pfkey_send_promisc_toggle(so, flag)
931 if ((len = pfkey_send_x3(so, SADB_X_PROMISC, (flag ? 1 : 0))) < 0)
938 * sending SADB_X_SPDADD message to the kernel.
940 * positive: success and return length sent.
941 * -1 : error occured, and set errno.
944 pfkey_send_spdadd(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
946 struct sockaddr *src, *dst;
947 u_int prefs, prefd, proto;
954 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
955 src, prefs, dst, prefd, proto,
957 policy, policylen, seq)) < 0)
964 * sending SADB_X_SPDADD message to the kernel.
966 * positive: success and return length sent.
967 * -1 : error occured, and set errno.
970 pfkey_send_spdadd2(so, src, prefs, dst, prefd, proto, ltime, vtime,
971 policy, policylen, seq)
973 struct sockaddr *src, *dst;
974 u_int prefs, prefd, proto;
975 u_int64_t ltime, vtime;
982 if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
983 src, prefs, dst, prefd, proto,
985 policy, policylen, seq)) < 0)
992 * sending SADB_X_SPDUPDATE message to the kernel.
994 * positive: success and return length sent.
995 * -1 : error occured, and set errno.
998 pfkey_send_spdupdate(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1000 struct sockaddr *src, *dst;
1001 u_int prefs, prefd, proto;
1008 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
1009 src, prefs, dst, prefd, proto,
1011 policy, policylen, seq)) < 0)
1018 * sending SADB_X_SPDUPDATE message to the kernel.
1020 * positive: success and return length sent.
1021 * -1 : error occured, and set errno.
1024 pfkey_send_spdupdate2(so, src, prefs, dst, prefd, proto, ltime, vtime,
1025 policy, policylen, seq)
1027 struct sockaddr *src, *dst;
1028 u_int prefs, prefd, proto;
1029 u_int64_t ltime, vtime;
1036 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
1037 src, prefs, dst, prefd, proto,
1039 policy, policylen, seq)) < 0)
1046 * sending SADB_X_SPDDELETE message to the kernel.
1048 * positive: success and return length sent.
1049 * -1 : error occured, and set errno.
1052 pfkey_send_spddelete(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1054 struct sockaddr *src, *dst;
1055 u_int prefs, prefd, proto;
1062 if (policylen != sizeof(struct sadb_x_policy)) {
1063 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1067 if ((len = pfkey_send_x4(so, SADB_X_SPDDELETE,
1068 src, prefs, dst, prefd, proto,
1070 policy, policylen, seq)) < 0)
1077 * sending SADB_X_SPDDELETE message to the kernel.
1079 * positive: success and return length sent.
1080 * -1 : error occured, and set errno.
1083 pfkey_send_spddelete2(so, spid)
1089 if ((len = pfkey_send_x5(so, SADB_X_SPDDELETE2, spid)) < 0)
1096 * sending SADB_X_SPDGET message to the kernel.
1098 * positive: success and return length sent.
1099 * -1 : error occured, and set errno.
1102 pfkey_send_spdget(so, spid)
1108 if ((len = pfkey_send_x5(so, SADB_X_SPDGET, spid)) < 0)
1115 * sending SADB_X_SPDSETIDX message to the kernel.
1117 * positive: success and return length sent.
1118 * -1 : error occured, and set errno.
1121 pfkey_send_spdsetidx(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1123 struct sockaddr *src, *dst;
1124 u_int prefs, prefd, proto;
1131 if (policylen != sizeof(struct sadb_x_policy)) {
1132 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1136 if ((len = pfkey_send_x4(so, SADB_X_SPDSETIDX,
1137 src, prefs, dst, prefd, proto,
1139 policy, policylen, seq)) < 0)
1146 * sending SADB_SPDFLUSH message to the kernel.
1148 * positive: success and return length sent.
1149 * -1 : error occured, and set errno.
1152 pfkey_send_spdflush(so)
1157 if ((len = pfkey_send_x3(so, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC)) < 0)
1164 * sending SADB_SPDDUMP message to the kernel.
1166 * positive: success and return length sent.
1167 * -1 : error occured, and set errno.
1170 pfkey_send_spddump(so)
1175 if ((len = pfkey_send_x3(so, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC)) < 0)
1181 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1183 pfkey_send_x1(so, type, satype, mode, src, dst, spi, reqid, wsize,
1184 keymat, e_type, e_keylen, a_type, a_keylen, flags,
1185 l_alloc, l_bytes, l_addtime, l_usetime, seq,
1186 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
1188 u_int type, satype, mode;
1189 struct sockaddr *src, *dst, *l_natt_oa;
1190 u_int32_t spi, reqid;
1193 u_int e_type, e_keylen, a_type, a_keylen, flags;
1194 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime, seq;
1195 u_int16_t l_natt_sport, l_natt_dport;
1196 u_int8_t l_natt_type;
1198 struct sadb_msg *newmsg;
1204 /* validity check */
1205 if (src == NULL || dst == NULL) {
1206 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1209 if (src->sa_family != dst->sa_family) {
1210 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1213 switch (src->sa_family) {
1215 plen = sizeof(struct in_addr) << 3;
1218 plen = sizeof(struct in6_addr) << 3;
1221 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1226 case SADB_SATYPE_ESP:
1227 if (e_type == SADB_EALG_NONE) {
1228 __ipsec_errcode = EIPSEC_NO_ALGS;
1232 case SADB_SATYPE_AH:
1233 if (e_type != SADB_EALG_NONE) {
1234 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1237 if (a_type == SADB_AALG_NONE) {
1238 __ipsec_errcode = EIPSEC_NO_ALGS;
1242 case SADB_X_SATYPE_IPCOMP:
1243 if (e_type == SADB_X_CALG_NONE) {
1244 __ipsec_errcode = EIPSEC_INVAL_ALGS;
1247 if (a_type != SADB_AALG_NONE) {
1248 __ipsec_errcode = EIPSEC_NO_ALGS;
1253 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1257 /* create new sadb_msg to reply. */
1258 len = sizeof(struct sadb_msg)
1259 + sizeof(struct sadb_sa)
1260 + sizeof(struct sadb_x_sa2)
1261 + sizeof(struct sadb_address)
1262 + PFKEY_ALIGN8(sysdep_sa_len(src))
1263 + sizeof(struct sadb_address)
1264 + PFKEY_ALIGN8(sysdep_sa_len(dst))
1265 + sizeof(struct sadb_lifetime)
1266 + sizeof(struct sadb_lifetime);
1268 if (e_type != SADB_EALG_NONE && satype != SADB_X_SATYPE_IPCOMP)
1269 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(e_keylen));
1270 if (a_type != SADB_AALG_NONE)
1271 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(a_keylen));
1273 #ifdef SADB_X_EXT_NAT_T_TYPE
1274 /* add nat-t packets */
1276 if (satype != SADB_SATYPE_ESP) {
1277 __ipsec_errcode = EIPSEC_NO_ALGS;
1281 len += sizeof(struct sadb_x_nat_t_type);
1282 len += sizeof(struct sadb_x_nat_t_port);
1283 len += sizeof(struct sadb_x_nat_t_port);
1285 len += sizeof(struct sadb_address) +
1286 PFKEY_ALIGN8(sysdep_sa_len(l_natt_oa));
1290 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1291 __ipsec_set_strerror(strerror(errno));
1294 ep = ((caddr_t)newmsg) + len;
1296 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1297 satype, seq, getpid());
1302 p = pfkey_setsadbsa(p, ep, spi, wsize, a_type, e_type, flags);
1307 p = pfkey_setsadbxsa2(p, ep, mode, reqid);
1312 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1318 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1325 if (e_type != SADB_EALG_NONE && satype != SADB_X_SATYPE_IPCOMP) {
1326 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_ENCRYPT,
1333 if (a_type != SADB_AALG_NONE) {
1334 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_AUTH,
1335 keymat + e_keylen, a_keylen);
1342 /* set sadb_lifetime for destination */
1343 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1344 l_alloc, l_bytes, l_addtime, l_usetime);
1349 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_SOFT,
1350 l_alloc, l_bytes, l_addtime, l_usetime);
1356 #ifdef SADB_X_EXT_NAT_T_TYPE
1357 /* Add nat-t messages */
1359 p = pfkey_set_natt_type(p, ep, SADB_X_EXT_NAT_T_TYPE, l_natt_type);
1365 p = pfkey_set_natt_port(p, ep, SADB_X_EXT_NAT_T_SPORT,
1372 p = pfkey_set_natt_port(p, ep, SADB_X_EXT_NAT_T_DPORT,
1380 p = pfkey_setsadbaddr(p, ep, SADB_X_EXT_NAT_T_OA,
1382 PFKEY_ALIGN8(sysdep_sa_len(l_natt_oa)),
1398 len = pfkey_send(so, newmsg, len);
1404 __ipsec_errcode = EIPSEC_NO_ERROR;
1408 /* sending SADB_DELETE or SADB_GET message to the kernel */
1410 pfkey_send_x2(so, type, satype, mode, src, dst, spi)
1412 u_int type, satype, mode;
1413 struct sockaddr *src, *dst;
1416 struct sadb_msg *newmsg;
1422 /* validity check */
1423 if (src == NULL || dst == NULL) {
1424 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1427 if (src->sa_family != dst->sa_family) {
1428 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1431 switch (src->sa_family) {
1433 plen = sizeof(struct in_addr) << 3;
1436 plen = sizeof(struct in6_addr) << 3;
1439 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1443 /* create new sadb_msg to reply. */
1444 len = sizeof(struct sadb_msg)
1445 + sizeof(struct sadb_sa)
1446 + sizeof(struct sadb_address)
1447 + PFKEY_ALIGN8(sysdep_sa_len(src))
1448 + sizeof(struct sadb_address)
1449 + PFKEY_ALIGN8(sysdep_sa_len(dst));
1451 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1452 __ipsec_set_strerror(strerror(errno));
1455 ep = ((caddr_t)newmsg) + len;
1457 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1463 p = pfkey_setsadbsa(p, ep, spi, 0, 0, 0, 0);
1468 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1474 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1476 if (!p || p != ep) {
1482 len = pfkey_send(so, newmsg, len);
1488 __ipsec_errcode = EIPSEC_NO_ERROR;
1493 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1497 pfkey_send_x3(so, type, satype)
1501 struct sadb_msg *newmsg;
1506 /* validity check */
1508 case SADB_X_PROMISC:
1509 if (satype != 0 && satype != 1) {
1510 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1516 case SADB_SATYPE_UNSPEC:
1517 case SADB_SATYPE_AH:
1518 case SADB_SATYPE_ESP:
1519 case SADB_X_SATYPE_IPCOMP:
1522 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1527 /* create new sadb_msg to send. */
1528 len = sizeof(struct sadb_msg);
1530 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1531 __ipsec_set_strerror(strerror(errno));
1534 ep = ((caddr_t)newmsg) + len;
1536 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1538 if (!p || p != ep) {
1544 len = pfkey_send(so, newmsg, len);
1550 __ipsec_errcode = EIPSEC_NO_ERROR;
1554 /* sending SADB_X_SPDADD message to the kernel */
1556 pfkey_send_x4(so, type, src, prefs, dst, prefd, proto,
1557 ltime, vtime, policy, policylen, seq)
1559 struct sockaddr *src, *dst;
1560 u_int type, prefs, prefd, proto;
1561 u_int64_t ltime, vtime;
1566 struct sadb_msg *newmsg;
1572 /* validity check */
1573 if (src == NULL || dst == NULL) {
1574 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1577 if (src->sa_family != dst->sa_family) {
1578 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1582 switch (src->sa_family) {
1584 plen = sizeof(struct in_addr) << 3;
1587 plen = sizeof(struct in6_addr) << 3;
1590 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1593 if (prefs > plen || prefd > plen) {
1594 __ipsec_errcode = EIPSEC_INVAL_PREFIXLEN;
1598 /* create new sadb_msg to reply. */
1599 len = sizeof(struct sadb_msg)
1600 + sizeof(struct sadb_address)
1601 + PFKEY_ALIGN8(sysdep_sa_len(src))
1602 + sizeof(struct sadb_address)
1603 + PFKEY_ALIGN8(sysdep_sa_len(src))
1604 + sizeof(struct sadb_lifetime)
1607 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1608 __ipsec_set_strerror(strerror(errno));
1611 ep = ((caddr_t)newmsg) + len;
1613 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1614 SADB_SATYPE_UNSPEC, seq, getpid());
1619 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, prefs, proto);
1624 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, prefd, proto);
1629 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1630 0, 0, ltime, vtime);
1631 if (!p || p + policylen != ep) {
1635 memcpy(p, policy, policylen);
1638 len = pfkey_send(so, newmsg, len);
1644 __ipsec_errcode = EIPSEC_NO_ERROR;
1648 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1650 pfkey_send_x5(so, type, spid)
1655 struct sadb_msg *newmsg;
1656 struct sadb_x_policy xpl;
1661 /* create new sadb_msg to reply. */
1662 len = sizeof(struct sadb_msg)
1665 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1666 __ipsec_set_strerror(strerror(errno));
1669 ep = ((caddr_t)newmsg) + len;
1671 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1672 SADB_SATYPE_UNSPEC, 0, getpid());
1678 if (p + sizeof(xpl) != ep) {
1682 memset(&xpl, 0, sizeof(xpl));
1683 xpl.sadb_x_policy_len = PFKEY_UNIT64(sizeof(xpl));
1684 xpl.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1685 xpl.sadb_x_policy_id = spid;
1686 memcpy(p, &xpl, sizeof(xpl));
1689 len = pfkey_send(so, newmsg, len);
1695 __ipsec_errcode = EIPSEC_NO_ERROR;
1703 * others : success and return value of socket.
1709 const int bufsiz = 128 * 1024; /*is 128K enough?*/
1711 if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
1712 __ipsec_set_strerror(strerror(errno));
1717 * This is a temporary workaround for KAME PR 154.
1718 * Don't really care even if it fails.
1720 (void)setsockopt(so, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz));
1721 (void)setsockopt(so, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz));
1723 __ipsec_errcode = EIPSEC_NO_ERROR;
1739 __ipsec_errcode = EIPSEC_NO_ERROR;
1744 * receive sadb_msg data, and return pointer to new buffer allocated.
1745 * Must free this buffer later.
1747 * NULL : error occured.
1748 * others : a pointer to sadb_msg structure.
1750 * XXX should be rewritten to pass length explicitly
1756 struct sadb_msg buf, *newmsg;
1759 while ((len = recv(so, (caddr_t)&buf, sizeof(buf), MSG_PEEK)) < 0) {
1762 __ipsec_set_strerror(strerror(errno));
1766 if (len < sizeof(buf)) {
1767 recv(so, (caddr_t)&buf, sizeof(buf), 0);
1768 __ipsec_errcode = EIPSEC_MAX;
1772 /* read real message */
1773 reallen = PFKEY_UNUNIT64(buf.sadb_msg_len);
1774 if ((newmsg = CALLOC(reallen, struct sadb_msg *)) == 0) {
1775 __ipsec_set_strerror(strerror(errno));
1779 while ((len = recv(so, (caddr_t)newmsg, reallen, 0)) < 0) {
1782 __ipsec_set_strerror(strerror(errno));
1787 if (len != reallen) {
1788 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1793 /* don't trust what the kernel says, validate! */
1794 if (PFKEY_UNUNIT64(newmsg->sadb_msg_len) != len) {
1795 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1800 __ipsec_errcode = EIPSEC_NO_ERROR;
1805 * send message to a socket.
1807 * others: success and return length sent.
1811 pfkey_send(so, msg, len)
1813 struct sadb_msg *msg;
1816 if ((len = send(so, (caddr_t)msg, len, 0)) < 0) {
1817 __ipsec_set_strerror(strerror(errno));
1821 __ipsec_errcode = EIPSEC_NO_ERROR;
1827 * NOTE: These functions are derived from netkey/key.c in KAME.
1830 * set the pointer to each header in this message buffer.
1831 * IN: msg: pointer to message buffer.
1832 * mhp: pointer to the buffer initialized like below:
1833 * caddr_t mhp[SADB_EXT_MAX + 1];
1837 * XXX should be rewritten to obtain length explicitly
1840 pfkey_align(msg, mhp)
1841 struct sadb_msg *msg;
1844 struct sadb_ext *ext;
1847 caddr_t ep; /* XXX should be passed from upper layer */
1849 /* validity check */
1850 if (msg == NULL || mhp == NULL) {
1851 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1856 for (i = 0; i < SADB_EXT_MAX + 1; i++)
1859 mhp[0] = (caddr_t)msg;
1863 ep = p + PFKEY_UNUNIT64(msg->sadb_msg_len);
1865 /* skip base header */
1866 p += sizeof(struct sadb_msg);
1869 ext = (struct sadb_ext *)p;
1870 if (ep < p + sizeof(*ext) || PFKEY_EXTLEN(ext) < sizeof(*ext) ||
1871 ep < p + PFKEY_EXTLEN(ext)) {
1872 /* invalid format */
1876 /* duplicate check */
1877 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1878 if (mhp[ext->sadb_ext_type] != NULL) {
1879 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1884 switch (ext->sadb_ext_type) {
1886 case SADB_EXT_LIFETIME_CURRENT:
1887 case SADB_EXT_LIFETIME_HARD:
1888 case SADB_EXT_LIFETIME_SOFT:
1889 case SADB_EXT_ADDRESS_SRC:
1890 case SADB_EXT_ADDRESS_DST:
1891 case SADB_EXT_ADDRESS_PROXY:
1892 case SADB_EXT_KEY_AUTH:
1893 /* XXX should to be check weak keys. */
1894 case SADB_EXT_KEY_ENCRYPT:
1895 /* XXX should to be check weak keys. */
1896 case SADB_EXT_IDENTITY_SRC:
1897 case SADB_EXT_IDENTITY_DST:
1898 case SADB_EXT_SENSITIVITY:
1899 case SADB_EXT_PROPOSAL:
1900 case SADB_EXT_SUPPORTED_AUTH:
1901 case SADB_EXT_SUPPORTED_ENCRYPT:
1902 case SADB_EXT_SPIRANGE:
1903 case SADB_X_EXT_POLICY:
1904 case SADB_X_EXT_SA2:
1905 #ifdef SADB_X_EXT_NAT_T_TYPE
1906 case SADB_X_EXT_NAT_T_TYPE:
1907 case SADB_X_EXT_NAT_T_SPORT:
1908 case SADB_X_EXT_NAT_T_DPORT:
1909 case SADB_X_EXT_NAT_T_OA:
1911 #ifdef SADB_X_EXT_TAG
1912 case SADB_X_EXT_TAG:
1914 mhp[ext->sadb_ext_type] = (caddr_t)ext;
1917 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1921 p += PFKEY_EXTLEN(ext);
1925 __ipsec_errcode = EIPSEC_INVAL_SADBMSG;
1929 __ipsec_errcode = EIPSEC_NO_ERROR;
1934 * check basic usage for sadb_msg,
1935 * NOTE: This routine is derived from netkey/key.c in KAME.
1936 * IN: msg: pointer to message buffer.
1937 * mhp: pointer to the buffer initialized like below:
1939 * caddr_t mhp[SADB_EXT_MAX + 1];
1948 struct sadb_msg *msg;
1950 /* validity check */
1951 if (mhp == NULL || mhp[0] == NULL) {
1952 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1956 msg = (struct sadb_msg *)mhp[0];
1959 if (msg->sadb_msg_version != PF_KEY_V2) {
1960 __ipsec_errcode = EIPSEC_INVAL_VERSION;
1965 if (msg->sadb_msg_type > SADB_MAX) {
1966 __ipsec_errcode = EIPSEC_INVAL_MSGTYPE;
1971 switch (msg->sadb_msg_satype) {
1972 case SADB_SATYPE_UNSPEC:
1973 switch (msg->sadb_msg_type) {
1981 #ifdef SADB_X_NAT_T_NEW_MAPPING
1982 case SADB_X_NAT_T_NEW_MAPPING:
1984 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1988 case SADB_SATYPE_ESP:
1989 case SADB_SATYPE_AH:
1990 case SADB_X_SATYPE_IPCOMP:
1991 switch (msg->sadb_msg_type) {
1993 case SADB_X_SPDDELETE:
1995 case SADB_X_SPDDUMP:
1996 case SADB_X_SPDFLUSH:
1997 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
2000 #ifdef SADB_X_NAT_T_NEW_MAPPING
2001 if (msg->sadb_msg_type == SADB_X_NAT_T_NEW_MAPPING &&
2002 msg->sadb_msg_satype != SADB_SATYPE_ESP) {
2003 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
2008 case SADB_SATYPE_RSVP:
2009 case SADB_SATYPE_OSPFV2:
2010 case SADB_SATYPE_RIPV2:
2011 case SADB_SATYPE_MIP:
2012 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
2014 case 1: /* XXX: What does it do ? */
2015 if (msg->sadb_msg_type == SADB_X_PROMISC)
2019 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
2023 /* check field of upper layer protocol and address family */
2024 if (mhp[SADB_EXT_ADDRESS_SRC] != NULL
2025 && mhp[SADB_EXT_ADDRESS_DST] != NULL) {
2026 struct sadb_address *src0, *dst0;
2028 src0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_SRC]);
2029 dst0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_DST]);
2031 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
2032 __ipsec_errcode = EIPSEC_PROTO_MISMATCH;
2036 if (PFKEY_ADDR_SADDR(src0)->sa_family
2037 != PFKEY_ADDR_SADDR(dst0)->sa_family) {
2038 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
2042 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
2047 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
2052 * prefixlen == 0 is valid because there must be the case
2053 * all addresses are matched.
2057 __ipsec_errcode = EIPSEC_NO_ERROR;
2062 * set data into sadb_msg.
2063 * `buf' must has been allocated sufficiently.
2066 pfkey_setsadbmsg(buf, lim, type, tlen, satype, seq, pid)
2077 p = (struct sadb_msg *)buf;
2078 len = sizeof(struct sadb_msg);
2080 if (buf + len > lim)
2084 p->sadb_msg_version = PF_KEY_V2;
2085 p->sadb_msg_type = type;
2086 p->sadb_msg_errno = 0;
2087 p->sadb_msg_satype = satype;
2088 p->sadb_msg_len = PFKEY_UNIT64(tlen);
2089 p->sadb_msg_reserved = 0;
2090 p->sadb_msg_seq = seq;
2091 p->sadb_msg_pid = (u_int32_t)pid;
2097 * copy secasvar data into sadb_address.
2098 * `buf' must has been allocated sufficiently.
2101 pfkey_setsadbsa(buf, lim, spi, wsize, auth, enc, flags)
2104 u_int32_t spi, flags;
2105 u_int wsize, auth, enc;
2110 p = (struct sadb_sa *)buf;
2111 len = sizeof(struct sadb_sa);
2113 if (buf + len > lim)
2117 p->sadb_sa_len = PFKEY_UNIT64(len);
2118 p->sadb_sa_exttype = SADB_EXT_SA;
2119 p->sadb_sa_spi = spi;
2120 p->sadb_sa_replay = wsize;
2121 p->sadb_sa_state = SADB_SASTATE_LARVAL;
2122 p->sadb_sa_auth = auth;
2123 p->sadb_sa_encrypt = enc;
2124 p->sadb_sa_flags = flags;
2130 * set data into sadb_address.
2131 * `buf' must has been allocated sufficiently.
2132 * prefixlen is in bits.
2135 pfkey_setsadbaddr(buf, lim, exttype, saddr, prefixlen, ul_proto)
2139 struct sockaddr *saddr;
2143 struct sadb_address *p;
2146 p = (struct sadb_address *)buf;
2147 len = sizeof(struct sadb_address) + PFKEY_ALIGN8(sysdep_sa_len(saddr));
2149 if (buf + len > lim)
2153 p->sadb_address_len = PFKEY_UNIT64(len);
2154 p->sadb_address_exttype = exttype & 0xffff;
2155 p->sadb_address_proto = ul_proto & 0xff;
2156 p->sadb_address_prefixlen = prefixlen;
2157 p->sadb_address_reserved = 0;
2159 memcpy(p + 1, saddr, sysdep_sa_len(saddr));
2165 * set sadb_key structure after clearing buffer with zero.
2166 * OUT: the pointer of buf + len.
2169 pfkey_setsadbkey(buf, lim, type, key, keylen)
2178 p = (struct sadb_key *)buf;
2179 len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
2181 if (buf + len > lim)
2185 p->sadb_key_len = PFKEY_UNIT64(len);
2186 p->sadb_key_exttype = type;
2187 p->sadb_key_bits = keylen << 3;
2188 p->sadb_key_reserved = 0;
2190 memcpy(p + 1, key, keylen);
2196 * set sadb_lifetime structure after clearing buffer with zero.
2197 * OUT: the pointer of buf + len.
2200 pfkey_setsadblifetime(buf, lim, type, l_alloc, l_bytes, l_addtime, l_usetime)
2204 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime;
2206 struct sadb_lifetime *p;
2209 p = (struct sadb_lifetime *)buf;
2210 len = sizeof(struct sadb_lifetime);
2212 if (buf + len > lim)
2216 p->sadb_lifetime_len = PFKEY_UNIT64(len);
2217 p->sadb_lifetime_exttype = type;
2220 case SADB_EXT_LIFETIME_SOFT:
2221 p->sadb_lifetime_allocations
2222 = (l_alloc * soft_lifetime_allocations_rate) /100;
2223 p->sadb_lifetime_bytes
2224 = (l_bytes * soft_lifetime_bytes_rate) /100;
2225 p->sadb_lifetime_addtime
2226 = (l_addtime * soft_lifetime_addtime_rate) /100;
2227 p->sadb_lifetime_usetime
2228 = (l_usetime * soft_lifetime_usetime_rate) /100;
2230 case SADB_EXT_LIFETIME_HARD:
2231 p->sadb_lifetime_allocations = l_alloc;
2232 p->sadb_lifetime_bytes = l_bytes;
2233 p->sadb_lifetime_addtime = l_addtime;
2234 p->sadb_lifetime_usetime = l_usetime;
2242 * copy secasvar data into sadb_address.
2243 * `buf' must has been allocated sufficiently.
2246 pfkey_setsadbxsa2(buf, lim, mode0, reqid)
2252 struct sadb_x_sa2 *p;
2253 u_int8_t mode = mode0 & 0xff;
2256 p = (struct sadb_x_sa2 *)buf;
2257 len = sizeof(struct sadb_x_sa2);
2259 if (buf + len > lim)
2263 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
2264 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
2265 p->sadb_x_sa2_mode = mode;
2266 p->sadb_x_sa2_reqid = reqid;
2271 #ifdef SADB_X_EXT_NAT_T_TYPE
2273 pfkey_set_natt_type(buf, lim, type, l_natt_type)
2277 u_int8_t l_natt_type;
2279 struct sadb_x_nat_t_type *p;
2282 p = (struct sadb_x_nat_t_type *)buf;
2283 len = sizeof(struct sadb_x_nat_t_type);
2285 if (buf + len > lim)
2289 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
2290 p->sadb_x_nat_t_type_exttype = type;
2291 p->sadb_x_nat_t_type_type = l_natt_type;
2297 pfkey_set_natt_port(buf, lim, type, l_natt_port)
2301 u_int16_t l_natt_port;
2303 struct sadb_x_nat_t_port *p;
2306 p = (struct sadb_x_nat_t_port *)buf;
2307 len = sizeof(struct sadb_x_nat_t_port);
2309 if (buf + len > lim)
2313 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
2314 p->sadb_x_nat_t_port_exttype = type;
2315 p->sadb_x_nat_t_port_port = htons(l_natt_port);