xref: /openbsd-src/sys/net/if_ethersubr.c (revision 94fd4554194a14f126fba33b837cc68a1df42468)
1 /*	$OpenBSD: if_ethersubr.c,v 1.106 2007/03/18 23:23:17 mpf Exp $	*/
2 /*	$NetBSD: if_ethersubr.c,v 1.19 1996/05/07 02:40:30 thorpej Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
62  */
63 
64 /*
65 %%% portions-copyright-nrl-95
66 Portions of this software are Copyright 1995-1998 by Randall Atkinson,
67 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
68 Reserved. All rights under this copyright have been assigned to the US
69 Naval Research Laboratory (NRL). The NRL Copyright Notice and License
70 Agreement Version 1.1 (January 17, 1995) applies to these portions of the
71 software.
72 You should have received a copy of the license with this software. If you
73 didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
74 */
75 
76 #include "bpfilter.h"
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86 #include <sys/errno.h>
87 #include <sys/syslog.h>
88 #include <sys/timeout.h>
89 
90 #include <machine/cpu.h>
91 
92 #include <net/if.h>
93 #include <net/netisr.h>
94 #include <net/route.h>
95 #include <net/if_llc.h>
96 #include <net/if_dl.h>
97 #include <net/if_media.h>
98 #include <net/if_types.h>
99 
100 #include <netinet/in.h>
101 #ifdef INET
102 #include <netinet/in_var.h>
103 #endif
104 #include <netinet/if_ether.h>
105 #include <netinet/ip_ipsp.h>
106 
107 #if NBPFILTER > 0
108 #include <net/bpf.h>
109 #endif
110 
111 #include "bridge.h"
112 #if NBRIDGE > 0
113 #include <net/if_bridge.h>
114 #endif
115 
116 #include "vlan.h"
117 #if NVLAN > 0
118 #include <net/if_vlan_var.h>
119 #endif /* NVLAN > 0 */
120 
121 #include "carp.h"
122 #if NCARP > 0
123 #include <netinet/ip_carp.h>
124 #endif
125 
126 #include "pppoe.h"
127 #if NPPPOE > 0
128 #include <net/if_pppoe.h>
129 #endif
130 
131 #include "trunk.h"
132 #if NTRUNK > 0
133 #include <net/if_trunk.h>
134 #endif
135 
136 #ifdef INET6
137 #ifndef INET
138 #include <netinet/in.h>
139 #endif
140 #include <netinet6/in6_var.h>
141 #include <netinet6/nd6.h>
142 #endif
143 
144 #ifdef IPX
145 #include <netipx/ipx.h>
146 #include <netipx/ipx_if.h>
147 #endif
148 
149 #ifdef NETATALK
150 #include <netatalk/at.h>
151 #include <netatalk/at_var.h>
152 #include <netatalk/at_extern.h>
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[ETHER_ADDR_LEN] =
159     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
160 #define senderr(e) { error = (e); goto bad;}
161 
162 
163 int
164 ether_ioctl(ifp, arp, cmd, data)
165 	struct ifnet *ifp;
166 	struct arpcom *arp;
167 	u_long cmd;
168 	caddr_t data;
169 {
170 	struct ifaddr *ifa = (struct ifaddr *)data;
171 	int	error = 0;
172 
173 	switch (cmd) {
174 
175 	case SIOCSIFADDR:
176 		switch (ifa->ifa_addr->sa_family) {
177 #ifdef IPX
178 		case AF_IPX:
179 		    {
180 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
181 
182 			if (ipx_nullhost(*ina))
183 				ina->ipx_host =
184 				    *(union ipx_host *)(arp->ac_enaddr);
185 			else
186 				bcopy(ina->ipx_host.c_host,
187 				    arp->ac_enaddr, sizeof(arp->ac_enaddr));
188 			break;
189 		    }
190 #endif /* IPX */
191 #ifdef NETATALK
192 		case AF_APPLETALK:
193 			/* Nothing to do. */
194 			break;
195 #endif /* NETATALK */
196 		}
197 		break;
198 	default:
199 		break;
200 	}
201 
202 	return error;
203 }
204 
205 /*
206  * Ethernet output routine.
207  * Encapsulate a packet of type family for the local net.
208  * Assumes that ifp is actually pointer to arpcom structure.
209  */
210 int
211 ether_output(ifp0, m0, dst, rt0)
212 	struct ifnet *ifp0;
213 	struct mbuf *m0;
214 	struct sockaddr *dst;
215 	struct rtentry *rt0;
216 {
217 	u_int16_t etype;
218 	int s, len, error = 0, hdrcmplt = 0;
219 	u_char edst[ETHER_ADDR_LEN], esrc[ETHER_ADDR_LEN];
220 	struct mbuf *m = m0;
221 	struct rtentry *rt;
222 	struct mbuf *mcopy = (struct mbuf *)0;
223 	struct ether_header *eh;
224 	struct arpcom *ac = (struct arpcom *)ifp0;
225 	short mflags;
226 	struct ifnet *ifp = ifp0;
227 
228 #if NTRUNK > 0
229 	if (ifp->if_type == IFT_IEEE8023ADLAG)
230 		senderr(EBUSY);
231 #endif
232 
233 #if NCARP > 0
234 	if (ifp->if_type == IFT_CARP) {
235 		struct ifaddr *ifa;
236 
237 		/* loop back if this is going to the carp interface */
238 		if (dst != NULL && LINK_STATE_IS_UP(ifp0->if_link_state) &&
239 		    (ifa = ifa_ifwithaddr(dst)) != NULL &&
240 		    ifa->ifa_ifp == ifp0)
241 			return (looutput(ifp0, m, dst, rt0));
242 
243 		ifp = ifp->if_carpdev;
244 		ac = (struct arpcom *)ifp;
245 
246 		if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
247 		    (IFF_UP|IFF_RUNNING))
248 			senderr(ENETDOWN);
249 	}
250 #endif /* NCARP > 0 */
251 
252 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
253 		senderr(ENETDOWN);
254 	if ((rt = rt0) != NULL) {
255 		if ((rt->rt_flags & RTF_UP) == 0) {
256 			if ((rt0 = rt = rtalloc1(dst, 1, 0)) != NULL)
257 				rt->rt_refcnt--;
258 			else
259 				senderr(EHOSTUNREACH);
260 		}
261 		if (rt->rt_flags & RTF_GATEWAY) {
262 			if (rt->rt_gwroute == 0)
263 				goto lookup;
264 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
265 				rtfree(rt); rt = rt0;
266 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0);
267 				if ((rt = rt->rt_gwroute) == 0)
268 					senderr(EHOSTUNREACH);
269 			}
270 		}
271 		if (rt->rt_flags & RTF_REJECT)
272 			if (rt->rt_rmx.rmx_expire == 0 ||
273 			    time_second < rt->rt_rmx.rmx_expire)
274 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
275 	}
276 
277 	switch (dst->sa_family) {
278 
279 #ifdef INET
280 	case AF_INET:
281 		if (!arpresolve(ac, rt, m, dst, edst))
282 			return (0);	/* if not yet resolved */
283 		/* If broadcasting on a simplex interface, loopback a copy */
284 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) {
285 #if NPF > 0
286 			struct pf_mtag	*t;
287 
288 			if ((t = pf_find_mtag(m)) == NULL || !t->routed)
289 #endif
290 				mcopy = m_copy(m, 0, (int)M_COPYALL);
291 		}
292 		etype = htons(ETHERTYPE_IP);
293 		break;
294 #endif
295 #ifdef INET6
296 	case AF_INET6:
297 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst))
298 			return (0); /* it must be impossible, but... */
299 		etype = htons(ETHERTYPE_IPV6);
300 		break;
301 #endif
302 #ifdef IPX
303 	case AF_IPX:
304 		etype = htons(ETHERTYPE_IPX);
305 		bcopy((caddr_t)&satosipx(dst)->sipx_addr.ipx_host,
306 		    (caddr_t)edst, sizeof(edst));
307 		/* If broadcasting on a simplex interface, loopback a copy */
308 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
309 			mcopy = m_copy(m, 0, (int)M_COPYALL);
310 		break;
311 #endif
312 #ifdef NETATALK
313 	case AF_APPLETALK: {
314 		struct at_ifaddr *aa;
315 
316 		if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
317 #ifdef NETATALKDEBUG
318 			extern char *prsockaddr(struct sockaddr *);
319 			printf("aarpresolv: failed for %s\n", prsockaddr(dst));
320 #endif /* NETATALKDEBUG */
321 			return (0);
322 		}
323 
324 		/*
325 		 * ifaddr is the first thing in at_ifaddr
326 		 */
327 		aa = (struct at_ifaddr *)at_ifawithnet(
328 			(struct sockaddr_at *)dst,
329 			TAILQ_FIRST(&ifp->if_addrlist));
330 		if (aa == 0)
331 			goto bad;
332 
333 		/*
334 		 * In the phase 2 case, we need to prepend an mbuf for the llc
335 		 * header. Since we must preserve the value of m, which is
336 		 * passed to us by value, we m_copy() the first mbuf,
337 		 * and use it for our llc header.
338 		 */
339 		if (aa->aa_flags & AFA_PHASE2) {
340 			struct llc llc;
341 
342 			M_PREPEND(m, AT_LLC_SIZE, M_DONTWAIT);
343 			if (m == NULL)
344 				return (0);
345 			/*
346 			 * FreeBSD doesn't count the LLC len in
347 			 * ifp->obytes, so they increment a length
348 			 * field here. We don't do this.
349 			 */
350 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
351 			llc.llc_control = LLC_UI;
352 			bcopy(at_org_code, llc.llc_snap.org_code,
353 				sizeof(at_org_code));
354 			llc.llc_snap.ether_type = htons( ETHERTYPE_AT );
355 			bcopy(&llc, mtod(m, caddr_t), AT_LLC_SIZE);
356 			etype = htons(m->m_pkthdr.len);
357 		} else {
358 			etype = htons(ETHERTYPE_AT);
359 		}
360 		} break;
361 #endif /* NETATALK */
362 	case pseudo_AF_HDRCMPLT:
363 		hdrcmplt = 1;
364 		eh = (struct ether_header *)dst->sa_data;
365 		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof(esrc));
366 		/* FALLTHROUGH */
367 
368 	case AF_UNSPEC:
369 		eh = (struct ether_header *)dst->sa_data;
370 		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof(edst));
371 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
372 		etype = eh->ether_type;
373 		break;
374 
375 	default:
376 		printf("%s: can't handle af%d\n", ifp->if_xname,
377 			dst->sa_family);
378 		senderr(EAFNOSUPPORT);
379 	}
380 
381 	/* XXX Should we feed-back an unencrypted IPsec packet ? */
382 	if (mcopy)
383 		(void) looutput(ifp, mcopy, dst, rt);
384 
385 	/*
386 	 * Add local net header.  If no space in first mbuf,
387 	 * allocate another.
388 	 */
389 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
390 	if (m == 0)
391 		senderr(ENOBUFS);
392 	eh = mtod(m, struct ether_header *);
393 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
394 		sizeof(eh->ether_type));
395 	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof(edst));
396 	if (hdrcmplt)
397 		bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
398 		    sizeof(eh->ether_shost));
399 	else
400 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
401 		    sizeof(eh->ether_shost));
402 
403 #if NCARP > 0
404 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP &&
405 	    !(ifp0->if_flags & IFF_LINK1)) {
406 		bcopy((caddr_t)((struct arpcom *)ifp0)->ac_enaddr,
407 		    (caddr_t)eh->ether_shost, sizeof(eh->ether_shost));
408 	}
409 #endif
410 
411 #if NBRIDGE > 0
412 	/*
413 	 * Interfaces that are bridge members need special handling
414 	 * for output.
415 	 */
416 	if (ifp->if_bridge) {
417 		struct m_tag *mtag;
418 
419 		/*
420 		 * Check if this packet has already been sent out through
421 		 * this bridge, in which case we simply send it out
422 		 * without further bridge processing.
423 		 */
424 		for (mtag = m_tag_find(m, PACKET_TAG_BRIDGE, NULL); mtag;
425 		    mtag = m_tag_find(m, PACKET_TAG_BRIDGE, mtag)) {
426 #ifdef DEBUG
427 			/* Check that the information is there */
428 			if (mtag->m_tag_len != sizeof(caddr_t)) {
429 				error = EINVAL;
430 				goto bad;
431 			}
432 #endif
433 			if (!bcmp(&ifp->if_bridge, mtag + 1, sizeof(caddr_t)))
434 				break;
435 		}
436 		if (mtag == NULL) {
437 			/* Attach a tag so we can detect loops */
438 			mtag = m_tag_get(PACKET_TAG_BRIDGE, sizeof(caddr_t),
439 			    M_NOWAIT);
440 			if (mtag == NULL) {
441 				error = ENOBUFS;
442 				goto bad;
443 			}
444 			bcopy(&ifp->if_bridge, mtag + 1, sizeof(caddr_t));
445 			m_tag_prepend(m, mtag);
446 			bridge_output(ifp, m, NULL, NULL);
447 			return (error);
448 		}
449 	}
450 #endif
451 
452 	mflags = m->m_flags;
453 	len = m->m_pkthdr.len;
454 	s = splnet();
455 	/*
456 	 * Queue message on interface, and start output if interface
457 	 * not yet active.
458 	 */
459 	IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
460 	if (error) {
461 		/* mbuf is already freed */
462 		splx(s);
463 		return (error);
464 	}
465 	ifp->if_obytes += len + ETHER_HDR_LEN;
466 #if NCARP > 0
467 	if (ifp != ifp0)
468 		ifp0->if_obytes += len + ETHER_HDR_LEN;
469 #endif /* NCARP > 0 */
470 	if (mflags & M_MCAST)
471 		ifp->if_omcasts++;
472 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
473 		(*ifp->if_start)(ifp);
474 	splx(s);
475 	return (error);
476 
477 bad:
478 	if (m)
479 		m_freem(m);
480 	return (error);
481 }
482 
483 /*
484  * Process a received Ethernet packet;
485  * the packet is in the mbuf chain m without
486  * the ether header, which is provided separately.
487  */
488 void
489 ether_input(ifp, eh, m)
490 	struct ifnet *ifp;
491 	struct ether_header *eh;
492 	struct mbuf *m;
493 {
494 	struct ifqueue *inq;
495 	u_int16_t etype;
496 	int s, llcfound = 0;
497 	struct llc *l;
498 	struct arpcom *ac;
499 #if NTRUNK > 0
500 	int i = 0;
501 #endif
502 #if NPPPOE > 0
503 	struct ether_header *eh_tmp;
504 #endif
505 
506 	if (eh == NULL) {
507 		eh = mtod(m, struct ether_header *);
508 		m_adj(m, ETHER_HDR_LEN);
509 	}
510 
511 #if NTRUNK > 0
512 	/* Handle input from a trunk port */
513 	while (ifp->if_type == IFT_IEEE8023ADLAG) {
514 		if (++i > TRUNK_MAX_STACKING ||
515 		    trunk_input(ifp, eh, m) != 0) {
516 			if (m)
517 				m_freem(m);
518 			return;
519 		}
520 
521 		/* Has been set to the trunk interface */
522 		ifp = m->m_pkthdr.rcvif;
523 	}
524 #endif
525 
526 	if ((ifp->if_flags & IFF_UP) == 0) {
527 		m_freem(m);
528 		return;
529 	}
530 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
531 		if ((ifp->if_flags & IFF_SIMPLEX) == 0) {
532 			struct ifaddr *ifa;
533 			struct sockaddr_dl *sdl = NULL;
534 
535 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
536 				if ((sdl =
537 				    (struct sockaddr_dl *)ifa->ifa_addr) &&
538 				    sdl->sdl_family == AF_LINK)
539 					break;
540 			}
541 			/*
542 			 * If this is not a simplex interface, drop the packet
543 			 * if it came from us.
544 			 */
545 			if (sdl && bcmp(LLADDR(sdl), eh->ether_shost,
546 			    ETHER_ADDR_LEN) == 0) {
547 				m_freem(m);
548 				return;
549 			}
550 		}
551 
552 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
553 		    sizeof(etherbroadcastaddr)) == 0)
554 			m->m_flags |= M_BCAST;
555 		else
556 			m->m_flags |= M_MCAST;
557 		ifp->if_imcasts++;
558 	}
559 
560 	ifp->if_ibytes += m->m_pkthdr.len + sizeof(*eh);
561 
562 	etype = ntohs(eh->ether_type);
563 
564 #if NVLAN > 0
565 	if (etype == ETHERTYPE_VLAN && (vlan_input(eh, m) == 0))
566 		return;
567 #endif
568 
569 #if NBRIDGE > 0
570 	/*
571 	 * Tap the packet off here for a bridge, if configured and
572 	 * active for this interface.  bridge_input returns
573 	 * NULL if it has consumed the packet, otherwise, it
574 	 * gets processed as normal.
575 	 */
576 	if (ifp->if_bridge) {
577 		if (m->m_flags & M_PROTO1)
578 			m->m_flags &= ~M_PROTO1;
579 		else {
580 			m = bridge_input(ifp, eh, m);
581 			if (m == NULL)
582 				return;
583 			/* The bridge has determined it's for us. */
584 			ifp = m->m_pkthdr.rcvif;
585 		}
586 	}
587 #endif
588 
589 #if NVLAN > 0
590 	if (etype == ETHERTYPE_VLAN) {
591 		/* The bridge did not want the vlan frame either, drop it. */
592 		ifp->if_noproto++;
593 		m_freem(m);
594 		return;
595 	}
596 #endif /* NVLAN > 0 */
597 
598 #if NCARP > 0
599 	if (ifp->if_carp) {
600 		if (ifp->if_type != IFT_CARP &&
601 		    (carp_input(m, (u_int8_t *)&eh->ether_shost,
602 		    (u_int8_t *)&eh->ether_dhost, eh->ether_type) == 0))
603 			return;
604 		/* Always clear multicast flags if received on a carp address */
605 		else if (ifp->if_type == IFT_CARP &&
606 		    ifp->if_flags & IFF_LINK2 &&
607 		    m->m_flags & (M_BCAST|M_MCAST) &&
608 		    !bcmp(((struct arpcom *)ifp)->ac_enaddr,
609 		    (caddr_t)eh->ether_dhost, ETHER_ADDR_LEN))
610 			m->m_flags &= ~(M_BCAST|M_MCAST);
611 	}
612 #endif /* NCARP > 0 */
613 
614 	ac = (struct arpcom *)ifp;
615 
616 	/*
617 	 * If packet has been filtered by the bpf listener, drop it now
618 	 */
619 	if (m->m_flags & M_FILDROP) {
620 		m_free(m);
621 		return;
622 	}
623 
624 	/*
625 	 * If packet is unicast and we're in promiscuous mode, make sure it
626 	 * is for us.  Drop otherwise.
627 	 */
628 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
629 	    (ifp->if_flags & IFF_PROMISC)) {
630 		if (bcmp(ac->ac_enaddr, (caddr_t)eh->ether_dhost,
631 		    ETHER_ADDR_LEN)) {
632 			m_freem(m);
633 			return;
634 		}
635 	}
636 
637 decapsulate:
638 
639 	switch (etype) {
640 #ifdef INET
641 	case ETHERTYPE_IP:
642 		schednetisr(NETISR_IP);
643 		inq = &ipintrq;
644 		break;
645 
646 	case ETHERTYPE_ARP:
647 		if (ifp->if_flags & IFF_NOARP)
648 			goto dropanyway;
649 		schednetisr(NETISR_ARP);
650 		inq = &arpintrq;
651 		break;
652 
653 	case ETHERTYPE_REVARP:
654 		if (ifp->if_flags & IFF_NOARP)
655 			goto dropanyway;
656 		revarpinput(m);	/* XXX queue? */
657 		return;
658 
659 #endif
660 #ifdef INET6
661 	/*
662 	 * Schedule IPv6 software interrupt for incoming IPv6 packet.
663 	 */
664 	case ETHERTYPE_IPV6:
665 		schednetisr(NETISR_IPV6);
666 		inq = &ip6intrq;
667 		break;
668 #endif /* INET6 */
669 #ifdef IPX
670 	case ETHERTYPE_IPX:
671 		schednetisr(NETISR_IPX);
672 		inq = &ipxintrq;
673 		break;
674 #endif
675 #ifdef NETATALK
676 	case ETHERTYPE_AT:
677 		schednetisr(NETISR_ATALK);
678 		inq = &atintrq1;
679 		break;
680 	case ETHERTYPE_AARP:
681 		/* probably this should be done with a NETISR as well */
682 		/* XXX queue this */
683 		aarpinput((struct arpcom *)ifp, m);
684 		return;
685 #endif
686 #if NPPPOE > 0
687 	case ETHERTYPE_PPPOEDISC:
688 	case ETHERTYPE_PPPOE:
689 		/* XXX we dont have this flag */
690 		/*
691 		if (m->m_flags & M_PROMISC) {
692 			m_freem(m);
693 			return;
694 		}
695 		*/
696 #ifndef PPPOE_SERVER
697 		if (m->m_flags & (M_MCAST | M_BCAST)) {
698 			m_freem(m);
699 			return;
700 		}
701 #endif
702 		M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
703 		if (m == NULL)
704 			return;
705 
706 		eh_tmp = mtod(m, struct ether_header *);
707 		bcopy(eh, eh_tmp, sizeof(struct ether_header));
708 
709 		if (etype == ETHERTYPE_PPPOEDISC)
710 			inq = &ppoediscinq;
711 		else
712 			inq = &ppoeinq;
713 
714 		schednetisr(NETISR_PPPOE);
715 		break;
716 #endif /* NPPPOE > 0 */
717 	default:
718 		if (llcfound || etype > ETHERMTU)
719 			goto dropanyway;
720 		llcfound = 1;
721 		l = mtod(m, struct llc *);
722 		switch (l->llc_dsap) {
723 		case LLC_SNAP_LSAP:
724 #ifdef NETATALK
725 			/*
726 			 * Some protocols (like Appletalk) need special
727 			 * handling depending on if they are type II
728 			 * or SNAP encapsulated. Everything else
729 			 * gets handled by stripping off the SNAP header
730 			 * and going back up to decapsulate.
731 			 */
732 			if (l->llc_control == LLC_UI &&
733 			    l->llc_ssap == LLC_SNAP_LSAP &&
734 			    Bcmp(&(l->llc_snap.org_code)[0],
735 			    at_org_code, sizeof(at_org_code)) == 0 &&
736 			    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
737 				inq = &atintrq2;
738 				m_adj(m, AT_LLC_SIZE);
739 				schednetisr(NETISR_ATALK);
740 				break;
741 			}
742 
743 			if (l->llc_control == LLC_UI &&
744 			    l->llc_ssap == LLC_SNAP_LSAP &&
745 			    Bcmp(&(l->llc_snap.org_code)[0],
746 			    aarp_org_code, sizeof(aarp_org_code)) == 0 &&
747 			    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
748 				m_adj(m, AT_LLC_SIZE);
749 				/* XXX Really this should use netisr too */
750 				aarpinput((struct arpcom *)ifp, m);
751 				return;
752 			}
753 #endif /* NETATALK */
754 			if (l->llc_control == LLC_UI &&
755 			    l->llc_dsap == LLC_SNAP_LSAP &&
756 			    l->llc_ssap == LLC_SNAP_LSAP) {
757 				/* SNAP */
758 				if (m->m_pkthdr.len > etype)
759 					m_adj(m, etype - m->m_pkthdr.len);
760 				m->m_data += 6;		/* XXX */
761 				m->m_len -= 6;		/* XXX */
762 				m->m_pkthdr.len -= 6;	/* XXX */
763 				M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
764 				if (m == 0)
765 					return;
766 				*mtod(m, struct ether_header *) = *eh;
767 				goto decapsulate;
768 			}
769 			goto dropanyway;
770 		dropanyway:
771 		default:
772 			m_freem(m);
773 			return;
774 		}
775 	}
776 
777 	s = splnet();
778 	IF_INPUT_ENQUEUE(inq, m);
779 	splx(s);
780 }
781 
782 /*
783  * Convert Ethernet address to printable (loggable) representation.
784  */
785 static char digits[] = "0123456789abcdef";
786 char *
787 ether_sprintf(ap)
788 	u_char *ap;
789 {
790 	int i;
791 	static char etherbuf[ETHER_ADDR_LEN * 3];
792 	char *cp = etherbuf;
793 
794 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
795 		*cp++ = digits[*ap >> 4];
796 		*cp++ = digits[*ap++ & 0xf];
797 		*cp++ = ':';
798 	}
799 	*--cp = 0;
800 	return (etherbuf);
801 }
802 
803 /*
804  * Perform common duties while attaching to interface list
805  */
806 void
807 ether_ifattach(ifp)
808 	struct ifnet *ifp;
809 {
810 
811 	/*
812 	 * Any interface which provides a MAC address which is obviously
813 	 * invalid gets whacked, so that users will notice.
814 	 */
815 	if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) {
816 		((struct arpcom *)ifp)->ac_enaddr[0] = 0x00;
817 		((struct arpcom *)ifp)->ac_enaddr[1] = 0xfe;
818 		((struct arpcom *)ifp)->ac_enaddr[2] = 0xe1;
819 		((struct arpcom *)ifp)->ac_enaddr[3] = 0xba;
820 		((struct arpcom *)ifp)->ac_enaddr[4] = 0xd0;
821 		/*
822 		 * XXX use of random() by anything except the scheduler is
823 		 * normally invalid, but this is boot time, so pre-scheduler,
824 		 * and the random subsystem is not alive yet
825 		 */
826 		((struct arpcom *)ifp)->ac_enaddr[5] = (u_char)random() & 0xff;
827 	}
828 
829 	ifp->if_type = IFT_ETHER;
830 	ifp->if_addrlen = ETHER_ADDR_LEN;
831 	ifp->if_hdrlen = ETHER_HDR_LEN;
832 	ifp->if_mtu = ETHERMTU;
833 	ifp->if_output = ether_output;
834 
835 	if (ifp->if_hardmtu == 0)
836 		ifp->if_hardmtu = ETHERMTU;
837 
838 	if_alloc_sadl(ifp);
839 	bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
840 	    LLADDR(ifp->if_sadl), ifp->if_addrlen);
841 	LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
842 #if NBPFILTER > 0
843 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN);
844 #endif
845 }
846 
847 void
848 ether_ifdetach(ifp)
849 	struct ifnet *ifp;
850 {
851 	struct arpcom *ac = (struct arpcom *)ifp;
852 	struct ether_multi *enm;
853 
854 	for (enm = LIST_FIRST(&ac->ac_multiaddrs);
855 	    enm != LIST_END(&ac->ac_multiaddrs);
856 	    enm = LIST_FIRST(&ac->ac_multiaddrs)) {
857 		LIST_REMOVE(enm, enm_list);
858 		free(enm, M_IFMADDR);
859 	}
860 
861 #if 0
862 	/* moved to if_detach() */
863 	if_free_sadl(ifp);
864 #endif
865 }
866 
867 #if 0
868 /*
869  * This is for reference.  We have table-driven versions of the
870  * crc32 generators, which are faster than the double-loop.
871  */
872 u_int32_t
873 ether_crc32_le(const u_int8_t *buf, size_t len)
874 {
875 	u_int32_t c, crc, carry;
876 	size_t i, j;
877 
878 	crc = 0xffffffffU;	/* initial value */
879 
880 	for (i = 0; i < len; i++) {
881 		c = buf[i];
882 		for (j = 0; j < 8; j++) {
883 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
884 			crc >>= 1;
885 			c >>= 1;
886 			if (carry)
887 				crc = (crc ^ ETHER_CRC_POLY_LE);
888 		}
889 	}
890 
891 	return (crc);
892 }
893 
894 u_int32_t
895 ether_crc32_be(const u_int8_t *buf, size_t len)
896 {
897 	u_int32_t c, crc, carry;
898 	size_t i, j;
899 
900 	crc = 0xffffffffU;	/* initial value */
901 
902 	for (i = 0; i < len; i++) {
903 		c = buf[i];
904 		for (j = 0; j < 8; j++) {
905 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
906 			crc <<= 1;
907 			c >>= 1;
908 			if (carry)
909 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
910 		}
911 	}
912 
913 	return (crc);
914 }
915 #else
916 u_int32_t
917 ether_crc32_le(const u_int8_t *buf, size_t len)
918 {
919 	static const u_int32_t crctab[] = {
920 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
921 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
922 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
923 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
924 	};
925 	size_t i;
926 	u_int32_t crc;
927 
928 	crc = 0xffffffffU;	/* initial value */
929 
930 	for (i = 0; i < len; i++) {
931 		crc ^= buf[i];
932 		crc = (crc >> 4) ^ crctab[crc & 0xf];
933 		crc = (crc >> 4) ^ crctab[crc & 0xf];
934 	}
935 
936 	return (crc);
937 }
938 
939 u_int32_t
940 ether_crc32_be(const u_int8_t *buf, size_t len)
941 {
942 	static const u_int8_t rev[] = {
943 		0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
944 		0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
945 	};
946 	static const u_int32_t crctab[] = {
947 		0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
948 		0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
949 		0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
950 		0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd
951 	};
952 	size_t i;
953 	u_int32_t crc;
954 	u_int8_t data;
955 
956 	crc = 0xffffffffU;	/* initial value */
957 	for (i = 0; i < len; i++) {
958 		data = buf[i];
959 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]];
960 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]];
961 	}
962 
963 	return (crc);
964 }
965 #endif
966 
967 #ifdef INET
968 u_char	ether_ipmulticast_min[ETHER_ADDR_LEN] =
969     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
970 u_char	ether_ipmulticast_max[ETHER_ADDR_LEN] =
971     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
972 #endif
973 
974 #ifdef INET6
975 u_char	ether_ip6multicast_min[ETHER_ADDR_LEN] =
976     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
977 u_char	ether_ip6multicast_max[ETHER_ADDR_LEN] =
978     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
979 #endif
980 
981 /*
982  * Convert a sockaddr into an Ethernet address or range of Ethernet
983  * addresses.
984  */
985 int
986 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
987     u_int8_t addrhi[ETHER_ADDR_LEN])
988 {
989 #ifdef INET
990 	struct sockaddr_in *sin;
991 #endif /* INET */
992 #ifdef INET6
993 	struct sockaddr_in6 *sin6;
994 #endif /* INET6 */
995 
996 	switch (sa->sa_family) {
997 
998 	case AF_UNSPEC:
999 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
1000 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1001 		break;
1002 
1003 #ifdef INET
1004 	case AF_INET:
1005 		sin = satosin(sa);
1006 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1007 			/*
1008 			 * An IP address of INADDR_ANY means listen to
1009 			 * or stop listening to all of the Ethernet
1010 			 * multicast addresses used for IP.
1011 			 * (This is for the sake of IP multicast routers.)
1012 			 */
1013 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
1014 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
1015 		} else {
1016 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1017 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1018 		}
1019 		break;
1020 #endif
1021 #ifdef INET6
1022 	case AF_INET6:
1023 		sin6 = satosin6(sa);
1024 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1025 			/*
1026 			 * An IP6 address of 0 means listen to or stop
1027 			 * listening to all of the Ethernet multicast
1028 			 * address used for IP6.
1029 			 *
1030 			 * (This might not be healthy, given IPv6's reliance on
1031 			 * multicast for things like neighbor discovery.
1032 			 * Perhaps initializing all-nodes, solicited nodes, and
1033 			 * possibly all-routers for this interface afterwards
1034 			 * is not a bad idea.)
1035 			 */
1036 
1037 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1038 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1039 		} else {
1040 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1041 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1042 		}
1043 		break;
1044 #endif
1045 
1046 	default:
1047 		return (EAFNOSUPPORT);
1048 	}
1049 	return (0);
1050 }
1051 
1052 /*
1053  * Add an Ethernet multicast address or range of addresses to the list for a
1054  * given interface.
1055  */
1056 int
1057 ether_addmulti(ifr, ac)
1058 	struct ifreq *ifr;
1059 	struct arpcom *ac;
1060 {
1061 	struct ether_multi *enm;
1062 	u_char addrlo[ETHER_ADDR_LEN];
1063 	u_char addrhi[ETHER_ADDR_LEN];
1064 	int s = splnet(), error;
1065 
1066 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1067 	if (error != 0) {
1068 		splx(s);
1069 		return (error);
1070 	}
1071 
1072 	/*
1073 	 * Verify that we have valid Ethernet multicast addresses.
1074 	 */
1075 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1076 		splx(s);
1077 		return (EINVAL);
1078 	}
1079 	/*
1080 	 * See if the address range is already in the list.
1081 	 */
1082 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
1083 	if (enm != NULL) {
1084 		/*
1085 		 * Found it; just increment the reference count.
1086 		 */
1087 		++enm->enm_refcount;
1088 		splx(s);
1089 		return (0);
1090 	}
1091 	/*
1092 	 * New address or range; malloc a new multicast record
1093 	 * and link it into the interface's multicast list.
1094 	 */
1095 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1096 	if (enm == NULL) {
1097 		splx(s);
1098 		return (ENOBUFS);
1099 	}
1100 	bcopy(addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1101 	bcopy(addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1102 	enm->enm_ac = ac;
1103 	enm->enm_refcount = 1;
1104 	LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
1105 	ac->ac_multicnt++;
1106 	splx(s);
1107 	/*
1108 	 * Return ENETRESET to inform the driver that the list has changed
1109 	 * and its reception filter should be adjusted accordingly.
1110 	 */
1111 	return (ENETRESET);
1112 }
1113 
1114 /*
1115  * Delete a multicast address record.
1116  */
1117 int
1118 ether_delmulti(ifr, ac)
1119 	struct ifreq *ifr;
1120 	struct arpcom *ac;
1121 {
1122 	struct ether_multi *enm;
1123 	u_char addrlo[ETHER_ADDR_LEN];
1124 	u_char addrhi[ETHER_ADDR_LEN];
1125 	int s = splnet(), error;
1126 
1127 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1128 	if (error != 0) {
1129 		splx(s);
1130 		return (error);
1131 	}
1132 
1133 	/*
1134 	 * Look up the address in our list.
1135 	 */
1136 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
1137 	if (enm == NULL) {
1138 		splx(s);
1139 		return (ENXIO);
1140 	}
1141 	if (--enm->enm_refcount != 0) {
1142 		/*
1143 		 * Still some claims to this record.
1144 		 */
1145 		splx(s);
1146 		return (0);
1147 	}
1148 	/*
1149 	 * No remaining claims to this record; unlink and free it.
1150 	 */
1151 	LIST_REMOVE(enm, enm_list);
1152 	free(enm, M_IFMADDR);
1153 	ac->ac_multicnt--;
1154 	splx(s);
1155 	/*
1156 	 * Return ENETRESET to inform the driver that the list has changed
1157 	 * and its reception filter should be adjusted accordingly.
1158 	 */
1159 	return (ENETRESET);
1160 }
1161