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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
34 #include "opt_inet6.h"
36 #include "opt_netgraph.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/globaldata.h>
43 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/msgport.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/thread.h>
54 #include <sys/thread2.h>
55 #include <sys/mplock2.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
60 #include <net/if_llc.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/ifq_var.h>
65 #include <net/ethernet.h>
66 #include <net/vlan/if_vlan_ether.h>
67 #include <net/vlan/if_vlan_var.h>
68 #include <net/netmsg2.h>
69 #include <net/netisr2.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/tcp_var.h>
75 #include <netinet/if_ether.h>
76 #include <netinet/ip_flow.h>
77 #include <net/ipfw/ip_fw.h>
78 #include <net/ipfw3/ip_fw.h>
79 #include <net/dummynet/ip_dummynet.h>
82 #include <netinet6/nd6.h>
86 #include <netinet/ip_carp.h>
90 #include <netproto/mpls/mpls.h>
93 /* netgraph node hooks for ng_ether(4) */
94 void (*ng_ether_input_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
95 void (*ng_ether_input_orphan_p
)(struct ifnet
*ifp
, struct mbuf
*m
);
96 int (*ng_ether_output_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
97 void (*ng_ether_attach_p
)(struct ifnet
*ifp
);
98 void (*ng_ether_detach_p
)(struct ifnet
*ifp
);
100 void (*vlan_input_p
)(struct mbuf
*);
102 static int ether_output(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
104 static void ether_restore_header(struct mbuf
**, const struct ether_header
*,
105 const struct ether_header
*);
106 static int ether_characterize(struct mbuf
**);
107 static void ether_dispatch(struct ifnet
*, int, struct mbuf
*, int);
112 struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
113 int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*);
114 void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
115 struct ifnet
*(*bridge_interface_p
)(void *if_bridge
);
117 static int ether_resolvemulti(struct ifnet
*, struct sockaddr
**,
123 void (*lagg_input_p
)(struct ifnet
*, struct mbuf
*);
124 int (*lagg_output_p
)(struct ifnet
*, struct mbuf
*);
126 const uint8_t etherbroadcastaddr
[ETHER_ADDR_LEN
] = {
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
130 #define gotoerr(e) do { error = (e); goto bad; } while (0)
131 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
133 static boolean_t
ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
,
135 const struct ether_header
*eh
);
137 static int ether_ipfw
;
138 static u_long ether_restore_hdr
;
139 static u_long ether_prepend_hdr
;
140 static u_long ether_input_wronghash
;
141 static int ether_debug
;
144 static u_long ether_pktinfo_try
;
145 static u_long ether_pktinfo_hit
;
146 static u_long ether_rss_nopi
;
147 static u_long ether_rss_nohash
;
148 static u_long ether_input_requeue
;
150 static u_long ether_input_wronghwhash
;
151 static int ether_input_ckhash
;
153 #define ETHER_TSOLEN_DEFAULT (4 * ETHERMTU)
155 #define ETHER_NMBCLUSTERS_DEFMIN 32
156 #define ETHER_NMBCLUSTERS_DEFAULT 256
158 static int ether_tsolen_default
= ETHER_TSOLEN_DEFAULT
;
159 TUNABLE_INT("net.link.ether.tsolen", ðer_tsolen_default
);
161 static int ether_nmbclusters_default
= ETHER_NMBCLUSTERS_DEFAULT
;
162 TUNABLE_INT("net.link.ether.nmbclusters", ðer_nmbclusters_default
);
164 SYSCTL_DECL(_net_link
);
165 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
166 SYSCTL_INT(_net_link_ether
, OID_AUTO
, debug
, CTLFLAG_RW
,
167 ðer_debug
, 0, "Ether debug");
168 SYSCTL_INT(_net_link_ether
, OID_AUTO
, ipfw
, CTLFLAG_RW
,
169 ðer_ipfw
, 0, "Pass ether pkts through firewall");
170 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, restore_hdr
, CTLFLAG_RW
,
171 ðer_restore_hdr
, 0, "# of ether header restoration");
172 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, prepend_hdr
, CTLFLAG_RW
,
173 ðer_prepend_hdr
, 0,
174 "# of ether header restoration which prepends mbuf");
175 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, input_wronghash
, CTLFLAG_RW
,
176 ðer_input_wronghash
, 0, "# of input packets with wrong hash");
177 SYSCTL_INT(_net_link_ether
, OID_AUTO
, tsolen
, CTLFLAG_RW
,
178 ðer_tsolen_default
, 0, "Default max TSO length");
181 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, rss_nopi
, CTLFLAG_RW
,
182 ðer_rss_nopi
, 0, "# of packets do not have pktinfo");
183 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, rss_nohash
, CTLFLAG_RW
,
184 ðer_rss_nohash
, 0, "# of packets do not have hash");
185 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, pktinfo_try
, CTLFLAG_RW
,
186 ðer_pktinfo_try
, 0,
187 "# of tries to find packets' msgport using pktinfo");
188 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, pktinfo_hit
, CTLFLAG_RW
,
189 ðer_pktinfo_hit
, 0,
190 "# of packets whose msgport are found using pktinfo");
191 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, input_requeue
, CTLFLAG_RW
,
192 ðer_input_requeue
, 0, "# of input packets gets requeued");
194 SYSCTL_ULONG(_net_link_ether
, OID_AUTO
, input_wronghwhash
, CTLFLAG_RW
,
195 ðer_input_wronghwhash
, 0, "# of input packets with wrong hw hash");
196 SYSCTL_INT(_net_link_ether
, OID_AUTO
, always_ckhash
, CTLFLAG_RW
,
197 ðer_input_ckhash
, 0, "always check hash");
199 #define ETHER_KTR_STR "ifp=%p"
200 #define ETHER_KTR_ARGS struct ifnet *ifp
202 #define KTR_ETHERNET KTR_ALL
204 KTR_INFO_MASTER(ether
);
205 KTR_INFO(KTR_ETHERNET
, ether
, pkt_beg
, 0, ETHER_KTR_STR
, ETHER_KTR_ARGS
);
206 KTR_INFO(KTR_ETHERNET
, ether
, pkt_end
, 1, ETHER_KTR_STR
, ETHER_KTR_ARGS
);
207 KTR_INFO(KTR_ETHERNET
, ether
, disp_beg
, 2, ETHER_KTR_STR
, ETHER_KTR_ARGS
);
208 KTR_INFO(KTR_ETHERNET
, ether
, disp_end
, 3, ETHER_KTR_STR
, ETHER_KTR_ARGS
);
209 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
212 * Ethernet output routine.
213 * Encapsulate a packet of type family for the local net.
214 * Use trailer local net encapsulation if enough data in first
215 * packet leaves a multiple of 512 bytes of data in remainder.
216 * Assumes that ifp is actually pointer to arpcom structure.
219 ether_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
222 struct ether_header
*eh
, *deh
;
225 int hlen
= ETHER_HDR_LEN
; /* link layer header length */
226 struct arpcom
*ac
= IFP2AC(ifp
);
229 ASSERT_NETISR_NCPUS(mycpuid
);
230 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp
);
232 if (ifp
->if_flags
& IFF_MONITOR
)
234 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
))
237 M_PREPEND(m
, sizeof(struct ether_header
), M_NOWAIT
);
240 m
->m_pkthdr
.csum_lhlen
= sizeof(struct ether_header
);
241 eh
= mtod(m
, struct ether_header
*);
242 edst
= eh
->ether_dhost
;
245 * Fill in the destination ethernet address and frame type.
247 switch (dst
->sa_family
) {
250 error
= arpresolve(ifp
, rt
, m
, dst
, edst
);
252 return error
== EWOULDBLOCK
? 0 : error
;
254 if (m
->m_flags
& M_MPLSLABELED
)
255 eh
->ether_type
= htons(ETHERTYPE_MPLS
);
258 eh
->ether_type
= htons(ETHERTYPE_IP
);
264 ah
= mtod(m
, struct arphdr
*);
265 ah
->ar_hrd
= htons(ARPHRD_ETHER
);
267 loop_copy
= -1; /* if this is for us, don't do it */
269 switch(ntohs(ah
->ar_op
)) {
270 case ARPOP_REVREQUEST
:
272 eh
->ether_type
= htons(ETHERTYPE_REVARP
);
277 eh
->ether_type
= htons(ETHERTYPE_ARP
);
281 if (m
->m_flags
& M_BCAST
)
282 bcopy(ifp
->if_broadcastaddr
, edst
, ETHER_ADDR_LEN
);
284 bcopy(ar_tha(ah
), edst
, ETHER_ADDR_LEN
);
289 error
= nd6_resolve(&ac
->ac_if
, rt
, m
, dst
, edst
);
291 return error
== EWOULDBLOCK
? 0 : error
;
292 eh
->ether_type
= htons(ETHERTYPE_IPV6
);
295 case pseudo_AF_HDRCMPLT
:
297 loop_copy
= -1; /* if this is for us, don't do it */
298 deh
= (struct ether_header
*)dst
->sa_data
;
299 memcpy(edst
, deh
->ether_dhost
, ETHER_ADDR_LEN
);
300 eh
->ether_type
= deh
->ether_type
;
304 if_printf(ifp
, "can't handle af%d\n", dst
->sa_family
);
305 gotoerr(EAFNOSUPPORT
);
308 if (dst
->sa_family
== pseudo_AF_HDRCMPLT
) /* unlikely */
309 memcpy(eh
->ether_shost
,
310 ((struct ether_header
*)dst
->sa_data
)->ether_shost
,
313 memcpy(eh
->ether_shost
, ac
->ac_enaddr
, ETHER_ADDR_LEN
);
316 * Bridges require special output handling.
318 if (ifp
->if_bridge
) {
319 KASSERT(bridge_output_p
!= NULL
,
320 ("%s: if_bridge not loaded!", __func__
));
321 return bridge_output_p(ifp
, m
);
325 KASSERT(lagg_output_p
!= NULL
,
326 ("%s: if_lagg not loaded!", __func__
));
327 return lagg_output_p(ifp
, m
);
332 * If a simplex interface, and the packet is being sent to our
333 * Ethernet address or a broadcast address, loopback a copy.
334 * XXX To make a simplex device behave exactly like a duplex
335 * device, we should copy in the case of sending to our own
336 * ethernet address (thus letting the original actually appear
337 * on the wire). However, we don't do that here for security
338 * reasons and compatibility with the original behavior.
340 if ((ifp
->if_flags
& IFF_SIMPLEX
) && (loop_copy
!= -1)) {
343 if (m
->m_pkthdr
.csum_flags
& CSUM_IP
)
344 csum_flags
|= (CSUM_IP_CHECKED
| CSUM_IP_VALID
);
345 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)
346 csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
);
347 if ((m
->m_flags
& M_BCAST
) || (loop_copy
> 0)) {
350 if ((n
= m_copypacket(m
, M_NOWAIT
)) != NULL
) {
351 n
->m_pkthdr
.csum_flags
|= csum_flags
;
352 if (csum_flags
& CSUM_DATA_VALID
)
353 n
->m_pkthdr
.csum_data
= 0xffff;
354 if_simloop(ifp
, n
, dst
->sa_family
, hlen
);
356 IFNET_STAT_INC(ifp
, iqdrops
, 1);
357 } else if (bcmp(eh
->ether_dhost
, eh
->ether_shost
,
358 ETHER_ADDR_LEN
) == 0) {
359 m
->m_pkthdr
.csum_flags
|= csum_flags
;
360 if (csum_flags
& CSUM_DATA_VALID
)
361 m
->m_pkthdr
.csum_data
= 0xffff;
362 if_simloop(ifp
, m
, dst
->sa_family
, hlen
);
363 return (0); /* XXX */
368 if (ifp
->if_type
== IFT_CARP
) {
369 ifp
= carp_parent(ifp
);
371 gotoerr(ENETUNREACH
);
376 * Check precondition again
378 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp
);
380 if (ifp
->if_flags
& IFF_MONITOR
)
382 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
383 (IFF_UP
| IFF_RUNNING
))
388 /* Handle ng_ether(4) processing, if any */
389 if (ng_ether_output_p
!= NULL
) {
391 * Hold BGL and recheck ng_ether_output_p
394 if (ng_ether_output_p
!= NULL
) {
395 if ((error
= ng_ether_output_p(ifp
, &m
)) != 0) {
407 /* Continue with link-layer output */
408 return ether_output_frame(ifp
, m
);
416 * Returns the bridge interface an ifp is associated
419 * Only call if ifp->if_bridge != NULL.
422 ether_bridge_interface(struct ifnet
*ifp
)
424 if (bridge_interface_p
)
425 return(bridge_interface_p(ifp
->if_bridge
));
430 * Ethernet link layer output routine to send a raw frame to the device.
432 * This assumes that the 14 byte Ethernet header is present and contiguous
436 ether_output_frame(struct ifnet
*ifp
, struct mbuf
*m
)
438 struct ip_fw
*rule
= NULL
;
440 struct altq_pktattr pktattr
;
442 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp
);
444 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
447 /* Extract info from dummynet tag */
448 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
449 KKASSERT(mtag
!= NULL
);
450 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
451 KKASSERT(rule
!= NULL
);
453 m_tag_delete(m
, mtag
);
454 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
457 if (ifq_is_enabled(&ifp
->if_snd
))
458 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
460 if ((IPFW_LOADED
|| IPFW3_LOADED
) && ether_ipfw
!= 0) {
461 struct ether_header save_eh
, *eh
;
463 eh
= mtod(m
, struct ether_header
*);
465 m_adj(m
, ETHER_HDR_LEN
);
466 if (!ether_ipfw_chk(&m
, ifp
, &rule
, eh
)) {
470 return ENOBUFS
; /* pkt dropped */
472 return 0; /* consumed e.g. in a pipe */
475 /* packet was ok, restore the ethernet header */
476 ether_restore_header(&m
, eh
, &save_eh
);
485 * Queue message on interface, update output statistics if
486 * successful, and start output if interface not yet active.
488 error
= ifq_dispatch(ifp
, m
, &pktattr
);
493 * ipfw processing for ethernet packets (in and out).
494 * The second parameter is NULL from ether_demux(), and ifp from
495 * ether_output_frame().
498 ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
, struct ip_fw
**rule
,
499 const struct ether_header
*eh
)
501 struct ether_header save_eh
= *eh
; /* might be a ptr in *m0 */
502 struct ip_fw_args args
;
507 if (*rule
!= NULL
&& fw_one_pass
)
508 return TRUE
; /* dummynet packet, already partially processed */
511 * I need some amount of data to be contiguous.
513 i
= min((*m0
)->m_pkthdr
.len
, max_protohdr
);
514 if ((*m0
)->m_len
< i
) {
515 *m0
= m_pullup(*m0
, i
);
523 if ((mtag
= m_tag_find(*m0
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
524 m_tag_delete(*m0
, mtag
);
525 if ((*m0
)->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
526 mtag
= m_tag_find(*m0
, PACKET_TAG_IPFORWARD
, NULL
);
527 KKASSERT(mtag
!= NULL
);
528 m_tag_delete(*m0
, mtag
);
529 (*m0
)->m_pkthdr
.fw_flags
&= ~IPFORWARD_MBUF_TAGGED
;
534 args
.m
= *m0
; /* the packet we are looking at */
535 args
.oif
= dst
; /* destination, if any */
536 args
.rule
= *rule
; /* matching rule to restart */
537 args
.eh
= &save_eh
; /* MAC header for bridged/MAC packets */
538 i
= ip_fw_chk_ptr(&args
);
553 * XXX at some point add support for divert/forward actions.
554 * If none of the above matches, we have to drop the pkt.
560 * Pass the pkt to dummynet, which consumes it.
562 m
= *m0
; /* pass the original to dummynet */
563 *m0
= NULL
; /* and nothing back to the caller */
565 ether_restore_header(&m
, eh
, &save_eh
);
569 m
= ip_fw_dn_io_ptr(m
, args
.cookie
,
570 dst
? DN_TO_ETH_OUT
: DN_TO_ETH_DEMUX
, &args
);
576 panic("unknown ipfw return value: %d", i
);
581 * Perform common duties while attaching to interface list
584 ether_ifattach(struct ifnet
*ifp
, const uint8_t *lla
,
585 lwkt_serialize_t serializer
)
587 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
592 ether_ifattach_bpf(struct ifnet
*ifp
, const uint8_t *lla
,
593 u_int dlt
, u_int hdrlen
, lwkt_serialize_t serializer
)
595 struct sockaddr_dl
*sdl
;
596 char ethstr
[ETHER_ADDRSTRLEN
+ 1];
601 * If driver does not configure # of mbuf clusters/jclusters
602 * that could sit on the device queues for quite some time,
604 * - The device queues only consume mbuf clusters.
605 * - No more than ether_nmbclusters_default (by default 256)
606 * mbuf clusters will sit on the device queues for quite
609 if (ifp
->if_nmbclusters
<= 0 && ifp
->if_nmbjclusters
<= 0) {
610 if (ether_nmbclusters_default
< ETHER_NMBCLUSTERS_DEFMIN
) {
611 kprintf("ether nmbclusters %d -> %d\n",
612 ether_nmbclusters_default
,
613 ETHER_NMBCLUSTERS_DEFAULT
);
614 ether_nmbclusters_default
= ETHER_NMBCLUSTERS_DEFAULT
;
616 ifp
->if_nmbclusters
= ether_nmbclusters_default
;
619 ifp
->if_type
= IFT_ETHER
;
620 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
621 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
622 if_attach(ifp
, serializer
);
624 for (i
= 0; i
< ifq
->altq_subq_cnt
; ++i
) {
625 struct ifaltq_subque
*ifsq
= ifq_get_subq(ifq
, i
);
627 ifsq
->ifsq_maxbcnt
= ifsq
->ifsq_maxlen
*
628 (ETHER_MAX_LEN
- ETHER_CRC_LEN
);
630 ifp
->if_mtu
= ETHERMTU
;
631 if (ifp
->if_tsolen
<= 0) {
632 if ((ether_tsolen_default
/ ETHERMTU
) < 2) {
633 kprintf("ether TSO maxlen %d -> %d\n",
634 ether_tsolen_default
, ETHER_TSOLEN_DEFAULT
);
635 ether_tsolen_default
= ETHER_TSOLEN_DEFAULT
;
637 ifp
->if_tsolen
= ether_tsolen_default
;
639 if (ifp
->if_baudrate
== 0)
640 ifp
->if_baudrate
= 10000000;
641 ifp
->if_output
= ether_output
;
642 ifp
->if_input
= ether_input
;
643 ifp
->if_resolvemulti
= ether_resolvemulti
;
644 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
645 sdl
= IF_LLSOCKADDR(ifp
);
646 sdl
->sdl_type
= IFT_ETHER
;
647 sdl
->sdl_alen
= ifp
->if_addrlen
;
648 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
650 * XXX Keep the current drivers happy.
651 * XXX Remove once all drivers have been cleaned up
653 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
654 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
655 bpfattach(ifp
, dlt
, hdrlen
);
656 if (ng_ether_attach_p
!= NULL
)
657 (*ng_ether_attach_p
)(ifp
);
659 if_printf(ifp
, "MAC address: %s\n", kether_ntoa(lla
, ethstr
));
663 * Perform common duties while detaching an Ethernet interface
666 ether_ifdetach(struct ifnet
*ifp
)
670 if (ng_ether_detach_p
!= NULL
)
671 (*ng_ether_detach_p
)(ifp
);
677 ether_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
)
679 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
680 struct ifreq
*ifr
= (struct ifreq
*) data
;
683 #define IF_INIT(ifp) \
685 if (((ifp)->if_flags & IFF_UP) == 0) { \
686 (ifp)->if_flags |= IFF_UP; \
687 (ifp)->if_init((ifp)->if_softc); \
691 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
695 switch (ifa
->ifa_addr
->sa_family
) {
698 IF_INIT(ifp
); /* before arpwhohas */
699 arp_ifinit(ifp
, ifa
);
710 error
= copyout(IFP2AC(ifp
)->ac_enaddr
,
711 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
717 * Set the interface MTU.
719 if (ifr
->ifr_mtu
> ETHERMTU
) {
722 ifp
->if_mtu
= ifr
->ifr_mtu
;
737 struct sockaddr
**llsa
,
740 struct sockaddr_dl
*sdl
;
742 struct sockaddr_in
*sin
;
745 struct sockaddr_in6
*sin6
;
749 switch(sa
->sa_family
) {
752 * No mapping needed. Just check that it's a valid MC address.
754 sdl
= (struct sockaddr_dl
*)sa
;
755 e_addr
= LLADDR(sdl
);
756 if ((e_addr
[0] & 1) != 1)
757 return EADDRNOTAVAIL
;
763 sin
= (struct sockaddr_in
*)sa
;
764 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
765 return EADDRNOTAVAIL
;
766 sdl
= kmalloc(sizeof *sdl
, M_IFMADDR
, M_WAITOK
| M_ZERO
);
767 sdl
->sdl_len
= sizeof *sdl
;
768 sdl
->sdl_family
= AF_LINK
;
769 sdl
->sdl_index
= ifp
->if_index
;
770 sdl
->sdl_type
= IFT_ETHER
;
771 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
772 e_addr
= LLADDR(sdl
);
773 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
774 *llsa
= (struct sockaddr
*)sdl
;
779 sin6
= (struct sockaddr_in6
*)sa
;
780 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
782 * An IP6 address of 0 means listen to all
783 * of the Ethernet multicast address used for IP6.
784 * (This is used for multicast routers.)
786 ifp
->if_flags
|= IFF_ALLMULTI
;
790 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
791 return EADDRNOTAVAIL
;
792 sdl
= kmalloc(sizeof *sdl
, M_IFMADDR
, M_WAITOK
| M_ZERO
);
793 sdl
->sdl_len
= sizeof *sdl
;
794 sdl
->sdl_family
= AF_LINK
;
795 sdl
->sdl_index
= ifp
->if_index
;
796 sdl
->sdl_type
= IFT_ETHER
;
797 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
798 e_addr
= LLADDR(sdl
);
799 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
800 *llsa
= (struct sockaddr
*)sdl
;
806 * Well, the text isn't quite right, but it's the name
815 * This is for reference. We have a table-driven version
816 * of the little-endian crc32 generator, which is faster
817 * than the double-loop.
820 ether_crc32_le(const uint8_t *buf
, size_t len
)
822 uint32_t c
, crc
, carry
;
825 crc
= 0xffffffffU
; /* initial value */
827 for (i
= 0; i
< len
; i
++) {
829 for (j
= 0; j
< 8; j
++) {
830 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
834 crc
= (crc
^ ETHER_CRC_POLY_LE
);
842 ether_crc32_le(const uint8_t *buf
, size_t len
)
844 static const uint32_t crctab
[] = {
845 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
846 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
847 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
848 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
853 crc
= 0xffffffffU
; /* initial value */
855 for (i
= 0; i
< len
; i
++) {
857 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
858 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
866 ether_crc32_be(const uint8_t *buf
, size_t len
)
868 uint32_t c
, crc
, carry
;
871 crc
= 0xffffffffU
; /* initial value */
873 for (i
= 0; i
< len
; i
++) {
875 for (j
= 0; j
< 8; j
++) {
876 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
880 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
888 * find the size of ethernet header, and call classifier
891 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
892 struct altq_pktattr
*pktattr
)
894 struct ether_header
*eh
;
896 int hlen
, af
, hdrsize
;
898 hlen
= sizeof(struct ether_header
);
899 eh
= mtod(m
, struct ether_header
*);
901 ether_type
= ntohs(eh
->ether_type
);
902 if (ether_type
< ETHERMTU
) {
904 struct llc
*llc
= (struct llc
*)(eh
+ 1);
907 if (m
->m_len
< hlen
||
908 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
909 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
910 llc
->llc_control
!= LLC_UI
)
911 goto bad
; /* not snap! */
913 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
916 if (ether_type
== ETHERTYPE_IP
) {
918 hdrsize
= 20; /* sizeof(struct ip) */
920 } else if (ether_type
== ETHERTYPE_IPV6
) {
922 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
927 while (m
->m_len
<= hlen
) {
931 if (m
->m_len
< hlen
+ hdrsize
) {
933 * ip header is not in a single mbuf. this should not
934 * happen in the current code.
935 * (todo: use m_pulldown in the future)
941 ifq_classify(ifq
, m
, af
, pktattr
);
948 pktattr
->pattr_class
= NULL
;
949 pktattr
->pattr_hdr
= NULL
;
950 pktattr
->pattr_af
= AF_UNSPEC
;
954 ether_restore_header(struct mbuf
**m0
, const struct ether_header
*eh
,
955 const struct ether_header
*save_eh
)
957 struct mbuf
*m
= *m0
;
962 * Prepend the header, optimize for the common case of
963 * eh pointing into the mbuf.
965 if ((const void *)(eh
+ 1) == (void *)m
->m_data
) {
966 m
->m_data
-= ETHER_HDR_LEN
;
967 m
->m_len
+= ETHER_HDR_LEN
;
968 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
972 M_PREPEND(m
, ETHER_HDR_LEN
, M_NOWAIT
);
974 bcopy(save_eh
, mtod(m
, struct ether_header
*),
982 * Upper layer processing for a received Ethernet packet.
985 ether_demux_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
987 struct ether_header
*eh
;
988 int isr
, discard
= 0;
990 struct ip_fw
*rule
= NULL
;
993 KASSERT(m
->m_len
>= ETHER_HDR_LEN
,
994 ("ether header is not contiguous!"));
996 eh
= mtod(m
, struct ether_header
*);
998 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
1001 /* Extract info from dummynet tag */
1002 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
1003 KKASSERT(mtag
!= NULL
);
1004 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
1005 KKASSERT(rule
!= NULL
);
1007 m_tag_delete(m
, mtag
);
1008 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
1010 /* packet is passing the second time */
1015 * We got a packet which was unicast to a different Ethernet
1016 * address. If the driver is working properly, then this
1017 * situation can only happen when the interface is in
1018 * promiscuous mode. We defer the packet discarding until the
1019 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1022 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
1023 !ETHER_IS_MULTICAST(eh
->ether_dhost
) &&
1024 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
1025 if (ether_debug
& 1) {
1026 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1027 "%02x:%02x:%02x:%02x:%02x:%02x "
1028 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1042 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[0],
1043 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[1],
1044 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[2],
1045 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[3],
1046 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[4],
1047 ((u_char
*)IFP2AC(ifp
)->ac_enaddr
)[5]
1050 if ((ether_debug
& 2) == 0)
1055 if ((IPFW_LOADED
|| IPFW3_LOADED
) && ether_ipfw
!= 0 && !discard
) {
1056 struct ether_header save_eh
= *eh
;
1058 /* XXX old crufty stuff, needs to be removed */
1059 m_adj(m
, sizeof(struct ether_header
));
1061 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
)) {
1066 ether_restore_header(&m
, eh
, &save_eh
);
1069 eh
= mtod(m
, struct ether_header
*);
1072 ether_type
= ntohs(eh
->ether_type
);
1073 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1075 /* Handle input from a lagg(4) port */
1076 if (ifp
->if_type
== IFT_IEEE8023ADLAG
) {
1077 KASSERT(lagg_input_p
!= NULL
,
1078 ("%s: if_lagg not loaded!", __func__
));
1079 (*lagg_input_p
)(ifp
, m
);
1083 if (m
->m_flags
& M_VLANTAG
) {
1084 void (*vlan_input_func
)(struct mbuf
*);
1086 vlan_input_func
= vlan_input_p
;
1087 /* Make sure 'vlan_input_func' is really used. */
1089 if (vlan_input_func
!= NULL
) {
1092 IFNET_STAT_INC(m
->m_pkthdr
.rcvif
, noproto
, 1);
1099 * If we have been asked to discard this packet
1100 * (e.g. not for us), drop it before entering
1109 * Clear protocol specific flags,
1110 * before entering the upper layer.
1112 m
->m_flags
&= ~M_ETHER_FLAGS
;
1114 /* Strip ethernet header. */
1115 m_adj(m
, sizeof(struct ether_header
));
1117 switch (ether_type
) {
1120 if ((m
->m_flags
& M_LENCHECKED
) == 0) {
1121 if (!ip_lengthcheck(&m
, 0))
1124 if (ipflow_fastforward(m
))
1130 if (ifp
->if_flags
& IFF_NOARP
) {
1131 /* Discard packet if ARP is disabled on interface */
1140 case ETHERTYPE_IPV6
:
1146 case ETHERTYPE_MPLS
:
1147 case ETHERTYPE_MPLS_MCAST
:
1148 /* Should have been set by ether_input(). */
1149 KKASSERT(m
->m_flags
& M_MPLSLABELED
);
1156 * The accurate msgport is not determined before
1157 * we reach here, so recharacterize packet.
1159 m
->m_flags
&= ~M_HASH
;
1160 if (ng_ether_input_orphan_p
!= NULL
) {
1162 * Put back the ethernet header so netgraph has a
1163 * consistent view of inbound packets.
1165 M_PREPEND(m
, ETHER_HDR_LEN
, M_NOWAIT
);
1168 * M_PREPEND frees the mbuf in case of failure.
1173 * Hold BGL and recheck ng_ether_input_orphan_p
1176 if (ng_ether_input_orphan_p
!= NULL
) {
1177 ng_ether_input_orphan_p(ifp
, m
);
1187 if (m
->m_flags
& M_HASH
) {
1188 if (&curthread
->td_msgport
==
1189 netisr_hashport(m
->m_pkthdr
.hash
)) {
1190 netisr_handle(isr
, m
);
1194 * XXX Something is wrong,
1195 * we probably should panic here!
1197 m
->m_flags
&= ~M_HASH
;
1198 atomic_add_long(ðer_input_wronghash
, 1);
1202 atomic_add_long(ðer_input_requeue
, 1);
1204 netisr_queue(isr
, m
);
1208 * First we perform any link layer operations, then continue to the
1209 * upper layers with ether_demux_oncpu().
1212 ether_input_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
1218 if ((ifp
->if_flags
& (IFF_UP
| IFF_MONITOR
)) != IFF_UP
) {
1220 * Receiving interface's flags are changed, when this
1221 * packet is waiting for processing; discard it.
1228 * A vlan tagged packet must be processed by ether_demux_oncpu()
1229 * immediately, before any bridging or packet filtering. If
1230 * the vlan decides to process it, this function will be called
1231 * again w/ the vlan interface for normal processing.
1233 if (m
->m_flags
& M_VLANTAG
) {
1234 ether_demux_oncpu(ifp
, m
);
1239 * Tap the packet off here for a bridge. bridge_input()
1240 * will return NULL if it has consumed the packet, otherwise
1241 * it gets processed as normal. Note that bridge_input()
1242 * will always return the original packet if we need to
1243 * process it locally.
1245 if (ifp
->if_bridge
) {
1246 KASSERT(bridge_input_p
!= NULL
,
1247 ("%s: if_bridge not loaded!", __func__
));
1249 if(m
->m_flags
& M_ETHER_BRIDGED
) {
1250 m
->m_flags
&= ~M_ETHER_BRIDGED
;
1252 m
= bridge_input_p(ifp
, m
);
1256 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
1257 ("bridge_input_p changed rcvif"));
1262 carp
= ifp
->if_carp
;
1264 m
= carp_input(carp
, m
);
1267 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
1268 ("carp_input changed rcvif"));
1272 /* Handle ng_ether(4) processing, if any */
1273 if (ng_ether_input_p
!= NULL
) {
1275 * Hold BGL and recheck ng_ether_input_p
1278 if (ng_ether_input_p
!= NULL
)
1279 ng_ether_input_p(ifp
, &m
);
1286 /* Continue with upper layer processing */
1287 ether_demux_oncpu(ifp
, m
);
1291 * Perform certain functions of ether_input():
1293 * - Update statistics
1294 * - Run bpf(4) tap if requested
1295 * Then pass the packet to ether_input_oncpu().
1297 * This function should be used by pseudo interface (e.g. vlan(4)),
1298 * when it tries to claim that the packet is received by it.
1304 ether_reinput_oncpu(struct ifnet
*ifp
, struct mbuf
*m
, int reinput_flags
)
1306 /* Discard packet if interface is not up */
1307 if (!(ifp
->if_flags
& IFF_UP
)) {
1313 * Change receiving interface. The bridge will often pass a flag to
1314 * ask that this not be done so ARPs get applied to the correct
1317 if ((reinput_flags
& REINPUT_KEEPRCVIF
) == 0 ||
1318 m
->m_pkthdr
.rcvif
== NULL
)
1320 m
->m_pkthdr
.rcvif
= ifp
;
1323 /* Update statistics */
1324 IFNET_STAT_INC(ifp
, ipackets
, 1);
1325 IFNET_STAT_INC(ifp
, ibytes
, m
->m_pkthdr
.len
);
1326 if (m
->m_flags
& (M_MCAST
| M_BCAST
))
1327 IFNET_STAT_INC(ifp
, imcasts
, 1);
1329 if (reinput_flags
& REINPUT_RUNBPF
)
1332 ether_input_oncpu(ifp
, m
);
1335 static __inline boolean_t
1336 ether_vlancheck(struct mbuf
**m0
)
1338 struct mbuf
*m
= *m0
;
1339 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
1340 uint16_t ether_type
= ntohs(eh
->ether_type
);
1342 if (ether_type
== ETHERTYPE_VLAN
) {
1343 if ((m
->m_flags
& M_VLANTAG
) == 0) {
1345 * Extract vlan tag if hardware does not do
1348 vlan_ether_decap(&m
);
1352 eh
= mtod(m
, struct ether_header
*);
1353 ether_type
= ntohs(eh
->ether_type
);
1354 if (ether_type
== ETHERTYPE_VLAN
) {
1356 * To prevent possible dangerous recursion,
1357 * we don't do vlan-in-vlan.
1359 IFNET_STAT_INC(m
->m_pkthdr
.rcvif
, noproto
, 1);
1364 * To prevent possible dangerous recursion,
1365 * we don't do vlan-in-vlan.
1367 IFNET_STAT_INC(m
->m_pkthdr
.rcvif
, noproto
, 1);
1370 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1373 m
->m_flags
|= M_ETHER_VLANCHECKED
;
1384 ether_input_handler(netmsg_t nmsg
)
1386 struct netmsg_packet
*nmp
= &nmsg
->packet
; /* actual size */
1387 struct ether_header
*eh
;
1394 if ((m
->m_flags
& M_ETHER_VLANCHECKED
) == 0) {
1395 if (!ether_vlancheck(&m
)) {
1396 KKASSERT(m
== NULL
);
1401 ifp
= m
->m_pkthdr
.rcvif
;
1402 if ((m
->m_flags
& (M_HASH
| M_CKHASH
)) == (M_HASH
| M_CKHASH
) ||
1403 __predict_false(ether_input_ckhash
)) {
1407 * Need to verify the hash supplied by the hardware
1408 * which could be wrong.
1410 m
->m_flags
&= ~(M_HASH
| M_CKHASH
);
1411 isr
= ether_characterize(&m
);
1414 KKASSERT(m
->m_flags
& M_HASH
);
1416 if (netisr_hashcpu(m
->m_pkthdr
.hash
) != mycpuid
) {
1418 * Wrong hardware supplied hash; redispatch
1420 ether_dispatch(ifp
, isr
, m
, -1);
1421 if (__predict_false(ether_input_ckhash
))
1422 atomic_add_long(ðer_input_wronghwhash
, 1);
1427 eh
= mtod(m
, struct ether_header
*);
1428 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
1429 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
1430 ifp
->if_addrlen
) == 0)
1431 m
->m_flags
|= M_BCAST
;
1433 m
->m_flags
|= M_MCAST
;
1434 IFNET_STAT_INC(ifp
, imcasts
, 1);
1437 ether_input_oncpu(ifp
, m
);
1441 * Send the packet to the target netisr msgport
1443 * At this point the packet must be characterized (M_HASH set),
1444 * so we know which netisr to send it to.
1447 ether_dispatch(struct ifnet
*ifp
, int isr
, struct mbuf
*m
, int cpuid
)
1449 struct netmsg_packet
*pmsg
;
1452 KKASSERT(m
->m_flags
& M_HASH
);
1453 target_cpuid
= netisr_hashcpu(m
->m_pkthdr
.hash
);
1455 pmsg
= &m
->m_hdr
.mh_netmsg
;
1456 netmsg_init(&pmsg
->base
, NULL
, &netisr_apanic_rport
,
1457 0, ether_input_handler
);
1458 pmsg
->nm_packet
= m
;
1459 pmsg
->base
.lmsg
.u
.ms_result
= isr
;
1461 logether(disp_beg
, NULL
);
1462 if (target_cpuid
== cpuid
) {
1463 if ((ifp
->if_flags
& IFF_IDIRECT
) && IN_NETISR_NCPUS(cpuid
)) {
1464 ether_input_handler((netmsg_t
)pmsg
);
1466 lwkt_sendmsg_oncpu(netisr_cpuport(target_cpuid
),
1470 lwkt_sendmsg(netisr_cpuport(target_cpuid
),
1473 logether(disp_end
, NULL
);
1477 * Process a received Ethernet packet.
1479 * The ethernet header is assumed to be in the mbuf so the caller
1480 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1481 * bytes in the first mbuf.
1483 * If the caller knows that the current thread is stick to the current
1484 * cpu, e.g. the interrupt thread or the netisr thread, the current cpuid
1485 * (mycpuid) should be passed through 'cpuid' argument. Else -1 should
1486 * be passed as 'cpuid' argument.
1489 ether_input(struct ifnet
*ifp
, struct mbuf
*m
, const struct pktinfo
*pi
,
1496 /* Discard packet if interface is not up */
1497 if (!(ifp
->if_flags
& IFF_UP
)) {
1502 if (m
->m_len
< sizeof(struct ether_header
)) {
1503 /* XXX error in the caller. */
1508 m
->m_pkthdr
.rcvif
= ifp
;
1510 logether(pkt_beg
, ifp
);
1512 ETHER_BPF_MTAP(ifp
, m
);
1514 IFNET_STAT_INC(ifp
, ibytes
, m
->m_pkthdr
.len
);
1516 if (ifp
->if_flags
& IFF_MONITOR
) {
1517 struct ether_header
*eh
;
1519 eh
= mtod(m
, struct ether_header
*);
1520 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
1521 IFNET_STAT_INC(ifp
, imcasts
, 1);
1524 * Interface marked for monitoring; discard packet.
1528 logether(pkt_end
, ifp
);
1533 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1534 * we can dispatch it immediately with trivial checks.
1536 if (pi
!= NULL
&& (m
->m_flags
& M_HASH
)) {
1538 atomic_add_long(ðer_pktinfo_try
, 1);
1540 netisr_hashcheck(pi
->pi_netisr
, m
, pi
);
1541 if (m
->m_flags
& M_HASH
) {
1542 ether_dispatch(ifp
, pi
->pi_netisr
, m
, cpuid
);
1544 atomic_add_long(ðer_pktinfo_hit
, 1);
1546 logether(pkt_end
, ifp
);
1551 else if (ifp
->if_capenable
& IFCAP_RSS
) {
1553 atomic_add_long(ðer_rss_nopi
, 1);
1555 atomic_add_long(ðer_rss_nohash
, 1);
1560 * Packet hash will be recalculated by software, so clear
1561 * the M_HASH and M_CKHASH flag set by the driver; the hash
1562 * value calculated by the hardware may not be exactly what
1565 m
->m_flags
&= ~(M_HASH
| M_CKHASH
);
1567 if (!ether_vlancheck(&m
)) {
1568 KKASSERT(m
== NULL
);
1569 logether(pkt_end
, ifp
);
1573 isr
= ether_characterize(&m
);
1575 logether(pkt_end
, ifp
);
1580 * Finally dispatch it
1582 ether_dispatch(ifp
, isr
, m
, cpuid
);
1584 logether(pkt_end
, ifp
);
1588 ether_characterize(struct mbuf
**m0
)
1590 struct mbuf
*m
= *m0
;
1591 struct ether_header
*eh
;
1592 uint16_t ether_type
;
1595 eh
= mtod(m
, struct ether_header
*);
1596 ether_type
= ntohs(eh
->ether_type
);
1599 * Map ether type to netisr id.
1601 switch (ether_type
) {
1613 case ETHERTYPE_IPV6
:
1619 case ETHERTYPE_MPLS
:
1620 case ETHERTYPE_MPLS_MCAST
:
1621 m
->m_flags
|= M_MPLSLABELED
;
1628 * NETISR_MAX is an invalid value; it is chosen to let
1629 * netisr_characterize() know that we have no clear
1630 * idea where this packet should go.
1637 * Ask the isr to characterize the packet since we couldn't.
1638 * This is an attempt to optimally get us onto the correct protocol
1641 netisr_characterize(isr
, &m
, sizeof(struct ether_header
));
1648 ether_demux_handler(netmsg_t nmsg
)
1650 struct netmsg_packet
*nmp
= &nmsg
->packet
; /* actual size */
1656 ifp
= m
->m_pkthdr
.rcvif
;
1658 ether_demux_oncpu(ifp
, m
);
1662 ether_demux(struct mbuf
*m
)
1664 struct netmsg_packet
*pmsg
;
1667 isr
= ether_characterize(&m
);
1671 KKASSERT(m
->m_flags
& M_HASH
);
1672 pmsg
= &m
->m_hdr
.mh_netmsg
;
1673 netmsg_init(&pmsg
->base
, NULL
, &netisr_apanic_rport
,
1674 0, ether_demux_handler
);
1675 pmsg
->nm_packet
= m
;
1676 pmsg
->base
.lmsg
.u
.ms_result
= isr
;
1678 lwkt_sendmsg(netisr_hashport(m
->m_pkthdr
.hash
), &pmsg
->base
.lmsg
);
1682 kether_aton(const char *macstr
, u_char
*addr
)
1684 unsigned int o0
, o1
, o2
, o3
, o4
, o5
;
1687 if (macstr
== NULL
|| addr
== NULL
)
1690 n
= ksscanf(macstr
, "%x:%x:%x:%x:%x:%x", &o0
, &o1
, &o2
,
1706 kether_ntoa(const u_char
*addr
, char *buf
)
1708 int len
= ETHER_ADDRSTRLEN
+ 1;
1711 n
= ksnprintf(buf
, len
, "%02x:%02x:%02x:%02x:%02x:%02x", addr
[0],
1712 addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
1720 MODULE_VERSION(ether
, 1);