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,
105 struct mbuf *m, const struct ether_header *eh);
106 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
107 void (*ng_ether_attach_p)(struct ifnet *ifp);
108 void (*ng_ether_detach_p)(struct ifnet *ifp);
110 void (*vlan_input_p)(struct mbuf *);
112 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
114 static void ether_restore_header(struct mbuf **, const struct ether_header *,
115 const struct ether_header *);
116 static int ether_characterize(struct mbuf **);
121 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
122 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
123 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
124 struct ifnet *(*bridge_interface_p)(void *if_bridge);
126 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
129 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
133 #define gotoerr(e) do { error = (e); goto bad; } while (0)
134 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
136 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
138 const struct ether_header *eh);
140 static int ether_ipfw;
141 static u_long ether_restore_hdr;
142 static u_long ether_prepend_hdr;
143 static u_long ether_input_wronghash;
144 static int ether_debug;
147 static u_long ether_pktinfo_try;
148 static u_long ether_pktinfo_hit;
149 static u_long ether_rss_nopi;
150 static u_long ether_rss_nohash;
151 static u_long ether_input_requeue;
154 SYSCTL_DECL(_net_link);
155 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
156 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
157 ðer_debug, 0, "Ether debug");
158 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
159 ðer_ipfw, 0, "Pass ether pkts through firewall");
160 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
161 ðer_restore_hdr, 0, "# of ether header restoration");
162 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
163 ðer_prepend_hdr, 0,
164 "# of ether header restoration which prepends mbuf");
165 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
166 ðer_input_wronghash, 0, "# of input packets with wrong hash");
168 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
169 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
170 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
171 ðer_rss_nohash, 0, "# of packets do not have hash");
172 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
173 ðer_pktinfo_try, 0,
174 "# of tries to find packets' msgport using pktinfo");
175 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
176 ðer_pktinfo_hit, 0,
177 "# of packets whose msgport are found using pktinfo");
178 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
179 ðer_input_requeue, 0, "# of input packets gets requeued");
182 #define ETHER_KTR_STR "ifp=%p"
183 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
185 #define KTR_ETHERNET KTR_ALL
187 KTR_INFO_MASTER(ether);
188 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
189 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
190 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
191 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
192 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
195 * Ethernet output routine.
196 * Encapsulate a packet of type family for the local net.
197 * Use trailer local net encapsulation if enough data in first
198 * packet leaves a multiple of 512 bytes of data in remainder.
199 * Assumes that ifp is actually pointer to arpcom structure.
202 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
205 struct ether_header *eh, *deh;
208 int hlen = ETHER_HDR_LEN; /* link layer header length */
209 struct arpcom *ac = IFP2AC(ifp);
212 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
214 if (ifp->if_flags & IFF_MONITOR)
216 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
219 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
222 eh = mtod(m, struct ether_header *);
223 edst = eh->ether_dhost;
226 * Fill in the destination ethernet address and frame type.
228 switch (dst->sa_family) {
231 if (!arpresolve(ifp, rt, m, dst, edst))
232 return (0); /* if not yet resolved */
234 if (m->m_flags & M_MPLSLABELED)
235 eh->ether_type = htons(ETHERTYPE_MPLS);
238 eh->ether_type = htons(ETHERTYPE_IP);
243 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
244 return (0); /* Something bad happenned. */
245 eh->ether_type = htons(ETHERTYPE_IPV6);
250 if (ef_outputp != NULL) {
252 * Hold BGL and recheck ef_outputp
255 if (ef_outputp != NULL) {
256 error = ef_outputp(ifp, &m, dst,
257 &eh->ether_type, &hlen);
266 eh->ether_type = htons(ETHERTYPE_IPX);
267 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
268 edst, ETHER_ADDR_LEN);
271 case pseudo_AF_HDRCMPLT:
273 loop_copy = -1; /* if this is for us, don't do it */
274 deh = (struct ether_header *)dst->sa_data;
275 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
276 eh->ether_type = deh->ether_type;
280 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
281 gotoerr(EAFNOSUPPORT);
284 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
285 memcpy(eh->ether_shost,
286 ((struct ether_header *)dst->sa_data)->ether_shost,
289 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
292 * Bridges require special output handling.
294 if (ifp->if_bridge) {
295 KASSERT(bridge_output_p != NULL,
296 ("%s: if_bridge not loaded!", __func__));
297 return bridge_output_p(ifp, m);
301 * If a simplex interface, and the packet is being sent to our
302 * Ethernet address or a broadcast address, loopback a copy.
303 * XXX To make a simplex device behave exactly like a duplex
304 * device, we should copy in the case of sending to our own
305 * ethernet address (thus letting the original actually appear
306 * on the wire). However, we don't do that here for security
307 * reasons and compatibility with the original behavior.
309 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
312 if (m->m_pkthdr.csum_flags & CSUM_IP)
313 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
314 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
315 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
316 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
319 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
320 n->m_pkthdr.csum_flags |= csum_flags;
321 if (csum_flags & CSUM_DATA_VALID)
322 n->m_pkthdr.csum_data = 0xffff;
323 if_simloop(ifp, n, dst->sa_family, hlen);
326 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
327 ETHER_ADDR_LEN) == 0) {
328 m->m_pkthdr.csum_flags |= csum_flags;
329 if (csum_flags & CSUM_DATA_VALID)
330 m->m_pkthdr.csum_data = 0xffff;
331 if_simloop(ifp, m, dst->sa_family, hlen);
332 return (0); /* XXX */
339 * Hold BGL and recheck ifp->if_carp
342 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
351 /* Handle ng_ether(4) processing, if any */
352 if (ng_ether_output_p != NULL) {
354 * Hold BGL and recheck ng_ether_output_p
357 if (ng_ether_output_p != NULL) {
358 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
370 /* Continue with link-layer output */
371 return ether_output_frame(ifp, m);
379 * Returns the bridge interface an ifp is associated
382 * Only call if ifp->if_bridge != NULL.
385 ether_bridge_interface(struct ifnet *ifp)
387 if (bridge_interface_p)
388 return(bridge_interface_p(ifp->if_bridge));
393 * Ethernet link layer output routine to send a raw frame to the device.
395 * This assumes that the 14 byte Ethernet header is present and contiguous
399 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
401 struct ip_fw *rule = NULL;
403 struct altq_pktattr pktattr;
405 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
407 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
410 /* Extract info from dummynet tag */
411 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
412 KKASSERT(mtag != NULL);
413 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
414 KKASSERT(rule != NULL);
416 m_tag_delete(m, mtag);
417 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
420 if (ifq_is_enabled(&ifp->if_snd))
421 altq_etherclassify(&ifp->if_snd, m, &pktattr);
423 if (IPFW_LOADED && ether_ipfw != 0) {
424 struct ether_header save_eh, *eh;
426 eh = mtod(m, struct ether_header *);
428 m_adj(m, ETHER_HDR_LEN);
429 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
433 return ENOBUFS; /* pkt dropped */
435 return 0; /* consumed e.g. in a pipe */
438 /* packet was ok, restore the ethernet header */
439 ether_restore_header(&m, eh, &save_eh);
448 * Queue message on interface, update output statistics if
449 * successful, and start output if interface not yet active.
451 error = ifq_dispatch(ifp, m, &pktattr);
456 * ipfw processing for ethernet packets (in and out).
457 * The second parameter is NULL from ether_demux(), and ifp from
458 * ether_output_frame().
461 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
462 const struct ether_header *eh)
464 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
465 struct ip_fw_args args;
470 if (*rule != NULL && fw_one_pass)
471 return TRUE; /* dummynet packet, already partially processed */
474 * I need some amount of data to be contiguous.
476 i = min((*m0)->m_pkthdr.len, max_protohdr);
477 if ((*m0)->m_len < i) {
478 *m0 = m_pullup(*m0, i);
486 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
487 m_tag_delete(*m0, mtag);
488 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
489 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
490 KKASSERT(mtag != NULL);
491 m_tag_delete(*m0, mtag);
492 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
495 args.m = *m0; /* the packet we are looking at */
496 args.oif = dst; /* destination, if any */
497 args.rule = *rule; /* matching rule to restart */
498 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
499 i = ip_fw_chk_ptr(&args);
514 * XXX at some point add support for divert/forward actions.
515 * If none of the above matches, we have to drop the pkt.
521 * Pass the pkt to dummynet, which consumes it.
523 m = *m0; /* pass the original to dummynet */
524 *m0 = NULL; /* and nothing back to the caller */
526 ether_restore_header(&m, eh, &save_eh);
530 ip_fw_dn_io_ptr(m, args.cookie,
531 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
536 panic("unknown ipfw return value: %d\n", i);
541 ether_input(struct ifnet *ifp, struct mbuf *m)
543 ether_input_chain(ifp, m, NULL, NULL);
547 * Perform common duties while attaching to interface list
550 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
552 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
557 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
558 lwkt_serialize_t serializer)
560 struct sockaddr_dl *sdl;
562 ifp->if_type = IFT_ETHER;
563 ifp->if_addrlen = ETHER_ADDR_LEN;
564 ifp->if_hdrlen = ETHER_HDR_LEN;
565 if_attach(ifp, serializer);
566 ifp->if_mtu = ETHERMTU;
567 if (ifp->if_baudrate == 0)
568 ifp->if_baudrate = 10000000;
569 ifp->if_output = ether_output;
570 ifp->if_input = ether_input;
571 ifp->if_resolvemulti = ether_resolvemulti;
572 ifp->if_broadcastaddr = etherbroadcastaddr;
573 sdl = IF_LLSOCKADDR(ifp);
574 sdl->sdl_type = IFT_ETHER;
575 sdl->sdl_alen = ifp->if_addrlen;
576 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
578 * XXX Keep the current drivers happy.
579 * XXX Remove once all drivers have been cleaned up
581 if (lla != IFP2AC(ifp)->ac_enaddr)
582 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
583 bpfattach(ifp, dlt, hdrlen);
584 if (ng_ether_attach_p != NULL)
585 (*ng_ether_attach_p)(ifp);
587 if_printf(ifp, "MAC address: %6D\n", lla, ":");
591 * Perform common duties while detaching an Ethernet interface
594 ether_ifdetach(struct ifnet *ifp)
598 if (ng_ether_detach_p != NULL)
599 (*ng_ether_detach_p)(ifp);
605 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
607 struct ifaddr *ifa = (struct ifaddr *) data;
608 struct ifreq *ifr = (struct ifreq *) data;
611 #define IF_INIT(ifp) \
613 if (((ifp)->if_flags & IFF_UP) == 0) { \
614 (ifp)->if_flags |= IFF_UP; \
615 (ifp)->if_init((ifp)->if_softc); \
619 ASSERT_IFNET_SERIALIZED_ALL(ifp);
623 switch (ifa->ifa_addr->sa_family) {
626 IF_INIT(ifp); /* before arpwhohas */
627 arp_ifinit(ifp, ifa);
632 * XXX - This code is probably wrong
636 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
637 struct arpcom *ac = IFP2AC(ifp);
639 if (ipx_nullhost(*ina))
640 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
642 bcopy(ina->x_host.c_host, ac->ac_enaddr,
643 sizeof ac->ac_enaddr);
645 IF_INIT(ifp); /* Set new address. */
656 bcopy(IFP2AC(ifp)->ac_enaddr,
657 ((struct sockaddr *)ifr->ifr_data)->sa_data,
663 * Set the interface MTU.
665 if (ifr->ifr_mtu > ETHERMTU) {
668 ifp->if_mtu = ifr->ifr_mtu;
683 struct sockaddr **llsa,
686 struct sockaddr_dl *sdl;
687 struct sockaddr_in *sin;
689 struct sockaddr_in6 *sin6;
693 switch(sa->sa_family) {
696 * No mapping needed. Just check that it's a valid MC address.
698 sdl = (struct sockaddr_dl *)sa;
699 e_addr = LLADDR(sdl);
700 if ((e_addr[0] & 1) != 1)
701 return EADDRNOTAVAIL;
707 sin = (struct sockaddr_in *)sa;
708 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
709 return EADDRNOTAVAIL;
710 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
712 sdl->sdl_len = sizeof *sdl;
713 sdl->sdl_family = AF_LINK;
714 sdl->sdl_index = ifp->if_index;
715 sdl->sdl_type = IFT_ETHER;
716 sdl->sdl_alen = ETHER_ADDR_LEN;
717 e_addr = LLADDR(sdl);
718 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
719 *llsa = (struct sockaddr *)sdl;
724 sin6 = (struct sockaddr_in6 *)sa;
725 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
727 * An IP6 address of 0 means listen to all
728 * of the Ethernet multicast address used for IP6.
729 * (This is used for multicast routers.)
731 ifp->if_flags |= IFF_ALLMULTI;
735 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
736 return EADDRNOTAVAIL;
737 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
739 sdl->sdl_len = sizeof *sdl;
740 sdl->sdl_family = AF_LINK;
741 sdl->sdl_index = ifp->if_index;
742 sdl->sdl_type = IFT_ETHER;
743 sdl->sdl_alen = ETHER_ADDR_LEN;
744 e_addr = LLADDR(sdl);
745 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
746 *llsa = (struct sockaddr *)sdl;
752 * Well, the text isn't quite right, but it's the name
761 * This is for reference. We have a table-driven version
762 * of the little-endian crc32 generator, which is faster
763 * than the double-loop.
766 ether_crc32_le(const uint8_t *buf, size_t len)
768 uint32_t c, crc, carry;
771 crc = 0xffffffffU; /* initial value */
773 for (i = 0; i < len; i++) {
775 for (j = 0; j < 8; j++) {
776 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
780 crc = (crc ^ ETHER_CRC_POLY_LE);
788 ether_crc32_le(const uint8_t *buf, size_t len)
790 static const uint32_t crctab[] = {
791 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
792 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
793 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
794 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
799 crc = 0xffffffffU; /* initial value */
801 for (i = 0; i < len; i++) {
803 crc = (crc >> 4) ^ crctab[crc & 0xf];
804 crc = (crc >> 4) ^ crctab[crc & 0xf];
812 ether_crc32_be(const uint8_t *buf, size_t len)
814 uint32_t c, crc, carry;
817 crc = 0xffffffffU; /* initial value */
819 for (i = 0; i < len; i++) {
821 for (j = 0; j < 8; j++) {
822 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
826 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
834 * find the size of ethernet header, and call classifier
837 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
838 struct altq_pktattr *pktattr)
840 struct ether_header *eh;
842 int hlen, af, hdrsize;
845 hlen = sizeof(struct ether_header);
846 eh = mtod(m, struct ether_header *);
848 ether_type = ntohs(eh->ether_type);
849 if (ether_type < ETHERMTU) {
851 struct llc *llc = (struct llc *)(eh + 1);
854 if (m->m_len < hlen ||
855 llc->llc_dsap != LLC_SNAP_LSAP ||
856 llc->llc_ssap != LLC_SNAP_LSAP ||
857 llc->llc_control != LLC_UI)
858 goto bad; /* not snap! */
860 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
863 if (ether_type == ETHERTYPE_IP) {
865 hdrsize = 20; /* sizeof(struct ip) */
867 } else if (ether_type == ETHERTYPE_IPV6) {
869 hdrsize = 40; /* sizeof(struct ip6_hdr) */
874 while (m->m_len <= hlen) {
878 hdr = m->m_data + hlen;
879 if (m->m_len < hlen + hdrsize) {
881 * ip header is not in a single mbuf. this should not
882 * happen in the current code.
883 * (todo: use m_pulldown in the future)
889 ifq_classify(ifq, m, af, pktattr);
896 pktattr->pattr_class = NULL;
897 pktattr->pattr_hdr = NULL;
898 pktattr->pattr_af = AF_UNSPEC;
902 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
903 const struct ether_header *save_eh)
905 struct mbuf *m = *m0;
910 * Prepend the header, optimize for the common case of
911 * eh pointing into the mbuf.
913 if ((const void *)(eh + 1) == (void *)m->m_data) {
914 m->m_data -= ETHER_HDR_LEN;
915 m->m_len += ETHER_HDR_LEN;
916 m->m_pkthdr.len += ETHER_HDR_LEN;
920 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
922 bcopy(save_eh, mtod(m, struct ether_header *),
930 ether_input_ipifunc(void *arg)
932 struct mbuf *m, *next;
933 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
939 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
945 ether_input_dispatch(struct mbuf_chain *chain)
950 logether(disp_beg, NULL);
951 for (i = 0; i < ncpus; ++i) {
952 if (chain[i].mc_head != NULL) {
953 lwkt_send_ipiq(globaldata_find(i),
954 ether_input_ipifunc, chain[i].mc_head);
958 logether(disp_beg, NULL);
959 if (chain->mc_head != NULL)
960 ether_input_ipifunc(chain->mc_head);
962 logether(disp_end, NULL);
966 ether_input_chain_init(struct mbuf_chain *chain)
971 for (i = 0; i < ncpus; ++i)
972 chain[i].mc_head = chain[i].mc_tail = NULL;
974 chain->mc_head = chain->mc_tail = NULL;
979 * Upper layer processing for a received Ethernet packet.
982 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
984 struct ether_header *eh;
985 int isr, discard = 0;
987 struct ip_fw *rule = NULL;
990 KASSERT(m->m_len >= ETHER_HDR_LEN,
991 ("ether header is no contiguous!\n"));
993 eh = mtod(m, struct ether_header *);
995 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
998 /* Extract info from dummynet tag */
999 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1000 KKASSERT(mtag != NULL);
1001 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1002 KKASSERT(rule != NULL);
1004 m_tag_delete(m, mtag);
1005 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1007 /* packet is passing the second time */
1013 * XXX: Okay, we need to call carp_forus() and - if it is for
1014 * us jump over code that does the normal check
1015 * "ac_enaddr == ether_dhost". The check sequence is a bit
1016 * different from OpenBSD, so we jump over as few code as
1017 * possible, to catch _all_ sanity checks. This needs
1018 * evaluation, to see if the carp ether_dhost values break any
1023 * Hold BGL and recheck ifp->if_carp
1026 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1035 * We got a packet which was unicast to a different Ethernet
1036 * address. If the driver is working properly, then this
1037 * situation can only happen when the interface is in
1038 * promiscuous mode. We defer the packet discarding until the
1039 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1042 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1043 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1044 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1045 if (ether_debug & 1) {
1046 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1047 "%02x:%02x:%02x:%02x:%02x:%02x "
1048 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1062 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1063 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1064 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1065 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1066 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1067 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1070 if ((ether_debug & 2) == 0)
1075 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1076 struct ether_header save_eh = *eh;
1078 /* XXX old crufty stuff, needs to be removed */
1079 m_adj(m, sizeof(struct ether_header));
1081 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1086 ether_restore_header(&m, eh, &save_eh);
1089 eh = mtod(m, struct ether_header *);
1092 ether_type = ntohs(eh->ether_type);
1093 KKASSERT(ether_type != ETHERTYPE_VLAN);
1095 if (m->m_flags & M_VLANTAG) {
1096 void (*vlan_input_func)(struct mbuf *);
1098 vlan_input_func = vlan_input_p;
1099 if (vlan_input_func != NULL) {
1102 m->m_pkthdr.rcvif->if_noproto++;
1109 * If we have been asked to discard this packet
1110 * (e.g. not for us), drop it before entering
1119 * Clear protocol specific flags,
1120 * before entering the upper layer.
1122 m->m_flags &= ~M_ETHER_FLAGS;
1124 /* Strip ethernet header. */
1125 m_adj(m, sizeof(struct ether_header));
1127 switch (ether_type) {
1130 if ((m->m_flags & M_LENCHECKED) == 0) {
1131 if (!ip_lengthcheck(&m, 0))
1134 if (ipflow_fastforward(m))
1140 if (ifp->if_flags & IFF_NOARP) {
1141 /* Discard packet if ARP is disabled on interface */
1150 case ETHERTYPE_IPV6:
1159 * Hold BGL and recheck ef_inputp
1162 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1173 case ETHERTYPE_MPLS:
1174 case ETHERTYPE_MPLS_MCAST:
1175 /* Should have been set by ether_input_chain(). */
1176 KKASSERT(m->m_flags & M_MPLSLABELED);
1183 * The accurate msgport is not determined before
1184 * we reach here, so recharacterize packet.
1186 m->m_flags &= ~M_HASH;
1190 * Hold BGL and recheck ef_inputp
1193 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1200 if (ng_ether_input_orphan_p != NULL) {
1202 * Hold BGL and recheck ng_ether_input_orphan_p
1205 if (ng_ether_input_orphan_p != NULL) {
1206 ng_ether_input_orphan_p(ifp, m, eh);
1216 if (m->m_flags & M_HASH) {
1217 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1218 netisr_handle(isr, m);
1222 * XXX Something is wrong,
1223 * we probably should panic here!
1225 m->m_flags &= ~M_HASH;
1226 ether_input_wronghash++;
1230 ether_input_requeue++;
1232 netisr_queue(isr, m);
1236 * First we perform any link layer operations, then continue to the
1237 * upper layers with ether_demux_oncpu().
1240 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1242 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1244 * Receiving interface's flags are changed, when this
1245 * packet is waiting for processing; discard it.
1252 * Tap the packet off here for a bridge. bridge_input()
1253 * will return NULL if it has consumed the packet, otherwise
1254 * it gets processed as normal. Note that bridge_input()
1255 * will always return the original packet if we need to
1256 * process it locally.
1258 if (ifp->if_bridge) {
1259 KASSERT(bridge_input_p != NULL,
1260 ("%s: if_bridge not loaded!", __func__));
1262 if(m->m_flags & M_ETHER_BRIDGED) {
1263 m->m_flags &= ~M_ETHER_BRIDGED;
1265 m = bridge_input_p(ifp, m);
1269 KASSERT(ifp == m->m_pkthdr.rcvif,
1270 ("bridge_input_p changed rcvif\n"));
1274 /* Handle ng_ether(4) processing, if any */
1275 if (ng_ether_input_p != NULL) {
1277 * Hold BGL and recheck ng_ether_input_p
1280 if (ng_ether_input_p != NULL)
1281 ng_ether_input_p(ifp, &m);
1288 /* Continue with upper layer processing */
1289 ether_demux_oncpu(ifp, m);
1293 * Perform certain functions of ether_input_chain():
1295 * - Update statistics
1296 * - Run bpf(4) tap if requested
1297 * Then pass the packet to ether_input_oncpu().
1299 * This function should be used by pseudo interface (e.g. vlan(4)),
1300 * when it tries to claim that the packet is received by it.
1306 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1308 /* Discard packet if interface is not up */
1309 if (!(ifp->if_flags & IFF_UP)) {
1315 * Change receiving interface. The bridge will often pass a flag to
1316 * ask that this not be done so ARPs get applied to the correct
1319 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1320 m->m_pkthdr.rcvif == NULL) {
1321 m->m_pkthdr.rcvif = ifp;
1324 /* Update statistics */
1326 ifp->if_ibytes += m->m_pkthdr.len;
1327 if (m->m_flags & (M_MCAST | M_BCAST))
1330 if (reinput_flags & REINPUT_RUNBPF)
1333 ether_input_oncpu(ifp, m);
1336 static __inline boolean_t
1337 ether_vlancheck(struct mbuf **m0)
1339 struct mbuf *m = *m0;
1340 struct ether_header *eh;
1341 uint16_t ether_type;
1343 eh = mtod(m, struct ether_header *);
1344 ether_type = ntohs(eh->ether_type);
1346 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1348 * Extract vlan tag if hardware does not do it for us
1350 vlan_ether_decap(&m);
1354 eh = mtod(m, struct ether_header *);
1355 ether_type = ntohs(eh->ether_type);
1358 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1360 * To prevent possible dangerous recursion,
1361 * we don't do vlan-in-vlan
1363 m->m_pkthdr.rcvif->if_noproto++;
1366 KKASSERT(ether_type != ETHERTYPE_VLAN);
1368 m->m_flags |= M_ETHER_VLANCHECKED;
1379 ether_input_handler(netmsg_t nmsg)
1381 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1382 struct ether_header *eh;
1388 ifp = m->m_pkthdr.rcvif;
1390 eh = mtod(m, struct ether_header *);
1391 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1392 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1393 ifp->if_addrlen) == 0)
1394 m->m_flags |= M_BCAST;
1396 m->m_flags |= M_MCAST;
1400 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1401 if (!ether_vlancheck(&m)) {
1402 KKASSERT(m == NULL);
1407 ether_input_oncpu(ifp, m);
1411 * Send the packet to the target msgport or queue it into 'chain'.
1413 * At this point the packet had better be characterized (M_HASH set),
1414 * so we know which cpu to send it to.
1417 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1419 struct netmsg_packet *pmsg;
1421 KKASSERT(m->m_flags & M_HASH);
1422 pmsg = &m->m_hdr.mh_netmsg;
1423 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1424 0, ether_input_handler);
1425 pmsg->nm_packet = m;
1426 pmsg->base.lmsg.u.ms_result = isr;
1428 if (chain != NULL) {
1429 int cpuid = m->m_pkthdr.hash;
1430 struct mbuf_chain *c;
1433 if (c->mc_head == NULL) {
1434 c->mc_head = c->mc_tail = m;
1436 c->mc_tail->m_nextpkt = m;
1439 m->m_nextpkt = NULL;
1441 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1446 * Process a received Ethernet packet.
1448 * The ethernet header is assumed to be in the mbuf so the caller
1449 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1450 * bytes in the first mbuf.
1452 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1453 * immediately. This situation happens when ether_input_chain is
1454 * accessed through ifnet.if_input.
1456 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1457 * bucket indexed by the target msgport's cpuid and the target msgport
1458 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1459 * must initialize 'chain' by calling ether_input_chain_init().
1460 * ether_input_dispatch must be called later to send ether frames
1461 * queued on 'chain' to their target msgport.
1464 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1465 struct mbuf_chain *chain)
1471 /* Discard packet if interface is not up */
1472 if (!(ifp->if_flags & IFF_UP)) {
1477 if (m->m_len < sizeof(struct ether_header)) {
1478 /* XXX error in the caller. */
1483 m->m_pkthdr.rcvif = ifp;
1485 logether(chain_beg, ifp);
1487 ETHER_BPF_MTAP(ifp, m);
1489 ifp->if_ibytes += m->m_pkthdr.len;
1491 if (ifp->if_flags & IFF_MONITOR) {
1492 struct ether_header *eh;
1494 eh = mtod(m, struct ether_header *);
1495 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1499 * Interface marked for monitoring; discard packet.
1503 logether(chain_end, ifp);
1508 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1509 * we can dispatch it immediately without further inspection.
1511 if (pi != NULL && (m->m_flags & M_HASH)) {
1513 ether_pktinfo_try++;
1515 netisr_hashcheck(pi->pi_netisr, m, pi);
1516 if (m->m_flags & M_HASH) {
1517 ether_dispatch(pi->pi_netisr, m, chain);
1519 ether_pktinfo_hit++;
1521 logether(chain_end, ifp);
1526 else if (ifp->if_capenable & IFCAP_RSS) {
1535 * Packet hash will be recalculated by software,
1536 * so clear the M_HASH flag set by the driver;
1537 * the hash value calculated by the hardware may
1538 * not be exactly what we want.
1540 m->m_flags &= ~M_HASH;
1542 if (!ether_vlancheck(&m)) {
1543 KKASSERT(m == NULL);
1544 logether(chain_end, ifp);
1548 isr = ether_characterize(&m);
1550 logether(chain_end, ifp);
1555 * Finally dispatch it
1557 ether_dispatch(isr, m, chain);
1559 logether(chain_end, ifp);
1563 ether_characterize(struct mbuf **m0)
1565 struct mbuf *m = *m0;
1566 struct ether_header *eh;
1567 uint16_t ether_type;
1570 eh = mtod(m, struct ether_header *);
1571 ether_type = ntohs(eh->ether_type);
1574 * Map ether type to netisr id.
1576 switch (ether_type) {
1588 case ETHERTYPE_IPV6:
1600 case ETHERTYPE_MPLS:
1601 case ETHERTYPE_MPLS_MCAST:
1602 m->m_flags |= M_MPLSLABELED;
1609 * NETISR_MAX is an invalid value; it is chosen to let
1610 * netisr_characterize() know that we have no clear
1611 * idea where this packet should go.
1618 * Ask the isr to characterize the packet since we couldn't.
1619 * This is an attempt to optimally get us onto the correct protocol
1622 netisr_characterize(isr, &m, sizeof(struct ether_header));
1629 ether_demux_handler(netmsg_t nmsg)
1631 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1637 ifp = m->m_pkthdr.rcvif;
1639 ether_demux_oncpu(ifp, m);
1643 ether_demux(struct mbuf *m)
1645 struct netmsg_packet *pmsg;
1648 isr = ether_characterize(&m);
1652 KKASSERT(m->m_flags & M_HASH);
1653 pmsg = &m->m_hdr.mh_netmsg;
1654 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1655 0, ether_demux_handler);
1656 pmsg->nm_packet = m;
1657 pmsg->base.lmsg.u.ms_result = isr;
1659 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1662 MODULE_VERSION(ether, 1);