xref: /freebsd/sys/net/if_ethersubr.c (revision 0bdf5171c856a1657a79c24f1fd3c36189d3f13a)
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);
16613f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
16713f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
168df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1697f760c48SMatthew N. Dodd 
17084dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
171df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
172df8bae1dSRodney W. Grimes #ifdef INET
173df8bae1dSRodney W. Grimes 	case AF_INET:
174cd46a114SLuigi Rizzo 		error = arpresolve(ifp, rt0, m, dst, edst);
175cd46a114SLuigi Rizzo 		if (error)
176cd46a114SLuigi Rizzo 			return (error == EWOULDBLOCK ? 0 : error);
17734bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
178df8bae1dSRodney W. Grimes 		break;
17943a6c75aSMatthew N. Dodd 	case AF_ARP:
18043a6c75aSMatthew N. Dodd 	{
18143a6c75aSMatthew N. Dodd 		struct arphdr *ah;
18243a6c75aSMatthew N. Dodd 		ah = mtod(m, struct arphdr *);
18343a6c75aSMatthew N. Dodd 		ah->ar_hrd = htons(ARPHRD_ETHER);
18443a6c75aSMatthew N. Dodd 
18543a6c75aSMatthew N. Dodd 		loop_copy = -1; /* if this is for us, don't do it */
18643a6c75aSMatthew N. Dodd 
18743a6c75aSMatthew N. Dodd 		switch(ntohs(ah->ar_op)) {
18843a6c75aSMatthew N. Dodd 		case ARPOP_REVREQUEST:
18943a6c75aSMatthew N. Dodd 		case ARPOP_REVREPLY:
19043a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_REVARP);
19143a6c75aSMatthew N. Dodd 			break;
19243a6c75aSMatthew N. Dodd 		case ARPOP_REQUEST:
19343a6c75aSMatthew N. Dodd 		case ARPOP_REPLY:
19443a6c75aSMatthew N. Dodd 		default:
19543a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_ARP);
19643a6c75aSMatthew N. Dodd 			break;
19743a6c75aSMatthew N. Dodd 		}
19843a6c75aSMatthew N. Dodd 
19943a6c75aSMatthew N. Dodd 		if (m->m_flags & M_BCAST)
20043a6c75aSMatthew N. Dodd 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
20143a6c75aSMatthew N. Dodd 		else
20243a6c75aSMatthew N. Dodd 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
20343a6c75aSMatthew N. Dodd 
20443a6c75aSMatthew N. Dodd 	}
20543a6c75aSMatthew N. Dodd 	break;
206df8bae1dSRodney W. Grimes #endif
20782cd038dSYoshinobu Inoue #ifdef INET6
20882cd038dSYoshinobu Inoue 	case AF_INET6:
209cd46a114SLuigi Rizzo 		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
210cd46a114SLuigi Rizzo 		if (error)
211cd46a114SLuigi Rizzo 			return error;
21282cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
21382cd038dSYoshinobu Inoue 		break;
21482cd038dSYoshinobu Inoue #endif
215cc6a66f2SJulian Elischer #ifdef IPX
216cc6a66f2SJulian Elischer 	case AF_IPX:
2174f93599fSBoris Popov 		if (ef_outputp) {
2182a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
2195accfb8cSBoris Popov 		    if (error)
2205accfb8cSBoris Popov 			goto bad;
2214f93599fSBoris Popov 		} else
22234bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
223cc6a66f2SJulian Elischer 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
224cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
225cc6a66f2SJulian Elischer 		break;
226cc6a66f2SJulian Elischer #endif
227655929bfSJulian Elischer #ifdef NETATALK
228655929bfSJulian Elischer 	case AF_APPLETALK:
2291d0eab59SJulian Elischer 	  {
230ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2311d0eab59SJulian Elischer 
232cd46a114SLuigi Rizzo 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
233cd46a114SLuigi Rizzo 		    senderr(EHOSTUNREACH); /* XXX */
234cd46a114SLuigi Rizzo 	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
2351d0eab59SJulian Elischer 		    return (0);
236655929bfSJulian Elischer 	    /*
237ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
238655929bfSJulian Elischer 	     */
239655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
240655929bfSJulian Elischer 		struct llc llc;
241655929bfSJulian Elischer 
2427e2041e0SRobert Watson 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
2432166ffe8SRobert Watson 		if (m == NULL)
2442166ffe8SRobert Watson 			senderr(ENOBUFS);
245655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
246655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
247655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
248655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
249797f247bSMatthew N. Dodd 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
25034bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
251797f247bSMatthew N. Dodd 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
252655929bfSJulian Elischer 	    } else {
25334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
254655929bfSJulian Elischer 	    }
255655929bfSJulian Elischer 	    break;
256ed7509acSJulian Elischer 	  }
2578cdfefbdSPeter Wemm #endif /* NETATALK */
258df8bae1dSRodney W. Grimes 
259114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
260114ae644SMike Smith 		hdrcmplt = 1;
261114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
262114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
263114ae644SMike Smith 		/* FALLTHROUGH */
264114ae644SMike Smith 
265df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
2669d3f194dSJulian Elischer 		loop_copy = -1; /* if this is for us, don't do it */
267df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
26894a5d9b6SDavid Greenman 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
269df8bae1dSRodney W. Grimes 		type = eh->ether_type;
270df8bae1dSRodney W. Grimes 		break;
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes 	default:
27329e1b85fSBrooks Davis 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
274df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
275df8bae1dSRodney W. Grimes 	}
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 	/*
278df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
279df8bae1dSRodney W. Grimes 	 * allocate another.
280df8bae1dSRodney W. Grimes 	 */
281797f247bSMatthew N. Dodd 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
2822166ffe8SRobert Watson 	if (m == NULL)
283df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
284df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
28594a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
286df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
28794a5d9b6SDavid Greenman 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
288114ae644SMike Smith 	if (hdrcmplt)
289114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
290114ae644SMike Smith 			sizeof(eh->ether_shost));
291114ae644SMike Smith 	else
292fc74a9f9SBrooks Davis 		(void)memcpy(eh->ether_shost, IFP2ENADDR(ifp),
293df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
294ed7509acSJulian Elischer 
295ed7509acSJulian Elischer        /*
2968f867517SAndrew Thompson 	* Bridges require special output handling.
2978f867517SAndrew Thompson 	*/
2988f867517SAndrew Thompson 	if (ifp->if_bridge) {
2998f867517SAndrew Thompson 		KASSERT(bridge_output_p != NULL,
3008f867517SAndrew Thompson 		    ("ether_input: if_bridge not loaded!"));
3018f867517SAndrew Thompson 		return ((*bridge_output_p)(ifp, m, NULL, NULL));
3028f867517SAndrew Thompson 	}
3038f867517SAndrew Thompson 
3048f867517SAndrew Thompson 	/*
305ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
306ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
307ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
308ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
309ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
310ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
311ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
312ed7509acSJulian Elischer 	 */
3130bdf5171SMax Laier 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1) &&
3140bdf5171SMax Laier 	    m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
3153cd67511SJesper Skriver 		int csum_flags = 0;
3163cd67511SJesper Skriver 
3173cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3183cd67511SJesper Skriver 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
3193cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
3203cd67511SJesper Skriver 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
32106f684b0SMatthew N. Dodd 
3229d3f194dSJulian Elischer 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
32306f684b0SMatthew N. Dodd 			struct mbuf *n;
324ed7509acSJulian Elischer 
32579db6ff3SArchie Cobbs 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
3263cd67511SJesper Skriver 				n->m_pkthdr.csum_flags |= csum_flags;
3273cd67511SJesper Skriver 				if (csum_flags & CSUM_DATA_VALID)
3283cd67511SJesper Skriver 					n->m_pkthdr.csum_data = 0xffff;
32906a429a3SArchie Cobbs 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
330c1404dc0SArchie Cobbs 			} else
331c1404dc0SArchie Cobbs 				ifp->if_iqdrops++;
33206f684b0SMatthew N. Dodd 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
33306f684b0SMatthew N. Dodd 				ETHER_ADDR_LEN) == 0) {
3343cd67511SJesper Skriver 			m->m_pkthdr.csum_flags |= csum_flags;
3353cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3363cd67511SJesper Skriver 				m->m_pkthdr.csum_data = 0xffff;
33706a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
338ed7509acSJulian Elischer 			return (0);	/* XXX */
339ed7509acSJulian Elischer 		}
340ed7509acSJulian Elischer 	}
3412e2de7f2SArchie Cobbs 
342a9771948SGleb Smirnoff #ifdef DEV_CARP
343a9771948SGleb Smirnoff 	if (ifp->if_carp &&
344a9771948SGleb Smirnoff 	    (error = carp_output(ifp, m, dst, NULL)))
345a9771948SGleb Smirnoff 		goto bad;
346a9771948SGleb Smirnoff #endif
347a9771948SGleb Smirnoff 
348e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
349eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
350514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_output_p != NULL,
351514bcb89SPoul-Henning Kamp 		    ("ng_ether_output_p is NULL"));
352e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
353e1e1452dSArchie Cobbs bad:			if (m != NULL)
354e1e1452dSArchie Cobbs 				m_freem(m);
355e1e1452dSArchie Cobbs 			return (error);
356e1e1452dSArchie Cobbs 		}
357e1e1452dSArchie Cobbs 		if (m == NULL)
358e1e1452dSArchie Cobbs 			return (0);
359e1e1452dSArchie Cobbs 	}
360e1e1452dSArchie Cobbs 
361e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
362e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
363e1e1452dSArchie Cobbs }
364e1e1452dSArchie Cobbs 
365e1e1452dSArchie Cobbs /*
366e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
367e1e1452dSArchie Cobbs  *
368e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
369e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
370e1e1452dSArchie Cobbs  */
371e1e1452dSArchie Cobbs int
372c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
373e1e1452dSArchie Cobbs {
374d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
375ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
376d7647d96SDag-Erling Smørgrav #else
377d7647d96SDag-Erling Smørgrav 	void *rule = NULL;
378d7647d96SDag-Erling Smørgrav #endif
379affc907dSMax Laier 	int error;
3802b25acc1SLuigi Rizzo 
381c1d93b05SSam Leffler 	if (rule == NULL && BDG_ACTIVE(ifp)) {
382c1d93b05SSam Leffler 		/*
383c1d93b05SSam Leffler 		 * Beware, the bridge code notices the null rcvif and
384c1d93b05SSam Leffler 		 * uses that identify that it's being called from
385c1d93b05SSam Leffler 		 * ether_output as opposd to ether_input.  Yech.
386c1d93b05SSam Leffler 		 */
387fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
388c1d93b05SSam Leffler 		m = bdg_forward_ptr(m, ifp);
3892e2de7f2SArchie Cobbs 		if (m != NULL)
3902e2de7f2SArchie Cobbs 			m_freem(m);
3911db59ce6SEivind Eklund 		return (0);
392fb5fbe46SLuigi Rizzo 	}
393d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
3944b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
395c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
3964b984093SLuigi Rizzo 			if (m) {
3974b984093SLuigi Rizzo 				m_freem(m);
398e7909401SDavid Malone 				return EACCES;	/* pkt dropped */
3994b984093SLuigi Rizzo 			} else
4004b984093SLuigi Rizzo 				return 0;	/* consumed e.g. in a pipe */
4014b984093SLuigi Rizzo 		}
4024b984093SLuigi Rizzo 	}
403d7647d96SDag-Erling Smørgrav #endif
4042b25acc1SLuigi Rizzo 
405df8bae1dSRodney W. Grimes 	/*
406df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
407df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
408df8bae1dSRodney W. Grimes 	 */
409affc907dSMax Laier 	IFQ_HANDOFF(ifp, m, error);
410affc907dSMax Laier 	return (error);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
413d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
414df8bae1dSRodney W. Grimes /*
4154b984093SLuigi Rizzo  * ipfw processing for ethernet packets (in and out).
4164b984093SLuigi Rizzo  * The second parameter is NULL from ether_demux, and ifp from
4174b984093SLuigi Rizzo  * ether_output_frame. This section of code could be used from
4182f8ebbf4SLuigi Rizzo  * bridge.c as well as long as we use some extra info
4194b984093SLuigi Rizzo  * to distinguish that case from ether_output_frame();
4204b984093SLuigi Rizzo  */
4214b984093SLuigi Rizzo int
4224b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
423c1d93b05SSam Leffler 	struct ip_fw **rule, int shared)
4244b984093SLuigi Rizzo {
425c1d93b05SSam Leffler 	struct ether_header *eh;
426c1d93b05SSam Leffler 	struct ether_header save_eh;
427c1d93b05SSam Leffler 	struct mbuf *m;
4284b984093SLuigi Rizzo 	int i;
4292b25acc1SLuigi Rizzo 	struct ip_fw_args args;
4304b984093SLuigi Rizzo 
43197850a5dSLuigi Rizzo 	if (*rule != NULL && fw_one_pass)
432015d72e0SLuigi Rizzo 		return 1; /* dummynet packet, already partially processed */
4332f8ebbf4SLuigi Rizzo 
4344b984093SLuigi Rizzo 	/*
4352f8ebbf4SLuigi Rizzo 	 * I need some amt of data to be contiguous, and in case others need
4364b984093SLuigi Rizzo 	 * the packet (shared==1) also better be in the first mbuf.
4374b984093SLuigi Rizzo 	 */
438c1d93b05SSam Leffler 	m = *m0;
439c1d93b05SSam Leffler 	i = min( m->m_pkthdr.len, max_protohdr);
440c1d93b05SSam Leffler 	if ( shared || m->m_len < i) {
441c1d93b05SSam Leffler 		m = m_pullup(m, i);
442c1d93b05SSam Leffler 		if (m == NULL) {
443c1d93b05SSam Leffler 			*m0 = m;
4444b984093SLuigi Rizzo 			return 0;
4454b984093SLuigi Rizzo 		}
446c1d93b05SSam Leffler 	}
447c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
448c1d93b05SSam Leffler 	save_eh = *eh;			/* save copy for restore below */
449c1d93b05SSam Leffler 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
4504b984093SLuigi Rizzo 
451c1d93b05SSam Leffler 	args.m = m;		/* the packet we are looking at		*/
4522b25acc1SLuigi Rizzo 	args.oif = dst;		/* destination, if any			*/
4532b25acc1SLuigi Rizzo 	args.rule = *rule;	/* matching rule to restart		*/
4542b25acc1SLuigi Rizzo 	args.next_hop = NULL;	/* we do not support forward yet	*/
4552b25acc1SLuigi Rizzo 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
4562b25acc1SLuigi Rizzo 	i = ip_fw_chk_ptr(&args);
457c1d93b05SSam Leffler 	m = args.m;
458c1d93b05SSam Leffler 	if (m != NULL) {
459c1d93b05SSam Leffler 		/*
460c1d93b05SSam Leffler 		 * Restore Ethernet header, as needed, in case the
461c1d93b05SSam Leffler 		 * mbuf chain was replaced by ipfw.
462c1d93b05SSam Leffler 		 */
463a163d034SWarner Losh 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
464c1d93b05SSam Leffler 		if (m == NULL) {
465c1d93b05SSam Leffler 			*m0 = m;
466c1d93b05SSam Leffler 			return 0;
467c1d93b05SSam Leffler 		}
468c1d93b05SSam Leffler 		if (eh != mtod(m, struct ether_header *))
469c1d93b05SSam Leffler 			bcopy(&save_eh, mtod(m, struct ether_header *),
470c1d93b05SSam Leffler 				ETHER_HDR_LEN);
471c1d93b05SSam Leffler 	}
472c1d93b05SSam Leffler 	*m0 = m;
4732b25acc1SLuigi Rizzo 	*rule = args.rule;
4742b25acc1SLuigi Rizzo 
4756c69a7c3SGleb Smirnoff 	if (i == IP_FW_DENY) /* drop */
4764b984093SLuigi Rizzo 		return 0;
4774b984093SLuigi Rizzo 
4786c69a7c3SGleb Smirnoff 	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
4796c69a7c3SGleb Smirnoff 
4806c69a7c3SGleb Smirnoff 	if (i == IP_FW_PASS) /* a PASS rule.  */
4814b984093SLuigi Rizzo 		return 1;
4824b984093SLuigi Rizzo 
4836c69a7c3SGleb Smirnoff 	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
4844b984093SLuigi Rizzo 		/*
4854b984093SLuigi Rizzo 		 * Pass the pkt to dummynet, which consumes it.
4864b984093SLuigi Rizzo 		 * If shared, make a copy and keep the original.
4874b984093SLuigi Rizzo 		 */
4884b984093SLuigi Rizzo 		if (shared) {
489a163d034SWarner Losh 			m = m_copypacket(m, M_DONTWAIT);
4902b25acc1SLuigi Rizzo 			if (m == NULL)
4914b984093SLuigi Rizzo 				return 0;
4924b984093SLuigi Rizzo 		} else {
4932b25acc1SLuigi Rizzo 			/*
494c1d93b05SSam Leffler 			 * Pass the original to dummynet and
495c1d93b05SSam Leffler 			 * nothing back to the caller
4962b25acc1SLuigi Rizzo 			 */
497c1d93b05SSam Leffler 			*m0 = NULL ;
4984b984093SLuigi Rizzo 		}
49928935658SGleb Smirnoff 		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
5004b984093SLuigi Rizzo 		return 0;
5014b984093SLuigi Rizzo 	}
5024b984093SLuigi Rizzo 	/*
5032f8ebbf4SLuigi Rizzo 	 * XXX at some point add support for divert/forward actions.
5042f8ebbf4SLuigi Rizzo 	 * If none of the above matches, we have to drop the pkt.
5054b984093SLuigi Rizzo 	 */
5064b984093SLuigi Rizzo 	return 0;
5074b984093SLuigi Rizzo }
508d7647d96SDag-Erling Smørgrav #endif
5094b984093SLuigi Rizzo 
5104b984093SLuigi Rizzo /*
511c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
512c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
513df8bae1dSRodney W. Grimes  */
514c1d93b05SSam Leffler static void
515c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m)
516df8bae1dSRodney W. Grimes {
517c1d93b05SSam Leffler 	struct ether_header *eh;
518c1d93b05SSam Leffler 	u_short etype;
519df8bae1dSRodney W. Grimes 
520c1d93b05SSam Leffler 	/*
521c1d93b05SSam Leffler 	 * Do consistency checks to verify assumptions
522c1d93b05SSam Leffler 	 * made by code past this point.
523c1d93b05SSam Leffler 	 */
524c1d93b05SSam Leffler 	if ((m->m_flags & M_PKTHDR) == 0) {
525c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o packet header\n");
526c1d93b05SSam Leffler 		ifp->if_ierrors++;
527c939f1aeSLuigi Rizzo 		m_freem(m);
528c939f1aeSLuigi Rizzo 		return;
529c939f1aeSLuigi Rizzo 	}
530797f247bSMatthew N. Dodd 	if (m->m_len < ETHER_HDR_LEN) {
531c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
532c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
533c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
534c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
535c1d93b05SSam Leffler 		ifp->if_ierrors++;
536c1d93b05SSam Leffler 		m_freem(m);
537c1d93b05SSam Leffler 		return;
5382e2de7f2SArchie Cobbs 	}
539c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
540c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
541c1d93b05SSam Leffler 	if (m->m_pkthdr.len >
542c1d93b05SSam Leffler 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
543c1d93b05SSam Leffler 		if_printf(ifp, "discard oversize frame "
544c1d93b05SSam Leffler 				"(ether type %x flags %x len %u > max %lu)\n",
545c1d93b05SSam Leffler 				etype, m->m_flags, m->m_pkthdr.len,
546c1d93b05SSam Leffler 				ETHER_MAX_FRAME(ifp, etype,
547c1d93b05SSam Leffler 						m->m_flags & M_HASFCS));
548c1d93b05SSam Leffler 		ifp->if_ierrors++;
549c1d93b05SSam Leffler 		m_freem(m);
550c1d93b05SSam Leffler 		return;
551c1d93b05SSam Leffler 	}
552c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif == NULL) {
553c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o interface pointer\n");
554c1d93b05SSam Leffler 		ifp->if_ierrors++;
555c1d93b05SSam Leffler 		m_freem(m);
556c1d93b05SSam Leffler 		return;
557c1d93b05SSam Leffler 	}
558c1d93b05SSam Leffler #ifdef DIAGNOSTIC
559c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif != ifp) {
5609bf40edeSBrooks Davis 		if_printf(ifp, "Warning, frame marked as received on %s\n",
5619bf40edeSBrooks Davis 			m->m_pkthdr.rcvif->if_xname);
562c1d93b05SSam Leffler 	}
563c1d93b05SSam Leffler #endif
564c1d93b05SSam Leffler 
565533d8562SRobert Watson #ifdef MAC
566533d8562SRobert Watson 	/*
567533d8562SRobert Watson 	 * Tag the mbuf with an appropriate MAC label before any other
568533d8562SRobert Watson 	 * consumers can get to it.
569533d8562SRobert Watson 	 */
570533d8562SRobert Watson 	mac_create_mbuf_from_ifnet(ifp, m);
571533d8562SRobert Watson #endif
572533d8562SRobert Watson 
573c1d93b05SSam Leffler 	/*
574c1d93b05SSam Leffler 	 * Give bpf a chance at the packet.
575c1d93b05SSam Leffler 	 */
576c1d93b05SSam Leffler 	BPF_MTAP(ifp, m);
5772e2de7f2SArchie Cobbs 
578afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR) {
579c1d93b05SSam Leffler 		/*
580c1d93b05SSam Leffler 		 * Interface marked for monitoring; discard packet.
581c1d93b05SSam Leffler 		 */
582afbe3a0fSPoul-Henning Kamp 		m_freem(m);
583afbe3a0fSPoul-Henning Kamp 		return;
584afbe3a0fSPoul-Henning Kamp 	}
585afbe3a0fSPoul-Henning Kamp 
586c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
587c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
588c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
589c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
590c1d93b05SSam Leffler 	}
591c1d93b05SSam Leffler 
592c1d93b05SSam Leffler 	ifp->if_ibytes += m->m_pkthdr.len;
59305463bb5SDavid Greenman 
594e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
595eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
596514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_p != NULL,
597514bcb89SPoul-Henning Kamp 		    ("ng_ether_input_p is NULL"));
598c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
599e1e1452dSArchie Cobbs 		if (m == NULL)
600e1e1452dSArchie Cobbs 			return;
601e1e1452dSArchie Cobbs 	}
602e1e1452dSArchie Cobbs 
6038f867517SAndrew Thompson 	/*
6048f867517SAndrew Thompson 	 * Tap the packet off here for a bridge.  bridge_input()
6058f867517SAndrew Thompson 	 * will return NULL if it has consumed the packet, otherwise
6068f867517SAndrew Thompson 	 * it gets processed as normal.  Note that bridge_input()
6078f867517SAndrew Thompson 	 * will always return the original packet if we need to
6088f867517SAndrew Thompson 	 * process it locally.
6098f867517SAndrew Thompson 	 */
6108f867517SAndrew Thompson 	if (ifp->if_bridge) {
6118f867517SAndrew Thompson 		KASSERT(bridge_input_p != NULL,
6128f867517SAndrew Thompson 		    ("ether_input: if_bridge not loaded!"));
6138f867517SAndrew Thompson 
6148f867517SAndrew Thompson 		/* Mark the packet as broadcast or multicast. This is also set
6158f867517SAndrew Thompson 		 * further down the code in ether_demux() but since the bridge
6168f867517SAndrew Thompson 		 * input routine rarely returns a mbuf for further processing,
6178f867517SAndrew Thompson 		 * it is an acceptable duplication.
6188f867517SAndrew Thompson 		 */
6198f867517SAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
6208f867517SAndrew Thompson 			if (bcmp(etherbroadcastaddr, eh->ether_dhost,
6218f867517SAndrew Thompson 				sizeof(etherbroadcastaddr)) == 0)
6228f867517SAndrew Thompson 				m->m_flags |= M_BCAST;
6238f867517SAndrew Thompson 			else
6248f867517SAndrew Thompson 				m->m_flags |= M_MCAST;
6258f867517SAndrew Thompson 		}
6268f867517SAndrew Thompson 
6278f867517SAndrew Thompson 		m = (*bridge_input_p)(ifp, m);
6288f867517SAndrew Thompson 		if (m == NULL)
6298f867517SAndrew Thompson 			return;
6308f867517SAndrew Thompson 		/*
6318f867517SAndrew Thompson 		 * Bridge has determined that the packet is for us.
6328f867517SAndrew Thompson 		 * Update our interface pointer -- we may have had
6338f867517SAndrew Thompson 		 * to "bridge" the packet locally.
6348f867517SAndrew Thompson 		 */
6358f867517SAndrew Thompson 		ifp = m->m_pkthdr.rcvif;
6368f867517SAndrew Thompson 	}
6378f867517SAndrew Thompson 
6382e2de7f2SArchie Cobbs 	/* Check for bridging mode */
639a176c2aeSGleb Smirnoff 	if (BDG_ACTIVE(ifp) )
640a176c2aeSGleb Smirnoff 		if ((m = bridge_in_ptr(ifp, m)) == NULL)
6412e2de7f2SArchie Cobbs 			return;
6422e2de7f2SArchie Cobbs 
643a6ddbff0SPoul-Henning Kamp 	/* First chunk of an mbuf contains good entropy */
64410b1fde0SMark Murray 	if (harvest.ethernet)
64510b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
646acf032f5SRobert Watson 	ether_demux(ifp, m);
647e1e1452dSArchie Cobbs }
648e1e1452dSArchie Cobbs 
649e1e1452dSArchie Cobbs /*
650e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
651e1e1452dSArchie Cobbs  */
652e1e1452dSArchie Cobbs void
653c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
654e1e1452dSArchie Cobbs {
655c1d93b05SSam Leffler 	struct ether_header *eh;
6561cafed39SJonathan Lemon 	int isr;
657e1e1452dSArchie Cobbs 	u_short ether_type;
658e1e1452dSArchie Cobbs #if defined(NETATALK)
659098a8c3bSMatthew N. Dodd 	struct llc *l;
660e1e1452dSArchie Cobbs #endif
661d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
662ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
663d7647d96SDag-Erling Smørgrav #endif
66436e8826fSMax Laier 
665c1d93b05SSam Leffler 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
666c1d93b05SSam Leffler 
667c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
668cd0cd014SJoerg Wunsch 	ether_type = ntohs(eh->ether_type);
669c1d93b05SSam Leffler 
670d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
6712b25acc1SLuigi Rizzo 	if (rule)	/* packet was already bridged */
6722b25acc1SLuigi Rizzo 		goto post_stats;
673d7647d96SDag-Erling Smørgrav #endif
6744b984093SLuigi Rizzo 
675cd0cd014SJoerg Wunsch 	if (!(BDG_ACTIVE(ifp)) &&
6766ee20ab5SRuslan Ermilov 	    !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
6776ee20ab5SRuslan Ermilov 	    ifp->if_nvlans > 0)) {
678a9771948SGleb Smirnoff #ifdef DEV_CARP
679a9771948SGleb Smirnoff 		/*
68058996b13SGleb Smirnoff 		 * XXX: Okay, we need to call carp_forus() and - if it is for
68158996b13SGleb Smirnoff 		 * us jump over code that does the normal check
682fc74a9f9SBrooks Davis 		 * "IFP2ENADDR(ifp) == ether_dhost". The check sequence is a bit
68358996b13SGleb Smirnoff 		 * different from OpenBSD, so we jump over as few code as
68458996b13SGleb Smirnoff 		 * possible, to catch _all_ sanity checks. This needs
68558996b13SGleb Smirnoff 		 * evaluation, to see if the carp ether_dhost values break any
68658996b13SGleb Smirnoff 		 * of these checks!
687a9771948SGleb Smirnoff 		 */
688a9771948SGleb Smirnoff 		if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
689a9771948SGleb Smirnoff 			goto pre_stats;
690a9771948SGleb Smirnoff #endif
691c6943100SPoul-Henning Kamp 		/*
692c6943100SPoul-Henning Kamp 		 * Discard packet if upper layers shouldn't see it because it
693c6943100SPoul-Henning Kamp 		 * was unicast to a different Ethernet address. If the driver
694c6943100SPoul-Henning Kamp 		 * is working properly, then this situation can only happen
695c6943100SPoul-Henning Kamp 		 * when the interface is in promiscuous mode.
696cd0cd014SJoerg Wunsch 		 *
697cd0cd014SJoerg Wunsch 		 * If VLANs are active, and this packet has a VLAN tag, do
698cd0cd014SJoerg Wunsch 		 * not drop it here but pass it on to the VLAN layer, to
699cd0cd014SJoerg Wunsch 		 * give them a chance to consider it as well (e. g. in case
700cd0cd014SJoerg Wunsch 		 * bridging is only active on a VLAN).  They will drop it if
701cd0cd014SJoerg Wunsch 		 * it's undesired.
702c6943100SPoul-Henning Kamp 		 */
703cb24f323SArchie Cobbs 		if ((ifp->if_flags & IFF_PROMISC) != 0
7046ee20ab5SRuslan Ermilov 		    && !ETHER_IS_MULTICAST(eh->ether_dhost)
705cb24f323SArchie Cobbs 		    && bcmp(eh->ether_dhost,
706fc74a9f9SBrooks Davis 		      IFP2ENADDR(ifp), ETHER_ADDR_LEN) != 0
70725faf49dSMaxim Sobolev 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
708cb24f323SArchie Cobbs 			    m_freem(m);
709cb24f323SArchie Cobbs 			    return;
710cb24f323SArchie Cobbs 		}
711c6943100SPoul-Henning Kamp 	}
712cb24f323SArchie Cobbs 
713a9771948SGleb Smirnoff #ifdef DEV_CARP
714a9771948SGleb Smirnoff pre_stats:
715a9771948SGleb Smirnoff #endif
7162e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
717df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
718df8bae1dSRodney W. Grimes 		m_freem(m);
719df8bae1dSRodney W. Grimes 		return;
720df8bae1dSRodney W. Grimes 	}
721588523faSSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
72264760eb0SWarner Losh 		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
723df8bae1dSRodney W. Grimes 		    sizeof(etherbroadcastaddr)) == 0)
724df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
7252cc2df49SGarrett Wollman 		else
726df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
7272cc2df49SGarrett Wollman 	}
728df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
729df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
730df8bae1dSRodney W. Grimes 
731d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
7324b984093SLuigi Rizzo post_stats:
7334b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
734c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
7354b984093SLuigi Rizzo 			if (m)
7364b984093SLuigi Rizzo 				m_freem(m);
7374b984093SLuigi Rizzo 			return;
7384b984093SLuigi Rizzo 		}
7394b984093SLuigi Rizzo 	}
740d7647d96SDag-Erling Smørgrav #endif
7414b984093SLuigi Rizzo 
742c1d93b05SSam Leffler 	/*
7436c23e6ccSRuslan Ermilov 	 * Check to see if the device performed the VLAN decapsulation and
744c1d93b05SSam Leffler 	 * provided us with the tag.
745c1d93b05SSam Leffler 	 */
7466ee20ab5SRuslan Ermilov 	if (m->m_flags & M_VLANTAG) {
747c1d93b05SSam Leffler 		/*
7486c23e6ccSRuslan Ermilov 		 * If no VLANs are configured, drop.
7496c23e6ccSRuslan Ermilov 		 */
7506c23e6ccSRuslan Ermilov 		if (ifp->if_nvlans == 0) {
7516c23e6ccSRuslan Ermilov 			ifp->if_noproto++;
7526c23e6ccSRuslan Ermilov 			m_freem(m);
7536c23e6ccSRuslan Ermilov 			return;
7546c23e6ccSRuslan Ermilov 		}
7556c23e6ccSRuslan Ermilov 		/*
756c1d93b05SSam Leffler 		 * vlan_input() will either recursively call ether_input()
757c1d93b05SSam Leffler 		 * or drop the packet.
758c1d93b05SSam Leffler 		 */
759c1d93b05SSam Leffler 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
760c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
761c1d93b05SSam Leffler 		return;
762c1d93b05SSam Leffler 	}
763c1d93b05SSam Leffler 
764c1d93b05SSam Leffler 	/*
765c1d93b05SSam Leffler 	 * Handle protocols that expect to have the Ethernet header
766c1d93b05SSam Leffler 	 * (and possibly FCS) intact.
767c1d93b05SSam Leffler 	 */
768c1d93b05SSam Leffler 	switch (ether_type) {
769c1d93b05SSam Leffler 	case ETHERTYPE_VLAN:
770c1d93b05SSam Leffler 		if (ifp->if_nvlans != 0) {
771c1d93b05SSam Leffler 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
772c1d93b05SSam Leffler 			(*vlan_input_p)(ifp, m);
773c1d93b05SSam Leffler 		} else {
774c1d93b05SSam Leffler 			ifp->if_noproto++;
775c1d93b05SSam Leffler 			m_freem(m);
776c1d93b05SSam Leffler 		}
777c1d93b05SSam Leffler 		return;
778c1d93b05SSam Leffler 	}
779c1d93b05SSam Leffler 
780c1d93b05SSam Leffler 	/* Strip off Ethernet header. */
781797f247bSMatthew N. Dodd 	m_adj(m, ETHER_HDR_LEN);
782c1d93b05SSam Leffler 
783c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
784c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
785c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
786c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
787c1d93b05SSam Leffler 	}
788c1d93b05SSam Leffler 
789307d80beSDavid Greenman 	switch (ether_type) {
790df8bae1dSRodney W. Grimes #ifdef INET
791df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
7929188b4a1SAndre Oppermann 		if (ip_fastforward(m))
7931f91d8c5SDavid Greenman 			return;
7941cafed39SJonathan Lemon 		isr = NETISR_IP;
795df8bae1dSRodney W. Grimes 		break;
796df8bae1dSRodney W. Grimes 
797df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
79808aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
79908aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
80008aadfbbSJonathan Lemon 			m_freem(m);
80108aadfbbSJonathan Lemon 			return;
80208aadfbbSJonathan Lemon 		}
8031cafed39SJonathan Lemon 		isr = NETISR_ARP;
804df8bae1dSRodney W. Grimes 		break;
805df8bae1dSRodney W. Grimes #endif
806cc6a66f2SJulian Elischer #ifdef IPX
807cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
8084f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8094f93599fSBoris Popov 			return;
8101cafed39SJonathan Lemon 		isr = NETISR_IPX;
811cc6a66f2SJulian Elischer 		break;
812cc6a66f2SJulian Elischer #endif
81382cd038dSYoshinobu Inoue #ifdef INET6
81482cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
8151cafed39SJonathan Lemon 		isr = NETISR_IPV6;
81682cd038dSYoshinobu Inoue 		break;
81782cd038dSYoshinobu Inoue #endif
818655929bfSJulian Elischer #ifdef NETATALK
819655929bfSJulian Elischer 	case ETHERTYPE_AT:
8201cafed39SJonathan Lemon 		isr = NETISR_ATALK1;
821655929bfSJulian Elischer 		break;
822655929bfSJulian Elischer 	case ETHERTYPE_AARP:
8231cafed39SJonathan Lemon 		isr = NETISR_AARP;
8241cafed39SJonathan Lemon 		break;
8258cdfefbdSPeter Wemm #endif /* NETATALK */
826df8bae1dSRodney W. Grimes 	default:
8274f93599fSBoris Popov #ifdef IPX
8284f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8294f93599fSBoris Popov 			return;
8304f93599fSBoris Popov #endif /* IPX */
831242c5536SPeter Wemm #if defined(NETATALK)
832307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
833c1d93b05SSam Leffler 			goto discard;
834df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
8351cafed39SJonathan Lemon 		if (l->llc_dsap == LLC_SNAP_LSAP &&
8361cafed39SJonathan Lemon 		    l->llc_ssap == LLC_SNAP_LSAP &&
8371cafed39SJonathan Lemon 		    l->llc_control == LLC_UI) {
8384158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
839655929bfSJulian Elischer 			    sizeof(at_org_code)) == 0 &&
840655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
841797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8421cafed39SJonathan Lemon 				isr = NETISR_ATALK2;
843655929bfSJulian Elischer 				break;
844655929bfSJulian Elischer 			}
8454158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
846655929bfSJulian Elischer 			    sizeof(aarp_org_code)) == 0 &&
847655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
848797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8491cafed39SJonathan Lemon 				isr = NETISR_AARP;
850655929bfSJulian Elischer 				break;
851df8bae1dSRodney W. Grimes 			}
8521cafed39SJonathan Lemon 		}
853242c5536SPeter Wemm #endif /* NETATALK */
8541cafed39SJonathan Lemon 		goto discard;
855df8bae1dSRodney W. Grimes 	}
8561cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
857c1d93b05SSam Leffler 	return;
8581cafed39SJonathan Lemon 
859c1d93b05SSam Leffler discard:
860c1d93b05SSam Leffler 	/*
861c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
862c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
863c1d93b05SSam Leffler 	 * otherwise dispose of it.
864c1d93b05SSam Leffler 	 */
865eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
866514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_orphan_p != NULL,
867514bcb89SPoul-Henning Kamp 		    ("ng_ether_input_orphan_p is NULL"));
868c1d93b05SSam Leffler 		/*
869c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
870c1d93b05SSam Leffler 		 * consistent view of inbound packets.
871c1d93b05SSam Leffler 		 */
872797f247bSMatthew N. Dodd 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
873c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
874c1d93b05SSam Leffler 		return;
875c1d93b05SSam Leffler 	}
876c1d93b05SSam Leffler 	m_freem(m);
877c1d93b05SSam Leffler }
878c1d93b05SSam Leffler 
879c1d93b05SSam Leffler /*
880c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
881c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
882c1d93b05SSam Leffler  *
883c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
884c1d93b05SSam Leffler  *
885c1d93b05SSam Leffler  * since there's no static buffer involved.
886c1d93b05SSam Leffler  */
887c1d93b05SSam Leffler char *
888c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
889c1d93b05SSam Leffler {
890c1d93b05SSam Leffler 	static char etherbuf[18];
891c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
892c1d93b05SSam Leffler 	return (etherbuf);
893df8bae1dSRodney W. Grimes }
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes /*
896df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
897df8bae1dSRodney W. Grimes  */
898df8bae1dSRodney W. Grimes void
899c1d93b05SSam Leffler ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
900df8bae1dSRodney W. Grimes {
901f93dfa28SBrooks Davis 	int i;
902098a8c3bSMatthew N. Dodd 	struct ifaddr *ifa;
903098a8c3bSMatthew N. Dodd 	struct sockaddr_dl *sdl;
904df8bae1dSRodney W. Grimes 
905c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
906c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
907cfeff1b6SJonathan Lemon 	if_attach(ifp);
908df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
909c1d93b05SSam Leffler 	ifp->if_output = ether_output;
910c1d93b05SSam Leffler 	ifp->if_input = ether_input;
9111158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
912a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
913c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
914322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
915c1d93b05SSam Leffler 
916f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
9176e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
91859562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
919df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
920df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
921c1d93b05SSam Leffler 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
922c1d93b05SSam Leffler 	/*
923c1d93b05SSam Leffler 	 * XXX: This doesn't belong here; we do it until
924c1d93b05SSam Leffler 	 * XXX:  all drivers are cleaned up
925c1d93b05SSam Leffler 	 */
926fc74a9f9SBrooks Davis 	if (llc != IFP2ENADDR(ifp))
927fc74a9f9SBrooks Davis 		bcopy(llc, IFP2ENADDR(ifp), ifp->if_addrlen);
928c1d93b05SSam Leffler 
929797f247bSMatthew N. Dodd 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
930e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
931e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
9327b109fa4SLuigi Rizzo 	if (BDG_LOADED)
933db69a05dSPaul Saab 		bdgtakeifaces_ptr();
934e3bbbec2SMatthew N. Dodd 
935f93dfa28SBrooks Davis 	/* Announce Ethernet MAC address if non-zero. */
936f93dfa28SBrooks Davis 	for (i = 0; i < ifp->if_addrlen; i++)
937f93dfa28SBrooks Davis 		if (llc[i] != 0)
938f93dfa28SBrooks Davis 			break;
939f93dfa28SBrooks Davis 	if (i != ifp->if_addrlen)
940e3bbbec2SMatthew N. Dodd 		if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
941af5e59bfSRobert Watson 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
942af5e59bfSRobert Watson 		if_printf(ifp, "if_start running deferred for Giant\n");
943e1e1452dSArchie Cobbs }
944e1e1452dSArchie Cobbs 
94521b8ebd9SArchie Cobbs /*
94621b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
94721b8ebd9SArchie Cobbs  */
94821b8ebd9SArchie Cobbs void
949c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
95021b8ebd9SArchie Cobbs {
951514bcb89SPoul-Henning Kamp 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
952514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_detach_p != NULL,
953514bcb89SPoul-Henning Kamp 		    ("ng_ether_detach_p is NULL"));
95421b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
955514bcb89SPoul-Henning Kamp 	}
95621b8ebd9SArchie Cobbs 	bpfdetach(ifp);
95721b8ebd9SArchie Cobbs 	if_detach(ifp);
9587b109fa4SLuigi Rizzo 	if (BDG_LOADED)
959db69a05dSPaul Saab 		bdgtakeifaces_ptr();
96021b8ebd9SArchie Cobbs }
96121b8ebd9SArchie Cobbs 
962ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
963602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
964d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
9654b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
9664b984093SLuigi Rizzo 	    &ether_ipfw,0,"Pass ether pkts through firewall");
967d7647d96SDag-Erling Smørgrav #endif
96830106f6aSPoul-Henning Kamp 
96916b4a343SChristian Weisgerber #if 0
97016b4a343SChristian Weisgerber /*
97116b4a343SChristian Weisgerber  * This is for reference.  We have a table-driven version
97216b4a343SChristian Weisgerber  * of the little-endian crc32 generator, which is faster
97316b4a343SChristian Weisgerber  * than the double-loop.
97416b4a343SChristian Weisgerber  */
97516b4a343SChristian Weisgerber uint32_t
97616b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
97716b4a343SChristian Weisgerber {
97816b4a343SChristian Weisgerber 	size_t i;
97916b4a343SChristian Weisgerber 	uint32_t crc;
98016b4a343SChristian Weisgerber 	int bit;
98116b4a343SChristian Weisgerber 	uint8_t data;
98216b4a343SChristian Weisgerber 
98316b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
98416b4a343SChristian Weisgerber 
98516b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
98616b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
98716b4a343SChristian Weisgerber 			carry = (crc ^ data) & 1;
98816b4a343SChristian Weisgerber 			crc >>= 1;
98916b4a343SChristian Weisgerber 			if (carry)
99016b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_LE);
99116b4a343SChristian Weisgerber 	}
99216b4a343SChristian Weisgerber 
99316b4a343SChristian Weisgerber 	return (crc);
99416b4a343SChristian Weisgerber }
99516b4a343SChristian Weisgerber #else
99616b4a343SChristian Weisgerber uint32_t
99716b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
99816b4a343SChristian Weisgerber {
99916b4a343SChristian Weisgerber 	static const uint32_t crctab[] = {
100016b4a343SChristian Weisgerber 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
100116b4a343SChristian Weisgerber 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
100216b4a343SChristian Weisgerber 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
100316b4a343SChristian Weisgerber 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
100416b4a343SChristian Weisgerber 	};
100516b4a343SChristian Weisgerber 	size_t i;
100616b4a343SChristian Weisgerber 	uint32_t crc;
100716b4a343SChristian Weisgerber 
100816b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
100916b4a343SChristian Weisgerber 
101016b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
101116b4a343SChristian Weisgerber 		crc ^= buf[i];
101216b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
101316b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
101416b4a343SChristian Weisgerber 	}
101516b4a343SChristian Weisgerber 
101616b4a343SChristian Weisgerber 	return (crc);
101716b4a343SChristian Weisgerber }
101816b4a343SChristian Weisgerber #endif
101916b4a343SChristian Weisgerber 
102016b4a343SChristian Weisgerber uint32_t
102116b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len)
102216b4a343SChristian Weisgerber {
102316b4a343SChristian Weisgerber 	size_t i;
102416b4a343SChristian Weisgerber 	uint32_t crc, carry;
102516b4a343SChristian Weisgerber 	int bit;
102616b4a343SChristian Weisgerber 	uint8_t data;
102716b4a343SChristian Weisgerber 
102816b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
102916b4a343SChristian Weisgerber 
103016b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
103116b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
103216b4a343SChristian Weisgerber 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
103316b4a343SChristian Weisgerber 			crc <<= 1;
103416b4a343SChristian Weisgerber 			if (carry)
103516b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
103616b4a343SChristian Weisgerber 		}
103716b4a343SChristian Weisgerber 	}
103816b4a343SChristian Weisgerber 
103916b4a343SChristian Weisgerber 	return (crc);
104016b4a343SChristian Weisgerber }
104116b4a343SChristian Weisgerber 
1042fb583156SDavid Greenman int
104372fd1b6aSDag-Erling Smørgrav ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
104430106f6aSPoul-Henning Kamp {
104530106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
104630106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
1047fb583156SDavid Greenman 	int error = 0;
104830106f6aSPoul-Henning Kamp 
104930106f6aSPoul-Henning Kamp 	switch (command) {
105030106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
105130106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
105230106f6aSPoul-Henning Kamp 
105330106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
105430106f6aSPoul-Henning Kamp #ifdef INET
105530106f6aSPoul-Henning Kamp 		case AF_INET:
105630106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1057322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
105830106f6aSPoul-Henning Kamp 			break;
105930106f6aSPoul-Henning Kamp #endif
106030106f6aSPoul-Henning Kamp #ifdef IPX
106130106f6aSPoul-Henning Kamp 		/*
106230106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
106330106f6aSPoul-Henning Kamp 		 */
106430106f6aSPoul-Henning Kamp 		case AF_IPX:
106530106f6aSPoul-Henning Kamp 			{
1066098a8c3bSMatthew N. Dodd 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
106730106f6aSPoul-Henning Kamp 
106830106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
106930106f6aSPoul-Henning Kamp 				ina->x_host =
107086101139SPoul-Henning Kamp 				    *(union ipx_host *)
1071fc74a9f9SBrooks Davis 				    IFP2ENADDR(ifp);
107230106f6aSPoul-Henning Kamp 			else {
107330106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
1074fc74a9f9SBrooks Davis 				      (caddr_t) IFP2ENADDR(ifp),
1075fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
107630106f6aSPoul-Henning Kamp 			}
107730106f6aSPoul-Henning Kamp 
107830106f6aSPoul-Henning Kamp 			/*
107930106f6aSPoul-Henning Kamp 			 * Set new address
108030106f6aSPoul-Henning Kamp 			 */
108130106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
108230106f6aSPoul-Henning Kamp 			break;
108330106f6aSPoul-Henning Kamp 			}
108430106f6aSPoul-Henning Kamp #endif
108530106f6aSPoul-Henning Kamp 		default:
108630106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
108730106f6aSPoul-Henning Kamp 			break;
108830106f6aSPoul-Henning Kamp 		}
108930106f6aSPoul-Henning Kamp 		break;
109030106f6aSPoul-Henning Kamp 
109130106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
109230106f6aSPoul-Henning Kamp 		{
109330106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
109430106f6aSPoul-Henning Kamp 
109530106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
1096fc74a9f9SBrooks Davis 			bcopy(IFP2ENADDR(ifp),
109730106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
109830106f6aSPoul-Henning Kamp 		}
109930106f6aSPoul-Henning Kamp 		break;
1100fb583156SDavid Greenman 
1101fb583156SDavid Greenman 	case SIOCSIFMTU:
1102fb583156SDavid Greenman 		/*
1103fb583156SDavid Greenman 		 * Set the interface MTU.
1104fb583156SDavid Greenman 		 */
1105fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1106fb583156SDavid Greenman 			error = EINVAL;
1107fb583156SDavid Greenman 		} else {
1108fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
110930106f6aSPoul-Henning Kamp 		}
1110fb583156SDavid Greenman 		break;
1111c1d93b05SSam Leffler 	default:
1112c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1113c1d93b05SSam Leffler 		break;
1114fb583156SDavid Greenman 	}
1115fb583156SDavid Greenman 	return (error);
111630106f6aSPoul-Henning Kamp }
11171158dfb7SGarrett Wollman 
111837c84183SPoul-Henning Kamp static int
111972fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
112072fd1b6aSDag-Erling Smørgrav 	struct sockaddr *sa)
11211158dfb7SGarrett Wollman {
11221158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
1123d7647d96SDag-Erling Smørgrav #ifdef INET
11241158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
1125d7647d96SDag-Erling Smørgrav #endif
112682cd038dSYoshinobu Inoue #ifdef INET6
112782cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
112882cd038dSYoshinobu Inoue #endif
11291158dfb7SGarrett Wollman 	u_char *e_addr;
11301158dfb7SGarrett Wollman 
11311158dfb7SGarrett Wollman 	switch(sa->sa_family) {
11321158dfb7SGarrett Wollman 	case AF_LINK:
11337f33a738SJulian Elischer 		/*
11347f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
11357f33a738SJulian Elischer 		 */
11361158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
11371158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
1138086e98c4SBruce M Simpson 		if (!ETHER_IS_MULTICAST(e_addr))
11391158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11401158dfb7SGarrett Wollman 		*llsa = 0;
11411158dfb7SGarrett Wollman 		return 0;
11421158dfb7SGarrett Wollman 
11431158dfb7SGarrett Wollman #ifdef INET
11441158dfb7SGarrett Wollman 	case AF_INET:
11451158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
11461158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
11471158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11481158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
114909df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
115009df718eSRobert Watson 		if (sdl == NULL)
115109df718eSRobert Watson 			return ENOMEM;
11521158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
11531158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
11541158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
11551158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
11561158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11571158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11581158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11591158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11601158dfb7SGarrett Wollman 		return 0;
11611158dfb7SGarrett Wollman #endif
116282cd038dSYoshinobu Inoue #ifdef INET6
116382cd038dSYoshinobu Inoue 	case AF_INET6:
116482cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1165595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1166595b8a1cSJun-ichiro itojun Hagino 			/*
1167595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1168595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1169595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1170595b8a1cSJun-ichiro itojun Hagino 			 */
1171595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1172595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
1173595b8a1cSJun-ichiro itojun Hagino 			return 0;
1174595b8a1cSJun-ichiro itojun Hagino 		}
117582cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
117682cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
117782cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
117809df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
117909df718eSRobert Watson 		if (sdl == NULL)
118009df718eSRobert Watson 			return (ENOMEM);
118182cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
118282cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
118382cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
118482cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
118582cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
118682cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
118782cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
118882cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
118982cd038dSYoshinobu Inoue 		return 0;
119082cd038dSYoshinobu Inoue #endif
11911158dfb7SGarrett Wollman 
11921158dfb7SGarrett Wollman 	default:
11931158dfb7SGarrett Wollman 		/*
11941158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
11951158dfb7SGarrett Wollman 		 * that counts...
11961158dfb7SGarrett Wollman 		 */
11971158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
11981158dfb7SGarrett Wollman 	}
11991158dfb7SGarrett Wollman }
120040811c14SMatthew N. Dodd 
1201fc74a9f9SBrooks Davis static void*
1202fc74a9f9SBrooks Davis ether_alloc(u_char type, struct ifnet *ifp)
1203fc74a9f9SBrooks Davis {
1204fc74a9f9SBrooks Davis 	struct arpcom	*ac;
1205fc74a9f9SBrooks Davis 
1206fc74a9f9SBrooks Davis 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1207fc74a9f9SBrooks Davis 	ac->ac_ifp = ifp;
1208fc74a9f9SBrooks Davis 
1209fc74a9f9SBrooks Davis 	return (ac);
1210fc74a9f9SBrooks Davis }
1211fc74a9f9SBrooks Davis 
1212fc74a9f9SBrooks Davis static void
1213fc74a9f9SBrooks Davis ether_free(void *com, u_char type)
1214fc74a9f9SBrooks Davis {
1215fc74a9f9SBrooks Davis 
1216fc74a9f9SBrooks Davis 	free(com, M_ARPCOM);
1217fc74a9f9SBrooks Davis }
1218fc74a9f9SBrooks Davis 
1219fc74a9f9SBrooks Davis static int
1220fc74a9f9SBrooks Davis ether_modevent(module_t mod, int type, void *data)
1221fc74a9f9SBrooks Davis {
1222fc74a9f9SBrooks Davis 
1223fc74a9f9SBrooks Davis 	switch (type) {
1224fc74a9f9SBrooks Davis 	case MOD_LOAD:
1225fc74a9f9SBrooks Davis 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1226fc74a9f9SBrooks Davis 		break;
1227fc74a9f9SBrooks Davis 	case MOD_UNLOAD:
1228fc74a9f9SBrooks Davis 		if_deregister_com_alloc(IFT_ETHER);
1229fc74a9f9SBrooks Davis 		break;
1230fc74a9f9SBrooks Davis 	default:
1231fc74a9f9SBrooks Davis 		return EOPNOTSUPP;
1232fc74a9f9SBrooks Davis 	}
1233fc74a9f9SBrooks Davis 
1234fc74a9f9SBrooks Davis 	return (0);
1235fc74a9f9SBrooks Davis }
1236fc74a9f9SBrooks Davis 
123740811c14SMatthew N. Dodd static moduledata_t ether_mod = {
123840811c14SMatthew N. Dodd 	"ether",
1239fc74a9f9SBrooks Davis 	ether_modevent,
124040811c14SMatthew N. Dodd 	0
124140811c14SMatthew N. Dodd };
124240811c14SMatthew N. Dodd 
1243fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
124440811c14SMatthew N. Dodd MODULE_VERSION(ether, 1);
1245