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