kernel - Port TCP-MD5 (RFC 2385) implementation.
[dragonfly.git] / sys / netinet6 / ipsec.h
1 /*      $FreeBSD: src/sys/netinet6/ipsec.h,v 1.4.2.4 2003/01/23 21:06:47 sam Exp $      */
2 /*      $DragonFly: src/sys/netinet6/ipsec.h,v 1.5 2006/10/24 06:18:42 hsu 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 _NETINET6_IPSEC_H_
39 #define _NETINET6_IPSEC_H_
40
41 #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
42 #include "opt_inet.h"
43 #include "opt_ipsec.h"
44 #endif
45
46 #include <net/pfkeyv2.h>
47 #include <netproto/key/keydb.h>
48
49 #ifdef _KERNEL
50
51 /*
52  * Security Policy Index
53  * Ensure that both address families in the "src" and "dst" are same.
54  * When the value of the ul_proto is ICMPv6, the port field in "src"
55  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
56  */
57 struct secpolicyindex {
58         u_int8_t dir;                   /* direction of packet flow, see blow */
59         struct sockaddr_storage src;    /* IP src address for SP */
60         struct sockaddr_storage dst;    /* IP dst address for SP */
61         u_int8_t prefs;                 /* prefix length in bits for src */
62         u_int8_t prefd;                 /* prefix length in bits for dst */
63         u_int16_t ul_proto;             /* upper layer Protocol */
64 #ifdef notyet
65         uid_t uids;
66         uid_t uidd;
67         gid_t gids;
68         gid_t gidd;
69 #endif
70 };
71
72 /* Security Policy Data Base */
73 struct secpolicy {
74         LIST_ENTRY(secpolicy) chain;
75
76         int refcnt;                     /* reference count */
77         struct secpolicyindex spidx;    /* selector */
78         u_int32_t id;                   /* It's unique number on the system. */
79         u_int state;                    /* 0: dead, others: alive */
80 #define IPSEC_SPSTATE_DEAD      0
81 #define IPSEC_SPSTATE_ALIVE     1
82
83         u_int policy;           /* DISCARD, NONE or IPSEC, see keyv2.h */
84         struct ipsecrequest *req;
85                                 /* pointer to the ipsec request tree, */
86                                 /* if policy == IPSEC else this value == NULL.*/
87
88         /*
89          * lifetime handler.
90          * the policy can be used without limitiation if both lifetime and
91          * validtime are zero.
92          * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
93          * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
94          */
95         long created;           /* time created the policy */
96         long lastused;          /* updated every when kernel sends a packet */
97         long lifetime;          /* duration of the lifetime of this policy */
98         long validtime;         /* duration this policy is valid without use */
99 };
100
101 /* Request for IPsec */
102 struct ipsecrequest {
103         struct ipsecrequest *next;
104                                 /* pointer to next structure */
105                                 /* If NULL, it means the end of chain. */
106         struct secasindex saidx;/* hint for search proper SA */
107                                 /* if __ss_len == 0 then no address specified.*/
108         u_int level;            /* IPsec level defined below. */
109
110         struct secasvar *sav;   /* place holder of SA for use */
111         struct secpolicy *sp;   /* back pointer to SP */
112 };
113
114 /* security policy in PCB */
115 struct inpcbpolicy {
116         struct secpolicy *sp_in;
117         struct secpolicy *sp_out;
118         int priv;                       /* privileged socket ? */
119 };
120
121 /* SP acquiring list table. */
122 struct secspacq {
123         LIST_ENTRY(secspacq) chain;
124
125         struct secpolicyindex spidx;
126
127         long created;           /* for lifetime */
128         int count;              /* for lifetime */
129         /* XXX: here is mbuf place holder to be sent ? */
130 };
131 #endif /* _KERNEL */
132
133 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
134 #define IPSEC_PORT_ANY          0
135 #define IPSEC_ULPROTO_ANY       255
136 #define IPSEC_PROTO_ANY         255
137
138 /* mode of security protocol */
139 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
140 #define IPSEC_MODE_ANY          0       /* i.e. wildcard. */
141 #define IPSEC_MODE_TRANSPORT    1
142 #define IPSEC_MODE_TUNNEL       2
143 #define IPSEC_MODE_TCPMD5 3  /* TCP MD5 mode */
144
145 /*
146  * Direction of security policy.
147  * NOTE: Since INVALID is used just as flag.
148  * The other are used for loop counter too.
149  */
150 #define IPSEC_DIR_ANY           0
151 #define IPSEC_DIR_INBOUND       1
152 #define IPSEC_DIR_OUTBOUND      2
153 #define IPSEC_DIR_MAX           3
154 #define IPSEC_DIR_INVALID       4
155
156 /* Policy level */
157 /*
158  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
159  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
160  * DISCARD and NONE are allowed for system default.
161  */
162 #define IPSEC_POLICY_DISCARD    0       /* discarding packet */
163 #define IPSEC_POLICY_NONE       1       /* through IPsec engine */
164 #define IPSEC_POLICY_IPSEC      2       /* do IPsec */
165 #define IPSEC_POLICY_ENTRUST    3       /* consulting SPD if present. */
166 #define IPSEC_POLICY_BYPASS     4       /* only for privileged socket. */
167 #define IPSEC_POLICY_TCP 5 /* TCP MD5 policy */
168
169 /* Security protocol level */
170 #define IPSEC_LEVEL_DEFAULT     0       /* reference to system default */
171 #define IPSEC_LEVEL_USE         1       /* use SA if present. */
172 #define IPSEC_LEVEL_REQUIRE     2       /* require SA. */
173 #define IPSEC_LEVEL_UNIQUE      3       /* unique SA. */
174
175 #define IPSEC_MANUAL_REQID_MAX  0x3fff
176                                 /*
177                                  * if security policy level == unique, this id
178                                  * indicate to a relative SA for use, else is
179                                  * zero.
180                                  * 1 - 0x3fff are reserved for manual keying.
181                                  * 0 are reserved for above reason.  Others is
182                                  * for kernel use.
183                                  * Note that this id doesn't identify SA
184                                  * by only itself.
185                                  */
186 #define IPSEC_REPLAYWSIZE  32
187
188 /* statistics for ipsec processing */
189 struct ipsecstat {
190         u_quad_t in_success;  /* succeeded inbound process */
191         u_quad_t in_polvio;
192                         /* security policy violation for inbound process */
193         u_quad_t in_nosa;     /* inbound SA is unavailable */
194         u_quad_t in_inval;    /* inbound processing failed due to EINVAL */
195         u_quad_t in_nomem;    /* inbound processing failed due to ENOBUFS */
196         u_quad_t in_badspi;   /* failed getting a SPI */
197         u_quad_t in_ahreplay; /* AH replay check failed */
198         u_quad_t in_espreplay; /* ESP replay check failed */
199         u_quad_t in_ahauthsucc; /* AH authentication success */
200         u_quad_t in_ahauthfail; /* AH authentication failure */
201         u_quad_t in_espauthsucc; /* ESP authentication success */
202         u_quad_t in_espauthfail; /* ESP authentication failure */
203         u_quad_t in_esphist[256];
204         u_quad_t in_ahhist[256];
205         u_quad_t in_comphist[256];
206         u_quad_t out_success; /* succeeded outbound process */
207         u_quad_t out_polvio;
208                         /* security policy violation for outbound process */
209         u_quad_t out_nosa;    /* outbound SA is unavailable */
210         u_quad_t out_inval;   /* outbound process failed due to EINVAL */
211         u_quad_t out_nomem;    /* inbound processing failed due to ENOBUFS */
212         u_quad_t out_noroute; /* there is no route */
213         u_quad_t out_esphist[256];
214         u_quad_t out_ahhist[256];
215         u_quad_t out_comphist[256];
216 };
217
218 /*
219  * Definitions for IPsec & Key sysctl operations.
220  */
221 /*
222  * Names for IPsec & Key sysctl objects
223  */
224 #define IPSECCTL_STATS                  1       /* stats */
225 #define IPSECCTL_DEF_POLICY             2
226 #define IPSECCTL_DEF_ESP_TRANSLEV       3       /* int; ESP transport mode */
227 #define IPSECCTL_DEF_ESP_NETLEV         4       /* int; ESP tunnel mode */
228 #define IPSECCTL_DEF_AH_TRANSLEV        5       /* int; AH transport mode */
229 #define IPSECCTL_DEF_AH_NETLEV          6       /* int; AH tunnel mode */
230 #if 0   /* obsolete, do not reuse */
231 #define IPSECCTL_INBOUND_CALL_IKE       7
232 #endif
233 #define IPSECCTL_AH_CLEARTOS            8
234 #define IPSECCTL_AH_OFFSETMASK          9
235 #define IPSECCTL_DFBIT                  10
236 #define IPSECCTL_ECN                    11
237 #define IPSECCTL_DEBUG                  12
238 #define IPSECCTL_ESP_RANDPAD            13
239 #define IPSECCTL_MAXID                  14
240
241 #define IPSECCTL_NAMES { \
242         { 0, 0 }, \
243         { 0, 0 }, \
244         { "def_policy", CTLTYPE_INT }, \
245         { "esp_trans_deflev", CTLTYPE_INT }, \
246         { "esp_net_deflev", CTLTYPE_INT }, \
247         { "ah_trans_deflev", CTLTYPE_INT }, \
248         { "ah_net_deflev", CTLTYPE_INT }, \
249         { 0, 0 }, \
250         { "ah_cleartos", CTLTYPE_INT }, \
251         { "ah_offsetmask", CTLTYPE_INT }, \
252         { "dfbit", CTLTYPE_INT }, \
253         { "ecn", CTLTYPE_INT }, \
254         { "debug", CTLTYPE_INT }, \
255         { "esp_randpad", CTLTYPE_INT }, \
256 }
257
258 #define IPSEC6CTL_NAMES { \
259         { 0, 0 }, \
260         { 0, 0 }, \
261         { "def_policy", CTLTYPE_INT }, \
262         { "esp_trans_deflev", CTLTYPE_INT }, \
263         { "esp_net_deflev", CTLTYPE_INT }, \
264         { "ah_trans_deflev", CTLTYPE_INT }, \
265         { "ah_net_deflev", CTLTYPE_INT }, \
266         { 0, 0 }, \
267         { 0, 0 }, \
268         { 0, 0 }, \
269         { 0, 0 }, \
270         { "ecn", CTLTYPE_INT }, \
271         { "debug", CTLTYPE_INT }, \
272         { "esp_randpad", CTLTYPE_INT }, \
273 }
274
275 #ifdef _KERNEL
276 struct ipsec_output_state {
277         struct mbuf *m;
278         struct route *ro;
279         struct sockaddr *dst;
280 };
281
282 struct ipsec_history {
283         int ih_proto;
284         u_int32_t ih_spi;
285 };
286
287 extern int ipsec_debug;
288
289 extern struct ipsecstat ipsecstat;
290 extern struct secpolicy ip4_def_policy;
291 extern int ip4_esp_trans_deflev;
292 extern int ip4_esp_net_deflev;
293 extern int ip4_ah_trans_deflev;
294 extern int ip4_ah_net_deflev;
295 extern int ip4_ah_cleartos;
296 extern int ip4_ah_offsetmask;
297 extern int ip4_ipsec_dfbit;
298 extern int ip4_ipsec_ecn;
299 extern int ip4_esp_randpad;
300
301 #define ipseclog(x)     do { if (ipsec_debug) log x; } while (0)
302
303 extern struct secpolicy *ipsec4_getpolicybysock
304         (struct mbuf *, u_int, struct socket *, int *);
305 extern struct secpolicy *ipsec4_getpolicybyaddr
306         (struct mbuf *, u_int, int, int *);
307
308 struct inpcb;
309 extern int ipsec_init_policy (struct socket *so, struct inpcbpolicy **);
310 extern int ipsec_copy_policy
311         (struct inpcbpolicy *, struct inpcbpolicy *);
312 extern u_int ipsec_get_reqlevel (struct ipsecrequest *);
313
314 extern int ipsec4_set_policy (struct inpcb *inp, int optname,
315         caddr_t request, size_t len, int priv);
316 extern int ipsec4_get_policy (struct inpcb *inpcb, caddr_t request,
317         size_t len, struct mbuf **mp);
318 extern int ipsec4_delete_pcbpolicy (struct inpcb *);
319 extern int ipsec4_in_reject_so (struct mbuf *, struct socket *);
320 extern int ipsec4_in_reject (struct mbuf *, struct inpcb *);
321
322 struct secas;
323 struct tcpcb;
324 extern int ipsec_chkreplay (u_int32_t, struct secasvar *);
325 extern int ipsec_updatereplay (u_int32_t, struct secasvar *);
326
327 extern size_t ipsec4_hdrsiz (struct mbuf *, u_int, struct inpcb *);
328 extern size_t ipsec_hdrsiz_tcp (struct tcpcb *);
329
330 struct ip;
331 extern const char *ipsec4_logpacketstr (struct ip *, u_int32_t);
332 extern const char *ipsec_logsastr (struct secasvar *);
333
334 extern void ipsec_dumpmbuf (struct mbuf *);
335
336 extern int ipsec4_output (struct ipsec_output_state *, struct secpolicy *,
337         int);
338 extern int ipsec4_tunnel_validate (struct mbuf *, int, u_int,
339         struct secasvar *);
340 extern struct mbuf *ipsec_copypkt (struct mbuf *);
341 extern void ipsec_delaux (struct mbuf *);
342 extern int ipsec_addhist (struct mbuf *, int, u_int32_t);
343 extern struct ipsec_history *ipsec_gethist (struct mbuf *, int *);
344 #endif /* _KERNEL */
345
346 #ifndef _KERNEL
347 extern caddr_t ipsec_set_policy (char *, int);
348 extern int ipsec_get_policylen (caddr_t);
349 extern char *ipsec_dump_policy (caddr_t, char *);
350
351 extern const char *ipsec_strerror (void);
352 #endif /* !_KERNEL */
353
354 #endif /* _NETINET6_IPSEC_H_ */