2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
38 #include "opt_inet6.h"
41 #include "opt_netgraph.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/globaldata.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/vlan/if_vlan_var.h>
73 #include <net/netmsg2.h>
75 #if defined(INET) || defined(INET6)
76 #include <netinet/in.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
85 #include <netinet6/nd6.h>
89 #include <netinet/ip_carp.h>
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 short *tp, int *hlen);
101 #include <netproto/mpls/mpls.h>
104 /* netgraph node hooks for ng_ether(4) */
105 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
106 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
107 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
108 void (*ng_ether_attach_p)(struct ifnet *ifp);
109 void (*ng_ether_detach_p)(struct ifnet *ifp);
111 void (*vlan_input_p)(struct mbuf *);
113 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115 static void ether_restore_header(struct mbuf **, const struct ether_header *,
116 const struct ether_header *);
117 static int ether_characterize(struct mbuf **);
118 static void ether_dispatch(int, struct mbuf *);
123 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
124 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
125 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
126 struct ifnet *(*bridge_interface_p)(void *if_bridge);
128 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
131 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
135 #define gotoerr(e) do { error = (e); goto bad; } while (0)
136 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
138 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
140 const struct ether_header *eh);
142 static int ether_ipfw;
143 static u_long ether_restore_hdr;
144 static u_long ether_prepend_hdr;
145 static u_long ether_input_wronghash;
146 static int ether_debug;
149 static u_long ether_pktinfo_try;
150 static u_long ether_pktinfo_hit;
151 static u_long ether_rss_nopi;
152 static u_long ether_rss_nohash;
153 static u_long ether_input_requeue;
154 static u_long ether_input_wronghwhash;
155 static int ether_input_ckhash;
158 SYSCTL_DECL(_net_link);
159 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
160 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
161 ðer_debug, 0, "Ether debug");
162 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
163 ðer_ipfw, 0, "Pass ether pkts through firewall");
164 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
165 ðer_restore_hdr, 0, "# of ether header restoration");
166 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
167 ðer_prepend_hdr, 0,
168 "# of ether header restoration which prepends mbuf");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
170 ðer_input_wronghash, 0, "# of input packets with wrong hash");
172 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
173 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
175 ðer_rss_nohash, 0, "# of packets do not have hash");
176 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
177 ðer_pktinfo_try, 0,
178 "# of tries to find packets' msgport using pktinfo");
179 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
180 ðer_pktinfo_hit, 0,
181 "# of packets whose msgport are found using pktinfo");
182 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
183 ðer_input_requeue, 0, "# of input packets gets requeued");
184 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
185 ðer_input_wronghwhash, 0, "# of input packets with wrong hw hash");
186 SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
187 ðer_input_ckhash, 0, "always check hash");
190 #define ETHER_KTR_STR "ifp=%p"
191 #define ETHER_KTR_ARGS struct ifnet *ifp
193 #define KTR_ETHERNET KTR_ALL
195 KTR_INFO_MASTER(ether);
196 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
197 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
198 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
199 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
200 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
203 * Ethernet output routine.
204 * Encapsulate a packet of type family for the local net.
205 * Use trailer local net encapsulation if enough data in first
206 * packet leaves a multiple of 512 bytes of data in remainder.
207 * Assumes that ifp is actually pointer to arpcom structure.
210 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
213 struct ether_header *eh, *deh;
216 int hlen = ETHER_HDR_LEN; /* link layer header length */
217 struct arpcom *ac = IFP2AC(ifp);
220 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
222 if (ifp->if_flags & IFF_MONITOR)
224 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
227 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
230 m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
231 eh = mtod(m, struct ether_header *);
232 edst = eh->ether_dhost;
235 * Fill in the destination ethernet address and frame type.
237 switch (dst->sa_family) {
240 if (!arpresolve(ifp, rt, m, dst, edst))
241 return (0); /* if not yet resolved */
243 if (m->m_flags & M_MPLSLABELED)
244 eh->ether_type = htons(ETHERTYPE_MPLS);
247 eh->ether_type = htons(ETHERTYPE_IP);
252 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
253 return (0); /* Something bad happenned. */
254 eh->ether_type = htons(ETHERTYPE_IPV6);
259 if (ef_outputp != NULL) {
261 * Hold BGL and recheck ef_outputp
264 if (ef_outputp != NULL) {
265 error = ef_outputp(ifp, &m, dst,
266 &eh->ether_type, &hlen);
275 eh->ether_type = htons(ETHERTYPE_IPX);
276 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
277 edst, ETHER_ADDR_LEN);
280 case pseudo_AF_HDRCMPLT:
282 loop_copy = -1; /* if this is for us, don't do it */
283 deh = (struct ether_header *)dst->sa_data;
284 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
285 eh->ether_type = deh->ether_type;
289 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
290 gotoerr(EAFNOSUPPORT);
293 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
294 memcpy(eh->ether_shost,
295 ((struct ether_header *)dst->sa_data)->ether_shost,
298 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
301 * Bridges require special output handling.
303 if (ifp->if_bridge) {
304 KASSERT(bridge_output_p != NULL,
305 ("%s: if_bridge not loaded!", __func__));
306 return bridge_output_p(ifp, m);
310 * If a simplex interface, and the packet is being sent to our
311 * Ethernet address or a broadcast address, loopback a copy.
312 * XXX To make a simplex device behave exactly like a duplex
313 * device, we should copy in the case of sending to our own
314 * ethernet address (thus letting the original actually appear
315 * on the wire). However, we don't do that here for security
316 * reasons and compatibility with the original behavior.
318 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
321 if (m->m_pkthdr.csum_flags & CSUM_IP)
322 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
323 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
324 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
325 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
328 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
329 n->m_pkthdr.csum_flags |= csum_flags;
330 if (csum_flags & CSUM_DATA_VALID)
331 n->m_pkthdr.csum_data = 0xffff;
332 if_simloop(ifp, n, dst->sa_family, hlen);
335 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
336 ETHER_ADDR_LEN) == 0) {
337 m->m_pkthdr.csum_flags |= csum_flags;
338 if (csum_flags & CSUM_DATA_VALID)
339 m->m_pkthdr.csum_data = 0xffff;
340 if_simloop(ifp, m, dst->sa_family, hlen);
341 return (0); /* XXX */
346 if (ifp->if_type == IFT_CARP) {
347 ifp = carp_parent(ifp);
349 gotoerr(ENETUNREACH);
354 * Check precondition again
356 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
358 if (ifp->if_flags & IFF_MONITOR)
360 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
361 (IFF_UP | IFF_RUNNING))
366 /* Handle ng_ether(4) processing, if any */
367 if (ng_ether_output_p != NULL) {
369 * Hold BGL and recheck ng_ether_output_p
372 if (ng_ether_output_p != NULL) {
373 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
385 /* Continue with link-layer output */
386 return ether_output_frame(ifp, m);
394 * Returns the bridge interface an ifp is associated
397 * Only call if ifp->if_bridge != NULL.
400 ether_bridge_interface(struct ifnet *ifp)
402 if (bridge_interface_p)
403 return(bridge_interface_p(ifp->if_bridge));
408 * Ethernet link layer output routine to send a raw frame to the device.
410 * This assumes that the 14 byte Ethernet header is present and contiguous
414 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
416 struct ip_fw *rule = NULL;
418 struct altq_pktattr pktattr;
420 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
422 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
425 /* Extract info from dummynet tag */
426 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
427 KKASSERT(mtag != NULL);
428 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
429 KKASSERT(rule != NULL);
431 m_tag_delete(m, mtag);
432 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
435 if (ifq_is_enabled(&ifp->if_snd))
436 altq_etherclassify(&ifp->if_snd, m, &pktattr);
438 if (IPFW_LOADED && ether_ipfw != 0) {
439 struct ether_header save_eh, *eh;
441 eh = mtod(m, struct ether_header *);
443 m_adj(m, ETHER_HDR_LEN);
444 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
448 return ENOBUFS; /* pkt dropped */
450 return 0; /* consumed e.g. in a pipe */
453 /* packet was ok, restore the ethernet header */
454 ether_restore_header(&m, eh, &save_eh);
463 * Queue message on interface, update output statistics if
464 * successful, and start output if interface not yet active.
466 error = ifq_dispatch(ifp, m, &pktattr);
471 * ipfw processing for ethernet packets (in and out).
472 * The second parameter is NULL from ether_demux(), and ifp from
473 * ether_output_frame().
476 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
477 const struct ether_header *eh)
479 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
480 struct ip_fw_args args;
485 if (*rule != NULL && fw_one_pass)
486 return TRUE; /* dummynet packet, already partially processed */
489 * I need some amount of data to be contiguous.
491 i = min((*m0)->m_pkthdr.len, max_protohdr);
492 if ((*m0)->m_len < i) {
493 *m0 = m_pullup(*m0, i);
501 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
502 m_tag_delete(*m0, mtag);
503 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
504 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
505 KKASSERT(mtag != NULL);
506 m_tag_delete(*m0, mtag);
507 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
510 args.m = *m0; /* the packet we are looking at */
511 args.oif = dst; /* destination, if any */
512 args.rule = *rule; /* matching rule to restart */
513 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
514 i = ip_fw_chk_ptr(&args);
529 * XXX at some point add support for divert/forward actions.
530 * If none of the above matches, we have to drop the pkt.
536 * Pass the pkt to dummynet, which consumes it.
538 m = *m0; /* pass the original to dummynet */
539 *m0 = NULL; /* and nothing back to the caller */
541 ether_restore_header(&m, eh, &save_eh);
545 ip_fw_dn_io_ptr(m, args.cookie,
546 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
551 panic("unknown ipfw return value: %d", i);
556 ether_input(struct ifnet *ifp, struct mbuf *m)
558 ether_input_pkt(ifp, m, NULL);
562 * Perform common duties while attaching to interface list
565 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
567 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
572 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
573 lwkt_serialize_t serializer)
575 struct sockaddr_dl *sdl;
576 char ethstr[ETHER_ADDRSTRLEN + 1];
578 ifp->if_type = IFT_ETHER;
579 ifp->if_addrlen = ETHER_ADDR_LEN;
580 ifp->if_hdrlen = ETHER_HDR_LEN;
581 if_attach(ifp, serializer);
582 ifp->if_mtu = ETHERMTU;
583 if (ifp->if_baudrate == 0)
584 ifp->if_baudrate = 10000000;
585 ifp->if_output = ether_output;
586 ifp->if_input = ether_input;
587 ifp->if_resolvemulti = ether_resolvemulti;
588 ifp->if_broadcastaddr = etherbroadcastaddr;
589 sdl = IF_LLSOCKADDR(ifp);
590 sdl->sdl_type = IFT_ETHER;
591 sdl->sdl_alen = ifp->if_addrlen;
592 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
594 * XXX Keep the current drivers happy.
595 * XXX Remove once all drivers have been cleaned up
597 if (lla != IFP2AC(ifp)->ac_enaddr)
598 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
599 bpfattach(ifp, dlt, hdrlen);
600 if (ng_ether_attach_p != NULL)
601 (*ng_ether_attach_p)(ifp);
603 if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
607 * Perform common duties while detaching an Ethernet interface
610 ether_ifdetach(struct ifnet *ifp)
614 if (ng_ether_detach_p != NULL)
615 (*ng_ether_detach_p)(ifp);
621 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
623 struct ifaddr *ifa = (struct ifaddr *) data;
624 struct ifreq *ifr = (struct ifreq *) data;
627 #define IF_INIT(ifp) \
629 if (((ifp)->if_flags & IFF_UP) == 0) { \
630 (ifp)->if_flags |= IFF_UP; \
631 (ifp)->if_init((ifp)->if_softc); \
635 ASSERT_IFNET_SERIALIZED_ALL(ifp);
639 switch (ifa->ifa_addr->sa_family) {
642 IF_INIT(ifp); /* before arpwhohas */
643 arp_ifinit(ifp, ifa);
648 * XXX - This code is probably wrong
652 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
653 struct arpcom *ac = IFP2AC(ifp);
655 if (ipx_nullhost(*ina))
656 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
658 bcopy(ina->x_host.c_host, ac->ac_enaddr,
659 sizeof ac->ac_enaddr);
661 IF_INIT(ifp); /* Set new address. */
672 bcopy(IFP2AC(ifp)->ac_enaddr,
673 ((struct sockaddr *)ifr->ifr_data)->sa_data,
679 * Set the interface MTU.
681 if (ifr->ifr_mtu > ETHERMTU) {
684 ifp->if_mtu = ifr->ifr_mtu;
699 struct sockaddr **llsa,
702 struct sockaddr_dl *sdl;
704 struct sockaddr_in *sin;
707 struct sockaddr_in6 *sin6;
711 switch(sa->sa_family) {
714 * No mapping needed. Just check that it's a valid MC address.
716 sdl = (struct sockaddr_dl *)sa;
717 e_addr = LLADDR(sdl);
718 if ((e_addr[0] & 1) != 1)
719 return EADDRNOTAVAIL;
725 sin = (struct sockaddr_in *)sa;
726 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
727 return EADDRNOTAVAIL;
728 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
729 sdl->sdl_len = sizeof *sdl;
730 sdl->sdl_family = AF_LINK;
731 sdl->sdl_index = ifp->if_index;
732 sdl->sdl_type = IFT_ETHER;
733 sdl->sdl_alen = ETHER_ADDR_LEN;
734 e_addr = LLADDR(sdl);
735 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
736 *llsa = (struct sockaddr *)sdl;
741 sin6 = (struct sockaddr_in6 *)sa;
742 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
744 * An IP6 address of 0 means listen to all
745 * of the Ethernet multicast address used for IP6.
746 * (This is used for multicast routers.)
748 ifp->if_flags |= IFF_ALLMULTI;
752 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
753 return EADDRNOTAVAIL;
754 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
755 sdl->sdl_len = sizeof *sdl;
756 sdl->sdl_family = AF_LINK;
757 sdl->sdl_index = ifp->if_index;
758 sdl->sdl_type = IFT_ETHER;
759 sdl->sdl_alen = ETHER_ADDR_LEN;
760 e_addr = LLADDR(sdl);
761 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
762 *llsa = (struct sockaddr *)sdl;
768 * Well, the text isn't quite right, but it's the name
777 * This is for reference. We have a table-driven version
778 * of the little-endian crc32 generator, which is faster
779 * than the double-loop.
782 ether_crc32_le(const uint8_t *buf, size_t len)
784 uint32_t c, crc, carry;
787 crc = 0xffffffffU; /* initial value */
789 for (i = 0; i < len; i++) {
791 for (j = 0; j < 8; j++) {
792 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
796 crc = (crc ^ ETHER_CRC_POLY_LE);
804 ether_crc32_le(const uint8_t *buf, size_t len)
806 static const uint32_t crctab[] = {
807 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
808 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
809 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
810 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
815 crc = 0xffffffffU; /* initial value */
817 for (i = 0; i < len; i++) {
819 crc = (crc >> 4) ^ crctab[crc & 0xf];
820 crc = (crc >> 4) ^ crctab[crc & 0xf];
828 ether_crc32_be(const uint8_t *buf, size_t len)
830 uint32_t c, crc, carry;
833 crc = 0xffffffffU; /* initial value */
835 for (i = 0; i < len; i++) {
837 for (j = 0; j < 8; j++) {
838 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
842 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
850 * find the size of ethernet header, and call classifier
853 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
854 struct altq_pktattr *pktattr)
856 struct ether_header *eh;
858 int hlen, af, hdrsize;
860 hlen = sizeof(struct ether_header);
861 eh = mtod(m, struct ether_header *);
863 ether_type = ntohs(eh->ether_type);
864 if (ether_type < ETHERMTU) {
866 struct llc *llc = (struct llc *)(eh + 1);
869 if (m->m_len < hlen ||
870 llc->llc_dsap != LLC_SNAP_LSAP ||
871 llc->llc_ssap != LLC_SNAP_LSAP ||
872 llc->llc_control != LLC_UI)
873 goto bad; /* not snap! */
875 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
878 if (ether_type == ETHERTYPE_IP) {
880 hdrsize = 20; /* sizeof(struct ip) */
882 } else if (ether_type == ETHERTYPE_IPV6) {
884 hdrsize = 40; /* sizeof(struct ip6_hdr) */
889 while (m->m_len <= hlen) {
893 if (m->m_len < hlen + hdrsize) {
895 * ip header is not in a single mbuf. this should not
896 * happen in the current code.
897 * (todo: use m_pulldown in the future)
903 ifq_classify(ifq, m, af, pktattr);
910 pktattr->pattr_class = NULL;
911 pktattr->pattr_hdr = NULL;
912 pktattr->pattr_af = AF_UNSPEC;
916 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
917 const struct ether_header *save_eh)
919 struct mbuf *m = *m0;
924 * Prepend the header, optimize for the common case of
925 * eh pointing into the mbuf.
927 if ((const void *)(eh + 1) == (void *)m->m_data) {
928 m->m_data -= ETHER_HDR_LEN;
929 m->m_len += ETHER_HDR_LEN;
930 m->m_pkthdr.len += ETHER_HDR_LEN;
934 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
936 bcopy(save_eh, mtod(m, struct ether_header *),
944 * Upper layer processing for a received Ethernet packet.
947 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
949 struct ether_header *eh;
950 int isr, discard = 0;
952 struct ip_fw *rule = NULL;
955 KASSERT(m->m_len >= ETHER_HDR_LEN,
956 ("ether header is not contiguous!"));
958 eh = mtod(m, struct ether_header *);
960 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
963 /* Extract info from dummynet tag */
964 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
965 KKASSERT(mtag != NULL);
966 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
967 KKASSERT(rule != NULL);
969 m_tag_delete(m, mtag);
970 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
972 /* packet is passing the second time */
977 * We got a packet which was unicast to a different Ethernet
978 * address. If the driver is working properly, then this
979 * situation can only happen when the interface is in
980 * promiscuous mode. We defer the packet discarding until the
981 * vlan processing is done, so that vlan/bridge or vlan/netgraph
984 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
985 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
986 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
987 if (ether_debug & 1) {
988 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
989 "%02x:%02x:%02x:%02x:%02x:%02x "
990 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1004 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1005 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1006 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1007 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1008 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1009 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1012 if ((ether_debug & 2) == 0)
1017 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1018 struct ether_header save_eh = *eh;
1020 /* XXX old crufty stuff, needs to be removed */
1021 m_adj(m, sizeof(struct ether_header));
1023 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1028 ether_restore_header(&m, eh, &save_eh);
1031 eh = mtod(m, struct ether_header *);
1034 ether_type = ntohs(eh->ether_type);
1035 KKASSERT(ether_type != ETHERTYPE_VLAN);
1037 if (m->m_flags & M_VLANTAG) {
1038 void (*vlan_input_func)(struct mbuf *);
1040 vlan_input_func = vlan_input_p;
1041 if (vlan_input_func != NULL) {
1044 m->m_pkthdr.rcvif->if_noproto++;
1051 * If we have been asked to discard this packet
1052 * (e.g. not for us), drop it before entering
1061 * Clear protocol specific flags,
1062 * before entering the upper layer.
1064 m->m_flags &= ~M_ETHER_FLAGS;
1066 /* Strip ethernet header. */
1067 m_adj(m, sizeof(struct ether_header));
1069 switch (ether_type) {
1072 if ((m->m_flags & M_LENCHECKED) == 0) {
1073 if (!ip_lengthcheck(&m, 0))
1076 if (ipflow_fastforward(m))
1082 if (ifp->if_flags & IFF_NOARP) {
1083 /* Discard packet if ARP is disabled on interface */
1092 case ETHERTYPE_IPV6:
1101 * Hold BGL and recheck ef_inputp
1104 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1115 case ETHERTYPE_MPLS:
1116 case ETHERTYPE_MPLS_MCAST:
1117 /* Should have been set by ether_input_pkt(). */
1118 KKASSERT(m->m_flags & M_MPLSLABELED);
1125 * The accurate msgport is not determined before
1126 * we reach here, so recharacterize packet.
1128 m->m_flags &= ~M_HASH;
1132 * Hold BGL and recheck ef_inputp
1135 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1142 if (ng_ether_input_orphan_p != NULL) {
1144 * Put back the ethernet header so netgraph has a
1145 * consistent view of inbound packets.
1147 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1150 * M_PREPEND frees the mbuf in case of failure.
1155 * Hold BGL and recheck ng_ether_input_orphan_p
1158 if (ng_ether_input_orphan_p != NULL) {
1159 ng_ether_input_orphan_p(ifp, m);
1169 if (m->m_flags & M_HASH) {
1170 if (&curthread->td_msgport == netisr_portfn(m->m_pkthdr.hash)) {
1171 netisr_handle(isr, m);
1175 * XXX Something is wrong,
1176 * we probably should panic here!
1178 m->m_flags &= ~M_HASH;
1179 atomic_add_long(ðer_input_wronghash, 1);
1183 atomic_add_long(ðer_input_requeue, 1);
1185 netisr_queue(isr, m);
1189 * First we perform any link layer operations, then continue to the
1190 * upper layers with ether_demux_oncpu().
1193 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1199 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1201 * Receiving interface's flags are changed, when this
1202 * packet is waiting for processing; discard it.
1209 * Tap the packet off here for a bridge. bridge_input()
1210 * will return NULL if it has consumed the packet, otherwise
1211 * it gets processed as normal. Note that bridge_input()
1212 * will always return the original packet if we need to
1213 * process it locally.
1215 if (ifp->if_bridge) {
1216 KASSERT(bridge_input_p != NULL,
1217 ("%s: if_bridge not loaded!", __func__));
1219 if(m->m_flags & M_ETHER_BRIDGED) {
1220 m->m_flags &= ~M_ETHER_BRIDGED;
1222 m = bridge_input_p(ifp, m);
1226 KASSERT(ifp == m->m_pkthdr.rcvif,
1227 ("bridge_input_p changed rcvif"));
1232 carp = ifp->if_carp;
1234 m = carp_input(carp, m);
1237 KASSERT(ifp == m->m_pkthdr.rcvif,
1238 ("carp_input changed rcvif"));
1242 /* Handle ng_ether(4) processing, if any */
1243 if (ng_ether_input_p != NULL) {
1245 * Hold BGL and recheck ng_ether_input_p
1248 if (ng_ether_input_p != NULL)
1249 ng_ether_input_p(ifp, &m);
1256 /* Continue with upper layer processing */
1257 ether_demux_oncpu(ifp, m);
1261 * Perform certain functions of ether_input_pkt():
1263 * - Update statistics
1264 * - Run bpf(4) tap if requested
1265 * Then pass the packet to ether_input_oncpu().
1267 * This function should be used by pseudo interface (e.g. vlan(4)),
1268 * when it tries to claim that the packet is received by it.
1274 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1276 /* Discard packet if interface is not up */
1277 if (!(ifp->if_flags & IFF_UP)) {
1283 * Change receiving interface. The bridge will often pass a flag to
1284 * ask that this not be done so ARPs get applied to the correct
1287 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1288 m->m_pkthdr.rcvif == NULL) {
1289 m->m_pkthdr.rcvif = ifp;
1292 /* Update statistics */
1294 ifp->if_ibytes += m->m_pkthdr.len;
1295 if (m->m_flags & (M_MCAST | M_BCAST))
1298 if (reinput_flags & REINPUT_RUNBPF)
1301 ether_input_oncpu(ifp, m);
1304 static __inline boolean_t
1305 ether_vlancheck(struct mbuf **m0)
1307 struct mbuf *m = *m0;
1308 struct ether_header *eh;
1309 uint16_t ether_type;
1311 eh = mtod(m, struct ether_header *);
1312 ether_type = ntohs(eh->ether_type);
1314 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1316 * Extract vlan tag if hardware does not do it for us
1318 vlan_ether_decap(&m);
1322 eh = mtod(m, struct ether_header *);
1323 ether_type = ntohs(eh->ether_type);
1326 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1328 * To prevent possible dangerous recursion,
1329 * we don't do vlan-in-vlan
1331 m->m_pkthdr.rcvif->if_noproto++;
1334 KKASSERT(ether_type != ETHERTYPE_VLAN);
1336 m->m_flags |= M_ETHER_VLANCHECKED;
1347 ether_input_handler(netmsg_t nmsg)
1349 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1350 struct ether_header *eh;
1357 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1358 if (!ether_vlancheck(&m)) {
1359 KKASSERT(m == NULL);
1363 if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH)
1365 || ether_input_ckhash
1371 * Need to verify the hash supplied by the hardware
1372 * which could be wrong.
1374 m->m_flags &= ~(M_HASH | M_CKHASH);
1375 isr = ether_characterize(&m);
1378 KKASSERT(m->m_flags & M_HASH);
1380 if (m->m_pkthdr.hash != mycpuid) {
1382 * Wrong hardware supplied hash; redispatch
1384 ether_dispatch(isr, m);
1386 atomic_add_long(ðer_input_wronghwhash, 1);
1391 ifp = m->m_pkthdr.rcvif;
1393 eh = mtod(m, struct ether_header *);
1394 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1395 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1396 ifp->if_addrlen) == 0)
1397 m->m_flags |= M_BCAST;
1399 m->m_flags |= M_MCAST;
1403 ether_input_oncpu(ifp, m);
1407 * Send the packet to the target msgport
1409 * At this point the packet had better be characterized (M_HASH set),
1410 * so we know which cpu to send it to.
1413 ether_dispatch(int isr, struct mbuf *m)
1415 struct netmsg_packet *pmsg;
1417 KKASSERT(m->m_flags & M_HASH);
1418 pmsg = &m->m_hdr.mh_netmsg;
1419 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1420 0, ether_input_handler);
1421 pmsg->nm_packet = m;
1422 pmsg->base.lmsg.u.ms_result = isr;
1424 logether(disp_beg, NULL);
1425 lwkt_sendmsg(netisr_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1426 logether(disp_end, NULL);
1430 * Process a received Ethernet packet.
1432 * The ethernet header is assumed to be in the mbuf so the caller
1433 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1434 * bytes in the first mbuf.
1437 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1443 /* Discard packet if interface is not up */
1444 if (!(ifp->if_flags & IFF_UP)) {
1449 if (m->m_len < sizeof(struct ether_header)) {
1450 /* XXX error in the caller. */
1455 m->m_pkthdr.rcvif = ifp;
1457 logether(pkt_beg, ifp);
1459 ETHER_BPF_MTAP(ifp, m);
1461 ifp->if_ibytes += m->m_pkthdr.len;
1463 if (ifp->if_flags & IFF_MONITOR) {
1464 struct ether_header *eh;
1466 eh = mtod(m, struct ether_header *);
1467 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1471 * Interface marked for monitoring; discard packet.
1475 logether(pkt_end, ifp);
1480 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1481 * we can dispatch it immediately without further inspection.
1483 if (pi != NULL && (m->m_flags & M_HASH)) {
1485 atomic_add_long(ðer_pktinfo_try, 1);
1487 netisr_hashcheck(pi->pi_netisr, m, pi);
1488 if (m->m_flags & M_HASH) {
1489 ether_dispatch(pi->pi_netisr, m);
1491 atomic_add_long(ðer_pktinfo_hit, 1);
1493 logether(pkt_end, ifp);
1498 else if (ifp->if_capenable & IFCAP_RSS) {
1500 atomic_add_long(ðer_rss_nopi, 1);
1502 atomic_add_long(ðer_rss_nohash, 1);
1507 * Packet hash will be recalculated by software, so clear
1508 * the M_HASH and M_CKHASH flag set by the driver; the hash
1509 * value calculated by the hardware may not be exactly what
1512 m->m_flags &= ~(M_HASH | M_CKHASH);
1514 if (!ether_vlancheck(&m)) {
1515 KKASSERT(m == NULL);
1516 logether(pkt_end, ifp);
1520 isr = ether_characterize(&m);
1522 logether(pkt_end, ifp);
1527 * Finally dispatch it
1529 ether_dispatch(isr, m);
1531 logether(pkt_end, ifp);
1535 ether_characterize(struct mbuf **m0)
1537 struct mbuf *m = *m0;
1538 struct ether_header *eh;
1539 uint16_t ether_type;
1542 eh = mtod(m, struct ether_header *);
1543 ether_type = ntohs(eh->ether_type);
1546 * Map ether type to netisr id.
1548 switch (ether_type) {
1560 case ETHERTYPE_IPV6:
1572 case ETHERTYPE_MPLS:
1573 case ETHERTYPE_MPLS_MCAST:
1574 m->m_flags |= M_MPLSLABELED;
1581 * NETISR_MAX is an invalid value; it is chosen to let
1582 * netisr_characterize() know that we have no clear
1583 * idea where this packet should go.
1590 * Ask the isr to characterize the packet since we couldn't.
1591 * This is an attempt to optimally get us onto the correct protocol
1594 netisr_characterize(isr, &m, sizeof(struct ether_header));
1601 ether_demux_handler(netmsg_t nmsg)
1603 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1609 ifp = m->m_pkthdr.rcvif;
1611 ether_demux_oncpu(ifp, m);
1615 ether_demux(struct mbuf *m)
1617 struct netmsg_packet *pmsg;
1620 isr = ether_characterize(&m);
1624 KKASSERT(m->m_flags & M_HASH);
1625 pmsg = &m->m_hdr.mh_netmsg;
1626 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1627 0, ether_demux_handler);
1628 pmsg->nm_packet = m;
1629 pmsg->base.lmsg.u.ms_result = isr;
1631 lwkt_sendmsg(netisr_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1635 ether_tso_pullup(struct mbuf **mp, int *hoff0, struct ip **ip, int *iphlen,
1636 struct tcphdr **th, int *thoff)
1638 struct mbuf *m = *mp;
1639 struct ether_header *eh;
1643 KASSERT(M_WRITABLE(m), ("not writable"));
1645 hoff = ETHER_HDR_LEN;
1646 if (m->m_len < hoff) {
1647 m = m_pullup(m, hoff);
1651 eh = mtod(m, struct ether_header *);
1652 type = eh->ether_type;
1654 if (type == htons(ETHERTYPE_VLAN)) {
1655 struct ether_vlan_header *evh;
1657 hoff += EVL_ENCAPLEN;
1658 if (m->m_len < hoff) {
1659 m = m_pullup(m, hoff);
1663 evh = mtod(m, struct ether_vlan_header *);
1664 type = evh->evl_proto;
1666 KASSERT(type == htons(ETHERTYPE_IP), ("not IP %d", ntohs(type)));
1670 return tcp_tso_pullup(mp, hoff, ip, iphlen, th, thoff);
1680 kether_aton(const char *macstr, u_char *addr)
1682 unsigned int o0, o1, o2, o3, o4, o5;
1685 if (macstr == NULL || addr == NULL)
1688 n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
1704 kether_ntoa(const u_char *addr, char *buf)
1706 int len = ETHER_ADDRSTRLEN + 1;
1709 n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0],
1710 addr[1], addr[2], addr[3], addr[4], addr[5]);
1718 MODULE_VERSION(ether, 1);