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

Cache object: 3a51b55259992ed3bbd8963190742cbc


[ 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.