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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
38 #include "opt_inet6.h"
41 #include "opt_netgraph.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/globaldata.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/netmsg2.h>
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/ip_flow.h>
79 #include <net/ipfw/ip_fw.h>
80 #include <net/dummynet/ip_dummynet.h>
83 #include <netinet6/nd6.h>
87 #include <netinet/ip_carp.h>
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
93 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
94 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
95 short *tp, int *hlen);
99 #include <netproto/mpls/mpls.h>
102 /* netgraph node hooks for ng_ether(4) */
103 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
104 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
105 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
106 void (*ng_ether_attach_p)(struct ifnet *ifp);
107 void (*ng_ether_detach_p)(struct ifnet *ifp);
109 void (*vlan_input_p)(struct mbuf *);
111 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
113 static void ether_restore_header(struct mbuf **, const struct ether_header *,
114 const struct ether_header *);
115 static int ether_characterize(struct mbuf **);
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
122 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
123 struct ifnet *(*bridge_interface_p)(void *if_bridge);
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
137 const struct ether_header *eh);
139 static int ether_ipfw;
140 static u_long ether_restore_hdr;
141 static u_long ether_prepend_hdr;
142 static u_long ether_input_wronghash;
143 static int ether_debug;
146 static u_long ether_pktinfo_try;
147 static u_long ether_pktinfo_hit;
148 static u_long ether_rss_nopi;
149 static u_long ether_rss_nohash;
150 static u_long ether_input_requeue;
153 SYSCTL_DECL(_net_link);
154 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
155 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
156 ðer_debug, 0, "Ether debug");
157 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
158 ðer_ipfw, 0, "Pass ether pkts through firewall");
159 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
160 ðer_restore_hdr, 0, "# of ether header restoration");
161 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
162 ðer_prepend_hdr, 0,
163 "# of ether header restoration which prepends mbuf");
164 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
165 ðer_input_wronghash, 0, "# of input packets with wrong hash");
167 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
168 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
170 ðer_rss_nohash, 0, "# of packets do not have hash");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
172 ðer_pktinfo_try, 0,
173 "# of tries to find packets' msgport using pktinfo");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
175 ðer_pktinfo_hit, 0,
176 "# of packets whose msgport are found using pktinfo");
177 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
178 ðer_input_requeue, 0, "# of input packets gets requeued");
181 #define ETHER_KTR_STR "ifp=%p"
182 #define ETHER_KTR_ARGS struct ifnet *ifp
184 #define KTR_ETHERNET KTR_ALL
186 KTR_INFO_MASTER(ether);
187 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
188 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
189 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
190 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
191 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
194 * Ethernet output routine.
195 * Encapsulate a packet of type family for the local net.
196 * Use trailer local net encapsulation if enough data in first
197 * packet leaves a multiple of 512 bytes of data in remainder.
198 * Assumes that ifp is actually pointer to arpcom structure.
201 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
204 struct ether_header *eh, *deh;
207 int hlen = ETHER_HDR_LEN; /* link layer header length */
208 struct arpcom *ac = IFP2AC(ifp);
211 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
213 if (ifp->if_flags & IFF_MONITOR)
215 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
218 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
221 eh = mtod(m, struct ether_header *);
222 edst = eh->ether_dhost;
225 * Fill in the destination ethernet address and frame type.
227 switch (dst->sa_family) {
230 if (!arpresolve(ifp, rt, m, dst, edst))
231 return (0); /* if not yet resolved */
233 if (m->m_flags & M_MPLSLABELED)
234 eh->ether_type = htons(ETHERTYPE_MPLS);
237 eh->ether_type = htons(ETHERTYPE_IP);
242 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
243 return (0); /* Something bad happenned. */
244 eh->ether_type = htons(ETHERTYPE_IPV6);
249 if (ef_outputp != NULL) {
251 * Hold BGL and recheck ef_outputp
254 if (ef_outputp != NULL) {
255 error = ef_outputp(ifp, &m, dst,
256 &eh->ether_type, &hlen);
265 eh->ether_type = htons(ETHERTYPE_IPX);
266 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
267 edst, ETHER_ADDR_LEN);
270 case pseudo_AF_HDRCMPLT:
272 loop_copy = -1; /* if this is for us, don't do it */
273 deh = (struct ether_header *)dst->sa_data;
274 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
275 eh->ether_type = deh->ether_type;
279 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
280 gotoerr(EAFNOSUPPORT);
283 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
284 memcpy(eh->ether_shost,
285 ((struct ether_header *)dst->sa_data)->ether_shost,
288 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
291 * Bridges require special output handling.
293 if (ifp->if_bridge) {
294 KASSERT(bridge_output_p != NULL,
295 ("%s: if_bridge not loaded!", __func__));
296 return bridge_output_p(ifp, m);
300 * If a simplex interface, and the packet is being sent to our
301 * Ethernet address or a broadcast address, loopback a copy.
302 * XXX To make a simplex device behave exactly like a duplex
303 * device, we should copy in the case of sending to our own
304 * ethernet address (thus letting the original actually appear
305 * on the wire). However, we don't do that here for security
306 * reasons and compatibility with the original behavior.
308 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
311 if (m->m_pkthdr.csum_flags & CSUM_IP)
312 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
313 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
314 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
315 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
318 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
319 n->m_pkthdr.csum_flags |= csum_flags;
320 if (csum_flags & CSUM_DATA_VALID)
321 n->m_pkthdr.csum_data = 0xffff;
322 if_simloop(ifp, n, dst->sa_family, hlen);
325 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
326 ETHER_ADDR_LEN) == 0) {
327 m->m_pkthdr.csum_flags |= csum_flags;
328 if (csum_flags & CSUM_DATA_VALID)
329 m->m_pkthdr.csum_data = 0xffff;
330 if_simloop(ifp, m, dst->sa_family, hlen);
331 return (0); /* XXX */
336 if (ifp->if_type == IFT_CARP) {
337 ifp = carp_parent(ifp);
339 gotoerr(ENETUNREACH);
344 * Check precondition again
346 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
348 if (ifp->if_flags & IFF_MONITOR)
350 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
351 (IFF_UP | IFF_RUNNING))
356 /* Handle ng_ether(4) processing, if any */
357 if (ng_ether_output_p != NULL) {
359 * Hold BGL and recheck ng_ether_output_p
362 if (ng_ether_output_p != NULL) {
363 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
375 /* Continue with link-layer output */
376 return ether_output_frame(ifp, m);
384 * Returns the bridge interface an ifp is associated
387 * Only call if ifp->if_bridge != NULL.
390 ether_bridge_interface(struct ifnet *ifp)
392 if (bridge_interface_p)
393 return(bridge_interface_p(ifp->if_bridge));
398 * Ethernet link layer output routine to send a raw frame to the device.
400 * This assumes that the 14 byte Ethernet header is present and contiguous
404 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
406 struct ip_fw *rule = NULL;
408 struct altq_pktattr pktattr;
410 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
412 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
415 /* Extract info from dummynet tag */
416 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
417 KKASSERT(mtag != NULL);
418 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
419 KKASSERT(rule != NULL);
421 m_tag_delete(m, mtag);
422 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
425 if (ifq_is_enabled(&ifp->if_snd))
426 altq_etherclassify(&ifp->if_snd, m, &pktattr);
428 if (IPFW_LOADED && ether_ipfw != 0) {
429 struct ether_header save_eh, *eh;
431 eh = mtod(m, struct ether_header *);
433 m_adj(m, ETHER_HDR_LEN);
434 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
438 return ENOBUFS; /* pkt dropped */
440 return 0; /* consumed e.g. in a pipe */
443 /* packet was ok, restore the ethernet header */
444 ether_restore_header(&m, eh, &save_eh);
453 * Queue message on interface, update output statistics if
454 * successful, and start output if interface not yet active.
456 error = ifq_dispatch(ifp, m, &pktattr);
461 * ipfw processing for ethernet packets (in and out).
462 * The second parameter is NULL from ether_demux(), and ifp from
463 * ether_output_frame().
466 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
467 const struct ether_header *eh)
469 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
470 struct ip_fw_args args;
475 if (*rule != NULL && fw_one_pass)
476 return TRUE; /* dummynet packet, already partially processed */
479 * I need some amount of data to be contiguous.
481 i = min((*m0)->m_pkthdr.len, max_protohdr);
482 if ((*m0)->m_len < i) {
483 *m0 = m_pullup(*m0, i);
491 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
492 m_tag_delete(*m0, mtag);
493 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
494 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
495 KKASSERT(mtag != NULL);
496 m_tag_delete(*m0, mtag);
497 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
500 args.m = *m0; /* the packet we are looking at */
501 args.oif = dst; /* destination, if any */
502 args.rule = *rule; /* matching rule to restart */
503 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
504 i = ip_fw_chk_ptr(&args);
519 * XXX at some point add support for divert/forward actions.
520 * If none of the above matches, we have to drop the pkt.
526 * Pass the pkt to dummynet, which consumes it.
528 m = *m0; /* pass the original to dummynet */
529 *m0 = NULL; /* and nothing back to the caller */
531 ether_restore_header(&m, eh, &save_eh);
535 ip_fw_dn_io_ptr(m, args.cookie,
536 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
541 panic("unknown ipfw return value: %d", i);
546 ether_input(struct ifnet *ifp, struct mbuf *m)
548 ether_input_pkt(ifp, m, NULL);
552 * Perform common duties while attaching to interface list
555 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
557 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
562 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
563 lwkt_serialize_t serializer)
565 struct sockaddr_dl *sdl;
567 ifp->if_type = IFT_ETHER;
568 ifp->if_addrlen = ETHER_ADDR_LEN;
569 ifp->if_hdrlen = ETHER_HDR_LEN;
570 if_attach(ifp, serializer);
571 ifp->if_mtu = ETHERMTU;
572 if (ifp->if_baudrate == 0)
573 ifp->if_baudrate = 10000000;
574 ifp->if_output = ether_output;
575 ifp->if_input = ether_input;
576 ifp->if_resolvemulti = ether_resolvemulti;
577 ifp->if_broadcastaddr = etherbroadcastaddr;
578 sdl = IF_LLSOCKADDR(ifp);
579 sdl->sdl_type = IFT_ETHER;
580 sdl->sdl_alen = ifp->if_addrlen;
581 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
583 * XXX Keep the current drivers happy.
584 * XXX Remove once all drivers have been cleaned up
586 if (lla != IFP2AC(ifp)->ac_enaddr)
587 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
588 bpfattach(ifp, dlt, hdrlen);
589 if (ng_ether_attach_p != NULL)
590 (*ng_ether_attach_p)(ifp);
592 if_printf(ifp, "MAC address: %6D\n", lla, ":");
596 * Perform common duties while detaching an Ethernet interface
599 ether_ifdetach(struct ifnet *ifp)
603 if (ng_ether_detach_p != NULL)
604 (*ng_ether_detach_p)(ifp);
610 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
612 struct ifaddr *ifa = (struct ifaddr *) data;
613 struct ifreq *ifr = (struct ifreq *) data;
616 #define IF_INIT(ifp) \
618 if (((ifp)->if_flags & IFF_UP) == 0) { \
619 (ifp)->if_flags |= IFF_UP; \
620 (ifp)->if_init((ifp)->if_softc); \
624 ASSERT_IFNET_SERIALIZED_ALL(ifp);
628 switch (ifa->ifa_addr->sa_family) {
631 IF_INIT(ifp); /* before arpwhohas */
632 arp_ifinit(ifp, ifa);
637 * XXX - This code is probably wrong
641 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
642 struct arpcom *ac = IFP2AC(ifp);
644 if (ipx_nullhost(*ina))
645 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
647 bcopy(ina->x_host.c_host, ac->ac_enaddr,
648 sizeof ac->ac_enaddr);
650 IF_INIT(ifp); /* Set new address. */
661 bcopy(IFP2AC(ifp)->ac_enaddr,
662 ((struct sockaddr *)ifr->ifr_data)->sa_data,
668 * Set the interface MTU.
670 if (ifr->ifr_mtu > ETHERMTU) {
673 ifp->if_mtu = ifr->ifr_mtu;
688 struct sockaddr **llsa,
691 struct sockaddr_dl *sdl;
693 struct sockaddr_in *sin;
696 struct sockaddr_in6 *sin6;
700 switch(sa->sa_family) {
703 * No mapping needed. Just check that it's a valid MC address.
705 sdl = (struct sockaddr_dl *)sa;
706 e_addr = LLADDR(sdl);
707 if ((e_addr[0] & 1) != 1)
708 return EADDRNOTAVAIL;
714 sin = (struct sockaddr_in *)sa;
715 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
716 return EADDRNOTAVAIL;
717 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
718 sdl->sdl_len = sizeof *sdl;
719 sdl->sdl_family = AF_LINK;
720 sdl->sdl_index = ifp->if_index;
721 sdl->sdl_type = IFT_ETHER;
722 sdl->sdl_alen = ETHER_ADDR_LEN;
723 e_addr = LLADDR(sdl);
724 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
725 *llsa = (struct sockaddr *)sdl;
730 sin6 = (struct sockaddr_in6 *)sa;
731 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
733 * An IP6 address of 0 means listen to all
734 * of the Ethernet multicast address used for IP6.
735 * (This is used for multicast routers.)
737 ifp->if_flags |= IFF_ALLMULTI;
741 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
742 return EADDRNOTAVAIL;
743 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
744 sdl->sdl_len = sizeof *sdl;
745 sdl->sdl_family = AF_LINK;
746 sdl->sdl_index = ifp->if_index;
747 sdl->sdl_type = IFT_ETHER;
748 sdl->sdl_alen = ETHER_ADDR_LEN;
749 e_addr = LLADDR(sdl);
750 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
751 *llsa = (struct sockaddr *)sdl;
757 * Well, the text isn't quite right, but it's the name
766 * This is for reference. We have a table-driven version
767 * of the little-endian crc32 generator, which is faster
768 * than the double-loop.
771 ether_crc32_le(const uint8_t *buf, size_t len)
773 uint32_t c, crc, carry;
776 crc = 0xffffffffU; /* initial value */
778 for (i = 0; i < len; i++) {
780 for (j = 0; j < 8; j++) {
781 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
785 crc = (crc ^ ETHER_CRC_POLY_LE);
793 ether_crc32_le(const uint8_t *buf, size_t len)
795 static const uint32_t crctab[] = {
796 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
797 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
798 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
799 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
804 crc = 0xffffffffU; /* initial value */
806 for (i = 0; i < len; i++) {
808 crc = (crc >> 4) ^ crctab[crc & 0xf];
809 crc = (crc >> 4) ^ crctab[crc & 0xf];
817 ether_crc32_be(const uint8_t *buf, size_t len)
819 uint32_t c, crc, carry;
822 crc = 0xffffffffU; /* initial value */
824 for (i = 0; i < len; i++) {
826 for (j = 0; j < 8; j++) {
827 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
831 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
839 * find the size of ethernet header, and call classifier
842 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
843 struct altq_pktattr *pktattr)
845 struct ether_header *eh;
847 int hlen, af, hdrsize;
850 hlen = sizeof(struct ether_header);
851 eh = mtod(m, struct ether_header *);
853 ether_type = ntohs(eh->ether_type);
854 if (ether_type < ETHERMTU) {
856 struct llc *llc = (struct llc *)(eh + 1);
859 if (m->m_len < hlen ||
860 llc->llc_dsap != LLC_SNAP_LSAP ||
861 llc->llc_ssap != LLC_SNAP_LSAP ||
862 llc->llc_control != LLC_UI)
863 goto bad; /* not snap! */
865 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
868 if (ether_type == ETHERTYPE_IP) {
870 hdrsize = 20; /* sizeof(struct ip) */
872 } else if (ether_type == ETHERTYPE_IPV6) {
874 hdrsize = 40; /* sizeof(struct ip6_hdr) */
879 while (m->m_len <= hlen) {
883 hdr = m->m_data + hlen;
884 if (m->m_len < hlen + hdrsize) {
886 * ip header is not in a single mbuf. this should not
887 * happen in the current code.
888 * (todo: use m_pulldown in the future)
894 ifq_classify(ifq, m, af, pktattr);
901 pktattr->pattr_class = NULL;
902 pktattr->pattr_hdr = NULL;
903 pktattr->pattr_af = AF_UNSPEC;
907 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
908 const struct ether_header *save_eh)
910 struct mbuf *m = *m0;
915 * Prepend the header, optimize for the common case of
916 * eh pointing into the mbuf.
918 if ((const void *)(eh + 1) == (void *)m->m_data) {
919 m->m_data -= ETHER_HDR_LEN;
920 m->m_len += ETHER_HDR_LEN;
921 m->m_pkthdr.len += ETHER_HDR_LEN;
925 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
927 bcopy(save_eh, mtod(m, struct ether_header *),
935 * Upper layer processing for a received Ethernet packet.
938 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
940 struct ether_header *eh;
941 int isr, discard = 0;
943 struct ip_fw *rule = NULL;
946 KASSERT(m->m_len >= ETHER_HDR_LEN,
947 ("ether header is not contiguous!"));
949 eh = mtod(m, struct ether_header *);
951 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
954 /* Extract info from dummynet tag */
955 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
956 KKASSERT(mtag != NULL);
957 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
958 KKASSERT(rule != NULL);
960 m_tag_delete(m, mtag);
961 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
963 /* packet is passing the second time */
968 * We got a packet which was unicast to a different Ethernet
969 * address. If the driver is working properly, then this
970 * situation can only happen when the interface is in
971 * promiscuous mode. We defer the packet discarding until the
972 * vlan processing is done, so that vlan/bridge or vlan/netgraph
975 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
976 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
977 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
978 if (ether_debug & 1) {
979 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
980 "%02x:%02x:%02x:%02x:%02x:%02x "
981 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
995 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
996 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
997 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
998 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
999 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1000 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1003 if ((ether_debug & 2) == 0)
1008 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1009 struct ether_header save_eh = *eh;
1011 /* XXX old crufty stuff, needs to be removed */
1012 m_adj(m, sizeof(struct ether_header));
1014 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1019 ether_restore_header(&m, eh, &save_eh);
1022 eh = mtod(m, struct ether_header *);
1025 ether_type = ntohs(eh->ether_type);
1026 KKASSERT(ether_type != ETHERTYPE_VLAN);
1028 if (m->m_flags & M_VLANTAG) {
1029 void (*vlan_input_func)(struct mbuf *);
1031 vlan_input_func = vlan_input_p;
1032 if (vlan_input_func != NULL) {
1035 m->m_pkthdr.rcvif->if_noproto++;
1042 * If we have been asked to discard this packet
1043 * (e.g. not for us), drop it before entering
1052 * Clear protocol specific flags,
1053 * before entering the upper layer.
1055 m->m_flags &= ~M_ETHER_FLAGS;
1057 /* Strip ethernet header. */
1058 m_adj(m, sizeof(struct ether_header));
1060 switch (ether_type) {
1063 if ((m->m_flags & M_LENCHECKED) == 0) {
1064 if (!ip_lengthcheck(&m, 0))
1067 if (ipflow_fastforward(m))
1073 if (ifp->if_flags & IFF_NOARP) {
1074 /* Discard packet if ARP is disabled on interface */
1083 case ETHERTYPE_IPV6:
1092 * Hold BGL and recheck ef_inputp
1095 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1106 case ETHERTYPE_MPLS:
1107 case ETHERTYPE_MPLS_MCAST:
1108 /* Should have been set by ether_input_pkt(). */
1109 KKASSERT(m->m_flags & M_MPLSLABELED);
1116 * The accurate msgport is not determined before
1117 * we reach here, so recharacterize packet.
1119 m->m_flags &= ~M_HASH;
1123 * Hold BGL and recheck ef_inputp
1126 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1133 if (ng_ether_input_orphan_p != NULL) {
1135 * Put back the ethernet header so netgraph has a
1136 * consistent view of inbound packets.
1138 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1141 * M_PREPEND frees the mbuf in case of failure.
1146 * Hold BGL and recheck ng_ether_input_orphan_p
1149 if (ng_ether_input_orphan_p != NULL) {
1150 ng_ether_input_orphan_p(ifp, m);
1160 if (m->m_flags & M_HASH) {
1161 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1162 netisr_handle(isr, m);
1166 * XXX Something is wrong,
1167 * we probably should panic here!
1169 m->m_flags &= ~M_HASH;
1170 atomic_add_long(ðer_input_wronghash, 1);
1174 atomic_add_long(ðer_input_requeue, 1);
1176 netisr_queue(isr, m);
1180 * First we perform any link layer operations, then continue to the
1181 * upper layers with ether_demux_oncpu().
1184 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1190 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1192 * Receiving interface's flags are changed, when this
1193 * packet is waiting for processing; discard it.
1200 * Tap the packet off here for a bridge. bridge_input()
1201 * will return NULL if it has consumed the packet, otherwise
1202 * it gets processed as normal. Note that bridge_input()
1203 * will always return the original packet if we need to
1204 * process it locally.
1206 if (ifp->if_bridge) {
1207 KASSERT(bridge_input_p != NULL,
1208 ("%s: if_bridge not loaded!", __func__));
1210 if(m->m_flags & M_ETHER_BRIDGED) {
1211 m->m_flags &= ~M_ETHER_BRIDGED;
1213 m = bridge_input_p(ifp, m);
1217 KASSERT(ifp == m->m_pkthdr.rcvif,
1218 ("bridge_input_p changed rcvif"));
1223 carp = ifp->if_carp;
1225 m = carp_input(carp, m);
1228 KASSERT(ifp == m->m_pkthdr.rcvif,
1229 ("carp_input changed rcvif"));
1233 /* Handle ng_ether(4) processing, if any */
1234 if (ng_ether_input_p != NULL) {
1236 * Hold BGL and recheck ng_ether_input_p
1239 if (ng_ether_input_p != NULL)
1240 ng_ether_input_p(ifp, &m);
1247 /* Continue with upper layer processing */
1248 ether_demux_oncpu(ifp, m);
1252 * Perform certain functions of ether_input_pkt():
1254 * - Update statistics
1255 * - Run bpf(4) tap if requested
1256 * Then pass the packet to ether_input_oncpu().
1258 * This function should be used by pseudo interface (e.g. vlan(4)),
1259 * when it tries to claim that the packet is received by it.
1265 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1267 /* Discard packet if interface is not up */
1268 if (!(ifp->if_flags & IFF_UP)) {
1274 * Change receiving interface. The bridge will often pass a flag to
1275 * ask that this not be done so ARPs get applied to the correct
1278 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1279 m->m_pkthdr.rcvif == NULL) {
1280 m->m_pkthdr.rcvif = ifp;
1283 /* Update statistics */
1285 ifp->if_ibytes += m->m_pkthdr.len;
1286 if (m->m_flags & (M_MCAST | M_BCAST))
1289 if (reinput_flags & REINPUT_RUNBPF)
1292 ether_input_oncpu(ifp, m);
1295 static __inline boolean_t
1296 ether_vlancheck(struct mbuf **m0)
1298 struct mbuf *m = *m0;
1299 struct ether_header *eh;
1300 uint16_t ether_type;
1302 eh = mtod(m, struct ether_header *);
1303 ether_type = ntohs(eh->ether_type);
1305 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1307 * Extract vlan tag if hardware does not do it for us
1309 vlan_ether_decap(&m);
1313 eh = mtod(m, struct ether_header *);
1314 ether_type = ntohs(eh->ether_type);
1317 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1319 * To prevent possible dangerous recursion,
1320 * we don't do vlan-in-vlan
1322 m->m_pkthdr.rcvif->if_noproto++;
1325 KKASSERT(ether_type != ETHERTYPE_VLAN);
1327 m->m_flags |= M_ETHER_VLANCHECKED;
1338 ether_input_handler(netmsg_t nmsg)
1340 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1341 struct ether_header *eh;
1347 ifp = m->m_pkthdr.rcvif;
1349 eh = mtod(m, struct ether_header *);
1350 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1351 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1352 ifp->if_addrlen) == 0)
1353 m->m_flags |= M_BCAST;
1355 m->m_flags |= M_MCAST;
1359 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1360 if (!ether_vlancheck(&m)) {
1361 KKASSERT(m == NULL);
1366 ether_input_oncpu(ifp, m);
1370 * Send the packet to the target msgport
1372 * At this point the packet had better be characterized (M_HASH set),
1373 * so we know which cpu to send it to.
1376 ether_dispatch(int isr, struct mbuf *m)
1378 struct netmsg_packet *pmsg;
1380 KKASSERT(m->m_flags & M_HASH);
1381 pmsg = &m->m_hdr.mh_netmsg;
1382 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1383 0, ether_input_handler);
1384 pmsg->nm_packet = m;
1385 pmsg->base.lmsg.u.ms_result = isr;
1387 logether(disp_beg, NULL);
1388 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1389 logether(disp_end, NULL);
1393 * Process a received Ethernet packet.
1395 * The ethernet header is assumed to be in the mbuf so the caller
1396 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1397 * bytes in the first mbuf.
1400 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1406 /* Discard packet if interface is not up */
1407 if (!(ifp->if_flags & IFF_UP)) {
1412 if (m->m_len < sizeof(struct ether_header)) {
1413 /* XXX error in the caller. */
1418 m->m_pkthdr.rcvif = ifp;
1420 logether(pkt_beg, ifp);
1422 ETHER_BPF_MTAP(ifp, m);
1424 ifp->if_ibytes += m->m_pkthdr.len;
1426 if (ifp->if_flags & IFF_MONITOR) {
1427 struct ether_header *eh;
1429 eh = mtod(m, struct ether_header *);
1430 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1434 * Interface marked for monitoring; discard packet.
1438 logether(pkt_end, ifp);
1443 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1444 * we can dispatch it immediately without further inspection.
1446 if (pi != NULL && (m->m_flags & M_HASH)) {
1448 atomic_add_long(ðer_pktinfo_try, 1);
1450 netisr_hashcheck(pi->pi_netisr, m, pi);
1451 if (m->m_flags & M_HASH) {
1452 ether_dispatch(pi->pi_netisr, m);
1454 atomic_add_long(ðer_pktinfo_hit, 1);
1456 logether(pkt_end, ifp);
1461 else if (ifp->if_capenable & IFCAP_RSS) {
1463 atomic_add_long(ðer_rss_nopi, 1);
1465 atomic_add_long(ðer_rss_nohash, 1);
1470 * Packet hash will be recalculated by software,
1471 * so clear the M_HASH flag set by the driver;
1472 * the hash value calculated by the hardware may
1473 * not be exactly what we want.
1475 m->m_flags &= ~M_HASH;
1477 if (!ether_vlancheck(&m)) {
1478 KKASSERT(m == NULL);
1479 logether(pkt_end, ifp);
1483 isr = ether_characterize(&m);
1485 logether(pkt_end, ifp);
1490 * Finally dispatch it
1492 ether_dispatch(isr, m);
1494 logether(pkt_end, ifp);
1498 ether_characterize(struct mbuf **m0)
1500 struct mbuf *m = *m0;
1501 struct ether_header *eh;
1502 uint16_t ether_type;
1505 eh = mtod(m, struct ether_header *);
1506 ether_type = ntohs(eh->ether_type);
1509 * Map ether type to netisr id.
1511 switch (ether_type) {
1523 case ETHERTYPE_IPV6:
1535 case ETHERTYPE_MPLS:
1536 case ETHERTYPE_MPLS_MCAST:
1537 m->m_flags |= M_MPLSLABELED;
1544 * NETISR_MAX is an invalid value; it is chosen to let
1545 * netisr_characterize() know that we have no clear
1546 * idea where this packet should go.
1553 * Ask the isr to characterize the packet since we couldn't.
1554 * This is an attempt to optimally get us onto the correct protocol
1557 netisr_characterize(isr, &m, sizeof(struct ether_header));
1564 ether_demux_handler(netmsg_t nmsg)
1566 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1572 ifp = m->m_pkthdr.rcvif;
1574 ether_demux_oncpu(ifp, m);
1578 ether_demux(struct mbuf *m)
1580 struct netmsg_packet *pmsg;
1583 isr = ether_characterize(&m);
1587 KKASSERT(m->m_flags & M_HASH);
1588 pmsg = &m->m_hdr.mh_netmsg;
1589 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1590 0, ether_demux_handler);
1591 pmsg->nm_packet = m;
1592 pmsg->base.lmsg.u.ms_result = isr;
1594 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1597 MODULE_VERSION(ether, 1);