xref: /openbsd-src/sys/net/if_ethersubr.c (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
1 /*	$OpenBSD: if_ethersubr.c,v 1.275 2021/07/07 20:19:01 sashan 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);
408 		if (m == NULL)
409 			return;
410 #endif /* NVLAN > 0 */
411 
412 		sdelim = 1;
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 
501 	switch (etype) {
502 	case ETHERTYPE_IP:
503 		input = ipv4_input;
504 		break;
505 
506 	case ETHERTYPE_ARP:
507 		if (ifp->if_flags & IFF_NOARP)
508 			goto dropanyway;
509 		input = arpinput;
510 		break;
511 
512 	case ETHERTYPE_REVARP:
513 		if (ifp->if_flags & IFF_NOARP)
514 			goto dropanyway;
515 		input = revarpinput;
516 		break;
517 
518 #ifdef INET6
519 	/*
520 	 * Schedule IPv6 software interrupt for incoming IPv6 packet.
521 	 */
522 	case ETHERTYPE_IPV6:
523 		input = ipv6_input;
524 		break;
525 #endif /* INET6 */
526 #if NPPPOE > 0 || defined(PIPEX)
527 	case ETHERTYPE_PPPOEDISC:
528 	case ETHERTYPE_PPPOE:
529 		if (m->m_flags & (M_MCAST | M_BCAST))
530 			goto dropanyway;
531 #ifdef PIPEX
532 		if (pipex_enable) {
533 			struct pipex_session *session;
534 
535 			if ((session = pipex_pppoe_lookup_session(m)) != NULL) {
536 				pipex_pppoe_input(m, session);
537 				return;
538 			}
539 		}
540 #endif
541 		if (etype == ETHERTYPE_PPPOEDISC)
542 			pppoe_disc_input(m);
543 		else
544 			pppoe_data_input(m);
545 		return;
546 #endif
547 #ifdef MPLS
548 	case ETHERTYPE_MPLS:
549 	case ETHERTYPE_MPLS_MCAST:
550 		input = mpls_input;
551 		break;
552 #endif
553 #if NBPE > 0
554 	case ETHERTYPE_PBB:
555 		bpe_input(ifp, m);
556 		return;
557 #endif
558 	default:
559 		goto dropanyway;
560 	}
561 
562 	m_adj(m, sizeof(*eh));
563 	(*input)(ifp, m);
564 	return;
565 dropanyway:
566 	m_freem(m);
567 	return;
568 }
569 
570 int
571 ether_brport_isset(struct ifnet *ifp)
572 {
573 	struct arpcom *ac = (struct arpcom *)ifp;
574 
575 	KERNEL_ASSERT_LOCKED();
576 	if (SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL)
577 		return (EBUSY);
578 
579 	return (0);
580 }
581 
582 void
583 ether_brport_set(struct ifnet *ifp, const struct ether_brport *eb)
584 {
585 	struct arpcom *ac = (struct arpcom *)ifp;
586 
587 	KERNEL_ASSERT_LOCKED();
588 	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) == NULL,
589 	    "%s setting an already set brport", ifp->if_xname);
590 
591 	SMR_PTR_SET_LOCKED(&ac->ac_brport, eb);
592 }
593 
594 void
595 ether_brport_clr(struct ifnet *ifp)
596 {
597 	struct arpcom *ac = (struct arpcom *)ifp;
598 
599 	KERNEL_ASSERT_LOCKED();
600 	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL,
601 	    "%s clearing an already clear brport", ifp->if_xname);
602 
603 	SMR_PTR_SET_LOCKED(&ac->ac_brport, NULL);
604 }
605 
606 const struct ether_brport *
607 ether_brport_get(struct ifnet *ifp)
608 {
609 	struct arpcom *ac = (struct arpcom *)ifp;
610 	SMR_ASSERT_CRITICAL();
611 	return (SMR_PTR_GET(&ac->ac_brport));
612 }
613 
614 const struct ether_brport *
615 ether_brport_get_locked(struct ifnet *ifp)
616 {
617 	struct arpcom *ac = (struct arpcom *)ifp;
618 	KERNEL_ASSERT_LOCKED();
619 	return (SMR_PTR_GET_LOCKED(&ac->ac_brport));
620 }
621 
622 /*
623  * Convert Ethernet address to printable (loggable) representation.
624  */
625 static char digits[] = "0123456789abcdef";
626 char *
627 ether_sprintf(u_char *ap)
628 {
629 	int i;
630 	static char etherbuf[ETHER_ADDR_LEN * 3];
631 	char *cp = etherbuf;
632 
633 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
634 		*cp++ = digits[*ap >> 4];
635 		*cp++ = digits[*ap++ & 0xf];
636 		*cp++ = ':';
637 	}
638 	*--cp = 0;
639 	return (etherbuf);
640 }
641 
642 /*
643  * Generate a (hopefully) acceptable MAC address, if asked.
644  */
645 void
646 ether_fakeaddr(struct ifnet *ifp)
647 {
648 	static int unit;
649 	int rng = arc4random();
650 
651 	/* Non-multicast; locally administered address */
652 	((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe;
653 	((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1;
654 	((struct arpcom *)ifp)->ac_enaddr[2] = 0xba;
655 	((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf);
656 	((struct arpcom *)ifp)->ac_enaddr[4] = rng;
657 	((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8;
658 }
659 
660 /*
661  * Perform common duties while attaching to interface list
662  */
663 void
664 ether_ifattach(struct ifnet *ifp)
665 {
666 	struct arpcom *ac = (struct arpcom *)ifp;
667 
668 	/*
669 	 * Any interface which provides a MAC address which is obviously
670 	 * invalid gets whacked, so that users will notice.
671 	 */
672 	if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr))
673 		ether_fakeaddr(ifp);
674 
675 	ifp->if_type = IFT_ETHER;
676 	ifp->if_addrlen = ETHER_ADDR_LEN;
677 	ifp->if_hdrlen = ETHER_HDR_LEN;
678 	ifp->if_mtu = ETHERMTU;
679 	ifp->if_input = ether_input;
680 	if (ifp->if_output == NULL)
681 		ifp->if_output = ether_output;
682 	ifp->if_rtrequest = ether_rtrequest;
683 
684 	if (ifp->if_hardmtu == 0)
685 		ifp->if_hardmtu = ETHERMTU;
686 
687 	if_alloc_sadl(ifp);
688 	memcpy(LLADDR(ifp->if_sadl), ac->ac_enaddr, ifp->if_addrlen);
689 	LIST_INIT(&ac->ac_multiaddrs);
690 #if NBPFILTER > 0
691 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN);
692 #endif
693 }
694 
695 void
696 ether_ifdetach(struct ifnet *ifp)
697 {
698 	struct arpcom *ac = (struct arpcom *)ifp;
699 	struct ether_multi *enm;
700 
701 	/* Undo pseudo-driver changes. */
702 	if_deactivate(ifp);
703 
704 	for (enm = LIST_FIRST(&ac->ac_multiaddrs);
705 	    enm != NULL;
706 	    enm = LIST_FIRST(&ac->ac_multiaddrs)) {
707 		LIST_REMOVE(enm, enm_list);
708 		free(enm, M_IFMADDR, sizeof *enm);
709 	}
710 }
711 
712 #if 0
713 /*
714  * This is for reference.  We have table-driven versions of the
715  * crc32 generators, which are faster than the double-loop.
716  */
717 u_int32_t __pure
718 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
719 {
720 	u_int32_t c, carry;
721 	size_t i, j;
722 
723 	for (i = 0; i < len; i++) {
724 		c = buf[i];
725 		for (j = 0; j < 8; j++) {
726 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
727 			crc >>= 1;
728 			c >>= 1;
729 			if (carry)
730 				crc = (crc ^ ETHER_CRC_POLY_LE);
731 		}
732 	}
733 
734 	return (crc);
735 }
736 
737 u_int32_t __pure
738 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
739 {
740 	u_int32_t c, carry;
741 	size_t i, j;
742 
743 	for (i = 0; i < len; i++) {
744 		c = buf[i];
745 		for (j = 0; j < 8; j++) {
746 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
747 			crc <<= 1;
748 			c >>= 1;
749 			if (carry)
750 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
751 		}
752 	}
753 
754 	return (crc);
755 }
756 #else
757 u_int32_t __pure
758 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len)
759 {
760 	static const u_int32_t crctab[] = {
761 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
762 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
763 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
764 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
765 	};
766 	size_t i;
767 
768 	for (i = 0; i < len; i++) {
769 		crc ^= buf[i];
770 		crc = (crc >> 4) ^ crctab[crc & 0xf];
771 		crc = (crc >> 4) ^ crctab[crc & 0xf];
772 	}
773 
774 	return (crc);
775 }
776 
777 u_int32_t __pure
778 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len)
779 {
780 	static const u_int8_t rev[] = {
781 		0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
782 		0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
783 	};
784 	static const u_int32_t crctab[] = {
785 		0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
786 		0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
787 		0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
788 		0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd
789 	};
790 	size_t i;
791 	u_int8_t data;
792 
793 	for (i = 0; i < len; i++) {
794 		data = buf[i];
795 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]];
796 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]];
797 	}
798 
799 	return (crc);
800 }
801 #endif
802 
803 u_int32_t
804 ether_crc32_le(const u_int8_t *buf, size_t len)
805 {
806 	return ether_crc32_le_update(0xffffffff, buf, len);
807 }
808 
809 u_int32_t
810 ether_crc32_be(const u_int8_t *buf, size_t len)
811 {
812 	return ether_crc32_be_update(0xffffffff, buf, len);
813 }
814 
815 u_char	ether_ipmulticast_min[ETHER_ADDR_LEN] =
816     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
817 u_char	ether_ipmulticast_max[ETHER_ADDR_LEN] =
818     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
819 
820 #ifdef INET6
821 u_char	ether_ip6multicast_min[ETHER_ADDR_LEN] =
822     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
823 u_char	ether_ip6multicast_max[ETHER_ADDR_LEN] =
824     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
825 #endif
826 
827 /*
828  * Convert a sockaddr into an Ethernet address or range of Ethernet
829  * addresses.
830  */
831 int
832 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
833     u_int8_t addrhi[ETHER_ADDR_LEN])
834 {
835 	struct sockaddr_in *sin;
836 #ifdef INET6
837 	struct sockaddr_in6 *sin6;
838 #endif /* INET6 */
839 
840 	switch (sa->sa_family) {
841 
842 	case AF_UNSPEC:
843 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
844 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
845 		break;
846 
847 	case AF_INET:
848 		sin = satosin(sa);
849 		if (sin->sin_addr.s_addr == INADDR_ANY) {
850 			/*
851 			 * An IP address of INADDR_ANY means listen to
852 			 * or stop listening to all of the Ethernet
853 			 * multicast addresses used for IP.
854 			 * (This is for the sake of IP multicast routers.)
855 			 */
856 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
857 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
858 		} else {
859 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
860 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
861 		}
862 		break;
863 #ifdef INET6
864 	case AF_INET6:
865 		sin6 = satosin6(sa);
866 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
867 			/*
868 			 * An IP6 address of 0 means listen to or stop
869 			 * listening to all of the Ethernet multicast
870 			 * address used for IP6.
871 			 *
872 			 * (This might not be healthy, given IPv6's reliance on
873 			 * multicast for things like neighbor discovery.
874 			 * Perhaps initializing all-nodes, solicited nodes, and
875 			 * possibly all-routers for this interface afterwards
876 			 * is not a bad idea.)
877 			 */
878 
879 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
880 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
881 		} else {
882 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
883 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
884 		}
885 		break;
886 #endif
887 
888 	default:
889 		return (EAFNOSUPPORT);
890 	}
891 	return (0);
892 }
893 
894 /*
895  * Add an Ethernet multicast address or range of addresses to the list for a
896  * given interface.
897  */
898 int
899 ether_addmulti(struct ifreq *ifr, struct arpcom *ac)
900 {
901 	struct ether_multi *enm;
902 	u_char addrlo[ETHER_ADDR_LEN];
903 	u_char addrhi[ETHER_ADDR_LEN];
904 	int s = splnet(), error;
905 
906 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
907 	if (error != 0) {
908 		splx(s);
909 		return (error);
910 	}
911 
912 	/*
913 	 * Verify that we have valid Ethernet multicast addresses.
914 	 */
915 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
916 		splx(s);
917 		return (EINVAL);
918 	}
919 	/*
920 	 * See if the address range is already in the list.
921 	 */
922 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
923 	if (enm != NULL) {
924 		/*
925 		 * Found it; just increment the reference count.
926 		 */
927 		++enm->enm_refcount;
928 		splx(s);
929 		return (0);
930 	}
931 	/*
932 	 * New address or range; malloc a new multicast record
933 	 * and link it into the interface's multicast list.
934 	 */
935 	enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
936 	if (enm == NULL) {
937 		splx(s);
938 		return (ENOBUFS);
939 	}
940 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
941 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
942 	enm->enm_refcount = 1;
943 	LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
944 	ac->ac_multicnt++;
945 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
946 		ac->ac_multirangecnt++;
947 	splx(s);
948 	/*
949 	 * Return ENETRESET to inform the driver that the list has changed
950 	 * and its reception filter should be adjusted accordingly.
951 	 */
952 	return (ENETRESET);
953 }
954 
955 /*
956  * Delete a multicast address record.
957  */
958 int
959 ether_delmulti(struct ifreq *ifr, struct arpcom *ac)
960 {
961 	struct ether_multi *enm;
962 	u_char addrlo[ETHER_ADDR_LEN];
963 	u_char addrhi[ETHER_ADDR_LEN];
964 	int s = splnet(), error;
965 
966 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
967 	if (error != 0) {
968 		splx(s);
969 		return (error);
970 	}
971 
972 	/*
973 	 * Look up the address in our list.
974 	 */
975 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
976 	if (enm == NULL) {
977 		splx(s);
978 		return (ENXIO);
979 	}
980 	if (--enm->enm_refcount != 0) {
981 		/*
982 		 * Still some claims to this record.
983 		 */
984 		splx(s);
985 		return (0);
986 	}
987 	/*
988 	 * No remaining claims to this record; unlink and free it.
989 	 */
990 	LIST_REMOVE(enm, enm_list);
991 	free(enm, M_IFMADDR, sizeof *enm);
992 	ac->ac_multicnt--;
993 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
994 		ac->ac_multirangecnt--;
995 	splx(s);
996 	/*
997 	 * Return ENETRESET to inform the driver that the list has changed
998 	 * and its reception filter should be adjusted accordingly.
999 	 */
1000 	return (ENETRESET);
1001 }
1002 
1003 uint64_t
1004 ether_addr_to_e64(const struct ether_addr *ea)
1005 {
1006 	uint64_t e64 = 0;
1007 	size_t i;
1008 
1009 	for (i = 0; i < nitems(ea->ether_addr_octet); i++) {
1010 		e64 <<= 8;
1011 		e64 |= ea->ether_addr_octet[i];
1012 	}
1013 
1014 	return (e64);
1015 }
1016 
1017 void
1018 ether_e64_to_addr(struct ether_addr *ea, uint64_t e64)
1019 {
1020 	size_t i = nitems(ea->ether_addr_octet);
1021 
1022 	do {
1023 		ea->ether_addr_octet[--i] = e64;
1024 		e64 >>= 8;
1025 	} while (i > 0);
1026 }
1027