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_ef.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) 1999, 2000 Boris Popov
    3  * 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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: releng/6.2/sys/net/if_ef.c 155690 2006-02-14 21:11:19Z rwatson $
   27  */
   28 
   29 #include "opt_inet.h"
   30 #include "opt_ipx.h"
   31 #include "opt_ef.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/sockio.h>
   36 #include <sys/malloc.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/socket.h>
   39 #include <sys/syslog.h>
   40 #include <sys/kernel.h>
   41 #include <sys/module.h>
   42 
   43 #include <net/ethernet.h>
   44 #include <net/if_llc.h>
   45 #include <net/if.h>
   46 #include <net/if_arp.h>
   47 #include <net/if_dl.h>
   48 #include <net/if_types.h>
   49 #include <net/netisr.h>
   50 #include <net/route.h>
   51 #include <net/bpf.h>
   52 
   53 #ifdef INET
   54 #include <netinet/in.h>
   55 #include <netinet/in_var.h>
   56 #include <netinet/if_ether.h>
   57 #endif
   58 
   59 #ifdef IPX
   60 #include <netipx/ipx.h>
   61 #include <netipx/ipx_if.h>
   62 #endif
   63 
   64 /* If none of the supported layers is enabled explicitly enable them all */
   65 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
   66     !defined(ETHER_SNAP)
   67 #define ETHER_II        1
   68 #define ETHER_8023      1
   69 #define ETHER_8022      1
   70 #define ETHER_SNAP      1
   71 #endif
   72 
   73 /* internal frame types */
   74 #define ETHER_FT_EII            0       /* Ethernet_II - default */
   75 #define ETHER_FT_8023           1       /* 802.3 (Novell) */
   76 #define ETHER_FT_8022           2       /* 802.2 */
   77 #define ETHER_FT_SNAP           3       /* SNAP */
   78 #define EF_NFT                  4       /* total number of frame types */
   79 
   80 #ifdef EF_DEBUG
   81 #define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
   82 #else
   83 #define EFDEBUG(format, args...)
   84 #endif
   85 
   86 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
   87 
   88 struct efnet {
   89         struct ifnet    *ef_ifp;
   90         struct ifnet    *ef_pifp;
   91         int             ef_frametype;
   92 };
   93 
   94 struct ef_link {
   95         SLIST_ENTRY(ef_link) el_next;
   96         struct ifnet    *el_ifp;                /* raw device for this clones */
   97         struct efnet    *el_units[EF_NFT];      /* our clones */
   98 };
   99 
  100 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
  101 static int efcount;
  102 
  103 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
  104 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
  105                 struct sockaddr *dst, short *tp, int *hlen);
  106 
  107 /*
  108 static void ef_reset (struct ifnet *);
  109 */
  110 static int ef_attach(struct efnet *sc);
  111 static int ef_detach(struct efnet *sc);
  112 static void ef_init(void *);
  113 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
  114 static void ef_start(struct ifnet *);
  115 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
  116 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
  117                 struct sockaddr *dst, short *tp, int *hlen);
  118 
  119 static int ef_load(void);
  120 static int ef_unload(void);
  121 
  122 /*
  123  * Install the interface, most of structure initialization done in ef_clone()
  124  */
  125 static int
  126 ef_attach(struct efnet *sc)
  127 {
  128         struct ifnet *ifp = sc->ef_ifp;
  129         struct ifaddr *ifa2;
  130         struct sockaddr_dl *sdl2;
  131 
  132         ifp->if_start = ef_start;
  133         ifp->if_watchdog = NULL;
  134         ifp->if_init = ef_init;
  135         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  136         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
  137         /*
  138          * Attach the interface
  139          */
  140         ifa2 = ifaddr_byindex(sc->ef_pifp->if_index);
  141         sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
  142         ether_ifattach(ifp, LLADDR(sdl2));
  143 
  144         ifp->if_resolvemulti = 0;
  145         ifp->if_type = IFT_XETHER;
  146         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  147 
  148         EFDEBUG("%s: attached\n", ifp->if_xname);
  149         return 1;
  150 }
  151 
  152 /*
  153  * This is for _testing_only_, just removes interface from interfaces list
  154  */
  155 static int
  156 ef_detach(struct efnet *sc)
  157 {
  158         struct ifnet *ifp = sc->ef_ifp;
  159         int s;
  160 
  161         s = splimp();
  162 
  163         ether_ifdetach(ifp);
  164         if_free(ifp);
  165 
  166         splx(s);
  167         return 0;
  168 }
  169 
  170 static void
  171 ef_init(void *foo) {
  172         return;
  173 }
  174 
  175 static int
  176 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  177 {
  178         struct efnet *sc = ifp->if_softc;
  179         struct ifaddr *ifa = (struct ifaddr*)data;
  180         int s, error;
  181 
  182         EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
  183         error = 0;
  184         s = splimp();
  185         switch (cmd) {
  186             case SIOCSIFFLAGS:
  187                 error = 0;
  188                 break;
  189             case SIOCSIFADDR:
  190                 if (sc->ef_frametype == ETHER_FT_8023 && 
  191                     ifa->ifa_addr->sa_family != AF_IPX) {
  192                         error = EAFNOSUPPORT;
  193                         break;
  194                 }
  195                 ifp->if_flags |= IFF_UP; 
  196                 /* FALL THROUGH */
  197             default:
  198                 error = ether_ioctl(ifp, cmd, data);
  199                 break;
  200         }
  201         splx(s);
  202         return error;
  203 }
  204 
  205 /*
  206  * Currently packet prepared in the ether_output(), but this can be a better
  207  * place.
  208  */
  209 static void
  210 ef_start(struct ifnet *ifp)
  211 {
  212         struct efnet *sc = (struct efnet*)ifp->if_softc;
  213         struct ifnet *p;
  214         struct mbuf *m;
  215         int error;
  216 
  217         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  218         p = sc->ef_pifp;
  219 
  220         EFDEBUG("\n");
  221         for (;;) {
  222                 IF_DEQUEUE(&ifp->if_snd, m);
  223                 if (m == 0)
  224                         break;
  225                 BPF_MTAP(ifp, m);
  226                 IFQ_HANDOFF(p, m, error);
  227                 if (error) {
  228                         ifp->if_oerrors++;
  229                         continue;
  230                 }
  231                 ifp->if_opackets++;
  232         }
  233         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  234         return;
  235 }
  236 
  237 /*
  238  * Inline functions do not put additional overhead to procedure call or
  239  * parameter passing but simplify the code
  240  */
  241 static int __inline
  242 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
  243 {
  244         int isr;
  245 
  246         switch(ether_type) {
  247 #ifdef IPX
  248         case ETHERTYPE_IPX:
  249                 isr = NETISR_IPX;
  250                 break;
  251 #endif
  252 #ifdef INET
  253         case ETHERTYPE_IP:
  254                 if (ip_fastforward(m))
  255                         return (0);
  256                 isr = NETISR_IP;
  257                 break;
  258 
  259         case ETHERTYPE_ARP:
  260                 isr = NETISR_ARP;
  261                 break;
  262 #endif
  263         default:
  264                 return (EPROTONOSUPPORT);
  265         }
  266         netisr_dispatch(isr, m);
  267         return (0);
  268 }
  269 
  270 static int __inline
  271 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
  272         u_short ether_type)
  273 {
  274         int isr;
  275 
  276         switch(ether_type) {
  277 #ifdef IPX
  278         case ETHERTYPE_IPX:
  279                 m_adj(m, 8);
  280                 isr = NETISR_IPX;
  281                 break;
  282 #endif
  283         default:
  284                 return (EPROTONOSUPPORT);
  285         }
  286         netisr_dispatch(isr, m);
  287         return (0);
  288 }
  289 
  290 static int __inline
  291 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
  292         u_short ether_type)
  293 {
  294         int isr;
  295 
  296         switch(ether_type) {
  297 #ifdef IPX
  298         case 0xe0:
  299                 m_adj(m, 3);
  300                 isr = NETISR_IPX;
  301                 break;
  302 #endif
  303         default:
  304                 return (EPROTONOSUPPORT);
  305         }
  306         netisr_dispatch(isr, m);
  307         return (0);
  308 }
  309 
  310 /*
  311  * Called from ether_input()
  312  */
  313 static int
  314 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
  315 {
  316         u_short ether_type;
  317         int ft = -1;
  318         struct efnet *efp;
  319         struct ifnet *eifp;
  320         struct llc *l;
  321         struct ef_link *efl;
  322         int isr;
  323 
  324         ether_type = ntohs(eh->ether_type);
  325         l = NULL;
  326         if (ether_type < ETHERMTU) {
  327                 l = mtod(m, struct llc*);
  328                 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
  329                         /* 
  330                          * Novell's "802.3" frame
  331                          */
  332                         ft = ETHER_FT_8023;
  333                 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
  334                         /*
  335                          * 802.2/SNAP
  336                          */
  337                         ft = ETHER_FT_SNAP;
  338                         ether_type = ntohs(l->llc_un.type_snap.ether_type);
  339                 } else if (l->llc_dsap == l->llc_ssap) {
  340                         /*
  341                          * 802.3/802.2
  342                          */
  343                         ft = ETHER_FT_8022;
  344                         ether_type = l->llc_ssap;
  345                 }
  346         } else
  347                 ft = ETHER_FT_EII;
  348 
  349         if (ft == -1) {
  350                 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
  351                 return EPROTONOSUPPORT;
  352         }
  353 
  354         /*
  355          * Check if interface configured for the given frame
  356          */
  357         efp = NULL;
  358         SLIST_FOREACH(efl, &efdev, el_next) {
  359                 if (efl->el_ifp == ifp) {
  360                         efp = efl->el_units[ft];
  361                         break;
  362                 }
  363         }
  364         if (efp == NULL) {
  365                 EFDEBUG("Can't find if for %d\n", ft);
  366                 return EPROTONOSUPPORT;
  367         }
  368         eifp = efp->ef_ifp;
  369         if ((eifp->if_flags & IFF_UP) == 0)
  370                 return EPROTONOSUPPORT;
  371         eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
  372         m->m_pkthdr.rcvif = eifp;
  373 
  374         BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
  375         /*
  376          * Now we ready to adjust mbufs and pass them to protocol intr's
  377          */
  378         switch(ft) {
  379         case ETHER_FT_EII:
  380                 return (ef_inputEII(m, eh, ether_type));
  381 #ifdef IPX
  382         case ETHER_FT_8023:             /* only IPX can be here */
  383                 isr = NETISR_IPX;
  384                 break;
  385 #endif
  386         case ETHER_FT_SNAP:
  387                 return (ef_inputSNAP(m, eh, l, ether_type));
  388         case ETHER_FT_8022:
  389                 return (ef_input8022(m, eh, l, ether_type));
  390         default:
  391                 EFDEBUG("No support for frame %d and proto %04x\n",
  392                         ft, ether_type);
  393                 return (EPROTONOSUPPORT);
  394         }
  395         netisr_dispatch(isr, m);
  396         return (0);
  397 }
  398 
  399 static int
  400 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
  401         int *hlen)
  402 {
  403         struct efnet *sc = (struct efnet*)ifp->if_softc;
  404         struct mbuf *m = *mp;
  405         u_char *cp;
  406         short type;
  407 
  408         if (ifp->if_type != IFT_XETHER)
  409                 return ENETDOWN;
  410         switch (sc->ef_frametype) {
  411             case ETHER_FT_EII:
  412 #ifdef IPX
  413                 type = htons(ETHERTYPE_IPX);
  414 #else
  415                 return EPFNOSUPPORT;
  416 #endif
  417                 break;
  418             case ETHER_FT_8023:
  419                 type = htons(m->m_pkthdr.len);
  420                 break;
  421             case ETHER_FT_8022:
  422                 M_PREPEND(m, ETHER_HDR_LEN + 3, M_TRYWAIT);
  423                 if (m == NULL) {
  424                         *mp = NULL;
  425                         return ENOBUFS;
  426                 }
  427                 /*
  428                  * Ensure that ethernet header and next three bytes
  429                  * will fit into single mbuf
  430                  */
  431                 m = m_pullup(m, ETHER_HDR_LEN + 3);
  432                 if (m == NULL) {
  433                         *mp = NULL;
  434                         return ENOBUFS;
  435                 }
  436                 m_adj(m, ETHER_HDR_LEN);
  437                 type = htons(m->m_pkthdr.len);
  438                 cp = mtod(m, u_char *);
  439                 *cp++ = 0xE0;
  440                 *cp++ = 0xE0;
  441                 *cp++ = 0x03;
  442                 *hlen += 3;
  443                 break;
  444             case ETHER_FT_SNAP:
  445                 M_PREPEND(m, 8, M_TRYWAIT);
  446                 if (m == NULL) {
  447                         *mp = NULL;
  448                         return ENOBUFS;
  449                 }
  450                 type = htons(m->m_pkthdr.len);
  451                 cp = mtod(m, u_char *);
  452                 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
  453                 *hlen += 8;
  454                 break;
  455             default:
  456                 return EPFNOSUPPORT;
  457         }
  458         *mp = m;
  459         *tp = type;
  460         return 0;
  461 }
  462 
  463 /*
  464  * Create clone from the given interface
  465  */
  466 static int
  467 ef_clone(struct ef_link *efl, int ft)
  468 {
  469         struct efnet *efp;
  470         struct ifnet *eifp;
  471         struct ifnet *ifp = efl->el_ifp;
  472 
  473         efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
  474             M_WAITOK | M_ZERO);
  475         if (efp == NULL)
  476                 return ENOMEM;
  477         efp->ef_pifp = ifp;
  478         efp->ef_frametype = ft;
  479         eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
  480         if (eifp == NULL) {
  481                 free(efp, M_IFADDR);
  482                 return (ENOSPC);
  483         }
  484         snprintf(eifp->if_xname, IFNAMSIZ,
  485             "%sf%d", ifp->if_xname, efp->ef_frametype);
  486         eifp->if_dname = "ef";
  487         eifp->if_dunit = IF_DUNIT_NONE;
  488         eifp->if_softc = efp;
  489         if (ifp->if_ioctl)
  490                 eifp->if_ioctl = ef_ioctl;
  491         efl->el_units[ft] = efp;
  492         return 0;
  493 }
  494 
  495 static int
  496 ef_load(void)
  497 {
  498         struct ifnet *ifp;
  499         struct efnet *efp;
  500         struct ef_link *efl = NULL, *efl_temp;
  501         int error = 0, d;
  502 
  503         IFNET_RLOCK();
  504         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  505                 if (ifp->if_type != IFT_ETHER) continue;
  506                 EFDEBUG("Found interface %s\n", ifp->if_xname);
  507                 efl = (struct ef_link*)malloc(sizeof(struct ef_link), 
  508                     M_IFADDR, M_WAITOK | M_ZERO);
  509                 if (efl == NULL) {
  510                         error = ENOMEM;
  511                         break;
  512                 }
  513 
  514                 efl->el_ifp = ifp;
  515 #ifdef ETHER_II
  516                 error = ef_clone(efl, ETHER_FT_EII);
  517                 if (error) break;
  518 #endif
  519 #ifdef ETHER_8023
  520                 error = ef_clone(efl, ETHER_FT_8023);
  521                 if (error) break;
  522 #endif
  523 #ifdef ETHER_8022
  524                 error = ef_clone(efl, ETHER_FT_8022);
  525                 if (error) break;
  526 #endif
  527 #ifdef ETHER_SNAP
  528                 error = ef_clone(efl, ETHER_FT_SNAP);
  529                 if (error) break;
  530 #endif
  531                 efcount++;
  532                 SLIST_INSERT_HEAD(&efdev, efl, el_next);
  533         }
  534         IFNET_RUNLOCK();
  535         if (error) {
  536                 if (efl)
  537                         SLIST_INSERT_HEAD(&efdev, efl, el_next);
  538                 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
  539                         for (d = 0; d < EF_NFT; d++)
  540                                 if (efl->el_units[d]) {
  541                                         if (efl->el_units[d]->ef_pifp != NULL)
  542                                                 if_free(efl->el_units[d]->ef_pifp);
  543                                         free(efl->el_units[d], M_IFADDR);
  544                                 }
  545                         free(efl, M_IFADDR);
  546                 }
  547                 return error;
  548         }
  549         SLIST_FOREACH(efl, &efdev, el_next) {
  550                 for (d = 0; d < EF_NFT; d++) {
  551                         efp = efl->el_units[d];
  552                         if (efp)
  553                                 ef_attach(efp);
  554                 }
  555         }
  556         ef_inputp = ef_input;
  557         ef_outputp = ef_output;
  558         EFDEBUG("Loaded\n");
  559         return 0;
  560 }
  561 
  562 static int
  563 ef_unload(void)
  564 {
  565         struct efnet *efp;
  566         struct ef_link *efl;
  567         int d;
  568 
  569         ef_inputp = NULL;
  570         ef_outputp = NULL;
  571         SLIST_FOREACH(efl, &efdev, el_next) {
  572                 for (d = 0; d < EF_NFT; d++) {
  573                         efp = efl->el_units[d];
  574                         if (efp) {
  575                                 ef_detach(efp);
  576                         }
  577                 }
  578         }
  579         EFDEBUG("Unloaded\n");
  580         return 0;
  581 }
  582 
  583 static int 
  584 if_ef_modevent(module_t mod, int type, void *data)
  585 {
  586         switch ((modeventtype_t)type) {
  587             case MOD_LOAD:
  588                 return ef_load();
  589             case MOD_UNLOAD:
  590                 return ef_unload();
  591             default:
  592                 return EOPNOTSUPP;
  593         }
  594         return 0;
  595 }
  596 
  597 static moduledata_t if_ef_mod = {
  598         "if_ef", if_ef_modevent, NULL
  599 };
  600 
  601 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);

Cache object: 76f027c6b30149f974e543d56f929375


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