1 /*        $NetBSD: libpfkey.h,v 1.21 2018/09/06 09:54:36 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 #include <sys/types.h>
57 #include PATH_IPSEC_H
58 
59 #ifndef HAVE_IPSEC_POLICY_T
60 typedef caddr_t ipsec_policy_t;
61 #ifdef __NetBSD__
62 #define __ipsec_const const
63 #else
64 #define __ipsec_const
65 #endif
66 #else
67 #define __ipsec_const const
68 #endif
69 
70 struct pfkey_send_sa_args {
71           int                 so;                           /* socket */
72           u_int               type;
73           u_int               satype;
74           u_int               mode;
75           struct sockaddr *src;                             /* IP src address for SA */
76           struct sockaddr *dst;                             /* IP dst address for SA */
77           u_int32_t           spi;                          /* SA's spi */
78           u_int32_t           reqid;
79           u_int               wsize;
80           caddr_t             keymat;
81           u_int               e_type, e_keylen;   /* Encryption alg and keylen */
82           u_int               a_type, a_keylen;   /* Authentication alg and key */
83           u_int               flags;
84           u_int32_t l_alloc;
85           u_int32_t l_bytes;
86           u_int32_t l_addtime;
87           u_int32_t l_usetime;
88           u_int32_t seq;
89           u_int8_t  l_natt_type;
90           u_int16_t l_natt_sport, l_natt_dport;
91           struct sockaddr *l_natt_oa;
92           u_int16_t l_natt_frag;
93           u_int8_t ctxdoi, ctxalg;      /* Security context DOI and algorithm */
94           caddr_t ctxstr;                         /* Security context string */
95           u_int16_t ctxstrlen;                    /* length of security context string */
96 };
97 
98 /* The options built into libipsec */
99 extern int libipsec_opt;
100 #define LIBIPSEC_OPT_NATT               0x01
101 #define LIBIPSEC_OPT_FRAG               0x02
102 #define LIBIPSEC_OPT_SEC_CTX            0x04
103 
104 /* IPsec Library Routines */
105 
106 int ipsec_check_keylen(u_int, u_int, u_int);
107 int ipsec_check_keylen2(u_int, u_int, u_int);
108 int ipsec_get_keylen(u_int, u_int, struct sadb_alg *);
109 char *ipsec_dump_policy_withports(void *, const char *);
110 void ipsec_hexdump(const void *, int);
111 const char *ipsec_strerror(void);
112 void kdebug_sadb(struct sadb_msg *);
113 ipsec_policy_t ipsec_set_policy(__ipsec_const char *, int);
114 int  ipsec_get_policylen(ipsec_policy_t);
115 char *ipsec_dump_policy(ipsec_policy_t, __ipsec_const char *);
116 
117 /* PFKey Routines */
118 
119 u_int pfkey_set_softrate(u_int, u_int);
120 u_int pfkey_get_softrate(u_int);
121 int pfkey_send_getspi(int, u_int, u_int, struct sockaddr *,
122           struct sockaddr *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
123 int pfkey_send_getspi_nat(int, u_int, u_int,
124           struct sockaddr *, struct sockaddr *, u_int8_t, u_int16_t, u_int16_t,
125           u_int32_t, u_int32_t, u_int32_t, u_int32_t);
126 
127 int pfkey_send_update2(struct pfkey_send_sa_args *);
128 int pfkey_send_add2(struct pfkey_send_sa_args *);
129 int pfkey_send_delete(int, u_int, u_int,
130           struct sockaddr *, struct sockaddr *, u_int32_t);
131 int pfkey_send_delete_all(int, u_int, u_int,
132           struct sockaddr *, struct sockaddr *);
133 int pfkey_send_get(int, u_int, u_int,
134           struct sockaddr *, struct sockaddr *, u_int32_t);
135 int pfkey_send_register(int, u_int);
136 int pfkey_recv_register(int);
137 int pfkey_set_supported(struct sadb_msg *, int);
138 int pfkey_send_flush(int, u_int);
139 int pfkey_send_dump(int, u_int);
140 int pfkey_send_promisc_toggle(int, int);
141 int pfkey_send_spdadd(int, struct sockaddr *, u_int,
142           struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t);
143 int pfkey_send_spdadd2(int, struct sockaddr *, u_int,
144           struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
145           caddr_t, int, u_int32_t);
146 int pfkey_send_spdupdate(int, struct sockaddr *, u_int,
147           struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t);
148 int pfkey_send_spdupdate2(int, struct sockaddr *, u_int,
149           struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
150           caddr_t, int, u_int32_t);
151 int pfkey_send_spddelete(int, struct sockaddr *, u_int,
152           struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t);
153 int pfkey_send_spddelete2(int, u_int32_t);
154 int pfkey_send_spdget(int, u_int32_t);
155 int pfkey_send_spdsetidx(int, struct sockaddr *, u_int,
156           struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t);
157 int pfkey_send_spdflush(int);
158 int pfkey_send_spddump(int);
159 #ifdef SADB_X_MIGRATE
160 int pfkey_send_migrate(int, struct sockaddr *, struct sockaddr *,
161         struct sockaddr *, u_int, struct sockaddr *, u_int, u_int,
162         caddr_t, int, u_int32_t);
163 #endif
164 
165 /* XXX should be somewhere else !!!
166  */
167 #ifdef SADB_X_EXT_NAT_T_TYPE
168 #define PFKEY_ADDR_X_PORT(ext) (ntohs(((struct sadb_x_nat_t_port *)ext)->sadb_x_nat_t_port_port))
169 #define PFKEY_ADDR_X_NATTYPE(ext) ( ext != NULL && ((struct sadb_x_nat_t_type *)ext)->sadb_x_nat_t_type_type )
170 #endif
171 
172 
173 int pfkey_open(void);
174 void pfkey_close(int);
175 int pfkey_set_buffer_size(int, int);
176 struct sadb_msg *pfkey_recv(int);
177 int pfkey_send(int, struct sadb_msg *, int);
178 int pfkey_align(struct sadb_msg *, caddr_t *);
179 int pfkey_check(caddr_t *);
180 
181 /*
182  * Deprecated, available for backward compatibility with third party
183  * libipsec users. Please use pfkey_send_update2 and pfkey_send_add2 instead
184  */
185 int pfkey_send_update(int, u_int, u_int, struct sockaddr *,
186           struct sockaddr *, u_int32_t, u_int32_t, u_int,
187           caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
188           u_int64_t, u_int64_t, u_int32_t);
189 int pfkey_send_update_nat(int, u_int, u_int, struct sockaddr *,
190           struct sockaddr *, u_int32_t, u_int32_t, u_int,
191           caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
192           u_int64_t, u_int64_t, u_int32_t,
193           u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t);
194 int pfkey_send_add(int, u_int, u_int, struct sockaddr *,
195           struct sockaddr *, u_int32_t, u_int32_t, u_int,
196           caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
197           u_int64_t, u_int64_t, u_int32_t);
198 int pfkey_send_add_nat(int, u_int, u_int, struct sockaddr *,
199           struct sockaddr *, u_int32_t, u_int32_t, u_int,
200           caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
201           u_int64_t, u_int64_t, u_int32_t,
202           u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t);
203 
204 #ifndef __SYSDEP_SA_LEN__
205 #define __SYSDEP_SA_LEN__
206 #include <netinet/in.h>
207 
208 #ifndef IPPROTO_IPV4
209 #define IPPROTO_IPV4 IPPROTO_IPIP
210 #endif
211 
212 #ifndef IPPROTO_IPCOMP
213 #define IPPROTO_IPCOMP IPPROTO_COMP
214 #endif
215 
216 #ifndef IPPROTO_MH
217 #define IPPROTO_MH            135
218 #endif
219 
220 static __inline u_int8_t
sysdep_sa_len(const struct sockaddr * sa)221 sysdep_sa_len (const struct sockaddr *sa)
222 {
223 #ifdef __linux__
224   switch (sa->sa_family)
225     {
226     case AF_INET:
227       return sizeof (struct sockaddr_in);
228     case AF_INET6:
229       return sizeof (struct sockaddr_in6);
230     }
231   // log_print ("sysdep_sa_len: unknown sa family %d", sa->sa_family);
232   return sizeof (struct sockaddr_in);
233 #else
234   return sa->sa_len;
235 #endif
236 }
237 #endif
238 
239 #endif /* KAME_LIBPFKEY_H */
240 
241 #endif /* _LIBPFKEY_H */
242