2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.96 2008/11/22 04:00:53 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/msgport.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/if_llc.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
72 #include <net/ethernet.h>
73 #include <net/vlan/if_vlan_ether.h>
74 #include <net/netmsg2.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
85 #include <netinet6/nd6.h>
89 #include <netinet/ip_carp.h>
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 short *tp, int *hlen);
101 #include <netproto/atalk/at.h>
102 #include <netproto/atalk/at_var.h>
103 #include <netproto/atalk/at_extern.h>
105 #define llc_snap_org_code llc_un.type_snap.org_code
106 #define llc_snap_ether_type llc_un.type_snap.ether_type
108 extern u_char at_org_code[3];
109 extern u_char aarp_org_code[3];
110 #endif /* NETATALK */
113 #include <netproto/mpls/mpls.h>
116 /* netgraph node hooks for ng_ether(4) */
117 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
118 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
119 struct mbuf *m, const struct ether_header *eh);
120 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
121 void (*ng_ether_attach_p)(struct ifnet *ifp);
122 void (*ng_ether_detach_p)(struct ifnet *ifp);
124 void (*vlan_input_p)(struct mbuf *);
126 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
128 static void ether_restore_header(struct mbuf **, const struct ether_header *,
129 const struct ether_header *);
134 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
135 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
136 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
137 struct ifnet *(*bridge_interface_p)(void *if_bridge);
139 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
142 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
146 #define gotoerr(e) do { error = (e); goto bad; } while (0)
147 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
149 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
151 const struct ether_header *eh);
153 static int ether_ipfw;
154 static u_int ether_restore_hdr;
155 static u_int ether_prepend_hdr;
156 static int ether_debug;
159 static u_int ether_pktinfo_try;
160 static u_int ether_pktinfo_hit;
161 static u_int ether_rss_nopi;
162 static u_int ether_rss_nohash;
165 SYSCTL_DECL(_net_link);
166 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
167 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
168 ðer_debug, 0, "Ether debug");
169 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
170 ðer_ipfw, 0, "Pass ether pkts through firewall");
171 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
172 ðer_restore_hdr, 0, "# of ether header restoration");
173 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
174 ðer_prepend_hdr, 0,
175 "# of ether header restoration which prepends mbuf");
177 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
178 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
179 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
180 ðer_rss_nohash, 0, "# of packets do not have hash");
181 SYSCTL_UINT(_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_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
185 ðer_pktinfo_hit, 0,
186 "# of packets whose msgport are found using pktinfo");
189 #define ETHER_KTR_STR "ifp=%p"
190 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
192 #define KTR_ETHERNET KTR_ALL
194 KTR_INFO_MASTER(ether);
195 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
196 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
197 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
198 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
199 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
202 * Ethernet output routine.
203 * Encapsulate a packet of type family for the local net.
204 * Use trailer local net encapsulation if enough data in first
205 * packet leaves a multiple of 512 bytes of data in remainder.
206 * Assumes that ifp is actually pointer to arpcom structure.
209 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
212 struct ether_header *eh, *deh;
215 int hlen = ETHER_HDR_LEN; /* link layer header length */
216 struct arpcom *ac = IFP2AC(ifp);
219 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
221 if (ifp->if_flags & IFF_MONITOR)
223 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
226 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
229 eh = mtod(m, struct ether_header *);
230 edst = eh->ether_dhost;
233 * Fill in the destination ethernet address and frame type.
235 switch (dst->sa_family) {
238 if (!arpresolve(ifp, rt, m, dst, edst))
239 return (0); /* if not yet resolved */
241 if (m->m_flags & M_MPLSLABELED)
242 eh->ether_type = htons(ETHERTYPE_MPLS);
245 eh->ether_type = htons(ETHERTYPE_IP);
250 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
251 return (0); /* Something bad happenned. */
252 eh->ether_type = htons(ETHERTYPE_IPV6);
257 if (ef_outputp != NULL) {
259 * Hold BGL and recheck ef_outputp
262 if (ef_outputp != NULL) {
263 error = ef_outputp(ifp, &m, dst,
264 &eh->ether_type, &hlen);
273 eh->ether_type = htons(ETHERTYPE_IPX);
274 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
275 edst, ETHER_ADDR_LEN);
280 struct at_ifaddr *aa;
287 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
293 * In the phase 2 case, need to prepend an mbuf for
294 * the llc header. Since we must preserve the value
295 * of m, which is passed to us by value, we m_copy()
296 * the first mbuf, and use it for our llc header.
298 if (aa->aa_flags & AFA_PHASE2) {
301 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
302 eh = mtod(m, struct ether_header *);
303 edst = eh->ether_dhost;
304 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
305 llc.llc_control = LLC_UI;
306 bcopy(at_org_code, llc.llc_snap_org_code,
308 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
310 mtod(m, caddr_t) + sizeof(struct ether_header),
312 eh->ether_type = htons(m->m_pkthdr.len);
313 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
315 eh->ether_type = htons(ETHERTYPE_AT);
317 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
326 case pseudo_AF_HDRCMPLT:
328 loop_copy = -1; /* if this is for us, don't do it */
329 deh = (struct ether_header *)dst->sa_data;
330 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
331 eh->ether_type = deh->ether_type;
335 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
336 gotoerr(EAFNOSUPPORT);
339 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
340 memcpy(eh->ether_shost,
341 ((struct ether_header *)dst->sa_data)->ether_shost,
344 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
347 * Bridges require special output handling.
349 if (ifp->if_bridge) {
350 KASSERT(bridge_output_p != NULL,
351 ("%s: if_bridge not loaded!", __func__));
352 return bridge_output_p(ifp, m);
356 * If a simplex interface, and the packet is being sent to our
357 * Ethernet address or a broadcast address, loopback a copy.
358 * XXX To make a simplex device behave exactly like a duplex
359 * device, we should copy in the case of sending to our own
360 * ethernet address (thus letting the original actually appear
361 * on the wire). However, we don't do that here for security
362 * reasons and compatibility with the original behavior.
364 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
367 if (m->m_pkthdr.csum_flags & CSUM_IP)
368 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
369 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
370 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
371 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
374 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
375 n->m_pkthdr.csum_flags |= csum_flags;
376 if (csum_flags & CSUM_DATA_VALID)
377 n->m_pkthdr.csum_data = 0xffff;
378 if_simloop(ifp, n, dst->sa_family, hlen);
381 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
382 ETHER_ADDR_LEN) == 0) {
383 m->m_pkthdr.csum_flags |= csum_flags;
384 if (csum_flags & CSUM_DATA_VALID)
385 m->m_pkthdr.csum_data = 0xffff;
386 if_simloop(ifp, m, dst->sa_family, hlen);
387 return (0); /* XXX */
394 * Hold BGL and recheck ifp->if_carp
397 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
406 /* Handle ng_ether(4) processing, if any */
407 if (ng_ether_output_p != NULL) {
409 * Hold BGL and recheck ng_ether_output_p
412 if (ng_ether_output_p != NULL) {
413 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
425 /* Continue with link-layer output */
426 return ether_output_frame(ifp, m);
434 * Returns the bridge interface an ifp is associated
437 * Only call if ifp->if_bridge != NULL.
440 ether_bridge_interface(struct ifnet *ifp)
442 if (bridge_interface_p)
443 return(bridge_interface_p(ifp->if_bridge));
448 * Ethernet link layer output routine to send a raw frame to the device.
450 * This assumes that the 14 byte Ethernet header is present and contiguous
454 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
456 struct ip_fw *rule = NULL;
458 struct altq_pktattr pktattr;
460 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
462 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
465 /* Extract info from dummynet tag */
466 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
467 KKASSERT(mtag != NULL);
468 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
469 KKASSERT(rule != NULL);
471 m_tag_delete(m, mtag);
472 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
475 if (ifq_is_enabled(&ifp->if_snd))
476 altq_etherclassify(&ifp->if_snd, m, &pktattr);
478 if (IPFW_LOADED && ether_ipfw != 0) {
479 struct ether_header save_eh, *eh;
481 eh = mtod(m, struct ether_header *);
483 m_adj(m, ETHER_HDR_LEN);
484 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
488 return ENOBUFS; /* pkt dropped */
490 return 0; /* consumed e.g. in a pipe */
493 /* packet was ok, restore the ethernet header */
494 ether_restore_header(&m, eh, &save_eh);
503 * Queue message on interface, update output statistics if
504 * successful, and start output if interface not yet active.
506 error = ifq_dispatch(ifp, m, &pktattr);
511 * ipfw processing for ethernet packets (in and out).
512 * The second parameter is NULL from ether_demux(), and ifp from
513 * ether_output_frame().
516 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
517 const struct ether_header *eh)
519 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
520 struct ip_fw_args args;
525 if (*rule != NULL && fw_one_pass)
526 return TRUE; /* dummynet packet, already partially processed */
529 * I need some amount of data to be contiguous.
531 i = min((*m0)->m_pkthdr.len, max_protohdr);
532 if ((*m0)->m_len < i) {
533 *m0 = m_pullup(*m0, i);
541 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
542 m_tag_delete(*m0, mtag);
543 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
544 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
545 KKASSERT(mtag != NULL);
546 m_tag_delete(*m0, mtag);
547 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
550 args.m = *m0; /* the packet we are looking at */
551 args.oif = dst; /* destination, if any */
552 args.rule = *rule; /* matching rule to restart */
553 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
554 i = ip_fw_chk_ptr(&args);
569 * XXX at some point add support for divert/forward actions.
570 * If none of the above matches, we have to drop the pkt.
576 * Pass the pkt to dummynet, which consumes it.
578 m = *m0; /* pass the original to dummynet */
579 *m0 = NULL; /* and nothing back to the caller */
581 ether_restore_header(&m, eh, &save_eh);
585 ip_fw_dn_io_ptr(m, args.cookie,
586 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
591 panic("unknown ipfw return value: %d\n", i);
596 ether_input(struct ifnet *ifp, struct mbuf *m)
598 ether_input_chain(ifp, m, NULL, NULL);
602 * Perform common duties while attaching to interface list
605 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
607 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
612 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
613 lwkt_serialize_t serializer)
615 struct sockaddr_dl *sdl;
617 ifp->if_type = IFT_ETHER;
618 ifp->if_addrlen = ETHER_ADDR_LEN;
619 ifp->if_hdrlen = ETHER_HDR_LEN;
620 if_attach(ifp, serializer);
621 ifp->if_mtu = ETHERMTU;
622 if (ifp->if_baudrate == 0)
623 ifp->if_baudrate = 10000000;
624 ifp->if_output = ether_output;
625 ifp->if_input = ether_input;
626 ifp->if_resolvemulti = ether_resolvemulti;
627 ifp->if_broadcastaddr = etherbroadcastaddr;
628 sdl = IF_LLSOCKADDR(ifp);
629 sdl->sdl_type = IFT_ETHER;
630 sdl->sdl_alen = ifp->if_addrlen;
631 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
633 * XXX Keep the current drivers happy.
634 * XXX Remove once all drivers have been cleaned up
636 if (lla != IFP2AC(ifp)->ac_enaddr)
637 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
638 bpfattach(ifp, dlt, hdrlen);
639 if (ng_ether_attach_p != NULL)
640 (*ng_ether_attach_p)(ifp);
642 if_printf(ifp, "MAC address: %6D\n", lla, ":");
646 * Perform common duties while detaching an Ethernet interface
649 ether_ifdetach(struct ifnet *ifp)
653 if (ng_ether_detach_p != NULL)
654 (*ng_ether_detach_p)(ifp);
660 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
662 struct ifaddr *ifa = (struct ifaddr *) data;
663 struct ifreq *ifr = (struct ifreq *) data;
666 #define IF_INIT(ifp) \
668 if (((ifp)->if_flags & IFF_UP) == 0) { \
669 (ifp)->if_flags |= IFF_UP; \
670 (ifp)->if_init((ifp)->if_softc); \
674 ASSERT_IFNET_SERIALIZED_ALL(ifp);
678 switch (ifa->ifa_addr->sa_family) {
681 IF_INIT(ifp); /* before arpwhohas */
682 arp_ifinit(ifp, ifa);
687 * XXX - This code is probably wrong
691 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
692 struct arpcom *ac = IFP2AC(ifp);
694 if (ipx_nullhost(*ina))
695 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
697 bcopy(ina->x_host.c_host, ac->ac_enaddr,
698 sizeof ac->ac_enaddr);
700 IF_INIT(ifp); /* Set new address. */
711 bcopy(IFP2AC(ifp)->ac_enaddr,
712 ((struct sockaddr *)ifr->ifr_data)->sa_data,
718 * Set the interface MTU.
720 if (ifr->ifr_mtu > ETHERMTU) {
723 ifp->if_mtu = ifr->ifr_mtu;
738 struct sockaddr **llsa,
741 struct sockaddr_dl *sdl;
742 struct sockaddr_in *sin;
744 struct sockaddr_in6 *sin6;
748 switch(sa->sa_family) {
751 * No mapping needed. Just check that it's a valid MC address.
753 sdl = (struct sockaddr_dl *)sa;
754 e_addr = LLADDR(sdl);
755 if ((e_addr[0] & 1) != 1)
756 return EADDRNOTAVAIL;
762 sin = (struct sockaddr_in *)sa;
763 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
764 return EADDRNOTAVAIL;
765 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
767 sdl->sdl_len = sizeof *sdl;
768 sdl->sdl_family = AF_LINK;
769 sdl->sdl_index = ifp->if_index;
770 sdl->sdl_type = IFT_ETHER;
771 sdl->sdl_alen = ETHER_ADDR_LEN;
772 e_addr = LLADDR(sdl);
773 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
774 *llsa = (struct sockaddr *)sdl;
779 sin6 = (struct sockaddr_in6 *)sa;
780 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
782 * An IP6 address of 0 means listen to all
783 * of the Ethernet multicast address used for IP6.
784 * (This is used for multicast routers.)
786 ifp->if_flags |= IFF_ALLMULTI;
790 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
791 return EADDRNOTAVAIL;
792 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
794 sdl->sdl_len = sizeof *sdl;
795 sdl->sdl_family = AF_LINK;
796 sdl->sdl_index = ifp->if_index;
797 sdl->sdl_type = IFT_ETHER;
798 sdl->sdl_alen = ETHER_ADDR_LEN;
799 e_addr = LLADDR(sdl);
800 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
801 *llsa = (struct sockaddr *)sdl;
807 * Well, the text isn't quite right, but it's the name
816 * This is for reference. We have a table-driven version
817 * of the little-endian crc32 generator, which is faster
818 * than the double-loop.
821 ether_crc32_le(const uint8_t *buf, size_t len)
823 uint32_t c, crc, carry;
826 crc = 0xffffffffU; /* initial value */
828 for (i = 0; i < len; i++) {
830 for (j = 0; j < 8; j++) {
831 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
835 crc = (crc ^ ETHER_CRC_POLY_LE);
843 ether_crc32_le(const uint8_t *buf, size_t len)
845 static const uint32_t crctab[] = {
846 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
847 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
848 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
849 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
854 crc = 0xffffffffU; /* initial value */
856 for (i = 0; i < len; i++) {
858 crc = (crc >> 4) ^ crctab[crc & 0xf];
859 crc = (crc >> 4) ^ crctab[crc & 0xf];
867 ether_crc32_be(const uint8_t *buf, size_t len)
869 uint32_t c, crc, carry;
872 crc = 0xffffffffU; /* initial value */
874 for (i = 0; i < len; i++) {
876 for (j = 0; j < 8; j++) {
877 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
881 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
889 * find the size of ethernet header, and call classifier
892 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
893 struct altq_pktattr *pktattr)
895 struct ether_header *eh;
897 int hlen, af, hdrsize;
900 hlen = sizeof(struct ether_header);
901 eh = mtod(m, struct ether_header *);
903 ether_type = ntohs(eh->ether_type);
904 if (ether_type < ETHERMTU) {
906 struct llc *llc = (struct llc *)(eh + 1);
909 if (m->m_len < hlen ||
910 llc->llc_dsap != LLC_SNAP_LSAP ||
911 llc->llc_ssap != LLC_SNAP_LSAP ||
912 llc->llc_control != LLC_UI)
913 goto bad; /* not snap! */
915 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
918 if (ether_type == ETHERTYPE_IP) {
920 hdrsize = 20; /* sizeof(struct ip) */
922 } else if (ether_type == ETHERTYPE_IPV6) {
924 hdrsize = 40; /* sizeof(struct ip6_hdr) */
929 while (m->m_len <= hlen) {
933 hdr = m->m_data + hlen;
934 if (m->m_len < hlen + hdrsize) {
936 * ip header is not in a single mbuf. this should not
937 * happen in the current code.
938 * (todo: use m_pulldown in the future)
944 ifq_classify(ifq, m, af, pktattr);
951 pktattr->pattr_class = NULL;
952 pktattr->pattr_hdr = NULL;
953 pktattr->pattr_af = AF_UNSPEC;
957 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
958 const struct ether_header *save_eh)
960 struct mbuf *m = *m0;
965 * Prepend the header, optimize for the common case of
966 * eh pointing into the mbuf.
968 if ((const void *)(eh + 1) == (void *)m->m_data) {
969 m->m_data -= ETHER_HDR_LEN;
970 m->m_len += ETHER_HDR_LEN;
971 m->m_pkthdr.len += ETHER_HDR_LEN;
975 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
977 bcopy(save_eh, mtod(m, struct ether_header *),
985 ether_input_ipifunc(void *arg)
987 struct mbuf *m, *next;
988 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
994 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
1000 ether_input_dispatch(struct mbuf_chain *chain)
1005 logether(disp_beg, NULL);
1006 for (i = 0; i < ncpus; ++i) {
1007 if (chain[i].mc_head != NULL) {
1008 lwkt_send_ipiq(globaldata_find(i),
1009 ether_input_ipifunc, chain[i].mc_head);
1013 logether(disp_beg, NULL);
1014 if (chain->mc_head != NULL)
1015 ether_input_ipifunc(chain->mc_head);
1017 logether(disp_end, NULL);
1021 ether_input_chain_init(struct mbuf_chain *chain)
1026 for (i = 0; i < ncpus; ++i)
1027 chain[i].mc_head = chain[i].mc_tail = NULL;
1029 chain->mc_head = chain->mc_tail = NULL;
1034 * Upper layer processing for a received Ethernet packet.
1037 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1039 struct ether_header *eh;
1040 int isr, discard = 0;
1042 struct ip_fw *rule = NULL;
1048 KASSERT(m->m_len >= ETHER_HDR_LEN,
1049 ("ether header is no contiguous!\n"));
1051 eh = mtod(m, struct ether_header *);
1053 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1056 /* Extract info from dummynet tag */
1057 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1058 KKASSERT(mtag != NULL);
1059 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1060 KKASSERT(rule != NULL);
1062 m_tag_delete(m, mtag);
1063 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1065 /* packet is passing the second time */
1071 * XXX: Okay, we need to call carp_forus() and - if it is for
1072 * us jump over code that does the normal check
1073 * "ac_enaddr == ether_dhost". The check sequence is a bit
1074 * different from OpenBSD, so we jump over as few code as
1075 * possible, to catch _all_ sanity checks. This needs
1076 * evaluation, to see if the carp ether_dhost values break any
1081 * Hold BGL and recheck ifp->if_carp
1084 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1093 * We got a packet which was unicast to a different Ethernet
1094 * address. If the driver is working properly, then this
1095 * situation can only happen when the interface is in
1096 * promiscuous mode. We defer the packet discarding until the
1097 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1100 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1101 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1102 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1103 if (ether_debug & 1) {
1104 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1105 "%02x:%02x:%02x:%02x:%02x:%02x "
1106 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1120 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1121 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1122 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1123 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1124 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1125 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1128 if ((ether_debug & 2) == 0)
1133 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1134 struct ether_header save_eh = *eh;
1136 /* XXX old crufty stuff, needs to be removed */
1137 m_adj(m, sizeof(struct ether_header));
1139 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1144 ether_restore_header(&m, eh, &save_eh);
1147 eh = mtod(m, struct ether_header *);
1150 ether_type = ntohs(eh->ether_type);
1151 KKASSERT(ether_type != ETHERTYPE_VLAN);
1153 if (m->m_flags & M_VLANTAG) {
1154 void (*vlan_input_func)(struct mbuf *);
1156 vlan_input_func = vlan_input_p;
1157 if (vlan_input_func != NULL) {
1160 m->m_pkthdr.rcvif->if_noproto++;
1167 * If we have been asked to discard this packet
1168 * (e.g. not for us), drop it before entering
1177 * Clear protocol specific flags,
1178 * before entering the upper layer.
1180 m->m_flags &= ~M_ETHER_FLAGS;
1182 /* Strip ethernet header. */
1183 m_adj(m, sizeof(struct ether_header));
1185 switch (ether_type) {
1188 if ((m->m_flags & M_LENCHECKED) == 0) {
1189 if (!ip_lengthcheck(&m, 0))
1192 if (ipflow_fastforward(m))
1198 if (ifp->if_flags & IFF_NOARP) {
1199 /* Discard packet if ARP is disabled on interface */
1208 case ETHERTYPE_IPV6:
1217 * Hold BGL and recheck ef_inputp
1220 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1232 isr = NETISR_ATALK1;
1234 case ETHERTYPE_AARP:
1240 case ETHERTYPE_MPLS:
1241 case ETHERTYPE_MPLS_MCAST:
1242 /* Should have been set by ether_input_chain(). */
1243 KKASSERT(m->m_flags & M_MPLSLABELED);
1250 * The accurate msgport is not determined before
1251 * we reach here, so redo the dispatching
1256 * Hold BGL and recheck ef_inputp
1259 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1267 if (ether_type > ETHERMTU)
1269 l = mtod(m, struct llc *);
1270 if (l->llc_dsap == LLC_SNAP_LSAP &&
1271 l->llc_ssap == LLC_SNAP_LSAP &&
1272 l->llc_control == LLC_UI) {
1273 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1274 sizeof at_org_code) == 0 &&
1275 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1276 m_adj(m, sizeof(struct llc));
1277 isr = NETISR_ATALK2;
1280 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1281 sizeof aarp_org_code) == 0 &&
1282 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1283 m_adj(m, sizeof(struct llc));
1290 if (ng_ether_input_orphan_p != NULL) {
1292 * Hold BGL and recheck ng_ether_input_orphan_p
1295 if (ng_ether_input_orphan_p != NULL) {
1296 ng_ether_input_orphan_p(ifp, m, eh);
1306 netisr_queue(isr, m);
1310 * First we perform any link layer operations, then continue to the
1311 * upper layers with ether_demux_oncpu().
1314 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1316 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1318 * Receiving interface's flags are changed, when this
1319 * packet is waiting for processing; discard it.
1326 * Tap the packet off here for a bridge. bridge_input()
1327 * will return NULL if it has consumed the packet, otherwise
1328 * it gets processed as normal. Note that bridge_input()
1329 * will always return the original packet if we need to
1330 * process it locally.
1332 if (ifp->if_bridge) {
1333 KASSERT(bridge_input_p != NULL,
1334 ("%s: if_bridge not loaded!", __func__));
1336 if(m->m_flags & M_ETHER_BRIDGED) {
1337 m->m_flags &= ~M_ETHER_BRIDGED;
1339 m = bridge_input_p(ifp, m);
1343 KASSERT(ifp == m->m_pkthdr.rcvif,
1344 ("bridge_input_p changed rcvif\n"));
1348 /* Handle ng_ether(4) processing, if any */
1349 if (ng_ether_input_p != NULL) {
1351 * Hold BGL and recheck ng_ether_input_p
1354 if (ng_ether_input_p != NULL)
1355 ng_ether_input_p(ifp, &m);
1362 /* Continue with upper layer processing */
1363 ether_demux_oncpu(ifp, m);
1367 * Perform certain functions of ether_input_chain():
1369 * - Update statistics
1370 * - Run bpf(4) tap if requested
1371 * Then pass the packet to ether_input_oncpu().
1373 * This function should be used by pseudo interface (e.g. vlan(4)),
1374 * when it tries to claim that the packet is received by it.
1380 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1382 /* Discard packet if interface is not up */
1383 if (!(ifp->if_flags & IFF_UP)) {
1389 * Change receiving interface. The bridge will often pass a flag to
1390 * ask that this not be done so ARPs get applied to the correct
1393 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1394 m->m_pkthdr.rcvif == NULL) {
1395 m->m_pkthdr.rcvif = ifp;
1398 /* Update statistics */
1400 ifp->if_ibytes += m->m_pkthdr.len;
1401 if (m->m_flags & (M_MCAST | M_BCAST))
1404 if (reinput_flags & REINPUT_RUNBPF)
1407 ether_input_oncpu(ifp, m);
1410 static __inline boolean_t
1411 ether_vlancheck(struct mbuf **m0)
1413 struct mbuf *m = *m0;
1414 struct ether_header *eh;
1415 uint16_t ether_type;
1417 eh = mtod(m, struct ether_header *);
1418 ether_type = ntohs(eh->ether_type);
1420 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1422 * Extract vlan tag if hardware does not do it for us
1424 vlan_ether_decap(&m);
1428 eh = mtod(m, struct ether_header *);
1429 ether_type = ntohs(eh->ether_type);
1432 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1434 * To prevent possible dangerous recursion,
1435 * we don't do vlan-in-vlan
1437 m->m_pkthdr.rcvif->if_noproto++;
1440 KKASSERT(ether_type != ETHERTYPE_VLAN);
1442 m->m_flags |= M_ETHER_VLANCHECKED;
1453 ether_input_handler(netmsg_t nmsg)
1455 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1456 struct ether_header *eh;
1462 ifp = m->m_pkthdr.rcvif;
1464 eh = mtod(m, struct ether_header *);
1465 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1466 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1467 ifp->if_addrlen) == 0)
1468 m->m_flags |= M_BCAST;
1470 m->m_flags |= M_MCAST;
1474 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1475 if (!ether_vlancheck(&m)) {
1476 KKASSERT(m == NULL);
1481 ether_input_oncpu(ifp, m);
1485 * Send the packet to the target msgport or queue it into 'chain'.
1487 * At this point the packet had better be characterized (M_HASH set),
1488 * so we know which cpu to send it to.
1491 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1493 struct netmsg_packet *pmsg;
1495 KKASSERT(m->m_flags & M_HASH);
1496 pmsg = &m->m_hdr.mh_netmsg;
1497 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1498 0, ether_input_handler);
1499 pmsg->nm_packet = m;
1500 pmsg->base.lmsg.u.ms_result = isr;
1502 if (chain != NULL) {
1503 int cpuid = m->m_pkthdr.hash;
1504 struct mbuf_chain *c;
1507 if (c->mc_head == NULL) {
1508 c->mc_head = c->mc_tail = m;
1510 c->mc_tail->m_nextpkt = m;
1513 m->m_nextpkt = NULL;
1515 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1520 * Process a received Ethernet packet.
1522 * The ethernet header is assumed to be in the mbuf so the caller
1523 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1524 * bytes in the first mbuf.
1526 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1527 * immediately. This situation happens when ether_input_chain is
1528 * accessed through ifnet.if_input.
1530 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1531 * bucket indexed by the target msgport's cpuid and the target msgport
1532 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1533 * must initialize 'chain' by calling ether_input_chain_init().
1534 * ether_input_dispatch must be called later to send ether frames
1535 * queued on 'chain' to their target msgport.
1538 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1539 struct mbuf_chain *chain)
1541 struct ether_header *eh;
1542 uint16_t ether_type;
1547 /* Discard packet if interface is not up */
1548 if (!(ifp->if_flags & IFF_UP)) {
1553 if (m->m_len < sizeof(struct ether_header)) {
1554 /* XXX error in the caller. */
1559 m->m_pkthdr.rcvif = ifp;
1561 logether(chain_beg, ifp);
1563 ETHER_BPF_MTAP(ifp, m);
1565 ifp->if_ibytes += m->m_pkthdr.len;
1567 if (ifp->if_flags & IFF_MONITOR) {
1568 eh = mtod(m, struct ether_header *);
1569 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1573 * Interface marked for monitoring; discard packet.
1577 logether(chain_end, ifp);
1582 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1583 * we can dispatch it immediately without further inspection.
1585 if (pi != NULL && (m->m_flags & M_HASH)) {
1587 ether_pktinfo_try++;
1589 ether_dispatch(pi->pi_netisr, m, chain);
1592 ether_pktinfo_hit++;
1594 logether(chain_end, ifp);
1598 else if (ifp->if_capenable & IFCAP_RSS) {
1607 * Packet hash will be recalculated by software,
1608 * so clear the M_HASH flag set by the driver;
1609 * the hash value calculated by the hardware may
1610 * not be exactly what we want.
1612 m->m_flags &= ~M_HASH;
1614 if (!ether_vlancheck(&m)) {
1615 KKASSERT(m == NULL);
1616 logether(chain_end, ifp);
1619 eh = mtod(m, struct ether_header *);
1620 ether_type = ntohs(eh->ether_type);
1623 * Map ether type to netisr id.
1625 switch (ether_type) {
1637 case ETHERTYPE_IPV6:
1650 isr = NETISR_ATALK1;
1652 case ETHERTYPE_AARP:
1658 case ETHERTYPE_MPLS:
1659 case ETHERTYPE_MPLS_MCAST:
1660 m->m_flags |= M_MPLSLABELED;
1667 * NETISR_MAX is an invalid value; it is chosen to let
1674 * Ask the isr to characterize the packet since we couldn't.
1675 * This is an attempt to optimally get us onto the correct protocol
1678 netisr_characterize(isr, &m, sizeof(struct ether_header));
1680 logether(chain_end, ifp);
1685 * Finally dispatch it
1687 ether_dispatch(isr, m, chain);
1689 logether(chain_end, ifp);
1692 MODULE_VERSION(ether, 1);