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