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

Cache object: 87d7811384af7f90451c594f19ef3296


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