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.68 2008/06/23 11:57:19 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
42 #include "opt_netgraph.h"
44 #include "opt_ethernet.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/msgport.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/thread.h>
57 #include <sys/thread2.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/if_llc.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/ifq_var.h>
67 #include <net/ethernet.h>
68 #include <net/vlan/if_vlan_ether.h>
69 #include <net/netmsg2.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <net/ipfw/ip_fw.h>
76 #include <net/dummynet/ip_dummynet.h>
79 #include <netinet6/nd6.h>
83 #include <netinet/ip_carp.h>
87 #include <netproto/ipx/ipx.h>
88 #include <netproto/ipx/ipx_if.h>
89 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
90 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
91 short *tp, int *hlen);
96 #include <netns/ns_if.h>
98 int ether_outputdebug = 0;
99 int ether_inputdebug = 0;
103 #include <netproto/atalk/at.h>
104 #include <netproto/atalk/at_var.h>
105 #include <netproto/atalk/at_extern.h>
107 #define llc_snap_org_code llc_un.type_snap.org_code
108 #define llc_snap_ether_type llc_un.type_snap.ether_type
110 extern u_char at_org_code[3];
111 extern u_char aarp_org_code[3];
112 #endif /* NETATALK */
114 /* netgraph node hooks for ng_ether(4) */
115 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
116 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
117 struct mbuf *m, const struct ether_header *eh);
118 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
119 void (*ng_ether_attach_p)(struct ifnet *ifp);
120 void (*ng_ether_detach_p)(struct ifnet *ifp);
122 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *);
124 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
126 static void ether_restore_header(struct mbuf **, const struct ether_header *,
127 const struct ether_header *);
128 static void ether_demux_chain(struct ifnet *, struct mbuf *,
129 struct mbuf_chain *);
134 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
135 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
136 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
138 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
141 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
145 #define gotoerr(e) do { error = (e); goto bad; } while (0)
146 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
148 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
150 const struct ether_header *eh);
152 static int ether_ipfw;
153 static u_int ether_restore_hdr;
154 static u_int ether_prepend_hdr;
156 SYSCTL_DECL(_net_link);
157 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
158 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
159 ðer_ipfw, 0, "Pass ether pkts through firewall");
160 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
161 ðer_restore_hdr, 0, "# of ether header restoration");
162 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
163 ðer_prepend_hdr, 0,
164 "# of ether header restoration which prepends mbuf");
167 * Ethernet output routine.
168 * Encapsulate a packet of type family for the local net.
169 * Use trailer local net encapsulation if enough data in first
170 * packet leaves a multiple of 512 bytes of data in remainder.
171 * Assumes that ifp is actually pointer to arpcom structure.
174 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
177 struct ether_header *eh, *deh;
180 int hlen = ETHER_HDR_LEN; /* link layer header length */
181 struct arpcom *ac = IFP2AC(ifp);
184 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
186 if (ifp->if_flags & IFF_MONITOR)
188 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
191 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
194 eh = mtod(m, struct ether_header *);
195 edst = eh->ether_dhost;
198 * Fill in the destination ethernet address and frame type.
200 switch (dst->sa_family) {
203 if (!arpresolve(ifp, rt, m, dst, edst))
204 return (0); /* if not yet resolved */
205 eh->ether_type = htons(ETHERTYPE_IP);
210 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
211 return (0); /* Something bad happenned. */
212 eh->ether_type = htons(ETHERTYPE_IPV6);
217 if (ef_outputp != NULL) {
218 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
223 eh->ether_type = htons(ETHERTYPE_IPX);
224 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
225 edst, ETHER_ADDR_LEN);
231 struct at_ifaddr *aa;
233 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
238 * In the phase 2 case, need to prepend an mbuf for
239 * the llc header. Since we must preserve the value
240 * of m, which is passed to us by value, we m_copy()
241 * the first mbuf, and use it for our llc header.
243 if (aa->aa_flags & AFA_PHASE2) {
246 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
247 eh = mtod(m, struct ether_header *);
248 edst = eh->ether_dhost;
249 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
250 llc.llc_control = LLC_UI;
251 bcopy(at_org_code, llc.llc_snap_org_code,
253 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
255 mtod(m, caddr_t) + sizeof(struct ether_header),
257 eh->ether_type = htons(m->m_pkthdr.len);
258 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
260 eh->ether_type = htons(ETHERTYPE_AT);
262 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
271 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
272 eh->ether_type = 0x8137;
274 case 0x0: /* Novell 802.3 */
275 eh->ether_type = htons(m->m_pkthdr.len);
277 case 0xe0e0: /* Novell 802.2 and Token-Ring */
278 M_PREPEND(m, 3, MB_DONTWAIT);
279 eh = mtod(m, struct ether_header *);
280 edst = eh->ether_dhost;
281 eh->ether_type = htons(m->m_pkthdr.len);
282 cp = mtod(m, u_char *) + sizeof(struct ether_header);
288 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
291 * XXX if ns_thishost is the same as the node's ethernet
292 * address then just the default code will catch this anyhow.
293 * So I'm not sure if this next clause should be here at all?
296 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
297 m->m_pkthdr.rcvif = ifp;
298 netisr_dispatch(NETISR_NS, m);
301 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
302 m->m_flags |= M_BCAST;
305 case pseudo_AF_HDRCMPLT:
307 loop_copy = -1; /* if this is for us, don't do it */
308 deh = (struct ether_header *)dst->sa_data;
309 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
310 eh->ether_type = deh->ether_type;
314 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
315 gotoerr(EAFNOSUPPORT);
318 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
319 memcpy(eh->ether_shost,
320 ((struct ether_header *)dst->sa_data)->ether_shost,
323 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
326 * Bridges require special output handling.
328 if (ifp->if_bridge) {
329 KASSERT(bridge_output_p != NULL,
330 ("%s: if_bridge not loaded!", __func__));
331 return bridge_output_p(ifp, m);
335 * If a simplex interface, and the packet is being sent to our
336 * Ethernet address or a broadcast address, loopback a copy.
337 * XXX To make a simplex device behave exactly like a duplex
338 * device, we should copy in the case of sending to our own
339 * ethernet address (thus letting the original actually appear
340 * on the wire). However, we don't do that here for security
341 * reasons and compatibility with the original behavior.
343 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
346 if (m->m_pkthdr.csum_flags & CSUM_IP)
347 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
348 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
349 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
350 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
353 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
354 n->m_pkthdr.csum_flags |= csum_flags;
355 if (csum_flags & CSUM_DATA_VALID)
356 n->m_pkthdr.csum_data = 0xffff;
357 if_simloop(ifp, n, dst->sa_family, hlen);
360 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
361 ETHER_ADDR_LEN) == 0) {
362 m->m_pkthdr.csum_flags |= csum_flags;
363 if (csum_flags & CSUM_DATA_VALID)
364 m->m_pkthdr.csum_data = 0xffff;
365 if_simloop(ifp, m, dst->sa_family, hlen);
366 return (0); /* XXX */
371 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
376 /* Handle ng_ether(4) processing, if any */
377 if (ng_ether_output_p != NULL) {
378 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
384 /* Continue with link-layer output */
385 return ether_output_frame(ifp, m);
393 * Ethernet link layer output routine to send a raw frame to the device.
395 * This assumes that the 14 byte Ethernet header is present and contiguous
399 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
401 struct ip_fw *rule = NULL;
403 struct altq_pktattr pktattr;
406 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
408 /* Extract info from dummynet tag */
409 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
411 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
413 m_tag_delete(m, mtag);
417 if (ifq_is_enabled(&ifp->if_snd))
418 altq_etherclassify(&ifp->if_snd, m, &pktattr);
420 if (IPFW_LOADED && ether_ipfw != 0) {
421 struct ether_header save_eh, *eh;
423 eh = mtod(m, struct ether_header *);
425 m_adj(m, ETHER_HDR_LEN);
426 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
430 return ENOBUFS; /* pkt dropped */
432 return 0; /* consumed e.g. in a pipe */
435 /* packet was ok, restore the ethernet header */
436 ether_restore_header(&m, eh, &save_eh);
445 * Queue message on interface, update output statistics if
446 * successful, and start output if interface not yet active.
448 error = ifq_dispatch(ifp, m, &pktattr);
453 * ipfw processing for ethernet packets (in and out).
454 * The second parameter is NULL from ether_demux(), and ifp from
455 * ether_output_frame().
458 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
459 const struct ether_header *eh)
461 struct ether_header save_eh = *eh; /* might be a ptr in m */
462 struct ip_fw_args args;
466 if (*rule != NULL && fw_one_pass)
467 return TRUE; /* dummynet packet, already partially processed */
470 * I need some amount of data to be contiguous.
472 i = min((*m0)->m_pkthdr.len, max_protohdr);
473 if ((*m0)->m_len < i) {
474 *m0 = m_pullup(*m0, i);
479 args.m = *m0; /* the packet we are looking at */
480 args.oif = dst; /* destination, if any */
481 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
482 m_tag_delete(*m0, mtag);
483 args.rule = *rule; /* matching rule to restart */
484 args.next_hop = NULL; /* we do not support forward yet */
485 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
486 i = ip_fw_chk_ptr(&args);
490 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
493 if (i == 0) /* a PASS rule. */
496 if (i & IP_FW_PORT_DYNT_FLAG) {
498 * Pass the pkt to dummynet, which consumes it.
502 m = *m0; /* pass the original to dummynet */
503 *m0 = NULL; /* and nothing back to the caller */
505 ether_restore_header(&m, eh, &save_eh);
509 ip_fw_dn_io_ptr(m, (i & 0xffff),
510 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
514 * XXX at some point add support for divert/forward actions.
515 * If none of the above matches, we have to drop the pkt.
521 * Process a received Ethernet packet.
523 * The ethernet header is assumed to be in the mbuf so the caller
524 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
525 * bytes in the first mbuf.
527 * This allows us to concentrate in one place a bunch of code which
528 * is replicated in all device drivers. Also, many functions called
529 * from ether_input() try to put the eh back into the mbuf, so we
530 * can later propagate the 'contiguous packet' interface to them.
532 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
533 * cluster, right before m_data. So be very careful when working on m,
534 * as you could destroy *eh !!
536 * First we perform any link layer operations, then continue to the
537 * upper layers with ether_demux().
540 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
542 struct ether_header *eh;
544 ASSERT_SERIALIZED(ifp->if_serializer);
547 /* Discard packet if interface is not up */
548 if (!(ifp->if_flags & IFF_UP)) {
553 if (m->m_len < sizeof(struct ether_header)) {
554 /* XXX error in the caller. */
558 eh = mtod(m, struct ether_header *);
560 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
561 (m->m_flags & M_VLANTAG) == 0) {
563 * Extract vlan tag if hardware does not do it for us
565 vlan_ether_decap(&m);
568 eh = mtod(m, struct ether_header *);
571 m->m_pkthdr.rcvif = ifp;
573 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
574 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
575 ifp->if_addrlen) == 0)
576 m->m_flags |= M_BCAST;
578 m->m_flags |= M_MCAST;
582 ETHER_BPF_MTAP(ifp, m);
584 ifp->if_ibytes += m->m_pkthdr.len;
586 if (ifp->if_flags & IFF_MONITOR) {
588 * Interface marked for monitoring; discard packet.
595 * Tap the packet off here for a bridge. bridge_input()
596 * will return NULL if it has consumed the packet, otherwise
597 * it gets processed as normal. Note that bridge_input()
598 * will always return the original packet if we need to
599 * process it locally.
601 if (ifp->if_bridge) {
602 KASSERT(bridge_input_p != NULL,
603 ("%s: if_bridge not loaded!", __func__));
605 if(m->m_flags & M_PROTO1) {
606 m->m_flags &= ~M_PROTO1;
608 /* clear M_PROMISC, in case the packets comes from a vlan */
609 /* m->m_flags &= ~M_PROMISC; */
610 lwkt_serialize_exit(ifp->if_serializer);
611 m = bridge_input_p(ifp, m);
612 lwkt_serialize_enter(ifp->if_serializer);
616 KASSERT(ifp == m->m_pkthdr.rcvif,
617 ("bridge_input_p changed rcvif\n"));
619 /* 'm' may be changed by bridge_input_p() */
620 eh = mtod(m, struct ether_header *);
624 /* Handle ng_ether(4) processing, if any */
625 if (ng_ether_input_p != NULL) {
626 ng_ether_input_p(ifp, &m);
630 /* 'm' may be changed by ng_ether_input_p() */
631 eh = mtod(m, struct ether_header *);
634 /* Continue with upper layer processing */
635 ether_demux_chain(ifp, m, chain);
639 ether_input(struct ifnet *ifp, struct mbuf *m)
641 ether_input_chain(ifp, m, NULL);
645 * Upper layer processing for a received Ethernet packet.
648 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
650 struct ether_header save_eh, *eh;
653 struct ip_fw *rule = NULL;
660 KASSERT(m->m_len >= ETHER_HDR_LEN,
661 ("ether header is no contiguous!\n"));
663 eh = mtod(m, struct ether_header *);
666 /* XXX old crufty stuff, needs to be removed */
667 m_adj(m, sizeof(struct ether_header));
669 /* Extract info from dummynet tag */
670 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
672 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
673 KKASSERT(ifp == NULL);
674 ifp = m->m_pkthdr.rcvif;
676 m_tag_delete(m, mtag);
679 if (rule) /* packet is passing the second time */
684 * XXX: Okay, we need to call carp_forus() and - if it is for
685 * us jump over code that does the normal check
686 * "ac_enaddr == ether_dhost". The check sequence is a bit
687 * different from OpenBSD, so we jump over as few code as
688 * possible, to catch _all_ sanity checks. This needs
689 * evaluation, to see if the carp ether_dhost values break any
692 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
697 * Discard packet if upper layers shouldn't see it because
698 * it was unicast to a different Ethernet address. If the
699 * driver is working properly, then this situation can only
700 * happen when the interface is in promiscuous mode.
702 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
703 (eh->ether_dhost[0] & 1) == 0 &&
704 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
710 if (IPFW_LOADED && ether_ipfw != 0) {
711 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
717 ether_type = ntohs(save_eh.ether_type);
719 if (m->m_flags & M_VLANTAG) {
720 if (ether_type == ETHERTYPE_VLAN) {
722 * To prevent possible dangerous recursion,
723 * we don't do vlan-in-vlan
725 m->m_pkthdr.rcvif->if_noproto++;
730 if (vlan_input_p != NULL) {
731 ether_restore_header(&m, eh, &save_eh);
733 vlan_input_p(m, chain);
735 m->m_pkthdr.rcvif->if_noproto++;
740 KKASSERT(ether_type != ETHERTYPE_VLAN);
742 switch (ether_type) {
745 if (ipflow_fastforward(m, ifp->if_serializer))
751 if (ifp->if_flags & IFF_NOARP) {
752 /* Discard packet if ARP is disabled on interface */
768 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
775 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
792 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
796 checksum = mtod(m, ushort *);
798 if ((ether_type <= ETHERMTU) &&
799 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
800 if (*checksum == 0xE0E0) {
801 m->m_pkthdr.len -= 3;
810 if (ether_type > ETHERMTU)
812 l = mtod(m, struct llc *);
813 if (l->llc_dsap == LLC_SNAP_LSAP &&
814 l->llc_ssap == LLC_SNAP_LSAP &&
815 l->llc_control == LLC_UI) {
816 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
817 sizeof at_org_code) == 0 &&
818 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
819 m_adj(m, sizeof(struct llc));
823 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
824 sizeof aarp_org_code) == 0 &&
825 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
826 m_adj(m, sizeof(struct llc));
833 if (ng_ether_input_orphan_p != NULL)
834 (*ng_ether_input_orphan_p)(ifp, m, &save_eh);
840 #ifdef ETHER_INPUT_CHAIN
842 struct mbuf_chain *c;
846 port = netisr_mport(isr, &m);
850 m->m_pkthdr.header = port; /* XXX */
851 cpuid = port->mpu_td->td_gd->gd_cpuid;
854 if (c->mc_head == NULL) {
855 c->mc_head = c->mc_tail = m;
857 c->mc_tail->m_nextpkt = m;
862 #endif /* ETHER_INPUT_CHAIN */
863 netisr_dispatch(isr, m);
867 ether_demux(struct ifnet *ifp, struct mbuf *m)
869 ether_demux_chain(ifp, m, NULL);
873 * Perform common duties while attaching to interface list
877 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
879 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
884 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
885 lwkt_serialize_t serializer)
887 struct sockaddr_dl *sdl;
889 ifp->if_type = IFT_ETHER;
890 ifp->if_addrlen = ETHER_ADDR_LEN;
891 ifp->if_hdrlen = ETHER_HDR_LEN;
892 if_attach(ifp, serializer);
893 ifp->if_mtu = ETHERMTU;
894 if (ifp->if_baudrate == 0)
895 ifp->if_baudrate = 10000000;
896 ifp->if_output = ether_output;
897 ifp->if_input = ether_input;
898 ifp->if_resolvemulti = ether_resolvemulti;
899 ifp->if_broadcastaddr = etherbroadcastaddr;
900 sdl = IF_LLSOCKADDR(ifp);
901 sdl->sdl_type = IFT_ETHER;
902 sdl->sdl_alen = ifp->if_addrlen;
903 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
905 * XXX Keep the current drivers happy.
906 * XXX Remove once all drivers have been cleaned up
908 if (lla != IFP2AC(ifp)->ac_enaddr)
909 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
910 bpfattach(ifp, dlt, hdrlen);
911 if (ng_ether_attach_p != NULL)
912 (*ng_ether_attach_p)(ifp);
914 if_printf(ifp, "MAC address: %6D\n", lla, ":");
918 * Perform common duties while detaching an Ethernet interface
921 ether_ifdetach(struct ifnet *ifp)
925 if (ng_ether_detach_p != NULL)
926 (*ng_ether_detach_p)(ifp);
932 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
934 struct ifaddr *ifa = (struct ifaddr *) data;
935 struct ifreq *ifr = (struct ifreq *) data;
938 #define IF_INIT(ifp) \
940 if (((ifp)->if_flags & IFF_UP) == 0) { \
941 (ifp)->if_flags |= IFF_UP; \
942 (ifp)->if_init((ifp)->if_softc); \
946 ASSERT_SERIALIZED(ifp->if_serializer);
950 switch (ifa->ifa_addr->sa_family) {
953 IF_INIT(ifp); /* before arpwhohas */
954 arp_ifinit(ifp, ifa);
959 * XXX - This code is probably wrong
963 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
964 struct arpcom *ac = IFP2AC(ifp);
966 if (ipx_nullhost(*ina))
967 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
969 bcopy(ina->x_host.c_host, ac->ac_enaddr,
970 sizeof ac->ac_enaddr);
972 IF_INIT(ifp); /* Set new address. */
978 * XXX - This code is probably wrong
982 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
983 struct arpcom *ac = IFP2AC(ifp);
985 if (ns_nullhost(*ina))
986 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
988 bcopy(ina->x_host.c_host, ac->ac_enaddr,
989 sizeof ac->ac_enaddr);
1005 bcopy(IFP2AC(ifp)->ac_enaddr,
1006 ((struct sockaddr *)ifr->ifr_data)->sa_data,
1012 * Set the interface MTU.
1014 if (ifr->ifr_mtu > ETHERMTU) {
1017 ifp->if_mtu = ifr->ifr_mtu;
1032 struct sockaddr **llsa,
1033 struct sockaddr *sa)
1035 struct sockaddr_dl *sdl;
1036 struct sockaddr_in *sin;
1038 struct sockaddr_in6 *sin6;
1042 switch(sa->sa_family) {
1045 * No mapping needed. Just check that it's a valid MC address.
1047 sdl = (struct sockaddr_dl *)sa;
1048 e_addr = LLADDR(sdl);
1049 if ((e_addr[0] & 1) != 1)
1050 return EADDRNOTAVAIL;
1056 sin = (struct sockaddr_in *)sa;
1057 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1058 return EADDRNOTAVAIL;
1059 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1061 sdl->sdl_len = sizeof *sdl;
1062 sdl->sdl_family = AF_LINK;
1063 sdl->sdl_index = ifp->if_index;
1064 sdl->sdl_type = IFT_ETHER;
1065 sdl->sdl_alen = ETHER_ADDR_LEN;
1066 e_addr = LLADDR(sdl);
1067 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1068 *llsa = (struct sockaddr *)sdl;
1073 sin6 = (struct sockaddr_in6 *)sa;
1074 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1076 * An IP6 address of 0 means listen to all
1077 * of the Ethernet multicast address used for IP6.
1078 * (This is used for multicast routers.)
1080 ifp->if_flags |= IFF_ALLMULTI;
1084 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1085 return EADDRNOTAVAIL;
1086 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1088 sdl->sdl_len = sizeof *sdl;
1089 sdl->sdl_family = AF_LINK;
1090 sdl->sdl_index = ifp->if_index;
1091 sdl->sdl_type = IFT_ETHER;
1092 sdl->sdl_alen = ETHER_ADDR_LEN;
1093 e_addr = LLADDR(sdl);
1094 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1095 *llsa = (struct sockaddr *)sdl;
1101 * Well, the text isn't quite right, but it's the name
1104 return EAFNOSUPPORT;
1110 * This is for reference. We have a table-driven version
1111 * of the little-endian crc32 generator, which is faster
1112 * than the double-loop.
1115 ether_crc32_le(const uint8_t *buf, size_t len)
1117 uint32_t c, crc, carry;
1120 crc = 0xffffffffU; /* initial value */
1122 for (i = 0; i < len; i++) {
1124 for (j = 0; j < 8; j++) {
1125 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1129 crc = (crc ^ ETHER_CRC_POLY_LE);
1137 ether_crc32_le(const uint8_t *buf, size_t len)
1139 static const uint32_t crctab[] = {
1140 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1141 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1142 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1143 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1148 crc = 0xffffffffU; /* initial value */
1150 for (i = 0; i < len; i++) {
1152 crc = (crc >> 4) ^ crctab[crc & 0xf];
1153 crc = (crc >> 4) ^ crctab[crc & 0xf];
1161 ether_crc32_be(const uint8_t *buf, size_t len)
1163 uint32_t c, crc, carry;
1166 crc = 0xffffffffU; /* initial value */
1168 for (i = 0; i < len; i++) {
1170 for (j = 0; j < 8; j++) {
1171 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1175 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1183 * find the size of ethernet header, and call classifier
1186 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1187 struct altq_pktattr *pktattr)
1189 struct ether_header *eh;
1190 uint16_t ether_type;
1191 int hlen, af, hdrsize;
1194 hlen = sizeof(struct ether_header);
1195 eh = mtod(m, struct ether_header *);
1197 ether_type = ntohs(eh->ether_type);
1198 if (ether_type < ETHERMTU) {
1200 struct llc *llc = (struct llc *)(eh + 1);
1203 if (m->m_len < hlen ||
1204 llc->llc_dsap != LLC_SNAP_LSAP ||
1205 llc->llc_ssap != LLC_SNAP_LSAP ||
1206 llc->llc_control != LLC_UI)
1207 goto bad; /* not snap! */
1209 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1212 if (ether_type == ETHERTYPE_IP) {
1214 hdrsize = 20; /* sizeof(struct ip) */
1216 } else if (ether_type == ETHERTYPE_IPV6) {
1218 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1223 while (m->m_len <= hlen) {
1227 hdr = m->m_data + hlen;
1228 if (m->m_len < hlen + hdrsize) {
1230 * ip header is not in a single mbuf. this should not
1231 * happen in the current code.
1232 * (todo: use m_pulldown in the future)
1238 ifq_classify(ifq, m, af, pktattr);
1245 pktattr->pattr_class = NULL;
1246 pktattr->pattr_hdr = NULL;
1247 pktattr->pattr_af = AF_UNSPEC;
1251 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1252 const struct ether_header *save_eh)
1254 struct mbuf *m = *m0;
1256 ether_restore_hdr++;
1259 * Prepend the header, optimize for the common case of
1260 * eh pointing into the mbuf.
1262 if ((const void *)(eh + 1) == (void *)m->m_data) {
1263 m->m_data -= ETHER_HDR_LEN;
1264 m->m_len += ETHER_HDR_LEN;
1265 m->m_pkthdr.len += ETHER_HDR_LEN;
1267 ether_prepend_hdr++;
1269 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1271 bcopy(save_eh, mtod(m, struct ether_header *),
1278 #ifdef ETHER_INPUT_CHAIN
1281 ether_input_ipifunc(void *arg)
1283 struct mbuf *m, *next;
1288 next = m->m_nextpkt;
1289 m->m_nextpkt = NULL;
1291 port = m->m_pkthdr.header;
1292 m->m_pkthdr.header = NULL;
1295 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1298 } while (m != NULL);
1302 ether_input_dispatch(struct mbuf_chain *chain)
1307 for (i = 0; i < ncpus; ++i) {
1308 if (chain[i].mc_head != NULL) {
1309 lwkt_send_ipiq(globaldata_find(i),
1310 ether_input_ipifunc, chain[i].mc_head);
1314 ether_input_ipifunc(chain->mc_head);
1318 #endif /* ETHER_INPUT_CHAIN */
1323 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1325 struct ether_header *eh;
1326 int isr, redispatch;
1328 struct ip_fw *rule = NULL;
1335 KASSERT(m->m_len >= ETHER_HDR_LEN,
1336 ("ether header is no contiguous!\n"));
1338 eh = mtod(m, struct ether_header *);
1340 /* Extract info from dummynet tag */
1341 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1343 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1344 KKASSERT(ifp == NULL);
1345 ifp = m->m_pkthdr.rcvif;
1347 m_tag_delete(m, mtag);
1350 if (rule) /* packet is passing the second time */
1355 * XXX: Okay, we need to call carp_forus() and - if it is for
1356 * us jump over code that does the normal check
1357 * "ac_enaddr == ether_dhost". The check sequence is a bit
1358 * different from OpenBSD, so we jump over as few code as
1359 * possible, to catch _all_ sanity checks. This needs
1360 * evaluation, to see if the carp ether_dhost values break any
1363 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1368 * Discard packet if upper layers shouldn't see it because
1369 * it was unicast to a different Ethernet address. If the
1370 * driver is working properly, then this situation can only
1371 * happen when the interface is in promiscuous mode.
1373 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1374 (eh->ether_dhost[0] & 1) == 0 &&
1375 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1381 if (IPFW_LOADED && ether_ipfw != 0) {
1382 struct ether_header save_eh = *eh;
1384 /* XXX old crufty stuff, needs to be removed */
1385 m_adj(m, sizeof(struct ether_header));
1387 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1392 ether_restore_header(&m, eh, &save_eh);
1395 eh = mtod(m, struct ether_header *);
1398 ether_type = ntohs(eh->ether_type);
1399 KKASSERT(ether_type != ETHERTYPE_VLAN);
1401 if (m->m_flags & M_VLANTAG) {
1404 if (vlan_input_p != NULL) {
1406 vlan_input_p(m, chain);
1408 m->m_pkthdr.rcvif->if_noproto++;
1412 m->m_pkthdr.rcvif->if_noproto++;
1418 m_adj(m, sizeof(struct ether_header));
1421 switch (ether_type) {
1425 if (ipflow_fastforward(m, ifp->if_serializer))
1432 if (ifp->if_flags & IFF_NOARP) {
1433 /* Discard packet if ARP is disabled on interface */
1442 case ETHERTYPE_IPV6:
1449 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1456 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1464 isr = NETISR_ATALK1;
1466 case ETHERTYPE_AARP:
1473 * The accurate msgport is not determined before
1474 * we reach here, so redo the dispatching
1478 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1482 checksum = mtod(m, ushort *);
1484 if ((ether_type <= ETHERMTU) &&
1485 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1486 if (*checksum == 0xE0E0) {
1487 m->m_pkthdr.len -= 3;
1496 if (ether_type > ETHERMTU)
1498 l = mtod(m, struct llc *);
1499 if (l->llc_dsap == LLC_SNAP_LSAP &&
1500 l->llc_ssap == LLC_SNAP_LSAP &&
1501 l->llc_control == LLC_UI) {
1502 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1503 sizeof at_org_code) == 0 &&
1504 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1505 m_adj(m, sizeof(struct llc));
1506 isr = NETISR_ATALK2;
1509 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1510 sizeof aarp_org_code) == 0 &&
1511 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1512 m_adj(m, sizeof(struct llc));
1519 if (ng_ether_input_orphan_p != NULL)
1520 ng_ether_input_orphan_p(ifp, m, eh);
1529 netisr_dispatch(isr, m);
1533 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1536 * Tap the packet off here for a bridge. bridge_input()
1537 * will return NULL if it has consumed the packet, otherwise
1538 * it gets processed as normal. Note that bridge_input()
1539 * will always return the original packet if we need to
1540 * process it locally.
1542 if (ifp->if_bridge) {
1543 KASSERT(bridge_input_p != NULL,
1544 ("%s: if_bridge not loaded!", __func__));
1546 if(m->m_flags & M_PROTO1) {
1547 m->m_flags &= ~M_PROTO1;
1549 /* clear M_PROMISC, in case the packets comes from a vlan */
1550 /* m->m_flags &= ~M_PROMISC; */
1551 m = bridge_input_p(ifp, m);
1555 KASSERT(ifp == m->m_pkthdr.rcvif,
1556 ("bridge_input_p changed rcvif\n"));
1560 /* Handle ng_ether(4) processing, if any */
1561 if (ng_ether_input_p != NULL) {
1562 ng_ether_input_p(ifp, &m);
1567 /* Continue with upper layer processing */
1568 ether_demux_oncpu(ifp, m);
1572 ether_input_handler(struct netmsg *nmsg)
1574 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1580 ifp = m->m_pkthdr.rcvif;
1582 ether_input_oncpu(ifp, m);
1585 static __inline void
1586 ether_init_netpacket(int num, struct mbuf *m)
1588 struct netmsg_packet *pmsg;
1590 pmsg = &m->m_hdr.mh_netmsg;
1591 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1592 ether_input_handler);
1593 pmsg->nm_packet = m;
1594 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1597 static __inline struct lwkt_port *
1598 ether_mport(int num, struct mbuf **m0)
1600 struct lwkt_port *port;
1601 struct mbuf *m = *m0;
1603 if (num == NETISR_MAX) {
1605 * All packets whose target msgports can't be
1606 * determined here are dispatched to netisr0,
1607 * where further dispatching may happen.
1609 return cpu_portfn(0);
1612 port = netisr_find_port(num, &m);
1621 ether_input_chain2(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1623 struct ether_header *eh, *save_eh, save_eh0;
1624 struct lwkt_port *port;
1625 uint16_t ether_type;
1628 ASSERT_SERIALIZED(ifp->if_serializer);
1631 /* Discard packet if interface is not up */
1632 if (!(ifp->if_flags & IFF_UP)) {
1637 if (m->m_len < sizeof(struct ether_header)) {
1638 /* XXX error in the caller. */
1642 eh = mtod(m, struct ether_header *);
1644 m->m_pkthdr.rcvif = ifp;
1646 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1647 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1648 ifp->if_addrlen) == 0)
1649 m->m_flags |= M_BCAST;
1651 m->m_flags |= M_MCAST;
1655 ETHER_BPF_MTAP(ifp, m);
1657 ifp->if_ibytes += m->m_pkthdr.len;
1659 if (ifp->if_flags & IFF_MONITOR) {
1661 * Interface marked for monitoring; discard packet.
1667 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1668 (m->m_flags & M_VLANTAG) == 0) {
1670 * Extract vlan tag if hardware does not do it for us
1672 vlan_ether_decap(&m);
1675 eh = mtod(m, struct ether_header *);
1677 ether_type = ntohs(eh->ether_type);
1679 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1681 * To prevent possible dangerous recursion,
1682 * we don't do vlan-in-vlan
1688 KKASSERT(ether_type != ETHERTYPE_VLAN);
1691 * Map ether type to netisr id.
1693 switch (ether_type) {
1705 case ETHERTYPE_IPV6:
1717 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1724 isr = NETISR_ATALK1;
1726 case ETHERTYPE_AARP:
1733 * NETISR_MAX is an invalid value; it is chosen to let
1734 * ether_mport() know that we are not able to decide
1735 * this packet's msgport here.
1742 * If the packet is in contiguous memory, following
1743 * m_adj() could ensure that the hidden ether header
1744 * will not be destroyed, else we will have to save
1745 * the ether header for the later restoration.
1747 if (m->m_pkthdr.len != m->m_len) {
1749 save_eh = &save_eh0;
1755 * Temporarily remove ether header; ether_mport()
1756 * expects a packet without ether header.
1758 m_adj(m, sizeof(struct ether_header));
1761 * Find the packet's target msgport.
1763 port = ether_mport(isr, &m);
1765 KKASSERT(m == NULL);
1770 * Restore ether header.
1772 if (save_eh != NULL) {
1773 ether_restore_header(&m, eh, save_eh);
1777 m->m_data -= ETHER_HDR_LEN;
1778 m->m_len += ETHER_HDR_LEN;
1779 m->m_pkthdr.len += ETHER_HDR_LEN;
1783 * Initialize mbuf's netmsg packet _after_ possible
1784 * ether header restoration, else the initialized
1785 * netmsg packet may be lost during ether header
1788 ether_init_netpacket(isr, m);
1790 #ifdef ETHER_INPUT_CHAIN
1791 if (chain != NULL) {
1792 struct mbuf_chain *c;
1795 m->m_pkthdr.header = port; /* XXX */
1796 cpuid = port->mpu_td->td_gd->gd_cpuid;
1799 if (c->mc_head == NULL) {
1800 c->mc_head = c->mc_tail = m;
1802 c->mc_tail->m_nextpkt = m;
1805 m->m_nextpkt = NULL;
1807 #endif /* ETHER_INPUT_CHAIN */
1808 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1811 #endif /* ETHER_INPUT2 */