xref: /freebsd/sys/net/if_ethersubr.c (revision 6de306ecee3831f48debaad1d0b22418faa48e10)
1 /*
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 #include "opt_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipx.h"
41 #include "opt_bdg.h"
42 #include "opt_netgraph.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/random.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 
63 #if defined(INET) || defined(INET6)
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #endif
68 #ifdef INET6
69 #include <netinet6/nd6.h>
70 #endif
71 
72 #ifdef IPX
73 #include <netipx/ipx.h>
74 #include <netipx/ipx_if.h>
75 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
76 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
77 		struct sockaddr *dst, short *tp, int *hlen);
78 #endif
79 
80 #ifdef NS
81 #include <netns/ns.h>
82 #include <netns/ns_if.h>
83 ushort ns_nettype;
84 int ether_outputdebug = 0;
85 int ether_inputdebug = 0;
86 #endif
87 
88 #ifdef NETATALK
89 #include <netatalk/at.h>
90 #include <netatalk/at_var.h>
91 #include <netatalk/at_extern.h>
92 
93 #define llc_snap_org_code llc_un.type_snap.org_code
94 #define llc_snap_ether_type llc_un.type_snap.ether_type
95 
96 extern u_char	at_org_code[3];
97 extern u_char	aarp_org_code[3];
98 #endif /* NETATALK */
99 
100 #ifdef BRIDGE
101 #include <net/bridge.h>
102 #endif
103 
104 #include "vlan.h"
105 #if NVLAN > 0
106 #include <net/if_vlan_var.h>
107 #endif /* NVLAN > 0 */
108 
109 /* netgraph node hooks for ng_ether(4) */
110 void	(*ng_ether_input_p)(struct ifnet *ifp,
111 		struct mbuf **mp, struct ether_header *eh);
112 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
113 		struct mbuf *m, struct ether_header *eh);
114 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
115 void	(*ng_ether_attach_p)(struct ifnet *ifp);
116 void	(*ng_ether_detach_p)(struct ifnet *ifp);
117 
118 static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
119 				    struct sockaddr *));
120 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121 #define senderr(e) do { error = (e); goto bad;} while (0)
122 #define IFP2AC(IFP) ((struct arpcom *)IFP)
123 
124 /*
125  * Ethernet output routine.
126  * Encapsulate a packet of type family for the local net.
127  * Use trailer local net encapsulation if enough data in first
128  * packet leaves a multiple of 512 bytes of data in remainder.
129  * Assumes that ifp is actually pointer to arpcom structure.
130  */
131 int
132 ether_output(ifp, m, dst, rt0)
133 	register struct ifnet *ifp;
134 	struct mbuf *m;
135 	struct sockaddr *dst;
136 	struct rtentry *rt0;
137 {
138 	short type;
139 	int error = 0, hdrcmplt = 0;
140  	u_char esrc[6], edst[6];
141 	register struct rtentry *rt;
142 	register struct ether_header *eh;
143 	int off, loop_copy = 0;
144 	int hlen;	/* link layer header lenght */
145 	struct arpcom *ac = IFP2AC(ifp);
146 
147 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
148 		senderr(ENETDOWN);
149 	rt = rt0;
150 	if (rt) {
151 		if ((rt->rt_flags & RTF_UP) == 0) {
152 			rt0 = rt = rtalloc1(dst, 1, 0UL);
153 			if (rt0)
154 				rt->rt_refcnt--;
155 			else
156 				senderr(EHOSTUNREACH);
157 		}
158 		if (rt->rt_flags & RTF_GATEWAY) {
159 			if (rt->rt_gwroute == 0)
160 				goto lookup;
161 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
162 				rtfree(rt); rt = rt0;
163 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
164 							  0UL);
165 				if ((rt = rt->rt_gwroute) == 0)
166 					senderr(EHOSTUNREACH);
167 			}
168 		}
169 		if (rt->rt_flags & RTF_REJECT)
170 			if (rt->rt_rmx.rmx_expire == 0 ||
171 			    time_second < rt->rt_rmx.rmx_expire)
172 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
173 	}
174 	hlen = ETHER_HDR_LEN;
175 	switch (dst->sa_family) {
176 #ifdef INET
177 	case AF_INET:
178 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
179 			return (0);	/* if not yet resolved */
180 		off = m->m_pkthdr.len - m->m_len;
181 		type = htons(ETHERTYPE_IP);
182 		break;
183 #endif
184 #ifdef INET6
185 	case AF_INET6:
186 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
187 			/* this must be impossible, so we bark */
188 			printf("nd6_storelladdr failed\n");
189 			return(0);
190 		}
191 		off = m->m_pkthdr.len - m->m_len;
192 		type = htons(ETHERTYPE_IPV6);
193 		break;
194 #endif
195 #ifdef IPX
196 	case AF_IPX:
197 		if (ef_outputp) {
198 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
199 		    if (error)
200 			goto bad;
201 		} else
202 		    type = htons(ETHERTYPE_IPX);
203  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
204 		    (caddr_t)edst, sizeof (edst));
205 		break;
206 #endif
207 #ifdef NETATALK
208 	case AF_APPLETALK:
209 	  {
210 	    struct at_ifaddr *aa;
211 
212 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
213 		    goto bad;
214 	    }
215 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
216 		    return (0);
217 	    /*
218 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
219 	     * Since we must preserve the value of m, which is passed to us by
220 	     * value, we m_copy() the first mbuf, and use it for our llc header.
221 	     */
222 	    if ( aa->aa_flags & AFA_PHASE2 ) {
223 		struct llc llc;
224 
225 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
226 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
227 		llc.llc_control = LLC_UI;
228 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
229 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
230 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
231 		type = htons(m->m_pkthdr.len);
232 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
233 	    } else {
234 		type = htons(ETHERTYPE_AT);
235 	    }
236 	    break;
237 	  }
238 #endif NETATALK
239 #ifdef NS
240 	case AF_NS:
241 		switch(ns_nettype){
242 		default:
243 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
244 			type = 0x8137;
245 			break;
246 		case 0x0: /* Novell 802.3 */
247 			type = htons( m->m_pkthdr.len);
248 			break;
249 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
250 			M_PREPEND(m, 3, M_TRYWAIT);
251 			type = htons( m->m_pkthdr.len);
252 			cp = mtod(m, u_char *);
253 			*cp++ = 0xE0;
254 			*cp++ = 0xE0;
255 			*cp++ = 0x03;
256 			break;
257 		}
258  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
259 		    (caddr_t)edst, sizeof (edst));
260 		/*
261 		 * XXX if ns_thishost is the same as the node's ethernet
262 		 * address then just the default code will catch this anyhow.
263 		 * So I'm not sure if this next clause should be here at all?
264 		 * [JRE]
265 		 */
266 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
267 			m->m_pkthdr.rcvif = ifp;
268 			inq = &nsintrq;
269 			if (IF_HANDOFF(inq, m, NULL))
270 				schednetisr(NETISR_NS);
271 			return (error);
272 		}
273 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
274 			m->m_flags |= M_BCAST;
275 		}
276 		break;
277 #endif /* NS */
278 
279 	case pseudo_AF_HDRCMPLT:
280 		hdrcmplt = 1;
281 		eh = (struct ether_header *)dst->sa_data;
282 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
283 		/* FALLTHROUGH */
284 
285 	case AF_UNSPEC:
286 		loop_copy = -1; /* if this is for us, don't do it */
287 		eh = (struct ether_header *)dst->sa_data;
288  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
289 		type = eh->ether_type;
290 		break;
291 
292 	default:
293 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
294 			dst->sa_family);
295 		senderr(EAFNOSUPPORT);
296 	}
297 
298 	/*
299 	 * Add local net header.  If no space in first mbuf,
300 	 * allocate another.
301 	 */
302 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
303 	if (m == 0)
304 		senderr(ENOBUFS);
305 	eh = mtod(m, struct ether_header *);
306 	(void)memcpy(&eh->ether_type, &type,
307 		sizeof(eh->ether_type));
308  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
309 	if (hdrcmplt)
310 		(void)memcpy(eh->ether_shost, esrc,
311 			sizeof(eh->ether_shost));
312 	else
313 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
314 			sizeof(eh->ether_shost));
315 
316 	/*
317 	 * If a simplex interface, and the packet is being sent to our
318 	 * Ethernet address or a broadcast address, loopback a copy.
319 	 * XXX To make a simplex device behave exactly like a duplex
320 	 * device, we should copy in the case of sending to our own
321 	 * ethernet address (thus letting the original actually appear
322 	 * on the wire). However, we don't do that here for security
323 	 * reasons and compatibility with the original behavior.
324 	 */
325 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
326 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
327 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
328 
329 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
330 		} else if (bcmp(eh->ether_dhost,
331 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
332 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
333 			return (0);	/* XXX */
334 		}
335 	}
336 
337 	/* Handle ng_ether(4) processing, if any */
338 	if (ng_ether_output_p != NULL) {
339 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
340 bad:			if (m != NULL)
341 				m_freem(m);
342 			return (error);
343 		}
344 		if (m == NULL)
345 			return (0);
346 	}
347 
348 	/* Continue with link-layer output */
349 	return ether_output_frame(ifp, m);
350 }
351 
352 /*
353  * Ethernet link layer output routine to send a raw frame to the device.
354  *
355  * This assumes that the 14 byte Ethernet header is present and contiguous
356  * in the first mbuf (if BRIDGE'ing).
357  */
358 int
359 ether_output_frame(ifp, m)
360 	struct ifnet *ifp;
361 	struct mbuf *m;
362 {
363 	int error = 0;
364 
365 #ifdef BRIDGE
366 	if (do_bridge && BDG_USED(ifp) ) {
367 		struct ether_header *eh; /* a ptr suffices */
368 
369 		m->m_pkthdr.rcvif = NULL;
370 		eh = mtod(m, struct ether_header *);
371 		m_adj(m, ETHER_HDR_LEN);
372 		m = bdg_forward(m, eh, ifp);
373 		if (m != NULL)
374 			m_freem(m);
375 		return (0);
376 	}
377 #endif
378 
379 	/*
380 	 * Queue message on interface, update output statistics if
381 	 * successful, and start output if interface not yet active.
382 	 */
383 	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
384 		return (ENOBUFS);
385 	return (error);
386 }
387 
388 /*
389  * Process a received Ethernet packet;
390  * the packet is in the mbuf chain m without
391  * the ether header, which is provided separately.
392  *
393  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
394  * cluster, right before m_data. So be very careful when working on m,
395  * as you could destroy *eh !!
396  * A (probably) more convenient and efficient interface to ether_input
397  * is to have the whole packet (with the ethernet header) into the mbuf:
398  * modules which do not need the ethernet header can easily drop it, while
399  * others (most noticeably bridge and ng_ether) do not need to do additional
400  * work to put the ethernet header back into the mbuf.
401  *
402  * First we perform any link layer operations, then continue
403  * to the upper layers with ether_demux().
404  */
405 void
406 ether_input(ifp, eh, m)
407 	struct ifnet *ifp;
408 	struct ether_header *eh;
409 	struct mbuf *m;
410 {
411 #ifdef BRIDGE
412 	struct ether_header save_eh;
413 #endif
414 
415 	/* Check for a BPF tap */
416 	if (ifp->if_bpf != NULL) {
417 		struct m_hdr mh;
418 
419 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
420 		mh.mh_next = m;
421 		mh.mh_data = (char *)eh;
422 		mh.mh_len = ETHER_HDR_LEN;
423 		bpf_mtap(ifp, (struct mbuf *)&mh);
424 	}
425 
426 	/* Handle ng_ether(4) processing, if any */
427 	if (ng_ether_input_p != NULL) {
428 		(*ng_ether_input_p)(ifp, &m, eh);
429 		if (m == NULL)
430 			return;
431 	}
432 
433 #ifdef BRIDGE
434 	/* Check for bridging mode */
435 	if (do_bridge && BDG_USED(ifp) ) {
436 		struct ifnet *bif;
437 
438 		/* Check with bridging code */
439 		if ((bif = bridge_in(ifp, eh)) == BDG_DROP) {
440 			m_freem(m);
441 			return;
442 		}
443 		if (bif != BDG_LOCAL) {
444 			struct mbuf *oldm = m ;
445 
446 			save_eh = *eh ; /* because it might change */
447 			m = bdg_forward(m, eh, bif);	/* needs forwarding */
448 			/*
449 			 * Do not continue if bdg_forward() processed our
450 			 * packet (and cleared the mbuf pointer m) or if
451 			 * it dropped (m_free'd) the packet itself.
452 			 */
453 			if (m == NULL) {
454 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
455 				printf("bdg_forward drop MULTICAST PKT\n");
456 			    return;
457 			}
458 			if (m != oldm) /* m changed! */
459 			    eh = &save_eh ;
460 		}
461 		if (bif == BDG_LOCAL
462 		    || bif == BDG_BCAST
463 		    || bif == BDG_MCAST)
464 			goto recvLocal;			/* receive locally */
465 
466 		/* If not local and not multicast, just drop it */
467 		if (m != NULL)
468 		    m_freem(m);
469 		return;
470        }
471 #endif
472 
473 #ifdef BRIDGE
474 recvLocal:
475 #endif
476 	/* Continue with upper layer processing */
477 	ether_demux(ifp, eh, m);
478 	/* First chunk of an mbuf contains good junk */
479 	if (harvest.ethernet)
480 		random_harvest(m, 16, 3, 0, RANDOM_NET);
481 }
482 
483 /*
484  * Upper layer processing for a received Ethernet packet.
485  */
486 void
487 ether_demux(ifp, eh, m)
488 	struct ifnet *ifp;
489 	struct ether_header *eh;
490 	struct mbuf *m;
491 {
492 	struct ifqueue *inq;
493 	u_short ether_type;
494 #if defined(NETATALK)
495 	register struct llc *l;
496 #endif
497 
498 	/* Discard packet if upper layers shouldn't see it because it was
499 	   unicast to a different Ethernet address. If the driver is working
500 	   properly, then this situation can only happen when the interface
501 	   is in promiscuous mode. */
502 	if ((ifp->if_flags & IFF_PROMISC) != 0
503 	    && (eh->ether_dhost[0] & 1) == 0
504 	    && bcmp(eh->ether_dhost,
505 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
506 		m_freem(m);
507 		return;
508 	}
509 
510 	/* Discard packet if interface is not up */
511 	if ((ifp->if_flags & IFF_UP) == 0) {
512 		m_freem(m);
513 		return;
514 	}
515 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
516 	if (eh->ether_dhost[0] & 1) {
517 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
518 			 sizeof(etherbroadcastaddr)) == 0)
519 			m->m_flags |= M_BCAST;
520 		else
521 			m->m_flags |= M_MCAST;
522 	}
523 	if (m->m_flags & (M_BCAST|M_MCAST))
524 		ifp->if_imcasts++;
525 
526 	ether_type = ntohs(eh->ether_type);
527 
528 #if NVLAN > 0
529 	if (ether_type == vlan_proto) {
530 		if (vlan_input(eh, m) < 0)
531 			ifp->if_data.ifi_noproto++;
532 		return;
533 	}
534 #endif /* NVLAN > 0 */
535 
536 	switch (ether_type) {
537 #ifdef INET
538 	case ETHERTYPE_IP:
539 		if (ipflow_fastforward(m))
540 			return;
541 		schednetisr(NETISR_IP);
542 		inq = &ipintrq;
543 		break;
544 
545 	case ETHERTYPE_ARP:
546 		schednetisr(NETISR_ARP);
547 		inq = &arpintrq;
548 		break;
549 #endif
550 #ifdef IPX
551 	case ETHERTYPE_IPX:
552 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
553 			return;
554 		schednetisr(NETISR_IPX);
555 		inq = &ipxintrq;
556 		break;
557 #endif
558 #ifdef INET6
559 	case ETHERTYPE_IPV6:
560 		schednetisr(NETISR_IPV6);
561 		inq = &ip6intrq;
562 		break;
563 #endif
564 #ifdef NS
565 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
566 		schednetisr(NETISR_NS);
567 		inq = &nsintrq;
568 		break;
569 
570 #endif /* NS */
571 #ifdef NETATALK
572         case ETHERTYPE_AT:
573                 schednetisr(NETISR_ATALK);
574                 inq = &atintrq1;
575                 break;
576         case ETHERTYPE_AARP:
577 		/* probably this should be done with a NETISR as well */
578                 aarpinput(IFP2AC(ifp), m); /* XXX */
579                 return;
580 #endif NETATALK
581 	default:
582 #ifdef IPX
583 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
584 			return;
585 #endif /* IPX */
586 #ifdef NS
587 		checksum = mtod(m, ushort *);
588 		/* Novell 802.3 */
589 		if ((ether_type <= ETHERMTU) &&
590 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
591 			if(*checksum == 0xE0E0) {
592 				m->m_pkthdr.len -= 3;
593 				m->m_len -= 3;
594 				m->m_data += 3;
595 			}
596 				schednetisr(NETISR_NS);
597 				inq = &nsintrq;
598 				break;
599 		}
600 #endif /* NS */
601 #if defined(NETATALK)
602 		if (ether_type > ETHERMTU)
603 			goto dropanyway;
604 		l = mtod(m, struct llc *);
605 		switch (l->llc_dsap) {
606 		case LLC_SNAP_LSAP:
607 		    switch (l->llc_control) {
608 		    case LLC_UI:
609 			if (l->llc_ssap != LLC_SNAP_LSAP)
610 			    goto dropanyway;
611 
612 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
613 				   sizeof(at_org_code)) == 0 &&
614 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
615 			    inq = &atintrq2;
616 			    m_adj( m, sizeof( struct llc ));
617 			    schednetisr(NETISR_ATALK);
618 			    break;
619 			}
620 
621 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
622 				   sizeof(aarp_org_code)) == 0 &&
623 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
624 			    m_adj( m, sizeof( struct llc ));
625 			    aarpinput(IFP2AC(ifp), m); /* XXX */
626 			    return;
627 			}
628 
629 		    default:
630 			goto dropanyway;
631 		    }
632 		    break;
633 		dropanyway:
634 		default:
635 			if (ng_ether_input_orphan_p != NULL)
636 				(*ng_ether_input_orphan_p)(ifp, m, eh);
637 			else
638 				m_freem(m);
639 			return;
640 		}
641 #else /* NETATALK */
642 		if (ng_ether_input_orphan_p != NULL)
643 			(*ng_ether_input_orphan_p)(ifp, m, eh);
644 		else
645 			m_freem(m);
646 		return;
647 #endif /* NETATALK */
648 	}
649 
650 	(void) IF_HANDOFF(inq, m, NULL);
651 }
652 
653 /*
654  * Perform common duties while attaching to interface list
655  */
656 void
657 ether_ifattach(ifp, bpf)
658 	register struct ifnet *ifp;
659 	int bpf;
660 {
661 	register struct ifaddr *ifa;
662 	register struct sockaddr_dl *sdl;
663 
664 	if_attach(ifp);
665 	ifp->if_type = IFT_ETHER;
666 	ifp->if_addrlen = 6;
667 	ifp->if_hdrlen = 14;
668 	ifp->if_mtu = ETHERMTU;
669 	ifp->if_resolvemulti = ether_resolvemulti;
670 	if (ifp->if_baudrate == 0)
671 	    ifp->if_baudrate = 10000000;
672 	ifa = ifnet_addrs[ifp->if_index - 1];
673 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
674 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
675 	sdl->sdl_type = IFT_ETHER;
676 	sdl->sdl_alen = ifp->if_addrlen;
677 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
678 	if (bpf)
679 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
680 	if (ng_ether_attach_p != NULL)
681 		(*ng_ether_attach_p)(ifp);
682 #ifdef BRIDGE
683 	bdgtakeifaces();
684 #endif
685 }
686 
687 /*
688  * Perform common duties while detaching an Ethernet interface
689  */
690 void
691 ether_ifdetach(ifp, bpf)
692 	struct ifnet *ifp;
693 	int bpf;
694 {
695 	if (ng_ether_detach_p != NULL)
696 		(*ng_ether_detach_p)(ifp);
697 	if (bpf)
698 		bpfdetach(ifp);
699 	if_detach(ifp);
700 #ifdef BRIDGE
701 	bdgtakeifaces();
702 #endif
703 }
704 
705 SYSCTL_DECL(_net_link);
706 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
707 
708 int
709 ether_ioctl(ifp, command, data)
710 	struct ifnet *ifp;
711 	int command;
712 	caddr_t data;
713 {
714 	struct ifaddr *ifa = (struct ifaddr *) data;
715 	struct ifreq *ifr = (struct ifreq *) data;
716 	int error = 0;
717 
718 	switch (command) {
719 	case SIOCSIFADDR:
720 		ifp->if_flags |= IFF_UP;
721 
722 		switch (ifa->ifa_addr->sa_family) {
723 #ifdef INET
724 		case AF_INET:
725 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
726 			arp_ifinit(IFP2AC(ifp), ifa);
727 			break;
728 #endif
729 #ifdef IPX
730 		/*
731 		 * XXX - This code is probably wrong
732 		 */
733 		case AF_IPX:
734 			{
735 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
736 			struct arpcom *ac = IFP2AC(ifp);
737 
738 			if (ipx_nullhost(*ina))
739 				ina->x_host =
740 				    *(union ipx_host *)
741 			            ac->ac_enaddr;
742 			else {
743 				bcopy((caddr_t) ina->x_host.c_host,
744 				      (caddr_t) ac->ac_enaddr,
745 				      sizeof(ac->ac_enaddr));
746 			}
747 
748 			/*
749 			 * Set new address
750 			 */
751 			ifp->if_init(ifp->if_softc);
752 			break;
753 			}
754 #endif
755 #ifdef NS
756 		/*
757 		 * XXX - This code is probably wrong
758 		 */
759 		case AF_NS:
760 		{
761 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
762 			struct arpcom *ac = IFP2AC(ifp);
763 
764 			if (ns_nullhost(*ina))
765 				ina->x_host =
766 				    *(union ns_host *) (ac->ac_enaddr);
767 			else {
768 				bcopy((caddr_t) ina->x_host.c_host,
769 				      (caddr_t) ac->ac_enaddr,
770 				      sizeof(ac->ac_enaddr));
771 			}
772 
773 			/*
774 			 * Set new address
775 			 */
776 			ifp->if_init(ifp->if_softc);
777 			break;
778 		}
779 #endif
780 		default:
781 			ifp->if_init(ifp->if_softc);
782 			break;
783 		}
784 		break;
785 
786 	case SIOCGIFADDR:
787 		{
788 			struct sockaddr *sa;
789 
790 			sa = (struct sockaddr *) & ifr->ifr_data;
791 			bcopy(IFP2AC(ifp)->ac_enaddr,
792 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
793 		}
794 		break;
795 
796 	case SIOCSIFMTU:
797 		/*
798 		 * Set the interface MTU.
799 		 */
800 		if (ifr->ifr_mtu > ETHERMTU) {
801 			error = EINVAL;
802 		} else {
803 			ifp->if_mtu = ifr->ifr_mtu;
804 		}
805 		break;
806 	}
807 	return (error);
808 }
809 
810 int
811 ether_resolvemulti(ifp, llsa, sa)
812 	struct ifnet *ifp;
813 	struct sockaddr **llsa;
814 	struct sockaddr *sa;
815 {
816 	struct sockaddr_dl *sdl;
817 	struct sockaddr_in *sin;
818 #ifdef INET6
819 	struct sockaddr_in6 *sin6;
820 #endif
821 	u_char *e_addr;
822 
823 	switch(sa->sa_family) {
824 	case AF_LINK:
825 		/*
826 		 * No mapping needed. Just check that it's a valid MC address.
827 		 */
828 		sdl = (struct sockaddr_dl *)sa;
829 		e_addr = LLADDR(sdl);
830 		if ((e_addr[0] & 1) != 1)
831 			return EADDRNOTAVAIL;
832 		*llsa = 0;
833 		return 0;
834 
835 #ifdef INET
836 	case AF_INET:
837 		sin = (struct sockaddr_in *)sa;
838 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
839 			return EADDRNOTAVAIL;
840 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
841 		       M_WAITOK);
842 		sdl->sdl_len = sizeof *sdl;
843 		sdl->sdl_family = AF_LINK;
844 		sdl->sdl_index = ifp->if_index;
845 		sdl->sdl_type = IFT_ETHER;
846 		sdl->sdl_nlen = 0;
847 		sdl->sdl_alen = ETHER_ADDR_LEN;
848 		sdl->sdl_slen = 0;
849 		e_addr = LLADDR(sdl);
850 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
851 		*llsa = (struct sockaddr *)sdl;
852 		return 0;
853 #endif
854 #ifdef INET6
855 	case AF_INET6:
856 		sin6 = (struct sockaddr_in6 *)sa;
857 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
858 			/*
859 			 * An IP6 address of 0 means listen to all
860 			 * of the Ethernet multicast address used for IP6.
861 			 * (This is used for multicast routers.)
862 			 */
863 			ifp->if_flags |= IFF_ALLMULTI;
864 			*llsa = 0;
865 			return 0;
866 		}
867 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
868 			return EADDRNOTAVAIL;
869 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
870 		       M_WAITOK);
871 		sdl->sdl_len = sizeof *sdl;
872 		sdl->sdl_family = AF_LINK;
873 		sdl->sdl_index = ifp->if_index;
874 		sdl->sdl_type = IFT_ETHER;
875 		sdl->sdl_nlen = 0;
876 		sdl->sdl_alen = ETHER_ADDR_LEN;
877 		sdl->sdl_slen = 0;
878 		e_addr = LLADDR(sdl);
879 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
880 		*llsa = (struct sockaddr *)sdl;
881 		return 0;
882 #endif
883 
884 	default:
885 		/*
886 		 * Well, the text isn't quite right, but it's the name
887 		 * that counts...
888 		 */
889 		return EAFNOSUPPORT;
890 	}
891 }
892 
893