xref: /freebsd/sys/net/if_ethersubr.c (revision 19fa89e93888ebfe79d06c7b831bcd39001dce63)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
351d5e9e22SEivind Eklund #include "opt_inet.h"
36cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
374cf49a43SJulian Elischer #include "opt_netgraph.h"
386eeac1d9SJulian Elischer #include "opt_mbuf_profiling.h"
397527624eSRobert Watson #include "opt_rss.h"
40430df5f4SEivind Eklund 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
438819ad85SSepherosa Ziehau #include <sys/bus.h>
448819ad85SSepherosa Ziehau #include <sys/eventhandler.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46385195c0SMarko Zec #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4841ee9f1cSPoul-Henning Kamp #include <sys/module.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50f1379734SKonstantin Belousov #include <sys/priv.h>
5110b1fde0SMark Murray #include <sys/random.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
5351a53488SBruce Evans #include <sys/sockio.h>
54602d513cSGarrett Wollman #include <sys/sysctl.h>
55ef1f9169SMarcel Moolenaar #include <sys/uuid.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
59d7647d96SDag-Erling Smørgrav #include <net/if_arp.h>
60df8bae1dSRodney W. Grimes #include <net/netisr.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62df8bae1dSRodney W. Grimes #include <net/if_llc.h>
63df8bae1dSRodney W. Grimes #include <net/if_dl.h>
64df8bae1dSRodney W. Grimes #include <net/if_types.h>
652e2de7f2SArchie Cobbs #include <net/bpf.h>
66e1e1452dSArchie Cobbs #include <net/ethernet.h>
67fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
68c1d93b05SSam Leffler #include <net/if_vlan_var.h>
696e6b3f7cSQing Li #include <net/if_llatbl.h>
707d4317bdSAlexander V. Chernikov #include <net/pfil.h>
71b2bdc62aSAdrian Chadd #include <net/rss_config.h>
724b79449eSBjoern A. Zeeb #include <net/vnet.h>
73df8bae1dSRodney W. Grimes 
7475bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h>
7575bf2db3SGleb Smirnoff 
7682cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6)
77df8bae1dSRodney W. Grimes #include <netinet/in.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
79df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
8054bfbd51SWill Andrews #include <netinet/ip_carp.h>
810bcfa8e4SLuigi Rizzo #include <netinet/ip_var.h>
821d5e9e22SEivind Eklund #endif
8382cd038dSYoshinobu Inoue #ifdef INET6
8482cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
8582cd038dSYoshinobu Inoue #endif
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
87aed55708SRobert Watson 
88d54d93acSEd Maste #ifdef CTASSERT
89d54d93acSEd Maste CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
90d54d93acSEd Maste CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
91d54d93acSEd Maste #endif
92d54d93acSEd Maste 
937d4317bdSAlexander V. Chernikov VNET_DEFINE(struct pfil_head, link_pfil_hook);	/* Packet filter hooks */
947d4317bdSAlexander V. Chernikov 
95e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */
96c1d93b05SSam Leffler void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
97c1d93b05SSam Leffler void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
98e1e1452dSArchie Cobbs int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
99e1e1452dSArchie Cobbs void	(*ng_ether_attach_p)(struct ifnet *ifp);
100e1e1452dSArchie Cobbs void	(*ng_ether_detach_p)(struct ifnet *ifp);
101e1e1452dSArchie Cobbs 
102824eb9dcSDavid E. O'Brien void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
1039d4fe4b2SBrooks Davis 
104fd6238a6SAndrew Thompson /* if_bridge(4) support */
105c8b01292SAndrew Thompson void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
1068f867517SAndrew Thompson 
10718242d3bSAndrew Thompson /* if_lagg(4) support */
10818242d3bSAndrew Thompson struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
109b47888ceSAndrew Thompson 
110e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
111868d8b62SMatthew N. Dodd 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
112868d8b62SMatthew N. Dodd 
113929ddbbbSAlfred Perlstein static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
114929ddbbbSAlfred Perlstein 		struct sockaddr *);
115d3c351c5SMarko Zec #ifdef VIMAGE
116d3c351c5SMarko Zec static	void ether_reassign(struct ifnet *, struct vnet *, char *);
117d3c351c5SMarko Zec #endif
1184fb3a820SAlexander V. Chernikov static	int ether_requestencap(struct ifnet *, struct if_encap_req *);
119868d8b62SMatthew N. Dodd 
12069462a82SBruce M Simpson 
1214c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0)
122df8bae1dSRodney W. Grimes 
12322f8ce43SAlexander V. Chernikov static void
12422f8ce43SAlexander V. Chernikov update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
12522f8ce43SAlexander V. Chernikov {
12622f8ce43SAlexander V. Chernikov 	int csum_flags = 0;
12722f8ce43SAlexander V. Chernikov 
12822f8ce43SAlexander V. Chernikov 	if (src->m_pkthdr.csum_flags & CSUM_IP)
12922f8ce43SAlexander V. Chernikov 		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
13022f8ce43SAlexander V. Chernikov 	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
13122f8ce43SAlexander V. Chernikov 		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
13222f8ce43SAlexander V. Chernikov 	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
13322f8ce43SAlexander V. Chernikov 		csum_flags |= CSUM_SCTP_VALID;
13422f8ce43SAlexander V. Chernikov 	dst->m_pkthdr.csum_flags |= csum_flags;
13522f8ce43SAlexander V. Chernikov 	if (csum_flags & CSUM_DATA_VALID)
13622f8ce43SAlexander V. Chernikov 		dst->m_pkthdr.csum_data = 0xffff;
13722f8ce43SAlexander V. Chernikov }
13822f8ce43SAlexander V. Chernikov 
139df8bae1dSRodney W. Grimes /*
1404fb3a820SAlexander V. Chernikov  * Handle link-layer encapsulation requests.
1414fb3a820SAlexander V. Chernikov  */
1424fb3a820SAlexander V. Chernikov static int
1434fb3a820SAlexander V. Chernikov ether_requestencap(struct ifnet *ifp, struct if_encap_req *req)
1444fb3a820SAlexander V. Chernikov {
1454fb3a820SAlexander V. Chernikov 	struct ether_header *eh;
1464fb3a820SAlexander V. Chernikov 	struct arphdr *ah;
1474fb3a820SAlexander V. Chernikov 	uint16_t etype;
1484fb3a820SAlexander V. Chernikov 	const u_char *lladdr;
1494fb3a820SAlexander V. Chernikov 
1504fb3a820SAlexander V. Chernikov 	if (req->rtype != IFENCAP_LL)
1514fb3a820SAlexander V. Chernikov 		return (EOPNOTSUPP);
1524fb3a820SAlexander V. Chernikov 
1534fb3a820SAlexander V. Chernikov 	if (req->bufsize < ETHER_HDR_LEN)
1544fb3a820SAlexander V. Chernikov 		return (ENOMEM);
1554fb3a820SAlexander V. Chernikov 
1564fb3a820SAlexander V. Chernikov 	eh = (struct ether_header *)req->buf;
1574fb3a820SAlexander V. Chernikov 	lladdr = req->lladdr;
1584fb3a820SAlexander V. Chernikov 	req->lladdr_off = 0;
1594fb3a820SAlexander V. Chernikov 
1604fb3a820SAlexander V. Chernikov 	switch (req->family) {
1614fb3a820SAlexander V. Chernikov 	case AF_INET:
1624fb3a820SAlexander V. Chernikov 		etype = htons(ETHERTYPE_IP);
1634fb3a820SAlexander V. Chernikov 		break;
1644fb3a820SAlexander V. Chernikov 	case AF_INET6:
1654fb3a820SAlexander V. Chernikov 		etype = htons(ETHERTYPE_IPV6);
1664fb3a820SAlexander V. Chernikov 		break;
1674fb3a820SAlexander V. Chernikov 	case AF_ARP:
1684fb3a820SAlexander V. Chernikov 		ah = (struct arphdr *)req->hdata;
1694fb3a820SAlexander V. Chernikov 		ah->ar_hrd = htons(ARPHRD_ETHER);
1704fb3a820SAlexander V. Chernikov 
1714fb3a820SAlexander V. Chernikov 		switch(ntohs(ah->ar_op)) {
1724fb3a820SAlexander V. Chernikov 		case ARPOP_REVREQUEST:
1734fb3a820SAlexander V. Chernikov 		case ARPOP_REVREPLY:
1744fb3a820SAlexander V. Chernikov 			etype = htons(ETHERTYPE_REVARP);
1754fb3a820SAlexander V. Chernikov 			break;
1764fb3a820SAlexander V. Chernikov 		case ARPOP_REQUEST:
1774fb3a820SAlexander V. Chernikov 		case ARPOP_REPLY:
1784fb3a820SAlexander V. Chernikov 		default:
1794fb3a820SAlexander V. Chernikov 			etype = htons(ETHERTYPE_ARP);
1804fb3a820SAlexander V. Chernikov 			break;
1814fb3a820SAlexander V. Chernikov 		}
1824fb3a820SAlexander V. Chernikov 
1834fb3a820SAlexander V. Chernikov 		if (req->flags & IFENCAP_FLAG_BROADCAST)
1844fb3a820SAlexander V. Chernikov 			lladdr = ifp->if_broadcastaddr;
1854fb3a820SAlexander V. Chernikov 		break;
1864fb3a820SAlexander V. Chernikov 	default:
1874fb3a820SAlexander V. Chernikov 		return (EAFNOSUPPORT);
1884fb3a820SAlexander V. Chernikov 	}
1894fb3a820SAlexander V. Chernikov 
1904fb3a820SAlexander V. Chernikov 	memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
1914fb3a820SAlexander V. Chernikov 	memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN);
1924fb3a820SAlexander V. Chernikov 	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1934fb3a820SAlexander V. Chernikov 	req->bufsize = sizeof(struct ether_header);
1944fb3a820SAlexander V. Chernikov 
1954fb3a820SAlexander V. Chernikov 	return (0);
1964fb3a820SAlexander V. Chernikov }
1974fb3a820SAlexander V. Chernikov 
1984fb3a820SAlexander V. Chernikov 
1994fb3a820SAlexander V. Chernikov static int
2004fb3a820SAlexander V. Chernikov ether_resolve_addr(struct ifnet *ifp, struct mbuf *m,
2014fb3a820SAlexander V. Chernikov 	const struct sockaddr *dst, struct route *ro, u_char *phdr,
2026d768226SGeorge V. Neville-Neil 	uint32_t *pflags, struct llentry **plle)
2034fb3a820SAlexander V. Chernikov {
2044fb3a820SAlexander V. Chernikov 	struct ether_header *eh;
2054fb3a820SAlexander V. Chernikov 	uint32_t lleflags = 0;
2064fb3a820SAlexander V. Chernikov 	int error = 0;
2074fb3a820SAlexander V. Chernikov #if defined(INET) || defined(INET6)
2084fb3a820SAlexander V. Chernikov 	uint16_t etype;
2094fb3a820SAlexander V. Chernikov #endif
2104fb3a820SAlexander V. Chernikov 
2116d768226SGeorge V. Neville-Neil 	if (plle)
2126d768226SGeorge V. Neville-Neil 		*plle = NULL;
2134fb3a820SAlexander V. Chernikov 	eh = (struct ether_header *)phdr;
2144fb3a820SAlexander V. Chernikov 
2154fb3a820SAlexander V. Chernikov 	switch (dst->sa_family) {
2164fb3a820SAlexander V. Chernikov #ifdef INET
2174fb3a820SAlexander V. Chernikov 	case AF_INET:
2184fb3a820SAlexander V. Chernikov 		if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
2196d768226SGeorge V. Neville-Neil 			error = arpresolve(ifp, 0, m, dst, phdr, &lleflags,
2206d768226SGeorge V. Neville-Neil 			    plle);
2214fb3a820SAlexander V. Chernikov 		else {
2224fb3a820SAlexander V. Chernikov 			if (m->m_flags & M_BCAST)
2234fb3a820SAlexander V. Chernikov 				memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
2244fb3a820SAlexander V. Chernikov 				    ETHER_ADDR_LEN);
2254fb3a820SAlexander V. Chernikov 			else {
2264fb3a820SAlexander V. Chernikov 				const struct in_addr *a;
2274fb3a820SAlexander V. Chernikov 				a = &(((const struct sockaddr_in *)dst)->sin_addr);
2284fb3a820SAlexander V. Chernikov 				ETHER_MAP_IP_MULTICAST(a, eh->ether_dhost);
2294fb3a820SAlexander V. Chernikov 			}
2304fb3a820SAlexander V. Chernikov 			etype = htons(ETHERTYPE_IP);
2314fb3a820SAlexander V. Chernikov 			memcpy(&eh->ether_type, &etype, sizeof(etype));
2324fb3a820SAlexander V. Chernikov 			memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
2334fb3a820SAlexander V. Chernikov 		}
2344fb3a820SAlexander V. Chernikov 		break;
2354fb3a820SAlexander V. Chernikov #endif
2364fb3a820SAlexander V. Chernikov #ifdef INET6
2374fb3a820SAlexander V. Chernikov 	case AF_INET6:
2384fb3a820SAlexander V. Chernikov 		if ((m->m_flags & M_MCAST) == 0)
2396d768226SGeorge V. Neville-Neil 			error = nd6_resolve(ifp, 0, m, dst, phdr, &lleflags,
2406d768226SGeorge V. Neville-Neil 			    plle);
2414fb3a820SAlexander V. Chernikov 		else {
2424fb3a820SAlexander V. Chernikov 			const struct in6_addr *a6;
2434fb3a820SAlexander V. Chernikov 			a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr);
2444fb3a820SAlexander V. Chernikov 			ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost);
2454fb3a820SAlexander V. Chernikov 			etype = htons(ETHERTYPE_IPV6);
2464fb3a820SAlexander V. Chernikov 			memcpy(&eh->ether_type, &etype, sizeof(etype));
2474fb3a820SAlexander V. Chernikov 			memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
2484fb3a820SAlexander V. Chernikov 		}
2494fb3a820SAlexander V. Chernikov 		break;
2504fb3a820SAlexander V. Chernikov #endif
2514fb3a820SAlexander V. Chernikov 	default:
2524fb3a820SAlexander V. Chernikov 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
2534fb3a820SAlexander V. Chernikov 		if (m != NULL)
2544fb3a820SAlexander V. Chernikov 			m_freem(m);
2554fb3a820SAlexander V. Chernikov 		return (EAFNOSUPPORT);
2564fb3a820SAlexander V. Chernikov 	}
2574fb3a820SAlexander V. Chernikov 
2584fb3a820SAlexander V. Chernikov 	if (error == EHOSTDOWN) {
25936402a68SAlexander V. Chernikov 		if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0)
2604fb3a820SAlexander V. Chernikov 			error = EHOSTUNREACH;
2614fb3a820SAlexander V. Chernikov 	}
2624fb3a820SAlexander V. Chernikov 
2634fb3a820SAlexander V. Chernikov 	if (error != 0)
2644fb3a820SAlexander V. Chernikov 		return (error);
2654fb3a820SAlexander V. Chernikov 
2664fb3a820SAlexander V. Chernikov 	*pflags = RT_MAY_LOOP;
2674fb3a820SAlexander V. Chernikov 	if (lleflags & LLE_IFADDR)
2684fb3a820SAlexander V. Chernikov 		*pflags |= RT_L2_ME;
2694fb3a820SAlexander V. Chernikov 
2704fb3a820SAlexander V. Chernikov 	return (0);
2714fb3a820SAlexander V. Chernikov }
2724fb3a820SAlexander V. Chernikov 
2734fb3a820SAlexander V. Chernikov /*
274df8bae1dSRodney W. Grimes  * Ethernet output routine.
275df8bae1dSRodney W. Grimes  * Encapsulate a packet of type family for the local net.
276df8bae1dSRodney W. Grimes  * Use trailer local net encapsulation if enough data in first
277df8bae1dSRodney W. Grimes  * packet leaves a multiple of 512 bytes of data in remainder.
278df8bae1dSRodney W. Grimes  */
279df8bae1dSRodney W. Grimes int
28072fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m,
28147e8d432SGleb Smirnoff 	const struct sockaddr *dst, struct route *ro)
282df8bae1dSRodney W. Grimes {
2834fb3a820SAlexander V. Chernikov 	int error = 0;
2844fb3a820SAlexander V. Chernikov 	char linkhdr[ETHER_HDR_LEN], *phdr;
285098a8c3bSMatthew N. Dodd 	struct ether_header *eh;
28660ee3847SMax Laier 	struct pf_mtag *t;
287c54c76ccSOleg Bulyzhin 	int loop_copy = 1;
288d32864c3SRobert Watson 	int hlen;	/* link layer header length */
2894fb3a820SAlexander V. Chernikov 	uint32_t pflags;
2906d768226SGeorge V. Neville-Neil 	struct llentry *lle = NULL;
2916d768226SGeorge V. Neville-Neil 	int addref = 0;
292df8bae1dSRodney W. Grimes 
2934fb3a820SAlexander V. Chernikov 	phdr = NULL;
2944fb3a820SAlexander V. Chernikov 	pflags = 0;
295279aa3d4SKip Macy 	if (ro != NULL) {
2966d768226SGeorge V. Neville-Neil 		/* XXX BPF uses ro_prepend */
2976d768226SGeorge V. Neville-Neil 		if (ro->ro_prepend != NULL) {
2984fb3a820SAlexander V. Chernikov 			phdr = ro->ro_prepend;
2994fb3a820SAlexander V. Chernikov 			hlen = ro->ro_plen;
3006d768226SGeorge V. Neville-Neil 		} else if (!(m->m_flags & (M_BCAST | M_MCAST))) {
3016d768226SGeorge V. Neville-Neil 			if ((ro->ro_flags & RT_LLE_CACHE) != 0) {
3026d768226SGeorge V. Neville-Neil 				lle = ro->ro_lle;
3036d768226SGeorge V. Neville-Neil 				if (lle != NULL &&
3046d768226SGeorge V. Neville-Neil 				    (lle->la_flags & LLE_VALID) == 0) {
3056d768226SGeorge V. Neville-Neil 					LLE_FREE(lle);
3066d768226SGeorge V. Neville-Neil 					lle = NULL;	/* redundant */
3076d768226SGeorge V. Neville-Neil 					ro->ro_lle = NULL;
308279aa3d4SKip Macy 				}
3096d768226SGeorge V. Neville-Neil 				if (lle == NULL) {
3106d768226SGeorge V. Neville-Neil 					/* if we lookup, keep cache */
3116d768226SGeorge V. Neville-Neil 					addref = 1;
3121435dcd9SAlexander V. Chernikov 				} else
3131435dcd9SAlexander V. Chernikov 					/*
3141435dcd9SAlexander V. Chernikov 					 * Notify LLE code that
3151435dcd9SAlexander V. Chernikov 					 * the entry was used
3161435dcd9SAlexander V. Chernikov 					 * by datapath.
3171435dcd9SAlexander V. Chernikov 					 */
3181435dcd9SAlexander V. Chernikov 					llentry_mark_used(lle);
3196d768226SGeorge V. Neville-Neil 			}
3206d768226SGeorge V. Neville-Neil 			if (lle != NULL) {
3216d768226SGeorge V. Neville-Neil 				phdr = lle->r_linkdata;
3226d768226SGeorge V. Neville-Neil 				hlen = lle->r_hdrlen;
3236d768226SGeorge V. Neville-Neil 				pflags = lle->r_flags;
3246d768226SGeorge V. Neville-Neil 			}
3256d768226SGeorge V. Neville-Neil 		}
3266d768226SGeorge V. Neville-Neil 	}
3276d768226SGeorge V. Neville-Neil 
32843b29369SRobert Watson #ifdef MAC
32930d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
33043b29369SRobert Watson 	if (error)
33143b29369SRobert Watson 		senderr(error);
33243b29369SRobert Watson #endif
33343b29369SRobert Watson 
3346eeac1d9SJulian Elischer 	M_PROFILE(m);
335afbe3a0fSPoul-Henning Kamp 	if (ifp->if_flags & IFF_MONITOR)
336afbe3a0fSPoul-Henning Kamp 		senderr(ENETDOWN);
33713f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
33813f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
339df8bae1dSRodney W. Grimes 		senderr(ENETDOWN);
3407f760c48SMatthew N. Dodd 
3414fb3a820SAlexander V. Chernikov 	if (phdr == NULL) {
3424fb3a820SAlexander V. Chernikov 		/* No prepend data supplied. Try to calculate ourselves. */
3434fb3a820SAlexander V. Chernikov 		phdr = linkhdr;
34484dd0fd0SJulian Elischer 		hlen = ETHER_HDR_LEN;
3456d768226SGeorge V. Neville-Neil 		error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags,
3466d768226SGeorge V. Neville-Neil 		    addref ? &lle : NULL);
3476d768226SGeorge V. Neville-Neil 		if (addref && lle != NULL)
3486d768226SGeorge V. Neville-Neil 			ro->ro_lle = lle;
3494fb3a820SAlexander V. Chernikov 		if (error != 0)
350cd46a114SLuigi Rizzo 			return (error == EWOULDBLOCK ? 0 : error);
35143a6c75aSMatthew N. Dodd 	}
35243a6c75aSMatthew N. Dodd 
3534fb3a820SAlexander V. Chernikov 	if ((pflags & RT_L2_ME) != 0) {
35422f8ce43SAlexander V. Chernikov 		update_mbuf_csumflags(m, m);
3556e6b3f7cSQing Li 		return (if_simloop(ifp, m, dst->sa_family, 0));
3566e6b3f7cSQing Li 	}
3574fb3a820SAlexander V. Chernikov 	loop_copy = pflags & RT_MAY_LOOP;
3586e6b3f7cSQing Li 
359df8bae1dSRodney W. Grimes 	/*
360df8bae1dSRodney W. Grimes 	 * Add local net header.  If no space in first mbuf,
361df8bae1dSRodney W. Grimes 	 * allocate another.
3626cdb1854SAlexander V. Chernikov 	 *
3636cdb1854SAlexander V. Chernikov 	 * Note that we do prepend regardless of RT_HAS_HEADER flag.
3646cdb1854SAlexander V. Chernikov 	 * This is done because BPF code shifts m_data pointer
3656cdb1854SAlexander V. Chernikov 	 * to the end of ethernet header prior to calling if_output().
366df8bae1dSRodney W. Grimes 	 */
3674fb3a820SAlexander V. Chernikov 	M_PREPEND(m, hlen, M_NOWAIT);
3682166ffe8SRobert Watson 	if (m == NULL)
369df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
3704fb3a820SAlexander V. Chernikov 	if ((pflags & RT_HAS_HEADER) == 0) {
371df8bae1dSRodney W. Grimes 		eh = mtod(m, struct ether_header *);
3724fb3a820SAlexander V. Chernikov 		memcpy(eh, phdr, hlen);
373c69aeaadSAlexander V. Chernikov 	}
374ed7509acSJulian Elischer 
375ed7509acSJulian Elischer 	/*
376ed7509acSJulian Elischer 	 * If a simplex interface, and the packet is being sent to our
377ed7509acSJulian Elischer 	 * Ethernet address or a broadcast address, loopback a copy.
378ed7509acSJulian Elischer 	 * XXX To make a simplex device behave exactly like a duplex
379ed7509acSJulian Elischer 	 * device, we should copy in the case of sending to our own
380ed7509acSJulian Elischer 	 * ethernet address (thus letting the original actually appear
381ed7509acSJulian Elischer 	 * on the wire). However, we don't do that here for security
382ed7509acSJulian Elischer 	 * reasons and compatibility with the original behavior.
383ed7509acSJulian Elischer 	 */
3844fb3a820SAlexander V. Chernikov 	if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) &&
38560ee3847SMax Laier 	    ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
38606f684b0SMatthew N. Dodd 		struct mbuf *n;
387ed7509acSJulian Elischer 
388294dd290SLuigi Rizzo 		/*
389294dd290SLuigi Rizzo 		 * Because if_simloop() modifies the packet, we need a
390294dd290SLuigi Rizzo 		 * writable copy through m_dup() instead of a readonly
391294dd290SLuigi Rizzo 		 * one as m_copy[m] would give us. The alternative would
392294dd290SLuigi Rizzo 		 * be to modify if_simloop() to handle the readonly mbuf,
393294dd290SLuigi Rizzo 		 * but performancewise it is mostly equivalent (trading
394294dd290SLuigi Rizzo 		 * extra data copying vs. extra locking).
3959983b3c0SYaroslav Tykhiy 		 *
3969983b3c0SYaroslav Tykhiy 		 * XXX This is a local workaround.  A number of less
3979983b3c0SYaroslav Tykhiy 		 * often used kernel parts suffer from the same bug.
3989983b3c0SYaroslav Tykhiy 		 * See PR kern/105943 for a proposed general solution.
399294dd290SLuigi Rizzo 		 */
400eb1b1807SGleb Smirnoff 		if ((n = m_dup(m, M_NOWAIT)) != NULL) {
40122f8ce43SAlexander V. Chernikov 			update_mbuf_csumflags(m, n);
40206a429a3SArchie Cobbs 			(void)if_simloop(ifp, n, dst->sa_family, hlen);
403c1404dc0SArchie Cobbs 		} else
4043751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
405ed7509acSJulian Elischer 	}
4062e2de7f2SArchie Cobbs 
4073f7d1396SAndrew Thompson        /*
4083f7d1396SAndrew Thompson 	* Bridges require special output handling.
4093f7d1396SAndrew Thompson 	*/
4103f7d1396SAndrew Thompson 	if (ifp->if_bridge) {
4113f7d1396SAndrew Thompson 		BRIDGE_OUTPUT(ifp, m, error);
4123f7d1396SAndrew Thompson 		return (error);
4133f7d1396SAndrew Thompson 	}
4143f7d1396SAndrew Thompson 
415259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6)
416a9771948SGleb Smirnoff 	if (ifp->if_carp &&
41708b68b0eSGleb Smirnoff 	    (error = (*carp_output_p)(ifp, m, dst)))
418a9771948SGleb Smirnoff 		goto bad;
419a9771948SGleb Smirnoff #endif
420a9771948SGleb Smirnoff 
421e1e1452dSArchie Cobbs 	/* Handle ng_ether(4) processing, if any */
422833e8dc5SGleb Smirnoff 	if (ifp->if_l2com != NULL) {
423514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_output_p != NULL,
424514bcb89SPoul-Henning Kamp 		    ("ng_ether_output_p is NULL"));
425e1e1452dSArchie Cobbs 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
426e1e1452dSArchie Cobbs bad:			if (m != NULL)
427e1e1452dSArchie Cobbs 				m_freem(m);
428e1e1452dSArchie Cobbs 			return (error);
429e1e1452dSArchie Cobbs 		}
430e1e1452dSArchie Cobbs 		if (m == NULL)
431e1e1452dSArchie Cobbs 			return (0);
432e1e1452dSArchie Cobbs 	}
433e1e1452dSArchie Cobbs 
434e1e1452dSArchie Cobbs 	/* Continue with link-layer output */
435e1e1452dSArchie Cobbs 	return ether_output_frame(ifp, m);
436e1e1452dSArchie Cobbs }
437e1e1452dSArchie Cobbs 
438f1379734SKonstantin Belousov static bool
439f1379734SKonstantin Belousov ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp)
440f1379734SKonstantin Belousov {
441f1379734SKonstantin Belousov 	struct ether_header *eh;
442f1379734SKonstantin Belousov 
443f1379734SKonstantin Belousov 	eh = mtod(*mp, struct ether_header *);
444f1379734SKonstantin Belousov 	if (ntohs(eh->ether_type) == ETHERTYPE_VLAN ||
445f1379734SKonstantin Belousov 	    ether_8021q_frame(mp, ifp, ifp, 0, pcp))
446f1379734SKonstantin Belousov 		return (true);
447f1379734SKonstantin Belousov 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
448f1379734SKonstantin Belousov 	return (false);
449f1379734SKonstantin Belousov }
450f1379734SKonstantin Belousov 
451e1e1452dSArchie Cobbs /*
452e1e1452dSArchie Cobbs  * Ethernet link layer output routine to send a raw frame to the device.
453e1e1452dSArchie Cobbs  *
454e1e1452dSArchie Cobbs  * This assumes that the 14 byte Ethernet header is present and contiguous
455e1e1452dSArchie Cobbs  * in the first mbuf (if BRIDGE'ing).
456e1e1452dSArchie Cobbs  */
457e1e1452dSArchie Cobbs int
458c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m)
459e1e1452dSArchie Cobbs {
460f1379734SKonstantin Belousov 	int error;
461f1379734SKonstantin Belousov 	uint8_t pcp;
462f1379734SKonstantin Belousov 
463f1379734SKonstantin Belousov 	pcp = ifp->if_pcp;
46447c39432SNavdeep Parhar 	if (pcp != IFNET_PCP_NONE && ifp->if_type != IFT_L2VLAN &&
46547c39432SNavdeep Parhar 	    !ether_set_pcp(&m, ifp, pcp))
466f1379734SKonstantin Belousov 		return (0);
4672b25acc1SLuigi Rizzo 
4687d4317bdSAlexander V. Chernikov 	if (PFIL_HOOKED(&V_link_pfil_hook)) {
469f1379734SKonstantin Belousov 		error = pfil_run_hooks(&V_link_pfil_hook, &m, ifp,
470f1379734SKonstantin Belousov 		    PFIL_OUT, 0, NULL);
471f1379734SKonstantin Belousov 		if (error != 0)
4727d4317bdSAlexander V. Chernikov 			return (EACCES);
4737d4317bdSAlexander V. Chernikov 
4747d4317bdSAlexander V. Chernikov 		if (m == NULL)
4757d4317bdSAlexander V. Chernikov 			return (0);
4764b984093SLuigi Rizzo 	}
4772b25acc1SLuigi Rizzo 
478df8bae1dSRodney W. Grimes 	/*
479df5e1987SJonathan Lemon 	 * Queue message on interface, update output statistics if
480df5e1987SJonathan Lemon 	 * successful, and start output if interface not yet active.
481df8bae1dSRodney W. Grimes 	 */
482aea78d20SKip Macy 	return ((ifp->if_transmit)(ifp, m));
483df8bae1dSRodney W. Grimes }
484df8bae1dSRodney W. Grimes 
4854b984093SLuigi Rizzo /*
486c1d93b05SSam Leffler  * Process a received Ethernet packet; the packet is in the
487c1d93b05SSam Leffler  * mbuf chain m with the ethernet header at the front.
488df8bae1dSRodney W. Grimes  */
489c1d93b05SSam Leffler static void
4906cb52192SRobert Watson ether_input_internal(struct ifnet *ifp, struct mbuf *m)
491df8bae1dSRodney W. Grimes {
492c1d93b05SSam Leffler 	struct ether_header *eh;
493c1d93b05SSam Leffler 	u_short etype;
494df8bae1dSRodney W. Grimes 
49569462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_UP) == 0) {
49669462a82SBruce M Simpson 		m_freem(m);
49769462a82SBruce M Simpson 		return;
49869462a82SBruce M Simpson 	}
49969462a82SBruce M Simpson #ifdef DIAGNOSTIC
50069462a82SBruce M Simpson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
50169462a82SBruce M Simpson 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
50269462a82SBruce M Simpson 		m_freem(m);
50369462a82SBruce M Simpson 		return;
50469462a82SBruce M Simpson 	}
50569462a82SBruce M Simpson #endif
506797f247bSMatthew N. Dodd 	if (m->m_len < ETHER_HDR_LEN) {
507c1d93b05SSam Leffler 		/* XXX maybe should pullup? */
508c1d93b05SSam Leffler 		if_printf(ifp, "discard frame w/o leading ethernet "
509c1d93b05SSam Leffler 				"header (len %u pkt len %u)\n",
510c1d93b05SSam Leffler 				m->m_len, m->m_pkthdr.len);
5113751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
512c1d93b05SSam Leffler 		m_freem(m);
513c1d93b05SSam Leffler 		return;
5142e2de7f2SArchie Cobbs 	}
515c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
516c1d93b05SSam Leffler 	etype = ntohs(eh->ether_type);
517*19fa89e9SMark Murray 	random_harvest_queue_ether(m, sizeof(*m));
518d1b06863SMark Murray 
51921ca7b57SMarko Zec 	CURVNET_SET_QUIET(ifp->if_vnet);
52021ca7b57SMarko Zec 
52169462a82SBruce M Simpson 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
52269462a82SBruce M Simpson 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
52369462a82SBruce M Simpson 			m->m_flags |= M_BCAST;
52469462a82SBruce M Simpson 		else
52569462a82SBruce M Simpson 			m->m_flags |= M_MCAST;
5263751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
52769462a82SBruce M Simpson 	}
52869462a82SBruce M Simpson 
529533d8562SRobert Watson #ifdef MAC
530533d8562SRobert Watson 	/*
531533d8562SRobert Watson 	 * Tag the mbuf with an appropriate MAC label before any other
532533d8562SRobert Watson 	 * consumers can get to it.
533533d8562SRobert Watson 	 */
53430d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
535533d8562SRobert Watson #endif
536533d8562SRobert Watson 
537c1d93b05SSam Leffler 	/*
53869462a82SBruce M Simpson 	 * Give bpf a chance at the packet.
539c1d93b05SSam Leffler 	 */
540d05d4616SChristian S.J. Peron 	ETHER_BPF_MTAP(ifp, m);
5412e2de7f2SArchie Cobbs 
54269462a82SBruce M Simpson 	/*
54369462a82SBruce M Simpson 	 * If the CRC is still on the packet, trim it off. We do this once
54469462a82SBruce M Simpson 	 * and once only in case we are re-entered. Nothing else on the
54569462a82SBruce M Simpson 	 * Ethernet receive path expects to see the FCS.
54669462a82SBruce M Simpson 	 */
547c1d93b05SSam Leffler 	if (m->m_flags & M_HASFCS) {
548c1d93b05SSam Leffler 		m_adj(m, -ETHER_CRC_LEN);
549c1d93b05SSam Leffler 		m->m_flags &= ~M_HASFCS;
550c1d93b05SSam Leffler 	}
551c1d93b05SSam Leffler 
5524cdc1f54SGleb Smirnoff 	if (!(ifp->if_capenable & IFCAP_HWSTATS))
5533751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
55405463bb5SDavid Greenman 
55569462a82SBruce M Simpson 	/* Allow monitor mode to claim this frame, after stats are updated. */
556de572b37SChristian S.J. Peron 	if (ifp->if_flags & IFF_MONITOR) {
557de572b37SChristian S.J. Peron 		m_freem(m);
55821ca7b57SMarko Zec 		CURVNET_RESTORE();
559de572b37SChristian S.J. Peron 		return;
560de572b37SChristian S.J. Peron 	}
561de572b37SChristian S.J. Peron 
56218242d3bSAndrew Thompson 	/* Handle input from a lagg(4) port */
563b47888ceSAndrew Thompson 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
56418242d3bSAndrew Thompson 		KASSERT(lagg_input_p != NULL,
56518242d3bSAndrew Thompson 		    ("%s: if_lagg not loaded!", __func__));
56618242d3bSAndrew Thompson 		m = (*lagg_input_p)(ifp, m);
567b47888ceSAndrew Thompson 		if (m != NULL)
568b47888ceSAndrew Thompson 			ifp = m->m_pkthdr.rcvif;
5692db13e75SMarko Zec 		else {
5702db13e75SMarko Zec 			CURVNET_RESTORE();
571b47888ceSAndrew Thompson 			return;
572b47888ceSAndrew Thompson 		}
5732db13e75SMarko Zec 	}
574b47888ceSAndrew Thompson 
57569462a82SBruce M Simpson 	/*
57669462a82SBruce M Simpson 	 * If the hardware did not process an 802.1Q tag, do this now,
57769462a82SBruce M Simpson 	 * to allow 802.1P priority frames to be passed to the main input
57869462a82SBruce M Simpson 	 * path correctly.
57969462a82SBruce M Simpson 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
58069462a82SBruce M Simpson 	 */
58169462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
58269462a82SBruce M Simpson 		struct ether_vlan_header *evl;
58369462a82SBruce M Simpson 
58469462a82SBruce M Simpson 		if (m->m_len < sizeof(*evl) &&
58569462a82SBruce M Simpson 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
586402d5e27SBruce M Simpson #ifdef DIAGNOSTIC
58769462a82SBruce M Simpson 			if_printf(ifp, "cannot pullup VLAN header\n");
588402d5e27SBruce M Simpson #endif
5893751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
5902db13e75SMarko Zec 			CURVNET_RESTORE();
59169462a82SBruce M Simpson 			return;
59269462a82SBruce M Simpson 		}
59369462a82SBruce M Simpson 
59469462a82SBruce M Simpson 		evl = mtod(m, struct ether_vlan_header *);
59569462a82SBruce M Simpson 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
59669462a82SBruce M Simpson 		m->m_flags |= M_VLANTAG;
59769462a82SBruce M Simpson 
59869462a82SBruce M Simpson 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
59969462a82SBruce M Simpson 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
60069462a82SBruce M Simpson 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
60197cce87fSGleb Smirnoff 		eh = mtod(m, struct ether_header *);
60269462a82SBruce M Simpson 	}
60369462a82SBruce M Simpson 
604a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
605a34c6aebSBjoern A. Zeeb 
60669462a82SBruce M Simpson 	/* Allow ng_ether(4) to claim this frame. */
607833e8dc5SGleb Smirnoff 	if (ifp->if_l2com != NULL) {
608514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_p != NULL,
60969462a82SBruce M Simpson 		    ("%s: ng_ether_input_p is NULL", __func__));
61069462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
611c1d93b05SSam Leffler 		(*ng_ether_input_p)(ifp, &m);
61221ca7b57SMarko Zec 		if (m == NULL) {
61321ca7b57SMarko Zec 			CURVNET_RESTORE();
614e1e1452dSArchie Cobbs 			return;
615e1e1452dSArchie Cobbs 		}
6165e9a5429SGleb Smirnoff 		eh = mtod(m, struct ether_header *);
61721ca7b57SMarko Zec 	}
618e1e1452dSArchie Cobbs 
6198f867517SAndrew Thompson 	/*
62069462a82SBruce M Simpson 	 * Allow if_bridge(4) to claim this frame.
62169462a82SBruce M Simpson 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
62269462a82SBruce M Simpson 	 * and the frame should be delivered locally.
6238f867517SAndrew Thompson 	 */
62469462a82SBruce M Simpson 	if (ifp->if_bridge != NULL) {
62569462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
626fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
62721ca7b57SMarko Zec 		if (m == NULL) {
62821ca7b57SMarko Zec 			CURVNET_RESTORE();
6298f867517SAndrew Thompson 			return;
6308f867517SAndrew Thompson 		}
6315e9a5429SGleb Smirnoff 		eh = mtod(m, struct ether_header *);
63221ca7b57SMarko Zec 	}
6338f867517SAndrew Thompson 
634259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6)
63569462a82SBruce M Simpson 	/*
63669462a82SBruce M Simpson 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
63769462a82SBruce M Simpson 	 * mbuf flows up to Layer 3.
63869462a82SBruce M Simpson 	 * FreeBSD's implementation of carp(4) uses the inprotosw
63969462a82SBruce M Simpson 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
64069462a82SBruce M Simpson 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
64169462a82SBruce M Simpson 	 * is outside the scope of the M_PROMISC test below.
64269462a82SBruce M Simpson 	 * TODO: Maintain a hash table of ethernet addresses other than
64369462a82SBruce M Simpson 	 * ether_dhost which may be active on this ifp.
64469462a82SBruce M Simpson 	 */
64554bfbd51SWill Andrews 	if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
64669462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
64769462a82SBruce M Simpson 	} else
64869462a82SBruce M Simpson #endif
64969462a82SBruce M Simpson 	{
65069462a82SBruce M Simpson 		/*
651e7f8c833SBruce M Simpson 		 * If the frame received was not for our MAC address, set the
65269462a82SBruce M Simpson 		 * M_PROMISC flag on the mbuf chain. The frame may need to
65369462a82SBruce M Simpson 		 * be seen by the rest of the Ethernet input path in case of
65469462a82SBruce M Simpson 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
65569462a82SBruce M Simpson 		 * seen by upper protocol layers.
65669462a82SBruce M Simpson 		 */
65769462a82SBruce M Simpson 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
658e7f8c833SBruce M Simpson 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
65969462a82SBruce M Simpson 			m->m_flags |= M_PROMISC;
66069462a82SBruce M Simpson 	}
66169462a82SBruce M Simpson 
662acf032f5SRobert Watson 	ether_demux(ifp, m);
66321ca7b57SMarko Zec 	CURVNET_RESTORE();
664e1e1452dSArchie Cobbs }
665e1e1452dSArchie Cobbs 
666e1e1452dSArchie Cobbs /*
6676cb52192SRobert Watson  * Ethernet input dispatch; by default, direct dispatch here regardless of
6687527624eSRobert Watson  * global configuration.  However, if RSS is enabled, hook up RSS affinity
6697527624eSRobert Watson  * so that when deferred or hybrid dispatch is enabled, we can redistribute
6707527624eSRobert Watson  * load based on RSS.
6717527624eSRobert Watson  *
6727527624eSRobert Watson  * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
6737527624eSRobert Watson  * not it had already done work distribution via multi-queue.  Then we could
6747527624eSRobert Watson  * direct dispatch in the event load balancing was already complete and
6757527624eSRobert Watson  * handle the case of interfaces with different capabilities better.
6767527624eSRobert Watson  *
6777527624eSRobert Watson  * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
6787527624eSRobert Watson  * at multiple layers?
6797527624eSRobert Watson  *
6807527624eSRobert Watson  * XXXRW: For now, enable all this only if RSS is compiled in, although it
6817527624eSRobert Watson  * works fine without RSS.  Need to characterise the performance overhead
6827527624eSRobert Watson  * of the detour through the netisr code in the event the result is always
6837527624eSRobert Watson  * direct dispatch.
6846cb52192SRobert Watson  */
6856cb52192SRobert Watson static void
6866cb52192SRobert Watson ether_nh_input(struct mbuf *m)
6876cb52192SRobert Watson {
6886cb52192SRobert Watson 
689b71bed24SAndrey V. Elsukov 	M_ASSERTPKTHDR(m);
690b71bed24SAndrey V. Elsukov 	KASSERT(m->m_pkthdr.rcvif != NULL,
691b71bed24SAndrey V. Elsukov 	    ("%s: NULL interface pointer", __func__));
6926cb52192SRobert Watson 	ether_input_internal(m->m_pkthdr.rcvif, m);
6936cb52192SRobert Watson }
6946cb52192SRobert Watson 
6956cb52192SRobert Watson static struct netisr_handler	ether_nh = {
6966cb52192SRobert Watson 	.nh_name = "ether",
6976cb52192SRobert Watson 	.nh_handler = ether_nh_input,
6986cb52192SRobert Watson 	.nh_proto = NETISR_ETHER,
6997527624eSRobert Watson #ifdef RSS
7007527624eSRobert Watson 	.nh_policy = NETISR_POLICY_CPU,
7017527624eSRobert Watson 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
7027527624eSRobert Watson 	.nh_m2cpuid = rss_m2cpuid,
7037527624eSRobert Watson #else
7046cb52192SRobert Watson 	.nh_policy = NETISR_POLICY_SOURCE,
7056cb52192SRobert Watson 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
7067527624eSRobert Watson #endif
7076cb52192SRobert Watson };
7086cb52192SRobert Watson 
7096cb52192SRobert Watson static void
7106cb52192SRobert Watson ether_init(__unused void *arg)
7116cb52192SRobert Watson {
7126cb52192SRobert Watson 
7136cb52192SRobert Watson 	netisr_register(&ether_nh);
7146cb52192SRobert Watson }
7156cb52192SRobert Watson SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
7166cb52192SRobert Watson 
7176cb52192SRobert Watson static void
7187d4317bdSAlexander V. Chernikov vnet_ether_init(__unused void *arg)
7197d4317bdSAlexander V. Chernikov {
7207d4317bdSAlexander V. Chernikov 	int i;
7217d4317bdSAlexander V. Chernikov 
7227d4317bdSAlexander V. Chernikov 	/* Initialize packet filter hooks. */
7237d4317bdSAlexander V. Chernikov 	V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
7247d4317bdSAlexander V. Chernikov 	V_link_pfil_hook.ph_af = AF_LINK;
7257d4317bdSAlexander V. Chernikov 	if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
7267d4317bdSAlexander V. Chernikov 		printf("%s: WARNING: unable to register pfil link hook, "
7277d4317bdSAlexander V. Chernikov 			"error %d\n", __func__, i);
728484149deSBjoern A. Zeeb #ifdef VIMAGE
729484149deSBjoern A. Zeeb 	netisr_register_vnet(&ether_nh);
730484149deSBjoern A. Zeeb #endif
7317d4317bdSAlexander V. Chernikov }
7327d4317bdSAlexander V. Chernikov VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
7337d4317bdSAlexander V. Chernikov     vnet_ether_init, NULL);
7347d4317bdSAlexander V. Chernikov 
735484149deSBjoern A. Zeeb #ifdef VIMAGE
7367d4317bdSAlexander V. Chernikov static void
737484149deSBjoern A. Zeeb vnet_ether_pfil_destroy(__unused void *arg)
7387d4317bdSAlexander V. Chernikov {
7397d4317bdSAlexander V. Chernikov 	int i;
7407d4317bdSAlexander V. Chernikov 
7417d4317bdSAlexander V. Chernikov 	if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
7427d4317bdSAlexander V. Chernikov 		printf("%s: WARNING: unable to unregister pfil link hook, "
7437d4317bdSAlexander V. Chernikov 			"error %d\n", __func__, i);
7447d4317bdSAlexander V. Chernikov }
745484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY,
746484149deSBjoern A. Zeeb     vnet_ether_pfil_destroy, NULL);
747484149deSBjoern A. Zeeb 
748484149deSBjoern A. Zeeb static void
749484149deSBjoern A. Zeeb vnet_ether_destroy(__unused void *arg)
750484149deSBjoern A. Zeeb {
751484149deSBjoern A. Zeeb 
752484149deSBjoern A. Zeeb 	netisr_unregister_vnet(&ether_nh);
753484149deSBjoern A. Zeeb }
7547d4317bdSAlexander V. Chernikov VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
7557d4317bdSAlexander V. Chernikov     vnet_ether_destroy, NULL);
756484149deSBjoern A. Zeeb #endif
7577d4317bdSAlexander V. Chernikov 
7587d4317bdSAlexander V. Chernikov 
7597d4317bdSAlexander V. Chernikov 
7607d4317bdSAlexander V. Chernikov static void
7616cb52192SRobert Watson ether_input(struct ifnet *ifp, struct mbuf *m)
7626cb52192SRobert Watson {
7636cb52192SRobert Watson 
7644857f5fbSGeorge V. Neville-Neil 	struct mbuf *mn;
7654857f5fbSGeorge V. Neville-Neil 
7664857f5fbSGeorge V. Neville-Neil 	/*
7674857f5fbSGeorge V. Neville-Neil 	 * The drivers are allowed to pass in a chain of packets linked with
7684857f5fbSGeorge V. Neville-Neil 	 * m_nextpkt. We split them up into separate packets here and pass
7694857f5fbSGeorge V. Neville-Neil 	 * them up. This allows the drivers to amortize the receive lock.
7704857f5fbSGeorge V. Neville-Neil 	 */
7714857f5fbSGeorge V. Neville-Neil 	while (m) {
7724857f5fbSGeorge V. Neville-Neil 		mn = m->m_nextpkt;
7734857f5fbSGeorge V. Neville-Neil 		m->m_nextpkt = NULL;
7744857f5fbSGeorge V. Neville-Neil 
7756cb52192SRobert Watson 		/*
7766cb52192SRobert Watson 		 * We will rely on rcvif being set properly in the deferred context,
7776cb52192SRobert Watson 		 * so assert it is correct here.
7786cb52192SRobert Watson 		 */
779e84ef07fSBjoern A. Zeeb 		KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p "
780e84ef07fSBjoern A. Zeeb 		    "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp));
781484149deSBjoern A. Zeeb 		CURVNET_SET_QUIET(ifp->if_vnet);
7826cb52192SRobert Watson 		netisr_dispatch(NETISR_ETHER, m);
783484149deSBjoern A. Zeeb 		CURVNET_RESTORE();
7844857f5fbSGeorge V. Neville-Neil 		m = mn;
7854857f5fbSGeorge V. Neville-Neil 	}
7866cb52192SRobert Watson }
7876cb52192SRobert Watson 
7886cb52192SRobert Watson /*
789e1e1452dSArchie Cobbs  * Upper layer processing for a received Ethernet packet.
790e1e1452dSArchie Cobbs  */
791e1e1452dSArchie Cobbs void
792c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m)
793e1e1452dSArchie Cobbs {
794c1d93b05SSam Leffler 	struct ether_header *eh;
7957d4317bdSAlexander V. Chernikov 	int i, isr;
796e1e1452dSArchie Cobbs 	u_short ether_type;
79769462a82SBruce M Simpson 
79869462a82SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
79969462a82SBruce M Simpson 
8007d4317bdSAlexander V. Chernikov 	/* Do not grab PROMISC frames in case we are re-entered. */
8017d4317bdSAlexander V. Chernikov 	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
802effaab88SKristof Provost 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, 0,
803effaab88SKristof Provost 		    NULL);
8047d4317bdSAlexander V. Chernikov 
8057d4317bdSAlexander V. Chernikov 		if (i != 0 || m == NULL)
8067d4317bdSAlexander V. Chernikov 			return;
80769462a82SBruce M Simpson 	}
8087d4317bdSAlexander V. Chernikov 
809c1d93b05SSam Leffler 	eh = mtod(m, struct ether_header *);
810cd0cd014SJoerg Wunsch 	ether_type = ntohs(eh->ether_type);
811c1d93b05SSam Leffler 
812a9771948SGleb Smirnoff 	/*
81369462a82SBruce M Simpson 	 * If this frame has a VLAN tag other than 0, call vlan_input()
81469462a82SBruce M Simpson 	 * if its module is loaded. Otherwise, drop.
815a9771948SGleb Smirnoff 	 */
81669462a82SBruce M Simpson 	if ((m->m_flags & M_VLANTAG) &&
81769462a82SBruce M Simpson 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
81875ee267cSGleb Smirnoff 		if (ifp->if_vlantrunk == NULL) {
8193751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
8206c23e6ccSRuslan Ermilov 			m_freem(m);
8216c23e6ccSRuslan Ermilov 			return;
8226c23e6ccSRuslan Ermilov 		}
82369462a82SBruce M Simpson 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
82469462a82SBruce M Simpson 		    __func__));
82569462a82SBruce M Simpson 		/* Clear before possibly re-entering ether_input(). */
82669462a82SBruce M Simpson 		m->m_flags &= ~M_PROMISC;
827c1d93b05SSam Leffler 		(*vlan_input_p)(ifp, m);
828c1d93b05SSam Leffler 		return;
829c1d93b05SSam Leffler 	}
830c1d93b05SSam Leffler 
831c1d93b05SSam Leffler 	/*
83269462a82SBruce M Simpson 	 * Pass promiscuously received frames to the upper layer if the user
83369462a82SBruce M Simpson 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
834c1d93b05SSam Leffler 	 */
83569462a82SBruce M Simpson 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
836c1d93b05SSam Leffler 		m_freem(m);
837c1d93b05SSam Leffler 		return;
838c1d93b05SSam Leffler 	}
839c1d93b05SSam Leffler 
84069462a82SBruce M Simpson 	/*
84169462a82SBruce M Simpson 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
84269462a82SBruce M Simpson 	 * Strip off Ethernet header.
84369462a82SBruce M Simpson 	 */
84469462a82SBruce M Simpson 	m->m_flags &= ~M_VLANTAG;
84586bd0491SAndre Oppermann 	m_clrprotoflags(m);
846797f247bSMatthew N. Dodd 	m_adj(m, ETHER_HDR_LEN);
847c1d93b05SSam Leffler 
84869462a82SBruce M Simpson 	/*
84969462a82SBruce M Simpson 	 * Dispatch frame to upper layer.
85069462a82SBruce M Simpson 	 */
851307d80beSDavid Greenman 	switch (ether_type) {
852df8bae1dSRodney W. Grimes #ifdef INET
853df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
8541cafed39SJonathan Lemon 		isr = NETISR_IP;
855df8bae1dSRodney W. Grimes 		break;
856df8bae1dSRodney W. Grimes 
857df8bae1dSRodney W. Grimes 	case ETHERTYPE_ARP:
85808aadfbbSJonathan Lemon 		if (ifp->if_flags & IFF_NOARP) {
85908aadfbbSJonathan Lemon 			/* Discard packet if ARP is disabled on interface */
86008aadfbbSJonathan Lemon 			m_freem(m);
86108aadfbbSJonathan Lemon 			return;
86208aadfbbSJonathan Lemon 		}
8631cafed39SJonathan Lemon 		isr = NETISR_ARP;
864df8bae1dSRodney W. Grimes 		break;
865df8bae1dSRodney W. Grimes #endif
86682cd038dSYoshinobu Inoue #ifdef INET6
86782cd038dSYoshinobu Inoue 	case ETHERTYPE_IPV6:
8681cafed39SJonathan Lemon 		isr = NETISR_IPV6;
86982cd038dSYoshinobu Inoue 		break;
87082cd038dSYoshinobu Inoue #endif
871df8bae1dSRodney W. Grimes 	default:
8721cafed39SJonathan Lemon 		goto discard;
873df8bae1dSRodney W. Grimes 	}
8741cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
875c1d93b05SSam Leffler 	return;
8761cafed39SJonathan Lemon 
877c1d93b05SSam Leffler discard:
878c1d93b05SSam Leffler 	/*
879c1d93b05SSam Leffler 	 * Packet is to be discarded.  If netgraph is present,
880c1d93b05SSam Leffler 	 * hand the packet to it for last chance processing;
881c1d93b05SSam Leffler 	 * otherwise dispose of it.
882c1d93b05SSam Leffler 	 */
883833e8dc5SGleb Smirnoff 	if (ifp->if_l2com != NULL) {
884514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_input_orphan_p != NULL,
885514bcb89SPoul-Henning Kamp 		    ("ng_ether_input_orphan_p is NULL"));
886c1d93b05SSam Leffler 		/*
887c1d93b05SSam Leffler 		 * Put back the ethernet header so netgraph has a
888c1d93b05SSam Leffler 		 * consistent view of inbound packets.
889c1d93b05SSam Leffler 		 */
890eb1b1807SGleb Smirnoff 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
891c1d93b05SSam Leffler 		(*ng_ether_input_orphan_p)(ifp, m);
892c1d93b05SSam Leffler 		return;
893c1d93b05SSam Leffler 	}
894c1d93b05SSam Leffler 	m_freem(m);
895c1d93b05SSam Leffler }
896c1d93b05SSam Leffler 
897c1d93b05SSam Leffler /*
898c1d93b05SSam Leffler  * Convert Ethernet address to printable (loggable) representation.
899c1d93b05SSam Leffler  * This routine is for compatibility; it's better to just use
900c1d93b05SSam Leffler  *
901c1d93b05SSam Leffler  *	printf("%6D", <pointer to address>, ":");
902c1d93b05SSam Leffler  *
903c1d93b05SSam Leffler  * since there's no static buffer involved.
904c1d93b05SSam Leffler  */
905c1d93b05SSam Leffler char *
906c1d93b05SSam Leffler ether_sprintf(const u_char *ap)
907c1d93b05SSam Leffler {
908c1d93b05SSam Leffler 	static char etherbuf[18];
909c1d93b05SSam Leffler 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
910c1d93b05SSam Leffler 	return (etherbuf);
911df8bae1dSRodney W. Grimes }
912df8bae1dSRodney W. Grimes 
913df8bae1dSRodney W. Grimes /*
914df8bae1dSRodney W. Grimes  * Perform common duties while attaching to interface list
915df8bae1dSRodney W. Grimes  */
916df8bae1dSRodney W. Grimes void
917d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
918df8bae1dSRodney W. Grimes {
919f93dfa28SBrooks Davis 	int i;
920098a8c3bSMatthew N. Dodd 	struct ifaddr *ifa;
921098a8c3bSMatthew N. Dodd 	struct sockaddr_dl *sdl;
922df8bae1dSRodney W. Grimes 
923c1d93b05SSam Leffler 	ifp->if_addrlen = ETHER_ADDR_LEN;
924c1d93b05SSam Leffler 	ifp->if_hdrlen = ETHER_HDR_LEN;
925cfeff1b6SJonathan Lemon 	if_attach(ifp);
926df8bae1dSRodney W. Grimes 	ifp->if_mtu = ETHERMTU;
927c1d93b05SSam Leffler 	ifp->if_output = ether_output;
928c1d93b05SSam Leffler 	ifp->if_input = ether_input;
9291158dfb7SGarrett Wollman 	ifp->if_resolvemulti = ether_resolvemulti;
9304fb3a820SAlexander V. Chernikov 	ifp->if_requestencap = ether_requestencap;
931d3c351c5SMarko Zec #ifdef VIMAGE
932d3c351c5SMarko Zec 	ifp->if_reassign = ether_reassign;
933d3c351c5SMarko Zec #endif
934a330e1f1SGary Palmer 	if (ifp->if_baudrate == 0)
935c1d93b05SSam Leffler 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
936322dcb8dSMax Khon 	ifp->if_broadcastaddr = etherbroadcastaddr;
937c1d93b05SSam Leffler 
9384a0d6638SRuslan Ermilov 	ifa = ifp->if_addr;
9396e551fb6SDavid E. O'Brien 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
94059562606SGarrett Wollman 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
941df8bae1dSRodney W. Grimes 	sdl->sdl_type = IFT_ETHER;
942df8bae1dSRodney W. Grimes 	sdl->sdl_alen = ifp->if_addrlen;
943d09ed26fSRuslan Ermilov 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
944c1d93b05SSam Leffler 
9455b1a5e45SRavi Pokala 	if (ifp->if_hw_addr != NULL)
946ddae5750SRavi Pokala 		bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen);
947ddae5750SRavi Pokala 
948797f247bSMatthew N. Dodd 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
949e1e1452dSArchie Cobbs 	if (ng_ether_attach_p != NULL)
950e1e1452dSArchie Cobbs 		(*ng_ether_attach_p)(ifp);
951e3bbbec2SMatthew N. Dodd 
952f93dfa28SBrooks Davis 	/* Announce Ethernet MAC address if non-zero. */
953f93dfa28SBrooks Davis 	for (i = 0; i < ifp->if_addrlen; i++)
954d09ed26fSRuslan Ermilov 		if (lla[i] != 0)
955f93dfa28SBrooks Davis 			break;
956f93dfa28SBrooks Davis 	if (i != ifp->if_addrlen)
957d09ed26fSRuslan Ermilov 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
958ef1f9169SMarcel Moolenaar 
959ef1f9169SMarcel Moolenaar 	uuid_ether_add(LLADDR(sdl));
9608819ad85SSepherosa Ziehau 
9618819ad85SSepherosa Ziehau 	/* Add necessary bits are setup; announce it now. */
9628819ad85SSepherosa Ziehau 	EVENTHANDLER_INVOKE(ether_ifattach_event, ifp);
9638819ad85SSepherosa Ziehau 	if (IS_DEFAULT_VNET(curvnet))
9648819ad85SSepherosa Ziehau 		devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL);
965e1e1452dSArchie Cobbs }
966e1e1452dSArchie Cobbs 
96721b8ebd9SArchie Cobbs /*
96821b8ebd9SArchie Cobbs  * Perform common duties while detaching an Ethernet interface
96921b8ebd9SArchie Cobbs  */
97021b8ebd9SArchie Cobbs void
971c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp)
97221b8ebd9SArchie Cobbs {
973ef1f9169SMarcel Moolenaar 	struct sockaddr_dl *sdl;
974ef1f9169SMarcel Moolenaar 
975ef1f9169SMarcel Moolenaar 	sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
976ef1f9169SMarcel Moolenaar 	uuid_ether_del(LLADDR(sdl));
977ef1f9169SMarcel Moolenaar 
978833e8dc5SGleb Smirnoff 	if (ifp->if_l2com != NULL) {
979514bcb89SPoul-Henning Kamp 		KASSERT(ng_ether_detach_p != NULL,
980514bcb89SPoul-Henning Kamp 		    ("ng_ether_detach_p is NULL"));
98121b8ebd9SArchie Cobbs 		(*ng_ether_detach_p)(ifp);
982514bcb89SPoul-Henning Kamp 	}
98320a65f37SAndrew Thompson 
98421b8ebd9SArchie Cobbs 	bpfdetach(ifp);
98521b8ebd9SArchie Cobbs 	if_detach(ifp);
98621b8ebd9SArchie Cobbs }
98721b8ebd9SArchie Cobbs 
988d3c351c5SMarko Zec #ifdef VIMAGE
989d3c351c5SMarko Zec void
990d3c351c5SMarko Zec ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
991d3c351c5SMarko Zec {
992d3c351c5SMarko Zec 
993833e8dc5SGleb Smirnoff 	if (ifp->if_l2com != NULL) {
994d3c351c5SMarko Zec 		KASSERT(ng_ether_detach_p != NULL,
995d3c351c5SMarko Zec 		    ("ng_ether_detach_p is NULL"));
996d3c351c5SMarko Zec 		(*ng_ether_detach_p)(ifp);
997d3c351c5SMarko Zec 	}
998d3c351c5SMarko Zec 
999d3c351c5SMarko Zec 	if (ng_ether_attach_p != NULL) {
1000d3c351c5SMarko Zec 		CURVNET_SET_QUIET(new_vnet);
1001d3c351c5SMarko Zec 		(*ng_ether_attach_p)(ifp);
1002d3c351c5SMarko Zec 		CURVNET_RESTORE();
1003d3c351c5SMarko Zec 	}
1004d3c351c5SMarko Zec }
1005d3c351c5SMarko Zec #endif
1006d3c351c5SMarko Zec 
1007ce02431fSDoug Rabson SYSCTL_DECL(_net_link);
1008602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
100930106f6aSPoul-Henning Kamp 
101016b4a343SChristian Weisgerber #if 0
101116b4a343SChristian Weisgerber /*
101216b4a343SChristian Weisgerber  * This is for reference.  We have a table-driven version
101316b4a343SChristian Weisgerber  * of the little-endian crc32 generator, which is faster
101416b4a343SChristian Weisgerber  * than the double-loop.
101516b4a343SChristian Weisgerber  */
101616b4a343SChristian Weisgerber uint32_t
101716b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
101816b4a343SChristian Weisgerber {
101916b4a343SChristian Weisgerber 	size_t i;
102016b4a343SChristian Weisgerber 	uint32_t crc;
102116b4a343SChristian Weisgerber 	int bit;
102216b4a343SChristian Weisgerber 	uint8_t data;
102316b4a343SChristian Weisgerber 
102416b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
102516b4a343SChristian Weisgerber 
102616b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
1027933dad75SAntoine Brodin 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
102816b4a343SChristian Weisgerber 			carry = (crc ^ data) & 1;
102916b4a343SChristian Weisgerber 			crc >>= 1;
103016b4a343SChristian Weisgerber 			if (carry)
103116b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_LE);
103216b4a343SChristian Weisgerber 		}
1033933dad75SAntoine Brodin 	}
103416b4a343SChristian Weisgerber 
103516b4a343SChristian Weisgerber 	return (crc);
103616b4a343SChristian Weisgerber }
103716b4a343SChristian Weisgerber #else
103816b4a343SChristian Weisgerber uint32_t
103916b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len)
104016b4a343SChristian Weisgerber {
104116b4a343SChristian Weisgerber 	static const uint32_t crctab[] = {
104216b4a343SChristian Weisgerber 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
104316b4a343SChristian Weisgerber 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
104416b4a343SChristian Weisgerber 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
104516b4a343SChristian Weisgerber 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
104616b4a343SChristian Weisgerber 	};
104716b4a343SChristian Weisgerber 	size_t i;
104816b4a343SChristian Weisgerber 	uint32_t crc;
104916b4a343SChristian Weisgerber 
105016b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
105116b4a343SChristian Weisgerber 
105216b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
105316b4a343SChristian Weisgerber 		crc ^= buf[i];
105416b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
105516b4a343SChristian Weisgerber 		crc = (crc >> 4) ^ crctab[crc & 0xf];
105616b4a343SChristian Weisgerber 	}
105716b4a343SChristian Weisgerber 
105816b4a343SChristian Weisgerber 	return (crc);
105916b4a343SChristian Weisgerber }
106016b4a343SChristian Weisgerber #endif
106116b4a343SChristian Weisgerber 
106216b4a343SChristian Weisgerber uint32_t
106316b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len)
106416b4a343SChristian Weisgerber {
106516b4a343SChristian Weisgerber 	size_t i;
106616b4a343SChristian Weisgerber 	uint32_t crc, carry;
106716b4a343SChristian Weisgerber 	int bit;
106816b4a343SChristian Weisgerber 	uint8_t data;
106916b4a343SChristian Weisgerber 
107016b4a343SChristian Weisgerber 	crc = 0xffffffff;	/* initial value */
107116b4a343SChristian Weisgerber 
107216b4a343SChristian Weisgerber 	for (i = 0; i < len; i++) {
107316b4a343SChristian Weisgerber 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
107416b4a343SChristian Weisgerber 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
107516b4a343SChristian Weisgerber 			crc <<= 1;
107616b4a343SChristian Weisgerber 			if (carry)
107716b4a343SChristian Weisgerber 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
107816b4a343SChristian Weisgerber 		}
107916b4a343SChristian Weisgerber 	}
108016b4a343SChristian Weisgerber 
108116b4a343SChristian Weisgerber 	return (crc);
108216b4a343SChristian Weisgerber }
108316b4a343SChristian Weisgerber 
1084fb583156SDavid Greenman int
1085995c7fd1SYaroslav Tykhiy ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
108630106f6aSPoul-Henning Kamp {
108730106f6aSPoul-Henning Kamp 	struct ifaddr *ifa = (struct ifaddr *) data;
108830106f6aSPoul-Henning Kamp 	struct ifreq *ifr = (struct ifreq *) data;
1089fb583156SDavid Greenman 	int error = 0;
109030106f6aSPoul-Henning Kamp 
109130106f6aSPoul-Henning Kamp 	switch (command) {
109230106f6aSPoul-Henning Kamp 	case SIOCSIFADDR:
109330106f6aSPoul-Henning Kamp 		ifp->if_flags |= IFF_UP;
109430106f6aSPoul-Henning Kamp 
109530106f6aSPoul-Henning Kamp 		switch (ifa->ifa_addr->sa_family) {
109630106f6aSPoul-Henning Kamp #ifdef INET
109730106f6aSPoul-Henning Kamp 		case AF_INET:
109830106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1099322dcb8dSMax Khon 			arp_ifinit(ifp, ifa);
110030106f6aSPoul-Henning Kamp 			break;
110130106f6aSPoul-Henning Kamp #endif
110230106f6aSPoul-Henning Kamp 		default:
110330106f6aSPoul-Henning Kamp 			ifp->if_init(ifp->if_softc);
110430106f6aSPoul-Henning Kamp 			break;
110530106f6aSPoul-Henning Kamp 		}
110630106f6aSPoul-Henning Kamp 		break;
110730106f6aSPoul-Henning Kamp 
110830106f6aSPoul-Henning Kamp 	case SIOCGIFADDR:
110938d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
111038d958a6SBrooks Davis 		    ETHER_ADDR_LEN);
111130106f6aSPoul-Henning Kamp 		break;
1112fb583156SDavid Greenman 
1113fb583156SDavid Greenman 	case SIOCSIFMTU:
1114fb583156SDavid Greenman 		/*
1115fb583156SDavid Greenman 		 * Set the interface MTU.
1116fb583156SDavid Greenman 		 */
1117fb583156SDavid Greenman 		if (ifr->ifr_mtu > ETHERMTU) {
1118fb583156SDavid Greenman 			error = EINVAL;
1119fb583156SDavid Greenman 		} else {
1120fb583156SDavid Greenman 			ifp->if_mtu = ifr->ifr_mtu;
112130106f6aSPoul-Henning Kamp 		}
1122fb583156SDavid Greenman 		break;
1123f1379734SKonstantin Belousov 
1124f1379734SKonstantin Belousov 	case SIOCSLANPCP:
1125f1379734SKonstantin Belousov 		error = priv_check(curthread, PRIV_NET_SETLANPCP);
1126f1379734SKonstantin Belousov 		if (error != 0)
1127f1379734SKonstantin Belousov 			break;
1128f1379734SKonstantin Belousov 		if (ifr->ifr_lan_pcp > 7 &&
11294a381a9eSHans Petter Selasky 		    ifr->ifr_lan_pcp != IFNET_PCP_NONE) {
1130f1379734SKonstantin Belousov 			error = EINVAL;
11314a381a9eSHans Petter Selasky 		} else {
1132f1379734SKonstantin Belousov 			ifp->if_pcp = ifr->ifr_lan_pcp;
11334a381a9eSHans Petter Selasky 			/* broadcast event about PCP change */
11344a381a9eSHans Petter Selasky 			EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
11354a381a9eSHans Petter Selasky 		}
1136f1379734SKonstantin Belousov 		break;
1137f1379734SKonstantin Belousov 
1138f1379734SKonstantin Belousov 	case SIOCGLANPCP:
1139f1379734SKonstantin Belousov 		ifr->ifr_lan_pcp = ifp->if_pcp;
1140f1379734SKonstantin Belousov 		break;
1141f1379734SKonstantin Belousov 
1142c1d93b05SSam Leffler 	default:
1143c1d93b05SSam Leffler 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1144c1d93b05SSam Leffler 		break;
1145fb583156SDavid Greenman 	}
1146fb583156SDavid Greenman 	return (error);
114730106f6aSPoul-Henning Kamp }
11481158dfb7SGarrett Wollman 
114937c84183SPoul-Henning Kamp static int
115072fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
115172fd1b6aSDag-Erling Smørgrav 	struct sockaddr *sa)
11521158dfb7SGarrett Wollman {
11531158dfb7SGarrett Wollman 	struct sockaddr_dl *sdl;
1154d7647d96SDag-Erling Smørgrav #ifdef INET
11551158dfb7SGarrett Wollman 	struct sockaddr_in *sin;
1156d7647d96SDag-Erling Smørgrav #endif
115782cd038dSYoshinobu Inoue #ifdef INET6
115882cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6;
115982cd038dSYoshinobu Inoue #endif
11601158dfb7SGarrett Wollman 	u_char *e_addr;
11611158dfb7SGarrett Wollman 
11621158dfb7SGarrett Wollman 	switch(sa->sa_family) {
11631158dfb7SGarrett Wollman 	case AF_LINK:
11647f33a738SJulian Elischer 		/*
11657f33a738SJulian Elischer 		 * No mapping needed. Just check that it's a valid MC address.
11667f33a738SJulian Elischer 		 */
11671158dfb7SGarrett Wollman 		sdl = (struct sockaddr_dl *)sa;
11681158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
1169086e98c4SBruce M Simpson 		if (!ETHER_IS_MULTICAST(e_addr))
11701158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
1171155d72c4SPedro F. Giffuni 		*llsa = NULL;
11721158dfb7SGarrett Wollman 		return 0;
11731158dfb7SGarrett Wollman 
11741158dfb7SGarrett Wollman #ifdef INET
11751158dfb7SGarrett Wollman 	case AF_INET:
11761158dfb7SGarrett Wollman 		sin = (struct sockaddr_in *)sa;
11771158dfb7SGarrett Wollman 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
11781158dfb7SGarrett Wollman 			return EADDRNOTAVAIL;
117995fbe4d0SAlexander V. Chernikov 		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
11801158dfb7SGarrett Wollman 		sdl->sdl_alen = ETHER_ADDR_LEN;
11811158dfb7SGarrett Wollman 		e_addr = LLADDR(sdl);
11821158dfb7SGarrett Wollman 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
11831158dfb7SGarrett Wollman 		*llsa = (struct sockaddr *)sdl;
11841158dfb7SGarrett Wollman 		return 0;
11851158dfb7SGarrett Wollman #endif
118682cd038dSYoshinobu Inoue #ifdef INET6
118782cd038dSYoshinobu Inoue 	case AF_INET6:
118882cd038dSYoshinobu Inoue 		sin6 = (struct sockaddr_in6 *)sa;
1189595b8a1cSJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1190595b8a1cSJun-ichiro itojun Hagino 			/*
1191595b8a1cSJun-ichiro itojun Hagino 			 * An IP6 address of 0 means listen to all
1192595b8a1cSJun-ichiro itojun Hagino 			 * of the Ethernet multicast address used for IP6.
1193595b8a1cSJun-ichiro itojun Hagino 			 * (This is used for multicast routers.)
1194595b8a1cSJun-ichiro itojun Hagino 			 */
1195595b8a1cSJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_ALLMULTI;
1196155d72c4SPedro F. Giffuni 			*llsa = NULL;
1197595b8a1cSJun-ichiro itojun Hagino 			return 0;
1198595b8a1cSJun-ichiro itojun Hagino 		}
119982cd038dSYoshinobu Inoue 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
120082cd038dSYoshinobu Inoue 			return EADDRNOTAVAIL;
120195fbe4d0SAlexander V. Chernikov 		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
120282cd038dSYoshinobu Inoue 		sdl->sdl_alen = ETHER_ADDR_LEN;
120382cd038dSYoshinobu Inoue 		e_addr = LLADDR(sdl);
120482cd038dSYoshinobu Inoue 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
120582cd038dSYoshinobu Inoue 		*llsa = (struct sockaddr *)sdl;
120682cd038dSYoshinobu Inoue 		return 0;
120782cd038dSYoshinobu Inoue #endif
12081158dfb7SGarrett Wollman 
12091158dfb7SGarrett Wollman 	default:
12101158dfb7SGarrett Wollman 		/*
12111158dfb7SGarrett Wollman 		 * Well, the text isn't quite right, but it's the name
12121158dfb7SGarrett Wollman 		 * that counts...
12131158dfb7SGarrett Wollman 		 */
12141158dfb7SGarrett Wollman 		return EAFNOSUPPORT;
12151158dfb7SGarrett Wollman 	}
12161158dfb7SGarrett Wollman }
121740811c14SMatthew N. Dodd 
121840811c14SMatthew N. Dodd static moduledata_t ether_mod = {
1219833e8dc5SGleb Smirnoff 	.name = "ether",
122040811c14SMatthew N. Dodd };
122140811c14SMatthew N. Dodd 
122252f1277eSChristian S.J. Peron void
122352f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
122452f1277eSChristian S.J. Peron {
122552f1277eSChristian S.J. Peron 	struct ether_vlan_header vlan;
122652f1277eSChristian S.J. Peron 	struct mbuf mv, mb;
122752f1277eSChristian S.J. Peron 
122852f1277eSChristian S.J. Peron 	KASSERT((m->m_flags & M_VLANTAG) != 0,
122952f1277eSChristian S.J. Peron 	    ("%s: vlan information not present", __func__));
123052f1277eSChristian S.J. Peron 	KASSERT(m->m_len >= sizeof(struct ether_header),
123152f1277eSChristian S.J. Peron 	    ("%s: mbuf not large enough for header", __func__));
123252f1277eSChristian S.J. Peron 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
123352f1277eSChristian S.J. Peron 	vlan.evl_proto = vlan.evl_encap_proto;
123452f1277eSChristian S.J. Peron 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
123552f1277eSChristian S.J. Peron 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
123652f1277eSChristian S.J. Peron 	m->m_len -= sizeof(struct ether_header);
123752f1277eSChristian S.J. Peron 	m->m_data += sizeof(struct ether_header);
123852f1277eSChristian S.J. Peron 	/*
123952f1277eSChristian S.J. Peron 	 * If a data link has been supplied by the caller, then we will need to
124052f1277eSChristian S.J. Peron 	 * re-create a stack allocated mbuf chain with the following structure:
124152f1277eSChristian S.J. Peron 	 *
124252f1277eSChristian S.J. Peron 	 * (1) mbuf #1 will contain the supplied data link
124352f1277eSChristian S.J. Peron 	 * (2) mbuf #2 will contain the vlan header
124452f1277eSChristian S.J. Peron 	 * (3) mbuf #3 will contain the original mbuf's packet data
124552f1277eSChristian S.J. Peron 	 *
124652f1277eSChristian S.J. Peron 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
124752f1277eSChristian S.J. Peron 	 */
124852f1277eSChristian S.J. Peron 	if (data != NULL) {
124952f1277eSChristian S.J. Peron 		mv.m_next = m;
125052f1277eSChristian S.J. Peron 		mv.m_data = (caddr_t)&vlan;
125152f1277eSChristian S.J. Peron 		mv.m_len = sizeof(vlan);
125252f1277eSChristian S.J. Peron 		mb.m_next = &mv;
125352f1277eSChristian S.J. Peron 		mb.m_data = data;
125452f1277eSChristian S.J. Peron 		mb.m_len = dlen;
125552f1277eSChristian S.J. Peron 		bpf_mtap(bp, &mb);
125652f1277eSChristian S.J. Peron 	} else
125752f1277eSChristian S.J. Peron 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
125852f1277eSChristian S.J. Peron 	m->m_len += sizeof(struct ether_header);
125952f1277eSChristian S.J. Peron 	m->m_data -= sizeof(struct ether_header);
126052f1277eSChristian S.J. Peron }
126152f1277eSChristian S.J. Peron 
126260e87ca8SAndrew Thompson struct mbuf *
1263a0cf8186SAndrew Thompson ether_vlanencap(struct mbuf *m, uint16_t tag)
126460e87ca8SAndrew Thompson {
126560e87ca8SAndrew Thompson 	struct ether_vlan_header *evl;
126660e87ca8SAndrew Thompson 
1267eb1b1807SGleb Smirnoff 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
126860e87ca8SAndrew Thompson 	if (m == NULL)
126960e87ca8SAndrew Thompson 		return (NULL);
127060e87ca8SAndrew Thompson 	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
127160e87ca8SAndrew Thompson 
127260e87ca8SAndrew Thompson 	if (m->m_len < sizeof(*evl)) {
127360e87ca8SAndrew Thompson 		m = m_pullup(m, sizeof(*evl));
127460e87ca8SAndrew Thompson 		if (m == NULL)
127560e87ca8SAndrew Thompson 			return (NULL);
127660e87ca8SAndrew Thompson 	}
127760e87ca8SAndrew Thompson 
127860e87ca8SAndrew Thompson 	/*
127960e87ca8SAndrew Thompson 	 * Transform the Ethernet header into an Ethernet header
128060e87ca8SAndrew Thompson 	 * with 802.1Q encapsulation.
128160e87ca8SAndrew Thompson 	 */
128260e87ca8SAndrew Thompson 	evl = mtod(m, struct ether_vlan_header *);
128360e87ca8SAndrew Thompson 	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
128460e87ca8SAndrew Thompson 	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
128560e87ca8SAndrew Thompson 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
128660e87ca8SAndrew Thompson 	evl->evl_tag = htons(tag);
128760e87ca8SAndrew Thompson 	return (m);
128860e87ca8SAndrew Thompson }
128960e87ca8SAndrew Thompson 
1290f1379734SKonstantin Belousov static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
1291f1379734SKonstantin Belousov     "IEEE 802.1Q VLAN");
1292f1379734SKonstantin Belousov static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
1293f1379734SKonstantin Belousov     "for consistency");
1294f1379734SKonstantin Belousov 
12955f901c92SAndrew Turner VNET_DEFINE_STATIC(int, soft_pad);
1296f1379734SKonstantin Belousov #define	V_soft_pad	VNET(soft_pad)
1297f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET,
1298f1379734SKonstantin Belousov     &VNET_NAME(soft_pad), 0,
1299f1379734SKonstantin Belousov     "pad short frames before tagging");
1300f1379734SKonstantin Belousov 
1301f1379734SKonstantin Belousov /*
1302f1379734SKonstantin Belousov  * For now, make preserving PCP via an mbuf tag optional, as it increases
1303f1379734SKonstantin Belousov  * per-packet memory allocations and frees.  In the future, it would be
1304f1379734SKonstantin Belousov  * preferable to reuse ether_vtag for this, or similar.
1305f1379734SKonstantin Belousov  */
1306f1379734SKonstantin Belousov int vlan_mtag_pcp = 0;
1307f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW,
1308f1379734SKonstantin Belousov     &vlan_mtag_pcp, 0,
1309f1379734SKonstantin Belousov     "Retain VLAN PCP information as packets are passed up the stack");
1310f1379734SKonstantin Belousov 
1311f1379734SKonstantin Belousov bool
1312f1379734SKonstantin Belousov ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p,
1313f1379734SKonstantin Belousov     uint16_t vid, uint8_t pcp)
1314f1379734SKonstantin Belousov {
1315f1379734SKonstantin Belousov 	struct m_tag *mtag;
1316f1379734SKonstantin Belousov 	int n;
1317f1379734SKonstantin Belousov 	uint16_t tag;
1318f1379734SKonstantin Belousov 	static const char pad[8];	/* just zeros */
1319f1379734SKonstantin Belousov 
1320f1379734SKonstantin Belousov 	/*
1321f1379734SKonstantin Belousov 	 * Pad the frame to the minimum size allowed if told to.
1322f1379734SKonstantin Belousov 	 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1323f1379734SKonstantin Belousov 	 * paragraph C.4.4.3.b.  It can help to work around buggy
1324f1379734SKonstantin Belousov 	 * bridges that violate paragraph C.4.4.3.a from the same
1325f1379734SKonstantin Belousov 	 * document, i.e., fail to pad short frames after untagging.
1326f1379734SKonstantin Belousov 	 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1327f1379734SKonstantin Belousov 	 * untagging it will produce a 62-byte frame, which is a runt
1328f1379734SKonstantin Belousov 	 * and requires padding.  There are VLAN-enabled network
1329f1379734SKonstantin Belousov 	 * devices that just discard such runts instead or mishandle
1330f1379734SKonstantin Belousov 	 * them somehow.
1331f1379734SKonstantin Belousov 	 */
1332f1379734SKonstantin Belousov 	if (V_soft_pad && p->if_type == IFT_ETHER) {
1333f1379734SKonstantin Belousov 		for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len;
1334f1379734SKonstantin Belousov 		     n > 0; n -= sizeof(pad)) {
1335f1379734SKonstantin Belousov 			if (!m_append(*mp, min(n, sizeof(pad)), pad))
1336f1379734SKonstantin Belousov 				break;
1337f1379734SKonstantin Belousov 		}
1338f1379734SKonstantin Belousov 		if (n > 0) {
1339f1379734SKonstantin Belousov 			m_freem(*mp);
1340f1379734SKonstantin Belousov 			*mp = NULL;
1341f1379734SKonstantin Belousov 			if_printf(ife, "cannot pad short frame");
1342f1379734SKonstantin Belousov 			return (false);
1343f1379734SKonstantin Belousov 		}
1344f1379734SKonstantin Belousov 	}
1345f1379734SKonstantin Belousov 
1346f1379734SKonstantin Belousov 	/*
1347f1379734SKonstantin Belousov 	 * If underlying interface can do VLAN tag insertion itself,
1348f1379734SKonstantin Belousov 	 * just pass the packet along. However, we need some way to
1349f1379734SKonstantin Belousov 	 * tell the interface where the packet came from so that it
1350f1379734SKonstantin Belousov 	 * knows how to find the VLAN tag to use, so we attach a
1351f1379734SKonstantin Belousov 	 * packet tag that holds it.
1352f1379734SKonstantin Belousov 	 */
1353f1379734SKonstantin Belousov 	if (vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q,
1354f1379734SKonstantin Belousov 	    MTAG_8021Q_PCP_OUT, NULL)) != NULL)
1355f1379734SKonstantin Belousov 		tag = EVL_MAKETAG(vid, *(uint8_t *)(mtag + 1), 0);
1356f1379734SKonstantin Belousov 	else
1357f1379734SKonstantin Belousov 		tag = EVL_MAKETAG(vid, pcp, 0);
1358f1379734SKonstantin Belousov 	if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1359f1379734SKonstantin Belousov 		(*mp)->m_pkthdr.ether_vtag = tag;
1360f1379734SKonstantin Belousov 		(*mp)->m_flags |= M_VLANTAG;
1361f1379734SKonstantin Belousov 	} else {
1362f1379734SKonstantin Belousov 		*mp = ether_vlanencap(*mp, tag);
1363f1379734SKonstantin Belousov 		if (*mp == NULL) {
1364f1379734SKonstantin Belousov 			if_printf(ife, "unable to prepend 802.1Q header");
1365f1379734SKonstantin Belousov 			return (false);
1366f1379734SKonstantin Belousov 		}
1367f1379734SKonstantin Belousov 	}
1368f1379734SKonstantin Belousov 	return (true);
1369f1379734SKonstantin Belousov }
1370f1379734SKonstantin Belousov 
1371fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
137240811c14SMatthew N. Dodd MODULE_VERSION(ether, 1);
1373