xref: /freebsd/sys/net/if_ethersubr.c (revision 8cdfefbd0c98c63ed30e8659bb1a2b026c87e0ae)
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 
104e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
105e1e1452dSArchie Cobbs void	(*ng_ether_input_p)(struct ifnet *ifp,
106e1e1452dSArchie Cobbs 		struct mbuf **mp, struct ether_header *eh);
107e1e1452dSArchie Cobbs void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
108e1e1452dSArchie Cobbs 		struct mbuf *m, struct ether_header *eh);
109e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
110e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
111e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
112e1e1452dSArchie Cobbs 
1139d4fe4b2SBrooks Davis int	(*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
1149d4fe4b2SBrooks Davis int	(*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
1159d4fe4b2SBrooks Davis 		u_int16_t t);
1169d4fe4b2SBrooks Davis 
1171158dfb7SGarrett Wollman static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
1181158dfb7SGarrett Wollman 				    struct sockaddr *));
119df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1204c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
1214c8e8c05SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP)
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes /*
124df8bae1dSRodney W. Grimes  * Ethernet output routine.
125df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
126df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
127df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
128df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
129df8bae1dSRodney W. Grimes  */
130df8bae1dSRodney W. Grimes int
131d25f3712SBrian Feldman ether_output(ifp, m, dst, rt0)
132df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
133d25f3712SBrian Feldman 	struct mbuf *m;
134df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
135df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
136df8bae1dSRodney W. Grimes {
137df8bae1dSRodney W. Grimes 	short type;
138e1e1452dSArchie Cobbs 	int error = 0, hdrcmplt = 0;
139114ae644SMike Smith  	u_char esrc[6], edst[6];
140df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
141df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
142e1e1452dSArchie Cobbs 	int off, loop_copy = 0;
14384dd0fd0SJulian Elischer 	int hlen;	/* link layer header lenght */
1444c8e8c05SJulian Elischer 	struct arpcom *ac = IFP2AC(ifp);
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
147df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1483bda9f9bSPoul-Henning Kamp 	rt = rt0;
1493bda9f9bSPoul-Henning Kamp 	if (rt) {
150df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1513bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1523bda9f9bSPoul-Henning Kamp 			if (rt0)
153df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
154df8bae1dSRodney W. Grimes 			else
155df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
156df8bae1dSRodney W. Grimes 		}
157df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
158df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
159df8bae1dSRodney W. Grimes 				goto lookup;
160df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
161df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
162995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
163995add1aSGarrett Wollman 							  0UL);
164df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
165df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
166df8bae1dSRodney W. Grimes 			}
167df8bae1dSRodney W. Grimes 		}
168df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
169df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
170227ee8a1SPoul-Henning Kamp 			    time_second < rt->rt_rmx.rmx_expire)
171df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
172df8bae1dSRodney W. Grimes 	}
17384dd0fd0SJulian Elischer 	hlen = ETHER_HDR_LEN;
174df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
175df8bae1dSRodney W. Grimes #ifdef INET
176df8bae1dSRodney W. Grimes 	case AF_INET:
1775df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
178df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
179df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
18034bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
181df8bae1dSRodney W. Grimes 		break;
182df8bae1dSRodney W. Grimes #endif
18382cd038dSYoshinobu Inoue #ifdef INET6
18482cd038dSYoshinobu Inoue 	case AF_INET6:
18582cd038dSYoshinobu Inoue 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
186fef5fd23SBosko Milekic 			/* Something bad happened */
18782cd038dSYoshinobu Inoue 			return(0);
18882cd038dSYoshinobu Inoue 		}
18982cd038dSYoshinobu Inoue 		off = m->m_pkthdr.len - m->m_len;
19082cd038dSYoshinobu Inoue 		type = htons(ETHERTYPE_IPV6);
19182cd038dSYoshinobu Inoue 		break;
19282cd038dSYoshinobu Inoue #endif
193cc6a66f2SJulian Elischer #ifdef IPX
194cc6a66f2SJulian Elischer 	case AF_IPX:
1954f93599fSBoris Popov 		if (ef_outputp) {
1962a7e8eceSBoris Popov 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
1975accfb8cSBoris Popov 		    if (error)
1985accfb8cSBoris Popov 			goto bad;
1994f93599fSBoris Popov 		} else
20034bed8b0SDavid Greenman 		    type = htons(ETHERTYPE_IPX);
201cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
202cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
203cc6a66f2SJulian Elischer 		break;
204cc6a66f2SJulian Elischer #endif
205655929bfSJulian Elischer #ifdef NETATALK
206655929bfSJulian Elischer 	case AF_APPLETALK:
2071d0eab59SJulian Elischer 	  {
208ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
2091d0eab59SJulian Elischer 
210ed7509acSJulian Elischer 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
2111d0eab59SJulian Elischer 		    goto bad;
2121d0eab59SJulian Elischer 	    }
213ed7509acSJulian Elischer 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
2141d0eab59SJulian Elischer 		    return (0);
215655929bfSJulian Elischer 	    /*
216ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
217655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
218655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
219655929bfSJulian Elischer 	     */
220655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
221655929bfSJulian Elischer 		struct llc llc;
222655929bfSJulian Elischer 
2232a0c503eSBosko Milekic 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
224655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
225655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
226655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
227655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
228655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
22934bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
23084dd0fd0SJulian Elischer 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
231655929bfSJulian Elischer 	    } else {
23234bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
233655929bfSJulian Elischer 	    }
234655929bfSJulian Elischer 	    break;
235ed7509acSJulian Elischer 	  }
2368cdfefbdSPeter Wemm #endif /* NETATALK */
237df8bae1dSRodney W. Grimes #ifdef NS
238df8bae1dSRodney W. Grimes 	case AF_NS:
23988e038feSJordan K. Hubbard 		switch(ns_nettype){
24088e038feSJordan K. Hubbard 		default:
24188e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
24288e038feSJordan K. Hubbard 			type = 0x8137;
24388e038feSJordan K. Hubbard 			break;
24488e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
24588e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
24688e038feSJordan K. Hubbard 			break;
24788e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
2482a0c503eSBosko Milekic 			M_PREPEND(m, 3, M_TRYWAIT);
24988e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
25088e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
25188e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25288e038feSJordan K. Hubbard 			*cp++ = 0xE0;
25388e038feSJordan K. Hubbard 			*cp++ = 0x03;
25488e038feSJordan K. Hubbard 			break;
25588e038feSJordan K. Hubbard 		}
256df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
257df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
2584c8e8c05SJulian Elischer 		/*
2594c8e8c05SJulian Elischer 		 * XXX if ns_thishost is the same as the node's ethernet
2604c8e8c05SJulian Elischer 		 * address then just the default code will catch this anyhow.
2614c8e8c05SJulian Elischer 		 * So I'm not sure if this next clause should be here at all?
2624c8e8c05SJulian Elischer 		 * [JRE]
2634c8e8c05SJulian Elischer 		 */
26488e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
26588e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
26688e038feSJordan K. Hubbard 			inq = &nsintrq;
267df5e1987SJonathan Lemon 			if (IF_HANDOFF(inq, m, NULL))
268df5e1987SJonathan Lemon 				schednetisr(NETISR_NS);
26988e038feSJordan K. Hubbard 			return (error);
27088e038feSJordan K. Hubbard 		}
27188e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
2724c8e8c05SJulian Elischer 			m->m_flags |= M_BCAST;
27388e038feSJordan K. Hubbard 		}
274df8bae1dSRodney W. Grimes 		break;
27588e038feSJordan K. Hubbard #endif /* NS */
276df8bae1dSRodney W. Grimes 
277114ae644SMike Smith 	case pseudo_AF_HDRCMPLT:
278114ae644SMike Smith 		hdrcmplt = 1;
279114ae644SMike Smith 		eh = (struct ether_header *)dst->sa_data;
280114ae644SMike Smith 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
281114ae644SMike Smith 		/* FALLTHROUGH */
282114ae644SMike Smith 
283df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
2849d3f194dSJulian Elischer 		loop_copy = -1; /* if this is for us, don't do it */
285df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
28694a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
287df8bae1dSRodney W. Grimes 		type = eh->ether_type;
288df8bae1dSRodney W. Grimes 		break;
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 	default:
291df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
292df8bae1dSRodney W. Grimes 			dst->sa_family);
293df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes 	/*
297df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
298df8bae1dSRodney W. Grimes 	 * allocate another.
299df8bae1dSRodney W. Grimes 	 */
300df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
301df8bae1dSRodney W. Grimes 	if (m == 0)
302df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
303df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
30494a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
305df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
30694a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
307114ae644SMike Smith 	if (hdrcmplt)
308114ae644SMike Smith 		(void)memcpy(eh->ether_shost, esrc,
309114ae644SMike Smith 			sizeof(eh->ether_shost));
310114ae644SMike Smith 	else
31194a5d9b6SDavid Greenman 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
312df8bae1dSRodney W. Grimes 			sizeof(eh->ether_shost));
313ed7509acSJulian Elischer 
314ed7509acSJulian Elischer 	/*
315ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
316ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
317ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
318ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
319ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
320ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
321ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
322ed7509acSJulian Elischer 	 */
3234c8e8c05SJulian Elischer 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
3249d3f194dSJulian Elischer 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
325ed7509acSJulian Elischer 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
326ed7509acSJulian Elischer 
32706a429a3SArchie Cobbs 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
328ed7509acSJulian Elischer 		} else if (bcmp(eh->ether_dhost,
329ed7509acSJulian Elischer 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
33006a429a3SArchie Cobbs 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
331ed7509acSJulian Elischer 			return (0);	/* XXX */
332ed7509acSJulian Elischer 		}
333ed7509acSJulian Elischer 	}
3342e2de7f2SArchie Cobbs 
335e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
336e1e1452dSArchie Cobbs 	if (ng_ether_output_p != NULL) {
337e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
338e1e1452dSArchie Cobbs bad:			if (m != NULL)
339e1e1452dSArchie Cobbs 				m_freem(m);
340e1e1452dSArchie Cobbs 			return (error);
341e1e1452dSArchie Cobbs 		}
342e1e1452dSArchie Cobbs 		if (m == NULL)
343e1e1452dSArchie Cobbs 			return (0);
344e1e1452dSArchie Cobbs 	}
345e1e1452dSArchie Cobbs 
346e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
347e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
348e1e1452dSArchie Cobbs }
349e1e1452dSArchie Cobbs 
350e1e1452dSArchie Cobbs /*
351e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
352e1e1452dSArchie Cobbs  *
353e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
354e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
355e1e1452dSArchie Cobbs  */
356e1e1452dSArchie Cobbs int
357e1e1452dSArchie Cobbs ether_output_frame(ifp, m)
358e1e1452dSArchie Cobbs 	struct ifnet *ifp;
359e1e1452dSArchie Cobbs 	struct mbuf *m;
360e1e1452dSArchie Cobbs {
361df5e1987SJonathan Lemon 	int error = 0;
362e1e1452dSArchie Cobbs 
363fb5fbe46SLuigi Rizzo #ifdef BRIDGE
364507b4b54SLuigi Rizzo 	if (do_bridge && BDG_USED(ifp) ) {
365507b4b54SLuigi Rizzo 		struct ether_header *eh; /* a ptr suffices */
3661db59ce6SEivind Eklund 
367fb5fbe46SLuigi Rizzo 		m->m_pkthdr.rcvif = NULL;
368507b4b54SLuigi Rizzo 		eh = mtod(m, struct ether_header *);
3692e2de7f2SArchie Cobbs 		m_adj(m, ETHER_HDR_LEN);
370507b4b54SLuigi Rizzo 		m = bdg_forward(m, eh, ifp);
3712e2de7f2SArchie Cobbs 		if (m != NULL)
3722e2de7f2SArchie Cobbs 			m_freem(m);
3731db59ce6SEivind Eklund 		return (0);
374fb5fbe46SLuigi Rizzo 	}
375fb5fbe46SLuigi Rizzo #endif
3762e2de7f2SArchie Cobbs 
377df8bae1dSRodney W. Grimes 	/*
378df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
379df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
380df8bae1dSRodney W. Grimes 	 */
381df5e1987SJonathan Lemon 	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
382e1e1452dSArchie Cobbs 		return (ENOBUFS);
383df8bae1dSRodney W. Grimes 	return (error);
384df8bae1dSRodney W. Grimes }
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes /*
387df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
388df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
389df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
390e1e1452dSArchie Cobbs  *
39102a282acSLuigi Rizzo  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
39202a282acSLuigi Rizzo  * cluster, right before m_data. So be very careful when working on m,
39302a282acSLuigi Rizzo  * as you could destroy *eh !!
39402a282acSLuigi Rizzo  * A (probably) more convenient and efficient interface to ether_input
39502a282acSLuigi Rizzo  * is to have the whole packet (with the ethernet header) into the mbuf:
39602a282acSLuigi Rizzo  * modules which do not need the ethernet header can easily drop it, while
39702a282acSLuigi Rizzo  * others (most noticeably bridge and ng_ether) do not need to do additional
39802a282acSLuigi Rizzo  * work to put the ethernet header back into the mbuf.
39902a282acSLuigi Rizzo  *
400e1e1452dSArchie Cobbs  * First we perform any link layer operations, then continue
401e1e1452dSArchie Cobbs  * to the upper layers with ether_demux().
402df8bae1dSRodney W. Grimes  */
403df8bae1dSRodney W. Grimes void
404df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
405df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
406e1e1452dSArchie Cobbs 	struct ether_header *eh;
407df8bae1dSRodney W. Grimes 	struct mbuf *m;
408df8bae1dSRodney W. Grimes {
409cb24f323SArchie Cobbs #ifdef BRIDGE
410507b4b54SLuigi Rizzo 	struct ether_header save_eh;
411cb24f323SArchie Cobbs #endif
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 #ifdef BRIDGE
4322e2de7f2SArchie Cobbs 	/* Check for bridging mode */
433507b4b54SLuigi Rizzo 	if (do_bridge && BDG_USED(ifp) ) {
4342e2de7f2SArchie Cobbs 		struct ifnet *bif;
4352e2de7f2SArchie Cobbs 
4362e2de7f2SArchie Cobbs 		/* Check with bridging code */
4372e2de7f2SArchie Cobbs 		if ((bif = bridge_in(ifp, eh)) == BDG_DROP) {
4382e2de7f2SArchie Cobbs 			m_freem(m);
4392e2de7f2SArchie Cobbs 			return;
4402e2de7f2SArchie Cobbs 		}
441ddacb30fSBosko Milekic 		if (bif != BDG_LOCAL) {
442507b4b54SLuigi Rizzo 			struct mbuf *oldm = m ;
443507b4b54SLuigi Rizzo 
444507b4b54SLuigi Rizzo 			save_eh = *eh ; /* because it might change */
4457e1cd0d2SLuigi Rizzo 			m = bdg_forward(m, eh, bif);	/* needs forwarding */
446ddacb30fSBosko Milekic 			/*
447ddacb30fSBosko Milekic 			 * Do not continue if bdg_forward() processed our
448ddacb30fSBosko Milekic 			 * packet (and cleared the mbuf pointer m) or if
449ddacb30fSBosko Milekic 			 * it dropped (m_free'd) the packet itself.
450ddacb30fSBosko Milekic 			 */
451507b4b54SLuigi Rizzo 			if (m == NULL) {
452507b4b54SLuigi Rizzo 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
453507b4b54SLuigi Rizzo 				printf("bdg_forward drop MULTICAST PKT\n");
454ddacb30fSBosko Milekic 			    return;
455ddacb30fSBosko Milekic 			}
456507b4b54SLuigi Rizzo 			if (m != oldm) /* m changed! */
457507b4b54SLuigi Rizzo 			    eh = &save_eh ;
458507b4b54SLuigi Rizzo 		}
4592e2de7f2SArchie Cobbs 		if (bif == BDG_LOCAL
4602e2de7f2SArchie Cobbs 		    || bif == BDG_BCAST
4612e2de7f2SArchie Cobbs 		    || bif == BDG_MCAST)
4622e2de7f2SArchie Cobbs 			goto recvLocal;			/* receive locally */
4632e2de7f2SArchie Cobbs 
4642e2de7f2SArchie Cobbs 		/* If not local and not multicast, just drop it */
4652e2de7f2SArchie Cobbs 		if (m != NULL)
4662e2de7f2SArchie Cobbs 		    m_freem(m);
4672e2de7f2SArchie Cobbs 		return;
4682e2de7f2SArchie Cobbs        }
4692e2de7f2SArchie Cobbs #endif
4702e2de7f2SArchie Cobbs 
4712e2de7f2SArchie Cobbs #ifdef BRIDGE
4722e2de7f2SArchie Cobbs recvLocal:
4732e2de7f2SArchie Cobbs #endif
474e1e1452dSArchie Cobbs 	/* Continue with upper layer processing */
475e1e1452dSArchie Cobbs 	ether_demux(ifp, eh, m);
47610b1fde0SMark Murray 	/* First chunk of an mbuf contains good junk */
47710b1fde0SMark Murray 	if (harvest.ethernet)
47810b1fde0SMark Murray 		random_harvest(m, 16, 3, 0, RANDOM_NET);
479e1e1452dSArchie Cobbs }
480e1e1452dSArchie Cobbs 
481e1e1452dSArchie Cobbs /*
482e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
483e1e1452dSArchie Cobbs  */
484e1e1452dSArchie Cobbs void
485e1e1452dSArchie Cobbs ether_demux(ifp, eh, m)
486e1e1452dSArchie Cobbs 	struct ifnet *ifp;
487e1e1452dSArchie Cobbs 	struct ether_header *eh;
488e1e1452dSArchie Cobbs 	struct mbuf *m;
489e1e1452dSArchie Cobbs {
490e1e1452dSArchie Cobbs 	struct ifqueue *inq;
491e1e1452dSArchie Cobbs 	u_short ether_type;
492e1e1452dSArchie Cobbs #if defined(NETATALK)
493e1e1452dSArchie Cobbs 	register struct llc *l;
494e1e1452dSArchie Cobbs #endif
495e1e1452dSArchie Cobbs 
496cb24f323SArchie Cobbs 	/* Discard packet if upper layers shouldn't see it because it was
497cb24f323SArchie Cobbs 	   unicast to a different Ethernet address. If the driver is working
498cb24f323SArchie Cobbs 	   properly, then this situation can only happen when the interface
499cb24f323SArchie Cobbs 	   is in promiscuous mode. */
500cb24f323SArchie Cobbs 	if ((ifp->if_flags & IFF_PROMISC) != 0
501cb24f323SArchie Cobbs 	    && (eh->ether_dhost[0] & 1) == 0
502cb24f323SArchie Cobbs 	    && bcmp(eh->ether_dhost,
503cb24f323SArchie Cobbs 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
504cb24f323SArchie Cobbs 		m_freem(m);
505cb24f323SArchie Cobbs 		return;
506cb24f323SArchie Cobbs 	}
507cb24f323SArchie Cobbs 
5082e2de7f2SArchie Cobbs 	/* Discard packet if interface is not up */
509df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
510df8bae1dSRodney W. Grimes 		m_freem(m);
511df8bae1dSRodney W. Grimes 		return;
512df8bae1dSRodney W. Grimes 	}
513df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
5142cc2df49SGarrett Wollman 	if (eh->ether_dhost[0] & 1) {
515df8bae1dSRodney W. Grimes 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
516df8bae1dSRodney W. Grimes 			 sizeof(etherbroadcastaddr)) == 0)
517df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
5182cc2df49SGarrett Wollman 		else
519df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
5202cc2df49SGarrett Wollman 	}
521df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
522df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
523df8bae1dSRodney W. Grimes 
524307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
525307d80beSDavid Greenman 
526307d80beSDavid Greenman 	switch (ether_type) {
527df8bae1dSRodney W. Grimes #ifdef INET
528df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
5291f91d8c5SDavid Greenman 		if (ipflow_fastforward(m))
5301f91d8c5SDavid Greenman 			return;
531df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
532df8bae1dSRodney W. Grimes 		inq = &ipintrq;
533df8bae1dSRodney W. Grimes 		break;
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
53608aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
53708aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
53808aadfbbSJonathan Lemon 			m_freem(m);
53908aadfbbSJonathan Lemon 			return;
54008aadfbbSJonathan Lemon 		}
541df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
542df8bae1dSRodney W. Grimes 		inq = &arpintrq;
543df8bae1dSRodney W. Grimes 		break;
544df8bae1dSRodney W. Grimes #endif
545cc6a66f2SJulian Elischer #ifdef IPX
546cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
5474f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5484f93599fSBoris Popov 			return;
549cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
550cc6a66f2SJulian Elischer 		inq = &ipxintrq;
551cc6a66f2SJulian Elischer 		break;
552cc6a66f2SJulian Elischer #endif
55382cd038dSYoshinobu Inoue #ifdef INET6
55482cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
55582cd038dSYoshinobu Inoue 		schednetisr(NETISR_IPV6);
55682cd038dSYoshinobu Inoue 		inq = &ip6intrq;
55782cd038dSYoshinobu Inoue 		break;
55882cd038dSYoshinobu Inoue #endif
559df8bae1dSRodney W. Grimes #ifdef NS
56088e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
561df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
562df8bae1dSRodney W. Grimes 		inq = &nsintrq;
563df8bae1dSRodney W. Grimes 		break;
56488e038feSJordan K. Hubbard 
56588e038feSJordan K. Hubbard #endif /* NS */
566655929bfSJulian Elischer #ifdef NETATALK
567655929bfSJulian Elischer         case ETHERTYPE_AT:
568655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
569655929bfSJulian Elischer                 inq = &atintrq1;
570655929bfSJulian Elischer                 break;
571655929bfSJulian Elischer         case ETHERTYPE_AARP:
572655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
5734c8e8c05SJulian Elischer                 aarpinput(IFP2AC(ifp), m); /* XXX */
574655929bfSJulian Elischer                 return;
5758cdfefbdSPeter Wemm #endif /* NETATALK */
5769d4fe4b2SBrooks Davis 	case ETHERTYPE_VLAN:
5779d4fe4b2SBrooks Davis 		VLAN_INPUT(ifp, eh, m);
5789d4fe4b2SBrooks Davis 		return;
579df8bae1dSRodney W. Grimes 	default:
5804f93599fSBoris Popov #ifdef IPX
5814f93599fSBoris Popov 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
5824f93599fSBoris Popov 			return;
5834f93599fSBoris Popov #endif /* IPX */
58488e038feSJordan K. Hubbard #ifdef NS
58588e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
58688e038feSJordan K. Hubbard 		/* Novell 802.3 */
58788e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
58888e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
58988e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
59088e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
59188e038feSJordan K. Hubbard 				m->m_len -= 3;
59288e038feSJordan K. Hubbard 				m->m_data += 3;
59388e038feSJordan K. Hubbard 			}
59488e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
59588e038feSJordan K. Hubbard 				inq = &nsintrq;
59688e038feSJordan K. Hubbard 				break;
59788e038feSJordan K. Hubbard 		}
59888e038feSJordan K. Hubbard #endif /* NS */
599242c5536SPeter Wemm #if defined(NETATALK)
600307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
601df8bae1dSRodney W. Grimes 			goto dropanyway;
602df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
603df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
604655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
605655929bfSJulian Elischer 		    switch (l->llc_control) {
606655929bfSJulian Elischer 		    case LLC_UI:
607655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
608655929bfSJulian Elischer 			    goto dropanyway;
609655929bfSJulian Elischer 
610655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
611655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
612655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
613655929bfSJulian Elischer 			    inq = &atintrq2;
614655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
615655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
616655929bfSJulian Elischer 			    break;
617655929bfSJulian Elischer 			}
618655929bfSJulian Elischer 
619655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
620655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
621655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
622655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
6234c8e8c05SJulian Elischer 			    aarpinput(IFP2AC(ifp), m); /* XXX */
624655929bfSJulian Elischer 			    return;
625655929bfSJulian Elischer 			}
626655929bfSJulian Elischer 
627655929bfSJulian Elischer 		    default:
628655929bfSJulian Elischer 			goto dropanyway;
629655929bfSJulian Elischer 		    }
630655929bfSJulian Elischer 		    break;
631df8bae1dSRodney W. Grimes 		dropanyway:
632df8bae1dSRodney W. Grimes 		default:
633e1e1452dSArchie Cobbs 			if (ng_ether_input_orphan_p != NULL)
634e1e1452dSArchie Cobbs 				(*ng_ether_input_orphan_p)(ifp, m, eh);
635e1e1452dSArchie Cobbs 			else
636df8bae1dSRodney W. Grimes 				m_freem(m);
637df8bae1dSRodney W. Grimes 			return;
638df8bae1dSRodney W. Grimes 		}
639242c5536SPeter Wemm #else /* NETATALK */
640e1e1452dSArchie Cobbs 		if (ng_ether_input_orphan_p != NULL)
641e1e1452dSArchie Cobbs 			(*ng_ether_input_orphan_p)(ifp, m, eh);
642e1e1452dSArchie Cobbs 		else
643df8bae1dSRodney W. Grimes 			m_freem(m);
644df8bae1dSRodney W. Grimes 		return;
645242c5536SPeter Wemm #endif /* NETATALK */
646df8bae1dSRodney W. Grimes 	}
647df8bae1dSRodney W. Grimes 
648df5e1987SJonathan Lemon 	(void) IF_HANDOFF(inq, m, NULL);
649df8bae1dSRodney W. Grimes }
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes /*
652df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
653df8bae1dSRodney W. Grimes  */
654df8bae1dSRodney W. Grimes void
65521b8ebd9SArchie Cobbs ether_ifattach(ifp, bpf)
656df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
65721b8ebd9SArchie Cobbs 	int bpf;
658df8bae1dSRodney W. Grimes {
659df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
660df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
661df8bae1dSRodney W. Grimes 
66221b8ebd9SArchie Cobbs 	if_attach(ifp);
663df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
664df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
665df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
666df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
6671158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
668a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
669a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
670f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
67121b8ebd9SArchie Cobbs 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
67259562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
673df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
674df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
6754c8e8c05SJulian Elischer 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
67621b8ebd9SArchie Cobbs 	if (bpf)
67721b8ebd9SArchie Cobbs 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
678e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
679e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
68082902fa3SNick Sayer #ifdef BRIDGE
68182902fa3SNick Sayer 	bdgtakeifaces();
68282902fa3SNick Sayer #endif
683e1e1452dSArchie Cobbs }
684e1e1452dSArchie Cobbs 
68521b8ebd9SArchie Cobbs /*
68621b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
68721b8ebd9SArchie Cobbs  */
68821b8ebd9SArchie Cobbs void
68921b8ebd9SArchie Cobbs ether_ifdetach(ifp, bpf)
69021b8ebd9SArchie Cobbs 	struct ifnet *ifp;
69121b8ebd9SArchie Cobbs 	int bpf;
69221b8ebd9SArchie Cobbs {
69321b8ebd9SArchie Cobbs 	if (ng_ether_detach_p != NULL)
69421b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
69521b8ebd9SArchie Cobbs 	if (bpf)
69621b8ebd9SArchie Cobbs 		bpfdetach(ifp);
69721b8ebd9SArchie Cobbs 	if_detach(ifp);
69882902fa3SNick Sayer #ifdef BRIDGE
69982902fa3SNick Sayer 	bdgtakeifaces();
70082902fa3SNick Sayer #endif
70121b8ebd9SArchie Cobbs }
70221b8ebd9SArchie Cobbs 
703ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
704602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
70530106f6aSPoul-Henning Kamp 
706fb583156SDavid Greenman int
707c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
708c5a1016bSBruce Evans 	struct ifnet *ifp;
709c5a1016bSBruce Evans 	int command;
710c5a1016bSBruce Evans 	caddr_t data;
71130106f6aSPoul-Henning Kamp {
71230106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
71330106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
714fb583156SDavid Greenman 	int error = 0;
71530106f6aSPoul-Henning Kamp 
71630106f6aSPoul-Henning Kamp 	switch (command) {
71730106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
71830106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
71930106f6aSPoul-Henning Kamp 
72030106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
72130106f6aSPoul-Henning Kamp #ifdef INET
72230106f6aSPoul-Henning Kamp 		case AF_INET:
72330106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
7244c8e8c05SJulian Elischer 			arp_ifinit(IFP2AC(ifp), ifa);
72530106f6aSPoul-Henning Kamp 			break;
72630106f6aSPoul-Henning Kamp #endif
72730106f6aSPoul-Henning Kamp #ifdef IPX
72830106f6aSPoul-Henning Kamp 		/*
72930106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
73030106f6aSPoul-Henning Kamp 		 */
73130106f6aSPoul-Henning Kamp 		case AF_IPX:
73230106f6aSPoul-Henning Kamp 			{
73330106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
7344c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
73530106f6aSPoul-Henning Kamp 
73630106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
73730106f6aSPoul-Henning Kamp 				ina->x_host =
73886101139SPoul-Henning Kamp 				    *(union ipx_host *)
73986101139SPoul-Henning Kamp 			            ac->ac_enaddr;
74030106f6aSPoul-Henning Kamp 			else {
74130106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
74286101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
74386101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
74430106f6aSPoul-Henning Kamp 			}
74530106f6aSPoul-Henning Kamp 
74630106f6aSPoul-Henning Kamp 			/*
74730106f6aSPoul-Henning Kamp 			 * Set new address
74830106f6aSPoul-Henning Kamp 			 */
74930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
75030106f6aSPoul-Henning Kamp 			break;
75130106f6aSPoul-Henning Kamp 			}
75230106f6aSPoul-Henning Kamp #endif
75330106f6aSPoul-Henning Kamp #ifdef NS
75430106f6aSPoul-Henning Kamp 		/*
75530106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
75630106f6aSPoul-Henning Kamp 		 */
75730106f6aSPoul-Henning Kamp 		case AF_NS:
75830106f6aSPoul-Henning Kamp 		{
75930106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
7604c8e8c05SJulian Elischer 			struct arpcom *ac = IFP2AC(ifp);
76130106f6aSPoul-Henning Kamp 
76230106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
76330106f6aSPoul-Henning Kamp 				ina->x_host =
76486101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
76530106f6aSPoul-Henning Kamp 			else {
76630106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
76786101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
76886101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
76930106f6aSPoul-Henning Kamp 			}
77030106f6aSPoul-Henning Kamp 
77130106f6aSPoul-Henning Kamp 			/*
77230106f6aSPoul-Henning Kamp 			 * Set new address
77330106f6aSPoul-Henning Kamp 			 */
77430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
77530106f6aSPoul-Henning Kamp 			break;
77630106f6aSPoul-Henning Kamp 		}
77730106f6aSPoul-Henning Kamp #endif
77830106f6aSPoul-Henning Kamp 		default:
77930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
78030106f6aSPoul-Henning Kamp 			break;
78130106f6aSPoul-Henning Kamp 		}
78230106f6aSPoul-Henning Kamp 		break;
78330106f6aSPoul-Henning Kamp 
78430106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
78530106f6aSPoul-Henning Kamp 		{
78630106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
78730106f6aSPoul-Henning Kamp 
78830106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
7894c8e8c05SJulian Elischer 			bcopy(IFP2AC(ifp)->ac_enaddr,
79030106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
79130106f6aSPoul-Henning Kamp 		}
79230106f6aSPoul-Henning Kamp 		break;
793fb583156SDavid Greenman 
794fb583156SDavid Greenman 	case SIOCSIFMTU:
795fb583156SDavid Greenman 		/*
796fb583156SDavid Greenman 		 * Set the interface MTU.
797fb583156SDavid Greenman 		 */
798fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
799fb583156SDavid Greenman 			error = EINVAL;
800fb583156SDavid Greenman 		} else {
801fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
80230106f6aSPoul-Henning Kamp 		}
803fb583156SDavid Greenman 		break;
804fb583156SDavid Greenman 	}
805fb583156SDavid Greenman 	return (error);
80630106f6aSPoul-Henning Kamp }
8071158dfb7SGarrett Wollman 
8081158dfb7SGarrett Wollman int
8091158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
8101158dfb7SGarrett Wollman 	struct ifnet *ifp;
8111158dfb7SGarrett Wollman 	struct sockaddr **llsa;
8121158dfb7SGarrett Wollman 	struct sockaddr *sa;
8131158dfb7SGarrett Wollman {
8141158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
8151158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
81682cd038dSYoshinobu Inoue #ifdef INET6
81782cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
81882cd038dSYoshinobu Inoue #endif
8191158dfb7SGarrett Wollman 	u_char *e_addr;
8201158dfb7SGarrett Wollman 
8211158dfb7SGarrett Wollman 	switch(sa->sa_family) {
8221158dfb7SGarrett Wollman 	case AF_LINK:
8237f33a738SJulian Elischer 		/*
8247f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
8257f33a738SJulian Elischer 		 */
8261158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
8271158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8281158dfb7SGarrett Wollman 		if ((e_addr[0] & 1) != 1)
8291158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8301158dfb7SGarrett Wollman 		*llsa = 0;
8311158dfb7SGarrett Wollman 		return 0;
8321158dfb7SGarrett Wollman 
8331158dfb7SGarrett Wollman #ifdef INET
8341158dfb7SGarrett Wollman 	case AF_INET:
8351158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
8361158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
8371158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
8381158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
83926e30963SBill Fenner 		       M_WAITOK|M_ZERO);
8401158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
8411158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
8421158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
8431158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
8441158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
8451158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8461158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
8471158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
8481158dfb7SGarrett Wollman 		return 0;
8491158dfb7SGarrett Wollman #endif
85082cd038dSYoshinobu Inoue #ifdef INET6
85182cd038dSYoshinobu Inoue 	case AF_INET6:
85282cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
853595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
854595b8a1cSJun-ichiro itojun Hagino 			/*
855595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
856595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
857595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
858595b8a1cSJun-ichiro itojun Hagino 			 */
859595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
860595b8a1cSJun-ichiro itojun Hagino 			*llsa = 0;
861595b8a1cSJun-ichiro itojun Hagino 			return 0;
862595b8a1cSJun-ichiro itojun Hagino 		}
86382cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
86482cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
86582cd038dSYoshinobu Inoue 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
86626e30963SBill Fenner 		       M_WAITOK|M_ZERO);
86782cd038dSYoshinobu Inoue 		sdl->sdl_len = sizeof *sdl;
86882cd038dSYoshinobu Inoue 		sdl->sdl_family = AF_LINK;
86982cd038dSYoshinobu Inoue 		sdl->sdl_index = ifp->if_index;
87082cd038dSYoshinobu Inoue 		sdl->sdl_type = IFT_ETHER;
87182cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
87282cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
87382cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
87482cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
87582cd038dSYoshinobu Inoue 		return 0;
87682cd038dSYoshinobu Inoue #endif
8771158dfb7SGarrett Wollman 
8781158dfb7SGarrett Wollman 	default:
8791158dfb7SGarrett Wollman 		/*
8801158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
8811158dfb7SGarrett Wollman 		 * that counts...
8821158dfb7SGarrett Wollman 		 */
8831158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
8841158dfb7SGarrett Wollman 	}
8851158dfb7SGarrett Wollman }
8864cf49a43SJulian Elischer 
887