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