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 <netns/ns.h>
102 #include <netns/ns_if.h>
104 int ether_outputdebug = 0;
105 int ether_inputdebug = 0;
109 #include <netproto/atalk/at.h>
110 #include <netproto/atalk/at_var.h>
111 #include <netproto/atalk/at_extern.h>
113 #define llc_snap_org_code llc_un.type_snap.org_code
114 #define llc_snap_ether_type llc_un.type_snap.ether_type
116 extern u_char at_org_code[3];
117 extern u_char aarp_org_code[3];
118 #endif /* NETATALK */
121 #include <netproto/mpls/mpls.h>
124 /* netgraph node hooks for ng_ether(4) */
125 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
126 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
127 struct mbuf *m, const struct ether_header *eh);
128 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
129 void (*ng_ether_attach_p)(struct ifnet *ifp);
130 void (*ng_ether_detach_p)(struct ifnet *ifp);
132 void (*vlan_input_p)(struct mbuf *);
134 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
136 static void ether_restore_header(struct mbuf **, const struct ether_header *,
137 const struct ether_header *);
142 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
143 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
144 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
146 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
149 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
153 #define gotoerr(e) do { error = (e); goto bad; } while (0)
154 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
156 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
158 const struct ether_header *eh);
160 static int ether_ipfw;
161 static u_int ether_restore_hdr;
162 static u_int ether_prepend_hdr;
165 static u_int ether_pktinfo_try;
166 static u_int ether_pktinfo_hit;
167 static u_int ether_rss_nopi;
168 static u_int ether_rss_nohash;
171 SYSCTL_DECL(_net_link);
172 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
173 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
174 ðer_ipfw, 0, "Pass ether pkts through firewall");
175 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
176 ðer_restore_hdr, 0, "# of ether header restoration");
177 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
178 ðer_prepend_hdr, 0,
179 "# of ether header restoration which prepends mbuf");
181 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
182 ðer_rss_nopi, 0, "# of packets do not have pktinfo");
183 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
184 ðer_rss_nohash, 0, "# of packets do not have hash");
185 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
186 ðer_pktinfo_try, 0,
187 "# of tries to find packets' msgport using pktinfo");
188 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
189 ðer_pktinfo_hit, 0,
190 "# of packets whose msgport are found using pktinfo");
193 #define ETHER_KTR_STR "ifp=%p"
194 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
196 #define KTR_ETHERNET KTR_ALL
198 KTR_INFO_MASTER(ether);
199 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
200 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
201 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
202 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
203 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
206 * Ethernet output routine.
207 * Encapsulate a packet of type family for the local net.
208 * Use trailer local net encapsulation if enough data in first
209 * packet leaves a multiple of 512 bytes of data in remainder.
210 * Assumes that ifp is actually pointer to arpcom structure.
213 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
216 struct ether_header *eh, *deh;
219 int hlen = ETHER_HDR_LEN; /* link layer header length */
220 struct arpcom *ac = IFP2AC(ifp);
223 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
225 if (ifp->if_flags & IFF_MONITOR)
227 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
230 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
233 eh = mtod(m, struct ether_header *);
234 edst = eh->ether_dhost;
237 * Fill in the destination ethernet address and frame type.
239 switch (dst->sa_family) {
242 if (!arpresolve(ifp, rt, m, dst, edst))
243 return (0); /* if not yet resolved */
245 if (m->m_flags & M_MPLSLABELED)
246 eh->ether_type = htons(ETHERTYPE_MPLS);
249 eh->ether_type = htons(ETHERTYPE_IP);
254 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
255 return (0); /* Something bad happenned. */
256 eh->ether_type = htons(ETHERTYPE_IPV6);
261 if (ef_outputp != NULL) {
263 * Hold BGL and recheck ef_outputp
266 if (ef_outputp != NULL) {
267 error = ef_outputp(ifp, &m, dst,
268 &eh->ether_type, &hlen);
277 eh->ether_type = htons(ETHERTYPE_IPX);
278 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
279 edst, ETHER_ADDR_LEN);
284 struct at_ifaddr *aa;
291 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
297 * In the phase 2 case, need to prepend an mbuf for
298 * the llc header. Since we must preserve the value
299 * of m, which is passed to us by value, we m_copy()
300 * the first mbuf, and use it for our llc header.
302 if (aa->aa_flags & AFA_PHASE2) {
305 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
306 eh = mtod(m, struct ether_header *);
307 edst = eh->ether_dhost;
308 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
309 llc.llc_control = LLC_UI;
310 bcopy(at_org_code, llc.llc_snap_org_code,
312 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
314 mtod(m, caddr_t) + sizeof(struct ether_header),
316 eh->ether_type = htons(m->m_pkthdr.len);
317 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
319 eh->ether_type = htons(ETHERTYPE_AT);
321 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
334 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
335 eh->ether_type = 0x8137;
337 case 0x0: /* Novell 802.3 */
338 eh->ether_type = htons(m->m_pkthdr.len);
340 case 0xe0e0: /* Novell 802.2 and Token-Ring */
341 M_PREPEND(m, 3, MB_DONTWAIT);
342 eh = mtod(m, struct ether_header *);
343 edst = eh->ether_dhost;
344 eh->ether_type = htons(m->m_pkthdr.len);
345 cp = mtod(m, u_char *) + sizeof(struct ether_header);
351 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
354 * XXX if ns_thishost is the same as the node's ethernet
355 * address then just the default code will catch this anyhow.
356 * So I'm not sure if this next clause should be here at all?
359 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
360 m->m_pkthdr.rcvif = ifp;
361 netisr_dispatch(NETISR_NS, m);
364 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
365 m->m_flags |= M_BCAST;
368 case pseudo_AF_HDRCMPLT:
370 loop_copy = -1; /* if this is for us, don't do it */
371 deh = (struct ether_header *)dst->sa_data;
372 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
373 eh->ether_type = deh->ether_type;
377 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
378 gotoerr(EAFNOSUPPORT);
381 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
382 memcpy(eh->ether_shost,
383 ((struct ether_header *)dst->sa_data)->ether_shost,
386 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
389 * Bridges require special output handling.
391 if (ifp->if_bridge) {
392 KASSERT(bridge_output_p != NULL,
393 ("%s: if_bridge not loaded!", __func__));
394 return bridge_output_p(ifp, m);
398 * If a simplex interface, and the packet is being sent to our
399 * Ethernet address or a broadcast address, loopback a copy.
400 * XXX To make a simplex device behave exactly like a duplex
401 * device, we should copy in the case of sending to our own
402 * ethernet address (thus letting the original actually appear
403 * on the wire). However, we don't do that here for security
404 * reasons and compatibility with the original behavior.
406 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
409 if (m->m_pkthdr.csum_flags & CSUM_IP)
410 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
411 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
412 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
413 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
416 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
417 n->m_pkthdr.csum_flags |= csum_flags;
418 if (csum_flags & CSUM_DATA_VALID)
419 n->m_pkthdr.csum_data = 0xffff;
420 if_simloop(ifp, n, dst->sa_family, hlen);
423 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
424 ETHER_ADDR_LEN) == 0) {
425 m->m_pkthdr.csum_flags |= csum_flags;
426 if (csum_flags & CSUM_DATA_VALID)
427 m->m_pkthdr.csum_data = 0xffff;
428 if_simloop(ifp, m, dst->sa_family, hlen);
429 return (0); /* XXX */
436 * Hold BGL and recheck ifp->if_carp
439 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
448 /* Handle ng_ether(4) processing, if any */
449 if (ng_ether_output_p != NULL) {
451 * Hold BGL and recheck ng_ether_output_p
454 if (ng_ether_output_p != NULL) {
455 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
467 /* Continue with link-layer output */
468 return ether_output_frame(ifp, m);
476 * Ethernet link layer output routine to send a raw frame to the device.
478 * This assumes that the 14 byte Ethernet header is present and contiguous
482 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
484 struct ip_fw *rule = NULL;
486 struct altq_pktattr pktattr;
488 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
490 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
493 /* Extract info from dummynet tag */
494 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
495 KKASSERT(mtag != NULL);
496 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
497 KKASSERT(rule != NULL);
499 m_tag_delete(m, mtag);
500 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
503 if (ifq_is_enabled(&ifp->if_snd))
504 altq_etherclassify(&ifp->if_snd, m, &pktattr);
506 if (IPFW_LOADED && ether_ipfw != 0) {
507 struct ether_header save_eh, *eh;
509 eh = mtod(m, struct ether_header *);
511 m_adj(m, ETHER_HDR_LEN);
512 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
516 return ENOBUFS; /* pkt dropped */
518 return 0; /* consumed e.g. in a pipe */
521 /* packet was ok, restore the ethernet header */
522 ether_restore_header(&m, eh, &save_eh);
531 * Queue message on interface, update output statistics if
532 * successful, and start output if interface not yet active.
534 error = ifq_dispatch(ifp, m, &pktattr);
539 * ipfw processing for ethernet packets (in and out).
540 * The second parameter is NULL from ether_demux(), and ifp from
541 * ether_output_frame().
544 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
545 const struct ether_header *eh)
547 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
548 struct ip_fw_args args;
553 if (*rule != NULL && fw_one_pass)
554 return TRUE; /* dummynet packet, already partially processed */
557 * I need some amount of data to be contiguous.
559 i = min((*m0)->m_pkthdr.len, max_protohdr);
560 if ((*m0)->m_len < i) {
561 *m0 = m_pullup(*m0, i);
569 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
570 m_tag_delete(*m0, mtag);
571 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
572 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
573 KKASSERT(mtag != NULL);
574 m_tag_delete(*m0, mtag);
575 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
578 args.m = *m0; /* the packet we are looking at */
579 args.oif = dst; /* destination, if any */
580 args.rule = *rule; /* matching rule to restart */
581 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
582 i = ip_fw_chk_ptr(&args);
597 * XXX at some point add support for divert/forward actions.
598 * If none of the above matches, we have to drop the pkt.
604 * Pass the pkt to dummynet, which consumes it.
606 m = *m0; /* pass the original to dummynet */
607 *m0 = NULL; /* and nothing back to the caller */
609 ether_restore_header(&m, eh, &save_eh);
613 ip_fw_dn_io_ptr(m, args.cookie,
614 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
619 panic("unknown ipfw return value: %d\n", i);
624 ether_input(struct ifnet *ifp, struct mbuf *m)
626 ether_input_chain(ifp, m, NULL, NULL);
630 * Perform common duties while attaching to interface list
633 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
635 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
640 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
641 lwkt_serialize_t serializer)
643 struct sockaddr_dl *sdl;
645 ifp->if_type = IFT_ETHER;
646 ifp->if_addrlen = ETHER_ADDR_LEN;
647 ifp->if_hdrlen = ETHER_HDR_LEN;
648 if_attach(ifp, serializer);
649 ifp->if_mtu = ETHERMTU;
650 if (ifp->if_baudrate == 0)
651 ifp->if_baudrate = 10000000;
652 ifp->if_output = ether_output;
653 ifp->if_input = ether_input;
654 ifp->if_resolvemulti = ether_resolvemulti;
655 ifp->if_broadcastaddr = etherbroadcastaddr;
656 sdl = IF_LLSOCKADDR(ifp);
657 sdl->sdl_type = IFT_ETHER;
658 sdl->sdl_alen = ifp->if_addrlen;
659 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
661 * XXX Keep the current drivers happy.
662 * XXX Remove once all drivers have been cleaned up
664 if (lla != IFP2AC(ifp)->ac_enaddr)
665 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
666 bpfattach(ifp, dlt, hdrlen);
667 if (ng_ether_attach_p != NULL)
668 (*ng_ether_attach_p)(ifp);
670 if_printf(ifp, "MAC address: %6D\n", lla, ":");
674 * Perform common duties while detaching an Ethernet interface
677 ether_ifdetach(struct ifnet *ifp)
681 if (ng_ether_detach_p != NULL)
682 (*ng_ether_detach_p)(ifp);
688 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
690 struct ifaddr *ifa = (struct ifaddr *) data;
691 struct ifreq *ifr = (struct ifreq *) data;
694 #define IF_INIT(ifp) \
696 if (((ifp)->if_flags & IFF_UP) == 0) { \
697 (ifp)->if_flags |= IFF_UP; \
698 (ifp)->if_init((ifp)->if_softc); \
702 ASSERT_IFNET_SERIALIZED_ALL(ifp);
706 switch (ifa->ifa_addr->sa_family) {
709 IF_INIT(ifp); /* before arpwhohas */
710 arp_ifinit(ifp, ifa);
715 * XXX - This code is probably wrong
719 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
720 struct arpcom *ac = IFP2AC(ifp);
722 if (ipx_nullhost(*ina))
723 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
725 bcopy(ina->x_host.c_host, ac->ac_enaddr,
726 sizeof ac->ac_enaddr);
728 IF_INIT(ifp); /* Set new address. */
734 * XXX - This code is probably wrong
738 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
739 struct arpcom *ac = IFP2AC(ifp);
741 if (ns_nullhost(*ina))
742 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
744 bcopy(ina->x_host.c_host, ac->ac_enaddr,
745 sizeof ac->ac_enaddr);
761 bcopy(IFP2AC(ifp)->ac_enaddr,
762 ((struct sockaddr *)ifr->ifr_data)->sa_data,
768 * Set the interface MTU.
770 if (ifr->ifr_mtu > ETHERMTU) {
773 ifp->if_mtu = ifr->ifr_mtu;
788 struct sockaddr **llsa,
791 struct sockaddr_dl *sdl;
792 struct sockaddr_in *sin;
794 struct sockaddr_in6 *sin6;
798 switch(sa->sa_family) {
801 * No mapping needed. Just check that it's a valid MC address.
803 sdl = (struct sockaddr_dl *)sa;
804 e_addr = LLADDR(sdl);
805 if ((e_addr[0] & 1) != 1)
806 return EADDRNOTAVAIL;
812 sin = (struct sockaddr_in *)sa;
813 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
814 return EADDRNOTAVAIL;
815 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
817 sdl->sdl_len = sizeof *sdl;
818 sdl->sdl_family = AF_LINK;
819 sdl->sdl_index = ifp->if_index;
820 sdl->sdl_type = IFT_ETHER;
821 sdl->sdl_alen = ETHER_ADDR_LEN;
822 e_addr = LLADDR(sdl);
823 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
824 *llsa = (struct sockaddr *)sdl;
829 sin6 = (struct sockaddr_in6 *)sa;
830 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
832 * An IP6 address of 0 means listen to all
833 * of the Ethernet multicast address used for IP6.
834 * (This is used for multicast routers.)
836 ifp->if_flags |= IFF_ALLMULTI;
840 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
841 return EADDRNOTAVAIL;
842 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
844 sdl->sdl_len = sizeof *sdl;
845 sdl->sdl_family = AF_LINK;
846 sdl->sdl_index = ifp->if_index;
847 sdl->sdl_type = IFT_ETHER;
848 sdl->sdl_alen = ETHER_ADDR_LEN;
849 e_addr = LLADDR(sdl);
850 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
851 *llsa = (struct sockaddr *)sdl;
857 * Well, the text isn't quite right, but it's the name
866 * This is for reference. We have a table-driven version
867 * of the little-endian crc32 generator, which is faster
868 * than the double-loop.
871 ether_crc32_le(const uint8_t *buf, size_t len)
873 uint32_t c, crc, carry;
876 crc = 0xffffffffU; /* initial value */
878 for (i = 0; i < len; i++) {
880 for (j = 0; j < 8; j++) {
881 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
885 crc = (crc ^ ETHER_CRC_POLY_LE);
893 ether_crc32_le(const uint8_t *buf, size_t len)
895 static const uint32_t crctab[] = {
896 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
897 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
898 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
899 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
904 crc = 0xffffffffU; /* initial value */
906 for (i = 0; i < len; i++) {
908 crc = (crc >> 4) ^ crctab[crc & 0xf];
909 crc = (crc >> 4) ^ crctab[crc & 0xf];
917 ether_crc32_be(const uint8_t *buf, size_t len)
919 uint32_t c, crc, carry;
922 crc = 0xffffffffU; /* initial value */
924 for (i = 0; i < len; i++) {
926 for (j = 0; j < 8; j++) {
927 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
931 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
939 * find the size of ethernet header, and call classifier
942 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
943 struct altq_pktattr *pktattr)
945 struct ether_header *eh;
947 int hlen, af, hdrsize;
950 hlen = sizeof(struct ether_header);
951 eh = mtod(m, struct ether_header *);
953 ether_type = ntohs(eh->ether_type);
954 if (ether_type < ETHERMTU) {
956 struct llc *llc = (struct llc *)(eh + 1);
959 if (m->m_len < hlen ||
960 llc->llc_dsap != LLC_SNAP_LSAP ||
961 llc->llc_ssap != LLC_SNAP_LSAP ||
962 llc->llc_control != LLC_UI)
963 goto bad; /* not snap! */
965 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
968 if (ether_type == ETHERTYPE_IP) {
970 hdrsize = 20; /* sizeof(struct ip) */
972 } else if (ether_type == ETHERTYPE_IPV6) {
974 hdrsize = 40; /* sizeof(struct ip6_hdr) */
979 while (m->m_len <= hlen) {
983 hdr = m->m_data + hlen;
984 if (m->m_len < hlen + hdrsize) {
986 * ip header is not in a single mbuf. this should not
987 * happen in the current code.
988 * (todo: use m_pulldown in the future)
994 ifq_classify(ifq, m, af, pktattr);
1001 pktattr->pattr_class = NULL;
1002 pktattr->pattr_hdr = NULL;
1003 pktattr->pattr_af = AF_UNSPEC;
1007 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1008 const struct ether_header *save_eh)
1010 struct mbuf *m = *m0;
1012 ether_restore_hdr++;
1015 * Prepend the header, optimize for the common case of
1016 * eh pointing into the mbuf.
1018 if ((const void *)(eh + 1) == (void *)m->m_data) {
1019 m->m_data -= ETHER_HDR_LEN;
1020 m->m_len += ETHER_HDR_LEN;
1021 m->m_pkthdr.len += ETHER_HDR_LEN;
1023 ether_prepend_hdr++;
1025 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1027 bcopy(save_eh, mtod(m, struct ether_header *),
1035 ether_input_ipifunc(void *arg)
1037 struct mbuf *m, *next;
1042 next = m->m_nextpkt;
1043 m->m_nextpkt = NULL;
1045 port = m->m_pkthdr.header;
1046 m->m_pkthdr.header = NULL;
1049 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1052 } while (m != NULL);
1056 ether_input_dispatch(struct mbuf_chain *chain)
1061 logether(disp_beg, NULL);
1062 for (i = 0; i < ncpus; ++i) {
1063 if (chain[i].mc_head != NULL) {
1064 lwkt_send_ipiq(globaldata_find(i),
1065 ether_input_ipifunc, chain[i].mc_head);
1069 logether(disp_beg, NULL);
1070 if (chain->mc_head != NULL)
1071 ether_input_ipifunc(chain->mc_head);
1073 logether(disp_end, NULL);
1077 ether_input_chain_init(struct mbuf_chain *chain)
1082 for (i = 0; i < ncpus; ++i)
1083 chain[i].mc_head = chain[i].mc_tail = NULL;
1085 chain->mc_head = chain->mc_tail = NULL;
1090 * Upper layer processing for a received Ethernet packet.
1093 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1095 struct ether_header *eh;
1096 int isr, redispatch, discard = 0;
1098 struct ip_fw *rule = NULL;
1104 KASSERT(m->m_len >= ETHER_HDR_LEN,
1105 ("ether header is no contiguous!\n"));
1107 eh = mtod(m, struct ether_header *);
1109 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1112 /* Extract info from dummynet tag */
1113 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1114 KKASSERT(mtag != NULL);
1115 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1116 KKASSERT(rule != NULL);
1118 m_tag_delete(m, mtag);
1119 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1121 /* packet is passing the second time */
1127 * XXX: Okay, we need to call carp_forus() and - if it is for
1128 * us jump over code that does the normal check
1129 * "ac_enaddr == ether_dhost". The check sequence is a bit
1130 * different from OpenBSD, so we jump over as few code as
1131 * possible, to catch _all_ sanity checks. This needs
1132 * evaluation, to see if the carp ether_dhost values break any
1137 * Hold BGL and recheck ifp->if_carp
1140 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1149 * We got a packet which was unicast to a different Ethernet
1150 * address. If the driver is working properly, then this
1151 * situation can only happen when the interface is in
1152 * promiscuous mode. We defer the packet discarding until the
1153 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1156 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1157 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1158 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN))
1162 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1163 struct ether_header save_eh = *eh;
1165 /* XXX old crufty stuff, needs to be removed */
1166 m_adj(m, sizeof(struct ether_header));
1168 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1173 ether_restore_header(&m, eh, &save_eh);
1176 eh = mtod(m, struct ether_header *);
1179 ether_type = ntohs(eh->ether_type);
1180 KKASSERT(ether_type != ETHERTYPE_VLAN);
1182 if (m->m_flags & M_VLANTAG) {
1183 void (*vlan_input_func)(struct mbuf *);
1185 vlan_input_func = vlan_input_p;
1186 if (vlan_input_func != NULL) {
1189 m->m_pkthdr.rcvif->if_noproto++;
1196 * If we have been asked to discard this packet
1197 * (e.g. not for us), drop it before entering
1206 * Clear protocol specific flags,
1207 * before entering the upper layer.
1209 m->m_flags &= ~M_ETHER_FLAGS;
1211 /* Strip ethernet header. */
1212 m_adj(m, sizeof(struct ether_header));
1215 * By default, we don't need to do the redispatch; for the
1216 * most common packet types, e.g. IPv4, ether_input_chain()
1217 * has already picked up the correct target network msgport.
1221 switch (ether_type) {
1224 if ((m->m_flags & M_LENCHECKED) == 0) {
1225 if (!ip_lengthcheck(&m))
1228 if (ipflow_fastforward(m))
1234 if (ifp->if_flags & IFF_NOARP) {
1235 /* Discard packet if ARP is disabled on interface */
1244 case ETHERTYPE_IPV6:
1253 * Hold BGL and recheck ef_inputp
1256 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1267 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1275 isr = NETISR_ATALK1;
1277 case ETHERTYPE_AARP:
1283 case ETHERTYPE_MPLS:
1284 case ETHERTYPE_MPLS_MCAST:
1285 /* Should have been set by ether_input_chain(). */
1286 KKASSERT(m->m_flags & M_MPLSLABELED);
1293 * The accurate msgport is not determined before
1294 * we reach here, so redo the dispatching
1300 * Hold BGL and recheck ef_inputp
1303 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1311 checksum = mtod(m, ushort *);
1313 if ((ether_type <= ETHERMTU) &&
1314 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1315 if (*checksum == 0xE0E0) {
1316 m->m_pkthdr.len -= 3;
1325 if (ether_type > ETHERMTU)
1327 l = mtod(m, struct llc *);
1328 if (l->llc_dsap == LLC_SNAP_LSAP &&
1329 l->llc_ssap == LLC_SNAP_LSAP &&
1330 l->llc_control == LLC_UI) {
1331 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1332 sizeof at_org_code) == 0 &&
1333 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1334 m_adj(m, sizeof(struct llc));
1335 isr = NETISR_ATALK2;
1338 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1339 sizeof aarp_org_code) == 0 &&
1340 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1341 m_adj(m, sizeof(struct llc));
1348 if (ng_ether_input_orphan_p != NULL) {
1350 * Hold BGL and recheck ng_ether_input_orphan_p
1353 if (ng_ether_input_orphan_p != NULL) {
1354 ng_ether_input_orphan_p(ifp, m, eh);
1367 netisr_dispatch(isr, m);
1371 * First we perform any link layer operations, then continue to the
1372 * upper layers with ether_demux_oncpu().
1375 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1377 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1379 * Receiving interface's flags are changed, when this
1380 * packet is waiting for processing; discard it.
1387 * Tap the packet off here for a bridge. bridge_input()
1388 * will return NULL if it has consumed the packet, otherwise
1389 * it gets processed as normal. Note that bridge_input()
1390 * will always return the original packet if we need to
1391 * process it locally.
1393 if (ifp->if_bridge) {
1394 KASSERT(bridge_input_p != NULL,
1395 ("%s: if_bridge not loaded!", __func__));
1397 if(m->m_flags & M_ETHER_BRIDGED) {
1398 m->m_flags &= ~M_ETHER_BRIDGED;
1400 m = bridge_input_p(ifp, m);
1404 KASSERT(ifp == m->m_pkthdr.rcvif,
1405 ("bridge_input_p changed rcvif\n"));
1409 /* Handle ng_ether(4) processing, if any */
1410 if (ng_ether_input_p != NULL) {
1412 * Hold BGL and recheck ng_ether_input_p
1415 if (ng_ether_input_p != NULL)
1416 ng_ether_input_p(ifp, &m);
1423 /* Continue with upper layer processing */
1424 ether_demux_oncpu(ifp, m);
1428 * Perform certain functions of ether_input_chain():
1430 * - Update statistics
1431 * - Run bpf(4) tap if requested
1432 * Then pass the packet to ether_input_oncpu().
1434 * This function should be used by pseudo interface (e.g. vlan(4)),
1435 * when it tries to claim that the packet is received by it.
1438 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int run_bpf)
1440 /* Discard packet if interface is not up */
1441 if (!(ifp->if_flags & IFF_UP)) {
1446 /* Change receiving interface */
1447 m->m_pkthdr.rcvif = ifp;
1449 /* Update statistics */
1451 ifp->if_ibytes += m->m_pkthdr.len;
1452 if (m->m_flags & (M_MCAST | M_BCAST))
1458 ether_input_oncpu(ifp, m);
1461 static __inline boolean_t
1462 ether_vlancheck(struct mbuf **m0)
1464 struct mbuf *m = *m0;
1465 struct ether_header *eh;
1466 uint16_t ether_type;
1468 eh = mtod(m, struct ether_header *);
1469 ether_type = ntohs(eh->ether_type);
1471 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1473 * Extract vlan tag if hardware does not do it for us
1475 vlan_ether_decap(&m);
1479 eh = mtod(m, struct ether_header *);
1480 ether_type = ntohs(eh->ether_type);
1483 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1485 * To prevent possible dangerous recursion,
1486 * we don't do vlan-in-vlan
1488 m->m_pkthdr.rcvif->if_noproto++;
1491 KKASSERT(ether_type != ETHERTYPE_VLAN);
1493 m->m_flags |= M_ETHER_VLANCHECKED;
1504 ether_input_handler(struct netmsg *nmsg)
1506 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1507 struct ether_header *eh;
1513 ifp = m->m_pkthdr.rcvif;
1515 eh = mtod(m, struct ether_header *);
1516 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1517 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1518 ifp->if_addrlen) == 0)
1519 m->m_flags |= M_BCAST;
1521 m->m_flags |= M_MCAST;
1525 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1526 if (!ether_vlancheck(&m)) {
1527 KKASSERT(m == NULL);
1532 ether_input_oncpu(ifp, m);
1535 static __inline void
1536 ether_init_netpacket(int num, struct mbuf *m)
1538 struct netmsg_packet *pmsg;
1540 pmsg = &m->m_hdr.mh_netmsg;
1541 netmsg_init(&pmsg->nm_netmsg, NULL, &netisr_apanic_rport,
1542 MSGF_MPSAFE, ether_input_handler);
1543 pmsg->nm_packet = m;
1544 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1547 static __inline struct lwkt_port *
1548 ether_mport(int num, struct mbuf **m)
1550 if (num == NETISR_MAX) {
1552 * All packets whose target msgports can't be
1553 * determined here are dispatched to netisr0,
1554 * where further dispatching may happen.
1556 return cpu_portfn(0);
1558 return netisr_find_port(num, m);
1562 * Send the packet to the target msgport or
1563 * queue it into 'chain'.
1566 ether_dispatch(int isr, struct lwkt_port *port, struct mbuf *m,
1567 struct mbuf_chain *chain)
1569 ether_init_netpacket(isr, m);
1571 if (chain != NULL) {
1572 struct mbuf_chain *c;
1575 m->m_pkthdr.header = port; /* XXX */
1576 cpuid = port->mpu_td->td_gd->gd_cpuid;
1579 if (c->mc_head == NULL) {
1580 c->mc_head = c->mc_tail = m;
1582 c->mc_tail->m_nextpkt = m;
1585 m->m_nextpkt = NULL;
1587 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1592 * Process a received Ethernet packet.
1594 * The ethernet header is assumed to be in the mbuf so the caller
1595 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1596 * bytes in the first mbuf.
1598 * We first try to find the target msgport for this ether frame, if
1599 * there is no target msgport for it, this ether frame is discarded,
1600 * else we do following processing according to whether 'chain' is
1602 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1603 * immediately. This situation happens when ether_input_chain is
1604 * accessed through ifnet.if_input.
1605 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1606 * bucket indexed by the target msgport's cpuid and the target msgport
1607 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1608 * must initialize 'chain' by calling ether_input_chain_init().
1609 * ether_input_dispatch must be called later to send ether frames
1610 * queued on 'chain' to their target msgport.
1613 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1614 struct mbuf_chain *chain)
1616 struct ether_header *eh, *save_eh, save_eh0;
1617 struct lwkt_port *port;
1618 uint16_t ether_type;
1623 /* Discard packet if interface is not up */
1624 if (!(ifp->if_flags & IFF_UP)) {
1629 if (m->m_len < sizeof(struct ether_header)) {
1630 /* XXX error in the caller. */
1635 m->m_pkthdr.rcvif = ifp;
1637 logether(chain_beg, ifp);
1639 ETHER_BPF_MTAP(ifp, m);
1641 ifp->if_ibytes += m->m_pkthdr.len;
1643 if (ifp->if_flags & IFF_MONITOR) {
1644 eh = mtod(m, struct ether_header *);
1645 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1649 * Interface marked for monitoring; discard packet.
1653 logether(chain_end, ifp);
1657 if (pi != NULL && (m->m_flags & M_HASH)) {
1659 ether_pktinfo_try++;
1661 /* Try finding the port using the packet info */
1662 port = netisr_find_pktinfo_port(pi, m);
1665 ether_pktinfo_hit++;
1667 ether_dispatch(pi->pi_netisr, port, m, chain);
1669 logether(chain_end, ifp);
1674 * The packet info does not contain enough
1675 * information, we will have to check the
1680 else if (ifp->if_capenable & IFCAP_RSS) {
1689 * Packet hash will be recalculated by software,
1690 * so clear the M_HASH flag set by the driver;
1691 * the hash value calculated by the hardware may
1692 * not be exactly what we want.
1694 m->m_flags &= ~M_HASH;
1696 if (!ether_vlancheck(&m)) {
1697 KKASSERT(m == NULL);
1698 logether(chain_end, ifp);
1701 eh = mtod(m, struct ether_header *);
1702 ether_type = ntohs(eh->ether_type);
1705 * Map ether type to netisr id.
1707 switch (ether_type) {
1719 case ETHERTYPE_IPV6:
1731 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1738 isr = NETISR_ATALK1;
1740 case ETHERTYPE_AARP:
1746 case ETHERTYPE_MPLS:
1747 case ETHERTYPE_MPLS_MCAST:
1748 m->m_flags |= M_MPLSLABELED;
1755 * NETISR_MAX is an invalid value; it is chosen to let
1756 * ether_mport() know that we are not able to decide
1757 * this packet's msgport here.
1764 * If the packet is in contiguous memory, following
1765 * m_adj() could ensure that the hidden ether header
1766 * will not be destroyed, else we will have to save
1767 * the ether header for the later restoration.
1769 if (m->m_pkthdr.len != m->m_len) {
1771 save_eh = &save_eh0;
1777 * Temporarily remove ether header; ether_mport()
1778 * expects a packet without ether header.
1780 m_adj(m, sizeof(struct ether_header));
1783 * Find the packet's target msgport.
1785 port = ether_mport(isr, &m);
1787 KKASSERT(m == NULL);
1788 logether(chain_end, ifp);
1793 * Restore ether header.
1795 if (save_eh != NULL) {
1796 ether_restore_header(&m, eh, save_eh);
1798 logether(chain_end, ifp);
1802 m->m_data -= ETHER_HDR_LEN;
1803 m->m_len += ETHER_HDR_LEN;
1804 m->m_pkthdr.len += ETHER_HDR_LEN;
1807 ether_dispatch(isr, port, m, chain);
1809 logether(chain_end, ifp);
1812 MODULE_VERSION(ether, 1);