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