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>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
48 #include <sys/random.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
61 #include <net/ethernet.h>
62 #include <net/if_bridgevar.h>
63 #include <net/if_vlan_var.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_fw.h>
70 #include <netinet/ip_dummynet.h>
73 #include <netinet6/nd6.h>
77 #include <netinet/ip_carp.h>
81 #include <netipx/ipx.h>
82 #include <netipx/ipx_if.h>
84 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
85 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
86 struct sockaddr *dst, short *tp, int *hlen);
89 #include <netatalk/at.h>
90 #include <netatalk/at_var.h>
91 #include <netatalk/at_extern.h>
93 #define llc_snap_org_code llc_un.type_snap.org_code
94 #define llc_snap_ether_type llc_un.type_snap.ether_type
96 extern u_char at_org_code[3];
97 extern u_char aarp_org_code[3];
100 /* netgraph node hooks for ng_ether(4) */
101 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
102 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
103 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
104 void (*ng_ether_attach_p)(struct ifnet *ifp);
105 void (*ng_ether_detach_p)(struct ifnet *ifp);
107 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
109 /* if_bridge(4) support */
110 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
111 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
112 struct sockaddr *, struct rtentry *);
113 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
115 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
116 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
118 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
121 /* XXX: should be in an arp support file, not here */
122 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
124 #define senderr(e) do { error = (e); goto bad;} while (0)
126 #if defined(INET) || defined(INET6)
128 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
129 struct ip_fw **rule, int shared);
130 static int ether_ipfw;
134 * Ethernet output routine.
135 * Encapsulate a packet of type family for the local net.
136 * Use trailer local net encapsulation if enough data in first
137 * packet leaves a multiple of 512 bytes of data in remainder.
140 ether_output(struct ifnet *ifp, struct mbuf *m,
141 struct sockaddr *dst, struct rtentry *rt0)
144 int error, hdrcmplt = 0;
145 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
146 struct ether_header *eh;
148 int hlen; /* link layer header length */
151 error = mac_check_ifnet_transmit(ifp, m);
156 if (ifp->if_flags & IFF_MONITOR)
158 if (!((ifp->if_flags & IFF_UP) &&
159 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
162 hlen = ETHER_HDR_LEN;
163 switch (dst->sa_family) {
166 error = arpresolve(ifp, rt0, m, dst, edst);
168 return (error == EWOULDBLOCK ? 0 : error);
169 type = htons(ETHERTYPE_IP);
174 ah = mtod(m, struct arphdr *);
175 ah->ar_hrd = htons(ARPHRD_ETHER);
177 loop_copy = 0; /* if this is for us, don't do it */
179 switch(ntohs(ah->ar_op)) {
180 case ARPOP_REVREQUEST:
182 type = htons(ETHERTYPE_REVARP);
187 type = htons(ETHERTYPE_ARP);
191 if (m->m_flags & M_BCAST)
192 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
194 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
201 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
204 type = htons(ETHERTYPE_IPV6);
210 error = ef_outputp(ifp, &m, dst, &type, &hlen);
214 type = htons(ETHERTYPE_IPX);
215 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
216 (caddr_t)edst, sizeof (edst));
222 struct at_ifaddr *aa;
224 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
225 senderr(EHOSTUNREACH); /* XXX */
226 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
229 * In the phase 2 case, need to prepend an mbuf for the llc header.
231 if ( aa->aa_flags & AFA_PHASE2 ) {
234 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
237 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
238 llc.llc_control = LLC_UI;
239 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
240 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
241 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
242 type = htons(m->m_pkthdr.len);
243 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
245 type = htons(ETHERTYPE_AT);
249 #endif /* NETATALK */
251 case pseudo_AF_HDRCMPLT:
253 eh = (struct ether_header *)dst->sa_data;
254 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
258 loop_copy = 0; /* if this is for us, don't do it */
259 eh = (struct ether_header *)dst->sa_data;
260 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
261 type = eh->ether_type;
265 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
266 senderr(EAFNOSUPPORT);
270 * Add local net header. If no space in first mbuf,
273 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
276 eh = mtod(m, struct ether_header *);
277 (void)memcpy(&eh->ether_type, &type,
278 sizeof(eh->ether_type));
279 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
281 (void)memcpy(eh->ether_shost, esrc,
282 sizeof(eh->ether_shost));
284 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
285 sizeof(eh->ether_shost));
288 * If a simplex interface, and the packet is being sent to our
289 * Ethernet address or a broadcast address, loopback a copy.
290 * XXX To make a simplex device behave exactly like a duplex
291 * device, we should copy in the case of sending to our own
292 * ethernet address (thus letting the original actually appear
293 * on the wire). However, we don't do that here for security
294 * reasons and compatibility with the original behavior.
296 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
297 m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
300 if (m->m_pkthdr.csum_flags & CSUM_IP)
301 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
302 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
303 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
305 if (m->m_flags & M_BCAST) {
308 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
309 n->m_pkthdr.csum_flags |= csum_flags;
310 if (csum_flags & CSUM_DATA_VALID)
311 n->m_pkthdr.csum_data = 0xffff;
312 (void)if_simloop(ifp, n, dst->sa_family, hlen);
315 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
316 ETHER_ADDR_LEN) == 0) {
317 m->m_pkthdr.csum_flags |= csum_flags;
318 if (csum_flags & CSUM_DATA_VALID)
319 m->m_pkthdr.csum_data = 0xffff;
320 (void) if_simloop(ifp, m, dst->sa_family, hlen);
321 return (0); /* XXX */
326 * Bridges require special output handling.
328 if (ifp->if_bridge) {
329 BRIDGE_OUTPUT(ifp, m, error);
335 (error = carp_output(ifp, m, dst, NULL)))
339 /* Handle ng_ether(4) processing, if any */
340 if (IFP2AC(ifp)->ac_netgraph != NULL) {
341 KASSERT(ng_ether_output_p != NULL,
342 ("ng_ether_output_p is NULL"));
343 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
352 /* Continue with link-layer output */
353 return ether_output_frame(ifp, m);
357 * Ethernet link layer output routine to send a raw frame to the device.
359 * This assumes that the 14 byte Ethernet header is present and contiguous
360 * in the first mbuf (if BRIDGE'ing).
363 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
366 #if defined(INET) || defined(INET6)
367 struct ip_fw *rule = ip_dn_claim_rule(m);
369 if (IPFW_LOADED && ether_ipfw != 0) {
370 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
373 return EACCES; /* pkt dropped */
375 return 0; /* consumed e.g. in a pipe */
381 * Queue message on interface, update output statistics if
382 * successful, and start output if interface not yet active.
384 IFQ_HANDOFF(ifp, m, error);
388 #if defined(INET) || defined(INET6)
390 * ipfw processing for ethernet packets (in and out).
391 * The second parameter is NULL from ether_demux, and ifp from
392 * ether_output_frame.
395 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
396 struct ip_fw **rule, int shared)
398 struct ether_header *eh;
399 struct ether_header save_eh;
402 struct ip_fw_args args;
404 if (*rule != NULL && fw_one_pass)
405 return 1; /* dummynet packet, already partially processed */
408 * I need some amt of data to be contiguous, and in case others need
409 * the packet (shared==1) also better be in the first mbuf.
412 i = min( m->m_pkthdr.len, max_protohdr);
413 if ( shared || m->m_len < i) {
420 eh = mtod(m, struct ether_header *);
421 save_eh = *eh; /* save copy for restore below */
422 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
424 args.m = m; /* the packet we are looking at */
425 args.oif = dst; /* destination, if any */
426 args.rule = *rule; /* matching rule to restart */
427 args.next_hop = NULL; /* we do not support forward yet */
428 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
429 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
430 i = ip_fw_chk_ptr(&args);
434 * Restore Ethernet header, as needed, in case the
435 * mbuf chain was replaced by ipfw.
437 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
442 if (eh != mtod(m, struct ether_header *))
443 bcopy(&save_eh, mtod(m, struct ether_header *),
449 if (i == IP_FW_DENY) /* drop */
452 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
454 if (i == IP_FW_PASS) /* a PASS rule. */
457 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
459 * Pass the pkt to dummynet, which consumes it.
460 * If shared, make a copy and keep the original.
463 m = m_copypacket(m, M_DONTWAIT);
468 * Pass the original to dummynet and
469 * nothing back to the caller
473 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
477 * XXX at some point add support for divert/forward actions.
478 * If none of the above matches, we have to drop the pkt.
485 * Process a received Ethernet packet; the packet is in the
486 * mbuf chain m with the ethernet header at the front.
489 ether_input(struct ifnet *ifp, struct mbuf *m)
491 struct ether_header *eh;
495 * Do consistency checks to verify assumptions
496 * made by code past this point.
498 if ((m->m_flags & M_PKTHDR) == 0) {
499 if_printf(ifp, "discard frame w/o packet header\n");
504 if (m->m_len < ETHER_HDR_LEN) {
505 /* XXX maybe should pullup? */
506 if_printf(ifp, "discard frame w/o leading ethernet "
507 "header (len %u pkt len %u)\n",
508 m->m_len, m->m_pkthdr.len);
513 eh = mtod(m, struct ether_header *);
514 etype = ntohs(eh->ether_type);
515 if (m->m_pkthdr.len >
516 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
517 if_printf(ifp, "discard oversize frame "
518 "(ether type %x flags %x len %u > max %lu)\n",
519 etype, m->m_flags, m->m_pkthdr.len,
520 ETHER_MAX_FRAME(ifp, etype,
521 m->m_flags & M_HASFCS));
526 if (m->m_pkthdr.rcvif == NULL) {
527 if_printf(ifp, "discard frame w/o interface pointer\n");
533 if (m->m_pkthdr.rcvif != ifp) {
534 if_printf(ifp, "Warning, frame marked as received on %s\n",
535 m->m_pkthdr.rcvif->if_xname);
541 * Tag the mbuf with an appropriate MAC label before any other
542 * consumers can get to it.
544 mac_create_mbuf_from_ifnet(ifp, m);
548 * Give bpf a chance at the packet.
552 /* If the CRC is still on the packet, trim it off. */
553 if (m->m_flags & M_HASFCS) {
554 m_adj(m, -ETHER_CRC_LEN);
555 m->m_flags &= ~M_HASFCS;
558 ifp->if_ibytes += m->m_pkthdr.len;
560 if (ifp->if_flags & IFF_MONITOR) {
562 * Interface marked for monitoring; discard packet.
568 /* Handle ng_ether(4) processing, if any */
569 if (IFP2AC(ifp)->ac_netgraph != NULL) {
570 KASSERT(ng_ether_input_p != NULL,
571 ("ng_ether_input_p is NULL"));
572 (*ng_ether_input_p)(ifp, &m);
578 * Tap the packet off here for a bridge. bridge_input()
579 * will return NULL if it has consumed the packet, otherwise
580 * it gets processed as normal. Note that bridge_input()
581 * will always return the original packet if we need to
582 * process it locally.
584 * XXX: When changing the below block, please also look
585 * at the src/sys/netgraph/ng_ether.c:ng_ether_rcv_upper()
587 if (ifp->if_bridge) {
588 BRIDGE_INPUT(ifp, m);
593 /* First chunk of an mbuf contains good entropy */
594 if (harvest.ethernet)
595 random_harvest(m, 16, 3, 0, RANDOM_NET);
600 * Upper layer processing for a received Ethernet packet.
603 ether_demux(struct ifnet *ifp, struct mbuf *m)
605 struct ether_header *eh;
608 #if defined(NETATALK)
611 #if defined(INET) || defined(INET6)
612 struct ip_fw *rule = ip_dn_claim_rule(m);
615 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
617 eh = mtod(m, struct ether_header *);
618 ether_type = ntohs(eh->ether_type);
620 #if defined(INET) || defined(INET6)
621 if (rule) /* packet was already bridged */
625 if (!(ifp->if_bridge) &&
626 !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
627 ifp->if_vlantrunk != NULL)) {
630 * XXX: Okay, we need to call carp_forus() and - if it is for
631 * us jump over code that does the normal check
632 * "IF_LLADDR(ifp) == ether_dhost". The check sequence is a bit
633 * different from OpenBSD, so we jump over as few code as
634 * possible, to catch _all_ sanity checks. This needs
635 * evaluation, to see if the carp ether_dhost values break any
638 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
642 * Discard packet if upper layers shouldn't see it because it
643 * was unicast to a different Ethernet address. If the driver
644 * is working properly, then this situation can only happen
645 * when the interface is in promiscuous mode.
647 * If VLANs are active, and this packet has a VLAN tag, do
648 * not drop it here but pass it on to the VLAN layer, to
649 * give them a chance to consider it as well (e. g. in case
650 * bridging is only active on a VLAN). They will drop it if
653 if ((ifp->if_flags & IFF_PROMISC) != 0
654 && !ETHER_IS_MULTICAST(eh->ether_dhost)
655 && bcmp(eh->ether_dhost,
656 IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0
657 && (ifp->if_flags & IFF_PPROMISC) == 0) {
666 /* Discard packet if interface is not up */
667 if ((ifp->if_flags & IFF_UP) == 0) {
671 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
672 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
673 sizeof(etherbroadcastaddr)) == 0)
674 m->m_flags |= M_BCAST;
676 m->m_flags |= M_MCAST;
678 if (m->m_flags & (M_BCAST|M_MCAST))
681 #if defined(INET) || defined(INET6)
683 if (IPFW_LOADED && ether_ipfw != 0) {
684 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
693 * Check to see if the device performed the VLAN decapsulation and
694 * provided us with the tag.
696 if (m->m_flags & M_VLANTAG) {
698 * If no VLANs are configured, drop.
700 if (ifp->if_vlantrunk == NULL) {
706 * vlan_input() will either recursively call ether_input()
707 * or drop the packet.
709 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
710 (*vlan_input_p)(ifp, m);
715 * Handle protocols that expect to have the Ethernet header
716 * (and possibly FCS) intact.
718 switch (ether_type) {
720 if (ifp->if_vlantrunk != NULL) {
721 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
722 (*vlan_input_p)(ifp, m);
730 /* Strip off Ethernet header. */
731 m_adj(m, ETHER_HDR_LEN);
733 /* If the CRC is still on the packet, trim it off. */
734 if (m->m_flags & M_HASFCS) {
735 m_adj(m, -ETHER_CRC_LEN);
736 m->m_flags &= ~M_HASFCS;
739 /* Reset layer specific mbuf flags to avoid confusing upper layers. */
740 m->m_flags &= ~(M_PROTOFLAGS);
742 switch (ether_type) {
745 if ((m = ip_fastforward(m)) == NULL)
751 if (ifp->if_flags & IFF_NOARP) {
752 /* Discard packet if ARP is disabled on interface */
761 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
778 #endif /* NETATALK */
781 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
784 #if defined(NETATALK)
785 if (ether_type > ETHERMTU)
787 l = mtod(m, struct llc *);
788 if (l->llc_dsap == LLC_SNAP_LSAP &&
789 l->llc_ssap == LLC_SNAP_LSAP &&
790 l->llc_control == LLC_UI) {
791 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
792 sizeof(at_org_code)) == 0 &&
793 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
794 m_adj(m, LLC_SNAPFRAMELEN);
798 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
799 sizeof(aarp_org_code)) == 0 &&
800 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
801 m_adj(m, LLC_SNAPFRAMELEN);
806 #endif /* NETATALK */
809 netisr_dispatch(isr, m);
814 * Packet is to be discarded. If netgraph is present,
815 * hand the packet to it for last chance processing;
816 * otherwise dispose of it.
818 if (IFP2AC(ifp)->ac_netgraph != NULL) {
819 KASSERT(ng_ether_input_orphan_p != NULL,
820 ("ng_ether_input_orphan_p is NULL"));
822 * Put back the ethernet header so netgraph has a
823 * consistent view of inbound packets.
825 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
826 (*ng_ether_input_orphan_p)(ifp, m);
833 * Convert Ethernet address to printable (loggable) representation.
834 * This routine is for compatibility; it's better to just use
836 * printf("%6D", <pointer to address>, ":");
838 * since there's no static buffer involved.
841 ether_sprintf(const u_char *ap)
843 static char etherbuf[18];
844 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
849 * Perform common duties while attaching to interface list
852 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
856 struct sockaddr_dl *sdl;
858 ifp->if_addrlen = ETHER_ADDR_LEN;
859 ifp->if_hdrlen = ETHER_HDR_LEN;
861 ifp->if_mtu = ETHERMTU;
862 ifp->if_output = ether_output;
863 ifp->if_input = ether_input;
864 ifp->if_resolvemulti = ether_resolvemulti;
865 if (ifp->if_baudrate == 0)
866 ifp->if_baudrate = IF_Mbps(10); /* just a default */
867 ifp->if_broadcastaddr = etherbroadcastaddr;
870 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
871 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
872 sdl->sdl_type = IFT_ETHER;
873 sdl->sdl_alen = ifp->if_addrlen;
874 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
876 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
877 if (ng_ether_attach_p != NULL)
878 (*ng_ether_attach_p)(ifp);
880 /* Announce Ethernet MAC address if non-zero. */
881 for (i = 0; i < ifp->if_addrlen; i++)
884 if (i != ifp->if_addrlen)
885 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
886 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
887 if_printf(ifp, "if_start running deferred for Giant\n");
891 * Perform common duties while detaching an Ethernet interface
894 ether_ifdetach(struct ifnet *ifp)
896 if (IFP2AC(ifp)->ac_netgraph != NULL) {
897 KASSERT(ng_ether_detach_p != NULL,
898 ("ng_ether_detach_p is NULL"));
899 (*ng_ether_detach_p)(ifp);
906 SYSCTL_DECL(_net_link);
907 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
908 #if defined(INET) || defined(INET6)
909 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
910 ðer_ipfw,0,"Pass ether pkts through firewall");
915 * This is for reference. We have a table-driven version
916 * of the little-endian crc32 generator, which is faster
917 * than the double-loop.
920 ether_crc32_le(const uint8_t *buf, size_t len)
927 crc = 0xffffffff; /* initial value */
929 for (i = 0; i < len; i++) {
930 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
931 carry = (crc ^ data) & 1;
934 crc = (crc ^ ETHER_CRC_POLY_LE);
941 ether_crc32_le(const uint8_t *buf, size_t len)
943 static const uint32_t crctab[] = {
944 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
945 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
946 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
947 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
952 crc = 0xffffffff; /* initial value */
954 for (i = 0; i < len; i++) {
956 crc = (crc >> 4) ^ crctab[crc & 0xf];
957 crc = (crc >> 4) ^ crctab[crc & 0xf];
965 ether_crc32_be(const uint8_t *buf, size_t len)
972 crc = 0xffffffff; /* initial value */
974 for (i = 0; i < len; i++) {
975 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
976 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
979 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
987 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
989 struct ifaddr *ifa = (struct ifaddr *) data;
990 struct ifreq *ifr = (struct ifreq *) data;
995 ifp->if_flags |= IFF_UP;
997 switch (ifa->ifa_addr->sa_family) {
1000 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1001 arp_ifinit(ifp, ifa);
1006 * XXX - This code is probably wrong
1010 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1012 if (ipx_nullhost(*ina))
1017 bcopy((caddr_t) ina->x_host.c_host,
1018 (caddr_t) IF_LLADDR(ifp),
1025 ifp->if_init(ifp->if_softc);
1030 ifp->if_init(ifp->if_softc);
1037 struct sockaddr *sa;
1039 sa = (struct sockaddr *) & ifr->ifr_data;
1040 bcopy(IF_LLADDR(ifp),
1041 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1047 * Set the interface MTU.
1049 if (ifr->ifr_mtu > ETHERMTU) {
1052 ifp->if_mtu = ifr->ifr_mtu;
1056 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1063 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1064 struct sockaddr *sa)
1066 struct sockaddr_dl *sdl;
1068 struct sockaddr_in *sin;
1071 struct sockaddr_in6 *sin6;
1075 switch(sa->sa_family) {
1078 * No mapping needed. Just check that it's a valid MC address.
1080 sdl = (struct sockaddr_dl *)sa;
1081 e_addr = LLADDR(sdl);
1082 if (!ETHER_IS_MULTICAST(e_addr))
1083 return EADDRNOTAVAIL;
1089 sin = (struct sockaddr_in *)sa;
1090 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1091 return EADDRNOTAVAIL;
1092 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1096 sdl->sdl_len = sizeof *sdl;
1097 sdl->sdl_family = AF_LINK;
1098 sdl->sdl_index = ifp->if_index;
1099 sdl->sdl_type = IFT_ETHER;
1100 sdl->sdl_alen = ETHER_ADDR_LEN;
1101 e_addr = LLADDR(sdl);
1102 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1103 *llsa = (struct sockaddr *)sdl;
1108 sin6 = (struct sockaddr_in6 *)sa;
1109 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1111 * An IP6 address of 0 means listen to all
1112 * of the Ethernet multicast address used for IP6.
1113 * (This is used for multicast routers.)
1115 ifp->if_flags |= IFF_ALLMULTI;
1119 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1120 return EADDRNOTAVAIL;
1121 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1125 sdl->sdl_len = sizeof *sdl;
1126 sdl->sdl_family = AF_LINK;
1127 sdl->sdl_index = ifp->if_index;
1128 sdl->sdl_type = IFT_ETHER;
1129 sdl->sdl_alen = ETHER_ADDR_LEN;
1130 e_addr = LLADDR(sdl);
1131 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1132 *llsa = (struct sockaddr *)sdl;
1138 * Well, the text isn't quite right, but it's the name
1141 return EAFNOSUPPORT;
1146 ether_alloc(u_char type, struct ifnet *ifp)
1150 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1157 ether_free(void *com, u_char type)
1160 free(com, M_ARPCOM);
1164 ether_modevent(module_t mod, int type, void *data)
1169 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1172 if_deregister_com_alloc(IFT_ETHER);
1181 static moduledata_t ether_mod = {
1187 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1188 MODULE_VERSION(ether, 1);