xref: /freebsd/sys/net/if_ethersubr.c (revision e7f8c8339cc1804bd487a4b9906d28e92b081617)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
337262d3e4SEivind Eklund #include "opt_atalk.h"
341d5e9e22SEivind Eklund #include "opt_inet.h"
35cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
36430df5f4SEivind Eklund #include "opt_ipx.h"
3743b29369SRobert Watson #include "opt_mac.h"
384cf49a43SJulian Elischer #include "opt_netgraph.h"
39a9771948SGleb Smirnoff #include "opt_carp.h"
40430df5f4SEivind Eklund 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4541ee9f1cSPoul-Henning Kamp #include <sys/module.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
4710b1fde0SMark Murray #include <sys/random.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
4951a53488SBruce Evans #include <sys/sockio.h>
50602d513cSGarrett Wollman #include <sys/sysctl.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <net/if.h>
53d7647d96SDag-Erling Smørgrav #include <net/if_arp.h>
54df8bae1dSRodney W. Grimes #include <net/netisr.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes #include <net/if_llc.h>
57df8bae1dSRodney W. Grimes #include <net/if_dl.h>
58df8bae1dSRodney W. Grimes #include <net/if_types.h>
592e2de7f2SArchie Cobbs #include <net/bpf.h>
60e1e1452dSArchie Cobbs #include <net/ethernet.h>
61fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
62c1d93b05SSam Leffler #include <net/if_vlan_var.h>
63df8bae1dSRodney W. Grimes 
6482cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
684b984093SLuigi Rizzo #include <netinet/ip_fw.h>
694b984093SLuigi Rizzo #include <netinet/ip_dummynet.h>
701d5e9e22SEivind Eklund #endif
7182cd038dSYoshinobu Inoue #ifdef INET6
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7382cd038dSYoshinobu Inoue #endif
74df8bae1dSRodney W. Grimes 
75a9771948SGleb Smirnoff #ifdef DEV_CARP
76a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
77a9771948SGleb Smirnoff #endif
78a9771948SGleb Smirnoff 
79cc6a66f2SJulian Elischer #ifdef IPX
80cc6a66f2SJulian Elischer #include <netipx/ipx.h>
81cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
82a1026028SMaxim Sobolev #endif
834f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
845accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
852a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
86cc6a66f2SJulian Elischer 
87655929bfSJulian Elischer #ifdef NETATALK
88655929bfSJulian Elischer #include <netatalk/at.h>
89655929bfSJulian Elischer #include <netatalk/at_var.h>
90655929bfSJulian Elischer #include <netatalk/at_extern.h>
91655929bfSJulian Elischer 
92655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
93655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
94655929bfSJulian Elischer 
95655929bfSJulian Elischer extern u_char	at_org_code[3];
96655929bfSJulian Elischer extern u_char	aarp_org_code[3];
972cc2df49SGarrett Wollman #endif /* NETATALK */
982cc2df49SGarrett Wollman 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
101e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
102c1d93b05SSam Leffler void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103c1d93b05SSam Leffler void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
106e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
107e1e1452dSArchie Cobbs 
108824eb9dcSDavid E. O'Brien void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
1099d4fe4b2SBrooks Davis 
110fd6238a6SAndrew Thompson /* if_bridge(4) support */
1118f867517SAndrew Thompson struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
1128f867517SAndrew Thompson int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
1138f867517SAndrew Thompson 		struct sockaddr *, struct rtentry *);
114c8b01292SAndrew Thompson void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
1158f867517SAndrew Thompson 
116e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
117868d8b62SMatthew N. Dodd 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
118868d8b62SMatthew N. Dodd 
119929ddbbbSAlfred Perlstein static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
120929ddbbbSAlfred Perlstein 		struct sockaddr *);
121868d8b62SMatthew N. Dodd 
122fc74a9f9SBrooks Davis /* XXX: should be in an arp support file, not here */
123fc74a9f9SBrooks Davis MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
124fc74a9f9SBrooks Davis 
12569462a82SBruce M Simpson #define	ETHER_IS_BROADCAST(addr) \
12669462a82SBruce M Simpson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
12769462a82SBruce M Simpson 
1284c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
129df8bae1dSRodney W. Grimes 
130d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
1314b984093SLuigi Rizzo int
1324b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
133c1d93b05SSam Leffler 	struct ip_fw **rule, int shared);
1344b984093SLuigi Rizzo static int ether_ipfw;
135d7647d96SDag-Erling Smørgrav #endif
1364b984093SLuigi Rizzo 
137df8bae1dSRodney W. Grimes /*
138df8bae1dSRodney W. Grimes  * Ethernet output routine.
139df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
140df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
141df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
142df8bae1dSRodney W. Grimes  */
143df8bae1dSRodney W. Grimes int
14472fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m,
14572fd1b6aSDag-Erling Smørgrav 	struct sockaddr *dst, struct rtentry *rt0)
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	short type;
148cd46a114SLuigi Rizzo 	int error, hdrcmplt = 0;
14964760eb0SWarner Losh 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
150098a8c3bSMatthew N. Dodd 	struct ether_header *eh;
151c54c76ccSOleg Bulyzhin 	int loop_copy = 1;
152d32864c3SRobert Watson 	int hlen;	/* link layer header length */
153df8bae1dSRodney W. Grimes 
15443b29369SRobert Watson #ifdef MAC
15543b29369SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
15643b29369SRobert Watson 	if (error)
15743b29369SRobert Watson 		senderr(error);
15843b29369SRobert Watson #endif
15943b29369SRobert Watson 
160afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR)
161afbe3a0fSPoul-Henning Kamp 		senderr(ENETDOWN);
16213f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
16313f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
164df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1657f760c48SMatthew N. Dodd 
16684dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
167df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
168df8bae1dSRodney W. Grimes #ifdef INET
169df8bae1dSRodney W. Grimes 	case AF_INET:
170cd46a114SLuigi Rizzo 		error = arpresolve(ifp, rt0, m, dst, edst);
171cd46a114SLuigi Rizzo 		if (error)
172cd46a114SLuigi Rizzo 			return (error == EWOULDBLOCK ? 0 : error);
17334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
174df8bae1dSRodney W. Grimes 		break;
17543a6c75aSMatthew N. Dodd 	case AF_ARP:
17643a6c75aSMatthew N. Dodd 	{
17743a6c75aSMatthew N. Dodd 		struct arphdr *ah;
17843a6c75aSMatthew N. Dodd 		ah = mtod(m, struct arphdr *);
17943a6c75aSMatthew N. Dodd 		ah->ar_hrd = htons(ARPHRD_ETHER);
18043a6c75aSMatthew N. Dodd 
181c54c76ccSOleg Bulyzhin 		loop_copy = 0; /* if this is for us, don't do it */
18243a6c75aSMatthew N. Dodd 
18343a6c75aSMatthew N. Dodd 		switch(ntohs(ah->ar_op)) {
18443a6c75aSMatthew N. Dodd 		case ARPOP_REVREQUEST:
18543a6c75aSMatthew N. Dodd 		case ARPOP_REVREPLY:
18643a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_REVARP);
18743a6c75aSMatthew N. Dodd 			break;
18843a6c75aSMatthew N. Dodd 		case ARPOP_REQUEST:
18943a6c75aSMatthew N. Dodd 		case ARPOP_REPLY:
19043a6c75aSMatthew N. Dodd 		default:
19143a6c75aSMatthew N. Dodd 			type = htons(ETHERTYPE_ARP);
19243a6c75aSMatthew N. Dodd 			break;
19343a6c75aSMatthew N. Dodd 		}
19443a6c75aSMatthew N. Dodd 
19543a6c75aSMatthew N. Dodd 		if (m->m_flags & M_BCAST)
19643a6c75aSMatthew N. Dodd 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
19743a6c75aSMatthew N. Dodd 		else
19843a6c75aSMatthew N. Dodd 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
19943a6c75aSMatthew N. Dodd 
20043a6c75aSMatthew N. Dodd 	}
20143a6c75aSMatthew N. Dodd 	break;
202df8bae1dSRodney W. Grimes #endif
20382cd038dSYoshinobu Inoue #ifdef INET6
20482cd038dSYoshinobu Inoue 	case AF_INET6:
205cd46a114SLuigi Rizzo 		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
206cd46a114SLuigi Rizzo 		if (error)
207cd46a114SLuigi Rizzo 			return error;
20882cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
20982cd038dSYoshinobu Inoue 		break;
21082cd038dSYoshinobu Inoue #endif
211cc6a66f2SJulian Elischer #ifdef IPX
212cc6a66f2SJulian Elischer 	case AF_IPX:
2134f93599fSBoris Popov 		if (ef_outputp) {
2142a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
2155accfb8cSBoris Popov 		    if (error)
2165accfb8cSBoris Popov 			goto bad;
2174f93599fSBoris Popov 		} else
21834bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
219cc6a66f2SJulian Elischer 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
220cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
221cc6a66f2SJulian Elischer 		break;
222cc6a66f2SJulian Elischer #endif
223655929bfSJulian Elischer #ifdef NETATALK
224655929bfSJulian Elischer 	case AF_APPLETALK:
2251d0eab59SJulian Elischer 	  {
226ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2271d0eab59SJulian Elischer 
228cd46a114SLuigi Rizzo 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
229cd46a114SLuigi Rizzo 		    senderr(EHOSTUNREACH); /* XXX */
230cd46a114SLuigi Rizzo 	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
2311d0eab59SJulian Elischer 		    return (0);
232655929bfSJulian Elischer 	    /*
233ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
234655929bfSJulian Elischer 	     */
235655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
236655929bfSJulian Elischer 		struct llc llc;
237655929bfSJulian Elischer 
2387e2041e0SRobert Watson 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
2392166ffe8SRobert Watson 		if (m == NULL)
2402166ffe8SRobert Watson 			senderr(ENOBUFS);
241655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
242655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
243655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
244655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
245797f247bSMatthew N. Dodd 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
24634bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
247797f247bSMatthew N. Dodd 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
248655929bfSJulian Elischer 	    } else {
24934bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
250655929bfSJulian Elischer 	    }
251655929bfSJulian Elischer 	    break;
252ed7509acSJulian Elischer 	  }
2538cdfefbdSPeter Wemm #endif /* NETATALK */
254df8bae1dSRodney W. Grimes 
255114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
256114ae644SMike Smith 		hdrcmplt = 1;
257114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
258114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259114ae644SMike Smith 		/* FALLTHROUGH */
260114ae644SMike Smith 
261df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
262c54c76ccSOleg Bulyzhin 		loop_copy = 0; /* if this is for us, don't do it */
263df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
26494a5d9b6SDavid Greenman 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
265df8bae1dSRodney W. Grimes 		type = eh->ether_type;
266df8bae1dSRodney W. Grimes 		break;
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes 	default:
26929e1b85fSBrooks Davis 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
270df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
271df8bae1dSRodney W. Grimes 	}
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	/*
274df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
275df8bae1dSRodney W. Grimes 	 * allocate another.
276df8bae1dSRodney W. Grimes 	 */
277797f247bSMatthew N. Dodd 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
2782166ffe8SRobert Watson 	if (m == NULL)
279df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
280df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
28194a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
282df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
28394a5d9b6SDavid Greenman 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
284114ae644SMike Smith 	if (hdrcmplt)
285114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
286114ae644SMike Smith 			sizeof(eh->ether_shost));
287114ae644SMike Smith 	else
2884a0d6638SRuslan Ermilov 		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
289df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
290ed7509acSJulian Elischer 
291ed7509acSJulian Elischer 	/*
292ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
293ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
294ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
295ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
296ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
297ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
298ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
299ed7509acSJulian Elischer 	 */
300c54c76ccSOleg Bulyzhin 	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
3010bdf5171SMax Laier 	    m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
3023cd67511SJesper Skriver 		int csum_flags = 0;
3033cd67511SJesper Skriver 
3043cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3053cd67511SJesper Skriver 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
3063cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
3073cd67511SJesper Skriver 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
30806f684b0SMatthew N. Dodd 
309c54c76ccSOleg Bulyzhin 		if (m->m_flags & M_BCAST) {
31006f684b0SMatthew N. Dodd 			struct mbuf *n;
311ed7509acSJulian Elischer 
312294dd290SLuigi Rizzo 			/*
313294dd290SLuigi Rizzo 			 * Because if_simloop() modifies the packet, we need a
314294dd290SLuigi Rizzo 			 * writable copy through m_dup() instead of a readonly
315294dd290SLuigi Rizzo 			 * one as m_copy[m] would give us. The alternative would
316294dd290SLuigi Rizzo 			 * be to modify if_simloop() to handle the readonly mbuf,
317294dd290SLuigi Rizzo 			 * but performancewise it is mostly equivalent (trading
318294dd290SLuigi Rizzo 			 * extra data copying vs. extra locking).
3199983b3c0SYaroslav Tykhiy 			 *
3209983b3c0SYaroslav Tykhiy 			 * XXX This is a local workaround.  A number of less
3219983b3c0SYaroslav Tykhiy 			 * often used kernel parts suffer from the same bug.
3229983b3c0SYaroslav Tykhiy 			 * See PR kern/105943 for a proposed general solution.
323294dd290SLuigi Rizzo 			 */
324294dd290SLuigi Rizzo 			if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
3253cd67511SJesper Skriver 				n->m_pkthdr.csum_flags |= csum_flags;
3263cd67511SJesper Skriver 				if (csum_flags & CSUM_DATA_VALID)
3273cd67511SJesper Skriver 					n->m_pkthdr.csum_data = 0xffff;
32806a429a3SArchie Cobbs 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
329c1404dc0SArchie Cobbs 			} else
330c1404dc0SArchie Cobbs 				ifp->if_iqdrops++;
33106f684b0SMatthew N. Dodd 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
33206f684b0SMatthew N. Dodd 				ETHER_ADDR_LEN) == 0) {
3333cd67511SJesper Skriver 			m->m_pkthdr.csum_flags |= csum_flags;
3343cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3353cd67511SJesper Skriver 				m->m_pkthdr.csum_data = 0xffff;
33606a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
337ed7509acSJulian Elischer 			return (0);	/* XXX */
338ed7509acSJulian Elischer 		}
339ed7509acSJulian Elischer 	}
3402e2de7f2SArchie Cobbs 
3413f7d1396SAndrew Thompson        /*
3423f7d1396SAndrew Thompson 	* Bridges require special output handling.
3433f7d1396SAndrew Thompson 	*/
3443f7d1396SAndrew Thompson 	if (ifp->if_bridge) {
3453f7d1396SAndrew Thompson 		BRIDGE_OUTPUT(ifp, m, error);
3463f7d1396SAndrew Thompson 		return (error);
3473f7d1396SAndrew Thompson 	}
3483f7d1396SAndrew Thompson 
349a9771948SGleb Smirnoff #ifdef DEV_CARP
350a9771948SGleb Smirnoff 	if (ifp->if_carp &&
351a9771948SGleb Smirnoff 	    (error = carp_output(ifp, m, dst, NULL)))
352a9771948SGleb Smirnoff 		goto bad;
353a9771948SGleb Smirnoff #endif
354a9771948SGleb Smirnoff 
355e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
356eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
357514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_output_p != NULL,
358514bcb89SPoul-Henning Kamp 		    ("ng_ether_output_p is NULL"));
359e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
360e1e1452dSArchie Cobbs bad:			if (m != NULL)
361e1e1452dSArchie Cobbs 				m_freem(m);
362e1e1452dSArchie Cobbs 			return (error);
363e1e1452dSArchie Cobbs 		}
364e1e1452dSArchie Cobbs 		if (m == NULL)
365e1e1452dSArchie Cobbs 			return (0);
366e1e1452dSArchie Cobbs 	}
367e1e1452dSArchie Cobbs 
368e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
369e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
370e1e1452dSArchie Cobbs }
371e1e1452dSArchie Cobbs 
372e1e1452dSArchie Cobbs /*
373e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
374e1e1452dSArchie Cobbs  *
375e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
376e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
377e1e1452dSArchie Cobbs  */
378e1e1452dSArchie Cobbs int
379c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
380e1e1452dSArchie Cobbs {
381d0a2acd4SJulian Elischer 	int error;
382d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
383ac9d7e26SMax Laier 	struct ip_fw *rule = ip_dn_claim_rule(m);
3842b25acc1SLuigi Rizzo 
3854b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
386c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
3874b984093SLuigi Rizzo 			if (m) {
3884b984093SLuigi Rizzo 				m_freem(m);
389e7909401SDavid Malone 				return EACCES;	/* pkt dropped */
3904b984093SLuigi Rizzo 			} else
3914b984093SLuigi Rizzo 				return 0;	/* consumed e.g. in a pipe */
3924b984093SLuigi Rizzo 		}
3934b984093SLuigi Rizzo 	}
394d7647d96SDag-Erling Smørgrav #endif
3952b25acc1SLuigi Rizzo 
396df8bae1dSRodney W. Grimes 	/*
397df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
398df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
399df8bae1dSRodney W. Grimes 	 */
400affc907dSMax Laier 	IFQ_HANDOFF(ifp, m, error);
401affc907dSMax Laier 	return (error);
402df8bae1dSRodney W. Grimes }
403df8bae1dSRodney W. Grimes 
404d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
405df8bae1dSRodney W. Grimes /*
4064b984093SLuigi Rizzo  * ipfw processing for ethernet packets (in and out).
4074b984093SLuigi Rizzo  * The second parameter is NULL from ether_demux, and ifp from
408b6de9e91SMax Laier  * ether_output_frame.
4094b984093SLuigi Rizzo  */
4104b984093SLuigi Rizzo int
4114b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
412c1d93b05SSam Leffler 	struct ip_fw **rule, int shared)
4134b984093SLuigi Rizzo {
414c1d93b05SSam Leffler 	struct ether_header *eh;
415c1d93b05SSam Leffler 	struct ether_header save_eh;
416c1d93b05SSam Leffler 	struct mbuf *m;
4174b984093SLuigi Rizzo 	int i;
4182b25acc1SLuigi Rizzo 	struct ip_fw_args args;
4194b984093SLuigi Rizzo 
42097850a5dSLuigi Rizzo 	if (*rule != NULL && fw_one_pass)
421015d72e0SLuigi Rizzo 		return 1; /* dummynet packet, already partially processed */
4222f8ebbf4SLuigi Rizzo 
4234b984093SLuigi Rizzo 	/*
4242f8ebbf4SLuigi Rizzo 	 * I need some amt of data to be contiguous, and in case others need
4254b984093SLuigi Rizzo 	 * the packet (shared==1) also better be in the first mbuf.
4264b984093SLuigi Rizzo 	 */
427c1d93b05SSam Leffler 	m = *m0;
428c1d93b05SSam Leffler 	i = min( m->m_pkthdr.len, max_protohdr);
429c1d93b05SSam Leffler 	if ( shared || m->m_len < i) {
430c1d93b05SSam Leffler 		m = m_pullup(m, i);
431c1d93b05SSam Leffler 		if (m == NULL) {
432c1d93b05SSam Leffler 			*m0 = m;
4334b984093SLuigi Rizzo 			return 0;
4344b984093SLuigi Rizzo 		}
435c1d93b05SSam Leffler 	}
436c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
437c1d93b05SSam Leffler 	save_eh = *eh;			/* save copy for restore below */
438c1d93b05SSam Leffler 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
4394b984093SLuigi Rizzo 
440c1d93b05SSam Leffler 	args.m = m;		/* the packet we are looking at		*/
4412b25acc1SLuigi Rizzo 	args.oif = dst;		/* destination, if any			*/
4422b25acc1SLuigi Rizzo 	args.rule = *rule;	/* matching rule to restart		*/
4432b25acc1SLuigi Rizzo 	args.next_hop = NULL;	/* we do not support forward yet	*/
4442b25acc1SLuigi Rizzo 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
4453ecf1851SOleg Bulyzhin 	args.inp = NULL;	/* used by ipfw uid/gid/jail rules	*/
4462b25acc1SLuigi Rizzo 	i = ip_fw_chk_ptr(&args);
447c1d93b05SSam Leffler 	m = args.m;
448c1d93b05SSam Leffler 	if (m != NULL) {
449c1d93b05SSam Leffler 		/*
450c1d93b05SSam Leffler 		 * Restore Ethernet header, as needed, in case the
451c1d93b05SSam Leffler 		 * mbuf chain was replaced by ipfw.
452c1d93b05SSam Leffler 		 */
453a163d034SWarner Losh 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
454c1d93b05SSam Leffler 		if (m == NULL) {
455c1d93b05SSam Leffler 			*m0 = m;
456c1d93b05SSam Leffler 			return 0;
457c1d93b05SSam Leffler 		}
458c1d93b05SSam Leffler 		if (eh != mtod(m, struct ether_header *))
459c1d93b05SSam Leffler 			bcopy(&save_eh, mtod(m, struct ether_header *),
460c1d93b05SSam Leffler 				ETHER_HDR_LEN);
461c1d93b05SSam Leffler 	}
462c1d93b05SSam Leffler 	*m0 = m;
4632b25acc1SLuigi Rizzo 	*rule = args.rule;
4642b25acc1SLuigi Rizzo 
4656c69a7c3SGleb Smirnoff 	if (i == IP_FW_DENY) /* drop */
4664b984093SLuigi Rizzo 		return 0;
4674b984093SLuigi Rizzo 
4686c69a7c3SGleb Smirnoff 	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
4696c69a7c3SGleb Smirnoff 
4706c69a7c3SGleb Smirnoff 	if (i == IP_FW_PASS) /* a PASS rule.  */
4714b984093SLuigi Rizzo 		return 1;
4724b984093SLuigi Rizzo 
4736c69a7c3SGleb Smirnoff 	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
4744b984093SLuigi Rizzo 		/*
4754b984093SLuigi Rizzo 		 * Pass the pkt to dummynet, which consumes it.
4764b984093SLuigi Rizzo 		 * If shared, make a copy and keep the original.
4774b984093SLuigi Rizzo 		 */
4784b984093SLuigi Rizzo 		if (shared) {
479a163d034SWarner Losh 			m = m_copypacket(m, M_DONTWAIT);
4802b25acc1SLuigi Rizzo 			if (m == NULL)
4814b984093SLuigi Rizzo 				return 0;
4824b984093SLuigi Rizzo 		} else {
4832b25acc1SLuigi Rizzo 			/*
484c1d93b05SSam Leffler 			 * Pass the original to dummynet and
485c1d93b05SSam Leffler 			 * nothing back to the caller
4862b25acc1SLuigi Rizzo 			 */
487c1d93b05SSam Leffler 			*m0 = NULL ;
4884b984093SLuigi Rizzo 		}
48928935658SGleb Smirnoff 		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
4904b984093SLuigi Rizzo 		return 0;
4914b984093SLuigi Rizzo 	}
4924b984093SLuigi Rizzo 	/*
4932f8ebbf4SLuigi Rizzo 	 * XXX at some point add support for divert/forward actions.
4942f8ebbf4SLuigi Rizzo 	 * If none of the above matches, we have to drop the pkt.
4954b984093SLuigi Rizzo 	 */
4964b984093SLuigi Rizzo 	return 0;
4974b984093SLuigi Rizzo }
498d7647d96SDag-Erling Smørgrav #endif
4994b984093SLuigi Rizzo 
5004b984093SLuigi Rizzo /*
501c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
502c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
503df8bae1dSRodney W. Grimes  */
504c1d93b05SSam Leffler static void
505c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m)
506df8bae1dSRodney W. Grimes {
507c1d93b05SSam Leffler 	struct ether_header *eh;
508c1d93b05SSam Leffler 	u_short etype;
509df8bae1dSRodney W. Grimes 
51069462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_UP) == 0) {
51169462a82SBruce M Simpson 		m_freem(m);
51269462a82SBruce M Simpson 		return;
51369462a82SBruce M Simpson 	}
51469462a82SBruce M Simpson #ifdef DIAGNOSTIC
51569462a82SBruce M Simpson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
51669462a82SBruce M Simpson 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
51769462a82SBruce M Simpson 		m_freem(m);
51869462a82SBruce M Simpson 		return;
51969462a82SBruce M Simpson 	}
52069462a82SBruce M Simpson #endif
521c1d93b05SSam Leffler 	/*
522c1d93b05SSam Leffler 	 * Do consistency checks to verify assumptions
523c1d93b05SSam Leffler 	 * made by code past this point.
524c1d93b05SSam Leffler 	 */
525c1d93b05SSam Leffler 	if ((m->m_flags & M_PKTHDR) == 0) {
526c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o packet header\n");
527c1d93b05SSam Leffler 		ifp->if_ierrors++;
528c939f1aeSLuigi Rizzo 		m_freem(m);
529c939f1aeSLuigi Rizzo 		return;
530c939f1aeSLuigi Rizzo 	}
531797f247bSMatthew N. Dodd 	if (m->m_len < ETHER_HDR_LEN) {
532c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
533c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
534c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
535c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
536c1d93b05SSam Leffler 		ifp->if_ierrors++;
537c1d93b05SSam Leffler 		m_freem(m);
538c1d93b05SSam Leffler 		return;
5392e2de7f2SArchie Cobbs 	}
540c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
541c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
542c1d93b05SSam Leffler 	if (m->m_pkthdr.len >
543c1d93b05SSam Leffler 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
544c1d93b05SSam Leffler 		if_printf(ifp, "discard oversize frame "
545c1d93b05SSam Leffler 				"(ether type %x flags %x len %u > max %lu)\n",
546c1d93b05SSam Leffler 				etype, m->m_flags, m->m_pkthdr.len,
547c1d93b05SSam Leffler 				ETHER_MAX_FRAME(ifp, etype,
548c1d93b05SSam Leffler 						m->m_flags & M_HASFCS));
549c1d93b05SSam Leffler 		ifp->if_ierrors++;
550c1d93b05SSam Leffler 		m_freem(m);
551c1d93b05SSam Leffler 		return;
552c1d93b05SSam Leffler 	}
553c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif == NULL) {
554c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o interface pointer\n");
555c1d93b05SSam Leffler 		ifp->if_ierrors++;
556c1d93b05SSam Leffler 		m_freem(m);
557c1d93b05SSam Leffler 		return;
558c1d93b05SSam Leffler 	}
559c1d93b05SSam Leffler #ifdef DIAGNOSTIC
560c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif != ifp) {
5619bf40edeSBrooks Davis 		if_printf(ifp, "Warning, frame marked as received on %s\n",
5629bf40edeSBrooks Davis 			m->m_pkthdr.rcvif->if_xname);
563c1d93b05SSam Leffler 	}
564c1d93b05SSam Leffler #endif
565c1d93b05SSam Leffler 
56669462a82SBruce M Simpson 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
56769462a82SBruce M Simpson 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
56869462a82SBruce M Simpson 			m->m_flags |= M_BCAST;
56969462a82SBruce M Simpson 		else
57069462a82SBruce M Simpson 			m->m_flags |= M_MCAST;
57169462a82SBruce M Simpson 		ifp->if_imcasts++;
57269462a82SBruce M Simpson 	}
57369462a82SBruce M Simpson 
574533d8562SRobert Watson #ifdef MAC
575533d8562SRobert Watson 	/*
576533d8562SRobert Watson 	 * Tag the mbuf with an appropriate MAC label before any other
577533d8562SRobert Watson 	 * consumers can get to it.
578533d8562SRobert Watson 	 */
579533d8562SRobert Watson 	mac_create_mbuf_from_ifnet(ifp, m);
580533d8562SRobert Watson #endif
581533d8562SRobert Watson 
582c1d93b05SSam Leffler 	/*
58369462a82SBruce M Simpson 	 * Give bpf a chance at the packet.
584c1d93b05SSam Leffler 	 */
585d05d4616SChristian S.J. Peron 	ETHER_BPF_MTAP(ifp, m);
5862e2de7f2SArchie Cobbs 
58769462a82SBruce M Simpson 	/*
58869462a82SBruce M Simpson 	 * If the CRC is still on the packet, trim it off. We do this once
58969462a82SBruce M Simpson 	 * and once only in case we are re-entered. Nothing else on the
59069462a82SBruce M Simpson 	 * Ethernet receive path expects to see the FCS.
59169462a82SBruce M Simpson 	 */
592c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
593c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
594c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
595c1d93b05SSam Leffler 	}
596c1d93b05SSam Leffler 
597c1d93b05SSam Leffler 	ifp->if_ibytes += m->m_pkthdr.len;
59805463bb5SDavid Greenman 
59969462a82SBruce M Simpson 	/* Allow monitor mode to claim this frame, after stats are updated. */
600de572b37SChristian S.J. Peron 	if (ifp->if_flags & IFF_MONITOR) {
601de572b37SChristian S.J. Peron 		m_freem(m);
602de572b37SChristian S.J. Peron 		return;
603de572b37SChristian S.J. Peron 	}
604de572b37SChristian S.J. Peron 
60569462a82SBruce M Simpson 	/*
60669462a82SBruce M Simpson 	 * If the hardware did not process an 802.1Q tag, do this now,
60769462a82SBruce M Simpson 	 * to allow 802.1P priority frames to be passed to the main input
60869462a82SBruce M Simpson 	 * path correctly.
60969462a82SBruce M Simpson 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
61069462a82SBruce M Simpson 	 */
61169462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
61269462a82SBruce M Simpson 		struct ether_vlan_header *evl;
61369462a82SBruce M Simpson 
61469462a82SBruce M Simpson 		if (m->m_len < sizeof(*evl) &&
61569462a82SBruce M Simpson 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
616402d5e27SBruce M Simpson #ifdef DIAGNOSTIC
61769462a82SBruce M Simpson 			if_printf(ifp, "cannot pullup VLAN header\n");
618402d5e27SBruce M Simpson #endif
61969462a82SBruce M Simpson 			ifp->if_ierrors++;
62069462a82SBruce M Simpson 			m_freem(m);
62169462a82SBruce M Simpson 			return;
62269462a82SBruce M Simpson 		}
62369462a82SBruce M Simpson 
62469462a82SBruce M Simpson 		evl = mtod(m, struct ether_vlan_header *);
62569462a82SBruce M Simpson 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
62669462a82SBruce M Simpson 		m->m_flags |= M_VLANTAG;
62769462a82SBruce M Simpson 
62869462a82SBruce M Simpson 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
62969462a82SBruce M Simpson 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
63069462a82SBruce M Simpson 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
63169462a82SBruce M Simpson 	}
63269462a82SBruce M Simpson 
63369462a82SBruce M Simpson 	/* Allow ng_ether(4) to claim this frame. */
634eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
635514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_p != NULL,
63669462a82SBruce M Simpson 		    ("%s: ng_ether_input_p is NULL", __func__));
63769462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
638c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
639e1e1452dSArchie Cobbs 		if (m == NULL)
640e1e1452dSArchie Cobbs 			return;
641e1e1452dSArchie Cobbs 	}
642e1e1452dSArchie Cobbs 
6438f867517SAndrew Thompson 	/*
64469462a82SBruce M Simpson 	 * Allow if_bridge(4) to claim this frame.
64569462a82SBruce M Simpson 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
64669462a82SBruce M Simpson 	 * and the frame should be delivered locally.
6478f867517SAndrew Thompson 	 */
64869462a82SBruce M Simpson 	if (ifp->if_bridge != NULL) {
64969462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
650fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
6518f867517SAndrew Thompson 		if (m == NULL)
6528f867517SAndrew Thompson 			return;
6538f867517SAndrew Thompson 	}
6548f867517SAndrew Thompson 
65569462a82SBruce M Simpson #ifdef DEV_CARP
65669462a82SBruce M Simpson 	/*
65769462a82SBruce M Simpson 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
65869462a82SBruce M Simpson 	 * mbuf flows up to Layer 3.
65969462a82SBruce M Simpson 	 * FreeBSD's implementation of carp(4) uses the inprotosw
66069462a82SBruce M Simpson 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
66169462a82SBruce M Simpson 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
66269462a82SBruce M Simpson 	 * is outside the scope of the M_PROMISC test below.
66369462a82SBruce M Simpson 	 * TODO: Maintain a hash table of ethernet addresses other than
66469462a82SBruce M Simpson 	 * ether_dhost which may be active on this ifp.
66569462a82SBruce M Simpson 	 */
66669462a82SBruce M Simpson 	if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
66769462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
66869462a82SBruce M Simpson 	} else
66969462a82SBruce M Simpson #endif
67069462a82SBruce M Simpson 	{
67169462a82SBruce M Simpson 		/*
672e7f8c833SBruce M Simpson 		 * If the frame received was not for our MAC address, set the
67369462a82SBruce M Simpson 		 * M_PROMISC flag on the mbuf chain. The frame may need to
67469462a82SBruce M Simpson 		 * be seen by the rest of the Ethernet input path in case of
67569462a82SBruce M Simpson 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
67669462a82SBruce M Simpson 		 * seen by upper protocol layers.
67769462a82SBruce M Simpson 		 */
67869462a82SBruce M Simpson 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
679e7f8c833SBruce M Simpson 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
68069462a82SBruce M Simpson 			m->m_flags |= M_PROMISC;
68169462a82SBruce M Simpson 	}
68269462a82SBruce M Simpson 
683a6ddbff0SPoul-Henning Kamp 	/* First chunk of an mbuf contains good entropy */
68410b1fde0SMark Murray 	if (harvest.ethernet)
68510b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
68669462a82SBruce M Simpson 
687acf032f5SRobert Watson 	ether_demux(ifp, m);
688e1e1452dSArchie Cobbs }
689e1e1452dSArchie Cobbs 
690e1e1452dSArchie Cobbs /*
691e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
692e1e1452dSArchie Cobbs  */
693e1e1452dSArchie Cobbs void
694c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
695e1e1452dSArchie Cobbs {
696c1d93b05SSam Leffler 	struct ether_header *eh;
6971cafed39SJonathan Lemon 	int isr;
698e1e1452dSArchie Cobbs 	u_short ether_type;
699e1e1452dSArchie Cobbs #if defined(NETATALK)
700098a8c3bSMatthew N. Dodd 	struct llc *l;
701e1e1452dSArchie Cobbs #endif
70269462a82SBruce M Simpson 
70369462a82SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
70469462a82SBruce M Simpson 
705d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
70669462a82SBruce M Simpson 	/*
70769462a82SBruce M Simpson 	 * Allow dummynet and/or ipfw to claim the frame.
70869462a82SBruce M Simpson 	 * Do not do this for PROMISC frames in case we are re-entered.
70969462a82SBruce M Simpson 	 */
71069462a82SBruce M Simpson 	if (IPFW_LOADED && ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
711ac9d7e26SMax Laier 		struct ip_fw *rule = ip_dn_claim_rule(m);
71269462a82SBruce M Simpson 
71369462a82SBruce M Simpson 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
71469462a82SBruce M Simpson 			if (m)
71569462a82SBruce M Simpson 				m_freem(m);	/* dropped; free mbuf chain */
71669462a82SBruce M Simpson 			return;			/* consumed */
71769462a82SBruce M Simpson 		}
71869462a82SBruce M Simpson 	}
719d7647d96SDag-Erling Smørgrav #endif
720c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
721cd0cd014SJoerg Wunsch 	ether_type = ntohs(eh->ether_type);
722c1d93b05SSam Leffler 
723a9771948SGleb Smirnoff 	/*
72469462a82SBruce M Simpson 	 * If this frame has a VLAN tag other than 0, call vlan_input()
72569462a82SBruce M Simpson 	 * if its module is loaded. Otherwise, drop.
726a9771948SGleb Smirnoff 	 */
72769462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) &&
72869462a82SBruce M Simpson 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
72975ee267cSGleb Smirnoff 		if (ifp->if_vlantrunk == NULL) {
7306c23e6ccSRuslan Ermilov 			ifp->if_noproto++;
7316c23e6ccSRuslan Ermilov 			m_freem(m);
7326c23e6ccSRuslan Ermilov 			return;
7336c23e6ccSRuslan Ermilov 		}
73469462a82SBruce M Simpson 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
73569462a82SBruce M Simpson 		    __func__));
73669462a82SBruce M Simpson 		/* Clear before possibly re-entering ether_input(). */
73769462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
738c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
739c1d93b05SSam Leffler 		return;
740c1d93b05SSam Leffler 	}
741c1d93b05SSam Leffler 
742c1d93b05SSam Leffler 	/*
74369462a82SBruce M Simpson 	 * Pass promiscuously received frames to the upper layer if the user
74469462a82SBruce M Simpson 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
745c1d93b05SSam Leffler 	 */
74669462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
747c1d93b05SSam Leffler 		m_freem(m);
748c1d93b05SSam Leffler 		return;
749c1d93b05SSam Leffler 	}
750c1d93b05SSam Leffler 
75169462a82SBruce M Simpson 	/*
75269462a82SBruce M Simpson 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
75369462a82SBruce M Simpson 	 * Strip off Ethernet header.
75469462a82SBruce M Simpson 	 */
75569462a82SBruce M Simpson 	m->m_flags &= ~M_VLANTAG;
75669462a82SBruce M Simpson 	m->m_flags &= ~(M_PROTOFLAGS);
757797f247bSMatthew N. Dodd 	m_adj(m, ETHER_HDR_LEN);
758c1d93b05SSam Leffler 
75969462a82SBruce M Simpson 	/*
76069462a82SBruce M Simpson 	 * Dispatch frame to upper layer.
76169462a82SBruce M Simpson 	 */
762307d80beSDavid Greenman 	switch (ether_type) {
763df8bae1dSRodney W. Grimes #ifdef INET
764df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
7655d691e6dSAndre Oppermann 		if ((m = ip_fastforward(m)) == NULL)
7661f91d8c5SDavid Greenman 			return;
7671cafed39SJonathan Lemon 		isr = NETISR_IP;
768df8bae1dSRodney W. Grimes 		break;
769df8bae1dSRodney W. Grimes 
770df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
77108aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
77208aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
77308aadfbbSJonathan Lemon 			m_freem(m);
77408aadfbbSJonathan Lemon 			return;
77508aadfbbSJonathan Lemon 		}
7761cafed39SJonathan Lemon 		isr = NETISR_ARP;
777df8bae1dSRodney W. Grimes 		break;
778df8bae1dSRodney W. Grimes #endif
779cc6a66f2SJulian Elischer #ifdef IPX
780cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
7814f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
7824f93599fSBoris Popov 			return;
7831cafed39SJonathan Lemon 		isr = NETISR_IPX;
784cc6a66f2SJulian Elischer 		break;
785cc6a66f2SJulian Elischer #endif
78682cd038dSYoshinobu Inoue #ifdef INET6
78782cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
7881cafed39SJonathan Lemon 		isr = NETISR_IPV6;
78982cd038dSYoshinobu Inoue 		break;
79082cd038dSYoshinobu Inoue #endif
791655929bfSJulian Elischer #ifdef NETATALK
792655929bfSJulian Elischer 	case ETHERTYPE_AT:
7931cafed39SJonathan Lemon 		isr = NETISR_ATALK1;
794655929bfSJulian Elischer 		break;
795655929bfSJulian Elischer 	case ETHERTYPE_AARP:
7961cafed39SJonathan Lemon 		isr = NETISR_AARP;
7971cafed39SJonathan Lemon 		break;
7988cdfefbdSPeter Wemm #endif /* NETATALK */
799df8bae1dSRodney W. Grimes 	default:
8004f93599fSBoris Popov #ifdef IPX
8014f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8024f93599fSBoris Popov 			return;
8034f93599fSBoris Popov #endif /* IPX */
804242c5536SPeter Wemm #if defined(NETATALK)
805307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
806c1d93b05SSam Leffler 			goto discard;
807df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
8081cafed39SJonathan Lemon 		if (l->llc_dsap == LLC_SNAP_LSAP &&
8091cafed39SJonathan Lemon 		    l->llc_ssap == LLC_SNAP_LSAP &&
8101cafed39SJonathan Lemon 		    l->llc_control == LLC_UI) {
8114158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
812655929bfSJulian Elischer 			    sizeof(at_org_code)) == 0 &&
813655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
814797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8151cafed39SJonathan Lemon 				isr = NETISR_ATALK2;
816655929bfSJulian Elischer 				break;
817655929bfSJulian Elischer 			}
8184158372fSLuigi Rizzo 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
819655929bfSJulian Elischer 			    sizeof(aarp_org_code)) == 0 &&
820655929bfSJulian Elischer 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
821797f247bSMatthew N. Dodd 				m_adj(m, LLC_SNAPFRAMELEN);
8221cafed39SJonathan Lemon 				isr = NETISR_AARP;
823655929bfSJulian Elischer 				break;
824df8bae1dSRodney W. Grimes 			}
8251cafed39SJonathan Lemon 		}
826242c5536SPeter Wemm #endif /* NETATALK */
8271cafed39SJonathan Lemon 		goto discard;
828df8bae1dSRodney W. Grimes 	}
8291cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
830c1d93b05SSam Leffler 	return;
8311cafed39SJonathan Lemon 
832c1d93b05SSam Leffler discard:
833c1d93b05SSam Leffler 	/*
834c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
835c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
836c1d93b05SSam Leffler 	 * otherwise dispose of it.
837c1d93b05SSam Leffler 	 */
838eb46c866SGleb Smirnoff 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
839514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_orphan_p != NULL,
840514bcb89SPoul-Henning Kamp 		    ("ng_ether_input_orphan_p is NULL"));
841c1d93b05SSam Leffler 		/*
842c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
843c1d93b05SSam Leffler 		 * consistent view of inbound packets.
844c1d93b05SSam Leffler 		 */
845797f247bSMatthew N. Dodd 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
846c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
847c1d93b05SSam Leffler 		return;
848c1d93b05SSam Leffler 	}
849c1d93b05SSam Leffler 	m_freem(m);
850c1d93b05SSam Leffler }
851c1d93b05SSam Leffler 
852c1d93b05SSam Leffler /*
853c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
854c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
855c1d93b05SSam Leffler  *
856c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
857c1d93b05SSam Leffler  *
858c1d93b05SSam Leffler  * since there's no static buffer involved.
859c1d93b05SSam Leffler  */
860c1d93b05SSam Leffler char *
861c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
862c1d93b05SSam Leffler {
863c1d93b05SSam Leffler 	static char etherbuf[18];
864c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
865c1d93b05SSam Leffler 	return (etherbuf);
866df8bae1dSRodney W. Grimes }
867df8bae1dSRodney W. Grimes 
868df8bae1dSRodney W. Grimes /*
869df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
870df8bae1dSRodney W. Grimes  */
871df8bae1dSRodney W. Grimes void
872d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
873df8bae1dSRodney W. Grimes {
874f93dfa28SBrooks Davis 	int i;
875098a8c3bSMatthew N. Dodd 	struct ifaddr *ifa;
876098a8c3bSMatthew N. Dodd 	struct sockaddr_dl *sdl;
877df8bae1dSRodney W. Grimes 
878c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
879c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
880cfeff1b6SJonathan Lemon 	if_attach(ifp);
881df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
882c1d93b05SSam Leffler 	ifp->if_output = ether_output;
883c1d93b05SSam Leffler 	ifp->if_input = ether_input;
8841158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
885a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
886c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
887322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
888c1d93b05SSam Leffler 
8894a0d6638SRuslan Ermilov 	ifa = ifp->if_addr;
8906e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
89159562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
892df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
893df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
894d09ed26fSRuslan Ermilov 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
895c1d93b05SSam Leffler 
896797f247bSMatthew N. Dodd 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
897e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
898e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
899e3bbbec2SMatthew N. Dodd 
900f93dfa28SBrooks Davis 	/* Announce Ethernet MAC address if non-zero. */
901f93dfa28SBrooks Davis 	for (i = 0; i < ifp->if_addrlen; i++)
902d09ed26fSRuslan Ermilov 		if (lla[i] != 0)
903f93dfa28SBrooks Davis 			break;
904f93dfa28SBrooks Davis 	if (i != ifp->if_addrlen)
905d09ed26fSRuslan Ermilov 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
906af5e59bfSRobert Watson 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
907af5e59bfSRobert Watson 		if_printf(ifp, "if_start running deferred for Giant\n");
908e1e1452dSArchie Cobbs }
909e1e1452dSArchie Cobbs 
91021b8ebd9SArchie Cobbs /*
91121b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
91221b8ebd9SArchie Cobbs  */
91321b8ebd9SArchie Cobbs void
914c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
91521b8ebd9SArchie Cobbs {
916514bcb89SPoul-Henning Kamp 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
917514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_detach_p != NULL,
918514bcb89SPoul-Henning Kamp 		    ("ng_ether_detach_p is NULL"));
91921b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
920514bcb89SPoul-Henning Kamp 	}
92120a65f37SAndrew Thompson 
92221b8ebd9SArchie Cobbs 	bpfdetach(ifp);
92321b8ebd9SArchie Cobbs 	if_detach(ifp);
92421b8ebd9SArchie Cobbs }
92521b8ebd9SArchie Cobbs 
926ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
927602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
928d7647d96SDag-Erling Smørgrav #if defined(INET) || defined(INET6)
9294b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
9304b984093SLuigi Rizzo 	    &ether_ipfw,0,"Pass ether pkts through firewall");
931d7647d96SDag-Erling Smørgrav #endif
93230106f6aSPoul-Henning Kamp 
93316b4a343SChristian Weisgerber #if 0
93416b4a343SChristian Weisgerber /*
93516b4a343SChristian Weisgerber  * This is for reference.  We have a table-driven version
93616b4a343SChristian Weisgerber  * of the little-endian crc32 generator, which is faster
93716b4a343SChristian Weisgerber  * than the double-loop.
93816b4a343SChristian Weisgerber  */
93916b4a343SChristian Weisgerber uint32_t
94016b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
94116b4a343SChristian Weisgerber {
94216b4a343SChristian Weisgerber 	size_t i;
94316b4a343SChristian Weisgerber 	uint32_t crc;
94416b4a343SChristian Weisgerber 	int bit;
94516b4a343SChristian Weisgerber 	uint8_t data;
94616b4a343SChristian Weisgerber 
94716b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
94816b4a343SChristian Weisgerber 
94916b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
95016b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
95116b4a343SChristian Weisgerber 			carry = (crc ^ data) & 1;
95216b4a343SChristian Weisgerber 			crc >>= 1;
95316b4a343SChristian Weisgerber 			if (carry)
95416b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_LE);
95516b4a343SChristian Weisgerber 	}
95616b4a343SChristian Weisgerber 
95716b4a343SChristian Weisgerber 	return (crc);
95816b4a343SChristian Weisgerber }
95916b4a343SChristian Weisgerber #else
96016b4a343SChristian Weisgerber uint32_t
96116b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
96216b4a343SChristian Weisgerber {
96316b4a343SChristian Weisgerber 	static const uint32_t crctab[] = {
96416b4a343SChristian Weisgerber 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
96516b4a343SChristian Weisgerber 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
96616b4a343SChristian Weisgerber 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
96716b4a343SChristian Weisgerber 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
96816b4a343SChristian Weisgerber 	};
96916b4a343SChristian Weisgerber 	size_t i;
97016b4a343SChristian Weisgerber 	uint32_t crc;
97116b4a343SChristian Weisgerber 
97216b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
97316b4a343SChristian Weisgerber 
97416b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
97516b4a343SChristian Weisgerber 		crc ^= buf[i];
97616b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
97716b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
97816b4a343SChristian Weisgerber 	}
97916b4a343SChristian Weisgerber 
98016b4a343SChristian Weisgerber 	return (crc);
98116b4a343SChristian Weisgerber }
98216b4a343SChristian Weisgerber #endif
98316b4a343SChristian Weisgerber 
98416b4a343SChristian Weisgerber uint32_t
98516b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len)
98616b4a343SChristian Weisgerber {
98716b4a343SChristian Weisgerber 	size_t i;
98816b4a343SChristian Weisgerber 	uint32_t crc, carry;
98916b4a343SChristian Weisgerber 	int bit;
99016b4a343SChristian Weisgerber 	uint8_t data;
99116b4a343SChristian Weisgerber 
99216b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
99316b4a343SChristian Weisgerber 
99416b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
99516b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
99616b4a343SChristian Weisgerber 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
99716b4a343SChristian Weisgerber 			crc <<= 1;
99816b4a343SChristian Weisgerber 			if (carry)
99916b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
100016b4a343SChristian Weisgerber 		}
100116b4a343SChristian Weisgerber 	}
100216b4a343SChristian Weisgerber 
100316b4a343SChristian Weisgerber 	return (crc);
100416b4a343SChristian Weisgerber }
100516b4a343SChristian Weisgerber 
1006fb583156SDavid Greenman int
100772fd1b6aSDag-Erling Smørgrav ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
100830106f6aSPoul-Henning Kamp {
100930106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
101030106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
1011fb583156SDavid Greenman 	int error = 0;
101230106f6aSPoul-Henning Kamp 
101330106f6aSPoul-Henning Kamp 	switch (command) {
101430106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
101530106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
101630106f6aSPoul-Henning Kamp 
101730106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
101830106f6aSPoul-Henning Kamp #ifdef INET
101930106f6aSPoul-Henning Kamp 		case AF_INET:
102030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1021322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
102230106f6aSPoul-Henning Kamp 			break;
102330106f6aSPoul-Henning Kamp #endif
102430106f6aSPoul-Henning Kamp #ifdef IPX
102530106f6aSPoul-Henning Kamp 		/*
102630106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
102730106f6aSPoul-Henning Kamp 		 */
102830106f6aSPoul-Henning Kamp 		case AF_IPX:
102930106f6aSPoul-Henning Kamp 			{
1030098a8c3bSMatthew N. Dodd 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
103130106f6aSPoul-Henning Kamp 
103230106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
103330106f6aSPoul-Henning Kamp 				ina->x_host =
103486101139SPoul-Henning Kamp 				    *(union ipx_host *)
10354a0d6638SRuslan Ermilov 				    IF_LLADDR(ifp);
103630106f6aSPoul-Henning Kamp 			else {
103730106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
10384a0d6638SRuslan Ermilov 				      (caddr_t) IF_LLADDR(ifp),
1039fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
104030106f6aSPoul-Henning Kamp 			}
104130106f6aSPoul-Henning Kamp 
104230106f6aSPoul-Henning Kamp 			/*
104330106f6aSPoul-Henning Kamp 			 * Set new address
104430106f6aSPoul-Henning Kamp 			 */
104530106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
104630106f6aSPoul-Henning Kamp 			break;
104730106f6aSPoul-Henning Kamp 			}
104830106f6aSPoul-Henning Kamp #endif
104930106f6aSPoul-Henning Kamp 		default:
105030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
105130106f6aSPoul-Henning Kamp 			break;
105230106f6aSPoul-Henning Kamp 		}
105330106f6aSPoul-Henning Kamp 		break;
105430106f6aSPoul-Henning Kamp 
105530106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
105630106f6aSPoul-Henning Kamp 		{
105730106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
105830106f6aSPoul-Henning Kamp 
105930106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
10604a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(ifp),
106130106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
106230106f6aSPoul-Henning Kamp 		}
106330106f6aSPoul-Henning Kamp 		break;
1064fb583156SDavid Greenman 
1065fb583156SDavid Greenman 	case SIOCSIFMTU:
1066fb583156SDavid Greenman 		/*
1067fb583156SDavid Greenman 		 * Set the interface MTU.
1068fb583156SDavid Greenman 		 */
1069fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1070fb583156SDavid Greenman 			error = EINVAL;
1071fb583156SDavid Greenman 		} else {
1072fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
107330106f6aSPoul-Henning Kamp 		}
1074fb583156SDavid Greenman 		break;
1075c1d93b05SSam Leffler 	default:
1076c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1077c1d93b05SSam Leffler 		break;
1078fb583156SDavid Greenman 	}
1079fb583156SDavid Greenman 	return (error);
108030106f6aSPoul-Henning Kamp }
10811158dfb7SGarrett Wollman 
108237c84183SPoul-Henning Kamp static int
108372fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
108472fd1b6aSDag-Erling Smørgrav 	struct sockaddr *sa)
10851158dfb7SGarrett Wollman {
10861158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
1087d7647d96SDag-Erling Smørgrav #ifdef INET
10881158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
1089d7647d96SDag-Erling Smørgrav #endif
109082cd038dSYoshinobu Inoue #ifdef INET6
109182cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
109282cd038dSYoshinobu Inoue #endif
10931158dfb7SGarrett Wollman 	u_char *e_addr;
10941158dfb7SGarrett Wollman 
10951158dfb7SGarrett Wollman 	switch(sa->sa_family) {
10961158dfb7SGarrett Wollman 	case AF_LINK:
10977f33a738SJulian Elischer 		/*
10987f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
10997f33a738SJulian Elischer 		 */
11001158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
11011158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
1102086e98c4SBruce M Simpson 		if (!ETHER_IS_MULTICAST(e_addr))
11031158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11041158dfb7SGarrett Wollman 		*llsa = 0;
11051158dfb7SGarrett Wollman 		return 0;
11061158dfb7SGarrett Wollman 
11071158dfb7SGarrett Wollman #ifdef INET
11081158dfb7SGarrett Wollman 	case AF_INET:
11091158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
11101158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
11111158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
11121158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
111309df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
111409df718eSRobert Watson 		if (sdl == NULL)
111509df718eSRobert Watson 			return ENOMEM;
11161158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
11171158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
11181158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
11191158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
11201158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11211158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11221158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11231158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11241158dfb7SGarrett Wollman 		return 0;
11251158dfb7SGarrett Wollman #endif
112682cd038dSYoshinobu Inoue #ifdef INET6
112782cd038dSYoshinobu Inoue 	case AF_INET6:
112882cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1129595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1130595b8a1cSJun-ichiro itojun Hagino 			/*
1131595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1132595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1133595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1134595b8a1cSJun-ichiro itojun Hagino 			 */
1135595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1136595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
1137595b8a1cSJun-ichiro itojun Hagino 			return 0;
1138595b8a1cSJun-ichiro itojun Hagino 		}
113982cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
114082cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
114182cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
114209df718eSRobert Watson 		       M_NOWAIT|M_ZERO);
114309df718eSRobert Watson 		if (sdl == NULL)
114409df718eSRobert Watson 			return (ENOMEM);
114582cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
114682cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
114782cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
114882cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
114982cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
115082cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
115182cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
115282cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
115382cd038dSYoshinobu Inoue 		return 0;
115482cd038dSYoshinobu Inoue #endif
11551158dfb7SGarrett Wollman 
11561158dfb7SGarrett Wollman 	default:
11571158dfb7SGarrett Wollman 		/*
11581158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
11591158dfb7SGarrett Wollman 		 * that counts...
11601158dfb7SGarrett Wollman 		 */
11611158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
11621158dfb7SGarrett Wollman 	}
11631158dfb7SGarrett Wollman }
116440811c14SMatthew N. Dodd 
1165fc74a9f9SBrooks Davis static void*
1166fc74a9f9SBrooks Davis ether_alloc(u_char type, struct ifnet *ifp)
1167fc74a9f9SBrooks Davis {
1168fc74a9f9SBrooks Davis 	struct arpcom	*ac;
1169fc74a9f9SBrooks Davis 
1170fc74a9f9SBrooks Davis 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1171fc74a9f9SBrooks Davis 	ac->ac_ifp = ifp;
1172fc74a9f9SBrooks Davis 
1173fc74a9f9SBrooks Davis 	return (ac);
1174fc74a9f9SBrooks Davis }
1175fc74a9f9SBrooks Davis 
1176fc74a9f9SBrooks Davis static void
1177fc74a9f9SBrooks Davis ether_free(void *com, u_char type)
1178fc74a9f9SBrooks Davis {
1179fc74a9f9SBrooks Davis 
1180fc74a9f9SBrooks Davis 	free(com, M_ARPCOM);
1181fc74a9f9SBrooks Davis }
1182fc74a9f9SBrooks Davis 
1183fc74a9f9SBrooks Davis static int
1184fc74a9f9SBrooks Davis ether_modevent(module_t mod, int type, void *data)
1185fc74a9f9SBrooks Davis {
1186fc74a9f9SBrooks Davis 
1187fc74a9f9SBrooks Davis 	switch (type) {
1188fc74a9f9SBrooks Davis 	case MOD_LOAD:
1189fc74a9f9SBrooks Davis 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1190fc74a9f9SBrooks Davis 		break;
1191fc74a9f9SBrooks Davis 	case MOD_UNLOAD:
1192fc74a9f9SBrooks Davis 		if_deregister_com_alloc(IFT_ETHER);
1193fc74a9f9SBrooks Davis 		break;
1194fc74a9f9SBrooks Davis 	default:
1195fc74a9f9SBrooks Davis 		return EOPNOTSUPP;
1196fc74a9f9SBrooks Davis 	}
1197fc74a9f9SBrooks Davis 
1198fc74a9f9SBrooks Davis 	return (0);
1199fc74a9f9SBrooks Davis }
1200fc74a9f9SBrooks Davis 
120140811c14SMatthew N. Dodd static moduledata_t ether_mod = {
120240811c14SMatthew N. Dodd 	"ether",
1203fc74a9f9SBrooks Davis 	ether_modevent,
120440811c14SMatthew N. Dodd 	0
120540811c14SMatthew N. Dodd };
120640811c14SMatthew N. Dodd 
120752f1277eSChristian S.J. Peron void
120852f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
120952f1277eSChristian S.J. Peron {
121052f1277eSChristian S.J. Peron 	struct ether_vlan_header vlan;
121152f1277eSChristian S.J. Peron 	struct mbuf mv, mb;
121252f1277eSChristian S.J. Peron 
121352f1277eSChristian S.J. Peron 	KASSERT((m->m_flags & M_VLANTAG) != 0,
121452f1277eSChristian S.J. Peron 	    ("%s: vlan information not present", __func__));
121552f1277eSChristian S.J. Peron 	KASSERT(m->m_len >= sizeof(struct ether_header),
121652f1277eSChristian S.J. Peron 	    ("%s: mbuf not large enough for header", __func__));
121752f1277eSChristian S.J. Peron 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
121852f1277eSChristian S.J. Peron 	vlan.evl_proto = vlan.evl_encap_proto;
121952f1277eSChristian S.J. Peron 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
122052f1277eSChristian S.J. Peron 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
122152f1277eSChristian S.J. Peron 	m->m_len -= sizeof(struct ether_header);
122252f1277eSChristian S.J. Peron 	m->m_data += sizeof(struct ether_header);
122352f1277eSChristian S.J. Peron 	/*
122452f1277eSChristian S.J. Peron 	 * If a data link has been supplied by the caller, then we will need to
122552f1277eSChristian S.J. Peron 	 * re-create a stack allocated mbuf chain with the following structure:
122652f1277eSChristian S.J. Peron 	 *
122752f1277eSChristian S.J. Peron 	 * (1) mbuf #1 will contain the supplied data link
122852f1277eSChristian S.J. Peron 	 * (2) mbuf #2 will contain the vlan header
122952f1277eSChristian S.J. Peron 	 * (3) mbuf #3 will contain the original mbuf's packet data
123052f1277eSChristian S.J. Peron 	 *
123152f1277eSChristian S.J. Peron 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
123252f1277eSChristian S.J. Peron 	 */
123352f1277eSChristian S.J. Peron 	if (data != NULL) {
123452f1277eSChristian S.J. Peron 		mv.m_next = m;
123552f1277eSChristian S.J. Peron 		mv.m_data = (caddr_t)&vlan;
123652f1277eSChristian S.J. Peron 		mv.m_len = sizeof(vlan);
123752f1277eSChristian S.J. Peron 		mb.m_next = &mv;
123852f1277eSChristian S.J. Peron 		mb.m_data = data;
123952f1277eSChristian S.J. Peron 		mb.m_len = dlen;
124052f1277eSChristian S.J. Peron 		bpf_mtap(bp, &mb);
124152f1277eSChristian S.J. Peron 	} else
124252f1277eSChristian S.J. Peron 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
124352f1277eSChristian S.J. Peron 	m->m_len += sizeof(struct ether_header);
124452f1277eSChristian S.J. Peron 	m->m_data -= sizeof(struct ether_header);
124552f1277eSChristian S.J. Peron }
124652f1277eSChristian S.J. Peron 
1247fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
124840811c14SMatthew N. Dodd MODULE_VERSION(ether, 1);
1249