xref: /netbsd-src/sys/net/if_ethersubr.c (revision 7cc2f76925f078d01ddc9e640a98f4ccfc9f8c3b)
1 /*	$NetBSD: if_ethersubr.c,v 1.72 2000/12/13 22:07:50 thorpej Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
65  */
66 
67 #include "opt_inet.h"
68 #include "opt_atalk.h"
69 #include "opt_ccitt.h"
70 #include "opt_llc.h"
71 #include "opt_iso.h"
72 #include "opt_ns.h"
73 #include "opt_gateway.h"
74 #include "vlan.h"
75 #include "bpfilter.h"
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/ioctl.h>
85 #include <sys/errno.h>
86 #include <sys/syslog.h>
87 
88 #include <machine/cpu.h>
89 
90 #include <net/if.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
93 #include <net/if_llc.h>
94 #include <net/if_dl.h>
95 #include <net/if_types.h>
96 
97 #if NBPFILTER > 0
98 #include <net/bpf.h>
99 #endif
100 
101 #include <net/if_ether.h>
102 #if NVLAN > 0
103 #include <net/if_vlanvar.h>
104 #endif
105 
106 #include <netinet/in.h>
107 #ifdef INET
108 #include <netinet/in_var.h>
109 #endif
110 #include <netinet/if_inarp.h>
111 
112 #ifdef INET6
113 #ifndef INET
114 #include <netinet/in.h>
115 #endif
116 #include <netinet6/in6_var.h>
117 #include <netinet6/nd6.h>
118 #endif
119 
120 #ifdef NS
121 #include <netns/ns.h>
122 #include <netns/ns_if.h>
123 #endif
124 
125 #ifdef IPX
126 #include <netipx/ipx.h>
127 #include <netipx/ipx_if.h>
128 #endif
129 
130 #ifdef ISO
131 #include <netiso/argo_debug.h>
132 #include <netiso/iso.h>
133 #include <netiso/iso_var.h>
134 #include <netiso/iso_snpac.h>
135 #endif
136 
137 #ifdef LLC
138 #include <netccitt/dll.h>
139 #include <netccitt/llc_var.h>
140 #endif
141 
142 #if defined(LLC) && defined(CCITT)
143 extern struct ifqueue pkintrq;
144 #endif
145 
146 #ifdef NETATALK
147 #include <netatalk/at.h>
148 #include <netatalk/at_var.h>
149 #include <netatalk/at_extern.h>
150 
151 #define llc_snap_org_code llc_un.type_snap.org_code
152 #define llc_snap_ether_type llc_un.type_snap.ether_type
153 
154 extern u_char	at_org_code[3];
155 extern u_char	aarp_org_code[3];
156 #endif /* NETATALK */
157 
158 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
159 #define senderr(e) { error = (e); goto bad;}
160 
161 #define SIN(x) ((struct sockaddr_in *)x)
162 
163 static	int ether_output __P((struct ifnet *, struct mbuf *,
164 	    struct sockaddr *, struct rtentry *));
165 static	void ether_input __P((struct ifnet *, struct mbuf *));
166 
167 /*
168  * Ethernet output routine.
169  * Encapsulate a packet of type family for the local net.
170  * Assumes that ifp is actually pointer to ethercom structure.
171  */
172 static int
173 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
174 	struct rtentry *rt0)
175 {
176 	u_int16_t etype = 0;
177 	int s, len, error = 0, hdrcmplt = 0;
178  	u_char esrc[6], edst[6];
179 	struct mbuf *m = m0;
180 	struct rtentry *rt;
181 	struct mbuf *mcopy = (struct mbuf *)0;
182 	struct ether_header *eh;
183 	ALTQ_DECL(struct altq_pktattr pktattr;)
184 #ifdef INET
185 	struct arphdr *ah;
186 #endif /* INET */
187 #ifdef NETATALK
188 	struct at_ifaddr *aa;
189 #endif /* NETATALK */
190 	short mflags;
191 
192 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
193 		senderr(ENETDOWN);
194 	ifp->if_lastchange = time;
195 	if ((rt = rt0) != NULL) {
196 		if ((rt->rt_flags & RTF_UP) == 0) {
197 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
198 				rt->rt_refcnt--;
199 				if (rt->rt_ifp != ifp)
200 					return (*rt->rt_ifp->if_output)
201 							(ifp, m0, dst, rt);
202 			} else
203 				senderr(EHOSTUNREACH);
204 		}
205 		if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
206 			if (rt->rt_gwroute == 0)
207 				goto lookup;
208 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
209 				rtfree(rt); rt = rt0;
210 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
211 				if ((rt = rt->rt_gwroute) == 0)
212 					senderr(EHOSTUNREACH);
213 				/* the "G" test below also prevents rt == rt0 */
214 				if ((rt->rt_flags & RTF_GATEWAY) ||
215 				    (rt->rt_ifp != ifp)) {
216 					rt->rt_refcnt--;
217 					rt0->rt_gwroute = 0;
218 					senderr(EHOSTUNREACH);
219 				}
220 			}
221 		}
222 		if (rt->rt_flags & RTF_REJECT)
223 			if (rt->rt_rmx.rmx_expire == 0 ||
224 			    time.tv_sec < rt->rt_rmx.rmx_expire)
225 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
226 	}
227 
228 	/*
229 	 * If the queueing discipline needs packet classification,
230 	 * do it before prepending link headers.
231 	 */
232 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
233 
234 	switch (dst->sa_family) {
235 
236 #ifdef INET
237 	case AF_INET:
238 		if (m->m_flags & M_BCAST)
239                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
240 				sizeof(edst));
241 
242 		else if (m->m_flags & M_MCAST) {
243 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
244 			    (caddr_t)edst)
245 
246 		} else if (!arpresolve(ifp, rt, m, dst, edst))
247 			return (0);	/* if not yet resolved */
248 		/* If broadcasting on a simplex interface, loopback a copy */
249 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
250 			mcopy = m_copy(m, 0, (int)M_COPYALL);
251 		etype = htons(ETHERTYPE_IP);
252 		break;
253 
254 	case AF_ARP:
255 		ah = mtod(m, struct arphdr *);
256 		if (m->m_flags & M_BCAST)
257                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
258 				sizeof(edst));
259 		else
260 			bcopy((caddr_t)ar_tha(ah),
261 				(caddr_t)edst, sizeof(edst));
262 
263 		ah->ar_hrd = htons(ARPHRD_ETHER);
264 
265 		switch(ntohs(ah->ar_op)) {
266 		case ARPOP_REVREQUEST:
267 		case ARPOP_REVREPLY:
268 			etype = htons(ETHERTYPE_REVARP);
269 			break;
270 
271 		case ARPOP_REQUEST:
272 		case ARPOP_REPLY:
273 		default:
274 			etype = htons(ETHERTYPE_ARP);
275 		}
276 
277 		break;
278 #endif
279 #ifdef INET6
280 	case AF_INET6:
281 #ifdef OLDIP6OUTPUT
282 		if (!nd6_resolve(ifp, rt, m, dst, (u_char *)edst))
283 			return(0);	/* if not yet resolves */
284 #else
285 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){
286 			/* something bad happened */
287 			return(0);
288 		}
289 #endif /* OLDIP6OUTPUT */
290 		etype = htons(ETHERTYPE_IPV6);
291 		break;
292 #endif
293 #ifdef NETATALK
294     case AF_APPLETALK:
295 		if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
296 #ifdef NETATALKDEBUG
297 			printf("aarpresolv failed\n");
298 #endif /* NETATALKDEBUG */
299 			return (0);
300 		}
301 		/*
302 		 * ifaddr is the first thing in at_ifaddr
303 		 */
304 		aa = (struct at_ifaddr *) at_ifawithnet(
305 		    (struct sockaddr_at *)dst, ifp);
306 		if (aa == NULL)
307 		    goto bad;
308 
309 		/*
310 		 * In the phase 2 case, we need to prepend an mbuf for the
311 		 * llc header.  Since we must preserve the value of m,
312 		 * which is passed to us by value, we m_copy() the first
313 		 * mbuf, and use it for our llc header.
314 		 */
315 		if (aa->aa_flags & AFA_PHASE2) {
316 			struct llc llc;
317 
318 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
319 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
320 			llc.llc_control = LLC_UI;
321 			bcopy(at_org_code, llc.llc_snap_org_code,
322 			    sizeof(llc.llc_snap_org_code));
323 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
324 			bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
325 		} else {
326 			etype = htons(ETHERTYPE_ATALK);
327 		}
328 		break;
329 #endif /* NETATALK */
330 #ifdef NS
331 	case AF_NS:
332 		etype = htons(ETHERTYPE_NS);
333  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
334 		    (caddr_t)edst, sizeof (edst));
335 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
336 			return (looutput(ifp, m, dst, rt));
337 		/* If broadcasting on a simplex interface, loopback a copy */
338 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
339 			mcopy = m_copy(m, 0, (int)M_COPYALL);
340 		break;
341 #endif
342 #ifdef IPX
343 	case AF_IPX:
344 		etype = htons(ETHERTYPE_IPX);
345  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
346 		    (caddr_t)edst, sizeof (edst));
347 		/* If broadcasting on a simplex interface, loopback a copy */
348 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
349 			mcopy = m_copy(m, 0, (int)M_COPYALL);
350 		break;
351 #endif
352 #ifdef	ISO
353 	case AF_ISO: {
354 		int	snpalen;
355 		struct	llc *l;
356 		struct sockaddr_dl *sdl;
357 
358 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
359 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
360 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
361 		} else {
362 			error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
363 						(char *)edst, &snpalen);
364 			if (error)
365 				goto bad; /* Not Resolved */
366 		}
367 		/* If broadcasting on a simplex interface, loopback a copy */
368 		if (*edst & 1)
369 			m->m_flags |= (M_BCAST|M_MCAST);
370 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
371 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
372 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
373 			if (mcopy) {
374 				eh = mtod(mcopy, struct ether_header *);
375 				bcopy((caddr_t)edst,
376 				      (caddr_t)eh->ether_dhost, sizeof (edst));
377 				bcopy(LLADDR(ifp->if_sadl),
378 				      (caddr_t)eh->ether_shost, sizeof (edst));
379 			}
380 		}
381 		M_PREPEND(m, 3, M_DONTWAIT);
382 		if (m == NULL)
383 			return (0);
384 		l = mtod(m, struct llc *);
385 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
386 		l->llc_control = LLC_UI;
387 #ifdef ARGO_DEBUG
388 		if (argo_debug[D_ETHER]) {
389 			int i;
390 			printf("unoutput: sending pkt to: ");
391 			for (i=0; i<6; i++)
392 				printf("%x ", edst[i] & 0xff);
393 			printf("\n");
394 		}
395 #endif
396 		} break;
397 #endif /* ISO */
398 #ifdef	LLC
399 /*	case AF_NSAP: */
400 	case AF_CCITT: {
401 		struct sockaddr_dl *sdl =
402 			(struct sockaddr_dl *) rt -> rt_gateway;
403 
404 		if (sdl && sdl->sdl_family == AF_LINK
405 		    && sdl->sdl_alen > 0) {
406 			bcopy(LLADDR(sdl), (char *)edst,
407 				sizeof(edst));
408 		} else goto bad; /* Not a link interface ? Funny ... */
409 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
410 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
411 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
412 			if (mcopy) {
413 				eh = mtod(mcopy, struct ether_header *);
414 				bcopy((caddr_t)edst,
415 				      (caddr_t)eh->ether_dhost, sizeof (edst));
416 				bcopy(LLADDR(ifp->if_sadl),
417 				      (caddr_t)eh->ether_shost, sizeof (edst));
418 			}
419 		}
420 #ifdef LLC_DEBUG
421 		{
422 			int i;
423 			struct llc *l = mtod(m, struct llc *);
424 
425 			printf("ether_output: sending LLC2 pkt to: ");
426 			for (i=0; i<6; i++)
427 				printf("%x ", edst[i] & 0xff);
428 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
429 			    m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
430 			    l->llc_control & 0xff);
431 
432 		}
433 #endif /* LLC_DEBUG */
434 		} break;
435 #endif /* LLC */
436 
437 	case pseudo_AF_HDRCMPLT:
438 		hdrcmplt = 1;
439 		eh = (struct ether_header *)dst->sa_data;
440 		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc));
441 		/* FALLTHROUGH */
442 
443 	case AF_UNSPEC:
444 		eh = (struct ether_header *)dst->sa_data;
445  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
446 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
447 		etype = eh->ether_type;
448 		break;
449 
450 	default:
451 		printf("%s: can't handle af%d\n", ifp->if_xname,
452 			dst->sa_family);
453 		senderr(EAFNOSUPPORT);
454 	}
455 
456 	if (mcopy)
457 		(void) looutput(ifp, mcopy, dst, rt);
458 
459 	/* If no ether type is set, this must be a 802.2 formatted packet.
460 	 */
461 	if (etype == 0)
462 		etype = htons(m->m_pkthdr.len);
463 	/*
464 	 * Add local net header.  If no space in first mbuf,
465 	 * allocate another.
466 	 */
467 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
468 	if (m == 0)
469 		senderr(ENOBUFS);
470 	eh = mtod(m, struct ether_header *);
471 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
472 		sizeof(eh->ether_type));
473  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
474 	if (hdrcmplt)
475 		bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
476 		    sizeof(eh->ether_shost));
477 	else
478 	 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost,
479 		    sizeof(eh->ether_shost));
480 	mflags = m->m_flags;
481 	len = m->m_pkthdr.len;
482 	s = splimp();
483 	/*
484 	 * Queue message on interface, and start output if interface
485 	 * not yet active.
486 	 */
487 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
488 	if (error) {
489 		/* mbuf is already freed */
490 		splx(s);
491 		return (error);
492 	}
493 	ifp->if_obytes += len;
494 	if (mflags & M_MCAST)
495 		ifp->if_omcasts++;
496 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
497 		(*ifp->if_start)(ifp);
498 	splx(s);
499 	return (error);
500 
501 bad:
502 	if (m)
503 		m_freem(m);
504 	return (error);
505 }
506 
507 /*
508  * Process a received Ethernet packet;
509  * the packet is in the mbuf chain m with
510  * the ether header.
511  */
512 static void
513 ether_input(struct ifnet *ifp, struct mbuf *m)
514 {
515 	struct ifqueue *inq;
516 	u_int16_t etype;
517 	int s;
518 	struct ether_header *eh;
519 	struct mbuf *n;
520 #if defined (ISO) || defined (LLC) || defined(NETATALK)
521 	struct llc *l;
522 #endif
523 
524 	if ((ifp->if_flags & IFF_UP) == 0) {
525 		m_freem(m);
526 		return;
527 	}
528 
529 	eh = mtod(m, struct ether_header *);
530 	etype = ntohs(eh->ether_type);
531 
532 	/*
533 	 * Determine if the packet is within its size limits.
534 	 */
535 	if (m->m_pkthdr.len > ETHER_MAX_FRAME(etype, m->m_flags & M_HASFCS)) {
536 		printf("%s: discarding oversize frame (len=%d)\n",
537 		    ifp->if_xname, m->m_pkthdr.len);
538 		m_freem(m);
539 		return;
540 	}
541 
542 	ifp->if_lastchange = time;
543 	ifp->if_ibytes += m->m_pkthdr.len;
544 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
545 		if (memcmp(etherbroadcastaddr,
546 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
547 			m->m_flags |= M_BCAST;
548 		else
549 			m->m_flags |= M_MCAST;
550 		ifp->if_imcasts++;
551 	} else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
552 		   memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
553 			  ETHER_ADDR_LEN) != 0) {
554 		m_freem(m);
555 		return;
556 	}
557 
558 	/* Check if the mbuf has a VLAN tag */
559 	n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
560 	if (n) {
561 #if NVLAN > 0
562 		/*
563 		 * vlan_input() will either recursively call ether_input()
564 		 * or drop the packet.
565 		 */
566 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
567 			vlan_input(ifp, m);
568 		else
569 #endif
570 			m_freem(m);
571 		return;
572 	}
573 
574 	/*
575 	 * Handle protocols that expect to have the Ethernet header
576 	 * (and possibly FCS) intact.
577 	 */
578 	switch (etype) {
579 #if NVLAN > 0
580 	case ETHERTYPE_VLAN:
581 		/*
582 		 * vlan_input() will either recursively call ether_input()
583 		 * or drop the packet.
584 		 */
585 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
586 			vlan_input(ifp, m);
587 		else
588 			m_freem(m);
589 		return;
590 #endif /* NVLAN > 0 */
591 	default:
592 		/* Nothing. */
593 	}
594 
595 	/* Strip off the Ethernet header. */
596 	m_adj(m, sizeof(struct ether_header));
597 
598 	/* If the CRC is still on the packet, trim it off. */
599 	if (m->m_flags & M_HASFCS)
600 		m_adj(m, -ETHER_CRC_LEN);
601 
602 	switch (etype) {
603 #ifdef INET
604 	case ETHERTYPE_IP:
605 #ifdef GATEWAY
606 		if (ipflow_fastforward(m))
607 			return;
608 #endif
609 		schednetisr(NETISR_IP);
610 		inq = &ipintrq;
611 		break;
612 
613 	case ETHERTYPE_ARP:
614 		schednetisr(NETISR_ARP);
615 		inq = &arpintrq;
616 		break;
617 
618 	case ETHERTYPE_REVARP:
619 		revarpinput(m);	/* XXX queue? */
620 		return;
621 #endif
622 #ifdef INET6
623 	case ETHERTYPE_IPV6:
624 		schednetisr(NETISR_IPV6);
625 		inq = &ip6intrq;
626 		break;
627 #endif
628 #ifdef NS
629 	case ETHERTYPE_NS:
630 		schednetisr(NETISR_NS);
631 		inq = &nsintrq;
632 		break;
633 
634 #endif
635 #ifdef IPX
636 	case ETHERTYPE_IPX:
637 		schednetisr(NETISR_IPX);
638 		inq = &ipxintrq;
639 		break;
640 #endif
641 #ifdef NETATALK
642         case ETHERTYPE_ATALK:
643                 schednetisr(NETISR_ATALK);
644                 inq = &atintrq1;
645                 break;
646         case ETHERTYPE_AARP:
647 		/* probably this should be done with a NETISR as well */
648                 aarpinput(ifp, m); /* XXX */
649                 return;
650 #endif /* NETATALK */
651 	default:
652 #if defined (ISO) || defined (LLC) || defined (NETATALK)
653 		if (etype > ETHERMTU)
654 			goto dropanyway;
655 		l = mtod(m, struct llc *);
656 		switch (l->llc_dsap) {
657 #ifdef NETATALK
658 		case LLC_SNAP_LSAP:
659 			switch (l->llc_control) {
660 			case LLC_UI:
661 				if (l->llc_ssap != LLC_SNAP_LSAP) {
662 					goto dropanyway;
663 				}
664 
665 				if (Bcmp(&(l->llc_snap_org_code)[0],
666 				    at_org_code, sizeof(at_org_code)) == 0 &&
667 				    ntohs(l->llc_snap_ether_type) ==
668 				    ETHERTYPE_ATALK) {
669 					inq = &atintrq2;
670 					m_adj(m, sizeof(struct llc));
671 					schednetisr(NETISR_ATALK);
672 					break;
673 				}
674 
675 				if (Bcmp(&(l->llc_snap_org_code)[0],
676 				    aarp_org_code,
677 				    sizeof(aarp_org_code)) == 0 &&
678 				    ntohs(l->llc_snap_ether_type) ==
679 				    ETHERTYPE_AARP) {
680 					m_adj( m, sizeof(struct llc));
681 					aarpinput(ifp, m); /* XXX */
682 				    return;
683 				}
684 
685 			default:
686 				goto dropanyway;
687 			}
688 			break;
689 #endif /* NETATALK */
690 #ifdef	ISO
691 		case LLC_ISO_LSAP:
692 			switch (l->llc_control) {
693 			case LLC_UI:
694 				/* LLC_UI_P forbidden in class 1 service */
695 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
696 				    (l->llc_ssap == LLC_ISO_LSAP)) {
697 					/* LSAP for ISO */
698 					if (m->m_pkthdr.len > etype)
699 						m_adj(m, etype - m->m_pkthdr.len);
700 					m->m_data += 3;		/* XXX */
701 					m->m_len -= 3;		/* XXX */
702 					m->m_pkthdr.len -= 3;	/* XXX */
703 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
704 					if (m == 0)
705 						return;
706 					*mtod(m, struct ether_header *) = *eh;
707 #ifdef ARGO_DEBUG
708 					if (argo_debug[D_ETHER])
709 						printf("clnp packet");
710 #endif
711 					schednetisr(NETISR_ISO);
712 					inq = &clnlintrq;
713 					break;
714 				}
715 				goto dropanyway;
716 
717 			case LLC_XID:
718 			case LLC_XID_P:
719 				if(m->m_len < 6)
720 					goto dropanyway;
721 				l->llc_window = 0;
722 				l->llc_fid = 9;
723 				l->llc_class = 1;
724 				l->llc_dsap = l->llc_ssap = 0;
725 				/* Fall through to */
726 			case LLC_TEST:
727 			case LLC_TEST_P:
728 			{
729 				struct sockaddr sa;
730 				struct ether_header *eh2;
731 				int i;
732 				u_char c = l->llc_dsap;
733 
734 				l->llc_dsap = l->llc_ssap;
735 				l->llc_ssap = c;
736 				if (m->m_flags & (M_BCAST | M_MCAST))
737 					bcopy(LLADDR(ifp->if_sadl),
738 					      (caddr_t)eh->ether_dhost, 6);
739 				sa.sa_family = AF_UNSPEC;
740 				sa.sa_len = sizeof(sa);
741 				eh2 = (struct ether_header *)sa.sa_data;
742 				for (i = 0; i < 6; i++) {
743 					eh2->ether_shost[i] = c =
744 					    eh->ether_dhost[i];
745 					eh2->ether_dhost[i] =
746 					    eh->ether_dhost[i] =
747 					    eh->ether_shost[i];
748 					eh->ether_shost[i] = c;
749 				}
750 				ifp->if_output(ifp, m, &sa, NULL);
751 				return;
752 			}
753 			default:
754 				m_freem(m);
755 				return;
756 			}
757 			break;
758 #endif /* ISO */
759 #ifdef LLC
760 		case LLC_X25_LSAP:
761 		{
762 			if (m->m_pkthdr.len > etype)
763 				m_adj(m, etype - m->m_pkthdr.len);
764 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
765 			if (m == 0)
766 				return;
767 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
768 					    eh->ether_dhost, LLC_X25_LSAP, 6,
769 					    mtod(m, struct sdl_hdr *)))
770 				panic("ETHER cons addr failure");
771 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
772 #ifdef LLC_DEBUG
773 				printf("llc packet\n");
774 #endif /* LLC_DEBUG */
775 			schednetisr(NETISR_CCITT);
776 			inq = &llcintrq;
777 			break;
778 		}
779 #endif /* LLC */
780 		dropanyway:
781 		default:
782 			m_freem(m);
783 			return;
784 		}
785 #else /* ISO || LLC  || NETATALK*/
786 	    m_freem(m);
787 	    return;
788 #endif /* ISO || LLC || NETATALK*/
789 	}
790 
791 	s = splimp();
792 	if (IF_QFULL(inq)) {
793 		IF_DROP(inq);
794 		m_freem(m);
795 	} else
796 		IF_ENQUEUE(inq, m);
797 	splx(s);
798 }
799 
800 /*
801  * Convert Ethernet address to printable (loggable) representation.
802  */
803 static char digits[] = "0123456789abcdef";
804 char *
805 ether_sprintf(const u_char *ap)
806 {
807 	static char etherbuf[18];
808 	char *cp = etherbuf;
809 	int i;
810 
811 	for (i = 0; i < 6; i++) {
812 		*cp++ = digits[*ap >> 4];
813 		*cp++ = digits[*ap++ & 0xf];
814 		*cp++ = ':';
815 	}
816 	*--cp = 0;
817 	return (etherbuf);
818 }
819 
820 /*
821  * Perform common duties while attaching to interface list
822  */
823 void
824 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
825 {
826 	struct sockaddr_dl *sdl;
827 
828 	ifp->if_type = IFT_ETHER;
829 	ifp->if_addrlen = 6;
830 	ifp->if_hdrlen = 14;
831 	ifp->if_mtu = ETHERMTU;
832 	ifp->if_output = ether_output;
833 	ifp->if_input = ether_input;
834 	if (ifp->if_baudrate == 0)
835 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
836 	if ((sdl = ifp->if_sadl) &&
837 	    sdl->sdl_family == AF_LINK) {
838 		sdl->sdl_type = IFT_ETHER;
839 		sdl->sdl_alen = ifp->if_addrlen;
840 		bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
841 	}
842 	LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
843 	ifp->if_broadcastaddr = etherbroadcastaddr;
844 #if NBPFILTER > 0
845 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
846 #endif
847 }
848 
849 void
850 ether_ifdetach(struct ifnet *ifp)
851 {
852 	struct ethercom *ec = (void *) ifp;
853 	struct sockaddr_dl *sdl = ifp->if_sadl;
854 	struct ether_multi *enm;
855 	int s;
856 
857 #if NBPFILTER > 0
858 	bpfdetach(ifp);
859 #endif
860 
861 #if NVLAN > 0
862 	if (ec->ec_nvlans)
863 		vlan_ifdetach(ifp);
864 #endif
865 
866 	s = splimp();
867 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
868 		LIST_REMOVE(enm, enm_list);
869 		free(enm, M_IFADDR);
870 		ec->ec_multicnt--;
871 	}
872 	splx(s);
873 
874 	memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
875 	sdl->sdl_alen = 0;
876 	sdl->sdl_type = 0;
877 }
878 
879 #if 0
880 /*
881  * This is for reference.  We have a table-driven version
882  * of the little-endian crc32 generator, which is faster
883  * than the double-loop.
884  */
885 u_int32_t
886 ether_crc32_le(const u_int8_t *buf, size_t len)
887 {
888 	u_int32_t c, crc, carry;
889 	size_t i, j;
890 
891 	crc = 0xffffffffU;	/* initial value */
892 
893 	for (i = 0; i < len; i++) {
894 		c = buf[i];
895 		for (j = 0; j < 8; j++) {
896 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
897 			crc >>= 1;
898 			c >>= 1;
899 			if (carry)
900 				crc = (crc ^ ETHER_CRC_POLY_LE);
901 		}
902 	}
903 
904 	return (crc);
905 }
906 #else
907 u_int32_t
908 ether_crc32_le(const u_int8_t *buf, size_t len)
909 {
910 	static const u_int32_t crctab[] = {
911 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
912 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
913 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
914 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
915 	};
916 	u_int32_t crc;
917 	int i;
918 
919 	crc = 0xffffffffU;	/* initial value */
920 
921 	for (i = 0; i < len; i++) {
922 		crc ^= buf[i];
923 		crc = (crc >> 4) ^ crctab[crc & 0xf];
924 		crc = (crc >> 4) ^ crctab[crc & 0xf];
925 	}
926 
927 	return (crc);
928 }
929 #endif
930 
931 u_int32_t
932 ether_crc32_be(const u_int8_t *buf, size_t len)
933 {
934 	u_int32_t c, crc, carry;
935 	size_t i, j;
936 
937 	crc = 0xffffffffU;	/* initial value */
938 
939 	for (i = 0; i < len; i++) {
940 		c = buf[i];
941 		for (j = 0; j < 8; j++) {
942 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
943 			crc <<= 1;
944 			c >>= 1;
945 			if (carry)
946 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
947 		}
948 	}
949 
950 	return (crc);
951 }
952 
953 #ifdef INET
954 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
955 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
956 #endif
957 #ifdef INET6
958 u_char	ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
959 u_char	ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
960 #endif
961 
962 /*
963  * Convert a sockaddr into an Ethernet address or range of Ethernet
964  * addresses.
965  */
966 int
967 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
968     u_int8_t addrhi[ETHER_ADDR_LEN])
969 {
970 #ifdef INET
971 	struct sockaddr_in *sin;
972 #endif /* INET */
973 #ifdef INET6
974 	struct sockaddr_in6 *sin6;
975 #endif /* INET6 */
976 
977 	switch (sa->sa_family) {
978 
979 	case AF_UNSPEC:
980 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
981 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
982 		break;
983 
984 #ifdef INET
985 	case AF_INET:
986 		sin = satosin(sa);
987 		if (sin->sin_addr.s_addr == INADDR_ANY) {
988 			/*
989 			 * An IP address of INADDR_ANY means listen to
990 			 * or stop listening to all of the Ethernet
991 			 * multicast addresses used for IP.
992 			 * (This is for the sake of IP multicast routers.)
993 			 */
994 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
995 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
996 		}
997 		else {
998 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
999 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1000 		}
1001 		break;
1002 #endif
1003 #ifdef INET6
1004 	case AF_INET6:
1005 		sin6 = satosin6(sa);
1006 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1007 			/*
1008 			 * An IP6 address of 0 means listen to or stop
1009 			 * listening to all of the Ethernet multicast
1010 			 * address used for IP6.
1011 			 * (This is used for multicast routers.)
1012 			 */
1013 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1014 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1015 		} else {
1016 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1017 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1018 		}
1019 		break;
1020 #endif
1021 
1022 	default:
1023 		return (EAFNOSUPPORT);
1024 	}
1025 	return (0);
1026 }
1027 
1028 /*
1029  * Add an Ethernet multicast address or range of addresses to the list for a
1030  * given interface.
1031  */
1032 int
1033 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1034 {
1035 	struct ether_multi *enm;
1036 	u_char addrlo[ETHER_ADDR_LEN];
1037 	u_char addrhi[ETHER_ADDR_LEN];
1038 	int s = splimp(), error;
1039 
1040 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1041 	if (error != 0) {
1042 		splx(s);
1043 		return (error);
1044 	}
1045 
1046 	/*
1047 	 * Verify that we have valid Ethernet multicast addresses.
1048 	 */
1049 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1050 		splx(s);
1051 		return (EINVAL);
1052 	}
1053 	/*
1054 	 * See if the address range is already in the list.
1055 	 */
1056 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1057 	if (enm != NULL) {
1058 		/*
1059 		 * Found it; just increment the reference count.
1060 		 */
1061 		++enm->enm_refcount;
1062 		splx(s);
1063 		return (0);
1064 	}
1065 	/*
1066 	 * New address or range; malloc a new multicast record
1067 	 * and link it into the interface's multicast list.
1068 	 */
1069 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1070 	if (enm == NULL) {
1071 		splx(s);
1072 		return (ENOBUFS);
1073 	}
1074 	bcopy(addrlo, enm->enm_addrlo, 6);
1075 	bcopy(addrhi, enm->enm_addrhi, 6);
1076 	enm->enm_ec = ec;
1077 	enm->enm_refcount = 1;
1078 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1079 	ec->ec_multicnt++;
1080 	splx(s);
1081 	/*
1082 	 * Return ENETRESET to inform the driver that the list has changed
1083 	 * and its reception filter should be adjusted accordingly.
1084 	 */
1085 	return (ENETRESET);
1086 }
1087 
1088 /*
1089  * Delete a multicast address record.
1090  */
1091 int
1092 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1093 {
1094 	struct ether_multi *enm;
1095 	u_char addrlo[ETHER_ADDR_LEN];
1096 	u_char addrhi[ETHER_ADDR_LEN];
1097 	int s = splimp(), error;
1098 
1099 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1100 	if (error != 0) {
1101 		splx(s);
1102 		return (error);
1103 	}
1104 
1105 	/*
1106 	 * Look ur the address in our list.
1107 	 */
1108 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1109 	if (enm == NULL) {
1110 		splx(s);
1111 		return (ENXIO);
1112 	}
1113 	if (--enm->enm_refcount != 0) {
1114 		/*
1115 		 * Still some claims to this record.
1116 		 */
1117 		splx(s);
1118 		return (0);
1119 	}
1120 	/*
1121 	 * No remaining claims to this record; unlink and free it.
1122 	 */
1123 	LIST_REMOVE(enm, enm_list);
1124 	free(enm, M_IFMADDR);
1125 	ec->ec_multicnt--;
1126 	splx(s);
1127 	/*
1128 	 * Return ENETRESET to inform the driver that the list has changed
1129 	 * and its reception filter should be adjusted accordingly.
1130 	 */
1131 	return (ENETRESET);
1132 }
1133 
1134 /*
1135  * Common ioctls for Ethernet interfaces.  Note, we must be
1136  * called at splnet().
1137  */
1138 int
1139 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1140 {
1141 	struct ethercom *ec = (void *) ifp;
1142 	struct ifreq *ifr = (struct ifreq *)data;
1143 	struct ifaddr *ifa = (struct ifaddr *)data;
1144 	int error = 0;
1145 
1146 	switch (cmd) {
1147 	case SIOCSIFADDR:
1148 		ifp->if_flags |= IFF_UP;
1149 		switch (ifa->ifa_addr->sa_family) {
1150 #ifdef INET
1151 		case AF_INET:
1152 			if ((error = (*ifp->if_init)(ifp)) != 0)
1153 				break;
1154 			arp_ifinit(ifp, ifa);
1155 			break;
1156 #endif /* INET */
1157 #ifdef NS
1158 		case AF_NS:
1159 		    {
1160 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1161 
1162 			if (ns_nullhost(*ina))
1163 				ina->x_host = *(union ns_host *)
1164 				    LLADDR(ifp->if_sadl);
1165 			else
1166 				memcpy(LLADDR(ifp->if_sadl),
1167 				    ina->x_host.c_host, ifp->if_addrlen);
1168 			/* Set new address. */
1169 			error = (*ifp->if_init)(ifp);
1170 			break;
1171 		    }
1172 #endif /* NS */
1173 		default:
1174 			error = (*ifp->if_init)(ifp);
1175 			break;
1176 		}
1177 		break;
1178 
1179 	case SIOCGIFADDR:
1180 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1181 		    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1182 		break;
1183 
1184 	case SIOCSIFMTU:
1185 		if (ifr->ifr_mtu > ETHERMTU)
1186 			error = EINVAL;
1187 		else
1188 			ifp->if_mtu = ifr->ifr_mtu;
1189 		break;
1190 
1191 	case SIOCSIFFLAGS:
1192 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1193 			/*
1194 			 * If interface is marked down and it is running,
1195 			 * then stop and disable it.
1196 			 */
1197 			(*ifp->if_stop)(ifp, 1);
1198 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1199 			/*
1200 			 * If interface is marked up and it is stopped, then
1201 			 * start it.
1202 			 */
1203 			error = (*ifp->if_init)(ifp);
1204 		} else if ((ifp->if_flags & IFF_UP) != 0) {
1205 			/*
1206 			 * Reset the interface to pick up changes in any other
1207 			 * flags that affect the hardware state.
1208 			 */
1209 			error = (*ifp->if_init)(ifp);
1210 		}
1211 		break;
1212 
1213 	case SIOCADDMULTI:
1214 	case SIOCDELMULTI:
1215 		error = (cmd == SIOCADDMULTI) ?
1216 		    ether_addmulti(ifr, ec) :
1217 		    ether_delmulti(ifr, ec);
1218 		break;
1219 
1220 	default:
1221 		error = ENOTTY;
1222 	}
1223 
1224 	return (error);
1225 }
1226