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.90 2008/09/20 10:53:16 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #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");
169 #define ETHER_KTR_STR "ifp=%p"
170 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
172 #define KTR_ETHERNET KTR_ALL
174 KTR_INFO_MASTER(ether);
175 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
176 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
177 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
178 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
179 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
182 * Ethernet output routine.
183 * Encapsulate a packet of type family for the local net.
184 * Use trailer local net encapsulation if enough data in first
185 * packet leaves a multiple of 512 bytes of data in remainder.
186 * Assumes that ifp is actually pointer to arpcom structure.
189 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
192 struct ether_header *eh, *deh;
195 int hlen = ETHER_HDR_LEN; /* link layer header length */
196 struct arpcom *ac = IFP2AC(ifp);
199 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
201 if (ifp->if_flags & IFF_MONITOR)
203 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
206 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
209 eh = mtod(m, struct ether_header *);
210 edst = eh->ether_dhost;
213 * Fill in the destination ethernet address and frame type.
215 switch (dst->sa_family) {
218 if (!arpresolve(ifp, rt, m, dst, edst))
219 return (0); /* if not yet resolved */
221 if (m->m_flags & M_MPLSLABELED)
222 eh->ether_type = htons(ETHERTYPE_MPLS);
225 eh->ether_type = htons(ETHERTYPE_IP);
230 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
231 return (0); /* Something bad happenned. */
232 eh->ether_type = htons(ETHERTYPE_IPV6);
237 if (ef_outputp != NULL) {
238 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
243 eh->ether_type = htons(ETHERTYPE_IPX);
244 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
245 edst, ETHER_ADDR_LEN);
251 struct at_ifaddr *aa;
253 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
258 * In the phase 2 case, need to prepend an mbuf for
259 * the llc header. Since we must preserve the value
260 * of m, which is passed to us by value, we m_copy()
261 * the first mbuf, and use it for our llc header.
263 if (aa->aa_flags & AFA_PHASE2) {
266 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
267 eh = mtod(m, struct ether_header *);
268 edst = eh->ether_dhost;
269 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
270 llc.llc_control = LLC_UI;
271 bcopy(at_org_code, llc.llc_snap_org_code,
273 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
275 mtod(m, caddr_t) + sizeof(struct ether_header),
277 eh->ether_type = htons(m->m_pkthdr.len);
278 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
280 eh->ether_type = htons(ETHERTYPE_AT);
282 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
291 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
292 eh->ether_type = 0x8137;
294 case 0x0: /* Novell 802.3 */
295 eh->ether_type = htons(m->m_pkthdr.len);
297 case 0xe0e0: /* Novell 802.2 and Token-Ring */
298 M_PREPEND(m, 3, MB_DONTWAIT);
299 eh = mtod(m, struct ether_header *);
300 edst = eh->ether_dhost;
301 eh->ether_type = htons(m->m_pkthdr.len);
302 cp = mtod(m, u_char *) + sizeof(struct ether_header);
308 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
311 * XXX if ns_thishost is the same as the node's ethernet
312 * address then just the default code will catch this anyhow.
313 * So I'm not sure if this next clause should be here at all?
316 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
317 m->m_pkthdr.rcvif = ifp;
318 netisr_dispatch(NETISR_NS, m);
321 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
322 m->m_flags |= M_BCAST;
325 case pseudo_AF_HDRCMPLT:
327 loop_copy = -1; /* if this is for us, don't do it */
328 deh = (struct ether_header *)dst->sa_data;
329 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
330 eh->ether_type = deh->ether_type;
334 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
335 gotoerr(EAFNOSUPPORT);
338 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
339 memcpy(eh->ether_shost,
340 ((struct ether_header *)dst->sa_data)->ether_shost,
343 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
346 * Bridges require special output handling.
348 if (ifp->if_bridge) {
349 KASSERT(bridge_output_p != NULL,
350 ("%s: if_bridge not loaded!", __func__));
351 return bridge_output_p(ifp, m);
355 * If a simplex interface, and the packet is being sent to our
356 * Ethernet address or a broadcast address, loopback a copy.
357 * XXX To make a simplex device behave exactly like a duplex
358 * device, we should copy in the case of sending to our own
359 * ethernet address (thus letting the original actually appear
360 * on the wire). However, we don't do that here for security
361 * reasons and compatibility with the original behavior.
363 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
366 if (m->m_pkthdr.csum_flags & CSUM_IP)
367 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
368 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
369 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
370 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
373 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
374 n->m_pkthdr.csum_flags |= csum_flags;
375 if (csum_flags & CSUM_DATA_VALID)
376 n->m_pkthdr.csum_data = 0xffff;
377 if_simloop(ifp, n, dst->sa_family, hlen);
380 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
381 ETHER_ADDR_LEN) == 0) {
382 m->m_pkthdr.csum_flags |= csum_flags;
383 if (csum_flags & CSUM_DATA_VALID)
384 m->m_pkthdr.csum_data = 0xffff;
385 if_simloop(ifp, m, dst->sa_family, hlen);
386 return (0); /* XXX */
391 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
396 /* Handle ng_ether(4) processing, if any */
397 if (ng_ether_output_p != NULL) {
398 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
404 /* Continue with link-layer output */
405 return ether_output_frame(ifp, m);
413 * Ethernet link layer output routine to send a raw frame to the device.
415 * This assumes that the 14 byte Ethernet header is present and contiguous
419 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
421 struct ip_fw *rule = NULL;
423 struct altq_pktattr pktattr;
425 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
427 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
430 /* Extract info from dummynet tag */
431 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
432 KKASSERT(mtag != NULL);
433 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
434 KKASSERT(rule != NULL);
436 m_tag_delete(m, mtag);
437 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
440 if (ifq_is_enabled(&ifp->if_snd))
441 altq_etherclassify(&ifp->if_snd, m, &pktattr);
443 if (IPFW_LOADED && ether_ipfw != 0) {
444 struct ether_header save_eh, *eh;
446 eh = mtod(m, struct ether_header *);
448 m_adj(m, ETHER_HDR_LEN);
449 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
453 return ENOBUFS; /* pkt dropped */
455 return 0; /* consumed e.g. in a pipe */
458 /* packet was ok, restore the ethernet header */
459 ether_restore_header(&m, eh, &save_eh);
468 * Queue message on interface, update output statistics if
469 * successful, and start output if interface not yet active.
471 error = ifq_dispatch(ifp, m, &pktattr);
476 * ipfw processing for ethernet packets (in and out).
477 * The second parameter is NULL from ether_demux(), and ifp from
478 * ether_output_frame().
481 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
482 const struct ether_header *eh)
484 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
485 struct ip_fw_args args;
490 if (*rule != NULL && fw_one_pass)
491 return TRUE; /* dummynet packet, already partially processed */
494 * I need some amount of data to be contiguous.
496 i = min((*m0)->m_pkthdr.len, max_protohdr);
497 if ((*m0)->m_len < i) {
498 *m0 = m_pullup(*m0, i);
506 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
507 m_tag_delete(*m0, mtag);
508 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
509 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
510 KKASSERT(mtag != NULL);
511 m_tag_delete(*m0, mtag);
512 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
515 args.m = *m0; /* the packet we are looking at */
516 args.oif = dst; /* destination, if any */
517 args.rule = *rule; /* matching rule to restart */
518 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
519 i = ip_fw_chk_ptr(&args);
534 * XXX at some point add support for divert/forward actions.
535 * If none of the above matches, we have to drop the pkt.
541 * Pass the pkt to dummynet, which consumes it.
543 m = *m0; /* pass the original to dummynet */
544 *m0 = NULL; /* and nothing back to the caller */
546 ether_restore_header(&m, eh, &save_eh);
550 ip_fw_dn_io_ptr(m, args.cookie,
551 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
556 panic("unknown ipfw return value: %d\n", i);
561 ether_input(struct ifnet *ifp, struct mbuf *m)
563 ether_input_chain(ifp, m, NULL);
567 * Perform common duties while attaching to interface list
570 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
572 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
577 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
578 lwkt_serialize_t serializer)
580 struct sockaddr_dl *sdl;
582 ifp->if_type = IFT_ETHER;
583 ifp->if_addrlen = ETHER_ADDR_LEN;
584 ifp->if_hdrlen = ETHER_HDR_LEN;
585 if_attach(ifp, serializer);
586 ifp->if_mtu = ETHERMTU;
587 if (ifp->if_baudrate == 0)
588 ifp->if_baudrate = 10000000;
589 ifp->if_output = ether_output;
590 ifp->if_input = ether_input;
591 ifp->if_resolvemulti = ether_resolvemulti;
592 ifp->if_broadcastaddr = etherbroadcastaddr;
593 sdl = IF_LLSOCKADDR(ifp);
594 sdl->sdl_type = IFT_ETHER;
595 sdl->sdl_alen = ifp->if_addrlen;
596 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
598 * XXX Keep the current drivers happy.
599 * XXX Remove once all drivers have been cleaned up
601 if (lla != IFP2AC(ifp)->ac_enaddr)
602 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
603 bpfattach(ifp, dlt, hdrlen);
604 if (ng_ether_attach_p != NULL)
605 (*ng_ether_attach_p)(ifp);
607 if_printf(ifp, "MAC address: %6D\n", lla, ":");
611 * Perform common duties while detaching an Ethernet interface
614 ether_ifdetach(struct ifnet *ifp)
618 if (ng_ether_detach_p != NULL)
619 (*ng_ether_detach_p)(ifp);
625 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
627 struct ifaddr *ifa = (struct ifaddr *) data;
628 struct ifreq *ifr = (struct ifreq *) data;
631 #define IF_INIT(ifp) \
633 if (((ifp)->if_flags & IFF_UP) == 0) { \
634 (ifp)->if_flags |= IFF_UP; \
635 (ifp)->if_init((ifp)->if_softc); \
639 ASSERT_SERIALIZED(ifp->if_serializer);
643 switch (ifa->ifa_addr->sa_family) {
646 IF_INIT(ifp); /* before arpwhohas */
647 arp_ifinit(ifp, ifa);
652 * XXX - This code is probably wrong
656 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
657 struct arpcom *ac = IFP2AC(ifp);
659 if (ipx_nullhost(*ina))
660 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
662 bcopy(ina->x_host.c_host, ac->ac_enaddr,
663 sizeof ac->ac_enaddr);
665 IF_INIT(ifp); /* Set new address. */
671 * XXX - This code is probably wrong
675 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
676 struct arpcom *ac = IFP2AC(ifp);
678 if (ns_nullhost(*ina))
679 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
681 bcopy(ina->x_host.c_host, ac->ac_enaddr,
682 sizeof ac->ac_enaddr);
698 bcopy(IFP2AC(ifp)->ac_enaddr,
699 ((struct sockaddr *)ifr->ifr_data)->sa_data,
705 * Set the interface MTU.
707 if (ifr->ifr_mtu > ETHERMTU) {
710 ifp->if_mtu = ifr->ifr_mtu;
725 struct sockaddr **llsa,
728 struct sockaddr_dl *sdl;
729 struct sockaddr_in *sin;
731 struct sockaddr_in6 *sin6;
735 switch(sa->sa_family) {
738 * No mapping needed. Just check that it's a valid MC address.
740 sdl = (struct sockaddr_dl *)sa;
741 e_addr = LLADDR(sdl);
742 if ((e_addr[0] & 1) != 1)
743 return EADDRNOTAVAIL;
749 sin = (struct sockaddr_in *)sa;
750 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
751 return EADDRNOTAVAIL;
752 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
754 sdl->sdl_len = sizeof *sdl;
755 sdl->sdl_family = AF_LINK;
756 sdl->sdl_index = ifp->if_index;
757 sdl->sdl_type = IFT_ETHER;
758 sdl->sdl_alen = ETHER_ADDR_LEN;
759 e_addr = LLADDR(sdl);
760 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
761 *llsa = (struct sockaddr *)sdl;
766 sin6 = (struct sockaddr_in6 *)sa;
767 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
769 * An IP6 address of 0 means listen to all
770 * of the Ethernet multicast address used for IP6.
771 * (This is used for multicast routers.)
773 ifp->if_flags |= IFF_ALLMULTI;
777 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
778 return EADDRNOTAVAIL;
779 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
781 sdl->sdl_len = sizeof *sdl;
782 sdl->sdl_family = AF_LINK;
783 sdl->sdl_index = ifp->if_index;
784 sdl->sdl_type = IFT_ETHER;
785 sdl->sdl_alen = ETHER_ADDR_LEN;
786 e_addr = LLADDR(sdl);
787 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
788 *llsa = (struct sockaddr *)sdl;
794 * Well, the text isn't quite right, but it's the name
803 * This is for reference. We have a table-driven version
804 * of the little-endian crc32 generator, which is faster
805 * than the double-loop.
808 ether_crc32_le(const uint8_t *buf, size_t len)
810 uint32_t c, crc, carry;
813 crc = 0xffffffffU; /* initial value */
815 for (i = 0; i < len; i++) {
817 for (j = 0; j < 8; j++) {
818 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
822 crc = (crc ^ ETHER_CRC_POLY_LE);
830 ether_crc32_le(const uint8_t *buf, size_t len)
832 static const uint32_t crctab[] = {
833 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
834 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
835 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
836 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
841 crc = 0xffffffffU; /* initial value */
843 for (i = 0; i < len; i++) {
845 crc = (crc >> 4) ^ crctab[crc & 0xf];
846 crc = (crc >> 4) ^ crctab[crc & 0xf];
854 ether_crc32_be(const uint8_t *buf, size_t len)
856 uint32_t c, crc, carry;
859 crc = 0xffffffffU; /* initial value */
861 for (i = 0; i < len; i++) {
863 for (j = 0; j < 8; j++) {
864 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
868 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
876 * find the size of ethernet header, and call classifier
879 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
880 struct altq_pktattr *pktattr)
882 struct ether_header *eh;
884 int hlen, af, hdrsize;
887 hlen = sizeof(struct ether_header);
888 eh = mtod(m, struct ether_header *);
890 ether_type = ntohs(eh->ether_type);
891 if (ether_type < ETHERMTU) {
893 struct llc *llc = (struct llc *)(eh + 1);
896 if (m->m_len < hlen ||
897 llc->llc_dsap != LLC_SNAP_LSAP ||
898 llc->llc_ssap != LLC_SNAP_LSAP ||
899 llc->llc_control != LLC_UI)
900 goto bad; /* not snap! */
902 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
905 if (ether_type == ETHERTYPE_IP) {
907 hdrsize = 20; /* sizeof(struct ip) */
909 } else if (ether_type == ETHERTYPE_IPV6) {
911 hdrsize = 40; /* sizeof(struct ip6_hdr) */
916 while (m->m_len <= hlen) {
920 hdr = m->m_data + hlen;
921 if (m->m_len < hlen + hdrsize) {
923 * ip header is not in a single mbuf. this should not
924 * happen in the current code.
925 * (todo: use m_pulldown in the future)
931 ifq_classify(ifq, m, af, pktattr);
938 pktattr->pattr_class = NULL;
939 pktattr->pattr_hdr = NULL;
940 pktattr->pattr_af = AF_UNSPEC;
944 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
945 const struct ether_header *save_eh)
947 struct mbuf *m = *m0;
952 * Prepend the header, optimize for the common case of
953 * eh pointing into the mbuf.
955 if ((const void *)(eh + 1) == (void *)m->m_data) {
956 m->m_data -= ETHER_HDR_LEN;
957 m->m_len += ETHER_HDR_LEN;
958 m->m_pkthdr.len += ETHER_HDR_LEN;
962 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
964 bcopy(save_eh, mtod(m, struct ether_header *),
972 ether_input_ipifunc(void *arg)
974 struct mbuf *m, *next;
982 port = m->m_pkthdr.header;
983 m->m_pkthdr.header = NULL;
986 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
993 ether_input_dispatch(struct mbuf_chain *chain)
998 logether(disp_beg, NULL);
999 for (i = 0; i < ncpus; ++i) {
1000 if (chain[i].mc_head != NULL) {
1001 lwkt_send_ipiq(globaldata_find(i),
1002 ether_input_ipifunc, chain[i].mc_head);
1006 logether(disp_beg, NULL);
1007 if (chain->mc_head != NULL)
1008 ether_input_ipifunc(chain->mc_head);
1010 logether(disp_end, NULL);
1014 ether_input_chain_init(struct mbuf_chain *chain)
1019 for (i = 0; i < ncpus; ++i)
1020 chain[i].mc_head = chain[i].mc_tail = NULL;
1022 chain->mc_head = chain->mc_tail = NULL;
1027 * Upper layer processing for a received Ethernet packet.
1030 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1032 struct ether_header *eh;
1033 int isr, redispatch;
1035 struct ip_fw *rule = NULL;
1041 KASSERT(m->m_len >= ETHER_HDR_LEN,
1042 ("ether header is no contiguous!\n"));
1044 eh = mtod(m, struct ether_header *);
1046 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1049 /* Extract info from dummynet tag */
1050 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1051 KKASSERT(mtag != NULL);
1052 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1053 KKASSERT(rule != NULL);
1055 m_tag_delete(m, mtag);
1056 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1058 /* packet is passing the second time */
1064 * XXX: Okay, we need to call carp_forus() and - if it is for
1065 * us jump over code that does the normal check
1066 * "ac_enaddr == ether_dhost". The check sequence is a bit
1067 * different from OpenBSD, so we jump over as few code as
1068 * possible, to catch _all_ sanity checks. This needs
1069 * evaluation, to see if the carp ether_dhost values break any
1072 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1077 * Discard packet if upper layers shouldn't see it because
1078 * it was unicast to a different Ethernet address. If the
1079 * driver is working properly, then this situation can only
1080 * happen when the interface is in promiscuous mode.
1082 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1083 (eh->ether_dhost[0] & 1) == 0 &&
1084 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1090 if (IPFW_LOADED && ether_ipfw != 0) {
1091 struct ether_header save_eh = *eh;
1093 /* XXX old crufty stuff, needs to be removed */
1094 m_adj(m, sizeof(struct ether_header));
1096 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1101 ether_restore_header(&m, eh, &save_eh);
1104 eh = mtod(m, struct ether_header *);
1107 ether_type = ntohs(eh->ether_type);
1108 KKASSERT(ether_type != ETHERTYPE_VLAN);
1110 if (m->m_flags & M_VLANTAG) {
1111 if (vlan_input_p != NULL) {
1114 m->m_pkthdr.rcvif->if_noproto++;
1120 m_adj(m, sizeof(struct ether_header));
1123 switch (ether_type) {
1126 if (ipflow_fastforward(m))
1132 if (ifp->if_flags & IFF_NOARP) {
1133 /* Discard packet if ARP is disabled on interface */
1142 case ETHERTYPE_IPV6:
1149 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1156 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1164 isr = NETISR_ATALK1;
1166 case ETHERTYPE_AARP:
1172 case ETHERTYPE_MPLS:
1173 case ETHERTYPE_MPLS_MCAST:
1174 /* Should have been set by ether_input_chain(). */
1175 KKASSERT(m->m_flags & M_MPLSLABELED);
1182 * The accurate msgport is not determined before
1183 * we reach here, so redo the dispatching
1187 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1191 checksum = mtod(m, ushort *);
1193 if ((ether_type <= ETHERMTU) &&
1194 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1195 if (*checksum == 0xE0E0) {
1196 m->m_pkthdr.len -= 3;
1205 if (ether_type > ETHERMTU)
1207 l = mtod(m, struct llc *);
1208 if (l->llc_dsap == LLC_SNAP_LSAP &&
1209 l->llc_ssap == LLC_SNAP_LSAP &&
1210 l->llc_control == LLC_UI) {
1211 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1212 sizeof at_org_code) == 0 &&
1213 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1214 m_adj(m, sizeof(struct llc));
1215 isr = NETISR_ATALK2;
1218 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1219 sizeof aarp_org_code) == 0 &&
1220 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1221 m_adj(m, sizeof(struct llc));
1228 if (ng_ether_input_orphan_p != NULL)
1229 ng_ether_input_orphan_p(ifp, m, eh);
1238 netisr_dispatch(isr, m);
1242 * First we perform any link layer operations, then continue to the
1243 * upper layers with ether_demux_oncpu().
1246 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1248 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1250 * Receiving interface's flags are changed, when this
1251 * packet is waiting for processing; discard it.
1258 * Tap the packet off here for a bridge. bridge_input()
1259 * will return NULL if it has consumed the packet, otherwise
1260 * it gets processed as normal. Note that bridge_input()
1261 * will always return the original packet if we need to
1262 * process it locally.
1264 if (ifp->if_bridge) {
1265 KASSERT(bridge_input_p != NULL,
1266 ("%s: if_bridge not loaded!", __func__));
1268 if(m->m_flags & M_PROTO1) {
1269 m->m_flags &= ~M_PROTO1;
1271 /* clear M_PROMISC, in case the packets comes from a vlan */
1272 /* m->m_flags &= ~M_PROMISC; */
1273 m = bridge_input_p(ifp, m);
1277 KASSERT(ifp == m->m_pkthdr.rcvif,
1278 ("bridge_input_p changed rcvif\n"));
1282 /* Handle ng_ether(4) processing, if any */
1283 if (ng_ether_input_p != NULL) {
1284 ng_ether_input_p(ifp, &m);
1289 /* Continue with upper layer processing */
1290 ether_demux_oncpu(ifp, m);
1294 ether_input_handler(struct netmsg *nmsg)
1296 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1302 ifp = m->m_pkthdr.rcvif;
1304 ether_input_oncpu(ifp, m);
1307 static __inline void
1308 ether_init_netpacket(int num, struct mbuf *m)
1310 struct netmsg_packet *pmsg;
1312 pmsg = &m->m_hdr.mh_netmsg;
1313 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1314 ether_input_handler);
1315 pmsg->nm_packet = m;
1316 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1319 static __inline struct lwkt_port *
1320 ether_mport(int num, struct mbuf **m)
1322 if (num == NETISR_MAX) {
1324 * All packets whose target msgports can't be
1325 * determined here are dispatched to netisr0,
1326 * where further dispatching may happen.
1328 return cpu_portfn(0);
1330 return netisr_find_port(num, m);
1334 * Process a received Ethernet packet.
1336 * The ethernet header is assumed to be in the mbuf so the caller
1337 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1338 * bytes in the first mbuf.
1340 * We first try to find the target msgport for this ether frame, if
1341 * there is no target msgport for it, this ether frame is discarded,
1342 * else we do following processing according to whether 'chain' is
1344 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1345 * immediately. This situation happens when ether_input_chain is
1346 * accessed through ifnet.if_input.
1347 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1348 * bucket indexed by the target msgport's cpuid and the target msgport
1349 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1350 * must initialize 'chain' by calling ether_input_chain_init().
1351 * ether_input_dispatch must be called later to send ether frames
1352 * queued on 'chain' to their target msgport.
1355 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1357 struct ether_header *eh, *save_eh, save_eh0;
1358 struct lwkt_port *port;
1359 uint16_t ether_type;
1362 ASSERT_SERIALIZED(ifp->if_serializer);
1365 /* Discard packet if interface is not up */
1366 if (!(ifp->if_flags & IFF_UP)) {
1371 if (m->m_len < sizeof(struct ether_header)) {
1372 /* XXX error in the caller. */
1376 eh = mtod(m, struct ether_header *);
1378 m->m_pkthdr.rcvif = ifp;
1380 logether(chain_beg, ifp);
1382 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1383 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1384 ifp->if_addrlen) == 0)
1385 m->m_flags |= M_BCAST;
1387 m->m_flags |= M_MCAST;
1391 ETHER_BPF_MTAP(ifp, m);
1393 ifp->if_ibytes += m->m_pkthdr.len;
1395 if (ifp->if_flags & IFF_MONITOR) {
1397 * Interface marked for monitoring; discard packet.
1401 logether(chain_end, ifp);
1405 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1406 (m->m_flags & M_VLANTAG) == 0) {
1408 * Extract vlan tag if hardware does not do it for us
1410 vlan_ether_decap(&m);
1413 eh = mtod(m, struct ether_header *);
1415 ether_type = ntohs(eh->ether_type);
1417 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1419 * To prevent possible dangerous recursion,
1420 * we don't do vlan-in-vlan
1426 KKASSERT(ether_type != ETHERTYPE_VLAN);
1429 * Map ether type to netisr id.
1431 switch (ether_type) {
1443 case ETHERTYPE_IPV6:
1455 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1462 isr = NETISR_ATALK1;
1464 case ETHERTYPE_AARP:
1470 case ETHERTYPE_MPLS:
1471 case ETHERTYPE_MPLS_MCAST:
1472 m->m_flags |= M_MPLSLABELED;
1479 * NETISR_MAX is an invalid value; it is chosen to let
1480 * ether_mport() know that we are not able to decide
1481 * this packet's msgport here.
1488 * If the packet is in contiguous memory, following
1489 * m_adj() could ensure that the hidden ether header
1490 * will not be destroyed, else we will have to save
1491 * the ether header for the later restoration.
1493 if (m->m_pkthdr.len != m->m_len) {
1495 save_eh = &save_eh0;
1501 * Temporarily remove ether header; ether_mport()
1502 * expects a packet without ether header.
1504 m_adj(m, sizeof(struct ether_header));
1507 * Find the packet's target msgport.
1509 port = ether_mport(isr, &m);
1511 KKASSERT(m == NULL);
1516 * Restore ether header.
1518 if (save_eh != NULL) {
1519 ether_restore_header(&m, eh, save_eh);
1523 m->m_data -= ETHER_HDR_LEN;
1524 m->m_len += ETHER_HDR_LEN;
1525 m->m_pkthdr.len += ETHER_HDR_LEN;
1529 * Initialize mbuf's netmsg packet _after_ possible
1530 * ether header restoration, else the initialized
1531 * netmsg packet may be lost during ether header
1534 ether_init_netpacket(isr, m);
1536 if (chain != NULL) {
1537 struct mbuf_chain *c;
1540 m->m_pkthdr.header = port; /* XXX */
1541 cpuid = port->mpu_td->td_gd->gd_cpuid;
1544 if (c->mc_head == NULL) {
1545 c->mc_head = c->mc_tail = m;
1547 c->mc_tail->m_nextpkt = m;
1550 m->m_nextpkt = NULL;
1552 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1554 logether(chain_end, ifp);