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