xref: /freebsd/sys/net/if_ethersubr.c (revision 602d513c)
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
34602d513cSGarrett Wollman  * $Id: if_ethersubr.c,v 1.12 1995/12/09 20:47:11 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 <machine/cpu.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <net/if.h>
52df8bae1dSRodney W. Grimes #include <net/netisr.h>
53df8bae1dSRodney W. Grimes #include <net/route.h>
54df8bae1dSRodney W. Grimes #include <net/if_llc.h>
55df8bae1dSRodney W. Grimes #include <net/if_dl.h>
56df8bae1dSRodney W. Grimes #include <net/if_types.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #ifdef INET
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
61df8bae1dSRodney W. Grimes #endif
62df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
63df8bae1dSRodney W. Grimes 
64cc6a66f2SJulian Elischer #ifdef IPX
65cc6a66f2SJulian Elischer #include <netipx/ipx.h>
66cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
67cc6a66f2SJulian Elischer #endif
68cc6a66f2SJulian Elischer 
69df8bae1dSRodney W. Grimes #ifdef NS
70df8bae1dSRodney W. Grimes #include <netns/ns.h>
71df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
72df8bae1dSRodney W. Grimes #endif
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #ifdef ISO
75df8bae1dSRodney W. Grimes #include <netiso/argo_debug.h>
76df8bae1dSRodney W. Grimes #include <netiso/iso.h>
77df8bae1dSRodney W. Grimes #include <netiso/iso_var.h>
78df8bae1dSRodney W. Grimes #include <netiso/iso_snpac.h>
79df8bae1dSRodney W. Grimes #endif
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes #ifdef LLC
82df8bae1dSRodney W. Grimes #include <netccitt/dll.h>
83df8bae1dSRodney W. Grimes #include <netccitt/llc_var.h>
84df8bae1dSRodney W. Grimes #endif
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #if defined(LLC) && defined(CCITT)
87df8bae1dSRodney W. Grimes extern struct ifqueue pkintrq;
88df8bae1dSRodney W. Grimes #endif
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
91df8bae1dSRodney W. Grimes #define senderr(e) { error = (e); goto bad;}
92df8bae1dSRodney W. Grimes 
93df8bae1dSRodney W. Grimes /*
94df8bae1dSRodney W. Grimes  * Ethernet output routine.
95df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
96df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
97df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
98df8bae1dSRodney W. Grimes  * Assumes that ifp is actually pointer to arpcom structure.
99df8bae1dSRodney W. Grimes  */
100df8bae1dSRodney W. Grimes int
101df8bae1dSRodney W. Grimes ether_output(ifp, m0, dst, rt0)
102df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
103df8bae1dSRodney W. Grimes 	struct mbuf *m0;
104df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
105df8bae1dSRodney W. Grimes 	struct rtentry *rt0;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 	short type;
108df8bae1dSRodney W. Grimes 	int s, error = 0;
109df8bae1dSRodney W. Grimes  	u_char edst[6];
110df8bae1dSRodney W. Grimes 	register struct mbuf *m = m0;
111df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
112df8bae1dSRodney W. Grimes 	struct mbuf *mcopy = (struct mbuf *)0;
113df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
114df8bae1dSRodney W. Grimes 	int off, len = m->m_pkthdr.len;
115df8bae1dSRodney W. Grimes 	struct arpcom *ac = (struct arpcom *)ifp;
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
118df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
119df8bae1dSRodney W. Grimes 	ifp->if_lastchange = time;
1203bda9f9bSPoul-Henning Kamp 	rt = rt0;
1213bda9f9bSPoul-Henning Kamp 	if (rt) {
122df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_UP) == 0) {
1233bda9f9bSPoul-Henning Kamp 			rt0 = rt = rtalloc1(dst, 1, 0UL);
1243bda9f9bSPoul-Henning Kamp 			if (rt0)
125df8bae1dSRodney W. Grimes 				rt->rt_refcnt--;
126df8bae1dSRodney W. Grimes 			else
127df8bae1dSRodney W. Grimes 				senderr(EHOSTUNREACH);
128df8bae1dSRodney W. Grimes 		}
129df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_GATEWAY) {
130df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute == 0)
131df8bae1dSRodney W. Grimes 				goto lookup;
132df8bae1dSRodney W. Grimes 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
133df8bae1dSRodney W. Grimes 				rtfree(rt); rt = rt0;
134995add1aSGarrett Wollman 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
135995add1aSGarrett Wollman 							  0UL);
136df8bae1dSRodney W. Grimes 				if ((rt = rt->rt_gwroute) == 0)
137df8bae1dSRodney W. Grimes 					senderr(EHOSTUNREACH);
138df8bae1dSRodney W. Grimes 			}
139df8bae1dSRodney W. Grimes 		}
140df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_REJECT)
141df8bae1dSRodney W. Grimes 			if (rt->rt_rmx.rmx_expire == 0 ||
142df8bae1dSRodney W. Grimes 			    time.tv_sec < rt->rt_rmx.rmx_expire)
143df8bae1dSRodney W. Grimes 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
144df8bae1dSRodney W. Grimes 	}
145df8bae1dSRodney W. Grimes 	switch (dst->sa_family) {
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes #ifdef INET
148df8bae1dSRodney W. Grimes 	case AF_INET:
1495df72964SGarrett Wollman 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
150df8bae1dSRodney W. Grimes 			return (0);	/* if not yet resolved */
151df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
152df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
153df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
154df8bae1dSRodney W. Grimes 		off = m->m_pkthdr.len - m->m_len;
155df8bae1dSRodney W. Grimes 		type = ETHERTYPE_IP;
156df8bae1dSRodney W. Grimes 		break;
157df8bae1dSRodney W. Grimes #endif
158cc6a66f2SJulian Elischer #ifdef IPX
159cc6a66f2SJulian Elischer 	case AF_IPX:
160cc6a66f2SJulian Elischer 		type = ETHERTYPE_IPX;
161cc6a66f2SJulian Elischer  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
162cc6a66f2SJulian Elischer 		    (caddr_t)edst, sizeof (edst));
163cc6a66f2SJulian Elischer 		if (!bcmp((caddr_t)edst, (caddr_t)&ipx_thishost, sizeof(edst)))
164cc6a66f2SJulian Elischer 			return (looutput(ifp, m, dst, rt));
165cc6a66f2SJulian Elischer 		/* If broadcasting on a simplex interface, loopback a copy */
166cc6a66f2SJulian Elischer 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
167cc6a66f2SJulian Elischer 			mcopy = m_copy(m, 0, (int)M_COPYALL);
168cc6a66f2SJulian Elischer 		break;
169cc6a66f2SJulian Elischer #endif
170df8bae1dSRodney W. Grimes #ifdef NS
171df8bae1dSRodney W. Grimes 	case AF_NS:
172df8bae1dSRodney W. Grimes 		type = ETHERTYPE_NS;
173df8bae1dSRodney W. Grimes  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
174df8bae1dSRodney W. Grimes 		    (caddr_t)edst, sizeof (edst));
175df8bae1dSRodney W. Grimes 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
176df8bae1dSRodney W. Grimes 			return (looutput(ifp, m, dst, rt));
177df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
178df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
179df8bae1dSRodney W. Grimes 			mcopy = m_copy(m, 0, (int)M_COPYALL);
180df8bae1dSRodney W. Grimes 		break;
181df8bae1dSRodney W. Grimes #endif
182df8bae1dSRodney W. Grimes #ifdef	ISO
183df8bae1dSRodney W. Grimes 	case AF_ISO: {
184df8bae1dSRodney W. Grimes 		int	snpalen;
185df8bae1dSRodney W. Grimes 		struct	llc *l;
186df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl;
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
189df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
190df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
191df8bae1dSRodney W. Grimes 		} else if (error =
192df8bae1dSRodney W. Grimes 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
193df8bae1dSRodney W. Grimes 					    (char *)edst, &snpalen))
194df8bae1dSRodney W. Grimes 			goto bad; /* Not Resolved */
195df8bae1dSRodney W. Grimes 		/* If broadcasting on a simplex interface, loopback a copy */
196df8bae1dSRodney W. Grimes 		if (*edst & 1)
197df8bae1dSRodney W. Grimes 			m->m_flags |= (M_BCAST|M_MCAST);
198df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
199df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
200df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
201df8bae1dSRodney W. Grimes 			if (mcopy) {
202df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
203df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
204df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
205df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
206df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
207df8bae1dSRodney W. Grimes 			}
208df8bae1dSRodney W. Grimes 		}
209df8bae1dSRodney W. Grimes 		M_PREPEND(m, 3, M_DONTWAIT);
210df8bae1dSRodney W. Grimes 		if (m == NULL)
211df8bae1dSRodney W. Grimes 			return (0);
212df8bae1dSRodney W. Grimes 		type = m->m_pkthdr.len;
213df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
214df8bae1dSRodney W. Grimes 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
215df8bae1dSRodney W. Grimes 		l->llc_control = LLC_UI;
216df8bae1dSRodney W. Grimes 		len += 3;
217df8bae1dSRodney W. Grimes 		IFDEBUG(D_ETHER)
218df8bae1dSRodney W. Grimes 			int i;
219df8bae1dSRodney W. Grimes 			printf("unoutput: sending pkt to: ");
220df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
221df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
222df8bae1dSRodney W. Grimes 			printf("\n");
223df8bae1dSRodney W. Grimes 		ENDDEBUG
224df8bae1dSRodney W. Grimes 		} break;
225df8bae1dSRodney W. Grimes #endif /* ISO */
226df8bae1dSRodney W. Grimes #ifdef	LLC
227df8bae1dSRodney W. Grimes /*	case AF_NSAP: */
228df8bae1dSRodney W. Grimes 	case AF_CCITT: {
229df8bae1dSRodney W. Grimes 		register struct sockaddr_dl *sdl =
230df8bae1dSRodney W. Grimes 			(struct sockaddr_dl *) rt -> rt_gateway;
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes 		if (sdl && sdl->sdl_family == AF_LINK
233df8bae1dSRodney W. Grimes 		    && sdl->sdl_alen > 0) {
234df8bae1dSRodney W. Grimes 			bcopy(LLADDR(sdl), (char *)edst,
235df8bae1dSRodney W. Grimes 				sizeof(edst));
236df8bae1dSRodney W. Grimes 		} else goto bad; /* Not a link interface ? Funny ... */
237df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
238df8bae1dSRodney W. Grimes 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
239df8bae1dSRodney W. Grimes 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
240df8bae1dSRodney W. Grimes 			if (mcopy) {
241df8bae1dSRodney W. Grimes 				eh = mtod(mcopy, struct ether_header *);
242df8bae1dSRodney W. Grimes 				bcopy((caddr_t)edst,
243df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_dhost, sizeof (edst));
244df8bae1dSRodney W. Grimes 				bcopy((caddr_t)ac->ac_enaddr,
245df8bae1dSRodney W. Grimes 				      (caddr_t)eh->ether_shost, sizeof (edst));
246df8bae1dSRodney W. Grimes 			}
247df8bae1dSRodney W. Grimes 		}
248df8bae1dSRodney W. Grimes 		type = m->m_pkthdr.len;
249df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
250df8bae1dSRodney W. Grimes 		{
251df8bae1dSRodney W. Grimes 			int i;
252df8bae1dSRodney W. Grimes 			register struct llc *l = mtod(m, struct llc *);
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes 			printf("ether_output: sending LLC2 pkt to: ");
255df8bae1dSRodney W. Grimes 			for (i=0; i<6; i++)
256df8bae1dSRodney W. Grimes 				printf("%x ", edst[i] & 0xff);
257df8bae1dSRodney W. Grimes 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
258df8bae1dSRodney W. Grimes 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
259df8bae1dSRodney W. Grimes 			       l->llc_control & 0xff);
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 		}
262df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
263df8bae1dSRodney W. Grimes 		} break;
264df8bae1dSRodney W. Grimes #endif /* LLC */
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
267df8bae1dSRodney W. Grimes 		eh = (struct ether_header *)dst->sa_data;
26894a5d9b6SDavid Greenman  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
269df8bae1dSRodney W. Grimes 		type = eh->ether_type;
270df8bae1dSRodney W. Grimes 		break;
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes 	default:
273df8bae1dSRodney W. Grimes 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
274df8bae1dSRodney W. Grimes 			dst->sa_family);
275df8bae1dSRodney W. Grimes 		senderr(EAFNOSUPPORT);
276df8bae1dSRodney W. Grimes 	}
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes 	if (mcopy)
280df8bae1dSRodney W. Grimes 		(void) looutput(ifp, mcopy, dst, rt);
281df8bae1dSRodney W. Grimes 	/*
282df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
283df8bae1dSRodney W. Grimes 	 * allocate another.
284df8bae1dSRodney W. Grimes 	 */
285df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
286df8bae1dSRodney W. Grimes 	if (m == 0)
287df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
288df8bae1dSRodney W. Grimes 	eh = mtod(m, struct ether_header *);
289df8bae1dSRodney W. Grimes 	type = htons((u_short)type);
29094a5d9b6SDavid Greenman 	(void)memcpy(&eh->ether_type, &type,
291df8bae1dSRodney W. Grimes 		sizeof(eh->ether_type));
29294a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
29394a5d9b6SDavid Greenman  	(void)memcpy(eh->ether_shost, ac->ac_enaddr,
294df8bae1dSRodney W. Grimes 	    sizeof(eh->ether_shost));
295df8bae1dSRodney W. Grimes 	s = splimp();
296df8bae1dSRodney W. Grimes 	/*
297df8bae1dSRodney W. Grimes 	 * Queue message on interface, and start output if interface
298df8bae1dSRodney W. Grimes 	 * not yet active.
299df8bae1dSRodney W. Grimes 	 */
300df8bae1dSRodney W. Grimes 	if (IF_QFULL(&ifp->if_snd)) {
301df8bae1dSRodney W. Grimes 		IF_DROP(&ifp->if_snd);
302df8bae1dSRodney W. Grimes 		splx(s);
303df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
304df8bae1dSRodney W. Grimes 	}
305df8bae1dSRodney W. Grimes 	IF_ENQUEUE(&ifp->if_snd, m);
306df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
307df8bae1dSRodney W. Grimes 		(*ifp->if_start)(ifp);
308df8bae1dSRodney W. Grimes 	splx(s);
309df8bae1dSRodney W. Grimes 	ifp->if_obytes += len + sizeof (struct ether_header);
310df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST)
311df8bae1dSRodney W. Grimes 		ifp->if_omcasts++;
312df8bae1dSRodney W. Grimes 	return (error);
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes bad:
315df8bae1dSRodney W. Grimes 	if (m)
316df8bae1dSRodney W. Grimes 		m_freem(m);
317df8bae1dSRodney W. Grimes 	return (error);
318df8bae1dSRodney W. Grimes }
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes /*
321df8bae1dSRodney W. Grimes  * Process a received Ethernet packet;
322df8bae1dSRodney W. Grimes  * the packet is in the mbuf chain m without
323df8bae1dSRodney W. Grimes  * the ether header, which is provided separately.
324df8bae1dSRodney W. Grimes  */
325df8bae1dSRodney W. Grimes void
326df8bae1dSRodney W. Grimes ether_input(ifp, eh, m)
327df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
328df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
329df8bae1dSRodney W. Grimes 	struct mbuf *m;
330df8bae1dSRodney W. Grimes {
331df8bae1dSRodney W. Grimes 	register struct ifqueue *inq;
332df8bae1dSRodney W. Grimes 	register struct llc *l;
333307d80beSDavid Greenman 	u_short ether_type;
334df8bae1dSRodney W. Grimes 	int s;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_UP) == 0) {
337df8bae1dSRodney W. Grimes 		m_freem(m);
338df8bae1dSRodney W. Grimes 		return;
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes 	ifp->if_lastchange = time;
341df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
342df8bae1dSRodney W. Grimes 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
343df8bae1dSRodney W. Grimes 	    sizeof(etherbroadcastaddr)) == 0)
344df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
345df8bae1dSRodney W. Grimes 	else if (eh->ether_dhost[0] & 1)
346df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
347df8bae1dSRodney W. Grimes 	if (m->m_flags & (M_BCAST|M_MCAST))
348df8bae1dSRodney W. Grimes 		ifp->if_imcasts++;
349df8bae1dSRodney W. Grimes 
350307d80beSDavid Greenman 	ether_type = ntohs(eh->ether_type);
351307d80beSDavid Greenman 
352307d80beSDavid Greenman 	switch (ether_type) {
353df8bae1dSRodney W. Grimes #ifdef INET
354df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
355df8bae1dSRodney W. Grimes 		schednetisr(NETISR_IP);
356df8bae1dSRodney W. Grimes 		inq = &ipintrq;
357df8bae1dSRodney W. Grimes 		break;
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
360df8bae1dSRodney W. Grimes 		schednetisr(NETISR_ARP);
361df8bae1dSRodney W. Grimes 		inq = &arpintrq;
362df8bae1dSRodney W. Grimes 		break;
363df8bae1dSRodney W. Grimes #endif
364cc6a66f2SJulian Elischer #ifdef IPX
365cc6a66f2SJulian Elischer 	case ETHERTYPE_IPX:
366cc6a66f2SJulian Elischer 		schednetisr(NETISR_IPX);
367cc6a66f2SJulian Elischer 		inq = &ipxintrq;
368cc6a66f2SJulian Elischer 		break;
369cc6a66f2SJulian Elischer #endif
370df8bae1dSRodney W. Grimes #ifdef NS
371df8bae1dSRodney W. Grimes 	case ETHERTYPE_NS:
372df8bae1dSRodney W. Grimes 		schednetisr(NETISR_NS);
373df8bae1dSRodney W. Grimes 		inq = &nsintrq;
374df8bae1dSRodney W. Grimes 		break;
375df8bae1dSRodney W. Grimes #endif
376df8bae1dSRodney W. Grimes 	default:
377df8bae1dSRodney W. Grimes #if defined (ISO) || defined (LLC)
378307d80beSDavid Greenman 		if (ether_type > ETHERMTU)
379df8bae1dSRodney W. Grimes 			goto dropanyway;
380df8bae1dSRodney W. Grimes 		l = mtod(m, struct llc *);
381df8bae1dSRodney W. Grimes 		switch (l->llc_dsap) {
382df8bae1dSRodney W. Grimes #ifdef	ISO
383df8bae1dSRodney W. Grimes 		case LLC_ISO_LSAP:
384df8bae1dSRodney W. Grimes 			switch (l->llc_control) {
385df8bae1dSRodney W. Grimes 			case LLC_UI:
386df8bae1dSRodney W. Grimes 				/* LLC_UI_P forbidden in class 1 service */
387df8bae1dSRodney W. Grimes 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
388df8bae1dSRodney W. Grimes 				    (l->llc_ssap == LLC_ISO_LSAP)) {
389df8bae1dSRodney W. Grimes 					/* LSAP for ISO */
390307d80beSDavid Greenman 					if (m->m_pkthdr.len > ether_type)
391307d80beSDavid Greenman 						m_adj(m, ether_type - m->m_pkthdr.len);
392df8bae1dSRodney W. Grimes 					m->m_data += 3;		/* XXX */
393df8bae1dSRodney W. Grimes 					m->m_len -= 3;		/* XXX */
394df8bae1dSRodney W. Grimes 					m->m_pkthdr.len -= 3;	/* XXX */
395df8bae1dSRodney W. Grimes 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
396df8bae1dSRodney W. Grimes 					if (m == 0)
397df8bae1dSRodney W. Grimes 						return;
398df8bae1dSRodney W. Grimes 					*mtod(m, struct ether_header *) = *eh;
399df8bae1dSRodney W. Grimes 					IFDEBUG(D_ETHER)
400df8bae1dSRodney W. Grimes 						printf("clnp packet");
401df8bae1dSRodney W. Grimes 					ENDDEBUG
402df8bae1dSRodney W. Grimes 					schednetisr(NETISR_ISO);
403df8bae1dSRodney W. Grimes 					inq = &clnlintrq;
404df8bae1dSRodney W. Grimes 					break;
405df8bae1dSRodney W. Grimes 				}
406df8bae1dSRodney W. Grimes 				goto dropanyway;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 			case LLC_XID:
409df8bae1dSRodney W. Grimes 			case LLC_XID_P:
410df8bae1dSRodney W. Grimes 				if(m->m_len < 6)
411df8bae1dSRodney W. Grimes 					goto dropanyway;
412df8bae1dSRodney W. Grimes 				l->llc_window = 0;
413df8bae1dSRodney W. Grimes 				l->llc_fid = 9;
414df8bae1dSRodney W. Grimes 				l->llc_class = 1;
415df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap = 0;
416df8bae1dSRodney W. Grimes 				/* Fall through to */
417df8bae1dSRodney W. Grimes 			case LLC_TEST:
418df8bae1dSRodney W. Grimes 			case LLC_TEST_P:
419df8bae1dSRodney W. Grimes 			{
420df8bae1dSRodney W. Grimes 				struct sockaddr sa;
421df8bae1dSRodney W. Grimes 				register struct ether_header *eh2;
422df8bae1dSRodney W. Grimes 				int i;
423df8bae1dSRodney W. Grimes 				u_char c = l->llc_dsap;
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes 				l->llc_dsap = l->llc_ssap;
426df8bae1dSRodney W. Grimes 				l->llc_ssap = c;
427df8bae1dSRodney W. Grimes 				if (m->m_flags & (M_BCAST | M_MCAST))
428df8bae1dSRodney W. Grimes 					bcopy((caddr_t)ac->ac_enaddr,
429df8bae1dSRodney W. Grimes 					      (caddr_t)eh->ether_dhost, 6);
430df8bae1dSRodney W. Grimes 				sa.sa_family = AF_UNSPEC;
431df8bae1dSRodney W. Grimes 				sa.sa_len = sizeof(sa);
432df8bae1dSRodney W. Grimes 				eh2 = (struct ether_header *)sa.sa_data;
433df8bae1dSRodney W. Grimes 				for (i = 0; i < 6; i++) {
434df8bae1dSRodney W. Grimes 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
435df8bae1dSRodney W. Grimes 					eh2->ether_dhost[i] =
436df8bae1dSRodney W. Grimes 						eh->ether_dhost[i] = eh->ether_shost[i];
437df8bae1dSRodney W. Grimes 					eh->ether_shost[i] = c;
438df8bae1dSRodney W. Grimes 				}
439df8bae1dSRodney W. Grimes 				ifp->if_output(ifp, m, &sa, NULL);
440df8bae1dSRodney W. Grimes 				return;
441df8bae1dSRodney W. Grimes 			}
442df8bae1dSRodney W. Grimes 			default:
443df8bae1dSRodney W. Grimes 				m_freem(m);
444df8bae1dSRodney W. Grimes 				return;
445df8bae1dSRodney W. Grimes 			}
446df8bae1dSRodney W. Grimes 			break;
447df8bae1dSRodney W. Grimes #endif /* ISO */
448df8bae1dSRodney W. Grimes #ifdef LLC
449df8bae1dSRodney W. Grimes 		case LLC_X25_LSAP:
450df8bae1dSRodney W. Grimes 		{
451307d80beSDavid Greenman 			if (m->m_pkthdr.len > ether_type)
452307d80beSDavid Greenman 				m_adj(m, ether_type - m->m_pkthdr.len);
453df8bae1dSRodney W. Grimes 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
454df8bae1dSRodney W. Grimes 			if (m == 0)
455df8bae1dSRodney W. Grimes 				return;
456df8bae1dSRodney W. Grimes 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
457df8bae1dSRodney W. Grimes 					    eh->ether_dhost, LLC_X25_LSAP, 6,
458df8bae1dSRodney W. Grimes 					    mtod(m, struct sdl_hdr *)))
459df8bae1dSRodney W. Grimes 				panic("ETHER cons addr failure");
460307d80beSDavid Greenman 			mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
461df8bae1dSRodney W. Grimes #ifdef LLC_DEBUG
462df8bae1dSRodney W. Grimes 				printf("llc packet\n");
463df8bae1dSRodney W. Grimes #endif /* LLC_DEBUG */
464df8bae1dSRodney W. Grimes 			schednetisr(NETISR_CCITT);
465df8bae1dSRodney W. Grimes 			inq = &llcintrq;
466df8bae1dSRodney W. Grimes 			break;
467df8bae1dSRodney W. Grimes 		}
468df8bae1dSRodney W. Grimes #endif /* LLC */
469df8bae1dSRodney W. Grimes 		dropanyway:
470df8bae1dSRodney W. Grimes 		default:
471df8bae1dSRodney W. Grimes 			m_freem(m);
472df8bae1dSRodney W. Grimes 			return;
473df8bae1dSRodney W. Grimes 		}
474df8bae1dSRodney W. Grimes #else /* ISO || LLC */
475df8bae1dSRodney W. Grimes 	    m_freem(m);
476df8bae1dSRodney W. Grimes 	    return;
477df8bae1dSRodney W. Grimes #endif /* ISO || LLC */
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 	s = splimp();
481df8bae1dSRodney W. Grimes 	if (IF_QFULL(inq)) {
482df8bae1dSRodney W. Grimes 		IF_DROP(inq);
483df8bae1dSRodney W. Grimes 		m_freem(m);
484df8bae1dSRodney W. Grimes 	} else
485df8bae1dSRodney W. Grimes 		IF_ENQUEUE(inq, m);
486df8bae1dSRodney W. Grimes 	splx(s);
487df8bae1dSRodney W. Grimes }
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes /*
490df8bae1dSRodney W. Grimes  * Convert Ethernet address to printable (loggable) representation.
491df8bae1dSRodney W. Grimes  */
492df8bae1dSRodney W. Grimes static char digits[] = "0123456789abcdef";
493df8bae1dSRodney W. Grimes char *
494df8bae1dSRodney W. Grimes ether_sprintf(ap)
495df8bae1dSRodney W. Grimes 	register u_char *ap;
496df8bae1dSRodney W. Grimes {
497df8bae1dSRodney W. Grimes 	register i;
498df8bae1dSRodney W. Grimes 	static char etherbuf[18];
499df8bae1dSRodney W. Grimes 	register char *cp = etherbuf;
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	for (i = 0; i < 6; i++) {
502df8bae1dSRodney W. Grimes 		*cp++ = digits[*ap >> 4];
503df8bae1dSRodney W. Grimes 		*cp++ = digits[*ap++ & 0xf];
504df8bae1dSRodney W. Grimes 		*cp++ = ':';
505df8bae1dSRodney W. Grimes 	}
506df8bae1dSRodney W. Grimes 	*--cp = 0;
507df8bae1dSRodney W. Grimes 	return (etherbuf);
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
512df8bae1dSRodney W. Grimes  */
513df8bae1dSRodney W. Grimes void
514df8bae1dSRodney W. Grimes ether_ifattach(ifp)
515df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
516df8bae1dSRodney W. Grimes {
517df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
518df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 	ifp->if_type = IFT_ETHER;
521df8bae1dSRodney W. Grimes 	ifp->if_addrlen = 6;
522df8bae1dSRodney W. Grimes 	ifp->if_hdrlen = 14;
523df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
524df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
525df8bae1dSRodney W. Grimes 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
526df8bae1dSRodney W. Grimes 		    sdl->sdl_family == AF_LINK) {
527df8bae1dSRodney W. Grimes 			sdl->sdl_type = IFT_ETHER;
528df8bae1dSRodney W. Grimes 			sdl->sdl_alen = ifp->if_addrlen;
529df8bae1dSRodney W. Grimes 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
530df8bae1dSRodney W. Grimes 			      LLADDR(sdl), ifp->if_addrlen);
531df8bae1dSRodney W. Grimes 			break;
532df8bae1dSRodney W. Grimes 		}
533df8bae1dSRodney W. Grimes }
534df8bae1dSRodney W. Grimes 
5353bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_min[6] =
5363bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
5373bda9f9bSPoul-Henning Kamp static u_char ether_ipmulticast_max[6] =
5383bda9f9bSPoul-Henning Kamp 	{ 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
539df8bae1dSRodney W. Grimes /*
540df8bae1dSRodney W. Grimes  * Add an Ethernet multicast address or range of addresses to the list for a
541df8bae1dSRodney W. Grimes  * given interface.
542df8bae1dSRodney W. Grimes  */
543df8bae1dSRodney W. Grimes int
544df8bae1dSRodney W. Grimes ether_addmulti(ifr, ac)
545df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
546df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
547df8bae1dSRodney W. Grimes {
548df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
549df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
550df8bae1dSRodney W. Grimes 	u_char addrlo[6];
551df8bae1dSRodney W. Grimes 	u_char addrhi[6];
552d3628763SRodney W. Grimes       int set_allmulti = 0;
553df8bae1dSRodney W. Grimes 	int s = splimp();
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
558df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
559df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
560df8bae1dSRodney W. Grimes 		break;
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes #ifdef INET
563df8bae1dSRodney W. Grimes 	case AF_INET:
564df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
565df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
566df8bae1dSRodney W. Grimes 			/*
567df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means listen to all
568df8bae1dSRodney W. Grimes 			 * of the Ethernet multicast addresses used for IP.
569df8bae1dSRodney W. Grimes 			 * (This is for the sake of IP multicast routers.)
570df8bae1dSRodney W. Grimes 			 */
571df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
572df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
573d3628763SRodney W. Grimes                       set_allmulti = 1;
574df8bae1dSRodney W. Grimes 		}
575df8bae1dSRodney W. Grimes 		else {
576df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
577df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
578df8bae1dSRodney W. Grimes 		}
579df8bae1dSRodney W. Grimes 		break;
580df8bae1dSRodney W. Grimes #endif
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	default:
583df8bae1dSRodney W. Grimes 		splx(s);
584df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
585df8bae1dSRodney W. Grimes 	}
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes 	/*
588df8bae1dSRodney W. Grimes 	 * Verify that we have valid Ethernet multicast addresses.
589df8bae1dSRodney W. Grimes 	 */
590df8bae1dSRodney W. Grimes 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
591df8bae1dSRodney W. Grimes 		splx(s);
592df8bae1dSRodney W. Grimes 		return (EINVAL);
593df8bae1dSRodney W. Grimes 	}
594df8bae1dSRodney W. Grimes 	/*
595df8bae1dSRodney W. Grimes 	 * See if the address range is already in the list.
596df8bae1dSRodney W. Grimes 	 */
597df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
598df8bae1dSRodney W. Grimes 	if (enm != NULL) {
599df8bae1dSRodney W. Grimes 		/*
600df8bae1dSRodney W. Grimes 		 * Found it; just increment the reference count.
601df8bae1dSRodney W. Grimes 		 */
602df8bae1dSRodney W. Grimes 		++enm->enm_refcount;
603df8bae1dSRodney W. Grimes 		splx(s);
604df8bae1dSRodney W. Grimes 		return (0);
605df8bae1dSRodney W. Grimes 	}
606df8bae1dSRodney W. Grimes 	/*
607df8bae1dSRodney W. Grimes 	 * New address or range; malloc a new multicast record
608df8bae1dSRodney W. Grimes 	 * and link it into the interface's multicast list.
609df8bae1dSRodney W. Grimes 	 */
610df8bae1dSRodney W. Grimes 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
611df8bae1dSRodney W. Grimes 	if (enm == NULL) {
612df8bae1dSRodney W. Grimes 		splx(s);
613df8bae1dSRodney W. Grimes 		return (ENOBUFS);
614df8bae1dSRodney W. Grimes 	}
615df8bae1dSRodney W. Grimes 	bcopy(addrlo, enm->enm_addrlo, 6);
616df8bae1dSRodney W. Grimes 	bcopy(addrhi, enm->enm_addrhi, 6);
617df8bae1dSRodney W. Grimes 	enm->enm_ac = ac;
618df8bae1dSRodney W. Grimes 	enm->enm_refcount = 1;
619df8bae1dSRodney W. Grimes 	enm->enm_next = ac->ac_multiaddrs;
620df8bae1dSRodney W. Grimes 	ac->ac_multiaddrs = enm;
621df8bae1dSRodney W. Grimes 	ac->ac_multicnt++;
622df8bae1dSRodney W. Grimes 	splx(s);
623d3628763SRodney W. Grimes       if (set_allmulti)
624d3628763SRodney W. Grimes               ac->ac_if.if_flags |= IFF_ALLMULTI;
625d3628763SRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	/*
627df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
628df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
629df8bae1dSRodney W. Grimes 	 */
630df8bae1dSRodney W. Grimes 	return (ENETRESET);
631df8bae1dSRodney W. Grimes }
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes /*
634df8bae1dSRodney W. Grimes  * Delete a multicast address record.
635df8bae1dSRodney W. Grimes  */
636df8bae1dSRodney W. Grimes int
637df8bae1dSRodney W. Grimes ether_delmulti(ifr, ac)
638df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
639df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
640df8bae1dSRodney W. Grimes {
641df8bae1dSRodney W. Grimes 	register struct ether_multi *enm;
642df8bae1dSRodney W. Grimes 	register struct ether_multi **p;
643df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
644df8bae1dSRodney W. Grimes 	u_char addrlo[6];
645df8bae1dSRodney W. Grimes 	u_char addrhi[6];
646d3628763SRodney W. Grimes       int unset_allmulti = 0;
647df8bae1dSRodney W. Grimes 	int s = splimp();
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 	switch (ifr->ifr_addr.sa_family) {
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes 	case AF_UNSPEC:
652df8bae1dSRodney W. Grimes 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
653df8bae1dSRodney W. Grimes 		bcopy(addrlo, addrhi, 6);
654df8bae1dSRodney W. Grimes 		break;
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes #ifdef INET
657df8bae1dSRodney W. Grimes 	case AF_INET:
658df8bae1dSRodney W. Grimes 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
659df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY) {
660df8bae1dSRodney W. Grimes 			/*
661df8bae1dSRodney W. Grimes 			 * An IP address of INADDR_ANY means stop listening
662df8bae1dSRodney W. Grimes 			 * to the range of Ethernet multicast addresses used
663df8bae1dSRodney W. Grimes 			 * for IP.
664df8bae1dSRodney W. Grimes 			 */
665df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_min, addrlo, 6);
666df8bae1dSRodney W. Grimes 			bcopy(ether_ipmulticast_max, addrhi, 6);
667d3628763SRodney W. Grimes                       unset_allmulti = 1;
668df8bae1dSRodney W. Grimes 		}
669df8bae1dSRodney W. Grimes 		else {
670df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
671df8bae1dSRodney W. Grimes 			bcopy(addrlo, addrhi, 6);
672df8bae1dSRodney W. Grimes 		}
673df8bae1dSRodney W. Grimes 		break;
674df8bae1dSRodney W. Grimes #endif
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	default:
677df8bae1dSRodney W. Grimes 		splx(s);
678df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
679df8bae1dSRodney W. Grimes 	}
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes 	/*
682df8bae1dSRodney W. Grimes 	 * Look up the address in our list.
683df8bae1dSRodney W. Grimes 	 */
684df8bae1dSRodney W. Grimes 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
685df8bae1dSRodney W. Grimes 	if (enm == NULL) {
686df8bae1dSRodney W. Grimes 		splx(s);
687df8bae1dSRodney W. Grimes 		return (ENXIO);
688df8bae1dSRodney W. Grimes 	}
689df8bae1dSRodney W. Grimes 	if (--enm->enm_refcount != 0) {
690df8bae1dSRodney W. Grimes 		/*
691df8bae1dSRodney W. Grimes 		 * Still some claims to this record.
692df8bae1dSRodney W. Grimes 		 */
693df8bae1dSRodney W. Grimes 		splx(s);
694df8bae1dSRodney W. Grimes 		return (0);
695df8bae1dSRodney W. Grimes 	}
696df8bae1dSRodney W. Grimes 	/*
697df8bae1dSRodney W. Grimes 	 * No remaining claims to this record; unlink and free it.
698df8bae1dSRodney W. Grimes 	 */
699df8bae1dSRodney W. Grimes 	for (p = &enm->enm_ac->ac_multiaddrs;
700df8bae1dSRodney W. Grimes 	     *p != enm;
701df8bae1dSRodney W. Grimes 	     p = &(*p)->enm_next)
702df8bae1dSRodney W. Grimes 		continue;
703df8bae1dSRodney W. Grimes 	*p = (*p)->enm_next;
704df8bae1dSRodney W. Grimes 	free(enm, M_IFMADDR);
705df8bae1dSRodney W. Grimes 	ac->ac_multicnt--;
706df8bae1dSRodney W. Grimes 	splx(s);
707d3628763SRodney W. Grimes       if (unset_allmulti)
708d3628763SRodney W. Grimes               ac->ac_if.if_flags &= ~IFF_ALLMULTI;
709d3628763SRodney W. Grimes 
710df8bae1dSRodney W. Grimes 	/*
711df8bae1dSRodney W. Grimes 	 * Return ENETRESET to inform the driver that the list has changed
712df8bae1dSRodney W. Grimes 	 * and its reception filter should be adjusted accordingly.
713df8bae1dSRodney W. Grimes 	 */
714df8bae1dSRodney W. Grimes 	return (ENETRESET);
715df8bae1dSRodney W. Grimes }
716602d513cSGarrett Wollman 
717602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
718