ipsec_input.c revision 249294
1105197Ssam/* $FreeBSD: head/sys/netipsec/ipsec_input.c 249294 2013-04-09 07:11:22Z ae $ */ 2112758Ssam/* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */ 3139823Simp/*- 4112758Ssam * The authors of this code are John Ioannidis (ji@tla.org), 5112758Ssam * Angelos D. Keromytis (kermit@csd.uch.gr) and 6112758Ssam * Niels Provos (provos@physnet.uni-hamburg.de). 7112758Ssam * 8112758Ssam * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 9112758Ssam * in November 1995. 10112758Ssam * 11112758Ssam * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12112758Ssam * by Angelos D. Keromytis. 13112758Ssam * 14112758Ssam * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15112758Ssam * and Niels Provos. 16112758Ssam * 17112758Ssam * Additional features in 1999 by Angelos D. Keromytis. 18112758Ssam * 19112758Ssam * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20112758Ssam * Angelos D. Keromytis and Niels Provos. 21112758Ssam * Copyright (c) 2001, Angelos D. Keromytis. 22112758Ssam * 23112758Ssam * Permission to use, copy, and modify this software with or without fee 24112758Ssam * is hereby granted, provided that this entire notice is included in 25112758Ssam * all copies of any software which is or includes a copy or 26112758Ssam * modification of this software. 27112758Ssam * You may use this code under the GNU public license if you so wish. Please 28112758Ssam * contribute changes back to the authors under this freer than GPL license 29112758Ssam * so that we may further the use of strong encryption without limitations to 30112758Ssam * all. 31112758Ssam * 32112758Ssam * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 33112758Ssam * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 34112758Ssam * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 35112758Ssam * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 36112758Ssam * PURPOSE. 37112758Ssam */ 38105197Ssam 39105197Ssam/* 40105197Ssam * IPsec input processing. 41105197Ssam */ 42105197Ssam 43105197Ssam#include "opt_inet.h" 44105197Ssam#include "opt_inet6.h" 45105197Ssam#include "opt_ipsec.h" 46159965Sthompsa#include "opt_enc.h" 47105197Ssam 48105197Ssam#include <sys/param.h> 49105197Ssam#include <sys/systm.h> 50105197Ssam#include <sys/malloc.h> 51105197Ssam#include <sys/mbuf.h> 52105197Ssam#include <sys/domain.h> 53105197Ssam#include <sys/protosw.h> 54105197Ssam#include <sys/socket.h> 55105197Ssam#include <sys/errno.h> 56105197Ssam#include <sys/syslog.h> 57105197Ssam 58105197Ssam#include <net/if.h> 59171497Sbz#include <net/pfil.h> 60105197Ssam#include <net/route.h> 61105197Ssam#include <net/netisr.h> 62195699Srwatson#include <net/vnet.h> 63105197Ssam 64105197Ssam#include <netinet/in.h> 65105197Ssam#include <netinet/in_systm.h> 66105197Ssam#include <netinet/ip.h> 67105197Ssam#include <netinet/ip_var.h> 68105197Ssam#include <netinet/in_var.h> 69105197Ssam 70105197Ssam#include <netinet/ip6.h> 71105197Ssam#ifdef INET6 72105197Ssam#include <netinet6/ip6_var.h> 73105197Ssam#endif 74105197Ssam#include <netinet/in_pcb.h> 75105197Ssam#ifdef INET6 76105197Ssam#include <netinet/icmp6.h> 77105197Ssam#endif 78105197Ssam 79105197Ssam#include <netipsec/ipsec.h> 80105197Ssam#ifdef INET6 81105197Ssam#include <netipsec/ipsec6.h> 82105197Ssam#endif 83105197Ssam#include <netipsec/ah_var.h> 84105197Ssam#include <netipsec/esp.h> 85105197Ssam#include <netipsec/esp_var.h> 86105197Ssam#include <netipsec/ipcomp_var.h> 87105197Ssam 88105197Ssam#include <netipsec/key.h> 89105197Ssam#include <netipsec/keydb.h> 90105197Ssam 91105197Ssam#include <netipsec/xform.h> 92105197Ssam#include <netinet6/ip6protosw.h> 93105197Ssam 94105197Ssam#include <machine/in_cksum.h> 95105197Ssam#include <machine/stdarg.h> 96105197Ssam 97181627Svanhu#ifdef DEV_ENC 98181627Svanhu#include <net/if_enc.h> 99181627Svanhu#endif 100181627Svanhu 101181627Svanhu 102105197Ssam#define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \ 103105197Ssam (p) == IPPROTO_AH ? (y)++ : (z)++) 104105197Ssam 105193947Sbz#ifdef INET 106120585Ssamstatic void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int); 107193947Sbz#endif 108120585Ssam 109105197Ssam/* 110105197Ssam * ipsec_common_input gets called when an IPsec-protected packet 111214351Sthomas * is received by IPv4 or IPv6. Its job is to find the right SA 112170793Sbz * and call the appropriate transform. The transform callback 113105197Ssam * takes care of further processing (like ingress filtering). 114105197Ssam */ 115105197Ssamstatic int 116105197Ssamipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto) 117105197Ssam{ 118105197Ssam union sockaddr_union dst_address; 119105197Ssam struct secasvar *sav; 120105197Ssam u_int32_t spi; 121119643Ssam int error; 122221129Sbz#ifdef INET 123194062Svanhu#ifdef IPSEC_NAT_T 124194062Svanhu struct m_tag *tag; 125194062Svanhu#endif 126221129Sbz#endif 127105197Ssam 128181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_input, V_ahstat.ahs_input, 129181803Sbz V_ipcompstat.ipcomps_input); 130105197Ssam 131120585Ssam IPSEC_ASSERT(m != NULL, ("null packet")); 132105197Ssam 133170792Sbz IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 134170792Sbz sproto == IPPROTO_IPCOMP, 135170792Sbz ("unexpected security protocol %u", sproto)); 136170792Sbz 137181803Sbz if ((sproto == IPPROTO_ESP && !V_esp_enable) || 138181803Sbz (sproto == IPPROTO_AH && !V_ah_enable) || 139181803Sbz (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 140105197Ssam m_freem(m); 141181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, V_ahstat.ahs_pdrops, 142181803Sbz V_ipcompstat.ipcomps_pdrops); 143105197Ssam return EOPNOTSUPP; 144105197Ssam } 145105197Ssam 146105197Ssam if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) { 147105197Ssam m_freem(m); 148181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops, 149181803Sbz V_ipcompstat.ipcomps_hdrops); 150120585Ssam DPRINTF(("%s: packet too small\n", __func__)); 151105197Ssam return EINVAL; 152105197Ssam } 153105197Ssam 154105197Ssam /* Retrieve the SPI from the relevant IPsec header */ 155105197Ssam if (sproto == IPPROTO_ESP) 156105197Ssam m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi); 157105197Ssam else if (sproto == IPPROTO_AH) 158105197Ssam m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), 159105197Ssam (caddr_t) &spi); 160105197Ssam else if (sproto == IPPROTO_IPCOMP) { 161105197Ssam u_int16_t cpi; 162105197Ssam m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), 163105197Ssam (caddr_t) &cpi); 164105197Ssam spi = ntohl(htons(cpi)); 165105197Ssam } 166105197Ssam 167105197Ssam /* 168105197Ssam * Find the SA and (indirectly) call the appropriate 169105197Ssam * kernel crypto routine. The resulting mbuf chain is a valid 170105197Ssam * IP packet ready to go through input processing. 171105197Ssam */ 172105197Ssam bzero(&dst_address, sizeof (dst_address)); 173105197Ssam dst_address.sa.sa_family = af; 174105197Ssam switch (af) { 175105197Ssam#ifdef INET 176105197Ssam case AF_INET: 177105197Ssam dst_address.sin.sin_len = sizeof(struct sockaddr_in); 178105197Ssam m_copydata(m, offsetof(struct ip, ip_dst), 179105197Ssam sizeof(struct in_addr), 180105197Ssam (caddr_t) &dst_address.sin.sin_addr); 181194062Svanhu#ifdef IPSEC_NAT_T 182194062Svanhu /* Find the source port for NAT-T; see udp*_espdecap. */ 183194062Svanhu tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL); 184194062Svanhu if (tag != NULL) 185194062Svanhu dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1]; 186194062Svanhu#endif /* IPSEC_NAT_T */ 187105197Ssam break; 188105197Ssam#endif /* INET */ 189105197Ssam#ifdef INET6 190105197Ssam case AF_INET6: 191105197Ssam dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 192105197Ssam m_copydata(m, offsetof(struct ip6_hdr, ip6_dst), 193105197Ssam sizeof(struct in6_addr), 194105197Ssam (caddr_t) &dst_address.sin6.sin6_addr); 195105197Ssam break; 196105197Ssam#endif /* INET6 */ 197105197Ssam default: 198120585Ssam DPRINTF(("%s: unsupported protocol family %u\n", __func__, af)); 199105197Ssam m_freem(m); 200181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_nopf, V_ahstat.ahs_nopf, 201181803Sbz V_ipcompstat.ipcomps_nopf); 202105197Ssam return EPFNOSUPPORT; 203105197Ssam } 204105197Ssam 205105197Ssam /* NB: only pass dst since key_allocsa follows RFC2401 */ 206105197Ssam sav = KEY_ALLOCSA(&dst_address, sproto, spi); 207105197Ssam if (sav == NULL) { 208120585Ssam DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n", 209120585Ssam __func__, ipsec_address(&dst_address), 210105197Ssam (u_long) ntohl(spi), sproto)); 211181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_notdb, V_ahstat.ahs_notdb, 212181803Sbz V_ipcompstat.ipcomps_notdb); 213105197Ssam m_freem(m); 214105197Ssam return ENOENT; 215105197Ssam } 216105197Ssam 217105197Ssam if (sav->tdb_xform == NULL) { 218120585Ssam DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n", 219120585Ssam __func__, ipsec_address(&dst_address), 220105197Ssam (u_long) ntohl(spi), sproto)); 221181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_noxform, V_ahstat.ahs_noxform, 222181803Sbz V_ipcompstat.ipcomps_noxform); 223105197Ssam KEY_FREESAV(&sav); 224105197Ssam m_freem(m); 225105197Ssam return ENXIO; 226105197Ssam } 227105197Ssam 228105197Ssam /* 229105197Ssam * Call appropriate transform and return -- callback takes care of 230105197Ssam * everything else. 231105197Ssam */ 232105197Ssam error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff); 233105197Ssam KEY_FREESAV(&sav); 234105197Ssam return error; 235105197Ssam} 236105197Ssam 237105197Ssam#ifdef INET 238105197Ssam/* 239105197Ssam * Common input handler for IPv4 AH, ESP, and IPCOMP. 240105197Ssam */ 241105197Ssamint 242105197Ssamipsec4_common_input(struct mbuf *m, ...) 243105197Ssam{ 244105197Ssam va_list ap; 245105197Ssam int off, nxt; 246105197Ssam 247105197Ssam va_start(ap, m); 248105197Ssam off = va_arg(ap, int); 249105197Ssam nxt = va_arg(ap, int); 250105197Ssam va_end(ap); 251105197Ssam 252105197Ssam return ipsec_common_input(m, off, offsetof(struct ip, ip_p), 253105197Ssam AF_INET, nxt); 254105197Ssam} 255105197Ssam 256106680Ssamvoid 257106680Ssamah4_input(struct mbuf *m, int off) 258106680Ssam{ 259106680Ssam ipsec4_common_input(m, off, IPPROTO_AH); 260106680Ssam} 261120585Ssamvoid 262120585Ssamah4_ctlinput(int cmd, struct sockaddr *sa, void *v) 263120585Ssam{ 264120585Ssam if (sa->sa_family == AF_INET && 265120585Ssam sa->sa_len == sizeof(struct sockaddr_in)) 266120585Ssam ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH); 267120585Ssam} 268106680Ssam 269106680Ssamvoid 270106680Ssamesp4_input(struct mbuf *m, int off) 271106680Ssam{ 272106680Ssam ipsec4_common_input(m, off, IPPROTO_ESP); 273106680Ssam} 274120585Ssamvoid 275120585Ssamesp4_ctlinput(int cmd, struct sockaddr *sa, void *v) 276120585Ssam{ 277120585Ssam if (sa->sa_family == AF_INET && 278120585Ssam sa->sa_len == sizeof(struct sockaddr_in)) 279120585Ssam ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP); 280120585Ssam} 281106680Ssam 282106680Ssamvoid 283106680Ssamipcomp4_input(struct mbuf *m, int off) 284106680Ssam{ 285106680Ssam ipsec4_common_input(m, off, IPPROTO_IPCOMP); 286106680Ssam} 287106680Ssam 288105197Ssam/* 289105197Ssam * IPsec input callback for INET protocols. 290105197Ssam * This routine is called as the transform callback. 291105197Ssam * Takes care of filtering and other sanity checks on 292105197Ssam * the processed packet. 293105197Ssam */ 294105197Ssamint 295105197Ssamipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, 296105197Ssam int skip, int protoff, struct m_tag *mt) 297105197Ssam{ 298105197Ssam int prot, af, sproto; 299105197Ssam struct ip *ip; 300105197Ssam struct m_tag *mtag; 301105197Ssam struct tdb_ident *tdbi; 302105197Ssam struct secasindex *saidx; 303105197Ssam int error; 304165222Sbz#ifdef INET6 305165118Sbz#ifdef notyet 306165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 307165118Sbz#endif 308165118Sbz#endif 309105197Ssam 310120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 311120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 312120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 313105197Ssam saidx = &sav->sah->saidx; 314105197Ssam af = saidx->dst.sa.sa_family; 315120585Ssam IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af)); 316105197Ssam sproto = saidx->proto; 317120585Ssam IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 318105197Ssam sproto == IPPROTO_IPCOMP, 319120585Ssam ("unexpected security protocol %u", sproto)); 320105197Ssam 321105197Ssam /* Sanity check */ 322105197Ssam if (m == NULL) { 323120585Ssam DPRINTF(("%s: null mbuf", __func__)); 324181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr, 325181803Sbz V_ipcompstat.ipcomps_badkcr); 326105197Ssam KEY_FREESAV(&sav); 327105197Ssam return EINVAL; 328105197Ssam } 329105197Ssam 330105197Ssam if (skip != 0) { 331241922Sglebius /* 332241922Sglebius * Fix IPv4 header 333241922Sglebius * XXXGL: do we need this entire block? 334241922Sglebius */ 335105197Ssam if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) { 336120585Ssam DPRINTF(("%s: processing failed for SA %s/%08lx\n", 337120585Ssam __func__, ipsec_address(&sav->sah->saidx.dst), 338105197Ssam (u_long) ntohl(sav->spi))); 339181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops, 340181803Sbz V_ipcompstat.ipcomps_hdrops); 341105197Ssam error = ENOBUFS; 342105197Ssam goto bad; 343105197Ssam } 344105197Ssam 345105197Ssam ip = mtod(m, struct ip *); 346105197Ssam ip->ip_len = htons(m->m_pkthdr.len); 347105197Ssam ip->ip_sum = 0; 348105197Ssam ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 349105197Ssam } else { 350105197Ssam ip = mtod(m, struct ip *); 351105197Ssam } 352105197Ssam prot = ip->ip_p; 353105197Ssam 354159215Sgnn#ifdef notyet 355105197Ssam /* IP-in-IP encapsulation */ 356105197Ssam if (prot == IPPROTO_IPIP) { 357105197Ssam struct ip ipn; 358105197Ssam 359118888Ssam if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 360181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 361181803Sbz V_ahstat.ahs_hdrops, 362181803Sbz V_ipcompstat.ipcomps_hdrops); 363118888Ssam error = EINVAL; 364118888Ssam goto bad; 365118888Ssam } 366105197Ssam /* ipn will now contain the inner IPv4 header */ 367105197Ssam m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), 368105197Ssam (caddr_t) &ipn); 369105197Ssam 370105197Ssam /* XXX PROXY address isn't recorded in SAH */ 371105197Ssam /* 372105197Ssam * Check that the inner source address is the same as 373105197Ssam * the proxy address, if available. 374105197Ssam */ 375105197Ssam if ((saidx->proxy.sa.sa_family == AF_INET && 376105197Ssam saidx->proxy.sin.sin_addr.s_addr != 377105197Ssam INADDR_ANY && 378105197Ssam ipn.ip_src.s_addr != 379105197Ssam saidx->proxy.sin.sin_addr.s_addr) || 380105197Ssam (saidx->proxy.sa.sa_family != AF_INET && 381105197Ssam saidx->proxy.sa.sa_family != 0)) { 382105197Ssam 383120585Ssam DPRINTF(("%s: inner source address %s doesn't " 384120585Ssam "correspond to expected proxy source %s, " 385120585Ssam "SA %s/%08lx\n", __func__, 386105197Ssam inet_ntoa4(ipn.ip_src), 387105197Ssam ipsp_address(saidx->proxy), 388105197Ssam ipsp_address(saidx->dst), 389105197Ssam (u_long) ntohl(sav->spi))); 390105197Ssam 391181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, 392181803Sbz V_ahstat.ahs_pdrops, 393181803Sbz V_ipcompstat.ipcomps_pdrops); 394105197Ssam error = EACCES; 395105197Ssam goto bad; 396105197Ssam } 397105197Ssam } 398159237Spjd#ifdef INET6 399105197Ssam /* IPv6-in-IP encapsulation. */ 400105197Ssam if (prot == IPPROTO_IPV6) { 401105197Ssam struct ip6_hdr ip6n; 402105197Ssam 403118888Ssam if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 404181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 405181803Sbz V_ahstat.ahs_hdrops, 406181803Sbz V_ipcompstat.ipcomps_hdrops); 407118888Ssam error = EINVAL; 408118888Ssam goto bad; 409118888Ssam } 410105197Ssam /* ip6n will now contain the inner IPv6 header. */ 411105197Ssam m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), 412105197Ssam (caddr_t) &ip6n); 413105197Ssam 414105197Ssam /* 415105197Ssam * Check that the inner source address is the same as 416105197Ssam * the proxy address, if available. 417105197Ssam */ 418105197Ssam if ((saidx->proxy.sa.sa_family == AF_INET6 && 419105197Ssam !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 420105197Ssam !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 421105197Ssam &saidx->proxy.sin6.sin6_addr)) || 422105197Ssam (saidx->proxy.sa.sa_family != AF_INET6 && 423105197Ssam saidx->proxy.sa.sa_family != 0)) { 424105197Ssam 425120585Ssam DPRINTF(("%s: inner source address %s doesn't " 426120585Ssam "correspond to expected proxy source %s, " 427120585Ssam "SA %s/%08lx\n", __func__, 428165118Sbz ip6_sprintf(ip6buf, &ip6n.ip6_src), 429105197Ssam ipsec_address(&saidx->proxy), 430105197Ssam ipsec_address(&saidx->dst), 431105197Ssam (u_long) ntohl(sav->spi))); 432105197Ssam 433181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, 434181803Sbz V_ahstat.ahs_pdrops, 435181803Sbz V_ipcompstat.ipcomps_pdrops); 436105197Ssam error = EACCES; 437105197Ssam goto bad; 438105197Ssam } 439105197Ssam } 440105197Ssam#endif /* INET6 */ 441159215Sgnn#endif /*XXX*/ 442105197Ssam 443105197Ssam /* 444105197Ssam * Record what we've done to the packet (under what SA it was 445105197Ssam * processed). If we've been passed an mtag, it means the packet 446105197Ssam * was already processed by an ethernet/crypto combo card and 447105197Ssam * thus has a tag attached with all the right information, but 448105197Ssam * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 449105197Ssam * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 450105197Ssam */ 451105197Ssam if (mt == NULL && sproto != IPPROTO_IPCOMP) { 452105197Ssam mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 453105197Ssam sizeof(struct tdb_ident), M_NOWAIT); 454105197Ssam if (mtag == NULL) { 455120585Ssam DPRINTF(("%s: failed to get tag\n", __func__)); 456181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 457181803Sbz V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops); 458105197Ssam error = ENOMEM; 459105197Ssam goto bad; 460105197Ssam } 461105197Ssam 462105197Ssam tdbi = (struct tdb_ident *)(mtag + 1); 463105197Ssam bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len); 464105197Ssam tdbi->proto = sproto; 465105197Ssam tdbi->spi = sav->spi; 466174054Sbz /* Cache those two for enc(4) in xform_ipip. */ 467174054Sbz tdbi->alg_auth = sav->alg_auth; 468174054Sbz tdbi->alg_enc = sav->alg_enc; 469105197Ssam 470105197Ssam m_tag_prepend(m, mtag); 471170797Sbz } else if (mt != NULL) { 472105197Ssam mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 473105197Ssam /* XXX do we need to mark m_flags??? */ 474105197Ssam } 475105197Ssam 476105197Ssam key_sa_recordxfer(sav, m); /* record data transfer */ 477105197Ssam 478159965Sthompsa#ifdef DEV_ENC 479181627Svanhu encif->if_ipackets++; 480181627Svanhu encif->if_ibytes += m->m_pkthdr.len; 481181627Svanhu 482105197Ssam /* 483159965Sthompsa * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP 484159965Sthompsa * packet later after it has been decapsulated. 485159965Sthompsa */ 486174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE); 487159965Sthompsa 488159965Sthompsa if (prot != IPPROTO_IPIP) 489174054Sbz if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0) 490159965Sthompsa return (error); 491159965Sthompsa#endif 492159965Sthompsa 493159965Sthompsa /* 494105197Ssam * Re-dispatch via software interrupt. 495105197Ssam */ 496208508Sbz if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) { 497181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_qfull, V_ahstat.ahs_qfull, 498181803Sbz V_ipcompstat.ipcomps_qfull); 499105197Ssam 500120585Ssam DPRINTF(("%s: queue full; proto %u packet dropped\n", 501120585Ssam __func__, sproto)); 502134391Sandre return error; 503105197Ssam } 504105197Ssam return 0; 505105197Ssambad: 506105197Ssam m_freem(m); 507105197Ssam return error; 508105197Ssam} 509120585Ssam 510120585Ssamvoid 511120585Ssamipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto) 512120585Ssam{ 513120585Ssam /* XXX nothing just yet */ 514120585Ssam} 515105197Ssam#endif /* INET */ 516105197Ssam 517105197Ssam#ifdef INET6 518105197Ssam/* IPv6 AH wrapper. */ 519105197Ssamint 520105197Ssamipsec6_common_input(struct mbuf **mp, int *offp, int proto) 521105197Ssam{ 522105197Ssam int l = 0; 523105197Ssam int protoff; 524105197Ssam struct ip6_ext ip6e; 525105197Ssam 526105197Ssam if (*offp < sizeof(struct ip6_hdr)) { 527120585Ssam DPRINTF(("%s: bad offset %u\n", __func__, *offp)); 528105197Ssam return IPPROTO_DONE; 529105197Ssam } else if (*offp == sizeof(struct ip6_hdr)) { 530105197Ssam protoff = offsetof(struct ip6_hdr, ip6_nxt); 531105197Ssam } else { 532105197Ssam /* Chase down the header chain... */ 533105197Ssam protoff = sizeof(struct ip6_hdr); 534105197Ssam 535105197Ssam do { 536105197Ssam protoff += l; 537105197Ssam m_copydata(*mp, protoff, sizeof(ip6e), 538105197Ssam (caddr_t) &ip6e); 539105197Ssam 540105197Ssam if (ip6e.ip6e_nxt == IPPROTO_AH) 541105197Ssam l = (ip6e.ip6e_len + 2) << 2; 542105197Ssam else 543105197Ssam l = (ip6e.ip6e_len + 1) << 3; 544120585Ssam IPSEC_ASSERT(l > 0, ("l went zero or negative")); 545105197Ssam } while (protoff + l < *offp); 546105197Ssam 547105197Ssam /* Malformed packet check */ 548105197Ssam if (protoff + l != *offp) { 549120585Ssam DPRINTF(("%s: bad packet header chain, protoff %u, " 550120585Ssam "l %u, off %u\n", __func__, protoff, l, *offp)); 551181803Sbz IPSEC_ISTAT(proto, V_espstat.esps_hdrops, 552181803Sbz V_ahstat.ahs_hdrops, 553181803Sbz V_ipcompstat.ipcomps_hdrops); 554105197Ssam m_freem(*mp); 555105197Ssam *mp = NULL; 556105197Ssam return IPPROTO_DONE; 557105197Ssam } 558105197Ssam protoff += offsetof(struct ip6_ext, ip6e_nxt); 559105197Ssam } 560105197Ssam (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto); 561105197Ssam return IPPROTO_DONE; 562105197Ssam} 563105197Ssam 564105197Ssam/* 565105197Ssam * IPsec input callback, called by the transform callback. Takes care of 566105197Ssam * filtering and other sanity checks on the processed packet. 567105197Ssam */ 568105197Ssamint 569105197Ssamipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff, 570105197Ssam struct m_tag *mt) 571105197Ssam{ 572105197Ssam int prot, af, sproto; 573105197Ssam struct ip6_hdr *ip6; 574105197Ssam struct m_tag *mtag; 575105197Ssam struct tdb_ident *tdbi; 576105197Ssam struct secasindex *saidx; 577105197Ssam int nxt; 578105197Ssam u_int8_t nxt8; 579105197Ssam int error, nest; 580165118Sbz#ifdef notyet 581165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 582165118Sbz#endif 583105197Ssam 584120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 585120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 586120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 587105197Ssam saidx = &sav->sah->saidx; 588105197Ssam af = saidx->dst.sa.sa_family; 589120585Ssam IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af)); 590105197Ssam sproto = saidx->proto; 591120585Ssam IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 592105197Ssam sproto == IPPROTO_IPCOMP, 593120585Ssam ("unexpected security protocol %u", sproto)); 594105197Ssam 595105197Ssam /* Sanity check */ 596105197Ssam if (m == NULL) { 597120585Ssam DPRINTF(("%s: null mbuf", __func__)); 598181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr, 599181803Sbz V_ipcompstat.ipcomps_badkcr); 600105197Ssam error = EINVAL; 601105197Ssam goto bad; 602105197Ssam } 603105197Ssam 604105197Ssam /* Fix IPv6 header */ 605105197Ssam if (m->m_len < sizeof(struct ip6_hdr) && 606105197Ssam (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 607105197Ssam 608120585Ssam DPRINTF(("%s: processing failed for SA %s/%08lx\n", 609120585Ssam __func__, ipsec_address(&sav->sah->saidx.dst), 610105197Ssam (u_long) ntohl(sav->spi))); 611105197Ssam 612181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops, 613181803Sbz V_ipcompstat.ipcomps_hdrops); 614105197Ssam error = EACCES; 615105197Ssam goto bad; 616105197Ssam } 617105197Ssam 618105197Ssam ip6 = mtod(m, struct ip6_hdr *); 619105197Ssam ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 620105197Ssam 621105197Ssam /* Save protocol */ 622105197Ssam m_copydata(m, protoff, 1, (unsigned char *) &prot); 623105197Ssam 624159215Sgnn#ifdef notyet 625105197Ssam#ifdef INET 626105197Ssam /* IP-in-IP encapsulation */ 627105197Ssam if (prot == IPPROTO_IPIP) { 628105197Ssam struct ip ipn; 629105197Ssam 630118888Ssam if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 631181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 632181803Sbz V_ahstat.ahs_hdrops, 633181803Sbz V_ipcompstat.ipcomps_hdrops); 634118888Ssam error = EINVAL; 635118888Ssam goto bad; 636118888Ssam } 637105197Ssam /* ipn will now contain the inner IPv4 header */ 638105197Ssam m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn); 639105197Ssam 640105197Ssam /* 641105197Ssam * Check that the inner source address is the same as 642105197Ssam * the proxy address, if available. 643105197Ssam */ 644105197Ssam if ((saidx->proxy.sa.sa_family == AF_INET && 645105197Ssam saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY && 646105197Ssam ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) || 647105197Ssam (saidx->proxy.sa.sa_family != AF_INET && 648105197Ssam saidx->proxy.sa.sa_family != 0)) { 649105197Ssam 650120585Ssam DPRINTF(("%s: inner source address %s doesn't " 651120585Ssam "correspond to expected proxy source %s, " 652120585Ssam "SA %s/%08lx\n", __func__, 653105197Ssam inet_ntoa4(ipn.ip_src), 654105197Ssam ipsec_address(&saidx->proxy), 655105197Ssam ipsec_address(&saidx->dst), 656105197Ssam (u_long) ntohl(sav->spi))); 657105197Ssam 658181803Sbz IPSEC_ISTATsproto, (V_espstat.esps_pdrops, 659181803Sbz V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops); 660105197Ssam error = EACCES; 661105197Ssam goto bad; 662105197Ssam } 663105197Ssam } 664105197Ssam#endif /* INET */ 665105197Ssam 666105197Ssam /* IPv6-in-IP encapsulation */ 667105197Ssam if (prot == IPPROTO_IPV6) { 668105197Ssam struct ip6_hdr ip6n; 669105197Ssam 670118888Ssam if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 671181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 672181803Sbz V_ahstat.ahs_hdrops, 673181803Sbz V_ipcompstat.ipcomps_hdrops); 674118888Ssam error = EINVAL; 675118888Ssam goto bad; 676118888Ssam } 677105197Ssam /* ip6n will now contain the inner IPv6 header. */ 678105197Ssam m_copydata(m, skip, sizeof(struct ip6_hdr), 679105197Ssam (caddr_t) &ip6n); 680105197Ssam 681105197Ssam /* 682105197Ssam * Check that the inner source address is the same as 683105197Ssam * the proxy address, if available. 684105197Ssam */ 685105197Ssam if ((saidx->proxy.sa.sa_family == AF_INET6 && 686105197Ssam !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 687105197Ssam !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 688105197Ssam &saidx->proxy.sin6.sin6_addr)) || 689105197Ssam (saidx->proxy.sa.sa_family != AF_INET6 && 690105197Ssam saidx->proxy.sa.sa_family != 0)) { 691105197Ssam 692120585Ssam DPRINTF(("%s: inner source address %s doesn't " 693120585Ssam "correspond to expected proxy source %s, " 694120585Ssam "SA %s/%08lx\n", __func__, 695165118Sbz ip6_sprintf(ip6buf, &ip6n.ip6_src), 696105197Ssam ipsec_address(&saidx->proxy), 697105197Ssam ipsec_address(&saidx->dst), 698105197Ssam (u_long) ntohl(sav->spi))); 699105197Ssam 700181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, 701181803Sbz V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops); 702105197Ssam error = EACCES; 703105197Ssam goto bad; 704105197Ssam } 705159215Sgnn } 706105197Ssam#endif /*XXX*/ 707105197Ssam 708105197Ssam /* 709105197Ssam * Record what we've done to the packet (under what SA it was 710105197Ssam * processed). If we've been passed an mtag, it means the packet 711105197Ssam * was already processed by an ethernet/crypto combo card and 712105197Ssam * thus has a tag attached with all the right information, but 713105197Ssam * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 714105197Ssam * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 715105197Ssam */ 716105197Ssam if (mt == NULL && sproto != IPPROTO_IPCOMP) { 717105197Ssam mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 718105197Ssam sizeof(struct tdb_ident), M_NOWAIT); 719105197Ssam if (mtag == NULL) { 720120585Ssam DPRINTF(("%s: failed to get tag\n", __func__)); 721181803Sbz IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, 722181803Sbz V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops); 723105197Ssam error = ENOMEM; 724105197Ssam goto bad; 725105197Ssam } 726105197Ssam 727105197Ssam tdbi = (struct tdb_ident *)(mtag + 1); 728105197Ssam bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union)); 729105197Ssam tdbi->proto = sproto; 730105197Ssam tdbi->spi = sav->spi; 731174054Sbz /* Cache those two for enc(4) in xform_ipip. */ 732174054Sbz tdbi->alg_auth = sav->alg_auth; 733174054Sbz tdbi->alg_enc = sav->alg_enc; 734105197Ssam 735105197Ssam m_tag_prepend(m, mtag); 736105197Ssam } else { 737120585Ssam if (mt != NULL) 738120585Ssam mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 739105197Ssam /* XXX do we need to mark m_flags??? */ 740105197Ssam } 741105197Ssam 742105197Ssam key_sa_recordxfer(sav, m); 743105197Ssam 744174054Sbz#ifdef DEV_ENC 745181627Svanhu encif->if_ipackets++; 746181627Svanhu encif->if_ibytes += m->m_pkthdr.len; 747181627Svanhu 748174054Sbz /* 749174054Sbz * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP 750174054Sbz * packet later after it has been decapsulated. 751174054Sbz */ 752174054Sbz ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE); 753174054Sbz 754174054Sbz /* XXX-BZ does not make sense. */ 755174054Sbz if (prot != IPPROTO_IPIP) 756174054Sbz if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0) 757174054Sbz return (error); 758174054Sbz#endif 759174054Sbz 760105197Ssam /* Retrieve new protocol */ 761105197Ssam m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8); 762105197Ssam 763105197Ssam /* 764105197Ssam * See the end of ip6_input for this logic. 765105197Ssam * IPPROTO_IPV[46] case will be processed just like other ones 766105197Ssam */ 767105197Ssam nest = 0; 768105197Ssam nxt = nxt8; 769105197Ssam while (nxt != IPPROTO_DONE) { 770181803Sbz if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 771249294Sae IP6STAT_INC(ip6s_toomanyhdr); 772105197Ssam error = EINVAL; 773105197Ssam goto bad; 774105197Ssam } 775105197Ssam 776105197Ssam /* 777105197Ssam * Protection against faulty packet - there should be 778105197Ssam * more sanity checks in header chain processing. 779105197Ssam */ 780105197Ssam if (m->m_pkthdr.len < skip) { 781249294Sae IP6STAT_INC(ip6s_tooshort); 782105197Ssam in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 783105197Ssam error = EINVAL; 784105197Ssam goto bad; 785105197Ssam } 786105197Ssam /* 787105197Ssam * Enforce IPsec policy checking if we are seeing last header. 788105197Ssam * note that we do not visit this with protocols with pcb layer 789105197Ssam * code - like udp/tcp/raw ip. 790105197Ssam */ 791105197Ssam if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 792105197Ssam ipsec6_in_reject(m, NULL)) { 793105197Ssam error = EINVAL; 794105197Ssam goto bad; 795105197Ssam } 796105197Ssam nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt); 797105197Ssam } 798105197Ssam return 0; 799105197Ssambad: 800105197Ssam if (m) 801105197Ssam m_freem(m); 802105197Ssam return error; 803105197Ssam} 804120585Ssam 805120585Ssamvoid 806120585Ssamesp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 807120585Ssam{ 808172149Sgnn struct ip6ctlparam *ip6cp = NULL; 809172149Sgnn struct mbuf *m = NULL; 810172149Sgnn struct ip6_hdr *ip6; 811172149Sgnn int off; 812172149Sgnn 813120585Ssam if (sa->sa_family != AF_INET6 || 814120585Ssam sa->sa_len != sizeof(struct sockaddr_in6)) 815120585Ssam return; 816120585Ssam if ((unsigned)cmd >= PRC_NCMDS) 817120585Ssam return; 818120585Ssam 819120585Ssam /* if the parameter is from icmp6, decode it. */ 820172149Sgnn if (d != NULL) { 821172149Sgnn ip6cp = (struct ip6ctlparam *)d; 822172149Sgnn m = ip6cp->ip6c_m; 823172149Sgnn ip6 = ip6cp->ip6c_ip6; 824172149Sgnn off = ip6cp->ip6c_off; 825172149Sgnn } else { 826172149Sgnn m = NULL; 827172149Sgnn ip6 = NULL; 828172149Sgnn off = 0; /* calm gcc */ 829172149Sgnn } 830120585Ssam 831172149Sgnn if (ip6 != NULL) { 832172149Sgnn 833120585Ssam struct ip6ctlparam ip6cp1; 834120585Ssam 835120585Ssam /* 836120585Ssam * Notify the error to all possible sockets via pfctlinput2. 837120585Ssam * Since the upper layer information (such as protocol type, 838120585Ssam * source and destination ports) is embedded in the encrypted 839120585Ssam * data and might have been cut, we can't directly call 840120585Ssam * an upper layer ctlinput function. However, the pcbnotify 841120585Ssam * function will consider source and destination addresses 842120585Ssam * as well as the flow info value, and may be able to find 843120585Ssam * some PCB that should be notified. 844120585Ssam * Although pfctlinput2 will call esp6_ctlinput(), there is 845120585Ssam * no possibility of an infinite loop of function calls, 846120585Ssam * because we don't pass the inner IPv6 header. 847120585Ssam */ 848120585Ssam bzero(&ip6cp1, sizeof(ip6cp1)); 849120585Ssam ip6cp1.ip6c_src = ip6cp->ip6c_src; 850120585Ssam pfctlinput2(cmd, sa, (void *)&ip6cp1); 851120585Ssam 852120585Ssam /* 853120585Ssam * Then go to special cases that need ESP header information. 854120585Ssam * XXX: We assume that when ip6 is non NULL, 855120585Ssam * M and OFF are valid. 856120585Ssam */ 857120585Ssam 858120585Ssam if (cmd == PRC_MSGSIZE) { 859120585Ssam struct secasvar *sav; 860120585Ssam u_int32_t spi; 861120585Ssam int valid; 862120585Ssam 863120585Ssam /* check header length before using m_copydata */ 864120585Ssam if (m->m_pkthdr.len < off + sizeof (struct esp)) 865120585Ssam return; 866120585Ssam m_copydata(m, off + offsetof(struct esp, esp_spi), 867120585Ssam sizeof(u_int32_t), (caddr_t) &spi); 868120585Ssam /* 869120585Ssam * Check to see if we have a valid SA corresponding to 870120585Ssam * the address in the ICMP message payload. 871120585Ssam */ 872120585Ssam sav = KEY_ALLOCSA((union sockaddr_union *)sa, 873120585Ssam IPPROTO_ESP, spi); 874120585Ssam valid = (sav != NULL); 875120585Ssam if (sav) 876120585Ssam KEY_FREESAV(&sav); 877120585Ssam 878120585Ssam /* XXX Further validation? */ 879120585Ssam 880120585Ssam /* 881120585Ssam * Depending on whether the SA is "valid" and 882120585Ssam * routing table size (mtudisc_{hi,lo}wat), we will: 883120585Ssam * - recalcurate the new MTU and create the 884120585Ssam * corresponding routing entry, or 885120585Ssam * - ignore the MTU change notification. 886120585Ssam */ 887120585Ssam icmp6_mtudisc_update(ip6cp, valid); 888120585Ssam } 889120585Ssam } else { 890120585Ssam /* we normally notify any pcb here */ 891120585Ssam } 892120585Ssam} 893105197Ssam#endif /* INET6 */ 894