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 ETHER_IS_BROADCAST(addr) \
126 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
128 #define senderr(e) do { error = (e); goto bad;} while (0)
130 #if defined(INET) || defined(INET6)
132 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
133 struct ip_fw **rule, int shared);
134 static int ether_ipfw;
138 * Ethernet output routine.
139 * Encapsulate a packet of type family for the local net.
140 * Use trailer local net encapsulation if enough data in first
141 * packet leaves a multiple of 512 bytes of data in remainder.
144 ether_output(struct ifnet *ifp, struct mbuf *m,
145 struct sockaddr *dst, struct rtentry *rt0)
148 int error, hdrcmplt = 0;
149 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
150 struct ether_header *eh;
152 int hlen; /* link layer header length */
155 error = mac_check_ifnet_transmit(ifp, m);
160 if (ifp->if_flags & IFF_MONITOR)
162 if (!((ifp->if_flags & IFF_UP) &&
163 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
166 hlen = ETHER_HDR_LEN;
167 switch (dst->sa_family) {
170 error = arpresolve(ifp, rt0, m, dst, edst);
172 return (error == EWOULDBLOCK ? 0 : error);
173 type = htons(ETHERTYPE_IP);
178 ah = mtod(m, struct arphdr *);
179 ah->ar_hrd = htons(ARPHRD_ETHER);
181 loop_copy = 0; /* if this is for us, don't do it */
183 switch(ntohs(ah->ar_op)) {
184 case ARPOP_REVREQUEST:
186 type = htons(ETHERTYPE_REVARP);
191 type = htons(ETHERTYPE_ARP);
195 if (m->m_flags & M_BCAST)
196 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
198 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
205 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
208 type = htons(ETHERTYPE_IPV6);
214 error = ef_outputp(ifp, &m, dst, &type, &hlen);
218 type = htons(ETHERTYPE_IPX);
219 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
220 (caddr_t)edst, sizeof (edst));
226 struct at_ifaddr *aa;
228 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
229 senderr(EHOSTUNREACH); /* XXX */
230 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
233 * In the phase 2 case, need to prepend an mbuf for the llc header.
235 if ( aa->aa_flags & AFA_PHASE2 ) {
238 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
241 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
242 llc.llc_control = LLC_UI;
243 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
244 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
245 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
246 type = htons(m->m_pkthdr.len);
247 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
249 type = htons(ETHERTYPE_AT);
253 #endif /* NETATALK */
255 case pseudo_AF_HDRCMPLT:
257 eh = (struct ether_header *)dst->sa_data;
258 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
262 loop_copy = 0; /* if this is for us, don't do it */
263 eh = (struct ether_header *)dst->sa_data;
264 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
265 type = eh->ether_type;
269 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
270 senderr(EAFNOSUPPORT);
274 * Add local net header. If no space in first mbuf,
277 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
280 eh = mtod(m, struct ether_header *);
281 (void)memcpy(&eh->ether_type, &type,
282 sizeof(eh->ether_type));
283 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
285 (void)memcpy(eh->ether_shost, esrc,
286 sizeof(eh->ether_shost));
288 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
289 sizeof(eh->ether_shost));
292 * If a simplex interface, and the packet is being sent to our
293 * Ethernet address or a broadcast address, loopback a copy.
294 * XXX To make a simplex device behave exactly like a duplex
295 * device, we should copy in the case of sending to our own
296 * ethernet address (thus letting the original actually appear
297 * on the wire). However, we don't do that here for security
298 * reasons and compatibility with the original behavior.
300 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
301 m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
304 if (m->m_pkthdr.csum_flags & CSUM_IP)
305 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
306 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
307 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
309 if (m->m_flags & M_BCAST) {
313 * Because if_simloop() modifies the packet, we need a
314 * writable copy through m_dup() instead of a readonly
315 * one as m_copy[m] would give us. The alternative would
316 * be to modify if_simloop() to handle the readonly mbuf,
317 * but performancewise it is mostly equivalent (trading
318 * extra data copying vs. extra locking).
320 * XXX This is a local workaround. A number of less
321 * often used kernel parts suffer from the same bug.
322 * See PR kern/105943 for a proposed general solution.
324 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
325 n->m_pkthdr.csum_flags |= csum_flags;
326 if (csum_flags & CSUM_DATA_VALID)
327 n->m_pkthdr.csum_data = 0xffff;
328 (void)if_simloop(ifp, n, dst->sa_family, hlen);
331 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
332 ETHER_ADDR_LEN) == 0) {
333 m->m_pkthdr.csum_flags |= csum_flags;
334 if (csum_flags & CSUM_DATA_VALID)
335 m->m_pkthdr.csum_data = 0xffff;
336 (void) if_simloop(ifp, m, dst->sa_family, hlen);
337 return (0); /* XXX */
342 * Bridges require special output handling.
344 if (ifp->if_bridge) {
345 BRIDGE_OUTPUT(ifp, m, error);
351 (error = carp_output(ifp, m, dst, NULL)))
355 /* Handle ng_ether(4) processing, if any */
356 if (IFP2AC(ifp)->ac_netgraph != NULL) {
357 KASSERT(ng_ether_output_p != NULL,
358 ("ng_ether_output_p is NULL"));
359 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
368 /* Continue with link-layer output */
369 return ether_output_frame(ifp, m);
373 * Ethernet link layer output routine to send a raw frame to the device.
375 * This assumes that the 14 byte Ethernet header is present and contiguous
376 * in the first mbuf (if BRIDGE'ing).
379 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
382 #if defined(INET) || defined(INET6)
383 struct ip_fw *rule = ip_dn_claim_rule(m);
385 if (IPFW_LOADED && ether_ipfw != 0) {
386 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
389 return EACCES; /* pkt dropped */
391 return 0; /* consumed e.g. in a pipe */
397 * Queue message on interface, update output statistics if
398 * successful, and start output if interface not yet active.
400 IFQ_HANDOFF(ifp, m, error);
404 #if defined(INET) || defined(INET6)
406 * ipfw processing for ethernet packets (in and out).
407 * The second parameter is NULL from ether_demux, and ifp from
408 * ether_output_frame.
411 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
412 struct ip_fw **rule, int shared)
414 struct ether_header *eh;
415 struct ether_header save_eh;
418 struct ip_fw_args args;
420 if (*rule != NULL && fw_one_pass)
421 return 1; /* dummynet packet, already partially processed */
424 * I need some amt of data to be contiguous, and in case others need
425 * the packet (shared==1) also better be in the first mbuf.
428 i = min( m->m_pkthdr.len, max_protohdr);
429 if ( shared || m->m_len < i) {
436 eh = mtod(m, struct ether_header *);
437 save_eh = *eh; /* save copy for restore below */
438 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
440 args.m = m; /* the packet we are looking at */
441 args.oif = dst; /* destination, if any */
442 args.rule = *rule; /* matching rule to restart */
443 args.next_hop = NULL; /* we do not support forward yet */
444 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
445 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
446 i = ip_fw_chk_ptr(&args);
450 * Restore Ethernet header, as needed, in case the
451 * mbuf chain was replaced by ipfw.
453 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
458 if (eh != mtod(m, struct ether_header *))
459 bcopy(&save_eh, mtod(m, struct ether_header *),
465 if (i == IP_FW_DENY) /* drop */
468 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
470 if (i == IP_FW_PASS) /* a PASS rule. */
473 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
475 * Pass the pkt to dummynet, which consumes it.
476 * If shared, make a copy and keep the original.
479 m = m_copypacket(m, M_DONTWAIT);
484 * Pass the original to dummynet and
485 * nothing back to the caller
489 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
493 * XXX at some point add support for divert/forward actions.
494 * If none of the above matches, we have to drop the pkt.
501 * Process a received Ethernet packet; the packet is in the
502 * mbuf chain m with the ethernet header at the front.
505 ether_input(struct ifnet *ifp, struct mbuf *m)
507 struct ether_header *eh;
510 if ((ifp->if_flags & IFF_UP) == 0) {
515 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
516 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
522 * Do consistency checks to verify assumptions
523 * made by code past this point.
525 if ((m->m_flags & M_PKTHDR) == 0) {
526 if_printf(ifp, "discard frame w/o packet header\n");
531 if (m->m_len < ETHER_HDR_LEN) {
532 /* XXX maybe should pullup? */
533 if_printf(ifp, "discard frame w/o leading ethernet "
534 "header (len %u pkt len %u)\n",
535 m->m_len, m->m_pkthdr.len);
540 eh = mtod(m, struct ether_header *);
541 etype = ntohs(eh->ether_type);
542 if (m->m_pkthdr.len >
543 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
544 if_printf(ifp, "discard oversize frame "
545 "(ether type %x flags %x len %u > max %lu)\n",
546 etype, m->m_flags, m->m_pkthdr.len,
547 ETHER_MAX_FRAME(ifp, etype,
548 m->m_flags & M_HASFCS));
553 if (m->m_pkthdr.rcvif == NULL) {
554 if_printf(ifp, "discard frame w/o interface pointer\n");
560 if (m->m_pkthdr.rcvif != ifp) {
561 if_printf(ifp, "Warning, frame marked as received on %s\n",
562 m->m_pkthdr.rcvif->if_xname);
566 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
567 if (ETHER_IS_BROADCAST(eh->ether_dhost))
568 m->m_flags |= M_BCAST;
570 m->m_flags |= M_MCAST;
576 * Tag the mbuf with an appropriate MAC label before any other
577 * consumers can get to it.
579 mac_create_mbuf_from_ifnet(ifp, m);
583 * Give bpf a chance at the packet.
585 ETHER_BPF_MTAP(ifp, m);
588 * If the CRC is still on the packet, trim it off. We do this once
589 * and once only in case we are re-entered. Nothing else on the
590 * Ethernet receive path expects to see the FCS.
592 if (m->m_flags & M_HASFCS) {
593 m_adj(m, -ETHER_CRC_LEN);
594 m->m_flags &= ~M_HASFCS;
597 ifp->if_ibytes += m->m_pkthdr.len;
599 /* Allow monitor mode to claim this frame, after stats are updated. */
600 if (ifp->if_flags & IFF_MONITOR) {
606 * If the hardware did not process an 802.1Q tag, do this now,
607 * to allow 802.1P priority frames to be passed to the main input
609 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
611 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
612 struct ether_vlan_header *evl;
614 if (m->m_len < sizeof(*evl) &&
615 (m = m_pullup(m, sizeof(*evl))) == NULL) {
616 if_printf(ifp, "cannot pullup VLAN header\n");
622 evl = mtod(m, struct ether_vlan_header *);
623 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
624 m->m_flags |= M_VLANTAG;
626 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
627 ETHER_HDR_LEN - ETHER_TYPE_LEN);
628 m_adj(m, ETHER_VLAN_ENCAP_LEN);
631 /* Allow ng_ether(4) to claim this frame. */
632 if (IFP2AC(ifp)->ac_netgraph != NULL) {
633 KASSERT(ng_ether_input_p != NULL,
634 ("%s: ng_ether_input_p is NULL", __func__));
635 m->m_flags &= ~M_PROMISC;
636 (*ng_ether_input_p)(ifp, &m);
642 * Allow if_bridge(4) to claim this frame.
643 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
644 * and the frame should be delivered locally.
646 if (ifp->if_bridge != NULL) {
647 m->m_flags &= ~M_PROMISC;
648 BRIDGE_INPUT(ifp, m);
655 * Clear M_PROMISC on frame so that carp(4) will see it when the
656 * mbuf flows up to Layer 3.
657 * FreeBSD's implementation of carp(4) uses the inprotosw
658 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
659 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
660 * is outside the scope of the M_PROMISC test below.
661 * TODO: Maintain a hash table of ethernet addresses other than
662 * ether_dhost which may be active on this ifp.
664 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
665 m->m_flags &= ~M_PROMISC;
670 * If the frame was received promiscuously, set the
671 * M_PROMISC flag on the mbuf chain. The frame may need to
672 * be seen by the rest of the Ethernet input path in case of
673 * re-entry (e.g. bridge, vlan, netgraph) but should not be
674 * seen by upper protocol layers.
676 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
677 (ifp->if_flags & IFF_PROMISC) != 0 &&
678 !bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN))
679 m->m_flags |= M_PROMISC;
682 /* First chunk of an mbuf contains good entropy */
683 if (harvest.ethernet)
684 random_harvest(m, 16, 3, 0, RANDOM_NET);
690 * Upper layer processing for a received Ethernet packet.
693 ether_demux(struct ifnet *ifp, struct mbuf *m)
695 struct ether_header *eh;
698 #if defined(NETATALK)
702 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
704 #if defined(INET) || defined(INET6)
706 * Allow dummynet and/or ipfw to claim the frame.
707 * Do not do this for PROMISC frames in case we are re-entered.
709 if (IPFW_LOADED && ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
710 struct ip_fw *rule = ip_dn_claim_rule(m);
712 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
714 m_freem(m); /* dropped; free mbuf chain */
715 return; /* consumed */
719 eh = mtod(m, struct ether_header *);
720 ether_type = ntohs(eh->ether_type);
723 * If this frame has a VLAN tag other than 0, call vlan_input()
724 * if its module is loaded. Otherwise, drop.
726 if ((m->m_flags & M_VLANTAG) &&
727 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
728 if (ifp->if_vlantrunk == NULL) {
733 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
735 /* Clear before possibly re-entering ether_input(). */
736 m->m_flags &= ~M_PROMISC;
737 (*vlan_input_p)(ifp, m);
742 * Pass promiscuously received frames to the upper layer if the user
743 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
745 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
751 * Reset layer specific mbuf flags to avoid confusing upper layers.
752 * Strip off Ethernet header.
754 m->m_flags &= ~M_VLANTAG;
755 m->m_flags &= ~(M_PROTOFLAGS);
756 m_adj(m, ETHER_HDR_LEN);
759 * Dispatch frame to upper layer.
761 switch (ether_type) {
764 if ((m = ip_fastforward(m)) == NULL)
770 if (ifp->if_flags & IFF_NOARP) {
771 /* Discard packet if ARP is disabled on interface */
780 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
797 #endif /* NETATALK */
800 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
803 #if defined(NETATALK)
804 if (ether_type > ETHERMTU)
806 l = mtod(m, struct llc *);
807 if (l->llc_dsap == LLC_SNAP_LSAP &&
808 l->llc_ssap == LLC_SNAP_LSAP &&
809 l->llc_control == LLC_UI) {
810 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
811 sizeof(at_org_code)) == 0 &&
812 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
813 m_adj(m, LLC_SNAPFRAMELEN);
817 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
818 sizeof(aarp_org_code)) == 0 &&
819 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
820 m_adj(m, LLC_SNAPFRAMELEN);
825 #endif /* NETATALK */
828 netisr_dispatch(isr, m);
833 * Packet is to be discarded. If netgraph is present,
834 * hand the packet to it for last chance processing;
835 * otherwise dispose of it.
837 if (IFP2AC(ifp)->ac_netgraph != NULL) {
838 KASSERT(ng_ether_input_orphan_p != NULL,
839 ("ng_ether_input_orphan_p is NULL"));
841 * Put back the ethernet header so netgraph has a
842 * consistent view of inbound packets.
844 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
845 (*ng_ether_input_orphan_p)(ifp, m);
852 * Convert Ethernet address to printable (loggable) representation.
853 * This routine is for compatibility; it's better to just use
855 * printf("%6D", <pointer to address>, ":");
857 * since there's no static buffer involved.
860 ether_sprintf(const u_char *ap)
862 static char etherbuf[18];
863 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
868 * Perform common duties while attaching to interface list
871 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
875 struct sockaddr_dl *sdl;
877 ifp->if_addrlen = ETHER_ADDR_LEN;
878 ifp->if_hdrlen = ETHER_HDR_LEN;
880 ifp->if_mtu = ETHERMTU;
881 ifp->if_output = ether_output;
882 ifp->if_input = ether_input;
883 ifp->if_resolvemulti = ether_resolvemulti;
884 if (ifp->if_baudrate == 0)
885 ifp->if_baudrate = IF_Mbps(10); /* just a default */
886 ifp->if_broadcastaddr = etherbroadcastaddr;
889 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
890 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
891 sdl->sdl_type = IFT_ETHER;
892 sdl->sdl_alen = ifp->if_addrlen;
893 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
895 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
896 if (ng_ether_attach_p != NULL)
897 (*ng_ether_attach_p)(ifp);
899 /* Announce Ethernet MAC address if non-zero. */
900 for (i = 0; i < ifp->if_addrlen; i++)
903 if (i != ifp->if_addrlen)
904 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
905 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
906 if_printf(ifp, "if_start running deferred for Giant\n");
910 * Perform common duties while detaching an Ethernet interface
913 ether_ifdetach(struct ifnet *ifp)
915 if (IFP2AC(ifp)->ac_netgraph != NULL) {
916 KASSERT(ng_ether_detach_p != NULL,
917 ("ng_ether_detach_p is NULL"));
918 (*ng_ether_detach_p)(ifp);
925 SYSCTL_DECL(_net_link);
926 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
927 #if defined(INET) || defined(INET6)
928 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
929 ðer_ipfw,0,"Pass ether pkts through firewall");
934 * This is for reference. We have a table-driven version
935 * of the little-endian crc32 generator, which is faster
936 * than the double-loop.
939 ether_crc32_le(const uint8_t *buf, size_t len)
946 crc = 0xffffffff; /* initial value */
948 for (i = 0; i < len; i++) {
949 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
950 carry = (crc ^ data) & 1;
953 crc = (crc ^ ETHER_CRC_POLY_LE);
960 ether_crc32_le(const uint8_t *buf, size_t len)
962 static const uint32_t crctab[] = {
963 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
964 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
965 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
966 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
971 crc = 0xffffffff; /* initial value */
973 for (i = 0; i < len; i++) {
975 crc = (crc >> 4) ^ crctab[crc & 0xf];
976 crc = (crc >> 4) ^ crctab[crc & 0xf];
984 ether_crc32_be(const uint8_t *buf, size_t len)
991 crc = 0xffffffff; /* initial value */
993 for (i = 0; i < len; i++) {
994 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
995 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
998 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1006 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
1008 struct ifaddr *ifa = (struct ifaddr *) data;
1009 struct ifreq *ifr = (struct ifreq *) data;
1014 ifp->if_flags |= IFF_UP;
1016 switch (ifa->ifa_addr->sa_family) {
1019 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1020 arp_ifinit(ifp, ifa);
1025 * XXX - This code is probably wrong
1029 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1031 if (ipx_nullhost(*ina))
1036 bcopy((caddr_t) ina->x_host.c_host,
1037 (caddr_t) IF_LLADDR(ifp),
1044 ifp->if_init(ifp->if_softc);
1049 ifp->if_init(ifp->if_softc);
1056 struct sockaddr *sa;
1058 sa = (struct sockaddr *) & ifr->ifr_data;
1059 bcopy(IF_LLADDR(ifp),
1060 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1066 * Set the interface MTU.
1068 if (ifr->ifr_mtu > ETHERMTU) {
1071 ifp->if_mtu = ifr->ifr_mtu;
1075 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1082 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1083 struct sockaddr *sa)
1085 struct sockaddr_dl *sdl;
1087 struct sockaddr_in *sin;
1090 struct sockaddr_in6 *sin6;
1094 switch(sa->sa_family) {
1097 * No mapping needed. Just check that it's a valid MC address.
1099 sdl = (struct sockaddr_dl *)sa;
1100 e_addr = LLADDR(sdl);
1101 if (!ETHER_IS_MULTICAST(e_addr))
1102 return EADDRNOTAVAIL;
1108 sin = (struct sockaddr_in *)sa;
1109 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1110 return EADDRNOTAVAIL;
1111 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1115 sdl->sdl_len = sizeof *sdl;
1116 sdl->sdl_family = AF_LINK;
1117 sdl->sdl_index = ifp->if_index;
1118 sdl->sdl_type = IFT_ETHER;
1119 sdl->sdl_alen = ETHER_ADDR_LEN;
1120 e_addr = LLADDR(sdl);
1121 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1122 *llsa = (struct sockaddr *)sdl;
1127 sin6 = (struct sockaddr_in6 *)sa;
1128 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1130 * An IP6 address of 0 means listen to all
1131 * of the Ethernet multicast address used for IP6.
1132 * (This is used for multicast routers.)
1134 ifp->if_flags |= IFF_ALLMULTI;
1138 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1139 return EADDRNOTAVAIL;
1140 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1144 sdl->sdl_len = sizeof *sdl;
1145 sdl->sdl_family = AF_LINK;
1146 sdl->sdl_index = ifp->if_index;
1147 sdl->sdl_type = IFT_ETHER;
1148 sdl->sdl_alen = ETHER_ADDR_LEN;
1149 e_addr = LLADDR(sdl);
1150 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1151 *llsa = (struct sockaddr *)sdl;
1157 * Well, the text isn't quite right, but it's the name
1160 return EAFNOSUPPORT;
1165 ether_alloc(u_char type, struct ifnet *ifp)
1169 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1176 ether_free(void *com, u_char type)
1179 free(com, M_ARPCOM);
1183 ether_modevent(module_t mod, int type, void *data)
1188 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1191 if_deregister_com_alloc(IFT_ETHER);
1200 static moduledata_t ether_mod = {
1207 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1209 struct ether_vlan_header vlan;
1212 KASSERT((m->m_flags & M_VLANTAG) != 0,
1213 ("%s: vlan information not present", __func__));
1214 KASSERT(m->m_len >= sizeof(struct ether_header),
1215 ("%s: mbuf not large enough for header", __func__));
1216 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1217 vlan.evl_proto = vlan.evl_encap_proto;
1218 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1219 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1220 m->m_len -= sizeof(struct ether_header);
1221 m->m_data += sizeof(struct ether_header);
1223 * If a data link has been supplied by the caller, then we will need to
1224 * re-create a stack allocated mbuf chain with the following structure:
1226 * (1) mbuf #1 will contain the supplied data link
1227 * (2) mbuf #2 will contain the vlan header
1228 * (3) mbuf #3 will contain the original mbuf's packet data
1230 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1234 mv.m_data = (caddr_t)&vlan;
1235 mv.m_len = sizeof(vlan);
1241 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1242 m->m_len += sizeof(struct ether_header);
1243 m->m_data -= sizeof(struct ether_header);
1246 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1247 MODULE_VERSION(ether, 1);