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