xref: /dragonfly/sys/net/if_ethersubr.c (revision a68e0df0)
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  * $DragonFly: src/sys/net/if_ethersubr.c,v 1.96 2008/11/22 04:00:53 sephe Exp $
36  */
37 
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_mpls.h"
43 #include "opt_netgraph.h"
44 #include "opt_carp.h"
45 #include "opt_rss.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
51 #include <sys/ktr.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/msgport.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread.h>
60 
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
63 
64 #include <net/if.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/if_llc.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
71 #include <net/bpf.h>
72 #include <net/ethernet.h>
73 #include <net/vlan/if_vlan_ether.h>
74 #include <net/netmsg2.h>
75 
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/ip_flow.h>
81 #include <net/ipfw/ip_fw.h>
82 #include <net/dummynet/ip_dummynet.h>
83 #endif
84 #ifdef INET6
85 #include <netinet6/nd6.h>
86 #endif
87 
88 #ifdef CARP
89 #include <netinet/ip_carp.h>
90 #endif
91 
92 #ifdef IPX
93 #include <netproto/ipx/ipx.h>
94 #include <netproto/ipx/ipx_if.h>
95 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
96 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
97 		  short *tp, int *hlen);
98 #endif
99 
100 #ifdef NS
101 #include <netns/ns.h>
102 #include <netns/ns_if.h>
103 ushort ns_nettype;
104 int ether_outputdebug = 0;
105 int ether_inputdebug = 0;
106 #endif
107 
108 #ifdef NETATALK
109 #include <netproto/atalk/at.h>
110 #include <netproto/atalk/at_var.h>
111 #include <netproto/atalk/at_extern.h>
112 
113 #define	llc_snap_org_code	llc_un.type_snap.org_code
114 #define	llc_snap_ether_type	llc_un.type_snap.ether_type
115 
116 extern u_char	at_org_code[3];
117 extern u_char	aarp_org_code[3];
118 #endif /* NETATALK */
119 
120 #ifdef MPLS
121 #include <netproto/mpls/mpls.h>
122 #endif
123 
124 /* netgraph node hooks for ng_ether(4) */
125 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
126 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
127 		struct mbuf *m, const struct ether_header *eh);
128 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
129 void	(*ng_ether_attach_p)(struct ifnet *ifp);
130 void	(*ng_ether_detach_p)(struct ifnet *ifp);
131 
132 void	(*vlan_input_p)(struct mbuf *);
133 
134 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
135 			struct rtentry *);
136 static void ether_restore_header(struct mbuf **, const struct ether_header *,
137 				 const struct ether_header *);
138 
139 /*
140  * if_bridge support
141  */
142 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
143 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
144 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
145 
146 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
147 			      struct sockaddr *);
148 
149 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
150 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
151 };
152 
153 #define gotoerr(e) do { error = (e); goto bad; } while (0)
154 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
155 
156 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
157 				struct ip_fw **rule,
158 				const struct ether_header *eh);
159 
160 static int ether_ipfw;
161 static u_int ether_restore_hdr;
162 static u_int ether_prepend_hdr;
163 
164 #ifdef RSS_DEBUG
165 static u_int ether_pktinfo_try;
166 static u_int ether_pktinfo_hit;
167 static u_int ether_rss_nopi;
168 static u_int ether_rss_nohash;
169 #endif
170 
171 SYSCTL_DECL(_net_link);
172 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
173 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
174 	   &ether_ipfw, 0, "Pass ether pkts through firewall");
175 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
176 	    &ether_restore_hdr, 0, "# of ether header restoration");
177 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
178 	    &ether_prepend_hdr, 0,
179 	    "# of ether header restoration which prepends mbuf");
180 #ifdef RSS_DEBUG
181 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
182 	    &ether_rss_nopi, 0, "# of packets do not have pktinfo");
183 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
184 	    &ether_rss_nohash, 0, "# of packets do not have hash");
185 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
186 	    &ether_pktinfo_try, 0,
187 	    "# of tries to find packets' msgport using pktinfo");
188 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
189 	    &ether_pktinfo_hit, 0,
190 	    "# of packets whose msgport are found using pktinfo");
191 #endif
192 
193 #define ETHER_KTR_STR		"ifp=%p"
194 #define ETHER_KTR_ARG_SIZE	(sizeof(void *))
195 #ifndef KTR_ETHERNET
196 #define KTR_ETHERNET		KTR_ALL
197 #endif
198 KTR_INFO_MASTER(ether);
199 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
200 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
201 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
202 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
203 #define logether(name, arg)	KTR_LOG(ether_ ## name, arg)
204 
205 /*
206  * Ethernet output routine.
207  * Encapsulate a packet of type family for the local net.
208  * Use trailer local net encapsulation if enough data in first
209  * packet leaves a multiple of 512 bytes of data in remainder.
210  * Assumes that ifp is actually pointer to arpcom structure.
211  */
212 static int
213 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
214 	     struct rtentry *rt)
215 {
216 	struct ether_header *eh, *deh;
217 	u_char *edst;
218 	int loop_copy = 0;
219 	int hlen = ETHER_HDR_LEN;	/* link layer header length */
220 	struct arpcom *ac = IFP2AC(ifp);
221 	int error;
222 
223 	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
224 
225 	if (ifp->if_flags & IFF_MONITOR)
226 		gotoerr(ENETDOWN);
227 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
228 		gotoerr(ENETDOWN);
229 
230 	M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
231 	if (m == NULL)
232 		return (ENOBUFS);
233 	eh = mtod(m, struct ether_header *);
234 	edst = eh->ether_dhost;
235 
236 	/*
237 	 * Fill in the destination ethernet address and frame type.
238 	 */
239 	switch (dst->sa_family) {
240 #ifdef INET
241 	case AF_INET:
242 		if (!arpresolve(ifp, rt, m, dst, edst))
243 			return (0);	/* if not yet resolved */
244 #ifdef MPLS
245 		if (m->m_flags & M_MPLSLABELED)
246 			eh->ether_type = htons(ETHERTYPE_MPLS);
247 		else
248 #endif
249 			eh->ether_type = htons(ETHERTYPE_IP);
250 		break;
251 #endif
252 #ifdef INET6
253 	case AF_INET6:
254 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
255 			return (0);		/* Something bad happenned. */
256 		eh->ether_type = htons(ETHERTYPE_IPV6);
257 		break;
258 #endif
259 #ifdef IPX
260 	case AF_IPX:
261 		if (ef_outputp != NULL) {
262 			/*
263 			 * Hold BGL and recheck ef_outputp
264 			 */
265 			get_mplock();
266 			if (ef_outputp != NULL) {
267 				error = ef_outputp(ifp, &m, dst,
268 						   &eh->ether_type, &hlen);
269 				rel_mplock();
270 				if (error)
271 					goto bad;
272 				else
273 					break;
274 			}
275 			rel_mplock();
276 		}
277 		eh->ether_type = htons(ETHERTYPE_IPX);
278 		bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
279 		      edst, ETHER_ADDR_LEN);
280 		break;
281 #endif
282 #ifdef NETATALK
283 	case AF_APPLETALK: {
284 		struct at_ifaddr *aa;
285 
286 		/*
287 		 * Hold BGL
288 		 */
289 		get_mplock();
290 
291 		if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
292 			error = 0;	/* XXX */
293 			rel_mplock();
294 			goto bad;
295 		}
296 		/*
297 		 * In the phase 2 case, need to prepend an mbuf for
298 		 * the llc header.  Since we must preserve the value
299 		 * of m, which is passed to us by value, we m_copy()
300 		 * the first mbuf, and use it for our llc header.
301 		 */
302 		if (aa->aa_flags & AFA_PHASE2) {
303 			struct llc llc;
304 
305 			M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
306 			eh = mtod(m, struct ether_header *);
307 			edst = eh->ether_dhost;
308 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
309 			llc.llc_control = LLC_UI;
310 			bcopy(at_org_code, llc.llc_snap_org_code,
311 			      sizeof at_org_code);
312 			llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
313 			bcopy(&llc,
314 			      mtod(m, caddr_t) + sizeof(struct ether_header),
315 			      sizeof(struct llc));
316 			eh->ether_type = htons(m->m_pkthdr.len);
317 			hlen = sizeof(struct llc) + ETHER_HDR_LEN;
318 		} else {
319 			eh->ether_type = htons(ETHERTYPE_AT);
320 		}
321 		if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
322 			rel_mplock();
323 			return (0);
324 		}
325 
326 		rel_mplock();
327 		break;
328 	  }
329 #endif
330 #ifdef NS
331 	case AF_NS:
332 		switch(ns_nettype) {
333 		default:
334 		case 0x8137:	/* Novell Ethernet_II Ethernet TYPE II */
335 			eh->ether_type = 0x8137;
336 			break;
337 		case 0x0:	/* Novell 802.3 */
338 			eh->ether_type = htons(m->m_pkthdr.len);
339 			break;
340 		case 0xe0e0:	/* Novell 802.2 and Token-Ring */
341 			M_PREPEND(m, 3, MB_DONTWAIT);
342 			eh = mtod(m, struct ether_header *);
343 			edst = eh->ether_dhost;
344 			eh->ether_type = htons(m->m_pkthdr.len);
345 			cp = mtod(m, u_char *) + sizeof(struct ether_header);
346 			*cp++ = 0xE0;
347 			*cp++ = 0xE0;
348 			*cp++ = 0x03;
349 			break;
350 		}
351 		bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
352 		      ETHER_ADDR_LEN);
353 		/*
354 		 * XXX if ns_thishost is the same as the node's ethernet
355 		 * address then just the default code will catch this anyhow.
356 		 * So I'm not sure if this next clause should be here at all?
357 		 * [JRE]
358 		 */
359 		if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
360 			m->m_pkthdr.rcvif = ifp;
361 			netisr_dispatch(NETISR_NS, m);
362 			return (error);
363 		}
364 		if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
365 			m->m_flags |= M_BCAST;
366 		break;
367 #endif
368 	case pseudo_AF_HDRCMPLT:
369 	case AF_UNSPEC:
370 		loop_copy = -1; /* if this is for us, don't do it */
371 		deh = (struct ether_header *)dst->sa_data;
372 		memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
373 		eh->ether_type = deh->ether_type;
374 		break;
375 
376 	default:
377 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
378 		gotoerr(EAFNOSUPPORT);
379 	}
380 
381 	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
382 		memcpy(eh->ether_shost,
383 		       ((struct ether_header *)dst->sa_data)->ether_shost,
384 		       ETHER_ADDR_LEN);
385 	else
386 		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
387 
388 	/*
389 	 * Bridges require special output handling.
390 	 */
391 	if (ifp->if_bridge) {
392 		KASSERT(bridge_output_p != NULL,
393 			("%s: if_bridge not loaded!", __func__));
394 		return bridge_output_p(ifp, m);
395 	}
396 
397 	/*
398 	 * If a simplex interface, and the packet is being sent to our
399 	 * Ethernet address or a broadcast address, loopback a copy.
400 	 * XXX To make a simplex device behave exactly like a duplex
401 	 * device, we should copy in the case of sending to our own
402 	 * ethernet address (thus letting the original actually appear
403 	 * on the wire). However, we don't do that here for security
404 	 * reasons and compatibility with the original behavior.
405 	 */
406 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
407 		int csum_flags = 0;
408 
409 		if (m->m_pkthdr.csum_flags & CSUM_IP)
410 			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
411 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
412 			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
413 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
414 			struct mbuf *n;
415 
416 			if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
417 				n->m_pkthdr.csum_flags |= csum_flags;
418 				if (csum_flags & CSUM_DATA_VALID)
419 					n->m_pkthdr.csum_data = 0xffff;
420 				if_simloop(ifp, n, dst->sa_family, hlen);
421 			} else
422 				ifp->if_iqdrops++;
423 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
424 				ETHER_ADDR_LEN) == 0) {
425 			m->m_pkthdr.csum_flags |= csum_flags;
426 			if (csum_flags & CSUM_DATA_VALID)
427 				m->m_pkthdr.csum_data = 0xffff;
428 			if_simloop(ifp, m, dst->sa_family, hlen);
429 			return (0);	/* XXX */
430 		}
431 	}
432 
433 #ifdef CARP
434 	if (ifp->if_carp) {
435 		/*
436 		 * Hold BGL and recheck ifp->if_carp
437 		 */
438 		get_mplock();
439 		if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
440 			rel_mplock();
441 			goto bad;
442 		}
443 		rel_mplock();
444 	}
445 #endif
446 
447 
448 	/* Handle ng_ether(4) processing, if any */
449 	if (ng_ether_output_p != NULL) {
450 		/*
451 		 * Hold BGL and recheck ng_ether_output_p
452 		 */
453 		get_mplock();
454 		if (ng_ether_output_p != NULL) {
455 			if ((error = ng_ether_output_p(ifp, &m)) != 0) {
456 				rel_mplock();
457 				goto bad;
458 			}
459 			if (m == NULL) {
460 				rel_mplock();
461 				return (0);
462 			}
463 		}
464 		rel_mplock();
465 	}
466 
467 	/* Continue with link-layer output */
468 	return ether_output_frame(ifp, m);
469 
470 bad:
471 	m_freem(m);
472 	return (error);
473 }
474 
475 /*
476  * Ethernet link layer output routine to send a raw frame to the device.
477  *
478  * This assumes that the 14 byte Ethernet header is present and contiguous
479  * in the first mbuf.
480  */
481 int
482 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
483 {
484 	struct ip_fw *rule = NULL;
485 	int error = 0;
486 	struct altq_pktattr pktattr;
487 
488 	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
489 
490 	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
491 		struct m_tag *mtag;
492 
493 		/* Extract info from dummynet tag */
494 		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
495 		KKASSERT(mtag != NULL);
496 		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
497 		KKASSERT(rule != NULL);
498 
499 		m_tag_delete(m, mtag);
500 		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
501 	}
502 
503 	if (ifq_is_enabled(&ifp->if_snd))
504 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
505 	crit_enter();
506 	if (IPFW_LOADED && ether_ipfw != 0) {
507 		struct ether_header save_eh, *eh;
508 
509 		eh = mtod(m, struct ether_header *);
510 		save_eh = *eh;
511 		m_adj(m, ETHER_HDR_LEN);
512 		if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
513 			crit_exit();
514 			if (m != NULL) {
515 				m_freem(m);
516 				return ENOBUFS; /* pkt dropped */
517 			} else
518 				return 0;	/* consumed e.g. in a pipe */
519 		}
520 
521 		/* packet was ok, restore the ethernet header */
522 		ether_restore_header(&m, eh, &save_eh);
523 		if (m == NULL) {
524 			crit_exit();
525 			return ENOBUFS;
526 		}
527 	}
528 	crit_exit();
529 
530 	/*
531 	 * Queue message on interface, update output statistics if
532 	 * successful, and start output if interface not yet active.
533 	 */
534 	error = ifq_dispatch(ifp, m, &pktattr);
535 	return (error);
536 }
537 
538 /*
539  * ipfw processing for ethernet packets (in and out).
540  * The second parameter is NULL from ether_demux(), and ifp from
541  * ether_output_frame().
542  */
543 static boolean_t
544 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
545 	       const struct ether_header *eh)
546 {
547 	struct ether_header save_eh = *eh;	/* might be a ptr in *m0 */
548 	struct ip_fw_args args;
549 	struct m_tag *mtag;
550 	struct mbuf *m;
551 	int i;
552 
553 	if (*rule != NULL && fw_one_pass)
554 		return TRUE; /* dummynet packet, already partially processed */
555 
556 	/*
557 	 * I need some amount of data to be contiguous.
558 	 */
559 	i = min((*m0)->m_pkthdr.len, max_protohdr);
560 	if ((*m0)->m_len < i) {
561 		*m0 = m_pullup(*m0, i);
562 		if (*m0 == NULL)
563 			return FALSE;
564 	}
565 
566 	/*
567 	 * Clean up tags
568 	 */
569 	if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
570 		m_tag_delete(*m0, mtag);
571 	if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
572 		mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
573 		KKASSERT(mtag != NULL);
574 		m_tag_delete(*m0, mtag);
575 		(*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
576 	}
577 
578 	args.m = *m0;		/* the packet we are looking at		*/
579 	args.oif = dst;		/* destination, if any			*/
580 	args.rule = *rule;	/* matching rule to restart		*/
581 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
582 	i = ip_fw_chk_ptr(&args);
583 	*m0 = args.m;
584 	*rule = args.rule;
585 
586 	if (*m0 == NULL)
587 		return FALSE;
588 
589 	switch (i) {
590 	case IP_FW_PASS:
591 		return TRUE;
592 
593 	case IP_FW_DIVERT:
594 	case IP_FW_TEE:
595 	case IP_FW_DENY:
596 		/*
597 		 * XXX at some point add support for divert/forward actions.
598 		 * If none of the above matches, we have to drop the pkt.
599 		 */
600 		return FALSE;
601 
602 	case IP_FW_DUMMYNET:
603 		/*
604 		 * Pass the pkt to dummynet, which consumes it.
605 		 */
606 		m = *m0;	/* pass the original to dummynet */
607 		*m0 = NULL;	/* and nothing back to the caller */
608 
609 		ether_restore_header(&m, eh, &save_eh);
610 		if (m == NULL)
611 			return FALSE;
612 
613 		ip_fw_dn_io_ptr(m, args.cookie,
614 				dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
615 		ip_dn_queue(m);
616 		return FALSE;
617 
618 	default:
619 		panic("unknown ipfw return value: %d\n", i);
620 	}
621 }
622 
623 static void
624 ether_input(struct ifnet *ifp, struct mbuf *m)
625 {
626 	ether_input_chain(ifp, m, NULL, NULL);
627 }
628 
629 /*
630  * Perform common duties while attaching to interface list
631  */
632 void
633 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
634 {
635 	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
636 			   serializer);
637 }
638 
639 void
640 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
641 		   lwkt_serialize_t serializer)
642 {
643 	struct sockaddr_dl *sdl;
644 
645 	ifp->if_type = IFT_ETHER;
646 	ifp->if_addrlen = ETHER_ADDR_LEN;
647 	ifp->if_hdrlen = ETHER_HDR_LEN;
648 	if_attach(ifp, serializer);
649 	ifp->if_mtu = ETHERMTU;
650 	if (ifp->if_baudrate == 0)
651 		ifp->if_baudrate = 10000000;
652 	ifp->if_output = ether_output;
653 	ifp->if_input = ether_input;
654 	ifp->if_resolvemulti = ether_resolvemulti;
655 	ifp->if_broadcastaddr = etherbroadcastaddr;
656 	sdl = IF_LLSOCKADDR(ifp);
657 	sdl->sdl_type = IFT_ETHER;
658 	sdl->sdl_alen = ifp->if_addrlen;
659 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
660 	/*
661 	 * XXX Keep the current drivers happy.
662 	 * XXX Remove once all drivers have been cleaned up
663 	 */
664 	if (lla != IFP2AC(ifp)->ac_enaddr)
665 		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
666 	bpfattach(ifp, dlt, hdrlen);
667 	if (ng_ether_attach_p != NULL)
668 		(*ng_ether_attach_p)(ifp);
669 
670 	if_printf(ifp, "MAC address: %6D\n", lla, ":");
671 }
672 
673 /*
674  * Perform common duties while detaching an Ethernet interface
675  */
676 void
677 ether_ifdetach(struct ifnet *ifp)
678 {
679 	if_down(ifp);
680 
681 	if (ng_ether_detach_p != NULL)
682 		(*ng_ether_detach_p)(ifp);
683 	bpfdetach(ifp);
684 	if_detach(ifp);
685 }
686 
687 int
688 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
689 {
690 	struct ifaddr *ifa = (struct ifaddr *) data;
691 	struct ifreq *ifr = (struct ifreq *) data;
692 	int error = 0;
693 
694 #define IF_INIT(ifp) \
695 do { \
696 	if (((ifp)->if_flags & IFF_UP) == 0) { \
697 		(ifp)->if_flags |= IFF_UP; \
698 		(ifp)->if_init((ifp)->if_softc); \
699 	} \
700 } while (0)
701 
702 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
703 
704 	switch (command) {
705 	case SIOCSIFADDR:
706 		switch (ifa->ifa_addr->sa_family) {
707 #ifdef INET
708 		case AF_INET:
709 			IF_INIT(ifp);	/* before arpwhohas */
710 			arp_ifinit(ifp, ifa);
711 			break;
712 #endif
713 #ifdef IPX
714 		/*
715 		 * XXX - This code is probably wrong
716 		 */
717 		case AF_IPX:
718 			{
719 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
720 			struct arpcom *ac = IFP2AC(ifp);
721 
722 			if (ipx_nullhost(*ina))
723 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
724 			else
725 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
726 				      sizeof ac->ac_enaddr);
727 
728 			IF_INIT(ifp);	/* Set new address. */
729 			break;
730 			}
731 #endif
732 #ifdef NS
733 		/*
734 		 * XXX - This code is probably wrong
735 		 */
736 		case AF_NS:
737 		{
738 			struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
739 			struct arpcom *ac = IFP2AC(ifp);
740 
741 			if (ns_nullhost(*ina))
742 				ina->x_host = *(union ns_host *)(ac->ac_enaddr);
743 			else
744 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
745 				      sizeof ac->ac_enaddr);
746 
747 			/*
748 			 * Set new address
749 			 */
750 			IF_INIT(ifp);
751 			break;
752 		}
753 #endif
754 		default:
755 			IF_INIT(ifp);
756 			break;
757 		}
758 		break;
759 
760 	case SIOCGIFADDR:
761 		bcopy(IFP2AC(ifp)->ac_enaddr,
762 		      ((struct sockaddr *)ifr->ifr_data)->sa_data,
763 		      ETHER_ADDR_LEN);
764 		break;
765 
766 	case SIOCSIFMTU:
767 		/*
768 		 * Set the interface MTU.
769 		 */
770 		if (ifr->ifr_mtu > ETHERMTU) {
771 			error = EINVAL;
772 		} else {
773 			ifp->if_mtu = ifr->ifr_mtu;
774 		}
775 		break;
776 	default:
777 		error = EINVAL;
778 		break;
779 	}
780 	return (error);
781 
782 #undef IF_INIT
783 }
784 
785 int
786 ether_resolvemulti(
787 	struct ifnet *ifp,
788 	struct sockaddr **llsa,
789 	struct sockaddr *sa)
790 {
791 	struct sockaddr_dl *sdl;
792 	struct sockaddr_in *sin;
793 #ifdef INET6
794 	struct sockaddr_in6 *sin6;
795 #endif
796 	u_char *e_addr;
797 
798 	switch(sa->sa_family) {
799 	case AF_LINK:
800 		/*
801 		 * No mapping needed. Just check that it's a valid MC address.
802 		 */
803 		sdl = (struct sockaddr_dl *)sa;
804 		e_addr = LLADDR(sdl);
805 		if ((e_addr[0] & 1) != 1)
806 			return EADDRNOTAVAIL;
807 		*llsa = 0;
808 		return 0;
809 
810 #ifdef INET
811 	case AF_INET:
812 		sin = (struct sockaddr_in *)sa;
813 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
814 			return EADDRNOTAVAIL;
815 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
816 		       M_WAITOK | M_ZERO);
817 		sdl->sdl_len = sizeof *sdl;
818 		sdl->sdl_family = AF_LINK;
819 		sdl->sdl_index = ifp->if_index;
820 		sdl->sdl_type = IFT_ETHER;
821 		sdl->sdl_alen = ETHER_ADDR_LEN;
822 		e_addr = LLADDR(sdl);
823 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
824 		*llsa = (struct sockaddr *)sdl;
825 		return 0;
826 #endif
827 #ifdef INET6
828 	case AF_INET6:
829 		sin6 = (struct sockaddr_in6 *)sa;
830 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
831 			/*
832 			 * An IP6 address of 0 means listen to all
833 			 * of the Ethernet multicast address used for IP6.
834 			 * (This is used for multicast routers.)
835 			 */
836 			ifp->if_flags |= IFF_ALLMULTI;
837 			*llsa = 0;
838 			return 0;
839 		}
840 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
841 			return EADDRNOTAVAIL;
842 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
843 		       M_WAITOK | M_ZERO);
844 		sdl->sdl_len = sizeof *sdl;
845 		sdl->sdl_family = AF_LINK;
846 		sdl->sdl_index = ifp->if_index;
847 		sdl->sdl_type = IFT_ETHER;
848 		sdl->sdl_alen = ETHER_ADDR_LEN;
849 		e_addr = LLADDR(sdl);
850 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
851 		*llsa = (struct sockaddr *)sdl;
852 		return 0;
853 #endif
854 
855 	default:
856 		/*
857 		 * Well, the text isn't quite right, but it's the name
858 		 * that counts...
859 		 */
860 		return EAFNOSUPPORT;
861 	}
862 }
863 
864 #if 0
865 /*
866  * This is for reference.  We have a table-driven version
867  * of the little-endian crc32 generator, which is faster
868  * than the double-loop.
869  */
870 uint32_t
871 ether_crc32_le(const uint8_t *buf, size_t len)
872 {
873 	uint32_t c, crc, carry;
874 	size_t i, j;
875 
876 	crc = 0xffffffffU;	/* initial value */
877 
878 	for (i = 0; i < len; i++) {
879 		c = buf[i];
880 		for (j = 0; j < 8; j++) {
881 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
882 			crc >>= 1;
883 			c >>= 1;
884 			if (carry)
885 				crc = (crc ^ ETHER_CRC_POLY_LE);
886 		}
887 	}
888 
889 	return (crc);
890 }
891 #else
892 uint32_t
893 ether_crc32_le(const uint8_t *buf, size_t len)
894 {
895 	static const uint32_t crctab[] = {
896 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
897 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
898 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
899 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
900 	};
901 	uint32_t crc;
902 	size_t i;
903 
904 	crc = 0xffffffffU;	/* initial value */
905 
906 	for (i = 0; i < len; i++) {
907 		crc ^= buf[i];
908 		crc = (crc >> 4) ^ crctab[crc & 0xf];
909 		crc = (crc >> 4) ^ crctab[crc & 0xf];
910 	}
911 
912 	return (crc);
913 }
914 #endif
915 
916 uint32_t
917 ether_crc32_be(const uint8_t *buf, size_t len)
918 {
919 	uint32_t c, crc, carry;
920 	size_t i, j;
921 
922 	crc = 0xffffffffU;	/* initial value */
923 
924 	for (i = 0; i < len; i++) {
925 		c = buf[i];
926 		for (j = 0; j < 8; j++) {
927 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
928 			crc <<= 1;
929 			c >>= 1;
930 			if (carry)
931 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
932 		}
933 	}
934 
935 	return (crc);
936 }
937 
938 /*
939  * find the size of ethernet header, and call classifier
940  */
941 void
942 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
943 		   struct altq_pktattr *pktattr)
944 {
945 	struct ether_header *eh;
946 	uint16_t ether_type;
947 	int hlen, af, hdrsize;
948 	caddr_t hdr;
949 
950 	hlen = sizeof(struct ether_header);
951 	eh = mtod(m, struct ether_header *);
952 
953 	ether_type = ntohs(eh->ether_type);
954 	if (ether_type < ETHERMTU) {
955 		/* ick! LLC/SNAP */
956 		struct llc *llc = (struct llc *)(eh + 1);
957 		hlen += 8;
958 
959 		if (m->m_len < hlen ||
960 		    llc->llc_dsap != LLC_SNAP_LSAP ||
961 		    llc->llc_ssap != LLC_SNAP_LSAP ||
962 		    llc->llc_control != LLC_UI)
963 			goto bad;  /* not snap! */
964 
965 		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
966 	}
967 
968 	if (ether_type == ETHERTYPE_IP) {
969 		af = AF_INET;
970 		hdrsize = 20;  /* sizeof(struct ip) */
971 #ifdef INET6
972 	} else if (ether_type == ETHERTYPE_IPV6) {
973 		af = AF_INET6;
974 		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
975 #endif
976 	} else
977 		goto bad;
978 
979 	while (m->m_len <= hlen) {
980 		hlen -= m->m_len;
981 		m = m->m_next;
982 	}
983 	hdr = m->m_data + hlen;
984 	if (m->m_len < hlen + hdrsize) {
985 		/*
986 		 * ip header is not in a single mbuf.  this should not
987 		 * happen in the current code.
988 		 * (todo: use m_pulldown in the future)
989 		 */
990 		goto bad;
991 	}
992 	m->m_data += hlen;
993 	m->m_len -= hlen;
994 	ifq_classify(ifq, m, af, pktattr);
995 	m->m_data -= hlen;
996 	m->m_len += hlen;
997 
998 	return;
999 
1000 bad:
1001 	pktattr->pattr_class = NULL;
1002 	pktattr->pattr_hdr = NULL;
1003 	pktattr->pattr_af = AF_UNSPEC;
1004 }
1005 
1006 static void
1007 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1008 		     const struct ether_header *save_eh)
1009 {
1010 	struct mbuf *m = *m0;
1011 
1012 	ether_restore_hdr++;
1013 
1014 	/*
1015 	 * Prepend the header, optimize for the common case of
1016 	 * eh pointing into the mbuf.
1017 	 */
1018 	if ((const void *)(eh + 1) == (void *)m->m_data) {
1019 		m->m_data -= ETHER_HDR_LEN;
1020 		m->m_len += ETHER_HDR_LEN;
1021 		m->m_pkthdr.len += ETHER_HDR_LEN;
1022 	} else {
1023 		ether_prepend_hdr++;
1024 
1025 		M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1026 		if (m != NULL) {
1027 			bcopy(save_eh, mtod(m, struct ether_header *),
1028 			      ETHER_HDR_LEN);
1029 		}
1030 	}
1031 	*m0 = m;
1032 }
1033 
1034 static void
1035 ether_input_ipifunc(void *arg)
1036 {
1037 	struct mbuf *m, *next;
1038 	lwkt_port_t port;
1039 
1040 	m = arg;
1041 	do {
1042 		next = m->m_nextpkt;
1043 		m->m_nextpkt = NULL;
1044 
1045 		port = m->m_pkthdr.header;
1046 		m->m_pkthdr.header = NULL;
1047 
1048 		lwkt_sendmsg(port,
1049 		&m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1050 
1051 		m = next;
1052 	} while (m != NULL);
1053 }
1054 
1055 void
1056 ether_input_dispatch(struct mbuf_chain *chain)
1057 {
1058 #ifdef SMP
1059 	int i;
1060 
1061 	logether(disp_beg, NULL);
1062 	for (i = 0; i < ncpus; ++i) {
1063 		if (chain[i].mc_head != NULL) {
1064 			lwkt_send_ipiq(globaldata_find(i),
1065 			ether_input_ipifunc, chain[i].mc_head);
1066 		}
1067 	}
1068 #else
1069 	logether(disp_beg, NULL);
1070 	if (chain->mc_head != NULL)
1071 		ether_input_ipifunc(chain->mc_head);
1072 #endif
1073 	logether(disp_end, NULL);
1074 }
1075 
1076 void
1077 ether_input_chain_init(struct mbuf_chain *chain)
1078 {
1079 #ifdef SMP
1080 	int i;
1081 
1082 	for (i = 0; i < ncpus; ++i)
1083 		chain[i].mc_head = chain[i].mc_tail = NULL;
1084 #else
1085 	chain->mc_head = chain->mc_tail = NULL;
1086 #endif
1087 }
1088 
1089 /*
1090  * Upper layer processing for a received Ethernet packet.
1091  */
1092 void
1093 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1094 {
1095 	struct ether_header *eh;
1096 	int isr, redispatch, discard = 0;
1097 	u_short ether_type;
1098 	struct ip_fw *rule = NULL;
1099 #ifdef NETATALK
1100 	struct llc *l;
1101 #endif
1102 
1103 	M_ASSERTPKTHDR(m);
1104 	KASSERT(m->m_len >= ETHER_HDR_LEN,
1105 		("ether header is no contiguous!\n"));
1106 
1107 	eh = mtod(m, struct ether_header *);
1108 
1109 	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1110 		struct m_tag *mtag;
1111 
1112 		/* Extract info from dummynet tag */
1113 		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1114 		KKASSERT(mtag != NULL);
1115 		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1116 		KKASSERT(rule != NULL);
1117 
1118 		m_tag_delete(m, mtag);
1119 		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1120 
1121 		/* packet is passing the second time */
1122 		goto post_stats;
1123 	}
1124 
1125 #ifdef CARP
1126 	/*
1127 	 * XXX: Okay, we need to call carp_forus() and - if it is for
1128 	 * us jump over code that does the normal check
1129 	 * "ac_enaddr == ether_dhost". The check sequence is a bit
1130 	 * different from OpenBSD, so we jump over as few code as
1131 	 * possible, to catch _all_ sanity checks. This needs
1132 	 * evaluation, to see if the carp ether_dhost values break any
1133 	 * of these checks!
1134 	 */
1135 	if (ifp->if_carp) {
1136 		/*
1137 		 * Hold BGL and recheck ifp->if_carp
1138 		 */
1139 		get_mplock();
1140 		if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1141 			rel_mplock();
1142 			goto post_stats;
1143 		}
1144 		rel_mplock();
1145 	}
1146 #endif
1147 
1148 	/*
1149 	 * We got a packet which was unicast to a different Ethernet
1150 	 * address.  If the driver is working properly, then this
1151 	 * situation can only happen when the interface is in
1152 	 * promiscuous mode.  We defer the packet discarding until the
1153 	 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1154 	 * could work.
1155 	 */
1156 	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1157 	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1158 	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN))
1159 		discard = 1;
1160 
1161 post_stats:
1162 	if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1163 		struct ether_header save_eh = *eh;
1164 
1165 		/* XXX old crufty stuff, needs to be removed */
1166 		m_adj(m, sizeof(struct ether_header));
1167 
1168 		if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1169 			m_freem(m);
1170 			return;
1171 		}
1172 
1173 		ether_restore_header(&m, eh, &save_eh);
1174 		if (m == NULL)
1175 			return;
1176 		eh = mtod(m, struct ether_header *);
1177 	}
1178 
1179 	ether_type = ntohs(eh->ether_type);
1180 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1181 
1182 	if (m->m_flags & M_VLANTAG) {
1183 		void (*vlan_input_func)(struct mbuf *);
1184 
1185 		vlan_input_func = vlan_input_p;
1186 		if (vlan_input_func != NULL) {
1187 			vlan_input_func(m);
1188 		} else {
1189 			m->m_pkthdr.rcvif->if_noproto++;
1190 			m_freem(m);
1191 		}
1192 		return;
1193 	}
1194 
1195 	/*
1196 	 * If we have been asked to discard this packet
1197 	 * (e.g. not for us), drop it before entering
1198 	 * the upper layer.
1199 	 */
1200 	if (discard) {
1201 		m_freem(m);
1202 		return;
1203 	}
1204 
1205 	/*
1206 	 * Clear protocol specific flags,
1207 	 * before entering the upper layer.
1208 	 */
1209 	m->m_flags &= ~M_ETHER_FLAGS;
1210 
1211 	/* Strip ethernet header. */
1212 	m_adj(m, sizeof(struct ether_header));
1213 
1214 	/*
1215 	 * By default, we don't need to do the redispatch; for the
1216 	 * most common packet types, e.g. IPv4, ether_input_chain()
1217 	 * has already picked up the correct target network msgport.
1218 	 */
1219 	redispatch = 0;
1220 
1221 	switch (ether_type) {
1222 #ifdef INET
1223 	case ETHERTYPE_IP:
1224 		if ((m->m_flags & M_LENCHECKED) == 0) {
1225 			if (!ip_lengthcheck(&m))
1226 				return;
1227 		}
1228 		if (ipflow_fastforward(m))
1229 			return;
1230 		isr = NETISR_IP;
1231 		break;
1232 
1233 	case ETHERTYPE_ARP:
1234 		if (ifp->if_flags & IFF_NOARP) {
1235 			/* Discard packet if ARP is disabled on interface */
1236 			m_freem(m);
1237 			return;
1238 		}
1239 		isr = NETISR_ARP;
1240 		break;
1241 #endif
1242 
1243 #ifdef INET6
1244 	case ETHERTYPE_IPV6:
1245 		isr = NETISR_IPV6;
1246 		break;
1247 #endif
1248 
1249 #ifdef IPX
1250 	case ETHERTYPE_IPX:
1251 		if (ef_inputp) {
1252 			/*
1253 			 * Hold BGL and recheck ef_inputp
1254 			 */
1255 			get_mplock();
1256 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1257 				rel_mplock();
1258 				return;
1259 			}
1260 			rel_mplock();
1261 		}
1262 		isr = NETISR_IPX;
1263 		break;
1264 #endif
1265 
1266 #ifdef NS
1267 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1268 		isr = NETISR_NS;
1269 		break;
1270 
1271 #endif
1272 
1273 #ifdef NETATALK
1274 	case ETHERTYPE_AT:
1275 		isr = NETISR_ATALK1;
1276 		break;
1277 	case ETHERTYPE_AARP:
1278 		isr = NETISR_AARP;
1279 		break;
1280 #endif
1281 
1282 #ifdef MPLS
1283 	case ETHERTYPE_MPLS:
1284 	case ETHERTYPE_MPLS_MCAST:
1285 		/* Should have been set by ether_input_chain(). */
1286 		KKASSERT(m->m_flags & M_MPLSLABELED);
1287 		isr = NETISR_MPLS;
1288 		break;
1289 #endif
1290 
1291 	default:
1292 		/*
1293 		 * The accurate msgport is not determined before
1294 		 * we reach here, so redo the dispatching
1295 		 */
1296 		redispatch = 1;
1297 #ifdef IPX
1298 		if (ef_inputp) {
1299 			/*
1300 			 * Hold BGL and recheck ef_inputp
1301 			 */
1302 			get_mplock();
1303 			if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1304 				rel_mplock();
1305 				return;
1306 			}
1307 			rel_mplock();
1308 		}
1309 #endif
1310 #ifdef NS
1311 		checksum = mtod(m, ushort *);
1312 		/* Novell 802.3 */
1313 		if ((ether_type <= ETHERMTU) &&
1314 		    ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1315 			if (*checksum == 0xE0E0) {
1316 				m->m_pkthdr.len -= 3;
1317 				m->m_len -= 3;
1318 				m->m_data += 3;
1319 			}
1320 			isr = NETISR_NS;
1321 			break;
1322 		}
1323 #endif
1324 #ifdef NETATALK
1325 		if (ether_type > ETHERMTU)
1326 			goto dropanyway;
1327 		l = mtod(m, struct llc *);
1328 		if (l->llc_dsap == LLC_SNAP_LSAP &&
1329 		    l->llc_ssap == LLC_SNAP_LSAP &&
1330 		    l->llc_control == LLC_UI) {
1331 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1332 				 sizeof at_org_code) == 0 &&
1333 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1334 				m_adj(m, sizeof(struct llc));
1335 				isr = NETISR_ATALK2;
1336 				break;
1337 			}
1338 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1339 				 sizeof aarp_org_code) == 0 &&
1340 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1341 				m_adj(m, sizeof(struct llc));
1342 				isr = NETISR_AARP;
1343 				break;
1344 			}
1345 		}
1346 dropanyway:
1347 #endif
1348 		if (ng_ether_input_orphan_p != NULL) {
1349 			/*
1350 			 * Hold BGL and recheck ng_ether_input_orphan_p
1351 			 */
1352 			get_mplock();
1353 			if (ng_ether_input_orphan_p != NULL) {
1354 				ng_ether_input_orphan_p(ifp, m, eh);
1355 				rel_mplock();
1356 				return;
1357 			}
1358 			rel_mplock();
1359 		}
1360 		m_freem(m);
1361 		return;
1362 	}
1363 
1364 	if (!redispatch)
1365 		netisr_run(isr, m);
1366 	else
1367 		netisr_dispatch(isr, m);
1368 }
1369 
1370 /*
1371  * First we perform any link layer operations, then continue to the
1372  * upper layers with ether_demux_oncpu().
1373  */
1374 void
1375 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1376 {
1377 	if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1378 		/*
1379 		 * Receiving interface's flags are changed, when this
1380 		 * packet is waiting for processing; discard it.
1381 		 */
1382 		m_freem(m);
1383 		return;
1384 	}
1385 
1386 	/*
1387 	 * Tap the packet off here for a bridge.  bridge_input()
1388 	 * will return NULL if it has consumed the packet, otherwise
1389 	 * it gets processed as normal.  Note that bridge_input()
1390 	 * will always return the original packet if we need to
1391 	 * process it locally.
1392 	 */
1393 	if (ifp->if_bridge) {
1394 		KASSERT(bridge_input_p != NULL,
1395 			("%s: if_bridge not loaded!", __func__));
1396 
1397 		if(m->m_flags & M_ETHER_BRIDGED) {
1398 			m->m_flags &= ~M_ETHER_BRIDGED;
1399 		} else {
1400 			m = bridge_input_p(ifp, m);
1401 			if (m == NULL)
1402 				return;
1403 
1404 			KASSERT(ifp == m->m_pkthdr.rcvif,
1405 				("bridge_input_p changed rcvif\n"));
1406 		}
1407 	}
1408 
1409 	/* Handle ng_ether(4) processing, if any */
1410 	if (ng_ether_input_p != NULL) {
1411 		/*
1412 		 * Hold BGL and recheck ng_ether_input_p
1413 		 */
1414 		get_mplock();
1415 		if (ng_ether_input_p != NULL)
1416 			ng_ether_input_p(ifp, &m);
1417 		rel_mplock();
1418 
1419 		if (m == NULL)
1420 			return;
1421 	}
1422 
1423 	/* Continue with upper layer processing */
1424 	ether_demux_oncpu(ifp, m);
1425 }
1426 
1427 /*
1428  * Perform certain functions of ether_input_chain():
1429  * - Test IFF_UP
1430  * - Update statistics
1431  * - Run bpf(4) tap if requested
1432  * Then pass the packet to ether_input_oncpu().
1433  *
1434  * This function should be used by pseudo interface (e.g. vlan(4)),
1435  * when it tries to claim that the packet is received by it.
1436  */
1437 void
1438 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int run_bpf)
1439 {
1440 	/* Discard packet if interface is not up */
1441 	if (!(ifp->if_flags & IFF_UP)) {
1442 		m_freem(m);
1443 		return;
1444 	}
1445 
1446 	/* Change receiving interface */
1447 	m->m_pkthdr.rcvif = ifp;
1448 
1449 	/* Update statistics */
1450 	ifp->if_ipackets++;
1451 	ifp->if_ibytes += m->m_pkthdr.len;
1452 	if (m->m_flags & (M_MCAST | M_BCAST))
1453 		ifp->if_imcasts++;
1454 
1455 	if (run_bpf)
1456 		BPF_MTAP(ifp, m);
1457 
1458 	ether_input_oncpu(ifp, m);
1459 }
1460 
1461 static __inline boolean_t
1462 ether_vlancheck(struct mbuf **m0)
1463 {
1464 	struct mbuf *m = *m0;
1465 	struct ether_header *eh;
1466 	uint16_t ether_type;
1467 
1468 	eh = mtod(m, struct ether_header *);
1469 	ether_type = ntohs(eh->ether_type);
1470 
1471 	if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1472 		/*
1473 		 * Extract vlan tag if hardware does not do it for us
1474 		 */
1475 		vlan_ether_decap(&m);
1476 		if (m == NULL)
1477 			goto failed;
1478 
1479 		eh = mtod(m, struct ether_header *);
1480 		ether_type = ntohs(eh->ether_type);
1481 	}
1482 
1483 	if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1484 		/*
1485 		 * To prevent possible dangerous recursion,
1486 		 * we don't do vlan-in-vlan
1487 		 */
1488 		m->m_pkthdr.rcvif->if_noproto++;
1489 		goto failed;
1490 	}
1491 	KKASSERT(ether_type != ETHERTYPE_VLAN);
1492 
1493 	m->m_flags |= M_ETHER_VLANCHECKED;
1494 	*m0 = m;
1495 	return TRUE;
1496 failed:
1497 	if (m != NULL)
1498 		m_freem(m);
1499 	*m0 = NULL;
1500 	return FALSE;
1501 }
1502 
1503 static void
1504 ether_input_handler(struct netmsg *nmsg)
1505 {
1506 	struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1507 	struct ether_header *eh;
1508 	struct ifnet *ifp;
1509 	struct mbuf *m;
1510 
1511 	m = nmp->nm_packet;
1512 	M_ASSERTPKTHDR(m);
1513 	ifp = m->m_pkthdr.rcvif;
1514 
1515 	eh = mtod(m, struct ether_header *);
1516 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1517 		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1518 			 ifp->if_addrlen) == 0)
1519 			m->m_flags |= M_BCAST;
1520 		else
1521 			m->m_flags |= M_MCAST;
1522 		ifp->if_imcasts++;
1523 	}
1524 
1525 	if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1526 		if (!ether_vlancheck(&m)) {
1527 			KKASSERT(m == NULL);
1528 			return;
1529 		}
1530 	}
1531 
1532 	ether_input_oncpu(ifp, m);
1533 }
1534 
1535 static __inline void
1536 ether_init_netpacket(int num, struct mbuf *m)
1537 {
1538 	struct netmsg_packet *pmsg;
1539 
1540 	pmsg = &m->m_hdr.mh_netmsg;
1541 	netmsg_init(&pmsg->nm_netmsg, NULL, &netisr_apanic_rport,
1542 		    MSGF_MPSAFE, ether_input_handler);
1543 	pmsg->nm_packet = m;
1544 	pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1545 }
1546 
1547 static __inline struct lwkt_port *
1548 ether_mport(int num, struct mbuf **m)
1549 {
1550 	if (num == NETISR_MAX) {
1551 		/*
1552 		 * All packets whose target msgports can't be
1553 		 * determined here are dispatched to netisr0,
1554 		 * where further dispatching may happen.
1555 		 */
1556 		return cpu_portfn(0);
1557 	}
1558 	return netisr_find_port(num, m);
1559 }
1560 
1561 /*
1562  * Send the packet to the target msgport or
1563  * queue it into 'chain'.
1564  */
1565 static void
1566 ether_dispatch(int isr, struct lwkt_port *port, struct mbuf *m,
1567 	       struct mbuf_chain *chain)
1568 {
1569 	ether_init_netpacket(isr, m);
1570 
1571 	if (chain != NULL) {
1572 		struct mbuf_chain *c;
1573 		int cpuid;
1574 
1575 		m->m_pkthdr.header = port; /* XXX */
1576 		cpuid = port->mpu_td->td_gd->gd_cpuid;
1577 
1578 		c = &chain[cpuid];
1579 		if (c->mc_head == NULL) {
1580 			c->mc_head = c->mc_tail = m;
1581 		} else {
1582 			c->mc_tail->m_nextpkt = m;
1583 			c->mc_tail = m;
1584 		}
1585 		m->m_nextpkt = NULL;
1586 	} else {
1587 		lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1588 	}
1589 }
1590 
1591 /*
1592  * Process a received Ethernet packet.
1593  *
1594  * The ethernet header is assumed to be in the mbuf so the caller
1595  * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1596  * bytes in the first mbuf.
1597  *
1598  * We first try to find the target msgport for this ether frame, if
1599  * there is no target msgport for it, this ether frame is discarded,
1600  * else we do following processing according to whether 'chain' is
1601  * NULL or not:
1602  * - If 'chain' is NULL, this ether frame is sent to the target msgport
1603  *   immediately.  This situation happens when ether_input_chain is
1604  *   accessed through ifnet.if_input.
1605  * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1606  *   bucket indexed by the target msgport's cpuid and the target msgport
1607  *   is saved in mbuf's m_pkthdr.m_head.  Caller of ether_input_chain
1608  *   must initialize 'chain' by calling ether_input_chain_init().
1609  *   ether_input_dispatch must be called later to send ether frames
1610  *   queued on 'chain' to their target msgport.
1611  */
1612 void
1613 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1614 		  struct mbuf_chain *chain)
1615 {
1616 	struct ether_header *eh, *save_eh, save_eh0;
1617 	struct lwkt_port *port;
1618 	uint16_t ether_type;
1619 	int isr;
1620 
1621 	M_ASSERTPKTHDR(m);
1622 
1623 	/* Discard packet if interface is not up */
1624 	if (!(ifp->if_flags & IFF_UP)) {
1625 		m_freem(m);
1626 		return;
1627 	}
1628 
1629 	if (m->m_len < sizeof(struct ether_header)) {
1630 		/* XXX error in the caller. */
1631 		m_freem(m);
1632 		return;
1633 	}
1634 
1635 	m->m_pkthdr.rcvif = ifp;
1636 
1637 	logether(chain_beg, ifp);
1638 
1639 	ETHER_BPF_MTAP(ifp, m);
1640 
1641 	ifp->if_ibytes += m->m_pkthdr.len;
1642 
1643 	if (ifp->if_flags & IFF_MONITOR) {
1644 		eh = mtod(m, struct ether_header *);
1645 		if (ETHER_IS_MULTICAST(eh->ether_dhost))
1646 			ifp->if_imcasts++;
1647 
1648 		/*
1649 		 * Interface marked for monitoring; discard packet.
1650 		 */
1651 		m_freem(m);
1652 
1653 		logether(chain_end, ifp);
1654 		return;
1655 	}
1656 
1657 	if (pi != NULL && (m->m_flags & M_HASH)) {
1658 #ifdef RSS_DEBUG
1659 		ether_pktinfo_try++;
1660 #endif
1661 		/* Try finding the port using the packet info */
1662 		port = netisr_find_pktinfo_port(pi, m);
1663 		if (port != NULL) {
1664 #ifdef RSS_DEBUG
1665 			ether_pktinfo_hit++;
1666 #endif
1667 			ether_dispatch(pi->pi_netisr, port, m, chain);
1668 
1669 			logether(chain_end, ifp);
1670 			return;
1671 		}
1672 
1673 		/*
1674 		 * The packet info does not contain enough
1675 		 * information, we will have to check the
1676 		 * packet content.
1677 		 */
1678 	}
1679 #ifdef RSS_DEBUG
1680 	else if (ifp->if_capenable & IFCAP_RSS) {
1681 		if (pi == NULL)
1682 			ether_rss_nopi++;
1683 		else
1684 			ether_rss_nohash++;
1685 	}
1686 #endif
1687 
1688 	/*
1689 	 * Packet hash will be recalculated by software,
1690 	 * so clear the M_HASH flag set by the driver;
1691 	 * the hash value calculated by the hardware may
1692 	 * not be exactly what we want.
1693 	 */
1694 	m->m_flags &= ~M_HASH;
1695 
1696 	if (!ether_vlancheck(&m)) {
1697 		KKASSERT(m == NULL);
1698 		logether(chain_end, ifp);
1699 		return;
1700 	}
1701 	eh = mtod(m, struct ether_header *);
1702 	ether_type = ntohs(eh->ether_type);
1703 
1704 	/*
1705 	 * Map ether type to netisr id.
1706 	 */
1707 	switch (ether_type) {
1708 #ifdef INET
1709 	case ETHERTYPE_IP:
1710 		isr = NETISR_IP;
1711 		break;
1712 
1713 	case ETHERTYPE_ARP:
1714 		isr = NETISR_ARP;
1715 		break;
1716 #endif
1717 
1718 #ifdef INET6
1719 	case ETHERTYPE_IPV6:
1720 		isr = NETISR_IPV6;
1721 		break;
1722 #endif
1723 
1724 #ifdef IPX
1725 	case ETHERTYPE_IPX:
1726 		isr = NETISR_IPX;
1727 		break;
1728 #endif
1729 
1730 #ifdef NS
1731 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1732 		isr = NETISR_NS;
1733 		break;
1734 #endif
1735 
1736 #ifdef NETATALK
1737 	case ETHERTYPE_AT:
1738 		isr = NETISR_ATALK1;
1739 		break;
1740 	case ETHERTYPE_AARP:
1741 		isr = NETISR_AARP;
1742 		break;
1743 #endif
1744 
1745 #ifdef MPLS
1746 	case ETHERTYPE_MPLS:
1747 	case ETHERTYPE_MPLS_MCAST:
1748 		m->m_flags |= M_MPLSLABELED;
1749 		isr = NETISR_MPLS;
1750 		break;
1751 #endif
1752 
1753 	default:
1754 		/*
1755 		 * NETISR_MAX is an invalid value; it is chosen to let
1756 		 * ether_mport() know that we are not able to decide
1757 		 * this packet's msgport here.
1758 		 */
1759 		isr = NETISR_MAX;
1760 		break;
1761 	}
1762 
1763 	/*
1764 	 * If the packet is in contiguous memory, following
1765 	 * m_adj() could ensure that the hidden ether header
1766 	 * will not be destroyed, else we will have to save
1767 	 * the ether header for the later restoration.
1768 	 */
1769 	if (m->m_pkthdr.len != m->m_len) {
1770 		save_eh0 = *eh;
1771 		save_eh = &save_eh0;
1772 	} else {
1773 		save_eh = NULL;
1774 	}
1775 
1776 	/*
1777 	 * Temporarily remove ether header; ether_mport()
1778 	 * expects a packet without ether header.
1779 	 */
1780 	m_adj(m, sizeof(struct ether_header));
1781 
1782 	/*
1783 	 * Find the packet's target msgport.
1784 	 */
1785 	port = ether_mport(isr, &m);
1786 	if (port == NULL) {
1787 		KKASSERT(m == NULL);
1788 		logether(chain_end, ifp);
1789 		return;
1790 	}
1791 
1792 	/*
1793 	 * Restore ether header.
1794 	 */
1795 	if (save_eh != NULL) {
1796 		ether_restore_header(&m, eh, save_eh);
1797 		if (m == NULL) {
1798 			logether(chain_end, ifp);
1799 			return;
1800 		}
1801 	} else {
1802 		m->m_data -= ETHER_HDR_LEN;
1803 		m->m_len += ETHER_HDR_LEN;
1804 		m->m_pkthdr.len += ETHER_HDR_LEN;
1805 	}
1806 
1807 	ether_dispatch(isr, port, m, chain);
1808 
1809 	logether(chain_end, ifp);
1810 }
1811 
1812 MODULE_VERSION(ether, 1);
1813