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_faith.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 /*      $KAME: if_faith.c,v 1.23 2001/12/17 13:55:29 sumikawa Exp $     */
    2 
    3 /*-
    4  * Copyright (c) 1982, 1986, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 4. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  * $FreeBSD: releng/8.1/sys/net/if_faith.c 196019 2009-08-01 19:26:27Z rwatson $
   32  */
   33 /*
   34  * derived from
   35  *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
   36  * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
   37  */
   38 
   39 /*
   40  * Loopback interface driver for protocol testing and timing.
   41  */
   42 #include "opt_inet.h"
   43 #include "opt_inet6.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/kernel.h>
   48 #include <sys/mbuf.h>
   49 #include <sys/module.h>
   50 #include <sys/socket.h>
   51 #include <sys/errno.h>
   52 #include <sys/sockio.h>
   53 #include <sys/time.h>
   54 #include <sys/queue.h>
   55 #include <sys/types.h>
   56 #include <sys/malloc.h>
   57 
   58 #include <net/if.h>
   59 #include <net/if_clone.h>
   60 #include <net/if_types.h>
   61 #include <net/netisr.h>
   62 #include <net/route.h>
   63 #include <net/bpf.h>
   64 #include <net/vnet.h>
   65 
   66 #ifdef  INET
   67 #include <netinet/in.h>
   68 #include <netinet/in_systm.h>
   69 #include <netinet/in_var.h>
   70 #include <netinet/ip.h>
   71 #endif
   72 
   73 #ifdef INET6
   74 #ifndef INET
   75 #include <netinet/in.h>
   76 #endif
   77 #include <netinet6/in6_var.h>
   78 #include <netinet/ip6.h>
   79 #include <netinet6/ip6_var.h>
   80 #endif
   81 
   82 #define FAITHNAME       "faith"
   83 
   84 struct faith_softc {
   85         struct ifnet *sc_ifp;
   86 };
   87 
   88 static int faithioctl(struct ifnet *, u_long, caddr_t);
   89 int faithoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
   90         struct route *);
   91 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
   92 #ifdef INET6
   93 static int faithprefix(struct in6_addr *);
   94 #endif
   95 
   96 static int faithmodevent(module_t, int, void *);
   97 
   98 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
   99 
  100 static int      faith_clone_create(struct if_clone *, int, caddr_t);
  101 static void     faith_clone_destroy(struct ifnet *);
  102 
  103 IFC_SIMPLE_DECLARE(faith, 0);
  104 
  105 #define FAITHMTU        1500
  106 
  107 static int
  108 faithmodevent(mod, type, data)
  109         module_t mod;
  110         int type;
  111         void *data;
  112 {
  113 
  114         switch (type) {
  115         case MOD_LOAD:
  116                 if_clone_attach(&faith_cloner);
  117 
  118 #ifdef INET6
  119                 faithprefix_p = faithprefix;
  120 #endif
  121 
  122                 break;
  123         case MOD_UNLOAD:
  124 #ifdef INET6
  125                 faithprefix_p = NULL;
  126 #endif
  127 
  128                 if_clone_detach(&faith_cloner);
  129                 break;
  130         default:
  131                 return EOPNOTSUPP;
  132         }
  133         return 0;
  134 }
  135 
  136 static moduledata_t faith_mod = {
  137         "if_faith",
  138         faithmodevent,
  139         0
  140 };
  141 
  142 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  143 MODULE_VERSION(if_faith, 1);
  144 
  145 static int
  146 faith_clone_create(ifc, unit, params)
  147         struct if_clone *ifc;
  148         int unit;
  149         caddr_t params;
  150 {
  151         struct ifnet *ifp;
  152         struct faith_softc *sc;
  153 
  154         sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
  155         ifp = sc->sc_ifp = if_alloc(IFT_FAITH);
  156         if (ifp == NULL) {
  157                 free(sc, M_FAITH);
  158                 return (ENOSPC);
  159         }
  160 
  161         ifp->if_softc = sc;
  162         if_initname(sc->sc_ifp, ifc->ifc_name, unit);
  163 
  164         ifp->if_mtu = FAITHMTU;
  165         /* Change to BROADCAST experimentaly to announce its prefix. */
  166         ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
  167         ifp->if_ioctl = faithioctl;
  168         ifp->if_output = faithoutput;
  169         ifp->if_hdrlen = 0;
  170         ifp->if_addrlen = 0;
  171         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  172         if_attach(ifp);
  173         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  174         return (0);
  175 }
  176 
  177 static void
  178 faith_clone_destroy(ifp)
  179         struct ifnet *ifp;
  180 {
  181         struct faith_softc *sc = ifp->if_softc;
  182 
  183         bpfdetach(ifp);
  184         if_detach(ifp);
  185         if_free(ifp);
  186         free(sc, M_FAITH);
  187 }
  188 
  189 int
  190 faithoutput(ifp, m, dst, ro)
  191         struct ifnet *ifp;
  192         struct mbuf *m;
  193         struct sockaddr *dst;
  194         struct route *ro;
  195 {
  196         int isr;
  197         u_int32_t af;
  198         struct rtentry *rt = NULL;
  199 
  200         M_ASSERTPKTHDR(m);
  201 
  202         if (ro != NULL)
  203                 rt = ro->ro_rt;
  204         /* BPF writes need to be handled specially. */
  205         if (dst->sa_family == AF_UNSPEC) {
  206                 bcopy(dst->sa_data, &af, sizeof(af));
  207                 dst->sa_family = af;
  208         }
  209 
  210         if (bpf_peers_present(ifp->if_bpf)) {
  211                 af = dst->sa_family;
  212                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
  213         }
  214 
  215         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
  216                 m_freem(m);
  217                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
  218                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  219         }
  220         ifp->if_opackets++;
  221         ifp->if_obytes += m->m_pkthdr.len;
  222         switch (dst->sa_family) {
  223 #ifdef INET
  224         case AF_INET:
  225                 isr = NETISR_IP;
  226                 break;
  227 #endif
  228 #ifdef INET6
  229         case AF_INET6:
  230                 isr = NETISR_IPV6;
  231                 break;
  232 #endif
  233         default:
  234                 m_freem(m);
  235                 return EAFNOSUPPORT;
  236         }
  237 
  238         /* XXX do we need more sanity checks? */
  239 
  240         m->m_pkthdr.rcvif = ifp;
  241         ifp->if_ipackets++;
  242         ifp->if_ibytes += m->m_pkthdr.len;
  243         netisr_dispatch(isr, m);
  244         return (0);
  245 }
  246 
  247 /* ARGSUSED */
  248 static void
  249 faithrtrequest(cmd, rt, info)
  250         int cmd;
  251         struct rtentry *rt;
  252         struct rt_addrinfo *info;
  253 {
  254         RT_LOCK_ASSERT(rt);
  255         rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  256 }
  257 
  258 /*
  259  * Process an ioctl request.
  260  */
  261 /* ARGSUSED */
  262 static int
  263 faithioctl(ifp, cmd, data)
  264         struct ifnet *ifp;
  265         u_long cmd;
  266         caddr_t data;
  267 {
  268         struct ifaddr *ifa;
  269         struct ifreq *ifr = (struct ifreq *)data;
  270         int error = 0;
  271 
  272         switch (cmd) {
  273 
  274         case SIOCSIFADDR:
  275                 ifp->if_flags |= IFF_UP;
  276                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  277                 ifa = (struct ifaddr *)data;
  278                 ifa->ifa_rtrequest = faithrtrequest;
  279                 /*
  280                  * Everything else is done at a higher level.
  281                  */
  282                 break;
  283 
  284         case SIOCADDMULTI:
  285         case SIOCDELMULTI:
  286                 if (ifr == 0) {
  287                         error = EAFNOSUPPORT;           /* XXX */
  288                         break;
  289                 }
  290                 switch (ifr->ifr_addr.sa_family) {
  291 #ifdef INET
  292                 case AF_INET:
  293                         break;
  294 #endif
  295 #ifdef INET6
  296                 case AF_INET6:
  297                         break;
  298 #endif
  299 
  300                 default:
  301                         error = EAFNOSUPPORT;
  302                         break;
  303                 }
  304                 break;
  305 
  306 #ifdef SIOCSIFMTU
  307         case SIOCSIFMTU:
  308                 ifp->if_mtu = ifr->ifr_mtu;
  309                 break;
  310 #endif
  311 
  312         case SIOCSIFFLAGS:
  313                 break;
  314 
  315         default:
  316                 error = EINVAL;
  317         }
  318         return (error);
  319 }
  320 
  321 #ifdef INET6
  322 /*
  323  * XXX could be slow
  324  * XXX could be layer violation to call sys/net from sys/netinet6
  325  */
  326 static int
  327 faithprefix(in6)
  328         struct in6_addr *in6;
  329 {
  330         struct rtentry *rt;
  331         struct sockaddr_in6 sin6;
  332         int ret;
  333 
  334         if (V_ip6_keepfaith == 0)
  335                 return 0;
  336 
  337         bzero(&sin6, sizeof(sin6));
  338         sin6.sin6_family = AF_INET6;
  339         sin6.sin6_len = sizeof(struct sockaddr_in6);
  340         sin6.sin6_addr = *in6;
  341         rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
  342         if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
  343             (rt->rt_ifp->if_flags & IFF_UP) != 0)
  344                 ret = 1;
  345         else
  346                 ret = 0;
  347         if (rt)
  348                 RTFREE_LOCKED(rt);
  349         return ret;
  350 }
  351 #endif

Cache object: 7c952f29254b224444cc482cfe3735aa


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