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