ip6_ipsec.c revision 275703
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: head/sys/netinet6/ip6_ipsec.c 275703 2014-12-11 14:58:55Z ae $"); 32174510Sobrien 33221129Sbz#include "opt_inet.h" 34195699Srwatson#include "opt_inet6.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> 61171133Sgnn 62171133Sgnn#include <machine/in_cksum.h> 63171133Sgnn 64171167Sgnn#ifdef IPSEC 65171133Sgnn#include <netipsec/ipsec.h> 66171133Sgnn#include <netipsec/ipsec6.h> 67171133Sgnn#include <netipsec/xform.h> 68171133Sgnn#include <netipsec/key.h> 69171133Sgnn#ifdef IPSEC_DEBUG 70171133Sgnn#include <netipsec/key_debug.h> 71171133Sgnn#else 72171133Sgnn#define KEYDEBUG(lev,arg) 73171133Sgnn#endif 74171167Sgnn#endif /*IPSEC*/ 75171133Sgnn 76171133Sgnn#include <netinet6/ip6_ipsec.h> 77180090Sbz#include <netinet6/ip6_var.h> 78171133Sgnn 79171133Sgnnextern struct protosw inet6sw[]; 80171133Sgnn 81195699Srwatson 82195699Srwatson#ifdef INET6 83195699Srwatson#ifdef IPSEC 84195699Srwatson#ifdef IPSEC_FILTERTUNNEL 85215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1; 86195699Srwatson#else 87215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0; 88192648Sbz#endif 89195727Srwatson#define V_ip6_ipsec6_filtertunnel VNET(ip6_ipsec6_filtertunnel) 90192648Sbz 91195699SrwatsonSYSCTL_DECL(_net_inet6_ipsec6); 92274225SglebiusSYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel, 93274225Sglebius CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel), 0, 94195699Srwatson "If set filter packets from an IPsec tunnel."); 95195699Srwatson#endif /* IPSEC */ 96195699Srwatson#endif /* INET6 */ 97195699Srwatson 98171133Sgnn/* 99171133Sgnn * Check if we have to jump over firewall processing for this packet. 100222845Sbz * Called from ip6_input(). 101171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall. 102171133Sgnn */ 103171133Sgnnint 104171732Sbzip6_ipsec_filtertunnel(struct mbuf *m) 105171133Sgnn{ 106230442Sbz#ifdef IPSEC 107192648Sbz 108171133Sgnn /* 109222845Sbz * Bypass packet filtering for packets previously handled by IPsec. 110171133Sgnn */ 111192648Sbz if (!V_ip6_ipsec6_filtertunnel && 112192648Sbz m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 113171133Sgnn return 1; 114171133Sgnn#endif 115171133Sgnn return 0; 116171133Sgnn} 117171133Sgnn 118171133Sgnn/* 119171133Sgnn * Check if this packet has an active SA and needs to be dropped instead 120171133Sgnn * of forwarded. 121222845Sbz * Called from ip6_input(). 122171133Sgnn * 1 = drop packet, 0 = forward packet. 123171133Sgnn */ 124171133Sgnnint 125171133Sgnnip6_ipsec_fwd(struct mbuf *m) 126171133Sgnn{ 127171167Sgnn#ifdef IPSEC 128171133Sgnn struct m_tag *mtag; 129171133Sgnn struct tdb_ident *tdbi; 130171133Sgnn struct secpolicy *sp; 131241686Sandre int error; 132171133Sgnn mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 133171133Sgnn if (mtag != NULL) { 134171133Sgnn tdbi = (struct tdb_ident *)(mtag + 1); 135171133Sgnn sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 136171133Sgnn } else { 137171133Sgnn sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 138171260Sdelphij IP_FORWARDING, &error); 139171133Sgnn } 140171133Sgnn if (sp == NULL) { /* NB: can happen if error */ 141171133Sgnn /*XXX error stat???*/ 142222845Sbz DPRINTF(("%s: no SP for forwarding\n", __func__)); /*XXX*/ 143171133Sgnn return 1; 144171133Sgnn } 145171133Sgnn 146171133Sgnn /* 147171133Sgnn * Check security policy against packet attributes. 148171133Sgnn */ 149171133Sgnn error = ipsec_in_reject(sp, m); 150171133Sgnn KEY_FREESP(&sp); 151171133Sgnn if (error) { 152249294Sae IP6STAT_INC(ip6s_cantforward); 153171133Sgnn return 1; 154171133Sgnn } 155171167Sgnn#endif /* IPSEC */ 156171133Sgnn return 0; 157171133Sgnn} 158171133Sgnn 159171133Sgnn/* 160171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC 161171133Sgnn * decryption or reject right now. Protocols with further headers get 162171133Sgnn * their IPSEC treatment within the protocol specific processing. 163222845Sbz * Called from ip6_input(). 164171133Sgnn * 1 = drop packet, 0 = continue processing packet. 165171133Sgnn */ 166171133Sgnnint 167171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt) 168171133Sgnn{ 169171167Sgnn#ifdef IPSEC 170171133Sgnn struct secpolicy *sp; 171241686Sandre int error; 172171133Sgnn /* 173171133Sgnn * enforce IPsec policy checking if we are seeing last header. 174171133Sgnn * note that we do not visit this with protocols with pcb layer 175171133Sgnn * code - like udp/tcp/raw ip. 176171133Sgnn */ 177171133Sgnn if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 178171133Sgnn ipsec6_in_reject(m, NULL)) { 179275703Sae sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 180275703Sae IP_FORWARDING, &error); 181171133Sgnn if (sp != NULL) { 182171133Sgnn /* 183171133Sgnn * Check security policy against packet attributes. 184171133Sgnn */ 185171133Sgnn error = ipsec_in_reject(sp, m); 186171133Sgnn KEY_FREESP(&sp); 187171133Sgnn } else { 188171133Sgnn /* XXX error stat??? */ 189171133Sgnn error = EINVAL; 190222845Sbz DPRINTF(("%s: no SP, packet discarded\n", __func__));/*XXX*/ 191171133Sgnn } 192275703Sae if (error != 0) 193275703Sae return (1); 194171133Sgnn } 195171167Sgnn#endif /* IPSEC */ 196275703Sae return (0); 197171133Sgnn} 198171133Sgnn 199171133Sgnn/* 200171133Sgnn * Called from ip6_output(). 201171133Sgnn * 1 = drop packet, 0 = continue processing packet, 202171260Sdelphij * -1 = packet was reinjected and stop processing packet 203171260Sdelphij */ 204171133Sgnn 205171133Sgnnint 206171133Sgnnip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error, 207266800Svanhu struct ifnet **ifp) 208171133Sgnn{ 209171167Sgnn#ifdef IPSEC 210275702Sae struct secpolicy *sp; 211171133Sgnn 212171133Sgnn /* 213275702Sae * Check the security policy (SP) for the packet and, if 214275702Sae * required, do IPsec-related processing. There are two 215275702Sae * cases here; the first time a packet is sent through 216275702Sae * it will be untagged and handled by ipsec4_checkpolicy. 217275702Sae * If the packet is resubmitted to ip6_output (e.g. after 218275702Sae * AH, ESP, etc. processing), there will be a tag to bypass 219275702Sae * the lookup and related policy checking. 220275702Sae */ 221275702Sae if (m_tag_find(*m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) { 222275702Sae *error = 0; 223275702Sae return (0); 224275702Sae } 225275702Sae sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags, error, inp); 226275702Sae /* 227171133Sgnn * There are four return cases: 228171260Sdelphij * sp != NULL apply IPsec policy 229171133Sgnn * sp == NULL, error == 0 no IPsec handling needed 230171133Sgnn * sp == NULL, error == -EINVAL discard packet w/o error 231171133Sgnn * sp == NULL, error != 0 discard packet, report error 232171133Sgnn */ 233266800Svanhu if (sp != NULL) { 234171133Sgnn /* 235171133Sgnn * Do delayed checksums now because we send before 236171133Sgnn * this is done in the normal processing path. 237171133Sgnn */ 238238935Sbz#ifdef INET 239171133Sgnn if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 240171133Sgnn in_delayed_cksum(*m); 241171133Sgnn (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 242171133Sgnn } 243238935Sbz#endif 244266800Svanhu if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 245266800Svanhu in6_delayed_cksum(*m, (*m)->m_pkthdr.len - sizeof(struct ip6_hdr), 246266800Svanhu sizeof(struct ip6_hdr)); 247266800Svanhu (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 248266800Svanhu } 249266800Svanhu#ifdef SCTP 250266800Svanhu if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 251266800Svanhu sctp_delayed_cksum(*m, sizeof(struct ip6_hdr)); 252266800Svanhu (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 253266800Svanhu } 254266800Svanhu#endif 255171133Sgnn 256266800Svanhu /* NB: callee frees mbuf */ 257266800Svanhu *error = ipsec6_process_packet(*m, sp->req); 258266800Svanhu 259266800Svanhu if (*error == EJUSTRETURN) { 260266800Svanhu /* 261266800Svanhu * We had a SP with a level of 'use' and no SA. We 262266800Svanhu * will just continue to process the packet without 263266800Svanhu * IPsec processing. 264266800Svanhu */ 265266800Svanhu *error = 0; 266266800Svanhu goto done; 267266800Svanhu } 268266800Svanhu 269171133Sgnn /* 270171133Sgnn * Preserve KAME behaviour: ENOENT can be returned 271171133Sgnn * when an SA acquire is in progress. Don't propagate 272171133Sgnn * this to user-level; it confuses applications. 273171133Sgnn * 274171133Sgnn * XXX this will go away when the SADB is redone. 275171133Sgnn */ 276171133Sgnn if (*error == ENOENT) 277171133Sgnn *error = 0; 278266800Svanhu goto reinjected; 279171133Sgnn } else { /* sp == NULL */ 280171133Sgnn if (*error != 0) { 281171133Sgnn /* 282171133Sgnn * Hack: -EINVAL is used to signal that a packet 283171133Sgnn * should be silently discarded. This is typically 284171133Sgnn * because we asked key management for an SA and 285171133Sgnn * it was delayed (e.g. kicked up to IKE). 286171133Sgnn */ 287171133Sgnn if (*error == -EINVAL) 288171133Sgnn *error = 0; 289171133Sgnn goto bad; 290171133Sgnn } 291275702Sae /* No IPsec processing for this packet. */ 292171133Sgnn } 293171133Sgnndone: 294266800Svanhu if (sp != NULL) 295266800Svanhu KEY_FREESP(&sp); 296171133Sgnn return 0; 297266800Svanhureinjected: 298266800Svanhu if (sp != NULL) 299266800Svanhu KEY_FREESP(&sp); 300171133Sgnn return -1; 301171133Sgnnbad: 302266800Svanhu if (sp != NULL) 303266800Svanhu KEY_FREESP(&sp); 304171133Sgnn return 1; 305171167Sgnn#endif /* IPSEC */ 306171133Sgnn return 0; 307171133Sgnn} 308171133Sgnn 309177166Sbz#if 0 310171133Sgnn/* 311171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated. 312171133Sgnn * Called from ip_forward(). 313171133Sgnn * Returns MTU suggestion for ICMP needfrag reply. 314171133Sgnn */ 315171133Sgnnint 316171133Sgnnip6_ipsec_mtu(struct mbuf *m) 317171133Sgnn{ 318171133Sgnn int mtu = 0; 319171133Sgnn /* 320171133Sgnn * If the packet is routed over IPsec tunnel, tell the 321171133Sgnn * originator the tunnel MTU. 322171133Sgnn * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 323171133Sgnn * XXX quickhack!!! 324171133Sgnn */ 325177166Sbz#ifdef IPSEC 326171133Sgnn struct secpolicy *sp = NULL; 327171133Sgnn int ipsecerror; 328171133Sgnn int ipsechdr; 329171133Sgnn struct route *ro; 330171133Sgnn sp = ipsec_getpolicybyaddr(m, 331171133Sgnn IPSEC_DIR_OUTBOUND, 332171133Sgnn IP_FORWARDING, 333171133Sgnn &ipsecerror); 334171133Sgnn if (sp != NULL) { 335171133Sgnn /* count IPsec header size */ 336188306Sbz ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL); 337171133Sgnn 338171133Sgnn /* 339171133Sgnn * find the correct route for outer IPv4 340171133Sgnn * header, compute tunnel MTU. 341171133Sgnn */ 342171133Sgnn if (sp->req != NULL && 343171133Sgnn sp->req->sav != NULL && 344171133Sgnn sp->req->sav->sah != NULL) { 345214250Sbz ro = &sp->req->sav->sah->route_cache.sa_route; 346171133Sgnn if (ro->ro_rt && ro->ro_rt->rt_ifp) { 347262763Sglebius mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu : 348171133Sgnn ro->ro_rt->rt_ifp->if_mtu; 349171133Sgnn mtu -= ipsechdr; 350171133Sgnn } 351171133Sgnn } 352171133Sgnn KEY_FREESP(&sp); 353177166Sbz } 354171167Sgnn#endif /* IPSEC */ 355177166Sbz /* XXX else case missing. */ 356171133Sgnn return mtu; 357171133Sgnn} 358177166Sbz#endif 359