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