xref: /freebsd/sys/net/if_ethersubr.c (revision 88e1602b1fd922873b8581ba0461d5279e476250)
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
3488e1602bSPoul-Henning Kamp  * $Id: if_ethersubr.c,v 1.23 1996/08/04 11:45:37 phk 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>
71df8bae1dSRodney W. Grimes #endif
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #ifdef ISO
74df8bae1dSRodney W. Grimes #include <netiso/argo_debug.h>
75df8bae1dSRodney W. Grimes #include <netiso/iso.h>
76df8bae1dSRodney W. Grimes #include <netiso/iso_var.h>
77df8bae1dSRodney W. Grimes #include <netiso/iso_snpac.h>
78df8bae1dSRodney W. Grimes #endif
79df8bae1dSRodney W. Grimes 
80655929bfSJulian Elischer /*#ifdef LLC
81df8bae1dSRodney W. Grimes #include <netccitt/dll.h>
82df8bae1dSRodney W. Grimes #include <netccitt/llc_var.h>
83655929bfSJulian Elischer #endif*/
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #if defined(LLC) && defined(CCITT)
86df8bae1dSRodney W. Grimes extern struct ifqueue pkintrq;
87df8bae1dSRodney W. Grimes #endif
88df8bae1dSRodney W. Grimes 
89655929bfSJulian Elischer #ifdef NETATALK
90655929bfSJulian Elischer #include <netatalk/at.h>
91655929bfSJulian Elischer #include <netatalk/at_var.h>
92655929bfSJulian Elischer #include <netatalk/at_extern.h>
93655929bfSJulian Elischer 
94655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code
95655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type
96655929bfSJulian Elischer 
97655929bfSJulian Elischer extern u_char	at_org_code[ 3 ];
98655929bfSJulian Elischer extern u_char	aarp_org_code[ 3 ];
99655929bfSJulian Elischer #endif NETATALK
100655929bfSJulian Elischer 
101df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
102df8bae1dSRodney W. Grimes #define senderr(e) { error = (e); goto bad;}
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes /*
105df8bae1dSRodney W. Grimes  * Ethernet output routine.
106df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
107df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
108df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
109df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
110df8bae1dSRodney W. Grimes  */
111df8bae1dSRodney W. Grimes int
112df8bae1dSRodney W. Grimes ether_output(ifp, m0, dst, rt0)
113df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
114df8bae1dSRodney W. Grimes 	struct mbuf *m0;
115df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
116df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
117df8bae1dSRodney W. Grimes {
118df8bae1dSRodney W. Grimes 	short type;
119df8bae1dSRodney W. Grimes 	int s, error = 0;
120df8bae1dSRodney W. Grimes  	u_char edst[6];
121df8bae1dSRodney W. Grimes 	register struct mbuf *m = m0;
122df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
123df8bae1dSRodney W. Grimes 	struct mbuf *mcopy = (struct mbuf *)0;
124df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
125df8bae1dSRodney W. Grimes 	int off, len = m->m_pkthdr.len;
126df8bae1dSRodney W. Grimes 	struct arpcom *ac = (struct arpcom *)ifp;
127655929bfSJulian Elischer #ifdef NETATALK
128655929bfSJulian Elischer 	struct at_ifaddr *aa;
129655929bfSJulian Elischer #endif NETATALK
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
132df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
1333bda9f9bSPoul-Henning Kamp 	rt = rt0;
1343bda9f9bSPoul-Henning Kamp 	if (rt) {
135df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1363bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1373bda9f9bSPoul-Henning Kamp 			if (rt0)
138df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
139df8bae1dSRodney W. Grimes 			else
140df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
141df8bae1dSRodney W. Grimes 		}
142df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
143df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
144df8bae1dSRodney W. Grimes 				goto lookup;
145df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
146df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
147995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
148995add1aSGarrett Wollman 							  0UL);
149df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
150df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
151df8bae1dSRodney W. Grimes 			}
152df8bae1dSRodney W. Grimes 		}
153df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
154df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
155df8bae1dSRodney W. Grimes 			    time.tv_sec < rt->rt_rmx.rmx_expire)
156df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
157df8bae1dSRodney W. Grimes 	}
158df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes #ifdef INET
161df8bae1dSRodney W. Grimes 	case AF_INET:
1625df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
163df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
164df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
165df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
166df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
167df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
16834bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IP);
169df8bae1dSRodney W. Grimes 		break;
170df8bae1dSRodney W. Grimes #endif
171cc6a66f2SJulian Elischer #ifdef IPX
172cc6a66f2SJulian Elischer 	case AF_IPX:
17334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_IPX);
174cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
175cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
176cc6a66f2SJulian Elischer 		if (!bcmp((caddr_t)edst, (caddr_t)&ipx_thishost, sizeof(edst)))
177cc6a66f2SJulian Elischer 			return (looutput(ifp, m, dst, rt));
178cc6a66f2SJulian Elischer 		/* If broadcasting on a simplex interface, loopback a copy */
179cc6a66f2SJulian Elischer 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
180cc6a66f2SJulian Elischer 			mcopy = m_copy(m, 0, (int)M_COPYALL);
181cc6a66f2SJulian Elischer 		break;
182cc6a66f2SJulian Elischer #endif
183655929bfSJulian Elischer #ifdef NETATALK
184655929bfSJulian Elischer 	case AF_APPLETALK:
185655929bfSJulian Elischer             if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
186655929bfSJulian Elischer #ifdef NETATALKDEBUG
187655929bfSJulian Elischer                 extern char *prsockaddr(struct sockaddr *);
188655929bfSJulian Elischer                 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
189655929bfSJulian Elischer #endif NETATALKDEBUG
190655929bfSJulian Elischer                 return (0);
191655929bfSJulian Elischer             }
192655929bfSJulian Elischer 	    /*
193655929bfSJulian Elischer 	     * ifaddr is the first thing in at_ifaddr
194655929bfSJulian Elischer 	     */
195655929bfSJulian Elischer 	    if ((aa = (struct at_ifaddr *)at_ifawithnet(
196655929bfSJulian Elischer 			(struct sockaddr_at *)dst, ifp->if_addrlist))
197655929bfSJulian Elischer 		== 0)
198655929bfSJulian Elischer 		goto bad;
199655929bfSJulian Elischer 
200655929bfSJulian Elischer 	    /*
201655929bfSJulian Elischer 	     * In the phase 2 case, we need to prepend an mbuf for the llc header.
202655929bfSJulian Elischer 	     * Since we must preserve the value of m, which is passed to us by
203655929bfSJulian Elischer 	     * value, we m_copy() the first mbuf, and use it for our llc header.
204655929bfSJulian Elischer 	     */
205655929bfSJulian Elischer 	    if ( aa->aa_flags & AFA_PHASE2 ) {
206655929bfSJulian Elischer 		struct llc llc;
207655929bfSJulian Elischer 
208655929bfSJulian Elischer 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
209655929bfSJulian Elischer 		len += sizeof(struct llc);
210655929bfSJulian Elischer 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
211655929bfSJulian Elischer 		llc.llc_control = LLC_UI;
212655929bfSJulian Elischer 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
213655929bfSJulian Elischer 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
214655929bfSJulian Elischer 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
21534bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
216655929bfSJulian Elischer 	    } else {
21734bed8b0SDavid Greenman 		type = htons(ETHERTYPE_AT);
218655929bfSJulian Elischer 	    }
219655929bfSJulian Elischer 	    break;
220655929bfSJulian Elischer #endif NETATALK
221df8bae1dSRodney W. Grimes #ifdef NS
222df8bae1dSRodney W. Grimes 	case AF_NS:
22334bed8b0SDavid Greenman 		type = htons(ETHERTYPE_NS);
224df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
225df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
226df8bae1dSRodney W. Grimes 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
227df8bae1dSRodney W. Grimes 			return (looutput(ifp, m, dst, rt));
228df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
229df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
230df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
231df8bae1dSRodney W. Grimes 		break;
232df8bae1dSRodney W. Grimes #endif
233df8bae1dSRodney W. Grimes #ifdef	ISO
234df8bae1dSRodney W. Grimes 	case AF_ISO: {
235df8bae1dSRodney W. Grimes 		int	snpalen;
236df8bae1dSRodney W. Grimes 		struct	llc *l;
237df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl;
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
240df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
241df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
242df8bae1dSRodney W. Grimes 		} else if (error =
243df8bae1dSRodney W. Grimes 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
244df8bae1dSRodney W. Grimes 					    (char *)edst, &snpalen))
245df8bae1dSRodney W. Grimes 			goto bad; /* Not Resolved */
246df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
247df8bae1dSRodney W. Grimes 		if (*edst & 1)
248df8bae1dSRodney W. Grimes 			m->m_flags |= (M_BCAST|M_MCAST);
249df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
250df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
251df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
252df8bae1dSRodney W. Grimes 			if (mcopy) {
253df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
254df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
255df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
256df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
257df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
258df8bae1dSRodney W. Grimes 			}
259df8bae1dSRodney W. Grimes 		}
260df8bae1dSRodney W. Grimes 		M_PREPEND(m, 3, M_DONTWAIT);
261df8bae1dSRodney W. Grimes 		if (m == NULL)
262df8bae1dSRodney W. Grimes 			return (0);
26334bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
264df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
265df8bae1dSRodney W. Grimes 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
266df8bae1dSRodney W. Grimes 		l->llc_control = LLC_UI;
267df8bae1dSRodney W. Grimes 		len += 3;
268df8bae1dSRodney W. Grimes 		IFDEBUG(D_ETHER)
269df8bae1dSRodney W. Grimes 			int i;
270df8bae1dSRodney W. Grimes 			printf("unoutput: sending pkt to: ");
271df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
272df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
273df8bae1dSRodney W. Grimes 			printf("\n");
274df8bae1dSRodney W. Grimes 		ENDDEBUG
275df8bae1dSRodney W. Grimes 		} break;
276df8bae1dSRodney W. Grimes #endif /* ISO */
277df8bae1dSRodney W. Grimes #ifdef	LLC
278df8bae1dSRodney W. Grimes /*	case AF_NSAP: */
279df8bae1dSRodney W. Grimes 	case AF_CCITT: {
280df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl =
281df8bae1dSRodney W. Grimes 			(struct sockaddr_dl *) rt -> rt_gateway;
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes 		if (sdl && sdl->sdl_family == AF_LINK
284df8bae1dSRodney W. Grimes 		    && sdl->sdl_alen > 0) {
285df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (char *)edst,
286df8bae1dSRodney W. Grimes 				sizeof(edst));
287df8bae1dSRodney W. Grimes 		} else goto bad; /* Not a link interface ? Funny ... */
288df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
289df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
290df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
291df8bae1dSRodney W. Grimes 			if (mcopy) {
292df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
293df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
294df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
295df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
296df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
297df8bae1dSRodney W. Grimes 			}
298df8bae1dSRodney W. Grimes 		}
29934bed8b0SDavid Greenman 		type = htons(m->m_pkthdr.len);
300df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
301df8bae1dSRodney W. Grimes 		{
302df8bae1dSRodney W. Grimes 			int i;
303df8bae1dSRodney W. Grimes 			register struct llc *l = mtod(m, struct llc *);
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 			printf("ether_output: sending LLC2 pkt to: ");
306df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
307df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
308df8bae1dSRodney W. Grimes 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
309df8bae1dSRodney W. Grimes 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
310df8bae1dSRodney W. Grimes 			       l->llc_control & 0xff);
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 		}
313df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
314df8bae1dSRodney W. Grimes 		} break;
315df8bae1dSRodney W. Grimes #endif /* LLC */
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
318df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
31994a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
320df8bae1dSRodney W. Grimes 		type = eh->ether_type;
321df8bae1dSRodney W. Grimes 		break;
322df8bae1dSRodney W. Grimes 
323df8bae1dSRodney W. Grimes 	default:
324df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
325df8bae1dSRodney W. Grimes 			dst->sa_family);
326df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	if (mcopy)
331df8bae1dSRodney W. Grimes 		(void) looutput(ifp, mcopy, dst, rt);
332df8bae1dSRodney W. Grimes 	/*
333df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
334df8bae1dSRodney W. Grimes 	 * allocate another.
335df8bae1dSRodney W. Grimes 	 */
336df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
337df8bae1dSRodney W. Grimes 	if (m == 0)
338df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
339df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
34094a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
341df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
34294a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
34394a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_shost, ac->ac_enaddr,
344df8bae1dSRodney W. Grimes 	    sizeof(eh->ether_shost));
345df8bae1dSRodney W. Grimes 	s = splimp();
346df8bae1dSRodney W. Grimes 	/*
347df8bae1dSRodney W. Grimes 	 * Queue message on interface, and start output if interface
348df8bae1dSRodney W. Grimes 	 * not yet active.
349df8bae1dSRodney W. Grimes 	 */
350df8bae1dSRodney W. Grimes 	if (IF_QFULL(&ifp->if_snd)) {
351df8bae1dSRodney W. Grimes 		IF_DROP(&ifp->if_snd);
352df8bae1dSRodney W. Grimes 		splx(s);
353df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
354df8bae1dSRodney W. Grimes 	}
355df8bae1dSRodney W. Grimes 	IF_ENQUEUE(&ifp->if_snd, m);
356df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
357df8bae1dSRodney W. Grimes 		(*ifp->if_start)(ifp);
358df8bae1dSRodney W. Grimes 	splx(s);
359df8bae1dSRodney W. Grimes 	ifp->if_obytes += len + sizeof (struct ether_header);
360df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST)
361df8bae1dSRodney W. Grimes 		ifp->if_omcasts++;
362df8bae1dSRodney W. Grimes 	return (error);
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes bad:
365df8bae1dSRodney W. Grimes 	if (m)
366df8bae1dSRodney W. Grimes 		m_freem(m);
367df8bae1dSRodney W. Grimes 	return (error);
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes /*
371df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
372df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
373df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
374df8bae1dSRodney W. Grimes  */
375df8bae1dSRodney W. Grimes void
376df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
377df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
378df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
379df8bae1dSRodney W. Grimes 	struct mbuf *m;
380df8bae1dSRodney W. Grimes {
381df8bae1dSRodney W. Grimes 	register struct ifqueue *inq;
382307d80beSDavid Greenman 	u_short ether_type;
383df8bae1dSRodney W. Grimes 	int s;
3848e3bda06SJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
385c23670e2SGary Palmer 	register struct llc *l;
386c23670e2SGary Palmer #endif
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
389df8bae1dSRodney W. Grimes 		m_freem(m);
390df8bae1dSRodney W. Grimes 		return;
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
393df8bae1dSRodney W. Grimes 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
394df8bae1dSRodney W. Grimes 	    sizeof(etherbroadcastaddr)) == 0)
395df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
396df8bae1dSRodney W. Grimes 	else if (eh->ether_dhost[0] & 1)
397df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
398df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
399df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
400df8bae1dSRodney W. Grimes 
401307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
402307d80beSDavid Greenman 
403307d80beSDavid Greenman 	switch (ether_type) {
404df8bae1dSRodney W. Grimes #ifdef INET
405df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
406df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
407df8bae1dSRodney W. Grimes 		inq = &ipintrq;
408df8bae1dSRodney W. Grimes 		break;
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
411df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
412df8bae1dSRodney W. Grimes 		inq = &arpintrq;
413df8bae1dSRodney W. Grimes 		break;
414df8bae1dSRodney W. Grimes #endif
415cc6a66f2SJulian Elischer #ifdef IPX
416cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
417cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
418cc6a66f2SJulian Elischer 		inq = &ipxintrq;
419cc6a66f2SJulian Elischer 		break;
420cc6a66f2SJulian Elischer #endif
421df8bae1dSRodney W. Grimes #ifdef NS
422df8bae1dSRodney W. Grimes 	case ETHERTYPE_NS:
423df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
424df8bae1dSRodney W. Grimes 		inq = &nsintrq;
425df8bae1dSRodney W. Grimes 		break;
426df8bae1dSRodney W. Grimes #endif
427655929bfSJulian Elischer #ifdef NETATALK
428655929bfSJulian Elischer         case ETHERTYPE_AT:
429655929bfSJulian Elischer                 schednetisr(NETISR_ATALK);
430655929bfSJulian Elischer                 inq = &atintrq1;
431655929bfSJulian Elischer                 break;
432655929bfSJulian Elischer         case ETHERTYPE_AARP:
433655929bfSJulian Elischer 		/* probably this should be done with a NETISR as well */
434655929bfSJulian Elischer                 aarpinput((struct arpcom *)ifp, m); /* XXX */
435655929bfSJulian Elischer                 return;
436655929bfSJulian Elischer #endif NETATALK
437df8bae1dSRodney W. Grimes 	default:
438655929bfSJulian Elischer #if defined (ISO) || defined (LLC) || defined(NETATALK)
439307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
440df8bae1dSRodney W. Grimes 			goto dropanyway;
441df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
442df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
443655929bfSJulian Elischer #ifdef NETATALK
444655929bfSJulian Elischer 		case LLC_SNAP_LSAP:
445655929bfSJulian Elischer 		    switch (l->llc_control) {
446655929bfSJulian Elischer 		    case LLC_UI:
447655929bfSJulian Elischer 			if (l->llc_ssap != LLC_SNAP_LSAP)
448655929bfSJulian Elischer 			    goto dropanyway;
449655929bfSJulian Elischer 
450655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
451655929bfSJulian Elischer 				   sizeof(at_org_code)) == 0 &&
452655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
453655929bfSJulian Elischer 			    inq = &atintrq2;
454655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
455655929bfSJulian Elischer 			    schednetisr(NETISR_ATALK);
456655929bfSJulian Elischer 			    break;
457655929bfSJulian Elischer 			}
458655929bfSJulian Elischer 
459655929bfSJulian Elischer 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
460655929bfSJulian Elischer 				   sizeof(aarp_org_code)) == 0 &&
461655929bfSJulian Elischer 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
462655929bfSJulian Elischer 			    m_adj( m, sizeof( struct llc ));
463655929bfSJulian Elischer 			    aarpinput((struct arpcom *)ifp, m); /* XXX */
464655929bfSJulian Elischer 			    return;
465655929bfSJulian Elischer 			}
466655929bfSJulian Elischer 
467655929bfSJulian Elischer 		    default:
468655929bfSJulian Elischer 			goto dropanyway;
469655929bfSJulian Elischer 		    }
470655929bfSJulian Elischer 		    break;
471655929bfSJulian Elischer #endif NETATALK
472df8bae1dSRodney W. Grimes #ifdef	ISO
473df8bae1dSRodney W. Grimes 		case LLC_ISO_LSAP:
474df8bae1dSRodney W. Grimes 			switch (l->llc_control) {
475df8bae1dSRodney W. Grimes 			case LLC_UI:
476df8bae1dSRodney W. Grimes 				/* LLC_UI_P forbidden in class 1 service */
477df8bae1dSRodney W. Grimes 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
478df8bae1dSRodney W. Grimes 				    (l->llc_ssap == LLC_ISO_LSAP)) {
479df8bae1dSRodney W. Grimes 					/* LSAP for ISO */
480307d80beSDavid Greenman 					if (m->m_pkthdr.len > ether_type)
481307d80beSDavid Greenman 						m_adj(m, ether_type - m->m_pkthdr.len);
482df8bae1dSRodney W. Grimes 					m->m_data += 3;		/* XXX */
483df8bae1dSRodney W. Grimes 					m->m_len -= 3;		/* XXX */
484df8bae1dSRodney W. Grimes 					m->m_pkthdr.len -= 3;	/* XXX */
485df8bae1dSRodney W. Grimes 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
486df8bae1dSRodney W. Grimes 					if (m == 0)
487df8bae1dSRodney W. Grimes 						return;
488df8bae1dSRodney W. Grimes 					*mtod(m, struct ether_header *) = *eh;
489df8bae1dSRodney W. Grimes 					IFDEBUG(D_ETHER)
490df8bae1dSRodney W. Grimes 						printf("clnp packet");
491df8bae1dSRodney W. Grimes 					ENDDEBUG
492df8bae1dSRodney W. Grimes 					schednetisr(NETISR_ISO);
493df8bae1dSRodney W. Grimes 					inq = &clnlintrq;
494df8bae1dSRodney W. Grimes 					break;
495df8bae1dSRodney W. Grimes 				}
496df8bae1dSRodney W. Grimes 				goto dropanyway;
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 			case LLC_XID:
499df8bae1dSRodney W. Grimes 			case LLC_XID_P:
500df8bae1dSRodney W. Grimes 				if(m->m_len < 6)
501df8bae1dSRodney W. Grimes 					goto dropanyway;
502df8bae1dSRodney W. Grimes 				l->llc_window = 0;
503df8bae1dSRodney W. Grimes 				l->llc_fid = 9;
504df8bae1dSRodney W. Grimes 				l->llc_class = 1;
505df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap = 0;
506df8bae1dSRodney W. Grimes 				/* Fall through to */
507df8bae1dSRodney W. Grimes 			case LLC_TEST:
508df8bae1dSRodney W. Grimes 			case LLC_TEST_P:
509df8bae1dSRodney W. Grimes 			{
510df8bae1dSRodney W. Grimes 				struct sockaddr sa;
511df8bae1dSRodney W. Grimes 				register struct ether_header *eh2;
512df8bae1dSRodney W. Grimes 				int i;
513df8bae1dSRodney W. Grimes 				u_char c = l->llc_dsap;
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap;
516df8bae1dSRodney W. Grimes 				l->llc_ssap = c;
517df8bae1dSRodney W. Grimes 				if (m->m_flags & (M_BCAST | M_MCAST))
518df8bae1dSRodney W. Grimes 					bcopy((caddr_t)ac->ac_enaddr,
519df8bae1dSRodney W. Grimes 					      (caddr_t)eh->ether_dhost, 6);
520df8bae1dSRodney W. Grimes 				sa.sa_family = AF_UNSPEC;
521df8bae1dSRodney W. Grimes 				sa.sa_len = sizeof(sa);
522df8bae1dSRodney W. Grimes 				eh2 = (struct ether_header *)sa.sa_data;
523df8bae1dSRodney W. Grimes 				for (i = 0; i < 6; i++) {
524df8bae1dSRodney W. Grimes 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
525df8bae1dSRodney W. Grimes 					eh2->ether_dhost[i] =
526df8bae1dSRodney W. Grimes 						eh->ether_dhost[i] = eh->ether_shost[i];
527df8bae1dSRodney W. Grimes 					eh->ether_shost[i] = c;
528df8bae1dSRodney W. Grimes 				}
529df8bae1dSRodney W. Grimes 				ifp->if_output(ifp, m, &sa, NULL);
530df8bae1dSRodney W. Grimes 				return;
531df8bae1dSRodney W. Grimes 			}
532df8bae1dSRodney W. Grimes 			default:
533df8bae1dSRodney W. Grimes 				m_freem(m);
534df8bae1dSRodney W. Grimes 				return;
535df8bae1dSRodney W. Grimes 			}
536df8bae1dSRodney W. Grimes 			break;
537df8bae1dSRodney W. Grimes #endif /* ISO */
538df8bae1dSRodney W. Grimes #ifdef LLC
539df8bae1dSRodney W. Grimes 		case LLC_X25_LSAP:
540df8bae1dSRodney W. Grimes 		{
541307d80beSDavid Greenman 			if (m->m_pkthdr.len > ether_type)
542307d80beSDavid Greenman 				m_adj(m, ether_type - m->m_pkthdr.len);
543df8bae1dSRodney W. Grimes 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
544df8bae1dSRodney W. Grimes 			if (m == 0)
545df8bae1dSRodney W. Grimes 				return;
546df8bae1dSRodney W. Grimes 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
547df8bae1dSRodney W. Grimes 					    eh->ether_dhost, LLC_X25_LSAP, 6,
548df8bae1dSRodney W. Grimes 					    mtod(m, struct sdl_hdr *)))
549df8bae1dSRodney W. Grimes 				panic("ETHER cons addr failure");
550307d80beSDavid Greenman 			mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
551df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
552df8bae1dSRodney W. Grimes 				printf("llc packet\n");
553df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
554df8bae1dSRodney W. Grimes 			schednetisr(NETISR_CCITT);
555df8bae1dSRodney W. Grimes 			inq = &llcintrq;
556df8bae1dSRodney W. Grimes 			break;
557df8bae1dSRodney W. Grimes 		}
558df8bae1dSRodney W. Grimes #endif /* LLC */
559df8bae1dSRodney W. Grimes 		dropanyway:
560df8bae1dSRodney W. Grimes 		default:
561df8bae1dSRodney W. Grimes 			m_freem(m);
562df8bae1dSRodney W. Grimes 			return;
563df8bae1dSRodney W. Grimes 		}
564655929bfSJulian Elischer #else /* ISO || LLC || NETATALK */
565df8bae1dSRodney W. Grimes 	    m_freem(m);
566df8bae1dSRodney W. Grimes 	    return;
567655929bfSJulian Elischer #endif /* ISO || LLC || NETATALK */
568df8bae1dSRodney W. Grimes 	}
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes 	s = splimp();
571df8bae1dSRodney W. Grimes 	if (IF_QFULL(inq)) {
572df8bae1dSRodney W. Grimes 		IF_DROP(inq);
573df8bae1dSRodney W. Grimes 		m_freem(m);
574df8bae1dSRodney W. Grimes 	} else
575df8bae1dSRodney W. Grimes 		IF_ENQUEUE(inq, m);
576df8bae1dSRodney W. Grimes 	splx(s);
577df8bae1dSRodney W. Grimes }
578df8bae1dSRodney W. Grimes 
579df8bae1dSRodney W. Grimes /*
580df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
581df8bae1dSRodney W. Grimes  */
582df8bae1dSRodney W. Grimes void
583df8bae1dSRodney W. Grimes ether_ifattach(ifp)
584df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
585df8bae1dSRodney W. Grimes {
586df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
587df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
588df8bae1dSRodney W. Grimes 
589df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
590df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
591df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
592df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
593a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
594a330e1f1SGary Palmer 	    ifp->if_baudrate = 10000000;
595df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
596df8bae1dSRodney W. Grimes 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
597df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK) {
598df8bae1dSRodney W. Grimes 			sdl->sdl_type = IFT_ETHER;
599df8bae1dSRodney W. Grimes 			sdl->sdl_alen = ifp->if_addrlen;
600df8bae1dSRodney W. Grimes 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
601df8bae1dSRodney W. Grimes 			      LLADDR(sdl), ifp->if_addrlen);
602df8bae1dSRodney W. Grimes 			break;
603df8bae1dSRodney W. Grimes 		}
604df8bae1dSRodney W. Grimes }
605df8bae1dSRodney W. Grimes 
6063bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_min[6] =
6073bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
6083bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_max[6] =
6093bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
610df8bae1dSRodney W. Grimes /*
611df8bae1dSRodney W. Grimes  * Add an Ethernet multicast address or range of addresses to the list for a
612df8bae1dSRodney W. Grimes  * given interface.
613df8bae1dSRodney W. Grimes  */
614df8bae1dSRodney W. Grimes int
615df8bae1dSRodney W. Grimes ether_addmulti(ifr, ac)
616df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
617df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
618df8bae1dSRodney W. Grimes {
619df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
620df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
621df8bae1dSRodney W. Grimes 	u_char addrlo[6];
622df8bae1dSRodney W. Grimes 	u_char addrhi[6];
623d3628763SRodney W. Grimes         int set_allmulti = 0;
624df8bae1dSRodney W. Grimes 	int s = splimp();
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
629df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
630df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
631df8bae1dSRodney W. Grimes 		break;
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes #ifdef INET
634df8bae1dSRodney W. Grimes 	case AF_INET:
635df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
636df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
637df8bae1dSRodney W. Grimes 			/*
638df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means listen to all
639df8bae1dSRodney W. Grimes 			 * of the Ethernet multicast addresses used for IP.
640df8bae1dSRodney W. Grimes 			 * (This is for the sake of IP multicast routers.)
641df8bae1dSRodney W. Grimes 			 */
642df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
643df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
644d3628763SRodney W. Grimes                       set_allmulti = 1;
645df8bae1dSRodney W. Grimes 		}
646df8bae1dSRodney W. Grimes 		else {
647df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
648df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
649df8bae1dSRodney W. Grimes 		}
650df8bae1dSRodney W. Grimes 		break;
651df8bae1dSRodney W. Grimes #endif
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	default:
654df8bae1dSRodney W. Grimes 		splx(s);
655df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
656df8bae1dSRodney W. Grimes 	}
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	/*
659df8bae1dSRodney W. Grimes 	 * Verify that we have valid Ethernet multicast addresses.
660df8bae1dSRodney W. Grimes 	 */
661df8bae1dSRodney W. Grimes 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
662df8bae1dSRodney W. Grimes 		splx(s);
663df8bae1dSRodney W. Grimes 		return (EINVAL);
664df8bae1dSRodney W. Grimes 	}
665df8bae1dSRodney W. Grimes 	/*
666df8bae1dSRodney W. Grimes 	 * See if the address range is already in the list.
667df8bae1dSRodney W. Grimes 	 */
668df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
669df8bae1dSRodney W. Grimes 	if (enm != NULL) {
670df8bae1dSRodney W. Grimes 		/*
671df8bae1dSRodney W. Grimes 		 * Found it; just increment the reference count.
672df8bae1dSRodney W. Grimes 		 */
673df8bae1dSRodney W. Grimes 		++enm->enm_refcount;
674df8bae1dSRodney W. Grimes 		splx(s);
675df8bae1dSRodney W. Grimes 		return (0);
676df8bae1dSRodney W. Grimes 	}
677df8bae1dSRodney W. Grimes 	/*
678df8bae1dSRodney W. Grimes 	 * New address or range; malloc a new multicast record
679df8bae1dSRodney W. Grimes 	 * and link it into the interface's multicast list.
680df8bae1dSRodney W. Grimes 	 */
681df8bae1dSRodney W. Grimes 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
682df8bae1dSRodney W. Grimes 	if (enm == NULL) {
683df8bae1dSRodney W. Grimes 		splx(s);
684df8bae1dSRodney W. Grimes 		return (ENOBUFS);
685df8bae1dSRodney W. Grimes 	}
686df8bae1dSRodney W. Grimes 	bcopy(addrlo, enm->enm_addrlo, 6);
687df8bae1dSRodney W. Grimes 	bcopy(addrhi, enm->enm_addrhi, 6);
688df8bae1dSRodney W. Grimes 	enm->enm_ac = ac;
689df8bae1dSRodney W. Grimes 	enm->enm_refcount = 1;
690df8bae1dSRodney W. Grimes 	enm->enm_next = ac->ac_multiaddrs;
691df8bae1dSRodney W. Grimes 	ac->ac_multiaddrs = enm;
692df8bae1dSRodney W. Grimes 	ac->ac_multicnt++;
693df8bae1dSRodney W. Grimes 	splx(s);
694d3628763SRodney W. Grimes         if (set_allmulti)
695d3628763SRodney W. Grimes         	ac->ac_if.if_flags |= IFF_ALLMULTI;
696d3628763SRodney W. Grimes 
697df8bae1dSRodney W. Grimes 	/*
698df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
699df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
700df8bae1dSRodney W. Grimes 	 */
701df8bae1dSRodney W. Grimes 	return (ENETRESET);
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes /*
705df8bae1dSRodney W. Grimes  * Delete a multicast address record.
706df8bae1dSRodney W. Grimes  */
707df8bae1dSRodney W. Grimes int
708df8bae1dSRodney W. Grimes ether_delmulti(ifr, ac)
709df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
710df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
711df8bae1dSRodney W. Grimes {
712df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
713df8bae1dSRodney W. Grimes 	register struct ether_multi **p;
714df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
715df8bae1dSRodney W. Grimes 	u_char addrlo[6];
716df8bae1dSRodney W. Grimes 	u_char addrhi[6];
717d3628763SRodney W. Grimes       int unset_allmulti = 0;
718df8bae1dSRodney W. Grimes 	int s = splimp();
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
723df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
724df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
725df8bae1dSRodney W. Grimes 		break;
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes #ifdef INET
728df8bae1dSRodney W. Grimes 	case AF_INET:
729df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
730df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
731df8bae1dSRodney W. Grimes 			/*
732df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means stop listening
733df8bae1dSRodney W. Grimes 			 * to the range of Ethernet multicast addresses used
734df8bae1dSRodney W. Grimes 			 * for IP.
735df8bae1dSRodney W. Grimes 			 */
736df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
737df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
738d3628763SRodney W. Grimes                       unset_allmulti = 1;
739df8bae1dSRodney W. Grimes 		}
740df8bae1dSRodney W. Grimes 		else {
741df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
742df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
743df8bae1dSRodney W. Grimes 		}
744df8bae1dSRodney W. Grimes 		break;
745df8bae1dSRodney W. Grimes #endif
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	default:
748df8bae1dSRodney W. Grimes 		splx(s);
749df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
750df8bae1dSRodney W. Grimes 	}
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	/*
753df8bae1dSRodney W. Grimes 	 * Look up the address in our list.
754df8bae1dSRodney W. Grimes 	 */
755df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
756df8bae1dSRodney W. Grimes 	if (enm == NULL) {
757df8bae1dSRodney W. Grimes 		splx(s);
758df8bae1dSRodney W. Grimes 		return (ENXIO);
759df8bae1dSRodney W. Grimes 	}
760df8bae1dSRodney W. Grimes 	if (--enm->enm_refcount != 0) {
761df8bae1dSRodney W. Grimes 		/*
762df8bae1dSRodney W. Grimes 		 * Still some claims to this record.
763df8bae1dSRodney W. Grimes 		 */
764df8bae1dSRodney W. Grimes 		splx(s);
765df8bae1dSRodney W. Grimes 		return (0);
766df8bae1dSRodney W. Grimes 	}
767df8bae1dSRodney W. Grimes 	/*
768df8bae1dSRodney W. Grimes 	 * No remaining claims to this record; unlink and free it.
769df8bae1dSRodney W. Grimes 	 */
770df8bae1dSRodney W. Grimes 	for (p = &enm->enm_ac->ac_multiaddrs;
771df8bae1dSRodney W. Grimes 	     *p != enm;
772df8bae1dSRodney W. Grimes 	     p = &(*p)->enm_next)
773df8bae1dSRodney W. Grimes 		continue;
774df8bae1dSRodney W. Grimes 	*p = (*p)->enm_next;
775df8bae1dSRodney W. Grimes 	free(enm, M_IFMADDR);
776df8bae1dSRodney W. Grimes 	ac->ac_multicnt--;
777df8bae1dSRodney W. Grimes 	splx(s);
778d3628763SRodney W. Grimes       if (unset_allmulti)
779d3628763SRodney W. Grimes               ac->ac_if.if_flags &= ~IFF_ALLMULTI;
780d3628763SRodney W. Grimes 
781df8bae1dSRodney W. Grimes 	/*
782df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
783df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
784df8bae1dSRodney W. Grimes 	 */
785df8bae1dSRodney W. Grimes 	return (ENETRESET);
786df8bae1dSRodney W. Grimes }
787602d513cSGarrett Wollman 
788602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
78930106f6aSPoul-Henning Kamp 
79030106f6aSPoul-Henning Kamp void
79130106f6aSPoul-Henning Kamp ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
79230106f6aSPoul-Henning Kamp {
79330106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
79430106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
79530106f6aSPoul-Henning Kamp 
79630106f6aSPoul-Henning Kamp 	switch (command) {
79730106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
79830106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
79930106f6aSPoul-Henning Kamp 
80030106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
80130106f6aSPoul-Henning Kamp #ifdef INET
80230106f6aSPoul-Henning Kamp 		case AF_INET:
80330106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
80430106f6aSPoul-Henning Kamp 			arp_ifinit((struct arpcom *)ifp, ifa);
80530106f6aSPoul-Henning Kamp 			break;
80630106f6aSPoul-Henning Kamp #endif
80730106f6aSPoul-Henning Kamp #ifdef IPX
80830106f6aSPoul-Henning Kamp 		/*
80930106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
81030106f6aSPoul-Henning Kamp 		 */
81130106f6aSPoul-Henning Kamp 		case AF_IPX:
81230106f6aSPoul-Henning Kamp 			{
81330106f6aSPoul-Henning Kamp 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
81486101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
81530106f6aSPoul-Henning Kamp 
81630106f6aSPoul-Henning Kamp 			if (ipx_nullhost(*ina))
81730106f6aSPoul-Henning Kamp 				ina->x_host =
81886101139SPoul-Henning Kamp 				    *(union ipx_host *)
81986101139SPoul-Henning Kamp 			            ac->ac_enaddr;
82030106f6aSPoul-Henning Kamp 			else {
82130106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
82286101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
82386101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
82430106f6aSPoul-Henning Kamp 			}
82530106f6aSPoul-Henning Kamp 
82630106f6aSPoul-Henning Kamp 			/*
82730106f6aSPoul-Henning Kamp 			 * Set new address
82830106f6aSPoul-Henning Kamp 			 */
82930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
83030106f6aSPoul-Henning Kamp 			break;
83130106f6aSPoul-Henning Kamp 			}
83230106f6aSPoul-Henning Kamp #endif
83330106f6aSPoul-Henning Kamp #ifdef NS
83430106f6aSPoul-Henning Kamp 		/*
83530106f6aSPoul-Henning Kamp 		 * XXX - This code is probably wrong
83630106f6aSPoul-Henning Kamp 		 */
83730106f6aSPoul-Henning Kamp 		case AF_NS:
83830106f6aSPoul-Henning Kamp 		{
83930106f6aSPoul-Henning Kamp 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
84086101139SPoul-Henning Kamp 			struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
84130106f6aSPoul-Henning Kamp 
84230106f6aSPoul-Henning Kamp 			if (ns_nullhost(*ina))
84330106f6aSPoul-Henning Kamp 				ina->x_host =
84486101139SPoul-Henning Kamp 				    *(union ns_host *) (ac->ac_enaddr);
84530106f6aSPoul-Henning Kamp 			else {
84630106f6aSPoul-Henning Kamp 				bcopy((caddr_t) ina->x_host.c_host,
84786101139SPoul-Henning Kamp 				      (caddr_t) ac->ac_enaddr,
84886101139SPoul-Henning Kamp 				      sizeof(ac->ac_enaddr));
84930106f6aSPoul-Henning Kamp 			}
85030106f6aSPoul-Henning Kamp 
85130106f6aSPoul-Henning Kamp 			/*
85230106f6aSPoul-Henning Kamp 			 * Set new address
85330106f6aSPoul-Henning Kamp 			 */
85430106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
85530106f6aSPoul-Henning Kamp 			break;
85630106f6aSPoul-Henning Kamp 		}
85730106f6aSPoul-Henning Kamp #endif
85830106f6aSPoul-Henning Kamp 		default:
85930106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
86030106f6aSPoul-Henning Kamp 			break;
86130106f6aSPoul-Henning Kamp 		}
86230106f6aSPoul-Henning Kamp 		break;
86330106f6aSPoul-Henning Kamp 
86430106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
86530106f6aSPoul-Henning Kamp 		{
86630106f6aSPoul-Henning Kamp 			struct sockaddr *sa;
86730106f6aSPoul-Henning Kamp 
86830106f6aSPoul-Henning Kamp 			sa = (struct sockaddr *) & ifr->ifr_data;
86930106f6aSPoul-Henning Kamp 			bcopy((caddr_t) ifp->if_softc,
87030106f6aSPoul-Henning Kamp 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
87130106f6aSPoul-Henning Kamp 		}
87230106f6aSPoul-Henning Kamp 		break;
87330106f6aSPoul-Henning Kamp 	}
87430106f6aSPoul-Henning Kamp 	return;
87530106f6aSPoul-Henning Kamp }
876