2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
38 #include "opt_inet6.h"
41 #include "opt_netgraph.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/globaldata.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/netmsg2.h>
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/ip_flow.h>
79 #include <net/ipfw/ip_fw.h>
80 #include <net/dummynet/ip_dummynet.h>
83 #include <netinet6/nd6.h>
87 #include <netinet/ip_carp.h>
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
93 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
94 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
95 short *tp, int *hlen);
99 #include <netproto/mpls/mpls.h>
102 /* netgraph node hooks for ng_ether(4) */
103 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
104 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
105 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
106 void (*ng_ether_attach_p)(struct ifnet *ifp);
107 void (*ng_ether_detach_p)(struct ifnet *ifp);
109 void (*vlan_input_p)(struct mbuf *);
111 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
113 static void ether_restore_header(struct mbuf **, const struct ether_header *,
114 const struct ether_header *);
115 static int ether_characterize(struct mbuf **);
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
122 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
123 struct ifnet *(*bridge_interface_p)(void *if_bridge);
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
137 const struct ether_header *eh);
139 static int ether_ipfw;
140 static u_long ether_restore_hdr;
141 static u_long ether_prepend_hdr;
142 static u_long ether_input_wronghash;
143 static int ether_debug;
146 static u_long ether_pktinfo_try;
147 static u_long ether_pktinfo_hit;
148 static u_long ether_rss_nopi;
149 static u_long ether_rss_nohash;
150 static u_long ether_input_requeue;
153 SYSCTL_DECL(_net_link);
154 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
155 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
156 ðer_debug, 0, "Ether debug");
157 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
158 ðer_ipfw, 0, "Pass ether pkts through firewall");
159 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
160 ðer_restore_hdr, 0, "# of ether header restoration");
161 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
162 ðer_prepend_hdr, 0,
163 "# of ether header restoration which prepends mbuf");
164 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
165 ðer_input_wronghash, 0, "# of input packets with wrong hash");
167 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
168 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
170 ðer_rss_nohash, 0, "# of packets do not have hash");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
172 ðer_pktinfo_try, 0,
173 "# of tries to find packets' msgport using pktinfo");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
175 ðer_pktinfo_hit, 0,
176 "# of packets whose msgport are found using pktinfo");
177 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
178 ðer_input_requeue, 0, "# of input packets gets requeued");
181 #define ETHER_KTR_STR "ifp=%p"
182 #define ETHER_KTR_ARGS struct ifnet *ifp
184 #define KTR_ETHERNET KTR_ALL
186 KTR_INFO_MASTER(ether);
187 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
188 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
189 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
190 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
191 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
194 * Ethernet output routine.
195 * Encapsulate a packet of type family for the local net.
196 * Use trailer local net encapsulation if enough data in first
197 * packet leaves a multiple of 512 bytes of data in remainder.
198 * Assumes that ifp is actually pointer to arpcom structure.
201 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
204 struct ether_header *eh, *deh;
207 int hlen = ETHER_HDR_LEN; /* link layer header length */
208 struct arpcom *ac = IFP2AC(ifp);
211 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
213 if (ifp->if_flags & IFF_MONITOR)
215 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
218 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
221 eh = mtod(m, struct ether_header *);
222 edst = eh->ether_dhost;
225 * Fill in the destination ethernet address and frame type.
227 switch (dst->sa_family) {
230 if (!arpresolve(ifp, rt, m, dst, edst))
231 return (0); /* if not yet resolved */
233 if (m->m_flags & M_MPLSLABELED)
234 eh->ether_type = htons(ETHERTYPE_MPLS);
237 eh->ether_type = htons(ETHERTYPE_IP);
242 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
243 return (0); /* Something bad happenned. */
244 eh->ether_type = htons(ETHERTYPE_IPV6);
249 if (ef_outputp != NULL) {
251 * Hold BGL and recheck ef_outputp
254 if (ef_outputp != NULL) {
255 error = ef_outputp(ifp, &m, dst,
256 &eh->ether_type, &hlen);
265 eh->ether_type = htons(ETHERTYPE_IPX);
266 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
267 edst, ETHER_ADDR_LEN);
270 case pseudo_AF_HDRCMPLT:
272 loop_copy = -1; /* if this is for us, don't do it */
273 deh = (struct ether_header *)dst->sa_data;
274 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
275 eh->ether_type = deh->ether_type;
279 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
280 gotoerr(EAFNOSUPPORT);
283 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
284 memcpy(eh->ether_shost,
285 ((struct ether_header *)dst->sa_data)->ether_shost,
288 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
291 * Bridges require special output handling.
293 if (ifp->if_bridge) {
294 KASSERT(bridge_output_p != NULL,
295 ("%s: if_bridge not loaded!", __func__));
296 return bridge_output_p(ifp, m);
300 * If a simplex interface, and the packet is being sent to our
301 * Ethernet address or a broadcast address, loopback a copy.
302 * XXX To make a simplex device behave exactly like a duplex
303 * device, we should copy in the case of sending to our own
304 * ethernet address (thus letting the original actually appear
305 * on the wire). However, we don't do that here for security
306 * reasons and compatibility with the original behavior.
308 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
311 if (m->m_pkthdr.csum_flags & CSUM_IP)
312 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
313 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
314 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
315 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
318 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
319 n->m_pkthdr.csum_flags |= csum_flags;
320 if (csum_flags & CSUM_DATA_VALID)
321 n->m_pkthdr.csum_data = 0xffff;
322 if_simloop(ifp, n, dst->sa_family, hlen);
325 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
326 ETHER_ADDR_LEN) == 0) {
327 m->m_pkthdr.csum_flags |= csum_flags;
328 if (csum_flags & CSUM_DATA_VALID)
329 m->m_pkthdr.csum_data = 0xffff;
330 if_simloop(ifp, m, dst->sa_family, hlen);
331 return (0); /* XXX */
338 * Hold BGL and recheck ifp->if_carp
341 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
350 /* Handle ng_ether(4) processing, if any */
351 if (ng_ether_output_p != NULL) {
353 * Hold BGL and recheck ng_ether_output_p
356 if (ng_ether_output_p != NULL) {
357 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
369 /* Continue with link-layer output */
370 return ether_output_frame(ifp, m);
378 * Returns the bridge interface an ifp is associated
381 * Only call if ifp->if_bridge != NULL.
384 ether_bridge_interface(struct ifnet *ifp)
386 if (bridge_interface_p)
387 return(bridge_interface_p(ifp->if_bridge));
392 * Ethernet link layer output routine to send a raw frame to the device.
394 * This assumes that the 14 byte Ethernet header is present and contiguous
398 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
400 struct ip_fw *rule = NULL;
402 struct altq_pktattr pktattr;
404 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
406 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
409 /* Extract info from dummynet tag */
410 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
411 KKASSERT(mtag != NULL);
412 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
413 KKASSERT(rule != NULL);
415 m_tag_delete(m, mtag);
416 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
419 if (ifq_is_enabled(&ifp->if_snd))
420 altq_etherclassify(&ifp->if_snd, m, &pktattr);
422 if (IPFW_LOADED && ether_ipfw != 0) {
423 struct ether_header save_eh, *eh;
425 eh = mtod(m, struct ether_header *);
427 m_adj(m, ETHER_HDR_LEN);
428 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
432 return ENOBUFS; /* pkt dropped */
434 return 0; /* consumed e.g. in a pipe */
437 /* packet was ok, restore the ethernet header */
438 ether_restore_header(&m, eh, &save_eh);
447 * Queue message on interface, update output statistics if
448 * successful, and start output if interface not yet active.
450 error = ifq_dispatch(ifp, m, &pktattr);
455 * ipfw processing for ethernet packets (in and out).
456 * The second parameter is NULL from ether_demux(), and ifp from
457 * ether_output_frame().
460 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
461 const struct ether_header *eh)
463 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
464 struct ip_fw_args args;
469 if (*rule != NULL && fw_one_pass)
470 return TRUE; /* dummynet packet, already partially processed */
473 * I need some amount of data to be contiguous.
475 i = min((*m0)->m_pkthdr.len, max_protohdr);
476 if ((*m0)->m_len < i) {
477 *m0 = m_pullup(*m0, i);
485 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
486 m_tag_delete(*m0, mtag);
487 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
488 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
489 KKASSERT(mtag != NULL);
490 m_tag_delete(*m0, mtag);
491 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
494 args.m = *m0; /* the packet we are looking at */
495 args.oif = dst; /* destination, if any */
496 args.rule = *rule; /* matching rule to restart */
497 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
498 i = ip_fw_chk_ptr(&args);
513 * XXX at some point add support for divert/forward actions.
514 * If none of the above matches, we have to drop the pkt.
520 * Pass the pkt to dummynet, which consumes it.
522 m = *m0; /* pass the original to dummynet */
523 *m0 = NULL; /* and nothing back to the caller */
525 ether_restore_header(&m, eh, &save_eh);
529 ip_fw_dn_io_ptr(m, args.cookie,
530 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
535 panic("unknown ipfw return value: %d\n", i);
540 ether_input(struct ifnet *ifp, struct mbuf *m)
542 ether_input_chain(ifp, m, NULL, NULL);
546 * Perform common duties while attaching to interface list
549 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
551 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
556 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
557 lwkt_serialize_t serializer)
559 struct sockaddr_dl *sdl;
561 ifp->if_type = IFT_ETHER;
562 ifp->if_addrlen = ETHER_ADDR_LEN;
563 ifp->if_hdrlen = ETHER_HDR_LEN;
564 if_attach(ifp, serializer);
565 ifp->if_mtu = ETHERMTU;
566 if (ifp->if_baudrate == 0)
567 ifp->if_baudrate = 10000000;
568 ifp->if_output = ether_output;
569 ifp->if_input = ether_input;
570 ifp->if_resolvemulti = ether_resolvemulti;
571 ifp->if_broadcastaddr = etherbroadcastaddr;
572 sdl = IF_LLSOCKADDR(ifp);
573 sdl->sdl_type = IFT_ETHER;
574 sdl->sdl_alen = ifp->if_addrlen;
575 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
577 * XXX Keep the current drivers happy.
578 * XXX Remove once all drivers have been cleaned up
580 if (lla != IFP2AC(ifp)->ac_enaddr)
581 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
582 bpfattach(ifp, dlt, hdrlen);
583 if (ng_ether_attach_p != NULL)
584 (*ng_ether_attach_p)(ifp);
586 if_printf(ifp, "MAC address: %6D\n", lla, ":");
590 * Perform common duties while detaching an Ethernet interface
593 ether_ifdetach(struct ifnet *ifp)
597 if (ng_ether_detach_p != NULL)
598 (*ng_ether_detach_p)(ifp);
604 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
606 struct ifaddr *ifa = (struct ifaddr *) data;
607 struct ifreq *ifr = (struct ifreq *) data;
610 #define IF_INIT(ifp) \
612 if (((ifp)->if_flags & IFF_UP) == 0) { \
613 (ifp)->if_flags |= IFF_UP; \
614 (ifp)->if_init((ifp)->if_softc); \
618 ASSERT_IFNET_SERIALIZED_ALL(ifp);
622 switch (ifa->ifa_addr->sa_family) {
625 IF_INIT(ifp); /* before arpwhohas */
626 arp_ifinit(ifp, ifa);
631 * XXX - This code is probably wrong
635 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
636 struct arpcom *ac = IFP2AC(ifp);
638 if (ipx_nullhost(*ina))
639 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
641 bcopy(ina->x_host.c_host, ac->ac_enaddr,
642 sizeof ac->ac_enaddr);
644 IF_INIT(ifp); /* Set new address. */
655 bcopy(IFP2AC(ifp)->ac_enaddr,
656 ((struct sockaddr *)ifr->ifr_data)->sa_data,
662 * Set the interface MTU.
664 if (ifr->ifr_mtu > ETHERMTU) {
667 ifp->if_mtu = ifr->ifr_mtu;
682 struct sockaddr **llsa,
685 struct sockaddr_dl *sdl;
687 struct sockaddr_in *sin;
690 struct sockaddr_in6 *sin6;
694 switch(sa->sa_family) {
697 * No mapping needed. Just check that it's a valid MC address.
699 sdl = (struct sockaddr_dl *)sa;
700 e_addr = LLADDR(sdl);
701 if ((e_addr[0] & 1) != 1)
702 return EADDRNOTAVAIL;
708 sin = (struct sockaddr_in *)sa;
709 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
710 return EADDRNOTAVAIL;
711 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
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 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
738 sdl->sdl_len = sizeof *sdl;
739 sdl->sdl_family = AF_LINK;
740 sdl->sdl_index = ifp->if_index;
741 sdl->sdl_type = IFT_ETHER;
742 sdl->sdl_alen = ETHER_ADDR_LEN;
743 e_addr = LLADDR(sdl);
744 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
745 *llsa = (struct sockaddr *)sdl;
751 * Well, the text isn't quite right, but it's the name
760 * This is for reference. We have a table-driven version
761 * of the little-endian crc32 generator, which is faster
762 * than the double-loop.
765 ether_crc32_le(const uint8_t *buf, size_t len)
767 uint32_t c, crc, carry;
770 crc = 0xffffffffU; /* initial value */
772 for (i = 0; i < len; i++) {
774 for (j = 0; j < 8; j++) {
775 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
779 crc = (crc ^ ETHER_CRC_POLY_LE);
787 ether_crc32_le(const uint8_t *buf, size_t len)
789 static const uint32_t crctab[] = {
790 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
791 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
792 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
793 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
798 crc = 0xffffffffU; /* initial value */
800 for (i = 0; i < len; i++) {
802 crc = (crc >> 4) ^ crctab[crc & 0xf];
803 crc = (crc >> 4) ^ crctab[crc & 0xf];
811 ether_crc32_be(const uint8_t *buf, size_t len)
813 uint32_t c, crc, carry;
816 crc = 0xffffffffU; /* initial value */
818 for (i = 0; i < len; i++) {
820 for (j = 0; j < 8; j++) {
821 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
825 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
833 * find the size of ethernet header, and call classifier
836 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
837 struct altq_pktattr *pktattr)
839 struct ether_header *eh;
841 int hlen, af, hdrsize;
844 hlen = sizeof(struct ether_header);
845 eh = mtod(m, struct ether_header *);
847 ether_type = ntohs(eh->ether_type);
848 if (ether_type < ETHERMTU) {
850 struct llc *llc = (struct llc *)(eh + 1);
853 if (m->m_len < hlen ||
854 llc->llc_dsap != LLC_SNAP_LSAP ||
855 llc->llc_ssap != LLC_SNAP_LSAP ||
856 llc->llc_control != LLC_UI)
857 goto bad; /* not snap! */
859 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
862 if (ether_type == ETHERTYPE_IP) {
864 hdrsize = 20; /* sizeof(struct ip) */
866 } else if (ether_type == ETHERTYPE_IPV6) {
868 hdrsize = 40; /* sizeof(struct ip6_hdr) */
873 while (m->m_len <= hlen) {
877 hdr = m->m_data + hlen;
878 if (m->m_len < hlen + hdrsize) {
880 * ip header is not in a single mbuf. this should not
881 * happen in the current code.
882 * (todo: use m_pulldown in the future)
888 ifq_classify(ifq, m, af, pktattr);
895 pktattr->pattr_class = NULL;
896 pktattr->pattr_hdr = NULL;
897 pktattr->pattr_af = AF_UNSPEC;
901 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
902 const struct ether_header *save_eh)
904 struct mbuf *m = *m0;
909 * Prepend the header, optimize for the common case of
910 * eh pointing into the mbuf.
912 if ((const void *)(eh + 1) == (void *)m->m_data) {
913 m->m_data -= ETHER_HDR_LEN;
914 m->m_len += ETHER_HDR_LEN;
915 m->m_pkthdr.len += ETHER_HDR_LEN;
919 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
921 bcopy(save_eh, mtod(m, struct ether_header *),
929 ether_input_ipifunc(void *arg)
931 struct mbuf *m, *next;
932 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
938 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
944 ether_input_dispatch(struct mbuf_chain *chain)
949 logether(disp_beg, NULL);
950 for (i = 0; i < ncpus; ++i) {
951 if (chain[i].mc_head != NULL) {
952 lwkt_send_ipiq(globaldata_find(i),
953 ether_input_ipifunc, chain[i].mc_head);
957 logether(disp_beg, NULL);
958 if (chain->mc_head != NULL)
959 ether_input_ipifunc(chain->mc_head);
961 logether(disp_end, NULL);
965 ether_input_chain_init(struct mbuf_chain *chain)
970 for (i = 0; i < ncpus; ++i)
971 chain[i].mc_head = chain[i].mc_tail = NULL;
973 chain->mc_head = chain->mc_tail = NULL;
978 * Upper layer processing for a received Ethernet packet.
981 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
983 struct ether_header *eh;
984 int isr, discard = 0;
986 struct ip_fw *rule = NULL;
989 KASSERT(m->m_len >= ETHER_HDR_LEN,
990 ("ether header is not contiguous!\n"));
992 eh = mtod(m, struct ether_header *);
994 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
997 /* Extract info from dummynet tag */
998 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
999 KKASSERT(mtag != NULL);
1000 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1001 KKASSERT(rule != NULL);
1003 m_tag_delete(m, mtag);
1004 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1006 /* packet is passing the second time */
1012 * XXX: Okay, we need to call carp_forus() and - if it is for
1013 * us jump over code that does the normal check
1014 * "ac_enaddr == ether_dhost". The check sequence is a bit
1015 * different from OpenBSD, so we jump over as few code as
1016 * possible, to catch _all_ sanity checks. This needs
1017 * evaluation, to see if the carp ether_dhost values break any
1022 * Hold BGL and recheck ifp->if_carp
1025 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1034 * We got a packet which was unicast to a different Ethernet
1035 * address. If the driver is working properly, then this
1036 * situation can only happen when the interface is in
1037 * promiscuous mode. We defer the packet discarding until the
1038 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1041 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1042 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1043 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1044 if (ether_debug & 1) {
1045 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1046 "%02x:%02x:%02x:%02x:%02x:%02x "
1047 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1061 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1062 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1063 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1064 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1065 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1066 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1069 if ((ether_debug & 2) == 0)
1074 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1075 struct ether_header save_eh = *eh;
1077 /* XXX old crufty stuff, needs to be removed */
1078 m_adj(m, sizeof(struct ether_header));
1080 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1085 ether_restore_header(&m, eh, &save_eh);
1088 eh = mtod(m, struct ether_header *);
1091 ether_type = ntohs(eh->ether_type);
1092 KKASSERT(ether_type != ETHERTYPE_VLAN);
1094 if (m->m_flags & M_VLANTAG) {
1095 void (*vlan_input_func)(struct mbuf *);
1097 vlan_input_func = vlan_input_p;
1098 if (vlan_input_func != NULL) {
1101 m->m_pkthdr.rcvif->if_noproto++;
1108 * If we have been asked to discard this packet
1109 * (e.g. not for us), drop it before entering
1118 * Clear protocol specific flags,
1119 * before entering the upper layer.
1121 m->m_flags &= ~M_ETHER_FLAGS;
1123 /* Strip ethernet header. */
1124 m_adj(m, sizeof(struct ether_header));
1126 switch (ether_type) {
1129 if ((m->m_flags & M_LENCHECKED) == 0) {
1130 if (!ip_lengthcheck(&m, 0))
1133 if (ipflow_fastforward(m))
1139 if (ifp->if_flags & IFF_NOARP) {
1140 /* Discard packet if ARP is disabled on interface */
1149 case ETHERTYPE_IPV6:
1158 * Hold BGL and recheck ef_inputp
1161 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1172 case ETHERTYPE_MPLS:
1173 case ETHERTYPE_MPLS_MCAST:
1174 /* Should have been set by ether_input_chain(). */
1175 KKASSERT(m->m_flags & M_MPLSLABELED);
1182 * The accurate msgport is not determined before
1183 * we reach here, so recharacterize packet.
1185 m->m_flags &= ~M_HASH;
1189 * Hold BGL and recheck ef_inputp
1192 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1199 if (ng_ether_input_orphan_p != NULL) {
1201 * Put back the ethernet header so netgraph has a
1202 * consistent view of inbound packets.
1204 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1207 * M_PREPEND frees the mbuf in case of failure.
1212 * Hold BGL and recheck ng_ether_input_orphan_p
1215 if (ng_ether_input_orphan_p != NULL) {
1216 ng_ether_input_orphan_p(ifp, m);
1226 if (m->m_flags & M_HASH) {
1227 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1228 netisr_handle(isr, m);
1232 * XXX Something is wrong,
1233 * we probably should panic here!
1235 m->m_flags &= ~M_HASH;
1236 ether_input_wronghash++;
1240 ether_input_requeue++;
1242 netisr_queue(isr, m);
1246 * First we perform any link layer operations, then continue to the
1247 * upper layers with ether_demux_oncpu().
1250 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1252 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1254 * Receiving interface's flags are changed, when this
1255 * packet is waiting for processing; discard it.
1262 * Tap the packet off here for a bridge. bridge_input()
1263 * will return NULL if it has consumed the packet, otherwise
1264 * it gets processed as normal. Note that bridge_input()
1265 * will always return the original packet if we need to
1266 * process it locally.
1268 if (ifp->if_bridge) {
1269 KASSERT(bridge_input_p != NULL,
1270 ("%s: if_bridge not loaded!", __func__));
1272 if(m->m_flags & M_ETHER_BRIDGED) {
1273 m->m_flags &= ~M_ETHER_BRIDGED;
1275 m = bridge_input_p(ifp, m);
1279 KASSERT(ifp == m->m_pkthdr.rcvif,
1280 ("bridge_input_p changed rcvif\n"));
1284 /* Handle ng_ether(4) processing, if any */
1285 if (ng_ether_input_p != NULL) {
1287 * Hold BGL and recheck ng_ether_input_p
1290 if (ng_ether_input_p != NULL)
1291 ng_ether_input_p(ifp, &m);
1298 /* Continue with upper layer processing */
1299 ether_demux_oncpu(ifp, m);
1303 * Perform certain functions of ether_input_chain():
1305 * - Update statistics
1306 * - Run bpf(4) tap if requested
1307 * Then pass the packet to ether_input_oncpu().
1309 * This function should be used by pseudo interface (e.g. vlan(4)),
1310 * when it tries to claim that the packet is received by it.
1316 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1318 /* Discard packet if interface is not up */
1319 if (!(ifp->if_flags & IFF_UP)) {
1325 * Change receiving interface. The bridge will often pass a flag to
1326 * ask that this not be done so ARPs get applied to the correct
1329 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1330 m->m_pkthdr.rcvif == NULL) {
1331 m->m_pkthdr.rcvif = ifp;
1334 /* Update statistics */
1336 ifp->if_ibytes += m->m_pkthdr.len;
1337 if (m->m_flags & (M_MCAST | M_BCAST))
1340 if (reinput_flags & REINPUT_RUNBPF)
1343 ether_input_oncpu(ifp, m);
1346 static __inline boolean_t
1347 ether_vlancheck(struct mbuf **m0)
1349 struct mbuf *m = *m0;
1350 struct ether_header *eh;
1351 uint16_t ether_type;
1353 eh = mtod(m, struct ether_header *);
1354 ether_type = ntohs(eh->ether_type);
1356 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1358 * Extract vlan tag if hardware does not do it for us
1360 vlan_ether_decap(&m);
1364 eh = mtod(m, struct ether_header *);
1365 ether_type = ntohs(eh->ether_type);
1368 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1370 * To prevent possible dangerous recursion,
1371 * we don't do vlan-in-vlan
1373 m->m_pkthdr.rcvif->if_noproto++;
1376 KKASSERT(ether_type != ETHERTYPE_VLAN);
1378 m->m_flags |= M_ETHER_VLANCHECKED;
1389 ether_input_handler(netmsg_t nmsg)
1391 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1392 struct ether_header *eh;
1398 ifp = m->m_pkthdr.rcvif;
1400 eh = mtod(m, struct ether_header *);
1401 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1402 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1403 ifp->if_addrlen) == 0)
1404 m->m_flags |= M_BCAST;
1406 m->m_flags |= M_MCAST;
1410 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1411 if (!ether_vlancheck(&m)) {
1412 KKASSERT(m == NULL);
1417 ether_input_oncpu(ifp, m);
1421 * Send the packet to the target msgport or queue it into 'chain'.
1423 * At this point the packet had better be characterized (M_HASH set),
1424 * so we know which cpu to send it to.
1427 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1429 struct netmsg_packet *pmsg;
1431 KKASSERT(m->m_flags & M_HASH);
1432 pmsg = &m->m_hdr.mh_netmsg;
1433 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1434 0, ether_input_handler);
1435 pmsg->nm_packet = m;
1436 pmsg->base.lmsg.u.ms_result = isr;
1438 if (chain != NULL) {
1439 int cpuid = m->m_pkthdr.hash;
1440 struct mbuf_chain *c;
1443 if (c->mc_head == NULL) {
1444 c->mc_head = c->mc_tail = m;
1446 c->mc_tail->m_nextpkt = m;
1449 m->m_nextpkt = NULL;
1451 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1456 * Process a received Ethernet packet.
1458 * The ethernet header is assumed to be in the mbuf so the caller
1459 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1460 * bytes in the first mbuf.
1462 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1463 * immediately. This situation happens when ether_input_chain is
1464 * accessed through ifnet.if_input.
1466 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1467 * bucket indexed by the target msgport's cpuid and the target msgport
1468 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1469 * must initialize 'chain' by calling ether_input_chain_init().
1470 * ether_input_dispatch must be called later to send ether frames
1471 * queued on 'chain' to their target msgport.
1474 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1475 struct mbuf_chain *chain)
1481 /* Discard packet if interface is not up */
1482 if (!(ifp->if_flags & IFF_UP)) {
1487 if (m->m_len < sizeof(struct ether_header)) {
1488 /* XXX error in the caller. */
1493 m->m_pkthdr.rcvif = ifp;
1495 logether(chain_beg, ifp);
1497 ETHER_BPF_MTAP(ifp, m);
1499 ifp->if_ibytes += m->m_pkthdr.len;
1501 if (ifp->if_flags & IFF_MONITOR) {
1502 struct ether_header *eh;
1504 eh = mtod(m, struct ether_header *);
1505 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1509 * Interface marked for monitoring; discard packet.
1513 logether(chain_end, ifp);
1518 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1519 * we can dispatch it immediately without further inspection.
1521 if (pi != NULL && (m->m_flags & M_HASH)) {
1523 ether_pktinfo_try++;
1525 netisr_hashcheck(pi->pi_netisr, m, pi);
1526 if (m->m_flags & M_HASH) {
1527 ether_dispatch(pi->pi_netisr, m, chain);
1529 ether_pktinfo_hit++;
1531 logether(chain_end, ifp);
1536 else if (ifp->if_capenable & IFCAP_RSS) {
1545 * Packet hash will be recalculated by software,
1546 * so clear the M_HASH flag set by the driver;
1547 * the hash value calculated by the hardware may
1548 * not be exactly what we want.
1550 m->m_flags &= ~M_HASH;
1552 if (!ether_vlancheck(&m)) {
1553 KKASSERT(m == NULL);
1554 logether(chain_end, ifp);
1558 isr = ether_characterize(&m);
1560 logether(chain_end, ifp);
1565 * Finally dispatch it
1567 ether_dispatch(isr, m, chain);
1569 logether(chain_end, ifp);
1573 ether_characterize(struct mbuf **m0)
1575 struct mbuf *m = *m0;
1576 struct ether_header *eh;
1577 uint16_t ether_type;
1580 eh = mtod(m, struct ether_header *);
1581 ether_type = ntohs(eh->ether_type);
1584 * Map ether type to netisr id.
1586 switch (ether_type) {
1598 case ETHERTYPE_IPV6:
1610 case ETHERTYPE_MPLS:
1611 case ETHERTYPE_MPLS_MCAST:
1612 m->m_flags |= M_MPLSLABELED;
1619 * NETISR_MAX is an invalid value; it is chosen to let
1620 * netisr_characterize() know that we have no clear
1621 * idea where this packet should go.
1628 * Ask the isr to characterize the packet since we couldn't.
1629 * This is an attempt to optimally get us onto the correct protocol
1632 netisr_characterize(isr, &m, sizeof(struct ether_header));
1639 ether_demux_handler(netmsg_t nmsg)
1641 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1647 ifp = m->m_pkthdr.rcvif;
1649 ether_demux_oncpu(ifp, m);
1653 ether_demux(struct mbuf *m)
1655 struct netmsg_packet *pmsg;
1658 isr = ether_characterize(&m);
1662 KKASSERT(m->m_flags & M_HASH);
1663 pmsg = &m->m_hdr.mh_netmsg;
1664 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1665 0, ether_demux_handler);
1666 pmsg->nm_packet = m;
1667 pmsg->base.lmsg.u.ms_result = isr;
1669 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1672 MODULE_VERSION(ether, 1);