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/xform_ipip.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$       */
    2 /*      $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun 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  * The original version of this code was written by John Ioannidis
    9  * for BSD/OS in Athens, Greece, 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  * IP-inside-IP processing
   41  */
   42 #include "opt_inet.h"
   43 #include "opt_inet6.h"
   44 #include "opt_enc.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/systm.h>
   48 #include <sys/mbuf.h>
   49 #include <sys/socket.h>
   50 #include <sys/kernel.h>
   51 #include <sys/protosw.h>
   52 #include <sys/sysctl.h>
   53 
   54 #include <net/if.h>
   55 #include <net/pfil.h>
   56 #include <net/route.h>
   57 #include <net/netisr.h>
   58 
   59 #include <netinet/in.h>
   60 #include <netinet/in_systm.h>
   61 #include <netinet/in_var.h>
   62 #include <netinet/ip.h>
   63 #include <netinet/ip_ecn.h>
   64 #include <netinet/ip_var.h>
   65 #include <netinet/ip_encap.h>
   66 
   67 #include <netipsec/ipsec.h>
   68 #include <netipsec/xform.h>
   69 
   70 #include <netipsec/ipip_var.h>
   71 
   72 #ifdef MROUTING
   73 #include <netinet/ip_mroute.h>
   74 #endif
   75 
   76 #ifdef INET6
   77 #include <netinet/ip6.h>
   78 #include <netipsec/ipsec6.h>
   79 #include <netinet6/ip6_ecn.h>
   80 #include <netinet6/in6_var.h>
   81 #include <netinet6/ip6protosw.h>
   82 #endif
   83 
   84 #include <netipsec/key.h>
   85 #include <netipsec/key_debug.h>
   86 
   87 #include <machine/stdarg.h>
   88 
   89 /*
   90  * We can control the acceptance of IP4 packets by altering the sysctl
   91  * net.inet.ipip.allow value.  Zero means drop them, all else is acceptance.
   92  */
   93 int     ipip_allow = 0;
   94 struct  ipipstat ipipstat;
   95 
   96 SYSCTL_DECL(_net_inet_ipip);
   97 SYSCTL_INT(_net_inet_ipip, OID_AUTO,
   98         ipip_allow,     CTLFLAG_RW,     &ipip_allow,    0, "");
   99 SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
  100         stats,          CTLFLAG_RD,     &ipipstat,      ipipstat, "");
  101 
  102 /* XXX IPCOMP */
  103 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
  104 
  105 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
  106 
  107 #ifdef INET6
  108 /*
  109  * Really only a wrapper for ipip_input(), for use with IPv6.
  110  */
  111 int
  112 ip4_input6(struct mbuf **m, int *offp, int proto)
  113 {
  114 #if 0
  115         /* If we do not accept IP-in-IP explicitly, drop.  */
  116         if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
  117                 DPRINTF(("%s: dropped due to policy\n", __func__));
  118                 ipipstat.ipips_pdrops++;
  119                 m_freem(*m);
  120                 return IPPROTO_DONE;
  121         }
  122 #endif
  123         _ipip_input(*m, *offp, NULL);
  124         return IPPROTO_DONE;
  125 }
  126 #endif /* INET6 */
  127 
  128 #ifdef INET
  129 /*
  130  * Really only a wrapper for ipip_input(), for use with IPv4.
  131  */
  132 void
  133 ip4_input(struct mbuf *m, int off)
  134 {
  135 #if 0
  136         /* If we do not accept IP-in-IP explicitly, drop.  */
  137         if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
  138                 DPRINTF(("%s: dropped due to policy\n", __func__));
  139                 ipipstat.ipips_pdrops++;
  140                 m_freem(m);
  141                 return;
  142         }
  143 #endif
  144         _ipip_input(m, off, NULL);
  145 }
  146 #endif /* INET */
  147 
  148 /*
  149  * ipip_input gets called when we receive an IP{46} encapsulated packet,
  150  * either because we got it at a real interface, or because AH or ESP
  151  * were being used in tunnel mode (in which case the rcvif element will
  152  * contain the address of the encX interface associated with the tunnel.
  153  */
  154 
  155 static void
  156 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
  157 {
  158         register struct sockaddr_in *sin;
  159         register struct ifnet *ifp;
  160         register struct ifaddr *ifa;
  161         struct ip *ipo;
  162 #ifdef INET6
  163         register struct sockaddr_in6 *sin6;
  164         struct ip6_hdr *ip6 = NULL;
  165         u_int8_t itos;
  166 #endif
  167         u_int8_t nxt;
  168         int isr;
  169         u_int8_t otos;
  170         u_int8_t v;
  171         int hlen;
  172 
  173         ipipstat.ipips_ipackets++;
  174 
  175         m_copydata(m, 0, 1, &v);
  176 
  177         switch (v >> 4) {
  178 #ifdef INET
  179         case 4:
  180                 hlen = sizeof(struct ip);
  181                 break;
  182 #endif /* INET */
  183 #ifdef INET6
  184         case 6:
  185                 hlen = sizeof(struct ip6_hdr);
  186                 break;
  187 #endif
  188         default:
  189                 ipipstat.ipips_family++;
  190                 m_freem(m);
  191                 return /* EAFNOSUPPORT */;
  192         }
  193 
  194         /* Bring the IP header in the first mbuf, if not there already */
  195         if (m->m_len < hlen) {
  196                 if ((m = m_pullup(m, hlen)) == NULL) {
  197                         DPRINTF(("%s: m_pullup (1) failed\n", __func__));
  198                         ipipstat.ipips_hdrops++;
  199                         return;
  200                 }
  201         }
  202 
  203         ipo = mtod(m, struct ip *);
  204 
  205 #ifdef MROUTING
  206         if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
  207                 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
  208                         ipip_mroute_input (m, iphlen);
  209                         return;
  210                 }
  211         }
  212 #endif /* MROUTING */
  213 
  214         /* Keep outer ecn field. */
  215         switch (v >> 4) {
  216 #ifdef INET
  217         case 4:
  218                 otos = ipo->ip_tos;
  219                 break;
  220 #endif /* INET */
  221 #ifdef INET6
  222         case 6:
  223                 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
  224                 break;
  225 #endif
  226         default:
  227                 panic("ipip_input: unknown ip version %u (outer)", v>>4);
  228         }
  229 
  230         /* Remove outer IP header */
  231         m_adj(m, iphlen);
  232 
  233         /* Sanity check */
  234         if (m->m_pkthdr.len < sizeof(struct ip))  {
  235                 ipipstat.ipips_hdrops++;
  236                 m_freem(m);
  237                 return;
  238         }
  239 
  240         m_copydata(m, 0, 1, &v);
  241 
  242         switch (v >> 4) {
  243 #ifdef INET
  244         case 4:
  245                 hlen = sizeof(struct ip);
  246                 break;
  247 #endif /* INET */
  248 
  249 #ifdef INET6
  250         case 6:
  251                 hlen = sizeof(struct ip6_hdr);
  252                 break;
  253 #endif
  254         default:
  255                 ipipstat.ipips_family++;
  256                 m_freem(m);
  257                 return; /* EAFNOSUPPORT */
  258         }
  259 
  260         /*
  261          * Bring the inner IP header in the first mbuf, if not there already.
  262          */
  263         if (m->m_len < hlen) {
  264                 if ((m = m_pullup(m, hlen)) == NULL) {
  265                         DPRINTF(("%s: m_pullup (2) failed\n", __func__));
  266                         ipipstat.ipips_hdrops++;
  267                         return;
  268                 }
  269         }
  270 
  271         /*
  272          * RFC 1853 specifies that the inner TTL should not be touched on
  273          * decapsulation. There's no reason this comment should be here, but
  274          * this is as good as any a position.
  275          */
  276 
  277         /* Some sanity checks in the inner IP header */
  278         switch (v >> 4) {
  279 #ifdef INET
  280         case 4:
  281                 ipo = mtod(m, struct ip *);
  282                 nxt = ipo->ip_p;
  283                 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
  284                 break;
  285 #endif /* INET */
  286 #ifdef INET6
  287         case 6:
  288                 ip6 = (struct ip6_hdr *) ipo;
  289                 nxt = ip6->ip6_nxt;
  290                 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
  291                 ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
  292                 ip6->ip6_flow &= ~htonl(0xff << 20);
  293                 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
  294                 break;
  295 #endif
  296         default:
  297                 panic("ipip_input: unknown ip version %u (inner)", v>>4);
  298         }
  299 
  300         /* Check for local address spoofing. */
  301         if ((m->m_pkthdr.rcvif == NULL ||
  302             !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
  303             ipip_allow != 2) {
  304                 IFNET_RLOCK();
  305                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
  306                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  307 #ifdef INET
  308                                 if (ipo) {
  309                                         if (ifa->ifa_addr->sa_family !=
  310                                             AF_INET)
  311                                                 continue;
  312 
  313                                         sin = (struct sockaddr_in *) ifa->ifa_addr;
  314 
  315                                         if (sin->sin_addr.s_addr ==
  316                                             ipo->ip_src.s_addr) {
  317                                                 ipipstat.ipips_spoof++;
  318                                                 m_freem(m);
  319                                                 IFNET_RUNLOCK();
  320                                                 return;
  321                                         }
  322                                 }
  323 #endif /* INET */
  324 
  325 #ifdef INET6
  326                                 if (ip6) {
  327                                         if (ifa->ifa_addr->sa_family !=
  328                                             AF_INET6)
  329                                                 continue;
  330 
  331                                         sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  332 
  333                                         if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
  334                                                 ipipstat.ipips_spoof++;
  335                                                 m_freem(m);
  336                                                 IFNET_RUNLOCK();
  337                                                 return;
  338                                         }
  339 
  340                                 }
  341 #endif /* INET6 */
  342                         }
  343                 }
  344                 IFNET_RUNLOCK();
  345         }
  346 
  347         /* Statistics */
  348         ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
  349 
  350 #ifdef DEV_ENC
  351         switch (v >> 4) {
  352 #ifdef INET
  353         case 4:
  354                 ipsec_bpf(m, NULL, AF_INET, ENC_IN|ENC_AFTER);
  355                 break;
  356 #endif
  357 #ifdef INET6
  358         case 6:
  359                 ipsec_bpf(m, NULL, AF_INET6, ENC_IN|ENC_AFTER);
  360                 break;
  361 #endif
  362         default:
  363                 panic("%s: bogus ip version %u", __func__, v>>4);
  364         }
  365         /* pass the mbuf to enc0 for packet filtering */
  366         if (ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER) != 0)
  367                 return;
  368 #endif
  369 
  370         /*
  371          * Interface pointer stays the same; if no IPsec processing has
  372          * been done (or will be done), this will point to a normal
  373          * interface. Otherwise, it'll point to an enc interface, which
  374          * will allow a packet filter to distinguish between secure and
  375          * untrusted packets.
  376          */
  377 
  378         switch (v >> 4) {
  379 #ifdef INET
  380         case 4:
  381                 isr = NETISR_IP;
  382                 break;
  383 #endif
  384 #ifdef INET6
  385         case 6:
  386                 isr = NETISR_IPV6;
  387                 break;
  388 #endif
  389         default:
  390                 panic("%s: bogus ip version %u", __func__, v>>4);
  391         }
  392 
  393         if (netisr_queue(isr, m)) {     /* (0) on success. */
  394                 ipipstat.ipips_qfull++;
  395                 DPRINTF(("%s: packet dropped because of full queue\n",
  396                         __func__));
  397         }
  398 }
  399 
  400 int
  401 ipip_output(
  402         struct mbuf *m,
  403         struct ipsecrequest *isr,
  404         struct mbuf **mp,
  405         int skip,
  406         int protoff
  407 )
  408 {
  409         struct secasvar *sav;
  410         u_int8_t tp, otos;
  411         struct secasindex *saidx;
  412         int error;
  413 #ifdef INET
  414         u_int8_t itos;
  415         struct ip *ipo;
  416 #endif /* INET */
  417 #ifdef INET6
  418         struct ip6_hdr *ip6, *ip6o;
  419 #endif /* INET6 */
  420 
  421         IPSEC_SPLASSERT_SOFTNET(__func__);
  422 
  423         sav = isr->sav;
  424         IPSEC_ASSERT(sav != NULL, ("null SA"));
  425         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  426 
  427         /* XXX Deal with empty TDB source/destination addresses. */
  428 
  429         m_copydata(m, 0, 1, &tp);
  430         tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
  431 
  432         saidx = &sav->sah->saidx;
  433         switch (saidx->dst.sa.sa_family) {
  434 #ifdef INET
  435         case AF_INET:
  436                 if (saidx->src.sa.sa_family != AF_INET ||
  437                     saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
  438                     saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
  439                         DPRINTF(("%s: unspecified tunnel endpoint "
  440                             "address in SA %s/%08lx\n", __func__,
  441                             ipsec_address(&saidx->dst),
  442                             (u_long) ntohl(sav->spi)));
  443                         ipipstat.ipips_unspec++;
  444                         error = EINVAL;
  445                         goto bad;
  446                 }
  447 
  448                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
  449                 if (m == 0) {
  450                         DPRINTF(("%s: M_PREPEND failed\n", __func__));
  451                         ipipstat.ipips_hdrops++;
  452                         error = ENOBUFS;
  453                         goto bad;
  454                 }
  455 
  456                 ipo = mtod(m, struct ip *);
  457 
  458                 ipo->ip_v = IPVERSION;
  459                 ipo->ip_hl = 5;
  460                 ipo->ip_len = htons(m->m_pkthdr.len);
  461                 ipo->ip_ttl = ip_defttl;
  462                 ipo->ip_sum = 0;
  463                 ipo->ip_src = saidx->src.sin.sin_addr;
  464                 ipo->ip_dst = saidx->dst.sin.sin_addr;
  465 
  466                 ipo->ip_id = ip_newid();
  467 
  468                 /* If the inner protocol is IP... */
  469                 if (tp == IPVERSION) {
  470                         /* Save ECN notification */
  471                         m_copydata(m, sizeof(struct ip) +
  472                             offsetof(struct ip, ip_tos),
  473                             sizeof(u_int8_t), (caddr_t) &itos);
  474 
  475                         ipo->ip_p = IPPROTO_IPIP;
  476 
  477                         /*
  478                          * We should be keeping tunnel soft-state and
  479                          * send back ICMPs if needed.
  480                          */
  481                         m_copydata(m, sizeof(struct ip) +
  482                             offsetof(struct ip, ip_off),
  483                             sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
  484                         ipo->ip_off = ntohs(ipo->ip_off);
  485                         ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
  486                         ipo->ip_off = htons(ipo->ip_off);
  487                 }
  488 #ifdef INET6
  489                 else if (tp == (IPV6_VERSION >> 4)) {
  490                         u_int32_t itos32;
  491 
  492                         /* Save ECN notification. */
  493                         m_copydata(m, sizeof(struct ip) +
  494                             offsetof(struct ip6_hdr, ip6_flow),
  495                             sizeof(u_int32_t), (caddr_t) &itos32);
  496                         itos = ntohl(itos32) >> 20;
  497                         ipo->ip_p = IPPROTO_IPV6;
  498                         ipo->ip_off = 0;
  499                 }
  500 #endif /* INET6 */
  501                 else {
  502                         goto nofamily;
  503                 }
  504 
  505                 otos = 0;
  506                 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
  507                 ipo->ip_tos = otos;
  508                 break;
  509 #endif /* INET */
  510 
  511 #ifdef INET6
  512         case AF_INET6:
  513                 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
  514                     saidx->src.sa.sa_family != AF_INET6 ||
  515                     IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
  516                         DPRINTF(("%s: unspecified tunnel endpoint "
  517                             "address in SA %s/%08lx\n", __func__,
  518                             ipsec_address(&saidx->dst),
  519                             (u_long) ntohl(sav->spi)));
  520                         ipipstat.ipips_unspec++;
  521                         error = ENOBUFS;
  522                         goto bad;
  523                 }
  524 
  525                 /* scoped address handling */
  526                 ip6 = mtod(m, struct ip6_hdr *);
  527                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
  528                         ip6->ip6_src.s6_addr16[1] = 0;
  529                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
  530                         ip6->ip6_dst.s6_addr16[1] = 0;
  531 
  532                 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
  533                 if (m == 0) {
  534                         DPRINTF(("%s: M_PREPEND failed\n", __func__));
  535                         ipipstat.ipips_hdrops++;
  536                         error = ENOBUFS;
  537                         goto bad;
  538                 }
  539 
  540                 /* Initialize IPv6 header */
  541                 ip6o = mtod(m, struct ip6_hdr *);
  542                 ip6o->ip6_flow = 0;
  543                 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
  544                 ip6o->ip6_vfc |= IPV6_VERSION;
  545                 ip6o->ip6_plen = htons(m->m_pkthdr.len);
  546                 ip6o->ip6_hlim = ip_defttl;
  547                 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
  548                 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
  549 
  550 #ifdef INET
  551                 if (tp == IPVERSION) {
  552                         /* Save ECN notification */
  553                         m_copydata(m, sizeof(struct ip6_hdr) +
  554                             offsetof(struct ip, ip_tos), sizeof(u_int8_t),
  555                             (caddr_t) &itos);
  556 
  557                         /* This is really IPVERSION. */
  558                         ip6o->ip6_nxt = IPPROTO_IPIP;
  559                 } else
  560 #endif /* INET */
  561                         if (tp == (IPV6_VERSION >> 4)) {
  562                                 u_int32_t itos32;
  563 
  564                                 /* Save ECN notification. */
  565                                 m_copydata(m, sizeof(struct ip6_hdr) +
  566                                     offsetof(struct ip6_hdr, ip6_flow),
  567                                     sizeof(u_int32_t), (caddr_t) &itos32);
  568                                 itos = ntohl(itos32) >> 20;
  569 
  570                                 ip6o->ip6_nxt = IPPROTO_IPV6;
  571                         } else {
  572                                 goto nofamily;
  573                         }
  574 
  575                 otos = 0;
  576                 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
  577                 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
  578                 break;
  579 #endif /* INET6 */
  580 
  581         default:
  582 nofamily:
  583                 DPRINTF(("%s: unsupported protocol family %u\n", __func__,
  584                     saidx->dst.sa.sa_family));
  585                 ipipstat.ipips_family++;
  586                 error = EAFNOSUPPORT;           /* XXX diffs from openbsd */
  587                 goto bad;
  588         }
  589 
  590         ipipstat.ipips_opackets++;
  591         *mp = m;
  592 
  593 #ifdef INET
  594         if (saidx->dst.sa.sa_family == AF_INET) {
  595 #if 0
  596                 if (sav->tdb_xform->xf_type == XF_IP4)
  597                         tdb->tdb_cur_bytes +=
  598                             m->m_pkthdr.len - sizeof(struct ip);
  599 #endif
  600                 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
  601         }
  602 #endif /* INET */
  603 
  604 #ifdef INET6
  605         if (saidx->dst.sa.sa_family == AF_INET6) {
  606 #if 0
  607                 if (sav->tdb_xform->xf_type == XF_IP4)
  608                         tdb->tdb_cur_bytes +=
  609                             m->m_pkthdr.len - sizeof(struct ip6_hdr);
  610 #endif
  611                 ipipstat.ipips_obytes +=
  612                     m->m_pkthdr.len - sizeof(struct ip6_hdr);
  613         }
  614 #endif /* INET6 */
  615 
  616         return 0;
  617 bad:
  618         if (m)
  619                 m_freem(m);
  620         *mp = NULL;
  621         return (error);
  622 }
  623 
  624 #ifdef IPSEC
  625 static int
  626 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
  627 {
  628         sav->tdb_xform = xsp;
  629         return 0;
  630 }
  631 
  632 static int
  633 ipe4_zeroize(struct secasvar *sav)
  634 {
  635         sav->tdb_xform = NULL;
  636         return 0;
  637 }
  638 
  639 static int
  640 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  641 {
  642         /* This is a rather serious mistake, so no conditional printing. */
  643         printf("%s: should never be called\n", __func__);
  644         if (m)
  645                 m_freem(m);
  646         return EOPNOTSUPP;
  647 }
  648 
  649 static struct xformsw ipe4_xformsw = {
  650         XF_IP4,         0,              "IPv4 Simple Encapsulation",
  651         ipe4_init,      ipe4_zeroize,   ipe4_input,     ipip_output,
  652 };
  653 
  654 extern struct domain inetdomain;
  655 static struct protosw ipe4_protosw = {
  656         .pr_type =      SOCK_RAW,
  657         .pr_domain =    &inetdomain,
  658         .pr_protocol =  IPPROTO_IPV4,
  659         .pr_flags =     PR_ATOMIC|PR_ADDR|PR_LASTHDR,
  660         .pr_input =     ip4_input,
  661         .pr_ctloutput = rip_ctloutput,
  662         .pr_usrreqs =   &rip_usrreqs
  663 };
  664 #ifdef INET6
  665 static struct ip6protosw ipe6_protosw = {
  666         .pr_type =      SOCK_RAW,
  667         .pr_domain =    &inetdomain,
  668         .pr_protocol =  IPPROTO_IPV6,
  669         .pr_flags =     PR_ATOMIC|PR_ADDR|PR_LASTHDR,
  670         .pr_input =     ip4_input6,
  671         .pr_ctloutput = rip_ctloutput,
  672         .pr_usrreqs =   &rip_usrreqs
  673 };
  674 #endif
  675 
  676 /*
  677  * Check the encapsulated packet to see if we want it
  678  */
  679 static int
  680 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
  681 {
  682         /*
  683          * Only take packets coming from IPSEC tunnels; the rest
  684          * must be handled by the gif tunnel code.  Note that we
  685          * also return a minimum priority when we want the packet
  686          * so any explicit gif tunnels take precedence.
  687          */
  688         return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
  689 }
  690 
  691 static void
  692 ipe4_attach(void)
  693 {
  694         xform_register(&ipe4_xformsw);
  695         /* attach to encapsulation framework */
  696         /* XXX save return cookie for detach on module remove */
  697         (void) encap_attach_func(AF_INET, -1,
  698                 ipe4_encapcheck, &ipe4_protosw, NULL);
  699 #ifdef INET6
  700         (void) encap_attach_func(AF_INET6, -1,
  701                 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
  702 #endif
  703 }
  704 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
  705 #endif  /* IPSEC */

Cache object: d997da482b75fe95ceb51856bc632f3d


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