2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
33 #include "opt_atalk.h"
35 #include "opt_inet6.h"
37 #include "opt_netgraph.h"
38 #include "opt_mbuf_profiling.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
47 #include <sys/random.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
61 #include <net/ethernet.h>
62 #include <net/if_bridgevar.h>
63 #include <net/if_vlan_var.h>
64 #include <net/if_llatbl.h>
68 #include <netpfil/pf/pf_mtag.h>
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/ip_carp.h>
75 #include <netinet/ip_var.h>
78 #include <netinet6/nd6.h>
82 #include <netipx/ipx.h>
83 #include <netipx/ipx_if.h>
86 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
87 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
88 const struct sockaddr *dst, short *tp, int *hlen);
91 #include <netatalk/at.h>
92 #include <netatalk/at_var.h>
93 #include <netatalk/at_extern.h>
95 #define llc_snap_org_code llc_un.type_snap.org_code
96 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 extern u_char at_org_code[3];
99 extern u_char aarp_org_code[3];
100 #endif /* NETATALK */
102 #include <security/mac/mac_framework.h>
105 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
106 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
109 VNET_DEFINE(struct pfil_head, link_pfil_hook); /* Packet filter hooks */
111 /* netgraph node hooks for ng_ether(4) */
112 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
113 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
114 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
115 void (*ng_ether_attach_p)(struct ifnet *ifp);
116 void (*ng_ether_detach_p)(struct ifnet *ifp);
118 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
120 /* if_bridge(4) support */
121 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
122 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
123 struct sockaddr *, struct rtentry *);
124 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
126 /* if_lagg(4) support */
127 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
129 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
130 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
132 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
135 static void ether_reassign(struct ifnet *, struct vnet *, char *);
138 /* XXX: should be in an arp support file, not here */
139 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
141 #define ETHER_IS_BROADCAST(addr) \
142 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
144 #define senderr(e) do { error = (e); goto bad;} while (0)
147 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
151 if (src->m_pkthdr.csum_flags & CSUM_IP)
152 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
153 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
154 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
155 if (src->m_pkthdr.csum_flags & CSUM_SCTP)
156 csum_flags |= CSUM_SCTP_VALID;
157 dst->m_pkthdr.csum_flags |= csum_flags;
158 if (csum_flags & CSUM_DATA_VALID)
159 dst->m_pkthdr.csum_data = 0xffff;
163 * Ethernet output routine.
164 * Encapsulate a packet of type family for the local net.
165 * Use trailer local net encapsulation if enough data in first
166 * packet leaves a multiple of 512 bytes of data in remainder.
169 ether_output(struct ifnet *ifp, struct mbuf *m,
170 const struct sockaddr *dst, struct route *ro)
173 int error = 0, hdrcmplt = 0;
174 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
175 struct llentry *lle = NULL;
176 struct rtentry *rt0 = NULL;
177 struct ether_header *eh;
180 int hlen; /* link layer header length */
183 if (!(m->m_flags & (M_BCAST | M_MCAST)))
188 error = mac_ifnet_check_transmit(ifp, m);
194 if (ifp->if_flags & IFF_MONITOR)
196 if (!((ifp->if_flags & IFF_UP) &&
197 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
200 hlen = ETHER_HDR_LEN;
201 switch (dst->sa_family) {
204 if (lle != NULL && (lle->la_flags & LLE_VALID))
205 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
207 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
209 return (error == EWOULDBLOCK ? 0 : error);
210 type = htons(ETHERTYPE_IP);
215 ah = mtod(m, struct arphdr *);
216 ah->ar_hrd = htons(ARPHRD_ETHER);
218 loop_copy = 0; /* if this is for us, don't do it */
220 switch(ntohs(ah->ar_op)) {
221 case ARPOP_REVREQUEST:
223 type = htons(ETHERTYPE_REVARP);
228 type = htons(ETHERTYPE_ARP);
232 if (m->m_flags & M_BCAST)
233 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
235 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
242 if (lle != NULL && (lle->la_flags & LLE_VALID))
243 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
245 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
248 type = htons(ETHERTYPE_IPV6);
254 error = ef_outputp(ifp, &m, dst, &type, &hlen);
258 type = htons(ETHERTYPE_IPX);
259 bcopy(&((const struct sockaddr_ipx *)dst)->sipx_addr.x_host,
260 edst, sizeof (edst));
266 struct at_ifaddr *aa;
268 if ((aa = at_ifawithnet((const struct sockaddr_at *)dst)) == NULL)
269 senderr(EHOSTUNREACH); /* XXX */
270 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
271 ifa_free(&aa->aa_ifa);
275 * In the phase 2 case, need to prepend an mbuf for the llc header.
277 if ( aa->aa_flags & AFA_PHASE2 ) {
280 ifa_free(&aa->aa_ifa);
281 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
284 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
285 llc.llc_control = LLC_UI;
286 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
287 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
288 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
289 type = htons(m->m_pkthdr.len);
290 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
292 ifa_free(&aa->aa_ifa);
293 type = htons(ETHERTYPE_AT);
297 #endif /* NETATALK */
299 case pseudo_AF_HDRCMPLT:
301 const struct ether_header *eh;
304 eh = (const struct ether_header *)dst->sa_data;
305 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
309 loop_copy = 0; /* if this is for us, don't do it */
310 eh = (const struct ether_header *)dst->sa_data;
311 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
312 type = eh->ether_type;
316 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
317 senderr(EAFNOSUPPORT);
320 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
321 update_mbuf_csumflags(m, m);
322 return (if_simloop(ifp, m, dst->sa_family, 0));
326 * Add local net header. If no space in first mbuf,
329 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
332 eh = mtod(m, struct ether_header *);
333 (void)memcpy(&eh->ether_type, &type,
334 sizeof(eh->ether_type));
335 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
337 (void)memcpy(eh->ether_shost, esrc,
338 sizeof(eh->ether_shost));
340 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
341 sizeof(eh->ether_shost));
344 * If a simplex interface, and the packet is being sent to our
345 * Ethernet address or a broadcast address, loopback a copy.
346 * XXX To make a simplex device behave exactly like a duplex
347 * device, we should copy in the case of sending to our own
348 * ethernet address (thus letting the original actually appear
349 * on the wire). However, we don't do that here for security
350 * reasons and compatibility with the original behavior.
352 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
353 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
354 if (m->m_flags & M_BCAST) {
358 * Because if_simloop() modifies the packet, we need a
359 * writable copy through m_dup() instead of a readonly
360 * one as m_copy[m] would give us. The alternative would
361 * be to modify if_simloop() to handle the readonly mbuf,
362 * but performancewise it is mostly equivalent (trading
363 * extra data copying vs. extra locking).
365 * XXX This is a local workaround. A number of less
366 * often used kernel parts suffer from the same bug.
367 * See PR kern/105943 for a proposed general solution.
369 if ((n = m_dup(m, M_NOWAIT)) != NULL) {
370 update_mbuf_csumflags(m, n);
371 (void)if_simloop(ifp, n, dst->sa_family, hlen);
374 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
375 ETHER_ADDR_LEN) == 0) {
376 update_mbuf_csumflags(m, m);
377 (void) if_simloop(ifp, m, dst->sa_family, hlen);
378 return (0); /* XXX */
383 * Bridges require special output handling.
385 if (ifp->if_bridge) {
386 BRIDGE_OUTPUT(ifp, m, error);
390 #if defined(INET) || defined(INET6)
392 (error = (*carp_output_p)(ifp, m, dst)))
396 /* Handle ng_ether(4) processing, if any */
397 if (IFP2AC(ifp)->ac_netgraph != NULL) {
398 KASSERT(ng_ether_output_p != NULL,
399 ("ng_ether_output_p is NULL"));
400 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
409 /* Continue with link-layer output */
410 return ether_output_frame(ifp, m);
414 * Ethernet link layer output routine to send a raw frame to the device.
416 * This assumes that the 14 byte Ethernet header is present and contiguous
417 * in the first mbuf (if BRIDGE'ing).
420 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
424 if (PFIL_HOOKED(&V_link_pfil_hook)) {
425 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
435 * Queue message on interface, update output statistics if
436 * successful, and start output if interface not yet active.
438 return ((ifp->if_transmit)(ifp, m));
441 #if defined(INET) || defined(INET6)
445 * Process a received Ethernet packet; the packet is in the
446 * mbuf chain m with the ethernet header at the front.
449 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
451 struct ether_header *eh;
454 if ((ifp->if_flags & IFF_UP) == 0) {
459 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
460 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
466 * Do consistency checks to verify assumptions
467 * made by code past this point.
469 if ((m->m_flags & M_PKTHDR) == 0) {
470 if_printf(ifp, "discard frame w/o packet header\n");
475 if (m->m_len < ETHER_HDR_LEN) {
476 /* XXX maybe should pullup? */
477 if_printf(ifp, "discard frame w/o leading ethernet "
478 "header (len %u pkt len %u)\n",
479 m->m_len, m->m_pkthdr.len);
484 eh = mtod(m, struct ether_header *);
485 etype = ntohs(eh->ether_type);
486 if (m->m_pkthdr.rcvif == NULL) {
487 if_printf(ifp, "discard frame w/o interface pointer\n");
493 if (m->m_pkthdr.rcvif != ifp) {
494 if_printf(ifp, "Warning, frame marked as received on %s\n",
495 m->m_pkthdr.rcvif->if_xname);
499 CURVNET_SET_QUIET(ifp->if_vnet);
501 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
502 if (ETHER_IS_BROADCAST(eh->ether_dhost))
503 m->m_flags |= M_BCAST;
505 m->m_flags |= M_MCAST;
511 * Tag the mbuf with an appropriate MAC label before any other
512 * consumers can get to it.
514 mac_ifnet_create_mbuf(ifp, m);
518 * Give bpf a chance at the packet.
520 ETHER_BPF_MTAP(ifp, m);
523 * If the CRC is still on the packet, trim it off. We do this once
524 * and once only in case we are re-entered. Nothing else on the
525 * Ethernet receive path expects to see the FCS.
527 if (m->m_flags & M_HASFCS) {
528 m_adj(m, -ETHER_CRC_LEN);
529 m->m_flags &= ~M_HASFCS;
532 if (!(ifp->if_capenable & IFCAP_HWSTATS))
533 ifp->if_ibytes += m->m_pkthdr.len;
535 /* Allow monitor mode to claim this frame, after stats are updated. */
536 if (ifp->if_flags & IFF_MONITOR) {
542 /* Handle input from a lagg(4) port */
543 if (ifp->if_type == IFT_IEEE8023ADLAG) {
544 KASSERT(lagg_input_p != NULL,
545 ("%s: if_lagg not loaded!", __func__));
546 m = (*lagg_input_p)(ifp, m);
548 ifp = m->m_pkthdr.rcvif;
556 * If the hardware did not process an 802.1Q tag, do this now,
557 * to allow 802.1P priority frames to be passed to the main input
559 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
561 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
562 struct ether_vlan_header *evl;
564 if (m->m_len < sizeof(*evl) &&
565 (m = m_pullup(m, sizeof(*evl))) == NULL) {
567 if_printf(ifp, "cannot pullup VLAN header\n");
575 evl = mtod(m, struct ether_vlan_header *);
576 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
577 m->m_flags |= M_VLANTAG;
579 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
580 ETHER_HDR_LEN - ETHER_TYPE_LEN);
581 m_adj(m, ETHER_VLAN_ENCAP_LEN);
582 eh = mtod(m, struct ether_header *);
585 M_SETFIB(m, ifp->if_fib);
587 /* Allow ng_ether(4) to claim this frame. */
588 if (IFP2AC(ifp)->ac_netgraph != NULL) {
589 KASSERT(ng_ether_input_p != NULL,
590 ("%s: ng_ether_input_p is NULL", __func__));
591 m->m_flags &= ~M_PROMISC;
592 (*ng_ether_input_p)(ifp, &m);
597 eh = mtod(m, struct ether_header *);
601 * Allow if_bridge(4) to claim this frame.
602 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
603 * and the frame should be delivered locally.
605 if (ifp->if_bridge != NULL) {
606 m->m_flags &= ~M_PROMISC;
607 BRIDGE_INPUT(ifp, m);
612 eh = mtod(m, struct ether_header *);
615 #if defined(INET) || defined(INET6)
617 * Clear M_PROMISC on frame so that carp(4) will see it when the
618 * mbuf flows up to Layer 3.
619 * FreeBSD's implementation of carp(4) uses the inprotosw
620 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
621 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
622 * is outside the scope of the M_PROMISC test below.
623 * TODO: Maintain a hash table of ethernet addresses other than
624 * ether_dhost which may be active on this ifp.
626 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
627 m->m_flags &= ~M_PROMISC;
632 * If the frame received was not for our MAC address, set the
633 * M_PROMISC flag on the mbuf chain. The frame may need to
634 * be seen by the rest of the Ethernet input path in case of
635 * re-entry (e.g. bridge, vlan, netgraph) but should not be
636 * seen by upper protocol layers.
638 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
639 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
640 m->m_flags |= M_PROMISC;
643 if (harvest.ethernet)
644 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
651 * Ethernet input dispatch; by default, direct dispatch here regardless of
652 * global configuration.
655 ether_nh_input(struct mbuf *m)
658 ether_input_internal(m->m_pkthdr.rcvif, m);
661 static struct netisr_handler ether_nh = {
663 .nh_handler = ether_nh_input,
664 .nh_proto = NETISR_ETHER,
665 .nh_policy = NETISR_POLICY_SOURCE,
666 .nh_dispatch = NETISR_DISPATCH_DIRECT,
670 ether_init(__unused void *arg)
673 netisr_register(ðer_nh);
675 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
678 vnet_ether_init(__unused void *arg)
682 /* Initialize packet filter hooks. */
683 V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
684 V_link_pfil_hook.ph_af = AF_LINK;
685 if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
686 printf("%s: WARNING: unable to register pfil link hook, "
687 "error %d\n", __func__, i);
689 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
690 vnet_ether_init, NULL);
693 vnet_ether_destroy(__unused void *arg)
697 if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
698 printf("%s: WARNING: unable to unregister pfil link hook, "
699 "error %d\n", __func__, i);
701 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
702 vnet_ether_destroy, NULL);
707 ether_input(struct ifnet *ifp, struct mbuf *m)
711 * We will rely on rcvif being set properly in the deferred context,
712 * so assert it is correct here.
714 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
716 netisr_dispatch(NETISR_ETHER, m);
720 * Upper layer processing for a received Ethernet packet.
723 ether_demux(struct ifnet *ifp, struct mbuf *m)
725 struct ether_header *eh;
728 #if defined(NETATALK)
732 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
734 /* Do not grab PROMISC frames in case we are re-entered. */
735 if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
736 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
738 if (i != 0 || m == NULL)
742 eh = mtod(m, struct ether_header *);
743 ether_type = ntohs(eh->ether_type);
746 * If this frame has a VLAN tag other than 0, call vlan_input()
747 * if its module is loaded. Otherwise, drop.
749 if ((m->m_flags & M_VLANTAG) &&
750 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
751 if (ifp->if_vlantrunk == NULL) {
756 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
758 /* Clear before possibly re-entering ether_input(). */
759 m->m_flags &= ~M_PROMISC;
760 (*vlan_input_p)(ifp, m);
765 * Pass promiscuously received frames to the upper layer if the user
766 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
768 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
774 * Reset layer specific mbuf flags to avoid confusing upper layers.
775 * Strip off Ethernet header.
777 m->m_flags &= ~M_VLANTAG;
779 m_adj(m, ETHER_HDR_LEN);
782 * Dispatch frame to upper layer.
784 switch (ether_type) {
787 if ((m = ip_fastforward(m)) == NULL)
793 if (ifp->if_flags & IFF_NOARP) {
794 /* Discard packet if ARP is disabled on interface */
803 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
820 #endif /* NETATALK */
823 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
826 #if defined(NETATALK)
827 if (ether_type > ETHERMTU)
829 l = mtod(m, struct llc *);
830 if (l->llc_dsap == LLC_SNAP_LSAP &&
831 l->llc_ssap == LLC_SNAP_LSAP &&
832 l->llc_control == LLC_UI) {
833 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
834 sizeof(at_org_code)) == 0 &&
835 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
836 m_adj(m, LLC_SNAPFRAMELEN);
840 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
841 sizeof(aarp_org_code)) == 0 &&
842 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
843 m_adj(m, LLC_SNAPFRAMELEN);
848 #endif /* NETATALK */
851 netisr_dispatch(isr, m);
856 * Packet is to be discarded. If netgraph is present,
857 * hand the packet to it for last chance processing;
858 * otherwise dispose of it.
860 if (IFP2AC(ifp)->ac_netgraph != NULL) {
861 KASSERT(ng_ether_input_orphan_p != NULL,
862 ("ng_ether_input_orphan_p is NULL"));
864 * Put back the ethernet header so netgraph has a
865 * consistent view of inbound packets.
867 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
868 (*ng_ether_input_orphan_p)(ifp, m);
875 * Convert Ethernet address to printable (loggable) representation.
876 * This routine is for compatibility; it's better to just use
878 * printf("%6D", <pointer to address>, ":");
880 * since there's no static buffer involved.
883 ether_sprintf(const u_char *ap)
885 static char etherbuf[18];
886 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
891 * Perform common duties while attaching to interface list
894 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
898 struct sockaddr_dl *sdl;
900 ifp->if_addrlen = ETHER_ADDR_LEN;
901 ifp->if_hdrlen = ETHER_HDR_LEN;
903 ifp->if_mtu = ETHERMTU;
904 ifp->if_output = ether_output;
905 ifp->if_input = ether_input;
906 ifp->if_resolvemulti = ether_resolvemulti;
908 ifp->if_reassign = ether_reassign;
910 if (ifp->if_baudrate == 0)
911 ifp->if_baudrate = IF_Mbps(10); /* just a default */
912 ifp->if_broadcastaddr = etherbroadcastaddr;
915 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
916 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
917 sdl->sdl_type = IFT_ETHER;
918 sdl->sdl_alen = ifp->if_addrlen;
919 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
921 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
922 if (ng_ether_attach_p != NULL)
923 (*ng_ether_attach_p)(ifp);
925 /* Announce Ethernet MAC address if non-zero. */
926 for (i = 0; i < ifp->if_addrlen; i++)
929 if (i != ifp->if_addrlen)
930 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
932 uuid_ether_add(LLADDR(sdl));
936 * Perform common duties while detaching an Ethernet interface
939 ether_ifdetach(struct ifnet *ifp)
941 struct sockaddr_dl *sdl;
943 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
944 uuid_ether_del(LLADDR(sdl));
946 if (IFP2AC(ifp)->ac_netgraph != NULL) {
947 KASSERT(ng_ether_detach_p != NULL,
948 ("ng_ether_detach_p is NULL"));
949 (*ng_ether_detach_p)(ifp);
958 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
961 if (IFP2AC(ifp)->ac_netgraph != NULL) {
962 KASSERT(ng_ether_detach_p != NULL,
963 ("ng_ether_detach_p is NULL"));
964 (*ng_ether_detach_p)(ifp);
967 if (ng_ether_attach_p != NULL) {
968 CURVNET_SET_QUIET(new_vnet);
969 (*ng_ether_attach_p)(ifp);
975 SYSCTL_DECL(_net_link);
976 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
980 * This is for reference. We have a table-driven version
981 * of the little-endian crc32 generator, which is faster
982 * than the double-loop.
985 ether_crc32_le(const uint8_t *buf, size_t len)
992 crc = 0xffffffff; /* initial value */
994 for (i = 0; i < len; i++) {
995 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
996 carry = (crc ^ data) & 1;
999 crc = (crc ^ ETHER_CRC_POLY_LE);
1007 ether_crc32_le(const uint8_t *buf, size_t len)
1009 static const uint32_t crctab[] = {
1010 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1011 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1012 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1013 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1018 crc = 0xffffffff; /* initial value */
1020 for (i = 0; i < len; i++) {
1022 crc = (crc >> 4) ^ crctab[crc & 0xf];
1023 crc = (crc >> 4) ^ crctab[crc & 0xf];
1031 ether_crc32_be(const uint8_t *buf, size_t len)
1034 uint32_t crc, carry;
1038 crc = 0xffffffff; /* initial value */
1040 for (i = 0; i < len; i++) {
1041 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1042 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1045 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1053 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1055 struct ifaddr *ifa = (struct ifaddr *) data;
1056 struct ifreq *ifr = (struct ifreq *) data;
1061 ifp->if_flags |= IFF_UP;
1063 switch (ifa->ifa_addr->sa_family) {
1066 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1067 arp_ifinit(ifp, ifa);
1072 * XXX - This code is probably wrong
1076 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1078 if (ipx_nullhost(*ina))
1083 bcopy((caddr_t) ina->x_host.c_host,
1084 (caddr_t) IF_LLADDR(ifp),
1091 ifp->if_init(ifp->if_softc);
1096 ifp->if_init(ifp->if_softc);
1103 struct sockaddr *sa;
1105 sa = (struct sockaddr *) & ifr->ifr_data;
1106 bcopy(IF_LLADDR(ifp),
1107 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1113 * Set the interface MTU.
1115 if (ifr->ifr_mtu > ETHERMTU) {
1118 ifp->if_mtu = ifr->ifr_mtu;
1122 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1129 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1130 struct sockaddr *sa)
1132 struct sockaddr_dl *sdl;
1134 struct sockaddr_in *sin;
1137 struct sockaddr_in6 *sin6;
1141 switch(sa->sa_family) {
1144 * No mapping needed. Just check that it's a valid MC address.
1146 sdl = (struct sockaddr_dl *)sa;
1147 e_addr = LLADDR(sdl);
1148 if (!ETHER_IS_MULTICAST(e_addr))
1149 return EADDRNOTAVAIL;
1155 sin = (struct sockaddr_in *)sa;
1156 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1157 return EADDRNOTAVAIL;
1158 sdl = malloc(sizeof *sdl, M_IFMADDR,
1162 sdl->sdl_len = sizeof *sdl;
1163 sdl->sdl_family = AF_LINK;
1164 sdl->sdl_index = ifp->if_index;
1165 sdl->sdl_type = IFT_ETHER;
1166 sdl->sdl_alen = ETHER_ADDR_LEN;
1167 e_addr = LLADDR(sdl);
1168 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1169 *llsa = (struct sockaddr *)sdl;
1174 sin6 = (struct sockaddr_in6 *)sa;
1175 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1177 * An IP6 address of 0 means listen to all
1178 * of the Ethernet multicast address used for IP6.
1179 * (This is used for multicast routers.)
1181 ifp->if_flags |= IFF_ALLMULTI;
1185 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1186 return EADDRNOTAVAIL;
1187 sdl = malloc(sizeof *sdl, M_IFMADDR,
1191 sdl->sdl_len = sizeof *sdl;
1192 sdl->sdl_family = AF_LINK;
1193 sdl->sdl_index = ifp->if_index;
1194 sdl->sdl_type = IFT_ETHER;
1195 sdl->sdl_alen = ETHER_ADDR_LEN;
1196 e_addr = LLADDR(sdl);
1197 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1198 *llsa = (struct sockaddr *)sdl;
1204 * Well, the text isn't quite right, but it's the name
1207 return EAFNOSUPPORT;
1212 ether_alloc(u_char type, struct ifnet *ifp)
1216 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1223 ether_free(void *com, u_char type)
1226 free(com, M_ARPCOM);
1230 ether_modevent(module_t mod, int type, void *data)
1235 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1238 if_deregister_com_alloc(IFT_ETHER);
1247 static moduledata_t ether_mod = {
1254 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1256 struct ether_vlan_header vlan;
1259 KASSERT((m->m_flags & M_VLANTAG) != 0,
1260 ("%s: vlan information not present", __func__));
1261 KASSERT(m->m_len >= sizeof(struct ether_header),
1262 ("%s: mbuf not large enough for header", __func__));
1263 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1264 vlan.evl_proto = vlan.evl_encap_proto;
1265 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1266 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1267 m->m_len -= sizeof(struct ether_header);
1268 m->m_data += sizeof(struct ether_header);
1270 * If a data link has been supplied by the caller, then we will need to
1271 * re-create a stack allocated mbuf chain with the following structure:
1273 * (1) mbuf #1 will contain the supplied data link
1274 * (2) mbuf #2 will contain the vlan header
1275 * (3) mbuf #3 will contain the original mbuf's packet data
1277 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1281 mv.m_data = (caddr_t)&vlan;
1282 mv.m_len = sizeof(vlan);
1288 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1289 m->m_len += sizeof(struct ether_header);
1290 m->m_data -= sizeof(struct ether_header);
1294 ether_vlanencap(struct mbuf *m, uint16_t tag)
1296 struct ether_vlan_header *evl;
1298 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1301 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1303 if (m->m_len < sizeof(*evl)) {
1304 m = m_pullup(m, sizeof(*evl));
1310 * Transform the Ethernet header into an Ethernet header
1311 * with 802.1Q encapsulation.
1313 evl = mtod(m, struct ether_vlan_header *);
1314 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1315 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1316 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1317 evl->evl_tag = htons(tag);
1321 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1322 MODULE_VERSION(ether, 1);