xref: /freebsd/sys/net/if_ethersubr.c (revision 322dcb8d)
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"
424cf49a43SJulian Elischer #include "opt_netgraph.h"
43430df5f4SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
4910b1fde0SMark Murray #include <sys/random.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
5151a53488SBruce Evans #include <sys/sockio.h>
52602d513cSGarrett Wollman #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/netisr.h>
56df8bae1dSRodney W. Grimes #include <net/route.h>
57df8bae1dSRodney W. Grimes #include <net/if_llc.h>
58df8bae1dSRodney W. Grimes #include <net/if_dl.h>
59df8bae1dSRodney W. Grimes #include <net/if_types.h>
602e2de7f2SArchie Cobbs #include <net/bpf.h>
61e1e1452dSArchie Cobbs #include <net/ethernet.h>
62db69a05dSPaul Saab #include <net/bridge.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>
681d5e9e22SEivind Eklund #endif
6982cd038dSYoshinobu Inoue #ifdef INET6
7082cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7182cd038dSYoshinobu Inoue #endif
72df8bae1dSRodney W. Grimes 
73cc6a66f2SJulian Elischer #ifdef IPX
74cc6a66f2SJulian Elischer #include <netipx/ipx.h>
75cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
764f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
775accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
782a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
79cc6a66f2SJulian Elischer #endif
80cc6a66f2SJulian Elischer 
81df8bae1dSRodney W. Grimes #ifdef NS
82df8bae1dSRodney W. Grimes #include <netns/ns.h>
83df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
84d0ec898dSJordan K. Hubbard ushort ns_nettype;
8588e038feSJordan K. Hubbard int ether_outputdebug = 0;
8688e038feSJordan K. Hubbard int ether_inputdebug = 0;
87df8bae1dSRodney W. Grimes #endif
88df8bae1dSRodney W. Grimes 
89655929bfSJulian Elischer #ifdef NETATALK
90655929bfSJulian Elischer #include <netatalk/at.h>
91655929bfSJulian Elischer #include <netatalk/at_var.h>
92655929bfSJulian Elischer #include <netatalk/at_extern.h>
93655929bfSJulian Elischer 
94655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
95655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
96655929bfSJulian Elischer 
97655929bfSJulian Elischer extern u_char	at_org_code[3];
98655929bfSJulian Elischer extern u_char	aarp_org_code[3];
992cc2df49SGarrett Wollman #endif /* NETATALK */
1002cc2df49SGarrett Wollman 
101e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
102e1e1452dSArchie Cobbs void	(*ng_ether_input_p)(struct ifnet *ifp,
103e1e1452dSArchie Cobbs 		struct mbuf **mp, struct ether_header *eh);
104e1e1452dSArchie Cobbs void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
105e1e1452dSArchie Cobbs 		struct mbuf *m, struct ether_header *eh);
106e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
107e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
108e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
109e1e1452dSArchie Cobbs 
1109d4fe4b2SBrooks Davis int	(*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
1119d4fe4b2SBrooks Davis int	(*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
1129d4fe4b2SBrooks Davis 		u_int16_t t);
1139d4fe4b2SBrooks Davis 
114db69a05dSPaul Saab /* bridge support */
115db69a05dSPaul Saab int do_bridge = 0;
116db69a05dSPaul Saab bridge_in_t *bridge_in_ptr;
117db69a05dSPaul Saab bdg_forward_t *bdg_forward_ptr;
118db69a05dSPaul Saab bdgtakeifaces_t *bdgtakeifaces_ptr;
119db69a05dSPaul Saab struct bdg_softc *ifp2sc = NULL;
120db69a05dSPaul Saab 
1211158dfb7SGarrett Wollman static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
1221158dfb7SGarrett Wollman 				    struct sockaddr *));
123df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1244c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
1254c8e8c05SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP)
126df8bae1dSRodney W. Grimes 
127df8bae1dSRodney W. Grimes /*
128df8bae1dSRodney W. Grimes  * Ethernet output routine.
129df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
130df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
131df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
132df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
133df8bae1dSRodney W. Grimes  */
134df8bae1dSRodney W. Grimes int
135d25f3712SBrian Feldman ether_output(ifp, m, dst, rt0)
136df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
137d25f3712SBrian Feldman 	struct mbuf *m;
138df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
139df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	short type;
142e1e1452dSArchie Cobbs 	int error = 0, hdrcmplt = 0;
143114ae644SMike Smith  	u_char esrc[6], edst[6];
144df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
145df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
146e1e1452dSArchie Cobbs 	int off, loop_copy = 0;
14784dd0fd0SJulian Elischer 	int hlen;	/* link layer header lenght */
1484c8e8c05SJulian Elischer 	struct arpcom *ac = IFP2AC(ifp);
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
151df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1523bda9f9bSPoul-Henning Kamp 	rt = rt0;
1533bda9f9bSPoul-Henning Kamp 	if (rt) {
154df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1553bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1563bda9f9bSPoul-Henning Kamp 			if (rt0)
157df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
158df8bae1dSRodney W. Grimes 			else
159df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
160df8bae1dSRodney W. Grimes 		}
161df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
162df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
163df8bae1dSRodney W. Grimes 				goto lookup;
164df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
165df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
166995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
167995add1aSGarrett Wollman 							  0UL);
168df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
169df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
170df8bae1dSRodney W. Grimes 			}
171df8bae1dSRodney W. Grimes 		}
172df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
173df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
174227ee8a1SPoul-Henning Kamp 			    time_second < rt->rt_rmx.rmx_expire)
175df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
176df8bae1dSRodney W. Grimes 	}
17784dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
178df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
179df8bae1dSRodney W. Grimes #ifdef INET
180df8bae1dSRodney W. Grimes 	case AF_INET:
181322dcb8dSMax Khon 		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
182df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
183df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
18434bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
185df8bae1dSRodney W. Grimes 		break;
186df8bae1dSRodney W. Grimes #endif
18782cd038dSYoshinobu Inoue #ifdef INET6
18882cd038dSYoshinobu Inoue 	case AF_INET6:
18982cd038dSYoshinobu Inoue 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
190fef5fd23SBosko Milekic 			/* Something bad happened */
19182cd038dSYoshinobu Inoue 			return(0);
19282cd038dSYoshinobu Inoue 		}
19382cd038dSYoshinobu Inoue 		off = m->m_pkthdr.len - m->m_len;
19482cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
19582cd038dSYoshinobu Inoue 		break;
19682cd038dSYoshinobu Inoue #endif
197cc6a66f2SJulian Elischer #ifdef IPX
198cc6a66f2SJulian Elischer 	case AF_IPX:
1994f93599fSBoris Popov 		if (ef_outputp) {
2002a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
2015accfb8cSBoris Popov 		    if (error)
2025accfb8cSBoris Popov 			goto bad;
2034f93599fSBoris Popov 		} else
20434bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
205cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
206cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
207cc6a66f2SJulian Elischer 		break;
208cc6a66f2SJulian Elischer #endif
209655929bfSJulian Elischer #ifdef NETATALK
210655929bfSJulian Elischer 	case AF_APPLETALK:
2111d0eab59SJulian Elischer 	  {
212ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2131d0eab59SJulian Elischer 
214ed7509acSJulian Elischer 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
2151d0eab59SJulian Elischer 		    goto bad;
2161d0eab59SJulian Elischer 	    }
217ed7509acSJulian Elischer 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
2181d0eab59SJulian Elischer 		    return (0);
219655929bfSJulian Elischer 	    /*
220ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
221655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
222655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
223655929bfSJulian Elischer 	     */
224655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
225655929bfSJulian Elischer 		struct llc llc;
226655929bfSJulian Elischer 
2272a0c503eSBosko Milekic 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
228655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
229655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
230655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
231655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
232655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
23334bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
23484dd0fd0SJulian Elischer 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
235655929bfSJulian Elischer 	    } else {
23634bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
237655929bfSJulian Elischer 	    }
238655929bfSJulian Elischer 	    break;
239ed7509acSJulian Elischer 	  }
2408cdfefbdSPeter Wemm #endif /* NETATALK */
241df8bae1dSRodney W. Grimes #ifdef NS
242df8bae1dSRodney W. Grimes 	case AF_NS:
24388e038feSJordan K. Hubbard 		switch(ns_nettype){
24488e038feSJordan K. Hubbard 		default:
24588e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
24688e038feSJordan K. Hubbard 			type = 0x8137;
24788e038feSJordan K. Hubbard 			break;
24888e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
24988e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
25088e038feSJordan K. Hubbard 			break;
25188e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
2522a0c503eSBosko Milekic 			M_PREPEND(m, 3, M_TRYWAIT);
25388e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
25488e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
25588e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25688e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25788e038feSJordan K. Hubbard 			*cp++ = 0x03;
25888e038feSJordan K. Hubbard 			break;
25988e038feSJordan K. Hubbard 		}
260df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
261df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
2624c8e8c05SJulian Elischer 		/*
2634c8e8c05SJulian Elischer 		 * XXX if ns_thishost is the same as the node's ethernet
2644c8e8c05SJulian Elischer 		 * address then just the default code will catch this anyhow.
2654c8e8c05SJulian Elischer 		 * So I'm not sure if this next clause should be here at all?
2664c8e8c05SJulian Elischer 		 * [JRE]
2674c8e8c05SJulian Elischer 		 */
26888e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
26988e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
27088e038feSJordan K. Hubbard 			inq = &nsintrq;
271df5e1987SJonathan Lemon 			if (IF_HANDOFF(inq, m, NULL))
272df5e1987SJonathan Lemon 				schednetisr(NETISR_NS);
27388e038feSJordan K. Hubbard 			return (error);
27488e038feSJordan K. Hubbard 		}
27588e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
2764c8e8c05SJulian Elischer 			m->m_flags |= M_BCAST;
27788e038feSJordan K. Hubbard 		}
278df8bae1dSRodney W. Grimes 		break;
27988e038feSJordan K. Hubbard #endif /* NS */
280df8bae1dSRodney W. Grimes 
281114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
282114ae644SMike Smith 		hdrcmplt = 1;
283114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
284114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
285114ae644SMike Smith 		/* FALLTHROUGH */
286114ae644SMike Smith 
287df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
2889d3f194dSJulian Elischer 		loop_copy = -1; /* if this is for us, don't do it */
289df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
29094a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
291df8bae1dSRodney W. Grimes 		type = eh->ether_type;
292df8bae1dSRodney W. Grimes 		break;
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes 	default:
295df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
296df8bae1dSRodney W. Grimes 			dst->sa_family);
297df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
298df8bae1dSRodney W. Grimes 	}
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
301df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
302df8bae1dSRodney W. Grimes 	 * allocate another.
303df8bae1dSRodney W. Grimes 	 */
304df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
305df8bae1dSRodney W. Grimes 	if (m == 0)
306df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
307df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
30894a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
309df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
31094a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
311114ae644SMike Smith 	if (hdrcmplt)
312114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
313114ae644SMike Smith 			sizeof(eh->ether_shost));
314114ae644SMike Smith 	else
31594a5d9b6SDavid Greenman 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
316df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
317ed7509acSJulian Elischer 
318ed7509acSJulian Elischer 	/*
319ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
320ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
321ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
322ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
323ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
324ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
325ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
326ed7509acSJulian Elischer 	 */
3274c8e8c05SJulian Elischer 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
3289d3f194dSJulian Elischer 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
329ed7509acSJulian Elischer 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
330ed7509acSJulian Elischer 
33106a429a3SArchie Cobbs 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
332ed7509acSJulian Elischer 		} else if (bcmp(eh->ether_dhost,
333ed7509acSJulian Elischer 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
33406a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
335ed7509acSJulian Elischer 			return (0);	/* XXX */
336ed7509acSJulian Elischer 		}
337ed7509acSJulian Elischer 	}
3382e2de7f2SArchie Cobbs 
339e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
340e1e1452dSArchie Cobbs 	if (ng_ether_output_p != NULL) {
341e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
342e1e1452dSArchie Cobbs bad:			if (m != NULL)
343e1e1452dSArchie Cobbs 				m_freem(m);
344e1e1452dSArchie Cobbs 			return (error);
345e1e1452dSArchie Cobbs 		}
346e1e1452dSArchie Cobbs 		if (m == NULL)
347e1e1452dSArchie Cobbs 			return (0);
348e1e1452dSArchie Cobbs 	}
349e1e1452dSArchie Cobbs 
350e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
351e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
352e1e1452dSArchie Cobbs }
353e1e1452dSArchie Cobbs 
354e1e1452dSArchie Cobbs /*
355e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
356e1e1452dSArchie Cobbs  *
357e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
358e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
359e1e1452dSArchie Cobbs  */
360e1e1452dSArchie Cobbs int
361e1e1452dSArchie Cobbs ether_output_frame(ifp, m)
362e1e1452dSArchie Cobbs 	struct ifnet *ifp;
363e1e1452dSArchie Cobbs 	struct mbuf *m;
364e1e1452dSArchie Cobbs {
365df5e1987SJonathan Lemon 	int error = 0;
366e1e1452dSArchie Cobbs 
367db69a05dSPaul Saab 	if (do_bridge && bdg_forward_ptr != NULL && BDG_USED(ifp) ) {
368507b4b54SLuigi Rizzo 		struct ether_header *eh; /* a ptr suffices */
3691db59ce6SEivind Eklund 
370fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
371507b4b54SLuigi Rizzo 		eh = mtod(m, struct ether_header *);
3722e2de7f2SArchie Cobbs 		m_adj(m, ETHER_HDR_LEN);
373db69a05dSPaul Saab 		m = bdg_forward_ptr(m, eh, ifp);
3742e2de7f2SArchie Cobbs 		if (m != NULL)
3752e2de7f2SArchie Cobbs 			m_freem(m);
3761db59ce6SEivind Eklund 		return (0);
377fb5fbe46SLuigi Rizzo 	}
3782e2de7f2SArchie Cobbs 
379df8bae1dSRodney W. Grimes 	/*
380df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
381df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
382df8bae1dSRodney W. Grimes 	 */
383df5e1987SJonathan Lemon 	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
384e1e1452dSArchie Cobbs 		return (ENOBUFS);
385df8bae1dSRodney W. Grimes 	return (error);
386df8bae1dSRodney W. Grimes }
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
390df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
391df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
392e1e1452dSArchie Cobbs  *
39302a282acSLuigi Rizzo  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
39402a282acSLuigi Rizzo  * cluster, right before m_data. So be very careful when working on m,
39502a282acSLuigi Rizzo  * as you could destroy *eh !!
39602a282acSLuigi Rizzo  * A (probably) more convenient and efficient interface to ether_input
39702a282acSLuigi Rizzo  * is to have the whole packet (with the ethernet header) into the mbuf:
39802a282acSLuigi Rizzo  * modules which do not need the ethernet header can easily drop it, while
39902a282acSLuigi Rizzo  * others (most noticeably bridge and ng_ether) do not need to do additional
40002a282acSLuigi Rizzo  * work to put the ethernet header back into the mbuf.
40102a282acSLuigi Rizzo  *
402e1e1452dSArchie Cobbs  * First we perform any link layer operations, then continue
403e1e1452dSArchie Cobbs  * to the upper layers with ether_demux().
404df8bae1dSRodney W. Grimes  */
405df8bae1dSRodney W. Grimes void
406df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
407df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
408e1e1452dSArchie Cobbs 	struct ether_header *eh;
409df8bae1dSRodney W. Grimes 	struct mbuf *m;
410df8bae1dSRodney W. Grimes {
411507b4b54SLuigi Rizzo 	struct ether_header save_eh;
412df8bae1dSRodney W. Grimes 
4132e2de7f2SArchie Cobbs 	/* Check for a BPF tap */
4142e2de7f2SArchie Cobbs 	if (ifp->if_bpf != NULL) {
4152e2de7f2SArchie Cobbs 		struct m_hdr mh;
4162e2de7f2SArchie Cobbs 
4172e2de7f2SArchie Cobbs 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
4182e2de7f2SArchie Cobbs 		mh.mh_next = m;
4192e2de7f2SArchie Cobbs 		mh.mh_data = (char *)eh;
4202e2de7f2SArchie Cobbs 		mh.mh_len = ETHER_HDR_LEN;
4212e2de7f2SArchie Cobbs 		bpf_mtap(ifp, (struct mbuf *)&mh);
4222e2de7f2SArchie Cobbs 	}
4232e2de7f2SArchie Cobbs 
424e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
425e1e1452dSArchie Cobbs 	if (ng_ether_input_p != NULL) {
426e1e1452dSArchie Cobbs 		(*ng_ether_input_p)(ifp, &m, eh);
427e1e1452dSArchie Cobbs 		if (m == NULL)
428e1e1452dSArchie Cobbs 			return;
429e1e1452dSArchie Cobbs 	}
430e1e1452dSArchie Cobbs 
4312e2de7f2SArchie Cobbs 	/* Check for bridging mode */
432db69a05dSPaul Saab 	if (do_bridge && bdg_forward_ptr != NULL && BDG_USED(ifp) ) {
4332e2de7f2SArchie Cobbs 		struct ifnet *bif;
4342e2de7f2SArchie Cobbs 
4352e2de7f2SArchie Cobbs 		/* Check with bridging code */
436db69a05dSPaul Saab 		if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
4372e2de7f2SArchie Cobbs 			m_freem(m);
4382e2de7f2SArchie Cobbs 			return;
4392e2de7f2SArchie Cobbs 		}
440ddacb30fSBosko Milekic 		if (bif != BDG_LOCAL) {
441507b4b54SLuigi Rizzo 			struct mbuf *oldm = m ;
442507b4b54SLuigi Rizzo 
443507b4b54SLuigi Rizzo 			save_eh = *eh ; /* because it might change */
444db69a05dSPaul Saab 			m = bdg_forward_ptr(m, eh, bif);	/* needs forwarding */
445ddacb30fSBosko Milekic 			/*
446db69a05dSPaul Saab 			 * Do not continue if bdg_forward_ptr() processed our
447ddacb30fSBosko Milekic 			 * packet (and cleared the mbuf pointer m) or if
448ddacb30fSBosko Milekic 			 * it dropped (m_free'd) the packet itself.
449ddacb30fSBosko Milekic 			 */
450507b4b54SLuigi Rizzo 			if (m == NULL) {
451507b4b54SLuigi Rizzo 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
452507b4b54SLuigi Rizzo 				printf("bdg_forward drop MULTICAST PKT\n");
453ddacb30fSBosko Milekic 			    return;
454ddacb30fSBosko Milekic 			}
455507b4b54SLuigi Rizzo 			if (m != oldm) /* m changed! */
456507b4b54SLuigi Rizzo 			    eh = &save_eh ;
457507b4b54SLuigi Rizzo 		}
4582e2de7f2SArchie Cobbs 		if (bif == BDG_LOCAL
4592e2de7f2SArchie Cobbs 		    || bif == BDG_BCAST
4602e2de7f2SArchie Cobbs 		    || bif == BDG_MCAST)
4612e2de7f2SArchie Cobbs 			goto recvLocal;			/* receive locally */
4622e2de7f2SArchie Cobbs 
4632e2de7f2SArchie Cobbs 		/* If not local and not multicast, just drop it */
4642e2de7f2SArchie Cobbs 		if (m != NULL)
4652e2de7f2SArchie Cobbs 		    m_freem(m);
4662e2de7f2SArchie Cobbs 		return;
4672e2de7f2SArchie Cobbs        }
4682e2de7f2SArchie Cobbs 
4692e2de7f2SArchie Cobbs recvLocal:
470e1e1452dSArchie Cobbs 	/* Continue with upper layer processing */
471e1e1452dSArchie Cobbs 	ether_demux(ifp, eh, m);
47210b1fde0SMark Murray 	/* First chunk of an mbuf contains good junk */
47310b1fde0SMark Murray 	if (harvest.ethernet)
47410b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
475e1e1452dSArchie Cobbs }
476e1e1452dSArchie Cobbs 
477e1e1452dSArchie Cobbs /*
478e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
479e1e1452dSArchie Cobbs  */
480e1e1452dSArchie Cobbs void
481e1e1452dSArchie Cobbs ether_demux(ifp, eh, m)
482e1e1452dSArchie Cobbs 	struct ifnet *ifp;
483e1e1452dSArchie Cobbs 	struct ether_header *eh;
484e1e1452dSArchie Cobbs 	struct mbuf *m;
485e1e1452dSArchie Cobbs {
486e1e1452dSArchie Cobbs 	struct ifqueue *inq;
487e1e1452dSArchie Cobbs 	u_short ether_type;
488e1e1452dSArchie Cobbs #if defined(NETATALK)
489e1e1452dSArchie Cobbs 	register struct llc *l;
490e1e1452dSArchie Cobbs #endif
491e1e1452dSArchie Cobbs 
492cb24f323SArchie Cobbs 	/* Discard packet if upper layers shouldn't see it because it was
493cb24f323SArchie Cobbs 	   unicast to a different Ethernet address. If the driver is working
494cb24f323SArchie Cobbs 	   properly, then this situation can only happen when the interface
495cb24f323SArchie Cobbs 	   is in promiscuous mode. */
496cb24f323SArchie Cobbs 	if ((ifp->if_flags & IFF_PROMISC) != 0
497cb24f323SArchie Cobbs 	    && (eh->ether_dhost[0] & 1) == 0
498cb24f323SArchie Cobbs 	    && bcmp(eh->ether_dhost,
499cb24f323SArchie Cobbs 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
500cb24f323SArchie Cobbs 		m_freem(m);
501cb24f323SArchie Cobbs 		return;
502cb24f323SArchie Cobbs 	}
503cb24f323SArchie Cobbs 
5042e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
505df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
506df8bae1dSRodney W. Grimes 		m_freem(m);
507df8bae1dSRodney W. Grimes 		return;
508df8bae1dSRodney W. Grimes 	}
509df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
5102cc2df49SGarrett Wollman 	if (eh->ether_dhost[0] & 1) {
511df8bae1dSRodney W. Grimes 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
512df8bae1dSRodney W. Grimes 			 sizeof(etherbroadcastaddr)) == 0)
513df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
5142cc2df49SGarrett Wollman 		else
515df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
5162cc2df49SGarrett Wollman 	}
517df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
518df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
519df8bae1dSRodney W. Grimes 
520307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
521307d80beSDavid Greenman 
522307d80beSDavid Greenman 	switch (ether_type) {
523df8bae1dSRodney W. Grimes #ifdef INET
524df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
5251f91d8c5SDavid Greenman 		if (ipflow_fastforward(m))
5261f91d8c5SDavid Greenman 			return;
527df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
528df8bae1dSRodney W. Grimes 		inq = &ipintrq;
529df8bae1dSRodney W. Grimes 		break;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
53208aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
53308aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
53408aadfbbSJonathan Lemon 			m_freem(m);
53508aadfbbSJonathan Lemon 			return;
53608aadfbbSJonathan Lemon 		}
537df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
538df8bae1dSRodney W. Grimes 		inq = &arpintrq;
539df8bae1dSRodney W. Grimes 		break;
540df8bae1dSRodney W. Grimes #endif
541cc6a66f2SJulian Elischer #ifdef IPX
542cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
5434f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5444f93599fSBoris Popov 			return;
545cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
546cc6a66f2SJulian Elischer 		inq = &ipxintrq;
547cc6a66f2SJulian Elischer 		break;
548cc6a66f2SJulian Elischer #endif
54982cd038dSYoshinobu Inoue #ifdef INET6
55082cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
55182cd038dSYoshinobu Inoue 		schednetisr(NETISR_IPV6);
55282cd038dSYoshinobu Inoue 		inq = &ip6intrq;
55382cd038dSYoshinobu Inoue 		break;
55482cd038dSYoshinobu Inoue #endif
555df8bae1dSRodney W. Grimes #ifdef NS
55688e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
557df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
558df8bae1dSRodney W. Grimes 		inq = &nsintrq;
559df8bae1dSRodney W. Grimes 		break;
56088e038feSJordan K. Hubbard 
56188e038feSJordan K. Hubbard #endif /* NS */
562655929bfSJulian Elischer #ifdef NETATALK
563655929bfSJulian Elischer         case ETHERTYPE_AT:
564655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
565655929bfSJulian Elischer                 inq = &atintrq1;
566655929bfSJulian Elischer                 break;
567655929bfSJulian Elischer         case ETHERTYPE_AARP:
568655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
5694c8e8c05SJulian Elischer                 aarpinput(IFP2AC(ifp), m); /* XXX */
570655929bfSJulian Elischer                 return;
5718cdfefbdSPeter Wemm #endif /* NETATALK */
5729d4fe4b2SBrooks Davis 	case ETHERTYPE_VLAN:
5739d4fe4b2SBrooks Davis 		VLAN_INPUT(ifp, eh, m);
5749d4fe4b2SBrooks Davis 		return;
575df8bae1dSRodney W. Grimes 	default:
5764f93599fSBoris Popov #ifdef IPX
5774f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5784f93599fSBoris Popov 			return;
5794f93599fSBoris Popov #endif /* IPX */
58088e038feSJordan K. Hubbard #ifdef NS
58188e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
58288e038feSJordan K. Hubbard 		/* Novell 802.3 */
58388e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
58488e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
58588e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
58688e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
58788e038feSJordan K. Hubbard 				m->m_len -= 3;
58888e038feSJordan K. Hubbard 				m->m_data += 3;
58988e038feSJordan K. Hubbard 			}
59088e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
59188e038feSJordan K. Hubbard 				inq = &nsintrq;
59288e038feSJordan K. Hubbard 				break;
59388e038feSJordan K. Hubbard 		}
59488e038feSJordan K. Hubbard #endif /* NS */
595242c5536SPeter Wemm #if defined(NETATALK)
596307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
597df8bae1dSRodney W. Grimes 			goto dropanyway;
598df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
599df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
600655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
601655929bfSJulian Elischer 		    switch (l->llc_control) {
602655929bfSJulian Elischer 		    case LLC_UI:
603655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
604655929bfSJulian Elischer 			    goto dropanyway;
605655929bfSJulian Elischer 
606655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
607655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
608655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
609655929bfSJulian Elischer 			    inq = &atintrq2;
610655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
611655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
612655929bfSJulian Elischer 			    break;
613655929bfSJulian Elischer 			}
614655929bfSJulian Elischer 
615655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
616655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
617655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
618655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
6194c8e8c05SJulian Elischer 			    aarpinput(IFP2AC(ifp), m); /* XXX */
620655929bfSJulian Elischer 			    return;
621655929bfSJulian Elischer 			}
622655929bfSJulian Elischer 
623655929bfSJulian Elischer 		    default:
624655929bfSJulian Elischer 			goto dropanyway;
625655929bfSJulian Elischer 		    }
626655929bfSJulian Elischer 		    break;
627df8bae1dSRodney W. Grimes 		dropanyway:
628df8bae1dSRodney W. Grimes 		default:
629e1e1452dSArchie Cobbs 			if (ng_ether_input_orphan_p != NULL)
630e1e1452dSArchie Cobbs 				(*ng_ether_input_orphan_p)(ifp, m, eh);
631e1e1452dSArchie Cobbs 			else
632df8bae1dSRodney W. Grimes 				m_freem(m);
633df8bae1dSRodney W. Grimes 			return;
634df8bae1dSRodney W. Grimes 		}
635242c5536SPeter Wemm #else /* NETATALK */
636e1e1452dSArchie Cobbs 		if (ng_ether_input_orphan_p != NULL)
637e1e1452dSArchie Cobbs 			(*ng_ether_input_orphan_p)(ifp, m, eh);
638e1e1452dSArchie Cobbs 		else
639df8bae1dSRodney W. Grimes 			m_freem(m);
640df8bae1dSRodney W. Grimes 		return;
641242c5536SPeter Wemm #endif /* NETATALK */
642df8bae1dSRodney W. Grimes 	}
643df8bae1dSRodney W. Grimes 
644df5e1987SJonathan Lemon 	(void) IF_HANDOFF(inq, m, NULL);
645df8bae1dSRodney W. Grimes }
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes /*
648df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
649df8bae1dSRodney W. Grimes  */
650df8bae1dSRodney W. Grimes void
65121b8ebd9SArchie Cobbs ether_ifattach(ifp, bpf)
652df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
65321b8ebd9SArchie Cobbs 	int bpf;
654df8bae1dSRodney W. Grimes {
655df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
656df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
659df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
660df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
661cfeff1b6SJonathan Lemon 	if_attach(ifp);
662df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
6631158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
664a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
665a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
666322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
667f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
66821b8ebd9SArchie Cobbs 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
66959562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
670df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
671df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
6724c8e8c05SJulian Elischer 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
67321b8ebd9SArchie Cobbs 	if (bpf)
67421b8ebd9SArchie Cobbs 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
675e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
676e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
677db69a05dSPaul Saab 	if (bdgtakeifaces_ptr != NULL)
678db69a05dSPaul Saab 		bdgtakeifaces_ptr();
679e1e1452dSArchie Cobbs }
680e1e1452dSArchie Cobbs 
68121b8ebd9SArchie Cobbs /*
68221b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
68321b8ebd9SArchie Cobbs  */
68421b8ebd9SArchie Cobbs void
68521b8ebd9SArchie Cobbs ether_ifdetach(ifp, bpf)
68621b8ebd9SArchie Cobbs 	struct ifnet *ifp;
68721b8ebd9SArchie Cobbs 	int bpf;
68821b8ebd9SArchie Cobbs {
68921b8ebd9SArchie Cobbs 	if (ng_ether_detach_p != NULL)
69021b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
69121b8ebd9SArchie Cobbs 	if (bpf)
69221b8ebd9SArchie Cobbs 		bpfdetach(ifp);
69321b8ebd9SArchie Cobbs 	if_detach(ifp);
694db69a05dSPaul Saab 	if (bdgtakeifaces_ptr != NULL)
695db69a05dSPaul Saab 		bdgtakeifaces_ptr();
69621b8ebd9SArchie Cobbs }
69721b8ebd9SArchie Cobbs 
698ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
699602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
70030106f6aSPoul-Henning Kamp 
701fb583156SDavid Greenman int
702c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
703c5a1016bSBruce Evans 	struct ifnet *ifp;
704c5a1016bSBruce Evans 	int command;
705c5a1016bSBruce Evans 	caddr_t data;
70630106f6aSPoul-Henning Kamp {
70730106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
70830106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
709fb583156SDavid Greenman 	int error = 0;
71030106f6aSPoul-Henning Kamp 
71130106f6aSPoul-Henning Kamp 	switch (command) {
71230106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
71330106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
71430106f6aSPoul-Henning Kamp 
71530106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
71630106f6aSPoul-Henning Kamp #ifdef INET
71730106f6aSPoul-Henning Kamp 		case AF_INET:
71830106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
719322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
72030106f6aSPoul-Henning Kamp 			break;
72130106f6aSPoul-Henning Kamp #endif
72230106f6aSPoul-Henning Kamp #ifdef IPX
72330106f6aSPoul-Henning Kamp 		/*
72430106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
72530106f6aSPoul-Henning Kamp 		 */
72630106f6aSPoul-Henning Kamp 		case AF_IPX:
72730106f6aSPoul-Henning Kamp 			{
72830106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
7294c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
73030106f6aSPoul-Henning Kamp 
73130106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
73230106f6aSPoul-Henning Kamp 				ina->x_host =
73386101139SPoul-Henning Kamp 				    *(union ipx_host *)
73486101139SPoul-Henning Kamp 			            ac->ac_enaddr;
73530106f6aSPoul-Henning Kamp 			else {
73630106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
73786101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
73886101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
73930106f6aSPoul-Henning Kamp 			}
74030106f6aSPoul-Henning Kamp 
74130106f6aSPoul-Henning Kamp 			/*
74230106f6aSPoul-Henning Kamp 			 * Set new address
74330106f6aSPoul-Henning Kamp 			 */
74430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
74530106f6aSPoul-Henning Kamp 			break;
74630106f6aSPoul-Henning Kamp 			}
74730106f6aSPoul-Henning Kamp #endif
74830106f6aSPoul-Henning Kamp #ifdef NS
74930106f6aSPoul-Henning Kamp 		/*
75030106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
75130106f6aSPoul-Henning Kamp 		 */
75230106f6aSPoul-Henning Kamp 		case AF_NS:
75330106f6aSPoul-Henning Kamp 		{
75430106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
7554c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
75630106f6aSPoul-Henning Kamp 
75730106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
75830106f6aSPoul-Henning Kamp 				ina->x_host =
75986101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
76030106f6aSPoul-Henning Kamp 			else {
76130106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
76286101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
76386101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
76430106f6aSPoul-Henning Kamp 			}
76530106f6aSPoul-Henning Kamp 
76630106f6aSPoul-Henning Kamp 			/*
76730106f6aSPoul-Henning Kamp 			 * Set new address
76830106f6aSPoul-Henning Kamp 			 */
76930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
77030106f6aSPoul-Henning Kamp 			break;
77130106f6aSPoul-Henning Kamp 		}
77230106f6aSPoul-Henning Kamp #endif
77330106f6aSPoul-Henning Kamp 		default:
77430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
77530106f6aSPoul-Henning Kamp 			break;
77630106f6aSPoul-Henning Kamp 		}
77730106f6aSPoul-Henning Kamp 		break;
77830106f6aSPoul-Henning Kamp 
77930106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
78030106f6aSPoul-Henning Kamp 		{
78130106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
78230106f6aSPoul-Henning Kamp 
78330106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
7844c8e8c05SJulian Elischer 			bcopy(IFP2AC(ifp)->ac_enaddr,
78530106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
78630106f6aSPoul-Henning Kamp 		}
78730106f6aSPoul-Henning Kamp 		break;
788fb583156SDavid Greenman 
789fb583156SDavid Greenman 	case SIOCSIFMTU:
790fb583156SDavid Greenman 		/*
791fb583156SDavid Greenman 		 * Set the interface MTU.
792fb583156SDavid Greenman 		 */
793fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
794fb583156SDavid Greenman 			error = EINVAL;
795fb583156SDavid Greenman 		} else {
796fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
79730106f6aSPoul-Henning Kamp 		}
798fb583156SDavid Greenman 		break;
799fb583156SDavid Greenman 	}
800fb583156SDavid Greenman 	return (error);
80130106f6aSPoul-Henning Kamp }
8021158dfb7SGarrett Wollman 
8031158dfb7SGarrett Wollman int
8041158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
8051158dfb7SGarrett Wollman 	struct ifnet *ifp;
8061158dfb7SGarrett Wollman 	struct sockaddr **llsa;
8071158dfb7SGarrett Wollman 	struct sockaddr *sa;
8081158dfb7SGarrett Wollman {
8091158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
8101158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
81182cd038dSYoshinobu Inoue #ifdef INET6
81282cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
81382cd038dSYoshinobu Inoue #endif
8141158dfb7SGarrett Wollman 	u_char *e_addr;
8151158dfb7SGarrett Wollman 
8161158dfb7SGarrett Wollman 	switch(sa->sa_family) {
8171158dfb7SGarrett Wollman 	case AF_LINK:
8187f33a738SJulian Elischer 		/*
8197f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
8207f33a738SJulian Elischer 		 */
8211158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
8221158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8231158dfb7SGarrett Wollman 		if ((e_addr[0] & 1) != 1)
8241158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8251158dfb7SGarrett Wollman 		*llsa = 0;
8261158dfb7SGarrett Wollman 		return 0;
8271158dfb7SGarrett Wollman 
8281158dfb7SGarrett Wollman #ifdef INET
8291158dfb7SGarrett Wollman 	case AF_INET:
8301158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
8311158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
8321158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8331158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
83426e30963SBill Fenner 		       M_WAITOK|M_ZERO);
8351158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
8361158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
8371158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
8381158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
8391158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
8401158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8411158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
8421158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
8431158dfb7SGarrett Wollman 		return 0;
8441158dfb7SGarrett Wollman #endif
84582cd038dSYoshinobu Inoue #ifdef INET6
84682cd038dSYoshinobu Inoue 	case AF_INET6:
84782cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
848595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
849595b8a1cSJun-ichiro itojun Hagino 			/*
850595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
851595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
852595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
853595b8a1cSJun-ichiro itojun Hagino 			 */
854595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
855595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
856595b8a1cSJun-ichiro itojun Hagino 			return 0;
857595b8a1cSJun-ichiro itojun Hagino 		}
85882cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
85982cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
86082cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
86126e30963SBill Fenner 		       M_WAITOK|M_ZERO);
86282cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
86382cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
86482cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
86582cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
86682cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
86782cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
86882cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
86982cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
87082cd038dSYoshinobu Inoue 		return 0;
87182cd038dSYoshinobu Inoue #endif
8721158dfb7SGarrett Wollman 
8731158dfb7SGarrett Wollman 	default:
8741158dfb7SGarrett Wollman 		/*
8751158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
8761158dfb7SGarrett Wollman 		 * that counts...
8771158dfb7SGarrett Wollman 		 */
8781158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
8791158dfb7SGarrett Wollman 	}
8801158dfb7SGarrett Wollman }
8814cf49a43SJulian Elischer 
882