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