1 /* $NetBSD: if_ethersubr.c,v 1.174 2009/11/28 02:58:21 isaki Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1982, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.174 2009/11/28 02:58:21 isaki Exp $");
67 #include "opt_atalk.h"
70 #include "opt_mbuftrace.h"
71 #include "opt_gateway.h"
72 #include "opt_pfil_hooks.h"
73 #include "opt_pppoe.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/callout.h>
85 #include <sys/malloc.h>
87 #include <sys/protosw.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
90 #include <sys/errno.h>
91 #include <sys/syslog.h>
92 #include <sys/kauth.h>
95 #include <sys/device.h>
98 #include <net/netisr.h>
99 #include <net/route.h>
100 #include <net/if_llc.h>
101 #include <net/if_dl.h>
102 #include <net/if_types.h>
104 #include <net/if_media.h>
105 #include <dev/mii/mii.h>
106 #include <dev/mii/miivar.h>
110 * XXX there should really be a way to issue this warning from within config(8)
112 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
119 #include <net/if_ether.h>
121 #include <net/if_vlanvar.h>
125 #include <net/if_pppoe.h>
129 #include <net/agr/ieee8023_slowprotocols.h> /* XXX */
130 #include <net/agr/ieee8023ad.h>
131 #include <net/agr/if_agrvar.h>
135 #include <net/if_bridgevar.h>
138 #include <netinet/in.h>
140 #include <netinet/in_var.h>
142 #include <netinet/if_inarp.h>
146 #include <netinet/in.h>
148 #include <netinet6/in6_var.h>
149 #include <netinet6/nd6.h>
155 #include <netinet/ip_carp.h>
159 #include <netipx/ipx.h>
160 #include <netipx/ipx_if.h>
164 #include <netiso/argo_debug.h>
165 #include <netiso/iso.h>
166 #include <netiso/iso_var.h>
167 #include <netiso/iso_snpac.h>
173 #include <netatalk/at.h>
174 #include <netatalk/at_var.h>
175 #include <netatalk/at_extern.h>
177 #define llc_snap_org_code llc_un.type_snap.org_code
178 #define llc_snap_ether_type llc_un.type_snap.ether_type
180 extern u_char at_org_code
[3];
181 extern u_char aarp_org_code
[3];
182 #endif /* NETATALK */
184 static struct timeval bigpktppslim_last
;
185 static int bigpktppslim
= 2; /* XXX */
186 static int bigpktpps_count
;
189 const uint8_t etherbroadcastaddr
[ETHER_ADDR_LEN
] =
190 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
191 const uint8_t ethermulticastaddr_slowprotocols
[ETHER_ADDR_LEN
] =
192 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
193 #define senderr(e) { error = (e); goto bad;}
195 static int ether_output(struct ifnet
*, struct mbuf
*,
196 const struct sockaddr
*, struct rtentry
*);
199 * Ethernet output routine.
200 * Encapsulate a packet of type family for the local net.
201 * Assumes that ifp is actually pointer to ethercom structure.
204 ether_output(struct ifnet
*ifp0
, struct mbuf
*m0
, const struct sockaddr
*dst
,
208 int error
= 0, hdrcmplt
= 0;
209 uint8_t esrc
[6], edst
[6];
212 struct mbuf
*mcopy
= NULL
;
213 struct ether_header
*eh
;
214 struct ifnet
*ifp
= ifp0
;
215 ALTQ_DECL(struct altq_pktattr pktattr
;)
220 struct at_ifaddr
*aa
;
221 #endif /* NETATALK */
224 m_claimm(m
, ifp
->if_mowner
);
228 if (ifp
->if_type
== IFT_CARP
) {
231 /* loop back if this is going to the carp interface */
232 if (dst
!= NULL
&& ifp0
->if_link_state
== LINK_STATE_UP
&&
233 (ifa
= ifa_ifwithaddr(dst
)) != NULL
&&
234 ifa
->ifa_ifp
== ifp0
)
235 return looutput(ifp0
, m
, dst
, rt0
);
237 ifp
= ifp
->if_carpdev
;
238 /* ac = (struct arpcom *)ifp; */
240 if ((ifp0
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
241 (IFF_UP
|IFF_RUNNING
))
244 #endif /* NCARP > 0 */
246 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
248 if ((rt
= rt0
) != NULL
) {
249 if ((rt
->rt_flags
& RTF_UP
) == 0) {
250 if ((rt0
= rt
= rtalloc1(dst
, 1)) != NULL
) {
252 if (rt
->rt_ifp
!= ifp
)
253 return (*rt
->rt_ifp
->if_output
)
256 senderr(EHOSTUNREACH
);
258 if ((rt
->rt_flags
& RTF_GATEWAY
) && dst
->sa_family
!= AF_NS
) {
259 if (rt
->rt_gwroute
== NULL
)
261 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
262 rtfree(rt
); rt
= rt0
;
263 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1);
264 if ((rt
= rt
->rt_gwroute
) == NULL
)
265 senderr(EHOSTUNREACH
);
266 /* the "G" test below also prevents rt == rt0 */
267 if ((rt
->rt_flags
& RTF_GATEWAY
) ||
268 (rt
->rt_ifp
!= ifp
)) {
270 rt0
->rt_gwroute
= NULL
;
271 senderr(EHOSTUNREACH
);
275 if (rt
->rt_flags
& RTF_REJECT
)
276 if (rt
->rt_rmx
.rmx_expire
== 0 ||
277 (u_long
) time_second
< rt
->rt_rmx
.rmx_expire
)
278 senderr(rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
281 switch (dst
->sa_family
) {
285 if (m
->m_flags
& M_BCAST
)
286 (void)memcpy(edst
, etherbroadcastaddr
, sizeof(edst
));
287 else if (m
->m_flags
& M_MCAST
)
288 ETHER_MAP_IP_MULTICAST(&satocsin(dst
)->sin_addr
, edst
);
289 else if (!arpresolve(ifp
, rt
, m
, dst
, edst
))
290 return (0); /* if not yet resolved */
291 /* If broadcasting on a simplex interface, loopback a copy */
292 if ((m
->m_flags
& M_BCAST
) && (ifp
->if_flags
& IFF_SIMPLEX
))
293 mcopy
= m_copy(m
, 0, (int)M_COPYALL
);
294 etype
= htons(ETHERTYPE_IP
);
298 ah
= mtod(m
, struct arphdr
*);
299 if (m
->m_flags
& M_BCAST
)
300 (void)memcpy(edst
, etherbroadcastaddr
, sizeof(edst
));
302 void *tha
= ar_tha(ah
);
305 /* fake with ARPHDR_IEEE1394 */
308 memcpy(edst
, tha
, sizeof(edst
));
311 ah
->ar_hrd
= htons(ARPHRD_ETHER
);
313 switch (ntohs(ah
->ar_op
)) {
314 case ARPOP_REVREQUEST
:
316 etype
= htons(ETHERTYPE_REVARP
);
322 etype
= htons(ETHERTYPE_ARP
);
329 if (!nd6_storelladdr(ifp
, rt
, m
, dst
, edst
, sizeof(edst
))){
330 /* something bad happened */
333 etype
= htons(ETHERTYPE_IPV6
);
338 if (!aarpresolve(ifp
, m
, (const struct sockaddr_at
*)dst
, edst
)) {
340 printf("aarpresolv failed\n");
341 #endif /* NETATALKDEBUG */
345 * ifaddr is the first thing in at_ifaddr
347 aa
= (struct at_ifaddr
*) at_ifawithnet(
348 (const struct sockaddr_at
*)dst
, ifp
);
353 * In the phase 2 case, we need to prepend an mbuf for the
354 * llc header. Since we must preserve the value of m,
355 * which is passed to us by value, we m_copy() the first
356 * mbuf, and use it for our llc header.
358 if (aa
->aa_flags
& AFA_PHASE2
) {
361 M_PREPEND(m
, sizeof(struct llc
), M_DONTWAIT
);
362 llc
.llc_dsap
= llc
.llc_ssap
= LLC_SNAP_LSAP
;
363 llc
.llc_control
= LLC_UI
;
364 memcpy(llc
.llc_snap_org_code
, at_org_code
,
365 sizeof(llc
.llc_snap_org_code
));
366 llc
.llc_snap_ether_type
= htons(ETHERTYPE_ATALK
);
367 memcpy(mtod(m
, void *), &llc
, sizeof(struct llc
));
369 etype
= htons(ETHERTYPE_ATALK
);
372 #endif /* NETATALK */
375 etype
= htons(ETHERTYPE_IPX
);
377 &(((const struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
379 /* If broadcasting on a simplex interface, loopback a copy */
380 if ((m
->m_flags
& M_BCAST
) && (ifp
->if_flags
& IFF_SIMPLEX
))
381 mcopy
= m_copy(m
, 0, (int)M_COPYALL
);
388 const struct sockaddr_dl
*sdl
;
390 if (rt
&& (sdl
= satocsdl(rt
->rt_gateway
)) &&
391 sdl
->sdl_family
== AF_LINK
&& sdl
->sdl_alen
> 0) {
392 memcpy(edst
, CLLADDR(sdl
), sizeof(edst
));
394 error
= iso_snparesolve(ifp
,
395 (const struct sockaddr_iso
*)dst
,
396 (char *)edst
, &snpalen
);
398 goto bad
; /* Not Resolved */
400 /* If broadcasting on a simplex interface, loopback a copy */
402 m
->m_flags
|= (M_BCAST
|M_MCAST
);
403 if ((m
->m_flags
& M_BCAST
) && (ifp
->if_flags
& IFF_SIMPLEX
) &&
404 (mcopy
= m_copy(m
, 0, (int)M_COPYALL
))) {
405 M_PREPEND(mcopy
, sizeof (*eh
), M_DONTWAIT
);
407 eh
= mtod(mcopy
, struct ether_header
*);
408 memcpy(eh
->ether_dhost
, edst
, sizeof(edst
));
409 memcpy(eh
->ether_shost
, CLLADDR(ifp
->if_sadl
),
413 M_PREPEND(m
, 3, M_DONTWAIT
);
416 l
= mtod(m
, struct llc
*);
417 l
->llc_dsap
= l
->llc_ssap
= LLC_ISO_LSAP
;
418 l
->llc_control
= LLC_UI
;
420 if (argo_debug
[D_ETHER
]) {
422 printf("unoutput: sending pkt to: ");
424 printf("%x ", edst
[i
] & 0xff);
431 case pseudo_AF_HDRCMPLT
:
434 ((const struct ether_header
*)dst
->sa_data
)->ether_shost
,
440 ((const struct ether_header
*)dst
->sa_data
)->ether_dhost
,
442 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
443 etype
= ((const struct ether_header
*)dst
->sa_data
)->ether_type
;
447 printf("%s: can't handle af%d\n", ifp
->if_xname
,
449 senderr(EAFNOSUPPORT
);
453 (void)looutput(ifp
, mcopy
, dst
, rt
);
455 /* If no ether type is set, this must be a 802.2 formatted packet.
458 etype
= htons(m
->m_pkthdr
.len
);
460 * Add local net header. If no space in first mbuf,
463 M_PREPEND(m
, sizeof (struct ether_header
), M_DONTWAIT
);
466 eh
= mtod(m
, struct ether_header
*);
467 /* Note: etype is already in network byte order. */
468 (void)memcpy(&eh
->ether_type
, &etype
, sizeof(eh
->ether_type
));
469 memcpy(eh
->ether_dhost
, edst
, sizeof(edst
));
471 memcpy(eh
->ether_shost
, esrc
, sizeof(eh
->ether_shost
));
473 memcpy(eh
->ether_shost
, CLLADDR(ifp
->if_sadl
),
474 sizeof(eh
->ether_shost
));
477 if (ifp0
!= ifp
&& ifp0
->if_type
== IFT_CARP
) {
478 memcpy(eh
->ether_shost
, CLLADDR(ifp0
->if_sadl
),
479 sizeof(eh
->ether_shost
));
481 #endif /* NCARP > 0 */
484 if ((error
= pfil_run_hooks(&ifp
->if_pfil
, &m
, ifp
, PFIL_OUT
)) != 0)
492 * Bridges require special output handling.
495 return (bridge_output(ifp
, m
, NULL
, NULL
));
500 ifp0
->if_obytes
+= m
->m_pkthdr
.len
+ ETHER_HDR_LEN
;
501 #endif /* NCARP > 0 */
505 * If ALTQ is enabled on the parent interface, do
506 * classification; the queueing discipline might not
507 * require classification, but might require the
508 * address family/header pointer in the pktattr.
510 if (ALTQ_IS_ENABLED(&ifp
->if_snd
))
511 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
514 return ifq_enqueue(ifp
, m ALTQ_COMMA
ALTQ_DECL(&pktattr
));
524 * This routine is a slight hack to allow a packet to be classified
525 * if the Ethernet headers are present. It will go away when ALTQ's
526 * classification engine understands link headers.
529 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
530 struct altq_pktattr
*pktattr
)
532 struct ether_header
*eh
;
534 int hlen
, af
, hdrsize
;
537 hlen
= ETHER_HDR_LEN
;
538 eh
= mtod(m
, struct ether_header
*);
540 ether_type
= htons(eh
->ether_type
);
542 if (ether_type
< ETHERMTU
) {
544 struct llc
*llc
= (struct llc
*)(eh
+ 1);
547 if (m
->m_len
< hlen
||
548 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
549 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
550 llc
->llc_control
!= LLC_UI
) {
555 ether_type
= htons(llc
->llc_un
.type_snap
.ether_type
);
558 switch (ether_type
) {
561 hdrsize
= 20; /* sizeof(struct ip) */
566 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
575 while (m
->m_len
<= hlen
) {
579 if (m
->m_len
< (hlen
+ hdrsize
)) {
581 * protocol header not in a single mbuf.
582 * We can't cope with this situation right
583 * now (but it shouldn't ever happen, really, anyhow).
586 printf("altq_etherclassify: headers span multiple mbufs: "
587 "%d < %d\n", m
->m_len
, (hlen
+ hdrsize
));
595 hdr
= mtod(m
, void *);
597 if (ALTQ_NEEDS_CLASSIFY(ifq
))
598 pktattr
->pattr_class
=
599 (*ifq
->altq_classify
)(ifq
->altq_clfier
, m
, af
);
600 pktattr
->pattr_af
= af
;
601 pktattr
->pattr_hdr
= hdr
;
609 pktattr
->pattr_class
= NULL
;
610 pktattr
->pattr_hdr
= NULL
;
611 pktattr
->pattr_af
= AF_UNSPEC
;
616 * Process a received Ethernet packet;
617 * the packet is in the mbuf chain m with
621 ether_input(struct ifnet
*ifp
, struct mbuf
*m
)
623 struct ethercom
*ec
= (struct ethercom
*) ifp
;
626 struct ether_header
*eh
;
627 #if defined (ISO) || defined (LLC) || defined(NETATALK)
631 if ((ifp
->if_flags
& IFF_UP
) == 0) {
637 m_claimm(m
, &ec
->ec_rx_mowner
);
639 eh
= mtod(m
, struct ether_header
*);
640 etype
= ntohs(eh
->ether_type
);
643 * Determine if the packet is within its size limits.
645 if (m
->m_pkthdr
.len
>
646 ETHER_MAX_FRAME(ifp
, etype
, m
->m_flags
& M_HASFCS
)) {
647 if (ppsratecheck(&bigpktppslim_last
, &bigpktpps_count
,
649 printf("%s: discarding oversize frame (len=%d)\n",
650 ifp
->if_xname
, m
->m_pkthdr
.len
);
656 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
658 * If this is not a simplex interface, drop the packet
659 * if it came from us.
661 if ((ifp
->if_flags
& IFF_SIMPLEX
) == 0 &&
662 memcmp(CLLADDR(ifp
->if_sadl
), eh
->ether_shost
,
663 ETHER_ADDR_LEN
) == 0) {
668 if (memcmp(etherbroadcastaddr
,
669 eh
->ether_dhost
, ETHER_ADDR_LEN
) == 0)
670 m
->m_flags
|= M_BCAST
;
672 m
->m_flags
|= M_MCAST
;
676 /* If the CRC is still on the packet, trim it off. */
677 if (m
->m_flags
& M_HASFCS
) {
678 m_adj(m
, -ETHER_CRC_LEN
);
679 m
->m_flags
&= ~M_HASFCS
;
682 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
686 * Tap the packet off here for a bridge. bridge_input()
687 * will return NULL if it has consumed the packet, otherwise
688 * it gets processed as normal. Note that bridge_input()
689 * will always return the original packet if we need to
690 * process it locally.
692 if (ifp
->if_bridge
) {
693 /* clear M_PROMISC, in case the packets comes from a vlan */
694 m
->m_flags
&= ~M_PROMISC
;
695 m
= bridge_input(ifp
, m
);
700 * Bridge has determined that the packet is for us.
701 * Update our interface pointer -- we may have had
702 * to "bridge" the packet locally.
704 ifp
= m
->m_pkthdr
.rcvif
;
706 #endif /* NBRIDGE > 0 */
710 if (__predict_false(ifp
->if_carp
&& ifp
->if_type
!= IFT_CARP
)) {
712 * clear M_PROMISC, in case the packets comes from a
715 m
->m_flags
&= ~M_PROMISC
;
716 if (carp_input(m
, (uint8_t *)&eh
->ether_shost
,
717 (uint8_t *)&eh
->ether_dhost
, eh
->ether_type
) == 0)
720 #endif /* NCARP > 0 */
721 if ((m
->m_flags
& (M_BCAST
|M_MCAST
|M_PROMISC
)) == 0 &&
722 (ifp
->if_flags
& IFF_PROMISC
) != 0 &&
723 memcmp(CLLADDR(ifp
->if_sadl
), eh
->ether_dhost
,
724 ETHER_ADDR_LEN
) != 0) {
725 m
->m_flags
|= M_PROMISC
;
730 if ((m
->m_flags
& M_PROMISC
) == 0) {
731 if (pfil_run_hooks(&ifp
->if_pfil
, &m
, ifp
, PFIL_IN
) != 0)
736 eh
= mtod(m
, struct ether_header
*);
737 etype
= ntohs(eh
->ether_type
);
742 if (ifp
->if_agrprivate
&&
743 __predict_true(etype
!= ETHERTYPE_SLOWPROTOCOLS
)) {
744 m
->m_flags
&= ~M_PROMISC
;
748 #endif /* NAGR > 0 */
751 * If VLANs are configured on the interface, check to
752 * see if the device performed the decapsulation and
753 * provided us with the tag.
755 if (ec
->ec_nvlans
&& m_tag_find(m
, PACKET_TAG_VLAN
, NULL
) != NULL
) {
758 * vlan_input() will either recursively call ether_input()
759 * or drop the packet.
769 * Handle protocols that expect to have the Ethernet header
770 * (and possibly FCS) intact.
776 * vlan_input() will either recursively call ether_input()
777 * or drop the packet.
779 if (((struct ethercom
*)ifp
)->ec_nvlans
!= 0)
784 #endif /* NVLAN > 0 */
786 case ETHERTYPE_PPPOEDISC
:
787 case ETHERTYPE_PPPOE
:
788 if (m
->m_flags
& M_PROMISC
) {
793 if (m
->m_flags
& (M_MCAST
| M_BCAST
)) {
799 if (etype
== ETHERTYPE_PPPOEDISC
)
808 softint_schedule(pppoe_softintr
);
810 #endif /* NPPPOE > 0 */
811 case ETHERTYPE_SLOWPROTOCOLS
: {
814 #if defined(DIAGNOSTIC)
815 if (m
->m_pkthdr
.len
< sizeof(*eh
) + sizeof(subtype
)) {
816 panic("ether_input: too short slow protocol packet");
819 m_copydata(m
, sizeof(*eh
), sizeof(subtype
), &subtype
);
822 case SLOWPROTOCOLS_SUBTYPE_LACP
:
823 if (ifp
->if_agrprivate
) {
824 ieee8023ad_lacp_input(ifp
, m
);
829 case SLOWPROTOCOLS_SUBTYPE_MARKER
:
830 if (ifp
->if_agrprivate
) {
831 ieee8023ad_marker_input(ifp
, m
);
835 #endif /* NAGR > 0 */
837 if (subtype
== 0 || subtype
> 10) {
842 /* unknown subtype */
848 if (m
->m_flags
& M_PROMISC
) {
854 /* If the CRC is still on the packet, trim it off. */
855 if (m
->m_flags
& M_HASFCS
) {
856 m_adj(m
, -ETHER_CRC_LEN
);
857 m
->m_flags
&= ~M_HASFCS
;
860 if (etype
> ETHERMTU
+ sizeof (struct ether_header
)) {
861 /* Strip off the Ethernet header. */
862 m_adj(m
, sizeof(struct ether_header
));
868 if (ipflow_fastforward(m
))
871 schednetisr(NETISR_IP
);
876 schednetisr(NETISR_ARP
);
880 case ETHERTYPE_REVARP
:
881 revarpinput(m
); /* XXX queue? */
887 if (ip6flow_fastforward(m
))
890 schednetisr(NETISR_IPV6
);
896 schednetisr(NETISR_IPX
);
901 case ETHERTYPE_ATALK
:
902 schednetisr(NETISR_ATALK
);
906 /* probably this should be done with a NETISR as well */
907 aarpinput(ifp
, m
); /* XXX */
909 #endif /* NETATALK */
915 #if defined (ISO) || defined (LLC) || defined (NETATALK)
916 l
= (struct llc
*)(eh
+1);
917 switch (l
->llc_dsap
) {
920 switch (l
->llc_control
) {
922 if (l
->llc_ssap
!= LLC_SNAP_LSAP
) {
926 if (memcmp(&(l
->llc_snap_org_code
)[0],
927 at_org_code
, sizeof(at_org_code
)) == 0 &&
928 ntohs(l
->llc_snap_ether_type
) ==
931 m_adj(m
, sizeof(struct ether_header
)
932 + sizeof(struct llc
));
933 schednetisr(NETISR_ATALK
);
937 if (memcmp(&(l
->llc_snap_org_code
)[0],
939 sizeof(aarp_org_code
)) == 0 &&
940 ntohs(l
->llc_snap_ether_type
) ==
942 m_adj( m
, sizeof(struct ether_header
)
943 + sizeof(struct llc
));
944 aarpinput(ifp
, m
); /* XXX */
952 #endif /* NETATALK */
955 switch (l
->llc_control
) {
957 /* LLC_UI_P forbidden in class 1 service */
958 if ((l
->llc_dsap
== LLC_ISO_LSAP
) && /* XXX? case tested */
959 (l
->llc_ssap
== LLC_ISO_LSAP
)) {
961 /* XXX length computation?? */
962 if (m
->m_pkthdr
.len
> etype
+ sizeof(struct ether_header
))
963 m_adj(m
, etype
- m
->m_pkthdr
.len
);
966 if (argo_debug
[D_ETHER
])
967 printf("clnp packet");
969 schednetisr(NETISR_ISO
);
977 if(m
->m_len
< LLC_XID_BASIC_MINLEN
+ sizeof(struct ether_header
))
981 l
->llc_fid
= LLC_XID_FORMAT_BASIC
;
982 l
->llc_class
= LLC_XID_CLASS_I
;
983 l
->llc_dsap
= l
->llc_ssap
= 0;
984 /* Fall through to */
989 struct ether_header
*eh2
;
991 u_char c
= l
->llc_dsap
;
993 l
->llc_dsap
= l
->llc_ssap
;
995 m_adj(m
, sizeof(struct ether_header
));
996 /* XXX we can optimize here? */
997 if (m
->m_flags
& (M_BCAST
| M_MCAST
))
998 memcpy(eh
->ether_dhost
,
999 CLLADDR(ifp
->if_sadl
),
1001 sa
.sa_family
= AF_UNSPEC
;
1002 sa
.sa_len
= sizeof(sa
);
1003 eh2
= (struct ether_header
*)sa
.sa_data
;
1004 for (i
= 0; i
< 6; i
++) {
1005 eh2
->ether_shost
[i
] = c
=
1007 eh2
->ether_dhost
[i
] =
1008 eh
->ether_dhost
[i
] =
1010 eh
->ether_shost
[i
] = c
;
1012 ifp
->if_output(ifp
, m
, &sa
, NULL
);
1021 #if defined (ISO) || defined (NETATALK)
1028 #else /* ISO || LLC || NETATALK*/
1031 #endif /* ISO || LLC || NETATALK*/
1034 if (IF_QFULL(inq
)) {
1042 * Convert Ethernet address to printable (loggable) representation.
1045 ether_sprintf(const u_char
*ap
)
1047 static char etherbuf
[3 * ETHER_ADDR_LEN
];
1048 return ether_snprintf(etherbuf
, sizeof(etherbuf
), ap
);
1052 ether_snprintf(char *buf
, size_t len
, const u_char
*ap
)
1057 for (i
= 0; i
< len
/ 3; i
++) {
1058 *cp
++ = hexdigits
[*ap
>> 4];
1059 *cp
++ = hexdigits
[*ap
++ & 0xf];
1067 * Perform common duties while attaching to interface list
1070 ether_ifattach(struct ifnet
*ifp
, const uint8_t *lla
)
1072 struct ethercom
*ec
= (struct ethercom
*)ifp
;
1074 ifp
->if_type
= IFT_ETHER
;
1075 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
1076 ifp
->if_dlt
= DLT_EN10MB
;
1077 ifp
->if_mtu
= ETHERMTU
;
1078 ifp
->if_output
= ether_output
;
1079 ifp
->if_input
= ether_input
;
1080 if (ifp
->if_baudrate
== 0)
1081 ifp
->if_baudrate
= IF_Mbps(10); /* just a default */
1083 if_set_sadl(ifp
, lla
, ETHER_ADDR_LEN
, !ETHER_IS_LOCAL(lla
));
1085 LIST_INIT(&ec
->ec_multiaddrs
);
1086 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
1088 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1091 strlcpy(ec
->ec_tx_mowner
.mo_name
, ifp
->if_xname
,
1092 sizeof(ec
->ec_tx_mowner
.mo_name
));
1093 strlcpy(ec
->ec_tx_mowner
.mo_descr
, "tx",
1094 sizeof(ec
->ec_tx_mowner
.mo_descr
));
1095 strlcpy(ec
->ec_rx_mowner
.mo_name
, ifp
->if_xname
,
1096 sizeof(ec
->ec_rx_mowner
.mo_name
));
1097 strlcpy(ec
->ec_rx_mowner
.mo_descr
, "rx",
1098 sizeof(ec
->ec_rx_mowner
.mo_descr
));
1099 MOWNER_ATTACH(&ec
->ec_tx_mowner
);
1100 MOWNER_ATTACH(&ec
->ec_rx_mowner
);
1101 ifp
->if_mowner
= &ec
->ec_tx_mowner
;
1106 ether_ifdetach(struct ifnet
*ifp
)
1108 struct ethercom
*ec
= (void *) ifp
;
1109 struct ether_multi
*enm
;
1114 bridge_ifdetach(ifp
);
1127 while ((enm
= LIST_FIRST(&ec
->ec_multiaddrs
)) != NULL
) {
1128 LIST_REMOVE(enm
, enm_list
);
1129 free(enm
, M_IFMADDR
);
1134 #if 0 /* done in if_detach() */
1138 MOWNER_DETACH(&ec
->ec_rx_mowner
);
1139 MOWNER_DETACH(&ec
->ec_tx_mowner
);
1144 * This is for reference. We have a table-driven version
1145 * of the little-endian crc32 generator, which is faster
1146 * than the double-loop.
1149 ether_crc32_le(const uint8_t *buf
, size_t len
)
1151 uint32_t c
, crc
, carry
;
1154 crc
= 0xffffffffU
; /* initial value */
1156 for (i
= 0; i
< len
; i
++) {
1158 for (j
= 0; j
< 8; j
++) {
1159 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
1163 crc
= (crc
^ ETHER_CRC_POLY_LE
);
1171 ether_crc32_le(const uint8_t *buf
, size_t len
)
1173 static const uint32_t crctab
[] = {
1174 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1175 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1176 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1177 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1182 crc
= 0xffffffffU
; /* initial value */
1184 for (i
= 0; i
< len
; i
++) {
1186 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1187 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1195 ether_crc32_be(const uint8_t *buf
, size_t len
)
1197 uint32_t c
, crc
, carry
;
1200 crc
= 0xffffffffU
; /* initial value */
1202 for (i
= 0; i
< len
; i
++) {
1204 for (j
= 0; j
< 8; j
++) {
1205 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
1209 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
1217 const uint8_t ether_ipmulticast_min
[ETHER_ADDR_LEN
] =
1218 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1219 const uint8_t ether_ipmulticast_max
[ETHER_ADDR_LEN
] =
1220 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1223 const uint8_t ether_ip6multicast_min
[ETHER_ADDR_LEN
] =
1224 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1225 const uint8_t ether_ip6multicast_max
[ETHER_ADDR_LEN
] =
1226 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1230 * ether_aton implementation, not using a static buffer.
1233 ether_nonstatic_aton(u_char
*dest
, char *str
)
1240 if (*cp > '9' && *cp < 'a') \
1242 else if (*cp > '9') \
1247 for (i
= 0; i
< 6; i
++, cp
++) {
1252 if (isxdigit(*cp
)) {
1257 if (*cp
== ':' || i
== 5)
1262 memcpy(dest
, val
, 6);
1269 * Convert a sockaddr into an Ethernet address or range of Ethernet
1273 ether_multiaddr(const struct sockaddr
*sa
, uint8_t addrlo
[ETHER_ADDR_LEN
],
1274 uint8_t addrhi
[ETHER_ADDR_LEN
])
1277 const struct sockaddr_in
*sin
;
1280 const struct sockaddr_in6
*sin6
;
1283 switch (sa
->sa_family
) {
1286 memcpy(addrlo
, sa
->sa_data
, ETHER_ADDR_LEN
);
1287 memcpy(addrhi
, addrlo
, ETHER_ADDR_LEN
);
1293 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
1295 * An IP address of INADDR_ANY means listen to
1296 * or stop listening to all of the Ethernet
1297 * multicast addresses used for IP.
1298 * (This is for the sake of IP multicast routers.)
1300 memcpy(addrlo
, ether_ipmulticast_min
, ETHER_ADDR_LEN
);
1301 memcpy(addrhi
, ether_ipmulticast_max
, ETHER_ADDR_LEN
);
1304 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
1305 memcpy(addrhi
, addrlo
, ETHER_ADDR_LEN
);
1311 sin6
= satocsin6(sa
);
1312 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
1314 * An IP6 address of 0 means listen to or stop
1315 * listening to all of the Ethernet multicast
1316 * address used for IP6.
1317 * (This is used for multicast routers.)
1319 memcpy(addrlo
, ether_ip6multicast_min
, ETHER_ADDR_LEN
);
1320 memcpy(addrhi
, ether_ip6multicast_max
, ETHER_ADDR_LEN
);
1322 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, addrlo
);
1323 memcpy(addrhi
, addrlo
, ETHER_ADDR_LEN
);
1329 return EAFNOSUPPORT
;
1335 * Add an Ethernet multicast address or range of addresses to the list for a
1339 ether_addmulti(const struct sockaddr
*sa
, struct ethercom
*ec
)
1341 struct ether_multi
*enm
;
1342 u_char addrlo
[ETHER_ADDR_LEN
];
1343 u_char addrhi
[ETHER_ADDR_LEN
];
1344 int s
= splnet(), error
;
1346 error
= ether_multiaddr(sa
, addrlo
, addrhi
);
1353 * Verify that we have valid Ethernet multicast addresses.
1355 if ((addrlo
[0] & 0x01) != 1 || (addrhi
[0] & 0x01) != 1) {
1360 * See if the address range is already in the list.
1362 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ec
, enm
);
1365 * Found it; just increment the reference count.
1367 ++enm
->enm_refcount
;
1372 * New address or range; malloc a new multicast record
1373 * and link it into the interface's multicast list.
1375 enm
= (struct ether_multi
*)malloc(sizeof(*enm
), M_IFMADDR
, M_NOWAIT
);
1380 memcpy(enm
->enm_addrlo
, addrlo
, 6);
1381 memcpy(enm
->enm_addrhi
, addrhi
, 6);
1382 enm
->enm_refcount
= 1;
1383 LIST_INSERT_HEAD(&ec
->ec_multiaddrs
, enm
, enm_list
);
1387 * Return ENETRESET to inform the driver that the list has changed
1388 * and its reception filter should be adjusted accordingly.
1394 * Delete a multicast address record.
1397 ether_delmulti(const struct sockaddr
*sa
, struct ethercom
*ec
)
1399 struct ether_multi
*enm
;
1400 u_char addrlo
[ETHER_ADDR_LEN
];
1401 u_char addrhi
[ETHER_ADDR_LEN
];
1402 int s
= splnet(), error
;
1404 error
= ether_multiaddr(sa
, addrlo
, addrhi
);
1411 * Look ur the address in our list.
1413 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ec
, enm
);
1418 if (--enm
->enm_refcount
!= 0) {
1420 * Still some claims to this record.
1426 * No remaining claims to this record; unlink and free it.
1428 LIST_REMOVE(enm
, enm_list
);
1429 free(enm
, M_IFMADDR
);
1433 * Return ENETRESET to inform the driver that the list has changed
1434 * and its reception filter should be adjusted accordingly.
1440 ether_set_ifflags_cb(struct ethercom
*ec
, ether_cb_t cb
)
1442 ec
->ec_ifflags_cb
= cb
;
1446 * Common ioctls for Ethernet interfaces. Note, we must be
1447 * called at splnet().
1450 ether_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1452 struct ethercom
*ec
= (void *) ifp
;
1453 struct ifreq
*ifr
= (struct ifreq
*)data
;
1454 struct if_laddrreq
*iflr
= data
;
1455 const struct sockaddr_dl
*sdl
;
1456 static const uint8_t zero
[ETHER_ADDR_LEN
];
1460 case SIOCINITIFADDR
:
1461 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
1462 (IFF_UP
|IFF_RUNNING
)) {
1463 ifp
->if_flags
|= IFF_UP
;
1464 if ((error
= (*ifp
->if_init
)(ifp
)) != 0)
1469 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
1471 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
1472 arp_ifinit(ifp
, ifa
);
1481 if (ec
->ec_capabilities
& ETHERCAP_JUMBO_MTU
)
1482 maxmtu
= ETHERMTU_JUMBO
;
1486 if (ifr
->ifr_mtu
< ETHERMIN
|| ifr
->ifr_mtu
> maxmtu
)
1488 else if ((error
= ifioctl_common(ifp
, cmd
, data
)) != ENETRESET
)
1490 else if (ifp
->if_flags
& IFF_UP
) {
1491 /* Make sure the device notices the MTU change. */
1492 return (*ifp
->if_init
)(ifp
);
1498 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
1500 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
1503 * If interface is marked down and it is running,
1504 * then stop and disable it.
1506 (*ifp
->if_stop
)(ifp
, 1);
1510 * If interface is marked up and it is stopped, then
1513 return (*ifp
->if_init
)(ifp
);
1514 case IFF_UP
|IFF_RUNNING
:
1516 if (ec
->ec_ifflags_cb
== NULL
||
1517 (error
= (*ec
->ec_ifflags_cb
)(ec
)) == ENETRESET
) {
1519 * Reset the interface to pick up
1520 * changes in any other flags that
1521 * affect the hardware state.
1523 return (*ifp
->if_init
)(ifp
);
1531 return ether_addmulti(ifreq_getaddr(cmd
, ifr
), ec
);
1533 return ether_delmulti(ifreq_getaddr(cmd
, ifr
), ec
);
1536 if (ec
->ec_mii
== NULL
)
1538 return ifmedia_ioctl(ifp
, ifr
, &ec
->ec_mii
->mii_media
, cmd
);
1540 sdl
= satocsdl(sstocsa(&iflr
->addr
));
1541 if (sdl
->sdl_family
!= AF_LINK
)
1543 else if (ETHER_IS_MULTICAST(CLLADDR(sdl
)))
1545 else if (memcmp(zero
, CLLADDR(sdl
), sizeof(zero
)) == 0)
1549 return ifioctl_common(ifp
, cmd
, data
);