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