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/bsd/net/ether_inet_pr_module.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) 2000 Apple Computer, Inc. All rights reserved.
    3  *
    4  * @APPLE_LICENSE_HEADER_START@
    5  * 
    6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
    7  * 
    8  * This file contains Original Code and/or Modifications of Original Code
    9  * as defined in and that are subject to the Apple Public Source License
   10  * Version 2.0 (the 'License'). You may not use this file except in
   11  * compliance with the License. Please obtain a copy of the License at
   12  * http://www.opensource.apple.com/apsl/ and read it before using this
   13  * file.
   14  * 
   15  * The Original Code and all software distributed under the License are
   16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   20  * Please see the License for the specific language governing rights and
   21  * limitations under the License.
   22  * 
   23  * @APPLE_LICENSE_HEADER_END@
   24  */
   25 /*
   26  * Copyright (c) 1982, 1989, 1993
   27  *      The Regents of the University of California.  All rights reserved.
   28  *
   29  * Redistribution and use in source and binary forms, with or without
   30  * modification, are permitted provided that the following conditions
   31  * are met:
   32  * 1. Redistributions of source code must retain the above copyright
   33  *    notice, this list of conditions and the following disclaimer.
   34  * 2. Redistributions in binary form must reproduce the above copyright
   35  *    notice, this list of conditions and the following disclaimer in the
   36  *    documentation and/or other materials provided with the distribution.
   37  * 3. All advertising materials mentioning features or use of this software
   38  *    must display the following acknowledgement:
   39  *      This product includes software developed by the University of
   40  *      California, Berkeley and its contributors.
   41  * 4. Neither the name of the University nor the names of its contributors
   42  *    may be used to endorse or promote products derived from this software
   43  *    without specific prior written permission.
   44  *
   45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   55  * SUCH DAMAGE.
   56  *
   57  */
   58 
   59 
   60 
   61 #include <sys/param.h>
   62 #include <sys/systm.h>
   63 #include <sys/kernel.h>
   64 #include <sys/malloc.h>
   65 #include <sys/mbuf.h>
   66 #include <sys/socket.h>
   67 #include <sys/sockio.h>
   68 #include <sys/sysctl.h>
   69 
   70 #include <net/if.h>
   71 #include <net/netisr.h>
   72 #include <net/route.h>
   73 #include <net/if_llc.h>
   74 #include <net/if_dl.h>
   75 #include <net/if_types.h>
   76 
   77 #include <netinet/in.h>
   78 #include <netinet/in_var.h>
   79 #include <netinet/if_ether.h>
   80 #include <netinet/in_systm.h>
   81 #include <netinet/ip.h>
   82 
   83 #include <sys/socketvar.h>
   84 
   85 #include <net/dlil.h>
   86 
   87 #if LLC && CCITT
   88 extern struct ifqueue pkintrq;
   89 #endif
   90 
   91 
   92 #if BRIDGE
   93 #include <net/bridge.h>
   94 #endif
   95 
   96 /* #include "vlan.h" */
   97 #if NVLAN > 0
   98 #include <net/if_vlan_var.h>
   99 #endif /* NVLAN > 0 */
  100 
  101 static u_long lo_dlt = 0;
  102 static ivedonethis = 0;
  103 static u_char   etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  104 
  105 #define IFP2AC(IFP) ((struct arpcom *)IFP)
  106 
  107 
  108 
  109 
  110 /*
  111  * Process a received Ethernet packet;
  112  * the packet is in the mbuf chain m without
  113  * the ether header, which is provided separately.
  114  */
  115 int
  116 inet_ether_input(m, frame_header, ifp, dl_tag, sync_ok)
  117     struct mbuf  *m;
  118     char         *frame_header;
  119     struct ifnet *ifp;
  120     u_long           dl_tag;
  121     int          sync_ok;
  122 
  123 {
  124     register struct ether_header *eh = (struct ether_header *) frame_header;
  125     register struct ifqueue *inq=0;
  126     u_short ether_type;
  127     int s;
  128     u_int16_t ptype = -1;
  129     unsigned char buf[18];
  130 
  131 #if ISO || LLC || NETAT
  132     register struct llc *l;
  133 #endif
  134 
  135     if ((ifp->if_flags & IFF_UP) == 0) {
  136          m_freem(m);
  137          return EJUSTRETURN;
  138     }
  139 
  140     ifp->if_lastchange = time;
  141 
  142     if (eh->ether_dhost[0] & 1) {
  143         if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
  144                  sizeof(etherbroadcastaddr)) == 0)
  145             m->m_flags |= M_BCAST;
  146         else
  147             m->m_flags |= M_MCAST;
  148     }
  149     if (m->m_flags & (M_BCAST|M_MCAST))
  150         ifp->if_imcasts++;
  151 
  152     ether_type = ntohs(eh->ether_type);
  153 
  154 #if NVLAN > 0
  155         if (ether_type == vlan_proto) {
  156                 if (vlan_input(eh, m) < 0)
  157                         ifp->if_data.ifi_noproto++;
  158                 return EJUSTRETURN;
  159         }
  160 #endif /* NVLAN > 0 */
  161 
  162     switch (ether_type) {
  163 
  164     case ETHERTYPE_IP:
  165         if (ipflow_fastforward(m))
  166             return EJUSTRETURN;
  167         ptype = mtod(m, struct ip *)->ip_p;
  168         if ((sync_ok == 0) || 
  169             (ptype != IPPROTO_TCP && ptype != IPPROTO_UDP)) {
  170             schednetisr(NETISR_IP); 
  171         }
  172 
  173         inq = &ipintrq;
  174         break;
  175 
  176     case ETHERTYPE_ARP:
  177         schednetisr(NETISR_ARP);
  178         inq = &arpintrq;
  179         break;
  180 
  181     default: {
  182         return ENOENT;
  183         }
  184     }
  185 
  186     if (inq == 0)
  187         return ENOENT;
  188 
  189         s = splimp();
  190         if (IF_QFULL(inq)) {
  191                 IF_DROP(inq);
  192                 m_freem(m);
  193                 splx(s);
  194                 return EJUSTRETURN;
  195         } else
  196                 IF_ENQUEUE(inq, m);
  197         splx(s);
  198 
  199     if ((sync_ok) && 
  200         (ptype == IPPROTO_TCP || ptype == IPPROTO_UDP)) {
  201         extern void ipintr(void);
  202 
  203         s = splnet();
  204         ipintr();
  205         splx(s);
  206     }
  207 
  208     return 0;
  209 }
  210 
  211 
  212 
  213 
  214 int
  215 inet_ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag )
  216     struct ifnet    *ifp;
  217     struct mbuf     **m0;
  218     struct sockaddr *dst_netaddr;
  219     caddr_t         route;
  220     char            *type;
  221     char            *edst;
  222     u_long          dl_tag;
  223 {
  224     struct rtentry  *rt0 = (struct rtentry *) route;
  225     int s;
  226     register struct mbuf *m = *m0;
  227     register struct rtentry *rt;
  228     register struct ether_header *eh;
  229     int off, len = m->m_pkthdr.len;
  230     int hlen;   /* link layer header lenght */
  231     struct arpcom *ac = IFP2AC(ifp);
  232 
  233 
  234 
  235     if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 
  236         return ENETDOWN;
  237 
  238     rt = rt0;
  239     if (rt) {
  240         if ((rt->rt_flags & RTF_UP) == 0) {
  241             rt0 = rt = rtalloc1(dst_netaddr, 1, 0UL);
  242             if (rt0)
  243                 rtunref(rt);
  244             else
  245                 return EHOSTUNREACH;
  246         }
  247 
  248         if (rt->rt_flags & RTF_GATEWAY) {
  249             if (rt->rt_gwroute == 0)
  250                 goto lookup;
  251             if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
  252                 rtfree(rt); rt = rt0;
  253             lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
  254                                               0UL);
  255                 if ((rt = rt->rt_gwroute) == 0)
  256                     return (EHOSTUNREACH);
  257             }
  258         }
  259 
  260         
  261         if (rt->rt_flags & RTF_REJECT)
  262             if (rt->rt_rmx.rmx_expire == 0 ||
  263                 time_second < rt->rt_rmx.rmx_expire)
  264                 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
  265     }
  266 
  267     hlen = ETHER_HDR_LEN;
  268 
  269     /*
  270      * Tell ether_frameout it's ok to loop packet unless negated below.
  271      */
  272     m->m_flags |= M_LOOP;
  273 
  274     switch (dst_netaddr->sa_family) {
  275 
  276     case AF_INET:
  277         if (!arpresolve(ac, rt, m, dst_netaddr, edst, rt0))
  278             return (EJUSTRETURN);       /* if not yet resolved */
  279         off = m->m_pkthdr.len - m->m_len;
  280         *(u_short *)type = htons(ETHERTYPE_IP);
  281         break;
  282 
  283     case AF_UNSPEC:
  284         m->m_flags &= ~M_LOOP;
  285         eh = (struct ether_header *)dst_netaddr->sa_data;
  286         (void)memcpy(edst, eh->ether_dhost, 6);
  287         *(u_short *)type = eh->ether_type;
  288         break;
  289 
  290     default:
  291         kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
  292                dst_netaddr->sa_family);
  293 
  294         return EAFNOSUPPORT;
  295     }
  296 
  297     return (0);
  298 }
  299 
  300 
  301 int
  302 ether_inet_prmod_ioctl(dl_tag, ifp, command, data)
  303     u_long       dl_tag;
  304     struct ifnet *ifp;
  305     int          command;
  306     caddr_t      data;
  307 {
  308     struct ifaddr *ifa = (struct ifaddr *) data;
  309     struct ifreq *ifr = (struct ifreq *) data;
  310     struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data;
  311     int error = 0;
  312     boolean_t funnel_state;
  313     struct arpcom *ac = (struct arpcom *) ifp;
  314     struct sockaddr_dl *sdl;
  315     struct sockaddr_in *sin;
  316     u_char *e_addr;
  317 
  318 
  319 #if 0
  320         /* No tneeded at soo_ioctlis already funnelled */
  321     funnel_state = thread_funnel_set(network_flock,TRUE);
  322 #endif
  323 
  324     switch (command) {
  325     case SIOCRSLVMULTI: {
  326         switch(rsreq->sa->sa_family) {
  327 
  328         case AF_INET:
  329                 sin = (struct sockaddr_in *)rsreq->sa;
  330                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
  331                         return EADDRNOTAVAIL;
  332                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
  333                        M_WAITOK);
  334                 sdl->sdl_len = sizeof *sdl;
  335                 sdl->sdl_family = AF_LINK;
  336                 sdl->sdl_index = ifp->if_index;
  337                 sdl->sdl_type = IFT_ETHER;
  338                 sdl->sdl_nlen = 0;
  339                 sdl->sdl_alen = ETHER_ADDR_LEN;
  340                 sdl->sdl_slen = 0;
  341                 e_addr = LLADDR(sdl);
  342                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
  343                 *rsreq->llsa = (struct sockaddr *)sdl;
  344                 return EJUSTRETURN;
  345 
  346         default:
  347                 /* 
  348                  * Well, the text isn't quite right, but it's the name
  349                  * that counts...
  350                  */
  351                 return EAFNOSUPPORT;
  352         }
  353 
  354     }
  355     case SIOCSIFADDR:
  356          if ((ifp->if_flags & IFF_RUNNING) == 0) {
  357               ifp->if_flags |= IFF_UP;
  358               dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
  359          }
  360 
  361          switch (ifa->ifa_addr->sa_family) {
  362 
  363          case AF_INET:
  364 
  365             if (ifp->if_init)
  366                 ifp->if_init(ifp->if_softc);    /* before arpwhohas */
  367 
  368             arp_ifinit(IFP2AC(ifp), ifa);
  369 
  370             /*
  371              * Register new IP and MAC addresses with the kernel debugger
  372              * for the en0 interface.
  373              */
  374             if (ifp->if_unit == 0)
  375                 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr), &(IFP2AC(ifp)->ac_enaddr));
  376 
  377             break;
  378 
  379         default:
  380             break;
  381         }
  382 
  383         break;
  384 
  385     case SIOCGIFADDR:
  386     {
  387         struct sockaddr *sa;
  388 
  389         sa = (struct sockaddr *) & ifr->ifr_data;
  390         bcopy(IFP2AC(ifp)->ac_enaddr,
  391               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
  392     }
  393     break;
  394 
  395     case SIOCSIFMTU:
  396         /*
  397          * IOKit IONetworkFamily will set the right MTU according to the driver
  398          */
  399 
  400          return (0);
  401 
  402     default:
  403          return EOPNOTSUPP;
  404     }
  405 
  406     //(void) thread_funnel_set(network_flock, FALSE);
  407 
  408     return (error);
  409 }
  410 
  411 
  412 
  413 
  414 
  415 int
  416 ether_attach_inet(struct ifnet *ifp, u_long *dl_tag)
  417 {
  418     struct dlil_proto_reg_str   reg;
  419     struct dlil_demux_desc      desc;
  420     struct dlil_demux_desc      desc2;
  421     u_short en_native=ETHERTYPE_IP;
  422     u_short arp_native=ETHERTYPE_ARP;
  423     int   stat;
  424     int i;
  425 
  426 
  427     stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, dl_tag);
  428     if (stat == 0)
  429          return (stat);
  430 
  431     TAILQ_INIT(&reg.demux_desc_head);
  432     desc.type = DLIL_DESC_RAW;
  433     desc.variants.bitmask.proto_id_length = 0;
  434     desc.variants.bitmask.proto_id = 0;
  435     desc.variants.bitmask.proto_id_mask = 0;
  436     desc.native_type = (char *) &en_native;
  437     TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
  438     reg.interface_family = ifp->if_family;
  439     reg.unit_number      = ifp->if_unit;
  440     reg.input            = inet_ether_input;
  441     reg.pre_output       = inet_ether_pre_output;
  442     reg.event            = 0;
  443     reg.offer            = 0;
  444     reg.ioctl            = ether_inet_prmod_ioctl;
  445     reg.default_proto    = 1;
  446     reg.protocol_family  = PF_INET;
  447 
  448     desc2 = desc;
  449     desc2.native_type = (char *) &arp_native;
  450     TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
  451 
  452     stat = dlil_attach_protocol(&reg, dl_tag);
  453     if (stat) {
  454         printf("WARNING: ether_attach_inet can't attach ip to interface\n");
  455         return stat;
  456     }
  457     return (0);
  458 }
  459 
  460 int  ether_detach_inet(struct ifnet *ifp, u_long dl_tag)
  461 {
  462     int         stat;
  463 
  464     stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, &dl_tag);
  465     if (stat == 0) {
  466         stat = dlil_detach_protocol(dl_tag);
  467         if (stat) {
  468             printf("WARNING: ether_detach_inet can't detach ip from interface\n");
  469         }
  470     }
  471     return stat;
  472 }
  473 

Cache object: a0c5ba2b8a11cdd56b19b72035b6add0


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