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.88 2008/09/17 07:51:59 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
45 #include "opt_ethernet.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
58 #include <sys/thread2.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/if_llc.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/ifq_var.h>
68 #include <net/ethernet.h>
69 #include <net/vlan/if_vlan_ether.h>
70 #include <net/netmsg2.h>
72 #if defined(INET) || defined(INET6)
73 #include <netinet/in.h>
74 #include <netinet/in_var.h>
75 #include <netinet/if_ether.h>
76 #include <net/ipfw/ip_fw.h>
77 #include <net/dummynet/ip_dummynet.h>
80 #include <netinet6/nd6.h>
84 #include <netinet/ip_carp.h>
88 #include <netproto/ipx/ipx.h>
89 #include <netproto/ipx/ipx_if.h>
90 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
91 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
92 short *tp, int *hlen);
97 #include <netns/ns_if.h>
99 int ether_outputdebug = 0;
100 int ether_inputdebug = 0;
104 #include <netproto/atalk/at.h>
105 #include <netproto/atalk/at_var.h>
106 #include <netproto/atalk/at_extern.h>
108 #define llc_snap_org_code llc_un.type_snap.org_code
109 #define llc_snap_ether_type llc_un.type_snap.ether_type
111 extern u_char at_org_code[3];
112 extern u_char aarp_org_code[3];
113 #endif /* NETATALK */
116 #include <netproto/mpls/mpls.h>
119 /* netgraph node hooks for ng_ether(4) */
120 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
121 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
122 struct mbuf *m, const struct ether_header *eh);
123 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
124 void (*ng_ether_attach_p)(struct ifnet *ifp);
125 void (*ng_ether_detach_p)(struct ifnet *ifp);
127 void (*vlan_input_p)(struct mbuf *);
129 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
131 static void ether_restore_header(struct mbuf **, const struct ether_header *,
132 const struct ether_header *);
137 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
138 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
139 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
141 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
144 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
148 #define gotoerr(e) do { error = (e); goto bad; } while (0)
149 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
151 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
153 const struct ether_header *eh);
155 static int ether_ipfw;
156 static u_int ether_restore_hdr;
157 static u_int ether_prepend_hdr;
159 SYSCTL_DECL(_net_link);
160 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
161 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
162 ðer_ipfw, 0, "Pass ether pkts through firewall");
163 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
164 ðer_restore_hdr, 0, "# of ether header restoration");
165 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
166 ðer_prepend_hdr, 0,
167 "# of ether header restoration which prepends mbuf");
170 * Ethernet output routine.
171 * Encapsulate a packet of type family for the local net.
172 * Use trailer local net encapsulation if enough data in first
173 * packet leaves a multiple of 512 bytes of data in remainder.
174 * Assumes that ifp is actually pointer to arpcom structure.
177 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
180 struct ether_header *eh, *deh;
183 int hlen = ETHER_HDR_LEN; /* link layer header length */
184 struct arpcom *ac = IFP2AC(ifp);
187 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
189 if (ifp->if_flags & IFF_MONITOR)
191 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
194 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
197 eh = mtod(m, struct ether_header *);
198 edst = eh->ether_dhost;
201 * Fill in the destination ethernet address and frame type.
203 switch (dst->sa_family) {
206 if (!arpresolve(ifp, rt, m, dst, edst))
207 return (0); /* if not yet resolved */
209 if (m->m_flags & M_MPLSLABELED)
210 eh->ether_type = htons(ETHERTYPE_MPLS);
213 eh->ether_type = htons(ETHERTYPE_IP);
218 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
219 return (0); /* Something bad happenned. */
220 eh->ether_type = htons(ETHERTYPE_IPV6);
225 if (ef_outputp != NULL) {
226 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
231 eh->ether_type = htons(ETHERTYPE_IPX);
232 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
233 edst, ETHER_ADDR_LEN);
239 struct at_ifaddr *aa;
241 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
246 * In the phase 2 case, need to prepend an mbuf for
247 * the llc header. Since we must preserve the value
248 * of m, which is passed to us by value, we m_copy()
249 * the first mbuf, and use it for our llc header.
251 if (aa->aa_flags & AFA_PHASE2) {
254 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
255 eh = mtod(m, struct ether_header *);
256 edst = eh->ether_dhost;
257 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
258 llc.llc_control = LLC_UI;
259 bcopy(at_org_code, llc.llc_snap_org_code,
261 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
263 mtod(m, caddr_t) + sizeof(struct ether_header),
265 eh->ether_type = htons(m->m_pkthdr.len);
266 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
268 eh->ether_type = htons(ETHERTYPE_AT);
270 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
279 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
280 eh->ether_type = 0x8137;
282 case 0x0: /* Novell 802.3 */
283 eh->ether_type = htons(m->m_pkthdr.len);
285 case 0xe0e0: /* Novell 802.2 and Token-Ring */
286 M_PREPEND(m, 3, MB_DONTWAIT);
287 eh = mtod(m, struct ether_header *);
288 edst = eh->ether_dhost;
289 eh->ether_type = htons(m->m_pkthdr.len);
290 cp = mtod(m, u_char *) + sizeof(struct ether_header);
296 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
299 * XXX if ns_thishost is the same as the node's ethernet
300 * address then just the default code will catch this anyhow.
301 * So I'm not sure if this next clause should be here at all?
304 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
305 m->m_pkthdr.rcvif = ifp;
306 netisr_dispatch(NETISR_NS, m);
309 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
310 m->m_flags |= M_BCAST;
313 case pseudo_AF_HDRCMPLT:
315 loop_copy = -1; /* if this is for us, don't do it */
316 deh = (struct ether_header *)dst->sa_data;
317 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
318 eh->ether_type = deh->ether_type;
322 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
323 gotoerr(EAFNOSUPPORT);
326 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
327 memcpy(eh->ether_shost,
328 ((struct ether_header *)dst->sa_data)->ether_shost,
331 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
334 * Bridges require special output handling.
336 if (ifp->if_bridge) {
337 KASSERT(bridge_output_p != NULL,
338 ("%s: if_bridge not loaded!", __func__));
339 return bridge_output_p(ifp, m);
343 * If a simplex interface, and the packet is being sent to our
344 * Ethernet address or a broadcast address, loopback a copy.
345 * XXX To make a simplex device behave exactly like a duplex
346 * device, we should copy in the case of sending to our own
347 * ethernet address (thus letting the original actually appear
348 * on the wire). However, we don't do that here for security
349 * reasons and compatibility with the original behavior.
351 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
354 if (m->m_pkthdr.csum_flags & CSUM_IP)
355 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
356 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
357 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
358 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
361 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
362 n->m_pkthdr.csum_flags |= csum_flags;
363 if (csum_flags & CSUM_DATA_VALID)
364 n->m_pkthdr.csum_data = 0xffff;
365 if_simloop(ifp, n, dst->sa_family, hlen);
368 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
369 ETHER_ADDR_LEN) == 0) {
370 m->m_pkthdr.csum_flags |= csum_flags;
371 if (csum_flags & CSUM_DATA_VALID)
372 m->m_pkthdr.csum_data = 0xffff;
373 if_simloop(ifp, m, dst->sa_family, hlen);
374 return (0); /* XXX */
379 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
384 /* Handle ng_ether(4) processing, if any */
385 if (ng_ether_output_p != NULL) {
386 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
392 /* Continue with link-layer output */
393 return ether_output_frame(ifp, m);
401 * Ethernet link layer output routine to send a raw frame to the device.
403 * This assumes that the 14 byte Ethernet header is present and contiguous
407 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
409 struct ip_fw *rule = NULL;
411 struct altq_pktattr pktattr;
413 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
415 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
418 /* Extract info from dummynet tag */
419 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
420 KKASSERT(mtag != NULL);
421 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
422 KKASSERT(rule != NULL);
424 m_tag_delete(m, mtag);
425 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
428 if (ifq_is_enabled(&ifp->if_snd))
429 altq_etherclassify(&ifp->if_snd, m, &pktattr);
431 if (IPFW_LOADED && ether_ipfw != 0) {
432 struct ether_header save_eh, *eh;
434 eh = mtod(m, struct ether_header *);
436 m_adj(m, ETHER_HDR_LEN);
437 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
441 return ENOBUFS; /* pkt dropped */
443 return 0; /* consumed e.g. in a pipe */
446 /* packet was ok, restore the ethernet header */
447 ether_restore_header(&m, eh, &save_eh);
456 * Queue message on interface, update output statistics if
457 * successful, and start output if interface not yet active.
459 error = ifq_dispatch(ifp, m, &pktattr);
464 * ipfw processing for ethernet packets (in and out).
465 * The second parameter is NULL from ether_demux(), and ifp from
466 * ether_output_frame().
469 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
470 const struct ether_header *eh)
472 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
473 struct ip_fw_args args;
478 if (*rule != NULL && fw_one_pass)
479 return TRUE; /* dummynet packet, already partially processed */
482 * I need some amount of data to be contiguous.
484 i = min((*m0)->m_pkthdr.len, max_protohdr);
485 if ((*m0)->m_len < i) {
486 *m0 = m_pullup(*m0, i);
494 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
495 m_tag_delete(*m0, mtag);
496 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
497 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
498 KKASSERT(mtag != NULL);
499 m_tag_delete(*m0, mtag);
500 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
503 args.m = *m0; /* the packet we are looking at */
504 args.oif = dst; /* destination, if any */
505 args.rule = *rule; /* matching rule to restart */
506 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
507 i = ip_fw_chk_ptr(&args);
522 * XXX at some point add support for divert/forward actions.
523 * If none of the above matches, we have to drop the pkt.
529 * Pass the pkt to dummynet, which consumes it.
531 m = *m0; /* pass the original to dummynet */
532 *m0 = NULL; /* and nothing back to the caller */
534 ether_restore_header(&m, eh, &save_eh);
538 ip_fw_dn_io_ptr(m, args.cookie,
539 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
544 panic("unknown ipfw return value: %d\n", i);
549 ether_input(struct ifnet *ifp, struct mbuf *m)
551 ether_input_chain(ifp, m, NULL);
555 * Perform common duties while attaching to interface list
558 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
560 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
565 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
566 lwkt_serialize_t serializer)
568 struct sockaddr_dl *sdl;
570 ifp->if_type = IFT_ETHER;
571 ifp->if_addrlen = ETHER_ADDR_LEN;
572 ifp->if_hdrlen = ETHER_HDR_LEN;
573 if_attach(ifp, serializer);
574 ifp->if_mtu = ETHERMTU;
575 if (ifp->if_baudrate == 0)
576 ifp->if_baudrate = 10000000;
577 ifp->if_output = ether_output;
578 ifp->if_input = ether_input;
579 ifp->if_resolvemulti = ether_resolvemulti;
580 ifp->if_broadcastaddr = etherbroadcastaddr;
581 sdl = IF_LLSOCKADDR(ifp);
582 sdl->sdl_type = IFT_ETHER;
583 sdl->sdl_alen = ifp->if_addrlen;
584 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
586 * XXX Keep the current drivers happy.
587 * XXX Remove once all drivers have been cleaned up
589 if (lla != IFP2AC(ifp)->ac_enaddr)
590 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
591 bpfattach(ifp, dlt, hdrlen);
592 if (ng_ether_attach_p != NULL)
593 (*ng_ether_attach_p)(ifp);
595 if_printf(ifp, "MAC address: %6D\n", lla, ":");
599 * Perform common duties while detaching an Ethernet interface
602 ether_ifdetach(struct ifnet *ifp)
606 if (ng_ether_detach_p != NULL)
607 (*ng_ether_detach_p)(ifp);
613 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
615 struct ifaddr *ifa = (struct ifaddr *) data;
616 struct ifreq *ifr = (struct ifreq *) data;
619 #define IF_INIT(ifp) \
621 if (((ifp)->if_flags & IFF_UP) == 0) { \
622 (ifp)->if_flags |= IFF_UP; \
623 (ifp)->if_init((ifp)->if_softc); \
627 ASSERT_SERIALIZED(ifp->if_serializer);
631 switch (ifa->ifa_addr->sa_family) {
634 IF_INIT(ifp); /* before arpwhohas */
635 arp_ifinit(ifp, ifa);
640 * XXX - This code is probably wrong
644 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
645 struct arpcom *ac = IFP2AC(ifp);
647 if (ipx_nullhost(*ina))
648 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
650 bcopy(ina->x_host.c_host, ac->ac_enaddr,
651 sizeof ac->ac_enaddr);
653 IF_INIT(ifp); /* Set new address. */
659 * XXX - This code is probably wrong
663 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
664 struct arpcom *ac = IFP2AC(ifp);
666 if (ns_nullhost(*ina))
667 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
669 bcopy(ina->x_host.c_host, ac->ac_enaddr,
670 sizeof ac->ac_enaddr);
686 bcopy(IFP2AC(ifp)->ac_enaddr,
687 ((struct sockaddr *)ifr->ifr_data)->sa_data,
693 * Set the interface MTU.
695 if (ifr->ifr_mtu > ETHERMTU) {
698 ifp->if_mtu = ifr->ifr_mtu;
713 struct sockaddr **llsa,
716 struct sockaddr_dl *sdl;
717 struct sockaddr_in *sin;
719 struct sockaddr_in6 *sin6;
723 switch(sa->sa_family) {
726 * No mapping needed. Just check that it's a valid MC address.
728 sdl = (struct sockaddr_dl *)sa;
729 e_addr = LLADDR(sdl);
730 if ((e_addr[0] & 1) != 1)
731 return EADDRNOTAVAIL;
737 sin = (struct sockaddr_in *)sa;
738 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
739 return EADDRNOTAVAIL;
740 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
742 sdl->sdl_len = sizeof *sdl;
743 sdl->sdl_family = AF_LINK;
744 sdl->sdl_index = ifp->if_index;
745 sdl->sdl_type = IFT_ETHER;
746 sdl->sdl_alen = ETHER_ADDR_LEN;
747 e_addr = LLADDR(sdl);
748 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
749 *llsa = (struct sockaddr *)sdl;
754 sin6 = (struct sockaddr_in6 *)sa;
755 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
757 * An IP6 address of 0 means listen to all
758 * of the Ethernet multicast address used for IP6.
759 * (This is used for multicast routers.)
761 ifp->if_flags |= IFF_ALLMULTI;
765 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
766 return EADDRNOTAVAIL;
767 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
769 sdl->sdl_len = sizeof *sdl;
770 sdl->sdl_family = AF_LINK;
771 sdl->sdl_index = ifp->if_index;
772 sdl->sdl_type = IFT_ETHER;
773 sdl->sdl_alen = ETHER_ADDR_LEN;
774 e_addr = LLADDR(sdl);
775 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
776 *llsa = (struct sockaddr *)sdl;
782 * Well, the text isn't quite right, but it's the name
791 * This is for reference. We have a table-driven version
792 * of the little-endian crc32 generator, which is faster
793 * than the double-loop.
796 ether_crc32_le(const uint8_t *buf, size_t len)
798 uint32_t c, crc, carry;
801 crc = 0xffffffffU; /* initial value */
803 for (i = 0; i < len; i++) {
805 for (j = 0; j < 8; j++) {
806 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
810 crc = (crc ^ ETHER_CRC_POLY_LE);
818 ether_crc32_le(const uint8_t *buf, size_t len)
820 static const uint32_t crctab[] = {
821 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
822 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
823 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
824 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
829 crc = 0xffffffffU; /* initial value */
831 for (i = 0; i < len; i++) {
833 crc = (crc >> 4) ^ crctab[crc & 0xf];
834 crc = (crc >> 4) ^ crctab[crc & 0xf];
842 ether_crc32_be(const uint8_t *buf, size_t len)
844 uint32_t c, crc, carry;
847 crc = 0xffffffffU; /* initial value */
849 for (i = 0; i < len; i++) {
851 for (j = 0; j < 8; j++) {
852 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
856 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
864 * find the size of ethernet header, and call classifier
867 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
868 struct altq_pktattr *pktattr)
870 struct ether_header *eh;
872 int hlen, af, hdrsize;
875 hlen = sizeof(struct ether_header);
876 eh = mtod(m, struct ether_header *);
878 ether_type = ntohs(eh->ether_type);
879 if (ether_type < ETHERMTU) {
881 struct llc *llc = (struct llc *)(eh + 1);
884 if (m->m_len < hlen ||
885 llc->llc_dsap != LLC_SNAP_LSAP ||
886 llc->llc_ssap != LLC_SNAP_LSAP ||
887 llc->llc_control != LLC_UI)
888 goto bad; /* not snap! */
890 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
893 if (ether_type == ETHERTYPE_IP) {
895 hdrsize = 20; /* sizeof(struct ip) */
897 } else if (ether_type == ETHERTYPE_IPV6) {
899 hdrsize = 40; /* sizeof(struct ip6_hdr) */
904 while (m->m_len <= hlen) {
908 hdr = m->m_data + hlen;
909 if (m->m_len < hlen + hdrsize) {
911 * ip header is not in a single mbuf. this should not
912 * happen in the current code.
913 * (todo: use m_pulldown in the future)
919 ifq_classify(ifq, m, af, pktattr);
926 pktattr->pattr_class = NULL;
927 pktattr->pattr_hdr = NULL;
928 pktattr->pattr_af = AF_UNSPEC;
932 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
933 const struct ether_header *save_eh)
935 struct mbuf *m = *m0;
940 * Prepend the header, optimize for the common case of
941 * eh pointing into the mbuf.
943 if ((const void *)(eh + 1) == (void *)m->m_data) {
944 m->m_data -= ETHER_HDR_LEN;
945 m->m_len += ETHER_HDR_LEN;
946 m->m_pkthdr.len += ETHER_HDR_LEN;
950 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
952 bcopy(save_eh, mtod(m, struct ether_header *),
959 #ifdef ETHER_INPUT_CHAIN
962 ether_input_ipifunc(void *arg)
964 struct mbuf *m, *next;
972 port = m->m_pkthdr.header;
973 m->m_pkthdr.header = NULL;
976 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
983 ether_input_dispatch(struct mbuf_chain *chain)
988 for (i = 0; i < ncpus; ++i) {
989 if (chain[i].mc_head != NULL) {
990 lwkt_send_ipiq(globaldata_find(i),
991 ether_input_ipifunc, chain[i].mc_head);
995 if (chain->mc_head != NULL)
996 ether_input_ipifunc(chain->mc_head);
1001 ether_input_chain_init(struct mbuf_chain *chain)
1006 for (i = 0; i < ncpus; ++i)
1007 chain[i].mc_head = chain[i].mc_tail = NULL;
1009 chain->mc_head = chain->mc_tail = NULL;
1013 #endif /* ETHER_INPUT_CHAIN */
1016 * Upper layer processing for a received Ethernet packet.
1019 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1021 struct ether_header *eh;
1022 int isr, redispatch;
1024 struct ip_fw *rule = NULL;
1030 KASSERT(m->m_len >= ETHER_HDR_LEN,
1031 ("ether header is no contiguous!\n"));
1033 eh = mtod(m, struct ether_header *);
1035 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1038 /* Extract info from dummynet tag */
1039 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1040 KKASSERT(mtag != NULL);
1041 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1042 KKASSERT(rule != NULL);
1044 m_tag_delete(m, mtag);
1045 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1047 /* packet is passing the second time */
1053 * XXX: Okay, we need to call carp_forus() and - if it is for
1054 * us jump over code that does the normal check
1055 * "ac_enaddr == ether_dhost". The check sequence is a bit
1056 * different from OpenBSD, so we jump over as few code as
1057 * possible, to catch _all_ sanity checks. This needs
1058 * evaluation, to see if the carp ether_dhost values break any
1061 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1066 * Discard packet if upper layers shouldn't see it because
1067 * it was unicast to a different Ethernet address. If the
1068 * driver is working properly, then this situation can only
1069 * happen when the interface is in promiscuous mode.
1071 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1072 (eh->ether_dhost[0] & 1) == 0 &&
1073 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1079 if (IPFW_LOADED && ether_ipfw != 0) {
1080 struct ether_header save_eh = *eh;
1082 /* XXX old crufty stuff, needs to be removed */
1083 m_adj(m, sizeof(struct ether_header));
1085 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1090 ether_restore_header(&m, eh, &save_eh);
1093 eh = mtod(m, struct ether_header *);
1096 ether_type = ntohs(eh->ether_type);
1097 KKASSERT(ether_type != ETHERTYPE_VLAN);
1099 if (m->m_flags & M_VLANTAG) {
1100 if (vlan_input_p != NULL) {
1103 m->m_pkthdr.rcvif->if_noproto++;
1109 m_adj(m, sizeof(struct ether_header));
1112 switch (ether_type) {
1115 if (ipflow_fastforward(m))
1121 if (ifp->if_flags & IFF_NOARP) {
1122 /* Discard packet if ARP is disabled on interface */
1131 case ETHERTYPE_IPV6:
1138 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1145 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1153 isr = NETISR_ATALK1;
1155 case ETHERTYPE_AARP:
1161 case ETHERTYPE_MPLS:
1162 case ETHERTYPE_MPLS_MCAST:
1163 /* Should have been set by ether_input_chain(). */
1164 KKASSERT(m->m_flags & M_MPLSLABELED);
1171 * The accurate msgport is not determined before
1172 * we reach here, so redo the dispatching
1176 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1180 checksum = mtod(m, ushort *);
1182 if ((ether_type <= ETHERMTU) &&
1183 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1184 if (*checksum == 0xE0E0) {
1185 m->m_pkthdr.len -= 3;
1194 if (ether_type > ETHERMTU)
1196 l = mtod(m, struct llc *);
1197 if (l->llc_dsap == LLC_SNAP_LSAP &&
1198 l->llc_ssap == LLC_SNAP_LSAP &&
1199 l->llc_control == LLC_UI) {
1200 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1201 sizeof at_org_code) == 0 &&
1202 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1203 m_adj(m, sizeof(struct llc));
1204 isr = NETISR_ATALK2;
1207 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1208 sizeof aarp_org_code) == 0 &&
1209 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1210 m_adj(m, sizeof(struct llc));
1217 if (ng_ether_input_orphan_p != NULL)
1218 ng_ether_input_orphan_p(ifp, m, eh);
1227 netisr_dispatch(isr, m);
1231 * First we perform any link layer operations, then continue to the
1232 * upper layers with ether_demux_oncpu().
1235 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1237 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1239 * Receiving interface's flags are changed, when this
1240 * packet is waiting for processing; discard it.
1247 * Tap the packet off here for a bridge. bridge_input()
1248 * will return NULL if it has consumed the packet, otherwise
1249 * it gets processed as normal. Note that bridge_input()
1250 * will always return the original packet if we need to
1251 * process it locally.
1253 if (ifp->if_bridge) {
1254 KASSERT(bridge_input_p != NULL,
1255 ("%s: if_bridge not loaded!", __func__));
1257 if(m->m_flags & M_PROTO1) {
1258 m->m_flags &= ~M_PROTO1;
1260 /* clear M_PROMISC, in case the packets comes from a vlan */
1261 /* m->m_flags &= ~M_PROMISC; */
1262 m = bridge_input_p(ifp, m);
1266 KASSERT(ifp == m->m_pkthdr.rcvif,
1267 ("bridge_input_p changed rcvif\n"));
1271 /* Handle ng_ether(4) processing, if any */
1272 if (ng_ether_input_p != NULL) {
1273 ng_ether_input_p(ifp, &m);
1278 /* Continue with upper layer processing */
1279 ether_demux_oncpu(ifp, m);
1283 ether_input_handler(struct netmsg *nmsg)
1285 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1291 ifp = m->m_pkthdr.rcvif;
1293 ether_input_oncpu(ifp, m);
1296 static __inline void
1297 ether_init_netpacket(int num, struct mbuf *m)
1299 struct netmsg_packet *pmsg;
1301 pmsg = &m->m_hdr.mh_netmsg;
1302 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1303 ether_input_handler);
1304 pmsg->nm_packet = m;
1305 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1308 static __inline struct lwkt_port *
1309 ether_mport(int num, struct mbuf **m)
1311 if (num == NETISR_MAX) {
1313 * All packets whose target msgports can't be
1314 * determined here are dispatched to netisr0,
1315 * where further dispatching may happen.
1317 return cpu_portfn(0);
1319 return netisr_find_port(num, m);
1323 * Process a received Ethernet packet.
1325 * The ethernet header is assumed to be in the mbuf so the caller
1326 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1327 * bytes in the first mbuf.
1329 * We first try to find the target msgport for this ether frame, if
1330 * there is no target msgport for it, this ether frame is discarded,
1331 * else we do following processing according to whether 'chain' is
1333 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1334 * immediately. This situation happens when ether_input_chain is
1335 * accessed through ifnet.if_input.
1336 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1337 * bucket indexed by the target msgport's cpuid and the target msgport
1338 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1339 * must initialize 'chain' by calling ether_input_chain_init().
1340 * ether_input_dispatch must be called later to send ether frames
1341 * queued on 'chain' to their target msgport.
1344 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1346 struct ether_header *eh, *save_eh, save_eh0;
1347 struct lwkt_port *port;
1348 uint16_t ether_type;
1351 ASSERT_SERIALIZED(ifp->if_serializer);
1354 /* Discard packet if interface is not up */
1355 if (!(ifp->if_flags & IFF_UP)) {
1360 if (m->m_len < sizeof(struct ether_header)) {
1361 /* XXX error in the caller. */
1365 eh = mtod(m, struct ether_header *);
1367 m->m_pkthdr.rcvif = ifp;
1369 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1370 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1371 ifp->if_addrlen) == 0)
1372 m->m_flags |= M_BCAST;
1374 m->m_flags |= M_MCAST;
1378 ETHER_BPF_MTAP(ifp, m);
1380 ifp->if_ibytes += m->m_pkthdr.len;
1382 if (ifp->if_flags & IFF_MONITOR) {
1384 * Interface marked for monitoring; discard packet.
1390 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1391 (m->m_flags & M_VLANTAG) == 0) {
1393 * Extract vlan tag if hardware does not do it for us
1395 vlan_ether_decap(&m);
1398 eh = mtod(m, struct ether_header *);
1400 ether_type = ntohs(eh->ether_type);
1402 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1404 * To prevent possible dangerous recursion,
1405 * we don't do vlan-in-vlan
1411 KKASSERT(ether_type != ETHERTYPE_VLAN);
1414 * Map ether type to netisr id.
1416 switch (ether_type) {
1428 case ETHERTYPE_IPV6:
1440 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1447 isr = NETISR_ATALK1;
1449 case ETHERTYPE_AARP:
1455 case ETHERTYPE_MPLS:
1456 case ETHERTYPE_MPLS_MCAST:
1457 m->m_flags |= M_MPLSLABELED;
1464 * NETISR_MAX is an invalid value; it is chosen to let
1465 * ether_mport() know that we are not able to decide
1466 * this packet's msgport here.
1473 * If the packet is in contiguous memory, following
1474 * m_adj() could ensure that the hidden ether header
1475 * will not be destroyed, else we will have to save
1476 * the ether header for the later restoration.
1478 if (m->m_pkthdr.len != m->m_len) {
1480 save_eh = &save_eh0;
1486 * Temporarily remove ether header; ether_mport()
1487 * expects a packet without ether header.
1489 m_adj(m, sizeof(struct ether_header));
1492 * Find the packet's target msgport.
1494 port = ether_mport(isr, &m);
1496 KKASSERT(m == NULL);
1501 * Restore ether header.
1503 if (save_eh != NULL) {
1504 ether_restore_header(&m, eh, save_eh);
1508 m->m_data -= ETHER_HDR_LEN;
1509 m->m_len += ETHER_HDR_LEN;
1510 m->m_pkthdr.len += ETHER_HDR_LEN;
1514 * Initialize mbuf's netmsg packet _after_ possible
1515 * ether header restoration, else the initialized
1516 * netmsg packet may be lost during ether header
1519 ether_init_netpacket(isr, m);
1521 #ifdef ETHER_INPUT_CHAIN
1522 if (chain != NULL) {
1523 struct mbuf_chain *c;
1526 m->m_pkthdr.header = port; /* XXX */
1527 cpuid = port->mpu_td->td_gd->gd_cpuid;
1530 if (c->mc_head == NULL) {
1531 c->mc_head = c->mc_tail = m;
1533 c->mc_tail->m_nextpkt = m;
1536 m->m_nextpkt = NULL;
1538 #endif /* ETHER_INPUT_CHAIN */
1539 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);