xref: /freebsd/sys/net/if_ethersubr.c (revision 995c7fd1bf57d48a40605c36ffaacfb048d677eb)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
337262d3e4SEivind Eklund #include "opt_atalk.h"
341d5e9e22SEivind Eklund #include "opt_inet.h"
35cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
36430df5f4SEivind Eklund #include "opt_ipx.h"
3743b29369SRobert Watson #include "opt_mac.h"
384cf49a43SJulian Elischer #include "opt_netgraph.h"
39a9771948SGleb Smirnoff #include "opt_carp.h"
40430df5f4SEivind Eklund 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4541ee9f1cSPoul-Henning Kamp #include <sys/module.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
4710b1fde0SMark Murray #include <sys/random.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
4951a53488SBruce Evans #include <sys/sockio.h>
50602d513cSGarrett Wollman #include <sys/sysctl.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <net/if.h>
53d7647d96SDag-Erling Smørgrav #include <net/if_arp.h>
54df8bae1dSRodney W. Grimes #include <net/netisr.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes #include <net/if_llc.h>
57df8bae1dSRodney W. Grimes #include <net/if_dl.h>
58df8bae1dSRodney W. Grimes #include <net/if_types.h>
592e2de7f2SArchie Cobbs #include <net/bpf.h>
60e1e1452dSArchie Cobbs #include <net/ethernet.h>
61fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
62c1d93b05SSam Leffler #include <net/if_vlan_var.h>
63df8bae1dSRodney W. Grimes 
6482cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
684b984093SLuigi Rizzo #include <netinet/ip_fw.h>
694b984093SLuigi Rizzo #include <netinet/ip_dummynet.h>
701d5e9e22SEivind Eklund #endif
7182cd038dSYoshinobu Inoue #ifdef INET6
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7382cd038dSYoshinobu Inoue #endif
74df8bae1dSRodney W. Grimes 
75a9771948SGleb Smirnoff #ifdef DEV_CARP
76a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
77a9771948SGleb Smirnoff #endif
78a9771948SGleb Smirnoff 
79cc6a66f2SJulian Elischer #ifdef IPX
80cc6a66f2SJulian Elischer #include <netipx/ipx.h>
81cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
82a1026028SMaxim Sobolev #endif
834f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
845accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
852a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
86cc6a66f2SJulian Elischer 
87655929bfSJulian Elischer #ifdef NETATALK
88655929bfSJulian Elischer #include <netatalk/at.h>
89655929bfSJulian Elischer #include <netatalk/at_var.h>
90655929bfSJulian Elischer #include <netatalk/at_extern.h>
91655929bfSJulian Elischer 
92655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
93655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
94655929bfSJulian Elischer 
95655929bfSJulian Elischer extern u_char	at_org_code[3];
96655929bfSJulian Elischer extern u_char	aarp_org_code[3];
972cc2df49SGarrett Wollman #endif /* NETATALK */
982cc2df49SGarrett Wollman 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
101e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
102c1d93b05SSam Leffler void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103c1d93b05SSam Leffler void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
106e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
107e1e1452dSArchie Cobbs 
108824eb9dcSDavid E. O'Brien void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
1099d4fe4b2SBrooks Davis 
110fd6238a6SAndrew Thompson /* if_bridge(4) support */
1118f867517SAndrew Thompson struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
1128f867517SAndrew Thompson int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
1138f867517SAndrew Thompson 		struct sockaddr *, struct rtentry *);
114c8b01292SAndrew Thompson void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
1158f867517SAndrew Thompson 
11618242d3bSAndrew Thompson /* if_lagg(4) support */
11718242d3bSAndrew Thompson struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
118b47888ceSAndrew Thompson 
119e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
120868d8b62SMatthew N. Dodd 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121868d8b62SMatthew N. Dodd 
122929ddbbbSAlfred Perlstein static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
123929ddbbbSAlfred Perlstein 		struct sockaddr *);
124868d8b62SMatthew N. Dodd 
125fc74a9f9SBrooks Davis /* XXX: should be in an arp support file, not here */
126fc74a9f9SBrooks Davis MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
127fc74a9f9SBrooks Davis 
12869462a82SBruce M Simpson #define	ETHER_IS_BROADCAST(addr) \
12969462a82SBruce M Simpson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
13069462a82SBruce M Simpson 
1314c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
132df8bae1dSRodney W. Grimes 
133d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
1344b984093SLuigi Rizzo int
1354b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136c1d93b05SSam Leffler 	struct ip_fw **rule, int shared);
1374b984093SLuigi Rizzo static int ether_ipfw;
138d7647d96SDag-Erling Smørgrav #endif
1394b984093SLuigi Rizzo 
140df8bae1dSRodney W. Grimes /*
141df8bae1dSRodney W. Grimes  * Ethernet output routine.
142df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
143df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
144df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
145df8bae1dSRodney W. Grimes  */
146df8bae1dSRodney W. Grimes int
14772fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m,
14872fd1b6aSDag-Erling Smørgrav 	struct sockaddr *dst, struct rtentry *rt0)
149df8bae1dSRodney W. Grimes {
150df8bae1dSRodney W. Grimes 	short type;
151cd46a114SLuigi Rizzo 	int error, hdrcmplt = 0;
15264760eb0SWarner Losh 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
153098a8c3bSMatthew N. Dodd 	struct ether_header *eh;
154c54c76ccSOleg Bulyzhin 	int loop_copy = 1;
155d32864c3SRobert Watson 	int hlen;	/* link layer header length */
156df8bae1dSRodney W. Grimes 
15743b29369SRobert Watson #ifdef MAC
15843b29369SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
15943b29369SRobert Watson 	if (error)
16043b29369SRobert Watson 		senderr(error);
16143b29369SRobert Watson #endif
16243b29369SRobert Watson 
163afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR)
164afbe3a0fSPoul-Henning Kamp 		senderr(ENETDOWN);
16513f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
16613f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
167df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1687f760c48SMatthew N. Dodd 
16984dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
170df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
171df8bae1dSRodney W. Grimes #ifdef INET
172df8bae1dSRodney W. Grimes 	case AF_INET:
173cd46a114SLuigi Rizzo 		error = arpresolve(ifp, rt0, m, dst, edst);
174cd46a114SLuigi Rizzo 		if (error)
175cd46a114SLuigi Rizzo 			return (error == EWOULDBLOCK ? 0 : error);
17634bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
177df8bae1dSRodney W. Grimes 		break;
17843a6c75aSMatthew N. Dodd 	case AF_ARP:
17943a6c75aSMatthew N. Dodd 	{
18043a6c75aSMatthew N. Dodd 		struct arphdr *ah;
18143a6c75aSMatthew N. Dodd 		ah = mtod(m, struct arphdr *);
18243a6c75aSMatthew N. Dodd 		ah->ar_hrd = htons(ARPHRD_ETHER);
18343a6c75aSMatthew N. Dodd 
184c54c76ccSOleg Bulyzhin 		loop_copy = 0; /* if this is for us, don't do it */
18543a6c75aSMatthew N. Dodd 
18643a6c75aSMatthew N. Dodd 		switch(ntohs(ah->ar_op)) {
18743a6c75aSMatthew N. Dodd 		case ARPOP_REVREQUEST:
18843a6c75aSMatthew N. Dodd 		case ARPOP_REVREPLY:
18943a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_REVARP);
19043a6c75aSMatthew N. Dodd 			break;
19143a6c75aSMatthew N. Dodd 		case ARPOP_REQUEST:
19243a6c75aSMatthew N. Dodd 		case ARPOP_REPLY:
19343a6c75aSMatthew N. Dodd 		default:
19443a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_ARP);
19543a6c75aSMatthew N. Dodd 			break;
19643a6c75aSMatthew N. Dodd 		}
19743a6c75aSMatthew N. Dodd 
19843a6c75aSMatthew N. Dodd 		if (m->m_flags & M_BCAST)
19943a6c75aSMatthew N. Dodd 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
20043a6c75aSMatthew N. Dodd 		else
20143a6c75aSMatthew N. Dodd 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
20243a6c75aSMatthew N. Dodd 
20343a6c75aSMatthew N. Dodd 	}
20443a6c75aSMatthew N. Dodd 	break;
205df8bae1dSRodney W. Grimes #endif
20682cd038dSYoshinobu Inoue #ifdef INET6
20782cd038dSYoshinobu Inoue 	case AF_INET6:
208cd46a114SLuigi Rizzo 		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
209cd46a114SLuigi Rizzo 		if (error)
210cd46a114SLuigi Rizzo 			return error;
21182cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
21282cd038dSYoshinobu Inoue 		break;
21382cd038dSYoshinobu Inoue #endif
214cc6a66f2SJulian Elischer #ifdef IPX
215cc6a66f2SJulian Elischer 	case AF_IPX:
2164f93599fSBoris Popov 		if (ef_outputp) {
2172a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
2185accfb8cSBoris Popov 		    if (error)
2195accfb8cSBoris Popov 			goto bad;
2204f93599fSBoris Popov 		} else
22134bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
222cc6a66f2SJulian Elischer 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
223cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
224cc6a66f2SJulian Elischer 		break;
225cc6a66f2SJulian Elischer #endif
226655929bfSJulian Elischer #ifdef NETATALK
227655929bfSJulian Elischer 	case AF_APPLETALK:
2281d0eab59SJulian Elischer 	  {
229ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2301d0eab59SJulian Elischer 
231cd46a114SLuigi Rizzo 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
232cd46a114SLuigi Rizzo 		    senderr(EHOSTUNREACH); /* XXX */
233cd46a114SLuigi Rizzo 	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
2341d0eab59SJulian Elischer 		    return (0);
235655929bfSJulian Elischer 	    /*
236ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
237655929bfSJulian Elischer 	     */
238655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
239655929bfSJulian Elischer 		struct llc llc;
240655929bfSJulian Elischer 
2417e2041e0SRobert Watson 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
2422166ffe8SRobert Watson 		if (m == NULL)
2432166ffe8SRobert Watson 			senderr(ENOBUFS);
244655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
245655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
246655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
247655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
248797f247bSMatthew N. Dodd 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
24934bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
250797f247bSMatthew N. Dodd 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
251655929bfSJulian Elischer 	    } else {
25234bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
253655929bfSJulian Elischer 	    }
254655929bfSJulian Elischer 	    break;
255ed7509acSJulian Elischer 	  }
2568cdfefbdSPeter Wemm #endif /* NETATALK */
257df8bae1dSRodney W. Grimes 
258114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
259114ae644SMike Smith 		hdrcmplt = 1;
260114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
261114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
262114ae644SMike Smith 		/* FALLTHROUGH */
263114ae644SMike Smith 
264df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
265c54c76ccSOleg Bulyzhin 		loop_copy = 0; /* if this is for us, don't do it */
266df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
26794a5d9b6SDavid Greenman 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
268df8bae1dSRodney W. Grimes 		type = eh->ether_type;
269df8bae1dSRodney W. Grimes 		break;
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	default:
27229e1b85fSBrooks Davis 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
273df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
274df8bae1dSRodney W. Grimes 	}
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	/*
277df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
278df8bae1dSRodney W. Grimes 	 * allocate another.
279df8bae1dSRodney W. Grimes 	 */
280797f247bSMatthew N. Dodd 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
2812166ffe8SRobert Watson 	if (m == NULL)
282df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
283df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
28494a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
285df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
28694a5d9b6SDavid Greenman 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
287114ae644SMike Smith 	if (hdrcmplt)
288114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
289114ae644SMike Smith 			sizeof(eh->ether_shost));
290114ae644SMike Smith 	else
2914a0d6638SRuslan Ermilov 		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
292df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
293ed7509acSJulian Elischer 
294ed7509acSJulian Elischer 	/*
295ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
296ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
297ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
298ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
299ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
300ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
301ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
302ed7509acSJulian Elischer 	 */
303c54c76ccSOleg Bulyzhin 	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
3040bdf5171SMax Laier 	    m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
3053cd67511SJesper Skriver 		int csum_flags = 0;
3063cd67511SJesper Skriver 
3073cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3083cd67511SJesper Skriver 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
3093cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
3103cd67511SJesper Skriver 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
31106f684b0SMatthew N. Dodd 
312c54c76ccSOleg Bulyzhin 		if (m->m_flags & M_BCAST) {
31306f684b0SMatthew N. Dodd 			struct mbuf *n;
314ed7509acSJulian Elischer 
315294dd290SLuigi Rizzo 			/*
316294dd290SLuigi Rizzo 			 * Because if_simloop() modifies the packet, we need a
317294dd290SLuigi Rizzo 			 * writable copy through m_dup() instead of a readonly
318294dd290SLuigi Rizzo 			 * one as m_copy[m] would give us. The alternative would
319294dd290SLuigi Rizzo 			 * be to modify if_simloop() to handle the readonly mbuf,
320294dd290SLuigi Rizzo 			 * but performancewise it is mostly equivalent (trading
321294dd290SLuigi Rizzo 			 * extra data copying vs. extra locking).
3229983b3c0SYaroslav Tykhiy 			 *
3239983b3c0SYaroslav Tykhiy 			 * XXX This is a local workaround.  A number of less
3249983b3c0SYaroslav Tykhiy 			 * often used kernel parts suffer from the same bug.
3259983b3c0SYaroslav Tykhiy 			 * See PR kern/105943 for a proposed general solution.
326294dd290SLuigi Rizzo 			 */
327294dd290SLuigi Rizzo 			if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
3283cd67511SJesper Skriver 				n->m_pkthdr.csum_flags |= csum_flags;
3293cd67511SJesper Skriver 				if (csum_flags & CSUM_DATA_VALID)
3303cd67511SJesper Skriver 					n->m_pkthdr.csum_data = 0xffff;
33106a429a3SArchie Cobbs 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
332c1404dc0SArchie Cobbs 			} else
333c1404dc0SArchie Cobbs 				ifp->if_iqdrops++;
33406f684b0SMatthew N. Dodd 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
33506f684b0SMatthew N. Dodd 				ETHER_ADDR_LEN) == 0) {
3363cd67511SJesper Skriver 			m->m_pkthdr.csum_flags |= csum_flags;
3373cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3383cd67511SJesper Skriver 				m->m_pkthdr.csum_data = 0xffff;
33906a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
340ed7509acSJulian Elischer 			return (0);	/* XXX */
341ed7509acSJulian Elischer 		}
342ed7509acSJulian Elischer 	}
3432e2de7f2SArchie Cobbs 
3443f7d1396SAndrew Thompson        /*
3453f7d1396SAndrew Thompson 	* Bridges require special output handling.
3463f7d1396SAndrew Thompson 	*/
3473f7d1396SAndrew Thompson 	if (ifp->if_bridge) {
3483f7d1396SAndrew Thompson 		BRIDGE_OUTPUT(ifp, m, error);
3493f7d1396SAndrew Thompson 		return (error);
3503f7d1396SAndrew Thompson 	}
3513f7d1396SAndrew Thompson 
352a9771948SGleb Smirnoff #ifdef DEV_CARP
353a9771948SGleb Smirnoff 	if (ifp->if_carp &&
354a9771948SGleb Smirnoff 	    (error = carp_output(ifp, m, dst, NULL)))
355a9771948SGleb Smirnoff 		goto bad;
356a9771948SGleb Smirnoff #endif
357a9771948SGleb Smirnoff 
358e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
359eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
360514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_output_p != NULL,
361514bcb89SPoul-Henning Kamp 		    ("ng_ether_output_p is NULL"));
362e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
363e1e1452dSArchie Cobbs bad:			if (m != NULL)
364e1e1452dSArchie Cobbs 				m_freem(m);
365e1e1452dSArchie Cobbs 			return (error);
366e1e1452dSArchie Cobbs 		}
367e1e1452dSArchie Cobbs 		if (m == NULL)
368e1e1452dSArchie Cobbs 			return (0);
369e1e1452dSArchie Cobbs 	}
370e1e1452dSArchie Cobbs 
371e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
372e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
373e1e1452dSArchie Cobbs }
374e1e1452dSArchie Cobbs 
375e1e1452dSArchie Cobbs /*
376e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
377e1e1452dSArchie Cobbs  *
378e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
379e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
380e1e1452dSArchie Cobbs  */
381e1e1452dSArchie Cobbs int
382c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
383e1e1452dSArchie Cobbs {
384d0a2acd4SJulian Elischer 	int error;
385d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
386ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
3872b25acc1SLuigi Rizzo 
3884b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
389c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
3904b984093SLuigi Rizzo 			if (m) {
3914b984093SLuigi Rizzo 				m_freem(m);
392e7909401SDavid Malone 				return EACCES;	/* pkt dropped */
3934b984093SLuigi Rizzo 			} else
3944b984093SLuigi Rizzo 				return 0;	/* consumed e.g. in a pipe */
3954b984093SLuigi Rizzo 		}
3964b984093SLuigi Rizzo 	}
397d7647d96SDag-Erling Smørgrav #endif
3982b25acc1SLuigi Rizzo 
399df8bae1dSRodney W. Grimes 	/*
400df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
401df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
402df8bae1dSRodney W. Grimes 	 */
403affc907dSMax Laier 	IFQ_HANDOFF(ifp, m, error);
404affc907dSMax Laier 	return (error);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
408df8bae1dSRodney W. Grimes /*
4094b984093SLuigi Rizzo  * ipfw processing for ethernet packets (in and out).
4104b984093SLuigi Rizzo  * The second parameter is NULL from ether_demux, and ifp from
411b6de9e91SMax Laier  * ether_output_frame.
4124b984093SLuigi Rizzo  */
4134b984093SLuigi Rizzo int
4144b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
415c1d93b05SSam Leffler 	struct ip_fw **rule, int shared)
4164b984093SLuigi Rizzo {
417c1d93b05SSam Leffler 	struct ether_header *eh;
418c1d93b05SSam Leffler 	struct ether_header save_eh;
419c1d93b05SSam Leffler 	struct mbuf *m;
4204b984093SLuigi Rizzo 	int i;
4212b25acc1SLuigi Rizzo 	struct ip_fw_args args;
4224b984093SLuigi Rizzo 
42397850a5dSLuigi Rizzo 	if (*rule != NULL && fw_one_pass)
424015d72e0SLuigi Rizzo 		return 1; /* dummynet packet, already partially processed */
4252f8ebbf4SLuigi Rizzo 
4264b984093SLuigi Rizzo 	/*
4272f8ebbf4SLuigi Rizzo 	 * I need some amt of data to be contiguous, and in case others need
4284b984093SLuigi Rizzo 	 * the packet (shared==1) also better be in the first mbuf.
4294b984093SLuigi Rizzo 	 */
430c1d93b05SSam Leffler 	m = *m0;
431c1d93b05SSam Leffler 	i = min( m->m_pkthdr.len, max_protohdr);
432c1d93b05SSam Leffler 	if ( shared || m->m_len < i) {
433c1d93b05SSam Leffler 		m = m_pullup(m, i);
434c1d93b05SSam Leffler 		if (m == NULL) {
435c1d93b05SSam Leffler 			*m0 = m;
4364b984093SLuigi Rizzo 			return 0;
4374b984093SLuigi Rizzo 		}
438c1d93b05SSam Leffler 	}
439c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
440c1d93b05SSam Leffler 	save_eh = *eh;			/* save copy for restore below */
441c1d93b05SSam Leffler 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
4424b984093SLuigi Rizzo 
443c1d93b05SSam Leffler 	args.m = m;		/* the packet we are looking at		*/
4442b25acc1SLuigi Rizzo 	args.oif = dst;		/* destination, if any			*/
4452b25acc1SLuigi Rizzo 	args.rule = *rule;	/* matching rule to restart		*/
4462b25acc1SLuigi Rizzo 	args.next_hop = NULL;	/* we do not support forward yet	*/
4472b25acc1SLuigi Rizzo 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
4483ecf1851SOleg Bulyzhin 	args.inp = NULL;	/* used by ipfw uid/gid/jail rules	*/
4492b25acc1SLuigi Rizzo 	i = ip_fw_chk_ptr(&args);
450c1d93b05SSam Leffler 	m = args.m;
451c1d93b05SSam Leffler 	if (m != NULL) {
452c1d93b05SSam Leffler 		/*
453c1d93b05SSam Leffler 		 * Restore Ethernet header, as needed, in case the
454c1d93b05SSam Leffler 		 * mbuf chain was replaced by ipfw.
455c1d93b05SSam Leffler 		 */
456a163d034SWarner Losh 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
457c1d93b05SSam Leffler 		if (m == NULL) {
458c1d93b05SSam Leffler 			*m0 = m;
459c1d93b05SSam Leffler 			return 0;
460c1d93b05SSam Leffler 		}
461c1d93b05SSam Leffler 		if (eh != mtod(m, struct ether_header *))
462c1d93b05SSam Leffler 			bcopy(&save_eh, mtod(m, struct ether_header *),
463c1d93b05SSam Leffler 				ETHER_HDR_LEN);
464c1d93b05SSam Leffler 	}
465c1d93b05SSam Leffler 	*m0 = m;
4662b25acc1SLuigi Rizzo 	*rule = args.rule;
4672b25acc1SLuigi Rizzo 
4686c69a7c3SGleb Smirnoff 	if (i == IP_FW_DENY) /* drop */
4694b984093SLuigi Rizzo 		return 0;
4704b984093SLuigi Rizzo 
4716c69a7c3SGleb Smirnoff 	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
4726c69a7c3SGleb Smirnoff 
4736c69a7c3SGleb Smirnoff 	if (i == IP_FW_PASS) /* a PASS rule.  */
4744b984093SLuigi Rizzo 		return 1;
4754b984093SLuigi Rizzo 
4766c69a7c3SGleb Smirnoff 	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
4774b984093SLuigi Rizzo 		/*
4784b984093SLuigi Rizzo 		 * Pass the pkt to dummynet, which consumes it.
4794b984093SLuigi Rizzo 		 * If shared, make a copy and keep the original.
4804b984093SLuigi Rizzo 		 */
4814b984093SLuigi Rizzo 		if (shared) {
482a163d034SWarner Losh 			m = m_copypacket(m, M_DONTWAIT);
4832b25acc1SLuigi Rizzo 			if (m == NULL)
4844b984093SLuigi Rizzo 				return 0;
4854b984093SLuigi Rizzo 		} else {
4862b25acc1SLuigi Rizzo 			/*
487c1d93b05SSam Leffler 			 * Pass the original to dummynet and
488c1d93b05SSam Leffler 			 * nothing back to the caller
4892b25acc1SLuigi Rizzo 			 */
490c1d93b05SSam Leffler 			*m0 = NULL ;
4914b984093SLuigi Rizzo 		}
49228935658SGleb Smirnoff 		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
4934b984093SLuigi Rizzo 		return 0;
4944b984093SLuigi Rizzo 	}
4954b984093SLuigi Rizzo 	/*
4962f8ebbf4SLuigi Rizzo 	 * XXX at some point add support for divert/forward actions.
4972f8ebbf4SLuigi Rizzo 	 * If none of the above matches, we have to drop the pkt.
4984b984093SLuigi Rizzo 	 */
4994b984093SLuigi Rizzo 	return 0;
5004b984093SLuigi Rizzo }
501d7647d96SDag-Erling Smørgrav #endif
5024b984093SLuigi Rizzo 
5034b984093SLuigi Rizzo /*
504c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
505c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
506df8bae1dSRodney W. Grimes  */
507c1d93b05SSam Leffler static void
508c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m)
509df8bae1dSRodney W. Grimes {
510c1d93b05SSam Leffler 	struct ether_header *eh;
511c1d93b05SSam Leffler 	u_short etype;
512df8bae1dSRodney W. Grimes 
51369462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_UP) == 0) {
51469462a82SBruce M Simpson 		m_freem(m);
51569462a82SBruce M Simpson 		return;
51669462a82SBruce M Simpson 	}
51769462a82SBruce M Simpson #ifdef DIAGNOSTIC
51869462a82SBruce M Simpson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
51969462a82SBruce M Simpson 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
52069462a82SBruce M Simpson 		m_freem(m);
52169462a82SBruce M Simpson 		return;
52269462a82SBruce M Simpson 	}
52369462a82SBruce M Simpson #endif
524c1d93b05SSam Leffler 	/*
525c1d93b05SSam Leffler 	 * Do consistency checks to verify assumptions
526c1d93b05SSam Leffler 	 * made by code past this point.
527c1d93b05SSam Leffler 	 */
528c1d93b05SSam Leffler 	if ((m->m_flags & M_PKTHDR) == 0) {
529c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o packet header\n");
530c1d93b05SSam Leffler 		ifp->if_ierrors++;
531c939f1aeSLuigi Rizzo 		m_freem(m);
532c939f1aeSLuigi Rizzo 		return;
533c939f1aeSLuigi Rizzo 	}
534797f247bSMatthew N. Dodd 	if (m->m_len < ETHER_HDR_LEN) {
535c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
536c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
537c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
538c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
539c1d93b05SSam Leffler 		ifp->if_ierrors++;
540c1d93b05SSam Leffler 		m_freem(m);
541c1d93b05SSam Leffler 		return;
5422e2de7f2SArchie Cobbs 	}
543c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
544c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
545c1d93b05SSam Leffler 	if (m->m_pkthdr.len >
546c1d93b05SSam Leffler 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
547c1d93b05SSam Leffler 		if_printf(ifp, "discard oversize frame "
548c1d93b05SSam Leffler 				"(ether type %x flags %x len %u > max %lu)\n",
549c1d93b05SSam Leffler 				etype, m->m_flags, m->m_pkthdr.len,
550c1d93b05SSam Leffler 				ETHER_MAX_FRAME(ifp, etype,
551c1d93b05SSam Leffler 						m->m_flags & M_HASFCS));
552c1d93b05SSam Leffler 		ifp->if_ierrors++;
553c1d93b05SSam Leffler 		m_freem(m);
554c1d93b05SSam Leffler 		return;
555c1d93b05SSam Leffler 	}
556c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif == NULL) {
557c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o interface pointer\n");
558c1d93b05SSam Leffler 		ifp->if_ierrors++;
559c1d93b05SSam Leffler 		m_freem(m);
560c1d93b05SSam Leffler 		return;
561c1d93b05SSam Leffler 	}
562c1d93b05SSam Leffler #ifdef DIAGNOSTIC
563c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif != ifp) {
5649bf40edeSBrooks Davis 		if_printf(ifp, "Warning, frame marked as received on %s\n",
5659bf40edeSBrooks Davis 			m->m_pkthdr.rcvif->if_xname);
566c1d93b05SSam Leffler 	}
567c1d93b05SSam Leffler #endif
568c1d93b05SSam Leffler 
56969462a82SBruce M Simpson 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
57069462a82SBruce M Simpson 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
57169462a82SBruce M Simpson 			m->m_flags |= M_BCAST;
57269462a82SBruce M Simpson 		else
57369462a82SBruce M Simpson 			m->m_flags |= M_MCAST;
57469462a82SBruce M Simpson 		ifp->if_imcasts++;
57569462a82SBruce M Simpson 	}
57669462a82SBruce M Simpson 
577533d8562SRobert Watson #ifdef MAC
578533d8562SRobert Watson 	/*
579533d8562SRobert Watson 	 * Tag the mbuf with an appropriate MAC label before any other
580533d8562SRobert Watson 	 * consumers can get to it.
581533d8562SRobert Watson 	 */
582533d8562SRobert Watson 	mac_create_mbuf_from_ifnet(ifp, m);
583533d8562SRobert Watson #endif
584533d8562SRobert Watson 
585c1d93b05SSam Leffler 	/*
58669462a82SBruce M Simpson 	 * Give bpf a chance at the packet.
587c1d93b05SSam Leffler 	 */
588d05d4616SChristian S.J. Peron 	ETHER_BPF_MTAP(ifp, m);
5892e2de7f2SArchie Cobbs 
59069462a82SBruce M Simpson 	/*
59169462a82SBruce M Simpson 	 * If the CRC is still on the packet, trim it off. We do this once
59269462a82SBruce M Simpson 	 * and once only in case we are re-entered. Nothing else on the
59369462a82SBruce M Simpson 	 * Ethernet receive path expects to see the FCS.
59469462a82SBruce M Simpson 	 */
595c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
596c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
597c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
598c1d93b05SSam Leffler 	}
599c1d93b05SSam Leffler 
600c1d93b05SSam Leffler 	ifp->if_ibytes += m->m_pkthdr.len;
60105463bb5SDavid Greenman 
60269462a82SBruce M Simpson 	/* Allow monitor mode to claim this frame, after stats are updated. */
603de572b37SChristian S.J. Peron 	if (ifp->if_flags & IFF_MONITOR) {
604de572b37SChristian S.J. Peron 		m_freem(m);
605de572b37SChristian S.J. Peron 		return;
606de572b37SChristian S.J. Peron 	}
607de572b37SChristian S.J. Peron 
60818242d3bSAndrew Thompson 	/* Handle input from a lagg(4) port */
609b47888ceSAndrew Thompson 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
61018242d3bSAndrew Thompson 		KASSERT(lagg_input_p != NULL,
61118242d3bSAndrew Thompson 		    ("%s: if_lagg not loaded!", __func__));
61218242d3bSAndrew Thompson 		m = (*lagg_input_p)(ifp, m);
613b47888ceSAndrew Thompson 		if (m != NULL)
614b47888ceSAndrew Thompson 			ifp = m->m_pkthdr.rcvif;
615b47888ceSAndrew Thompson 		else
616b47888ceSAndrew Thompson 			return;
617b47888ceSAndrew Thompson 	}
618b47888ceSAndrew Thompson 
61969462a82SBruce M Simpson 	/*
62069462a82SBruce M Simpson 	 * If the hardware did not process an 802.1Q tag, do this now,
62169462a82SBruce M Simpson 	 * to allow 802.1P priority frames to be passed to the main input
62269462a82SBruce M Simpson 	 * path correctly.
62369462a82SBruce M Simpson 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
62469462a82SBruce M Simpson 	 */
62569462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
62669462a82SBruce M Simpson 		struct ether_vlan_header *evl;
62769462a82SBruce M Simpson 
62869462a82SBruce M Simpson 		if (m->m_len < sizeof(*evl) &&
62969462a82SBruce M Simpson 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
630402d5e27SBruce M Simpson #ifdef DIAGNOSTIC
63169462a82SBruce M Simpson 			if_printf(ifp, "cannot pullup VLAN header\n");
632402d5e27SBruce M Simpson #endif
63369462a82SBruce M Simpson 			ifp->if_ierrors++;
63469462a82SBruce M Simpson 			m_freem(m);
63569462a82SBruce M Simpson 			return;
63669462a82SBruce M Simpson 		}
63769462a82SBruce M Simpson 
63869462a82SBruce M Simpson 		evl = mtod(m, struct ether_vlan_header *);
63969462a82SBruce M Simpson 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
64069462a82SBruce M Simpson 		m->m_flags |= M_VLANTAG;
64169462a82SBruce M Simpson 
64269462a82SBruce M Simpson 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
64369462a82SBruce M Simpson 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
64469462a82SBruce M Simpson 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
64569462a82SBruce M Simpson 	}
64669462a82SBruce M Simpson 
64769462a82SBruce M Simpson 	/* Allow ng_ether(4) to claim this frame. */
648eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
649514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_p != NULL,
65069462a82SBruce M Simpson 		    ("%s: ng_ether_input_p is NULL", __func__));
65169462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
652c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
653e1e1452dSArchie Cobbs 		if (m == NULL)
654e1e1452dSArchie Cobbs 			return;
655e1e1452dSArchie Cobbs 	}
656e1e1452dSArchie Cobbs 
6578f867517SAndrew Thompson 	/*
65869462a82SBruce M Simpson 	 * Allow if_bridge(4) to claim this frame.
65969462a82SBruce M Simpson 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
66069462a82SBruce M Simpson 	 * and the frame should be delivered locally.
6618f867517SAndrew Thompson 	 */
66269462a82SBruce M Simpson 	if (ifp->if_bridge != NULL) {
66369462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
664fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
6658f867517SAndrew Thompson 		if (m == NULL)
6668f867517SAndrew Thompson 			return;
6678f867517SAndrew Thompson 	}
6688f867517SAndrew Thompson 
66969462a82SBruce M Simpson #ifdef DEV_CARP
67069462a82SBruce M Simpson 	/*
67169462a82SBruce M Simpson 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
67269462a82SBruce M Simpson 	 * mbuf flows up to Layer 3.
67369462a82SBruce M Simpson 	 * FreeBSD's implementation of carp(4) uses the inprotosw
67469462a82SBruce M Simpson 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
67569462a82SBruce M Simpson 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
67669462a82SBruce M Simpson 	 * is outside the scope of the M_PROMISC test below.
67769462a82SBruce M Simpson 	 * TODO: Maintain a hash table of ethernet addresses other than
67869462a82SBruce M Simpson 	 * ether_dhost which may be active on this ifp.
67969462a82SBruce M Simpson 	 */
68069462a82SBruce M Simpson 	if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
68169462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
68269462a82SBruce M Simpson 	} else
68369462a82SBruce M Simpson #endif
68469462a82SBruce M Simpson 	{
68569462a82SBruce M Simpson 		/*
686e7f8c833SBruce M Simpson 		 * If the frame received was not for our MAC address, set the
68769462a82SBruce M Simpson 		 * M_PROMISC flag on the mbuf chain. The frame may need to
68869462a82SBruce M Simpson 		 * be seen by the rest of the Ethernet input path in case of
68969462a82SBruce M Simpson 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
69069462a82SBruce M Simpson 		 * seen by upper protocol layers.
69169462a82SBruce M Simpson 		 */
69269462a82SBruce M Simpson 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
693e7f8c833SBruce M Simpson 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
69469462a82SBruce M Simpson 			m->m_flags |= M_PROMISC;
69569462a82SBruce M Simpson 	}
69669462a82SBruce M Simpson 
697a6ddbff0SPoul-Henning Kamp 	/* First chunk of an mbuf contains good entropy */
69810b1fde0SMark Murray 	if (harvest.ethernet)
69910b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
70069462a82SBruce M Simpson 
701acf032f5SRobert Watson 	ether_demux(ifp, m);
702e1e1452dSArchie Cobbs }
703e1e1452dSArchie Cobbs 
704e1e1452dSArchie Cobbs /*
705e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
706e1e1452dSArchie Cobbs  */
707e1e1452dSArchie Cobbs void
708c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
709e1e1452dSArchie Cobbs {
710c1d93b05SSam Leffler 	struct ether_header *eh;
7111cafed39SJonathan Lemon 	int isr;
712e1e1452dSArchie Cobbs 	u_short ether_type;
713e1e1452dSArchie Cobbs #if defined(NETATALK)
714098a8c3bSMatthew N. Dodd 	struct llc *l;
715e1e1452dSArchie Cobbs #endif
71669462a82SBruce M Simpson 
71769462a82SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
71869462a82SBruce M Simpson 
719d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
72069462a82SBruce M Simpson 	/*
72169462a82SBruce M Simpson 	 * Allow dummynet and/or ipfw to claim the frame.
72269462a82SBruce M Simpson 	 * Do not do this for PROMISC frames in case we are re-entered.
72369462a82SBruce M Simpson 	 */
72469462a82SBruce M Simpson 	if (IPFW_LOADED && ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
725ac9d7e26SMax Laier 		struct ip_fw *rule = ip_dn_claim_rule(m);
72669462a82SBruce M Simpson 
72769462a82SBruce M Simpson 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
72869462a82SBruce M Simpson 			if (m)
72969462a82SBruce M Simpson 				m_freem(m);	/* dropped; free mbuf chain */
73069462a82SBruce M Simpson 			return;			/* consumed */
73169462a82SBruce M Simpson 		}
73269462a82SBruce M Simpson 	}
733d7647d96SDag-Erling Smørgrav #endif
734c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
735cd0cd014SJoerg Wunsch 	ether_type = ntohs(eh->ether_type);
736c1d93b05SSam Leffler 
737a9771948SGleb Smirnoff 	/*
73869462a82SBruce M Simpson 	 * If this frame has a VLAN tag other than 0, call vlan_input()
73969462a82SBruce M Simpson 	 * if its module is loaded. Otherwise, drop.
740a9771948SGleb Smirnoff 	 */
74169462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) &&
74269462a82SBruce M Simpson 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
74375ee267cSGleb Smirnoff 		if (ifp->if_vlantrunk == NULL) {
7446c23e6ccSRuslan Ermilov 			ifp->if_noproto++;
7456c23e6ccSRuslan Ermilov 			m_freem(m);
7466c23e6ccSRuslan Ermilov 			return;
7476c23e6ccSRuslan Ermilov 		}
74869462a82SBruce M Simpson 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
74969462a82SBruce M Simpson 		    __func__));
75069462a82SBruce M Simpson 		/* Clear before possibly re-entering ether_input(). */
75169462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
752c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
753c1d93b05SSam Leffler 		return;
754c1d93b05SSam Leffler 	}
755c1d93b05SSam Leffler 
756c1d93b05SSam Leffler 	/*
75769462a82SBruce M Simpson 	 * Pass promiscuously received frames to the upper layer if the user
75869462a82SBruce M Simpson 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
759c1d93b05SSam Leffler 	 */
76069462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
761c1d93b05SSam Leffler 		m_freem(m);
762c1d93b05SSam Leffler 		return;
763c1d93b05SSam Leffler 	}
764c1d93b05SSam Leffler 
76569462a82SBruce M Simpson 	/*
76669462a82SBruce M Simpson 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
76769462a82SBruce M Simpson 	 * Strip off Ethernet header.
76869462a82SBruce M Simpson 	 */
76969462a82SBruce M Simpson 	m->m_flags &= ~M_VLANTAG;
77069462a82SBruce M Simpson 	m->m_flags &= ~(M_PROTOFLAGS);
771797f247bSMatthew N. Dodd 	m_adj(m, ETHER_HDR_LEN);
772c1d93b05SSam Leffler 
77369462a82SBruce M Simpson 	/*
77469462a82SBruce M Simpson 	 * Dispatch frame to upper layer.
77569462a82SBruce M Simpson 	 */
776307d80beSDavid Greenman 	switch (ether_type) {
777df8bae1dSRodney W. Grimes #ifdef INET
778df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
7795d691e6dSAndre Oppermann 		if ((m = ip_fastforward(m)) == NULL)
7801f91d8c5SDavid Greenman 			return;
7811cafed39SJonathan Lemon 		isr = NETISR_IP;
782df8bae1dSRodney W. Grimes 		break;
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
78508aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
78608aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
78708aadfbbSJonathan Lemon 			m_freem(m);
78808aadfbbSJonathan Lemon 			return;
78908aadfbbSJonathan Lemon 		}
7901cafed39SJonathan Lemon 		isr = NETISR_ARP;
791df8bae1dSRodney W. Grimes 		break;
792df8bae1dSRodney W. Grimes #endif
793cc6a66f2SJulian Elischer #ifdef IPX
794cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
7954f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
7964f93599fSBoris Popov 			return;
7971cafed39SJonathan Lemon 		isr = NETISR_IPX;
798cc6a66f2SJulian Elischer 		break;
799cc6a66f2SJulian Elischer #endif
80082cd038dSYoshinobu Inoue #ifdef INET6
80182cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
8021cafed39SJonathan Lemon 		isr = NETISR_IPV6;
80382cd038dSYoshinobu Inoue 		break;
80482cd038dSYoshinobu Inoue #endif
805655929bfSJulian Elischer #ifdef NETATALK
806655929bfSJulian Elischer 	case ETHERTYPE_AT:
8071cafed39SJonathan Lemon 		isr = NETISR_ATALK1;
808655929bfSJulian Elischer 		break;
809655929bfSJulian Elischer 	case ETHERTYPE_AARP:
8101cafed39SJonathan Lemon 		isr = NETISR_AARP;
8111cafed39SJonathan Lemon 		break;
8128cdfefbdSPeter Wemm #endif /* NETATALK */
813df8bae1dSRodney W. Grimes 	default:
8144f93599fSBoris Popov #ifdef IPX
8154f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8164f93599fSBoris Popov 			return;
8174f93599fSBoris Popov #endif /* IPX */
818242c5536SPeter Wemm #if defined(NETATALK)
819307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
820c1d93b05SSam Leffler 			goto discard;
821df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
8221cafed39SJonathan Lemon 		if (l->llc_dsap == LLC_SNAP_LSAP &&
8231cafed39SJonathan Lemon 		    l->llc_ssap == LLC_SNAP_LSAP &&
8241cafed39SJonathan Lemon 		    l->llc_control == LLC_UI) {
8254158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
826655929bfSJulian Elischer 			    sizeof(at_org_code)) == 0 &&
827655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
828797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8291cafed39SJonathan Lemon 				isr = NETISR_ATALK2;
830655929bfSJulian Elischer 				break;
831655929bfSJulian Elischer 			}
8324158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
833655929bfSJulian Elischer 			    sizeof(aarp_org_code)) == 0 &&
834655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
835797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8361cafed39SJonathan Lemon 				isr = NETISR_AARP;
837655929bfSJulian Elischer 				break;
838df8bae1dSRodney W. Grimes 			}
8391cafed39SJonathan Lemon 		}
840242c5536SPeter Wemm #endif /* NETATALK */
8411cafed39SJonathan Lemon 		goto discard;
842df8bae1dSRodney W. Grimes 	}
8431cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
844c1d93b05SSam Leffler 	return;
8451cafed39SJonathan Lemon 
846c1d93b05SSam Leffler discard:
847c1d93b05SSam Leffler 	/*
848c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
849c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
850c1d93b05SSam Leffler 	 * otherwise dispose of it.
851c1d93b05SSam Leffler 	 */
852eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
853514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_orphan_p != NULL,
854514bcb89SPoul-Henning Kamp 		    ("ng_ether_input_orphan_p is NULL"));
855c1d93b05SSam Leffler 		/*
856c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
857c1d93b05SSam Leffler 		 * consistent view of inbound packets.
858c1d93b05SSam Leffler 		 */
859797f247bSMatthew N. Dodd 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
860c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
861c1d93b05SSam Leffler 		return;
862c1d93b05SSam Leffler 	}
863c1d93b05SSam Leffler 	m_freem(m);
864c1d93b05SSam Leffler }
865c1d93b05SSam Leffler 
866c1d93b05SSam Leffler /*
867c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
868c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
869c1d93b05SSam Leffler  *
870c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
871c1d93b05SSam Leffler  *
872c1d93b05SSam Leffler  * since there's no static buffer involved.
873c1d93b05SSam Leffler  */
874c1d93b05SSam Leffler char *
875c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
876c1d93b05SSam Leffler {
877c1d93b05SSam Leffler 	static char etherbuf[18];
878c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
879c1d93b05SSam Leffler 	return (etherbuf);
880df8bae1dSRodney W. Grimes }
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes /*
883df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
884df8bae1dSRodney W. Grimes  */
885df8bae1dSRodney W. Grimes void
886d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
887df8bae1dSRodney W. Grimes {
888f93dfa28SBrooks Davis 	int i;
889098a8c3bSMatthew N. Dodd 	struct ifaddr *ifa;
890098a8c3bSMatthew N. Dodd 	struct sockaddr_dl *sdl;
891df8bae1dSRodney W. Grimes 
892c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
893c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
894cfeff1b6SJonathan Lemon 	if_attach(ifp);
895df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
896c1d93b05SSam Leffler 	ifp->if_output = ether_output;
897c1d93b05SSam Leffler 	ifp->if_input = ether_input;
8981158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
899a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
900c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
901322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
902c1d93b05SSam Leffler 
9034a0d6638SRuslan Ermilov 	ifa = ifp->if_addr;
9046e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
90559562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
906df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
907df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
908d09ed26fSRuslan Ermilov 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
909c1d93b05SSam Leffler 
910797f247bSMatthew N. Dodd 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
911e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
912e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
913e3bbbec2SMatthew N. Dodd 
914f93dfa28SBrooks Davis 	/* Announce Ethernet MAC address if non-zero. */
915f93dfa28SBrooks Davis 	for (i = 0; i < ifp->if_addrlen; i++)
916d09ed26fSRuslan Ermilov 		if (lla[i] != 0)
917f93dfa28SBrooks Davis 			break;
918f93dfa28SBrooks Davis 	if (i != ifp->if_addrlen)
919d09ed26fSRuslan Ermilov 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
920af5e59bfSRobert Watson 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
921af5e59bfSRobert Watson 		if_printf(ifp, "if_start running deferred for Giant\n");
922e1e1452dSArchie Cobbs }
923e1e1452dSArchie Cobbs 
92421b8ebd9SArchie Cobbs /*
92521b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
92621b8ebd9SArchie Cobbs  */
92721b8ebd9SArchie Cobbs void
928c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
92921b8ebd9SArchie Cobbs {
930514bcb89SPoul-Henning Kamp 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
931514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_detach_p != NULL,
932514bcb89SPoul-Henning Kamp 		    ("ng_ether_detach_p is NULL"));
93321b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
934514bcb89SPoul-Henning Kamp 	}
93520a65f37SAndrew Thompson 
93621b8ebd9SArchie Cobbs 	bpfdetach(ifp);
93721b8ebd9SArchie Cobbs 	if_detach(ifp);
93821b8ebd9SArchie Cobbs }
93921b8ebd9SArchie Cobbs 
940ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
941602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
942d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
9434b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
9444b984093SLuigi Rizzo 	    &ether_ipfw,0,"Pass ether pkts through firewall");
945d7647d96SDag-Erling Smørgrav #endif
94630106f6aSPoul-Henning Kamp 
94716b4a343SChristian Weisgerber #if 0
94816b4a343SChristian Weisgerber /*
94916b4a343SChristian Weisgerber  * This is for reference.  We have a table-driven version
95016b4a343SChristian Weisgerber  * of the little-endian crc32 generator, which is faster
95116b4a343SChristian Weisgerber  * than the double-loop.
95216b4a343SChristian Weisgerber  */
95316b4a343SChristian Weisgerber uint32_t
95416b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
95516b4a343SChristian Weisgerber {
95616b4a343SChristian Weisgerber 	size_t i;
95716b4a343SChristian Weisgerber 	uint32_t crc;
95816b4a343SChristian Weisgerber 	int bit;
95916b4a343SChristian Weisgerber 	uint8_t data;
96016b4a343SChristian Weisgerber 
96116b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
96216b4a343SChristian Weisgerber 
96316b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
96416b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
96516b4a343SChristian Weisgerber 			carry = (crc ^ data) & 1;
96616b4a343SChristian Weisgerber 			crc >>= 1;
96716b4a343SChristian Weisgerber 			if (carry)
96816b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_LE);
96916b4a343SChristian Weisgerber 	}
97016b4a343SChristian Weisgerber 
97116b4a343SChristian Weisgerber 	return (crc);
97216b4a343SChristian Weisgerber }
97316b4a343SChristian Weisgerber #else
97416b4a343SChristian Weisgerber uint32_t
97516b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
97616b4a343SChristian Weisgerber {
97716b4a343SChristian Weisgerber 	static const uint32_t crctab[] = {
97816b4a343SChristian Weisgerber 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
97916b4a343SChristian Weisgerber 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
98016b4a343SChristian Weisgerber 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
98116b4a343SChristian Weisgerber 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
98216b4a343SChristian Weisgerber 	};
98316b4a343SChristian Weisgerber 	size_t i;
98416b4a343SChristian Weisgerber 	uint32_t crc;
98516b4a343SChristian Weisgerber 
98616b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
98716b4a343SChristian Weisgerber 
98816b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
98916b4a343SChristian Weisgerber 		crc ^= buf[i];
99016b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
99116b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
99216b4a343SChristian Weisgerber 	}
99316b4a343SChristian Weisgerber 
99416b4a343SChristian Weisgerber 	return (crc);
99516b4a343SChristian Weisgerber }
99616b4a343SChristian Weisgerber #endif
99716b4a343SChristian Weisgerber 
99816b4a343SChristian Weisgerber uint32_t
99916b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len)
100016b4a343SChristian Weisgerber {
100116b4a343SChristian Weisgerber 	size_t i;
100216b4a343SChristian Weisgerber 	uint32_t crc, carry;
100316b4a343SChristian Weisgerber 	int bit;
100416b4a343SChristian Weisgerber 	uint8_t data;
100516b4a343SChristian Weisgerber 
100616b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
100716b4a343SChristian Weisgerber 
100816b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
100916b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
101016b4a343SChristian Weisgerber 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
101116b4a343SChristian Weisgerber 			crc <<= 1;
101216b4a343SChristian Weisgerber 			if (carry)
101316b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
101416b4a343SChristian Weisgerber 		}
101516b4a343SChristian Weisgerber 	}
101616b4a343SChristian Weisgerber 
101716b4a343SChristian Weisgerber 	return (crc);
101816b4a343SChristian Weisgerber }
101916b4a343SChristian Weisgerber 
1020fb583156SDavid Greenman int
1021995c7fd1SYaroslav Tykhiy ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
102230106f6aSPoul-Henning Kamp {
102330106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
102430106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
1025fb583156SDavid Greenman 	int error = 0;
102630106f6aSPoul-Henning Kamp 
102730106f6aSPoul-Henning Kamp 	switch (command) {
102830106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
102930106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
103030106f6aSPoul-Henning Kamp 
103130106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
103230106f6aSPoul-Henning Kamp #ifdef INET
103330106f6aSPoul-Henning Kamp 		case AF_INET:
103430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1035322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
103630106f6aSPoul-Henning Kamp 			break;
103730106f6aSPoul-Henning Kamp #endif
103830106f6aSPoul-Henning Kamp #ifdef IPX
103930106f6aSPoul-Henning Kamp 		/*
104030106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
104130106f6aSPoul-Henning Kamp 		 */
104230106f6aSPoul-Henning Kamp 		case AF_IPX:
104330106f6aSPoul-Henning Kamp 			{
1044098a8c3bSMatthew N. Dodd 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
104530106f6aSPoul-Henning Kamp 
104630106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
104730106f6aSPoul-Henning Kamp 				ina->x_host =
104886101139SPoul-Henning Kamp 				    *(union ipx_host *)
10494a0d6638SRuslan Ermilov 				    IF_LLADDR(ifp);
105030106f6aSPoul-Henning Kamp 			else {
105130106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
10524a0d6638SRuslan Ermilov 				      (caddr_t) IF_LLADDR(ifp),
1053fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
105430106f6aSPoul-Henning Kamp 			}
105530106f6aSPoul-Henning Kamp 
105630106f6aSPoul-Henning Kamp 			/*
105730106f6aSPoul-Henning Kamp 			 * Set new address
105830106f6aSPoul-Henning Kamp 			 */
105930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
106030106f6aSPoul-Henning Kamp 			break;
106130106f6aSPoul-Henning Kamp 			}
106230106f6aSPoul-Henning Kamp #endif
106330106f6aSPoul-Henning Kamp 		default:
106430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
106530106f6aSPoul-Henning Kamp 			break;
106630106f6aSPoul-Henning Kamp 		}
106730106f6aSPoul-Henning Kamp 		break;
106830106f6aSPoul-Henning Kamp 
106930106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
107030106f6aSPoul-Henning Kamp 		{
107130106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
107230106f6aSPoul-Henning Kamp 
107330106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
10744a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(ifp),
107530106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
107630106f6aSPoul-Henning Kamp 		}
107730106f6aSPoul-Henning Kamp 		break;
1078fb583156SDavid Greenman 
1079fb583156SDavid Greenman 	case SIOCSIFMTU:
1080fb583156SDavid Greenman 		/*
1081fb583156SDavid Greenman 		 * Set the interface MTU.
1082fb583156SDavid Greenman 		 */
1083fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1084fb583156SDavid Greenman 			error = EINVAL;
1085fb583156SDavid Greenman 		} else {
1086fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
108730106f6aSPoul-Henning Kamp 		}
1088fb583156SDavid Greenman 		break;
1089c1d93b05SSam Leffler 	default:
1090c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1091c1d93b05SSam Leffler 		break;
1092fb583156SDavid Greenman 	}
1093fb583156SDavid Greenman 	return (error);
109430106f6aSPoul-Henning Kamp }
10951158dfb7SGarrett Wollman 
109637c84183SPoul-Henning Kamp static int
109772fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
109872fd1b6aSDag-Erling Smørgrav 	struct sockaddr *sa)
10991158dfb7SGarrett Wollman {
11001158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
1101d7647d96SDag-Erling Smørgrav #ifdef INET
11021158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
1103d7647d96SDag-Erling Smørgrav #endif
110482cd038dSYoshinobu Inoue #ifdef INET6
110582cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
110682cd038dSYoshinobu Inoue #endif
11071158dfb7SGarrett Wollman 	u_char *e_addr;
11081158dfb7SGarrett Wollman 
11091158dfb7SGarrett Wollman 	switch(sa->sa_family) {
11101158dfb7SGarrett Wollman 	case AF_LINK:
11117f33a738SJulian Elischer 		/*
11127f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
11137f33a738SJulian Elischer 		 */
11141158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
11151158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
1116086e98c4SBruce M Simpson 		if (!ETHER_IS_MULTICAST(e_addr))
11171158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11181158dfb7SGarrett Wollman 		*llsa = 0;
11191158dfb7SGarrett Wollman 		return 0;
11201158dfb7SGarrett Wollman 
11211158dfb7SGarrett Wollman #ifdef INET
11221158dfb7SGarrett Wollman 	case AF_INET:
11231158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
11241158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
11251158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11261158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
112709df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
112809df718eSRobert Watson 		if (sdl == NULL)
112909df718eSRobert Watson 			return ENOMEM;
11301158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
11311158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
11321158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
11331158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
11341158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11351158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11361158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11371158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11381158dfb7SGarrett Wollman 		return 0;
11391158dfb7SGarrett Wollman #endif
114082cd038dSYoshinobu Inoue #ifdef INET6
114182cd038dSYoshinobu Inoue 	case AF_INET6:
114282cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1143595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1144595b8a1cSJun-ichiro itojun Hagino 			/*
1145595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1146595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1147595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1148595b8a1cSJun-ichiro itojun Hagino 			 */
1149595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1150595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
1151595b8a1cSJun-ichiro itojun Hagino 			return 0;
1152595b8a1cSJun-ichiro itojun Hagino 		}
115382cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
115482cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
115582cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
115609df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
115709df718eSRobert Watson 		if (sdl == NULL)
115809df718eSRobert Watson 			return (ENOMEM);
115982cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
116082cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
116182cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
116282cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
116382cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
116482cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
116582cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
116682cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
116782cd038dSYoshinobu Inoue 		return 0;
116882cd038dSYoshinobu Inoue #endif
11691158dfb7SGarrett Wollman 
11701158dfb7SGarrett Wollman 	default:
11711158dfb7SGarrett Wollman 		/*
11721158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
11731158dfb7SGarrett Wollman 		 * that counts...
11741158dfb7SGarrett Wollman 		 */
11751158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
11761158dfb7SGarrett Wollman 	}
11771158dfb7SGarrett Wollman }
117840811c14SMatthew N. Dodd 
1179fc74a9f9SBrooks Davis static void*
1180fc74a9f9SBrooks Davis ether_alloc(u_char type, struct ifnet *ifp)
1181fc74a9f9SBrooks Davis {
1182fc74a9f9SBrooks Davis 	struct arpcom	*ac;
1183fc74a9f9SBrooks Davis 
1184fc74a9f9SBrooks Davis 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1185fc74a9f9SBrooks Davis 	ac->ac_ifp = ifp;
1186fc74a9f9SBrooks Davis 
1187fc74a9f9SBrooks Davis 	return (ac);
1188fc74a9f9SBrooks Davis }
1189fc74a9f9SBrooks Davis 
1190fc74a9f9SBrooks Davis static void
1191fc74a9f9SBrooks Davis ether_free(void *com, u_char type)
1192fc74a9f9SBrooks Davis {
1193fc74a9f9SBrooks Davis 
1194fc74a9f9SBrooks Davis 	free(com, M_ARPCOM);
1195fc74a9f9SBrooks Davis }
1196fc74a9f9SBrooks Davis 
1197fc74a9f9SBrooks Davis static int
1198fc74a9f9SBrooks Davis ether_modevent(module_t mod, int type, void *data)
1199fc74a9f9SBrooks Davis {
1200fc74a9f9SBrooks Davis 
1201fc74a9f9SBrooks Davis 	switch (type) {
1202fc74a9f9SBrooks Davis 	case MOD_LOAD:
1203fc74a9f9SBrooks Davis 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1204fc74a9f9SBrooks Davis 		break;
1205fc74a9f9SBrooks Davis 	case MOD_UNLOAD:
1206fc74a9f9SBrooks Davis 		if_deregister_com_alloc(IFT_ETHER);
1207fc74a9f9SBrooks Davis 		break;
1208fc74a9f9SBrooks Davis 	default:
1209fc74a9f9SBrooks Davis 		return EOPNOTSUPP;
1210fc74a9f9SBrooks Davis 	}
1211fc74a9f9SBrooks Davis 
1212fc74a9f9SBrooks Davis 	return (0);
1213fc74a9f9SBrooks Davis }
1214fc74a9f9SBrooks Davis 
121540811c14SMatthew N. Dodd static moduledata_t ether_mod = {
121640811c14SMatthew N. Dodd 	"ether",
1217fc74a9f9SBrooks Davis 	ether_modevent,
121840811c14SMatthew N. Dodd 	0
121940811c14SMatthew N. Dodd };
122040811c14SMatthew N. Dodd 
122152f1277eSChristian S.J. Peron void
122252f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
122352f1277eSChristian S.J. Peron {
122452f1277eSChristian S.J. Peron 	struct ether_vlan_header vlan;
122552f1277eSChristian S.J. Peron 	struct mbuf mv, mb;
122652f1277eSChristian S.J. Peron 
122752f1277eSChristian S.J. Peron 	KASSERT((m->m_flags & M_VLANTAG) != 0,
122852f1277eSChristian S.J. Peron 	    ("%s: vlan information not present", __func__));
122952f1277eSChristian S.J. Peron 	KASSERT(m->m_len >= sizeof(struct ether_header),
123052f1277eSChristian S.J. Peron 	    ("%s: mbuf not large enough for header", __func__));
123152f1277eSChristian S.J. Peron 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
123252f1277eSChristian S.J. Peron 	vlan.evl_proto = vlan.evl_encap_proto;
123352f1277eSChristian S.J. Peron 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
123452f1277eSChristian S.J. Peron 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
123552f1277eSChristian S.J. Peron 	m->m_len -= sizeof(struct ether_header);
123652f1277eSChristian S.J. Peron 	m->m_data += sizeof(struct ether_header);
123752f1277eSChristian S.J. Peron 	/*
123852f1277eSChristian S.J. Peron 	 * If a data link has been supplied by the caller, then we will need to
123952f1277eSChristian S.J. Peron 	 * re-create a stack allocated mbuf chain with the following structure:
124052f1277eSChristian S.J. Peron 	 *
124152f1277eSChristian S.J. Peron 	 * (1) mbuf #1 will contain the supplied data link
124252f1277eSChristian S.J. Peron 	 * (2) mbuf #2 will contain the vlan header
124352f1277eSChristian S.J. Peron 	 * (3) mbuf #3 will contain the original mbuf's packet data
124452f1277eSChristian S.J. Peron 	 *
124552f1277eSChristian S.J. Peron 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
124652f1277eSChristian S.J. Peron 	 */
124752f1277eSChristian S.J. Peron 	if (data != NULL) {
124852f1277eSChristian S.J. Peron 		mv.m_next = m;
124952f1277eSChristian S.J. Peron 		mv.m_data = (caddr_t)&vlan;
125052f1277eSChristian S.J. Peron 		mv.m_len = sizeof(vlan);
125152f1277eSChristian S.J. Peron 		mb.m_next = &mv;
125252f1277eSChristian S.J. Peron 		mb.m_data = data;
125352f1277eSChristian S.J. Peron 		mb.m_len = dlen;
125452f1277eSChristian S.J. Peron 		bpf_mtap(bp, &mb);
125552f1277eSChristian S.J. Peron 	} else
125652f1277eSChristian S.J. Peron 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
125752f1277eSChristian S.J. Peron 	m->m_len += sizeof(struct ether_header);
125852f1277eSChristian S.J. Peron 	m->m_data -= sizeof(struct ether_header);
125952f1277eSChristian S.J. Peron }
126052f1277eSChristian S.J. Peron 
1261fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
126240811c14SMatthew N. Dodd MODULE_VERSION(ether, 1);
1263