2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * IPsec controller part.
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.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>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
59 #include <net/if_enc.h>
60 #include <net/if_var.h>
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>
73 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
77 #include <netinet/in_pcb.h>
79 #include <netinet/icmp6.h>
82 #include <sys/types.h>
83 #include <netipsec/ipsec.h>
85 #include <netipsec/ipsec6.h>
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>
93 #include <netipsec/key.h>
94 #include <netipsec/keydb.h>
95 #include <netipsec/key_debug.h>
97 #include <netipsec/xform.h>
99 #include <machine/in_cksum.h>
101 #include <opencrypto/cryptodev.h>
103 /* NB: name changed so netstat doesn't use it. */
104 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
105 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
108 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
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;
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)
129 sysctl_def_policy(SYSCTL_HANDLER_ARGS)
133 value = V_def_policy->policy;
134 error = sysctl_handle_int(oidp, &value, 0, req);
136 if (value != IPSEC_POLICY_DISCARD &&
137 value != IPSEC_POLICY_NONE)
139 V_def_policy->policy = value;
145 * Crypto support requirements:
147 * 1 require hardware support
148 * -1 require software support
151 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
154 * Use asynchronous mode to parallelize crypto jobs:
159 VNET_DEFINE(int, async_crypto) = 0;
162 * TCP/UDP checksum handling policy for transport mode NAT-T (RFC3948)
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.
169 VNET_DEFINE(int, natt_cksum_policy) = 0;
171 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
172 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
174 SYSCTL_DECL(_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.");
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.
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");
232 * When set 1, IPsec will send packets with corrupted HMAC.
233 * This allows to verify if the other side properly detects modified packets.
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");
242 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
243 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
246 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
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) */
255 static VNET_DEFINE(int, ip6_filtertunnel) = 0;
256 #define V_ip6_filtertunnel VNET(ip6_filtertunnel)
258 SYSCTL_DECL(_net_inet6_ipsec6);
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.");
286 static int ipsec_in_reject(struct secpolicy *, struct inpcb *,
287 const struct mbuf *);
290 static void ipsec4_get_ulp(const struct mbuf *, struct secpolicyindex *, int);
291 static void ipsec4_setspidx_ipaddr(const struct mbuf *,
292 struct secpolicyindex *);
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 *);
301 * Return a held reference to the default SP.
303 static struct secpolicy *
304 key_allocsp_default(void)
307 key_addref(V_def_policy);
308 return (V_def_policy);
312 ipsec_invalidate_cache(struct inpcb *inp, u_int dir)
314 struct secpolicy *sp;
316 INP_WLOCK_ASSERT(inp);
317 if (dir == IPSEC_DIR_OUTBOUND) {
318 if (inp->inp_sp->flags & INP_INBOUND_POLICY)
320 sp = inp->inp_sp->sp_in;
321 inp->inp_sp->sp_in = NULL;
323 if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
325 sp = inp->inp_sp->sp_out;
326 inp->inp_sp->sp_out = NULL;
329 key_freesp(&sp); /* release extra reference */
333 ipsec_cachepolicy(struct inpcb *inp, struct secpolicy *sp, u_int dir)
338 INP_LOCK_ASSERT(inp);
340 if (dir == IPSEC_DIR_OUTBOUND) {
341 /* Do we have configured PCB policy? */
342 if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
344 /* Another thread has already set cached policy */
345 if (inp->inp_sp->sp_out != NULL)
348 * Do not cache OUTBOUND policy if PCB isn't connected,
349 * i.e. foreign address is INADDR_ANY/UNSPECIFIED.
352 if ((inp->inp_vflag & INP_IPV4) != 0 &&
353 inp->inp_faddr.s_addr == INADDR_ANY)
357 if ((inp->inp_vflag & INP_IPV6) != 0 &&
358 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
362 /* Do we have configured PCB policy? */
363 if (inp->inp_sp->flags & INP_INBOUND_POLICY)
365 /* Another thread has already set cached policy */
366 if (inp->inp_sp->sp_in != NULL)
369 * Do not cache INBOUND policy for listen socket,
370 * that is bound to INADDR_ANY/UNSPECIFIED address.
373 if ((inp->inp_vflag & INP_IPV4) != 0 &&
374 inp->inp_faddr.s_addr == INADDR_ANY)
378 if ((inp->inp_vflag & INP_IPV6) != 0 &&
379 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
384 if (!INP_WLOCKED(inp)) {
385 if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
388 if (dir == IPSEC_DIR_OUTBOUND)
389 inp->inp_sp->sp_out = sp;
391 inp->inp_sp->sp_in = sp;
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()
400 genid = key_getspgen();
401 if (genid != inp->inp_sp->genid) {
402 ipsec_invalidate_cache(inp, dir);
403 inp->inp_sp->genid = genid;
406 printf("%s: PCB(%p): cached %s SP(%p)\n",
407 __func__, inp, dir == IPSEC_DIR_OUTBOUND ? "OUTBOUND":
413 static struct secpolicy *
414 ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error)
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);
421 switch (sp->policy) {
423 printf("%s: invalid policy %u\n", __func__, sp->policy);
425 case IPSEC_POLICY_DISCARD:
426 *error = -EINVAL; /* Packet is discarded by caller. */
428 case IPSEC_POLICY_BYPASS:
429 case IPSEC_POLICY_NONE:
431 sp = NULL; /* NB: force NULL result. */
433 case IPSEC_POLICY_IPSEC:
434 /* XXXAE: handle LARVAL SP */
438 printf("%s: get SP(%p), error %d\n", __func__, sp, *error));
442 static struct secpolicy *
443 ipsec_getpcbpolicy(struct inpcb *inp, u_int dir)
445 struct secpolicy *sp;
446 int flags, downgrade;
448 if (inp == NULL || inp->inp_sp == NULL)
451 INP_LOCK_ASSERT(inp);
453 flags = inp->inp_sp->flags;
454 if (dir == IPSEC_DIR_OUTBOUND) {
455 sp = inp->inp_sp->sp_out;
456 flags &= INP_OUTBOUND_POLICY;
458 sp = inp->inp_sp->sp_in;
459 flags &= INP_INBOUND_POLICY;
462 * Check flags. If we have PCB SP, just return it.
463 * Otherwise we need to check that cached SP entry isn't stale.
468 if (inp->inp_sp->genid != key_getspgen()) {
469 /* Invalidate the cache. */
471 if (!INP_WLOCKED(inp)) {
472 if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
475 ipsec_invalidate_cache(inp, IPSEC_DIR_OUTBOUND);
476 ipsec_invalidate_cache(inp, IPSEC_DIR_INBOUND);
482 printf("%s: PCB(%p): cache hit SP(%p)\n",
484 /* Return referenced cached policy */
492 ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
499 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),
500 ("packet too short"));
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))
506 off = ip->ip_hl << 2;
511 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
512 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
518 while (off < m->m_pkthdr.len) {
525 spidx->ul_proto = nxt;
528 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
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;
535 spidx->ul_proto = nxt;
538 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
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;
545 if (off + sizeof(ip6e) > m->m_pkthdr.len)
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;
554 /* XXX Intermediate headers??? */
555 spidx->ul_proto = nxt;
560 spidx->ul_proto = IPSEC_ULPROTO_ANY;
562 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
563 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
565 printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
569 ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
572 ipsec4_setsockaddrs(m, &spidx->src, &spidx->dst);
573 spidx->prefs = sizeof(struct in_addr) << 3;
574 spidx->prefd = sizeof(struct in_addr) << 3;
577 static struct secpolicy *
578 ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
581 struct secpolicyindex spidx;
582 struct secpolicy *sp;
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);
590 sp = key_allocsp(&spidx, dir);
592 if (sp == NULL) /* No SP found, use system default. */
593 sp = key_allocsp_default();
598 * Check security policy for *OUTBOUND* IPv4 packet.
601 ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
604 struct secpolicy *sp;
607 sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
609 sp = ipsec_checkpolicy(sp, inp, error);
612 case 0: /* No IPsec required: BYPASS or NONE */
615 IPSECSTAT_INC(ips_out_polvio);
618 IPSECSTAT_INC(ips_out_inval);
622 printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
624 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
629 * Check IPv4 packet against *INBOUND* security policy.
630 * This function is called from tcp_input(), udp_input(),
631 * rip_input() and sctp_input().
634 ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp)
636 struct secpolicy *sp;
639 sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
640 result = ipsec_in_reject(sp, inp, m);
643 IPSECSTAT_INC(ips_in_polvio);
648 * IPSEC_CAP() method implementation for IPv4.
651 ipsec4_capability(struct mbuf *m, u_int cap)
655 case IPSEC_CAP_BYPASS_FILTER:
657 * Bypass packet filtering for packets previously handled
660 if (!V_ip4_filtertunnel &&
661 m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
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)
678 ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
686 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip6_hdr),
687 ("packet too short"));
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;
695 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
696 if (off < 0 || m->m_pkthdr.len < off)
701 spidx->ul_proto = nxt;
704 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
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;
711 spidx->ul_proto = nxt;
714 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
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;
721 spidx->ul_proto = nxt;
722 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
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);
729 /* XXX Intermediate headers??? */
730 spidx->ul_proto = nxt;
734 printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
738 ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
741 ipsec6_setsockaddrs(m, &spidx->src, &spidx->dst);
742 spidx->prefs = sizeof(struct in6_addr) << 3;
743 spidx->prefd = sizeof(struct in6_addr) << 3;
746 static struct secpolicy *
747 ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
750 struct secpolicyindex spidx;
751 struct secpolicy *sp;
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);
759 sp = key_allocsp(&spidx, dir);
761 if (sp == NULL) /* No SP found, use system default. */
762 sp = key_allocsp_default();
767 * Check security policy for *OUTBOUND* IPv6 packet.
770 ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
773 struct secpolicy *sp;
776 sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
778 sp = ipsec_checkpolicy(sp, inp, error);
781 case 0: /* No IPsec required: BYPASS or NONE */
784 IPSEC6STAT_INC(ips_out_polvio);
787 IPSEC6STAT_INC(ips_out_inval);
791 printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
793 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
798 * Check IPv6 packet against inbound security policy.
799 * This function is called from tcp6_input(), udp6_input(),
800 * rip6_input() and sctp_input().
803 ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
805 struct secpolicy *sp;
808 sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
809 result = ipsec_in_reject(sp, inp, m);
812 IPSEC6STAT_INC(ips_in_polvio);
817 * IPSEC_CAP() method implementation for IPv6.
820 ipsec6_capability(struct mbuf *m, u_int cap)
824 case IPSEC_CAP_BYPASS_FILTER:
826 * Bypass packet filtering for packets previously handled
829 if (!V_ip6_filtertunnel &&
830 m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
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)
845 ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type)
852 idx = HHOOK_IPSEC_INET;
857 idx = HHOOK_IPSEC_INET6;
861 return (EPFNOSUPPORT);
863 if (type == HHOOK_TYPE_IPSEC_IN)
864 HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL);
866 HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL);
867 if (*ctx->mp == NULL)
873 * Return current level.
874 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
877 ipsec_get_reqlevel(struct secpolicy *sp, u_int idx)
879 struct ipsecrequest *isr;
880 u_int esp_trans_deflev, esp_net_deflev;
881 u_int ah_trans_deflev, ah_net_deflev;
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) \
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))
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.
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);
905 /* Set default level. */
906 switch (sp->spidx.src.sa.sa_family) {
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);
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);
924 panic("%s: unknown af %u",
925 __func__, sp->spidx.src.sa.sa_family);
928 #undef IPSEC_CHECK_DEFAULT
932 switch (isr->level) {
933 case IPSEC_LEVEL_DEFAULT:
934 switch (isr->saidx.proto) {
936 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
937 level = esp_net_deflev;
939 level = esp_trans_deflev;
942 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
943 level = ah_net_deflev;
945 level = ah_trans_deflev;
949 * We don't really care, as IPcomp document says that
950 * we shouldn't compress small packets.
952 level = IPSEC_LEVEL_USE;
955 panic("%s: Illegal protocol defined %u\n", __func__,
960 case IPSEC_LEVEL_USE:
961 case IPSEC_LEVEL_REQUIRE:
964 case IPSEC_LEVEL_UNIQUE:
965 level = IPSEC_LEVEL_REQUIRE;
969 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
976 ipsec_check_history(const struct mbuf *m, struct secpolicy *sp, u_int idx)
978 struct xform_history *xh;
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);
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)
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)
997 * For transport mode IPsec request doesn't contain
998 * addresses. We need to use address from spidx.
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)
1005 if (key_sockaddrcmp(&xh->dst.sa,
1006 &sp->req[idx]->saidx.dst.sa, 0) != 0)
1009 return (0); /* matched */
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".
1025 ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m)
1030 printf("%s: PCB(%p): using SP(%p)\n", __func__, inp, sp));
1031 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1033 if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_in == NULL)
1034 ipsec_cachepolicy(inp, sp, IPSEC_DIR_INBOUND);
1037 switch (sp->policy) {
1038 case IPSEC_POLICY_DISCARD:
1040 case IPSEC_POLICY_BYPASS:
1041 case IPSEC_POLICY_NONE:
1045 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1046 ("invalid policy %u", sp->policy));
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.
1054 for (i = 0; i < sp->tcount; i++) {
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
1061 if (sp->req[i]->saidx.proto == IPPROTO_IPCOMP ||
1062 ipsec_get_reqlevel(sp, i) != IPSEC_LEVEL_REQUIRE)
1064 if (V_check_policy_history != 0 &&
1065 ipsec_check_history(m, sp, i) != 0)
1067 else switch (sp->req[i]->saidx.proto) {
1069 if ((m->m_flags & M_DECRYPTED) == 0) {
1071 printf("%s: ESP m_flags:%x\n", __func__,
1077 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1079 printf("%s: AH m_flags:%x\n", __func__,
1086 return (0); /* Valid. */
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.
1094 ipsec_hdrsiz_internal(struct secpolicy *sp)
1099 KEYDBG(IPSEC_STAMP, printf("%s: using SP(%p)\n", __func__, sp));
1100 KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1102 switch (sp->policy) {
1103 case IPSEC_POLICY_DISCARD:
1104 case IPSEC_POLICY_BYPASS:
1105 case IPSEC_POLICY_NONE:
1109 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1110 ("invalid policy %u", sp->policy));
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.
1118 for (i = 0; i < sp->tcount; i++) {
1119 switch (sp->req[i]->saidx.proto) {
1121 size += esp_hdrsiz(NULL);
1124 size += ah_hdrsiz(NULL);
1126 case IPPROTO_IPCOMP:
1127 size += sizeof(struct ipcomp);
1131 if (sp->req[i]->saidx.mode == IPSEC_MODE_TUNNEL) {
1132 switch (sp->req[i]->saidx.dst.sa.sa_family) {
1135 size += sizeof(struct ip);
1140 size += sizeof(struct ip6_hdr);
1144 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1145 "IPsec tunnel SA\n", __func__,
1146 sp->req[i]->saidx.dst.sa.sa_family));
1155 * Compute ESP/AH header size for protocols with PCB, including
1156 * outer IP header. Currently only tcp_output() uses it.
1159 ipsec_hdrsiz_inpcb(struct inpcb *inp)
1161 struct secpolicyindex spidx;
1162 struct secpolicy *sp;
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);
1171 sp = key_allocsp_default();
1172 sz = ipsec_hdrsiz_internal(sp);
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
1183 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1185 * Based on RFC 6479. Blocks are 32 bits unsigned integers
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)
1194 ipsec_chkreplay(uint32_t seq, struct secasvar *sav)
1196 const struct secreplay *replay;
1197 uint32_t wsizeb; /* Constant: window size. */
1198 int index, bit_location;
1200 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1201 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1203 replay = sav->replay;
1205 /* No need to check replay if disabled. */
1206 if (replay->wsize == 0)
1210 wsizeb = replay->wsize << 3;
1212 /* Sequence number of 0 is invalid. */
1216 /* First time is always okay. */
1217 if (replay->count == 0)
1220 /* Larger sequences are okay. */
1221 if (seq > replay->lastseq)
1224 /* Over range to check, i.e. too old or wrapped. */
1225 if (replay->lastseq - seq >= wsizeb)
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
1232 bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1233 index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS)
1234 & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1236 /* This packet already seen? */
1237 if ((replay->bitmap)[index] & (1 << bit_location))
1243 * Check replay counter whether to update or not.
1248 ipsec_updatereplay(uint32_t seq, struct secasvar *sav)
1251 struct secreplay *replay;
1252 uint32_t wsizeb; /* Constant: window size. */
1253 int diff, index, bit_location;
1255 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1256 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1258 replay = sav->replay;
1260 if (replay->wsize == 0)
1261 goto ok; /* No need to check replay. */
1264 wsizeb = replay->wsize << 3;
1266 /* Sequence number of 0 is invalid. */
1270 /* The packet is too old, no need to update */
1271 if (wsizeb + seq < replay->lastseq)
1274 /* Now update the bit */
1275 index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS);
1277 /* First check if the sequence number is in the range */
1278 if (seq > replay->lastseq) {
1280 int index_cur = replay->lastseq >> IPSEC_REDUNDANT_BIT_SHIFTS;
1282 diff = index - index_cur;
1283 if (diff > replay->bitmap_size) {
1284 /* something unusual in this case */
1285 diff = replay->bitmap_size;
1288 for (id = 0; id < diff; ++id) {
1289 replay->bitmap[(id + index_cur + 1)
1290 & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0;
1293 replay->lastseq = seq;
1296 index &= IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1297 bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1299 /* this packet has already been received */
1300 if (replay->bitmap[index] & (1 << bit_location))
1303 replay->bitmap[index] |= (1 << bit_location);
1306 if (replay->count == ~0) {
1308 /* Set overflow flag. */
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);
1320 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1321 __func__, replay->overflow,
1322 ipsec_sa2str(sav, buf, sizeof(buf))));
1328 ipsec_updateid(struct secasvar *sav, uint64_t *new, uint64_t *old)
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.
1340 * This function will be called when we got EAGAIN from crypto
1342 * *new is cryptoid that was returned by crypto subsystem in
1344 * *old is the original cryptoid that we stored in xform_data.
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.
1353 * XXXAE: check this more carefully.
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));
1360 if (sav->tdb_cryptoid != *old) {
1361 /* cryptoid was already updated */
1363 *new = sav->tdb_cryptoid;
1365 SECASVAR_UNLOCK(sav);
1368 sav->tdb_cryptoid = *new;
1369 SECASVAR_UNLOCK(sav);
1374 ipsec_initialized(void)
1377 return (V_def_policy != NULL);
1381 def_policy_init(const void *unused __unused)
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 */
1390 printf("%s: failed to initialize default policy\n", __func__);
1395 def_policy_uninit(const void *unused __unused)
1398 if (V_def_policy != NULL) {
1399 key_freesp(&V_def_policy);
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);