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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
   34  * $FreeBSD$
   35  */
   36 
   37 /*
   38  * Loopback interface driver for protocol testing and timing.
   39  */
   40 #include "loop.h"
   41 
   42 #include "opt_atalk.h"
   43 #include "opt_inet.h"
   44 #include "opt_inet6.h"
   45 #include "opt_ipx.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/kernel.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/socket.h>
   52 #include <sys/sockio.h>
   53 
   54 #include <net/if.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/bpfdesc.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 NS
   80 #include <netns/ns.h>
   81 #include <netns/ns_if.h>
   82 #endif
   83 
   84 #ifdef NETATALK
   85 #include <netatalk/at.h>
   86 #include <netatalk/at_var.h>
   87 #endif NETATALK
   88 
   89 int loioctl __P((struct ifnet *, u_long, caddr_t));
   90 static void lortrequest __P((int, struct rtentry *, struct rt_addrinfo *));
   91 
   92 static void loopattach __P((void *));
   93 PSEUDO_SET(loopattach, if_loop);
   94 
   95 int looutput __P((struct ifnet *ifp,
   96                 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt));
   97 
   98 #ifdef TINY_LOMTU
   99 #define LOMTU   (1024+512)
  100 #elif defined(LARGE_LOMTU)
  101 #define LOMTU   131072
  102 #else
  103 #define LOMTU   16384
  104 #endif
  105 
  106 struct  ifnet loif[NLOOP];
  107 
  108 /* ARGSUSED */
  109 static void
  110 loopattach(dummy)
  111         void *dummy;
  112 {
  113         register struct ifnet *ifp;
  114         register int i = 0;
  115 
  116         for (ifp = loif; i < NLOOP; ifp++) {
  117             ifp->if_name = "lo";
  118             ifp->if_unit = i++;
  119             ifp->if_mtu = LOMTU;
  120             ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
  121             ifp->if_ioctl = loioctl;
  122             ifp->if_output = looutput;
  123             ifp->if_type = IFT_LOOP;
  124             ifp->if_snd.ifq_maxlen = ifqmaxlen;
  125             if_attach(ifp);
  126             bpfattach(ifp, DLT_NULL, sizeof(u_int));
  127         }
  128 }
  129 
  130 int
  131 looutput(ifp, m, dst, rt)
  132         struct ifnet *ifp;
  133         register struct mbuf *m;
  134         struct sockaddr *dst;
  135         register struct rtentry *rt;
  136 {
  137 
  138         if ((m->m_flags & M_PKTHDR) == 0)
  139                 panic("looutput no HDR");
  140 
  141         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
  142                 m_freem(m);
  143                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
  144                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  145         }
  146 
  147         ifp->if_opackets++;
  148         ifp->if_obytes += m->m_pkthdr.len;
  149 #if 1   /* XXX */
  150         switch (dst->sa_family) {
  151         case AF_INET:
  152         case AF_INET6:
  153         case AF_IPX:
  154         case AF_NS:
  155         case AF_APPLETALK:
  156                 break;
  157         default:
  158                 printf("looutput: af=%d unexpected\n", dst->sa_family);
  159                 m_freem(m);
  160                 return (EAFNOSUPPORT);
  161         }
  162 #endif
  163         return(if_simloop(ifp, m, dst->sa_family, 0));
  164 }
  165 
  166 /*
  167  * if_simloop()
  168  *
  169  * This function is to support software emulation of hardware loopback,
  170  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
  171  * hear their own broadcasts, we create a copy of the packet that we
  172  * would normally receive via a hardware loopback.
  173  *
  174  * This function expects the packet to include the media header of length hlen.
  175  */
  176 
  177 int
  178 if_simloop(ifp, m, af, hlen)
  179         struct ifnet *ifp;
  180         struct mbuf *m;
  181         int af;
  182         int hlen;
  183 {
  184         int s, isr;
  185         register struct ifqueue *ifq = 0;
  186 
  187         KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
  188         m->m_pkthdr.rcvif = ifp;
  189 
  190         /* BPF write needs to be handled specially */
  191         if (af == AF_UNSPEC) {
  192                 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
  193                 af = *(mtod(m, int *));
  194                 m->m_len -= sizeof(int);
  195                 m->m_pkthdr.len -= sizeof(int);
  196                 m->m_data += sizeof(int);
  197         }
  198 
  199         /* Let BPF see incoming packet */
  200         if (ifp->if_bpf) {
  201                 struct mbuf m0, *n = m;
  202 
  203                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
  204                         /*
  205                          * We need to prepend the address family as
  206                          * a four byte field.  Cons up a dummy header
  207                          * to pacify bpf.  This is safe because bpf
  208                          * will only read from the mbuf (i.e., it won't
  209                          * try to free it or keep a pointer a to it).
  210                          */
  211                         m0.m_next = m;
  212                         m0.m_len = 4;
  213                         m0.m_data = (char *)&af;
  214                         n = &m0;
  215                 }
  216                 bpf_mtap(ifp, n);
  217         }
  218 
  219         /* Strip away media header */
  220         if (hlen > 0) {
  221                 m_adj(m, hlen);
  222 #ifdef __alpha__
  223                 /* The alpha doesn't like unaligned data.
  224                  * We move data down in the first mbuf */
  225                 if (mtod(m, vm_offset_t) & 3) {
  226                         KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
  227                         bcopy(m->m_data, 
  228                             (char *)(mtod(m, vm_offset_t) 
  229                                 - (mtod(m, vm_offset_t) & 3)),
  230                             m->m_len);
  231                         mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3);
  232                 }
  233 #endif
  234         }
  235 
  236         /* Deliver to upper layer protocol */
  237         switch (af) {
  238 #ifdef INET
  239         case AF_INET:
  240                 ifq = &ipintrq;
  241                 isr = NETISR_IP;
  242                 break;
  243 #endif
  244 #ifdef INET6
  245         case AF_INET6:
  246                 m->m_flags |= M_LOOP;
  247                 ifq = &ip6intrq;
  248                 isr = NETISR_IPV6;
  249                 break;
  250 #endif
  251 #ifdef IPX
  252         case AF_IPX:
  253                 ifq = &ipxintrq;
  254                 isr = NETISR_IPX;
  255                 break;
  256 #endif
  257 #ifdef NS
  258         case AF_NS:
  259                 ifq = &nsintrq;
  260                 isr = NETISR_NS;
  261                 break;
  262 #endif
  263 #ifdef NETATALK
  264         case AF_APPLETALK:
  265                 ifq = &atintrq2;
  266                 isr = NETISR_ATALK;
  267                 break;
  268 #endif NETATALK
  269         default:
  270                 printf("if_simloop: can't handle af=%d\n", af);
  271                 m_freem(m);
  272                 return (EAFNOSUPPORT);
  273         }
  274         s = splimp();
  275         if (IF_QFULL(ifq)) {
  276                 IF_DROP(ifq);
  277                 m_freem(m);
  278                 splx(s);
  279                 return (ENOBUFS);
  280         }
  281         IF_ENQUEUE(ifq, m);
  282         schednetisr(isr);
  283         ifp->if_ipackets++;
  284         ifp->if_ibytes += m->m_pkthdr.len;
  285         splx(s);
  286         return (0);
  287 }
  288 
  289 /* ARGSUSED */
  290 static void
  291 lortrequest(cmd, rt, info)
  292         int cmd;
  293         struct rtentry *rt;
  294         struct rt_addrinfo *info;
  295 {
  296         if (rt) {
  297                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
  298                 /*
  299                  * For optimal performance, the send and receive buffers
  300                  * should be at least twice the MTU plus a little more for
  301                  * overhead.
  302                  */
  303                 rt->rt_rmx.rmx_recvpipe =
  304                         rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
  305         }
  306 }
  307 
  308 /*
  309  * Process an ioctl request.
  310  */
  311 /* ARGSUSED */
  312 int
  313 loioctl(ifp, cmd, data)
  314         register struct ifnet *ifp;
  315         u_long cmd;
  316         caddr_t data;
  317 {
  318         register struct ifaddr *ifa;
  319         register struct ifreq *ifr = (struct ifreq *)data;
  320         register int error = 0;
  321 
  322         switch (cmd) {
  323 
  324         case SIOCSIFADDR:
  325                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
  326                 ifa = (struct ifaddr *)data;
  327                 ifa->ifa_rtrequest = lortrequest;
  328                 /*
  329                  * Everything else is done at a higher level.
  330                  */
  331                 break;
  332 
  333         case SIOCADDMULTI:
  334         case SIOCDELMULTI:
  335                 if (ifr == 0) {
  336                         error = EAFNOSUPPORT;           /* XXX */
  337                         break;
  338                 }
  339                 switch (ifr->ifr_addr.sa_family) {
  340 
  341 #ifdef INET
  342                 case AF_INET:
  343                         break;
  344 #endif
  345 #ifdef INET6
  346                 case AF_INET6:
  347                         break;
  348 #endif
  349 
  350                 default:
  351                         error = EAFNOSUPPORT;
  352                         break;
  353                 }
  354                 break;
  355 
  356         case SIOCSIFMTU:
  357                 ifp->if_mtu = ifr->ifr_mtu;
  358                 break;
  359 
  360         case SIOCSIFFLAGS:
  361                 break;
  362 
  363         default:
  364                 error = EINVAL;
  365         }
  366         return (error);
  367 }

Cache object: 48594750f60fa48d6d1883836d06790f


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