1 | /* $FreeBSD: src/sys/netinet6/ipsec.h,v 1.4.2.2 2001/07/03 11:01:54 ume Exp $ */ |
2 | /* $KAME: ipsec.h,v 1.44 2001/03/23 08:08:47 itojun Exp $ */ |
3 | |
4 | /* |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. Neither the name of the project nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | * SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * IPsec controller part. |
35 | */ |
36 | |
37 | #ifndef _NETINET6_IPSEC_H_ |
38 | #define _NETINET6_IPSEC_H_ |
39 | #include <sys/cdefs.h> |
40 | #include <sys/appleapiopts.h> |
41 | |
42 | #include <net/pfkeyv2.h> |
43 | #ifdef BSD_KERNEL_PRIVATE |
44 | #include <netkey/keydb.h> |
45 | #include <netinet/ip_var.h> |
46 | |
47 | /* lock for IPSec stats */ |
48 | extern lck_grp_t *sadb_stat_mutex_grp; |
49 | extern lck_grp_attr_t *sadb_stat_mutex_grp_attr; |
50 | extern lck_attr_t *sadb_stat_mutex_attr; |
51 | extern lck_mtx_t *sadb_stat_mutex; |
52 | |
53 | |
54 | #define IPSEC_STAT_INCREMENT(x) \ |
55 | OSIncrementAtomic64((SInt64 *)&x) |
56 | |
57 | struct secpolicyaddrrange { |
58 | struct sockaddr_storage start; /* Start (low values) of address range */ |
59 | struct sockaddr_storage end; /* End (high values) of address range */ |
60 | }; |
61 | |
62 | /* |
63 | * Security Policy Index |
64 | * Ensure that both address families in the "src" and "dst" are same. |
65 | * When the value of the ul_proto is ICMPv6, the port field in "src" |
66 | * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code. |
67 | */ |
68 | struct secpolicyindex { |
69 | u_int8_t dir; /* direction of packet flow, see blow */ |
70 | struct sockaddr_storage src; /* IP src address for SP */ |
71 | struct sockaddr_storage dst; /* IP dst address for SP */ |
72 | u_int8_t prefs; /* prefix length in bits for src */ |
73 | u_int8_t prefd; /* prefix length in bits for dst */ |
74 | u_int16_t ul_proto; /* upper layer Protocol */ |
75 | ifnet_t internal_if; /* Interface a matching packet is bound to */ |
76 | struct secpolicyaddrrange src_range; /* IP src address range for SP */ |
77 | struct secpolicyaddrrange dst_range; /* IP dst address range for SP */ |
78 | #ifdef notyet |
79 | uid_t uids; |
80 | uid_t uidd; |
81 | gid_t gids; |
82 | gid_t gidd; |
83 | #endif |
84 | }; |
85 | |
86 | /* Security Policy Data Base */ |
87 | struct secpolicy { |
88 | LIST_ENTRY(secpolicy) chain; |
89 | |
90 | int refcnt; /* reference count */ |
91 | struct secpolicyindex spidx; /* selector */ |
92 | u_int32_t id; /* It's unique number on the system. */ |
93 | u_int state; /* 0: dead, others: alive */ |
94 | #define IPSEC_SPSTATE_DEAD 0 |
95 | #define IPSEC_SPSTATE_ALIVE 1 |
96 | |
97 | u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */ |
98 | struct ipsecrequest *req; |
99 | /* pointer to the ipsec request tree, */ |
100 | /* if policy == IPSEC else this value == NULL.*/ |
101 | |
102 | ifnet_t ipsec_if; /* IPSec interface to use */ |
103 | ifnet_t outgoing_if; /* Outgoing interface for encrypted traffic */ |
104 | |
105 | char disabled; /* Set to ignore policy */ |
106 | |
107 | /* |
108 | * lifetime handler. |
109 | * the policy can be used without limitiation if both lifetime and |
110 | * validtime are zero. |
111 | * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime. |
112 | * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime. |
113 | */ |
114 | long created; /* time created the policy */ |
115 | long lastused; /* updated every when kernel sends a packet */ |
116 | long lifetime; /* duration of the lifetime of this policy */ |
117 | long validtime; /* duration this policy is valid without use */ |
118 | }; |
119 | |
120 | /* Request for IPsec */ |
121 | struct ipsecrequest { |
122 | struct ipsecrequest *next; |
123 | /* pointer to next structure */ |
124 | /* If NULL, it means the end of chain. */ |
125 | struct secasindex saidx;/* hint for search proper SA */ |
126 | /* if __ss_len == 0 then no address specified.*/ |
127 | u_int level; /* IPsec level defined below. */ |
128 | |
129 | struct secpolicy *sp; /* back pointer to SP */ |
130 | }; |
131 | |
132 | /* security policy in PCB */ |
133 | struct inpcbpolicy { |
134 | struct secpolicy *sp_in; |
135 | struct secpolicy *sp_out; |
136 | int priv; /* privileged socket ? */ |
137 | }; |
138 | |
139 | /* SP acquiring list table. */ |
140 | struct secspacq { |
141 | LIST_ENTRY(secspacq) chain; |
142 | |
143 | struct secpolicyindex spidx; |
144 | |
145 | long created; /* for lifetime */ |
146 | int count; /* for lifetime */ |
147 | /* XXX: here is mbuf place holder to be sent ? */ |
148 | }; |
149 | #endif /* BSD_KERNEL_PRIVATE */ |
150 | |
151 | /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */ |
152 | #define IPSEC_PORT_ANY 0 |
153 | #define IPSEC_ULPROTO_ANY 255 |
154 | #define IPSEC_PROTO_ANY 255 |
155 | |
156 | /* mode of security protocol */ |
157 | /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */ |
158 | #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */ |
159 | #define IPSEC_MODE_TRANSPORT 1 |
160 | #define IPSEC_MODE_TUNNEL 2 |
161 | |
162 | /* |
163 | * Direction of security policy. |
164 | * NOTE: Since INVALID is used just as flag. |
165 | * The other are used for loop counter too. |
166 | */ |
167 | #define IPSEC_DIR_ANY 0 |
168 | #define IPSEC_DIR_INBOUND 1 |
169 | #define IPSEC_DIR_OUTBOUND 2 |
170 | #define IPSEC_DIR_MAX 3 |
171 | #define IPSEC_DIR_INVALID 4 |
172 | |
173 | /* Policy level */ |
174 | /* |
175 | * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB, |
176 | * DISCARD, IPSEC and NONE are allowed for setkey() in SPD. |
177 | * DISCARD and NONE are allowed for system default. |
178 | */ |
179 | #define IPSEC_POLICY_DISCARD 0 /* discarding packet */ |
180 | #define IPSEC_POLICY_NONE 1 /* through IPsec engine */ |
181 | #define IPSEC_POLICY_IPSEC 2 /* do IPsec */ |
182 | #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */ |
183 | #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */ |
184 | #define IPSEC_POLICY_GENERATE 5 /* same as discard - IKE daemon can override with generated policy */ |
185 | |
186 | /* Security protocol level */ |
187 | #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */ |
188 | #define IPSEC_LEVEL_USE 1 /* use SA if present. */ |
189 | #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */ |
190 | #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */ |
191 | |
192 | #define IPSEC_MANUAL_REQID_MAX 0x3fff |
193 | /* |
194 | * if security policy level == unique, this id |
195 | * indicate to a relative SA for use, else is |
196 | * zero. |
197 | * 1 - 0x3fff are reserved for manual keying. |
198 | * 0 are reserved for above reason. Others is |
199 | * for kernel use. |
200 | * Note that this id doesn't identify SA |
201 | * by only itself. |
202 | */ |
203 | #define IPSEC_REPLAYWSIZE 32 |
204 | |
205 | /* statistics for ipsec processing */ |
206 | struct ipsecstat { |
207 | u_quad_t in_success __attribute__ ((aligned (8))); /* succeeded inbound process */ |
208 | u_quad_t in_polvio __attribute__ ((aligned (8))); |
209 | /* security policy violation for inbound process */ |
210 | u_quad_t in_nosa __attribute__ ((aligned (8))); /* inbound SA is unavailable */ |
211 | u_quad_t in_inval __attribute__ ((aligned (8))); /* inbound processing failed due to EINVAL */ |
212 | u_quad_t in_nomem __attribute__ ((aligned (8))); /* inbound processing failed due to ENOBUFS */ |
213 | u_quad_t in_badspi __attribute__ ((aligned (8))); /* failed getting a SPI */ |
214 | u_quad_t in_ahreplay __attribute__ ((aligned (8))); /* AH replay check failed */ |
215 | u_quad_t in_espreplay __attribute__ ((aligned (8))); /* ESP replay check failed */ |
216 | u_quad_t in_ahauthsucc __attribute__ ((aligned (8))); /* AH authentication success */ |
217 | u_quad_t in_ahauthfail __attribute__ ((aligned (8))); /* AH authentication failure */ |
218 | u_quad_t in_espauthsucc __attribute__ ((aligned (8))); /* ESP authentication success */ |
219 | u_quad_t in_espauthfail __attribute__ ((aligned (8))); /* ESP authentication failure */ |
220 | u_quad_t in_esphist[256] __attribute__ ((aligned (8))); |
221 | u_quad_t in_ahhist[256] __attribute__ ((aligned (8))); |
222 | u_quad_t in_comphist[256] __attribute__ ((aligned (8))); |
223 | u_quad_t out_success __attribute__ ((aligned (8))); /* succeeded outbound process */ |
224 | u_quad_t out_polvio __attribute__ ((aligned (8))); |
225 | /* security policy violation for outbound process */ |
226 | u_quad_t out_nosa __attribute__ ((aligned (8))); /* outbound SA is unavailable */ |
227 | u_quad_t out_inval __attribute__ ((aligned (8))); /* outbound process failed due to EINVAL */ |
228 | u_quad_t out_nomem __attribute__ ((aligned (8))); /* inbound processing failed due to ENOBUFS */ |
229 | u_quad_t out_noroute __attribute__ ((aligned (8))); /* there is no route */ |
230 | u_quad_t out_esphist[256] __attribute__ ((aligned (8))); |
231 | u_quad_t out_ahhist[256] __attribute__ ((aligned (8))); |
232 | u_quad_t out_comphist[256] __attribute__ ((aligned (8))); |
233 | }; |
234 | |
235 | #ifdef BSD_KERNEL_PRIVATE |
236 | /* |
237 | * Definitions for IPsec & Key sysctl operations. |
238 | */ |
239 | /* |
240 | * Names for IPsec & Key sysctl objects |
241 | */ |
242 | #define IPSECCTL_STATS 1 /* stats */ |
243 | #define IPSECCTL_DEF_POLICY 2 |
244 | #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */ |
245 | #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */ |
246 | #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */ |
247 | #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */ |
248 | #if 0 /* obsolete, do not reuse */ |
249 | #define IPSECCTL_INBOUND_CALL_IKE 7 |
250 | #endif |
251 | #define IPSECCTL_AH_CLEARTOS 8 |
252 | #define IPSECCTL_AH_OFFSETMASK 9 |
253 | #define IPSECCTL_DFBIT 10 |
254 | #define IPSECCTL_ECN 11 |
255 | #define IPSECCTL_DEBUG 12 |
256 | #define IPSECCTL_ESP_RANDPAD 13 |
257 | #define IPSECCTL_MAXID 14 |
258 | |
259 | #define IPSECCTL_NAMES { \ |
260 | { 0, 0 }, \ |
261 | { 0, 0 }, \ |
262 | { "def_policy", CTLTYPE_INT }, \ |
263 | { "esp_trans_deflev", CTLTYPE_INT }, \ |
264 | { "esp_net_deflev", CTLTYPE_INT }, \ |
265 | { "ah_trans_deflev", CTLTYPE_INT }, \ |
266 | { "ah_net_deflev", CTLTYPE_INT }, \ |
267 | { 0, 0 }, \ |
268 | { "ah_cleartos", CTLTYPE_INT }, \ |
269 | { "ah_offsetmask", CTLTYPE_INT }, \ |
270 | { "dfbit", CTLTYPE_INT }, \ |
271 | { "ecn", CTLTYPE_INT }, \ |
272 | { "debug", CTLTYPE_INT }, \ |
273 | { "esp_randpad", CTLTYPE_INT }, \ |
274 | } |
275 | |
276 | #define IPSEC6CTL_NAMES { \ |
277 | { 0, 0 }, \ |
278 | { 0, 0 }, \ |
279 | { "def_policy", CTLTYPE_INT }, \ |
280 | { "esp_trans_deflev", CTLTYPE_INT }, \ |
281 | { "esp_net_deflev", CTLTYPE_INT }, \ |
282 | { "ah_trans_deflev", CTLTYPE_INT }, \ |
283 | { "ah_net_deflev", CTLTYPE_INT }, \ |
284 | { 0, 0 }, \ |
285 | { 0, 0 }, \ |
286 | { 0, 0 }, \ |
287 | { 0, 0 }, \ |
288 | { "ecn", CTLTYPE_INT }, \ |
289 | { "debug", CTLTYPE_INT }, \ |
290 | { "esp_randpad", CTLTYPE_INT }, \ |
291 | } |
292 | |
293 | #if defined(__ARM__) |
294 | #define IPSEC_IS_P2ALIGNED(p) IS_P2ALIGNED(p, sizeof (u_int32_t)) |
295 | #define IPSEC_GET_P2UNALIGNED_OFS(p) (sizeof(u_int32_t) - (((uintptr_t)(p)) & ((uintptr_t)(sizeof(u_int32_t)) - 1))) |
296 | #else |
297 | #define IPSEC_IS_P2ALIGNED(p) 1 |
298 | #define IPSEC_GET_P2UNALIGNED_OFS(p) 0 |
299 | #endif |
300 | |
301 | struct ipsec_output_state { |
302 | int tunneled; |
303 | struct mbuf *m; |
304 | struct route_in6 ro; |
305 | struct sockaddr *dst; |
306 | u_int outgoing_if; |
307 | }; |
308 | |
309 | struct ipsec_history { |
310 | int ih_proto; |
311 | u_int32_t ih_spi; |
312 | }; |
313 | |
314 | extern int ipsec_debug; |
315 | |
316 | extern struct ipsecstat ipsecstat; |
317 | extern struct secpolicy ip4_def_policy; |
318 | extern int ip4_esp_trans_deflev; |
319 | extern int ip4_esp_net_deflev; |
320 | extern int ip4_ah_trans_deflev; |
321 | extern int ip4_ah_net_deflev; |
322 | extern int ip4_ah_cleartos; |
323 | extern int ip4_ah_offsetmask; |
324 | extern int ip4_ipsec_dfbit; |
325 | extern int ip4_ipsec_ecn; |
326 | extern int ip4_esp_randpad; |
327 | |
328 | #define ipseclog(x) do { if (ipsec_debug) log x; } while (0) |
329 | |
330 | extern struct secpolicy *ipsec4_getpolicybysock(struct mbuf *, u_int, |
331 | struct socket *, int *); |
332 | extern struct secpolicy *ipsec4_getpolicybyaddr(struct mbuf *, u_int, int, |
333 | int *); |
334 | extern int ipsec4_getpolicybyinterface(struct mbuf *, u_int, int *, |
335 | struct ip_out_args *, struct secpolicy **); |
336 | |
337 | extern u_int ipsec_get_reqlevel(struct ipsecrequest *); |
338 | |
339 | struct inpcb; |
340 | extern int ipsec_init_policy(struct socket *so, struct inpcbpolicy **); |
341 | extern int ipsec_copy_policy(struct inpcbpolicy *, struct inpcbpolicy *); |
342 | extern u_int ipsec_get_reqlevel(struct ipsecrequest *); |
343 | |
344 | extern int ipsec4_set_policy(struct inpcb *inp, int optname, |
345 | caddr_t request, size_t len, int priv); |
346 | extern int ipsec4_delete_pcbpolicy(struct inpcb *); |
347 | extern int ipsec4_in_reject_so(struct mbuf *, struct socket *); |
348 | extern int ipsec4_in_reject(struct mbuf *, struct inpcb *); |
349 | |
350 | struct secas; |
351 | struct tcpcb; |
352 | extern int ipsec_chkreplay(u_int32_t, struct secasvar *); |
353 | extern int ipsec_updatereplay(u_int32_t, struct secasvar *); |
354 | |
355 | extern size_t ipsec4_hdrsiz(struct mbuf *, u_int, struct inpcb *); |
356 | extern size_t ipsec_hdrsiz_tcp(struct tcpcb *); |
357 | extern size_t ipsec_hdrsiz(struct secpolicy *); |
358 | |
359 | struct ip; |
360 | extern const char *ipsec4_logpacketstr(struct ip *, u_int32_t); |
361 | extern const char *ipsec_logsastr(struct secasvar *); |
362 | |
363 | extern void ipsec_dumpmbuf(struct mbuf *); |
364 | |
365 | extern int ipsec4_interface_output(struct ipsec_output_state *state, ifnet_t interface); |
366 | extern int ipsec4_output(struct ipsec_output_state *, struct secpolicy *, int); |
367 | #if INET |
368 | extern struct mbuf * ipsec4_splithdr(struct mbuf *); |
369 | extern int ipsec4_encapsulate(struct mbuf *, struct secasvar *); |
370 | #endif |
371 | #if INET6 |
372 | extern struct mbuf * ipsec6_splithdr(struct mbuf *); |
373 | extern int ipsec6_encapsulate(struct mbuf *, struct secasvar *); |
374 | #endif |
375 | extern int ipsec4_tunnel_validate(struct mbuf *, int, u_int, struct secasvar *, sa_family_t *); |
376 | extern struct mbuf *ipsec_copypkt(struct mbuf *); |
377 | extern void ipsec_delaux(struct mbuf *); |
378 | extern int ipsec_setsocket(struct mbuf *, struct socket *); |
379 | extern struct socket *ipsec_getsocket(struct mbuf *); |
380 | extern int ipsec_addhist(struct mbuf *, int, u_int32_t); |
381 | extern struct ipsec_history *ipsec_gethist(struct mbuf *, int *); |
382 | extern void ipsec_clearhist(struct mbuf *); |
383 | #endif /* BSD_KERNEL_PRIVATE */ |
384 | |
385 | #ifndef KERNEL |
386 | __BEGIN_DECLS |
387 | extern caddr_t ipsec_set_policy(char *, int); |
388 | extern int ipsec_get_policylen(caddr_t); |
389 | extern char *ipsec_dump_policy(caddr_t, char *); |
390 | |
391 | extern const char *ipsec_strerror(void); |
392 | __END_DECLS |
393 | #endif /* KERNEL */ |
394 | |
395 | #endif /* _NETINET6_IPSEC_H_ */ |
396 | |