xref: /freebsd/sys/net/if_ethersubr.c (revision fef5fd231531bd66a8b09083b1f387c93f43d39a)
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>
62df8bae1dSRodney W. Grimes 
6382cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
671d5e9e22SEivind Eklund #endif
6882cd038dSYoshinobu Inoue #ifdef INET6
6982cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7082cd038dSYoshinobu Inoue #endif
71df8bae1dSRodney W. Grimes 
72cc6a66f2SJulian Elischer #ifdef IPX
73cc6a66f2SJulian Elischer #include <netipx/ipx.h>
74cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
754f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
765accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
772a7e8eceSBoris Popov 		struct sockaddr *dst, short *tp, int *hlen);
78cc6a66f2SJulian Elischer #endif
79cc6a66f2SJulian Elischer 
80df8bae1dSRodney W. Grimes #ifdef NS
81df8bae1dSRodney W. Grimes #include <netns/ns.h>
82df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
83d0ec898dSJordan K. Hubbard ushort ns_nettype;
8488e038feSJordan K. Hubbard int ether_outputdebug = 0;
8588e038feSJordan K. Hubbard int ether_inputdebug = 0;
86df8bae1dSRodney W. Grimes #endif
87df8bae1dSRodney W. Grimes 
88655929bfSJulian Elischer #ifdef NETATALK
89655929bfSJulian Elischer #include <netatalk/at.h>
90655929bfSJulian Elischer #include <netatalk/at_var.h>
91655929bfSJulian Elischer #include <netatalk/at_extern.h>
92655929bfSJulian Elischer 
93655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
94655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
95655929bfSJulian Elischer 
96655929bfSJulian Elischer extern u_char	at_org_code[3];
97655929bfSJulian Elischer extern u_char	aarp_org_code[3];
982cc2df49SGarrett Wollman #endif /* NETATALK */
992cc2df49SGarrett Wollman 
100fb5fbe46SLuigi Rizzo #ifdef BRIDGE
101fb5fbe46SLuigi Rizzo #include <net/bridge.h>
102fb5fbe46SLuigi Rizzo #endif
103fb5fbe46SLuigi Rizzo 
1042cc2df49SGarrett Wollman #include "vlan.h"
1052cc2df49SGarrett Wollman #if NVLAN > 0
1062cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
1072cc2df49SGarrett Wollman #endif /* NVLAN > 0 */
108655929bfSJulian Elischer 
109e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
110e1e1452dSArchie Cobbs void	(*ng_ether_input_p)(struct ifnet *ifp,
111e1e1452dSArchie Cobbs 		struct mbuf **mp, struct ether_header *eh);
112e1e1452dSArchie Cobbs void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
113e1e1452dSArchie Cobbs 		struct mbuf *m, struct ether_header *eh);
114e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
115e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
116e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
117e1e1452dSArchie Cobbs 
1181158dfb7SGarrett Wollman static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
1191158dfb7SGarrett Wollman 				    struct sockaddr *));
120df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1214c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
1224c8e8c05SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP)
123df8bae1dSRodney W. Grimes 
124df8bae1dSRodney W. Grimes /*
125df8bae1dSRodney W. Grimes  * Ethernet output routine.
126df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
127df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
128df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
129df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
130df8bae1dSRodney W. Grimes  */
131df8bae1dSRodney W. Grimes int
132d25f3712SBrian Feldman ether_output(ifp, m, dst, rt0)
133df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
134d25f3712SBrian Feldman 	struct mbuf *m;
135df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
136df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
137df8bae1dSRodney W. Grimes {
138df8bae1dSRodney W. Grimes 	short type;
139e1e1452dSArchie Cobbs 	int error = 0, hdrcmplt = 0;
140114ae644SMike Smith  	u_char esrc[6], edst[6];
141df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
142df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
143e1e1452dSArchie Cobbs 	int off, loop_copy = 0;
14484dd0fd0SJulian Elischer 	int hlen;	/* link layer header lenght */
1454c8e8c05SJulian Elischer 	struct arpcom *ac = IFP2AC(ifp);
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
148df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1493bda9f9bSPoul-Henning Kamp 	rt = rt0;
1503bda9f9bSPoul-Henning Kamp 	if (rt) {
151df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1523bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1533bda9f9bSPoul-Henning Kamp 			if (rt0)
154df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
155df8bae1dSRodney W. Grimes 			else
156df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
157df8bae1dSRodney W. Grimes 		}
158df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
159df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
160df8bae1dSRodney W. Grimes 				goto lookup;
161df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
162df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
163995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
164995add1aSGarrett Wollman 							  0UL);
165df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
166df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
167df8bae1dSRodney W. Grimes 			}
168df8bae1dSRodney W. Grimes 		}
169df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
170df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
171227ee8a1SPoul-Henning Kamp 			    time_second < rt->rt_rmx.rmx_expire)
172df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
173df8bae1dSRodney W. Grimes 	}
17484dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
175df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
176df8bae1dSRodney W. Grimes #ifdef INET
177df8bae1dSRodney W. Grimes 	case AF_INET:
1785df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
179df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
180df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
18134bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
182df8bae1dSRodney W. Grimes 		break;
183df8bae1dSRodney W. Grimes #endif
18482cd038dSYoshinobu Inoue #ifdef INET6
18582cd038dSYoshinobu Inoue 	case AF_INET6:
18682cd038dSYoshinobu Inoue 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
187fef5fd23SBosko Milekic 			/* Something bad happened */
18882cd038dSYoshinobu Inoue 			return(0);
18982cd038dSYoshinobu Inoue 		}
19082cd038dSYoshinobu Inoue 		off = m->m_pkthdr.len - m->m_len;
19182cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
19282cd038dSYoshinobu Inoue 		break;
19382cd038dSYoshinobu Inoue #endif
194cc6a66f2SJulian Elischer #ifdef IPX
195cc6a66f2SJulian Elischer 	case AF_IPX:
1964f93599fSBoris Popov 		if (ef_outputp) {
1972a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
1985accfb8cSBoris Popov 		    if (error)
1995accfb8cSBoris Popov 			goto bad;
2004f93599fSBoris Popov 		} else
20134bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
202cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
203cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
204cc6a66f2SJulian Elischer 		break;
205cc6a66f2SJulian Elischer #endif
206655929bfSJulian Elischer #ifdef NETATALK
207655929bfSJulian Elischer 	case AF_APPLETALK:
2081d0eab59SJulian Elischer 	  {
209ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2101d0eab59SJulian Elischer 
211ed7509acSJulian Elischer 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
2121d0eab59SJulian Elischer 		    goto bad;
2131d0eab59SJulian Elischer 	    }
214ed7509acSJulian Elischer 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
2151d0eab59SJulian Elischer 		    return (0);
216655929bfSJulian Elischer 	    /*
217ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
218655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
219655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
220655929bfSJulian Elischer 	     */
221655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
222655929bfSJulian Elischer 		struct llc llc;
223655929bfSJulian Elischer 
2242a0c503eSBosko Milekic 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
225655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
226655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
227655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
228655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
229655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
23034bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
23184dd0fd0SJulian Elischer 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
232655929bfSJulian Elischer 	    } else {
23334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
234655929bfSJulian Elischer 	    }
235655929bfSJulian Elischer 	    break;
236ed7509acSJulian Elischer 	  }
237655929bfSJulian Elischer #endif NETATALK
238df8bae1dSRodney W. Grimes #ifdef NS
239df8bae1dSRodney W. Grimes 	case AF_NS:
24088e038feSJordan K. Hubbard 		switch(ns_nettype){
24188e038feSJordan K. Hubbard 		default:
24288e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
24388e038feSJordan K. Hubbard 			type = 0x8137;
24488e038feSJordan K. Hubbard 			break;
24588e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
24688e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
24788e038feSJordan K. Hubbard 			break;
24888e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
2492a0c503eSBosko Milekic 			M_PREPEND(m, 3, M_TRYWAIT);
25088e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
25188e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
25288e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25388e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25488e038feSJordan K. Hubbard 			*cp++ = 0x03;
25588e038feSJordan K. Hubbard 			break;
25688e038feSJordan K. Hubbard 		}
257df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
258df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
2594c8e8c05SJulian Elischer 		/*
2604c8e8c05SJulian Elischer 		 * XXX if ns_thishost is the same as the node's ethernet
2614c8e8c05SJulian Elischer 		 * address then just the default code will catch this anyhow.
2624c8e8c05SJulian Elischer 		 * So I'm not sure if this next clause should be here at all?
2634c8e8c05SJulian Elischer 		 * [JRE]
2644c8e8c05SJulian Elischer 		 */
26588e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
26688e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
26788e038feSJordan K. Hubbard 			inq = &nsintrq;
268df5e1987SJonathan Lemon 			if (IF_HANDOFF(inq, m, NULL))
269df5e1987SJonathan Lemon 				schednetisr(NETISR_NS);
27088e038feSJordan K. Hubbard 			return (error);
27188e038feSJordan K. Hubbard 		}
27288e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
2734c8e8c05SJulian Elischer 			m->m_flags |= M_BCAST;
27488e038feSJordan K. Hubbard 		}
275df8bae1dSRodney W. Grimes 		break;
27688e038feSJordan K. Hubbard #endif /* NS */
277df8bae1dSRodney W. Grimes 
278114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
279114ae644SMike Smith 		hdrcmplt = 1;
280114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
281114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
282114ae644SMike Smith 		/* FALLTHROUGH */
283114ae644SMike Smith 
284df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
2859d3f194dSJulian Elischer 		loop_copy = -1; /* if this is for us, don't do it */
286df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
28794a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
288df8bae1dSRodney W. Grimes 		type = eh->ether_type;
289df8bae1dSRodney W. Grimes 		break;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	default:
292df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
293df8bae1dSRodney W. Grimes 			dst->sa_family);
294df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
295df8bae1dSRodney W. Grimes 	}
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	/*
298df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
299df8bae1dSRodney W. Grimes 	 * allocate another.
300df8bae1dSRodney W. Grimes 	 */
301df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
302df8bae1dSRodney W. Grimes 	if (m == 0)
303df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
304df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
30594a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
306df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
30794a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
308114ae644SMike Smith 	if (hdrcmplt)
309114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
310114ae644SMike Smith 			sizeof(eh->ether_shost));
311114ae644SMike Smith 	else
31294a5d9b6SDavid Greenman 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
313df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
314ed7509acSJulian Elischer 
315ed7509acSJulian Elischer 	/*
316ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
317ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
318ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
319ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
320ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
321ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
322ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
323ed7509acSJulian Elischer 	 */
3244c8e8c05SJulian Elischer 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
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 
32806a429a3SArchie Cobbs 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
329ed7509acSJulian Elischer 		} else if (bcmp(eh->ether_dhost,
330ed7509acSJulian Elischer 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
33106a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
332ed7509acSJulian Elischer 			return (0);	/* XXX */
333ed7509acSJulian Elischer 		}
334ed7509acSJulian Elischer 	}
3352e2de7f2SArchie Cobbs 
336e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
337e1e1452dSArchie Cobbs 	if (ng_ether_output_p != NULL) {
338e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
339e1e1452dSArchie Cobbs bad:			if (m != NULL)
340e1e1452dSArchie Cobbs 				m_freem(m);
341e1e1452dSArchie Cobbs 			return (error);
342e1e1452dSArchie Cobbs 		}
343e1e1452dSArchie Cobbs 		if (m == NULL)
344e1e1452dSArchie Cobbs 			return (0);
345e1e1452dSArchie Cobbs 	}
346e1e1452dSArchie Cobbs 
347e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
348e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
349e1e1452dSArchie Cobbs }
350e1e1452dSArchie Cobbs 
351e1e1452dSArchie Cobbs /*
352e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
353e1e1452dSArchie Cobbs  *
354e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
355e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
356e1e1452dSArchie Cobbs  */
357e1e1452dSArchie Cobbs int
358e1e1452dSArchie Cobbs ether_output_frame(ifp, m)
359e1e1452dSArchie Cobbs 	struct ifnet *ifp;
360e1e1452dSArchie Cobbs 	struct mbuf *m;
361e1e1452dSArchie Cobbs {
362df5e1987SJonathan Lemon 	int error = 0;
363e1e1452dSArchie Cobbs 
364fb5fbe46SLuigi Rizzo #ifdef BRIDGE
365507b4b54SLuigi Rizzo 	if (do_bridge && BDG_USED(ifp) ) {
366507b4b54SLuigi Rizzo 		struct ether_header *eh; /* a ptr suffices */
3671db59ce6SEivind Eklund 
368fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
369507b4b54SLuigi Rizzo 		eh = mtod(m, struct ether_header *);
3702e2de7f2SArchie Cobbs 		m_adj(m, ETHER_HDR_LEN);
371507b4b54SLuigi Rizzo 		m = bdg_forward(m, eh, ifp);
3722e2de7f2SArchie Cobbs 		if (m != NULL)
3732e2de7f2SArchie Cobbs 			m_freem(m);
3741db59ce6SEivind Eklund 		return (0);
375fb5fbe46SLuigi Rizzo 	}
376fb5fbe46SLuigi Rizzo #endif
3772e2de7f2SArchie Cobbs 
378df8bae1dSRodney W. Grimes 	/*
379df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
380df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
381df8bae1dSRodney W. Grimes 	 */
382df5e1987SJonathan Lemon 	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
383e1e1452dSArchie Cobbs 		return (ENOBUFS);
384df8bae1dSRodney W. Grimes 	return (error);
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes /*
388df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
389df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
390df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
391e1e1452dSArchie Cobbs  *
39202a282acSLuigi Rizzo  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
39302a282acSLuigi Rizzo  * cluster, right before m_data. So be very careful when working on m,
39402a282acSLuigi Rizzo  * as you could destroy *eh !!
39502a282acSLuigi Rizzo  * A (probably) more convenient and efficient interface to ether_input
39602a282acSLuigi Rizzo  * is to have the whole packet (with the ethernet header) into the mbuf:
39702a282acSLuigi Rizzo  * modules which do not need the ethernet header can easily drop it, while
39802a282acSLuigi Rizzo  * others (most noticeably bridge and ng_ether) do not need to do additional
39902a282acSLuigi Rizzo  * work to put the ethernet header back into the mbuf.
40002a282acSLuigi Rizzo  *
401e1e1452dSArchie Cobbs  * First we perform any link layer operations, then continue
402e1e1452dSArchie Cobbs  * to the upper layers with ether_demux().
403df8bae1dSRodney W. Grimes  */
404df8bae1dSRodney W. Grimes void
405df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
406df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
407e1e1452dSArchie Cobbs 	struct ether_header *eh;
408df8bae1dSRodney W. Grimes 	struct mbuf *m;
409df8bae1dSRodney W. Grimes {
410cb24f323SArchie Cobbs #ifdef BRIDGE
411507b4b54SLuigi Rizzo 	struct ether_header save_eh;
412cb24f323SArchie Cobbs #endif
413df8bae1dSRodney W. Grimes 
4142e2de7f2SArchie Cobbs 	/* Check for a BPF tap */
4152e2de7f2SArchie Cobbs 	if (ifp->if_bpf != NULL) {
4162e2de7f2SArchie Cobbs 		struct m_hdr mh;
4172e2de7f2SArchie Cobbs 
4182e2de7f2SArchie Cobbs 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
4192e2de7f2SArchie Cobbs 		mh.mh_next = m;
4202e2de7f2SArchie Cobbs 		mh.mh_data = (char *)eh;
4212e2de7f2SArchie Cobbs 		mh.mh_len = ETHER_HDR_LEN;
4222e2de7f2SArchie Cobbs 		bpf_mtap(ifp, (struct mbuf *)&mh);
4232e2de7f2SArchie Cobbs 	}
4242e2de7f2SArchie Cobbs 
425e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
426e1e1452dSArchie Cobbs 	if (ng_ether_input_p != NULL) {
427e1e1452dSArchie Cobbs 		(*ng_ether_input_p)(ifp, &m, eh);
428e1e1452dSArchie Cobbs 		if (m == NULL)
429e1e1452dSArchie Cobbs 			return;
430e1e1452dSArchie Cobbs 	}
431e1e1452dSArchie Cobbs 
4322e2de7f2SArchie Cobbs #ifdef BRIDGE
4332e2de7f2SArchie Cobbs 	/* Check for bridging mode */
434507b4b54SLuigi Rizzo 	if (do_bridge && BDG_USED(ifp) ) {
4352e2de7f2SArchie Cobbs 		struct ifnet *bif;
4362e2de7f2SArchie Cobbs 
4372e2de7f2SArchie Cobbs 		/* Check with bridging code */
4382e2de7f2SArchie Cobbs 		if ((bif = bridge_in(ifp, eh)) == BDG_DROP) {
4392e2de7f2SArchie Cobbs 			m_freem(m);
4402e2de7f2SArchie Cobbs 			return;
4412e2de7f2SArchie Cobbs 		}
442ddacb30fSBosko Milekic 		if (bif != BDG_LOCAL) {
443507b4b54SLuigi Rizzo 			struct mbuf *oldm = m ;
444507b4b54SLuigi Rizzo 
445507b4b54SLuigi Rizzo 			save_eh = *eh ; /* because it might change */
4467e1cd0d2SLuigi Rizzo 			m = bdg_forward(m, eh, bif);	/* needs forwarding */
447ddacb30fSBosko Milekic 			/*
448ddacb30fSBosko Milekic 			 * Do not continue if bdg_forward() processed our
449ddacb30fSBosko Milekic 			 * packet (and cleared the mbuf pointer m) or if
450ddacb30fSBosko Milekic 			 * it dropped (m_free'd) the packet itself.
451ddacb30fSBosko Milekic 			 */
452507b4b54SLuigi Rizzo 			if (m == NULL) {
453507b4b54SLuigi Rizzo 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
454507b4b54SLuigi Rizzo 				printf("bdg_forward drop MULTICAST PKT\n");
455ddacb30fSBosko Milekic 			    return;
456ddacb30fSBosko Milekic 			}
457507b4b54SLuigi Rizzo 			if (m != oldm) /* m changed! */
458507b4b54SLuigi Rizzo 			    eh = &save_eh ;
459507b4b54SLuigi Rizzo 		}
4602e2de7f2SArchie Cobbs 		if (bif == BDG_LOCAL
4612e2de7f2SArchie Cobbs 		    || bif == BDG_BCAST
4622e2de7f2SArchie Cobbs 		    || bif == BDG_MCAST)
4632e2de7f2SArchie Cobbs 			goto recvLocal;			/* receive locally */
4642e2de7f2SArchie Cobbs 
4652e2de7f2SArchie Cobbs 		/* If not local and not multicast, just drop it */
4662e2de7f2SArchie Cobbs 		if (m != NULL)
4672e2de7f2SArchie Cobbs 		    m_freem(m);
4682e2de7f2SArchie Cobbs 		return;
4692e2de7f2SArchie Cobbs        }
4702e2de7f2SArchie Cobbs #endif
4712e2de7f2SArchie Cobbs 
4722e2de7f2SArchie Cobbs #ifdef BRIDGE
4732e2de7f2SArchie Cobbs recvLocal:
4742e2de7f2SArchie Cobbs #endif
475e1e1452dSArchie Cobbs 	/* Continue with upper layer processing */
476e1e1452dSArchie Cobbs 	ether_demux(ifp, eh, m);
47710b1fde0SMark Murray 	/* First chunk of an mbuf contains good junk */
47810b1fde0SMark Murray 	if (harvest.ethernet)
47910b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
480e1e1452dSArchie Cobbs }
481e1e1452dSArchie Cobbs 
482e1e1452dSArchie Cobbs /*
483e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
484e1e1452dSArchie Cobbs  */
485e1e1452dSArchie Cobbs void
486e1e1452dSArchie Cobbs ether_demux(ifp, eh, m)
487e1e1452dSArchie Cobbs 	struct ifnet *ifp;
488e1e1452dSArchie Cobbs 	struct ether_header *eh;
489e1e1452dSArchie Cobbs 	struct mbuf *m;
490e1e1452dSArchie Cobbs {
491e1e1452dSArchie Cobbs 	struct ifqueue *inq;
492e1e1452dSArchie Cobbs 	u_short ether_type;
493e1e1452dSArchie Cobbs #if defined(NETATALK)
494e1e1452dSArchie Cobbs 	register struct llc *l;
495e1e1452dSArchie Cobbs #endif
496e1e1452dSArchie Cobbs 
497cb24f323SArchie Cobbs 	/* Discard packet if upper layers shouldn't see it because it was
498cb24f323SArchie Cobbs 	   unicast to a different Ethernet address. If the driver is working
499cb24f323SArchie Cobbs 	   properly, then this situation can only happen when the interface
500cb24f323SArchie Cobbs 	   is in promiscuous mode. */
501cb24f323SArchie Cobbs 	if ((ifp->if_flags & IFF_PROMISC) != 0
502cb24f323SArchie Cobbs 	    && (eh->ether_dhost[0] & 1) == 0
503cb24f323SArchie Cobbs 	    && bcmp(eh->ether_dhost,
504cb24f323SArchie Cobbs 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
505cb24f323SArchie Cobbs 		m_freem(m);
506cb24f323SArchie Cobbs 		return;
507cb24f323SArchie Cobbs 	}
508cb24f323SArchie Cobbs 
5092e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
510df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
511df8bae1dSRodney W. Grimes 		m_freem(m);
512df8bae1dSRodney W. Grimes 		return;
513df8bae1dSRodney W. Grimes 	}
514df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
5152cc2df49SGarrett Wollman 	if (eh->ether_dhost[0] & 1) {
516df8bae1dSRodney W. Grimes 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
517df8bae1dSRodney W. Grimes 			 sizeof(etherbroadcastaddr)) == 0)
518df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
5192cc2df49SGarrett Wollman 		else
520df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
5212cc2df49SGarrett Wollman 	}
522df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
523df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
524df8bae1dSRodney W. Grimes 
525307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
526307d80beSDavid Greenman 
5272cc2df49SGarrett Wollman #if NVLAN > 0
5282cc2df49SGarrett Wollman 	if (ether_type == vlan_proto) {
5292cc2df49SGarrett Wollman 		if (vlan_input(eh, m) < 0)
5302cc2df49SGarrett Wollman 			ifp->if_data.ifi_noproto++;
5312cc2df49SGarrett Wollman 		return;
5322cc2df49SGarrett Wollman 	}
5332cc2df49SGarrett Wollman #endif /* NVLAN > 0 */
5342cc2df49SGarrett Wollman 
535307d80beSDavid Greenman 	switch (ether_type) {
536df8bae1dSRodney W. Grimes #ifdef INET
537df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
5381f91d8c5SDavid Greenman 		if (ipflow_fastforward(m))
5391f91d8c5SDavid Greenman 			return;
540df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
541df8bae1dSRodney W. Grimes 		inq = &ipintrq;
542df8bae1dSRodney W. Grimes 		break;
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
545df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
546df8bae1dSRodney W. Grimes 		inq = &arpintrq;
547df8bae1dSRodney W. Grimes 		break;
548df8bae1dSRodney W. Grimes #endif
549cc6a66f2SJulian Elischer #ifdef IPX
550cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
5514f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5524f93599fSBoris Popov 			return;
553cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
554cc6a66f2SJulian Elischer 		inq = &ipxintrq;
555cc6a66f2SJulian Elischer 		break;
556cc6a66f2SJulian Elischer #endif
55782cd038dSYoshinobu Inoue #ifdef INET6
55882cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
55982cd038dSYoshinobu Inoue 		schednetisr(NETISR_IPV6);
56082cd038dSYoshinobu Inoue 		inq = &ip6intrq;
56182cd038dSYoshinobu Inoue 		break;
56282cd038dSYoshinobu Inoue #endif
563df8bae1dSRodney W. Grimes #ifdef NS
56488e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
565df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
566df8bae1dSRodney W. Grimes 		inq = &nsintrq;
567df8bae1dSRodney W. Grimes 		break;
56888e038feSJordan K. Hubbard 
56988e038feSJordan K. Hubbard #endif /* NS */
570655929bfSJulian Elischer #ifdef NETATALK
571655929bfSJulian Elischer         case ETHERTYPE_AT:
572655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
573655929bfSJulian Elischer                 inq = &atintrq1;
574655929bfSJulian Elischer                 break;
575655929bfSJulian Elischer         case ETHERTYPE_AARP:
576655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
5774c8e8c05SJulian Elischer                 aarpinput(IFP2AC(ifp), m); /* XXX */
578655929bfSJulian Elischer                 return;
579655929bfSJulian Elischer #endif NETATALK
580df8bae1dSRodney W. Grimes 	default:
5814f93599fSBoris Popov #ifdef IPX
5824f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5834f93599fSBoris Popov 			return;
5844f93599fSBoris Popov #endif /* IPX */
58588e038feSJordan K. Hubbard #ifdef NS
58688e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
58788e038feSJordan K. Hubbard 		/* Novell 802.3 */
58888e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
58988e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
59088e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
59188e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
59288e038feSJordan K. Hubbard 				m->m_len -= 3;
59388e038feSJordan K. Hubbard 				m->m_data += 3;
59488e038feSJordan K. Hubbard 			}
59588e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
59688e038feSJordan K. Hubbard 				inq = &nsintrq;
59788e038feSJordan K. Hubbard 				break;
59888e038feSJordan K. Hubbard 		}
59988e038feSJordan K. Hubbard #endif /* NS */
600242c5536SPeter Wemm #if defined(NETATALK)
601307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
602df8bae1dSRodney W. Grimes 			goto dropanyway;
603df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
604df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
605655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
606655929bfSJulian Elischer 		    switch (l->llc_control) {
607655929bfSJulian Elischer 		    case LLC_UI:
608655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
609655929bfSJulian Elischer 			    goto dropanyway;
610655929bfSJulian Elischer 
611655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
612655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
613655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
614655929bfSJulian Elischer 			    inq = &atintrq2;
615655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
616655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
617655929bfSJulian Elischer 			    break;
618655929bfSJulian Elischer 			}
619655929bfSJulian Elischer 
620655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
621655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
622655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
623655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
6244c8e8c05SJulian Elischer 			    aarpinput(IFP2AC(ifp), m); /* XXX */
625655929bfSJulian Elischer 			    return;
626655929bfSJulian Elischer 			}
627655929bfSJulian Elischer 
628655929bfSJulian Elischer 		    default:
629655929bfSJulian Elischer 			goto dropanyway;
630655929bfSJulian Elischer 		    }
631655929bfSJulian Elischer 		    break;
632df8bae1dSRodney W. Grimes 		dropanyway:
633df8bae1dSRodney W. Grimes 		default:
634e1e1452dSArchie Cobbs 			if (ng_ether_input_orphan_p != NULL)
635e1e1452dSArchie Cobbs 				(*ng_ether_input_orphan_p)(ifp, m, eh);
636e1e1452dSArchie Cobbs 			else
637df8bae1dSRodney W. Grimes 				m_freem(m);
638df8bae1dSRodney W. Grimes 			return;
639df8bae1dSRodney W. Grimes 		}
640242c5536SPeter Wemm #else /* NETATALK */
641e1e1452dSArchie Cobbs 		if (ng_ether_input_orphan_p != NULL)
642e1e1452dSArchie Cobbs 			(*ng_ether_input_orphan_p)(ifp, m, eh);
643e1e1452dSArchie Cobbs 		else
644df8bae1dSRodney W. Grimes 			m_freem(m);
645df8bae1dSRodney W. Grimes 		return;
646242c5536SPeter Wemm #endif /* NETATALK */
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes 
649df5e1987SJonathan Lemon 	(void) IF_HANDOFF(inq, m, NULL);
650df8bae1dSRodney W. Grimes }
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes /*
653df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
654df8bae1dSRodney W. Grimes  */
655df8bae1dSRodney W. Grimes void
65621b8ebd9SArchie Cobbs ether_ifattach(ifp, bpf)
657df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
65821b8ebd9SArchie Cobbs 	int bpf;
659df8bae1dSRodney W. Grimes {
660df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
661df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
662df8bae1dSRodney W. Grimes 
66321b8ebd9SArchie Cobbs 	if_attach(ifp);
664df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
665df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
666df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
667df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
6681158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
669a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
670a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
67159562606SGarrett Wollman 	ifa = ifnet_addrs[ifp->if_index - 1];
67221b8ebd9SArchie Cobbs 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
67359562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
674df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
675df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
6764c8e8c05SJulian Elischer 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
67721b8ebd9SArchie Cobbs 	if (bpf)
67821b8ebd9SArchie Cobbs 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
679e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
680e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
68182902fa3SNick Sayer #ifdef BRIDGE
68282902fa3SNick Sayer 	bdgtakeifaces();
68382902fa3SNick Sayer #endif
684e1e1452dSArchie Cobbs }
685e1e1452dSArchie Cobbs 
68621b8ebd9SArchie Cobbs /*
68721b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
68821b8ebd9SArchie Cobbs  */
68921b8ebd9SArchie Cobbs void
69021b8ebd9SArchie Cobbs ether_ifdetach(ifp, bpf)
69121b8ebd9SArchie Cobbs 	struct ifnet *ifp;
69221b8ebd9SArchie Cobbs 	int bpf;
69321b8ebd9SArchie Cobbs {
69421b8ebd9SArchie Cobbs 	if (ng_ether_detach_p != NULL)
69521b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
69621b8ebd9SArchie Cobbs 	if (bpf)
69721b8ebd9SArchie Cobbs 		bpfdetach(ifp);
69821b8ebd9SArchie Cobbs 	if_detach(ifp);
69982902fa3SNick Sayer #ifdef BRIDGE
70082902fa3SNick Sayer 	bdgtakeifaces();
70182902fa3SNick Sayer #endif
70221b8ebd9SArchie Cobbs }
70321b8ebd9SArchie Cobbs 
704ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
705602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
70630106f6aSPoul-Henning Kamp 
707fb583156SDavid Greenman int
708c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
709c5a1016bSBruce Evans 	struct ifnet *ifp;
710c5a1016bSBruce Evans 	int command;
711c5a1016bSBruce Evans 	caddr_t data;
71230106f6aSPoul-Henning Kamp {
71330106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
71430106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
715fb583156SDavid Greenman 	int error = 0;
71630106f6aSPoul-Henning Kamp 
71730106f6aSPoul-Henning Kamp 	switch (command) {
71830106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
71930106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
72030106f6aSPoul-Henning Kamp 
72130106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
72230106f6aSPoul-Henning Kamp #ifdef INET
72330106f6aSPoul-Henning Kamp 		case AF_INET:
72430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
7254c8e8c05SJulian Elischer 			arp_ifinit(IFP2AC(ifp), ifa);
72630106f6aSPoul-Henning Kamp 			break;
72730106f6aSPoul-Henning Kamp #endif
72830106f6aSPoul-Henning Kamp #ifdef IPX
72930106f6aSPoul-Henning Kamp 		/*
73030106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
73130106f6aSPoul-Henning Kamp 		 */
73230106f6aSPoul-Henning Kamp 		case AF_IPX:
73330106f6aSPoul-Henning Kamp 			{
73430106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
7354c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
73630106f6aSPoul-Henning Kamp 
73730106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
73830106f6aSPoul-Henning Kamp 				ina->x_host =
73986101139SPoul-Henning Kamp 				    *(union ipx_host *)
74086101139SPoul-Henning Kamp 			            ac->ac_enaddr;
74130106f6aSPoul-Henning Kamp 			else {
74230106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
74386101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
74486101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
74530106f6aSPoul-Henning Kamp 			}
74630106f6aSPoul-Henning Kamp 
74730106f6aSPoul-Henning Kamp 			/*
74830106f6aSPoul-Henning Kamp 			 * Set new address
74930106f6aSPoul-Henning Kamp 			 */
75030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
75130106f6aSPoul-Henning Kamp 			break;
75230106f6aSPoul-Henning Kamp 			}
75330106f6aSPoul-Henning Kamp #endif
75430106f6aSPoul-Henning Kamp #ifdef NS
75530106f6aSPoul-Henning Kamp 		/*
75630106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
75730106f6aSPoul-Henning Kamp 		 */
75830106f6aSPoul-Henning Kamp 		case AF_NS:
75930106f6aSPoul-Henning Kamp 		{
76030106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
7614c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
76230106f6aSPoul-Henning Kamp 
76330106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
76430106f6aSPoul-Henning Kamp 				ina->x_host =
76586101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
76630106f6aSPoul-Henning Kamp 			else {
76730106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
76886101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
76986101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
77030106f6aSPoul-Henning Kamp 			}
77130106f6aSPoul-Henning Kamp 
77230106f6aSPoul-Henning Kamp 			/*
77330106f6aSPoul-Henning Kamp 			 * Set new address
77430106f6aSPoul-Henning Kamp 			 */
77530106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
77630106f6aSPoul-Henning Kamp 			break;
77730106f6aSPoul-Henning Kamp 		}
77830106f6aSPoul-Henning Kamp #endif
77930106f6aSPoul-Henning Kamp 		default:
78030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
78130106f6aSPoul-Henning Kamp 			break;
78230106f6aSPoul-Henning Kamp 		}
78330106f6aSPoul-Henning Kamp 		break;
78430106f6aSPoul-Henning Kamp 
78530106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
78630106f6aSPoul-Henning Kamp 		{
78730106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
78830106f6aSPoul-Henning Kamp 
78930106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
7904c8e8c05SJulian Elischer 			bcopy(IFP2AC(ifp)->ac_enaddr,
79130106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
79230106f6aSPoul-Henning Kamp 		}
79330106f6aSPoul-Henning Kamp 		break;
794fb583156SDavid Greenman 
795fb583156SDavid Greenman 	case SIOCSIFMTU:
796fb583156SDavid Greenman 		/*
797fb583156SDavid Greenman 		 * Set the interface MTU.
798fb583156SDavid Greenman 		 */
799fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
800fb583156SDavid Greenman 			error = EINVAL;
801fb583156SDavid Greenman 		} else {
802fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
80330106f6aSPoul-Henning Kamp 		}
804fb583156SDavid Greenman 		break;
805fb583156SDavid Greenman 	}
806fb583156SDavid Greenman 	return (error);
80730106f6aSPoul-Henning Kamp }
8081158dfb7SGarrett Wollman 
8091158dfb7SGarrett Wollman int
8101158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
8111158dfb7SGarrett Wollman 	struct ifnet *ifp;
8121158dfb7SGarrett Wollman 	struct sockaddr **llsa;
8131158dfb7SGarrett Wollman 	struct sockaddr *sa;
8141158dfb7SGarrett Wollman {
8151158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
8161158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
81782cd038dSYoshinobu Inoue #ifdef INET6
81882cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
81982cd038dSYoshinobu Inoue #endif
8201158dfb7SGarrett Wollman 	u_char *e_addr;
8211158dfb7SGarrett Wollman 
8221158dfb7SGarrett Wollman 	switch(sa->sa_family) {
8231158dfb7SGarrett Wollman 	case AF_LINK:
8247f33a738SJulian Elischer 		/*
8257f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
8267f33a738SJulian Elischer 		 */
8271158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
8281158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8291158dfb7SGarrett Wollman 		if ((e_addr[0] & 1) != 1)
8301158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8311158dfb7SGarrett Wollman 		*llsa = 0;
8321158dfb7SGarrett Wollman 		return 0;
8331158dfb7SGarrett Wollman 
8341158dfb7SGarrett Wollman #ifdef INET
8351158dfb7SGarrett Wollman 	case AF_INET:
8361158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
8371158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
8381158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8391158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
8401158dfb7SGarrett Wollman 		       M_WAITOK);
8411158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
8421158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
8431158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
8441158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
8451158dfb7SGarrett Wollman 		sdl->sdl_nlen = 0;
8461158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
8471158dfb7SGarrett Wollman 		sdl->sdl_slen = 0;
8481158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8491158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
8501158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
8511158dfb7SGarrett Wollman 		return 0;
8521158dfb7SGarrett Wollman #endif
85382cd038dSYoshinobu Inoue #ifdef INET6
85482cd038dSYoshinobu Inoue 	case AF_INET6:
85582cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
856595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
857595b8a1cSJun-ichiro itojun Hagino 			/*
858595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
859595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
860595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
861595b8a1cSJun-ichiro itojun Hagino 			 */
862595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
863595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
864595b8a1cSJun-ichiro itojun Hagino 			return 0;
865595b8a1cSJun-ichiro itojun Hagino 		}
86682cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
86782cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
86882cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
86982cd038dSYoshinobu Inoue 		       M_WAITOK);
87082cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
87182cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
87282cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
87382cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
87482cd038dSYoshinobu Inoue 		sdl->sdl_nlen = 0;
87582cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
87682cd038dSYoshinobu Inoue 		sdl->sdl_slen = 0;
87782cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
87882cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
87982cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
88082cd038dSYoshinobu Inoue 		return 0;
88182cd038dSYoshinobu Inoue #endif
8821158dfb7SGarrett Wollman 
8831158dfb7SGarrett Wollman 	default:
8841158dfb7SGarrett Wollman 		/*
8851158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
8861158dfb7SGarrett Wollman 		 * that counts...
8871158dfb7SGarrett Wollman 		 */
8881158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
8891158dfb7SGarrett Wollman 	}
8901158dfb7SGarrett Wollman }
8914cf49a43SJulian Elischer 
892