if_ethersubr.c revision 263140
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 263140 2014-03-14 02:58:48Z glebius $
31 */
32
33#include "opt_atalk.h"
34#include "opt_inet.h"
35#include "opt_inet6.h"
36#include "opt_netgraph.h"
37#include "opt_mbuf_profiling.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/lock.h>
43#include <sys/malloc.h>
44#include <sys/module.h>
45#include <sys/mbuf.h>
46#include <sys/random.h>
47#include <sys/socket.h>
48#include <sys/sockio.h>
49#include <sys/sysctl.h>
50#include <sys/uuid.h>
51
52#include <net/if.h>
53#include <net/if_var.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#include <net/if_llatbl.h>
65#include <net/pfil.h>
66#include <net/vnet.h>
67
68#include <netpfil/pf/pf_mtag.h>
69
70#if defined(INET) || defined(INET6)
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#include <netinet/if_ether.h>
74#include <netinet/ip_carp.h>
75#include <netinet/ip_var.h>
76#endif
77#ifdef INET6
78#include <netinet6/nd6.h>
79#endif
80
81int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
82int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
83		const struct sockaddr *dst, short *tp, int *hlen);
84
85#ifdef NETATALK
86#include <netatalk/at.h>
87#include <netatalk/at_var.h>
88#include <netatalk/at_extern.h>
89
90#define llc_snap_org_code llc_un.type_snap.org_code
91#define llc_snap_ether_type llc_un.type_snap.ether_type
92
93extern u_char	at_org_code[3];
94extern u_char	aarp_org_code[3];
95#endif /* NETATALK */
96
97#include <security/mac/mac_framework.h>
98
99#ifdef CTASSERT
100CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
101CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
102#endif
103
104VNET_DEFINE(struct pfil_head, link_pfil_hook);	/* Packet filter hooks */
105
106/* netgraph node hooks for ng_ether(4) */
107void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
108void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
109int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
110void	(*ng_ether_attach_p)(struct ifnet *ifp);
111void	(*ng_ether_detach_p)(struct ifnet *ifp);
112
113void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
114
115/* if_bridge(4) support */
116struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
117int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
118		struct sockaddr *, struct rtentry *);
119void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
120
121/* if_lagg(4) support */
122struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
123
124static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
125			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
126
127static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
128		struct sockaddr *);
129#ifdef VIMAGE
130static	void ether_reassign(struct ifnet *, struct vnet *, char *);
131#endif
132
133/* XXX: should be in an arp support file, not here */
134static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
135
136#define	ETHER_IS_BROADCAST(addr) \
137	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
138
139#define senderr(e) do { error = (e); goto bad;} while (0)
140
141static void
142update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
143{
144	int csum_flags = 0;
145
146	if (src->m_pkthdr.csum_flags & CSUM_IP)
147		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
148	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
149		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
150	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
151		csum_flags |= CSUM_SCTP_VALID;
152	dst->m_pkthdr.csum_flags |= csum_flags;
153	if (csum_flags & CSUM_DATA_VALID)
154		dst->m_pkthdr.csum_data = 0xffff;
155}
156
157/*
158 * Ethernet output routine.
159 * Encapsulate a packet of type family for the local net.
160 * Use trailer local net encapsulation if enough data in first
161 * packet leaves a multiple of 512 bytes of data in remainder.
162 */
163int
164ether_output(struct ifnet *ifp, struct mbuf *m,
165	const struct sockaddr *dst, struct route *ro)
166{
167	short type;
168	int error = 0, hdrcmplt = 0;
169	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
170	struct llentry *lle = NULL;
171	struct rtentry *rt0 = NULL;
172	struct ether_header *eh;
173	struct pf_mtag *t;
174	int loop_copy = 1;
175	int hlen;	/* link layer header length */
176
177	if (ro != NULL) {
178		if (!(m->m_flags & (M_BCAST | M_MCAST)))
179			lle = ro->ro_lle;
180		rt0 = ro->ro_rt;
181	}
182#ifdef MAC
183	error = mac_ifnet_check_transmit(ifp, m);
184	if (error)
185		senderr(error);
186#endif
187
188	M_PROFILE(m);
189	if (ifp->if_flags & IFF_MONITOR)
190		senderr(ENETDOWN);
191	if (!((ifp->if_flags & IFF_UP) &&
192	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
193		senderr(ENETDOWN);
194
195	hlen = ETHER_HDR_LEN;
196	switch (dst->sa_family) {
197#ifdef INET
198	case AF_INET:
199		if (lle != NULL && (lle->la_flags & LLE_VALID))
200			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
201		else
202			error = arpresolve(ifp, rt0, m, dst, edst, &lle);
203		if (error)
204			return (error == EWOULDBLOCK ? 0 : error);
205		type = htons(ETHERTYPE_IP);
206		break;
207	case AF_ARP:
208	{
209		struct arphdr *ah;
210		ah = mtod(m, struct arphdr *);
211		ah->ar_hrd = htons(ARPHRD_ETHER);
212
213		loop_copy = 0; /* if this is for us, don't do it */
214
215		switch(ntohs(ah->ar_op)) {
216		case ARPOP_REVREQUEST:
217		case ARPOP_REVREPLY:
218			type = htons(ETHERTYPE_REVARP);
219			break;
220		case ARPOP_REQUEST:
221		case ARPOP_REPLY:
222		default:
223			type = htons(ETHERTYPE_ARP);
224			break;
225		}
226
227		if (m->m_flags & M_BCAST)
228			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
229		else
230			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
231
232	}
233	break;
234#endif
235#ifdef INET6
236	case AF_INET6:
237		if (lle != NULL && (lle->la_flags & LLE_VALID))
238			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
239		else
240			error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
241		if (error)
242			return error;
243		type = htons(ETHERTYPE_IPV6);
244		break;
245#endif
246#ifdef NETATALK
247	case AF_APPLETALK:
248	  {
249	    struct at_ifaddr *aa;
250
251	    if ((aa = at_ifawithnet((const struct sockaddr_at *)dst)) == NULL)
252		    senderr(EHOSTUNREACH); /* XXX */
253	    if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
254		    ifa_free(&aa->aa_ifa);
255		    return (0);
256	    }
257	    /*
258	     * In the phase 2 case, need to prepend an mbuf for the llc header.
259	     */
260	    if ( aa->aa_flags & AFA_PHASE2 ) {
261		struct llc llc;
262
263		ifa_free(&aa->aa_ifa);
264		M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
265		if (m == NULL)
266			senderr(ENOBUFS);
267		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
268		llc.llc_control = LLC_UI;
269		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
270		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
271		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
272		type = htons(m->m_pkthdr.len);
273		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
274	    } else {
275		ifa_free(&aa->aa_ifa);
276		type = htons(ETHERTYPE_AT);
277	    }
278	    break;
279	  }
280#endif /* NETATALK */
281
282	case pseudo_AF_HDRCMPLT:
283	    {
284		const struct ether_header *eh;
285
286		hdrcmplt = 1;
287		eh = (const struct ether_header *)dst->sa_data;
288		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
289		/* FALLTHROUGH */
290
291	case AF_UNSPEC:
292		loop_copy = 0; /* if this is for us, don't do it */
293		eh = (const struct ether_header *)dst->sa_data;
294		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
295		type = eh->ether_type;
296		break;
297            }
298	default:
299		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
300		senderr(EAFNOSUPPORT);
301	}
302
303	if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
304		update_mbuf_csumflags(m, m);
305		return (if_simloop(ifp, m, dst->sa_family, 0));
306	}
307
308	/*
309	 * Add local net header.  If no space in first mbuf,
310	 * allocate another.
311	 */
312	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
313	if (m == NULL)
314		senderr(ENOBUFS);
315	eh = mtod(m, struct ether_header *);
316	(void)memcpy(&eh->ether_type, &type,
317		sizeof(eh->ether_type));
318	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
319	if (hdrcmplt)
320		(void)memcpy(eh->ether_shost, esrc,
321			sizeof(eh->ether_shost));
322	else
323		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
324			sizeof(eh->ether_shost));
325
326	/*
327	 * If a simplex interface, and the packet is being sent to our
328	 * Ethernet address or a broadcast address, loopback a copy.
329	 * XXX To make a simplex device behave exactly like a duplex
330	 * device, we should copy in the case of sending to our own
331	 * ethernet address (thus letting the original actually appear
332	 * on the wire). However, we don't do that here for security
333	 * reasons and compatibility with the original behavior.
334	 */
335	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
336	    ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
337		if (m->m_flags & M_BCAST) {
338			struct mbuf *n;
339
340			/*
341			 * Because if_simloop() modifies the packet, we need a
342			 * writable copy through m_dup() instead of a readonly
343			 * one as m_copy[m] would give us. The alternative would
344			 * be to modify if_simloop() to handle the readonly mbuf,
345			 * but performancewise it is mostly equivalent (trading
346			 * extra data copying vs. extra locking).
347			 *
348			 * XXX This is a local workaround.  A number of less
349			 * often used kernel parts suffer from the same bug.
350			 * See PR kern/105943 for a proposed general solution.
351			 */
352			if ((n = m_dup(m, M_NOWAIT)) != NULL) {
353				update_mbuf_csumflags(m, n);
354				(void)if_simloop(ifp, n, dst->sa_family, hlen);
355			} else
356				ifp->if_iqdrops++;
357		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
358				ETHER_ADDR_LEN) == 0) {
359			update_mbuf_csumflags(m, m);
360			(void) if_simloop(ifp, m, dst->sa_family, hlen);
361			return (0);	/* XXX */
362		}
363	}
364
365       /*
366	* Bridges require special output handling.
367	*/
368	if (ifp->if_bridge) {
369		BRIDGE_OUTPUT(ifp, m, error);
370		return (error);
371	}
372
373#if defined(INET) || defined(INET6)
374	if (ifp->if_carp &&
375	    (error = (*carp_output_p)(ifp, m, dst)))
376		goto bad;
377#endif
378
379	/* Handle ng_ether(4) processing, if any */
380	if (IFP2AC(ifp)->ac_netgraph != NULL) {
381		KASSERT(ng_ether_output_p != NULL,
382		    ("ng_ether_output_p is NULL"));
383		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
384bad:			if (m != NULL)
385				m_freem(m);
386			return (error);
387		}
388		if (m == NULL)
389			return (0);
390	}
391
392	/* Continue with link-layer output */
393	return ether_output_frame(ifp, m);
394}
395
396/*
397 * Ethernet link layer output routine to send a raw frame to the device.
398 *
399 * This assumes that the 14 byte Ethernet header is present and contiguous
400 * in the first mbuf (if BRIDGE'ing).
401 */
402int
403ether_output_frame(struct ifnet *ifp, struct mbuf *m)
404{
405	int i;
406
407	if (PFIL_HOOKED(&V_link_pfil_hook)) {
408		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
409
410		if (i != 0)
411			return (EACCES);
412
413		if (m == NULL)
414			return (0);
415	}
416
417	/*
418	 * Queue message on interface, update output statistics if
419	 * successful, and start output if interface not yet active.
420	 */
421	return ((ifp->if_transmit)(ifp, m));
422}
423
424#if defined(INET) || defined(INET6)
425#endif
426
427/*
428 * Process a received Ethernet packet; the packet is in the
429 * mbuf chain m with the ethernet header at the front.
430 */
431static void
432ether_input_internal(struct ifnet *ifp, struct mbuf *m)
433{
434	struct ether_header *eh;
435	u_short etype;
436
437	if ((ifp->if_flags & IFF_UP) == 0) {
438		m_freem(m);
439		return;
440	}
441#ifdef DIAGNOSTIC
442	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
443		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
444		m_freem(m);
445		return;
446	}
447#endif
448	/*
449	 * Do consistency checks to verify assumptions
450	 * made by code past this point.
451	 */
452	if ((m->m_flags & M_PKTHDR) == 0) {
453		if_printf(ifp, "discard frame w/o packet header\n");
454		ifp->if_ierrors++;
455		m_freem(m);
456		return;
457	}
458	if (m->m_len < ETHER_HDR_LEN) {
459		/* XXX maybe should pullup? */
460		if_printf(ifp, "discard frame w/o leading ethernet "
461				"header (len %u pkt len %u)\n",
462				m->m_len, m->m_pkthdr.len);
463		ifp->if_ierrors++;
464		m_freem(m);
465		return;
466	}
467	eh = mtod(m, struct ether_header *);
468	etype = ntohs(eh->ether_type);
469	if (m->m_pkthdr.rcvif == NULL) {
470		if_printf(ifp, "discard frame w/o interface pointer\n");
471		ifp->if_ierrors++;
472		m_freem(m);
473		return;
474	}
475#ifdef DIAGNOSTIC
476	if (m->m_pkthdr.rcvif != ifp) {
477		if_printf(ifp, "Warning, frame marked as received on %s\n",
478			m->m_pkthdr.rcvif->if_xname);
479	}
480#endif
481
482	CURVNET_SET_QUIET(ifp->if_vnet);
483
484	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
485		if (ETHER_IS_BROADCAST(eh->ether_dhost))
486			m->m_flags |= M_BCAST;
487		else
488			m->m_flags |= M_MCAST;
489		ifp->if_imcasts++;
490	}
491
492#ifdef MAC
493	/*
494	 * Tag the mbuf with an appropriate MAC label before any other
495	 * consumers can get to it.
496	 */
497	mac_ifnet_create_mbuf(ifp, m);
498#endif
499
500	/*
501	 * Give bpf a chance at the packet.
502	 */
503	ETHER_BPF_MTAP(ifp, m);
504
505	/*
506	 * If the CRC is still on the packet, trim it off. We do this once
507	 * and once only in case we are re-entered. Nothing else on the
508	 * Ethernet receive path expects to see the FCS.
509	 */
510	if (m->m_flags & M_HASFCS) {
511		m_adj(m, -ETHER_CRC_LEN);
512		m->m_flags &= ~M_HASFCS;
513	}
514
515	if (!(ifp->if_capenable & IFCAP_HWSTATS))
516		ifp->if_ibytes += m->m_pkthdr.len;
517
518	/* Allow monitor mode to claim this frame, after stats are updated. */
519	if (ifp->if_flags & IFF_MONITOR) {
520		m_freem(m);
521		CURVNET_RESTORE();
522		return;
523	}
524
525	/* Handle input from a lagg(4) port */
526	if (ifp->if_type == IFT_IEEE8023ADLAG) {
527		KASSERT(lagg_input_p != NULL,
528		    ("%s: if_lagg not loaded!", __func__));
529		m = (*lagg_input_p)(ifp, m);
530		if (m != NULL)
531			ifp = m->m_pkthdr.rcvif;
532		else {
533			CURVNET_RESTORE();
534			return;
535		}
536	}
537
538	/*
539	 * If the hardware did not process an 802.1Q tag, do this now,
540	 * to allow 802.1P priority frames to be passed to the main input
541	 * path correctly.
542	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
543	 */
544	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
545		struct ether_vlan_header *evl;
546
547		if (m->m_len < sizeof(*evl) &&
548		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
549#ifdef DIAGNOSTIC
550			if_printf(ifp, "cannot pullup VLAN header\n");
551#endif
552			ifp->if_ierrors++;
553			m_freem(m);
554			CURVNET_RESTORE();
555			return;
556		}
557
558		evl = mtod(m, struct ether_vlan_header *);
559		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
560		m->m_flags |= M_VLANTAG;
561
562		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
563		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
564		m_adj(m, ETHER_VLAN_ENCAP_LEN);
565		eh = mtod(m, struct ether_header *);
566	}
567
568	M_SETFIB(m, ifp->if_fib);
569
570	/* Allow ng_ether(4) to claim this frame. */
571	if (IFP2AC(ifp)->ac_netgraph != NULL) {
572		KASSERT(ng_ether_input_p != NULL,
573		    ("%s: ng_ether_input_p is NULL", __func__));
574		m->m_flags &= ~M_PROMISC;
575		(*ng_ether_input_p)(ifp, &m);
576		if (m == NULL) {
577			CURVNET_RESTORE();
578			return;
579		}
580		eh = mtod(m, struct ether_header *);
581	}
582
583	/*
584	 * Allow if_bridge(4) to claim this frame.
585	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
586	 * and the frame should be delivered locally.
587	 */
588	if (ifp->if_bridge != NULL) {
589		m->m_flags &= ~M_PROMISC;
590		BRIDGE_INPUT(ifp, m);
591		if (m == NULL) {
592			CURVNET_RESTORE();
593			return;
594		}
595		eh = mtod(m, struct ether_header *);
596	}
597
598#if defined(INET) || defined(INET6)
599	/*
600	 * Clear M_PROMISC on frame so that carp(4) will see it when the
601	 * mbuf flows up to Layer 3.
602	 * FreeBSD's implementation of carp(4) uses the inprotosw
603	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
604	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
605	 * is outside the scope of the M_PROMISC test below.
606	 * TODO: Maintain a hash table of ethernet addresses other than
607	 * ether_dhost which may be active on this ifp.
608	 */
609	if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
610		m->m_flags &= ~M_PROMISC;
611	} else
612#endif
613	{
614		/*
615		 * If the frame received was not for our MAC address, set the
616		 * M_PROMISC flag on the mbuf chain. The frame may need to
617		 * be seen by the rest of the Ethernet input path in case of
618		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
619		 * seen by upper protocol layers.
620		 */
621		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
622		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
623			m->m_flags |= M_PROMISC;
624	}
625
626	if (harvest.ethernet)
627		random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
628
629	ether_demux(ifp, m);
630	CURVNET_RESTORE();
631}
632
633/*
634 * Ethernet input dispatch; by default, direct dispatch here regardless of
635 * global configuration.
636 */
637static void
638ether_nh_input(struct mbuf *m)
639{
640
641	ether_input_internal(m->m_pkthdr.rcvif, m);
642}
643
644static struct netisr_handler	ether_nh = {
645	.nh_name = "ether",
646	.nh_handler = ether_nh_input,
647	.nh_proto = NETISR_ETHER,
648	.nh_policy = NETISR_POLICY_SOURCE,
649	.nh_dispatch = NETISR_DISPATCH_DIRECT,
650};
651
652static void
653ether_init(__unused void *arg)
654{
655
656	netisr_register(&ether_nh);
657}
658SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
659
660static void
661vnet_ether_init(__unused void *arg)
662{
663	int i;
664
665	/* Initialize packet filter hooks. */
666	V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
667	V_link_pfil_hook.ph_af = AF_LINK;
668	if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
669		printf("%s: WARNING: unable to register pfil link hook, "
670			"error %d\n", __func__, i);
671}
672VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
673    vnet_ether_init, NULL);
674
675static void
676vnet_ether_destroy(__unused void *arg)
677{
678	int i;
679
680	if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
681		printf("%s: WARNING: unable to unregister pfil link hook, "
682			"error %d\n", __func__, i);
683}
684VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
685    vnet_ether_destroy, NULL);
686
687
688
689static void
690ether_input(struct ifnet *ifp, struct mbuf *m)
691{
692
693	struct mbuf *mn;
694
695	/*
696	 * The drivers are allowed to pass in a chain of packets linked with
697	 * m_nextpkt. We split them up into separate packets here and pass
698	 * them up. This allows the drivers to amortize the receive lock.
699	 */
700	while (m) {
701		mn = m->m_nextpkt;
702		m->m_nextpkt = NULL;
703
704		/*
705		 * We will rely on rcvif being set properly in the deferred context,
706		 * so assert it is correct here.
707		 */
708		KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
709		netisr_dispatch(NETISR_ETHER, m);
710		m = mn;
711	}
712}
713
714/*
715 * Upper layer processing for a received Ethernet packet.
716 */
717void
718ether_demux(struct ifnet *ifp, struct mbuf *m)
719{
720	struct ether_header *eh;
721	int i, isr;
722	u_short ether_type;
723#if defined(NETATALK)
724	struct llc *l;
725#endif
726
727	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
728
729	/* Do not grab PROMISC frames in case we are re-entered. */
730	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
731		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
732
733		if (i != 0 || m == NULL)
734			return;
735	}
736
737	eh = mtod(m, struct ether_header *);
738	ether_type = ntohs(eh->ether_type);
739
740	/*
741	 * If this frame has a VLAN tag other than 0, call vlan_input()
742	 * if its module is loaded. Otherwise, drop.
743	 */
744	if ((m->m_flags & M_VLANTAG) &&
745	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
746		if (ifp->if_vlantrunk == NULL) {
747			ifp->if_noproto++;
748			m_freem(m);
749			return;
750		}
751		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
752		    __func__));
753		/* Clear before possibly re-entering ether_input(). */
754		m->m_flags &= ~M_PROMISC;
755		(*vlan_input_p)(ifp, m);
756		return;
757	}
758
759	/*
760	 * Pass promiscuously received frames to the upper layer if the user
761	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
762	 */
763	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
764		m_freem(m);
765		return;
766	}
767
768	/*
769	 * Reset layer specific mbuf flags to avoid confusing upper layers.
770	 * Strip off Ethernet header.
771	 */
772	m->m_flags &= ~M_VLANTAG;
773	m_clrprotoflags(m);
774	m_adj(m, ETHER_HDR_LEN);
775
776	/*
777	 * Dispatch frame to upper layer.
778	 */
779	switch (ether_type) {
780#ifdef INET
781	case ETHERTYPE_IP:
782		if ((m = ip_fastforward(m)) == NULL)
783			return;
784		isr = NETISR_IP;
785		break;
786
787	case ETHERTYPE_ARP:
788		if (ifp->if_flags & IFF_NOARP) {
789			/* Discard packet if ARP is disabled on interface */
790			m_freem(m);
791			return;
792		}
793		isr = NETISR_ARP;
794		break;
795#endif
796#ifdef INET6
797	case ETHERTYPE_IPV6:
798		isr = NETISR_IPV6;
799		break;
800#endif
801#ifdef NETATALK
802	case ETHERTYPE_AT:
803		isr = NETISR_ATALK1;
804		break;
805	case ETHERTYPE_AARP:
806		isr = NETISR_AARP;
807		break;
808#endif /* NETATALK */
809	default:
810#if defined(NETATALK)
811		if (ether_type > ETHERMTU)
812			goto discard;
813		l = mtod(m, struct llc *);
814		if (l->llc_dsap == LLC_SNAP_LSAP &&
815		    l->llc_ssap == LLC_SNAP_LSAP &&
816		    l->llc_control == LLC_UI) {
817			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
818			    sizeof(at_org_code)) == 0 &&
819			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
820				m_adj(m, LLC_SNAPFRAMELEN);
821				isr = NETISR_ATALK2;
822				break;
823			}
824			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
825			    sizeof(aarp_org_code)) == 0 &&
826			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
827				m_adj(m, LLC_SNAPFRAMELEN);
828				isr = NETISR_AARP;
829				break;
830			}
831		}
832#endif /* NETATALK */
833		goto discard;
834	}
835	netisr_dispatch(isr, m);
836	return;
837
838discard:
839	/*
840	 * Packet is to be discarded.  If netgraph is present,
841	 * hand the packet to it for last chance processing;
842	 * otherwise dispose of it.
843	 */
844	if (IFP2AC(ifp)->ac_netgraph != NULL) {
845		KASSERT(ng_ether_input_orphan_p != NULL,
846		    ("ng_ether_input_orphan_p is NULL"));
847		/*
848		 * Put back the ethernet header so netgraph has a
849		 * consistent view of inbound packets.
850		 */
851		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
852		(*ng_ether_input_orphan_p)(ifp, m);
853		return;
854	}
855	m_freem(m);
856}
857
858/*
859 * Convert Ethernet address to printable (loggable) representation.
860 * This routine is for compatibility; it's better to just use
861 *
862 *	printf("%6D", <pointer to address>, ":");
863 *
864 * since there's no static buffer involved.
865 */
866char *
867ether_sprintf(const u_char *ap)
868{
869	static char etherbuf[18];
870	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
871	return (etherbuf);
872}
873
874/*
875 * Perform common duties while attaching to interface list
876 */
877void
878ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
879{
880	int i;
881	struct ifaddr *ifa;
882	struct sockaddr_dl *sdl;
883
884	ifp->if_addrlen = ETHER_ADDR_LEN;
885	ifp->if_hdrlen = ETHER_HDR_LEN;
886	if_attach(ifp);
887	ifp->if_mtu = ETHERMTU;
888	ifp->if_output = ether_output;
889	ifp->if_input = ether_input;
890	ifp->if_resolvemulti = ether_resolvemulti;
891#ifdef VIMAGE
892	ifp->if_reassign = ether_reassign;
893#endif
894	if (ifp->if_baudrate == 0)
895		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
896	ifp->if_broadcastaddr = etherbroadcastaddr;
897
898	ifa = ifp->if_addr;
899	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
900	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
901	sdl->sdl_type = IFT_ETHER;
902	sdl->sdl_alen = ifp->if_addrlen;
903	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
904
905	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
906	if (ng_ether_attach_p != NULL)
907		(*ng_ether_attach_p)(ifp);
908
909	/* Announce Ethernet MAC address if non-zero. */
910	for (i = 0; i < ifp->if_addrlen; i++)
911		if (lla[i] != 0)
912			break;
913	if (i != ifp->if_addrlen)
914		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
915
916	uuid_ether_add(LLADDR(sdl));
917}
918
919/*
920 * Perform common duties while detaching an Ethernet interface
921 */
922void
923ether_ifdetach(struct ifnet *ifp)
924{
925	struct sockaddr_dl *sdl;
926
927	sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
928	uuid_ether_del(LLADDR(sdl));
929
930	if (IFP2AC(ifp)->ac_netgraph != NULL) {
931		KASSERT(ng_ether_detach_p != NULL,
932		    ("ng_ether_detach_p is NULL"));
933		(*ng_ether_detach_p)(ifp);
934	}
935
936	bpfdetach(ifp);
937	if_detach(ifp);
938}
939
940#ifdef VIMAGE
941void
942ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
943{
944
945	if (IFP2AC(ifp)->ac_netgraph != NULL) {
946		KASSERT(ng_ether_detach_p != NULL,
947		    ("ng_ether_detach_p is NULL"));
948		(*ng_ether_detach_p)(ifp);
949	}
950
951	if (ng_ether_attach_p != NULL) {
952		CURVNET_SET_QUIET(new_vnet);
953		(*ng_ether_attach_p)(ifp);
954		CURVNET_RESTORE();
955	}
956}
957#endif
958
959SYSCTL_DECL(_net_link);
960SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
961
962#if 0
963/*
964 * This is for reference.  We have a table-driven version
965 * of the little-endian crc32 generator, which is faster
966 * than the double-loop.
967 */
968uint32_t
969ether_crc32_le(const uint8_t *buf, size_t len)
970{
971	size_t i;
972	uint32_t crc;
973	int bit;
974	uint8_t data;
975
976	crc = 0xffffffff;	/* initial value */
977
978	for (i = 0; i < len; i++) {
979		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
980			carry = (crc ^ data) & 1;
981			crc >>= 1;
982			if (carry)
983				crc = (crc ^ ETHER_CRC_POLY_LE);
984		}
985	}
986
987	return (crc);
988}
989#else
990uint32_t
991ether_crc32_le(const uint8_t *buf, size_t len)
992{
993	static const uint32_t crctab[] = {
994		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
995		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
996		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
997		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
998	};
999	size_t i;
1000	uint32_t crc;
1001
1002	crc = 0xffffffff;	/* initial value */
1003
1004	for (i = 0; i < len; i++) {
1005		crc ^= buf[i];
1006		crc = (crc >> 4) ^ crctab[crc & 0xf];
1007		crc = (crc >> 4) ^ crctab[crc & 0xf];
1008	}
1009
1010	return (crc);
1011}
1012#endif
1013
1014uint32_t
1015ether_crc32_be(const uint8_t *buf, size_t len)
1016{
1017	size_t i;
1018	uint32_t crc, carry;
1019	int bit;
1020	uint8_t data;
1021
1022	crc = 0xffffffff;	/* initial value */
1023
1024	for (i = 0; i < len; i++) {
1025		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1026			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1027			crc <<= 1;
1028			if (carry)
1029				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1030		}
1031	}
1032
1033	return (crc);
1034}
1035
1036int
1037ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1038{
1039	struct ifaddr *ifa = (struct ifaddr *) data;
1040	struct ifreq *ifr = (struct ifreq *) data;
1041	int error = 0;
1042
1043	switch (command) {
1044	case SIOCSIFADDR:
1045		ifp->if_flags |= IFF_UP;
1046
1047		switch (ifa->ifa_addr->sa_family) {
1048#ifdef INET
1049		case AF_INET:
1050			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1051			arp_ifinit(ifp, ifa);
1052			break;
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(IF_LLADDR(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		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1118		sdl->sdl_alen = ETHER_ADDR_LEN;
1119		e_addr = LLADDR(sdl);
1120		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1121		*llsa = (struct sockaddr *)sdl;
1122		return 0;
1123#endif
1124#ifdef INET6
1125	case AF_INET6:
1126		sin6 = (struct sockaddr_in6 *)sa;
1127		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1128			/*
1129			 * An IP6 address of 0 means listen to all
1130			 * of the Ethernet multicast address used for IP6.
1131			 * (This is used for multicast routers.)
1132			 */
1133			ifp->if_flags |= IFF_ALLMULTI;
1134			*llsa = 0;
1135			return 0;
1136		}
1137		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1138			return EADDRNOTAVAIL;
1139		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1140		sdl->sdl_alen = ETHER_ADDR_LEN;
1141		e_addr = LLADDR(sdl);
1142		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1143		*llsa = (struct sockaddr *)sdl;
1144		return 0;
1145#endif
1146
1147	default:
1148		/*
1149		 * Well, the text isn't quite right, but it's the name
1150		 * that counts...
1151		 */
1152		return EAFNOSUPPORT;
1153	}
1154}
1155
1156static void*
1157ether_alloc(u_char type, struct ifnet *ifp)
1158{
1159	struct arpcom	*ac;
1160
1161	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1162	ac->ac_ifp = ifp;
1163
1164	return (ac);
1165}
1166
1167static void
1168ether_free(void *com, u_char type)
1169{
1170
1171	free(com, M_ARPCOM);
1172}
1173
1174static int
1175ether_modevent(module_t mod, int type, void *data)
1176{
1177
1178	switch (type) {
1179	case MOD_LOAD:
1180		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1181		break;
1182	case MOD_UNLOAD:
1183		if_deregister_com_alloc(IFT_ETHER);
1184		break;
1185	default:
1186		return EOPNOTSUPP;
1187	}
1188
1189	return (0);
1190}
1191
1192static moduledata_t ether_mod = {
1193	"ether",
1194	ether_modevent,
1195	0
1196};
1197
1198void
1199ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1200{
1201	struct ether_vlan_header vlan;
1202	struct mbuf mv, mb;
1203
1204	KASSERT((m->m_flags & M_VLANTAG) != 0,
1205	    ("%s: vlan information not present", __func__));
1206	KASSERT(m->m_len >= sizeof(struct ether_header),
1207	    ("%s: mbuf not large enough for header", __func__));
1208	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1209	vlan.evl_proto = vlan.evl_encap_proto;
1210	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1211	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1212	m->m_len -= sizeof(struct ether_header);
1213	m->m_data += sizeof(struct ether_header);
1214	/*
1215	 * If a data link has been supplied by the caller, then we will need to
1216	 * re-create a stack allocated mbuf chain with the following structure:
1217	 *
1218	 * (1) mbuf #1 will contain the supplied data link
1219	 * (2) mbuf #2 will contain the vlan header
1220	 * (3) mbuf #3 will contain the original mbuf's packet data
1221	 *
1222	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1223	 */
1224	if (data != NULL) {
1225		mv.m_next = m;
1226		mv.m_data = (caddr_t)&vlan;
1227		mv.m_len = sizeof(vlan);
1228		mb.m_next = &mv;
1229		mb.m_data = data;
1230		mb.m_len = dlen;
1231		bpf_mtap(bp, &mb);
1232	} else
1233		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1234	m->m_len += sizeof(struct ether_header);
1235	m->m_data -= sizeof(struct ether_header);
1236}
1237
1238struct mbuf *
1239ether_vlanencap(struct mbuf *m, uint16_t tag)
1240{
1241	struct ether_vlan_header *evl;
1242
1243	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1244	if (m == NULL)
1245		return (NULL);
1246	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1247
1248	if (m->m_len < sizeof(*evl)) {
1249		m = m_pullup(m, sizeof(*evl));
1250		if (m == NULL)
1251			return (NULL);
1252	}
1253
1254	/*
1255	 * Transform the Ethernet header into an Ethernet header
1256	 * with 802.1Q encapsulation.
1257	 */
1258	evl = mtod(m, struct ether_vlan_header *);
1259	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1260	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1261	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1262	evl->evl_tag = htons(tag);
1263	return (m);
1264}
1265
1266DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1267MODULE_VERSION(ether, 1);
1268