xref: /freebsd/sys/net/if_ethersubr.c (revision 7f760c48909ddfa241914fdef1cfc33f2d30a056)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
377262d3e4SEivind Eklund #include "opt_atalk.h"
381d5e9e22SEivind Eklund #include "opt_inet.h"
39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
40430df5f4SEivind Eklund #include "opt_ipx.h"
41fb5fbe46SLuigi Rizzo #include "opt_bdg.h"
4243b29369SRobert Watson #include "opt_mac.h"
434cf49a43SJulian Elischer #include "opt_netgraph.h"
44430df5f4SEivind Eklund 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4843b29369SRobert Watson #include <sys/mac.h>
49df8bae1dSRodney W. Grimes #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
5110b1fde0SMark Murray #include <sys/random.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
5351a53488SBruce Evans #include <sys/sockio.h>
54602d513cSGarrett Wollman #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/netisr.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59df8bae1dSRodney W. Grimes #include <net/if_llc.h>
60df8bae1dSRodney W. Grimes #include <net/if_dl.h>
61df8bae1dSRodney W. Grimes #include <net/if_types.h>
622e2de7f2SArchie Cobbs #include <net/bpf.h>
63e1e1452dSArchie Cobbs #include <net/ethernet.h>
64db69a05dSPaul Saab #include <net/bridge.h>
65c1d93b05SSam Leffler #include <net/if_vlan_var.h>
66df8bae1dSRodney W. Grimes 
6782cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
714b984093SLuigi Rizzo #include <netinet/ip_fw.h>
724b984093SLuigi Rizzo #include <netinet/ip_dummynet.h>
731d5e9e22SEivind Eklund #endif
7482cd038dSYoshinobu Inoue #ifdef INET6
7582cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7682cd038dSYoshinobu Inoue #endif
77df8bae1dSRodney W. Grimes 
78cc6a66f2SJulian Elischer #ifdef IPX
79cc6a66f2SJulian Elischer #include <netipx/ipx.h>
80cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
814f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
825accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
832a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
84cc6a66f2SJulian Elischer #endif
85cc6a66f2SJulian Elischer 
86df8bae1dSRodney W. Grimes #ifdef NS
87df8bae1dSRodney W. Grimes #include <netns/ns.h>
88df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
89d0ec898dSJordan K. Hubbard ushort ns_nettype;
9088e038feSJordan K. Hubbard int ether_outputdebug = 0;
9188e038feSJordan K. Hubbard int ether_inputdebug = 0;
92df8bae1dSRodney W. Grimes #endif
93df8bae1dSRodney W. Grimes 
94655929bfSJulian Elischer #ifdef NETATALK
95655929bfSJulian Elischer #include <netatalk/at.h>
96655929bfSJulian Elischer #include <netatalk/at_var.h>
97655929bfSJulian Elischer #include <netatalk/at_extern.h>
98655929bfSJulian Elischer 
99655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
100655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
101655929bfSJulian Elischer 
102655929bfSJulian Elischer extern u_char	at_org_code[3];
103655929bfSJulian Elischer extern u_char	aarp_org_code[3];
1042cc2df49SGarrett Wollman #endif /* NETATALK */
1052cc2df49SGarrett Wollman 
106e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
107c1d93b05SSam Leffler void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
108c1d93b05SSam Leffler void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
109e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
110e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
111e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
112e1e1452dSArchie Cobbs 
113c1d93b05SSam Leffler void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
1149d4fe4b2SBrooks Davis 
115db69a05dSPaul Saab /* bridge support */
116cad15830SLuigi Rizzo int do_bridge;
117db69a05dSPaul Saab bridge_in_t *bridge_in_ptr;
118db69a05dSPaul Saab bdg_forward_t *bdg_forward_ptr;
119db69a05dSPaul Saab bdgtakeifaces_t *bdgtakeifaces_ptr;
120cad15830SLuigi Rizzo struct bdg_softc *ifp2sc;
121db69a05dSPaul Saab 
122929ddbbbSAlfred Perlstein static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
123929ddbbbSAlfred Perlstein 		struct sockaddr *);
124df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1254c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
1264c8e8c05SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP)
127df8bae1dSRodney W. Grimes 
1284b984093SLuigi Rizzo int
1294b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
130c1d93b05SSam Leffler 	struct ip_fw **rule, int shared);
1314b984093SLuigi Rizzo static int ether_ipfw;
1324b984093SLuigi Rizzo 
133df8bae1dSRodney W. Grimes /*
134df8bae1dSRodney W. Grimes  * Ethernet output routine.
135df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
136df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
137df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
138df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
139df8bae1dSRodney W. Grimes  */
140df8bae1dSRodney W. Grimes int
141d25f3712SBrian Feldman ether_output(ifp, m, dst, rt0)
142df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
143d25f3712SBrian Feldman 	struct mbuf *m;
144df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
145df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	short type;
148e1e1452dSArchie Cobbs 	int error = 0, hdrcmplt = 0;
149114ae644SMike Smith  	u_char esrc[6], edst[6];
150df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
151df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
1529961e27dSLuigi Rizzo 	int loop_copy = 0;
15384dd0fd0SJulian Elischer 	int hlen;	/* link layer header lenght */
1544c8e8c05SJulian Elischer 	struct arpcom *ac = IFP2AC(ifp);
155df8bae1dSRodney W. Grimes 
15643b29369SRobert Watson #ifdef MAC
15743b29369SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
15843b29369SRobert Watson 	if (error)
15943b29369SRobert Watson 		senderr(error);
16043b29369SRobert Watson #endif
16143b29369SRobert Watson 
162afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR)
163afbe3a0fSPoul-Henning Kamp 		senderr(ENETDOWN);
164df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
165df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1667f760c48SMatthew N. Dodd 
1677f760c48SMatthew N. Dodd 	error = rt_check(&rt, &rt0, dst);
1687f760c48SMatthew N. Dodd 	if (error)
1697f760c48SMatthew N. Dodd 		goto bad;
1707f760c48SMatthew N. Dodd 
17184dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
172df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
173df8bae1dSRodney W. Grimes #ifdef INET
174df8bae1dSRodney W. Grimes 	case AF_INET:
175322dcb8dSMax Khon 		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
176df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
17734bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
178df8bae1dSRodney W. Grimes 		break;
179df8bae1dSRodney W. Grimes #endif
18082cd038dSYoshinobu Inoue #ifdef INET6
18182cd038dSYoshinobu Inoue 	case AF_INET6:
18282cd038dSYoshinobu Inoue 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
183fef5fd23SBosko Milekic 			/* Something bad happened */
18482cd038dSYoshinobu Inoue 			return(0);
18582cd038dSYoshinobu Inoue 		}
18682cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
18782cd038dSYoshinobu Inoue 		break;
18882cd038dSYoshinobu Inoue #endif
189cc6a66f2SJulian Elischer #ifdef IPX
190cc6a66f2SJulian Elischer 	case AF_IPX:
1914f93599fSBoris Popov 		if (ef_outputp) {
1922a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
1935accfb8cSBoris Popov 		    if (error)
1945accfb8cSBoris Popov 			goto bad;
1954f93599fSBoris Popov 		} else
19634bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
197cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
198cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
199cc6a66f2SJulian Elischer 		break;
200cc6a66f2SJulian Elischer #endif
201655929bfSJulian Elischer #ifdef NETATALK
202655929bfSJulian Elischer 	case AF_APPLETALK:
2031d0eab59SJulian Elischer 	  {
204ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2051d0eab59SJulian Elischer 
206ed7509acSJulian Elischer 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
2071d0eab59SJulian Elischer 		    goto bad;
2081d0eab59SJulian Elischer 	    }
209ed7509acSJulian Elischer 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
2101d0eab59SJulian Elischer 		    return (0);
211655929bfSJulian Elischer 	    /*
212ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
213655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
214655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
215655929bfSJulian Elischer 	     */
216655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
217655929bfSJulian Elischer 		struct llc llc;
218655929bfSJulian Elischer 
219a163d034SWarner Losh 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
220655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
221655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
222655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
223655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
224655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
22534bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
22684dd0fd0SJulian Elischer 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
227655929bfSJulian Elischer 	    } else {
22834bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
229655929bfSJulian Elischer 	    }
230655929bfSJulian Elischer 	    break;
231ed7509acSJulian Elischer 	  }
2328cdfefbdSPeter Wemm #endif /* NETATALK */
233df8bae1dSRodney W. Grimes #ifdef NS
234df8bae1dSRodney W. Grimes 	case AF_NS:
23588e038feSJordan K. Hubbard 		switch(ns_nettype){
23688e038feSJordan K. Hubbard 		default:
23788e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
23888e038feSJordan K. Hubbard 			type = 0x8137;
23988e038feSJordan K. Hubbard 			break;
24088e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
24188e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
24288e038feSJordan K. Hubbard 			break;
24388e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
244a163d034SWarner Losh 			M_PREPEND(m, 3, M_TRYWAIT);
24588e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
24688e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
24788e038feSJordan K. Hubbard 			*cp++ = 0xE0;
24888e038feSJordan K. Hubbard 			*cp++ = 0xE0;
24988e038feSJordan K. Hubbard 			*cp++ = 0x03;
25088e038feSJordan K. Hubbard 			break;
25188e038feSJordan K. Hubbard 		}
252df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
253df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
2544c8e8c05SJulian Elischer 		/*
2554c8e8c05SJulian Elischer 		 * XXX if ns_thishost is the same as the node's ethernet
2564c8e8c05SJulian Elischer 		 * address then just the default code will catch this anyhow.
2574c8e8c05SJulian Elischer 		 * So I'm not sure if this next clause should be here at all?
2584c8e8c05SJulian Elischer 		 * [JRE]
2594c8e8c05SJulian Elischer 		 */
26088e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
26188e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
26288e038feSJordan K. Hubbard 			inq = &nsintrq;
263df5e1987SJonathan Lemon 			if (IF_HANDOFF(inq, m, NULL))
264df5e1987SJonathan Lemon 				schednetisr(NETISR_NS);
26588e038feSJordan K. Hubbard 			return (error);
26688e038feSJordan K. Hubbard 		}
26788e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
2684c8e8c05SJulian Elischer 			m->m_flags |= M_BCAST;
26988e038feSJordan K. Hubbard 		}
270df8bae1dSRodney W. Grimes 		break;
27188e038feSJordan K. Hubbard #endif /* NS */
272df8bae1dSRodney W. Grimes 
273114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
274114ae644SMike Smith 		hdrcmplt = 1;
275114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
276114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
277114ae644SMike Smith 		/* FALLTHROUGH */
278114ae644SMike Smith 
279df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
2809d3f194dSJulian Elischer 		loop_copy = -1; /* if this is for us, don't do it */
281df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
28294a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
283df8bae1dSRodney W. Grimes 		type = eh->ether_type;
284df8bae1dSRodney W. Grimes 		break;
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes 	default:
28729e1b85fSBrooks Davis 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
288df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
289df8bae1dSRodney W. Grimes 	}
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	/*
292df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
293df8bae1dSRodney W. Grimes 	 * allocate another.
294df8bae1dSRodney W. Grimes 	 */
295a163d034SWarner Losh 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
296df8bae1dSRodney W. Grimes 	if (m == 0)
297df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
298df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
29994a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
300df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
30194a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
302114ae644SMike Smith 	if (hdrcmplt)
303114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
304114ae644SMike Smith 			sizeof(eh->ether_shost));
305114ae644SMike Smith 	else
30694a5d9b6SDavid Greenman 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
307df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
308ed7509acSJulian Elischer 
309ed7509acSJulian Elischer 	/*
310ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
311ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
312ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
313ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
314ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
315ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
316ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
317ed7509acSJulian Elischer 	 */
3184c8e8c05SJulian Elischer 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
3193cd67511SJesper Skriver 		int csum_flags = 0;
3203cd67511SJesper Skriver 
3213cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3223cd67511SJesper Skriver 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
3233cd67511SJesper Skriver 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
3243cd67511SJesper Skriver 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
3259d3f194dSJulian Elischer 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
326ed7509acSJulian Elischer 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
327ed7509acSJulian Elischer 
3283cd67511SJesper Skriver 			n->m_pkthdr.csum_flags |= csum_flags;
3293cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3303cd67511SJesper Skriver 				n->m_pkthdr.csum_data = 0xffff;
3313cd67511SJesper Skriver 
33206a429a3SArchie Cobbs 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
333ed7509acSJulian Elischer 		} else if (bcmp(eh->ether_dhost,
334ed7509acSJulian Elischer 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
3353cd67511SJesper Skriver 			m->m_pkthdr.csum_flags |= csum_flags;
3363cd67511SJesper Skriver 			if (csum_flags & CSUM_DATA_VALID)
3373cd67511SJesper Skriver 				m->m_pkthdr.csum_data = 0xffff;
33806a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
339ed7509acSJulian Elischer 			return (0);	/* XXX */
340ed7509acSJulian Elischer 		}
341ed7509acSJulian Elischer 	}
3422e2de7f2SArchie Cobbs 
343e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
344e1e1452dSArchie Cobbs 	if (ng_ether_output_p != NULL) {
345e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
346e1e1452dSArchie Cobbs bad:			if (m != NULL)
347e1e1452dSArchie Cobbs 				m_freem(m);
348e1e1452dSArchie Cobbs 			return (error);
349e1e1452dSArchie Cobbs 		}
350e1e1452dSArchie Cobbs 		if (m == NULL)
351e1e1452dSArchie Cobbs 			return (0);
352e1e1452dSArchie Cobbs 	}
353e1e1452dSArchie Cobbs 
354e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
355e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
356e1e1452dSArchie Cobbs }
357e1e1452dSArchie Cobbs 
358e1e1452dSArchie Cobbs /*
359e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
360e1e1452dSArchie Cobbs  *
361e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
362e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
363e1e1452dSArchie Cobbs  */
364e1e1452dSArchie Cobbs int
365c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
366e1e1452dSArchie Cobbs {
3674b984093SLuigi Rizzo 	struct ip_fw *rule = NULL;
3682b25acc1SLuigi Rizzo 
3692b25acc1SLuigi Rizzo 	/* Extract info from dummynet tag, ignore others */
3702b25acc1SLuigi Rizzo 	for (; m->m_type == MT_TAG; m = m->m_next)
3712b25acc1SLuigi Rizzo 		if (m->m_flags == PACKET_TAG_DUMMYNET)
3722b25acc1SLuigi Rizzo 			rule = ((struct dn_pkt *)m)->rule;
3732b25acc1SLuigi Rizzo 
374c1d93b05SSam Leffler 	if (rule == NULL && BDG_ACTIVE(ifp)) {
375c1d93b05SSam Leffler 		/*
376c1d93b05SSam Leffler 		 * Beware, the bridge code notices the null rcvif and
377c1d93b05SSam Leffler 		 * uses that identify that it's being called from
378c1d93b05SSam Leffler 		 * ether_output as opposd to ether_input.  Yech.
379c1d93b05SSam Leffler 		 */
380fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
381c1d93b05SSam Leffler 		m = bdg_forward_ptr(m, ifp);
3822e2de7f2SArchie Cobbs 		if (m != NULL)
3832e2de7f2SArchie Cobbs 			m_freem(m);
3841db59ce6SEivind Eklund 		return (0);
385fb5fbe46SLuigi Rizzo 	}
3864b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
387c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
3884b984093SLuigi Rizzo 			if (m) {
3894b984093SLuigi Rizzo 				m_freem(m);
3904b984093SLuigi Rizzo 				return ENOBUFS;	/* pkt dropped */
3914b984093SLuigi Rizzo 			} else
3924b984093SLuigi Rizzo 				return 0;	/* consumed e.g. in a pipe */
3934b984093SLuigi Rizzo 		}
3944b984093SLuigi Rizzo 	}
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 	 */
400c1d93b05SSam Leffler 	return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS);
401df8bae1dSRodney W. Grimes }
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes /*
4044b984093SLuigi Rizzo  * ipfw processing for ethernet packets (in and out).
4054b984093SLuigi Rizzo  * The second parameter is NULL from ether_demux, and ifp from
4064b984093SLuigi Rizzo  * ether_output_frame. This section of code could be used from
4072f8ebbf4SLuigi Rizzo  * bridge.c as well as long as we use some extra info
4084b984093SLuigi Rizzo  * to distinguish that case from 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.divert_rule = 0;	/* we do not support divert yet		*/
4432b25acc1SLuigi Rizzo 	args.rule = *rule;	/* matching rule to restart		*/
4442b25acc1SLuigi Rizzo 	args.next_hop = NULL;	/* we do not support forward yet	*/
4452b25acc1SLuigi Rizzo 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
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 
465c1d93b05SSam Leffler 	if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */
4664b984093SLuigi Rizzo 		return 0;
4674b984093SLuigi Rizzo 
4684b984093SLuigi Rizzo 	if (i == 0) /* a PASS rule.  */
4694b984093SLuigi Rizzo 		return 1;
4704b984093SLuigi Rizzo 
4714b984093SLuigi Rizzo 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
4724b984093SLuigi Rizzo 		/*
4734b984093SLuigi Rizzo 		 * Pass the pkt to dummynet, which consumes it.
4744b984093SLuigi Rizzo 		 * If shared, make a copy and keep the original.
4754b984093SLuigi Rizzo 		 */
4764b984093SLuigi Rizzo 		if (shared) {
477a163d034SWarner Losh 			m = m_copypacket(m, M_DONTWAIT);
4782b25acc1SLuigi Rizzo 			if (m == NULL)
4794b984093SLuigi Rizzo 				return 0;
4804b984093SLuigi Rizzo 		} else {
4812b25acc1SLuigi Rizzo 			/*
482c1d93b05SSam Leffler 			 * Pass the original to dummynet and
483c1d93b05SSam Leffler 			 * nothing back to the caller
4842b25acc1SLuigi Rizzo 			 */
485c1d93b05SSam Leffler 			*m0 = NULL ;
4864b984093SLuigi Rizzo 		}
4872b25acc1SLuigi Rizzo 		ip_dn_io_ptr(m, (i & 0xffff),
4882b25acc1SLuigi Rizzo 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
4894b984093SLuigi Rizzo 		return 0;
4904b984093SLuigi Rizzo 	}
4914b984093SLuigi Rizzo 	/*
4922f8ebbf4SLuigi Rizzo 	 * XXX at some point add support for divert/forward actions.
4932f8ebbf4SLuigi Rizzo 	 * If none of the above matches, we have to drop the pkt.
4944b984093SLuigi Rizzo 	 */
4954b984093SLuigi Rizzo 	return 0;
4964b984093SLuigi Rizzo }
4974b984093SLuigi Rizzo 
4984b984093SLuigi Rizzo /*
499c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
500c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
501df8bae1dSRodney W. Grimes  */
502c1d93b05SSam Leffler static void
503c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m)
504df8bae1dSRodney W. Grimes {
505c1d93b05SSam Leffler 	struct ether_header *eh;
506c1d93b05SSam Leffler 	u_short etype;
507df8bae1dSRodney W. Grimes 
508c1d93b05SSam Leffler 	/*
509c1d93b05SSam Leffler 	 * Do consistency checks to verify assumptions
510c1d93b05SSam Leffler 	 * made by code past this point.
511c1d93b05SSam Leffler 	 */
512c1d93b05SSam Leffler 	if ((m->m_flags & M_PKTHDR) == 0) {
513c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o packet header\n");
514c1d93b05SSam Leffler 		ifp->if_ierrors++;
515c939f1aeSLuigi Rizzo 		m_freem(m);
516c939f1aeSLuigi Rizzo 		return;
517c939f1aeSLuigi Rizzo 	}
518c1d93b05SSam Leffler 	if (m->m_len < sizeof (struct ether_header)) {
519c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
520c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
521c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
522c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
523c1d93b05SSam Leffler 		ifp->if_ierrors++;
524c1d93b05SSam Leffler 		m_freem(m);
525c1d93b05SSam Leffler 		return;
5262e2de7f2SArchie Cobbs 	}
527c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
528c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
529c1d93b05SSam Leffler 	if (m->m_pkthdr.len >
530c1d93b05SSam Leffler 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
531c1d93b05SSam Leffler 		if_printf(ifp, "discard oversize frame "
532c1d93b05SSam Leffler 				"(ether type %x flags %x len %u > max %lu)\n",
533c1d93b05SSam Leffler 				etype, m->m_flags, m->m_pkthdr.len,
534c1d93b05SSam Leffler 				ETHER_MAX_FRAME(ifp, etype,
535c1d93b05SSam Leffler 						m->m_flags & M_HASFCS));
536c1d93b05SSam Leffler 		ifp->if_ierrors++;
537c1d93b05SSam Leffler 		m_freem(m);
538c1d93b05SSam Leffler 		return;
539c1d93b05SSam Leffler 	}
540c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif == NULL) {
541c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o interface pointer\n");
542c1d93b05SSam Leffler 		ifp->if_ierrors++;
543c1d93b05SSam Leffler 		m_freem(m);
544c1d93b05SSam Leffler 		return;
545c1d93b05SSam Leffler 	}
546c1d93b05SSam Leffler #ifdef DIAGNOSTIC
547c1d93b05SSam Leffler 	if (m->m_pkthdr.rcvif != ifp) {
548c1d93b05SSam Leffler 		if_printf(ifp, "Warning, frame marked as received on %s%u\n",
549c1d93b05SSam Leffler 			m->m_pkthdr.rcvif->if_name,
550c1d93b05SSam Leffler 			m->m_pkthdr.rcvif->if_unit);
551c1d93b05SSam Leffler 	}
552c1d93b05SSam Leffler #endif
553c1d93b05SSam Leffler 
554c1d93b05SSam Leffler 	/*
555c1d93b05SSam Leffler 	 * Give bpf a chance at the packet.
556c1d93b05SSam Leffler 	 */
557c1d93b05SSam Leffler 	BPF_MTAP(ifp, m);
5582e2de7f2SArchie Cobbs 
559afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR) {
560c1d93b05SSam Leffler 		/*
561c1d93b05SSam Leffler 		 * Interface marked for monitoring; discard packet.
562c1d93b05SSam Leffler 		 */
563afbe3a0fSPoul-Henning Kamp 		m_freem(m);
564afbe3a0fSPoul-Henning Kamp 		return;
565afbe3a0fSPoul-Henning Kamp 	}
566afbe3a0fSPoul-Henning Kamp 
567c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
568c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
569c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
570c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
571c1d93b05SSam Leffler 	}
572c1d93b05SSam Leffler 
5732201e1b0SPoul-Henning Kamp #ifdef MAC
5742201e1b0SPoul-Henning Kamp 	mac_create_mbuf_from_ifnet(ifp, m);
5752201e1b0SPoul-Henning Kamp #endif
5762201e1b0SPoul-Henning Kamp 
577c1d93b05SSam Leffler 	ifp->if_ibytes += m->m_pkthdr.len;
57805463bb5SDavid Greenman 
579e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
580e1e1452dSArchie Cobbs 	if (ng_ether_input_p != NULL) {
581c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
582e1e1452dSArchie Cobbs 		if (m == NULL)
583e1e1452dSArchie Cobbs 			return;
584e1e1452dSArchie Cobbs 	}
585e1e1452dSArchie Cobbs 
5862e2de7f2SArchie Cobbs 	/* Check for bridging mode */
5877b109fa4SLuigi Rizzo 	if (BDG_ACTIVE(ifp) ) {
5882e2de7f2SArchie Cobbs 		struct ifnet *bif;
5892e2de7f2SArchie Cobbs 
590c1d93b05SSam Leffler 		/*
591c1d93b05SSam Leffler 		 * Check with bridging code to see how the packet
592c1d93b05SSam Leffler 		 * should be handled.  Possibilities are:
593c1d93b05SSam Leffler 		 *
594c1d93b05SSam Leffler 		 *    BDG_BCAST		broadcast
595c1d93b05SSam Leffler 		 *    BDG_MCAST		multicast
596c1d93b05SSam Leffler 		 *    BDG_LOCAL		for local address, don't forward
597c1d93b05SSam Leffler 		 *    BDG_DROP		discard
598c1d93b05SSam Leffler 		 *    ifp		forward only to specified interface(s)
599c1d93b05SSam Leffler 		 *
600c1d93b05SSam Leffler 		 * Non-local destinations are handled by passing the
601c1d93b05SSam Leffler 		 * packet back to the bridge code.
602c1d93b05SSam Leffler 		 */
603c1d93b05SSam Leffler 		bif = bridge_in_ptr(ifp, eh);
604c1d93b05SSam Leffler 		if (bif == BDG_DROP) {		/* discard packet */
6052e2de7f2SArchie Cobbs 			m_freem(m);
6062e2de7f2SArchie Cobbs 			return;
6072e2de7f2SArchie Cobbs 		}
608c1d93b05SSam Leffler 		if (bif != BDG_LOCAL) {		/* non-local, forward */
609c1d93b05SSam Leffler 			m = bdg_forward_ptr(m, bif);
610ddacb30fSBosko Milekic 			/*
611c1d93b05SSam Leffler 			 * The bridge may consume the packet if it's not
612c1d93b05SSam Leffler 			 * supposed to be passed up or if a problem occurred
613c1d93b05SSam Leffler 			 * while doing its job.  This is reflected by it
614c1d93b05SSam Leffler 			 * returning a NULL mbuf pointer.
615ddacb30fSBosko Milekic 			 */
616507b4b54SLuigi Rizzo 			if (m == NULL) {
617507b4b54SLuigi Rizzo 				if (bif == BDG_BCAST || bif == BDG_MCAST)
618c1d93b05SSam Leffler 					if_printf(ifp,
619c1d93b05SSam Leffler 						"bridge dropped %s packet\n",
620c1d93b05SSam Leffler 						bif == BDG_BCAST ? "broadcast" :
621c1d93b05SSam Leffler 								   "multicast");
622ddacb30fSBosko Milekic 				return;
623ddacb30fSBosko Milekic 			}
624c1d93b05SSam Leffler 			/*
625c1d93b05SSam Leffler 			 * But in some cases the bridge may return the
626c1d93b05SSam Leffler 			 * packet for us to free; sigh.
627c1d93b05SSam Leffler 			 */
628c1d93b05SSam Leffler 			if (bif != BDG_BCAST && bif != BDG_MCAST) {
6292e2de7f2SArchie Cobbs 				m_freem(m);
6302e2de7f2SArchie Cobbs 				return;
6312e2de7f2SArchie Cobbs 			}
632c1d93b05SSam Leffler 		}
633c1d93b05SSam Leffler 	}
6342e2de7f2SArchie Cobbs 
635c1d93b05SSam Leffler 	ether_demux(ifp, m);
636a6ddbff0SPoul-Henning Kamp 	/* First chunk of an mbuf contains good entropy */
63710b1fde0SMark Murray 	if (harvest.ethernet)
63810b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
639e1e1452dSArchie Cobbs }
640e1e1452dSArchie Cobbs 
641e1e1452dSArchie Cobbs /*
642e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
643e1e1452dSArchie Cobbs  */
644e1e1452dSArchie Cobbs void
645c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
646e1e1452dSArchie Cobbs {
647c1d93b05SSam Leffler 	struct ether_header *eh;
648e1e1452dSArchie Cobbs 	struct ifqueue *inq;
649e1e1452dSArchie Cobbs 	u_short ether_type;
650e1e1452dSArchie Cobbs #if defined(NETATALK)
651e1e1452dSArchie Cobbs 	register struct llc *l;
652e1e1452dSArchie Cobbs #endif
6534b984093SLuigi Rizzo 	struct ip_fw *rule = NULL;
6542b25acc1SLuigi Rizzo 
6552b25acc1SLuigi Rizzo 	/* Extract info from dummynet tag, ignore others */
6562b25acc1SLuigi Rizzo 	for (;m->m_type == MT_TAG; m = m->m_next)
6572b25acc1SLuigi Rizzo 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
6582b25acc1SLuigi Rizzo 			rule = ((struct dn_pkt *)m)->rule;
6592b25acc1SLuigi Rizzo 			ifp = m->m_next->m_pkthdr.rcvif;
6604b984093SLuigi Rizzo 		}
6612b25acc1SLuigi Rizzo 
662c1d93b05SSam Leffler 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
663c1d93b05SSam Leffler 
664c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
665c1d93b05SSam Leffler 
6662b25acc1SLuigi Rizzo 	if (rule)	/* packet was already bridged */
6672b25acc1SLuigi Rizzo 		goto post_stats;
6684b984093SLuigi Rizzo 
669c6943100SPoul-Henning Kamp 	if (!(BDG_ACTIVE(ifp))) {
670c6943100SPoul-Henning Kamp 		/*
671c6943100SPoul-Henning Kamp 		 * Discard packet if upper layers shouldn't see it because it
672c6943100SPoul-Henning Kamp 		 * was unicast to a different Ethernet address. If the driver
673c6943100SPoul-Henning Kamp 		 * is working properly, then this situation can only happen
674c6943100SPoul-Henning Kamp 		 * when the interface is in promiscuous mode.
675c6943100SPoul-Henning Kamp 		 */
676cb24f323SArchie Cobbs 		if ((ifp->if_flags & IFF_PROMISC) != 0
677cb24f323SArchie Cobbs 		    && (eh->ether_dhost[0] & 1) == 0
678cb24f323SArchie Cobbs 		    && bcmp(eh->ether_dhost,
679ffb079beSMaxim Sobolev 		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
68025faf49dSMaxim Sobolev 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
681cb24f323SArchie Cobbs 			    m_freem(m);
682cb24f323SArchie Cobbs 			    return;
683cb24f323SArchie Cobbs 		}
684c6943100SPoul-Henning Kamp 	}
685cb24f323SArchie Cobbs 
6862e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
687df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
688df8bae1dSRodney W. Grimes 		m_freem(m);
689df8bae1dSRodney W. Grimes 		return;
690df8bae1dSRodney W. Grimes 	}
6912cc2df49SGarrett Wollman 	if (eh->ether_dhost[0] & 1) {
692df8bae1dSRodney W. Grimes 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
693df8bae1dSRodney W. Grimes 			 sizeof(etherbroadcastaddr)) == 0)
694df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
6952cc2df49SGarrett Wollman 		else
696df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
6972cc2df49SGarrett Wollman 	}
698df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
699df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
700df8bae1dSRodney W. Grimes 
7014b984093SLuigi Rizzo post_stats:
7024b984093SLuigi Rizzo 	if (IPFW_LOADED && ether_ipfw != 0) {
703c1d93b05SSam Leffler 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
7044b984093SLuigi Rizzo 			if (m)
7054b984093SLuigi Rizzo 				m_freem(m);
7064b984093SLuigi Rizzo 			return;
7074b984093SLuigi Rizzo 		}
7084b984093SLuigi Rizzo 	}
7094b984093SLuigi Rizzo 
710c1d93b05SSam Leffler 	/*
711c1d93b05SSam Leffler 	 * If VLANs are configured on the interface, check to
712c1d93b05SSam Leffler 	 * see if the device performed the decapsulation and
713c1d93b05SSam Leffler 	 * provided us with the tag.
714c1d93b05SSam Leffler 	 */
715c1d93b05SSam Leffler 	if (ifp->if_nvlans &&
716c1d93b05SSam Leffler 	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
717c1d93b05SSam Leffler 		/*
718c1d93b05SSam Leffler 		 * vlan_input() will either recursively call ether_input()
719c1d93b05SSam Leffler 		 * or drop the packet.
720c1d93b05SSam Leffler 		 */
721c1d93b05SSam Leffler 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
722c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
723c1d93b05SSam Leffler 		return;
724c1d93b05SSam Leffler 	}
725c1d93b05SSam Leffler 
726307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
727307d80beSDavid Greenman 
728c1d93b05SSam Leffler 	/*
729c1d93b05SSam Leffler 	 * Handle protocols that expect to have the Ethernet header
730c1d93b05SSam Leffler 	 * (and possibly FCS) intact.
731c1d93b05SSam Leffler 	 */
732c1d93b05SSam Leffler 	switch (ether_type) {
733c1d93b05SSam Leffler 	case ETHERTYPE_VLAN:
734c1d93b05SSam Leffler 		if (ifp->if_nvlans != 0) {
735c1d93b05SSam Leffler 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
736c1d93b05SSam Leffler 			(*vlan_input_p)(ifp, m);
737c1d93b05SSam Leffler 		} else {
738c1d93b05SSam Leffler 			ifp->if_noproto++;
739c1d93b05SSam Leffler 			m_freem(m);
740c1d93b05SSam Leffler 		}
741c1d93b05SSam Leffler 		return;
742c1d93b05SSam Leffler 	}
743c1d93b05SSam Leffler 
744c1d93b05SSam Leffler 	/* Strip off Ethernet header. */
745c1d93b05SSam Leffler 	m_adj(m, sizeof (struct ether_header));
746c1d93b05SSam Leffler 
747c1d93b05SSam Leffler 	/* If the CRC is still on the packet, trim it off. */
748c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
749c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
750c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
751c1d93b05SSam Leffler 	}
752c1d93b05SSam Leffler 
753307d80beSDavid Greenman 	switch (ether_type) {
754df8bae1dSRodney W. Grimes #ifdef INET
755df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
7561f91d8c5SDavid Greenman 		if (ipflow_fastforward(m))
7571f91d8c5SDavid Greenman 			return;
758df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
759df8bae1dSRodney W. Grimes 		inq = &ipintrq;
760df8bae1dSRodney W. Grimes 		break;
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
76308aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
76408aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
76508aadfbbSJonathan Lemon 			m_freem(m);
76608aadfbbSJonathan Lemon 			return;
76708aadfbbSJonathan Lemon 		}
768df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
769df8bae1dSRodney W. Grimes 		inq = &arpintrq;
770df8bae1dSRodney W. Grimes 		break;
771df8bae1dSRodney W. Grimes #endif
772cc6a66f2SJulian Elischer #ifdef IPX
773cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
7744f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
7754f93599fSBoris Popov 			return;
776cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
777cc6a66f2SJulian Elischer 		inq = &ipxintrq;
778cc6a66f2SJulian Elischer 		break;
779cc6a66f2SJulian Elischer #endif
78082cd038dSYoshinobu Inoue #ifdef INET6
78182cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
78282cd038dSYoshinobu Inoue 		schednetisr(NETISR_IPV6);
78382cd038dSYoshinobu Inoue 		inq = &ip6intrq;
78482cd038dSYoshinobu Inoue 		break;
78582cd038dSYoshinobu Inoue #endif
786df8bae1dSRodney W. Grimes #ifdef NS
78788e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
788df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
789df8bae1dSRodney W. Grimes 		inq = &nsintrq;
790df8bae1dSRodney W. Grimes 		break;
79188e038feSJordan K. Hubbard 
79288e038feSJordan K. Hubbard #endif /* NS */
793655929bfSJulian Elischer #ifdef NETATALK
794655929bfSJulian Elischer         case ETHERTYPE_AT:
795655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
796655929bfSJulian Elischer                 inq = &atintrq1;
797655929bfSJulian Elischer                 break;
798655929bfSJulian Elischer         case ETHERTYPE_AARP:
799655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
8004c8e8c05SJulian Elischer                 aarpinput(IFP2AC(ifp), m); /* XXX */
801655929bfSJulian Elischer                 return;
8028cdfefbdSPeter Wemm #endif /* NETATALK */
803df8bae1dSRodney W. Grimes 	default:
8044f93599fSBoris Popov #ifdef IPX
8054f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
8064f93599fSBoris Popov 			return;
8074f93599fSBoris Popov #endif /* IPX */
80888e038feSJordan K. Hubbard #ifdef NS
80988e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
81088e038feSJordan K. Hubbard 		/* Novell 802.3 */
81188e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
81288e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
81388e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
81488e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
81588e038feSJordan K. Hubbard 				m->m_len -= 3;
81688e038feSJordan K. Hubbard 				m->m_data += 3;
81788e038feSJordan K. Hubbard 			}
81888e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
81988e038feSJordan K. Hubbard 				inq = &nsintrq;
82088e038feSJordan K. Hubbard 				break;
82188e038feSJordan K. Hubbard 		}
82288e038feSJordan K. Hubbard #endif /* NS */
823242c5536SPeter Wemm #if defined(NETATALK)
824307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
825c1d93b05SSam Leffler 			goto discard;
826df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
827df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
828655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
829655929bfSJulian Elischer 		    switch (l->llc_control) {
830655929bfSJulian Elischer 		    case LLC_UI:
831655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
832c1d93b05SSam Leffler 			    goto discard;
833655929bfSJulian Elischer 
834655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
835655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
836655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
837655929bfSJulian Elischer 			    inq = &atintrq2;
838655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
839655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
840655929bfSJulian Elischer 			    break;
841655929bfSJulian Elischer 			}
842655929bfSJulian Elischer 
843655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
844655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
845655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
846655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
8474c8e8c05SJulian Elischer 			    aarpinput(IFP2AC(ifp), m); /* XXX */
848655929bfSJulian Elischer 			    return;
849655929bfSJulian Elischer 			}
850655929bfSJulian Elischer 
851655929bfSJulian Elischer 		    default:
852c1d93b05SSam Leffler 			goto discard;
853655929bfSJulian Elischer 		    }
854655929bfSJulian Elischer 		    break;
855df8bae1dSRodney W. Grimes 		default:
856c1d93b05SSam Leffler 			goto discard;
857df8bae1dSRodney W. Grimes 		}
858242c5536SPeter Wemm #else /* NETATALK */
859c1d93b05SSam Leffler 		goto discard;
860242c5536SPeter Wemm #endif /* NETATALK */
861df8bae1dSRodney W. Grimes 	}
862df8bae1dSRodney W. Grimes 
863df5e1987SJonathan Lemon 	(void) IF_HANDOFF(inq, m, NULL);
864c1d93b05SSam Leffler 	return;
865c1d93b05SSam Leffler discard:
866c1d93b05SSam Leffler 	/*
867c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
868c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
869c1d93b05SSam Leffler 	 * otherwise dispose of it.
870c1d93b05SSam Leffler 	 */
871c1d93b05SSam Leffler 	if (ng_ether_input_orphan_p != NULL) {
872c1d93b05SSam Leffler 		/*
873c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
874c1d93b05SSam Leffler 		 * consistent view of inbound packets.
875c1d93b05SSam Leffler 		 */
876a163d034SWarner Losh 		M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
877c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
878c1d93b05SSam Leffler 		return;
879c1d93b05SSam Leffler 	}
880c1d93b05SSam Leffler 	m_freem(m);
881c1d93b05SSam Leffler }
882c1d93b05SSam Leffler 
883c1d93b05SSam Leffler /*
884c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
885c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
886c1d93b05SSam Leffler  *
887c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
888c1d93b05SSam Leffler  *
889c1d93b05SSam Leffler  * since there's no static buffer involved.
890c1d93b05SSam Leffler  */
891c1d93b05SSam Leffler char *
892c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
893c1d93b05SSam Leffler {
894c1d93b05SSam Leffler 	static char etherbuf[18];
895c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
896c1d93b05SSam Leffler 	return (etherbuf);
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes /*
900df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
901df8bae1dSRodney W. Grimes  */
902df8bae1dSRodney W. Grimes void
903c1d93b05SSam Leffler ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
904df8bae1dSRodney W. Grimes {
905df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
906df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
907df8bae1dSRodney W. Grimes 
908df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
909c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
910c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
911cfeff1b6SJonathan Lemon 	if_attach(ifp);
912df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
913c1d93b05SSam Leffler 	ifp->if_output = ether_output;
914c1d93b05SSam Leffler 	ifp->if_input = ether_input;
9151158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
916a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
917c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
918322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
919c1d93b05SSam Leffler 
920f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
9216e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
92259562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
923df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
924df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
925c1d93b05SSam Leffler 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
926c1d93b05SSam Leffler 	/*
927c1d93b05SSam Leffler 	 * XXX: This doesn't belong here; we do it until
928c1d93b05SSam Leffler 	 * XXX:  all drivers are cleaned up
929c1d93b05SSam Leffler 	 */
930c1d93b05SSam Leffler 	if (llc != IFP2AC(ifp)->ac_enaddr)
931c1d93b05SSam Leffler 		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
932c1d93b05SSam Leffler 
93321b8ebd9SArchie Cobbs 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
934e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
935e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
9367b109fa4SLuigi Rizzo 	if (BDG_LOADED)
937db69a05dSPaul Saab 		bdgtakeifaces_ptr();
938e1e1452dSArchie Cobbs }
939e1e1452dSArchie Cobbs 
94021b8ebd9SArchie Cobbs /*
94121b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
94221b8ebd9SArchie Cobbs  */
94321b8ebd9SArchie Cobbs void
944c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
94521b8ebd9SArchie Cobbs {
94621b8ebd9SArchie Cobbs 	if (ng_ether_detach_p != NULL)
94721b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
94821b8ebd9SArchie Cobbs 	bpfdetach(ifp);
94921b8ebd9SArchie Cobbs 	if_detach(ifp);
9507b109fa4SLuigi Rizzo 	if (BDG_LOADED)
951db69a05dSPaul Saab 		bdgtakeifaces_ptr();
95221b8ebd9SArchie Cobbs }
95321b8ebd9SArchie Cobbs 
954ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
955602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
9564b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
9574b984093SLuigi Rizzo 	    &ether_ipfw,0,"Pass ether pkts through firewall");
95830106f6aSPoul-Henning Kamp 
959fb583156SDavid Greenman int
960c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
961c5a1016bSBruce Evans 	struct ifnet *ifp;
962c5a1016bSBruce Evans 	int command;
963c5a1016bSBruce Evans 	caddr_t data;
96430106f6aSPoul-Henning Kamp {
96530106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
96630106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
967fb583156SDavid Greenman 	int error = 0;
96830106f6aSPoul-Henning Kamp 
96930106f6aSPoul-Henning Kamp 	switch (command) {
97030106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
97130106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
97230106f6aSPoul-Henning Kamp 
97330106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
97430106f6aSPoul-Henning Kamp #ifdef INET
97530106f6aSPoul-Henning Kamp 		case AF_INET:
97630106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
977322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
97830106f6aSPoul-Henning Kamp 			break;
97930106f6aSPoul-Henning Kamp #endif
98030106f6aSPoul-Henning Kamp #ifdef IPX
98130106f6aSPoul-Henning Kamp 		/*
98230106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
98330106f6aSPoul-Henning Kamp 		 */
98430106f6aSPoul-Henning Kamp 		case AF_IPX:
98530106f6aSPoul-Henning Kamp 			{
98630106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
9874c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
98830106f6aSPoul-Henning Kamp 
98930106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
99030106f6aSPoul-Henning Kamp 				ina->x_host =
99186101139SPoul-Henning Kamp 				    *(union ipx_host *)
99286101139SPoul-Henning Kamp 			            ac->ac_enaddr;
99330106f6aSPoul-Henning Kamp 			else {
99430106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
99586101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
99686101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
99730106f6aSPoul-Henning Kamp 			}
99830106f6aSPoul-Henning Kamp 
99930106f6aSPoul-Henning Kamp 			/*
100030106f6aSPoul-Henning Kamp 			 * Set new address
100130106f6aSPoul-Henning Kamp 			 */
100230106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
100330106f6aSPoul-Henning Kamp 			break;
100430106f6aSPoul-Henning Kamp 			}
100530106f6aSPoul-Henning Kamp #endif
100630106f6aSPoul-Henning Kamp #ifdef NS
100730106f6aSPoul-Henning Kamp 		/*
100830106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
100930106f6aSPoul-Henning Kamp 		 */
101030106f6aSPoul-Henning Kamp 		case AF_NS:
101130106f6aSPoul-Henning Kamp 		{
101230106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
10134c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
101430106f6aSPoul-Henning Kamp 
101530106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
101630106f6aSPoul-Henning Kamp 				ina->x_host =
101786101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
101830106f6aSPoul-Henning Kamp 			else {
101930106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
102086101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
102186101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
102230106f6aSPoul-Henning Kamp 			}
102330106f6aSPoul-Henning Kamp 
102430106f6aSPoul-Henning Kamp 			/*
102530106f6aSPoul-Henning Kamp 			 * Set new address
102630106f6aSPoul-Henning Kamp 			 */
102730106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
102830106f6aSPoul-Henning Kamp 			break;
102930106f6aSPoul-Henning Kamp 		}
103030106f6aSPoul-Henning Kamp #endif
103130106f6aSPoul-Henning Kamp 		default:
103230106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
103330106f6aSPoul-Henning Kamp 			break;
103430106f6aSPoul-Henning Kamp 		}
103530106f6aSPoul-Henning Kamp 		break;
103630106f6aSPoul-Henning Kamp 
103730106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
103830106f6aSPoul-Henning Kamp 		{
103930106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
104030106f6aSPoul-Henning Kamp 
104130106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
10424c8e8c05SJulian Elischer 			bcopy(IFP2AC(ifp)->ac_enaddr,
104330106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
104430106f6aSPoul-Henning Kamp 		}
104530106f6aSPoul-Henning Kamp 		break;
1046fb583156SDavid Greenman 
1047fb583156SDavid Greenman 	case SIOCSIFMTU:
1048fb583156SDavid Greenman 		/*
1049fb583156SDavid Greenman 		 * Set the interface MTU.
1050fb583156SDavid Greenman 		 */
1051fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1052fb583156SDavid Greenman 			error = EINVAL;
1053fb583156SDavid Greenman 		} else {
1054fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
105530106f6aSPoul-Henning Kamp 		}
1056fb583156SDavid Greenman 		break;
1057c1d93b05SSam Leffler 	default:
1058c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1059c1d93b05SSam Leffler 		break;
1060fb583156SDavid Greenman 	}
1061fb583156SDavid Greenman 	return (error);
106230106f6aSPoul-Henning Kamp }
10631158dfb7SGarrett Wollman 
106437c84183SPoul-Henning Kamp static int
10651158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
10661158dfb7SGarrett Wollman 	struct ifnet *ifp;
10671158dfb7SGarrett Wollman 	struct sockaddr **llsa;
10681158dfb7SGarrett Wollman 	struct sockaddr *sa;
10691158dfb7SGarrett Wollman {
10701158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
10711158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
107282cd038dSYoshinobu Inoue #ifdef INET6
107382cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
107482cd038dSYoshinobu Inoue #endif
10751158dfb7SGarrett Wollman 	u_char *e_addr;
10761158dfb7SGarrett Wollman 
10771158dfb7SGarrett Wollman 	switch(sa->sa_family) {
10781158dfb7SGarrett Wollman 	case AF_LINK:
10797f33a738SJulian Elischer 		/*
10807f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
10817f33a738SJulian Elischer 		 */
10821158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
10831158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
10841158dfb7SGarrett Wollman 		if ((e_addr[0] & 1) != 1)
10851158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
10861158dfb7SGarrett Wollman 		*llsa = 0;
10871158dfb7SGarrett Wollman 		return 0;
10881158dfb7SGarrett Wollman 
10891158dfb7SGarrett Wollman #ifdef INET
10901158dfb7SGarrett Wollman 	case AF_INET:
10911158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
10921158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
10931158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
10941158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1095a163d034SWarner Losh 		       M_WAITOK|M_ZERO);
10961158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
10971158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
10981158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
10991158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
11001158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11011158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11021158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11031158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11041158dfb7SGarrett Wollman 		return 0;
11051158dfb7SGarrett Wollman #endif
110682cd038dSYoshinobu Inoue #ifdef INET6
110782cd038dSYoshinobu Inoue 	case AF_INET6:
110882cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1109595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1110595b8a1cSJun-ichiro itojun Hagino 			/*
1111595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1112595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1113595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1114595b8a1cSJun-ichiro itojun Hagino 			 */
1115595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1116595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
1117595b8a1cSJun-ichiro itojun Hagino 			return 0;
1118595b8a1cSJun-ichiro itojun Hagino 		}
111982cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
112082cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
112182cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1122a163d034SWarner Losh 		       M_WAITOK|M_ZERO);
112382cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
112482cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
112582cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
112682cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
112782cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
112882cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
112982cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
113082cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
113182cd038dSYoshinobu Inoue 		return 0;
113282cd038dSYoshinobu Inoue #endif
11331158dfb7SGarrett Wollman 
11341158dfb7SGarrett Wollman 	default:
11351158dfb7SGarrett Wollman 		/*
11361158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
11371158dfb7SGarrett Wollman 		 * that counts...
11381158dfb7SGarrett Wollman 		 */
11391158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
11401158dfb7SGarrett Wollman 	}
11411158dfb7SGarrett Wollman }
1142