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_var.h>
55 #include <net/if_arp.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_llc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
62 #include <net/ethernet.h>
63 #include <net/if_bridgevar.h>
64 #include <net/if_vlan_var.h>
65 #include <net/if_llatbl.h>
69 #include <netpfil/pf/pf_mtag.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/ip_var.h>
79 #include <netinet6/nd6.h>
83 #include <netipx/ipx.h>
84 #include <netipx/ipx_if.h>
87 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
88 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
89 const struct sockaddr *dst, short *tp, int *hlen);
92 #include <netatalk/at.h>
93 #include <netatalk/at_var.h>
94 #include <netatalk/at_extern.h>
96 #define llc_snap_org_code llc_un.type_snap.org_code
97 #define llc_snap_ether_type llc_un.type_snap.ether_type
99 extern u_char at_org_code[3];
100 extern u_char aarp_org_code[3];
101 #endif /* NETATALK */
103 #include <security/mac/mac_framework.h>
106 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
107 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
110 VNET_DEFINE(struct pfil_head, link_pfil_hook); /* Packet filter hooks */
112 /* netgraph node hooks for ng_ether(4) */
113 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
114 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
115 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
116 void (*ng_ether_attach_p)(struct ifnet *ifp);
117 void (*ng_ether_detach_p)(struct ifnet *ifp);
119 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
121 /* if_bridge(4) support */
122 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
123 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
124 struct sockaddr *, struct rtentry *);
125 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
127 /* if_lagg(4) support */
128 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
130 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
131 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
133 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
136 static void ether_reassign(struct ifnet *, struct vnet *, char *);
139 /* XXX: should be in an arp support file, not here */
140 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
142 #define ETHER_IS_BROADCAST(addr) \
143 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
145 #define senderr(e) do { error = (e); goto bad;} while (0)
148 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
152 if (src->m_pkthdr.csum_flags & CSUM_IP)
153 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
154 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
155 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
156 if (src->m_pkthdr.csum_flags & CSUM_SCTP)
157 csum_flags |= CSUM_SCTP_VALID;
158 dst->m_pkthdr.csum_flags |= csum_flags;
159 if (csum_flags & CSUM_DATA_VALID)
160 dst->m_pkthdr.csum_data = 0xffff;
164 * Ethernet output routine.
165 * Encapsulate a packet of type family for the local net.
166 * Use trailer local net encapsulation if enough data in first
167 * packet leaves a multiple of 512 bytes of data in remainder.
170 ether_output(struct ifnet *ifp, struct mbuf *m,
171 const struct sockaddr *dst, struct route *ro)
174 int error = 0, hdrcmplt = 0;
175 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
176 struct llentry *lle = NULL;
177 struct rtentry *rt0 = NULL;
178 struct ether_header *eh;
181 int hlen; /* link layer header length */
184 if (!(m->m_flags & (M_BCAST | M_MCAST)))
189 error = mac_ifnet_check_transmit(ifp, m);
195 if (ifp->if_flags & IFF_MONITOR)
197 if (!((ifp->if_flags & IFF_UP) &&
198 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
201 hlen = ETHER_HDR_LEN;
202 switch (dst->sa_family) {
205 if (lle != NULL && (lle->la_flags & LLE_VALID))
206 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
208 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
210 return (error == EWOULDBLOCK ? 0 : error);
211 type = htons(ETHERTYPE_IP);
216 ah = mtod(m, struct arphdr *);
217 ah->ar_hrd = htons(ARPHRD_ETHER);
219 loop_copy = 0; /* if this is for us, don't do it */
221 switch(ntohs(ah->ar_op)) {
222 case ARPOP_REVREQUEST:
224 type = htons(ETHERTYPE_REVARP);
229 type = htons(ETHERTYPE_ARP);
233 if (m->m_flags & M_BCAST)
234 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
236 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
243 if (lle != NULL && (lle->la_flags & LLE_VALID))
244 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
246 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
249 type = htons(ETHERTYPE_IPV6);
255 error = ef_outputp(ifp, &m, dst, &type, &hlen);
259 type = htons(ETHERTYPE_IPX);
260 bcopy(&((const struct sockaddr_ipx *)dst)->sipx_addr.x_host,
261 edst, sizeof (edst));
267 struct at_ifaddr *aa;
269 if ((aa = at_ifawithnet((const struct sockaddr_at *)dst)) == NULL)
270 senderr(EHOSTUNREACH); /* XXX */
271 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
272 ifa_free(&aa->aa_ifa);
276 * In the phase 2 case, need to prepend an mbuf for the llc header.
278 if ( aa->aa_flags & AFA_PHASE2 ) {
281 ifa_free(&aa->aa_ifa);
282 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
285 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
286 llc.llc_control = LLC_UI;
287 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
288 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
289 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
290 type = htons(m->m_pkthdr.len);
291 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
293 ifa_free(&aa->aa_ifa);
294 type = htons(ETHERTYPE_AT);
298 #endif /* NETATALK */
300 case pseudo_AF_HDRCMPLT:
302 const struct ether_header *eh;
305 eh = (const struct ether_header *)dst->sa_data;
306 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
310 loop_copy = 0; /* if this is for us, don't do it */
311 eh = (const struct ether_header *)dst->sa_data;
312 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
313 type = eh->ether_type;
317 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
318 senderr(EAFNOSUPPORT);
321 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
322 update_mbuf_csumflags(m, m);
323 return (if_simloop(ifp, m, dst->sa_family, 0));
327 * Add local net header. If no space in first mbuf,
330 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
333 eh = mtod(m, struct ether_header *);
334 (void)memcpy(&eh->ether_type, &type,
335 sizeof(eh->ether_type));
336 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
338 (void)memcpy(eh->ether_shost, esrc,
339 sizeof(eh->ether_shost));
341 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
342 sizeof(eh->ether_shost));
345 * If a simplex interface, and the packet is being sent to our
346 * Ethernet address or a broadcast address, loopback a copy.
347 * XXX To make a simplex device behave exactly like a duplex
348 * device, we should copy in the case of sending to our own
349 * ethernet address (thus letting the original actually appear
350 * on the wire). However, we don't do that here for security
351 * reasons and compatibility with the original behavior.
353 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
354 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
355 if (m->m_flags & M_BCAST) {
359 * Because if_simloop() modifies the packet, we need a
360 * writable copy through m_dup() instead of a readonly
361 * one as m_copy[m] would give us. The alternative would
362 * be to modify if_simloop() to handle the readonly mbuf,
363 * but performancewise it is mostly equivalent (trading
364 * extra data copying vs. extra locking).
366 * XXX This is a local workaround. A number of less
367 * often used kernel parts suffer from the same bug.
368 * See PR kern/105943 for a proposed general solution.
370 if ((n = m_dup(m, M_NOWAIT)) != NULL) {
371 update_mbuf_csumflags(m, n);
372 (void)if_simloop(ifp, n, dst->sa_family, hlen);
375 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
376 ETHER_ADDR_LEN) == 0) {
377 update_mbuf_csumflags(m, m);
378 (void) if_simloop(ifp, m, dst->sa_family, hlen);
379 return (0); /* XXX */
384 * Bridges require special output handling.
386 if (ifp->if_bridge) {
387 BRIDGE_OUTPUT(ifp, m, error);
391 #if defined(INET) || defined(INET6)
393 (error = (*carp_output_p)(ifp, m, dst)))
397 /* Handle ng_ether(4) processing, if any */
398 if (IFP2AC(ifp)->ac_netgraph != NULL) {
399 KASSERT(ng_ether_output_p != NULL,
400 ("ng_ether_output_p is NULL"));
401 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
410 /* Continue with link-layer output */
411 return ether_output_frame(ifp, m);
415 * Ethernet link layer output routine to send a raw frame to the device.
417 * This assumes that the 14 byte Ethernet header is present and contiguous
418 * in the first mbuf (if BRIDGE'ing).
421 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
425 if (PFIL_HOOKED(&V_link_pfil_hook)) {
426 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
436 * Queue message on interface, update output statistics if
437 * successful, and start output if interface not yet active.
439 return ((ifp->if_transmit)(ifp, m));
442 #if defined(INET) || defined(INET6)
446 * Process a received Ethernet packet; the packet is in the
447 * mbuf chain m with the ethernet header at the front.
450 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
452 struct ether_header *eh;
455 if ((ifp->if_flags & IFF_UP) == 0) {
460 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
461 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
467 * Do consistency checks to verify assumptions
468 * made by code past this point.
470 if ((m->m_flags & M_PKTHDR) == 0) {
471 if_printf(ifp, "discard frame w/o packet header\n");
476 if (m->m_len < ETHER_HDR_LEN) {
477 /* XXX maybe should pullup? */
478 if_printf(ifp, "discard frame w/o leading ethernet "
479 "header (len %u pkt len %u)\n",
480 m->m_len, m->m_pkthdr.len);
485 eh = mtod(m, struct ether_header *);
486 etype = ntohs(eh->ether_type);
487 if (m->m_pkthdr.rcvif == NULL) {
488 if_printf(ifp, "discard frame w/o interface pointer\n");
494 if (m->m_pkthdr.rcvif != ifp) {
495 if_printf(ifp, "Warning, frame marked as received on %s\n",
496 m->m_pkthdr.rcvif->if_xname);
500 CURVNET_SET_QUIET(ifp->if_vnet);
502 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
503 if (ETHER_IS_BROADCAST(eh->ether_dhost))
504 m->m_flags |= M_BCAST;
506 m->m_flags |= M_MCAST;
512 * Tag the mbuf with an appropriate MAC label before any other
513 * consumers can get to it.
515 mac_ifnet_create_mbuf(ifp, m);
519 * Give bpf a chance at the packet.
521 ETHER_BPF_MTAP(ifp, m);
524 * If the CRC is still on the packet, trim it off. We do this once
525 * and once only in case we are re-entered. Nothing else on the
526 * Ethernet receive path expects to see the FCS.
528 if (m->m_flags & M_HASFCS) {
529 m_adj(m, -ETHER_CRC_LEN);
530 m->m_flags &= ~M_HASFCS;
533 if (!(ifp->if_capenable & IFCAP_HWSTATS))
534 ifp->if_ibytes += m->m_pkthdr.len;
536 /* Allow monitor mode to claim this frame, after stats are updated. */
537 if (ifp->if_flags & IFF_MONITOR) {
543 /* Handle input from a lagg(4) port */
544 if (ifp->if_type == IFT_IEEE8023ADLAG) {
545 KASSERT(lagg_input_p != NULL,
546 ("%s: if_lagg not loaded!", __func__));
547 m = (*lagg_input_p)(ifp, m);
549 ifp = m->m_pkthdr.rcvif;
557 * If the hardware did not process an 802.1Q tag, do this now,
558 * to allow 802.1P priority frames to be passed to the main input
560 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
562 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
563 struct ether_vlan_header *evl;
565 if (m->m_len < sizeof(*evl) &&
566 (m = m_pullup(m, sizeof(*evl))) == NULL) {
568 if_printf(ifp, "cannot pullup VLAN header\n");
576 evl = mtod(m, struct ether_vlan_header *);
577 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
578 m->m_flags |= M_VLANTAG;
580 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
581 ETHER_HDR_LEN - ETHER_TYPE_LEN);
582 m_adj(m, ETHER_VLAN_ENCAP_LEN);
583 eh = mtod(m, struct ether_header *);
586 M_SETFIB(m, ifp->if_fib);
588 /* Allow ng_ether(4) to claim this frame. */
589 if (IFP2AC(ifp)->ac_netgraph != NULL) {
590 KASSERT(ng_ether_input_p != NULL,
591 ("%s: ng_ether_input_p is NULL", __func__));
592 m->m_flags &= ~M_PROMISC;
593 (*ng_ether_input_p)(ifp, &m);
598 eh = mtod(m, struct ether_header *);
602 * Allow if_bridge(4) to claim this frame.
603 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
604 * and the frame should be delivered locally.
606 if (ifp->if_bridge != NULL) {
607 m->m_flags &= ~M_PROMISC;
608 BRIDGE_INPUT(ifp, m);
613 eh = mtod(m, struct ether_header *);
616 #if defined(INET) || defined(INET6)
618 * Clear M_PROMISC on frame so that carp(4) will see it when the
619 * mbuf flows up to Layer 3.
620 * FreeBSD's implementation of carp(4) uses the inprotosw
621 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
622 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
623 * is outside the scope of the M_PROMISC test below.
624 * TODO: Maintain a hash table of ethernet addresses other than
625 * ether_dhost which may be active on this ifp.
627 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
628 m->m_flags &= ~M_PROMISC;
633 * If the frame received was not for our MAC address, set the
634 * M_PROMISC flag on the mbuf chain. The frame may need to
635 * be seen by the rest of the Ethernet input path in case of
636 * re-entry (e.g. bridge, vlan, netgraph) but should not be
637 * seen by upper protocol layers.
639 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
640 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
641 m->m_flags |= M_PROMISC;
644 if (harvest.ethernet)
645 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
652 * Ethernet input dispatch; by default, direct dispatch here regardless of
653 * global configuration.
656 ether_nh_input(struct mbuf *m)
659 ether_input_internal(m->m_pkthdr.rcvif, m);
662 static struct netisr_handler ether_nh = {
664 .nh_handler = ether_nh_input,
665 .nh_proto = NETISR_ETHER,
666 .nh_policy = NETISR_POLICY_SOURCE,
667 .nh_dispatch = NETISR_DISPATCH_DIRECT,
671 ether_init(__unused void *arg)
674 netisr_register(ðer_nh);
676 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
679 vnet_ether_init(__unused void *arg)
683 /* Initialize packet filter hooks. */
684 V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
685 V_link_pfil_hook.ph_af = AF_LINK;
686 if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
687 printf("%s: WARNING: unable to register pfil link hook, "
688 "error %d\n", __func__, i);
690 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
691 vnet_ether_init, NULL);
694 vnet_ether_destroy(__unused void *arg)
698 if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
699 printf("%s: WARNING: unable to unregister pfil link hook, "
700 "error %d\n", __func__, i);
702 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
703 vnet_ether_destroy, NULL);
708 ether_input(struct ifnet *ifp, struct mbuf *m)
714 * The drivers are allowed to pass in a chain of packets linked with
715 * m_nextpkt. We split them up into separate packets here and pass
716 * them up. This allows the drivers to amortize the receive lock.
723 * We will rely on rcvif being set properly in the deferred context,
724 * so assert it is correct here.
726 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
727 netisr_dispatch(NETISR_ETHER, m);
733 * Upper layer processing for a received Ethernet packet.
736 ether_demux(struct ifnet *ifp, struct mbuf *m)
738 struct ether_header *eh;
741 #if defined(NETATALK)
745 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
747 /* Do not grab PROMISC frames in case we are re-entered. */
748 if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
749 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
751 if (i != 0 || m == NULL)
755 eh = mtod(m, struct ether_header *);
756 ether_type = ntohs(eh->ether_type);
759 * If this frame has a VLAN tag other than 0, call vlan_input()
760 * if its module is loaded. Otherwise, drop.
762 if ((m->m_flags & M_VLANTAG) &&
763 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
764 if (ifp->if_vlantrunk == NULL) {
769 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
771 /* Clear before possibly re-entering ether_input(). */
772 m->m_flags &= ~M_PROMISC;
773 (*vlan_input_p)(ifp, m);
778 * Pass promiscuously received frames to the upper layer if the user
779 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
781 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
787 * Reset layer specific mbuf flags to avoid confusing upper layers.
788 * Strip off Ethernet header.
790 m->m_flags &= ~M_VLANTAG;
792 m_adj(m, ETHER_HDR_LEN);
795 * Dispatch frame to upper layer.
797 switch (ether_type) {
800 if ((m = ip_fastforward(m)) == NULL)
806 if (ifp->if_flags & IFF_NOARP) {
807 /* Discard packet if ARP is disabled on interface */
816 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
833 #endif /* NETATALK */
836 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
839 #if defined(NETATALK)
840 if (ether_type > ETHERMTU)
842 l = mtod(m, struct llc *);
843 if (l->llc_dsap == LLC_SNAP_LSAP &&
844 l->llc_ssap == LLC_SNAP_LSAP &&
845 l->llc_control == LLC_UI) {
846 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
847 sizeof(at_org_code)) == 0 &&
848 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
849 m_adj(m, LLC_SNAPFRAMELEN);
853 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
854 sizeof(aarp_org_code)) == 0 &&
855 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
856 m_adj(m, LLC_SNAPFRAMELEN);
861 #endif /* NETATALK */
864 netisr_dispatch(isr, m);
869 * Packet is to be discarded. If netgraph is present,
870 * hand the packet to it for last chance processing;
871 * otherwise dispose of it.
873 if (IFP2AC(ifp)->ac_netgraph != NULL) {
874 KASSERT(ng_ether_input_orphan_p != NULL,
875 ("ng_ether_input_orphan_p is NULL"));
877 * Put back the ethernet header so netgraph has a
878 * consistent view of inbound packets.
880 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
881 (*ng_ether_input_orphan_p)(ifp, m);
888 * Convert Ethernet address to printable (loggable) representation.
889 * This routine is for compatibility; it's better to just use
891 * printf("%6D", <pointer to address>, ":");
893 * since there's no static buffer involved.
896 ether_sprintf(const u_char *ap)
898 static char etherbuf[18];
899 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
904 * Perform common duties while attaching to interface list
907 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
911 struct sockaddr_dl *sdl;
913 ifp->if_addrlen = ETHER_ADDR_LEN;
914 ifp->if_hdrlen = ETHER_HDR_LEN;
916 ifp->if_mtu = ETHERMTU;
917 ifp->if_output = ether_output;
918 ifp->if_input = ether_input;
919 ifp->if_resolvemulti = ether_resolvemulti;
921 ifp->if_reassign = ether_reassign;
923 if (ifp->if_baudrate == 0)
924 ifp->if_baudrate = IF_Mbps(10); /* just a default */
925 ifp->if_broadcastaddr = etherbroadcastaddr;
928 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
929 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
930 sdl->sdl_type = IFT_ETHER;
931 sdl->sdl_alen = ifp->if_addrlen;
932 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
934 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
935 if (ng_ether_attach_p != NULL)
936 (*ng_ether_attach_p)(ifp);
938 /* Announce Ethernet MAC address if non-zero. */
939 for (i = 0; i < ifp->if_addrlen; i++)
942 if (i != ifp->if_addrlen)
943 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
945 uuid_ether_add(LLADDR(sdl));
949 * Perform common duties while detaching an Ethernet interface
952 ether_ifdetach(struct ifnet *ifp)
954 struct sockaddr_dl *sdl;
956 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
957 uuid_ether_del(LLADDR(sdl));
959 if (IFP2AC(ifp)->ac_netgraph != NULL) {
960 KASSERT(ng_ether_detach_p != NULL,
961 ("ng_ether_detach_p is NULL"));
962 (*ng_ether_detach_p)(ifp);
971 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
974 if (IFP2AC(ifp)->ac_netgraph != NULL) {
975 KASSERT(ng_ether_detach_p != NULL,
976 ("ng_ether_detach_p is NULL"));
977 (*ng_ether_detach_p)(ifp);
980 if (ng_ether_attach_p != NULL) {
981 CURVNET_SET_QUIET(new_vnet);
982 (*ng_ether_attach_p)(ifp);
988 SYSCTL_DECL(_net_link);
989 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
993 * This is for reference. We have a table-driven version
994 * of the little-endian crc32 generator, which is faster
995 * than the double-loop.
998 ether_crc32_le(const uint8_t *buf, size_t len)
1005 crc = 0xffffffff; /* initial value */
1007 for (i = 0; i < len; i++) {
1008 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1009 carry = (crc ^ data) & 1;
1012 crc = (crc ^ ETHER_CRC_POLY_LE);
1020 ether_crc32_le(const uint8_t *buf, size_t len)
1022 static const uint32_t crctab[] = {
1023 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1024 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1025 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1026 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1031 crc = 0xffffffff; /* initial value */
1033 for (i = 0; i < len; i++) {
1035 crc = (crc >> 4) ^ crctab[crc & 0xf];
1036 crc = (crc >> 4) ^ crctab[crc & 0xf];
1044 ether_crc32_be(const uint8_t *buf, size_t len)
1047 uint32_t crc, carry;
1051 crc = 0xffffffff; /* initial value */
1053 for (i = 0; i < len; i++) {
1054 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1055 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1058 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1066 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1068 struct ifaddr *ifa = (struct ifaddr *) data;
1069 struct ifreq *ifr = (struct ifreq *) data;
1074 ifp->if_flags |= IFF_UP;
1076 switch (ifa->ifa_addr->sa_family) {
1079 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1080 arp_ifinit(ifp, ifa);
1085 * XXX - This code is probably wrong
1089 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1091 if (ipx_nullhost(*ina))
1096 bcopy((caddr_t) ina->x_host.c_host,
1097 (caddr_t) IF_LLADDR(ifp),
1104 ifp->if_init(ifp->if_softc);
1109 ifp->if_init(ifp->if_softc);
1116 struct sockaddr *sa;
1118 sa = (struct sockaddr *) & ifr->ifr_data;
1119 bcopy(IF_LLADDR(ifp),
1120 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1126 * Set the interface MTU.
1128 if (ifr->ifr_mtu > ETHERMTU) {
1131 ifp->if_mtu = ifr->ifr_mtu;
1135 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1142 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1143 struct sockaddr *sa)
1145 struct sockaddr_dl *sdl;
1147 struct sockaddr_in *sin;
1150 struct sockaddr_in6 *sin6;
1154 switch(sa->sa_family) {
1157 * No mapping needed. Just check that it's a valid MC address.
1159 sdl = (struct sockaddr_dl *)sa;
1160 e_addr = LLADDR(sdl);
1161 if (!ETHER_IS_MULTICAST(e_addr))
1162 return EADDRNOTAVAIL;
1168 sin = (struct sockaddr_in *)sa;
1169 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1170 return EADDRNOTAVAIL;
1171 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1172 sdl->sdl_alen = ETHER_ADDR_LEN;
1173 e_addr = LLADDR(sdl);
1174 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1175 *llsa = (struct sockaddr *)sdl;
1180 sin6 = (struct sockaddr_in6 *)sa;
1181 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1183 * An IP6 address of 0 means listen to all
1184 * of the Ethernet multicast address used for IP6.
1185 * (This is used for multicast routers.)
1187 ifp->if_flags |= IFF_ALLMULTI;
1191 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1192 return EADDRNOTAVAIL;
1193 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1194 sdl->sdl_alen = ETHER_ADDR_LEN;
1195 e_addr = LLADDR(sdl);
1196 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1197 *llsa = (struct sockaddr *)sdl;
1203 * Well, the text isn't quite right, but it's the name
1206 return EAFNOSUPPORT;
1211 ether_alloc(u_char type, struct ifnet *ifp)
1215 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1222 ether_free(void *com, u_char type)
1225 free(com, M_ARPCOM);
1229 ether_modevent(module_t mod, int type, void *data)
1234 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1237 if_deregister_com_alloc(IFT_ETHER);
1246 static moduledata_t ether_mod = {
1253 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1255 struct ether_vlan_header vlan;
1258 KASSERT((m->m_flags & M_VLANTAG) != 0,
1259 ("%s: vlan information not present", __func__));
1260 KASSERT(m->m_len >= sizeof(struct ether_header),
1261 ("%s: mbuf not large enough for header", __func__));
1262 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1263 vlan.evl_proto = vlan.evl_encap_proto;
1264 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1265 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1266 m->m_len -= sizeof(struct ether_header);
1267 m->m_data += sizeof(struct ether_header);
1269 * If a data link has been supplied by the caller, then we will need to
1270 * re-create a stack allocated mbuf chain with the following structure:
1272 * (1) mbuf #1 will contain the supplied data link
1273 * (2) mbuf #2 will contain the vlan header
1274 * (3) mbuf #3 will contain the original mbuf's packet data
1276 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1280 mv.m_data = (caddr_t)&vlan;
1281 mv.m_len = sizeof(vlan);
1287 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1288 m->m_len += sizeof(struct ether_header);
1289 m->m_data -= sizeof(struct ether_header);
1293 ether_vlanencap(struct mbuf *m, uint16_t tag)
1295 struct ether_vlan_header *evl;
1297 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1300 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1302 if (m->m_len < sizeof(*evl)) {
1303 m = m_pullup(m, sizeof(*evl));
1309 * Transform the Ethernet header into an Ethernet header
1310 * with 802.1Q encapsulation.
1312 evl = mtod(m, struct ether_vlan_header *);
1313 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1314 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1315 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1316 evl->evl_tag = htons(tag);
1320 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1321 MODULE_VERSION(ether, 1);