xref: /openbsd-src/sys/net/if_ethersubr.c (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
1 /*	$OpenBSD: if_ethersubr.c,v 1.276 2021/08/19 10:22:00 dlg 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 #include <sys/smr.h>
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 #include <netinet/if_ether.h>
101 #include <netinet/ip_ipsp.h>
102 
103 #if NBPFILTER > 0
104 #include <net/bpf.h>
105 #endif
106 
107 #include "vlan.h"
108 #if NVLAN > 0
109 #include <net/if_vlan_var.h>
110 #endif
111 
112 #include "carp.h"
113 #if NCARP > 0
114 #include <netinet/ip_carp.h>
115 #endif
116 
117 #include "pppoe.h"
118 #if NPPPOE > 0
119 #include <net/if_pppoe.h>
120 #endif
121 
122 #include "bpe.h"
123 #if NBPE > 0
124 #include <net/if_bpe.h>
125 #endif
126 
127 #ifdef INET6
128 #include <netinet6/in6_var.h>
129 #include <netinet6/nd6.h>
130 #endif
131 
132 #ifdef PIPEX
133 #include <net/pipex.h>
134 #endif
135 
136 #ifdef MPLS
137 #include <netmpls/mpls.h>
138 #endif /* MPLS */
139 
140 u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
141     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
142 u_int8_t etheranyaddr[ETHER_ADDR_LEN] =
143     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
144 #define senderr(e) { error = (e); goto bad;}
145 
146 int
147 ether_ioctl(struct ifnet *ifp, struct arpcom *arp, u_long cmd, caddr_t data)
148 {
149 	struct ifreq *ifr = (struct ifreq *)data;
150 	int error = 0;
151 
152 	switch (cmd) {
153 	case SIOCSIFADDR:
154 		break;
155 
156 	case SIOCSIFMTU:
157 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
158 			error = EINVAL;
159 		else
160 			ifp->if_mtu = ifr->ifr_mtu;
161 		break;
162 
163 	case SIOCADDMULTI:
164 	case SIOCDELMULTI:
165 		if (ifp->if_flags & IFF_MULTICAST) {
166 			error = (cmd == SIOCADDMULTI) ?
167 			    ether_addmulti(ifr, arp) :
168 			    ether_delmulti(ifr, arp);
169 		} else
170 			error = ENOTTY;
171 		break;
172 
173 	default:
174 		error = ENOTTY;
175 	}
176 
177 	return (error);
178 }
179 
180 
181 void
182 ether_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
183 {
184 	if (rt == NULL)
185 		return;
186 
187 	switch (rt_key(rt)->sa_family) {
188 	case AF_INET:
189 		arp_rtrequest(ifp, req, rt);
190 		break;
191 #ifdef INET6
192 	case AF_INET6:
193 		nd6_rtrequest(ifp, req, rt);
194 		break;
195 #endif
196 	default:
197 		break;
198 	}
199 }
200 
201 int
202 ether_resolve(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
203     struct rtentry *rt, struct ether_header *eh)
204 {
205 	struct arpcom *ac = (struct arpcom *)ifp;
206 	sa_family_t af = dst->sa_family;
207 	int error = 0;
208 
209 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
210 		senderr(ENETDOWN);
211 
212 	KASSERT(rt != NULL || ISSET(m->m_flags, M_MCAST|M_BCAST) ||
213 		af == AF_UNSPEC || af == pseudo_AF_HDRCMPLT);
214 
215 #ifdef DIAGNOSTIC
216 	if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid)) {
217 		printf("%s: trying to send packet on wrong domain. "
218 		    "if %d vs. mbuf %d\n", ifp->if_xname,
219 		    ifp->if_rdomain, rtable_l2(m->m_pkthdr.ph_rtableid));
220 	}
221 #endif
222 
223 	switch (af) {
224 	case AF_INET:
225 		error = arpresolve(ifp, rt, m, dst, eh->ether_dhost);
226 		if (error)
227 			return (error);
228 		eh->ether_type = htons(ETHERTYPE_IP);
229 
230 		/*
231 		 * If broadcasting on a simplex interface, loopback a copy.
232 		 * The checksum must be calculated in software.  Keep the
233 		 * condition in sync with in_ifcap_cksum().
234 		 */
235 		if (ISSET(m->m_flags, M_BCAST) &&
236 		    ISSET(ifp->if_flags, IFF_SIMPLEX) &&
237 		    !m->m_pkthdr.pf.routed) {
238 			struct mbuf *mcopy;
239 
240 			/* XXX Should we input an unencrypted IPsec packet? */
241 			mcopy = m_copym(m, 0, M_COPYALL, M_NOWAIT);
242 			if (mcopy != NULL)
243 				if_input_local(ifp, mcopy, af);
244 		}
245 		break;
246 #ifdef INET6
247 	case AF_INET6:
248 		error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost);
249 		if (error)
250 			return (error);
251 		eh->ether_type = htons(ETHERTYPE_IPV6);
252 		break;
253 #endif
254 #ifdef MPLS
255 	case AF_MPLS:
256 		if (rt == NULL)
257 			senderr(EHOSTUNREACH);
258 
259 		if (!ISSET(ifp->if_xflags, IFXF_MPLS))
260 			senderr(ENETUNREACH);
261 
262 		dst = ISSET(rt->rt_flags, RTF_GATEWAY) ?
263 		    rt->rt_gateway : rt_key(rt);
264 
265 		switch (dst->sa_family) {
266 		case AF_LINK:
267 			if (satosdl(dst)->sdl_alen < sizeof(eh->ether_dhost))
268 				senderr(EHOSTUNREACH);
269 			memcpy(eh->ether_dhost, LLADDR(satosdl(dst)),
270 			    sizeof(eh->ether_dhost));
271 			break;
272 #ifdef INET6
273 		case AF_INET6:
274 			error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost);
275 			if (error)
276 				return (error);
277 			break;
278 #endif
279 		case AF_INET:
280 			error = arpresolve(ifp, rt, m, dst, eh->ether_dhost);
281 			if (error)
282 				return (error);
283 			break;
284 		default:
285 			senderr(EHOSTUNREACH);
286 		}
287 		/* XXX handling for simplex devices in case of M/BCAST ?? */
288 		if (m->m_flags & (M_BCAST | M_MCAST))
289 			eh->ether_type = htons(ETHERTYPE_MPLS_MCAST);
290 		else
291 			eh->ether_type = htons(ETHERTYPE_MPLS);
292 		break;
293 #endif /* MPLS */
294 	case pseudo_AF_HDRCMPLT:
295 		/* take the whole header from the sa */
296 		memcpy(eh, dst->sa_data, sizeof(*eh));
297 		return (0);
298 
299 	case AF_UNSPEC:
300 		/* take the dst and type from the sa, but get src below */
301 		memcpy(eh, dst->sa_data, sizeof(*eh));
302 		break;
303 
304 	default:
305 		printf("%s: can't handle af%d\n", ifp->if_xname, af);
306 		senderr(EAFNOSUPPORT);
307 	}
308 
309 	memcpy(eh->ether_shost, ac->ac_enaddr, sizeof(eh->ether_shost));
310 
311 	return (0);
312 
313 bad:
314 	m_freem(m);
315 	return (error);
316 }
317 
318 struct mbuf*
319 ether_encap(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
320     struct rtentry *rt, int *errorp)
321 {
322 	struct ether_header eh;
323 	int error;
324 
325 	error = ether_resolve(ifp, m, dst, rt, &eh);
326 	switch (error) {
327 	case 0:
328 		break;
329 	case EAGAIN:
330 		error = 0;
331 	default:
332 		*errorp = error;
333 		return (NULL);
334 	}
335 
336 	m = m_prepend(m, ETHER_ALIGN + sizeof(eh), M_DONTWAIT);
337 	if (m == NULL) {
338 		*errorp = ENOBUFS;
339 		return (NULL);
340 	}
341 
342 	m_adj(m, ETHER_ALIGN);
343 	memcpy(mtod(m, struct ether_header *), &eh, sizeof(eh));
344 
345 	return (m);
346 }
347 
348 int
349 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
350     struct rtentry *rt)
351 {
352 	int error;
353 
354 	m = ether_encap(ifp, m, dst, rt, &error);
355 	if (m == NULL)
356 		return (error);
357 
358 	return (if_enqueue(ifp, m));
359 }
360 
361 /*
362  * Process a received Ethernet packet.
363  *
364  * Ethernet input has several "phases" of filtering packets to
365  * support virtual/pseudo interfaces before actual layer 3 protocol
366  * handling.
367  *
368  * First phase:
369  *
370  * The first phase supports drivers that aggregate multiple Ethernet
371  * ports into a single logical interface, ie, aggr(4) and trunk(4).
372  * These drivers intercept packets by swapping out the if_input handler
373  * on the "port" interfaces to steal the packets before they get here
374  * to ether_input().
375  */
376 void
377 ether_input(struct ifnet *ifp, struct mbuf *m)
378 {
379 	struct ether_header *eh;
380 	void (*input)(struct ifnet *, struct mbuf *);
381 	u_int16_t etype;
382 	struct arpcom *ac;
383 	const struct ether_brport *eb;
384 	unsigned int sdelim = 0;
385 	uint64_t dst, self;
386 
387 	/* Drop short frames */
388 	if (m->m_len < ETHER_HDR_LEN)
389 		goto dropanyway;
390 
391 	/*
392 	 * Second phase: service delimited packet filtering.
393 	 *
394 	 * Let vlan(4) and svlan(4) look at "service delimited"
395 	 * packets. If a virtual interface does not exist to take
396 	 * those packets, they're returned to ether_input() so a
397 	 * bridge can have a go at forwarding them.
398 	 */
399 
400 	eh = mtod(m, struct ether_header *);
401 	dst = ether_addr_to_e64((struct ether_addr *)eh->ether_dhost);
402 	etype = ntohs(eh->ether_type);
403 
404 	if (ISSET(m->m_flags, M_VLANTAG) ||
405 	    etype == ETHERTYPE_VLAN || etype == ETHERTYPE_QINQ) {
406 #if NVLAN > 0
407 		m = vlan_input(ifp, m, &sdelim);
408 		if (m == NULL)
409 			return;
410 #else
411 		sdelim = 1;
412 #endif
413 	}
414 
415 	/*
416 	 * Third phase: bridge processing.
417 	 *
418 	 * Give the packet to a bridge interface, ie, bridge(4),
419 	 * switch(4), or tpmr(4), if it is configured. A bridge
420 	 * may take the packet and forward it to another port, or it
421 	 * may return it here to ether_input() to support local
422 	 * delivery to this port.
423 	 */
424 
425 	ac = (struct arpcom *)ifp;
426 
427 	smr_read_enter();
428 	eb = SMR_PTR_GET(&ac->ac_brport);
429 	if (eb != NULL)
430 		eb->eb_port_take(eb->eb_port);
431 	smr_read_leave();
432 	if (eb != NULL) {
433 		m = (*eb->eb_input)(ifp, m, dst, eb->eb_port);
434 		eb->eb_port_rele(eb->eb_port);
435 		if (m == NULL) {
436 			return;
437 		}
438 	}
439 
440 	/*
441 	 * Fourth phase: drop service delimited packets.
442 	 *
443 	 * If the packet has a tag, and a bridge didn't want it,
444 	 * it's not for this port.
445 	 */
446 
447 	if (sdelim)
448 		goto dropanyway;
449 
450 	/*
451 	 * Fifth phase: destination address check.
452 	 *
453 	 * Is the packet specifically addressed to this port?
454 	 */
455 
456 	eh = mtod(m, struct ether_header *);
457 	self = ether_addr_to_e64((struct ether_addr *)ac->ac_enaddr);
458 	if (dst != self) {
459 #if NCARP > 0
460 		/*
461 		 * If it's not for this port, it could be for carp(4).
462 		 */
463 		if (ifp->if_type != IFT_CARP &&
464 		    !SRPL_EMPTY_LOCKED(&ifp->if_carp)) {
465 			m = carp_input(ifp, m, dst);
466 			if (m == NULL)
467 				return;
468 
469 			eh = mtod(m, struct ether_header *);
470 		}
471 #endif
472 
473 		/*
474 		 * If not, it must be multicast or broadcast to go further.
475 		 */
476 		if (!ETH64_IS_MULTICAST(dst))
477 			goto dropanyway;
478 
479 		/*
480 		 * If this is not a simplex interface, drop the packet
481 		 * if it came from us.
482 		 */
483 		if ((ifp->if_flags & IFF_SIMPLEX) == 0) {
484 			uint64_t src = ether_addr_to_e64(
485 			    (struct ether_addr *)eh->ether_shost);
486 			if (self == src)
487 				goto dropanyway;
488 		}
489 
490 		SET(m->m_flags, ETH64_IS_BROADCAST(dst) ? M_BCAST : M_MCAST);
491 		ifp->if_imcasts++;
492 	}
493 
494 	/*
495 	 * Sixth phase: protocol demux.
496 	 *
497 	 * At this point it is known that the packet is destined
498 	 * for layer 3 protocol handling on the local port.
499 	 */
500 	etype = ntohs(eh->ether_type);
501 
502 	switch (etype) {
503 	case ETHERTYPE_IP:
504 		input = ipv4_input;
505 		break;
506 
507 	case ETHERTYPE_ARP:
508 		if (ifp->if_flags & IFF_NOARP)
509 			goto dropanyway;
510 		input = arpinput;
511 		break;
512 
513 	case ETHERTYPE_REVARP:
514 		if (ifp->if_flags & IFF_NOARP)
515 			goto dropanyway;
516 		input = revarpinput;
517 		break;
518 
519 #ifdef INET6
520 	/*
521 	 * Schedule IPv6 software interrupt for incoming IPv6 packet.
522 	 */
523 	case ETHERTYPE_IPV6:
524 		input = ipv6_input;
525 		break;
526 #endif /* INET6 */
527 #if NPPPOE > 0 || defined(PIPEX)
528 	case ETHERTYPE_PPPOEDISC:
529 	case ETHERTYPE_PPPOE:
530 		if (m->m_flags & (M_MCAST | M_BCAST))
531 			goto dropanyway;
532 #ifdef PIPEX
533 		if (pipex_enable) {
534 			struct pipex_session *session;
535 
536 			if ((session = pipex_pppoe_lookup_session(m)) != NULL) {
537 				pipex_pppoe_input(m, session);
538 				return;
539 			}
540 		}
541 #endif
542 		if (etype == ETHERTYPE_PPPOEDISC)
543 			pppoe_disc_input(m);
544 		else
545 			pppoe_data_input(m);
546 		return;
547 #endif
548 #ifdef MPLS
549 	case ETHERTYPE_MPLS:
550 	case ETHERTYPE_MPLS_MCAST:
551 		input = mpls_input;
552 		break;
553 #endif
554 #if NBPE > 0
555 	case ETHERTYPE_PBB:
556 		bpe_input(ifp, m);
557 		return;
558 #endif
559 	default:
560 		goto dropanyway;
561 	}
562 
563 	m_adj(m, sizeof(*eh));
564 	(*input)(ifp, m);
565 	return;
566 dropanyway:
567 	m_freem(m);
568 	return;
569 }
570 
571 int
572 ether_brport_isset(struct ifnet *ifp)
573 {
574 	struct arpcom *ac = (struct arpcom *)ifp;
575 
576 	KERNEL_ASSERT_LOCKED();
577 	if (SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL)
578 		return (EBUSY);
579 
580 	return (0);
581 }
582 
583 void
584 ether_brport_set(struct ifnet *ifp, const struct ether_brport *eb)
585 {
586 	struct arpcom *ac = (struct arpcom *)ifp;
587 
588 	KERNEL_ASSERT_LOCKED();
589 	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) == NULL,
590 	    "%s setting an already set brport", ifp->if_xname);
591 
592 	SMR_PTR_SET_LOCKED(&ac->ac_brport, eb);
593 }
594 
595 void
596 ether_brport_clr(struct ifnet *ifp)
597 {
598 	struct arpcom *ac = (struct arpcom *)ifp;
599 
600 	KERNEL_ASSERT_LOCKED();
601 	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL,
602 	    "%s clearing an already clear brport", ifp->if_xname);
603 
604 	SMR_PTR_SET_LOCKED(&ac->ac_brport, NULL);
605 }
606 
607 const struct ether_brport *
608 ether_brport_get(struct ifnet *ifp)
609 {
610 	struct arpcom *ac = (struct arpcom *)ifp;
611 	SMR_ASSERT_CRITICAL();
612 	return (SMR_PTR_GET(&ac->ac_brport));
613 }
614 
615 const struct ether_brport *
616 ether_brport_get_locked(struct ifnet *ifp)
617 {
618 	struct arpcom *ac = (struct arpcom *)ifp;
619 	KERNEL_ASSERT_LOCKED();
620 	return (SMR_PTR_GET_LOCKED(&ac->ac_brport));
621 }
622 
623 /*
624  * Convert Ethernet address to printable (loggable) representation.
625  */
626 static char digits[] = "0123456789abcdef";
627 char *
628 ether_sprintf(u_char *ap)
629 {
630 	int i;
631 	static char etherbuf[ETHER_ADDR_LEN * 3];
632 	char *cp = etherbuf;
633 
634 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
635 		*cp++ = digits[*ap >> 4];
636 		*cp++ = digits[*ap++ & 0xf];
637 		*cp++ = ':';
638 	}
639 	*--cp = 0;
640 	return (etherbuf);
641 }
642 
643 /*
644  * Generate a (hopefully) acceptable MAC address, if asked.
645  */
646 void
647 ether_fakeaddr(struct ifnet *ifp)
648 {
649 	static int unit;
650 	int rng = arc4random();
651 
652 	/* Non-multicast; locally administered address */
653 	((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe;
654 	((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1;
655 	((struct arpcom *)ifp)->ac_enaddr[2] = 0xba;
656 	((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf);
657 	((struct arpcom *)ifp)->ac_enaddr[4] = rng;
658 	((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8;
659 }
660 
661 /*
662  * Perform common duties while attaching to interface list
663  */
664 void
665 ether_ifattach(struct ifnet *ifp)
666 {
667 	struct arpcom *ac = (struct arpcom *)ifp;
668 
669 	/*
670 	 * Any interface which provides a MAC address which is obviously
671 	 * invalid gets whacked, so that users will notice.
672 	 */
673 	if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr))
674 		ether_fakeaddr(ifp);
675 
676 	ifp->if_type = IFT_ETHER;
677 	ifp->if_addrlen = ETHER_ADDR_LEN;
678 	ifp->if_hdrlen = ETHER_HDR_LEN;
679 	ifp->if_mtu = ETHERMTU;
680 	ifp->if_input = ether_input;
681 	if (ifp->if_output == NULL)
682 		ifp->if_output = ether_output;
683 	ifp->if_rtrequest = ether_rtrequest;
684 
685 	if (ifp->if_hardmtu == 0)
686 		ifp->if_hardmtu = ETHERMTU;
687 
688 	if_alloc_sadl(ifp);
689 	memcpy(LLADDR(ifp->if_sadl), ac->ac_enaddr, ifp->if_addrlen);
690 	LIST_INIT(&ac->ac_multiaddrs);
691 #if NBPFILTER > 0
692 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN);
693 #endif
694 }
695 
696 void
697 ether_ifdetach(struct ifnet *ifp)
698 {
699 	struct arpcom *ac = (struct arpcom *)ifp;
700 	struct ether_multi *enm;
701 
702 	/* Undo pseudo-driver changes. */
703 	if_deactivate(ifp);
704 
705 	for (enm = LIST_FIRST(&ac->ac_multiaddrs);
706 	    enm != NULL;
707 	    enm = LIST_FIRST(&ac->ac_multiaddrs)) {
708 		LIST_REMOVE(enm, enm_list);
709 		free(enm, M_IFMADDR, sizeof *enm);
710 	}
711 }
712 
713 #if 0
714 /*
715  * This is for reference.  We have table-driven versions of the
716  * crc32 generators, which are faster than the double-loop.
717  */
718 u_int32_t __pure
719 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
720 {
721 	u_int32_t c, carry;
722 	size_t i, j;
723 
724 	for (i = 0; i < len; i++) {
725 		c = buf[i];
726 		for (j = 0; j < 8; j++) {
727 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
728 			crc >>= 1;
729 			c >>= 1;
730 			if (carry)
731 				crc = (crc ^ ETHER_CRC_POLY_LE);
732 		}
733 	}
734 
735 	return (crc);
736 }
737 
738 u_int32_t __pure
739 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
740 {
741 	u_int32_t c, carry;
742 	size_t i, j;
743 
744 	for (i = 0; i < len; i++) {
745 		c = buf[i];
746 		for (j = 0; j < 8; j++) {
747 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
748 			crc <<= 1;
749 			c >>= 1;
750 			if (carry)
751 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
752 		}
753 	}
754 
755 	return (crc);
756 }
757 #else
758 u_int32_t __pure
759 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len)
760 {
761 	static const u_int32_t crctab[] = {
762 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
763 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
764 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
765 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
766 	};
767 	size_t i;
768 
769 	for (i = 0; i < len; i++) {
770 		crc ^= buf[i];
771 		crc = (crc >> 4) ^ crctab[crc & 0xf];
772 		crc = (crc >> 4) ^ crctab[crc & 0xf];
773 	}
774 
775 	return (crc);
776 }
777 
778 u_int32_t __pure
779 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len)
780 {
781 	static const u_int8_t rev[] = {
782 		0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
783 		0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
784 	};
785 	static const u_int32_t crctab[] = {
786 		0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
787 		0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
788 		0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
789 		0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd
790 	};
791 	size_t i;
792 	u_int8_t data;
793 
794 	for (i = 0; i < len; i++) {
795 		data = buf[i];
796 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]];
797 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]];
798 	}
799 
800 	return (crc);
801 }
802 #endif
803 
804 u_int32_t
805 ether_crc32_le(const u_int8_t *buf, size_t len)
806 {
807 	return ether_crc32_le_update(0xffffffff, buf, len);
808 }
809 
810 u_int32_t
811 ether_crc32_be(const u_int8_t *buf, size_t len)
812 {
813 	return ether_crc32_be_update(0xffffffff, buf, len);
814 }
815 
816 u_char	ether_ipmulticast_min[ETHER_ADDR_LEN] =
817     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
818 u_char	ether_ipmulticast_max[ETHER_ADDR_LEN] =
819     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
820 
821 #ifdef INET6
822 u_char	ether_ip6multicast_min[ETHER_ADDR_LEN] =
823     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
824 u_char	ether_ip6multicast_max[ETHER_ADDR_LEN] =
825     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
826 #endif
827 
828 /*
829  * Convert a sockaddr into an Ethernet address or range of Ethernet
830  * addresses.
831  */
832 int
833 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
834     u_int8_t addrhi[ETHER_ADDR_LEN])
835 {
836 	struct sockaddr_in *sin;
837 #ifdef INET6
838 	struct sockaddr_in6 *sin6;
839 #endif /* INET6 */
840 
841 	switch (sa->sa_family) {
842 
843 	case AF_UNSPEC:
844 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
845 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
846 		break;
847 
848 	case AF_INET:
849 		sin = satosin(sa);
850 		if (sin->sin_addr.s_addr == INADDR_ANY) {
851 			/*
852 			 * An IP address of INADDR_ANY means listen to
853 			 * or stop listening to all of the Ethernet
854 			 * multicast addresses used for IP.
855 			 * (This is for the sake of IP multicast routers.)
856 			 */
857 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
858 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
859 		} else {
860 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
861 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
862 		}
863 		break;
864 #ifdef INET6
865 	case AF_INET6:
866 		sin6 = satosin6(sa);
867 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
868 			/*
869 			 * An IP6 address of 0 means listen to or stop
870 			 * listening to all of the Ethernet multicast
871 			 * address used for IP6.
872 			 *
873 			 * (This might not be healthy, given IPv6's reliance on
874 			 * multicast for things like neighbor discovery.
875 			 * Perhaps initializing all-nodes, solicited nodes, and
876 			 * possibly all-routers for this interface afterwards
877 			 * is not a bad idea.)
878 			 */
879 
880 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
881 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
882 		} else {
883 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
884 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
885 		}
886 		break;
887 #endif
888 
889 	default:
890 		return (EAFNOSUPPORT);
891 	}
892 	return (0);
893 }
894 
895 /*
896  * Add an Ethernet multicast address or range of addresses to the list for a
897  * given interface.
898  */
899 int
900 ether_addmulti(struct ifreq *ifr, struct arpcom *ac)
901 {
902 	struct ether_multi *enm;
903 	u_char addrlo[ETHER_ADDR_LEN];
904 	u_char addrhi[ETHER_ADDR_LEN];
905 	int s = splnet(), error;
906 
907 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
908 	if (error != 0) {
909 		splx(s);
910 		return (error);
911 	}
912 
913 	/*
914 	 * Verify that we have valid Ethernet multicast addresses.
915 	 */
916 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
917 		splx(s);
918 		return (EINVAL);
919 	}
920 	/*
921 	 * See if the address range is already in the list.
922 	 */
923 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
924 	if (enm != NULL) {
925 		/*
926 		 * Found it; just increment the reference count.
927 		 */
928 		++enm->enm_refcount;
929 		splx(s);
930 		return (0);
931 	}
932 	/*
933 	 * New address or range; malloc a new multicast record
934 	 * and link it into the interface's multicast list.
935 	 */
936 	enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
937 	if (enm == NULL) {
938 		splx(s);
939 		return (ENOBUFS);
940 	}
941 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
942 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
943 	enm->enm_refcount = 1;
944 	LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
945 	ac->ac_multicnt++;
946 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
947 		ac->ac_multirangecnt++;
948 	splx(s);
949 	/*
950 	 * Return ENETRESET to inform the driver that the list has changed
951 	 * and its reception filter should be adjusted accordingly.
952 	 */
953 	return (ENETRESET);
954 }
955 
956 /*
957  * Delete a multicast address record.
958  */
959 int
960 ether_delmulti(struct ifreq *ifr, struct arpcom *ac)
961 {
962 	struct ether_multi *enm;
963 	u_char addrlo[ETHER_ADDR_LEN];
964 	u_char addrhi[ETHER_ADDR_LEN];
965 	int s = splnet(), error;
966 
967 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
968 	if (error != 0) {
969 		splx(s);
970 		return (error);
971 	}
972 
973 	/*
974 	 * Look up the address in our list.
975 	 */
976 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
977 	if (enm == NULL) {
978 		splx(s);
979 		return (ENXIO);
980 	}
981 	if (--enm->enm_refcount != 0) {
982 		/*
983 		 * Still some claims to this record.
984 		 */
985 		splx(s);
986 		return (0);
987 	}
988 	/*
989 	 * No remaining claims to this record; unlink and free it.
990 	 */
991 	LIST_REMOVE(enm, enm_list);
992 	free(enm, M_IFMADDR, sizeof *enm);
993 	ac->ac_multicnt--;
994 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
995 		ac->ac_multirangecnt--;
996 	splx(s);
997 	/*
998 	 * Return ENETRESET to inform the driver that the list has changed
999 	 * and its reception filter should be adjusted accordingly.
1000 	 */
1001 	return (ENETRESET);
1002 }
1003 
1004 uint64_t
1005 ether_addr_to_e64(const struct ether_addr *ea)
1006 {
1007 	uint64_t e64 = 0;
1008 	size_t i;
1009 
1010 	for (i = 0; i < nitems(ea->ether_addr_octet); i++) {
1011 		e64 <<= 8;
1012 		e64 |= ea->ether_addr_octet[i];
1013 	}
1014 
1015 	return (e64);
1016 }
1017 
1018 void
1019 ether_e64_to_addr(struct ether_addr *ea, uint64_t e64)
1020 {
1021 	size_t i = nitems(ea->ether_addr_octet);
1022 
1023 	do {
1024 		ea->ether_addr_octet[--i] = e64;
1025 		e64 >>= 8;
1026 	} while (i > 0);
1027 }
1028