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.96 2008/11/22 04:00:53 sephe Exp $
38 #include "opt_atalk.h"
40 #include "opt_inet6.h"
43 #include "opt_netgraph.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/msgport.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/thread.h>
59 #include <sys/thread2.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/if_llc.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/ifq_var.h>
69 #include <net/ethernet.h>
70 #include <net/vlan/if_vlan_ether.h>
71 #include <net/netmsg2.h>
73 #if defined(INET) || defined(INET6)
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/ip_flow.h>
78 #include <net/ipfw/ip_fw.h>
79 #include <net/dummynet/ip_dummynet.h>
82 #include <netinet6/nd6.h>
86 #include <netinet/ip_carp.h>
90 #include <netproto/ipx/ipx.h>
91 #include <netproto/ipx/ipx_if.h>
92 int (*ef_inputp
)(struct ifnet
*, const struct ether_header
*eh
, struct mbuf
*m
);
93 int (*ef_outputp
)(struct ifnet
*ifp
, struct mbuf
**mp
, struct sockaddr
*dst
,
94 short *tp
, int *hlen
);
99 #include <netns/ns_if.h>
101 int ether_outputdebug
= 0;
102 int ether_inputdebug
= 0;
106 #include <netproto/atalk/at.h>
107 #include <netproto/atalk/at_var.h>
108 #include <netproto/atalk/at_extern.h>
110 #define llc_snap_org_code llc_un.type_snap.org_code
111 #define llc_snap_ether_type llc_un.type_snap.ether_type
113 extern u_char at_org_code
[3];
114 extern u_char aarp_org_code
[3];
115 #endif /* NETATALK */
118 #include <netproto/mpls/mpls.h>
121 /* netgraph node hooks for ng_ether(4) */
122 void (*ng_ether_input_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
123 void (*ng_ether_input_orphan_p
)(struct ifnet
*ifp
,
124 struct mbuf
*m
, const struct ether_header
*eh
);
125 int (*ng_ether_output_p
)(struct ifnet
*ifp
, struct mbuf
**mp
);
126 void (*ng_ether_attach_p
)(struct ifnet
*ifp
);
127 void (*ng_ether_detach_p
)(struct ifnet
*ifp
);
129 void (*vlan_input_p
)(struct mbuf
*);
131 static int ether_output(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
133 static void ether_restore_header(struct mbuf
**, const struct ether_header
*,
134 const struct ether_header
*);
139 struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
140 int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*);
141 void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
143 static int ether_resolvemulti(struct ifnet
*, struct sockaddr
**,
146 const uint8_t etherbroadcastaddr
[ETHER_ADDR_LEN
] = {
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
150 #define gotoerr(e) do { error = (e); goto bad; } while (0)
151 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
153 static boolean_t
ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
,
155 const struct ether_header
*eh
);
157 static int ether_ipfw
;
158 static u_int ether_restore_hdr
;
159 static u_int ether_prepend_hdr
;
161 SYSCTL_DECL(_net_link
);
162 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
163 SYSCTL_INT(_net_link_ether
, OID_AUTO
, ipfw
, CTLFLAG_RW
,
164 ðer_ipfw
, 0, "Pass ether pkts through firewall");
165 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, restore_hdr
, CTLFLAG_RW
,
166 ðer_restore_hdr
, 0, "# of ether header restoration");
167 SYSCTL_UINT(_net_link_ether
, OID_AUTO
, prepend_hdr
, CTLFLAG_RW
,
168 ðer_prepend_hdr
, 0,
169 "# of ether header restoration which prepends mbuf");
171 #define ETHER_KTR_STR "ifp=%p"
172 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
174 #define KTR_ETHERNET KTR_ALL
176 KTR_INFO_MASTER(ether
);
177 KTR_INFO(KTR_ETHERNET
, ether
, chain_beg
, 0, ETHER_KTR_STR
, ETHER_KTR_ARG_SIZE
);
178 KTR_INFO(KTR_ETHERNET
, ether
, chain_end
, 1, ETHER_KTR_STR
, ETHER_KTR_ARG_SIZE
);
179 KTR_INFO(KTR_ETHERNET
, ether
, disp_beg
, 2, ETHER_KTR_STR
, ETHER_KTR_ARG_SIZE
);
180 KTR_INFO(KTR_ETHERNET
, ether
, disp_end
, 3, ETHER_KTR_STR
, ETHER_KTR_ARG_SIZE
);
181 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
184 * Ethernet output routine.
185 * Encapsulate a packet of type family for the local net.
186 * Use trailer local net encapsulation if enough data in first
187 * packet leaves a multiple of 512 bytes of data in remainder.
188 * Assumes that ifp is actually pointer to arpcom structure.
191 ether_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
194 struct ether_header
*eh
, *deh
;
197 int hlen
= ETHER_HDR_LEN
; /* link layer header length */
198 struct arpcom
*ac
= IFP2AC(ifp
);
201 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
203 if (ifp
->if_flags
& IFF_MONITOR
)
205 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
))
208 M_PREPEND(m
, sizeof(struct ether_header
), MB_DONTWAIT
);
211 eh
= mtod(m
, struct ether_header
*);
212 edst
= eh
->ether_dhost
;
215 * Fill in the destination ethernet address and frame type.
217 switch (dst
->sa_family
) {
220 if (!arpresolve(ifp
, rt
, m
, dst
, edst
))
221 return (0); /* if not yet resolved */
223 if (m
->m_flags
& M_MPLSLABELED
)
224 eh
->ether_type
= htons(ETHERTYPE_MPLS
);
227 eh
->ether_type
= htons(ETHERTYPE_IP
);
232 if (!nd6_storelladdr(&ac
->ac_if
, rt
, m
, dst
, edst
))
233 return (0); /* Something bad happenned. */
234 eh
->ether_type
= htons(ETHERTYPE_IPV6
);
239 if (ef_outputp
!= NULL
) {
241 * Hold BGL and recheck ef_outputp
244 if (ef_outputp
!= NULL
) {
245 error
= ef_outputp(ifp
, &m
, dst
,
246 &eh
->ether_type
, &hlen
);
255 eh
->ether_type
= htons(ETHERTYPE_IPX
);
256 bcopy(&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
257 edst
, ETHER_ADDR_LEN
);
262 struct at_ifaddr
*aa
;
269 if ((aa
= at_ifawithnet((struct sockaddr_at
*)dst
)) == NULL
) {
275 * In the phase 2 case, need to prepend an mbuf for
276 * the llc header. Since we must preserve the value
277 * of m, which is passed to us by value, we m_copy()
278 * the first mbuf, and use it for our llc header.
280 if (aa
->aa_flags
& AFA_PHASE2
) {
283 M_PREPEND(m
, sizeof(struct llc
), MB_DONTWAIT
);
284 eh
= mtod(m
, struct ether_header
*);
285 edst
= eh
->ether_dhost
;
286 llc
.llc_dsap
= llc
.llc_ssap
= LLC_SNAP_LSAP
;
287 llc
.llc_control
= LLC_UI
;
288 bcopy(at_org_code
, llc
.llc_snap_org_code
,
290 llc
.llc_snap_ether_type
= htons(ETHERTYPE_AT
);
292 mtod(m
, caddr_t
) + sizeof(struct ether_header
),
294 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
295 hlen
= sizeof(struct llc
) + ETHER_HDR_LEN
;
297 eh
->ether_type
= htons(ETHERTYPE_AT
);
299 if (!aarpresolve(ac
, m
, (struct sockaddr_at
*)dst
, edst
)) {
312 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
313 eh
->ether_type
= 0x8137;
315 case 0x0: /* Novell 802.3 */
316 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
318 case 0xe0e0: /* Novell 802.2 and Token-Ring */
319 M_PREPEND(m
, 3, MB_DONTWAIT
);
320 eh
= mtod(m
, struct ether_header
*);
321 edst
= eh
->ether_dhost
;
322 eh
->ether_type
= htons(m
->m_pkthdr
.len
);
323 cp
= mtod(m
, u_char
*) + sizeof(struct ether_header
);
329 bcopy(&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
), edst
,
332 * XXX if ns_thishost is the same as the node's ethernet
333 * address then just the default code will catch this anyhow.
334 * So I'm not sure if this next clause should be here at all?
337 if (bcmp(edst
, &ns_thishost
, ETHER_ADDR_LEN
) == 0) {
338 m
->m_pkthdr
.rcvif
= ifp
;
339 netisr_dispatch(NETISR_NS
, m
);
342 if (bcmp(edst
, &ns_broadhost
, ETHER_ADDR_LEN
) == 0)
343 m
->m_flags
|= M_BCAST
;
346 case pseudo_AF_HDRCMPLT
:
348 loop_copy
= -1; /* if this is for us, don't do it */
349 deh
= (struct ether_header
*)dst
->sa_data
;
350 memcpy(edst
, deh
->ether_dhost
, ETHER_ADDR_LEN
);
351 eh
->ether_type
= deh
->ether_type
;
355 if_printf(ifp
, "can't handle af%d\n", dst
->sa_family
);
356 gotoerr(EAFNOSUPPORT
);
359 if (dst
->sa_family
== pseudo_AF_HDRCMPLT
) /* unlikely */
360 memcpy(eh
->ether_shost
,
361 ((struct ether_header
*)dst
->sa_data
)->ether_shost
,
364 memcpy(eh
->ether_shost
, ac
->ac_enaddr
, ETHER_ADDR_LEN
);
367 * Bridges require special output handling.
369 if (ifp
->if_bridge
) {
370 KASSERT(bridge_output_p
!= NULL
,
371 ("%s: if_bridge not loaded!", __func__
));
372 return bridge_output_p(ifp
, m
);
376 * If a simplex interface, and the packet is being sent to our
377 * Ethernet address or a broadcast address, loopback a copy.
378 * XXX To make a simplex device behave exactly like a duplex
379 * device, we should copy in the case of sending to our own
380 * ethernet address (thus letting the original actually appear
381 * on the wire). However, we don't do that here for security
382 * reasons and compatibility with the original behavior.
384 if ((ifp
->if_flags
& IFF_SIMPLEX
) && (loop_copy
!= -1)) {
387 if (m
->m_pkthdr
.csum_flags
& CSUM_IP
)
388 csum_flags
|= (CSUM_IP_CHECKED
| CSUM_IP_VALID
);
389 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)
390 csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
);
391 if ((m
->m_flags
& M_BCAST
) || (loop_copy
> 0)) {
394 if ((n
= m_copypacket(m
, MB_DONTWAIT
)) != NULL
) {
395 n
->m_pkthdr
.csum_flags
|= csum_flags
;
396 if (csum_flags
& CSUM_DATA_VALID
)
397 n
->m_pkthdr
.csum_data
= 0xffff;
398 if_simloop(ifp
, n
, dst
->sa_family
, hlen
);
401 } else if (bcmp(eh
->ether_dhost
, eh
->ether_shost
,
402 ETHER_ADDR_LEN
) == 0) {
403 m
->m_pkthdr
.csum_flags
|= csum_flags
;
404 if (csum_flags
& CSUM_DATA_VALID
)
405 m
->m_pkthdr
.csum_data
= 0xffff;
406 if_simloop(ifp
, m
, dst
->sa_family
, hlen
);
407 return (0); /* XXX */
414 * Hold BGL and recheck ifp->if_carp
417 if (ifp
->if_carp
&& (error
= carp_output(ifp
, m
, dst
, NULL
))) {
426 /* Handle ng_ether(4) processing, if any */
427 if (ng_ether_output_p
!= NULL
) {
429 * Hold BGL and recheck ng_ether_output_p
432 if (ng_ether_output_p
!= NULL
) {
433 if ((error
= ng_ether_output_p(ifp
, &m
)) != 0) {
445 /* Continue with link-layer output */
446 return ether_output_frame(ifp
, m
);
454 * Ethernet link layer output routine to send a raw frame to the device.
456 * This assumes that the 14 byte Ethernet header is present and contiguous
460 ether_output_frame(struct ifnet
*ifp
, struct mbuf
*m
)
462 struct ip_fw
*rule
= NULL
;
464 struct altq_pktattr pktattr
;
466 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
468 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
471 /* Extract info from dummynet tag */
472 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
473 KKASSERT(mtag
!= NULL
);
474 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
475 KKASSERT(rule
!= NULL
);
477 m_tag_delete(m
, mtag
);
478 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
481 if (ifq_is_enabled(&ifp
->if_snd
))
482 altq_etherclassify(&ifp
->if_snd
, m
, &pktattr
);
484 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
485 struct ether_header save_eh
, *eh
;
487 eh
= mtod(m
, struct ether_header
*);
489 m_adj(m
, ETHER_HDR_LEN
);
490 if (!ether_ipfw_chk(&m
, ifp
, &rule
, eh
)) {
494 return ENOBUFS
; /* pkt dropped */
496 return 0; /* consumed e.g. in a pipe */
499 /* packet was ok, restore the ethernet header */
500 ether_restore_header(&m
, eh
, &save_eh
);
509 * Queue message on interface, update output statistics if
510 * successful, and start output if interface not yet active.
512 error
= ifq_dispatch(ifp
, m
, &pktattr
);
517 * ipfw processing for ethernet packets (in and out).
518 * The second parameter is NULL from ether_demux(), and ifp from
519 * ether_output_frame().
522 ether_ipfw_chk(struct mbuf
**m0
, struct ifnet
*dst
, struct ip_fw
**rule
,
523 const struct ether_header
*eh
)
525 struct ether_header save_eh
= *eh
; /* might be a ptr in *m0 */
526 struct ip_fw_args args
;
531 if (*rule
!= NULL
&& fw_one_pass
)
532 return TRUE
; /* dummynet packet, already partially processed */
535 * I need some amount of data to be contiguous.
537 i
= min((*m0
)->m_pkthdr
.len
, max_protohdr
);
538 if ((*m0
)->m_len
< i
) {
539 *m0
= m_pullup(*m0
, i
);
547 if ((mtag
= m_tag_find(*m0
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
548 m_tag_delete(*m0
, mtag
);
549 if ((*m0
)->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
550 mtag
= m_tag_find(*m0
, PACKET_TAG_IPFORWARD
, NULL
);
551 KKASSERT(mtag
!= NULL
);
552 m_tag_delete(*m0
, mtag
);
553 (*m0
)->m_pkthdr
.fw_flags
&= ~IPFORWARD_MBUF_TAGGED
;
556 args
.m
= *m0
; /* the packet we are looking at */
557 args
.oif
= dst
; /* destination, if any */
558 args
.rule
= *rule
; /* matching rule to restart */
559 args
.eh
= &save_eh
; /* MAC header for bridged/MAC packets */
560 i
= ip_fw_chk_ptr(&args
);
575 * XXX at some point add support for divert/forward actions.
576 * If none of the above matches, we have to drop the pkt.
582 * Pass the pkt to dummynet, which consumes it.
584 m
= *m0
; /* pass the original to dummynet */
585 *m0
= NULL
; /* and nothing back to the caller */
587 ether_restore_header(&m
, eh
, &save_eh
);
591 ip_fw_dn_io_ptr(m
, args
.cookie
,
592 dst
? DN_TO_ETH_OUT
: DN_TO_ETH_DEMUX
, &args
);
597 panic("unknown ipfw return value: %d\n", i
);
602 ether_input(struct ifnet
*ifp
, struct mbuf
*m
)
604 ether_input_chain(ifp
, m
, NULL
);
608 * Perform common duties while attaching to interface list
611 ether_ifattach(struct ifnet
*ifp
, uint8_t *lla
, lwkt_serialize_t serializer
)
613 ether_ifattach_bpf(ifp
, lla
, DLT_EN10MB
, sizeof(struct ether_header
),
618 ether_ifattach_bpf(struct ifnet
*ifp
, uint8_t *lla
, u_int dlt
, u_int hdrlen
,
619 lwkt_serialize_t serializer
)
621 struct sockaddr_dl
*sdl
;
623 ifp
->if_type
= IFT_ETHER
;
624 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
625 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
626 if_attach(ifp
, serializer
);
627 ifp
->if_mtu
= ETHERMTU
;
628 if (ifp
->if_baudrate
== 0)
629 ifp
->if_baudrate
= 10000000;
630 ifp
->if_output
= ether_output
;
631 ifp
->if_input
= ether_input
;
632 ifp
->if_resolvemulti
= ether_resolvemulti
;
633 ifp
->if_broadcastaddr
= etherbroadcastaddr
;
634 sdl
= IF_LLSOCKADDR(ifp
);
635 sdl
->sdl_type
= IFT_ETHER
;
636 sdl
->sdl_alen
= ifp
->if_addrlen
;
637 bcopy(lla
, LLADDR(sdl
), ifp
->if_addrlen
);
639 * XXX Keep the current drivers happy.
640 * XXX Remove once all drivers have been cleaned up
642 if (lla
!= IFP2AC(ifp
)->ac_enaddr
)
643 bcopy(lla
, IFP2AC(ifp
)->ac_enaddr
, ifp
->if_addrlen
);
644 bpfattach(ifp
, dlt
, hdrlen
);
645 if (ng_ether_attach_p
!= NULL
)
646 (*ng_ether_attach_p
)(ifp
);
648 if_printf(ifp
, "MAC address: %6D\n", lla
, ":");
652 * Perform common duties while detaching an Ethernet interface
655 ether_ifdetach(struct ifnet
*ifp
)
659 if (ng_ether_detach_p
!= NULL
)
660 (*ng_ether_detach_p
)(ifp
);
666 ether_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
668 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
669 struct ifreq
*ifr
= (struct ifreq
*) data
;
672 #define IF_INIT(ifp) \
674 if (((ifp)->if_flags & IFF_UP) == 0) { \
675 (ifp)->if_flags |= IFF_UP; \
676 (ifp)->if_init((ifp)->if_softc); \
680 ASSERT_SERIALIZED(ifp
->if_serializer
);
684 switch (ifa
->ifa_addr
->sa_family
) {
687 IF_INIT(ifp
); /* before arpwhohas */
688 arp_ifinit(ifp
, ifa
);
693 * XXX - This code is probably wrong
697 struct ipx_addr
*ina
= &IA_SIPX(ifa
)->sipx_addr
;
698 struct arpcom
*ac
= IFP2AC(ifp
);
700 if (ipx_nullhost(*ina
))
701 ina
->x_host
= *(union ipx_host
*) ac
->ac_enaddr
;
703 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
704 sizeof ac
->ac_enaddr
);
706 IF_INIT(ifp
); /* Set new address. */
712 * XXX - This code is probably wrong
716 struct ns_addr
*ina
= &(IA_SNS(ifa
)->sns_addr
);
717 struct arpcom
*ac
= IFP2AC(ifp
);
719 if (ns_nullhost(*ina
))
720 ina
->x_host
= *(union ns_host
*)(ac
->ac_enaddr
);
722 bcopy(ina
->x_host
.c_host
, ac
->ac_enaddr
,
723 sizeof ac
->ac_enaddr
);
739 bcopy(IFP2AC(ifp
)->ac_enaddr
,
740 ((struct sockaddr
*)ifr
->ifr_data
)->sa_data
,
746 * Set the interface MTU.
748 if (ifr
->ifr_mtu
> ETHERMTU
) {
751 ifp
->if_mtu
= ifr
->ifr_mtu
;
766 struct sockaddr
**llsa
,
769 struct sockaddr_dl
*sdl
;
770 struct sockaddr_in
*sin
;
772 struct sockaddr_in6
*sin6
;
776 switch(sa
->sa_family
) {
779 * No mapping needed. Just check that it's a valid MC address.
781 sdl
= (struct sockaddr_dl
*)sa
;
782 e_addr
= LLADDR(sdl
);
783 if ((e_addr
[0] & 1) != 1)
784 return EADDRNOTAVAIL
;
790 sin
= (struct sockaddr_in
*)sa
;
791 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
792 return EADDRNOTAVAIL
;
793 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
795 sdl
->sdl_len
= sizeof *sdl
;
796 sdl
->sdl_family
= AF_LINK
;
797 sdl
->sdl_index
= ifp
->if_index
;
798 sdl
->sdl_type
= IFT_ETHER
;
799 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
800 e_addr
= LLADDR(sdl
);
801 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
802 *llsa
= (struct sockaddr
*)sdl
;
807 sin6
= (struct sockaddr_in6
*)sa
;
808 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
810 * An IP6 address of 0 means listen to all
811 * of the Ethernet multicast address used for IP6.
812 * (This is used for multicast routers.)
814 ifp
->if_flags
|= IFF_ALLMULTI
;
818 if (!IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
819 return EADDRNOTAVAIL
;
820 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
822 sdl
->sdl_len
= sizeof *sdl
;
823 sdl
->sdl_family
= AF_LINK
;
824 sdl
->sdl_index
= ifp
->if_index
;
825 sdl
->sdl_type
= IFT_ETHER
;
826 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
827 e_addr
= LLADDR(sdl
);
828 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
829 *llsa
= (struct sockaddr
*)sdl
;
835 * Well, the text isn't quite right, but it's the name
844 * This is for reference. We have a table-driven version
845 * of the little-endian crc32 generator, which is faster
846 * than the double-loop.
849 ether_crc32_le(const uint8_t *buf
, size_t len
)
851 uint32_t c
, crc
, carry
;
854 crc
= 0xffffffffU
; /* initial value */
856 for (i
= 0; i
< len
; i
++) {
858 for (j
= 0; j
< 8; j
++) {
859 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
863 crc
= (crc
^ ETHER_CRC_POLY_LE
);
871 ether_crc32_le(const uint8_t *buf
, size_t len
)
873 static const uint32_t crctab
[] = {
874 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
875 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
876 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
877 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
882 crc
= 0xffffffffU
; /* initial value */
884 for (i
= 0; i
< len
; i
++) {
886 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
887 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
895 ether_crc32_be(const uint8_t *buf
, size_t len
)
897 uint32_t c
, crc
, carry
;
900 crc
= 0xffffffffU
; /* initial value */
902 for (i
= 0; i
< len
; i
++) {
904 for (j
= 0; j
< 8; j
++) {
905 carry
= ((crc
& 0x80000000U
) ? 1 : 0) ^ (c
& 0x01);
909 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
917 * find the size of ethernet header, and call classifier
920 altq_etherclassify(struct ifaltq
*ifq
, struct mbuf
*m
,
921 struct altq_pktattr
*pktattr
)
923 struct ether_header
*eh
;
925 int hlen
, af
, hdrsize
;
928 hlen
= sizeof(struct ether_header
);
929 eh
= mtod(m
, struct ether_header
*);
931 ether_type
= ntohs(eh
->ether_type
);
932 if (ether_type
< ETHERMTU
) {
934 struct llc
*llc
= (struct llc
*)(eh
+ 1);
937 if (m
->m_len
< hlen
||
938 llc
->llc_dsap
!= LLC_SNAP_LSAP
||
939 llc
->llc_ssap
!= LLC_SNAP_LSAP
||
940 llc
->llc_control
!= LLC_UI
)
941 goto bad
; /* not snap! */
943 ether_type
= ntohs(llc
->llc_un
.type_snap
.ether_type
);
946 if (ether_type
== ETHERTYPE_IP
) {
948 hdrsize
= 20; /* sizeof(struct ip) */
950 } else if (ether_type
== ETHERTYPE_IPV6
) {
952 hdrsize
= 40; /* sizeof(struct ip6_hdr) */
957 while (m
->m_len
<= hlen
) {
961 hdr
= m
->m_data
+ hlen
;
962 if (m
->m_len
< hlen
+ hdrsize
) {
964 * ip header is not in a single mbuf. this should not
965 * happen in the current code.
966 * (todo: use m_pulldown in the future)
972 ifq_classify(ifq
, m
, af
, pktattr
);
979 pktattr
->pattr_class
= NULL
;
980 pktattr
->pattr_hdr
= NULL
;
981 pktattr
->pattr_af
= AF_UNSPEC
;
985 ether_restore_header(struct mbuf
**m0
, const struct ether_header
*eh
,
986 const struct ether_header
*save_eh
)
988 struct mbuf
*m
= *m0
;
993 * Prepend the header, optimize for the common case of
994 * eh pointing into the mbuf.
996 if ((const void *)(eh
+ 1) == (void *)m
->m_data
) {
997 m
->m_data
-= ETHER_HDR_LEN
;
998 m
->m_len
+= ETHER_HDR_LEN
;
999 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1001 ether_prepend_hdr
++;
1003 M_PREPEND(m
, ETHER_HDR_LEN
, MB_DONTWAIT
);
1005 bcopy(save_eh
, mtod(m
, struct ether_header
*),
1013 ether_input_ipifunc(void *arg
)
1015 struct mbuf
*m
, *next
;
1020 next
= m
->m_nextpkt
;
1021 m
->m_nextpkt
= NULL
;
1023 port
= m
->m_pkthdr
.header
;
1024 m
->m_pkthdr
.header
= NULL
;
1027 &m
->m_hdr
.mh_netmsg
.nm_netmsg
.nm_lmsg
);
1030 } while (m
!= NULL
);
1034 ether_input_dispatch(struct mbuf_chain
*chain
)
1039 logether(disp_beg
, NULL
);
1040 for (i
= 0; i
< ncpus
; ++i
) {
1041 if (chain
[i
].mc_head
!= NULL
) {
1042 lwkt_send_ipiq(globaldata_find(i
),
1043 ether_input_ipifunc
, chain
[i
].mc_head
);
1047 logether(disp_beg
, NULL
);
1048 if (chain
->mc_head
!= NULL
)
1049 ether_input_ipifunc(chain
->mc_head
);
1051 logether(disp_end
, NULL
);
1055 ether_input_chain_init(struct mbuf_chain
*chain
)
1060 for (i
= 0; i
< ncpus
; ++i
)
1061 chain
[i
].mc_head
= chain
[i
].mc_tail
= NULL
;
1063 chain
->mc_head
= chain
->mc_tail
= NULL
;
1068 * Upper layer processing for a received Ethernet packet.
1071 ether_demux_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
1073 struct ether_header
*eh
;
1074 int isr
, redispatch
;
1076 struct ip_fw
*rule
= NULL
;
1082 KASSERT(m
->m_len
>= ETHER_HDR_LEN
,
1083 ("ether header is no contiguous!\n"));
1085 eh
= mtod(m
, struct ether_header
*);
1087 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
1090 /* Extract info from dummynet tag */
1091 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
1092 KKASSERT(mtag
!= NULL
);
1093 rule
= ((struct dn_pkt
*)m_tag_data(mtag
))->dn_priv
;
1094 KKASSERT(rule
!= NULL
);
1096 m_tag_delete(m
, mtag
);
1097 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
1099 /* packet is passing the second time */
1105 * XXX: Okay, we need to call carp_forus() and - if it is for
1106 * us jump over code that does the normal check
1107 * "ac_enaddr == ether_dhost". The check sequence is a bit
1108 * different from OpenBSD, so we jump over as few code as
1109 * possible, to catch _all_ sanity checks. This needs
1110 * evaluation, to see if the carp ether_dhost values break any
1115 * Hold BGL and recheck ifp->if_carp
1118 if (ifp
->if_carp
&& carp_forus(ifp
->if_carp
, eh
->ether_dhost
)) {
1127 * Discard packet if upper layers shouldn't see it because
1128 * it was unicast to a different Ethernet address. If the
1129 * driver is working properly, then this situation can only
1130 * happen when the interface is in promiscuous mode.
1132 if (((ifp
->if_flags
& (IFF_PROMISC
| IFF_PPROMISC
)) == IFF_PROMISC
) &&
1133 (eh
->ether_dhost
[0] & 1) == 0 &&
1134 bcmp(eh
->ether_dhost
, IFP2AC(ifp
)->ac_enaddr
, ETHER_ADDR_LEN
)) {
1140 if (IPFW_LOADED
&& ether_ipfw
!= 0) {
1141 struct ether_header save_eh
= *eh
;
1143 /* XXX old crufty stuff, needs to be removed */
1144 m_adj(m
, sizeof(struct ether_header
));
1146 if (!ether_ipfw_chk(&m
, NULL
, &rule
, eh
)) {
1151 ether_restore_header(&m
, eh
, &save_eh
);
1154 eh
= mtod(m
, struct ether_header
*);
1157 ether_type
= ntohs(eh
->ether_type
);
1158 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1160 if (m
->m_flags
& M_VLANTAG
) {
1161 void (*vlan_input_func
)(struct mbuf
*);
1163 vlan_input_func
= vlan_input_p
;
1164 if (vlan_input_func
!= NULL
) {
1167 m
->m_pkthdr
.rcvif
->if_noproto
++;
1173 m_adj(m
, sizeof(struct ether_header
));
1176 switch (ether_type
) {
1179 if (ipflow_fastforward(m
))
1185 if (ifp
->if_flags
& IFF_NOARP
) {
1186 /* Discard packet if ARP is disabled on interface */
1195 case ETHERTYPE_IPV6
:
1204 * Hold BGL and recheck ef_inputp
1207 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0) {
1218 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1226 isr
= NETISR_ATALK1
;
1228 case ETHERTYPE_AARP
:
1234 case ETHERTYPE_MPLS
:
1235 case ETHERTYPE_MPLS_MCAST
:
1236 /* Should have been set by ether_input_chain(). */
1237 KKASSERT(m
->m_flags
& M_MPLSLABELED
);
1244 * The accurate msgport is not determined before
1245 * we reach here, so redo the dispatching
1251 * Hold BGL and recheck ef_inputp
1254 if (ef_inputp
&& ef_inputp(ifp
, eh
, m
) == 0) {
1262 checksum
= mtod(m
, ushort
*);
1264 if ((ether_type
<= ETHERMTU
) &&
1265 ((*checksum
== 0xffff) || (*checksum
== 0xE0E0))) {
1266 if (*checksum
== 0xE0E0) {
1267 m
->m_pkthdr
.len
-= 3;
1276 if (ether_type
> ETHERMTU
)
1278 l
= mtod(m
, struct llc
*);
1279 if (l
->llc_dsap
== LLC_SNAP_LSAP
&&
1280 l
->llc_ssap
== LLC_SNAP_LSAP
&&
1281 l
->llc_control
== LLC_UI
) {
1282 if (bcmp(&(l
->llc_snap_org_code
)[0], at_org_code
,
1283 sizeof at_org_code
) == 0 &&
1284 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AT
) {
1285 m_adj(m
, sizeof(struct llc
));
1286 isr
= NETISR_ATALK2
;
1289 if (bcmp(&(l
->llc_snap_org_code
)[0], aarp_org_code
,
1290 sizeof aarp_org_code
) == 0 &&
1291 ntohs(l
->llc_snap_ether_type
) == ETHERTYPE_AARP
) {
1292 m_adj(m
, sizeof(struct llc
));
1299 if (ng_ether_input_orphan_p
!= NULL
) {
1301 * Hold BGL and recheck ng_ether_input_orphan_p
1304 if (ng_ether_input_orphan_p
!= NULL
) {
1305 ng_ether_input_orphan_p(ifp
, m
, eh
);
1318 netisr_dispatch(isr
, m
);
1322 * First we perform any link layer operations, then continue to the
1323 * upper layers with ether_demux_oncpu().
1326 ether_input_oncpu(struct ifnet
*ifp
, struct mbuf
*m
)
1328 if ((ifp
->if_flags
& (IFF_UP
| IFF_MONITOR
)) != IFF_UP
) {
1330 * Receiving interface's flags are changed, when this
1331 * packet is waiting for processing; discard it.
1338 * Tap the packet off here for a bridge. bridge_input()
1339 * will return NULL if it has consumed the packet, otherwise
1340 * it gets processed as normal. Note that bridge_input()
1341 * will always return the original packet if we need to
1342 * process it locally.
1344 if (ifp
->if_bridge
) {
1345 KASSERT(bridge_input_p
!= NULL
,
1346 ("%s: if_bridge not loaded!", __func__
));
1348 if(m
->m_flags
& M_PROTO1
) {
1349 m
->m_flags
&= ~M_PROTO1
;
1351 /* clear M_PROMISC, in case the packets comes from a vlan */
1352 /* m->m_flags &= ~M_PROMISC; */
1353 m
= bridge_input_p(ifp
, m
);
1357 KASSERT(ifp
== m
->m_pkthdr
.rcvif
,
1358 ("bridge_input_p changed rcvif\n"));
1362 /* Handle ng_ether(4) processing, if any */
1363 if (ng_ether_input_p
!= NULL
) {
1365 * Hold BGL and recheck ng_ether_input_p
1368 if (ng_ether_input_p
!= NULL
)
1369 ng_ether_input_p(ifp
, &m
);
1376 /* Continue with upper layer processing */
1377 ether_demux_oncpu(ifp
, m
);
1381 * Perform certain functions of ether_input_chain():
1383 * - Update statistics
1384 * - Run bpf(4) tap if requested
1385 * Then pass the packet to ether_input_oncpu().
1387 * This function should be used by pseudo interface (e.g. vlan(4)),
1388 * when it tries to claim that the packet is received by it.
1391 ether_reinput_oncpu(struct ifnet
*ifp
, struct mbuf
*m
, int run_bpf
)
1393 /* Discard packet if interface is not up */
1394 if (!(ifp
->if_flags
& IFF_UP
)) {
1399 /* Change receiving interface */
1400 m
->m_pkthdr
.rcvif
= ifp
;
1402 /* Update statistics */
1404 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1405 if (m
->m_flags
& (M_MCAST
| M_BCAST
))
1411 ether_input_oncpu(ifp
, m
);
1415 ether_input_handler(struct netmsg
*nmsg
)
1417 struct netmsg_packet
*nmp
= (struct netmsg_packet
*)nmsg
;
1423 ifp
= m
->m_pkthdr
.rcvif
;
1425 ether_input_oncpu(ifp
, m
);
1428 static __inline
void
1429 ether_init_netpacket(int num
, struct mbuf
*m
)
1431 struct netmsg_packet
*pmsg
;
1433 pmsg
= &m
->m_hdr
.mh_netmsg
;
1434 netmsg_init(&pmsg
->nm_netmsg
, &netisr_apanic_rport
, MSGF_MPSAFE
,
1435 ether_input_handler
);
1436 pmsg
->nm_packet
= m
;
1437 pmsg
->nm_netmsg
.nm_lmsg
.u
.ms_result
= num
;
1440 static __inline
struct lwkt_port
*
1441 ether_mport(int num
, struct mbuf
**m
)
1443 if (num
== NETISR_MAX
) {
1445 * All packets whose target msgports can't be
1446 * determined here are dispatched to netisr0,
1447 * where further dispatching may happen.
1449 return cpu_portfn(0);
1451 return netisr_find_port(num
, m
);
1455 * Process a received Ethernet packet.
1457 * The ethernet header is assumed to be in the mbuf so the caller
1458 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1459 * bytes in the first mbuf.
1461 * We first try to find the target msgport for this ether frame, if
1462 * there is no target msgport for it, this ether frame is discarded,
1463 * else we do following processing according to whether 'chain' is
1465 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1466 * immediately. This situation happens when ether_input_chain is
1467 * accessed through ifnet.if_input.
1468 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1469 * bucket indexed by the target msgport's cpuid and the target msgport
1470 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1471 * must initialize 'chain' by calling ether_input_chain_init().
1472 * ether_input_dispatch must be called later to send ether frames
1473 * queued on 'chain' to their target msgport.
1476 ether_input_chain(struct ifnet
*ifp
, struct mbuf
*m
, struct mbuf_chain
*chain
)
1478 struct ether_header
*eh
, *save_eh
, save_eh0
;
1479 struct lwkt_port
*port
;
1480 uint16_t ether_type
;
1483 ASSERT_SERIALIZED(ifp
->if_serializer
);
1486 /* Discard packet if interface is not up */
1487 if (!(ifp
->if_flags
& IFF_UP
)) {
1492 if (m
->m_len
< sizeof(struct ether_header
)) {
1493 /* XXX error in the caller. */
1497 eh
= mtod(m
, struct ether_header
*);
1499 m
->m_pkthdr
.rcvif
= ifp
;
1501 logether(chain_beg
, ifp
);
1503 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
1504 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
1505 ifp
->if_addrlen
) == 0)
1506 m
->m_flags
|= M_BCAST
;
1508 m
->m_flags
|= M_MCAST
;
1512 ETHER_BPF_MTAP(ifp
, m
);
1514 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1516 if (ifp
->if_flags
& IFF_MONITOR
) {
1518 * Interface marked for monitoring; discard packet.
1522 logether(chain_end
, ifp
);
1526 if (ntohs(eh
->ether_type
) == ETHERTYPE_VLAN
&&
1527 (m
->m_flags
& M_VLANTAG
) == 0) {
1529 * Extract vlan tag if hardware does not do it for us
1531 vlan_ether_decap(&m
);
1534 eh
= mtod(m
, struct ether_header
*);
1536 ether_type
= ntohs(eh
->ether_type
);
1538 if ((m
->m_flags
& M_VLANTAG
) && ether_type
== ETHERTYPE_VLAN
) {
1540 * To prevent possible dangerous recursion,
1541 * we don't do vlan-in-vlan
1547 KKASSERT(ether_type
!= ETHERTYPE_VLAN
);
1550 * Map ether type to netisr id.
1552 switch (ether_type
) {
1564 case ETHERTYPE_IPV6
:
1576 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1583 isr
= NETISR_ATALK1
;
1585 case ETHERTYPE_AARP
:
1591 case ETHERTYPE_MPLS
:
1592 case ETHERTYPE_MPLS_MCAST
:
1593 m
->m_flags
|= M_MPLSLABELED
;
1600 * NETISR_MAX is an invalid value; it is chosen to let
1601 * ether_mport() know that we are not able to decide
1602 * this packet's msgport here.
1609 * If the packet is in contiguous memory, following
1610 * m_adj() could ensure that the hidden ether header
1611 * will not be destroyed, else we will have to save
1612 * the ether header for the later restoration.
1614 if (m
->m_pkthdr
.len
!= m
->m_len
) {
1616 save_eh
= &save_eh0
;
1622 * Temporarily remove ether header; ether_mport()
1623 * expects a packet without ether header.
1625 m_adj(m
, sizeof(struct ether_header
));
1628 * Find the packet's target msgport.
1630 port
= ether_mport(isr
, &m
);
1632 KKASSERT(m
== NULL
);
1637 * Restore ether header.
1639 if (save_eh
!= NULL
) {
1640 ether_restore_header(&m
, eh
, save_eh
);
1644 m
->m_data
-= ETHER_HDR_LEN
;
1645 m
->m_len
+= ETHER_HDR_LEN
;
1646 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1650 * Initialize mbuf's netmsg packet _after_ possible
1651 * ether header restoration, else the initialized
1652 * netmsg packet may be lost during ether header
1655 ether_init_netpacket(isr
, m
);
1657 if (chain
!= NULL
) {
1658 struct mbuf_chain
*c
;
1661 m
->m_pkthdr
.header
= port
; /* XXX */
1662 cpuid
= port
->mpu_td
->td_gd
->gd_cpuid
;
1665 if (c
->mc_head
== NULL
) {
1666 c
->mc_head
= c
->mc_tail
= m
;
1668 c
->mc_tail
->m_nextpkt
= m
;
1671 m
->m_nextpkt
= NULL
;
1673 lwkt_sendmsg(port
, &m
->m_hdr
.mh_netmsg
.nm_netmsg
.nm_lmsg
);
1675 logether(chain_end
, ifp
);