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"
38 #include "opt_mbuf_profiling.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
47 #include <sys/random.h>
48 #include <sys/rwlock.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
61 #include <net/ethernet.h>
62 #include <net/if_bridgevar.h>
63 #include <net/if_vlan_var.h>
64 #include <net/if_llatbl.h>
65 #include <net/pf_mtag.h>
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/ip_carp.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>
82 #include <netipx/ipx.h>
83 #include <netipx/ipx_if.h>
86 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
87 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
88 struct sockaddr *dst, short *tp, int *hlen);
91 #include <netatalk/at.h>
92 #include <netatalk/at_var.h>
93 #include <netatalk/at_extern.h>
95 #define llc_snap_org_code llc_un.type_snap.org_code
96 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 extern u_char at_org_code[3];
99 extern u_char aarp_org_code[3];
100 #endif /* NETATALK */
102 #include <security/mac/mac_framework.h>
105 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
106 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
109 /* netgraph node hooks for ng_ether(4) */
110 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
111 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
112 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
113 void (*ng_ether_attach_p)(struct ifnet *ifp);
114 void (*ng_ether_detach_p)(struct ifnet *ifp);
116 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
118 /* if_bridge(4) support */
119 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
120 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
121 struct sockaddr *, struct rtentry *);
122 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
124 /* if_lagg(4) support */
125 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
127 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
128 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
130 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
133 static void ether_reassign(struct ifnet *, struct vnet *, char *);
136 /* XXX: should be in an arp support file, not here */
137 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
139 #define ETHER_IS_BROADCAST(addr) \
140 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
142 #define senderr(e) do { error = (e); goto bad;} while (0)
144 #if defined(INET) || defined(INET6)
146 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared);
147 static VNET_DEFINE(int, ether_ipfw);
148 #define V_ether_ipfw VNET(ether_ipfw)
153 * Ethernet output routine.
154 * Encapsulate a packet of type family for the local net.
155 * Use trailer local net encapsulation if enough data in first
156 * packet leaves a multiple of 512 bytes of data in remainder.
159 ether_output(struct ifnet *ifp, struct mbuf *m,
160 struct sockaddr *dst, struct route *ro)
163 int error = 0, hdrcmplt = 0;
164 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
165 struct llentry *lle = NULL;
166 struct rtentry *rt0 = NULL;
167 struct ether_header *eh;
170 int hlen; /* link layer header length */
173 if (!(m->m_flags & (M_BCAST | M_MCAST)))
178 error = mac_ifnet_check_transmit(ifp, m);
184 if (ifp->if_flags & IFF_MONITOR)
186 if (!((ifp->if_flags & IFF_UP) &&
187 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
190 hlen = ETHER_HDR_LEN;
191 switch (dst->sa_family) {
194 if (lle != NULL && (lle->la_flags & LLE_VALID))
195 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
197 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
199 return (error == EWOULDBLOCK ? 0 : error);
200 type = htons(ETHERTYPE_IP);
205 ah = mtod(m, struct arphdr *);
206 ah->ar_hrd = htons(ARPHRD_ETHER);
208 loop_copy = 0; /* if this is for us, don't do it */
210 switch(ntohs(ah->ar_op)) {
211 case ARPOP_REVREQUEST:
213 type = htons(ETHERTYPE_REVARP);
218 type = htons(ETHERTYPE_ARP);
222 if (m->m_flags & M_BCAST)
223 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
225 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
232 if (lle != NULL && (lle->la_flags & LLE_VALID))
233 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
235 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
238 type = htons(ETHERTYPE_IPV6);
244 error = ef_outputp(ifp, &m, dst, &type, &hlen);
248 type = htons(ETHERTYPE_IPX);
249 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
250 (caddr_t)edst, sizeof (edst));
256 struct at_ifaddr *aa;
258 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
259 senderr(EHOSTUNREACH); /* XXX */
260 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
261 ifa_free(&aa->aa_ifa);
265 * In the phase 2 case, need to prepend an mbuf for the llc header.
267 if ( aa->aa_flags & AFA_PHASE2 ) {
270 ifa_free(&aa->aa_ifa);
271 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
274 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
275 llc.llc_control = LLC_UI;
276 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
277 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
278 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
279 type = htons(m->m_pkthdr.len);
280 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
282 ifa_free(&aa->aa_ifa);
283 type = htons(ETHERTYPE_AT);
287 #endif /* NETATALK */
289 case pseudo_AF_HDRCMPLT:
291 eh = (struct ether_header *)dst->sa_data;
292 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
296 loop_copy = 0; /* if this is for us, don't do it */
297 eh = (struct ether_header *)dst->sa_data;
298 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
299 type = eh->ether_type;
303 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
304 senderr(EAFNOSUPPORT);
307 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
309 if (m->m_pkthdr.csum_flags & CSUM_IP)
310 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
311 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
312 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
313 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
314 csum_flags |= CSUM_SCTP_VALID;
315 m->m_pkthdr.csum_flags |= csum_flags;
316 m->m_pkthdr.csum_data = 0xffff;
317 return (if_simloop(ifp, m, dst->sa_family, 0));
321 * Add local net header. If no space in first mbuf,
324 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
327 eh = mtod(m, struct ether_header *);
328 (void)memcpy(&eh->ether_type, &type,
329 sizeof(eh->ether_type));
330 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
332 (void)memcpy(eh->ether_shost, esrc,
333 sizeof(eh->ether_shost));
335 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
336 sizeof(eh->ether_shost));
339 * If a simplex interface, and the packet is being sent to our
340 * Ethernet address or a broadcast address, loopback a copy.
341 * XXX To make a simplex device behave exactly like a duplex
342 * device, we should copy in the case of sending to our own
343 * ethernet address (thus letting the original actually appear
344 * on the wire). However, we don't do that here for security
345 * reasons and compatibility with the original behavior.
347 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
348 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
351 if (m->m_pkthdr.csum_flags & CSUM_IP)
352 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
353 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
354 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
355 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
356 csum_flags |= CSUM_SCTP_VALID;
358 if (m->m_flags & M_BCAST) {
362 * Because if_simloop() modifies the packet, we need a
363 * writable copy through m_dup() instead of a readonly
364 * one as m_copy[m] would give us. The alternative would
365 * be to modify if_simloop() to handle the readonly mbuf,
366 * but performancewise it is mostly equivalent (trading
367 * extra data copying vs. extra locking).
369 * XXX This is a local workaround. A number of less
370 * often used kernel parts suffer from the same bug.
371 * See PR kern/105943 for a proposed general solution.
373 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
374 n->m_pkthdr.csum_flags |= csum_flags;
375 if (csum_flags & CSUM_DATA_VALID)
376 n->m_pkthdr.csum_data = 0xffff;
377 (void)if_simloop(ifp, n, dst->sa_family, hlen);
380 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
381 ETHER_ADDR_LEN) == 0) {
382 m->m_pkthdr.csum_flags |= csum_flags;
383 if (csum_flags & CSUM_DATA_VALID)
384 m->m_pkthdr.csum_data = 0xffff;
385 (void) if_simloop(ifp, m, dst->sa_family, hlen);
386 return (0); /* XXX */
391 * Bridges require special output handling.
393 if (ifp->if_bridge) {
394 BRIDGE_OUTPUT(ifp, m, error);
398 #if defined(INET) || defined(INET6)
400 (error = (*carp_output_p)(ifp, m, dst, NULL)))
404 /* Handle ng_ether(4) processing, if any */
405 if (IFP2AC(ifp)->ac_netgraph != NULL) {
406 KASSERT(ng_ether_output_p != NULL,
407 ("ng_ether_output_p is NULL"));
408 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
417 /* Continue with link-layer output */
418 return ether_output_frame(ifp, m);
422 * Ethernet link layer output routine to send a raw frame to the device.
424 * This assumes that the 14 byte Ethernet header is present and contiguous
425 * in the first mbuf (if BRIDGE'ing).
428 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
430 #if defined(INET) || defined(INET6)
432 if (V_ip_fw_chk_ptr && V_ether_ipfw != 0) {
433 if (ether_ipfw_chk(&m, ifp, 0) == 0) {
436 return EACCES; /* pkt dropped */
438 return 0; /* consumed e.g. in a pipe */
444 * Queue message on interface, update output statistics if
445 * successful, and start output if interface not yet active.
447 return ((ifp->if_transmit)(ifp, m));
450 #if defined(INET) || defined(INET6)
452 * ipfw processing for ethernet packets (in and out).
453 * The second parameter is NULL from ether_demux, and ifp from
454 * ether_output_frame.
457 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared)
459 struct ether_header *eh;
460 struct ether_header save_eh;
463 struct ip_fw_args args;
466 /* fetch start point from rule, if any */
467 mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
471 /* dummynet packet, already partially processed */
472 struct ipfw_rule_ref *r;
474 /* XXX can we free it after use ? */
475 mtag->m_tag_id = PACKET_TAG_NONE;
476 r = (struct ipfw_rule_ref *)(mtag + 1);
477 if (r->info & IPFW_ONEPASS)
483 * I need some amt of data to be contiguous, and in case others need
484 * the packet (shared==1) also better be in the first mbuf.
487 i = min( m->m_pkthdr.len, max_protohdr);
488 if ( shared || m->m_len < i) {
495 eh = mtod(m, struct ether_header *);
496 save_eh = *eh; /* save copy for restore below */
497 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
499 args.m = m; /* the packet we are looking at */
500 args.oif = dst; /* destination, if any */
501 args.next_hop = NULL; /* we do not support forward yet */
502 args.next_hop6 = 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 = V_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)) {
534 * Pass the pkt to dummynet, which consumes it.
535 * If shared, make a copy and keep the original.
538 m = m_copypacket(m, M_DONTWAIT);
543 * Pass the original to dummynet and
544 * nothing back to the caller
548 dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
549 ip_dn_io_ptr(&m, dir, &args);
553 * XXX at some point add support for divert/forward actions.
554 * If none of the above matches, we have to drop the pkt.
561 * Process a received Ethernet packet; the packet is in the
562 * mbuf chain m with the ethernet header at the front.
565 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
567 struct ether_header *eh;
570 if ((ifp->if_flags & IFF_UP) == 0) {
575 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
576 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
582 * Do consistency checks to verify assumptions
583 * made by code past this point.
585 if ((m->m_flags & M_PKTHDR) == 0) {
586 if_printf(ifp, "discard frame w/o packet header\n");
591 if (m->m_len < ETHER_HDR_LEN) {
592 /* XXX maybe should pullup? */
593 if_printf(ifp, "discard frame w/o leading ethernet "
594 "header (len %u pkt len %u)\n",
595 m->m_len, m->m_pkthdr.len);
600 eh = mtod(m, struct ether_header *);
601 etype = ntohs(eh->ether_type);
602 if (m->m_pkthdr.rcvif == NULL) {
603 if_printf(ifp, "discard frame w/o interface pointer\n");
609 if (m->m_pkthdr.rcvif != ifp) {
610 if_printf(ifp, "Warning, frame marked as received on %s\n",
611 m->m_pkthdr.rcvif->if_xname);
615 CURVNET_SET_QUIET(ifp->if_vnet);
617 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
618 if (ETHER_IS_BROADCAST(eh->ether_dhost))
619 m->m_flags |= M_BCAST;
621 m->m_flags |= M_MCAST;
627 * Tag the mbuf with an appropriate MAC label before any other
628 * consumers can get to it.
630 mac_ifnet_create_mbuf(ifp, m);
634 * Give bpf a chance at the packet.
636 ETHER_BPF_MTAP(ifp, m);
639 * If the CRC is still on the packet, trim it off. We do this once
640 * and once only in case we are re-entered. Nothing else on the
641 * Ethernet receive path expects to see the FCS.
643 if (m->m_flags & M_HASFCS) {
644 m_adj(m, -ETHER_CRC_LEN);
645 m->m_flags &= ~M_HASFCS;
648 ifp->if_ibytes += m->m_pkthdr.len;
650 /* Allow monitor mode to claim this frame, after stats are updated. */
651 if (ifp->if_flags & IFF_MONITOR) {
657 /* Handle input from a lagg(4) port */
658 if (ifp->if_type == IFT_IEEE8023ADLAG) {
659 KASSERT(lagg_input_p != NULL,
660 ("%s: if_lagg not loaded!", __func__));
661 m = (*lagg_input_p)(ifp, m);
663 ifp = m->m_pkthdr.rcvif;
669 * If the hardware did not process an 802.1Q tag, do this now,
670 * to allow 802.1P priority frames to be passed to the main input
672 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
674 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
675 struct ether_vlan_header *evl;
677 if (m->m_len < sizeof(*evl) &&
678 (m = m_pullup(m, sizeof(*evl))) == NULL) {
680 if_printf(ifp, "cannot pullup VLAN header\n");
687 evl = mtod(m, struct ether_vlan_header *);
688 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
689 m->m_flags |= M_VLANTAG;
691 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
692 ETHER_HDR_LEN - ETHER_TYPE_LEN);
693 m_adj(m, ETHER_VLAN_ENCAP_LEN);
696 M_SETFIB(m, ifp->if_fib);
698 /* Allow ng_ether(4) to claim this frame. */
699 if (IFP2AC(ifp)->ac_netgraph != NULL) {
700 KASSERT(ng_ether_input_p != NULL,
701 ("%s: ng_ether_input_p is NULL", __func__));
702 m->m_flags &= ~M_PROMISC;
703 (*ng_ether_input_p)(ifp, &m);
711 * Allow if_bridge(4) to claim this frame.
712 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
713 * and the frame should be delivered locally.
715 if (ifp->if_bridge != NULL) {
716 m->m_flags &= ~M_PROMISC;
717 BRIDGE_INPUT(ifp, m);
724 #if defined(INET) || defined(INET6)
726 * Clear M_PROMISC on frame so that carp(4) will see it when the
727 * mbuf flows up to Layer 3.
728 * FreeBSD's implementation of carp(4) uses the inprotosw
729 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
730 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
731 * is outside the scope of the M_PROMISC test below.
732 * TODO: Maintain a hash table of ethernet addresses other than
733 * ether_dhost which may be active on this ifp.
735 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
736 m->m_flags &= ~M_PROMISC;
741 * If the frame received was not for our MAC address, set the
742 * M_PROMISC flag on the mbuf chain. The frame may need to
743 * be seen by the rest of the Ethernet input path in case of
744 * re-entry (e.g. bridge, vlan, netgraph) but should not be
745 * seen by upper protocol layers.
747 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
748 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
749 m->m_flags |= M_PROMISC;
752 /* First chunk of an mbuf contains good entropy */
753 if (harvest.ethernet)
754 random_harvest(m, 16, 3, 0, RANDOM_NET);
761 * Ethernet input dispatch; by default, direct dispatch here regardless of
762 * global configuration.
765 ether_nh_input(struct mbuf *m)
768 ether_input_internal(m->m_pkthdr.rcvif, m);
771 static struct netisr_handler ether_nh = {
773 .nh_handler = ether_nh_input,
774 .nh_proto = NETISR_ETHER,
775 .nh_policy = NETISR_POLICY_SOURCE,
776 .nh_dispatch = NETISR_DISPATCH_DIRECT,
780 ether_init(__unused void *arg)
783 netisr_register(ðer_nh);
785 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
788 ether_input(struct ifnet *ifp, struct mbuf *m)
792 * We will rely on rcvif being set properly in the deferred context,
793 * so assert it is correct here.
795 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
797 netisr_dispatch(NETISR_ETHER, m);
801 * Upper layer processing for a received Ethernet packet.
804 ether_demux(struct ifnet *ifp, struct mbuf *m)
806 struct ether_header *eh;
809 #if defined(NETATALK)
813 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
815 #if defined(INET) || defined(INET6)
817 * Allow dummynet and/or ipfw to claim the frame.
818 * Do not do this for PROMISC frames in case we are re-entered.
820 if (V_ip_fw_chk_ptr && V_ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
821 if (ether_ipfw_chk(&m, NULL, 0) == 0) {
823 m_freem(m); /* dropped; free mbuf chain */
824 return; /* consumed */
828 eh = mtod(m, struct ether_header *);
829 ether_type = ntohs(eh->ether_type);
832 * If this frame has a VLAN tag other than 0, call vlan_input()
833 * if its module is loaded. Otherwise, drop.
835 if ((m->m_flags & M_VLANTAG) &&
836 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
837 if (ifp->if_vlantrunk == NULL) {
842 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
844 /* Clear before possibly re-entering ether_input(). */
845 m->m_flags &= ~M_PROMISC;
846 (*vlan_input_p)(ifp, m);
851 * Pass promiscuously received frames to the upper layer if the user
852 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
854 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
860 * Reset layer specific mbuf flags to avoid confusing upper layers.
861 * Strip off Ethernet header.
863 m->m_flags &= ~M_VLANTAG;
864 m->m_flags &= ~(M_PROTOFLAGS);
865 m_adj(m, ETHER_HDR_LEN);
868 * Dispatch frame to upper layer.
870 switch (ether_type) {
873 if ((m = ip_fastforward(m)) == NULL)
879 if (ifp->if_flags & IFF_NOARP) {
880 /* Discard packet if ARP is disabled on interface */
889 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
906 #endif /* NETATALK */
909 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
912 #if defined(NETATALK)
913 if (ether_type > ETHERMTU)
915 l = mtod(m, struct llc *);
916 if (l->llc_dsap == LLC_SNAP_LSAP &&
917 l->llc_ssap == LLC_SNAP_LSAP &&
918 l->llc_control == LLC_UI) {
919 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
920 sizeof(at_org_code)) == 0 &&
921 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
922 m_adj(m, LLC_SNAPFRAMELEN);
926 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
927 sizeof(aarp_org_code)) == 0 &&
928 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
929 m_adj(m, LLC_SNAPFRAMELEN);
934 #endif /* NETATALK */
937 netisr_dispatch(isr, m);
942 * Packet is to be discarded. If netgraph is present,
943 * hand the packet to it for last chance processing;
944 * otherwise dispose of it.
946 if (IFP2AC(ifp)->ac_netgraph != NULL) {
947 KASSERT(ng_ether_input_orphan_p != NULL,
948 ("ng_ether_input_orphan_p is NULL"));
950 * Put back the ethernet header so netgraph has a
951 * consistent view of inbound packets.
953 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
954 (*ng_ether_input_orphan_p)(ifp, m);
961 * Convert Ethernet address to printable (loggable) representation.
962 * This routine is for compatibility; it's better to just use
964 * printf("%6D", <pointer to address>, ":");
966 * since there's no static buffer involved.
969 ether_sprintf(const u_char *ap)
971 static char etherbuf[18];
972 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
977 * Perform common duties while attaching to interface list
980 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
984 struct sockaddr_dl *sdl;
986 ifp->if_addrlen = ETHER_ADDR_LEN;
987 ifp->if_hdrlen = ETHER_HDR_LEN;
989 ifp->if_mtu = ETHERMTU;
990 ifp->if_output = ether_output;
991 ifp->if_input = ether_input;
992 ifp->if_resolvemulti = ether_resolvemulti;
994 ifp->if_reassign = ether_reassign;
996 if (ifp->if_baudrate == 0)
997 ifp->if_baudrate = IF_Mbps(10); /* just a default */
998 ifp->if_broadcastaddr = etherbroadcastaddr;
1001 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
1002 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1003 sdl->sdl_type = IFT_ETHER;
1004 sdl->sdl_alen = ifp->if_addrlen;
1005 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
1007 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
1008 if (ng_ether_attach_p != NULL)
1009 (*ng_ether_attach_p)(ifp);
1011 /* Announce Ethernet MAC address if non-zero. */
1012 for (i = 0; i < ifp->if_addrlen; i++)
1015 if (i != ifp->if_addrlen)
1016 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
1020 * Perform common duties while detaching an Ethernet interface
1023 ether_ifdetach(struct ifnet *ifp)
1025 if (IFP2AC(ifp)->ac_netgraph != NULL) {
1026 KASSERT(ng_ether_detach_p != NULL,
1027 ("ng_ether_detach_p is NULL"));
1028 (*ng_ether_detach_p)(ifp);
1037 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
1040 if (IFP2AC(ifp)->ac_netgraph != NULL) {
1041 KASSERT(ng_ether_detach_p != NULL,
1042 ("ng_ether_detach_p is NULL"));
1043 (*ng_ether_detach_p)(ifp);
1046 if (ng_ether_attach_p != NULL) {
1047 CURVNET_SET_QUIET(new_vnet);
1048 (*ng_ether_attach_p)(ifp);
1054 SYSCTL_DECL(_net_link);
1055 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
1056 #if defined(INET) || defined(INET6)
1057 SYSCTL_VNET_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
1058 &VNET_NAME(ether_ipfw), 0, "Pass ether pkts through firewall");
1063 * This is for reference. We have a table-driven version
1064 * of the little-endian crc32 generator, which is faster
1065 * than the double-loop.
1068 ether_crc32_le(const uint8_t *buf, size_t len)
1075 crc = 0xffffffff; /* initial value */
1077 for (i = 0; i < len; i++) {
1078 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1079 carry = (crc ^ data) & 1;
1082 crc = (crc ^ ETHER_CRC_POLY_LE);
1090 ether_crc32_le(const uint8_t *buf, size_t len)
1092 static const uint32_t crctab[] = {
1093 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1094 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1095 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1096 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1101 crc = 0xffffffff; /* initial value */
1103 for (i = 0; i < len; i++) {
1105 crc = (crc >> 4) ^ crctab[crc & 0xf];
1106 crc = (crc >> 4) ^ crctab[crc & 0xf];
1114 ether_crc32_be(const uint8_t *buf, size_t len)
1117 uint32_t crc, carry;
1121 crc = 0xffffffff; /* initial value */
1123 for (i = 0; i < len; i++) {
1124 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1125 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1128 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1136 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1138 struct ifaddr *ifa = (struct ifaddr *) data;
1139 struct ifreq *ifr = (struct ifreq *) data;
1144 ifp->if_flags |= IFF_UP;
1146 switch (ifa->ifa_addr->sa_family) {
1149 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1150 arp_ifinit(ifp, ifa);
1155 * XXX - This code is probably wrong
1159 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1161 if (ipx_nullhost(*ina))
1166 bcopy((caddr_t) ina->x_host.c_host,
1167 (caddr_t) IF_LLADDR(ifp),
1174 ifp->if_init(ifp->if_softc);
1179 ifp->if_init(ifp->if_softc);
1186 struct sockaddr *sa;
1188 sa = (struct sockaddr *) & ifr->ifr_data;
1189 bcopy(IF_LLADDR(ifp),
1190 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1196 * Set the interface MTU.
1198 if (ifr->ifr_mtu > ETHERMTU) {
1201 ifp->if_mtu = ifr->ifr_mtu;
1205 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1212 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1213 struct sockaddr *sa)
1215 struct sockaddr_dl *sdl;
1217 struct sockaddr_in *sin;
1220 struct sockaddr_in6 *sin6;
1224 switch(sa->sa_family) {
1227 * No mapping needed. Just check that it's a valid MC address.
1229 sdl = (struct sockaddr_dl *)sa;
1230 e_addr = LLADDR(sdl);
1231 if (!ETHER_IS_MULTICAST(e_addr))
1232 return EADDRNOTAVAIL;
1238 sin = (struct sockaddr_in *)sa;
1239 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1240 return EADDRNOTAVAIL;
1241 sdl = malloc(sizeof *sdl, M_IFMADDR,
1245 sdl->sdl_len = sizeof *sdl;
1246 sdl->sdl_family = AF_LINK;
1247 sdl->sdl_index = ifp->if_index;
1248 sdl->sdl_type = IFT_ETHER;
1249 sdl->sdl_alen = ETHER_ADDR_LEN;
1250 e_addr = LLADDR(sdl);
1251 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1252 *llsa = (struct sockaddr *)sdl;
1257 sin6 = (struct sockaddr_in6 *)sa;
1258 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1260 * An IP6 address of 0 means listen to all
1261 * of the Ethernet multicast address used for IP6.
1262 * (This is used for multicast routers.)
1264 ifp->if_flags |= IFF_ALLMULTI;
1268 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1269 return EADDRNOTAVAIL;
1270 sdl = malloc(sizeof *sdl, M_IFMADDR,
1274 sdl->sdl_len = sizeof *sdl;
1275 sdl->sdl_family = AF_LINK;
1276 sdl->sdl_index = ifp->if_index;
1277 sdl->sdl_type = IFT_ETHER;
1278 sdl->sdl_alen = ETHER_ADDR_LEN;
1279 e_addr = LLADDR(sdl);
1280 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1281 *llsa = (struct sockaddr *)sdl;
1287 * Well, the text isn't quite right, but it's the name
1290 return EAFNOSUPPORT;
1295 ether_alloc(u_char type, struct ifnet *ifp)
1299 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1306 ether_free(void *com, u_char type)
1309 free(com, M_ARPCOM);
1313 ether_modevent(module_t mod, int type, void *data)
1318 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1321 if_deregister_com_alloc(IFT_ETHER);
1330 static moduledata_t ether_mod = {
1337 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1339 struct ether_vlan_header vlan;
1342 KASSERT((m->m_flags & M_VLANTAG) != 0,
1343 ("%s: vlan information not present", __func__));
1344 KASSERT(m->m_len >= sizeof(struct ether_header),
1345 ("%s: mbuf not large enough for header", __func__));
1346 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1347 vlan.evl_proto = vlan.evl_encap_proto;
1348 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1349 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1350 m->m_len -= sizeof(struct ether_header);
1351 m->m_data += sizeof(struct ether_header);
1353 * If a data link has been supplied by the caller, then we will need to
1354 * re-create a stack allocated mbuf chain with the following structure:
1356 * (1) mbuf #1 will contain the supplied data link
1357 * (2) mbuf #2 will contain the vlan header
1358 * (3) mbuf #3 will contain the original mbuf's packet data
1360 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1364 mv.m_data = (caddr_t)&vlan;
1365 mv.m_len = sizeof(vlan);
1371 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1372 m->m_len += sizeof(struct ether_header);
1373 m->m_data -= sizeof(struct ether_header);
1377 ether_vlanencap(struct mbuf *m, uint16_t tag)
1379 struct ether_vlan_header *evl;
1381 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1384 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1386 if (m->m_len < sizeof(*evl)) {
1387 m = m_pullup(m, sizeof(*evl));
1393 * Transform the Ethernet header into an Ethernet header
1394 * with 802.1Q encapsulation.
1396 evl = mtod(m, struct ether_vlan_header *);
1397 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1398 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1399 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1400 evl->evl_tag = htons(tag);
1404 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1405 MODULE_VERSION(ether, 1);