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.37 2006/07/02 00:49:22 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 ASSERT_SERIALIZED(ifp
->if_serializer
);
555 /* XXX old crufty stuff, needs to be removed */
557 printf("ether_input got mbuf without embedded ethernet header");
562 if (m
->m_len
< sizeof(struct ether_header
)) {
563 /* XXX error in the caller. */
567 m
->m_pkthdr
.rcvif
= ifp
;
568 eh
= mtod(m
, struct ether_header
*);
572 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
575 * Tap the packet off here for a bridge. bridge_input()
576 * will return NULL if it has consumed the packet, otherwise
577 * it gets processed as normal. Note that bridge_input()
578 * will always return the original packet if we need to
579 * process it locally.
581 if (ifp
->if_bridge
) {
582 KASSERT(bridge_input_p
!= NULL
,("ether_input: if_bridge not loaded!"));
584 if(m
->m_flags
& M_PROTO1
) {
585 m
->m_flags
&= ~M_PROTO1
;
587 /* clear M_PROMISC, in case the packets comes from a vlan */
588 /* m->m_flags &= ~M_PROMISC; */
589 lwkt_serialize_exit(ifp
->if_serializer
);
590 m
= (*bridge_input_p
)(ifp
, m
);
591 lwkt_serialize_enter(ifp
->if_serializer
);
596 * Bridge has determined that the packet is for us.
597 * Update our interface pointer -- we may have had
598 * to "bridge" the packet locally.
600 ifp
= m
->m_pkthdr
.rcvif
;
604 /* XXX old crufty stuff, needs to be removed */
605 m_adj(m
, sizeof(struct ether_header
));
607 /* m->m_pkthdr.len = m->m_len; */
609 /* Handle ng_ether(4) processing, if any */
610 if (ng_ether_input_p
!= NULL
) {
611 lwkt_serialize_exit(ifp
->if_serializer
);
612 (*ng_ether_input_p
)(ifp
, &m
, eh
);
613 lwkt_serialize_enter(ifp
->if_serializer
);
618 /* Continue with upper layer processing */
619 ether_demux(ifp
, eh
, m
);
623 * Upper layer processing for a received Ethernet packet.
626 ether_demux(struct ifnet
*ifp
, struct ether_header
*eh
, struct mbuf
*m
)
630 struct ip_fw
*rule
= NULL
;
635 /* Extract info from dummynet tag, ignore others */
636 while (m
->m_type
== MT_TAG
) {
637 if (m
->m_flags
== PACKET_TAG_DUMMYNET
) {
638 rule
= ((struct dn_pkt
*)m
)->rule
;
639 ifp
= m
->m_next
->m_pkthdr
.rcvif
;
644 if (rule
) /* packet is passing the second time */
648 * Discard packet if upper layers shouldn't see it because
649 * it was unicast to a different Ethernet address. If the
650 * driver is working properly, then this situation can only
651 * happen when the interface is in promiscuous mode.
653 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
654 (eh
->ether_dhost
[0] & 1) == 0 &&
655 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
659 /* Discard packet if interface is not up */
660 if (!(ifp
->if_flags
& IFF_UP
)) {
664 if (eh
->ether_dhost
[0] & 1) {
665 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
666 ifp
->if_addrlen
) == 0)
667 m
->m_flags
|= M_BCAST
;
669 m
->m_flags
|= M_MCAST
;
674 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
675 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
, FALSE
)) {
679 eh
= mtod(m
, struct ether_header
*);
682 ether_type
= ntohs(eh
->ether_type
);
684 switch (ether_type
) {
687 if (ipflow_fastforward(m
, ifp
->if_serializer
))
693 if (ifp
->if_flags
& IFF_NOARP
) {
694 /* Discard packet if ARP is disabled on interface */
710 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
717 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
733 if (vlan_input_p
!= NULL
)
734 (*vlan_input_p
)(eh
, m
);
736 m
->m_pkthdr
.rcvif
->if_noproto
++;
743 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
747 checksum
= mtod(m
, ushort
*);
749 if ((ether_type
<= ETHERMTU
) &&
750 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
751 if (*checksum
== 0xE0E0) {
752 m
->m_pkthdr
.len
-= 3;
761 if (ether_type
> ETHERMTU
)
763 l
= mtod(m
, struct llc
*);
764 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
765 l
->llc_ssap
== LLC_SNAP_LSAP
&&
766 l
->llc_control
== LLC_UI
) {
767 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
768 sizeof at_org_code
) == 0 &&
769 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
770 m_adj(m
, sizeof(struct llc
));
774 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
775 sizeof aarp_org_code
) == 0 &&
776 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
777 m_adj(m
, sizeof(struct llc
));
784 if (ng_ether_input_orphan_p
!= NULL
)
785 (*ng_ether_input_orphan_p
)(ifp
, m
, eh
);
790 netisr_dispatch(isr
, m
);
794 * Perform common duties while attaching to interface list
798 ether_ifattach(struct ifnet
*ifp
, uint8_t *lla
, lwkt_serialize_t serializer
)
800 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
805 ether_ifattach_bpf(struct ifnet
*ifp
, uint8_t *lla
, u_int dlt
, u_int hdrlen
,
806 lwkt_serialize_t serializer
)
808 struct sockaddr_dl
*sdl
;
810 ifp
->if_type
= IFT_ETHER
;
811 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
812 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
813 if_attach(ifp
, serializer
);
814 ifp
->if_mtu
= ETHERMTU
;
815 if (ifp
->if_baudrate
== 0)
816 ifp
->if_baudrate
= 10000000;
817 ifp
->if_output
= ether_output
;
818 ifp
->if_input
= ether_input_internal
;
819 ifp
->if_resolvemulti
= ether_resolvemulti
;
820 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
821 sdl
= IF_LLSOCKADDR(ifp
);
822 sdl
->sdl_type
= IFT_ETHER
;
823 sdl
->sdl_alen
= ifp
->if_addrlen
;
824 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
826 * XXX Keep the current drivers happy.
827 * XXX Remove once all drivers have been cleaned up
829 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
830 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
831 bpfattach(ifp
, dlt
, hdrlen
);
832 if (ng_ether_attach_p
!= NULL
)
833 (*ng_ether_attach_p
)(ifp
);
835 if_printf(ifp
, "MAC address: %6D\n", lla
, ":");
839 * Perform common duties while detaching an Ethernet interface
842 ether_ifdetach(struct ifnet
*ifp
)
846 if (ng_ether_detach_p
!= NULL
)
847 (*ng_ether_detach_p
)(ifp
);
853 ether_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
855 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
856 struct ifreq
*ifr
= (struct ifreq
*) data
;
859 ASSERT_SERIALIZED(ifp
->if_serializer
);
863 ifp
->if_flags
|= IFF_UP
;
865 switch (ifa
->ifa_addr
->sa_family
) {
868 ifp
->if_init(ifp
->if_softc
); /* before arpwhohas */
869 arp_ifinit(ifp
, ifa
);
874 * XXX - This code is probably wrong
878 struct ipx_addr
*ina
= &IA_SIPX(ifa
)->sipx_addr
;
879 struct arpcom
*ac
= IFP2AC(ifp
);
881 if (ipx_nullhost(*ina
))
882 ina
->x_host
= *(union ipx_host
*) ac
->ac_enaddr
;
884 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
885 sizeof ac
->ac_enaddr
);
887 ifp
->if_init(ifp
->if_softc
); /* Set new address. */
893 * XXX - This code is probably wrong
897 struct ns_addr
*ina
= &(IA_SNS(ifa
)->sns_addr
);
898 struct arpcom
*ac
= IFP2AC(ifp
);
900 if (ns_nullhost(*ina
))
901 ina
->x_host
= *(union ns_host
*)(ac
->ac_enaddr
);
903 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
904 sizeof ac
->ac_enaddr
);
909 ifp
->if_init(ifp
->if_softc
);
914 ifp
->if_init(ifp
->if_softc
);
920 bcopy(IFP2AC(ifp
)->ac_enaddr
,
921 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
927 * Set the interface MTU.
929 if (ifr
->ifr_mtu
> ETHERMTU
) {
932 ifp
->if_mtu
= ifr
->ifr_mtu
;
945 struct sockaddr
**llsa
,
948 struct sockaddr_dl
*sdl
;
949 struct sockaddr_in
*sin
;
951 struct sockaddr_in6
*sin6
;
955 switch(sa
->sa_family
) {
958 * No mapping needed. Just check that it's a valid MC address.
960 sdl
= (struct sockaddr_dl
*)sa
;
961 e_addr
= LLADDR(sdl
);
962 if ((e_addr
[0] & 1) != 1)
963 return EADDRNOTAVAIL
;
969 sin
= (struct sockaddr_in
*)sa
;
970 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
971 return EADDRNOTAVAIL
;
972 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
974 sdl
->sdl_len
= sizeof *sdl
;
975 sdl
->sdl_family
= AF_LINK
;
976 sdl
->sdl_index
= ifp
->if_index
;
977 sdl
->sdl_type
= IFT_ETHER
;
978 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
979 e_addr
= LLADDR(sdl
);
980 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
981 *llsa
= (struct sockaddr
*)sdl
;
986 sin6
= (struct sockaddr_in6
*)sa
;
987 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
989 * An IP6 address of 0 means listen to all
990 * of the Ethernet multicast address used for IP6.
991 * (This is used for multicast routers.)
993 ifp
->if_flags
|= IFF_ALLMULTI
;
997 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
998 return EADDRNOTAVAIL
;
999 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1001 sdl
->sdl_len
= sizeof *sdl
;
1002 sdl
->sdl_family
= AF_LINK
;
1003 sdl
->sdl_index
= ifp
->if_index
;
1004 sdl
->sdl_type
= IFT_ETHER
;
1005 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1006 e_addr
= LLADDR(sdl
);
1007 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
1008 *llsa
= (struct sockaddr
*)sdl
;
1014 * Well, the text isn't quite right, but it's the name
1017 return EAFNOSUPPORT
;
1023 * This is for reference. We have a table-driven version
1024 * of the little-endian crc32 generator, which is faster
1025 * than the double-loop.
1028 ether_crc32_le(const uint8_t *buf
, size_t len
)
1030 uint32_t c
, crc
, carry
;
1033 crc
= 0xffffffffU
; /* initial value */
1035 for (i
= 0; i
< len
; i
++) {
1037 for (j
= 0; j
< 8; j
++) {
1038 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
1042 crc
= (crc
^ ETHER_CRC_POLY_LE
);
1050 ether_crc32_le(const uint8_t *buf
, size_t len
)
1052 static const uint32_t crctab
[] = {
1053 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1054 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1055 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1056 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1061 crc
= 0xffffffffU
; /* initial value */
1063 for (i
= 0; i
< len
; i
++) {
1065 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1066 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1074 ether_crc32_be(const uint8_t *buf
, size_t len
)
1076 uint32_t c
, crc
, carry
;
1079 crc
= 0xffffffffU
; /* initial value */
1081 for (i
= 0; i
< len
; i
++) {
1083 for (j
= 0; j
< 8; j
++) {
1084 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
1088 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
1096 * find the size of ethernet header, and call classifier
1099 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
1100 struct altq_pktattr
*pktattr
)
1102 struct ether_header
*eh
;
1103 uint16_t ether_type
;
1104 int hlen
, af
, hdrsize
;
1107 hlen
= sizeof(struct ether_header
);
1108 eh
= mtod(m
, struct ether_header
*);
1110 ether_type
= ntohs(eh
->ether_type
);
1111 if (ether_type
< ETHERMTU
) {
1113 struct llc
*llc
= (struct llc
*)(eh
+ 1);
1116 if (m
->m_len
< hlen
||
1117 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
1118 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
1119 llc
->llc_control
!= LLC_UI
)
1120 goto bad
; /* not snap! */
1122 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
1125 if (ether_type
== ETHERTYPE_IP
) {
1127 hdrsize
= 20; /* sizeof(struct ip) */
1129 } else if (ether_type
== ETHERTYPE_IPV6
) {
1131 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
1136 while (m
->m_len
<= hlen
) {
1140 hdr
= m
->m_data
+ hlen
;
1141 if (m
->m_len
< hlen
+ hdrsize
) {
1143 * ip header is not in a single mbuf. this should not
1144 * happen in the current code.
1145 * (todo: use m_pulldown in the future)
1151 ifq_classify(ifq
, m
, af
, pktattr
);
1158 pktattr
->pattr_class
= NULL
;
1159 pktattr
->pattr_hdr
= NULL
;
1160 pktattr
->pattr_af
= AF_UNSPEC
;