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 $
35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.96 2008/11/22 04:00:53 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/msgport.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/if_llc.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
72 #include <net/ethernet.h>
73 #include <net/vlan/if_vlan_ether.h>
74 #include <net/netmsg2.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
85 #include <netinet6/nd6.h>
89 #include <netinet/ip_carp.h>
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 short *tp, int *hlen);
101 #include <netproto/atalk/at.h>
102 #include <netproto/atalk/at_var.h>
103 #include <netproto/atalk/at_extern.h>
105 #define llc_snap_org_code llc_un.type_snap.org_code
106 #define llc_snap_ether_type llc_un.type_snap.ether_type
108 extern u_char at_org_code[3];
109 extern u_char aarp_org_code[3];
110 #endif /* NETATALK */
113 #include <netproto/mpls/mpls.h>
116 /* netgraph node hooks for ng_ether(4) */
117 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
118 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
119 struct mbuf *m, const struct ether_header *eh);
120 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
121 void (*ng_ether_attach_p)(struct ifnet *ifp);
122 void (*ng_ether_detach_p)(struct ifnet *ifp);
124 void (*vlan_input_p)(struct mbuf *);
126 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
128 static void ether_restore_header(struct mbuf **, const struct ether_header *,
129 const struct ether_header *);
134 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
135 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
136 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
138 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
141 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
145 #define gotoerr(e) do { error = (e); goto bad; } while (0)
146 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
148 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
150 const struct ether_header *eh);
152 static int ether_ipfw;
153 static u_int ether_restore_hdr;
154 static u_int ether_prepend_hdr;
157 static u_int ether_pktinfo_try;
158 static u_int ether_pktinfo_hit;
159 static u_int ether_rss_nopi;
160 static u_int ether_rss_nohash;
163 SYSCTL_DECL(_net_link);
164 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
165 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
166 ðer_ipfw, 0, "Pass ether pkts through firewall");
167 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
168 ðer_restore_hdr, 0, "# of ether header restoration");
169 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
170 ðer_prepend_hdr, 0,
171 "# of ether header restoration which prepends mbuf");
173 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
174 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
175 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
176 ðer_rss_nohash, 0, "# of packets do not have hash");
177 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
178 ðer_pktinfo_try, 0,
179 "# of tries to find packets' msgport using pktinfo");
180 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
181 ðer_pktinfo_hit, 0,
182 "# of packets whose msgport are found using pktinfo");
185 #define ETHER_KTR_STR "ifp=%p"
186 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
188 #define KTR_ETHERNET KTR_ALL
190 KTR_INFO_MASTER(ether);
191 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
192 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
193 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
194 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
195 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
198 * Ethernet output routine.
199 * Encapsulate a packet of type family for the local net.
200 * Use trailer local net encapsulation if enough data in first
201 * packet leaves a multiple of 512 bytes of data in remainder.
202 * Assumes that ifp is actually pointer to arpcom structure.
205 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
208 struct ether_header *eh, *deh;
211 int hlen = ETHER_HDR_LEN; /* link layer header length */
212 struct arpcom *ac = IFP2AC(ifp);
215 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
217 if (ifp->if_flags & IFF_MONITOR)
219 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
222 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
225 eh = mtod(m, struct ether_header *);
226 edst = eh->ether_dhost;
229 * Fill in the destination ethernet address and frame type.
231 switch (dst->sa_family) {
234 if (!arpresolve(ifp, rt, m, dst, edst))
235 return (0); /* if not yet resolved */
237 if (m->m_flags & M_MPLSLABELED)
238 eh->ether_type = htons(ETHERTYPE_MPLS);
241 eh->ether_type = htons(ETHERTYPE_IP);
246 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
247 return (0); /* Something bad happenned. */
248 eh->ether_type = htons(ETHERTYPE_IPV6);
253 if (ef_outputp != NULL) {
255 * Hold BGL and recheck ef_outputp
258 if (ef_outputp != NULL) {
259 error = ef_outputp(ifp, &m, dst,
260 &eh->ether_type, &hlen);
269 eh->ether_type = htons(ETHERTYPE_IPX);
270 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
271 edst, ETHER_ADDR_LEN);
276 struct at_ifaddr *aa;
283 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
289 * In the phase 2 case, need to prepend an mbuf for
290 * the llc header. Since we must preserve the value
291 * of m, which is passed to us by value, we m_copy()
292 * the first mbuf, and use it for our llc header.
294 if (aa->aa_flags & AFA_PHASE2) {
297 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
298 eh = mtod(m, struct ether_header *);
299 edst = eh->ether_dhost;
300 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
301 llc.llc_control = LLC_UI;
302 bcopy(at_org_code, llc.llc_snap_org_code,
304 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
306 mtod(m, caddr_t) + sizeof(struct ether_header),
308 eh->ether_type = htons(m->m_pkthdr.len);
309 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
311 eh->ether_type = htons(ETHERTYPE_AT);
313 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
322 case pseudo_AF_HDRCMPLT:
324 loop_copy = -1; /* if this is for us, don't do it */
325 deh = (struct ether_header *)dst->sa_data;
326 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
327 eh->ether_type = deh->ether_type;
331 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
332 gotoerr(EAFNOSUPPORT);
335 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
336 memcpy(eh->ether_shost,
337 ((struct ether_header *)dst->sa_data)->ether_shost,
340 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
343 * Bridges require special output handling.
345 if (ifp->if_bridge) {
346 KASSERT(bridge_output_p != NULL,
347 ("%s: if_bridge not loaded!", __func__));
348 return bridge_output_p(ifp, m);
352 * If a simplex interface, and the packet is being sent to our
353 * Ethernet address or a broadcast address, loopback a copy.
354 * XXX To make a simplex device behave exactly like a duplex
355 * device, we should copy in the case of sending to our own
356 * ethernet address (thus letting the original actually appear
357 * on the wire). However, we don't do that here for security
358 * reasons and compatibility with the original behavior.
360 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
363 if (m->m_pkthdr.csum_flags & CSUM_IP)
364 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
365 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
366 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
367 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
370 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
371 n->m_pkthdr.csum_flags |= csum_flags;
372 if (csum_flags & CSUM_DATA_VALID)
373 n->m_pkthdr.csum_data = 0xffff;
374 if_simloop(ifp, n, dst->sa_family, hlen);
377 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
378 ETHER_ADDR_LEN) == 0) {
379 m->m_pkthdr.csum_flags |= csum_flags;
380 if (csum_flags & CSUM_DATA_VALID)
381 m->m_pkthdr.csum_data = 0xffff;
382 if_simloop(ifp, m, dst->sa_family, hlen);
383 return (0); /* XXX */
390 * Hold BGL and recheck ifp->if_carp
393 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
402 /* Handle ng_ether(4) processing, if any */
403 if (ng_ether_output_p != NULL) {
405 * Hold BGL and recheck ng_ether_output_p
408 if (ng_ether_output_p != NULL) {
409 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
421 /* Continue with link-layer output */
422 return ether_output_frame(ifp, m);
430 * Ethernet link layer output routine to send a raw frame to the device.
432 * This assumes that the 14 byte Ethernet header is present and contiguous
436 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
438 struct ip_fw *rule = NULL;
440 struct altq_pktattr pktattr;
442 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
444 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
447 /* Extract info from dummynet tag */
448 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
449 KKASSERT(mtag != NULL);
450 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
451 KKASSERT(rule != NULL);
453 m_tag_delete(m, mtag);
454 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
457 if (ifq_is_enabled(&ifp->if_snd))
458 altq_etherclassify(&ifp->if_snd, m, &pktattr);
460 if (IPFW_LOADED && ether_ipfw != 0) {
461 struct ether_header save_eh, *eh;
463 eh = mtod(m, struct ether_header *);
465 m_adj(m, ETHER_HDR_LEN);
466 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
470 return ENOBUFS; /* pkt dropped */
472 return 0; /* consumed e.g. in a pipe */
475 /* packet was ok, restore the ethernet header */
476 ether_restore_header(&m, eh, &save_eh);
485 * Queue message on interface, update output statistics if
486 * successful, and start output if interface not yet active.
488 error = ifq_dispatch(ifp, m, &pktattr);
493 * ipfw processing for ethernet packets (in and out).
494 * The second parameter is NULL from ether_demux(), and ifp from
495 * ether_output_frame().
498 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
499 const struct ether_header *eh)
501 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
502 struct ip_fw_args args;
507 if (*rule != NULL && fw_one_pass)
508 return TRUE; /* dummynet packet, already partially processed */
511 * I need some amount of data to be contiguous.
513 i = min((*m0)->m_pkthdr.len, max_protohdr);
514 if ((*m0)->m_len < i) {
515 *m0 = m_pullup(*m0, i);
523 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
524 m_tag_delete(*m0, mtag);
525 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
526 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
527 KKASSERT(mtag != NULL);
528 m_tag_delete(*m0, mtag);
529 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
532 args.m = *m0; /* the packet we are looking at */
533 args.oif = dst; /* destination, if any */
534 args.rule = *rule; /* matching rule to restart */
535 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
536 i = ip_fw_chk_ptr(&args);
551 * XXX at some point add support for divert/forward actions.
552 * If none of the above matches, we have to drop the pkt.
558 * Pass the pkt to dummynet, which consumes it.
560 m = *m0; /* pass the original to dummynet */
561 *m0 = NULL; /* and nothing back to the caller */
563 ether_restore_header(&m, eh, &save_eh);
567 ip_fw_dn_io_ptr(m, args.cookie,
568 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
573 panic("unknown ipfw return value: %d\n", i);
578 ether_input(struct ifnet *ifp, struct mbuf *m)
580 ether_input_chain(ifp, m, NULL, NULL);
584 * Perform common duties while attaching to interface list
587 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
589 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
594 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
595 lwkt_serialize_t serializer)
597 struct sockaddr_dl *sdl;
599 ifp->if_type = IFT_ETHER;
600 ifp->if_addrlen = ETHER_ADDR_LEN;
601 ifp->if_hdrlen = ETHER_HDR_LEN;
602 if_attach(ifp, serializer);
603 ifp->if_mtu = ETHERMTU;
604 if (ifp->if_baudrate == 0)
605 ifp->if_baudrate = 10000000;
606 ifp->if_output = ether_output;
607 ifp->if_input = ether_input;
608 ifp->if_resolvemulti = ether_resolvemulti;
609 ifp->if_broadcastaddr = etherbroadcastaddr;
610 sdl = IF_LLSOCKADDR(ifp);
611 sdl->sdl_type = IFT_ETHER;
612 sdl->sdl_alen = ifp->if_addrlen;
613 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
615 * XXX Keep the current drivers happy.
616 * XXX Remove once all drivers have been cleaned up
618 if (lla != IFP2AC(ifp)->ac_enaddr)
619 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
620 bpfattach(ifp, dlt, hdrlen);
621 if (ng_ether_attach_p != NULL)
622 (*ng_ether_attach_p)(ifp);
624 if_printf(ifp, "MAC address: %6D\n", lla, ":");
628 * Perform common duties while detaching an Ethernet interface
631 ether_ifdetach(struct ifnet *ifp)
635 if (ng_ether_detach_p != NULL)
636 (*ng_ether_detach_p)(ifp);
642 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
644 struct ifaddr *ifa = (struct ifaddr *) data;
645 struct ifreq *ifr = (struct ifreq *) data;
648 #define IF_INIT(ifp) \
650 if (((ifp)->if_flags & IFF_UP) == 0) { \
651 (ifp)->if_flags |= IFF_UP; \
652 (ifp)->if_init((ifp)->if_softc); \
656 ASSERT_IFNET_SERIALIZED_ALL(ifp);
660 switch (ifa->ifa_addr->sa_family) {
663 IF_INIT(ifp); /* before arpwhohas */
664 arp_ifinit(ifp, ifa);
669 * XXX - This code is probably wrong
673 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
674 struct arpcom *ac = IFP2AC(ifp);
676 if (ipx_nullhost(*ina))
677 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
679 bcopy(ina->x_host.c_host, ac->ac_enaddr,
680 sizeof ac->ac_enaddr);
682 IF_INIT(ifp); /* Set new address. */
693 bcopy(IFP2AC(ifp)->ac_enaddr,
694 ((struct sockaddr *)ifr->ifr_data)->sa_data,
700 * Set the interface MTU.
702 if (ifr->ifr_mtu > ETHERMTU) {
705 ifp->if_mtu = ifr->ifr_mtu;
720 struct sockaddr **llsa,
723 struct sockaddr_dl *sdl;
724 struct sockaddr_in *sin;
726 struct sockaddr_in6 *sin6;
730 switch(sa->sa_family) {
733 * No mapping needed. Just check that it's a valid MC address.
735 sdl = (struct sockaddr_dl *)sa;
736 e_addr = LLADDR(sdl);
737 if ((e_addr[0] & 1) != 1)
738 return EADDRNOTAVAIL;
744 sin = (struct sockaddr_in *)sa;
745 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
746 return EADDRNOTAVAIL;
747 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
749 sdl->sdl_len = sizeof *sdl;
750 sdl->sdl_family = AF_LINK;
751 sdl->sdl_index = ifp->if_index;
752 sdl->sdl_type = IFT_ETHER;
753 sdl->sdl_alen = ETHER_ADDR_LEN;
754 e_addr = LLADDR(sdl);
755 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
756 *llsa = (struct sockaddr *)sdl;
761 sin6 = (struct sockaddr_in6 *)sa;
762 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
764 * An IP6 address of 0 means listen to all
765 * of the Ethernet multicast address used for IP6.
766 * (This is used for multicast routers.)
768 ifp->if_flags |= IFF_ALLMULTI;
772 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
773 return EADDRNOTAVAIL;
774 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
776 sdl->sdl_len = sizeof *sdl;
777 sdl->sdl_family = AF_LINK;
778 sdl->sdl_index = ifp->if_index;
779 sdl->sdl_type = IFT_ETHER;
780 sdl->sdl_alen = ETHER_ADDR_LEN;
781 e_addr = LLADDR(sdl);
782 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
783 *llsa = (struct sockaddr *)sdl;
789 * Well, the text isn't quite right, but it's the name
798 * This is for reference. We have a table-driven version
799 * of the little-endian crc32 generator, which is faster
800 * than the double-loop.
803 ether_crc32_le(const uint8_t *buf, size_t len)
805 uint32_t c, crc, carry;
808 crc = 0xffffffffU; /* initial value */
810 for (i = 0; i < len; i++) {
812 for (j = 0; j < 8; j++) {
813 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
817 crc = (crc ^ ETHER_CRC_POLY_LE);
825 ether_crc32_le(const uint8_t *buf, size_t len)
827 static const uint32_t crctab[] = {
828 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
829 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
830 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
831 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
836 crc = 0xffffffffU; /* initial value */
838 for (i = 0; i < len; i++) {
840 crc = (crc >> 4) ^ crctab[crc & 0xf];
841 crc = (crc >> 4) ^ crctab[crc & 0xf];
849 ether_crc32_be(const uint8_t *buf, size_t len)
851 uint32_t c, crc, carry;
854 crc = 0xffffffffU; /* initial value */
856 for (i = 0; i < len; i++) {
858 for (j = 0; j < 8; j++) {
859 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
863 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
871 * find the size of ethernet header, and call classifier
874 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
875 struct altq_pktattr *pktattr)
877 struct ether_header *eh;
879 int hlen, af, hdrsize;
882 hlen = sizeof(struct ether_header);
883 eh = mtod(m, struct ether_header *);
885 ether_type = ntohs(eh->ether_type);
886 if (ether_type < ETHERMTU) {
888 struct llc *llc = (struct llc *)(eh + 1);
891 if (m->m_len < hlen ||
892 llc->llc_dsap != LLC_SNAP_LSAP ||
893 llc->llc_ssap != LLC_SNAP_LSAP ||
894 llc->llc_control != LLC_UI)
895 goto bad; /* not snap! */
897 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
900 if (ether_type == ETHERTYPE_IP) {
902 hdrsize = 20; /* sizeof(struct ip) */
904 } else if (ether_type == ETHERTYPE_IPV6) {
906 hdrsize = 40; /* sizeof(struct ip6_hdr) */
911 while (m->m_len <= hlen) {
915 hdr = m->m_data + hlen;
916 if (m->m_len < hlen + hdrsize) {
918 * ip header is not in a single mbuf. this should not
919 * happen in the current code.
920 * (todo: use m_pulldown in the future)
926 ifq_classify(ifq, m, af, pktattr);
933 pktattr->pattr_class = NULL;
934 pktattr->pattr_hdr = NULL;
935 pktattr->pattr_af = AF_UNSPEC;
939 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
940 const struct ether_header *save_eh)
942 struct mbuf *m = *m0;
947 * Prepend the header, optimize for the common case of
948 * eh pointing into the mbuf.
950 if ((const void *)(eh + 1) == (void *)m->m_data) {
951 m->m_data -= ETHER_HDR_LEN;
952 m->m_len += ETHER_HDR_LEN;
953 m->m_pkthdr.len += ETHER_HDR_LEN;
957 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
959 bcopy(save_eh, mtod(m, struct ether_header *),
967 ether_input_ipifunc(void *arg)
969 struct mbuf *m, *next;
970 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
976 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
982 ether_input_dispatch(struct mbuf_chain *chain)
987 logether(disp_beg, NULL);
988 for (i = 0; i < ncpus; ++i) {
989 if (chain[i].mc_head != NULL) {
990 lwkt_send_ipiq(globaldata_find(i),
991 ether_input_ipifunc, chain[i].mc_head);
995 logether(disp_beg, NULL);
996 if (chain->mc_head != NULL)
997 ether_input_ipifunc(chain->mc_head);
999 logether(disp_end, NULL);
1003 ether_input_chain_init(struct mbuf_chain *chain)
1008 for (i = 0; i < ncpus; ++i)
1009 chain[i].mc_head = chain[i].mc_tail = NULL;
1011 chain->mc_head = chain->mc_tail = NULL;
1016 * Upper layer processing for a received Ethernet packet.
1019 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1021 struct ether_header *eh;
1022 int isr, discard = 0;
1024 struct ip_fw *rule = NULL;
1030 KASSERT(m->m_len >= ETHER_HDR_LEN,
1031 ("ether header is no contiguous!\n"));
1033 eh = mtod(m, struct ether_header *);
1035 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1038 /* Extract info from dummynet tag */
1039 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1040 KKASSERT(mtag != NULL);
1041 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1042 KKASSERT(rule != NULL);
1044 m_tag_delete(m, mtag);
1045 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1047 /* packet is passing the second time */
1053 * XXX: Okay, we need to call carp_forus() and - if it is for
1054 * us jump over code that does the normal check
1055 * "ac_enaddr == ether_dhost". The check sequence is a bit
1056 * different from OpenBSD, so we jump over as few code as
1057 * possible, to catch _all_ sanity checks. This needs
1058 * evaluation, to see if the carp ether_dhost values break any
1063 * Hold BGL and recheck ifp->if_carp
1066 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1075 * We got a packet which was unicast to a different Ethernet
1076 * address. If the driver is working properly, then this
1077 * situation can only happen when the interface is in
1078 * promiscuous mode. We defer the packet discarding until the
1079 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1082 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1083 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1084 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN))
1088 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1089 struct ether_header save_eh = *eh;
1091 /* XXX old crufty stuff, needs to be removed */
1092 m_adj(m, sizeof(struct ether_header));
1094 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1099 ether_restore_header(&m, eh, &save_eh);
1102 eh = mtod(m, struct ether_header *);
1105 ether_type = ntohs(eh->ether_type);
1106 KKASSERT(ether_type != ETHERTYPE_VLAN);
1108 if (m->m_flags & M_VLANTAG) {
1109 void (*vlan_input_func)(struct mbuf *);
1111 vlan_input_func = vlan_input_p;
1112 if (vlan_input_func != NULL) {
1115 m->m_pkthdr.rcvif->if_noproto++;
1122 * If we have been asked to discard this packet
1123 * (e.g. not for us), drop it before entering
1132 * Clear protocol specific flags,
1133 * before entering the upper layer.
1135 m->m_flags &= ~M_ETHER_FLAGS;
1137 /* Strip ethernet header. */
1138 m_adj(m, sizeof(struct ether_header));
1140 switch (ether_type) {
1143 if ((m->m_flags & M_LENCHECKED) == 0) {
1144 if (!ip_lengthcheck(&m, 0))
1147 if (ipflow_fastforward(m))
1153 if (ifp->if_flags & IFF_NOARP) {
1154 /* Discard packet if ARP is disabled on interface */
1163 case ETHERTYPE_IPV6:
1172 * Hold BGL and recheck ef_inputp
1175 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1187 isr = NETISR_ATALK1;
1189 case ETHERTYPE_AARP:
1195 case ETHERTYPE_MPLS:
1196 case ETHERTYPE_MPLS_MCAST:
1197 /* Should have been set by ether_input_chain(). */
1198 KKASSERT(m->m_flags & M_MPLSLABELED);
1205 * The accurate msgport is not determined before
1206 * we reach here, so redo the dispatching
1211 * Hold BGL and recheck ef_inputp
1214 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1222 if (ether_type > ETHERMTU)
1224 l = mtod(m, struct llc *);
1225 if (l->llc_dsap == LLC_SNAP_LSAP &&
1226 l->llc_ssap == LLC_SNAP_LSAP &&
1227 l->llc_control == LLC_UI) {
1228 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1229 sizeof at_org_code) == 0 &&
1230 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1231 m_adj(m, sizeof(struct llc));
1232 isr = NETISR_ATALK2;
1235 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1236 sizeof aarp_org_code) == 0 &&
1237 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1238 m_adj(m, sizeof(struct llc));
1245 if (ng_ether_input_orphan_p != NULL) {
1247 * Hold BGL and recheck ng_ether_input_orphan_p
1250 if (ng_ether_input_orphan_p != NULL) {
1251 ng_ether_input_orphan_p(ifp, m, eh);
1261 netisr_queue(isr, m);
1265 * First we perform any link layer operations, then continue to the
1266 * upper layers with ether_demux_oncpu().
1269 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1271 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1273 * Receiving interface's flags are changed, when this
1274 * packet is waiting for processing; discard it.
1281 * Tap the packet off here for a bridge. bridge_input()
1282 * will return NULL if it has consumed the packet, otherwise
1283 * it gets processed as normal. Note that bridge_input()
1284 * will always return the original packet if we need to
1285 * process it locally.
1287 if (ifp->if_bridge) {
1288 KASSERT(bridge_input_p != NULL,
1289 ("%s: if_bridge not loaded!", __func__));
1291 if(m->m_flags & M_ETHER_BRIDGED) {
1292 m->m_flags &= ~M_ETHER_BRIDGED;
1294 m = bridge_input_p(ifp, m);
1298 KASSERT(ifp == m->m_pkthdr.rcvif,
1299 ("bridge_input_p changed rcvif\n"));
1303 /* Handle ng_ether(4) processing, if any */
1304 if (ng_ether_input_p != NULL) {
1306 * Hold BGL and recheck ng_ether_input_p
1309 if (ng_ether_input_p != NULL)
1310 ng_ether_input_p(ifp, &m);
1317 /* Continue with upper layer processing */
1318 ether_demux_oncpu(ifp, m);
1322 * Perform certain functions of ether_input_chain():
1324 * - Update statistics
1325 * - Run bpf(4) tap if requested
1326 * Then pass the packet to ether_input_oncpu().
1328 * This function should be used by pseudo interface (e.g. vlan(4)),
1329 * when it tries to claim that the packet is received by it.
1332 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int run_bpf)
1334 /* Discard packet if interface is not up */
1335 if (!(ifp->if_flags & IFF_UP)) {
1340 /* Change receiving interface */
1341 m->m_pkthdr.rcvif = ifp;
1343 /* Update statistics */
1345 ifp->if_ibytes += m->m_pkthdr.len;
1346 if (m->m_flags & (M_MCAST | M_BCAST))
1352 ether_input_oncpu(ifp, m);
1355 static __inline boolean_t
1356 ether_vlancheck(struct mbuf **m0)
1358 struct mbuf *m = *m0;
1359 struct ether_header *eh;
1360 uint16_t ether_type;
1362 eh = mtod(m, struct ether_header *);
1363 ether_type = ntohs(eh->ether_type);
1365 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1367 * Extract vlan tag if hardware does not do it for us
1369 vlan_ether_decap(&m);
1373 eh = mtod(m, struct ether_header *);
1374 ether_type = ntohs(eh->ether_type);
1377 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1379 * To prevent possible dangerous recursion,
1380 * we don't do vlan-in-vlan
1382 m->m_pkthdr.rcvif->if_noproto++;
1385 KKASSERT(ether_type != ETHERTYPE_VLAN);
1387 m->m_flags |= M_ETHER_VLANCHECKED;
1398 ether_input_handler(netmsg_t nmsg)
1400 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1401 struct ether_header *eh;
1407 ifp = m->m_pkthdr.rcvif;
1409 eh = mtod(m, struct ether_header *);
1410 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1411 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1412 ifp->if_addrlen) == 0)
1413 m->m_flags |= M_BCAST;
1415 m->m_flags |= M_MCAST;
1419 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1420 if (!ether_vlancheck(&m)) {
1421 KKASSERT(m == NULL);
1426 ether_input_oncpu(ifp, m);
1430 * Send the packet to the target msgport or queue it into 'chain'.
1432 * At this point the packet had better be characterized (M_HASH set),
1433 * so we know which cpu to send it to.
1436 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1438 struct netmsg_packet *pmsg;
1440 KKASSERT(m->m_flags & M_HASH);
1441 pmsg = &m->m_hdr.mh_netmsg;
1442 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1443 0, ether_input_handler);
1444 pmsg->nm_packet = m;
1445 pmsg->base.lmsg.u.ms_result = isr;
1447 if (chain != NULL) {
1448 int cpuid = m->m_pkthdr.hash;
1449 struct mbuf_chain *c;
1452 if (c->mc_head == NULL) {
1453 c->mc_head = c->mc_tail = m;
1455 c->mc_tail->m_nextpkt = m;
1458 m->m_nextpkt = NULL;
1460 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1465 * Process a received Ethernet packet.
1467 * The ethernet header is assumed to be in the mbuf so the caller
1468 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1469 * bytes in the first mbuf.
1471 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1472 * immediately. This situation happens when ether_input_chain is
1473 * accessed through ifnet.if_input.
1475 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1476 * bucket indexed by the target msgport's cpuid and the target msgport
1477 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1478 * must initialize 'chain' by calling ether_input_chain_init().
1479 * ether_input_dispatch must be called later to send ether frames
1480 * queued on 'chain' to their target msgport.
1483 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1484 struct mbuf_chain *chain)
1486 struct ether_header *eh;
1487 uint16_t ether_type;
1492 /* Discard packet if interface is not up */
1493 if (!(ifp->if_flags & IFF_UP)) {
1498 if (m->m_len < sizeof(struct ether_header)) {
1499 /* XXX error in the caller. */
1504 m->m_pkthdr.rcvif = ifp;
1506 logether(chain_beg, ifp);
1508 ETHER_BPF_MTAP(ifp, m);
1510 ifp->if_ibytes += m->m_pkthdr.len;
1512 if (ifp->if_flags & IFF_MONITOR) {
1513 eh = mtod(m, struct ether_header *);
1514 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1518 * Interface marked for monitoring; discard packet.
1522 logether(chain_end, ifp);
1527 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1528 * we can dispatch it immediately without further inspection.
1530 if (pi != NULL && (m->m_flags & M_HASH)) {
1532 ether_pktinfo_try++;
1534 ether_dispatch(pi->pi_netisr, m, chain);
1537 ether_pktinfo_hit++;
1539 logether(chain_end, ifp);
1543 else if (ifp->if_capenable & IFCAP_RSS) {
1552 * Packet hash will be recalculated by software,
1553 * so clear the M_HASH flag set by the driver;
1554 * the hash value calculated by the hardware may
1555 * not be exactly what we want.
1557 m->m_flags &= ~M_HASH;
1559 if (!ether_vlancheck(&m)) {
1560 KKASSERT(m == NULL);
1561 logether(chain_end, ifp);
1564 eh = mtod(m, struct ether_header *);
1565 ether_type = ntohs(eh->ether_type);
1568 * Map ether type to netisr id.
1570 switch (ether_type) {
1582 case ETHERTYPE_IPV6:
1595 isr = NETISR_ATALK1;
1597 case ETHERTYPE_AARP:
1603 case ETHERTYPE_MPLS:
1604 case ETHERTYPE_MPLS_MCAST:
1605 m->m_flags |= M_MPLSLABELED;
1612 * NETISR_MAX is an invalid value; it is chosen to let
1619 * Ask the isr to characterize the packet since we couldn't.
1620 * This is an attempt to optimally get us onto the correct protocol
1623 netisr_characterize(isr, &m, sizeof(struct ether_header));
1625 logether(chain_end, ifp);
1630 * Finally dispatch it
1632 ether_dispatch(isr, m, chain);
1634 logether(chain_end, ifp);
1637 MODULE_VERSION(ether, 1);