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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
34 #include "opt_inet6.h"
37 #include "opt_netgraph.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/globaldata.h>
44 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/msgport.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread.h>
55 #include <sys/thread2.h>
56 #include <sys/mplock2.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
61 #include <net/if_llc.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/ifq_var.h>
66 #include <net/ethernet.h>
67 #include <net/vlan/if_vlan_ether.h>
68 #include <net/vlan/if_vlan_var.h>
69 #include <net/netmsg2.h>
70 #include <net/netisr2.h>
72 #if defined(INET) || defined(INET6)
73 #include <netinet/in.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/tcp_var.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/ip_flow.h>
78 #include <net/ipfw/ip_fw.h>
79 #include <net/dummynet/ip_dummynet.h>
82 #include <netinet6/nd6.h>
86 #include <netinet/ip_carp.h>
90 #include <netproto/ipx/ipx.h>
91 #include <netproto/ipx/ipx_if.h>
92 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
93 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
94 short *tp, int *hlen);
98 #include <netproto/mpls/mpls.h>
101 /* netgraph node hooks for ng_ether(4) */
102 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105 void (*ng_ether_attach_p)(struct ifnet *ifp);
106 void (*ng_ether_detach_p)(struct ifnet *ifp);
108 void (*vlan_input_p)(struct mbuf *);
110 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
112 static void ether_restore_header(struct mbuf **, const struct ether_header *,
113 const struct ether_header *);
114 static int ether_characterize(struct mbuf **);
115 static void ether_dispatch(int, 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;
152 static u_long ether_input_wronghwhash;
153 static int ether_input_ckhash;
155 #define ETHER_TSOLEN_DEFAULT (4 * ETHERMTU)
157 static int ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
158 TUNABLE_INT("net.link.ether.tsolen", ðer_tsolen_default);
160 SYSCTL_DECL(_net_link);
161 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
162 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
163 ðer_debug, 0, "Ether debug");
164 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
165 ðer_ipfw, 0, "Pass ether pkts through firewall");
166 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
167 ðer_restore_hdr, 0, "# of ether header restoration");
168 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
169 ðer_prepend_hdr, 0,
170 "# of ether header restoration which prepends mbuf");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
172 ðer_input_wronghash, 0, "# of input packets with wrong hash");
173 SYSCTL_INT(_net_link_ether, OID_AUTO, tsolen, CTLFLAG_RW,
174 ðer_tsolen_default, 0, "Default max TSO length");
177 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
178 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
179 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
180 ðer_rss_nohash, 0, "# of packets do not have hash");
181 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
182 ðer_pktinfo_try, 0,
183 "# of tries to find packets' msgport using pktinfo");
184 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
185 ðer_pktinfo_hit, 0,
186 "# of packets whose msgport are found using pktinfo");
187 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
188 ðer_input_requeue, 0, "# of input packets gets requeued");
190 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
191 ðer_input_wronghwhash, 0, "# of input packets with wrong hw hash");
192 SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
193 ðer_input_ckhash, 0, "always check hash");
195 #define ETHER_KTR_STR "ifp=%p"
196 #define ETHER_KTR_ARGS struct ifnet *ifp
198 #define KTR_ETHERNET KTR_ALL
200 KTR_INFO_MASTER(ether);
201 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
202 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
203 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
204 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
205 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
208 * Ethernet output routine.
209 * Encapsulate a packet of type family for the local net.
210 * Use trailer local net encapsulation if enough data in first
211 * packet leaves a multiple of 512 bytes of data in remainder.
212 * Assumes that ifp is actually pointer to arpcom structure.
215 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
218 struct ether_header *eh, *deh;
221 int hlen = ETHER_HDR_LEN; /* link layer header length */
222 struct arpcom *ac = IFP2AC(ifp);
225 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
227 if (ifp->if_flags & IFF_MONITOR)
229 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
232 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
235 m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
236 eh = mtod(m, struct ether_header *);
237 edst = eh->ether_dhost;
240 * Fill in the destination ethernet address and frame type.
242 switch (dst->sa_family) {
245 if (!arpresolve(ifp, rt, m, dst, edst))
246 return (0); /* if not yet resolved */
248 if (m->m_flags & M_MPLSLABELED)
249 eh->ether_type = htons(ETHERTYPE_MPLS);
252 eh->ether_type = htons(ETHERTYPE_IP);
257 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
258 return (0); /* Something bad happenned. */
259 eh->ether_type = htons(ETHERTYPE_IPV6);
264 if (ef_outputp != NULL) {
266 * Hold BGL and recheck ef_outputp
269 if (ef_outputp != NULL) {
270 error = ef_outputp(ifp, &m, dst,
271 &eh->ether_type, &hlen);
280 eh->ether_type = htons(ETHERTYPE_IPX);
281 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
282 edst, ETHER_ADDR_LEN);
285 case pseudo_AF_HDRCMPLT:
287 loop_copy = -1; /* if this is for us, don't do it */
288 deh = (struct ether_header *)dst->sa_data;
289 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
290 eh->ether_type = deh->ether_type;
294 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
295 gotoerr(EAFNOSUPPORT);
298 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
299 memcpy(eh->ether_shost,
300 ((struct ether_header *)dst->sa_data)->ether_shost,
303 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
306 * Bridges require special output handling.
308 if (ifp->if_bridge) {
309 KASSERT(bridge_output_p != NULL,
310 ("%s: if_bridge not loaded!", __func__));
311 return bridge_output_p(ifp, m);
315 * If a simplex interface, and the packet is being sent to our
316 * Ethernet address or a broadcast address, loopback a copy.
317 * XXX To make a simplex device behave exactly like a duplex
318 * device, we should copy in the case of sending to our own
319 * ethernet address (thus letting the original actually appear
320 * on the wire). However, we don't do that here for security
321 * reasons and compatibility with the original behavior.
323 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
326 if (m->m_pkthdr.csum_flags & CSUM_IP)
327 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
328 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
329 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
330 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
333 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
334 n->m_pkthdr.csum_flags |= csum_flags;
335 if (csum_flags & CSUM_DATA_VALID)
336 n->m_pkthdr.csum_data = 0xffff;
337 if_simloop(ifp, n, dst->sa_family, hlen);
339 IFNET_STAT_INC(ifp, iqdrops, 1);
340 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
341 ETHER_ADDR_LEN) == 0) {
342 m->m_pkthdr.csum_flags |= csum_flags;
343 if (csum_flags & CSUM_DATA_VALID)
344 m->m_pkthdr.csum_data = 0xffff;
345 if_simloop(ifp, m, dst->sa_family, hlen);
346 return (0); /* XXX */
351 if (ifp->if_type == IFT_CARP) {
352 ifp = carp_parent(ifp);
354 gotoerr(ENETUNREACH);
359 * Check precondition again
361 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
363 if (ifp->if_flags & IFF_MONITOR)
365 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
366 (IFF_UP | IFF_RUNNING))
371 /* Handle ng_ether(4) processing, if any */
372 if (ng_ether_output_p != NULL) {
374 * Hold BGL and recheck ng_ether_output_p
377 if (ng_ether_output_p != NULL) {
378 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
390 /* Continue with link-layer output */
391 return ether_output_frame(ifp, m);
399 * Returns the bridge interface an ifp is associated
402 * Only call if ifp->if_bridge != NULL.
405 ether_bridge_interface(struct ifnet *ifp)
407 if (bridge_interface_p)
408 return(bridge_interface_p(ifp->if_bridge));
413 * Ethernet link layer output routine to send a raw frame to the device.
415 * This assumes that the 14 byte Ethernet header is present and contiguous
419 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
421 struct ip_fw *rule = NULL;
423 struct altq_pktattr pktattr;
425 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
427 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
430 /* Extract info from dummynet tag */
431 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
432 KKASSERT(mtag != NULL);
433 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
434 KKASSERT(rule != NULL);
436 m_tag_delete(m, mtag);
437 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
440 if (ifq_is_enabled(&ifp->if_snd))
441 altq_etherclassify(&ifp->if_snd, m, &pktattr);
443 if (IPFW_LOADED && ether_ipfw != 0) {
444 struct ether_header save_eh, *eh;
446 eh = mtod(m, struct ether_header *);
448 m_adj(m, ETHER_HDR_LEN);
449 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
453 return ENOBUFS; /* pkt dropped */
455 return 0; /* consumed e.g. in a pipe */
458 /* packet was ok, restore the ethernet header */
459 ether_restore_header(&m, eh, &save_eh);
468 * Queue message on interface, update output statistics if
469 * successful, and start output if interface not yet active.
471 error = ifq_dispatch(ifp, m, &pktattr);
476 * ipfw processing for ethernet packets (in and out).
477 * The second parameter is NULL from ether_demux(), and ifp from
478 * ether_output_frame().
481 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
482 const struct ether_header *eh)
484 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
485 struct ip_fw_args args;
490 if (*rule != NULL && fw_one_pass)
491 return TRUE; /* dummynet packet, already partially processed */
494 * I need some amount of data to be contiguous.
496 i = min((*m0)->m_pkthdr.len, max_protohdr);
497 if ((*m0)->m_len < i) {
498 *m0 = m_pullup(*m0, i);
506 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
507 m_tag_delete(*m0, mtag);
508 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
509 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
510 KKASSERT(mtag != NULL);
511 m_tag_delete(*m0, mtag);
512 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
515 args.m = *m0; /* the packet we are looking at */
516 args.oif = dst; /* destination, if any */
517 args.rule = *rule; /* matching rule to restart */
518 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
519 i = ip_fw_chk_ptr(&args);
534 * XXX at some point add support for divert/forward actions.
535 * If none of the above matches, we have to drop the pkt.
541 * Pass the pkt to dummynet, which consumes it.
543 m = *m0; /* pass the original to dummynet */
544 *m0 = NULL; /* and nothing back to the caller */
546 ether_restore_header(&m, eh, &save_eh);
550 ip_fw_dn_io_ptr(m, args.cookie,
551 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
556 panic("unknown ipfw return value: %d", i);
561 ether_input(struct ifnet *ifp, struct mbuf *m)
563 ether_input_pkt(ifp, m, NULL);
567 * Perform common duties while attaching to interface list
570 ether_ifattach(struct ifnet *ifp, const uint8_t *lla,
571 lwkt_serialize_t serializer)
573 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
578 ether_ifattach_bpf(struct ifnet *ifp, const uint8_t *lla,
579 u_int dlt, u_int hdrlen, lwkt_serialize_t serializer)
581 struct sockaddr_dl *sdl;
582 char ethstr[ETHER_ADDRSTRLEN + 1];
586 ifp->if_type = IFT_ETHER;
587 ifp->if_addrlen = ETHER_ADDR_LEN;
588 ifp->if_hdrlen = ETHER_HDR_LEN;
589 if_attach(ifp, serializer);
591 for (i = 0; i < ifq->altq_subq_cnt; ++i) {
592 struct ifaltq_subque *ifsq = ifq_get_subq(ifq, i);
594 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen *
595 (ETHER_MAX_LEN - ETHER_CRC_LEN);
597 ifp->if_mtu = ETHERMTU;
598 if (ifp->if_tsolen <= 0) {
599 if ((ether_tsolen_default / ETHERMTU) < 2) {
600 kprintf("ether TSO maxlen %d -> %d\n",
601 ether_tsolen_default, ETHER_TSOLEN_DEFAULT);
602 ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
604 ifp->if_tsolen = ether_tsolen_default;
606 if (ifp->if_baudrate == 0)
607 ifp->if_baudrate = 10000000;
608 ifp->if_output = ether_output;
609 ifp->if_input = ether_input;
610 ifp->if_resolvemulti = ether_resolvemulti;
611 ifp->if_broadcastaddr = etherbroadcastaddr;
612 sdl = IF_LLSOCKADDR(ifp);
613 sdl->sdl_type = IFT_ETHER;
614 sdl->sdl_alen = ifp->if_addrlen;
615 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
617 * XXX Keep the current drivers happy.
618 * XXX Remove once all drivers have been cleaned up
620 if (lla != IFP2AC(ifp)->ac_enaddr)
621 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
622 bpfattach(ifp, dlt, hdrlen);
623 if (ng_ether_attach_p != NULL)
624 (*ng_ether_attach_p)(ifp);
626 if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
630 * Perform common duties while detaching an Ethernet interface
633 ether_ifdetach(struct ifnet *ifp)
637 if (ng_ether_detach_p != NULL)
638 (*ng_ether_detach_p)(ifp);
644 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
646 struct ifaddr *ifa = (struct ifaddr *) data;
647 struct ifreq *ifr = (struct ifreq *) data;
650 #define IF_INIT(ifp) \
652 if (((ifp)->if_flags & IFF_UP) == 0) { \
653 (ifp)->if_flags |= IFF_UP; \
654 (ifp)->if_init((ifp)->if_softc); \
658 ASSERT_IFNET_SERIALIZED_ALL(ifp);
662 switch (ifa->ifa_addr->sa_family) {
665 IF_INIT(ifp); /* before arpwhohas */
666 arp_ifinit(ifp, ifa);
671 * XXX - This code is probably wrong
675 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
676 struct arpcom *ac = IFP2AC(ifp);
678 if (ipx_nullhost(*ina))
679 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
681 bcopy(ina->x_host.c_host, ac->ac_enaddr,
682 sizeof ac->ac_enaddr);
684 IF_INIT(ifp); /* Set new address. */
695 bcopy(IFP2AC(ifp)->ac_enaddr,
696 ((struct sockaddr *)ifr->ifr_data)->sa_data,
702 * Set the interface MTU.
704 if (ifr->ifr_mtu > ETHERMTU) {
707 ifp->if_mtu = ifr->ifr_mtu;
722 struct sockaddr **llsa,
725 struct sockaddr_dl *sdl;
727 struct sockaddr_in *sin;
730 struct sockaddr_in6 *sin6;
734 switch(sa->sa_family) {
737 * No mapping needed. Just check that it's a valid MC address.
739 sdl = (struct sockaddr_dl *)sa;
740 e_addr = LLADDR(sdl);
741 if ((e_addr[0] & 1) != 1)
742 return EADDRNOTAVAIL;
748 sin = (struct sockaddr_in *)sa;
749 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
750 return EADDRNOTAVAIL;
751 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
752 sdl->sdl_len = sizeof *sdl;
753 sdl->sdl_family = AF_LINK;
754 sdl->sdl_index = ifp->if_index;
755 sdl->sdl_type = IFT_ETHER;
756 sdl->sdl_alen = ETHER_ADDR_LEN;
757 e_addr = LLADDR(sdl);
758 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
759 *llsa = (struct sockaddr *)sdl;
764 sin6 = (struct sockaddr_in6 *)sa;
765 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
767 * An IP6 address of 0 means listen to all
768 * of the Ethernet multicast address used for IP6.
769 * (This is used for multicast routers.)
771 ifp->if_flags |= IFF_ALLMULTI;
775 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
776 return EADDRNOTAVAIL;
777 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
778 sdl->sdl_len = sizeof *sdl;
779 sdl->sdl_family = AF_LINK;
780 sdl->sdl_index = ifp->if_index;
781 sdl->sdl_type = IFT_ETHER;
782 sdl->sdl_alen = ETHER_ADDR_LEN;
783 e_addr = LLADDR(sdl);
784 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
785 *llsa = (struct sockaddr *)sdl;
791 * Well, the text isn't quite right, but it's the name
800 * This is for reference. We have a table-driven version
801 * of the little-endian crc32 generator, which is faster
802 * than the double-loop.
805 ether_crc32_le(const uint8_t *buf, size_t len)
807 uint32_t c, crc, carry;
810 crc = 0xffffffffU; /* initial value */
812 for (i = 0; i < len; i++) {
814 for (j = 0; j < 8; j++) {
815 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
819 crc = (crc ^ ETHER_CRC_POLY_LE);
827 ether_crc32_le(const uint8_t *buf, size_t len)
829 static const uint32_t crctab[] = {
830 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
831 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
832 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
833 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
838 crc = 0xffffffffU; /* initial value */
840 for (i = 0; i < len; i++) {
842 crc = (crc >> 4) ^ crctab[crc & 0xf];
843 crc = (crc >> 4) ^ crctab[crc & 0xf];
851 ether_crc32_be(const uint8_t *buf, size_t len)
853 uint32_t c, crc, carry;
856 crc = 0xffffffffU; /* initial value */
858 for (i = 0; i < len; i++) {
860 for (j = 0; j < 8; j++) {
861 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
865 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
873 * find the size of ethernet header, and call classifier
876 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
877 struct altq_pktattr *pktattr)
879 struct ether_header *eh;
881 int hlen, af, hdrsize;
883 hlen = sizeof(struct ether_header);
884 eh = mtod(m, struct ether_header *);
886 ether_type = ntohs(eh->ether_type);
887 if (ether_type < ETHERMTU) {
889 struct llc *llc = (struct llc *)(eh + 1);
892 if (m->m_len < hlen ||
893 llc->llc_dsap != LLC_SNAP_LSAP ||
894 llc->llc_ssap != LLC_SNAP_LSAP ||
895 llc->llc_control != LLC_UI)
896 goto bad; /* not snap! */
898 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
901 if (ether_type == ETHERTYPE_IP) {
903 hdrsize = 20; /* sizeof(struct ip) */
905 } else if (ether_type == ETHERTYPE_IPV6) {
907 hdrsize = 40; /* sizeof(struct ip6_hdr) */
912 while (m->m_len <= 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 * Upper layer processing for a received Ethernet packet.
970 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
972 struct ether_header *eh;
973 int isr, discard = 0;
975 struct ip_fw *rule = NULL;
978 KASSERT(m->m_len >= ETHER_HDR_LEN,
979 ("ether header is not contiguous!"));
981 eh = mtod(m, struct ether_header *);
983 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
986 /* Extract info from dummynet tag */
987 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
988 KKASSERT(mtag != NULL);
989 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
990 KKASSERT(rule != NULL);
992 m_tag_delete(m, mtag);
993 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
995 /* packet is passing the second time */
1000 * We got a packet which was unicast to a different Ethernet
1001 * address. If the driver is working properly, then this
1002 * situation can only happen when the interface is in
1003 * promiscuous mode. We defer the packet discarding until the
1004 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1007 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1008 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1009 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1010 if (ether_debug & 1) {
1011 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1012 "%02x:%02x:%02x:%02x:%02x:%02x "
1013 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1027 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1028 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1029 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1030 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1031 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1032 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1035 if ((ether_debug & 2) == 0)
1040 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1041 struct ether_header save_eh = *eh;
1043 /* XXX old crufty stuff, needs to be removed */
1044 m_adj(m, sizeof(struct ether_header));
1046 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1051 ether_restore_header(&m, eh, &save_eh);
1054 eh = mtod(m, struct ether_header *);
1057 ether_type = ntohs(eh->ether_type);
1058 KKASSERT(ether_type != ETHERTYPE_VLAN);
1060 if (m->m_flags & M_VLANTAG) {
1061 void (*vlan_input_func)(struct mbuf *);
1063 vlan_input_func = vlan_input_p;
1064 if (vlan_input_func != NULL) {
1067 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1074 * If we have been asked to discard this packet
1075 * (e.g. not for us), drop it before entering
1084 * Clear protocol specific flags,
1085 * before entering the upper layer.
1087 m->m_flags &= ~M_ETHER_FLAGS;
1089 /* Strip ethernet header. */
1090 m_adj(m, sizeof(struct ether_header));
1092 switch (ether_type) {
1095 if ((m->m_flags & M_LENCHECKED) == 0) {
1096 if (!ip_lengthcheck(&m, 0))
1099 if (ipflow_fastforward(m))
1105 if (ifp->if_flags & IFF_NOARP) {
1106 /* Discard packet if ARP is disabled on interface */
1115 case ETHERTYPE_IPV6:
1124 * Hold BGL and recheck ef_inputp
1127 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1138 case ETHERTYPE_MPLS:
1139 case ETHERTYPE_MPLS_MCAST:
1140 /* Should have been set by ether_input_pkt(). */
1141 KKASSERT(m->m_flags & M_MPLSLABELED);
1148 * The accurate msgport is not determined before
1149 * we reach here, so recharacterize packet.
1151 m->m_flags &= ~M_HASH;
1155 * Hold BGL and recheck ef_inputp
1158 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1165 if (ng_ether_input_orphan_p != NULL) {
1167 * Put back the ethernet header so netgraph has a
1168 * consistent view of inbound packets.
1170 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1173 * M_PREPEND frees the mbuf in case of failure.
1178 * Hold BGL and recheck ng_ether_input_orphan_p
1181 if (ng_ether_input_orphan_p != NULL) {
1182 ng_ether_input_orphan_p(ifp, m);
1192 if (m->m_flags & M_HASH) {
1193 if (&curthread->td_msgport ==
1194 netisr_hashport(m->m_pkthdr.hash)) {
1195 netisr_handle(isr, m);
1199 * XXX Something is wrong,
1200 * we probably should panic here!
1202 m->m_flags &= ~M_HASH;
1203 atomic_add_long(ðer_input_wronghash, 1);
1207 atomic_add_long(ðer_input_requeue, 1);
1209 netisr_queue(isr, m);
1213 * First we perform any link layer operations, then continue to the
1214 * upper layers with ether_demux_oncpu().
1217 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1223 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1225 * Receiving interface's flags are changed, when this
1226 * packet is waiting for processing; discard it.
1233 * Tap the packet off here for a bridge. bridge_input()
1234 * will return NULL if it has consumed the packet, otherwise
1235 * it gets processed as normal. Note that bridge_input()
1236 * will always return the original packet if we need to
1237 * process it locally.
1239 if (ifp->if_bridge) {
1240 KASSERT(bridge_input_p != NULL,
1241 ("%s: if_bridge not loaded!", __func__));
1243 if(m->m_flags & M_ETHER_BRIDGED) {
1244 m->m_flags &= ~M_ETHER_BRIDGED;
1246 m = bridge_input_p(ifp, m);
1250 KASSERT(ifp == m->m_pkthdr.rcvif,
1251 ("bridge_input_p changed rcvif"));
1256 carp = ifp->if_carp;
1258 m = carp_input(carp, m);
1261 KASSERT(ifp == m->m_pkthdr.rcvif,
1262 ("carp_input changed rcvif"));
1266 /* Handle ng_ether(4) processing, if any */
1267 if (ng_ether_input_p != NULL) {
1269 * Hold BGL and recheck ng_ether_input_p
1272 if (ng_ether_input_p != NULL)
1273 ng_ether_input_p(ifp, &m);
1280 /* Continue with upper layer processing */
1281 ether_demux_oncpu(ifp, m);
1285 * Perform certain functions of ether_input_pkt():
1287 * - Update statistics
1288 * - Run bpf(4) tap if requested
1289 * Then pass the packet to ether_input_oncpu().
1291 * This function should be used by pseudo interface (e.g. vlan(4)),
1292 * when it tries to claim that the packet is received by it.
1298 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1300 /* Discard packet if interface is not up */
1301 if (!(ifp->if_flags & IFF_UP)) {
1307 * Change receiving interface. The bridge will often pass a flag to
1308 * ask that this not be done so ARPs get applied to the correct
1311 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1312 m->m_pkthdr.rcvif == NULL) {
1313 m->m_pkthdr.rcvif = ifp;
1316 /* Update statistics */
1317 IFNET_STAT_INC(ifp, ipackets, 1);
1318 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1319 if (m->m_flags & (M_MCAST | M_BCAST))
1320 IFNET_STAT_INC(ifp, imcasts, 1);
1322 if (reinput_flags & REINPUT_RUNBPF)
1325 ether_input_oncpu(ifp, m);
1328 static __inline boolean_t
1329 ether_vlancheck(struct mbuf **m0)
1331 struct mbuf *m = *m0;
1332 struct ether_header *eh;
1333 uint16_t ether_type;
1335 eh = mtod(m, struct ether_header *);
1336 ether_type = ntohs(eh->ether_type);
1338 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1340 * Extract vlan tag if hardware does not do it for us
1342 vlan_ether_decap(&m);
1346 eh = mtod(m, struct ether_header *);
1347 ether_type = ntohs(eh->ether_type);
1350 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1352 * To prevent possible dangerous recursion,
1353 * we don't do vlan-in-vlan
1355 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1358 KKASSERT(ether_type != ETHERTYPE_VLAN);
1360 m->m_flags |= M_ETHER_VLANCHECKED;
1371 ether_input_handler(netmsg_t nmsg)
1373 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1374 struct ether_header *eh;
1381 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1382 if (!ether_vlancheck(&m)) {
1383 KKASSERT(m == NULL);
1387 if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH) ||
1388 __predict_false(ether_input_ckhash)) {
1392 * Need to verify the hash supplied by the hardware
1393 * which could be wrong.
1395 m->m_flags &= ~(M_HASH | M_CKHASH);
1396 isr = ether_characterize(&m);
1399 KKASSERT(m->m_flags & M_HASH);
1401 if (netisr_hashcpu(m->m_pkthdr.hash) != mycpuid) {
1403 * Wrong hardware supplied hash; redispatch
1405 ether_dispatch(isr, m);
1406 if (__predict_false(ether_input_ckhash))
1407 atomic_add_long(ðer_input_wronghwhash, 1);
1411 ifp = m->m_pkthdr.rcvif;
1413 eh = mtod(m, struct ether_header *);
1414 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1415 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1416 ifp->if_addrlen) == 0)
1417 m->m_flags |= M_BCAST;
1419 m->m_flags |= M_MCAST;
1420 IFNET_STAT_INC(ifp, imcasts, 1);
1423 ether_input_oncpu(ifp, m);
1427 * Send the packet to the target netisr msgport
1429 * At this point the packet must be characterized (M_HASH set),
1430 * so we know which netisr to send it to.
1433 ether_dispatch(int isr, struct mbuf *m)
1435 struct netmsg_packet *pmsg;
1437 KKASSERT(m->m_flags & M_HASH);
1438 pmsg = &m->m_hdr.mh_netmsg;
1439 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1440 0, ether_input_handler);
1441 pmsg->nm_packet = m;
1442 pmsg->base.lmsg.u.ms_result = isr;
1444 logether(disp_beg, NULL);
1445 lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1446 logether(disp_end, NULL);
1450 * Process a received Ethernet packet.
1452 * The ethernet header is assumed to be in the mbuf so the caller
1453 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1454 * bytes in the first mbuf.
1457 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1463 /* Discard packet if interface is not up */
1464 if (!(ifp->if_flags & IFF_UP)) {
1469 if (m->m_len < sizeof(struct ether_header)) {
1470 /* XXX error in the caller. */
1475 m->m_pkthdr.rcvif = ifp;
1477 logether(pkt_beg, ifp);
1479 ETHER_BPF_MTAP(ifp, m);
1481 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1483 if (ifp->if_flags & IFF_MONITOR) {
1484 struct ether_header *eh;
1486 eh = mtod(m, struct ether_header *);
1487 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1488 IFNET_STAT_INC(ifp, imcasts, 1);
1491 * Interface marked for monitoring; discard packet.
1495 logether(pkt_end, ifp);
1500 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1501 * we can dispatch it immediately with trivial checks.
1503 if (pi != NULL && (m->m_flags & M_HASH)) {
1505 atomic_add_long(ðer_pktinfo_try, 1);
1507 netisr_hashcheck(pi->pi_netisr, m, pi);
1508 if (m->m_flags & M_HASH) {
1509 ether_dispatch(pi->pi_netisr, m);
1511 atomic_add_long(ðer_pktinfo_hit, 1);
1513 logether(pkt_end, ifp);
1518 else if (ifp->if_capenable & IFCAP_RSS) {
1520 atomic_add_long(ðer_rss_nopi, 1);
1522 atomic_add_long(ðer_rss_nohash, 1);
1527 * Packet hash will be recalculated by software, so clear
1528 * the M_HASH and M_CKHASH flag set by the driver; the hash
1529 * value calculated by the hardware may not be exactly what
1532 m->m_flags &= ~(M_HASH | M_CKHASH);
1534 if (!ether_vlancheck(&m)) {
1535 KKASSERT(m == NULL);
1536 logether(pkt_end, ifp);
1540 isr = ether_characterize(&m);
1542 logether(pkt_end, ifp);
1547 * Finally dispatch it
1549 ether_dispatch(isr, m);
1551 logether(pkt_end, ifp);
1555 ether_characterize(struct mbuf **m0)
1557 struct mbuf *m = *m0;
1558 struct ether_header *eh;
1559 uint16_t ether_type;
1562 eh = mtod(m, struct ether_header *);
1563 ether_type = ntohs(eh->ether_type);
1566 * Map ether type to netisr id.
1568 switch (ether_type) {
1580 case ETHERTYPE_IPV6:
1592 case ETHERTYPE_MPLS:
1593 case ETHERTYPE_MPLS_MCAST:
1594 m->m_flags |= M_MPLSLABELED;
1601 * NETISR_MAX is an invalid value; it is chosen to let
1602 * netisr_characterize() know that we have no clear
1603 * idea where this packet should go.
1610 * Ask the isr to characterize the packet since we couldn't.
1611 * This is an attempt to optimally get us onto the correct protocol
1614 netisr_characterize(isr, &m, sizeof(struct ether_header));
1621 ether_demux_handler(netmsg_t nmsg)
1623 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1629 ifp = m->m_pkthdr.rcvif;
1631 ether_demux_oncpu(ifp, m);
1635 ether_demux(struct mbuf *m)
1637 struct netmsg_packet *pmsg;
1640 isr = ether_characterize(&m);
1644 KKASSERT(m->m_flags & M_HASH);
1645 pmsg = &m->m_hdr.mh_netmsg;
1646 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1647 0, ether_demux_handler);
1648 pmsg->nm_packet = m;
1649 pmsg->base.lmsg.u.ms_result = isr;
1651 lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1655 kether_aton(const char *macstr, u_char *addr)
1657 unsigned int o0, o1, o2, o3, o4, o5;
1660 if (macstr == NULL || addr == NULL)
1663 n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
1679 kether_ntoa(const u_char *addr, char *buf)
1681 int len = ETHER_ADDRSTRLEN + 1;
1684 n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0],
1685 addr[1], addr[2], addr[3], addr[4], addr[5]);
1693 MODULE_VERSION(ether, 1);