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