xref: /dragonfly/sys/net/if_ethersubr.c (revision 678e8cc6)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  */
36 
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipx.h"
40 #include "opt_mpls.h"
41 #include "opt_netgraph.h"
42 #include "opt_carp.h"
43 #include "opt_rss.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/globaldata.h>
48 #include <sys/kernel.h>
49 #include <sys/ktr.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
58 
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
61 
62 #include <net/if.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
69 #include <net/bpf.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/netmsg2.h>
73 
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/ip_flow.h>
79 #include <net/ipfw/ip_fw.h>
80 #include <net/dummynet/ip_dummynet.h>
81 #endif
82 #ifdef INET6
83 #include <netinet6/nd6.h>
84 #endif
85 
86 #ifdef CARP
87 #include <netinet/ip_carp.h>
88 #endif
89 
90 #ifdef IPX
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
93 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
94 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
95 		  short *tp, int *hlen);
96 #endif
97 
98 #ifdef MPLS
99 #include <netproto/mpls/mpls.h>
100 #endif
101 
102 /* netgraph node hooks for ng_ether(4) */
103 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
104 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
105 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
106 void	(*ng_ether_attach_p)(struct ifnet *ifp);
107 void	(*ng_ether_detach_p)(struct ifnet *ifp);
108 
109 void	(*vlan_input_p)(struct mbuf *);
110 
111 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
112 			struct rtentry *);
113 static void ether_restore_header(struct mbuf **, const struct ether_header *,
114 				 const struct ether_header *);
115 static int ether_characterize(struct mbuf **);
116 
117 /*
118  * if_bridge support
119  */
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
122 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
123 struct ifnet *(*bridge_interface_p)(void *if_bridge);
124 
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
126 			      struct sockaddr *);
127 
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
130 };
131 
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
134 
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 				struct ip_fw **rule,
137 				const struct ether_header *eh);
138 
139 static int ether_ipfw;
140 static u_long ether_restore_hdr;
141 static u_long ether_prepend_hdr;
142 static u_long ether_input_wronghash;
143 static int ether_debug;
144 
145 #ifdef RSS_DEBUG
146 static u_long ether_pktinfo_try;
147 static u_long ether_pktinfo_hit;
148 static u_long ether_rss_nopi;
149 static u_long ether_rss_nohash;
150 static u_long ether_input_requeue;
151 #endif
152 
153 SYSCTL_DECL(_net_link);
154 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
155 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
156     &ether_debug, 0, "Ether debug");
157 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
158     &ether_ipfw, 0, "Pass ether pkts through firewall");
159 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
160     &ether_restore_hdr, 0, "# of ether header restoration");
161 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
162     &ether_prepend_hdr, 0,
163     "# of ether header restoration which prepends mbuf");
164 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
165     &ether_input_wronghash, 0, "# of input packets with wrong hash");
166 #ifdef RSS_DEBUG
167 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
168     &ether_rss_nopi, 0, "# of packets do not have pktinfo");
169 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
170     &ether_rss_nohash, 0, "# of packets do not have hash");
171 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
172     &ether_pktinfo_try, 0,
173     "# of tries to find packets' msgport using pktinfo");
174 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
175     &ether_pktinfo_hit, 0,
176     "# of packets whose msgport are found using pktinfo");
177 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
178     &ether_input_requeue, 0, "# of input packets gets requeued");
179 #endif
180 
181 #define ETHER_KTR_STR		"ifp=%p"
182 #define ETHER_KTR_ARGS	struct ifnet *ifp
183 #ifndef KTR_ETHERNET
184 #define KTR_ETHERNET		KTR_ALL
185 #endif
186 KTR_INFO_MASTER(ether);
187 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
188 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
189 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
190 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
191 #define logether(name, arg)	KTR_LOG(ether_ ## name, arg)
192 
193 /*
194  * Ethernet output routine.
195  * Encapsulate a packet of type family for the local net.
196  * Use trailer local net encapsulation if enough data in first
197  * packet leaves a multiple of 512 bytes of data in remainder.
198  * Assumes that ifp is actually pointer to arpcom structure.
199  */
200 static int
201 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
202 	     struct rtentry *rt)
203 {
204 	struct ether_header *eh, *deh;
205 	u_char *edst;
206 	int loop_copy = 0;
207 	int hlen = ETHER_HDR_LEN;	/* link layer header length */
208 	struct arpcom *ac = IFP2AC(ifp);
209 	int error;
210 
211 	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
212 
213 	if (ifp->if_flags & IFF_MONITOR)
214 		gotoerr(ENETDOWN);
215 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
216 		gotoerr(ENETDOWN);
217 
218 	M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
219 	if (m == NULL)
220 		return (ENOBUFS);
221 	eh = mtod(m, struct ether_header *);
222 	edst = eh->ether_dhost;
223 
224 	/*
225 	 * Fill in the destination ethernet address and frame type.
226 	 */
227 	switch (dst->sa_family) {
228 #ifdef INET
229 	case AF_INET:
230 		if (!arpresolve(ifp, rt, m, dst, edst))
231 			return (0);	/* if not yet resolved */
232 #ifdef MPLS
233 		if (m->m_flags & M_MPLSLABELED)
234 			eh->ether_type = htons(ETHERTYPE_MPLS);
235 		else
236 #endif
237 			eh->ether_type = htons(ETHERTYPE_IP);
238 		break;
239 #endif
240 #ifdef INET6
241 	case AF_INET6:
242 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
243 			return (0);		/* Something bad happenned. */
244 		eh->ether_type = htons(ETHERTYPE_IPV6);
245 		break;
246 #endif
247 #ifdef IPX
248 	case AF_IPX:
249 		if (ef_outputp != NULL) {
250 			/*
251 			 * Hold BGL and recheck ef_outputp
252 			 */
253 			get_mplock();
254 			if (ef_outputp != NULL) {
255 				error = ef_outputp(ifp, &m, dst,
256 						   &eh->ether_type, &hlen);
257 				rel_mplock();
258 				if (error)
259 					goto bad;
260 				else
261 					break;
262 			}
263 			rel_mplock();
264 		}
265 		eh->ether_type = htons(ETHERTYPE_IPX);
266 		bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
267 		      edst, ETHER_ADDR_LEN);
268 		break;
269 #endif
270 	case pseudo_AF_HDRCMPLT:
271 	case AF_UNSPEC:
272 		loop_copy = -1; /* if this is for us, don't do it */
273 		deh = (struct ether_header *)dst->sa_data;
274 		memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
275 		eh->ether_type = deh->ether_type;
276 		break;
277 
278 	default:
279 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
280 		gotoerr(EAFNOSUPPORT);
281 	}
282 
283 	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
284 		memcpy(eh->ether_shost,
285 		       ((struct ether_header *)dst->sa_data)->ether_shost,
286 		       ETHER_ADDR_LEN);
287 	else
288 		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
289 
290 	/*
291 	 * Bridges require special output handling.
292 	 */
293 	if (ifp->if_bridge) {
294 		KASSERT(bridge_output_p != NULL,
295 			("%s: if_bridge not loaded!", __func__));
296 		return bridge_output_p(ifp, m);
297 	}
298 
299 	/*
300 	 * If a simplex interface, and the packet is being sent to our
301 	 * Ethernet address or a broadcast address, loopback a copy.
302 	 * XXX To make a simplex device behave exactly like a duplex
303 	 * device, we should copy in the case of sending to our own
304 	 * ethernet address (thus letting the original actually appear
305 	 * on the wire). However, we don't do that here for security
306 	 * reasons and compatibility with the original behavior.
307 	 */
308 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
309 		int csum_flags = 0;
310 
311 		if (m->m_pkthdr.csum_flags & CSUM_IP)
312 			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
313 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
314 			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
315 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
316 			struct mbuf *n;
317 
318 			if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
319 				n->m_pkthdr.csum_flags |= csum_flags;
320 				if (csum_flags & CSUM_DATA_VALID)
321 					n->m_pkthdr.csum_data = 0xffff;
322 				if_simloop(ifp, n, dst->sa_family, hlen);
323 			} else
324 				ifp->if_iqdrops++;
325 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
326 				ETHER_ADDR_LEN) == 0) {
327 			m->m_pkthdr.csum_flags |= csum_flags;
328 			if (csum_flags & CSUM_DATA_VALID)
329 				m->m_pkthdr.csum_data = 0xffff;
330 			if_simloop(ifp, m, dst->sa_family, hlen);
331 			return (0);	/* XXX */
332 		}
333 	}
334 
335 #ifdef CARP
336 	if (ifp->if_type == IFT_CARP) {
337 		ifp = carp_parent(ifp);
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 && 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\n", i);
539 	}
540 }
541 
542 static void
543 ether_input(struct ifnet *ifp, struct mbuf *m)
544 {
545 	ether_input_chain(ifp, m, NULL, NULL);
546 }
547 
548 /*
549  * Perform common duties while attaching to interface list
550  */
551 void
552 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
553 {
554 	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
555 			   serializer);
556 }
557 
558 void
559 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
560 		   lwkt_serialize_t serializer)
561 {
562 	struct sockaddr_dl *sdl;
563 
564 	ifp->if_type = IFT_ETHER;
565 	ifp->if_addrlen = ETHER_ADDR_LEN;
566 	ifp->if_hdrlen = ETHER_HDR_LEN;
567 	if_attach(ifp, serializer);
568 	ifp->if_mtu = ETHERMTU;
569 	if (ifp->if_baudrate == 0)
570 		ifp->if_baudrate = 10000000;
571 	ifp->if_output = ether_output;
572 	ifp->if_input = ether_input;
573 	ifp->if_resolvemulti = ether_resolvemulti;
574 	ifp->if_broadcastaddr = etherbroadcastaddr;
575 	sdl = IF_LLSOCKADDR(ifp);
576 	sdl->sdl_type = IFT_ETHER;
577 	sdl->sdl_alen = ifp->if_addrlen;
578 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
579 	/*
580 	 * XXX Keep the current drivers happy.
581 	 * XXX Remove once all drivers have been cleaned up
582 	 */
583 	if (lla != IFP2AC(ifp)->ac_enaddr)
584 		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
585 	bpfattach(ifp, dlt, hdrlen);
586 	if (ng_ether_attach_p != NULL)
587 		(*ng_ether_attach_p)(ifp);
588 
589 	if_printf(ifp, "MAC address: %6D\n", lla, ":");
590 }
591 
592 /*
593  * Perform common duties while detaching an Ethernet interface
594  */
595 void
596 ether_ifdetach(struct ifnet *ifp)
597 {
598 	if_down(ifp);
599 
600 	if (ng_ether_detach_p != NULL)
601 		(*ng_ether_detach_p)(ifp);
602 	bpfdetach(ifp);
603 	if_detach(ifp);
604 }
605 
606 int
607 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
608 {
609 	struct ifaddr *ifa = (struct ifaddr *) data;
610 	struct ifreq *ifr = (struct ifreq *) data;
611 	int error = 0;
612 
613 #define IF_INIT(ifp) \
614 do { \
615 	if (((ifp)->if_flags & IFF_UP) == 0) { \
616 		(ifp)->if_flags |= IFF_UP; \
617 		(ifp)->if_init((ifp)->if_softc); \
618 	} \
619 } while (0)
620 
621 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
622 
623 	switch (command) {
624 	case SIOCSIFADDR:
625 		switch (ifa->ifa_addr->sa_family) {
626 #ifdef INET
627 		case AF_INET:
628 			IF_INIT(ifp);	/* before arpwhohas */
629 			arp_ifinit(ifp, ifa);
630 			break;
631 #endif
632 #ifdef IPX
633 		/*
634 		 * XXX - This code is probably wrong
635 		 */
636 		case AF_IPX:
637 			{
638 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
639 			struct arpcom *ac = IFP2AC(ifp);
640 
641 			if (ipx_nullhost(*ina))
642 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
643 			else
644 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
645 				      sizeof ac->ac_enaddr);
646 
647 			IF_INIT(ifp);	/* Set new address. */
648 			break;
649 			}
650 #endif
651 		default:
652 			IF_INIT(ifp);
653 			break;
654 		}
655 		break;
656 
657 	case SIOCGIFADDR:
658 		bcopy(IFP2AC(ifp)->ac_enaddr,
659 		      ((struct sockaddr *)ifr->ifr_data)->sa_data,
660 		      ETHER_ADDR_LEN);
661 		break;
662 
663 	case SIOCSIFMTU:
664 		/*
665 		 * Set the interface MTU.
666 		 */
667 		if (ifr->ifr_mtu > ETHERMTU) {
668 			error = EINVAL;
669 		} else {
670 			ifp->if_mtu = ifr->ifr_mtu;
671 		}
672 		break;
673 	default:
674 		error = EINVAL;
675 		break;
676 	}
677 	return (error);
678 
679 #undef IF_INIT
680 }
681 
682 int
683 ether_resolvemulti(
684 	struct ifnet *ifp,
685 	struct sockaddr **llsa,
686 	struct sockaddr *sa)
687 {
688 	struct sockaddr_dl *sdl;
689 #ifdef INET
690 	struct sockaddr_in *sin;
691 #endif
692 #ifdef INET6
693 	struct sockaddr_in6 *sin6;
694 #endif
695 	u_char *e_addr;
696 
697 	switch(sa->sa_family) {
698 	case AF_LINK:
699 		/*
700 		 * No mapping needed. Just check that it's a valid MC address.
701 		 */
702 		sdl = (struct sockaddr_dl *)sa;
703 		e_addr = LLADDR(sdl);
704 		if ((e_addr[0] & 1) != 1)
705 			return EADDRNOTAVAIL;
706 		*llsa = NULL;
707 		return 0;
708 
709 #ifdef INET
710 	case AF_INET:
711 		sin = (struct sockaddr_in *)sa;
712 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
713 			return EADDRNOTAVAIL;
714 		sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
715 		sdl->sdl_len = sizeof *sdl;
716 		sdl->sdl_family = AF_LINK;
717 		sdl->sdl_index = ifp->if_index;
718 		sdl->sdl_type = IFT_ETHER;
719 		sdl->sdl_alen = ETHER_ADDR_LEN;
720 		e_addr = LLADDR(sdl);
721 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
722 		*llsa = (struct sockaddr *)sdl;
723 		return 0;
724 #endif
725 #ifdef INET6
726 	case AF_INET6:
727 		sin6 = (struct sockaddr_in6 *)sa;
728 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
729 			/*
730 			 * An IP6 address of 0 means listen to all
731 			 * of the Ethernet multicast address used for IP6.
732 			 * (This is used for multicast routers.)
733 			 */
734 			ifp->if_flags |= IFF_ALLMULTI;
735 			*llsa = NULL;
736 			return 0;
737 		}
738 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
739 			return EADDRNOTAVAIL;
740 		sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
741 		sdl->sdl_len = sizeof *sdl;
742 		sdl->sdl_family = AF_LINK;
743 		sdl->sdl_index = ifp->if_index;
744 		sdl->sdl_type = IFT_ETHER;
745 		sdl->sdl_alen = ETHER_ADDR_LEN;
746 		e_addr = LLADDR(sdl);
747 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
748 		*llsa = (struct sockaddr *)sdl;
749 		return 0;
750 #endif
751 
752 	default:
753 		/*
754 		 * Well, the text isn't quite right, but it's the name
755 		 * that counts...
756 		 */
757 		return EAFNOSUPPORT;
758 	}
759 }
760 
761 #if 0
762 /*
763  * This is for reference.  We have a table-driven version
764  * of the little-endian crc32 generator, which is faster
765  * than the double-loop.
766  */
767 uint32_t
768 ether_crc32_le(const uint8_t *buf, size_t len)
769 {
770 	uint32_t c, crc, carry;
771 	size_t i, j;
772 
773 	crc = 0xffffffffU;	/* initial value */
774 
775 	for (i = 0; i < len; i++) {
776 		c = buf[i];
777 		for (j = 0; j < 8; j++) {
778 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
779 			crc >>= 1;
780 			c >>= 1;
781 			if (carry)
782 				crc = (crc ^ ETHER_CRC_POLY_LE);
783 		}
784 	}
785 
786 	return (crc);
787 }
788 #else
789 uint32_t
790 ether_crc32_le(const uint8_t *buf, size_t len)
791 {
792 	static const uint32_t crctab[] = {
793 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
794 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
795 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
796 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
797 	};
798 	uint32_t crc;
799 	size_t i;
800 
801 	crc = 0xffffffffU;	/* initial value */
802 
803 	for (i = 0; i < len; i++) {
804 		crc ^= buf[i];
805 		crc = (crc >> 4) ^ crctab[crc & 0xf];
806 		crc = (crc >> 4) ^ crctab[crc & 0xf];
807 	}
808 
809 	return (crc);
810 }
811 #endif
812 
813 uint32_t
814 ether_crc32_be(const uint8_t *buf, size_t len)
815 {
816 	uint32_t c, crc, carry;
817 	size_t i, j;
818 
819 	crc = 0xffffffffU;	/* initial value */
820 
821 	for (i = 0; i < len; i++) {
822 		c = buf[i];
823 		for (j = 0; j < 8; j++) {
824 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
825 			crc <<= 1;
826 			c >>= 1;
827 			if (carry)
828 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
829 		}
830 	}
831 
832 	return (crc);
833 }
834 
835 /*
836  * find the size of ethernet header, and call classifier
837  */
838 void
839 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
840 		   struct altq_pktattr *pktattr)
841 {
842 	struct ether_header *eh;
843 	uint16_t ether_type;
844 	int hlen, af, hdrsize;
845 	caddr_t hdr;
846 
847 	hlen = sizeof(struct ether_header);
848 	eh = mtod(m, struct ether_header *);
849 
850 	ether_type = ntohs(eh->ether_type);
851 	if (ether_type < ETHERMTU) {
852 		/* ick! LLC/SNAP */
853 		struct llc *llc = (struct llc *)(eh + 1);
854 		hlen += 8;
855 
856 		if (m->m_len < hlen ||
857 		    llc->llc_dsap != LLC_SNAP_LSAP ||
858 		    llc->llc_ssap != LLC_SNAP_LSAP ||
859 		    llc->llc_control != LLC_UI)
860 			goto bad;  /* not snap! */
861 
862 		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
863 	}
864 
865 	if (ether_type == ETHERTYPE_IP) {
866 		af = AF_INET;
867 		hdrsize = 20;  /* sizeof(struct ip) */
868 #ifdef INET6
869 	} else if (ether_type == ETHERTYPE_IPV6) {
870 		af = AF_INET6;
871 		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
872 #endif
873 	} else
874 		goto bad;
875 
876 	while (m->m_len <= hlen) {
877 		hlen -= m->m_len;
878 		m = m->m_next;
879 	}
880 	hdr = m->m_data + hlen;
881 	if (m->m_len < hlen + hdrsize) {
882 		/*
883 		 * ip header is not in a single mbuf.  this should not
884 		 * happen in the current code.
885 		 * (todo: use m_pulldown in the future)
886 		 */
887 		goto bad;
888 	}
889 	m->m_data += hlen;
890 	m->m_len -= hlen;
891 	ifq_classify(ifq, m, af, pktattr);
892 	m->m_data -= hlen;
893 	m->m_len += hlen;
894 
895 	return;
896 
897 bad:
898 	pktattr->pattr_class = NULL;
899 	pktattr->pattr_hdr = NULL;
900 	pktattr->pattr_af = AF_UNSPEC;
901 }
902 
903 static void
904 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
905 		     const struct ether_header *save_eh)
906 {
907 	struct mbuf *m = *m0;
908 
909 	ether_restore_hdr++;
910 
911 	/*
912 	 * Prepend the header, optimize for the common case of
913 	 * eh pointing into the mbuf.
914 	 */
915 	if ((const void *)(eh + 1) == (void *)m->m_data) {
916 		m->m_data -= ETHER_HDR_LEN;
917 		m->m_len += ETHER_HDR_LEN;
918 		m->m_pkthdr.len += ETHER_HDR_LEN;
919 	} else {
920 		ether_prepend_hdr++;
921 
922 		M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
923 		if (m != NULL) {
924 			bcopy(save_eh, mtod(m, struct ether_header *),
925 			      ETHER_HDR_LEN);
926 		}
927 	}
928 	*m0 = m;
929 }
930 
931 static void
932 ether_input_ipifunc(void *arg)
933 {
934 	struct mbuf *m, *next;
935 	lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
936 
937 	m = arg;
938 	do {
939 		next = m->m_nextpkt;
940 		m->m_nextpkt = NULL;
941 		lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
942 		m = next;
943 	} while (m != NULL);
944 }
945 
946 void
947 ether_input_dispatch(struct mbuf_chain *chain)
948 {
949 #ifdef SMP
950 	int i;
951 
952 	logether(disp_beg, NULL);
953 	for (i = 0; i < ncpus; ++i) {
954 		if (chain[i].mc_head != NULL) {
955 			lwkt_send_ipiq(globaldata_find(i),
956 				       ether_input_ipifunc, chain[i].mc_head);
957 		}
958 	}
959 #else
960 	logether(disp_beg, NULL);
961 	if (chain->mc_head != NULL)
962 		ether_input_ipifunc(chain->mc_head);
963 #endif
964 	logether(disp_end, NULL);
965 }
966 
967 void
968 ether_input_chain_init(struct mbuf_chain *chain)
969 {
970 #ifdef SMP
971 	int i;
972 
973 	for (i = 0; i < ncpus; ++i)
974 		chain[i].mc_head = chain[i].mc_tail = NULL;
975 #else
976 	chain->mc_head = chain->mc_tail = NULL;
977 #endif
978 }
979 
980 /*
981  * Upper layer processing for a received Ethernet packet.
982  */
983 void
984 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
985 {
986 	struct ether_header *eh;
987 	int isr, discard = 0;
988 	u_short ether_type;
989 	struct ip_fw *rule = NULL;
990 
991 	M_ASSERTPKTHDR(m);
992 	KASSERT(m->m_len >= ETHER_HDR_LEN,
993 		("ether header is not contiguous!\n"));
994 
995 	eh = mtod(m, struct ether_header *);
996 
997 	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
998 		struct m_tag *mtag;
999 
1000 		/* Extract info from dummynet tag */
1001 		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1002 		KKASSERT(mtag != NULL);
1003 		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1004 		KKASSERT(rule != NULL);
1005 
1006 		m_tag_delete(m, mtag);
1007 		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1008 
1009 		/* packet is passing the second time */
1010 		goto post_stats;
1011 	}
1012 
1013 	/*
1014 	 * We got a packet which was unicast to a different Ethernet
1015 	 * address.  If the driver is working properly, then this
1016 	 * situation can only happen when the interface is in
1017 	 * promiscuous mode.  We defer the packet discarding until the
1018 	 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1019 	 * could work.
1020 	 */
1021 	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1022 	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1023 	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1024 		if (ether_debug & 1) {
1025 			kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1026 				"%02x:%02x:%02x:%02x:%02x:%02x "
1027 				"%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1028 				eh->ether_dhost[0],
1029 				eh->ether_dhost[1],
1030 				eh->ether_dhost[2],
1031 				eh->ether_dhost[3],
1032 				eh->ether_dhost[4],
1033 				eh->ether_dhost[5],
1034 				eh->ether_shost[0],
1035 				eh->ether_shost[1],
1036 				eh->ether_shost[2],
1037 				eh->ether_shost[3],
1038 				eh->ether_shost[4],
1039 				eh->ether_shost[5],
1040 				eh->ether_type,
1041 				((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1042 				((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1043 				((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1044 				((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1045 				((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1046 				((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1047 			);
1048 		}
1049 		if ((ether_debug & 2) == 0)
1050 			discard = 1;
1051 	}
1052 
1053 post_stats:
1054 	if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1055 		struct ether_header save_eh = *eh;
1056 
1057 		/* XXX old crufty stuff, needs to be removed */
1058 		m_adj(m, sizeof(struct ether_header));
1059 
1060 		if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1061 			m_freem(m);
1062 			return;
1063 		}
1064 
1065 		ether_restore_header(&m, eh, &save_eh);
1066 		if (m == NULL)
1067 			return;
1068 		eh = mtod(m, struct ether_header *);
1069 	}
1070 
1071 	ether_type = ntohs(eh->ether_type);
1072 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1073 
1074 	if (m->m_flags & M_VLANTAG) {
1075 		void (*vlan_input_func)(struct mbuf *);
1076 
1077 		vlan_input_func = vlan_input_p;
1078 		if (vlan_input_func != NULL) {
1079 			vlan_input_func(m);
1080 		} else {
1081 			m->m_pkthdr.rcvif->if_noproto++;
1082 			m_freem(m);
1083 		}
1084 		return;
1085 	}
1086 
1087 	/*
1088 	 * If we have been asked to discard this packet
1089 	 * (e.g. not for us), drop it before entering
1090 	 * the upper layer.
1091 	 */
1092 	if (discard) {
1093 		m_freem(m);
1094 		return;
1095 	}
1096 
1097 	/*
1098 	 * Clear protocol specific flags,
1099 	 * before entering the upper layer.
1100 	 */
1101 	m->m_flags &= ~M_ETHER_FLAGS;
1102 
1103 	/* Strip ethernet header. */
1104 	m_adj(m, sizeof(struct ether_header));
1105 
1106 	switch (ether_type) {
1107 #ifdef INET
1108 	case ETHERTYPE_IP:
1109 		if ((m->m_flags & M_LENCHECKED) == 0) {
1110 			if (!ip_lengthcheck(&m, 0))
1111 				return;
1112 		}
1113 		if (ipflow_fastforward(m))
1114 			return;
1115 		isr = NETISR_IP;
1116 		break;
1117 
1118 	case ETHERTYPE_ARP:
1119 		if (ifp->if_flags & IFF_NOARP) {
1120 			/* Discard packet if ARP is disabled on interface */
1121 			m_freem(m);
1122 			return;
1123 		}
1124 		isr = NETISR_ARP;
1125 		break;
1126 #endif
1127 
1128 #ifdef INET6
1129 	case ETHERTYPE_IPV6:
1130 		isr = NETISR_IPV6;
1131 		break;
1132 #endif
1133 
1134 #ifdef IPX
1135 	case ETHERTYPE_IPX:
1136 		if (ef_inputp) {
1137 			/*
1138 			 * Hold BGL and recheck ef_inputp
1139 			 */
1140 			get_mplock();
1141 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1142 				rel_mplock();
1143 				return;
1144 			}
1145 			rel_mplock();
1146 		}
1147 		isr = NETISR_IPX;
1148 		break;
1149 #endif
1150 
1151 #ifdef MPLS
1152 	case ETHERTYPE_MPLS:
1153 	case ETHERTYPE_MPLS_MCAST:
1154 		/* Should have been set by ether_input_chain(). */
1155 		KKASSERT(m->m_flags & M_MPLSLABELED);
1156 		isr = NETISR_MPLS;
1157 		break;
1158 #endif
1159 
1160 	default:
1161 		/*
1162 		 * The accurate msgport is not determined before
1163 		 * we reach here, so recharacterize packet.
1164 		 */
1165 		m->m_flags &= ~M_HASH;
1166 #ifdef IPX
1167 		if (ef_inputp) {
1168 			/*
1169 			 * Hold BGL and recheck ef_inputp
1170 			 */
1171 			get_mplock();
1172 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1173 				rel_mplock();
1174 				return;
1175 			}
1176 			rel_mplock();
1177 		}
1178 #endif
1179 		if (ng_ether_input_orphan_p != NULL) {
1180 			/*
1181 			 * Put back the ethernet header so netgraph has a
1182 			 * consistent view of inbound packets.
1183 			 */
1184 			M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1185 			if (m == NULL) {
1186 				/*
1187 				 * M_PREPEND frees the mbuf in case of failure.
1188 				 */
1189 				return;
1190 			}
1191 			/*
1192 			 * Hold BGL and recheck ng_ether_input_orphan_p
1193 			 */
1194 			get_mplock();
1195 			if (ng_ether_input_orphan_p != NULL) {
1196 				ng_ether_input_orphan_p(ifp, m);
1197 				rel_mplock();
1198 				return;
1199 			}
1200 			rel_mplock();
1201 		}
1202 		m_freem(m);
1203 		return;
1204 	}
1205 
1206 	if (m->m_flags & M_HASH) {
1207 		if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1208 			netisr_handle(isr, m);
1209 			return;
1210 		} else {
1211 			/*
1212 			 * XXX Something is wrong,
1213 			 * we probably should panic here!
1214 			 */
1215 			m->m_flags &= ~M_HASH;
1216 			ether_input_wronghash++;
1217 		}
1218 	}
1219 #ifdef RSS_DEBUG
1220 	ether_input_requeue++;
1221 #endif
1222 	netisr_queue(isr, m);
1223 }
1224 
1225 /*
1226  * First we perform any link layer operations, then continue to the
1227  * upper layers with ether_demux_oncpu().
1228  */
1229 static void
1230 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1231 {
1232 	if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1233 		/*
1234 		 * Receiving interface's flags are changed, when this
1235 		 * packet is waiting for processing; discard it.
1236 		 */
1237 		m_freem(m);
1238 		return;
1239 	}
1240 
1241 	/*
1242 	 * Tap the packet off here for a bridge.  bridge_input()
1243 	 * will return NULL if it has consumed the packet, otherwise
1244 	 * it gets processed as normal.  Note that bridge_input()
1245 	 * will always return the original packet if we need to
1246 	 * process it locally.
1247 	 */
1248 	if (ifp->if_bridge) {
1249 		KASSERT(bridge_input_p != NULL,
1250 			("%s: if_bridge not loaded!", __func__));
1251 
1252 		if(m->m_flags & M_ETHER_BRIDGED) {
1253 			m->m_flags &= ~M_ETHER_BRIDGED;
1254 		} else {
1255 			m = bridge_input_p(ifp, m);
1256 			if (m == NULL)
1257 				return;
1258 
1259 			KASSERT(ifp == m->m_pkthdr.rcvif,
1260 				("bridge_input_p changed rcvif\n"));
1261 		}
1262 	}
1263 
1264 #ifdef CARP
1265 	if (ifp->if_carp) {
1266 		/*
1267 		 * Hold CARP token and recheck ifp->if_carp
1268 		 */
1269 		carp_gettok();
1270 		if (ifp->if_carp) {
1271 			m = carp_input(ifp->if_carp, m);
1272 			if (m == NULL) {
1273 				carp_reltok();
1274 				return;
1275 			}
1276 			KASSERT(ifp == m->m_pkthdr.rcvif,
1277 				("carp_input changed rcvif\n"));
1278 		}
1279 		carp_reltok();
1280 	}
1281 #endif
1282 
1283 	/* Handle ng_ether(4) processing, if any */
1284 	if (ng_ether_input_p != NULL) {
1285 		/*
1286 		 * Hold BGL and recheck ng_ether_input_p
1287 		 */
1288 		get_mplock();
1289 		if (ng_ether_input_p != NULL)
1290 			ng_ether_input_p(ifp, &m);
1291 		rel_mplock();
1292 
1293 		if (m == NULL)
1294 			return;
1295 	}
1296 
1297 	/* Continue with upper layer processing */
1298 	ether_demux_oncpu(ifp, m);
1299 }
1300 
1301 /*
1302  * Perform certain functions of ether_input_chain():
1303  * - Test IFF_UP
1304  * - Update statistics
1305  * - Run bpf(4) tap if requested
1306  * Then pass the packet to ether_input_oncpu().
1307  *
1308  * This function should be used by pseudo interface (e.g. vlan(4)),
1309  * when it tries to claim that the packet is received by it.
1310  *
1311  * REINPUT_KEEPRCVIF
1312  * REINPUT_RUNBPF
1313  */
1314 void
1315 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1316 {
1317 	/* Discard packet if interface is not up */
1318 	if (!(ifp->if_flags & IFF_UP)) {
1319 		m_freem(m);
1320 		return;
1321 	}
1322 
1323 	/*
1324 	 * Change receiving interface.  The bridge will often pass a flag to
1325 	 * ask that this not be done so ARPs get applied to the correct
1326 	 * side.
1327 	 */
1328 	if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1329 	    m->m_pkthdr.rcvif == NULL) {
1330 		m->m_pkthdr.rcvif = ifp;
1331 	}
1332 
1333 	/* Update statistics */
1334 	ifp->if_ipackets++;
1335 	ifp->if_ibytes += m->m_pkthdr.len;
1336 	if (m->m_flags & (M_MCAST | M_BCAST))
1337 		ifp->if_imcasts++;
1338 
1339 	if (reinput_flags & REINPUT_RUNBPF)
1340 		BPF_MTAP(ifp, m);
1341 
1342 	ether_input_oncpu(ifp, m);
1343 }
1344 
1345 static __inline boolean_t
1346 ether_vlancheck(struct mbuf **m0)
1347 {
1348 	struct mbuf *m = *m0;
1349 	struct ether_header *eh;
1350 	uint16_t ether_type;
1351 
1352 	eh = mtod(m, struct ether_header *);
1353 	ether_type = ntohs(eh->ether_type);
1354 
1355 	if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1356 		/*
1357 		 * Extract vlan tag if hardware does not do it for us
1358 		 */
1359 		vlan_ether_decap(&m);
1360 		if (m == NULL)
1361 			goto failed;
1362 
1363 		eh = mtod(m, struct ether_header *);
1364 		ether_type = ntohs(eh->ether_type);
1365 	}
1366 
1367 	if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1368 		/*
1369 		 * To prevent possible dangerous recursion,
1370 		 * we don't do vlan-in-vlan
1371 		 */
1372 		m->m_pkthdr.rcvif->if_noproto++;
1373 		goto failed;
1374 	}
1375 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1376 
1377 	m->m_flags |= M_ETHER_VLANCHECKED;
1378 	*m0 = m;
1379 	return TRUE;
1380 failed:
1381 	if (m != NULL)
1382 		m_freem(m);
1383 	*m0 = NULL;
1384 	return FALSE;
1385 }
1386 
1387 static void
1388 ether_input_handler(netmsg_t nmsg)
1389 {
1390 	struct netmsg_packet *nmp = &nmsg->packet;	/* actual size */
1391 	struct ether_header *eh;
1392 	struct ifnet *ifp;
1393 	struct mbuf *m;
1394 
1395 	m = nmp->nm_packet;
1396 	M_ASSERTPKTHDR(m);
1397 	ifp = m->m_pkthdr.rcvif;
1398 
1399 	eh = mtod(m, struct ether_header *);
1400 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1401 		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1402 			 ifp->if_addrlen) == 0)
1403 			m->m_flags |= M_BCAST;
1404 		else
1405 			m->m_flags |= M_MCAST;
1406 		ifp->if_imcasts++;
1407 	}
1408 
1409 	if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1410 		if (!ether_vlancheck(&m)) {
1411 			KKASSERT(m == NULL);
1412 			return;
1413 		}
1414 	}
1415 
1416 	ether_input_oncpu(ifp, m);
1417 }
1418 
1419 /*
1420  * Send the packet to the target msgport or queue it into 'chain'.
1421  *
1422  * At this point the packet had better be characterized (M_HASH set),
1423  * so we know which cpu to send it to.
1424  */
1425 static void
1426 ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
1427 {
1428 	struct netmsg_packet *pmsg;
1429 
1430 	KKASSERT(m->m_flags & M_HASH);
1431 	pmsg = &m->m_hdr.mh_netmsg;
1432 	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1433 		    0, ether_input_handler);
1434 	pmsg->nm_packet = m;
1435 	pmsg->base.lmsg.u.ms_result = isr;
1436 
1437 	if (chain != NULL) {
1438 		int cpuid = m->m_pkthdr.hash;
1439 		struct mbuf_chain *c;
1440 
1441 		c = &chain[cpuid];
1442 		if (c->mc_head == NULL) {
1443 			c->mc_head = c->mc_tail = m;
1444 		} else {
1445 			c->mc_tail->m_nextpkt = m;
1446 			c->mc_tail = m;
1447 		}
1448 		m->m_nextpkt = NULL;
1449 	} else {
1450 		lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1451 	}
1452 }
1453 
1454 /*
1455  * Process a received Ethernet packet.
1456  *
1457  * The ethernet header is assumed to be in the mbuf so the caller
1458  * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1459  * bytes in the first mbuf.
1460  *
1461  * - If 'chain' is NULL, this ether frame is sent to the target msgport
1462  *   immediately.  This situation happens when ether_input_chain is
1463  *   accessed through ifnet.if_input.
1464  *
1465  * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1466  *   bucket indexed by the target msgport's cpuid and the target msgport
1467  *   is saved in mbuf's m_pkthdr.m_head.  Caller of ether_input_chain
1468  *   must initialize 'chain' by calling ether_input_chain_init().
1469  *   ether_input_dispatch must be called later to send ether frames
1470  *   queued on 'chain' to their target msgport.
1471  */
1472 void
1473 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1474 		  struct mbuf_chain *chain)
1475 {
1476 	int isr;
1477 
1478 	M_ASSERTPKTHDR(m);
1479 
1480 	/* Discard packet if interface is not up */
1481 	if (!(ifp->if_flags & IFF_UP)) {
1482 		m_freem(m);
1483 		return;
1484 	}
1485 
1486 	if (m->m_len < sizeof(struct ether_header)) {
1487 		/* XXX error in the caller. */
1488 		m_freem(m);
1489 		return;
1490 	}
1491 
1492 	m->m_pkthdr.rcvif = ifp;
1493 
1494 	logether(chain_beg, ifp);
1495 
1496 	ETHER_BPF_MTAP(ifp, m);
1497 
1498 	ifp->if_ibytes += m->m_pkthdr.len;
1499 
1500 	if (ifp->if_flags & IFF_MONITOR) {
1501 		struct ether_header *eh;
1502 
1503 		eh = mtod(m, struct ether_header *);
1504 		if (ETHER_IS_MULTICAST(eh->ether_dhost))
1505 			ifp->if_imcasts++;
1506 
1507 		/*
1508 		 * Interface marked for monitoring; discard packet.
1509 		 */
1510 		m_freem(m);
1511 
1512 		logether(chain_end, ifp);
1513 		return;
1514 	}
1515 
1516 	/*
1517 	 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1518 	 * we can dispatch it immediately without further inspection.
1519 	 */
1520 	if (pi != NULL && (m->m_flags & M_HASH)) {
1521 #ifdef RSS_DEBUG
1522 		ether_pktinfo_try++;
1523 #endif
1524 		netisr_hashcheck(pi->pi_netisr, m, pi);
1525 		if (m->m_flags & M_HASH) {
1526 			ether_dispatch(pi->pi_netisr, m, chain);
1527 #ifdef RSS_DEBUG
1528 			ether_pktinfo_hit++;
1529 #endif
1530 			logether(chain_end, ifp);
1531 			return;
1532 		}
1533 	}
1534 #ifdef RSS_DEBUG
1535 	else if (ifp->if_capenable & IFCAP_RSS) {
1536 		if (pi == NULL)
1537 			ether_rss_nopi++;
1538 		else
1539 			ether_rss_nohash++;
1540 	}
1541 #endif
1542 
1543 	/*
1544 	 * Packet hash will be recalculated by software,
1545 	 * so clear the M_HASH flag set by the driver;
1546 	 * the hash value calculated by the hardware may
1547 	 * not be exactly what we want.
1548 	 */
1549 	m->m_flags &= ~M_HASH;
1550 
1551 	if (!ether_vlancheck(&m)) {
1552 		KKASSERT(m == NULL);
1553 		logether(chain_end, ifp);
1554 		return;
1555 	}
1556 
1557 	isr = ether_characterize(&m);
1558 	if (m == NULL) {
1559 		logether(chain_end, ifp);
1560 		return;
1561 	}
1562 
1563 	/*
1564 	 * Finally dispatch it
1565 	 */
1566 	ether_dispatch(isr, m, chain);
1567 
1568 	logether(chain_end, ifp);
1569 }
1570 
1571 static int
1572 ether_characterize(struct mbuf **m0)
1573 {
1574 	struct mbuf *m = *m0;
1575 	struct ether_header *eh;
1576 	uint16_t ether_type;
1577 	int isr;
1578 
1579 	eh = mtod(m, struct ether_header *);
1580 	ether_type = ntohs(eh->ether_type);
1581 
1582 	/*
1583 	 * Map ether type to netisr id.
1584 	 */
1585 	switch (ether_type) {
1586 #ifdef INET
1587 	case ETHERTYPE_IP:
1588 		isr = NETISR_IP;
1589 		break;
1590 
1591 	case ETHERTYPE_ARP:
1592 		isr = NETISR_ARP;
1593 		break;
1594 #endif
1595 
1596 #ifdef INET6
1597 	case ETHERTYPE_IPV6:
1598 		isr = NETISR_IPV6;
1599 		break;
1600 #endif
1601 
1602 #ifdef IPX
1603 	case ETHERTYPE_IPX:
1604 		isr = NETISR_IPX;
1605 		break;
1606 #endif
1607 
1608 #ifdef MPLS
1609 	case ETHERTYPE_MPLS:
1610 	case ETHERTYPE_MPLS_MCAST:
1611 		m->m_flags |= M_MPLSLABELED;
1612 		isr = NETISR_MPLS;
1613 		break;
1614 #endif
1615 
1616 	default:
1617 		/*
1618 		 * NETISR_MAX is an invalid value; it is chosen to let
1619 		 * netisr_characterize() know that we have no clear
1620 		 * idea where this packet should go.
1621 		 */
1622 		isr = NETISR_MAX;
1623 		break;
1624 	}
1625 
1626 	/*
1627 	 * Ask the isr to characterize the packet since we couldn't.
1628 	 * This is an attempt to optimally get us onto the correct protocol
1629 	 * thread.
1630 	 */
1631 	netisr_characterize(isr, &m, sizeof(struct ether_header));
1632 
1633 	*m0 = m;
1634 	return isr;
1635 }
1636 
1637 static void
1638 ether_demux_handler(netmsg_t nmsg)
1639 {
1640 	struct netmsg_packet *nmp = &nmsg->packet;	/* actual size */
1641 	struct ifnet *ifp;
1642 	struct mbuf *m;
1643 
1644 	m = nmp->nm_packet;
1645 	M_ASSERTPKTHDR(m);
1646 	ifp = m->m_pkthdr.rcvif;
1647 
1648 	ether_demux_oncpu(ifp, m);
1649 }
1650 
1651 void
1652 ether_demux(struct mbuf *m)
1653 {
1654 	struct netmsg_packet *pmsg;
1655 	int isr;
1656 
1657 	isr = ether_characterize(&m);
1658 	if (m == NULL)
1659 		return;
1660 
1661 	KKASSERT(m->m_flags & M_HASH);
1662 	pmsg = &m->m_hdr.mh_netmsg;
1663 	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1664 	    0, ether_demux_handler);
1665 	pmsg->nm_packet = m;
1666 	pmsg->base.lmsg.u.ms_result = isr;
1667 
1668 	lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1669 }
1670 
1671 MODULE_VERSION(ether, 1);
1672