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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
33 #include "opt_atalk.h"
35 #include "opt_inet6.h"
38 #include "opt_netgraph.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
47 #include <sys/random.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
53 #include <net/if_arp.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
60 #include <net/ethernet.h>
61 #include <net/if_bridgevar.h>
62 #include <net/if_vlan_var.h>
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip_fw.h>
69 #include <netinet/ip_dummynet.h>
72 #include <netinet6/nd6.h>
76 #include <netinet/ip_carp.h>
80 #include <netipx/ipx.h>
81 #include <netipx/ipx_if.h>
83 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
84 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
85 struct sockaddr *dst, short *tp, int *hlen);
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
92 #define llc_snap_org_code llc_un.type_snap.org_code
93 #define llc_snap_ether_type llc_un.type_snap.ether_type
95 extern u_char at_org_code[3];
96 extern u_char aarp_org_code[3];
99 #include <security/mac/mac_framework.h>
101 /* netgraph node hooks for ng_ether(4) */
102 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105 void (*ng_ether_attach_p)(struct ifnet *ifp);
106 void (*ng_ether_detach_p)(struct ifnet *ifp);
108 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
110 /* if_bridge(4) support */
111 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
112 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
113 struct sockaddr *, struct rtentry *);
114 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
116 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
117 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
119 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
122 /* XXX: should be in an arp support file, not here */
123 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
125 #define senderr(e) do { error = (e); goto bad;} while (0)
127 #if defined(INET) || defined(INET6)
129 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
130 struct ip_fw **rule, int shared);
131 static int ether_ipfw;
135 * Ethernet output routine.
136 * Encapsulate a packet of type family for the local net.
137 * Use trailer local net encapsulation if enough data in first
138 * packet leaves a multiple of 512 bytes of data in remainder.
141 ether_output(struct ifnet *ifp, struct mbuf *m,
142 struct sockaddr *dst, struct rtentry *rt0)
145 int error, hdrcmplt = 0;
146 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
147 struct ether_header *eh;
149 int hlen; /* link layer header length */
152 error = mac_check_ifnet_transmit(ifp, m);
157 if (ifp->if_flags & IFF_MONITOR)
159 if (!((ifp->if_flags & IFF_UP) &&
160 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
163 hlen = ETHER_HDR_LEN;
164 switch (dst->sa_family) {
167 error = arpresolve(ifp, rt0, m, dst, edst);
169 return (error == EWOULDBLOCK ? 0 : error);
170 type = htons(ETHERTYPE_IP);
175 ah = mtod(m, struct arphdr *);
176 ah->ar_hrd = htons(ARPHRD_ETHER);
178 loop_copy = 0; /* if this is for us, don't do it */
180 switch(ntohs(ah->ar_op)) {
181 case ARPOP_REVREQUEST:
183 type = htons(ETHERTYPE_REVARP);
188 type = htons(ETHERTYPE_ARP);
192 if (m->m_flags & M_BCAST)
193 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
195 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
202 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
205 type = htons(ETHERTYPE_IPV6);
211 error = ef_outputp(ifp, &m, dst, &type, &hlen);
215 type = htons(ETHERTYPE_IPX);
216 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
217 (caddr_t)edst, sizeof (edst));
223 struct at_ifaddr *aa;
225 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
226 senderr(EHOSTUNREACH); /* XXX */
227 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
230 * In the phase 2 case, need to prepend an mbuf for the llc header.
232 if ( aa->aa_flags & AFA_PHASE2 ) {
235 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
238 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
239 llc.llc_control = LLC_UI;
240 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
241 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
242 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
243 type = htons(m->m_pkthdr.len);
244 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
246 type = htons(ETHERTYPE_AT);
250 #endif /* NETATALK */
252 case pseudo_AF_HDRCMPLT:
254 eh = (struct ether_header *)dst->sa_data;
255 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259 loop_copy = 0; /* if this is for us, don't do it */
260 eh = (struct ether_header *)dst->sa_data;
261 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
262 type = eh->ether_type;
266 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
267 senderr(EAFNOSUPPORT);
271 * Add local net header. If no space in first mbuf,
274 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
277 eh = mtod(m, struct ether_header *);
278 (void)memcpy(&eh->ether_type, &type,
279 sizeof(eh->ether_type));
280 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
282 (void)memcpy(eh->ether_shost, esrc,
283 sizeof(eh->ether_shost));
285 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
286 sizeof(eh->ether_shost));
289 * If a simplex interface, and the packet is being sent to our
290 * Ethernet address or a broadcast address, loopback a copy.
291 * XXX To make a simplex device behave exactly like a duplex
292 * device, we should copy in the case of sending to our own
293 * ethernet address (thus letting the original actually appear
294 * on the wire). However, we don't do that here for security
295 * reasons and compatibility with the original behavior.
297 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
298 m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
301 if (m->m_pkthdr.csum_flags & CSUM_IP)
302 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
303 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
304 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
306 if (m->m_flags & M_BCAST) {
310 * Because if_simloop() modifies the packet, we need a
311 * writable copy through m_dup() instead of a readonly
312 * one as m_copy[m] would give us. The alternative would
313 * be to modify if_simloop() to handle the readonly mbuf,
314 * but performancewise it is mostly equivalent (trading
315 * extra data copying vs. extra locking).
317 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
318 n->m_pkthdr.csum_flags |= csum_flags;
319 if (csum_flags & CSUM_DATA_VALID)
320 n->m_pkthdr.csum_data = 0xffff;
321 (void)if_simloop(ifp, n, dst->sa_family, hlen);
324 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
325 ETHER_ADDR_LEN) == 0) {
326 m->m_pkthdr.csum_flags |= csum_flags;
327 if (csum_flags & CSUM_DATA_VALID)
328 m->m_pkthdr.csum_data = 0xffff;
329 (void) if_simloop(ifp, m, dst->sa_family, hlen);
330 return (0); /* XXX */
335 * Bridges require special output handling.
337 if (ifp->if_bridge) {
338 BRIDGE_OUTPUT(ifp, m, error);
344 (error = carp_output(ifp, m, dst, NULL)))
348 /* Handle ng_ether(4) processing, if any */
349 if (IFP2AC(ifp)->ac_netgraph != NULL) {
350 KASSERT(ng_ether_output_p != NULL,
351 ("ng_ether_output_p is NULL"));
352 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
361 /* Continue with link-layer output */
362 return ether_output_frame(ifp, m);
366 * Ethernet link layer output routine to send a raw frame to the device.
368 * This assumes that the 14 byte Ethernet header is present and contiguous
369 * in the first mbuf (if BRIDGE'ing).
372 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
375 #if defined(INET) || defined(INET6)
376 struct ip_fw *rule = ip_dn_claim_rule(m);
378 if (IPFW_LOADED && ether_ipfw != 0) {
379 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
382 return EACCES; /* pkt dropped */
384 return 0; /* consumed e.g. in a pipe */
390 * Queue message on interface, update output statistics if
391 * successful, and start output if interface not yet active.
393 IFQ_HANDOFF(ifp, m, error);
397 #if defined(INET) || defined(INET6)
399 * ipfw processing for ethernet packets (in and out).
400 * The second parameter is NULL from ether_demux, and ifp from
401 * ether_output_frame.
404 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
405 struct ip_fw **rule, int shared)
407 struct ether_header *eh;
408 struct ether_header save_eh;
411 struct ip_fw_args args;
413 if (*rule != NULL && fw_one_pass)
414 return 1; /* dummynet packet, already partially processed */
417 * I need some amt of data to be contiguous, and in case others need
418 * the packet (shared==1) also better be in the first mbuf.
421 i = min( m->m_pkthdr.len, max_protohdr);
422 if ( shared || m->m_len < i) {
429 eh = mtod(m, struct ether_header *);
430 save_eh = *eh; /* save copy for restore below */
431 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
433 args.m = m; /* the packet we are looking at */
434 args.oif = dst; /* destination, if any */
435 args.rule = *rule; /* matching rule to restart */
436 args.next_hop = NULL; /* we do not support forward yet */
437 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
438 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
439 i = ip_fw_chk_ptr(&args);
443 * Restore Ethernet header, as needed, in case the
444 * mbuf chain was replaced by ipfw.
446 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
451 if (eh != mtod(m, struct ether_header *))
452 bcopy(&save_eh, mtod(m, struct ether_header *),
458 if (i == IP_FW_DENY) /* drop */
461 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
463 if (i == IP_FW_PASS) /* a PASS rule. */
466 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
468 * Pass the pkt to dummynet, which consumes it.
469 * If shared, make a copy and keep the original.
472 m = m_copypacket(m, M_DONTWAIT);
477 * Pass the original to dummynet and
478 * nothing back to the caller
482 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
486 * XXX at some point add support for divert/forward actions.
487 * If none of the above matches, we have to drop the pkt.
494 * Process a received Ethernet packet; the packet is in the
495 * mbuf chain m with the ethernet header at the front.
498 ether_input(struct ifnet *ifp, struct mbuf *m)
500 struct ether_header *eh;
504 * Do consistency checks to verify assumptions
505 * made by code past this point.
507 if ((m->m_flags & M_PKTHDR) == 0) {
508 if_printf(ifp, "discard frame w/o packet header\n");
513 if (m->m_len < ETHER_HDR_LEN) {
514 /* XXX maybe should pullup? */
515 if_printf(ifp, "discard frame w/o leading ethernet "
516 "header (len %u pkt len %u)\n",
517 m->m_len, m->m_pkthdr.len);
522 eh = mtod(m, struct ether_header *);
523 etype = ntohs(eh->ether_type);
524 if (m->m_pkthdr.len >
525 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
526 if_printf(ifp, "discard oversize frame "
527 "(ether type %x flags %x len %u > max %lu)\n",
528 etype, m->m_flags, m->m_pkthdr.len,
529 ETHER_MAX_FRAME(ifp, etype,
530 m->m_flags & M_HASFCS));
535 if (m->m_pkthdr.rcvif == NULL) {
536 if_printf(ifp, "discard frame w/o interface pointer\n");
542 if (m->m_pkthdr.rcvif != ifp) {
543 if_printf(ifp, "Warning, frame marked as received on %s\n",
544 m->m_pkthdr.rcvif->if_xname);
550 * Tag the mbuf with an appropriate MAC label before any other
551 * consumers can get to it.
553 mac_create_mbuf_from_ifnet(ifp, m);
557 * Give bpf a chance at the packet.
561 /* If the CRC is still on the packet, trim it off. */
562 if (m->m_flags & M_HASFCS) {
563 m_adj(m, -ETHER_CRC_LEN);
564 m->m_flags &= ~M_HASFCS;
567 ifp->if_ibytes += m->m_pkthdr.len;
569 if (ifp->if_flags & IFF_MONITOR) {
571 * Interface marked for monitoring; discard packet.
577 /* Handle ng_ether(4) processing, if any */
578 if (IFP2AC(ifp)->ac_netgraph != NULL) {
579 KASSERT(ng_ether_input_p != NULL,
580 ("ng_ether_input_p is NULL"));
581 (*ng_ether_input_p)(ifp, &m);
587 * Tap the packet off here for a bridge. bridge_input()
588 * will return NULL if it has consumed the packet, otherwise
589 * it gets processed as normal. Note that bridge_input()
590 * will always return the original packet if we need to
591 * process it locally.
593 * XXX: When changing the below block, please also look
594 * at the src/sys/netgraph/ng_ether.c:ng_ether_rcv_upper()
596 if (ifp->if_bridge) {
597 BRIDGE_INPUT(ifp, m);
602 /* First chunk of an mbuf contains good entropy */
603 if (harvest.ethernet)
604 random_harvest(m, 16, 3, 0, RANDOM_NET);
609 * Upper layer processing for a received Ethernet packet.
612 ether_demux(struct ifnet *ifp, struct mbuf *m)
614 struct ether_header *eh;
617 #if defined(NETATALK)
620 #if defined(INET) || defined(INET6)
621 struct ip_fw *rule = ip_dn_claim_rule(m);
624 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
626 eh = mtod(m, struct ether_header *);
627 ether_type = ntohs(eh->ether_type);
629 #if defined(INET) || defined(INET6)
630 if (rule) /* packet was already bridged */
634 if (!(ifp->if_bridge) &&
635 !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
636 ifp->if_vlantrunk != NULL)) {
639 * XXX: Okay, we need to call carp_forus() and - if it is for
640 * us jump over code that does the normal check
641 * "IF_LLADDR(ifp) == ether_dhost". The check sequence is a bit
642 * different from OpenBSD, so we jump over as few code as
643 * possible, to catch _all_ sanity checks. This needs
644 * evaluation, to see if the carp ether_dhost values break any
647 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
651 * Discard packet if upper layers shouldn't see it because it
652 * was unicast to a different Ethernet address. If the driver
653 * is working properly, then this situation can only happen
654 * when the interface is in promiscuous mode.
656 * If VLANs are active, and this packet has a VLAN tag, do
657 * not drop it here but pass it on to the VLAN layer, to
658 * give them a chance to consider it as well (e. g. in case
659 * bridging is only active on a VLAN). They will drop it if
662 if ((ifp->if_flags & IFF_PROMISC) != 0
663 && !ETHER_IS_MULTICAST(eh->ether_dhost)
664 && bcmp(eh->ether_dhost,
665 IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0
666 && (ifp->if_flags & IFF_PPROMISC) == 0) {
675 /* Discard packet if interface is not up */
676 if ((ifp->if_flags & IFF_UP) == 0) {
680 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
681 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
682 sizeof(etherbroadcastaddr)) == 0)
683 m->m_flags |= M_BCAST;
685 m->m_flags |= M_MCAST;
687 if (m->m_flags & (M_BCAST|M_MCAST))
690 #if defined(INET) || defined(INET6)
692 if (IPFW_LOADED && ether_ipfw != 0) {
693 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
702 * Check to see if the device performed the VLAN decapsulation and
703 * provided us with the tag.
705 if (m->m_flags & M_VLANTAG) {
707 * If no VLANs are configured, drop.
709 if (ifp->if_vlantrunk == NULL) {
715 * vlan_input() will either recursively call ether_input()
716 * or drop the packet.
718 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
719 (*vlan_input_p)(ifp, m);
724 * Handle protocols that expect to have the Ethernet header
725 * (and possibly FCS) intact.
727 switch (ether_type) {
729 if (ifp->if_vlantrunk != NULL) {
730 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
731 (*vlan_input_p)(ifp, m);
739 /* Strip off Ethernet header. */
740 m_adj(m, ETHER_HDR_LEN);
742 /* If the CRC is still on the packet, trim it off. */
743 if (m->m_flags & M_HASFCS) {
744 m_adj(m, -ETHER_CRC_LEN);
745 m->m_flags &= ~M_HASFCS;
748 /* Reset layer specific mbuf flags to avoid confusing upper layers. */
749 m->m_flags &= ~(M_PROTOFLAGS);
751 switch (ether_type) {
754 if ((m = ip_fastforward(m)) == NULL)
760 if (ifp->if_flags & IFF_NOARP) {
761 /* Discard packet if ARP is disabled on interface */
770 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
787 #endif /* NETATALK */
790 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
793 #if defined(NETATALK)
794 if (ether_type > ETHERMTU)
796 l = mtod(m, struct llc *);
797 if (l->llc_dsap == LLC_SNAP_LSAP &&
798 l->llc_ssap == LLC_SNAP_LSAP &&
799 l->llc_control == LLC_UI) {
800 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
801 sizeof(at_org_code)) == 0 &&
802 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
803 m_adj(m, LLC_SNAPFRAMELEN);
807 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
808 sizeof(aarp_org_code)) == 0 &&
809 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
810 m_adj(m, LLC_SNAPFRAMELEN);
815 #endif /* NETATALK */
818 netisr_dispatch(isr, m);
823 * Packet is to be discarded. If netgraph is present,
824 * hand the packet to it for last chance processing;
825 * otherwise dispose of it.
827 if (IFP2AC(ifp)->ac_netgraph != NULL) {
828 KASSERT(ng_ether_input_orphan_p != NULL,
829 ("ng_ether_input_orphan_p is NULL"));
831 * Put back the ethernet header so netgraph has a
832 * consistent view of inbound packets.
834 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
835 (*ng_ether_input_orphan_p)(ifp, m);
842 * Convert Ethernet address to printable (loggable) representation.
843 * This routine is for compatibility; it's better to just use
845 * printf("%6D", <pointer to address>, ":");
847 * since there's no static buffer involved.
850 ether_sprintf(const u_char *ap)
852 static char etherbuf[18];
853 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
858 * Perform common duties while attaching to interface list
861 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
865 struct sockaddr_dl *sdl;
867 ifp->if_addrlen = ETHER_ADDR_LEN;
868 ifp->if_hdrlen = ETHER_HDR_LEN;
870 ifp->if_mtu = ETHERMTU;
871 ifp->if_output = ether_output;
872 ifp->if_input = ether_input;
873 ifp->if_resolvemulti = ether_resolvemulti;
874 if (ifp->if_baudrate == 0)
875 ifp->if_baudrate = IF_Mbps(10); /* just a default */
876 ifp->if_broadcastaddr = etherbroadcastaddr;
879 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
880 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
881 sdl->sdl_type = IFT_ETHER;
882 sdl->sdl_alen = ifp->if_addrlen;
883 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
885 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
886 if (ng_ether_attach_p != NULL)
887 (*ng_ether_attach_p)(ifp);
889 /* Announce Ethernet MAC address if non-zero. */
890 for (i = 0; i < ifp->if_addrlen; i++)
893 if (i != ifp->if_addrlen)
894 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
895 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
896 if_printf(ifp, "if_start running deferred for Giant\n");
900 * Perform common duties while detaching an Ethernet interface
903 ether_ifdetach(struct ifnet *ifp)
905 if (IFP2AC(ifp)->ac_netgraph != NULL) {
906 KASSERT(ng_ether_detach_p != NULL,
907 ("ng_ether_detach_p is NULL"));
908 (*ng_ether_detach_p)(ifp);
915 SYSCTL_DECL(_net_link);
916 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
917 #if defined(INET) || defined(INET6)
918 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
919 ðer_ipfw,0,"Pass ether pkts through firewall");
924 * This is for reference. We have a table-driven version
925 * of the little-endian crc32 generator, which is faster
926 * than the double-loop.
929 ether_crc32_le(const uint8_t *buf, size_t len)
936 crc = 0xffffffff; /* initial value */
938 for (i = 0; i < len; i++) {
939 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
940 carry = (crc ^ data) & 1;
943 crc = (crc ^ ETHER_CRC_POLY_LE);
950 ether_crc32_le(const uint8_t *buf, size_t len)
952 static const uint32_t crctab[] = {
953 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
954 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
955 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
956 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
961 crc = 0xffffffff; /* initial value */
963 for (i = 0; i < len; i++) {
965 crc = (crc >> 4) ^ crctab[crc & 0xf];
966 crc = (crc >> 4) ^ crctab[crc & 0xf];
974 ether_crc32_be(const uint8_t *buf, size_t len)
981 crc = 0xffffffff; /* initial value */
983 for (i = 0; i < len; i++) {
984 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
985 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
988 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
996 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
998 struct ifaddr *ifa = (struct ifaddr *) data;
999 struct ifreq *ifr = (struct ifreq *) data;
1004 ifp->if_flags |= IFF_UP;
1006 switch (ifa->ifa_addr->sa_family) {
1009 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1010 arp_ifinit(ifp, ifa);
1015 * XXX - This code is probably wrong
1019 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1021 if (ipx_nullhost(*ina))
1026 bcopy((caddr_t) ina->x_host.c_host,
1027 (caddr_t) IF_LLADDR(ifp),
1034 ifp->if_init(ifp->if_softc);
1039 ifp->if_init(ifp->if_softc);
1046 struct sockaddr *sa;
1048 sa = (struct sockaddr *) & ifr->ifr_data;
1049 bcopy(IF_LLADDR(ifp),
1050 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1056 * Set the interface MTU.
1058 if (ifr->ifr_mtu > ETHERMTU) {
1061 ifp->if_mtu = ifr->ifr_mtu;
1065 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1072 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1073 struct sockaddr *sa)
1075 struct sockaddr_dl *sdl;
1077 struct sockaddr_in *sin;
1080 struct sockaddr_in6 *sin6;
1084 switch(sa->sa_family) {
1087 * No mapping needed. Just check that it's a valid MC address.
1089 sdl = (struct sockaddr_dl *)sa;
1090 e_addr = LLADDR(sdl);
1091 if (!ETHER_IS_MULTICAST(e_addr))
1092 return EADDRNOTAVAIL;
1098 sin = (struct sockaddr_in *)sa;
1099 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1100 return EADDRNOTAVAIL;
1101 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1105 sdl->sdl_len = sizeof *sdl;
1106 sdl->sdl_family = AF_LINK;
1107 sdl->sdl_index = ifp->if_index;
1108 sdl->sdl_type = IFT_ETHER;
1109 sdl->sdl_alen = ETHER_ADDR_LEN;
1110 e_addr = LLADDR(sdl);
1111 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1112 *llsa = (struct sockaddr *)sdl;
1117 sin6 = (struct sockaddr_in6 *)sa;
1118 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1120 * An IP6 address of 0 means listen to all
1121 * of the Ethernet multicast address used for IP6.
1122 * (This is used for multicast routers.)
1124 ifp->if_flags |= IFF_ALLMULTI;
1128 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1129 return EADDRNOTAVAIL;
1130 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1134 sdl->sdl_len = sizeof *sdl;
1135 sdl->sdl_family = AF_LINK;
1136 sdl->sdl_index = ifp->if_index;
1137 sdl->sdl_type = IFT_ETHER;
1138 sdl->sdl_alen = ETHER_ADDR_LEN;
1139 e_addr = LLADDR(sdl);
1140 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1141 *llsa = (struct sockaddr *)sdl;
1147 * Well, the text isn't quite right, but it's the name
1150 return EAFNOSUPPORT;
1155 ether_alloc(u_char type, struct ifnet *ifp)
1159 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1166 ether_free(void *com, u_char type)
1169 free(com, M_ARPCOM);
1173 ether_modevent(module_t mod, int type, void *data)
1178 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1181 if_deregister_com_alloc(IFT_ETHER);
1190 static moduledata_t ether_mod = {
1197 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1199 struct ether_vlan_header vlan;
1202 KASSERT((m->m_flags & M_VLANTAG) != 0,
1203 ("%s: vlan information not present", __func__));
1204 KASSERT(m->m_len >= sizeof(struct ether_header),
1205 ("%s: mbuf not large enough for header", __func__));
1206 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1207 vlan.evl_proto = vlan.evl_encap_proto;
1208 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1209 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1210 m->m_len -= sizeof(struct ether_header);
1211 m->m_data += sizeof(struct ether_header);
1213 * If a data link has been supplied by the caller, then we will need to
1214 * re-create a stack allocated mbuf chain with the following structure:
1216 * (1) mbuf #1 will contain the supplied data link
1217 * (2) mbuf #2 will contain the vlan header
1218 * (3) mbuf #3 will contain the original mbuf's packet data
1220 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1224 mv.m_data = (caddr_t)&vlan;
1225 mv.m_len = sizeof(vlan);
1231 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1232 m->m_len += sizeof(struct ether_header);
1233 m->m_data -= sizeof(struct ether_header);
1236 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1237 MODULE_VERSION(ether, 1);