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_var.h>
74 #include <netinet/ip_fw.h>
75 #include <netinet/ipfw/ip_fw_private.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 (V_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;
471 /* fetch start point from rule, if any */
472 mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
476 /* dummynet packet, already partially processed */
477 struct ipfw_rule_ref *r;
479 /* XXX can we free it after use ? */
480 mtag->m_tag_id = PACKET_TAG_NONE;
481 r = (struct ipfw_rule_ref *)(mtag + 1);
482 if (r->info & IPFW_ONEPASS)
488 * I need some amt of data to be contiguous, and in case others need
489 * the packet (shared==1) also better be in the first mbuf.
492 i = min( m->m_pkthdr.len, max_protohdr);
493 if ( shared || m->m_len < i) {
500 eh = mtod(m, struct ether_header *);
501 save_eh = *eh; /* save copy for restore below */
502 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
504 args.m = m; /* the packet we are looking at */
505 args.oif = dst; /* destination, if any */
506 args.next_hop = NULL; /* we do not support forward yet */
507 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
508 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
509 i = V_ip_fw_chk_ptr(&args);
513 * Restore Ethernet header, as needed, in case the
514 * mbuf chain was replaced by ipfw.
516 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
521 if (eh != mtod(m, struct ether_header *))
522 bcopy(&save_eh, mtod(m, struct ether_header *),
527 if (i == IP_FW_DENY) /* drop */
530 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
532 if (i == IP_FW_PASS) /* a PASS rule. */
535 if (ip_dn_io_ptr && (i == IP_FW_DUMMYNET)) {
538 * Pass the pkt to dummynet, which consumes it.
539 * If shared, make a copy and keep the original.
542 m = m_copypacket(m, M_DONTWAIT);
547 * Pass the original to dummynet and
548 * nothing back to the caller
552 dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
553 ip_dn_io_ptr(&m, dir, &args);
557 * XXX at some point add support for divert/forward actions.
558 * If none of the above matches, we have to drop the pkt.
565 * Process a received Ethernet packet; the packet is in the
566 * mbuf chain m with the ethernet header at the front.
569 ether_input(struct ifnet *ifp, struct mbuf *m)
571 struct ether_header *eh;
574 if ((ifp->if_flags & IFF_UP) == 0) {
579 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
580 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
586 * Do consistency checks to verify assumptions
587 * made by code past this point.
589 if ((m->m_flags & M_PKTHDR) == 0) {
590 if_printf(ifp, "discard frame w/o packet header\n");
595 if (m->m_len < ETHER_HDR_LEN) {
596 /* XXX maybe should pullup? */
597 if_printf(ifp, "discard frame w/o leading ethernet "
598 "header (len %u pkt len %u)\n",
599 m->m_len, m->m_pkthdr.len);
604 eh = mtod(m, struct ether_header *);
605 etype = ntohs(eh->ether_type);
606 if (m->m_pkthdr.rcvif == NULL) {
607 if_printf(ifp, "discard frame w/o interface pointer\n");
613 if (m->m_pkthdr.rcvif != ifp) {
614 if_printf(ifp, "Warning, frame marked as received on %s\n",
615 m->m_pkthdr.rcvif->if_xname);
619 CURVNET_SET_QUIET(ifp->if_vnet);
621 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
622 if (ETHER_IS_BROADCAST(eh->ether_dhost))
623 m->m_flags |= M_BCAST;
625 m->m_flags |= M_MCAST;
631 * Tag the mbuf with an appropriate MAC label before any other
632 * consumers can get to it.
634 mac_ifnet_create_mbuf(ifp, m);
638 * Give bpf a chance at the packet.
640 ETHER_BPF_MTAP(ifp, m);
643 * If the CRC is still on the packet, trim it off. We do this once
644 * and once only in case we are re-entered. Nothing else on the
645 * Ethernet receive path expects to see the FCS.
647 if (m->m_flags & M_HASFCS) {
648 m_adj(m, -ETHER_CRC_LEN);
649 m->m_flags &= ~M_HASFCS;
652 ifp->if_ibytes += m->m_pkthdr.len;
654 /* Allow monitor mode to claim this frame, after stats are updated. */
655 if (ifp->if_flags & IFF_MONITOR) {
661 /* Handle input from a lagg(4) port */
662 if (ifp->if_type == IFT_IEEE8023ADLAG) {
663 KASSERT(lagg_input_p != NULL,
664 ("%s: if_lagg not loaded!", __func__));
665 m = (*lagg_input_p)(ifp, m);
667 ifp = m->m_pkthdr.rcvif;
673 * If the hardware did not process an 802.1Q tag, do this now,
674 * to allow 802.1P priority frames to be passed to the main input
676 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
678 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
679 struct ether_vlan_header *evl;
681 if (m->m_len < sizeof(*evl) &&
682 (m = m_pullup(m, sizeof(*evl))) == NULL) {
684 if_printf(ifp, "cannot pullup VLAN header\n");
691 evl = mtod(m, struct ether_vlan_header *);
692 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
693 m->m_flags |= M_VLANTAG;
695 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
696 ETHER_HDR_LEN - ETHER_TYPE_LEN);
697 m_adj(m, ETHER_VLAN_ENCAP_LEN);
700 /* Allow ng_ether(4) to claim this frame. */
701 if (IFP2AC(ifp)->ac_netgraph != NULL) {
702 KASSERT(ng_ether_input_p != NULL,
703 ("%s: ng_ether_input_p is NULL", __func__));
704 m->m_flags &= ~M_PROMISC;
705 (*ng_ether_input_p)(ifp, &m);
713 * Allow if_bridge(4) to claim this frame.
714 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
715 * and the frame should be delivered locally.
717 if (ifp->if_bridge != NULL) {
718 m->m_flags &= ~M_PROMISC;
719 BRIDGE_INPUT(ifp, m);
726 #if defined(INET) || defined(INET6)
729 * Clear M_PROMISC on frame so that carp(4) will see it when the
730 * mbuf flows up to Layer 3.
731 * FreeBSD's implementation of carp(4) uses the inprotosw
732 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
733 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
734 * is outside the scope of the M_PROMISC test below.
735 * TODO: Maintain a hash table of ethernet addresses other than
736 * ether_dhost which may be active on this ifp.
738 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
739 m->m_flags &= ~M_PROMISC;
745 * If the frame received was not for our MAC address, set the
746 * M_PROMISC flag on the mbuf chain. The frame may need to
747 * be seen by the rest of the Ethernet input path in case of
748 * re-entry (e.g. bridge, vlan, netgraph) but should not be
749 * seen by upper protocol layers.
751 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
752 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
753 m->m_flags |= M_PROMISC;
756 /* First chunk of an mbuf contains good entropy */
757 if (harvest.ethernet)
758 random_harvest(m, 16, 3, 0, RANDOM_NET);
765 * Upper layer processing for a received Ethernet packet.
768 ether_demux(struct ifnet *ifp, struct mbuf *m)
770 struct ether_header *eh;
773 #if defined(NETATALK)
777 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
779 #if defined(INET) || defined(INET6)
781 * Allow dummynet and/or ipfw to claim the frame.
782 * Do not do this for PROMISC frames in case we are re-entered.
784 if (V_ip_fw_chk_ptr && V_ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
785 if (ether_ipfw_chk(&m, NULL, 0) == 0) {
787 m_freem(m); /* dropped; free mbuf chain */
788 return; /* consumed */
792 eh = mtod(m, struct ether_header *);
793 ether_type = ntohs(eh->ether_type);
796 * If this frame has a VLAN tag other than 0, call vlan_input()
797 * if its module is loaded. Otherwise, drop.
799 if ((m->m_flags & M_VLANTAG) &&
800 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
801 if (ifp->if_vlantrunk == NULL) {
806 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
808 /* Clear before possibly re-entering ether_input(). */
809 m->m_flags &= ~M_PROMISC;
810 (*vlan_input_p)(ifp, m);
815 * Pass promiscuously received frames to the upper layer if the user
816 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
818 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
824 * Reset layer specific mbuf flags to avoid confusing upper layers.
825 * Strip off Ethernet header.
827 m->m_flags &= ~M_VLANTAG;
828 m->m_flags &= ~(M_PROTOFLAGS);
829 m_adj(m, ETHER_HDR_LEN);
832 * Dispatch frame to upper layer.
834 switch (ether_type) {
837 if ((m = ip_fastforward(m)) == NULL)
843 if (ifp->if_flags & IFF_NOARP) {
844 /* Discard packet if ARP is disabled on interface */
853 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
870 #endif /* NETATALK */
873 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
876 #if defined(NETATALK)
877 if (ether_type > ETHERMTU)
879 l = mtod(m, struct llc *);
880 if (l->llc_dsap == LLC_SNAP_LSAP &&
881 l->llc_ssap == LLC_SNAP_LSAP &&
882 l->llc_control == LLC_UI) {
883 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
884 sizeof(at_org_code)) == 0 &&
885 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
886 m_adj(m, LLC_SNAPFRAMELEN);
890 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
891 sizeof(aarp_org_code)) == 0 &&
892 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
893 m_adj(m, LLC_SNAPFRAMELEN);
898 #endif /* NETATALK */
901 netisr_dispatch(isr, m);
906 * Packet is to be discarded. If netgraph is present,
907 * hand the packet to it for last chance processing;
908 * otherwise dispose of it.
910 if (IFP2AC(ifp)->ac_netgraph != NULL) {
911 KASSERT(ng_ether_input_orphan_p != NULL,
912 ("ng_ether_input_orphan_p is NULL"));
914 * Put back the ethernet header so netgraph has a
915 * consistent view of inbound packets.
917 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
918 (*ng_ether_input_orphan_p)(ifp, m);
925 * Convert Ethernet address to printable (loggable) representation.
926 * This routine is for compatibility; it's better to just use
928 * printf("%6D", <pointer to address>, ":");
930 * since there's no static buffer involved.
933 ether_sprintf(const u_char *ap)
935 static char etherbuf[18];
936 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
941 * Perform common duties while attaching to interface list
944 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
948 struct sockaddr_dl *sdl;
950 ifp->if_addrlen = ETHER_ADDR_LEN;
951 ifp->if_hdrlen = ETHER_HDR_LEN;
953 ifp->if_mtu = ETHERMTU;
954 ifp->if_output = ether_output;
955 ifp->if_input = ether_input;
956 ifp->if_resolvemulti = ether_resolvemulti;
957 if (ifp->if_baudrate == 0)
958 ifp->if_baudrate = IF_Mbps(10); /* just a default */
959 ifp->if_broadcastaddr = etherbroadcastaddr;
962 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
963 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
964 sdl->sdl_type = IFT_ETHER;
965 sdl->sdl_alen = ifp->if_addrlen;
966 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
968 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
969 if (ng_ether_attach_p != NULL)
970 (*ng_ether_attach_p)(ifp);
972 /* Announce Ethernet MAC address if non-zero. */
973 for (i = 0; i < ifp->if_addrlen; i++)
976 if (i != ifp->if_addrlen)
977 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
981 * Perform common duties while detaching an Ethernet interface
984 ether_ifdetach(struct ifnet *ifp)
986 if (IFP2AC(ifp)->ac_netgraph != NULL) {
987 KASSERT(ng_ether_detach_p != NULL,
988 ("ng_ether_detach_p is NULL"));
989 (*ng_ether_detach_p)(ifp);
996 SYSCTL_DECL(_net_link);
997 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
998 #if defined(INET) || defined(INET6)
999 SYSCTL_VNET_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
1000 &VNET_NAME(ether_ipfw), 0, "Pass ether pkts through firewall");
1005 * This is for reference. We have a table-driven version
1006 * of the little-endian crc32 generator, which is faster
1007 * than the double-loop.
1010 ether_crc32_le(const uint8_t *buf, size_t len)
1017 crc = 0xffffffff; /* initial value */
1019 for (i = 0; i < len; i++) {
1020 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1021 carry = (crc ^ data) & 1;
1024 crc = (crc ^ ETHER_CRC_POLY_LE);
1032 ether_crc32_le(const uint8_t *buf, size_t len)
1034 static const uint32_t crctab[] = {
1035 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1036 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1037 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1038 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1043 crc = 0xffffffff; /* initial value */
1045 for (i = 0; i < len; i++) {
1047 crc = (crc >> 4) ^ crctab[crc & 0xf];
1048 crc = (crc >> 4) ^ crctab[crc & 0xf];
1056 ether_crc32_be(const uint8_t *buf, size_t len)
1059 uint32_t crc, carry;
1063 crc = 0xffffffff; /* initial value */
1065 for (i = 0; i < len; i++) {
1066 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1067 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1070 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1078 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1080 struct ifaddr *ifa = (struct ifaddr *) data;
1081 struct ifreq *ifr = (struct ifreq *) data;
1086 ifp->if_flags |= IFF_UP;
1088 switch (ifa->ifa_addr->sa_family) {
1091 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1092 arp_ifinit(ifp, ifa);
1097 * XXX - This code is probably wrong
1101 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1103 if (ipx_nullhost(*ina))
1108 bcopy((caddr_t) ina->x_host.c_host,
1109 (caddr_t) IF_LLADDR(ifp),
1116 ifp->if_init(ifp->if_softc);
1121 ifp->if_init(ifp->if_softc);
1128 struct sockaddr *sa;
1130 sa = (struct sockaddr *) & ifr->ifr_data;
1131 bcopy(IF_LLADDR(ifp),
1132 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1138 * Set the interface MTU.
1140 if (ifr->ifr_mtu > ETHERMTU) {
1143 ifp->if_mtu = ifr->ifr_mtu;
1147 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1154 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1155 struct sockaddr *sa)
1157 struct sockaddr_dl *sdl;
1159 struct sockaddr_in *sin;
1162 struct sockaddr_in6 *sin6;
1166 switch(sa->sa_family) {
1169 * No mapping needed. Just check that it's a valid MC address.
1171 sdl = (struct sockaddr_dl *)sa;
1172 e_addr = LLADDR(sdl);
1173 if (!ETHER_IS_MULTICAST(e_addr))
1174 return EADDRNOTAVAIL;
1180 sin = (struct sockaddr_in *)sa;
1181 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1182 return EADDRNOTAVAIL;
1183 sdl = malloc(sizeof *sdl, M_IFMADDR,
1187 sdl->sdl_len = sizeof *sdl;
1188 sdl->sdl_family = AF_LINK;
1189 sdl->sdl_index = ifp->if_index;
1190 sdl->sdl_type = IFT_ETHER;
1191 sdl->sdl_alen = ETHER_ADDR_LEN;
1192 e_addr = LLADDR(sdl);
1193 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1194 *llsa = (struct sockaddr *)sdl;
1199 sin6 = (struct sockaddr_in6 *)sa;
1200 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1202 * An IP6 address of 0 means listen to all
1203 * of the Ethernet multicast address used for IP6.
1204 * (This is used for multicast routers.)
1206 ifp->if_flags |= IFF_ALLMULTI;
1210 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1211 return EADDRNOTAVAIL;
1212 sdl = malloc(sizeof *sdl, M_IFMADDR,
1216 sdl->sdl_len = sizeof *sdl;
1217 sdl->sdl_family = AF_LINK;
1218 sdl->sdl_index = ifp->if_index;
1219 sdl->sdl_type = IFT_ETHER;
1220 sdl->sdl_alen = ETHER_ADDR_LEN;
1221 e_addr = LLADDR(sdl);
1222 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1223 *llsa = (struct sockaddr *)sdl;
1229 * Well, the text isn't quite right, but it's the name
1232 return EAFNOSUPPORT;
1237 ether_alloc(u_char type, struct ifnet *ifp)
1241 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1248 ether_free(void *com, u_char type)
1251 free(com, M_ARPCOM);
1255 ether_modevent(module_t mod, int type, void *data)
1260 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1263 if_deregister_com_alloc(IFT_ETHER);
1272 static moduledata_t ether_mod = {
1279 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1281 struct ether_vlan_header vlan;
1284 KASSERT((m->m_flags & M_VLANTAG) != 0,
1285 ("%s: vlan information not present", __func__));
1286 KASSERT(m->m_len >= sizeof(struct ether_header),
1287 ("%s: mbuf not large enough for header", __func__));
1288 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1289 vlan.evl_proto = vlan.evl_encap_proto;
1290 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1291 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1292 m->m_len -= sizeof(struct ether_header);
1293 m->m_data += sizeof(struct ether_header);
1295 * If a data link has been supplied by the caller, then we will need to
1296 * re-create a stack allocated mbuf chain with the following structure:
1298 * (1) mbuf #1 will contain the supplied data link
1299 * (2) mbuf #2 will contain the vlan header
1300 * (3) mbuf #3 will contain the original mbuf's packet data
1302 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1306 mv.m_data = (caddr_t)&vlan;
1307 mv.m_len = sizeof(vlan);
1313 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1314 m->m_len += sizeof(struct ether_header);
1315 m->m_data -= sizeof(struct ether_header);
1319 ether_vlanencap(struct mbuf *m, uint16_t tag)
1321 struct ether_vlan_header *evl;
1323 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1326 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1328 if (m->m_len < sizeof(*evl)) {
1329 m = m_pullup(m, sizeof(*evl));
1335 * Transform the Ethernet header into an Ethernet header
1336 * with 802.1Q encapsulation.
1338 evl = mtod(m, struct ether_vlan_header *);
1339 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1340 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1341 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1342 evl->evl_tag = htons(tag);
1346 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1347 MODULE_VERSION(ether, 1);