xref: /freebsd/sys/net/if_ethersubr.c (revision ed7509ac)
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
34ed7509acSJulian Elischer  * $Id: if_ethersubr.c,v 1.48 1998/05/19 14:04:02 dg Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
377262d3e4SEivind Eklund #include "opt_atalk.h"
381d5e9e22SEivind Eklund #include "opt_inet.h"
39430df5f4SEivind Eklund #include "opt_ipx.h"
40430df5f4SEivind Eklund 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46df8bae1dSRodney W. Grimes #include <sys/socket.h>
4751a53488SBruce Evans #include <sys/sockio.h>
48602d513cSGarrett Wollman #include <sys/sysctl.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
51df8bae1dSRodney W. Grimes #include <net/netisr.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53df8bae1dSRodney W. Grimes #include <net/if_llc.h>
54df8bae1dSRodney W. Grimes #include <net/if_dl.h>
55df8bae1dSRodney W. Grimes #include <net/if_types.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #ifdef INET
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
611d5e9e22SEivind Eklund #endif
62df8bae1dSRodney W. Grimes 
63cc6a66f2SJulian Elischer #ifdef IPX
64cc6a66f2SJulian Elischer #include <netipx/ipx.h>
65cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
66cc6a66f2SJulian Elischer #endif
67cc6a66f2SJulian Elischer 
68df8bae1dSRodney W. Grimes #ifdef NS
69df8bae1dSRodney W. Grimes #include <netns/ns.h>
70df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
71d0ec898dSJordan K. Hubbard ushort ns_nettype;
7288e038feSJordan K. Hubbard int ether_outputdebug = 0;
7388e038feSJordan K. Hubbard int ether_inputdebug = 0;
74df8bae1dSRodney W. Grimes #endif
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #ifdef ISO
77df8bae1dSRodney W. Grimes #include <netiso/argo_debug.h>
78df8bae1dSRodney W. Grimes #include <netiso/iso.h>
79df8bae1dSRodney W. Grimes #include <netiso/iso_var.h>
80df8bae1dSRodney W. Grimes #include <netiso/iso_snpac.h>
81df8bae1dSRodney W. Grimes #endif
82df8bae1dSRodney W. Grimes 
83655929bfSJulian Elischer /*#ifdef LLC
84df8bae1dSRodney W. Grimes #include <netccitt/dll.h>
85df8bae1dSRodney W. Grimes #include <netccitt/llc_var.h>
86655929bfSJulian Elischer #endif*/
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes #if defined(LLC) && defined(CCITT)
89df8bae1dSRodney W. Grimes extern struct ifqueue pkintrq;
90df8bae1dSRodney W. Grimes #endif
91df8bae1dSRodney W. Grimes 
92655929bfSJulian Elischer #ifdef NETATALK
93655929bfSJulian Elischer #include <netatalk/at.h>
94655929bfSJulian Elischer #include <netatalk/at_var.h>
95655929bfSJulian Elischer #include <netatalk/at_extern.h>
96655929bfSJulian Elischer 
97655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
98655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
99655929bfSJulian Elischer 
100655929bfSJulian Elischer extern u_char	at_org_code[3];
101655929bfSJulian Elischer extern u_char	aarp_org_code[3];
1022cc2df49SGarrett Wollman #endif /* NETATALK */
1032cc2df49SGarrett Wollman 
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 
1091158dfb7SGarrett Wollman static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
1101158dfb7SGarrett Wollman 				    struct sockaddr *));
111df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
112df8bae1dSRodney W. Grimes #define senderr(e) { error = (e); goto bad;}
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes /*
115df8bae1dSRodney W. Grimes  * Ethernet output routine.
116df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
117df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
118df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
119df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
120df8bae1dSRodney W. Grimes  */
121df8bae1dSRodney W. Grimes int
122df8bae1dSRodney W. Grimes ether_output(ifp, m0, dst, rt0)
123df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
124df8bae1dSRodney W. Grimes 	struct mbuf *m0;
125df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
126df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
127df8bae1dSRodney W. Grimes {
128df8bae1dSRodney W. Grimes 	short type;
129df8bae1dSRodney W. Grimes 	int s, error = 0;
1304a11ca4eSPoul-Henning Kamp  	u_char edst[6];
1314a11ca4eSPoul-Henning Kamp 	register struct mbuf *m = m0;
132df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
133df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
134ed7509acSJulian Elischer 	int off, len = m->m_pkthdr.len, loop_copy = 0;
135df8bae1dSRodney W. Grimes 	struct arpcom *ac = (struct arpcom *)ifp;
136df8bae1dSRodney W. Grimes 
137df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
138df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1393bda9f9bSPoul-Henning Kamp 	rt = rt0;
1403bda9f9bSPoul-Henning Kamp 	if (rt) {
141df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1423bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1433bda9f9bSPoul-Henning Kamp 			if (rt0)
144df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
145df8bae1dSRodney W. Grimes 			else
146df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
147df8bae1dSRodney W. Grimes 		}
148df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
149df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
150df8bae1dSRodney W. Grimes 				goto lookup;
151df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
152df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
153995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
154995add1aSGarrett Wollman 							  0UL);
155df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
156df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
157df8bae1dSRodney W. Grimes 			}
158df8bae1dSRodney W. Grimes 		}
159df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
160df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
161227ee8a1SPoul-Henning Kamp 			    time_second < rt->rt_rmx.rmx_expire)
162df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
163df8bae1dSRodney W. Grimes 	}
164df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
165df8bae1dSRodney W. Grimes #ifdef INET
166df8bae1dSRodney W. Grimes 	case AF_INET:
1675df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
168df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
169df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
17034bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
171df8bae1dSRodney W. Grimes 		break;
172df8bae1dSRodney W. Grimes #endif
173cc6a66f2SJulian Elischer #ifdef IPX
174cc6a66f2SJulian Elischer 	case AF_IPX:
17534bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IPX);
176cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
177cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
178cc6a66f2SJulian Elischer 		break;
179cc6a66f2SJulian Elischer #endif
180655929bfSJulian Elischer #ifdef NETATALK
181655929bfSJulian Elischer 	case AF_APPLETALK:
1821d0eab59SJulian Elischer 	  {
183ed7509acSJulian Elischer 	    struct at_ifaddr *aa;
1841d0eab59SJulian Elischer 
185ed7509acSJulian Elischer 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
1861d0eab59SJulian Elischer 		    goto bad;
1871d0eab59SJulian Elischer 	    }
188ed7509acSJulian Elischer 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
1891d0eab59SJulian Elischer 		    return (0);
190655929bfSJulian Elischer 	    /*
191ed7509acSJulian Elischer 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
192655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
193655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
194655929bfSJulian Elischer 	     */
195655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
196655929bfSJulian Elischer 		struct llc llc;
197655929bfSJulian Elischer 
198655929bfSJulian Elischer 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
199655929bfSJulian Elischer 		len += sizeof(struct llc);
200655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
201655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
202655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
203655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
204655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
20534bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
206655929bfSJulian Elischer 	    } else {
20734bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
208655929bfSJulian Elischer 	    }
209655929bfSJulian Elischer 	    break;
210ed7509acSJulian Elischer 	  }
211655929bfSJulian Elischer #endif NETATALK
212df8bae1dSRodney W. Grimes #ifdef NS
213df8bae1dSRodney W. Grimes 	case AF_NS:
21488e038feSJordan K. Hubbard 		switch(ns_nettype){
21588e038feSJordan K. Hubbard 		default:
21688e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
21788e038feSJordan K. Hubbard 			type = 0x8137;
21888e038feSJordan K. Hubbard 			break;
21988e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
22088e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
22188e038feSJordan K. Hubbard 			break;
22288e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
22388e038feSJordan K. Hubbard 			M_PREPEND(m, 3, M_WAIT);
22488e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
22588e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
22688e038feSJordan K. Hubbard 			*cp++ = 0xE0;
22788e038feSJordan K. Hubbard 			*cp++ = 0xE0;
22888e038feSJordan K. Hubbard 			*cp++ = 0x03;
22988e038feSJordan K. Hubbard 			break;
23088e038feSJordan K. Hubbard 		}
231df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
232df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
23388e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
23488e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
23588e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
23688e038feSJordan K. Hubbard 			inq = &nsintrq;
23788e038feSJordan K. Hubbard 			s = splimp();
23888e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
23988e038feSJordan K. Hubbard 				IF_DROP(inq);
24088e038feSJordan K. Hubbard 				m_freem(m);
24188e038feSJordan K. Hubbard 			} else
24288e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m);
24388e038feSJordan K. Hubbard 			splx(s);
24488e038feSJordan K. Hubbard 			return (error);
24588e038feSJordan K. Hubbard 		}
24688e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
24788e038feSJordan K. Hubbard 			m2 = m_copy(m, 0, (int)M_COPYALL);
24888e038feSJordan K. Hubbard 			m2->m_pkthdr.rcvif = ifp;
24988e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
25088e038feSJordan K. Hubbard 			inq = &nsintrq;
25188e038feSJordan K. Hubbard 			s = splimp();
25288e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
25388e038feSJordan K. Hubbard 				IF_DROP(inq);
25488e038feSJordan K. Hubbard 				m_freem(m2);
25588e038feSJordan K. Hubbard 			} else
25688e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m2);
25788e038feSJordan K. Hubbard 			splx(s);
25888e038feSJordan K. Hubbard 		}
259df8bae1dSRodney W. Grimes 		break;
26088e038feSJordan K. Hubbard #endif /* NS */
261df8bae1dSRodney W. Grimes #ifdef	ISO
262df8bae1dSRodney W. Grimes 	case AF_ISO: {
263df8bae1dSRodney W. Grimes 		int	snpalen;
264df8bae1dSRodney W. Grimes 		struct	llc *l;
265df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl;
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
268df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
269df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
270df8bae1dSRodney W. Grimes 		} else if (error =
271df8bae1dSRodney W. Grimes 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
272df8bae1dSRodney W. Grimes 					    (char *)edst, &snpalen))
273df8bae1dSRodney W. Grimes 			goto bad; /* Not Resolved */
274df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
275df8bae1dSRodney W. Grimes 		if (*edst & 1)
276df8bae1dSRodney W. Grimes 			m->m_flags |= (M_BCAST|M_MCAST);
277df8bae1dSRodney W. Grimes 		M_PREPEND(m, 3, M_DONTWAIT);
278df8bae1dSRodney W. Grimes 		if (m == NULL)
279df8bae1dSRodney W. Grimes 			return (0);
28034bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
281df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
282df8bae1dSRodney W. Grimes 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
283df8bae1dSRodney W. Grimes 		l->llc_control = LLC_UI;
284df8bae1dSRodney W. Grimes 		len += 3;
285df8bae1dSRodney W. Grimes 		IFDEBUG(D_ETHER)
286df8bae1dSRodney W. Grimes 			int i;
287df8bae1dSRodney W. Grimes 			printf("unoutput: sending pkt to: ");
288df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
289df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
290df8bae1dSRodney W. Grimes 			printf("\n");
291df8bae1dSRodney W. Grimes 		ENDDEBUG
292df8bae1dSRodney W. Grimes 		} break;
293df8bae1dSRodney W. Grimes #endif /* ISO */
294df8bae1dSRodney W. Grimes #ifdef	LLC
295df8bae1dSRodney W. Grimes /*	case AF_NSAP: */
296df8bae1dSRodney W. Grimes 	case AF_CCITT: {
297df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl =
298df8bae1dSRodney W. Grimes 			(struct sockaddr_dl *) rt -> rt_gateway;
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 		if (sdl && sdl->sdl_family == AF_LINK
301df8bae1dSRodney W. Grimes 		    && sdl->sdl_alen > 0) {
302ed7509acSJulian Elischer 			bcopy(LLADDR(sdl), (char *)edst, sizeof(edst));
303df8bae1dSRodney W. Grimes 		} else goto bad; /* Not a link interface ? Funny ... */
304ed7509acSJulian Elischer 		if (*edst & 1)
305ed7509acSJulian Elischer 			loop_copy = 1;
30634bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
307df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
308df8bae1dSRodney W. Grimes 		{
309df8bae1dSRodney W. Grimes 			int i;
310df8bae1dSRodney W. Grimes 			register struct llc *l = mtod(m, struct llc *);
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 			printf("ether_output: sending LLC2 pkt to: ");
313df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
314df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
315df8bae1dSRodney W. Grimes 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
316df8bae1dSRodney W. Grimes 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
317df8bae1dSRodney W. Grimes 			       l->llc_control & 0xff);
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 		}
320df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
321df8bae1dSRodney W. Grimes 		} break;
322df8bae1dSRodney W. Grimes #endif /* LLC */
323df8bae1dSRodney W. Grimes 
324df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
325df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
32694a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
327df8bae1dSRodney W. Grimes 		type = eh->ether_type;
328df8bae1dSRodney W. Grimes 		break;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	default:
331df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
332df8bae1dSRodney W. Grimes 			dst->sa_family);
333df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
334df8bae1dSRodney W. Grimes 	}
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
338df8bae1dSRodney W. Grimes 	 * allocate another.
339df8bae1dSRodney W. Grimes 	 */
340df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
341df8bae1dSRodney W. Grimes 	if (m == 0)
342df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
343df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
34494a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
345df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
34694a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
34794a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_shost, ac->ac_enaddr,
348df8bae1dSRodney W. Grimes 	    sizeof(eh->ether_shost));
349ed7509acSJulian Elischer 
350ed7509acSJulian Elischer 	/*
351ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
352ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
353ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
354ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
355ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
356ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
357ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
358ed7509acSJulian Elischer 	 */
359ed7509acSJulian Elischer 	if (ifp->if_flags & IFF_SIMPLEX) {
360ed7509acSJulian Elischer 		if ((m->m_flags & M_BCAST) || loop_copy) {
361ed7509acSJulian Elischer 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
362ed7509acSJulian Elischer 
363ed7509acSJulian Elischer 			(void) if_simloop(ifp, n, dst, ETHER_HDR_LEN);
364ed7509acSJulian Elischer 		} else if (bcmp(eh->ether_dhost,
365ed7509acSJulian Elischer 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
366ed7509acSJulian Elischer 			(void) if_simloop(ifp, m, dst, ETHER_HDR_LEN);
367ed7509acSJulian Elischer 			return(0);	/* XXX */
368ed7509acSJulian Elischer 		}
369ed7509acSJulian Elischer 	}
370ed7509acSJulian Elischer 
371df8bae1dSRodney W. Grimes 	s = splimp();
372df8bae1dSRodney W. Grimes 	/*
373df8bae1dSRodney W. Grimes 	 * Queue message on interface, and start output if interface
374df8bae1dSRodney W. Grimes 	 * not yet active.
375df8bae1dSRodney W. Grimes 	 */
376df8bae1dSRodney W. Grimes 	if (IF_QFULL(&ifp->if_snd)) {
377df8bae1dSRodney W. Grimes 		IF_DROP(&ifp->if_snd);
378df8bae1dSRodney W. Grimes 		splx(s);
379df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
380df8bae1dSRodney W. Grimes 	}
381df8bae1dSRodney W. Grimes 	IF_ENQUEUE(&ifp->if_snd, m);
382df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
383df8bae1dSRodney W. Grimes 		(*ifp->if_start)(ifp);
384df8bae1dSRodney W. Grimes 	splx(s);
385df8bae1dSRodney W. Grimes 	ifp->if_obytes += len + sizeof (struct ether_header);
386df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST)
387df8bae1dSRodney W. Grimes 		ifp->if_omcasts++;
388df8bae1dSRodney W. Grimes 	return (error);
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes bad:
391df8bae1dSRodney W. Grimes 	if (m)
392df8bae1dSRodney W. Grimes 		m_freem(m);
393df8bae1dSRodney W. Grimes 	return (error);
394df8bae1dSRodney W. Grimes }
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes /*
397df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
398df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
399df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
400df8bae1dSRodney W. Grimes  */
401df8bae1dSRodney W. Grimes void
402df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
403df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
404df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
405df8bae1dSRodney W. Grimes 	struct mbuf *m;
406df8bae1dSRodney W. Grimes {
407df8bae1dSRodney W. Grimes 	register struct ifqueue *inq;
4084a11ca4eSPoul-Henning Kamp 	u_short ether_type;
409df8bae1dSRodney W. Grimes 	int s;
4108e3bda06SJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
411c23670e2SGary Palmer 	register struct llc *l;
412c23670e2SGary Palmer #endif
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
415df8bae1dSRodney W. Grimes 		m_freem(m);
416df8bae1dSRodney W. Grimes 		return;
417df8bae1dSRodney W. Grimes 	}
418df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
4192cc2df49SGarrett Wollman 	if (eh->ether_dhost[0] & 1) {
420df8bae1dSRodney W. Grimes 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
421df8bae1dSRodney W. Grimes 			 sizeof(etherbroadcastaddr)) == 0)
422df8bae1dSRodney W. Grimes 			m->m_flags |= M_BCAST;
4232cc2df49SGarrett Wollman 		else
424df8bae1dSRodney W. Grimes 			m->m_flags |= M_MCAST;
4252cc2df49SGarrett Wollman 	}
426df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
427df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
428df8bae1dSRodney W. Grimes 
429307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
430307d80beSDavid Greenman 
4312cc2df49SGarrett Wollman #if NVLAN > 0
4322cc2df49SGarrett Wollman 	if (ether_type == vlan_proto) {
4332cc2df49SGarrett Wollman 		if (vlan_input(eh, m) < 0)
4342cc2df49SGarrett Wollman 			ifp->if_data.ifi_noproto++;
4352cc2df49SGarrett Wollman 		return;
4362cc2df49SGarrett Wollman 	}
4372cc2df49SGarrett Wollman #endif /* NVLAN > 0 */
4382cc2df49SGarrett Wollman 
439307d80beSDavid Greenman 	switch (ether_type) {
440df8bae1dSRodney W. Grimes #ifdef INET
441df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
4421f91d8c5SDavid Greenman 		if (ipflow_fastforward(m))
4431f91d8c5SDavid Greenman 			return;
444df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
445df8bae1dSRodney W. Grimes 		inq = &ipintrq;
446df8bae1dSRodney W. Grimes 		break;
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
449df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
450df8bae1dSRodney W. Grimes 		inq = &arpintrq;
451df8bae1dSRodney W. Grimes 		break;
452df8bae1dSRodney W. Grimes #endif
453cc6a66f2SJulian Elischer #ifdef IPX
454cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
455cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
456cc6a66f2SJulian Elischer 		inq = &ipxintrq;
457cc6a66f2SJulian Elischer 		break;
458cc6a66f2SJulian Elischer #endif
459df8bae1dSRodney W. Grimes #ifdef NS
46088e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
461df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
462df8bae1dSRodney W. Grimes 		inq = &nsintrq;
463df8bae1dSRodney W. Grimes 		break;
46488e038feSJordan K. Hubbard 
46588e038feSJordan K. Hubbard #endif /* NS */
466655929bfSJulian Elischer #ifdef NETATALK
467655929bfSJulian Elischer         case ETHERTYPE_AT:
468655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
469655929bfSJulian Elischer                 inq = &atintrq1;
470655929bfSJulian Elischer                 break;
471655929bfSJulian Elischer         case ETHERTYPE_AARP:
472655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
473655929bfSJulian Elischer                 aarpinput((struct arpcom *)ifp, m); /* XXX */
474655929bfSJulian Elischer                 return;
475655929bfSJulian Elischer #endif NETATALK
476df8bae1dSRodney W. Grimes 	default:
47788e038feSJordan K. Hubbard #ifdef NS
47888e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
47988e038feSJordan K. Hubbard 		/* Novell 802.3 */
48088e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
48188e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
48288e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
48388e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
48488e038feSJordan K. Hubbard 				m->m_len -= 3;
48588e038feSJordan K. Hubbard 				m->m_data += 3;
48688e038feSJordan K. Hubbard 			}
48788e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
48888e038feSJordan K. Hubbard 				inq = &nsintrq;
48988e038feSJordan K. Hubbard 				break;
49088e038feSJordan K. Hubbard 		}
49188e038feSJordan K. Hubbard #endif /* NS */
492655929bfSJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
493307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
494df8bae1dSRodney W. Grimes 			goto dropanyway;
495df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
496df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
497655929bfSJulian Elischer #ifdef NETATALK
498655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
499655929bfSJulian Elischer 		    switch (l->llc_control) {
500655929bfSJulian Elischer 		    case LLC_UI:
501655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
502655929bfSJulian Elischer 			    goto dropanyway;
503655929bfSJulian Elischer 
504655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
505655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
506655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
507655929bfSJulian Elischer 			    inq = &atintrq2;
508655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
509655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
510655929bfSJulian Elischer 			    break;
511655929bfSJulian Elischer 			}
512655929bfSJulian Elischer 
513655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
514655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
515655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
516655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
517655929bfSJulian Elischer 			    aarpinput((struct arpcom *)ifp, m); /* XXX */
518655929bfSJulian Elischer 			    return;
519655929bfSJulian Elischer 			}
520655929bfSJulian Elischer 
521655929bfSJulian Elischer 		    default:
522655929bfSJulian Elischer 			goto dropanyway;
523655929bfSJulian Elischer 		    }
524655929bfSJulian Elischer 		    break;
525655929bfSJulian Elischer #endif NETATALK
526df8bae1dSRodney W. Grimes #ifdef	ISO
527df8bae1dSRodney W. Grimes 		case LLC_ISO_LSAP:
528df8bae1dSRodney W. Grimes 			switch (l->llc_control) {
529df8bae1dSRodney W. Grimes 			case LLC_UI:
530df8bae1dSRodney W. Grimes 				/* LLC_UI_P forbidden in class 1 service */
531df8bae1dSRodney W. Grimes 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
532df8bae1dSRodney W. Grimes 				    (l->llc_ssap == LLC_ISO_LSAP)) {
533df8bae1dSRodney W. Grimes 					/* LSAP for ISO */
534307d80beSDavid Greenman 					if (m->m_pkthdr.len > ether_type)
535307d80beSDavid Greenman 						m_adj(m, ether_type - m->m_pkthdr.len);
536df8bae1dSRodney W. Grimes 					m->m_data += 3;		/* XXX */
537df8bae1dSRodney W. Grimes 					m->m_len -= 3;		/* XXX */
538df8bae1dSRodney W. Grimes 					m->m_pkthdr.len -= 3;	/* XXX */
539df8bae1dSRodney W. Grimes 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
540df8bae1dSRodney W. Grimes 					if (m == 0)
541df8bae1dSRodney W. Grimes 						return;
542df8bae1dSRodney W. Grimes 					*mtod(m, struct ether_header *) = *eh;
543df8bae1dSRodney W. Grimes 					IFDEBUG(D_ETHER)
544df8bae1dSRodney W. Grimes 						printf("clnp packet");
545df8bae1dSRodney W. Grimes 					ENDDEBUG
546df8bae1dSRodney W. Grimes 					schednetisr(NETISR_ISO);
547df8bae1dSRodney W. Grimes 					inq = &clnlintrq;
548df8bae1dSRodney W. Grimes 					break;
549df8bae1dSRodney W. Grimes 				}
550df8bae1dSRodney W. Grimes 				goto dropanyway;
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes 			case LLC_XID:
553df8bae1dSRodney W. Grimes 			case LLC_XID_P:
554df8bae1dSRodney W. Grimes 				if(m->m_len < 6)
555df8bae1dSRodney W. Grimes 					goto dropanyway;
556df8bae1dSRodney W. Grimes 				l->llc_window = 0;
557df8bae1dSRodney W. Grimes 				l->llc_fid = 9;
558df8bae1dSRodney W. Grimes 				l->llc_class = 1;
559df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap = 0;
560df8bae1dSRodney W. Grimes 				/* Fall through to */
561df8bae1dSRodney W. Grimes 			case LLC_TEST:
562df8bae1dSRodney W. Grimes 			case LLC_TEST_P:
563df8bae1dSRodney W. Grimes 			{
564df8bae1dSRodney W. Grimes 				struct sockaddr sa;
565df8bae1dSRodney W. Grimes 				register struct ether_header *eh2;
566df8bae1dSRodney W. Grimes 				int i;
567df8bae1dSRodney W. Grimes 				u_char c = l->llc_dsap;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap;
570df8bae1dSRodney W. Grimes 				l->llc_ssap = c;
571df8bae1dSRodney W. Grimes 				if (m->m_flags & (M_BCAST | M_MCAST))
572df8bae1dSRodney W. Grimes 					bcopy((caddr_t)ac->ac_enaddr,
573df8bae1dSRodney W. Grimes 					      (caddr_t)eh->ether_dhost, 6);
574df8bae1dSRodney W. Grimes 				sa.sa_family = AF_UNSPEC;
575df8bae1dSRodney W. Grimes 				sa.sa_len = sizeof(sa);
576df8bae1dSRodney W. Grimes 				eh2 = (struct ether_header *)sa.sa_data;
577df8bae1dSRodney W. Grimes 				for (i = 0; i < 6; i++) {
578df8bae1dSRodney W. Grimes 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
579df8bae1dSRodney W. Grimes 					eh2->ether_dhost[i] =
580df8bae1dSRodney W. Grimes 						eh->ether_dhost[i] = eh->ether_shost[i];
581df8bae1dSRodney W. Grimes 					eh->ether_shost[i] = c;
582df8bae1dSRodney W. Grimes 				}
583df8bae1dSRodney W. Grimes 				ifp->if_output(ifp, m, &sa, NULL);
584df8bae1dSRodney W. Grimes 				return;
585df8bae1dSRodney W. Grimes 			}
586df8bae1dSRodney W. Grimes 			default:
587df8bae1dSRodney W. Grimes 				m_freem(m);
588df8bae1dSRodney W. Grimes 				return;
589df8bae1dSRodney W. Grimes 			}
590df8bae1dSRodney W. Grimes 			break;
591df8bae1dSRodney W. Grimes #endif /* ISO */
592df8bae1dSRodney W. Grimes #ifdef LLC
593df8bae1dSRodney W. Grimes 		case LLC_X25_LSAP:
594df8bae1dSRodney W. Grimes 		{
595307d80beSDavid Greenman 			if (m->m_pkthdr.len > ether_type)
596307d80beSDavid Greenman 				m_adj(m, ether_type - m->m_pkthdr.len);
597df8bae1dSRodney W. Grimes 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
598df8bae1dSRodney W. Grimes 			if (m == 0)
599df8bae1dSRodney W. Grimes 				return;
600df8bae1dSRodney W. Grimes 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
601df8bae1dSRodney W. Grimes 					    eh->ether_dhost, LLC_X25_LSAP, 6,
602df8bae1dSRodney W. Grimes 					    mtod(m, struct sdl_hdr *)))
603df8bae1dSRodney W. Grimes 				panic("ETHER cons addr failure");
604307d80beSDavid Greenman 			mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
605df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
606df8bae1dSRodney W. Grimes 				printf("llc packet\n");
607df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
608df8bae1dSRodney W. Grimes 			schednetisr(NETISR_CCITT);
609df8bae1dSRodney W. Grimes 			inq = &llcintrq;
610df8bae1dSRodney W. Grimes 			break;
611df8bae1dSRodney W. Grimes 		}
612df8bae1dSRodney W. Grimes #endif /* LLC */
613df8bae1dSRodney W. Grimes 		dropanyway:
614df8bae1dSRodney W. Grimes 		default:
615df8bae1dSRodney W. Grimes 			m_freem(m);
616df8bae1dSRodney W. Grimes 			return;
617df8bae1dSRodney W. Grimes 		}
618655929bfSJulian Elischer #else /* ISO || LLC || NETATALK */
619df8bae1dSRodney W. Grimes 	    m_freem(m);
620df8bae1dSRodney W. Grimes 	    return;
621655929bfSJulian Elischer #endif /* ISO || LLC || NETATALK */
622df8bae1dSRodney W. Grimes 	}
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes 	s = splimp();
625df8bae1dSRodney W. Grimes 	if (IF_QFULL(inq)) {
626df8bae1dSRodney W. Grimes 		IF_DROP(inq);
627df8bae1dSRodney W. Grimes 		m_freem(m);
628df8bae1dSRodney W. Grimes 	} else
629df8bae1dSRodney W. Grimes 		IF_ENQUEUE(inq, m);
630df8bae1dSRodney W. Grimes 	splx(s);
631df8bae1dSRodney W. Grimes }
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes /*
634df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
635df8bae1dSRodney W. Grimes  */
636df8bae1dSRodney W. Grimes void
637df8bae1dSRodney W. Grimes ether_ifattach(ifp)
638df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
639df8bae1dSRodney W. Grimes {
640df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
641df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
644df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
645df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
646df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
6471158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
648a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
649a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
65059562606SGarrett Wollman 	ifa = ifnet_addrs[ifp->if_index - 1];
65159562606SGarrett Wollman 	if (ifa == 0) {
65259562606SGarrett Wollman 		printf("ether_ifattach: no lladdr!\n");
65359562606SGarrett Wollman 		return;
65459562606SGarrett Wollman 	}
65559562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
656df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
657df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
65859562606SGarrett Wollman 	bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
659df8bae1dSRodney W. Grimes }
660df8bae1dSRodney W. Grimes 
661602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
66230106f6aSPoul-Henning Kamp 
663fb583156SDavid Greenman int
664c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
665c5a1016bSBruce Evans 	struct ifnet *ifp;
666c5a1016bSBruce Evans 	int command;
667c5a1016bSBruce Evans 	caddr_t data;
66830106f6aSPoul-Henning Kamp {
66930106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
67030106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
671fb583156SDavid Greenman 	int error = 0;
67230106f6aSPoul-Henning Kamp 
67330106f6aSPoul-Henning Kamp 	switch (command) {
67430106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
67530106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
67630106f6aSPoul-Henning Kamp 
67730106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
67830106f6aSPoul-Henning Kamp #ifdef INET
67930106f6aSPoul-Henning Kamp 		case AF_INET:
68030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
68130106f6aSPoul-Henning Kamp 			arp_ifinit((struct arpcom *)ifp, ifa);
68230106f6aSPoul-Henning Kamp 			break;
68330106f6aSPoul-Henning Kamp #endif
68430106f6aSPoul-Henning Kamp #ifdef IPX
68530106f6aSPoul-Henning Kamp 		/*
68630106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
68730106f6aSPoul-Henning Kamp 		 */
68830106f6aSPoul-Henning Kamp 		case AF_IPX:
68930106f6aSPoul-Henning Kamp 			{
69030106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
69186101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
69230106f6aSPoul-Henning Kamp 
69330106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
69430106f6aSPoul-Henning Kamp 				ina->x_host =
69586101139SPoul-Henning Kamp 				    *(union ipx_host *)
69686101139SPoul-Henning Kamp 			            ac->ac_enaddr;
69730106f6aSPoul-Henning Kamp 			else {
69830106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
69986101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
70086101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
70130106f6aSPoul-Henning Kamp 			}
70230106f6aSPoul-Henning Kamp 
70330106f6aSPoul-Henning Kamp 			/*
70430106f6aSPoul-Henning Kamp 			 * Set new address
70530106f6aSPoul-Henning Kamp 			 */
70630106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
70730106f6aSPoul-Henning Kamp 			break;
70830106f6aSPoul-Henning Kamp 			}
70930106f6aSPoul-Henning Kamp #endif
71030106f6aSPoul-Henning Kamp #ifdef NS
71130106f6aSPoul-Henning Kamp 		/*
71230106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
71330106f6aSPoul-Henning Kamp 		 */
71430106f6aSPoul-Henning Kamp 		case AF_NS:
71530106f6aSPoul-Henning Kamp 		{
71630106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
71786101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
71830106f6aSPoul-Henning Kamp 
71930106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
72030106f6aSPoul-Henning Kamp 				ina->x_host =
72186101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
72230106f6aSPoul-Henning Kamp 			else {
72330106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
72486101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
72586101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
72630106f6aSPoul-Henning Kamp 			}
72730106f6aSPoul-Henning Kamp 
72830106f6aSPoul-Henning Kamp 			/*
72930106f6aSPoul-Henning Kamp 			 * Set new address
73030106f6aSPoul-Henning Kamp 			 */
73130106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
73230106f6aSPoul-Henning Kamp 			break;
73330106f6aSPoul-Henning Kamp 		}
73430106f6aSPoul-Henning Kamp #endif
73530106f6aSPoul-Henning Kamp 		default:
73630106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
73730106f6aSPoul-Henning Kamp 			break;
73830106f6aSPoul-Henning Kamp 		}
73930106f6aSPoul-Henning Kamp 		break;
74030106f6aSPoul-Henning Kamp 
74130106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
74230106f6aSPoul-Henning Kamp 		{
74330106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
74430106f6aSPoul-Henning Kamp 
74530106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
7465b73c186SDavid Greenman 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
74730106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
74830106f6aSPoul-Henning Kamp 		}
74930106f6aSPoul-Henning Kamp 		break;
750fb583156SDavid Greenman 
751fb583156SDavid Greenman 	case SIOCSIFMTU:
752fb583156SDavid Greenman 		/*
753fb583156SDavid Greenman 		 * Set the interface MTU.
754fb583156SDavid Greenman 		 */
755fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
756fb583156SDavid Greenman 			error = EINVAL;
757fb583156SDavid Greenman 		} else {
758fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
75930106f6aSPoul-Henning Kamp 		}
760fb583156SDavid Greenman 		break;
761fb583156SDavid Greenman 	}
762fb583156SDavid Greenman 	return (error);
76330106f6aSPoul-Henning Kamp }
7641158dfb7SGarrett Wollman 
7651158dfb7SGarrett Wollman int
7661158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
7671158dfb7SGarrett Wollman 	struct ifnet *ifp;
7681158dfb7SGarrett Wollman 	struct sockaddr **llsa;
7691158dfb7SGarrett Wollman 	struct sockaddr *sa;
7701158dfb7SGarrett Wollman {
7711158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
7721158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
7731158dfb7SGarrett Wollman 	u_char *e_addr;
7741158dfb7SGarrett Wollman 
7751158dfb7SGarrett Wollman 	switch(sa->sa_family) {
7761158dfb7SGarrett Wollman 	case AF_LINK:
7777f33a738SJulian Elischer 		/*
7787f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
7797f33a738SJulian Elischer 		 */
7801158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
7811158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
7821158dfb7SGarrett Wollman 		if ((e_addr[0] & 1) != 1)
7831158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
7841158dfb7SGarrett Wollman 		*llsa = 0;
7851158dfb7SGarrett Wollman 		return 0;
7861158dfb7SGarrett Wollman 
7871158dfb7SGarrett Wollman #ifdef INET
7881158dfb7SGarrett Wollman 	case AF_INET:
7891158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
7901158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
7911158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
7921158dfb7SGarrett Wollman 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
7931158dfb7SGarrett Wollman 		       M_WAITOK);
7941158dfb7SGarrett Wollman 		sdl->sdl_len = sizeof *sdl;
7951158dfb7SGarrett Wollman 		sdl->sdl_family = AF_LINK;
7961158dfb7SGarrett Wollman 		sdl->sdl_index = ifp->if_index;
7971158dfb7SGarrett Wollman 		sdl->sdl_type = IFT_ETHER;
7981158dfb7SGarrett Wollman 		sdl->sdl_nlen = 0;
7991158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
8001158dfb7SGarrett Wollman 		sdl->sdl_slen = 0;
8011158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
8021158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
8031158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
8041158dfb7SGarrett Wollman 		return 0;
8051158dfb7SGarrett Wollman #endif
8061158dfb7SGarrett Wollman 
8071158dfb7SGarrett Wollman 	default:
8081158dfb7SGarrett Wollman 		/*
8091158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
8101158dfb7SGarrett Wollman 		 * that counts...
8111158dfb7SGarrett Wollman 		 */
8121158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
8131158dfb7SGarrett Wollman 	}
8141158dfb7SGarrett Wollman }
815