if_ethersubr.c revision 150987
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30 * $FreeBSD: head/sys/net/if_ethersubr.c 150987 2005-10-06 01:21:40Z thompsa $
31 */
32
33#include "opt_atalk.h"
34#include "opt_inet.h"
35#include "opt_inet6.h"
36#include "opt_ipx.h"
37#include "opt_mac.h"
38#include "opt_netgraph.h"
39#include "opt_carp.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/mac.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/mbuf.h>
48#include <sys/random.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/if_arp.h>
55#include <net/netisr.h>
56#include <net/route.h>
57#include <net/if_llc.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/bpf.h>
61#include <net/ethernet.h>
62#include <net/if_vlan_var.h>
63
64#if defined(INET) || defined(INET6)
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#include <netinet/if_ether.h>
68#include <netinet/ip_fw.h>
69#include <netinet/ip_dummynet.h>
70#endif
71#ifdef INET6
72#include <netinet6/nd6.h>
73#endif
74
75#ifdef DEV_CARP
76#include <netinet/ip_carp.h>
77#endif
78
79#ifdef IPX
80#include <netipx/ipx.h>
81#include <netipx/ipx_if.h>
82#endif
83int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
84int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
85		struct sockaddr *dst, short *tp, int *hlen);
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
95extern u_char	at_org_code[3];
96extern u_char	aarp_org_code[3];
97#endif /* NETATALK */
98
99/* netgraph node hooks for ng_ether(4) */
100void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
101void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
102int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
103void	(*ng_ether_attach_p)(struct ifnet *ifp);
104void	(*ng_ether_detach_p)(struct ifnet *ifp);
105
106void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
107
108/* bridge support */
109struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
110int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
111		struct sockaddr *, struct rtentry *);
112void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
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/* XXX: should be in an arp support file, not here */
121MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
122
123#define senderr(e) do { error = (e); goto bad;} while (0)
124
125#if defined(INET) || defined(INET6)
126int
127ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
128	struct ip_fw **rule, int shared);
129static int ether_ipfw;
130#endif
131
132/*
133 * Ethernet output routine.
134 * Encapsulate a packet of type family for the local net.
135 * Use trailer local net encapsulation if enough data in first
136 * packet leaves a multiple of 512 bytes of data in remainder.
137 * Assumes that ifp is actually pointer to arpcom structure.
138 */
139int
140ether_output(struct ifnet *ifp, struct mbuf *m,
141	struct sockaddr *dst, struct rtentry *rt0)
142{
143	short type;
144	int error, hdrcmplt = 0;
145	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
146	struct ether_header *eh;
147	int loop_copy = 0;
148	int hlen;	/* link layer header length */
149
150#ifdef MAC
151	error = mac_check_ifnet_transmit(ifp, m);
152	if (error)
153		senderr(error);
154#endif
155
156	if (ifp->if_flags & IFF_MONITOR)
157		senderr(ENETDOWN);
158	if (!((ifp->if_flags & IFF_UP) &&
159	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
160		senderr(ENETDOWN);
161
162	hlen = ETHER_HDR_LEN;
163	switch (dst->sa_family) {
164#ifdef INET
165	case AF_INET:
166		error = arpresolve(ifp, rt0, m, dst, edst);
167		if (error)
168			return (error == EWOULDBLOCK ? 0 : error);
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		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
202		if (error)
203			return error;
204		type = htons(ETHERTYPE_IPV6);
205		break;
206#endif
207#ifdef IPX
208	case AF_IPX:
209		if (ef_outputp) {
210		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
211		    if (error)
212			goto bad;
213		} else
214		    type = htons(ETHERTYPE_IPX);
215		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
216		    (caddr_t)edst, sizeof (edst));
217		break;
218#endif
219#ifdef NETATALK
220	case AF_APPLETALK:
221	  {
222	    struct at_ifaddr *aa;
223
224	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
225		    senderr(EHOSTUNREACH); /* XXX */
226	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
227		    return (0);
228	    /*
229	     * In the phase 2 case, need to prepend an mbuf for the llc header.
230	     */
231	    if ( aa->aa_flags & AFA_PHASE2 ) {
232		struct llc llc;
233
234		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
235		if (m == NULL)
236			senderr(ENOBUFS);
237		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
238		llc.llc_control = LLC_UI;
239		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
240		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
241		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
242		type = htons(m->m_pkthdr.len);
243		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
244	    } else {
245		type = htons(ETHERTYPE_AT);
246	    }
247	    break;
248	  }
249#endif /* NETATALK */
250
251	case pseudo_AF_HDRCMPLT:
252		hdrcmplt = 1;
253		eh = (struct ether_header *)dst->sa_data;
254		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
255		/* FALLTHROUGH */
256
257	case AF_UNSPEC:
258		loop_copy = -1; /* if this is for us, don't do it */
259		eh = (struct ether_header *)dst->sa_data;
260		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
261		type = eh->ether_type;
262		break;
263
264	default:
265		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
266		senderr(EAFNOSUPPORT);
267	}
268
269	/*
270	 * Add local net header.  If no space in first mbuf,
271	 * allocate another.
272	 */
273	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
274	if (m == NULL)
275		senderr(ENOBUFS);
276	eh = mtod(m, struct ether_header *);
277	(void)memcpy(&eh->ether_type, &type,
278		sizeof(eh->ether_type));
279	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
280	if (hdrcmplt)
281		(void)memcpy(eh->ether_shost, esrc,
282			sizeof(eh->ether_shost));
283	else
284		(void)memcpy(eh->ether_shost, IFP2ENADDR(ifp),
285			sizeof(eh->ether_shost));
286
287       /*
288	* Bridges require special output handling.
289	*/
290	if (ifp->if_bridge) {
291		KASSERT(bridge_output_p != NULL,
292		    ("%s: if_bridge not loaded!", __func__));
293		return ((*bridge_output_p)(ifp, m, NULL, NULL));
294	}
295
296	/*
297	 * If a simplex interface, and the packet is being sent to our
298	 * Ethernet address or a broadcast address, loopback a copy.
299	 * XXX To make a simplex device behave exactly like a duplex
300	 * device, we should copy in the case of sending to our own
301	 * ethernet address (thus letting the original actually appear
302	 * on the wire). However, we don't do that here for security
303	 * reasons and compatibility with the original behavior.
304	 */
305	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1) &&
306	    m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
307		int csum_flags = 0;
308
309		if (m->m_pkthdr.csum_flags & CSUM_IP)
310			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
311		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
312			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
313
314		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
315			struct mbuf *n;
316
317			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
318				n->m_pkthdr.csum_flags |= csum_flags;
319				if (csum_flags & CSUM_DATA_VALID)
320					n->m_pkthdr.csum_data = 0xffff;
321				(void)if_simloop(ifp, n, dst->sa_family, hlen);
322			} else
323				ifp->if_iqdrops++;
324		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
325				ETHER_ADDR_LEN) == 0) {
326			m->m_pkthdr.csum_flags |= csum_flags;
327			if (csum_flags & CSUM_DATA_VALID)
328				m->m_pkthdr.csum_data = 0xffff;
329			(void) if_simloop(ifp, m, dst->sa_family, hlen);
330			return (0);	/* XXX */
331		}
332	}
333
334#ifdef DEV_CARP
335	if (ifp->if_carp &&
336	    (error = carp_output(ifp, m, dst, NULL)))
337		goto bad;
338#endif
339
340	/* Handle ng_ether(4) processing, if any */
341	if (IFP2AC(ifp)->ac_netgraph != NULL) {
342		KASSERT(ng_ether_output_p != NULL,
343		    ("ng_ether_output_p is NULL"));
344		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
345bad:			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 */
363int
364ether_output_frame(struct ifnet *ifp, struct mbuf *m)
365{
366#if defined(INET) || defined(INET6)
367	struct ip_fw *rule = ip_dn_claim_rule(m);
368#else
369	void *rule = NULL;
370#endif
371	int error;
372
373#if defined(INET) || defined(INET6)
374	if (IPFW_LOADED && ether_ipfw != 0) {
375		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
376			if (m) {
377				m_freem(m);
378				return EACCES;	/* pkt dropped */
379			} else
380				return 0;	/* consumed e.g. in a pipe */
381		}
382	}
383#endif
384
385	/*
386	 * Queue message on interface, update output statistics if
387	 * successful, and start output if interface not yet active.
388	 */
389	IFQ_HANDOFF(ifp, m, error);
390	return (error);
391}
392
393#if defined(INET) || defined(INET6)
394/*
395 * ipfw processing for ethernet packets (in and out).
396 * The second parameter is NULL from ether_demux, and ifp from
397 * ether_output_frame.
398 */
399int
400ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
401	struct ip_fw **rule, int shared)
402{
403	struct ether_header *eh;
404	struct ether_header save_eh;
405	struct mbuf *m;
406	int i;
407	struct ip_fw_args args;
408
409	if (*rule != NULL && fw_one_pass)
410		return 1; /* dummynet packet, already partially processed */
411
412	/*
413	 * I need some amt of data to be contiguous, and in case others need
414	 * the packet (shared==1) also better be in the first mbuf.
415	 */
416	m = *m0;
417	i = min( m->m_pkthdr.len, max_protohdr);
418	if ( shared || m->m_len < i) {
419		m = m_pullup(m, i);
420		if (m == NULL) {
421			*m0 = m;
422			return 0;
423		}
424	}
425	eh = mtod(m, struct ether_header *);
426	save_eh = *eh;			/* save copy for restore below */
427	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
428
429	args.m = m;		/* the packet we are looking at		*/
430	args.oif = dst;		/* destination, if any			*/
431	args.rule = *rule;	/* matching rule to restart		*/
432	args.next_hop = NULL;	/* we do not support forward yet	*/
433	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
434	i = ip_fw_chk_ptr(&args);
435	m = args.m;
436	if (m != NULL) {
437		/*
438		 * Restore Ethernet header, as needed, in case the
439		 * mbuf chain was replaced by ipfw.
440		 */
441		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
442		if (m == NULL) {
443			*m0 = m;
444			return 0;
445		}
446		if (eh != mtod(m, struct ether_header *))
447			bcopy(&save_eh, mtod(m, struct ether_header *),
448				ETHER_HDR_LEN);
449	}
450	*m0 = m;
451	*rule = args.rule;
452
453	if (i == IP_FW_DENY) /* drop */
454		return 0;
455
456	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
457
458	if (i == IP_FW_PASS) /* a PASS rule.  */
459		return 1;
460
461	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
462		/*
463		 * Pass the pkt to dummynet, which consumes it.
464		 * If shared, make a copy and keep the original.
465		 */
466		if (shared) {
467			m = m_copypacket(m, M_DONTWAIT);
468			if (m == NULL)
469				return 0;
470		} else {
471			/*
472			 * Pass the original to dummynet and
473			 * nothing back to the caller
474			 */
475			*m0 = NULL ;
476		}
477		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
478		return 0;
479	}
480	/*
481	 * XXX at some point add support for divert/forward actions.
482	 * If none of the above matches, we have to drop the pkt.
483	 */
484	return 0;
485}
486#endif
487
488/*
489 * Process a received Ethernet packet; the packet is in the
490 * mbuf chain m with the ethernet header at the front.
491 */
492static void
493ether_input(struct ifnet *ifp, struct mbuf *m)
494{
495	struct ether_header *eh;
496	u_short etype;
497
498	/*
499	 * Do consistency checks to verify assumptions
500	 * made by code past this point.
501	 */
502	if ((m->m_flags & M_PKTHDR) == 0) {
503		if_printf(ifp, "discard frame w/o packet header\n");
504		ifp->if_ierrors++;
505		m_freem(m);
506		return;
507	}
508	if (m->m_len < ETHER_HDR_LEN) {
509		/* XXX maybe should pullup? */
510		if_printf(ifp, "discard frame w/o leading ethernet "
511				"header (len %u pkt len %u)\n",
512				m->m_len, m->m_pkthdr.len);
513		ifp->if_ierrors++;
514		m_freem(m);
515		return;
516	}
517	eh = mtod(m, struct ether_header *);
518	etype = ntohs(eh->ether_type);
519	if (m->m_pkthdr.len >
520	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
521		if_printf(ifp, "discard oversize frame "
522				"(ether type %x flags %x len %u > max %lu)\n",
523				etype, m->m_flags, m->m_pkthdr.len,
524				ETHER_MAX_FRAME(ifp, etype,
525						m->m_flags & M_HASFCS));
526		ifp->if_ierrors++;
527		m_freem(m);
528		return;
529	}
530	if (m->m_pkthdr.rcvif == NULL) {
531		if_printf(ifp, "discard frame w/o interface pointer\n");
532		ifp->if_ierrors++;
533		m_freem(m);
534		return;
535	}
536#ifdef DIAGNOSTIC
537	if (m->m_pkthdr.rcvif != ifp) {
538		if_printf(ifp, "Warning, frame marked as received on %s\n",
539			m->m_pkthdr.rcvif->if_xname);
540	}
541#endif
542
543#ifdef MAC
544	/*
545	 * Tag the mbuf with an appropriate MAC label before any other
546	 * consumers can get to it.
547	 */
548	mac_create_mbuf_from_ifnet(ifp, m);
549#endif
550
551	/*
552	 * Give bpf a chance at the packet.
553	 */
554	BPF_MTAP(ifp, m);
555
556	if (ifp->if_flags & IFF_MONITOR) {
557		/*
558		 * Interface marked for monitoring; discard packet.
559		 */
560		m_freem(m);
561		return;
562	}
563
564	/* If the CRC is still on the packet, trim it off. */
565	if (m->m_flags & M_HASFCS) {
566		m_adj(m, -ETHER_CRC_LEN);
567		m->m_flags &= ~M_HASFCS;
568	}
569
570	ifp->if_ibytes += m->m_pkthdr.len;
571
572	/* Handle ng_ether(4) processing, if any */
573	if (IFP2AC(ifp)->ac_netgraph != NULL) {
574		KASSERT(ng_ether_input_p != NULL,
575		    ("ng_ether_input_p is NULL"));
576		(*ng_ether_input_p)(ifp, &m);
577		if (m == NULL)
578			return;
579	}
580
581	/*
582	 * Tap the packet off here for a bridge.  bridge_input()
583	 * will return NULL if it has consumed the packet, otherwise
584	 * it gets processed as normal.  Note that bridge_input()
585	 * will always return the original packet if we need to
586	 * process it locally.
587	 */
588	if (ifp->if_bridge) {
589		KASSERT(bridge_input_p != NULL,
590		    ("%s: if_bridge not loaded!", __func__));
591
592		/* Mark the packet as broadcast or multicast. This is also set
593		 * further down the code in ether_demux() but since the bridge
594		 * input routine rarely returns a mbuf for further processing,
595		 * it is an acceptable duplication.
596		 */
597		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
598			if (bcmp(etherbroadcastaddr, eh->ether_dhost,
599				sizeof(etherbroadcastaddr)) == 0)
600				m->m_flags |= M_BCAST;
601			else
602				m->m_flags |= M_MCAST;
603		}
604
605		m = (*bridge_input_p)(ifp, m);
606		if (m == NULL)
607			return;
608		/*
609		 * Bridge has determined that the packet is for us.
610		 * Update our interface pointer -- we may have had
611		 * to "bridge" the packet locally.
612		 */
613		ifp = m->m_pkthdr.rcvif;
614	}
615
616	/* First chunk of an mbuf contains good entropy */
617	if (harvest.ethernet)
618		random_harvest(m, 16, 3, 0, RANDOM_NET);
619	ether_demux(ifp, m);
620}
621
622/*
623 * Upper layer processing for a received Ethernet packet.
624 */
625void
626ether_demux(struct ifnet *ifp, struct mbuf *m)
627{
628	struct ether_header *eh;
629	int isr;
630	u_short ether_type;
631#if defined(NETATALK)
632	struct llc *l;
633#endif
634#if defined(INET) || defined(INET6)
635	struct ip_fw *rule = ip_dn_claim_rule(m);
636#endif
637
638	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
639
640	eh = mtod(m, struct ether_header *);
641	ether_type = ntohs(eh->ether_type);
642
643#if defined(INET) || defined(INET6)
644	if (rule)	/* packet was already bridged */
645		goto post_stats;
646#endif
647
648	if (!(ifp->if_bridge) &&
649	    !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
650	    ifp->if_nvlans > 0)) {
651#ifdef DEV_CARP
652		/*
653		 * XXX: Okay, we need to call carp_forus() and - if it is for
654		 * us jump over code that does the normal check
655		 * "IFP2ENADDR(ifp) == ether_dhost". The check sequence is a bit
656		 * different from OpenBSD, so we jump over as few code as
657		 * possible, to catch _all_ sanity checks. This needs
658		 * evaluation, to see if the carp ether_dhost values break any
659		 * of these checks!
660		 */
661		if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
662			goto pre_stats;
663#endif
664		/*
665		 * Discard packet if upper layers shouldn't see it because it
666		 * was unicast to a different Ethernet address. If the driver
667		 * is working properly, then this situation can only happen
668		 * when the interface is in promiscuous mode.
669		 *
670		 * If VLANs are active, and this packet has a VLAN tag, do
671		 * not drop it here but pass it on to the VLAN layer, to
672		 * give them a chance to consider it as well (e. g. in case
673		 * bridging is only active on a VLAN).  They will drop it if
674		 * it's undesired.
675		 */
676		if ((ifp->if_flags & IFF_PROMISC) != 0
677		    && !ETHER_IS_MULTICAST(eh->ether_dhost)
678		    && bcmp(eh->ether_dhost,
679		      IFP2ENADDR(ifp), ETHER_ADDR_LEN) != 0
680		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
681			    m_freem(m);
682			    return;
683		}
684	}
685
686#ifdef DEV_CARP
687pre_stats:
688#endif
689	/* Discard packet if interface is not up */
690	if ((ifp->if_flags & IFF_UP) == 0) {
691		m_freem(m);
692		return;
693	}
694	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
695		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
696		    sizeof(etherbroadcastaddr)) == 0)
697			m->m_flags |= M_BCAST;
698		else
699			m->m_flags |= M_MCAST;
700	}
701	if (m->m_flags & (M_BCAST|M_MCAST))
702		ifp->if_imcasts++;
703
704#if defined(INET) || defined(INET6)
705post_stats:
706	if (IPFW_LOADED && ether_ipfw != 0) {
707		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
708			if (m)
709				m_freem(m);
710			return;
711		}
712	}
713#endif
714
715	/*
716	 * Check to see if the device performed the VLAN decapsulation and
717	 * provided us with the tag.
718	 */
719	if (m->m_flags & M_VLANTAG) {
720		/*
721		 * If no VLANs are configured, drop.
722		 */
723		if (ifp->if_nvlans == 0) {
724			ifp->if_noproto++;
725			m_freem(m);
726			return;
727		}
728		/*
729		 * vlan_input() will either recursively call ether_input()
730		 * or drop the packet.
731		 */
732		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
733		(*vlan_input_p)(ifp, m);
734		return;
735	}
736
737	/*
738	 * Handle protocols that expect to have the Ethernet header
739	 * (and possibly FCS) intact.
740	 */
741	switch (ether_type) {
742	case ETHERTYPE_VLAN:
743		if (ifp->if_nvlans != 0) {
744			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
745			(*vlan_input_p)(ifp, m);
746		} else {
747			ifp->if_noproto++;
748			m_freem(m);
749		}
750		return;
751	}
752
753	/* Strip off Ethernet header. */
754	m_adj(m, ETHER_HDR_LEN);
755
756	/* If the CRC is still on the packet, trim it off. */
757	if (m->m_flags & M_HASFCS) {
758		m_adj(m, -ETHER_CRC_LEN);
759		m->m_flags &= ~M_HASFCS;
760	}
761
762	switch (ether_type) {
763#ifdef INET
764	case ETHERTYPE_IP:
765		if (ip_fastforward(m))
766			return;
767		isr = NETISR_IP;
768		break;
769
770	case ETHERTYPE_ARP:
771		if (ifp->if_flags & IFF_NOARP) {
772			/* Discard packet if ARP is disabled on interface */
773			m_freem(m);
774			return;
775		}
776		isr = NETISR_ARP;
777		break;
778#endif
779#ifdef IPX
780	case ETHERTYPE_IPX:
781		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
782			return;
783		isr = NETISR_IPX;
784		break;
785#endif
786#ifdef INET6
787	case ETHERTYPE_IPV6:
788		isr = NETISR_IPV6;
789		break;
790#endif
791#ifdef NETATALK
792	case ETHERTYPE_AT:
793		isr = NETISR_ATALK1;
794		break;
795	case ETHERTYPE_AARP:
796		isr = NETISR_AARP;
797		break;
798#endif /* NETATALK */
799	default:
800#ifdef IPX
801		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
802			return;
803#endif /* IPX */
804#if defined(NETATALK)
805		if (ether_type > ETHERMTU)
806			goto discard;
807		l = mtod(m, struct llc *);
808		if (l->llc_dsap == LLC_SNAP_LSAP &&
809		    l->llc_ssap == LLC_SNAP_LSAP &&
810		    l->llc_control == LLC_UI) {
811			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
812			    sizeof(at_org_code)) == 0 &&
813			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
814				m_adj(m, LLC_SNAPFRAMELEN);
815				isr = NETISR_ATALK2;
816				break;
817			}
818			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
819			    sizeof(aarp_org_code)) == 0 &&
820			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
821				m_adj(m, LLC_SNAPFRAMELEN);
822				isr = NETISR_AARP;
823				break;
824			}
825		}
826#endif /* NETATALK */
827		goto discard;
828	}
829	netisr_dispatch(isr, m);
830	return;
831
832discard:
833	/*
834	 * Packet is to be discarded.  If netgraph is present,
835	 * hand the packet to it for last chance processing;
836	 * otherwise dispose of it.
837	 */
838	if (IFP2AC(ifp)->ac_netgraph != NULL) {
839		KASSERT(ng_ether_input_orphan_p != NULL,
840		    ("ng_ether_input_orphan_p is NULL"));
841		/*
842		 * Put back the ethernet header so netgraph has a
843		 * consistent view of inbound packets.
844		 */
845		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
846		(*ng_ether_input_orphan_p)(ifp, m);
847		return;
848	}
849	m_freem(m);
850}
851
852/*
853 * Convert Ethernet address to printable (loggable) representation.
854 * This routine is for compatibility; it's better to just use
855 *
856 *	printf("%6D", <pointer to address>, ":");
857 *
858 * since there's no static buffer involved.
859 */
860char *
861ether_sprintf(const u_char *ap)
862{
863	static char etherbuf[18];
864	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
865	return (etherbuf);
866}
867
868/*
869 * Perform common duties while attaching to interface list
870 */
871void
872ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
873{
874	int i;
875	struct ifaddr *ifa;
876	struct sockaddr_dl *sdl;
877
878	ifp->if_addrlen = ETHER_ADDR_LEN;
879	ifp->if_hdrlen = ETHER_HDR_LEN;
880	if_attach(ifp);
881	ifp->if_mtu = ETHERMTU;
882	ifp->if_output = ether_output;
883	ifp->if_input = ether_input;
884	ifp->if_resolvemulti = ether_resolvemulti;
885	if (ifp->if_baudrate == 0)
886		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
887	ifp->if_broadcastaddr = etherbroadcastaddr;
888
889	ifa = ifaddr_byindex(ifp->if_index);
890	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
891	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
892	sdl->sdl_type = IFT_ETHER;
893	sdl->sdl_alen = ifp->if_addrlen;
894	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
895	/*
896	 * XXX: This doesn't belong here; we do it until
897	 * XXX:  all drivers are cleaned up
898	 */
899	if (llc != IFP2ENADDR(ifp))
900		bcopy(llc, IFP2ENADDR(ifp), ifp->if_addrlen);
901
902	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
903	if (ng_ether_attach_p != NULL)
904		(*ng_ether_attach_p)(ifp);
905
906	/* Announce Ethernet MAC address if non-zero. */
907	for (i = 0; i < ifp->if_addrlen; i++)
908		if (llc[i] != 0)
909			break;
910	if (i != ifp->if_addrlen)
911		if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
912	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
913		if_printf(ifp, "if_start running deferred for Giant\n");
914}
915
916/*
917 * Perform common duties while detaching an Ethernet interface
918 */
919void
920ether_ifdetach(struct ifnet *ifp)
921{
922	if (IFP2AC(ifp)->ac_netgraph != NULL) {
923		KASSERT(ng_ether_detach_p != NULL,
924		    ("ng_ether_detach_p is NULL"));
925		(*ng_ether_detach_p)(ifp);
926	}
927	bpfdetach(ifp);
928	if_detach(ifp);
929}
930
931SYSCTL_DECL(_net_link);
932SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
933#if defined(INET) || defined(INET6)
934SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
935	    &ether_ipfw,0,"Pass ether pkts through firewall");
936#endif
937
938#if 0
939/*
940 * This is for reference.  We have a table-driven version
941 * of the little-endian crc32 generator, which is faster
942 * than the double-loop.
943 */
944uint32_t
945ether_crc32_le(const uint8_t *buf, size_t len)
946{
947	size_t i;
948	uint32_t crc;
949	int bit;
950	uint8_t data;
951
952	crc = 0xffffffff;	/* initial value */
953
954	for (i = 0; i < len; i++) {
955		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
956			carry = (crc ^ data) & 1;
957			crc >>= 1;
958			if (carry)
959				crc = (crc ^ ETHER_CRC_POLY_LE);
960	}
961
962	return (crc);
963}
964#else
965uint32_t
966ether_crc32_le(const uint8_t *buf, size_t len)
967{
968	static const uint32_t crctab[] = {
969		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
970		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
971		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
972		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
973	};
974	size_t i;
975	uint32_t crc;
976
977	crc = 0xffffffff;	/* initial value */
978
979	for (i = 0; i < len; i++) {
980		crc ^= buf[i];
981		crc = (crc >> 4) ^ crctab[crc & 0xf];
982		crc = (crc >> 4) ^ crctab[crc & 0xf];
983	}
984
985	return (crc);
986}
987#endif
988
989uint32_t
990ether_crc32_be(const uint8_t *buf, size_t len)
991{
992	size_t i;
993	uint32_t crc, carry;
994	int bit;
995	uint8_t data;
996
997	crc = 0xffffffff;	/* initial value */
998
999	for (i = 0; i < len; i++) {
1000		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1001			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1002			crc <<= 1;
1003			if (carry)
1004				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1005		}
1006	}
1007
1008	return (crc);
1009}
1010
1011int
1012ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
1013{
1014	struct ifaddr *ifa = (struct ifaddr *) data;
1015	struct ifreq *ifr = (struct ifreq *) data;
1016	int error = 0;
1017
1018	switch (command) {
1019	case SIOCSIFADDR:
1020		ifp->if_flags |= IFF_UP;
1021
1022		switch (ifa->ifa_addr->sa_family) {
1023#ifdef INET
1024		case AF_INET:
1025			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1026			arp_ifinit(ifp, ifa);
1027			break;
1028#endif
1029#ifdef IPX
1030		/*
1031		 * XXX - This code is probably wrong
1032		 */
1033		case AF_IPX:
1034			{
1035			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1036
1037			if (ipx_nullhost(*ina))
1038				ina->x_host =
1039				    *(union ipx_host *)
1040				    IFP2ENADDR(ifp);
1041			else {
1042				bcopy((caddr_t) ina->x_host.c_host,
1043				      (caddr_t) IFP2ENADDR(ifp),
1044				      ETHER_ADDR_LEN);
1045			}
1046
1047			/*
1048			 * Set new address
1049			 */
1050			ifp->if_init(ifp->if_softc);
1051			break;
1052			}
1053#endif
1054		default:
1055			ifp->if_init(ifp->if_softc);
1056			break;
1057		}
1058		break;
1059
1060	case SIOCGIFADDR:
1061		{
1062			struct sockaddr *sa;
1063
1064			sa = (struct sockaddr *) & ifr->ifr_data;
1065			bcopy(IFP2ENADDR(ifp),
1066			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1067		}
1068		break;
1069
1070	case SIOCSIFMTU:
1071		/*
1072		 * Set the interface MTU.
1073		 */
1074		if (ifr->ifr_mtu > ETHERMTU) {
1075			error = EINVAL;
1076		} else {
1077			ifp->if_mtu = ifr->ifr_mtu;
1078		}
1079		break;
1080	default:
1081		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1082		break;
1083	}
1084	return (error);
1085}
1086
1087static int
1088ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1089	struct sockaddr *sa)
1090{
1091	struct sockaddr_dl *sdl;
1092#ifdef INET
1093	struct sockaddr_in *sin;
1094#endif
1095#ifdef INET6
1096	struct sockaddr_in6 *sin6;
1097#endif
1098	u_char *e_addr;
1099
1100	switch(sa->sa_family) {
1101	case AF_LINK:
1102		/*
1103		 * No mapping needed. Just check that it's a valid MC address.
1104		 */
1105		sdl = (struct sockaddr_dl *)sa;
1106		e_addr = LLADDR(sdl);
1107		if (!ETHER_IS_MULTICAST(e_addr))
1108			return EADDRNOTAVAIL;
1109		*llsa = 0;
1110		return 0;
1111
1112#ifdef INET
1113	case AF_INET:
1114		sin = (struct sockaddr_in *)sa;
1115		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1116			return EADDRNOTAVAIL;
1117		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1118		       M_NOWAIT|M_ZERO);
1119		if (sdl == NULL)
1120			return ENOMEM;
1121		sdl->sdl_len = sizeof *sdl;
1122		sdl->sdl_family = AF_LINK;
1123		sdl->sdl_index = ifp->if_index;
1124		sdl->sdl_type = IFT_ETHER;
1125		sdl->sdl_alen = ETHER_ADDR_LEN;
1126		e_addr = LLADDR(sdl);
1127		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1128		*llsa = (struct sockaddr *)sdl;
1129		return 0;
1130#endif
1131#ifdef INET6
1132	case AF_INET6:
1133		sin6 = (struct sockaddr_in6 *)sa;
1134		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1135			/*
1136			 * An IP6 address of 0 means listen to all
1137			 * of the Ethernet multicast address used for IP6.
1138			 * (This is used for multicast routers.)
1139			 */
1140			ifp->if_flags |= IFF_ALLMULTI;
1141			*llsa = 0;
1142			return 0;
1143		}
1144		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1145			return EADDRNOTAVAIL;
1146		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1147		       M_NOWAIT|M_ZERO);
1148		if (sdl == NULL)
1149			return (ENOMEM);
1150		sdl->sdl_len = sizeof *sdl;
1151		sdl->sdl_family = AF_LINK;
1152		sdl->sdl_index = ifp->if_index;
1153		sdl->sdl_type = IFT_ETHER;
1154		sdl->sdl_alen = ETHER_ADDR_LEN;
1155		e_addr = LLADDR(sdl);
1156		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1157		*llsa = (struct sockaddr *)sdl;
1158		return 0;
1159#endif
1160
1161	default:
1162		/*
1163		 * Well, the text isn't quite right, but it's the name
1164		 * that counts...
1165		 */
1166		return EAFNOSUPPORT;
1167	}
1168}
1169
1170static void*
1171ether_alloc(u_char type, struct ifnet *ifp)
1172{
1173	struct arpcom	*ac;
1174
1175	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1176	ac->ac_ifp = ifp;
1177
1178	return (ac);
1179}
1180
1181static void
1182ether_free(void *com, u_char type)
1183{
1184
1185	free(com, M_ARPCOM);
1186}
1187
1188static int
1189ether_modevent(module_t mod, int type, void *data)
1190{
1191
1192	switch (type) {
1193	case MOD_LOAD:
1194		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1195		break;
1196	case MOD_UNLOAD:
1197		if_deregister_com_alloc(IFT_ETHER);
1198		break;
1199	default:
1200		return EOPNOTSUPP;
1201	}
1202
1203	return (0);
1204}
1205
1206static moduledata_t ether_mod = {
1207	"ether",
1208	ether_modevent,
1209	0
1210};
1211
1212DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1213MODULE_VERSION(ether, 1);
1214