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 u_long ether_input_wronghash;
158 static int ether_debug;
161 static u_long ether_pktinfo_try;
162 static u_long ether_pktinfo_hit;
163 static u_long ether_rss_nopi;
164 static u_long ether_rss_nohash;
165 static u_long ether_input_requeue;
168 SYSCTL_DECL(_net_link);
169 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
170 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
171 ðer_debug, 0, "Ether debug");
172 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
173 ðer_ipfw, 0, "Pass ether pkts through firewall");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
175 ðer_restore_hdr, 0, "# of ether header restoration");
176 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
177 ðer_prepend_hdr, 0,
178 "# of ether header restoration which prepends mbuf");
179 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
180 ðer_input_wronghash, 0, "# of input packets with wrong hash");
182 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
183 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
184 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
185 ðer_rss_nohash, 0, "# of packets do not have hash");
186 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
187 ðer_pktinfo_try, 0,
188 "# of tries to find packets' msgport using pktinfo");
189 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
190 ðer_pktinfo_hit, 0,
191 "# of packets whose msgport are found using pktinfo");
192 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
193 ðer_input_requeue, 0, "# of input packets gets requeued");
196 #define ETHER_KTR_STR "ifp=%p"
197 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
199 #define KTR_ETHERNET KTR_ALL
201 KTR_INFO_MASTER(ether);
202 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
203 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
204 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
205 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
206 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
209 * Ethernet output routine.
210 * Encapsulate a packet of type family for the local net.
211 * Use trailer local net encapsulation if enough data in first
212 * packet leaves a multiple of 512 bytes of data in remainder.
213 * Assumes that ifp is actually pointer to arpcom structure.
216 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
219 struct ether_header *eh, *deh;
222 int hlen = ETHER_HDR_LEN; /* link layer header length */
223 struct arpcom *ac = IFP2AC(ifp);
226 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
228 if (ifp->if_flags & IFF_MONITOR)
230 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
233 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
236 eh = mtod(m, struct ether_header *);
237 edst = eh->ether_dhost;
240 * Fill in the destination ethernet address and frame type.
242 switch (dst->sa_family) {
245 if (!arpresolve(ifp, rt, m, dst, edst))
246 return (0); /* if not yet resolved */
248 if (m->m_flags & M_MPLSLABELED)
249 eh->ether_type = htons(ETHERTYPE_MPLS);
252 eh->ether_type = htons(ETHERTYPE_IP);
257 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
258 return (0); /* Something bad happenned. */
259 eh->ether_type = htons(ETHERTYPE_IPV6);
264 if (ef_outputp != NULL) {
266 * Hold BGL and recheck ef_outputp
269 if (ef_outputp != NULL) {
270 error = ef_outputp(ifp, &m, dst,
271 &eh->ether_type, &hlen);
280 eh->ether_type = htons(ETHERTYPE_IPX);
281 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
282 edst, ETHER_ADDR_LEN);
287 struct at_ifaddr *aa;
294 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
300 * In the phase 2 case, need to prepend an mbuf for
301 * the llc header. Since we must preserve the value
302 * of m, which is passed to us by value, we m_copy()
303 * the first mbuf, and use it for our llc header.
305 if (aa->aa_flags & AFA_PHASE2) {
308 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
309 eh = mtod(m, struct ether_header *);
310 edst = eh->ether_dhost;
311 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
312 llc.llc_control = LLC_UI;
313 bcopy(at_org_code, llc.llc_snap_org_code,
315 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
317 mtod(m, caddr_t) + sizeof(struct ether_header),
319 eh->ether_type = htons(m->m_pkthdr.len);
320 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
322 eh->ether_type = htons(ETHERTYPE_AT);
324 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
333 case pseudo_AF_HDRCMPLT:
335 loop_copy = -1; /* if this is for us, don't do it */
336 deh = (struct ether_header *)dst->sa_data;
337 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
338 eh->ether_type = deh->ether_type;
342 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
343 gotoerr(EAFNOSUPPORT);
346 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
347 memcpy(eh->ether_shost,
348 ((struct ether_header *)dst->sa_data)->ether_shost,
351 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
354 * Bridges require special output handling.
356 if (ifp->if_bridge) {
357 KASSERT(bridge_output_p != NULL,
358 ("%s: if_bridge not loaded!", __func__));
359 return bridge_output_p(ifp, m);
363 * If a simplex interface, and the packet is being sent to our
364 * Ethernet address or a broadcast address, loopback a copy.
365 * XXX To make a simplex device behave exactly like a duplex
366 * device, we should copy in the case of sending to our own
367 * ethernet address (thus letting the original actually appear
368 * on the wire). However, we don't do that here for security
369 * reasons and compatibility with the original behavior.
371 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
374 if (m->m_pkthdr.csum_flags & CSUM_IP)
375 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
376 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
377 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
378 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
381 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
382 n->m_pkthdr.csum_flags |= csum_flags;
383 if (csum_flags & CSUM_DATA_VALID)
384 n->m_pkthdr.csum_data = 0xffff;
385 if_simloop(ifp, n, dst->sa_family, hlen);
388 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
389 ETHER_ADDR_LEN) == 0) {
390 m->m_pkthdr.csum_flags |= csum_flags;
391 if (csum_flags & CSUM_DATA_VALID)
392 m->m_pkthdr.csum_data = 0xffff;
393 if_simloop(ifp, m, dst->sa_family, hlen);
394 return (0); /* XXX */
401 * Hold BGL and recheck ifp->if_carp
404 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
413 /* Handle ng_ether(4) processing, if any */
414 if (ng_ether_output_p != NULL) {
416 * Hold BGL and recheck ng_ether_output_p
419 if (ng_ether_output_p != NULL) {
420 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
432 /* Continue with link-layer output */
433 return ether_output_frame(ifp, m);
441 * Returns the bridge interface an ifp is associated
444 * Only call if ifp->if_bridge != NULL.
447 ether_bridge_interface(struct ifnet *ifp)
449 if (bridge_interface_p)
450 return(bridge_interface_p(ifp->if_bridge));
455 * Ethernet link layer output routine to send a raw frame to the device.
457 * This assumes that the 14 byte Ethernet header is present and contiguous
461 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
463 struct ip_fw *rule = NULL;
465 struct altq_pktattr pktattr;
467 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
469 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
472 /* Extract info from dummynet tag */
473 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
474 KKASSERT(mtag != NULL);
475 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
476 KKASSERT(rule != NULL);
478 m_tag_delete(m, mtag);
479 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
482 if (ifq_is_enabled(&ifp->if_snd))
483 altq_etherclassify(&ifp->if_snd, m, &pktattr);
485 if (IPFW_LOADED && ether_ipfw != 0) {
486 struct ether_header save_eh, *eh;
488 eh = mtod(m, struct ether_header *);
490 m_adj(m, ETHER_HDR_LEN);
491 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
495 return ENOBUFS; /* pkt dropped */
497 return 0; /* consumed e.g. in a pipe */
500 /* packet was ok, restore the ethernet header */
501 ether_restore_header(&m, eh, &save_eh);
510 * Queue message on interface, update output statistics if
511 * successful, and start output if interface not yet active.
513 error = ifq_dispatch(ifp, m, &pktattr);
518 * ipfw processing for ethernet packets (in and out).
519 * The second parameter is NULL from ether_demux(), and ifp from
520 * ether_output_frame().
523 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
524 const struct ether_header *eh)
526 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
527 struct ip_fw_args args;
532 if (*rule != NULL && fw_one_pass)
533 return TRUE; /* dummynet packet, already partially processed */
536 * I need some amount of data to be contiguous.
538 i = min((*m0)->m_pkthdr.len, max_protohdr);
539 if ((*m0)->m_len < i) {
540 *m0 = m_pullup(*m0, i);
548 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
549 m_tag_delete(*m0, mtag);
550 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
551 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
552 KKASSERT(mtag != NULL);
553 m_tag_delete(*m0, mtag);
554 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
557 args.m = *m0; /* the packet we are looking at */
558 args.oif = dst; /* destination, if any */
559 args.rule = *rule; /* matching rule to restart */
560 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
561 i = ip_fw_chk_ptr(&args);
576 * XXX at some point add support for divert/forward actions.
577 * If none of the above matches, we have to drop the pkt.
583 * Pass the pkt to dummynet, which consumes it.
585 m = *m0; /* pass the original to dummynet */
586 *m0 = NULL; /* and nothing back to the caller */
588 ether_restore_header(&m, eh, &save_eh);
592 ip_fw_dn_io_ptr(m, args.cookie,
593 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
598 panic("unknown ipfw return value: %d\n", i);
603 ether_input(struct ifnet *ifp, struct mbuf *m)
605 ether_input_chain(ifp, m, NULL, NULL);
609 * Perform common duties while attaching to interface list
612 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
614 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
619 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
620 lwkt_serialize_t serializer)
622 struct sockaddr_dl *sdl;
624 ifp->if_type = IFT_ETHER;
625 ifp->if_addrlen = ETHER_ADDR_LEN;
626 ifp->if_hdrlen = ETHER_HDR_LEN;
627 if_attach(ifp, serializer);
628 ifp->if_mtu = ETHERMTU;
629 if (ifp->if_baudrate == 0)
630 ifp->if_baudrate = 10000000;
631 ifp->if_output = ether_output;
632 ifp->if_input = ether_input;
633 ifp->if_resolvemulti = ether_resolvemulti;
634 ifp->if_broadcastaddr = etherbroadcastaddr;
635 sdl = IF_LLSOCKADDR(ifp);
636 sdl->sdl_type = IFT_ETHER;
637 sdl->sdl_alen = ifp->if_addrlen;
638 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
640 * XXX Keep the current drivers happy.
641 * XXX Remove once all drivers have been cleaned up
643 if (lla != IFP2AC(ifp)->ac_enaddr)
644 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
645 bpfattach(ifp, dlt, hdrlen);
646 if (ng_ether_attach_p != NULL)
647 (*ng_ether_attach_p)(ifp);
649 if_printf(ifp, "MAC address: %6D\n", lla, ":");
653 * Perform common duties while detaching an Ethernet interface
656 ether_ifdetach(struct ifnet *ifp)
660 if (ng_ether_detach_p != NULL)
661 (*ng_ether_detach_p)(ifp);
667 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
669 struct ifaddr *ifa = (struct ifaddr *) data;
670 struct ifreq *ifr = (struct ifreq *) data;
673 #define IF_INIT(ifp) \
675 if (((ifp)->if_flags & IFF_UP) == 0) { \
676 (ifp)->if_flags |= IFF_UP; \
677 (ifp)->if_init((ifp)->if_softc); \
681 ASSERT_IFNET_SERIALIZED_ALL(ifp);
685 switch (ifa->ifa_addr->sa_family) {
688 IF_INIT(ifp); /* before arpwhohas */
689 arp_ifinit(ifp, ifa);
694 * XXX - This code is probably wrong
698 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
699 struct arpcom *ac = IFP2AC(ifp);
701 if (ipx_nullhost(*ina))
702 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
704 bcopy(ina->x_host.c_host, ac->ac_enaddr,
705 sizeof ac->ac_enaddr);
707 IF_INIT(ifp); /* Set new address. */
718 bcopy(IFP2AC(ifp)->ac_enaddr,
719 ((struct sockaddr *)ifr->ifr_data)->sa_data,
725 * Set the interface MTU.
727 if (ifr->ifr_mtu > ETHERMTU) {
730 ifp->if_mtu = ifr->ifr_mtu;
745 struct sockaddr **llsa,
748 struct sockaddr_dl *sdl;
749 struct sockaddr_in *sin;
751 struct sockaddr_in6 *sin6;
755 switch(sa->sa_family) {
758 * No mapping needed. Just check that it's a valid MC address.
760 sdl = (struct sockaddr_dl *)sa;
761 e_addr = LLADDR(sdl);
762 if ((e_addr[0] & 1) != 1)
763 return EADDRNOTAVAIL;
769 sin = (struct sockaddr_in *)sa;
770 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
771 return EADDRNOTAVAIL;
772 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
774 sdl->sdl_len = sizeof *sdl;
775 sdl->sdl_family = AF_LINK;
776 sdl->sdl_index = ifp->if_index;
777 sdl->sdl_type = IFT_ETHER;
778 sdl->sdl_alen = ETHER_ADDR_LEN;
779 e_addr = LLADDR(sdl);
780 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
781 *llsa = (struct sockaddr *)sdl;
786 sin6 = (struct sockaddr_in6 *)sa;
787 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
789 * An IP6 address of 0 means listen to all
790 * of the Ethernet multicast address used for IP6.
791 * (This is used for multicast routers.)
793 ifp->if_flags |= IFF_ALLMULTI;
797 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
798 return EADDRNOTAVAIL;
799 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
801 sdl->sdl_len = sizeof *sdl;
802 sdl->sdl_family = AF_LINK;
803 sdl->sdl_index = ifp->if_index;
804 sdl->sdl_type = IFT_ETHER;
805 sdl->sdl_alen = ETHER_ADDR_LEN;
806 e_addr = LLADDR(sdl);
807 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
808 *llsa = (struct sockaddr *)sdl;
814 * Well, the text isn't quite right, but it's the name
823 * This is for reference. We have a table-driven version
824 * of the little-endian crc32 generator, which is faster
825 * than the double-loop.
828 ether_crc32_le(const uint8_t *buf, size_t len)
830 uint32_t c, crc, carry;
833 crc = 0xffffffffU; /* initial value */
835 for (i = 0; i < len; i++) {
837 for (j = 0; j < 8; j++) {
838 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
842 crc = (crc ^ ETHER_CRC_POLY_LE);
850 ether_crc32_le(const uint8_t *buf, size_t len)
852 static const uint32_t crctab[] = {
853 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
854 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
855 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
856 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
861 crc = 0xffffffffU; /* initial value */
863 for (i = 0; i < len; i++) {
865 crc = (crc >> 4) ^ crctab[crc & 0xf];
866 crc = (crc >> 4) ^ crctab[crc & 0xf];
874 ether_crc32_be(const uint8_t *buf, size_t len)
876 uint32_t c, crc, carry;
879 crc = 0xffffffffU; /* initial value */
881 for (i = 0; i < len; i++) {
883 for (j = 0; j < 8; j++) {
884 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
888 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
896 * find the size of ethernet header, and call classifier
899 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
900 struct altq_pktattr *pktattr)
902 struct ether_header *eh;
904 int hlen, af, hdrsize;
907 hlen = sizeof(struct ether_header);
908 eh = mtod(m, struct ether_header *);
910 ether_type = ntohs(eh->ether_type);
911 if (ether_type < ETHERMTU) {
913 struct llc *llc = (struct llc *)(eh + 1);
916 if (m->m_len < hlen ||
917 llc->llc_dsap != LLC_SNAP_LSAP ||
918 llc->llc_ssap != LLC_SNAP_LSAP ||
919 llc->llc_control != LLC_UI)
920 goto bad; /* not snap! */
922 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
925 if (ether_type == ETHERTYPE_IP) {
927 hdrsize = 20; /* sizeof(struct ip) */
929 } else if (ether_type == ETHERTYPE_IPV6) {
931 hdrsize = 40; /* sizeof(struct ip6_hdr) */
936 while (m->m_len <= hlen) {
940 hdr = m->m_data + hlen;
941 if (m->m_len < hlen + hdrsize) {
943 * ip header is not in a single mbuf. this should not
944 * happen in the current code.
945 * (todo: use m_pulldown in the future)
951 ifq_classify(ifq, m, af, pktattr);
958 pktattr->pattr_class = NULL;
959 pktattr->pattr_hdr = NULL;
960 pktattr->pattr_af = AF_UNSPEC;
964 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
965 const struct ether_header *save_eh)
967 struct mbuf *m = *m0;
972 * Prepend the header, optimize for the common case of
973 * eh pointing into the mbuf.
975 if ((const void *)(eh + 1) == (void *)m->m_data) {
976 m->m_data -= ETHER_HDR_LEN;
977 m->m_len += ETHER_HDR_LEN;
978 m->m_pkthdr.len += ETHER_HDR_LEN;
982 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
984 bcopy(save_eh, mtod(m, struct ether_header *),
992 ether_input_ipifunc(void *arg)
994 struct mbuf *m, *next;
995 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
1000 m->m_nextpkt = NULL;
1001 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
1003 } while (m != NULL);
1007 ether_input_dispatch(struct mbuf_chain *chain)
1012 logether(disp_beg, NULL);
1013 for (i = 0; i < ncpus; ++i) {
1014 if (chain[i].mc_head != NULL) {
1015 lwkt_send_ipiq(globaldata_find(i),
1016 ether_input_ipifunc, chain[i].mc_head);
1020 logether(disp_beg, NULL);
1021 if (chain->mc_head != NULL)
1022 ether_input_ipifunc(chain->mc_head);
1024 logether(disp_end, NULL);
1028 ether_input_chain_init(struct mbuf_chain *chain)
1033 for (i = 0; i < ncpus; ++i)
1034 chain[i].mc_head = chain[i].mc_tail = NULL;
1036 chain->mc_head = chain->mc_tail = NULL;
1041 * Upper layer processing for a received Ethernet packet.
1044 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1046 struct ether_header *eh;
1047 int isr, discard = 0;
1049 struct ip_fw *rule = NULL;
1055 KASSERT(m->m_len >= ETHER_HDR_LEN,
1056 ("ether header is no contiguous!\n"));
1058 eh = mtod(m, struct ether_header *);
1060 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1063 /* Extract info from dummynet tag */
1064 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1065 KKASSERT(mtag != NULL);
1066 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1067 KKASSERT(rule != NULL);
1069 m_tag_delete(m, mtag);
1070 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1072 /* packet is passing the second time */
1078 * XXX: Okay, we need to call carp_forus() and - if it is for
1079 * us jump over code that does the normal check
1080 * "ac_enaddr == ether_dhost". The check sequence is a bit
1081 * different from OpenBSD, so we jump over as few code as
1082 * possible, to catch _all_ sanity checks. This needs
1083 * evaluation, to see if the carp ether_dhost values break any
1088 * Hold BGL and recheck ifp->if_carp
1091 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1100 * We got a packet which was unicast to a different Ethernet
1101 * address. If the driver is working properly, then this
1102 * situation can only happen when the interface is in
1103 * promiscuous mode. We defer the packet discarding until the
1104 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1107 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1108 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1109 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1110 if (ether_debug & 1) {
1111 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1112 "%02x:%02x:%02x:%02x:%02x:%02x "
1113 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1127 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1128 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1129 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1130 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1131 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1132 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1135 if ((ether_debug & 2) == 0)
1140 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1141 struct ether_header save_eh = *eh;
1143 /* XXX old crufty stuff, needs to be removed */
1144 m_adj(m, sizeof(struct ether_header));
1146 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1151 ether_restore_header(&m, eh, &save_eh);
1154 eh = mtod(m, struct ether_header *);
1157 ether_type = ntohs(eh->ether_type);
1158 KKASSERT(ether_type != ETHERTYPE_VLAN);
1160 if (m->m_flags & M_VLANTAG) {
1161 void (*vlan_input_func)(struct mbuf *);
1163 vlan_input_func = vlan_input_p;
1164 if (vlan_input_func != NULL) {
1167 m->m_pkthdr.rcvif->if_noproto++;
1174 * If we have been asked to discard this packet
1175 * (e.g. not for us), drop it before entering
1184 * Clear protocol specific flags,
1185 * before entering the upper layer.
1187 m->m_flags &= ~M_ETHER_FLAGS;
1189 /* Strip ethernet header. */
1190 m_adj(m, sizeof(struct ether_header));
1192 switch (ether_type) {
1195 if ((m->m_flags & M_LENCHECKED) == 0) {
1196 if (!ip_lengthcheck(&m, 0))
1199 if (ipflow_fastforward(m))
1205 if (ifp->if_flags & IFF_NOARP) {
1206 /* Discard packet if ARP is disabled on interface */
1215 case ETHERTYPE_IPV6:
1224 * Hold BGL and recheck ef_inputp
1227 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1239 isr = NETISR_ATALK1;
1241 case ETHERTYPE_AARP:
1247 case ETHERTYPE_MPLS:
1248 case ETHERTYPE_MPLS_MCAST:
1249 /* Should have been set by ether_input_chain(). */
1250 KKASSERT(m->m_flags & M_MPLSLABELED);
1257 * The accurate msgport is not determined before
1258 * we reach here, so recharacterize packet.
1260 m->m_flags &= ~M_HASH;
1264 * Hold BGL and recheck ef_inputp
1267 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1275 if (ether_type > ETHERMTU)
1277 l = mtod(m, struct llc *);
1278 if (l->llc_dsap == LLC_SNAP_LSAP &&
1279 l->llc_ssap == LLC_SNAP_LSAP &&
1280 l->llc_control == LLC_UI) {
1281 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1282 sizeof at_org_code) == 0 &&
1283 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1284 m_adj(m, sizeof(struct llc));
1285 isr = NETISR_ATALK2;
1288 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1289 sizeof aarp_org_code) == 0 &&
1290 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1291 m_adj(m, sizeof(struct llc));
1298 if (ng_ether_input_orphan_p != NULL) {
1300 * Hold BGL and recheck ng_ether_input_orphan_p
1303 if (ng_ether_input_orphan_p != NULL) {
1304 ng_ether_input_orphan_p(ifp, m, eh);
1314 if (m->m_flags & M_HASH) {
1315 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1316 netisr_handle(isr, m);
1320 * XXX Something is wrong,
1321 * we probably should panic here!
1323 m->m_flags &= ~M_HASH;
1324 ether_input_wronghash++;
1328 ether_input_requeue++;
1330 netisr_queue(isr, m);
1334 * First we perform any link layer operations, then continue to the
1335 * upper layers with ether_demux_oncpu().
1338 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1340 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1342 * Receiving interface's flags are changed, when this
1343 * packet is waiting for processing; discard it.
1350 * Tap the packet off here for a bridge. bridge_input()
1351 * will return NULL if it has consumed the packet, otherwise
1352 * it gets processed as normal. Note that bridge_input()
1353 * will always return the original packet if we need to
1354 * process it locally.
1356 if (ifp->if_bridge) {
1357 KASSERT(bridge_input_p != NULL,
1358 ("%s: if_bridge not loaded!", __func__));
1360 if(m->m_flags & M_ETHER_BRIDGED) {
1361 m->m_flags &= ~M_ETHER_BRIDGED;
1363 m = bridge_input_p(ifp, m);
1367 KASSERT(ifp == m->m_pkthdr.rcvif,
1368 ("bridge_input_p changed rcvif\n"));
1372 /* Handle ng_ether(4) processing, if any */
1373 if (ng_ether_input_p != NULL) {
1375 * Hold BGL and recheck ng_ether_input_p
1378 if (ng_ether_input_p != NULL)
1379 ng_ether_input_p(ifp, &m);
1386 /* Continue with upper layer processing */
1387 ether_demux_oncpu(ifp, m);
1391 * Perform certain functions of ether_input_chain():
1393 * - Update statistics
1394 * - Run bpf(4) tap if requested
1395 * Then pass the packet to ether_input_oncpu().
1397 * This function should be used by pseudo interface (e.g. vlan(4)),
1398 * when it tries to claim that the packet is received by it.
1404 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1406 /* Discard packet if interface is not up */
1407 if (!(ifp->if_flags & IFF_UP)) {
1413 * Change receiving interface. The bridge will often pass a flag to
1414 * ask that this not be done so ARPs get applied to the correct
1417 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1418 m->m_pkthdr.rcvif == NULL) {
1419 m->m_pkthdr.rcvif = ifp;
1422 /* Update statistics */
1424 ifp->if_ibytes += m->m_pkthdr.len;
1425 if (m->m_flags & (M_MCAST | M_BCAST))
1428 if (reinput_flags & REINPUT_RUNBPF)
1431 ether_input_oncpu(ifp, m);
1434 static __inline boolean_t
1435 ether_vlancheck(struct mbuf **m0)
1437 struct mbuf *m = *m0;
1438 struct ether_header *eh;
1439 uint16_t ether_type;
1441 eh = mtod(m, struct ether_header *);
1442 ether_type = ntohs(eh->ether_type);
1444 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1446 * Extract vlan tag if hardware does not do it for us
1448 vlan_ether_decap(&m);
1452 eh = mtod(m, struct ether_header *);
1453 ether_type = ntohs(eh->ether_type);
1456 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1458 * To prevent possible dangerous recursion,
1459 * we don't do vlan-in-vlan
1461 m->m_pkthdr.rcvif->if_noproto++;
1464 KKASSERT(ether_type != ETHERTYPE_VLAN);
1466 m->m_flags |= M_ETHER_VLANCHECKED;
1477 ether_input_handler(netmsg_t nmsg)
1479 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1480 struct ether_header *eh;
1486 ifp = m->m_pkthdr.rcvif;
1488 eh = mtod(m, struct ether_header *);
1489 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1490 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1491 ifp->if_addrlen) == 0)
1492 m->m_flags |= M_BCAST;
1494 m->m_flags |= M_MCAST;
1498 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1499 if (!ether_vlancheck(&m)) {
1500 KKASSERT(m == NULL);
1505 ether_input_oncpu(ifp, m);
1509 * Send the packet to the target msgport or queue it into 'chain'.
1511 * At this point the packet had better be characterized (M_HASH set),
1512 * so we know which cpu to send it to.
1515 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1517 struct netmsg_packet *pmsg;
1519 KKASSERT(m->m_flags & M_HASH);
1520 pmsg = &m->m_hdr.mh_netmsg;
1521 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1522 0, ether_input_handler);
1523 pmsg->nm_packet = m;
1524 pmsg->base.lmsg.u.ms_result = isr;
1526 if (chain != NULL) {
1527 int cpuid = m->m_pkthdr.hash;
1528 struct mbuf_chain *c;
1531 if (c->mc_head == NULL) {
1532 c->mc_head = c->mc_tail = m;
1534 c->mc_tail->m_nextpkt = m;
1537 m->m_nextpkt = NULL;
1539 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1544 * Process a received Ethernet packet.
1546 * The ethernet header is assumed to be in the mbuf so the caller
1547 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1548 * bytes in the first mbuf.
1550 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1551 * immediately. This situation happens when ether_input_chain is
1552 * accessed through ifnet.if_input.
1554 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1555 * bucket indexed by the target msgport's cpuid and the target msgport
1556 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1557 * must initialize 'chain' by calling ether_input_chain_init().
1558 * ether_input_dispatch must be called later to send ether frames
1559 * queued on 'chain' to their target msgport.
1562 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1563 struct mbuf_chain *chain)
1569 /* Discard packet if interface is not up */
1570 if (!(ifp->if_flags & IFF_UP)) {
1575 if (m->m_len < sizeof(struct ether_header)) {
1576 /* XXX error in the caller. */
1581 m->m_pkthdr.rcvif = ifp;
1583 logether(chain_beg, ifp);
1585 ETHER_BPF_MTAP(ifp, m);
1587 ifp->if_ibytes += m->m_pkthdr.len;
1589 if (ifp->if_flags & IFF_MONITOR) {
1590 struct ether_header *eh;
1592 eh = mtod(m, struct ether_header *);
1593 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1597 * Interface marked for monitoring; discard packet.
1601 logether(chain_end, ifp);
1606 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1607 * we can dispatch it immediately without further inspection.
1609 if (pi != NULL && (m->m_flags & M_HASH)) {
1611 ether_pktinfo_try++;
1613 netisr_hashcheck(pi->pi_netisr, m, pi);
1614 if (m->m_flags & M_HASH) {
1615 ether_dispatch(pi->pi_netisr, m, chain);
1617 ether_pktinfo_hit++;
1619 logether(chain_end, ifp);
1624 else if (ifp->if_capenable & IFCAP_RSS) {
1633 * Packet hash will be recalculated by software,
1634 * so clear the M_HASH flag set by the driver;
1635 * the hash value calculated by the hardware may
1636 * not be exactly what we want.
1638 m->m_flags &= ~M_HASH;
1640 if (!ether_vlancheck(&m)) {
1641 KKASSERT(m == NULL);
1642 logether(chain_end, ifp);
1646 isr = ether_characterize(&m);
1648 logether(chain_end, ifp);
1653 * Finally dispatch it
1655 ether_dispatch(isr, m, chain);
1657 logether(chain_end, ifp);
1661 ether_characterize(struct mbuf **m0)
1663 struct mbuf *m = *m0;
1664 struct ether_header *eh;
1665 uint16_t ether_type;
1668 eh = mtod(m, struct ether_header *);
1669 ether_type = ntohs(eh->ether_type);
1672 * Map ether type to netisr id.
1674 switch (ether_type) {
1686 case ETHERTYPE_IPV6:
1699 isr = NETISR_ATALK1;
1701 case ETHERTYPE_AARP:
1707 case ETHERTYPE_MPLS:
1708 case ETHERTYPE_MPLS_MCAST:
1709 m->m_flags |= M_MPLSLABELED;
1716 * NETISR_MAX is an invalid value; it is chosen to let
1717 * netisr_characterize() know that we have no clear
1718 * idea where this packet should go.
1725 * Ask the isr to characterize the packet since we couldn't.
1726 * This is an attempt to optimally get us onto the correct protocol
1729 netisr_characterize(isr, &m, sizeof(struct ether_header));
1736 ether_demux_handler(netmsg_t nmsg)
1738 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1744 ifp = m->m_pkthdr.rcvif;
1746 ether_demux_oncpu(ifp, m);
1750 ether_demux(struct mbuf *m)
1752 struct netmsg_packet *pmsg;
1755 isr = ether_characterize(&m);
1759 KKASSERT(m->m_flags & M_HASH);
1760 pmsg = &m->m_hdr.mh_netmsg;
1761 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1762 0, ether_demux_handler);
1763 pmsg->nm_packet = m;
1764 pmsg->base.lmsg.u.ms_result = isr;
1766 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1769 MODULE_VERSION(ether, 1);