xref: /freebsd/sys/net/if_ethersubr.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
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_WAIT);
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_WAIT);
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 			schednetisr(NETISR_NS);
268 			inq = &nsintrq;
269 			s = splimp();
270 			if (IF_QFULL(inq)) {
271 				IF_DROP(inq);
272 				m_freem(m);
273 			} else
274 				IF_ENQUEUE(inq, m);
275 			splx(s);
276 			return (error);
277 		}
278 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
279 			m->m_flags |= M_BCAST;
280 		}
281 		break;
282 #endif /* NS */
283 
284 	case pseudo_AF_HDRCMPLT:
285 		hdrcmplt = 1;
286 		eh = (struct ether_header *)dst->sa_data;
287 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
288 		/* FALLTHROUGH */
289 
290 	case AF_UNSPEC:
291 		loop_copy = -1; /* if this is for us, don't do it */
292 		eh = (struct ether_header *)dst->sa_data;
293  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
294 		type = eh->ether_type;
295 		break;
296 
297 	default:
298 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
299 			dst->sa_family);
300 		senderr(EAFNOSUPPORT);
301 	}
302 
303 	/*
304 	 * Add local net header.  If no space in first mbuf,
305 	 * allocate another.
306 	 */
307 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
308 	if (m == 0)
309 		senderr(ENOBUFS);
310 	eh = mtod(m, struct ether_header *);
311 	(void)memcpy(&eh->ether_type, &type,
312 		sizeof(eh->ether_type));
313  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
314 	if (hdrcmplt)
315 		(void)memcpy(eh->ether_shost, esrc,
316 			sizeof(eh->ether_shost));
317 	else
318 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
319 			sizeof(eh->ether_shost));
320 
321 	/*
322 	 * If a simplex interface, and the packet is being sent to our
323 	 * Ethernet address or a broadcast address, loopback a copy.
324 	 * XXX To make a simplex device behave exactly like a duplex
325 	 * device, we should copy in the case of sending to our own
326 	 * ethernet address (thus letting the original actually appear
327 	 * on the wire). However, we don't do that here for security
328 	 * reasons and compatibility with the original behavior.
329 	 */
330 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
331 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
332 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
333 
334 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
335 		} else if (bcmp(eh->ether_dhost,
336 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
337 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
338 			return (0);	/* XXX */
339 		}
340 	}
341 
342 	/* Handle ng_ether(4) processing, if any */
343 	if (ng_ether_output_p != NULL) {
344 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
345 bad:			if (m != NULL)
346 				m_freem(m);
347 			return (error);
348 		}
349 		if (m == NULL)
350 			return (0);
351 	}
352 
353 	/* Continue with link-layer output */
354 	return ether_output_frame(ifp, m);
355 }
356 
357 /*
358  * Ethernet link layer output routine to send a raw frame to the device.
359  *
360  * This assumes that the 14 byte Ethernet header is present and contiguous
361  * in the first mbuf (if BRIDGE'ing).
362  */
363 int
364 ether_output_frame(ifp, m)
365 	struct ifnet *ifp;
366 	struct mbuf *m;
367 {
368 	int s, error = 0;
369 
370 #ifdef BRIDGE
371 	if (do_bridge) {
372 		struct ether_header hdr;
373 
374 		m->m_pkthdr.rcvif = NULL;
375 		bcopy(mtod(m, struct ether_header *), &hdr, ETHER_HDR_LEN);
376 		m_adj(m, ETHER_HDR_LEN);
377 		ifp = bridge_dst_lookup(&hdr);
378 		bdg_forward(&m, &hdr, ifp);
379 		if (m != NULL)
380 			m_freem(m);
381 		return (0);
382 	}
383 #endif
384 
385 	s = splimp();
386 	/*
387 	 * Queue message on interface, and start output if interface
388 	 * not yet active.
389 	 */
390 	if (IF_QFULL(&ifp->if_snd)) {
391 		IF_DROP(&ifp->if_snd);
392 		splx(s);
393 		m_freem(m);
394 		return (ENOBUFS);
395 	}
396 	ifp->if_obytes += m->m_pkthdr.len;
397 	if (m->m_flags & M_MCAST)
398 		ifp->if_omcasts++;
399 	IF_ENQUEUE(&ifp->if_snd, m);
400 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
401 		(*ifp->if_start)(ifp);
402 	splx(s);
403 	return (error);
404 }
405 
406 /*
407  * Process a received Ethernet packet;
408  * the packet is in the mbuf chain m without
409  * the ether header, which is provided separately.
410  *
411  * First we perform any link layer operations, then continue
412  * to the upper layers with ether_demux().
413  */
414 void
415 ether_input(ifp, eh, m)
416 	struct ifnet *ifp;
417 	struct ether_header *eh;
418 	struct mbuf *m;
419 {
420 
421 	/* Check for a BPF tap */
422 	if (ifp->if_bpf != NULL) {
423 		struct m_hdr mh;
424 
425 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
426 		mh.mh_next = m;
427 		mh.mh_data = (char *)eh;
428 		mh.mh_len = ETHER_HDR_LEN;
429 		bpf_mtap(ifp, (struct mbuf *)&mh);
430 	}
431 
432 	/* Handle ng_ether(4) processing, if any */
433 	if (ng_ether_input_p != NULL) {
434 		(*ng_ether_input_p)(ifp, &m, eh);
435 		if (m == NULL)
436 			return;
437 	}
438 
439 #ifdef BRIDGE
440 	/* Check for bridging mode */
441 	if (do_bridge) {
442 		struct ifnet *bif;
443 
444 		/* Check with bridging code */
445 		if ((bif = bridge_in(ifp, eh)) == BDG_DROP) {
446 			m_freem(m);
447 			return;
448 		}
449 		if (bif != BDG_LOCAL) {
450 			bdg_forward(&m, eh, bif);	/* needs forwarding */
451 			/*
452 			 * Do not continue if bdg_forward() processed our
453 			 * packet (and cleared the mbuf pointer m) or if
454 			 * it dropped (m_free'd) the packet itself.
455 			 */
456 			if (m == NULL)
457 				return;
458 		}
459 		if (bif == BDG_LOCAL
460 		    || bif == BDG_BCAST
461 		    || bif == BDG_MCAST)
462 			goto recvLocal;			/* receive locally */
463 
464 		/* If not local and not multicast, just drop it */
465 		if (m != NULL)
466 			m_freem(m);
467 		return;
468        }
469 #endif
470 
471 	/* Discard packet if upper layers shouldn't see it. This should
472 	   only happen when the interface is in promiscuous mode. */
473 	if ((ifp->if_flags & IFF_PROMISC) != 0
474 	    && (eh->ether_dhost[0] & 1) == 0
475 	    && bcmp(eh->ether_dhost,
476 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
477 		m_freem(m);
478 		return;
479 	}
480 
481 #ifdef BRIDGE
482 recvLocal:
483 #endif
484 	/* Continue with upper layer processing */
485 	ether_demux(ifp, eh, m);
486 }
487 
488 /*
489  * Upper layer processing for a received Ethernet packet.
490  */
491 void
492 ether_demux(ifp, eh, m)
493 	struct ifnet *ifp;
494 	struct ether_header *eh;
495 	struct mbuf *m;
496 {
497 	struct ifqueue *inq;
498 	u_short ether_type;
499 	int s;
500 #if defined(NETATALK)
501 	register struct llc *l;
502 #endif
503 
504 	/* Discard packet if interface is not up */
505 	if ((ifp->if_flags & IFF_UP) == 0) {
506 		m_freem(m);
507 		return;
508 	}
509 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
510 	if (eh->ether_dhost[0] & 1) {
511 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
512 			 sizeof(etherbroadcastaddr)) == 0)
513 			m->m_flags |= M_BCAST;
514 		else
515 			m->m_flags |= M_MCAST;
516 	}
517 	if (m->m_flags & (M_BCAST|M_MCAST))
518 		ifp->if_imcasts++;
519 
520 	ether_type = ntohs(eh->ether_type);
521 
522 #if NVLAN > 0
523 	if (ether_type == vlan_proto) {
524 		if (vlan_input(eh, m) < 0)
525 			ifp->if_data.ifi_noproto++;
526 		return;
527 	}
528 #endif /* NVLAN > 0 */
529 
530 	switch (ether_type) {
531 #ifdef INET
532 	case ETHERTYPE_IP:
533 		if (ipflow_fastforward(m))
534 			return;
535 		schednetisr(NETISR_IP);
536 		inq = &ipintrq;
537 		break;
538 
539 	case ETHERTYPE_ARP:
540 		schednetisr(NETISR_ARP);
541 		inq = &arpintrq;
542 		break;
543 #endif
544 #ifdef IPX
545 	case ETHERTYPE_IPX:
546 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
547 			return;
548 		schednetisr(NETISR_IPX);
549 		inq = &ipxintrq;
550 		break;
551 #endif
552 #ifdef INET6
553 	case ETHERTYPE_IPV6:
554 		schednetisr(NETISR_IPV6);
555 		inq = &ip6intrq;
556 		break;
557 #endif
558 #ifdef NS
559 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
560 		schednetisr(NETISR_NS);
561 		inq = &nsintrq;
562 		break;
563 
564 #endif /* NS */
565 #ifdef NETATALK
566         case ETHERTYPE_AT:
567                 schednetisr(NETISR_ATALK);
568                 inq = &atintrq1;
569                 break;
570         case ETHERTYPE_AARP:
571 		/* probably this should be done with a NETISR as well */
572                 aarpinput(IFP2AC(ifp), m); /* XXX */
573                 return;
574 #endif NETATALK
575 	default:
576 #ifdef IPX
577 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
578 			return;
579 #endif /* IPX */
580 #ifdef NS
581 		checksum = mtod(m, ushort *);
582 		/* Novell 802.3 */
583 		if ((ether_type <= ETHERMTU) &&
584 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
585 			if(*checksum == 0xE0E0) {
586 				m->m_pkthdr.len -= 3;
587 				m->m_len -= 3;
588 				m->m_data += 3;
589 			}
590 				schednetisr(NETISR_NS);
591 				inq = &nsintrq;
592 				break;
593 		}
594 #endif /* NS */
595 #if defined(NETATALK)
596 		if (ether_type > ETHERMTU)
597 			goto dropanyway;
598 		l = mtod(m, struct llc *);
599 		switch (l->llc_dsap) {
600 		case LLC_SNAP_LSAP:
601 		    switch (l->llc_control) {
602 		    case LLC_UI:
603 			if (l->llc_ssap != LLC_SNAP_LSAP)
604 			    goto dropanyway;
605 
606 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
607 				   sizeof(at_org_code)) == 0 &&
608 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
609 			    inq = &atintrq2;
610 			    m_adj( m, sizeof( struct llc ));
611 			    schednetisr(NETISR_ATALK);
612 			    break;
613 			}
614 
615 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
616 				   sizeof(aarp_org_code)) == 0 &&
617 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
618 			    m_adj( m, sizeof( struct llc ));
619 			    aarpinput(IFP2AC(ifp), m); /* XXX */
620 			    return;
621 			}
622 
623 		    default:
624 			goto dropanyway;
625 		    }
626 		    break;
627 		dropanyway:
628 		default:
629 			if (ng_ether_input_orphan_p != NULL)
630 				(*ng_ether_input_orphan_p)(ifp, m, eh);
631 			else
632 				m_freem(m);
633 			return;
634 		}
635 #else /* NETATALK */
636 		if (ng_ether_input_orphan_p != NULL)
637 			(*ng_ether_input_orphan_p)(ifp, m, eh);
638 		else
639 			m_freem(m);
640 		return;
641 #endif /* NETATALK */
642 	}
643 
644 	s = splimp();
645 	if (IF_QFULL(inq)) {
646 		IF_DROP(inq);
647 		m_freem(m);
648 	} else
649 		IF_ENQUEUE(inq, m);
650 	splx(s);
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