xref: /netbsd-src/sys/net/if_ethersubr.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: if_ethersubr.c,v 1.74 2000/12/26 23:54:34 augustss 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_dlt = DLT_EN10MB;
832 	ifp->if_mtu = ETHERMTU;
833 	ifp->if_output = ether_output;
834 	ifp->if_input = ether_input;
835 	if (ifp->if_baudrate == 0)
836 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
837 	if ((sdl = ifp->if_sadl) &&
838 	    sdl->sdl_family == AF_LINK) {
839 		sdl->sdl_type = IFT_ETHER;
840 		sdl->sdl_alen = ifp->if_addrlen;
841 		bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
842 	}
843 	LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
844 	ifp->if_broadcastaddr = etherbroadcastaddr;
845 #if NBPFILTER > 0
846 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
847 #endif
848 }
849 
850 void
851 ether_ifdetach(struct ifnet *ifp)
852 {
853 	struct ethercom *ec = (void *) ifp;
854 	struct sockaddr_dl *sdl = ifp->if_sadl;
855 	struct ether_multi *enm;
856 	int s;
857 
858 #if NBPFILTER > 0
859 	bpfdetach(ifp);
860 #endif
861 
862 #if NVLAN > 0
863 	if (ec->ec_nvlans)
864 		vlan_ifdetach(ifp);
865 #endif
866 
867 	s = splimp();
868 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
869 		LIST_REMOVE(enm, enm_list);
870 		free(enm, M_IFADDR);
871 		ec->ec_multicnt--;
872 	}
873 	splx(s);
874 
875 	memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
876 	sdl->sdl_alen = 0;
877 	sdl->sdl_type = 0;
878 }
879 
880 #if 0
881 /*
882  * This is for reference.  We have a table-driven version
883  * of the little-endian crc32 generator, which is faster
884  * than the double-loop.
885  */
886 u_int32_t
887 ether_crc32_le(const u_int8_t *buf, size_t len)
888 {
889 	u_int32_t c, crc, carry;
890 	size_t i, j;
891 
892 	crc = 0xffffffffU;	/* initial value */
893 
894 	for (i = 0; i < len; i++) {
895 		c = buf[i];
896 		for (j = 0; j < 8; j++) {
897 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
898 			crc >>= 1;
899 			c >>= 1;
900 			if (carry)
901 				crc = (crc ^ ETHER_CRC_POLY_LE);
902 		}
903 	}
904 
905 	return (crc);
906 }
907 #else
908 u_int32_t
909 ether_crc32_le(const u_int8_t *buf, size_t len)
910 {
911 	static const u_int32_t crctab[] = {
912 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
913 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
914 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
915 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
916 	};
917 	u_int32_t crc;
918 	int i;
919 
920 	crc = 0xffffffffU;	/* initial value */
921 
922 	for (i = 0; i < len; i++) {
923 		crc ^= buf[i];
924 		crc = (crc >> 4) ^ crctab[crc & 0xf];
925 		crc = (crc >> 4) ^ crctab[crc & 0xf];
926 	}
927 
928 	return (crc);
929 }
930 #endif
931 
932 u_int32_t
933 ether_crc32_be(const u_int8_t *buf, size_t len)
934 {
935 	u_int32_t c, crc, carry;
936 	size_t i, j;
937 
938 	crc = 0xffffffffU;	/* initial value */
939 
940 	for (i = 0; i < len; i++) {
941 		c = buf[i];
942 		for (j = 0; j < 8; j++) {
943 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
944 			crc <<= 1;
945 			c >>= 1;
946 			if (carry)
947 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
948 		}
949 	}
950 
951 	return (crc);
952 }
953 
954 #ifdef INET
955 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
956 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
957 #endif
958 #ifdef INET6
959 u_char	ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
960 u_char	ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
961 #endif
962 
963 /*
964  * Convert a sockaddr into an Ethernet address or range of Ethernet
965  * addresses.
966  */
967 int
968 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
969     u_int8_t addrhi[ETHER_ADDR_LEN])
970 {
971 #ifdef INET
972 	struct sockaddr_in *sin;
973 #endif /* INET */
974 #ifdef INET6
975 	struct sockaddr_in6 *sin6;
976 #endif /* INET6 */
977 
978 	switch (sa->sa_family) {
979 
980 	case AF_UNSPEC:
981 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
982 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
983 		break;
984 
985 #ifdef INET
986 	case AF_INET:
987 		sin = satosin(sa);
988 		if (sin->sin_addr.s_addr == INADDR_ANY) {
989 			/*
990 			 * An IP address of INADDR_ANY means listen to
991 			 * or stop listening to all of the Ethernet
992 			 * multicast addresses used for IP.
993 			 * (This is for the sake of IP multicast routers.)
994 			 */
995 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
996 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
997 		}
998 		else {
999 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1000 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1001 		}
1002 		break;
1003 #endif
1004 #ifdef INET6
1005 	case AF_INET6:
1006 		sin6 = satosin6(sa);
1007 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1008 			/*
1009 			 * An IP6 address of 0 means listen to or stop
1010 			 * listening to all of the Ethernet multicast
1011 			 * address used for IP6.
1012 			 * (This is used for multicast routers.)
1013 			 */
1014 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1015 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1016 		} else {
1017 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1018 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1019 		}
1020 		break;
1021 #endif
1022 
1023 	default:
1024 		return (EAFNOSUPPORT);
1025 	}
1026 	return (0);
1027 }
1028 
1029 /*
1030  * Add an Ethernet multicast address or range of addresses to the list for a
1031  * given interface.
1032  */
1033 int
1034 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1035 {
1036 	struct ether_multi *enm;
1037 	u_char addrlo[ETHER_ADDR_LEN];
1038 	u_char addrhi[ETHER_ADDR_LEN];
1039 	int s = splimp(), error;
1040 
1041 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1042 	if (error != 0) {
1043 		splx(s);
1044 		return (error);
1045 	}
1046 
1047 	/*
1048 	 * Verify that we have valid Ethernet multicast addresses.
1049 	 */
1050 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1051 		splx(s);
1052 		return (EINVAL);
1053 	}
1054 	/*
1055 	 * See if the address range is already in the list.
1056 	 */
1057 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1058 	if (enm != NULL) {
1059 		/*
1060 		 * Found it; just increment the reference count.
1061 		 */
1062 		++enm->enm_refcount;
1063 		splx(s);
1064 		return (0);
1065 	}
1066 	/*
1067 	 * New address or range; malloc a new multicast record
1068 	 * and link it into the interface's multicast list.
1069 	 */
1070 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1071 	if (enm == NULL) {
1072 		splx(s);
1073 		return (ENOBUFS);
1074 	}
1075 	bcopy(addrlo, enm->enm_addrlo, 6);
1076 	bcopy(addrhi, enm->enm_addrhi, 6);
1077 	enm->enm_ec = ec;
1078 	enm->enm_refcount = 1;
1079 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1080 	ec->ec_multicnt++;
1081 	splx(s);
1082 	/*
1083 	 * Return ENETRESET to inform the driver that the list has changed
1084 	 * and its reception filter should be adjusted accordingly.
1085 	 */
1086 	return (ENETRESET);
1087 }
1088 
1089 /*
1090  * Delete a multicast address record.
1091  */
1092 int
1093 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1094 {
1095 	struct ether_multi *enm;
1096 	u_char addrlo[ETHER_ADDR_LEN];
1097 	u_char addrhi[ETHER_ADDR_LEN];
1098 	int s = splimp(), error;
1099 
1100 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1101 	if (error != 0) {
1102 		splx(s);
1103 		return (error);
1104 	}
1105 
1106 	/*
1107 	 * Look ur the address in our list.
1108 	 */
1109 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1110 	if (enm == NULL) {
1111 		splx(s);
1112 		return (ENXIO);
1113 	}
1114 	if (--enm->enm_refcount != 0) {
1115 		/*
1116 		 * Still some claims to this record.
1117 		 */
1118 		splx(s);
1119 		return (0);
1120 	}
1121 	/*
1122 	 * No remaining claims to this record; unlink and free it.
1123 	 */
1124 	LIST_REMOVE(enm, enm_list);
1125 	free(enm, M_IFMADDR);
1126 	ec->ec_multicnt--;
1127 	splx(s);
1128 	/*
1129 	 * Return ENETRESET to inform the driver that the list has changed
1130 	 * and its reception filter should be adjusted accordingly.
1131 	 */
1132 	return (ENETRESET);
1133 }
1134 
1135 /*
1136  * Common ioctls for Ethernet interfaces.  Note, we must be
1137  * called at splnet().
1138  */
1139 int
1140 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1141 {
1142 	struct ethercom *ec = (void *) ifp;
1143 	struct ifreq *ifr = (struct ifreq *)data;
1144 	struct ifaddr *ifa = (struct ifaddr *)data;
1145 	int error = 0;
1146 
1147 	switch (cmd) {
1148 	case SIOCSIFADDR:
1149 		ifp->if_flags |= IFF_UP;
1150 		switch (ifa->ifa_addr->sa_family) {
1151 #ifdef INET
1152 		case AF_INET:
1153 			if ((error = (*ifp->if_init)(ifp)) != 0)
1154 				break;
1155 			arp_ifinit(ifp, ifa);
1156 			break;
1157 #endif /* INET */
1158 #ifdef NS
1159 		case AF_NS:
1160 		    {
1161 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1162 
1163 			if (ns_nullhost(*ina))
1164 				ina->x_host = *(union ns_host *)
1165 				    LLADDR(ifp->if_sadl);
1166 			else
1167 				memcpy(LLADDR(ifp->if_sadl),
1168 				    ina->x_host.c_host, ifp->if_addrlen);
1169 			/* Set new address. */
1170 			error = (*ifp->if_init)(ifp);
1171 			break;
1172 		    }
1173 #endif /* NS */
1174 		default:
1175 			error = (*ifp->if_init)(ifp);
1176 			break;
1177 		}
1178 		break;
1179 
1180 	case SIOCGIFADDR:
1181 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1182 		    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1183 		break;
1184 
1185 	case SIOCSIFMTU:
1186 		if (ifr->ifr_mtu > ETHERMTU)
1187 			error = EINVAL;
1188 		else
1189 			ifp->if_mtu = ifr->ifr_mtu;
1190 		break;
1191 
1192 	case SIOCSIFFLAGS:
1193 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1194 			/*
1195 			 * If interface is marked down and it is running,
1196 			 * then stop and disable it.
1197 			 */
1198 			(*ifp->if_stop)(ifp, 1);
1199 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1200 			/*
1201 			 * If interface is marked up and it is stopped, then
1202 			 * start it.
1203 			 */
1204 			error = (*ifp->if_init)(ifp);
1205 		} else if ((ifp->if_flags & IFF_UP) != 0) {
1206 			/*
1207 			 * Reset the interface to pick up changes in any other
1208 			 * flags that affect the hardware state.
1209 			 */
1210 			error = (*ifp->if_init)(ifp);
1211 		}
1212 		break;
1213 
1214 	case SIOCADDMULTI:
1215 		error = ether_addmulti(ifr, ec);
1216 		break;
1217 
1218 	case SIOCDELMULTI:
1219 		error = ether_delmulti(ifr, ec);
1220 		break;
1221 
1222 	default:
1223 		error = ENOTTY;
1224 	}
1225 
1226 	return (error);
1227 }
1228