1171133Sgnn/*- 2171133Sgnn * Copyright (c) 1982, 1986, 1988, 1993 3171133Sgnn * The Regents of the University of California. All rights reserved. 4171133Sgnn * 5171133Sgnn * Redistribution and use in source and binary forms, with or without 6171133Sgnn * modification, are permitted provided that the following conditions 7171133Sgnn * are met: 8171133Sgnn * 1. Redistributions of source code must retain the above copyright 9171133Sgnn * notice, this list of conditions and the following disclaimer. 10171133Sgnn * 2. Redistributions in binary form must reproduce the above copyright 11171133Sgnn * notice, this list of conditions and the following disclaimer in the 12171133Sgnn * documentation and/or other materials provided with the distribution. 13171133Sgnn * 4. Neither the name of the University nor the names of its contributors 14171133Sgnn * may be used to endorse or promote products derived from this software 15171133Sgnn * without specific prior written permission. 16171133Sgnn * 17171133Sgnn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18171133Sgnn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19171133Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20171133Sgnn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21171133Sgnn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22171133Sgnn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23171133Sgnn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24171133Sgnn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25171133Sgnn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26171133Sgnn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27171133Sgnn * SUCH DAMAGE. 28171133Sgnn */ 29171133Sgnn 30174510Sobrien#include <sys/cdefs.h> 31174510Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/netinet6/ip6_ipsec.c 288418 2015-09-30 08:16:33Z ae $"); 32174510Sobrien 33221129Sbz#include "opt_inet.h" 34282575Sae#include "opt_sctp.h" 35171133Sgnn#include "opt_ipsec.h" 36171133Sgnn 37171133Sgnn#include <sys/param.h> 38171133Sgnn#include <sys/systm.h> 39171133Sgnn#include <sys/kernel.h> 40171133Sgnn#include <sys/mac.h> 41171133Sgnn#include <sys/malloc.h> 42171133Sgnn#include <sys/mbuf.h> 43171133Sgnn#include <sys/protosw.h> 44171133Sgnn#include <sys/socket.h> 45171133Sgnn#include <sys/socketvar.h> 46171133Sgnn#include <sys/sysctl.h> 47221129Sbz#include <sys/syslog.h> 48171133Sgnn 49171133Sgnn#include <net/if.h> 50257176Sglebius#include <net/if_var.h> 51195699Srwatson#include <net/vnet.h> 52171133Sgnn 53171133Sgnn#include <netinet/in.h> 54171133Sgnn#include <netinet/in_systm.h> 55171133Sgnn#include <netinet/in_var.h> 56171133Sgnn#include <netinet/ip.h> 57180085Sjulian#include <netinet/ip6.h> 58171133Sgnn#include <netinet/in_pcb.h> 59171133Sgnn#include <netinet/ip_var.h> 60171133Sgnn#include <netinet/ip_options.h> 61282575Sae#ifdef SCTP 62282575Sae#include <netinet/sctp_crc32.h> 63282575Sae#endif 64171133Sgnn 65171133Sgnn#include <machine/in_cksum.h> 66171133Sgnn 67171133Sgnn#include <netipsec/ipsec.h> 68171133Sgnn#include <netipsec/ipsec6.h> 69171133Sgnn#include <netipsec/xform.h> 70171133Sgnn#include <netipsec/key.h> 71171133Sgnn#ifdef IPSEC_DEBUG 72171133Sgnn#include <netipsec/key_debug.h> 73171133Sgnn#else 74171133Sgnn#define KEYDEBUG(lev,arg) 75171133Sgnn#endif 76171133Sgnn 77171133Sgnn#include <netinet6/ip6_ipsec.h> 78180090Sbz#include <netinet6/ip6_var.h> 79171133Sgnn 80171133Sgnnextern struct protosw inet6sw[]; 81171133Sgnn 82195699Srwatson#ifdef IPSEC_FILTERTUNNEL 83215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1; 84195699Srwatson#else 85215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0; 86192648Sbz#endif 87195727Srwatson#define V_ip6_ipsec6_filtertunnel VNET(ip6_ipsec6_filtertunnel) 88192648Sbz 89195699SrwatsonSYSCTL_DECL(_net_inet6_ipsec6); 90274225SglebiusSYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel, 91274225Sglebius CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel), 0, 92195699Srwatson "If set filter packets from an IPsec tunnel."); 93195699Srwatson 94171133Sgnn/* 95171133Sgnn * Check if we have to jump over firewall processing for this packet. 96222845Sbz * Called from ip6_input(). 97171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall. 98171133Sgnn */ 99171133Sgnnint 100171732Sbzip6_ipsec_filtertunnel(struct mbuf *m) 101171133Sgnn{ 102192648Sbz 103171133Sgnn /* 104222845Sbz * Bypass packet filtering for packets previously handled by IPsec. 105171133Sgnn */ 106192648Sbz if (!V_ip6_ipsec6_filtertunnel && 107192648Sbz m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 108282575Sae return (1); 109282575Sae return (0); 110171133Sgnn} 111171133Sgnn 112171133Sgnn/* 113171133Sgnn * Check if this packet has an active SA and needs to be dropped instead 114171133Sgnn * of forwarded. 115275715Sae * Called from ip6_forward(). 116171133Sgnn * 1 = drop packet, 0 = forward packet. 117171133Sgnn */ 118171133Sgnnint 119171133Sgnnip6_ipsec_fwd(struct mbuf *m) 120171133Sgnn{ 121275715Sae 122275715Sae return (ipsec6_in_reject(m, NULL)); 123171133Sgnn} 124171133Sgnn 125171133Sgnn/* 126171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC 127171133Sgnn * decryption or reject right now. Protocols with further headers get 128171133Sgnn * their IPSEC treatment within the protocol specific processing. 129222845Sbz * Called from ip6_input(). 130171133Sgnn * 1 = drop packet, 0 = continue processing packet. 131171133Sgnn */ 132171133Sgnnint 133171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt) 134171133Sgnn{ 135275715Sae 136171133Sgnn /* 137171133Sgnn * enforce IPsec policy checking if we are seeing last header. 138171133Sgnn * note that we do not visit this with protocols with pcb layer 139171133Sgnn * code - like udp/tcp/raw ip. 140171133Sgnn */ 141275715Sae if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0) 142275715Sae return (ipsec6_in_reject(m, NULL)); 143275703Sae return (0); 144171133Sgnn} 145171133Sgnn 146171133Sgnn/* 147171133Sgnn * Called from ip6_output(). 148171133Sgnn * 1 = drop packet, 0 = continue processing packet, 149171260Sdelphij * -1 = packet was reinjected and stop processing packet 150171260Sdelphij */ 151171133Sgnn 152171133Sgnnint 153275710Saeip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *error) 154171133Sgnn{ 155275702Sae struct secpolicy *sp; 156171133Sgnn 157171133Sgnn /* 158275702Sae * Check the security policy (SP) for the packet and, if 159275702Sae * required, do IPsec-related processing. There are two 160275702Sae * cases here; the first time a packet is sent through 161275702Sae * it will be untagged and handled by ipsec4_checkpolicy. 162275702Sae * If the packet is resubmitted to ip6_output (e.g. after 163275702Sae * AH, ESP, etc. processing), there will be a tag to bypass 164275702Sae * the lookup and related policy checking. 165275702Sae */ 166275702Sae if (m_tag_find(*m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) { 167275702Sae *error = 0; 168275702Sae return (0); 169275702Sae } 170275710Sae sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, error, inp); 171275702Sae /* 172171133Sgnn * There are four return cases: 173171260Sdelphij * sp != NULL apply IPsec policy 174171133Sgnn * sp == NULL, error == 0 no IPsec handling needed 175171133Sgnn * sp == NULL, error == -EINVAL discard packet w/o error 176171133Sgnn * sp == NULL, error != 0 discard packet, report error 177171133Sgnn */ 178266800Svanhu if (sp != NULL) { 179171133Sgnn /* 180171133Sgnn * Do delayed checksums now because we send before 181171133Sgnn * this is done in the normal processing path. 182171133Sgnn */ 183238935Sbz#ifdef INET 184171133Sgnn if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 185171133Sgnn in_delayed_cksum(*m); 186171133Sgnn (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 187171133Sgnn } 188238935Sbz#endif 189266800Svanhu if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 190266800Svanhu in6_delayed_cksum(*m, (*m)->m_pkthdr.len - sizeof(struct ip6_hdr), 191266800Svanhu sizeof(struct ip6_hdr)); 192266800Svanhu (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 193266800Svanhu } 194266800Svanhu#ifdef SCTP 195266800Svanhu if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 196266800Svanhu sctp_delayed_cksum(*m, sizeof(struct ip6_hdr)); 197266800Svanhu (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 198266800Svanhu } 199266800Svanhu#endif 200171133Sgnn 201266800Svanhu /* NB: callee frees mbuf */ 202266800Svanhu *error = ipsec6_process_packet(*m, sp->req); 203288418Sae KEY_FREESP(&sp); 204266800Svanhu if (*error == EJUSTRETURN) { 205266800Svanhu /* 206266800Svanhu * We had a SP with a level of 'use' and no SA. We 207266800Svanhu * will just continue to process the packet without 208266800Svanhu * IPsec processing. 209266800Svanhu */ 210266800Svanhu *error = 0; 211266800Svanhu goto done; 212266800Svanhu } 213266800Svanhu 214171133Sgnn /* 215171133Sgnn * Preserve KAME behaviour: ENOENT can be returned 216171133Sgnn * when an SA acquire is in progress. Don't propagate 217171133Sgnn * this to user-level; it confuses applications. 218171133Sgnn * 219171133Sgnn * XXX this will go away when the SADB is redone. 220171133Sgnn */ 221171133Sgnn if (*error == ENOENT) 222171133Sgnn *error = 0; 223266800Svanhu goto reinjected; 224171133Sgnn } else { /* sp == NULL */ 225171133Sgnn if (*error != 0) { 226171133Sgnn /* 227171133Sgnn * Hack: -EINVAL is used to signal that a packet 228171133Sgnn * should be silently discarded. This is typically 229171133Sgnn * because we asked key management for an SA and 230171133Sgnn * it was delayed (e.g. kicked up to IKE). 231171133Sgnn */ 232171133Sgnn if (*error == -EINVAL) 233171133Sgnn *error = 0; 234171133Sgnn goto bad; 235171133Sgnn } 236275702Sae /* No IPsec processing for this packet. */ 237171133Sgnn } 238171133Sgnndone: 239282047Sae return (0); 240266800Svanhureinjected: 241282046Sae return (-1); 242171133Sgnnbad: 243266800Svanhu if (sp != NULL) 244266800Svanhu KEY_FREESP(&sp); 245282575Sae return (1); 246171133Sgnn} 247171133Sgnn 248177166Sbz#if 0 249171133Sgnn/* 250171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated. 251171133Sgnn * Called from ip_forward(). 252171133Sgnn * Returns MTU suggestion for ICMP needfrag reply. 253171133Sgnn */ 254171133Sgnnint 255171133Sgnnip6_ipsec_mtu(struct mbuf *m) 256171133Sgnn{ 257171133Sgnn int mtu = 0; 258171133Sgnn /* 259171133Sgnn * If the packet is routed over IPsec tunnel, tell the 260171133Sgnn * originator the tunnel MTU. 261171133Sgnn * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 262171133Sgnn * XXX quickhack!!! 263171133Sgnn */ 264177166Sbz#ifdef IPSEC 265171133Sgnn struct secpolicy *sp = NULL; 266171133Sgnn int ipsecerror; 267171133Sgnn int ipsechdr; 268171133Sgnn struct route *ro; 269171133Sgnn sp = ipsec_getpolicybyaddr(m, 270171133Sgnn IPSEC_DIR_OUTBOUND, 271171133Sgnn IP_FORWARDING, 272171133Sgnn &ipsecerror); 273171133Sgnn if (sp != NULL) { 274171133Sgnn /* count IPsec header size */ 275188306Sbz ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL); 276171133Sgnn 277171133Sgnn /* 278171133Sgnn * find the correct route for outer IPv4 279171133Sgnn * header, compute tunnel MTU. 280171133Sgnn */ 281171133Sgnn if (sp->req != NULL && 282171133Sgnn sp->req->sav != NULL && 283171133Sgnn sp->req->sav->sah != NULL) { 284214250Sbz ro = &sp->req->sav->sah->route_cache.sa_route; 285171133Sgnn if (ro->ro_rt && ro->ro_rt->rt_ifp) { 286262763Sglebius mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu : 287171133Sgnn ro->ro_rt->rt_ifp->if_mtu; 288171133Sgnn mtu -= ipsechdr; 289171133Sgnn } 290171133Sgnn } 291171133Sgnn KEY_FREESP(&sp); 292177166Sbz } 293171167Sgnn#endif /* IPSEC */ 294177166Sbz /* XXX else case missing. */ 295171133Sgnn return mtu; 296171133Sgnn} 297177166Sbz#endif 298