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.25 2005/01/06 09:14:13 hsu 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>
61 #include <net/ethernet.h>
62 #include <net/bridge/bridge.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 <net/ipfw/ip_fw.h>
69 #include <net/dummynet/ip_dummynet.h>
72 #include <netinet6/nd6.h>
76 #include <netproto/ipx/ipx.h>
77 #include <netproto/ipx/ipx_if.h>
78 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
79 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
80 short *tp, int *hlen);
85 #include <netns/ns_if.h>
87 int ether_outputdebug = 0;
88 int ether_inputdebug = 0;
92 #include <netproto/atalk/at.h>
93 #include <netproto/atalk/at_var.h>
94 #include <netproto/atalk/at_extern.h>
96 #define llc_snap_org_code llc_un.type_snap.org_code
97 #define llc_snap_ether_type llc_un.type_snap.ether_type
99 extern u_char at_org_code[3];
100 extern u_char aarp_org_code[3];
101 #endif /* NETATALK */
103 /* netgraph node hooks for ng_ether(4) */
104 void (*ng_ether_input_p)(struct ifnet *ifp,
105 struct mbuf **mp, struct ether_header *eh);
106 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
107 struct mbuf *m, struct ether_header *eh);
108 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
109 void (*ng_ether_attach_p)(struct ifnet *ifp);
110 void (*ng_ether_detach_p)(struct ifnet *ifp);
112 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
113 int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
115 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
122 bridge_in_t *bridge_in_ptr;
123 bdg_forward_t *bdg_forward_ptr;
124 bdgtakeifaces_t *bdgtakeifaces_ptr;
125 struct bdg_softc *ifp2sc;
127 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
130 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
134 #define gotoerr(e) do { error = (e); goto bad; } while (0)
135 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
137 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
138 struct ip_fw **rule, struct ether_header *eh,
141 static int ether_ipfw;
142 SYSCTL_DECL(_net_link);
143 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
144 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
145 ðer_ipfw, 0, "Pass ether pkts through firewall");
148 * Ethernet output routine.
149 * Encapsulate a packet of type family for the local net.
150 * Use trailer local net encapsulation if enough data in first
151 * packet leaves a multiple of 512 bytes of data in remainder.
152 * Assumes that ifp is actually pointer to arpcom structure.
155 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
158 struct ether_header *eh, *deh;
161 int hlen = ETHER_HDR_LEN; /* link layer header length */
162 struct arpcom *ac = IFP2AC(ifp);
165 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
168 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
171 eh = mtod(m, struct ether_header *);
172 edst = eh->ether_dhost;
175 * Fill in the destination ethernet address and frame type.
177 switch (dst->sa_family) {
180 if (!arpresolve(ifp, rt, m, dst, edst))
181 return (0); /* if not yet resolved */
182 eh->ether_type = htons(ETHERTYPE_IP);
187 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
188 return (0); /* Something bad happenned. */
189 eh->ether_type = htons(ETHERTYPE_IPV6);
194 if (ef_outputp != NULL) {
195 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
200 eh->ether_type = htons(ETHERTYPE_IPX);
201 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
202 edst, ETHER_ADDR_LEN);
208 struct at_ifaddr *aa;
210 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
215 * In the phase 2 case, need to prepend an mbuf for
216 * the llc header. Since we must preserve the value
217 * of m, which is passed to us by value, we m_copy()
218 * the first mbuf, and use it for our llc header.
220 if (aa->aa_flags & AFA_PHASE2) {
223 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
224 eh = mtod(m, struct ether_header *);
225 edst = eh->ether_dhost;
226 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
227 llc.llc_control = LLC_UI;
228 bcopy(at_org_code, llc.llc_snap_org_code,
230 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
232 mtod(m, caddr_t) + sizeof(struct ether_header),
234 eh->ether_type = htons(m->m_pkthdr.len);
235 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
237 eh->ether_type = htons(ETHERTYPE_AT);
239 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
248 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
249 eh->ether_type = 0x8137;
251 case 0x0: /* Novell 802.3 */
252 eh->ether_type = htons(m->m_pkthdr.len);
254 case 0xe0e0: /* Novell 802.2 and Token-Ring */
255 M_PREPEND(m, 3, MB_DONTWAIT);
256 eh = mtod(m, struct ether_header *);
257 edst = eh->ether_dhost;
258 eh->ether_type = htons(m->m_pkthdr.len);
259 cp = mtod(m, u_char *) + sizeof(struct ether_header);
265 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
268 * XXX if ns_thishost is the same as the node's ethernet
269 * address then just the default code will catch this anyhow.
270 * So I'm not sure if this next clause should be here at all?
273 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
274 m->m_pkthdr.rcvif = ifp;
275 netisr_dispatch(NETISR_NS, m);
278 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
279 m->m_flags |= M_BCAST;
282 case pseudo_AF_HDRCMPLT:
284 loop_copy = -1; /* if this is for us, don't do it */
285 deh = (struct ether_header *)dst->sa_data;
286 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
287 eh->ether_type = deh->ether_type;
291 printf("%s: can't handle af%d\n", ifp->if_xname,
293 gotoerr(EAFNOSUPPORT);
296 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
297 memcpy(eh->ether_shost,
298 ((struct ether_header *)dst->sa_data)->ether_shost,
301 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
304 * If a simplex interface, and the packet is being sent to our
305 * Ethernet address or a broadcast address, loopback a copy.
306 * XXX To make a simplex device behave exactly like a duplex
307 * device, we should copy in the case of sending to our own
308 * ethernet address (thus letting the original actually appear
309 * on the wire). However, we don't do that here for security
310 * reasons and compatibility with the original behavior.
312 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
315 if (m->m_pkthdr.csum_flags & CSUM_IP)
316 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
317 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
318 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
319 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
322 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
323 n->m_pkthdr.csum_flags |= csum_flags;
324 if (csum_flags & CSUM_DATA_VALID)
325 n->m_pkthdr.csum_data = 0xffff;
326 if_simloop(ifp, n, dst->sa_family, hlen);
329 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
330 ETHER_ADDR_LEN) == 0) {
331 m->m_pkthdr.csum_flags |= csum_flags;
332 if (csum_flags & CSUM_DATA_VALID)
333 m->m_pkthdr.csum_data = 0xffff;
334 if_simloop(ifp, m, dst->sa_family, hlen);
335 return (0); /* XXX */
339 /* Handle ng_ether(4) processing, if any */
340 if (ng_ether_output_p != NULL) {
341 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
347 /* Continue with link-layer output */
348 return ether_output_frame(ifp, m);
356 * Ethernet link layer output routine to send a raw frame to the device.
358 * This assumes that the 14 byte Ethernet header is present and contiguous
359 * in the first mbuf (if BRIDGE'ing).
362 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
364 struct ip_fw *rule = NULL;
368 /* Extract info from dummynet tag, ignore others */
369 while (m->m_type == MT_TAG) {
370 if (m->m_flags == PACKET_TAG_DUMMYNET) {
371 rule = ((struct dn_pkt *)m)->rule;
376 if (rule != NULL) /* packet was already bridged */
379 if (BDG_ACTIVE(ifp)) {
380 struct ether_header *eh; /* a pointer suffices */
382 m->m_pkthdr.rcvif = NULL;
383 eh = mtod(m, struct ether_header *);
384 m_adj(m, ETHER_HDR_LEN);
385 m = bdg_forward_ptr(m, eh, ifp);
392 if (IPFW_LOADED && ether_ipfw != 0) {
393 struct ether_header save_eh, *eh;
395 eh = mtod(m, struct ether_header *);
397 m_adj(m, ETHER_HDR_LEN);
398 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
401 return ENOBUFS; /* pkt dropped */
403 return 0; /* consumed e.g. in a pipe */
405 eh = mtod(m, struct ether_header *);
406 /* packet was ok, restore the ethernet header */
407 if ((void *)(eh + 1) == (void *)m->m_data) {
408 m->m_data -= ETHER_HDR_LEN ;
409 m->m_len += ETHER_HDR_LEN ;
410 m->m_pkthdr.len += ETHER_HDR_LEN ;
412 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
413 if (m == NULL) /* nope... */
415 bcopy(&save_eh, mtod(m, struct ether_header *),
421 * Queue message on interface, update output statistics if
422 * successful, and start output if interface not yet active.
424 if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
431 * ipfw processing for ethernet packets (in and out).
432 * The second parameter is NULL from ether_demux(), and ifp from
433 * ether_output_frame(). This section of code could be used from
434 * bridge.c as well as long as we use some extra info
435 * to distinguish that case from ether_output_frame().
442 struct ether_header *eh,
445 struct ether_header save_eh = *eh; /* might be a ptr in m */
446 struct ip_fw_args args;
449 if (*rule != NULL && fw_one_pass)
450 return TRUE; /* dummynet packet, already partially processed */
453 * I need some amount of data to be contiguous, and in case others
454 * need the packet (shared==TRUE), it also better be in the first mbuf.
456 i = min((*m0)->m_pkthdr.len, max_protohdr);
457 if (shared || (*m0)->m_len < i) {
458 *m0 = m_pullup(*m0, i);
463 args.m = *m0; /* the packet we are looking at */
464 args.oif = dst; /* destination, if any */
465 args.divert_rule = 0; /* we do not support divert yet */
466 args.rule = *rule; /* matching rule to restart */
467 args.next_hop = NULL; /* we do not support forward yet */
468 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
469 i = ip_fw_chk_ptr(&args);
473 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
476 if (i == 0) /* a PASS rule. */
479 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
481 * Pass the pkt to dummynet, which consumes it.
482 * If shared, make a copy and keep the original.
487 m = m_copypacket(*m0, MB_DONTWAIT);
491 m = *m0 ; /* pass the original to dummynet */
492 *m0 = NULL ; /* and nothing back to the caller */
495 * Prepend the header, optimize for the common case of
496 * eh pointing into the mbuf.
498 if ((void *)(eh + 1) == (void *)m->m_data) {
499 m->m_data -= ETHER_HDR_LEN ;
500 m->m_len += ETHER_HDR_LEN ;
501 m->m_pkthdr.len += ETHER_HDR_LEN ;
503 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
506 bcopy(&save_eh, mtod(m, struct ether_header *),
509 ip_dn_io_ptr(m, (i & 0xffff),
510 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
514 * XXX at some point add support for divert/forward actions.
515 * If none of the above matches, we have to drop the pkt.
521 * XXX merge this function with ether_input.
524 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
526 ether_input(ifp, NULL, m);
530 * Process a received Ethernet packet. We have two different interfaces:
531 * one (conventional) assumes the packet in the mbuf, with the ethernet
532 * header provided separately in *eh. The second one (new) has everything
533 * in the mbuf, and we can tell it because eh == NULL.
534 * The caller MUST MAKE SURE that there are at least
535 * sizeof(struct ether_header) bytes in the first mbuf.
537 * This allows us to concentrate in one place a bunch of code which
538 * is replicated in all device drivers. Also, many functions called
539 * from ether_input() try to put the eh back into the mbuf, so we
540 * can later propagate the 'contiguous packet' interface to them,
541 * and handle the old interface just here.
543 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
544 * cluster, right before m_data. So be very careful when working on m,
545 * as you could destroy *eh !!
547 * First we perform any link layer operations, then continue
548 * to the upper layers with ether_demux().
551 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
553 struct ether_header save_eh;
556 if (m->m_len < sizeof(struct ether_header)) {
557 /* XXX error in the caller. */
561 m->m_pkthdr.rcvif = ifp;
562 eh = mtod(m, struct ether_header *);
563 m_adj(m, sizeof(struct ether_header));
565 /* m->m_pkthdr.len = m->m_len; */
568 /* Check for a BPF tap */
569 if (ifp->if_bpf != NULL) {
572 /* This kludge is OK; BPF treats the "mbuf" as read-only */
574 mh.mh_data = (char *)eh;
575 mh.mh_len = ETHER_HDR_LEN;
576 bpf_mtap(ifp, (struct mbuf *)&mh);
579 ifp->if_ibytes += m->m_pkthdr.len + (sizeof *eh);
581 /* Handle ng_ether(4) processing, if any */
582 if (ng_ether_input_p != NULL) {
583 (*ng_ether_input_p)(ifp, &m, eh);
588 /* Check for bridging mode */
589 if (BDG_ACTIVE(ifp)) {
592 /* Check with bridging code */
593 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
597 if (bif != BDG_LOCAL) {
598 save_eh = *eh ; /* because it might change */
599 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
601 * Do not continue if bdg_forward_ptr() processed our
602 * packet (and cleared the mbuf pointer m) or if
603 * it dropped (m_free'd) the packet itself.
606 if (bif == BDG_BCAST || bif == BDG_MCAST)
607 printf("bdg_forward drop MULTICAST PKT\n");
612 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST)
613 goto recvLocal; /* receive locally */
615 /* If not local and not multicast, just drop it */
621 /* Continue with upper layer processing */
622 ether_demux(ifp, eh, m);
626 * Upper layer processing for a received Ethernet packet.
629 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
633 struct ip_fw *rule = NULL;
638 /* Extract info from dummynet tag, ignore others */
639 while (m->m_type == MT_TAG) {
640 if (m->m_flags == PACKET_TAG_DUMMYNET) {
641 rule = ((struct dn_pkt *)m)->rule;
642 ifp = m->m_next->m_pkthdr.rcvif;
647 if (rule) /* packet was already bridged */
651 * Discard packet if upper layers shouldn't see it because
652 * it was unicast to a different Ethernet address. If the
653 * driver is working properly, then this situation can only
654 * happen when the interface is in promiscuous mode.
656 if (!BDG_ACTIVE(ifp) &&
657 ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
658 (eh->ether_dhost[0] & 1) == 0 &&
659 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
663 /* Discard packet if interface is not up */
664 if (!(ifp->if_flags & IFF_UP)) {
668 if (eh->ether_dhost[0] & 1) {
669 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
670 ifp->if_addrlen) == 0)
671 m->m_flags |= M_BCAST;
673 m->m_flags |= M_MCAST;
678 if (IPFW_LOADED && ether_ipfw != 0) {
679 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
683 eh = mtod(m, struct ether_header *);
686 ether_type = ntohs(eh->ether_type);
688 switch (ether_type) {
691 if (ipflow_fastforward(m))
697 if (ifp->if_flags & IFF_NOARP) {
698 /* Discard packet if ARP is disabled on interface */
714 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
721 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
737 if (vlan_input_p != NULL)
738 (*vlan_input_p)(eh, m);
740 m->m_pkthdr.rcvif->if_noproto++;
747 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
751 checksum = mtod(m, ushort *);
753 if ((ether_type <= ETHERMTU) &&
754 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
755 if (*checksum == 0xE0E0) {
756 m->m_pkthdr.len -= 3;
765 if (ether_type > ETHERMTU)
767 l = mtod(m, struct llc *);
768 if (l->llc_dsap == LLC_SNAP_LSAP &&
769 l->llc_ssap == LLC_SNAP_LSAP &&
770 l->llc_control == LLC_UI) {
771 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
772 sizeof at_org_code) == 0 &&
773 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
774 m_adj(m, sizeof(struct llc));
778 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
779 sizeof aarp_org_code) == 0 &&
780 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
781 m_adj(m, sizeof(struct llc));
788 if (ng_ether_input_orphan_p != NULL)
789 (*ng_ether_input_orphan_p)(ifp, m, eh);
794 netisr_dispatch(isr, m);
798 * Perform common duties while attaching to interface list
802 ether_ifattach(struct ifnet *ifp, uint8_t *lla)
804 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header));
808 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen)
811 struct sockaddr_dl *sdl;
813 ifp->if_type = IFT_ETHER;
814 ifp->if_addrlen = ETHER_ADDR_LEN;
815 ifp->if_hdrlen = ETHER_HDR_LEN;
817 ifp->if_mtu = ETHERMTU;
818 if (ifp->if_baudrate == 0)
819 ifp->if_baudrate = 10000000;
820 ifp->if_output = ether_output;
821 ifp->if_input = ether_input_internal;
822 ifp->if_resolvemulti = ether_resolvemulti;
823 ifp->if_broadcastaddr = etherbroadcastaddr;
824 ifa = ifnet_addrs[ifp->if_index - 1];
825 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
826 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
827 sdl->sdl_type = IFT_ETHER;
828 sdl->sdl_alen = ifp->if_addrlen;
829 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
831 * XXX Keep the current drivers happy.
832 * XXX Remove once all drivers have been cleaned up
834 if (lla != IFP2AC(ifp)->ac_enaddr)
835 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
836 bpfattach(ifp, dlt, hdrlen);
837 if (ng_ether_attach_p != NULL)
838 (*ng_ether_attach_p)(ifp);
842 if_printf(ifp, "MAC address: %6D\n", lla, ":");
846 * Perform common duties while detaching an Ethernet interface
849 ether_ifdetach(struct ifnet *ifp)
857 if (ng_ether_detach_p != NULL)
858 (*ng_ether_detach_p)(ifp);
866 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
868 struct ifaddr *ifa = (struct ifaddr *) data;
869 struct ifreq *ifr = (struct ifreq *) data;
874 ifp->if_flags |= IFF_UP;
876 switch (ifa->ifa_addr->sa_family) {
879 ifp->if_init(ifp->if_softc); /* before arpwhohas */
880 arp_ifinit(ifp, ifa);
885 * XXX - This code is probably wrong
889 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
890 struct arpcom *ac = IFP2AC(ifp);
892 if (ipx_nullhost(*ina))
893 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
895 bcopy(ina->x_host.c_host, ac->ac_enaddr,
896 sizeof ac->ac_enaddr);
898 ifp->if_init(ifp->if_softc); /* Set new address. */
904 * XXX - This code is probably wrong
908 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
909 struct arpcom *ac = IFP2AC(ifp);
911 if (ns_nullhost(*ina))
912 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
914 bcopy(ina->x_host.c_host, ac->ac_enaddr,
915 sizeof ac->ac_enaddr);
920 ifp->if_init(ifp->if_softc);
925 ifp->if_init(ifp->if_softc);
931 bcopy(IFP2AC(ifp)->ac_enaddr,
932 ((struct sockaddr *)ifr->ifr_data)->sa_data,
938 * Set the interface MTU.
940 if (ifr->ifr_mtu > ETHERMTU) {
943 ifp->if_mtu = ifr->ifr_mtu;
956 struct sockaddr **llsa,
959 struct sockaddr_dl *sdl;
960 struct sockaddr_in *sin;
962 struct sockaddr_in6 *sin6;
966 switch(sa->sa_family) {
969 * No mapping needed. Just check that it's a valid MC address.
971 sdl = (struct sockaddr_dl *)sa;
972 e_addr = LLADDR(sdl);
973 if ((e_addr[0] & 1) != 1)
974 return EADDRNOTAVAIL;
980 sin = (struct sockaddr_in *)sa;
981 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
982 return EADDRNOTAVAIL;
983 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
985 sdl->sdl_len = sizeof *sdl;
986 sdl->sdl_family = AF_LINK;
987 sdl->sdl_index = ifp->if_index;
988 sdl->sdl_type = IFT_ETHER;
989 sdl->sdl_alen = ETHER_ADDR_LEN;
990 e_addr = LLADDR(sdl);
991 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
992 *llsa = (struct sockaddr *)sdl;
997 sin6 = (struct sockaddr_in6 *)sa;
998 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1000 * An IP6 address of 0 means listen to all
1001 * of the Ethernet multicast address used for IP6.
1002 * (This is used for multicast routers.)
1004 ifp->if_flags |= IFF_ALLMULTI;
1008 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1009 return EADDRNOTAVAIL;
1010 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1012 sdl->sdl_len = sizeof *sdl;
1013 sdl->sdl_family = AF_LINK;
1014 sdl->sdl_index = ifp->if_index;
1015 sdl->sdl_type = IFT_ETHER;
1016 sdl->sdl_alen = ETHER_ADDR_LEN;
1017 e_addr = LLADDR(sdl);
1018 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1019 *llsa = (struct sockaddr *)sdl;
1025 * Well, the text isn't quite right, but it's the name
1028 return EAFNOSUPPORT;
1034 * This is for reference. We have a table-driven version
1035 * of the little-endian crc32 generator, which is faster
1036 * than the double-loop.
1039 ether_crc32_le(const uint8_t *buf, size_t len)
1041 uint32_t c, crc, carry;
1044 crc = 0xffffffffU; /* initial value */
1046 for (i = 0; i < len; i++) {
1048 for (j = 0; j < 8; j++) {
1049 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1053 crc = (crc ^ ETHER_CRC_POLY_LE);
1061 ether_crc32_le(const uint8_t *buf, size_t len)
1063 static const uint32_t crctab[] = {
1064 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1065 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1066 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1067 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1072 crc = 0xffffffffU; /* initial value */
1074 for (i = 0; i < len; i++) {
1076 crc = (crc >> 4) ^ crctab[crc & 0xf];
1077 crc = (crc >> 4) ^ crctab[crc & 0xf];
1085 ether_crc32_be(const uint8_t *buf, size_t len)
1087 uint32_t c, crc, carry;
1090 crc = 0xffffffffU; /* initial value */
1092 for (i = 0; i < len; i++) {
1094 for (j = 0; j < 8; j++) {
1095 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1099 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;