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 <sys/sysctl.h>
41 #include <netinet/in.h>
42 #ifdef HAVE_NETINET6_IPSEC
43 # include <netinet6/ipsec.h>
45 # include <netinet/ipsec.h>
57 #include "ipsec_strerror.h"
60 #define CALLOC(size, cast) (cast)calloc(1, (size))
62 static int findsupportedmap (int);
63 static int setsupportedmap (struct sadb_supported
*);
64 static struct sadb_alg
*findsupportedalg (u_int
, u_int
);
65 static int pfkey_send_x1 (int, u_int
, u_int
, u_int
, struct sockaddr_storage
*,
66 struct sockaddr_storage
*, u_int32_t
, u_int32_t
, u_int
, caddr_t
,
67 u_int
, u_int
, u_int
, u_int
, u_int
, u_int32_t
, u_int32_t
,
68 u_int32_t
, u_int32_t
, u_int32_t
, u_int16_t
, u_int
);
69 static int pfkey_send_x2 (int, u_int
, u_int
, u_int
,
70 struct sockaddr_storage
*, struct sockaddr_storage
*, u_int32_t
);
71 static int pfkey_send_x3 (int, u_int
, u_int
);
72 static int pfkey_send_x4 (int, u_int
, struct sockaddr_storage
*, struct sockaddr_storage
*, u_int
,
73 struct sockaddr_storage
*, struct sockaddr_storage
*, u_int
, u_int
, u_int64_t
, u_int64_t
,
74 char *, int, u_int32_t
, char *, char *, char *, u_int
);
75 static int pfkey_send_x5 (int, u_int
, u_int32_t
);
77 static caddr_t
pfkey_setsadbmsg (caddr_t
, caddr_t
, u_int
, u_int
,
78 u_int
, u_int32_t
, pid_t
);
79 static caddr_t
pfkey_setsadbsa (caddr_t
, caddr_t
, u_int32_t
, u_int
,
80 u_int
, u_int
, u_int32_t
, u_int16_t
);
81 static caddr_t
pfkey_setsadbaddr (caddr_t
, caddr_t
, u_int
,
82 struct sockaddr_storage
*, u_int
, u_int
);
83 static caddr_t
pfkey_setsadbkey (caddr_t
, caddr_t
, u_int
, caddr_t
, u_int
);
84 static caddr_t
pfkey_setsadblifetime (caddr_t
, caddr_t
, u_int
, u_int32_t
,
85 u_int32_t
, u_int32_t
, u_int32_t
);
86 static caddr_t
pfkey_setsadbipsecif(caddr_t
, caddr_t
, char *,
88 static caddr_t
pfkey_setsadbxsa2 (caddr_t
, caddr_t
, u_int32_t
, u_int32_t
, u_int
);
90 #ifdef SADB_X_EXT_NAT_T_TYPE
91 static caddr_t
pfkey_set_natt_type (caddr_t
, caddr_t
, u_int
, u_int8_t
);
92 static caddr_t
pfkey_set_natt_port (caddr_t
, caddr_t
, u_int
, u_int16_t
);
94 #ifdef SADB_X_EXT_NAT_T_FRAG
95 static caddr_t
pfkey_set_natt_frag (caddr_t
, caddr_t
, u_int
, u_int16_t
);
99 * make and search supported algorithm structure.
101 static struct sadb_supported
*ipsec_supported
[] = { NULL
, NULL
, NULL
,
102 #ifdef SADB_X_SATYPE_TCPSIGNATURE
107 static int supported_map
[] = {
110 SADB_X_SATYPE_IPCOMP
,
111 #ifdef SADB_X_SATYPE_TCPSIGNATURE
112 SADB_X_SATYPE_TCPSIGNATURE
,
117 findsupportedmap(int satype
)
121 for (i
= 0; i
< sizeof(supported_map
)/sizeof(supported_map
[0]); i
++)
122 if (supported_map
[i
] == satype
)
127 static struct sadb_alg
*
128 findsupportedalg(u_int satype
, u_int alg_id
)
135 algno
= findsupportedmap((int)satype
);
137 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
140 if (ipsec_supported
[algno
] == NULL
) {
141 __ipsec_errcode
= EIPSEC_DO_GET_SUPP_LIST
;
145 tlen
= ipsec_supported
[algno
]->sadb_supported_len
146 - sizeof(struct sadb_supported
);
147 p
= (void *)(ipsec_supported
[algno
] + 1);
149 if (tlen
< sizeof(struct sadb_alg
)) {
153 if (((struct sadb_alg
*)(void *)p
)->sadb_alg_id
== alg_id
)
156 tlen
-= sizeof(struct sadb_alg
);
157 p
+= sizeof(struct sadb_alg
);
160 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
165 setsupportedmap(struct sadb_supported
*sup
)
167 struct sadb_supported
**ipsup
;
169 switch (sup
->sadb_supported_exttype
) {
170 case SADB_EXT_SUPPORTED_AUTH
:
171 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_AH
)];
173 case SADB_EXT_SUPPORTED_ENCRYPT
:
174 ipsup
= &ipsec_supported
[findsupportedmap(SADB_SATYPE_ESP
)];
177 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
184 *ipsup
= malloc((size_t)sup
->sadb_supported_len
);
186 __ipsec_set_strerror(strerror(errno
));
189 memcpy(*ipsup
, sup
, (size_t)sup
->sadb_supported_len
);
195 * check key length against algorithm specified.
196 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
197 * augument, and only calls to ipsec_check_keylen2();
198 * keylen is the unit of bit.
204 ipsec_check_keylen(u_int supported
, u_int alg_id
, u_int keylen
)
210 case SADB_EXT_SUPPORTED_AUTH
:
211 satype
= SADB_SATYPE_AH
;
213 case SADB_EXT_SUPPORTED_ENCRYPT
:
214 satype
= SADB_SATYPE_ESP
;
217 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
221 return ipsec_check_keylen2(satype
, alg_id
, keylen
);
225 * check key length against algorithm specified.
226 * satype is one of satype defined at pfkeyv2.h.
227 * keylen is the unit of bit.
233 ipsec_check_keylen2(u_int satype
, u_int alg_id
, u_int keylen
)
235 struct sadb_alg
*alg
;
237 alg
= findsupportedalg(satype
, alg_id
);
241 if (keylen
< alg
->sadb_alg_minbits
|| keylen
> alg
->sadb_alg_maxbits
) {
242 fprintf(stderr
, "%d %d %d\n", keylen
, alg
->sadb_alg_minbits
,
243 alg
->sadb_alg_maxbits
);
244 __ipsec_errcode
= EIPSEC_INVAL_KEYLEN
;
248 __ipsec_errcode
= EIPSEC_NO_ERROR
;
253 * get max/min key length against algorithm specified.
254 * satype is one of satype defined at pfkeyv2.h.
255 * keylen is the unit of bit.
261 ipsec_get_keylen(u_int supported
, u_int alg_id
, struct sadb_alg
*alg0
)
263 struct sadb_alg
*alg
;
268 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
273 case SADB_EXT_SUPPORTED_AUTH
:
274 satype
= SADB_SATYPE_AH
;
276 case SADB_EXT_SUPPORTED_ENCRYPT
:
277 satype
= SADB_SATYPE_ESP
;
280 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
284 alg
= findsupportedalg(satype
, alg_id
);
288 memcpy(alg0
, alg
, sizeof(*alg0
));
290 __ipsec_errcode
= EIPSEC_NO_ERROR
;
295 * set the rate for SOFT lifetime against HARD one.
296 * If rate is more than 100 or equal to zero, then set to 100.
298 static u_int soft_lifetime_allocations_rate
= PFKEY_SOFT_LIFETIME_RATE
;
299 static u_int soft_lifetime_bytes_rate
= PFKEY_SOFT_LIFETIME_RATE
;
300 static u_int soft_lifetime_addtime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
301 static u_int soft_lifetime_usetime_rate
= PFKEY_SOFT_LIFETIME_RATE
;
304 pfkey_set_softrate(u_int type
, u_int rate
)
306 __ipsec_errcode
= EIPSEC_NO_ERROR
;
308 if (rate
> 100 || rate
== 0)
312 case SADB_X_LIFETIME_ALLOCATIONS
:
313 soft_lifetime_allocations_rate
= rate
;
315 case SADB_X_LIFETIME_BYTES
:
316 soft_lifetime_bytes_rate
= rate
;
318 case SADB_X_LIFETIME_ADDTIME
:
319 soft_lifetime_addtime_rate
= rate
;
321 case SADB_X_LIFETIME_USETIME
:
322 soft_lifetime_usetime_rate
= rate
;
326 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
331 * get current rate for SOFT lifetime against HARD one.
332 * ATTENTION: ~0 is returned if invalid type was passed.
335 pfkey_get_softrate(u_int type
)
338 case SADB_X_LIFETIME_ALLOCATIONS
:
339 return soft_lifetime_allocations_rate
;
340 case SADB_X_LIFETIME_BYTES
:
341 return soft_lifetime_bytes_rate
;
342 case SADB_X_LIFETIME_ADDTIME
:
343 return soft_lifetime_addtime_rate
;
344 case SADB_X_LIFETIME_USETIME
:
345 return soft_lifetime_usetime_rate
;
352 * sending SADB_GETSPI message to the kernel.
354 * positive: success and return length sent.
355 * -1 : error occured, and set errno.
358 pfkey_send_getspi(int so
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
,
359 u_int32_t min
, u_int32_t max
, u_int32_t reqid
, u_int use_addtime
, u_int64_t l_addtime
, u_int32_t seq
, u_int always_expire
)
361 struct sadb_msg
*newmsg
;
364 int need_spirange
= 0;
369 if (src
== NULL
|| dst
== NULL
) {
370 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
373 if (src
->ss_family
!= dst
->ss_family
) {
374 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
377 if (min
> max
|| (min
> 0 && min
<= 255)) {
378 __ipsec_errcode
= EIPSEC_INVAL_SPI
;
381 switch (src
->ss_family
) {
383 plen
= sizeof(struct in_addr
) << 3;
386 plen
= sizeof(struct in6_addr
) << 3;
389 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
393 /* create new sadb_msg to send. */
394 len
= sizeof(struct sadb_msg
)
395 + sizeof(struct sadb_x_sa2
)
396 + sizeof(struct sadb_address
)
397 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
398 + sizeof(struct sadb_address
)
399 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)dst
))
400 + ((use_addtime
) ? sizeof(struct sadb_lifetime
) : 0);
402 if (min
> 255 && max
< (u_int
)~0) {
404 len
+= sizeof(struct sadb_spirange
);
407 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
408 __ipsec_set_strerror(strerror(errno
));
411 ep
= ((caddr_t
)(void *)newmsg
) + len
;
413 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, SADB_GETSPI
,
414 (u_int
)len
, satype
, seq
, getpid());
420 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
, always_expire
);
426 /* set sadb_address for source */
427 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, (u_int
)plen
,
434 /* set sadb_address for destination */
435 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, (u_int
)plen
,
443 /* set sadb_lifetime, only hard lifetime applicable for larval SAs */
444 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
452 /* proccessing spi range */
454 struct sadb_spirange spirange
;
456 if (p
+ sizeof(spirange
) > ep
) {
461 memset(&spirange
, 0, sizeof(spirange
));
462 spirange
.sadb_spirange_len
= PFKEY_UNIT64(sizeof(spirange
));
463 spirange
.sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
464 spirange
.sadb_spirange_min
= min
;
465 spirange
.sadb_spirange_max
= max
;
467 memcpy(p
, &spirange
, sizeof(spirange
));
469 p
+= sizeof(spirange
);
477 len
= pfkey_send(so
, newmsg
, len
);
483 __ipsec_errcode
= EIPSEC_NO_ERROR
;
489 * sending SADB_UPDATE message to the kernel.
490 * The length of key material is a_keylen + e_keylen.
492 * positive: success and return length sent.
493 * -1 : error occured, and set errno.
496 pfkey_send_update(int so
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
,
497 u_int32_t spi
, u_int32_t reqid
, u_int wsize
, caddr_t keymat
, u_int e_type
, u_int e_keylen
,
498 u_int a_type
, u_int a_keylen
, u_int flags
, u_int32_t l_alloc
, u_int64_t l_bytes
,
499 u_int64_t l_addtime
, u_int64_t l_usetime
, u_int32_t seq
, u_int16_t port
, u_int always_expire
)
502 if ((len
= pfkey_send_x1(so
, SADB_UPDATE
, satype
, mode
, src
, dst
, spi
,
504 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
505 l_alloc
, (u_int
)l_bytes
, (u_int
)l_addtime
,
506 (u_int
)l_usetime
, seq
, port
, always_expire
)) < 0)
514 * sending SADB_ADD message to the kernel.
515 * The length of key material is a_keylen + e_keylen.
517 * positive: success and return length sent.
518 * -1 : error occured, and set errno.
521 pfkey_send_add(int so
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
,
522 u_int32_t spi
, u_int32_t reqid
, u_int wsize
, caddr_t keymat
, u_int e_type
, u_int e_keylen
,
523 u_int a_type
, u_int a_keylen
, u_int flags
, u_int32_t l_alloc
, u_int64_t l_bytes
,
524 u_int64_t l_addtime
, u_int64_t l_usetime
, u_int32_t seq
, u_int16_t port
, u_int always_expire
)
527 if ((len
= pfkey_send_x1(so
, SADB_ADD
, satype
, mode
, src
, dst
, spi
,
529 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
530 l_alloc
, (u_int
)l_bytes
, (u_int
)l_addtime
,
531 (u_int
)l_usetime
, seq
, port
, always_expire
)) < 0)
539 * sending SADB_DELETE message to the kernel.
541 * positive: success and return length sent.
542 * -1 : error occured, and set errno.
545 pfkey_send_delete(so
, satype
, mode
, src
, dst
, spi
)
548 struct sockaddr_storage
*src
, *dst
;
552 if ((len
= pfkey_send_x2(so
, SADB_DELETE
, satype
, mode
, src
, dst
, spi
)) < 0)
559 * sending SADB_DELETE without spi to the kernel. This is
560 * the "delete all" request (an extension also present in
564 * positive: success and return length sent
565 * -1 : error occured, and set errno
569 pfkey_send_delete_all(int so
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
)
571 struct sadb_msg
*newmsg
;
578 if (src
== NULL
|| dst
== NULL
) {
579 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
582 if (src
->ss_family
!= dst
->ss_family
) {
583 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
586 switch (src
->ss_family
) {
588 plen
= sizeof(struct in_addr
) << 3;
591 plen
= sizeof(struct in6_addr
) << 3;
594 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
598 /* create new sadb_msg to reply. */
599 len
= sizeof(struct sadb_msg
)
600 + sizeof(struct sadb_address
)
601 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
602 + sizeof(struct sadb_address
)
603 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)dst
));
605 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
606 __ipsec_set_strerror(strerror(errno
));
609 ep
= ((caddr_t
)(void *)newmsg
) + len
;
611 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, SADB_DELETE
, (u_int
)len
,
612 satype
, 0, getpid());
617 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, (u_int
)plen
,
623 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, (u_int
)plen
,
631 len
= pfkey_send(so
, newmsg
, len
);
637 __ipsec_errcode
= EIPSEC_NO_ERROR
;
642 * sending SADB_GET message to the kernel.
644 * positive: success and return length sent.
645 * -1 : error occured, and set errno.
648 pfkey_send_get(int so
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int32_t spi
)
651 if ((len
= pfkey_send_x2(so
, SADB_GET
, satype
, mode
, src
, dst
, spi
)) < 0)
658 * sending SADB_REGISTER message to the kernel.
660 * positive: success and return length sent.
661 * -1 : error occured, and set errno.
664 pfkey_send_register(int so
, u_int satype
)
668 if (satype
== PF_UNSPEC
) {
670 algno
< sizeof(supported_map
)/sizeof(supported_map
[0]);
672 if (ipsec_supported
[algno
]) {
673 free(ipsec_supported
[algno
]);
674 ipsec_supported
[algno
] = NULL
;
678 algno
= findsupportedmap((int)satype
);
680 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
684 if (ipsec_supported
[algno
]) {
685 free(ipsec_supported
[algno
]);
686 ipsec_supported
[algno
] = NULL
;
690 if ((len
= pfkey_send_x3(so
, SADB_REGISTER
, satype
)) < 0)
697 * receiving SADB_REGISTER message from the kernel, and copy buffer for
698 * sadb_supported returned into ipsec_supported.
700 * 0: success and return length sent.
701 * -1: error occured, and set errno.
704 pfkey_recv_register(int so
)
706 pid_t pid
= getpid();
707 struct sadb_msg
*newmsg
;
710 /* receive message */
712 if ((newmsg
= pfkey_recv(so
)) == NULL
)
714 if (newmsg
->sadb_msg_type
== SADB_REGISTER
&&
715 newmsg
->sadb_msg_pid
== pid
)
721 newmsg
->sadb_msg_len
= PFKEY_UNUNIT64(newmsg
->sadb_msg_len
);
723 error
= pfkey_set_supported(newmsg
, newmsg
->sadb_msg_len
);
727 __ipsec_errcode
= EIPSEC_NO_ERROR
;
733 * receiving SADB_REGISTER message from the kernel, and copy buffer for
734 * sadb_supported returned into ipsec_supported.
735 * NOTE: sadb_msg_len must be host order.
737 * tlen: msg length, it's to makeing sure.
739 * 0: success and return length sent.
740 * -1: error occured, and set errno.
743 pfkey_set_supported(struct sadb_msg
*msg
, int tlen
)
745 struct sadb_supported
*sup
;
750 if (msg
->sadb_msg_len
!= tlen
) {
751 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
758 p
+= sizeof(struct sadb_msg
);
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(int so
, u_int satype
)
809 if ((len
= pfkey_send_x3(so
, SADB_FLUSH
, satype
)) < 0)
816 * sending SADB_DUMP message to the kernel.
818 * positive: success and return length sent.
819 * -1 : error occured, and set errno.
822 pfkey_send_dump(int so
, u_int satype
)
826 if ((len
= pfkey_send_x3(so
, SADB_DUMP
, satype
)) < 0)
833 * sending SADB_X_PROMISC message to the kernel.
834 * NOTE that this function handles promisc mode toggle only.
836 * flag: set promisc off if zero, set promisc on if non-zero.
838 * positive: success and return length sent.
839 * -1 : error occured, and set errno.
840 * 0 : error occured, and set errno.
841 * others: a pointer to new allocated buffer in which supported
845 pfkey_send_promisc_toggle(int so
, int flag
)
849 if ((len
= pfkey_send_x3(so
, SADB_X_PROMISC
,
850 (u_int
)(flag
? 1 : 0))) < 0)
857 * sending SADB_X_SPDADD message to the kernel.
859 * positive: success and return length sent.
860 * -1 : error occured, and set errno.
863 pfkey_send_spdadd(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
,
864 u_int prefd
, u_int proto
, caddr_t policy
, int policylen
, u_int32_t seq
)
868 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
869 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
870 (u_int64_t
)0, (u_int64_t
)0,
871 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 0)
878 * sending SADB_X_SPDADD message to the kernel.
880 * positive: success and return length sent.
881 * -1 : error occured, and set errno.
884 pfkey_send_spdadd_with_interface(int so
, struct sockaddr_storage
*src
, struct sockaddr_storage
*src_end
, u_int prefs
, struct sockaddr_storage
*dst
,
885 struct sockaddr_storage
*dst_end
, u_int prefd
, u_int proto
, caddr_t policy
, int policylen
, u_int32_t seq
, char *ipsec_if
,
886 char *internal_if
, char *outgoing_if
, u_int disabled
)
890 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
891 src
, src_end
, prefs
, dst
, dst_end
, prefd
, proto
,
892 (u_int64_t
)0, (u_int64_t
)0,
893 policy
, policylen
, seq
, ipsec_if
, internal_if
, outgoing_if
, disabled
)) < 0)
900 * sending SADB_X_SPDADD message to the kernel.
902 * positive: success and return length sent.
903 * -1 : error occured, and set errno.
906 pfkey_send_spdadd2(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
, u_int prefd
, u_int proto
, u_int64_t ltime
, u_int64_t vtime
,
907 caddr_t policy
, int policylen
, u_int32_t seq
)
911 if ((len
= pfkey_send_x4(so
, SADB_X_SPDADD
,
912 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
914 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 0)
921 * sending SADB_X_SPDUPDATE message to the kernel.
923 * positive: success and return length sent.
924 * -1 : error occured, and set errno.
927 pfkey_send_spdupdate(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
,
928 u_int prefd
, u_int proto
, caddr_t policy
, int policylen
, u_int32_t seq
)
932 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
933 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
934 (u_int64_t
)0, (u_int64_t
)0,
935 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 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(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
,
949 u_int prefd
, u_int proto
, u_int64_t ltime
, u_int64_t vtime
,
950 caddr_t policy
, int policylen
, u_int32_t seq
)
954 if ((len
= pfkey_send_x4(so
, SADB_X_SPDUPDATE
,
955 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
957 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 0)
964 * sending SADB_X_SPDDELETE message to the kernel.
966 * positive: success and return length sent.
967 * -1 : error occured, and set errno.
970 pfkey_send_spddelete(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
,
971 u_int prefd
, u_int proto
, caddr_t policy
, int policylen
, u_int32_t seq
)
975 if (policylen
!= sizeof(struct sadb_x_policy
)) {
976 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
980 if ((len
= pfkey_send_x4(so
, SADB_X_SPDDELETE
,
981 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
982 (u_int64_t
)0, (u_int64_t
)0,
983 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 0)
990 * sending SADB_X_SPDDELETE message to the kernel.
992 * positive: success and return length sent.
993 * -1 : error occured, and set errno.
996 pfkey_send_spddelete2(int so
, u_int32_t spid
)
1000 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDELETE2
, spid
)) < 0)
1007 pfkey_send_spdenable(int so
, u_int32_t spid
)
1011 if ((len
= pfkey_send_x5(so
, SADB_X_SPDENABLE
, spid
)) < 0)
1018 pfkey_send_spddisable(int so
, u_int32_t spid
)
1022 if ((len
= pfkey_send_x5(so
, SADB_X_SPDDISABLE
, spid
)) < 0)
1029 * sending SADB_X_SPDGET message to the kernel.
1031 * positive: success and return length sent.
1032 * -1 : error occured, and set errno.
1035 pfkey_send_spdget(int so
, u_int32_t spid
)
1039 if ((len
= pfkey_send_x5(so
, SADB_X_SPDGET
, spid
)) < 0)
1046 * sending SADB_X_SPDSETIDX message to the kernel.
1048 * positive: success and return length sent.
1049 * -1 : error occured, and set errno.
1052 pfkey_send_spdsetidx(int so
, struct sockaddr_storage
*src
, u_int prefs
, struct sockaddr_storage
*dst
,
1053 u_int prefd
, u_int proto
, caddr_t policy
, int policylen
, u_int32_t seq
)
1057 if (policylen
!= sizeof(struct sadb_x_policy
)) {
1058 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1062 if ((len
= pfkey_send_x4(so
, SADB_X_SPDSETIDX
,
1063 src
, NULL
, prefs
, dst
, NULL
, prefd
, proto
,
1064 (u_int64_t
)0, (u_int64_t
)0,
1065 policy
, policylen
, seq
, NULL
, NULL
, NULL
, 0)) < 0)
1072 * sending SADB_SPDFLUSH message to the kernel.
1074 * positive: success and return length sent.
1075 * -1 : error occured, and set errno.
1078 pfkey_send_spdflush(int so
)
1082 if ((len
= pfkey_send_x3(so
, SADB_X_SPDFLUSH
, SADB_SATYPE_UNSPEC
)) < 0)
1089 * sending SADB_SPDDUMP message to the kernel.
1091 * positive: success and return length sent.
1092 * -1 : error occured, and set errno.
1095 pfkey_send_spddump(int so
)
1099 if ((len
= pfkey_send_x3(so
, SADB_X_SPDDUMP
, SADB_SATYPE_UNSPEC
)) < 0)
1106 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1108 pfkey_send_x1(int so
, u_int type
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
,
1109 struct sockaddr_storage
*dst
, u_int32_t spi
, u_int32_t reqid
, u_int wsize
,
1110 caddr_t keymat
, u_int e_type
, u_int e_keylen
, u_int a_type
, u_int a_keylen
, u_int flags
,
1111 u_int32_t l_alloc
, u_int32_t l_bytes
, u_int32_t l_addtime
, u_int32_t l_usetime
, u_int32_t seq
, u_int16_t port
,
1112 u_int always_expire
)
1114 struct sadb_msg
*newmsg
= NULL
;
1115 struct ifaddrs
*ifap
= NULL
;
1116 struct ifaddrs
*ifa
= NULL
;
1123 /* validity check */
1124 if (src
== NULL
|| dst
== NULL
) {
1125 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1128 if (src
->ss_family
!= dst
->ss_family
) {
1129 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1132 switch (src
->ss_family
) {
1134 plen
= sizeof(struct in_addr
) << 3;
1137 plen
= sizeof(struct in6_addr
) << 3;
1140 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1145 case SADB_SATYPE_ESP
:
1146 if (e_type
== SADB_EALG_NONE
) {
1147 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1151 case SADB_SATYPE_AH
:
1152 if (e_type
!= SADB_EALG_NONE
) {
1153 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1156 if (a_type
== SADB_AALG_NONE
) {
1157 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1161 case SADB_X_SATYPE_IPCOMP
:
1162 if (e_type
== SADB_X_CALG_NONE
) {
1163 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1166 if (a_type
!= SADB_AALG_NONE
) {
1167 __ipsec_errcode
= EIPSEC_NO_ALGS
;
1171 #ifdef SADB_X_AALG_TCP_MD5
1172 case SADB_X_SATYPE_TCPSIGNATURE
:
1173 if (e_type
!= SADB_EALG_NONE
) {
1174 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1177 if (a_type
!= SADB_X_AALG_TCP_MD5
) {
1178 __ipsec_errcode
= EIPSEC_INVAL_ALGS
;
1184 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1188 if (getifaddrs(&ifap
) < 0) {
1192 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
1193 if (ifa
->ifa_addr
== NULL
|| ifa
->ifa_addr
->sa_family
!= src
->ss_family
)
1196 if (src
->ss_family
== AF_INET
) {
1197 if (memcmp(&((struct sockaddr_in
*)(ifa
->ifa_addr
))->sin_addr
, &((struct sockaddr_in
*)src
)->sin_addr
, sizeof(struct in_addr
)) != 0)
1199 } else if (src
->ss_family
== AF_INET6
) {
1200 if (memcmp(&((struct sockaddr_in6
*)(ifa
->ifa_addr
))->sin6_addr
, &((struct sockaddr_in6
*)src
)->sin6_addr
, sizeof(struct in6_addr
)) != 0)
1207 /* create new sadb_msg to reply. */
1208 len
= sizeof(struct sadb_msg
)
1209 + sizeof(struct sadb_sa_2
)
1210 + sizeof(struct sadb_x_sa2
)
1211 + sizeof(struct sadb_address
)
1212 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
1213 + sizeof(struct sadb_address
)
1214 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)dst
))
1215 + sizeof(struct sadb_lifetime
)
1216 + sizeof(struct sadb_lifetime
)
1217 + ((ifa
!= NULL
&& ifa
->ifa_name
!= NULL
) ? sizeof(struct sadb_x_ipsecif
) : 0);
1219 if (e_type
!= SADB_EALG_NONE
&& satype
!= SADB_X_SATYPE_IPCOMP
)
1220 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(e_keylen
));
1221 if (a_type
!= SADB_AALG_NONE
)
1222 len
+= (sizeof(struct sadb_key
) + PFKEY_ALIGN8(a_keylen
));
1224 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
1225 __ipsec_set_strerror(strerror(errno
));
1228 ep
= ((caddr_t
)(void *)newmsg
) + len
;
1230 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, type
, (u_int
)len
,
1231 satype
, seq
, getpid());
1237 p
= pfkey_setsadbsa(p
, ep
, spi
, wsize
, a_type
, e_type
, flags
, port
);
1241 p
= pfkey_setsadbxsa2(p
, ep
, mode
, reqid
, always_expire
);
1245 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, (u_int
)plen
,
1250 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, (u_int
)plen
,
1256 if (ifa
!= NULL
&& ifa
->ifa_name
!= NULL
) {
1257 p
= pfkey_setsadbipsecif(p
, ep
, NULL
, ifa
->ifa_name
, NULL
, 0);
1263 if (e_type
!= SADB_EALG_NONE
&& satype
!= SADB_X_SATYPE_IPCOMP
) {
1264 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_ENCRYPT
,
1270 if (a_type
!= SADB_AALG_NONE
) {
1271 p
= pfkey_setsadbkey(p
, ep
, SADB_EXT_KEY_AUTH
,
1272 keymat
+ e_keylen
, a_keylen
);
1278 /* set sadb_lifetime for destination */
1279 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1280 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1284 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_SOFT
,
1285 l_alloc
, l_bytes
, l_addtime
, l_usetime
);
1295 len
= pfkey_send(so
, newmsg
, len
);
1300 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1308 if (newmsg
!= NULL
) {
1316 /* sending SADB_DELETE or SADB_GET message to the kernel */
1319 pfkey_send_x2(int so
, u_int type
, u_int satype
, u_int mode
, struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int32_t spi
)
1321 struct sadb_msg
*newmsg
;
1327 /* validity check */
1328 if (src
== NULL
|| dst
== NULL
) {
1329 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1332 if (src
->ss_family
!= dst
->ss_family
) {
1333 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1336 switch (src
->ss_family
) {
1338 plen
= sizeof(struct in_addr
) << 3;
1341 plen
= sizeof(struct in6_addr
) << 3;
1344 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1348 /* create new sadb_msg to reply. */
1349 len
= sizeof(struct sadb_msg
)
1350 + sizeof(struct sadb_sa_2
)
1351 + sizeof(struct sadb_address
)
1352 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
1353 + sizeof(struct sadb_address
)
1354 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)dst
));
1356 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
1357 __ipsec_set_strerror(strerror(errno
));
1360 ep
= ((caddr_t
)(void *)newmsg
) + len
;
1362 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, type
, (u_int
)len
, satype
, 0,
1368 p
= pfkey_setsadbsa(p
, ep
, spi
, 0, 0, 0, 0, 0);
1373 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, (u_int
)plen
,
1379 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, (u_int
)plen
,
1381 if (!p
|| p
!= ep
) {
1387 len
= pfkey_send(so
, newmsg
, len
);
1393 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1398 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1402 pfkey_send_x3(int so
, u_int type
, u_int satype
)
1404 struct sadb_msg
*newmsg
;
1409 /* validity check */
1411 case SADB_X_PROMISC
:
1412 if (satype
!= 0 && satype
!= 1) {
1413 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1419 case SADB_SATYPE_UNSPEC
:
1420 case SADB_SATYPE_AH
:
1421 case SADB_SATYPE_ESP
:
1422 case SADB_X_SATYPE_IPCOMP
:
1423 #ifdef SADB_X_SATYPE_TCPSIGNATURE
1424 case SADB_X_SATYPE_TCPSIGNATURE
:
1428 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1433 /* create new sadb_msg to send. */
1434 len
= sizeof(struct sadb_msg
);
1436 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
1437 __ipsec_set_strerror(strerror(errno
));
1440 ep
= ((caddr_t
)(void *)newmsg
) + len
;
1442 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, type
, (u_int
)len
, satype
, 0,
1444 if (!p
|| p
!= ep
) {
1450 len
= pfkey_send(so
, newmsg
, len
);
1456 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1460 /* sending SADB_X_SPDADD message to the kernel */
1462 pfkey_send_x4(int so
, u_int type
, struct sockaddr_storage
*src
, struct sockaddr_storage
*src_end
, u_int prefs
, struct sockaddr_storage
*dst
, struct sockaddr_storage
*dst_end
,
1463 u_int prefd
, u_int proto
, u_int64_t ltime
, u_int64_t vtime
, char *policy
, int policylen
, u_int32_t seq
, char *ipsec_if
, char *internal_if
, char *outgoing_if
,
1466 struct sadb_msg
*newmsg
;
1471 int include_ipsec_if_msg
= 0;
1473 /* validity check */
1474 if (src
== NULL
|| dst
== NULL
) {
1475 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1478 if (src
->ss_family
!= dst
->ss_family
) {
1479 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1483 switch (src
->ss_family
) {
1485 plen
= sizeof(struct in_addr
) << 3;
1488 plen
= sizeof(struct in6_addr
) << 3;
1491 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
1494 if (prefs
> plen
|| prefd
> plen
) {
1495 __ipsec_errcode
= EIPSEC_INVAL_PREFIXLEN
;
1499 if (ipsec_if
|| internal_if
|| outgoing_if
|| disabled
) {
1500 include_ipsec_if_msg
= 1;
1503 /* create new sadb_msg to reply. */
1504 len
= sizeof(struct sadb_msg
)
1505 + sizeof(struct sadb_address
)
1506 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
1507 + ((src_end
) ? sizeof(struct sadb_address
) + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src_end
)) : 0)
1508 + sizeof(struct sadb_address
)
1509 + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)src
))
1510 + ((dst_end
) ? sizeof(struct sadb_address
) + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)dst_end
)) : 0)
1511 + ((include_ipsec_if_msg
) ? sizeof(struct sadb_x_ipsecif
) : 0)
1512 + sizeof(struct sadb_lifetime
)
1515 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
1516 __ipsec_set_strerror(strerror(errno
));
1519 ep
= ((caddr_t
)(void *)newmsg
) + len
;
1521 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, type
, (u_int
)len
,
1522 SADB_SATYPE_UNSPEC
, seq
, getpid());
1528 p
= pfkey_setsadbaddr(p
, ep
, SADB_X_EXT_ADDR_RANGE_SRC_START
, src
, prefs
, proto
);
1533 p
= pfkey_setsadbaddr(p
, ep
, SADB_X_EXT_ADDR_RANGE_SRC_END
, src_end
, prefs
, proto
);
1539 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_SRC
, src
, prefs
, proto
);
1546 p
= pfkey_setsadbaddr(p
, ep
, SADB_X_EXT_ADDR_RANGE_DST_START
, dst
, prefd
, proto
);
1551 p
= pfkey_setsadbaddr(p
, ep
, SADB_X_EXT_ADDR_RANGE_DST_END
, dst_end
, prefd
, proto
);
1557 p
= pfkey_setsadbaddr(p
, ep
, SADB_EXT_ADDRESS_DST
, dst
, prefd
, proto
);
1563 if (include_ipsec_if_msg
) {
1564 p
= pfkey_setsadbipsecif(p
, ep
, internal_if
, outgoing_if
, ipsec_if
, disabled
);
1570 p
= pfkey_setsadblifetime(p
, ep
, SADB_EXT_LIFETIME_HARD
,
1571 0, 0, (u_int
)ltime
, (u_int
)vtime
);
1572 if (!p
|| p
+ policylen
!= ep
) {
1576 memcpy(p
, policy
, (size_t)policylen
);
1579 len
= pfkey_send(so
, newmsg
, len
);
1585 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1589 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1591 pfkey_send_x5(int so
, u_int type
, u_int32_t spid
)
1593 struct sadb_msg
*newmsg
;
1594 struct sadb_x_policy xpl
;
1599 /* create new sadb_msg to reply. */
1600 len
= sizeof(struct sadb_msg
)
1603 if ((newmsg
= CALLOC((size_t)len
, struct sadb_msg
*)) == NULL
) {
1604 __ipsec_set_strerror(strerror(errno
));
1607 ep
= ((caddr_t
)(void *)newmsg
) + len
;
1609 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, type
, (u_int
)len
,
1610 SADB_SATYPE_UNSPEC
, 0, getpid());
1616 if (p
+ sizeof(xpl
) != ep
) {
1620 memset(&xpl
, 0, sizeof(xpl
));
1621 xpl
.sadb_x_policy_len
= PFKEY_UNIT64(sizeof(xpl
));
1622 xpl
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1623 xpl
.sadb_x_policy_id
= spid
;
1624 memcpy(p
, &xpl
, sizeof(xpl
));
1627 len
= pfkey_send(so
, newmsg
, len
);
1633 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1641 * others : success and return value of socket.
1647 int bufsiz
= 0; /* Max allowed by default */
1648 const unsigned long newbufk
= 2176;
1649 unsigned long oldmax
;
1650 size_t oldmaxsize
= sizeof(oldmax
);
1651 unsigned long newmax
= newbufk
* (1024 + 128);
1653 if ((so
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
)) < 0) {
1654 __ipsec_set_strerror(strerror(errno
));
1659 * This is a temporary workaround for KAME PR 154.
1660 * Don't really care even if it fails.
1662 if (sysctlbyname("kern.ipc.maxsockbuf", &oldmax
, &oldmaxsize
, &newmax
, sizeof(newmax
)) != 0)
1663 bufsiz
= 233016; /* Max allowed by default */
1665 bufsiz
= newbufk
* 1024;
1667 setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &bufsiz
, sizeof(bufsiz
));
1668 setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &bufsiz
, sizeof(bufsiz
));
1670 if (bufsiz
== newbufk
* 1024)
1671 sysctlbyname("kern.ipc.maxsockbuf", NULL
, NULL
, &oldmax
, oldmaxsize
);
1673 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1684 pfkey_close_sock(int so
)
1688 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1693 * receive sadb_msg data, and return pointer to new buffer allocated.
1694 * Must free this buffer later.
1696 * NULL : error occured.
1697 * others : a pointer to sadb_msg structure.
1699 * XXX should be rewritten to pass length explicitly
1704 struct sadb_msg buf
, *newmsg
;
1708 while ((len
= recv(so
, (void *)&buf
, sizeof(buf
), MSG_PEEK
)) < 0) {
1711 __ipsec_set_strerror(strerror(errno
));
1715 if (len
< sizeof(buf
)) {
1716 recv(so
, (void *)&buf
, sizeof(buf
), 0);
1717 __ipsec_errcode
= EIPSEC_MAX
;
1721 /* read real message */
1722 reallen
= PFKEY_UNUNIT64(buf
.sadb_msg_len
);
1723 if ((newmsg
= CALLOC((size_t)reallen
, struct sadb_msg
*)) == 0) {
1724 __ipsec_set_strerror(strerror(errno
));
1728 while ((len
= recv(so
, (void *)newmsg
, (socklen_t
)reallen
, 0)) < 0) {
1731 __ipsec_set_strerror(strerror(errno
));
1736 if (len
!= reallen
) {
1737 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1742 /* don't trust what the kernel says, validate! */
1743 if (PFKEY_UNUNIT64(newmsg
->sadb_msg_len
) != len
) {
1744 __ipsec_errcode
= EIPSEC_SYSTEM_ERROR
;
1749 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1754 * send message to a socket.
1756 * others: success and return length sent.
1760 pfkey_send(int so
, struct sadb_msg
*msg
, int len
)
1762 if ((len
= send(so
, (void *)msg
, (socklen_t
)len
, 0)) < 0) {
1763 __ipsec_set_strerror(strerror(errno
));
1767 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1773 * NOTE: These functions are derived from netkey/key.c in KAME.
1776 * set the pointer to each header in this message buffer.
1777 * IN: msg: pointer to message buffer.
1778 * mhp: pointer to the buffer initialized like below:
1779 * caddr_t mhp[SADB_EXT_MAX + 1];
1783 * XXX should be rewritten to obtain length explicitly
1786 pfkey_align(struct sadb_msg
*msg
, caddr_t
*mhp
)
1788 struct sadb_ext
*ext
;
1791 caddr_t ep
; /* XXX should be passed from upper layer */
1793 /* validity check */
1794 if (msg
== NULL
|| mhp
== NULL
) {
1795 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1800 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
1803 mhp
[0] = (void *)msg
;
1807 ep
= p
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
);
1809 /* skip base header */
1810 p
+= sizeof(struct sadb_msg
);
1814 if (ep
< p
+ sizeof(*ext
) || PFKEY_EXTLEN(ext
) < sizeof(*ext
) ||
1815 ep
< p
+ PFKEY_EXTLEN(ext
)) {
1816 /* invalid format */
1820 /* duplicate check */
1821 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1822 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
1823 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1828 switch (ext
->sadb_ext_type
) {
1830 case SADB_EXT_LIFETIME_CURRENT
:
1831 case SADB_EXT_LIFETIME_HARD
:
1832 case SADB_EXT_LIFETIME_SOFT
:
1833 case SADB_EXT_ADDRESS_SRC
:
1834 case SADB_EXT_ADDRESS_DST
:
1835 case SADB_EXT_ADDRESS_PROXY
:
1836 case SADB_EXT_KEY_AUTH
:
1837 /* XXX should to be check weak keys. */
1838 case SADB_EXT_KEY_ENCRYPT
:
1839 /* XXX should to be check weak keys. */
1840 case SADB_EXT_IDENTITY_SRC
:
1841 case SADB_EXT_IDENTITY_DST
:
1842 case SADB_EXT_SENSITIVITY
:
1843 case SADB_EXT_PROPOSAL
:
1844 case SADB_EXT_SUPPORTED_AUTH
:
1845 case SADB_EXT_SUPPORTED_ENCRYPT
:
1846 case SADB_EXT_SPIRANGE
:
1847 case SADB_X_EXT_POLICY
:
1848 case SADB_X_EXT_SA2
:
1849 case SADB_EXT_SESSION_ID
:
1850 case SADB_EXT_SASTAT
:
1851 #ifdef SADB_X_EXT_NAT_T_TYPE
1852 case SADB_X_EXT_NAT_T_TYPE
:
1853 case SADB_X_EXT_NAT_T_SPORT
:
1854 case SADB_X_EXT_NAT_T_DPORT
:
1855 case SADB_X_EXT_NAT_T_OA
:
1857 #ifdef SADB_X_EXT_TAG
1858 case SADB_X_EXT_TAG
:
1860 #ifdef SADB_X_EXT_PACKET
1861 case SADB_X_EXT_PACKET
:
1863 case SADB_X_EXT_IPSECIF
:
1864 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
1865 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
1866 case SADB_X_EXT_ADDR_RANGE_DST_START
:
1867 case SADB_X_EXT_ADDR_RANGE_DST_END
:
1868 mhp
[ext
->sadb_ext_type
] = (void *)ext
;
1871 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
1875 p
+= PFKEY_EXTLEN(ext
);
1879 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
1883 __ipsec_errcode
= EIPSEC_NO_ERROR
;
1888 * check basic usage for sadb_msg,
1889 * NOTE: This routine is derived from netkey/key.c in KAME.
1890 * IN: msg: pointer to message buffer.
1891 * mhp: pointer to the buffer initialized like below:
1893 * caddr_t mhp[SADB_EXT_MAX + 1];
1899 pfkey_check(caddr_t
* mhp
)
1901 struct sadb_msg
*msg
;
1903 /* validity check */
1904 if (mhp
== NULL
|| mhp
[0] == NULL
) {
1905 __ipsec_errcode
= EIPSEC_INVAL_ARGUMENT
;
1909 msg
= (void *)mhp
[0];
1912 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
1913 __ipsec_errcode
= EIPSEC_INVAL_VERSION
;
1918 if (msg
->sadb_msg_type
> SADB_MAX
) {
1919 __ipsec_errcode
= EIPSEC_INVAL_MSGTYPE
;
1924 switch (msg
->sadb_msg_satype
) {
1925 case SADB_SATYPE_UNSPEC
:
1926 switch (msg
->sadb_msg_type
) {
1934 #ifdef SADB_X_NAT_T_NEW_MAPPING
1935 case SADB_X_NAT_T_NEW_MAPPING
:
1937 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1941 case SADB_SATYPE_ESP
:
1942 case SADB_SATYPE_AH
:
1943 case SADB_X_SATYPE_IPCOMP
:
1944 #ifdef SADB_X_SATYPE_TCPSIGNATURE
1945 case SADB_X_SATYPE_TCPSIGNATURE
:
1947 switch (msg
->sadb_msg_type
) {
1949 case SADB_X_SPDDELETE
:
1951 case SADB_X_SPDDUMP
:
1952 case SADB_X_SPDFLUSH
:
1953 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1956 #ifdef SADB_X_NAT_T_NEW_MAPPING
1957 if (msg
->sadb_msg_type
== SADB_X_NAT_T_NEW_MAPPING
&&
1958 msg
->sadb_msg_satype
!= SADB_SATYPE_ESP
) {
1959 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1964 case SADB_SATYPE_RSVP
:
1965 case SADB_SATYPE_OSPFV2
:
1966 case SADB_SATYPE_RIPV2
:
1967 case SADB_SATYPE_MIP
:
1968 __ipsec_errcode
= EIPSEC_NOT_SUPPORTED
;
1970 case 1: /* XXX: What does it do ? */
1971 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
1975 __ipsec_errcode
= EIPSEC_INVAL_SATYPE
;
1979 /* check field of upper layer protocol and address family */
1980 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
1981 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
1982 struct sadb_address
*src0
, *dst0
;
1984 src0
= (void *)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1985 dst0
= (void *)(mhp
[SADB_EXT_ADDRESS_DST
]);
1987 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
1988 __ipsec_errcode
= EIPSEC_PROTO_MISMATCH
;
1992 if (PFKEY_ADDR_SADDR(src0
)->sa_family
1993 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
1994 __ipsec_errcode
= EIPSEC_FAMILY_MISMATCH
;
1998 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
2003 __ipsec_errcode
= EIPSEC_INVAL_FAMILY
;
2008 * prefixlen == 0 is valid because there must be the case
2009 * all addresses are matched.
2013 __ipsec_errcode
= EIPSEC_NO_ERROR
;
2018 * set data into sadb_msg.
2019 * `buf' must has been allocated sufficiently.
2022 pfkey_setsadbmsg(caddr_t buf
, caddr_t lim
, u_int type
, u_int tlen
, u_int satype
, u_int32_t seq
, pid_t pid
)
2028 len
= sizeof(struct sadb_msg
);
2030 if (buf
+ len
> lim
)
2034 p
->sadb_msg_version
= PF_KEY_V2
;
2035 p
->sadb_msg_type
= type
;
2036 p
->sadb_msg_errno
= 0;
2037 p
->sadb_msg_satype
= satype
;
2038 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
2039 p
->sadb_msg_reserved
= 0;
2040 p
->sadb_msg_seq
= seq
;
2041 p
->sadb_msg_pid
= (u_int32_t
)pid
;
2047 * copy secasvar data into sadb_address.
2048 * `buf' must has been allocated sufficiently.
2051 pfkey_setsadbsa(caddr_t buf
, caddr_t lim
, u_int32_t spi
, u_int wsize
, u_int auth
, u_int enc
, u_int32_t flags
, u_int16_t port
)
2053 struct sadb_sa_2
*p
;
2057 len
= sizeof(struct sadb_sa_2
);
2059 if (buf
+ len
> lim
)
2063 p
->sa
.sadb_sa_len
= PFKEY_UNIT64(len
);
2064 p
->sa
.sadb_sa_exttype
= SADB_EXT_SA
;
2065 p
->sa
.sadb_sa_spi
= spi
;
2066 p
->sa
.sadb_sa_replay
= wsize
;
2067 p
->sa
.sadb_sa_state
= SADB_SASTATE_LARVAL
;
2068 p
->sa
.sadb_sa_auth
= auth
;
2069 p
->sa
.sadb_sa_encrypt
= enc
;
2070 p
->sa
.sadb_sa_flags
= flags
;
2071 p
->sadb_sa_natt_port
= port
;
2077 * set data into sadb_address.
2078 * `buf' must has been allocated sufficiently.
2079 * prefixlen is in bits.
2082 pfkey_setsadbaddr(caddr_t buf
, caddr_t lim
, u_int exttype
, struct sockaddr_storage
*saddr
, u_int prefixlen
, u_int ul_proto
)
2084 struct sadb_address
*p
;
2088 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(sysdep_sa_len((struct sockaddr
*)saddr
));
2090 if (buf
+ len
> lim
)
2094 p
->sadb_address_len
= PFKEY_UNIT64(len
);
2095 p
->sadb_address_exttype
= exttype
& 0xffff;
2096 p
->sadb_address_proto
= ul_proto
& 0xff;
2097 p
->sadb_address_prefixlen
= prefixlen
;
2098 p
->sadb_address_reserved
= 0;
2100 memcpy(p
+ 1, saddr
, (size_t)sysdep_sa_len((struct sockaddr
*)saddr
));
2106 * set sadb_key structure after clearing buffer with zero.
2107 * OUT: the pointer of buf + len.
2110 pfkey_setsadbkey(caddr_t buf
, caddr_t lim
, u_int type
, caddr_t key
, u_int keylen
)
2116 len
= sizeof(struct sadb_key
) + PFKEY_ALIGN8(keylen
);
2118 if (buf
+ len
> lim
)
2122 p
->sadb_key_len
= PFKEY_UNIT64(len
);
2123 p
->sadb_key_exttype
= type
;
2124 p
->sadb_key_bits
= keylen
<< 3;
2125 p
->sadb_key_reserved
= 0;
2127 memcpy(p
+ 1, key
, keylen
);
2133 * set sadb_lifetime structure after clearing buffer with zero.
2134 * OUT: the pointer of buf + len.
2137 pfkey_setsadblifetime(caddr_t buf
, caddr_t lim
, u_int type
, u_int32_t l_alloc
,
2138 u_int32_t l_bytes
, u_int32_t l_addtime
, u_int32_t l_usetime
)
2140 struct sadb_lifetime
*p
;
2144 len
= sizeof(struct sadb_lifetime
);
2146 if (buf
+ len
> lim
)
2150 p
->sadb_lifetime_len
= PFKEY_UNIT64(len
);
2151 p
->sadb_lifetime_exttype
= type
;
2154 case SADB_EXT_LIFETIME_SOFT
:
2155 p
->sadb_lifetime_allocations
2156 = (l_alloc
* soft_lifetime_allocations_rate
) /100;
2157 p
->sadb_lifetime_bytes
2158 = (l_bytes
* soft_lifetime_bytes_rate
) /100;
2159 p
->sadb_lifetime_addtime
2160 = (l_addtime
* soft_lifetime_addtime_rate
) /100;
2161 p
->sadb_lifetime_usetime
2162 = (l_usetime
* soft_lifetime_usetime_rate
) /100;
2164 case SADB_EXT_LIFETIME_HARD
:
2165 p
->sadb_lifetime_allocations
= l_alloc
;
2166 p
->sadb_lifetime_bytes
= l_bytes
;
2167 p
->sadb_lifetime_addtime
= l_addtime
;
2168 p
->sadb_lifetime_usetime
= l_usetime
;
2176 pfkey_setsadbipsecif(caddr_t buf
, caddr_t lim
, char *internal_if
, char *outgoing_if
, char *ipsec_if
, int init_disabled
)
2178 struct sadb_x_ipsecif
*p
;
2182 len
= sizeof(struct sadb_x_ipsecif
);
2184 if (buf
+ len
> lim
)
2188 p
->sadb_x_ipsecif_len
= PFKEY_UNIT64(len
);
2189 p
->sadb_x_ipsecif_exttype
= SADB_X_EXT_IPSECIF
;
2191 if (internal_if
!= NULL
)
2192 strncpy(p
->sadb_x_ipsecif_internal_if
, internal_if
, sizeof(p
->sadb_x_ipsecif_internal_if
) - 1);
2193 if (outgoing_if
!= NULL
)
2194 strncpy(p
->sadb_x_ipsecif_outgoing_if
, outgoing_if
, sizeof(p
->sadb_x_ipsecif_outgoing_if
) - 1);
2195 if (ipsec_if
!= NULL
)
2196 strncpy(p
->sadb_x_ipsecif_ipsec_if
, ipsec_if
, sizeof(p
->sadb_x_ipsecif_ipsec_if
) - 1);
2198 p
->sadb_x_ipsecif_init_disabled
= init_disabled
;
2204 * copy secasvar data into sadb_address.
2205 * `buf' must has been allocated sufficiently.
2208 pfkey_setsadbxsa2(caddr_t buf
, caddr_t lim
, u_int32_t mode0
, u_int32_t reqid
, u_int always_expire
)
2210 struct sadb_x_sa2
*p
;
2211 u_int8_t mode
= mode0
& 0xff;
2215 len
= sizeof(struct sadb_x_sa2
);
2217 if (buf
+ len
> lim
)
2221 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
2222 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
2223 p
->sadb_x_sa2_mode
= mode
;
2224 p
->sadb_x_sa2_reqid
= reqid
;
2225 p
->sadb_x_sa2_alwaysexpire
= always_expire
;
2226 #ifdef SADB_X_EXT_SA2_DELETE_ON_DETACH
2227 p
->sadb_x_sa2_flags
|= SADB_X_EXT_SA2_DELETE_ON_DETACH
;
2228 #endif /* SADB_X_EXT_SA2_DELETE_ON_DETACH */
2233 #ifdef SADB_X_EXT_NAT_T_TYPE
2235 pfkey_set_natt_type(caddr_t buf
, caddr_t lim
, u_int type
, u_int8_t l_natt_type
)
2237 struct sadb_x_nat_t_type
*p
;
2241 len
= sizeof(struct sadb_x_nat_t_type
);
2243 if (buf
+ len
> lim
)
2247 p
->sadb_x_nat_t_type_len
= PFKEY_UNIT64(len
);
2248 p
->sadb_x_nat_t_type_exttype
= type
;
2249 p
->sadb_x_nat_t_type_type
= l_natt_type
;
2255 pfkey_set_natt_port(caddr_t buf
, caddr_t lim
, u_int type
, u_int16_t l_natt_port
)
2257 struct sadb_x_nat_t_port
*p
;
2261 len
= sizeof(struct sadb_x_nat_t_port
);
2263 if (buf
+ len
> lim
)
2267 p
->sadb_x_nat_t_port_len
= PFKEY_UNIT64(len
);
2268 p
->sadb_x_nat_t_port_exttype
= type
;
2269 p
->sadb_x_nat_t_port_port
= htons(l_natt_port
);
2275 #ifdef SADB_X_EXT_NAT_T_FRAG
2277 pfkey_set_natt_frag(caddr_t buf
, caddr_t lim
, u_int type
, u_int16_t l_natt_frag
)
2279 struct sadb_x_nat_t_frag
*p
;
2283 len
= sizeof(struct sadb_x_nat_t_frag
);
2285 if (buf
+ len
> lim
)
2289 p
->sadb_x_nat_t_frag_len
= PFKEY_UNIT64(len
);
2290 p
->sadb_x_nat_t_frag_exttype
= type
;
2291 p
->sadb_x_nat_t_frag_fraglen
= l_natt_frag
;
2299 pfkey_setsadbsession_id (caddr_t buf
,
2301 u_int64_t session_ids
[],
2302 u_int32_t max_session_ids
)
2304 struct sadb_session_id
*p
;
2307 if (!max_session_ids
)
2313 if (buf
+ len
> lim
)
2317 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
2318 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
2319 p
->sadb_session_id_v
[0] = session_ids
[0];
2320 if (max_session_ids
> 1)
2321 p
->sadb_session_id_v
[1] = session_ids
[1];
2327 pfkey_setsadbsastats (caddr_t buf
,
2330 struct sastat
*stats
,
2331 u_int32_t max_stats
)
2333 struct sadb_sastat
*p
;
2334 u_int len
, list_len
;
2336 if (!stats
|| !max_stats
)
2339 p
= ALIGNED_CAST(__typeof__(p
))buf
; // Wcast-align fix - buffer passed to here is malloc'd message buffer
2340 list_len
= sizeof(*stats
) * max_stats
;
2341 len
= sizeof(*p
) + PFKEY_ALIGN8(list_len
);
2343 if (buf
+ len
> lim
)
2347 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
2348 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
2349 p
->sadb_sastat_dir
= dir
;
2350 p
->sadb_sastat_list_len
= max_stats
;
2351 bcopy(stats
, p
+ 1, list_len
);
2357 pfkey_send_getsastats (int so
,
2359 u_int64_t
*session_ids
,
2360 u_int32_t max_session_ids
,
2362 struct sastat
*stats
,
2363 u_int32_t max_stats
)
2365 struct sadb_msg
*newmsg
;
2367 int list_len
, out_len
, len
;
2370 if (!session_ids
|| !stats
|| !max_stats
) {
2374 list_len
= sizeof(*stats
) * max_stats
;
2375 /* create new sadb_msg to send. */
2376 out_len
= sizeof(struct sadb_msg
) + sizeof(struct sadb_session_id
) + sizeof(struct sadb_sastat
) + PFKEY_ALIGN8(list_len
);
2378 if ((newmsg
= CALLOC((size_t)out_len
, struct sadb_msg
*)) == NULL
) {
2379 __ipsec_set_strerror(strerror(errno
));
2382 ep
= ((caddr_t
)(void *)newmsg
) + out_len
;
2384 p
= pfkey_setsadbmsg((void *)newmsg
, ep
, SADB_GETSASTAT
, (u_int
)out_len
, SADB_SATYPE_UNSPEC
, seq
, getpid());
2390 p
= pfkey_setsadbsession_id(p
, ep
, session_ids
, max_session_ids
);
2396 p
= pfkey_setsadbsastats(p
, ep
, dir
, stats
, max_stats
);
2403 len
= pfkey_send(so
, newmsg
, out_len
);
2409 __ipsec_errcode
= EIPSEC_NO_ERROR
;