1 /* $NetBSD: libpfkey.h,v 1.20 2018/05/28 20:45:38 maxv Exp $ */ 2 3 /* Id: libpfkey.h,v 1.13 2005/12/04 20:26:43 manubsd Exp */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef _LIBPFKEY_H 35 #define _LIBPFKEY_H 36 37 #ifndef KAME_LIBPFKEY_H 38 #define KAME_LIBPFKEY_H 39 40 #define PRIORITY_LOW 0xC0000000 41 #define PRIORITY_DEFAULT 0x80000000 42 #define PRIORITY_HIGH 0x40000000 43 44 #define PRIORITY_OFFSET_POSITIVE_MAX 0x3fffffff 45 #define PRIORITY_OFFSET_NEGATIVE_MAX 0x40000000 46 47 struct sadb_msg; 48 extern void pfkey_sadump(struct sadb_msg *); 49 extern void pfkey_sadump_withports(struct sadb_msg *); 50 extern void pfkey_spdump(struct sadb_msg *); 51 extern void pfkey_spdump_withports(struct sadb_msg *); 52 53 struct sockaddr; 54 struct sadb_alg; 55 56 /* Accomodate different prototypes in <netinet6/ipsec.h> */ 57 #include <sys/types.h> 58 #include PATH_IPSEC_H 59 60 #ifndef HAVE_IPSEC_POLICY_T 61 typedef caddr_t ipsec_policy_t; 62 #ifdef __NetBSD__ 63 #define __ipsec_const const 64 #else 65 #define __ipsec_const 66 #endif 67 #else 68 #define __ipsec_const const 69 #endif 70 71 struct pfkey_send_sa_args { 72 int so; /* socket */ 73 u_int type; 74 u_int satype; 75 u_int mode; 76 struct sockaddr *src; /* IP src address for SA */ 77 struct sockaddr *dst; /* IP dst address for SA */ 78 u_int32_t spi; /* SA's spi */ 79 u_int32_t reqid; 80 u_int wsize; 81 caddr_t keymat; 82 u_int e_type, e_keylen; /* Encryption alg and keylen */ 83 u_int a_type, a_keylen; /* Authentication alg and key */ 84 u_int flags; 85 u_int32_t l_alloc; 86 u_int32_t l_bytes; 87 u_int32_t l_addtime; 88 u_int32_t l_usetime; 89 u_int32_t seq; 90 u_int8_t l_natt_type; 91 u_int16_t l_natt_sport, l_natt_dport; 92 struct sockaddr *l_natt_oa; 93 u_int16_t l_natt_frag; 94 u_int8_t ctxdoi, ctxalg; /* Security context DOI and algorithm */ 95 caddr_t ctxstr; /* Security context string */ 96 u_int16_t ctxstrlen; /* length of security context string */ 97 }; 98 99 /* The options built into libipsec */ 100 extern int libipsec_opt; 101 #define LIBIPSEC_OPT_NATT 0x01 102 #define LIBIPSEC_OPT_FRAG 0x02 103 #define LIBIPSEC_OPT_SEC_CTX 0x04 104 105 /* IPsec Library Routines */ 106 107 int ipsec_check_keylen(u_int, u_int, u_int); 108 int ipsec_check_keylen2(u_int, u_int, u_int); 109 int ipsec_get_keylen(u_int, u_int, struct sadb_alg *); 110 char *ipsec_dump_policy_withports(void *, const char *); 111 void ipsec_hexdump(const void *, int); 112 const char *ipsec_strerror(void); 113 void kdebug_sadb(struct sadb_msg *); 114 ipsec_policy_t ipsec_set_policy(__ipsec_const char *, int); 115 int ipsec_get_policylen(ipsec_policy_t); 116 char *ipsec_dump_policy(ipsec_policy_t, __ipsec_const char *); 117 118 /* PFKey Routines */ 119 120 u_int pfkey_set_softrate(u_int, u_int); 121 u_int pfkey_get_softrate(u_int); 122 int pfkey_send_getspi(int, u_int, u_int, struct sockaddr *, 123 struct sockaddr *, u_int32_t, u_int32_t, u_int32_t, u_int32_t); 124 int pfkey_send_getspi_nat(int, u_int, u_int, 125 struct sockaddr *, struct sockaddr *, u_int8_t, u_int16_t, u_int16_t, 126 u_int32_t, u_int32_t, u_int32_t, u_int32_t); 127 128 int pfkey_send_update2(struct pfkey_send_sa_args *); 129 int pfkey_send_add2(struct pfkey_send_sa_args *); 130 int pfkey_send_delete(int, u_int, u_int, 131 struct sockaddr *, struct sockaddr *, u_int32_t); 132 int pfkey_send_delete_all(int, u_int, u_int, 133 struct sockaddr *, struct sockaddr *); 134 int pfkey_send_get(int, u_int, u_int, 135 struct sockaddr *, struct sockaddr *, u_int32_t); 136 int pfkey_send_register(int, u_int); 137 int pfkey_recv_register(int); 138 int pfkey_set_supported(struct sadb_msg *, int); 139 int pfkey_send_flush(int, u_int); 140 int pfkey_send_dump(int, u_int); 141 int pfkey_send_promisc_toggle(int, int); 142 int pfkey_send_spdadd(int, struct sockaddr *, u_int, 143 struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t); 144 int pfkey_send_spdadd2(int, struct sockaddr *, u_int, 145 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t, 146 caddr_t, int, u_int32_t); 147 int pfkey_send_spdupdate(int, struct sockaddr *, u_int, 148 struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t); 149 int pfkey_send_spdupdate2(int, struct sockaddr *, u_int, 150 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t, 151 caddr_t, int, u_int32_t); 152 int pfkey_send_spddelete(int, struct sockaddr *, u_int, 153 struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t); 154 int pfkey_send_spddelete2(int, u_int32_t); 155 int pfkey_send_spdget(int, u_int32_t); 156 int pfkey_send_spdsetidx(int, struct sockaddr *, u_int, 157 struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t); 158 int pfkey_send_spdflush(int); 159 int pfkey_send_spddump(int); 160 #ifdef SADB_X_MIGRATE 161 int pfkey_send_migrate(int, struct sockaddr *, struct sockaddr *, 162 struct sockaddr *, u_int, struct sockaddr *, u_int, u_int, 163 caddr_t, int, u_int32_t); 164 #endif 165 166 /* XXX should be somewhere else !!! 167 */ 168 #ifdef SADB_X_EXT_NAT_T_TYPE 169 #define PFKEY_ADDR_X_PORT(ext) (ntohs(((struct sadb_x_nat_t_port *)ext)->sadb_x_nat_t_port_port)) 170 #define PFKEY_ADDR_X_NATTYPE(ext) ( ext != NULL && ((struct sadb_x_nat_t_type *)ext)->sadb_x_nat_t_type_type ) 171 #endif 172 173 174 int pfkey_open(void); 175 void pfkey_close(int); 176 int pfkey_set_buffer_size(int, int); 177 struct sadb_msg *pfkey_recv(int); 178 int pfkey_send(int, struct sadb_msg *, int); 179 int pfkey_align(struct sadb_msg *, caddr_t *); 180 int pfkey_check(caddr_t *); 181 182 /* 183 * Deprecated, available for backward compatibility with third party 184 * libipsec users. Please use pfkey_send_update2 and pfkey_send_add2 instead 185 */ 186 int pfkey_send_update(int, u_int, u_int, struct sockaddr *, 187 struct sockaddr *, u_int32_t, u_int32_t, u_int, 188 caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t, 189 u_int64_t, u_int64_t, u_int32_t); 190 int pfkey_send_update_nat(int, u_int, u_int, struct sockaddr *, 191 struct sockaddr *, u_int32_t, u_int32_t, u_int, 192 caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t, 193 u_int64_t, u_int64_t, u_int32_t, 194 u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t); 195 int pfkey_send_add(int, u_int, u_int, struct sockaddr *, 196 struct sockaddr *, u_int32_t, u_int32_t, u_int, 197 caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t, 198 u_int64_t, u_int64_t, u_int32_t); 199 int pfkey_send_add_nat(int, u_int, u_int, struct sockaddr *, 200 struct sockaddr *, u_int32_t, u_int32_t, u_int, 201 caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t, 202 u_int64_t, u_int64_t, u_int32_t, 203 u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t); 204 205 #ifndef __SYSDEP_SA_LEN__ 206 #define __SYSDEP_SA_LEN__ 207 #include <netinet/in.h> 208 209 #ifndef IPPROTO_IPV4 210 #define IPPROTO_IPV4 IPPROTO_IPIP 211 #endif 212 213 #ifndef IPPROTO_IPCOMP 214 #define IPPROTO_IPCOMP IPPROTO_COMP 215 #endif 216 217 #ifndef IPPROTO_MH 218 #define IPPROTO_MH 135 219 #endif 220 221 static __inline u_int8_t 222 sysdep_sa_len (const struct sockaddr *sa) 223 { 224 #ifdef __linux__ 225 switch (sa->sa_family) 226 { 227 case AF_INET: 228 return sizeof (struct sockaddr_in); 229 case AF_INET6: 230 return sizeof (struct sockaddr_in6); 231 } 232 // log_print ("sysdep_sa_len: unknown sa family %d", sa->sa_family); 233 return sizeof (struct sockaddr_in); 234 #else 235 return sa->sa_len; 236 #endif 237 } 238 #endif 239 240 #endif /* KAME_LIBPFKEY_H */ 241 242 #endif /* _LIBPFKEY_H */ 243