xref: /freebsd/sys/net/if_ethersubr.c (revision 068ebf32749921b35beda31c860e2e084da2f7d2)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_netgraph.h"
36 #include "opt_mbuf_profiling.h"
37 #include "opt_rss.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mbuf.h>
46 #include <sys/random.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/uuid.h>
51 
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/if_bridgevar.h>
63 #include <net/if_vlan_var.h>
64 #include <net/if_llatbl.h>
65 #include <net/pfil.h>
66 #include <net/vnet.h>
67 
68 #include <netpfil/pf/pf_mtag.h>
69 
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/in_rss.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/ip_var.h>
77 #endif
78 #ifdef INET6
79 #include <netinet6/nd6.h>
80 #endif
81 
82 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
83 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
84 		const struct sockaddr *dst, short *tp, int *hlen);
85 
86 #include <security/mac/mac_framework.h>
87 
88 #ifdef CTASSERT
89 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
90 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
91 #endif
92 
93 VNET_DEFINE(struct pfil_head, link_pfil_hook);	/* Packet filter hooks */
94 
95 /* netgraph node hooks for ng_ether(4) */
96 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
97 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
98 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
99 void	(*ng_ether_attach_p)(struct ifnet *ifp);
100 void	(*ng_ether_detach_p)(struct ifnet *ifp);
101 
102 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
103 
104 /* if_bridge(4) support */
105 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
106 int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
107 		struct sockaddr *, struct rtentry *);
108 void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
109 
110 /* if_lagg(4) support */
111 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
112 
113 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
114 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
115 
116 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
117 		struct sockaddr *);
118 #ifdef VIMAGE
119 static	void ether_reassign(struct ifnet *, struct vnet *, char *);
120 #endif
121 
122 /* XXX: should be in an arp support file, not here */
123 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
124 
125 #define	ETHER_IS_BROADCAST(addr) \
126 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
127 
128 #define senderr(e) do { error = (e); goto bad;} while (0)
129 
130 static void
131 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
132 {
133 	int csum_flags = 0;
134 
135 	if (src->m_pkthdr.csum_flags & CSUM_IP)
136 		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
137 	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
138 		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
139 	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
140 		csum_flags |= CSUM_SCTP_VALID;
141 	dst->m_pkthdr.csum_flags |= csum_flags;
142 	if (csum_flags & CSUM_DATA_VALID)
143 		dst->m_pkthdr.csum_data = 0xffff;
144 }
145 
146 /*
147  * Ethernet output routine.
148  * Encapsulate a packet of type family for the local net.
149  * Use trailer local net encapsulation if enough data in first
150  * packet leaves a multiple of 512 bytes of data in remainder.
151  */
152 int
153 ether_output(struct ifnet *ifp, struct mbuf *m,
154 	const struct sockaddr *dst, struct route *ro)
155 {
156 	short type;
157 	int error = 0, hdrcmplt = 0;
158 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
159 	struct llentry *lle = NULL;
160 	struct rtentry *rt0 = NULL;
161 	struct ether_header *eh;
162 	struct pf_mtag *t;
163 	int loop_copy = 1;
164 	int hlen;	/* link layer header length */
165 
166 	if (ro != NULL) {
167 		if (!(m->m_flags & (M_BCAST | M_MCAST)))
168 			lle = ro->ro_lle;
169 		rt0 = ro->ro_rt;
170 	}
171 #ifdef MAC
172 	error = mac_ifnet_check_transmit(ifp, m);
173 	if (error)
174 		senderr(error);
175 #endif
176 
177 	M_PROFILE(m);
178 	if (ifp->if_flags & IFF_MONITOR)
179 		senderr(ENETDOWN);
180 	if (!((ifp->if_flags & IFF_UP) &&
181 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
182 		senderr(ENETDOWN);
183 
184 	hlen = ETHER_HDR_LEN;
185 	switch (dst->sa_family) {
186 #ifdef INET
187 	case AF_INET:
188 		if (lle != NULL && (lle->la_flags & LLE_VALID))
189 			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
190 		else
191 			error = arpresolve(ifp, rt0, m, dst, edst, &lle);
192 		if (error)
193 			return (error == EWOULDBLOCK ? 0 : error);
194 		type = htons(ETHERTYPE_IP);
195 		break;
196 	case AF_ARP:
197 	{
198 		struct arphdr *ah;
199 		ah = mtod(m, struct arphdr *);
200 		ah->ar_hrd = htons(ARPHRD_ETHER);
201 
202 		loop_copy = 0; /* if this is for us, don't do it */
203 
204 		switch(ntohs(ah->ar_op)) {
205 		case ARPOP_REVREQUEST:
206 		case ARPOP_REVREPLY:
207 			type = htons(ETHERTYPE_REVARP);
208 			break;
209 		case ARPOP_REQUEST:
210 		case ARPOP_REPLY:
211 		default:
212 			type = htons(ETHERTYPE_ARP);
213 			break;
214 		}
215 
216 		if (m->m_flags & M_BCAST)
217 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
218 		else
219 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
220 
221 	}
222 	break;
223 #endif
224 #ifdef INET6
225 	case AF_INET6:
226 		if (lle != NULL && (lle->la_flags & LLE_VALID))
227 			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
228 		else
229 			error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
230 		if (error)
231 			return error;
232 		type = htons(ETHERTYPE_IPV6);
233 		break;
234 #endif
235 	case pseudo_AF_HDRCMPLT:
236 	    {
237 		const struct ether_header *eh;
238 
239 		hdrcmplt = 1;
240 		eh = (const struct ether_header *)dst->sa_data;
241 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
242 		/* FALLTHROUGH */
243 
244 	case AF_UNSPEC:
245 		loop_copy = 0; /* if this is for us, don't do it */
246 		eh = (const struct ether_header *)dst->sa_data;
247 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
248 		type = eh->ether_type;
249 		break;
250             }
251 	default:
252 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
253 		senderr(EAFNOSUPPORT);
254 	}
255 
256 	if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
257 		update_mbuf_csumflags(m, m);
258 		return (if_simloop(ifp, m, dst->sa_family, 0));
259 	}
260 
261 	/*
262 	 * Add local net header.  If no space in first mbuf,
263 	 * allocate another.
264 	 */
265 	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
266 	if (m == NULL)
267 		senderr(ENOBUFS);
268 	eh = mtod(m, struct ether_header *);
269 	(void)memcpy(&eh->ether_type, &type,
270 		sizeof(eh->ether_type));
271 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
272 	if (hdrcmplt)
273 		(void)memcpy(eh->ether_shost, esrc,
274 			sizeof(eh->ether_shost));
275 	else
276 		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
277 			sizeof(eh->ether_shost));
278 
279 	/*
280 	 * If a simplex interface, and the packet is being sent to our
281 	 * Ethernet address or a broadcast address, loopback a copy.
282 	 * XXX To make a simplex device behave exactly like a duplex
283 	 * device, we should copy in the case of sending to our own
284 	 * ethernet address (thus letting the original actually appear
285 	 * on the wire). However, we don't do that here for security
286 	 * reasons and compatibility with the original behavior.
287 	 */
288 	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
289 	    ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
290 		if (m->m_flags & M_BCAST) {
291 			struct mbuf *n;
292 
293 			/*
294 			 * Because if_simloop() modifies the packet, we need a
295 			 * writable copy through m_dup() instead of a readonly
296 			 * one as m_copy[m] would give us. The alternative would
297 			 * be to modify if_simloop() to handle the readonly mbuf,
298 			 * but performancewise it is mostly equivalent (trading
299 			 * extra data copying vs. extra locking).
300 			 *
301 			 * XXX This is a local workaround.  A number of less
302 			 * often used kernel parts suffer from the same bug.
303 			 * See PR kern/105943 for a proposed general solution.
304 			 */
305 			if ((n = m_dup(m, M_NOWAIT)) != NULL) {
306 				update_mbuf_csumflags(m, n);
307 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
308 			} else
309 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
310 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
311 				ETHER_ADDR_LEN) == 0) {
312 			update_mbuf_csumflags(m, m);
313 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
314 			return (0);	/* XXX */
315 		}
316 	}
317 
318        /*
319 	* Bridges require special output handling.
320 	*/
321 	if (ifp->if_bridge) {
322 		BRIDGE_OUTPUT(ifp, m, error);
323 		return (error);
324 	}
325 
326 #if defined(INET) || defined(INET6)
327 	if (ifp->if_carp &&
328 	    (error = (*carp_output_p)(ifp, m, dst)))
329 		goto bad;
330 #endif
331 
332 	/* Handle ng_ether(4) processing, if any */
333 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
334 		KASSERT(ng_ether_output_p != NULL,
335 		    ("ng_ether_output_p is NULL"));
336 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
337 bad:			if (m != NULL)
338 				m_freem(m);
339 			return (error);
340 		}
341 		if (m == NULL)
342 			return (0);
343 	}
344 
345 	/* Continue with link-layer output */
346 	return ether_output_frame(ifp, m);
347 }
348 
349 /*
350  * Ethernet link layer output routine to send a raw frame to the device.
351  *
352  * This assumes that the 14 byte Ethernet header is present and contiguous
353  * in the first mbuf (if BRIDGE'ing).
354  */
355 int
356 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
357 {
358 	int i;
359 
360 	if (PFIL_HOOKED(&V_link_pfil_hook)) {
361 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
362 
363 		if (i != 0)
364 			return (EACCES);
365 
366 		if (m == NULL)
367 			return (0);
368 	}
369 
370 	/*
371 	 * Queue message on interface, update output statistics if
372 	 * successful, and start output if interface not yet active.
373 	 */
374 	return ((ifp->if_transmit)(ifp, m));
375 }
376 
377 #if defined(INET) || defined(INET6)
378 #endif
379 
380 /*
381  * Process a received Ethernet packet; the packet is in the
382  * mbuf chain m with the ethernet header at the front.
383  */
384 static void
385 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
386 {
387 	struct ether_header *eh;
388 	u_short etype;
389 
390 	if ((ifp->if_flags & IFF_UP) == 0) {
391 		m_freem(m);
392 		return;
393 	}
394 #ifdef DIAGNOSTIC
395 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
396 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
397 		m_freem(m);
398 		return;
399 	}
400 #endif
401 	/*
402 	 * Do consistency checks to verify assumptions
403 	 * made by code past this point.
404 	 */
405 	if ((m->m_flags & M_PKTHDR) == 0) {
406 		if_printf(ifp, "discard frame w/o packet header\n");
407 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
408 		m_freem(m);
409 		return;
410 	}
411 	if (m->m_len < ETHER_HDR_LEN) {
412 		/* XXX maybe should pullup? */
413 		if_printf(ifp, "discard frame w/o leading ethernet "
414 				"header (len %u pkt len %u)\n",
415 				m->m_len, m->m_pkthdr.len);
416 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
417 		m_freem(m);
418 		return;
419 	}
420 	eh = mtod(m, struct ether_header *);
421 	etype = ntohs(eh->ether_type);
422 	if (m->m_pkthdr.rcvif == NULL) {
423 		if_printf(ifp, "discard frame w/o interface pointer\n");
424 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
425 		m_freem(m);
426 		return;
427 	}
428 #ifdef DIAGNOSTIC
429 	if (m->m_pkthdr.rcvif != ifp) {
430 		if_printf(ifp, "Warning, frame marked as received on %s\n",
431 			m->m_pkthdr.rcvif->if_xname);
432 	}
433 #endif
434 
435 	CURVNET_SET_QUIET(ifp->if_vnet);
436 
437 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
438 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
439 			m->m_flags |= M_BCAST;
440 		else
441 			m->m_flags |= M_MCAST;
442 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
443 	}
444 
445 #ifdef MAC
446 	/*
447 	 * Tag the mbuf with an appropriate MAC label before any other
448 	 * consumers can get to it.
449 	 */
450 	mac_ifnet_create_mbuf(ifp, m);
451 #endif
452 
453 	/*
454 	 * Give bpf a chance at the packet.
455 	 */
456 	ETHER_BPF_MTAP(ifp, m);
457 
458 	/*
459 	 * If the CRC is still on the packet, trim it off. We do this once
460 	 * and once only in case we are re-entered. Nothing else on the
461 	 * Ethernet receive path expects to see the FCS.
462 	 */
463 	if (m->m_flags & M_HASFCS) {
464 		m_adj(m, -ETHER_CRC_LEN);
465 		m->m_flags &= ~M_HASFCS;
466 	}
467 
468 	if (!(ifp->if_capenable & IFCAP_HWSTATS))
469 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
470 
471 	/* Allow monitor mode to claim this frame, after stats are updated. */
472 	if (ifp->if_flags & IFF_MONITOR) {
473 		m_freem(m);
474 		CURVNET_RESTORE();
475 		return;
476 	}
477 
478 	/* Handle input from a lagg(4) port */
479 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
480 		KASSERT(lagg_input_p != NULL,
481 		    ("%s: if_lagg not loaded!", __func__));
482 		m = (*lagg_input_p)(ifp, m);
483 		if (m != NULL)
484 			ifp = m->m_pkthdr.rcvif;
485 		else {
486 			CURVNET_RESTORE();
487 			return;
488 		}
489 	}
490 
491 	/*
492 	 * If the hardware did not process an 802.1Q tag, do this now,
493 	 * to allow 802.1P priority frames to be passed to the main input
494 	 * path correctly.
495 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
496 	 */
497 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
498 		struct ether_vlan_header *evl;
499 
500 		if (m->m_len < sizeof(*evl) &&
501 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
502 #ifdef DIAGNOSTIC
503 			if_printf(ifp, "cannot pullup VLAN header\n");
504 #endif
505 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
506 			m_freem(m);
507 			CURVNET_RESTORE();
508 			return;
509 		}
510 
511 		evl = mtod(m, struct ether_vlan_header *);
512 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
513 		m->m_flags |= M_VLANTAG;
514 
515 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
516 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
517 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
518 		eh = mtod(m, struct ether_header *);
519 	}
520 
521 	M_SETFIB(m, ifp->if_fib);
522 
523 	/* Allow ng_ether(4) to claim this frame. */
524 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
525 		KASSERT(ng_ether_input_p != NULL,
526 		    ("%s: ng_ether_input_p is NULL", __func__));
527 		m->m_flags &= ~M_PROMISC;
528 		(*ng_ether_input_p)(ifp, &m);
529 		if (m == NULL) {
530 			CURVNET_RESTORE();
531 			return;
532 		}
533 		eh = mtod(m, struct ether_header *);
534 	}
535 
536 	/*
537 	 * Allow if_bridge(4) to claim this frame.
538 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
539 	 * and the frame should be delivered locally.
540 	 */
541 	if (ifp->if_bridge != NULL) {
542 		m->m_flags &= ~M_PROMISC;
543 		BRIDGE_INPUT(ifp, m);
544 		if (m == NULL) {
545 			CURVNET_RESTORE();
546 			return;
547 		}
548 		eh = mtod(m, struct ether_header *);
549 	}
550 
551 #if defined(INET) || defined(INET6)
552 	/*
553 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
554 	 * mbuf flows up to Layer 3.
555 	 * FreeBSD's implementation of carp(4) uses the inprotosw
556 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
557 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
558 	 * is outside the scope of the M_PROMISC test below.
559 	 * TODO: Maintain a hash table of ethernet addresses other than
560 	 * ether_dhost which may be active on this ifp.
561 	 */
562 	if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
563 		m->m_flags &= ~M_PROMISC;
564 	} else
565 #endif
566 	{
567 		/*
568 		 * If the frame received was not for our MAC address, set the
569 		 * M_PROMISC flag on the mbuf chain. The frame may need to
570 		 * be seen by the rest of the Ethernet input path in case of
571 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
572 		 * seen by upper protocol layers.
573 		 */
574 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
575 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
576 			m->m_flags |= M_PROMISC;
577 	}
578 
579 	random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
580 
581 	ether_demux(ifp, m);
582 	CURVNET_RESTORE();
583 }
584 
585 /*
586  * Ethernet input dispatch; by default, direct dispatch here regardless of
587  * global configuration.  However, if RSS is enabled, hook up RSS affinity
588  * so that when deferred or hybrid dispatch is enabled, we can redistribute
589  * load based on RSS.
590  *
591  * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
592  * not it had already done work distribution via multi-queue.  Then we could
593  * direct dispatch in the event load balancing was already complete and
594  * handle the case of interfaces with different capabilities better.
595  *
596  * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
597  * at multiple layers?
598  *
599  * XXXRW: For now, enable all this only if RSS is compiled in, although it
600  * works fine without RSS.  Need to characterise the performance overhead
601  * of the detour through the netisr code in the event the result is always
602  * direct dispatch.
603  */
604 static void
605 ether_nh_input(struct mbuf *m)
606 {
607 
608 	ether_input_internal(m->m_pkthdr.rcvif, m);
609 }
610 
611 static struct netisr_handler	ether_nh = {
612 	.nh_name = "ether",
613 	.nh_handler = ether_nh_input,
614 	.nh_proto = NETISR_ETHER,
615 #ifdef RSS
616 	.nh_policy = NETISR_POLICY_CPU,
617 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
618 	.nh_m2cpuid = rss_m2cpuid,
619 #else
620 	.nh_policy = NETISR_POLICY_SOURCE,
621 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
622 #endif
623 };
624 
625 static void
626 ether_init(__unused void *arg)
627 {
628 
629 	netisr_register(&ether_nh);
630 }
631 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
632 
633 static void
634 vnet_ether_init(__unused void *arg)
635 {
636 	int i;
637 
638 	/* Initialize packet filter hooks. */
639 	V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
640 	V_link_pfil_hook.ph_af = AF_LINK;
641 	if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
642 		printf("%s: WARNING: unable to register pfil link hook, "
643 			"error %d\n", __func__, i);
644 }
645 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
646     vnet_ether_init, NULL);
647 
648 static void
649 vnet_ether_destroy(__unused void *arg)
650 {
651 	int i;
652 
653 	if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
654 		printf("%s: WARNING: unable to unregister pfil link hook, "
655 			"error %d\n", __func__, i);
656 }
657 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
658     vnet_ether_destroy, NULL);
659 
660 
661 
662 static void
663 ether_input(struct ifnet *ifp, struct mbuf *m)
664 {
665 
666 	struct mbuf *mn;
667 
668 	/*
669 	 * The drivers are allowed to pass in a chain of packets linked with
670 	 * m_nextpkt. We split them up into separate packets here and pass
671 	 * them up. This allows the drivers to amortize the receive lock.
672 	 */
673 	while (m) {
674 		mn = m->m_nextpkt;
675 		m->m_nextpkt = NULL;
676 
677 		/*
678 		 * We will rely on rcvif being set properly in the deferred context,
679 		 * so assert it is correct here.
680 		 */
681 		KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
682 		netisr_dispatch(NETISR_ETHER, m);
683 		m = mn;
684 	}
685 }
686 
687 /*
688  * Upper layer processing for a received Ethernet packet.
689  */
690 void
691 ether_demux(struct ifnet *ifp, struct mbuf *m)
692 {
693 	struct ether_header *eh;
694 	int i, isr;
695 	u_short ether_type;
696 
697 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
698 
699 	/* Do not grab PROMISC frames in case we are re-entered. */
700 	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
701 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
702 
703 		if (i != 0 || m == NULL)
704 			return;
705 	}
706 
707 	eh = mtod(m, struct ether_header *);
708 	ether_type = ntohs(eh->ether_type);
709 
710 	/*
711 	 * If this frame has a VLAN tag other than 0, call vlan_input()
712 	 * if its module is loaded. Otherwise, drop.
713 	 */
714 	if ((m->m_flags & M_VLANTAG) &&
715 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
716 		if (ifp->if_vlantrunk == NULL) {
717 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
718 			m_freem(m);
719 			return;
720 		}
721 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
722 		    __func__));
723 		/* Clear before possibly re-entering ether_input(). */
724 		m->m_flags &= ~M_PROMISC;
725 		(*vlan_input_p)(ifp, m);
726 		return;
727 	}
728 
729 	/*
730 	 * Pass promiscuously received frames to the upper layer if the user
731 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
732 	 */
733 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
734 		m_freem(m);
735 		return;
736 	}
737 
738 	/*
739 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
740 	 * Strip off Ethernet header.
741 	 */
742 	m->m_flags &= ~M_VLANTAG;
743 	m_clrprotoflags(m);
744 	m_adj(m, ETHER_HDR_LEN);
745 
746 	/*
747 	 * Dispatch frame to upper layer.
748 	 */
749 	switch (ether_type) {
750 #ifdef INET
751 	case ETHERTYPE_IP:
752 		if ((m = ip_fastforward(m)) == NULL)
753 			return;
754 		isr = NETISR_IP;
755 		break;
756 
757 	case ETHERTYPE_ARP:
758 		if (ifp->if_flags & IFF_NOARP) {
759 			/* Discard packet if ARP is disabled on interface */
760 			m_freem(m);
761 			return;
762 		}
763 		isr = NETISR_ARP;
764 		break;
765 #endif
766 #ifdef INET6
767 	case ETHERTYPE_IPV6:
768 		isr = NETISR_IPV6;
769 		break;
770 #endif
771 	default:
772 		goto discard;
773 	}
774 	netisr_dispatch(isr, m);
775 	return;
776 
777 discard:
778 	/*
779 	 * Packet is to be discarded.  If netgraph is present,
780 	 * hand the packet to it for last chance processing;
781 	 * otherwise dispose of it.
782 	 */
783 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
784 		KASSERT(ng_ether_input_orphan_p != NULL,
785 		    ("ng_ether_input_orphan_p is NULL"));
786 		/*
787 		 * Put back the ethernet header so netgraph has a
788 		 * consistent view of inbound packets.
789 		 */
790 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
791 		(*ng_ether_input_orphan_p)(ifp, m);
792 		return;
793 	}
794 	m_freem(m);
795 }
796 
797 /*
798  * Convert Ethernet address to printable (loggable) representation.
799  * This routine is for compatibility; it's better to just use
800  *
801  *	printf("%6D", <pointer to address>, ":");
802  *
803  * since there's no static buffer involved.
804  */
805 char *
806 ether_sprintf(const u_char *ap)
807 {
808 	static char etherbuf[18];
809 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
810 	return (etherbuf);
811 }
812 
813 /*
814  * Perform common duties while attaching to interface list
815  */
816 void
817 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
818 {
819 	int i;
820 	struct ifaddr *ifa;
821 	struct sockaddr_dl *sdl;
822 
823 	ifp->if_addrlen = ETHER_ADDR_LEN;
824 	ifp->if_hdrlen = ETHER_HDR_LEN;
825 	if_attach(ifp);
826 	ifp->if_mtu = ETHERMTU;
827 	ifp->if_output = ether_output;
828 	ifp->if_input = ether_input;
829 	ifp->if_resolvemulti = ether_resolvemulti;
830 #ifdef VIMAGE
831 	ifp->if_reassign = ether_reassign;
832 #endif
833 	if (ifp->if_baudrate == 0)
834 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
835 	ifp->if_broadcastaddr = etherbroadcastaddr;
836 
837 	ifa = ifp->if_addr;
838 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
839 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
840 	sdl->sdl_type = IFT_ETHER;
841 	sdl->sdl_alen = ifp->if_addrlen;
842 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
843 
844 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
845 	if (ng_ether_attach_p != NULL)
846 		(*ng_ether_attach_p)(ifp);
847 
848 	/* Announce Ethernet MAC address if non-zero. */
849 	for (i = 0; i < ifp->if_addrlen; i++)
850 		if (lla[i] != 0)
851 			break;
852 	if (i != ifp->if_addrlen)
853 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
854 
855 	uuid_ether_add(LLADDR(sdl));
856 }
857 
858 /*
859  * Perform common duties while detaching an Ethernet interface
860  */
861 void
862 ether_ifdetach(struct ifnet *ifp)
863 {
864 	struct sockaddr_dl *sdl;
865 
866 	sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
867 	uuid_ether_del(LLADDR(sdl));
868 
869 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
870 		KASSERT(ng_ether_detach_p != NULL,
871 		    ("ng_ether_detach_p is NULL"));
872 		(*ng_ether_detach_p)(ifp);
873 	}
874 
875 	bpfdetach(ifp);
876 	if_detach(ifp);
877 }
878 
879 #ifdef VIMAGE
880 void
881 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
882 {
883 
884 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
885 		KASSERT(ng_ether_detach_p != NULL,
886 		    ("ng_ether_detach_p is NULL"));
887 		(*ng_ether_detach_p)(ifp);
888 	}
889 
890 	if (ng_ether_attach_p != NULL) {
891 		CURVNET_SET_QUIET(new_vnet);
892 		(*ng_ether_attach_p)(ifp);
893 		CURVNET_RESTORE();
894 	}
895 }
896 #endif
897 
898 SYSCTL_DECL(_net_link);
899 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
900 
901 #if 0
902 /*
903  * This is for reference.  We have a table-driven version
904  * of the little-endian crc32 generator, which is faster
905  * than the double-loop.
906  */
907 uint32_t
908 ether_crc32_le(const uint8_t *buf, size_t len)
909 {
910 	size_t i;
911 	uint32_t crc;
912 	int bit;
913 	uint8_t data;
914 
915 	crc = 0xffffffff;	/* initial value */
916 
917 	for (i = 0; i < len; i++) {
918 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
919 			carry = (crc ^ data) & 1;
920 			crc >>= 1;
921 			if (carry)
922 				crc = (crc ^ ETHER_CRC_POLY_LE);
923 		}
924 	}
925 
926 	return (crc);
927 }
928 #else
929 uint32_t
930 ether_crc32_le(const uint8_t *buf, size_t len)
931 {
932 	static const uint32_t crctab[] = {
933 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
934 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
935 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
936 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
937 	};
938 	size_t i;
939 	uint32_t crc;
940 
941 	crc = 0xffffffff;	/* initial value */
942 
943 	for (i = 0; i < len; i++) {
944 		crc ^= buf[i];
945 		crc = (crc >> 4) ^ crctab[crc & 0xf];
946 		crc = (crc >> 4) ^ crctab[crc & 0xf];
947 	}
948 
949 	return (crc);
950 }
951 #endif
952 
953 uint32_t
954 ether_crc32_be(const uint8_t *buf, size_t len)
955 {
956 	size_t i;
957 	uint32_t crc, carry;
958 	int bit;
959 	uint8_t data;
960 
961 	crc = 0xffffffff;	/* initial value */
962 
963 	for (i = 0; i < len; i++) {
964 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
965 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
966 			crc <<= 1;
967 			if (carry)
968 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
969 		}
970 	}
971 
972 	return (crc);
973 }
974 
975 int
976 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
977 {
978 	struct ifaddr *ifa = (struct ifaddr *) data;
979 	struct ifreq *ifr = (struct ifreq *) data;
980 	int error = 0;
981 
982 	switch (command) {
983 	case SIOCSIFADDR:
984 		ifp->if_flags |= IFF_UP;
985 
986 		switch (ifa->ifa_addr->sa_family) {
987 #ifdef INET
988 		case AF_INET:
989 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
990 			arp_ifinit(ifp, ifa);
991 			break;
992 #endif
993 		default:
994 			ifp->if_init(ifp->if_softc);
995 			break;
996 		}
997 		break;
998 
999 	case SIOCGIFADDR:
1000 		{
1001 			struct sockaddr *sa;
1002 
1003 			sa = (struct sockaddr *) & ifr->ifr_data;
1004 			bcopy(IF_LLADDR(ifp),
1005 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1006 		}
1007 		break;
1008 
1009 	case SIOCSIFMTU:
1010 		/*
1011 		 * Set the interface MTU.
1012 		 */
1013 		if (ifr->ifr_mtu > ETHERMTU) {
1014 			error = EINVAL;
1015 		} else {
1016 			ifp->if_mtu = ifr->ifr_mtu;
1017 		}
1018 		break;
1019 	default:
1020 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1021 		break;
1022 	}
1023 	return (error);
1024 }
1025 
1026 static int
1027 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1028 	struct sockaddr *sa)
1029 {
1030 	struct sockaddr_dl *sdl;
1031 #ifdef INET
1032 	struct sockaddr_in *sin;
1033 #endif
1034 #ifdef INET6
1035 	struct sockaddr_in6 *sin6;
1036 #endif
1037 	u_char *e_addr;
1038 
1039 	switch(sa->sa_family) {
1040 	case AF_LINK:
1041 		/*
1042 		 * No mapping needed. Just check that it's a valid MC address.
1043 		 */
1044 		sdl = (struct sockaddr_dl *)sa;
1045 		e_addr = LLADDR(sdl);
1046 		if (!ETHER_IS_MULTICAST(e_addr))
1047 			return EADDRNOTAVAIL;
1048 		*llsa = 0;
1049 		return 0;
1050 
1051 #ifdef INET
1052 	case AF_INET:
1053 		sin = (struct sockaddr_in *)sa;
1054 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1055 			return EADDRNOTAVAIL;
1056 		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1057 		sdl->sdl_alen = ETHER_ADDR_LEN;
1058 		e_addr = LLADDR(sdl);
1059 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1060 		*llsa = (struct sockaddr *)sdl;
1061 		return 0;
1062 #endif
1063 #ifdef INET6
1064 	case AF_INET6:
1065 		sin6 = (struct sockaddr_in6 *)sa;
1066 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1067 			/*
1068 			 * An IP6 address of 0 means listen to all
1069 			 * of the Ethernet multicast address used for IP6.
1070 			 * (This is used for multicast routers.)
1071 			 */
1072 			ifp->if_flags |= IFF_ALLMULTI;
1073 			*llsa = 0;
1074 			return 0;
1075 		}
1076 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1077 			return EADDRNOTAVAIL;
1078 		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1079 		sdl->sdl_alen = ETHER_ADDR_LEN;
1080 		e_addr = LLADDR(sdl);
1081 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1082 		*llsa = (struct sockaddr *)sdl;
1083 		return 0;
1084 #endif
1085 
1086 	default:
1087 		/*
1088 		 * Well, the text isn't quite right, but it's the name
1089 		 * that counts...
1090 		 */
1091 		return EAFNOSUPPORT;
1092 	}
1093 }
1094 
1095 static void*
1096 ether_alloc(u_char type, struct ifnet *ifp)
1097 {
1098 	struct arpcom	*ac;
1099 
1100 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1101 	ac->ac_ifp = ifp;
1102 
1103 	return (ac);
1104 }
1105 
1106 static void
1107 ether_free(void *com, u_char type)
1108 {
1109 
1110 	free(com, M_ARPCOM);
1111 }
1112 
1113 static int
1114 ether_modevent(module_t mod, int type, void *data)
1115 {
1116 
1117 	switch (type) {
1118 	case MOD_LOAD:
1119 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1120 		break;
1121 	case MOD_UNLOAD:
1122 		if_deregister_com_alloc(IFT_ETHER);
1123 		break;
1124 	default:
1125 		return EOPNOTSUPP;
1126 	}
1127 
1128 	return (0);
1129 }
1130 
1131 static moduledata_t ether_mod = {
1132 	"ether",
1133 	ether_modevent,
1134 	0
1135 };
1136 
1137 void
1138 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1139 {
1140 	struct ether_vlan_header vlan;
1141 	struct mbuf mv, mb;
1142 
1143 	KASSERT((m->m_flags & M_VLANTAG) != 0,
1144 	    ("%s: vlan information not present", __func__));
1145 	KASSERT(m->m_len >= sizeof(struct ether_header),
1146 	    ("%s: mbuf not large enough for header", __func__));
1147 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1148 	vlan.evl_proto = vlan.evl_encap_proto;
1149 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1150 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1151 	m->m_len -= sizeof(struct ether_header);
1152 	m->m_data += sizeof(struct ether_header);
1153 	/*
1154 	 * If a data link has been supplied by the caller, then we will need to
1155 	 * re-create a stack allocated mbuf chain with the following structure:
1156 	 *
1157 	 * (1) mbuf #1 will contain the supplied data link
1158 	 * (2) mbuf #2 will contain the vlan header
1159 	 * (3) mbuf #3 will contain the original mbuf's packet data
1160 	 *
1161 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1162 	 */
1163 	if (data != NULL) {
1164 		mv.m_next = m;
1165 		mv.m_data = (caddr_t)&vlan;
1166 		mv.m_len = sizeof(vlan);
1167 		mb.m_next = &mv;
1168 		mb.m_data = data;
1169 		mb.m_len = dlen;
1170 		bpf_mtap(bp, &mb);
1171 	} else
1172 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1173 	m->m_len += sizeof(struct ether_header);
1174 	m->m_data -= sizeof(struct ether_header);
1175 }
1176 
1177 struct mbuf *
1178 ether_vlanencap(struct mbuf *m, uint16_t tag)
1179 {
1180 	struct ether_vlan_header *evl;
1181 
1182 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1183 	if (m == NULL)
1184 		return (NULL);
1185 	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1186 
1187 	if (m->m_len < sizeof(*evl)) {
1188 		m = m_pullup(m, sizeof(*evl));
1189 		if (m == NULL)
1190 			return (NULL);
1191 	}
1192 
1193 	/*
1194 	 * Transform the Ethernet header into an Ethernet header
1195 	 * with 802.1Q encapsulation.
1196 	 */
1197 	evl = mtod(m, struct ether_vlan_header *);
1198 	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1199 	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1200 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1201 	evl->evl_tag = htons(tag);
1202 	return (m);
1203 }
1204 
1205 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1206 MODULE_VERSION(ether, 1);
1207