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$"); 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> 50195699Srwatson#include <net/vnet.h> 51171133Sgnn 52171133Sgnn#include <netinet/in.h> 53171133Sgnn#include <netinet/in_systm.h> 54171133Sgnn#include <netinet/in_var.h> 55171133Sgnn#include <netinet/ip.h> 56180085Sjulian#include <netinet/ip6.h> 57171133Sgnn#include <netinet/in_pcb.h> 58171133Sgnn#include <netinet/ip_var.h> 59171133Sgnn#include <netinet/ip_options.h> 60171133Sgnn 61171133Sgnn#include <machine/in_cksum.h> 62171133Sgnn 63171167Sgnn#ifdef IPSEC 64171133Sgnn#include <netipsec/ipsec.h> 65171133Sgnn#include <netipsec/ipsec6.h> 66171133Sgnn#include <netipsec/xform.h> 67171133Sgnn#include <netipsec/key.h> 68171133Sgnn#ifdef IPSEC_DEBUG 69171133Sgnn#include <netipsec/key_debug.h> 70171133Sgnn#else 71171133Sgnn#define KEYDEBUG(lev,arg) 72171133Sgnn#endif 73171167Sgnn#endif /*IPSEC*/ 74171133Sgnn 75171133Sgnn#include <netinet6/ip6_ipsec.h> 76180090Sbz#include <netinet6/ip6_var.h> 77171133Sgnn 78171133Sgnnextern struct protosw inet6sw[]; 79171133Sgnn 80195699Srwatson 81195699Srwatson#ifdef INET6 82195699Srwatson#ifdef IPSEC 83195699Srwatson#ifdef IPSEC_FILTERTUNNEL 84215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1; 85195699Srwatson#else 86215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0; 87192648Sbz#endif 88195727Srwatson#define V_ip6_ipsec6_filtertunnel VNET(ip6_ipsec6_filtertunnel) 89192648Sbz 90195699SrwatsonSYSCTL_DECL(_net_inet6_ipsec6); 91195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ipsec6, OID_AUTO, 92195699Srwatson filtertunnel, CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel), 0, 93195699Srwatson "If set filter packets from an IPsec tunnel."); 94195699Srwatson#endif /* IPSEC */ 95195699Srwatson#endif /* INET6 */ 96195699Srwatson 97171133Sgnn/* 98171133Sgnn * Check if we have to jump over firewall processing for this packet. 99222845Sbz * Called from ip6_input(). 100171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall. 101171133Sgnn */ 102171133Sgnnint 103171732Sbzip6_ipsec_filtertunnel(struct mbuf *m) 104171133Sgnn{ 105230442Sbz#ifdef IPSEC 106192648Sbz 107171133Sgnn /* 108222845Sbz * Bypass packet filtering for packets previously handled by IPsec. 109171133Sgnn */ 110192648Sbz if (!V_ip6_ipsec6_filtertunnel && 111192648Sbz m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 112171133Sgnn return 1; 113171133Sgnn#endif 114171133Sgnn return 0; 115171133Sgnn} 116171133Sgnn 117171133Sgnn/* 118171133Sgnn * Check if this packet has an active SA and needs to be dropped instead 119171133Sgnn * of forwarded. 120222845Sbz * Called from ip6_input(). 121171133Sgnn * 1 = drop packet, 0 = forward packet. 122171133Sgnn */ 123171133Sgnnint 124171133Sgnnip6_ipsec_fwd(struct mbuf *m) 125171133Sgnn{ 126171167Sgnn#ifdef IPSEC 127171133Sgnn struct m_tag *mtag; 128171133Sgnn struct tdb_ident *tdbi; 129171133Sgnn struct secpolicy *sp; 130241686Sandre int error; 131171133Sgnn mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 132171133Sgnn if (mtag != NULL) { 133171133Sgnn tdbi = (struct tdb_ident *)(mtag + 1); 134171133Sgnn sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 135171133Sgnn } else { 136171133Sgnn sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 137171260Sdelphij IP_FORWARDING, &error); 138171133Sgnn } 139171133Sgnn if (sp == NULL) { /* NB: can happen if error */ 140171133Sgnn /*XXX error stat???*/ 141222845Sbz DPRINTF(("%s: no SP for forwarding\n", __func__)); /*XXX*/ 142171133Sgnn return 1; 143171133Sgnn } 144171133Sgnn 145171133Sgnn /* 146171133Sgnn * Check security policy against packet attributes. 147171133Sgnn */ 148171133Sgnn error = ipsec_in_reject(sp, m); 149171133Sgnn KEY_FREESP(&sp); 150171133Sgnn if (error) { 151249294Sae IP6STAT_INC(ip6s_cantforward); 152171133Sgnn return 1; 153171133Sgnn } 154171167Sgnn#endif /* IPSEC */ 155171133Sgnn return 0; 156171133Sgnn} 157171133Sgnn 158171133Sgnn/* 159171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC 160171133Sgnn * decryption or reject right now. Protocols with further headers get 161171133Sgnn * their IPSEC treatment within the protocol specific processing. 162222845Sbz * Called from ip6_input(). 163171133Sgnn * 1 = drop packet, 0 = continue processing packet. 164171133Sgnn */ 165171133Sgnnint 166171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt) 167171133Sgnn{ 168171167Sgnn#ifdef IPSEC 169171133Sgnn struct m_tag *mtag; 170171133Sgnn struct tdb_ident *tdbi; 171171133Sgnn struct secpolicy *sp; 172241686Sandre int error; 173171133Sgnn /* 174171133Sgnn * enforce IPsec policy checking if we are seeing last header. 175171133Sgnn * note that we do not visit this with protocols with pcb layer 176171133Sgnn * code - like udp/tcp/raw ip. 177171133Sgnn */ 178171133Sgnn if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 179171133Sgnn ipsec6_in_reject(m, NULL)) { 180171133Sgnn 181171133Sgnn /* 182171133Sgnn * Check if the packet has already had IPsec processing 183171133Sgnn * done. If so, then just pass it along. This tag gets 184171133Sgnn * set during AH, ESP, etc. input handling, before the 185171133Sgnn * packet is returned to the ip input queue for delivery. 186171260Sdelphij */ 187171133Sgnn mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 188171133Sgnn if (mtag != NULL) { 189171133Sgnn tdbi = (struct tdb_ident *)(mtag + 1); 190171133Sgnn sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 191171133Sgnn } else { 192171133Sgnn sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 193171260Sdelphij IP_FORWARDING, &error); 194171133Sgnn } 195171133Sgnn if (sp != NULL) { 196171133Sgnn /* 197171133Sgnn * Check security policy against packet attributes. 198171133Sgnn */ 199171133Sgnn error = ipsec_in_reject(sp, m); 200171133Sgnn KEY_FREESP(&sp); 201171133Sgnn } else { 202171133Sgnn /* XXX error stat??? */ 203171133Sgnn error = EINVAL; 204222845Sbz DPRINTF(("%s: no SP, packet discarded\n", __func__));/*XXX*/ 205171133Sgnn return 1; 206171133Sgnn } 207171133Sgnn if (error) 208171133Sgnn return 1; 209171133Sgnn } 210171167Sgnn#endif /* IPSEC */ 211171133Sgnn return 0; 212171133Sgnn} 213171133Sgnn 214171133Sgnn/* 215171133Sgnn * Called from ip6_output(). 216171133Sgnn * 1 = drop packet, 0 = continue processing packet, 217171260Sdelphij * -1 = packet was reinjected and stop processing packet 218171260Sdelphij */ 219171133Sgnn 220171133Sgnnint 221171133Sgnnip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error, 222274132Sae struct ifnet **ifp) 223171133Sgnn{ 224171167Sgnn#ifdef IPSEC 225274132Sae struct secpolicy *sp = NULL; 226171133Sgnn struct tdb_ident *tdbi; 227171133Sgnn struct m_tag *mtag; 228171237Speter /* XXX int s; */ 229171133Sgnn mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 230171133Sgnn if (mtag != NULL) { 231171133Sgnn tdbi = (struct tdb_ident *)(mtag + 1); 232274132Sae sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 233274132Sae if (sp == NULL) 234171133Sgnn *error = -EINVAL; /* force silent drop */ 235171133Sgnn m_tag_delete(*m, mtag); 236171133Sgnn } else { 237274132Sae sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags, 238171133Sgnn error, inp); 239171133Sgnn } 240171133Sgnn 241171133Sgnn /* 242171133Sgnn * There are four return cases: 243171260Sdelphij * sp != NULL apply IPsec policy 244171133Sgnn * sp == NULL, error == 0 no IPsec handling needed 245171133Sgnn * sp == NULL, error == -EINVAL discard packet w/o error 246171133Sgnn * sp == NULL, error != 0 discard packet, report error 247171133Sgnn */ 248274132Sae if (sp != NULL) { 249171133Sgnn /* Loop detection, check if ipsec processing already done */ 250274132Sae KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 251171133Sgnn for (mtag = m_tag_first(*m); mtag != NULL; 252171133Sgnn mtag = m_tag_next(*m, mtag)) { 253171133Sgnn if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 254171133Sgnn continue; 255171133Sgnn if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 256171133Sgnn mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 257171133Sgnn continue; 258171133Sgnn /* 259238877Sbz * Check if policy has no SA associated with it. 260171133Sgnn * This can happen when an SP has yet to acquire 261171133Sgnn * an SA; e.g. on first reference. If it occurs, 262171133Sgnn * then we let ipsec4_process_packet do its thing. 263171133Sgnn */ 264274132Sae if (sp->req->sav == NULL) 265171133Sgnn break; 266171133Sgnn tdbi = (struct tdb_ident *)(mtag + 1); 267274132Sae if (tdbi->spi == sp->req->sav->spi && 268274132Sae tdbi->proto == sp->req->sav->sah->saidx.proto && 269274132Sae bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 270171133Sgnn sizeof (union sockaddr_union)) == 0) { 271171133Sgnn /* 272171133Sgnn * No IPsec processing is needed, free 273171133Sgnn * reference to SP. 274171133Sgnn */ 275171133Sgnn goto done; 276171133Sgnn } 277171133Sgnn } 278171133Sgnn 279171133Sgnn /* 280171133Sgnn * Do delayed checksums now because we send before 281171133Sgnn * this is done in the normal processing path. 282171133Sgnn */ 283238935Sbz#ifdef INET 284171133Sgnn if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 285171133Sgnn in_delayed_cksum(*m); 286171133Sgnn (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 287171133Sgnn } 288238935Sbz#endif 289274132Sae if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 290274132Sae in6_delayed_cksum(*m, (*m)->m_pkthdr.len - sizeof(struct ip6_hdr), 291274132Sae sizeof(struct ip6_hdr)); 292274132Sae (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 293274132Sae } 294274132Sae#ifdef SCTP 295274132Sae if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 296274132Sae sctp_delayed_cksum(*m, sizeof(struct ip6_hdr)); 297274132Sae (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 298274132Sae } 299274132Sae#endif 300171133Sgnn 301274132Sae /* NB: callee frees mbuf */ 302274132Sae *error = ipsec6_process_packet(*m, sp->req); 303274132Sae 304274132Sae if (*error == EJUSTRETURN) { 305274132Sae /* 306274132Sae * We had a SP with a level of 'use' and no SA. We 307274132Sae * will just continue to process the packet without 308274132Sae * IPsec processing. 309274132Sae */ 310274132Sae *error = 0; 311274132Sae goto done; 312274132Sae } 313274132Sae 314171133Sgnn /* 315171133Sgnn * Preserve KAME behaviour: ENOENT can be returned 316171133Sgnn * when an SA acquire is in progress. Don't propagate 317171133Sgnn * this to user-level; it confuses applications. 318171133Sgnn * 319171133Sgnn * XXX this will go away when the SADB is redone. 320171133Sgnn */ 321171133Sgnn if (*error == ENOENT) 322171133Sgnn *error = 0; 323274132Sae goto reinjected; 324171133Sgnn } else { /* sp == NULL */ 325171133Sgnn if (*error != 0) { 326171133Sgnn /* 327171133Sgnn * Hack: -EINVAL is used to signal that a packet 328171133Sgnn * should be silently discarded. This is typically 329171133Sgnn * because we asked key management for an SA and 330171133Sgnn * it was delayed (e.g. kicked up to IKE). 331171133Sgnn */ 332171133Sgnn if (*error == -EINVAL) 333171133Sgnn *error = 0; 334171133Sgnn goto bad; 335171133Sgnn } else { 336171133Sgnn /* No IPsec processing for this packet. */ 337171133Sgnn } 338171133Sgnn } 339171133Sgnndone: 340274132Sae if (sp != NULL) 341274132Sae KEY_FREESP(&sp); 342171133Sgnn return 0; 343274132Saereinjected: 344274132Sae if (sp != NULL) 345274132Sae KEY_FREESP(&sp); 346171133Sgnn return -1; 347171133Sgnnbad: 348274132Sae if (sp != NULL) 349274132Sae KEY_FREESP(&sp); 350171133Sgnn return 1; 351171167Sgnn#endif /* IPSEC */ 352171133Sgnn return 0; 353171133Sgnn} 354171133Sgnn 355177166Sbz#if 0 356171133Sgnn/* 357171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated. 358171133Sgnn * Called from ip_forward(). 359171133Sgnn * Returns MTU suggestion for ICMP needfrag reply. 360171133Sgnn */ 361171133Sgnnint 362171133Sgnnip6_ipsec_mtu(struct mbuf *m) 363171133Sgnn{ 364171133Sgnn int mtu = 0; 365171133Sgnn /* 366171133Sgnn * If the packet is routed over IPsec tunnel, tell the 367171133Sgnn * originator the tunnel MTU. 368171133Sgnn * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 369171133Sgnn * XXX quickhack!!! 370171133Sgnn */ 371177166Sbz#ifdef IPSEC 372171133Sgnn struct secpolicy *sp = NULL; 373171133Sgnn int ipsecerror; 374171133Sgnn int ipsechdr; 375171133Sgnn struct route *ro; 376171133Sgnn sp = ipsec_getpolicybyaddr(m, 377171133Sgnn IPSEC_DIR_OUTBOUND, 378171133Sgnn IP_FORWARDING, 379171133Sgnn &ipsecerror); 380171133Sgnn if (sp != NULL) { 381171133Sgnn /* count IPsec header size */ 382188306Sbz ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL); 383171133Sgnn 384171133Sgnn /* 385171133Sgnn * find the correct route for outer IPv4 386171133Sgnn * header, compute tunnel MTU. 387171133Sgnn */ 388171133Sgnn if (sp->req != NULL && 389171133Sgnn sp->req->sav != NULL && 390171133Sgnn sp->req->sav->sah != NULL) { 391214250Sbz ro = &sp->req->sav->sah->route_cache.sa_route; 392171133Sgnn if (ro->ro_rt && ro->ro_rt->rt_ifp) { 393263478Sglebius mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu : 394171133Sgnn ro->ro_rt->rt_ifp->if_mtu; 395171133Sgnn mtu -= ipsechdr; 396171133Sgnn } 397171133Sgnn } 398171133Sgnn KEY_FREESP(&sp); 399177166Sbz } 400171167Sgnn#endif /* IPSEC */ 401177166Sbz /* XXX else case missing. */ 402171133Sgnn return mtu; 403171133Sgnn} 404177166Sbz#endif 405