Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.200
      1 /*	$NetBSD: if_ethersubr.c,v 1.200 2014/06/10 09:38:30 joerg 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.200 2014/06/10 09:38:30 joerg 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 NBRIDGE > 0
    644 	/*
    645 	 * Tap the packet off here for a bridge.  bridge_input()
    646 	 * will return NULL if it has consumed the packet, otherwise
    647 	 * it gets processed as normal.  Note that bridge_input()
    648 	 * will always return the original packet if we need to
    649 	 * process it locally.
    650 	 */
    651 	if (ifp->if_bridge) {
    652 		/* clear M_PROMISC, in case the packets comes from a vlan */
    653 		m->m_flags &= ~M_PROMISC;
    654 		m = bridge_input(ifp, m);
    655 		if (m == NULL)
    656 			return;
    657 
    658 		/*
    659 		 * Bridge has determined that the packet is for us.
    660 		 * Update our interface pointer -- we may have had
    661 		 * to "bridge" the packet locally.
    662 		 */
    663 		ifp = m->m_pkthdr.rcvif;
    664 	} else
    665 #endif /* NBRIDGE > 0 */
    666 	{
    667 
    668 #if NCARP > 0
    669 		if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    670 			/*
    671 			 * clear M_PROMISC, in case the packets comes from a
    672 			 * vlan
    673 			 */
    674 			m->m_flags &= ~M_PROMISC;
    675 			if (carp_input(m, (uint8_t *)&eh->ether_shost,
    676 			    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    677 				return;
    678 		}
    679 #endif /* NCARP > 0 */
    680 		if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
    681 		    (ifp->if_flags & IFF_PROMISC) != 0 &&
    682 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    683 			   ETHER_ADDR_LEN) != 0) {
    684 			m->m_flags |= M_PROMISC;
    685 		}
    686 	}
    687 
    688 	if ((m->m_flags & M_PROMISC) == 0) {
    689 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    690 			return;
    691 		if (m == NULL)
    692 			return;
    693 
    694 		eh = mtod(m, struct ether_header *);
    695 		etype = ntohs(eh->ether_type);
    696 		ehlen = sizeof(*eh);
    697 	}
    698 
    699 #if NAGR > 0
    700 	if (ifp->if_agrprivate &&
    701 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    702 		m->m_flags &= ~M_PROMISC;
    703 		agr_input(ifp, m);
    704 		return;
    705 	}
    706 #endif /* NAGR > 0 */
    707 
    708 	/*
    709 	 * If VLANs are configured on the interface, check to
    710 	 * see if the device performed the decapsulation and
    711 	 * provided us with the tag.
    712 	 */
    713 	if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
    714 #if NVLAN > 0
    715 		/*
    716 		 * vlan_input() will either recursively call ether_input()
    717 		 * or drop the packet.
    718 		 */
    719 		vlan_input(ifp, m);
    720 #else
    721 		m_freem(m);
    722 #endif
    723 		return;
    724 	}
    725 
    726 	/*
    727 	 * Handle protocols that expect to have the Ethernet header
    728 	 * (and possibly FCS) intact.
    729 	 */
    730 	switch (etype) {
    731 	case ETHERTYPE_VLAN: {
    732 		struct ether_vlan_header *evl = (void *)eh;
    733 		/*
    734 		 * If there is a tag of 0, then the VLAN header was probably
    735 		 * just being used to store the priority.  Extract the ether
    736 		 * type, and if IP or IPV6, let them deal with it.
    737 		 */
    738 		if (m->m_len <= sizeof(*evl)
    739 		    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
    740 			etype = ntohs(evl->evl_proto);
    741 			ehlen = sizeof(*evl);
    742 			if ((m->m_flags & M_PROMISC) == 0
    743 			    && (etype == ETHERTYPE_IP
    744 				|| etype == ETHERTYPE_IPV6))
    745 				break;
    746 		}
    747 #if NVLAN > 0
    748 		/*
    749 		 * vlan_input() will either recursively call ether_input()
    750 		 * or drop the packet.
    751 		 */
    752 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    753 			vlan_input(ifp, m);
    754 		else
    755 #endif /* NVLAN > 0 */
    756 			m_freem(m);
    757 		return;
    758 	}
    759 #if NPPPOE > 0
    760 	case ETHERTYPE_PPPOEDISC:
    761 	case ETHERTYPE_PPPOE:
    762 		if (m->m_flags & M_PROMISC) {
    763 			m_freem(m);
    764 			return;
    765 		}
    766 #ifndef PPPOE_SERVER
    767 		if (m->m_flags & (M_MCAST | M_BCAST)) {
    768 			m_freem(m);
    769 			return;
    770 		}
    771 #endif
    772 
    773 		if (etype == ETHERTYPE_PPPOEDISC)
    774 			inq = &ppoediscinq;
    775 		else
    776 			inq = &ppoeinq;
    777 		if (IF_QFULL(inq)) {
    778 			IF_DROP(inq);
    779 			m_freem(m);
    780 		} else
    781 			IF_ENQUEUE(inq, m);
    782 		softint_schedule(pppoe_softintr);
    783 		return;
    784 #endif /* NPPPOE > 0 */
    785 	case ETHERTYPE_SLOWPROTOCOLS: {
    786 		uint8_t subtype;
    787 
    788 #if defined(DIAGNOSTIC)
    789 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
    790 			panic("ether_input: too short slow protocol packet");
    791 		}
    792 #endif
    793 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    794 		switch (subtype) {
    795 #if NAGR > 0
    796 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    797 			if (ifp->if_agrprivate) {
    798 				ieee8023ad_lacp_input(ifp, m);
    799 				return;
    800 			}
    801 			break;
    802 
    803 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    804 			if (ifp->if_agrprivate) {
    805 				ieee8023ad_marker_input(ifp, m);
    806 				return;
    807 			}
    808 			break;
    809 #endif /* NAGR > 0 */
    810 		default:
    811 			if (subtype == 0 || subtype > 10) {
    812 				/* illegal value */
    813 				m_freem(m);
    814 				return;
    815 			}
    816 			/* unknown subtype */
    817 			break;
    818 		}
    819 		/* FALLTHROUGH */
    820 	}
    821 	default:
    822 		if (m->m_flags & M_PROMISC) {
    823 			m_freem(m);
    824 			return;
    825 		}
    826 	}
    827 
    828 	/* If the CRC is still on the packet, trim it off. */
    829 	if (m->m_flags & M_HASFCS) {
    830 		m_adj(m, -ETHER_CRC_LEN);
    831 		m->m_flags &= ~M_HASFCS;
    832 	}
    833 
    834 	if (etype > ETHERMTU + sizeof (struct ether_header)) {
    835 		/* Strip off the Ethernet header. */
    836 		m_adj(m, ehlen);
    837 
    838 		switch (etype) {
    839 #ifdef INET
    840 		case ETHERTYPE_IP:
    841 #ifdef GATEWAY
    842 			if (ipflow_fastforward(m))
    843 				return;
    844 #endif
    845 			pktq = ip_pktq;
    846 			break;
    847 
    848 		case ETHERTYPE_ARP:
    849 			isr = NETISR_ARP;
    850 			inq = &arpintrq;
    851 			break;
    852 
    853 		case ETHERTYPE_REVARP:
    854 			revarpinput(m);	/* XXX queue? */
    855 			return;
    856 #endif
    857 #ifdef INET6
    858 		case ETHERTYPE_IPV6:
    859 			if (__predict_false(!in6_present)) {
    860 				m_freem(m);
    861 				return;
    862 			}
    863 #ifdef GATEWAY
    864 			if (ip6flow_fastforward(&m))
    865 				return;
    866 #endif
    867 			pktq = ip6_pktq;
    868 			break;
    869 #endif
    870 #ifdef IPX
    871 		case ETHERTYPE_IPX:
    872 			isr = NETISR_IPX;
    873 			inq = &ipxintrq;
    874 			break;
    875 #endif
    876 #ifdef NETATALK
    877 		case ETHERTYPE_ATALK:
    878 			isr = NETISR_ATALK;
    879 			inq = &atintrq1;
    880 			break;
    881 		case ETHERTYPE_AARP:
    882 			/* probably this should be done with a NETISR as well */
    883 			aarpinput(ifp, m); /* XXX */
    884 			return;
    885 #endif /* NETATALK */
    886 #ifdef MPLS
    887 		case ETHERTYPE_MPLS:
    888 			isr = NETISR_MPLS;
    889 			inq = &mplsintrq;
    890 			break;
    891 #endif
    892 		default:
    893 			m_freem(m);
    894 			return;
    895 		}
    896 	} else {
    897 #if defined (LLC) || defined (NETATALK)
    898 		l = (struct llc *)(eh+1);
    899 		switch (l->llc_dsap) {
    900 #ifdef NETATALK
    901 		case LLC_SNAP_LSAP:
    902 			switch (l->llc_control) {
    903 			case LLC_UI:
    904 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    905 					goto dropanyway;
    906 				}
    907 
    908 				if (memcmp(&(l->llc_snap_org_code)[0],
    909 				    at_org_code, sizeof(at_org_code)) == 0 &&
    910 				    ntohs(l->llc_snap_ether_type) ==
    911 				    ETHERTYPE_ATALK) {
    912 					inq = &atintrq2;
    913 					m_adj(m, sizeof(struct ether_header)
    914 					    + sizeof(struct llc));
    915 					isr = NETISR_ATALK;
    916 					break;
    917 				}
    918 
    919 				if (memcmp(&(l->llc_snap_org_code)[0],
    920 				    aarp_org_code,
    921 				    sizeof(aarp_org_code)) == 0 &&
    922 				    ntohs(l->llc_snap_ether_type) ==
    923 				    ETHERTYPE_AARP) {
    924 					m_adj( m, sizeof(struct ether_header)
    925 					    + sizeof(struct llc));
    926 					aarpinput(ifp, m); /* XXX */
    927 				    return;
    928 				}
    929 
    930 			default:
    931 				goto dropanyway;
    932 			}
    933 			break;
    934 		dropanyway:
    935 #endif
    936 		default:
    937 			m_freem(m);
    938 			return;
    939 		}
    940 #else /* ISO || LLC || NETATALK*/
    941 		m_freem(m);
    942 		return;
    943 #endif /* ISO || LLC || NETATALK*/
    944 	}
    945 
    946 	if (__predict_true(pktq)) {
    947 		const uint32_t h = pktq_rps_hash(m);
    948 		if (__predict_false(!pktq_enqueue(pktq, m, h))) {
    949 			m_freem(m);
    950 		}
    951 		return;
    952 	}
    953 
    954 	if (__predict_false(!inq)) {
    955 		/* Should not happen. */
    956 		m_freem(m);
    957 		return;
    958 	}
    959 	if (IF_QFULL(inq)) {
    960 		IF_DROP(inq);
    961 		m_freem(m);
    962 	} else {
    963 		IF_ENQUEUE(inq, m);
    964 		schednetisr(isr);
    965 	}
    966 }
    967 
    968 /*
    969  * Convert Ethernet address to printable (loggable) representation.
    970  */
    971 char *
    972 ether_sprintf(const u_char *ap)
    973 {
    974 	static char etherbuf[3 * ETHER_ADDR_LEN];
    975 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
    976 }
    977 
    978 char *
    979 ether_snprintf(char *buf, size_t len, const u_char *ap)
    980 {
    981 	char *cp = buf;
    982 	size_t i;
    983 
    984 	for (i = 0; i < len / 3; i++) {
    985 		*cp++ = hexdigits[*ap >> 4];
    986 		*cp++ = hexdigits[*ap++ & 0xf];
    987 		*cp++ = ':';
    988 	}
    989 	*--cp = '\0';
    990 	return buf;
    991 }
    992 
    993 /*
    994  * Perform common duties while attaching to interface list
    995  */
    996 void
    997 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
    998 {
    999 	struct ethercom *ec = (struct ethercom *)ifp;
   1000 
   1001 	ifp->if_type = IFT_ETHER;
   1002 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1003 	ifp->if_dlt = DLT_EN10MB;
   1004 	ifp->if_mtu = ETHERMTU;
   1005 	ifp->if_output = ether_output;
   1006 	ifp->if_input = ether_input;
   1007 	if (ifp->if_baudrate == 0)
   1008 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1009 
   1010 	if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1011 
   1012 	LIST_INIT(&ec->ec_multiaddrs);
   1013 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1014 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1015 #ifdef MBUFTRACE
   1016 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
   1017 	    sizeof(ec->ec_tx_mowner.mo_name));
   1018 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
   1019 	    sizeof(ec->ec_tx_mowner.mo_descr));
   1020 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
   1021 	    sizeof(ec->ec_rx_mowner.mo_name));
   1022 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
   1023 	    sizeof(ec->ec_rx_mowner.mo_descr));
   1024 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1025 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1026 	ifp->if_mowner = &ec->ec_tx_mowner;
   1027 #endif
   1028 }
   1029 
   1030 void
   1031 ether_ifdetach(struct ifnet *ifp)
   1032 {
   1033 	struct ethercom *ec = (void *) ifp;
   1034 	struct ether_multi *enm;
   1035 	int s;
   1036 
   1037 	/*
   1038 	 * Prevent further calls to ioctl (for example turning off
   1039 	 * promiscuous mode from the bridge code), which eventually can
   1040 	 * call if_init() which can cause panics because the interface
   1041 	 * is in the process of being detached. Return device not configured
   1042 	 * instead.
   1043 	 */
   1044 	ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
   1045 
   1046 #if NBRIDGE > 0
   1047 	if (ifp->if_bridge)
   1048 		bridge_ifdetach(ifp);
   1049 #endif
   1050 
   1051 	bpf_detach(ifp);
   1052 
   1053 #if NVLAN > 0
   1054 	if (ec->ec_nvlans)
   1055 		vlan_ifdetach(ifp);
   1056 #endif
   1057 
   1058 	s = splnet();
   1059 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1060 		LIST_REMOVE(enm, enm_list);
   1061 		free(enm, M_IFMADDR);
   1062 		ec->ec_multicnt--;
   1063 	}
   1064 	splx(s);
   1065 
   1066 #if 0	/* done in if_detach() */
   1067 	if_free_sadl(ifp);
   1068 #endif
   1069 
   1070 	ifp->if_mowner = NULL;
   1071 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1072 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1073 }
   1074 
   1075 #if 0
   1076 /*
   1077  * This is for reference.  We have a table-driven version
   1078  * of the little-endian crc32 generator, which is faster
   1079  * than the double-loop.
   1080  */
   1081 uint32_t
   1082 ether_crc32_le(const uint8_t *buf, size_t len)
   1083 {
   1084 	uint32_t c, crc, carry;
   1085 	size_t i, j;
   1086 
   1087 	crc = 0xffffffffU;	/* initial value */
   1088 
   1089 	for (i = 0; i < len; i++) {
   1090 		c = buf[i];
   1091 		for (j = 0; j < 8; j++) {
   1092 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1093 			crc >>= 1;
   1094 			c >>= 1;
   1095 			if (carry)
   1096 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1097 		}
   1098 	}
   1099 
   1100 	return (crc);
   1101 }
   1102 #else
   1103 uint32_t
   1104 ether_crc32_le(const uint8_t *buf, size_t len)
   1105 {
   1106 	static const uint32_t crctab[] = {
   1107 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1108 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1109 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1110 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1111 	};
   1112 	uint32_t crc;
   1113 	size_t i;
   1114 
   1115 	crc = 0xffffffffU;	/* initial value */
   1116 
   1117 	for (i = 0; i < len; i++) {
   1118 		crc ^= buf[i];
   1119 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1120 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1121 	}
   1122 
   1123 	return (crc);
   1124 }
   1125 #endif
   1126 
   1127 uint32_t
   1128 ether_crc32_be(const uint8_t *buf, size_t len)
   1129 {
   1130 	uint32_t c, crc, carry;
   1131 	size_t i, j;
   1132 
   1133 	crc = 0xffffffffU;	/* initial value */
   1134 
   1135 	for (i = 0; i < len; i++) {
   1136 		c = buf[i];
   1137 		for (j = 0; j < 8; j++) {
   1138 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1139 			crc <<= 1;
   1140 			c >>= 1;
   1141 			if (carry)
   1142 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1143 		}
   1144 	}
   1145 
   1146 	return (crc);
   1147 }
   1148 
   1149 #ifdef INET
   1150 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1151     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1152 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1153     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1154 #endif
   1155 #ifdef INET6
   1156 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1157     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1158 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1159     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1160 #endif
   1161 
   1162 /*
   1163  * ether_aton implementation, not using a static buffer.
   1164  */
   1165 int
   1166 ether_aton_r(u_char *dest, size_t len, const char *str)
   1167 {
   1168         const u_char *cp = (const void *)str;
   1169 	u_char *ep;
   1170 
   1171 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1172 
   1173 	if (len < ETHER_ADDR_LEN)
   1174 		return ENOSPC;
   1175 
   1176 	ep = dest + ETHER_ADDR_LEN;
   1177 
   1178 	while (*cp) {
   1179                 if (!isxdigit(*cp))
   1180                         return EINVAL;
   1181 		*dest = atox(*cp);
   1182 		cp++;
   1183                 if (isxdigit(*cp)) {
   1184                         *dest = (*dest << 4) | atox(*cp);
   1185 			dest++;
   1186 			cp++;
   1187                 } else
   1188 			dest++;
   1189 		if (dest == ep)
   1190 			return *cp == '\0' ? 0 : ENAMETOOLONG;
   1191 		switch (*cp) {
   1192 		case ':':
   1193 		case '-':
   1194 		case '.':
   1195 			cp++;
   1196 			break;
   1197 		}
   1198         }
   1199 	return ENOBUFS;
   1200 }
   1201 
   1202 /*
   1203  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1204  * addresses.
   1205  */
   1206 int
   1207 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1208     uint8_t addrhi[ETHER_ADDR_LEN])
   1209 {
   1210 #ifdef INET
   1211 	const struct sockaddr_in *sin;
   1212 #endif /* INET */
   1213 #ifdef INET6
   1214 	const struct sockaddr_in6 *sin6;
   1215 #endif /* INET6 */
   1216 
   1217 	switch (sa->sa_family) {
   1218 
   1219 	case AF_UNSPEC:
   1220 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1221 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1222 		break;
   1223 
   1224 #ifdef INET
   1225 	case AF_INET:
   1226 		sin = satocsin(sa);
   1227 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1228 			/*
   1229 			 * An IP address of INADDR_ANY means listen to
   1230 			 * or stop listening to all of the Ethernet
   1231 			 * multicast addresses used for IP.
   1232 			 * (This is for the sake of IP multicast routers.)
   1233 			 */
   1234 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1235 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1236 		}
   1237 		else {
   1238 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1239 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1240 		}
   1241 		break;
   1242 #endif
   1243 #ifdef INET6
   1244 	case AF_INET6:
   1245 		sin6 = satocsin6(sa);
   1246 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1247 			/*
   1248 			 * An IP6 address of 0 means listen to or stop
   1249 			 * listening to all of the Ethernet multicast
   1250 			 * address used for IP6.
   1251 			 * (This is used for multicast routers.)
   1252 			 */
   1253 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1254 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1255 		} else {
   1256 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1257 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1258 		}
   1259 		break;
   1260 #endif
   1261 
   1262 	default:
   1263 		return EAFNOSUPPORT;
   1264 	}
   1265 	return 0;
   1266 }
   1267 
   1268 /*
   1269  * Add an Ethernet multicast address or range of addresses to the list for a
   1270  * given interface.
   1271  */
   1272 int
   1273 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1274 {
   1275 	struct ether_multi *enm;
   1276 	u_char addrlo[ETHER_ADDR_LEN];
   1277 	u_char addrhi[ETHER_ADDR_LEN];
   1278 	int s = splnet(), error;
   1279 
   1280 	error = ether_multiaddr(sa, addrlo, addrhi);
   1281 	if (error != 0) {
   1282 		splx(s);
   1283 		return error;
   1284 	}
   1285 
   1286 	/*
   1287 	 * Verify that we have valid Ethernet multicast addresses.
   1288 	 */
   1289 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1290 		splx(s);
   1291 		return EINVAL;
   1292 	}
   1293 	/*
   1294 	 * See if the address range is already in the list.
   1295 	 */
   1296 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1297 	if (enm != NULL) {
   1298 		/*
   1299 		 * Found it; just increment the reference count.
   1300 		 */
   1301 		++enm->enm_refcount;
   1302 		splx(s);
   1303 		return 0;
   1304 	}
   1305 	/*
   1306 	 * New address or range; malloc a new multicast record
   1307 	 * and link it into the interface's multicast list.
   1308 	 */
   1309 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1310 	if (enm == NULL) {
   1311 		splx(s);
   1312 		return ENOBUFS;
   1313 	}
   1314 	memcpy(enm->enm_addrlo, addrlo, 6);
   1315 	memcpy(enm->enm_addrhi, addrhi, 6);
   1316 	enm->enm_refcount = 1;
   1317 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1318 	ec->ec_multicnt++;
   1319 	splx(s);
   1320 	/*
   1321 	 * Return ENETRESET to inform the driver that the list has changed
   1322 	 * and its reception filter should be adjusted accordingly.
   1323 	 */
   1324 	return ENETRESET;
   1325 }
   1326 
   1327 /*
   1328  * Delete a multicast address record.
   1329  */
   1330 int
   1331 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1332 {
   1333 	struct ether_multi *enm;
   1334 	u_char addrlo[ETHER_ADDR_LEN];
   1335 	u_char addrhi[ETHER_ADDR_LEN];
   1336 	int s = splnet(), error;
   1337 
   1338 	error = ether_multiaddr(sa, addrlo, addrhi);
   1339 	if (error != 0) {
   1340 		splx(s);
   1341 		return (error);
   1342 	}
   1343 
   1344 	/*
   1345 	 * Look ur the address in our list.
   1346 	 */
   1347 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1348 	if (enm == NULL) {
   1349 		splx(s);
   1350 		return (ENXIO);
   1351 	}
   1352 	if (--enm->enm_refcount != 0) {
   1353 		/*
   1354 		 * Still some claims to this record.
   1355 		 */
   1356 		splx(s);
   1357 		return (0);
   1358 	}
   1359 	/*
   1360 	 * No remaining claims to this record; unlink and free it.
   1361 	 */
   1362 	LIST_REMOVE(enm, enm_list);
   1363 	free(enm, M_IFMADDR);
   1364 	ec->ec_multicnt--;
   1365 	splx(s);
   1366 	/*
   1367 	 * Return ENETRESET to inform the driver that the list has changed
   1368 	 * and its reception filter should be adjusted accordingly.
   1369 	 */
   1370 	return (ENETRESET);
   1371 }
   1372 
   1373 void
   1374 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1375 {
   1376 	ec->ec_ifflags_cb = cb;
   1377 }
   1378 
   1379 /*
   1380  * Common ioctls for Ethernet interfaces.  Note, we must be
   1381  * called at splnet().
   1382  */
   1383 int
   1384 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1385 {
   1386 	struct ethercom *ec = (void *) ifp;
   1387 	struct eccapreq *eccr;
   1388 	struct ifreq *ifr = (struct ifreq *)data;
   1389 	struct if_laddrreq *iflr = data;
   1390 	const struct sockaddr_dl *sdl;
   1391 	static const uint8_t zero[ETHER_ADDR_LEN];
   1392 	int error;
   1393 
   1394 	switch (cmd) {
   1395 	case SIOCINITIFADDR:
   1396 	    {
   1397 		struct ifaddr *ifa = (struct ifaddr *)data;
   1398 		if (ifa->ifa_addr->sa_family != AF_LINK
   1399 		    && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
   1400 		       (IFF_UP|IFF_RUNNING)) {
   1401 			ifp->if_flags |= IFF_UP;
   1402 			if ((error = (*ifp->if_init)(ifp)) != 0)
   1403 				return error;
   1404 		}
   1405 #ifdef INET
   1406 		if (ifa->ifa_addr->sa_family == AF_INET)
   1407 			arp_ifinit(ifp, ifa);
   1408 #endif /* INET */
   1409 		return 0;
   1410 	    }
   1411 
   1412 	case SIOCSIFMTU:
   1413 	    {
   1414 		int maxmtu;
   1415 
   1416 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1417 			maxmtu = ETHERMTU_JUMBO;
   1418 		else
   1419 			maxmtu = ETHERMTU;
   1420 
   1421 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1422 			return EINVAL;
   1423 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1424 			return error;
   1425 		else if (ifp->if_flags & IFF_UP) {
   1426 			/* Make sure the device notices the MTU change. */
   1427 			return (*ifp->if_init)(ifp);
   1428 		} else
   1429 			return 0;
   1430 	    }
   1431 
   1432 	case SIOCSIFFLAGS:
   1433 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1434 			return error;
   1435 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   1436 		case IFF_RUNNING:
   1437 			/*
   1438 			 * If interface is marked down and it is running,
   1439 			 * then stop and disable it.
   1440 			 */
   1441 			(*ifp->if_stop)(ifp, 1);
   1442 			break;
   1443 		case IFF_UP:
   1444 			/*
   1445 			 * If interface is marked up and it is stopped, then
   1446 			 * start it.
   1447 			 */
   1448 			return (*ifp->if_init)(ifp);
   1449 		case IFF_UP|IFF_RUNNING:
   1450 			error = 0;
   1451 			if (ec->ec_ifflags_cb == NULL ||
   1452 			    (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
   1453 				/*
   1454 				 * Reset the interface to pick up
   1455 				 * changes in any other flags that
   1456 				 * affect the hardware state.
   1457 				 */
   1458 				return (*ifp->if_init)(ifp);
   1459 			} else
   1460 				return error;
   1461 		case 0:
   1462 			break;
   1463 		}
   1464 		return 0;
   1465 	case SIOCGETHERCAP:
   1466 		eccr = (struct eccapreq *)data;
   1467 		eccr->eccr_capabilities = ec->ec_capabilities;
   1468 		eccr->eccr_capenable = ec->ec_capenable;
   1469 		return 0;
   1470 	case SIOCADDMULTI:
   1471 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1472 	case SIOCDELMULTI:
   1473 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1474 	case SIOCSIFMEDIA:
   1475 	case SIOCGIFMEDIA:
   1476 		if (ec->ec_mii == NULL)
   1477 			return ENOTTY;
   1478 		return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
   1479 	case SIOCALIFADDR:
   1480 		sdl = satocsdl(sstocsa(&iflr->addr));
   1481 		if (sdl->sdl_family != AF_LINK)
   1482 			;
   1483 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1484 			return EINVAL;
   1485 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1486 			return EINVAL;
   1487 		/*FALLTHROUGH*/
   1488 	default:
   1489 		return ifioctl_common(ifp, cmd, data);
   1490 	}
   1491 	return 0;
   1492 }
   1493 
   1494 static int
   1495 ether_multicast_sysctl(SYSCTLFN_ARGS)
   1496 {
   1497 	struct ether_multi *enm;
   1498 	struct ether_multi_sysctl addr;
   1499 	struct ifnet *ifp;
   1500 	struct ethercom *ec;
   1501 	int error;
   1502 	size_t written;
   1503 
   1504 	if (namelen != 1)
   1505 		return EINVAL;
   1506 
   1507 	ifp = if_byindex(name[0]);
   1508 	if (ifp == NULL)
   1509 		return ENODEV;
   1510 	if (ifp->if_type != IFT_ETHER) {
   1511 		*oldlenp = 0;
   1512 		return 0;
   1513 	}
   1514 	ec = (struct ethercom *)ifp;
   1515 
   1516 	if (oldp == NULL) {
   1517 		*oldlenp = ec->ec_multicnt * sizeof(addr);
   1518 		return 0;
   1519 	}
   1520 
   1521 	memset(&addr, 0, sizeof(addr));
   1522 	error = 0;
   1523 	written = 0;
   1524 
   1525 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
   1526 		if (written + sizeof(addr) > *oldlenp)
   1527 			break;
   1528 		addr.enm_refcount = enm->enm_refcount;
   1529 		memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
   1530 		memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
   1531 		error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
   1532 		if (error)
   1533 			break;
   1534 		written += sizeof(addr);
   1535 		oldp = (char *)oldp + sizeof(addr);
   1536 	}
   1537 
   1538 	*oldlenp = written;
   1539 	return error;
   1540 }
   1541 
   1542 SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
   1543 {
   1544 	const struct sysctlnode *rnode = NULL;
   1545 
   1546 	sysctl_createv(clog, 0, NULL, &rnode,
   1547 		       CTLFLAG_PERMANENT,
   1548 		       CTLTYPE_NODE, "ether",
   1549 		       SYSCTL_DESCR("Ethernet-specific information"),
   1550 		       NULL, 0, NULL, 0,
   1551 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1552 
   1553 	sysctl_createv(clog, 0, &rnode, NULL,
   1554 		       CTLFLAG_PERMANENT,
   1555 		       CTLTYPE_NODE, "multicast",
   1556 		       SYSCTL_DESCR("multicast addresses"),
   1557 		       ether_multicast_sysctl, 0, NULL, 0,
   1558 		       CTL_CREATE, CTL_EOL);
   1559 }
   1560