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> 4941ee9f1cSPoul-Henning Kamp #include <sys/module.h> 50df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 513c3aa8c1SKyle Evans #include <sys/proc.h> 52f1379734SKonstantin Belousov #include <sys/priv.h> 5310b1fde0SMark Murray #include <sys/random.h> 54df8bae1dSRodney W. Grimes #include <sys/socket.h> 5551a53488SBruce Evans #include <sys/sockio.h> 56602d513cSGarrett Wollman #include <sys/sysctl.h> 57ef1f9169SMarcel Moolenaar #include <sys/uuid.h> 58df8bae1dSRodney W. Grimes 593c3aa8c1SKyle Evans #include <net/ieee_oui.h> 60df8bae1dSRodney W. Grimes #include <net/if.h> 6176039bc8SGleb Smirnoff #include <net/if_var.h> 62d7647d96SDag-Erling Smørgrav #include <net/if_arp.h> 63df8bae1dSRodney W. Grimes #include <net/netisr.h> 64df8bae1dSRodney W. Grimes #include <net/route.h> 65df8bae1dSRodney W. Grimes #include <net/if_llc.h> 66df8bae1dSRodney W. Grimes #include <net/if_dl.h> 67df8bae1dSRodney W. Grimes #include <net/if_types.h> 682e2de7f2SArchie Cobbs #include <net/bpf.h> 69e1e1452dSArchie Cobbs #include <net/ethernet.h> 70fd6238a6SAndrew Thompson #include <net/if_bridgevar.h> 71c1d93b05SSam Leffler #include <net/if_vlan_var.h> 726e6b3f7cSQing Li #include <net/if_llatbl.h> 737d4317bdSAlexander V. Chernikov #include <net/pfil.h> 74b2bdc62aSAdrian Chadd #include <net/rss_config.h> 754b79449eSBjoern A. Zeeb #include <net/vnet.h> 76df8bae1dSRodney W. Grimes 7775bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h> 7875bf2db3SGleb Smirnoff 7982cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6) 80df8bae1dSRodney W. Grimes #include <netinet/in.h> 81df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 82df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 8354bfbd51SWill Andrews #include <netinet/ip_carp.h> 840bcfa8e4SLuigi Rizzo #include <netinet/ip_var.h> 851d5e9e22SEivind Eklund #endif 8682cd038dSYoshinobu Inoue #ifdef INET6 8782cd038dSYoshinobu Inoue #include <netinet6/nd6.h> 8882cd038dSYoshinobu Inoue #endif 89aed55708SRobert Watson #include <security/mac/mac_framework.h> 90aed55708SRobert Watson 913c3aa8c1SKyle Evans #include <crypto/sha1.h> 923c3aa8c1SKyle Evans 93d54d93acSEd Maste #ifdef CTASSERT 94d54d93acSEd Maste CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2); 95d54d93acSEd Maste CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN); 96d54d93acSEd Maste #endif 97d54d93acSEd Maste 98b252313fSGleb Smirnoff VNET_DEFINE(pfil_head_t, link_pfil_head); /* Packet filter hooks */ 997d4317bdSAlexander V. Chernikov 100e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */ 101c1d93b05SSam Leffler void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 102c1d93b05SSam Leffler void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 103e1e1452dSArchie Cobbs int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 104e1e1452dSArchie Cobbs void (*ng_ether_attach_p)(struct ifnet *ifp); 105e1e1452dSArchie Cobbs void (*ng_ether_detach_p)(struct ifnet *ifp); 106e1e1452dSArchie Cobbs 107824eb9dcSDavid E. O'Brien void (*vlan_input_p)(struct ifnet *, struct mbuf *); 1089d4fe4b2SBrooks Davis 109fd6238a6SAndrew Thompson /* if_bridge(4) support */ 110c8b01292SAndrew Thompson void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 1118f867517SAndrew Thompson 11218242d3bSAndrew Thompson /* if_lagg(4) support */ 11318242d3bSAndrew Thompson struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *); 114b47888ceSAndrew Thompson 115e589108dSRobert Watson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 116868d8b62SMatthew N. Dodd { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 117868d8b62SMatthew N. Dodd 118929ddbbbSAlfred Perlstein static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 119929ddbbbSAlfred Perlstein struct sockaddr *); 120d3c351c5SMarko Zec #ifdef VIMAGE 121d3c351c5SMarko Zec static void ether_reassign(struct ifnet *, struct vnet *, char *); 122d3c351c5SMarko Zec #endif 1234fb3a820SAlexander V. Chernikov static int ether_requestencap(struct ifnet *, struct if_encap_req *); 124868d8b62SMatthew N. Dodd 1254c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0) 126df8bae1dSRodney W. Grimes 12722f8ce43SAlexander V. Chernikov static void 12822f8ce43SAlexander V. Chernikov update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 12922f8ce43SAlexander V. Chernikov { 13022f8ce43SAlexander V. Chernikov int csum_flags = 0; 13122f8ce43SAlexander V. Chernikov 13222f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_IP) 13322f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 13422f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 13522f8ce43SAlexander V. Chernikov csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 13622f8ce43SAlexander V. Chernikov if (src->m_pkthdr.csum_flags & CSUM_SCTP) 13722f8ce43SAlexander V. Chernikov csum_flags |= CSUM_SCTP_VALID; 13822f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_flags |= csum_flags; 13922f8ce43SAlexander V. Chernikov if (csum_flags & CSUM_DATA_VALID) 14022f8ce43SAlexander V. Chernikov dst->m_pkthdr.csum_data = 0xffff; 14122f8ce43SAlexander V. Chernikov } 14222f8ce43SAlexander V. Chernikov 143df8bae1dSRodney W. Grimes /* 1444fb3a820SAlexander V. Chernikov * Handle link-layer encapsulation requests. 1454fb3a820SAlexander V. Chernikov */ 1464fb3a820SAlexander V. Chernikov static int 1474fb3a820SAlexander V. Chernikov ether_requestencap(struct ifnet *ifp, struct if_encap_req *req) 1484fb3a820SAlexander V. Chernikov { 1494fb3a820SAlexander V. Chernikov struct ether_header *eh; 1504fb3a820SAlexander V. Chernikov struct arphdr *ah; 1514fb3a820SAlexander V. Chernikov uint16_t etype; 1524fb3a820SAlexander V. Chernikov const u_char *lladdr; 1534fb3a820SAlexander V. Chernikov 1544fb3a820SAlexander V. Chernikov if (req->rtype != IFENCAP_LL) 1554fb3a820SAlexander V. Chernikov return (EOPNOTSUPP); 1564fb3a820SAlexander V. Chernikov 1574fb3a820SAlexander V. Chernikov if (req->bufsize < ETHER_HDR_LEN) 1584fb3a820SAlexander V. Chernikov return (ENOMEM); 1594fb3a820SAlexander V. Chernikov 1604fb3a820SAlexander V. Chernikov eh = (struct ether_header *)req->buf; 1614fb3a820SAlexander V. Chernikov lladdr = req->lladdr; 1624fb3a820SAlexander V. Chernikov req->lladdr_off = 0; 1634fb3a820SAlexander V. Chernikov 1644fb3a820SAlexander V. Chernikov switch (req->family) { 1654fb3a820SAlexander V. Chernikov case AF_INET: 1664fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IP); 1674fb3a820SAlexander V. Chernikov break; 1684fb3a820SAlexander V. Chernikov case AF_INET6: 1694fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 1704fb3a820SAlexander V. Chernikov break; 1714fb3a820SAlexander V. Chernikov case AF_ARP: 1724fb3a820SAlexander V. Chernikov ah = (struct arphdr *)req->hdata; 1734fb3a820SAlexander V. Chernikov ah->ar_hrd = htons(ARPHRD_ETHER); 1744fb3a820SAlexander V. Chernikov 1754fb3a820SAlexander V. Chernikov switch(ntohs(ah->ar_op)) { 1764fb3a820SAlexander V. Chernikov case ARPOP_REVREQUEST: 1774fb3a820SAlexander V. Chernikov case ARPOP_REVREPLY: 1784fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_REVARP); 1794fb3a820SAlexander V. Chernikov break; 1804fb3a820SAlexander V. Chernikov case ARPOP_REQUEST: 1814fb3a820SAlexander V. Chernikov case ARPOP_REPLY: 1824fb3a820SAlexander V. Chernikov default: 1834fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_ARP); 1844fb3a820SAlexander V. Chernikov break; 1854fb3a820SAlexander V. Chernikov } 1864fb3a820SAlexander V. Chernikov 1874fb3a820SAlexander V. Chernikov if (req->flags & IFENCAP_FLAG_BROADCAST) 1884fb3a820SAlexander V. Chernikov lladdr = ifp->if_broadcastaddr; 1894fb3a820SAlexander V. Chernikov break; 1904fb3a820SAlexander V. Chernikov default: 1914fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 1924fb3a820SAlexander V. Chernikov } 1934fb3a820SAlexander V. Chernikov 1944fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 1954fb3a820SAlexander V. Chernikov memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN); 1964fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 1974fb3a820SAlexander V. Chernikov req->bufsize = sizeof(struct ether_header); 1984fb3a820SAlexander V. Chernikov 1994fb3a820SAlexander V. Chernikov return (0); 2004fb3a820SAlexander V. Chernikov } 2014fb3a820SAlexander V. Chernikov 2024fb3a820SAlexander V. Chernikov static int 2034fb3a820SAlexander V. Chernikov ether_resolve_addr(struct ifnet *ifp, struct mbuf *m, 2044fb3a820SAlexander V. Chernikov const struct sockaddr *dst, struct route *ro, u_char *phdr, 2056d768226SGeorge V. Neville-Neil uint32_t *pflags, struct llentry **plle) 2064fb3a820SAlexander V. Chernikov { 2074fb3a820SAlexander V. Chernikov struct ether_header *eh; 2084fb3a820SAlexander V. Chernikov uint32_t lleflags = 0; 2094fb3a820SAlexander V. Chernikov int error = 0; 2104fb3a820SAlexander V. Chernikov #if defined(INET) || defined(INET6) 2114fb3a820SAlexander V. Chernikov uint16_t etype; 2124fb3a820SAlexander V. Chernikov #endif 2134fb3a820SAlexander V. Chernikov 2146d768226SGeorge V. Neville-Neil if (plle) 2156d768226SGeorge V. Neville-Neil *plle = NULL; 2164fb3a820SAlexander V. Chernikov eh = (struct ether_header *)phdr; 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: 2414fb3a820SAlexander V. Chernikov if ((m->m_flags & M_MCAST) == 0) 2426d768226SGeorge V. Neville-Neil error = nd6_resolve(ifp, 0, m, dst, phdr, &lleflags, 2436d768226SGeorge V. Neville-Neil plle); 2444fb3a820SAlexander V. Chernikov else { 2454fb3a820SAlexander V. Chernikov const struct in6_addr *a6; 2464fb3a820SAlexander V. Chernikov a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr); 2474fb3a820SAlexander V. Chernikov ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost); 2484fb3a820SAlexander V. Chernikov etype = htons(ETHERTYPE_IPV6); 2494fb3a820SAlexander V. Chernikov memcpy(&eh->ether_type, &etype, sizeof(etype)); 2504fb3a820SAlexander V. Chernikov memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 2514fb3a820SAlexander V. Chernikov } 2524fb3a820SAlexander V. Chernikov break; 2534fb3a820SAlexander V. Chernikov #endif 2544fb3a820SAlexander V. Chernikov default: 2554fb3a820SAlexander V. Chernikov if_printf(ifp, "can't handle af%d\n", dst->sa_family); 2564fb3a820SAlexander V. Chernikov if (m != NULL) 2574fb3a820SAlexander V. Chernikov m_freem(m); 2584fb3a820SAlexander V. Chernikov return (EAFNOSUPPORT); 2594fb3a820SAlexander V. Chernikov } 2604fb3a820SAlexander V. Chernikov 2614fb3a820SAlexander V. Chernikov if (error == EHOSTDOWN) { 26236402a68SAlexander V. Chernikov if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 2634fb3a820SAlexander V. Chernikov error = EHOSTUNREACH; 2644fb3a820SAlexander V. Chernikov } 2654fb3a820SAlexander V. Chernikov 2664fb3a820SAlexander V. Chernikov if (error != 0) 2674fb3a820SAlexander V. Chernikov return (error); 2684fb3a820SAlexander V. Chernikov 2694fb3a820SAlexander V. Chernikov *pflags = RT_MAY_LOOP; 2704fb3a820SAlexander V. Chernikov if (lleflags & LLE_IFADDR) 2714fb3a820SAlexander V. Chernikov *pflags |= RT_L2_ME; 2724fb3a820SAlexander V. Chernikov 2734fb3a820SAlexander V. Chernikov return (0); 2744fb3a820SAlexander V. Chernikov } 2754fb3a820SAlexander V. Chernikov 2764fb3a820SAlexander V. Chernikov /* 277df8bae1dSRodney W. Grimes * Ethernet output routine. 278df8bae1dSRodney W. Grimes * Encapsulate a packet of type family for the local net. 279df8bae1dSRodney W. Grimes * Use trailer local net encapsulation if enough data in first 280df8bae1dSRodney W. Grimes * packet leaves a multiple of 512 bytes of data in remainder. 281df8bae1dSRodney W. Grimes */ 282df8bae1dSRodney W. Grimes int 28372fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m, 28447e8d432SGleb Smirnoff const struct sockaddr *dst, struct route *ro) 285df8bae1dSRodney W. Grimes { 2864fb3a820SAlexander V. Chernikov int error = 0; 2874fb3a820SAlexander V. Chernikov char linkhdr[ETHER_HDR_LEN], *phdr; 288098a8c3bSMatthew N. Dodd struct ether_header *eh; 28960ee3847SMax Laier struct pf_mtag *t; 290c54c76ccSOleg Bulyzhin int loop_copy = 1; 291d32864c3SRobert Watson int hlen; /* link layer header length */ 2924fb3a820SAlexander V. Chernikov uint32_t pflags; 2936d768226SGeorge V. Neville-Neil struct llentry *lle = NULL; 2946d768226SGeorge V. Neville-Neil int addref = 0; 295df8bae1dSRodney W. Grimes 2964fb3a820SAlexander V. Chernikov phdr = NULL; 2974fb3a820SAlexander V. Chernikov pflags = 0; 298279aa3d4SKip Macy if (ro != NULL) { 2996d768226SGeorge V. Neville-Neil /* XXX BPF uses ro_prepend */ 3006d768226SGeorge V. Neville-Neil if (ro->ro_prepend != NULL) { 3014fb3a820SAlexander V. Chernikov phdr = ro->ro_prepend; 3024fb3a820SAlexander V. Chernikov hlen = ro->ro_plen; 3036d768226SGeorge V. Neville-Neil } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 3046d768226SGeorge V. Neville-Neil if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 3056d768226SGeorge V. Neville-Neil lle = ro->ro_lle; 3066d768226SGeorge V. Neville-Neil if (lle != NULL && 3076d768226SGeorge V. Neville-Neil (lle->la_flags & LLE_VALID) == 0) { 3086d768226SGeorge V. Neville-Neil LLE_FREE(lle); 3096d768226SGeorge V. Neville-Neil lle = NULL; /* redundant */ 3106d768226SGeorge V. Neville-Neil ro->ro_lle = NULL; 311279aa3d4SKip Macy } 3126d768226SGeorge V. Neville-Neil if (lle == NULL) { 3136d768226SGeorge V. Neville-Neil /* if we lookup, keep cache */ 3146d768226SGeorge V. Neville-Neil addref = 1; 3151435dcd9SAlexander V. Chernikov } else 3161435dcd9SAlexander V. Chernikov /* 3171435dcd9SAlexander V. Chernikov * Notify LLE code that 3181435dcd9SAlexander V. Chernikov * the entry was used 3191435dcd9SAlexander V. Chernikov * by datapath. 3201435dcd9SAlexander V. Chernikov */ 3211435dcd9SAlexander V. Chernikov llentry_mark_used(lle); 3226d768226SGeorge V. Neville-Neil } 3236d768226SGeorge V. Neville-Neil if (lle != NULL) { 3246d768226SGeorge V. Neville-Neil phdr = lle->r_linkdata; 3256d768226SGeorge V. Neville-Neil hlen = lle->r_hdrlen; 3266d768226SGeorge V. Neville-Neil pflags = lle->r_flags; 3276d768226SGeorge V. Neville-Neil } 3286d768226SGeorge V. Neville-Neil } 3296d768226SGeorge V. Neville-Neil } 3306d768226SGeorge V. Neville-Neil 33143b29369SRobert Watson #ifdef MAC 33230d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 33343b29369SRobert Watson if (error) 33443b29369SRobert Watson senderr(error); 33543b29369SRobert Watson #endif 33643b29369SRobert Watson 3376eeac1d9SJulian Elischer M_PROFILE(m); 338afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) 339afbe3a0fSPoul-Henning Kamp senderr(ENETDOWN); 34013f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 34113f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 342df8bae1dSRodney W. Grimes senderr(ENETDOWN); 3437f760c48SMatthew N. Dodd 3444fb3a820SAlexander V. Chernikov if (phdr == NULL) { 3454fb3a820SAlexander V. Chernikov /* No prepend data supplied. Try to calculate ourselves. */ 3464fb3a820SAlexander V. Chernikov phdr = linkhdr; 34784dd0fd0SJulian Elischer hlen = ETHER_HDR_LEN; 3486d768226SGeorge V. Neville-Neil error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 3496d768226SGeorge V. Neville-Neil addref ? &lle : NULL); 3506d768226SGeorge V. Neville-Neil if (addref && lle != NULL) 3516d768226SGeorge V. Neville-Neil ro->ro_lle = lle; 3524fb3a820SAlexander V. Chernikov if (error != 0) 353cd46a114SLuigi Rizzo return (error == EWOULDBLOCK ? 0 : error); 35443a6c75aSMatthew N. Dodd } 35543a6c75aSMatthew N. Dodd 3564fb3a820SAlexander V. Chernikov if ((pflags & RT_L2_ME) != 0) { 35722f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, m); 3586e6b3f7cSQing Li return (if_simloop(ifp, m, dst->sa_family, 0)); 3596e6b3f7cSQing Li } 3604fb3a820SAlexander V. Chernikov loop_copy = pflags & RT_MAY_LOOP; 3616e6b3f7cSQing Li 362df8bae1dSRodney W. Grimes /* 363df8bae1dSRodney W. Grimes * Add local net header. If no space in first mbuf, 364df8bae1dSRodney W. Grimes * allocate another. 3656cdb1854SAlexander V. Chernikov * 3666cdb1854SAlexander V. Chernikov * Note that we do prepend regardless of RT_HAS_HEADER flag. 3676cdb1854SAlexander V. Chernikov * This is done because BPF code shifts m_data pointer 3686cdb1854SAlexander V. Chernikov * to the end of ethernet header prior to calling if_output(). 369df8bae1dSRodney W. Grimes */ 3704fb3a820SAlexander V. Chernikov M_PREPEND(m, hlen, M_NOWAIT); 3712166ffe8SRobert Watson if (m == NULL) 372df8bae1dSRodney W. Grimes senderr(ENOBUFS); 3734fb3a820SAlexander V. Chernikov if ((pflags & RT_HAS_HEADER) == 0) { 374df8bae1dSRodney W. Grimes eh = mtod(m, struct ether_header *); 3754fb3a820SAlexander V. Chernikov memcpy(eh, phdr, hlen); 376c69aeaadSAlexander V. Chernikov } 377ed7509acSJulian Elischer 378ed7509acSJulian Elischer /* 379ed7509acSJulian Elischer * If a simplex interface, and the packet is being sent to our 380ed7509acSJulian Elischer * Ethernet address or a broadcast address, loopback a copy. 381ed7509acSJulian Elischer * XXX To make a simplex device behave exactly like a duplex 382ed7509acSJulian Elischer * device, we should copy in the case of sending to our own 383ed7509acSJulian Elischer * ethernet address (thus letting the original actually appear 384ed7509acSJulian Elischer * on the wire). However, we don't do that here for security 385ed7509acSJulian Elischer * reasons and compatibility with the original behavior. 386ed7509acSJulian Elischer */ 3874fb3a820SAlexander V. Chernikov if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) && 38860ee3847SMax Laier ((t = pf_find_mtag(m)) == NULL || !t->routed)) { 38906f684b0SMatthew N. Dodd struct mbuf *n; 390ed7509acSJulian Elischer 391294dd290SLuigi Rizzo /* 392294dd290SLuigi Rizzo * Because if_simloop() modifies the packet, we need a 393294dd290SLuigi Rizzo * writable copy through m_dup() instead of a readonly 394294dd290SLuigi Rizzo * one as m_copy[m] would give us. The alternative would 395294dd290SLuigi Rizzo * be to modify if_simloop() to handle the readonly mbuf, 396294dd290SLuigi Rizzo * but performancewise it is mostly equivalent (trading 397294dd290SLuigi Rizzo * extra data copying vs. extra locking). 3989983b3c0SYaroslav Tykhiy * 3999983b3c0SYaroslav Tykhiy * XXX This is a local workaround. A number of less 4009983b3c0SYaroslav Tykhiy * often used kernel parts suffer from the same bug. 4019983b3c0SYaroslav Tykhiy * See PR kern/105943 for a proposed general solution. 402294dd290SLuigi Rizzo */ 403eb1b1807SGleb Smirnoff if ((n = m_dup(m, M_NOWAIT)) != NULL) { 40422f8ce43SAlexander V. Chernikov update_mbuf_csumflags(m, n); 40506a429a3SArchie Cobbs (void)if_simloop(ifp, n, dst->sa_family, hlen); 406c1404dc0SArchie Cobbs } else 4073751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 408ed7509acSJulian Elischer } 4092e2de7f2SArchie Cobbs 4103f7d1396SAndrew Thompson /* 4113f7d1396SAndrew Thompson * Bridges require special output handling. 4123f7d1396SAndrew Thompson */ 4133f7d1396SAndrew Thompson if (ifp->if_bridge) { 4143f7d1396SAndrew Thompson BRIDGE_OUTPUT(ifp, m, error); 4153f7d1396SAndrew Thompson return (error); 4163f7d1396SAndrew Thompson } 4173f7d1396SAndrew Thompson 418259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 419a9771948SGleb Smirnoff if (ifp->if_carp && 42008b68b0eSGleb Smirnoff (error = (*carp_output_p)(ifp, m, dst))) 421a9771948SGleb Smirnoff goto bad; 422a9771948SGleb Smirnoff #endif 423a9771948SGleb Smirnoff 424e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 425833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 426514bcb89SPoul-Henning Kamp KASSERT(ng_ether_output_p != NULL, 427514bcb89SPoul-Henning Kamp ("ng_ether_output_p is NULL")); 428e1e1452dSArchie Cobbs if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 429e1e1452dSArchie Cobbs bad: if (m != NULL) 430e1e1452dSArchie Cobbs m_freem(m); 431e1e1452dSArchie Cobbs return (error); 432e1e1452dSArchie Cobbs } 433e1e1452dSArchie Cobbs if (m == NULL) 434e1e1452dSArchie Cobbs return (0); 435e1e1452dSArchie Cobbs } 436e1e1452dSArchie Cobbs 437e1e1452dSArchie Cobbs /* Continue with link-layer output */ 438e1e1452dSArchie Cobbs return ether_output_frame(ifp, m); 439e1e1452dSArchie Cobbs } 440e1e1452dSArchie Cobbs 441f1379734SKonstantin Belousov static bool 442f1379734SKonstantin Belousov ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp) 443f1379734SKonstantin Belousov { 444*c7cffd65SAlexander V. Chernikov struct ether_8021q_tag qtag; 445f1379734SKonstantin Belousov struct ether_header *eh; 446f1379734SKonstantin Belousov 447f1379734SKonstantin Belousov eh = mtod(*mp, struct ether_header *); 448f1379734SKonstantin Belousov if (ntohs(eh->ether_type) == ETHERTYPE_VLAN || 449*c7cffd65SAlexander V. Chernikov ntohs(eh->ether_type) == ETHERTYPE_QINQ) 450*c7cffd65SAlexander V. Chernikov return (true); 451*c7cffd65SAlexander V. Chernikov 452*c7cffd65SAlexander V. Chernikov qtag.vid = 0; 453*c7cffd65SAlexander V. Chernikov qtag.pcp = pcp; 454*c7cffd65SAlexander V. Chernikov qtag.proto = ETHERTYPE_VLAN; 455*c7cffd65SAlexander V. Chernikov if (ether_8021q_frame(mp, ifp, ifp, &qtag)) 456f1379734SKonstantin Belousov return (true); 457f1379734SKonstantin Belousov if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 458f1379734SKonstantin Belousov return (false); 459f1379734SKonstantin Belousov } 460f1379734SKonstantin Belousov 461e1e1452dSArchie Cobbs /* 462e1e1452dSArchie Cobbs * Ethernet link layer output routine to send a raw frame to the device. 463e1e1452dSArchie Cobbs * 464e1e1452dSArchie Cobbs * This assumes that the 14 byte Ethernet header is present and contiguous 465e1e1452dSArchie Cobbs * in the first mbuf (if BRIDGE'ing). 466e1e1452dSArchie Cobbs */ 467e1e1452dSArchie Cobbs int 468c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m) 469e1e1452dSArchie Cobbs { 470f1379734SKonstantin Belousov uint8_t pcp; 471f1379734SKonstantin Belousov 472f1379734SKonstantin Belousov pcp = ifp->if_pcp; 47347c39432SNavdeep Parhar if (pcp != IFNET_PCP_NONE && ifp->if_type != IFT_L2VLAN && 47447c39432SNavdeep Parhar !ether_set_pcp(&m, ifp, pcp)) 475f1379734SKonstantin Belousov return (0); 4762b25acc1SLuigi Rizzo 477b252313fSGleb Smirnoff if (PFIL_HOOKED_OUT(V_link_pfil_head)) 478b252313fSGleb Smirnoff switch (pfil_run_hooks(V_link_pfil_head, &m, ifp, PFIL_OUT, 479b252313fSGleb Smirnoff NULL)) { 480b252313fSGleb Smirnoff case PFIL_DROPPED: 4817d4317bdSAlexander V. Chernikov return (EACCES); 482b252313fSGleb Smirnoff case PFIL_CONSUMED: 4837d4317bdSAlexander V. Chernikov return (0); 4844b984093SLuigi Rizzo } 4852b25acc1SLuigi Rizzo 486201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL 487201100c5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 488201100c5SBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 48921231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 49021231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 491201100c5SBjoern A. Zeeb struct ether_header *eh; 492201100c5SBjoern A. Zeeb 493201100c5SBjoern A. Zeeb eh = mtod(m, struct ether_header *); 494201100c5SBjoern A. Zeeb switch (ntohs(eh->ether_type)) { 495201100c5SBjoern A. Zeeb case ETHERTYPE_IP: 496201100c5SBjoern A. Zeeb case ETHERTYPE_ARP: 49721231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 498201100c5SBjoern A. Zeeb m_freem(m); 499201100c5SBjoern A. Zeeb return (EAFNOSUPPORT); 500201100c5SBjoern A. Zeeb /* NOTREACHED */ 501201100c5SBjoern A. Zeeb break; 502201100c5SBjoern A. Zeeb }; 503201100c5SBjoern A. Zeeb } 504201100c5SBjoern A. Zeeb #endif 505201100c5SBjoern A. Zeeb #endif 506201100c5SBjoern A. Zeeb 507df8bae1dSRodney W. Grimes /* 508df5e1987SJonathan Lemon * Queue message on interface, update output statistics if 509df5e1987SJonathan Lemon * successful, and start output if interface not yet active. 510df8bae1dSRodney W. Grimes */ 511aea78d20SKip Macy return ((ifp->if_transmit)(ifp, m)); 512df8bae1dSRodney W. Grimes } 513df8bae1dSRodney W. Grimes 5144b984093SLuigi Rizzo /* 515c1d93b05SSam Leffler * Process a received Ethernet packet; the packet is in the 516c1d93b05SSam Leffler * mbuf chain m with the ethernet header at the front. 517df8bae1dSRodney W. Grimes */ 518c1d93b05SSam Leffler static void 5196cb52192SRobert Watson ether_input_internal(struct ifnet *ifp, struct mbuf *m) 520df8bae1dSRodney W. Grimes { 521c1d93b05SSam Leffler struct ether_header *eh; 522c1d93b05SSam Leffler u_short etype; 523df8bae1dSRodney W. Grimes 52469462a82SBruce M Simpson if ((ifp->if_flags & IFF_UP) == 0) { 52569462a82SBruce M Simpson m_freem(m); 52669462a82SBruce M Simpson return; 52769462a82SBruce M Simpson } 52869462a82SBruce M Simpson #ifdef DIAGNOSTIC 52969462a82SBruce M Simpson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 53069462a82SBruce M Simpson if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n"); 53169462a82SBruce M Simpson m_freem(m); 53269462a82SBruce M Simpson return; 53369462a82SBruce M Simpson } 53469462a82SBruce M Simpson #endif 535797f247bSMatthew N. Dodd if (m->m_len < ETHER_HDR_LEN) { 536c1d93b05SSam Leffler /* XXX maybe should pullup? */ 537c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o leading ethernet " 538c1d93b05SSam Leffler "header (len %u pkt len %u)\n", 539c1d93b05SSam Leffler m->m_len, m->m_pkthdr.len); 5403751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 541c1d93b05SSam Leffler m_freem(m); 542c1d93b05SSam Leffler return; 5432e2de7f2SArchie Cobbs } 544c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 545c1d93b05SSam Leffler etype = ntohs(eh->ether_type); 54619fa89e9SMark Murray random_harvest_queue_ether(m, sizeof(*m)); 547d1b06863SMark Murray 54821231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL 54921231a7aSBjoern A. Zeeb #if defined(INET6) && defined(INET) 55021231a7aSBjoern A. Zeeb /* draft-ietf-6man-ipv6only-flag */ 55121231a7aSBjoern A. Zeeb /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */ 55221231a7aSBjoern A. Zeeb if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) { 55321231a7aSBjoern A. Zeeb switch (etype) { 55421231a7aSBjoern A. Zeeb case ETHERTYPE_IP: 55521231a7aSBjoern A. Zeeb case ETHERTYPE_ARP: 55621231a7aSBjoern A. Zeeb case ETHERTYPE_REVARP: 55721231a7aSBjoern A. Zeeb m_freem(m); 55821231a7aSBjoern A. Zeeb return; 55921231a7aSBjoern A. Zeeb /* NOTREACHED */ 56021231a7aSBjoern A. Zeeb break; 56121231a7aSBjoern A. Zeeb }; 56221231a7aSBjoern A. Zeeb } 56321231a7aSBjoern A. Zeeb #endif 56421231a7aSBjoern A. Zeeb #endif 56521231a7aSBjoern A. Zeeb 56621ca7b57SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet); 56721ca7b57SMarko Zec 56869462a82SBruce M Simpson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 56969462a82SBruce M Simpson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 57069462a82SBruce M Simpson m->m_flags |= M_BCAST; 57169462a82SBruce M Simpson else 57269462a82SBruce M Simpson m->m_flags |= M_MCAST; 5733751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 57469462a82SBruce M Simpson } 57569462a82SBruce M Simpson 576533d8562SRobert Watson #ifdef MAC 577533d8562SRobert Watson /* 578533d8562SRobert Watson * Tag the mbuf with an appropriate MAC label before any other 579533d8562SRobert Watson * consumers can get to it. 580533d8562SRobert Watson */ 58130d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 582533d8562SRobert Watson #endif 583533d8562SRobert Watson 584c1d93b05SSam Leffler /* 58569462a82SBruce M Simpson * Give bpf a chance at the packet. 586c1d93b05SSam Leffler */ 587d05d4616SChristian S.J. Peron ETHER_BPF_MTAP(ifp, m); 5882e2de7f2SArchie Cobbs 58969462a82SBruce M Simpson /* 59069462a82SBruce M Simpson * If the CRC is still on the packet, trim it off. We do this once 59169462a82SBruce M Simpson * and once only in case we are re-entered. Nothing else on the 59269462a82SBruce M Simpson * Ethernet receive path expects to see the FCS. 59369462a82SBruce M Simpson */ 594c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 595c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 596c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 597c1d93b05SSam Leffler } 598c1d93b05SSam Leffler 5994cdc1f54SGleb Smirnoff if (!(ifp->if_capenable & IFCAP_HWSTATS)) 6003751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 60105463bb5SDavid Greenman 60269462a82SBruce M Simpson /* Allow monitor mode to claim this frame, after stats are updated. */ 603de572b37SChristian S.J. Peron if (ifp->if_flags & IFF_MONITOR) { 604de572b37SChristian S.J. Peron m_freem(m); 60521ca7b57SMarko Zec CURVNET_RESTORE(); 606de572b37SChristian S.J. Peron return; 607de572b37SChristian S.J. Peron } 608de572b37SChristian S.J. Peron 60918242d3bSAndrew Thompson /* Handle input from a lagg(4) port */ 610b47888ceSAndrew Thompson if (ifp->if_type == IFT_IEEE8023ADLAG) { 61118242d3bSAndrew Thompson KASSERT(lagg_input_p != NULL, 61218242d3bSAndrew Thompson ("%s: if_lagg not loaded!", __func__)); 61318242d3bSAndrew Thompson m = (*lagg_input_p)(ifp, m); 614b47888ceSAndrew Thompson if (m != NULL) 615b47888ceSAndrew Thompson ifp = m->m_pkthdr.rcvif; 6162db13e75SMarko Zec else { 6172db13e75SMarko Zec CURVNET_RESTORE(); 618b47888ceSAndrew Thompson return; 619b47888ceSAndrew Thompson } 6202db13e75SMarko Zec } 621b47888ceSAndrew Thompson 62269462a82SBruce M Simpson /* 62369462a82SBruce M Simpson * If the hardware did not process an 802.1Q tag, do this now, 62469462a82SBruce M Simpson * to allow 802.1P priority frames to be passed to the main input 62569462a82SBruce M Simpson * path correctly. 62669462a82SBruce M Simpson */ 627*c7cffd65SAlexander V. Chernikov if ((m->m_flags & M_VLANTAG) == 0 && 628*c7cffd65SAlexander V. Chernikov ((etype == ETHERTYPE_VLAN) || (etype == ETHERTYPE_QINQ))) { 62969462a82SBruce M Simpson struct ether_vlan_header *evl; 63069462a82SBruce M Simpson 63169462a82SBruce M Simpson if (m->m_len < sizeof(*evl) && 63269462a82SBruce M Simpson (m = m_pullup(m, sizeof(*evl))) == NULL) { 633402d5e27SBruce M Simpson #ifdef DIAGNOSTIC 63469462a82SBruce M Simpson if_printf(ifp, "cannot pullup VLAN header\n"); 635402d5e27SBruce M Simpson #endif 6363751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 6372db13e75SMarko Zec CURVNET_RESTORE(); 63869462a82SBruce M Simpson return; 63969462a82SBruce M Simpson } 64069462a82SBruce M Simpson 64169462a82SBruce M Simpson evl = mtod(m, struct ether_vlan_header *); 64269462a82SBruce M Simpson m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 64369462a82SBruce M Simpson m->m_flags |= M_VLANTAG; 64469462a82SBruce M Simpson 64569462a82SBruce M Simpson bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 64669462a82SBruce M Simpson ETHER_HDR_LEN - ETHER_TYPE_LEN); 64769462a82SBruce M Simpson m_adj(m, ETHER_VLAN_ENCAP_LEN); 64897cce87fSGleb Smirnoff eh = mtod(m, struct ether_header *); 64969462a82SBruce M Simpson } 65069462a82SBruce M Simpson 651a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 652a34c6aebSBjoern A. Zeeb 65369462a82SBruce M Simpson /* Allow ng_ether(4) to claim this frame. */ 654833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 655514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_p != NULL, 65669462a82SBruce M Simpson ("%s: ng_ether_input_p is NULL", __func__)); 65769462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 658c1d93b05SSam Leffler (*ng_ether_input_p)(ifp, &m); 65921ca7b57SMarko Zec if (m == NULL) { 66021ca7b57SMarko Zec CURVNET_RESTORE(); 661e1e1452dSArchie Cobbs return; 662e1e1452dSArchie Cobbs } 6635e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 66421ca7b57SMarko Zec } 665e1e1452dSArchie Cobbs 6668f867517SAndrew Thompson /* 66769462a82SBruce M Simpson * Allow if_bridge(4) to claim this frame. 66869462a82SBruce M Simpson * The BRIDGE_INPUT() macro will update ifp if the bridge changed it 66969462a82SBruce M Simpson * and the frame should be delivered locally. 6708f867517SAndrew Thompson */ 67169462a82SBruce M Simpson if (ifp->if_bridge != NULL) { 67269462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 673fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m); 67421ca7b57SMarko Zec if (m == NULL) { 67521ca7b57SMarko Zec CURVNET_RESTORE(); 6768f867517SAndrew Thompson return; 6778f867517SAndrew Thompson } 6785e9a5429SGleb Smirnoff eh = mtod(m, struct ether_header *); 67921ca7b57SMarko Zec } 6808f867517SAndrew Thompson 681259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 68269462a82SBruce M Simpson /* 68369462a82SBruce M Simpson * Clear M_PROMISC on frame so that carp(4) will see it when the 68469462a82SBruce M Simpson * mbuf flows up to Layer 3. 68569462a82SBruce M Simpson * FreeBSD's implementation of carp(4) uses the inprotosw 68669462a82SBruce M Simpson * to dispatch IPPROTO_CARP. carp(4) also allocates its own 68769462a82SBruce M Simpson * Ethernet addresses of the form 00:00:5e:00:01:xx, which 68869462a82SBruce M Simpson * is outside the scope of the M_PROMISC test below. 68969462a82SBruce M Simpson * TODO: Maintain a hash table of ethernet addresses other than 69069462a82SBruce M Simpson * ether_dhost which may be active on this ifp. 69169462a82SBruce M Simpson */ 69254bfbd51SWill Andrews if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) { 69369462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 69469462a82SBruce M Simpson } else 69569462a82SBruce M Simpson #endif 69669462a82SBruce M Simpson { 69769462a82SBruce M Simpson /* 698e7f8c833SBruce M Simpson * If the frame received was not for our MAC address, set the 69969462a82SBruce M Simpson * M_PROMISC flag on the mbuf chain. The frame may need to 70069462a82SBruce M Simpson * be seen by the rest of the Ethernet input path in case of 70169462a82SBruce M Simpson * re-entry (e.g. bridge, vlan, netgraph) but should not be 70269462a82SBruce M Simpson * seen by upper protocol layers. 70369462a82SBruce M Simpson */ 70469462a82SBruce M Simpson if (!ETHER_IS_MULTICAST(eh->ether_dhost) && 705e7f8c833SBruce M Simpson bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0) 70669462a82SBruce M Simpson m->m_flags |= M_PROMISC; 70769462a82SBruce M Simpson } 70869462a82SBruce M Simpson 709acf032f5SRobert Watson ether_demux(ifp, m); 71021ca7b57SMarko Zec CURVNET_RESTORE(); 711e1e1452dSArchie Cobbs } 712e1e1452dSArchie Cobbs 713e1e1452dSArchie Cobbs /* 7146cb52192SRobert Watson * Ethernet input dispatch; by default, direct dispatch here regardless of 7157527624eSRobert Watson * global configuration. However, if RSS is enabled, hook up RSS affinity 7167527624eSRobert Watson * so that when deferred or hybrid dispatch is enabled, we can redistribute 7177527624eSRobert Watson * load based on RSS. 7187527624eSRobert Watson * 7197527624eSRobert Watson * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or 7207527624eSRobert Watson * not it had already done work distribution via multi-queue. Then we could 7217527624eSRobert Watson * direct dispatch in the event load balancing was already complete and 7227527624eSRobert Watson * handle the case of interfaces with different capabilities better. 7237527624eSRobert Watson * 7247527624eSRobert Watson * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions 7257527624eSRobert Watson * at multiple layers? 7267527624eSRobert Watson * 7277527624eSRobert Watson * XXXRW: For now, enable all this only if RSS is compiled in, although it 7287527624eSRobert Watson * works fine without RSS. Need to characterise the performance overhead 7297527624eSRobert Watson * of the detour through the netisr code in the event the result is always 7307527624eSRobert Watson * direct dispatch. 7316cb52192SRobert Watson */ 7326cb52192SRobert Watson static void 7336cb52192SRobert Watson ether_nh_input(struct mbuf *m) 7346cb52192SRobert Watson { 7356cb52192SRobert Watson 736b71bed24SAndrey V. Elsukov M_ASSERTPKTHDR(m); 737b71bed24SAndrey V. Elsukov KASSERT(m->m_pkthdr.rcvif != NULL, 738b71bed24SAndrey V. Elsukov ("%s: NULL interface pointer", __func__)); 7396cb52192SRobert Watson ether_input_internal(m->m_pkthdr.rcvif, m); 7406cb52192SRobert Watson } 7416cb52192SRobert Watson 7426cb52192SRobert Watson static struct netisr_handler ether_nh = { 7436cb52192SRobert Watson .nh_name = "ether", 7446cb52192SRobert Watson .nh_handler = ether_nh_input, 7456cb52192SRobert Watson .nh_proto = NETISR_ETHER, 7467527624eSRobert Watson #ifdef RSS 7477527624eSRobert Watson .nh_policy = NETISR_POLICY_CPU, 7487527624eSRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7497527624eSRobert Watson .nh_m2cpuid = rss_m2cpuid, 7507527624eSRobert Watson #else 7516cb52192SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 7526cb52192SRobert Watson .nh_dispatch = NETISR_DISPATCH_DIRECT, 7537527624eSRobert Watson #endif 7546cb52192SRobert Watson }; 7556cb52192SRobert Watson 7566cb52192SRobert Watson static void 7576cb52192SRobert Watson ether_init(__unused void *arg) 7586cb52192SRobert Watson { 7596cb52192SRobert Watson 7606cb52192SRobert Watson netisr_register(ðer_nh); 7616cb52192SRobert Watson } 7626cb52192SRobert Watson SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL); 7636cb52192SRobert Watson 7646cb52192SRobert Watson static void 7657d4317bdSAlexander V. Chernikov vnet_ether_init(__unused void *arg) 7667d4317bdSAlexander V. Chernikov { 767b252313fSGleb Smirnoff struct pfil_head_args args; 7687d4317bdSAlexander V. Chernikov 769b252313fSGleb Smirnoff args.pa_version = PFIL_VERSION; 770b252313fSGleb Smirnoff args.pa_flags = PFIL_IN | PFIL_OUT; 771b252313fSGleb Smirnoff args.pa_type = PFIL_TYPE_ETHERNET; 772b252313fSGleb Smirnoff args.pa_headname = PFIL_ETHER_NAME; 773b252313fSGleb Smirnoff V_link_pfil_head = pfil_head_register(&args); 774b252313fSGleb Smirnoff 775484149deSBjoern A. Zeeb #ifdef VIMAGE 776484149deSBjoern A. Zeeb netisr_register_vnet(ðer_nh); 777484149deSBjoern A. Zeeb #endif 7787d4317bdSAlexander V. Chernikov } 7797d4317bdSAlexander V. Chernikov VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY, 7807d4317bdSAlexander V. Chernikov vnet_ether_init, NULL); 7817d4317bdSAlexander V. Chernikov 782484149deSBjoern A. Zeeb #ifdef VIMAGE 7837d4317bdSAlexander V. Chernikov static void 784484149deSBjoern A. Zeeb vnet_ether_pfil_destroy(__unused void *arg) 7857d4317bdSAlexander V. Chernikov { 7867d4317bdSAlexander V. Chernikov 787b252313fSGleb Smirnoff pfil_head_unregister(V_link_pfil_head); 7887d4317bdSAlexander V. Chernikov } 789484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY, 790484149deSBjoern A. Zeeb vnet_ether_pfil_destroy, NULL); 791484149deSBjoern A. Zeeb 792484149deSBjoern A. Zeeb static void 793484149deSBjoern A. Zeeb vnet_ether_destroy(__unused void *arg) 794484149deSBjoern A. Zeeb { 795484149deSBjoern A. Zeeb 796484149deSBjoern A. Zeeb netisr_unregister_vnet(ðer_nh); 797484149deSBjoern A. Zeeb } 7987d4317bdSAlexander V. Chernikov VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, 7997d4317bdSAlexander V. Chernikov vnet_ether_destroy, NULL); 800484149deSBjoern A. Zeeb #endif 8017d4317bdSAlexander V. Chernikov 8027d4317bdSAlexander V. Chernikov static void 8036cb52192SRobert Watson ether_input(struct ifnet *ifp, struct mbuf *m) 8046cb52192SRobert Watson { 805b8a6e03fSGleb Smirnoff struct epoch_tracker et; 8064857f5fbSGeorge V. Neville-Neil struct mbuf *mn; 807e87c4940SGleb Smirnoff bool needs_epoch; 808e87c4940SGleb Smirnoff 809e87c4940SGleb Smirnoff needs_epoch = !(ifp->if_flags & IFF_KNOWSEPOCH); 8104857f5fbSGeorge V. Neville-Neil 8114857f5fbSGeorge V. Neville-Neil /* 8124857f5fbSGeorge V. Neville-Neil * The drivers are allowed to pass in a chain of packets linked with 8134857f5fbSGeorge V. Neville-Neil * m_nextpkt. We split them up into separate packets here and pass 8144857f5fbSGeorge V. Neville-Neil * them up. This allows the drivers to amortize the receive lock. 8154857f5fbSGeorge V. Neville-Neil */ 816b8a6e03fSGleb Smirnoff CURVNET_SET_QUIET(ifp->if_vnet); 817e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 818b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et); 8194857f5fbSGeorge V. Neville-Neil while (m) { 8204857f5fbSGeorge V. Neville-Neil mn = m->m_nextpkt; 8214857f5fbSGeorge V. Neville-Neil m->m_nextpkt = NULL; 8224857f5fbSGeorge V. Neville-Neil 8236cb52192SRobert Watson /* 824b8a6e03fSGleb Smirnoff * We will rely on rcvif being set properly in the deferred 825b8a6e03fSGleb Smirnoff * context, so assert it is correct here. 8266cb52192SRobert Watson */ 827fb3bc596SJohn Baldwin MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 828e84ef07fSBjoern A. Zeeb KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p " 829e84ef07fSBjoern A. Zeeb "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp)); 8306cb52192SRobert Watson netisr_dispatch(NETISR_ETHER, m); 8314857f5fbSGeorge V. Neville-Neil m = mn; 8324857f5fbSGeorge V. Neville-Neil } 833e87c4940SGleb Smirnoff if (__predict_false(needs_epoch)) 834b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et); 835b8a6e03fSGleb Smirnoff CURVNET_RESTORE(); 8366cb52192SRobert Watson } 8376cb52192SRobert Watson 8386cb52192SRobert Watson /* 839e1e1452dSArchie Cobbs * Upper layer processing for a received Ethernet packet. 840e1e1452dSArchie Cobbs */ 841e1e1452dSArchie Cobbs void 842c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m) 843e1e1452dSArchie Cobbs { 844c1d93b05SSam Leffler struct ether_header *eh; 8457d4317bdSAlexander V. Chernikov int i, isr; 846e1e1452dSArchie Cobbs u_short ether_type; 84769462a82SBruce M Simpson 848b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 84969462a82SBruce M Simpson KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__)); 85069462a82SBruce M Simpson 8517d4317bdSAlexander V. Chernikov /* Do not grab PROMISC frames in case we are re-entered. */ 852b252313fSGleb Smirnoff if (PFIL_HOOKED_IN(V_link_pfil_head) && !(m->m_flags & M_PROMISC)) { 853b252313fSGleb Smirnoff i = pfil_run_hooks(V_link_pfil_head, &m, ifp, PFIL_IN, NULL); 8547d4317bdSAlexander V. Chernikov if (i != 0 || m == NULL) 8557d4317bdSAlexander V. Chernikov return; 85669462a82SBruce M Simpson } 8577d4317bdSAlexander V. Chernikov 858c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 859cd0cd014SJoerg Wunsch ether_type = ntohs(eh->ether_type); 860c1d93b05SSam Leffler 861a9771948SGleb Smirnoff /* 86269462a82SBruce M Simpson * If this frame has a VLAN tag other than 0, call vlan_input() 86369462a82SBruce M Simpson * if its module is loaded. Otherwise, drop. 864a9771948SGleb Smirnoff */ 86569462a82SBruce M Simpson if ((m->m_flags & M_VLANTAG) && 86669462a82SBruce M Simpson EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 86775ee267cSGleb Smirnoff if (ifp->if_vlantrunk == NULL) { 8683751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 8696c23e6ccSRuslan Ermilov m_freem(m); 8706c23e6ccSRuslan Ermilov return; 8716c23e6ccSRuslan Ermilov } 87269462a82SBruce M Simpson KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 87369462a82SBruce M Simpson __func__)); 87469462a82SBruce M Simpson /* Clear before possibly re-entering ether_input(). */ 87569462a82SBruce M Simpson m->m_flags &= ~M_PROMISC; 876c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 877c1d93b05SSam Leffler return; 878c1d93b05SSam Leffler } 879c1d93b05SSam Leffler 880c1d93b05SSam Leffler /* 88169462a82SBruce M Simpson * Pass promiscuously received frames to the upper layer if the user 88269462a82SBruce M Simpson * requested this by setting IFF_PPROMISC. Otherwise, drop them. 883c1d93b05SSam Leffler */ 88469462a82SBruce M Simpson if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 885c1d93b05SSam Leffler m_freem(m); 886c1d93b05SSam Leffler return; 887c1d93b05SSam Leffler } 888c1d93b05SSam Leffler 88969462a82SBruce M Simpson /* 89069462a82SBruce M Simpson * Reset layer specific mbuf flags to avoid confusing upper layers. 89169462a82SBruce M Simpson * Strip off Ethernet header. 89269462a82SBruce M Simpson */ 89369462a82SBruce M Simpson m->m_flags &= ~M_VLANTAG; 89486bd0491SAndre Oppermann m_clrprotoflags(m); 895797f247bSMatthew N. Dodd m_adj(m, ETHER_HDR_LEN); 896c1d93b05SSam Leffler 89769462a82SBruce M Simpson /* 89869462a82SBruce M Simpson * Dispatch frame to upper layer. 89969462a82SBruce M Simpson */ 900307d80beSDavid Greenman switch (ether_type) { 901df8bae1dSRodney W. Grimes #ifdef INET 902df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 9031cafed39SJonathan Lemon isr = NETISR_IP; 904df8bae1dSRodney W. Grimes break; 905df8bae1dSRodney W. Grimes 906df8bae1dSRodney W. Grimes case ETHERTYPE_ARP: 90708aadfbbSJonathan Lemon if (ifp->if_flags & IFF_NOARP) { 90808aadfbbSJonathan Lemon /* Discard packet if ARP is disabled on interface */ 90908aadfbbSJonathan Lemon m_freem(m); 91008aadfbbSJonathan Lemon return; 91108aadfbbSJonathan Lemon } 9121cafed39SJonathan Lemon isr = NETISR_ARP; 913df8bae1dSRodney W. Grimes break; 914df8bae1dSRodney W. Grimes #endif 91582cd038dSYoshinobu Inoue #ifdef INET6 91682cd038dSYoshinobu Inoue case ETHERTYPE_IPV6: 9171cafed39SJonathan Lemon isr = NETISR_IPV6; 91882cd038dSYoshinobu Inoue break; 91982cd038dSYoshinobu Inoue #endif 920df8bae1dSRodney W. Grimes default: 9211cafed39SJonathan Lemon goto discard; 922df8bae1dSRodney W. Grimes } 9231cafed39SJonathan Lemon netisr_dispatch(isr, m); 924c1d93b05SSam Leffler return; 9251cafed39SJonathan Lemon 926c1d93b05SSam Leffler discard: 927c1d93b05SSam Leffler /* 928c1d93b05SSam Leffler * Packet is to be discarded. If netgraph is present, 929c1d93b05SSam Leffler * hand the packet to it for last chance processing; 930c1d93b05SSam Leffler * otherwise dispose of it. 931c1d93b05SSam Leffler */ 932833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 933514bcb89SPoul-Henning Kamp KASSERT(ng_ether_input_orphan_p != NULL, 934514bcb89SPoul-Henning Kamp ("ng_ether_input_orphan_p is NULL")); 935c1d93b05SSam Leffler /* 936c1d93b05SSam Leffler * Put back the ethernet header so netgraph has a 937c1d93b05SSam Leffler * consistent view of inbound packets. 938c1d93b05SSam Leffler */ 939eb1b1807SGleb Smirnoff M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT); 940c1d93b05SSam Leffler (*ng_ether_input_orphan_p)(ifp, m); 941c1d93b05SSam Leffler return; 942c1d93b05SSam Leffler } 943c1d93b05SSam Leffler m_freem(m); 944c1d93b05SSam Leffler } 945c1d93b05SSam Leffler 946c1d93b05SSam Leffler /* 947c1d93b05SSam Leffler * Convert Ethernet address to printable (loggable) representation. 948c1d93b05SSam Leffler * This routine is for compatibility; it's better to just use 949c1d93b05SSam Leffler * 950c1d93b05SSam Leffler * printf("%6D", <pointer to address>, ":"); 951c1d93b05SSam Leffler * 952c1d93b05SSam Leffler * since there's no static buffer involved. 953c1d93b05SSam Leffler */ 954c1d93b05SSam Leffler char * 955c1d93b05SSam Leffler ether_sprintf(const u_char *ap) 956c1d93b05SSam Leffler { 957c1d93b05SSam Leffler static char etherbuf[18]; 958c1d93b05SSam Leffler snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 959c1d93b05SSam Leffler return (etherbuf); 960df8bae1dSRodney W. Grimes } 961df8bae1dSRodney W. Grimes 962df8bae1dSRodney W. Grimes /* 963df8bae1dSRodney W. Grimes * Perform common duties while attaching to interface list 964df8bae1dSRodney W. Grimes */ 965df8bae1dSRodney W. Grimes void 966d09ed26fSRuslan Ermilov ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 967df8bae1dSRodney W. Grimes { 968f93dfa28SBrooks Davis int i; 969098a8c3bSMatthew N. Dodd struct ifaddr *ifa; 970098a8c3bSMatthew N. Dodd struct sockaddr_dl *sdl; 971df8bae1dSRodney W. Grimes 972c1d93b05SSam Leffler ifp->if_addrlen = ETHER_ADDR_LEN; 973c1d93b05SSam Leffler ifp->if_hdrlen = ETHER_HDR_LEN; 974df8bae1dSRodney W. Grimes ifp->if_mtu = ETHERMTU; 975fa2ab81eSKyle Evans if_attach(ifp); 976c1d93b05SSam Leffler ifp->if_output = ether_output; 977c1d93b05SSam Leffler ifp->if_input = ether_input; 9781158dfb7SGarrett Wollman ifp->if_resolvemulti = ether_resolvemulti; 9794fb3a820SAlexander V. Chernikov ifp->if_requestencap = ether_requestencap; 980d3c351c5SMarko Zec #ifdef VIMAGE 981d3c351c5SMarko Zec ifp->if_reassign = ether_reassign; 982d3c351c5SMarko Zec #endif 983a330e1f1SGary Palmer if (ifp->if_baudrate == 0) 984c1d93b05SSam Leffler ifp->if_baudrate = IF_Mbps(10); /* just a default */ 985322dcb8dSMax Khon ifp->if_broadcastaddr = etherbroadcastaddr; 986c1d93b05SSam Leffler 9874a0d6638SRuslan Ermilov ifa = ifp->if_addr; 9886e551fb6SDavid E. O'Brien KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 98959562606SGarrett Wollman sdl = (struct sockaddr_dl *)ifa->ifa_addr; 990df8bae1dSRodney W. Grimes sdl->sdl_type = IFT_ETHER; 991df8bae1dSRodney W. Grimes sdl->sdl_alen = ifp->if_addrlen; 992d09ed26fSRuslan Ermilov bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 993c1d93b05SSam Leffler 9945b1a5e45SRavi Pokala if (ifp->if_hw_addr != NULL) 995ddae5750SRavi Pokala bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen); 996ddae5750SRavi Pokala 997797f247bSMatthew N. Dodd bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 998e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) 999e1e1452dSArchie Cobbs (*ng_ether_attach_p)(ifp); 1000e3bbbec2SMatthew N. Dodd 1001f93dfa28SBrooks Davis /* Announce Ethernet MAC address if non-zero. */ 1002f93dfa28SBrooks Davis for (i = 0; i < ifp->if_addrlen; i++) 1003d09ed26fSRuslan Ermilov if (lla[i] != 0) 1004f93dfa28SBrooks Davis break; 1005f93dfa28SBrooks Davis if (i != ifp->if_addrlen) 1006d09ed26fSRuslan Ermilov if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 1007ef1f9169SMarcel Moolenaar 1008ef1f9169SMarcel Moolenaar uuid_ether_add(LLADDR(sdl)); 10098819ad85SSepherosa Ziehau 10108819ad85SSepherosa Ziehau /* Add necessary bits are setup; announce it now. */ 10118819ad85SSepherosa Ziehau EVENTHANDLER_INVOKE(ether_ifattach_event, ifp); 10128819ad85SSepherosa Ziehau if (IS_DEFAULT_VNET(curvnet)) 10138819ad85SSepherosa Ziehau devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL); 1014e1e1452dSArchie Cobbs } 1015e1e1452dSArchie Cobbs 101621b8ebd9SArchie Cobbs /* 101721b8ebd9SArchie Cobbs * Perform common duties while detaching an Ethernet interface 101821b8ebd9SArchie Cobbs */ 101921b8ebd9SArchie Cobbs void 1020c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp) 102121b8ebd9SArchie Cobbs { 1022ef1f9169SMarcel Moolenaar struct sockaddr_dl *sdl; 1023ef1f9169SMarcel Moolenaar 1024ef1f9169SMarcel Moolenaar sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr); 1025ef1f9169SMarcel Moolenaar uuid_ether_del(LLADDR(sdl)); 1026ef1f9169SMarcel Moolenaar 1027833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1028514bcb89SPoul-Henning Kamp KASSERT(ng_ether_detach_p != NULL, 1029514bcb89SPoul-Henning Kamp ("ng_ether_detach_p is NULL")); 103021b8ebd9SArchie Cobbs (*ng_ether_detach_p)(ifp); 1031514bcb89SPoul-Henning Kamp } 103220a65f37SAndrew Thompson 103321b8ebd9SArchie Cobbs bpfdetach(ifp); 103421b8ebd9SArchie Cobbs if_detach(ifp); 103521b8ebd9SArchie Cobbs } 103621b8ebd9SArchie Cobbs 1037d3c351c5SMarko Zec #ifdef VIMAGE 1038d3c351c5SMarko Zec void 1039d3c351c5SMarko Zec ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused) 1040d3c351c5SMarko Zec { 1041d3c351c5SMarko Zec 1042833e8dc5SGleb Smirnoff if (ifp->if_l2com != NULL) { 1043d3c351c5SMarko Zec KASSERT(ng_ether_detach_p != NULL, 1044d3c351c5SMarko Zec ("ng_ether_detach_p is NULL")); 1045d3c351c5SMarko Zec (*ng_ether_detach_p)(ifp); 1046d3c351c5SMarko Zec } 1047d3c351c5SMarko Zec 1048d3c351c5SMarko Zec if (ng_ether_attach_p != NULL) { 1049d3c351c5SMarko Zec CURVNET_SET_QUIET(new_vnet); 1050d3c351c5SMarko Zec (*ng_ether_attach_p)(ifp); 1051d3c351c5SMarko Zec CURVNET_RESTORE(); 1052d3c351c5SMarko Zec } 1053d3c351c5SMarko Zec } 1054d3c351c5SMarko Zec #endif 1055d3c351c5SMarko Zec 1056ce02431fSDoug Rabson SYSCTL_DECL(_net_link); 10577029da5cSPawel Biernacki SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 10587029da5cSPawel Biernacki "Ethernet"); 105930106f6aSPoul-Henning Kamp 106016b4a343SChristian Weisgerber #if 0 106116b4a343SChristian Weisgerber /* 106216b4a343SChristian Weisgerber * This is for reference. We have a table-driven version 106316b4a343SChristian Weisgerber * of the little-endian crc32 generator, which is faster 106416b4a343SChristian Weisgerber * than the double-loop. 106516b4a343SChristian Weisgerber */ 106616b4a343SChristian Weisgerber uint32_t 106716b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 106816b4a343SChristian Weisgerber { 106916b4a343SChristian Weisgerber size_t i; 107016b4a343SChristian Weisgerber uint32_t crc; 107116b4a343SChristian Weisgerber int bit; 107216b4a343SChristian Weisgerber uint8_t data; 107316b4a343SChristian Weisgerber 107416b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 107516b4a343SChristian Weisgerber 107616b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 1077933dad75SAntoine Brodin for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 107816b4a343SChristian Weisgerber carry = (crc ^ data) & 1; 107916b4a343SChristian Weisgerber crc >>= 1; 108016b4a343SChristian Weisgerber if (carry) 108116b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_LE); 108216b4a343SChristian Weisgerber } 1083933dad75SAntoine Brodin } 108416b4a343SChristian Weisgerber 108516b4a343SChristian Weisgerber return (crc); 108616b4a343SChristian Weisgerber } 108716b4a343SChristian Weisgerber #else 108816b4a343SChristian Weisgerber uint32_t 108916b4a343SChristian Weisgerber ether_crc32_le(const uint8_t *buf, size_t len) 109016b4a343SChristian Weisgerber { 109116b4a343SChristian Weisgerber static const uint32_t crctab[] = { 109216b4a343SChristian Weisgerber 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 109316b4a343SChristian Weisgerber 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 109416b4a343SChristian Weisgerber 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 109516b4a343SChristian Weisgerber 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 109616b4a343SChristian Weisgerber }; 109716b4a343SChristian Weisgerber size_t i; 109816b4a343SChristian Weisgerber uint32_t crc; 109916b4a343SChristian Weisgerber 110016b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 110116b4a343SChristian Weisgerber 110216b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 110316b4a343SChristian Weisgerber crc ^= buf[i]; 110416b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 110516b4a343SChristian Weisgerber crc = (crc >> 4) ^ crctab[crc & 0xf]; 110616b4a343SChristian Weisgerber } 110716b4a343SChristian Weisgerber 110816b4a343SChristian Weisgerber return (crc); 110916b4a343SChristian Weisgerber } 111016b4a343SChristian Weisgerber #endif 111116b4a343SChristian Weisgerber 111216b4a343SChristian Weisgerber uint32_t 111316b4a343SChristian Weisgerber ether_crc32_be(const uint8_t *buf, size_t len) 111416b4a343SChristian Weisgerber { 111516b4a343SChristian Weisgerber size_t i; 111616b4a343SChristian Weisgerber uint32_t crc, carry; 111716b4a343SChristian Weisgerber int bit; 111816b4a343SChristian Weisgerber uint8_t data; 111916b4a343SChristian Weisgerber 112016b4a343SChristian Weisgerber crc = 0xffffffff; /* initial value */ 112116b4a343SChristian Weisgerber 112216b4a343SChristian Weisgerber for (i = 0; i < len; i++) { 112316b4a343SChristian Weisgerber for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 112416b4a343SChristian Weisgerber carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 112516b4a343SChristian Weisgerber crc <<= 1; 112616b4a343SChristian Weisgerber if (carry) 112716b4a343SChristian Weisgerber crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 112816b4a343SChristian Weisgerber } 112916b4a343SChristian Weisgerber } 113016b4a343SChristian Weisgerber 113116b4a343SChristian Weisgerber return (crc); 113216b4a343SChristian Weisgerber } 113316b4a343SChristian Weisgerber 1134fb583156SDavid Greenman int 1135995c7fd1SYaroslav Tykhiy ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 113630106f6aSPoul-Henning Kamp { 113730106f6aSPoul-Henning Kamp struct ifaddr *ifa = (struct ifaddr *) data; 113830106f6aSPoul-Henning Kamp struct ifreq *ifr = (struct ifreq *) data; 1139fb583156SDavid Greenman int error = 0; 114030106f6aSPoul-Henning Kamp 114130106f6aSPoul-Henning Kamp switch (command) { 114230106f6aSPoul-Henning Kamp case SIOCSIFADDR: 114330106f6aSPoul-Henning Kamp ifp->if_flags |= IFF_UP; 114430106f6aSPoul-Henning Kamp 114530106f6aSPoul-Henning Kamp switch (ifa->ifa_addr->sa_family) { 114630106f6aSPoul-Henning Kamp #ifdef INET 114730106f6aSPoul-Henning Kamp case AF_INET: 114830106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1149322dcb8dSMax Khon arp_ifinit(ifp, ifa); 115030106f6aSPoul-Henning Kamp break; 115130106f6aSPoul-Henning Kamp #endif 115230106f6aSPoul-Henning Kamp default: 115330106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 115430106f6aSPoul-Henning Kamp break; 115530106f6aSPoul-Henning Kamp } 115630106f6aSPoul-Henning Kamp break; 115730106f6aSPoul-Henning Kamp 115830106f6aSPoul-Henning Kamp case SIOCGIFADDR: 115938d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 116038d958a6SBrooks Davis ETHER_ADDR_LEN); 116130106f6aSPoul-Henning Kamp break; 1162fb583156SDavid Greenman 1163fb583156SDavid Greenman case SIOCSIFMTU: 1164fb583156SDavid Greenman /* 1165fb583156SDavid Greenman * Set the interface MTU. 1166fb583156SDavid Greenman */ 1167fb583156SDavid Greenman if (ifr->ifr_mtu > ETHERMTU) { 1168fb583156SDavid Greenman error = EINVAL; 1169fb583156SDavid Greenman } else { 1170fb583156SDavid Greenman ifp->if_mtu = ifr->ifr_mtu; 117130106f6aSPoul-Henning Kamp } 1172fb583156SDavid Greenman break; 1173f1379734SKonstantin Belousov 1174f1379734SKonstantin Belousov case SIOCSLANPCP: 1175f1379734SKonstantin Belousov error = priv_check(curthread, PRIV_NET_SETLANPCP); 1176f1379734SKonstantin Belousov if (error != 0) 1177f1379734SKonstantin Belousov break; 1178f1379734SKonstantin Belousov if (ifr->ifr_lan_pcp > 7 && 11794a381a9eSHans Petter Selasky ifr->ifr_lan_pcp != IFNET_PCP_NONE) { 1180f1379734SKonstantin Belousov error = EINVAL; 11814a381a9eSHans Petter Selasky } else { 1182f1379734SKonstantin Belousov ifp->if_pcp = ifr->ifr_lan_pcp; 11834a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 11844a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 11854a381a9eSHans Petter Selasky } 1186f1379734SKonstantin Belousov break; 1187f1379734SKonstantin Belousov 1188f1379734SKonstantin Belousov case SIOCGLANPCP: 1189f1379734SKonstantin Belousov ifr->ifr_lan_pcp = ifp->if_pcp; 1190f1379734SKonstantin Belousov break; 1191f1379734SKonstantin Belousov 1192c1d93b05SSam Leffler default: 1193c1d93b05SSam Leffler error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1194c1d93b05SSam Leffler break; 1195fb583156SDavid Greenman } 1196fb583156SDavid Greenman return (error); 119730106f6aSPoul-Henning Kamp } 11981158dfb7SGarrett Wollman 119937c84183SPoul-Henning Kamp static int 120072fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 120172fd1b6aSDag-Erling Smørgrav struct sockaddr *sa) 12021158dfb7SGarrett Wollman { 12031158dfb7SGarrett Wollman struct sockaddr_dl *sdl; 1204d7647d96SDag-Erling Smørgrav #ifdef INET 12051158dfb7SGarrett Wollman struct sockaddr_in *sin; 1206d7647d96SDag-Erling Smørgrav #endif 120782cd038dSYoshinobu Inoue #ifdef INET6 120882cd038dSYoshinobu Inoue struct sockaddr_in6 *sin6; 120982cd038dSYoshinobu Inoue #endif 12101158dfb7SGarrett Wollman u_char *e_addr; 12111158dfb7SGarrett Wollman 12121158dfb7SGarrett Wollman switch(sa->sa_family) { 12131158dfb7SGarrett Wollman case AF_LINK: 12147f33a738SJulian Elischer /* 12157f33a738SJulian Elischer * No mapping needed. Just check that it's a valid MC address. 12167f33a738SJulian Elischer */ 12171158dfb7SGarrett Wollman sdl = (struct sockaddr_dl *)sa; 12181158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 1219086e98c4SBruce M Simpson if (!ETHER_IS_MULTICAST(e_addr)) 12201158dfb7SGarrett Wollman return EADDRNOTAVAIL; 1221155d72c4SPedro F. Giffuni *llsa = NULL; 12221158dfb7SGarrett Wollman return 0; 12231158dfb7SGarrett Wollman 12241158dfb7SGarrett Wollman #ifdef INET 12251158dfb7SGarrett Wollman case AF_INET: 12261158dfb7SGarrett Wollman sin = (struct sockaddr_in *)sa; 12271158dfb7SGarrett Wollman if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 12281158dfb7SGarrett Wollman return EADDRNOTAVAIL; 122995fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 12301158dfb7SGarrett Wollman sdl->sdl_alen = ETHER_ADDR_LEN; 12311158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 12321158dfb7SGarrett Wollman ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 12331158dfb7SGarrett Wollman *llsa = (struct sockaddr *)sdl; 12341158dfb7SGarrett Wollman return 0; 12351158dfb7SGarrett Wollman #endif 123682cd038dSYoshinobu Inoue #ifdef INET6 123782cd038dSYoshinobu Inoue case AF_INET6: 123882cd038dSYoshinobu Inoue sin6 = (struct sockaddr_in6 *)sa; 1239595b8a1cSJun-ichiro itojun Hagino if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1240595b8a1cSJun-ichiro itojun Hagino /* 1241595b8a1cSJun-ichiro itojun Hagino * An IP6 address of 0 means listen to all 1242595b8a1cSJun-ichiro itojun Hagino * of the Ethernet multicast address used for IP6. 1243595b8a1cSJun-ichiro itojun Hagino * (This is used for multicast routers.) 1244595b8a1cSJun-ichiro itojun Hagino */ 1245595b8a1cSJun-ichiro itojun Hagino ifp->if_flags |= IFF_ALLMULTI; 1246155d72c4SPedro F. Giffuni *llsa = NULL; 1247595b8a1cSJun-ichiro itojun Hagino return 0; 1248595b8a1cSJun-ichiro itojun Hagino } 124982cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 125082cd038dSYoshinobu Inoue return EADDRNOTAVAIL; 125195fbe4d0SAlexander V. Chernikov sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 125282cd038dSYoshinobu Inoue sdl->sdl_alen = ETHER_ADDR_LEN; 125382cd038dSYoshinobu Inoue e_addr = LLADDR(sdl); 125482cd038dSYoshinobu Inoue ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 125582cd038dSYoshinobu Inoue *llsa = (struct sockaddr *)sdl; 125682cd038dSYoshinobu Inoue return 0; 125782cd038dSYoshinobu Inoue #endif 12581158dfb7SGarrett Wollman 12591158dfb7SGarrett Wollman default: 12601158dfb7SGarrett Wollman /* 12611158dfb7SGarrett Wollman * Well, the text isn't quite right, but it's the name 12621158dfb7SGarrett Wollman * that counts... 12631158dfb7SGarrett Wollman */ 12641158dfb7SGarrett Wollman return EAFNOSUPPORT; 12651158dfb7SGarrett Wollman } 12661158dfb7SGarrett Wollman } 126740811c14SMatthew N. Dodd 126840811c14SMatthew N. Dodd static moduledata_t ether_mod = { 1269833e8dc5SGleb Smirnoff .name = "ether", 127040811c14SMatthew N. Dodd }; 127140811c14SMatthew N. Dodd 127252f1277eSChristian S.J. Peron void 127352f1277eSChristian S.J. Peron ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 127452f1277eSChristian S.J. Peron { 127552f1277eSChristian S.J. Peron struct ether_vlan_header vlan; 127652f1277eSChristian S.J. Peron struct mbuf mv, mb; 127752f1277eSChristian S.J. Peron 127852f1277eSChristian S.J. Peron KASSERT((m->m_flags & M_VLANTAG) != 0, 127952f1277eSChristian S.J. Peron ("%s: vlan information not present", __func__)); 128052f1277eSChristian S.J. Peron KASSERT(m->m_len >= sizeof(struct ether_header), 128152f1277eSChristian S.J. Peron ("%s: mbuf not large enough for header", __func__)); 128252f1277eSChristian S.J. Peron bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 128352f1277eSChristian S.J. Peron vlan.evl_proto = vlan.evl_encap_proto; 128452f1277eSChristian S.J. Peron vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 128552f1277eSChristian S.J. Peron vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 128652f1277eSChristian S.J. Peron m->m_len -= sizeof(struct ether_header); 128752f1277eSChristian S.J. Peron m->m_data += sizeof(struct ether_header); 128852f1277eSChristian S.J. Peron /* 128952f1277eSChristian S.J. Peron * If a data link has been supplied by the caller, then we will need to 129052f1277eSChristian S.J. Peron * re-create a stack allocated mbuf chain with the following structure: 129152f1277eSChristian S.J. Peron * 129252f1277eSChristian S.J. Peron * (1) mbuf #1 will contain the supplied data link 129352f1277eSChristian S.J. Peron * (2) mbuf #2 will contain the vlan header 129452f1277eSChristian S.J. Peron * (3) mbuf #3 will contain the original mbuf's packet data 129552f1277eSChristian S.J. Peron * 129652f1277eSChristian S.J. Peron * Otherwise, submit the packet and vlan header via bpf_mtap2(). 129752f1277eSChristian S.J. Peron */ 129852f1277eSChristian S.J. Peron if (data != NULL) { 129952f1277eSChristian S.J. Peron mv.m_next = m; 130052f1277eSChristian S.J. Peron mv.m_data = (caddr_t)&vlan; 130152f1277eSChristian S.J. Peron mv.m_len = sizeof(vlan); 130252f1277eSChristian S.J. Peron mb.m_next = &mv; 130352f1277eSChristian S.J. Peron mb.m_data = data; 130452f1277eSChristian S.J. Peron mb.m_len = dlen; 130552f1277eSChristian S.J. Peron bpf_mtap(bp, &mb); 130652f1277eSChristian S.J. Peron } else 130752f1277eSChristian S.J. Peron bpf_mtap2(bp, &vlan, sizeof(vlan), m); 130852f1277eSChristian S.J. Peron m->m_len += sizeof(struct ether_header); 130952f1277eSChristian S.J. Peron m->m_data -= sizeof(struct ether_header); 131052f1277eSChristian S.J. Peron } 131152f1277eSChristian S.J. Peron 131260e87ca8SAndrew Thompson struct mbuf * 1313*c7cffd65SAlexander V. Chernikov ether_vlanencap_proto(struct mbuf *m, uint16_t tag, uint16_t proto) 131460e87ca8SAndrew Thompson { 131560e87ca8SAndrew Thompson struct ether_vlan_header *evl; 131660e87ca8SAndrew Thompson 1317eb1b1807SGleb Smirnoff M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 131860e87ca8SAndrew Thompson if (m == NULL) 131960e87ca8SAndrew Thompson return (NULL); 132060e87ca8SAndrew Thompson /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 132160e87ca8SAndrew Thompson 132260e87ca8SAndrew Thompson if (m->m_len < sizeof(*evl)) { 132360e87ca8SAndrew Thompson m = m_pullup(m, sizeof(*evl)); 132460e87ca8SAndrew Thompson if (m == NULL) 132560e87ca8SAndrew Thompson return (NULL); 132660e87ca8SAndrew Thompson } 132760e87ca8SAndrew Thompson 132860e87ca8SAndrew Thompson /* 132960e87ca8SAndrew Thompson * Transform the Ethernet header into an Ethernet header 133060e87ca8SAndrew Thompson * with 802.1Q encapsulation. 133160e87ca8SAndrew Thompson */ 133260e87ca8SAndrew Thompson evl = mtod(m, struct ether_vlan_header *); 133360e87ca8SAndrew Thompson bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 133460e87ca8SAndrew Thompson (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1335*c7cffd65SAlexander V. Chernikov evl->evl_encap_proto = htons(proto); 133660e87ca8SAndrew Thompson evl->evl_tag = htons(tag); 133760e87ca8SAndrew Thompson return (m); 133860e87ca8SAndrew Thompson } 133960e87ca8SAndrew Thompson 13407029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1341f1379734SKonstantin Belousov "IEEE 802.1Q VLAN"); 13427029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, 13437029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1344f1379734SKonstantin Belousov "for consistency"); 1345f1379734SKonstantin Belousov 13465f901c92SAndrew Turner VNET_DEFINE_STATIC(int, soft_pad); 1347f1379734SKonstantin Belousov #define V_soft_pad VNET(soft_pad) 1348f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 1349f1379734SKonstantin Belousov &VNET_NAME(soft_pad), 0, 1350f1379734SKonstantin Belousov "pad short frames before tagging"); 1351f1379734SKonstantin Belousov 1352f1379734SKonstantin Belousov /* 1353f1379734SKonstantin Belousov * For now, make preserving PCP via an mbuf tag optional, as it increases 1354f1379734SKonstantin Belousov * per-packet memory allocations and frees. In the future, it would be 1355f1379734SKonstantin Belousov * preferable to reuse ether_vtag for this, or similar. 1356f1379734SKonstantin Belousov */ 1357f1379734SKonstantin Belousov int vlan_mtag_pcp = 0; 1358f1379734SKonstantin Belousov SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW, 1359f1379734SKonstantin Belousov &vlan_mtag_pcp, 0, 1360f1379734SKonstantin Belousov "Retain VLAN PCP information as packets are passed up the stack"); 1361f1379734SKonstantin Belousov 1362f1379734SKonstantin Belousov bool 1363f1379734SKonstantin Belousov ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, 1364*c7cffd65SAlexander V. Chernikov struct ether_8021q_tag *qtag) 1365f1379734SKonstantin Belousov { 1366f1379734SKonstantin Belousov struct m_tag *mtag; 1367f1379734SKonstantin Belousov int n; 1368f1379734SKonstantin Belousov uint16_t tag; 1369f1379734SKonstantin Belousov static const char pad[8]; /* just zeros */ 1370f1379734SKonstantin Belousov 1371f1379734SKonstantin Belousov /* 1372f1379734SKonstantin Belousov * Pad the frame to the minimum size allowed if told to. 1373f1379734SKonstantin Belousov * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1374f1379734SKonstantin Belousov * paragraph C.4.4.3.b. It can help to work around buggy 1375f1379734SKonstantin Belousov * bridges that violate paragraph C.4.4.3.a from the same 1376f1379734SKonstantin Belousov * document, i.e., fail to pad short frames after untagging. 1377f1379734SKonstantin Belousov * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1378f1379734SKonstantin Belousov * untagging it will produce a 62-byte frame, which is a runt 1379f1379734SKonstantin Belousov * and requires padding. There are VLAN-enabled network 1380f1379734SKonstantin Belousov * devices that just discard such runts instead or mishandle 1381f1379734SKonstantin Belousov * them somehow. 1382f1379734SKonstantin Belousov */ 1383f1379734SKonstantin Belousov if (V_soft_pad && p->if_type == IFT_ETHER) { 1384f1379734SKonstantin Belousov for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len; 1385f1379734SKonstantin Belousov n > 0; n -= sizeof(pad)) { 1386f1379734SKonstantin Belousov if (!m_append(*mp, min(n, sizeof(pad)), pad)) 1387f1379734SKonstantin Belousov break; 1388f1379734SKonstantin Belousov } 1389f1379734SKonstantin Belousov if (n > 0) { 1390f1379734SKonstantin Belousov m_freem(*mp); 1391f1379734SKonstantin Belousov *mp = NULL; 1392f1379734SKonstantin Belousov if_printf(ife, "cannot pad short frame"); 1393f1379734SKonstantin Belousov return (false); 1394f1379734SKonstantin Belousov } 1395f1379734SKonstantin Belousov } 1396f1379734SKonstantin Belousov 1397f1379734SKonstantin Belousov /* 1398868aabb4SRichard Scheffenegger * If PCP is set in mbuf, use it 1399868aabb4SRichard Scheffenegger */ 1400868aabb4SRichard Scheffenegger if ((*mp)->m_flags & M_VLANTAG) { 1401*c7cffd65SAlexander V. Chernikov qtag->pcp = EVL_PRIOFTAG((*mp)->m_pkthdr.ether_vtag); 1402868aabb4SRichard Scheffenegger } 1403868aabb4SRichard Scheffenegger 1404868aabb4SRichard Scheffenegger /* 1405f1379734SKonstantin Belousov * If underlying interface can do VLAN tag insertion itself, 1406f1379734SKonstantin Belousov * just pass the packet along. However, we need some way to 1407f1379734SKonstantin Belousov * tell the interface where the packet came from so that it 1408f1379734SKonstantin Belousov * knows how to find the VLAN tag to use, so we attach a 1409f1379734SKonstantin Belousov * packet tag that holds it. 1410f1379734SKonstantin Belousov */ 1411f1379734SKonstantin Belousov if (vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q, 1412f1379734SKonstantin Belousov MTAG_8021Q_PCP_OUT, NULL)) != NULL) 1413*c7cffd65SAlexander V. Chernikov tag = EVL_MAKETAG(qtag->vid, *(uint8_t *)(mtag + 1), 0); 1414f1379734SKonstantin Belousov else 1415*c7cffd65SAlexander V. Chernikov tag = EVL_MAKETAG(qtag->vid, qtag->pcp, 0); 1416*c7cffd65SAlexander V. Chernikov if ((p->if_capenable & IFCAP_VLAN_HWTAGGING) && 1417*c7cffd65SAlexander V. Chernikov (qtag->proto == ETHERTYPE_VLAN)) { 1418f1379734SKonstantin Belousov (*mp)->m_pkthdr.ether_vtag = tag; 1419f1379734SKonstantin Belousov (*mp)->m_flags |= M_VLANTAG; 1420f1379734SKonstantin Belousov } else { 1421*c7cffd65SAlexander V. Chernikov *mp = ether_vlanencap_proto(*mp, tag, qtag->proto); 1422f1379734SKonstantin Belousov if (*mp == NULL) { 1423f1379734SKonstantin Belousov if_printf(ife, "unable to prepend 802.1Q header"); 1424f1379734SKonstantin Belousov return (false); 1425f1379734SKonstantin Belousov } 1426f1379734SKonstantin Belousov } 1427f1379734SKonstantin Belousov return (true); 1428f1379734SKonstantin Belousov } 1429f1379734SKonstantin Belousov 14306b7e0c1cSKyle Evans /* 14313c3aa8c1SKyle Evans * Allocate an address from the FreeBSD Foundation OUI. This uses a 14323f8bc99cSKristof Provost * cryptographic hash function on the containing jail's name, UUID and the 14333f8bc99cSKristof Provost * interface name to attempt to provide a unique but stable address. 14343f8bc99cSKristof Provost * Pseudo-interfaces which require a MAC address should use this function to 14353f8bc99cSKristof Provost * allocate non-locally-administered addresses. 14366b7e0c1cSKyle Evans */ 14373c3aa8c1SKyle Evans void 14383c3aa8c1SKyle Evans ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) 14393c3aa8c1SKyle Evans { 14403c3aa8c1SKyle Evans SHA1_CTX ctx; 14413f8bc99cSKristof Provost char *buf; 14423c3aa8c1SKyle Evans char uuid[HOSTUUIDLEN + 1]; 14433c3aa8c1SKyle Evans uint64_t addr; 14443c3aa8c1SKyle Evans int i, sz; 14453c3aa8c1SKyle Evans char digest[SHA1_RESULTLEN]; 14463f8bc99cSKristof Provost char jailname[MAXHOSTNAMELEN]; 14473c3aa8c1SKyle Evans 14483c3aa8c1SKyle Evans getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); 14493f8bc99cSKristof Provost /* If each (vnet) jail would also have a unique hostuuid this would not 14503f8bc99cSKristof Provost * be necessary. */ 14513f8bc99cSKristof Provost getjailname(curthread->td_ucred, jailname, sizeof(jailname)); 14523f8bc99cSKristof Provost sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, if_name(ifp), 14533f8bc99cSKristof Provost jailname); 14543f8bc99cSKristof Provost if (sz < 0) { 14553f8bc99cSKristof Provost /* Fall back to a random mac address. */ 14563f8bc99cSKristof Provost arc4rand(hwaddr, sizeof(*hwaddr), 0); 14573f8bc99cSKristof Provost hwaddr->octet[0] = 0x02; 14583f8bc99cSKristof Provost return; 14593f8bc99cSKristof Provost } 14603f8bc99cSKristof Provost 14613c3aa8c1SKyle Evans SHA1Init(&ctx); 14623c3aa8c1SKyle Evans SHA1Update(&ctx, buf, sz); 14633c3aa8c1SKyle Evans SHA1Final(digest, &ctx); 14643f8bc99cSKristof Provost free(buf, M_TEMP); 14653c3aa8c1SKyle Evans 14663c3aa8c1SKyle Evans addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) & 14673c3aa8c1SKyle Evans OUI_FREEBSD_GENERATED_MASK; 14683c3aa8c1SKyle Evans addr = OUI_FREEBSD(addr); 14693c3aa8c1SKyle Evans for (i = 0; i < ETHER_ADDR_LEN; ++i) { 14703c3aa8c1SKyle Evans hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & 14713c3aa8c1SKyle Evans 0xFF; 14723c3aa8c1SKyle Evans } 14736b7e0c1cSKyle Evans } 14746b7e0c1cSKyle Evans 1475fc74a9f9SBrooks Davis DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 147640811c14SMatthew N. Dodd MODULE_VERSION(ether, 1); 1477