xref: /freebsd/sys/net/if_ethersubr.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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 #include <net/bridge.h>
63 
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #endif
69 #ifdef INET6
70 #include <netinet6/nd6.h>
71 #endif
72 
73 #ifdef IPX
74 #include <netipx/ipx.h>
75 #include <netipx/ipx_if.h>
76 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
77 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
78 		struct sockaddr *dst, short *tp, int *hlen);
79 #endif
80 
81 #ifdef NS
82 #include <netns/ns.h>
83 #include <netns/ns_if.h>
84 ushort ns_nettype;
85 int ether_outputdebug = 0;
86 int ether_inputdebug = 0;
87 #endif
88 
89 #ifdef NETATALK
90 #include <netatalk/at.h>
91 #include <netatalk/at_var.h>
92 #include <netatalk/at_extern.h>
93 
94 #define llc_snap_org_code llc_un.type_snap.org_code
95 #define llc_snap_ether_type llc_un.type_snap.ether_type
96 
97 extern u_char	at_org_code[3];
98 extern u_char	aarp_org_code[3];
99 #endif /* NETATALK */
100 
101 /* netgraph node hooks for ng_ether(4) */
102 void	(*ng_ether_input_p)(struct ifnet *ifp,
103 		struct mbuf **mp, struct ether_header *eh);
104 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
105 		struct mbuf *m, struct ether_header *eh);
106 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
107 void	(*ng_ether_attach_p)(struct ifnet *ifp);
108 void	(*ng_ether_detach_p)(struct ifnet *ifp);
109 
110 int	(*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
111 int	(*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
112 		u_int16_t t);
113 
114 /* bridge support */
115 int do_bridge = 0;
116 bridge_in_t *bridge_in_ptr;
117 bdg_forward_t *bdg_forward_ptr;
118 bdgtakeifaces_t *bdgtakeifaces_ptr;
119 struct bdg_softc *ifp2sc = NULL;
120 
121 static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
122 				    struct sockaddr *));
123 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
124 #define senderr(e) do { error = (e); goto bad;} while (0)
125 #define IFP2AC(IFP) ((struct arpcom *)IFP)
126 
127 /*
128  * Ethernet output routine.
129  * Encapsulate a packet of type family for the local net.
130  * Use trailer local net encapsulation if enough data in first
131  * packet leaves a multiple of 512 bytes of data in remainder.
132  * Assumes that ifp is actually pointer to arpcom structure.
133  */
134 int
135 ether_output(ifp, m, dst, rt0)
136 	register struct ifnet *ifp;
137 	struct mbuf *m;
138 	struct sockaddr *dst;
139 	struct rtentry *rt0;
140 {
141 	short type;
142 	int error = 0, hdrcmplt = 0;
143  	u_char esrc[6], edst[6];
144 	register struct rtentry *rt;
145 	register struct ether_header *eh;
146 	int off, loop_copy = 0;
147 	int hlen;	/* link layer header lenght */
148 	struct arpcom *ac = IFP2AC(ifp);
149 
150 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
151 		senderr(ENETDOWN);
152 	rt = rt0;
153 	if (rt) {
154 		if ((rt->rt_flags & RTF_UP) == 0) {
155 			rt0 = rt = rtalloc1(dst, 1, 0UL);
156 			if (rt0)
157 				rt->rt_refcnt--;
158 			else
159 				senderr(EHOSTUNREACH);
160 		}
161 		if (rt->rt_flags & RTF_GATEWAY) {
162 			if (rt->rt_gwroute == 0)
163 				goto lookup;
164 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
165 				rtfree(rt); rt = rt0;
166 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
167 							  0UL);
168 				if ((rt = rt->rt_gwroute) == 0)
169 					senderr(EHOSTUNREACH);
170 			}
171 		}
172 		if (rt->rt_flags & RTF_REJECT)
173 			if (rt->rt_rmx.rmx_expire == 0 ||
174 			    time_second < rt->rt_rmx.rmx_expire)
175 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
176 	}
177 	hlen = ETHER_HDR_LEN;
178 	switch (dst->sa_family) {
179 #ifdef INET
180 	case AF_INET:
181 		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
182 			return (0);	/* if not yet resolved */
183 		off = m->m_pkthdr.len - m->m_len;
184 		type = htons(ETHERTYPE_IP);
185 		break;
186 #endif
187 #ifdef INET6
188 	case AF_INET6:
189 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
190 			/* Something bad happened */
191 			return(0);
192 		}
193 		off = m->m_pkthdr.len - m->m_len;
194 		type = htons(ETHERTYPE_IPV6);
195 		break;
196 #endif
197 #ifdef IPX
198 	case AF_IPX:
199 		if (ef_outputp) {
200 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
201 		    if (error)
202 			goto bad;
203 		} else
204 		    type = htons(ETHERTYPE_IPX);
205  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
206 		    (caddr_t)edst, sizeof (edst));
207 		break;
208 #endif
209 #ifdef NETATALK
210 	case AF_APPLETALK:
211 	  {
212 	    struct at_ifaddr *aa;
213 
214 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
215 		    goto bad;
216 	    }
217 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
218 		    return (0);
219 	    /*
220 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
221 	     * Since we must preserve the value of m, which is passed to us by
222 	     * value, we m_copy() the first mbuf, and use it for our llc header.
223 	     */
224 	    if ( aa->aa_flags & AFA_PHASE2 ) {
225 		struct llc llc;
226 
227 		M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
228 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
229 		llc.llc_control = LLC_UI;
230 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
231 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
232 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
233 		type = htons(m->m_pkthdr.len);
234 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
235 	    } else {
236 		type = htons(ETHERTYPE_AT);
237 	    }
238 	    break;
239 	  }
240 #endif /* NETATALK */
241 #ifdef NS
242 	case AF_NS:
243 		switch(ns_nettype){
244 		default:
245 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
246 			type = 0x8137;
247 			break;
248 		case 0x0: /* Novell 802.3 */
249 			type = htons( m->m_pkthdr.len);
250 			break;
251 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
252 			M_PREPEND(m, 3, M_TRYWAIT);
253 			type = htons( m->m_pkthdr.len);
254 			cp = mtod(m, u_char *);
255 			*cp++ = 0xE0;
256 			*cp++ = 0xE0;
257 			*cp++ = 0x03;
258 			break;
259 		}
260  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
261 		    (caddr_t)edst, sizeof (edst));
262 		/*
263 		 * XXX if ns_thishost is the same as the node's ethernet
264 		 * address then just the default code will catch this anyhow.
265 		 * So I'm not sure if this next clause should be here at all?
266 		 * [JRE]
267 		 */
268 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
269 			m->m_pkthdr.rcvif = ifp;
270 			inq = &nsintrq;
271 			if (IF_HANDOFF(inq, m, NULL))
272 				schednetisr(NETISR_NS);
273 			return (error);
274 		}
275 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
276 			m->m_flags |= M_BCAST;
277 		}
278 		break;
279 #endif /* NS */
280 
281 	case pseudo_AF_HDRCMPLT:
282 		hdrcmplt = 1;
283 		eh = (struct ether_header *)dst->sa_data;
284 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
285 		/* FALLTHROUGH */
286 
287 	case AF_UNSPEC:
288 		loop_copy = -1; /* if this is for us, don't do it */
289 		eh = (struct ether_header *)dst->sa_data;
290  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
291 		type = eh->ether_type;
292 		break;
293 
294 	default:
295 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
296 			dst->sa_family);
297 		senderr(EAFNOSUPPORT);
298 	}
299 
300 	/*
301 	 * Add local net header.  If no space in first mbuf,
302 	 * allocate another.
303 	 */
304 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
305 	if (m == 0)
306 		senderr(ENOBUFS);
307 	eh = mtod(m, struct ether_header *);
308 	(void)memcpy(&eh->ether_type, &type,
309 		sizeof(eh->ether_type));
310  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
311 	if (hdrcmplt)
312 		(void)memcpy(eh->ether_shost, esrc,
313 			sizeof(eh->ether_shost));
314 	else
315 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
316 			sizeof(eh->ether_shost));
317 
318 	/*
319 	 * If a simplex interface, and the packet is being sent to our
320 	 * Ethernet address or a broadcast address, loopback a copy.
321 	 * XXX To make a simplex device behave exactly like a duplex
322 	 * device, we should copy in the case of sending to our own
323 	 * ethernet address (thus letting the original actually appear
324 	 * on the wire). However, we don't do that here for security
325 	 * reasons and compatibility with the original behavior.
326 	 */
327 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
328 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
329 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
330 
331 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
332 		} else if (bcmp(eh->ether_dhost,
333 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
334 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
335 			return (0);	/* XXX */
336 		}
337 	}
338 
339 	/* Handle ng_ether(4) processing, if any */
340 	if (ng_ether_output_p != NULL) {
341 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
342 bad:			if (m != NULL)
343 				m_freem(m);
344 			return (error);
345 		}
346 		if (m == NULL)
347 			return (0);
348 	}
349 
350 	/* Continue with link-layer output */
351 	return ether_output_frame(ifp, m);
352 }
353 
354 /*
355  * Ethernet link layer output routine to send a raw frame to the device.
356  *
357  * This assumes that the 14 byte Ethernet header is present and contiguous
358  * in the first mbuf (if BRIDGE'ing).
359  */
360 int
361 ether_output_frame(ifp, m)
362 	struct ifnet *ifp;
363 	struct mbuf *m;
364 {
365 	int error = 0;
366 
367 	if (BDG_ACTIVE(ifp) ) {
368 		struct ether_header *eh; /* a ptr suffices */
369 
370 		m->m_pkthdr.rcvif = NULL;
371 		eh = mtod(m, struct ether_header *);
372 		m_adj(m, ETHER_HDR_LEN);
373 		m = bdg_forward_ptr(m, eh, ifp);
374 		if (m != NULL)
375 			m_freem(m);
376 		return (0);
377 	}
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 	struct ether_header save_eh;
412 
413 	/* Check for a BPF tap */
414 	if (ifp->if_bpf != NULL) {
415 		struct m_hdr mh;
416 
417 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
418 		mh.mh_next = m;
419 		mh.mh_data = (char *)eh;
420 		mh.mh_len = ETHER_HDR_LEN;
421 		bpf_mtap(ifp, (struct mbuf *)&mh);
422 	}
423 
424 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
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 	/* Check for bridging mode */
434 	if (BDG_ACTIVE(ifp) ) {
435 		struct ifnet *bif;
436 
437 		/* Check with bridging code */
438 		if ((bif = bridge_in_ptr(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_ptr(m, eh, bif); /* needs forwarding */
447 			/*
448 			 * Do not continue if bdg_forward_ptr() 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 
471 recvLocal:
472 	/* Continue with upper layer processing */
473 	ether_demux(ifp, eh, m);
474 	/* First chunk of an mbuf contains good junk */
475 	if (harvest.ethernet)
476 		random_harvest(m, 16, 3, 0, RANDOM_NET);
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     if (! (BDG_ACTIVE(ifp) ) )
495 	/* Discard packet if upper layers shouldn't see it because it was
496 	   unicast to a different Ethernet address. If the driver is working
497 	   properly, then this situation can only happen when the interface
498 	   is in promiscuous mode. */
499 	if ((ifp->if_flags & IFF_PROMISC) != 0
500 	    && (eh->ether_dhost[0] & 1) == 0
501 	    && bcmp(eh->ether_dhost,
502 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
503 		m_freem(m);
504 		return;
505 	}
506 
507 	/* Discard packet if interface is not up */
508 	if ((ifp->if_flags & IFF_UP) == 0) {
509 		m_freem(m);
510 		return;
511 	}
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 	switch (ether_type) {
525 #ifdef INET
526 	case ETHERTYPE_IP:
527 		if (ipflow_fastforward(m))
528 			return;
529 		schednetisr(NETISR_IP);
530 		inq = &ipintrq;
531 		break;
532 
533 	case ETHERTYPE_ARP:
534 		if (ifp->if_flags & IFF_NOARP) {
535 			/* Discard packet if ARP is disabled on interface */
536 			m_freem(m);
537 			return;
538 		}
539 		schednetisr(NETISR_ARP);
540 		inq = &arpintrq;
541 		break;
542 #endif
543 #ifdef IPX
544 	case ETHERTYPE_IPX:
545 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
546 			return;
547 		schednetisr(NETISR_IPX);
548 		inq = &ipxintrq;
549 		break;
550 #endif
551 #ifdef INET6
552 	case ETHERTYPE_IPV6:
553 		schednetisr(NETISR_IPV6);
554 		inq = &ip6intrq;
555 		break;
556 #endif
557 #ifdef NS
558 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
559 		schednetisr(NETISR_NS);
560 		inq = &nsintrq;
561 		break;
562 
563 #endif /* NS */
564 #ifdef NETATALK
565         case ETHERTYPE_AT:
566                 schednetisr(NETISR_ATALK);
567                 inq = &atintrq1;
568                 break;
569         case ETHERTYPE_AARP:
570 		/* probably this should be done with a NETISR as well */
571                 aarpinput(IFP2AC(ifp), m); /* XXX */
572                 return;
573 #endif /* NETATALK */
574 	case ETHERTYPE_VLAN:
575 		VLAN_INPUT(eh, m);
576 		return;
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 	ifp->if_type = IFT_ETHER;
661 	ifp->if_addrlen = 6;
662 	ifp->if_hdrlen = 14;
663 	if_attach(ifp);
664 	ifp->if_mtu = ETHERMTU;
665 	ifp->if_resolvemulti = ether_resolvemulti;
666 	if (ifp->if_baudrate == 0)
667 	    ifp->if_baudrate = 10000000;
668 	ifp->if_broadcastaddr = etherbroadcastaddr;
669 	ifa = ifaddr_byindex(ifp->if_index);
670 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
671 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
672 	sdl->sdl_type = IFT_ETHER;
673 	sdl->sdl_alen = ifp->if_addrlen;
674 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
675 	if (bpf)
676 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
677 	if (ng_ether_attach_p != NULL)
678 		(*ng_ether_attach_p)(ifp);
679 	if (BDG_LOADED)
680 		bdgtakeifaces_ptr();
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 	if (BDG_LOADED)
697 		bdgtakeifaces_ptr();
698 }
699 
700 SYSCTL_DECL(_net_link);
701 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
702 
703 int
704 ether_ioctl(ifp, command, data)
705 	struct ifnet *ifp;
706 	int command;
707 	caddr_t data;
708 {
709 	struct ifaddr *ifa = (struct ifaddr *) data;
710 	struct ifreq *ifr = (struct ifreq *) data;
711 	int error = 0;
712 
713 	switch (command) {
714 	case SIOCSIFADDR:
715 		ifp->if_flags |= IFF_UP;
716 
717 		switch (ifa->ifa_addr->sa_family) {
718 #ifdef INET
719 		case AF_INET:
720 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
721 			arp_ifinit(ifp, ifa);
722 			break;
723 #endif
724 #ifdef IPX
725 		/*
726 		 * XXX - This code is probably wrong
727 		 */
728 		case AF_IPX:
729 			{
730 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
731 			struct arpcom *ac = IFP2AC(ifp);
732 
733 			if (ipx_nullhost(*ina))
734 				ina->x_host =
735 				    *(union ipx_host *)
736 			            ac->ac_enaddr;
737 			else {
738 				bcopy((caddr_t) ina->x_host.c_host,
739 				      (caddr_t) ac->ac_enaddr,
740 				      sizeof(ac->ac_enaddr));
741 			}
742 
743 			/*
744 			 * Set new address
745 			 */
746 			ifp->if_init(ifp->if_softc);
747 			break;
748 			}
749 #endif
750 #ifdef NS
751 		/*
752 		 * XXX - This code is probably wrong
753 		 */
754 		case AF_NS:
755 		{
756 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
757 			struct arpcom *ac = IFP2AC(ifp);
758 
759 			if (ns_nullhost(*ina))
760 				ina->x_host =
761 				    *(union ns_host *) (ac->ac_enaddr);
762 			else {
763 				bcopy((caddr_t) ina->x_host.c_host,
764 				      (caddr_t) ac->ac_enaddr,
765 				      sizeof(ac->ac_enaddr));
766 			}
767 
768 			/*
769 			 * Set new address
770 			 */
771 			ifp->if_init(ifp->if_softc);
772 			break;
773 		}
774 #endif
775 		default:
776 			ifp->if_init(ifp->if_softc);
777 			break;
778 		}
779 		break;
780 
781 	case SIOCGIFADDR:
782 		{
783 			struct sockaddr *sa;
784 
785 			sa = (struct sockaddr *) & ifr->ifr_data;
786 			bcopy(IFP2AC(ifp)->ac_enaddr,
787 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
788 		}
789 		break;
790 
791 	case SIOCSIFMTU:
792 		/*
793 		 * Set the interface MTU.
794 		 */
795 		if (ifr->ifr_mtu > ETHERMTU) {
796 			error = EINVAL;
797 		} else {
798 			ifp->if_mtu = ifr->ifr_mtu;
799 		}
800 		break;
801 	}
802 	return (error);
803 }
804 
805 int
806 ether_resolvemulti(ifp, llsa, sa)
807 	struct ifnet *ifp;
808 	struct sockaddr **llsa;
809 	struct sockaddr *sa;
810 {
811 	struct sockaddr_dl *sdl;
812 	struct sockaddr_in *sin;
813 #ifdef INET6
814 	struct sockaddr_in6 *sin6;
815 #endif
816 	u_char *e_addr;
817 
818 	switch(sa->sa_family) {
819 	case AF_LINK:
820 		/*
821 		 * No mapping needed. Just check that it's a valid MC address.
822 		 */
823 		sdl = (struct sockaddr_dl *)sa;
824 		e_addr = LLADDR(sdl);
825 		if ((e_addr[0] & 1) != 1)
826 			return EADDRNOTAVAIL;
827 		*llsa = 0;
828 		return 0;
829 
830 #ifdef INET
831 	case AF_INET:
832 		sin = (struct sockaddr_in *)sa;
833 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
834 			return EADDRNOTAVAIL;
835 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
836 		       M_WAITOK|M_ZERO);
837 		sdl->sdl_len = sizeof *sdl;
838 		sdl->sdl_family = AF_LINK;
839 		sdl->sdl_index = ifp->if_index;
840 		sdl->sdl_type = IFT_ETHER;
841 		sdl->sdl_alen = ETHER_ADDR_LEN;
842 		e_addr = LLADDR(sdl);
843 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
844 		*llsa = (struct sockaddr *)sdl;
845 		return 0;
846 #endif
847 #ifdef INET6
848 	case AF_INET6:
849 		sin6 = (struct sockaddr_in6 *)sa;
850 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
851 			/*
852 			 * An IP6 address of 0 means listen to all
853 			 * of the Ethernet multicast address used for IP6.
854 			 * (This is used for multicast routers.)
855 			 */
856 			ifp->if_flags |= IFF_ALLMULTI;
857 			*llsa = 0;
858 			return 0;
859 		}
860 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
861 			return EADDRNOTAVAIL;
862 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
863 		       M_WAITOK|M_ZERO);
864 		sdl->sdl_len = sizeof *sdl;
865 		sdl->sdl_family = AF_LINK;
866 		sdl->sdl_index = ifp->if_index;
867 		sdl->sdl_type = IFT_ETHER;
868 		sdl->sdl_alen = ETHER_ADDR_LEN;
869 		e_addr = LLADDR(sdl);
870 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
871 		*llsa = (struct sockaddr *)sdl;
872 		return 0;
873 #endif
874 
875 	default:
876 		/*
877 		 * Well, the text isn't quite right, but it's the name
878 		 * that counts...
879 		 */
880 		return EAFNOSUPPORT;
881 	}
882 }
883 
884