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.73 2008/06/24 13:32:27 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>
69 #include <net/netmsg2.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <net/ipfw/ip_fw.h>
76 #include <net/dummynet/ip_dummynet.h>
79 #include <netinet6/nd6.h>
83 #include <netinet/ip_carp.h>
87 #include <netproto/ipx/ipx.h>
88 #include <netproto/ipx/ipx_if.h>
89 int (*ef_inputp
)(struct ifnet
*, const struct ether_header
*eh
, struct mbuf
*m
);
90 int (*ef_outputp
)(struct ifnet
*ifp
, struct mbuf
**mp
, struct sockaddr
*dst
,
91 short *tp
, int *hlen
);
96 #include <netns/ns_if.h>
98 int ether_outputdebug
= 0;
99 int ether_inputdebug
= 0;
103 #include <netproto/atalk/at.h>
104 #include <netproto/atalk/at_var.h>
105 #include <netproto/atalk/at_extern.h>
107 #define llc_snap_org_code llc_un.type_snap.org_code
108 #define llc_snap_ether_type llc_un.type_snap.ether_type
110 extern u_char at_org_code
[3];
111 extern u_char aarp_org_code
[3];
112 #endif /* NETATALK */
114 /* netgraph node hooks for ng_ether(4) */
115 void (*ng_ether_input_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
116 void (*ng_ether_input_orphan_p
)(struct ifnet
*ifp
,
117 struct mbuf
*m
, const struct ether_header
*eh
);
118 int (*ng_ether_output_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
119 void (*ng_ether_attach_p
)(struct ifnet
*ifp
);
120 void (*ng_ether_detach_p
)(struct ifnet
*ifp
);
122 int (*vlan_input_p
)(struct mbuf
*, struct mbuf_chain
*);
123 void (*vlan_input2_p
)(struct mbuf
*);
125 static int ether_output(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
127 static void ether_restore_header(struct mbuf
**, const struct ether_header
*,
128 const struct ether_header
*);
129 static void ether_demux_chain(struct ifnet
*, struct mbuf
*,
130 struct mbuf_chain
*);
135 struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
136 int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*);
137 void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
139 static int ether_resolvemulti(struct ifnet
*, struct sockaddr
**,
142 const uint8_t etherbroadcastaddr
[ETHER_ADDR_LEN
] = {
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
146 #define gotoerr(e) do { error = (e); goto bad; } while (0)
147 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
149 static boolean_t
ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
,
151 const struct ether_header
*eh
);
153 static int ether_ipfw
;
154 static u_int ether_restore_hdr
;
155 static u_int ether_prepend_hdr
;
157 SYSCTL_DECL(_net_link
);
158 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
159 SYSCTL_INT(_net_link_ether
, OID_AUTO
, ipfw
, CTLFLAG_RW
,
160 ðer_ipfw
, 0, "Pass ether pkts through firewall");
161 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, restore_hdr
, CTLFLAG_RW
,
162 ðer_restore_hdr
, 0, "# of ether header restoration");
163 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, prepend_hdr
, CTLFLAG_RW
,
164 ðer_prepend_hdr
, 0,
165 "# of ether header restoration which prepends mbuf");
168 * Ethernet output routine.
169 * Encapsulate a packet of type family for the local net.
170 * Use trailer local net encapsulation if enough data in first
171 * packet leaves a multiple of 512 bytes of data in remainder.
172 * Assumes that ifp is actually pointer to arpcom structure.
175 ether_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
178 struct ether_header
*eh
, *deh
;
181 int hlen
= ETHER_HDR_LEN
; /* link layer header length */
182 struct arpcom
*ac
= IFP2AC(ifp
);
185 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
187 if (ifp
->if_flags
& IFF_MONITOR
)
189 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
))
192 M_PREPEND(m
, sizeof(struct ether_header
), MB_DONTWAIT
);
195 eh
= mtod(m
, struct ether_header
*);
196 edst
= eh
->ether_dhost
;
199 * Fill in the destination ethernet address and frame type.
201 switch (dst
->sa_family
) {
204 if (!arpresolve(ifp
, rt
, m
, dst
, edst
))
205 return (0); /* if not yet resolved */
206 eh
->ether_type
= htons(ETHERTYPE_IP
);
211 if (!nd6_storelladdr(&ac
->ac_if
, rt
, m
, dst
, edst
))
212 return (0); /* Something bad happenned. */
213 eh
->ether_type
= htons(ETHERTYPE_IPV6
);
218 if (ef_outputp
!= NULL
) {
219 error
= ef_outputp(ifp
, &m
, dst
, &eh
->ether_type
,
224 eh
->ether_type
= htons(ETHERTYPE_IPX
);
225 bcopy(&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
226 edst
, ETHER_ADDR_LEN
);
232 struct at_ifaddr
*aa
;
234 if ((aa
= at_ifawithnet((struct sockaddr_at
*)dst
)) == NULL
) {
239 * In the phase 2 case, need to prepend an mbuf for
240 * the llc header. Since we must preserve the value
241 * of m, which is passed to us by value, we m_copy()
242 * the first mbuf, and use it for our llc header.
244 if (aa
->aa_flags
& AFA_PHASE2
) {
247 M_PREPEND(m
, sizeof(struct llc
), MB_DONTWAIT
);
248 eh
= mtod(m
, struct ether_header
*);
249 edst
= eh
->ether_dhost
;
250 llc
.llc_dsap
= llc
.llc_ssap
= LLC_SNAP_LSAP
;
251 llc
.llc_control
= LLC_UI
;
252 bcopy(at_org_code
, llc
.llc_snap_org_code
,
254 llc
.llc_snap_ether_type
= htons(ETHERTYPE_AT
);
256 mtod(m
, caddr_t
) + sizeof(struct ether_header
),
258 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
259 hlen
= sizeof(struct llc
) + ETHER_HDR_LEN
;
261 eh
->ether_type
= htons(ETHERTYPE_AT
);
263 if (!aarpresolve(ac
, m
, (struct sockaddr_at
*)dst
, edst
))
272 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
273 eh
->ether_type
= 0x8137;
275 case 0x0: /* Novell 802.3 */
276 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
278 case 0xe0e0: /* Novell 802.2 and Token-Ring */
279 M_PREPEND(m
, 3, MB_DONTWAIT
);
280 eh
= mtod(m
, struct ether_header
*);
281 edst
= eh
->ether_dhost
;
282 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
283 cp
= mtod(m
, u_char
*) + sizeof(struct ether_header
);
289 bcopy(&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
), edst
,
292 * XXX if ns_thishost is the same as the node's ethernet
293 * address then just the default code will catch this anyhow.
294 * So I'm not sure if this next clause should be here at all?
297 if (bcmp(edst
, &ns_thishost
, ETHER_ADDR_LEN
) == 0) {
298 m
->m_pkthdr
.rcvif
= ifp
;
299 netisr_dispatch(NETISR_NS
, m
);
302 if (bcmp(edst
, &ns_broadhost
, ETHER_ADDR_LEN
) == 0)
303 m
->m_flags
|= M_BCAST
;
306 case pseudo_AF_HDRCMPLT
:
308 loop_copy
= -1; /* if this is for us, don't do it */
309 deh
= (struct ether_header
*)dst
->sa_data
;
310 memcpy(edst
, deh
->ether_dhost
, ETHER_ADDR_LEN
);
311 eh
->ether_type
= deh
->ether_type
;
315 if_printf(ifp
, "can't handle af%d\n", dst
->sa_family
);
316 gotoerr(EAFNOSUPPORT
);
319 if (dst
->sa_family
== pseudo_AF_HDRCMPLT
) /* unlikely */
320 memcpy(eh
->ether_shost
,
321 ((struct ether_header
*)dst
->sa_data
)->ether_shost
,
324 memcpy(eh
->ether_shost
, ac
->ac_enaddr
, ETHER_ADDR_LEN
);
327 * Bridges require special output handling.
329 if (ifp
->if_bridge
) {
330 KASSERT(bridge_output_p
!= NULL
,
331 ("%s: if_bridge not loaded!", __func__
));
332 return bridge_output_p(ifp
, m
);
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 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
409 /* Extract info from dummynet tag */
410 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
412 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
414 m_tag_delete(m
, mtag
);
418 if (ifq_is_enabled(&ifp
->if_snd
))
419 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
421 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
422 struct ether_header save_eh
, *eh
;
424 eh
= mtod(m
, struct ether_header
*);
426 m_adj(m
, ETHER_HDR_LEN
);
427 if (!ether_ipfw_chk(&m
, ifp
, &rule
, eh
)) {
431 return ENOBUFS
; /* pkt dropped */
433 return 0; /* consumed e.g. in a pipe */
436 /* packet was ok, restore the ethernet header */
437 ether_restore_header(&m
, eh
, &save_eh
);
446 * Queue message on interface, update output statistics if
447 * successful, and start output if interface not yet active.
449 error
= ifq_dispatch(ifp
, m
, &pktattr
);
454 * ipfw processing for ethernet packets (in and out).
455 * The second parameter is NULL from ether_demux(), and ifp from
456 * ether_output_frame().
459 ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
, struct ip_fw
**rule
,
460 const struct ether_header
*eh
)
462 struct ether_header save_eh
= *eh
; /* might be a ptr in m */
463 struct ip_fw_args args
;
467 if (*rule
!= NULL
&& fw_one_pass
)
468 return TRUE
; /* dummynet packet, already partially processed */
471 * I need some amount of data to be contiguous.
473 i
= min((*m0
)->m_pkthdr
.len
, max_protohdr
);
474 if ((*m0
)->m_len
< i
) {
475 *m0
= m_pullup(*m0
, i
);
480 args
.m
= *m0
; /* the packet we are looking at */
481 args
.oif
= dst
; /* destination, if any */
482 if ((mtag
= m_tag_find(*m0
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
483 m_tag_delete(*m0
, mtag
);
484 args
.rule
= *rule
; /* matching rule to restart */
485 args
.next_hop
= NULL
; /* we do not support forward yet */
486 args
.eh
= &save_eh
; /* MAC header for bridged/MAC packets */
487 i
= ip_fw_chk_ptr(&args
);
491 if ((i
& IP_FW_PORT_DENY_FLAG
) || *m0
== NULL
) /* drop */
494 if (i
== 0) /* a PASS rule. */
497 if (i
& IP_FW_PORT_DYNT_FLAG
) {
499 * Pass the pkt to dummynet, which consumes it.
503 m
= *m0
; /* pass the original to dummynet */
504 *m0
= NULL
; /* and nothing back to the caller */
506 ether_restore_header(&m
, eh
, &save_eh
);
510 ip_fw_dn_io_ptr(m
, (i
& 0xffff),
511 dst
? DN_TO_ETH_OUT
: DN_TO_ETH_DEMUX
, &args
);
515 * XXX at some point add support for divert/forward actions.
516 * If none of the above matches, we have to drop the pkt.
522 * Process a received Ethernet packet.
524 * The ethernet header is assumed to be in the mbuf so the caller
525 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
526 * bytes in the first mbuf.
528 * This allows us to concentrate in one place a bunch of code which
529 * is replicated in all device drivers. Also, many functions called
530 * from ether_input() try to put the eh back into the mbuf, so we
531 * can later propagate the 'contiguous packet' interface to them.
533 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
534 * cluster, right before m_data. So be very careful when working on m,
535 * as you could destroy *eh !!
537 * First we perform any link layer operations, then continue to the
538 * upper layers with ether_demux().
541 ether_input_chain(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
543 struct ether_header
*eh
;
545 ASSERT_SERIALIZED(ifp
->if_serializer
);
548 /* Discard packet if interface is not up */
549 if (!(ifp
->if_flags
& IFF_UP
)) {
554 if (m
->m_len
< sizeof(struct ether_header
)) {
555 /* XXX error in the caller. */
559 eh
= mtod(m
, struct ether_header
*);
561 if (ntohs(eh
->ether_type
) == ETHERTYPE_VLAN
&&
562 (m
->m_flags
& M_VLANTAG
) == 0) {
564 * Extract vlan tag if hardware does not do it for us
566 vlan_ether_decap(&m
);
569 eh
= mtod(m
, struct ether_header
*);
572 m
->m_pkthdr
.rcvif
= ifp
;
574 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
575 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
576 ifp
->if_addrlen
) == 0)
577 m
->m_flags
|= M_BCAST
;
579 m
->m_flags
|= M_MCAST
;
583 ETHER_BPF_MTAP(ifp
, m
);
585 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
587 if (ifp
->if_flags
& IFF_MONITOR
) {
589 * Interface marked for monitoring; discard packet.
596 * Tap the packet off here for a bridge. bridge_input()
597 * will return NULL if it has consumed the packet, otherwise
598 * it gets processed as normal. Note that bridge_input()
599 * will always return the original packet if we need to
600 * process it locally.
602 if (ifp
->if_bridge
) {
603 KASSERT(bridge_input_p
!= NULL
,
604 ("%s: if_bridge not loaded!", __func__
));
606 if(m
->m_flags
& M_PROTO1
) {
607 m
->m_flags
&= ~M_PROTO1
;
609 /* clear M_PROMISC, in case the packets comes from a vlan */
610 /* m->m_flags &= ~M_PROMISC; */
611 lwkt_serialize_exit(ifp
->if_serializer
);
612 m
= bridge_input_p(ifp
, m
);
613 lwkt_serialize_enter(ifp
->if_serializer
);
617 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
618 ("bridge_input_p changed rcvif\n"));
620 /* 'm' may be changed by bridge_input_p() */
621 eh
= mtod(m
, struct ether_header
*);
625 /* Handle ng_ether(4) processing, if any */
626 if (ng_ether_input_p
!= NULL
) {
627 ng_ether_input_p(ifp
, &m
);
631 /* 'm' may be changed by ng_ether_input_p() */
632 eh
= mtod(m
, struct ether_header
*);
635 /* Continue with upper layer processing */
636 ether_demux_chain(ifp
, m
, chain
);
640 ether_input(struct ifnet
*ifp
, struct mbuf
*m
)
642 ether_input_chain(ifp
, m
, NULL
);
646 * Upper layer processing for a received Ethernet packet.
649 ether_demux_chain(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
651 struct ether_header save_eh
, *eh
;
654 struct ip_fw
*rule
= NULL
;
661 KASSERT(m
->m_len
>= ETHER_HDR_LEN
,
662 ("ether header is no contiguous!\n"));
664 eh
= mtod(m
, struct ether_header
*);
667 /* XXX old crufty stuff, needs to be removed */
668 m_adj(m
, sizeof(struct ether_header
));
670 /* Extract info from dummynet tag */
671 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
673 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
674 KKASSERT(ifp
== NULL
);
675 ifp
= m
->m_pkthdr
.rcvif
;
677 m_tag_delete(m
, mtag
);
680 if (rule
) /* packet is passing the second time */
685 * XXX: Okay, we need to call carp_forus() and - if it is for
686 * us jump over code that does the normal check
687 * "ac_enaddr == ether_dhost". The check sequence is a bit
688 * different from OpenBSD, so we jump over as few code as
689 * possible, to catch _all_ sanity checks. This needs
690 * evaluation, to see if the carp ether_dhost values break any
693 if (ifp
->if_carp
&& carp_forus(ifp
->if_carp
, eh
->ether_dhost
))
698 * Discard packet if upper layers shouldn't see it because
699 * it was unicast to a different Ethernet address. If the
700 * driver is working properly, then this situation can only
701 * happen when the interface is in promiscuous mode.
703 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
704 (eh
->ether_dhost
[0] & 1) == 0 &&
705 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
711 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
712 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
)) {
718 ether_type
= ntohs(save_eh
.ether_type
);
720 if (m
->m_flags
& M_VLANTAG
) {
721 if (ether_type
== ETHERTYPE_VLAN
) {
723 * To prevent possible dangerous recursion,
724 * we don't do vlan-in-vlan
726 m
->m_pkthdr
.rcvif
->if_noproto
++;
731 if (vlan_input_p
!= NULL
) {
732 ether_restore_header(&m
, eh
, &save_eh
);
734 vlan_input_p(m
, chain
);
736 m
->m_pkthdr
.rcvif
->if_noproto
++;
741 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
743 switch (ether_type
) {
746 if (ipflow_fastforward(m
, ifp
->if_serializer
))
752 if (ifp
->if_flags
& IFF_NOARP
) {
753 /* Discard packet if ARP is disabled on interface */
769 if (ef_inputp
&& ef_inputp(ifp
, &save_eh
, m
) == 0)
776 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
793 if (ef_inputp
&& ef_inputp(ifp
, &save_eh
, m
) == 0)
797 checksum
= mtod(m
, ushort
*);
799 if ((ether_type
<= ETHERMTU
) &&
800 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
801 if (*checksum
== 0xE0E0) {
802 m
->m_pkthdr
.len
-= 3;
811 if (ether_type
> ETHERMTU
)
813 l
= mtod(m
, struct llc
*);
814 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
815 l
->llc_ssap
== LLC_SNAP_LSAP
&&
816 l
->llc_control
== LLC_UI
) {
817 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
818 sizeof at_org_code
) == 0 &&
819 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
820 m_adj(m
, sizeof(struct llc
));
824 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
825 sizeof aarp_org_code
) == 0 &&
826 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
827 m_adj(m
, sizeof(struct llc
));
834 if (ng_ether_input_orphan_p
!= NULL
)
835 (*ng_ether_input_orphan_p
)(ifp
, m
, &save_eh
);
841 #ifdef ETHER_INPUT_CHAIN
843 struct mbuf_chain
*c
;
847 port
= netisr_mport(isr
, &m
);
851 m
->m_pkthdr
.header
= port
; /* XXX */
852 cpuid
= port
->mpu_td
->td_gd
->gd_cpuid
;
855 if (c
->mc_head
== NULL
) {
856 c
->mc_head
= c
->mc_tail
= m
;
858 c
->mc_tail
->m_nextpkt
= m
;
863 #endif /* ETHER_INPUT_CHAIN */
864 netisr_dispatch(isr
, m
);
868 ether_demux(struct ifnet
*ifp
, struct mbuf
*m
)
870 ether_demux_chain(ifp
, m
, NULL
);
874 * Perform common duties while attaching to interface list
878 ether_ifattach(struct ifnet
*ifp
, uint8_t *lla
, lwkt_serialize_t serializer
)
880 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
885 ether_ifattach_bpf(struct ifnet
*ifp
, uint8_t *lla
, u_int dlt
, u_int hdrlen
,
886 lwkt_serialize_t serializer
)
888 struct sockaddr_dl
*sdl
;
890 ifp
->if_type
= IFT_ETHER
;
891 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
892 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
893 if_attach(ifp
, serializer
);
894 ifp
->if_mtu
= ETHERMTU
;
895 if (ifp
->if_baudrate
== 0)
896 ifp
->if_baudrate
= 10000000;
897 ifp
->if_output
= ether_output
;
898 ifp
->if_input
= ether_input
;
899 ifp
->if_resolvemulti
= ether_resolvemulti
;
900 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
901 sdl
= IF_LLSOCKADDR(ifp
);
902 sdl
->sdl_type
= IFT_ETHER
;
903 sdl
->sdl_alen
= ifp
->if_addrlen
;
904 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
906 * XXX Keep the current drivers happy.
907 * XXX Remove once all drivers have been cleaned up
909 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
910 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
911 bpfattach(ifp
, dlt
, hdrlen
);
912 if (ng_ether_attach_p
!= NULL
)
913 (*ng_ether_attach_p
)(ifp
);
915 if_printf(ifp
, "MAC address: %6D\n", lla
, ":");
919 * Perform common duties while detaching an Ethernet interface
922 ether_ifdetach(struct ifnet
*ifp
)
926 if (ng_ether_detach_p
!= NULL
)
927 (*ng_ether_detach_p
)(ifp
);
933 ether_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
935 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
936 struct ifreq
*ifr
= (struct ifreq
*) data
;
939 #define IF_INIT(ifp) \
941 if (((ifp)->if_flags & IFF_UP) == 0) { \
942 (ifp)->if_flags |= IFF_UP; \
943 (ifp)->if_init((ifp)->if_softc); \
947 ASSERT_SERIALIZED(ifp
->if_serializer
);
951 switch (ifa
->ifa_addr
->sa_family
) {
954 IF_INIT(ifp
); /* before arpwhohas */
955 arp_ifinit(ifp
, ifa
);
960 * XXX - This code is probably wrong
964 struct ipx_addr
*ina
= &IA_SIPX(ifa
)->sipx_addr
;
965 struct arpcom
*ac
= IFP2AC(ifp
);
967 if (ipx_nullhost(*ina
))
968 ina
->x_host
= *(union ipx_host
*) ac
->ac_enaddr
;
970 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
971 sizeof ac
->ac_enaddr
);
973 IF_INIT(ifp
); /* Set new address. */
979 * XXX - This code is probably wrong
983 struct ns_addr
*ina
= &(IA_SNS(ifa
)->sns_addr
);
984 struct arpcom
*ac
= IFP2AC(ifp
);
986 if (ns_nullhost(*ina
))
987 ina
->x_host
= *(union ns_host
*)(ac
->ac_enaddr
);
989 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
990 sizeof ac
->ac_enaddr
);
1006 bcopy(IFP2AC(ifp
)->ac_enaddr
,
1007 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
1013 * Set the interface MTU.
1015 if (ifr
->ifr_mtu
> ETHERMTU
) {
1018 ifp
->if_mtu
= ifr
->ifr_mtu
;
1033 struct sockaddr
**llsa
,
1034 struct sockaddr
*sa
)
1036 struct sockaddr_dl
*sdl
;
1037 struct sockaddr_in
*sin
;
1039 struct sockaddr_in6
*sin6
;
1043 switch(sa
->sa_family
) {
1046 * No mapping needed. Just check that it's a valid MC address.
1048 sdl
= (struct sockaddr_dl
*)sa
;
1049 e_addr
= LLADDR(sdl
);
1050 if ((e_addr
[0] & 1) != 1)
1051 return EADDRNOTAVAIL
;
1057 sin
= (struct sockaddr_in
*)sa
;
1058 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
1059 return EADDRNOTAVAIL
;
1060 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1062 sdl
->sdl_len
= sizeof *sdl
;
1063 sdl
->sdl_family
= AF_LINK
;
1064 sdl
->sdl_index
= ifp
->if_index
;
1065 sdl
->sdl_type
= IFT_ETHER
;
1066 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1067 e_addr
= LLADDR(sdl
);
1068 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
1069 *llsa
= (struct sockaddr
*)sdl
;
1074 sin6
= (struct sockaddr_in6
*)sa
;
1075 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
1077 * An IP6 address of 0 means listen to all
1078 * of the Ethernet multicast address used for IP6.
1079 * (This is used for multicast routers.)
1081 ifp
->if_flags
|= IFF_ALLMULTI
;
1085 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
1086 return EADDRNOTAVAIL
;
1087 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
1089 sdl
->sdl_len
= sizeof *sdl
;
1090 sdl
->sdl_family
= AF_LINK
;
1091 sdl
->sdl_index
= ifp
->if_index
;
1092 sdl
->sdl_type
= IFT_ETHER
;
1093 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1094 e_addr
= LLADDR(sdl
);
1095 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
1096 *llsa
= (struct sockaddr
*)sdl
;
1102 * Well, the text isn't quite right, but it's the name
1105 return EAFNOSUPPORT
;
1111 * This is for reference. We have a table-driven version
1112 * of the little-endian crc32 generator, which is faster
1113 * than the double-loop.
1116 ether_crc32_le(const uint8_t *buf
, size_t len
)
1118 uint32_t c
, crc
, carry
;
1121 crc
= 0xffffffffU
; /* initial value */
1123 for (i
= 0; i
< len
; i
++) {
1125 for (j
= 0; j
< 8; j
++) {
1126 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
1130 crc
= (crc
^ ETHER_CRC_POLY_LE
);
1138 ether_crc32_le(const uint8_t *buf
, size_t len
)
1140 static const uint32_t crctab
[] = {
1141 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1142 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1143 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1144 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1149 crc
= 0xffffffffU
; /* initial value */
1151 for (i
= 0; i
< len
; i
++) {
1153 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1154 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
1162 ether_crc32_be(const uint8_t *buf
, size_t len
)
1164 uint32_t c
, crc
, carry
;
1167 crc
= 0xffffffffU
; /* initial value */
1169 for (i
= 0; i
< len
; i
++) {
1171 for (j
= 0; j
< 8; j
++) {
1172 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
1176 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
1184 * find the size of ethernet header, and call classifier
1187 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
1188 struct altq_pktattr
*pktattr
)
1190 struct ether_header
*eh
;
1191 uint16_t ether_type
;
1192 int hlen
, af
, hdrsize
;
1195 hlen
= sizeof(struct ether_header
);
1196 eh
= mtod(m
, struct ether_header
*);
1198 ether_type
= ntohs(eh
->ether_type
);
1199 if (ether_type
< ETHERMTU
) {
1201 struct llc
*llc
= (struct llc
*)(eh
+ 1);
1204 if (m
->m_len
< hlen
||
1205 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
1206 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
1207 llc
->llc_control
!= LLC_UI
)
1208 goto bad
; /* not snap! */
1210 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
1213 if (ether_type
== ETHERTYPE_IP
) {
1215 hdrsize
= 20; /* sizeof(struct ip) */
1217 } else if (ether_type
== ETHERTYPE_IPV6
) {
1219 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
1224 while (m
->m_len
<= hlen
) {
1228 hdr
= m
->m_data
+ hlen
;
1229 if (m
->m_len
< hlen
+ hdrsize
) {
1231 * ip header is not in a single mbuf. this should not
1232 * happen in the current code.
1233 * (todo: use m_pulldown in the future)
1239 ifq_classify(ifq
, m
, af
, pktattr
);
1246 pktattr
->pattr_class
= NULL
;
1247 pktattr
->pattr_hdr
= NULL
;
1248 pktattr
->pattr_af
= AF_UNSPEC
;
1252 ether_restore_header(struct mbuf
**m0
, const struct ether_header
*eh
,
1253 const struct ether_header
*save_eh
)
1255 struct mbuf
*m
= *m0
;
1257 ether_restore_hdr
++;
1260 * Prepend the header, optimize for the common case of
1261 * eh pointing into the mbuf.
1263 if ((const void *)(eh
+ 1) == (void *)m
->m_data
) {
1264 m
->m_data
-= ETHER_HDR_LEN
;
1265 m
->m_len
+= ETHER_HDR_LEN
;
1266 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1268 ether_prepend_hdr
++;
1270 M_PREPEND(m
, ETHER_HDR_LEN
, MB_DONTWAIT
);
1272 bcopy(save_eh
, mtod(m
, struct ether_header
*),
1279 #ifdef ETHER_INPUT_CHAIN
1282 ether_input_ipifunc(void *arg
)
1284 struct mbuf
*m
, *next
;
1289 next
= m
->m_nextpkt
;
1290 m
->m_nextpkt
= NULL
;
1292 port
= m
->m_pkthdr
.header
;
1293 m
->m_pkthdr
.header
= NULL
;
1296 &m
->m_hdr
.mh_netmsg
.nm_netmsg
.nm_lmsg
);
1299 } while (m
!= NULL
);
1303 ether_input_dispatch(struct mbuf_chain
*chain
)
1308 for (i
= 0; i
< ncpus
; ++i
) {
1309 if (chain
[i
].mc_head
!= NULL
) {
1310 lwkt_send_ipiq(globaldata_find(i
),
1311 ether_input_ipifunc
, chain
[i
].mc_head
);
1315 ether_input_ipifunc(chain
->mc_head
);
1320 ether_input_chain_init(struct mbuf_chain
*chain
)
1325 for (i
= 0; i
< ncpus
; ++i
)
1326 chain
[i
].mc_head
= chain
[i
].mc_tail
= NULL
;
1328 chain
->mc_head
= chain
->mc_tail
= NULL
;
1332 #endif /* ETHER_INPUT_CHAIN */
1337 ether_demux_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
1339 struct ether_header
*eh
;
1340 int isr
, redispatch
;
1342 struct ip_fw
*rule
= NULL
;
1349 KASSERT(m
->m_len
>= ETHER_HDR_LEN
,
1350 ("ether header is no contiguous!\n"));
1352 eh
= mtod(m
, struct ether_header
*);
1354 /* Extract info from dummynet tag */
1355 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
1357 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
1358 KKASSERT(ifp
== NULL
);
1359 ifp
= m
->m_pkthdr
.rcvif
;
1361 m_tag_delete(m
, mtag
);
1364 if (rule
) /* packet is passing the second time */
1369 * XXX: Okay, we need to call carp_forus() and - if it is for
1370 * us jump over code that does the normal check
1371 * "ac_enaddr == ether_dhost". The check sequence is a bit
1372 * different from OpenBSD, so we jump over as few code as
1373 * possible, to catch _all_ sanity checks. This needs
1374 * evaluation, to see if the carp ether_dhost values break any
1377 if (ifp
->if_carp
&& carp_forus(ifp
->if_carp
, eh
->ether_dhost
))
1382 * Discard packet if upper layers shouldn't see it because
1383 * it was unicast to a different Ethernet address. If the
1384 * driver is working properly, then this situation can only
1385 * happen when the interface is in promiscuous mode.
1387 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
1388 (eh
->ether_dhost
[0] & 1) == 0 &&
1389 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
1395 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
1396 struct ether_header save_eh
= *eh
;
1398 /* XXX old crufty stuff, needs to be removed */
1399 m_adj(m
, sizeof(struct ether_header
));
1401 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
)) {
1406 ether_restore_header(&m
, eh
, &save_eh
);
1409 eh
= mtod(m
, struct ether_header
*);
1412 ether_type
= ntohs(eh
->ether_type
);
1413 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1415 if (m
->m_flags
& M_VLANTAG
) {
1416 if (vlan_input2_p
!= NULL
) {
1419 m
->m_pkthdr
.rcvif
->if_noproto
++;
1425 m_adj(m
, sizeof(struct ether_header
));
1428 switch (ether_type
) {
1432 if (ipflow_fastforward(m
, ifp
->if_serializer
))
1439 if (ifp
->if_flags
& IFF_NOARP
) {
1440 /* Discard packet if ARP is disabled on interface */
1449 case ETHERTYPE_IPV6
:
1456 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
1463 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1471 isr
= NETISR_ATALK1
;
1473 case ETHERTYPE_AARP
:
1480 * The accurate msgport is not determined before
1481 * we reach here, so redo the dispatching
1485 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0)
1489 checksum
= mtod(m
, ushort
*);
1491 if ((ether_type
<= ETHERMTU
) &&
1492 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
1493 if (*checksum
== 0xE0E0) {
1494 m
->m_pkthdr
.len
-= 3;
1503 if (ether_type
> ETHERMTU
)
1505 l
= mtod(m
, struct llc
*);
1506 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
1507 l
->llc_ssap
== LLC_SNAP_LSAP
&&
1508 l
->llc_control
== LLC_UI
) {
1509 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
1510 sizeof at_org_code
) == 0 &&
1511 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
1512 m_adj(m
, sizeof(struct llc
));
1513 isr
= NETISR_ATALK2
;
1516 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
1517 sizeof aarp_org_code
) == 0 &&
1518 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
1519 m_adj(m
, sizeof(struct llc
));
1526 if (ng_ether_input_orphan_p
!= NULL
)
1527 ng_ether_input_orphan_p(ifp
, m
, eh
);
1536 netisr_dispatch(isr
, m
);
1540 ether_input_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
1542 if ((ifp
->if_flags
& (IFF_UP
| IFF_MONITOR
)) != IFF_UP
) {
1544 * Receiving interface's flags are changed, when this
1545 * packet is waiting for processing; discard it.
1552 * Tap the packet off here for a bridge. bridge_input()
1553 * will return NULL if it has consumed the packet, otherwise
1554 * it gets processed as normal. Note that bridge_input()
1555 * will always return the original packet if we need to
1556 * process it locally.
1558 if (ifp
->if_bridge
) {
1559 KASSERT(bridge_input_p
!= NULL
,
1560 ("%s: if_bridge not loaded!", __func__
));
1562 if(m
->m_flags
& M_PROTO1
) {
1563 m
->m_flags
&= ~M_PROTO1
;
1565 /* clear M_PROMISC, in case the packets comes from a vlan */
1566 /* m->m_flags &= ~M_PROMISC; */
1567 m
= bridge_input_p(ifp
, m
);
1571 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
1572 ("bridge_input_p changed rcvif\n"));
1576 /* Handle ng_ether(4) processing, if any */
1577 if (ng_ether_input_p
!= NULL
) {
1578 ng_ether_input_p(ifp
, &m
);
1583 /* Continue with upper layer processing */
1584 ether_demux_oncpu(ifp
, m
);
1588 ether_input_handler(struct netmsg
*nmsg
)
1590 struct netmsg_packet
*nmp
= (struct netmsg_packet
*)nmsg
;
1596 ifp
= m
->m_pkthdr
.rcvif
;
1598 ether_input_oncpu(ifp
, m
);
1601 static __inline
void
1602 ether_init_netpacket(int num
, struct mbuf
*m
)
1604 struct netmsg_packet
*pmsg
;
1606 pmsg
= &m
->m_hdr
.mh_netmsg
;
1607 netmsg_init(&pmsg
->nm_netmsg
, &netisr_apanic_rport
, 0,
1608 ether_input_handler
);
1609 pmsg
->nm_packet
= m
;
1610 pmsg
->nm_netmsg
.nm_lmsg
.u
.ms_result
= num
;
1613 static __inline
struct lwkt_port
*
1614 ether_mport(int num
, struct mbuf
**m0
)
1616 struct lwkt_port
*port
;
1617 struct mbuf
*m
= *m0
;
1619 if (num
== NETISR_MAX
) {
1621 * All packets whose target msgports can't be
1622 * determined here are dispatched to netisr0,
1623 * where further dispatching may happen.
1625 return cpu_portfn(0);
1628 port
= netisr_find_port(num
, &m
);
1637 ether_input_chain2(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
1639 struct ether_header
*eh
, *save_eh
, save_eh0
;
1640 struct lwkt_port
*port
;
1641 uint16_t ether_type
;
1644 ASSERT_SERIALIZED(ifp
->if_serializer
);
1647 /* Discard packet if interface is not up */
1648 if (!(ifp
->if_flags
& IFF_UP
)) {
1653 if (m
->m_len
< sizeof(struct ether_header
)) {
1654 /* XXX error in the caller. */
1658 eh
= mtod(m
, struct ether_header
*);
1660 m
->m_pkthdr
.rcvif
= ifp
;
1662 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
1663 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
1664 ifp
->if_addrlen
) == 0)
1665 m
->m_flags
|= M_BCAST
;
1667 m
->m_flags
|= M_MCAST
;
1671 ETHER_BPF_MTAP(ifp
, m
);
1673 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1675 if (ifp
->if_flags
& IFF_MONITOR
) {
1677 * Interface marked for monitoring; discard packet.
1683 if (ntohs(eh
->ether_type
) == ETHERTYPE_VLAN
&&
1684 (m
->m_flags
& M_VLANTAG
) == 0) {
1686 * Extract vlan tag if hardware does not do it for us
1688 vlan_ether_decap(&m
);
1691 eh
= mtod(m
, struct ether_header
*);
1693 ether_type
= ntohs(eh
->ether_type
);
1695 if ((m
->m_flags
& M_VLANTAG
) && ether_type
== ETHERTYPE_VLAN
) {
1697 * To prevent possible dangerous recursion,
1698 * we don't do vlan-in-vlan
1704 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1707 * Map ether type to netisr id.
1709 switch (ether_type
) {
1721 case ETHERTYPE_IPV6
:
1733 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1740 isr
= NETISR_ATALK1
;
1742 case ETHERTYPE_AARP
:
1749 * NETISR_MAX is an invalid value; it is chosen to let
1750 * ether_mport() know that we are not able to decide
1751 * this packet's msgport here.
1758 * If the packet is in contiguous memory, following
1759 * m_adj() could ensure that the hidden ether header
1760 * will not be destroyed, else we will have to save
1761 * the ether header for the later restoration.
1763 if (m
->m_pkthdr
.len
!= m
->m_len
) {
1765 save_eh
= &save_eh0
;
1771 * Temporarily remove ether header; ether_mport()
1772 * expects a packet without ether header.
1774 m_adj(m
, sizeof(struct ether_header
));
1777 * Find the packet's target msgport.
1779 port
= ether_mport(isr
, &m
);
1781 KKASSERT(m
== NULL
);
1786 * Restore ether header.
1788 if (save_eh
!= NULL
) {
1789 ether_restore_header(&m
, eh
, save_eh
);
1793 m
->m_data
-= ETHER_HDR_LEN
;
1794 m
->m_len
+= ETHER_HDR_LEN
;
1795 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1799 * Initialize mbuf's netmsg packet _after_ possible
1800 * ether header restoration, else the initialized
1801 * netmsg packet may be lost during ether header
1804 ether_init_netpacket(isr
, m
);
1806 #ifdef ETHER_INPUT_CHAIN
1807 if (chain
!= NULL
) {
1808 struct mbuf_chain
*c
;
1811 m
->m_pkthdr
.header
= port
; /* XXX */
1812 cpuid
= port
->mpu_td
->td_gd
->gd_cpuid
;
1815 if (c
->mc_head
== NULL
) {
1816 c
->mc_head
= c
->mc_tail
= m
;
1818 c
->mc_tail
->m_nextpkt
= m
;
1821 m
->m_nextpkt
= NULL
;
1823 #endif /* ETHER_INPUT_CHAIN */
1824 lwkt_sendmsg(port
, &m
->m_hdr
.mh_netmsg
.nm_netmsg
.nm_lmsg
);
1827 #endif /* ETHER_INPUT2 */