xref: /freebsd/sys/net/if_ethersubr.c (revision fc74a9f93a5fbc83262aa12084404ac953c854b5)
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"
37fb5fbe46SLuigi Rizzo #include "opt_bdg.h"
3843b29369SRobert Watson #include "opt_mac.h"
394cf49a43SJulian Elischer #include "opt_netgraph.h"
40a9771948SGleb Smirnoff #include "opt_carp.h"
41430df5f4SEivind Eklund 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4543b29369SRobert Watson #include <sys/mac.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4741ee9f1cSPoul-Henning Kamp #include <sys/module.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
4910b1fde0SMark Murray #include <sys/random.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
5151a53488SBruce Evans #include <sys/sockio.h>
52602d513cSGarrett Wollman #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55d7647d96SDag-Erling Smørgrav #include <net/if_arp.h>
56df8bae1dSRodney W. Grimes #include <net/netisr.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes #include <net/if_llc.h>
59df8bae1dSRodney W. Grimes #include <net/if_dl.h>
60df8bae1dSRodney W. Grimes #include <net/if_types.h>
612e2de7f2SArchie Cobbs #include <net/bpf.h>
62e1e1452dSArchie Cobbs #include <net/ethernet.h>
63db69a05dSPaul Saab #include <net/bridge.h>
64c1d93b05SSam Leffler #include <net/if_vlan_var.h>
65df8bae1dSRodney W. Grimes 
6682cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
67df8bae1dSRodney W. Grimes #include <netinet/in.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
704b984093SLuigi Rizzo #include <netinet/ip_fw.h>
714b984093SLuigi Rizzo #include <netinet/ip_dummynet.h>
721d5e9e22SEivind Eklund #endif
7382cd038dSYoshinobu Inoue #ifdef INET6
7482cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7582cd038dSYoshinobu Inoue #endif
76df8bae1dSRodney W. Grimes 
77a9771948SGleb Smirnoff #ifdef DEV_CARP
78a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
79a9771948SGleb Smirnoff #endif
80a9771948SGleb Smirnoff 
81cc6a66f2SJulian Elischer #ifdef IPX
82cc6a66f2SJulian Elischer #include <netipx/ipx.h>
83cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
84a1026028SMaxim Sobolev #endif
854f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
865accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
872a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
88cc6a66f2SJulian Elischer 
89655929bfSJulian Elischer #ifdef NETATALK
90655929bfSJulian Elischer #include <netatalk/at.h>
91655929bfSJulian Elischer #include <netatalk/at_var.h>
92655929bfSJulian Elischer #include <netatalk/at_extern.h>
93655929bfSJulian Elischer 
94655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
95655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
96655929bfSJulian Elischer 
97655929bfSJulian Elischer extern u_char	at_org_code[3];
98655929bfSJulian Elischer extern u_char	aarp_org_code[3];
992cc2df49SGarrett Wollman #endif /* NETATALK */
1002cc2df49SGarrett Wollman 
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 
110db69a05dSPaul Saab /* bridge support */
111cad15830SLuigi Rizzo int do_bridge;
112db69a05dSPaul Saab bridge_in_t *bridge_in_ptr;
113db69a05dSPaul Saab bdg_forward_t *bdg_forward_ptr;
114db69a05dSPaul Saab bdgtakeifaces_t *bdgtakeifaces_ptr;
115cad15830SLuigi Rizzo struct bdg_softc *ifp2sc;
116db69a05dSPaul Saab 
1178f867517SAndrew Thompson struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
1188f867517SAndrew Thompson int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
1198f867517SAndrew Thompson 		struct sockaddr *, struct rtentry *);
120c8b01292SAndrew Thompson void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
1218f867517SAndrew Thompson 
122e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
123868d8b62SMatthew N. Dodd 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
124868d8b62SMatthew N. Dodd 
125929ddbbbSAlfred Perlstein static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
126929ddbbbSAlfred Perlstein 		struct sockaddr *);
127868d8b62SMatthew N. Dodd 
128fc74a9f9SBrooks Davis /* XXX: should be in an arp support file, not here */
129fc74a9f9SBrooks Davis MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
130fc74a9f9SBrooks Davis 
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  * Assumes that ifp is actually pointer to arpcom structure.
146df8bae1dSRodney W. Grimes  */
147df8bae1dSRodney W. Grimes int
14872fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m,
14972fd1b6aSDag-Erling Smørgrav 	struct sockaddr *dst, struct rtentry *rt0)
150df8bae1dSRodney W. Grimes {
151df8bae1dSRodney W. Grimes 	short type;
152cd46a114SLuigi Rizzo 	int error, hdrcmplt = 0;
15364760eb0SWarner Losh 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
154098a8c3bSMatthew N. Dodd 	struct ether_header *eh;
1559961e27dSLuigi Rizzo 	int loop_copy = 0;
156d32864c3SRobert Watson 	int hlen;	/* link layer header length */
157df8bae1dSRodney W. Grimes 
15843b29369SRobert Watson #ifdef MAC
15943b29369SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
16043b29369SRobert Watson 	if (error)
16143b29369SRobert Watson 		senderr(error);
16243b29369SRobert Watson #endif
16343b29369SRobert Watson 
164afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR)
165afbe3a0fSPoul-Henning Kamp 		senderr(ENETDOWN);
166df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_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 
18443a6c75aSMatthew N. Dodd 		loop_copy = -1; /* 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:
2659d3f194dSJulian Elischer 		loop_copy = -1; /* 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
291fc74a9f9SBrooks Davis 		(void)memcpy(eh->ether_shost, IFP2ENADDR(ifp),
292df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
293ed7509acSJulian Elischer 
294ed7509acSJulian Elischer        /*
2958f867517SAndrew Thompson 	* Bridges require special output handling.
2968f867517SAndrew Thompson 	*/
2978f867517SAndrew Thompson 	if (ifp->if_bridge) {
2988f867517SAndrew Thompson 		KASSERT(bridge_output_p != NULL,
2998f867517SAndrew Thompson 		    ("ether_input: if_bridge not loaded!"));
3008f867517SAndrew Thompson 		return ((*bridge_output_p)(ifp, m, NULL, NULL));
3018f867517SAndrew Thompson 	}
3028f867517SAndrew Thompson 
3038f867517SAndrew Thompson 	/*
304ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
305ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
306ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
307ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
308ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
309ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
310ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
311ed7509acSJulian Elischer 	 */
3124c8e8c05SJulian Elischer 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
3133cd67511SJesper Skriver 		int csum_flags = 0;
3143cd67511SJesper Skriver 
3153cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3163cd67511SJesper Skriver 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
3173cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
3183cd67511SJesper Skriver 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
31906f684b0SMatthew N. Dodd 
3209d3f194dSJulian Elischer 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
32106f684b0SMatthew N. Dodd 			struct mbuf *n;
322ed7509acSJulian Elischer 
32379db6ff3SArchie Cobbs 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
3243cd67511SJesper Skriver 				n->m_pkthdr.csum_flags |= csum_flags;
3253cd67511SJesper Skriver 				if (csum_flags & CSUM_DATA_VALID)
3263cd67511SJesper Skriver 					n->m_pkthdr.csum_data = 0xffff;
32706a429a3SArchie Cobbs 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
328c1404dc0SArchie Cobbs 			} else
329c1404dc0SArchie Cobbs 				ifp->if_iqdrops++;
33006f684b0SMatthew N. Dodd 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
33106f684b0SMatthew N. Dodd 				ETHER_ADDR_LEN) == 0) {
3323cd67511SJesper Skriver 			m->m_pkthdr.csum_flags |= csum_flags;
3333cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3343cd67511SJesper Skriver 				m->m_pkthdr.csum_data = 0xffff;
33506a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
336ed7509acSJulian Elischer 			return (0);	/* XXX */
337ed7509acSJulian Elischer 		}
338ed7509acSJulian Elischer 	}
3392e2de7f2SArchie Cobbs 
340a9771948SGleb Smirnoff #ifdef DEV_CARP
341a9771948SGleb Smirnoff 	if (ifp->if_carp &&
342a9771948SGleb Smirnoff 	    (error = carp_output(ifp, m, dst, NULL)))
343a9771948SGleb Smirnoff 		goto bad;
344a9771948SGleb Smirnoff #endif
345a9771948SGleb Smirnoff 
346e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
347eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
348e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
349e1e1452dSArchie Cobbs bad:			if (m != NULL)
350e1e1452dSArchie Cobbs 				m_freem(m);
351e1e1452dSArchie Cobbs 			return (error);
352e1e1452dSArchie Cobbs 		}
353e1e1452dSArchie Cobbs 		if (m == NULL)
354e1e1452dSArchie Cobbs 			return (0);
355e1e1452dSArchie Cobbs 	}
356e1e1452dSArchie Cobbs 
357e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
358e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
359e1e1452dSArchie Cobbs }
360e1e1452dSArchie Cobbs 
361e1e1452dSArchie Cobbs /*
362e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
363e1e1452dSArchie Cobbs  *
364e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
365e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
366e1e1452dSArchie Cobbs  */
367e1e1452dSArchie Cobbs int
368c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
369e1e1452dSArchie Cobbs {
370d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
371ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
372d7647d96SDag-Erling Smørgrav #else
373d7647d96SDag-Erling Smørgrav 	void *rule = NULL;
374d7647d96SDag-Erling Smørgrav #endif
375affc907dSMax Laier 	int error;
3762b25acc1SLuigi Rizzo 
377c1d93b05SSam Leffler 	if (rule == NULL && BDG_ACTIVE(ifp)) {
378c1d93b05SSam Leffler 		/*
379c1d93b05SSam Leffler 		 * Beware, the bridge code notices the null rcvif and
380c1d93b05SSam Leffler 		 * uses that identify that it's being called from
381c1d93b05SSam Leffler 		 * ether_output as opposd to ether_input.  Yech.
382c1d93b05SSam Leffler 		 */
383fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
384c1d93b05SSam Leffler 		m = bdg_forward_ptr(m, ifp);
3852e2de7f2SArchie Cobbs 		if (m != NULL)
3862e2de7f2SArchie Cobbs 			m_freem(m);
3871db59ce6SEivind Eklund 		return (0);
388fb5fbe46SLuigi Rizzo 	}
389d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
3904b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
391c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
3924b984093SLuigi Rizzo 			if (m) {
3934b984093SLuigi Rizzo 				m_freem(m);
394e7909401SDavid Malone 				return EACCES;	/* pkt dropped */
3954b984093SLuigi Rizzo 			} else
3964b984093SLuigi Rizzo 				return 0;	/* consumed e.g. in a pipe */
3974b984093SLuigi Rizzo 		}
3984b984093SLuigi Rizzo 	}
399d7647d96SDag-Erling Smørgrav #endif
4002b25acc1SLuigi Rizzo 
401df8bae1dSRodney W. Grimes 	/*
402df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
403df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
404df8bae1dSRodney W. Grimes 	 */
405affc907dSMax Laier 	IFQ_HANDOFF(ifp, m, error);
406affc907dSMax Laier 	return (error);
407df8bae1dSRodney W. Grimes }
408df8bae1dSRodney W. Grimes 
409d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
410df8bae1dSRodney W. Grimes /*
4114b984093SLuigi Rizzo  * ipfw processing for ethernet packets (in and out).
4124b984093SLuigi Rizzo  * The second parameter is NULL from ether_demux, and ifp from
4134b984093SLuigi Rizzo  * ether_output_frame. This section of code could be used from
4142f8ebbf4SLuigi Rizzo  * bridge.c as well as long as we use some extra info
4154b984093SLuigi Rizzo  * to distinguish that case from ether_output_frame();
4164b984093SLuigi Rizzo  */
4174b984093SLuigi Rizzo int
4184b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
419c1d93b05SSam Leffler 	struct ip_fw **rule, int shared)
4204b984093SLuigi Rizzo {
421c1d93b05SSam Leffler 	struct ether_header *eh;
422c1d93b05SSam Leffler 	struct ether_header save_eh;
423c1d93b05SSam Leffler 	struct mbuf *m;
4244b984093SLuigi Rizzo 	int i;
4252b25acc1SLuigi Rizzo 	struct ip_fw_args args;
4264b984093SLuigi Rizzo 
42797850a5dSLuigi Rizzo 	if (*rule != NULL && fw_one_pass)
428015d72e0SLuigi Rizzo 		return 1; /* dummynet packet, already partially processed */
4292f8ebbf4SLuigi Rizzo 
4304b984093SLuigi Rizzo 	/*
4312f8ebbf4SLuigi Rizzo 	 * I need some amt of data to be contiguous, and in case others need
4324b984093SLuigi Rizzo 	 * the packet (shared==1) also better be in the first mbuf.
4334b984093SLuigi Rizzo 	 */
434c1d93b05SSam Leffler 	m = *m0;
435c1d93b05SSam Leffler 	i = min( m->m_pkthdr.len, max_protohdr);
436c1d93b05SSam Leffler 	if ( shared || m->m_len < i) {
437c1d93b05SSam Leffler 		m = m_pullup(m, i);
438c1d93b05SSam Leffler 		if (m == NULL) {
439c1d93b05SSam Leffler 			*m0 = m;
4404b984093SLuigi Rizzo 			return 0;
4414b984093SLuigi Rizzo 		}
442c1d93b05SSam Leffler 	}
443c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
444c1d93b05SSam Leffler 	save_eh = *eh;			/* save copy for restore below */
445c1d93b05SSam Leffler 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
4464b984093SLuigi Rizzo 
447c1d93b05SSam Leffler 	args.m = m;		/* the packet we are looking at		*/
4482b25acc1SLuigi Rizzo 	args.oif = dst;		/* destination, if any			*/
4492b25acc1SLuigi Rizzo 	args.rule = *rule;	/* matching rule to restart		*/
4502b25acc1SLuigi Rizzo 	args.next_hop = NULL;	/* we do not support forward yet	*/
4512b25acc1SLuigi Rizzo 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
4522b25acc1SLuigi Rizzo 	i = ip_fw_chk_ptr(&args);
453c1d93b05SSam Leffler 	m = args.m;
454c1d93b05SSam Leffler 	if (m != NULL) {
455c1d93b05SSam Leffler 		/*
456c1d93b05SSam Leffler 		 * Restore Ethernet header, as needed, in case the
457c1d93b05SSam Leffler 		 * mbuf chain was replaced by ipfw.
458c1d93b05SSam Leffler 		 */
459a163d034SWarner Losh 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
460c1d93b05SSam Leffler 		if (m == NULL) {
461c1d93b05SSam Leffler 			*m0 = m;
462c1d93b05SSam Leffler 			return 0;
463c1d93b05SSam Leffler 		}
464c1d93b05SSam Leffler 		if (eh != mtod(m, struct ether_header *))
465c1d93b05SSam Leffler 			bcopy(&save_eh, mtod(m, struct ether_header *),
466c1d93b05SSam Leffler 				ETHER_HDR_LEN);
467c1d93b05SSam Leffler 	}
468c1d93b05SSam Leffler 	*m0 = m;
4692b25acc1SLuigi Rizzo 	*rule = args.rule;
4702b25acc1SLuigi Rizzo 
4716c69a7c3SGleb Smirnoff 	if (i == IP_FW_DENY) /* drop */
4724b984093SLuigi Rizzo 		return 0;
4734b984093SLuigi Rizzo 
4746c69a7c3SGleb Smirnoff 	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
4756c69a7c3SGleb Smirnoff 
4766c69a7c3SGleb Smirnoff 	if (i == IP_FW_PASS) /* a PASS rule.  */
4774b984093SLuigi Rizzo 		return 1;
4784b984093SLuigi Rizzo 
4796c69a7c3SGleb Smirnoff 	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
4804b984093SLuigi Rizzo 		/*
4814b984093SLuigi Rizzo 		 * Pass the pkt to dummynet, which consumes it.
4824b984093SLuigi Rizzo 		 * If shared, make a copy and keep the original.
4834b984093SLuigi Rizzo 		 */
4844b984093SLuigi Rizzo 		if (shared) {
485a163d034SWarner Losh 			m = m_copypacket(m, M_DONTWAIT);
4862b25acc1SLuigi Rizzo 			if (m == NULL)
4874b984093SLuigi Rizzo 				return 0;
4884b984093SLuigi Rizzo 		} else {
4892b25acc1SLuigi Rizzo 			/*
490c1d93b05SSam Leffler 			 * Pass the original to dummynet and
491c1d93b05SSam Leffler 			 * nothing back to the caller
4922b25acc1SLuigi Rizzo 			 */
493c1d93b05SSam Leffler 			*m0 = NULL ;
4944b984093SLuigi Rizzo 		}
49528935658SGleb Smirnoff 		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
4964b984093SLuigi Rizzo 		return 0;
4974b984093SLuigi Rizzo 	}
4984b984093SLuigi Rizzo 	/*
4992f8ebbf4SLuigi Rizzo 	 * XXX at some point add support for divert/forward actions.
5002f8ebbf4SLuigi Rizzo 	 * If none of the above matches, we have to drop the pkt.
5014b984093SLuigi Rizzo 	 */
5024b984093SLuigi Rizzo 	return 0;
5034b984093SLuigi Rizzo }
504d7647d96SDag-Erling Smørgrav #endif
5054b984093SLuigi Rizzo 
5064b984093SLuigi Rizzo /*
507c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
508c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
509df8bae1dSRodney W. Grimes  */
510c1d93b05SSam Leffler static void
511c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m)
512df8bae1dSRodney W. Grimes {
513c1d93b05SSam Leffler 	struct ether_header *eh;
514c1d93b05SSam Leffler 	u_short etype;
515df8bae1dSRodney W. Grimes 
516c1d93b05SSam Leffler 	/*
517c1d93b05SSam Leffler 	 * Do consistency checks to verify assumptions
518c1d93b05SSam Leffler 	 * made by code past this point.
519c1d93b05SSam Leffler 	 */
520c1d93b05SSam Leffler 	if ((m->m_flags & M_PKTHDR) == 0) {
521c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o packet header\n");
522c1d93b05SSam Leffler 		ifp->if_ierrors++;
523c939f1aeSLuigi Rizzo 		m_freem(m);
524c939f1aeSLuigi Rizzo 		return;
525c939f1aeSLuigi Rizzo 	}
526797f247bSMatthew N. Dodd 	if (m->m_len < ETHER_HDR_LEN) {
527c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
528c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
529c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
530c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
531c1d93b05SSam Leffler 		ifp->if_ierrors++;
532c1d93b05SSam Leffler 		m_freem(m);
533c1d93b05SSam Leffler 		return;
5342e2de7f2SArchie Cobbs 	}
535c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
536c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
537c1d93b05SSam Leffler 	if (m->m_pkthdr.len >
538c1d93b05SSam Leffler 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
539c1d93b05SSam Leffler 		if_printf(ifp, "discard oversize frame "
540c1d93b05SSam Leffler 				"(ether type %x flags %x len %u > max %lu)\n",
541c1d93b05SSam Leffler 				etype, m->m_flags, m->m_pkthdr.len,
542c1d93b05SSam Leffler 				ETHER_MAX_FRAME(ifp, etype,
543c1d93b05SSam Leffler 						m->m_flags & M_HASFCS));
544c1d93b05SSam Leffler 		ifp->if_ierrors++;
545c1d93b05SSam Leffler 		m_freem(m);
546c1d93b05SSam Leffler 		return;
547c1d93b05SSam Leffler 	}
548c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif == NULL) {
549c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o interface pointer\n");
550c1d93b05SSam Leffler 		ifp->if_ierrors++;
551c1d93b05SSam Leffler 		m_freem(m);
552c1d93b05SSam Leffler 		return;
553c1d93b05SSam Leffler 	}
554c1d93b05SSam Leffler #ifdef DIAGNOSTIC
555c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif != ifp) {
5569bf40edeSBrooks Davis 		if_printf(ifp, "Warning, frame marked as received on %s\n",
5579bf40edeSBrooks Davis 			m->m_pkthdr.rcvif->if_xname);
558c1d93b05SSam Leffler 	}
559c1d93b05SSam Leffler #endif
560c1d93b05SSam Leffler 
561533d8562SRobert Watson #ifdef MAC
562533d8562SRobert Watson 	/*
563533d8562SRobert Watson 	 * Tag the mbuf with an appropriate MAC label before any other
564533d8562SRobert Watson 	 * consumers can get to it.
565533d8562SRobert Watson 	 */
566533d8562SRobert Watson 	mac_create_mbuf_from_ifnet(ifp, m);
567533d8562SRobert Watson #endif
568533d8562SRobert Watson 
569c1d93b05SSam Leffler 	/*
570c1d93b05SSam Leffler 	 * Give bpf a chance at the packet.
571c1d93b05SSam Leffler 	 */
572c1d93b05SSam Leffler 	BPF_MTAP(ifp, m);
5732e2de7f2SArchie Cobbs 
574afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR) {
575c1d93b05SSam Leffler 		/*
576c1d93b05SSam Leffler 		 * Interface marked for monitoring; discard packet.
577c1d93b05SSam Leffler 		 */
578afbe3a0fSPoul-Henning Kamp 		m_freem(m);
579afbe3a0fSPoul-Henning Kamp 		return;
580afbe3a0fSPoul-Henning Kamp 	}
581afbe3a0fSPoul-Henning Kamp 
582c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
583c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
584c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
585c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
586c1d93b05SSam Leffler 	}
587c1d93b05SSam Leffler 
588c1d93b05SSam Leffler 	ifp->if_ibytes += m->m_pkthdr.len;
58905463bb5SDavid Greenman 
590e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
591eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
592c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
593e1e1452dSArchie Cobbs 		if (m == NULL)
594e1e1452dSArchie Cobbs 			return;
595e1e1452dSArchie Cobbs 	}
596e1e1452dSArchie Cobbs 
5978f867517SAndrew Thompson 	/*
5988f867517SAndrew Thompson 	 * Tap the packet off here for a bridge.  bridge_input()
5998f867517SAndrew Thompson 	 * will return NULL if it has consumed the packet, otherwise
6008f867517SAndrew Thompson 	 * it gets processed as normal.  Note that bridge_input()
6018f867517SAndrew Thompson 	 * will always return the original packet if we need to
6028f867517SAndrew Thompson 	 * process it locally.
6038f867517SAndrew Thompson 	 */
6048f867517SAndrew Thompson 	if (ifp->if_bridge) {
6058f867517SAndrew Thompson 		KASSERT(bridge_input_p != NULL,
6068f867517SAndrew Thompson 		    ("ether_input: if_bridge not loaded!"));
6078f867517SAndrew Thompson 
6088f867517SAndrew Thompson 		/* Mark the packet as broadcast or multicast. This is also set
6098f867517SAndrew Thompson 		 * further down the code in ether_demux() but since the bridge
6108f867517SAndrew Thompson 		 * input routine rarely returns a mbuf for further processing,
6118f867517SAndrew Thompson 		 * it is an acceptable duplication.
6128f867517SAndrew Thompson 		 */
6138f867517SAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
6148f867517SAndrew Thompson 			if (bcmp(etherbroadcastaddr, eh->ether_dhost,
6158f867517SAndrew Thompson 				sizeof(etherbroadcastaddr)) == 0)
6168f867517SAndrew Thompson 				m->m_flags |= M_BCAST;
6178f867517SAndrew Thompson 			else
6188f867517SAndrew Thompson 				m->m_flags |= M_MCAST;
6198f867517SAndrew Thompson 		}
6208f867517SAndrew Thompson 
6218f867517SAndrew Thompson 		m = (*bridge_input_p)(ifp, m);
6228f867517SAndrew Thompson 		if (m == NULL)
6238f867517SAndrew Thompson 			return;
6248f867517SAndrew Thompson 		/*
6258f867517SAndrew Thompson 		 * Bridge has determined that the packet is for us.
6268f867517SAndrew Thompson 		 * Update our interface pointer -- we may have had
6278f867517SAndrew Thompson 		 * to "bridge" the packet locally.
6288f867517SAndrew Thompson 		 */
6298f867517SAndrew Thompson 		ifp = m->m_pkthdr.rcvif;
6308f867517SAndrew Thompson 	}
6318f867517SAndrew Thompson 
6322e2de7f2SArchie Cobbs 	/* Check for bridging mode */
633a176c2aeSGleb Smirnoff 	if (BDG_ACTIVE(ifp) )
634a176c2aeSGleb Smirnoff 		if ((m = bridge_in_ptr(ifp, m)) == NULL)
6352e2de7f2SArchie Cobbs 			return;
6362e2de7f2SArchie Cobbs 
637a6ddbff0SPoul-Henning Kamp 	/* First chunk of an mbuf contains good entropy */
63810b1fde0SMark Murray 	if (harvest.ethernet)
63910b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
640acf032f5SRobert Watson 	ether_demux(ifp, m);
641e1e1452dSArchie Cobbs }
642e1e1452dSArchie Cobbs 
643e1e1452dSArchie Cobbs /*
644e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
645e1e1452dSArchie Cobbs  */
646e1e1452dSArchie Cobbs void
647c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
648e1e1452dSArchie Cobbs {
649c1d93b05SSam Leffler 	struct ether_header *eh;
6501cafed39SJonathan Lemon 	int isr;
651e1e1452dSArchie Cobbs 	u_short ether_type;
652e1e1452dSArchie Cobbs #if defined(NETATALK)
653098a8c3bSMatthew N. Dodd 	struct llc *l;
654e1e1452dSArchie Cobbs #endif
655d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
656ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
657d7647d96SDag-Erling Smørgrav #endif
65836e8826fSMax Laier 
659c1d93b05SSam Leffler 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
660c1d93b05SSam Leffler 
661c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
662cd0cd014SJoerg Wunsch 	ether_type = ntohs(eh->ether_type);
663c1d93b05SSam Leffler 
664d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
6652b25acc1SLuigi Rizzo 	if (rule)	/* packet was already bridged */
6662b25acc1SLuigi Rizzo 		goto post_stats;
667d7647d96SDag-Erling Smørgrav #endif
6684b984093SLuigi Rizzo 
669cd0cd014SJoerg Wunsch 	if (!(BDG_ACTIVE(ifp)) &&
6706ee20ab5SRuslan Ermilov 	    !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
6716ee20ab5SRuslan Ermilov 	    ifp->if_nvlans > 0)) {
672a9771948SGleb Smirnoff #ifdef DEV_CARP
673a9771948SGleb Smirnoff 		/*
67458996b13SGleb Smirnoff 		 * XXX: Okay, we need to call carp_forus() and - if it is for
67558996b13SGleb Smirnoff 		 * us jump over code that does the normal check
676fc74a9f9SBrooks Davis 		 * "IFP2ENADDR(ifp) == ether_dhost". The check sequence is a bit
67758996b13SGleb Smirnoff 		 * different from OpenBSD, so we jump over as few code as
67858996b13SGleb Smirnoff 		 * possible, to catch _all_ sanity checks. This needs
67958996b13SGleb Smirnoff 		 * evaluation, to see if the carp ether_dhost values break any
68058996b13SGleb Smirnoff 		 * of these checks!
681a9771948SGleb Smirnoff 		 */
682a9771948SGleb Smirnoff 		if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
683a9771948SGleb Smirnoff 			goto pre_stats;
684a9771948SGleb Smirnoff #endif
685c6943100SPoul-Henning Kamp 		/*
686c6943100SPoul-Henning Kamp 		 * Discard packet if upper layers shouldn't see it because it
687c6943100SPoul-Henning Kamp 		 * was unicast to a different Ethernet address. If the driver
688c6943100SPoul-Henning Kamp 		 * is working properly, then this situation can only happen
689c6943100SPoul-Henning Kamp 		 * when the interface is in promiscuous mode.
690cd0cd014SJoerg Wunsch 		 *
691cd0cd014SJoerg Wunsch 		 * If VLANs are active, and this packet has a VLAN tag, do
692cd0cd014SJoerg Wunsch 		 * not drop it here but pass it on to the VLAN layer, to
693cd0cd014SJoerg Wunsch 		 * give them a chance to consider it as well (e. g. in case
694cd0cd014SJoerg Wunsch 		 * bridging is only active on a VLAN).  They will drop it if
695cd0cd014SJoerg Wunsch 		 * it's undesired.
696c6943100SPoul-Henning Kamp 		 */
697cb24f323SArchie Cobbs 		if ((ifp->if_flags & IFF_PROMISC) != 0
6986ee20ab5SRuslan Ermilov 		    && !ETHER_IS_MULTICAST(eh->ether_dhost)
699cb24f323SArchie Cobbs 		    && bcmp(eh->ether_dhost,
700fc74a9f9SBrooks Davis 		      IFP2ENADDR(ifp), ETHER_ADDR_LEN) != 0
70125faf49dSMaxim Sobolev 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
702cb24f323SArchie Cobbs 			    m_freem(m);
703cb24f323SArchie Cobbs 			    return;
704cb24f323SArchie Cobbs 		}
705c6943100SPoul-Henning Kamp 	}
706cb24f323SArchie Cobbs 
707a9771948SGleb Smirnoff #ifdef DEV_CARP
708a9771948SGleb Smirnoff pre_stats:
709a9771948SGleb Smirnoff #endif
7102e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
711df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
712df8bae1dSRodney W. Grimes 		m_freem(m);
713df8bae1dSRodney W. Grimes 		return;
714df8bae1dSRodney W. Grimes 	}
715588523faSSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
71664760eb0SWarner Losh 		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
717df8bae1dSRodney W. Grimes 		    sizeof(etherbroadcastaddr)) == 0)
718df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
7192cc2df49SGarrett Wollman 		else
720df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
7212cc2df49SGarrett Wollman 	}
722df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
723df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
724df8bae1dSRodney W. Grimes 
725d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
7264b984093SLuigi Rizzo post_stats:
7274b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
728c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
7294b984093SLuigi Rizzo 			if (m)
7304b984093SLuigi Rizzo 				m_freem(m);
7314b984093SLuigi Rizzo 			return;
7324b984093SLuigi Rizzo 		}
7334b984093SLuigi Rizzo 	}
734d7647d96SDag-Erling Smørgrav #endif
7354b984093SLuigi Rizzo 
736c1d93b05SSam Leffler 	/*
7376c23e6ccSRuslan Ermilov 	 * Check to see if the device performed the VLAN decapsulation and
738c1d93b05SSam Leffler 	 * provided us with the tag.
739c1d93b05SSam Leffler 	 */
7406ee20ab5SRuslan Ermilov 	if (m->m_flags & M_VLANTAG) {
741c1d93b05SSam Leffler 		/*
7426c23e6ccSRuslan Ermilov 		 * If no VLANs are configured, drop.
7436c23e6ccSRuslan Ermilov 		 */
7446c23e6ccSRuslan Ermilov 		if (ifp->if_nvlans == 0) {
7456c23e6ccSRuslan Ermilov 			ifp->if_noproto++;
7466c23e6ccSRuslan Ermilov 			m_freem(m);
7476c23e6ccSRuslan Ermilov 			return;
7486c23e6ccSRuslan Ermilov 		}
7496c23e6ccSRuslan Ermilov 		/*
750c1d93b05SSam Leffler 		 * vlan_input() will either recursively call ether_input()
751c1d93b05SSam Leffler 		 * or drop the packet.
752c1d93b05SSam Leffler 		 */
753c1d93b05SSam Leffler 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
754c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
755c1d93b05SSam Leffler 		return;
756c1d93b05SSam Leffler 	}
757c1d93b05SSam Leffler 
758c1d93b05SSam Leffler 	/*
759c1d93b05SSam Leffler 	 * Handle protocols that expect to have the Ethernet header
760c1d93b05SSam Leffler 	 * (and possibly FCS) intact.
761c1d93b05SSam Leffler 	 */
762c1d93b05SSam Leffler 	switch (ether_type) {
763c1d93b05SSam Leffler 	case ETHERTYPE_VLAN:
764c1d93b05SSam Leffler 		if (ifp->if_nvlans != 0) {
765c1d93b05SSam Leffler 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
766c1d93b05SSam Leffler 			(*vlan_input_p)(ifp, m);
767c1d93b05SSam Leffler 		} else {
768c1d93b05SSam Leffler 			ifp->if_noproto++;
769c1d93b05SSam Leffler 			m_freem(m);
770c1d93b05SSam Leffler 		}
771c1d93b05SSam Leffler 		return;
772c1d93b05SSam Leffler 	}
773c1d93b05SSam Leffler 
774c1d93b05SSam Leffler 	/* Strip off Ethernet header. */
775797f247bSMatthew N. Dodd 	m_adj(m, ETHER_HDR_LEN);
776c1d93b05SSam Leffler 
777c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
778c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
779c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
780c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
781c1d93b05SSam Leffler 	}
782c1d93b05SSam Leffler 
783307d80beSDavid Greenman 	switch (ether_type) {
784df8bae1dSRodney W. Grimes #ifdef INET
785df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
7869188b4a1SAndre Oppermann 		if (ip_fastforward(m))
7871f91d8c5SDavid Greenman 			return;
7881cafed39SJonathan Lemon 		isr = NETISR_IP;
789df8bae1dSRodney W. Grimes 		break;
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
79208aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
79308aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
79408aadfbbSJonathan Lemon 			m_freem(m);
79508aadfbbSJonathan Lemon 			return;
79608aadfbbSJonathan Lemon 		}
7971cafed39SJonathan Lemon 		isr = NETISR_ARP;
798df8bae1dSRodney W. Grimes 		break;
799df8bae1dSRodney W. Grimes #endif
800cc6a66f2SJulian Elischer #ifdef IPX
801cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
8024f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8034f93599fSBoris Popov 			return;
8041cafed39SJonathan Lemon 		isr = NETISR_IPX;
805cc6a66f2SJulian Elischer 		break;
806cc6a66f2SJulian Elischer #endif
80782cd038dSYoshinobu Inoue #ifdef INET6
80882cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
8091cafed39SJonathan Lemon 		isr = NETISR_IPV6;
81082cd038dSYoshinobu Inoue 		break;
81182cd038dSYoshinobu Inoue #endif
812655929bfSJulian Elischer #ifdef NETATALK
813655929bfSJulian Elischer 	case ETHERTYPE_AT:
8141cafed39SJonathan Lemon 		isr = NETISR_ATALK1;
815655929bfSJulian Elischer 		break;
816655929bfSJulian Elischer 	case ETHERTYPE_AARP:
8171cafed39SJonathan Lemon 		isr = NETISR_AARP;
8181cafed39SJonathan Lemon 		break;
8198cdfefbdSPeter Wemm #endif /* NETATALK */
820df8bae1dSRodney W. Grimes 	default:
8214f93599fSBoris Popov #ifdef IPX
8224f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8234f93599fSBoris Popov 			return;
8244f93599fSBoris Popov #endif /* IPX */
825242c5536SPeter Wemm #if defined(NETATALK)
826307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
827c1d93b05SSam Leffler 			goto discard;
828df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
8291cafed39SJonathan Lemon 		if (l->llc_dsap == LLC_SNAP_LSAP &&
8301cafed39SJonathan Lemon 		    l->llc_ssap == LLC_SNAP_LSAP &&
8311cafed39SJonathan Lemon 		    l->llc_control == LLC_UI) {
8324158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
833655929bfSJulian Elischer 			    sizeof(at_org_code)) == 0 &&
834655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
835797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8361cafed39SJonathan Lemon 				isr = NETISR_ATALK2;
837655929bfSJulian Elischer 				break;
838655929bfSJulian Elischer 			}
8394158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
840655929bfSJulian Elischer 			    sizeof(aarp_org_code)) == 0 &&
841655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
842797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8431cafed39SJonathan Lemon 				isr = NETISR_AARP;
844655929bfSJulian Elischer 				break;
845df8bae1dSRodney W. Grimes 			}
8461cafed39SJonathan Lemon 		}
847242c5536SPeter Wemm #endif /* NETATALK */
8481cafed39SJonathan Lemon 		goto discard;
849df8bae1dSRodney W. Grimes 	}
8501cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
851c1d93b05SSam Leffler 	return;
8521cafed39SJonathan Lemon 
853c1d93b05SSam Leffler discard:
854c1d93b05SSam Leffler 	/*
855c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
856c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
857c1d93b05SSam Leffler 	 * otherwise dispose of it.
858c1d93b05SSam Leffler 	 */
859eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
860c1d93b05SSam Leffler 		/*
861c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
862c1d93b05SSam Leffler 		 * consistent view of inbound packets.
863c1d93b05SSam Leffler 		 */
864797f247bSMatthew N. Dodd 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
865c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
866c1d93b05SSam Leffler 		return;
867c1d93b05SSam Leffler 	}
868c1d93b05SSam Leffler 	m_freem(m);
869c1d93b05SSam Leffler }
870c1d93b05SSam Leffler 
871c1d93b05SSam Leffler /*
872c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
873c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
874c1d93b05SSam Leffler  *
875c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
876c1d93b05SSam Leffler  *
877c1d93b05SSam Leffler  * since there's no static buffer involved.
878c1d93b05SSam Leffler  */
879c1d93b05SSam Leffler char *
880c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
881c1d93b05SSam Leffler {
882c1d93b05SSam Leffler 	static char etherbuf[18];
883c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
884c1d93b05SSam Leffler 	return (etherbuf);
885df8bae1dSRodney W. Grimes }
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes /*
888df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
889df8bae1dSRodney W. Grimes  */
890df8bae1dSRodney W. Grimes void
891c1d93b05SSam Leffler ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
892df8bae1dSRodney W. Grimes {
893f93dfa28SBrooks Davis 	int i;
894098a8c3bSMatthew N. Dodd 	struct ifaddr *ifa;
895098a8c3bSMatthew N. Dodd 	struct sockaddr_dl *sdl;
896df8bae1dSRodney W. Grimes 
897c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
898c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
899cfeff1b6SJonathan Lemon 	if_attach(ifp);
900df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
901c1d93b05SSam Leffler 	ifp->if_output = ether_output;
902c1d93b05SSam Leffler 	ifp->if_input = ether_input;
9031158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
904a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
905c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
906322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
907c1d93b05SSam Leffler 
908f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
9096e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
91059562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
911df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
912df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
913c1d93b05SSam Leffler 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
914c1d93b05SSam Leffler 	/*
915c1d93b05SSam Leffler 	 * XXX: This doesn't belong here; we do it until
916c1d93b05SSam Leffler 	 * XXX:  all drivers are cleaned up
917c1d93b05SSam Leffler 	 */
918fc74a9f9SBrooks Davis 	if (llc != IFP2ENADDR(ifp))
919fc74a9f9SBrooks Davis 		bcopy(llc, IFP2ENADDR(ifp), ifp->if_addrlen);
920c1d93b05SSam Leffler 
921797f247bSMatthew N. Dodd 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
922e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
923e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
9247b109fa4SLuigi Rizzo 	if (BDG_LOADED)
925db69a05dSPaul Saab 		bdgtakeifaces_ptr();
926e3bbbec2SMatthew N. Dodd 
927f93dfa28SBrooks Davis 	/* Announce Ethernet MAC address if non-zero. */
928f93dfa28SBrooks Davis 	for (i = 0; i < ifp->if_addrlen; i++)
929f93dfa28SBrooks Davis 		if (llc[i] != 0)
930f93dfa28SBrooks Davis 			break;
931f93dfa28SBrooks Davis 	if (i != ifp->if_addrlen)
932e3bbbec2SMatthew N. Dodd 		if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
933af5e59bfSRobert Watson 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
934af5e59bfSRobert Watson 		if_printf(ifp, "if_start running deferred for Giant\n");
935e1e1452dSArchie Cobbs }
936e1e1452dSArchie Cobbs 
93721b8ebd9SArchie Cobbs /*
93821b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
93921b8ebd9SArchie Cobbs  */
94021b8ebd9SArchie Cobbs void
941c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
94221b8ebd9SArchie Cobbs {
943eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL)
94421b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
94521b8ebd9SArchie Cobbs 	bpfdetach(ifp);
94621b8ebd9SArchie Cobbs 	if_detach(ifp);
9477b109fa4SLuigi Rizzo 	if (BDG_LOADED)
948db69a05dSPaul Saab 		bdgtakeifaces_ptr();
94921b8ebd9SArchie Cobbs }
95021b8ebd9SArchie Cobbs 
951ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
952602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
953d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
9544b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
9554b984093SLuigi Rizzo 	    &ether_ipfw,0,"Pass ether pkts through firewall");
956d7647d96SDag-Erling Smørgrav #endif
95730106f6aSPoul-Henning Kamp 
95816b4a343SChristian Weisgerber #if 0
95916b4a343SChristian Weisgerber /*
96016b4a343SChristian Weisgerber  * This is for reference.  We have a table-driven version
96116b4a343SChristian Weisgerber  * of the little-endian crc32 generator, which is faster
96216b4a343SChristian Weisgerber  * than the double-loop.
96316b4a343SChristian Weisgerber  */
96416b4a343SChristian Weisgerber uint32_t
96516b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
96616b4a343SChristian Weisgerber {
96716b4a343SChristian Weisgerber 	size_t i;
96816b4a343SChristian Weisgerber 	uint32_t crc;
96916b4a343SChristian Weisgerber 	int bit;
97016b4a343SChristian Weisgerber 	uint8_t data;
97116b4a343SChristian Weisgerber 
97216b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
97316b4a343SChristian Weisgerber 
97416b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
97516b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
97616b4a343SChristian Weisgerber 			carry = (crc ^ data) & 1;
97716b4a343SChristian Weisgerber 			crc >>= 1;
97816b4a343SChristian Weisgerber 			if (carry)
97916b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_LE);
98016b4a343SChristian Weisgerber 	}
98116b4a343SChristian Weisgerber 
98216b4a343SChristian Weisgerber 	return (crc);
98316b4a343SChristian Weisgerber }
98416b4a343SChristian Weisgerber #else
98516b4a343SChristian Weisgerber uint32_t
98616b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
98716b4a343SChristian Weisgerber {
98816b4a343SChristian Weisgerber 	static const uint32_t crctab[] = {
98916b4a343SChristian Weisgerber 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
99016b4a343SChristian Weisgerber 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
99116b4a343SChristian Weisgerber 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
99216b4a343SChristian Weisgerber 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
99316b4a343SChristian Weisgerber 	};
99416b4a343SChristian Weisgerber 	size_t i;
99516b4a343SChristian Weisgerber 	uint32_t crc;
99616b4a343SChristian Weisgerber 
99716b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
99816b4a343SChristian Weisgerber 
99916b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
100016b4a343SChristian Weisgerber 		crc ^= buf[i];
100116b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
100216b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
100316b4a343SChristian Weisgerber 	}
100416b4a343SChristian Weisgerber 
100516b4a343SChristian Weisgerber 	return (crc);
100616b4a343SChristian Weisgerber }
100716b4a343SChristian Weisgerber #endif
100816b4a343SChristian Weisgerber 
100916b4a343SChristian Weisgerber uint32_t
101016b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len)
101116b4a343SChristian Weisgerber {
101216b4a343SChristian Weisgerber 	size_t i;
101316b4a343SChristian Weisgerber 	uint32_t crc, carry;
101416b4a343SChristian Weisgerber 	int bit;
101516b4a343SChristian Weisgerber 	uint8_t data;
101616b4a343SChristian Weisgerber 
101716b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
101816b4a343SChristian Weisgerber 
101916b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
102016b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
102116b4a343SChristian Weisgerber 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
102216b4a343SChristian Weisgerber 			crc <<= 1;
102316b4a343SChristian Weisgerber 			if (carry)
102416b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
102516b4a343SChristian Weisgerber 		}
102616b4a343SChristian Weisgerber 	}
102716b4a343SChristian Weisgerber 
102816b4a343SChristian Weisgerber 	return (crc);
102916b4a343SChristian Weisgerber }
103016b4a343SChristian Weisgerber 
1031fb583156SDavid Greenman int
103272fd1b6aSDag-Erling Smørgrav ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
103330106f6aSPoul-Henning Kamp {
103430106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
103530106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
1036fb583156SDavid Greenman 	int error = 0;
103730106f6aSPoul-Henning Kamp 
103830106f6aSPoul-Henning Kamp 	switch (command) {
103930106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
104030106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
104130106f6aSPoul-Henning Kamp 
104230106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
104330106f6aSPoul-Henning Kamp #ifdef INET
104430106f6aSPoul-Henning Kamp 		case AF_INET:
104530106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1046322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
104730106f6aSPoul-Henning Kamp 			break;
104830106f6aSPoul-Henning Kamp #endif
104930106f6aSPoul-Henning Kamp #ifdef IPX
105030106f6aSPoul-Henning Kamp 		/*
105130106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
105230106f6aSPoul-Henning Kamp 		 */
105330106f6aSPoul-Henning Kamp 		case AF_IPX:
105430106f6aSPoul-Henning Kamp 			{
1055098a8c3bSMatthew N. Dodd 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
105630106f6aSPoul-Henning Kamp 
105730106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
105830106f6aSPoul-Henning Kamp 				ina->x_host =
105986101139SPoul-Henning Kamp 				    *(union ipx_host *)
1060fc74a9f9SBrooks Davis 				    IFP2ENADDR(ifp);
106130106f6aSPoul-Henning Kamp 			else {
106230106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
1063fc74a9f9SBrooks Davis 				      (caddr_t) IFP2ENADDR(ifp),
1064fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
106530106f6aSPoul-Henning Kamp 			}
106630106f6aSPoul-Henning Kamp 
106730106f6aSPoul-Henning Kamp 			/*
106830106f6aSPoul-Henning Kamp 			 * Set new address
106930106f6aSPoul-Henning Kamp 			 */
107030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
107130106f6aSPoul-Henning Kamp 			break;
107230106f6aSPoul-Henning Kamp 			}
107330106f6aSPoul-Henning Kamp #endif
107430106f6aSPoul-Henning Kamp 		default:
107530106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
107630106f6aSPoul-Henning Kamp 			break;
107730106f6aSPoul-Henning Kamp 		}
107830106f6aSPoul-Henning Kamp 		break;
107930106f6aSPoul-Henning Kamp 
108030106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
108130106f6aSPoul-Henning Kamp 		{
108230106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
108330106f6aSPoul-Henning Kamp 
108430106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
1085fc74a9f9SBrooks Davis 			bcopy(IFP2ENADDR(ifp),
108630106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
108730106f6aSPoul-Henning Kamp 		}
108830106f6aSPoul-Henning Kamp 		break;
1089fb583156SDavid Greenman 
1090fb583156SDavid Greenman 	case SIOCSIFMTU:
1091fb583156SDavid Greenman 		/*
1092fb583156SDavid Greenman 		 * Set the interface MTU.
1093fb583156SDavid Greenman 		 */
1094fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1095fb583156SDavid Greenman 			error = EINVAL;
1096fb583156SDavid Greenman 		} else {
1097fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
109830106f6aSPoul-Henning Kamp 		}
1099fb583156SDavid Greenman 		break;
1100c1d93b05SSam Leffler 	default:
1101c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1102c1d93b05SSam Leffler 		break;
1103fb583156SDavid Greenman 	}
1104fb583156SDavid Greenman 	return (error);
110530106f6aSPoul-Henning Kamp }
11061158dfb7SGarrett Wollman 
110737c84183SPoul-Henning Kamp static int
110872fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
110972fd1b6aSDag-Erling Smørgrav 	struct sockaddr *sa)
11101158dfb7SGarrett Wollman {
11111158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
1112d7647d96SDag-Erling Smørgrav #ifdef INET
11131158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
1114d7647d96SDag-Erling Smørgrav #endif
111582cd038dSYoshinobu Inoue #ifdef INET6
111682cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
111782cd038dSYoshinobu Inoue #endif
11181158dfb7SGarrett Wollman 	u_char *e_addr;
11191158dfb7SGarrett Wollman 
11201158dfb7SGarrett Wollman 	switch(sa->sa_family) {
11211158dfb7SGarrett Wollman 	case AF_LINK:
11227f33a738SJulian Elischer 		/*
11237f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
11247f33a738SJulian Elischer 		 */
11251158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
11261158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
1127086e98c4SBruce M Simpson 		if (!ETHER_IS_MULTICAST(e_addr))
11281158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11291158dfb7SGarrett Wollman 		*llsa = 0;
11301158dfb7SGarrett Wollman 		return 0;
11311158dfb7SGarrett Wollman 
11321158dfb7SGarrett Wollman #ifdef INET
11331158dfb7SGarrett Wollman 	case AF_INET:
11341158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
11351158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
11361158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11371158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1138a163d034SWarner Losh 		       M_WAITOK|M_ZERO);
11391158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
11401158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
11411158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
11421158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
11431158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11441158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11451158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11461158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11471158dfb7SGarrett Wollman 		return 0;
11481158dfb7SGarrett Wollman #endif
114982cd038dSYoshinobu Inoue #ifdef INET6
115082cd038dSYoshinobu Inoue 	case AF_INET6:
115182cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1152595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1153595b8a1cSJun-ichiro itojun Hagino 			/*
1154595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1155595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1156595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1157595b8a1cSJun-ichiro itojun Hagino 			 */
1158595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1159595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
1160595b8a1cSJun-ichiro itojun Hagino 			return 0;
1161595b8a1cSJun-ichiro itojun Hagino 		}
116282cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
116382cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
116482cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1165a163d034SWarner Losh 		       M_WAITOK|M_ZERO);
116682cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
116782cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
116882cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
116982cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
117082cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
117182cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
117282cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
117382cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
117482cd038dSYoshinobu Inoue 		return 0;
117582cd038dSYoshinobu Inoue #endif
11761158dfb7SGarrett Wollman 
11771158dfb7SGarrett Wollman 	default:
11781158dfb7SGarrett Wollman 		/*
11791158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
11801158dfb7SGarrett Wollman 		 * that counts...
11811158dfb7SGarrett Wollman 		 */
11821158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
11831158dfb7SGarrett Wollman 	}
11841158dfb7SGarrett Wollman }
118540811c14SMatthew N. Dodd 
1186fc74a9f9SBrooks Davis static void*
1187fc74a9f9SBrooks Davis ether_alloc(u_char type, struct ifnet *ifp)
1188fc74a9f9SBrooks Davis {
1189fc74a9f9SBrooks Davis 	struct arpcom	*ac;
1190fc74a9f9SBrooks Davis 
1191fc74a9f9SBrooks Davis 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1192fc74a9f9SBrooks Davis 	ac->ac_ifp = ifp;
1193fc74a9f9SBrooks Davis 
1194fc74a9f9SBrooks Davis 	return (ac);
1195fc74a9f9SBrooks Davis }
1196fc74a9f9SBrooks Davis 
1197fc74a9f9SBrooks Davis static void
1198fc74a9f9SBrooks Davis ether_free(void *com, u_char type)
1199fc74a9f9SBrooks Davis {
1200fc74a9f9SBrooks Davis 
1201fc74a9f9SBrooks Davis 	free(com, M_ARPCOM);
1202fc74a9f9SBrooks Davis }
1203fc74a9f9SBrooks Davis 
1204fc74a9f9SBrooks Davis static int
1205fc74a9f9SBrooks Davis ether_modevent(module_t mod, int type, void *data)
1206fc74a9f9SBrooks Davis {
1207fc74a9f9SBrooks Davis 
1208fc74a9f9SBrooks Davis 	switch (type) {
1209fc74a9f9SBrooks Davis 	case MOD_LOAD:
1210fc74a9f9SBrooks Davis 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1211fc74a9f9SBrooks Davis 		break;
1212fc74a9f9SBrooks Davis 	case MOD_UNLOAD:
1213fc74a9f9SBrooks Davis 		if_deregister_com_alloc(IFT_ETHER);
1214fc74a9f9SBrooks Davis 		break;
1215fc74a9f9SBrooks Davis 	default:
1216fc74a9f9SBrooks Davis 		return EOPNOTSUPP;
1217fc74a9f9SBrooks Davis 	}
1218fc74a9f9SBrooks Davis 
1219fc74a9f9SBrooks Davis 	return (0);
1220fc74a9f9SBrooks Davis }
1221fc74a9f9SBrooks Davis 
122240811c14SMatthew N. Dodd static moduledata_t ether_mod = {
122340811c14SMatthew N. Dodd 	"ether",
1224fc74a9f9SBrooks Davis 	ether_modevent,
122540811c14SMatthew N. Dodd 	0
122640811c14SMatthew N. Dodd };
122740811c14SMatthew N. Dodd 
1228fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
122940811c14SMatthew N. Dodd MODULE_VERSION(ether, 1);
1230