xref: /freebsd/sys/net/if_ethersubr.c (revision 5b73c1866a4e1881a9f5186b597d4286fcb1bf12)
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
345b73c186SDavid Greenman  * $Id: if_ethersubr.c,v 1.26 1996/10/18 15:59:25 jkh Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/kernel.h>
40df8bae1dSRodney W. Grimes #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42df8bae1dSRodney W. Grimes #include <sys/protosw.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
45df8bae1dSRodney W. Grimes #include <sys/errno.h>
46df8bae1dSRodney W. Grimes #include <sys/syslog.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>
5588e1602bSPoul-Henning Kamp #include <net/ethernet.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 #endif
61df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
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 ];
102655929bfSJulian Elischer #endif NETATALK
103655929bfSJulian Elischer 
104df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
105df8bae1dSRodney W. Grimes #define senderr(e) { error = (e); goto bad;}
106df8bae1dSRodney W. Grimes 
107df8bae1dSRodney W. Grimes /*
108df8bae1dSRodney W. Grimes  * Ethernet output routine.
109df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
110df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
111df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
112df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
113df8bae1dSRodney W. Grimes  */
114df8bae1dSRodney W. Grimes int
115df8bae1dSRodney W. Grimes ether_output(ifp, m0, dst, rt0)
116df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
117df8bae1dSRodney W. Grimes 	struct mbuf *m0;
118df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
119df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
120df8bae1dSRodney W. Grimes {
121df8bae1dSRodney W. Grimes 	short type;
122df8bae1dSRodney W. Grimes 	int s, error = 0;
12388e038feSJordan K. Hubbard  	u_char *cp, edst[6];
12488e038feSJordan K. Hubbard 	register struct mbuf *m2, *m = m0;
125df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
126df8bae1dSRodney W. Grimes 	struct mbuf *mcopy = (struct mbuf *)0;
127df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
128df8bae1dSRodney W. Grimes 	int off, len = m->m_pkthdr.len;
129df8bae1dSRodney W. Grimes 	struct arpcom *ac = (struct arpcom *)ifp;
13088e038feSJordan K. Hubbard 	register struct ifqueue *inq;
131655929bfSJulian Elischer #ifdef NETATALK
132655929bfSJulian Elischer 	struct at_ifaddr *aa;
133655929bfSJulian Elischer #endif NETATALK
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
136df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1373bda9f9bSPoul-Henning Kamp 	rt = rt0;
1383bda9f9bSPoul-Henning Kamp 	if (rt) {
139df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1403bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1413bda9f9bSPoul-Henning Kamp 			if (rt0)
142df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
143df8bae1dSRodney W. Grimes 			else
144df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
145df8bae1dSRodney W. Grimes 		}
146df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
147df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
148df8bae1dSRodney W. Grimes 				goto lookup;
149df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
150df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
151995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
152995add1aSGarrett Wollman 							  0UL);
153df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
154df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
155df8bae1dSRodney W. Grimes 			}
156df8bae1dSRodney W. Grimes 		}
157df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
158df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
159df8bae1dSRodney W. Grimes 			    time.tv_sec < rt->rt_rmx.rmx_expire)
160df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
161df8bae1dSRodney W. Grimes 	}
162df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes #ifdef INET
165df8bae1dSRodney W. Grimes 	case AF_INET:
1665df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
167df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
168df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
169df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
170df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
171df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
17234bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
173df8bae1dSRodney W. Grimes 		break;
174df8bae1dSRodney W. Grimes #endif
175cc6a66f2SJulian Elischer #ifdef IPX
176cc6a66f2SJulian Elischer 	case AF_IPX:
17734bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IPX);
178cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
179cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
180cc6a66f2SJulian Elischer 		if (!bcmp((caddr_t)edst, (caddr_t)&ipx_thishost, sizeof(edst)))
181cc6a66f2SJulian Elischer 			return (looutput(ifp, m, dst, rt));
182cc6a66f2SJulian Elischer 		/* If broadcasting on a simplex interface, loopback a copy */
183cc6a66f2SJulian Elischer 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
184cc6a66f2SJulian Elischer 			mcopy = m_copy(m, 0, (int)M_COPYALL);
185cc6a66f2SJulian Elischer 		break;
186cc6a66f2SJulian Elischer #endif
187655929bfSJulian Elischer #ifdef NETATALK
188655929bfSJulian Elischer 	case AF_APPLETALK:
189655929bfSJulian Elischer             if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
190655929bfSJulian Elischer #ifdef NETATALKDEBUG
191655929bfSJulian Elischer                 extern char *prsockaddr(struct sockaddr *);
192655929bfSJulian Elischer                 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
193655929bfSJulian Elischer #endif NETATALKDEBUG
194655929bfSJulian Elischer                 return (0);
195655929bfSJulian Elischer             }
196655929bfSJulian Elischer 	    /*
197655929bfSJulian Elischer 	     * ifaddr is the first thing in at_ifaddr
198655929bfSJulian Elischer 	     */
199655929bfSJulian Elischer 	    if ((aa = (struct at_ifaddr *)at_ifawithnet(
200655929bfSJulian Elischer 			(struct sockaddr_at *)dst, ifp->if_addrlist))
201655929bfSJulian Elischer 		== 0)
202655929bfSJulian Elischer 		goto bad;
203655929bfSJulian Elischer 
204655929bfSJulian Elischer 	    /*
205655929bfSJulian Elischer 	     * In the phase 2 case, we need to prepend an mbuf for the llc header.
206655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
207655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
208655929bfSJulian Elischer 	     */
209655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
210655929bfSJulian Elischer 		struct llc llc;
211655929bfSJulian Elischer 
212655929bfSJulian Elischer 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
213655929bfSJulian Elischer 		len += sizeof(struct llc);
214655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
215655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
216655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
217655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
218655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
21934bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
220655929bfSJulian Elischer 	    } else {
22134bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
222655929bfSJulian Elischer 	    }
223655929bfSJulian Elischer 	    break;
224655929bfSJulian Elischer #endif NETATALK
225df8bae1dSRodney W. Grimes #ifdef NS
226df8bae1dSRodney W. Grimes 	case AF_NS:
22788e038feSJordan K. Hubbard 		switch(ns_nettype){
22888e038feSJordan K. Hubbard 		default:
22988e038feSJordan K. Hubbard 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
23088e038feSJordan K. Hubbard 			type = 0x8137;
23188e038feSJordan K. Hubbard 			break;
23288e038feSJordan K. Hubbard 		case 0x0: /* Novell 802.3 */
23388e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
23488e038feSJordan K. Hubbard 			break;
23588e038feSJordan K. Hubbard 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
23688e038feSJordan K. Hubbard 			M_PREPEND(m, 3, M_WAIT);
23788e038feSJordan K. Hubbard 			type = htons( m->m_pkthdr.len);
23888e038feSJordan K. Hubbard 			cp = mtod(m, u_char *);
23988e038feSJordan K. Hubbard 			*cp++ = 0xE0;
24088e038feSJordan K. Hubbard 			*cp++ = 0xE0;
24188e038feSJordan K. Hubbard 			*cp++ = 0x03;
24288e038feSJordan K. Hubbard 			break;
24388e038feSJordan K. Hubbard 		}
244df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
245df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
24688e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
24788e038feSJordan K. Hubbard 			m->m_pkthdr.rcvif = ifp;
24888e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
24988e038feSJordan K. Hubbard 			inq = &nsintrq;
25088e038feSJordan K. Hubbard 			s = splimp();
25188e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
25288e038feSJordan K. Hubbard 				IF_DROP(inq);
25388e038feSJordan K. Hubbard 				m_freem(m);
25488e038feSJordan K. Hubbard 			} else
25588e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m);
25688e038feSJordan K. Hubbard 			splx(s);
25788e038feSJordan K. Hubbard 			return (error);
25888e038feSJordan K. Hubbard 		}
25988e038feSJordan K. Hubbard 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
26088e038feSJordan K. Hubbard 			m2 = m_copy(m, 0, (int)M_COPYALL);
26188e038feSJordan K. Hubbard 			m2->m_pkthdr.rcvif = ifp;
26288e038feSJordan K. Hubbard 			schednetisr(NETISR_NS);
26388e038feSJordan K. Hubbard 			inq = &nsintrq;
26488e038feSJordan K. Hubbard 			s = splimp();
26588e038feSJordan K. Hubbard 			if (IF_QFULL(inq)) {
26688e038feSJordan K. Hubbard 				IF_DROP(inq);
26788e038feSJordan K. Hubbard 				m_freem(m2);
26888e038feSJordan K. Hubbard 			} else
26988e038feSJordan K. Hubbard 				IF_ENQUEUE(inq, m2);
27088e038feSJordan K. Hubbard 			splx(s);
27188e038feSJordan K. Hubbard 		}
272df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
27388e038feSJordan K. Hubbard 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)){
274df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
27588e038feSJordan K. Hubbard 		}
276df8bae1dSRodney W. Grimes 		break;
27788e038feSJordan K. Hubbard #endif /* NS */
278df8bae1dSRodney W. Grimes #ifdef	ISO
279df8bae1dSRodney W. Grimes 	case AF_ISO: {
280df8bae1dSRodney W. Grimes 		int	snpalen;
281df8bae1dSRodney W. Grimes 		struct	llc *l;
282df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl;
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
285df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
286df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
287df8bae1dSRodney W. Grimes 		} else if (error =
288df8bae1dSRodney W. Grimes 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
289df8bae1dSRodney W. Grimes 					    (char *)edst, &snpalen))
290df8bae1dSRodney W. Grimes 			goto bad; /* Not Resolved */
291df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
292df8bae1dSRodney W. Grimes 		if (*edst & 1)
293df8bae1dSRodney W. Grimes 			m->m_flags |= (M_BCAST|M_MCAST);
294df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
295df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
296df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
297df8bae1dSRodney W. Grimes 			if (mcopy) {
298df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
299df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
300df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
301df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
302df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
303df8bae1dSRodney W. Grimes 			}
304df8bae1dSRodney W. Grimes 		}
305df8bae1dSRodney W. Grimes 		M_PREPEND(m, 3, M_DONTWAIT);
306df8bae1dSRodney W. Grimes 		if (m == NULL)
307df8bae1dSRodney W. Grimes 			return (0);
30834bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
309df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
310df8bae1dSRodney W. Grimes 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
311df8bae1dSRodney W. Grimes 		l->llc_control = LLC_UI;
312df8bae1dSRodney W. Grimes 		len += 3;
313df8bae1dSRodney W. Grimes 		IFDEBUG(D_ETHER)
314df8bae1dSRodney W. Grimes 			int i;
315df8bae1dSRodney W. Grimes 			printf("unoutput: sending pkt to: ");
316df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
317df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
318df8bae1dSRodney W. Grimes 			printf("\n");
319df8bae1dSRodney W. Grimes 		ENDDEBUG
320df8bae1dSRodney W. Grimes 		} break;
321df8bae1dSRodney W. Grimes #endif /* ISO */
322df8bae1dSRodney W. Grimes #ifdef	LLC
323df8bae1dSRodney W. Grimes /*	case AF_NSAP: */
324df8bae1dSRodney W. Grimes 	case AF_CCITT: {
325df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl =
326df8bae1dSRodney W. Grimes 			(struct sockaddr_dl *) rt -> rt_gateway;
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 		if (sdl && sdl->sdl_family == AF_LINK
329df8bae1dSRodney W. Grimes 		    && sdl->sdl_alen > 0) {
330df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (char *)edst,
331df8bae1dSRodney W. Grimes 				sizeof(edst));
332df8bae1dSRodney W. Grimes 		} else goto bad; /* Not a link interface ? Funny ... */
333df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
334df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
335df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
336df8bae1dSRodney W. Grimes 			if (mcopy) {
337df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
338df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
339df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
340df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
341df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
342df8bae1dSRodney W. Grimes 			}
343df8bae1dSRodney W. Grimes 		}
34434bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
345df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
346df8bae1dSRodney W. Grimes 		{
347df8bae1dSRodney W. Grimes 			int i;
348df8bae1dSRodney W. Grimes 			register struct llc *l = mtod(m, struct llc *);
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 			printf("ether_output: sending LLC2 pkt to: ");
351df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
352df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
353df8bae1dSRodney W. Grimes 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
354df8bae1dSRodney W. Grimes 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
355df8bae1dSRodney W. Grimes 			       l->llc_control & 0xff);
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 		}
358df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
359df8bae1dSRodney W. Grimes 		} break;
360df8bae1dSRodney W. Grimes #endif /* LLC */
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
363df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
36494a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
365df8bae1dSRodney W. Grimes 		type = eh->ether_type;
366df8bae1dSRodney W. Grimes 		break;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	default:
369df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
370df8bae1dSRodney W. Grimes 			dst->sa_family);
371df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
372df8bae1dSRodney W. Grimes 	}
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes 	if (mcopy)
376df8bae1dSRodney W. Grimes 		(void) looutput(ifp, mcopy, dst, rt);
377df8bae1dSRodney W. Grimes 	/*
378df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
379df8bae1dSRodney W. Grimes 	 * allocate another.
380df8bae1dSRodney W. Grimes 	 */
381df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
382df8bae1dSRodney W. Grimes 	if (m == 0)
383df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
384df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
38594a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
386df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
38794a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
38894a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_shost, ac->ac_enaddr,
389df8bae1dSRodney W. Grimes 	    sizeof(eh->ether_shost));
390df8bae1dSRodney W. Grimes 	s = splimp();
391df8bae1dSRodney W. Grimes 	/*
392df8bae1dSRodney W. Grimes 	 * Queue message on interface, and start output if interface
393df8bae1dSRodney W. Grimes 	 * not yet active.
394df8bae1dSRodney W. Grimes 	 */
395df8bae1dSRodney W. Grimes 	if (IF_QFULL(&ifp->if_snd)) {
396df8bae1dSRodney W. Grimes 		IF_DROP(&ifp->if_snd);
397df8bae1dSRodney W. Grimes 		splx(s);
398df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
399df8bae1dSRodney W. Grimes 	}
400df8bae1dSRodney W. Grimes 	IF_ENQUEUE(&ifp->if_snd, m);
401df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
402df8bae1dSRodney W. Grimes 		(*ifp->if_start)(ifp);
403df8bae1dSRodney W. Grimes 	splx(s);
404df8bae1dSRodney W. Grimes 	ifp->if_obytes += len + sizeof (struct ether_header);
405df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST)
406df8bae1dSRodney W. Grimes 		ifp->if_omcasts++;
407df8bae1dSRodney W. Grimes 	return (error);
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes bad:
410df8bae1dSRodney W. Grimes 	if (m)
411df8bae1dSRodney W. Grimes 		m_freem(m);
412df8bae1dSRodney W. Grimes 	return (error);
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes /*
416df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
417df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
418df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
419df8bae1dSRodney W. Grimes  */
420df8bae1dSRodney W. Grimes void
421df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
422df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
423df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
424df8bae1dSRodney W. Grimes 	struct mbuf *m;
425df8bae1dSRodney W. Grimes {
426df8bae1dSRodney W. Grimes 	register struct ifqueue *inq;
42788e038feSJordan K. Hubbard 	u_short ether_type, *checksum;
428df8bae1dSRodney W. Grimes 	int s;
4298e3bda06SJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
430c23670e2SGary Palmer 	register struct llc *l;
431c23670e2SGary Palmer #endif
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
434df8bae1dSRodney W. Grimes 		m_freem(m);
435df8bae1dSRodney W. Grimes 		return;
436df8bae1dSRodney W. Grimes 	}
437df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
438df8bae1dSRodney W. Grimes 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
439df8bae1dSRodney W. Grimes 	    sizeof(etherbroadcastaddr)) == 0)
440df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
441df8bae1dSRodney W. Grimes 	else if (eh->ether_dhost[0] & 1)
442df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
443df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
444df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
445df8bae1dSRodney W. Grimes 
446307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
447307d80beSDavid Greenman 
448307d80beSDavid Greenman 	switch (ether_type) {
449df8bae1dSRodney W. Grimes #ifdef INET
450df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
451df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
452df8bae1dSRodney W. Grimes 		inq = &ipintrq;
453df8bae1dSRodney W. Grimes 		break;
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
456df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
457df8bae1dSRodney W. Grimes 		inq = &arpintrq;
458df8bae1dSRodney W. Grimes 		break;
459df8bae1dSRodney W. Grimes #endif
460cc6a66f2SJulian Elischer #ifdef IPX
461cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
462cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
463cc6a66f2SJulian Elischer 		inq = &ipxintrq;
464cc6a66f2SJulian Elischer 		break;
465cc6a66f2SJulian Elischer #endif
466df8bae1dSRodney W. Grimes #ifdef NS
46788e038feSJordan K. Hubbard 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
468df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
469df8bae1dSRodney W. Grimes 		inq = &nsintrq;
470df8bae1dSRodney W. Grimes 		break;
47188e038feSJordan K. Hubbard 
47288e038feSJordan K. Hubbard #endif /* NS */
473655929bfSJulian Elischer #ifdef NETATALK
474655929bfSJulian Elischer         case ETHERTYPE_AT:
475655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
476655929bfSJulian Elischer                 inq = &atintrq1;
477655929bfSJulian Elischer                 break;
478655929bfSJulian Elischer         case ETHERTYPE_AARP:
479655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
480655929bfSJulian Elischer                 aarpinput((struct arpcom *)ifp, m); /* XXX */
481655929bfSJulian Elischer                 return;
482655929bfSJulian Elischer #endif NETATALK
483df8bae1dSRodney W. Grimes 	default:
48488e038feSJordan K. Hubbard #ifdef NS
48588e038feSJordan K. Hubbard 		checksum = mtod(m, ushort *);
48688e038feSJordan K. Hubbard 		/* Novell 802.3 */
48788e038feSJordan K. Hubbard 		if ((ether_type <= ETHERMTU) &&
48888e038feSJordan K. Hubbard 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
48988e038feSJordan K. Hubbard 			if(*checksum == 0xE0E0) {
49088e038feSJordan K. Hubbard 				m->m_pkthdr.len -= 3;
49188e038feSJordan K. Hubbard 				m->m_len -= 3;
49288e038feSJordan K. Hubbard 				m->m_data += 3;
49388e038feSJordan K. Hubbard 			}
49488e038feSJordan K. Hubbard 				schednetisr(NETISR_NS);
49588e038feSJordan K. Hubbard 				inq = &nsintrq;
49688e038feSJordan K. Hubbard 				break;
49788e038feSJordan K. Hubbard 		}
49888e038feSJordan K. Hubbard #endif /* NS */
499655929bfSJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
500307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
501df8bae1dSRodney W. Grimes 			goto dropanyway;
502df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
503df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
504655929bfSJulian Elischer #ifdef NETATALK
505655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
506655929bfSJulian Elischer 		    switch (l->llc_control) {
507655929bfSJulian Elischer 		    case LLC_UI:
508655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
509655929bfSJulian Elischer 			    goto dropanyway;
510655929bfSJulian Elischer 
511655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
512655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
513655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
514655929bfSJulian Elischer 			    inq = &atintrq2;
515655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
516655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
517655929bfSJulian Elischer 			    break;
518655929bfSJulian Elischer 			}
519655929bfSJulian Elischer 
520655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
521655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
522655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
523655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
524655929bfSJulian Elischer 			    aarpinput((struct arpcom *)ifp, m); /* XXX */
525655929bfSJulian Elischer 			    return;
526655929bfSJulian Elischer 			}
527655929bfSJulian Elischer 
528655929bfSJulian Elischer 		    default:
529655929bfSJulian Elischer 			goto dropanyway;
530655929bfSJulian Elischer 		    }
531655929bfSJulian Elischer 		    break;
532655929bfSJulian Elischer #endif NETATALK
533df8bae1dSRodney W. Grimes #ifdef	ISO
534df8bae1dSRodney W. Grimes 		case LLC_ISO_LSAP:
535df8bae1dSRodney W. Grimes 			switch (l->llc_control) {
536df8bae1dSRodney W. Grimes 			case LLC_UI:
537df8bae1dSRodney W. Grimes 				/* LLC_UI_P forbidden in class 1 service */
538df8bae1dSRodney W. Grimes 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
539df8bae1dSRodney W. Grimes 				    (l->llc_ssap == LLC_ISO_LSAP)) {
540df8bae1dSRodney W. Grimes 					/* LSAP for ISO */
541307d80beSDavid Greenman 					if (m->m_pkthdr.len > ether_type)
542307d80beSDavid Greenman 						m_adj(m, ether_type - m->m_pkthdr.len);
543df8bae1dSRodney W. Grimes 					m->m_data += 3;		/* XXX */
544df8bae1dSRodney W. Grimes 					m->m_len -= 3;		/* XXX */
545df8bae1dSRodney W. Grimes 					m->m_pkthdr.len -= 3;	/* XXX */
546df8bae1dSRodney W. Grimes 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
547df8bae1dSRodney W. Grimes 					if (m == 0)
548df8bae1dSRodney W. Grimes 						return;
549df8bae1dSRodney W. Grimes 					*mtod(m, struct ether_header *) = *eh;
550df8bae1dSRodney W. Grimes 					IFDEBUG(D_ETHER)
551df8bae1dSRodney W. Grimes 						printf("clnp packet");
552df8bae1dSRodney W. Grimes 					ENDDEBUG
553df8bae1dSRodney W. Grimes 					schednetisr(NETISR_ISO);
554df8bae1dSRodney W. Grimes 					inq = &clnlintrq;
555df8bae1dSRodney W. Grimes 					break;
556df8bae1dSRodney W. Grimes 				}
557df8bae1dSRodney W. Grimes 				goto dropanyway;
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 			case LLC_XID:
560df8bae1dSRodney W. Grimes 			case LLC_XID_P:
561df8bae1dSRodney W. Grimes 				if(m->m_len < 6)
562df8bae1dSRodney W. Grimes 					goto dropanyway;
563df8bae1dSRodney W. Grimes 				l->llc_window = 0;
564df8bae1dSRodney W. Grimes 				l->llc_fid = 9;
565df8bae1dSRodney W. Grimes 				l->llc_class = 1;
566df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap = 0;
567df8bae1dSRodney W. Grimes 				/* Fall through to */
568df8bae1dSRodney W. Grimes 			case LLC_TEST:
569df8bae1dSRodney W. Grimes 			case LLC_TEST_P:
570df8bae1dSRodney W. Grimes 			{
571df8bae1dSRodney W. Grimes 				struct sockaddr sa;
572df8bae1dSRodney W. Grimes 				register struct ether_header *eh2;
573df8bae1dSRodney W. Grimes 				int i;
574df8bae1dSRodney W. Grimes 				u_char c = l->llc_dsap;
575df8bae1dSRodney W. Grimes 
576df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap;
577df8bae1dSRodney W. Grimes 				l->llc_ssap = c;
578df8bae1dSRodney W. Grimes 				if (m->m_flags & (M_BCAST | M_MCAST))
579df8bae1dSRodney W. Grimes 					bcopy((caddr_t)ac->ac_enaddr,
580df8bae1dSRodney W. Grimes 					      (caddr_t)eh->ether_dhost, 6);
581df8bae1dSRodney W. Grimes 				sa.sa_family = AF_UNSPEC;
582df8bae1dSRodney W. Grimes 				sa.sa_len = sizeof(sa);
583df8bae1dSRodney W. Grimes 				eh2 = (struct ether_header *)sa.sa_data;
584df8bae1dSRodney W. Grimes 				for (i = 0; i < 6; i++) {
585df8bae1dSRodney W. Grimes 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
586df8bae1dSRodney W. Grimes 					eh2->ether_dhost[i] =
587df8bae1dSRodney W. Grimes 						eh->ether_dhost[i] = eh->ether_shost[i];
588df8bae1dSRodney W. Grimes 					eh->ether_shost[i] = c;
589df8bae1dSRodney W. Grimes 				}
590df8bae1dSRodney W. Grimes 				ifp->if_output(ifp, m, &sa, NULL);
591df8bae1dSRodney W. Grimes 				return;
592df8bae1dSRodney W. Grimes 			}
593df8bae1dSRodney W. Grimes 			default:
594df8bae1dSRodney W. Grimes 				m_freem(m);
595df8bae1dSRodney W. Grimes 				return;
596df8bae1dSRodney W. Grimes 			}
597df8bae1dSRodney W. Grimes 			break;
598df8bae1dSRodney W. Grimes #endif /* ISO */
599df8bae1dSRodney W. Grimes #ifdef LLC
600df8bae1dSRodney W. Grimes 		case LLC_X25_LSAP:
601df8bae1dSRodney W. Grimes 		{
602307d80beSDavid Greenman 			if (m->m_pkthdr.len > ether_type)
603307d80beSDavid Greenman 				m_adj(m, ether_type - m->m_pkthdr.len);
604df8bae1dSRodney W. Grimes 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
605df8bae1dSRodney W. Grimes 			if (m == 0)
606df8bae1dSRodney W. Grimes 				return;
607df8bae1dSRodney W. Grimes 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
608df8bae1dSRodney W. Grimes 					    eh->ether_dhost, LLC_X25_LSAP, 6,
609df8bae1dSRodney W. Grimes 					    mtod(m, struct sdl_hdr *)))
610df8bae1dSRodney W. Grimes 				panic("ETHER cons addr failure");
611307d80beSDavid Greenman 			mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
612df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
613df8bae1dSRodney W. Grimes 				printf("llc packet\n");
614df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
615df8bae1dSRodney W. Grimes 			schednetisr(NETISR_CCITT);
616df8bae1dSRodney W. Grimes 			inq = &llcintrq;
617df8bae1dSRodney W. Grimes 			break;
618df8bae1dSRodney W. Grimes 		}
619df8bae1dSRodney W. Grimes #endif /* LLC */
620df8bae1dSRodney W. Grimes 		dropanyway:
621df8bae1dSRodney W. Grimes 		default:
622df8bae1dSRodney W. Grimes 			m_freem(m);
623df8bae1dSRodney W. Grimes 			return;
624df8bae1dSRodney W. Grimes 		}
625655929bfSJulian Elischer #else /* ISO || LLC || NETATALK */
626df8bae1dSRodney W. Grimes 	    m_freem(m);
627df8bae1dSRodney W. Grimes 	    return;
628655929bfSJulian Elischer #endif /* ISO || LLC || NETATALK */
629df8bae1dSRodney W. Grimes 	}
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes 	s = splimp();
632df8bae1dSRodney W. Grimes 	if (IF_QFULL(inq)) {
633df8bae1dSRodney W. Grimes 		IF_DROP(inq);
634df8bae1dSRodney W. Grimes 		m_freem(m);
635df8bae1dSRodney W. Grimes 	} else
636df8bae1dSRodney W. Grimes 		IF_ENQUEUE(inq, m);
637df8bae1dSRodney W. Grimes 	splx(s);
638df8bae1dSRodney W. Grimes }
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes /*
641df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
642df8bae1dSRodney W. Grimes  */
643df8bae1dSRodney W. Grimes void
644df8bae1dSRodney W. Grimes ether_ifattach(ifp)
645df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
648df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
651df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
652df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
653df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
654a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
655a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
656df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
657df8bae1dSRodney W. Grimes 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
658df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK) {
659df8bae1dSRodney W. Grimes 			sdl->sdl_type = IFT_ETHER;
660df8bae1dSRodney W. Grimes 			sdl->sdl_alen = ifp->if_addrlen;
661df8bae1dSRodney W. Grimes 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
662df8bae1dSRodney W. Grimes 			      LLADDR(sdl), ifp->if_addrlen);
663df8bae1dSRodney W. Grimes 			break;
664df8bae1dSRodney W. Grimes 		}
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
6673bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_min[6] =
6683bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
6693bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_max[6] =
6703bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
671df8bae1dSRodney W. Grimes /*
672df8bae1dSRodney W. Grimes  * Add an Ethernet multicast address or range of addresses to the list for a
673df8bae1dSRodney W. Grimes  * given interface.
674df8bae1dSRodney W. Grimes  */
675df8bae1dSRodney W. Grimes int
676df8bae1dSRodney W. Grimes ether_addmulti(ifr, ac)
677df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
678df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
679df8bae1dSRodney W. Grimes {
680df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
681df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
682df8bae1dSRodney W. Grimes 	u_char addrlo[6];
683df8bae1dSRodney W. Grimes 	u_char addrhi[6];
684d3628763SRodney W. Grimes         int set_allmulti = 0;
685df8bae1dSRodney W. Grimes 	int s = splimp();
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
690df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
691df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
692df8bae1dSRodney W. Grimes 		break;
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes #ifdef INET
695df8bae1dSRodney W. Grimes 	case AF_INET:
696df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
697df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
698df8bae1dSRodney W. Grimes 			/*
699df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means listen to all
700df8bae1dSRodney W. Grimes 			 * of the Ethernet multicast addresses used for IP.
701df8bae1dSRodney W. Grimes 			 * (This is for the sake of IP multicast routers.)
702df8bae1dSRodney W. Grimes 			 */
703df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
704df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
705d3628763SRodney W. Grimes                       set_allmulti = 1;
706df8bae1dSRodney W. Grimes 		}
707df8bae1dSRodney W. Grimes 		else {
708df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
709df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
710df8bae1dSRodney W. Grimes 		}
711df8bae1dSRodney W. Grimes 		break;
712df8bae1dSRodney W. Grimes #endif
713df8bae1dSRodney W. Grimes 
714df8bae1dSRodney W. Grimes 	default:
715df8bae1dSRodney W. Grimes 		splx(s);
716df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
717df8bae1dSRodney W. Grimes 	}
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * Verify that we have valid Ethernet multicast addresses.
721df8bae1dSRodney W. Grimes 	 */
722df8bae1dSRodney W. Grimes 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
723df8bae1dSRodney W. Grimes 		splx(s);
724df8bae1dSRodney W. Grimes 		return (EINVAL);
725df8bae1dSRodney W. Grimes 	}
726df8bae1dSRodney W. Grimes 	/*
727df8bae1dSRodney W. Grimes 	 * See if the address range is already in the list.
728df8bae1dSRodney W. Grimes 	 */
729df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
730df8bae1dSRodney W. Grimes 	if (enm != NULL) {
731df8bae1dSRodney W. Grimes 		/*
732df8bae1dSRodney W. Grimes 		 * Found it; just increment the reference count.
733df8bae1dSRodney W. Grimes 		 */
734df8bae1dSRodney W. Grimes 		++enm->enm_refcount;
735df8bae1dSRodney W. Grimes 		splx(s);
736df8bae1dSRodney W. Grimes 		return (0);
737df8bae1dSRodney W. Grimes 	}
738df8bae1dSRodney W. Grimes 	/*
739df8bae1dSRodney W. Grimes 	 * New address or range; malloc a new multicast record
740df8bae1dSRodney W. Grimes 	 * and link it into the interface's multicast list.
741df8bae1dSRodney W. Grimes 	 */
742df8bae1dSRodney W. Grimes 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
743df8bae1dSRodney W. Grimes 	if (enm == NULL) {
744df8bae1dSRodney W. Grimes 		splx(s);
745df8bae1dSRodney W. Grimes 		return (ENOBUFS);
746df8bae1dSRodney W. Grimes 	}
747df8bae1dSRodney W. Grimes 	bcopy(addrlo, enm->enm_addrlo, 6);
748df8bae1dSRodney W. Grimes 	bcopy(addrhi, enm->enm_addrhi, 6);
749df8bae1dSRodney W. Grimes 	enm->enm_ac = ac;
750df8bae1dSRodney W. Grimes 	enm->enm_refcount = 1;
751df8bae1dSRodney W. Grimes 	enm->enm_next = ac->ac_multiaddrs;
752df8bae1dSRodney W. Grimes 	ac->ac_multiaddrs = enm;
753df8bae1dSRodney W. Grimes 	ac->ac_multicnt++;
754df8bae1dSRodney W. Grimes 	splx(s);
755d3628763SRodney W. Grimes         if (set_allmulti)
756d3628763SRodney W. Grimes         	ac->ac_if.if_flags |= IFF_ALLMULTI;
757d3628763SRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	/*
759df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
760df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
761df8bae1dSRodney W. Grimes 	 */
762df8bae1dSRodney W. Grimes 	return (ENETRESET);
763df8bae1dSRodney W. Grimes }
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes /*
766df8bae1dSRodney W. Grimes  * Delete a multicast address record.
767df8bae1dSRodney W. Grimes  */
768df8bae1dSRodney W. Grimes int
769df8bae1dSRodney W. Grimes ether_delmulti(ifr, ac)
770df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
771df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
772df8bae1dSRodney W. Grimes {
773df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
774df8bae1dSRodney W. Grimes 	register struct ether_multi **p;
775df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
776df8bae1dSRodney W. Grimes 	u_char addrlo[6];
777df8bae1dSRodney W. Grimes 	u_char addrhi[6];
778d3628763SRodney W. Grimes       int unset_allmulti = 0;
779df8bae1dSRodney W. Grimes 	int s = splimp();
780df8bae1dSRodney W. Grimes 
781df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
784df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
785df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
786df8bae1dSRodney W. Grimes 		break;
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes #ifdef INET
789df8bae1dSRodney W. Grimes 	case AF_INET:
790df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
791df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
792df8bae1dSRodney W. Grimes 			/*
793df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means stop listening
794df8bae1dSRodney W. Grimes 			 * to the range of Ethernet multicast addresses used
795df8bae1dSRodney W. Grimes 			 * for IP.
796df8bae1dSRodney W. Grimes 			 */
797df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
798df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
799d3628763SRodney W. Grimes                       unset_allmulti = 1;
800df8bae1dSRodney W. Grimes 		}
801df8bae1dSRodney W. Grimes 		else {
802df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
803df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
804df8bae1dSRodney W. Grimes 		}
805df8bae1dSRodney W. Grimes 		break;
806df8bae1dSRodney W. Grimes #endif
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	default:
809df8bae1dSRodney W. Grimes 		splx(s);
810df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
811df8bae1dSRodney W. Grimes 	}
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	/*
814df8bae1dSRodney W. Grimes 	 * Look up the address in our list.
815df8bae1dSRodney W. Grimes 	 */
816df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
817df8bae1dSRodney W. Grimes 	if (enm == NULL) {
818df8bae1dSRodney W. Grimes 		splx(s);
819df8bae1dSRodney W. Grimes 		return (ENXIO);
820df8bae1dSRodney W. Grimes 	}
821df8bae1dSRodney W. Grimes 	if (--enm->enm_refcount != 0) {
822df8bae1dSRodney W. Grimes 		/*
823df8bae1dSRodney W. Grimes 		 * Still some claims to this record.
824df8bae1dSRodney W. Grimes 		 */
825df8bae1dSRodney W. Grimes 		splx(s);
826df8bae1dSRodney W. Grimes 		return (0);
827df8bae1dSRodney W. Grimes 	}
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * No remaining claims to this record; unlink and free it.
830df8bae1dSRodney W. Grimes 	 */
831df8bae1dSRodney W. Grimes 	for (p = &enm->enm_ac->ac_multiaddrs;
832df8bae1dSRodney W. Grimes 	     *p != enm;
833df8bae1dSRodney W. Grimes 	     p = &(*p)->enm_next)
834df8bae1dSRodney W. Grimes 		continue;
835df8bae1dSRodney W. Grimes 	*p = (*p)->enm_next;
836df8bae1dSRodney W. Grimes 	free(enm, M_IFMADDR);
837df8bae1dSRodney W. Grimes 	ac->ac_multicnt--;
838df8bae1dSRodney W. Grimes 	splx(s);
839d3628763SRodney W. Grimes       if (unset_allmulti)
840d3628763SRodney W. Grimes               ac->ac_if.if_flags &= ~IFF_ALLMULTI;
841d3628763SRodney W. Grimes 
842df8bae1dSRodney W. Grimes 	/*
843df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
844df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
845df8bae1dSRodney W. Grimes 	 */
846df8bae1dSRodney W. Grimes 	return (ENETRESET);
847df8bae1dSRodney W. Grimes }
848602d513cSGarrett Wollman 
849602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
85030106f6aSPoul-Henning Kamp 
85130106f6aSPoul-Henning Kamp void
85230106f6aSPoul-Henning Kamp ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
85330106f6aSPoul-Henning Kamp {
85430106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
85530106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
85630106f6aSPoul-Henning Kamp 
85730106f6aSPoul-Henning Kamp 	switch (command) {
85830106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
85930106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
86030106f6aSPoul-Henning Kamp 
86130106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
86230106f6aSPoul-Henning Kamp #ifdef INET
86330106f6aSPoul-Henning Kamp 		case AF_INET:
86430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
86530106f6aSPoul-Henning Kamp 			arp_ifinit((struct arpcom *)ifp, ifa);
86630106f6aSPoul-Henning Kamp 			break;
86730106f6aSPoul-Henning Kamp #endif
86830106f6aSPoul-Henning Kamp #ifdef IPX
86930106f6aSPoul-Henning Kamp 		/*
87030106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
87130106f6aSPoul-Henning Kamp 		 */
87230106f6aSPoul-Henning Kamp 		case AF_IPX:
87330106f6aSPoul-Henning Kamp 			{
87430106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
87586101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
87630106f6aSPoul-Henning Kamp 
87730106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
87830106f6aSPoul-Henning Kamp 				ina->x_host =
87986101139SPoul-Henning Kamp 				    *(union ipx_host *)
88086101139SPoul-Henning Kamp 			            ac->ac_enaddr;
88130106f6aSPoul-Henning Kamp 			else {
88230106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
88386101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
88486101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
88530106f6aSPoul-Henning Kamp 			}
88630106f6aSPoul-Henning Kamp 
88730106f6aSPoul-Henning Kamp 			/*
88830106f6aSPoul-Henning Kamp 			 * Set new address
88930106f6aSPoul-Henning Kamp 			 */
89030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
89130106f6aSPoul-Henning Kamp 			break;
89230106f6aSPoul-Henning Kamp 			}
89330106f6aSPoul-Henning Kamp #endif
89430106f6aSPoul-Henning Kamp #ifdef NS
89530106f6aSPoul-Henning Kamp 		/*
89630106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
89730106f6aSPoul-Henning Kamp 		 */
89830106f6aSPoul-Henning Kamp 		case AF_NS:
89930106f6aSPoul-Henning Kamp 		{
90030106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
90186101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
90230106f6aSPoul-Henning Kamp 
90330106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
90430106f6aSPoul-Henning Kamp 				ina->x_host =
90586101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
90630106f6aSPoul-Henning Kamp 			else {
90730106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
90886101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
90986101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
91030106f6aSPoul-Henning Kamp 			}
91130106f6aSPoul-Henning Kamp 
91230106f6aSPoul-Henning Kamp 			/*
91330106f6aSPoul-Henning Kamp 			 * Set new address
91430106f6aSPoul-Henning Kamp 			 */
91530106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
91630106f6aSPoul-Henning Kamp 			break;
91730106f6aSPoul-Henning Kamp 		}
91830106f6aSPoul-Henning Kamp #endif
91930106f6aSPoul-Henning Kamp 		default:
92030106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
92130106f6aSPoul-Henning Kamp 			break;
92230106f6aSPoul-Henning Kamp 		}
92330106f6aSPoul-Henning Kamp 		break;
92430106f6aSPoul-Henning Kamp 
92530106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
92630106f6aSPoul-Henning Kamp 		{
92730106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
92830106f6aSPoul-Henning Kamp 
92930106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
9305b73c186SDavid Greenman 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
93130106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
93230106f6aSPoul-Henning Kamp 		}
93330106f6aSPoul-Henning Kamp 		break;
93430106f6aSPoul-Henning Kamp 	}
93530106f6aSPoul-Henning Kamp 	return;
93630106f6aSPoul-Henning Kamp }
937