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.35 2005/12/21 16:37:15 corecode 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/oldbridge/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 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
129 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
130 struct sockaddr *, struct rtentry *);
131 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
133 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
136 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
140 #define gotoerr(e) do { error = (e); goto bad; } while (0)
141 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
143 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
144 struct ip_fw **rule, struct ether_header *eh,
147 static int ether_ipfw;
148 SYSCTL_DECL(_net_link);
149 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
150 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
151 ðer_ipfw, 0, "Pass ether pkts through firewall");
154 * Ethernet output routine.
155 * Encapsulate a packet of type family for the local net.
156 * Use trailer local net encapsulation if enough data in first
157 * packet leaves a multiple of 512 bytes of data in remainder.
158 * Assumes that ifp is actually pointer to arpcom structure.
161 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
164 struct ether_header *eh, *deh;
167 int hlen = ETHER_HDR_LEN; /* link layer header length */
168 struct arpcom *ac = IFP2AC(ifp);
171 ASSERT_SERIALIZED(ifp->if_serializer);
173 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
176 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
179 eh = mtod(m, struct ether_header *);
180 edst = eh->ether_dhost;
183 * Fill in the destination ethernet address and frame type.
185 switch (dst->sa_family) {
188 if (!arpresolve(ifp, rt, m, dst, edst))
189 return (0); /* if not yet resolved */
190 eh->ether_type = htons(ETHERTYPE_IP);
195 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
196 return (0); /* Something bad happenned. */
197 eh->ether_type = htons(ETHERTYPE_IPV6);
202 if (ef_outputp != NULL) {
203 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
208 eh->ether_type = htons(ETHERTYPE_IPX);
209 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
210 edst, ETHER_ADDR_LEN);
216 struct at_ifaddr *aa;
218 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
223 * In the phase 2 case, need to prepend an mbuf for
224 * the llc header. Since we must preserve the value
225 * of m, which is passed to us by value, we m_copy()
226 * the first mbuf, and use it for our llc header.
228 if (aa->aa_flags & AFA_PHASE2) {
231 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
232 eh = mtod(m, struct ether_header *);
233 edst = eh->ether_dhost;
234 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
235 llc.llc_control = LLC_UI;
236 bcopy(at_org_code, llc.llc_snap_org_code,
238 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
240 mtod(m, caddr_t) + sizeof(struct ether_header),
242 eh->ether_type = htons(m->m_pkthdr.len);
243 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
245 eh->ether_type = htons(ETHERTYPE_AT);
247 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
256 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
257 eh->ether_type = 0x8137;
259 case 0x0: /* Novell 802.3 */
260 eh->ether_type = htons(m->m_pkthdr.len);
262 case 0xe0e0: /* Novell 802.2 and Token-Ring */
263 M_PREPEND(m, 3, MB_DONTWAIT);
264 eh = mtod(m, struct ether_header *);
265 edst = eh->ether_dhost;
266 eh->ether_type = htons(m->m_pkthdr.len);
267 cp = mtod(m, u_char *) + sizeof(struct ether_header);
273 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
276 * XXX if ns_thishost is the same as the node's ethernet
277 * address then just the default code will catch this anyhow.
278 * So I'm not sure if this next clause should be here at all?
281 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
282 m->m_pkthdr.rcvif = ifp;
283 netisr_dispatch(NETISR_NS, m);
286 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
287 m->m_flags |= M_BCAST;
290 case pseudo_AF_HDRCMPLT:
292 loop_copy = -1; /* if this is for us, don't do it */
293 deh = (struct ether_header *)dst->sa_data;
294 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
295 eh->ether_type = deh->ether_type;
299 printf("%s: can't handle af%d\n", ifp->if_xname,
301 gotoerr(EAFNOSUPPORT);
304 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
305 memcpy(eh->ether_shost,
306 ((struct ether_header *)dst->sa_data)->ether_shost,
309 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
312 * Bridges require special output handling.
314 if (ifp->if_bridge) {
315 KASSERT(bridge_output_p != NULL,("ether_input: if_bridge not loaded!"));
316 return ((*bridge_output_p)(ifp, m, NULL, NULL));
320 * If a simplex interface, and the packet is being sent to our
321 * Ethernet address or a broadcast address, loopback a copy.
322 * XXX To make a simplex device behave exactly like a duplex
323 * device, we should copy in the case of sending to our own
324 * ethernet address (thus letting the original actually appear
325 * on the wire). However, we don't do that here for security
326 * reasons and compatibility with the original behavior.
328 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
331 if (m->m_pkthdr.csum_flags & CSUM_IP)
332 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
333 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
334 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
335 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
338 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
339 n->m_pkthdr.csum_flags |= csum_flags;
340 if (csum_flags & CSUM_DATA_VALID)
341 n->m_pkthdr.csum_data = 0xffff;
342 if_simloop(ifp, n, dst->sa_family, hlen);
345 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
346 ETHER_ADDR_LEN) == 0) {
347 m->m_pkthdr.csum_flags |= csum_flags;
348 if (csum_flags & CSUM_DATA_VALID)
349 m->m_pkthdr.csum_data = 0xffff;
350 if_simloop(ifp, m, dst->sa_family, hlen);
351 return (0); /* XXX */
355 /* Handle ng_ether(4) processing, if any */
356 if (ng_ether_output_p != NULL) {
357 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
363 /* Continue with link-layer output */
364 return ether_output_frame(ifp, m);
372 * Ethernet link layer output routine to send a raw frame to the device.
374 * This assumes that the 14 byte Ethernet header is present and contiguous
375 * in the first mbuf (if BRIDGE'ing).
378 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
380 struct ip_fw *rule = NULL;
382 struct altq_pktattr pktattr;
384 ASSERT_SERIALIZED(ifp->if_serializer);
386 /* Extract info from dummynet tag, ignore others */
387 while (m->m_type == MT_TAG) {
388 if (m->m_flags == PACKET_TAG_DUMMYNET) {
389 rule = ((struct dn_pkt *)m)->rule;
394 if (rule != NULL) /* packet was already bridged */
397 if (BDG_ACTIVE(ifp)) {
398 struct ether_header *eh; /* a pointer suffices */
400 m->m_pkthdr.rcvif = NULL;
401 eh = mtod(m, struct ether_header *);
402 m_adj(m, ETHER_HDR_LEN);
403 lwkt_serialize_exit(ifp->if_serializer);
404 m = bdg_forward_ptr(m, eh, ifp);
405 lwkt_serialize_enter(ifp->if_serializer);
411 if (ifq_is_enabled(&ifp->if_snd))
412 altq_etherclassify(&ifp->if_snd, m, &pktattr);
414 if (IPFW_LOADED && ether_ipfw != 0) {
415 struct ether_header save_eh, *eh;
417 eh = mtod(m, struct ether_header *);
419 m_adj(m, ETHER_HDR_LEN);
420 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
424 return ENOBUFS; /* pkt dropped */
426 return 0; /* consumed e.g. in a pipe */
428 eh = mtod(m, struct ether_header *);
429 /* packet was ok, restore the ethernet header */
430 if ((void *)(eh + 1) == (void *)m->m_data) {
431 m->m_data -= ETHER_HDR_LEN ;
432 m->m_len += ETHER_HDR_LEN ;
433 m->m_pkthdr.len += ETHER_HDR_LEN ;
435 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
436 if (m == NULL) /* nope... */ {
440 bcopy(&save_eh, mtod(m, struct ether_header *),
447 * Queue message on interface, update output statistics if
448 * successful, and start output if interface not yet active.
450 error = ifq_handoff(ifp, m, &pktattr);
455 * ipfw processing for ethernet packets (in and out).
456 * The second parameter is NULL from ether_demux(), and ifp from
457 * ether_output_frame(). This section of code could be used from
458 * bridge.c as well as long as we use some extra info
459 * to distinguish that case from ether_output_frame().
466 struct ether_header *eh,
469 struct ether_header save_eh = *eh; /* might be a ptr in m */
470 struct ip_fw_args args;
474 if (*rule != NULL && fw_one_pass)
475 return TRUE; /* dummynet packet, already partially processed */
478 * I need some amount of data to be contiguous, and in case others
479 * need the packet (shared==TRUE), it also better be in the first mbuf.
481 i = min((*m0)->m_pkthdr.len, max_protohdr);
482 if (shared || (*m0)->m_len < i) {
483 *m0 = m_pullup(*m0, i);
488 args.m = *m0; /* the packet we are looking at */
489 args.oif = dst; /* destination, if any */
490 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
491 m_tag_delete(*m0, mtag);
492 args.rule = *rule; /* matching rule to restart */
493 args.next_hop = NULL; /* we do not support forward yet */
494 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
495 i = ip_fw_chk_ptr(&args);
499 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
502 if (i == 0) /* a PASS rule. */
505 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
507 * Pass the pkt to dummynet, which consumes it.
508 * If shared, make a copy and keep the original.
513 m = m_copypacket(*m0, MB_DONTWAIT);
517 m = *m0 ; /* pass the original to dummynet */
518 *m0 = NULL ; /* and nothing back to the caller */
521 * Prepend the header, optimize for the common case of
522 * eh pointing into the mbuf.
524 if ((void *)(eh + 1) == (void *)m->m_data) {
525 m->m_data -= ETHER_HDR_LEN ;
526 m->m_len += ETHER_HDR_LEN ;
527 m->m_pkthdr.len += ETHER_HDR_LEN ;
529 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
532 bcopy(&save_eh, mtod(m, struct ether_header *),
535 ip_dn_io_ptr(m, (i & 0xffff),
536 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
540 * XXX at some point add support for divert/forward actions.
541 * If none of the above matches, we have to drop the pkt.
547 * XXX merge this function with ether_input.
550 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
552 ether_input(ifp, NULL, m);
556 * Process a received Ethernet packet. We have two different interfaces:
557 * one (conventional) assumes the packet in the mbuf, with the ethernet
558 * header provided separately in *eh. The second one (new) has everything
559 * in the mbuf, and we can tell it because eh == NULL.
560 * The caller MUST MAKE SURE that there are at least
561 * sizeof(struct ether_header) bytes in the first mbuf.
563 * This allows us to concentrate in one place a bunch of code which
564 * is replicated in all device drivers. Also, many functions called
565 * from ether_input() try to put the eh back into the mbuf, so we
566 * can later propagate the 'contiguous packet' interface to them,
567 * and handle the old interface just here.
569 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
570 * cluster, right before m_data. So be very careful when working on m,
571 * as you could destroy *eh !!
573 * First we perform any link layer operations, then continue
574 * to the upper layers with ether_demux().
577 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
579 struct ether_header save_eh;
581 ASSERT_SERIALIZED(ifp->if_serializer);
583 /* XXX old crufty stuff, needs to be removed */
585 printf("ether_input got mbuf without embedded ethernet header");
590 if (m->m_len < sizeof(struct ether_header)) {
591 /* XXX error in the caller. */
595 m->m_pkthdr.rcvif = ifp;
596 eh = mtod(m, struct ether_header *);
600 ifp->if_ibytes += m->m_pkthdr.len;
603 * Tap the packet off here for a bridge. bridge_input()
604 * will return NULL if it has consumed the packet, otherwise
605 * it gets processed as normal. Note that bridge_input()
606 * will always return the original packet if we need to
607 * process it locally.
609 if (ifp->if_bridge) {
610 KASSERT(bridge_input_p != NULL,("ether_input: if_bridge not loaded!"));
612 if(m->m_flags & M_PROTO1) {
613 m->m_flags &= ~M_PROTO1;
615 /* clear M_PROMISC, in case the packets comes from a vlan */
616 /* m->m_flags &= ~M_PROMISC; */
617 lwkt_serialize_exit(ifp->if_serializer);
618 m = (*bridge_input_p)(ifp, m);
619 lwkt_serialize_enter(ifp->if_serializer);
624 * Bridge has determined that the packet is for us.
625 * Update our interface pointer -- we may have had
626 * to "bridge" the packet locally.
628 ifp = m->m_pkthdr.rcvif;
632 /* XXX old crufty stuff, needs to be removed */
633 m_adj(m, sizeof(struct ether_header));
635 /* m->m_pkthdr.len = m->m_len; */
637 /* Handle ng_ether(4) processing, if any */
638 if (ng_ether_input_p != NULL) {
639 lwkt_serialize_exit(ifp->if_serializer);
640 (*ng_ether_input_p)(ifp, &m, eh);
641 lwkt_serialize_enter(ifp->if_serializer);
646 /* Check for bridging mode */
647 if (BDG_ACTIVE(ifp)) {
650 /* Check with bridging code */
651 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
655 if (bif != BDG_LOCAL) {
656 save_eh = *eh ; /* because it might change */
657 lwkt_serialize_exit(ifp->if_serializer);
658 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
659 lwkt_serialize_enter(ifp->if_serializer);
661 * Do not continue if bdg_forward_ptr() processed our
662 * packet (and cleared the mbuf pointer m) or if
663 * it dropped (m_free'd) the packet itself.
666 if (bif == BDG_BCAST || bif == BDG_MCAST)
667 printf("bdg_forward drop MULTICAST PKT\n");
672 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST)
673 goto recvLocal; /* receive locally */
675 /* If not local and not multicast, just drop it */
681 /* Continue with upper layer processing */
682 ether_demux(ifp, eh, m);
686 * Upper layer processing for a received Ethernet packet.
689 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
693 struct ip_fw *rule = NULL;
698 /* Extract info from dummynet tag, ignore others */
699 while (m->m_type == MT_TAG) {
700 if (m->m_flags == PACKET_TAG_DUMMYNET) {
701 rule = ((struct dn_pkt *)m)->rule;
702 ifp = m->m_next->m_pkthdr.rcvif;
707 if (rule) /* packet was already bridged */
711 * Discard packet if upper layers shouldn't see it because
712 * it was unicast to a different Ethernet address. If the
713 * driver is working properly, then this situation can only
714 * happen when the interface is in promiscuous mode.
716 if (!BDG_ACTIVE(ifp) &&
717 ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
718 (eh->ether_dhost[0] & 1) == 0 &&
719 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
723 /* Discard packet if interface is not up */
724 if (!(ifp->if_flags & IFF_UP)) {
728 if (eh->ether_dhost[0] & 1) {
729 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
730 ifp->if_addrlen) == 0)
731 m->m_flags |= M_BCAST;
733 m->m_flags |= M_MCAST;
738 if (IPFW_LOADED && ether_ipfw != 0) {
739 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
743 eh = mtod(m, struct ether_header *);
746 ether_type = ntohs(eh->ether_type);
748 switch (ether_type) {
751 if (ipflow_fastforward(m, ifp->if_serializer))
757 if (ifp->if_flags & IFF_NOARP) {
758 /* Discard packet if ARP is disabled on interface */
774 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
781 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
797 if (vlan_input_p != NULL)
798 (*vlan_input_p)(eh, m);
800 m->m_pkthdr.rcvif->if_noproto++;
807 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
811 checksum = mtod(m, ushort *);
813 if ((ether_type <= ETHERMTU) &&
814 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
815 if (*checksum == 0xE0E0) {
816 m->m_pkthdr.len -= 3;
825 if (ether_type > ETHERMTU)
827 l = mtod(m, struct llc *);
828 if (l->llc_dsap == LLC_SNAP_LSAP &&
829 l->llc_ssap == LLC_SNAP_LSAP &&
830 l->llc_control == LLC_UI) {
831 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
832 sizeof at_org_code) == 0 &&
833 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
834 m_adj(m, sizeof(struct llc));
838 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
839 sizeof aarp_org_code) == 0 &&
840 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
841 m_adj(m, sizeof(struct llc));
848 if (ng_ether_input_orphan_p != NULL)
849 (*ng_ether_input_orphan_p)(ifp, m, eh);
854 netisr_dispatch(isr, m);
858 * Perform common duties while attaching to interface list
862 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
864 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
869 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
870 lwkt_serialize_t serializer)
872 struct sockaddr_dl *sdl;
874 ifp->if_type = IFT_ETHER;
875 ifp->if_addrlen = ETHER_ADDR_LEN;
876 ifp->if_hdrlen = ETHER_HDR_LEN;
877 if_attach(ifp, serializer);
878 ifp->if_mtu = ETHERMTU;
879 if (ifp->if_baudrate == 0)
880 ifp->if_baudrate = 10000000;
881 ifp->if_output = ether_output;
882 ifp->if_input = ether_input_internal;
883 ifp->if_resolvemulti = ether_resolvemulti;
884 ifp->if_broadcastaddr = etherbroadcastaddr;
885 sdl = IF_LLSOCKADDR(ifp);
886 sdl->sdl_type = IFT_ETHER;
887 sdl->sdl_alen = ifp->if_addrlen;
888 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
890 * XXX Keep the current drivers happy.
891 * XXX Remove once all drivers have been cleaned up
893 if (lla != IFP2AC(ifp)->ac_enaddr)
894 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
895 bpfattach(ifp, dlt, hdrlen);
896 if (ng_ether_attach_p != NULL)
897 (*ng_ether_attach_p)(ifp);
901 if_printf(ifp, "MAC address: %6D\n", lla, ":");
905 * Perform common duties while detaching an Ethernet interface
908 ether_ifdetach(struct ifnet *ifp)
912 if (ng_ether_detach_p != NULL)
913 (*ng_ether_detach_p)(ifp);
921 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
923 struct ifaddr *ifa = (struct ifaddr *) data;
924 struct ifreq *ifr = (struct ifreq *) data;
927 ASSERT_SERIALIZED(ifp->if_serializer);
931 ifp->if_flags |= IFF_UP;
933 switch (ifa->ifa_addr->sa_family) {
936 ifp->if_init(ifp->if_softc); /* before arpwhohas */
937 arp_ifinit(ifp, ifa);
942 * XXX - This code is probably wrong
946 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
947 struct arpcom *ac = IFP2AC(ifp);
949 if (ipx_nullhost(*ina))
950 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
952 bcopy(ina->x_host.c_host, ac->ac_enaddr,
953 sizeof ac->ac_enaddr);
955 ifp->if_init(ifp->if_softc); /* Set new address. */
961 * XXX - This code is probably wrong
965 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
966 struct arpcom *ac = IFP2AC(ifp);
968 if (ns_nullhost(*ina))
969 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
971 bcopy(ina->x_host.c_host, ac->ac_enaddr,
972 sizeof ac->ac_enaddr);
977 ifp->if_init(ifp->if_softc);
982 ifp->if_init(ifp->if_softc);
988 bcopy(IFP2AC(ifp)->ac_enaddr,
989 ((struct sockaddr *)ifr->ifr_data)->sa_data,
995 * Set the interface MTU.
997 if (ifr->ifr_mtu > ETHERMTU) {
1000 ifp->if_mtu = ifr->ifr_mtu;
1013 struct sockaddr **llsa,
1014 struct sockaddr *sa)
1016 struct sockaddr_dl *sdl;
1017 struct sockaddr_in *sin;
1019 struct sockaddr_in6 *sin6;
1023 switch(sa->sa_family) {
1026 * No mapping needed. Just check that it's a valid MC address.
1028 sdl = (struct sockaddr_dl *)sa;
1029 e_addr = LLADDR(sdl);
1030 if ((e_addr[0] & 1) != 1)
1031 return EADDRNOTAVAIL;
1037 sin = (struct sockaddr_in *)sa;
1038 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1039 return EADDRNOTAVAIL;
1040 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1042 sdl->sdl_len = sizeof *sdl;
1043 sdl->sdl_family = AF_LINK;
1044 sdl->sdl_index = ifp->if_index;
1045 sdl->sdl_type = IFT_ETHER;
1046 sdl->sdl_alen = ETHER_ADDR_LEN;
1047 e_addr = LLADDR(sdl);
1048 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1049 *llsa = (struct sockaddr *)sdl;
1054 sin6 = (struct sockaddr_in6 *)sa;
1055 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1057 * An IP6 address of 0 means listen to all
1058 * of the Ethernet multicast address used for IP6.
1059 * (This is used for multicast routers.)
1061 ifp->if_flags |= IFF_ALLMULTI;
1065 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1066 return EADDRNOTAVAIL;
1067 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1069 sdl->sdl_len = sizeof *sdl;
1070 sdl->sdl_family = AF_LINK;
1071 sdl->sdl_index = ifp->if_index;
1072 sdl->sdl_type = IFT_ETHER;
1073 sdl->sdl_alen = ETHER_ADDR_LEN;
1074 e_addr = LLADDR(sdl);
1075 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1076 *llsa = (struct sockaddr *)sdl;
1082 * Well, the text isn't quite right, but it's the name
1085 return EAFNOSUPPORT;
1091 * This is for reference. We have a table-driven version
1092 * of the little-endian crc32 generator, which is faster
1093 * than the double-loop.
1096 ether_crc32_le(const uint8_t *buf, size_t len)
1098 uint32_t c, crc, carry;
1101 crc = 0xffffffffU; /* initial value */
1103 for (i = 0; i < len; i++) {
1105 for (j = 0; j < 8; j++) {
1106 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1110 crc = (crc ^ ETHER_CRC_POLY_LE);
1118 ether_crc32_le(const uint8_t *buf, size_t len)
1120 static const uint32_t crctab[] = {
1121 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1122 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1123 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1124 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1129 crc = 0xffffffffU; /* initial value */
1131 for (i = 0; i < len; i++) {
1133 crc = (crc >> 4) ^ crctab[crc & 0xf];
1134 crc = (crc >> 4) ^ crctab[crc & 0xf];
1142 ether_crc32_be(const uint8_t *buf, size_t len)
1144 uint32_t c, crc, carry;
1147 crc = 0xffffffffU; /* initial value */
1149 for (i = 0; i < len; i++) {
1151 for (j = 0; j < 8; j++) {
1152 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1156 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1164 * find the size of ethernet header, and call classifier
1167 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1168 struct altq_pktattr *pktattr)
1170 struct ether_header *eh;
1171 uint16_t ether_type;
1172 int hlen, af, hdrsize;
1175 hlen = sizeof(struct ether_header);
1176 eh = mtod(m, struct ether_header *);
1178 ether_type = ntohs(eh->ether_type);
1179 if (ether_type < ETHERMTU) {
1181 struct llc *llc = (struct llc *)(eh + 1);
1184 if (m->m_len < hlen ||
1185 llc->llc_dsap != LLC_SNAP_LSAP ||
1186 llc->llc_ssap != LLC_SNAP_LSAP ||
1187 llc->llc_control != LLC_UI)
1188 goto bad; /* not snap! */
1190 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1193 if (ether_type == ETHERTYPE_IP) {
1195 hdrsize = 20; /* sizeof(struct ip) */
1197 } else if (ether_type == ETHERTYPE_IPV6) {
1199 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1204 while (m->m_len <= hlen) {
1208 hdr = m->m_data + hlen;
1209 if (m->m_len < hlen + hdrsize) {
1211 * ip header is not in a single mbuf. this should not
1212 * happen in the current code.
1213 * (todo: use m_pulldown in the future)
1219 ifq_classify(ifq, m, af, pktattr);
1226 pktattr->pattr_class = NULL;
1227 pktattr->pattr_hdr = NULL;
1228 pktattr->pattr_af = AF_UNSPEC;