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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d67ead097058d17cccb2eb12a3c455c6


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