]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/ipsec.c
MFV r326007: less v529.
[FreeBSD/FreeBSD.git] / sys / netipsec / ipsec.c
1 /*      $FreeBSD$       */
2 /*      $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane 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 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/hhook.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56 #include <sys/proc.h>
57
58 #include <net/if.h>
59 #include <net/if_enc.h>
60 #include <net/if_var.h>
61 #include <net/vnet.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 #include <netinet/tcp.h>
71 #include <netinet/udp.h>
72
73 #include <netinet/ip6.h>
74 #ifdef INET6
75 #include <netinet6/ip6_var.h>
76 #endif
77 #include <netinet/in_pcb.h>
78 #ifdef INET6
79 #include <netinet/icmp6.h>
80 #endif
81
82 #include <sys/types.h>
83 #include <netipsec/ipsec.h>
84 #ifdef INET6
85 #include <netipsec/ipsec6.h>
86 #endif
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp_var.h>
89 #include <netipsec/ipcomp.h>            /*XXX*/
90 #include <netipsec/ipcomp_var.h>
91 #include <netipsec/ipsec_support.h>
92
93 #include <netipsec/key.h>
94 #include <netipsec/keydb.h>
95 #include <netipsec/key_debug.h>
96
97 #include <netipsec/xform.h>
98
99 #include <machine/in_cksum.h>
100
101 #include <opencrypto/cryptodev.h>
102
103 /* NB: name changed so netstat doesn't use it. */
104 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
105 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
106
107 #ifdef VIMAGE
108 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
109 #endif /* VIMAGE */
110
111 VNET_DEFINE(int, ip4_ah_offsetmask) = 0;        /* maybe IP_DF? */
112 /* DF bit on encap. 0: clear 1: set 2: copy */
113 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
114 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
115 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
116 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
117 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
118 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
119 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
120 VNET_DEFINE(int, ip4_esp_randpad) = -1;
121
122 static VNET_DEFINE(int, ip4_filtertunnel) = 0;
123 #define V_ip4_filtertunnel VNET(ip4_filtertunnel)
124 static VNET_DEFINE(int, check_policy_history) = 0;
125 #define V_check_policy_history  VNET(check_policy_history)
126 static VNET_DEFINE(struct secpolicy *, def_policy) = NULL;
127 #define V_def_policy    VNET(def_policy)
128 static int
129 sysctl_def_policy(SYSCTL_HANDLER_ARGS)
130 {
131         int error, value;
132
133         value = V_def_policy->policy;
134         error = sysctl_handle_int(oidp, &value, 0, req);
135         if (error == 0) {
136                 if (value != IPSEC_POLICY_DISCARD &&
137                     value != IPSEC_POLICY_NONE)
138                         return (EINVAL);
139                 V_def_policy->policy = value;
140         }
141         return (error);
142 }
143
144 /*
145  * Crypto support requirements:
146  *
147  *  1   require hardware support
148  * -1   require software support
149  *  0   take anything
150  */
151 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
152
153 /*
154  * Use asynchronous mode to parallelize crypto jobs:
155  *
156  *  0 - disabled
157  *  1 - enabled
158  */
159 VNET_DEFINE(int, async_crypto) = 0;
160
161 /*
162  * TCP/UDP checksum handling policy for transport mode NAT-T (RFC3948)
163  *
164  * 0 - auto: incrementally recompute, when checksum delta is known;
165  *     if checksum delta isn't known, reset checksum to zero for UDP,
166  *     and mark csum_flags as valid for TCP.
167  * 1 - fully recompute TCP/UDP checksum.
168  */
169 VNET_DEFINE(int, natt_cksum_policy) = 0;
170
171 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
172 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
173
174 SYSCTL_DECL(_net_inet_ipsec);
175
176 /* net.inet.ipsec */
177 SYSCTL_PROC(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
178         CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW, 0, 0, sysctl_def_policy, "I",
179         "IPsec default policy.");
180 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
181         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
182         "Default ESP transport mode level");
183 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
184         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
185         "Default ESP tunnel mode level.");
186 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
187         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
188         "AH transfer mode default level.");
189 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
190         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
191         "AH tunnel mode default level.");
192 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
193         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
194         "If set, clear type-of-service field when doing AH computation.");
195 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask,
196         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0,
197         "If not set, clear offset field mask when doing AH computation.");
198 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
199         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
200         "Do not fragment bit on encap.");
201 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
202         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
203         "Explicit Congestion Notification handling.");
204 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
205         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
206         "Crypto driver selection.");
207 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, async_crypto,
208         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(async_crypto), 0,
209         "Use asynchronous mode to parallelize crypto jobs.");
210 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, check_policy_history,
211         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(check_policy_history), 0,
212         "Use strict check of inbound packets to security policy compliance.");
213 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, natt_cksum_policy,
214         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(natt_cksum_policy), 0,
215         "Method to fix TCP/UDP checksum for transport mode IPsec after NAT.");
216 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, filtertunnel,
217         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_filtertunnel), 0,
218         "If set, filter packets from an IPsec tunnel.");
219 SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat,
220     ipsec4stat, "IPsec IPv4 statistics.");
221
222 #ifdef REGRESSION
223 /*
224  * When set to 1, IPsec will send packets with the same sequence number.
225  * This allows to verify if the other side has proper replay attacks detection.
226  */
227 VNET_DEFINE(int, ipsec_replay) = 0;
228 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay,
229         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
230         "Emulate replay attack");
231 /*
232  * When set 1, IPsec will send packets with corrupted HMAC.
233  * This allows to verify if the other side properly detects modified packets.
234  */
235 VNET_DEFINE(int, ipsec_integrity) = 0;
236 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
237         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
238         "Emulate man-in-the-middle attack");
239 #endif
240
241 #ifdef INET6 
242 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
243 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
244
245 #ifdef VIMAGE
246 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
247 #endif /* VIMAGE */
248
249 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
250 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
251 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
252 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
253 VNET_DEFINE(int, ip6_ipsec_ecn) = 0;    /* ECN ignore(-1)/forbidden(0)/allowed(1) */
254
255 static VNET_DEFINE(int, ip6_filtertunnel) = 0;
256 #define V_ip6_filtertunnel      VNET(ip6_filtertunnel)
257
258 SYSCTL_DECL(_net_inet6_ipsec6);
259
260 /* net.inet6.ipsec6 */
261 SYSCTL_PROC(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy,
262         CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW, 0, 0, sysctl_def_policy, "I",
263         "IPsec default policy.");
264 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
265         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
266         "Default ESP transport mode level.");
267 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
268         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0,
269         "Default ESP tunnel mode level.");
270 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
271         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0,
272         "AH transfer mode default level.");
273 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
274         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0,
275         "AH tunnel mode default level.");
276 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn,
277         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0,
278         "Explicit Congestion Notification handling.");
279 SYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel,
280         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_filtertunnel),  0,
281         "If set, filter packets from an IPsec tunnel.");
282 SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats,
283     struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics.");
284 #endif /* INET6 */
285
286 static int ipsec_in_reject(struct secpolicy *, struct inpcb *,
287     const struct mbuf *);
288
289 #ifdef INET
290 static void ipsec4_get_ulp(const struct mbuf *, struct secpolicyindex *, int);
291 static void ipsec4_setspidx_ipaddr(const struct mbuf *,
292     struct secpolicyindex *);
293 #endif
294 #ifdef INET6
295 static void ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *, int);
296 static void ipsec6_setspidx_ipaddr(const struct mbuf *,
297     struct secpolicyindex *);
298 #endif
299
300 /*
301  * Return a held reference to the default SP.
302  */
303 static struct secpolicy *
304 key_allocsp_default(void)
305 {
306
307         key_addref(V_def_policy);
308         return (V_def_policy);
309 }
310
311 static void
312 ipsec_invalidate_cache(struct inpcb *inp, u_int dir)
313 {
314         struct secpolicy *sp;
315
316         INP_WLOCK_ASSERT(inp);
317         if (dir == IPSEC_DIR_OUTBOUND) {
318                 if (inp->inp_sp->flags & INP_INBOUND_POLICY)
319                         return;
320                 sp = inp->inp_sp->sp_in;
321                 inp->inp_sp->sp_in = NULL;
322         } else {
323                 if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
324                         return;
325                 sp = inp->inp_sp->sp_out;
326                 inp->inp_sp->sp_out = NULL;
327         }
328         if (sp != NULL)
329                 key_freesp(&sp); /* release extra reference */
330 }
331
332 static void
333 ipsec_cachepolicy(struct inpcb *inp, struct secpolicy *sp, u_int dir)
334 {
335         uint32_t genid;
336         int downgrade;
337
338         INP_LOCK_ASSERT(inp);
339
340         if (dir == IPSEC_DIR_OUTBOUND) {
341                 /* Do we have configured PCB policy? */
342                 if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
343                         return;
344                 /* Another thread has already set cached policy */
345                 if (inp->inp_sp->sp_out != NULL)
346                         return;
347                 /*
348                  * Do not cache OUTBOUND policy if PCB isn't connected,
349                  * i.e. foreign address is INADDR_ANY/UNSPECIFIED.
350                  */
351 #ifdef INET
352                 if ((inp->inp_vflag & INP_IPV4) != 0 &&
353                     inp->inp_faddr.s_addr == INADDR_ANY)
354                         return;
355 #endif
356 #ifdef INET6
357                 if ((inp->inp_vflag & INP_IPV6) != 0 &&
358                     IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
359                         return;
360 #endif
361         } else {
362                 /* Do we have configured PCB policy? */
363                 if (inp->inp_sp->flags & INP_INBOUND_POLICY)
364                         return;
365                 /* Another thread has already set cached policy */
366                 if (inp->inp_sp->sp_in != NULL)
367                         return;
368                 /*
369                  * Do not cache INBOUND policy for listen socket,
370                  * that is bound to INADDR_ANY/UNSPECIFIED address.
371                  */
372 #ifdef INET
373                 if ((inp->inp_vflag & INP_IPV4) != 0 &&
374                     inp->inp_faddr.s_addr == INADDR_ANY)
375                         return;
376 #endif
377 #ifdef INET6
378                 if ((inp->inp_vflag & INP_IPV6) != 0 &&
379                     IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
380                         return;
381 #endif
382         }
383         downgrade = 0;
384         if (!INP_WLOCKED(inp)) {
385                 if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
386                         return;
387         }
388         if (dir == IPSEC_DIR_OUTBOUND)
389                 inp->inp_sp->sp_out = sp;
390         else
391                 inp->inp_sp->sp_in = sp;
392         /*
393          * SP is already referenced by the lookup code.
394          * We take extra reference here to avoid race in the
395          * ipsec_getpcbpolicy() function - SP will not be freed in the
396          * time between we take SP pointer from the cache and key_addref()
397          * call.
398          */
399         key_addref(sp);
400         genid = key_getspgen();
401         if (genid != inp->inp_sp->genid) {
402                 ipsec_invalidate_cache(inp, dir);
403                 inp->inp_sp->genid = genid;
404         }
405         KEYDBG(IPSEC_STAMP,
406             printf("%s: PCB(%p): cached %s SP(%p)\n",
407             __func__, inp, dir == IPSEC_DIR_OUTBOUND ? "OUTBOUND":
408             "INBOUND", sp));
409         if (downgrade != 0)
410                 INP_DOWNGRADE(inp);
411 }
412
413 static struct secpolicy *
414 ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error)
415 {
416
417         /* Save found OUTBOUND policy into PCB SP cache. */
418         if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_out == NULL)
419                 ipsec_cachepolicy(inp, sp, IPSEC_DIR_OUTBOUND);
420
421         switch (sp->policy) {
422         default:
423                 printf("%s: invalid policy %u\n", __func__, sp->policy);
424                 /* FALLTHROUGH */
425         case IPSEC_POLICY_DISCARD:
426                 *error = -EINVAL;       /* Packet is discarded by caller. */
427                 /* FALLTHROUGH */
428         case IPSEC_POLICY_BYPASS:
429         case IPSEC_POLICY_NONE:
430                 key_freesp(&sp);
431                 sp = NULL;              /* NB: force NULL result. */
432                 break;
433         case IPSEC_POLICY_IPSEC:
434                 /* XXXAE: handle LARVAL SP */
435                 break;
436         }
437         KEYDBG(IPSEC_DUMP,
438             printf("%s: get SP(%p), error %d\n", __func__, sp, *error));
439         return (sp);
440 }
441
442 static struct secpolicy *
443 ipsec_getpcbpolicy(struct inpcb *inp, u_int dir)
444 {
445         struct secpolicy *sp;
446         int flags, downgrade;
447
448         if (inp == NULL || inp->inp_sp == NULL)
449                 return (NULL);
450
451         INP_LOCK_ASSERT(inp);
452
453         flags = inp->inp_sp->flags;
454         if (dir == IPSEC_DIR_OUTBOUND) {
455                 sp = inp->inp_sp->sp_out;
456                 flags &= INP_OUTBOUND_POLICY;
457         } else {
458                 sp = inp->inp_sp->sp_in;
459                 flags &= INP_INBOUND_POLICY;
460         }
461         /*
462          * Check flags. If we have PCB SP, just return it.
463          * Otherwise we need to check that cached SP entry isn't stale.
464          */
465         if (flags == 0) {
466                 if (sp == NULL)
467                         return (NULL);
468                 if (inp->inp_sp->genid != key_getspgen()) {
469                         /* Invalidate the cache. */
470                         downgrade = 0;
471                         if (!INP_WLOCKED(inp)) {
472                                 if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
473                                         return (NULL);
474                         }
475                         ipsec_invalidate_cache(inp, IPSEC_DIR_OUTBOUND);
476                         ipsec_invalidate_cache(inp, IPSEC_DIR_INBOUND);
477                         if (downgrade != 0)
478                                 INP_DOWNGRADE(inp);
479                         return (NULL);
480                 }
481                 KEYDBG(IPSEC_STAMP,
482                     printf("%s: PCB(%p): cache hit SP(%p)\n",
483                     __func__, inp, sp));
484                 /* Return referenced cached policy */
485         }
486         key_addref(sp);
487         return (sp);
488 }
489
490 #ifdef INET
491 static void
492 ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
493     int needport)
494 {
495         uint8_t nxt;
496         int off;
497
498         /* Sanity check. */
499         IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),
500             ("packet too short"));
501
502         if (m->m_len >= sizeof (struct ip)) {
503                 const struct ip *ip = mtod(m, const struct ip *);
504                 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
505                         goto done;
506                 off = ip->ip_hl << 2;
507                 nxt = ip->ip_p;
508         } else {
509                 struct ip ih;
510
511                 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
512                 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
513                         goto done;
514                 off = ih.ip_hl << 2;
515                 nxt = ih.ip_p;
516         }
517
518         while (off < m->m_pkthdr.len) {
519                 struct ip6_ext ip6e;
520                 struct tcphdr th;
521                 struct udphdr uh;
522
523                 switch (nxt) {
524                 case IPPROTO_TCP:
525                         spidx->ul_proto = nxt;
526                         if (!needport)
527                                 goto done_proto;
528                         if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
529                                 goto done;
530                         m_copydata(m, off, sizeof (th), (caddr_t) &th);
531                         spidx->src.sin.sin_port = th.th_sport;
532                         spidx->dst.sin.sin_port = th.th_dport;
533                         return;
534                 case IPPROTO_UDP:
535                         spidx->ul_proto = nxt;
536                         if (!needport)
537                                 goto done_proto;
538                         if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
539                                 goto done;
540                         m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
541                         spidx->src.sin.sin_port = uh.uh_sport;
542                         spidx->dst.sin.sin_port = uh.uh_dport;
543                         return;
544                 case IPPROTO_AH:
545                         if (off + sizeof(ip6e) > m->m_pkthdr.len)
546                                 goto done;
547                         /* XXX Sigh, this works but is totally bogus. */
548                         m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
549                         off += (ip6e.ip6e_len + 2) << 2;
550                         nxt = ip6e.ip6e_nxt;
551                         break;
552                 case IPPROTO_ICMP:
553                 default:
554                         /* XXX Intermediate headers??? */
555                         spidx->ul_proto = nxt;
556                         goto done_proto;
557                 }
558         }
559 done:
560         spidx->ul_proto = IPSEC_ULPROTO_ANY;
561 done_proto:
562         spidx->src.sin.sin_port = IPSEC_PORT_ANY;
563         spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
564         KEYDBG(IPSEC_DUMP,
565             printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
566 }
567
568 static void
569 ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
570 {
571
572         ipsec4_setsockaddrs(m, &spidx->src, &spidx->dst);
573         spidx->prefs = sizeof(struct in_addr) << 3;
574         spidx->prefd = sizeof(struct in_addr) << 3;
575 }
576
577 static struct secpolicy *
578 ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
579     int needport)
580 {
581         struct secpolicyindex spidx;
582         struct secpolicy *sp;
583
584         sp = ipsec_getpcbpolicy(inp, dir);
585         if (sp == NULL && key_havesp(dir)) {
586                 /* Make an index to look for a policy. */
587                 ipsec4_setspidx_ipaddr(m, &spidx);
588                 ipsec4_get_ulp(m, &spidx, needport);
589                 spidx.dir = dir;
590                 sp = key_allocsp(&spidx, dir);
591         }
592         if (sp == NULL)         /* No SP found, use system default. */
593                 sp = key_allocsp_default();
594         return (sp);
595 }
596
597 /*
598  * Check security policy for *OUTBOUND* IPv4 packet.
599  */
600 struct secpolicy *
601 ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
602     int needport)
603 {
604         struct secpolicy *sp;
605
606         *error = 0;
607         sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
608         if (sp != NULL)
609                 sp = ipsec_checkpolicy(sp, inp, error);
610         if (sp == NULL) {
611                 switch (*error) {
612                 case 0: /* No IPsec required: BYPASS or NONE */
613                         break;
614                 case -EINVAL:
615                         IPSECSTAT_INC(ips_out_polvio);
616                         break;
617                 default:
618                         IPSECSTAT_INC(ips_out_inval);
619                 }
620         }
621         KEYDBG(IPSEC_STAMP,
622             printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
623         if (sp != NULL)
624                 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
625         return (sp);
626 }
627
628 /*
629  * Check IPv4 packet against *INBOUND* security policy.
630  * This function is called from tcp_input(), udp_input(),
631  * rip_input() and sctp_input().
632  */
633 int
634 ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp)
635 {
636         struct secpolicy *sp;
637         int result;
638
639         sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
640         result = ipsec_in_reject(sp, inp, m);
641         key_freesp(&sp);
642         if (result != 0)
643                 IPSECSTAT_INC(ips_in_polvio);
644         return (result);
645 }
646
647 /*
648  * IPSEC_CAP() method implementation for IPv4.
649  */
650 int
651 ipsec4_capability(struct mbuf *m, u_int cap)
652 {
653
654         switch (cap) {
655         case IPSEC_CAP_BYPASS_FILTER:
656                 /*
657                  * Bypass packet filtering for packets previously handled
658                  * by IPsec.
659                  */
660                 if (!V_ip4_filtertunnel &&
661                     m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
662                         return (1);
663                 return (0);
664         case IPSEC_CAP_OPERABLE:
665                 /* Do we have active security policies? */
666                 if (key_havesp(IPSEC_DIR_INBOUND) != 0 ||
667                     key_havesp(IPSEC_DIR_OUTBOUND) != 0)
668                         return (1);
669                 return (0);
670         };
671         return (EOPNOTSUPP);
672 }
673
674 #endif /* INET */
675
676 #ifdef INET6
677 static void
678 ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
679     int needport)
680 {
681         struct tcphdr th;
682         struct udphdr uh;
683         struct icmp6_hdr ih;
684         int off, nxt;
685
686         IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip6_hdr),
687             ("packet too short"));
688
689         /* Set default. */
690         spidx->ul_proto = IPSEC_ULPROTO_ANY;
691         spidx->src.sin6.sin6_port = IPSEC_PORT_ANY;
692         spidx->dst.sin6.sin6_port = IPSEC_PORT_ANY;
693
694         nxt = -1;
695         off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
696         if (off < 0 || m->m_pkthdr.len < off)
697                 return;
698
699         switch (nxt) {
700         case IPPROTO_TCP:
701                 spidx->ul_proto = nxt;
702                 if (!needport)
703                         break;
704                 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
705                         break;
706                 m_copydata(m, off, sizeof(th), (caddr_t)&th);
707                 spidx->src.sin6.sin6_port = th.th_sport;
708                 spidx->dst.sin6.sin6_port = th.th_dport;
709                 break;
710         case IPPROTO_UDP:
711                 spidx->ul_proto = nxt;
712                 if (!needport)
713                         break;
714                 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
715                         break;
716                 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
717                 spidx->src.sin6.sin6_port = uh.uh_sport;
718                 spidx->dst.sin6.sin6_port = uh.uh_dport;
719                 break;
720         case IPPROTO_ICMPV6:
721                 spidx->ul_proto = nxt;
722                 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
723                         break;
724                 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
725                 spidx->src.sin6.sin6_port = htons((uint16_t)ih.icmp6_type);
726                 spidx->dst.sin6.sin6_port = htons((uint16_t)ih.icmp6_code);
727                 break;
728         default:
729                 /* XXX Intermediate headers??? */
730                 spidx->ul_proto = nxt;
731                 break;
732         }
733         KEYDBG(IPSEC_DUMP,
734             printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
735 }
736
737 static void
738 ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
739 {
740
741         ipsec6_setsockaddrs(m, &spidx->src, &spidx->dst);
742         spidx->prefs = sizeof(struct in6_addr) << 3;
743         spidx->prefd = sizeof(struct in6_addr) << 3;
744 }
745
746 static struct secpolicy *
747 ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
748     int needport)
749 {
750         struct secpolicyindex spidx;
751         struct secpolicy *sp;
752
753         sp = ipsec_getpcbpolicy(inp, dir);
754         if (sp == NULL && key_havesp(dir)) {
755                 /* Make an index to look for a policy. */
756                 ipsec6_setspidx_ipaddr(m, &spidx);
757                 ipsec6_get_ulp(m, &spidx, needport);
758                 spidx.dir = dir;
759                 sp = key_allocsp(&spidx, dir);
760         }
761         if (sp == NULL)         /* No SP found, use system default. */
762                 sp = key_allocsp_default();
763         return (sp);
764 }
765
766 /*
767  * Check security policy for *OUTBOUND* IPv6 packet.
768  */
769 struct secpolicy *
770 ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
771     int needport)
772 {
773         struct secpolicy *sp;
774
775         *error = 0;
776         sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
777         if (sp != NULL)
778                 sp = ipsec_checkpolicy(sp, inp, error);
779         if (sp == NULL) {
780                 switch (*error) {
781                 case 0: /* No IPsec required: BYPASS or NONE */
782                         break;
783                 case -EINVAL:
784                         IPSEC6STAT_INC(ips_out_polvio);
785                         break;
786                 default:
787                         IPSEC6STAT_INC(ips_out_inval);
788                 }
789         }
790         KEYDBG(IPSEC_STAMP,
791             printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
792         if (sp != NULL)
793                 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
794         return (sp);
795 }
796
797 /*
798  * Check IPv6 packet against inbound security policy.
799  * This function is called from tcp6_input(), udp6_input(),
800  * rip6_input() and sctp_input().
801  */
802 int
803 ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
804 {
805         struct secpolicy *sp;
806         int result;
807
808         sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
809         result = ipsec_in_reject(sp, inp, m);
810         key_freesp(&sp);
811         if (result)
812                 IPSEC6STAT_INC(ips_in_polvio);
813         return (result);
814 }
815
816 /*
817  * IPSEC_CAP() method implementation for IPv6.
818  */
819 int
820 ipsec6_capability(struct mbuf *m, u_int cap)
821 {
822
823         switch (cap) {
824         case IPSEC_CAP_BYPASS_FILTER:
825                 /*
826                  * Bypass packet filtering for packets previously handled
827                  * by IPsec.
828                  */
829                 if (!V_ip6_filtertunnel &&
830                     m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
831                         return (1);
832                 return (0);
833         case IPSEC_CAP_OPERABLE:
834                 /* Do we have active security policies? */
835                 if (key_havesp(IPSEC_DIR_INBOUND) != 0 ||
836                     key_havesp(IPSEC_DIR_OUTBOUND) != 0)
837                         return (1);
838                 return (0);
839         };
840         return (EOPNOTSUPP);
841 }
842 #endif /* INET6 */
843
844 int
845 ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type)
846 {
847         int idx;
848
849         switch (ctx->af) {
850 #ifdef INET
851         case AF_INET:
852                 idx = HHOOK_IPSEC_INET;
853                 break;
854 #endif
855 #ifdef INET6
856         case AF_INET6:
857                 idx = HHOOK_IPSEC_INET6;
858                 break;
859 #endif
860         default:
861                 return (EPFNOSUPPORT);
862         }
863         if (type == HHOOK_TYPE_IPSEC_IN)
864                 HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL);
865         else
866                 HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL);
867         if (*ctx->mp == NULL)
868                 return (EACCES);
869         return (0);
870 }
871
872 /*
873  * Return current level.
874  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
875  */
876 u_int
877 ipsec_get_reqlevel(struct secpolicy *sp, u_int idx)
878 {
879         struct ipsecrequest *isr;
880         u_int esp_trans_deflev, esp_net_deflev;
881         u_int ah_trans_deflev, ah_net_deflev;
882         u_int level = 0;
883
884         IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
885 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
886 #define IPSEC_CHECK_DEFAULT(lev) \
887         (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE &&   \
888           (lev) != IPSEC_LEVEL_UNIQUE)                                  \
889                 ? (V_ipsec_debug  ?                                     \
890                 log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
891                 (lev), IPSEC_LEVEL_REQUIRE) : 0),                       \
892                 (lev) = IPSEC_LEVEL_REQUIRE, (lev) : (lev))
893
894         /*
895          * IPsec VTI uses unique security policy with fake spidx filled
896          * with zeroes. Just return IPSEC_LEVEL_REQUIRE instead of doing
897          * full level lookup for such policies.
898          */
899         if (sp->state == IPSEC_SPSTATE_IFNET) {
900                 IPSEC_ASSERT(sp->req[idx]->level == IPSEC_LEVEL_UNIQUE,
901                     ("Wrong IPsec request level %d", sp->req[idx]->level));
902                 return (IPSEC_LEVEL_REQUIRE);
903         }
904
905         /* Set default level. */
906         switch (sp->spidx.src.sa.sa_family) {
907 #ifdef INET
908         case AF_INET:
909                 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
910                 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
911                 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
912                 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
913                 break;
914 #endif
915 #ifdef INET6
916         case AF_INET6:
917                 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
918                 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
919                 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
920                 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
921                 break;
922 #endif /* INET6 */
923         default:
924                 panic("%s: unknown af %u",
925                         __func__, sp->spidx.src.sa.sa_family);
926         }
927
928 #undef IPSEC_CHECK_DEFAULT
929
930         isr = sp->req[idx];
931         /* Set level. */
932         switch (isr->level) {
933         case IPSEC_LEVEL_DEFAULT:
934                 switch (isr->saidx.proto) {
935                 case IPPROTO_ESP:
936                         if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
937                                 level = esp_net_deflev;
938                         else
939                                 level = esp_trans_deflev;
940                         break;
941                 case IPPROTO_AH:
942                         if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
943                                 level = ah_net_deflev;
944                         else
945                                 level = ah_trans_deflev;
946                         break;
947                 case IPPROTO_IPCOMP:
948                         /*
949                          * We don't really care, as IPcomp document says that
950                          * we shouldn't compress small packets.
951                          */
952                         level = IPSEC_LEVEL_USE;
953                         break;
954                 default:
955                         panic("%s: Illegal protocol defined %u\n", __func__,
956                                 isr->saidx.proto);
957                 }
958                 break;
959
960         case IPSEC_LEVEL_USE:
961         case IPSEC_LEVEL_REQUIRE:
962                 level = isr->level;
963                 break;
964         case IPSEC_LEVEL_UNIQUE:
965                 level = IPSEC_LEVEL_REQUIRE;
966                 break;
967
968         default:
969                 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
970         }
971
972         return (level);
973 }
974
975 static int
976 ipsec_check_history(const struct mbuf *m, struct secpolicy *sp, u_int idx)
977 {
978         struct xform_history *xh;
979         struct m_tag *mtag;
980
981         mtag = NULL;
982         while ((mtag = m_tag_find(__DECONST(struct mbuf *, m),
983             PACKET_TAG_IPSEC_IN_DONE, mtag)) != NULL) {
984                 xh = (struct xform_history *)(mtag + 1);
985                 KEYDBG(IPSEC_DATA,
986                     char buf[IPSEC_ADDRSTRLEN];
987                     printf("%s: mode %s proto %u dst %s\n", __func__,
988                         kdebug_secasindex_mode(xh->mode), xh->proto,
989                         ipsec_address(&xh->dst, buf, sizeof(buf))));
990                 if (xh->proto != sp->req[idx]->saidx.proto)
991                         continue;
992                 /* If SA had IPSEC_MODE_ANY, consider this as match. */
993                 if (xh->mode != sp->req[idx]->saidx.mode &&
994                     xh->mode != IPSEC_MODE_ANY)
995                         continue;
996                 /*
997                  * For transport mode IPsec request doesn't contain
998                  * addresses. We need to use address from spidx.
999                  */
1000                 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TRANSPORT) {
1001                         if (key_sockaddrcmp_withmask(&xh->dst.sa,
1002                             &sp->spidx.dst.sa, sp->spidx.prefd) != 0)
1003                                 continue;
1004                 } else {
1005                         if (key_sockaddrcmp(&xh->dst.sa,
1006                             &sp->req[idx]->saidx.dst.sa, 0) != 0)
1007                                 continue;
1008                 }
1009                 return (0); /* matched */
1010         }
1011         return (1);
1012 }
1013
1014 /*
1015  * Check security policy requirements against the actual
1016  * packet contents.  Return one if the packet should be
1017  * reject as "invalid"; otherwiser return zero to have the
1018  * packet treated as "valid".
1019  *
1020  * OUT:
1021  *      0: valid
1022  *      1: invalid
1023  */
1024 static int
1025 ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m)
1026 {
1027         int i;
1028
1029         KEYDBG(IPSEC_STAMP,
1030             printf("%s: PCB(%p): using SP(%p)\n", __func__, inp, sp));
1031         KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1032
1033         if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_in == NULL)
1034                 ipsec_cachepolicy(inp, sp, IPSEC_DIR_INBOUND);
1035
1036         /* Check policy. */
1037         switch (sp->policy) {
1038         case IPSEC_POLICY_DISCARD:
1039                 return (1);
1040         case IPSEC_POLICY_BYPASS:
1041         case IPSEC_POLICY_NONE:
1042                 return (0);
1043         }
1044
1045         IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1046                 ("invalid policy %u", sp->policy));
1047
1048         /*
1049          * ipsec[46]_common_input_cb after each transform adds
1050          * PACKET_TAG_IPSEC_IN_DONE mbuf tag. It contains SPI, proto, mode
1051          * and destination address from saidx. We can compare info from
1052          * these tags with requirements in SP.
1053          */
1054         for (i = 0; i < sp->tcount; i++) {
1055                 /*
1056                  * Do not check IPcomp, since IPcomp document
1057                  * says that we shouldn't compress small packets.
1058                  * IPComp policy should always be treated as being
1059                  * in "use" level.
1060                  */
1061                 if (sp->req[i]->saidx.proto == IPPROTO_IPCOMP ||
1062                     ipsec_get_reqlevel(sp, i) != IPSEC_LEVEL_REQUIRE)
1063                         continue;
1064                 if (V_check_policy_history != 0 &&
1065                     ipsec_check_history(m, sp, i) != 0)
1066                         return (1);
1067                 else switch (sp->req[i]->saidx.proto) {
1068                 case IPPROTO_ESP:
1069                         if ((m->m_flags & M_DECRYPTED) == 0) {
1070                                 KEYDBG(IPSEC_DUMP,
1071                                     printf("%s: ESP m_flags:%x\n", __func__,
1072                                             m->m_flags));
1073                                 return (1);
1074                         }
1075                         break;
1076                 case IPPROTO_AH:
1077                         if ((m->m_flags & M_AUTHIPHDR) == 0) {
1078                                 KEYDBG(IPSEC_DUMP,
1079                                     printf("%s: AH m_flags:%x\n", __func__,
1080                                             m->m_flags));
1081                                 return (1);
1082                         }
1083                         break;
1084                 }
1085         }
1086         return (0);             /* Valid. */
1087 }
1088
1089 /*
1090  * Compute the byte size to be occupied by IPsec header.
1091  * In case it is tunnelled, it includes the size of outer IP header.
1092  */
1093 static size_t
1094 ipsec_hdrsiz_internal(struct secpolicy *sp)
1095 {
1096         size_t size;
1097         int i;
1098
1099         KEYDBG(IPSEC_STAMP, printf("%s: using SP(%p)\n", __func__, sp));
1100         KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1101
1102         switch (sp->policy) {
1103         case IPSEC_POLICY_DISCARD:
1104         case IPSEC_POLICY_BYPASS:
1105         case IPSEC_POLICY_NONE:
1106                 return (0);
1107         }
1108
1109         IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1110                 ("invalid policy %u", sp->policy));
1111
1112         /*
1113          * XXX: for each transform we need to lookup suitable SA
1114          * and use info from SA to calculate headers size.
1115          * XXX: for NAT-T we need to cosider UDP header size.
1116          */
1117         size = 0;
1118         for (i = 0; i < sp->tcount; i++) {
1119                 switch (sp->req[i]->saidx.proto) {
1120                 case IPPROTO_ESP:
1121                         size += esp_hdrsiz(NULL);
1122                         break;
1123                 case IPPROTO_AH:
1124                         size += ah_hdrsiz(NULL);
1125                         break;
1126                 case IPPROTO_IPCOMP:
1127                         size += sizeof(struct ipcomp);
1128                         break;
1129                 }
1130
1131                 if (sp->req[i]->saidx.mode == IPSEC_MODE_TUNNEL) {
1132                         switch (sp->req[i]->saidx.dst.sa.sa_family) {
1133 #ifdef INET
1134                         case AF_INET:
1135                                 size += sizeof(struct ip);
1136                                 break;
1137 #endif
1138 #ifdef INET6
1139                         case AF_INET6:
1140                                 size += sizeof(struct ip6_hdr);
1141                                 break;
1142 #endif
1143                         default:
1144                                 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1145                                     "IPsec tunnel SA\n", __func__,
1146                                     sp->req[i]->saidx.dst.sa.sa_family));
1147                                 break;
1148                         }
1149                 }
1150         }
1151         return (size);
1152 }
1153
1154 /*
1155  * Compute ESP/AH header size for protocols with PCB, including
1156  * outer IP header. Currently only tcp_output() uses it.
1157  */
1158 size_t
1159 ipsec_hdrsiz_inpcb(struct inpcb *inp)
1160 {
1161         struct secpolicyindex spidx;
1162         struct secpolicy *sp;
1163         size_t sz;
1164
1165         sp = ipsec_getpcbpolicy(inp, IPSEC_DIR_OUTBOUND);
1166         if (sp == NULL && key_havesp(IPSEC_DIR_OUTBOUND)) {
1167                 ipsec_setspidx_inpcb(inp, &spidx, IPSEC_DIR_OUTBOUND);
1168                 sp = key_allocsp(&spidx, IPSEC_DIR_OUTBOUND);
1169         }
1170         if (sp == NULL)
1171                 sp = key_allocsp_default();
1172         sz = ipsec_hdrsiz_internal(sp);
1173         key_freesp(&sp);
1174         return (sz);
1175 }
1176
1177 /*
1178  * Check the variable replay window.
1179  * ipsec_chkreplay() performs replay check before ICV verification.
1180  * ipsec_updatereplay() updates replay bitmap.  This must be called after
1181  * ICV verification (it also performs replay check, which is usually done
1182  * beforehand).
1183  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1184  *
1185  * Based on RFC 6479. Blocks are 32 bits unsigned integers
1186  */
1187
1188 #define IPSEC_BITMAP_INDEX_MASK(w)      (w - 1)
1189 #define IPSEC_REDUNDANT_BIT_SHIFTS      5
1190 #define IPSEC_REDUNDANT_BITS            (1 << IPSEC_REDUNDANT_BIT_SHIFTS)
1191 #define IPSEC_BITMAP_LOC_MASK           (IPSEC_REDUNDANT_BITS - 1)
1192
1193 int
1194 ipsec_chkreplay(uint32_t seq, struct secasvar *sav)
1195 {
1196         const struct secreplay *replay;
1197         uint32_t wsizeb;                /* Constant: window size. */
1198         int index, bit_location;
1199
1200         IPSEC_ASSERT(sav != NULL, ("Null SA"));
1201         IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1202
1203         replay = sav->replay;
1204
1205         /* No need to check replay if disabled. */
1206         if (replay->wsize == 0)
1207                 return (1);
1208
1209         /* Constant. */
1210         wsizeb = replay->wsize << 3;
1211
1212         /* Sequence number of 0 is invalid. */
1213         if (seq == 0)
1214                 return (0);
1215
1216         /* First time is always okay. */
1217         if (replay->count == 0)
1218                 return (1);
1219
1220         /* Larger sequences are okay. */
1221         if (seq > replay->lastseq)
1222                 return (1);
1223
1224         /* Over range to check, i.e. too old or wrapped. */
1225         if (replay->lastseq - seq >= wsizeb)
1226                 return (0);
1227
1228         /* The sequence is inside the sliding window
1229          * now check the bit in the bitmap
1230          * bit location only depends on the sequence number
1231          */
1232         bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1233         index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS)
1234                 & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1235
1236         /* This packet already seen? */
1237         if ((replay->bitmap)[index] & (1 << bit_location))
1238                 return (0);
1239         return (1);
1240 }
1241
1242 /*
1243  * Check replay counter whether to update or not.
1244  * OUT: 0:      OK
1245  *      1:      NG
1246  */
1247 int
1248 ipsec_updatereplay(uint32_t seq, struct secasvar *sav)
1249 {
1250         char buf[128];
1251         struct secreplay *replay;
1252         uint32_t wsizeb;                /* Constant: window size. */
1253         int diff, index, bit_location;
1254
1255         IPSEC_ASSERT(sav != NULL, ("Null SA"));
1256         IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1257
1258         replay = sav->replay;
1259
1260         if (replay->wsize == 0)
1261                 goto ok;        /* No need to check replay. */
1262
1263         /* Constant. */
1264         wsizeb = replay->wsize << 3;
1265
1266         /* Sequence number of 0 is invalid. */
1267         if (seq == 0)
1268                 return (1);
1269
1270         /* The packet is too old, no need to update */
1271         if (wsizeb + seq < replay->lastseq)
1272                 goto ok;
1273
1274         /* Now update the bit */
1275         index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS);
1276
1277         /* First check if the sequence number is in the range */
1278         if (seq > replay->lastseq) {
1279                 int id;
1280                 int index_cur = replay->lastseq >> IPSEC_REDUNDANT_BIT_SHIFTS;
1281
1282                 diff = index - index_cur;
1283                 if (diff > replay->bitmap_size) {
1284                         /* something unusual in this case */
1285                         diff = replay->bitmap_size;
1286                 }
1287
1288                 for (id = 0; id < diff; ++id) {
1289                         replay->bitmap[(id + index_cur + 1)
1290                         & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0;
1291                 }
1292
1293                 replay->lastseq = seq;
1294         }
1295
1296         index &= IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1297         bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1298
1299         /* this packet has already been received */
1300         if (replay->bitmap[index] & (1 << bit_location))
1301                 return (1);
1302
1303         replay->bitmap[index] |= (1 << bit_location);
1304
1305 ok:
1306         if (replay->count == ~0) {
1307
1308                 /* Set overflow flag. */
1309                 replay->overflow++;
1310
1311                 /* Don't increment, no more packets accepted. */
1312                 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1313                         if (sav->sah->saidx.proto == IPPROTO_AH)
1314                                 AHSTAT_INC(ahs_wrap);
1315                         else if (sav->sah->saidx.proto == IPPROTO_ESP)
1316                                 ESPSTAT_INC(esps_wrap);
1317                         return (1);
1318                 }
1319
1320                 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1321                     __func__, replay->overflow,
1322                     ipsec_sa2str(sav, buf, sizeof(buf))));
1323         }
1324         return (0);
1325 }
1326
1327 int
1328 ipsec_updateid(struct secasvar *sav, uint64_t *new, uint64_t *old)
1329 {
1330         uint64_t tmp;
1331
1332         /*
1333          * tdb_cryptoid is initialized by xform_init().
1334          * Then it can be changed only when some crypto error occurred or
1335          * when SA is deleted. We stored used cryptoid in the xform_data
1336          * structure. In case when crypto error occurred and crypto
1337          * subsystem has reinited the session, it returns new cryptoid
1338          * and EAGAIN error code.
1339          *
1340          * This function will be called when we got EAGAIN from crypto
1341          * subsystem.
1342          * *new is cryptoid that was returned by crypto subsystem in
1343          * the crp_sid.
1344          * *old is the original cryptoid that we stored in xform_data.
1345          *
1346          * For first failed request *old == sav->tdb_cryptoid, then
1347          * we update sav->tdb_cryptoid and redo crypto_dispatch().
1348          * For next failed request *old != sav->tdb_cryptoid, then
1349          * we store cryptoid from first request into the *new variable
1350          * and crp_sid from this second session will be returned via
1351          * *old pointer, so caller can release second session.
1352          *
1353          * XXXAE: check this more carefully.
1354          */
1355         KEYDBG(IPSEC_STAMP,
1356             printf("%s: SA(%p) moves cryptoid %jd -> %jd\n",
1357                 __func__, sav, (uintmax_t)(*old), (uintmax_t)(*new)));
1358         KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1359         SECASVAR_LOCK(sav);
1360         if (sav->tdb_cryptoid != *old) {
1361                 /* cryptoid was already updated */
1362                 tmp = *new;
1363                 *new = sav->tdb_cryptoid;
1364                 *old = tmp;
1365                 SECASVAR_UNLOCK(sav);
1366                 return (1);
1367         }
1368         sav->tdb_cryptoid = *new;
1369         SECASVAR_UNLOCK(sav);
1370         return (0);
1371 }
1372
1373 int
1374 ipsec_initialized(void)
1375 {
1376
1377         return (V_def_policy != NULL);
1378 }
1379
1380 static void
1381 def_policy_init(const void *unused __unused)
1382 {
1383
1384         V_def_policy = key_newsp();
1385         if (V_def_policy != NULL) {
1386                 V_def_policy->policy = IPSEC_POLICY_NONE;
1387                 /* Force INPCB SP cache invalidation */
1388                 key_bumpspgen();
1389         } else
1390                 printf("%s: failed to initialize default policy\n", __func__);
1391 }
1392
1393
1394 static void
1395 def_policy_uninit(const void *unused __unused)
1396 {
1397
1398         if (V_def_policy != NULL) {
1399                 key_freesp(&V_def_policy);
1400                 key_bumpspgen();
1401         }
1402 }
1403
1404 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1405     def_policy_init, NULL);
1406 VNET_SYSUNINIT(def_policy_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1407     def_policy_uninit, NULL);