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/net/if_loop.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  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) 1982, 1986, 1993
    3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)if_loop.c   8.2 (Berkeley) 1/9/95
   30  * $FreeBSD: releng/8.1/sys/net/if_loop.c 208679 2010-05-31 22:18:42Z rwatson $
   31  */
   32 
   33 /*
   34  * Loopback interface driver for protocol testing and timing.
   35  */
   36 
   37 #include "opt_atalk.h"
   38 #include "opt_inet.h"
   39 #include "opt_inet6.h"
   40 #include "opt_ipx.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/mbuf.h>
   46 #include <sys/module.h>
   47 #include <machine/bus.h>
   48 #include <sys/rman.h>
   49 #include <sys/socket.h>
   50 #include <sys/sockio.h>
   51 #include <sys/sysctl.h>
   52 
   53 #include <net/if.h>
   54 #include <net/if_clone.h>
   55 #include <net/if_types.h>
   56 #include <net/netisr.h>
   57 #include <net/route.h>
   58 #include <net/bpf.h>
   59 #include <net/vnet.h>
   60 
   61 #ifdef  INET
   62 #include <netinet/in.h>
   63 #include <netinet/in_var.h>
   64 #endif
   65 
   66 #ifdef IPX
   67 #include <netipx/ipx.h>
   68 #include <netipx/ipx_if.h>
   69 #endif
   70 
   71 #ifdef INET6
   72 #ifndef INET
   73 #include <netinet/in.h>
   74 #endif
   75 #include <netinet6/in6_var.h>
   76 #include <netinet/ip6.h>
   77 #endif
   78 
   79 #ifdef NETATALK
   80 #include <netatalk/at.h>
   81 #include <netatalk/at_var.h>
   82 #endif
   83 
   84 #include <security/mac/mac_framework.h>
   85 
   86 #ifdef TINY_LOMTU
   87 #define LOMTU   (1024+512)
   88 #elif defined(LARGE_LOMTU)
   89 #define LOMTU   131072
   90 #else
   91 #define LOMTU   16384
   92 #endif
   93 
   94 #define LO_CSUM_FEATURES        (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
   95 #define LO_CSUM_SET             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR | \
   96                                     CSUM_IP_CHECKED | CSUM_IP_VALID | \
   97                                     CSUM_SCTP_VALID)
   98 
   99 int             loioctl(struct ifnet *, u_long, caddr_t);
  100 static void     lortrequest(int, struct rtentry *, struct rt_addrinfo *);
  101 int             looutput(struct ifnet *ifp, struct mbuf *m,
  102                     struct sockaddr *dst, struct route *ro);
  103 static int      lo_clone_create(struct if_clone *, int, caddr_t);
  104 static void     lo_clone_destroy(struct ifnet *);
  105 
  106 VNET_DEFINE(struct ifnet *, loif);      /* Used externally */
  107 
  108 #ifdef VIMAGE
  109 static VNET_DEFINE(struct ifc_simple_data, lo_cloner_data);
  110 static VNET_DEFINE(struct if_clone, lo_cloner);
  111 #define V_lo_cloner_data        VNET(lo_cloner_data)
  112 #define V_lo_cloner             VNET(lo_cloner)
  113 #endif
  114 
  115 IFC_SIMPLE_DECLARE(lo, 1);
  116 
  117 static void
  118 lo_clone_destroy(struct ifnet *ifp)
  119 {
  120 
  121 #ifndef VIMAGE
  122         /* XXX: destroying lo0 will lead to panics. */
  123         KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
  124 #endif
  125 
  126         bpfdetach(ifp);
  127         if_detach(ifp);
  128         if_free(ifp);
  129 }
  130 
  131 static int
  132 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  133 {
  134         struct ifnet *ifp;
  135 
  136         ifp = if_alloc(IFT_LOOP);
  137         if (ifp == NULL)
  138                 return (ENOSPC);
  139 
  140         if_initname(ifp, ifc->ifc_name, unit);
  141         ifp->if_mtu = LOMTU;
  142         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
  143         ifp->if_ioctl = loioctl;
  144         ifp->if_output = looutput;
  145         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  146         ifp->if_capabilities = ifp->if_capenable = IFCAP_HWCSUM;
  147         ifp->if_hwassist = LO_CSUM_FEATURES;
  148         if_attach(ifp);
  149         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  150         if (V_loif == NULL)
  151                 V_loif = ifp;
  152 
  153         return (0);
  154 }
  155 
  156 static void
  157 vnet_loif_init(const void *unused __unused)
  158 {
  159 
  160 #ifdef VIMAGE
  161         V_lo_cloner = lo_cloner;
  162         V_lo_cloner_data = lo_cloner_data;
  163         V_lo_cloner.ifc_data = &V_lo_cloner_data;
  164         if_clone_attach(&V_lo_cloner);
  165 #else
  166         if_clone_attach(&lo_cloner);
  167 #endif
  168 }
  169 VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  170     vnet_loif_init, NULL);
  171 
  172 #ifdef VIMAGE
  173 static void
  174 vnet_loif_uninit(const void *unused __unused)
  175 {
  176 
  177         if_clone_detach(&V_lo_cloner);
  178         V_loif = NULL;
  179 }
  180 VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  181     vnet_loif_uninit, NULL);
  182 #endif
  183 
  184 static int
  185 loop_modevent(module_t mod, int type, void *data)
  186 {
  187 
  188         switch (type) {
  189         case MOD_LOAD:
  190                 break;
  191 
  192         case MOD_UNLOAD:
  193                 printf("loop module unload - not possible for this module type\n");
  194                 return (EINVAL);
  195 
  196         default:
  197                 return (EOPNOTSUPP);
  198         }
  199         return (0);
  200 }
  201 
  202 static moduledata_t loop_mod = {
  203         "if_lo",
  204         loop_modevent,
  205         0
  206 };
  207 
  208 DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
  209 
  210 int
  211 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
  212     struct route *ro)
  213 {
  214         u_int32_t af;
  215         struct rtentry *rt = NULL;
  216 #ifdef MAC
  217         int error;
  218 #endif
  219 
  220         M_ASSERTPKTHDR(m); /* check if we have the packet header */
  221 
  222         if (ro != NULL)
  223                 rt = ro->ro_rt;
  224 #ifdef MAC
  225         error = mac_ifnet_check_transmit(ifp, m);
  226         if (error) {
  227                 m_freem(m);
  228                 return (error);
  229         }
  230 #endif
  231 
  232         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
  233                 m_freem(m);
  234                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
  235                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  236         }
  237 
  238         ifp->if_opackets++;
  239         ifp->if_obytes += m->m_pkthdr.len;
  240 
  241         /* BPF writes need to be handled specially. */
  242         if (dst->sa_family == AF_UNSPEC) {
  243                 bcopy(dst->sa_data, &af, sizeof(af));
  244                 dst->sa_family = af;
  245         }
  246 
  247 #if 1   /* XXX */
  248         switch (dst->sa_family) {
  249         case AF_INET:
  250                 if (ifp->if_capenable & IFCAP_RXCSUM) {
  251                         m->m_pkthdr.csum_data = 0xffff;
  252                         m->m_pkthdr.csum_flags = LO_CSUM_SET;
  253                 }
  254                 m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
  255         case AF_INET6:
  256         case AF_IPX:
  257         case AF_APPLETALK:
  258                 break;
  259         default:
  260                 printf("looutput: af=%d unexpected\n", dst->sa_family);
  261                 m_freem(m);
  262                 return (EAFNOSUPPORT);
  263         }
  264 #endif
  265         return (if_simloop(ifp, m, dst->sa_family, 0));
  266 }
  267 
  268 /*
  269  * if_simloop()
  270  *
  271  * This function is to support software emulation of hardware loopback,
  272  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
  273  * hear their own broadcasts, we create a copy of the packet that we
  274  * would normally receive via a hardware loopback.
  275  *
  276  * This function expects the packet to include the media header of length hlen.
  277  */
  278 int
  279 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
  280 {
  281         int isr;
  282 
  283         M_ASSERTPKTHDR(m);
  284         m_tag_delete_nonpersistent(m);
  285         m->m_pkthdr.rcvif = ifp;
  286 
  287 #ifdef MAC
  288         mac_ifnet_create_mbuf(ifp, m);
  289 #endif
  290 
  291         /*
  292          * Let BPF see incoming packet in the following manner:
  293          *  - Emulated packet loopback for a simplex interface
  294          *    (net/if_ethersubr.c)
  295          *      -> passes it to ifp's BPF
  296          *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
  297          *      -> not passes it to any BPF
  298          *  - Normal packet loopback from myself to myself (net/if_loop.c)
  299          *      -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
  300          */
  301         if (hlen > 0) {
  302                 if (bpf_peers_present(ifp->if_bpf)) {
  303                         bpf_mtap(ifp->if_bpf, m);
  304                 }
  305         } else {
  306                 if (bpf_peers_present(V_loif->if_bpf)) {
  307                         if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
  308                                 /* XXX beware sizeof(af) != 4 */
  309                                 u_int32_t af1 = af;
  310 
  311                                 /*
  312                                  * We need to prepend the address family.
  313                                  */
  314                                 bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
  315                         }
  316                 }
  317         }
  318 
  319         /* Strip away media header */
  320         if (hlen > 0) {
  321                 m_adj(m, hlen);
  322 #ifndef __NO_STRICT_ALIGNMENT
  323                 /*
  324                  * Some archs do not like unaligned data, so
  325                  * we move data down in the first mbuf.
  326                  */
  327                 if (mtod(m, vm_offset_t) & 3) {
  328                         KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
  329                         bcopy(m->m_data,
  330                             (char *)(mtod(m, vm_offset_t)
  331                                 - (mtod(m, vm_offset_t) & 3)),
  332                             m->m_len);
  333                         m->m_data -= (mtod(m,vm_offset_t) & 3);
  334                 }
  335 #endif
  336         }
  337 
  338         /* Deliver to upper layer protocol */
  339         switch (af) {
  340 #ifdef INET
  341         case AF_INET:
  342                 isr = NETISR_IP;
  343                 break;
  344 #endif
  345 #ifdef INET6
  346         case AF_INET6:
  347                 m->m_flags |= M_LOOP;
  348                 isr = NETISR_IPV6;
  349                 break;
  350 #endif
  351 #ifdef IPX
  352         case AF_IPX:
  353                 isr = NETISR_IPX;
  354                 break;
  355 #endif
  356 #ifdef NETATALK
  357         case AF_APPLETALK:
  358                 isr = NETISR_ATALK2;
  359                 break;
  360 #endif
  361         default:
  362                 printf("if_simloop: can't handle af=%d\n", af);
  363                 m_freem(m);
  364                 return (EAFNOSUPPORT);
  365         }
  366         ifp->if_ipackets++;
  367         ifp->if_ibytes += m->m_pkthdr.len;
  368         netisr_queue(isr, m);   /* mbuf is free'd on failure. */
  369         return (0);
  370 }
  371 
  372 /* ARGSUSED */
  373 static void
  374 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
  375 {
  376 
  377         RT_LOCK_ASSERT(rt);
  378         rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  379 }
  380 
  381 /*
  382  * Process an ioctl request.
  383  */
  384 /* ARGSUSED */
  385 int
  386 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  387 {
  388         struct ifaddr *ifa;
  389         struct ifreq *ifr = (struct ifreq *)data;
  390         int error = 0, mask;
  391 
  392         switch (cmd) {
  393         case SIOCSIFADDR:
  394                 ifp->if_flags |= IFF_UP;
  395                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  396                 ifa = (struct ifaddr *)data;
  397                 ifa->ifa_rtrequest = lortrequest;
  398                 /*
  399                  * Everything else is done at a higher level.
  400                  */
  401                 break;
  402 
  403         case SIOCADDMULTI:
  404         case SIOCDELMULTI:
  405                 if (ifr == 0) {
  406                         error = EAFNOSUPPORT;           /* XXX */
  407                         break;
  408                 }
  409                 switch (ifr->ifr_addr.sa_family) {
  410 
  411 #ifdef INET
  412                 case AF_INET:
  413                         break;
  414 #endif
  415 #ifdef INET6
  416                 case AF_INET6:
  417                         break;
  418 #endif
  419 
  420                 default:
  421                         error = EAFNOSUPPORT;
  422                         break;
  423                 }
  424                 break;
  425 
  426         case SIOCSIFMTU:
  427                 ifp->if_mtu = ifr->ifr_mtu;
  428                 break;
  429 
  430         case SIOCSIFFLAGS:
  431                 break;
  432 
  433         case SIOCSIFCAP:
  434                 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
  435                 if ((mask & IFCAP_RXCSUM) != 0)
  436                         ifp->if_capenable ^= IFCAP_RXCSUM;
  437                 if ((mask & IFCAP_TXCSUM) != 0)
  438                         ifp->if_capenable ^= IFCAP_TXCSUM;
  439                 if (ifp->if_capenable & IFCAP_TXCSUM)
  440                         ifp->if_hwassist = LO_CSUM_FEATURES;
  441                 else
  442                         ifp->if_hwassist = 0;
  443                 break;
  444 
  445         default:
  446                 error = EINVAL;
  447         }
  448         return (error);
  449 }

Cache object: fd11448c106ff4b1e626fdd6c837cc38


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