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_output.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 /*-
    2  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD$
   27  */
   28 
   29 /*
   30  * IPsec output processing.
   31  */
   32 #include "opt_inet.h"
   33 #include "opt_inet6.h"
   34 #include "opt_ipsec.h"
   35 #include "opt_enc.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/mbuf.h>
   40 #include <sys/domain.h>
   41 #include <sys/protosw.h>
   42 #include <sys/socket.h>
   43 #include <sys/errno.h>
   44 #include <sys/syslog.h>
   45 
   46 #include <net/if.h>
   47 #include <net/pfil.h>
   48 #include <net/route.h>
   49 
   50 #include <netinet/in.h>
   51 #include <netinet/in_systm.h>
   52 #include <netinet/ip.h>
   53 #include <netinet/ip_var.h>
   54 #include <netinet/in_var.h>
   55 #include <netinet/ip_ecn.h>
   56 #ifdef INET6
   57 #include <netinet6/ip6_ecn.h>
   58 #endif
   59 
   60 #include <netinet/ip6.h>
   61 #ifdef INET6
   62 #include <netinet6/ip6_var.h>
   63 #endif
   64 #include <netinet/in_pcb.h>
   65 #ifdef INET6
   66 #include <netinet/icmp6.h>
   67 #endif
   68 
   69 #include <netipsec/ipsec.h>
   70 #ifdef INET6
   71 #include <netipsec/ipsec6.h>
   72 #endif
   73 #include <netipsec/ah_var.h>
   74 #include <netipsec/esp_var.h>
   75 #include <netipsec/ipcomp_var.h>
   76 
   77 #include <netipsec/xform.h>
   78 
   79 #include <netipsec/key.h>
   80 #include <netipsec/keydb.h>
   81 #include <netipsec/key_debug.h>
   82 
   83 #include <machine/in_cksum.h>
   84 
   85 int
   86 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
   87 {
   88         struct tdb_ident *tdbi;
   89         struct m_tag *mtag;
   90         struct secasvar *sav;
   91         struct secasindex *saidx;
   92         int error;
   93 
   94         IPSEC_SPLASSERT_SOFTNET(__func__);
   95 
   96         IPSEC_ASSERT(m != NULL, ("null mbuf"));
   97         IPSEC_ASSERT(isr != NULL, ("null ISR"));
   98         sav = isr->sav;
   99         IPSEC_ASSERT(sav != NULL, ("null SA"));
  100         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  101 
  102         saidx = &sav->sah->saidx;
  103         switch (saidx->dst.sa.sa_family) {
  104 #ifdef INET
  105         case AF_INET:
  106                 /* Fix the header length, for AH processing. */
  107                 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
  108                 break;
  109 #endif /* INET */
  110 #ifdef INET6
  111         case AF_INET6:
  112                 /* Fix the header length, for AH processing. */
  113                 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
  114                         error = ENXIO;
  115                         goto bad;
  116                 }
  117                 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
  118                         /* No jumbogram support. */
  119                         error = ENXIO;  /*?*/
  120                         goto bad;
  121                 }
  122                 mtod(m, struct ip6_hdr *)->ip6_plen =
  123                         htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
  124                 break;
  125 #endif /* INET6 */
  126         default:
  127                 DPRINTF(("%s: unknown protocol family %u\n", __func__,
  128                     saidx->dst.sa.sa_family));
  129                 error = ENXIO;
  130                 goto bad;
  131         }
  132 
  133         /*
  134          * Add a record of what we've done or what needs to be done to the
  135          * packet.
  136          */
  137         mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
  138                         sizeof(struct tdb_ident), M_NOWAIT);
  139         if (mtag == NULL) {
  140                 DPRINTF(("%s: could not get packet tag\n", __func__));
  141                 error = ENOMEM;
  142                 goto bad;
  143         }
  144 
  145         tdbi = (struct tdb_ident *)(mtag + 1);
  146         tdbi->dst = saidx->dst;
  147         tdbi->proto = saidx->proto;
  148         tdbi->spi = sav->spi;
  149         m_tag_prepend(m, mtag);
  150 
  151         /*
  152          * If there's another (bundled) SA to apply, do so.
  153          * Note that this puts a burden on the kernel stack size.
  154          * If this is a problem we'll need to introduce a queue
  155          * to set the packet on so we can unwind the stack before
  156          * doing further processing.
  157          */
  158         if (isr->next) {
  159                 newipsecstat.ips_out_bundlesa++;
  160                 return ipsec4_process_packet(m, isr->next, 0, 0);
  161         }
  162         key_sa_recordxfer(sav, m);              /* record data transfer */
  163 
  164         /*
  165          * We're done with IPsec processing, transmit the packet using the
  166          * appropriate network protocol (IP or IPv6). SPD lookup will be
  167          * performed again there.
  168          */
  169         switch (saidx->dst.sa.sa_family) {
  170 #ifdef INET
  171         struct ip *ip;
  172         case AF_INET:
  173                 ip = mtod(m, struct ip *);
  174                 ip->ip_len = ntohs(ip->ip_len);
  175                 ip->ip_off = ntohs(ip->ip_off);
  176 
  177                 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
  178 #endif /* INET */
  179 #ifdef INET6
  180         case AF_INET6:
  181                 /*
  182                  * We don't need massage, IPv6 header fields are always in
  183                  * net endian.
  184                  */
  185                 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
  186 #endif /* INET6 */
  187         }
  188         panic("ipsec_process_done");
  189 bad:
  190         m_freem(m);
  191         KEY_FREESAV(&sav);
  192         return (error);
  193 }
  194 
  195 static struct ipsecrequest *
  196 ipsec_nextisr(
  197         struct mbuf *m,
  198         struct ipsecrequest *isr,
  199         int af,
  200         struct secasindex *saidx,
  201         int *error
  202 )
  203 {
  204 #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
  205                             isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
  206         struct secasvar *sav;
  207 
  208         IPSEC_SPLASSERT_SOFTNET(__func__);
  209         IPSECREQUEST_LOCK_ASSERT(isr);
  210 
  211         IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
  212                 ("invalid address family %u", af));
  213 again:
  214         /*
  215          * Craft SA index to search for proper SA.  Note that
  216          * we only fillin unspecified SA peers for transport
  217          * mode; for tunnel mode they must already be filled in.
  218          */
  219         *saidx = isr->saidx;
  220         if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
  221                 /* Fillin unspecified SA peers only for transport mode */
  222                 if (af == AF_INET) {
  223                         struct sockaddr_in *sin;
  224                         struct ip *ip = mtod(m, struct ip *);
  225 
  226                         if (saidx->src.sa.sa_len == 0) {
  227                                 sin = &saidx->src.sin;
  228                                 sin->sin_len = sizeof(*sin);
  229                                 sin->sin_family = AF_INET;
  230                                 sin->sin_port = IPSEC_PORT_ANY;
  231                                 sin->sin_addr = ip->ip_src;
  232                         }
  233                         if (saidx->dst.sa.sa_len == 0) {
  234                                 sin = &saidx->dst.sin;
  235                                 sin->sin_len = sizeof(*sin);
  236                                 sin->sin_family = AF_INET;
  237                                 sin->sin_port = IPSEC_PORT_ANY;
  238                                 sin->sin_addr = ip->ip_dst;
  239                         }
  240                 } else {
  241                         struct sockaddr_in6 *sin6;
  242                         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  243 
  244                         if (saidx->src.sin6.sin6_len == 0) {
  245                                 sin6 = (struct sockaddr_in6 *)&saidx->src;
  246                                 sin6->sin6_len = sizeof(*sin6);
  247                                 sin6->sin6_family = AF_INET6;
  248                                 sin6->sin6_port = IPSEC_PORT_ANY;
  249                                 sin6->sin6_addr = ip6->ip6_src;
  250                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
  251                                         /* fix scope id for comparing SPD */
  252                                         sin6->sin6_addr.s6_addr16[1] = 0;
  253                                         sin6->sin6_scope_id =
  254                                             ntohs(ip6->ip6_src.s6_addr16[1]);
  255                                 }
  256                         }
  257                         if (saidx->dst.sin6.sin6_len == 0) {
  258                                 sin6 = (struct sockaddr_in6 *)&saidx->dst;
  259                                 sin6->sin6_len = sizeof(*sin6);
  260                                 sin6->sin6_family = AF_INET6;
  261                                 sin6->sin6_port = IPSEC_PORT_ANY;
  262                                 sin6->sin6_addr = ip6->ip6_dst;
  263                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
  264                                         /* fix scope id for comparing SPD */
  265                                         sin6->sin6_addr.s6_addr16[1] = 0;
  266                                         sin6->sin6_scope_id =
  267                                             ntohs(ip6->ip6_dst.s6_addr16[1]);
  268                                 }
  269                         }
  270                 }
  271         }
  272 
  273         /*
  274          * Lookup SA and validate it.
  275          */
  276         *error = key_checkrequest(isr, saidx);
  277         if (*error != 0) {
  278                 /*
  279                  * IPsec processing is required, but no SA found.
  280                  * I assume that key_acquire() had been called
  281                  * to get/establish the SA. Here I discard
  282                  * this packet because it is responsibility for
  283                  * upper layer to retransmit the packet.
  284                  */
  285                 newipsecstat.ips_out_nosa++;
  286                 goto bad;
  287         }
  288         sav = isr->sav;
  289         if (sav == NULL) {              /* XXX valid return */
  290                 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
  291                         ("no SA found, but required; level %u",
  292                         ipsec_get_reqlevel(isr)));
  293                 IPSECREQUEST_UNLOCK(isr);
  294                 isr = isr->next;
  295                 if (isr == NULL) {
  296                         /*XXXstatistic??*/
  297                         *error = EINVAL;                /*XXX*/
  298                         return isr;
  299                 }
  300                 IPSECREQUEST_LOCK(isr);
  301                 goto again;
  302         }
  303 
  304         /*
  305          * Check system global policy controls.
  306          */
  307         if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
  308             (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
  309             (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
  310                 DPRINTF(("%s: IPsec outbound packet dropped due"
  311                         " to policy (check your sysctls)\n", __func__));
  312                 IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
  313                     ipcompstat.ipcomps_pdrops);
  314                 *error = EHOSTUNREACH;
  315                 goto bad;
  316         }
  317 
  318         /*
  319          * Sanity check the SA contents for the caller
  320          * before they invoke the xform output method.
  321          */
  322         if (sav->tdb_xform == NULL) {
  323                 DPRINTF(("%s: no transform for SA\n", __func__));
  324                 IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
  325                     ipcompstat.ipcomps_noxform);
  326                 *error = EHOSTUNREACH;
  327                 goto bad;
  328         }
  329         return isr;
  330 bad:
  331         IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
  332         IPSECREQUEST_UNLOCK(isr);
  333         return NULL;
  334 #undef IPSEC_OSTAT
  335 }
  336 
  337 #ifdef INET
  338 /*
  339  * IPsec output logic for IPv4.
  340  */
  341 int
  342 ipsec4_process_packet(
  343         struct mbuf *m,
  344         struct ipsecrequest *isr,
  345         int flags,
  346         int tunalready)
  347 {
  348         struct secasindex saidx;
  349         struct secasvar *sav;
  350         struct ip *ip;
  351         int error, i, off;
  352 
  353         IPSEC_ASSERT(m != NULL, ("null mbuf"));
  354         IPSEC_ASSERT(isr != NULL, ("null isr"));
  355 
  356         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
  357 
  358         isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
  359         if (isr == NULL)
  360                 goto bad;
  361 
  362         sav = isr->sav;
  363 
  364 #ifdef DEV_ENC
  365         /* pass the mbuf to enc0 for packet filtering */
  366         if ((error = ipsec_filter(&m, PFIL_OUT)) != 0)
  367                 goto bad;
  368 #endif
  369 
  370         if (!tunalready) {
  371                 union sockaddr_union *dst = &sav->sah->saidx.dst;
  372                 int setdf;
  373 
  374                 /*
  375                  * Collect IP_DF state from the outer header.
  376                  */
  377                 if (dst->sa.sa_family == AF_INET) {
  378                         if (m->m_len < sizeof (struct ip) &&
  379                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
  380                                 error = ENOBUFS;
  381                                 goto bad;
  382                         }
  383                         ip = mtod(m, struct ip *);
  384                         /* Honor system-wide control of how to handle IP_DF */
  385                         switch (ip4_ipsec_dfbit) {
  386                         case 0:                 /* clear in outer header */
  387                         case 1:                 /* set in outer header */
  388                                 setdf = ip4_ipsec_dfbit;
  389                                 break;
  390                         default:                /* propagate to outer header */
  391                                 setdf = ntohs(ip->ip_off & IP_DF);
  392                                 break;
  393                         }
  394                 } else {
  395                         ip = NULL;              /* keep compiler happy */
  396                         setdf = 0;
  397                 }
  398                 /* Do the appropriate encapsulation, if necessary */
  399                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
  400                     dst->sa.sa_family != AF_INET ||         /* PF mismatch */
  401 #if 0
  402                     (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
  403                     sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
  404 #endif
  405                     (dst->sa.sa_family == AF_INET &&        /* Proxy */
  406                      dst->sin.sin_addr.s_addr != INADDR_ANY &&
  407                      dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
  408                         struct mbuf *mp;
  409 
  410                         /* Fix IPv4 header checksum and length */
  411                         if (m->m_len < sizeof (struct ip) &&
  412                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
  413                                 error = ENOBUFS;
  414                                 goto bad;
  415                         }
  416                         ip = mtod(m, struct ip *);
  417                         ip->ip_len = htons(m->m_pkthdr.len);
  418                         ip->ip_sum = 0;
  419 #ifdef _IP_VHL
  420                         if (ip->ip_vhl == IP_VHL_BORING)
  421                                 ip->ip_sum = in_cksum_hdr(ip);
  422                         else
  423                                 ip->ip_sum = in_cksum(m,
  424                                         _IP_VHL_HL(ip->ip_vhl) << 2);
  425 #else
  426                         ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
  427 #endif
  428 
  429                         /* Encapsulate the packet */
  430                         error = ipip_output(m, isr, &mp, 0, 0);
  431                         if (mp == NULL && !error) {
  432                                 /* Should never happen. */
  433                                 DPRINTF(("%s: ipip_output returns no mbuf and "
  434                                         "no error!", __func__));
  435                                 error = EFAULT;
  436                         }
  437                         if (error) {
  438                                 if (mp) {
  439                                         /* XXX: Should never happen! */
  440                                         m_freem(mp);
  441                                 }
  442                                 m = NULL; /* ipip_output() already freed it */
  443                                 goto bad;
  444                         }
  445                         m = mp, mp = NULL;
  446                         /*
  447                          * ipip_output clears IP_DF in the new header.  If
  448                          * we need to propagate IP_DF from the outer header,
  449                          * then we have to do it here.
  450                          *
  451                          * XXX shouldn't assume what ipip_output does.
  452                          */
  453                         if (dst->sa.sa_family == AF_INET && setdf) {
  454                                 if (m->m_len < sizeof (struct ip) &&
  455                                     (m = m_pullup(m, sizeof (struct ip))) == NULL) {
  456                                         error = ENOBUFS;
  457                                         goto bad;
  458                                 }
  459                                 ip = mtod(m, struct ip *);
  460                                 ip->ip_off = ntohs(ip->ip_off);
  461                                 ip->ip_off |= IP_DF;
  462                                 ip->ip_off = htons(ip->ip_off);
  463                         }
  464                 }
  465         }
  466 
  467 #ifdef DEV_ENC
  468         /* pass the mbuf to enc0 for bpf processing */
  469         ipsec_bpf(m, sav, AF_INET);
  470 #endif
  471 
  472         /*
  473          * Dispatch to the appropriate IPsec transform logic.  The
  474          * packet will be returned for transmission after crypto
  475          * processing, etc. are completed.  For encapsulation we
  476          * bypass this call because of the explicit call done above
  477          * (necessary to deal with IP_DF handling for IPv4).
  478          *
  479          * NB: m & sav are ``passed to caller'' who's reponsible for
  480          *     for reclaiming their resources.
  481          */
  482         if (sav->tdb_xform->xf_type != XF_IP4) {
  483                 ip = mtod(m, struct ip *);
  484                 i = ip->ip_hl << 2;
  485                 off = offsetof(struct ip, ip_p);
  486                 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
  487         } else {
  488                 error = ipsec_process_done(m, isr);
  489         }
  490         IPSECREQUEST_UNLOCK(isr);
  491         return error;
  492 bad:
  493         if (isr)
  494                 IPSECREQUEST_UNLOCK(isr);
  495         if (m)
  496                 m_freem(m);
  497         return error;
  498 }
  499 #endif
  500 
  501 #ifdef INET6
  502 /*
  503  * Chop IP6 header from the payload.
  504  */
  505 static struct mbuf *
  506 ipsec6_splithdr(struct mbuf *m)
  507 {
  508         struct mbuf *mh;
  509         struct ip6_hdr *ip6;
  510         int hlen;
  511 
  512         IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
  513                 ("first mbuf too short, len %u", m->m_len));
  514         ip6 = mtod(m, struct ip6_hdr *);
  515         hlen = sizeof(struct ip6_hdr);
  516         if (m->m_len > hlen) {
  517                 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
  518                 if (!mh) {
  519                         m_freem(m);
  520                         return NULL;
  521                 }
  522                 M_MOVE_PKTHDR(mh, m);
  523                 MH_ALIGN(mh, hlen);
  524                 m->m_len -= hlen;
  525                 m->m_data += hlen;
  526                 mh->m_next = m;
  527                 m = mh;
  528                 m->m_len = hlen;
  529                 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
  530         } else if (m->m_len < hlen) {
  531                 m = m_pullup(m, hlen);
  532                 if (!m)
  533                         return NULL;
  534         }
  535         return m;
  536 }
  537 
  538 /*
  539  * IPsec output logic for IPv6, transport mode.
  540  */
  541 int
  542 ipsec6_output_trans(
  543         struct ipsec_output_state *state,
  544         u_char *nexthdrp,
  545         struct mbuf *mprev,
  546         struct secpolicy *sp,
  547         int flags,
  548         int *tun)
  549 {
  550         struct ipsecrequest *isr;
  551         struct secasindex saidx;
  552         int error = 0;
  553         struct mbuf *m;
  554 
  555         IPSEC_ASSERT(state != NULL, ("null state"));
  556         IPSEC_ASSERT(state->m != NULL, ("null m"));
  557         IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
  558         IPSEC_ASSERT(mprev != NULL, ("null mprev"));
  559         IPSEC_ASSERT(sp != NULL, ("null sp"));
  560         IPSEC_ASSERT(tun != NULL, ("null tun"));
  561 
  562         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
  563                 printf("%s: applied SP\n", __func__);
  564                 kdebug_secpolicy(sp));
  565 
  566         isr = sp->req;
  567         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
  568                 /* the rest will be handled by ipsec6_output_tunnel() */
  569                 *tun = 1;               /* need tunnel-mode processing */
  570                 return 0;
  571         }
  572 
  573         *tun = 0;
  574         m = state->m;
  575 
  576         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
  577         if (isr == NULL) {
  578 #ifdef notdef
  579                 /* XXX should notification be done for all errors ? */
  580                 /*
  581                  * Notify the fact that the packet is discarded
  582                  * to ourselves. I believe this is better than
  583                  * just silently discarding. (jinmei@kame.net)
  584                  * XXX: should we restrict the error to TCP packets?
  585                  * XXX: should we directly notify sockets via
  586                  *      pfctlinputs?
  587                  */
  588                 icmp6_error(m, ICMP6_DST_UNREACH,
  589                             ICMP6_DST_UNREACH_ADMIN, 0);
  590                 m = NULL;       /* NB: icmp6_error frees mbuf */
  591 #endif
  592                 goto bad;
  593         }
  594 
  595         return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
  596                 sizeof (struct ip6_hdr),
  597                 offsetof(struct ip6_hdr, ip6_nxt));
  598 bad:
  599         if (m)
  600                 m_freem(m);
  601         state->m = NULL;
  602         return error;
  603 }
  604 
  605 static int
  606 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
  607 {
  608         struct ip6_hdr *oip6;
  609         struct ip6_hdr *ip6;
  610         size_t plen;
  611 
  612         /* can't tunnel between different AFs */
  613         if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
  614             sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
  615                 m_freem(m);
  616                 return EINVAL;
  617         }
  618         IPSEC_ASSERT(m->m_len != sizeof (struct ip6_hdr),
  619                 ("mbuf wrong size; len %u", m->m_len));
  620 
  621 
  622         /*
  623          * grow the mbuf to accomodate the new IPv6 header.
  624          */
  625         plen = m->m_pkthdr.len;
  626         if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
  627                 struct mbuf *n;
  628                 MGET(n, M_DONTWAIT, MT_DATA);
  629                 if (!n) {
  630                         m_freem(m);
  631                         return ENOBUFS;
  632                 }
  633                 n->m_len = sizeof(struct ip6_hdr);
  634                 n->m_next = m->m_next;
  635                 m->m_next = n;
  636                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
  637                 oip6 = mtod(n, struct ip6_hdr *);
  638         } else {
  639                 m->m_next->m_len += sizeof(struct ip6_hdr);
  640                 m->m_next->m_data -= sizeof(struct ip6_hdr);
  641                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
  642                 oip6 = mtod(m->m_next, struct ip6_hdr *);
  643         }
  644         ip6 = mtod(m, struct ip6_hdr *);
  645         bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
  646 
  647         /* Fake link-local scope-class addresses */
  648         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
  649                 oip6->ip6_src.s6_addr16[1] = 0;
  650         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
  651                 oip6->ip6_dst.s6_addr16[1] = 0;
  652 
  653         /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
  654         /* ECN consideration. */
  655         ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
  656         if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
  657                 ip6->ip6_plen = htons(plen);
  658         else {
  659                 /* ip6->ip6_plen will be updated in ip6_output() */
  660         }
  661         ip6->ip6_nxt = IPPROTO_IPV6;
  662         sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
  663         sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
  664         ip6->ip6_hlim = IPV6_DEFHLIM;
  665 
  666         /* XXX Should ip6_src be updated later ? */
  667 
  668         return 0;
  669 }
  670 
  671 /*
  672  * IPsec output logic for IPv6, tunnel mode.
  673  */
  674 int
  675 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
  676 {
  677         struct ip6_hdr *ip6;
  678         struct ipsecrequest *isr;
  679         struct secasindex saidx;
  680         int error;
  681         struct sockaddr_in6* dst6;
  682         struct mbuf *m;
  683 
  684         IPSEC_ASSERT(state != NULL, ("null state"));
  685         IPSEC_ASSERT(state->m != NULL, ("null m"));
  686         IPSEC_ASSERT(sp != NULL, ("null sp"));
  687 
  688         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
  689                 printf("%s: applied SP\n", __func__);
  690                 kdebug_secpolicy(sp));
  691 
  692         m = state->m;
  693         /*
  694          * transport mode ipsec (before the 1st tunnel mode) is already
  695          * processed by ipsec6_output_trans().
  696          */
  697         for (isr = sp->req; isr; isr = isr->next) {
  698                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
  699                         break;
  700         }
  701         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
  702         if (isr == NULL)
  703                 goto bad;
  704 
  705         /*
  706          * There may be the case that SA status will be changed when
  707          * we are refering to one. So calling splsoftnet().
  708          */
  709         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
  710                 /*
  711                  * build IPsec tunnel.
  712                  */
  713                 /* XXX should be processed with other familiy */
  714                 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
  715                         ipseclog((LOG_ERR, "%s: family mismatched between "
  716                             "inner and outer, spi=%u\n", __func__,
  717                             ntohl(isr->sav->spi)));
  718                         newipsecstat.ips_out_inval++;
  719                         error = EAFNOSUPPORT;
  720                         goto bad;
  721                 }
  722 
  723                 m = ipsec6_splithdr(m);
  724                 if (!m) {
  725                         newipsecstat.ips_out_nomem++;
  726                         error = ENOMEM;
  727                         goto bad;
  728                 }
  729                 error = ipsec6_encapsulate(m, isr->sav);
  730                 if (error) {
  731                         m = NULL;
  732                         goto bad;
  733                 }
  734                 ip6 = mtod(m, struct ip6_hdr *);
  735 
  736                 state->ro = &isr->sav->sah->sa_route;
  737                 state->dst = (struct sockaddr *)&state->ro->ro_dst;
  738                 dst6 = (struct sockaddr_in6 *)state->dst;
  739                 if (state->ro->ro_rt
  740                  && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
  741                   || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
  742                         RTFREE(state->ro->ro_rt);
  743                         state->ro->ro_rt = NULL;
  744                 }
  745                 if (state->ro->ro_rt == 0) {
  746                         bzero(dst6, sizeof(*dst6));
  747                         dst6->sin6_family = AF_INET6;
  748                         dst6->sin6_len = sizeof(*dst6);
  749                         dst6->sin6_addr = ip6->ip6_dst;
  750                         rtalloc(state->ro);
  751                 }
  752                 if (state->ro->ro_rt == 0) {
  753                         ip6stat.ip6s_noroute++;
  754                         newipsecstat.ips_out_noroute++;
  755                         error = EHOSTUNREACH;
  756                         goto bad;
  757                 }
  758 
  759                 /* adjust state->dst if tunnel endpoint is offlink */
  760                 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
  761                         state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
  762                         dst6 = (struct sockaddr_in6 *)state->dst;
  763                 }
  764         }
  765 
  766         m = ipsec6_splithdr(m);
  767         if (!m) {
  768                 newipsecstat.ips_out_nomem++;
  769                 error = ENOMEM;
  770                 goto bad;
  771         }
  772         ip6 = mtod(m, struct ip6_hdr *);
  773         return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
  774                 sizeof (struct ip6_hdr),
  775                 offsetof(struct ip6_hdr, ip6_nxt));
  776 bad:
  777         if (m)
  778                 m_freem(m);
  779         state->m = NULL;
  780         return error;
  781 }
  782 #endif /*INET6*/

Cache object: a41fd16e544c04618493713b9525871c


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