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.61 2008/05/18 04:38:44 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
42 #include "opt_netgraph.h"
44 #include "opt_ethernet.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/msgport.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/thread.h>
57 #include <sys/thread2.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/if_llc.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/ifq_var.h>
67 #include <net/ethernet.h>
68 #include <net/vlan/if_vlan_ether.h>
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <net/ipfw/ip_fw.h>
75 #include <net/dummynet/ip_dummynet.h>
78 #include <netinet6/nd6.h>
82 #include <netinet/ip_carp.h>
86 #include <netproto/ipx/ipx.h>
87 #include <netproto/ipx/ipx_if.h>
88 int (*ef_inputp
)(struct ifnet
*, const struct ether_header
*eh
, struct mbuf
*m
);
89 int (*ef_outputp
)(struct ifnet
*ifp
, struct mbuf
**mp
, struct sockaddr
*dst
,
90 short *tp
, int *hlen
);
95 #include <netns/ns_if.h>
97 int ether_outputdebug
= 0;
98 int ether_inputdebug
= 0;
102 #include <netproto/atalk/at.h>
103 #include <netproto/atalk/at_var.h>
104 #include <netproto/atalk/at_extern.h>
106 #define llc_snap_org_code llc_un.type_snap.org_code
107 #define llc_snap_ether_type llc_un.type_snap.ether_type
109 extern u_char at_org_code
[3];
110 extern u_char aarp_org_code
[3];
111 #endif /* NETATALK */
113 /* netgraph node hooks for ng_ether(4) */
114 void (*ng_ether_input_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
115 void (*ng_ether_input_orphan_p
)(struct ifnet
*ifp
,
116 struct mbuf
*m
, const struct ether_header
*eh
);
117 int (*ng_ether_output_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
118 void (*ng_ether_attach_p
)(struct ifnet
*ifp
);
119 void (*ng_ether_detach_p
)(struct ifnet
*ifp
);
121 int (*vlan_input_p
)(struct mbuf
*, struct mbuf_chain
*);
123 static int ether_output(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
125 static void ether_restore_header(struct mbuf
**, const struct ether_header
*,
126 const struct ether_header
*);
127 static void ether_demux_chain(struct ifnet
*, struct mbuf
*,
128 struct mbuf_chain
*);
133 struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
134 int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*,
135 struct sockaddr
*, struct rtentry
*);
136 void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
138 static int ether_resolvemulti(struct ifnet
*, struct sockaddr
**,
141 const uint8_t etherbroadcastaddr
[ETHER_ADDR_LEN
] = {
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
145 #define gotoerr(e) do { error = (e); goto bad; } while (0)
146 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
148 static boolean_t
ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
,
150 const struct ether_header
*eh
);
152 static int ether_ipfw
;
153 static u_int ether_restore_hdr
;
154 static u_int ether_prepend_hdr
;
156 SYSCTL_DECL(_net_link
);
157 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
158 SYSCTL_INT(_net_link_ether
, OID_AUTO
, ipfw
, CTLFLAG_RW
,
159 ðer_ipfw
, 0, "Pass ether pkts through firewall");
160 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, restore_hdr
, CTLFLAG_RW
,
161 ðer_restore_hdr
, 0, "# of ether header restoration");
162 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, prepend_hdr
, CTLFLAG_RW
,
163 ðer_prepend_hdr
, 0,
164 "# of ether header restoration which prepends mbuf");
167 * Ethernet output routine.
168 * Encapsulate a packet of type family for the local net.
169 * Use trailer local net encapsulation if enough data in first
170 * packet leaves a multiple of 512 bytes of data in remainder.
171 * Assumes that ifp is actually pointer to arpcom structure.
174 ether_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
177 struct ether_header
*eh
, *deh
;
180 int hlen
= ETHER_HDR_LEN
; /* link layer header length */
181 struct arpcom
*ac
= IFP2AC(ifp
);
184 if (ifp
->if_flags
& IFF_MONITOR
)
186 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
))
189 M_PREPEND(m
, sizeof(struct ether_header
), MB_DONTWAIT
);
192 eh
= mtod(m
, struct ether_header
*);
193 edst
= eh
->ether_dhost
;
196 * Fill in the destination ethernet address and frame type.
198 switch (dst
->sa_family
) {
201 if (!arpresolve(ifp
, rt
, m
, dst
, edst
))
202 return (0); /* if not yet resolved */
203 eh
->ether_type
= htons(ETHERTYPE_IP
);
208 if (!nd6_storelladdr(&ac
->ac_if
, rt
, m
, dst
, edst
))
209 return (0); /* Something bad happenned. */
210 eh
->ether_type
= htons(ETHERTYPE_IPV6
);
215 if (ef_outputp
!= NULL
) {
216 error
= ef_outputp(ifp
, &m
, dst
, &eh
->ether_type
,
221 eh
->ether_type
= htons(ETHERTYPE_IPX
);
222 bcopy(&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
223 edst
, ETHER_ADDR_LEN
);
229 struct at_ifaddr
*aa
;
231 if ((aa
= at_ifawithnet((struct sockaddr_at
*)dst
)) == NULL
) {
236 * In the phase 2 case, need to prepend an mbuf for
237 * the llc header. Since we must preserve the value
238 * of m, which is passed to us by value, we m_copy()
239 * the first mbuf, and use it for our llc header.
241 if (aa
->aa_flags
& AFA_PHASE2
) {
244 M_PREPEND(m
, sizeof(struct llc
), MB_DONTWAIT
);
245 eh
= mtod(m
, struct ether_header
*);
246 edst
= eh
->ether_dhost
;
247 llc
.llc_dsap
= llc
.llc_ssap
= LLC_SNAP_LSAP
;
248 llc
.llc_control
= LLC_UI
;
249 bcopy(at_org_code
, llc
.llc_snap_org_code
,
251 llc
.llc_snap_ether_type
= htons(ETHERTYPE_AT
);
253 mtod(m
, caddr_t
) + sizeof(struct ether_header
),
255 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
256 hlen
= sizeof(struct llc
) + ETHER_HDR_LEN
;
258 eh
->ether_type
= htons(ETHERTYPE_AT
);
260 if (!aarpresolve(ac
, m
, (struct sockaddr_at
*)dst
, edst
))
269 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
270 eh
->ether_type
= 0x8137;
272 case 0x0: /* Novell 802.3 */
273 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
275 case 0xe0e0: /* Novell 802.2 and Token-Ring */
276 M_PREPEND(m
, 3, MB_DONTWAIT
);
277 eh
= mtod(m
, struct ether_header
*);
278 edst
= eh
->ether_dhost
;
279 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
280 cp
= mtod(m
, u_char
*) + sizeof(struct ether_header
);
286 bcopy(&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
), edst
,
289 * XXX if ns_thishost is the same as the node's ethernet
290 * address then just the default code will catch this anyhow.
291 * So I'm not sure if this next clause should be here at all?
294 if (bcmp(edst
, &ns_thishost
, ETHER_ADDR_LEN
) == 0) {
295 m
->m_pkthdr
.rcvif
= ifp
;
296 netisr_dispatch(NETISR_NS
, m
);
299 if (bcmp(edst
, &ns_broadhost
, ETHER_ADDR_LEN
) == 0)
300 m
->m_flags
|= M_BCAST
;
303 case pseudo_AF_HDRCMPLT
:
305 loop_copy
= -1; /* if this is for us, don't do it */
306 deh
= (struct ether_header
*)dst
->sa_data
;
307 memcpy(edst
, deh
->ether_dhost
, ETHER_ADDR_LEN
);
308 eh
->ether_type
= deh
->ether_type
;
312 if_printf(ifp
, "can't handle af%d\n", dst
->sa_family
);
313 gotoerr(EAFNOSUPPORT
);
316 if (dst
->sa_family
== pseudo_AF_HDRCMPLT
) /* unlikely */
317 memcpy(eh
->ether_shost
,
318 ((struct ether_header
*)dst
->sa_data
)->ether_shost
,
321 memcpy(eh
->ether_shost
, ac
->ac_enaddr
, ETHER_ADDR_LEN
);
324 * Bridges require special output handling.
326 if (ifp
->if_bridge
) {
327 KASSERT(bridge_output_p
!= NULL
,
328 ("%s: if_bridge not loaded!", __func__
));
329 lwkt_serialize_enter(ifp
->if_serializer
);
330 error
= bridge_output_p(ifp
, m
, NULL
, NULL
);
331 lwkt_serialize_exit(ifp
->if_serializer
);
336 * If a simplex interface, and the packet is being sent to our
337 * Ethernet address or a broadcast address, loopback a copy.
338 * XXX To make a simplex device behave exactly like a duplex
339 * device, we should copy in the case of sending to our own
340 * ethernet address (thus letting the original actually appear
341 * on the wire). However, we don't do that here for security
342 * reasons and compatibility with the original behavior.
344 if ((ifp
->if_flags
& IFF_SIMPLEX
) && (loop_copy
!= -1)) {
347 if (m
->m_pkthdr
.csum_flags
& CSUM_IP
)
348 csum_flags
|= (CSUM_IP_CHECKED
| CSUM_IP_VALID
);
349 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)
350 csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
);
351 if ((m
->m_flags
& M_BCAST
) || (loop_copy
> 0)) {
354 if ((n
= m_copypacket(m
, MB_DONTWAIT
)) != NULL
) {
355 n
->m_pkthdr
.csum_flags
|= csum_flags
;
356 if (csum_flags
& CSUM_DATA_VALID
)
357 n
->m_pkthdr
.csum_data
= 0xffff;
358 if_simloop(ifp
, n
, dst
->sa_family
, hlen
);
361 } else if (bcmp(eh
->ether_dhost
, eh
->ether_shost
,
362 ETHER_ADDR_LEN
) == 0) {
363 m
->m_pkthdr
.csum_flags
|= csum_flags
;
364 if (csum_flags
& CSUM_DATA_VALID
)
365 m
->m_pkthdr
.csum_data
= 0xffff;
366 if_simloop(ifp
, m
, dst
->sa_family
, hlen
);
367 return (0); /* XXX */
372 if (ifp
->if_carp
&& (error
= carp_output(ifp
, m
, dst
, NULL
)))
377 /* Handle ng_ether(4) processing, if any */
378 if (ng_ether_output_p
!= NULL
) {
379 if ((error
= (*ng_ether_output_p
)(ifp
, &m
)) != 0)
385 /* Continue with link-layer output */
386 return ether_output_frame(ifp
, m
);
394 * Ethernet link layer output routine to send a raw frame to the device.
396 * This assumes that the 14 byte Ethernet header is present and contiguous
400 ether_output_frame(struct ifnet
*ifp
, struct mbuf
*m
)
402 struct ip_fw
*rule
= NULL
;
404 struct altq_pktattr pktattr
;
407 /* Extract info from dummynet tag */
408 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
410 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
412 m_tag_delete(m
, mtag
);
416 if (ifq_is_enabled(&ifp
->if_snd
))
417 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
419 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
420 struct ether_header save_eh
, *eh
;
422 eh
= mtod(m
, struct ether_header
*);
424 m_adj(m
, ETHER_HDR_LEN
);
425 if (!ether_ipfw_chk(&m
, ifp
, &rule
, eh
)) {
429 return ENOBUFS
; /* pkt dropped */
431 return 0; /* consumed e.g. in a pipe */
434 /* packet was ok, restore the ethernet header */
435 ether_restore_header(&m
, eh
, &save_eh
);
444 * Queue message on interface, update output statistics if
445 * successful, and start output if interface not yet active.
447 error
= ifq_dispatch(ifp
, m
, &pktattr
);
452 * ipfw processing for ethernet packets (in and out).
453 * The second parameter is NULL from ether_demux(), and ifp from
454 * ether_output_frame().
457 ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
, struct ip_fw
**rule
,
458 const struct ether_header
*eh
)
460 struct ether_header save_eh
= *eh
; /* might be a ptr in m */
461 struct ip_fw_args args
;
465 if (*rule
!= NULL
&& fw_one_pass
)
466 return TRUE
; /* dummynet packet, already partially processed */
469 * I need some amount of data to be contiguous.
471 i
= min((*m0
)->m_pkthdr
.len
, max_protohdr
);
472 if ((*m0
)->m_len
< i
) {
473 *m0
= m_pullup(*m0
, i
);
478 args
.m
= *m0
; /* the packet we are looking at */
479 args
.oif
= dst
; /* destination, if any */
480 if ((mtag
= m_tag_find(*m0
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
481 m_tag_delete(*m0
, mtag
);
482 args
.rule
= *rule
; /* matching rule to restart */
483 args
.next_hop
= NULL
; /* we do not support forward yet */
484 args
.eh
= &save_eh
; /* MAC header for bridged/MAC packets */
485 i
= ip_fw_chk_ptr(&args
);
489 if ((i
& IP_FW_PORT_DENY_FLAG
) || *m0
== NULL
) /* drop */
492 if (i
== 0) /* a PASS rule. */
495 if (i
& IP_FW_PORT_DYNT_FLAG
) {
497 * Pass the pkt to dummynet, which consumes it.
501 m
= *m0
; /* pass the original to dummynet */
502 *m0
= NULL
; /* and nothing back to the caller */
504 ether_restore_header(&m
, eh
, &save_eh
);
508 ip_fw_dn_io_ptr(m
, (i
& 0xffff),
509 dst
? DN_TO_ETH_OUT
: DN_TO_ETH_DEMUX
, &args
);
513 * XXX at some point add support for divert/forward actions.
514 * If none of the above matches, we have to drop the pkt.
520 * Process a received Ethernet packet.
522 * The ethernet header is assumed to be in the mbuf so the caller
523 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
524 * bytes in the first mbuf.
526 * This allows us to concentrate in one place a bunch of code which
527 * is replicated in all device drivers. Also, many functions called
528 * from ether_input() try to put the eh back into the mbuf, so we
529 * can later propagate the 'contiguous packet' interface to them.
531 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
532 * cluster, right before m_data. So be very careful when working on m,
533 * as you could destroy *eh !!
535 * First we perform any link layer operations, then continue to the
536 * upper layers with ether_demux().
539 ether_input_chain(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
541 struct ether_header
*eh
;
543 ASSERT_SERIALIZED(ifp
->if_serializer
);
546 /* Discard packet if interface is not up */
547 if (!(ifp
->if_flags
& IFF_UP
)) {
552 if (m
->m_len
< sizeof(struct ether_header
)) {
553 /* XXX error in the caller. */
557 eh
= mtod(m
, struct ether_header
*);
559 if (ntohs(eh
->ether_type
) == ETHERTYPE_VLAN
&&
560 (m
->m_flags
& M_VLANTAG
) == 0) {
562 * Extract vlan tag if hardware does not do it for us
564 vlan_ether_decap(&m
);
567 eh
= mtod(m
, struct ether_header
*);
570 m
->m_pkthdr
.rcvif
= ifp
;
572 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
573 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
574 ifp
->if_addrlen
) == 0)
575 m
->m_flags
|= M_BCAST
;
577 m
->m_flags
|= M_MCAST
;
581 ETHER_BPF_MTAP(ifp
, m
);
583 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
585 if (ifp
->if_flags
& IFF_MONITOR
) {
587 * Interface marked for monitoring; discard packet.
594 * Tap the packet off here for a bridge. bridge_input()
595 * will return NULL if it has consumed the packet, otherwise
596 * it gets processed as normal. Note that bridge_input()
597 * will always return the original packet if we need to
598 * process it locally.
600 if (ifp
->if_bridge
) {
601 KASSERT(bridge_input_p
!= NULL
,
602 ("%s: if_bridge not loaded!", __func__
));
604 if(m
->m_flags
& M_PROTO1
) {
605 m
->m_flags
&= ~M_PROTO1
;
607 /* clear M_PROMISC, in case the packets comes from a vlan */
608 /* m->m_flags &= ~M_PROMISC; */
609 lwkt_serialize_exit(ifp
->if_serializer
);
610 m
= (*bridge_input_p
)(ifp
, m
);
611 lwkt_serialize_enter(ifp
->if_serializer
);
615 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
616 ("bridge_input_p changed rcvif\n"));
618 /* 'm' may be changed by bridge_input_p() */
619 eh
= mtod(m
, struct ether_header
*);
623 /* Handle ng_ether(4) processing, if any */
624 if (ng_ether_input_p
!= NULL
) {
625 ng_ether_input_p(ifp
, &m
);
629 /* 'm' may be changed by ng_ether_input_p() */
630 eh
= mtod(m
, struct ether_header
*);
633 /* Continue with upper layer processing */
634 ether_demux_chain(ifp
, m
, chain
);
638 ether_input(struct ifnet
*ifp
, struct mbuf
*m
)
640 ether_input_chain(ifp
, m
, NULL
);
644 * Upper layer processing for a received Ethernet packet.
647 ether_demux_chain(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
649 struct ether_header save_eh
, *eh
;
652 struct ip_fw
*rule
= NULL
;
659 KASSERT(m
->m_len
>= ETHER_HDR_LEN
,
660 ("ether header is no contiguous!\n"));
662 eh
= mtod(m
, struct ether_header
*);
665 /* XXX old crufty stuff, needs to be removed */
666 m_adj(m
, sizeof(struct ether_header
));
668 /* Extract info from dummynet tag */
669 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
671 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
672 KKASSERT(ifp
== NULL
);
673 ifp
= m
->m_pkthdr
.rcvif
;
675 m_tag_delete(m
, mtag
);
678 if (rule
) /* packet is passing the second time */
683 * XXX: Okay, we need to call carp_forus() and - if it is for
684 * us jump over code that does the normal check
685 * "ac_enaddr == ether_dhost". The check sequence is a bit
686 * different from OpenBSD, so we jump over as few code as
687 * possible, to catch _all_ sanity checks. This needs
688 * evaluation, to see if the carp ether_dhost values break any
691 if (ifp
->if_carp
&& carp_forus(ifp
->if_carp
, eh
->ether_dhost
))
696 * Discard packet if upper layers shouldn't see it because
697 * it was unicast to a different Ethernet address. If the
698 * driver is working properly, then this situation can only
699 * happen when the interface is in promiscuous mode.
701 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
702 (eh
->ether_dhost
[0] & 1) == 0 &&
703 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
709 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
710 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
)) {
716 ether_type
= ntohs(save_eh
.ether_type
);
718 if (m
->m_flags
& M_VLANTAG
) {
719 if (ether_type
== ETHERTYPE_VLAN
) {
721 * To prevent possible dangerous recursion,
722 * we don't do vlan-in-vlan
724 m
->m_pkthdr
.rcvif
->if_noproto
++;
728 if (vlan_input_p
!= NULL
) {
729 ether_restore_header(&m
, eh
, &save_eh
);
731 vlan_input_p(m
, chain
);
733 m
->m_pkthdr
.rcvif
->if_noproto
++;
738 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
740 switch (ether_type
) {
743 if (ipflow_fastforward(m
, ifp
->if_serializer
))
749 if (ifp
->if_flags
& IFF_NOARP
) {
750 /* Discard packet if ARP is disabled on interface */
766 if (ef_inputp
&& ef_inputp(ifp
, &save_eh
, m
) == 0)
773 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
790 if (ef_inputp
&& ef_inputp(ifp
, &save_eh
, m
) == 0)
794 checksum
= mtod(m
, ushort
*);
796 if ((ether_type
<= ETHERMTU
) &&
797 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
798 if (*checksum
== 0xE0E0) {
799 m
->m_pkthdr
.len
-= 3;
808 if (ether_type
> ETHERMTU
)
810 l
= mtod(m
, struct llc
*);
811 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
812 l
->llc_ssap
== LLC_SNAP_LSAP
&&
813 l
->llc_control
== LLC_UI
) {
814 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
815 sizeof at_org_code
) == 0 &&
816 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
817 m_adj(m
, sizeof(struct llc
));
821 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
822 sizeof aarp_org_code
) == 0 &&
823 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
824 m_adj(m
, sizeof(struct llc
));
831 if (ng_ether_input_orphan_p
!= NULL
)
832 (*ng_ether_input_orphan_p
)(ifp
, m
, &save_eh
);
838 #ifdef ETHER_INPUT_CHAIN
840 struct mbuf_chain
*c
;
844 port
= netisr_mport(isr
, &m
);
848 m
->m_pkthdr
.header
= port
; /* XXX */
849 cpuid
= port
->mpu_td
->td_gd
->gd_cpuid
;
852 if (c
->mc_head
== NULL
) {
853 c
->mc_head
= c
->mc_tail
= m
;
855 c
->mc_tail
->m_nextpkt
= m
;
860 #endif /* ETHER_INPUT_CHAIN */
861 netisr_dispatch(isr
, m
);
865 ether_demux(struct ifnet
*ifp
, struct mbuf
*m
)
867 ether_demux_chain(ifp
, m
, NULL
);
871 * Perform common duties while attaching to interface list
875 ether_ifattach(struct ifnet
*ifp
, uint8_t *lla
, lwkt_serialize_t serializer
)
877 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
882 ether_ifattach_bpf(struct ifnet
*ifp
, uint8_t *lla
, u_int dlt
, u_int hdrlen
,
883 lwkt_serialize_t serializer
)
885 struct sockaddr_dl
*sdl
;
887 ifp
->if_type
= IFT_ETHER
;
888 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
889 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
890 if_attach(ifp
, serializer
);
891 ifp
->if_mtu
= ETHERMTU
;
892 if (ifp
->if_baudrate
== 0)
893 ifp
->if_baudrate
= 10000000;
894 ifp
->if_output
= ether_output
;
895 ifp
->if_input
= ether_input
;
896 ifp
->if_resolvemulti
= ether_resolvemulti
;
897 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
898 sdl
= IF_LLSOCKADDR(ifp
);
899 sdl
->sdl_type
= IFT_ETHER
;
900 sdl
->sdl_alen
= ifp
->if_addrlen
;
901 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
903 * XXX Keep the current drivers happy.
904 * XXX Remove once all drivers have been cleaned up
906 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
907 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
908 bpfattach(ifp
, dlt
, hdrlen
);
909 if (ng_ether_attach_p
!= NULL
)
910 (*ng_ether_attach_p
)(ifp
);
912 if_printf(ifp
, "MAC address: %6D\n", lla
, ":");
916 * Perform common duties while detaching an Ethernet interface
919 ether_ifdetach(struct ifnet
*ifp
)
923 if (ng_ether_detach_p
!= NULL
)
924 (*ng_ether_detach_p
)(ifp
);
930 ether_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
932 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
933 struct ifreq
*ifr
= (struct ifreq
*) data
;
936 #define IF_INIT(ifp) \
938 if (((ifp)->if_flags & IFF_UP) == 0) { \
939 (ifp)->if_flags |= IFF_UP; \
940 (ifp)->if_init((ifp)->if_softc); \
944 ASSERT_SERIALIZED(ifp
->if_serializer
);
948 switch (ifa
->ifa_addr
->sa_family
) {
951 IF_INIT(ifp
); /* before arpwhohas */
952 arp_ifinit(ifp
, ifa
);
957 * XXX - This code is probably wrong
961 struct ipx_addr
*ina
= &IA_SIPX(ifa
)->sipx_addr
;
962 struct arpcom
*ac
= IFP2AC(ifp
);
964 if (ipx_nullhost(*ina
))
965 ina
->x_host
= *(union ipx_host
*) ac
->ac_enaddr
;
967 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
968 sizeof ac
->ac_enaddr
);
970 IF_INIT(ifp
); /* Set new address. */
976 * XXX - This code is probably wrong
980 struct ns_addr
*ina
= &(IA_SNS(ifa
)->sns_addr
);
981 struct arpcom
*ac
= IFP2AC(ifp
);
983 if (ns_nullhost(*ina
))
984 ina
->x_host
= *(union ns_host
*)(ac
->ac_enaddr
);
986 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
987 sizeof ac
->ac_enaddr
);
1003 bcopy(IFP2AC(ifp
)->ac_enaddr
,
1004 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
1010 * Set the interface MTU.
1012 if (ifr
->ifr_mtu
> ETHERMTU
) {
1015 ifp
->if_mtu
= ifr
->ifr_mtu
;
1030 struct sockaddr
**llsa
,
1031 struct sockaddr
*sa
)
1033 struct sockaddr_dl
*sdl
;
1034 struct sockaddr_in
*sin
;
1036 struct sockaddr_in6
*sin6
;
1040 switch(sa
->sa_family
) {
1043 * No mapping needed. Just check that it's a valid MC address.
1045 sdl
= (struct sockaddr_dl
*)sa
;
1046 e_addr
= LLADDR(sdl
);
1047 if ((e_addr
[0] & 1) != 1)
1048 return EADDRNOTAVAIL
;
1054 sin
= (struct sockaddr_in
*)sa
;
1055 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
1056 return EADDRNOTAVAIL
;
1057 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1059 sdl
->sdl_len
= sizeof *sdl
;
1060 sdl
->sdl_family
= AF_LINK
;
1061 sdl
->sdl_index
= ifp
->if_index
;
1062 sdl
->sdl_type
= IFT_ETHER
;
1063 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1064 e_addr
= LLADDR(sdl
);
1065 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
1066 *llsa
= (struct sockaddr
*)sdl
;
1071 sin6
= (struct sockaddr_in6
*)sa
;
1072 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
1074 * An IP6 address of 0 means listen to all
1075 * of the Ethernet multicast address used for IP6.
1076 * (This is used for multicast routers.)
1078 ifp
->if_flags
|= IFF_ALLMULTI
;
1082 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
1083 return EADDRNOTAVAIL
;
1084 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1086 sdl
->sdl_len
= sizeof *sdl
;
1087 sdl
->sdl_family
= AF_LINK
;
1088 sdl
->sdl_index
= ifp
->if_index
;
1089 sdl
->sdl_type
= IFT_ETHER
;
1090 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1091 e_addr
= LLADDR(sdl
);
1092 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
1093 *llsa
= (struct sockaddr
*)sdl
;
1099 * Well, the text isn't quite right, but it's the name
1102 return EAFNOSUPPORT
;
1108 * This is for reference. We have a table-driven version
1109 * of the little-endian crc32 generator, which is faster
1110 * than the double-loop.
1113 ether_crc32_le(const uint8_t *buf
, size_t len
)
1115 uint32_t c
, crc
, carry
;
1118 crc
= 0xffffffffU
; /* initial value */
1120 for (i
= 0; i
< len
; i
++) {
1122 for (j
= 0; j
< 8; j
++) {
1123 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
1127 crc
= (crc
^ ETHER_CRC_POLY_LE
);
1135 ether_crc32_le(const uint8_t *buf
, size_t len
)
1137 static const uint32_t crctab
[] = {
1138 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1139 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1140 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1141 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1146 crc
= 0xffffffffU
; /* initial value */
1148 for (i
= 0; i
< len
; i
++) {
1150 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1151 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1159 ether_crc32_be(const uint8_t *buf
, size_t len
)
1161 uint32_t c
, crc
, carry
;
1164 crc
= 0xffffffffU
; /* initial value */
1166 for (i
= 0; i
< len
; i
++) {
1168 for (j
= 0; j
< 8; j
++) {
1169 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
1173 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
1181 * find the size of ethernet header, and call classifier
1184 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
1185 struct altq_pktattr
*pktattr
)
1187 struct ether_header
*eh
;
1188 uint16_t ether_type
;
1189 int hlen
, af
, hdrsize
;
1192 hlen
= sizeof(struct ether_header
);
1193 eh
= mtod(m
, struct ether_header
*);
1195 ether_type
= ntohs(eh
->ether_type
);
1196 if (ether_type
< ETHERMTU
) {
1198 struct llc
*llc
= (struct llc
*)(eh
+ 1);
1201 if (m
->m_len
< hlen
||
1202 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
1203 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
1204 llc
->llc_control
!= LLC_UI
)
1205 goto bad
; /* not snap! */
1207 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
1210 if (ether_type
== ETHERTYPE_IP
) {
1212 hdrsize
= 20; /* sizeof(struct ip) */
1214 } else if (ether_type
== ETHERTYPE_IPV6
) {
1216 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
1221 while (m
->m_len
<= hlen
) {
1225 hdr
= m
->m_data
+ hlen
;
1226 if (m
->m_len
< hlen
+ hdrsize
) {
1228 * ip header is not in a single mbuf. this should not
1229 * happen in the current code.
1230 * (todo: use m_pulldown in the future)
1236 ifq_classify(ifq
, m
, af
, pktattr
);
1243 pktattr
->pattr_class
= NULL
;
1244 pktattr
->pattr_hdr
= NULL
;
1245 pktattr
->pattr_af
= AF_UNSPEC
;
1249 ether_restore_header(struct mbuf
**m0
, const struct ether_header
*eh
,
1250 const struct ether_header
*save_eh
)
1252 struct mbuf
*m
= *m0
;
1254 ether_restore_hdr
++;
1257 * Prepend the header, optimize for the common case of
1258 * eh pointing into the mbuf.
1260 if ((const void *)(eh
+ 1) == (void *)m
->m_data
) {
1261 m
->m_data
-= ETHER_HDR_LEN
;
1262 m
->m_len
+= ETHER_HDR_LEN
;
1263 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1265 ether_prepend_hdr
++;
1267 M_PREPEND(m
, ETHER_HDR_LEN
, MB_DONTWAIT
);
1269 bcopy(save_eh
, mtod(m
, struct ether_header
*),
1276 #ifdef ETHER_INPUT_CHAIN
1279 ether_input_ipifunc(void *arg
)
1281 struct mbuf
*m
, *next
;
1286 next
= m
->m_nextpkt
;
1287 m
->m_nextpkt
= NULL
;
1289 port
= m
->m_pkthdr
.header
;
1290 m
->m_pkthdr
.header
= NULL
;
1293 &m
->m_hdr
.mh_netmsg
.nm_netmsg
.nm_lmsg
);
1296 } while (m
!= NULL
);
1300 ether_input_dispatch(struct mbuf_chain
*chain
)
1305 for (i
= 0; i
< ncpus
; ++i
) {
1306 if (chain
[i
].mc_head
!= NULL
) {
1307 lwkt_send_ipiq(globaldata_find(i
),
1308 ether_input_ipifunc
, chain
[i
].mc_head
);
1312 ether_input_ipifunc(chain
->mc_head
);
1316 #endif /* ETHER_INPUT_CHAIN */