if_ethersubr.c revision 126951
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: head/sys/net/if_ethersubr.c 126951 2004-03-14 05:24:54Z mdodd $
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_mac.h"
43#include "opt_netgraph.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/random.h>
52#include <sys/socket.h>
53#include <sys/sockio.h>
54#include <sys/sysctl.h>
55
56#include <net/if.h>
57#include <net/netisr.h>
58#include <net/route.h>
59#include <net/if_llc.h>
60#include <net/if_dl.h>
61#include <net/if_types.h>
62#include <net/bpf.h>
63#include <net/ethernet.h>
64#include <net/bridge.h>
65#include <net/if_vlan_var.h>
66
67#if defined(INET) || defined(INET6)
68#include <netinet/in.h>
69#include <netinet/in_var.h>
70#include <netinet/if_ether.h>
71#include <netinet/ip_fw.h>
72#include <netinet/ip_dummynet.h>
73#endif
74#ifdef INET6
75#include <netinet6/nd6.h>
76#endif
77
78#ifdef IPX
79#include <netipx/ipx.h>
80#include <netipx/ipx_if.h>
81int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
82int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
83		struct sockaddr *dst, short *tp, int *hlen);
84#endif
85
86#ifdef NETATALK
87#include <netatalk/at.h>
88#include <netatalk/at_var.h>
89#include <netatalk/at_extern.h>
90
91#define llc_snap_org_code llc_un.type_snap.org_code
92#define llc_snap_ether_type llc_un.type_snap.ether_type
93
94extern u_char	at_org_code[3];
95extern u_char	aarp_org_code[3];
96#endif /* NETATALK */
97
98/* netgraph node hooks for ng_ether(4) */
99void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
100void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
101int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
102void	(*ng_ether_attach_p)(struct ifnet *ifp);
103void	(*ng_ether_detach_p)(struct ifnet *ifp);
104
105void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
106
107/* bridge support */
108int do_bridge;
109bridge_in_t *bridge_in_ptr;
110bdg_forward_t *bdg_forward_ptr;
111bdgtakeifaces_t *bdgtakeifaces_ptr;
112struct bdg_softc *ifp2sc;
113
114static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
115			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
116
117static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
118		struct sockaddr *);
119
120#define senderr(e) do { error = (e); goto bad;} while (0)
121#define IFP2AC(IFP) ((struct arpcom *)IFP)
122
123int
124ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
125	struct ip_fw **rule, int shared);
126static int ether_ipfw;
127
128/*
129 * Ethernet output routine.
130 * Encapsulate a packet of type family for the local net.
131 * Use trailer local net encapsulation if enough data in first
132 * packet leaves a multiple of 512 bytes of data in remainder.
133 * Assumes that ifp is actually pointer to arpcom structure.
134 */
135int
136ether_output(struct ifnet *ifp, struct mbuf *m,
137	struct sockaddr *dst, struct rtentry *rt0)
138{
139	short type;
140	int error = 0, hdrcmplt = 0;
141	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
142	struct rtentry *rt;
143	struct ether_header *eh;
144	int loop_copy = 0;
145	int hlen;	/* link layer header length */
146	struct arpcom *ac = IFP2AC(ifp);
147
148#ifdef MAC
149	error = mac_check_ifnet_transmit(ifp, m);
150	if (error)
151		senderr(error);
152#endif
153
154	if (ifp->if_flags & IFF_MONITOR)
155		senderr(ENETDOWN);
156	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
157		senderr(ENETDOWN);
158
159	error = rt_check(&rt, &rt0, dst);
160	if (error)
161		goto bad;
162
163	hlen = ETHER_HDR_LEN;
164	switch (dst->sa_family) {
165#ifdef INET
166	case AF_INET:
167		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
168			return (0);	/* if not yet resolved */
169		type = htons(ETHERTYPE_IP);
170		break;
171	case AF_ARP:
172	{
173		struct arphdr *ah;
174		ah = mtod(m, struct arphdr *);
175		ah->ar_hrd = htons(ARPHRD_ETHER);
176
177		loop_copy = -1; /* if this is for us, don't do it */
178
179		switch(ntohs(ah->ar_op)) {
180		case ARPOP_REVREQUEST:
181		case ARPOP_REVREPLY:
182			type = htons(ETHERTYPE_REVARP);
183			break;
184		case ARPOP_REQUEST:
185		case ARPOP_REPLY:
186		default:
187			type = htons(ETHERTYPE_ARP);
188			break;
189		}
190
191		if (m->m_flags & M_BCAST)
192			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
193		else
194			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
195
196	}
197	break;
198#endif
199#ifdef INET6
200	case AF_INET6:
201		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
202			/* Something bad happened */
203			return(0);
204		}
205		type = htons(ETHERTYPE_IPV6);
206		break;
207#endif
208#ifdef IPX
209	case AF_IPX:
210		if (ef_outputp) {
211		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
212		    if (error)
213			goto bad;
214		} else
215		    type = htons(ETHERTYPE_IPX);
216		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
217		    (caddr_t)edst, sizeof (edst));
218		break;
219#endif
220#ifdef NETATALK
221	case AF_APPLETALK:
222	  {
223	    struct at_ifaddr *aa;
224
225	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
226		    goto bad;
227	    }
228	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
229		    return (0);
230	    /*
231	     * In the phase 2 case, need to prepend an mbuf for the llc header.
232	     * Since we must preserve the value of m, which is passed to us by
233	     * value, we m_copy() the first mbuf, and use it for our llc header.
234	     */
235	    if ( aa->aa_flags & AFA_PHASE2 ) {
236		struct llc llc;
237
238		M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
239		if (m == NULL)
240			senderr(ENOBUFS);
241		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
242		llc.llc_control = LLC_UI;
243		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
244		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
245		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
246		type = htons(m->m_pkthdr.len);
247		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
248	    } else {
249		type = htons(ETHERTYPE_AT);
250	    }
251	    break;
252	  }
253#endif /* NETATALK */
254
255	case pseudo_AF_HDRCMPLT:
256		hdrcmplt = 1;
257		eh = (struct ether_header *)dst->sa_data;
258		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259		/* FALLTHROUGH */
260
261	case AF_UNSPEC:
262		loop_copy = -1; /* if this is for us, don't do it */
263		eh = (struct ether_header *)dst->sa_data;
264		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
265		type = eh->ether_type;
266		break;
267
268	default:
269		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
270		senderr(EAFNOSUPPORT);
271	}
272
273	/*
274	 * Add local net header.  If no space in first mbuf,
275	 * allocate another.
276	 */
277	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
278	if (m == NULL)
279		senderr(ENOBUFS);
280	eh = mtod(m, struct ether_header *);
281	(void)memcpy(&eh->ether_type, &type,
282		sizeof(eh->ether_type));
283	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
284	if (hdrcmplt)
285		(void)memcpy(eh->ether_shost, esrc,
286			sizeof(eh->ether_shost));
287	else
288		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
289			sizeof(eh->ether_shost));
290
291	/*
292	 * If a simplex interface, and the packet is being sent to our
293	 * Ethernet address or a broadcast address, loopback a copy.
294	 * XXX To make a simplex device behave exactly like a duplex
295	 * device, we should copy in the case of sending to our own
296	 * ethernet address (thus letting the original actually appear
297	 * on the wire). However, we don't do that here for security
298	 * reasons and compatibility with the original behavior.
299	 */
300	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
301		int csum_flags = 0;
302
303		if (m->m_pkthdr.csum_flags & CSUM_IP)
304			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
305		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
306			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
307
308		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
309			struct mbuf *n;
310
311			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
312				n->m_pkthdr.csum_flags |= csum_flags;
313				if (csum_flags & CSUM_DATA_VALID)
314					n->m_pkthdr.csum_data = 0xffff;
315				(void)if_simloop(ifp, n, dst->sa_family, hlen);
316			} else
317				ifp->if_iqdrops++;
318		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
319				ETHER_ADDR_LEN) == 0) {
320			m->m_pkthdr.csum_flags |= csum_flags;
321			if (csum_flags & CSUM_DATA_VALID)
322				m->m_pkthdr.csum_data = 0xffff;
323			(void) if_simloop(ifp, m, dst->sa_family, hlen);
324			return (0);	/* XXX */
325		}
326	}
327
328	/* Handle ng_ether(4) processing, if any */
329	if (ng_ether_output_p != NULL) {
330		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
331bad:			if (m != NULL)
332				m_freem(m);
333			return (error);
334		}
335		if (m == NULL)
336			return (0);
337	}
338
339	/* Continue with link-layer output */
340	return ether_output_frame(ifp, m);
341}
342
343/*
344 * Ethernet link layer output routine to send a raw frame to the device.
345 *
346 * This assumes that the 14 byte Ethernet header is present and contiguous
347 * in the first mbuf (if BRIDGE'ing).
348 */
349int
350ether_output_frame(struct ifnet *ifp, struct mbuf *m)
351{
352	struct ip_fw *rule = ip_dn_claim_rule(m);
353
354	if (rule == NULL && BDG_ACTIVE(ifp)) {
355		/*
356		 * Beware, the bridge code notices the null rcvif and
357		 * uses that identify that it's being called from
358		 * ether_output as opposd to ether_input.  Yech.
359		 */
360		m->m_pkthdr.rcvif = NULL;
361		m = bdg_forward_ptr(m, ifp);
362		if (m != NULL)
363			m_freem(m);
364		return (0);
365	}
366	if (IPFW_LOADED && ether_ipfw != 0) {
367		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
368			if (m) {
369				m_freem(m);
370				return EACCES;	/* pkt dropped */
371			} else
372				return 0;	/* consumed e.g. in a pipe */
373		}
374	}
375
376	/*
377	 * Queue message on interface, update output statistics if
378	 * successful, and start output if interface not yet active.
379	 */
380	return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS);
381}
382
383/*
384 * ipfw processing for ethernet packets (in and out).
385 * The second parameter is NULL from ether_demux, and ifp from
386 * ether_output_frame. This section of code could be used from
387 * bridge.c as well as long as we use some extra info
388 * to distinguish that case from ether_output_frame();
389 */
390int
391ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
392	struct ip_fw **rule, int shared)
393{
394	struct ether_header *eh;
395	struct ether_header save_eh;
396	struct mbuf *m;
397	int i;
398	struct ip_fw_args args;
399
400	if (*rule != NULL && fw_one_pass)
401		return 1; /* dummynet packet, already partially processed */
402
403	/*
404	 * I need some amt of data to be contiguous, and in case others need
405	 * the packet (shared==1) also better be in the first mbuf.
406	 */
407	m = *m0;
408	i = min( m->m_pkthdr.len, max_protohdr);
409	if ( shared || m->m_len < i) {
410		m = m_pullup(m, i);
411		if (m == NULL) {
412			*m0 = m;
413			return 0;
414		}
415	}
416	eh = mtod(m, struct ether_header *);
417	save_eh = *eh;			/* save copy for restore below */
418	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
419
420	args.m = m;		/* the packet we are looking at		*/
421	args.oif = dst;		/* destination, if any			*/
422	args.rule = *rule;	/* matching rule to restart		*/
423	args.next_hop = NULL;	/* we do not support forward yet	*/
424	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
425	i = ip_fw_chk_ptr(&args);
426	m = args.m;
427	if (m != NULL) {
428		/*
429		 * Restore Ethernet header, as needed, in case the
430		 * mbuf chain was replaced by ipfw.
431		 */
432		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
433		if (m == NULL) {
434			*m0 = m;
435			return 0;
436		}
437		if (eh != mtod(m, struct ether_header *))
438			bcopy(&save_eh, mtod(m, struct ether_header *),
439				ETHER_HDR_LEN);
440	}
441	*m0 = m;
442	*rule = args.rule;
443
444	if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */
445		return 0;
446
447	if (i == 0) /* a PASS rule.  */
448		return 1;
449
450	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
451		/*
452		 * Pass the pkt to dummynet, which consumes it.
453		 * If shared, make a copy and keep the original.
454		 */
455		if (shared) {
456			m = m_copypacket(m, M_DONTWAIT);
457			if (m == NULL)
458				return 0;
459		} else {
460			/*
461			 * Pass the original to dummynet and
462			 * nothing back to the caller
463			 */
464			*m0 = NULL ;
465		}
466		ip_dn_io_ptr(m, (i & 0xffff),
467			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
468		return 0;
469	}
470	/*
471	 * XXX at some point add support for divert/forward actions.
472	 * If none of the above matches, we have to drop the pkt.
473	 */
474	return 0;
475}
476
477/*
478 * Process a received Ethernet packet; the packet is in the
479 * mbuf chain m with the ethernet header at the front.
480 */
481static void
482ether_input(struct ifnet *ifp, struct mbuf *m)
483{
484	struct ether_header *eh;
485	u_short etype;
486
487	/*
488	 * Do consistency checks to verify assumptions
489	 * made by code past this point.
490	 */
491	if ((m->m_flags & M_PKTHDR) == 0) {
492		if_printf(ifp, "discard frame w/o packet header\n");
493		ifp->if_ierrors++;
494		m_freem(m);
495		return;
496	}
497	if (m->m_len < ETHER_HDR_LEN) {
498		/* XXX maybe should pullup? */
499		if_printf(ifp, "discard frame w/o leading ethernet "
500				"header (len %u pkt len %u)\n",
501				m->m_len, m->m_pkthdr.len);
502		ifp->if_ierrors++;
503		m_freem(m);
504		return;
505	}
506	eh = mtod(m, struct ether_header *);
507	etype = ntohs(eh->ether_type);
508	if (m->m_pkthdr.len >
509	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
510		if_printf(ifp, "discard oversize frame "
511				"(ether type %x flags %x len %u > max %lu)\n",
512				etype, m->m_flags, m->m_pkthdr.len,
513				ETHER_MAX_FRAME(ifp, etype,
514						m->m_flags & M_HASFCS));
515		ifp->if_ierrors++;
516		m_freem(m);
517		return;
518	}
519	if (m->m_pkthdr.rcvif == NULL) {
520		if_printf(ifp, "discard frame w/o interface pointer\n");
521		ifp->if_ierrors++;
522		m_freem(m);
523		return;
524	}
525#ifdef DIAGNOSTIC
526	if (m->m_pkthdr.rcvif != ifp) {
527		if_printf(ifp, "Warning, frame marked as received on %s\n",
528			m->m_pkthdr.rcvif->if_xname);
529	}
530#endif
531
532#ifdef MAC
533	/*
534	 * Tag the mbuf with an appropriate MAC label before any other
535	 * consumers can get to it.
536	 */
537	mac_create_mbuf_from_ifnet(ifp, m);
538#endif
539
540	/*
541	 * Give bpf a chance at the packet.
542	 */
543	BPF_MTAP(ifp, m);
544
545	if (ifp->if_flags & IFF_MONITOR) {
546		/*
547		 * Interface marked for monitoring; discard packet.
548		 */
549		m_freem(m);
550		return;
551	}
552
553	/* If the CRC is still on the packet, trim it off. */
554	if (m->m_flags & M_HASFCS) {
555		m_adj(m, -ETHER_CRC_LEN);
556		m->m_flags &= ~M_HASFCS;
557	}
558
559	ifp->if_ibytes += m->m_pkthdr.len;
560
561	/* Handle ng_ether(4) processing, if any */
562	if (ng_ether_input_p != NULL) {
563		(*ng_ether_input_p)(ifp, &m);
564		if (m == NULL)
565			return;
566	}
567
568	/* Check for bridging mode */
569	if (BDG_ACTIVE(ifp) ) {
570		struct ifnet *bif;
571
572		/*
573		 * Check with bridging code to see how the packet
574		 * should be handled.  Possibilities are:
575		 *
576		 *    BDG_BCAST		broadcast
577		 *    BDG_MCAST		multicast
578		 *    BDG_LOCAL		for local address, don't forward
579		 *    BDG_DROP		discard
580		 *    ifp		forward only to specified interface(s)
581		 *
582		 * Non-local destinations are handled by passing the
583		 * packet back to the bridge code.
584		 */
585		bif = bridge_in_ptr(ifp, eh);
586		if (bif == BDG_DROP) {		/* discard packet */
587			m_freem(m);
588			return;
589		}
590		if (bif != BDG_LOCAL) {		/* non-local, forward */
591			m = bdg_forward_ptr(m, bif);
592			/*
593			 * The bridge may consume the packet if it's not
594			 * supposed to be passed up or if a problem occurred
595			 * while doing its job.  This is reflected by it
596			 * returning a NULL mbuf pointer.
597			 */
598			if (m == NULL) {
599				if (bif == BDG_BCAST || bif == BDG_MCAST)
600					if_printf(ifp,
601						"bridge dropped %s packet\n",
602						bif == BDG_BCAST ? "broadcast" :
603								   "multicast");
604				return;
605			}
606			/*
607			 * But in some cases the bridge may return the
608			 * packet for us to free; sigh.
609			 */
610			if (bif != BDG_BCAST && bif != BDG_MCAST) {
611				m_freem(m);
612				return;
613			}
614		}
615	}
616
617	ether_demux(ifp, m);
618	/* First chunk of an mbuf contains good entropy */
619	if (harvest.ethernet)
620		random_harvest(m, 16, 3, 0, RANDOM_NET);
621}
622
623/*
624 * Upper layer processing for a received Ethernet packet.
625 */
626void
627ether_demux(struct ifnet *ifp, struct mbuf *m)
628{
629	struct ether_header *eh;
630	int isr;
631	u_short ether_type;
632#if defined(NETATALK)
633	struct llc *l;
634#endif
635	struct ip_fw *rule = ip_dn_claim_rule(m);
636
637	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
638
639	eh = mtod(m, struct ether_header *);
640
641	if (rule)	/* packet was already bridged */
642		goto post_stats;
643
644	if (!(BDG_ACTIVE(ifp))) {
645		/*
646		 * Discard packet if upper layers shouldn't see it because it
647		 * was unicast to a different Ethernet address. If the driver
648		 * is working properly, then this situation can only happen
649		 * when the interface is in promiscuous mode.
650		 */
651		if ((ifp->if_flags & IFF_PROMISC) != 0
652		    && (eh->ether_dhost[0] & 1) == 0
653		    && bcmp(eh->ether_dhost,
654		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
655		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
656			    m_freem(m);
657			    return;
658		}
659	}
660
661	/* Discard packet if interface is not up */
662	if ((ifp->if_flags & IFF_UP) == 0) {
663		m_freem(m);
664		return;
665	}
666	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
667		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
668		    sizeof(etherbroadcastaddr)) == 0)
669			m->m_flags |= M_BCAST;
670		else
671			m->m_flags |= M_MCAST;
672	}
673	if (m->m_flags & (M_BCAST|M_MCAST))
674		ifp->if_imcasts++;
675
676post_stats:
677	if (IPFW_LOADED && ether_ipfw != 0) {
678		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
679			if (m)
680				m_freem(m);
681			return;
682		}
683	}
684
685	/*
686	 * If VLANs are configured on the interface, check to
687	 * see if the device performed the decapsulation and
688	 * provided us with the tag.
689	 */
690	if (ifp->if_nvlans &&
691	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
692		/*
693		 * vlan_input() will either recursively call ether_input()
694		 * or drop the packet.
695		 */
696		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
697		(*vlan_input_p)(ifp, m);
698		return;
699	}
700
701	ether_type = ntohs(eh->ether_type);
702
703	/*
704	 * Handle protocols that expect to have the Ethernet header
705	 * (and possibly FCS) intact.
706	 */
707	switch (ether_type) {
708	case ETHERTYPE_VLAN:
709		if (ifp->if_nvlans != 0) {
710			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
711			(*vlan_input_p)(ifp, m);
712		} else {
713			ifp->if_noproto++;
714			m_freem(m);
715		}
716		return;
717	}
718
719	/* Strip off Ethernet header. */
720	m_adj(m, ETHER_HDR_LEN);
721
722	/* If the CRC is still on the packet, trim it off. */
723	if (m->m_flags & M_HASFCS) {
724		m_adj(m, -ETHER_CRC_LEN);
725		m->m_flags &= ~M_HASFCS;
726	}
727
728	switch (ether_type) {
729#ifdef INET
730	case ETHERTYPE_IP:
731		if (ip_fastforward(m))
732			return;
733		isr = NETISR_IP;
734		break;
735
736	case ETHERTYPE_ARP:
737		if (ifp->if_flags & IFF_NOARP) {
738			/* Discard packet if ARP is disabled on interface */
739			m_freem(m);
740			return;
741		}
742		isr = NETISR_ARP;
743		break;
744#endif
745#ifdef IPX
746	case ETHERTYPE_IPX:
747		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
748			return;
749		isr = NETISR_IPX;
750		break;
751#endif
752#ifdef INET6
753	case ETHERTYPE_IPV6:
754		isr = NETISR_IPV6;
755		break;
756#endif
757#ifdef NETATALK
758	case ETHERTYPE_AT:
759		isr = NETISR_ATALK1;
760		break;
761	case ETHERTYPE_AARP:
762		isr = NETISR_AARP;
763		break;
764#endif /* NETATALK */
765	default:
766#ifdef IPX
767		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
768			return;
769#endif /* IPX */
770#if defined(NETATALK)
771		if (ether_type > ETHERMTU)
772			goto discard;
773		l = mtod(m, struct llc *);
774		if (l->llc_dsap == LLC_SNAP_LSAP &&
775		    l->llc_ssap == LLC_SNAP_LSAP &&
776		    l->llc_control == LLC_UI) {
777			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
778			    sizeof(at_org_code)) == 0 &&
779			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
780				m_adj(m, LLC_SNAPFRAMELEN);
781				isr = NETISR_ATALK2;
782				break;
783			}
784			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
785			    sizeof(aarp_org_code)) == 0 &&
786			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
787				m_adj(m, LLC_SNAPFRAMELEN);
788				isr = NETISR_AARP;
789				break;
790			}
791		}
792#endif /* NETATALK */
793		goto discard;
794	}
795	netisr_dispatch(isr, m);
796	return;
797
798discard:
799	/*
800	 * Packet is to be discarded.  If netgraph is present,
801	 * hand the packet to it for last chance processing;
802	 * otherwise dispose of it.
803	 */
804	if (ng_ether_input_orphan_p != NULL) {
805		/*
806		 * Put back the ethernet header so netgraph has a
807		 * consistent view of inbound packets.
808		 */
809		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
810		(*ng_ether_input_orphan_p)(ifp, m);
811		return;
812	}
813	m_freem(m);
814}
815
816/*
817 * Convert Ethernet address to printable (loggable) representation.
818 * This routine is for compatibility; it's better to just use
819 *
820 *	printf("%6D", <pointer to address>, ":");
821 *
822 * since there's no static buffer involved.
823 */
824char *
825ether_sprintf(const u_char *ap)
826{
827	static char etherbuf[18];
828	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
829	return (etherbuf);
830}
831
832/*
833 * Perform common duties while attaching to interface list
834 */
835void
836ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
837{
838	struct ifaddr *ifa;
839	struct sockaddr_dl *sdl;
840
841	ifp->if_type = IFT_ETHER;
842	ifp->if_addrlen = ETHER_ADDR_LEN;
843	ifp->if_hdrlen = ETHER_HDR_LEN;
844	if_attach(ifp);
845	ifp->if_mtu = ETHERMTU;
846	ifp->if_output = ether_output;
847	ifp->if_input = ether_input;
848	ifp->if_resolvemulti = ether_resolvemulti;
849	if (ifp->if_baudrate == 0)
850		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
851	ifp->if_broadcastaddr = etherbroadcastaddr;
852
853	ifa = ifaddr_byindex(ifp->if_index);
854	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
855	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
856	sdl->sdl_type = IFT_ETHER;
857	sdl->sdl_alen = ifp->if_addrlen;
858	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
859	/*
860	 * XXX: This doesn't belong here; we do it until
861	 * XXX:  all drivers are cleaned up
862	 */
863	if (llc != IFP2AC(ifp)->ac_enaddr)
864		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
865
866	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
867	if (ng_ether_attach_p != NULL)
868		(*ng_ether_attach_p)(ifp);
869	if (BDG_LOADED)
870		bdgtakeifaces_ptr();
871}
872
873/*
874 * Perform common duties while detaching an Ethernet interface
875 */
876void
877ether_ifdetach(struct ifnet *ifp)
878{
879	if (ng_ether_detach_p != NULL)
880		(*ng_ether_detach_p)(ifp);
881	bpfdetach(ifp);
882	if_detach(ifp);
883	if (BDG_LOADED)
884		bdgtakeifaces_ptr();
885}
886
887SYSCTL_DECL(_net_link);
888SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
889SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
890	    &ether_ipfw,0,"Pass ether pkts through firewall");
891
892int
893ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
894{
895	struct ifaddr *ifa = (struct ifaddr *) data;
896	struct ifreq *ifr = (struct ifreq *) data;
897	int error = 0;
898
899	switch (command) {
900	case SIOCSIFADDR:
901		ifp->if_flags |= IFF_UP;
902
903		switch (ifa->ifa_addr->sa_family) {
904#ifdef INET
905		case AF_INET:
906			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
907			arp_ifinit(ifp, ifa);
908			break;
909#endif
910#ifdef IPX
911		/*
912		 * XXX - This code is probably wrong
913		 */
914		case AF_IPX:
915			{
916			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
917			struct arpcom *ac = IFP2AC(ifp);
918
919			if (ipx_nullhost(*ina))
920				ina->x_host =
921				    *(union ipx_host *)
922				    ac->ac_enaddr;
923			else {
924				bcopy((caddr_t) ina->x_host.c_host,
925				      (caddr_t) ac->ac_enaddr,
926				      sizeof(ac->ac_enaddr));
927			}
928
929			/*
930			 * Set new address
931			 */
932			ifp->if_init(ifp->if_softc);
933			break;
934			}
935#endif
936		default:
937			ifp->if_init(ifp->if_softc);
938			break;
939		}
940		break;
941
942	case SIOCGIFADDR:
943		{
944			struct sockaddr *sa;
945
946			sa = (struct sockaddr *) & ifr->ifr_data;
947			bcopy(IFP2AC(ifp)->ac_enaddr,
948			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
949		}
950		break;
951
952	case SIOCSIFMTU:
953		/*
954		 * Set the interface MTU.
955		 */
956		if (ifr->ifr_mtu > ETHERMTU) {
957			error = EINVAL;
958		} else {
959			ifp->if_mtu = ifr->ifr_mtu;
960		}
961		break;
962	default:
963		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
964		break;
965	}
966	return (error);
967}
968
969static int
970ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
971	struct sockaddr *sa)
972{
973	struct sockaddr_dl *sdl;
974	struct sockaddr_in *sin;
975#ifdef INET6
976	struct sockaddr_in6 *sin6;
977#endif
978	u_char *e_addr;
979
980	switch(sa->sa_family) {
981	case AF_LINK:
982		/*
983		 * No mapping needed. Just check that it's a valid MC address.
984		 */
985		sdl = (struct sockaddr_dl *)sa;
986		e_addr = LLADDR(sdl);
987		if ((e_addr[0] & 1) != 1)
988			return EADDRNOTAVAIL;
989		*llsa = 0;
990		return 0;
991
992#ifdef INET
993	case AF_INET:
994		sin = (struct sockaddr_in *)sa;
995		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
996			return EADDRNOTAVAIL;
997		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
998		       M_WAITOK|M_ZERO);
999		sdl->sdl_len = sizeof *sdl;
1000		sdl->sdl_family = AF_LINK;
1001		sdl->sdl_index = ifp->if_index;
1002		sdl->sdl_type = IFT_ETHER;
1003		sdl->sdl_alen = ETHER_ADDR_LEN;
1004		e_addr = LLADDR(sdl);
1005		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1006		*llsa = (struct sockaddr *)sdl;
1007		return 0;
1008#endif
1009#ifdef INET6
1010	case AF_INET6:
1011		sin6 = (struct sockaddr_in6 *)sa;
1012		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1013			/*
1014			 * An IP6 address of 0 means listen to all
1015			 * of the Ethernet multicast address used for IP6.
1016			 * (This is used for multicast routers.)
1017			 */
1018			ifp->if_flags |= IFF_ALLMULTI;
1019			*llsa = 0;
1020			return 0;
1021		}
1022		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1023			return EADDRNOTAVAIL;
1024		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1025		       M_WAITOK|M_ZERO);
1026		sdl->sdl_len = sizeof *sdl;
1027		sdl->sdl_family = AF_LINK;
1028		sdl->sdl_index = ifp->if_index;
1029		sdl->sdl_type = IFT_ETHER;
1030		sdl->sdl_alen = ETHER_ADDR_LEN;
1031		e_addr = LLADDR(sdl);
1032		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1033		*llsa = (struct sockaddr *)sdl;
1034		return 0;
1035#endif
1036
1037	default:
1038		/*
1039		 * Well, the text isn't quite right, but it's the name
1040		 * that counts...
1041		 */
1042		return EAFNOSUPPORT;
1043	}
1044}
1045
1046static moduledata_t ether_mod = {
1047	"ether",
1048	NULL,
1049	0
1050};
1051
1052DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1053MODULE_VERSION(ether, 1);
1054