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