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