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