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