The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipsec/ipsec_input.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/10.1/sys/netipsec/ipsec_input.c 259385 2013-12-14 04:24:32Z ae $       */
    2 /*      $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $        */
    3 /*-
    4  * The authors of this code are John Ioannidis (ji@tla.org),
    5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
    6  * Niels Provos (provos@physnet.uni-hamburg.de).
    7  *
    8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
    9  * in November 1995.
   10  *
   11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
   12  * by Angelos D. Keromytis.
   13  *
   14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
   15  * and Niels Provos.
   16  *
   17  * Additional features in 1999 by Angelos D. Keromytis.
   18  *
   19  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
   20  * Angelos D. Keromytis and Niels Provos.
   21  * Copyright (c) 2001, Angelos D. Keromytis.
   22  *
   23  * Permission to use, copy, and modify this software with or without fee
   24  * is hereby granted, provided that this entire notice is included in
   25  * all copies of any software which is or includes a copy or
   26  * modification of this software.
   27  * You may use this code under the GNU public license if you so wish. Please
   28  * contribute changes back to the authors under this freer than GPL license
   29  * so that we may further the use of strong encryption without limitations to
   30  * all.
   31  *
   32  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
   33  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
   34  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
   35  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
   36  * PURPOSE.
   37  */
   38 
   39 /*
   40  * IPsec input processing.
   41  */
   42 
   43 #include "opt_inet.h"
   44 #include "opt_inet6.h"
   45 #include "opt_ipsec.h"
   46 #include "opt_enc.h"
   47 
   48 #include <sys/param.h>
   49 #include <sys/systm.h>
   50 #include <sys/malloc.h>
   51 #include <sys/mbuf.h>
   52 #include <sys/domain.h>
   53 #include <sys/protosw.h>
   54 #include <sys/socket.h>
   55 #include <sys/errno.h>
   56 #include <sys/syslog.h>
   57 
   58 #include <net/if.h>
   59 #include <net/pfil.h>
   60 #include <net/route.h>
   61 #include <net/netisr.h>
   62 #include <net/vnet.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/in_systm.h>
   66 #include <netinet/ip.h>
   67 #include <netinet/ip_var.h>
   68 #include <netinet/in_var.h>
   69 
   70 #include <netinet/ip6.h>
   71 #ifdef INET6
   72 #include <netinet6/ip6_var.h>
   73 #endif
   74 #include <netinet/in_pcb.h>
   75 #ifdef INET6
   76 #include <netinet/icmp6.h>
   77 #endif
   78 
   79 #include <netipsec/ipsec.h>
   80 #ifdef INET6
   81 #include <netipsec/ipsec6.h>
   82 #endif
   83 #include <netipsec/ah_var.h>
   84 #include <netipsec/esp.h>
   85 #include <netipsec/esp_var.h>
   86 #include <netipsec/ipcomp_var.h>
   87 
   88 #include <netipsec/key.h>
   89 #include <netipsec/keydb.h>
   90 
   91 #include <netipsec/xform.h>
   92 #include <netinet6/ip6protosw.h>
   93 
   94 #include <machine/in_cksum.h>
   95 #include <machine/stdarg.h>
   96 
   97 #ifdef DEV_ENC
   98 #include <net/if_enc.h>
   99 #endif
  100 
  101 
  102 #define IPSEC_ISTAT(proto, name)        do {    \
  103         if ((proto) == IPPROTO_ESP)             \
  104                 ESPSTAT_INC(esps_##name);       \
  105         else if ((proto) == IPPROTO_AH)         \
  106                 AHSTAT_INC(ahs_##name);         \
  107         else                                    \
  108                 IPCOMPSTAT_INC(ipcomps_##name); \
  109 } while (0)
  110 
  111 #ifdef INET
  112 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
  113 #endif
  114 
  115 /*
  116  * ipsec_common_input gets called when an IPsec-protected packet
  117  * is received by IPv4 or IPv6.  Its job is to find the right SA
  118  * and call the appropriate transform.  The transform callback
  119  * takes care of further processing (like ingress filtering).
  120  */
  121 static int
  122 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
  123 {
  124         union sockaddr_union dst_address;
  125         struct secasvar *sav;
  126         u_int32_t spi;
  127         int error;
  128 #ifdef INET
  129 #ifdef IPSEC_NAT_T
  130         struct m_tag *tag;
  131 #endif
  132 #endif
  133 
  134         IPSEC_ISTAT(sproto, input);
  135 
  136         IPSEC_ASSERT(m != NULL, ("null packet"));
  137 
  138         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  139                 sproto == IPPROTO_IPCOMP,
  140                 ("unexpected security protocol %u", sproto));
  141 
  142         if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
  143             (sproto == IPPROTO_AH && !V_ah_enable) ||
  144             (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
  145                 m_freem(m);
  146                 IPSEC_ISTAT(sproto, pdrops);
  147                 return EOPNOTSUPP;
  148         }
  149 
  150         if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
  151                 m_freem(m);
  152                 IPSEC_ISTAT(sproto, hdrops);
  153                 DPRINTF(("%s: packet too small\n", __func__));
  154                 return EINVAL;
  155         }
  156 
  157         /* Retrieve the SPI from the relevant IPsec header */
  158         if (sproto == IPPROTO_ESP)
  159                 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
  160         else if (sproto == IPPROTO_AH)
  161                 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
  162                     (caddr_t) &spi);
  163         else if (sproto == IPPROTO_IPCOMP) {
  164                 u_int16_t cpi;
  165                 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
  166                     (caddr_t) &cpi);
  167                 spi = ntohl(htons(cpi));
  168         }
  169 
  170         /*
  171          * Find the SA and (indirectly) call the appropriate
  172          * kernel crypto routine. The resulting mbuf chain is a valid
  173          * IP packet ready to go through input processing.
  174          */
  175         bzero(&dst_address, sizeof (dst_address));
  176         dst_address.sa.sa_family = af;
  177         switch (af) {
  178 #ifdef INET
  179         case AF_INET:
  180                 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
  181                 m_copydata(m, offsetof(struct ip, ip_dst),
  182                     sizeof(struct in_addr),
  183                     (caddr_t) &dst_address.sin.sin_addr);
  184 #ifdef IPSEC_NAT_T
  185                 /* Find the source port for NAT-T; see udp*_espdecap. */
  186                 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
  187                 if (tag != NULL)
  188                         dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
  189 #endif /* IPSEC_NAT_T */
  190                 break;
  191 #endif /* INET */
  192 #ifdef INET6
  193         case AF_INET6:
  194                 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
  195                 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
  196                     sizeof(struct in6_addr),
  197                     (caddr_t) &dst_address.sin6.sin6_addr);
  198                 break;
  199 #endif /* INET6 */
  200         default:
  201                 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
  202                 m_freem(m);
  203                 IPSEC_ISTAT(sproto, nopf);
  204                 return EPFNOSUPPORT;
  205         }
  206 
  207         /* NB: only pass dst since key_allocsa follows RFC2401 */
  208         sav = KEY_ALLOCSA(&dst_address, sproto, spi);
  209         if (sav == NULL) {
  210                 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
  211                           __func__, ipsec_address(&dst_address),
  212                           (u_long) ntohl(spi), sproto));
  213                 IPSEC_ISTAT(sproto, notdb);
  214                 m_freem(m);
  215                 return ENOENT;
  216         }
  217 
  218         if (sav->tdb_xform == NULL) {
  219                 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
  220                          __func__, ipsec_address(&dst_address),
  221                          (u_long) ntohl(spi), sproto));
  222                 IPSEC_ISTAT(sproto, noxform);
  223                 KEY_FREESAV(&sav);
  224                 m_freem(m);
  225                 return ENXIO;
  226         }
  227 
  228         /*
  229          * Call appropriate transform and return -- callback takes care of
  230          * everything else.
  231          */
  232         error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
  233         KEY_FREESAV(&sav);
  234         return error;
  235 }
  236 
  237 #ifdef INET
  238 /*
  239  * Common input handler for IPv4 AH, ESP, and IPCOMP.
  240  */
  241 int
  242 ipsec4_common_input(struct mbuf *m, ...)
  243 {
  244         va_list ap;
  245         int off, nxt;
  246 
  247         va_start(ap, m);
  248         off = va_arg(ap, int);
  249         nxt = va_arg(ap, int);
  250         va_end(ap);
  251 
  252         return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
  253                                   AF_INET, nxt);
  254 }
  255 
  256 void
  257 ah4_input(struct mbuf *m, int off)
  258 {
  259         ipsec4_common_input(m, off, IPPROTO_AH);
  260 }
  261 void
  262 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
  263 {
  264         if (sa->sa_family == AF_INET &&
  265             sa->sa_len == sizeof(struct sockaddr_in))
  266                 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
  267 }
  268 
  269 void
  270 esp4_input(struct mbuf *m, int off)
  271 {
  272         ipsec4_common_input(m, off, IPPROTO_ESP);
  273 }
  274 void
  275 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
  276 {
  277         if (sa->sa_family == AF_INET &&
  278             sa->sa_len == sizeof(struct sockaddr_in))
  279                 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
  280 }
  281 
  282 void
  283 ipcomp4_input(struct mbuf *m, int off)
  284 {
  285         ipsec4_common_input(m, off, IPPROTO_IPCOMP);
  286 }
  287 
  288 /*
  289  * IPsec input callback for INET protocols.
  290  * This routine is called as the transform callback.
  291  * Takes care of filtering and other sanity checks on
  292  * the processed packet.
  293  */
  294 int
  295 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
  296                         int skip, int protoff, struct m_tag *mt)
  297 {
  298         int prot, af, sproto;
  299         struct ip *ip;
  300         struct m_tag *mtag;
  301         struct tdb_ident *tdbi;
  302         struct secasindex *saidx;
  303         int error;
  304 #ifdef INET6
  305 #ifdef notyet
  306         char ip6buf[INET6_ADDRSTRLEN];
  307 #endif
  308 #endif
  309 
  310         IPSEC_ASSERT(m != NULL, ("null mbuf"));
  311         IPSEC_ASSERT(sav != NULL, ("null SA"));
  312         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  313         saidx = &sav->sah->saidx;
  314         af = saidx->dst.sa.sa_family;
  315         IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
  316         sproto = saidx->proto;
  317         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  318                 sproto == IPPROTO_IPCOMP,
  319                 ("unexpected security protocol %u", sproto));
  320 
  321         /* Sanity check */
  322         if (m == NULL) {
  323                 DPRINTF(("%s: null mbuf", __func__));
  324                 IPSEC_ISTAT(sproto, badkcr);
  325                 KEY_FREESAV(&sav);
  326                 return EINVAL;
  327         }
  328 
  329         if (skip != 0) {
  330                 /*
  331                  * Fix IPv4 header
  332                  * XXXGL: do we need this entire block?
  333                  */
  334                 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
  335                         DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  336                             __func__, ipsec_address(&sav->sah->saidx.dst),
  337                             (u_long) ntohl(sav->spi)));
  338                         IPSEC_ISTAT(sproto, hdrops);
  339                         error = ENOBUFS;
  340                         goto bad;
  341                 }
  342 
  343                 ip = mtod(m, struct ip *);
  344                 ip->ip_len = htons(m->m_pkthdr.len);
  345                 ip->ip_sum = 0;
  346                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
  347         } else {
  348                 ip = mtod(m, struct ip *);
  349         }
  350         prot = ip->ip_p;
  351 
  352 #ifdef notyet
  353         /* IP-in-IP encapsulation */
  354         if (prot == IPPROTO_IPIP) {
  355                 struct ip ipn;
  356 
  357                 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  358                         IPSEC_ISTAT(sproto, hdrops);
  359                         error = EINVAL;
  360                         goto bad;
  361                 }
  362                 /* ipn will now contain the inner IPv4 header */
  363                 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
  364                     (caddr_t) &ipn);
  365 
  366                 /* XXX PROXY address isn't recorded in SAH */
  367                 /*
  368                  * Check that the inner source address is the same as
  369                  * the proxy address, if available.
  370                  */
  371                 if ((saidx->proxy.sa.sa_family == AF_INET &&
  372                     saidx->proxy.sin.sin_addr.s_addr !=
  373                     INADDR_ANY &&
  374                     ipn.ip_src.s_addr !=
  375                     saidx->proxy.sin.sin_addr.s_addr) ||
  376                     (saidx->proxy.sa.sa_family != AF_INET &&
  377                         saidx->proxy.sa.sa_family != 0)) {
  378 
  379                         DPRINTF(("%s: inner source address %s doesn't "
  380                             "correspond to expected proxy source %s, "
  381                             "SA %s/%08lx\n", __func__,
  382                             inet_ntoa4(ipn.ip_src),
  383                             ipsp_address(saidx->proxy),
  384                             ipsp_address(saidx->dst),
  385                             (u_long) ntohl(sav->spi)));
  386 
  387                         IPSEC_ISTAT(sproto, pdrops);
  388                         error = EACCES;
  389                         goto bad;
  390                 }
  391         }
  392 #ifdef INET6
  393         /* IPv6-in-IP encapsulation. */
  394         if (prot == IPPROTO_IPV6) {
  395                 struct ip6_hdr ip6n;
  396 
  397                 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  398                         IPSEC_ISTAT(sproto, hdrops);
  399                         error = EINVAL;
  400                         goto bad;
  401                 }
  402                 /* ip6n will now contain the inner IPv6 header. */
  403                 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
  404                     (caddr_t) &ip6n);
  405 
  406                 /*
  407                  * Check that the inner source address is the same as
  408                  * the proxy address, if available.
  409                  */
  410                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
  411                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
  412                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
  413                         &saidx->proxy.sin6.sin6_addr)) ||
  414                     (saidx->proxy.sa.sa_family != AF_INET6 &&
  415                         saidx->proxy.sa.sa_family != 0)) {
  416 
  417                         DPRINTF(("%s: inner source address %s doesn't "
  418                             "correspond to expected proxy source %s, "
  419                             "SA %s/%08lx\n", __func__,
  420                             ip6_sprintf(ip6buf, &ip6n.ip6_src),
  421                             ipsec_address(&saidx->proxy),
  422                             ipsec_address(&saidx->dst),
  423                             (u_long) ntohl(sav->spi)));
  424 
  425                         IPSEC_ISTAT(sproto, pdrops);
  426                         error = EACCES;
  427                         goto bad;
  428                 }
  429         }
  430 #endif /* INET6 */
  431 #endif /*XXX*/
  432 
  433         /*
  434          * Record what we've done to the packet (under what SA it was
  435          * processed). If we've been passed an mtag, it means the packet
  436          * was already processed by an ethernet/crypto combo card and
  437          * thus has a tag attached with all the right information, but
  438          * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
  439          * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
  440          */
  441         if (mt == NULL && sproto != IPPROTO_IPCOMP) {
  442                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  443                     sizeof(struct tdb_ident), M_NOWAIT);
  444                 if (mtag == NULL) {
  445                         DPRINTF(("%s: failed to get tag\n", __func__));
  446                         IPSEC_ISTAT(sproto, hdrops);
  447                         error = ENOMEM;
  448                         goto bad;
  449                 }
  450 
  451                 tdbi = (struct tdb_ident *)(mtag + 1);
  452                 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
  453                 tdbi->proto = sproto;
  454                 tdbi->spi = sav->spi;
  455                 /* Cache those two for enc(4) in xform_ipip. */
  456                 tdbi->alg_auth = sav->alg_auth;
  457                 tdbi->alg_enc = sav->alg_enc;
  458 
  459                 m_tag_prepend(m, mtag);
  460         } else if (mt != NULL) {
  461                 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
  462                 /* XXX do we need to mark m_flags??? */
  463         }
  464 
  465         key_sa_recordxfer(sav, m);              /* record data transfer */
  466 
  467 #ifdef DEV_ENC
  468         encif->if_ipackets++;
  469         encif->if_ibytes += m->m_pkthdr.len;
  470 
  471         /*
  472          * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
  473          * packet later after it has been decapsulated.
  474          */
  475         ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
  476 
  477         if (prot != IPPROTO_IPIP)
  478                 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
  479                         return (error);
  480 #endif
  481 
  482         /*
  483          * Re-dispatch via software interrupt.
  484          */
  485         if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) {
  486                 IPSEC_ISTAT(sproto, qfull);
  487                 DPRINTF(("%s: queue full; proto %u packet dropped\n",
  488                         __func__, sproto));
  489                 return error;
  490         }
  491         return 0;
  492 bad:
  493         m_freem(m);
  494         return error;
  495 }
  496 
  497 void
  498 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
  499 {
  500         /* XXX nothing just yet */
  501 }
  502 #endif /* INET */
  503 
  504 #ifdef INET6
  505 /* IPv6 AH wrapper. */
  506 int
  507 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
  508 {
  509         int l = 0;
  510         int protoff;
  511         struct ip6_ext ip6e;
  512 
  513         if (*offp < sizeof(struct ip6_hdr)) {
  514                 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
  515                 return IPPROTO_DONE;
  516         } else if (*offp == sizeof(struct ip6_hdr)) {
  517                 protoff = offsetof(struct ip6_hdr, ip6_nxt);
  518         } else {
  519                 /* Chase down the header chain... */
  520                 protoff = sizeof(struct ip6_hdr);
  521 
  522                 do {
  523                         protoff += l;
  524                         m_copydata(*mp, protoff, sizeof(ip6e),
  525                             (caddr_t) &ip6e);
  526 
  527                         if (ip6e.ip6e_nxt == IPPROTO_AH)
  528                                 l = (ip6e.ip6e_len + 2) << 2;
  529                         else
  530                                 l = (ip6e.ip6e_len + 1) << 3;
  531                         IPSEC_ASSERT(l > 0, ("l went zero or negative"));
  532                 } while (protoff + l < *offp);
  533 
  534                 /* Malformed packet check */
  535                 if (protoff + l != *offp) {
  536                         DPRINTF(("%s: bad packet header chain, protoff %u, "
  537                                 "l %u, off %u\n", __func__, protoff, l, *offp));
  538                         IPSEC_ISTAT(proto, hdrops);
  539                         m_freem(*mp);
  540                         *mp = NULL;
  541                         return IPPROTO_DONE;
  542                 }
  543                 protoff += offsetof(struct ip6_ext, ip6e_nxt);
  544         }
  545         (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
  546         return IPPROTO_DONE;
  547 }
  548 
  549 /*
  550  * IPsec input callback, called by the transform callback. Takes care of
  551  * filtering and other sanity checks on the processed packet.
  552  */
  553 int
  554 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
  555     struct m_tag *mt)
  556 {
  557         int prot, af, sproto;
  558         struct ip6_hdr *ip6;
  559         struct m_tag *mtag;
  560         struct tdb_ident *tdbi;
  561         struct secasindex *saidx;
  562         int nxt;
  563         u_int8_t nxt8;
  564         int error, nest;
  565 #ifdef notyet
  566         char ip6buf[INET6_ADDRSTRLEN];
  567 #endif
  568 
  569         IPSEC_ASSERT(m != NULL, ("null mbuf"));
  570         IPSEC_ASSERT(sav != NULL, ("null SA"));
  571         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  572         saidx = &sav->sah->saidx;
  573         af = saidx->dst.sa.sa_family;
  574         IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
  575         sproto = saidx->proto;
  576         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  577                 sproto == IPPROTO_IPCOMP,
  578                 ("unexpected security protocol %u", sproto));
  579 
  580         /* Sanity check */
  581         if (m == NULL) {
  582                 DPRINTF(("%s: null mbuf", __func__));
  583                 IPSEC_ISTAT(sproto, badkcr);
  584                 error = EINVAL;
  585                 goto bad;
  586         }
  587 
  588         /* Fix IPv6 header */
  589         if (m->m_len < sizeof(struct ip6_hdr) &&
  590             (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  591 
  592                 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  593                     __func__, ipsec_address(&sav->sah->saidx.dst),
  594                     (u_long) ntohl(sav->spi)));
  595 
  596                 IPSEC_ISTAT(sproto, hdrops);
  597                 error = EACCES;
  598                 goto bad;
  599         }
  600 
  601         ip6 = mtod(m, struct ip6_hdr *);
  602         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
  603 
  604         /* Save protocol */
  605         prot = 0;
  606         m_copydata(m, protoff, 1, (unsigned char *) &prot);
  607 
  608 #ifdef notyet
  609 #ifdef INET
  610         /* IP-in-IP encapsulation */
  611         if (prot == IPPROTO_IPIP) {
  612                 struct ip ipn;
  613 
  614                 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  615                         IPSEC_ISTAT(sproto, hdrops);
  616                         error = EINVAL;
  617                         goto bad;
  618                 }
  619                 /* ipn will now contain the inner IPv4 header */
  620                 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
  621 
  622                 /*
  623                  * Check that the inner source address is the same as
  624                  * the proxy address, if available.
  625                  */
  626                 if ((saidx->proxy.sa.sa_family == AF_INET &&
  627                     saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
  628                     ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
  629                     (saidx->proxy.sa.sa_family != AF_INET &&
  630                         saidx->proxy.sa.sa_family != 0)) {
  631 
  632                         DPRINTF(("%s: inner source address %s doesn't "
  633                             "correspond to expected proxy source %s, "
  634                             "SA %s/%08lx\n", __func__,
  635                             inet_ntoa4(ipn.ip_src),
  636                             ipsec_address(&saidx->proxy),
  637                             ipsec_address(&saidx->dst),
  638                             (u_long) ntohl(sav->spi)));
  639 
  640                         IPSEC_ISTAT(sproto, pdrops);
  641                         error = EACCES;
  642                         goto bad;
  643                 }
  644         }
  645 #endif /* INET */
  646 
  647         /* IPv6-in-IP encapsulation */
  648         if (prot == IPPROTO_IPV6) {
  649                 struct ip6_hdr ip6n;
  650 
  651                 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  652                         IPSEC_ISTAT(sproto, hdrops);
  653                         error = EINVAL;
  654                         goto bad;
  655                 }
  656                 /* ip6n will now contain the inner IPv6 header. */
  657                 m_copydata(m, skip, sizeof(struct ip6_hdr),
  658                     (caddr_t) &ip6n);
  659 
  660                 /*
  661                  * Check that the inner source address is the same as
  662                  * the proxy address, if available.
  663                  */
  664                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
  665                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
  666                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
  667                         &saidx->proxy.sin6.sin6_addr)) ||
  668                     (saidx->proxy.sa.sa_family != AF_INET6 &&
  669                         saidx->proxy.sa.sa_family != 0)) {
  670 
  671                         DPRINTF(("%s: inner source address %s doesn't "
  672                             "correspond to expected proxy source %s, "
  673                             "SA %s/%08lx\n", __func__,
  674                             ip6_sprintf(ip6buf, &ip6n.ip6_src),
  675                             ipsec_address(&saidx->proxy),
  676                             ipsec_address(&saidx->dst),
  677                             (u_long) ntohl(sav->spi)));
  678 
  679                         IPSEC_ISTAT(sproto, pdrops);
  680                         error = EACCES;
  681                         goto bad;
  682                 }
  683         }
  684 #endif /*XXX*/
  685 
  686         /*
  687          * Record what we've done to the packet (under what SA it was
  688          * processed). If we've been passed an mtag, it means the packet
  689          * was already processed by an ethernet/crypto combo card and
  690          * thus has a tag attached with all the right information, but
  691          * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
  692          * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
  693          */
  694         if (mt == NULL && sproto != IPPROTO_IPCOMP) {
  695                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  696                     sizeof(struct tdb_ident), M_NOWAIT);
  697                 if (mtag == NULL) {
  698                         DPRINTF(("%s: failed to get tag\n", __func__));
  699                         IPSEC_ISTAT(sproto, hdrops);
  700                         error = ENOMEM;
  701                         goto bad;
  702                 }
  703 
  704                 tdbi = (struct tdb_ident *)(mtag + 1);
  705                 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
  706                 tdbi->proto = sproto;
  707                 tdbi->spi = sav->spi;
  708                 /* Cache those two for enc(4) in xform_ipip. */
  709                 tdbi->alg_auth = sav->alg_auth;
  710                 tdbi->alg_enc = sav->alg_enc;
  711 
  712                 m_tag_prepend(m, mtag);
  713         } else {
  714                 if (mt != NULL)
  715                         mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
  716                 /* XXX do we need to mark m_flags??? */
  717         }
  718 
  719         key_sa_recordxfer(sav, m);
  720 
  721 #ifdef DEV_ENC
  722         encif->if_ipackets++;
  723         encif->if_ibytes += m->m_pkthdr.len;
  724 
  725         /*
  726          * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
  727          * packet later after it has been decapsulated.
  728          */
  729         ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
  730 
  731         /* XXX-BZ does not make sense. */
  732         if (prot != IPPROTO_IPIP)
  733                 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
  734                         return (error);
  735 #endif
  736 
  737         /* Retrieve new protocol */
  738         m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
  739 
  740         /*
  741          * See the end of ip6_input for this logic.
  742          * IPPROTO_IPV[46] case will be processed just like other ones
  743          */
  744         nest = 0;
  745         nxt = nxt8;
  746         while (nxt != IPPROTO_DONE) {
  747                 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
  748                         IP6STAT_INC(ip6s_toomanyhdr);
  749                         error = EINVAL;
  750                         goto bad;
  751                 }
  752 
  753                 /*
  754                  * Protection against faulty packet - there should be
  755                  * more sanity checks in header chain processing.
  756                  */
  757                 if (m->m_pkthdr.len < skip) {
  758                         IP6STAT_INC(ip6s_tooshort);
  759                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  760                         error = EINVAL;
  761                         goto bad;
  762                 }
  763                 /*
  764                  * Enforce IPsec policy checking if we are seeing last header.
  765                  * note that we do not visit this with protocols with pcb layer
  766                  * code - like udp/tcp/raw ip.
  767                  */
  768                 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
  769                     ipsec6_in_reject(m, NULL)) {
  770                         error = EINVAL;
  771                         goto bad;
  772                 }
  773                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
  774         }
  775         return 0;
  776 bad:
  777         if (m)
  778                 m_freem(m);
  779         return error;
  780 }
  781 
  782 void
  783 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  784 {
  785         struct ip6ctlparam *ip6cp = NULL;
  786         struct mbuf *m = NULL;
  787         struct ip6_hdr *ip6;
  788         int off;
  789 
  790         if (sa->sa_family != AF_INET6 ||
  791             sa->sa_len != sizeof(struct sockaddr_in6))
  792                 return;
  793         if ((unsigned)cmd >= PRC_NCMDS)
  794                 return;
  795 
  796         /* if the parameter is from icmp6, decode it. */
  797         if (d != NULL) {
  798                 ip6cp = (struct ip6ctlparam *)d;
  799                 m = ip6cp->ip6c_m;
  800                 ip6 = ip6cp->ip6c_ip6;
  801                 off = ip6cp->ip6c_off;
  802         } else {
  803                 m = NULL;
  804                 ip6 = NULL;
  805                 off = 0;        /* calm gcc */
  806         }
  807 
  808         if (ip6 != NULL) {
  809 
  810                 struct ip6ctlparam ip6cp1;
  811 
  812                 /*
  813                  * Notify the error to all possible sockets via pfctlinput2.
  814                  * Since the upper layer information (such as protocol type,
  815                  * source and destination ports) is embedded in the encrypted
  816                  * data and might have been cut, we can't directly call
  817                  * an upper layer ctlinput function. However, the pcbnotify
  818                  * function will consider source and destination addresses
  819                  * as well as the flow info value, and may be able to find
  820                  * some PCB that should be notified.
  821                  * Although pfctlinput2 will call esp6_ctlinput(), there is
  822                  * no possibility of an infinite loop of function calls,
  823                  * because we don't pass the inner IPv6 header.
  824                  */
  825                 bzero(&ip6cp1, sizeof(ip6cp1));
  826                 ip6cp1.ip6c_src = ip6cp->ip6c_src;
  827                 pfctlinput2(cmd, sa, (void *)&ip6cp1);
  828 
  829                 /*
  830                  * Then go to special cases that need ESP header information.
  831                  * XXX: We assume that when ip6 is non NULL,
  832                  * M and OFF are valid.
  833                  */
  834 
  835                 if (cmd == PRC_MSGSIZE) {
  836                         struct secasvar *sav;
  837                         u_int32_t spi;
  838                         int valid;
  839 
  840                         /* check header length before using m_copydata */
  841                         if (m->m_pkthdr.len < off + sizeof (struct esp))
  842                                 return;
  843                         m_copydata(m, off + offsetof(struct esp, esp_spi),
  844                                 sizeof(u_int32_t), (caddr_t) &spi);
  845                         /*
  846                          * Check to see if we have a valid SA corresponding to
  847                          * the address in the ICMP message payload.
  848                          */
  849                         sav = KEY_ALLOCSA((union sockaddr_union *)sa,
  850                                         IPPROTO_ESP, spi);
  851                         valid = (sav != NULL);
  852                         if (sav)
  853                                 KEY_FREESAV(&sav);
  854 
  855                         /* XXX Further validation? */
  856 
  857                         /*
  858                          * Depending on whether the SA is "valid" and
  859                          * routing table size (mtudisc_{hi,lo}wat), we will:
  860                          * - recalcurate the new MTU and create the
  861                          *   corresponding routing entry, or
  862                          * - ignore the MTU change notification.
  863                          */
  864                         icmp6_mtudisc_update(ip6cp, valid);
  865                 }
  866         } else {
  867                 /* we normally notify any pcb here */
  868         }
  869 }
  870 #endif /* INET6 */

Cache object: 24f61ddac28682f439d1c8237984d272


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.