xref: /freebsd/sys/net/if_ethersubr.c (revision 1d5e9e22)
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
341d5e9e22SEivind Eklund  * $Id: if_ethersubr.c,v 1.41 1997/12/20 00:07:02 bde Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
371d5e9e22SEivind Eklund #include "opt_inet.h"
38430df5f4SEivind Eklund #include "opt_ipx.h"
39430df5f4SEivind Eklund 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
4651a53488SBruce Evans #include <sys/sockio.h>
47602d513cSGarrett Wollman #include <sys/sysctl.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
50df8bae1dSRodney W. Grimes #include <net/netisr.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52df8bae1dSRodney W. Grimes #include <net/if_llc.h>
53df8bae1dSRodney W. Grimes #include <net/if_dl.h>
54df8bae1dSRodney W. Grimes #include <net/if_types.h>
55df8bae1dSRodney W. Grimes 
561d5e9e22SEivind Eklund #include <net/ethernet.h>
571d5e9e22SEivind Eklund #include <net/if_arp.h>
581d5e9e22SEivind Eklund 
59df8bae1dSRodney W. Grimes #ifdef INET
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
62df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
631d5e9e22SEivind Eklund #endif
64df8bae1dSRodney W. Grimes 
65cc6a66f2SJulian Elischer #ifdef IPX
66cc6a66f2SJulian Elischer #include <netipx/ipx.h>
67cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
68cc6a66f2SJulian Elischer #endif
69cc6a66f2SJulian Elischer 
70df8bae1dSRodney W. Grimes #ifdef NS
71df8bae1dSRodney W. Grimes #include <netns/ns.h>
72df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
73d0ec898dSJordan K. Hubbard ushort ns_nettype;
7488e038feSJordan K. Hubbard int ether_outputdebug = 0;
7588e038feSJordan K. Hubbard int ether_inputdebug = 0;
76df8bae1dSRodney W. Grimes #endif
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #ifdef ISO
79df8bae1dSRodney W. Grimes #include <netiso/argo_debug.h>
80df8bae1dSRodney W. Grimes #include <netiso/iso.h>
81df8bae1dSRodney W. Grimes #include <netiso/iso_var.h>
82df8bae1dSRodney W. Grimes #include <netiso/iso_snpac.h>
83df8bae1dSRodney W. Grimes #endif
84df8bae1dSRodney W. Grimes 
85655929bfSJulian Elischer /*#ifdef LLC
86df8bae1dSRodney W. Grimes #include <netccitt/dll.h>
87df8bae1dSRodney W. Grimes #include <netccitt/llc_var.h>
88655929bfSJulian Elischer #endif*/
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes #if defined(LLC) && defined(CCITT)
91df8bae1dSRodney W. Grimes extern struct ifqueue pkintrq;
92df8bae1dSRodney W. Grimes #endif
93df8bae1dSRodney W. Grimes 
94655929bfSJulian Elischer #ifdef NETATALK
95655929bfSJulian Elischer #include <netatalk/at.h>
96655929bfSJulian Elischer #include <netatalk/at_var.h>
97655929bfSJulian Elischer #include <netatalk/at_extern.h>
98655929bfSJulian Elischer 
99655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
100655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
101655929bfSJulian Elischer 
102655929bfSJulian Elischer extern u_char	at_org_code[ 3 ];
103655929bfSJulian Elischer extern u_char	aarp_org_code[ 3 ];
104655929bfSJulian Elischer #endif NETATALK
105655929bfSJulian Elischer 
1061158dfb7SGarrett Wollman static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
1071158dfb7SGarrett Wollman 				    struct sockaddr *));
108df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
109df8bae1dSRodney W. Grimes #define senderr(e) { error = (e); goto bad;}
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes /*
112df8bae1dSRodney W. Grimes  * Ethernet output routine.
113df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
114df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
115df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
116df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
117df8bae1dSRodney W. Grimes  */
118df8bae1dSRodney W. Grimes int
119df8bae1dSRodney W. Grimes ether_output(ifp, m0, dst, rt0)
120df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
121df8bae1dSRodney W. Grimes 	struct mbuf *m0;
122df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
123df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
124df8bae1dSRodney W. Grimes {
125df8bae1dSRodney W. Grimes 	short type;
126df8bae1dSRodney W. Grimes 	int s, error = 0;
1274a11ca4eSPoul-Henning Kamp  	u_char edst[6];
1284a11ca4eSPoul-Henning Kamp 	register struct mbuf *m = m0;
129df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
130df8bae1dSRodney W. Grimes 	struct mbuf *mcopy = (struct mbuf *)0;
131df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
132df8bae1dSRodney W. Grimes 	int off, len = m->m_pkthdr.len;
133df8bae1dSRodney W. Grimes 	struct arpcom *ac = (struct arpcom *)ifp;
134655929bfSJulian Elischer #ifdef NETATALK
135655929bfSJulian Elischer 	struct at_ifaddr *aa;
136655929bfSJulian Elischer #endif NETATALK
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
139df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1403bda9f9bSPoul-Henning Kamp 	rt = rt0;
1413bda9f9bSPoul-Henning Kamp 	if (rt) {
142df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1433bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1443bda9f9bSPoul-Henning Kamp 			if (rt0)
145df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
146df8bae1dSRodney W. Grimes 			else
147df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
148df8bae1dSRodney W. Grimes 		}
149df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
150df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
151df8bae1dSRodney W. Grimes 				goto lookup;
152df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
153df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
154995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
155995add1aSGarrett Wollman 							  0UL);
156df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
157df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
158df8bae1dSRodney W. Grimes 			}
159df8bae1dSRodney W. Grimes 		}
160df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
161df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
162df8bae1dSRodney W. Grimes 			    time.tv_sec < rt->rt_rmx.rmx_expire)
163df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
164df8bae1dSRodney W. Grimes 	}
165df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes #ifdef INET
168df8bae1dSRodney W. Grimes 	case AF_INET:
1695df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
170df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
171df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
172df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
173df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
174df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
17534bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
176df8bae1dSRodney W. Grimes 		break;
177df8bae1dSRodney W. Grimes #endif
178cc6a66f2SJulian Elischer #ifdef IPX
179cc6a66f2SJulian Elischer 	case AF_IPX:
180b1c9d77eSJohn Hay 		{
181b1c9d77eSJohn Hay 		struct ifaddr *ia;
182b1c9d77eSJohn Hay 
18334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IPX);
184cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
185cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
186b1c9d77eSJohn Hay 
187b1c9d77eSJohn Hay 		for(ia = ifp->if_addrhead.tqh_first; ia != 0;
188b1c9d77eSJohn Hay 		    ia = ia->ifa_link.tqe_next) {
189b1c9d77eSJohn Hay 			if(ia->ifa_addr->sa_family == AF_IPX &&
190b1c9d77eSJohn Hay 			    !bcmp((caddr_t)edst,
191b1c9d77eSJohn Hay 				  (caddr_t)&((struct ipx_ifaddr *)ia)->ia_addr.sipx_addr.x_host,
192b1c9d77eSJohn Hay 				  sizeof(edst)) )
193cc6a66f2SJulian Elischer 				return (looutput(ifp, m, dst, rt));
194b1c9d77eSJohn Hay 			}
195b1c9d77eSJohn Hay 
196cc6a66f2SJulian Elischer 		/* If broadcasting on a simplex interface, loopback a copy */
197cc6a66f2SJulian Elischer 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
198cc6a66f2SJulian Elischer 			mcopy = m_copy(m, 0, (int)M_COPYALL);
199cc6a66f2SJulian Elischer 		break;
200b1c9d77eSJohn Hay 		}
201cc6a66f2SJulian Elischer #endif
202655929bfSJulian Elischer #ifdef NETATALK
203655929bfSJulian Elischer 	case AF_APPLETALK:
2041d0eab59SJulian Elischer 	    {
2051d0eab59SJulian Elischer 		struct sockaddr_at *sat = (struct sockaddr_at *)dst;
2061d0eab59SJulian Elischer 
2071d0eab59SJulian Elischer 		/*
2081d0eab59SJulian Elischer 		 * super hack..
2091d0eab59SJulian Elischer 		 * Most of this loopback code should move into the appletalk
2101d0eab59SJulian Elischer 		 * code, but it's here for now.. remember to move it! [JRE]
2111d0eab59SJulian Elischer 		 * This may not get the same interface we started with,
2121d0eab59SJulian Elischer 		 * fix asap. XXX
2131d0eab59SJulian Elischer 		 */
2141d0eab59SJulian Elischer 		aa = at_ifawithnet( sat );
2151d0eab59SJulian Elischer 		if (aa == NULL) {
2161d0eab59SJulian Elischer 			goto bad;
2171d0eab59SJulian Elischer 		}
2181d0eab59SJulian Elischer 		if( aa->aa_ifa.ifa_ifp != ifp ) {
2191d0eab59SJulian Elischer 			(*aa->aa_ifa.ifa_ifp->if_output)(aa->aa_ifa.ifa_ifp,
2201d0eab59SJulian Elischer 							m,dst,rt);
2211d0eab59SJulian Elischer 		}
2221d0eab59SJulian Elischer 		if (((sat->sat_addr.s_net == ATADDR_ANYNET)
2231d0eab59SJulian Elischer 		  && (sat->sat_addr.s_node == ATADDR_ANYNODE))
2241d0eab59SJulian Elischer 		|| ((sat->sat_addr.s_net == aa->aa_addr.sat_addr.s_net )
2251d0eab59SJulian Elischer 		  && (sat->sat_addr.s_node == aa->aa_addr.sat_addr.s_node))) {
2261d0eab59SJulian Elischer 			(void) looutput(ifp, m, dst, rt);
2271d0eab59SJulian Elischer 			return(0);
2281d0eab59SJulian Elischer 		}
2291d0eab59SJulian Elischer 
230655929bfSJulian Elischer         	if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
231655929bfSJulian Elischer #ifdef NETATALKDEBUG
232655929bfSJulian Elischer                 	extern char *prsockaddr(struct sockaddr *);
233655929bfSJulian Elischer                 	printf("aarpresolv: failed for %s\n", prsockaddr(dst));
234655929bfSJulian Elischer #endif NETATALKDEBUG
235655929bfSJulian Elischer                 	return (0);
236655929bfSJulian Elischer         	}
237655929bfSJulian Elischer 
238655929bfSJulian Elischer 		/*
2391d0eab59SJulian Elischer 		 * If broadcasting on a simplex interface, loopback a copy
2401d0eab59SJulian Elischer 		 */
2411d0eab59SJulian Elischer 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
2421d0eab59SJulian Elischer 			mcopy = m_copy(m, 0, (int)M_COPYALL);
2431d0eab59SJulian Elischer 	    }
2441d0eab59SJulian Elischer 	    /*
245655929bfSJulian Elischer 	     * In the phase 2 case, we need to prepend an mbuf for the llc header.
246655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
247655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
248655929bfSJulian Elischer 	     */
249655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
250655929bfSJulian Elischer 		struct llc llc;
251655929bfSJulian Elischer 
252655929bfSJulian Elischer 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
253655929bfSJulian Elischer 		len += sizeof(struct llc);
254655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
255655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
256655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
257655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
258655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
25934bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
260655929bfSJulian Elischer 	    } else {
26134bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
262655929bfSJulian Elischer 	    }
263655929bfSJulian Elischer 	    break;
264655929bfSJulian Elischer #endif NETATALK
265df8bae1dSRodney W. Grimes #ifdef NS
266df8bae1dSRodney W. Grimes 	case AF_NS:
26788e038feSJordan K. Hubbard 		switch(ns_nettype){
26888e038feSJordan K. Hubbard 		default:
26988e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
27088e038feSJordan K. Hubbard 			type = 0x8137;
27188e038feSJordan K. Hubbard 			break;
27288e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
27388e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
27488e038feSJordan K. Hubbard 			break;
27588e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
27688e038feSJordan K. Hubbard 			M_PREPEND(m, 3, M_WAIT);
27788e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
27888e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
27988e038feSJordan K. Hubbard 			*cp++ = 0xE0;
28088e038feSJordan K. Hubbard 			*cp++ = 0xE0;
28188e038feSJordan K. Hubbard 			*cp++ = 0x03;
28288e038feSJordan K. Hubbard 			break;
28388e038feSJordan K. Hubbard 		}
284df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
285df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
28688e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
28788e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
28888e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
28988e038feSJordan K. Hubbard 			inq = &nsintrq;
29088e038feSJordan K. Hubbard 			s = splimp();
29188e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
29288e038feSJordan K. Hubbard 				IF_DROP(inq);
29388e038feSJordan K. Hubbard 				m_freem(m);
29488e038feSJordan K. Hubbard 			} else
29588e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m);
29688e038feSJordan K. Hubbard 			splx(s);
29788e038feSJordan K. Hubbard 			return (error);
29888e038feSJordan K. Hubbard 		}
29988e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
30088e038feSJordan K. Hubbard 			m2 = m_copy(m, 0, (int)M_COPYALL);
30188e038feSJordan K. Hubbard 			m2->m_pkthdr.rcvif = ifp;
30288e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
30388e038feSJordan K. Hubbard 			inq = &nsintrq;
30488e038feSJordan K. Hubbard 			s = splimp();
30588e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
30688e038feSJordan K. Hubbard 				IF_DROP(inq);
30788e038feSJordan K. Hubbard 				m_freem(m2);
30888e038feSJordan K. Hubbard 			} else
30988e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m2);
31088e038feSJordan K. Hubbard 			splx(s);
31188e038feSJordan K. Hubbard 		}
312df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
31388e038feSJordan K. Hubbard 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)){
314df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
31588e038feSJordan K. Hubbard 		}
316df8bae1dSRodney W. Grimes 		break;
31788e038feSJordan K. Hubbard #endif /* NS */
318df8bae1dSRodney W. Grimes #ifdef	ISO
319df8bae1dSRodney W. Grimes 	case AF_ISO: {
320df8bae1dSRodney W. Grimes 		int	snpalen;
321df8bae1dSRodney W. Grimes 		struct	llc *l;
322df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl;
323df8bae1dSRodney W. Grimes 
324df8bae1dSRodney W. Grimes 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
325df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
326df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
327df8bae1dSRodney W. Grimes 		} else if (error =
328df8bae1dSRodney W. Grimes 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
329df8bae1dSRodney W. Grimes 					    (char *)edst, &snpalen))
330df8bae1dSRodney W. Grimes 			goto bad; /* Not Resolved */
331df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
332df8bae1dSRodney W. Grimes 		if (*edst & 1)
333df8bae1dSRodney W. Grimes 			m->m_flags |= (M_BCAST|M_MCAST);
334df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
335df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
336df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
337df8bae1dSRodney W. Grimes 			if (mcopy) {
338df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
339df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
340df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
341df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
342df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
343df8bae1dSRodney W. Grimes 			}
344df8bae1dSRodney W. Grimes 		}
345df8bae1dSRodney W. Grimes 		M_PREPEND(m, 3, M_DONTWAIT);
346df8bae1dSRodney W. Grimes 		if (m == NULL)
347df8bae1dSRodney W. Grimes 			return (0);
34834bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
349df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
350df8bae1dSRodney W. Grimes 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
351df8bae1dSRodney W. Grimes 		l->llc_control = LLC_UI;
352df8bae1dSRodney W. Grimes 		len += 3;
353df8bae1dSRodney W. Grimes 		IFDEBUG(D_ETHER)
354df8bae1dSRodney W. Grimes 			int i;
355df8bae1dSRodney W. Grimes 			printf("unoutput: sending pkt to: ");
356df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
357df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
358df8bae1dSRodney W. Grimes 			printf("\n");
359df8bae1dSRodney W. Grimes 		ENDDEBUG
360df8bae1dSRodney W. Grimes 		} break;
361df8bae1dSRodney W. Grimes #endif /* ISO */
362df8bae1dSRodney W. Grimes #ifdef	LLC
363df8bae1dSRodney W. Grimes /*	case AF_NSAP: */
364df8bae1dSRodney W. Grimes 	case AF_CCITT: {
365df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl =
366df8bae1dSRodney W. Grimes 			(struct sockaddr_dl *) rt -> rt_gateway;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 		if (sdl && sdl->sdl_family == AF_LINK
369df8bae1dSRodney W. Grimes 		    && sdl->sdl_alen > 0) {
370df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (char *)edst,
371df8bae1dSRodney W. Grimes 				sizeof(edst));
372df8bae1dSRodney W. Grimes 		} else goto bad; /* Not a link interface ? Funny ... */
373df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
374df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
375df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
376df8bae1dSRodney W. Grimes 			if (mcopy) {
377df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
378df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
379df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
380df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
381df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
382df8bae1dSRodney W. Grimes 			}
383df8bae1dSRodney W. Grimes 		}
38434bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
385df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
386df8bae1dSRodney W. Grimes 		{
387df8bae1dSRodney W. Grimes 			int i;
388df8bae1dSRodney W. Grimes 			register struct llc *l = mtod(m, struct llc *);
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes 			printf("ether_output: sending LLC2 pkt to: ");
391df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
392df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
393df8bae1dSRodney W. Grimes 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
394df8bae1dSRodney W. Grimes 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
395df8bae1dSRodney W. Grimes 			       l->llc_control & 0xff);
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 		}
398df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
399df8bae1dSRodney W. Grimes 		} break;
400df8bae1dSRodney W. Grimes #endif /* LLC */
401df8bae1dSRodney W. Grimes 
402df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
403df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
40494a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
405df8bae1dSRodney W. Grimes 		type = eh->ether_type;
406df8bae1dSRodney W. Grimes 		break;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	default:
409df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
410df8bae1dSRodney W. Grimes 			dst->sa_family);
411df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
412df8bae1dSRodney W. Grimes 	}
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes 	if (mcopy)
416df8bae1dSRodney W. Grimes 		(void) looutput(ifp, mcopy, dst, rt);
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
419df8bae1dSRodney W. Grimes 	 * allocate another.
420df8bae1dSRodney W. Grimes 	 */
421df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
422df8bae1dSRodney W. Grimes 	if (m == 0)
423df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
424df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
42594a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
426df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
42794a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
42894a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_shost, ac->ac_enaddr,
429df8bae1dSRodney W. Grimes 	    sizeof(eh->ether_shost));
430df8bae1dSRodney W. Grimes 	s = splimp();
431df8bae1dSRodney W. Grimes 	/*
432df8bae1dSRodney W. Grimes 	 * Queue message on interface, and start output if interface
433df8bae1dSRodney W. Grimes 	 * not yet active.
434df8bae1dSRodney W. Grimes 	 */
435df8bae1dSRodney W. Grimes 	if (IF_QFULL(&ifp->if_snd)) {
436df8bae1dSRodney W. Grimes 		IF_DROP(&ifp->if_snd);
437df8bae1dSRodney W. Grimes 		splx(s);
438df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
439df8bae1dSRodney W. Grimes 	}
440df8bae1dSRodney W. Grimes 	IF_ENQUEUE(&ifp->if_snd, m);
441df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
442df8bae1dSRodney W. Grimes 		(*ifp->if_start)(ifp);
443df8bae1dSRodney W. Grimes 	splx(s);
444df8bae1dSRodney W. Grimes 	ifp->if_obytes += len + sizeof (struct ether_header);
445df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST)
446df8bae1dSRodney W. Grimes 		ifp->if_omcasts++;
447df8bae1dSRodney W. Grimes 	return (error);
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes bad:
450df8bae1dSRodney W. Grimes 	if (m)
451df8bae1dSRodney W. Grimes 		m_freem(m);
452df8bae1dSRodney W. Grimes 	return (error);
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes /*
456df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
457df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
458df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
459df8bae1dSRodney W. Grimes  */
460df8bae1dSRodney W. Grimes void
461df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
462df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
463df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
464df8bae1dSRodney W. Grimes 	struct mbuf *m;
465df8bae1dSRodney W. Grimes {
466df8bae1dSRodney W. Grimes 	register struct ifqueue *inq;
4674a11ca4eSPoul-Henning Kamp 	u_short ether_type;
468df8bae1dSRodney W. Grimes 	int s;
4698e3bda06SJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
470c23670e2SGary Palmer 	register struct llc *l;
471c23670e2SGary Palmer #endif
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
474df8bae1dSRodney W. Grimes 		m_freem(m);
475df8bae1dSRodney W. Grimes 		return;
476df8bae1dSRodney W. Grimes 	}
477df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
478df8bae1dSRodney W. Grimes 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
479df8bae1dSRodney W. Grimes 	    sizeof(etherbroadcastaddr)) == 0)
480df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
481df8bae1dSRodney W. Grimes 	else if (eh->ether_dhost[0] & 1)
482df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
483df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
484df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
485df8bae1dSRodney W. Grimes 
486307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
487307d80beSDavid Greenman 
488307d80beSDavid Greenman 	switch (ether_type) {
489df8bae1dSRodney W. Grimes #ifdef INET
490df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
491df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
492df8bae1dSRodney W. Grimes 		inq = &ipintrq;
493df8bae1dSRodney W. Grimes 		break;
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
496df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
497df8bae1dSRodney W. Grimes 		inq = &arpintrq;
498df8bae1dSRodney W. Grimes 		break;
499df8bae1dSRodney W. Grimes #endif
500cc6a66f2SJulian Elischer #ifdef IPX
501cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
502cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
503cc6a66f2SJulian Elischer 		inq = &ipxintrq;
504cc6a66f2SJulian Elischer 		break;
505cc6a66f2SJulian Elischer #endif
506df8bae1dSRodney W. Grimes #ifdef NS
50788e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
508df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
509df8bae1dSRodney W. Grimes 		inq = &nsintrq;
510df8bae1dSRodney W. Grimes 		break;
51188e038feSJordan K. Hubbard 
51288e038feSJordan K. Hubbard #endif /* NS */
513655929bfSJulian Elischer #ifdef NETATALK
514655929bfSJulian Elischer         case ETHERTYPE_AT:
515655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
516655929bfSJulian Elischer                 inq = &atintrq1;
517655929bfSJulian Elischer                 break;
518655929bfSJulian Elischer         case ETHERTYPE_AARP:
519655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
520655929bfSJulian Elischer                 aarpinput((struct arpcom *)ifp, m); /* XXX */
521655929bfSJulian Elischer                 return;
522655929bfSJulian Elischer #endif NETATALK
523df8bae1dSRodney W. Grimes 	default:
52488e038feSJordan K. Hubbard #ifdef NS
52588e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
52688e038feSJordan K. Hubbard 		/* Novell 802.3 */
52788e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
52888e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
52988e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
53088e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
53188e038feSJordan K. Hubbard 				m->m_len -= 3;
53288e038feSJordan K. Hubbard 				m->m_data += 3;
53388e038feSJordan K. Hubbard 			}
53488e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
53588e038feSJordan K. Hubbard 				inq = &nsintrq;
53688e038feSJordan K. Hubbard 				break;
53788e038feSJordan K. Hubbard 		}
53888e038feSJordan K. Hubbard #endif /* NS */
539655929bfSJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
540307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
541df8bae1dSRodney W. Grimes 			goto dropanyway;
542df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
543df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
544655929bfSJulian Elischer #ifdef NETATALK
545655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
546655929bfSJulian Elischer 		    switch (l->llc_control) {
547655929bfSJulian Elischer 		    case LLC_UI:
548655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
549655929bfSJulian Elischer 			    goto dropanyway;
550655929bfSJulian Elischer 
551655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
552655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
553655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
554655929bfSJulian Elischer 			    inq = &atintrq2;
555655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
556655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
557655929bfSJulian Elischer 			    break;
558655929bfSJulian Elischer 			}
559655929bfSJulian Elischer 
560655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
561655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
562655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
563655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
564655929bfSJulian Elischer 			    aarpinput((struct arpcom *)ifp, m); /* XXX */
565655929bfSJulian Elischer 			    return;
566655929bfSJulian Elischer 			}
567655929bfSJulian Elischer 
568655929bfSJulian Elischer 		    default:
569655929bfSJulian Elischer 			goto dropanyway;
570655929bfSJulian Elischer 		    }
571655929bfSJulian Elischer 		    break;
572655929bfSJulian Elischer #endif NETATALK
573df8bae1dSRodney W. Grimes #ifdef	ISO
574df8bae1dSRodney W. Grimes 		case LLC_ISO_LSAP:
575df8bae1dSRodney W. Grimes 			switch (l->llc_control) {
576df8bae1dSRodney W. Grimes 			case LLC_UI:
577df8bae1dSRodney W. Grimes 				/* LLC_UI_P forbidden in class 1 service */
578df8bae1dSRodney W. Grimes 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
579df8bae1dSRodney W. Grimes 				    (l->llc_ssap == LLC_ISO_LSAP)) {
580df8bae1dSRodney W. Grimes 					/* LSAP for ISO */
581307d80beSDavid Greenman 					if (m->m_pkthdr.len > ether_type)
582307d80beSDavid Greenman 						m_adj(m, ether_type - m->m_pkthdr.len);
583df8bae1dSRodney W. Grimes 					m->m_data += 3;		/* XXX */
584df8bae1dSRodney W. Grimes 					m->m_len -= 3;		/* XXX */
585df8bae1dSRodney W. Grimes 					m->m_pkthdr.len -= 3;	/* XXX */
586df8bae1dSRodney W. Grimes 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
587df8bae1dSRodney W. Grimes 					if (m == 0)
588df8bae1dSRodney W. Grimes 						return;
589df8bae1dSRodney W. Grimes 					*mtod(m, struct ether_header *) = *eh;
590df8bae1dSRodney W. Grimes 					IFDEBUG(D_ETHER)
591df8bae1dSRodney W. Grimes 						printf("clnp packet");
592df8bae1dSRodney W. Grimes 					ENDDEBUG
593df8bae1dSRodney W. Grimes 					schednetisr(NETISR_ISO);
594df8bae1dSRodney W. Grimes 					inq = &clnlintrq;
595df8bae1dSRodney W. Grimes 					break;
596df8bae1dSRodney W. Grimes 				}
597df8bae1dSRodney W. Grimes 				goto dropanyway;
598df8bae1dSRodney W. Grimes 
599df8bae1dSRodney W. Grimes 			case LLC_XID:
600df8bae1dSRodney W. Grimes 			case LLC_XID_P:
601df8bae1dSRodney W. Grimes 				if(m->m_len < 6)
602df8bae1dSRodney W. Grimes 					goto dropanyway;
603df8bae1dSRodney W. Grimes 				l->llc_window = 0;
604df8bae1dSRodney W. Grimes 				l->llc_fid = 9;
605df8bae1dSRodney W. Grimes 				l->llc_class = 1;
606df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap = 0;
607df8bae1dSRodney W. Grimes 				/* Fall through to */
608df8bae1dSRodney W. Grimes 			case LLC_TEST:
609df8bae1dSRodney W. Grimes 			case LLC_TEST_P:
610df8bae1dSRodney W. Grimes 			{
611df8bae1dSRodney W. Grimes 				struct sockaddr sa;
612df8bae1dSRodney W. Grimes 				register struct ether_header *eh2;
613df8bae1dSRodney W. Grimes 				int i;
614df8bae1dSRodney W. Grimes 				u_char c = l->llc_dsap;
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap;
617df8bae1dSRodney W. Grimes 				l->llc_ssap = c;
618df8bae1dSRodney W. Grimes 				if (m->m_flags & (M_BCAST | M_MCAST))
619df8bae1dSRodney W. Grimes 					bcopy((caddr_t)ac->ac_enaddr,
620df8bae1dSRodney W. Grimes 					      (caddr_t)eh->ether_dhost, 6);
621df8bae1dSRodney W. Grimes 				sa.sa_family = AF_UNSPEC;
622df8bae1dSRodney W. Grimes 				sa.sa_len = sizeof(sa);
623df8bae1dSRodney W. Grimes 				eh2 = (struct ether_header *)sa.sa_data;
624df8bae1dSRodney W. Grimes 				for (i = 0; i < 6; i++) {
625df8bae1dSRodney W. Grimes 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
626df8bae1dSRodney W. Grimes 					eh2->ether_dhost[i] =
627df8bae1dSRodney W. Grimes 						eh->ether_dhost[i] = eh->ether_shost[i];
628df8bae1dSRodney W. Grimes 					eh->ether_shost[i] = c;
629df8bae1dSRodney W. Grimes 				}
630df8bae1dSRodney W. Grimes 				ifp->if_output(ifp, m, &sa, NULL);
631df8bae1dSRodney W. Grimes 				return;
632df8bae1dSRodney W. Grimes 			}
633df8bae1dSRodney W. Grimes 			default:
634df8bae1dSRodney W. Grimes 				m_freem(m);
635df8bae1dSRodney W. Grimes 				return;
636df8bae1dSRodney W. Grimes 			}
637df8bae1dSRodney W. Grimes 			break;
638df8bae1dSRodney W. Grimes #endif /* ISO */
639df8bae1dSRodney W. Grimes #ifdef LLC
640df8bae1dSRodney W. Grimes 		case LLC_X25_LSAP:
641df8bae1dSRodney W. Grimes 		{
642307d80beSDavid Greenman 			if (m->m_pkthdr.len > ether_type)
643307d80beSDavid Greenman 				m_adj(m, ether_type - m->m_pkthdr.len);
644df8bae1dSRodney W. Grimes 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
645df8bae1dSRodney W. Grimes 			if (m == 0)
646df8bae1dSRodney W. Grimes 				return;
647df8bae1dSRodney W. Grimes 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
648df8bae1dSRodney W. Grimes 					    eh->ether_dhost, LLC_X25_LSAP, 6,
649df8bae1dSRodney W. Grimes 					    mtod(m, struct sdl_hdr *)))
650df8bae1dSRodney W. Grimes 				panic("ETHER cons addr failure");
651307d80beSDavid Greenman 			mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
652df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
653df8bae1dSRodney W. Grimes 				printf("llc packet\n");
654df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
655df8bae1dSRodney W. Grimes 			schednetisr(NETISR_CCITT);
656df8bae1dSRodney W. Grimes 			inq = &llcintrq;
657df8bae1dSRodney W. Grimes 			break;
658df8bae1dSRodney W. Grimes 		}
659df8bae1dSRodney W. Grimes #endif /* LLC */
660df8bae1dSRodney W. Grimes 		dropanyway:
661df8bae1dSRodney W. Grimes 		default:
662df8bae1dSRodney W. Grimes 			m_freem(m);
663df8bae1dSRodney W. Grimes 			return;
664df8bae1dSRodney W. Grimes 		}
665655929bfSJulian Elischer #else /* ISO || LLC || NETATALK */
666df8bae1dSRodney W. Grimes 	    m_freem(m);
667df8bae1dSRodney W. Grimes 	    return;
668655929bfSJulian Elischer #endif /* ISO || LLC || NETATALK */
669df8bae1dSRodney W. Grimes 	}
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	s = splimp();
672df8bae1dSRodney W. Grimes 	if (IF_QFULL(inq)) {
673df8bae1dSRodney W. Grimes 		IF_DROP(inq);
674df8bae1dSRodney W. Grimes 		m_freem(m);
675df8bae1dSRodney W. Grimes 	} else
676df8bae1dSRodney W. Grimes 		IF_ENQUEUE(inq, m);
677df8bae1dSRodney W. Grimes 	splx(s);
678df8bae1dSRodney W. Grimes }
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes /*
681df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
682df8bae1dSRodney W. Grimes  */
683df8bae1dSRodney W. Grimes void
684df8bae1dSRodney W. Grimes ether_ifattach(ifp)
685df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
686df8bae1dSRodney W. Grimes {
687df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
688df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
691df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
692df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
693df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
6941158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
695a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
696a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
69759562606SGarrett Wollman 	ifa = ifnet_addrs[ifp->if_index - 1];
69859562606SGarrett Wollman 	if (ifa == 0) {
69959562606SGarrett Wollman 		printf("ether_ifattach: no lladdr!\n");
70059562606SGarrett Wollman 		return;
70159562606SGarrett Wollman 	}
70259562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
703df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
704df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
70559562606SGarrett Wollman 	bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
706df8bae1dSRodney W. Grimes }
707df8bae1dSRodney W. Grimes 
708602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
70930106f6aSPoul-Henning Kamp 
710fb583156SDavid Greenman int
711c5a1016bSBruce Evans ether_ioctl(ifp, command, data)
712c5a1016bSBruce Evans 	struct ifnet *ifp;
713c5a1016bSBruce Evans 	int command;
714c5a1016bSBruce Evans 	caddr_t data;
71530106f6aSPoul-Henning Kamp {
71630106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
71730106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
718fb583156SDavid Greenman 	int error = 0;
71930106f6aSPoul-Henning Kamp 
72030106f6aSPoul-Henning Kamp 	switch (command) {
72130106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
72230106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
72330106f6aSPoul-Henning Kamp 
72430106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
72530106f6aSPoul-Henning Kamp #ifdef INET
72630106f6aSPoul-Henning Kamp 		case AF_INET:
72730106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
72830106f6aSPoul-Henning Kamp 			arp_ifinit((struct arpcom *)ifp, ifa);
72930106f6aSPoul-Henning Kamp 			break;
73030106f6aSPoul-Henning Kamp #endif
73130106f6aSPoul-Henning Kamp #ifdef IPX
73230106f6aSPoul-Henning Kamp 		/*
73330106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
73430106f6aSPoul-Henning Kamp 		 */
73530106f6aSPoul-Henning Kamp 		case AF_IPX:
73630106f6aSPoul-Henning Kamp 			{
73730106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
73886101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
73930106f6aSPoul-Henning Kamp 
74030106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
74130106f6aSPoul-Henning Kamp 				ina->x_host =
74286101139SPoul-Henning Kamp 				    *(union ipx_host *)
74386101139SPoul-Henning Kamp 			            ac->ac_enaddr;
74430106f6aSPoul-Henning Kamp 			else {
74530106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
74686101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
74786101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
74830106f6aSPoul-Henning Kamp 			}
74930106f6aSPoul-Henning Kamp 
75030106f6aSPoul-Henning Kamp 			/*
75130106f6aSPoul-Henning Kamp 			 * Set new address
75230106f6aSPoul-Henning Kamp 			 */
75330106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
75430106f6aSPoul-Henning Kamp 			break;
75530106f6aSPoul-Henning Kamp 			}
75630106f6aSPoul-Henning Kamp #endif
75730106f6aSPoul-Henning Kamp #ifdef NS
75830106f6aSPoul-Henning Kamp 		/*
75930106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
76030106f6aSPoul-Henning Kamp 		 */
76130106f6aSPoul-Henning Kamp 		case AF_NS:
76230106f6aSPoul-Henning Kamp 		{
76330106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
76486101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
76530106f6aSPoul-Henning Kamp 
76630106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
76730106f6aSPoul-Henning Kamp 				ina->x_host =
76886101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
76930106f6aSPoul-Henning Kamp 			else {
77030106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
77186101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
77286101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
77330106f6aSPoul-Henning Kamp 			}
77430106f6aSPoul-Henning Kamp 
77530106f6aSPoul-Henning Kamp 			/*
77630106f6aSPoul-Henning Kamp 			 * Set new address
77730106f6aSPoul-Henning Kamp 			 */
77830106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
77930106f6aSPoul-Henning Kamp 			break;
78030106f6aSPoul-Henning Kamp 		}
78130106f6aSPoul-Henning Kamp #endif
78230106f6aSPoul-Henning Kamp 		default:
78330106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
78430106f6aSPoul-Henning Kamp 			break;
78530106f6aSPoul-Henning Kamp 		}
78630106f6aSPoul-Henning Kamp 		break;
78730106f6aSPoul-Henning Kamp 
78830106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
78930106f6aSPoul-Henning Kamp 		{
79030106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
79130106f6aSPoul-Henning Kamp 
79230106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
7935b73c186SDavid Greenman 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
79430106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
79530106f6aSPoul-Henning Kamp 		}
79630106f6aSPoul-Henning Kamp 		break;
797fb583156SDavid Greenman 
798fb583156SDavid Greenman 	case SIOCSIFMTU:
799fb583156SDavid Greenman 		/*
800fb583156SDavid Greenman 		 * Set the interface MTU.
801fb583156SDavid Greenman 		 */
802fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
803fb583156SDavid Greenman 			error = EINVAL;
804fb583156SDavid Greenman 		} else {
805fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
80630106f6aSPoul-Henning Kamp 		}
807fb583156SDavid Greenman 		break;
808fb583156SDavid Greenman 	}
809fb583156SDavid Greenman 	return (error);
81030106f6aSPoul-Henning Kamp }
8111158dfb7SGarrett Wollman 
8121158dfb7SGarrett Wollman int
8131158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa)
8141158dfb7SGarrett Wollman 	struct ifnet *ifp;
8151158dfb7SGarrett Wollman 	struct sockaddr **llsa;
8161158dfb7SGarrett Wollman 	struct sockaddr *sa;
8171158dfb7SGarrett Wollman {
8181158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
8191158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
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
8531158dfb7SGarrett Wollman 
8541158dfb7SGarrett Wollman 	default:
8551158dfb7SGarrett Wollman 		/*
8561158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
8571158dfb7SGarrett Wollman 		 * that counts...
8581158dfb7SGarrett Wollman 		 */
8591158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
8601158dfb7SGarrett Wollman 	}
8611158dfb7SGarrett Wollman }
862