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