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"
38 #include "opt_netgraph.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #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>
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>
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip_fw.h>
69 #include <netinet/ip_dummynet.h>
72 #include <netinet6/nd6.h>
76 #include <netinet/ip_carp.h>
80 #include <netipx/ipx.h>
81 #include <netipx/ipx_if.h>
83 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
84 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
85 struct sockaddr *dst, short *tp, int *hlen);
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
92 #define llc_snap_org_code llc_un.type_snap.org_code
93 #define llc_snap_ether_type llc_un.type_snap.ether_type
95 extern u_char at_org_code[3];
96 extern u_char aarp_org_code[3];
99 #include <security/mac/mac_framework.h>
101 /* netgraph node hooks for ng_ether(4) */
102 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105 void (*ng_ether_attach_p)(struct ifnet *ifp);
106 void (*ng_ether_detach_p)(struct ifnet *ifp);
108 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
110 /* if_bridge(4) support */
111 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
112 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
113 struct sockaddr *, struct rtentry *);
114 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
116 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
117 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
119 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
122 /* XXX: should be in an arp support file, not here */
123 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
125 #define senderr(e) do { error = (e); goto bad;} while (0)
127 #if defined(INET) || defined(INET6)
129 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
130 struct ip_fw **rule, int shared);
131 static int ether_ipfw;
135 * Ethernet output routine.
136 * Encapsulate a packet of type family for the local net.
137 * Use trailer local net encapsulation if enough data in first
138 * packet leaves a multiple of 512 bytes of data in remainder.
141 ether_output(struct ifnet *ifp, struct mbuf *m,
142 struct sockaddr *dst, struct rtentry *rt0)
145 int error, hdrcmplt = 0;
146 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
147 struct ether_header *eh;
149 int hlen; /* link layer header length */
152 error = mac_check_ifnet_transmit(ifp, m);
157 if (ifp->if_flags & IFF_MONITOR)
159 if (!((ifp->if_flags & IFF_UP) &&
160 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
163 hlen = ETHER_HDR_LEN;
164 switch (dst->sa_family) {
167 error = arpresolve(ifp, rt0, m, dst, edst);
169 return (error == EWOULDBLOCK ? 0 : error);
170 type = htons(ETHERTYPE_IP);
175 ah = mtod(m, struct arphdr *);
176 ah->ar_hrd = htons(ARPHRD_ETHER);
178 loop_copy = 0; /* if this is for us, don't do it */
180 switch(ntohs(ah->ar_op)) {
181 case ARPOP_REVREQUEST:
183 type = htons(ETHERTYPE_REVARP);
188 type = htons(ETHERTYPE_ARP);
192 if (m->m_flags & M_BCAST)
193 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
195 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
202 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
205 type = htons(ETHERTYPE_IPV6);
211 error = ef_outputp(ifp, &m, dst, &type, &hlen);
215 type = htons(ETHERTYPE_IPX);
216 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
217 (caddr_t)edst, sizeof (edst));
223 struct at_ifaddr *aa;
225 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
226 senderr(EHOSTUNREACH); /* XXX */
227 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
230 * In the phase 2 case, need to prepend an mbuf for the llc header.
232 if ( aa->aa_flags & AFA_PHASE2 ) {
235 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
238 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
239 llc.llc_control = LLC_UI;
240 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
241 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
242 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
243 type = htons(m->m_pkthdr.len);
244 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
246 type = htons(ETHERTYPE_AT);
250 #endif /* NETATALK */
252 case pseudo_AF_HDRCMPLT:
254 eh = (struct ether_header *)dst->sa_data;
255 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259 loop_copy = 0; /* if this is for us, don't do it */
260 eh = (struct ether_header *)dst->sa_data;
261 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
262 type = eh->ether_type;
266 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
267 senderr(EAFNOSUPPORT);
271 * Add local net header. If no space in first mbuf,
274 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
277 eh = mtod(m, struct ether_header *);
278 (void)memcpy(&eh->ether_type, &type,
279 sizeof(eh->ether_type));
280 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
282 (void)memcpy(eh->ether_shost, esrc,
283 sizeof(eh->ether_shost));
285 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
286 sizeof(eh->ether_shost));
289 * If a simplex interface, and the packet is being sent to our
290 * Ethernet address or a broadcast address, loopback a copy.
291 * XXX To make a simplex device behave exactly like a duplex
292 * device, we should copy in the case of sending to our own
293 * ethernet address (thus letting the original actually appear
294 * on the wire). However, we don't do that here for security
295 * reasons and compatibility with the original behavior.
297 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
298 m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
301 if (m->m_pkthdr.csum_flags & CSUM_IP)
302 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
303 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
304 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
306 if (m->m_flags & M_BCAST) {
310 * Because if_simloop() modifies the packet, we need a
311 * writable copy through m_dup() instead of a readonly
312 * one as m_copy[m] would give us. The alternative would
313 * be to modify if_simloop() to handle the readonly mbuf,
314 * but performancewise it is mostly equivalent (trading
315 * extra data copying vs. extra locking).
317 * XXX This is a local workaround. A number of less
318 * often used kernel parts suffer from the same bug.
319 * See PR kern/105943 for a proposed general solution.
321 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
322 n->m_pkthdr.csum_flags |= csum_flags;
323 if (csum_flags & CSUM_DATA_VALID)
324 n->m_pkthdr.csum_data = 0xffff;
325 (void)if_simloop(ifp, n, dst->sa_family, hlen);
328 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
329 ETHER_ADDR_LEN) == 0) {
330 m->m_pkthdr.csum_flags |= csum_flags;
331 if (csum_flags & CSUM_DATA_VALID)
332 m->m_pkthdr.csum_data = 0xffff;
333 (void) if_simloop(ifp, m, dst->sa_family, hlen);
334 return (0); /* XXX */
339 * Bridges require special output handling.
341 if (ifp->if_bridge) {
342 BRIDGE_OUTPUT(ifp, m, error);
348 (error = carp_output(ifp, m, dst, NULL)))
352 /* Handle ng_ether(4) processing, if any */
353 if (IFP2AC(ifp)->ac_netgraph != NULL) {
354 KASSERT(ng_ether_output_p != NULL,
355 ("ng_ether_output_p is NULL"));
356 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
365 /* Continue with link-layer output */
366 return ether_output_frame(ifp, m);
370 * Ethernet link layer output routine to send a raw frame to the device.
372 * This assumes that the 14 byte Ethernet header is present and contiguous
373 * in the first mbuf (if BRIDGE'ing).
376 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
379 #if defined(INET) || defined(INET6)
380 struct ip_fw *rule = ip_dn_claim_rule(m);
382 if (IPFW_LOADED && ether_ipfw != 0) {
383 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
386 return EACCES; /* pkt dropped */
388 return 0; /* consumed e.g. in a pipe */
394 * Queue message on interface, update output statistics if
395 * successful, and start output if interface not yet active.
397 IFQ_HANDOFF(ifp, m, error);
401 #if defined(INET) || defined(INET6)
403 * ipfw processing for ethernet packets (in and out).
404 * The second parameter is NULL from ether_demux, and ifp from
405 * ether_output_frame.
408 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
409 struct ip_fw **rule, int shared)
411 struct ether_header *eh;
412 struct ether_header save_eh;
415 struct ip_fw_args args;
417 if (*rule != NULL && fw_one_pass)
418 return 1; /* dummynet packet, already partially processed */
421 * I need some amt of data to be contiguous, and in case others need
422 * the packet (shared==1) also better be in the first mbuf.
425 i = min( m->m_pkthdr.len, max_protohdr);
426 if ( shared || m->m_len < i) {
433 eh = mtod(m, struct ether_header *);
434 save_eh = *eh; /* save copy for restore below */
435 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
437 args.m = m; /* the packet we are looking at */
438 args.oif = dst; /* destination, if any */
439 args.rule = *rule; /* matching rule to restart */
440 args.next_hop = NULL; /* we do not support forward yet */
441 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
442 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
443 i = ip_fw_chk_ptr(&args);
447 * Restore Ethernet header, as needed, in case the
448 * mbuf chain was replaced by ipfw.
450 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
455 if (eh != mtod(m, struct ether_header *))
456 bcopy(&save_eh, mtod(m, struct ether_header *),
462 if (i == IP_FW_DENY) /* drop */
465 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
467 if (i == IP_FW_PASS) /* a PASS rule. */
470 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
472 * Pass the pkt to dummynet, which consumes it.
473 * If shared, make a copy and keep the original.
476 m = m_copypacket(m, M_DONTWAIT);
481 * Pass the original to dummynet and
482 * nothing back to the caller
486 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
490 * XXX at some point add support for divert/forward actions.
491 * If none of the above matches, we have to drop the pkt.
498 * Process a received Ethernet packet; the packet is in the
499 * mbuf chain m with the ethernet header at the front.
502 ether_input(struct ifnet *ifp, struct mbuf *m)
504 struct ether_header *eh;
508 * Do consistency checks to verify assumptions
509 * made by code past this point.
511 if ((m->m_flags & M_PKTHDR) == 0) {
512 if_printf(ifp, "discard frame w/o packet header\n");
517 if (m->m_len < ETHER_HDR_LEN) {
518 /* XXX maybe should pullup? */
519 if_printf(ifp, "discard frame w/o leading ethernet "
520 "header (len %u pkt len %u)\n",
521 m->m_len, m->m_pkthdr.len);
526 eh = mtod(m, struct ether_header *);
527 etype = ntohs(eh->ether_type);
528 if (m->m_pkthdr.len >
529 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
530 if_printf(ifp, "discard oversize frame "
531 "(ether type %x flags %x len %u > max %lu)\n",
532 etype, m->m_flags, m->m_pkthdr.len,
533 ETHER_MAX_FRAME(ifp, etype,
534 m->m_flags & M_HASFCS));
539 if (m->m_pkthdr.rcvif == NULL) {
540 if_printf(ifp, "discard frame w/o interface pointer\n");
546 if (m->m_pkthdr.rcvif != ifp) {
547 if_printf(ifp, "Warning, frame marked as received on %s\n",
548 m->m_pkthdr.rcvif->if_xname);
554 * Tag the mbuf with an appropriate MAC label before any other
555 * consumers can get to it.
557 mac_create_mbuf_from_ifnet(ifp, m);
561 * Give bpf a chance at the packet.
565 /* If the CRC is still on the packet, trim it off. */
566 if (m->m_flags & M_HASFCS) {
567 m_adj(m, -ETHER_CRC_LEN);
568 m->m_flags &= ~M_HASFCS;
571 ifp->if_ibytes += m->m_pkthdr.len;
573 if (ifp->if_flags & IFF_MONITOR) {
575 * Interface marked for monitoring; discard packet.
581 /* Handle ng_ether(4) processing, if any */
582 if (IFP2AC(ifp)->ac_netgraph != NULL) {
583 KASSERT(ng_ether_input_p != NULL,
584 ("ng_ether_input_p is NULL"));
585 (*ng_ether_input_p)(ifp, &m);
591 * Tap the packet off here for a bridge. bridge_input()
592 * will return NULL if it has consumed the packet, otherwise
593 * it gets processed as normal. Note that bridge_input()
594 * will always return the original packet if we need to
595 * process it locally.
597 * XXX: When changing the below block, please also look
598 * at the src/sys/netgraph/ng_ether.c:ng_ether_rcv_upper()
600 if (ifp->if_bridge) {
601 BRIDGE_INPUT(ifp, m);
606 /* First chunk of an mbuf contains good entropy */
607 if (harvest.ethernet)
608 random_harvest(m, 16, 3, 0, RANDOM_NET);
613 * Upper layer processing for a received Ethernet packet.
616 ether_demux(struct ifnet *ifp, struct mbuf *m)
618 struct ether_header *eh;
621 #if defined(NETATALK)
624 #if defined(INET) || defined(INET6)
625 struct ip_fw *rule = ip_dn_claim_rule(m);
628 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
630 eh = mtod(m, struct ether_header *);
631 ether_type = ntohs(eh->ether_type);
633 #if defined(INET) || defined(INET6)
634 if (rule) /* packet was already bridged */
638 if (!(ifp->if_bridge) &&
639 !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
640 ifp->if_vlantrunk != NULL)) {
643 * XXX: Okay, we need to call carp_forus() and - if it is for
644 * us jump over code that does the normal check
645 * "IF_LLADDR(ifp) == ether_dhost". The check sequence is a bit
646 * different from OpenBSD, so we jump over as few code as
647 * possible, to catch _all_ sanity checks. This needs
648 * evaluation, to see if the carp ether_dhost values break any
651 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
655 * Discard packet if upper layers shouldn't see it because it
656 * was unicast to a different Ethernet address. If the driver
657 * is working properly, then this situation can only happen
658 * when the interface is in promiscuous mode.
660 * If VLANs are active, and this packet has a VLAN tag, do
661 * not drop it here but pass it on to the VLAN layer, to
662 * give them a chance to consider it as well (e. g. in case
663 * bridging is only active on a VLAN). They will drop it if
666 if ((ifp->if_flags & IFF_PROMISC) != 0
667 && !ETHER_IS_MULTICAST(eh->ether_dhost)
668 && bcmp(eh->ether_dhost,
669 IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0
670 && (ifp->if_flags & IFF_PPROMISC) == 0) {
679 /* Discard packet if interface is not up */
680 if ((ifp->if_flags & IFF_UP) == 0) {
684 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
685 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
686 sizeof(etherbroadcastaddr)) == 0)
687 m->m_flags |= M_BCAST;
689 m->m_flags |= M_MCAST;
691 if (m->m_flags & (M_BCAST|M_MCAST))
694 #if defined(INET) || defined(INET6)
696 if (IPFW_LOADED && ether_ipfw != 0) {
697 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
706 * Check to see if the device performed the VLAN decapsulation and
707 * provided us with the tag.
709 if (m->m_flags & M_VLANTAG) {
711 * If no VLANs are configured, drop.
713 if (ifp->if_vlantrunk == NULL) {
719 * vlan_input() will either recursively call ether_input()
720 * or drop the packet.
722 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
723 (*vlan_input_p)(ifp, m);
728 * Handle protocols that expect to have the Ethernet header
729 * (and possibly FCS) intact.
731 switch (ether_type) {
733 if (ifp->if_vlantrunk != NULL) {
734 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
735 (*vlan_input_p)(ifp, m);
743 /* Strip off Ethernet header. */
744 m_adj(m, ETHER_HDR_LEN);
746 /* If the CRC is still on the packet, trim it off. */
747 if (m->m_flags & M_HASFCS) {
748 m_adj(m, -ETHER_CRC_LEN);
749 m->m_flags &= ~M_HASFCS;
752 /* Reset layer specific mbuf flags to avoid confusing upper layers. */
753 m->m_flags &= ~(M_PROTOFLAGS);
755 switch (ether_type) {
758 if ((m = ip_fastforward(m)) == NULL)
764 if (ifp->if_flags & IFF_NOARP) {
765 /* Discard packet if ARP is disabled on interface */
774 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
791 #endif /* NETATALK */
794 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
797 #if defined(NETATALK)
798 if (ether_type > ETHERMTU)
800 l = mtod(m, struct llc *);
801 if (l->llc_dsap == LLC_SNAP_LSAP &&
802 l->llc_ssap == LLC_SNAP_LSAP &&
803 l->llc_control == LLC_UI) {
804 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
805 sizeof(at_org_code)) == 0 &&
806 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
807 m_adj(m, LLC_SNAPFRAMELEN);
811 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
812 sizeof(aarp_org_code)) == 0 &&
813 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
814 m_adj(m, LLC_SNAPFRAMELEN);
819 #endif /* NETATALK */
822 netisr_dispatch(isr, m);
827 * Packet is to be discarded. If netgraph is present,
828 * hand the packet to it for last chance processing;
829 * otherwise dispose of it.
831 if (IFP2AC(ifp)->ac_netgraph != NULL) {
832 KASSERT(ng_ether_input_orphan_p != NULL,
833 ("ng_ether_input_orphan_p is NULL"));
835 * Put back the ethernet header so netgraph has a
836 * consistent view of inbound packets.
838 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
839 (*ng_ether_input_orphan_p)(ifp, m);
846 * Convert Ethernet address to printable (loggable) representation.
847 * This routine is for compatibility; it's better to just use
849 * printf("%6D", <pointer to address>, ":");
851 * since there's no static buffer involved.
854 ether_sprintf(const u_char *ap)
856 static char etherbuf[18];
857 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
862 * Perform common duties while attaching to interface list
865 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
869 struct sockaddr_dl *sdl;
871 ifp->if_addrlen = ETHER_ADDR_LEN;
872 ifp->if_hdrlen = ETHER_HDR_LEN;
874 ifp->if_mtu = ETHERMTU;
875 ifp->if_output = ether_output;
876 ifp->if_input = ether_input;
877 ifp->if_resolvemulti = ether_resolvemulti;
878 if (ifp->if_baudrate == 0)
879 ifp->if_baudrate = IF_Mbps(10); /* just a default */
880 ifp->if_broadcastaddr = etherbroadcastaddr;
883 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
884 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
885 sdl->sdl_type = IFT_ETHER;
886 sdl->sdl_alen = ifp->if_addrlen;
887 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
889 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
890 if (ng_ether_attach_p != NULL)
891 (*ng_ether_attach_p)(ifp);
893 /* Announce Ethernet MAC address if non-zero. */
894 for (i = 0; i < ifp->if_addrlen; i++)
897 if (i != ifp->if_addrlen)
898 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
899 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
900 if_printf(ifp, "if_start running deferred for Giant\n");
904 * Perform common duties while detaching an Ethernet interface
907 ether_ifdetach(struct ifnet *ifp)
909 if (IFP2AC(ifp)->ac_netgraph != NULL) {
910 KASSERT(ng_ether_detach_p != NULL,
911 ("ng_ether_detach_p is NULL"));
912 (*ng_ether_detach_p)(ifp);
919 SYSCTL_DECL(_net_link);
920 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
921 #if defined(INET) || defined(INET6)
922 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
923 ðer_ipfw,0,"Pass ether pkts through firewall");
928 * This is for reference. We have a table-driven version
929 * of the little-endian crc32 generator, which is faster
930 * than the double-loop.
933 ether_crc32_le(const uint8_t *buf, size_t len)
940 crc = 0xffffffff; /* initial value */
942 for (i = 0; i < len; i++) {
943 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
944 carry = (crc ^ data) & 1;
947 crc = (crc ^ ETHER_CRC_POLY_LE);
954 ether_crc32_le(const uint8_t *buf, size_t len)
956 static const uint32_t crctab[] = {
957 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
958 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
959 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
960 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
965 crc = 0xffffffff; /* initial value */
967 for (i = 0; i < len; i++) {
969 crc = (crc >> 4) ^ crctab[crc & 0xf];
970 crc = (crc >> 4) ^ crctab[crc & 0xf];
978 ether_crc32_be(const uint8_t *buf, size_t len)
985 crc = 0xffffffff; /* initial value */
987 for (i = 0; i < len; i++) {
988 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
989 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
992 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1000 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
1002 struct ifaddr *ifa = (struct ifaddr *) data;
1003 struct ifreq *ifr = (struct ifreq *) data;
1008 ifp->if_flags |= IFF_UP;
1010 switch (ifa->ifa_addr->sa_family) {
1013 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1014 arp_ifinit(ifp, ifa);
1019 * XXX - This code is probably wrong
1023 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1025 if (ipx_nullhost(*ina))
1030 bcopy((caddr_t) ina->x_host.c_host,
1031 (caddr_t) IF_LLADDR(ifp),
1038 ifp->if_init(ifp->if_softc);
1043 ifp->if_init(ifp->if_softc);
1050 struct sockaddr *sa;
1052 sa = (struct sockaddr *) & ifr->ifr_data;
1053 bcopy(IF_LLADDR(ifp),
1054 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1060 * Set the interface MTU.
1062 if (ifr->ifr_mtu > ETHERMTU) {
1065 ifp->if_mtu = ifr->ifr_mtu;
1069 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1076 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1077 struct sockaddr *sa)
1079 struct sockaddr_dl *sdl;
1081 struct sockaddr_in *sin;
1084 struct sockaddr_in6 *sin6;
1088 switch(sa->sa_family) {
1091 * No mapping needed. Just check that it's a valid MC address.
1093 sdl = (struct sockaddr_dl *)sa;
1094 e_addr = LLADDR(sdl);
1095 if (!ETHER_IS_MULTICAST(e_addr))
1096 return EADDRNOTAVAIL;
1102 sin = (struct sockaddr_in *)sa;
1103 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1104 return EADDRNOTAVAIL;
1105 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1109 sdl->sdl_len = sizeof *sdl;
1110 sdl->sdl_family = AF_LINK;
1111 sdl->sdl_index = ifp->if_index;
1112 sdl->sdl_type = IFT_ETHER;
1113 sdl->sdl_alen = ETHER_ADDR_LEN;
1114 e_addr = LLADDR(sdl);
1115 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1116 *llsa = (struct sockaddr *)sdl;
1121 sin6 = (struct sockaddr_in6 *)sa;
1122 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1124 * An IP6 address of 0 means listen to all
1125 * of the Ethernet multicast address used for IP6.
1126 * (This is used for multicast routers.)
1128 ifp->if_flags |= IFF_ALLMULTI;
1132 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1133 return EADDRNOTAVAIL;
1134 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1138 sdl->sdl_len = sizeof *sdl;
1139 sdl->sdl_family = AF_LINK;
1140 sdl->sdl_index = ifp->if_index;
1141 sdl->sdl_type = IFT_ETHER;
1142 sdl->sdl_alen = ETHER_ADDR_LEN;
1143 e_addr = LLADDR(sdl);
1144 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1145 *llsa = (struct sockaddr *)sdl;
1151 * Well, the text isn't quite right, but it's the name
1154 return EAFNOSUPPORT;
1159 ether_alloc(u_char type, struct ifnet *ifp)
1163 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1170 ether_free(void *com, u_char type)
1173 free(com, M_ARPCOM);
1177 ether_modevent(module_t mod, int type, void *data)
1182 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1185 if_deregister_com_alloc(IFT_ETHER);
1194 static moduledata_t ether_mod = {
1201 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1203 struct ether_vlan_header vlan;
1206 KASSERT((m->m_flags & M_VLANTAG) != 0,
1207 ("%s: vlan information not present", __func__));
1208 KASSERT(m->m_len >= sizeof(struct ether_header),
1209 ("%s: mbuf not large enough for header", __func__));
1210 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1211 vlan.evl_proto = vlan.evl_encap_proto;
1212 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1213 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1214 m->m_len -= sizeof(struct ether_header);
1215 m->m_data += sizeof(struct ether_header);
1217 * If a data link has been supplied by the caller, then we will need to
1218 * re-create a stack allocated mbuf chain with the following structure:
1220 * (1) mbuf #1 will contain the supplied data link
1221 * (2) mbuf #2 will contain the vlan header
1222 * (3) mbuf #3 will contain the original mbuf's packet data
1224 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1228 mv.m_data = (caddr_t)&vlan;
1229 mv.m_len = sizeof(vlan);
1235 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1236 m->m_len += sizeof(struct ether_header);
1237 m->m_data -= sizeof(struct ether_header);
1240 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1241 MODULE_VERSION(ether, 1);