Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.92
      1 /*	$NetBSD: if_ethersubr.c,v 1.92 2002/03/05 04:13:00 itojun 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. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.92 2002/03/05 04:13:00 itojun Exp $");
     69 
     70 #include "opt_inet.h"
     71 #include "opt_atalk.h"
     72 #include "opt_ccitt.h"
     73 #include "opt_llc.h"
     74 #include "opt_iso.h"
     75 #include "opt_ns.h"
     76 #include "opt_gateway.h"
     77 #include "opt_pfil_hooks.h"
     78 #include "vlan.h"
     79 #include "pppoe.h"
     80 #include "bridge.h"
     81 #include "bpfilter.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/callout.h>
     87 #include <sys/malloc.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/protosw.h>
     90 #include <sys/socket.h>
     91 #include <sys/ioctl.h>
     92 #include <sys/errno.h>
     93 #include <sys/syslog.h>
     94 
     95 #include <machine/cpu.h>
     96 
     97 #include <net/if.h>
     98 #include <net/netisr.h>
     99 #include <net/route.h>
    100 #include <net/if_llc.h>
    101 #include <net/if_dl.h>
    102 #include <net/if_types.h>
    103 
    104 #if NBPFILTER > 0
    105 #include <net/bpf.h>
    106 #endif
    107 
    108 #include <net/if_ether.h>
    109 #if NVLAN > 0
    110 #include <net/if_vlanvar.h>
    111 #endif
    112 
    113 #if NPPPOE > 0
    114 #include <net/if_pppoe.h>
    115 #endif
    116 
    117 #if NBRIDGE > 0
    118 #include <net/if_bridgevar.h>
    119 #endif
    120 
    121 #include <netinet/in.h>
    122 #ifdef INET
    123 #include <netinet/in_var.h>
    124 #endif
    125 #include <netinet/if_inarp.h>
    126 
    127 #ifdef INET6
    128 #ifndef INET
    129 #include <netinet/in.h>
    130 #endif
    131 #include <netinet6/in6_var.h>
    132 #include <netinet6/nd6.h>
    133 #endif
    134 
    135 #ifdef NS
    136 #include <netns/ns.h>
    137 #include <netns/ns_if.h>
    138 #endif
    139 
    140 #ifdef IPX
    141 #include <netipx/ipx.h>
    142 #include <netipx/ipx_if.h>
    143 #endif
    144 
    145 #ifdef ISO
    146 #include <netiso/argo_debug.h>
    147 #include <netiso/iso.h>
    148 #include <netiso/iso_var.h>
    149 #include <netiso/iso_snpac.h>
    150 #endif
    151 
    152 #ifdef LLC
    153 #include <netccitt/dll.h>
    154 #include <netccitt/llc_var.h>
    155 #endif
    156 
    157 #if defined(LLC) && defined(CCITT)
    158 extern struct ifqueue pkintrq;
    159 #endif
    160 
    161 #ifdef NETATALK
    162 #include <netatalk/at.h>
    163 #include <netatalk/at_var.h>
    164 #include <netatalk/at_extern.h>
    165 
    166 #define llc_snap_org_code llc_un.type_snap.org_code
    167 #define llc_snap_ether_type llc_un.type_snap.ether_type
    168 
    169 extern u_char	at_org_code[3];
    170 extern u_char	aarp_org_code[3];
    171 #endif /* NETATALK */
    172 
    173 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    174 #define senderr(e) { error = (e); goto bad;}
    175 
    176 #define SIN(x) ((struct sockaddr_in *)x)
    177 
    178 static	int ether_output __P((struct ifnet *, struct mbuf *,
    179 	    struct sockaddr *, struct rtentry *));
    180 static	void ether_input __P((struct ifnet *, struct mbuf *));
    181 
    182 /*
    183  * Ethernet output routine.
    184  * Encapsulate a packet of type family for the local net.
    185  * Assumes that ifp is actually pointer to ethercom structure.
    186  */
    187 static int
    188 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
    189 	struct rtentry *rt0)
    190 {
    191 	u_int16_t etype = 0;
    192 	int s, len, error = 0, hdrcmplt = 0;
    193  	u_char esrc[6], edst[6];
    194 	struct mbuf *m = m0;
    195 	struct rtentry *rt;
    196 	struct mbuf *mcopy = (struct mbuf *)0;
    197 	struct ether_header *eh;
    198 	ALTQ_DECL(struct altq_pktattr pktattr;)
    199 #ifdef INET
    200 	struct arphdr *ah;
    201 #endif /* INET */
    202 #ifdef NETATALK
    203 	struct at_ifaddr *aa;
    204 #endif /* NETATALK */
    205 	short mflags;
    206 
    207 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    208 		senderr(ENETDOWN);
    209 	if ((rt = rt0) != NULL) {
    210 		if ((rt->rt_flags & RTF_UP) == 0) {
    211 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    212 				rt->rt_refcnt--;
    213 				if (rt->rt_ifp != ifp)
    214 					return (*rt->rt_ifp->if_output)
    215 							(ifp, m0, dst, rt);
    216 			} else
    217 				senderr(EHOSTUNREACH);
    218 		}
    219 		if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
    220 			if (rt->rt_gwroute == 0)
    221 				goto lookup;
    222 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    223 				rtfree(rt); rt = rt0;
    224 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    225 				if ((rt = rt->rt_gwroute) == 0)
    226 					senderr(EHOSTUNREACH);
    227 				/* the "G" test below also prevents rt == rt0 */
    228 				if ((rt->rt_flags & RTF_GATEWAY) ||
    229 				    (rt->rt_ifp != ifp)) {
    230 					rt->rt_refcnt--;
    231 					rt0->rt_gwroute = 0;
    232 					senderr(EHOSTUNREACH);
    233 				}
    234 			}
    235 		}
    236 		if (rt->rt_flags & RTF_REJECT)
    237 			if (rt->rt_rmx.rmx_expire == 0 ||
    238 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    239 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    240 	}
    241 
    242 	switch (dst->sa_family) {
    243 
    244 #ifdef INET
    245 	case AF_INET:
    246 		if (m->m_flags & M_BCAST)
    247                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
    248 				sizeof(edst));
    249 
    250 		else if (m->m_flags & M_MCAST) {
    251 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
    252 			    (caddr_t)edst)
    253 
    254 		} else if (!arpresolve(ifp, rt, m, dst, edst))
    255 			return (0);	/* if not yet resolved */
    256 		/* If broadcasting on a simplex interface, loopback a copy */
    257 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    258 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    259 		etype = htons(ETHERTYPE_IP);
    260 		break;
    261 
    262 	case AF_ARP:
    263 		ah = mtod(m, struct arphdr *);
    264 		if (m->m_flags & M_BCAST)
    265                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
    266 				sizeof(edst));
    267 		else
    268 			bcopy((caddr_t)ar_tha(ah),
    269 				(caddr_t)edst, sizeof(edst));
    270 
    271 		ah->ar_hrd = htons(ARPHRD_ETHER);
    272 
    273 		switch(ntohs(ah->ar_op)) {
    274 		case ARPOP_REVREQUEST:
    275 		case ARPOP_REVREPLY:
    276 			etype = htons(ETHERTYPE_REVARP);
    277 			break;
    278 
    279 		case ARPOP_REQUEST:
    280 		case ARPOP_REPLY:
    281 		default:
    282 			etype = htons(ETHERTYPE_ARP);
    283 		}
    284 
    285 		break;
    286 #endif
    287 #ifdef INET6
    288 	case AF_INET6:
    289 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){
    290 			/* something bad happened */
    291 			return(0);
    292 		}
    293 		etype = htons(ETHERTYPE_IPV6);
    294 		break;
    295 #endif
    296 #ifdef NETATALK
    297     case AF_APPLETALK:
    298 		if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
    299 #ifdef NETATALKDEBUG
    300 			printf("aarpresolv failed\n");
    301 #endif /* NETATALKDEBUG */
    302 			return (0);
    303 		}
    304 		/*
    305 		 * ifaddr is the first thing in at_ifaddr
    306 		 */
    307 		aa = (struct at_ifaddr *) at_ifawithnet(
    308 		    (struct sockaddr_at *)dst, ifp);
    309 		if (aa == NULL)
    310 		    goto bad;
    311 
    312 		/*
    313 		 * In the phase 2 case, we need to prepend an mbuf for the
    314 		 * llc header.  Since we must preserve the value of m,
    315 		 * which is passed to us by value, we m_copy() the first
    316 		 * mbuf, and use it for our llc header.
    317 		 */
    318 		if (aa->aa_flags & AFA_PHASE2) {
    319 			struct llc llc;
    320 
    321 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    322 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
    323 			llc.llc_control = LLC_UI;
    324 			bcopy(at_org_code, llc.llc_snap_org_code,
    325 			    sizeof(llc.llc_snap_org_code));
    326 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
    327 			bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
    328 		} else {
    329 			etype = htons(ETHERTYPE_ATALK);
    330 		}
    331 		break;
    332 #endif /* NETATALK */
    333 #ifdef NS
    334 	case AF_NS:
    335 		etype = htons(ETHERTYPE_NS);
    336  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
    337 		    (caddr_t)edst, sizeof (edst));
    338 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
    339 			return (looutput(ifp, m, dst, rt));
    340 		/* If broadcasting on a simplex interface, loopback a copy */
    341 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    342 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    343 		break;
    344 #endif
    345 #ifdef IPX
    346 	case AF_IPX:
    347 		etype = htons(ETHERTYPE_IPX);
    348  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
    349 		    (caddr_t)edst, sizeof (edst));
    350 		/* If broadcasting on a simplex interface, loopback a copy */
    351 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    352 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    353 		break;
    354 #endif
    355 #ifdef	ISO
    356 	case AF_ISO: {
    357 		int	snpalen;
    358 		struct	llc *l;
    359 		struct sockaddr_dl *sdl;
    360 
    361 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
    362 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
    363 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
    364 		} else {
    365 			error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
    366 						(char *)edst, &snpalen);
    367 			if (error)
    368 				goto bad; /* Not Resolved */
    369 		}
    370 		/* If broadcasting on a simplex interface, loopback a copy */
    371 		if (*edst & 1)
    372 			m->m_flags |= (M_BCAST|M_MCAST);
    373 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
    374 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    375 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    376 			if (mcopy) {
    377 				eh = mtod(mcopy, struct ether_header *);
    378 				bcopy((caddr_t)edst,
    379 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    380 				bcopy(LLADDR(ifp->if_sadl),
    381 				      (caddr_t)eh->ether_shost, sizeof (edst));
    382 			}
    383 		}
    384 		M_PREPEND(m, 3, M_DONTWAIT);
    385 		if (m == NULL)
    386 			return (0);
    387 		l = mtod(m, struct llc *);
    388 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
    389 		l->llc_control = LLC_UI;
    390 #ifdef ARGO_DEBUG
    391 		if (argo_debug[D_ETHER]) {
    392 			int i;
    393 			printf("unoutput: sending pkt to: ");
    394 			for (i=0; i<6; i++)
    395 				printf("%x ", edst[i] & 0xff);
    396 			printf("\n");
    397 		}
    398 #endif
    399 		} break;
    400 #endif /* ISO */
    401 #ifdef	LLC
    402 /*	case AF_NSAP: */
    403 	case AF_CCITT: {
    404 		struct sockaddr_dl *sdl =
    405 			(struct sockaddr_dl *) rt -> rt_gateway;
    406 
    407 		if (sdl && sdl->sdl_family == AF_LINK
    408 		    && sdl->sdl_alen > 0) {
    409 			bcopy(LLADDR(sdl), (char *)edst,
    410 				sizeof(edst));
    411 		} else goto bad; /* Not a link interface ? Funny ... */
    412 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
    413 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    414 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    415 			if (mcopy) {
    416 				eh = mtod(mcopy, struct ether_header *);
    417 				bcopy((caddr_t)edst,
    418 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    419 				bcopy(LLADDR(ifp->if_sadl),
    420 				      (caddr_t)eh->ether_shost, sizeof (edst));
    421 			}
    422 		}
    423 #ifdef LLC_DEBUG
    424 		{
    425 			int i;
    426 			struct llc *l = mtod(m, struct llc *);
    427 
    428 			printf("ether_output: sending LLC2 pkt to: ");
    429 			for (i=0; i<6; i++)
    430 				printf("%x ", edst[i] & 0xff);
    431 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
    432 			    m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
    433 			    l->llc_control & 0xff);
    434 
    435 		}
    436 #endif /* LLC_DEBUG */
    437 		} break;
    438 #endif /* LLC */
    439 
    440 	case pseudo_AF_HDRCMPLT:
    441 		hdrcmplt = 1;
    442 		eh = (struct ether_header *)dst->sa_data;
    443 		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc));
    444 		/* FALLTHROUGH */
    445 
    446 	case AF_UNSPEC:
    447 		eh = (struct ether_header *)dst->sa_data;
    448  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
    449 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    450 		etype = eh->ether_type;
    451 		break;
    452 
    453 	default:
    454 		printf("%s: can't handle af%d\n", ifp->if_xname,
    455 			dst->sa_family);
    456 		senderr(EAFNOSUPPORT);
    457 	}
    458 
    459 	if (mcopy)
    460 		(void) looutput(ifp, mcopy, dst, rt);
    461 
    462 	/* If no ether type is set, this must be a 802.2 formatted packet.
    463 	 */
    464 	if (etype == 0)
    465 		etype = htons(m->m_pkthdr.len);
    466 	/*
    467 	 * Add local net header.  If no space in first mbuf,
    468 	 * allocate another.
    469 	 */
    470 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
    471 	if (m == 0)
    472 		senderr(ENOBUFS);
    473 	eh = mtod(m, struct ether_header *);
    474 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
    475 		sizeof(eh->ether_type));
    476  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
    477 	if (hdrcmplt)
    478 		bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
    479 		    sizeof(eh->ether_shost));
    480 	else
    481 	 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost,
    482 		    sizeof(eh->ether_shost));
    483 
    484 #ifdef PFIL_HOOKS
    485 	if ((error = pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
    486 		return (error);
    487 	if (m == NULL)
    488 		return (0);
    489 #endif
    490 
    491 #if NBRIDGE > 0
    492 	/*
    493 	 * Bridges require special output handling.
    494 	 */
    495 	if (ifp->if_bridge)
    496 		return (bridge_output(ifp, m, NULL, NULL));
    497 #endif
    498 
    499 #ifdef ALTQ
    500 	/*
    501 	 * If ALTQ is enabled on the parent interface, do
    502 	 * classification; the queueing discipline might not
    503 	 * require classification, but might require the
    504 	 * address family/header pointer in the pktattr.
    505 	 */
    506 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    507 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
    508 #endif
    509 
    510 	mflags = m->m_flags;
    511 	len = m->m_pkthdr.len;
    512 	s = splnet();
    513 	/*
    514 	 * Queue message on interface, and start output if interface
    515 	 * not yet active.
    516 	 */
    517 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    518 	if (error) {
    519 		/* mbuf is already freed */
    520 		splx(s);
    521 		return (error);
    522 	}
    523 	ifp->if_obytes += len;
    524 	if (mflags & M_MCAST)
    525 		ifp->if_omcasts++;
    526 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    527 		(*ifp->if_start)(ifp);
    528 	splx(s);
    529 	return (error);
    530 
    531 bad:
    532 	if (m)
    533 		m_freem(m);
    534 	return (error);
    535 }
    536 
    537 #ifdef ALTQ
    538 /*
    539  * This routine is a slight hack to allow a packet to be classified
    540  * if the Ethernet headers are present.  It will go away when ALTQ's
    541  * classification engine understands link headers.
    542  */
    543 void
    544 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
    545     struct altq_pktattr *pktattr)
    546 {
    547 	struct ether_header *eh;
    548 	u_int16_t ether_type;
    549 	int hlen, af, hdrsize;
    550 	caddr_t hdr;
    551 
    552 	hlen = ETHER_HDR_LEN;
    553 	eh = mtod(m, struct ether_header *);
    554 
    555 	ether_type = htons(eh->ether_type);
    556 
    557 	if (ether_type < ETHERMTU) {
    558 		/* LLC/SNAP */
    559 		struct llc *llc = (struct llc *)(eh + 1);
    560 		hlen += 8;
    561 
    562 		if (m->m_len < hlen ||
    563 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    564 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    565 		    llc->llc_control != LLC_UI) {
    566 			/* Not SNAP. */
    567 			goto bad;
    568 		}
    569 
    570 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    571 	}
    572 
    573 	switch (ether_type) {
    574 	case ETHERTYPE_IP:
    575 		af = AF_INET;
    576 		hdrsize = 20;		/* sizeof(struct ip) */
    577 		break;
    578 
    579 	case ETHERTYPE_IPV6:
    580 		af = AF_INET6;
    581 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    582 		break;
    583 
    584 	default:
    585 		af = AF_UNSPEC;
    586 		hdrsize = 0;
    587 		break;
    588 	}
    589 
    590 	if (m->m_len < (hlen + hdrsize)) {
    591 		/*
    592 		 * Ethernet and protocol header not in a single
    593 		 * mbuf.  We can't cope with this situation right
    594 		 * now (but it shouldn't ever happen, really, anyhow).
    595 		 * XXX Should use m_pulldown().
    596 		 */
    597 #ifdef DEBUG
    598 		printf("altq_etherclassify: headers span multiple mbufs: "
    599 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
    600 #endif
    601 		goto bad;
    602 	}
    603 
    604 	m->m_data += hlen;
    605 	m->m_len -= hlen;
    606 
    607 	hdr = mtod(m, caddr_t);
    608 
    609 	if (ALTQ_NEEDS_CLASSIFY(ifq))
    610 		pktattr->pattr_class =
    611 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    612 	pktattr->pattr_af = af;
    613 	pktattr->pattr_hdr = hdr;
    614 
    615 	m->m_data -= hlen;
    616 	m->m_len += hlen;
    617 
    618 	return;
    619 
    620  bad:
    621 	pktattr->pattr_class = NULL;
    622 	pktattr->pattr_hdr = NULL;
    623 	pktattr->pattr_af = AF_UNSPEC;
    624 }
    625 #endif /* ALTQ */
    626 
    627 /*
    628  * Process a received Ethernet packet;
    629  * the packet is in the mbuf chain m with
    630  * the ether header.
    631  */
    632 static void
    633 ether_input(struct ifnet *ifp, struct mbuf *m)
    634 {
    635 	struct ethercom *ec = (struct ethercom *) ifp;
    636 	struct ifqueue *inq;
    637 	u_int16_t etype;
    638 	int s;
    639 	struct ether_header *eh;
    640 	struct mbuf *n;
    641 #if defined (ISO) || defined (LLC) || defined(NETATALK)
    642 	struct llc *l;
    643 #endif
    644 
    645 	if ((ifp->if_flags & IFF_UP) == 0) {
    646 		m_freem(m);
    647 		return;
    648 	}
    649 
    650 	eh = mtod(m, struct ether_header *);
    651 	etype = ntohs(eh->ether_type);
    652 
    653 	/*
    654 	 * Determine if the packet is within its size limits.
    655 	 */
    656 	if (m->m_pkthdr.len >
    657 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
    658 		printf("%s: discarding oversize frame (len=%d)\n",
    659 		    ifp->if_xname, m->m_pkthdr.len);
    660 		m_freem(m);
    661 		return;
    662 	}
    663 
    664 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    665 		/*
    666 		 * If this is not a simplex interface, drop the packet
    667 		 * if it came from us.
    668 		 */
    669 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
    670 		    memcmp(LLADDR(ifp->if_sadl), eh->ether_shost,
    671 		    ETHER_ADDR_LEN) == 0) {
    672 			m_freem(m);
    673 			return;
    674 		}
    675 
    676 		if (memcmp(etherbroadcastaddr,
    677 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    678 			m->m_flags |= M_BCAST;
    679 		else
    680 			m->m_flags |= M_MCAST;
    681 		ifp->if_imcasts++;
    682 	}
    683 
    684 	/* If the CRC is still on the packet, trim it off. */
    685 	if (m->m_flags & M_HASFCS) {
    686 		m_adj(m, -ETHER_CRC_LEN);
    687 		m->m_flags &= ~M_HASFCS;
    688 	}
    689 
    690 	ifp->if_ibytes += m->m_pkthdr.len;
    691 
    692 #if NBRIDGE > 0
    693 	/*
    694 	 * Tap the packet off here for a bridge.  bridge_input()
    695 	 * will return NULL if it has consumed the packet, otherwise
    696 	 * it gets processed as normal.  Note that bridge_input()
    697 	 * will always return the original packet if we need to
    698 	 * process it locally.
    699 	 */
    700 	if (ifp->if_bridge) {
    701 		m = bridge_input(ifp, m);
    702 		if (m == NULL)
    703 			return;
    704 
    705 		/*
    706 		 * Bridge has determined that the packet is for us.
    707 		 * Update our interface pointer -- we may have had
    708 		 * to "bridge" the packet locally.
    709 		 */
    710 		ifp = m->m_pkthdr.rcvif;
    711 	}
    712 #endif /* NBRIDGE > 0 */
    713 
    714 	/*
    715 	 * XXX This comparison is redundant if we are a bridge
    716 	 * XXX and processing the packet locally.
    717 	 */
    718 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
    719 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
    720 	    memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
    721 		   ETHER_ADDR_LEN) != 0) {
    722 		m_freem(m);
    723 		return;
    724 	}
    725 
    726 #ifdef PFIL_HOOKS
    727 	if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    728 		return;
    729 	if (m == NULL)
    730 		return;
    731 
    732 	eh = mtod(m, struct ether_header *);
    733 	etype = ntohs(eh->ether_type);
    734 #endif
    735 
    736 	/*
    737 	 * If VLANs are configured on the interface, check to
    738 	 * see if the device performed the decapsulation and
    739 	 * provided us with the tag.
    740 	 */
    741 	if (ec->ec_nvlans &&
    742 	    (n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN)) != NULL) {
    743 #if NVLAN > 0
    744 		/*
    745 		 * vlan_input() will either recursively call ether_input()
    746 		 * or drop the packet.
    747 		 */
    748 		vlan_input(ifp, m);
    749 #else
    750 		m_freem(m);
    751 #endif
    752 		return;
    753 	}
    754 
    755 	/*
    756 	 * Handle protocols that expect to have the Ethernet header
    757 	 * (and possibly FCS) intact.
    758 	 */
    759 	switch (etype) {
    760 #if NVLAN > 0
    761 	case ETHERTYPE_VLAN:
    762 		/*
    763 		 * vlan_input() will either recursively call ether_input()
    764 		 * or drop the packet.
    765 		 */
    766 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    767 			vlan_input(ifp, m);
    768 		else
    769 			m_freem(m);
    770 		return;
    771 #endif /* NVLAN > 0 */
    772 #if NPPPOE > 0
    773 	case ETHERTYPE_PPPOEDISC:
    774 	case ETHERTYPE_PPPOE:
    775 		if (etype == ETHERTYPE_PPPOEDISC)
    776 			inq = &ppoediscinq;
    777 		else
    778 			inq = &ppoeinq;
    779 		s = splnet();
    780 		if (IF_QFULL(inq)) {
    781 			IF_DROP(inq);
    782 			m_freem(m);
    783 		} else
    784 			IF_ENQUEUE(inq, m);
    785 		splx(s);
    786 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    787 		if (!callout_active(&pppoe_softintr))
    788 			callout_reset(&pppoe_softintr, 1, pppoe_softintr_handler, NULL);
    789 #else
    790 		softintr_schedule(pppoe_softintr);
    791 #endif
    792 		return;
    793 #endif /* NPPPOE > 0 */
    794 	default:
    795 		; /* Nothing. */
    796 	}
    797 
    798 	/* Strip off the Ethernet header. */
    799 	m_adj(m, sizeof(struct ether_header));
    800 
    801 	/* If the CRC is still on the packet, trim it off. */
    802 	if (m->m_flags & M_HASFCS) {
    803 		m_adj(m, -ETHER_CRC_LEN);
    804 		m->m_flags &= ~M_HASFCS;
    805 	}
    806 
    807 	switch (etype) {
    808 #ifdef INET
    809 	case ETHERTYPE_IP:
    810 #ifdef GATEWAY
    811 		if (ipflow_fastforward(m))
    812 			return;
    813 #endif
    814 		schednetisr(NETISR_IP);
    815 		inq = &ipintrq;
    816 		break;
    817 
    818 	case ETHERTYPE_ARP:
    819 		schednetisr(NETISR_ARP);
    820 		inq = &arpintrq;
    821 		break;
    822 
    823 	case ETHERTYPE_REVARP:
    824 		revarpinput(m);	/* XXX queue? */
    825 		return;
    826 #endif
    827 #ifdef INET6
    828 	case ETHERTYPE_IPV6:
    829 		schednetisr(NETISR_IPV6);
    830 		inq = &ip6intrq;
    831 		break;
    832 #endif
    833 #ifdef NS
    834 	case ETHERTYPE_NS:
    835 		schednetisr(NETISR_NS);
    836 		inq = &nsintrq;
    837 		break;
    838 
    839 #endif
    840 #ifdef IPX
    841 	case ETHERTYPE_IPX:
    842 		schednetisr(NETISR_IPX);
    843 		inq = &ipxintrq;
    844 		break;
    845 #endif
    846 #ifdef NETATALK
    847         case ETHERTYPE_ATALK:
    848                 schednetisr(NETISR_ATALK);
    849                 inq = &atintrq1;
    850                 break;
    851         case ETHERTYPE_AARP:
    852 		/* probably this should be done with a NETISR as well */
    853                 aarpinput(ifp, m); /* XXX */
    854                 return;
    855 #endif /* NETATALK */
    856 	default:
    857 #if defined (ISO) || defined (LLC) || defined (NETATALK)
    858 		if (etype > ETHERMTU)
    859 			goto dropanyway;
    860 		l = mtod(m, struct llc *);
    861 		switch (l->llc_dsap) {
    862 #ifdef NETATALK
    863 		case LLC_SNAP_LSAP:
    864 			switch (l->llc_control) {
    865 			case LLC_UI:
    866 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    867 					goto dropanyway;
    868 				}
    869 
    870 				if (Bcmp(&(l->llc_snap_org_code)[0],
    871 				    at_org_code, sizeof(at_org_code)) == 0 &&
    872 				    ntohs(l->llc_snap_ether_type) ==
    873 				    ETHERTYPE_ATALK) {
    874 					inq = &atintrq2;
    875 					m_adj(m, sizeof(struct llc));
    876 					schednetisr(NETISR_ATALK);
    877 					break;
    878 				}
    879 
    880 				if (Bcmp(&(l->llc_snap_org_code)[0],
    881 				    aarp_org_code,
    882 				    sizeof(aarp_org_code)) == 0 &&
    883 				    ntohs(l->llc_snap_ether_type) ==
    884 				    ETHERTYPE_AARP) {
    885 					m_adj( m, sizeof(struct llc));
    886 					aarpinput(ifp, m); /* XXX */
    887 				    return;
    888 				}
    889 
    890 			default:
    891 				goto dropanyway;
    892 			}
    893 			break;
    894 #endif /* NETATALK */
    895 #ifdef	ISO
    896 		case LLC_ISO_LSAP:
    897 			switch (l->llc_control) {
    898 			case LLC_UI:
    899 				/* LLC_UI_P forbidden in class 1 service */
    900 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
    901 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    902 					/* LSAP for ISO */
    903 					if (m->m_pkthdr.len > etype)
    904 						m_adj(m, etype - m->m_pkthdr.len);
    905 					m->m_data += 3;		/* XXX */
    906 					m->m_len -= 3;		/* XXX */
    907 					m->m_pkthdr.len -= 3;	/* XXX */
    908 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    909 					if (m == 0)
    910 						return;
    911 					*mtod(m, struct ether_header *) = *eh;
    912 #ifdef ARGO_DEBUG
    913 					if (argo_debug[D_ETHER])
    914 						printf("clnp packet");
    915 #endif
    916 					schednetisr(NETISR_ISO);
    917 					inq = &clnlintrq;
    918 					break;
    919 				}
    920 				goto dropanyway;
    921 
    922 			case LLC_XID:
    923 			case LLC_XID_P:
    924 				if(m->m_len < 6)
    925 					goto dropanyway;
    926 				l->llc_window = 0;
    927 				l->llc_fid = 9;
    928 				l->llc_class = 1;
    929 				l->llc_dsap = l->llc_ssap = 0;
    930 				/* Fall through to */
    931 			case LLC_TEST:
    932 			case LLC_TEST_P:
    933 			{
    934 				struct sockaddr sa;
    935 				struct ether_header *eh2;
    936 				int i;
    937 				u_char c = l->llc_dsap;
    938 
    939 				l->llc_dsap = l->llc_ssap;
    940 				l->llc_ssap = c;
    941 				if (m->m_flags & (M_BCAST | M_MCAST))
    942 					bcopy(LLADDR(ifp->if_sadl),
    943 					      (caddr_t)eh->ether_dhost, 6);
    944 				sa.sa_family = AF_UNSPEC;
    945 				sa.sa_len = sizeof(sa);
    946 				eh2 = (struct ether_header *)sa.sa_data;
    947 				for (i = 0; i < 6; i++) {
    948 					eh2->ether_shost[i] = c =
    949 					    eh->ether_dhost[i];
    950 					eh2->ether_dhost[i] =
    951 					    eh->ether_dhost[i] =
    952 					    eh->ether_shost[i];
    953 					eh->ether_shost[i] = c;
    954 				}
    955 				ifp->if_output(ifp, m, &sa, NULL);
    956 				return;
    957 			}
    958 			default:
    959 				m_freem(m);
    960 				return;
    961 			}
    962 			break;
    963 #endif /* ISO */
    964 #ifdef LLC
    965 		case LLC_X25_LSAP:
    966 		{
    967 			if (m->m_pkthdr.len > etype)
    968 				m_adj(m, etype - m->m_pkthdr.len);
    969 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
    970 			if (m == 0)
    971 				return;
    972 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
    973 					    eh->ether_dhost, LLC_X25_LSAP, 6,
    974 					    mtod(m, struct sdl_hdr *)))
    975 				panic("ETHER cons addr failure");
    976 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
    977 #ifdef LLC_DEBUG
    978 				printf("llc packet\n");
    979 #endif /* LLC_DEBUG */
    980 			schednetisr(NETISR_CCITT);
    981 			inq = &llcintrq;
    982 			break;
    983 		}
    984 #endif /* LLC */
    985 		dropanyway:
    986 		default:
    987 			m_freem(m);
    988 			return;
    989 		}
    990 #else /* ISO || LLC  || NETATALK*/
    991 	    m_freem(m);
    992 	    return;
    993 #endif /* ISO || LLC || NETATALK*/
    994 	}
    995 
    996 	s = splnet();
    997 	if (IF_QFULL(inq)) {
    998 		IF_DROP(inq);
    999 		m_freem(m);
   1000 	} else
   1001 		IF_ENQUEUE(inq, m);
   1002 	splx(s);
   1003 }
   1004 
   1005 /*
   1006  * Convert Ethernet address to printable (loggable) representation.
   1007  */
   1008 static char digits[] = "0123456789abcdef";
   1009 char *
   1010 ether_sprintf(const u_char *ap)
   1011 {
   1012 	static char etherbuf[18];
   1013 	char *cp = etherbuf;
   1014 	int i;
   1015 
   1016 	for (i = 0; i < 6; i++) {
   1017 		*cp++ = digits[*ap >> 4];
   1018 		*cp++ = digits[*ap++ & 0xf];
   1019 		*cp++ = ':';
   1020 	}
   1021 	*--cp = 0;
   1022 	return (etherbuf);
   1023 }
   1024 
   1025 /*
   1026  * Perform common duties while attaching to interface list
   1027  */
   1028 void
   1029 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
   1030 {
   1031 
   1032 	ifp->if_type = IFT_ETHER;
   1033 	ifp->if_addrlen = ETHER_ADDR_LEN;
   1034 	ifp->if_hdrlen = 14;
   1035 	ifp->if_dlt = DLT_EN10MB;
   1036 	ifp->if_mtu = ETHERMTU;
   1037 	ifp->if_output = ether_output;
   1038 	ifp->if_input = ether_input;
   1039 	if (ifp->if_baudrate == 0)
   1040 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1041 
   1042 	if_alloc_sadl(ifp);
   1043 	memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
   1044 
   1045 	LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
   1046 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1047 #if NBPFILTER > 0
   1048 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1049 #endif
   1050 }
   1051 
   1052 void
   1053 ether_ifdetach(struct ifnet *ifp)
   1054 {
   1055 	struct ethercom *ec = (void *) ifp;
   1056 	struct ether_multi *enm;
   1057 	int s;
   1058 
   1059 #if NBPFILTER > 0
   1060 	bpfdetach(ifp);
   1061 #endif
   1062 
   1063 #if NVLAN > 0
   1064 	if (ec->ec_nvlans)
   1065 		vlan_ifdetach(ifp);
   1066 #endif
   1067 
   1068 	s = splnet();
   1069 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1070 		LIST_REMOVE(enm, enm_list);
   1071 		free(enm, M_IFADDR);
   1072 		ec->ec_multicnt--;
   1073 	}
   1074 	splx(s);
   1075 
   1076 	if_free_sadl(ifp);
   1077 }
   1078 
   1079 #if 0
   1080 /*
   1081  * This is for reference.  We have a table-driven version
   1082  * of the little-endian crc32 generator, which is faster
   1083  * than the double-loop.
   1084  */
   1085 u_int32_t
   1086 ether_crc32_le(const u_int8_t *buf, size_t len)
   1087 {
   1088 	u_int32_t c, crc, carry;
   1089 	size_t i, j;
   1090 
   1091 	crc = 0xffffffffU;	/* initial value */
   1092 
   1093 	for (i = 0; i < len; i++) {
   1094 		c = buf[i];
   1095 		for (j = 0; j < 8; j++) {
   1096 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1097 			crc >>= 1;
   1098 			c >>= 1;
   1099 			if (carry)
   1100 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1101 		}
   1102 	}
   1103 
   1104 	return (crc);
   1105 }
   1106 #else
   1107 u_int32_t
   1108 ether_crc32_le(const u_int8_t *buf, size_t len)
   1109 {
   1110 	static const u_int32_t crctab[] = {
   1111 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1112 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1113 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1114 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1115 	};
   1116 	u_int32_t crc;
   1117 	int i;
   1118 
   1119 	crc = 0xffffffffU;	/* initial value */
   1120 
   1121 	for (i = 0; i < len; i++) {
   1122 		crc ^= buf[i];
   1123 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1124 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1125 	}
   1126 
   1127 	return (crc);
   1128 }
   1129 #endif
   1130 
   1131 u_int32_t
   1132 ether_crc32_be(const u_int8_t *buf, size_t len)
   1133 {
   1134 	u_int32_t c, crc, carry;
   1135 	size_t i, j;
   1136 
   1137 	crc = 0xffffffffU;	/* initial value */
   1138 
   1139 	for (i = 0; i < len; i++) {
   1140 		c = buf[i];
   1141 		for (j = 0; j < 8; j++) {
   1142 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1143 			crc <<= 1;
   1144 			c >>= 1;
   1145 			if (carry)
   1146 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1147 		}
   1148 	}
   1149 
   1150 	return (crc);
   1151 }
   1152 
   1153 #ifdef INET
   1154 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1155 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1156 #endif
   1157 #ifdef INET6
   1158 u_char	ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1159 u_char	ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1160 #endif
   1161 
   1162 /*
   1163  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1164  * addresses.
   1165  */
   1166 int
   1167 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
   1168     u_int8_t addrhi[ETHER_ADDR_LEN])
   1169 {
   1170 #ifdef INET
   1171 	struct sockaddr_in *sin;
   1172 #endif /* INET */
   1173 #ifdef INET6
   1174 	struct sockaddr_in6 *sin6;
   1175 #endif /* INET6 */
   1176 
   1177 	switch (sa->sa_family) {
   1178 
   1179 	case AF_UNSPEC:
   1180 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
   1181 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1182 		break;
   1183 
   1184 #ifdef INET
   1185 	case AF_INET:
   1186 		sin = satosin(sa);
   1187 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1188 			/*
   1189 			 * An IP address of INADDR_ANY means listen to
   1190 			 * or stop listening to all of the Ethernet
   1191 			 * multicast addresses used for IP.
   1192 			 * (This is for the sake of IP multicast routers.)
   1193 			 */
   1194 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
   1195 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
   1196 		}
   1197 		else {
   1198 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1199 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1200 		}
   1201 		break;
   1202 #endif
   1203 #ifdef INET6
   1204 	case AF_INET6:
   1205 		sin6 = satosin6(sa);
   1206 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1207 			/*
   1208 			 * An IP6 address of 0 means listen to or stop
   1209 			 * listening to all of the Ethernet multicast
   1210 			 * address used for IP6.
   1211 			 * (This is used for multicast routers.)
   1212 			 */
   1213 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
   1214 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
   1215 		} else {
   1216 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1217 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1218 		}
   1219 		break;
   1220 #endif
   1221 
   1222 	default:
   1223 		return (EAFNOSUPPORT);
   1224 	}
   1225 	return (0);
   1226 }
   1227 
   1228 /*
   1229  * Add an Ethernet multicast address or range of addresses to the list for a
   1230  * given interface.
   1231  */
   1232 int
   1233 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
   1234 {
   1235 	struct ether_multi *enm;
   1236 	u_char addrlo[ETHER_ADDR_LEN];
   1237 	u_char addrhi[ETHER_ADDR_LEN];
   1238 	int s = splnet(), error;
   1239 
   1240 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1241 	if (error != 0) {
   1242 		splx(s);
   1243 		return (error);
   1244 	}
   1245 
   1246 	/*
   1247 	 * Verify that we have valid Ethernet multicast addresses.
   1248 	 */
   1249 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
   1250 		splx(s);
   1251 		return (EINVAL);
   1252 	}
   1253 	/*
   1254 	 * See if the address range is already in the list.
   1255 	 */
   1256 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1257 	if (enm != NULL) {
   1258 		/*
   1259 		 * Found it; just increment the reference count.
   1260 		 */
   1261 		++enm->enm_refcount;
   1262 		splx(s);
   1263 		return (0);
   1264 	}
   1265 	/*
   1266 	 * New address or range; malloc a new multicast record
   1267 	 * and link it into the interface's multicast list.
   1268 	 */
   1269 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1270 	if (enm == NULL) {
   1271 		splx(s);
   1272 		return (ENOBUFS);
   1273 	}
   1274 	bcopy(addrlo, enm->enm_addrlo, 6);
   1275 	bcopy(addrhi, enm->enm_addrhi, 6);
   1276 	enm->enm_ec = ec;
   1277 	enm->enm_refcount = 1;
   1278 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1279 	ec->ec_multicnt++;
   1280 	splx(s);
   1281 	/*
   1282 	 * Return ENETRESET to inform the driver that the list has changed
   1283 	 * and its reception filter should be adjusted accordingly.
   1284 	 */
   1285 	return (ENETRESET);
   1286 }
   1287 
   1288 /*
   1289  * Delete a multicast address record.
   1290  */
   1291 int
   1292 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
   1293 {
   1294 	struct ether_multi *enm;
   1295 	u_char addrlo[ETHER_ADDR_LEN];
   1296 	u_char addrhi[ETHER_ADDR_LEN];
   1297 	int s = splnet(), error;
   1298 
   1299 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1300 	if (error != 0) {
   1301 		splx(s);
   1302 		return (error);
   1303 	}
   1304 
   1305 	/*
   1306 	 * Look ur the address in our list.
   1307 	 */
   1308 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1309 	if (enm == NULL) {
   1310 		splx(s);
   1311 		return (ENXIO);
   1312 	}
   1313 	if (--enm->enm_refcount != 0) {
   1314 		/*
   1315 		 * Still some claims to this record.
   1316 		 */
   1317 		splx(s);
   1318 		return (0);
   1319 	}
   1320 	/*
   1321 	 * No remaining claims to this record; unlink and free it.
   1322 	 */
   1323 	LIST_REMOVE(enm, enm_list);
   1324 	free(enm, M_IFMADDR);
   1325 	ec->ec_multicnt--;
   1326 	splx(s);
   1327 	/*
   1328 	 * Return ENETRESET to inform the driver that the list has changed
   1329 	 * and its reception filter should be adjusted accordingly.
   1330 	 */
   1331 	return (ENETRESET);
   1332 }
   1333 
   1334 /*
   1335  * Common ioctls for Ethernet interfaces.  Note, we must be
   1336  * called at splnet().
   1337  */
   1338 int
   1339 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1340 {
   1341 	struct ethercom *ec = (void *) ifp;
   1342 	struct ifreq *ifr = (struct ifreq *)data;
   1343 	struct ifaddr *ifa = (struct ifaddr *)data;
   1344 	int error = 0;
   1345 
   1346 	switch (cmd) {
   1347 	case SIOCSIFADDR:
   1348 		ifp->if_flags |= IFF_UP;
   1349 		switch (ifa->ifa_addr->sa_family) {
   1350 		case AF_LINK:
   1351 		    {
   1352 			struct sockaddr_dl *sdl =
   1353 			    (struct sockaddr_dl *) ifa->ifa_addr;
   1354 
   1355 			if (sdl->sdl_type != IFT_ETHER ||
   1356 			    sdl->sdl_alen != ifp->if_addrlen) {
   1357 				error = EINVAL;
   1358 				break;
   1359 			}
   1360 
   1361 			memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
   1362 			    ifp->if_addrlen);
   1363 
   1364 			/* Set new address. */
   1365 			error = (*ifp->if_init)(ifp);
   1366 			break;
   1367 		    }
   1368 #ifdef INET
   1369 		case AF_INET:
   1370 			if ((ifp->if_flags & IFF_RUNNING) == 0 &&
   1371 			    (error = (*ifp->if_init)(ifp)) != 0)
   1372 				break;
   1373 			arp_ifinit(ifp, ifa);
   1374 			break;
   1375 #endif /* INET */
   1376 #ifdef NS
   1377 		case AF_NS:
   1378 		    {
   1379 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1380 
   1381 			if (ns_nullhost(*ina))
   1382 				ina->x_host = *(union ns_host *)
   1383 				    LLADDR(ifp->if_sadl);
   1384 			else
   1385 				memcpy(LLADDR(ifp->if_sadl),
   1386 				    ina->x_host.c_host, ifp->if_addrlen);
   1387 			/* Set new address. */
   1388 			error = (*ifp->if_init)(ifp);
   1389 			break;
   1390 		    }
   1391 #endif /* NS */
   1392 		default:
   1393 			if ((ifp->if_flags & IFF_RUNNING) == 0)
   1394 				error = (*ifp->if_init)(ifp);
   1395 			break;
   1396 		}
   1397 		break;
   1398 
   1399 	case SIOCGIFADDR:
   1400 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
   1401 		    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1402 		break;
   1403 
   1404 	case SIOCSIFMTU:
   1405 	    {
   1406 		int maxmtu;
   1407 
   1408 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1409 			maxmtu = ETHERMTU_JUMBO;
   1410 		else
   1411 			maxmtu = ETHERMTU;
   1412 
   1413 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1414 			error = EINVAL;
   1415 		else {
   1416 			ifp->if_mtu = ifr->ifr_mtu;
   1417 
   1418 			/* Make sure the device notices the MTU change. */
   1419 			if (ifp->if_flags & IFF_UP)
   1420 				error = (*ifp->if_init)(ifp);
   1421 		}
   1422 		break;
   1423 	    }
   1424 
   1425 	case SIOCSIFFLAGS:
   1426 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
   1427 			/*
   1428 			 * If interface is marked down and it is running,
   1429 			 * then stop and disable it.
   1430 			 */
   1431 			(*ifp->if_stop)(ifp, 1);
   1432 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
   1433 			/*
   1434 			 * If interface is marked up and it is stopped, then
   1435 			 * start it.
   1436 			 */
   1437 			error = (*ifp->if_init)(ifp);
   1438 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1439 			/*
   1440 			 * Reset the interface to pick up changes in any other
   1441 			 * flags that affect the hardware state.
   1442 			 */
   1443 			error = (*ifp->if_init)(ifp);
   1444 		}
   1445 		break;
   1446 
   1447 	case SIOCADDMULTI:
   1448 		error = ether_addmulti(ifr, ec);
   1449 		break;
   1450 
   1451 	case SIOCDELMULTI:
   1452 		error = ether_delmulti(ifr, ec);
   1453 		break;
   1454 
   1455 	default:
   1456 		error = ENOTTY;
   1457 	}
   1458 
   1459 	return (error);
   1460 }
   1461