2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
33 #include "opt_atalk.h"
35 #include "opt_inet6.h"
37 #include "opt_netgraph.h"
39 #include "opt_mbuf_profiling.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
48 #include <sys/random.h>
49 #include <sys/rwlock.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
55 #include <net/if_arp.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_llc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
62 #include <net/ethernet.h>
63 #include <net/if_bridgevar.h>
64 #include <net/if_vlan_var.h>
65 #include <net/if_llatbl.h>
66 #include <net/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_fw.h>
74 #include <netinet/ip_dummynet.h>
75 #include <netinet/ip_var.h>
78 #include <netinet6/nd6.h>
81 #if defined(INET) || defined(INET6)
83 #include <netinet/ip_carp.h>
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
92 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
93 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
94 struct sockaddr *dst, short *tp, int *hlen);
97 #include <netatalk/at.h>
98 #include <netatalk/at_var.h>
99 #include <netatalk/at_extern.h>
101 #define llc_snap_org_code llc_un.type_snap.org_code
102 #define llc_snap_ether_type llc_un.type_snap.ether_type
104 extern u_char at_org_code[3];
105 extern u_char aarp_org_code[3];
106 #endif /* NETATALK */
108 #include <security/mac/mac_framework.h>
111 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
112 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
115 /* netgraph node hooks for ng_ether(4) */
116 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
117 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
118 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
119 void (*ng_ether_attach_p)(struct ifnet *ifp);
120 void (*ng_ether_detach_p)(struct ifnet *ifp);
122 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
124 /* if_bridge(4) support */
125 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
126 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
127 struct sockaddr *, struct rtentry *);
128 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
130 /* if_lagg(4) support */
131 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
133 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
134 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
136 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
139 /* XXX: should be in an arp support file, not here */
140 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
142 #define ETHER_IS_BROADCAST(addr) \
143 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
145 #define senderr(e) do { error = (e); goto bad;} while (0)
147 #if defined(INET) || defined(INET6)
149 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared);
150 static VNET_DEFINE(int, ether_ipfw);
151 #define V_ether_ipfw VNET(ether_ipfw)
156 * Ethernet output routine.
157 * Encapsulate a packet of type family for the local net.
158 * Use trailer local net encapsulation if enough data in first
159 * packet leaves a multiple of 512 bytes of data in remainder.
162 ether_output(struct ifnet *ifp, struct mbuf *m,
163 struct sockaddr *dst, struct route *ro)
166 int error = 0, hdrcmplt = 0;
167 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
168 struct llentry *lle = NULL;
169 struct rtentry *rt0 = NULL;
170 struct ether_header *eh;
173 int hlen; /* link layer header length */
176 if (!(m->m_flags & (M_BCAST | M_MCAST)))
181 error = mac_ifnet_check_transmit(ifp, m);
187 if (ifp->if_flags & IFF_MONITOR)
189 if (!((ifp->if_flags & IFF_UP) &&
190 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
193 hlen = ETHER_HDR_LEN;
194 switch (dst->sa_family) {
197 if (lle != NULL && (lle->la_flags & LLE_VALID))
198 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
200 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
202 return (error == EWOULDBLOCK ? 0 : error);
203 type = htons(ETHERTYPE_IP);
208 ah = mtod(m, struct arphdr *);
209 ah->ar_hrd = htons(ARPHRD_ETHER);
211 loop_copy = 0; /* if this is for us, don't do it */
213 switch(ntohs(ah->ar_op)) {
214 case ARPOP_REVREQUEST:
216 type = htons(ETHERTYPE_REVARP);
221 type = htons(ETHERTYPE_ARP);
225 if (m->m_flags & M_BCAST)
226 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
228 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
235 if (lle != NULL && (lle->la_flags & LLE_VALID))
236 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
238 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
241 type = htons(ETHERTYPE_IPV6);
247 error = ef_outputp(ifp, &m, dst, &type, &hlen);
251 type = htons(ETHERTYPE_IPX);
252 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
253 (caddr_t)edst, sizeof (edst));
259 struct at_ifaddr *aa;
261 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
262 senderr(EHOSTUNREACH); /* XXX */
263 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
264 ifa_free(&aa->aa_ifa);
268 * In the phase 2 case, need to prepend an mbuf for the llc header.
270 if ( aa->aa_flags & AFA_PHASE2 ) {
273 ifa_free(&aa->aa_ifa);
274 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
277 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
278 llc.llc_control = LLC_UI;
279 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
280 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
281 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
282 type = htons(m->m_pkthdr.len);
283 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
285 ifa_free(&aa->aa_ifa);
286 type = htons(ETHERTYPE_AT);
290 #endif /* NETATALK */
292 case pseudo_AF_HDRCMPLT:
294 eh = (struct ether_header *)dst->sa_data;
295 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
299 loop_copy = 0; /* if this is for us, don't do it */
300 eh = (struct ether_header *)dst->sa_data;
301 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
302 type = eh->ether_type;
306 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
307 senderr(EAFNOSUPPORT);
310 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
312 if (m->m_pkthdr.csum_flags & CSUM_IP)
313 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
314 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
315 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
316 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
317 csum_flags |= CSUM_SCTP_VALID;
318 m->m_pkthdr.csum_flags |= csum_flags;
319 m->m_pkthdr.csum_data = 0xffff;
320 return (if_simloop(ifp, m, dst->sa_family, 0));
324 * Add local net header. If no space in first mbuf,
327 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
330 eh = mtod(m, struct ether_header *);
331 (void)memcpy(&eh->ether_type, &type,
332 sizeof(eh->ether_type));
333 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
335 (void)memcpy(eh->ether_shost, esrc,
336 sizeof(eh->ether_shost));
338 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
339 sizeof(eh->ether_shost));
342 * If a simplex interface, and the packet is being sent to our
343 * Ethernet address or a broadcast address, loopback a copy.
344 * XXX To make a simplex device behave exactly like a duplex
345 * device, we should copy in the case of sending to our own
346 * ethernet address (thus letting the original actually appear
347 * on the wire). However, we don't do that here for security
348 * reasons and compatibility with the original behavior.
350 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
351 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
354 if (m->m_pkthdr.csum_flags & CSUM_IP)
355 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
356 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
357 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
358 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
359 csum_flags |= CSUM_SCTP_VALID;
361 if (m->m_flags & M_BCAST) {
365 * Because if_simloop() modifies the packet, we need a
366 * writable copy through m_dup() instead of a readonly
367 * one as m_copy[m] would give us. The alternative would
368 * be to modify if_simloop() to handle the readonly mbuf,
369 * but performancewise it is mostly equivalent (trading
370 * extra data copying vs. extra locking).
372 * XXX This is a local workaround. A number of less
373 * often used kernel parts suffer from the same bug.
374 * See PR kern/105943 for a proposed general solution.
376 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
377 n->m_pkthdr.csum_flags |= csum_flags;
378 if (csum_flags & CSUM_DATA_VALID)
379 n->m_pkthdr.csum_data = 0xffff;
380 (void)if_simloop(ifp, n, dst->sa_family, hlen);
383 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
384 ETHER_ADDR_LEN) == 0) {
385 m->m_pkthdr.csum_flags |= csum_flags;
386 if (csum_flags & CSUM_DATA_VALID)
387 m->m_pkthdr.csum_data = 0xffff;
388 (void) if_simloop(ifp, m, dst->sa_family, hlen);
389 return (0); /* XXX */
394 * Bridges require special output handling.
396 if (ifp->if_bridge) {
397 BRIDGE_OUTPUT(ifp, m, error);
401 #if defined(INET) || defined(INET6)
404 (error = carp_output(ifp, m, dst, NULL)))
409 /* Handle ng_ether(4) processing, if any */
410 if (IFP2AC(ifp)->ac_netgraph != NULL) {
411 KASSERT(ng_ether_output_p != NULL,
412 ("ng_ether_output_p is NULL"));
413 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
422 /* Continue with link-layer output */
423 return ether_output_frame(ifp, m);
427 * Ethernet link layer output routine to send a raw frame to the device.
429 * This assumes that the 14 byte Ethernet header is present and contiguous
430 * in the first mbuf (if BRIDGE'ing).
433 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
435 #if defined(INET) || defined(INET6)
437 if (ip_fw_chk_ptr && V_ether_ipfw != 0) {
438 if (ether_ipfw_chk(&m, ifp, 0) == 0) {
441 return EACCES; /* pkt dropped */
443 return 0; /* consumed e.g. in a pipe */
449 * Queue message on interface, update output statistics if
450 * successful, and start output if interface not yet active.
452 return ((ifp->if_transmit)(ifp, m));
455 #if defined(INET) || defined(INET6)
457 * ipfw processing for ethernet packets (in and out).
458 * The second parameter is NULL from ether_demux, and ifp from
459 * ether_output_frame.
462 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared)
464 struct ether_header *eh;
465 struct ether_header save_eh;
468 struct ip_fw_args args;
469 struct dn_pkt_tag *dn_tag;
471 dn_tag = ip_dn_claim_tag(*m0);
473 if (dn_tag != NULL) {
474 if (dn_tag->rule != NULL && V_fw_one_pass)
475 /* dummynet packet, already partially processed */
477 args.rule = dn_tag->rule; /* matching rule to restart */
478 args.rule_id = dn_tag->rule_id;
479 args.chain_id = dn_tag->chain_id;
484 * I need some amt of data to be contiguous, and in case others need
485 * the packet (shared==1) also better be in the first mbuf.
488 i = min( m->m_pkthdr.len, max_protohdr);
489 if ( shared || m->m_len < i) {
496 eh = mtod(m, struct ether_header *);
497 save_eh = *eh; /* save copy for restore below */
498 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
500 args.m = m; /* the packet we are looking at */
501 args.oif = dst; /* destination, if any */
502 args.next_hop = NULL; /* we do not support forward yet */
503 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
504 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
505 i = ip_fw_chk_ptr(&args);
509 * Restore Ethernet header, as needed, in case the
510 * mbuf chain was replaced by ipfw.
512 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
517 if (eh != mtod(m, struct ether_header *))
518 bcopy(&save_eh, mtod(m, struct ether_header *),
523 if (i == IP_FW_DENY) /* drop */
526 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
528 if (i == IP_FW_PASS) /* a PASS rule. */
531 if (ip_dn_io_ptr && (i == IP_FW_DUMMYNET)) {
533 * Pass the pkt to dummynet, which consumes it.
534 * If shared, make a copy and keep the original.
537 m = m_copypacket(m, M_DONTWAIT);
542 * Pass the original to dummynet and
543 * nothing back to the caller
547 ip_dn_io_ptr(&m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
551 * XXX at some point add support for divert/forward actions.
552 * If none of the above matches, we have to drop the pkt.
559 * Process a received Ethernet packet; the packet is in the
560 * mbuf chain m with the ethernet header at the front.
563 ether_input(struct ifnet *ifp, struct mbuf *m)
565 struct ether_header *eh;
568 if ((ifp->if_flags & IFF_UP) == 0) {
573 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
574 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
580 * Do consistency checks to verify assumptions
581 * made by code past this point.
583 if ((m->m_flags & M_PKTHDR) == 0) {
584 if_printf(ifp, "discard frame w/o packet header\n");
589 if (m->m_len < ETHER_HDR_LEN) {
590 /* XXX maybe should pullup? */
591 if_printf(ifp, "discard frame w/o leading ethernet "
592 "header (len %u pkt len %u)\n",
593 m->m_len, m->m_pkthdr.len);
598 eh = mtod(m, struct ether_header *);
599 etype = ntohs(eh->ether_type);
600 if (m->m_pkthdr.rcvif == NULL) {
601 if_printf(ifp, "discard frame w/o interface pointer\n");
607 if (m->m_pkthdr.rcvif != ifp) {
608 if_printf(ifp, "Warning, frame marked as received on %s\n",
609 m->m_pkthdr.rcvif->if_xname);
613 CURVNET_SET_QUIET(ifp->if_vnet);
615 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
616 if (ETHER_IS_BROADCAST(eh->ether_dhost))
617 m->m_flags |= M_BCAST;
619 m->m_flags |= M_MCAST;
625 * Tag the mbuf with an appropriate MAC label before any other
626 * consumers can get to it.
628 mac_ifnet_create_mbuf(ifp, m);
632 * Give bpf a chance at the packet.
634 ETHER_BPF_MTAP(ifp, m);
637 * If the CRC is still on the packet, trim it off. We do this once
638 * and once only in case we are re-entered. Nothing else on the
639 * Ethernet receive path expects to see the FCS.
641 if (m->m_flags & M_HASFCS) {
642 m_adj(m, -ETHER_CRC_LEN);
643 m->m_flags &= ~M_HASFCS;
646 ifp->if_ibytes += m->m_pkthdr.len;
648 /* Allow monitor mode to claim this frame, after stats are updated. */
649 if (ifp->if_flags & IFF_MONITOR) {
655 /* Handle input from a lagg(4) port */
656 if (ifp->if_type == IFT_IEEE8023ADLAG) {
657 KASSERT(lagg_input_p != NULL,
658 ("%s: if_lagg not loaded!", __func__));
659 m = (*lagg_input_p)(ifp, m);
661 ifp = m->m_pkthdr.rcvif;
667 * If the hardware did not process an 802.1Q tag, do this now,
668 * to allow 802.1P priority frames to be passed to the main input
670 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
672 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
673 struct ether_vlan_header *evl;
675 if (m->m_len < sizeof(*evl) &&
676 (m = m_pullup(m, sizeof(*evl))) == NULL) {
678 if_printf(ifp, "cannot pullup VLAN header\n");
685 evl = mtod(m, struct ether_vlan_header *);
686 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
687 m->m_flags |= M_VLANTAG;
689 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
690 ETHER_HDR_LEN - ETHER_TYPE_LEN);
691 m_adj(m, ETHER_VLAN_ENCAP_LEN);
694 /* Allow ng_ether(4) to claim this frame. */
695 if (IFP2AC(ifp)->ac_netgraph != NULL) {
696 KASSERT(ng_ether_input_p != NULL,
697 ("%s: ng_ether_input_p is NULL", __func__));
698 m->m_flags &= ~M_PROMISC;
699 (*ng_ether_input_p)(ifp, &m);
707 * Allow if_bridge(4) to claim this frame.
708 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
709 * and the frame should be delivered locally.
711 if (ifp->if_bridge != NULL) {
712 m->m_flags &= ~M_PROMISC;
713 BRIDGE_INPUT(ifp, m);
720 #if defined(INET) || defined(INET6)
723 * Clear M_PROMISC on frame so that carp(4) will see it when the
724 * mbuf flows up to Layer 3.
725 * FreeBSD's implementation of carp(4) uses the inprotosw
726 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
727 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
728 * is outside the scope of the M_PROMISC test below.
729 * TODO: Maintain a hash table of ethernet addresses other than
730 * ether_dhost which may be active on this ifp.
732 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
733 m->m_flags &= ~M_PROMISC;
739 * If the frame received was not for our MAC address, set the
740 * M_PROMISC flag on the mbuf chain. The frame may need to
741 * be seen by the rest of the Ethernet input path in case of
742 * re-entry (e.g. bridge, vlan, netgraph) but should not be
743 * seen by upper protocol layers.
745 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
746 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
747 m->m_flags |= M_PROMISC;
750 /* First chunk of an mbuf contains good entropy */
751 if (harvest.ethernet)
752 random_harvest(m, 16, 3, 0, RANDOM_NET);
759 * Upper layer processing for a received Ethernet packet.
762 ether_demux(struct ifnet *ifp, struct mbuf *m)
764 struct ether_header *eh;
767 #if defined(NETATALK)
771 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
773 #if defined(INET) || defined(INET6)
775 * Allow dummynet and/or ipfw to claim the frame.
776 * Do not do this for PROMISC frames in case we are re-entered.
778 if (ip_fw_chk_ptr && V_ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
779 if (ether_ipfw_chk(&m, NULL, 0) == 0) {
781 m_freem(m); /* dropped; free mbuf chain */
782 return; /* consumed */
786 eh = mtod(m, struct ether_header *);
787 ether_type = ntohs(eh->ether_type);
790 * If this frame has a VLAN tag other than 0, call vlan_input()
791 * if its module is loaded. Otherwise, drop.
793 if ((m->m_flags & M_VLANTAG) &&
794 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
795 if (ifp->if_vlantrunk == NULL) {
800 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
802 /* Clear before possibly re-entering ether_input(). */
803 m->m_flags &= ~M_PROMISC;
804 (*vlan_input_p)(ifp, m);
809 * Pass promiscuously received frames to the upper layer if the user
810 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
812 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
818 * Reset layer specific mbuf flags to avoid confusing upper layers.
819 * Strip off Ethernet header.
821 m->m_flags &= ~M_VLANTAG;
822 m->m_flags &= ~(M_PROTOFLAGS);
823 m_adj(m, ETHER_HDR_LEN);
826 * Dispatch frame to upper layer.
828 switch (ether_type) {
831 if ((m = ip_fastforward(m)) == NULL)
837 if (ifp->if_flags & IFF_NOARP) {
838 /* Discard packet if ARP is disabled on interface */
847 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
864 #endif /* NETATALK */
867 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
870 #if defined(NETATALK)
871 if (ether_type > ETHERMTU)
873 l = mtod(m, struct llc *);
874 if (l->llc_dsap == LLC_SNAP_LSAP &&
875 l->llc_ssap == LLC_SNAP_LSAP &&
876 l->llc_control == LLC_UI) {
877 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
878 sizeof(at_org_code)) == 0 &&
879 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
880 m_adj(m, LLC_SNAPFRAMELEN);
884 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
885 sizeof(aarp_org_code)) == 0 &&
886 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
887 m_adj(m, LLC_SNAPFRAMELEN);
892 #endif /* NETATALK */
895 netisr_dispatch(isr, m);
900 * Packet is to be discarded. If netgraph is present,
901 * hand the packet to it for last chance processing;
902 * otherwise dispose of it.
904 if (IFP2AC(ifp)->ac_netgraph != NULL) {
905 KASSERT(ng_ether_input_orphan_p != NULL,
906 ("ng_ether_input_orphan_p is NULL"));
908 * Put back the ethernet header so netgraph has a
909 * consistent view of inbound packets.
911 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
912 (*ng_ether_input_orphan_p)(ifp, m);
919 * Convert Ethernet address to printable (loggable) representation.
920 * This routine is for compatibility; it's better to just use
922 * printf("%6D", <pointer to address>, ":");
924 * since there's no static buffer involved.
927 ether_sprintf(const u_char *ap)
929 static char etherbuf[18];
930 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
935 * Perform common duties while attaching to interface list
938 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
942 struct sockaddr_dl *sdl;
944 ifp->if_addrlen = ETHER_ADDR_LEN;
945 ifp->if_hdrlen = ETHER_HDR_LEN;
947 ifp->if_mtu = ETHERMTU;
948 ifp->if_output = ether_output;
949 ifp->if_input = ether_input;
950 ifp->if_resolvemulti = ether_resolvemulti;
951 if (ifp->if_baudrate == 0)
952 ifp->if_baudrate = IF_Mbps(10); /* just a default */
953 ifp->if_broadcastaddr = etherbroadcastaddr;
956 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
957 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
958 sdl->sdl_type = IFT_ETHER;
959 sdl->sdl_alen = ifp->if_addrlen;
960 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
962 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
963 if (ng_ether_attach_p != NULL)
964 (*ng_ether_attach_p)(ifp);
966 /* Announce Ethernet MAC address if non-zero. */
967 for (i = 0; i < ifp->if_addrlen; i++)
970 if (i != ifp->if_addrlen)
971 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
975 * Perform common duties while detaching an Ethernet interface
978 ether_ifdetach(struct ifnet *ifp)
980 if (IFP2AC(ifp)->ac_netgraph != NULL) {
981 KASSERT(ng_ether_detach_p != NULL,
982 ("ng_ether_detach_p is NULL"));
983 (*ng_ether_detach_p)(ifp);
990 SYSCTL_DECL(_net_link);
991 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
992 #if defined(INET) || defined(INET6)
993 SYSCTL_VNET_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
994 &VNET_NAME(ether_ipfw), 0, "Pass ether pkts through firewall");
999 * This is for reference. We have a table-driven version
1000 * of the little-endian crc32 generator, which is faster
1001 * than the double-loop.
1004 ether_crc32_le(const uint8_t *buf, size_t len)
1011 crc = 0xffffffff; /* initial value */
1013 for (i = 0; i < len; i++) {
1014 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1015 carry = (crc ^ data) & 1;
1018 crc = (crc ^ ETHER_CRC_POLY_LE);
1026 ether_crc32_le(const uint8_t *buf, size_t len)
1028 static const uint32_t crctab[] = {
1029 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1030 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1031 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1032 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1037 crc = 0xffffffff; /* initial value */
1039 for (i = 0; i < len; i++) {
1041 crc = (crc >> 4) ^ crctab[crc & 0xf];
1042 crc = (crc >> 4) ^ crctab[crc & 0xf];
1050 ether_crc32_be(const uint8_t *buf, size_t len)
1053 uint32_t crc, carry;
1057 crc = 0xffffffff; /* initial value */
1059 for (i = 0; i < len; i++) {
1060 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1061 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1064 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1072 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1074 struct ifaddr *ifa = (struct ifaddr *) data;
1075 struct ifreq *ifr = (struct ifreq *) data;
1080 ifp->if_flags |= IFF_UP;
1082 switch (ifa->ifa_addr->sa_family) {
1085 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1086 arp_ifinit(ifp, ifa);
1091 * XXX - This code is probably wrong
1095 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1097 if (ipx_nullhost(*ina))
1102 bcopy((caddr_t) ina->x_host.c_host,
1103 (caddr_t) IF_LLADDR(ifp),
1110 ifp->if_init(ifp->if_softc);
1115 ifp->if_init(ifp->if_softc);
1122 struct sockaddr *sa;
1124 sa = (struct sockaddr *) & ifr->ifr_data;
1125 bcopy(IF_LLADDR(ifp),
1126 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1132 * Set the interface MTU.
1134 if (ifr->ifr_mtu > ETHERMTU) {
1137 ifp->if_mtu = ifr->ifr_mtu;
1141 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1148 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1149 struct sockaddr *sa)
1151 struct sockaddr_dl *sdl;
1153 struct sockaddr_in *sin;
1156 struct sockaddr_in6 *sin6;
1160 switch(sa->sa_family) {
1163 * No mapping needed. Just check that it's a valid MC address.
1165 sdl = (struct sockaddr_dl *)sa;
1166 e_addr = LLADDR(sdl);
1167 if (!ETHER_IS_MULTICAST(e_addr))
1168 return EADDRNOTAVAIL;
1174 sin = (struct sockaddr_in *)sa;
1175 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1176 return EADDRNOTAVAIL;
1177 sdl = malloc(sizeof *sdl, M_IFMADDR,
1181 sdl->sdl_len = sizeof *sdl;
1182 sdl->sdl_family = AF_LINK;
1183 sdl->sdl_index = ifp->if_index;
1184 sdl->sdl_type = IFT_ETHER;
1185 sdl->sdl_alen = ETHER_ADDR_LEN;
1186 e_addr = LLADDR(sdl);
1187 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1188 *llsa = (struct sockaddr *)sdl;
1193 sin6 = (struct sockaddr_in6 *)sa;
1194 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1196 * An IP6 address of 0 means listen to all
1197 * of the Ethernet multicast address used for IP6.
1198 * (This is used for multicast routers.)
1200 ifp->if_flags |= IFF_ALLMULTI;
1204 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1205 return EADDRNOTAVAIL;
1206 sdl = malloc(sizeof *sdl, M_IFMADDR,
1210 sdl->sdl_len = sizeof *sdl;
1211 sdl->sdl_family = AF_LINK;
1212 sdl->sdl_index = ifp->if_index;
1213 sdl->sdl_type = IFT_ETHER;
1214 sdl->sdl_alen = ETHER_ADDR_LEN;
1215 e_addr = LLADDR(sdl);
1216 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1217 *llsa = (struct sockaddr *)sdl;
1223 * Well, the text isn't quite right, but it's the name
1226 return EAFNOSUPPORT;
1231 ether_alloc(u_char type, struct ifnet *ifp)
1235 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1242 ether_free(void *com, u_char type)
1245 free(com, M_ARPCOM);
1249 ether_modevent(module_t mod, int type, void *data)
1254 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1257 if_deregister_com_alloc(IFT_ETHER);
1266 static moduledata_t ether_mod = {
1273 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1275 struct ether_vlan_header vlan;
1278 KASSERT((m->m_flags & M_VLANTAG) != 0,
1279 ("%s: vlan information not present", __func__));
1280 KASSERT(m->m_len >= sizeof(struct ether_header),
1281 ("%s: mbuf not large enough for header", __func__));
1282 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1283 vlan.evl_proto = vlan.evl_encap_proto;
1284 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1285 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1286 m->m_len -= sizeof(struct ether_header);
1287 m->m_data += sizeof(struct ether_header);
1289 * If a data link has been supplied by the caller, then we will need to
1290 * re-create a stack allocated mbuf chain with the following structure:
1292 * (1) mbuf #1 will contain the supplied data link
1293 * (2) mbuf #2 will contain the vlan header
1294 * (3) mbuf #3 will contain the original mbuf's packet data
1296 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1300 mv.m_data = (caddr_t)&vlan;
1301 mv.m_len = sizeof(vlan);
1307 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1308 m->m_len += sizeof(struct ether_header);
1309 m->m_data -= sizeof(struct ether_header);
1313 ether_vlanencap(struct mbuf *m, uint16_t tag)
1315 struct ether_vlan_header *evl;
1317 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1320 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1322 if (m->m_len < sizeof(*evl)) {
1323 m = m_pullup(m, sizeof(*evl));
1329 * Transform the Ethernet header into an Ethernet header
1330 * with 802.1Q encapsulation.
1332 evl = mtod(m, struct ether_vlan_header *);
1333 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1334 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1335 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1336 evl->evl_tag = htons(tag);
1340 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1341 MODULE_VERSION(ether, 1);