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