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