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;
155 static u_long ether_input_wronghwhash;
156 static int ether_input_ckhash;
158 #define ETHER_TSOLEN_DEFAULT (4 * ETHERMTU)
160 static int ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
161 TUNABLE_INT("net.link.ether.tsolen", ðer_tsolen_default);
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, debug, CTLFLAG_RW,
166 ðer_debug, 0, "Ether debug");
167 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
168 ðer_ipfw, 0, "Pass ether pkts through firewall");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
170 ðer_restore_hdr, 0, "# of ether header restoration");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
172 ðer_prepend_hdr, 0,
173 "# of ether header restoration which prepends mbuf");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
175 ðer_input_wronghash, 0, "# of input packets with wrong hash");
176 SYSCTL_INT(_net_link_ether, OID_AUTO, tsolen, CTLFLAG_RW,
177 ðer_tsolen_default, 0, "Default max TSO length");
180 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
181 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
182 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
183 ðer_rss_nohash, 0, "# of packets do not have hash");
184 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
185 ðer_pktinfo_try, 0,
186 "# of tries to find packets' msgport using pktinfo");
187 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
188 ðer_pktinfo_hit, 0,
189 "# of packets whose msgport are found using pktinfo");
190 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
191 ðer_input_requeue, 0, "# of input packets gets requeued");
193 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
194 ðer_input_wronghwhash, 0, "# of input packets with wrong hw hash");
195 SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
196 ðer_input_ckhash, 0, "always check hash");
198 #define ETHER_KTR_STR "ifp=%p"
199 #define ETHER_KTR_ARGS struct ifnet *ifp
201 #define KTR_ETHERNET KTR_ALL
203 KTR_INFO_MASTER(ether);
204 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
205 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
206 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
207 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
208 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
211 * Ethernet output routine.
212 * Encapsulate a packet of type family for the local net.
213 * Use trailer local net encapsulation if enough data in first
214 * packet leaves a multiple of 512 bytes of data in remainder.
215 * Assumes that ifp is actually pointer to arpcom structure.
218 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
221 struct ether_header *eh, *deh;
224 int hlen = ETHER_HDR_LEN; /* link layer header length */
225 struct arpcom *ac = IFP2AC(ifp);
228 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
230 if (ifp->if_flags & IFF_MONITOR)
232 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
235 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
238 m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
239 eh = mtod(m, struct ether_header *);
240 edst = eh->ether_dhost;
243 * Fill in the destination ethernet address and frame type.
245 switch (dst->sa_family) {
248 if (!arpresolve(ifp, rt, m, dst, edst))
249 return (0); /* if not yet resolved */
251 if (m->m_flags & M_MPLSLABELED)
252 eh->ether_type = htons(ETHERTYPE_MPLS);
255 eh->ether_type = htons(ETHERTYPE_IP);
260 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
261 return (0); /* Something bad happenned. */
262 eh->ether_type = htons(ETHERTYPE_IPV6);
267 if (ef_outputp != NULL) {
269 * Hold BGL and recheck ef_outputp
272 if (ef_outputp != NULL) {
273 error = ef_outputp(ifp, &m, dst,
274 &eh->ether_type, &hlen);
283 eh->ether_type = htons(ETHERTYPE_IPX);
284 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
285 edst, ETHER_ADDR_LEN);
288 case pseudo_AF_HDRCMPLT:
290 loop_copy = -1; /* if this is for us, don't do it */
291 deh = (struct ether_header *)dst->sa_data;
292 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
293 eh->ether_type = deh->ether_type;
297 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
298 gotoerr(EAFNOSUPPORT);
301 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
302 memcpy(eh->ether_shost,
303 ((struct ether_header *)dst->sa_data)->ether_shost,
306 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
309 * Bridges require special output handling.
311 if (ifp->if_bridge) {
312 KASSERT(bridge_output_p != NULL,
313 ("%s: if_bridge not loaded!", __func__));
314 return bridge_output_p(ifp, m);
318 * If a simplex interface, and the packet is being sent to our
319 * Ethernet address or a broadcast address, loopback a copy.
320 * XXX To make a simplex device behave exactly like a duplex
321 * device, we should copy in the case of sending to our own
322 * ethernet address (thus letting the original actually appear
323 * on the wire). However, we don't do that here for security
324 * reasons and compatibility with the original behavior.
326 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
329 if (m->m_pkthdr.csum_flags & CSUM_IP)
330 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
331 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
332 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
333 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
336 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
337 n->m_pkthdr.csum_flags |= csum_flags;
338 if (csum_flags & CSUM_DATA_VALID)
339 n->m_pkthdr.csum_data = 0xffff;
340 if_simloop(ifp, n, dst->sa_family, hlen);
342 IFNET_STAT_INC(ifp, iqdrops, 1);
343 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
344 ETHER_ADDR_LEN) == 0) {
345 m->m_pkthdr.csum_flags |= csum_flags;
346 if (csum_flags & CSUM_DATA_VALID)
347 m->m_pkthdr.csum_data = 0xffff;
348 if_simloop(ifp, m, dst->sa_family, hlen);
349 return (0); /* XXX */
354 if (ifp->if_type == IFT_CARP) {
355 ifp = carp_parent(ifp);
357 gotoerr(ENETUNREACH);
362 * Check precondition again
364 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
366 if (ifp->if_flags & IFF_MONITOR)
368 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
369 (IFF_UP | IFF_RUNNING))
374 /* Handle ng_ether(4) processing, if any */
375 if (ng_ether_output_p != NULL) {
377 * Hold BGL and recheck ng_ether_output_p
380 if (ng_ether_output_p != NULL) {
381 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
393 /* Continue with link-layer output */
394 return ether_output_frame(ifp, m);
402 * Returns the bridge interface an ifp is associated
405 * Only call if ifp->if_bridge != NULL.
408 ether_bridge_interface(struct ifnet *ifp)
410 if (bridge_interface_p)
411 return(bridge_interface_p(ifp->if_bridge));
416 * Ethernet link layer output routine to send a raw frame to the device.
418 * This assumes that the 14 byte Ethernet header is present and contiguous
422 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
424 struct ip_fw *rule = NULL;
426 struct altq_pktattr pktattr;
428 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
430 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
433 /* Extract info from dummynet tag */
434 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
435 KKASSERT(mtag != NULL);
436 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
437 KKASSERT(rule != NULL);
439 m_tag_delete(m, mtag);
440 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
443 if (ifq_is_enabled(&ifp->if_snd))
444 altq_etherclassify(&ifp->if_snd, m, &pktattr);
446 if (IPFW_LOADED && ether_ipfw != 0) {
447 struct ether_header save_eh, *eh;
449 eh = mtod(m, struct ether_header *);
451 m_adj(m, ETHER_HDR_LEN);
452 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
456 return ENOBUFS; /* pkt dropped */
458 return 0; /* consumed e.g. in a pipe */
461 /* packet was ok, restore the ethernet header */
462 ether_restore_header(&m, eh, &save_eh);
471 * Queue message on interface, update output statistics if
472 * successful, and start output if interface not yet active.
474 error = ifq_dispatch(ifp, m, &pktattr);
479 * ipfw processing for ethernet packets (in and out).
480 * The second parameter is NULL from ether_demux(), and ifp from
481 * ether_output_frame().
484 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
485 const struct ether_header *eh)
487 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
488 struct ip_fw_args args;
493 if (*rule != NULL && fw_one_pass)
494 return TRUE; /* dummynet packet, already partially processed */
497 * I need some amount of data to be contiguous.
499 i = min((*m0)->m_pkthdr.len, max_protohdr);
500 if ((*m0)->m_len < i) {
501 *m0 = m_pullup(*m0, i);
509 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
510 m_tag_delete(*m0, mtag);
511 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
512 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
513 KKASSERT(mtag != NULL);
514 m_tag_delete(*m0, mtag);
515 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
518 args.m = *m0; /* the packet we are looking at */
519 args.oif = dst; /* destination, if any */
520 args.rule = *rule; /* matching rule to restart */
521 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
522 i = ip_fw_chk_ptr(&args);
537 * XXX at some point add support for divert/forward actions.
538 * If none of the above matches, we have to drop the pkt.
544 * Pass the pkt to dummynet, which consumes it.
546 m = *m0; /* pass the original to dummynet */
547 *m0 = NULL; /* and nothing back to the caller */
549 ether_restore_header(&m, eh, &save_eh);
553 ip_fw_dn_io_ptr(m, args.cookie,
554 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
559 panic("unknown ipfw return value: %d", i);
564 ether_input(struct ifnet *ifp, struct mbuf *m)
566 ether_input_pkt(ifp, m, NULL);
570 * Perform common duties while attaching to interface list
573 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
575 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
580 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
581 lwkt_serialize_t serializer)
583 struct sockaddr_dl *sdl;
584 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);
590 ifp->if_mtu = ETHERMTU;
591 if (ifp->if_tsolen <= 0) {
592 if ((ether_tsolen_default / ETHERMTU) < 2) {
593 kprintf("ether TSO maxlen %d -> %d\n",
594 ether_tsolen_default, ETHER_TSOLEN_DEFAULT);
595 ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
597 ifp->if_tsolen = ether_tsolen_default;
599 if (ifp->if_baudrate == 0)
600 ifp->if_baudrate = 10000000;
601 ifp->if_output = ether_output;
602 ifp->if_input = ether_input;
603 ifp->if_resolvemulti = ether_resolvemulti;
604 ifp->if_broadcastaddr = etherbroadcastaddr;
605 sdl = IF_LLSOCKADDR(ifp);
606 sdl->sdl_type = IFT_ETHER;
607 sdl->sdl_alen = ifp->if_addrlen;
608 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
610 * XXX Keep the current drivers happy.
611 * XXX Remove once all drivers have been cleaned up
613 if (lla != IFP2AC(ifp)->ac_enaddr)
614 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
615 bpfattach(ifp, dlt, hdrlen);
616 if (ng_ether_attach_p != NULL)
617 (*ng_ether_attach_p)(ifp);
619 if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
623 * Perform common duties while detaching an Ethernet interface
626 ether_ifdetach(struct ifnet *ifp)
630 if (ng_ether_detach_p != NULL)
631 (*ng_ether_detach_p)(ifp);
637 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
639 struct ifaddr *ifa = (struct ifaddr *) data;
640 struct ifreq *ifr = (struct ifreq *) data;
643 #define IF_INIT(ifp) \
645 if (((ifp)->if_flags & IFF_UP) == 0) { \
646 (ifp)->if_flags |= IFF_UP; \
647 (ifp)->if_init((ifp)->if_softc); \
651 ASSERT_IFNET_SERIALIZED_ALL(ifp);
655 switch (ifa->ifa_addr->sa_family) {
658 IF_INIT(ifp); /* before arpwhohas */
659 arp_ifinit(ifp, ifa);
664 * XXX - This code is probably wrong
668 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
669 struct arpcom *ac = IFP2AC(ifp);
671 if (ipx_nullhost(*ina))
672 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
674 bcopy(ina->x_host.c_host, ac->ac_enaddr,
675 sizeof ac->ac_enaddr);
677 IF_INIT(ifp); /* Set new address. */
688 bcopy(IFP2AC(ifp)->ac_enaddr,
689 ((struct sockaddr *)ifr->ifr_data)->sa_data,
695 * Set the interface MTU.
697 if (ifr->ifr_mtu > ETHERMTU) {
700 ifp->if_mtu = ifr->ifr_mtu;
715 struct sockaddr **llsa,
718 struct sockaddr_dl *sdl;
720 struct sockaddr_in *sin;
723 struct sockaddr_in6 *sin6;
727 switch(sa->sa_family) {
730 * No mapping needed. Just check that it's a valid MC address.
732 sdl = (struct sockaddr_dl *)sa;
733 e_addr = LLADDR(sdl);
734 if ((e_addr[0] & 1) != 1)
735 return EADDRNOTAVAIL;
741 sin = (struct sockaddr_in *)sa;
742 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
743 return EADDRNOTAVAIL;
744 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
745 sdl->sdl_len = sizeof *sdl;
746 sdl->sdl_family = AF_LINK;
747 sdl->sdl_index = ifp->if_index;
748 sdl->sdl_type = IFT_ETHER;
749 sdl->sdl_alen = ETHER_ADDR_LEN;
750 e_addr = LLADDR(sdl);
751 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
752 *llsa = (struct sockaddr *)sdl;
757 sin6 = (struct sockaddr_in6 *)sa;
758 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
760 * An IP6 address of 0 means listen to all
761 * of the Ethernet multicast address used for IP6.
762 * (This is used for multicast routers.)
764 ifp->if_flags |= IFF_ALLMULTI;
768 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
769 return EADDRNOTAVAIL;
770 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
771 sdl->sdl_len = sizeof *sdl;
772 sdl->sdl_family = AF_LINK;
773 sdl->sdl_index = ifp->if_index;
774 sdl->sdl_type = IFT_ETHER;
775 sdl->sdl_alen = ETHER_ADDR_LEN;
776 e_addr = LLADDR(sdl);
777 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
778 *llsa = (struct sockaddr *)sdl;
784 * Well, the text isn't quite right, but it's the name
793 * This is for reference. We have a table-driven version
794 * of the little-endian crc32 generator, which is faster
795 * than the double-loop.
798 ether_crc32_le(const uint8_t *buf, size_t len)
800 uint32_t c, crc, carry;
803 crc = 0xffffffffU; /* initial value */
805 for (i = 0; i < len; i++) {
807 for (j = 0; j < 8; j++) {
808 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
812 crc = (crc ^ ETHER_CRC_POLY_LE);
820 ether_crc32_le(const uint8_t *buf, size_t len)
822 static const uint32_t crctab[] = {
823 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
824 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
825 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
826 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
831 crc = 0xffffffffU; /* initial value */
833 for (i = 0; i < len; i++) {
835 crc = (crc >> 4) ^ crctab[crc & 0xf];
836 crc = (crc >> 4) ^ crctab[crc & 0xf];
844 ether_crc32_be(const uint8_t *buf, size_t len)
846 uint32_t c, crc, carry;
849 crc = 0xffffffffU; /* initial value */
851 for (i = 0; i < len; i++) {
853 for (j = 0; j < 8; j++) {
854 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
858 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
866 * find the size of ethernet header, and call classifier
869 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
870 struct altq_pktattr *pktattr)
872 struct ether_header *eh;
874 int hlen, af, hdrsize;
876 hlen = sizeof(struct ether_header);
877 eh = mtod(m, struct ether_header *);
879 ether_type = ntohs(eh->ether_type);
880 if (ether_type < ETHERMTU) {
882 struct llc *llc = (struct llc *)(eh + 1);
885 if (m->m_len < hlen ||
886 llc->llc_dsap != LLC_SNAP_LSAP ||
887 llc->llc_ssap != LLC_SNAP_LSAP ||
888 llc->llc_control != LLC_UI)
889 goto bad; /* not snap! */
891 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
894 if (ether_type == ETHERTYPE_IP) {
896 hdrsize = 20; /* sizeof(struct ip) */
898 } else if (ether_type == ETHERTYPE_IPV6) {
900 hdrsize = 40; /* sizeof(struct ip6_hdr) */
905 while (m->m_len <= hlen) {
909 if (m->m_len < hlen + hdrsize) {
911 * ip header is not in a single mbuf. this should not
912 * happen in the current code.
913 * (todo: use m_pulldown in the future)
919 ifq_classify(ifq, m, af, pktattr);
926 pktattr->pattr_class = NULL;
927 pktattr->pattr_hdr = NULL;
928 pktattr->pattr_af = AF_UNSPEC;
932 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
933 const struct ether_header *save_eh)
935 struct mbuf *m = *m0;
940 * Prepend the header, optimize for the common case of
941 * eh pointing into the mbuf.
943 if ((const void *)(eh + 1) == (void *)m->m_data) {
944 m->m_data -= ETHER_HDR_LEN;
945 m->m_len += ETHER_HDR_LEN;
946 m->m_pkthdr.len += ETHER_HDR_LEN;
950 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
952 bcopy(save_eh, mtod(m, struct ether_header *),
960 * Upper layer processing for a received Ethernet packet.
963 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
965 struct ether_header *eh;
966 int isr, discard = 0;
968 struct ip_fw *rule = NULL;
971 KASSERT(m->m_len >= ETHER_HDR_LEN,
972 ("ether header is not contiguous!"));
974 eh = mtod(m, struct ether_header *);
976 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
979 /* Extract info from dummynet tag */
980 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
981 KKASSERT(mtag != NULL);
982 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
983 KKASSERT(rule != NULL);
985 m_tag_delete(m, mtag);
986 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
988 /* packet is passing the second time */
993 * We got a packet which was unicast to a different Ethernet
994 * address. If the driver is working properly, then this
995 * situation can only happen when the interface is in
996 * promiscuous mode. We defer the packet discarding until the
997 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1000 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1001 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1002 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1003 if (ether_debug & 1) {
1004 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1005 "%02x:%02x:%02x:%02x:%02x:%02x "
1006 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1020 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1021 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1022 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1023 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1024 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1025 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1028 if ((ether_debug & 2) == 0)
1033 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1034 struct ether_header save_eh = *eh;
1036 /* XXX old crufty stuff, needs to be removed */
1037 m_adj(m, sizeof(struct ether_header));
1039 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1044 ether_restore_header(&m, eh, &save_eh);
1047 eh = mtod(m, struct ether_header *);
1050 ether_type = ntohs(eh->ether_type);
1051 KKASSERT(ether_type != ETHERTYPE_VLAN);
1053 if (m->m_flags & M_VLANTAG) {
1054 void (*vlan_input_func)(struct mbuf *);
1056 vlan_input_func = vlan_input_p;
1057 if (vlan_input_func != NULL) {
1060 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1067 * If we have been asked to discard this packet
1068 * (e.g. not for us), drop it before entering
1077 * Clear protocol specific flags,
1078 * before entering the upper layer.
1080 m->m_flags &= ~M_ETHER_FLAGS;
1082 /* Strip ethernet header. */
1083 m_adj(m, sizeof(struct ether_header));
1085 switch (ether_type) {
1088 if ((m->m_flags & M_LENCHECKED) == 0) {
1089 if (!ip_lengthcheck(&m, 0))
1092 if (ipflow_fastforward(m))
1098 if (ifp->if_flags & IFF_NOARP) {
1099 /* Discard packet if ARP is disabled on interface */
1108 case ETHERTYPE_IPV6:
1117 * Hold BGL and recheck ef_inputp
1120 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1131 case ETHERTYPE_MPLS:
1132 case ETHERTYPE_MPLS_MCAST:
1133 /* Should have been set by ether_input_pkt(). */
1134 KKASSERT(m->m_flags & M_MPLSLABELED);
1141 * The accurate msgport is not determined before
1142 * we reach here, so recharacterize packet.
1144 m->m_flags &= ~M_HASH;
1148 * Hold BGL and recheck ef_inputp
1151 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1158 if (ng_ether_input_orphan_p != NULL) {
1160 * Put back the ethernet header so netgraph has a
1161 * consistent view of inbound packets.
1163 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1166 * M_PREPEND frees the mbuf in case of failure.
1171 * Hold BGL and recheck ng_ether_input_orphan_p
1174 if (ng_ether_input_orphan_p != NULL) {
1175 ng_ether_input_orphan_p(ifp, m);
1185 if (m->m_flags & M_HASH) {
1186 if (&curthread->td_msgport ==
1187 netisr_cpuport(m->m_pkthdr.hash)) {
1188 netisr_handle(isr, m);
1192 * XXX Something is wrong,
1193 * we probably should panic here!
1195 m->m_flags &= ~M_HASH;
1196 atomic_add_long(ðer_input_wronghash, 1);
1200 atomic_add_long(ðer_input_requeue, 1);
1202 netisr_queue(isr, m);
1206 * First we perform any link layer operations, then continue to the
1207 * upper layers with ether_demux_oncpu().
1210 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1216 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1218 * Receiving interface's flags are changed, when this
1219 * packet is waiting for processing; discard it.
1226 * Tap the packet off here for a bridge. bridge_input()
1227 * will return NULL if it has consumed the packet, otherwise
1228 * it gets processed as normal. Note that bridge_input()
1229 * will always return the original packet if we need to
1230 * process it locally.
1232 if (ifp->if_bridge) {
1233 KASSERT(bridge_input_p != NULL,
1234 ("%s: if_bridge not loaded!", __func__));
1236 if(m->m_flags & M_ETHER_BRIDGED) {
1237 m->m_flags &= ~M_ETHER_BRIDGED;
1239 m = bridge_input_p(ifp, m);
1243 KASSERT(ifp == m->m_pkthdr.rcvif,
1244 ("bridge_input_p changed rcvif"));
1249 carp = ifp->if_carp;
1251 m = carp_input(carp, m);
1254 KASSERT(ifp == m->m_pkthdr.rcvif,
1255 ("carp_input changed rcvif"));
1259 /* Handle ng_ether(4) processing, if any */
1260 if (ng_ether_input_p != NULL) {
1262 * Hold BGL and recheck ng_ether_input_p
1265 if (ng_ether_input_p != NULL)
1266 ng_ether_input_p(ifp, &m);
1273 /* Continue with upper layer processing */
1274 ether_demux_oncpu(ifp, m);
1278 * Perform certain functions of ether_input_pkt():
1280 * - Update statistics
1281 * - Run bpf(4) tap if requested
1282 * Then pass the packet to ether_input_oncpu().
1284 * This function should be used by pseudo interface (e.g. vlan(4)),
1285 * when it tries to claim that the packet is received by it.
1291 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1293 /* Discard packet if interface is not up */
1294 if (!(ifp->if_flags & IFF_UP)) {
1300 * Change receiving interface. The bridge will often pass a flag to
1301 * ask that this not be done so ARPs get applied to the correct
1304 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1305 m->m_pkthdr.rcvif == NULL) {
1306 m->m_pkthdr.rcvif = ifp;
1309 /* Update statistics */
1310 IFNET_STAT_INC(ifp, ipackets, 1);
1311 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1312 if (m->m_flags & (M_MCAST | M_BCAST))
1313 IFNET_STAT_INC(ifp, imcasts, 1);
1315 if (reinput_flags & REINPUT_RUNBPF)
1318 ether_input_oncpu(ifp, m);
1321 static __inline boolean_t
1322 ether_vlancheck(struct mbuf **m0)
1324 struct mbuf *m = *m0;
1325 struct ether_header *eh;
1326 uint16_t ether_type;
1328 eh = mtod(m, struct ether_header *);
1329 ether_type = ntohs(eh->ether_type);
1331 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1333 * Extract vlan tag if hardware does not do it for us
1335 vlan_ether_decap(&m);
1339 eh = mtod(m, struct ether_header *);
1340 ether_type = ntohs(eh->ether_type);
1343 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1345 * To prevent possible dangerous recursion,
1346 * we don't do vlan-in-vlan
1348 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1351 KKASSERT(ether_type != ETHERTYPE_VLAN);
1353 m->m_flags |= M_ETHER_VLANCHECKED;
1364 ether_input_handler(netmsg_t nmsg)
1366 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1367 struct ether_header *eh;
1374 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1375 if (!ether_vlancheck(&m)) {
1376 KKASSERT(m == NULL);
1380 if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH) ||
1381 __predict_false(ether_input_ckhash)) {
1385 * Need to verify the hash supplied by the hardware
1386 * which could be wrong.
1388 m->m_flags &= ~(M_HASH | M_CKHASH);
1389 isr = ether_characterize(&m);
1392 KKASSERT(m->m_flags & M_HASH);
1394 if (m->m_pkthdr.hash != mycpuid) {
1396 * Wrong hardware supplied hash; redispatch
1398 ether_dispatch(isr, m);
1399 if (__predict_false(ether_input_ckhash))
1400 atomic_add_long(ðer_input_wronghwhash, 1);
1404 ifp = m->m_pkthdr.rcvif;
1406 eh = mtod(m, struct ether_header *);
1407 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1408 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1409 ifp->if_addrlen) == 0)
1410 m->m_flags |= M_BCAST;
1412 m->m_flags |= M_MCAST;
1413 IFNET_STAT_INC(ifp, imcasts, 1);
1416 ether_input_oncpu(ifp, m);
1420 * Send the packet to the target msgport
1422 * At this point the packet had better be characterized (M_HASH set),
1423 * so we know which cpu to send it to.
1426 ether_dispatch(int isr, struct mbuf *m)
1428 struct netmsg_packet *pmsg;
1430 KKASSERT(m->m_flags & M_HASH);
1431 pmsg = &m->m_hdr.mh_netmsg;
1432 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1433 0, ether_input_handler);
1434 pmsg->nm_packet = m;
1435 pmsg->base.lmsg.u.ms_result = isr;
1437 logether(disp_beg, NULL);
1438 lwkt_sendmsg(netisr_cpuport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1439 logether(disp_end, NULL);
1443 * Process a received Ethernet packet.
1445 * The ethernet header is assumed to be in the mbuf so the caller
1446 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1447 * bytes in the first mbuf.
1450 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1456 /* Discard packet if interface is not up */
1457 if (!(ifp->if_flags & IFF_UP)) {
1462 if (m->m_len < sizeof(struct ether_header)) {
1463 /* XXX error in the caller. */
1468 m->m_pkthdr.rcvif = ifp;
1470 logether(pkt_beg, ifp);
1472 ETHER_BPF_MTAP(ifp, m);
1474 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1476 if (ifp->if_flags & IFF_MONITOR) {
1477 struct ether_header *eh;
1479 eh = mtod(m, struct ether_header *);
1480 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1481 IFNET_STAT_INC(ifp, imcasts, 1);
1484 * Interface marked for monitoring; discard packet.
1488 logether(pkt_end, ifp);
1493 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1494 * we can dispatch it immediately without further inspection.
1496 if (pi != NULL && (m->m_flags & M_HASH)) {
1498 atomic_add_long(ðer_pktinfo_try, 1);
1500 netisr_hashcheck(pi->pi_netisr, m, pi);
1501 if (m->m_flags & M_HASH) {
1502 ether_dispatch(pi->pi_netisr, m);
1504 atomic_add_long(ðer_pktinfo_hit, 1);
1506 logether(pkt_end, ifp);
1511 else if (ifp->if_capenable & IFCAP_RSS) {
1513 atomic_add_long(ðer_rss_nopi, 1);
1515 atomic_add_long(ðer_rss_nohash, 1);
1520 * Packet hash will be recalculated by software, so clear
1521 * the M_HASH and M_CKHASH flag set by the driver; the hash
1522 * value calculated by the hardware may not be exactly what
1525 m->m_flags &= ~(M_HASH | M_CKHASH);
1527 if (!ether_vlancheck(&m)) {
1528 KKASSERT(m == NULL);
1529 logether(pkt_end, ifp);
1533 isr = ether_characterize(&m);
1535 logether(pkt_end, ifp);
1540 * Finally dispatch it
1542 ether_dispatch(isr, m);
1544 logether(pkt_end, ifp);
1548 ether_characterize(struct mbuf **m0)
1550 struct mbuf *m = *m0;
1551 struct ether_header *eh;
1552 uint16_t ether_type;
1555 eh = mtod(m, struct ether_header *);
1556 ether_type = ntohs(eh->ether_type);
1559 * Map ether type to netisr id.
1561 switch (ether_type) {
1573 case ETHERTYPE_IPV6:
1585 case ETHERTYPE_MPLS:
1586 case ETHERTYPE_MPLS_MCAST:
1587 m->m_flags |= M_MPLSLABELED;
1594 * NETISR_MAX is an invalid value; it is chosen to let
1595 * netisr_characterize() know that we have no clear
1596 * idea where this packet should go.
1603 * Ask the isr to characterize the packet since we couldn't.
1604 * This is an attempt to optimally get us onto the correct protocol
1607 netisr_characterize(isr, &m, sizeof(struct ether_header));
1614 ether_demux_handler(netmsg_t nmsg)
1616 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1622 ifp = m->m_pkthdr.rcvif;
1624 ether_demux_oncpu(ifp, m);
1628 ether_demux(struct mbuf *m)
1630 struct netmsg_packet *pmsg;
1633 isr = ether_characterize(&m);
1637 KKASSERT(m->m_flags & M_HASH);
1638 pmsg = &m->m_hdr.mh_netmsg;
1639 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1640 0, ether_demux_handler);
1641 pmsg->nm_packet = m;
1642 pmsg->base.lmsg.u.ms_result = isr;
1644 lwkt_sendmsg(netisr_cpuport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1648 ether_tso_pullup(struct mbuf **mp, int *hoff0, struct ip **ip, int *iphlen,
1649 struct tcphdr **th, int *thoff)
1651 struct mbuf *m = *mp;
1652 struct ether_header *eh;
1656 KASSERT(M_WRITABLE(m), ("not writable"));
1658 hoff = ETHER_HDR_LEN;
1659 if (m->m_len < hoff) {
1660 m = m_pullup(m, hoff);
1664 eh = mtod(m, struct ether_header *);
1665 type = eh->ether_type;
1667 if (type == htons(ETHERTYPE_VLAN)) {
1668 struct ether_vlan_header *evh;
1670 hoff += EVL_ENCAPLEN;
1671 if (m->m_len < hoff) {
1672 m = m_pullup(m, hoff);
1676 evh = mtod(m, struct ether_vlan_header *);
1677 type = evh->evl_proto;
1679 KASSERT(type == htons(ETHERTYPE_IP), ("not IP %d", ntohs(type)));
1683 return tcp_tso_pullup(mp, hoff, ip, iphlen, th, thoff);
1693 kether_aton(const char *macstr, u_char *addr)
1695 unsigned int o0, o1, o2, o3, o4, o5;
1698 if (macstr == NULL || addr == NULL)
1701 n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
1717 kether_ntoa(const u_char *addr, char *buf)
1719 int len = ETHER_ADDRSTRLEN + 1;
1722 n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0],
1723 addr[1], addr[2], addr[3], addr[4], addr[5]);
1731 MODULE_VERSION(ether, 1);