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> 43773e541eSWarner Losh #include <sys/devctl.h> 448819ad85SSepherosa Ziehau #include <sys/eventhandler.h> 453c3aa8c1SKyle Evans #include <sys/jail.h> 46df8bae1dSRodney W. Grimes #include <sys/kernel.h> 47385195c0SMarko Zec #include <sys/lock.h> 48df8bae1dSRodney W. Grimes #include <sys/malloc.h> 49df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 5024fe4612SMark Johnston #include <sys/module.h> 5124fe4612SMark Johnston #include <sys/msan.h> 523c3aa8c1SKyle Evans #include <sys/proc.h> 53f1379734SKonstantin Belousov #include <sys/priv.h> 5410b1fde0SMark Murray #include <sys/random.h> 55df8bae1dSRodney W. Grimes #include <sys/socket.h> 5651a53488SBruce Evans #include <sys/sockio.h> 57602d513cSGarrett Wollman #include <sys/sysctl.h> 58ef1f9169SMarcel Moolenaar #include <sys/uuid.h> 59df8bae1dSRodney W. Grimes 603c3aa8c1SKyle Evans #include <net/ieee_oui.h> 61df8bae1dSRodney W. Grimes #include <net/if.h> 6276039bc8SGleb Smirnoff #include <net/if_var.h> 63d7647d96SDag-Erling Smørgrav #include <net/if_arp.h> 64df8bae1dSRodney W. Grimes #include <net/netisr.h> 65df8bae1dSRodney W. Grimes #include <net/route.h> 66df8bae1dSRodney W. Grimes #include <net/if_llc.h> 67df8bae1dSRodney W. Grimes #include <net/if_dl.h> 68df8bae1dSRodney W. Grimes #include <net/if_types.h> 692e2de7f2SArchie Cobbs #include <net/bpf.h> 70e1e1452dSArchie Cobbs #include <net/ethernet.h> 71fd6238a6SAndrew Thompson #include <net/if_bridgevar.h> 72c1d93b05SSam Leffler #include <net/if_vlan_var.h> 736e6b3f7cSQing Li #include <net/if_llatbl.h> 747d4317bdSAlexander V. Chernikov #include <net/pfil.h> 75b2bdc62aSAdrian Chadd #include <net/rss_config.h> 764b79449eSBjoern A. Zeeb #include <net/vnet.h> 77df8bae1dSRodney W. Grimes 7875bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h> 7975bf2db3SGleb Smirnoff 8082cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6) 81df8bae1dSRodney W. Grimes #include <netinet/in.h> 82df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 83df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 8454bfbd51SWill Andrews #include <netinet/ip_carp.h> 850bcfa8e4SLuigi Rizzo #include <netinet/ip_var.h> 861d5e9e22SEivind Eklund #endif 8782cd038dSYoshinobu Inoue #ifdef INET6 8882cd038dSYoshinobu Inoue #include <netinet6/nd6.h> 8982cd038dSYoshinobu Inoue #endif 90aed55708SRobert Watson #include <security/mac/mac_framework.h> 91aed55708SRobert Watson 923c3aa8c1SKyle Evans #include <crypto/sha1.h> 933c3aa8c1SKyle Evans 94d54d93acSEd Maste #ifdef CTASSERT 95d54d93acSEd Maste CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2); 96d54d93acSEd Maste CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN); 97d54d93acSEd Maste #endif 98d54d93acSEd Maste 99b252313fSGleb Smirnoff VNET_DEFINE(pfil_head_t, link_pfil_head); /* Packet filter hooks */ 1007d4317bdSAlexander V. Chernikov 101e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */ 102c1d93b05SSam Leffler void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 103c1d93b05SSam Leffler void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 104e1e1452dSArchie Cobbs int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 105e1e1452dSArchie Cobbs void (*ng_ether_attach_p)(struct ifnet *ifp); 106e1e1452dSArchie Cobbs void (*ng_ether_detach_p)(struct ifnet *ifp); 107e1e1452dSArchie Cobbs 108824eb9dcSDavid E. O'Brien void (*vlan_input_p)(struct ifnet *, struct mbuf *); 1099d4fe4b2SBrooks Davis 110fd6238a6SAndrew Thompson /* if_bridge(4) support */ 111c8b01292SAndrew Thompson void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 1128f867517SAndrew Thompson 11318242d3bSAndrew Thompson /* if_lagg(4) support */ 114a92c4bb6SHans Petter Selasky struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *); 115b47888ceSAndrew Thompson 116e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 117868d8b62SMatthew N. Dodd { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 118868d8b62SMatthew N. Dodd 119929ddbbbSAlfred Perlstein static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 120929ddbbbSAlfred Perlstein struct sockaddr *); 1214fb3a820SAlexander V. Chernikov static int ether_requestencap(struct ifnet *, struct if_encap_req *); 122868d8b62SMatthew N. Dodd 1234c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0) 124df8bae1dSRodney W. Grimes 12522f8ce43SAlexander V. Chernikov static void 12622f8ce43SAlexander V. Chernikov update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 12722f8ce43SAlexander V. Chernikov { 12822f8ce43SAlexander V. Chernikov int csum_flags = 0; 12922f8ce43SAlexander V. Chernikov 13022f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_IP) 13122f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 13222f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 13322f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 13422f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_SCTP) 13522f8ce43SAlexander V. Chernikov csum_flags |= CSUM_SCTP_VALID; 13622f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_flags |= csum_flags; 13722f8ce43SAlexander V. Chernikov if (csum_flags & CSUM_DATA_VALID) 13822f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_data = 0xffff; 13922f8ce43SAlexander V. Chernikov } 14022f8ce43SAlexander V. Chernikov 141df8bae1dSRodney W. Grimes /* 1424fb3a820SAlexander V. Chernikov * Handle link-layer encapsulation requests. 1434fb3a820SAlexander V. Chernikov */ 1444fb3a820SAlexander V. Chernikov static int 1454fb3a820SAlexander V. Chernikov ether_requestencap(struct ifnet *ifp, struct if_encap_req *req) 1464fb3a820SAlexander V. Chernikov { 1474fb3a820SAlexander V. Chernikov struct ether_header *eh; 1484fb3a820SAlexander V. Chernikov struct arphdr *ah; 1494fb3a820SAlexander V. Chernikov uint16_t etype; 1504fb3a820SAlexander V. Chernikov const u_char *lladdr; 1514fb3a820SAlexander V. Chernikov 1524fb3a820SAlexander V. Chernikov if (req->rtype != IFENCAP_LL) 1534fb3a820SAlexander V. Chernikov return (EOPNOTSUPP); 1544fb3a820SAlexander V. Chernikov 1554fb3a820SAlexander V. Chernikov if (req->bufsize < ETHER_HDR_LEN) 1564fb3a820SAlexander V. Chernikov return (ENOMEM); 1574fb3a820SAlexander V. Chernikov 1584fb3a820SAlexander V. Chernikov eh = (struct ether_header *)req->buf; 1594fb3a820SAlexander V. Chernikov lladdr = req->lladdr; 1604fb3a820SAlexander V. Chernikov req->lladdr_off = 0; 1614fb3a820SAlexander V. Chernikov 1624fb3a820SAlexander V. Chernikov switch (req->family) { 1634fb3a820SAlexander V. Chernikov case AF_INET: 1644fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IP); 1654fb3a820SAlexander V. Chernikov break; 1664fb3a820SAlexander V. Chernikov case AF_INET6: 1674fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 1684fb3a820SAlexander V. Chernikov break; 1694fb3a820SAlexander V. Chernikov case AF_ARP: 1704fb3a820SAlexander V. Chernikov ah = (struct arphdr *)req->hdata; 1714fb3a820SAlexander V. Chernikov ah->ar_hrd = htons(ARPHRD_ETHER); 1724fb3a820SAlexander V. Chernikov 1734fb3a820SAlexander V. Chernikov switch(ntohs(ah->ar_op)) { 1744fb3a820SAlexander V. Chernikov case ARPOP_REVREQUEST: 1754fb3a820SAlexander V. Chernikov case ARPOP_REVREPLY: 1764fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_REVARP); 1774fb3a820SAlexander V. Chernikov break; 1784fb3a820SAlexander V. Chernikov case ARPOP_REQUEST: 1794fb3a820SAlexander V. Chernikov case ARPOP_REPLY: 1804fb3a820SAlexander V. Chernikov default: 1814fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_ARP); 1824fb3a820SAlexander V. Chernikov break; 1834fb3a820SAlexander V. Chernikov } 1844fb3a820SAlexander V. Chernikov 1854fb3a820SAlexander V. Chernikov if (req->flags & IFENCAP_FLAG_BROADCAST) 1864fb3a820SAlexander V. Chernikov lladdr = ifp->if_broadcastaddr; 1874fb3a820SAlexander V. Chernikov break; 1884fb3a820SAlexander V. Chernikov default: 1894fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 1904fb3a820SAlexander V. Chernikov } 1914fb3a820SAlexander V. Chernikov 1924fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 1934fb3a820SAlexander V. Chernikov memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN); 1944fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 1954fb3a820SAlexander V. Chernikov req->bufsize = sizeof(struct ether_header); 1964fb3a820SAlexander V. Chernikov 1974fb3a820SAlexander V. Chernikov return (0); 1984fb3a820SAlexander V. Chernikov } 1994fb3a820SAlexander V. Chernikov 2004fb3a820SAlexander V. Chernikov static int 2014fb3a820SAlexander V. Chernikov ether_resolve_addr(struct ifnet *ifp, struct mbuf *m, 2024fb3a820SAlexander V. Chernikov const struct sockaddr *dst, struct route *ro, u_char *phdr, 2036d768226SGeorge V. Neville-Neil uint32_t *pflags, struct llentry **plle) 2044fb3a820SAlexander V. Chernikov { 2054fb3a820SAlexander V. Chernikov struct ether_header *eh; 2064fb3a820SAlexander V. Chernikov uint32_t lleflags = 0; 2074fb3a820SAlexander V. Chernikov int error = 0; 2084fb3a820SAlexander V. Chernikov #if defined(INET) || defined(INET6) 2094fb3a820SAlexander V. Chernikov uint16_t etype; 2104fb3a820SAlexander V. Chernikov #endif 2114fb3a820SAlexander V. Chernikov 2126d768226SGeorge V. Neville-Neil if (plle) 2136d768226SGeorge V. Neville-Neil *plle = NULL; 2144fb3a820SAlexander V. Chernikov eh = (struct ether_header *)phdr; 2154fb3a820SAlexander V. Chernikov 2164fb3a820SAlexander V. Chernikov switch (dst->sa_family) { 2174fb3a820SAlexander V. Chernikov #ifdef INET 2184fb3a820SAlexander V. Chernikov case AF_INET: 2194fb3a820SAlexander V. Chernikov if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) 2206d768226SGeorge V. Neville-Neil error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, 2216d768226SGeorge V. Neville-Neil plle); 2224fb3a820SAlexander V. Chernikov else { 2234fb3a820SAlexander V. Chernikov if (m->m_flags & M_BCAST) 2244fb3a820SAlexander V. Chernikov memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 2254fb3a820SAlexander V. Chernikov ETHER_ADDR_LEN); 2264fb3a820SAlexander V. Chernikov else { 2274fb3a820SAlexander V. Chernikov const struct in_addr *a; 2284fb3a820SAlexander V. Chernikov a = &(((const struct sockaddr_in *)dst)->sin_addr); 2294fb3a820SAlexander V. Chernikov ETHER_MAP_IP_MULTICAST(a, eh->ether_dhost); 2304fb3a820SAlexander V. Chernikov } 2314fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IP); 2324fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(etype)); 2334fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 2344fb3a820SAlexander V. Chernikov } 2354fb3a820SAlexander V. Chernikov break; 2364fb3a820SAlexander V. Chernikov #endif 2374fb3a820SAlexander V. Chernikov #ifdef INET6 2384fb3a820SAlexander V. Chernikov case AF_INET6: 239*62e1a437SZhenlei Huang if ((m->m_flags & M_MCAST) == 0) { 240*62e1a437SZhenlei Huang int af = RO_GET_FAMILY(ro, dst); 241*62e1a437SZhenlei Huang error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr, 242c541bd36SAlexander V. Chernikov &lleflags, plle); 243*62e1a437SZhenlei Huang } else { 2444fb3a820SAlexander V. Chernikov const struct in6_addr *a6; 2454fb3a820SAlexander V. Chernikov a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr); 2464fb3a820SAlexander V. Chernikov ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost); 2474fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 2484fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(etype)); 2494fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 2504fb3a820SAlexander V. Chernikov } 2514fb3a820SAlexander V. Chernikov break; 2524fb3a820SAlexander V. Chernikov #endif 2534fb3a820SAlexander V. Chernikov default: 2544fb3a820SAlexander V. Chernikov if_printf(ifp, "can't handle af%d\n", dst->sa_family); 2554fb3a820SAlexander V. Chernikov if (m != NULL) 2564fb3a820SAlexander V. Chernikov m_freem(m); 2574fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 2584fb3a820SAlexander V. Chernikov } 2594fb3a820SAlexander V. Chernikov 2604fb3a820SAlexander V. Chernikov if (error == EHOSTDOWN) { 26136402a68SAlexander V. Chernikov if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 2624fb3a820SAlexander V. Chernikov error = EHOSTUNREACH; 2634fb3a820SAlexander V. Chernikov } 2644fb3a820SAlexander V. Chernikov 2654fb3a820SAlexander V. Chernikov if (error != 0) 2664fb3a820SAlexander V. Chernikov return (error); 2674fb3a820SAlexander V. Chernikov 2684fb3a820SAlexander V. Chernikov *pflags = RT_MAY_LOOP; 2694fb3a820SAlexander V. Chernikov if (lleflags & LLE_IFADDR) 2704fb3a820SAlexander V. Chernikov *pflags |= RT_L2_ME; 2714fb3a820SAlexander V. Chernikov 2724fb3a820SAlexander V. Chernikov return (0); 2734fb3a820SAlexander V. Chernikov } 2744fb3a820SAlexander V. Chernikov 2754fb3a820SAlexander V. Chernikov /* 276df8bae1dSRodney W. Grimes * Ethernet output routine. 277df8bae1dSRodney W. Grimes * Encapsulate a packet of type family for the local net. 278df8bae1dSRodney W. Grimes * Use trailer local net encapsulation if enough data in first 279df8bae1dSRodney W. Grimes * packet leaves a multiple of 512 bytes of data in remainder. 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes int 28272fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m, 28347e8d432SGleb Smirnoff const struct sockaddr *dst, struct route *ro) 284df8bae1dSRodney W. Grimes { 2854fb3a820SAlexander V. Chernikov int error = 0; 2864fb3a820SAlexander V. Chernikov char linkhdr[ETHER_HDR_LEN], *phdr; 287098a8c3bSMatthew N. Dodd struct ether_header *eh; 28860ee3847SMax Laier struct pf_mtag *t; 289ddce63fcSHans Petter Selasky bool loop_copy; 290d32864c3SRobert Watson int hlen; /* link layer header length */ 2914fb3a820SAlexander V. Chernikov uint32_t pflags; 2926d768226SGeorge V. Neville-Neil struct llentry *lle = NULL; 2936d768226SGeorge V. Neville-Neil int addref = 0; 294df8bae1dSRodney W. Grimes 2954fb3a820SAlexander V. Chernikov phdr = NULL; 2964fb3a820SAlexander V. Chernikov pflags = 0; 297279aa3d4SKip Macy if (ro != NULL) { 2986d768226SGeorge V. Neville-Neil /* XXX BPF uses ro_prepend */ 2996d768226SGeorge V. Neville-Neil if (ro->ro_prepend != NULL) { 3004fb3a820SAlexander V. Chernikov phdr = ro->ro_prepend; 3014fb3a820SAlexander V. Chernikov hlen = ro->ro_plen; 3026d768226SGeorge V. Neville-Neil } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 3036d768226SGeorge V. Neville-Neil if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 3046d768226SGeorge V. Neville-Neil lle = ro->ro_lle; 3056d768226SGeorge V. Neville-Neil if (lle != NULL && 3066d768226SGeorge V. Neville-Neil (lle->la_flags & LLE_VALID) == 0) { 3076d768226SGeorge V. Neville-Neil LLE_FREE(lle); 3086d768226SGeorge V. Neville-Neil lle = NULL; /* redundant */ 3096d768226SGeorge V. Neville-Neil ro->ro_lle = NULL; 310279aa3d4SKip Macy } 3116d768226SGeorge V. Neville-Neil if (lle == NULL) { 3126d768226SGeorge V. Neville-Neil /* if we lookup, keep cache */ 3136d768226SGeorge V. Neville-Neil addref = 1; 3141435dcd9SAlexander V. Chernikov } else 3151435dcd9SAlexander V. Chernikov /* 3161435dcd9SAlexander V. Chernikov * Notify LLE code that 3171435dcd9SAlexander V. Chernikov * the entry was used 3181435dcd9SAlexander V. Chernikov * by datapath. 3191435dcd9SAlexander V. Chernikov */ 320f3a3b061SAlexander V. Chernikov llentry_provide_feedback(lle); 3216d768226SGeorge V. Neville-Neil } 3226d768226SGeorge V. Neville-Neil if (lle != NULL) { 3236d768226SGeorge V. Neville-Neil phdr = lle->r_linkdata; 3246d768226SGeorge V. Neville-Neil hlen = lle->r_hdrlen; 3256d768226SGeorge V. Neville-Neil pflags = lle->r_flags; 3266d768226SGeorge V. Neville-Neil } 3276d768226SGeorge V. Neville-Neil } 3286d768226SGeorge V. Neville-Neil } 3296d768226SGeorge V. Neville-Neil 33043b29369SRobert Watson #ifdef MAC 33130d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 33243b29369SRobert Watson if (error) 33343b29369SRobert Watson senderr(error); 33443b29369SRobert Watson #endif 33543b29369SRobert Watson 3366eeac1d9SJulian Elischer M_PROFILE(m); 337afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) 338afbe3a0fSPoul-Henning Kamp senderr(ENETDOWN); 33913f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 34013f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 341df8bae1dSRodney W. Grimes senderr(ENETDOWN); 3427f760c48SMatthew N. Dodd 3434fb3a820SAlexander V. Chernikov if (phdr == NULL) { 3444fb3a820SAlexander V. Chernikov /* No prepend data supplied. Try to calculate ourselves. */ 3454fb3a820SAlexander V. Chernikov phdr = linkhdr; 34684dd0fd0SJulian Elischer hlen = ETHER_HDR_LEN; 3476d768226SGeorge V. Neville-Neil error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 3486d768226SGeorge V. Neville-Neil addref ? &lle : NULL); 3496d768226SGeorge V. Neville-Neil if (addref && lle != NULL) 3506d768226SGeorge V. Neville-Neil ro->ro_lle = lle; 3514fb3a820SAlexander V. Chernikov if (error != 0) 352cd46a114SLuigi Rizzo return (error == EWOULDBLOCK ? 0 : error); 35343a6c75aSMatthew N. Dodd } 35443a6c75aSMatthew N. Dodd 3554fb3a820SAlexander V. Chernikov if ((pflags & RT_L2_ME) != 0) { 35622f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, m); 357*62e1a437SZhenlei Huang return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0)); 3586e6b3f7cSQing Li } 359ddce63fcSHans Petter Selasky loop_copy = (pflags & RT_MAY_LOOP) != 0; 3606e6b3f7cSQing Li 361df8bae1dSRodney W. Grimes /* 362df8bae1dSRodney W. Grimes * Add local net header. If no space in first mbuf, 363df8bae1dSRodney W. Grimes * allocate another. 3646cdb1854SAlexander V. Chernikov * 3656cdb1854SAlexander V. Chernikov * Note that we do prepend regardless of RT_HAS_HEADER flag. 3666cdb1854SAlexander V. Chernikov * This is done because BPF code shifts m_data pointer 3676cdb1854SAlexander V. Chernikov * to the end of ethernet header prior to calling if_output(). 368df8bae1dSRodney W. Grimes */ 3694fb3a820SAlexander V. Chernikov M_PREPEND(m, hlen, M_NOWAIT); 3702166ffe8SRobert Watson if (m == NULL) 371df8bae1dSRodney W. Grimes senderr(ENOBUFS); 3724fb3a820SAlexander V. Chernikov if ((pflags & RT_HAS_HEADER) == 0) { 373df8bae1dSRodney W. Grimes eh = mtod(m, struct ether_header *); 3744fb3a820SAlexander V. Chernikov memcpy(eh, phdr, hlen); 375c69aeaadSAlexander V. Chernikov } 376ed7509acSJulian Elischer 377ed7509acSJulian Elischer /* 378ed7509acSJulian Elischer * If a simplex interface, and the packet is being sent to our 379ed7509acSJulian Elischer * Ethernet address or a broadcast address, loopback a copy. 380ed7509acSJulian Elischer * XXX To make a simplex device behave exactly like a duplex 381ed7509acSJulian Elischer * device, we should copy in the case of sending to our own 382ed7509acSJulian Elischer * ethernet address (thus letting the original actually appear 383ed7509acSJulian Elischer * on the wire). However, we don't do that here for security 384ed7509acSJulian Elischer * reasons and compatibility with the original behavior. 385ed7509acSJulian Elischer */ 3864fb3a820SAlexander V. Chernikov if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) && 38760ee3847SMax Laier ((t = pf_find_mtag(m)) == NULL || !t->routed)) { 38806f684b0SMatthew N. Dodd struct mbuf *n; 389ed7509acSJulian Elischer 390294dd290SLuigi Rizzo /* 391294dd290SLuigi Rizzo * Because if_simloop() modifies the packet, we need a 392294dd290SLuigi Rizzo * writable copy through m_dup() instead of a readonly 393294dd290SLuigi Rizzo * one as m_copy[m] would give us. The alternative would 394294dd290SLuigi Rizzo * be to modify if_simloop() to handle the readonly mbuf, 395294dd290SLuigi Rizzo * but performancewise it is mostly equivalent (trading 396294dd290SLuigi Rizzo * extra data copying vs. extra locking). 3979983b3c0SYaroslav Tykhiy * 3989983b3c0SYaroslav Tykhiy * XXX This is a local workaround. A number of less 3999983b3c0SYaroslav Tykhiy * often used kernel parts suffer from the same bug. 4009983b3c0SYaroslav Tykhiy * See PR kern/105943 for a proposed general solution. 401294dd290SLuigi Rizzo */ 402eb1b1807SGleb Smirnoff if ((n = m_dup(m, M_NOWAIT)) != NULL) { 40322f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, n); 404*62e1a437SZhenlei Huang (void)if_simloop(ifp, n, RO_GET_FAMILY(ro, dst), hlen); 405c1404dc0SArchie Cobbs } else 4063751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 407ed7509acSJulian Elischer } 4082e2de7f2SArchie Cobbs 4093f7d1396SAndrew Thompson /* 4103f7d1396SAndrew Thompson * Bridges require special output handling. 4113f7d1396SAndrew Thompson */ 4123f7d1396SAndrew Thompson if (ifp->if_bridge) { 4133f7d1396SAndrew Thompson BRIDGE_OUTPUT(ifp, m, error); 4143f7d1396SAndrew Thompson return (error); 4153f7d1396SAndrew Thompson } 4163f7d1396SAndrew Thompson 417259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 418a9771948SGleb Smirnoff if (ifp->if_carp && 41908b68b0eSGleb Smirnoff (error = (*carp_output_p)(ifp, m, dst))) 420a9771948SGleb Smirnoff goto bad; 421a9771948SGleb Smirnoff #endif 422a9771948SGleb Smirnoff 423e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 424833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 425514bcb89SPoul-Henning Kamp KASSERT(ng_ether_output_p != NULL, 426514bcb89SPoul-Henning Kamp ("ng_ether_output_p is NULL")); 427e1e1452dSArchie Cobbs if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 428e1e1452dSArchie Cobbs bad: if (m != NULL) 429e1e1452dSArchie Cobbs m_freem(m); 430e1e1452dSArchie Cobbs return (error); 431e1e1452dSArchie Cobbs } 432e1e1452dSArchie Cobbs if (m == NULL) 433e1e1452dSArchie Cobbs return (0); 434e1e1452dSArchie Cobbs } 435e1e1452dSArchie Cobbs 436e1e1452dSArchie Cobbs /* Continue with link-layer output */ 437e1e1452dSArchie Cobbs return ether_output_frame(ifp, m); 438e1e1452dSArchie Cobbs } 439e1e1452dSArchie Cobbs 440f1379734SKonstantin Belousov static bool 441f1379734SKonstantin Belousov ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp) 442f1379734SKonstantin Belousov { 443c7cffd65SAlexander V. Chernikov struct ether_8021q_tag qtag; 444f1379734SKonstantin Belousov struct ether_header *eh; 445f1379734SKonstantin Belousov 446f1379734SKonstantin Belousov eh = mtod(*mp, struct ether_header *); 447f1379734SKonstantin Belousov if (ntohs(eh->ether_type) == ETHERTYPE_VLAN || 448c7cffd65SAlexander V. Chernikov ntohs(eh->ether_type) == ETHERTYPE_QINQ) 449c7cffd65SAlexander V. Chernikov return (true); 450c7cffd65SAlexander V. Chernikov 451c7cffd65SAlexander V. Chernikov qtag.vid = 0; 452c7cffd65SAlexander V. Chernikov qtag.pcp = pcp; 453c7cffd65SAlexander V. Chernikov qtag.proto = ETHERTYPE_VLAN; 454c7cffd65SAlexander V. Chernikov if (ether_8021q_frame(mp, ifp, ifp, &qtag)) 455f1379734SKonstantin Belousov return (true); 456f1379734SKonstantin Belousov if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 457f1379734SKonstantin Belousov return (false); 458f1379734SKonstantin Belousov } 459f1379734SKonstantin Belousov 460e1e1452dSArchie Cobbs /* 461e1e1452dSArchie Cobbs * Ethernet link layer output routine to send a raw frame to the device. 462e1e1452dSArchie Cobbs * 463e1e1452dSArchie Cobbs * This assumes that the 14 byte Ethernet header is present and contiguous 464e1e1452dSArchie Cobbs * in the first mbuf (if BRIDGE'ing). 465e1e1452dSArchie Cobbs */ 466e1e1452dSArchie Cobbs int 467c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m) 468e1e1452dSArchie Cobbs { 469f1379734SKonstantin Belousov uint8_t pcp; 470f1379734SKonstantin Belousov 471f1379734SKonstantin Belousov pcp = ifp->if_pcp; 47247c39432SNavdeep Parhar if (pcp != IFNET_PCP_NONE && ifp->if_type != IFT_L2VLAN && 47347c39432SNavdeep Parhar !ether_set_pcp(&m, ifp, pcp)) 474f1379734SKonstantin Belousov return (0); 4752b25acc1SLuigi Rizzo 476b252313fSGleb Smirnoff if (PFIL_HOOKED_OUT(V_link_pfil_head)) 477b252313fSGleb Smirnoff switch (pfil_run_hooks(V_link_pfil_head, &m, ifp, PFIL_OUT, 478b252313fSGleb Smirnoff NULL)) { 479b252313fSGleb Smirnoff case PFIL_DROPPED: 4807d4317bdSAlexander V. Chernikov return (EACCES); 481b252313fSGleb Smirnoff case PFIL_CONSUMED: 4827d4317bdSAlexander V. Chernikov return (0); 4834b984093SLuigi Rizzo } 4842b25acc1SLuigi Rizzo 485201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL 486201100c5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 487201100c5SBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 48821231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 48921231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 490201100c5SBjoern A. Zeeb struct ether_header *eh; 491201100c5SBjoern A. Zeeb 492201100c5SBjoern A. Zeeb eh = mtod(m, struct ether_header *); 493201100c5SBjoern A. Zeeb switch (ntohs(eh->ether_type)) { 494201100c5SBjoern A. Zeeb case ETHERTYPE_IP: 495201100c5SBjoern A. Zeeb case ETHERTYPE_ARP: 49621231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 497201100c5SBjoern A. Zeeb m_freem(m); 498201100c5SBjoern A. Zeeb return (EAFNOSUPPORT); 499201100c5SBjoern A. Zeeb /* NOTREACHED */ 500201100c5SBjoern A. Zeeb break; 501201100c5SBjoern A. Zeeb }; 502201100c5SBjoern A. Zeeb } 503201100c5SBjoern A. Zeeb #endif 504201100c5SBjoern A. Zeeb #endif 505201100c5SBjoern A. Zeeb 506df8bae1dSRodney W. Grimes /* 50724fe4612SMark Johnston * Queue message on interface, update output statistics if successful, 50824fe4612SMark Johnston * and start output if interface not yet active. 50924fe4612SMark Johnston * 51024fe4612SMark Johnston * If KMSAN is enabled, use it to verify that the data does not contain 51124fe4612SMark Johnston * any uninitialized bytes. 512df8bae1dSRodney W. Grimes */ 51324fe4612SMark Johnston kmsan_check_mbuf(m, "ether_output"); 514aea78d20SKip Macy return ((ifp->if_transmit)(ifp, m)); 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes 5174b984093SLuigi Rizzo /* 518c1d93b05SSam Leffler * Process a received Ethernet packet; the packet is in the 519c1d93b05SSam Leffler * mbuf chain m with the ethernet header at the front. 520df8bae1dSRodney W. Grimes */ 521c1d93b05SSam Leffler static void 5226cb52192SRobert Watson ether_input_internal(struct ifnet *ifp, struct mbuf *m) 523df8bae1dSRodney W. Grimes { 524c1d93b05SSam Leffler struct ether_header *eh; 525c1d93b05SSam Leffler u_short etype; 526df8bae1dSRodney W. Grimes 52769462a82SBruce M Simpson if ((ifp->if_flags & IFF_UP) == 0) { 52869462a82SBruce M Simpson m_freem(m); 52969462a82SBruce M Simpson return; 53069462a82SBruce M Simpson } 53169462a82SBruce M Simpson #ifdef DIAGNOSTIC 53269462a82SBruce M Simpson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 53369462a82SBruce M Simpson if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n"); 53469462a82SBruce M Simpson m_freem(m); 53569462a82SBruce M Simpson return; 53669462a82SBruce M Simpson } 53769462a82SBruce M Simpson #endif 538797f247bSMatthew N. Dodd if (m->m_len < ETHER_HDR_LEN) { 539c1d93b05SSam Leffler /* XXX maybe should pullup? */ 540c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o leading ethernet " 541c1d93b05SSam Leffler "header (len %u pkt len %u)\n", 542c1d93b05SSam Leffler m->m_len, m->m_pkthdr.len); 5433751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 544c1d93b05SSam Leffler m_freem(m); 545c1d93b05SSam Leffler return; 5462e2de7f2SArchie Cobbs } 547c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 548c1d93b05SSam Leffler etype = ntohs(eh->ether_type); 54919fa89e9SMark Murray random_harvest_queue_ether(m, sizeof(*m)); 550d1b06863SMark Murray 55121231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL 55221231a7aSBjoern A. Zeeb #if defined(INET6) && defined(INET) 55321231a7aSBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 55421231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 55521231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 55621231a7aSBjoern A. Zeeb switch (etype) { 55721231a7aSBjoern A. Zeeb case ETHERTYPE_IP: 55821231a7aSBjoern A. Zeeb case ETHERTYPE_ARP: 55921231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 56021231a7aSBjoern A. Zeeb m_freem(m); 56121231a7aSBjoern A. Zeeb return; 56221231a7aSBjoern A. Zeeb /* NOTREACHED */ 56321231a7aSBjoern A. Zeeb break; 56421231a7aSBjoern A. Zeeb }; 56521231a7aSBjoern A. Zeeb } 56621231a7aSBjoern A. Zeeb #endif 56721231a7aSBjoern A. Zeeb #endif 56821231a7aSBjoern A. Zeeb 56921ca7b57SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet); 57021ca7b57SMarko Zec 57169462a82SBruce M Simpson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 57269462a82SBruce M Simpson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 57369462a82SBruce M Simpson m->m_flags |= M_BCAST; 57469462a82SBruce M Simpson else 57569462a82SBruce M Simpson m->m_flags |= M_MCAST; 5763751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 57769462a82SBruce M Simpson } 57869462a82SBruce M Simpson 579533d8562SRobert Watson #ifdef MAC 580533d8562SRobert Watson /* 581533d8562SRobert Watson * Tag the mbuf with an appropriate MAC label before any other 582533d8562SRobert Watson * consumers can get to it. 583533d8562SRobert Watson */ 58430d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 585533d8562SRobert Watson #endif 586533d8562SRobert Watson 587c1d93b05SSam Leffler /* 58869462a82SBruce M Simpson * Give bpf a chance at the packet. 589c1d93b05SSam Leffler */ 590d05d4616SChristian S.J. Peron ETHER_BPF_MTAP(ifp, m); 5912e2de7f2SArchie Cobbs 59269462a82SBruce M Simpson /* 59369462a82SBruce M Simpson * If the CRC is still on the packet, trim it off. We do this once 59469462a82SBruce M Simpson * and once only in case we are re-entered. Nothing else on the 59569462a82SBruce M Simpson * Ethernet receive path expects to see the FCS. 59669462a82SBruce M Simpson */ 597c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 598c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 599c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 600c1d93b05SSam Leffler } 601c1d93b05SSam Leffler 6024cdc1f54SGleb Smirnoff if (!(ifp->if_capenable & IFCAP_HWSTATS)) 6033751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 60405463bb5SDavid Greenman 60569462a82SBruce M Simpson /* Allow monitor mode to claim this frame, after stats are updated. */ 606de572b37SChristian S.J. Peron if (ifp->if_flags & IFF_MONITOR) { 607de572b37SChristian S.J. Peron m_freem(m); 60821ca7b57SMarko Zec CURVNET_RESTORE(); 609de572b37SChristian S.J. Peron return; 610de572b37SChristian S.J. Peron } 611de572b37SChristian S.J. Peron 61218242d3bSAndrew Thompson /* Handle input from a lagg(4) port */ 613b47888ceSAndrew Thompson if (ifp->if_type == IFT_IEEE8023ADLAG) { 614a92c4bb6SHans Petter Selasky KASSERT(lagg_input_ethernet_p != NULL, 61518242d3bSAndrew Thompson ("%s: if_lagg not loaded!", __func__)); 616a92c4bb6SHans Petter Selasky m = (*lagg_input_ethernet_p)(ifp, m); 617b47888ceSAndrew Thompson if (m != NULL) 618b47888ceSAndrew Thompson ifp = m->m_pkthdr.rcvif; 6192db13e75SMarko Zec else { 6202db13e75SMarko Zec CURVNET_RESTORE(); 621b47888ceSAndrew Thompson return; 622b47888ceSAndrew Thompson } 6232db13e75SMarko Zec } 624b47888ceSAndrew Thompson 62569462a82SBruce M Simpson /* 62669462a82SBruce M Simpson * If the hardware did not process an 802.1Q tag, do this now, 62769462a82SBruce M Simpson * to allow 802.1P priority frames to be passed to the main input 62869462a82SBruce M Simpson * path correctly. 62969462a82SBruce M Simpson */ 630c7cffd65SAlexander V. Chernikov if ((m->m_flags & M_VLANTAG) == 0 && 631c7cffd65SAlexander V. Chernikov ((etype == ETHERTYPE_VLAN) || (etype == ETHERTYPE_QINQ))) { 63269462a82SBruce M Simpson struct ether_vlan_header *evl; 63369462a82SBruce M Simpson 63469462a82SBruce M Simpson if (m->m_len < sizeof(*evl) && 63569462a82SBruce M Simpson (m = m_pullup(m, sizeof(*evl))) == NULL) { 636402d5e27SBruce M Simpson #ifdef DIAGNOSTIC 63769462a82SBruce M Simpson if_printf(ifp, "cannot pullup VLAN header\n"); 638402d5e27SBruce M Simpson #endif 6393751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 6402db13e75SMarko Zec CURVNET_RESTORE(); 64169462a82SBruce M Simpson return; 64269462a82SBruce M Simpson } 64369462a82SBruce M Simpson 64469462a82SBruce M Simpson evl = mtod(m, struct ether_vlan_header *); 64569462a82SBruce M Simpson m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 64669462a82SBruce M Simpson m->m_flags |= M_VLANTAG; 64769462a82SBruce M Simpson 64869462a82SBruce M Simpson bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 64969462a82SBruce M Simpson ETHER_HDR_LEN - ETHER_TYPE_LEN); 65069462a82SBruce M Simpson m_adj(m, ETHER_VLAN_ENCAP_LEN); 65197cce87fSGleb Smirnoff eh = mtod(m, struct ether_header *); 65269462a82SBruce M Simpson } 65369462a82SBruce M Simpson 654a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 655a34c6aebSBjoern A. Zeeb 65669462a82SBruce M Simpson /* Allow ng_ether(4) to claim this frame. */ 657833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 658514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_p != NULL, 65969462a82SBruce M Simpson ("%s: ng_ether_input_p is NULL", __func__)); 66069462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 661c1d93b05SSam Leffler (*ng_ether_input_p)(ifp, &m); 66221ca7b57SMarko Zec if (m == NULL) { 66321ca7b57SMarko Zec CURVNET_RESTORE(); 664e1e1452dSArchie Cobbs return; 665e1e1452dSArchie Cobbs } 6665e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 66721ca7b57SMarko Zec } 668e1e1452dSArchie Cobbs 6698f867517SAndrew Thompson /* 67069462a82SBruce M Simpson * Allow if_bridge(4) to claim this frame. 67169462a82SBruce M Simpson * The BRIDGE_INPUT() macro will update ifp if the bridge changed it 67269462a82SBruce M Simpson * and the frame should be delivered locally. 6738f867517SAndrew Thompson */ 67469462a82SBruce M Simpson if (ifp->if_bridge != NULL) { 67569462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 676fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m); 67721ca7b57SMarko Zec if (m == NULL) { 67821ca7b57SMarko Zec CURVNET_RESTORE(); 6798f867517SAndrew Thompson return; 6808f867517SAndrew Thompson } 6815e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 68221ca7b57SMarko Zec } 6838f867517SAndrew Thompson 684259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 68569462a82SBruce M Simpson /* 68669462a82SBruce M Simpson * Clear M_PROMISC on frame so that carp(4) will see it when the 68769462a82SBruce M Simpson * mbuf flows up to Layer 3. 68869462a82SBruce M Simpson * FreeBSD's implementation of carp(4) uses the inprotosw 68969462a82SBruce M Simpson * to dispatch IPPROTO_CARP. carp(4) also allocates its own 69069462a82SBruce M Simpson * Ethernet addresses of the form 00:00:5e:00:01:xx, which 69169462a82SBruce M Simpson * is outside the scope of the M_PROMISC test below. 69269462a82SBruce M Simpson * TODO: Maintain a hash table of ethernet addresses other than 69369462a82SBruce M Simpson * ether_dhost which may be active on this ifp. 69469462a82SBruce M Simpson */ 69554bfbd51SWill Andrews if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) { 69669462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 69769462a82SBruce M Simpson } else 69869462a82SBruce M Simpson #endif 69969462a82SBruce M Simpson { 70069462a82SBruce M Simpson /* 701e7f8c833SBruce M Simpson * If the frame received was not for our MAC address, set the 70269462a82SBruce M Simpson * M_PROMISC flag on the mbuf chain. The frame may need to 70369462a82SBruce M Simpson * be seen by the rest of the Ethernet input path in case of 70469462a82SBruce M Simpson * re-entry (e.g. bridge, vlan, netgraph) but should not be 70569462a82SBruce M Simpson * seen by upper protocol layers. 70669462a82SBruce M Simpson */ 70769462a82SBruce M Simpson if (!ETHER_IS_MULTICAST(eh->ether_dhost) && 708e7f8c833SBruce M Simpson bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0) 70969462a82SBruce M Simpson m->m_flags |= M_PROMISC; 71069462a82SBruce M Simpson } 71169462a82SBruce M Simpson 712acf032f5SRobert Watson ether_demux(ifp, m); 71321ca7b57SMarko Zec CURVNET_RESTORE(); 714e1e1452dSArchie Cobbs } 715e1e1452dSArchie Cobbs 716e1e1452dSArchie Cobbs /* 7176cb52192SRobert Watson * Ethernet input dispatch; by default, direct dispatch here regardless of 7187527624eSRobert Watson * global configuration. However, if RSS is enabled, hook up RSS affinity 7197527624eSRobert Watson * so that when deferred or hybrid dispatch is enabled, we can redistribute 7207527624eSRobert Watson * load based on RSS. 7217527624eSRobert Watson * 7227527624eSRobert Watson * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or 7237527624eSRobert Watson * not it had already done work distribution via multi-queue. Then we could 7247527624eSRobert Watson * direct dispatch in the event load balancing was already complete and 7257527624eSRobert Watson * handle the case of interfaces with different capabilities better. 7267527624eSRobert Watson * 7277527624eSRobert Watson * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions 7287527624eSRobert Watson * at multiple layers? 7297527624eSRobert Watson * 7307527624eSRobert Watson * XXXRW: For now, enable all this only if RSS is compiled in, although it 7317527624eSRobert Watson * works fine without RSS. Need to characterise the performance overhead 7327527624eSRobert Watson * of the detour through the netisr code in the event the result is always 7337527624eSRobert Watson * direct dispatch. 7346cb52192SRobert Watson */ 7356cb52192SRobert Watson static void 7366cb52192SRobert Watson ether_nh_input(struct mbuf *m) 7376cb52192SRobert Watson { 7386cb52192SRobert Watson 739b71bed24SAndrey V. Elsukov M_ASSERTPKTHDR(m); 740b71bed24SAndrey V. Elsukov KASSERT(m->m_pkthdr.rcvif != NULL, 741b71bed24SAndrey V. Elsukov ("%s: NULL interface pointer", __func__)); 7426cb52192SRobert Watson ether_input_internal(m->m_pkthdr.rcvif, m); 7436cb52192SRobert Watson } 7446cb52192SRobert Watson 7456cb52192SRobert Watson static struct netisr_handler ether_nh = { 7466cb52192SRobert Watson .nh_name = "ether", 7476cb52192SRobert Watson .nh_handler = ether_nh_input, 7486cb52192SRobert Watson .nh_proto = NETISR_ETHER, 7497527624eSRobert Watson #ifdef RSS 7507527624eSRobert Watson .nh_policy = NETISR_POLICY_CPU, 7517527624eSRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7527527624eSRobert Watson .nh_m2cpuid = rss_m2cpuid, 7537527624eSRobert Watson #else 7546cb52192SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 7556cb52192SRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7567527624eSRobert Watson #endif 7576cb52192SRobert Watson }; 7586cb52192SRobert Watson 7596cb52192SRobert Watson static void 7606cb52192SRobert Watson ether_init(__unused void *arg) 7616cb52192SRobert Watson { 7626cb52192SRobert Watson 7636cb52192SRobert Watson netisr_register(ðer_nh); 7646cb52192SRobert Watson } 7656cb52192SRobert Watson SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL); 7666cb52192SRobert Watson 7676cb52192SRobert Watson static void 7687d4317bdSAlexander V. Chernikov vnet_ether_init(__unused void *arg) 7697d4317bdSAlexander V. Chernikov { 770b252313fSGleb Smirnoff struct pfil_head_args args; 7717d4317bdSAlexander V. Chernikov 772b252313fSGleb Smirnoff args.pa_version = PFIL_VERSION; 773b252313fSGleb Smirnoff args.pa_flags = PFIL_IN | PFIL_OUT; 774b252313fSGleb Smirnoff args.pa_type = PFIL_TYPE_ETHERNET; 775b252313fSGleb Smirnoff args.pa_headname = PFIL_ETHER_NAME; 776b252313fSGleb Smirnoff V_link_pfil_head = pfil_head_register(&args); 777b252313fSGleb Smirnoff 778484149deSBjoern A. Zeeb #ifdef VIMAGE 779484149deSBjoern A. Zeeb netisr_register_vnet(ðer_nh); 780484149deSBjoern A. Zeeb #endif 7817d4317bdSAlexander V. Chernikov } 7827d4317bdSAlexander V. Chernikov VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY, 7837d4317bdSAlexander V. Chernikov vnet_ether_init, NULL); 7847d4317bdSAlexander V. Chernikov 785484149deSBjoern A. Zeeb #ifdef VIMAGE 7867d4317bdSAlexander V. Chernikov static void 787484149deSBjoern A. Zeeb vnet_ether_pfil_destroy(__unused void *arg) 7887d4317bdSAlexander V. Chernikov { 7897d4317bdSAlexander V. Chernikov 790b252313fSGleb Smirnoff pfil_head_unregister(V_link_pfil_head); 7917d4317bdSAlexander V. Chernikov } 792484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY, 793484149deSBjoern A. Zeeb vnet_ether_pfil_destroy, NULL); 794484149deSBjoern A. Zeeb 795484149deSBjoern A. Zeeb static void 796484149deSBjoern A. Zeeb vnet_ether_destroy(__unused void *arg) 797484149deSBjoern A. Zeeb { 798484149deSBjoern A. Zeeb 799484149deSBjoern A. Zeeb netisr_unregister_vnet(ðer_nh); 800484149deSBjoern A. Zeeb } 8017d4317bdSAlexander V. Chernikov VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, 8027d4317bdSAlexander V. Chernikov vnet_ether_destroy, NULL); 803484149deSBjoern A. Zeeb #endif 8047d4317bdSAlexander V. Chernikov 8057d4317bdSAlexander V. Chernikov static void 8066cb52192SRobert Watson ether_input(struct ifnet *ifp, struct mbuf *m) 8076cb52192SRobert Watson { 808b8a6e03fSGleb Smirnoff struct epoch_tracker et; 8094857f5fbSGeorge V. Neville-Neil struct mbuf *mn; 810e87c4940SGleb Smirnoff bool needs_epoch; 811e87c4940SGleb Smirnoff 812e87c4940SGleb Smirnoff needs_epoch = !(ifp->if_flags & IFF_KNOWSEPOCH); 8134857f5fbSGeorge V. Neville-Neil 8144857f5fbSGeorge V. Neville-Neil /* 8154857f5fbSGeorge V. Neville-Neil * The drivers are allowed to pass in a chain of packets linked with 8164857f5fbSGeorge V. Neville-Neil * m_nextpkt. We split them up into separate packets here and pass 8174857f5fbSGeorge V. Neville-Neil * them up. This allows the drivers to amortize the receive lock. 8184857f5fbSGeorge V. Neville-Neil */ 819b8a6e03fSGleb Smirnoff CURVNET_SET_QUIET(ifp->if_vnet); 820e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 821b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et); 8224857f5fbSGeorge V. Neville-Neil while (m) { 8234857f5fbSGeorge V. Neville-Neil mn = m->m_nextpkt; 8244857f5fbSGeorge V. Neville-Neil m->m_nextpkt = NULL; 8254857f5fbSGeorge V. Neville-Neil 8266cb52192SRobert Watson /* 827b8a6e03fSGleb Smirnoff * We will rely on rcvif being set properly in the deferred 828b8a6e03fSGleb Smirnoff * context, so assert it is correct here. 8296cb52192SRobert Watson */ 830fb3bc596SJohn Baldwin MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 831e84ef07fSBjoern A. Zeeb KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p " 832e84ef07fSBjoern A. Zeeb "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp)); 8336cb52192SRobert Watson netisr_dispatch(NETISR_ETHER, m); 8344857f5fbSGeorge V. Neville-Neil m = mn; 8354857f5fbSGeorge V. Neville-Neil } 836e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 837b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et); 838b8a6e03fSGleb Smirnoff CURVNET_RESTORE(); 8396cb52192SRobert Watson } 8406cb52192SRobert Watson 8416cb52192SRobert Watson /* 842e1e1452dSArchie Cobbs * Upper layer processing for a received Ethernet packet. 843e1e1452dSArchie Cobbs */ 844e1e1452dSArchie Cobbs void 845c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m) 846e1e1452dSArchie Cobbs { 847c1d93b05SSam Leffler struct ether_header *eh; 8487d4317bdSAlexander V. Chernikov int i, isr; 849e1e1452dSArchie Cobbs u_short ether_type; 85069462a82SBruce M Simpson 851b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 85269462a82SBruce M Simpson KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__)); 85369462a82SBruce M Simpson 8547d4317bdSAlexander V. Chernikov /* Do not grab PROMISC frames in case we are re-entered. */ 855b252313fSGleb Smirnoff if (PFIL_HOOKED_IN(V_link_pfil_head) && !(m->m_flags & M_PROMISC)) { 856b252313fSGleb Smirnoff i = pfil_run_hooks(V_link_pfil_head, &m, ifp, PFIL_IN, NULL); 8577d4317bdSAlexander V. Chernikov if (i != 0 || m == NULL) 8587d4317bdSAlexander V. Chernikov return; 85969462a82SBruce M Simpson } 8607d4317bdSAlexander V. Chernikov 861c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 862cd0cd014SJoerg Wunsch ether_type = ntohs(eh->ether_type); 863c1d93b05SSam Leffler 864a9771948SGleb Smirnoff /* 86569462a82SBruce M Simpson * If this frame has a VLAN tag other than 0, call vlan_input() 86669462a82SBruce M Simpson * if its module is loaded. Otherwise, drop. 867a9771948SGleb Smirnoff */ 86869462a82SBruce M Simpson if ((m->m_flags & M_VLANTAG) && 86969462a82SBruce M Simpson EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 87075ee267cSGleb Smirnoff if (ifp->if_vlantrunk == NULL) { 8713751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 8726c23e6ccSRuslan Ermilov m_freem(m); 8736c23e6ccSRuslan Ermilov return; 8746c23e6ccSRuslan Ermilov } 87569462a82SBruce M Simpson KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 87669462a82SBruce M Simpson __func__)); 87769462a82SBruce M Simpson /* Clear before possibly re-entering ether_input(). */ 87869462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 879c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 880c1d93b05SSam Leffler return; 881c1d93b05SSam Leffler } 882c1d93b05SSam Leffler 883c1d93b05SSam Leffler /* 88469462a82SBruce M Simpson * Pass promiscuously received frames to the upper layer if the user 88569462a82SBruce M Simpson * requested this by setting IFF_PPROMISC. Otherwise, drop them. 886c1d93b05SSam Leffler */ 88769462a82SBruce M Simpson if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 888c1d93b05SSam Leffler m_freem(m); 889c1d93b05SSam Leffler return; 890c1d93b05SSam Leffler } 891c1d93b05SSam Leffler 89269462a82SBruce M Simpson /* 89369462a82SBruce M Simpson * Reset layer specific mbuf flags to avoid confusing upper layers. 89469462a82SBruce M Simpson * Strip off Ethernet header. 89569462a82SBruce M Simpson */ 89669462a82SBruce M Simpson m->m_flags &= ~M_VLANTAG; 89786bd0491SAndre Oppermann m_clrprotoflags(m); 898797f247bSMatthew N. Dodd m_adj(m, ETHER_HDR_LEN); 899c1d93b05SSam Leffler 90069462a82SBruce M Simpson /* 90169462a82SBruce M Simpson * Dispatch frame to upper layer. 90269462a82SBruce M Simpson */ 903307d80beSDavid Greenman switch (ether_type) { 904df8bae1dSRodney W. Grimes #ifdef INET 905df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 9061cafed39SJonathan Lemon isr = NETISR_IP; 907df8bae1dSRodney W. Grimes break; 908df8bae1dSRodney W. Grimes 909df8bae1dSRodney W. Grimes case ETHERTYPE_ARP: 91008aadfbbSJonathan Lemon if (ifp->if_flags & IFF_NOARP) { 91108aadfbbSJonathan Lemon /* Discard packet if ARP is disabled on interface */ 91208aadfbbSJonathan Lemon m_freem(m); 91308aadfbbSJonathan Lemon return; 91408aadfbbSJonathan Lemon } 9151cafed39SJonathan Lemon isr = NETISR_ARP; 916df8bae1dSRodney W. Grimes break; 917df8bae1dSRodney W. Grimes #endif 91882cd038dSYoshinobu Inoue #ifdef INET6 91982cd038dSYoshinobu Inoue case ETHERTYPE_IPV6: 9201cafed39SJonathan Lemon isr = NETISR_IPV6; 92182cd038dSYoshinobu Inoue break; 92282cd038dSYoshinobu Inoue #endif 923df8bae1dSRodney W. Grimes default: 9241cafed39SJonathan Lemon goto discard; 925df8bae1dSRodney W. Grimes } 9261cafed39SJonathan Lemon netisr_dispatch(isr, m); 927c1d93b05SSam Leffler return; 9281cafed39SJonathan Lemon 929c1d93b05SSam Leffler discard: 930c1d93b05SSam Leffler /* 931c1d93b05SSam Leffler * Packet is to be discarded. If netgraph is present, 932c1d93b05SSam Leffler * hand the packet to it for last chance processing; 933c1d93b05SSam Leffler * otherwise dispose of it. 934c1d93b05SSam Leffler */ 935833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 936514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_orphan_p != NULL, 937514bcb89SPoul-Henning Kamp ("ng_ether_input_orphan_p is NULL")); 938c1d93b05SSam Leffler /* 939c1d93b05SSam Leffler * Put back the ethernet header so netgraph has a 940c1d93b05SSam Leffler * consistent view of inbound packets. 941c1d93b05SSam Leffler */ 942eb1b1807SGleb Smirnoff M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT); 943c1d93b05SSam Leffler (*ng_ether_input_orphan_p)(ifp, m); 944c1d93b05SSam Leffler return; 945c1d93b05SSam Leffler } 946c1d93b05SSam Leffler m_freem(m); 947c1d93b05SSam Leffler } 948c1d93b05SSam Leffler 949c1d93b05SSam Leffler /* 950c1d93b05SSam Leffler * Convert Ethernet address to printable (loggable) representation. 951c1d93b05SSam Leffler * This routine is for compatibility; it's better to just use 952c1d93b05SSam Leffler * 953c1d93b05SSam Leffler * printf("%6D", <pointer to address>, ":"); 954c1d93b05SSam Leffler * 955c1d93b05SSam Leffler * since there's no static buffer involved. 956c1d93b05SSam Leffler */ 957c1d93b05SSam Leffler char * 958c1d93b05SSam Leffler ether_sprintf(const u_char *ap) 959c1d93b05SSam Leffler { 960c1d93b05SSam Leffler static char etherbuf[18]; 961c1d93b05SSam Leffler snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 962c1d93b05SSam Leffler return (etherbuf); 963df8bae1dSRodney W. Grimes } 964df8bae1dSRodney W. Grimes 965df8bae1dSRodney W. Grimes /* 966df8bae1dSRodney W. Grimes * Perform common duties while attaching to interface list 967df8bae1dSRodney W. Grimes */ 968df8bae1dSRodney W. Grimes void 969d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 970df8bae1dSRodney W. Grimes { 971f93dfa28SBrooks Davis int i; 972098a8c3bSMatthew N. Dodd struct ifaddr *ifa; 973098a8c3bSMatthew N. Dodd struct sockaddr_dl *sdl; 974df8bae1dSRodney W. Grimes 975c1d93b05SSam Leffler ifp->if_addrlen = ETHER_ADDR_LEN; 976c1d93b05SSam Leffler ifp->if_hdrlen = ETHER_HDR_LEN; 977df8bae1dSRodney W. Grimes ifp->if_mtu = ETHERMTU; 978fa2ab81eSKyle Evans if_attach(ifp); 979c1d93b05SSam Leffler ifp->if_output = ether_output; 980c1d93b05SSam Leffler ifp->if_input = ether_input; 9811158dfb7SGarrett Wollman ifp->if_resolvemulti = ether_resolvemulti; 9824fb3a820SAlexander V. Chernikov ifp->if_requestencap = ether_requestencap; 983d3c351c5SMarko Zec #ifdef VIMAGE 984d3c351c5SMarko Zec ifp->if_reassign = ether_reassign; 985d3c351c5SMarko Zec #endif 986a330e1f1SGary Palmer if (ifp->if_baudrate == 0) 987c1d93b05SSam Leffler ifp->if_baudrate = IF_Mbps(10); /* just a default */ 988322dcb8dSMax Khon ifp->if_broadcastaddr = etherbroadcastaddr; 989c1d93b05SSam Leffler 9904a0d6638SRuslan Ermilov ifa = ifp->if_addr; 9916e551fb6SDavid E. O'Brien KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 99259562606SGarrett Wollman sdl = (struct sockaddr_dl *)ifa->ifa_addr; 993df8bae1dSRodney W. Grimes sdl->sdl_type = IFT_ETHER; 994df8bae1dSRodney W. Grimes sdl->sdl_alen = ifp->if_addrlen; 995d09ed26fSRuslan Ermilov bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 996c1d93b05SSam Leffler 9975b1a5e45SRavi Pokala if (ifp->if_hw_addr != NULL) 998ddae5750SRavi Pokala bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen); 999ddae5750SRavi Pokala 1000797f247bSMatthew N. Dodd bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 1001e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) 1002e1e1452dSArchie Cobbs (*ng_ether_attach_p)(ifp); 1003e3bbbec2SMatthew N. Dodd 1004f93dfa28SBrooks Davis /* Announce Ethernet MAC address if non-zero. */ 1005f93dfa28SBrooks Davis for (i = 0; i < ifp->if_addrlen; i++) 1006d09ed26fSRuslan Ermilov if (lla[i] != 0) 1007f93dfa28SBrooks Davis break; 1008f93dfa28SBrooks Davis if (i != ifp->if_addrlen) 1009d09ed26fSRuslan Ermilov if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 1010ef1f9169SMarcel Moolenaar 1011ef1f9169SMarcel Moolenaar uuid_ether_add(LLADDR(sdl)); 10128819ad85SSepherosa Ziehau 10138819ad85SSepherosa Ziehau /* Add necessary bits are setup; announce it now. */ 10148819ad85SSepherosa Ziehau EVENTHANDLER_INVOKE(ether_ifattach_event, ifp); 10158819ad85SSepherosa Ziehau if (IS_DEFAULT_VNET(curvnet)) 10168819ad85SSepherosa Ziehau devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL); 1017e1e1452dSArchie Cobbs } 1018e1e1452dSArchie Cobbs 101921b8ebd9SArchie Cobbs /* 102021b8ebd9SArchie Cobbs * Perform common duties while detaching an Ethernet interface 102121b8ebd9SArchie Cobbs */ 102221b8ebd9SArchie Cobbs void 1023c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp) 102421b8ebd9SArchie Cobbs { 1025ef1f9169SMarcel Moolenaar struct sockaddr_dl *sdl; 1026ef1f9169SMarcel Moolenaar 1027ef1f9169SMarcel Moolenaar sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr); 1028ef1f9169SMarcel Moolenaar uuid_ether_del(LLADDR(sdl)); 1029ef1f9169SMarcel Moolenaar 1030833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1031514bcb89SPoul-Henning Kamp KASSERT(ng_ether_detach_p != NULL, 1032514bcb89SPoul-Henning Kamp ("ng_ether_detach_p is NULL")); 103321b8ebd9SArchie Cobbs (*ng_ether_detach_p)(ifp); 1034514bcb89SPoul-Henning Kamp } 103520a65f37SAndrew Thompson 103621b8ebd9SArchie Cobbs bpfdetach(ifp); 103721b8ebd9SArchie Cobbs if_detach(ifp); 103821b8ebd9SArchie Cobbs } 103921b8ebd9SArchie Cobbs 1040d3c351c5SMarko Zec #ifdef VIMAGE 1041d3c351c5SMarko Zec void 1042d3c351c5SMarko Zec ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused) 1043d3c351c5SMarko Zec { 1044d3c351c5SMarko Zec 1045833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1046d3c351c5SMarko Zec KASSERT(ng_ether_detach_p != NULL, 1047d3c351c5SMarko Zec ("ng_ether_detach_p is NULL")); 1048d3c351c5SMarko Zec (*ng_ether_detach_p)(ifp); 1049d3c351c5SMarko Zec } 1050d3c351c5SMarko Zec 1051d3c351c5SMarko Zec if (ng_ether_attach_p != NULL) { 1052d3c351c5SMarko Zec CURVNET_SET_QUIET(new_vnet); 1053d3c351c5SMarko Zec (*ng_ether_attach_p)(ifp); 1054d3c351c5SMarko Zec CURVNET_RESTORE(); 1055d3c351c5SMarko Zec } 1056d3c351c5SMarko Zec } 1057d3c351c5SMarko Zec #endif 1058d3c351c5SMarko Zec 1059ce02431fSDoug Rabson SYSCTL_DECL(_net_link); 10607029da5cSPawel Biernacki SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 10617029da5cSPawel Biernacki "Ethernet"); 106230106f6aSPoul-Henning Kamp 106316b4a343SChristian Weisgerber #if 0 106416b4a343SChristian Weisgerber /* 106516b4a343SChristian Weisgerber * This is for reference. We have a table-driven version 106616b4a343SChristian Weisgerber * of the little-endian crc32 generator, which is faster 106716b4a343SChristian Weisgerber * than the double-loop. 106816b4a343SChristian Weisgerber */ 106916b4a343SChristian Weisgerber uint32_t 107016b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 107116b4a343SChristian Weisgerber { 107216b4a343SChristian Weisgerber size_t i; 107316b4a343SChristian Weisgerber uint32_t crc; 107416b4a343SChristian Weisgerber int bit; 107516b4a343SChristian Weisgerber uint8_t data; 107616b4a343SChristian Weisgerber 107716b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 107816b4a343SChristian Weisgerber 107916b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 1080933dad75SAntoine Brodin for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 108116b4a343SChristian Weisgerber carry = (crc ^ data) & 1; 108216b4a343SChristian Weisgerber crc >>= 1; 108316b4a343SChristian Weisgerber if (carry) 108416b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_LE); 108516b4a343SChristian Weisgerber } 1086933dad75SAntoine Brodin } 108716b4a343SChristian Weisgerber 108816b4a343SChristian Weisgerber return (crc); 108916b4a343SChristian Weisgerber } 109016b4a343SChristian Weisgerber #else 109116b4a343SChristian Weisgerber uint32_t 109216b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 109316b4a343SChristian Weisgerber { 109416b4a343SChristian Weisgerber static const uint32_t crctab[] = { 109516b4a343SChristian Weisgerber 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 109616b4a343SChristian Weisgerber 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 109716b4a343SChristian Weisgerber 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 109816b4a343SChristian Weisgerber 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 109916b4a343SChristian Weisgerber }; 110016b4a343SChristian Weisgerber size_t i; 110116b4a343SChristian Weisgerber uint32_t crc; 110216b4a343SChristian Weisgerber 110316b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 110416b4a343SChristian Weisgerber 110516b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 110616b4a343SChristian Weisgerber crc ^= buf[i]; 110716b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 110816b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 110916b4a343SChristian Weisgerber } 111016b4a343SChristian Weisgerber 111116b4a343SChristian Weisgerber return (crc); 111216b4a343SChristian Weisgerber } 111316b4a343SChristian Weisgerber #endif 111416b4a343SChristian Weisgerber 111516b4a343SChristian Weisgerber uint32_t 111616b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len) 111716b4a343SChristian Weisgerber { 111816b4a343SChristian Weisgerber size_t i; 111916b4a343SChristian Weisgerber uint32_t crc, carry; 112016b4a343SChristian Weisgerber int bit; 112116b4a343SChristian Weisgerber uint8_t data; 112216b4a343SChristian Weisgerber 112316b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 112416b4a343SChristian Weisgerber 112516b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 112616b4a343SChristian Weisgerber for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 112716b4a343SChristian Weisgerber carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 112816b4a343SChristian Weisgerber crc <<= 1; 112916b4a343SChristian Weisgerber if (carry) 113016b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 113116b4a343SChristian Weisgerber } 113216b4a343SChristian Weisgerber } 113316b4a343SChristian Weisgerber 113416b4a343SChristian Weisgerber return (crc); 113516b4a343SChristian Weisgerber } 113616b4a343SChristian Weisgerber 1137fb583156SDavid Greenman int 1138995c7fd1SYaroslav Tykhiy ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 113930106f6aSPoul-Henning Kamp { 114030106f6aSPoul-Henning Kamp struct ifaddr *ifa = (struct ifaddr *) data; 114130106f6aSPoul-Henning Kamp struct ifreq *ifr = (struct ifreq *) data; 1142fb583156SDavid Greenman int error = 0; 114330106f6aSPoul-Henning Kamp 114430106f6aSPoul-Henning Kamp switch (command) { 114530106f6aSPoul-Henning Kamp case SIOCSIFADDR: 114630106f6aSPoul-Henning Kamp ifp->if_flags |= IFF_UP; 114730106f6aSPoul-Henning Kamp 114830106f6aSPoul-Henning Kamp switch (ifa->ifa_addr->sa_family) { 114930106f6aSPoul-Henning Kamp #ifdef INET 115030106f6aSPoul-Henning Kamp case AF_INET: 115130106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1152322dcb8dSMax Khon arp_ifinit(ifp, ifa); 115330106f6aSPoul-Henning Kamp break; 115430106f6aSPoul-Henning Kamp #endif 115530106f6aSPoul-Henning Kamp default: 115630106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 115730106f6aSPoul-Henning Kamp break; 115830106f6aSPoul-Henning Kamp } 115930106f6aSPoul-Henning Kamp break; 116030106f6aSPoul-Henning Kamp 116130106f6aSPoul-Henning Kamp case SIOCGIFADDR: 116238d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 116338d958a6SBrooks Davis ETHER_ADDR_LEN); 116430106f6aSPoul-Henning Kamp break; 1165fb583156SDavid Greenman 1166fb583156SDavid Greenman case SIOCSIFMTU: 1167fb583156SDavid Greenman /* 1168fb583156SDavid Greenman * Set the interface MTU. 1169fb583156SDavid Greenman */ 1170fb583156SDavid Greenman if (ifr->ifr_mtu > ETHERMTU) { 1171fb583156SDavid Greenman error = EINVAL; 1172fb583156SDavid Greenman } else { 1173fb583156SDavid Greenman ifp->if_mtu = ifr->ifr_mtu; 117430106f6aSPoul-Henning Kamp } 1175fb583156SDavid Greenman break; 1176f1379734SKonstantin Belousov 1177f1379734SKonstantin Belousov case SIOCSLANPCP: 1178f1379734SKonstantin Belousov error = priv_check(curthread, PRIV_NET_SETLANPCP); 1179f1379734SKonstantin Belousov if (error != 0) 1180f1379734SKonstantin Belousov break; 1181f1379734SKonstantin Belousov if (ifr->ifr_lan_pcp > 7 && 11824a381a9eSHans Petter Selasky ifr->ifr_lan_pcp != IFNET_PCP_NONE) { 1183f1379734SKonstantin Belousov error = EINVAL; 11844a381a9eSHans Petter Selasky } else { 1185f1379734SKonstantin Belousov ifp->if_pcp = ifr->ifr_lan_pcp; 11864a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 11874a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 11884a381a9eSHans Petter Selasky } 1189f1379734SKonstantin Belousov break; 1190f1379734SKonstantin Belousov 1191f1379734SKonstantin Belousov case SIOCGLANPCP: 1192f1379734SKonstantin Belousov ifr->ifr_lan_pcp = ifp->if_pcp; 1193f1379734SKonstantin Belousov break; 1194f1379734SKonstantin Belousov 1195c1d93b05SSam Leffler default: 1196c1d93b05SSam Leffler error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1197c1d93b05SSam Leffler break; 1198fb583156SDavid Greenman } 1199fb583156SDavid Greenman return (error); 120030106f6aSPoul-Henning Kamp } 12011158dfb7SGarrett Wollman 120237c84183SPoul-Henning Kamp static int 120372fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 120472fd1b6aSDag-Erling Smørgrav struct sockaddr *sa) 12051158dfb7SGarrett Wollman { 12061158dfb7SGarrett Wollman struct sockaddr_dl *sdl; 1207d7647d96SDag-Erling Smørgrav #ifdef INET 12081158dfb7SGarrett Wollman struct sockaddr_in *sin; 1209d7647d96SDag-Erling Smørgrav #endif 121082cd038dSYoshinobu Inoue #ifdef INET6 121182cd038dSYoshinobu Inoue struct sockaddr_in6 *sin6; 121282cd038dSYoshinobu Inoue #endif 12131158dfb7SGarrett Wollman u_char *e_addr; 12141158dfb7SGarrett Wollman 12151158dfb7SGarrett Wollman switch(sa->sa_family) { 12161158dfb7SGarrett Wollman case AF_LINK: 12177f33a738SJulian Elischer /* 12187f33a738SJulian Elischer * No mapping needed. Just check that it's a valid MC address. 12197f33a738SJulian Elischer */ 12201158dfb7SGarrett Wollman sdl = (struct sockaddr_dl *)sa; 12211158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 1222086e98c4SBruce M Simpson if (!ETHER_IS_MULTICAST(e_addr)) 12231158dfb7SGarrett Wollman return EADDRNOTAVAIL; 1224155d72c4SPedro F. Giffuni *llsa = NULL; 12251158dfb7SGarrett Wollman return 0; 12261158dfb7SGarrett Wollman 12271158dfb7SGarrett Wollman #ifdef INET 12281158dfb7SGarrett Wollman case AF_INET: 12291158dfb7SGarrett Wollman sin = (struct sockaddr_in *)sa; 12301158dfb7SGarrett Wollman if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 12311158dfb7SGarrett Wollman return EADDRNOTAVAIL; 123295fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 12331158dfb7SGarrett Wollman sdl->sdl_alen = ETHER_ADDR_LEN; 12341158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 12351158dfb7SGarrett Wollman ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 12361158dfb7SGarrett Wollman *llsa = (struct sockaddr *)sdl; 12371158dfb7SGarrett Wollman return 0; 12381158dfb7SGarrett Wollman #endif 123982cd038dSYoshinobu Inoue #ifdef INET6 124082cd038dSYoshinobu Inoue case AF_INET6: 124182cd038dSYoshinobu Inoue sin6 = (struct sockaddr_in6 *)sa; 1242595b8a1cSJun-ichiro itojun Hagino if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1243595b8a1cSJun-ichiro itojun Hagino /* 1244595b8a1cSJun-ichiro itojun Hagino * An IP6 address of 0 means listen to all 1245595b8a1cSJun-ichiro itojun Hagino * of the Ethernet multicast address used for IP6. 1246595b8a1cSJun-ichiro itojun Hagino * (This is used for multicast routers.) 1247595b8a1cSJun-ichiro itojun Hagino */ 1248595b8a1cSJun-ichiro itojun Hagino ifp->if_flags |= IFF_ALLMULTI; 1249155d72c4SPedro F. Giffuni *llsa = NULL; 1250595b8a1cSJun-ichiro itojun Hagino return 0; 1251595b8a1cSJun-ichiro itojun Hagino } 125282cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 125382cd038dSYoshinobu Inoue return EADDRNOTAVAIL; 125495fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 125582cd038dSYoshinobu Inoue sdl->sdl_alen = ETHER_ADDR_LEN; 125682cd038dSYoshinobu Inoue e_addr = LLADDR(sdl); 125782cd038dSYoshinobu Inoue ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 125882cd038dSYoshinobu Inoue *llsa = (struct sockaddr *)sdl; 125982cd038dSYoshinobu Inoue return 0; 126082cd038dSYoshinobu Inoue #endif 12611158dfb7SGarrett Wollman 12621158dfb7SGarrett Wollman default: 12631158dfb7SGarrett Wollman /* 12641158dfb7SGarrett Wollman * Well, the text isn't quite right, but it's the name 12651158dfb7SGarrett Wollman * that counts... 12661158dfb7SGarrett Wollman */ 12671158dfb7SGarrett Wollman return EAFNOSUPPORT; 12681158dfb7SGarrett Wollman } 12691158dfb7SGarrett Wollman } 127040811c14SMatthew N. Dodd 127140811c14SMatthew N. Dodd static moduledata_t ether_mod = { 1272833e8dc5SGleb Smirnoff .name = "ether", 127340811c14SMatthew N. Dodd }; 127440811c14SMatthew N. Dodd 127552f1277eSChristian S.J. Peron void 127652f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 127752f1277eSChristian S.J. Peron { 127852f1277eSChristian S.J. Peron struct ether_vlan_header vlan; 127952f1277eSChristian S.J. Peron struct mbuf mv, mb; 128052f1277eSChristian S.J. Peron 128152f1277eSChristian S.J. Peron KASSERT((m->m_flags & M_VLANTAG) != 0, 128252f1277eSChristian S.J. Peron ("%s: vlan information not present", __func__)); 128352f1277eSChristian S.J. Peron KASSERT(m->m_len >= sizeof(struct ether_header), 128452f1277eSChristian S.J. Peron ("%s: mbuf not large enough for header", __func__)); 128552f1277eSChristian S.J. Peron bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 128652f1277eSChristian S.J. Peron vlan.evl_proto = vlan.evl_encap_proto; 128752f1277eSChristian S.J. Peron vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 128852f1277eSChristian S.J. Peron vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 128952f1277eSChristian S.J. Peron m->m_len -= sizeof(struct ether_header); 129052f1277eSChristian S.J. Peron m->m_data += sizeof(struct ether_header); 129152f1277eSChristian S.J. Peron /* 129252f1277eSChristian S.J. Peron * If a data link has been supplied by the caller, then we will need to 129352f1277eSChristian S.J. Peron * re-create a stack allocated mbuf chain with the following structure: 129452f1277eSChristian S.J. Peron * 129552f1277eSChristian S.J. Peron * (1) mbuf #1 will contain the supplied data link 129652f1277eSChristian S.J. Peron * (2) mbuf #2 will contain the vlan header 129752f1277eSChristian S.J. Peron * (3) mbuf #3 will contain the original mbuf's packet data 129852f1277eSChristian S.J. Peron * 129952f1277eSChristian S.J. Peron * Otherwise, submit the packet and vlan header via bpf_mtap2(). 130052f1277eSChristian S.J. Peron */ 130152f1277eSChristian S.J. Peron if (data != NULL) { 130252f1277eSChristian S.J. Peron mv.m_next = m; 130352f1277eSChristian S.J. Peron mv.m_data = (caddr_t)&vlan; 130452f1277eSChristian S.J. Peron mv.m_len = sizeof(vlan); 130552f1277eSChristian S.J. Peron mb.m_next = &mv; 130652f1277eSChristian S.J. Peron mb.m_data = data; 130752f1277eSChristian S.J. Peron mb.m_len = dlen; 130852f1277eSChristian S.J. Peron bpf_mtap(bp, &mb); 130952f1277eSChristian S.J. Peron } else 131052f1277eSChristian S.J. Peron bpf_mtap2(bp, &vlan, sizeof(vlan), m); 131152f1277eSChristian S.J. Peron m->m_len += sizeof(struct ether_header); 131252f1277eSChristian S.J. Peron m->m_data -= sizeof(struct ether_header); 131352f1277eSChristian S.J. Peron } 131452f1277eSChristian S.J. Peron 131560e87ca8SAndrew Thompson struct mbuf * 1316c7cffd65SAlexander V. Chernikov ether_vlanencap_proto(struct mbuf *m, uint16_t tag, uint16_t proto) 131760e87ca8SAndrew Thompson { 131860e87ca8SAndrew Thompson struct ether_vlan_header *evl; 131960e87ca8SAndrew Thompson 1320eb1b1807SGleb Smirnoff M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 132160e87ca8SAndrew Thompson if (m == NULL) 132260e87ca8SAndrew Thompson return (NULL); 132360e87ca8SAndrew Thompson /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 132460e87ca8SAndrew Thompson 132560e87ca8SAndrew Thompson if (m->m_len < sizeof(*evl)) { 132660e87ca8SAndrew Thompson m = m_pullup(m, sizeof(*evl)); 132760e87ca8SAndrew Thompson if (m == NULL) 132860e87ca8SAndrew Thompson return (NULL); 132960e87ca8SAndrew Thompson } 133060e87ca8SAndrew Thompson 133160e87ca8SAndrew Thompson /* 133260e87ca8SAndrew Thompson * Transform the Ethernet header into an Ethernet header 133360e87ca8SAndrew Thompson * with 802.1Q encapsulation. 133460e87ca8SAndrew Thompson */ 133560e87ca8SAndrew Thompson evl = mtod(m, struct ether_vlan_header *); 133660e87ca8SAndrew Thompson bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 133760e87ca8SAndrew Thompson (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1338c7cffd65SAlexander V. Chernikov evl->evl_encap_proto = htons(proto); 133960e87ca8SAndrew Thompson evl->evl_tag = htons(tag); 134060e87ca8SAndrew Thompson return (m); 134160e87ca8SAndrew Thompson } 134260e87ca8SAndrew Thompson 13437029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1344f1379734SKonstantin Belousov "IEEE 802.1Q VLAN"); 13457029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, 13467029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1347f1379734SKonstantin Belousov "for consistency"); 1348f1379734SKonstantin Belousov 13495f901c92SAndrew Turner VNET_DEFINE_STATIC(int, soft_pad); 1350f1379734SKonstantin Belousov #define V_soft_pad VNET(soft_pad) 1351f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 1352f1379734SKonstantin Belousov &VNET_NAME(soft_pad), 0, 1353f1379734SKonstantin Belousov "pad short frames before tagging"); 1354f1379734SKonstantin Belousov 1355f1379734SKonstantin Belousov /* 1356f1379734SKonstantin Belousov * For now, make preserving PCP via an mbuf tag optional, as it increases 1357f1379734SKonstantin Belousov * per-packet memory allocations and frees. In the future, it would be 1358f1379734SKonstantin Belousov * preferable to reuse ether_vtag for this, or similar. 1359f1379734SKonstantin Belousov */ 1360f1379734SKonstantin Belousov int vlan_mtag_pcp = 0; 1361f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW, 1362f1379734SKonstantin Belousov &vlan_mtag_pcp, 0, 1363f1379734SKonstantin Belousov "Retain VLAN PCP information as packets are passed up the stack"); 1364f1379734SKonstantin Belousov 1365f1379734SKonstantin Belousov bool 1366f1379734SKonstantin Belousov ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, 1367c7cffd65SAlexander V. Chernikov struct ether_8021q_tag *qtag) 1368f1379734SKonstantin Belousov { 1369f1379734SKonstantin Belousov struct m_tag *mtag; 1370f1379734SKonstantin Belousov int n; 1371f1379734SKonstantin Belousov uint16_t tag; 1372f1379734SKonstantin Belousov static const char pad[8]; /* just zeros */ 1373f1379734SKonstantin Belousov 1374f1379734SKonstantin Belousov /* 1375f1379734SKonstantin Belousov * Pad the frame to the minimum size allowed if told to. 1376f1379734SKonstantin Belousov * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1377f1379734SKonstantin Belousov * paragraph C.4.4.3.b. It can help to work around buggy 1378f1379734SKonstantin Belousov * bridges that violate paragraph C.4.4.3.a from the same 1379f1379734SKonstantin Belousov * document, i.e., fail to pad short frames after untagging. 1380f1379734SKonstantin Belousov * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1381f1379734SKonstantin Belousov * untagging it will produce a 62-byte frame, which is a runt 1382f1379734SKonstantin Belousov * and requires padding. There are VLAN-enabled network 1383f1379734SKonstantin Belousov * devices that just discard such runts instead or mishandle 1384f1379734SKonstantin Belousov * them somehow. 1385f1379734SKonstantin Belousov */ 1386f1379734SKonstantin Belousov if (V_soft_pad && p->if_type == IFT_ETHER) { 1387f1379734SKonstantin Belousov for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len; 1388f1379734SKonstantin Belousov n > 0; n -= sizeof(pad)) { 1389f1379734SKonstantin Belousov if (!m_append(*mp, min(n, sizeof(pad)), pad)) 1390f1379734SKonstantin Belousov break; 1391f1379734SKonstantin Belousov } 1392f1379734SKonstantin Belousov if (n > 0) { 1393f1379734SKonstantin Belousov m_freem(*mp); 1394f1379734SKonstantin Belousov *mp = NULL; 1395f1379734SKonstantin Belousov if_printf(ife, "cannot pad short frame"); 1396f1379734SKonstantin Belousov return (false); 1397f1379734SKonstantin Belousov } 1398f1379734SKonstantin Belousov } 1399f1379734SKonstantin Belousov 1400f1379734SKonstantin Belousov /* 1401868aabb4SRichard Scheffenegger * If PCP is set in mbuf, use it 1402868aabb4SRichard Scheffenegger */ 1403868aabb4SRichard Scheffenegger if ((*mp)->m_flags & M_VLANTAG) { 1404c7cffd65SAlexander V. Chernikov qtag->pcp = EVL_PRIOFTAG((*mp)->m_pkthdr.ether_vtag); 1405868aabb4SRichard Scheffenegger } 1406868aabb4SRichard Scheffenegger 1407868aabb4SRichard Scheffenegger /* 1408f1379734SKonstantin Belousov * If underlying interface can do VLAN tag insertion itself, 1409f1379734SKonstantin Belousov * just pass the packet along. However, we need some way to 1410f1379734SKonstantin Belousov * tell the interface where the packet came from so that it 1411f1379734SKonstantin Belousov * knows how to find the VLAN tag to use, so we attach a 1412f1379734SKonstantin Belousov * packet tag that holds it. 1413f1379734SKonstantin Belousov */ 1414f1379734SKonstantin Belousov if (vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q, 1415f1379734SKonstantin Belousov MTAG_8021Q_PCP_OUT, NULL)) != NULL) 1416c7cffd65SAlexander V. Chernikov tag = EVL_MAKETAG(qtag->vid, *(uint8_t *)(mtag + 1), 0); 1417f1379734SKonstantin Belousov else 1418c7cffd65SAlexander V. Chernikov tag = EVL_MAKETAG(qtag->vid, qtag->pcp, 0); 1419c7cffd65SAlexander V. Chernikov if ((p->if_capenable & IFCAP_VLAN_HWTAGGING) && 1420c7cffd65SAlexander V. Chernikov (qtag->proto == ETHERTYPE_VLAN)) { 1421f1379734SKonstantin Belousov (*mp)->m_pkthdr.ether_vtag = tag; 1422f1379734SKonstantin Belousov (*mp)->m_flags |= M_VLANTAG; 1423f1379734SKonstantin Belousov } else { 1424c7cffd65SAlexander V. Chernikov *mp = ether_vlanencap_proto(*mp, tag, qtag->proto); 1425f1379734SKonstantin Belousov if (*mp == NULL) { 1426f1379734SKonstantin Belousov if_printf(ife, "unable to prepend 802.1Q header"); 1427f1379734SKonstantin Belousov return (false); 1428f1379734SKonstantin Belousov } 1429f1379734SKonstantin Belousov } 1430f1379734SKonstantin Belousov return (true); 1431f1379734SKonstantin Belousov } 1432f1379734SKonstantin Belousov 14336b7e0c1cSKyle Evans /* 14343c3aa8c1SKyle Evans * Allocate an address from the FreeBSD Foundation OUI. This uses a 14353f8bc99cSKristof Provost * cryptographic hash function on the containing jail's name, UUID and the 14363f8bc99cSKristof Provost * interface name to attempt to provide a unique but stable address. 14373f8bc99cSKristof Provost * Pseudo-interfaces which require a MAC address should use this function to 14383f8bc99cSKristof Provost * allocate non-locally-administered addresses. 14396b7e0c1cSKyle Evans */ 14403c3aa8c1SKyle Evans void 14413c3aa8c1SKyle Evans ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) 14423c3aa8c1SKyle Evans { 14433c3aa8c1SKyle Evans SHA1_CTX ctx; 14443f8bc99cSKristof Provost char *buf; 14453c3aa8c1SKyle Evans char uuid[HOSTUUIDLEN + 1]; 14463c3aa8c1SKyle Evans uint64_t addr; 14473c3aa8c1SKyle Evans int i, sz; 14483c3aa8c1SKyle Evans char digest[SHA1_RESULTLEN]; 14493f8bc99cSKristof Provost char jailname[MAXHOSTNAMELEN]; 14503c3aa8c1SKyle Evans 14513c3aa8c1SKyle Evans getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); 14522d741f33SKyle Evans if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) { 14532d741f33SKyle Evans /* Fall back to a random mac address. */ 14542d741f33SKyle Evans goto rando; 14552d741f33SKyle Evans } 14562d741f33SKyle Evans 14573f8bc99cSKristof Provost /* If each (vnet) jail would also have a unique hostuuid this would not 14583f8bc99cSKristof Provost * be necessary. */ 14593f8bc99cSKristof Provost getjailname(curthread->td_ucred, jailname, sizeof(jailname)); 14603f8bc99cSKristof Provost sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, if_name(ifp), 14613f8bc99cSKristof Provost jailname); 14623f8bc99cSKristof Provost if (sz < 0) { 14633f8bc99cSKristof Provost /* Fall back to a random mac address. */ 14642d741f33SKyle Evans goto rando; 14653f8bc99cSKristof Provost } 14663f8bc99cSKristof Provost 14673c3aa8c1SKyle Evans SHA1Init(&ctx); 14683c3aa8c1SKyle Evans SHA1Update(&ctx, buf, sz); 14693c3aa8c1SKyle Evans SHA1Final(digest, &ctx); 14703f8bc99cSKristof Provost free(buf, M_TEMP); 14713c3aa8c1SKyle Evans 14723c3aa8c1SKyle Evans addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) & 14733c3aa8c1SKyle Evans OUI_FREEBSD_GENERATED_MASK; 14743c3aa8c1SKyle Evans addr = OUI_FREEBSD(addr); 14753c3aa8c1SKyle Evans for (i = 0; i < ETHER_ADDR_LEN; ++i) { 14763c3aa8c1SKyle Evans hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & 14773c3aa8c1SKyle Evans 0xFF; 14783c3aa8c1SKyle Evans } 14792d741f33SKyle Evans 14802d741f33SKyle Evans return; 14812d741f33SKyle Evans rando: 14822d741f33SKyle Evans arc4rand(hwaddr, sizeof(*hwaddr), 0); 14832d741f33SKyle Evans /* Unicast */ 14842d741f33SKyle Evans hwaddr->octet[0] &= 0xFE; 14852d741f33SKyle Evans /* Locally administered. */ 14862d741f33SKyle Evans hwaddr->octet[0] |= 0x02; 14876b7e0c1cSKyle Evans } 14886b7e0c1cSKyle Evans 1489fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 149040811c14SMatthew N. Dodd MODULE_VERSION(ether, 1); 1491