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