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