xref: /netbsd-src/sys/net/if_ethersubr.c (revision b5bbe2e3e7c9fd9fdc348587c2f4eb55a8b89393)
1 /*	$NetBSD: if_ethersubr.c,v 1.206 2015/04/03 07:55:18 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.206 2015/04/03 07:55:18 ozaki-r Exp $");
65 
66 #include "opt_inet.h"
67 #include "opt_atalk.h"
68 #include "opt_ipx.h"
69 #include "opt_mbuftrace.h"
70 #include "opt_mpls.h"
71 #include "opt_gateway.h"
72 #include "opt_pppoe.h"
73 #include "opt_net_mpsafe.h"
74 #include "vlan.h"
75 #include "pppoe.h"
76 #include "bridge.h"
77 #include "arp.h"
78 #include "agr.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/sysctl.h>
83 #include <sys/kernel.h>
84 #include <sys/callout.h>
85 #include <sys/malloc.h>
86 #include <sys/mbuf.h>
87 #include <sys/protosw.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
90 #include <sys/errno.h>
91 #include <sys/syslog.h>
92 #include <sys/kauth.h>
93 #include <sys/cpu.h>
94 #include <sys/intr.h>
95 #include <sys/device.h>
96 #include <sys/rnd.h>
97 
98 #include <net/if.h>
99 #include <net/netisr.h>
100 #include <net/route.h>
101 #include <net/if_llc.h>
102 #include <net/if_dl.h>
103 #include <net/if_types.h>
104 
105 #include <net/if_media.h>
106 #include <dev/mii/mii.h>
107 #include <dev/mii/miivar.h>
108 
109 #if NARP == 0
110 /*
111  * XXX there should really be a way to issue this warning from within config(8)
112  */
113 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
114 #endif
115 
116 #include <net/bpf.h>
117 
118 #include <net/if_ether.h>
119 #include <net/if_vlanvar.h>
120 
121 #if NPPPOE > 0
122 #include <net/if_pppoe.h>
123 #endif
124 
125 #if NAGR > 0
126 #include <net/agr/ieee8023_slowprotocols.h>	/* XXX */
127 #include <net/agr/ieee8023ad.h>
128 #include <net/agr/if_agrvar.h>
129 #endif
130 
131 #if NBRIDGE > 0
132 #include <net/if_bridgevar.h>
133 #endif
134 
135 #include <netinet/in.h>
136 #ifdef INET
137 #include <netinet/in_var.h>
138 #endif
139 #include <netinet/if_inarp.h>
140 
141 #ifdef INET6
142 #ifndef INET
143 #include <netinet/in.h>
144 #endif
145 #include <netinet6/in6_var.h>
146 #include <netinet6/nd6.h>
147 #endif
148 
149 
150 #include "carp.h"
151 #if NCARP > 0
152 #include <netinet/ip_carp.h>
153 #endif
154 
155 #ifdef IPX
156 #include <netipx/ipx.h>
157 #include <netipx/ipx_if.h>
158 #endif
159 
160 #ifdef NETATALK
161 #include <netatalk/at.h>
162 #include <netatalk/at_var.h>
163 #include <netatalk/at_extern.h>
164 
165 #define llc_snap_org_code llc_un.type_snap.org_code
166 #define llc_snap_ether_type llc_un.type_snap.ether_type
167 
168 extern u_char	at_org_code[3];
169 extern u_char	aarp_org_code[3];
170 #endif /* NETATALK */
171 
172 #ifdef MPLS
173 #include <netmpls/mpls.h>
174 #include <netmpls/mpls_var.h>
175 #endif
176 
177 static struct timeval bigpktppslim_last;
178 static int bigpktppslim = 2;	/* XXX */
179 static int bigpktpps_count;
180 static kmutex_t bigpktpps_lock __cacheline_aligned;
181 
182 
183 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
184     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
185 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
186     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
187 #define senderr(e) { error = (e); goto bad;}
188 
189 static	int ether_output(struct ifnet *, struct mbuf *,
190 	    const struct sockaddr *, struct rtentry *);
191 
192 /*
193  * Ethernet output routine.
194  * Encapsulate a packet of type family for the local net.
195  * Assumes that ifp is actually pointer to ethercom structure.
196  */
197 static int
198 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
199 	const struct sockaddr * const dst,
200 	struct rtentry *rt0)
201 {
202 	uint16_t etype = 0;
203 	int error = 0, hdrcmplt = 0;
204  	uint8_t esrc[6], edst[6];
205 	struct mbuf *m = m0;
206 	struct rtentry *rt;
207 	struct mbuf *mcopy = NULL;
208 	struct ether_header *eh;
209 	struct ifnet *ifp = ifp0;
210 	ALTQ_DECL(struct altq_pktattr pktattr;)
211 #ifdef INET
212 	struct arphdr *ah;
213 #endif /* INET */
214 #ifdef NETATALK
215 	struct at_ifaddr *aa;
216 #endif /* NETATALK */
217 
218 #ifndef NET_MPSAFE
219 	KASSERT(KERNEL_LOCKED_P());
220 #endif
221 
222 #ifdef MBUFTRACE
223 	m_claimm(m, ifp->if_mowner);
224 #endif
225 
226 #if NCARP > 0
227 	if (ifp->if_type == IFT_CARP) {
228 		struct ifaddr *ifa;
229 
230 		/* loop back if this is going to the carp interface */
231 		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
232 		    (ifa = ifa_ifwithaddr(dst)) != NULL &&
233 		    ifa->ifa_ifp == ifp0)
234 			return looutput(ifp0, m, dst, rt0);
235 
236 		ifp = ifp->if_carpdev;
237 		/* ac = (struct arpcom *)ifp; */
238 
239 		if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
240 		    (IFF_UP|IFF_RUNNING))
241 			senderr(ENETDOWN);
242 	}
243 #endif /* NCARP > 0 */
244 
245 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
246 		senderr(ENETDOWN);
247 	if ((rt = rt0) != NULL) {
248 		if ((rt->rt_flags & RTF_UP) == 0) {
249 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
250 				rt->rt_refcnt--;
251 				if (rt->rt_ifp != ifp)
252 					return (*rt->rt_ifp->if_output)
253 							(ifp, m0, dst, rt);
254 			} else
255 				senderr(EHOSTUNREACH);
256 		}
257 		if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
258 			if (rt->rt_gwroute == NULL)
259 				goto lookup;
260 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
261 				rtfree(rt); rt = rt0;
262 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
263 				if ((rt = rt->rt_gwroute) == NULL)
264 					senderr(EHOSTUNREACH);
265 				/* the "G" test below also prevents rt == rt0 */
266 				if ((rt->rt_flags & RTF_GATEWAY) ||
267 				    (rt->rt_ifp != ifp)) {
268 					rt->rt_refcnt--;
269 					rt0->rt_gwroute = NULL;
270 					senderr(EHOSTUNREACH);
271 				}
272 			}
273 		}
274 		if (rt->rt_flags & RTF_REJECT)
275 			if (rt->rt_rmx.rmx_expire == 0 ||
276 			    (u_long) time_second < rt->rt_rmx.rmx_expire)
277 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
278 	}
279 
280 	switch (dst->sa_family) {
281 
282 #ifdef INET
283 	case AF_INET:
284 		if (m->m_flags & M_BCAST)
285 			(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
286 		else if (m->m_flags & M_MCAST)
287 			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
288 		else if (!arpresolve(ifp, rt, m, dst, edst))
289 			return (0);	/* if not yet resolved */
290 		/* If broadcasting on a simplex interface, loopback a copy */
291 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
292 			mcopy = m_copy(m, 0, (int)M_COPYALL);
293 		etype = htons(ETHERTYPE_IP);
294 		break;
295 
296 	case AF_ARP:
297 		ah = mtod(m, struct arphdr *);
298 		if (m->m_flags & M_BCAST)
299 			(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
300 		else {
301 			void *tha = ar_tha(ah);
302 
303 			if (tha == NULL) {
304 				/* fake with ARPHDR_IEEE1394 */
305 				return 0;
306 			}
307 			memcpy(edst, tha, sizeof(edst));
308 		}
309 
310 		ah->ar_hrd = htons(ARPHRD_ETHER);
311 
312 		switch (ntohs(ah->ar_op)) {
313 		case ARPOP_REVREQUEST:
314 		case ARPOP_REVREPLY:
315 			etype = htons(ETHERTYPE_REVARP);
316 			break;
317 
318 		case ARPOP_REQUEST:
319 		case ARPOP_REPLY:
320 		default:
321 			etype = htons(ETHERTYPE_ARP);
322 		}
323 
324 		break;
325 #endif
326 #ifdef INET6
327 	case AF_INET6:
328 		if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
329 			/* something bad happened */
330 			return (0);
331 		}
332 		etype = htons(ETHERTYPE_IPV6);
333 		break;
334 #endif
335 #ifdef NETATALK
336     case AF_APPLETALK:
337 		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
338 #ifdef NETATALKDEBUG
339 			printf("aarpresolv failed\n");
340 #endif /* NETATALKDEBUG */
341 			return (0);
342 		}
343 		/*
344 		 * ifaddr is the first thing in at_ifaddr
345 		 */
346 		aa = (struct at_ifaddr *) at_ifawithnet(
347 		    (const struct sockaddr_at *)dst, ifp);
348 		if (aa == NULL)
349 		    goto bad;
350 
351 		/*
352 		 * In the phase 2 case, we need to prepend an mbuf for the
353 		 * llc header.  Since we must preserve the value of m,
354 		 * which is passed to us by value, we m_copy() the first
355 		 * mbuf, and use it for our llc header.
356 		 */
357 		if (aa->aa_flags & AFA_PHASE2) {
358 			struct llc llc;
359 
360 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
361 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
362 			llc.llc_control = LLC_UI;
363 			memcpy(llc.llc_snap_org_code, at_org_code,
364 			    sizeof(llc.llc_snap_org_code));
365 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
366 			memcpy(mtod(m, void *), &llc, sizeof(struct llc));
367 		} else {
368 			etype = htons(ETHERTYPE_ATALK);
369 		}
370 		break;
371 #endif /* NETATALK */
372 #ifdef IPX
373 	case AF_IPX:
374 		etype = htons(ETHERTYPE_IPX);
375  		memcpy(edst,
376 		    &(((const struct sockaddr_ipx *)dst)->sipx_addr.x_host),
377 		    sizeof(edst));
378 		/* If broadcasting on a simplex interface, loopback a copy */
379 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
380 			mcopy = m_copy(m, 0, (int)M_COPYALL);
381 		break;
382 #endif
383 	case pseudo_AF_HDRCMPLT:
384 		hdrcmplt = 1;
385 		memcpy(esrc,
386 		    ((const struct ether_header *)dst->sa_data)->ether_shost,
387 		    sizeof(esrc));
388 		/* FALLTHROUGH */
389 
390 	case AF_UNSPEC:
391  		memcpy(edst,
392 		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
393 		    sizeof(edst));
394 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
395 		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
396 		break;
397 
398 	default:
399 		printf("%s: can't handle af%d\n", ifp->if_xname,
400 			dst->sa_family);
401 		senderr(EAFNOSUPPORT);
402 	}
403 
404 #ifdef MPLS
405 	if (rt0 != NULL && rt_gettag(rt0) != NULL &&
406 	    rt_gettag(rt0)->sa_family == AF_MPLS &&
407 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
408 		union mpls_shim msh;
409 		msh.s_addr = MPLS_GETSADDR(rt0);
410 		if (msh.shim.label != MPLS_LABEL_IMPLNULL)
411 			etype = htons(ETHERTYPE_MPLS);
412 	}
413 #endif
414 
415 	if (mcopy)
416 		(void)looutput(ifp, mcopy, dst, rt);
417 
418 	/* If no ether type is set, this must be a 802.2 formatted packet.
419 	 */
420 	if (etype == 0)
421 		etype = htons(m->m_pkthdr.len);
422 	/*
423 	 * Add local net header.  If no space in first mbuf,
424 	 * allocate another.
425 	 */
426 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
427 	if (m == 0)
428 		senderr(ENOBUFS);
429 	eh = mtod(m, struct ether_header *);
430 	/* Note: etype is already in network byte order. */
431 	(void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
432  	memcpy(eh->ether_dhost, edst, sizeof(edst));
433 	if (hdrcmplt)
434 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
435 	else
436 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
437 		    sizeof(eh->ether_shost));
438 
439 #if NCARP > 0
440 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
441 	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
442 		    sizeof(eh->ether_shost));
443 	}
444 #endif /* NCARP > 0 */
445 
446 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
447 		return (error);
448 	if (m == NULL)
449 		return (0);
450 
451 #if NBRIDGE > 0
452 	/*
453 	 * Bridges require special output handling.
454 	 */
455 	if (ifp->if_bridge)
456 		return (bridge_output(ifp, m, NULL, NULL));
457 #endif
458 
459 #if NCARP > 0
460 	if (ifp != ifp0)
461 		ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
462 #endif /* NCARP > 0 */
463 
464 #ifdef ALTQ
465 	/*
466 	 * If ALTQ is enabled on the parent interface, do
467 	 * classification; the queueing discipline might not
468 	 * require classification, but might require the
469 	 * address family/header pointer in the pktattr.
470 	 */
471 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
472 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
473 #endif
474 	return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
475 
476 bad:
477 	if (m)
478 		m_freem(m);
479 	return (error);
480 }
481 
482 #ifdef ALTQ
483 /*
484  * This routine is a slight hack to allow a packet to be classified
485  * if the Ethernet headers are present.  It will go away when ALTQ's
486  * classification engine understands link headers.
487  */
488 void
489 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
490     struct altq_pktattr *pktattr)
491 {
492 	struct ether_header *eh;
493 	uint16_t ether_type;
494 	int hlen, af, hdrsize;
495 	void *hdr;
496 
497 	hlen = ETHER_HDR_LEN;
498 	eh = mtod(m, struct ether_header *);
499 
500 	ether_type = htons(eh->ether_type);
501 
502 	if (ether_type < ETHERMTU) {
503 		/* LLC/SNAP */
504 		struct llc *llc = (struct llc *)(eh + 1);
505 		hlen += 8;
506 
507 		if (m->m_len < hlen ||
508 		    llc->llc_dsap != LLC_SNAP_LSAP ||
509 		    llc->llc_ssap != LLC_SNAP_LSAP ||
510 		    llc->llc_control != LLC_UI) {
511 			/* Not SNAP. */
512 			goto bad;
513 		}
514 
515 		ether_type = htons(llc->llc_un.type_snap.ether_type);
516 	}
517 
518 	switch (ether_type) {
519 	case ETHERTYPE_IP:
520 		af = AF_INET;
521 		hdrsize = 20;		/* sizeof(struct ip) */
522 		break;
523 
524 	case ETHERTYPE_IPV6:
525 		af = AF_INET6;
526 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
527 		break;
528 
529 	default:
530 		af = AF_UNSPEC;
531 		hdrsize = 0;
532 		break;
533 	}
534 
535 	while (m->m_len <= hlen) {
536 		hlen -= m->m_len;
537 		m = m->m_next;
538 	}
539 	if (m->m_len < (hlen + hdrsize)) {
540 		/*
541 		 * protocol header not in a single mbuf.
542 		 * We can't cope with this situation right
543 		 * now (but it shouldn't ever happen, really, anyhow).
544 		 */
545 #ifdef DEBUG
546 		printf("altq_etherclassify: headers span multiple mbufs: "
547 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
548 #endif
549 		goto bad;
550 	}
551 
552 	m->m_data += hlen;
553 	m->m_len -= hlen;
554 
555 	hdr = mtod(m, void *);
556 
557 	if (ALTQ_NEEDS_CLASSIFY(ifq))
558 		pktattr->pattr_class =
559 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
560 	pktattr->pattr_af = af;
561 	pktattr->pattr_hdr = hdr;
562 
563 	m->m_data -= hlen;
564 	m->m_len += hlen;
565 
566 	return;
567 
568  bad:
569 	pktattr->pattr_class = NULL;
570 	pktattr->pattr_hdr = NULL;
571 	pktattr->pattr_af = AF_UNSPEC;
572 }
573 #endif /* ALTQ */
574 
575 /*
576  * Process a received Ethernet packet;
577  * the packet is in the mbuf chain m with
578  * the ether header.
579  */
580 void
581 ether_input(struct ifnet *ifp, struct mbuf *m)
582 {
583 	struct ethercom *ec = (struct ethercom *) ifp;
584 	pktqueue_t *pktq = NULL;
585 	struct ifqueue *inq = NULL;
586 	uint16_t etype;
587 	struct ether_header *eh;
588 	size_t ehlen;
589 	static int earlypkts;
590 	int isr = 0;
591 #if defined (LLC) || defined(NETATALK)
592 	struct llc *l;
593 #endif
594 
595 	if ((ifp->if_flags & IFF_UP) == 0) {
596 		m_freem(m);
597 		return;
598 	}
599 
600 #ifdef MBUFTRACE
601 	m_claimm(m, &ec->ec_rx_mowner);
602 #endif
603 	eh = mtod(m, struct ether_header *);
604 	etype = ntohs(eh->ether_type);
605 	ehlen = sizeof(*eh);
606 
607 	if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
608 		rnd_add_data(NULL, eh, ehlen, 0);
609 		earlypkts++;
610 	}
611 
612 	/*
613 	 * Determine if the packet is within its size limits.
614 	 */
615 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
616 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
617 		mutex_enter(&bigpktpps_lock);
618 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
619 			    bigpktppslim)) {
620 			printf("%s: discarding oversize frame (len=%d)\n",
621 			    ifp->if_xname, m->m_pkthdr.len);
622 		}
623 		mutex_exit(&bigpktpps_lock);
624 		m_freem(m);
625 		return;
626 	}
627 
628 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
629 		/*
630 		 * If this is not a simplex interface, drop the packet
631 		 * if it came from us.
632 		 */
633 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
634 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
635 		    ETHER_ADDR_LEN) == 0) {
636 			m_freem(m);
637 			return;
638 		}
639 
640 		if (memcmp(etherbroadcastaddr,
641 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
642 			m->m_flags |= M_BCAST;
643 		else
644 			m->m_flags |= M_MCAST;
645 		ifp->if_imcasts++;
646 	}
647 
648 	/* If the CRC is still on the packet, trim it off. */
649 	if (m->m_flags & M_HASFCS) {
650 		m_adj(m, -ETHER_CRC_LEN);
651 		m->m_flags &= ~M_HASFCS;
652 	}
653 
654 	ifp->if_ibytes += m->m_pkthdr.len;
655 
656 #if NCARP > 0
657 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
658 		/*
659 		 * clear M_PROMISC, in case the packets comes from a
660 		 * vlan
661 		 */
662 		m->m_flags &= ~M_PROMISC;
663 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
664 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
665 			return;
666 	}
667 #endif /* NCARP > 0 */
668 	if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
669 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
670 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
671 		   ETHER_ADDR_LEN) != 0) {
672 		m->m_flags |= M_PROMISC;
673 	}
674 
675 	if ((m->m_flags & M_PROMISC) == 0) {
676 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
677 			return;
678 		if (m == NULL)
679 			return;
680 
681 		eh = mtod(m, struct ether_header *);
682 		etype = ntohs(eh->ether_type);
683 		ehlen = sizeof(*eh);
684 	}
685 
686 #if NAGR > 0
687 	if (ifp->if_agrprivate &&
688 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
689 		m->m_flags &= ~M_PROMISC;
690 		agr_input(ifp, m);
691 		return;
692 	}
693 #endif /* NAGR > 0 */
694 
695 	/*
696 	 * If VLANs are configured on the interface, check to
697 	 * see if the device performed the decapsulation and
698 	 * provided us with the tag.
699 	 */
700 	if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
701 #if NVLAN > 0
702 		/*
703 		 * vlan_input() will either recursively call ether_input()
704 		 * or drop the packet.
705 		 */
706 		vlan_input(ifp, m);
707 #else
708 		m_freem(m);
709 #endif
710 		return;
711 	}
712 
713 	/*
714 	 * Handle protocols that expect to have the Ethernet header
715 	 * (and possibly FCS) intact.
716 	 */
717 	switch (etype) {
718 	case ETHERTYPE_VLAN: {
719 		struct ether_vlan_header *evl = (void *)eh;
720 		/*
721 		 * If there is a tag of 0, then the VLAN header was probably
722 		 * just being used to store the priority.  Extract the ether
723 		 * type, and if IP or IPV6, let them deal with it.
724 		 */
725 		if (m->m_len <= sizeof(*evl)
726 		    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
727 			etype = ntohs(evl->evl_proto);
728 			ehlen = sizeof(*evl);
729 			if ((m->m_flags & M_PROMISC) == 0
730 			    && (etype == ETHERTYPE_IP
731 				|| etype == ETHERTYPE_IPV6))
732 				break;
733 		}
734 #if NVLAN > 0
735 		/*
736 		 * vlan_input() will either recursively call ether_input()
737 		 * or drop the packet.
738 		 */
739 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
740 			vlan_input(ifp, m);
741 		else
742 #endif /* NVLAN > 0 */
743 			m_freem(m);
744 		return;
745 	}
746 #if NPPPOE > 0
747 	case ETHERTYPE_PPPOEDISC:
748 	case ETHERTYPE_PPPOE:
749 		if (m->m_flags & M_PROMISC) {
750 			m_freem(m);
751 			return;
752 		}
753 #ifndef PPPOE_SERVER
754 		if (m->m_flags & (M_MCAST | M_BCAST)) {
755 			m_freem(m);
756 			return;
757 		}
758 #endif
759 
760 		if (etype == ETHERTYPE_PPPOEDISC)
761 			inq = &ppoediscinq;
762 		else
763 			inq = &ppoeinq;
764 		if (IF_QFULL(inq)) {
765 			IF_DROP(inq);
766 			m_freem(m);
767 		} else {
768 			IF_ENQUEUE(inq, m);
769 			softint_schedule(pppoe_softintr);
770 		}
771 		return;
772 #endif /* NPPPOE > 0 */
773 	case ETHERTYPE_SLOWPROTOCOLS: {
774 		uint8_t subtype;
775 
776 #if defined(DIAGNOSTIC)
777 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
778 			panic("ether_input: too short slow protocol packet");
779 		}
780 #endif
781 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
782 		switch (subtype) {
783 #if NAGR > 0
784 		case SLOWPROTOCOLS_SUBTYPE_LACP:
785 			if (ifp->if_agrprivate) {
786 				ieee8023ad_lacp_input(ifp, m);
787 				return;
788 			}
789 			break;
790 
791 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
792 			if (ifp->if_agrprivate) {
793 				ieee8023ad_marker_input(ifp, m);
794 				return;
795 			}
796 			break;
797 #endif /* NAGR > 0 */
798 		default:
799 			if (subtype == 0 || subtype > 10) {
800 				/* illegal value */
801 				m_freem(m);
802 				return;
803 			}
804 			/* unknown subtype */
805 			break;
806 		}
807 		/* FALLTHROUGH */
808 	}
809 	default:
810 		if (m->m_flags & M_PROMISC) {
811 			m_freem(m);
812 			return;
813 		}
814 	}
815 
816 	/* If the CRC is still on the packet, trim it off. */
817 	if (m->m_flags & M_HASFCS) {
818 		m_adj(m, -ETHER_CRC_LEN);
819 		m->m_flags &= ~M_HASFCS;
820 	}
821 
822 	if (etype > ETHERMTU + sizeof (struct ether_header)) {
823 		/* Strip off the Ethernet header. */
824 		m_adj(m, ehlen);
825 
826 		switch (etype) {
827 #ifdef INET
828 		case ETHERTYPE_IP:
829 #ifdef GATEWAY
830 			if (ipflow_fastforward(m))
831 				return;
832 #endif
833 			pktq = ip_pktq;
834 			break;
835 
836 		case ETHERTYPE_ARP:
837 			isr = NETISR_ARP;
838 			inq = &arpintrq;
839 			break;
840 
841 		case ETHERTYPE_REVARP:
842 			revarpinput(m);	/* XXX queue? */
843 			return;
844 #endif
845 #ifdef INET6
846 		case ETHERTYPE_IPV6:
847 			if (__predict_false(!in6_present)) {
848 				m_freem(m);
849 				return;
850 			}
851 #ifdef GATEWAY
852 			if (ip6flow_fastforward(&m))
853 				return;
854 #endif
855 			pktq = ip6_pktq;
856 			break;
857 #endif
858 #ifdef IPX
859 		case ETHERTYPE_IPX:
860 			isr = NETISR_IPX;
861 			inq = &ipxintrq;
862 			break;
863 #endif
864 #ifdef NETATALK
865 		case ETHERTYPE_ATALK:
866 			isr = NETISR_ATALK;
867 			inq = &atintrq1;
868 			break;
869 		case ETHERTYPE_AARP:
870 			/* probably this should be done with a NETISR as well */
871 			aarpinput(ifp, m); /* XXX */
872 			return;
873 #endif /* NETATALK */
874 #ifdef MPLS
875 		case ETHERTYPE_MPLS:
876 			isr = NETISR_MPLS;
877 			inq = &mplsintrq;
878 			break;
879 #endif
880 		default:
881 			m_freem(m);
882 			return;
883 		}
884 	} else {
885 #if defined (LLC) || defined (NETATALK)
886 		l = (struct llc *)(eh+1);
887 		switch (l->llc_dsap) {
888 #ifdef NETATALK
889 		case LLC_SNAP_LSAP:
890 			switch (l->llc_control) {
891 			case LLC_UI:
892 				if (l->llc_ssap != LLC_SNAP_LSAP) {
893 					goto dropanyway;
894 				}
895 
896 				if (memcmp(&(l->llc_snap_org_code)[0],
897 				    at_org_code, sizeof(at_org_code)) == 0 &&
898 				    ntohs(l->llc_snap_ether_type) ==
899 				    ETHERTYPE_ATALK) {
900 					inq = &atintrq2;
901 					m_adj(m, sizeof(struct ether_header)
902 					    + sizeof(struct llc));
903 					isr = NETISR_ATALK;
904 					break;
905 				}
906 
907 				if (memcmp(&(l->llc_snap_org_code)[0],
908 				    aarp_org_code,
909 				    sizeof(aarp_org_code)) == 0 &&
910 				    ntohs(l->llc_snap_ether_type) ==
911 				    ETHERTYPE_AARP) {
912 					m_adj( m, sizeof(struct ether_header)
913 					    + sizeof(struct llc));
914 					aarpinput(ifp, m); /* XXX */
915 				    return;
916 				}
917 
918 			default:
919 				goto dropanyway;
920 			}
921 			break;
922 		dropanyway:
923 #endif
924 		default:
925 			m_freem(m);
926 			return;
927 		}
928 #else /* ISO || LLC || NETATALK*/
929 		m_freem(m);
930 		return;
931 #endif /* ISO || LLC || NETATALK*/
932 	}
933 
934 	if (__predict_true(pktq)) {
935 		const uint32_t h = pktq_rps_hash(m);
936 		if (__predict_false(!pktq_enqueue(pktq, m, h))) {
937 			m_freem(m);
938 		}
939 		return;
940 	}
941 
942 	if (__predict_false(!inq)) {
943 		/* Should not happen. */
944 		m_freem(m);
945 		return;
946 	}
947 	if (IF_QFULL(inq)) {
948 		IF_DROP(inq);
949 		m_freem(m);
950 	} else {
951 		IF_ENQUEUE(inq, m);
952 		schednetisr(isr);
953 	}
954 }
955 
956 /*
957  * Convert Ethernet address to printable (loggable) representation.
958  */
959 char *
960 ether_sprintf(const u_char *ap)
961 {
962 	static char etherbuf[3 * ETHER_ADDR_LEN];
963 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
964 }
965 
966 char *
967 ether_snprintf(char *buf, size_t len, const u_char *ap)
968 {
969 	char *cp = buf;
970 	size_t i;
971 
972 	for (i = 0; i < len / 3; i++) {
973 		*cp++ = hexdigits[*ap >> 4];
974 		*cp++ = hexdigits[*ap++ & 0xf];
975 		*cp++ = ':';
976 	}
977 	*--cp = '\0';
978 	return buf;
979 }
980 
981 /*
982  * Perform common duties while attaching to interface list
983  */
984 void
985 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
986 {
987 	struct ethercom *ec = (struct ethercom *)ifp;
988 
989 	ifp->if_type = IFT_ETHER;
990 	ifp->if_hdrlen = ETHER_HDR_LEN;
991 	ifp->if_dlt = DLT_EN10MB;
992 	ifp->if_mtu = ETHERMTU;
993 	ifp->if_output = ether_output;
994 	ifp->if_input = ether_input;
995 	if (ifp->if_baudrate == 0)
996 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
997 
998 	if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
999 
1000 	LIST_INIT(&ec->ec_multiaddrs);
1001 	ifp->if_broadcastaddr = etherbroadcastaddr;
1002 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1003 #ifdef MBUFTRACE
1004 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
1005 	    sizeof(ec->ec_tx_mowner.mo_name));
1006 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
1007 	    sizeof(ec->ec_tx_mowner.mo_descr));
1008 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
1009 	    sizeof(ec->ec_rx_mowner.mo_name));
1010 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
1011 	    sizeof(ec->ec_rx_mowner.mo_descr));
1012 	MOWNER_ATTACH(&ec->ec_tx_mowner);
1013 	MOWNER_ATTACH(&ec->ec_rx_mowner);
1014 	ifp->if_mowner = &ec->ec_tx_mowner;
1015 #endif
1016 }
1017 
1018 void
1019 ether_ifdetach(struct ifnet *ifp)
1020 {
1021 	struct ethercom *ec = (void *) ifp;
1022 	struct ether_multi *enm;
1023 	int s;
1024 
1025 	/*
1026 	 * Prevent further calls to ioctl (for example turning off
1027 	 * promiscuous mode from the bridge code), which eventually can
1028 	 * call if_init() which can cause panics because the interface
1029 	 * is in the process of being detached. Return device not configured
1030 	 * instead.
1031 	 */
1032 	ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
1033 
1034 #if NBRIDGE > 0
1035 	if (ifp->if_bridge)
1036 		bridge_ifdetach(ifp);
1037 #endif
1038 
1039 	bpf_detach(ifp);
1040 
1041 #if NVLAN > 0
1042 	if (ec->ec_nvlans)
1043 		vlan_ifdetach(ifp);
1044 #endif
1045 
1046 	s = splnet();
1047 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1048 		LIST_REMOVE(enm, enm_list);
1049 		free(enm, M_IFMADDR);
1050 		ec->ec_multicnt--;
1051 	}
1052 	splx(s);
1053 
1054 	ifp->if_mowner = NULL;
1055 	MOWNER_DETACH(&ec->ec_rx_mowner);
1056 	MOWNER_DETACH(&ec->ec_tx_mowner);
1057 }
1058 
1059 #if 0
1060 /*
1061  * This is for reference.  We have a table-driven version
1062  * of the little-endian crc32 generator, which is faster
1063  * than the double-loop.
1064  */
1065 uint32_t
1066 ether_crc32_le(const uint8_t *buf, size_t len)
1067 {
1068 	uint32_t c, crc, carry;
1069 	size_t i, j;
1070 
1071 	crc = 0xffffffffU;	/* initial value */
1072 
1073 	for (i = 0; i < len; i++) {
1074 		c = buf[i];
1075 		for (j = 0; j < 8; j++) {
1076 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1077 			crc >>= 1;
1078 			c >>= 1;
1079 			if (carry)
1080 				crc = (crc ^ ETHER_CRC_POLY_LE);
1081 		}
1082 	}
1083 
1084 	return (crc);
1085 }
1086 #else
1087 uint32_t
1088 ether_crc32_le(const uint8_t *buf, size_t len)
1089 {
1090 	static const uint32_t crctab[] = {
1091 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1092 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1093 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1094 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1095 	};
1096 	uint32_t crc;
1097 	size_t i;
1098 
1099 	crc = 0xffffffffU;	/* initial value */
1100 
1101 	for (i = 0; i < len; i++) {
1102 		crc ^= buf[i];
1103 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1104 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1105 	}
1106 
1107 	return (crc);
1108 }
1109 #endif
1110 
1111 uint32_t
1112 ether_crc32_be(const uint8_t *buf, size_t len)
1113 {
1114 	uint32_t c, crc, carry;
1115 	size_t i, j;
1116 
1117 	crc = 0xffffffffU;	/* initial value */
1118 
1119 	for (i = 0; i < len; i++) {
1120 		c = buf[i];
1121 		for (j = 0; j < 8; j++) {
1122 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1123 			crc <<= 1;
1124 			c >>= 1;
1125 			if (carry)
1126 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1127 		}
1128 	}
1129 
1130 	return (crc);
1131 }
1132 
1133 #ifdef INET
1134 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1135     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1136 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1137     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1138 #endif
1139 #ifdef INET6
1140 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1141     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1142 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1143     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1144 #endif
1145 
1146 /*
1147  * ether_aton implementation, not using a static buffer.
1148  */
1149 int
1150 ether_aton_r(u_char *dest, size_t len, const char *str)
1151 {
1152         const u_char *cp = (const void *)str;
1153 	u_char *ep;
1154 
1155 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1156 
1157 	if (len < ETHER_ADDR_LEN)
1158 		return ENOSPC;
1159 
1160 	ep = dest + ETHER_ADDR_LEN;
1161 
1162 	while (*cp) {
1163                 if (!isxdigit(*cp))
1164                         return EINVAL;
1165 		*dest = atox(*cp);
1166 		cp++;
1167                 if (isxdigit(*cp)) {
1168                         *dest = (*dest << 4) | atox(*cp);
1169 			dest++;
1170 			cp++;
1171                 } else
1172 			dest++;
1173 		if (dest == ep)
1174 			return *cp == '\0' ? 0 : ENAMETOOLONG;
1175 		switch (*cp) {
1176 		case ':':
1177 		case '-':
1178 		case '.':
1179 			cp++;
1180 			break;
1181 		}
1182         }
1183 	return ENOBUFS;
1184 }
1185 
1186 /*
1187  * Convert a sockaddr into an Ethernet address or range of Ethernet
1188  * addresses.
1189  */
1190 int
1191 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1192     uint8_t addrhi[ETHER_ADDR_LEN])
1193 {
1194 #ifdef INET
1195 	const struct sockaddr_in *sin;
1196 #endif /* INET */
1197 #ifdef INET6
1198 	const struct sockaddr_in6 *sin6;
1199 #endif /* INET6 */
1200 
1201 	switch (sa->sa_family) {
1202 
1203 	case AF_UNSPEC:
1204 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1205 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1206 		break;
1207 
1208 #ifdef INET
1209 	case AF_INET:
1210 		sin = satocsin(sa);
1211 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1212 			/*
1213 			 * An IP address of INADDR_ANY means listen to
1214 			 * or stop listening to all of the Ethernet
1215 			 * multicast addresses used for IP.
1216 			 * (This is for the sake of IP multicast routers.)
1217 			 */
1218 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1219 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1220 		}
1221 		else {
1222 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1223 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1224 		}
1225 		break;
1226 #endif
1227 #ifdef INET6
1228 	case AF_INET6:
1229 		sin6 = satocsin6(sa);
1230 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1231 			/*
1232 			 * An IP6 address of 0 means listen to or stop
1233 			 * listening to all of the Ethernet multicast
1234 			 * address used for IP6.
1235 			 * (This is used for multicast routers.)
1236 			 */
1237 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1238 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1239 		} else {
1240 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1241 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1242 		}
1243 		break;
1244 #endif
1245 
1246 	default:
1247 		return EAFNOSUPPORT;
1248 	}
1249 	return 0;
1250 }
1251 
1252 /*
1253  * Add an Ethernet multicast address or range of addresses to the list for a
1254  * given interface.
1255  */
1256 int
1257 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1258 {
1259 	struct ether_multi *enm;
1260 	u_char addrlo[ETHER_ADDR_LEN];
1261 	u_char addrhi[ETHER_ADDR_LEN];
1262 	int s = splnet(), error;
1263 
1264 	error = ether_multiaddr(sa, addrlo, addrhi);
1265 	if (error != 0) {
1266 		splx(s);
1267 		return error;
1268 	}
1269 
1270 	/*
1271 	 * Verify that we have valid Ethernet multicast addresses.
1272 	 */
1273 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1274 		splx(s);
1275 		return EINVAL;
1276 	}
1277 	/*
1278 	 * See if the address range is already in the list.
1279 	 */
1280 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1281 	if (enm != NULL) {
1282 		/*
1283 		 * Found it; just increment the reference count.
1284 		 */
1285 		++enm->enm_refcount;
1286 		splx(s);
1287 		return 0;
1288 	}
1289 	/*
1290 	 * New address or range; malloc a new multicast record
1291 	 * and link it into the interface's multicast list.
1292 	 */
1293 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1294 	if (enm == NULL) {
1295 		splx(s);
1296 		return ENOBUFS;
1297 	}
1298 	memcpy(enm->enm_addrlo, addrlo, 6);
1299 	memcpy(enm->enm_addrhi, addrhi, 6);
1300 	enm->enm_refcount = 1;
1301 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1302 	ec->ec_multicnt++;
1303 	splx(s);
1304 	/*
1305 	 * Return ENETRESET to inform the driver that the list has changed
1306 	 * and its reception filter should be adjusted accordingly.
1307 	 */
1308 	return ENETRESET;
1309 }
1310 
1311 /*
1312  * Delete a multicast address record.
1313  */
1314 int
1315 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1316 {
1317 	struct ether_multi *enm;
1318 	u_char addrlo[ETHER_ADDR_LEN];
1319 	u_char addrhi[ETHER_ADDR_LEN];
1320 	int s = splnet(), error;
1321 
1322 	error = ether_multiaddr(sa, addrlo, addrhi);
1323 	if (error != 0) {
1324 		splx(s);
1325 		return (error);
1326 	}
1327 
1328 	/*
1329 	 * Look ur the address in our list.
1330 	 */
1331 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1332 	if (enm == NULL) {
1333 		splx(s);
1334 		return (ENXIO);
1335 	}
1336 	if (--enm->enm_refcount != 0) {
1337 		/*
1338 		 * Still some claims to this record.
1339 		 */
1340 		splx(s);
1341 		return (0);
1342 	}
1343 	/*
1344 	 * No remaining claims to this record; unlink and free it.
1345 	 */
1346 	LIST_REMOVE(enm, enm_list);
1347 	free(enm, M_IFMADDR);
1348 	ec->ec_multicnt--;
1349 	splx(s);
1350 	/*
1351 	 * Return ENETRESET to inform the driver that the list has changed
1352 	 * and its reception filter should be adjusted accordingly.
1353 	 */
1354 	return (ENETRESET);
1355 }
1356 
1357 void
1358 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1359 {
1360 	ec->ec_ifflags_cb = cb;
1361 }
1362 
1363 /*
1364  * Common ioctls for Ethernet interfaces.  Note, we must be
1365  * called at splnet().
1366  */
1367 int
1368 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1369 {
1370 	struct ethercom *ec = (void *) ifp;
1371 	struct eccapreq *eccr;
1372 	struct ifreq *ifr = (struct ifreq *)data;
1373 	struct if_laddrreq *iflr = data;
1374 	const struct sockaddr_dl *sdl;
1375 	static const uint8_t zero[ETHER_ADDR_LEN];
1376 	int error;
1377 
1378 	switch (cmd) {
1379 	case SIOCINITIFADDR:
1380 	    {
1381 		struct ifaddr *ifa = (struct ifaddr *)data;
1382 		if (ifa->ifa_addr->sa_family != AF_LINK
1383 		    && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1384 		       (IFF_UP|IFF_RUNNING)) {
1385 			ifp->if_flags |= IFF_UP;
1386 			if ((error = (*ifp->if_init)(ifp)) != 0)
1387 				return error;
1388 		}
1389 #ifdef INET
1390 		if (ifa->ifa_addr->sa_family == AF_INET)
1391 			arp_ifinit(ifp, ifa);
1392 #endif /* INET */
1393 		return 0;
1394 	    }
1395 
1396 	case SIOCSIFMTU:
1397 	    {
1398 		int maxmtu;
1399 
1400 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1401 			maxmtu = ETHERMTU_JUMBO;
1402 		else
1403 			maxmtu = ETHERMTU;
1404 
1405 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1406 			return EINVAL;
1407 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1408 			return error;
1409 		else if (ifp->if_flags & IFF_UP) {
1410 			/* Make sure the device notices the MTU change. */
1411 			return (*ifp->if_init)(ifp);
1412 		} else
1413 			return 0;
1414 	    }
1415 
1416 	case SIOCSIFFLAGS:
1417 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1418 			return error;
1419 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1420 		case IFF_RUNNING:
1421 			/*
1422 			 * If interface is marked down and it is running,
1423 			 * then stop and disable it.
1424 			 */
1425 			(*ifp->if_stop)(ifp, 1);
1426 			break;
1427 		case IFF_UP:
1428 			/*
1429 			 * If interface is marked up and it is stopped, then
1430 			 * start it.
1431 			 */
1432 			return (*ifp->if_init)(ifp);
1433 		case IFF_UP|IFF_RUNNING:
1434 			error = 0;
1435 			if (ec->ec_ifflags_cb == NULL ||
1436 			    (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1437 				/*
1438 				 * Reset the interface to pick up
1439 				 * changes in any other flags that
1440 				 * affect the hardware state.
1441 				 */
1442 				return (*ifp->if_init)(ifp);
1443 			} else
1444 				return error;
1445 		case 0:
1446 			break;
1447 		}
1448 		return 0;
1449 	case SIOCGETHERCAP:
1450 		eccr = (struct eccapreq *)data;
1451 		eccr->eccr_capabilities = ec->ec_capabilities;
1452 		eccr->eccr_capenable = ec->ec_capenable;
1453 		return 0;
1454 	case SIOCADDMULTI:
1455 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1456 	case SIOCDELMULTI:
1457 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1458 	case SIOCSIFMEDIA:
1459 	case SIOCGIFMEDIA:
1460 		if (ec->ec_mii == NULL)
1461 			return ENOTTY;
1462 		return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1463 	case SIOCALIFADDR:
1464 		sdl = satocsdl(sstocsa(&iflr->addr));
1465 		if (sdl->sdl_family != AF_LINK)
1466 			;
1467 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1468 			return EINVAL;
1469 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1470 			return EINVAL;
1471 		/*FALLTHROUGH*/
1472 	default:
1473 		return ifioctl_common(ifp, cmd, data);
1474 	}
1475 	return 0;
1476 }
1477 
1478 static int
1479 ether_multicast_sysctl(SYSCTLFN_ARGS)
1480 {
1481 	struct ether_multi *enm;
1482 	struct ether_multi_sysctl addr;
1483 	struct ifnet *ifp;
1484 	struct ethercom *ec;
1485 	int error;
1486 	size_t written;
1487 
1488 	if (namelen != 1)
1489 		return EINVAL;
1490 
1491 	ifp = if_byindex(name[0]);
1492 	if (ifp == NULL)
1493 		return ENODEV;
1494 	if (ifp->if_type != IFT_ETHER) {
1495 		*oldlenp = 0;
1496 		return 0;
1497 	}
1498 	ec = (struct ethercom *)ifp;
1499 
1500 	if (oldp == NULL) {
1501 		*oldlenp = ec->ec_multicnt * sizeof(addr);
1502 		return 0;
1503 	}
1504 
1505 	memset(&addr, 0, sizeof(addr));
1506 	error = 0;
1507 	written = 0;
1508 
1509 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1510 		if (written + sizeof(addr) > *oldlenp)
1511 			break;
1512 		addr.enm_refcount = enm->enm_refcount;
1513 		memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1514 		memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1515 		error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
1516 		if (error)
1517 			break;
1518 		written += sizeof(addr);
1519 		oldp = (char *)oldp + sizeof(addr);
1520 	}
1521 
1522 	*oldlenp = written;
1523 	return error;
1524 }
1525 
1526 SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
1527 {
1528 	const struct sysctlnode *rnode = NULL;
1529 
1530 	sysctl_createv(clog, 0, NULL, &rnode,
1531 		       CTLFLAG_PERMANENT,
1532 		       CTLTYPE_NODE, "ether",
1533 		       SYSCTL_DESCR("Ethernet-specific information"),
1534 		       NULL, 0, NULL, 0,
1535 		       CTL_NET, CTL_CREATE, CTL_EOL);
1536 
1537 	sysctl_createv(clog, 0, &rnode, NULL,
1538 		       CTLFLAG_PERMANENT,
1539 		       CTLTYPE_NODE, "multicast",
1540 		       SYSCTL_DESCR("multicast addresses"),
1541 		       ether_multicast_sysctl, 0, NULL, 0,
1542 		       CTL_CREATE, CTL_EOL);
1543 }
1544 
1545 void
1546 etherinit(void)
1547 {
1548 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1549 }
1550