1 /* $NetBSD: ipsec.h,v 1.37 2015/04/01 02:49:44 ozaki-r Exp $ */
2 /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.h,v 1.2.4.2 2004/02/14 22:23:23 bms Exp $ */
3 /* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun 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 /*
35 * IPsec controller part.
36 */
37
38 #ifndef _NETIPSEC_IPSEC_H_
39 #define _NETIPSEC_IPSEC_H_
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_inet.h"
43 #include "opt_ipsec.h"
44 #endif
45
46 #include <net/pfkeyv2.h>
47
48 #ifdef _KERNEL
49
50 #include <netipsec/keydb.h>
51 #include <netipsec/ipsec_osdep.h>
52
53 /*
54 * Security Policy Index
55 * Ensure that both address families in the "src" and "dst" are same.
56 * When the value of the ul_proto is ICMPv6, the port field in "src"
57 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
58 */
59 struct secpolicyindex {
60 u_int8_t dir; /* direction of packet flow, see blow */
61 union sockaddr_union src; /* IP src address for SP */
62 union sockaddr_union dst; /* IP dst address for SP */
63 u_int8_t prefs; /* prefix length in bits for src */
64 u_int8_t prefd; /* prefix length in bits for dst */
65 u_int16_t ul_proto; /* upper layer Protocol */
66 #ifdef notyet
67 uid_t uids;
68 uid_t uidd;
69 gid_t gids;
70 gid_t gidd;
71 #endif
72 };
73
74 /* Security Policy Data Base */
75 struct secpolicy {
76 LIST_ENTRY(secpolicy) chain;
77
78 u_int refcnt; /* reference count */
79 struct secpolicyindex spidx; /* selector */
80 u_int32_t id; /* It's unique number on the system. */
81 u_int state; /* 0: dead, others: alive */
82 #define IPSEC_SPSTATE_DEAD 0
83 #define IPSEC_SPSTATE_ALIVE 1
84
85 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */
86 struct ipsecrequest *req;
87 /* pointer to the ipsec request tree, */
88 /* if policy == IPSEC else this value == NULL.*/
89
90 /*
91 * lifetime handler.
92 * the policy can be used without limitiation if both lifetime and
93 * validtime are zero.
94 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
95 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
96 */
97 time_t created; /* time created the policy */
98 time_t lastused; /* updated every when kernel sends a packet */
99 time_t lifetime; /* duration of the lifetime of this policy */
100 time_t validtime; /* duration this policy is valid without use */
101 };
102
103 /* Request for IPsec */
104 struct ipsecrequest {
105 struct ipsecrequest *next;
106 /* pointer to next structure */
107 /* If NULL, it means the end of chain. */
108 struct secasindex saidx;/* hint for search proper SA */
109 /* if __ss_len == 0 then no address specified.*/
110 u_int level; /* IPsec level defined below. */
111
112 struct secasvar *sav; /* place holder of SA for use */
113 struct secpolicy *sp; /* back pointer to SP */
114 };
115
116 /* security policy in PCB */
117 struct inpcbpolicy {
118 struct secpolicy *sp_in;
119 struct secpolicy *sp_out;
120 int priv; /* privileged socket ? */
121
122 #ifdef __NetBSD__
123 /* cached policy */
124 struct {
125 struct secpolicy *cachesp;
126 struct secpolicyindex cacheidx;
127 int cachehint; /* processing requirement hint: */
128 #define IPSEC_PCBHINT_MAYBE 0 /* IPsec processing maybe required */
129 #define IPSEC_PCBHINT_YES 1 /* IPsec processing is required */
130 #define IPSEC_PCBHINT_NO 2 /* IPsec processing not required */
131 u_int cachegen; /* spdgen when cache filled */
132 } sp_cache[3]; /* XXX 3 == IPSEC_DIR_MAX */
133 int sp_cacheflags;
134 #define IPSEC_PCBSP_CONNECTED 1
135 #endif /* __NetBSD__ */
136 };
137
138 #ifdef __NetBSD__
139 #define IPSEC_PCB_SKIP_IPSEC(inpp, dir) \
140 ((inpp)->sp_cache[(dir)].cachehint == IPSEC_PCBHINT_NO && \
141 (inpp)->sp_cache[(dir)].cachegen == ipsec_spdgen)
142 #endif /* __NetBSD__ */
143
144 /* SP acquiring list table. */
145 struct secspacq {
146 LIST_ENTRY(secspacq) chain;
147
148 struct secpolicyindex spidx;
149
150 time_t created; /* for lifetime */
151 int count; /* for lifetime */
152 /* XXX: here is mbuf place holder to be sent ? */
153 };
154 #endif /* _KERNEL */
155
156 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
157 #define IPSEC_PORT_ANY 0
158 #define IPSEC_ULPROTO_ANY 255
159 #define IPSEC_PROTO_ANY 255
160
161 /* mode of security protocol */
162 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
163 #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
164 #define IPSEC_MODE_TRANSPORT 1
165 #define IPSEC_MODE_TUNNEL 2
166 #define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */
167
168 /*
169 * Direction of security policy.
170 * NOTE: Since INVALID is used just as flag.
171 * The other are used for loop counter too.
172 */
173 #define IPSEC_DIR_ANY 0
174 #define IPSEC_DIR_INBOUND 1
175 #define IPSEC_DIR_OUTBOUND 2
176 #define IPSEC_DIR_MAX 3
177 #define IPSEC_DIR_INVALID 4
178
179 /* Policy level */
180 /*
181 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
182 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
183 * DISCARD and NONE are allowed for system default.
184 */
185 #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
186 #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
187 #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
188 #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
189 #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
190
191 /* Security protocol level */
192 #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
193 #define IPSEC_LEVEL_USE 1 /* use SA if present. */
194 #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
195 #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
196
197 #define IPSEC_MANUAL_REQID_MAX 0x3fff
198 /*
199 * if security policy level == unique, this id
200 * indicate to a relative SA for use, else is
201 * zero.
202 * 1 - 0x3fff are reserved for manual keying.
203 * 0 are reserved for above reason. Others is
204 * for kernel use.
205 * Note that this id doesn't identify SA
206 * by only itself.
207 */
208 #define IPSEC_REPLAYWSIZE 32
209
210 #ifdef _KERNEL
211 struct ipsec_output_state {
212 struct mbuf *m;
213 struct route *ro;
214 struct sockaddr *dst;
215 };
216
217 struct ipsec_history {
218 int ih_proto;
219 u_int32_t ih_spi;
220 };
221
222 extern int ipsec_debug;
223 #ifdef IPSEC_DEBUG
224 extern int ipsec_replay;
225 extern int ipsec_integrity;
226 #endif
227
228 extern struct secpolicy ip4_def_policy;
229 extern int ip4_esp_trans_deflev;
230 extern int ip4_esp_net_deflev;
231 extern int ip4_ah_trans_deflev;
232 extern int ip4_ah_net_deflev;
233 extern int ip4_ah_cleartos;
234 extern int ip4_ah_offsetmask;
235 extern int ip4_ipsec_dfbit;
236 extern int ip4_ipsec_ecn;
237 extern int ip4_esp_randpad;
238 extern int crypto_support;
239
240 #define ipseclog(x) do { if (ipsec_debug) log x; } while (0)
241 /* for openbsd compatibility */
242 #define DPRINTF(x) do { if (ipsec_debug) printf x; } while (0)
243
244 #ifdef __NetBSD__
245 void ipsec_pcbconn (struct inpcbpolicy *);
246 void ipsec_pcbdisconn (struct inpcbpolicy *);
247 void ipsec_invalpcbcacheall (void);
248
249 extern u_int ipsec_spdgen;
250 #endif /* __NetBSD__ */
251
252 struct tdb_ident;
253 struct secpolicy *ipsec_getpolicy (const struct tdb_ident*, u_int);
254 struct inpcb;
255 struct secpolicy *ipsec4_checkpolicy (struct mbuf *, u_int, u_int,
256 int *, struct inpcb *);
257 struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
258 int, int *);
259 int ipsec4_output(struct mbuf *, struct socket *, int,
260 struct secpolicy **, u_long *, bool *, bool *);
261 int ipsec4_input(struct mbuf *, int);
262 int ipsec4_forward(struct mbuf *, int *);
263 #ifdef INET6
264 int ipsec6_input(struct mbuf *);
265 #endif
266
267 static __inline struct secpolicy*
ipsec4_getpolicybysock(struct mbuf * m,u_int dir,const struct socket * so,int * err)268 ipsec4_getpolicybysock(
269 struct mbuf *m,
270 u_int dir,
271 const struct socket *so,
272 int *err
273 )
274 {
275 panic("ipsec4_getpolicybysock");
276 }
277
278 static __inline int
ipsec_copy_pcbpolicy(struct inpcbpolicy * oldp,struct inpcbpolicy * newp)279 ipsec_copy_pcbpolicy(
280 struct inpcbpolicy *oldp,
281 struct inpcbpolicy *newp
282 )
283 {
284 /*XXX do nothing */
285 return (0);
286 }
287
288 struct inpcb;
289 #define ipsec_init_pcbpolicy ipsec_init_policy
290 int ipsec_init_policy (struct socket *so, struct inpcbpolicy **);
291 int ipsec_copy_policy
292 (const struct inpcbpolicy *, struct inpcbpolicy *);
293 u_int ipsec_get_reqlevel (const struct ipsecrequest *);
294 int ipsec_in_reject (const struct secpolicy *, const struct mbuf *);
295
296 int ipsec4_set_policy (struct inpcb *, int, const void *, size_t, kauth_cred_t);
297 int ipsec4_get_policy (struct inpcb *, const void *, size_t, struct mbuf **);
298 int ipsec4_delete_pcbpolicy (struct inpcb *);
299 int ipsec4_in_reject (struct mbuf *, struct inpcb *);
300 /*
301 * KAME ipsec4_in_reject_so(struct mbuf*, struct so) compatibility shim
302 */
303 #define ipsec4_in_reject_so(m, _so) \
304 ipsec4_in_reject(m, ((_so) == NULL? NULL : sotoinpcb(_so)))
305
306
307 struct secas;
308 struct tcpcb;
309 int ipsec_chkreplay (u_int32_t, const struct secasvar *);
310 int ipsec_updatereplay (u_int32_t, const struct secasvar *);
311
312 size_t ipsec4_hdrsiz (struct mbuf *, u_int, struct inpcb *);
313 #ifdef __FreeBSD__
314 size_t ipsec_hdrsiz_tcp (struct tcpcb *);
315 #else
316 size_t ipsec4_hdrsiz_tcp (struct tcpcb *);
317 #define ipsec4_getpolicybyaddr ipsec_getpolicybyaddr
318 #endif
319
320 union sockaddr_union;
321 const char *ipsec_address(const union sockaddr_union* sa);
322 const char *ipsec_logsastr (const struct secasvar *);
323
324 void ipsec_dumpmbuf (struct mbuf *);
325
326 /* NetBSD protosw ctlin entrypoint */
327 void *esp4_ctlinput(int, const struct sockaddr *, void *);
328 void *ah4_ctlinput(int, const struct sockaddr *, void *);
329
330 struct m_tag;
331 void ipsec4_common_input(struct mbuf *m, ...);
332 int ipsec4_common_input_cb(struct mbuf *, struct secasvar *,
333 int, int, struct m_tag *);
334 int ipsec4_process_packet (struct mbuf *, struct ipsecrequest *,
335 int, int);
336 int ipsec_process_done (struct mbuf *, struct ipsecrequest *);
337 #define ipsec_indone(m) \
338 (m_tag_find((m), PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
339
340 #define ipsec_outdone(m) \
341 (m_tag_find((m), PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
342
343 struct mbuf *ipsec_copypkt (struct mbuf *);
344
345 void m_checkalignment(const char* , struct mbuf *, int, int);
346 struct mbuf *m_clone(struct mbuf *);
347 struct mbuf *m_makespace(struct mbuf *, int, int, int *);
348 void *m_pad(struct mbuf *, int );
349 int m_striphdr(struct mbuf *, int, int);
350
351 /* Per-socket caching of IPsec output policy */
352 static __inline
ipsec_clear_socket_cache(struct mbuf * m)353 int ipsec_clear_socket_cache(struct mbuf *m)
354 {
355 return 0;
356 }
357
358 void nat_t_ports_get(struct mbuf *, u_int16_t *, u_int16_t *);
359
360 extern int ipsec_used __read_mostly, ipsec_enabled __read_mostly;
361
362 #endif /* _KERNEL */
363
364 #ifndef _KERNEL
365 char *ipsec_set_policy (const char *, int);
366 int ipsec_get_policylen (char *);
367 char *ipsec_dump_policy (char *, const char *);
368
369 const char *ipsec_strerror (void);
370 #endif /* !_KERNEL */
371
372 #ifdef _KERNEL
373 /* External declarations of per-file init functions */
374 INITFN void ah_attach(void);
375 INITFN void esp_attach(void);
376 INITFN void ipcomp_attach(void);
377 INITFN void ipe4_attach(void);
378 INITFN void ipe4_attach(void);
379 INITFN void tcpsignature_attach(void);
380
381 INITFN void ipsec_attach(void);
382 #endif /* _KERNEL */
383 #endif /* !_NETIPSEC_IPSEC_H_ */
384