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