]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/ipsec.h
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / sys / netipsec / ipsec.h
1 /*      $FreeBSD$       */
2 /*      $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $  */
3
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * IPsec controller part.
37  */
38
39 #ifndef _NETIPSEC_IPSEC_H_
40 #define _NETIPSEC_IPSEC_H_
41
42 #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
43 #include "opt_inet.h"
44 #include "opt_ipsec.h"
45 #endif
46
47 #include <net/pfkeyv2.h>
48 #include <netipsec/keydb.h>
49
50 #ifdef _KERNEL
51
52 #include <sys/_lock.h>
53 #include <sys/_mutex.h>
54 #include <sys/_rwlock.h>
55
56 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
57
58 /*
59  * Security Policy Index
60  * Ensure that both address families in the "src" and "dst" are same.
61  * When the value of the ul_proto is ICMPv6, the port field in "src"
62  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
63  */
64 struct secpolicyindex {
65         union sockaddr_union src;       /* IP src address for SP */
66         union sockaddr_union dst;       /* IP dst address for SP */
67         uint8_t ul_proto;               /* upper layer Protocol */
68         uint8_t dir;                    /* direction of packet flow */
69         uint8_t prefs;                  /* prefix length in bits for src */
70         uint8_t prefd;                  /* prefix length in bits for dst */
71 };
72
73 /* Request for IPsec */
74 struct ipsecrequest {
75         struct secasindex saidx;/* hint for search proper SA */
76                                 /* if __ss_len == 0 then no address specified.*/
77         u_int level;            /* IPsec level defined below. */
78 };
79
80 /* Security Policy Data Base */
81 struct secpolicy {
82         TAILQ_ENTRY(secpolicy) chain;
83         LIST_ENTRY(secpolicy) idhash;
84         LIST_ENTRY(secpolicy) drainq;
85
86         struct secpolicyindex spidx;    /* selector */
87 #define IPSEC_MAXREQ            4
88         struct ipsecrequest *req[IPSEC_MAXREQ];
89         u_int tcount;                   /* IPsec transforms count */
90         volatile u_int refcnt;          /* reference count */
91         u_int policy;                   /* policy_type per pfkeyv2.h */
92         u_int state;
93 #define IPSEC_SPSTATE_DEAD      0
94 #define IPSEC_SPSTATE_LARVAL    1
95 #define IPSEC_SPSTATE_ALIVE     2
96 #define IPSEC_SPSTATE_PCB       3
97 #define IPSEC_SPSTATE_IFNET     4
98         uint32_t priority;              /* priority of this policy */
99         uint32_t id;                    /* It's unique number on the system. */
100         /*
101          * lifetime handler.
102          * the policy can be used without limitiation if both lifetime and
103          * validtime are zero.
104          * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
105          * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
106          */
107         time_t created;         /* time created the policy */
108         time_t lastused;        /* updated every when kernel sends a packet */
109         long lifetime;          /* duration of the lifetime of this policy */
110         long validtime;         /* duration this policy is valid without use */
111 };
112
113 /*
114  * PCB security policies.
115  * Application can setup private security policies for socket.
116  * Such policies can have IPSEC, BYPASS and ENTRUST type.
117  * By default, policies are set to NULL. This means that they have ENTRUST type.
118  * When application sets BYPASS or IPSEC type policy, the flags field
119  * is also updated. When flags is not set, the system could store
120  * used security policy into the sp_in/sp_out pointer to speed up further
121  * lookups.
122  */
123 struct inpcbpolicy {
124         struct secpolicy        *sp_in;
125         struct secpolicy        *sp_out;
126
127         uint32_t                genid;
128         uint16_t                flags;
129 #define INP_INBOUND_POLICY      0x0001
130 #define INP_OUTBOUND_POLICY     0x0002
131         uint16_t                hdrsz;
132 };
133
134 /* SP acquiring list table. */
135 struct secspacq {
136         LIST_ENTRY(secspacq) chain;
137
138         struct secpolicyindex spidx;
139
140         time_t created;         /* for lifetime */
141         int count;              /* for lifetime */
142         /* XXX: here is mbuf place holder to be sent ? */
143 };
144 #endif /* _KERNEL */
145
146 /* buffer size for formatted output of ipsec address */
147 #define IPSEC_ADDRSTRLEN        (INET6_ADDRSTRLEN + 11)
148
149 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
150 #define IPSEC_PORT_ANY          0
151 #define IPSEC_ULPROTO_ANY       255
152 #define IPSEC_PROTO_ANY         255
153
154 /* mode of security protocol */
155 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
156 #define IPSEC_MODE_ANY          0       /* i.e. wildcard. */
157 #define IPSEC_MODE_TRANSPORT    1
158 #define IPSEC_MODE_TUNNEL       2
159 #define IPSEC_MODE_TCPMD5       3       /* TCP MD5 mode */
160
161 /*
162  * Direction of security policy.
163  * NOTE: Since INVALID is used just as flag.
164  * The other are used for loop counter too.
165  */
166 #define IPSEC_DIR_ANY           0
167 #define IPSEC_DIR_INBOUND       1
168 #define IPSEC_DIR_OUTBOUND      2
169 #define IPSEC_DIR_MAX           3
170 #define IPSEC_DIR_INVALID       4
171
172 /* Policy level */
173 /*
174  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
175  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
176  * DISCARD and NONE are allowed for system default.
177  */
178 #define IPSEC_POLICY_DISCARD    0       /* discarding packet */
179 #define IPSEC_POLICY_NONE       1       /* through IPsec engine */
180 #define IPSEC_POLICY_IPSEC      2       /* do IPsec */
181 #define IPSEC_POLICY_ENTRUST    3       /* consulting SPD if present. */
182 #define IPSEC_POLICY_BYPASS     4       /* only for privileged socket. */
183
184 /* Policy scope */
185 #define IPSEC_POLICYSCOPE_ANY           0x00    /* unspecified */
186 #define IPSEC_POLICYSCOPE_GLOBAL        0x01    /* global scope */
187 #define IPSEC_POLICYSCOPE_IFNET         0x02    /* if_ipsec(4) scope */
188 #define IPSEC_POLICYSCOPE_PCB           0x04    /* PCB scope */
189
190 /* Security protocol level */
191 #define IPSEC_LEVEL_DEFAULT     0       /* reference to system default */
192 #define IPSEC_LEVEL_USE         1       /* use SA if present. */
193 #define IPSEC_LEVEL_REQUIRE     2       /* require SA. */
194 #define IPSEC_LEVEL_UNIQUE      3       /* unique SA. */
195
196 #define IPSEC_MANUAL_REQID_MAX  0x3fff
197                                 /*
198                                  * if security policy level == unique, this id
199                                  * indicate to a relative SA for use, else is
200                                  * zero.
201                                  * 1 - 0x3fff are reserved for manual keying.
202                                  * 0 are reserved for above reason.  Others is
203                                  * for kernel use.
204                                  * Note that this id doesn't identify SA
205                                  * by only itself.
206                                  */
207 #define IPSEC_REPLAYWSIZE  32
208
209 /* statistics for ipsec processing */
210 struct ipsecstat {
211         uint64_t ips_in_polvio;         /* input: sec policy violation */
212         uint64_t ips_in_nomem;          /* input: no memory available */
213         uint64_t ips_in_inval;          /* input: generic error */
214
215         uint64_t ips_out_polvio;        /* output: sec policy violation */
216         uint64_t ips_out_nosa;          /* output: SA unavailable  */
217         uint64_t ips_out_nomem;         /* output: no memory available */
218         uint64_t ips_out_noroute;       /* output: no route available */
219         uint64_t ips_out_inval;         /* output: generic error */
220         uint64_t ips_out_bundlesa;      /* output: bundled SA processed */
221
222         uint64_t ips_spdcache_hits;     /* SPD cache hits */
223         uint64_t ips_spdcache_misses;   /* SPD cache misses */
224
225         uint64_t ips_clcopied;          /* clusters copied during clone */
226         uint64_t ips_mbinserted;        /* mbufs inserted during makespace */
227         /* 
228          * Temporary statistics for performance analysis.
229          */
230         /* See where ESP/AH/IPCOMP header land in mbuf on input */
231         uint64_t ips_input_front;
232         uint64_t ips_input_middle;
233         uint64_t ips_input_end;
234 };
235
236 /*
237  * Definitions for IPsec & Key sysctl operations.
238  */
239 #define IPSECCTL_STATS                  1       /* stats */
240 #define IPSECCTL_DEF_POLICY             2
241 #define IPSECCTL_DEF_ESP_TRANSLEV       3       /* int; ESP transport mode */
242 #define IPSECCTL_DEF_ESP_NETLEV         4       /* int; ESP tunnel mode */
243 #define IPSECCTL_DEF_AH_TRANSLEV        5       /* int; AH transport mode */
244 #define IPSECCTL_DEF_AH_NETLEV          6       /* int; AH tunnel mode */
245 #if 0   /* obsolete, do not reuse */
246 #define IPSECCTL_INBOUND_CALL_IKE       7
247 #endif
248 #define IPSECCTL_AH_CLEARTOS            8
249 #define IPSECCTL_AH_OFFSETMASK          9
250 #define IPSECCTL_DFBIT                  10
251 #define IPSECCTL_ECN                    11
252 #define IPSECCTL_DEBUG                  12
253 #define IPSECCTL_ESP_RANDPAD            13
254
255 #ifdef _KERNEL
256 #include <sys/counter.h>
257
258 struct ipsec_ctx_data;
259 #define IPSEC_INIT_CTX(_ctx, _mp, _inp, _sav, _af, _enc) do {   \
260         (_ctx)->mp = (_mp);                             \
261         (_ctx)->inp = (_inp);                           \
262         (_ctx)->sav = (_sav);                           \
263         (_ctx)->af = (_af);                             \
264         (_ctx)->enc = (_enc);                           \
265 } while(0)
266 int     ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction);
267
268 VNET_DECLARE(int, ipsec_debug);
269 #define V_ipsec_debug           VNET(ipsec_debug)
270
271 #ifdef REGRESSION
272 VNET_DECLARE(int, ipsec_replay);
273 VNET_DECLARE(int, ipsec_integrity);
274
275 #define V_ipsec_replay          VNET(ipsec_replay)
276 #define V_ipsec_integrity       VNET(ipsec_integrity)
277 #endif
278
279 VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
280 VNET_DECLARE(int, ip4_esp_trans_deflev);
281 VNET_DECLARE(int, ip4_esp_net_deflev);
282 VNET_DECLARE(int, ip4_ah_trans_deflev);
283 VNET_DECLARE(int, ip4_ah_net_deflev);
284 VNET_DECLARE(int, ip4_ipsec_dfbit);
285 VNET_DECLARE(int, ip4_ipsec_ecn);
286 VNET_DECLARE(int, crypto_support);
287 VNET_DECLARE(int, async_crypto);
288 VNET_DECLARE(int, natt_cksum_policy);
289
290 extern struct timeval ipsec_warn_interval;
291
292 #define IPSECSTAT_INC(name)     \
293     VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
294 #define V_ip4_esp_trans_deflev  VNET(ip4_esp_trans_deflev)
295 #define V_ip4_esp_net_deflev    VNET(ip4_esp_net_deflev)
296 #define V_ip4_ah_trans_deflev   VNET(ip4_ah_trans_deflev)
297 #define V_ip4_ah_net_deflev     VNET(ip4_ah_net_deflev)
298 #define V_ip4_ipsec_dfbit       VNET(ip4_ipsec_dfbit)
299 #define V_ip4_ipsec_ecn         VNET(ip4_ipsec_ecn)
300 #define V_crypto_support        VNET(crypto_support)
301 #define V_async_crypto          VNET(async_crypto)
302 #define V_natt_cksum_policy     VNET(natt_cksum_policy)
303
304 #define ipseclog(x)     do { if (V_ipsec_debug) log x; } while (0)
305 /* for openbsd compatibility */
306 #ifdef IPSEC_DEBUG
307 #define IPSEC_DEBUG_DECLARE(x)  x
308 #define DPRINTF(x)      do { if (V_ipsec_debug) printf x; } while (0)
309 #else
310 #define IPSEC_DEBUG_DECLARE(x)
311 #define DPRINTF(x)
312 #endif
313
314 struct inpcb;
315 struct m_tag;
316 struct secasvar;
317 struct sockopt;
318 struct tcphdr;
319 union sockaddr_union;
320
321 int ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t);
322
323 struct ipsecrequest *ipsec_newisr(void);
324 void ipsec_delisr(struct ipsecrequest *);
325 struct secpolicy *ipsec4_checkpolicy(const struct mbuf *, struct inpcb *,
326     int *, int);
327
328 u_int ipsec_get_reqlevel(struct secpolicy *, u_int);
329
330 void udp_ipsec_adjust_cksum(struct mbuf *, struct secasvar *, int, int);
331 int udp_ipsec_output(struct mbuf *, struct secasvar *);
332 int udp_ipsec_input(struct mbuf *, int, int);
333 int udp_ipsec_pcbctl(struct inpcb *, struct sockopt *);
334
335 int ipsec_chkreplay(uint32_t, struct secasvar *);
336 int ipsec_updatereplay(uint32_t, struct secasvar *);
337 int ipsec_updateid(struct secasvar *, crypto_session_t *, crypto_session_t *);
338 int ipsec_initialized(void);
339
340 void ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int);
341
342 void ipsec4_setsockaddrs(const struct mbuf *, union sockaddr_union *,
343     union sockaddr_union *);
344 int ipsec4_in_reject(const struct mbuf *, struct inpcb *);
345 int ipsec4_input(struct mbuf *, int, int);
346 int ipsec4_forward(struct mbuf *);
347 int ipsec4_pcbctl(struct inpcb *, struct sockopt *);
348 int ipsec4_output(struct mbuf *, struct inpcb *);
349 int ipsec4_capability(struct mbuf *, u_int);
350 int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
351 int ipsec4_process_packet(struct mbuf *, struct secpolicy *, struct inpcb *);
352 int ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *,
353     u_int);
354
355 extern  void m_checkalignment(const char* where, struct mbuf *m0,
356                 int off, int len);
357 extern  struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
358 extern  caddr_t m_pad(struct mbuf *m, int n);
359 extern  int m_striphdr(struct mbuf *m, int skip, int hlen);
360
361 #endif /* _KERNEL */
362
363 #ifndef _KERNEL
364 extern caddr_t ipsec_set_policy(char *, int);
365 extern int ipsec_get_policylen(caddr_t);
366 extern char *ipsec_dump_policy(caddr_t, char *);
367 extern const char *ipsec_strerror(void);
368
369 #endif /* ! KERNEL */
370
371 #endif /* _NETIPSEC_IPSEC_H_ */