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.94 2008/10/04 11:24:37 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>
52 #include <sys/malloc.h>
54 #include <sys/msgport.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/thread.h>
59 #include <sys/thread2.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/if_llc.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/ifq_var.h>
69 #include <net/ethernet.h>
70 #include <net/vlan/if_vlan_ether.h>
71 #include <net/netmsg2.h>
73 #if defined(INET) || defined(INET6)
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/if_ether.h>
77 #include <net/ipfw/ip_fw.h>
78 #include <net/dummynet/ip_dummynet.h>
81 #include <netinet6/nd6.h>
85 #include <netinet/ip_carp.h>
89 #include <netproto/ipx/ipx.h>
90 #include <netproto/ipx/ipx_if.h>
91 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
92 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
93 short *tp, int *hlen);
98 #include <netns/ns_if.h>
100 int ether_outputdebug = 0;
101 int ether_inputdebug = 0;
105 #include <netproto/atalk/at.h>
106 #include <netproto/atalk/at_var.h>
107 #include <netproto/atalk/at_extern.h>
109 #define llc_snap_org_code llc_un.type_snap.org_code
110 #define llc_snap_ether_type llc_un.type_snap.ether_type
112 extern u_char at_org_code[3];
113 extern u_char aarp_org_code[3];
114 #endif /* NETATALK */
117 #include <netproto/mpls/mpls.h>
120 /* netgraph node hooks for ng_ether(4) */
121 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
122 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
123 struct mbuf *m, const struct ether_header *eh);
124 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
125 void (*ng_ether_attach_p)(struct ifnet *ifp);
126 void (*ng_ether_detach_p)(struct ifnet *ifp);
128 void (*vlan_input_p)(struct mbuf *);
130 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
132 static void ether_restore_header(struct mbuf **, const struct ether_header *,
133 const struct ether_header *);
138 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
139 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
140 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
142 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
145 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
149 #define gotoerr(e) do { error = (e); goto bad; } while (0)
150 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
152 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
154 const struct ether_header *eh);
156 static int ether_ipfw;
157 static u_int ether_restore_hdr;
158 static u_int ether_prepend_hdr;
160 SYSCTL_DECL(_net_link);
161 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
162 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
163 ðer_ipfw, 0, "Pass ether pkts through firewall");
164 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
165 ðer_restore_hdr, 0, "# of ether header restoration");
166 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
167 ðer_prepend_hdr, 0,
168 "# of ether header restoration which prepends mbuf");
170 #define ETHER_KTR_STR "ifp=%p"
171 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
173 #define KTR_ETHERNET KTR_ALL
175 KTR_INFO_MASTER(ether);
176 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
177 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
178 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
179 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
180 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
183 * Ethernet output routine.
184 * Encapsulate a packet of type family for the local net.
185 * Use trailer local net encapsulation if enough data in first
186 * packet leaves a multiple of 512 bytes of data in remainder.
187 * Assumes that ifp is actually pointer to arpcom structure.
190 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
193 struct ether_header *eh, *deh;
196 int hlen = ETHER_HDR_LEN; /* link layer header length */
197 struct arpcom *ac = IFP2AC(ifp);
200 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
202 if (ifp->if_flags & IFF_MONITOR)
204 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
207 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
210 eh = mtod(m, struct ether_header *);
211 edst = eh->ether_dhost;
214 * Fill in the destination ethernet address and frame type.
216 switch (dst->sa_family) {
219 if (!arpresolve(ifp, rt, m, dst, edst))
220 return (0); /* if not yet resolved */
222 if (m->m_flags & M_MPLSLABELED)
223 eh->ether_type = htons(ETHERTYPE_MPLS);
226 eh->ether_type = htons(ETHERTYPE_IP);
231 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
232 return (0); /* Something bad happenned. */
233 eh->ether_type = htons(ETHERTYPE_IPV6);
238 if (ef_outputp != NULL) {
240 * Hold BGL and recheck ef_outputp
243 if (ef_outputp != NULL) {
244 error = ef_outputp(ifp, &m, dst,
245 &eh->ether_type, &hlen);
254 eh->ether_type = htons(ETHERTYPE_IPX);
255 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
256 edst, ETHER_ADDR_LEN);
261 struct at_ifaddr *aa;
268 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
274 * In the phase 2 case, need to prepend an mbuf for
275 * the llc header. Since we must preserve the value
276 * of m, which is passed to us by value, we m_copy()
277 * the first mbuf, and use it for our llc header.
279 if (aa->aa_flags & AFA_PHASE2) {
282 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
283 eh = mtod(m, struct ether_header *);
284 edst = eh->ether_dhost;
285 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
286 llc.llc_control = LLC_UI;
287 bcopy(at_org_code, llc.llc_snap_org_code,
289 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
291 mtod(m, caddr_t) + sizeof(struct ether_header),
293 eh->ether_type = htons(m->m_pkthdr.len);
294 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
296 eh->ether_type = htons(ETHERTYPE_AT);
298 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
311 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
312 eh->ether_type = 0x8137;
314 case 0x0: /* Novell 802.3 */
315 eh->ether_type = htons(m->m_pkthdr.len);
317 case 0xe0e0: /* Novell 802.2 and Token-Ring */
318 M_PREPEND(m, 3, MB_DONTWAIT);
319 eh = mtod(m, struct ether_header *);
320 edst = eh->ether_dhost;
321 eh->ether_type = htons(m->m_pkthdr.len);
322 cp = mtod(m, u_char *) + sizeof(struct ether_header);
328 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
331 * XXX if ns_thishost is the same as the node's ethernet
332 * address then just the default code will catch this anyhow.
333 * So I'm not sure if this next clause should be here at all?
336 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
337 m->m_pkthdr.rcvif = ifp;
338 netisr_dispatch(NETISR_NS, m);
341 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
342 m->m_flags |= M_BCAST;
345 case pseudo_AF_HDRCMPLT:
347 loop_copy = -1; /* if this is for us, don't do it */
348 deh = (struct ether_header *)dst->sa_data;
349 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
350 eh->ether_type = deh->ether_type;
354 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
355 gotoerr(EAFNOSUPPORT);
358 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
359 memcpy(eh->ether_shost,
360 ((struct ether_header *)dst->sa_data)->ether_shost,
363 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
366 * Bridges require special output handling.
368 if (ifp->if_bridge) {
369 KASSERT(bridge_output_p != NULL,
370 ("%s: if_bridge not loaded!", __func__));
371 return bridge_output_p(ifp, m);
375 * If a simplex interface, and the packet is being sent to our
376 * Ethernet address or a broadcast address, loopback a copy.
377 * XXX To make a simplex device behave exactly like a duplex
378 * device, we should copy in the case of sending to our own
379 * ethernet address (thus letting the original actually appear
380 * on the wire). However, we don't do that here for security
381 * reasons and compatibility with the original behavior.
383 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
386 if (m->m_pkthdr.csum_flags & CSUM_IP)
387 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
388 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
389 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
390 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
393 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
394 n->m_pkthdr.csum_flags |= csum_flags;
395 if (csum_flags & CSUM_DATA_VALID)
396 n->m_pkthdr.csum_data = 0xffff;
397 if_simloop(ifp, n, dst->sa_family, hlen);
400 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
401 ETHER_ADDR_LEN) == 0) {
402 m->m_pkthdr.csum_flags |= csum_flags;
403 if (csum_flags & CSUM_DATA_VALID)
404 m->m_pkthdr.csum_data = 0xffff;
405 if_simloop(ifp, m, dst->sa_family, hlen);
406 return (0); /* XXX */
413 * Hold BGL and recheck ifp->if_carp
416 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
425 /* Handle ng_ether(4) processing, if any */
426 if (ng_ether_output_p != NULL) {
428 * Hold BGL and recheck ng_ether_output_p
431 if (ng_ether_output_p != NULL) {
432 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
444 /* Continue with link-layer output */
445 return ether_output_frame(ifp, m);
453 * Ethernet link layer output routine to send a raw frame to the device.
455 * This assumes that the 14 byte Ethernet header is present and contiguous
459 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
461 struct ip_fw *rule = NULL;
463 struct altq_pktattr pktattr;
465 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
467 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
470 /* Extract info from dummynet tag */
471 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
472 KKASSERT(mtag != NULL);
473 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
474 KKASSERT(rule != NULL);
476 m_tag_delete(m, mtag);
477 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
480 if (ifq_is_enabled(&ifp->if_snd))
481 altq_etherclassify(&ifp->if_snd, m, &pktattr);
483 if (IPFW_LOADED && ether_ipfw != 0) {
484 struct ether_header save_eh, *eh;
486 eh = mtod(m, struct ether_header *);
488 m_adj(m, ETHER_HDR_LEN);
489 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
493 return ENOBUFS; /* pkt dropped */
495 return 0; /* consumed e.g. in a pipe */
498 /* packet was ok, restore the ethernet header */
499 ether_restore_header(&m, eh, &save_eh);
508 * Queue message on interface, update output statistics if
509 * successful, and start output if interface not yet active.
511 error = ifq_dispatch(ifp, m, &pktattr);
516 * ipfw processing for ethernet packets (in and out).
517 * The second parameter is NULL from ether_demux(), and ifp from
518 * ether_output_frame().
521 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
522 const struct ether_header *eh)
524 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
525 struct ip_fw_args args;
530 if (*rule != NULL && fw_one_pass)
531 return TRUE; /* dummynet packet, already partially processed */
534 * I need some amount of data to be contiguous.
536 i = min((*m0)->m_pkthdr.len, max_protohdr);
537 if ((*m0)->m_len < i) {
538 *m0 = m_pullup(*m0, i);
546 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
547 m_tag_delete(*m0, mtag);
548 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
549 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
550 KKASSERT(mtag != NULL);
551 m_tag_delete(*m0, mtag);
552 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
555 args.m = *m0; /* the packet we are looking at */
556 args.oif = dst; /* destination, if any */
557 args.rule = *rule; /* matching rule to restart */
558 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
559 i = ip_fw_chk_ptr(&args);
574 * XXX at some point add support for divert/forward actions.
575 * If none of the above matches, we have to drop the pkt.
581 * Pass the pkt to dummynet, which consumes it.
583 m = *m0; /* pass the original to dummynet */
584 *m0 = NULL; /* and nothing back to the caller */
586 ether_restore_header(&m, eh, &save_eh);
590 ip_fw_dn_io_ptr(m, args.cookie,
591 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
596 panic("unknown ipfw return value: %d\n", i);
601 ether_input(struct ifnet *ifp, struct mbuf *m)
603 ether_input_chain(ifp, m, NULL);
607 * Perform common duties while attaching to interface list
610 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
612 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
617 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
618 lwkt_serialize_t serializer)
620 struct sockaddr_dl *sdl;
622 ifp->if_type = IFT_ETHER;
623 ifp->if_addrlen = ETHER_ADDR_LEN;
624 ifp->if_hdrlen = ETHER_HDR_LEN;
625 if_attach(ifp, serializer);
626 ifp->if_mtu = ETHERMTU;
627 if (ifp->if_baudrate == 0)
628 ifp->if_baudrate = 10000000;
629 ifp->if_output = ether_output;
630 ifp->if_input = ether_input;
631 ifp->if_resolvemulti = ether_resolvemulti;
632 ifp->if_broadcastaddr = etherbroadcastaddr;
633 sdl = IF_LLSOCKADDR(ifp);
634 sdl->sdl_type = IFT_ETHER;
635 sdl->sdl_alen = ifp->if_addrlen;
636 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
638 * XXX Keep the current drivers happy.
639 * XXX Remove once all drivers have been cleaned up
641 if (lla != IFP2AC(ifp)->ac_enaddr)
642 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
643 bpfattach(ifp, dlt, hdrlen);
644 if (ng_ether_attach_p != NULL)
645 (*ng_ether_attach_p)(ifp);
647 if_printf(ifp, "MAC address: %6D\n", lla, ":");
651 * Perform common duties while detaching an Ethernet interface
654 ether_ifdetach(struct ifnet *ifp)
658 if (ng_ether_detach_p != NULL)
659 (*ng_ether_detach_p)(ifp);
665 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
667 struct ifaddr *ifa = (struct ifaddr *) data;
668 struct ifreq *ifr = (struct ifreq *) data;
671 #define IF_INIT(ifp) \
673 if (((ifp)->if_flags & IFF_UP) == 0) { \
674 (ifp)->if_flags |= IFF_UP; \
675 (ifp)->if_init((ifp)->if_softc); \
679 ASSERT_SERIALIZED(ifp->if_serializer);
683 switch (ifa->ifa_addr->sa_family) {
686 IF_INIT(ifp); /* before arpwhohas */
687 arp_ifinit(ifp, ifa);
692 * XXX - This code is probably wrong
696 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
697 struct arpcom *ac = IFP2AC(ifp);
699 if (ipx_nullhost(*ina))
700 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
702 bcopy(ina->x_host.c_host, ac->ac_enaddr,
703 sizeof ac->ac_enaddr);
705 IF_INIT(ifp); /* Set new address. */
711 * XXX - This code is probably wrong
715 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
716 struct arpcom *ac = IFP2AC(ifp);
718 if (ns_nullhost(*ina))
719 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
721 bcopy(ina->x_host.c_host, ac->ac_enaddr,
722 sizeof ac->ac_enaddr);
738 bcopy(IFP2AC(ifp)->ac_enaddr,
739 ((struct sockaddr *)ifr->ifr_data)->sa_data,
745 * Set the interface MTU.
747 if (ifr->ifr_mtu > ETHERMTU) {
750 ifp->if_mtu = ifr->ifr_mtu;
765 struct sockaddr **llsa,
768 struct sockaddr_dl *sdl;
769 struct sockaddr_in *sin;
771 struct sockaddr_in6 *sin6;
775 switch(sa->sa_family) {
778 * No mapping needed. Just check that it's a valid MC address.
780 sdl = (struct sockaddr_dl *)sa;
781 e_addr = LLADDR(sdl);
782 if ((e_addr[0] & 1) != 1)
783 return EADDRNOTAVAIL;
789 sin = (struct sockaddr_in *)sa;
790 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
791 return EADDRNOTAVAIL;
792 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
794 sdl->sdl_len = sizeof *sdl;
795 sdl->sdl_family = AF_LINK;
796 sdl->sdl_index = ifp->if_index;
797 sdl->sdl_type = IFT_ETHER;
798 sdl->sdl_alen = ETHER_ADDR_LEN;
799 e_addr = LLADDR(sdl);
800 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
801 *llsa = (struct sockaddr *)sdl;
806 sin6 = (struct sockaddr_in6 *)sa;
807 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
809 * An IP6 address of 0 means listen to all
810 * of the Ethernet multicast address used for IP6.
811 * (This is used for multicast routers.)
813 ifp->if_flags |= IFF_ALLMULTI;
817 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
818 return EADDRNOTAVAIL;
819 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
821 sdl->sdl_len = sizeof *sdl;
822 sdl->sdl_family = AF_LINK;
823 sdl->sdl_index = ifp->if_index;
824 sdl->sdl_type = IFT_ETHER;
825 sdl->sdl_alen = ETHER_ADDR_LEN;
826 e_addr = LLADDR(sdl);
827 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
828 *llsa = (struct sockaddr *)sdl;
834 * Well, the text isn't quite right, but it's the name
843 * This is for reference. We have a table-driven version
844 * of the little-endian crc32 generator, which is faster
845 * than the double-loop.
848 ether_crc32_le(const uint8_t *buf, size_t len)
850 uint32_t c, crc, carry;
853 crc = 0xffffffffU; /* initial value */
855 for (i = 0; i < len; i++) {
857 for (j = 0; j < 8; j++) {
858 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
862 crc = (crc ^ ETHER_CRC_POLY_LE);
870 ether_crc32_le(const uint8_t *buf, size_t len)
872 static const uint32_t crctab[] = {
873 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
874 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
875 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
876 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
881 crc = 0xffffffffU; /* initial value */
883 for (i = 0; i < len; i++) {
885 crc = (crc >> 4) ^ crctab[crc & 0xf];
886 crc = (crc >> 4) ^ crctab[crc & 0xf];
894 ether_crc32_be(const uint8_t *buf, size_t len)
896 uint32_t c, crc, carry;
899 crc = 0xffffffffU; /* initial value */
901 for (i = 0; i < len; i++) {
903 for (j = 0; j < 8; j++) {
904 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
908 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
916 * find the size of ethernet header, and call classifier
919 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
920 struct altq_pktattr *pktattr)
922 struct ether_header *eh;
924 int hlen, af, hdrsize;
927 hlen = sizeof(struct ether_header);
928 eh = mtod(m, struct ether_header *);
930 ether_type = ntohs(eh->ether_type);
931 if (ether_type < ETHERMTU) {
933 struct llc *llc = (struct llc *)(eh + 1);
936 if (m->m_len < hlen ||
937 llc->llc_dsap != LLC_SNAP_LSAP ||
938 llc->llc_ssap != LLC_SNAP_LSAP ||
939 llc->llc_control != LLC_UI)
940 goto bad; /* not snap! */
942 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
945 if (ether_type == ETHERTYPE_IP) {
947 hdrsize = 20; /* sizeof(struct ip) */
949 } else if (ether_type == ETHERTYPE_IPV6) {
951 hdrsize = 40; /* sizeof(struct ip6_hdr) */
956 while (m->m_len <= hlen) {
960 hdr = m->m_data + hlen;
961 if (m->m_len < hlen + hdrsize) {
963 * ip header is not in a single mbuf. this should not
964 * happen in the current code.
965 * (todo: use m_pulldown in the future)
971 ifq_classify(ifq, m, af, pktattr);
978 pktattr->pattr_class = NULL;
979 pktattr->pattr_hdr = NULL;
980 pktattr->pattr_af = AF_UNSPEC;
984 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
985 const struct ether_header *save_eh)
987 struct mbuf *m = *m0;
992 * Prepend the header, optimize for the common case of
993 * eh pointing into the mbuf.
995 if ((const void *)(eh + 1) == (void *)m->m_data) {
996 m->m_data -= ETHER_HDR_LEN;
997 m->m_len += ETHER_HDR_LEN;
998 m->m_pkthdr.len += ETHER_HDR_LEN;
1000 ether_prepend_hdr++;
1002 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1004 bcopy(save_eh, mtod(m, struct ether_header *),
1012 ether_input_ipifunc(void *arg)
1014 struct mbuf *m, *next;
1019 next = m->m_nextpkt;
1020 m->m_nextpkt = NULL;
1022 port = m->m_pkthdr.header;
1023 m->m_pkthdr.header = NULL;
1026 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1029 } while (m != NULL);
1033 ether_input_dispatch(struct mbuf_chain *chain)
1038 logether(disp_beg, NULL);
1039 for (i = 0; i < ncpus; ++i) {
1040 if (chain[i].mc_head != NULL) {
1041 lwkt_send_ipiq(globaldata_find(i),
1042 ether_input_ipifunc, chain[i].mc_head);
1046 logether(disp_beg, NULL);
1047 if (chain->mc_head != NULL)
1048 ether_input_ipifunc(chain->mc_head);
1050 logether(disp_end, NULL);
1054 ether_input_chain_init(struct mbuf_chain *chain)
1059 for (i = 0; i < ncpus; ++i)
1060 chain[i].mc_head = chain[i].mc_tail = NULL;
1062 chain->mc_head = chain->mc_tail = NULL;
1067 * Upper layer processing for a received Ethernet packet.
1070 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1072 struct ether_header *eh;
1073 int isr, redispatch;
1075 struct ip_fw *rule = NULL;
1081 KASSERT(m->m_len >= ETHER_HDR_LEN,
1082 ("ether header is no contiguous!\n"));
1084 eh = mtod(m, struct ether_header *);
1086 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1089 /* Extract info from dummynet tag */
1090 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1091 KKASSERT(mtag != NULL);
1092 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1093 KKASSERT(rule != NULL);
1095 m_tag_delete(m, mtag);
1096 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1098 /* packet is passing the second time */
1104 * XXX: Okay, we need to call carp_forus() and - if it is for
1105 * us jump over code that does the normal check
1106 * "ac_enaddr == ether_dhost". The check sequence is a bit
1107 * different from OpenBSD, so we jump over as few code as
1108 * possible, to catch _all_ sanity checks. This needs
1109 * evaluation, to see if the carp ether_dhost values break any
1114 * Hold BGL and recheck ifp->if_carp
1117 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1126 * Discard packet if upper layers shouldn't see it because
1127 * it was unicast to a different Ethernet address. If the
1128 * driver is working properly, then this situation can only
1129 * happen when the interface is in promiscuous mode.
1131 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1132 (eh->ether_dhost[0] & 1) == 0 &&
1133 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1139 if (IPFW_LOADED && ether_ipfw != 0) {
1140 struct ether_header save_eh = *eh;
1142 /* XXX old crufty stuff, needs to be removed */
1143 m_adj(m, sizeof(struct ether_header));
1145 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1150 ether_restore_header(&m, eh, &save_eh);
1153 eh = mtod(m, struct ether_header *);
1156 ether_type = ntohs(eh->ether_type);
1157 KKASSERT(ether_type != ETHERTYPE_VLAN);
1159 if (m->m_flags & M_VLANTAG) {
1160 void (*vlan_input_func)(struct mbuf *);
1162 vlan_input_func = vlan_input_p;
1163 if (vlan_input_func != NULL) {
1166 m->m_pkthdr.rcvif->if_noproto++;
1172 m_adj(m, sizeof(struct ether_header));
1175 switch (ether_type) {
1178 if (ipflow_fastforward(m))
1184 if (ifp->if_flags & IFF_NOARP) {
1185 /* Discard packet if ARP is disabled on interface */
1194 case ETHERTYPE_IPV6:
1203 * Hold BGL and recheck ef_inputp
1206 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1217 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1225 isr = NETISR_ATALK1;
1227 case ETHERTYPE_AARP:
1233 case ETHERTYPE_MPLS:
1234 case ETHERTYPE_MPLS_MCAST:
1235 /* Should have been set by ether_input_chain(). */
1236 KKASSERT(m->m_flags & M_MPLSLABELED);
1243 * The accurate msgport is not determined before
1244 * we reach here, so redo the dispatching
1250 * Hold BGL and recheck ef_inputp
1253 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1261 checksum = mtod(m, ushort *);
1263 if ((ether_type <= ETHERMTU) &&
1264 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1265 if (*checksum == 0xE0E0) {
1266 m->m_pkthdr.len -= 3;
1275 if (ether_type > ETHERMTU)
1277 l = mtod(m, struct llc *);
1278 if (l->llc_dsap == LLC_SNAP_LSAP &&
1279 l->llc_ssap == LLC_SNAP_LSAP &&
1280 l->llc_control == LLC_UI) {
1281 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1282 sizeof at_org_code) == 0 &&
1283 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1284 m_adj(m, sizeof(struct llc));
1285 isr = NETISR_ATALK2;
1288 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1289 sizeof aarp_org_code) == 0 &&
1290 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1291 m_adj(m, sizeof(struct llc));
1298 if (ng_ether_input_orphan_p != NULL) {
1300 * Hold BGL and recheck ng_ether_input_orphan_p
1303 if (ng_ether_input_orphan_p != NULL) {
1304 ng_ether_input_orphan_p(ifp, m, eh);
1317 netisr_dispatch(isr, m);
1321 * First we perform any link layer operations, then continue to the
1322 * upper layers with ether_demux_oncpu().
1325 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1327 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1329 * Receiving interface's flags are changed, when this
1330 * packet is waiting for processing; discard it.
1337 * Tap the packet off here for a bridge. bridge_input()
1338 * will return NULL if it has consumed the packet, otherwise
1339 * it gets processed as normal. Note that bridge_input()
1340 * will always return the original packet if we need to
1341 * process it locally.
1343 if (ifp->if_bridge) {
1344 KASSERT(bridge_input_p != NULL,
1345 ("%s: if_bridge not loaded!", __func__));
1347 if(m->m_flags & M_PROTO1) {
1348 m->m_flags &= ~M_PROTO1;
1350 /* clear M_PROMISC, in case the packets comes from a vlan */
1351 /* m->m_flags &= ~M_PROMISC; */
1352 m = bridge_input_p(ifp, m);
1356 KASSERT(ifp == m->m_pkthdr.rcvif,
1357 ("bridge_input_p changed rcvif\n"));
1361 /* Handle ng_ether(4) processing, if any */
1362 if (ng_ether_input_p != NULL) {
1364 * Hold BGL and recheck ng_ether_input_p
1367 if (ng_ether_input_p != NULL)
1368 ng_ether_input_p(ifp, &m);
1375 /* Continue with upper layer processing */
1376 ether_demux_oncpu(ifp, m);
1380 ether_input_handler(struct netmsg *nmsg)
1382 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1388 ifp = m->m_pkthdr.rcvif;
1390 ether_input_oncpu(ifp, m);
1393 static __inline void
1394 ether_init_netpacket(int num, struct mbuf *m)
1396 struct netmsg_packet *pmsg;
1398 pmsg = &m->m_hdr.mh_netmsg;
1399 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, MSGF_MPSAFE,
1400 ether_input_handler);
1401 pmsg->nm_packet = m;
1402 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1405 static __inline struct lwkt_port *
1406 ether_mport(int num, struct mbuf **m)
1408 if (num == NETISR_MAX) {
1410 * All packets whose target msgports can't be
1411 * determined here are dispatched to netisr0,
1412 * where further dispatching may happen.
1414 return cpu_portfn(0);
1416 return netisr_find_port(num, m);
1420 * Process a received Ethernet packet.
1422 * The ethernet header is assumed to be in the mbuf so the caller
1423 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1424 * bytes in the first mbuf.
1426 * We first try to find the target msgport for this ether frame, if
1427 * there is no target msgport for it, this ether frame is discarded,
1428 * else we do following processing according to whether 'chain' is
1430 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1431 * immediately. This situation happens when ether_input_chain is
1432 * accessed through ifnet.if_input.
1433 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1434 * bucket indexed by the target msgport's cpuid and the target msgport
1435 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1436 * must initialize 'chain' by calling ether_input_chain_init().
1437 * ether_input_dispatch must be called later to send ether frames
1438 * queued on 'chain' to their target msgport.
1441 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1443 struct ether_header *eh, *save_eh, save_eh0;
1444 struct lwkt_port *port;
1445 uint16_t ether_type;
1448 ASSERT_SERIALIZED(ifp->if_serializer);
1451 /* Discard packet if interface is not up */
1452 if (!(ifp->if_flags & IFF_UP)) {
1457 if (m->m_len < sizeof(struct ether_header)) {
1458 /* XXX error in the caller. */
1462 eh = mtod(m, struct ether_header *);
1464 m->m_pkthdr.rcvif = ifp;
1466 logether(chain_beg, ifp);
1468 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1469 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1470 ifp->if_addrlen) == 0)
1471 m->m_flags |= M_BCAST;
1473 m->m_flags |= M_MCAST;
1477 ETHER_BPF_MTAP(ifp, m);
1479 ifp->if_ibytes += m->m_pkthdr.len;
1481 if (ifp->if_flags & IFF_MONITOR) {
1483 * Interface marked for monitoring; discard packet.
1487 logether(chain_end, ifp);
1491 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1492 (m->m_flags & M_VLANTAG) == 0) {
1494 * Extract vlan tag if hardware does not do it for us
1496 vlan_ether_decap(&m);
1499 eh = mtod(m, struct ether_header *);
1501 ether_type = ntohs(eh->ether_type);
1503 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1505 * To prevent possible dangerous recursion,
1506 * we don't do vlan-in-vlan
1512 KKASSERT(ether_type != ETHERTYPE_VLAN);
1515 * Map ether type to netisr id.
1517 switch (ether_type) {
1529 case ETHERTYPE_IPV6:
1541 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1548 isr = NETISR_ATALK1;
1550 case ETHERTYPE_AARP:
1556 case ETHERTYPE_MPLS:
1557 case ETHERTYPE_MPLS_MCAST:
1558 m->m_flags |= M_MPLSLABELED;
1565 * NETISR_MAX is an invalid value; it is chosen to let
1566 * ether_mport() know that we are not able to decide
1567 * this packet's msgport here.
1574 * If the packet is in contiguous memory, following
1575 * m_adj() could ensure that the hidden ether header
1576 * will not be destroyed, else we will have to save
1577 * the ether header for the later restoration.
1579 if (m->m_pkthdr.len != m->m_len) {
1581 save_eh = &save_eh0;
1587 * Temporarily remove ether header; ether_mport()
1588 * expects a packet without ether header.
1590 m_adj(m, sizeof(struct ether_header));
1593 * Find the packet's target msgport.
1595 port = ether_mport(isr, &m);
1597 KKASSERT(m == NULL);
1602 * Restore ether header.
1604 if (save_eh != NULL) {
1605 ether_restore_header(&m, eh, save_eh);
1609 m->m_data -= ETHER_HDR_LEN;
1610 m->m_len += ETHER_HDR_LEN;
1611 m->m_pkthdr.len += ETHER_HDR_LEN;
1615 * Initialize mbuf's netmsg packet _after_ possible
1616 * ether header restoration, else the initialized
1617 * netmsg packet may be lost during ether header
1620 ether_init_netpacket(isr, m);
1622 if (chain != NULL) {
1623 struct mbuf_chain *c;
1626 m->m_pkthdr.header = port; /* XXX */
1627 cpuid = port->mpu_td->td_gd->gd_cpuid;
1630 if (c->mc_head == NULL) {
1631 c->mc_head = c->mc_tail = m;
1633 c->mc_tail->m_nextpkt = m;
1636 m->m_nextpkt = NULL;
1638 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1640 logether(chain_end, ifp);