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/vlan/if_vlan_var.h>
73 #include <net/netmsg2.h>
75 #if defined(INET) || defined(INET6)
76 #include <netinet/in.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
85 #include <netinet6/nd6.h>
89 #include <netinet/ip_carp.h>
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 short *tp, int *hlen);
101 #include <netproto/mpls/mpls.h>
104 /* netgraph node hooks for ng_ether(4) */
105 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
106 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
107 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
108 void (*ng_ether_attach_p)(struct ifnet *ifp);
109 void (*ng_ether_detach_p)(struct ifnet *ifp);
111 void (*vlan_input_p)(struct mbuf *);
113 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115 static void ether_restore_header(struct mbuf **, const struct ether_header *,
116 const struct ether_header *);
117 static int ether_characterize(struct mbuf **);
122 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
123 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
124 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
125 struct ifnet *(*bridge_interface_p)(void *if_bridge);
127 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
130 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
134 #define gotoerr(e) do { error = (e); goto bad; } while (0)
135 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
137 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
139 const struct ether_header *eh);
141 static int ether_ipfw;
142 static u_long ether_restore_hdr;
143 static u_long ether_prepend_hdr;
144 static u_long ether_input_wronghash;
145 static int ether_debug;
148 static u_long ether_pktinfo_try;
149 static u_long ether_pktinfo_hit;
150 static u_long ether_rss_nopi;
151 static u_long ether_rss_nohash;
152 static u_long ether_input_requeue;
155 SYSCTL_DECL(_net_link);
156 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
157 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
158 ðer_debug, 0, "Ether debug");
159 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
160 ðer_ipfw, 0, "Pass ether pkts through firewall");
161 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
162 ðer_restore_hdr, 0, "# of ether header restoration");
163 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
164 ðer_prepend_hdr, 0,
165 "# of ether header restoration which prepends mbuf");
166 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
167 ðer_input_wronghash, 0, "# of input packets with wrong hash");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
170 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
172 ðer_rss_nohash, 0, "# of packets do not have hash");
173 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
174 ðer_pktinfo_try, 0,
175 "# of tries to find packets' msgport using pktinfo");
176 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
177 ðer_pktinfo_hit, 0,
178 "# of packets whose msgport are found using pktinfo");
179 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
180 ðer_input_requeue, 0, "# of input packets gets requeued");
183 #define ETHER_KTR_STR "ifp=%p"
184 #define ETHER_KTR_ARGS struct ifnet *ifp
186 #define KTR_ETHERNET KTR_ALL
188 KTR_INFO_MASTER(ether);
189 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
190 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
191 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
192 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
193 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
196 * Ethernet output routine.
197 * Encapsulate a packet of type family for the local net.
198 * Use trailer local net encapsulation if enough data in first
199 * packet leaves a multiple of 512 bytes of data in remainder.
200 * Assumes that ifp is actually pointer to arpcom structure.
203 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
206 struct ether_header *eh, *deh;
209 int hlen = ETHER_HDR_LEN; /* link layer header length */
210 struct arpcom *ac = IFP2AC(ifp);
213 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
215 if (ifp->if_flags & IFF_MONITOR)
217 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
220 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
223 eh = mtod(m, struct ether_header *);
224 edst = eh->ether_dhost;
227 * Fill in the destination ethernet address and frame type.
229 switch (dst->sa_family) {
232 if (!arpresolve(ifp, rt, m, dst, edst))
233 return (0); /* if not yet resolved */
235 if (m->m_flags & M_MPLSLABELED)
236 eh->ether_type = htons(ETHERTYPE_MPLS);
239 eh->ether_type = htons(ETHERTYPE_IP);
244 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
245 return (0); /* Something bad happenned. */
246 eh->ether_type = htons(ETHERTYPE_IPV6);
251 if (ef_outputp != NULL) {
253 * Hold BGL and recheck ef_outputp
256 if (ef_outputp != NULL) {
257 error = ef_outputp(ifp, &m, dst,
258 &eh->ether_type, &hlen);
267 eh->ether_type = htons(ETHERTYPE_IPX);
268 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
269 edst, ETHER_ADDR_LEN);
272 case pseudo_AF_HDRCMPLT:
274 loop_copy = -1; /* if this is for us, don't do it */
275 deh = (struct ether_header *)dst->sa_data;
276 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
277 eh->ether_type = deh->ether_type;
281 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
282 gotoerr(EAFNOSUPPORT);
285 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
286 memcpy(eh->ether_shost,
287 ((struct ether_header *)dst->sa_data)->ether_shost,
290 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
293 * Bridges require special output handling.
295 if (ifp->if_bridge) {
296 KASSERT(bridge_output_p != NULL,
297 ("%s: if_bridge not loaded!", __func__));
298 return bridge_output_p(ifp, m);
302 * If a simplex interface, and the packet is being sent to our
303 * Ethernet address or a broadcast address, loopback a copy.
304 * XXX To make a simplex device behave exactly like a duplex
305 * device, we should copy in the case of sending to our own
306 * ethernet address (thus letting the original actually appear
307 * on the wire). However, we don't do that here for security
308 * reasons and compatibility with the original behavior.
310 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
313 if (m->m_pkthdr.csum_flags & CSUM_IP)
314 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
315 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
316 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
317 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
320 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
321 n->m_pkthdr.csum_flags |= csum_flags;
322 if (csum_flags & CSUM_DATA_VALID)
323 n->m_pkthdr.csum_data = 0xffff;
324 if_simloop(ifp, n, dst->sa_family, hlen);
327 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
328 ETHER_ADDR_LEN) == 0) {
329 m->m_pkthdr.csum_flags |= csum_flags;
330 if (csum_flags & CSUM_DATA_VALID)
331 m->m_pkthdr.csum_data = 0xffff;
332 if_simloop(ifp, m, dst->sa_family, hlen);
333 return (0); /* XXX */
338 if (ifp->if_type == IFT_CARP) {
339 ifp = carp_parent(ifp);
341 gotoerr(ENETUNREACH);
346 * Check precondition again
348 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
350 if (ifp->if_flags & IFF_MONITOR)
352 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
353 (IFF_UP | IFF_RUNNING))
358 /* Handle ng_ether(4) processing, if any */
359 if (ng_ether_output_p != NULL) {
361 * Hold BGL and recheck ng_ether_output_p
364 if (ng_ether_output_p != NULL) {
365 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
377 /* Continue with link-layer output */
378 return ether_output_frame(ifp, m);
386 * Returns the bridge interface an ifp is associated
389 * Only call if ifp->if_bridge != NULL.
392 ether_bridge_interface(struct ifnet *ifp)
394 if (bridge_interface_p)
395 return(bridge_interface_p(ifp->if_bridge));
400 * Ethernet link layer output routine to send a raw frame to the device.
402 * This assumes that the 14 byte Ethernet header is present and contiguous
406 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
408 struct ip_fw *rule = NULL;
410 struct altq_pktattr pktattr;
412 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
414 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
417 /* Extract info from dummynet tag */
418 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
419 KKASSERT(mtag != NULL);
420 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
421 KKASSERT(rule != NULL);
423 m_tag_delete(m, mtag);
424 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
427 if (ifq_is_enabled(&ifp->if_snd))
428 altq_etherclassify(&ifp->if_snd, m, &pktattr);
430 if (IPFW_LOADED && ether_ipfw != 0) {
431 struct ether_header save_eh, *eh;
433 eh = mtod(m, struct ether_header *);
435 m_adj(m, ETHER_HDR_LEN);
436 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
440 return ENOBUFS; /* pkt dropped */
442 return 0; /* consumed e.g. in a pipe */
445 /* packet was ok, restore the ethernet header */
446 ether_restore_header(&m, eh, &save_eh);
455 * Queue message on interface, update output statistics if
456 * successful, and start output if interface not yet active.
458 error = ifq_dispatch(ifp, m, &pktattr);
463 * ipfw processing for ethernet packets (in and out).
464 * The second parameter is NULL from ether_demux(), and ifp from
465 * ether_output_frame().
468 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
469 const struct ether_header *eh)
471 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
472 struct ip_fw_args args;
477 if (*rule != NULL && fw_one_pass)
478 return TRUE; /* dummynet packet, already partially processed */
481 * I need some amount of data to be contiguous.
483 i = min((*m0)->m_pkthdr.len, max_protohdr);
484 if ((*m0)->m_len < i) {
485 *m0 = m_pullup(*m0, i);
493 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
494 m_tag_delete(*m0, mtag);
495 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
496 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
497 KKASSERT(mtag != NULL);
498 m_tag_delete(*m0, mtag);
499 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
502 args.m = *m0; /* the packet we are looking at */
503 args.oif = dst; /* destination, if any */
504 args.rule = *rule; /* matching rule to restart */
505 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
506 i = ip_fw_chk_ptr(&args);
521 * XXX at some point add support for divert/forward actions.
522 * If none of the above matches, we have to drop the pkt.
528 * Pass the pkt to dummynet, which consumes it.
530 m = *m0; /* pass the original to dummynet */
531 *m0 = NULL; /* and nothing back to the caller */
533 ether_restore_header(&m, eh, &save_eh);
537 ip_fw_dn_io_ptr(m, args.cookie,
538 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
543 panic("unknown ipfw return value: %d", i);
548 ether_input(struct ifnet *ifp, struct mbuf *m)
550 ether_input_pkt(ifp, m, NULL);
554 * Perform common duties while attaching to interface list
557 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
559 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
564 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
565 lwkt_serialize_t serializer)
567 struct sockaddr_dl *sdl;
569 ifp->if_type = IFT_ETHER;
570 ifp->if_addrlen = ETHER_ADDR_LEN;
571 ifp->if_hdrlen = ETHER_HDR_LEN;
572 if_attach(ifp, serializer);
573 ifp->if_mtu = ETHERMTU;
574 if (ifp->if_baudrate == 0)
575 ifp->if_baudrate = 10000000;
576 ifp->if_output = ether_output;
577 ifp->if_input = ether_input;
578 ifp->if_resolvemulti = ether_resolvemulti;
579 ifp->if_broadcastaddr = etherbroadcastaddr;
580 sdl = IF_LLSOCKADDR(ifp);
581 sdl->sdl_type = IFT_ETHER;
582 sdl->sdl_alen = ifp->if_addrlen;
583 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
585 * XXX Keep the current drivers happy.
586 * XXX Remove once all drivers have been cleaned up
588 if (lla != IFP2AC(ifp)->ac_enaddr)
589 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
590 bpfattach(ifp, dlt, hdrlen);
591 if (ng_ether_attach_p != NULL)
592 (*ng_ether_attach_p)(ifp);
594 if_printf(ifp, "MAC address: %6D\n", lla, ":");
598 * Perform common duties while detaching an Ethernet interface
601 ether_ifdetach(struct ifnet *ifp)
605 if (ng_ether_detach_p != NULL)
606 (*ng_ether_detach_p)(ifp);
612 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
614 struct ifaddr *ifa = (struct ifaddr *) data;
615 struct ifreq *ifr = (struct ifreq *) data;
618 #define IF_INIT(ifp) \
620 if (((ifp)->if_flags & IFF_UP) == 0) { \
621 (ifp)->if_flags |= IFF_UP; \
622 (ifp)->if_init((ifp)->if_softc); \
626 ASSERT_IFNET_SERIALIZED_ALL(ifp);
630 switch (ifa->ifa_addr->sa_family) {
633 IF_INIT(ifp); /* before arpwhohas */
634 arp_ifinit(ifp, ifa);
639 * XXX - This code is probably wrong
643 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
644 struct arpcom *ac = IFP2AC(ifp);
646 if (ipx_nullhost(*ina))
647 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
649 bcopy(ina->x_host.c_host, ac->ac_enaddr,
650 sizeof ac->ac_enaddr);
652 IF_INIT(ifp); /* Set new address. */
663 bcopy(IFP2AC(ifp)->ac_enaddr,
664 ((struct sockaddr *)ifr->ifr_data)->sa_data,
670 * Set the interface MTU.
672 if (ifr->ifr_mtu > ETHERMTU) {
675 ifp->if_mtu = ifr->ifr_mtu;
690 struct sockaddr **llsa,
693 struct sockaddr_dl *sdl;
695 struct sockaddr_in *sin;
698 struct sockaddr_in6 *sin6;
702 switch(sa->sa_family) {
705 * No mapping needed. Just check that it's a valid MC address.
707 sdl = (struct sockaddr_dl *)sa;
708 e_addr = LLADDR(sdl);
709 if ((e_addr[0] & 1) != 1)
710 return EADDRNOTAVAIL;
716 sin = (struct sockaddr_in *)sa;
717 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
718 return EADDRNOTAVAIL;
719 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
720 sdl->sdl_len = sizeof *sdl;
721 sdl->sdl_family = AF_LINK;
722 sdl->sdl_index = ifp->if_index;
723 sdl->sdl_type = IFT_ETHER;
724 sdl->sdl_alen = ETHER_ADDR_LEN;
725 e_addr = LLADDR(sdl);
726 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
727 *llsa = (struct sockaddr *)sdl;
732 sin6 = (struct sockaddr_in6 *)sa;
733 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
735 * An IP6 address of 0 means listen to all
736 * of the Ethernet multicast address used for IP6.
737 * (This is used for multicast routers.)
739 ifp->if_flags |= IFF_ALLMULTI;
743 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
744 return EADDRNOTAVAIL;
745 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
746 sdl->sdl_len = sizeof *sdl;
747 sdl->sdl_family = AF_LINK;
748 sdl->sdl_index = ifp->if_index;
749 sdl->sdl_type = IFT_ETHER;
750 sdl->sdl_alen = ETHER_ADDR_LEN;
751 e_addr = LLADDR(sdl);
752 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
753 *llsa = (struct sockaddr *)sdl;
759 * Well, the text isn't quite right, but it's the name
768 * This is for reference. We have a table-driven version
769 * of the little-endian crc32 generator, which is faster
770 * than the double-loop.
773 ether_crc32_le(const uint8_t *buf, size_t len)
775 uint32_t c, crc, carry;
778 crc = 0xffffffffU; /* initial value */
780 for (i = 0; i < len; i++) {
782 for (j = 0; j < 8; j++) {
783 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
787 crc = (crc ^ ETHER_CRC_POLY_LE);
795 ether_crc32_le(const uint8_t *buf, size_t len)
797 static const uint32_t crctab[] = {
798 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
799 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
800 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
801 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
806 crc = 0xffffffffU; /* initial value */
808 for (i = 0; i < len; i++) {
810 crc = (crc >> 4) ^ crctab[crc & 0xf];
811 crc = (crc >> 4) ^ crctab[crc & 0xf];
819 ether_crc32_be(const uint8_t *buf, size_t len)
821 uint32_t c, crc, carry;
824 crc = 0xffffffffU; /* initial value */
826 for (i = 0; i < len; i++) {
828 for (j = 0; j < 8; j++) {
829 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
833 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
841 * find the size of ethernet header, and call classifier
844 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
845 struct altq_pktattr *pktattr)
847 struct ether_header *eh;
849 int hlen, af, hdrsize;
852 hlen = sizeof(struct ether_header);
853 eh = mtod(m, struct ether_header *);
855 ether_type = ntohs(eh->ether_type);
856 if (ether_type < ETHERMTU) {
858 struct llc *llc = (struct llc *)(eh + 1);
861 if (m->m_len < hlen ||
862 llc->llc_dsap != LLC_SNAP_LSAP ||
863 llc->llc_ssap != LLC_SNAP_LSAP ||
864 llc->llc_control != LLC_UI)
865 goto bad; /* not snap! */
867 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
870 if (ether_type == ETHERTYPE_IP) {
872 hdrsize = 20; /* sizeof(struct ip) */
874 } else if (ether_type == ETHERTYPE_IPV6) {
876 hdrsize = 40; /* sizeof(struct ip6_hdr) */
881 while (m->m_len <= hlen) {
885 hdr = m->m_data + hlen;
886 if (m->m_len < hlen + hdrsize) {
888 * ip header is not in a single mbuf. this should not
889 * happen in the current code.
890 * (todo: use m_pulldown in the future)
896 ifq_classify(ifq, m, af, pktattr);
903 pktattr->pattr_class = NULL;
904 pktattr->pattr_hdr = NULL;
905 pktattr->pattr_af = AF_UNSPEC;
909 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
910 const struct ether_header *save_eh)
912 struct mbuf *m = *m0;
917 * Prepend the header, optimize for the common case of
918 * eh pointing into the mbuf.
920 if ((const void *)(eh + 1) == (void *)m->m_data) {
921 m->m_data -= ETHER_HDR_LEN;
922 m->m_len += ETHER_HDR_LEN;
923 m->m_pkthdr.len += ETHER_HDR_LEN;
927 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
929 bcopy(save_eh, mtod(m, struct ether_header *),
937 * Upper layer processing for a received Ethernet packet.
940 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
942 struct ether_header *eh;
943 int isr, discard = 0;
945 struct ip_fw *rule = NULL;
948 KASSERT(m->m_len >= ETHER_HDR_LEN,
949 ("ether header is not contiguous!"));
951 eh = mtod(m, struct ether_header *);
953 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
956 /* Extract info from dummynet tag */
957 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
958 KKASSERT(mtag != NULL);
959 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
960 KKASSERT(rule != NULL);
962 m_tag_delete(m, mtag);
963 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
965 /* packet is passing the second time */
970 * We got a packet which was unicast to a different Ethernet
971 * address. If the driver is working properly, then this
972 * situation can only happen when the interface is in
973 * promiscuous mode. We defer the packet discarding until the
974 * vlan processing is done, so that vlan/bridge or vlan/netgraph
977 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
978 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
979 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
980 if (ether_debug & 1) {
981 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
982 "%02x:%02x:%02x:%02x:%02x:%02x "
983 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
997 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
998 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
999 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1000 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1001 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1002 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1005 if ((ether_debug & 2) == 0)
1010 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1011 struct ether_header save_eh = *eh;
1013 /* XXX old crufty stuff, needs to be removed */
1014 m_adj(m, sizeof(struct ether_header));
1016 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1021 ether_restore_header(&m, eh, &save_eh);
1024 eh = mtod(m, struct ether_header *);
1027 ether_type = ntohs(eh->ether_type);
1028 KKASSERT(ether_type != ETHERTYPE_VLAN);
1030 if (m->m_flags & M_VLANTAG) {
1031 void (*vlan_input_func)(struct mbuf *);
1033 vlan_input_func = vlan_input_p;
1034 if (vlan_input_func != NULL) {
1037 m->m_pkthdr.rcvif->if_noproto++;
1044 * If we have been asked to discard this packet
1045 * (e.g. not for us), drop it before entering
1054 * Clear protocol specific flags,
1055 * before entering the upper layer.
1057 m->m_flags &= ~M_ETHER_FLAGS;
1059 /* Strip ethernet header. */
1060 m_adj(m, sizeof(struct ether_header));
1062 switch (ether_type) {
1065 if ((m->m_flags & M_LENCHECKED) == 0) {
1066 if (!ip_lengthcheck(&m, 0))
1069 if (ipflow_fastforward(m))
1075 if (ifp->if_flags & IFF_NOARP) {
1076 /* Discard packet if ARP is disabled on interface */
1085 case ETHERTYPE_IPV6:
1094 * Hold BGL and recheck ef_inputp
1097 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1108 case ETHERTYPE_MPLS:
1109 case ETHERTYPE_MPLS_MCAST:
1110 /* Should have been set by ether_input_pkt(). */
1111 KKASSERT(m->m_flags & M_MPLSLABELED);
1118 * The accurate msgport is not determined before
1119 * we reach here, so recharacterize packet.
1121 m->m_flags &= ~M_HASH;
1125 * Hold BGL and recheck ef_inputp
1128 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1135 if (ng_ether_input_orphan_p != NULL) {
1137 * Put back the ethernet header so netgraph has a
1138 * consistent view of inbound packets.
1140 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1143 * M_PREPEND frees the mbuf in case of failure.
1148 * Hold BGL and recheck ng_ether_input_orphan_p
1151 if (ng_ether_input_orphan_p != NULL) {
1152 ng_ether_input_orphan_p(ifp, m);
1162 if (m->m_flags & M_HASH) {
1163 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1164 netisr_handle(isr, m);
1168 * XXX Something is wrong,
1169 * we probably should panic here!
1171 m->m_flags &= ~M_HASH;
1172 atomic_add_long(ðer_input_wronghash, 1);
1176 atomic_add_long(ðer_input_requeue, 1);
1178 netisr_queue(isr, m);
1182 * First we perform any link layer operations, then continue to the
1183 * upper layers with ether_demux_oncpu().
1186 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1192 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1194 * Receiving interface's flags are changed, when this
1195 * packet is waiting for processing; discard it.
1202 * Tap the packet off here for a bridge. bridge_input()
1203 * will return NULL if it has consumed the packet, otherwise
1204 * it gets processed as normal. Note that bridge_input()
1205 * will always return the original packet if we need to
1206 * process it locally.
1208 if (ifp->if_bridge) {
1209 KASSERT(bridge_input_p != NULL,
1210 ("%s: if_bridge not loaded!", __func__));
1212 if(m->m_flags & M_ETHER_BRIDGED) {
1213 m->m_flags &= ~M_ETHER_BRIDGED;
1215 m = bridge_input_p(ifp, m);
1219 KASSERT(ifp == m->m_pkthdr.rcvif,
1220 ("bridge_input_p changed rcvif"));
1225 carp = ifp->if_carp;
1227 m = carp_input(carp, m);
1230 KASSERT(ifp == m->m_pkthdr.rcvif,
1231 ("carp_input changed rcvif"));
1235 /* Handle ng_ether(4) processing, if any */
1236 if (ng_ether_input_p != NULL) {
1238 * Hold BGL and recheck ng_ether_input_p
1241 if (ng_ether_input_p != NULL)
1242 ng_ether_input_p(ifp, &m);
1249 /* Continue with upper layer processing */
1250 ether_demux_oncpu(ifp, m);
1254 * Perform certain functions of ether_input_pkt():
1256 * - Update statistics
1257 * - Run bpf(4) tap if requested
1258 * Then pass the packet to ether_input_oncpu().
1260 * This function should be used by pseudo interface (e.g. vlan(4)),
1261 * when it tries to claim that the packet is received by it.
1267 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1269 /* Discard packet if interface is not up */
1270 if (!(ifp->if_flags & IFF_UP)) {
1276 * Change receiving interface. The bridge will often pass a flag to
1277 * ask that this not be done so ARPs get applied to the correct
1280 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1281 m->m_pkthdr.rcvif == NULL) {
1282 m->m_pkthdr.rcvif = ifp;
1285 /* Update statistics */
1287 ifp->if_ibytes += m->m_pkthdr.len;
1288 if (m->m_flags & (M_MCAST | M_BCAST))
1291 if (reinput_flags & REINPUT_RUNBPF)
1294 ether_input_oncpu(ifp, m);
1297 static __inline boolean_t
1298 ether_vlancheck(struct mbuf **m0)
1300 struct mbuf *m = *m0;
1301 struct ether_header *eh;
1302 uint16_t ether_type;
1304 eh = mtod(m, struct ether_header *);
1305 ether_type = ntohs(eh->ether_type);
1307 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1309 * Extract vlan tag if hardware does not do it for us
1311 vlan_ether_decap(&m);
1315 eh = mtod(m, struct ether_header *);
1316 ether_type = ntohs(eh->ether_type);
1319 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1321 * To prevent possible dangerous recursion,
1322 * we don't do vlan-in-vlan
1324 m->m_pkthdr.rcvif->if_noproto++;
1327 KKASSERT(ether_type != ETHERTYPE_VLAN);
1329 m->m_flags |= M_ETHER_VLANCHECKED;
1340 ether_input_handler(netmsg_t nmsg)
1342 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1343 struct ether_header *eh;
1349 ifp = m->m_pkthdr.rcvif;
1351 eh = mtod(m, struct ether_header *);
1352 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1353 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1354 ifp->if_addrlen) == 0)
1355 m->m_flags |= M_BCAST;
1357 m->m_flags |= M_MCAST;
1361 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1362 if (!ether_vlancheck(&m)) {
1363 KKASSERT(m == NULL);
1368 ether_input_oncpu(ifp, m);
1372 * Send the packet to the target msgport
1374 * At this point the packet had better be characterized (M_HASH set),
1375 * so we know which cpu to send it to.
1378 ether_dispatch(int isr, struct mbuf *m)
1380 struct netmsg_packet *pmsg;
1382 KKASSERT(m->m_flags & M_HASH);
1383 pmsg = &m->m_hdr.mh_netmsg;
1384 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1385 0, ether_input_handler);
1386 pmsg->nm_packet = m;
1387 pmsg->base.lmsg.u.ms_result = isr;
1389 logether(disp_beg, NULL);
1390 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1391 logether(disp_end, NULL);
1395 * Process a received Ethernet packet.
1397 * The ethernet header is assumed to be in the mbuf so the caller
1398 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1399 * bytes in the first mbuf.
1402 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1408 /* Discard packet if interface is not up */
1409 if (!(ifp->if_flags & IFF_UP)) {
1414 if (m->m_len < sizeof(struct ether_header)) {
1415 /* XXX error in the caller. */
1420 m->m_pkthdr.rcvif = ifp;
1422 logether(pkt_beg, ifp);
1424 ETHER_BPF_MTAP(ifp, m);
1426 ifp->if_ibytes += m->m_pkthdr.len;
1428 if (ifp->if_flags & IFF_MONITOR) {
1429 struct ether_header *eh;
1431 eh = mtod(m, struct ether_header *);
1432 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1436 * Interface marked for monitoring; discard packet.
1440 logether(pkt_end, ifp);
1445 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1446 * we can dispatch it immediately without further inspection.
1448 if (pi != NULL && (m->m_flags & M_HASH)) {
1450 atomic_add_long(ðer_pktinfo_try, 1);
1452 netisr_hashcheck(pi->pi_netisr, m, pi);
1453 if (m->m_flags & M_HASH) {
1454 ether_dispatch(pi->pi_netisr, m);
1456 atomic_add_long(ðer_pktinfo_hit, 1);
1458 logether(pkt_end, ifp);
1463 else if (ifp->if_capenable & IFCAP_RSS) {
1465 atomic_add_long(ðer_rss_nopi, 1);
1467 atomic_add_long(ðer_rss_nohash, 1);
1472 * Packet hash will be recalculated by software,
1473 * so clear the M_HASH flag set by the driver;
1474 * the hash value calculated by the hardware may
1475 * not be exactly what we want.
1477 m->m_flags &= ~M_HASH;
1479 if (!ether_vlancheck(&m)) {
1480 KKASSERT(m == NULL);
1481 logether(pkt_end, ifp);
1485 isr = ether_characterize(&m);
1487 logether(pkt_end, ifp);
1492 * Finally dispatch it
1494 ether_dispatch(isr, m);
1496 logether(pkt_end, ifp);
1500 ether_characterize(struct mbuf **m0)
1502 struct mbuf *m = *m0;
1503 struct ether_header *eh;
1504 uint16_t ether_type;
1507 eh = mtod(m, struct ether_header *);
1508 ether_type = ntohs(eh->ether_type);
1511 * Map ether type to netisr id.
1513 switch (ether_type) {
1525 case ETHERTYPE_IPV6:
1537 case ETHERTYPE_MPLS:
1538 case ETHERTYPE_MPLS_MCAST:
1539 m->m_flags |= M_MPLSLABELED;
1546 * NETISR_MAX is an invalid value; it is chosen to let
1547 * netisr_characterize() know that we have no clear
1548 * idea where this packet should go.
1555 * Ask the isr to characterize the packet since we couldn't.
1556 * This is an attempt to optimally get us onto the correct protocol
1559 netisr_characterize(isr, &m, sizeof(struct ether_header));
1566 ether_demux_handler(netmsg_t nmsg)
1568 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1574 ifp = m->m_pkthdr.rcvif;
1576 ether_demux_oncpu(ifp, m);
1580 ether_demux(struct mbuf *m)
1582 struct netmsg_packet *pmsg;
1585 isr = ether_characterize(&m);
1589 KKASSERT(m->m_flags & M_HASH);
1590 pmsg = &m->m_hdr.mh_netmsg;
1591 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1592 0, ether_demux_handler);
1593 pmsg->nm_packet = m;
1594 pmsg->base.lmsg.u.ms_result = isr;
1596 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1600 ether_tso_pullup(struct mbuf **mp, int *hoff0, struct ip **ip, int *iphlen,
1601 struct tcphdr **th, int *thoff)
1603 struct mbuf *m = *mp;
1604 struct ether_header *eh;
1608 KASSERT(M_WRITABLE(m), ("not writable"));
1610 hoff = ETHER_HDR_LEN;
1611 if (m->m_len < hoff) {
1612 m = m_pullup(m, hoff);
1616 eh = mtod(m, struct ether_header *);
1617 type = eh->ether_type;
1619 if (type == htons(ETHERTYPE_VLAN)) {
1620 struct ether_vlan_header *evh;
1622 hoff += EVL_ENCAPLEN;
1623 if (m->m_len < hoff) {
1624 m = m_pullup(m, hoff);
1628 evh = mtod(m, struct ether_vlan_header *);
1629 type = evh->evl_proto;
1631 KASSERT(type == htons(ETHERTYPE_IP), ("not IP %d", ntohs(type)));
1635 return tcp_tso_pullup(mp, hoff, ip, iphlen, th, thoff);
1644 MODULE_VERSION(ether, 1);