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.36 2006/06/25 11:02:39 corecode Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
42 #include "opt_netgraph.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.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>
59 #include <net/ifq_var.h>
61 #include <net/ethernet.h>
63 #if defined(INET) || defined(INET6)
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #include <net/ipfw/ip_fw.h>
68 #include <net/dummynet/ip_dummynet.h>
71 #include <netinet6/nd6.h>
75 #include <netproto/ipx/ipx.h>
76 #include <netproto/ipx/ipx_if.h>
77 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
78 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
79 short *tp, int *hlen);
84 #include <netns/ns_if.h>
86 int ether_outputdebug = 0;
87 int ether_inputdebug = 0;
91 #include <netproto/atalk/at.h>
92 #include <netproto/atalk/at_var.h>
93 #include <netproto/atalk/at_extern.h>
95 #define llc_snap_org_code llc_un.type_snap.org_code
96 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 extern u_char at_org_code[3];
99 extern u_char aarp_org_code[3];
100 #endif /* NETATALK */
102 /* netgraph node hooks for ng_ether(4) */
103 void (*ng_ether_input_p)(struct ifnet *ifp,
104 struct mbuf **mp, struct ether_header *eh);
105 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
106 struct mbuf *m, struct ether_header *eh);
107 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
108 void (*ng_ether_attach_p)(struct ifnet *ifp);
109 void (*ng_ether_detach_p)(struct ifnet *ifp);
111 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
112 int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
114 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
122 struct sockaddr *, struct rtentry *);
123 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 struct ip_fw **rule, struct ether_header *eh,
139 static int ether_ipfw;
140 SYSCTL_DECL(_net_link);
141 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
142 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
143 ðer_ipfw, 0, "Pass ether pkts through firewall");
146 * Ethernet output routine.
147 * Encapsulate a packet of type family for the local net.
148 * Use trailer local net encapsulation if enough data in first
149 * packet leaves a multiple of 512 bytes of data in remainder.
150 * Assumes that ifp is actually pointer to arpcom structure.
153 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
156 struct ether_header *eh, *deh;
159 int hlen = ETHER_HDR_LEN; /* link layer header length */
160 struct arpcom *ac = IFP2AC(ifp);
163 ASSERT_SERIALIZED(ifp->if_serializer);
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 * Bridges require special output handling.
306 if (ifp->if_bridge) {
307 KASSERT(bridge_output_p != NULL,("ether_input: if_bridge not loaded!"));
308 return ((*bridge_output_p)(ifp, m, NULL, NULL));
312 * If a simplex interface, and the packet is being sent to our
313 * Ethernet address or a broadcast address, loopback a copy.
314 * XXX To make a simplex device behave exactly like a duplex
315 * device, we should copy in the case of sending to our own
316 * ethernet address (thus letting the original actually appear
317 * on the wire). However, we don't do that here for security
318 * reasons and compatibility with the original behavior.
320 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
323 if (m->m_pkthdr.csum_flags & CSUM_IP)
324 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
325 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
326 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
327 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
330 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
331 n->m_pkthdr.csum_flags |= csum_flags;
332 if (csum_flags & CSUM_DATA_VALID)
333 n->m_pkthdr.csum_data = 0xffff;
334 if_simloop(ifp, n, dst->sa_family, hlen);
337 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
338 ETHER_ADDR_LEN) == 0) {
339 m->m_pkthdr.csum_flags |= csum_flags;
340 if (csum_flags & CSUM_DATA_VALID)
341 m->m_pkthdr.csum_data = 0xffff;
342 if_simloop(ifp, m, dst->sa_family, hlen);
343 return (0); /* XXX */
347 /* Handle ng_ether(4) processing, if any */
348 if (ng_ether_output_p != NULL) {
349 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
355 /* Continue with link-layer output */
356 return ether_output_frame(ifp, m);
364 * Ethernet link layer output routine to send a raw frame to the device.
366 * This assumes that the 14 byte Ethernet header is present and contiguous
370 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
372 struct ip_fw *rule = NULL;
374 struct altq_pktattr pktattr;
376 ASSERT_SERIALIZED(ifp->if_serializer);
378 /* Extract info from dummynet tag, ignore others */
379 while (m->m_type == MT_TAG) {
380 if (m->m_flags == PACKET_TAG_DUMMYNET) {
381 rule = ((struct dn_pkt *)m)->rule;
387 if (ifq_is_enabled(&ifp->if_snd))
388 altq_etherclassify(&ifp->if_snd, m, &pktattr);
390 if (IPFW_LOADED && ether_ipfw != 0) {
391 struct ether_header save_eh, *eh;
393 eh = mtod(m, struct ether_header *);
395 m_adj(m, ETHER_HDR_LEN);
396 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
400 return ENOBUFS; /* pkt dropped */
402 return 0; /* consumed e.g. in a pipe */
404 eh = mtod(m, struct ether_header *);
405 /* packet was ok, restore the ethernet header */
406 if ((void *)(eh + 1) == (void *)m->m_data) {
407 m->m_data -= ETHER_HDR_LEN ;
408 m->m_len += ETHER_HDR_LEN ;
409 m->m_pkthdr.len += ETHER_HDR_LEN ;
411 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
412 if (m == NULL) /* nope... */ {
416 bcopy(&save_eh, mtod(m, struct ether_header *),
423 * Queue message on interface, update output statistics if
424 * successful, and start output if interface not yet active.
426 error = ifq_handoff(ifp, m, &pktattr);
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().
440 struct ether_header *eh,
443 struct ether_header save_eh = *eh; /* might be a ptr in m */
444 struct ip_fw_args args;
448 if (*rule != NULL && fw_one_pass)
449 return TRUE; /* dummynet packet, already partially processed */
452 * I need some amount of data to be contiguous, and in case others
453 * need the packet (shared==TRUE), it also better be in the first mbuf.
455 i = min((*m0)->m_pkthdr.len, max_protohdr);
456 if (shared || (*m0)->m_len < i) {
457 *m0 = m_pullup(*m0, i);
462 args.m = *m0; /* the packet we are looking at */
463 args.oif = dst; /* destination, if any */
464 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
465 m_tag_delete(*m0, mtag);
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;
555 ASSERT_SERIALIZED(ifp->if_serializer);
557 /* XXX old crufty stuff, needs to be removed */
559 printf("ether_input got mbuf without embedded ethernet header");
564 if (m->m_len < sizeof(struct ether_header)) {
565 /* XXX error in the caller. */
569 m->m_pkthdr.rcvif = ifp;
570 eh = mtod(m, struct ether_header *);
574 ifp->if_ibytes += m->m_pkthdr.len;
577 * Tap the packet off here for a bridge. bridge_input()
578 * will return NULL if it has consumed the packet, otherwise
579 * it gets processed as normal. Note that bridge_input()
580 * will always return the original packet if we need to
581 * process it locally.
583 if (ifp->if_bridge) {
584 KASSERT(bridge_input_p != NULL,("ether_input: if_bridge not loaded!"));
586 if(m->m_flags & M_PROTO1) {
587 m->m_flags &= ~M_PROTO1;
589 /* clear M_PROMISC, in case the packets comes from a vlan */
590 /* m->m_flags &= ~M_PROMISC; */
591 lwkt_serialize_exit(ifp->if_serializer);
592 m = (*bridge_input_p)(ifp, m);
593 lwkt_serialize_enter(ifp->if_serializer);
598 * Bridge has determined that the packet is for us.
599 * Update our interface pointer -- we may have had
600 * to "bridge" the packet locally.
602 ifp = m->m_pkthdr.rcvif;
606 /* XXX old crufty stuff, needs to be removed */
607 m_adj(m, sizeof(struct ether_header));
609 /* m->m_pkthdr.len = m->m_len; */
611 /* Handle ng_ether(4) processing, if any */
612 if (ng_ether_input_p != NULL) {
613 lwkt_serialize_exit(ifp->if_serializer);
614 (*ng_ether_input_p)(ifp, &m, eh);
615 lwkt_serialize_enter(ifp->if_serializer);
620 /* Continue with upper layer processing */
621 ether_demux(ifp, eh, m);
625 * Upper layer processing for a received Ethernet packet.
628 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
632 struct ip_fw *rule = NULL;
637 /* Extract info from dummynet tag, ignore others */
638 while (m->m_type == MT_TAG) {
639 if (m->m_flags == PACKET_TAG_DUMMYNET) {
640 rule = ((struct dn_pkt *)m)->rule;
641 ifp = m->m_next->m_pkthdr.rcvif;
646 if (rule) /* packet is passing the second time */
650 * Discard packet if upper layers shouldn't see it because
651 * it was unicast to a different Ethernet address. If the
652 * driver is working properly, then this situation can only
653 * happen when the interface is in promiscuous mode.
655 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
656 (eh->ether_dhost[0] & 1) == 0 &&
657 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
661 /* Discard packet if interface is not up */
662 if (!(ifp->if_flags & IFF_UP)) {
666 if (eh->ether_dhost[0] & 1) {
667 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
668 ifp->if_addrlen) == 0)
669 m->m_flags |= M_BCAST;
671 m->m_flags |= M_MCAST;
676 if (IPFW_LOADED && ether_ipfw != 0) {
677 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
681 eh = mtod(m, struct ether_header *);
684 ether_type = ntohs(eh->ether_type);
686 switch (ether_type) {
689 if (ipflow_fastforward(m, ifp->if_serializer))
695 if (ifp->if_flags & IFF_NOARP) {
696 /* Discard packet if ARP is disabled on interface */
712 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
719 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
735 if (vlan_input_p != NULL)
736 (*vlan_input_p)(eh, m);
738 m->m_pkthdr.rcvif->if_noproto++;
745 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
749 checksum = mtod(m, ushort *);
751 if ((ether_type <= ETHERMTU) &&
752 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
753 if (*checksum == 0xE0E0) {
754 m->m_pkthdr.len -= 3;
763 if (ether_type > ETHERMTU)
765 l = mtod(m, struct llc *);
766 if (l->llc_dsap == LLC_SNAP_LSAP &&
767 l->llc_ssap == LLC_SNAP_LSAP &&
768 l->llc_control == LLC_UI) {
769 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
770 sizeof at_org_code) == 0 &&
771 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
772 m_adj(m, sizeof(struct llc));
776 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
777 sizeof aarp_org_code) == 0 &&
778 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
779 m_adj(m, sizeof(struct llc));
786 if (ng_ether_input_orphan_p != NULL)
787 (*ng_ether_input_orphan_p)(ifp, m, eh);
792 netisr_dispatch(isr, m);
796 * Perform common duties while attaching to interface list
800 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
802 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
807 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
808 lwkt_serialize_t serializer)
810 struct sockaddr_dl *sdl;
812 ifp->if_type = IFT_ETHER;
813 ifp->if_addrlen = ETHER_ADDR_LEN;
814 ifp->if_hdrlen = ETHER_HDR_LEN;
815 if_attach(ifp, serializer);
816 ifp->if_mtu = ETHERMTU;
817 if (ifp->if_baudrate == 0)
818 ifp->if_baudrate = 10000000;
819 ifp->if_output = ether_output;
820 ifp->if_input = ether_input_internal;
821 ifp->if_resolvemulti = ether_resolvemulti;
822 ifp->if_broadcastaddr = etherbroadcastaddr;
823 sdl = IF_LLSOCKADDR(ifp);
824 sdl->sdl_type = IFT_ETHER;
825 sdl->sdl_alen = ifp->if_addrlen;
826 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
828 * XXX Keep the current drivers happy.
829 * XXX Remove once all drivers have been cleaned up
831 if (lla != IFP2AC(ifp)->ac_enaddr)
832 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
833 bpfattach(ifp, dlt, hdrlen);
834 if (ng_ether_attach_p != NULL)
835 (*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)
848 if (ng_ether_detach_p != NULL)
849 (*ng_ether_detach_p)(ifp);
855 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
857 struct ifaddr *ifa = (struct ifaddr *) data;
858 struct ifreq *ifr = (struct ifreq *) data;
861 ASSERT_SERIALIZED(ifp->if_serializer);
865 ifp->if_flags |= IFF_UP;
867 switch (ifa->ifa_addr->sa_family) {
870 ifp->if_init(ifp->if_softc); /* before arpwhohas */
871 arp_ifinit(ifp, ifa);
876 * XXX - This code is probably wrong
880 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
881 struct arpcom *ac = IFP2AC(ifp);
883 if (ipx_nullhost(*ina))
884 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
886 bcopy(ina->x_host.c_host, ac->ac_enaddr,
887 sizeof ac->ac_enaddr);
889 ifp->if_init(ifp->if_softc); /* Set new address. */
895 * XXX - This code is probably wrong
899 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
900 struct arpcom *ac = IFP2AC(ifp);
902 if (ns_nullhost(*ina))
903 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
905 bcopy(ina->x_host.c_host, ac->ac_enaddr,
906 sizeof ac->ac_enaddr);
911 ifp->if_init(ifp->if_softc);
916 ifp->if_init(ifp->if_softc);
922 bcopy(IFP2AC(ifp)->ac_enaddr,
923 ((struct sockaddr *)ifr->ifr_data)->sa_data,
929 * Set the interface MTU.
931 if (ifr->ifr_mtu > ETHERMTU) {
934 ifp->if_mtu = ifr->ifr_mtu;
947 struct sockaddr **llsa,
950 struct sockaddr_dl *sdl;
951 struct sockaddr_in *sin;
953 struct sockaddr_in6 *sin6;
957 switch(sa->sa_family) {
960 * No mapping needed. Just check that it's a valid MC address.
962 sdl = (struct sockaddr_dl *)sa;
963 e_addr = LLADDR(sdl);
964 if ((e_addr[0] & 1) != 1)
965 return EADDRNOTAVAIL;
971 sin = (struct sockaddr_in *)sa;
972 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
973 return EADDRNOTAVAIL;
974 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
976 sdl->sdl_len = sizeof *sdl;
977 sdl->sdl_family = AF_LINK;
978 sdl->sdl_index = ifp->if_index;
979 sdl->sdl_type = IFT_ETHER;
980 sdl->sdl_alen = ETHER_ADDR_LEN;
981 e_addr = LLADDR(sdl);
982 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
983 *llsa = (struct sockaddr *)sdl;
988 sin6 = (struct sockaddr_in6 *)sa;
989 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
991 * An IP6 address of 0 means listen to all
992 * of the Ethernet multicast address used for IP6.
993 * (This is used for multicast routers.)
995 ifp->if_flags |= IFF_ALLMULTI;
999 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1000 return EADDRNOTAVAIL;
1001 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1003 sdl->sdl_len = sizeof *sdl;
1004 sdl->sdl_family = AF_LINK;
1005 sdl->sdl_index = ifp->if_index;
1006 sdl->sdl_type = IFT_ETHER;
1007 sdl->sdl_alen = ETHER_ADDR_LEN;
1008 e_addr = LLADDR(sdl);
1009 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1010 *llsa = (struct sockaddr *)sdl;
1016 * Well, the text isn't quite right, but it's the name
1019 return EAFNOSUPPORT;
1025 * This is for reference. We have a table-driven version
1026 * of the little-endian crc32 generator, which is faster
1027 * than the double-loop.
1030 ether_crc32_le(const uint8_t *buf, size_t len)
1032 uint32_t c, crc, carry;
1035 crc = 0xffffffffU; /* initial value */
1037 for (i = 0; i < len; i++) {
1039 for (j = 0; j < 8; j++) {
1040 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1044 crc = (crc ^ ETHER_CRC_POLY_LE);
1052 ether_crc32_le(const uint8_t *buf, size_t len)
1054 static const uint32_t crctab[] = {
1055 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1056 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1057 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1058 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1063 crc = 0xffffffffU; /* initial value */
1065 for (i = 0; i < len; i++) {
1067 crc = (crc >> 4) ^ crctab[crc & 0xf];
1068 crc = (crc >> 4) ^ crctab[crc & 0xf];
1076 ether_crc32_be(const uint8_t *buf, size_t len)
1078 uint32_t c, crc, carry;
1081 crc = 0xffffffffU; /* initial value */
1083 for (i = 0; i < len; i++) {
1085 for (j = 0; j < 8; j++) {
1086 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1090 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1098 * find the size of ethernet header, and call classifier
1101 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1102 struct altq_pktattr *pktattr)
1104 struct ether_header *eh;
1105 uint16_t ether_type;
1106 int hlen, af, hdrsize;
1109 hlen = sizeof(struct ether_header);
1110 eh = mtod(m, struct ether_header *);
1112 ether_type = ntohs(eh->ether_type);
1113 if (ether_type < ETHERMTU) {
1115 struct llc *llc = (struct llc *)(eh + 1);
1118 if (m->m_len < hlen ||
1119 llc->llc_dsap != LLC_SNAP_LSAP ||
1120 llc->llc_ssap != LLC_SNAP_LSAP ||
1121 llc->llc_control != LLC_UI)
1122 goto bad; /* not snap! */
1124 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1127 if (ether_type == ETHERTYPE_IP) {
1129 hdrsize = 20; /* sizeof(struct ip) */
1131 } else if (ether_type == ETHERTYPE_IPV6) {
1133 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1138 while (m->m_len <= hlen) {
1142 hdr = m->m_data + hlen;
1143 if (m->m_len < hlen + hdrsize) {
1145 * ip header is not in a single mbuf. this should not
1146 * happen in the current code.
1147 * (todo: use m_pulldown in the future)
1153 ifq_classify(ifq, m, af, pktattr);
1160 pktattr->pattr_class = NULL;
1161 pktattr->pattr_hdr = NULL;
1162 pktattr->pattr_af = AF_UNSPEC;