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