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.62 2008/05/28 12:11:13 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>
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <net/ipfw/ip_fw.h>
75 #include <net/dummynet/ip_dummynet.h>
78 #include <netinet6/nd6.h>
82 #include <netinet/ip_carp.h>
86 #include <netproto/ipx/ipx.h>
87 #include <netproto/ipx/ipx_if.h>
88 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
89 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
90 short *tp, int *hlen);
95 #include <netns/ns_if.h>
97 int ether_outputdebug = 0;
98 int ether_inputdebug = 0;
102 #include <netproto/atalk/at.h>
103 #include <netproto/atalk/at_var.h>
104 #include <netproto/atalk/at_extern.h>
106 #define llc_snap_org_code llc_un.type_snap.org_code
107 #define llc_snap_ether_type llc_un.type_snap.ether_type
109 extern u_char at_org_code[3];
110 extern u_char aarp_org_code[3];
111 #endif /* NETATALK */
113 /* netgraph node hooks for ng_ether(4) */
114 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
115 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
116 struct mbuf *m, const struct ether_header *eh);
117 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
118 void (*ng_ether_attach_p)(struct ifnet *ifp);
119 void (*ng_ether_detach_p)(struct ifnet *ifp);
121 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *);
123 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
125 static void ether_restore_header(struct mbuf **, const struct ether_header *,
126 const struct ether_header *);
127 static void ether_demux_chain(struct ifnet *, struct mbuf *,
128 struct mbuf_chain *);
133 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
134 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
135 struct sockaddr *, struct rtentry *);
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 lwkt_serialize_enter(ifp->if_serializer);
332 error = bridge_output_p(ifp, m, NULL, NULL);
333 lwkt_serialize_exit(ifp->if_serializer);
338 * If a simplex interface, and the packet is being sent to our
339 * Ethernet address or a broadcast address, loopback a copy.
340 * XXX To make a simplex device behave exactly like a duplex
341 * device, we should copy in the case of sending to our own
342 * ethernet address (thus letting the original actually appear
343 * on the wire). However, we don't do that here for security
344 * reasons and compatibility with the original behavior.
346 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
349 if (m->m_pkthdr.csum_flags & CSUM_IP)
350 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
351 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
352 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
353 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
356 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
357 n->m_pkthdr.csum_flags |= csum_flags;
358 if (csum_flags & CSUM_DATA_VALID)
359 n->m_pkthdr.csum_data = 0xffff;
360 if_simloop(ifp, n, dst->sa_family, hlen);
363 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
364 ETHER_ADDR_LEN) == 0) {
365 m->m_pkthdr.csum_flags |= csum_flags;
366 if (csum_flags & CSUM_DATA_VALID)
367 m->m_pkthdr.csum_data = 0xffff;
368 if_simloop(ifp, m, dst->sa_family, hlen);
369 return (0); /* XXX */
374 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
379 /* Handle ng_ether(4) processing, if any */
380 if (ng_ether_output_p != NULL) {
381 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
387 /* Continue with link-layer output */
388 return ether_output_frame(ifp, m);
396 * Ethernet link layer output routine to send a raw frame to the device.
398 * This assumes that the 14 byte Ethernet header is present and contiguous
402 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
404 struct ip_fw *rule = NULL;
406 struct altq_pktattr pktattr;
409 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
411 /* Extract info from dummynet tag */
412 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
414 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
416 m_tag_delete(m, mtag);
420 if (ifq_is_enabled(&ifp->if_snd))
421 altq_etherclassify(&ifp->if_snd, m, &pktattr);
423 if (IPFW_LOADED && ether_ipfw != 0) {
424 struct ether_header save_eh, *eh;
426 eh = mtod(m, struct ether_header *);
428 m_adj(m, ETHER_HDR_LEN);
429 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
433 return ENOBUFS; /* pkt dropped */
435 return 0; /* consumed e.g. in a pipe */
438 /* packet was ok, restore the ethernet header */
439 ether_restore_header(&m, eh, &save_eh);
448 * Queue message on interface, update output statistics if
449 * successful, and start output if interface not yet active.
451 error = ifq_dispatch(ifp, m, &pktattr);
456 * ipfw processing for ethernet packets (in and out).
457 * The second parameter is NULL from ether_demux(), and ifp from
458 * ether_output_frame().
461 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
462 const struct ether_header *eh)
464 struct ether_header save_eh = *eh; /* might be a ptr in m */
465 struct ip_fw_args args;
469 if (*rule != NULL && fw_one_pass)
470 return TRUE; /* dummynet packet, already partially processed */
473 * I need some amount of data to be contiguous.
475 i = min((*m0)->m_pkthdr.len, max_protohdr);
476 if ((*m0)->m_len < i) {
477 *m0 = m_pullup(*m0, i);
482 args.m = *m0; /* the packet we are looking at */
483 args.oif = dst; /* destination, if any */
484 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
485 m_tag_delete(*m0, mtag);
486 args.rule = *rule; /* matching rule to restart */
487 args.next_hop = NULL; /* we do not support forward yet */
488 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
489 i = ip_fw_chk_ptr(&args);
493 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
496 if (i == 0) /* a PASS rule. */
499 if (i & IP_FW_PORT_DYNT_FLAG) {
501 * Pass the pkt to dummynet, which consumes it.
505 m = *m0; /* pass the original to dummynet */
506 *m0 = NULL; /* and nothing back to the caller */
508 ether_restore_header(&m, eh, &save_eh);
512 ip_fw_dn_io_ptr(m, (i & 0xffff),
513 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
517 * XXX at some point add support for divert/forward actions.
518 * If none of the above matches, we have to drop the pkt.
524 * Process a received Ethernet packet.
526 * The ethernet header is assumed to be in the mbuf so the caller
527 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
528 * bytes in the first mbuf.
530 * This allows us to concentrate in one place a bunch of code which
531 * is replicated in all device drivers. Also, many functions called
532 * from ether_input() try to put the eh back into the mbuf, so we
533 * can later propagate the 'contiguous packet' interface to them.
535 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
536 * cluster, right before m_data. So be very careful when working on m,
537 * as you could destroy *eh !!
539 * First we perform any link layer operations, then continue to the
540 * upper layers with ether_demux().
543 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
545 struct ether_header *eh;
547 ASSERT_SERIALIZED(ifp->if_serializer);
550 /* Discard packet if interface is not up */
551 if (!(ifp->if_flags & IFF_UP)) {
556 if (m->m_len < sizeof(struct ether_header)) {
557 /* XXX error in the caller. */
561 eh = mtod(m, struct ether_header *);
563 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
564 (m->m_flags & M_VLANTAG) == 0) {
566 * Extract vlan tag if hardware does not do it for us
568 vlan_ether_decap(&m);
571 eh = mtod(m, struct ether_header *);
574 m->m_pkthdr.rcvif = ifp;
576 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
577 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
578 ifp->if_addrlen) == 0)
579 m->m_flags |= M_BCAST;
581 m->m_flags |= M_MCAST;
585 ETHER_BPF_MTAP(ifp, m);
587 ifp->if_ibytes += m->m_pkthdr.len;
589 if (ifp->if_flags & IFF_MONITOR) {
591 * Interface marked for monitoring; discard packet.
598 * Tap the packet off here for a bridge. bridge_input()
599 * will return NULL if it has consumed the packet, otherwise
600 * it gets processed as normal. Note that bridge_input()
601 * will always return the original packet if we need to
602 * process it locally.
604 if (ifp->if_bridge) {
605 KASSERT(bridge_input_p != NULL,
606 ("%s: if_bridge not loaded!", __func__));
608 if(m->m_flags & M_PROTO1) {
609 m->m_flags &= ~M_PROTO1;
611 /* clear M_PROMISC, in case the packets comes from a vlan */
612 /* m->m_flags &= ~M_PROMISC; */
613 lwkt_serialize_exit(ifp->if_serializer);
614 m = (*bridge_input_p)(ifp, m);
615 lwkt_serialize_enter(ifp->if_serializer);
619 KASSERT(ifp == m->m_pkthdr.rcvif,
620 ("bridge_input_p changed rcvif\n"));
622 /* 'm' may be changed by bridge_input_p() */
623 eh = mtod(m, struct ether_header *);
627 /* Handle ng_ether(4) processing, if any */
628 if (ng_ether_input_p != NULL) {
629 ng_ether_input_p(ifp, &m);
633 /* 'm' may be changed by ng_ether_input_p() */
634 eh = mtod(m, struct ether_header *);
637 /* Continue with upper layer processing */
638 ether_demux_chain(ifp, m, chain);
642 ether_input(struct ifnet *ifp, struct mbuf *m)
644 ether_input_chain(ifp, m, NULL);
648 * Upper layer processing for a received Ethernet packet.
651 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
653 struct ether_header save_eh, *eh;
656 struct ip_fw *rule = NULL;
663 KASSERT(m->m_len >= ETHER_HDR_LEN,
664 ("ether header is no contiguous!\n"));
666 eh = mtod(m, struct ether_header *);
669 /* XXX old crufty stuff, needs to be removed */
670 m_adj(m, sizeof(struct ether_header));
672 /* Extract info from dummynet tag */
673 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
675 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
676 KKASSERT(ifp == NULL);
677 ifp = m->m_pkthdr.rcvif;
679 m_tag_delete(m, mtag);
682 if (rule) /* packet is passing the second time */
687 * XXX: Okay, we need to call carp_forus() and - if it is for
688 * us jump over code that does the normal check
689 * "ac_enaddr == ether_dhost". The check sequence is a bit
690 * different from OpenBSD, so we jump over as few code as
691 * possible, to catch _all_ sanity checks. This needs
692 * evaluation, to see if the carp ether_dhost values break any
695 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
700 * Discard packet if upper layers shouldn't see it because
701 * it was unicast to a different Ethernet address. If the
702 * driver is working properly, then this situation can only
703 * happen when the interface is in promiscuous mode.
705 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
706 (eh->ether_dhost[0] & 1) == 0 &&
707 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
713 if (IPFW_LOADED && ether_ipfw != 0) {
714 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
720 ether_type = ntohs(save_eh.ether_type);
722 if (m->m_flags & M_VLANTAG) {
723 if (ether_type == ETHERTYPE_VLAN) {
725 * To prevent possible dangerous recursion,
726 * we don't do vlan-in-vlan
728 m->m_pkthdr.rcvif->if_noproto++;
732 if (vlan_input_p != NULL) {
733 ether_restore_header(&m, eh, &save_eh);
735 vlan_input_p(m, chain);
737 m->m_pkthdr.rcvif->if_noproto++;
742 KKASSERT(ether_type != ETHERTYPE_VLAN);
744 switch (ether_type) {
747 if (ipflow_fastforward(m, ifp->if_serializer))
753 if (ifp->if_flags & IFF_NOARP) {
754 /* Discard packet if ARP is disabled on interface */
770 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
777 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
794 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
798 checksum = mtod(m, ushort *);
800 if ((ether_type <= ETHERMTU) &&
801 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
802 if (*checksum == 0xE0E0) {
803 m->m_pkthdr.len -= 3;
812 if (ether_type > ETHERMTU)
814 l = mtod(m, struct llc *);
815 if (l->llc_dsap == LLC_SNAP_LSAP &&
816 l->llc_ssap == LLC_SNAP_LSAP &&
817 l->llc_control == LLC_UI) {
818 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
819 sizeof at_org_code) == 0 &&
820 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
821 m_adj(m, sizeof(struct llc));
825 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
826 sizeof aarp_org_code) == 0 &&
827 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
828 m_adj(m, sizeof(struct llc));
835 if (ng_ether_input_orphan_p != NULL)
836 (*ng_ether_input_orphan_p)(ifp, m, &save_eh);
842 #ifdef ETHER_INPUT_CHAIN
844 struct mbuf_chain *c;
848 port = netisr_mport(isr, &m);
852 m->m_pkthdr.header = port; /* XXX */
853 cpuid = port->mpu_td->td_gd->gd_cpuid;
856 if (c->mc_head == NULL) {
857 c->mc_head = c->mc_tail = m;
859 c->mc_tail->m_nextpkt = m;
864 #endif /* ETHER_INPUT_CHAIN */
865 netisr_dispatch(isr, m);
869 ether_demux(struct ifnet *ifp, struct mbuf *m)
871 ether_demux_chain(ifp, m, NULL);
875 * Perform common duties while attaching to interface list
879 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
881 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
886 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
887 lwkt_serialize_t serializer)
889 struct sockaddr_dl *sdl;
891 ifp->if_type = IFT_ETHER;
892 ifp->if_addrlen = ETHER_ADDR_LEN;
893 ifp->if_hdrlen = ETHER_HDR_LEN;
894 if_attach(ifp, serializer);
895 ifp->if_mtu = ETHERMTU;
896 if (ifp->if_baudrate == 0)
897 ifp->if_baudrate = 10000000;
898 ifp->if_output = ether_output;
899 ifp->if_input = ether_input;
900 ifp->if_resolvemulti = ether_resolvemulti;
901 ifp->if_broadcastaddr = etherbroadcastaddr;
902 sdl = IF_LLSOCKADDR(ifp);
903 sdl->sdl_type = IFT_ETHER;
904 sdl->sdl_alen = ifp->if_addrlen;
905 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
907 * XXX Keep the current drivers happy.
908 * XXX Remove once all drivers have been cleaned up
910 if (lla != IFP2AC(ifp)->ac_enaddr)
911 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
912 bpfattach(ifp, dlt, hdrlen);
913 if (ng_ether_attach_p != NULL)
914 (*ng_ether_attach_p)(ifp);
916 if_printf(ifp, "MAC address: %6D\n", lla, ":");
920 * Perform common duties while detaching an Ethernet interface
923 ether_ifdetach(struct ifnet *ifp)
927 if (ng_ether_detach_p != NULL)
928 (*ng_ether_detach_p)(ifp);
934 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
936 struct ifaddr *ifa = (struct ifaddr *) data;
937 struct ifreq *ifr = (struct ifreq *) data;
940 #define IF_INIT(ifp) \
942 if (((ifp)->if_flags & IFF_UP) == 0) { \
943 (ifp)->if_flags |= IFF_UP; \
944 (ifp)->if_init((ifp)->if_softc); \
948 ASSERT_SERIALIZED(ifp->if_serializer);
952 switch (ifa->ifa_addr->sa_family) {
955 IF_INIT(ifp); /* before arpwhohas */
956 arp_ifinit(ifp, ifa);
961 * XXX - This code is probably wrong
965 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
966 struct arpcom *ac = IFP2AC(ifp);
968 if (ipx_nullhost(*ina))
969 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
971 bcopy(ina->x_host.c_host, ac->ac_enaddr,
972 sizeof ac->ac_enaddr);
974 IF_INIT(ifp); /* Set new address. */
980 * XXX - This code is probably wrong
984 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
985 struct arpcom *ac = IFP2AC(ifp);
987 if (ns_nullhost(*ina))
988 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
990 bcopy(ina->x_host.c_host, ac->ac_enaddr,
991 sizeof ac->ac_enaddr);
1007 bcopy(IFP2AC(ifp)->ac_enaddr,
1008 ((struct sockaddr *)ifr->ifr_data)->sa_data,
1014 * Set the interface MTU.
1016 if (ifr->ifr_mtu > ETHERMTU) {
1019 ifp->if_mtu = ifr->ifr_mtu;
1034 struct sockaddr **llsa,
1035 struct sockaddr *sa)
1037 struct sockaddr_dl *sdl;
1038 struct sockaddr_in *sin;
1040 struct sockaddr_in6 *sin6;
1044 switch(sa->sa_family) {
1047 * No mapping needed. Just check that it's a valid MC address.
1049 sdl = (struct sockaddr_dl *)sa;
1050 e_addr = LLADDR(sdl);
1051 if ((e_addr[0] & 1) != 1)
1052 return EADDRNOTAVAIL;
1058 sin = (struct sockaddr_in *)sa;
1059 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1060 return EADDRNOTAVAIL;
1061 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1063 sdl->sdl_len = sizeof *sdl;
1064 sdl->sdl_family = AF_LINK;
1065 sdl->sdl_index = ifp->if_index;
1066 sdl->sdl_type = IFT_ETHER;
1067 sdl->sdl_alen = ETHER_ADDR_LEN;
1068 e_addr = LLADDR(sdl);
1069 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1070 *llsa = (struct sockaddr *)sdl;
1075 sin6 = (struct sockaddr_in6 *)sa;
1076 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1078 * An IP6 address of 0 means listen to all
1079 * of the Ethernet multicast address used for IP6.
1080 * (This is used for multicast routers.)
1082 ifp->if_flags |= IFF_ALLMULTI;
1086 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1087 return EADDRNOTAVAIL;
1088 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1090 sdl->sdl_len = sizeof *sdl;
1091 sdl->sdl_family = AF_LINK;
1092 sdl->sdl_index = ifp->if_index;
1093 sdl->sdl_type = IFT_ETHER;
1094 sdl->sdl_alen = ETHER_ADDR_LEN;
1095 e_addr = LLADDR(sdl);
1096 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1097 *llsa = (struct sockaddr *)sdl;
1103 * Well, the text isn't quite right, but it's the name
1106 return EAFNOSUPPORT;
1112 * This is for reference. We have a table-driven version
1113 * of the little-endian crc32 generator, which is faster
1114 * than the double-loop.
1117 ether_crc32_le(const uint8_t *buf, size_t len)
1119 uint32_t c, crc, carry;
1122 crc = 0xffffffffU; /* initial value */
1124 for (i = 0; i < len; i++) {
1126 for (j = 0; j < 8; j++) {
1127 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1131 crc = (crc ^ ETHER_CRC_POLY_LE);
1139 ether_crc32_le(const uint8_t *buf, size_t len)
1141 static const uint32_t crctab[] = {
1142 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1143 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1144 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1145 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1150 crc = 0xffffffffU; /* initial value */
1152 for (i = 0; i < len; i++) {
1154 crc = (crc >> 4) ^ crctab[crc & 0xf];
1155 crc = (crc >> 4) ^ crctab[crc & 0xf];
1163 ether_crc32_be(const uint8_t *buf, size_t len)
1165 uint32_t c, crc, carry;
1168 crc = 0xffffffffU; /* initial value */
1170 for (i = 0; i < len; i++) {
1172 for (j = 0; j < 8; j++) {
1173 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1177 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1185 * find the size of ethernet header, and call classifier
1188 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1189 struct altq_pktattr *pktattr)
1191 struct ether_header *eh;
1192 uint16_t ether_type;
1193 int hlen, af, hdrsize;
1196 hlen = sizeof(struct ether_header);
1197 eh = mtod(m, struct ether_header *);
1199 ether_type = ntohs(eh->ether_type);
1200 if (ether_type < ETHERMTU) {
1202 struct llc *llc = (struct llc *)(eh + 1);
1205 if (m->m_len < hlen ||
1206 llc->llc_dsap != LLC_SNAP_LSAP ||
1207 llc->llc_ssap != LLC_SNAP_LSAP ||
1208 llc->llc_control != LLC_UI)
1209 goto bad; /* not snap! */
1211 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1214 if (ether_type == ETHERTYPE_IP) {
1216 hdrsize = 20; /* sizeof(struct ip) */
1218 } else if (ether_type == ETHERTYPE_IPV6) {
1220 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1225 while (m->m_len <= hlen) {
1229 hdr = m->m_data + hlen;
1230 if (m->m_len < hlen + hdrsize) {
1232 * ip header is not in a single mbuf. this should not
1233 * happen in the current code.
1234 * (todo: use m_pulldown in the future)
1240 ifq_classify(ifq, m, af, pktattr);
1247 pktattr->pattr_class = NULL;
1248 pktattr->pattr_hdr = NULL;
1249 pktattr->pattr_af = AF_UNSPEC;
1253 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1254 const struct ether_header *save_eh)
1256 struct mbuf *m = *m0;
1258 ether_restore_hdr++;
1261 * Prepend the header, optimize for the common case of
1262 * eh pointing into the mbuf.
1264 if ((const void *)(eh + 1) == (void *)m->m_data) {
1265 m->m_data -= ETHER_HDR_LEN;
1266 m->m_len += ETHER_HDR_LEN;
1267 m->m_pkthdr.len += ETHER_HDR_LEN;
1269 ether_prepend_hdr++;
1271 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1273 bcopy(save_eh, mtod(m, struct ether_header *),
1280 #ifdef ETHER_INPUT_CHAIN
1283 ether_input_ipifunc(void *arg)
1285 struct mbuf *m, *next;
1290 next = m->m_nextpkt;
1291 m->m_nextpkt = NULL;
1293 port = m->m_pkthdr.header;
1294 m->m_pkthdr.header = NULL;
1297 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1300 } while (m != NULL);
1304 ether_input_dispatch(struct mbuf_chain *chain)
1309 for (i = 0; i < ncpus; ++i) {
1310 if (chain[i].mc_head != NULL) {
1311 lwkt_send_ipiq(globaldata_find(i),
1312 ether_input_ipifunc, chain[i].mc_head);
1316 ether_input_ipifunc(chain->mc_head);
1320 #endif /* ETHER_INPUT_CHAIN */