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  -  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) 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/6.1/sys/net/if_loop.c 149443 2005-08-25 05:01:24Z 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/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/module.h>
   48 #include <machine/bus.h>
   49 #include <sys/rman.h>
   50 #include <sys/socket.h>
   51 #include <sys/sockio.h>
   52 #include <sys/sysctl.h>
   53 
   54 #include <net/if.h>
   55 #include <net/if_clone.h>
   56 #include <net/if_types.h>
   57 #include <net/netisr.h>
   58 #include <net/route.h>
   59 #include <net/bpf.h>
   60 #include <net/bpfdesc.h>
   61 
   62 #ifdef  INET
   63 #include <netinet/in.h>
   64 #include <netinet/in_var.h>
   65 #endif
   66 
   67 #ifdef IPX
   68 #include <netipx/ipx.h>
   69 #include <netipx/ipx_if.h>
   70 #endif
   71 
   72 #ifdef INET6
   73 #ifndef INET
   74 #include <netinet/in.h>
   75 #endif
   76 #include <netinet6/in6_var.h>
   77 #include <netinet/ip6.h>
   78 #endif
   79 
   80 #ifdef NETATALK
   81 #include <netatalk/at.h>
   82 #include <netatalk/at_var.h>
   83 #endif
   84 
   85 #ifdef TINY_LOMTU
   86 #define LOMTU   (1024+512)
   87 #elif defined(LARGE_LOMTU)
   88 #define LOMTU   131072
   89 #else
   90 #define LOMTU   16384
   91 #endif
   92 
   93 #define LONAME  "lo"
   94 
   95 struct lo_softc {
   96         struct  ifnet *sc_ifp;
   97         LIST_ENTRY(lo_softc) sc_next;
   98 };
   99 
  100 int             loioctl(struct ifnet *, u_long, caddr_t);
  101 static void     lortrequest(int, struct rtentry *, struct rt_addrinfo *);
  102 int             looutput(struct ifnet *ifp, struct mbuf *m,
  103                     struct sockaddr *dst, struct rtentry *rt);
  104 static int      lo_clone_create(struct if_clone *, int);
  105 static void     lo_clone_destroy(struct ifnet *);
  106 
  107 struct ifnet *loif = NULL;                      /* Used externally */
  108 
  109 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface");
  110 
  111 static struct mtx lo_mtx;
  112 static LIST_HEAD(lo_list, lo_softc) lo_list;
  113 
  114 IFC_SIMPLE_DECLARE(lo, 1);
  115 
  116 static void
  117 lo_clone_destroy(ifp)
  118         struct ifnet *ifp;
  119 {
  120         struct lo_softc *sc;
  121         
  122         sc = ifp->if_softc;
  123 
  124         /* XXX: destroying lo0 will lead to panics. */
  125         KASSERT(loif != ifp, ("%s: destroying lo0", __func__));
  126 
  127         mtx_lock(&lo_mtx);
  128         LIST_REMOVE(sc, sc_next);
  129         mtx_unlock(&lo_mtx);
  130         bpfdetach(ifp);
  131         if_detach(ifp);
  132         if_free(ifp);
  133         free(sc, M_LO);
  134 }
  135 
  136 static int
  137 lo_clone_create(ifc, unit)
  138         struct if_clone *ifc;
  139         int unit;
  140 {
  141         struct ifnet *ifp;
  142         struct lo_softc *sc;
  143 
  144         MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO);
  145         ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
  146         if (ifp == NULL) {
  147                 free(sc, M_LO);
  148                 return (ENOSPC);
  149         }
  150 
  151         if_initname(ifp, ifc->ifc_name, unit);
  152         ifp->if_mtu = LOMTU;
  153         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
  154         ifp->if_ioctl = loioctl;
  155         ifp->if_output = looutput;
  156         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  157         ifp->if_softc = sc;
  158         if_attach(ifp);
  159         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  160         mtx_lock(&lo_mtx);
  161         LIST_INSERT_HEAD(&lo_list, sc, sc_next);
  162         mtx_unlock(&lo_mtx);
  163         if (loif == NULL)
  164                 loif = ifp;
  165 
  166         return (0);
  167 }
  168 
  169 static int
  170 loop_modevent(module_t mod, int type, void *data) 
  171 { 
  172         switch (type) { 
  173         case MOD_LOAD: 
  174                 mtx_init(&lo_mtx, "lo_mtx", NULL, MTX_DEF);
  175                 LIST_INIT(&lo_list);
  176                 if_clone_attach(&lo_cloner);
  177                 break; 
  178         case MOD_UNLOAD: 
  179                 printf("loop module unload - not possible for this module type\n"); 
  180                 return EINVAL; 
  181         default:
  182                 return EOPNOTSUPP;
  183         } 
  184         return 0; 
  185 } 
  186 
  187 static moduledata_t loop_mod = { 
  188         "loop", 
  189         loop_modevent, 
  190         0
  191 }; 
  192 
  193 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
  194 
  195 int
  196 looutput(ifp, m, dst, rt)
  197         struct ifnet *ifp;
  198         register struct mbuf *m;
  199         struct sockaddr *dst;
  200         register struct rtentry *rt;
  201 {
  202         u_int32_t af;
  203 
  204         M_ASSERTPKTHDR(m); /* check if we have the packet header */
  205 
  206         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
  207                 m_freem(m);
  208                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
  209                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  210         }
  211 
  212         ifp->if_opackets++;
  213         ifp->if_obytes += m->m_pkthdr.len;
  214 
  215         /* BPF writes need to be handled specially. */
  216         if (dst->sa_family == AF_UNSPEC) {
  217                 bcopy(dst->sa_data, &af, sizeof(af));
  218                 dst->sa_family = af;
  219         }
  220 
  221 #if 1   /* XXX */
  222         switch (dst->sa_family) {
  223         case AF_INET:
  224         case AF_INET6:
  225         case AF_IPX:
  226         case AF_APPLETALK:
  227                 break;
  228         default:
  229                 printf("looutput: af=%d unexpected\n", dst->sa_family);
  230                 m_freem(m);
  231                 return (EAFNOSUPPORT);
  232         }
  233 #endif
  234         return(if_simloop(ifp, m, dst->sa_family, 0));
  235 }
  236 
  237 /*
  238  * if_simloop()
  239  *
  240  * This function is to support software emulation of hardware loopback,
  241  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
  242  * hear their own broadcasts, we create a copy of the packet that we
  243  * would normally receive via a hardware loopback.
  244  *
  245  * This function expects the packet to include the media header of length hlen.
  246  */
  247 
  248 int
  249 if_simloop(ifp, m, af, hlen)
  250         struct ifnet *ifp;
  251         struct mbuf *m;
  252         int af;
  253         int hlen;
  254 {
  255         int isr;
  256 
  257         M_ASSERTPKTHDR(m);
  258         m_tag_delete_nonpersistent(m);
  259         m->m_pkthdr.rcvif = ifp;
  260 
  261         /* Let BPF see incoming packet */
  262         if (ifp->if_bpf) {
  263                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
  264                         u_int32_t af1 = af;     /* XXX beware sizeof(af) != 4 */
  265                         /*
  266                          * We need to prepend the address family.
  267                          */
  268                         bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
  269                 } else
  270                         bpf_mtap(ifp->if_bpf, m);
  271         }
  272 
  273         /* Strip away media header */
  274         if (hlen > 0) {
  275                 m_adj(m, hlen);
  276 #if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__)
  277                 /* The alpha doesn't like unaligned data.
  278                  * We move data down in the first mbuf */
  279                 if (mtod(m, vm_offset_t) & 3) {
  280                         KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
  281                         bcopy(m->m_data, 
  282                             (char *)(mtod(m, vm_offset_t) 
  283                                 - (mtod(m, vm_offset_t) & 3)),
  284                             m->m_len);
  285                         m->m_data -= (mtod(m,vm_offset_t) & 3);
  286                 }
  287 #endif
  288         }
  289 
  290         /* Deliver to upper layer protocol */
  291         switch (af) {
  292 #ifdef INET
  293         case AF_INET:
  294                 isr = NETISR_IP;
  295                 break;
  296 #endif
  297 #ifdef INET6
  298         case AF_INET6:
  299                 m->m_flags |= M_LOOP;
  300                 isr = NETISR_IPV6;
  301                 break;
  302 #endif
  303 #ifdef IPX
  304         case AF_IPX:
  305                 isr = NETISR_IPX;
  306                 break;
  307 #endif
  308 #ifdef NETATALK
  309         case AF_APPLETALK:
  310                 isr = NETISR_ATALK2;
  311                 break;
  312 #endif
  313         default:
  314                 printf("if_simloop: can't handle af=%d\n", af);
  315                 m_freem(m);
  316                 return (EAFNOSUPPORT);
  317         }
  318         ifp->if_ipackets++;
  319         ifp->if_ibytes += m->m_pkthdr.len;
  320         netisr_queue(isr, m);   /* mbuf is free'd on failure. */
  321         return (0);
  322 }
  323 
  324 /* ARGSUSED */
  325 static void
  326 lortrequest(cmd, rt, info)
  327         int cmd;
  328         struct rtentry *rt;
  329         struct rt_addrinfo *info;
  330 {
  331         RT_LOCK_ASSERT(rt);
  332         rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  333 }
  334 
  335 /*
  336  * Process an ioctl request.
  337  */
  338 /* ARGSUSED */
  339 int
  340 loioctl(ifp, cmd, data)
  341         register struct ifnet *ifp;
  342         u_long cmd;
  343         caddr_t data;
  344 {
  345         register struct ifaddr *ifa;
  346         register struct ifreq *ifr = (struct ifreq *)data;
  347         register int error = 0;
  348 
  349         switch (cmd) {
  350 
  351         case SIOCSIFADDR:
  352                 ifp->if_flags |= IFF_UP;
  353                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  354                 ifa = (struct ifaddr *)data;
  355                 ifa->ifa_rtrequest = lortrequest;
  356                 /*
  357                  * Everything else is done at a higher level.
  358                  */
  359                 break;
  360 
  361         case SIOCADDMULTI:
  362         case SIOCDELMULTI:
  363                 if (ifr == 0) {
  364                         error = EAFNOSUPPORT;           /* XXX */
  365                         break;
  366                 }
  367                 switch (ifr->ifr_addr.sa_family) {
  368 
  369 #ifdef INET
  370                 case AF_INET:
  371                         break;
  372 #endif
  373 #ifdef INET6
  374                 case AF_INET6:
  375                         break;
  376 #endif
  377 
  378                 default:
  379                         error = EAFNOSUPPORT;
  380                         break;
  381                 }
  382                 break;
  383 
  384         case SIOCSIFMTU:
  385                 ifp->if_mtu = ifr->ifr_mtu;
  386                 break;
  387 
  388         case SIOCSIFFLAGS:
  389                 break;
  390 
  391         default:
  392                 error = EINVAL;
  393         }
  394         return (error);
  395 }

Cache object: 73f260589ddd44285c65ed0053619f5c


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