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