xref: /openbsd-src/sys/net/if_ethersubr.c (revision cd1eb269cafb12c415be1749cd4a4b5422710415)
1 /*	$OpenBSD: if_ethersubr.c,v 1.142 2010/05/07 13:33:16 claudio 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 #include <machine/cpu.h>
91 
92 #include <net/if.h>
93 #include <net/netisr.h>
94 #include <net/route.h>
95 #include <net/if_llc.h>
96 #include <net/if_dl.h>
97 #include <net/if_media.h>
98 #include <net/if_types.h>
99 
100 #include <netinet/in.h>
101 #ifdef INET
102 #include <netinet/in_var.h>
103 #endif
104 #include <netinet/if_ether.h>
105 #include <netinet/ip_ipsp.h>
106 
107 #include <dev/rndvar.h>
108 
109 #if NBPFILTER > 0
110 #include <net/bpf.h>
111 #endif
112 
113 #include "bridge.h"
114 #if NBRIDGE > 0
115 #include <net/if_bridge.h>
116 #endif
117 
118 #include "vlan.h"
119 #if NVLAN > 0
120 #include <net/if_vlan_var.h>
121 #endif /* NVLAN > 0 */
122 
123 #include "carp.h"
124 #if NCARP > 0
125 #include <netinet/ip_carp.h>
126 #endif
127 
128 #include "pppoe.h"
129 #if NPPPOE > 0
130 #include <net/if_pppoe.h>
131 #endif
132 
133 #include "trunk.h"
134 #if NTRUNK > 0
135 #include <net/if_trunk.h>
136 #endif
137 
138 #ifdef AOE
139 #include <net/if_aoe.h>
140 #endif /* AOE */
141 
142 #ifdef INET6
143 #ifndef INET
144 #include <netinet/in.h>
145 #endif
146 #include <netinet6/in6_var.h>
147 #include <netinet6/nd6.h>
148 #endif
149 
150 #ifdef PIPEX
151 #include <net/pipex.h>
152 #endif
153 
154 #ifdef NETATALK
155 #include <netatalk/at.h>
156 #include <netatalk/at_var.h>
157 #include <netatalk/at_extern.h>
158 
159 extern u_char	at_org_code[ 3 ];
160 extern u_char	aarp_org_code[ 3 ];
161 #endif /* NETATALK */
162 
163 #ifdef MPLS
164 #include <netmpls/mpls.h>
165 #endif /* MPLS */
166 
167 u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
168     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
169 #define senderr(e) { error = (e); goto bad;}
170 
171 
172 int
173 ether_ioctl(struct ifnet *ifp, struct arpcom *arp, u_long cmd, caddr_t data)
174 {
175 	struct ifaddr *ifa = (struct ifaddr *)data;
176 	struct ifreq *ifr = (struct ifreq *)data;
177 	int error = 0;
178 
179 	switch (cmd) {
180 	case SIOCSIFADDR:
181 		switch (ifa->ifa_addr->sa_family) {
182 #ifdef NETATALK
183 		case AF_APPLETALK:
184 			/* Nothing to do. */
185 			break;
186 #endif /* NETATALK */
187 		}
188 		break;
189 
190 	case SIOCSIFMTU:
191 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
192 			error = EINVAL;
193 		else
194 			ifp->if_mtu = ifr->ifr_mtu;
195 		break;
196 
197 	case SIOCADDMULTI:
198 	case SIOCDELMULTI:
199 		if (ifp->if_flags & IFF_MULTICAST) {
200 			error = (cmd == SIOCADDMULTI) ?
201 			    ether_addmulti(ifr, arp) :
202 			    ether_delmulti(ifr, arp);
203 		} else
204 			error = ENOTTY;
205 		break;
206 
207 	default:
208 		error = ENOTTY;
209 	}
210 
211 	return (error);
212 }
213 
214 /*
215  * Ethernet output routine.
216  * Encapsulate a packet of type family for the local net.
217  * Assumes that ifp is actually pointer to arpcom structure.
218  */
219 int
220 ether_output(ifp0, m0, dst, rt0)
221 	struct ifnet *ifp0;
222 	struct mbuf *m0;
223 	struct sockaddr *dst;
224 	struct rtentry *rt0;
225 {
226 	u_int16_t etype;
227 	int s, len, error = 0, hdrcmplt = 0;
228 	u_char edst[ETHER_ADDR_LEN], esrc[ETHER_ADDR_LEN];
229 	struct mbuf *m = m0;
230 	struct rtentry *rt;
231 	struct mbuf *mcopy = (struct mbuf *)0;
232 	struct ether_header *eh;
233 	struct arpcom *ac = (struct arpcom *)ifp0;
234 	short mflags;
235 	struct ifnet *ifp = ifp0;
236 
237 #ifdef DIAGNOSTIC
238 	if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.rdomain)) {
239 		printf("%s: trying to send packet on wrong domain. "
240 		    "if %d vs. mbuf %d, AF %d\n", ifp->if_xname,
241 		    ifp->if_rdomain, rtable_l2(m->m_pkthdr.rdomain),
242 		    dst->sa_family);
243 	}
244 #endif
245 
246 #if NTRUNK > 0
247 	if (ifp->if_type == IFT_IEEE8023ADLAG)
248 		senderr(EBUSY);
249 #endif
250 
251 #if NCARP > 0
252 	if (ifp->if_type == IFT_CARP) {
253 		struct ifaddr *ifa;
254 
255 		/* loop back if this is going to the carp interface */
256 		if (dst != NULL && LINK_STATE_IS_UP(ifp0->if_link_state) &&
257 		    (ifa = ifa_ifwithaddr(dst, ifp->if_rdomain)) != NULL &&
258 		    ifa->ifa_ifp == ifp0)
259 			return (looutput(ifp0, m, dst, rt0));
260 
261 		ifp = ifp->if_carpdev;
262 		ac = (struct arpcom *)ifp;
263 
264 		if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
265 		    (IFF_UP|IFF_RUNNING))
266 			senderr(ENETDOWN);
267 	}
268 #endif /* NCARP > 0 */
269 
270 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
271 		senderr(ENETDOWN);
272 	if ((rt = rt0) != NULL) {
273 		if ((rt->rt_flags & RTF_UP) == 0) {
274 			if ((rt0 = rt = rtalloc1(dst, RT_REPORT,
275 			    m->m_pkthdr.rdomain)) != NULL)
276 				rt->rt_refcnt--;
277 			else
278 				senderr(EHOSTUNREACH);
279 		}
280 #ifdef MPLS
281 		if (rt->rt_flags & RTF_MPLS) {
282 			if ((m = mpls_output(m, rt)) == NULL)
283 				senderr(EHOSTUNREACH);
284 		}
285 #endif
286 		if (rt->rt_flags & RTF_GATEWAY) {
287 			if (rt->rt_gwroute == 0)
288 				goto lookup;
289 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
290 				rtfree(rt); rt = rt0;
291 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway,
292 			    RT_REPORT, ifp->if_rdomain);
293 				if ((rt = rt->rt_gwroute) == 0)
294 					senderr(EHOSTUNREACH);
295 			}
296 		}
297 		if (rt->rt_flags & RTF_REJECT)
298 			if (rt->rt_rmx.rmx_expire == 0 ||
299 			    time_second < rt->rt_rmx.rmx_expire)
300 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
301 	}
302 
303 	switch (dst->sa_family) {
304 
305 #ifdef INET
306 	case AF_INET:
307 		if (!arpresolve(ac, rt, m, dst, edst))
308 			return (0);	/* if not yet resolved */
309 		/* If broadcasting on a simplex interface, loopback a copy */
310 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
311 		    !m->m_pkthdr.pf.routed)
312 			mcopy = m_copy(m, 0, (int)M_COPYALL);
313 #ifdef MPLS
314 		if (rt0 != NULL && rt0->rt_flags & RTF_MPLS)
315 			etype = htons(ETHERTYPE_MPLS);
316 		else
317 #endif
318 			etype = htons(ETHERTYPE_IP);
319 		break;
320 #endif
321 #ifdef INET6
322 	case AF_INET6:
323 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst))
324 			return (0); /* it must be impossible, but... */
325 		etype = htons(ETHERTYPE_IPV6);
326 		break;
327 #endif
328 #ifdef NETATALK
329 	case AF_APPLETALK: {
330 		struct at_ifaddr *aa;
331 
332 		if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
333 #ifdef NETATALKDEBUG
334 			extern char *prsockaddr(struct sockaddr *);
335 			printf("aarpresolv: failed for %s\n", prsockaddr(dst));
336 #endif /* NETATALKDEBUG */
337 			return (0);
338 		}
339 
340 		/*
341 		 * ifaddr is the first thing in at_ifaddr
342 		 */
343 		aa = (struct at_ifaddr *)at_ifawithnet(
344 			(struct sockaddr_at *)dst,
345 			TAILQ_FIRST(&ifp->if_addrlist));
346 		if (aa == 0)
347 			goto bad;
348 
349 		/*
350 		 * In the phase 2 case, we need to prepend an mbuf for the llc
351 		 * header. Since we must preserve the value of m, which is
352 		 * passed to us by value, we m_copy() the first mbuf,
353 		 * and use it for our llc header.
354 		 */
355 		if (aa->aa_flags & AFA_PHASE2) {
356 			struct llc llc;
357 
358 			M_PREPEND(m, AT_LLC_SIZE, M_DONTWAIT);
359 			if (m == NULL)
360 				return (0);
361 			/*
362 			 * FreeBSD doesn't count the LLC len in
363 			 * ifp->obytes, so they increment a length
364 			 * field here. We don't do this.
365 			 */
366 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
367 			llc.llc_control = LLC_UI;
368 			bcopy(at_org_code, llc.llc_snap.org_code,
369 				sizeof(at_org_code));
370 			llc.llc_snap.ether_type = htons( ETHERTYPE_AT );
371 			bcopy(&llc, mtod(m, caddr_t), AT_LLC_SIZE);
372 			etype = htons(m->m_pkthdr.len);
373 		} else {
374 			etype = htons(ETHERTYPE_AT);
375 		}
376 		} break;
377 #endif /* NETATALK */
378 #ifdef MPLS
379        case AF_MPLS:
380 		if (rt)
381 			dst = rt_key(rt);
382 		else
383 			senderr(EHOSTUNREACH);
384 
385 		switch (dst->sa_family) {
386 			case AF_LINK:
387 				if (((struct sockaddr_dl *)dst)->sdl_alen <
388 				    sizeof(edst))
389 					senderr(EHOSTUNREACH);
390 				bcopy(LLADDR(((struct sockaddr_dl *)dst)), edst,
391 				    sizeof(edst));
392 				break;
393 			case AF_INET:
394 				if (!arpresolve(ac, rt, m, dst, edst))
395 					return (0); /* if not yet resolved */
396 				break;
397 			default:
398 				senderr(EHOSTUNREACH);
399 		}
400 		/* XXX handling for simplex devices in case of M/BCAST ?? */
401 		if (m->m_flags & (M_BCAST | M_MCAST))
402 			etype = htons(ETHERTYPE_MPLS_MCAST);
403 		else
404 			etype = htons(ETHERTYPE_MPLS);
405 		break;
406 #endif /* MPLS */
407 	case pseudo_AF_HDRCMPLT:
408 		hdrcmplt = 1;
409 		eh = (struct ether_header *)dst->sa_data;
410 		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof(esrc));
411 		/* FALLTHROUGH */
412 
413 	case AF_UNSPEC:
414 		eh = (struct ether_header *)dst->sa_data;
415 		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof(edst));
416 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
417 		etype = eh->ether_type;
418 		break;
419 
420 	default:
421 		printf("%s: can't handle af%d\n", ifp->if_xname,
422 			dst->sa_family);
423 		senderr(EAFNOSUPPORT);
424 	}
425 
426 	/* XXX Should we feed-back an unencrypted IPsec packet ? */
427 	if (mcopy)
428 		(void) looutput(ifp, mcopy, dst, rt);
429 
430 	/*
431 	 * Add local net header.  If no space in first mbuf,
432 	 * allocate another.
433 	 */
434 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
435 	if (m == 0)
436 		senderr(ENOBUFS);
437 	eh = mtod(m, struct ether_header *);
438 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
439 		sizeof(eh->ether_type));
440 	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof(edst));
441 	if (hdrcmplt)
442 		bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
443 		    sizeof(eh->ether_shost));
444 	else
445 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
446 		    sizeof(eh->ether_shost));
447 
448 #if NCARP > 0
449 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP)
450 	    carp_rewrite_lladdr(ifp0, eh->ether_shost);
451 #endif
452 
453 #if NBRIDGE > 0
454 	/*
455 	 * Interfaces that are bridge members need special handling
456 	 * for output.
457 	 */
458 	if (ifp->if_bridge) {
459 		struct m_tag *mtag;
460 
461 		/*
462 		 * Check if this packet has already been sent out through
463 		 * this bridge, in which case we simply send it out
464 		 * without further bridge processing.
465 		 */
466 		for (mtag = m_tag_find(m, PACKET_TAG_BRIDGE, NULL); mtag;
467 		    mtag = m_tag_find(m, PACKET_TAG_BRIDGE, mtag)) {
468 #ifdef DEBUG
469 			/* Check that the information is there */
470 			if (mtag->m_tag_len != sizeof(caddr_t)) {
471 				error = EINVAL;
472 				goto bad;
473 			}
474 #endif
475 			if (!bcmp(&ifp->if_bridge, mtag + 1, sizeof(caddr_t)))
476 				break;
477 		}
478 		if (mtag == NULL) {
479 			/* Attach a tag so we can detect loops */
480 			mtag = m_tag_get(PACKET_TAG_BRIDGE, sizeof(caddr_t),
481 			    M_NOWAIT);
482 			if (mtag == NULL) {
483 				error = ENOBUFS;
484 				goto bad;
485 			}
486 			bcopy(&ifp->if_bridge, mtag + 1, sizeof(caddr_t));
487 			m_tag_prepend(m, mtag);
488 			error = bridge_output(ifp, m, NULL, NULL);
489 			return (error);
490 		}
491 	}
492 #endif
493 
494 	mflags = m->m_flags;
495 	len = m->m_pkthdr.len;
496 	s = splnet();
497 	/*
498 	 * Queue message on interface, and start output if interface
499 	 * not yet active.
500 	 */
501 	IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
502 	if (error) {
503 		/* mbuf is already freed */
504 		splx(s);
505 		return (error);
506 	}
507 	ifp->if_obytes += len;
508 #if NCARP > 0
509 	if (ifp != ifp0)
510 		ifp0->if_obytes += len;
511 #endif /* NCARP > 0 */
512 	if (mflags & M_MCAST)
513 		ifp->if_omcasts++;
514 	if_start(ifp);
515 	splx(s);
516 	return (error);
517 
518 bad:
519 	if (m)
520 		m_freem(m);
521 	return (error);
522 }
523 
524 /*
525  * Process a received Ethernet packet;
526  * the packet is in the mbuf chain m without
527  * the ether header, which is provided separately.
528  */
529 void
530 ether_input(ifp0, eh, m)
531 	struct ifnet *ifp0;
532 	struct ether_header *eh;
533 	struct mbuf *m;
534 {
535 	struct ifqueue *inq;
536 	u_int16_t etype;
537 	int s, llcfound = 0;
538 	struct llc *l;
539 	struct arpcom *ac;
540 	struct ifnet *ifp = ifp0;
541 #if NTRUNK > 0
542 	int i = 0;
543 #endif
544 #if NPPPOE > 0
545 	struct ether_header *eh_tmp;
546 #endif
547 
548 	m_cluncount(m, 1);
549 
550 	/* mark incoming routing domain */
551 	m->m_pkthdr.rdomain = ifp->if_rdomain;
552 
553 	if (eh == NULL) {
554 		eh = mtod(m, struct ether_header *);
555 		m_adj(m, ETHER_HDR_LEN);
556 	}
557 
558 #if NTRUNK > 0
559 	/* Handle input from a trunk port */
560 	while (ifp->if_type == IFT_IEEE8023ADLAG) {
561 		if (++i > TRUNK_MAX_STACKING) {
562 			m_freem(m);
563 			return;
564 		}
565 		if (trunk_input(ifp, eh, m) != 0)
566 			return;
567 
568 		/* Has been set to the trunk interface */
569 		ifp = m->m_pkthdr.rcvif;
570 	}
571 #endif
572 
573 	if ((ifp->if_flags & IFF_UP) == 0) {
574 		m_freem(m);
575 		return;
576 	}
577 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
578 		if ((ifp->if_flags & IFF_SIMPLEX) == 0) {
579 			struct ifaddr *ifa;
580 			struct sockaddr_dl *sdl = NULL;
581 
582 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
583 				if ((sdl =
584 				    (struct sockaddr_dl *)ifa->ifa_addr) &&
585 				    sdl->sdl_family == AF_LINK)
586 					break;
587 			}
588 			/*
589 			 * If this is not a simplex interface, drop the packet
590 			 * if it came from us.
591 			 */
592 			if (sdl && bcmp(LLADDR(sdl), eh->ether_shost,
593 			    ETHER_ADDR_LEN) == 0) {
594 				m_freem(m);
595 				return;
596 			}
597 		}
598 
599 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
600 		    sizeof(etherbroadcastaddr)) == 0)
601 			m->m_flags |= M_BCAST;
602 		else
603 			m->m_flags |= M_MCAST;
604 		ifp->if_imcasts++;
605 #if NTRUNK > 0
606 		if (ifp != ifp0)
607 			ifp0->if_imcasts++;
608 #endif
609 	}
610 
611 	ifp->if_ibytes += m->m_pkthdr.len + sizeof(*eh);
612 #if NTRUNK > 0
613 	if (ifp != ifp0)
614 		ifp0->if_ibytes += m->m_pkthdr.len + sizeof(*eh);
615 #endif
616 
617 	etype = ntohs(eh->ether_type);
618 
619 	if (!(netisr & (1 << NETISR_RND_DONE))) {
620 		add_net_randomness(etype);
621 		atomic_setbits_int(&netisr, (1 << NETISR_RND_DONE));
622 	}
623 
624 #if NVLAN > 0
625 	if (((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN)
626 	    && (vlan_input(eh, m) == 0))
627 		return;
628 #endif
629 
630 #if NBRIDGE > 0
631 	/*
632 	 * Tap the packet off here for a bridge, if configured and
633 	 * active for this interface.  bridge_input returns
634 	 * NULL if it has consumed the packet, otherwise, it
635 	 * gets processed as normal.
636 	 */
637 	if (ifp->if_bridge) {
638 		if (m->m_flags & M_PROTO1)
639 			m->m_flags &= ~M_PROTO1;
640 		else {
641 			m = bridge_input(ifp, eh, m);
642 			if (m == NULL)
643 				return;
644 			/* The bridge has determined it's for us. */
645 			ifp = m->m_pkthdr.rcvif;
646 		}
647 	}
648 #endif
649 
650 #if NVLAN > 0
651 	if ((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN) {
652 		/* The bridge did not want the vlan frame either, drop it. */
653 		ifp->if_noproto++;
654 		m_freem(m);
655 		return;
656 	}
657 #endif /* NVLAN > 0 */
658 
659 #if NCARP > 0
660 	if (ifp->if_carp) {
661 		if (ifp->if_type != IFT_CARP &&
662 		    (carp_input(m, (u_int8_t *)&eh->ether_shost,
663 		    (u_int8_t *)&eh->ether_dhost, eh->ether_type) == 0))
664 			return;
665 		/* clear mcast if received on a carp IP balanced address */
666 		else if (ifp->if_type == IFT_CARP &&
667 		    m->m_flags & (M_BCAST|M_MCAST) &&
668 		    carp_our_mcastaddr(ifp, (u_int8_t *)&eh->ether_dhost))
669 			m->m_flags &= ~(M_BCAST|M_MCAST);
670 	}
671 #endif /* NCARP > 0 */
672 
673 	ac = (struct arpcom *)ifp;
674 
675 	/*
676 	 * If packet has been filtered by the bpf listener, drop it now
677 	 */
678 	if (m->m_flags & M_FILDROP) {
679 		m_free(m);
680 		return;
681 	}
682 
683 	/*
684 	 * If packet is unicast and we're in promiscuous mode, make sure it
685 	 * is for us.  Drop otherwise.
686 	 */
687 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
688 	    (ifp->if_flags & IFF_PROMISC)) {
689 		if (bcmp(ac->ac_enaddr, (caddr_t)eh->ether_dhost,
690 		    ETHER_ADDR_LEN)) {
691 			m_freem(m);
692 			return;
693 		}
694 	}
695 
696 	/*
697 	 * Schedule softnet interrupt and enqueue packet within the same spl.
698 	 */
699 	s = splnet();
700 decapsulate:
701 
702 	switch (etype) {
703 #ifdef INET
704 	case ETHERTYPE_IP:
705 		schednetisr(NETISR_IP);
706 		inq = &ipintrq;
707 		break;
708 
709 	case ETHERTYPE_ARP:
710 		if (ifp->if_flags & IFF_NOARP)
711 			goto dropanyway;
712 		schednetisr(NETISR_ARP);
713 		inq = &arpintrq;
714 		break;
715 
716 	case ETHERTYPE_REVARP:
717 		if (ifp->if_flags & IFF_NOARP)
718 			goto dropanyway;
719 		revarpinput(m);	/* XXX queue? */
720 		goto done;
721 
722 #endif
723 #ifdef INET6
724 	/*
725 	 * Schedule IPv6 software interrupt for incoming IPv6 packet.
726 	 */
727 	case ETHERTYPE_IPV6:
728 		schednetisr(NETISR_IPV6);
729 		inq = &ip6intrq;
730 		break;
731 #endif /* INET6 */
732 #ifdef NETATALK
733 	case ETHERTYPE_AT:
734 		schednetisr(NETISR_ATALK);
735 		inq = &atintrq1;
736 		break;
737 	case ETHERTYPE_AARP:
738 		/* probably this should be done with a NETISR as well */
739 		/* XXX queue this */
740 		aarpinput((struct arpcom *)ifp, m);
741 		goto done;
742 #endif
743 #if NPPPOE > 0 || defined(PIPEX)
744 	case ETHERTYPE_PPPOEDISC:
745 	case ETHERTYPE_PPPOE:
746 		/* XXX we dont have this flag */
747 		/*
748 		if (m->m_flags & M_PROMISC) {
749 			m_freem(m);
750 			goto done;
751 		}
752 		*/
753 #ifndef PPPOE_SERVER
754 		if (m->m_flags & (M_MCAST | M_BCAST)) {
755 			m_freem(m);
756 			goto done;
757 		}
758 #endif
759 		M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
760 		if (m == NULL)
761 			goto done;
762 
763 		eh_tmp = mtod(m, struct ether_header *);
764 		bcopy(eh, eh_tmp, sizeof(struct ether_header));
765 #ifdef PIPEX
766 	{
767 		struct pipex_session *session;
768 
769 		if ((session = pipex_pppoe_lookup_session(m)) != NULL) {
770 			pipex_pppoe_input(m, session);
771 			goto done;
772 		}
773 	}
774 #endif
775 		if (etype == ETHERTYPE_PPPOEDISC)
776 			inq = &pppoediscinq;
777 		else
778 			inq = &pppoeinq;
779 
780 		schednetisr(NETISR_PPPOE);
781 		break;
782 #endif /* NPPPOE > 0 || defined(PIPEX) */
783 #ifdef AOE
784 	case ETHERTYPE_AOE:
785 		aoe_input(ifp, m);
786 		goto done;
787 #endif /* AOE */
788 #ifdef MPLS
789 	case ETHERTYPE_MPLS:
790 	case ETHERTYPE_MPLS_MCAST:
791 		inq = &mplsintrq;
792 		schednetisr(NETISR_MPLS);
793 		break;
794 #endif
795 	default:
796 		if (llcfound || etype > ETHERMTU)
797 			goto dropanyway;
798 		llcfound = 1;
799 		l = mtod(m, struct llc *);
800 		switch (l->llc_dsap) {
801 		case LLC_SNAP_LSAP:
802 #ifdef NETATALK
803 			/*
804 			 * Some protocols (like Appletalk) need special
805 			 * handling depending on if they are type II
806 			 * or SNAP encapsulated. Everything else
807 			 * gets handled by stripping off the SNAP header
808 			 * and going back up to decapsulate.
809 			 */
810 			if (l->llc_control == LLC_UI &&
811 			    l->llc_ssap == LLC_SNAP_LSAP &&
812 			    Bcmp(&(l->llc_snap.org_code)[0],
813 			    at_org_code, sizeof(at_org_code)) == 0 &&
814 			    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
815 				inq = &atintrq2;
816 				m_adj(m, AT_LLC_SIZE);
817 				schednetisr(NETISR_ATALK);
818 				break;
819 			}
820 
821 			if (l->llc_control == LLC_UI &&
822 			    l->llc_ssap == LLC_SNAP_LSAP &&
823 			    Bcmp(&(l->llc_snap.org_code)[0],
824 			    aarp_org_code, sizeof(aarp_org_code)) == 0 &&
825 			    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
826 				m_adj(m, AT_LLC_SIZE);
827 				/* XXX Really this should use netisr too */
828 				aarpinput((struct arpcom *)ifp, m);
829 				goto done;
830 			}
831 #endif /* NETATALK */
832 			if (l->llc_control == LLC_UI &&
833 			    l->llc_dsap == LLC_SNAP_LSAP &&
834 			    l->llc_ssap == LLC_SNAP_LSAP) {
835 				/* SNAP */
836 				if (m->m_pkthdr.len > etype)
837 					m_adj(m, etype - m->m_pkthdr.len);
838 				m->m_data += 6;		/* XXX */
839 				m->m_len -= 6;		/* XXX */
840 				m->m_pkthdr.len -= 6;	/* XXX */
841 				M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
842 				if (m == 0)
843 					goto done;
844 				*mtod(m, struct ether_header *) = *eh;
845 				goto decapsulate;
846 			}
847 			goto dropanyway;
848 		dropanyway:
849 		default:
850 			m_freem(m);
851 			goto done;
852 		}
853 	}
854 
855 	IF_INPUT_ENQUEUE(inq, m);
856 done:
857 	splx(s);
858 }
859 
860 /*
861  * Convert Ethernet address to printable (loggable) representation.
862  */
863 static char digits[] = "0123456789abcdef";
864 char *
865 ether_sprintf(ap)
866 	u_char *ap;
867 {
868 	int i;
869 	static char etherbuf[ETHER_ADDR_LEN * 3];
870 	char *cp = etherbuf;
871 
872 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
873 		*cp++ = digits[*ap >> 4];
874 		*cp++ = digits[*ap++ & 0xf];
875 		*cp++ = ':';
876 	}
877 	*--cp = 0;
878 	return (etherbuf);
879 }
880 
881 /*
882  * Generate a (hopefully) acceptable MAC address, if asked.
883  */
884 void
885 ether_fakeaddr(struct ifnet *ifp)
886 {
887 	static int unit;
888 	int rng;
889 
890 	/* Non-multicast; locally administered address */
891 	((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe;
892 	((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1;
893 	((struct arpcom *)ifp)->ac_enaddr[2] = 0xba;
894 	((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf);
895 	rng = cold ? random() ^ (long)ifp : arc4random();
896 	((struct arpcom *)ifp)->ac_enaddr[4] = rng;
897 	((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8;
898 }
899 
900 /*
901  * Perform common duties while attaching to interface list
902  */
903 void
904 ether_ifattach(ifp)
905 	struct ifnet *ifp;
906 {
907 	/*
908 	 * Any interface which provides a MAC address which is obviously
909 	 * invalid gets whacked, so that users will notice.
910 	 */
911 	if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr))
912 		ether_fakeaddr(ifp);
913 
914 	ifp->if_type = IFT_ETHER;
915 	ifp->if_addrlen = ETHER_ADDR_LEN;
916 	ifp->if_hdrlen = ETHER_HDR_LEN;
917 	ifp->if_mtu = ETHERMTU;
918 	ifp->if_output = ether_output;
919 
920 	if (ifp->if_hardmtu == 0)
921 		ifp->if_hardmtu = ETHERMTU;
922 
923 	if_alloc_sadl(ifp);
924 	bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
925 	    LLADDR(ifp->if_sadl), ifp->if_addrlen);
926 	LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
927 #if NBPFILTER > 0
928 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN);
929 #endif
930 }
931 
932 void
933 ether_ifdetach(ifp)
934 	struct ifnet *ifp;
935 {
936 	struct arpcom *ac = (struct arpcom *)ifp;
937 	struct ether_multi *enm;
938 
939 	for (enm = LIST_FIRST(&ac->ac_multiaddrs);
940 	    enm != LIST_END(&ac->ac_multiaddrs);
941 	    enm = LIST_FIRST(&ac->ac_multiaddrs)) {
942 		LIST_REMOVE(enm, enm_list);
943 		free(enm, M_IFMADDR);
944 	}
945 
946 #if 0
947 	/* moved to if_detach() */
948 	if_free_sadl(ifp);
949 #endif
950 }
951 
952 #if 0
953 /*
954  * This is for reference.  We have table-driven versions of the
955  * crc32 generators, which are faster than the double-loop.
956  */
957 u_int32_t __pure
958 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
959 {
960 	u_int32_t c, carry;
961 	size_t i, j;
962 
963 	for (i = 0; i < len; i++) {
964 		c = buf[i];
965 		for (j = 0; j < 8; j++) {
966 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
967 			crc >>= 1;
968 			c >>= 1;
969 			if (carry)
970 				crc = (crc ^ ETHER_CRC_POLY_LE);
971 		}
972 	}
973 
974 	return (crc);
975 }
976 
977 u_int32_t __pure
978 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len)
979 {
980 	u_int32_t c, carry;
981 	size_t i, j;
982 
983 	for (i = 0; i < len; i++) {
984 		c = buf[i];
985 		for (j = 0; j < 8; j++) {
986 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
987 			crc <<= 1;
988 			c >>= 1;
989 			if (carry)
990 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
991 		}
992 	}
993 
994 	return (crc);
995 }
996 #else
997 u_int32_t __pure
998 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len)
999 {
1000 	static const u_int32_t crctab[] = {
1001 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1002 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1003 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1004 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1005 	};
1006 	size_t i;
1007 
1008 	for (i = 0; i < len; i++) {
1009 		crc ^= buf[i];
1010 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1011 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1012 	}
1013 
1014 	return (crc);
1015 }
1016 
1017 u_int32_t __pure
1018 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len)
1019 {
1020 	static const u_int8_t rev[] = {
1021 		0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
1022 		0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
1023 	};
1024 	static const u_int32_t crctab[] = {
1025 		0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
1026 		0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
1027 		0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
1028 		0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd
1029 	};
1030 	size_t i;
1031 	u_int8_t data;
1032 
1033 	for (i = 0; i < len; i++) {
1034 		data = buf[i];
1035 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]];
1036 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]];
1037 	}
1038 
1039 	return (crc);
1040 }
1041 #endif
1042 
1043 u_int32_t
1044 ether_crc32_le(const u_int8_t *buf, size_t len)
1045 {
1046 	return ether_crc32_le_update(0xffffffff, buf, len);
1047 }
1048 
1049 u_int32_t
1050 ether_crc32_be(const u_int8_t *buf, size_t len)
1051 {
1052 	return ether_crc32_be_update(0xffffffff, buf, len);
1053 }
1054 
1055 #ifdef INET
1056 u_char	ether_ipmulticast_min[ETHER_ADDR_LEN] =
1057     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1058 u_char	ether_ipmulticast_max[ETHER_ADDR_LEN] =
1059     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1060 #endif
1061 
1062 #ifdef INET6
1063 u_char	ether_ip6multicast_min[ETHER_ADDR_LEN] =
1064     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1065 u_char	ether_ip6multicast_max[ETHER_ADDR_LEN] =
1066     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1067 #endif
1068 
1069 /*
1070  * Convert a sockaddr into an Ethernet address or range of Ethernet
1071  * addresses.
1072  */
1073 int
1074 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
1075     u_int8_t addrhi[ETHER_ADDR_LEN])
1076 {
1077 #ifdef INET
1078 	struct sockaddr_in *sin;
1079 #endif /* INET */
1080 #ifdef INET6
1081 	struct sockaddr_in6 *sin6;
1082 #endif /* INET6 */
1083 
1084 	switch (sa->sa_family) {
1085 
1086 	case AF_UNSPEC:
1087 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
1088 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1089 		break;
1090 
1091 #ifdef INET
1092 	case AF_INET:
1093 		sin = satosin(sa);
1094 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1095 			/*
1096 			 * An IP address of INADDR_ANY means listen to
1097 			 * or stop listening to all of the Ethernet
1098 			 * multicast addresses used for IP.
1099 			 * (This is for the sake of IP multicast routers.)
1100 			 */
1101 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
1102 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
1103 		} else {
1104 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1105 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1106 		}
1107 		break;
1108 #endif
1109 #ifdef INET6
1110 	case AF_INET6:
1111 		sin6 = satosin6(sa);
1112 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1113 			/*
1114 			 * An IP6 address of 0 means listen to or stop
1115 			 * listening to all of the Ethernet multicast
1116 			 * address used for IP6.
1117 			 *
1118 			 * (This might not be healthy, given IPv6's reliance on
1119 			 * multicast for things like neighbor discovery.
1120 			 * Perhaps initializing all-nodes, solicited nodes, and
1121 			 * possibly all-routers for this interface afterwards
1122 			 * is not a bad idea.)
1123 			 */
1124 
1125 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1126 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1127 		} else {
1128 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1129 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1130 		}
1131 		break;
1132 #endif
1133 
1134 	default:
1135 		return (EAFNOSUPPORT);
1136 	}
1137 	return (0);
1138 }
1139 
1140 /*
1141  * Add an Ethernet multicast address or range of addresses to the list for a
1142  * given interface.
1143  */
1144 int
1145 ether_addmulti(ifr, ac)
1146 	struct ifreq *ifr;
1147 	struct arpcom *ac;
1148 {
1149 	struct ether_multi *enm;
1150 	u_char addrlo[ETHER_ADDR_LEN];
1151 	u_char addrhi[ETHER_ADDR_LEN];
1152 	int s = splnet(), error;
1153 
1154 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1155 	if (error != 0) {
1156 		splx(s);
1157 		return (error);
1158 	}
1159 
1160 	/*
1161 	 * Verify that we have valid Ethernet multicast addresses.
1162 	 */
1163 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1164 		splx(s);
1165 		return (EINVAL);
1166 	}
1167 	/*
1168 	 * See if the address range is already in the list.
1169 	 */
1170 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
1171 	if (enm != NULL) {
1172 		/*
1173 		 * Found it; just increment the reference count.
1174 		 */
1175 		++enm->enm_refcount;
1176 		splx(s);
1177 		return (0);
1178 	}
1179 	/*
1180 	 * New address or range; malloc a new multicast record
1181 	 * and link it into the interface's multicast list.
1182 	 */
1183 	enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1184 	if (enm == NULL) {
1185 		splx(s);
1186 		return (ENOBUFS);
1187 	}
1188 	bcopy(addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1189 	bcopy(addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1190 	enm->enm_ac = ac;
1191 	enm->enm_refcount = 1;
1192 	LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
1193 	ac->ac_multicnt++;
1194 	if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
1195 		ac->ac_multirangecnt++;
1196 	splx(s);
1197 	/*
1198 	 * Return ENETRESET to inform the driver that the list has changed
1199 	 * and its reception filter should be adjusted accordingly.
1200 	 */
1201 	return (ENETRESET);
1202 }
1203 
1204 /*
1205  * Delete a multicast address record.
1206  */
1207 int
1208 ether_delmulti(ifr, ac)
1209 	struct ifreq *ifr;
1210 	struct arpcom *ac;
1211 {
1212 	struct ether_multi *enm;
1213 	u_char addrlo[ETHER_ADDR_LEN];
1214 	u_char addrhi[ETHER_ADDR_LEN];
1215 	int s = splnet(), error;
1216 
1217 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1218 	if (error != 0) {
1219 		splx(s);
1220 		return (error);
1221 	}
1222 
1223 	/*
1224 	 * Look up the address in our list.
1225 	 */
1226 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
1227 	if (enm == NULL) {
1228 		splx(s);
1229 		return (ENXIO);
1230 	}
1231 	if (--enm->enm_refcount != 0) {
1232 		/*
1233 		 * Still some claims to this record.
1234 		 */
1235 		splx(s);
1236 		return (0);
1237 	}
1238 	/*
1239 	 * No remaining claims to this record; unlink and free it.
1240 	 */
1241 	LIST_REMOVE(enm, enm_list);
1242 	free(enm, M_IFMADDR);
1243 	ac->ac_multicnt--;
1244 	if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
1245 		ac->ac_multirangecnt--;
1246 	splx(s);
1247 	/*
1248 	 * Return ENETRESET to inform the driver that the list has changed
1249 	 * and its reception filter should be adjusted accordingly.
1250 	 */
1251 	return (ENETRESET);
1252 }
1253