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