]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ipsec.h
This commit was generated by cvs2svn to compensate for changes in r147353,
[FreeBSD/FreeBSD.git] / sys / netinet6 / ipsec.h
1 /*      $FreeBSD$       */
2 /*      $KAME: ipsec.h,v 1.69 2003/09/10 23:49:11 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
40 #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
41 #include "opt_inet.h"
42 #include "opt_ipsec.h"
43 #endif
44
45 #include <net/pfkeyv2.h>
46 #include <netkey/keydb.h>
47
48 #ifdef _KERNEL
49
50 /*
51  * Security Policy Index
52  * Ensure that both address families in the "src" and "dst" are same.
53  * When the value of the ul_proto is ICMPv6, the port field in "src"
54  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
55  */
56 struct secpolicyindex {
57         struct sockaddr_storage src;    /* IP src address for SP */
58         struct sockaddr_storage dst;    /* IP dst address for SP */
59         u_int8_t prefs;                 /* prefix length in bits for src */
60         u_int8_t prefd;                 /* prefix length in bits for dst */
61         u_int16_t ul_proto;             /* upper layer Protocol */
62 #ifdef notyet
63         uid_t uids;
64         uid_t uidd;
65         gid_t gids;
66         gid_t gidd;
67 #endif
68 };
69
70 /* Security Policy Data Base */
71 struct secpolicy {
72         TAILQ_ENTRY(secpolicy) tailq;   /* all SPD entries, both pcb/table */
73         LIST_ENTRY(secpolicy) chain;    /* SPD entries on table */
74
75         u_int8_t dir;                   /* direction of packet flow */
76         int readonly;                   /* write prohibited */
77         int persist;                    /* will never be removed */
78         int refcnt;                     /* reference count */
79         struct secpolicyindex *spidx;   /* selector - NULL if not valid */
80         u_int32_t id;                   /* it identifies a policy in the SPD. */
81 #define IPSEC_MANUAL_POLICYID_MAX       0x3fff
82                                 /*
83                                  * 1 - 0x3fff are reserved for user operation.
84                                  * 0 are reserved.  Others are for kernel use.
85                                  */
86         struct socket *so;              /* backpointer to per-socket policy */
87         u_int state;                    /* 0: dead, others: alive */
88 #define IPSEC_SPSTATE_DEAD      0
89 #define IPSEC_SPSTATE_ALIVE     1
90
91         int policy;             /* DISCARD, NONE or IPSEC, see below */
92         struct ipsecrequest *req;
93                                 /* pointer to the ipsec request tree, */
94                                 /* if policy == IPSEC else this value == NULL.*/
95
96         /*
97          * lifetime handler.
98          * the policy can be used without limitiation if both lifetime and
99          * validtime are zero.
100          * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
101          * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
102          */
103         long created;           /* time created the policy */
104         long lastused;          /* updated every when kernel sends a packet */
105         long lifetime;          /* duration of the lifetime of this policy */
106         long validtime;         /* duration this policy is valid without use */
107 };
108
109 /* Request for IPsec */
110 struct ifnet;
111 struct ipsecrequest {
112         struct ipsecrequest *next;
113                                 /* pointer to next structure */
114                                 /* If NULL, it means the end of chain. */
115         struct secasindex saidx;/* hint for search proper SA */
116                                 /* if __ss_len == 0 then no address specified.*/
117         u_int level;            /* IPsec level defined below. */
118
119         struct secasvar *sav;   /* place holder of SA for use */
120         struct secpolicy *sp;   /* back pointer to SP */
121
122         struct ifnet *tunifp;   /* interface for tunnelling */
123 };
124
125 /* security policy in PCB */
126 struct inpcbpolicy {
127         struct secpolicy *sp_in;
128         struct secpolicy *sp_out;
129         int priv;                       /* privileged socket ? */
130
131         /* cached policy */
132         /* XXX 3 == IPSEC_DIR_MAX */
133         struct secpolicy *cache[3];
134         struct secpolicyindex cacheidx[3];
135         int cachegen[3];        /* cache generation #, the time we filled it */
136         int cacheflags;
137 #define IPSEC_PCBSP_CONNECTED   1
138 };
139
140 /* SP acquiring list table. */
141 struct secspacq {
142         LIST_ENTRY(secspacq) chain;
143
144         struct secpolicyindex spidx;
145
146         long created;           /* for lifetime */
147         int count;              /* for lifetime */
148         /* XXX: here is mbuf place holder to be sent ? */
149 };
150
151 struct ipsecaux {
152         int hdrs;       /* # of ipsec headers */
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       /* discard the packet */
186 #define IPSEC_POLICY_NONE       1       /* bypass IPsec engine */
187 #define IPSEC_POLICY_IPSEC      2       /* pass to IPsec */
188 #define IPSEC_POLICY_ENTRUST    3       /* consulting SPD if present. */
189 #define IPSEC_POLICY_BYPASS     4       /* only for privileged socket. */
190 #define IPSEC_POLICY_TCP        5       /* TCP MD5 policy */
191
192 /* Security protocol level */
193 #define IPSEC_LEVEL_DEFAULT     0       /* reference to system default */
194 #define IPSEC_LEVEL_USE         1       /* use SA if present. */
195 #define IPSEC_LEVEL_REQUIRE     2       /* require SA. */
196 #define IPSEC_LEVEL_UNIQUE      3       /* unique SA. */
197
198 #define IPSEC_MANUAL_REQID_MAX  0x3fff
199                                 /*
200                                  * if security policy level == unique, this id
201                                  * indicate to a relative SA for use, else is
202                                  * zero.
203                                  * 1 - 0x3fff are reserved for manual keying.
204                                  * 0 are reserved for above reason.  Others is
205                                  * for kernel use.
206                                  * Note that this id doesn't identify SA
207                                  * by only itself.
208                                  */
209 #define IPSEC_REPLAYWSIZE  32
210
211 /* statistics for ipsec processing */
212 struct ipsecstat {
213         u_quad_t in_success;  /* succeeded inbound process */
214         u_quad_t in_polvio;
215                         /* security policy violation for inbound process */
216         u_quad_t in_nosa;     /* inbound SA is unavailable */
217         u_quad_t in_inval;    /* inbound processing failed due to EINVAL */
218         u_quad_t in_nomem;    /* inbound processing failed due to ENOBUFS */
219         u_quad_t in_badspi;   /* failed getting a SPI */
220         u_quad_t in_ahreplay; /* AH replay check failed */
221         u_quad_t in_espreplay; /* ESP replay check failed */
222         u_quad_t in_ahauthsucc; /* AH authentication success */
223         u_quad_t in_ahauthfail; /* AH authentication failure */
224         u_quad_t in_espauthsucc; /* ESP authentication success */
225         u_quad_t in_espauthfail; /* ESP authentication failure */
226         u_quad_t in_esphist[256];
227         u_quad_t in_ahhist[256];
228         u_quad_t in_comphist[256];
229         u_quad_t out_success; /* succeeded outbound process */
230         u_quad_t out_polvio;
231                         /* security policy violation for outbound process */
232         u_quad_t out_nosa;    /* outbound SA is unavailable */
233         u_quad_t out_inval;   /* outbound process failed due to EINVAL */
234         u_quad_t out_nomem;    /* inbound processing failed due to ENOBUFS */
235         u_quad_t out_noroute; /* there is no route */
236         u_quad_t out_esphist[256];
237         u_quad_t out_ahhist[256];
238         u_quad_t out_comphist[256];
239
240         u_quad_t spdcachelookup;
241         u_quad_t spdcachemiss;
242 };
243
244 /*
245  * Definitions for IPsec & Key sysctl operations.
246  */
247 /*
248  * Names for IPsec & Key sysctl objects
249  */
250 #define IPSECCTL_STATS                  1       /* stats */
251 #define IPSECCTL_DEF_POLICY             2
252 #define IPSECCTL_DEF_ESP_TRANSLEV       3       /* int; ESP transport mode */
253 #define IPSECCTL_DEF_ESP_NETLEV         4       /* int; ESP tunnel mode */
254 #define IPSECCTL_DEF_AH_TRANSLEV        5       /* int; AH transport mode */
255 #define IPSECCTL_DEF_AH_NETLEV          6       /* int; AH tunnel mode */
256 #if 0   /* obsolete, do not reuse */
257 #define IPSECCTL_INBOUND_CALL_IKE       7
258 #endif
259 #define IPSECCTL_AH_CLEARTOS            8
260 #define IPSECCTL_AH_OFFSETMASK          9
261 #define IPSECCTL_DFBIT                  10
262 #define IPSECCTL_ECN                    11
263 #define IPSECCTL_DEBUG                  12
264 #define IPSECCTL_ESP_RANDPAD            13
265 #define IPSECCTL_MAXID                  14
266
267 #define IPSECCTL_NAMES { \
268         { 0, 0 }, \
269         { 0, 0 }, \
270         { "def_policy", CTLTYPE_INT }, \
271         { "esp_trans_deflev", CTLTYPE_INT }, \
272         { "esp_net_deflev", CTLTYPE_INT }, \
273         { "ah_trans_deflev", CTLTYPE_INT }, \
274         { "ah_net_deflev", CTLTYPE_INT }, \
275         { 0, 0 }, \
276         { "ah_cleartos", CTLTYPE_INT }, \
277         { "ah_offsetmask", CTLTYPE_INT }, \
278         { "dfbit", CTLTYPE_INT }, \
279         { "ecn", CTLTYPE_INT }, \
280         { "debug", CTLTYPE_INT }, \
281         { "esp_randpad", CTLTYPE_INT }, \
282 }
283
284 #define IPSEC6CTL_NAMES { \
285         { 0, 0 }, \
286         { 0, 0 }, \
287         { "def_policy", CTLTYPE_INT }, \
288         { "esp_trans_deflev", CTLTYPE_INT }, \
289         { "esp_net_deflev", CTLTYPE_INT }, \
290         { "ah_trans_deflev", CTLTYPE_INT }, \
291         { "ah_net_deflev", CTLTYPE_INT }, \
292         { 0, 0 }, \
293         { 0, 0 }, \
294         { 0, 0 }, \
295         { 0, 0 }, \
296         { "ecn", CTLTYPE_INT }, \
297         { "debug", CTLTYPE_INT }, \
298         { "esp_randpad", CTLTYPE_INT }, \
299 }
300
301 #ifdef _KERNEL
302 struct ipsec_output_state {
303         struct mbuf *m;
304         struct route *ro;
305         struct sockaddr *dst;
306         int encap;
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 #ifdef INET
317 extern struct ipsecstat ipsecstat;
318 extern struct secpolicy *ip4_def_policy;
319 extern int ip4_esp_trans_deflev;
320 extern int ip4_esp_net_deflev;
321 extern int ip4_ah_trans_deflev;
322 extern int ip4_ah_net_deflev;
323 extern int ip4_ah_cleartos;
324 extern int ip4_ah_offsetmask;
325 extern int ip4_ipsec_dfbit;
326 extern int ip4_ipsec_ecn;
327 extern int ip4_esp_randpad;
328 #endif
329
330 #define ipseclog(x)     do { if (ipsec_debug) log x; } while (/*CONSTCOND*/ 0)
331
332 extern int ipsec_pcbconn __P((struct inpcbpolicy *));
333 extern int ipsec_pcbdisconn __P((struct inpcbpolicy *));
334 extern int ipsec_invalpcbcacheall __P((void));
335
336 struct inpcb;
337 extern struct secpolicy *ipsec4_getpolicybypcb
338         __P((struct mbuf *, u_int, struct inpcb *, int *));
339 extern struct secpolicy *ipsec4_getpolicybyaddr
340         __P((struct mbuf *, u_int, int, int *));
341
342 extern int ipsec_init_pcbpolicy __P((struct socket *, struct inpcbpolicy **));
343 extern int ipsec_copy_pcbpolicy
344         __P((struct inpcbpolicy *, struct inpcbpolicy *));
345 extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *, int));
346
347 extern int ipsec4_set_policy __P((struct inpcb *, int, caddr_t, size_t, int));
348 extern int ipsec4_get_policy __P((struct inpcb *, caddr_t, size_t,
349         struct mbuf **));
350 extern int ipsec4_delete_pcbpolicy __P((struct inpcb *));
351 extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
352
353 struct secas;
354 struct tcpcb;
355 struct tcp6cb;
356 extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
357 extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
358
359 extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
360 extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
361
362 struct ip;
363 extern const char *ipsec4_logpacketstr __P((struct ip *, u_int32_t));
364 extern const char *ipsec_logsastr __P((struct secasvar *));
365
366 extern void ipsec_dumpmbuf __P((struct mbuf *));
367
368 extern int ipsec4_output __P((struct ipsec_output_state *, struct secpolicy *,
369         int));
370 extern int ipsec4_tunnel_validate __P((struct mbuf *, int, u_int,
371         struct secasvar *));
372 extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
373 extern void ipsec_delaux __P((struct mbuf *));
374 extern int ipsec_addhist __P((struct mbuf *, int, u_int32_t));
375 extern int ipsec_getnhist __P((struct mbuf *));
376 extern void ipsec_clearhist __P((struct mbuf *));
377
378 #endif /* _KERNEL */
379
380 #ifndef _KERNEL
381 extern caddr_t ipsec_set_policy __P((char *, int));
382 extern int ipsec_get_policylen __P((caddr_t));
383 extern char *ipsec_dump_policy __P((caddr_t, char *));
384
385 extern const char *ipsec_strerror __P((void));
386 #endif /* !_KERNEL */
387
388 #endif /* _NETINET6_IPSEC_H_ */