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_ARGS struct ifnet *ifp
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_ARGS);
189 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
190 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
191 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
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, u_long 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 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
711 sdl->sdl_len = sizeof *sdl;
712 sdl->sdl_family = AF_LINK;
713 sdl->sdl_index = ifp->if_index;
714 sdl->sdl_type = IFT_ETHER;
715 sdl->sdl_alen = ETHER_ADDR_LEN;
716 e_addr = LLADDR(sdl);
717 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
718 *llsa = (struct sockaddr *)sdl;
723 sin6 = (struct sockaddr_in6 *)sa;
724 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
726 * An IP6 address of 0 means listen to all
727 * of the Ethernet multicast address used for IP6.
728 * (This is used for multicast routers.)
730 ifp->if_flags |= IFF_ALLMULTI;
734 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
735 return EADDRNOTAVAIL;
736 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
737 sdl->sdl_len = sizeof *sdl;
738 sdl->sdl_family = AF_LINK;
739 sdl->sdl_index = ifp->if_index;
740 sdl->sdl_type = IFT_ETHER;
741 sdl->sdl_alen = ETHER_ADDR_LEN;
742 e_addr = LLADDR(sdl);
743 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
744 *llsa = (struct sockaddr *)sdl;
750 * Well, the text isn't quite right, but it's the name
759 * This is for reference. We have a table-driven version
760 * of the little-endian crc32 generator, which is faster
761 * than the double-loop.
764 ether_crc32_le(const uint8_t *buf, size_t len)
766 uint32_t c, crc, carry;
769 crc = 0xffffffffU; /* initial value */
771 for (i = 0; i < len; i++) {
773 for (j = 0; j < 8; j++) {
774 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
778 crc = (crc ^ ETHER_CRC_POLY_LE);
786 ether_crc32_le(const uint8_t *buf, size_t len)
788 static const uint32_t crctab[] = {
789 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
790 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
791 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
792 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
797 crc = 0xffffffffU; /* initial value */
799 for (i = 0; i < len; i++) {
801 crc = (crc >> 4) ^ crctab[crc & 0xf];
802 crc = (crc >> 4) ^ crctab[crc & 0xf];
810 ether_crc32_be(const uint8_t *buf, size_t len)
812 uint32_t c, crc, carry;
815 crc = 0xffffffffU; /* initial value */
817 for (i = 0; i < len; i++) {
819 for (j = 0; j < 8; j++) {
820 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
824 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
832 * find the size of ethernet header, and call classifier
835 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
836 struct altq_pktattr *pktattr)
838 struct ether_header *eh;
840 int hlen, af, hdrsize;
843 hlen = sizeof(struct ether_header);
844 eh = mtod(m, struct ether_header *);
846 ether_type = ntohs(eh->ether_type);
847 if (ether_type < ETHERMTU) {
849 struct llc *llc = (struct llc *)(eh + 1);
852 if (m->m_len < hlen ||
853 llc->llc_dsap != LLC_SNAP_LSAP ||
854 llc->llc_ssap != LLC_SNAP_LSAP ||
855 llc->llc_control != LLC_UI)
856 goto bad; /* not snap! */
858 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
861 if (ether_type == ETHERTYPE_IP) {
863 hdrsize = 20; /* sizeof(struct ip) */
865 } else if (ether_type == ETHERTYPE_IPV6) {
867 hdrsize = 40; /* sizeof(struct ip6_hdr) */
872 while (m->m_len <= hlen) {
876 hdr = m->m_data + hlen;
877 if (m->m_len < hlen + hdrsize) {
879 * ip header is not in a single mbuf. this should not
880 * happen in the current code.
881 * (todo: use m_pulldown in the future)
887 ifq_classify(ifq, m, af, pktattr);
894 pktattr->pattr_class = NULL;
895 pktattr->pattr_hdr = NULL;
896 pktattr->pattr_af = AF_UNSPEC;
900 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
901 const struct ether_header *save_eh)
903 struct mbuf *m = *m0;
908 * Prepend the header, optimize for the common case of
909 * eh pointing into the mbuf.
911 if ((const void *)(eh + 1) == (void *)m->m_data) {
912 m->m_data -= ETHER_HDR_LEN;
913 m->m_len += ETHER_HDR_LEN;
914 m->m_pkthdr.len += ETHER_HDR_LEN;
918 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
920 bcopy(save_eh, mtod(m, struct ether_header *),
928 ether_input_ipifunc(void *arg)
930 struct mbuf *m, *next;
931 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
937 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
943 ether_input_dispatch(struct mbuf_chain *chain)
948 logether(disp_beg, NULL);
949 for (i = 0; i < ncpus; ++i) {
950 if (chain[i].mc_head != NULL) {
951 lwkt_send_ipiq(globaldata_find(i),
952 ether_input_ipifunc, chain[i].mc_head);
956 logether(disp_beg, NULL);
957 if (chain->mc_head != NULL)
958 ether_input_ipifunc(chain->mc_head);
960 logether(disp_end, NULL);
964 ether_input_chain_init(struct mbuf_chain *chain)
969 for (i = 0; i < ncpus; ++i)
970 chain[i].mc_head = chain[i].mc_tail = NULL;
972 chain->mc_head = chain->mc_tail = NULL;
977 * Upper layer processing for a received Ethernet packet.
980 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
982 struct ether_header *eh;
983 int isr, discard = 0;
985 struct ip_fw *rule = NULL;
988 KASSERT(m->m_len >= ETHER_HDR_LEN,
989 ("ether header is no contiguous!\n"));
991 eh = mtod(m, struct ether_header *);
993 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
996 /* Extract info from dummynet tag */
997 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
998 KKASSERT(mtag != NULL);
999 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1000 KKASSERT(rule != NULL);
1002 m_tag_delete(m, mtag);
1003 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1005 /* packet is passing the second time */
1011 * XXX: Okay, we need to call carp_forus() and - if it is for
1012 * us jump over code that does the normal check
1013 * "ac_enaddr == ether_dhost". The check sequence is a bit
1014 * different from OpenBSD, so we jump over as few code as
1015 * possible, to catch _all_ sanity checks. This needs
1016 * evaluation, to see if the carp ether_dhost values break any
1021 * Hold BGL and recheck ifp->if_carp
1024 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1033 * We got a packet which was unicast to a different Ethernet
1034 * address. If the driver is working properly, then this
1035 * situation can only happen when the interface is in
1036 * promiscuous mode. We defer the packet discarding until the
1037 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1040 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1041 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1042 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1043 if (ether_debug & 1) {
1044 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1045 "%02x:%02x:%02x:%02x:%02x:%02x "
1046 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1060 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1061 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1062 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1063 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1064 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1065 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1068 if ((ether_debug & 2) == 0)
1073 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1074 struct ether_header save_eh = *eh;
1076 /* XXX old crufty stuff, needs to be removed */
1077 m_adj(m, sizeof(struct ether_header));
1079 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1084 ether_restore_header(&m, eh, &save_eh);
1087 eh = mtod(m, struct ether_header *);
1090 ether_type = ntohs(eh->ether_type);
1091 KKASSERT(ether_type != ETHERTYPE_VLAN);
1093 if (m->m_flags & M_VLANTAG) {
1094 void (*vlan_input_func)(struct mbuf *);
1096 vlan_input_func = vlan_input_p;
1097 if (vlan_input_func != NULL) {
1100 m->m_pkthdr.rcvif->if_noproto++;
1107 * If we have been asked to discard this packet
1108 * (e.g. not for us), drop it before entering
1117 * Clear protocol specific flags,
1118 * before entering the upper layer.
1120 m->m_flags &= ~M_ETHER_FLAGS;
1122 /* Strip ethernet header. */
1123 m_adj(m, sizeof(struct ether_header));
1125 switch (ether_type) {
1128 if ((m->m_flags & M_LENCHECKED) == 0) {
1129 if (!ip_lengthcheck(&m, 0))
1132 if (ipflow_fastforward(m))
1138 if (ifp->if_flags & IFF_NOARP) {
1139 /* Discard packet if ARP is disabled on interface */
1148 case ETHERTYPE_IPV6:
1157 * Hold BGL and recheck ef_inputp
1160 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1171 case ETHERTYPE_MPLS:
1172 case ETHERTYPE_MPLS_MCAST:
1173 /* Should have been set by ether_input_chain(). */
1174 KKASSERT(m->m_flags & M_MPLSLABELED);
1181 * The accurate msgport is not determined before
1182 * we reach here, so recharacterize packet.
1184 m->m_flags &= ~M_HASH;
1188 * Hold BGL and recheck ef_inputp
1191 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1198 if (ng_ether_input_orphan_p != NULL) {
1200 * Hold BGL and recheck ng_ether_input_orphan_p
1203 if (ng_ether_input_orphan_p != NULL) {
1204 ng_ether_input_orphan_p(ifp, m, eh);
1214 if (m->m_flags & M_HASH) {
1215 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1216 netisr_handle(isr, m);
1220 * XXX Something is wrong,
1221 * we probably should panic here!
1223 m->m_flags &= ~M_HASH;
1224 ether_input_wronghash++;
1228 ether_input_requeue++;
1230 netisr_queue(isr, m);
1234 * First we perform any link layer operations, then continue to the
1235 * upper layers with ether_demux_oncpu().
1238 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1240 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1242 * Receiving interface's flags are changed, when this
1243 * packet is waiting for processing; discard it.
1250 * Tap the packet off here for a bridge. bridge_input()
1251 * will return NULL if it has consumed the packet, otherwise
1252 * it gets processed as normal. Note that bridge_input()
1253 * will always return the original packet if we need to
1254 * process it locally.
1256 if (ifp->if_bridge) {
1257 KASSERT(bridge_input_p != NULL,
1258 ("%s: if_bridge not loaded!", __func__));
1260 if(m->m_flags & M_ETHER_BRIDGED) {
1261 m->m_flags &= ~M_ETHER_BRIDGED;
1263 m = bridge_input_p(ifp, m);
1267 KASSERT(ifp == m->m_pkthdr.rcvif,
1268 ("bridge_input_p changed rcvif\n"));
1272 /* Handle ng_ether(4) processing, if any */
1273 if (ng_ether_input_p != NULL) {
1275 * Hold BGL and recheck ng_ether_input_p
1278 if (ng_ether_input_p != NULL)
1279 ng_ether_input_p(ifp, &m);
1286 /* Continue with upper layer processing */
1287 ether_demux_oncpu(ifp, m);
1291 * Perform certain functions of ether_input_chain():
1293 * - Update statistics
1294 * - Run bpf(4) tap if requested
1295 * Then pass the packet to ether_input_oncpu().
1297 * This function should be used by pseudo interface (e.g. vlan(4)),
1298 * when it tries to claim that the packet is received by it.
1304 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1306 /* Discard packet if interface is not up */
1307 if (!(ifp->if_flags & IFF_UP)) {
1313 * Change receiving interface. The bridge will often pass a flag to
1314 * ask that this not be done so ARPs get applied to the correct
1317 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1318 m->m_pkthdr.rcvif == NULL) {
1319 m->m_pkthdr.rcvif = ifp;
1322 /* Update statistics */
1324 ifp->if_ibytes += m->m_pkthdr.len;
1325 if (m->m_flags & (M_MCAST | M_BCAST))
1328 if (reinput_flags & REINPUT_RUNBPF)
1331 ether_input_oncpu(ifp, m);
1334 static __inline boolean_t
1335 ether_vlancheck(struct mbuf **m0)
1337 struct mbuf *m = *m0;
1338 struct ether_header *eh;
1339 uint16_t ether_type;
1341 eh = mtod(m, struct ether_header *);
1342 ether_type = ntohs(eh->ether_type);
1344 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1346 * Extract vlan tag if hardware does not do it for us
1348 vlan_ether_decap(&m);
1352 eh = mtod(m, struct ether_header *);
1353 ether_type = ntohs(eh->ether_type);
1356 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1358 * To prevent possible dangerous recursion,
1359 * we don't do vlan-in-vlan
1361 m->m_pkthdr.rcvif->if_noproto++;
1364 KKASSERT(ether_type != ETHERTYPE_VLAN);
1366 m->m_flags |= M_ETHER_VLANCHECKED;
1377 ether_input_handler(netmsg_t nmsg)
1379 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1380 struct ether_header *eh;
1386 ifp = m->m_pkthdr.rcvif;
1388 eh = mtod(m, struct ether_header *);
1389 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1390 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1391 ifp->if_addrlen) == 0)
1392 m->m_flags |= M_BCAST;
1394 m->m_flags |= M_MCAST;
1398 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1399 if (!ether_vlancheck(&m)) {
1400 KKASSERT(m == NULL);
1405 ether_input_oncpu(ifp, m);
1409 * Send the packet to the target msgport or queue it into 'chain'.
1411 * At this point the packet had better be characterized (M_HASH set),
1412 * so we know which cpu to send it to.
1415 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1417 struct netmsg_packet *pmsg;
1419 KKASSERT(m->m_flags & M_HASH);
1420 pmsg = &m->m_hdr.mh_netmsg;
1421 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1422 0, ether_input_handler);
1423 pmsg->nm_packet = m;
1424 pmsg->base.lmsg.u.ms_result = isr;
1426 if (chain != NULL) {
1427 int cpuid = m->m_pkthdr.hash;
1428 struct mbuf_chain *c;
1431 if (c->mc_head == NULL) {
1432 c->mc_head = c->mc_tail = m;
1434 c->mc_tail->m_nextpkt = m;
1437 m->m_nextpkt = NULL;
1439 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1444 * Process a received Ethernet packet.
1446 * The ethernet header is assumed to be in the mbuf so the caller
1447 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1448 * bytes in the first mbuf.
1450 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1451 * immediately. This situation happens when ether_input_chain is
1452 * accessed through ifnet.if_input.
1454 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1455 * bucket indexed by the target msgport's cpuid and the target msgport
1456 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1457 * must initialize 'chain' by calling ether_input_chain_init().
1458 * ether_input_dispatch must be called later to send ether frames
1459 * queued on 'chain' to their target msgport.
1462 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1463 struct mbuf_chain *chain)
1469 /* Discard packet if interface is not up */
1470 if (!(ifp->if_flags & IFF_UP)) {
1475 if (m->m_len < sizeof(struct ether_header)) {
1476 /* XXX error in the caller. */
1481 m->m_pkthdr.rcvif = ifp;
1483 logether(chain_beg, ifp);
1485 ETHER_BPF_MTAP(ifp, m);
1487 ifp->if_ibytes += m->m_pkthdr.len;
1489 if (ifp->if_flags & IFF_MONITOR) {
1490 struct ether_header *eh;
1492 eh = mtod(m, struct ether_header *);
1493 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1497 * Interface marked for monitoring; discard packet.
1501 logether(chain_end, ifp);
1506 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1507 * we can dispatch it immediately without further inspection.
1509 if (pi != NULL && (m->m_flags & M_HASH)) {
1511 ether_pktinfo_try++;
1513 netisr_hashcheck(pi->pi_netisr, m, pi);
1514 if (m->m_flags & M_HASH) {
1515 ether_dispatch(pi->pi_netisr, m, chain);
1517 ether_pktinfo_hit++;
1519 logether(chain_end, ifp);
1524 else if (ifp->if_capenable & IFCAP_RSS) {
1533 * Packet hash will be recalculated by software,
1534 * so clear the M_HASH flag set by the driver;
1535 * the hash value calculated by the hardware may
1536 * not be exactly what we want.
1538 m->m_flags &= ~M_HASH;
1540 if (!ether_vlancheck(&m)) {
1541 KKASSERT(m == NULL);
1542 logether(chain_end, ifp);
1546 isr = ether_characterize(&m);
1548 logether(chain_end, ifp);
1553 * Finally dispatch it
1555 ether_dispatch(isr, m, chain);
1557 logether(chain_end, ifp);
1561 ether_characterize(struct mbuf **m0)
1563 struct mbuf *m = *m0;
1564 struct ether_header *eh;
1565 uint16_t ether_type;
1568 eh = mtod(m, struct ether_header *);
1569 ether_type = ntohs(eh->ether_type);
1572 * Map ether type to netisr id.
1574 switch (ether_type) {
1586 case ETHERTYPE_IPV6:
1598 case ETHERTYPE_MPLS:
1599 case ETHERTYPE_MPLS_MCAST:
1600 m->m_flags |= M_MPLSLABELED;
1607 * NETISR_MAX is an invalid value; it is chosen to let
1608 * netisr_characterize() know that we have no clear
1609 * idea where this packet should go.
1616 * Ask the isr to characterize the packet since we couldn't.
1617 * This is an attempt to optimally get us onto the correct protocol
1620 netisr_characterize(isr, &m, sizeof(struct ether_header));
1627 ether_demux_handler(netmsg_t nmsg)
1629 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1635 ifp = m->m_pkthdr.rcvif;
1637 ether_demux_oncpu(ifp, m);
1641 ether_demux(struct mbuf *m)
1643 struct netmsg_packet *pmsg;
1646 isr = ether_characterize(&m);
1650 KKASSERT(m->m_flags & M_HASH);
1651 pmsg = &m->m_hdr.mh_netmsg;
1652 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1653 0, ether_demux_handler);
1654 pmsg->nm_packet = m;
1655 pmsg->base.lmsg.u.ms_result = isr;
1657 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1660 MODULE_VERSION(ether, 1);