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 321d5e9e22SEivind Eklund #include "opt_inet.h" 33cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 344cf49a43SJulian Elischer #include "opt_netgraph.h" 356eeac1d9SJulian Elischer #include "opt_mbuf_profiling.h" 367527624eSRobert Watson #include "opt_rss.h" 37430df5f4SEivind Eklund 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40773e541eSWarner Losh #include <sys/devctl.h> 418819ad85SSepherosa Ziehau #include <sys/eventhandler.h> 423c3aa8c1SKyle Evans #include <sys/jail.h> 43df8bae1dSRodney W. Grimes #include <sys/kernel.h> 44385195c0SMarko Zec #include <sys/lock.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 4724fe4612SMark Johnston #include <sys/module.h> 4824fe4612SMark Johnston #include <sys/msan.h> 493c3aa8c1SKyle Evans #include <sys/proc.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> 56a6b55ee6SGleb Smirnoff #ifdef KDB 57a6b55ee6SGleb Smirnoff #include <sys/kdb.h> 58a6b55ee6SGleb Smirnoff #endif 59df8bae1dSRodney W. Grimes 603c3aa8c1SKyle Evans #include <net/ieee_oui.h> 61df8bae1dSRodney W. Grimes #include <net/if.h> 6276039bc8SGleb Smirnoff #include <net/if_var.h> 632c2b37adSJustin Hibbits #include <net/if_private.h> 64d7647d96SDag-Erling Smørgrav #include <net/if_arp.h> 65df8bae1dSRodney W. Grimes #include <net/netisr.h> 66df8bae1dSRodney W. Grimes #include <net/route.h> 67df8bae1dSRodney W. Grimes #include <net/if_llc.h> 68df8bae1dSRodney W. Grimes #include <net/if_dl.h> 69df8bae1dSRodney W. Grimes #include <net/if_types.h> 702e2de7f2SArchie Cobbs #include <net/bpf.h> 71e1e1452dSArchie Cobbs #include <net/ethernet.h> 72fd6238a6SAndrew Thompson #include <net/if_bridgevar.h> 73c1d93b05SSam Leffler #include <net/if_vlan_var.h> 746e6b3f7cSQing Li #include <net/if_llatbl.h> 757d4317bdSAlexander V. Chernikov #include <net/pfil.h> 76b2bdc62aSAdrian Chadd #include <net/rss_config.h> 774b79449eSBjoern A. Zeeb #include <net/vnet.h> 78df8bae1dSRodney W. Grimes 7975bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h> 8075bf2db3SGleb Smirnoff 8182cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6) 82df8bae1dSRodney W. Grimes #include <netinet/in.h> 83df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 8554bfbd51SWill Andrews #include <netinet/ip_carp.h> 860bcfa8e4SLuigi Rizzo #include <netinet/ip_var.h> 871d5e9e22SEivind Eklund #endif 8882cd038dSYoshinobu Inoue #ifdef INET6 8982cd038dSYoshinobu Inoue #include <netinet6/nd6.h> 9082cd038dSYoshinobu Inoue #endif 91aed55708SRobert Watson #include <security/mac/mac_framework.h> 92aed55708SRobert Watson 933c3aa8c1SKyle Evans #include <crypto/sha1.h> 943c3aa8c1SKyle Evans 95d54d93acSEd Maste #ifdef CTASSERT 96d54d93acSEd Maste CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2); 97d54d93acSEd Maste CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN); 98d54d93acSEd Maste #endif 99d54d93acSEd Maste 100b252313fSGleb Smirnoff VNET_DEFINE(pfil_head_t, link_pfil_head); /* Packet filter hooks */ 1017d4317bdSAlexander V. Chernikov 102e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */ 103c1d93b05SSam Leffler void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 104c1d93b05SSam Leffler void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 105e1e1452dSArchie Cobbs int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 106e1e1452dSArchie Cobbs void (*ng_ether_attach_p)(struct ifnet *ifp); 107e1e1452dSArchie Cobbs void (*ng_ether_detach_p)(struct ifnet *ifp); 108e1e1452dSArchie Cobbs 109824eb9dcSDavid E. O'Brien void (*vlan_input_p)(struct ifnet *, struct mbuf *); 1109d4fe4b2SBrooks Davis 111fd6238a6SAndrew Thompson /* if_bridge(4) support */ 112c8b01292SAndrew Thompson void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 1138f867517SAndrew Thompson 11418242d3bSAndrew Thompson /* if_lagg(4) support */ 115a92c4bb6SHans Petter Selasky struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *); 116b47888ceSAndrew Thompson 117e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 118868d8b62SMatthew N. Dodd { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 119868d8b62SMatthew N. Dodd 120929ddbbbSAlfred Perlstein static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 121929ddbbbSAlfred Perlstein struct sockaddr *); 1224fb3a820SAlexander V. Chernikov static int ether_requestencap(struct ifnet *, struct if_encap_req *); 123868d8b62SMatthew N. Dodd 12449d6743dSZhenlei Huang static inline bool ether_do_pcp(struct ifnet *, struct mbuf *); 12549d6743dSZhenlei Huang 1264c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0) 127df8bae1dSRodney W. Grimes 12822f8ce43SAlexander V. Chernikov static void 12922f8ce43SAlexander V. Chernikov update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 13022f8ce43SAlexander V. Chernikov { 13122f8ce43SAlexander V. Chernikov int csum_flags = 0; 13222f8ce43SAlexander V. Chernikov 13322f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_IP) 13422f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 13522f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 13622f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 13722f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_SCTP) 13822f8ce43SAlexander V. Chernikov csum_flags |= CSUM_SCTP_VALID; 13922f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_flags |= csum_flags; 14022f8ce43SAlexander V. Chernikov if (csum_flags & CSUM_DATA_VALID) 14122f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_data = 0xffff; 14222f8ce43SAlexander V. Chernikov } 14322f8ce43SAlexander V. Chernikov 144df8bae1dSRodney W. Grimes /* 1454fb3a820SAlexander V. Chernikov * Handle link-layer encapsulation requests. 1464fb3a820SAlexander V. Chernikov */ 1474fb3a820SAlexander V. Chernikov static int 1484fb3a820SAlexander V. Chernikov ether_requestencap(struct ifnet *ifp, struct if_encap_req *req) 1494fb3a820SAlexander V. Chernikov { 1504fb3a820SAlexander V. Chernikov struct ether_header *eh; 1514fb3a820SAlexander V. Chernikov struct arphdr *ah; 1524fb3a820SAlexander V. Chernikov uint16_t etype; 1534fb3a820SAlexander V. Chernikov const u_char *lladdr; 1544fb3a820SAlexander V. Chernikov 1554fb3a820SAlexander V. Chernikov if (req->rtype != IFENCAP_LL) 1564fb3a820SAlexander V. Chernikov return (EOPNOTSUPP); 1574fb3a820SAlexander V. Chernikov 1584fb3a820SAlexander V. Chernikov if (req->bufsize < ETHER_HDR_LEN) 1594fb3a820SAlexander V. Chernikov return (ENOMEM); 1604fb3a820SAlexander V. Chernikov 1614fb3a820SAlexander V. Chernikov eh = (struct ether_header *)req->buf; 1624fb3a820SAlexander V. Chernikov lladdr = req->lladdr; 1634fb3a820SAlexander V. Chernikov req->lladdr_off = 0; 1644fb3a820SAlexander V. Chernikov 1654fb3a820SAlexander V. Chernikov switch (req->family) { 1664fb3a820SAlexander V. Chernikov case AF_INET: 1674fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IP); 1684fb3a820SAlexander V. Chernikov break; 1694fb3a820SAlexander V. Chernikov case AF_INET6: 1704fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 1714fb3a820SAlexander V. Chernikov break; 1724fb3a820SAlexander V. Chernikov case AF_ARP: 1734fb3a820SAlexander V. Chernikov ah = (struct arphdr *)req->hdata; 1744fb3a820SAlexander V. Chernikov ah->ar_hrd = htons(ARPHRD_ETHER); 1754fb3a820SAlexander V. Chernikov 1764fb3a820SAlexander V. Chernikov switch(ntohs(ah->ar_op)) { 1774fb3a820SAlexander V. Chernikov case ARPOP_REVREQUEST: 1784fb3a820SAlexander V. Chernikov case ARPOP_REVREPLY: 1794fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_REVARP); 1804fb3a820SAlexander V. Chernikov break; 1814fb3a820SAlexander V. Chernikov case ARPOP_REQUEST: 1824fb3a820SAlexander V. Chernikov case ARPOP_REPLY: 1834fb3a820SAlexander V. Chernikov default: 1844fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_ARP); 1854fb3a820SAlexander V. Chernikov break; 1864fb3a820SAlexander V. Chernikov } 1874fb3a820SAlexander V. Chernikov 1884fb3a820SAlexander V. Chernikov if (req->flags & IFENCAP_FLAG_BROADCAST) 1894fb3a820SAlexander V. Chernikov lladdr = ifp->if_broadcastaddr; 1904fb3a820SAlexander V. Chernikov break; 1914fb3a820SAlexander V. Chernikov default: 1924fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 1934fb3a820SAlexander V. Chernikov } 1944fb3a820SAlexander V. Chernikov 1954fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 1964fb3a820SAlexander V. Chernikov memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN); 1974fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 1984fb3a820SAlexander V. Chernikov req->bufsize = sizeof(struct ether_header); 1994fb3a820SAlexander V. Chernikov 2004fb3a820SAlexander V. Chernikov return (0); 2014fb3a820SAlexander V. Chernikov } 2024fb3a820SAlexander V. Chernikov 2034fb3a820SAlexander V. Chernikov static int 2044fb3a820SAlexander V. Chernikov ether_resolve_addr(struct ifnet *ifp, struct mbuf *m, 2054fb3a820SAlexander V. Chernikov const struct sockaddr *dst, struct route *ro, u_char *phdr, 2066d768226SGeorge V. Neville-Neil uint32_t *pflags, struct llentry **plle) 2074fb3a820SAlexander V. Chernikov { 2084fb3a820SAlexander V. Chernikov uint32_t lleflags = 0; 2094fb3a820SAlexander V. Chernikov int error = 0; 2104fb3a820SAlexander V. Chernikov #if defined(INET) || defined(INET6) 211d9898158SJohn Baldwin struct ether_header *eh = (struct ether_header *)phdr; 2124fb3a820SAlexander V. Chernikov uint16_t etype; 2134fb3a820SAlexander V. Chernikov #endif 2144fb3a820SAlexander V. Chernikov 2156d768226SGeorge V. Neville-Neil if (plle) 2166d768226SGeorge V. Neville-Neil *plle = NULL; 2174fb3a820SAlexander V. Chernikov 2184fb3a820SAlexander V. Chernikov switch (dst->sa_family) { 2194fb3a820SAlexander V. Chernikov #ifdef INET 2204fb3a820SAlexander V. Chernikov case AF_INET: 2214fb3a820SAlexander V. Chernikov if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) 2226d768226SGeorge V. Neville-Neil error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, 2236d768226SGeorge V. Neville-Neil plle); 2244fb3a820SAlexander V. Chernikov else { 2254fb3a820SAlexander V. Chernikov if (m->m_flags & M_BCAST) 2264fb3a820SAlexander V. Chernikov memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 2274fb3a820SAlexander V. Chernikov ETHER_ADDR_LEN); 2284fb3a820SAlexander V. Chernikov else { 2294fb3a820SAlexander V. Chernikov const struct in_addr *a; 2304fb3a820SAlexander V. Chernikov a = &(((const struct sockaddr_in *)dst)->sin_addr); 2314fb3a820SAlexander V. Chernikov ETHER_MAP_IP_MULTICAST(a, eh->ether_dhost); 2324fb3a820SAlexander V. Chernikov } 2334fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IP); 2344fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(etype)); 2354fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 2364fb3a820SAlexander V. Chernikov } 2374fb3a820SAlexander V. Chernikov break; 2384fb3a820SAlexander V. Chernikov #endif 2394fb3a820SAlexander V. Chernikov #ifdef INET6 2404fb3a820SAlexander V. Chernikov case AF_INET6: 24162e1a437SZhenlei Huang if ((m->m_flags & M_MCAST) == 0) { 24262e1a437SZhenlei Huang int af = RO_GET_FAMILY(ro, dst); 24362e1a437SZhenlei Huang error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr, 244c541bd36SAlexander V. Chernikov &lleflags, plle); 24562e1a437SZhenlei Huang } else { 2464fb3a820SAlexander V. Chernikov const struct in6_addr *a6; 2474fb3a820SAlexander V. Chernikov a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr); 2484fb3a820SAlexander V. Chernikov ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost); 2494fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 2504fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(etype)); 2514fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 2524fb3a820SAlexander V. Chernikov } 2534fb3a820SAlexander V. Chernikov break; 2544fb3a820SAlexander V. Chernikov #endif 2554fb3a820SAlexander V. Chernikov default: 2564fb3a820SAlexander V. Chernikov if_printf(ifp, "can't handle af%d\n", dst->sa_family); 2574fb3a820SAlexander V. Chernikov if (m != NULL) 2584fb3a820SAlexander V. Chernikov m_freem(m); 2594fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 2604fb3a820SAlexander V. Chernikov } 2614fb3a820SAlexander V. Chernikov 2624fb3a820SAlexander V. Chernikov if (error == EHOSTDOWN) { 26336402a68SAlexander V. Chernikov if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 2644fb3a820SAlexander V. Chernikov error = EHOSTUNREACH; 2654fb3a820SAlexander V. Chernikov } 2664fb3a820SAlexander V. Chernikov 2674fb3a820SAlexander V. Chernikov if (error != 0) 2684fb3a820SAlexander V. Chernikov return (error); 2694fb3a820SAlexander V. Chernikov 2704fb3a820SAlexander V. Chernikov *pflags = RT_MAY_LOOP; 2714fb3a820SAlexander V. Chernikov if (lleflags & LLE_IFADDR) 2724fb3a820SAlexander V. Chernikov *pflags |= RT_L2_ME; 2734fb3a820SAlexander V. Chernikov 2744fb3a820SAlexander V. Chernikov return (0); 2754fb3a820SAlexander V. Chernikov } 2764fb3a820SAlexander V. Chernikov 2774fb3a820SAlexander V. Chernikov /* 278df8bae1dSRodney W. Grimes * Ethernet output routine. 279df8bae1dSRodney W. Grimes * Encapsulate a packet of type family for the local net. 280df8bae1dSRodney W. Grimes * Use trailer local net encapsulation if enough data in first 281df8bae1dSRodney W. Grimes * packet leaves a multiple of 512 bytes of data in remainder. 282df8bae1dSRodney W. Grimes */ 283df8bae1dSRodney W. Grimes int 28472fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m, 28547e8d432SGleb Smirnoff const struct sockaddr *dst, struct route *ro) 286df8bae1dSRodney W. Grimes { 2874fb3a820SAlexander V. Chernikov int error = 0; 2884fb3a820SAlexander V. Chernikov char linkhdr[ETHER_HDR_LEN], *phdr; 289098a8c3bSMatthew N. Dodd struct ether_header *eh; 29060ee3847SMax Laier struct pf_mtag *t; 291ddce63fcSHans Petter Selasky bool loop_copy; 292d32864c3SRobert Watson int hlen; /* link layer header length */ 2934fb3a820SAlexander V. Chernikov uint32_t pflags; 2946d768226SGeorge V. Neville-Neil struct llentry *lle = NULL; 2956d768226SGeorge V. Neville-Neil int addref = 0; 296df8bae1dSRodney W. Grimes 2974fb3a820SAlexander V. Chernikov phdr = NULL; 2984fb3a820SAlexander V. Chernikov pflags = 0; 299279aa3d4SKip Macy if (ro != NULL) { 3006d768226SGeorge V. Neville-Neil /* XXX BPF uses ro_prepend */ 3016d768226SGeorge V. Neville-Neil if (ro->ro_prepend != NULL) { 3024fb3a820SAlexander V. Chernikov phdr = ro->ro_prepend; 3034fb3a820SAlexander V. Chernikov hlen = ro->ro_plen; 3046d768226SGeorge V. Neville-Neil } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 3056d768226SGeorge V. Neville-Neil if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 3066d768226SGeorge V. Neville-Neil lle = ro->ro_lle; 3076d768226SGeorge V. Neville-Neil if (lle != NULL && 3086d768226SGeorge V. Neville-Neil (lle->la_flags & LLE_VALID) == 0) { 3096d768226SGeorge V. Neville-Neil LLE_FREE(lle); 3106d768226SGeorge V. Neville-Neil lle = NULL; /* redundant */ 3116d768226SGeorge V. Neville-Neil ro->ro_lle = NULL; 312279aa3d4SKip Macy } 3136d768226SGeorge V. Neville-Neil if (lle == NULL) { 3146d768226SGeorge V. Neville-Neil /* if we lookup, keep cache */ 3156d768226SGeorge V. Neville-Neil addref = 1; 3161435dcd9SAlexander V. Chernikov } else 3171435dcd9SAlexander V. Chernikov /* 3181435dcd9SAlexander V. Chernikov * Notify LLE code that 3191435dcd9SAlexander V. Chernikov * the entry was used 3201435dcd9SAlexander V. Chernikov * by datapath. 3211435dcd9SAlexander V. Chernikov */ 322f3a3b061SAlexander V. Chernikov llentry_provide_feedback(lle); 3236d768226SGeorge V. Neville-Neil } 3246d768226SGeorge V. Neville-Neil if (lle != NULL) { 3256d768226SGeorge V. Neville-Neil phdr = lle->r_linkdata; 3266d768226SGeorge V. Neville-Neil hlen = lle->r_hdrlen; 3276d768226SGeorge V. Neville-Neil pflags = lle->r_flags; 3286d768226SGeorge V. Neville-Neil } 3296d768226SGeorge V. Neville-Neil } 3306d768226SGeorge V. Neville-Neil } 3316d768226SGeorge V. Neville-Neil 33243b29369SRobert Watson #ifdef MAC 33330d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 33443b29369SRobert Watson if (error) 33543b29369SRobert Watson senderr(error); 33643b29369SRobert Watson #endif 33743b29369SRobert Watson 3386eeac1d9SJulian Elischer M_PROFILE(m); 339afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) 340afbe3a0fSPoul-Henning Kamp senderr(ENETDOWN); 34113f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 34213f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 343df8bae1dSRodney W. Grimes senderr(ENETDOWN); 3447f760c48SMatthew N. Dodd 3454fb3a820SAlexander V. Chernikov if (phdr == NULL) { 3464fb3a820SAlexander V. Chernikov /* No prepend data supplied. Try to calculate ourselves. */ 3474fb3a820SAlexander V. Chernikov phdr = linkhdr; 34884dd0fd0SJulian Elischer hlen = ETHER_HDR_LEN; 3496d768226SGeorge V. Neville-Neil error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 3506d768226SGeorge V. Neville-Neil addref ? &lle : NULL); 3516d768226SGeorge V. Neville-Neil if (addref && lle != NULL) 3526d768226SGeorge V. Neville-Neil ro->ro_lle = lle; 3534fb3a820SAlexander V. Chernikov if (error != 0) 354cd46a114SLuigi Rizzo return (error == EWOULDBLOCK ? 0 : error); 35543a6c75aSMatthew N. Dodd } 35643a6c75aSMatthew N. Dodd 3574fb3a820SAlexander V. Chernikov if ((pflags & RT_L2_ME) != 0) { 35822f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, m); 35962e1a437SZhenlei Huang return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0)); 3606e6b3f7cSQing Li } 361ddce63fcSHans Petter Selasky loop_copy = (pflags & RT_MAY_LOOP) != 0; 3626e6b3f7cSQing Li 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * Add local net header. If no space in first mbuf, 365df8bae1dSRodney W. Grimes * allocate another. 3666cdb1854SAlexander V. Chernikov * 3676cdb1854SAlexander V. Chernikov * Note that we do prepend regardless of RT_HAS_HEADER flag. 3686cdb1854SAlexander V. Chernikov * This is done because BPF code shifts m_data pointer 3696cdb1854SAlexander V. Chernikov * to the end of ethernet header prior to calling if_output(). 370df8bae1dSRodney W. Grimes */ 3714fb3a820SAlexander V. Chernikov M_PREPEND(m, hlen, M_NOWAIT); 3722166ffe8SRobert Watson if (m == NULL) 373df8bae1dSRodney W. Grimes senderr(ENOBUFS); 3744fb3a820SAlexander V. Chernikov if ((pflags & RT_HAS_HEADER) == 0) { 375df8bae1dSRodney W. Grimes eh = mtod(m, struct ether_header *); 3764fb3a820SAlexander V. Chernikov memcpy(eh, phdr, hlen); 377c69aeaadSAlexander V. Chernikov } 378ed7509acSJulian Elischer 379ed7509acSJulian Elischer /* 380ed7509acSJulian Elischer * If a simplex interface, and the packet is being sent to our 381ed7509acSJulian Elischer * Ethernet address or a broadcast address, loopback a copy. 382ed7509acSJulian Elischer * XXX To make a simplex device behave exactly like a duplex 383ed7509acSJulian Elischer * device, we should copy in the case of sending to our own 384ed7509acSJulian Elischer * ethernet address (thus letting the original actually appear 385ed7509acSJulian Elischer * on the wire). However, we don't do that here for security 386ed7509acSJulian Elischer * reasons and compatibility with the original behavior. 387ed7509acSJulian Elischer */ 3884fb3a820SAlexander V. Chernikov if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) && 38960ee3847SMax Laier ((t = pf_find_mtag(m)) == NULL || !t->routed)) { 39006f684b0SMatthew N. Dodd struct mbuf *n; 391ed7509acSJulian Elischer 392294dd290SLuigi Rizzo /* 393294dd290SLuigi Rizzo * Because if_simloop() modifies the packet, we need a 394294dd290SLuigi Rizzo * writable copy through m_dup() instead of a readonly 395294dd290SLuigi Rizzo * one as m_copy[m] would give us. The alternative would 396294dd290SLuigi Rizzo * be to modify if_simloop() to handle the readonly mbuf, 397294dd290SLuigi Rizzo * but performancewise it is mostly equivalent (trading 398294dd290SLuigi Rizzo * extra data copying vs. extra locking). 3999983b3c0SYaroslav Tykhiy * 4009983b3c0SYaroslav Tykhiy * XXX This is a local workaround. A number of less 4019983b3c0SYaroslav Tykhiy * often used kernel parts suffer from the same bug. 4029983b3c0SYaroslav Tykhiy * See PR kern/105943 for a proposed general solution. 403294dd290SLuigi Rizzo */ 404eb1b1807SGleb Smirnoff if ((n = m_dup(m, M_NOWAIT)) != NULL) { 40522f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, n); 40662e1a437SZhenlei Huang (void)if_simloop(ifp, n, RO_GET_FAMILY(ro, dst), hlen); 407c1404dc0SArchie Cobbs } else 4083751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 409ed7509acSJulian Elischer } 4102e2de7f2SArchie Cobbs 4113f7d1396SAndrew Thompson /* 4123f7d1396SAndrew Thompson * Bridges require special output handling. 4133f7d1396SAndrew Thompson */ 4143f7d1396SAndrew Thompson if (ifp->if_bridge) { 4153f7d1396SAndrew Thompson BRIDGE_OUTPUT(ifp, m, error); 4163f7d1396SAndrew Thompson return (error); 4173f7d1396SAndrew Thompson } 4183f7d1396SAndrew Thompson 419259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 420a9771948SGleb Smirnoff if (ifp->if_carp && 42108b68b0eSGleb Smirnoff (error = (*carp_output_p)(ifp, m, dst))) 422a9771948SGleb Smirnoff goto bad; 423a9771948SGleb Smirnoff #endif 424a9771948SGleb Smirnoff 425e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 426833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 427514bcb89SPoul-Henning Kamp KASSERT(ng_ether_output_p != NULL, 428514bcb89SPoul-Henning Kamp ("ng_ether_output_p is NULL")); 429e1e1452dSArchie Cobbs if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 430e1e1452dSArchie Cobbs bad: if (m != NULL) 431e1e1452dSArchie Cobbs m_freem(m); 432e1e1452dSArchie Cobbs return (error); 433e1e1452dSArchie Cobbs } 434e1e1452dSArchie Cobbs if (m == NULL) 435e1e1452dSArchie Cobbs return (0); 436e1e1452dSArchie Cobbs } 437e1e1452dSArchie Cobbs 438e1e1452dSArchie Cobbs /* Continue with link-layer output */ 439e1e1452dSArchie Cobbs return ether_output_frame(ifp, m); 440e1e1452dSArchie Cobbs } 441e1e1452dSArchie Cobbs 442f1379734SKonstantin Belousov static bool 443f1379734SKonstantin Belousov ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp) 444f1379734SKonstantin Belousov { 445c7cffd65SAlexander V. Chernikov struct ether_8021q_tag qtag; 446f1379734SKonstantin Belousov struct ether_header *eh; 447f1379734SKonstantin Belousov 448f1379734SKonstantin Belousov eh = mtod(*mp, struct ether_header *); 449b22aae41SZhenlei Huang if (eh->ether_type == htons(ETHERTYPE_VLAN) || 450b22aae41SZhenlei Huang eh->ether_type == htons(ETHERTYPE_QINQ)) { 451b22aae41SZhenlei Huang (*mp)->m_flags &= ~M_VLANTAG; 452c7cffd65SAlexander V. Chernikov return (true); 453b22aae41SZhenlei Huang } 454c7cffd65SAlexander V. Chernikov 455c7cffd65SAlexander V. Chernikov qtag.vid = 0; 456c7cffd65SAlexander V. Chernikov qtag.pcp = pcp; 457c7cffd65SAlexander V. Chernikov qtag.proto = ETHERTYPE_VLAN; 458c7cffd65SAlexander V. Chernikov if (ether_8021q_frame(mp, ifp, ifp, &qtag)) 459f1379734SKonstantin Belousov return (true); 460f1379734SKonstantin Belousov if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 461f1379734SKonstantin Belousov return (false); 462f1379734SKonstantin Belousov } 463f1379734SKonstantin Belousov 464e1e1452dSArchie Cobbs /* 465e1e1452dSArchie Cobbs * Ethernet link layer output routine to send a raw frame to the device. 466e1e1452dSArchie Cobbs * 467e1e1452dSArchie Cobbs * This assumes that the 14 byte Ethernet header is present and contiguous 468e1e1452dSArchie Cobbs * in the first mbuf (if BRIDGE'ing). 469e1e1452dSArchie Cobbs */ 470e1e1452dSArchie Cobbs int 471c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m) 472e1e1452dSArchie Cobbs { 47349d6743dSZhenlei Huang if (ether_do_pcp(ifp, m) && !ether_set_pcp(&m, ifp, ifp->if_pcp)) 474f1379734SKonstantin Belousov return (0); 4752b25acc1SLuigi Rizzo 476b252313fSGleb Smirnoff if (PFIL_HOOKED_OUT(V_link_pfil_head)) 477a2256150SGleb Smirnoff switch (pfil_mbuf_out(V_link_pfil_head, &m, ifp, NULL)) { 478b252313fSGleb Smirnoff case PFIL_DROPPED: 4797d4317bdSAlexander V. Chernikov return (EACCES); 480b252313fSGleb Smirnoff case PFIL_CONSUMED: 4817d4317bdSAlexander V. Chernikov return (0); 4824b984093SLuigi Rizzo } 4832b25acc1SLuigi Rizzo 484201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL 485201100c5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 486201100c5SBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 48721231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 48821231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 489201100c5SBjoern A. Zeeb struct ether_header *eh; 490201100c5SBjoern A. Zeeb 491201100c5SBjoern A. Zeeb eh = mtod(m, struct ether_header *); 492201100c5SBjoern A. Zeeb switch (ntohs(eh->ether_type)) { 493201100c5SBjoern A. Zeeb case ETHERTYPE_IP: 494201100c5SBjoern A. Zeeb case ETHERTYPE_ARP: 49521231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 496201100c5SBjoern A. Zeeb m_freem(m); 497201100c5SBjoern A. Zeeb return (EAFNOSUPPORT); 498201100c5SBjoern A. Zeeb /* NOTREACHED */ 499201100c5SBjoern A. Zeeb break; 500201100c5SBjoern A. Zeeb }; 501201100c5SBjoern A. Zeeb } 502201100c5SBjoern A. Zeeb #endif 503201100c5SBjoern A. Zeeb #endif 504201100c5SBjoern A. Zeeb 505df8bae1dSRodney W. Grimes /* 50624fe4612SMark Johnston * Queue message on interface, update output statistics if successful, 50724fe4612SMark Johnston * and start output if interface not yet active. 50824fe4612SMark Johnston * 50924fe4612SMark Johnston * If KMSAN is enabled, use it to verify that the data does not contain 51024fe4612SMark Johnston * any uninitialized bytes. 511df8bae1dSRodney W. Grimes */ 51224fe4612SMark Johnston kmsan_check_mbuf(m, "ether_output"); 513aea78d20SKip Macy return ((ifp->if_transmit)(ifp, m)); 514df8bae1dSRodney W. Grimes } 515df8bae1dSRodney W. Grimes 5164b984093SLuigi Rizzo /* 517c1d93b05SSam Leffler * Process a received Ethernet packet; the packet is in the 518c1d93b05SSam Leffler * mbuf chain m with the ethernet header at the front. 519df8bae1dSRodney W. Grimes */ 520c1d93b05SSam Leffler static void 5216cb52192SRobert Watson ether_input_internal(struct ifnet *ifp, struct mbuf *m) 522df8bae1dSRodney W. Grimes { 523c1d93b05SSam Leffler struct ether_header *eh; 524c1d93b05SSam Leffler u_short etype; 525df8bae1dSRodney W. Grimes 52669462a82SBruce M Simpson if ((ifp->if_flags & IFF_UP) == 0) { 52769462a82SBruce M Simpson m_freem(m); 52869462a82SBruce M Simpson return; 52969462a82SBruce M Simpson } 53069462a82SBruce M Simpson #ifdef DIAGNOSTIC 53169462a82SBruce M Simpson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 53269462a82SBruce M Simpson if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n"); 53369462a82SBruce M Simpson m_freem(m); 53469462a82SBruce M Simpson return; 53569462a82SBruce M Simpson } 53669462a82SBruce M Simpson #endif 537*e7102929SZhenlei Huang if (__predict_false(m->m_len < ETHER_HDR_LEN)) { 538*e7102929SZhenlei Huang /* Drivers should pullup and ensure the mbuf is valid */ 539c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o leading ethernet " 540*e7102929SZhenlei Huang "header (len %d pkt len %d)\n", 541c1d93b05SSam Leffler m->m_len, m->m_pkthdr.len); 5423751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 543c1d93b05SSam Leffler m_freem(m); 544c1d93b05SSam Leffler return; 5452e2de7f2SArchie Cobbs } 546c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 547c1d93b05SSam Leffler etype = ntohs(eh->ether_type); 54819fa89e9SMark Murray random_harvest_queue_ether(m, sizeof(*m)); 549d1b06863SMark Murray 55021231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL 55121231a7aSBjoern A. Zeeb #if defined(INET6) && defined(INET) 55221231a7aSBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 55321231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 55421231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 55521231a7aSBjoern A. Zeeb switch (etype) { 55621231a7aSBjoern A. Zeeb case ETHERTYPE_IP: 55721231a7aSBjoern A. Zeeb case ETHERTYPE_ARP: 55821231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 55921231a7aSBjoern A. Zeeb m_freem(m); 56021231a7aSBjoern A. Zeeb return; 56121231a7aSBjoern A. Zeeb /* NOTREACHED */ 56221231a7aSBjoern A. Zeeb break; 56321231a7aSBjoern A. Zeeb }; 56421231a7aSBjoern A. Zeeb } 56521231a7aSBjoern A. Zeeb #endif 56621231a7aSBjoern A. Zeeb #endif 56721231a7aSBjoern A. Zeeb 56821ca7b57SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet); 56921ca7b57SMarko Zec 57069462a82SBruce M Simpson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 57169462a82SBruce M Simpson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 57269462a82SBruce M Simpson m->m_flags |= M_BCAST; 57369462a82SBruce M Simpson else 57469462a82SBruce M Simpson m->m_flags |= M_MCAST; 5753751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 57669462a82SBruce M Simpson } 57769462a82SBruce M Simpson 578533d8562SRobert Watson #ifdef MAC 579533d8562SRobert Watson /* 580533d8562SRobert Watson * Tag the mbuf with an appropriate MAC label before any other 581533d8562SRobert Watson * consumers can get to it. 582533d8562SRobert Watson */ 58330d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 584533d8562SRobert Watson #endif 585533d8562SRobert Watson 586c1d93b05SSam Leffler /* 58769462a82SBruce M Simpson * Give bpf a chance at the packet. 588c1d93b05SSam Leffler */ 589d05d4616SChristian S.J. Peron ETHER_BPF_MTAP(ifp, m); 5902e2de7f2SArchie Cobbs 5914cdc1f54SGleb Smirnoff if (!(ifp->if_capenable & IFCAP_HWSTATS)) 5923751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 59305463bb5SDavid Greenman 59469462a82SBruce M Simpson /* Allow monitor mode to claim this frame, after stats are updated. */ 595de572b37SChristian S.J. Peron if (ifp->if_flags & IFF_MONITOR) { 596de572b37SChristian S.J. Peron m_freem(m); 59721ca7b57SMarko Zec CURVNET_RESTORE(); 598de572b37SChristian S.J. Peron return; 599de572b37SChristian S.J. Peron } 600de572b37SChristian S.J. Peron 60118242d3bSAndrew Thompson /* Handle input from a lagg(4) port */ 602b47888ceSAndrew Thompson if (ifp->if_type == IFT_IEEE8023ADLAG) { 603a92c4bb6SHans Petter Selasky KASSERT(lagg_input_ethernet_p != NULL, 60418242d3bSAndrew Thompson ("%s: if_lagg not loaded!", __func__)); 605a92c4bb6SHans Petter Selasky m = (*lagg_input_ethernet_p)(ifp, m); 606b47888ceSAndrew Thompson if (m != NULL) 607b47888ceSAndrew Thompson ifp = m->m_pkthdr.rcvif; 6082db13e75SMarko Zec else { 6092db13e75SMarko Zec CURVNET_RESTORE(); 610b47888ceSAndrew Thompson return; 611b47888ceSAndrew Thompson } 6122db13e75SMarko Zec } 613b47888ceSAndrew Thompson 61469462a82SBruce M Simpson /* 61569462a82SBruce M Simpson * If the hardware did not process an 802.1Q tag, do this now, 61669462a82SBruce M Simpson * to allow 802.1P priority frames to be passed to the main input 61769462a82SBruce M Simpson * path correctly. 61869462a82SBruce M Simpson */ 619c7cffd65SAlexander V. Chernikov if ((m->m_flags & M_VLANTAG) == 0 && 620c7cffd65SAlexander V. Chernikov ((etype == ETHERTYPE_VLAN) || (etype == ETHERTYPE_QINQ))) { 62169462a82SBruce M Simpson struct ether_vlan_header *evl; 62269462a82SBruce M Simpson 62369462a82SBruce M Simpson if (m->m_len < sizeof(*evl) && 62469462a82SBruce M Simpson (m = m_pullup(m, sizeof(*evl))) == NULL) { 625402d5e27SBruce M Simpson #ifdef DIAGNOSTIC 62669462a82SBruce M Simpson if_printf(ifp, "cannot pullup VLAN header\n"); 627402d5e27SBruce M Simpson #endif 6283751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 6292db13e75SMarko Zec CURVNET_RESTORE(); 63069462a82SBruce M Simpson return; 63169462a82SBruce M Simpson } 63269462a82SBruce M Simpson 63369462a82SBruce M Simpson evl = mtod(m, struct ether_vlan_header *); 63469462a82SBruce M Simpson m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 63569462a82SBruce M Simpson m->m_flags |= M_VLANTAG; 63669462a82SBruce M Simpson 63769462a82SBruce M Simpson bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 63869462a82SBruce M Simpson ETHER_HDR_LEN - ETHER_TYPE_LEN); 63969462a82SBruce M Simpson m_adj(m, ETHER_VLAN_ENCAP_LEN); 64097cce87fSGleb Smirnoff eh = mtod(m, struct ether_header *); 64169462a82SBruce M Simpson } 64269462a82SBruce M Simpson 643a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 644a34c6aebSBjoern A. Zeeb 64569462a82SBruce M Simpson /* Allow ng_ether(4) to claim this frame. */ 646833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 647514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_p != NULL, 64869462a82SBruce M Simpson ("%s: ng_ether_input_p is NULL", __func__)); 64969462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 650c1d93b05SSam Leffler (*ng_ether_input_p)(ifp, &m); 65121ca7b57SMarko Zec if (m == NULL) { 65221ca7b57SMarko Zec CURVNET_RESTORE(); 653e1e1452dSArchie Cobbs return; 654e1e1452dSArchie Cobbs } 6555e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 65621ca7b57SMarko Zec } 657e1e1452dSArchie Cobbs 6588f867517SAndrew Thompson /* 65969462a82SBruce M Simpson * Allow if_bridge(4) to claim this frame. 660d862b165SMark Johnston * 66169462a82SBruce M Simpson * The BRIDGE_INPUT() macro will update ifp if the bridge changed it 66269462a82SBruce M Simpson * and the frame should be delivered locally. 663d862b165SMark Johnston * 664d862b165SMark Johnston * If M_BRIDGE_INJECT is set, the packet was received directly by the 665d862b165SMark Johnston * bridge via netmap, so "ifp" is the bridge itself and the packet 666d862b165SMark Johnston * should be re-examined. 6678f867517SAndrew Thompson */ 668d862b165SMark Johnston if (ifp->if_bridge != NULL || (m->m_flags & M_BRIDGE_INJECT) != 0) { 66969462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 670fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m); 67121ca7b57SMarko Zec if (m == NULL) { 67221ca7b57SMarko Zec CURVNET_RESTORE(); 6738f867517SAndrew Thompson return; 6748f867517SAndrew Thompson } 6755e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 67621ca7b57SMarko Zec } 6778f867517SAndrew Thompson 678259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 67969462a82SBruce M Simpson /* 68069462a82SBruce M Simpson * Clear M_PROMISC on frame so that carp(4) will see it when the 68169462a82SBruce M Simpson * mbuf flows up to Layer 3. 68269462a82SBruce M Simpson * FreeBSD's implementation of carp(4) uses the inprotosw 68369462a82SBruce M Simpson * to dispatch IPPROTO_CARP. carp(4) also allocates its own 68469462a82SBruce M Simpson * Ethernet addresses of the form 00:00:5e:00:01:xx, which 68569462a82SBruce M Simpson * is outside the scope of the M_PROMISC test below. 68669462a82SBruce M Simpson * TODO: Maintain a hash table of ethernet addresses other than 68769462a82SBruce M Simpson * ether_dhost which may be active on this ifp. 68869462a82SBruce M Simpson */ 68954bfbd51SWill Andrews if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) { 69069462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 69169462a82SBruce M Simpson } else 69269462a82SBruce M Simpson #endif 69369462a82SBruce M Simpson { 69469462a82SBruce M Simpson /* 695e7f8c833SBruce M Simpson * If the frame received was not for our MAC address, set the 69669462a82SBruce M Simpson * M_PROMISC flag on the mbuf chain. The frame may need to 69769462a82SBruce M Simpson * be seen by the rest of the Ethernet input path in case of 69869462a82SBruce M Simpson * re-entry (e.g. bridge, vlan, netgraph) but should not be 69969462a82SBruce M Simpson * seen by upper protocol layers. 70069462a82SBruce M Simpson */ 70169462a82SBruce M Simpson if (!ETHER_IS_MULTICAST(eh->ether_dhost) && 702e7f8c833SBruce M Simpson bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0) 70369462a82SBruce M Simpson m->m_flags |= M_PROMISC; 70469462a82SBruce M Simpson } 70569462a82SBruce M Simpson 706acf032f5SRobert Watson ether_demux(ifp, m); 70721ca7b57SMarko Zec CURVNET_RESTORE(); 708e1e1452dSArchie Cobbs } 709e1e1452dSArchie Cobbs 710e1e1452dSArchie Cobbs /* 7116cb52192SRobert Watson * Ethernet input dispatch; by default, direct dispatch here regardless of 7127527624eSRobert Watson * global configuration. However, if RSS is enabled, hook up RSS affinity 7137527624eSRobert Watson * so that when deferred or hybrid dispatch is enabled, we can redistribute 7147527624eSRobert Watson * load based on RSS. 7157527624eSRobert Watson * 7167527624eSRobert Watson * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or 7177527624eSRobert Watson * not it had already done work distribution via multi-queue. Then we could 7187527624eSRobert Watson * direct dispatch in the event load balancing was already complete and 7197527624eSRobert Watson * handle the case of interfaces with different capabilities better. 7207527624eSRobert Watson * 7217527624eSRobert Watson * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions 7227527624eSRobert Watson * at multiple layers? 7237527624eSRobert Watson * 7247527624eSRobert Watson * XXXRW: For now, enable all this only if RSS is compiled in, although it 7257527624eSRobert Watson * works fine without RSS. Need to characterise the performance overhead 7267527624eSRobert Watson * of the detour through the netisr code in the event the result is always 7277527624eSRobert Watson * direct dispatch. 7286cb52192SRobert Watson */ 7296cb52192SRobert Watson static void 7306cb52192SRobert Watson ether_nh_input(struct mbuf *m) 7316cb52192SRobert Watson { 7326cb52192SRobert Watson 733b71bed24SAndrey V. Elsukov M_ASSERTPKTHDR(m); 734b71bed24SAndrey V. Elsukov KASSERT(m->m_pkthdr.rcvif != NULL, 735b71bed24SAndrey V. Elsukov ("%s: NULL interface pointer", __func__)); 7366cb52192SRobert Watson ether_input_internal(m->m_pkthdr.rcvif, m); 7376cb52192SRobert Watson } 7386cb52192SRobert Watson 7396cb52192SRobert Watson static struct netisr_handler ether_nh = { 7406cb52192SRobert Watson .nh_name = "ether", 7416cb52192SRobert Watson .nh_handler = ether_nh_input, 7426cb52192SRobert Watson .nh_proto = NETISR_ETHER, 7437527624eSRobert Watson #ifdef RSS 7447527624eSRobert Watson .nh_policy = NETISR_POLICY_CPU, 7457527624eSRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7467527624eSRobert Watson .nh_m2cpuid = rss_m2cpuid, 7477527624eSRobert Watson #else 7486cb52192SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 7496cb52192SRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7507527624eSRobert Watson #endif 7516cb52192SRobert Watson }; 7526cb52192SRobert Watson 7536cb52192SRobert Watson static void 7546cb52192SRobert Watson ether_init(__unused void *arg) 7556cb52192SRobert Watson { 7566cb52192SRobert Watson 7576cb52192SRobert Watson netisr_register(ðer_nh); 7586cb52192SRobert Watson } 7596cb52192SRobert Watson SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL); 7606cb52192SRobert Watson 7616cb52192SRobert Watson static void 7627d4317bdSAlexander V. Chernikov vnet_ether_init(__unused void *arg) 7637d4317bdSAlexander V. Chernikov { 764b252313fSGleb Smirnoff struct pfil_head_args args; 7657d4317bdSAlexander V. Chernikov 766b252313fSGleb Smirnoff args.pa_version = PFIL_VERSION; 767b252313fSGleb Smirnoff args.pa_flags = PFIL_IN | PFIL_OUT; 768b252313fSGleb Smirnoff args.pa_type = PFIL_TYPE_ETHERNET; 769b252313fSGleb Smirnoff args.pa_headname = PFIL_ETHER_NAME; 770b252313fSGleb Smirnoff V_link_pfil_head = pfil_head_register(&args); 771b252313fSGleb Smirnoff 772484149deSBjoern A. Zeeb #ifdef VIMAGE 773484149deSBjoern A. Zeeb netisr_register_vnet(ðer_nh); 774484149deSBjoern A. Zeeb #endif 7757d4317bdSAlexander V. Chernikov } 7767d4317bdSAlexander V. Chernikov VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY, 7777d4317bdSAlexander V. Chernikov vnet_ether_init, NULL); 7787d4317bdSAlexander V. Chernikov 779484149deSBjoern A. Zeeb #ifdef VIMAGE 7807d4317bdSAlexander V. Chernikov static void 781484149deSBjoern A. Zeeb vnet_ether_pfil_destroy(__unused void *arg) 7827d4317bdSAlexander V. Chernikov { 7837d4317bdSAlexander V. Chernikov 784b252313fSGleb Smirnoff pfil_head_unregister(V_link_pfil_head); 7857d4317bdSAlexander V. Chernikov } 786484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY, 787484149deSBjoern A. Zeeb vnet_ether_pfil_destroy, NULL); 788484149deSBjoern A. Zeeb 789484149deSBjoern A. Zeeb static void 790484149deSBjoern A. Zeeb vnet_ether_destroy(__unused void *arg) 791484149deSBjoern A. Zeeb { 792484149deSBjoern A. Zeeb 793484149deSBjoern A. Zeeb netisr_unregister_vnet(ðer_nh); 794484149deSBjoern A. Zeeb } 7957d4317bdSAlexander V. Chernikov VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, 7967d4317bdSAlexander V. Chernikov vnet_ether_destroy, NULL); 797484149deSBjoern A. Zeeb #endif 7987d4317bdSAlexander V. Chernikov 7997d4317bdSAlexander V. Chernikov static void 8006cb52192SRobert Watson ether_input(struct ifnet *ifp, struct mbuf *m) 8016cb52192SRobert Watson { 802b8a6e03fSGleb Smirnoff struct epoch_tracker et; 8034857f5fbSGeorge V. Neville-Neil struct mbuf *mn; 804e87c4940SGleb Smirnoff bool needs_epoch; 805e87c4940SGleb Smirnoff 806a6b55ee6SGleb Smirnoff needs_epoch = (ifp->if_flags & IFF_NEEDSEPOCH); 807a6b55ee6SGleb Smirnoff #ifdef INVARIANTS 808a6b55ee6SGleb Smirnoff /* 809a6b55ee6SGleb Smirnoff * This temporary code is here to prevent epoch unaware and unmarked 810a6b55ee6SGleb Smirnoff * drivers to panic the system. Once all drivers are taken care of, 811a6b55ee6SGleb Smirnoff * the whole INVARIANTS block should go away. 812a6b55ee6SGleb Smirnoff */ 813a6b55ee6SGleb Smirnoff if (!needs_epoch && !in_epoch(net_epoch_preempt)) { 814a6b55ee6SGleb Smirnoff static bool printedonce; 815a6b55ee6SGleb Smirnoff 816a6b55ee6SGleb Smirnoff needs_epoch = true; 817a6b55ee6SGleb Smirnoff if (!printedonce) { 818a6b55ee6SGleb Smirnoff printedonce = true; 819a6b55ee6SGleb Smirnoff if_printf(ifp, "called %s w/o net epoch! " 820a6b55ee6SGleb Smirnoff "PLEASE file a bug report.", __func__); 821a6b55ee6SGleb Smirnoff #ifdef KDB 822a6b55ee6SGleb Smirnoff kdb_backtrace(); 823a6b55ee6SGleb Smirnoff #endif 824a6b55ee6SGleb Smirnoff } 825a6b55ee6SGleb Smirnoff } 826a6b55ee6SGleb Smirnoff #endif 8274857f5fbSGeorge V. Neville-Neil 8284857f5fbSGeorge V. Neville-Neil /* 8294857f5fbSGeorge V. Neville-Neil * The drivers are allowed to pass in a chain of packets linked with 8304857f5fbSGeorge V. Neville-Neil * m_nextpkt. We split them up into separate packets here and pass 8314857f5fbSGeorge V. Neville-Neil * them up. This allows the drivers to amortize the receive lock. 8324857f5fbSGeorge V. Neville-Neil */ 833b8a6e03fSGleb Smirnoff CURVNET_SET_QUIET(ifp->if_vnet); 834e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 835b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et); 8364857f5fbSGeorge V. Neville-Neil while (m) { 8374857f5fbSGeorge V. Neville-Neil mn = m->m_nextpkt; 8384857f5fbSGeorge V. Neville-Neil m->m_nextpkt = NULL; 8394857f5fbSGeorge V. Neville-Neil 8406cb52192SRobert Watson /* 841b8a6e03fSGleb Smirnoff * We will rely on rcvif being set properly in the deferred 842b8a6e03fSGleb Smirnoff * context, so assert it is correct here. 8436cb52192SRobert Watson */ 844fb3bc596SJohn Baldwin MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 845e84ef07fSBjoern A. Zeeb KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p " 846e84ef07fSBjoern A. Zeeb "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp)); 8476cb52192SRobert Watson netisr_dispatch(NETISR_ETHER, m); 8484857f5fbSGeorge V. Neville-Neil m = mn; 8494857f5fbSGeorge V. Neville-Neil } 850e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 851b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et); 852b8a6e03fSGleb Smirnoff CURVNET_RESTORE(); 8536cb52192SRobert Watson } 8546cb52192SRobert Watson 8556cb52192SRobert Watson /* 856e1e1452dSArchie Cobbs * Upper layer processing for a received Ethernet packet. 857e1e1452dSArchie Cobbs */ 858e1e1452dSArchie Cobbs void 859c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m) 860e1e1452dSArchie Cobbs { 861c1d93b05SSam Leffler struct ether_header *eh; 8627d4317bdSAlexander V. Chernikov int i, isr; 863e1e1452dSArchie Cobbs u_short ether_type; 86469462a82SBruce M Simpson 865b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 86669462a82SBruce M Simpson KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__)); 86769462a82SBruce M Simpson 8687d4317bdSAlexander V. Chernikov /* Do not grab PROMISC frames in case we are re-entered. */ 869b252313fSGleb Smirnoff if (PFIL_HOOKED_IN(V_link_pfil_head) && !(m->m_flags & M_PROMISC)) { 870a2256150SGleb Smirnoff i = pfil_mbuf_in(V_link_pfil_head, &m, ifp, NULL); 871ffeab76bSKristof Provost if (i != PFIL_PASS) 8727d4317bdSAlexander V. Chernikov return; 87369462a82SBruce M Simpson } 8747d4317bdSAlexander V. Chernikov 875c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 876cd0cd014SJoerg Wunsch ether_type = ntohs(eh->ether_type); 877c1d93b05SSam Leffler 878a9771948SGleb Smirnoff /* 87969462a82SBruce M Simpson * If this frame has a VLAN tag other than 0, call vlan_input() 88069462a82SBruce M Simpson * if its module is loaded. Otherwise, drop. 881a9771948SGleb Smirnoff */ 88269462a82SBruce M Simpson if ((m->m_flags & M_VLANTAG) && 88369462a82SBruce M Simpson EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 88475ee267cSGleb Smirnoff if (ifp->if_vlantrunk == NULL) { 8853751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 8866c23e6ccSRuslan Ermilov m_freem(m); 8876c23e6ccSRuslan Ermilov return; 8886c23e6ccSRuslan Ermilov } 88969462a82SBruce M Simpson KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 89069462a82SBruce M Simpson __func__)); 89169462a82SBruce M Simpson /* Clear before possibly re-entering ether_input(). */ 89269462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 893c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 894c1d93b05SSam Leffler return; 895c1d93b05SSam Leffler } 896c1d93b05SSam Leffler 897c1d93b05SSam Leffler /* 89869462a82SBruce M Simpson * Pass promiscuously received frames to the upper layer if the user 89969462a82SBruce M Simpson * requested this by setting IFF_PPROMISC. Otherwise, drop them. 900c1d93b05SSam Leffler */ 90169462a82SBruce M Simpson if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 902c1d93b05SSam Leffler m_freem(m); 903c1d93b05SSam Leffler return; 904c1d93b05SSam Leffler } 905c1d93b05SSam Leffler 90669462a82SBruce M Simpson /* 90769462a82SBruce M Simpson * Reset layer specific mbuf flags to avoid confusing upper layers. 90869462a82SBruce M Simpson */ 90969462a82SBruce M Simpson m->m_flags &= ~M_VLANTAG; 91086bd0491SAndre Oppermann m_clrprotoflags(m); 911c1d93b05SSam Leffler 91269462a82SBruce M Simpson /* 91369462a82SBruce M Simpson * Dispatch frame to upper layer. 91469462a82SBruce M Simpson */ 915307d80beSDavid Greenman switch (ether_type) { 916df8bae1dSRodney W. Grimes #ifdef INET 917df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 9181cafed39SJonathan Lemon isr = NETISR_IP; 919df8bae1dSRodney W. Grimes break; 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes case ETHERTYPE_ARP: 92208aadfbbSJonathan Lemon if (ifp->if_flags & IFF_NOARP) { 92308aadfbbSJonathan Lemon /* Discard packet if ARP is disabled on interface */ 92408aadfbbSJonathan Lemon m_freem(m); 92508aadfbbSJonathan Lemon return; 92608aadfbbSJonathan Lemon } 9271cafed39SJonathan Lemon isr = NETISR_ARP; 928df8bae1dSRodney W. Grimes break; 929df8bae1dSRodney W. Grimes #endif 93082cd038dSYoshinobu Inoue #ifdef INET6 93182cd038dSYoshinobu Inoue case ETHERTYPE_IPV6: 9321cafed39SJonathan Lemon isr = NETISR_IPV6; 93382cd038dSYoshinobu Inoue break; 93482cd038dSYoshinobu Inoue #endif 935df8bae1dSRodney W. Grimes default: 9361cafed39SJonathan Lemon goto discard; 937df8bae1dSRodney W. Grimes } 93879b67994SJohn Baldwin 93979b67994SJohn Baldwin /* Strip off Ethernet header. */ 94079b67994SJohn Baldwin m_adj(m, ETHER_HDR_LEN); 94179b67994SJohn Baldwin 9421cafed39SJonathan Lemon netisr_dispatch(isr, m); 943c1d93b05SSam Leffler return; 9441cafed39SJonathan Lemon 945c1d93b05SSam Leffler discard: 946c1d93b05SSam Leffler /* 947c1d93b05SSam Leffler * Packet is to be discarded. If netgraph is present, 948c1d93b05SSam Leffler * hand the packet to it for last chance processing; 949c1d93b05SSam Leffler * otherwise dispose of it. 950c1d93b05SSam Leffler */ 951833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 952514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_orphan_p != NULL, 953514bcb89SPoul-Henning Kamp ("ng_ether_input_orphan_p is NULL")); 954c1d93b05SSam Leffler (*ng_ether_input_orphan_p)(ifp, m); 955c1d93b05SSam Leffler return; 956c1d93b05SSam Leffler } 957c1d93b05SSam Leffler m_freem(m); 958c1d93b05SSam Leffler } 959c1d93b05SSam Leffler 960c1d93b05SSam Leffler /* 961c1d93b05SSam Leffler * Convert Ethernet address to printable (loggable) representation. 962c1d93b05SSam Leffler * This routine is for compatibility; it's better to just use 963c1d93b05SSam Leffler * 964c1d93b05SSam Leffler * printf("%6D", <pointer to address>, ":"); 965c1d93b05SSam Leffler * 966c1d93b05SSam Leffler * since there's no static buffer involved. 967c1d93b05SSam Leffler */ 968c1d93b05SSam Leffler char * 969c1d93b05SSam Leffler ether_sprintf(const u_char *ap) 970c1d93b05SSam Leffler { 971c1d93b05SSam Leffler static char etherbuf[18]; 972c1d93b05SSam Leffler snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 973c1d93b05SSam Leffler return (etherbuf); 974df8bae1dSRodney W. Grimes } 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes /* 977df8bae1dSRodney W. Grimes * Perform common duties while attaching to interface list 978df8bae1dSRodney W. Grimes */ 979df8bae1dSRodney W. Grimes void 980d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 981df8bae1dSRodney W. Grimes { 982f93dfa28SBrooks Davis int i; 983098a8c3bSMatthew N. Dodd struct ifaddr *ifa; 984098a8c3bSMatthew N. Dodd struct sockaddr_dl *sdl; 985df8bae1dSRodney W. Grimes 986c1d93b05SSam Leffler ifp->if_addrlen = ETHER_ADDR_LEN; 987c1d93b05SSam Leffler ifp->if_hdrlen = ETHER_HDR_LEN; 988df8bae1dSRodney W. Grimes ifp->if_mtu = ETHERMTU; 989fa2ab81eSKyle Evans if_attach(ifp); 990c1d93b05SSam Leffler ifp->if_output = ether_output; 991c1d93b05SSam Leffler ifp->if_input = ether_input; 9921158dfb7SGarrett Wollman ifp->if_resolvemulti = ether_resolvemulti; 9934fb3a820SAlexander V. Chernikov ifp->if_requestencap = ether_requestencap; 994d3c351c5SMarko Zec #ifdef VIMAGE 995d3c351c5SMarko Zec ifp->if_reassign = ether_reassign; 996d3c351c5SMarko Zec #endif 997a330e1f1SGary Palmer if (ifp->if_baudrate == 0) 998c1d93b05SSam Leffler ifp->if_baudrate = IF_Mbps(10); /* just a default */ 999322dcb8dSMax Khon ifp->if_broadcastaddr = etherbroadcastaddr; 1000c1d93b05SSam Leffler 10014a0d6638SRuslan Ermilov ifa = ifp->if_addr; 10026e551fb6SDavid E. O'Brien KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 100359562606SGarrett Wollman sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1004df8bae1dSRodney W. Grimes sdl->sdl_type = IFT_ETHER; 1005df8bae1dSRodney W. Grimes sdl->sdl_alen = ifp->if_addrlen; 1006d09ed26fSRuslan Ermilov bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 1007c1d93b05SSam Leffler 10085b1a5e45SRavi Pokala if (ifp->if_hw_addr != NULL) 1009ddae5750SRavi Pokala bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen); 1010ddae5750SRavi Pokala 1011797f247bSMatthew N. Dodd bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 1012e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) 1013e1e1452dSArchie Cobbs (*ng_ether_attach_p)(ifp); 1014e3bbbec2SMatthew N. Dodd 1015f93dfa28SBrooks Davis /* Announce Ethernet MAC address if non-zero. */ 1016f93dfa28SBrooks Davis for (i = 0; i < ifp->if_addrlen; i++) 1017d09ed26fSRuslan Ermilov if (lla[i] != 0) 1018f93dfa28SBrooks Davis break; 1019f93dfa28SBrooks Davis if (i != ifp->if_addrlen) 1020d09ed26fSRuslan Ermilov if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 1021ef1f9169SMarcel Moolenaar 1022ef1f9169SMarcel Moolenaar uuid_ether_add(LLADDR(sdl)); 10238819ad85SSepherosa Ziehau 10248819ad85SSepherosa Ziehau /* Add necessary bits are setup; announce it now. */ 10258819ad85SSepherosa Ziehau EVENTHANDLER_INVOKE(ether_ifattach_event, ifp); 10268819ad85SSepherosa Ziehau if (IS_DEFAULT_VNET(curvnet)) 10278819ad85SSepherosa Ziehau devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL); 1028e1e1452dSArchie Cobbs } 1029e1e1452dSArchie Cobbs 103021b8ebd9SArchie Cobbs /* 103121b8ebd9SArchie Cobbs * Perform common duties while detaching an Ethernet interface 103221b8ebd9SArchie Cobbs */ 103321b8ebd9SArchie Cobbs void 1034c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp) 103521b8ebd9SArchie Cobbs { 1036ef1f9169SMarcel Moolenaar struct sockaddr_dl *sdl; 1037ef1f9169SMarcel Moolenaar 1038ef1f9169SMarcel Moolenaar sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr); 1039ef1f9169SMarcel Moolenaar uuid_ether_del(LLADDR(sdl)); 1040ef1f9169SMarcel Moolenaar 1041833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1042514bcb89SPoul-Henning Kamp KASSERT(ng_ether_detach_p != NULL, 1043514bcb89SPoul-Henning Kamp ("ng_ether_detach_p is NULL")); 104421b8ebd9SArchie Cobbs (*ng_ether_detach_p)(ifp); 1045514bcb89SPoul-Henning Kamp } 104620a65f37SAndrew Thompson 104721b8ebd9SArchie Cobbs bpfdetach(ifp); 104821b8ebd9SArchie Cobbs if_detach(ifp); 104921b8ebd9SArchie Cobbs } 105021b8ebd9SArchie Cobbs 1051d3c351c5SMarko Zec #ifdef VIMAGE 1052d3c351c5SMarko Zec void 1053d3c351c5SMarko Zec ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused) 1054d3c351c5SMarko Zec { 1055d3c351c5SMarko Zec 1056833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1057d3c351c5SMarko Zec KASSERT(ng_ether_detach_p != NULL, 1058d3c351c5SMarko Zec ("ng_ether_detach_p is NULL")); 1059d3c351c5SMarko Zec (*ng_ether_detach_p)(ifp); 1060d3c351c5SMarko Zec } 1061d3c351c5SMarko Zec 1062d3c351c5SMarko Zec if (ng_ether_attach_p != NULL) { 1063d3c351c5SMarko Zec CURVNET_SET_QUIET(new_vnet); 1064d3c351c5SMarko Zec (*ng_ether_attach_p)(ifp); 1065d3c351c5SMarko Zec CURVNET_RESTORE(); 1066d3c351c5SMarko Zec } 1067d3c351c5SMarko Zec } 1068d3c351c5SMarko Zec #endif 1069d3c351c5SMarko Zec 1070ce02431fSDoug Rabson SYSCTL_DECL(_net_link); 10717029da5cSPawel Biernacki SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 10727029da5cSPawel Biernacki "Ethernet"); 107330106f6aSPoul-Henning Kamp 107416b4a343SChristian Weisgerber #if 0 107516b4a343SChristian Weisgerber /* 107616b4a343SChristian Weisgerber * This is for reference. We have a table-driven version 107716b4a343SChristian Weisgerber * of the little-endian crc32 generator, which is faster 107816b4a343SChristian Weisgerber * than the double-loop. 107916b4a343SChristian Weisgerber */ 108016b4a343SChristian Weisgerber uint32_t 108116b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 108216b4a343SChristian Weisgerber { 108316b4a343SChristian Weisgerber size_t i; 108416b4a343SChristian Weisgerber uint32_t crc; 108516b4a343SChristian Weisgerber int bit; 108616b4a343SChristian Weisgerber uint8_t data; 108716b4a343SChristian Weisgerber 108816b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 108916b4a343SChristian Weisgerber 109016b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 1091933dad75SAntoine Brodin for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 109216b4a343SChristian Weisgerber carry = (crc ^ data) & 1; 109316b4a343SChristian Weisgerber crc >>= 1; 109416b4a343SChristian Weisgerber if (carry) 109516b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_LE); 109616b4a343SChristian Weisgerber } 1097933dad75SAntoine Brodin } 109816b4a343SChristian Weisgerber 109916b4a343SChristian Weisgerber return (crc); 110016b4a343SChristian Weisgerber } 110116b4a343SChristian Weisgerber #else 110216b4a343SChristian Weisgerber uint32_t 110316b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 110416b4a343SChristian Weisgerber { 110516b4a343SChristian Weisgerber static const uint32_t crctab[] = { 110616b4a343SChristian Weisgerber 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 110716b4a343SChristian Weisgerber 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 110816b4a343SChristian Weisgerber 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 110916b4a343SChristian Weisgerber 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 111016b4a343SChristian Weisgerber }; 111116b4a343SChristian Weisgerber size_t i; 111216b4a343SChristian Weisgerber uint32_t crc; 111316b4a343SChristian Weisgerber 111416b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 111516b4a343SChristian Weisgerber 111616b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 111716b4a343SChristian Weisgerber crc ^= buf[i]; 111816b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 111916b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 112016b4a343SChristian Weisgerber } 112116b4a343SChristian Weisgerber 112216b4a343SChristian Weisgerber return (crc); 112316b4a343SChristian Weisgerber } 112416b4a343SChristian Weisgerber #endif 112516b4a343SChristian Weisgerber 112616b4a343SChristian Weisgerber uint32_t 112716b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len) 112816b4a343SChristian Weisgerber { 112916b4a343SChristian Weisgerber size_t i; 113016b4a343SChristian Weisgerber uint32_t crc, carry; 113116b4a343SChristian Weisgerber int bit; 113216b4a343SChristian Weisgerber uint8_t data; 113316b4a343SChristian Weisgerber 113416b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 113516b4a343SChristian Weisgerber 113616b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 113716b4a343SChristian Weisgerber for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 113816b4a343SChristian Weisgerber carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 113916b4a343SChristian Weisgerber crc <<= 1; 114016b4a343SChristian Weisgerber if (carry) 114116b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 114216b4a343SChristian Weisgerber } 114316b4a343SChristian Weisgerber } 114416b4a343SChristian Weisgerber 114516b4a343SChristian Weisgerber return (crc); 114616b4a343SChristian Weisgerber } 114716b4a343SChristian Weisgerber 1148fb583156SDavid Greenman int 1149995c7fd1SYaroslav Tykhiy ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 115030106f6aSPoul-Henning Kamp { 115130106f6aSPoul-Henning Kamp struct ifaddr *ifa = (struct ifaddr *) data; 115230106f6aSPoul-Henning Kamp struct ifreq *ifr = (struct ifreq *) data; 1153fb583156SDavid Greenman int error = 0; 115430106f6aSPoul-Henning Kamp 115530106f6aSPoul-Henning Kamp switch (command) { 115630106f6aSPoul-Henning Kamp case SIOCSIFADDR: 115730106f6aSPoul-Henning Kamp ifp->if_flags |= IFF_UP; 115830106f6aSPoul-Henning Kamp 115930106f6aSPoul-Henning Kamp switch (ifa->ifa_addr->sa_family) { 116030106f6aSPoul-Henning Kamp #ifdef INET 116130106f6aSPoul-Henning Kamp case AF_INET: 116230106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1163322dcb8dSMax Khon arp_ifinit(ifp, ifa); 116430106f6aSPoul-Henning Kamp break; 116530106f6aSPoul-Henning Kamp #endif 116630106f6aSPoul-Henning Kamp default: 116730106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 116830106f6aSPoul-Henning Kamp break; 116930106f6aSPoul-Henning Kamp } 117030106f6aSPoul-Henning Kamp break; 117130106f6aSPoul-Henning Kamp 117230106f6aSPoul-Henning Kamp case SIOCGIFADDR: 117338d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 117438d958a6SBrooks Davis ETHER_ADDR_LEN); 117530106f6aSPoul-Henning Kamp break; 1176fb583156SDavid Greenman 1177fb583156SDavid Greenman case SIOCSIFMTU: 1178fb583156SDavid Greenman /* 1179fb583156SDavid Greenman * Set the interface MTU. 1180fb583156SDavid Greenman */ 1181fb583156SDavid Greenman if (ifr->ifr_mtu > ETHERMTU) { 1182fb583156SDavid Greenman error = EINVAL; 1183fb583156SDavid Greenman } else { 1184fb583156SDavid Greenman ifp->if_mtu = ifr->ifr_mtu; 118530106f6aSPoul-Henning Kamp } 1186fb583156SDavid Greenman break; 1187f1379734SKonstantin Belousov 1188f1379734SKonstantin Belousov case SIOCSLANPCP: 1189f1379734SKonstantin Belousov error = priv_check(curthread, PRIV_NET_SETLANPCP); 1190f1379734SKonstantin Belousov if (error != 0) 1191f1379734SKonstantin Belousov break; 1192f1379734SKonstantin Belousov if (ifr->ifr_lan_pcp > 7 && 11934a381a9eSHans Petter Selasky ifr->ifr_lan_pcp != IFNET_PCP_NONE) { 1194f1379734SKonstantin Belousov error = EINVAL; 11954a381a9eSHans Petter Selasky } else { 1196f1379734SKonstantin Belousov ifp->if_pcp = ifr->ifr_lan_pcp; 11974a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 11984a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 11994a381a9eSHans Petter Selasky } 1200f1379734SKonstantin Belousov break; 1201f1379734SKonstantin Belousov 1202f1379734SKonstantin Belousov case SIOCGLANPCP: 1203f1379734SKonstantin Belousov ifr->ifr_lan_pcp = ifp->if_pcp; 1204f1379734SKonstantin Belousov break; 1205f1379734SKonstantin Belousov 1206c1d93b05SSam Leffler default: 1207c1d93b05SSam Leffler error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1208c1d93b05SSam Leffler break; 1209fb583156SDavid Greenman } 1210fb583156SDavid Greenman return (error); 121130106f6aSPoul-Henning Kamp } 12121158dfb7SGarrett Wollman 121337c84183SPoul-Henning Kamp static int 121472fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 121572fd1b6aSDag-Erling Smørgrav struct sockaddr *sa) 12161158dfb7SGarrett Wollman { 12171158dfb7SGarrett Wollman struct sockaddr_dl *sdl; 1218d7647d96SDag-Erling Smørgrav #ifdef INET 12191158dfb7SGarrett Wollman struct sockaddr_in *sin; 1220d7647d96SDag-Erling Smørgrav #endif 122182cd038dSYoshinobu Inoue #ifdef INET6 122282cd038dSYoshinobu Inoue struct sockaddr_in6 *sin6; 122382cd038dSYoshinobu Inoue #endif 12241158dfb7SGarrett Wollman u_char *e_addr; 12251158dfb7SGarrett Wollman 12261158dfb7SGarrett Wollman switch(sa->sa_family) { 12271158dfb7SGarrett Wollman case AF_LINK: 12287f33a738SJulian Elischer /* 12297f33a738SJulian Elischer * No mapping needed. Just check that it's a valid MC address. 12307f33a738SJulian Elischer */ 12311158dfb7SGarrett Wollman sdl = (struct sockaddr_dl *)sa; 12321158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 1233086e98c4SBruce M Simpson if (!ETHER_IS_MULTICAST(e_addr)) 12341158dfb7SGarrett Wollman return EADDRNOTAVAIL; 1235155d72c4SPedro F. Giffuni *llsa = NULL; 12361158dfb7SGarrett Wollman return 0; 12371158dfb7SGarrett Wollman 12381158dfb7SGarrett Wollman #ifdef INET 12391158dfb7SGarrett Wollman case AF_INET: 12401158dfb7SGarrett Wollman sin = (struct sockaddr_in *)sa; 12411158dfb7SGarrett Wollman if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 12421158dfb7SGarrett Wollman return EADDRNOTAVAIL; 124395fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 12441158dfb7SGarrett Wollman sdl->sdl_alen = ETHER_ADDR_LEN; 12451158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 12461158dfb7SGarrett Wollman ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 12471158dfb7SGarrett Wollman *llsa = (struct sockaddr *)sdl; 12481158dfb7SGarrett Wollman return 0; 12491158dfb7SGarrett Wollman #endif 125082cd038dSYoshinobu Inoue #ifdef INET6 125182cd038dSYoshinobu Inoue case AF_INET6: 125282cd038dSYoshinobu Inoue sin6 = (struct sockaddr_in6 *)sa; 1253595b8a1cSJun-ichiro itojun Hagino if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1254595b8a1cSJun-ichiro itojun Hagino /* 1255595b8a1cSJun-ichiro itojun Hagino * An IP6 address of 0 means listen to all 1256595b8a1cSJun-ichiro itojun Hagino * of the Ethernet multicast address used for IP6. 1257595b8a1cSJun-ichiro itojun Hagino * (This is used for multicast routers.) 1258595b8a1cSJun-ichiro itojun Hagino */ 1259595b8a1cSJun-ichiro itojun Hagino ifp->if_flags |= IFF_ALLMULTI; 1260155d72c4SPedro F. Giffuni *llsa = NULL; 1261595b8a1cSJun-ichiro itojun Hagino return 0; 1262595b8a1cSJun-ichiro itojun Hagino } 126382cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 126482cd038dSYoshinobu Inoue return EADDRNOTAVAIL; 126595fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 126682cd038dSYoshinobu Inoue sdl->sdl_alen = ETHER_ADDR_LEN; 126782cd038dSYoshinobu Inoue e_addr = LLADDR(sdl); 126882cd038dSYoshinobu Inoue ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 126982cd038dSYoshinobu Inoue *llsa = (struct sockaddr *)sdl; 127082cd038dSYoshinobu Inoue return 0; 127182cd038dSYoshinobu Inoue #endif 12721158dfb7SGarrett Wollman 12731158dfb7SGarrett Wollman default: 12741158dfb7SGarrett Wollman /* 12751158dfb7SGarrett Wollman * Well, the text isn't quite right, but it's the name 12761158dfb7SGarrett Wollman * that counts... 12771158dfb7SGarrett Wollman */ 12781158dfb7SGarrett Wollman return EAFNOSUPPORT; 12791158dfb7SGarrett Wollman } 12801158dfb7SGarrett Wollman } 128140811c14SMatthew N. Dodd 128240811c14SMatthew N. Dodd static moduledata_t ether_mod = { 1283833e8dc5SGleb Smirnoff .name = "ether", 128440811c14SMatthew N. Dodd }; 128540811c14SMatthew N. Dodd 128652f1277eSChristian S.J. Peron void 128752f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 128852f1277eSChristian S.J. Peron { 128952f1277eSChristian S.J. Peron struct ether_vlan_header vlan; 129052f1277eSChristian S.J. Peron struct mbuf mv, mb; 129152f1277eSChristian S.J. Peron 129252f1277eSChristian S.J. Peron KASSERT((m->m_flags & M_VLANTAG) != 0, 129352f1277eSChristian S.J. Peron ("%s: vlan information not present", __func__)); 129452f1277eSChristian S.J. Peron KASSERT(m->m_len >= sizeof(struct ether_header), 129552f1277eSChristian S.J. Peron ("%s: mbuf not large enough for header", __func__)); 129652f1277eSChristian S.J. Peron bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 129752f1277eSChristian S.J. Peron vlan.evl_proto = vlan.evl_encap_proto; 129852f1277eSChristian S.J. Peron vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 129952f1277eSChristian S.J. Peron vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 130052f1277eSChristian S.J. Peron m->m_len -= sizeof(struct ether_header); 130152f1277eSChristian S.J. Peron m->m_data += sizeof(struct ether_header); 130252f1277eSChristian S.J. Peron /* 130352f1277eSChristian S.J. Peron * If a data link has been supplied by the caller, then we will need to 130452f1277eSChristian S.J. Peron * re-create a stack allocated mbuf chain with the following structure: 130552f1277eSChristian S.J. Peron * 130652f1277eSChristian S.J. Peron * (1) mbuf #1 will contain the supplied data link 130752f1277eSChristian S.J. Peron * (2) mbuf #2 will contain the vlan header 130852f1277eSChristian S.J. Peron * (3) mbuf #3 will contain the original mbuf's packet data 130952f1277eSChristian S.J. Peron * 131052f1277eSChristian S.J. Peron * Otherwise, submit the packet and vlan header via bpf_mtap2(). 131152f1277eSChristian S.J. Peron */ 131252f1277eSChristian S.J. Peron if (data != NULL) { 131352f1277eSChristian S.J. Peron mv.m_next = m; 131452f1277eSChristian S.J. Peron mv.m_data = (caddr_t)&vlan; 131552f1277eSChristian S.J. Peron mv.m_len = sizeof(vlan); 131652f1277eSChristian S.J. Peron mb.m_next = &mv; 131752f1277eSChristian S.J. Peron mb.m_data = data; 131852f1277eSChristian S.J. Peron mb.m_len = dlen; 131952f1277eSChristian S.J. Peron bpf_mtap(bp, &mb); 132052f1277eSChristian S.J. Peron } else 132152f1277eSChristian S.J. Peron bpf_mtap2(bp, &vlan, sizeof(vlan), m); 132252f1277eSChristian S.J. Peron m->m_len += sizeof(struct ether_header); 132352f1277eSChristian S.J. Peron m->m_data -= sizeof(struct ether_header); 132452f1277eSChristian S.J. Peron } 132552f1277eSChristian S.J. Peron 132660e87ca8SAndrew Thompson struct mbuf * 1327c7cffd65SAlexander V. Chernikov ether_vlanencap_proto(struct mbuf *m, uint16_t tag, uint16_t proto) 132860e87ca8SAndrew Thompson { 132960e87ca8SAndrew Thompson struct ether_vlan_header *evl; 133060e87ca8SAndrew Thompson 1331eb1b1807SGleb Smirnoff M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 133260e87ca8SAndrew Thompson if (m == NULL) 133360e87ca8SAndrew Thompson return (NULL); 133460e87ca8SAndrew Thompson /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 133560e87ca8SAndrew Thompson 133660e87ca8SAndrew Thompson if (m->m_len < sizeof(*evl)) { 133760e87ca8SAndrew Thompson m = m_pullup(m, sizeof(*evl)); 133860e87ca8SAndrew Thompson if (m == NULL) 133960e87ca8SAndrew Thompson return (NULL); 134060e87ca8SAndrew Thompson } 134160e87ca8SAndrew Thompson 134260e87ca8SAndrew Thompson /* 134360e87ca8SAndrew Thompson * Transform the Ethernet header into an Ethernet header 134460e87ca8SAndrew Thompson * with 802.1Q encapsulation. 134560e87ca8SAndrew Thompson */ 134660e87ca8SAndrew Thompson evl = mtod(m, struct ether_vlan_header *); 134760e87ca8SAndrew Thompson bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 134860e87ca8SAndrew Thompson (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1349c7cffd65SAlexander V. Chernikov evl->evl_encap_proto = htons(proto); 135060e87ca8SAndrew Thompson evl->evl_tag = htons(tag); 135160e87ca8SAndrew Thompson return (m); 135260e87ca8SAndrew Thompson } 135360e87ca8SAndrew Thompson 1354950cc1f4SJustin Hibbits void 1355950cc1f4SJustin Hibbits ether_bpf_mtap_if(struct ifnet *ifp, struct mbuf *m) 1356950cc1f4SJustin Hibbits { 1357950cc1f4SJustin Hibbits if (bpf_peers_present(ifp->if_bpf)) { 1358950cc1f4SJustin Hibbits M_ASSERTVALID(m); 1359950cc1f4SJustin Hibbits if ((m->m_flags & M_VLANTAG) != 0) 1360950cc1f4SJustin Hibbits ether_vlan_mtap(ifp->if_bpf, m, NULL, 0); 1361950cc1f4SJustin Hibbits else 1362950cc1f4SJustin Hibbits bpf_mtap(ifp->if_bpf, m); 1363950cc1f4SJustin Hibbits } 1364950cc1f4SJustin Hibbits } 1365950cc1f4SJustin Hibbits 13667029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1367f1379734SKonstantin Belousov "IEEE 802.1Q VLAN"); 13687029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, 13697029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1370f1379734SKonstantin Belousov "for consistency"); 1371f1379734SKonstantin Belousov 13725f901c92SAndrew Turner VNET_DEFINE_STATIC(int, soft_pad); 1373f1379734SKonstantin Belousov #define V_soft_pad VNET(soft_pad) 1374f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 1375f1379734SKonstantin Belousov &VNET_NAME(soft_pad), 0, 1376f1379734SKonstantin Belousov "pad short frames before tagging"); 1377f1379734SKonstantin Belousov 1378f1379734SKonstantin Belousov /* 1379f1379734SKonstantin Belousov * For now, make preserving PCP via an mbuf tag optional, as it increases 1380f1379734SKonstantin Belousov * per-packet memory allocations and frees. In the future, it would be 1381f1379734SKonstantin Belousov * preferable to reuse ether_vtag for this, or similar. 1382f1379734SKonstantin Belousov */ 138378bc3d5eSKristof Provost VNET_DEFINE(int, vlan_mtag_pcp) = 0; 138478bc3d5eSKristof Provost #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 138578bc3d5eSKristof Provost SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW | CTLFLAG_VNET, 138678bc3d5eSKristof Provost &VNET_NAME(vlan_mtag_pcp), 0, 1387f1379734SKonstantin Belousov "Retain VLAN PCP information as packets are passed up the stack"); 1388f1379734SKonstantin Belousov 138949d6743dSZhenlei Huang static inline bool 139049d6743dSZhenlei Huang ether_do_pcp(struct ifnet *ifp, struct mbuf *m) 139149d6743dSZhenlei Huang { 139249d6743dSZhenlei Huang if (ifp->if_type == IFT_L2VLAN) 139349d6743dSZhenlei Huang return (false); 139449d6743dSZhenlei Huang if (ifp->if_pcp != IFNET_PCP_NONE || (m->m_flags & M_VLANTAG) != 0) 139549d6743dSZhenlei Huang return (true); 139649d6743dSZhenlei Huang if (V_vlan_mtag_pcp && 139749d6743dSZhenlei Huang m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL) != NULL) 139849d6743dSZhenlei Huang return (true); 139949d6743dSZhenlei Huang return (false); 140049d6743dSZhenlei Huang } 140149d6743dSZhenlei Huang 1402f1379734SKonstantin Belousov bool 1403f1379734SKonstantin Belousov ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, 1404838c8c47SZhenlei Huang const struct ether_8021q_tag *qtag) 1405f1379734SKonstantin Belousov { 1406f1379734SKonstantin Belousov struct m_tag *mtag; 1407f1379734SKonstantin Belousov int n; 1408f1379734SKonstantin Belousov uint16_t tag; 1409838c8c47SZhenlei Huang uint8_t pcp = qtag->pcp; 1410f1379734SKonstantin Belousov static const char pad[8]; /* just zeros */ 1411f1379734SKonstantin Belousov 1412f1379734SKonstantin Belousov /* 1413f1379734SKonstantin Belousov * Pad the frame to the minimum size allowed if told to. 1414f1379734SKonstantin Belousov * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1415f1379734SKonstantin Belousov * paragraph C.4.4.3.b. It can help to work around buggy 1416f1379734SKonstantin Belousov * bridges that violate paragraph C.4.4.3.a from the same 1417f1379734SKonstantin Belousov * document, i.e., fail to pad short frames after untagging. 1418f1379734SKonstantin Belousov * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1419f1379734SKonstantin Belousov * untagging it will produce a 62-byte frame, which is a runt 1420f1379734SKonstantin Belousov * and requires padding. There are VLAN-enabled network 1421f1379734SKonstantin Belousov * devices that just discard such runts instead or mishandle 1422f1379734SKonstantin Belousov * them somehow. 1423f1379734SKonstantin Belousov */ 1424f1379734SKonstantin Belousov if (V_soft_pad && p->if_type == IFT_ETHER) { 1425f1379734SKonstantin Belousov for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len; 1426f1379734SKonstantin Belousov n > 0; n -= sizeof(pad)) { 1427f1379734SKonstantin Belousov if (!m_append(*mp, min(n, sizeof(pad)), pad)) 1428f1379734SKonstantin Belousov break; 1429f1379734SKonstantin Belousov } 1430f1379734SKonstantin Belousov if (n > 0) { 1431f1379734SKonstantin Belousov m_freem(*mp); 1432f1379734SKonstantin Belousov *mp = NULL; 1433f1379734SKonstantin Belousov if_printf(ife, "cannot pad short frame"); 1434f1379734SKonstantin Belousov return (false); 1435f1379734SKonstantin Belousov } 1436f1379734SKonstantin Belousov } 1437f1379734SKonstantin Belousov 1438f1379734SKonstantin Belousov /* 1439868aabb4SRichard Scheffenegger * If PCP is set in mbuf, use it 1440868aabb4SRichard Scheffenegger */ 1441868aabb4SRichard Scheffenegger if ((*mp)->m_flags & M_VLANTAG) { 1442838c8c47SZhenlei Huang pcp = EVL_PRIOFTAG((*mp)->m_pkthdr.ether_vtag); 1443868aabb4SRichard Scheffenegger } 1444868aabb4SRichard Scheffenegger 1445868aabb4SRichard Scheffenegger /* 1446f1379734SKonstantin Belousov * If underlying interface can do VLAN tag insertion itself, 1447f1379734SKonstantin Belousov * just pass the packet along. However, we need some way to 1448f1379734SKonstantin Belousov * tell the interface where the packet came from so that it 1449f1379734SKonstantin Belousov * knows how to find the VLAN tag to use, so we attach a 1450f1379734SKonstantin Belousov * packet tag that holds it. 1451f1379734SKonstantin Belousov */ 145278bc3d5eSKristof Provost if (V_vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q, 1453f1379734SKonstantin Belousov MTAG_8021Q_PCP_OUT, NULL)) != NULL) 1454c7cffd65SAlexander V. Chernikov tag = EVL_MAKETAG(qtag->vid, *(uint8_t *)(mtag + 1), 0); 1455f1379734SKonstantin Belousov else 1456838c8c47SZhenlei Huang tag = EVL_MAKETAG(qtag->vid, pcp, 0); 1457c7cffd65SAlexander V. Chernikov if ((p->if_capenable & IFCAP_VLAN_HWTAGGING) && 1458c7cffd65SAlexander V. Chernikov (qtag->proto == ETHERTYPE_VLAN)) { 1459f1379734SKonstantin Belousov (*mp)->m_pkthdr.ether_vtag = tag; 1460f1379734SKonstantin Belousov (*mp)->m_flags |= M_VLANTAG; 1461f1379734SKonstantin Belousov } else { 1462c7cffd65SAlexander V. Chernikov *mp = ether_vlanencap_proto(*mp, tag, qtag->proto); 1463f1379734SKonstantin Belousov if (*mp == NULL) { 1464f1379734SKonstantin Belousov if_printf(ife, "unable to prepend 802.1Q header"); 1465f1379734SKonstantin Belousov return (false); 1466f1379734SKonstantin Belousov } 1467b22aae41SZhenlei Huang (*mp)->m_flags &= ~M_VLANTAG; 1468f1379734SKonstantin Belousov } 1469f1379734SKonstantin Belousov return (true); 1470f1379734SKonstantin Belousov } 1471f1379734SKonstantin Belousov 14726b7e0c1cSKyle Evans /* 14733c3aa8c1SKyle Evans * Allocate an address from the FreeBSD Foundation OUI. This uses a 14743f8bc99cSKristof Provost * cryptographic hash function on the containing jail's name, UUID and the 14753f8bc99cSKristof Provost * interface name to attempt to provide a unique but stable address. 14763f8bc99cSKristof Provost * Pseudo-interfaces which require a MAC address should use this function to 14773f8bc99cSKristof Provost * allocate non-locally-administered addresses. 14786b7e0c1cSKyle Evans */ 14793c3aa8c1SKyle Evans void 14803878bbf1SRonald Klop ether_gen_addr_byname(const char *nameunit, struct ether_addr *hwaddr) 14813c3aa8c1SKyle Evans { 14823c3aa8c1SKyle Evans SHA1_CTX ctx; 14833f8bc99cSKristof Provost char *buf; 14843c3aa8c1SKyle Evans char uuid[HOSTUUIDLEN + 1]; 14853c3aa8c1SKyle Evans uint64_t addr; 14863c3aa8c1SKyle Evans int i, sz; 14873c3aa8c1SKyle Evans char digest[SHA1_RESULTLEN]; 14883f8bc99cSKristof Provost char jailname[MAXHOSTNAMELEN]; 14893c3aa8c1SKyle Evans 14903c3aa8c1SKyle Evans getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); 14912d741f33SKyle Evans if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) { 14922d741f33SKyle Evans /* Fall back to a random mac address. */ 14932d741f33SKyle Evans goto rando; 14942d741f33SKyle Evans } 14952d741f33SKyle Evans 14963f8bc99cSKristof Provost /* If each (vnet) jail would also have a unique hostuuid this would not 14973f8bc99cSKristof Provost * be necessary. */ 14983f8bc99cSKristof Provost getjailname(curthread->td_ucred, jailname, sizeof(jailname)); 14993878bbf1SRonald Klop sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, nameunit, 15003f8bc99cSKristof Provost jailname); 15013f8bc99cSKristof Provost if (sz < 0) { 15023f8bc99cSKristof Provost /* Fall back to a random mac address. */ 15032d741f33SKyle Evans goto rando; 15043f8bc99cSKristof Provost } 15053f8bc99cSKristof Provost 15063c3aa8c1SKyle Evans SHA1Init(&ctx); 15073c3aa8c1SKyle Evans SHA1Update(&ctx, buf, sz); 15083c3aa8c1SKyle Evans SHA1Final(digest, &ctx); 15093f8bc99cSKristof Provost free(buf, M_TEMP); 15103c3aa8c1SKyle Evans 15113c3aa8c1SKyle Evans addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) & 15123c3aa8c1SKyle Evans OUI_FREEBSD_GENERATED_MASK; 15133c3aa8c1SKyle Evans addr = OUI_FREEBSD(addr); 15143c3aa8c1SKyle Evans for (i = 0; i < ETHER_ADDR_LEN; ++i) { 15153c3aa8c1SKyle Evans hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & 15163c3aa8c1SKyle Evans 0xFF; 15173c3aa8c1SKyle Evans } 15182d741f33SKyle Evans 15192d741f33SKyle Evans return; 15202d741f33SKyle Evans rando: 15212d741f33SKyle Evans arc4rand(hwaddr, sizeof(*hwaddr), 0); 15222d741f33SKyle Evans /* Unicast */ 15232d741f33SKyle Evans hwaddr->octet[0] &= 0xFE; 15242d741f33SKyle Evans /* Locally administered. */ 15252d741f33SKyle Evans hwaddr->octet[0] |= 0x02; 15266b7e0c1cSKyle Evans } 15276b7e0c1cSKyle Evans 15283878bbf1SRonald Klop void 15293878bbf1SRonald Klop ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) 15303878bbf1SRonald Klop { 15313878bbf1SRonald Klop ether_gen_addr_byname(if_name(ifp), hwaddr); 15323878bbf1SRonald Klop } 15333878bbf1SRonald Klop 1534fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 153540811c14SMatthew N. Dodd MODULE_VERSION(ether, 1); 1536