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