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