]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/ipsec.h
Upgrade to OpenSSH 7.4p1.
[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  * 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 _NETIPSEC_IPSEC_H_
38 #define _NETIPSEC_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 <netipsec/keydb.h>
47
48 #ifdef _KERNEL
49
50 #include <sys/_lock.h>
51 #include <sys/_mutex.h>
52 #include <sys/_rwlock.h>
53
54 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
55
56 /*
57  * Security Policy Index
58  * Ensure that both address families in the "src" and "dst" are same.
59  * When the value of the ul_proto is ICMPv6, the port field in "src"
60  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
61  */
62 struct secpolicyindex {
63         union sockaddr_union src;       /* IP src address for SP */
64         union sockaddr_union dst;       /* IP dst address for SP */
65         uint8_t ul_proto;               /* upper layer Protocol */
66         uint8_t dir;                    /* direction of packet flow */
67         uint8_t prefs;                  /* prefix length in bits for src */
68         uint8_t prefd;                  /* prefix length in bits for dst */
69 };
70
71 /* Request for IPsec */
72 struct ipsecrequest {
73         struct secasindex saidx;/* hint for search proper SA */
74                                 /* if __ss_len == 0 then no address specified.*/
75         u_int level;            /* IPsec level defined below. */
76 };
77
78 /* Security Policy Data Base */
79 struct secpolicy {
80         TAILQ_ENTRY(secpolicy) chain;
81         LIST_ENTRY(secpolicy) idhash;
82         LIST_ENTRY(secpolicy) drainq;
83
84         struct secpolicyindex spidx;    /* selector */
85 #define IPSEC_MAXREQ            4
86         struct ipsecrequest *req[IPSEC_MAXREQ];
87         u_int tcount;                   /* IPsec transforms count */
88         volatile u_int refcnt;          /* reference count */
89         u_int policy;                   /* policy_type per pfkeyv2.h */
90         u_int state;
91 #define IPSEC_SPSTATE_DEAD      0
92 #define IPSEC_SPSTATE_LARVAL    1
93 #define IPSEC_SPSTATE_ALIVE     2
94 #define IPSEC_SPSTATE_PCB       3
95 #define IPSEC_SPSTATE_IFNET     4
96         uint32_t priority;              /* priority of this policy */
97         uint32_t id;                    /* It's unique number on the system. */
98         /*
99          * lifetime handler.
100          * the policy can be used without limitiation if both lifetime and
101          * validtime are zero.
102          * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
103          * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
104          */
105         time_t created;         /* time created the policy */
106         time_t lastused;        /* updated every when kernel sends a packet */
107         long lifetime;          /* duration of the lifetime of this policy */
108         long validtime;         /* duration this policy is valid without use */
109 };
110
111 /*
112  * PCB security policies.
113  * Application can setup private security policies for socket.
114  * Such policies can have IPSEC, BYPASS and ENTRUST type.
115  * By default, policies are set to NULL. This means that they have ENTRUST type.
116  * When application sets BYPASS or IPSEC type policy, the flags field
117  * is also updated. When flags is not set, the system could store
118  * used security policy into the sp_in/sp_out pointer to speed up further
119  * lookups.
120  */
121 struct inpcbpolicy {
122         struct secpolicy        *sp_in;
123         struct secpolicy        *sp_out;
124
125         uint32_t                genid;
126         uint16_t                flags;
127 #define INP_INBOUND_POLICY      0x0001
128 #define INP_OUTBOUND_POLICY     0x0002
129         uint16_t                hdrsz;
130 };
131
132 /* SP acquiring list table. */
133 struct secspacq {
134         LIST_ENTRY(secspacq) chain;
135
136         struct secpolicyindex spidx;
137
138         time_t created;         /* for lifetime */
139         int count;              /* for lifetime */
140         /* XXX: here is mbuf place holder to be sent ? */
141 };
142 #endif /* _KERNEL */
143
144 /* buffer size for formatted output of ipsec address */
145 #define IPSEC_ADDRSTRLEN        (INET6_ADDRSTRLEN + 11)
146
147 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
148 #define IPSEC_PORT_ANY          0
149 #define IPSEC_ULPROTO_ANY       255
150 #define IPSEC_PROTO_ANY         255
151
152 /* mode of security protocol */
153 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
154 #define IPSEC_MODE_ANY          0       /* i.e. wildcard. */
155 #define IPSEC_MODE_TRANSPORT    1
156 #define IPSEC_MODE_TUNNEL       2
157 #define IPSEC_MODE_TCPMD5       3       /* TCP MD5 mode */
158
159 /*
160  * Direction of security policy.
161  * NOTE: Since INVALID is used just as flag.
162  * The other are used for loop counter too.
163  */
164 #define IPSEC_DIR_ANY           0
165 #define IPSEC_DIR_INBOUND       1
166 #define IPSEC_DIR_OUTBOUND      2
167 #define IPSEC_DIR_MAX           3
168 #define IPSEC_DIR_INVALID       4
169
170 /* Policy level */
171 /*
172  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
173  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
174  * DISCARD and NONE are allowed for system default.
175  */
176 #define IPSEC_POLICY_DISCARD    0       /* discarding packet */
177 #define IPSEC_POLICY_NONE       1       /* through IPsec engine */
178 #define IPSEC_POLICY_IPSEC      2       /* do IPsec */
179 #define IPSEC_POLICY_ENTRUST    3       /* consulting SPD if present. */
180 #define IPSEC_POLICY_BYPASS     4       /* only for privileged socket. */
181
182 /* Security protocol level */
183 #define IPSEC_LEVEL_DEFAULT     0       /* reference to system default */
184 #define IPSEC_LEVEL_USE         1       /* use SA if present. */
185 #define IPSEC_LEVEL_REQUIRE     2       /* require SA. */
186 #define IPSEC_LEVEL_UNIQUE      3       /* unique SA. */
187
188 #define IPSEC_MANUAL_REQID_MAX  0x3fff
189                                 /*
190                                  * if security policy level == unique, this id
191                                  * indicate to a relative SA for use, else is
192                                  * zero.
193                                  * 1 - 0x3fff are reserved for manual keying.
194                                  * 0 are reserved for above reason.  Others is
195                                  * for kernel use.
196                                  * Note that this id doesn't identify SA
197                                  * by only itself.
198                                  */
199 #define IPSEC_REPLAYWSIZE  32
200
201 /* statistics for ipsec processing */
202 struct ipsecstat {
203         uint64_t ips_in_polvio;         /* input: sec policy violation */
204         uint64_t ips_in_nomem;          /* input: no memory available */
205         uint64_t ips_in_inval;          /* input: generic error */
206
207         uint64_t ips_out_polvio;        /* output: sec policy violation */
208         uint64_t ips_out_nosa;          /* output: SA unavailable  */
209         uint64_t ips_out_nomem;         /* output: no memory available */
210         uint64_t ips_out_noroute;       /* output: no route available */
211         uint64_t ips_out_inval;         /* output: generic error */
212         uint64_t ips_out_bundlesa;      /* output: bundled SA processed */
213
214         uint64_t ips_mbcoalesced;       /* mbufs coalesced during clone */
215         uint64_t ips_clcoalesced;       /* clusters coalesced during clone */
216         uint64_t ips_clcopied;          /* clusters copied during clone */
217         uint64_t ips_mbinserted;        /* mbufs inserted during makespace */
218         /* 
219          * Temporary statistics for performance analysis.
220          */
221         /* See where ESP/AH/IPCOMP header land in mbuf on input */
222         uint64_t ips_input_front;
223         uint64_t ips_input_middle;
224         uint64_t ips_input_end;
225 };
226
227 /*
228  * Definitions for IPsec & Key sysctl operations.
229  */
230 #define IPSECCTL_STATS                  1       /* stats */
231 #define IPSECCTL_DEF_POLICY             2
232 #define IPSECCTL_DEF_ESP_TRANSLEV       3       /* int; ESP transport mode */
233 #define IPSECCTL_DEF_ESP_NETLEV         4       /* int; ESP tunnel mode */
234 #define IPSECCTL_DEF_AH_TRANSLEV        5       /* int; AH transport mode */
235 #define IPSECCTL_DEF_AH_NETLEV          6       /* int; AH tunnel mode */
236 #if 0   /* obsolete, do not reuse */
237 #define IPSECCTL_INBOUND_CALL_IKE       7
238 #endif
239 #define IPSECCTL_AH_CLEARTOS            8
240 #define IPSECCTL_AH_OFFSETMASK          9
241 #define IPSECCTL_DFBIT                  10
242 #define IPSECCTL_ECN                    11
243 #define IPSECCTL_DEBUG                  12
244 #define IPSECCTL_ESP_RANDPAD            13
245
246 #ifdef _KERNEL
247 #include <sys/counter.h>
248
249 struct ipsec_ctx_data;
250 #define IPSEC_INIT_CTX(_ctx, _mp, _sav, _af, _enc) do { \
251         (_ctx)->mp = (_mp);                             \
252         (_ctx)->sav = (_sav);                           \
253         (_ctx)->af = (_af);                             \
254         (_ctx)->enc = (_enc);                           \
255 } while(0)
256 int     ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction);
257
258 VNET_DECLARE(int, ipsec_debug);
259 #define V_ipsec_debug           VNET(ipsec_debug)
260
261 #ifdef REGRESSION
262 VNET_DECLARE(int, ipsec_replay);
263 VNET_DECLARE(int, ipsec_integrity);
264
265 #define V_ipsec_replay          VNET(ipsec_replay)
266 #define V_ipsec_integrity       VNET(ipsec_integrity)
267 #endif
268
269 VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
270 VNET_DECLARE(int, ip4_esp_trans_deflev);
271 VNET_DECLARE(int, ip4_esp_net_deflev);
272 VNET_DECLARE(int, ip4_ah_trans_deflev);
273 VNET_DECLARE(int, ip4_ah_net_deflev);
274 VNET_DECLARE(int, ip4_ah_offsetmask);
275 VNET_DECLARE(int, ip4_ipsec_dfbit);
276 VNET_DECLARE(int, ip4_ipsec_ecn);
277 VNET_DECLARE(int, ip4_esp_randpad);
278 VNET_DECLARE(int, crypto_support);
279 VNET_DECLARE(int, natt_cksum_policy);
280
281 #define IPSECSTAT_INC(name)     \
282     VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
283 #define V_ip4_esp_trans_deflev  VNET(ip4_esp_trans_deflev)
284 #define V_ip4_esp_net_deflev    VNET(ip4_esp_net_deflev)
285 #define V_ip4_ah_trans_deflev   VNET(ip4_ah_trans_deflev)
286 #define V_ip4_ah_net_deflev     VNET(ip4_ah_net_deflev)
287 #define V_ip4_ah_offsetmask     VNET(ip4_ah_offsetmask)
288 #define V_ip4_ipsec_dfbit       VNET(ip4_ipsec_dfbit)
289 #define V_ip4_ipsec_ecn         VNET(ip4_ipsec_ecn)
290 #define V_ip4_esp_randpad       VNET(ip4_esp_randpad)
291 #define V_crypto_support        VNET(crypto_support)
292 #define V_natt_cksum_policy     VNET(natt_cksum_policy)
293
294 #define ipseclog(x)     do { if (V_ipsec_debug) log x; } while (0)
295 /* for openbsd compatibility */
296 #define DPRINTF(x)      do { if (V_ipsec_debug) printf x; } while (0)
297
298 struct inpcb;
299 struct m_tag;
300 struct secasvar;
301 struct sockopt;
302 struct tcphdr;
303 union sockaddr_union;
304
305 int ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t);
306
307 struct ipsecrequest *ipsec_newisr(void);
308 void ipsec_delisr(struct ipsecrequest *);
309 struct secpolicy *ipsec4_checkpolicy(const struct mbuf *, struct inpcb *,
310     int *);
311
312 u_int ipsec_get_reqlevel(struct secpolicy *, u_int);
313
314 void udp_ipsec_adjust_cksum(struct mbuf *, struct secasvar *, int, int);
315 int udp_ipsec_output(struct mbuf *, struct secasvar *);
316 int udp_ipsec_input(struct mbuf *, int, int);
317 int udp_ipsec_pcbctl(struct inpcb *, struct sockopt *);
318
319 int ipsec_chkreplay(uint32_t, struct secasvar *);
320 int ipsec_updatereplay(uint32_t, struct secasvar *);
321 int ipsec_updateid(struct secasvar *, uint64_t *, uint64_t *);
322 int ipsec_initialized(void);
323
324 void ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int);
325
326 void ipsec4_setsockaddrs(const struct mbuf *, union sockaddr_union *,
327     union sockaddr_union *);
328 int ipsec4_in_reject(const struct mbuf *, struct inpcb *);
329 int ipsec4_input(struct mbuf *, int, int);
330 int ipsec4_forward(struct mbuf *);
331 int ipsec4_pcbctl(struct inpcb *, struct sockopt *);
332 int ipsec4_output(struct mbuf *, struct inpcb *);
333 int ipsec4_capability(struct mbuf *, u_int);
334 int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
335 int ipsec4_process_packet(struct mbuf *, struct secpolicy *, struct inpcb *);
336 int ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *,
337     u_int);
338
339 extern  void m_checkalignment(const char* where, struct mbuf *m0,
340                 int off, int len);
341 extern  struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
342 extern  caddr_t m_pad(struct mbuf *m, int n);
343 extern  int m_striphdr(struct mbuf *m, int skip, int hlen);
344
345 #endif /* _KERNEL */
346
347 #ifndef _KERNEL
348 extern caddr_t ipsec_set_policy(char *, int);
349 extern int ipsec_get_policylen(caddr_t);
350 extern char *ipsec_dump_policy(caddr_t, char *);
351 extern const char *ipsec_strerror(void);
352
353 #endif /* ! KERNEL */
354
355 #endif /* _NETIPSEC_IPSEC_H_ */