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.41 2007/06/05 13:41:39 sephe 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_MONITOR
)
167 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
))
170 M_PREPEND(m
, sizeof(struct ether_header
), MB_DONTWAIT
);
173 eh
= mtod(m
, struct ether_header
*);
174 edst
= eh
->ether_dhost
;
177 * Fill in the destination ethernet address and frame type.
179 switch (dst
->sa_family
) {
182 if (!arpresolve(ifp
, rt
, m
, dst
, edst
))
183 return (0); /* if not yet resolved */
184 eh
->ether_type
= htons(ETHERTYPE_IP
);
189 if (!nd6_storelladdr(&ac
->ac_if
, rt
, m
, dst
, edst
))
190 return (0); /* Something bad happenned. */
191 eh
->ether_type
= htons(ETHERTYPE_IPV6
);
196 if (ef_outputp
!= NULL
) {
197 error
= ef_outputp(ifp
, &m
, dst
, &eh
->ether_type
,
202 eh
->ether_type
= htons(ETHERTYPE_IPX
);
203 bcopy(&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
204 edst
, ETHER_ADDR_LEN
);
210 struct at_ifaddr
*aa
;
212 if ((aa
= at_ifawithnet((struct sockaddr_at
*)dst
)) == NULL
) {
217 * In the phase 2 case, need to prepend an mbuf for
218 * the llc header. Since we must preserve the value
219 * of m, which is passed to us by value, we m_copy()
220 * the first mbuf, and use it for our llc header.
222 if (aa
->aa_flags
& AFA_PHASE2
) {
225 M_PREPEND(m
, sizeof(struct llc
), MB_DONTWAIT
);
226 eh
= mtod(m
, struct ether_header
*);
227 edst
= eh
->ether_dhost
;
228 llc
.llc_dsap
= llc
.llc_ssap
= LLC_SNAP_LSAP
;
229 llc
.llc_control
= LLC_UI
;
230 bcopy(at_org_code
, llc
.llc_snap_org_code
,
232 llc
.llc_snap_ether_type
= htons(ETHERTYPE_AT
);
234 mtod(m
, caddr_t
) + sizeof(struct ether_header
),
236 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
237 hlen
= sizeof(struct llc
) + ETHER_HDR_LEN
;
239 eh
->ether_type
= htons(ETHERTYPE_AT
);
241 if (!aarpresolve(ac
, m
, (struct sockaddr_at
*)dst
, edst
))
250 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
251 eh
->ether_type
= 0x8137;
253 case 0x0: /* Novell 802.3 */
254 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
256 case 0xe0e0: /* Novell 802.2 and Token-Ring */
257 M_PREPEND(m
, 3, MB_DONTWAIT
);
258 eh
= mtod(m
, struct ether_header
*);
259 edst
= eh
->ether_dhost
;
260 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
261 cp
= mtod(m
, u_char
*) + sizeof(struct ether_header
);
267 bcopy(&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
), edst
,
270 * XXX if ns_thishost is the same as the node's ethernet
271 * address then just the default code will catch this anyhow.
272 * So I'm not sure if this next clause should be here at all?
275 if (bcmp(edst
, &ns_thishost
, ETHER_ADDR_LEN
) == 0) {
276 m
->m_pkthdr
.rcvif
= ifp
;
277 netisr_dispatch(NETISR_NS
, m
);
280 if (bcmp(edst
, &ns_broadhost
, ETHER_ADDR_LEN
) == 0)
281 m
->m_flags
|= M_BCAST
;
284 case pseudo_AF_HDRCMPLT
:
286 loop_copy
= -1; /* if this is for us, don't do it */
287 deh
= (struct ether_header
*)dst
->sa_data
;
288 memcpy(edst
, deh
->ether_dhost
, ETHER_ADDR_LEN
);
289 eh
->ether_type
= deh
->ether_type
;
293 if_printf(ifp
, "can't handle af%d\n", dst
->sa_family
);
294 gotoerr(EAFNOSUPPORT
);
297 if (dst
->sa_family
== pseudo_AF_HDRCMPLT
) /* unlikely */
298 memcpy(eh
->ether_shost
,
299 ((struct ether_header
*)dst
->sa_data
)->ether_shost
,
302 memcpy(eh
->ether_shost
, ac
->ac_enaddr
, ETHER_ADDR_LEN
);
305 * Bridges require special output handling.
307 if (ifp
->if_bridge
) {
308 KASSERT(bridge_output_p
!= NULL
,
309 ("%s: if_bridge not loaded!", __func__
));
310 return ((*bridge_output_p
)(ifp
, m
, NULL
, NULL
));
314 * If a simplex interface, and the packet is being sent to our
315 * Ethernet address or a broadcast address, loopback a copy.
316 * XXX To make a simplex device behave exactly like a duplex
317 * device, we should copy in the case of sending to our own
318 * ethernet address (thus letting the original actually appear
319 * on the wire). However, we don't do that here for security
320 * reasons and compatibility with the original behavior.
322 if ((ifp
->if_flags
& IFF_SIMPLEX
) && (loop_copy
!= -1)) {
325 if (m
->m_pkthdr
.csum_flags
& CSUM_IP
)
326 csum_flags
|= (CSUM_IP_CHECKED
| CSUM_IP_VALID
);
327 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)
328 csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
);
329 if ((m
->m_flags
& M_BCAST
) || (loop_copy
> 0)) {
332 if ((n
= m_copypacket(m
, MB_DONTWAIT
)) != NULL
) {
333 n
->m_pkthdr
.csum_flags
|= csum_flags
;
334 if (csum_flags
& CSUM_DATA_VALID
)
335 n
->m_pkthdr
.csum_data
= 0xffff;
336 if_simloop(ifp
, n
, dst
->sa_family
, hlen
);
339 } else if (bcmp(eh
->ether_dhost
, eh
->ether_shost
,
340 ETHER_ADDR_LEN
) == 0) {
341 m
->m_pkthdr
.csum_flags
|= csum_flags
;
342 if (csum_flags
& CSUM_DATA_VALID
)
343 m
->m_pkthdr
.csum_data
= 0xffff;
344 if_simloop(ifp
, m
, dst
->sa_family
, hlen
);
345 return (0); /* XXX */
349 /* Handle ng_ether(4) processing, if any */
350 if (ng_ether_output_p
!= NULL
) {
351 if ((error
= (*ng_ether_output_p
)(ifp
, &m
)) != 0)
357 /* Continue with link-layer output */
358 return ether_output_frame(ifp
, m
);
366 * Ethernet link layer output routine to send a raw frame to the device.
368 * This assumes that the 14 byte Ethernet header is present and contiguous
372 ether_output_frame(struct ifnet
*ifp
, struct mbuf
*m
)
374 struct ip_fw
*rule
= NULL
;
376 struct altq_pktattr pktattr
;
378 ASSERT_SERIALIZED(ifp
->if_serializer
);
380 /* Extract info from dummynet tag, ignore others */
381 while (m
->m_type
== MT_TAG
) {
382 if (m
->m_flags
== PACKET_TAG_DUMMYNET
) {
383 rule
= ((struct dn_pkt
*)m
)->rule
;
389 if (ifq_is_enabled(&ifp
->if_snd
))
390 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
392 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
393 struct ether_header save_eh
, *eh
;
395 eh
= mtod(m
, struct ether_header
*);
397 m_adj(m
, ETHER_HDR_LEN
);
398 if (!ether_ipfw_chk(&m
, ifp
, &rule
, eh
, FALSE
)) {
402 return ENOBUFS
; /* pkt dropped */
404 return 0; /* consumed e.g. in a pipe */
406 eh
= mtod(m
, struct ether_header
*);
407 /* packet was ok, restore the ethernet header */
408 if ((void *)(eh
+ 1) == (void *)m
->m_data
) {
409 m
->m_data
-= ETHER_HDR_LEN
;
410 m
->m_len
+= ETHER_HDR_LEN
;
411 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
413 M_PREPEND(m
, ETHER_HDR_LEN
, MB_DONTWAIT
);
414 if (m
== NULL
) /* nope... */ {
418 bcopy(&save_eh
, mtod(m
, struct ether_header
*),
425 * Queue message on interface, update output statistics if
426 * successful, and start output if interface not yet active.
428 error
= ifq_handoff(ifp
, m
, &pktattr
);
433 * ipfw processing for ethernet packets (in and out).
434 * The second parameter is NULL from ether_demux(), and ifp from
435 * ether_output_frame().
442 struct ether_header
*eh
,
445 struct ether_header save_eh
= *eh
; /* might be a ptr in m */
446 struct ip_fw_args args
;
450 if (*rule
!= NULL
&& fw_one_pass
)
451 return TRUE
; /* dummynet packet, already partially processed */
454 * I need some amount of data to be contiguous, and in case others
455 * need the packet (shared==TRUE), it also better be in the first mbuf.
457 i
= min((*m0
)->m_pkthdr
.len
, max_protohdr
);
458 if (shared
|| (*m0
)->m_len
< i
) {
459 *m0
= m_pullup(*m0
, i
);
464 args
.m
= *m0
; /* the packet we are looking at */
465 args
.oif
= dst
; /* destination, if any */
466 if ((mtag
= m_tag_find(*m0
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
467 m_tag_delete(*m0
, mtag
);
468 args
.rule
= *rule
; /* matching rule to restart */
469 args
.next_hop
= NULL
; /* we do not support forward yet */
470 args
.eh
= &save_eh
; /* MAC header for bridged/MAC packets */
471 i
= ip_fw_chk_ptr(&args
);
475 if ((i
& IP_FW_PORT_DENY_FLAG
) || *m0
== NULL
) /* drop */
478 if (i
== 0) /* a PASS rule. */
481 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
)) {
483 * Pass the pkt to dummynet, which consumes it.
484 * If shared, make a copy and keep the original.
489 m
= m_copypacket(*m0
, MB_DONTWAIT
);
493 m
= *m0
; /* pass the original to dummynet */
494 *m0
= NULL
; /* and nothing back to the caller */
497 * Prepend the header, optimize for the common case of
498 * eh pointing into the mbuf.
500 if ((void *)(eh
+ 1) == (void *)m
->m_data
) {
501 m
->m_data
-= ETHER_HDR_LEN
;
502 m
->m_len
+= ETHER_HDR_LEN
;
503 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
505 M_PREPEND(m
, ETHER_HDR_LEN
, MB_DONTWAIT
);
508 bcopy(&save_eh
, mtod(m
, struct ether_header
*),
511 ip_dn_io_ptr(m
, (i
& 0xffff),
512 dst
? DN_TO_ETH_OUT
: DN_TO_ETH_DEMUX
, &args
);
516 * XXX at some point add support for divert/forward actions.
517 * If none of the above matches, we have to drop the pkt.
523 * XXX merge this function with ether_input.
526 ether_input_internal(struct ifnet
*ifp
, struct mbuf
*m
)
528 ether_input(ifp
, NULL
, m
);
532 * Process a received Ethernet packet. We have two different interfaces:
533 * one (conventional) assumes the packet in the mbuf, with the ethernet
534 * header provided separately in *eh. The second one (new) has everything
535 * in the mbuf, and we can tell it because eh == NULL.
536 * The caller MUST MAKE SURE that there are at least
537 * sizeof(struct ether_header) bytes in the first mbuf.
539 * This allows us to concentrate in one place a bunch of code which
540 * is replicated in all device drivers. Also, many functions called
541 * from ether_input() try to put the eh back into the mbuf, so we
542 * can later propagate the 'contiguous packet' interface to them,
543 * and handle the old interface just here.
545 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
546 * cluster, right before m_data. So be very careful when working on m,
547 * as you could destroy *eh !!
549 * First we perform any link layer operations, then continue
550 * to the upper layers with ether_demux().
553 ether_input(struct ifnet
*ifp
, struct ether_header
*eh
, struct mbuf
*m
)
555 ASSERT_SERIALIZED(ifp
->if_serializer
);
557 /* XXX old crufty stuff, needs to be removed */
559 kprintf("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
;
576 if (ifp
->if_flags
& IFF_MONITOR
) {
578 * Interface marked for monitoring; discard packet.
585 * Tap the packet off here for a bridge. bridge_input()
586 * will return NULL if it has consumed the packet, otherwise
587 * it gets processed as normal. Note that bridge_input()
588 * will always return the original packet if we need to
589 * process it locally.
591 if (ifp
->if_bridge
) {
592 KASSERT(bridge_input_p
!= NULL
,
593 ("%s: if_bridge not loaded!", __func__
));
595 if(m
->m_flags
& M_PROTO1
) {
596 m
->m_flags
&= ~M_PROTO1
;
598 /* clear M_PROMISC, in case the packets comes from a vlan */
599 /* m->m_flags &= ~M_PROMISC; */
600 lwkt_serialize_exit(ifp
->if_serializer
);
601 m
= (*bridge_input_p
)(ifp
, m
);
602 lwkt_serialize_enter(ifp
->if_serializer
);
606 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
607 ("bridge_input_p changed rcvif\n"));
611 /* XXX old crufty stuff, needs to be removed */
612 m_adj(m
, sizeof(struct ether_header
));
614 /* m->m_pkthdr.len = m->m_len; */
616 /* Handle ng_ether(4) processing, if any */
617 if (ng_ether_input_p
!= NULL
) {
618 lwkt_serialize_exit(ifp
->if_serializer
);
619 (*ng_ether_input_p
)(ifp
, &m
, eh
);
620 lwkt_serialize_enter(ifp
->if_serializer
);
625 /* Continue with upper layer processing */
626 ether_demux(ifp
, eh
, m
);
630 * Upper layer processing for a received Ethernet packet.
633 ether_demux(struct ifnet
*ifp
, struct ether_header
*eh
, struct mbuf
*m
)
637 struct ip_fw
*rule
= NULL
;
642 /* Extract info from dummynet tag, ignore others */
643 while (m
->m_type
== MT_TAG
) {
644 if (m
->m_flags
== PACKET_TAG_DUMMYNET
) {
645 rule
= ((struct dn_pkt
*)m
)->rule
;
646 ifp
= m
->m_next
->m_pkthdr
.rcvif
;
651 if (rule
) /* packet is passing the second time */
655 * Discard packet if upper layers shouldn't see it because
656 * it was unicast to a different Ethernet address. If the
657 * driver is working properly, then this situation can only
658 * happen when the interface is in promiscuous mode.
660 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
661 (eh
->ether_dhost
[0] & 1) == 0 &&
662 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
666 /* Discard packet if interface is not up */
667 if (!(ifp
->if_flags
& IFF_UP
)) {
671 if (eh
->ether_dhost
[0] & 1) {
672 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
673 ifp
->if_addrlen
) == 0)
674 m
->m_flags
|= M_BCAST
;
676 m
->m_flags
|= M_MCAST
;
681 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
682 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
, FALSE
)) {
686 eh
= mtod(m
, struct ether_header
*);
689 ether_type
= ntohs(eh
->ether_type
);
691 switch (ether_type
) {
694 if (ipflow_fastforward(m
, ifp
->if_serializer
))
700 if (ifp
->if_flags
& IFF_NOARP
) {
701 /* Discard packet if ARP is disabled on interface */
717 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
724 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
740 if (vlan_input_p
!= NULL
)
741 (*vlan_input_p
)(eh
, m
);
743 m
->m_pkthdr
.rcvif
->if_noproto
++;
750 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
754 checksum
= mtod(m
, ushort
*);
756 if ((ether_type
<= ETHERMTU
) &&
757 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
758 if (*checksum
== 0xE0E0) {
759 m
->m_pkthdr
.len
-= 3;
768 if (ether_type
> ETHERMTU
)
770 l
= mtod(m
, struct llc
*);
771 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
772 l
->llc_ssap
== LLC_SNAP_LSAP
&&
773 l
->llc_control
== LLC_UI
) {
774 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
775 sizeof at_org_code
) == 0 &&
776 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
777 m_adj(m
, sizeof(struct llc
));
781 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
782 sizeof aarp_org_code
) == 0 &&
783 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
784 m_adj(m
, sizeof(struct llc
));
791 if (ng_ether_input_orphan_p
!= NULL
)
792 (*ng_ether_input_orphan_p
)(ifp
, m
, eh
);
797 netisr_dispatch(isr
, m
);
801 * Perform common duties while attaching to interface list
805 ether_ifattach(struct ifnet
*ifp
, uint8_t *lla
, lwkt_serialize_t serializer
)
807 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
812 ether_ifattach_bpf(struct ifnet
*ifp
, uint8_t *lla
, u_int dlt
, u_int hdrlen
,
813 lwkt_serialize_t serializer
)
815 struct sockaddr_dl
*sdl
;
817 ifp
->if_type
= IFT_ETHER
;
818 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
819 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
820 if_attach(ifp
, serializer
);
821 ifp
->if_mtu
= ETHERMTU
;
822 if (ifp
->if_baudrate
== 0)
823 ifp
->if_baudrate
= 10000000;
824 ifp
->if_output
= ether_output
;
825 ifp
->if_input
= ether_input_internal
;
826 ifp
->if_resolvemulti
= ether_resolvemulti
;
827 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
828 sdl
= IF_LLSOCKADDR(ifp
);
829 sdl
->sdl_type
= IFT_ETHER
;
830 sdl
->sdl_alen
= ifp
->if_addrlen
;
831 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
833 * XXX Keep the current drivers happy.
834 * XXX Remove once all drivers have been cleaned up
836 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
837 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
838 bpfattach(ifp
, dlt
, hdrlen
);
839 if (ng_ether_attach_p
!= NULL
)
840 (*ng_ether_attach_p
)(ifp
);
842 if_printf(ifp
, "MAC address: %6D\n", lla
, ":");
846 * Perform common duties while detaching an Ethernet interface
849 ether_ifdetach(struct ifnet
*ifp
)
853 if (ng_ether_detach_p
!= NULL
)
854 (*ng_ether_detach_p
)(ifp
);
860 ether_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
862 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
863 struct ifreq
*ifr
= (struct ifreq
*) data
;
866 ASSERT_SERIALIZED(ifp
->if_serializer
);
870 ifp
->if_flags
|= IFF_UP
;
872 switch (ifa
->ifa_addr
->sa_family
) {
875 ifp
->if_init(ifp
->if_softc
); /* before arpwhohas */
876 arp_ifinit(ifp
, ifa
);
881 * XXX - This code is probably wrong
885 struct ipx_addr
*ina
= &IA_SIPX(ifa
)->sipx_addr
;
886 struct arpcom
*ac
= IFP2AC(ifp
);
888 if (ipx_nullhost(*ina
))
889 ina
->x_host
= *(union ipx_host
*) ac
->ac_enaddr
;
891 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
892 sizeof ac
->ac_enaddr
);
894 ifp
->if_init(ifp
->if_softc
); /* Set new address. */
900 * XXX - This code is probably wrong
904 struct ns_addr
*ina
= &(IA_SNS(ifa
)->sns_addr
);
905 struct arpcom
*ac
= IFP2AC(ifp
);
907 if (ns_nullhost(*ina
))
908 ina
->x_host
= *(union ns_host
*)(ac
->ac_enaddr
);
910 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
911 sizeof ac
->ac_enaddr
);
916 ifp
->if_init(ifp
->if_softc
);
921 ifp
->if_init(ifp
->if_softc
);
927 bcopy(IFP2AC(ifp
)->ac_enaddr
,
928 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
934 * Set the interface MTU.
936 if (ifr
->ifr_mtu
> ETHERMTU
) {
939 ifp
->if_mtu
= ifr
->ifr_mtu
;
952 struct sockaddr
**llsa
,
955 struct sockaddr_dl
*sdl
;
956 struct sockaddr_in
*sin
;
958 struct sockaddr_in6
*sin6
;
962 switch(sa
->sa_family
) {
965 * No mapping needed. Just check that it's a valid MC address.
967 sdl
= (struct sockaddr_dl
*)sa
;
968 e_addr
= LLADDR(sdl
);
969 if ((e_addr
[0] & 1) != 1)
970 return EADDRNOTAVAIL
;
976 sin
= (struct sockaddr_in
*)sa
;
977 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
978 return EADDRNOTAVAIL
;
979 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
981 sdl
->sdl_len
= sizeof *sdl
;
982 sdl
->sdl_family
= AF_LINK
;
983 sdl
->sdl_index
= ifp
->if_index
;
984 sdl
->sdl_type
= IFT_ETHER
;
985 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
986 e_addr
= LLADDR(sdl
);
987 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
988 *llsa
= (struct sockaddr
*)sdl
;
993 sin6
= (struct sockaddr_in6
*)sa
;
994 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
996 * An IP6 address of 0 means listen to all
997 * of the Ethernet multicast address used for IP6.
998 * (This is used for multicast routers.)
1000 ifp
->if_flags
|= IFF_ALLMULTI
;
1004 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
1005 return EADDRNOTAVAIL
;
1006 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1008 sdl
->sdl_len
= sizeof *sdl
;
1009 sdl
->sdl_family
= AF_LINK
;
1010 sdl
->sdl_index
= ifp
->if_index
;
1011 sdl
->sdl_type
= IFT_ETHER
;
1012 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1013 e_addr
= LLADDR(sdl
);
1014 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
1015 *llsa
= (struct sockaddr
*)sdl
;
1021 * Well, the text isn't quite right, but it's the name
1024 return EAFNOSUPPORT
;
1030 * This is for reference. We have a table-driven version
1031 * of the little-endian crc32 generator, which is faster
1032 * than the double-loop.
1035 ether_crc32_le(const uint8_t *buf
, size_t len
)
1037 uint32_t c
, crc
, carry
;
1040 crc
= 0xffffffffU
; /* initial value */
1042 for (i
= 0; i
< len
; i
++) {
1044 for (j
= 0; j
< 8; j
++) {
1045 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
1049 crc
= (crc
^ ETHER_CRC_POLY_LE
);
1057 ether_crc32_le(const uint8_t *buf
, size_t len
)
1059 static const uint32_t crctab
[] = {
1060 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1061 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1062 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1063 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1068 crc
= 0xffffffffU
; /* initial value */
1070 for (i
= 0; i
< len
; i
++) {
1072 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1073 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1081 ether_crc32_be(const uint8_t *buf
, size_t len
)
1083 uint32_t c
, crc
, carry
;
1086 crc
= 0xffffffffU
; /* initial value */
1088 for (i
= 0; i
< len
; i
++) {
1090 for (j
= 0; j
< 8; j
++) {
1091 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
1095 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
1103 * find the size of ethernet header, and call classifier
1106 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
1107 struct altq_pktattr
*pktattr
)
1109 struct ether_header
*eh
;
1110 uint16_t ether_type
;
1111 int hlen
, af
, hdrsize
;
1114 hlen
= sizeof(struct ether_header
);
1115 eh
= mtod(m
, struct ether_header
*);
1117 ether_type
= ntohs(eh
->ether_type
);
1118 if (ether_type
< ETHERMTU
) {
1120 struct llc
*llc
= (struct llc
*)(eh
+ 1);
1123 if (m
->m_len
< hlen
||
1124 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
1125 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
1126 llc
->llc_control
!= LLC_UI
)
1127 goto bad
; /* not snap! */
1129 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
1132 if (ether_type
== ETHERTYPE_IP
) {
1134 hdrsize
= 20; /* sizeof(struct ip) */
1136 } else if (ether_type
== ETHERTYPE_IPV6
) {
1138 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
1143 while (m
->m_len
<= hlen
) {
1147 hdr
= m
->m_data
+ hlen
;
1148 if (m
->m_len
< hlen
+ hdrsize
) {
1150 * ip header is not in a single mbuf. this should not
1151 * happen in the current code.
1152 * (todo: use m_pulldown in the future)
1158 ifq_classify(ifq
, m
, af
, pktattr
);
1165 pktattr
->pattr_class
= NULL
;
1166 pktattr
->pattr_hdr
= NULL
;
1167 pktattr
->pattr_af
= AF_UNSPEC
;