2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.96 2008/11/22 04:00:53 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/msgport.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/if_llc.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
72 #include <net/ethernet.h>
73 #include <net/vlan/if_vlan_ether.h>
74 #include <net/netmsg2.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
85 #include <netinet6/nd6.h>
89 #include <netinet/ip_carp.h>
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 short *tp, int *hlen);
101 #include <netproto/atalk/at.h>
102 #include <netproto/atalk/at_var.h>
103 #include <netproto/atalk/at_extern.h>
105 #define llc_snap_org_code llc_un.type_snap.org_code
106 #define llc_snap_ether_type llc_un.type_snap.ether_type
108 extern u_char at_org_code[3];
109 extern u_char aarp_org_code[3];
110 #endif /* NETATALK */
113 #include <netproto/mpls/mpls.h>
116 /* netgraph node hooks for ng_ether(4) */
117 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
118 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
119 struct mbuf *m, const struct ether_header *eh);
120 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
121 void (*ng_ether_attach_p)(struct ifnet *ifp);
122 void (*ng_ether_detach_p)(struct ifnet *ifp);
124 void (*vlan_input_p)(struct mbuf *);
126 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
128 static void ether_restore_header(struct mbuf **, const struct ether_header *,
129 const struct ether_header *);
130 static int ether_characterize(struct mbuf **);
135 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
136 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
137 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
138 struct ifnet *(*bridge_interface_p)(void *if_bridge);
140 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
143 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
147 #define gotoerr(e) do { error = (e); goto bad; } while (0)
148 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
150 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
152 const struct ether_header *eh);
154 static int ether_ipfw;
155 static u_long ether_restore_hdr;
156 static u_long ether_prepend_hdr;
157 static int ether_debug;
160 static u_long ether_pktinfo_try;
161 static u_long ether_pktinfo_hit;
162 static u_long ether_rss_nopi;
163 static u_long ether_rss_nohash;
166 SYSCTL_DECL(_net_link);
167 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
168 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
169 ðer_debug, 0, "Ether debug");
170 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
171 ðer_ipfw, 0, "Pass ether pkts through firewall");
172 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
173 ðer_restore_hdr, 0, "# of ether header restoration");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
175 ðer_prepend_hdr, 0,
176 "# of ether header restoration which prepends mbuf");
178 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
179 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
180 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
181 ðer_rss_nohash, 0, "# of packets do not have hash");
182 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
183 ðer_pktinfo_try, 0,
184 "# of tries to find packets' msgport using pktinfo");
185 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
186 ðer_pktinfo_hit, 0,
187 "# of packets whose msgport are found using pktinfo");
190 #define ETHER_KTR_STR "ifp=%p"
191 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
193 #define KTR_ETHERNET KTR_ALL
195 KTR_INFO_MASTER(ether);
196 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
197 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
198 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
199 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
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 eh = mtod(m, struct ether_header *);
231 edst = eh->ether_dhost;
234 * Fill in the destination ethernet address and frame type.
236 switch (dst->sa_family) {
239 if (!arpresolve(ifp, rt, m, dst, edst))
240 return (0); /* if not yet resolved */
242 if (m->m_flags & M_MPLSLABELED)
243 eh->ether_type = htons(ETHERTYPE_MPLS);
246 eh->ether_type = htons(ETHERTYPE_IP);
251 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
252 return (0); /* Something bad happenned. */
253 eh->ether_type = htons(ETHERTYPE_IPV6);
258 if (ef_outputp != NULL) {
260 * Hold BGL and recheck ef_outputp
263 if (ef_outputp != NULL) {
264 error = ef_outputp(ifp, &m, dst,
265 &eh->ether_type, &hlen);
274 eh->ether_type = htons(ETHERTYPE_IPX);
275 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
276 edst, ETHER_ADDR_LEN);
281 struct at_ifaddr *aa;
288 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
294 * In the phase 2 case, need to prepend an mbuf for
295 * the llc header. Since we must preserve the value
296 * of m, which is passed to us by value, we m_copy()
297 * the first mbuf, and use it for our llc header.
299 if (aa->aa_flags & AFA_PHASE2) {
302 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
303 eh = mtod(m, struct ether_header *);
304 edst = eh->ether_dhost;
305 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
306 llc.llc_control = LLC_UI;
307 bcopy(at_org_code, llc.llc_snap_org_code,
309 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
311 mtod(m, caddr_t) + sizeof(struct ether_header),
313 eh->ether_type = htons(m->m_pkthdr.len);
314 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
316 eh->ether_type = htons(ETHERTYPE_AT);
318 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
327 case pseudo_AF_HDRCMPLT:
329 loop_copy = -1; /* if this is for us, don't do it */
330 deh = (struct ether_header *)dst->sa_data;
331 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
332 eh->ether_type = deh->ether_type;
336 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
337 gotoerr(EAFNOSUPPORT);
340 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
341 memcpy(eh->ether_shost,
342 ((struct ether_header *)dst->sa_data)->ether_shost,
345 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
348 * Bridges require special output handling.
350 if (ifp->if_bridge) {
351 KASSERT(bridge_output_p != NULL,
352 ("%s: if_bridge not loaded!", __func__));
353 return bridge_output_p(ifp, m);
357 * If a simplex interface, and the packet is being sent to our
358 * Ethernet address or a broadcast address, loopback a copy.
359 * XXX To make a simplex device behave exactly like a duplex
360 * device, we should copy in the case of sending to our own
361 * ethernet address (thus letting the original actually appear
362 * on the wire). However, we don't do that here for security
363 * reasons and compatibility with the original behavior.
365 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
368 if (m->m_pkthdr.csum_flags & CSUM_IP)
369 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
370 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
371 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
372 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
375 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
376 n->m_pkthdr.csum_flags |= csum_flags;
377 if (csum_flags & CSUM_DATA_VALID)
378 n->m_pkthdr.csum_data = 0xffff;
379 if_simloop(ifp, n, dst->sa_family, hlen);
382 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
383 ETHER_ADDR_LEN) == 0) {
384 m->m_pkthdr.csum_flags |= csum_flags;
385 if (csum_flags & CSUM_DATA_VALID)
386 m->m_pkthdr.csum_data = 0xffff;
387 if_simloop(ifp, m, dst->sa_family, hlen);
388 return (0); /* XXX */
395 * Hold BGL and recheck ifp->if_carp
398 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
407 /* Handle ng_ether(4) processing, if any */
408 if (ng_ether_output_p != NULL) {
410 * Hold BGL and recheck ng_ether_output_p
413 if (ng_ether_output_p != NULL) {
414 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
426 /* Continue with link-layer output */
427 return ether_output_frame(ifp, m);
435 * Returns the bridge interface an ifp is associated
438 * Only call if ifp->if_bridge != NULL.
441 ether_bridge_interface(struct ifnet *ifp)
443 if (bridge_interface_p)
444 return(bridge_interface_p(ifp->if_bridge));
449 * Ethernet link layer output routine to send a raw frame to the device.
451 * This assumes that the 14 byte Ethernet header is present and contiguous
455 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
457 struct ip_fw *rule = NULL;
459 struct altq_pktattr pktattr;
461 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
463 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
466 /* Extract info from dummynet tag */
467 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
468 KKASSERT(mtag != NULL);
469 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
470 KKASSERT(rule != NULL);
472 m_tag_delete(m, mtag);
473 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
476 if (ifq_is_enabled(&ifp->if_snd))
477 altq_etherclassify(&ifp->if_snd, m, &pktattr);
479 if (IPFW_LOADED && ether_ipfw != 0) {
480 struct ether_header save_eh, *eh;
482 eh = mtod(m, struct ether_header *);
484 m_adj(m, ETHER_HDR_LEN);
485 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
489 return ENOBUFS; /* pkt dropped */
491 return 0; /* consumed e.g. in a pipe */
494 /* packet was ok, restore the ethernet header */
495 ether_restore_header(&m, eh, &save_eh);
504 * Queue message on interface, update output statistics if
505 * successful, and start output if interface not yet active.
507 error = ifq_dispatch(ifp, m, &pktattr);
512 * ipfw processing for ethernet packets (in and out).
513 * The second parameter is NULL from ether_demux(), and ifp from
514 * ether_output_frame().
517 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
518 const struct ether_header *eh)
520 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
521 struct ip_fw_args args;
526 if (*rule != NULL && fw_one_pass)
527 return TRUE; /* dummynet packet, already partially processed */
530 * I need some amount of data to be contiguous.
532 i = min((*m0)->m_pkthdr.len, max_protohdr);
533 if ((*m0)->m_len < i) {
534 *m0 = m_pullup(*m0, i);
542 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
543 m_tag_delete(*m0, mtag);
544 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
545 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
546 KKASSERT(mtag != NULL);
547 m_tag_delete(*m0, mtag);
548 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
551 args.m = *m0; /* the packet we are looking at */
552 args.oif = dst; /* destination, if any */
553 args.rule = *rule; /* matching rule to restart */
554 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
555 i = ip_fw_chk_ptr(&args);
570 * XXX at some point add support for divert/forward actions.
571 * If none of the above matches, we have to drop the pkt.
577 * Pass the pkt to dummynet, which consumes it.
579 m = *m0; /* pass the original to dummynet */
580 *m0 = NULL; /* and nothing back to the caller */
582 ether_restore_header(&m, eh, &save_eh);
586 ip_fw_dn_io_ptr(m, args.cookie,
587 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
592 panic("unknown ipfw return value: %d\n", i);
597 ether_input(struct ifnet *ifp, struct mbuf *m)
599 ether_input_chain(ifp, m, NULL, NULL);
603 * Perform common duties while attaching to interface list
606 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
608 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
613 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
614 lwkt_serialize_t serializer)
616 struct sockaddr_dl *sdl;
618 ifp->if_type = IFT_ETHER;
619 ifp->if_addrlen = ETHER_ADDR_LEN;
620 ifp->if_hdrlen = ETHER_HDR_LEN;
621 if_attach(ifp, serializer);
622 ifp->if_mtu = ETHERMTU;
623 if (ifp->if_baudrate == 0)
624 ifp->if_baudrate = 10000000;
625 ifp->if_output = ether_output;
626 ifp->if_input = ether_input;
627 ifp->if_resolvemulti = ether_resolvemulti;
628 ifp->if_broadcastaddr = etherbroadcastaddr;
629 sdl = IF_LLSOCKADDR(ifp);
630 sdl->sdl_type = IFT_ETHER;
631 sdl->sdl_alen = ifp->if_addrlen;
632 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
634 * XXX Keep the current drivers happy.
635 * XXX Remove once all drivers have been cleaned up
637 if (lla != IFP2AC(ifp)->ac_enaddr)
638 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
639 bpfattach(ifp, dlt, hdrlen);
640 if (ng_ether_attach_p != NULL)
641 (*ng_ether_attach_p)(ifp);
643 if_printf(ifp, "MAC address: %6D\n", lla, ":");
647 * Perform common duties while detaching an Ethernet interface
650 ether_ifdetach(struct ifnet *ifp)
654 if (ng_ether_detach_p != NULL)
655 (*ng_ether_detach_p)(ifp);
661 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
663 struct ifaddr *ifa = (struct ifaddr *) data;
664 struct ifreq *ifr = (struct ifreq *) data;
667 #define IF_INIT(ifp) \
669 if (((ifp)->if_flags & IFF_UP) == 0) { \
670 (ifp)->if_flags |= IFF_UP; \
671 (ifp)->if_init((ifp)->if_softc); \
675 ASSERT_IFNET_SERIALIZED_ALL(ifp);
679 switch (ifa->ifa_addr->sa_family) {
682 IF_INIT(ifp); /* before arpwhohas */
683 arp_ifinit(ifp, ifa);
688 * XXX - This code is probably wrong
692 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
693 struct arpcom *ac = IFP2AC(ifp);
695 if (ipx_nullhost(*ina))
696 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
698 bcopy(ina->x_host.c_host, ac->ac_enaddr,
699 sizeof ac->ac_enaddr);
701 IF_INIT(ifp); /* Set new address. */
712 bcopy(IFP2AC(ifp)->ac_enaddr,
713 ((struct sockaddr *)ifr->ifr_data)->sa_data,
719 * Set the interface MTU.
721 if (ifr->ifr_mtu > ETHERMTU) {
724 ifp->if_mtu = ifr->ifr_mtu;
739 struct sockaddr **llsa,
742 struct sockaddr_dl *sdl;
743 struct sockaddr_in *sin;
745 struct sockaddr_in6 *sin6;
749 switch(sa->sa_family) {
752 * No mapping needed. Just check that it's a valid MC address.
754 sdl = (struct sockaddr_dl *)sa;
755 e_addr = LLADDR(sdl);
756 if ((e_addr[0] & 1) != 1)
757 return EADDRNOTAVAIL;
763 sin = (struct sockaddr_in *)sa;
764 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
765 return EADDRNOTAVAIL;
766 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
768 sdl->sdl_len = sizeof *sdl;
769 sdl->sdl_family = AF_LINK;
770 sdl->sdl_index = ifp->if_index;
771 sdl->sdl_type = IFT_ETHER;
772 sdl->sdl_alen = ETHER_ADDR_LEN;
773 e_addr = LLADDR(sdl);
774 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
775 *llsa = (struct sockaddr *)sdl;
780 sin6 = (struct sockaddr_in6 *)sa;
781 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
783 * An IP6 address of 0 means listen to all
784 * of the Ethernet multicast address used for IP6.
785 * (This is used for multicast routers.)
787 ifp->if_flags |= IFF_ALLMULTI;
791 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
792 return EADDRNOTAVAIL;
793 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
795 sdl->sdl_len = sizeof *sdl;
796 sdl->sdl_family = AF_LINK;
797 sdl->sdl_index = ifp->if_index;
798 sdl->sdl_type = IFT_ETHER;
799 sdl->sdl_alen = ETHER_ADDR_LEN;
800 e_addr = LLADDR(sdl);
801 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
802 *llsa = (struct sockaddr *)sdl;
808 * Well, the text isn't quite right, but it's the name
817 * This is for reference. We have a table-driven version
818 * of the little-endian crc32 generator, which is faster
819 * than the double-loop.
822 ether_crc32_le(const uint8_t *buf, size_t len)
824 uint32_t c, crc, carry;
827 crc = 0xffffffffU; /* initial value */
829 for (i = 0; i < len; i++) {
831 for (j = 0; j < 8; j++) {
832 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
836 crc = (crc ^ ETHER_CRC_POLY_LE);
844 ether_crc32_le(const uint8_t *buf, size_t len)
846 static const uint32_t crctab[] = {
847 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
848 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
849 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
850 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
855 crc = 0xffffffffU; /* initial value */
857 for (i = 0; i < len; i++) {
859 crc = (crc >> 4) ^ crctab[crc & 0xf];
860 crc = (crc >> 4) ^ crctab[crc & 0xf];
868 ether_crc32_be(const uint8_t *buf, size_t len)
870 uint32_t c, crc, carry;
873 crc = 0xffffffffU; /* initial value */
875 for (i = 0; i < len; i++) {
877 for (j = 0; j < 8; j++) {
878 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
882 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
890 * find the size of ethernet header, and call classifier
893 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
894 struct altq_pktattr *pktattr)
896 struct ether_header *eh;
898 int hlen, af, hdrsize;
901 hlen = sizeof(struct ether_header);
902 eh = mtod(m, struct ether_header *);
904 ether_type = ntohs(eh->ether_type);
905 if (ether_type < ETHERMTU) {
907 struct llc *llc = (struct llc *)(eh + 1);
910 if (m->m_len < hlen ||
911 llc->llc_dsap != LLC_SNAP_LSAP ||
912 llc->llc_ssap != LLC_SNAP_LSAP ||
913 llc->llc_control != LLC_UI)
914 goto bad; /* not snap! */
916 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
919 if (ether_type == ETHERTYPE_IP) {
921 hdrsize = 20; /* sizeof(struct ip) */
923 } else if (ether_type == ETHERTYPE_IPV6) {
925 hdrsize = 40; /* sizeof(struct ip6_hdr) */
930 while (m->m_len <= hlen) {
934 hdr = m->m_data + hlen;
935 if (m->m_len < hlen + hdrsize) {
937 * ip header is not in a single mbuf. this should not
938 * happen in the current code.
939 * (todo: use m_pulldown in the future)
945 ifq_classify(ifq, m, af, pktattr);
952 pktattr->pattr_class = NULL;
953 pktattr->pattr_hdr = NULL;
954 pktattr->pattr_af = AF_UNSPEC;
958 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
959 const struct ether_header *save_eh)
961 struct mbuf *m = *m0;
966 * Prepend the header, optimize for the common case of
967 * eh pointing into the mbuf.
969 if ((const void *)(eh + 1) == (void *)m->m_data) {
970 m->m_data -= ETHER_HDR_LEN;
971 m->m_len += ETHER_HDR_LEN;
972 m->m_pkthdr.len += ETHER_HDR_LEN;
976 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
978 bcopy(save_eh, mtod(m, struct ether_header *),
986 ether_input_ipifunc(void *arg)
988 struct mbuf *m, *next;
989 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
995 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
1001 ether_input_dispatch(struct mbuf_chain *chain)
1006 logether(disp_beg, NULL);
1007 for (i = 0; i < ncpus; ++i) {
1008 if (chain[i].mc_head != NULL) {
1009 lwkt_send_ipiq(globaldata_find(i),
1010 ether_input_ipifunc, chain[i].mc_head);
1014 logether(disp_beg, NULL);
1015 if (chain->mc_head != NULL)
1016 ether_input_ipifunc(chain->mc_head);
1018 logether(disp_end, NULL);
1022 ether_input_chain_init(struct mbuf_chain *chain)
1027 for (i = 0; i < ncpus; ++i)
1028 chain[i].mc_head = chain[i].mc_tail = NULL;
1030 chain->mc_head = chain->mc_tail = NULL;
1035 * Upper layer processing for a received Ethernet packet.
1038 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1040 struct ether_header *eh;
1041 int isr, discard = 0;
1043 struct ip_fw *rule = NULL;
1049 KASSERT(m->m_len >= ETHER_HDR_LEN,
1050 ("ether header is no contiguous!\n"));
1052 eh = mtod(m, struct ether_header *);
1054 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1057 /* Extract info from dummynet tag */
1058 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1059 KKASSERT(mtag != NULL);
1060 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1061 KKASSERT(rule != NULL);
1063 m_tag_delete(m, mtag);
1064 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1066 /* packet is passing the second time */
1072 * XXX: Okay, we need to call carp_forus() and - if it is for
1073 * us jump over code that does the normal check
1074 * "ac_enaddr == ether_dhost". The check sequence is a bit
1075 * different from OpenBSD, so we jump over as few code as
1076 * possible, to catch _all_ sanity checks. This needs
1077 * evaluation, to see if the carp ether_dhost values break any
1082 * Hold BGL and recheck ifp->if_carp
1085 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1094 * We got a packet which was unicast to a different Ethernet
1095 * address. If the driver is working properly, then this
1096 * situation can only happen when the interface is in
1097 * promiscuous mode. We defer the packet discarding until the
1098 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1101 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1102 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1103 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1104 if (ether_debug & 1) {
1105 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1106 "%02x:%02x:%02x:%02x:%02x:%02x "
1107 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1121 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1122 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1123 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1124 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1125 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1126 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1129 if ((ether_debug & 2) == 0)
1134 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1135 struct ether_header save_eh = *eh;
1137 /* XXX old crufty stuff, needs to be removed */
1138 m_adj(m, sizeof(struct ether_header));
1140 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1145 ether_restore_header(&m, eh, &save_eh);
1148 eh = mtod(m, struct ether_header *);
1151 ether_type = ntohs(eh->ether_type);
1152 KKASSERT(ether_type != ETHERTYPE_VLAN);
1154 if (m->m_flags & M_VLANTAG) {
1155 void (*vlan_input_func)(struct mbuf *);
1157 vlan_input_func = vlan_input_p;
1158 if (vlan_input_func != NULL) {
1161 m->m_pkthdr.rcvif->if_noproto++;
1168 * If we have been asked to discard this packet
1169 * (e.g. not for us), drop it before entering
1178 * Clear protocol specific flags,
1179 * before entering the upper layer.
1181 m->m_flags &= ~M_ETHER_FLAGS;
1183 /* Strip ethernet header. */
1184 m_adj(m, sizeof(struct ether_header));
1186 switch (ether_type) {
1189 if ((m->m_flags & M_LENCHECKED) == 0) {
1190 if (!ip_lengthcheck(&m, 0))
1193 if (ipflow_fastforward(m))
1199 if (ifp->if_flags & IFF_NOARP) {
1200 /* Discard packet if ARP is disabled on interface */
1209 case ETHERTYPE_IPV6:
1218 * Hold BGL and recheck ef_inputp
1221 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1233 isr = NETISR_ATALK1;
1235 case ETHERTYPE_AARP:
1241 case ETHERTYPE_MPLS:
1242 case ETHERTYPE_MPLS_MCAST:
1243 /* Should have been set by ether_input_chain(). */
1244 KKASSERT(m->m_flags & M_MPLSLABELED);
1251 * The accurate msgport is not determined before
1252 * we reach here, so redo the dispatching
1257 * Hold BGL and recheck ef_inputp
1260 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1268 if (ether_type > ETHERMTU)
1270 l = mtod(m, struct llc *);
1271 if (l->llc_dsap == LLC_SNAP_LSAP &&
1272 l->llc_ssap == LLC_SNAP_LSAP &&
1273 l->llc_control == LLC_UI) {
1274 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1275 sizeof at_org_code) == 0 &&
1276 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1277 m_adj(m, sizeof(struct llc));
1278 isr = NETISR_ATALK2;
1281 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1282 sizeof aarp_org_code) == 0 &&
1283 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1284 m_adj(m, sizeof(struct llc));
1291 if (ng_ether_input_orphan_p != NULL) {
1293 * Hold BGL and recheck ng_ether_input_orphan_p
1296 if (ng_ether_input_orphan_p != NULL) {
1297 ng_ether_input_orphan_p(ifp, m, eh);
1307 netisr_queue(isr, m);
1311 * First we perform any link layer operations, then continue to the
1312 * upper layers with ether_demux_oncpu().
1315 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1317 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1319 * Receiving interface's flags are changed, when this
1320 * packet is waiting for processing; discard it.
1327 * Tap the packet off here for a bridge. bridge_input()
1328 * will return NULL if it has consumed the packet, otherwise
1329 * it gets processed as normal. Note that bridge_input()
1330 * will always return the original packet if we need to
1331 * process it locally.
1333 if (ifp->if_bridge) {
1334 KASSERT(bridge_input_p != NULL,
1335 ("%s: if_bridge not loaded!", __func__));
1337 if(m->m_flags & M_ETHER_BRIDGED) {
1338 m->m_flags &= ~M_ETHER_BRIDGED;
1340 m = bridge_input_p(ifp, m);
1344 KASSERT(ifp == m->m_pkthdr.rcvif,
1345 ("bridge_input_p changed rcvif\n"));
1349 /* Handle ng_ether(4) processing, if any */
1350 if (ng_ether_input_p != NULL) {
1352 * Hold BGL and recheck ng_ether_input_p
1355 if (ng_ether_input_p != NULL)
1356 ng_ether_input_p(ifp, &m);
1363 /* Continue with upper layer processing */
1364 ether_demux_oncpu(ifp, m);
1368 * Perform certain functions of ether_input_chain():
1370 * - Update statistics
1371 * - Run bpf(4) tap if requested
1372 * Then pass the packet to ether_input_oncpu().
1374 * This function should be used by pseudo interface (e.g. vlan(4)),
1375 * when it tries to claim that the packet is received by it.
1381 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1383 /* Discard packet if interface is not up */
1384 if (!(ifp->if_flags & IFF_UP)) {
1390 * Change receiving interface. The bridge will often pass a flag to
1391 * ask that this not be done so ARPs get applied to the correct
1394 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1395 m->m_pkthdr.rcvif == NULL) {
1396 m->m_pkthdr.rcvif = ifp;
1399 /* Update statistics */
1401 ifp->if_ibytes += m->m_pkthdr.len;
1402 if (m->m_flags & (M_MCAST | M_BCAST))
1405 if (reinput_flags & REINPUT_RUNBPF)
1408 ether_input_oncpu(ifp, m);
1411 static __inline boolean_t
1412 ether_vlancheck(struct mbuf **m0)
1414 struct mbuf *m = *m0;
1415 struct ether_header *eh;
1416 uint16_t ether_type;
1418 eh = mtod(m, struct ether_header *);
1419 ether_type = ntohs(eh->ether_type);
1421 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1423 * Extract vlan tag if hardware does not do it for us
1425 vlan_ether_decap(&m);
1429 eh = mtod(m, struct ether_header *);
1430 ether_type = ntohs(eh->ether_type);
1433 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1435 * To prevent possible dangerous recursion,
1436 * we don't do vlan-in-vlan
1438 m->m_pkthdr.rcvif->if_noproto++;
1441 KKASSERT(ether_type != ETHERTYPE_VLAN);
1443 m->m_flags |= M_ETHER_VLANCHECKED;
1454 ether_input_handler(netmsg_t nmsg)
1456 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1457 struct ether_header *eh;
1463 ifp = m->m_pkthdr.rcvif;
1465 eh = mtod(m, struct ether_header *);
1466 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1467 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1468 ifp->if_addrlen) == 0)
1469 m->m_flags |= M_BCAST;
1471 m->m_flags |= M_MCAST;
1475 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1476 if (!ether_vlancheck(&m)) {
1477 KKASSERT(m == NULL);
1482 ether_input_oncpu(ifp, m);
1486 * Send the packet to the target msgport or queue it into 'chain'.
1488 * At this point the packet had better be characterized (M_HASH set),
1489 * so we know which cpu to send it to.
1492 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1494 struct netmsg_packet *pmsg;
1496 KKASSERT(m->m_flags & M_HASH);
1497 pmsg = &m->m_hdr.mh_netmsg;
1498 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1499 0, ether_input_handler);
1500 pmsg->nm_packet = m;
1501 pmsg->base.lmsg.u.ms_result = isr;
1503 if (chain != NULL) {
1504 int cpuid = m->m_pkthdr.hash;
1505 struct mbuf_chain *c;
1508 if (c->mc_head == NULL) {
1509 c->mc_head = c->mc_tail = m;
1511 c->mc_tail->m_nextpkt = m;
1514 m->m_nextpkt = NULL;
1516 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1521 * Process a received Ethernet packet.
1523 * The ethernet header is assumed to be in the mbuf so the caller
1524 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1525 * bytes in the first mbuf.
1527 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1528 * immediately. This situation happens when ether_input_chain is
1529 * accessed through ifnet.if_input.
1531 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1532 * bucket indexed by the target msgport's cpuid and the target msgport
1533 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1534 * must initialize 'chain' by calling ether_input_chain_init().
1535 * ether_input_dispatch must be called later to send ether frames
1536 * queued on 'chain' to their target msgport.
1539 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1540 struct mbuf_chain *chain)
1546 /* Discard packet if interface is not up */
1547 if (!(ifp->if_flags & IFF_UP)) {
1552 if (m->m_len < sizeof(struct ether_header)) {
1553 /* XXX error in the caller. */
1558 m->m_pkthdr.rcvif = ifp;
1560 logether(chain_beg, ifp);
1562 ETHER_BPF_MTAP(ifp, m);
1564 ifp->if_ibytes += m->m_pkthdr.len;
1566 if (ifp->if_flags & IFF_MONITOR) {
1567 struct ether_header *eh;
1569 eh = mtod(m, struct ether_header *);
1570 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1574 * Interface marked for monitoring; discard packet.
1578 logether(chain_end, ifp);
1583 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1584 * we can dispatch it immediately without further inspection.
1586 if (pi != NULL && (m->m_flags & M_HASH)) {
1588 ether_pktinfo_try++;
1590 netisr_hashcheck(pi->pi_netisr, m, pi);
1591 if (m->m_flags & M_HASH) {
1592 ether_dispatch(pi->pi_netisr, m, chain);
1594 ether_pktinfo_hit++;
1596 logether(chain_end, ifp);
1601 else if (ifp->if_capenable & IFCAP_RSS) {
1610 * Packet hash will be recalculated by software,
1611 * so clear the M_HASH flag set by the driver;
1612 * the hash value calculated by the hardware may
1613 * not be exactly what we want.
1615 m->m_flags &= ~M_HASH;
1617 if (!ether_vlancheck(&m)) {
1618 KKASSERT(m == NULL);
1619 logether(chain_end, ifp);
1623 isr = ether_characterize(&m);
1625 logether(chain_end, ifp);
1630 * Finally dispatch it
1632 ether_dispatch(isr, m, chain);
1634 logether(chain_end, ifp);
1638 ether_characterize(struct mbuf **m0)
1640 struct mbuf *m = *m0;
1641 struct ether_header *eh;
1642 uint16_t ether_type;
1645 eh = mtod(m, struct ether_header *);
1646 ether_type = ntohs(eh->ether_type);
1649 * Map ether type to netisr id.
1651 switch (ether_type) {
1663 case ETHERTYPE_IPV6:
1676 isr = NETISR_ATALK1;
1678 case ETHERTYPE_AARP:
1684 case ETHERTYPE_MPLS:
1685 case ETHERTYPE_MPLS_MCAST:
1686 m->m_flags |= M_MPLSLABELED;
1693 * NETISR_MAX is an invalid value; it is chosen to let
1694 * netisr_characterize() know that we have no clear
1695 * idea where this packet should go.
1702 * Ask the isr to characterize the packet since we couldn't.
1703 * This is an attempt to optimally get us onto the correct protocol
1706 netisr_characterize(isr, &m, sizeof(struct ether_header));
1713 ether_demux_handler(netmsg_t nmsg)
1715 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1721 ifp = m->m_pkthdr.rcvif;
1723 ether_demux_oncpu(ifp, m);
1727 ether_demux(struct mbuf *m)
1729 struct netmsg_packet *pmsg;
1732 isr = ether_characterize(&m);
1736 KKASSERT(m->m_flags & M_HASH);
1737 pmsg = &m->m_hdr.mh_netmsg;
1738 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1739 0, ether_demux_handler);
1740 pmsg->nm_packet = m;
1741 pmsg->base.lmsg.u.ms_result = isr;
1743 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1746 MODULE_VERSION(ether, 1);