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"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
45 #include <sys/random.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
60 #include <net/ethernet.h>
61 #include <net/if_bridgevar.h>
62 #include <net/if_vlan_var.h>
63 #include <net/if_llatbl.h>
67 #include <netpfil/pf/pf_mtag.h>
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
73 #include <netinet/ip_carp.h>
74 #include <netinet/ip_var.h>
77 #include <netinet6/nd6.h>
80 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
81 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
82 const struct sockaddr *dst, short *tp, int *hlen);
84 #include <security/mac/mac_framework.h>
87 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
88 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
91 VNET_DEFINE(struct pfil_head, link_pfil_hook); /* Packet filter hooks */
93 /* netgraph node hooks for ng_ether(4) */
94 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
95 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
96 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
97 void (*ng_ether_attach_p)(struct ifnet *ifp);
98 void (*ng_ether_detach_p)(struct ifnet *ifp);
100 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
102 /* if_bridge(4) support */
103 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
104 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
105 struct sockaddr *, struct rtentry *);
106 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
108 /* if_lagg(4) support */
109 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
111 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
112 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
114 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
117 static void ether_reassign(struct ifnet *, struct vnet *, char *);
120 /* XXX: should be in an arp support file, not here */
121 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
123 #define ETHER_IS_BROADCAST(addr) \
124 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
126 #define senderr(e) do { error = (e); goto bad;} while (0)
129 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
133 if (src->m_pkthdr.csum_flags & CSUM_IP)
134 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
135 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
136 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
137 if (src->m_pkthdr.csum_flags & CSUM_SCTP)
138 csum_flags |= CSUM_SCTP_VALID;
139 dst->m_pkthdr.csum_flags |= csum_flags;
140 if (csum_flags & CSUM_DATA_VALID)
141 dst->m_pkthdr.csum_data = 0xffff;
145 * Ethernet output routine.
146 * Encapsulate a packet of type family for the local net.
147 * Use trailer local net encapsulation if enough data in first
148 * packet leaves a multiple of 512 bytes of data in remainder.
151 ether_output(struct ifnet *ifp, struct mbuf *m,
152 const struct sockaddr *dst, struct route *ro)
155 int error = 0, hdrcmplt = 0;
156 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
157 struct llentry *lle = NULL;
158 struct rtentry *rt0 = NULL;
159 struct ether_header *eh;
162 int hlen; /* link layer header length */
165 if (!(m->m_flags & (M_BCAST | M_MCAST)))
170 error = mac_ifnet_check_transmit(ifp, m);
176 if (ifp->if_flags & IFF_MONITOR)
178 if (!((ifp->if_flags & IFF_UP) &&
179 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
182 hlen = ETHER_HDR_LEN;
183 switch (dst->sa_family) {
186 if (lle != NULL && (lle->la_flags & LLE_VALID))
187 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
189 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
191 return (error == EWOULDBLOCK ? 0 : error);
192 type = htons(ETHERTYPE_IP);
197 ah = mtod(m, struct arphdr *);
198 ah->ar_hrd = htons(ARPHRD_ETHER);
200 loop_copy = 0; /* if this is for us, don't do it */
202 switch(ntohs(ah->ar_op)) {
203 case ARPOP_REVREQUEST:
205 type = htons(ETHERTYPE_REVARP);
210 type = htons(ETHERTYPE_ARP);
214 if (m->m_flags & M_BCAST)
215 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
217 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
224 if (lle != NULL && (lle->la_flags & LLE_VALID))
225 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
227 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
230 type = htons(ETHERTYPE_IPV6);
233 case pseudo_AF_HDRCMPLT:
235 const struct ether_header *eh;
238 eh = (const struct ether_header *)dst->sa_data;
239 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
243 loop_copy = 0; /* if this is for us, don't do it */
244 eh = (const struct ether_header *)dst->sa_data;
245 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
246 type = eh->ether_type;
250 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
251 senderr(EAFNOSUPPORT);
254 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
255 update_mbuf_csumflags(m, m);
256 return (if_simloop(ifp, m, dst->sa_family, 0));
260 * Add local net header. If no space in first mbuf,
263 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
266 eh = mtod(m, struct ether_header *);
267 (void)memcpy(&eh->ether_type, &type,
268 sizeof(eh->ether_type));
269 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
271 (void)memcpy(eh->ether_shost, esrc,
272 sizeof(eh->ether_shost));
274 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
275 sizeof(eh->ether_shost));
278 * If a simplex interface, and the packet is being sent to our
279 * Ethernet address or a broadcast address, loopback a copy.
280 * XXX To make a simplex device behave exactly like a duplex
281 * device, we should copy in the case of sending to our own
282 * ethernet address (thus letting the original actually appear
283 * on the wire). However, we don't do that here for security
284 * reasons and compatibility with the original behavior.
286 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
287 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
288 if (m->m_flags & M_BCAST) {
292 * Because if_simloop() modifies the packet, we need a
293 * writable copy through m_dup() instead of a readonly
294 * one as m_copy[m] would give us. The alternative would
295 * be to modify if_simloop() to handle the readonly mbuf,
296 * but performancewise it is mostly equivalent (trading
297 * extra data copying vs. extra locking).
299 * XXX This is a local workaround. A number of less
300 * often used kernel parts suffer from the same bug.
301 * See PR kern/105943 for a proposed general solution.
303 if ((n = m_dup(m, M_NOWAIT)) != NULL) {
304 update_mbuf_csumflags(m, n);
305 (void)if_simloop(ifp, n, dst->sa_family, hlen);
308 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
309 ETHER_ADDR_LEN) == 0) {
310 update_mbuf_csumflags(m, m);
311 (void) if_simloop(ifp, m, dst->sa_family, hlen);
312 return (0); /* XXX */
317 * Bridges require special output handling.
319 if (ifp->if_bridge) {
320 BRIDGE_OUTPUT(ifp, m, error);
324 #if defined(INET) || defined(INET6)
326 (error = (*carp_output_p)(ifp, m, dst)))
330 /* Handle ng_ether(4) processing, if any */
331 if (IFP2AC(ifp)->ac_netgraph != NULL) {
332 KASSERT(ng_ether_output_p != NULL,
333 ("ng_ether_output_p is NULL"));
334 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
343 /* Continue with link-layer output */
344 return ether_output_frame(ifp, m);
348 * Ethernet link layer output routine to send a raw frame to the device.
350 * This assumes that the 14 byte Ethernet header is present and contiguous
351 * in the first mbuf (if BRIDGE'ing).
354 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
358 if (PFIL_HOOKED(&V_link_pfil_hook)) {
359 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
369 * Queue message on interface, update output statistics if
370 * successful, and start output if interface not yet active.
372 return ((ifp->if_transmit)(ifp, m));
375 #if defined(INET) || defined(INET6)
379 * Process a received Ethernet packet; the packet is in the
380 * mbuf chain m with the ethernet header at the front.
383 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
385 struct ether_header *eh;
388 if ((ifp->if_flags & IFF_UP) == 0) {
393 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
394 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
400 * Do consistency checks to verify assumptions
401 * made by code past this point.
403 if ((m->m_flags & M_PKTHDR) == 0) {
404 if_printf(ifp, "discard frame w/o packet header\n");
409 if (m->m_len < ETHER_HDR_LEN) {
410 /* XXX maybe should pullup? */
411 if_printf(ifp, "discard frame w/o leading ethernet "
412 "header (len %u pkt len %u)\n",
413 m->m_len, m->m_pkthdr.len);
418 eh = mtod(m, struct ether_header *);
419 etype = ntohs(eh->ether_type);
420 if (m->m_pkthdr.rcvif == NULL) {
421 if_printf(ifp, "discard frame w/o interface pointer\n");
427 if (m->m_pkthdr.rcvif != ifp) {
428 if_printf(ifp, "Warning, frame marked as received on %s\n",
429 m->m_pkthdr.rcvif->if_xname);
433 CURVNET_SET_QUIET(ifp->if_vnet);
435 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
436 if (ETHER_IS_BROADCAST(eh->ether_dhost))
437 m->m_flags |= M_BCAST;
439 m->m_flags |= M_MCAST;
445 * Tag the mbuf with an appropriate MAC label before any other
446 * consumers can get to it.
448 mac_ifnet_create_mbuf(ifp, m);
452 * Give bpf a chance at the packet.
454 ETHER_BPF_MTAP(ifp, m);
457 * If the CRC is still on the packet, trim it off. We do this once
458 * and once only in case we are re-entered. Nothing else on the
459 * Ethernet receive path expects to see the FCS.
461 if (m->m_flags & M_HASFCS) {
462 m_adj(m, -ETHER_CRC_LEN);
463 m->m_flags &= ~M_HASFCS;
466 if (!(ifp->if_capenable & IFCAP_HWSTATS))
467 ifp->if_ibytes += m->m_pkthdr.len;
469 /* Allow monitor mode to claim this frame, after stats are updated. */
470 if (ifp->if_flags & IFF_MONITOR) {
476 /* Handle input from a lagg(4) port */
477 if (ifp->if_type == IFT_IEEE8023ADLAG) {
478 KASSERT(lagg_input_p != NULL,
479 ("%s: if_lagg not loaded!", __func__));
480 m = (*lagg_input_p)(ifp, m);
482 ifp = m->m_pkthdr.rcvif;
490 * If the hardware did not process an 802.1Q tag, do this now,
491 * to allow 802.1P priority frames to be passed to the main input
493 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
495 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
496 struct ether_vlan_header *evl;
498 if (m->m_len < sizeof(*evl) &&
499 (m = m_pullup(m, sizeof(*evl))) == NULL) {
501 if_printf(ifp, "cannot pullup VLAN header\n");
509 evl = mtod(m, struct ether_vlan_header *);
510 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
511 m->m_flags |= M_VLANTAG;
513 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
514 ETHER_HDR_LEN - ETHER_TYPE_LEN);
515 m_adj(m, ETHER_VLAN_ENCAP_LEN);
516 eh = mtod(m, struct ether_header *);
519 M_SETFIB(m, ifp->if_fib);
521 /* Allow ng_ether(4) to claim this frame. */
522 if (IFP2AC(ifp)->ac_netgraph != NULL) {
523 KASSERT(ng_ether_input_p != NULL,
524 ("%s: ng_ether_input_p is NULL", __func__));
525 m->m_flags &= ~M_PROMISC;
526 (*ng_ether_input_p)(ifp, &m);
531 eh = mtod(m, struct ether_header *);
535 * Allow if_bridge(4) to claim this frame.
536 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
537 * and the frame should be delivered locally.
539 if (ifp->if_bridge != NULL) {
540 m->m_flags &= ~M_PROMISC;
541 BRIDGE_INPUT(ifp, m);
546 eh = mtod(m, struct ether_header *);
549 #if defined(INET) || defined(INET6)
551 * Clear M_PROMISC on frame so that carp(4) will see it when the
552 * mbuf flows up to Layer 3.
553 * FreeBSD's implementation of carp(4) uses the inprotosw
554 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
555 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
556 * is outside the scope of the M_PROMISC test below.
557 * TODO: Maintain a hash table of ethernet addresses other than
558 * ether_dhost which may be active on this ifp.
560 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
561 m->m_flags &= ~M_PROMISC;
566 * If the frame received was not for our MAC address, set the
567 * M_PROMISC flag on the mbuf chain. The frame may need to
568 * be seen by the rest of the Ethernet input path in case of
569 * re-entry (e.g. bridge, vlan, netgraph) but should not be
570 * seen by upper protocol layers.
572 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
573 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
574 m->m_flags |= M_PROMISC;
577 if (harvest.ethernet)
578 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
585 * Ethernet input dispatch; by default, direct dispatch here regardless of
586 * global configuration.
589 ether_nh_input(struct mbuf *m)
592 ether_input_internal(m->m_pkthdr.rcvif, m);
595 static struct netisr_handler ether_nh = {
597 .nh_handler = ether_nh_input,
598 .nh_proto = NETISR_ETHER,
599 .nh_policy = NETISR_POLICY_SOURCE,
600 .nh_dispatch = NETISR_DISPATCH_DIRECT,
604 ether_init(__unused void *arg)
607 netisr_register(ðer_nh);
609 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
612 vnet_ether_init(__unused void *arg)
616 /* Initialize packet filter hooks. */
617 V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
618 V_link_pfil_hook.ph_af = AF_LINK;
619 if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
620 printf("%s: WARNING: unable to register pfil link hook, "
621 "error %d\n", __func__, i);
623 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
624 vnet_ether_init, NULL);
627 vnet_ether_destroy(__unused void *arg)
631 if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
632 printf("%s: WARNING: unable to unregister pfil link hook, "
633 "error %d\n", __func__, i);
635 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
636 vnet_ether_destroy, NULL);
641 ether_input(struct ifnet *ifp, struct mbuf *m)
647 * The drivers are allowed to pass in a chain of packets linked with
648 * m_nextpkt. We split them up into separate packets here and pass
649 * them up. This allows the drivers to amortize the receive lock.
656 * We will rely on rcvif being set properly in the deferred context,
657 * so assert it is correct here.
659 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
660 netisr_dispatch(NETISR_ETHER, m);
666 * Upper layer processing for a received Ethernet packet.
669 ether_demux(struct ifnet *ifp, struct mbuf *m)
671 struct ether_header *eh;
675 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
677 /* Do not grab PROMISC frames in case we are re-entered. */
678 if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
679 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
681 if (i != 0 || m == NULL)
685 eh = mtod(m, struct ether_header *);
686 ether_type = ntohs(eh->ether_type);
689 * If this frame has a VLAN tag other than 0, call vlan_input()
690 * if its module is loaded. Otherwise, drop.
692 if ((m->m_flags & M_VLANTAG) &&
693 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
694 if (ifp->if_vlantrunk == NULL) {
699 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
701 /* Clear before possibly re-entering ether_input(). */
702 m->m_flags &= ~M_PROMISC;
703 (*vlan_input_p)(ifp, m);
708 * Pass promiscuously received frames to the upper layer if the user
709 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
711 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
717 * Reset layer specific mbuf flags to avoid confusing upper layers.
718 * Strip off Ethernet header.
720 m->m_flags &= ~M_VLANTAG;
722 m_adj(m, ETHER_HDR_LEN);
725 * Dispatch frame to upper layer.
727 switch (ether_type) {
730 if ((m = ip_fastforward(m)) == NULL)
736 if (ifp->if_flags & IFF_NOARP) {
737 /* Discard packet if ARP is disabled on interface */
752 netisr_dispatch(isr, m);
757 * Packet is to be discarded. If netgraph is present,
758 * hand the packet to it for last chance processing;
759 * otherwise dispose of it.
761 if (IFP2AC(ifp)->ac_netgraph != NULL) {
762 KASSERT(ng_ether_input_orphan_p != NULL,
763 ("ng_ether_input_orphan_p is NULL"));
765 * Put back the ethernet header so netgraph has a
766 * consistent view of inbound packets.
768 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
769 (*ng_ether_input_orphan_p)(ifp, m);
776 * Convert Ethernet address to printable (loggable) representation.
777 * This routine is for compatibility; it's better to just use
779 * printf("%6D", <pointer to address>, ":");
781 * since there's no static buffer involved.
784 ether_sprintf(const u_char *ap)
786 static char etherbuf[18];
787 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
792 * Perform common duties while attaching to interface list
795 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
799 struct sockaddr_dl *sdl;
801 ifp->if_addrlen = ETHER_ADDR_LEN;
802 ifp->if_hdrlen = ETHER_HDR_LEN;
804 ifp->if_mtu = ETHERMTU;
805 ifp->if_output = ether_output;
806 ifp->if_input = ether_input;
807 ifp->if_resolvemulti = ether_resolvemulti;
809 ifp->if_reassign = ether_reassign;
811 if (ifp->if_baudrate == 0)
812 ifp->if_baudrate = IF_Mbps(10); /* just a default */
813 ifp->if_broadcastaddr = etherbroadcastaddr;
816 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
817 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
818 sdl->sdl_type = IFT_ETHER;
819 sdl->sdl_alen = ifp->if_addrlen;
820 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
822 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
823 if (ng_ether_attach_p != NULL)
824 (*ng_ether_attach_p)(ifp);
826 /* Announce Ethernet MAC address if non-zero. */
827 for (i = 0; i < ifp->if_addrlen; i++)
830 if (i != ifp->if_addrlen)
831 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
833 uuid_ether_add(LLADDR(sdl));
837 * Perform common duties while detaching an Ethernet interface
840 ether_ifdetach(struct ifnet *ifp)
842 struct sockaddr_dl *sdl;
844 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
845 uuid_ether_del(LLADDR(sdl));
847 if (IFP2AC(ifp)->ac_netgraph != NULL) {
848 KASSERT(ng_ether_detach_p != NULL,
849 ("ng_ether_detach_p is NULL"));
850 (*ng_ether_detach_p)(ifp);
859 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
862 if (IFP2AC(ifp)->ac_netgraph != NULL) {
863 KASSERT(ng_ether_detach_p != NULL,
864 ("ng_ether_detach_p is NULL"));
865 (*ng_ether_detach_p)(ifp);
868 if (ng_ether_attach_p != NULL) {
869 CURVNET_SET_QUIET(new_vnet);
870 (*ng_ether_attach_p)(ifp);
876 SYSCTL_DECL(_net_link);
877 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
881 * This is for reference. We have a table-driven version
882 * of the little-endian crc32 generator, which is faster
883 * than the double-loop.
886 ether_crc32_le(const uint8_t *buf, size_t len)
893 crc = 0xffffffff; /* initial value */
895 for (i = 0; i < len; i++) {
896 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
897 carry = (crc ^ data) & 1;
900 crc = (crc ^ ETHER_CRC_POLY_LE);
908 ether_crc32_le(const uint8_t *buf, size_t len)
910 static const uint32_t crctab[] = {
911 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
912 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
913 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
914 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
919 crc = 0xffffffff; /* initial value */
921 for (i = 0; i < len; i++) {
923 crc = (crc >> 4) ^ crctab[crc & 0xf];
924 crc = (crc >> 4) ^ crctab[crc & 0xf];
932 ether_crc32_be(const uint8_t *buf, size_t len)
939 crc = 0xffffffff; /* initial value */
941 for (i = 0; i < len; i++) {
942 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
943 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
946 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
954 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
956 struct ifaddr *ifa = (struct ifaddr *) data;
957 struct ifreq *ifr = (struct ifreq *) data;
962 ifp->if_flags |= IFF_UP;
964 switch (ifa->ifa_addr->sa_family) {
967 ifp->if_init(ifp->if_softc); /* before arpwhohas */
968 arp_ifinit(ifp, ifa);
972 ifp->if_init(ifp->if_softc);
981 sa = (struct sockaddr *) & ifr->ifr_data;
982 bcopy(IF_LLADDR(ifp),
983 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
989 * Set the interface MTU.
991 if (ifr->ifr_mtu > ETHERMTU) {
994 ifp->if_mtu = ifr->ifr_mtu;
998 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1005 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1006 struct sockaddr *sa)
1008 struct sockaddr_dl *sdl;
1010 struct sockaddr_in *sin;
1013 struct sockaddr_in6 *sin6;
1017 switch(sa->sa_family) {
1020 * No mapping needed. Just check that it's a valid MC address.
1022 sdl = (struct sockaddr_dl *)sa;
1023 e_addr = LLADDR(sdl);
1024 if (!ETHER_IS_MULTICAST(e_addr))
1025 return EADDRNOTAVAIL;
1031 sin = (struct sockaddr_in *)sa;
1032 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1033 return EADDRNOTAVAIL;
1034 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1035 sdl->sdl_alen = ETHER_ADDR_LEN;
1036 e_addr = LLADDR(sdl);
1037 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1038 *llsa = (struct sockaddr *)sdl;
1043 sin6 = (struct sockaddr_in6 *)sa;
1044 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1046 * An IP6 address of 0 means listen to all
1047 * of the Ethernet multicast address used for IP6.
1048 * (This is used for multicast routers.)
1050 ifp->if_flags |= IFF_ALLMULTI;
1054 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1055 return EADDRNOTAVAIL;
1056 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1057 sdl->sdl_alen = ETHER_ADDR_LEN;
1058 e_addr = LLADDR(sdl);
1059 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1060 *llsa = (struct sockaddr *)sdl;
1066 * Well, the text isn't quite right, but it's the name
1069 return EAFNOSUPPORT;
1074 ether_alloc(u_char type, struct ifnet *ifp)
1078 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1085 ether_free(void *com, u_char type)
1088 free(com, M_ARPCOM);
1092 ether_modevent(module_t mod, int type, void *data)
1097 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1100 if_deregister_com_alloc(IFT_ETHER);
1109 static moduledata_t ether_mod = {
1116 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1118 struct ether_vlan_header vlan;
1121 KASSERT((m->m_flags & M_VLANTAG) != 0,
1122 ("%s: vlan information not present", __func__));
1123 KASSERT(m->m_len >= sizeof(struct ether_header),
1124 ("%s: mbuf not large enough for header", __func__));
1125 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1126 vlan.evl_proto = vlan.evl_encap_proto;
1127 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1128 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1129 m->m_len -= sizeof(struct ether_header);
1130 m->m_data += sizeof(struct ether_header);
1132 * If a data link has been supplied by the caller, then we will need to
1133 * re-create a stack allocated mbuf chain with the following structure:
1135 * (1) mbuf #1 will contain the supplied data link
1136 * (2) mbuf #2 will contain the vlan header
1137 * (3) mbuf #3 will contain the original mbuf's packet data
1139 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1143 mv.m_data = (caddr_t)&vlan;
1144 mv.m_len = sizeof(vlan);
1150 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1151 m->m_len += sizeof(struct ether_header);
1152 m->m_data -= sizeof(struct ether_header);
1156 ether_vlanencap(struct mbuf *m, uint16_t tag)
1158 struct ether_vlan_header *evl;
1160 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1163 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1165 if (m->m_len < sizeof(*evl)) {
1166 m = m_pullup(m, sizeof(*evl));
1172 * Transform the Ethernet header into an Ethernet header
1173 * with 802.1Q encapsulation.
1175 evl = mtod(m, struct ether_vlan_header *);
1176 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1177 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1178 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1179 evl->evl_tag = htons(tag);
1183 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1184 MODULE_VERSION(ether, 1);