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