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.29 2005/03/12 02:42:28 joerg Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.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>
60 #include <net/ifq_var.h>
62 #include <net/ethernet.h>
63 #include <net/bridge/bridge.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 <net/ipfw/ip_fw.h>
70 #include <net/dummynet/ip_dummynet.h>
73 #include <netinet6/nd6.h>
77 #include <netproto/ipx/ipx.h>
78 #include <netproto/ipx/ipx_if.h>
79 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
80 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
81 short *tp, int *hlen);
86 #include <netns/ns_if.h>
88 int ether_outputdebug = 0;
89 int ether_inputdebug = 0;
93 #include <netproto/atalk/at.h>
94 #include <netproto/atalk/at_var.h>
95 #include <netproto/atalk/at_extern.h>
97 #define llc_snap_org_code llc_un.type_snap.org_code
98 #define llc_snap_ether_type llc_un.type_snap.ether_type
100 extern u_char at_org_code[3];
101 extern u_char aarp_org_code[3];
102 #endif /* NETATALK */
104 /* netgraph node hooks for ng_ether(4) */
105 void (*ng_ether_input_p)(struct ifnet *ifp,
106 struct mbuf **mp, struct ether_header *eh);
107 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
108 struct mbuf *m, struct ether_header *eh);
109 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
110 void (*ng_ether_attach_p)(struct ifnet *ifp);
111 void (*ng_ether_detach_p)(struct ifnet *ifp);
113 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
114 int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
116 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
123 bridge_in_t *bridge_in_ptr;
124 bdg_forward_t *bdg_forward_ptr;
125 bdgtakeifaces_t *bdgtakeifaces_ptr;
126 struct bdg_softc *ifp2sc;
128 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
131 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
135 #define gotoerr(e) do { error = (e); goto bad; } while (0)
136 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
138 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
139 struct ip_fw **rule, struct ether_header *eh,
142 static int ether_ipfw;
143 SYSCTL_DECL(_net_link);
144 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
145 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
146 ðer_ipfw, 0, "Pass ether pkts through firewall");
149 * Ethernet output routine.
150 * Encapsulate a packet of type family for the local net.
151 * Use trailer local net encapsulation if enough data in first
152 * packet leaves a multiple of 512 bytes of data in remainder.
153 * Assumes that ifp is actually pointer to arpcom structure.
156 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
159 struct ether_header *eh, *deh;
162 int hlen = ETHER_HDR_LEN; /* link layer header length */
163 struct arpcom *ac = IFP2AC(ifp);
166 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
169 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
172 eh = mtod(m, struct ether_header *);
173 edst = eh->ether_dhost;
176 * Fill in the destination ethernet address and frame type.
178 switch (dst->sa_family) {
181 if (!arpresolve(ifp, rt, m, dst, edst))
182 return (0); /* if not yet resolved */
183 eh->ether_type = htons(ETHERTYPE_IP);
188 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
189 return (0); /* Something bad happenned. */
190 eh->ether_type = htons(ETHERTYPE_IPV6);
195 if (ef_outputp != NULL) {
196 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
201 eh->ether_type = htons(ETHERTYPE_IPX);
202 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
203 edst, ETHER_ADDR_LEN);
209 struct at_ifaddr *aa;
211 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
216 * In the phase 2 case, need to prepend an mbuf for
217 * the llc header. Since we must preserve the value
218 * of m, which is passed to us by value, we m_copy()
219 * the first mbuf, and use it for our llc header.
221 if (aa->aa_flags & AFA_PHASE2) {
224 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
225 eh = mtod(m, struct ether_header *);
226 edst = eh->ether_dhost;
227 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
228 llc.llc_control = LLC_UI;
229 bcopy(at_org_code, llc.llc_snap_org_code,
231 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
233 mtod(m, caddr_t) + sizeof(struct ether_header),
235 eh->ether_type = htons(m->m_pkthdr.len);
236 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
238 eh->ether_type = htons(ETHERTYPE_AT);
240 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
249 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
250 eh->ether_type = 0x8137;
252 case 0x0: /* Novell 802.3 */
253 eh->ether_type = htons(m->m_pkthdr.len);
255 case 0xe0e0: /* Novell 802.2 and Token-Ring */
256 M_PREPEND(m, 3, MB_DONTWAIT);
257 eh = mtod(m, struct ether_header *);
258 edst = eh->ether_dhost;
259 eh->ether_type = htons(m->m_pkthdr.len);
260 cp = mtod(m, u_char *) + sizeof(struct ether_header);
266 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
269 * XXX if ns_thishost is the same as the node's ethernet
270 * address then just the default code will catch this anyhow.
271 * So I'm not sure if this next clause should be here at all?
274 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
275 m->m_pkthdr.rcvif = ifp;
276 netisr_dispatch(NETISR_NS, m);
279 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
280 m->m_flags |= M_BCAST;
283 case pseudo_AF_HDRCMPLT:
285 loop_copy = -1; /* if this is for us, don't do it */
286 deh = (struct ether_header *)dst->sa_data;
287 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
288 eh->ether_type = deh->ether_type;
292 printf("%s: can't handle af%d\n", ifp->if_xname,
294 gotoerr(EAFNOSUPPORT);
297 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
298 memcpy(eh->ether_shost,
299 ((struct ether_header *)dst->sa_data)->ether_shost,
302 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
305 * If a simplex interface, and the packet is being sent to our
306 * Ethernet address or a broadcast address, loopback a copy.
307 * XXX To make a simplex device behave exactly like a duplex
308 * device, we should copy in the case of sending to our own
309 * ethernet address (thus letting the original actually appear
310 * on the wire). However, we don't do that here for security
311 * reasons and compatibility with the original behavior.
313 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
316 if (m->m_pkthdr.csum_flags & CSUM_IP)
317 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
318 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
319 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
320 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
323 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
324 n->m_pkthdr.csum_flags |= csum_flags;
325 if (csum_flags & CSUM_DATA_VALID)
326 n->m_pkthdr.csum_data = 0xffff;
327 if_simloop(ifp, n, dst->sa_family, hlen);
330 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
331 ETHER_ADDR_LEN) == 0) {
332 m->m_pkthdr.csum_flags |= csum_flags;
333 if (csum_flags & CSUM_DATA_VALID)
334 m->m_pkthdr.csum_data = 0xffff;
335 if_simloop(ifp, m, dst->sa_family, hlen);
336 return (0); /* XXX */
340 /* Handle ng_ether(4) processing, if any */
341 if (ng_ether_output_p != NULL) {
342 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
348 /* Continue with link-layer output */
349 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)
365 struct ip_fw *rule = NULL;
368 struct altq_pktattr pktattr;
370 /* Extract info from dummynet tag, ignore others */
371 while (m->m_type == MT_TAG) {
372 if (m->m_flags == PACKET_TAG_DUMMYNET) {
373 rule = ((struct dn_pkt *)m)->rule;
378 if (rule != NULL) /* packet was already bridged */
381 if (BDG_ACTIVE(ifp)) {
382 struct ether_header *eh; /* a pointer suffices */
384 m->m_pkthdr.rcvif = NULL;
385 eh = mtod(m, struct ether_header *);
386 m_adj(m, ETHER_HDR_LEN);
387 m = bdg_forward_ptr(m, eh, ifp);
393 if (ifq_is_enabled(&ifp->if_snd))
394 altq_etherclassify(&ifp->if_snd, m, &pktattr);
396 if (IPFW_LOADED && ether_ipfw != 0) {
397 struct ether_header save_eh, *eh;
399 eh = mtod(m, struct ether_header *);
401 m_adj(m, ETHER_HDR_LEN);
402 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
405 return ENOBUFS; /* pkt dropped */
407 return 0; /* consumed e.g. in a pipe */
409 eh = mtod(m, struct ether_header *);
410 /* packet was ok, restore the ethernet header */
411 if ((void *)(eh + 1) == (void *)m->m_data) {
412 m->m_data -= ETHER_HDR_LEN ;
413 m->m_len += ETHER_HDR_LEN ;
414 m->m_pkthdr.len += ETHER_HDR_LEN ;
416 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
417 if (m == NULL) /* nope... */
419 bcopy(&save_eh, mtod(m, struct ether_header *),
425 * Queue message on interface, update output statistics if
426 * successful, and start output if interface not yet active.
428 error = ifq_handoff(ifp, m, &pktattr);
434 * ipfw processing for ethernet packets (in and out).
435 * The second parameter is NULL from ether_demux(), and ifp from
436 * ether_output_frame(). This section of code could be used from
437 * bridge.c as well as long as we use some extra info
438 * to distinguish that case from ether_output_frame().
445 struct ether_header *eh,
448 struct ether_header save_eh = *eh; /* might be a ptr in m */
449 struct ip_fw_args args;
452 if (*rule != NULL && fw_one_pass)
453 return TRUE; /* dummynet packet, already partially processed */
456 * I need some amount of data to be contiguous, and in case others
457 * need the packet (shared==TRUE), it also better be in the first mbuf.
459 i = min((*m0)->m_pkthdr.len, max_protohdr);
460 if (shared || (*m0)->m_len < i) {
461 *m0 = m_pullup(*m0, i);
466 args.m = *m0; /* the packet we are looking at */
467 args.oif = dst; /* destination, if any */
468 args.divert_rule = 0; /* we do not support divert yet */
469 args.rule = *rule; /* matching rule to restart */
470 args.next_hop = NULL; /* we do not support forward yet */
471 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
472 i = ip_fw_chk_ptr(&args);
476 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
479 if (i == 0) /* a PASS rule. */
482 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
484 * Pass the pkt to dummynet, which consumes it.
485 * If shared, make a copy and keep the original.
490 m = m_copypacket(*m0, MB_DONTWAIT);
494 m = *m0 ; /* pass the original to dummynet */
495 *m0 = NULL ; /* and nothing back to the caller */
498 * Prepend the header, optimize for the common case of
499 * eh pointing into the mbuf.
501 if ((void *)(eh + 1) == (void *)m->m_data) {
502 m->m_data -= ETHER_HDR_LEN ;
503 m->m_len += ETHER_HDR_LEN ;
504 m->m_pkthdr.len += ETHER_HDR_LEN ;
506 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
509 bcopy(&save_eh, mtod(m, struct ether_header *),
512 ip_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 * XXX merge this function with ether_input.
527 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
529 ether_input(ifp, NULL, m);
533 * Process a received Ethernet packet. We have two different interfaces:
534 * one (conventional) assumes the packet in the mbuf, with the ethernet
535 * header provided separately in *eh. The second one (new) has everything
536 * in the mbuf, and we can tell it because eh == NULL.
537 * The caller MUST MAKE SURE that there are at least
538 * sizeof(struct ether_header) bytes in the first mbuf.
540 * This allows us to concentrate in one place a bunch of code which
541 * is replicated in all device drivers. Also, many functions called
542 * from ether_input() try to put the eh back into the mbuf, so we
543 * can later propagate the 'contiguous packet' interface to them,
544 * and handle the old interface just here.
546 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
547 * cluster, right before m_data. So be very careful when working on m,
548 * as you could destroy *eh !!
550 * First we perform any link layer operations, then continue
551 * to the upper layers with ether_demux().
554 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
556 struct ether_header save_eh;
559 if (m->m_len < sizeof(struct ether_header)) {
560 /* XXX error in the caller. */
564 m->m_pkthdr.rcvif = ifp;
565 eh = mtod(m, struct ether_header *);
566 m_adj(m, sizeof(struct ether_header));
568 /* m->m_pkthdr.len = m->m_len; */
572 bpf_ptap(ifp->if_bpf, m, eh, ETHER_HDR_LEN);
574 ifp->if_ibytes += m->m_pkthdr.len + (sizeof *eh);
576 /* Handle ng_ether(4) processing, if any */
577 if (ng_ether_input_p != NULL) {
578 (*ng_ether_input_p)(ifp, &m, eh);
583 /* Check for bridging mode */
584 if (BDG_ACTIVE(ifp)) {
587 /* Check with bridging code */
588 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
592 if (bif != BDG_LOCAL) {
593 save_eh = *eh ; /* because it might change */
594 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
596 * Do not continue if bdg_forward_ptr() processed our
597 * packet (and cleared the mbuf pointer m) or if
598 * it dropped (m_free'd) the packet itself.
601 if (bif == BDG_BCAST || bif == BDG_MCAST)
602 printf("bdg_forward drop MULTICAST PKT\n");
607 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST)
608 goto recvLocal; /* receive locally */
610 /* If not local and not multicast, just drop it */
616 /* Continue with upper layer processing */
617 ether_demux(ifp, eh, m);
621 * Upper layer processing for a received Ethernet packet.
624 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
628 struct ip_fw *rule = NULL;
633 /* Extract info from dummynet tag, ignore others */
634 while (m->m_type == MT_TAG) {
635 if (m->m_flags == PACKET_TAG_DUMMYNET) {
636 rule = ((struct dn_pkt *)m)->rule;
637 ifp = m->m_next->m_pkthdr.rcvif;
642 if (rule) /* packet was already bridged */
646 * Discard packet if upper layers shouldn't see it because
647 * it was unicast to a different Ethernet address. If the
648 * driver is working properly, then this situation can only
649 * happen when the interface is in promiscuous mode.
651 if (!BDG_ACTIVE(ifp) &&
652 ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
653 (eh->ether_dhost[0] & 1) == 0 &&
654 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
658 /* Discard packet if interface is not up */
659 if (!(ifp->if_flags & IFF_UP)) {
663 if (eh->ether_dhost[0] & 1) {
664 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
665 ifp->if_addrlen) == 0)
666 m->m_flags |= M_BCAST;
668 m->m_flags |= M_MCAST;
673 if (IPFW_LOADED && ether_ipfw != 0) {
674 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
678 eh = mtod(m, struct ether_header *);
681 ether_type = ntohs(eh->ether_type);
683 switch (ether_type) {
686 if (ipflow_fastforward(m))
692 if (ifp->if_flags & IFF_NOARP) {
693 /* Discard packet if ARP is disabled on interface */
709 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
716 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
732 if (vlan_input_p != NULL)
733 (*vlan_input_p)(eh, m);
735 m->m_pkthdr.rcvif->if_noproto++;
742 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
746 checksum = mtod(m, ushort *);
748 if ((ether_type <= ETHERMTU) &&
749 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
750 if (*checksum == 0xE0E0) {
751 m->m_pkthdr.len -= 3;
760 if (ether_type > ETHERMTU)
762 l = mtod(m, struct llc *);
763 if (l->llc_dsap == LLC_SNAP_LSAP &&
764 l->llc_ssap == LLC_SNAP_LSAP &&
765 l->llc_control == LLC_UI) {
766 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
767 sizeof at_org_code) == 0 &&
768 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
769 m_adj(m, sizeof(struct llc));
773 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
774 sizeof aarp_org_code) == 0 &&
775 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
776 m_adj(m, sizeof(struct llc));
783 if (ng_ether_input_orphan_p != NULL)
784 (*ng_ether_input_orphan_p)(ifp, m, eh);
789 netisr_dispatch(isr, m);
793 * Perform common duties while attaching to interface list
797 ether_ifattach(struct ifnet *ifp, uint8_t *lla)
799 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header));
803 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen)
806 struct sockaddr_dl *sdl;
808 ifp->if_type = IFT_ETHER;
809 ifp->if_addrlen = ETHER_ADDR_LEN;
810 ifp->if_hdrlen = ETHER_HDR_LEN;
812 ifp->if_mtu = ETHERMTU;
813 if (ifp->if_baudrate == 0)
814 ifp->if_baudrate = 10000000;
815 ifp->if_output = ether_output;
816 ifp->if_input = ether_input_internal;
817 ifp->if_resolvemulti = ether_resolvemulti;
818 ifp->if_broadcastaddr = etherbroadcastaddr;
819 ifa = ifnet_addrs[ifp->if_index - 1];
820 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
821 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
822 sdl->sdl_type = IFT_ETHER;
823 sdl->sdl_alen = ifp->if_addrlen;
824 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
826 * XXX Keep the current drivers happy.
827 * XXX Remove once all drivers have been cleaned up
829 if (lla != IFP2AC(ifp)->ac_enaddr)
830 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
831 bpfattach(ifp, dlt, hdrlen);
832 if (ng_ether_attach_p != NULL)
833 (*ng_ether_attach_p)(ifp);
837 if_printf(ifp, "MAC address: %6D\n", lla, ":");
841 * Perform common duties while detaching an Ethernet interface
844 ether_ifdetach(struct ifnet *ifp)
852 if (ng_ether_detach_p != NULL)
853 (*ng_ether_detach_p)(ifp);
861 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
863 struct ifaddr *ifa = (struct ifaddr *) data;
864 struct ifreq *ifr = (struct ifreq *) data;
869 ifp->if_flags |= IFF_UP;
871 switch (ifa->ifa_addr->sa_family) {
874 ifp->if_init(ifp->if_softc); /* before arpwhohas */
875 arp_ifinit(ifp, ifa);
880 * XXX - This code is probably wrong
884 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
885 struct arpcom *ac = IFP2AC(ifp);
887 if (ipx_nullhost(*ina))
888 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
890 bcopy(ina->x_host.c_host, ac->ac_enaddr,
891 sizeof ac->ac_enaddr);
893 ifp->if_init(ifp->if_softc); /* Set new address. */
899 * XXX - This code is probably wrong
903 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
904 struct arpcom *ac = IFP2AC(ifp);
906 if (ns_nullhost(*ina))
907 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
909 bcopy(ina->x_host.c_host, ac->ac_enaddr,
910 sizeof ac->ac_enaddr);
915 ifp->if_init(ifp->if_softc);
920 ifp->if_init(ifp->if_softc);
926 bcopy(IFP2AC(ifp)->ac_enaddr,
927 ((struct sockaddr *)ifr->ifr_data)->sa_data,
933 * Set the interface MTU.
935 if (ifr->ifr_mtu > ETHERMTU) {
938 ifp->if_mtu = ifr->ifr_mtu;
951 struct sockaddr **llsa,
954 struct sockaddr_dl *sdl;
955 struct sockaddr_in *sin;
957 struct sockaddr_in6 *sin6;
961 switch(sa->sa_family) {
964 * No mapping needed. Just check that it's a valid MC address.
966 sdl = (struct sockaddr_dl *)sa;
967 e_addr = LLADDR(sdl);
968 if ((e_addr[0] & 1) != 1)
969 return EADDRNOTAVAIL;
975 sin = (struct sockaddr_in *)sa;
976 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
977 return EADDRNOTAVAIL;
978 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
980 sdl->sdl_len = sizeof *sdl;
981 sdl->sdl_family = AF_LINK;
982 sdl->sdl_index = ifp->if_index;
983 sdl->sdl_type = IFT_ETHER;
984 sdl->sdl_alen = ETHER_ADDR_LEN;
985 e_addr = LLADDR(sdl);
986 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
987 *llsa = (struct sockaddr *)sdl;
992 sin6 = (struct sockaddr_in6 *)sa;
993 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
995 * An IP6 address of 0 means listen to all
996 * of the Ethernet multicast address used for IP6.
997 * (This is used for multicast routers.)
999 ifp->if_flags |= IFF_ALLMULTI;
1003 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1004 return EADDRNOTAVAIL;
1005 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1007 sdl->sdl_len = sizeof *sdl;
1008 sdl->sdl_family = AF_LINK;
1009 sdl->sdl_index = ifp->if_index;
1010 sdl->sdl_type = IFT_ETHER;
1011 sdl->sdl_alen = ETHER_ADDR_LEN;
1012 e_addr = LLADDR(sdl);
1013 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1014 *llsa = (struct sockaddr *)sdl;
1020 * Well, the text isn't quite right, but it's the name
1023 return EAFNOSUPPORT;
1029 * This is for reference. We have a table-driven version
1030 * of the little-endian crc32 generator, which is faster
1031 * than the double-loop.
1034 ether_crc32_le(const uint8_t *buf, size_t len)
1036 uint32_t c, crc, carry;
1039 crc = 0xffffffffU; /* initial value */
1041 for (i = 0; i < len; i++) {
1043 for (j = 0; j < 8; j++) {
1044 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1048 crc = (crc ^ ETHER_CRC_POLY_LE);
1056 ether_crc32_le(const uint8_t *buf, size_t len)
1058 static const uint32_t crctab[] = {
1059 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1060 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1061 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1062 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1067 crc = 0xffffffffU; /* initial value */
1069 for (i = 0; i < len; i++) {
1071 crc = (crc >> 4) ^ crctab[crc & 0xf];
1072 crc = (crc >> 4) ^ crctab[crc & 0xf];
1080 ether_crc32_be(const uint8_t *buf, size_t len)
1082 uint32_t c, crc, carry;
1085 crc = 0xffffffffU; /* initial value */
1087 for (i = 0; i < len; i++) {
1089 for (j = 0; j < 8; j++) {
1090 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1094 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1102 * find the size of ethernet header, and call classifier
1105 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1106 struct altq_pktattr *pktattr)
1108 struct ether_header *eh;
1109 uint16_t ether_type;
1110 int hlen, af, hdrsize;
1113 hlen = sizeof(struct ether_header);
1114 eh = mtod(m, struct ether_header *);
1116 ether_type = ntohs(eh->ether_type);
1117 if (ether_type < ETHERMTU) {
1119 struct llc *llc = (struct llc *)(eh + 1);
1122 if (m->m_len < hlen ||
1123 llc->llc_dsap != LLC_SNAP_LSAP ||
1124 llc->llc_ssap != LLC_SNAP_LSAP ||
1125 llc->llc_control != LLC_UI)
1126 goto bad; /* not snap! */
1128 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1131 if (ether_type == ETHERTYPE_IP) {
1133 hdrsize = 20; /* sizeof(struct ip) */
1135 } else if (ether_type == ETHERTYPE_IPV6) {
1137 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1142 while (m->m_len <= hlen) {
1146 hdr = m->m_data + hlen;
1147 if (m->m_len < hlen + hdrsize) {
1149 * ip header is not in a single mbuf. this should not
1150 * happen in the current code.
1151 * (todo: use m_pulldown in the future)
1157 ifq_classify(ifq, m, af, pktattr);
1164 pktattr->pattr_class = NULL;
1165 pktattr->pattr_hdr = NULL;
1166 pktattr->pattr_af = AF_UNSPEC;