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
34 #include "opt_inet6.h"
35 #include "opt_netgraph.h"
36 #include "opt_mbuf_profiling.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
46 #include <sys/random.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
53 #include <net/if_var.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/in_rss.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/ip_var.h>
79 #include <netinet6/nd6.h>
82 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
83 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
84 const struct sockaddr *dst, short *tp, int *hlen);
86 #include <security/mac/mac_framework.h>
89 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
90 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
93 VNET_DEFINE(struct pfil_head, link_pfil_hook); /* Packet filter hooks */
95 /* netgraph node hooks for ng_ether(4) */
96 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
97 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
98 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
99 void (*ng_ether_attach_p)(struct ifnet *ifp);
100 void (*ng_ether_detach_p)(struct ifnet *ifp);
102 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
104 /* if_bridge(4) support */
105 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
106 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
107 struct sockaddr *, struct rtentry *);
108 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
110 /* if_lagg(4) support */
111 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
113 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
114 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
116 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
119 static void ether_reassign(struct ifnet *, struct vnet *, char *);
122 /* XXX: should be in an arp support file, not here */
123 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
125 #define ETHER_IS_BROADCAST(addr) \
126 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
128 #define senderr(e) do { error = (e); goto bad;} while (0)
131 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
135 if (src->m_pkthdr.csum_flags & CSUM_IP)
136 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
137 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
138 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
139 if (src->m_pkthdr.csum_flags & CSUM_SCTP)
140 csum_flags |= CSUM_SCTP_VALID;
141 dst->m_pkthdr.csum_flags |= csum_flags;
142 if (csum_flags & CSUM_DATA_VALID)
143 dst->m_pkthdr.csum_data = 0xffff;
147 * Ethernet output routine.
148 * Encapsulate a packet of type family for the local net.
149 * Use trailer local net encapsulation if enough data in first
150 * packet leaves a multiple of 512 bytes of data in remainder.
153 ether_output(struct ifnet *ifp, struct mbuf *m,
154 const struct sockaddr *dst, struct route *ro)
157 int error = 0, hdrcmplt = 0;
158 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
159 struct llentry *lle = NULL;
160 struct rtentry *rt0 = NULL;
161 struct ether_header *eh;
164 int hlen; /* link layer header length */
167 if (!(m->m_flags & (M_BCAST | M_MCAST)))
172 error = mac_ifnet_check_transmit(ifp, m);
178 if (ifp->if_flags & IFF_MONITOR)
180 if (!((ifp->if_flags & IFF_UP) &&
181 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
184 hlen = ETHER_HDR_LEN;
185 switch (dst->sa_family) {
188 if (lle != NULL && (lle->la_flags & LLE_VALID))
189 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
191 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
193 return (error == EWOULDBLOCK ? 0 : error);
194 type = htons(ETHERTYPE_IP);
199 ah = mtod(m, struct arphdr *);
200 ah->ar_hrd = htons(ARPHRD_ETHER);
202 loop_copy = 0; /* if this is for us, don't do it */
204 switch(ntohs(ah->ar_op)) {
205 case ARPOP_REVREQUEST:
207 type = htons(ETHERTYPE_REVARP);
212 type = htons(ETHERTYPE_ARP);
216 if (m->m_flags & M_BCAST)
217 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
219 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
226 if (lle != NULL && (lle->la_flags & LLE_VALID))
227 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
229 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
232 type = htons(ETHERTYPE_IPV6);
235 case pseudo_AF_HDRCMPLT:
237 const struct ether_header *eh;
240 eh = (const struct ether_header *)dst->sa_data;
241 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
245 loop_copy = 0; /* if this is for us, don't do it */
246 eh = (const struct ether_header *)dst->sa_data;
247 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
248 type = eh->ether_type;
252 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
253 senderr(EAFNOSUPPORT);
256 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
257 update_mbuf_csumflags(m, m);
258 return (if_simloop(ifp, m, dst->sa_family, 0));
262 * Add local net header. If no space in first mbuf,
265 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
268 eh = mtod(m, struct ether_header *);
269 (void)memcpy(&eh->ether_type, &type,
270 sizeof(eh->ether_type));
271 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
273 (void)memcpy(eh->ether_shost, esrc,
274 sizeof(eh->ether_shost));
276 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
277 sizeof(eh->ether_shost));
280 * If a simplex interface, and the packet is being sent to our
281 * Ethernet address or a broadcast address, loopback a copy.
282 * XXX To make a simplex device behave exactly like a duplex
283 * device, we should copy in the case of sending to our own
284 * ethernet address (thus letting the original actually appear
285 * on the wire). However, we don't do that here for security
286 * reasons and compatibility with the original behavior.
288 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
289 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
290 if (m->m_flags & M_BCAST) {
294 * Because if_simloop() modifies the packet, we need a
295 * writable copy through m_dup() instead of a readonly
296 * one as m_copy[m] would give us. The alternative would
297 * be to modify if_simloop() to handle the readonly mbuf,
298 * but performancewise it is mostly equivalent (trading
299 * extra data copying vs. extra locking).
301 * XXX This is a local workaround. A number of less
302 * often used kernel parts suffer from the same bug.
303 * See PR kern/105943 for a proposed general solution.
305 if ((n = m_dup(m, M_NOWAIT)) != NULL) {
306 update_mbuf_csumflags(m, n);
307 (void)if_simloop(ifp, n, dst->sa_family, hlen);
310 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
311 ETHER_ADDR_LEN) == 0) {
312 update_mbuf_csumflags(m, m);
313 (void) if_simloop(ifp, m, dst->sa_family, hlen);
314 return (0); /* XXX */
319 * Bridges require special output handling.
321 if (ifp->if_bridge) {
322 BRIDGE_OUTPUT(ifp, m, error);
326 #if defined(INET) || defined(INET6)
328 (error = (*carp_output_p)(ifp, m, dst)))
332 /* Handle ng_ether(4) processing, if any */
333 if (IFP2AC(ifp)->ac_netgraph != NULL) {
334 KASSERT(ng_ether_output_p != NULL,
335 ("ng_ether_output_p is NULL"));
336 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
345 /* Continue with link-layer output */
346 return ether_output_frame(ifp, m);
350 * Ethernet link layer output routine to send a raw frame to the device.
352 * This assumes that the 14 byte Ethernet header is present and contiguous
353 * in the first mbuf (if BRIDGE'ing).
356 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
360 if (PFIL_HOOKED(&V_link_pfil_hook)) {
361 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
371 * Queue message on interface, update output statistics if
372 * successful, and start output if interface not yet active.
374 return ((ifp->if_transmit)(ifp, m));
377 #if defined(INET) || defined(INET6)
381 * Process a received Ethernet packet; the packet is in the
382 * mbuf chain m with the ethernet header at the front.
385 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
387 struct ether_header *eh;
390 if ((ifp->if_flags & IFF_UP) == 0) {
395 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
396 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
402 * Do consistency checks to verify assumptions
403 * made by code past this point.
405 if ((m->m_flags & M_PKTHDR) == 0) {
406 if_printf(ifp, "discard frame w/o packet header\n");
411 if (m->m_len < ETHER_HDR_LEN) {
412 /* XXX maybe should pullup? */
413 if_printf(ifp, "discard frame w/o leading ethernet "
414 "header (len %u pkt len %u)\n",
415 m->m_len, m->m_pkthdr.len);
420 eh = mtod(m, struct ether_header *);
421 etype = ntohs(eh->ether_type);
422 if (m->m_pkthdr.rcvif == NULL) {
423 if_printf(ifp, "discard frame w/o interface pointer\n");
429 if (m->m_pkthdr.rcvif != ifp) {
430 if_printf(ifp, "Warning, frame marked as received on %s\n",
431 m->m_pkthdr.rcvif->if_xname);
435 CURVNET_SET_QUIET(ifp->if_vnet);
437 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
438 if (ETHER_IS_BROADCAST(eh->ether_dhost))
439 m->m_flags |= M_BCAST;
441 m->m_flags |= M_MCAST;
447 * Tag the mbuf with an appropriate MAC label before any other
448 * consumers can get to it.
450 mac_ifnet_create_mbuf(ifp, m);
454 * Give bpf a chance at the packet.
456 ETHER_BPF_MTAP(ifp, m);
459 * If the CRC is still on the packet, trim it off. We do this once
460 * and once only in case we are re-entered. Nothing else on the
461 * Ethernet receive path expects to see the FCS.
463 if (m->m_flags & M_HASFCS) {
464 m_adj(m, -ETHER_CRC_LEN);
465 m->m_flags &= ~M_HASFCS;
468 if (!(ifp->if_capenable & IFCAP_HWSTATS))
469 ifp->if_ibytes += m->m_pkthdr.len;
471 /* Allow monitor mode to claim this frame, after stats are updated. */
472 if (ifp->if_flags & IFF_MONITOR) {
478 /* Handle input from a lagg(4) port */
479 if (ifp->if_type == IFT_IEEE8023ADLAG) {
480 KASSERT(lagg_input_p != NULL,
481 ("%s: if_lagg not loaded!", __func__));
482 m = (*lagg_input_p)(ifp, m);
484 ifp = m->m_pkthdr.rcvif;
492 * If the hardware did not process an 802.1Q tag, do this now,
493 * to allow 802.1P priority frames to be passed to the main input
495 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
497 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
498 struct ether_vlan_header *evl;
500 if (m->m_len < sizeof(*evl) &&
501 (m = m_pullup(m, sizeof(*evl))) == NULL) {
503 if_printf(ifp, "cannot pullup VLAN header\n");
511 evl = mtod(m, struct ether_vlan_header *);
512 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
513 m->m_flags |= M_VLANTAG;
515 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
516 ETHER_HDR_LEN - ETHER_TYPE_LEN);
517 m_adj(m, ETHER_VLAN_ENCAP_LEN);
518 eh = mtod(m, struct ether_header *);
521 M_SETFIB(m, ifp->if_fib);
523 /* Allow ng_ether(4) to claim this frame. */
524 if (IFP2AC(ifp)->ac_netgraph != NULL) {
525 KASSERT(ng_ether_input_p != NULL,
526 ("%s: ng_ether_input_p is NULL", __func__));
527 m->m_flags &= ~M_PROMISC;
528 (*ng_ether_input_p)(ifp, &m);
533 eh = mtod(m, struct ether_header *);
537 * Allow if_bridge(4) to claim this frame.
538 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
539 * and the frame should be delivered locally.
541 if (ifp->if_bridge != NULL) {
542 m->m_flags &= ~M_PROMISC;
543 BRIDGE_INPUT(ifp, m);
548 eh = mtod(m, struct ether_header *);
551 #if defined(INET) || defined(INET6)
553 * Clear M_PROMISC on frame so that carp(4) will see it when the
554 * mbuf flows up to Layer 3.
555 * FreeBSD's implementation of carp(4) uses the inprotosw
556 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
557 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
558 * is outside the scope of the M_PROMISC test below.
559 * TODO: Maintain a hash table of ethernet addresses other than
560 * ether_dhost which may be active on this ifp.
562 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
563 m->m_flags &= ~M_PROMISC;
568 * If the frame received was not for our MAC address, set the
569 * M_PROMISC flag on the mbuf chain. The frame may need to
570 * be seen by the rest of the Ethernet input path in case of
571 * re-entry (e.g. bridge, vlan, netgraph) but should not be
572 * seen by upper protocol layers.
574 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
575 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
576 m->m_flags |= M_PROMISC;
579 if (harvest.ethernet)
580 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
587 * Ethernet input dispatch; by default, direct dispatch here regardless of
588 * global configuration. However, if RSS is enabled, hook up RSS affinity
589 * so that when deferred or hybrid dispatch is enabled, we can redistribute
592 * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
593 * not it had already done work distribution via multi-queue. Then we could
594 * direct dispatch in the event load balancing was already complete and
595 * handle the case of interfaces with different capabilities better.
597 * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
598 * at multiple layers?
600 * XXXRW: For now, enable all this only if RSS is compiled in, although it
601 * works fine without RSS. Need to characterise the performance overhead
602 * of the detour through the netisr code in the event the result is always
606 ether_nh_input(struct mbuf *m)
609 ether_input_internal(m->m_pkthdr.rcvif, m);
612 static struct netisr_handler ether_nh = {
614 .nh_handler = ether_nh_input,
615 .nh_proto = NETISR_ETHER,
617 .nh_policy = NETISR_POLICY_CPU,
618 .nh_dispatch = NETISR_DISPATCH_DIRECT,
619 .nh_m2cpuid = rss_m2cpuid,
621 .nh_policy = NETISR_POLICY_SOURCE,
622 .nh_dispatch = NETISR_DISPATCH_DIRECT,
627 ether_init(__unused void *arg)
630 netisr_register(ðer_nh);
632 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
635 vnet_ether_init(__unused void *arg)
639 /* Initialize packet filter hooks. */
640 V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
641 V_link_pfil_hook.ph_af = AF_LINK;
642 if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
643 printf("%s: WARNING: unable to register pfil link hook, "
644 "error %d\n", __func__, i);
646 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
647 vnet_ether_init, NULL);
650 vnet_ether_destroy(__unused void *arg)
654 if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
655 printf("%s: WARNING: unable to unregister pfil link hook, "
656 "error %d\n", __func__, i);
658 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
659 vnet_ether_destroy, NULL);
664 ether_input(struct ifnet *ifp, struct mbuf *m)
670 * The drivers are allowed to pass in a chain of packets linked with
671 * m_nextpkt. We split them up into separate packets here and pass
672 * them up. This allows the drivers to amortize the receive lock.
679 * We will rely on rcvif being set properly in the deferred context,
680 * so assert it is correct here.
682 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
683 netisr_dispatch(NETISR_ETHER, m);
689 * Upper layer processing for a received Ethernet packet.
692 ether_demux(struct ifnet *ifp, struct mbuf *m)
694 struct ether_header *eh;
698 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
700 /* Do not grab PROMISC frames in case we are re-entered. */
701 if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
702 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
704 if (i != 0 || m == NULL)
708 eh = mtod(m, struct ether_header *);
709 ether_type = ntohs(eh->ether_type);
712 * If this frame has a VLAN tag other than 0, call vlan_input()
713 * if its module is loaded. Otherwise, drop.
715 if ((m->m_flags & M_VLANTAG) &&
716 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
717 if (ifp->if_vlantrunk == NULL) {
722 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
724 /* Clear before possibly re-entering ether_input(). */
725 m->m_flags &= ~M_PROMISC;
726 (*vlan_input_p)(ifp, m);
731 * Pass promiscuously received frames to the upper layer if the user
732 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
734 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
740 * Reset layer specific mbuf flags to avoid confusing upper layers.
741 * Strip off Ethernet header.
743 m->m_flags &= ~M_VLANTAG;
745 m_adj(m, ETHER_HDR_LEN);
748 * Dispatch frame to upper layer.
750 switch (ether_type) {
753 if ((m = ip_fastforward(m)) == NULL)
759 if (ifp->if_flags & IFF_NOARP) {
760 /* Discard packet if ARP is disabled on interface */
775 netisr_dispatch(isr, m);
780 * Packet is to be discarded. If netgraph is present,
781 * hand the packet to it for last chance processing;
782 * otherwise dispose of it.
784 if (IFP2AC(ifp)->ac_netgraph != NULL) {
785 KASSERT(ng_ether_input_orphan_p != NULL,
786 ("ng_ether_input_orphan_p is NULL"));
788 * Put back the ethernet header so netgraph has a
789 * consistent view of inbound packets.
791 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
792 (*ng_ether_input_orphan_p)(ifp, m);
799 * Convert Ethernet address to printable (loggable) representation.
800 * This routine is for compatibility; it's better to just use
802 * printf("%6D", <pointer to address>, ":");
804 * since there's no static buffer involved.
807 ether_sprintf(const u_char *ap)
809 static char etherbuf[18];
810 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
815 * Perform common duties while attaching to interface list
818 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
822 struct sockaddr_dl *sdl;
824 ifp->if_addrlen = ETHER_ADDR_LEN;
825 ifp->if_hdrlen = ETHER_HDR_LEN;
827 ifp->if_mtu = ETHERMTU;
828 ifp->if_output = ether_output;
829 ifp->if_input = ether_input;
830 ifp->if_resolvemulti = ether_resolvemulti;
832 ifp->if_reassign = ether_reassign;
834 if (ifp->if_baudrate == 0)
835 ifp->if_baudrate = IF_Mbps(10); /* just a default */
836 ifp->if_broadcastaddr = etherbroadcastaddr;
839 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
840 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
841 sdl->sdl_type = IFT_ETHER;
842 sdl->sdl_alen = ifp->if_addrlen;
843 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
845 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
846 if (ng_ether_attach_p != NULL)
847 (*ng_ether_attach_p)(ifp);
849 /* Announce Ethernet MAC address if non-zero. */
850 for (i = 0; i < ifp->if_addrlen; i++)
853 if (i != ifp->if_addrlen)
854 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
856 uuid_ether_add(LLADDR(sdl));
860 * Perform common duties while detaching an Ethernet interface
863 ether_ifdetach(struct ifnet *ifp)
865 struct sockaddr_dl *sdl;
867 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
868 uuid_ether_del(LLADDR(sdl));
870 if (IFP2AC(ifp)->ac_netgraph != NULL) {
871 KASSERT(ng_ether_detach_p != NULL,
872 ("ng_ether_detach_p is NULL"));
873 (*ng_ether_detach_p)(ifp);
882 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
885 if (IFP2AC(ifp)->ac_netgraph != NULL) {
886 KASSERT(ng_ether_detach_p != NULL,
887 ("ng_ether_detach_p is NULL"));
888 (*ng_ether_detach_p)(ifp);
891 if (ng_ether_attach_p != NULL) {
892 CURVNET_SET_QUIET(new_vnet);
893 (*ng_ether_attach_p)(ifp);
899 SYSCTL_DECL(_net_link);
900 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
904 * This is for reference. We have a table-driven version
905 * of the little-endian crc32 generator, which is faster
906 * than the double-loop.
909 ether_crc32_le(const uint8_t *buf, size_t len)
916 crc = 0xffffffff; /* initial value */
918 for (i = 0; i < len; i++) {
919 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
920 carry = (crc ^ data) & 1;
923 crc = (crc ^ ETHER_CRC_POLY_LE);
931 ether_crc32_le(const uint8_t *buf, size_t len)
933 static const uint32_t crctab[] = {
934 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
935 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
936 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
937 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
942 crc = 0xffffffff; /* initial value */
944 for (i = 0; i < len; i++) {
946 crc = (crc >> 4) ^ crctab[crc & 0xf];
947 crc = (crc >> 4) ^ crctab[crc & 0xf];
955 ether_crc32_be(const uint8_t *buf, size_t len)
962 crc = 0xffffffff; /* initial value */
964 for (i = 0; i < len; i++) {
965 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
966 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
969 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
977 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
979 struct ifaddr *ifa = (struct ifaddr *) data;
980 struct ifreq *ifr = (struct ifreq *) data;
985 ifp->if_flags |= IFF_UP;
987 switch (ifa->ifa_addr->sa_family) {
990 ifp->if_init(ifp->if_softc); /* before arpwhohas */
991 arp_ifinit(ifp, ifa);
995 ifp->if_init(ifp->if_softc);
1002 struct sockaddr *sa;
1004 sa = (struct sockaddr *) & ifr->ifr_data;
1005 bcopy(IF_LLADDR(ifp),
1006 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1012 * Set the interface MTU.
1014 if (ifr->ifr_mtu > ETHERMTU) {
1017 ifp->if_mtu = ifr->ifr_mtu;
1021 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1028 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1029 struct sockaddr *sa)
1031 struct sockaddr_dl *sdl;
1033 struct sockaddr_in *sin;
1036 struct sockaddr_in6 *sin6;
1040 switch(sa->sa_family) {
1043 * No mapping needed. Just check that it's a valid MC address.
1045 sdl = (struct sockaddr_dl *)sa;
1046 e_addr = LLADDR(sdl);
1047 if (!ETHER_IS_MULTICAST(e_addr))
1048 return EADDRNOTAVAIL;
1054 sin = (struct sockaddr_in *)sa;
1055 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1056 return EADDRNOTAVAIL;
1057 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1058 sdl->sdl_alen = ETHER_ADDR_LEN;
1059 e_addr = LLADDR(sdl);
1060 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1061 *llsa = (struct sockaddr *)sdl;
1066 sin6 = (struct sockaddr_in6 *)sa;
1067 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1069 * An IP6 address of 0 means listen to all
1070 * of the Ethernet multicast address used for IP6.
1071 * (This is used for multicast routers.)
1073 ifp->if_flags |= IFF_ALLMULTI;
1077 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1078 return EADDRNOTAVAIL;
1079 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1080 sdl->sdl_alen = ETHER_ADDR_LEN;
1081 e_addr = LLADDR(sdl);
1082 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1083 *llsa = (struct sockaddr *)sdl;
1089 * Well, the text isn't quite right, but it's the name
1092 return EAFNOSUPPORT;
1097 ether_alloc(u_char type, struct ifnet *ifp)
1101 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1108 ether_free(void *com, u_char type)
1111 free(com, M_ARPCOM);
1115 ether_modevent(module_t mod, int type, void *data)
1120 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1123 if_deregister_com_alloc(IFT_ETHER);
1132 static moduledata_t ether_mod = {
1139 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1141 struct ether_vlan_header vlan;
1144 KASSERT((m->m_flags & M_VLANTAG) != 0,
1145 ("%s: vlan information not present", __func__));
1146 KASSERT(m->m_len >= sizeof(struct ether_header),
1147 ("%s: mbuf not large enough for header", __func__));
1148 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1149 vlan.evl_proto = vlan.evl_encap_proto;
1150 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1151 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1152 m->m_len -= sizeof(struct ether_header);
1153 m->m_data += sizeof(struct ether_header);
1155 * If a data link has been supplied by the caller, then we will need to
1156 * re-create a stack allocated mbuf chain with the following structure:
1158 * (1) mbuf #1 will contain the supplied data link
1159 * (2) mbuf #2 will contain the vlan header
1160 * (3) mbuf #3 will contain the original mbuf's packet data
1162 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1166 mv.m_data = (caddr_t)&vlan;
1167 mv.m_len = sizeof(vlan);
1173 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1174 m->m_len += sizeof(struct ether_header);
1175 m->m_data -= sizeof(struct ether_header);
1179 ether_vlanencap(struct mbuf *m, uint16_t tag)
1181 struct ether_vlan_header *evl;
1183 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1186 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1188 if (m->m_len < sizeof(*evl)) {
1189 m = m_pullup(m, sizeof(*evl));
1195 * Transform the Ethernet header into an Ethernet header
1196 * with 802.1Q encapsulation.
1198 evl = mtod(m, struct ether_vlan_header *);
1199 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1200 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1201 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1202 evl->evl_tag = htons(tag);
1206 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1207 MODULE_VERSION(ether, 1);