xref: /dflybsd-src/sys/net/if_ethersubr.c (revision 212efd99d654b838bcfc3fc5965862b0a610f81e)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  * $DragonFly: src/sys/net/if_ethersubr.c,v 1.94 2008/10/04 11:24:37 sephe Exp $
36  */
37 
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_mpls.h"
43 #include "opt_netgraph.h"
44 #include "opt_carp.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
50 #include <sys/ktr.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/msgport.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/thread.h>
59 #include <sys/thread2.h>
60 
61 #include <net/if.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/if_llc.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/ifq_var.h>
68 #include <net/bpf.h>
69 #include <net/ethernet.h>
70 #include <net/vlan/if_vlan_ether.h>
71 #include <net/netmsg2.h>
72 
73 #if defined(INET) || defined(INET6)
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/if_ether.h>
77 #include <net/ipfw/ip_fw.h>
78 #include <net/dummynet/ip_dummynet.h>
79 #endif
80 #ifdef INET6
81 #include <netinet6/nd6.h>
82 #endif
83 
84 #ifdef CARP
85 #include <netinet/ip_carp.h>
86 #endif
87 
88 #ifdef IPX
89 #include <netproto/ipx/ipx.h>
90 #include <netproto/ipx/ipx_if.h>
91 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
92 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
93 		  short *tp, int *hlen);
94 #endif
95 
96 #ifdef NS
97 #include <netns/ns.h>
98 #include <netns/ns_if.h>
99 ushort ns_nettype;
100 int ether_outputdebug = 0;
101 int ether_inputdebug = 0;
102 #endif
103 
104 #ifdef NETATALK
105 #include <netproto/atalk/at.h>
106 #include <netproto/atalk/at_var.h>
107 #include <netproto/atalk/at_extern.h>
108 
109 #define	llc_snap_org_code	llc_un.type_snap.org_code
110 #define	llc_snap_ether_type	llc_un.type_snap.ether_type
111 
112 extern u_char	at_org_code[3];
113 extern u_char	aarp_org_code[3];
114 #endif /* NETATALK */
115 
116 #ifdef MPLS
117 #include <netproto/mpls/mpls.h>
118 #endif
119 
120 /* netgraph node hooks for ng_ether(4) */
121 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
122 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
123 		struct mbuf *m, const struct ether_header *eh);
124 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
125 void	(*ng_ether_attach_p)(struct ifnet *ifp);
126 void	(*ng_ether_detach_p)(struct ifnet *ifp);
127 
128 void	(*vlan_input_p)(struct mbuf *);
129 
130 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
131 			struct rtentry *);
132 static void ether_restore_header(struct mbuf **, const struct ether_header *,
133 				 const struct ether_header *);
134 
135 /*
136  * if_bridge support
137  */
138 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
139 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
140 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
141 
142 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
143 			      struct sockaddr *);
144 
145 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
146 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
147 };
148 
149 #define gotoerr(e) do { error = (e); goto bad; } while (0)
150 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
151 
152 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
153 				struct ip_fw **rule,
154 				const struct ether_header *eh);
155 
156 static int ether_ipfw;
157 static u_int ether_restore_hdr;
158 static u_int ether_prepend_hdr;
159 
160 SYSCTL_DECL(_net_link);
161 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
162 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
163 	   &ether_ipfw, 0, "Pass ether pkts through firewall");
164 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
165 	    &ether_restore_hdr, 0, "# of ether header restoration");
166 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
167 	    &ether_prepend_hdr, 0,
168 	    "# of ether header restoration which prepends mbuf");
169 
170 #define ETHER_KTR_STR		"ifp=%p"
171 #define ETHER_KTR_ARG_SIZE	(sizeof(void *))
172 #ifndef KTR_ETHERNET
173 #define KTR_ETHERNET		KTR_ALL
174 #endif
175 KTR_INFO_MASTER(ether);
176 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
177 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
178 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
179 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
180 #define logether(name, arg)	KTR_LOG(ether_ ## name, arg)
181 
182 /*
183  * Ethernet output routine.
184  * Encapsulate a packet of type family for the local net.
185  * Use trailer local net encapsulation if enough data in first
186  * packet leaves a multiple of 512 bytes of data in remainder.
187  * Assumes that ifp is actually pointer to arpcom structure.
188  */
189 static int
190 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
191 	     struct rtentry *rt)
192 {
193 	struct ether_header *eh, *deh;
194 	u_char *edst;
195 	int loop_copy = 0;
196 	int hlen = ETHER_HDR_LEN;	/* link layer header length */
197 	struct arpcom *ac = IFP2AC(ifp);
198 	int error;
199 
200 	ASSERT_NOT_SERIALIZED(ifp->if_serializer);
201 
202 	if (ifp->if_flags & IFF_MONITOR)
203 		gotoerr(ENETDOWN);
204 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
205 		gotoerr(ENETDOWN);
206 
207 	M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
208 	if (m == NULL)
209 		return (ENOBUFS);
210 	eh = mtod(m, struct ether_header *);
211 	edst = eh->ether_dhost;
212 
213 	/*
214 	 * Fill in the destination ethernet address and frame type.
215 	 */
216 	switch (dst->sa_family) {
217 #ifdef INET
218 	case AF_INET:
219 		if (!arpresolve(ifp, rt, m, dst, edst))
220 			return (0);	/* if not yet resolved */
221 #ifdef MPLS
222 		if (m->m_flags & M_MPLSLABELED)
223 			eh->ether_type = htons(ETHERTYPE_MPLS);
224 		else
225 #endif
226 			eh->ether_type = htons(ETHERTYPE_IP);
227 		break;
228 #endif
229 #ifdef INET6
230 	case AF_INET6:
231 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
232 			return (0);		/* Something bad happenned. */
233 		eh->ether_type = htons(ETHERTYPE_IPV6);
234 		break;
235 #endif
236 #ifdef IPX
237 	case AF_IPX:
238 		if (ef_outputp != NULL) {
239 			/*
240 			 * Hold BGL and recheck ef_outputp
241 			 */
242 			get_mplock();
243 			if (ef_outputp != NULL) {
244 				error = ef_outputp(ifp, &m, dst,
245 						   &eh->ether_type, &hlen);
246 				rel_mplock();
247 				if (error)
248 					goto bad;
249 				else
250 					break;
251 			}
252 			rel_mplock();
253 		}
254 		eh->ether_type = htons(ETHERTYPE_IPX);
255 		bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
256 		      edst, ETHER_ADDR_LEN);
257 		break;
258 #endif
259 #ifdef NETATALK
260 	case AF_APPLETALK: {
261 		struct at_ifaddr *aa;
262 
263 		/*
264 		 * Hold BGL
265 		 */
266 		get_mplock();
267 
268 		if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
269 			error = 0;	/* XXX */
270 			rel_mplock();
271 			goto bad;
272 		}
273 		/*
274 		 * In the phase 2 case, need to prepend an mbuf for
275 		 * the llc header.  Since we must preserve the value
276 		 * of m, which is passed to us by value, we m_copy()
277 		 * the first mbuf, and use it for our llc header.
278 		 */
279 		if (aa->aa_flags & AFA_PHASE2) {
280 			struct llc llc;
281 
282 			M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
283 			eh = mtod(m, struct ether_header *);
284 			edst = eh->ether_dhost;
285 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
286 			llc.llc_control = LLC_UI;
287 			bcopy(at_org_code, llc.llc_snap_org_code,
288 			      sizeof at_org_code);
289 			llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
290 			bcopy(&llc,
291 			      mtod(m, caddr_t) + sizeof(struct ether_header),
292 			      sizeof(struct llc));
293 			eh->ether_type = htons(m->m_pkthdr.len);
294 			hlen = sizeof(struct llc) + ETHER_HDR_LEN;
295 		} else {
296 			eh->ether_type = htons(ETHERTYPE_AT);
297 		}
298 		if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
299 			rel_mplock();
300 			return (0);
301 		}
302 
303 		rel_mplock();
304 		break;
305 	  }
306 #endif
307 #ifdef NS
308 	case AF_NS:
309 		switch(ns_nettype) {
310 		default:
311 		case 0x8137:	/* Novell Ethernet_II Ethernet TYPE II */
312 			eh->ether_type = 0x8137;
313 			break;
314 		case 0x0:	/* Novell 802.3 */
315 			eh->ether_type = htons(m->m_pkthdr.len);
316 			break;
317 		case 0xe0e0:	/* Novell 802.2 and Token-Ring */
318 			M_PREPEND(m, 3, MB_DONTWAIT);
319 			eh = mtod(m, struct ether_header *);
320 			edst = eh->ether_dhost;
321 			eh->ether_type = htons(m->m_pkthdr.len);
322 			cp = mtod(m, u_char *) + sizeof(struct ether_header);
323 			*cp++ = 0xE0;
324 			*cp++ = 0xE0;
325 			*cp++ = 0x03;
326 			break;
327 		}
328 		bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
329 		      ETHER_ADDR_LEN);
330 		/*
331 		 * XXX if ns_thishost is the same as the node's ethernet
332 		 * address then just the default code will catch this anyhow.
333 		 * So I'm not sure if this next clause should be here at all?
334 		 * [JRE]
335 		 */
336 		if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
337 			m->m_pkthdr.rcvif = ifp;
338 			netisr_dispatch(NETISR_NS, m);
339 			return (error);
340 		}
341 		if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
342 			m->m_flags |= M_BCAST;
343 		break;
344 #endif
345 	case pseudo_AF_HDRCMPLT:
346 	case AF_UNSPEC:
347 		loop_copy = -1; /* if this is for us, don't do it */
348 		deh = (struct ether_header *)dst->sa_data;
349 		memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
350 		eh->ether_type = deh->ether_type;
351 		break;
352 
353 	default:
354 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
355 		gotoerr(EAFNOSUPPORT);
356 	}
357 
358 	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
359 		memcpy(eh->ether_shost,
360 		       ((struct ether_header *)dst->sa_data)->ether_shost,
361 		       ETHER_ADDR_LEN);
362 	else
363 		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
364 
365 	/*
366 	 * Bridges require special output handling.
367 	 */
368 	if (ifp->if_bridge) {
369 		KASSERT(bridge_output_p != NULL,
370 			("%s: if_bridge not loaded!", __func__));
371 		return bridge_output_p(ifp, m);
372 	}
373 
374 	/*
375 	 * If a simplex interface, and the packet is being sent to our
376 	 * Ethernet address or a broadcast address, loopback a copy.
377 	 * XXX To make a simplex device behave exactly like a duplex
378 	 * device, we should copy in the case of sending to our own
379 	 * ethernet address (thus letting the original actually appear
380 	 * on the wire). However, we don't do that here for security
381 	 * reasons and compatibility with the original behavior.
382 	 */
383 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
384 		int csum_flags = 0;
385 
386 		if (m->m_pkthdr.csum_flags & CSUM_IP)
387 			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
388 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
389 			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
390 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
391 			struct mbuf *n;
392 
393 			if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
394 				n->m_pkthdr.csum_flags |= csum_flags;
395 				if (csum_flags & CSUM_DATA_VALID)
396 					n->m_pkthdr.csum_data = 0xffff;
397 				if_simloop(ifp, n, dst->sa_family, hlen);
398 			} else
399 				ifp->if_iqdrops++;
400 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
401 				ETHER_ADDR_LEN) == 0) {
402 			m->m_pkthdr.csum_flags |= csum_flags;
403 			if (csum_flags & CSUM_DATA_VALID)
404 				m->m_pkthdr.csum_data = 0xffff;
405 			if_simloop(ifp, m, dst->sa_family, hlen);
406 			return (0);	/* XXX */
407 		}
408 	}
409 
410 #ifdef CARP
411 	if (ifp->if_carp) {
412 		/*
413 		 * Hold BGL and recheck ifp->if_carp
414 		 */
415 		get_mplock();
416 		if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
417 			rel_mplock();
418 			goto bad;
419 		}
420 		rel_mplock();
421 	}
422 #endif
423 
424 
425 	/* Handle ng_ether(4) processing, if any */
426 	if (ng_ether_output_p != NULL) {
427 		/*
428 		 * Hold BGL and recheck ng_ether_output_p
429 		 */
430 		get_mplock();
431 		if (ng_ether_output_p != NULL) {
432 			if ((error = ng_ether_output_p(ifp, &m)) != 0) {
433 				rel_mplock();
434 				goto bad;
435 			}
436 			if (m == NULL) {
437 				rel_mplock();
438 				return (0);
439 			}
440 		}
441 		rel_mplock();
442 	}
443 
444 	/* Continue with link-layer output */
445 	return ether_output_frame(ifp, m);
446 
447 bad:
448 	m_freem(m);
449 	return (error);
450 }
451 
452 /*
453  * Ethernet link layer output routine to send a raw frame to the device.
454  *
455  * This assumes that the 14 byte Ethernet header is present and contiguous
456  * in the first mbuf.
457  */
458 int
459 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
460 {
461 	struct ip_fw *rule = NULL;
462 	int error = 0;
463 	struct altq_pktattr pktattr;
464 
465 	ASSERT_NOT_SERIALIZED(ifp->if_serializer);
466 
467 	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
468 		struct m_tag *mtag;
469 
470 		/* Extract info from dummynet tag */
471 		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
472 		KKASSERT(mtag != NULL);
473 		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
474 		KKASSERT(rule != NULL);
475 
476 		m_tag_delete(m, mtag);
477 		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
478 	}
479 
480 	if (ifq_is_enabled(&ifp->if_snd))
481 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
482 	crit_enter();
483 	if (IPFW_LOADED && ether_ipfw != 0) {
484 		struct ether_header save_eh, *eh;
485 
486 		eh = mtod(m, struct ether_header *);
487 		save_eh = *eh;
488 		m_adj(m, ETHER_HDR_LEN);
489 		if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
490 			crit_exit();
491 			if (m != NULL) {
492 				m_freem(m);
493 				return ENOBUFS; /* pkt dropped */
494 			} else
495 				return 0;	/* consumed e.g. in a pipe */
496 		}
497 
498 		/* packet was ok, restore the ethernet header */
499 		ether_restore_header(&m, eh, &save_eh);
500 		if (m == NULL) {
501 			crit_exit();
502 			return ENOBUFS;
503 		}
504 	}
505 	crit_exit();
506 
507 	/*
508 	 * Queue message on interface, update output statistics if
509 	 * successful, and start output if interface not yet active.
510 	 */
511 	error = ifq_dispatch(ifp, m, &pktattr);
512 	return (error);
513 }
514 
515 /*
516  * ipfw processing for ethernet packets (in and out).
517  * The second parameter is NULL from ether_demux(), and ifp from
518  * ether_output_frame().
519  */
520 static boolean_t
521 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
522 	       const struct ether_header *eh)
523 {
524 	struct ether_header save_eh = *eh;	/* might be a ptr in *m0 */
525 	struct ip_fw_args args;
526 	struct m_tag *mtag;
527 	struct mbuf *m;
528 	int i;
529 
530 	if (*rule != NULL && fw_one_pass)
531 		return TRUE; /* dummynet packet, already partially processed */
532 
533 	/*
534 	 * I need some amount of data to be contiguous.
535 	 */
536 	i = min((*m0)->m_pkthdr.len, max_protohdr);
537 	if ((*m0)->m_len < i) {
538 		*m0 = m_pullup(*m0, i);
539 		if (*m0 == NULL)
540 			return FALSE;
541 	}
542 
543 	/*
544 	 * Clean up tags
545 	 */
546 	if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
547 		m_tag_delete(*m0, mtag);
548 	if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
549 		mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
550 		KKASSERT(mtag != NULL);
551 		m_tag_delete(*m0, mtag);
552 		(*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
553 	}
554 
555 	args.m = *m0;		/* the packet we are looking at		*/
556 	args.oif = dst;		/* destination, if any			*/
557 	args.rule = *rule;	/* matching rule to restart		*/
558 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
559 	i = ip_fw_chk_ptr(&args);
560 	*m0 = args.m;
561 	*rule = args.rule;
562 
563 	if (*m0 == NULL)
564 		return FALSE;
565 
566 	switch (i) {
567 	case IP_FW_PASS:
568 		return TRUE;
569 
570 	case IP_FW_DIVERT:
571 	case IP_FW_TEE:
572 	case IP_FW_DENY:
573 		/*
574 		 * XXX at some point add support for divert/forward actions.
575 		 * If none of the above matches, we have to drop the pkt.
576 		 */
577 		return FALSE;
578 
579 	case IP_FW_DUMMYNET:
580 		/*
581 		 * Pass the pkt to dummynet, which consumes it.
582 		 */
583 		m = *m0;	/* pass the original to dummynet */
584 		*m0 = NULL;	/* and nothing back to the caller */
585 
586 		ether_restore_header(&m, eh, &save_eh);
587 		if (m == NULL)
588 			return FALSE;
589 
590 		ip_fw_dn_io_ptr(m, args.cookie,
591 				dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
592 		ip_dn_queue(m);
593 		return FALSE;
594 
595 	default:
596 		panic("unknown ipfw return value: %d\n", i);
597 	}
598 }
599 
600 static void
601 ether_input(struct ifnet *ifp, struct mbuf *m)
602 {
603 	ether_input_chain(ifp, m, NULL);
604 }
605 
606 /*
607  * Perform common duties while attaching to interface list
608  */
609 void
610 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
611 {
612 	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
613 			   serializer);
614 }
615 
616 void
617 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
618 		   lwkt_serialize_t serializer)
619 {
620 	struct sockaddr_dl *sdl;
621 
622 	ifp->if_type = IFT_ETHER;
623 	ifp->if_addrlen = ETHER_ADDR_LEN;
624 	ifp->if_hdrlen = ETHER_HDR_LEN;
625 	if_attach(ifp, serializer);
626 	ifp->if_mtu = ETHERMTU;
627 	if (ifp->if_baudrate == 0)
628 		ifp->if_baudrate = 10000000;
629 	ifp->if_output = ether_output;
630 	ifp->if_input = ether_input;
631 	ifp->if_resolvemulti = ether_resolvemulti;
632 	ifp->if_broadcastaddr = etherbroadcastaddr;
633 	sdl = IF_LLSOCKADDR(ifp);
634 	sdl->sdl_type = IFT_ETHER;
635 	sdl->sdl_alen = ifp->if_addrlen;
636 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
637 	/*
638 	 * XXX Keep the current drivers happy.
639 	 * XXX Remove once all drivers have been cleaned up
640 	 */
641 	if (lla != IFP2AC(ifp)->ac_enaddr)
642 		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
643 	bpfattach(ifp, dlt, hdrlen);
644 	if (ng_ether_attach_p != NULL)
645 		(*ng_ether_attach_p)(ifp);
646 
647 	if_printf(ifp, "MAC address: %6D\n", lla, ":");
648 }
649 
650 /*
651  * Perform common duties while detaching an Ethernet interface
652  */
653 void
654 ether_ifdetach(struct ifnet *ifp)
655 {
656 	if_down(ifp);
657 
658 	if (ng_ether_detach_p != NULL)
659 		(*ng_ether_detach_p)(ifp);
660 	bpfdetach(ifp);
661 	if_detach(ifp);
662 }
663 
664 int
665 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
666 {
667 	struct ifaddr *ifa = (struct ifaddr *) data;
668 	struct ifreq *ifr = (struct ifreq *) data;
669 	int error = 0;
670 
671 #define IF_INIT(ifp) \
672 do { \
673 	if (((ifp)->if_flags & IFF_UP) == 0) { \
674 		(ifp)->if_flags |= IFF_UP; \
675 		(ifp)->if_init((ifp)->if_softc); \
676 	} \
677 } while (0)
678 
679 	ASSERT_SERIALIZED(ifp->if_serializer);
680 
681 	switch (command) {
682 	case SIOCSIFADDR:
683 		switch (ifa->ifa_addr->sa_family) {
684 #ifdef INET
685 		case AF_INET:
686 			IF_INIT(ifp);	/* before arpwhohas */
687 			arp_ifinit(ifp, ifa);
688 			break;
689 #endif
690 #ifdef IPX
691 		/*
692 		 * XXX - This code is probably wrong
693 		 */
694 		case AF_IPX:
695 			{
696 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
697 			struct arpcom *ac = IFP2AC(ifp);
698 
699 			if (ipx_nullhost(*ina))
700 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
701 			else
702 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
703 				      sizeof ac->ac_enaddr);
704 
705 			IF_INIT(ifp);	/* Set new address. */
706 			break;
707 			}
708 #endif
709 #ifdef NS
710 		/*
711 		 * XXX - This code is probably wrong
712 		 */
713 		case AF_NS:
714 		{
715 			struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
716 			struct arpcom *ac = IFP2AC(ifp);
717 
718 			if (ns_nullhost(*ina))
719 				ina->x_host = *(union ns_host *)(ac->ac_enaddr);
720 			else
721 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
722 				      sizeof ac->ac_enaddr);
723 
724 			/*
725 			 * Set new address
726 			 */
727 			IF_INIT(ifp);
728 			break;
729 		}
730 #endif
731 		default:
732 			IF_INIT(ifp);
733 			break;
734 		}
735 		break;
736 
737 	case SIOCGIFADDR:
738 		bcopy(IFP2AC(ifp)->ac_enaddr,
739 		      ((struct sockaddr *)ifr->ifr_data)->sa_data,
740 		      ETHER_ADDR_LEN);
741 		break;
742 
743 	case SIOCSIFMTU:
744 		/*
745 		 * Set the interface MTU.
746 		 */
747 		if (ifr->ifr_mtu > ETHERMTU) {
748 			error = EINVAL;
749 		} else {
750 			ifp->if_mtu = ifr->ifr_mtu;
751 		}
752 		break;
753 	default:
754 		error = EINVAL;
755 		break;
756 	}
757 	return (error);
758 
759 #undef IF_INIT
760 }
761 
762 int
763 ether_resolvemulti(
764 	struct ifnet *ifp,
765 	struct sockaddr **llsa,
766 	struct sockaddr *sa)
767 {
768 	struct sockaddr_dl *sdl;
769 	struct sockaddr_in *sin;
770 #ifdef INET6
771 	struct sockaddr_in6 *sin6;
772 #endif
773 	u_char *e_addr;
774 
775 	switch(sa->sa_family) {
776 	case AF_LINK:
777 		/*
778 		 * No mapping needed. Just check that it's a valid MC address.
779 		 */
780 		sdl = (struct sockaddr_dl *)sa;
781 		e_addr = LLADDR(sdl);
782 		if ((e_addr[0] & 1) != 1)
783 			return EADDRNOTAVAIL;
784 		*llsa = 0;
785 		return 0;
786 
787 #ifdef INET
788 	case AF_INET:
789 		sin = (struct sockaddr_in *)sa;
790 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
791 			return EADDRNOTAVAIL;
792 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
793 		       M_WAITOK | M_ZERO);
794 		sdl->sdl_len = sizeof *sdl;
795 		sdl->sdl_family = AF_LINK;
796 		sdl->sdl_index = ifp->if_index;
797 		sdl->sdl_type = IFT_ETHER;
798 		sdl->sdl_alen = ETHER_ADDR_LEN;
799 		e_addr = LLADDR(sdl);
800 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
801 		*llsa = (struct sockaddr *)sdl;
802 		return 0;
803 #endif
804 #ifdef INET6
805 	case AF_INET6:
806 		sin6 = (struct sockaddr_in6 *)sa;
807 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
808 			/*
809 			 * An IP6 address of 0 means listen to all
810 			 * of the Ethernet multicast address used for IP6.
811 			 * (This is used for multicast routers.)
812 			 */
813 			ifp->if_flags |= IFF_ALLMULTI;
814 			*llsa = 0;
815 			return 0;
816 		}
817 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
818 			return EADDRNOTAVAIL;
819 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
820 		       M_WAITOK | M_ZERO);
821 		sdl->sdl_len = sizeof *sdl;
822 		sdl->sdl_family = AF_LINK;
823 		sdl->sdl_index = ifp->if_index;
824 		sdl->sdl_type = IFT_ETHER;
825 		sdl->sdl_alen = ETHER_ADDR_LEN;
826 		e_addr = LLADDR(sdl);
827 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
828 		*llsa = (struct sockaddr *)sdl;
829 		return 0;
830 #endif
831 
832 	default:
833 		/*
834 		 * Well, the text isn't quite right, but it's the name
835 		 * that counts...
836 		 */
837 		return EAFNOSUPPORT;
838 	}
839 }
840 
841 #if 0
842 /*
843  * This is for reference.  We have a table-driven version
844  * of the little-endian crc32 generator, which is faster
845  * than the double-loop.
846  */
847 uint32_t
848 ether_crc32_le(const uint8_t *buf, size_t len)
849 {
850 	uint32_t c, crc, carry;
851 	size_t i, j;
852 
853 	crc = 0xffffffffU;	/* initial value */
854 
855 	for (i = 0; i < len; i++) {
856 		c = buf[i];
857 		for (j = 0; j < 8; j++) {
858 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
859 			crc >>= 1;
860 			c >>= 1;
861 			if (carry)
862 				crc = (crc ^ ETHER_CRC_POLY_LE);
863 		}
864 	}
865 
866 	return (crc);
867 }
868 #else
869 uint32_t
870 ether_crc32_le(const uint8_t *buf, size_t len)
871 {
872 	static const uint32_t crctab[] = {
873 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
874 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
875 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
876 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
877 	};
878 	uint32_t crc;
879 	size_t i;
880 
881 	crc = 0xffffffffU;	/* initial value */
882 
883 	for (i = 0; i < len; i++) {
884 		crc ^= buf[i];
885 		crc = (crc >> 4) ^ crctab[crc & 0xf];
886 		crc = (crc >> 4) ^ crctab[crc & 0xf];
887 	}
888 
889 	return (crc);
890 }
891 #endif
892 
893 uint32_t
894 ether_crc32_be(const uint8_t *buf, size_t len)
895 {
896 	uint32_t c, crc, carry;
897 	size_t i, j;
898 
899 	crc = 0xffffffffU;	/* initial value */
900 
901 	for (i = 0; i < len; i++) {
902 		c = buf[i];
903 		for (j = 0; j < 8; j++) {
904 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
905 			crc <<= 1;
906 			c >>= 1;
907 			if (carry)
908 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
909 		}
910 	}
911 
912 	return (crc);
913 }
914 
915 /*
916  * find the size of ethernet header, and call classifier
917  */
918 void
919 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
920 		   struct altq_pktattr *pktattr)
921 {
922 	struct ether_header *eh;
923 	uint16_t ether_type;
924 	int hlen, af, hdrsize;
925 	caddr_t hdr;
926 
927 	hlen = sizeof(struct ether_header);
928 	eh = mtod(m, struct ether_header *);
929 
930 	ether_type = ntohs(eh->ether_type);
931 	if (ether_type < ETHERMTU) {
932 		/* ick! LLC/SNAP */
933 		struct llc *llc = (struct llc *)(eh + 1);
934 		hlen += 8;
935 
936 		if (m->m_len < hlen ||
937 		    llc->llc_dsap != LLC_SNAP_LSAP ||
938 		    llc->llc_ssap != LLC_SNAP_LSAP ||
939 		    llc->llc_control != LLC_UI)
940 			goto bad;  /* not snap! */
941 
942 		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
943 	}
944 
945 	if (ether_type == ETHERTYPE_IP) {
946 		af = AF_INET;
947 		hdrsize = 20;  /* sizeof(struct ip) */
948 #ifdef INET6
949 	} else if (ether_type == ETHERTYPE_IPV6) {
950 		af = AF_INET6;
951 		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
952 #endif
953 	} else
954 		goto bad;
955 
956 	while (m->m_len <= hlen) {
957 		hlen -= m->m_len;
958 		m = m->m_next;
959 	}
960 	hdr = m->m_data + hlen;
961 	if (m->m_len < hlen + hdrsize) {
962 		/*
963 		 * ip header is not in a single mbuf.  this should not
964 		 * happen in the current code.
965 		 * (todo: use m_pulldown in the future)
966 		 */
967 		goto bad;
968 	}
969 	m->m_data += hlen;
970 	m->m_len -= hlen;
971 	ifq_classify(ifq, m, af, pktattr);
972 	m->m_data -= hlen;
973 	m->m_len += hlen;
974 
975 	return;
976 
977 bad:
978 	pktattr->pattr_class = NULL;
979 	pktattr->pattr_hdr = NULL;
980 	pktattr->pattr_af = AF_UNSPEC;
981 }
982 
983 static void
984 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
985 		     const struct ether_header *save_eh)
986 {
987 	struct mbuf *m = *m0;
988 
989 	ether_restore_hdr++;
990 
991 	/*
992 	 * Prepend the header, optimize for the common case of
993 	 * eh pointing into the mbuf.
994 	 */
995 	if ((const void *)(eh + 1) == (void *)m->m_data) {
996 		m->m_data -= ETHER_HDR_LEN;
997 		m->m_len += ETHER_HDR_LEN;
998 		m->m_pkthdr.len += ETHER_HDR_LEN;
999 	} else {
1000 		ether_prepend_hdr++;
1001 
1002 		M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1003 		if (m != NULL) {
1004 			bcopy(save_eh, mtod(m, struct ether_header *),
1005 			      ETHER_HDR_LEN);
1006 		}
1007 	}
1008 	*m0 = m;
1009 }
1010 
1011 static void
1012 ether_input_ipifunc(void *arg)
1013 {
1014 	struct mbuf *m, *next;
1015 	lwkt_port_t port;
1016 
1017 	m = arg;
1018 	do {
1019 		next = m->m_nextpkt;
1020 		m->m_nextpkt = NULL;
1021 
1022 		port = m->m_pkthdr.header;
1023 		m->m_pkthdr.header = NULL;
1024 
1025 		lwkt_sendmsg(port,
1026 		&m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1027 
1028 		m = next;
1029 	} while (m != NULL);
1030 }
1031 
1032 void
1033 ether_input_dispatch(struct mbuf_chain *chain)
1034 {
1035 #ifdef SMP
1036 	int i;
1037 
1038 	logether(disp_beg, NULL);
1039 	for (i = 0; i < ncpus; ++i) {
1040 		if (chain[i].mc_head != NULL) {
1041 			lwkt_send_ipiq(globaldata_find(i),
1042 			ether_input_ipifunc, chain[i].mc_head);
1043 		}
1044 	}
1045 #else
1046 	logether(disp_beg, NULL);
1047 	if (chain->mc_head != NULL)
1048 		ether_input_ipifunc(chain->mc_head);
1049 #endif
1050 	logether(disp_end, NULL);
1051 }
1052 
1053 void
1054 ether_input_chain_init(struct mbuf_chain *chain)
1055 {
1056 #ifdef SMP
1057 	int i;
1058 
1059 	for (i = 0; i < ncpus; ++i)
1060 		chain[i].mc_head = chain[i].mc_tail = NULL;
1061 #else
1062 	chain->mc_head = chain->mc_tail = NULL;
1063 #endif
1064 }
1065 
1066 /*
1067  * Upper layer processing for a received Ethernet packet.
1068  */
1069 void
1070 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1071 {
1072 	struct ether_header *eh;
1073 	int isr, redispatch;
1074 	u_short ether_type;
1075 	struct ip_fw *rule = NULL;
1076 #ifdef NETATALK
1077 	struct llc *l;
1078 #endif
1079 
1080 	M_ASSERTPKTHDR(m);
1081 	KASSERT(m->m_len >= ETHER_HDR_LEN,
1082 		("ether header is no contiguous!\n"));
1083 
1084 	eh = mtod(m, struct ether_header *);
1085 
1086 	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1087 		struct m_tag *mtag;
1088 
1089 		/* Extract info from dummynet tag */
1090 		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1091 		KKASSERT(mtag != NULL);
1092 		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1093 		KKASSERT(rule != NULL);
1094 
1095 		m_tag_delete(m, mtag);
1096 		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1097 
1098 		/* packet is passing the second time */
1099 		goto post_stats;
1100 	}
1101 
1102 #ifdef CARP
1103 	/*
1104 	 * XXX: Okay, we need to call carp_forus() and - if it is for
1105 	 * us jump over code that does the normal check
1106 	 * "ac_enaddr == ether_dhost". The check sequence is a bit
1107 	 * different from OpenBSD, so we jump over as few code as
1108 	 * possible, to catch _all_ sanity checks. This needs
1109 	 * evaluation, to see if the carp ether_dhost values break any
1110 	 * of these checks!
1111 	 */
1112 	if (ifp->if_carp) {
1113 		/*
1114 		 * Hold BGL and recheck ifp->if_carp
1115 		 */
1116 		get_mplock();
1117 		if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1118 			rel_mplock();
1119 			goto post_stats;
1120 		}
1121 		rel_mplock();
1122 	}
1123 #endif
1124 
1125 	/*
1126 	 * Discard packet if upper layers shouldn't see it because
1127 	 * it was unicast to a different Ethernet address.  If the
1128 	 * driver is working properly, then this situation can only
1129 	 * happen when the interface is in promiscuous mode.
1130 	 */
1131 	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1132 	    (eh->ether_dhost[0] & 1) == 0 &&
1133 	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1134 		m_freem(m);
1135 		return;
1136 	}
1137 
1138 post_stats:
1139 	if (IPFW_LOADED && ether_ipfw != 0) {
1140 		struct ether_header save_eh = *eh;
1141 
1142 		/* XXX old crufty stuff, needs to be removed */
1143 		m_adj(m, sizeof(struct ether_header));
1144 
1145 		if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1146 			m_freem(m);
1147 			return;
1148 		}
1149 
1150 		ether_restore_header(&m, eh, &save_eh);
1151 		if (m == NULL)
1152 			return;
1153 		eh = mtod(m, struct ether_header *);
1154 	}
1155 
1156 	ether_type = ntohs(eh->ether_type);
1157 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1158 
1159 	if (m->m_flags & M_VLANTAG) {
1160 		void (*vlan_input_func)(struct mbuf *);
1161 
1162 		vlan_input_func = vlan_input_p;
1163 		if (vlan_input_func != NULL) {
1164 			vlan_input_func(m);
1165 		} else {
1166 			m->m_pkthdr.rcvif->if_noproto++;
1167 			m_freem(m);
1168 		}
1169 		return;
1170 	}
1171 
1172 	m_adj(m, sizeof(struct ether_header));
1173 	redispatch = 0;
1174 
1175 	switch (ether_type) {
1176 #ifdef INET
1177 	case ETHERTYPE_IP:
1178 		if (ipflow_fastforward(m))
1179 			return;
1180 		isr = NETISR_IP;
1181 		break;
1182 
1183 	case ETHERTYPE_ARP:
1184 		if (ifp->if_flags & IFF_NOARP) {
1185 			/* Discard packet if ARP is disabled on interface */
1186 			m_freem(m);
1187 			return;
1188 		}
1189 		isr = NETISR_ARP;
1190 		break;
1191 #endif
1192 
1193 #ifdef INET6
1194 	case ETHERTYPE_IPV6:
1195 		isr = NETISR_IPV6;
1196 		break;
1197 #endif
1198 
1199 #ifdef IPX
1200 	case ETHERTYPE_IPX:
1201 		if (ef_inputp) {
1202 			/*
1203 			 * Hold BGL and recheck ef_inputp
1204 			 */
1205 			get_mplock();
1206 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1207 				rel_mplock();
1208 				return;
1209 			}
1210 			rel_mplock();
1211 		}
1212 		isr = NETISR_IPX;
1213 		break;
1214 #endif
1215 
1216 #ifdef NS
1217 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1218 		isr = NETISR_NS;
1219 		break;
1220 
1221 #endif
1222 
1223 #ifdef NETATALK
1224 	case ETHERTYPE_AT:
1225 		isr = NETISR_ATALK1;
1226 		break;
1227 	case ETHERTYPE_AARP:
1228 		isr = NETISR_AARP;
1229 		break;
1230 #endif
1231 
1232 #ifdef MPLS
1233 	case ETHERTYPE_MPLS:
1234 	case ETHERTYPE_MPLS_MCAST:
1235 		/* Should have been set by ether_input_chain(). */
1236 		KKASSERT(m->m_flags & M_MPLSLABELED);
1237 		isr = NETISR_MPLS;
1238 		break;
1239 #endif
1240 
1241 	default:
1242 		/*
1243 		 * The accurate msgport is not determined before
1244 		 * we reach here, so redo the dispatching
1245 		 */
1246 		redispatch = 1;
1247 #ifdef IPX
1248 		if (ef_inputp) {
1249 			/*
1250 			 * Hold BGL and recheck ef_inputp
1251 			 */
1252 			get_mplock();
1253 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1254 				rel_mplock();
1255 				return;
1256 			}
1257 			rel_mplock();
1258 		}
1259 #endif
1260 #ifdef NS
1261 		checksum = mtod(m, ushort *);
1262 		/* Novell 802.3 */
1263 		if ((ether_type <= ETHERMTU) &&
1264 		    ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1265 			if (*checksum == 0xE0E0) {
1266 				m->m_pkthdr.len -= 3;
1267 				m->m_len -= 3;
1268 				m->m_data += 3;
1269 			}
1270 			isr = NETISR_NS;
1271 			break;
1272 		}
1273 #endif
1274 #ifdef NETATALK
1275 		if (ether_type > ETHERMTU)
1276 			goto dropanyway;
1277 		l = mtod(m, struct llc *);
1278 		if (l->llc_dsap == LLC_SNAP_LSAP &&
1279 		    l->llc_ssap == LLC_SNAP_LSAP &&
1280 		    l->llc_control == LLC_UI) {
1281 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1282 				 sizeof at_org_code) == 0 &&
1283 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1284 				m_adj(m, sizeof(struct llc));
1285 				isr = NETISR_ATALK2;
1286 				break;
1287 			}
1288 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1289 				 sizeof aarp_org_code) == 0 &&
1290 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1291 				m_adj(m, sizeof(struct llc));
1292 				isr = NETISR_AARP;
1293 				break;
1294 			}
1295 		}
1296 dropanyway:
1297 #endif
1298 		if (ng_ether_input_orphan_p != NULL) {
1299 			/*
1300 			 * Hold BGL and recheck ng_ether_input_orphan_p
1301 			 */
1302 			get_mplock();
1303 			if (ng_ether_input_orphan_p != NULL) {
1304 				ng_ether_input_orphan_p(ifp, m, eh);
1305 				rel_mplock();
1306 				return;
1307 			}
1308 			rel_mplock();
1309 		}
1310 		m_freem(m);
1311 		return;
1312 	}
1313 
1314 	if (!redispatch)
1315 		netisr_run(isr, m);
1316 	else
1317 		netisr_dispatch(isr, m);
1318 }
1319 
1320 /*
1321  * First we perform any link layer operations, then continue to the
1322  * upper layers with ether_demux_oncpu().
1323  */
1324 void
1325 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1326 {
1327 	if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1328 		/*
1329 		 * Receiving interface's flags are changed, when this
1330 		 * packet is waiting for processing; discard it.
1331 		 */
1332 		m_freem(m);
1333 		return;
1334 	}
1335 
1336 	/*
1337 	 * Tap the packet off here for a bridge.  bridge_input()
1338 	 * will return NULL if it has consumed the packet, otherwise
1339 	 * it gets processed as normal.  Note that bridge_input()
1340 	 * will always return the original packet if we need to
1341 	 * process it locally.
1342 	 */
1343 	if (ifp->if_bridge) {
1344 		KASSERT(bridge_input_p != NULL,
1345 			("%s: if_bridge not loaded!", __func__));
1346 
1347 		if(m->m_flags & M_PROTO1) {
1348 			m->m_flags &= ~M_PROTO1;
1349 		} else {
1350 			/* clear M_PROMISC, in case the packets comes from a vlan */
1351 			/* m->m_flags &= ~M_PROMISC; */
1352 			m = bridge_input_p(ifp, m);
1353 			if (m == NULL)
1354 				return;
1355 
1356 			KASSERT(ifp == m->m_pkthdr.rcvif,
1357 				("bridge_input_p changed rcvif\n"));
1358 		}
1359 	}
1360 
1361 	/* Handle ng_ether(4) processing, if any */
1362 	if (ng_ether_input_p != NULL) {
1363 		/*
1364 		 * Hold BGL and recheck ng_ether_input_p
1365 		 */
1366 		get_mplock();
1367 		if (ng_ether_input_p != NULL)
1368 			ng_ether_input_p(ifp, &m);
1369 		rel_mplock();
1370 
1371 		if (m == NULL)
1372 			return;
1373 	}
1374 
1375 	/* Continue with upper layer processing */
1376 	ether_demux_oncpu(ifp, m);
1377 }
1378 
1379 static void
1380 ether_input_handler(struct netmsg *nmsg)
1381 {
1382 	struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1383 	struct ifnet *ifp;
1384 	struct mbuf *m;
1385 
1386 	m = nmp->nm_packet;
1387 	M_ASSERTPKTHDR(m);
1388 	ifp = m->m_pkthdr.rcvif;
1389 
1390 	ether_input_oncpu(ifp, m);
1391 }
1392 
1393 static __inline void
1394 ether_init_netpacket(int num, struct mbuf *m)
1395 {
1396 	struct netmsg_packet *pmsg;
1397 
1398 	pmsg = &m->m_hdr.mh_netmsg;
1399 	netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, MSGF_MPSAFE,
1400 		    ether_input_handler);
1401 	pmsg->nm_packet = m;
1402 	pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1403 }
1404 
1405 static __inline struct lwkt_port *
1406 ether_mport(int num, struct mbuf **m)
1407 {
1408 	if (num == NETISR_MAX) {
1409 		/*
1410 		 * All packets whose target msgports can't be
1411 		 * determined here are dispatched to netisr0,
1412 		 * where further dispatching may happen.
1413 		 */
1414 		return cpu_portfn(0);
1415 	}
1416 	return netisr_find_port(num, m);
1417 }
1418 
1419 /*
1420  * Process a received Ethernet packet.
1421  *
1422  * The ethernet header is assumed to be in the mbuf so the caller
1423  * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1424  * bytes in the first mbuf.
1425  *
1426  * We first try to find the target msgport for this ether frame, if
1427  * there is no target msgport for it, this ether frame is discarded,
1428  * else we do following processing according to whether 'chain' is
1429  * NULL or not:
1430  * - If 'chain' is NULL, this ether frame is sent to the target msgport
1431  *   immediately.  This situation happens when ether_input_chain is
1432  *   accessed through ifnet.if_input.
1433  * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1434  *   bucket indexed by the target msgport's cpuid and the target msgport
1435  *   is saved in mbuf's m_pkthdr.m_head.  Caller of ether_input_chain
1436  *   must initialize 'chain' by calling ether_input_chain_init().
1437  *   ether_input_dispatch must be called later to send ether frames
1438  *   queued on 'chain' to their target msgport.
1439  */
1440 void
1441 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1442 {
1443 	struct ether_header *eh, *save_eh, save_eh0;
1444 	struct lwkt_port *port;
1445 	uint16_t ether_type;
1446 	int isr;
1447 
1448 	ASSERT_SERIALIZED(ifp->if_serializer);
1449 	M_ASSERTPKTHDR(m);
1450 
1451 	/* Discard packet if interface is not up */
1452 	if (!(ifp->if_flags & IFF_UP)) {
1453 		m_freem(m);
1454 		return;
1455 	}
1456 
1457 	if (m->m_len < sizeof(struct ether_header)) {
1458 		/* XXX error in the caller. */
1459 		m_freem(m);
1460 		return;
1461 	}
1462 	eh = mtod(m, struct ether_header *);
1463 
1464 	m->m_pkthdr.rcvif = ifp;
1465 
1466 	logether(chain_beg, ifp);
1467 
1468 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1469 		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1470 			 ifp->if_addrlen) == 0)
1471 			m->m_flags |= M_BCAST;
1472 		else
1473 			m->m_flags |= M_MCAST;
1474 		ifp->if_imcasts++;
1475 	}
1476 
1477 	ETHER_BPF_MTAP(ifp, m);
1478 
1479 	ifp->if_ibytes += m->m_pkthdr.len;
1480 
1481 	if (ifp->if_flags & IFF_MONITOR) {
1482 		/*
1483 		 * Interface marked for monitoring; discard packet.
1484 		 */
1485 		m_freem(m);
1486 
1487 		logether(chain_end, ifp);
1488 		return;
1489 	}
1490 
1491 	if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1492 	    (m->m_flags & M_VLANTAG) == 0) {
1493 		/*
1494 		 * Extract vlan tag if hardware does not do it for us
1495 		 */
1496 		vlan_ether_decap(&m);
1497 		if (m == NULL)
1498 			return;
1499 		eh = mtod(m, struct ether_header *);
1500 	}
1501 	ether_type = ntohs(eh->ether_type);
1502 
1503 	if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1504 		/*
1505 		 * To prevent possible dangerous recursion,
1506 		 * we don't do vlan-in-vlan
1507 		 */
1508 		ifp->if_noproto++;
1509 		m_freem(m);
1510 		return;
1511 	}
1512 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1513 
1514 	/*
1515 	 * Map ether type to netisr id.
1516 	 */
1517 	switch (ether_type) {
1518 #ifdef INET
1519 	case ETHERTYPE_IP:
1520 		isr = NETISR_IP;
1521 		break;
1522 
1523 	case ETHERTYPE_ARP:
1524 		isr = NETISR_ARP;
1525 		break;
1526 #endif
1527 
1528 #ifdef INET6
1529 	case ETHERTYPE_IPV6:
1530 		isr = NETISR_IPV6;
1531 		break;
1532 #endif
1533 
1534 #ifdef IPX
1535 	case ETHERTYPE_IPX:
1536 		isr = NETISR_IPX;
1537 		break;
1538 #endif
1539 
1540 #ifdef NS
1541 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1542 		isr = NETISR_NS;
1543 		break;
1544 #endif
1545 
1546 #ifdef NETATALK
1547 	case ETHERTYPE_AT:
1548 		isr = NETISR_ATALK1;
1549 		break;
1550 	case ETHERTYPE_AARP:
1551 		isr = NETISR_AARP;
1552 		break;
1553 #endif
1554 
1555 #ifdef MPLS
1556 	case ETHERTYPE_MPLS:
1557 	case ETHERTYPE_MPLS_MCAST:
1558 		m->m_flags |= M_MPLSLABELED;
1559 		isr = NETISR_MPLS;
1560 		break;
1561 #endif
1562 
1563 	default:
1564 		/*
1565 		 * NETISR_MAX is an invalid value; it is chosen to let
1566 		 * ether_mport() know that we are not able to decide
1567 		 * this packet's msgport here.
1568 		 */
1569 		isr = NETISR_MAX;
1570 		break;
1571 	}
1572 
1573 	/*
1574 	 * If the packet is in contiguous memory, following
1575 	 * m_adj() could ensure that the hidden ether header
1576 	 * will not be destroyed, else we will have to save
1577 	 * the ether header for the later restoration.
1578 	 */
1579 	if (m->m_pkthdr.len != m->m_len) {
1580 		save_eh0 = *eh;
1581 		save_eh = &save_eh0;
1582 	} else {
1583 		save_eh = NULL;
1584 	}
1585 
1586 	/*
1587 	 * Temporarily remove ether header; ether_mport()
1588 	 * expects a packet without ether header.
1589 	 */
1590 	m_adj(m, sizeof(struct ether_header));
1591 
1592 	/*
1593 	 * Find the packet's target msgport.
1594 	 */
1595 	port = ether_mport(isr, &m);
1596 	if (port == NULL) {
1597 		KKASSERT(m == NULL);
1598 		return;
1599 	}
1600 
1601 	/*
1602 	 * Restore ether header.
1603 	 */
1604 	if (save_eh != NULL) {
1605 		ether_restore_header(&m, eh, save_eh);
1606 		if (m == NULL)
1607 			return;
1608 	} else {
1609 		m->m_data -= ETHER_HDR_LEN;
1610 		m->m_len += ETHER_HDR_LEN;
1611 		m->m_pkthdr.len += ETHER_HDR_LEN;
1612 	}
1613 
1614 	/*
1615 	 * Initialize mbuf's netmsg packet _after_ possible
1616 	 * ether header restoration, else the initialized
1617 	 * netmsg packet may be lost during ether header
1618 	 * restoration.
1619 	 */
1620 	ether_init_netpacket(isr, m);
1621 
1622 	if (chain != NULL) {
1623 		struct mbuf_chain *c;
1624 		int cpuid;
1625 
1626 		m->m_pkthdr.header = port; /* XXX */
1627 		cpuid = port->mpu_td->td_gd->gd_cpuid;
1628 
1629 		c = &chain[cpuid];
1630 		if (c->mc_head == NULL) {
1631 			c->mc_head = c->mc_tail = m;
1632 		} else {
1633 			c->mc_tail->m_nextpkt = m;
1634 			c->mc_tail = m;
1635 		}
1636 		m->m_nextpkt = NULL;
1637 	} else {
1638 		lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1639 	}
1640 	logether(chain_end, ifp);
1641 }
1642