1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 377262d3e4SEivind Eklund #include "opt_atalk.h" 381d5e9e22SEivind Eklund #include "opt_inet.h" 39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 40430df5f4SEivind Eklund #include "opt_ipx.h" 41fb5fbe46SLuigi Rizzo #include "opt_bdg.h" 4243b29369SRobert Watson #include "opt_mac.h" 434cf49a43SJulian Elischer #include "opt_netgraph.h" 44430df5f4SEivind Eklund 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 47df8bae1dSRodney W. Grimes #include <sys/kernel.h> 4843b29369SRobert Watson #include <sys/mac.h> 49df8bae1dSRodney W. Grimes #include <sys/malloc.h> 50df8bae1dSRodney W. Grimes #include <sys/mbuf.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> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 57df8bae1dSRodney W. Grimes #include <net/netisr.h> 58df8bae1dSRodney W. Grimes #include <net/route.h> 59df8bae1dSRodney W. Grimes #include <net/if_llc.h> 60df8bae1dSRodney W. Grimes #include <net/if_dl.h> 61df8bae1dSRodney W. Grimes #include <net/if_types.h> 622e2de7f2SArchie Cobbs #include <net/bpf.h> 63e1e1452dSArchie Cobbs #include <net/ethernet.h> 64db69a05dSPaul Saab #include <net/bridge.h> 65c1d93b05SSam Leffler #include <net/if_vlan_var.h> 66df8bae1dSRodney W. Grimes 6782cd038dSYoshinobu Inoue #if defined(INET) || defined(INET6) 68df8bae1dSRodney W. Grimes #include <netinet/in.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 714b984093SLuigi Rizzo #include <netinet/ip_fw.h> 724b984093SLuigi Rizzo #include <netinet/ip_dummynet.h> 731d5e9e22SEivind Eklund #endif 7482cd038dSYoshinobu Inoue #ifdef INET6 7582cd038dSYoshinobu Inoue #include <netinet6/nd6.h> 7682cd038dSYoshinobu Inoue #endif 77df8bae1dSRodney W. Grimes 78cc6a66f2SJulian Elischer #ifdef IPX 79cc6a66f2SJulian Elischer #include <netipx/ipx.h> 80cc6a66f2SJulian Elischer #include <netipx/ipx_if.h> 814f93599fSBoris Popov int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 825accfb8cSBoris Popov int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 832a7e8eceSBoris Popov struct sockaddr *dst, short *tp, int *hlen); 84cc6a66f2SJulian Elischer #endif 85cc6a66f2SJulian Elischer 86655929bfSJulian Elischer #ifdef NETATALK 87655929bfSJulian Elischer #include <netatalk/at.h> 88655929bfSJulian Elischer #include <netatalk/at_var.h> 89655929bfSJulian Elischer #include <netatalk/at_extern.h> 90655929bfSJulian Elischer 91655929bfSJulian Elischer #define llc_snap_org_code llc_un.type_snap.org_code 92655929bfSJulian Elischer #define llc_snap_ether_type llc_un.type_snap.ether_type 93655929bfSJulian Elischer 94655929bfSJulian Elischer extern u_char at_org_code[3]; 95655929bfSJulian Elischer extern u_char aarp_org_code[3]; 962cc2df49SGarrett Wollman #endif /* NETATALK */ 972cc2df49SGarrett Wollman 98e1e1452dSArchie Cobbs /* netgraph node hooks for ng_ether(4) */ 99c1d93b05SSam Leffler void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 100c1d93b05SSam Leffler void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 101e1e1452dSArchie Cobbs int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 102e1e1452dSArchie Cobbs void (*ng_ether_attach_p)(struct ifnet *ifp); 103e1e1452dSArchie Cobbs void (*ng_ether_detach_p)(struct ifnet *ifp); 104e1e1452dSArchie Cobbs 105824eb9dcSDavid E. O'Brien void (*vlan_input_p)(struct ifnet *, struct mbuf *); 1069d4fe4b2SBrooks Davis 107db69a05dSPaul Saab /* bridge support */ 108cad15830SLuigi Rizzo int do_bridge; 109db69a05dSPaul Saab bridge_in_t *bridge_in_ptr; 110db69a05dSPaul Saab bdg_forward_t *bdg_forward_ptr; 111db69a05dSPaul Saab bdgtakeifaces_t *bdgtakeifaces_ptr; 112cad15830SLuigi Rizzo struct bdg_softc *ifp2sc; 113db69a05dSPaul Saab 114868d8b62SMatthew N. Dodd static u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 115868d8b62SMatthew N. Dodd { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 116868d8b62SMatthew N. Dodd 117929ddbbbSAlfred Perlstein static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 118929ddbbbSAlfred Perlstein struct sockaddr *); 119868d8b62SMatthew N. Dodd 1204c8e8c05SJulian Elischer #define senderr(e) do { error = (e); goto bad;} while (0) 1214c8e8c05SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP) 122df8bae1dSRodney W. Grimes 1234b984093SLuigi Rizzo int 1244b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 125c1d93b05SSam Leffler struct ip_fw **rule, int shared); 1264b984093SLuigi Rizzo static int ether_ipfw; 1274b984093SLuigi Rizzo 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * Ethernet output routine. 130df8bae1dSRodney W. Grimes * Encapsulate a packet of type family for the local net. 131df8bae1dSRodney W. Grimes * Use trailer local net encapsulation if enough data in first 132df8bae1dSRodney W. Grimes * packet leaves a multiple of 512 bytes of data in remainder. 133df8bae1dSRodney W. Grimes * Assumes that ifp is actually pointer to arpcom structure. 134df8bae1dSRodney W. Grimes */ 135df8bae1dSRodney W. Grimes int 13672fd1b6aSDag-Erling Smørgrav ether_output(struct ifnet *ifp, struct mbuf *m, 13772fd1b6aSDag-Erling Smørgrav struct sockaddr *dst, struct rtentry *rt0) 138df8bae1dSRodney W. Grimes { 139df8bae1dSRodney W. Grimes short type; 140e1e1452dSArchie Cobbs int error = 0, hdrcmplt = 0; 14164760eb0SWarner Losh u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN]; 142098a8c3bSMatthew N. Dodd struct rtentry *rt; 143098a8c3bSMatthew N. Dodd struct ether_header *eh; 1449961e27dSLuigi Rizzo int loop_copy = 0; 145d32864c3SRobert Watson int hlen; /* link layer header length */ 1464c8e8c05SJulian Elischer struct arpcom *ac = IFP2AC(ifp); 147df8bae1dSRodney W. Grimes 14843b29369SRobert Watson #ifdef MAC 14943b29369SRobert Watson error = mac_check_ifnet_transmit(ifp, m); 15043b29369SRobert Watson if (error) 15143b29369SRobert Watson senderr(error); 15243b29369SRobert Watson #endif 15343b29369SRobert Watson 154afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) 155afbe3a0fSPoul-Henning Kamp senderr(ENETDOWN); 156df8bae1dSRodney W. Grimes if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 157df8bae1dSRodney W. Grimes senderr(ENETDOWN); 1587f760c48SMatthew N. Dodd 1597f760c48SMatthew N. Dodd error = rt_check(&rt, &rt0, dst); 1607f760c48SMatthew N. Dodd if (error) 1617f760c48SMatthew N. Dodd goto bad; 1627f760c48SMatthew N. Dodd 16384dd0fd0SJulian Elischer hlen = ETHER_HDR_LEN; 164df8bae1dSRodney W. Grimes switch (dst->sa_family) { 165df8bae1dSRodney W. Grimes #ifdef INET 166df8bae1dSRodney W. Grimes case AF_INET: 167322dcb8dSMax Khon if (!arpresolve(ifp, rt, m, dst, edst, rt0)) 168df8bae1dSRodney W. Grimes return (0); /* if not yet resolved */ 16934bed8b0SDavid Greenman type = htons(ETHERTYPE_IP); 170df8bae1dSRodney W. Grimes break; 171df8bae1dSRodney W. Grimes #endif 17282cd038dSYoshinobu Inoue #ifdef INET6 17382cd038dSYoshinobu Inoue case AF_INET6: 17482cd038dSYoshinobu Inoue if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 175fef5fd23SBosko Milekic /* Something bad happened */ 17682cd038dSYoshinobu Inoue return(0); 17782cd038dSYoshinobu Inoue } 17882cd038dSYoshinobu Inoue type = htons(ETHERTYPE_IPV6); 17982cd038dSYoshinobu Inoue break; 18082cd038dSYoshinobu Inoue #endif 181cc6a66f2SJulian Elischer #ifdef IPX 182cc6a66f2SJulian Elischer case AF_IPX: 1834f93599fSBoris Popov if (ef_outputp) { 1842a7e8eceSBoris Popov error = ef_outputp(ifp, &m, dst, &type, &hlen); 1855accfb8cSBoris Popov if (error) 1865accfb8cSBoris Popov goto bad; 1874f93599fSBoris Popov } else 18834bed8b0SDavid Greenman type = htons(ETHERTYPE_IPX); 189cc6a66f2SJulian Elischer bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 190cc6a66f2SJulian Elischer (caddr_t)edst, sizeof (edst)); 191cc6a66f2SJulian Elischer break; 192cc6a66f2SJulian Elischer #endif 193655929bfSJulian Elischer #ifdef NETATALK 194655929bfSJulian Elischer case AF_APPLETALK: 1951d0eab59SJulian Elischer { 196ed7509acSJulian Elischer struct at_ifaddr *aa; 1971d0eab59SJulian Elischer 198ed7509acSJulian Elischer if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 1991d0eab59SJulian Elischer goto bad; 2001d0eab59SJulian Elischer } 201ed7509acSJulian Elischer if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 2021d0eab59SJulian Elischer return (0); 203655929bfSJulian Elischer /* 204ed7509acSJulian Elischer * In the phase 2 case, need to prepend an mbuf for the llc header. 205655929bfSJulian Elischer * Since we must preserve the value of m, which is passed to us by 206655929bfSJulian Elischer * value, we m_copy() the first mbuf, and use it for our llc header. 207655929bfSJulian Elischer */ 208655929bfSJulian Elischer if ( aa->aa_flags & AFA_PHASE2 ) { 209655929bfSJulian Elischer struct llc llc; 210655929bfSJulian Elischer 211797f247bSMatthew N. Dodd M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT); 2122166ffe8SRobert Watson if (m == NULL) 2132166ffe8SRobert Watson senderr(ENOBUFS); 214655929bfSJulian Elischer llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 215655929bfSJulian Elischer llc.llc_control = LLC_UI; 216655929bfSJulian Elischer bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 217655929bfSJulian Elischer llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 218797f247bSMatthew N. Dodd bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 21934bed8b0SDavid Greenman type = htons(m->m_pkthdr.len); 220797f247bSMatthew N. Dodd hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN; 221655929bfSJulian Elischer } else { 22234bed8b0SDavid Greenman type = htons(ETHERTYPE_AT); 223655929bfSJulian Elischer } 224655929bfSJulian Elischer break; 225ed7509acSJulian Elischer } 2268cdfefbdSPeter Wemm #endif /* NETATALK */ 227df8bae1dSRodney W. Grimes 228114ae644SMike Smith case pseudo_AF_HDRCMPLT: 229114ae644SMike Smith hdrcmplt = 1; 230114ae644SMike Smith eh = (struct ether_header *)dst->sa_data; 231114ae644SMike Smith (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 232114ae644SMike Smith /* FALLTHROUGH */ 233114ae644SMike Smith 234df8bae1dSRodney W. Grimes case AF_UNSPEC: 2359d3f194dSJulian Elischer loop_copy = -1; /* if this is for us, don't do it */ 236df8bae1dSRodney W. Grimes eh = (struct ether_header *)dst->sa_data; 23794a5d9b6SDavid Greenman (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 238df8bae1dSRodney W. Grimes type = eh->ether_type; 239df8bae1dSRodney W. Grimes break; 240df8bae1dSRodney W. Grimes 241df8bae1dSRodney W. Grimes default: 24229e1b85fSBrooks Davis if_printf(ifp, "can't handle af%d\n", dst->sa_family); 243df8bae1dSRodney W. Grimes senderr(EAFNOSUPPORT); 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes 246df8bae1dSRodney W. Grimes /* 247df8bae1dSRodney W. Grimes * Add local net header. If no space in first mbuf, 248df8bae1dSRodney W. Grimes * allocate another. 249df8bae1dSRodney W. Grimes */ 250797f247bSMatthew N. Dodd M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 2512166ffe8SRobert Watson if (m == NULL) 252df8bae1dSRodney W. Grimes senderr(ENOBUFS); 253df8bae1dSRodney W. Grimes eh = mtod(m, struct ether_header *); 25494a5d9b6SDavid Greenman (void)memcpy(&eh->ether_type, &type, 255df8bae1dSRodney W. Grimes sizeof(eh->ether_type)); 25694a5d9b6SDavid Greenman (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 257114ae644SMike Smith if (hdrcmplt) 258114ae644SMike Smith (void)memcpy(eh->ether_shost, esrc, 259114ae644SMike Smith sizeof(eh->ether_shost)); 260114ae644SMike Smith else 26194a5d9b6SDavid Greenman (void)memcpy(eh->ether_shost, ac->ac_enaddr, 262df8bae1dSRodney W. Grimes sizeof(eh->ether_shost)); 263ed7509acSJulian Elischer 264ed7509acSJulian Elischer /* 265ed7509acSJulian Elischer * If a simplex interface, and the packet is being sent to our 266ed7509acSJulian Elischer * Ethernet address or a broadcast address, loopback a copy. 267ed7509acSJulian Elischer * XXX To make a simplex device behave exactly like a duplex 268ed7509acSJulian Elischer * device, we should copy in the case of sending to our own 269ed7509acSJulian Elischer * ethernet address (thus letting the original actually appear 270ed7509acSJulian Elischer * on the wire). However, we don't do that here for security 271ed7509acSJulian Elischer * reasons and compatibility with the original behavior. 272ed7509acSJulian Elischer */ 2734c8e8c05SJulian Elischer if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 2743cd67511SJesper Skriver int csum_flags = 0; 2753cd67511SJesper Skriver 2763cd67511SJesper Skriver if (m->m_pkthdr.csum_flags & CSUM_IP) 2773cd67511SJesper Skriver csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 2783cd67511SJesper Skriver if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 2793cd67511SJesper Skriver csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 28006f684b0SMatthew N. Dodd 2819d3f194dSJulian Elischer if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 28206f684b0SMatthew N. Dodd struct mbuf *n; 283ed7509acSJulian Elischer 28479db6ff3SArchie Cobbs if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2853cd67511SJesper Skriver n->m_pkthdr.csum_flags |= csum_flags; 2863cd67511SJesper Skriver if (csum_flags & CSUM_DATA_VALID) 2873cd67511SJesper Skriver n->m_pkthdr.csum_data = 0xffff; 28806a429a3SArchie Cobbs (void)if_simloop(ifp, n, dst->sa_family, hlen); 289c1404dc0SArchie Cobbs } else 290c1404dc0SArchie Cobbs ifp->if_iqdrops++; 29106f684b0SMatthew N. Dodd } else if (bcmp(eh->ether_dhost, eh->ether_shost, 29206f684b0SMatthew N. Dodd ETHER_ADDR_LEN) == 0) { 2933cd67511SJesper Skriver m->m_pkthdr.csum_flags |= csum_flags; 2943cd67511SJesper Skriver if (csum_flags & CSUM_DATA_VALID) 2953cd67511SJesper Skriver m->m_pkthdr.csum_data = 0xffff; 29606a429a3SArchie Cobbs (void) if_simloop(ifp, m, dst->sa_family, hlen); 297ed7509acSJulian Elischer return (0); /* XXX */ 298ed7509acSJulian Elischer } 299ed7509acSJulian Elischer } 3002e2de7f2SArchie Cobbs 301e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 302e1e1452dSArchie Cobbs if (ng_ether_output_p != NULL) { 303e1e1452dSArchie Cobbs if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 304e1e1452dSArchie Cobbs bad: if (m != NULL) 305e1e1452dSArchie Cobbs m_freem(m); 306e1e1452dSArchie Cobbs return (error); 307e1e1452dSArchie Cobbs } 308e1e1452dSArchie Cobbs if (m == NULL) 309e1e1452dSArchie Cobbs return (0); 310e1e1452dSArchie Cobbs } 311e1e1452dSArchie Cobbs 312e1e1452dSArchie Cobbs /* Continue with link-layer output */ 313e1e1452dSArchie Cobbs return ether_output_frame(ifp, m); 314e1e1452dSArchie Cobbs } 315e1e1452dSArchie Cobbs 316e1e1452dSArchie Cobbs /* 317e1e1452dSArchie Cobbs * Ethernet link layer output routine to send a raw frame to the device. 318e1e1452dSArchie Cobbs * 319e1e1452dSArchie Cobbs * This assumes that the 14 byte Ethernet header is present and contiguous 320e1e1452dSArchie Cobbs * in the first mbuf (if BRIDGE'ing). 321e1e1452dSArchie Cobbs */ 322e1e1452dSArchie Cobbs int 323c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m) 324e1e1452dSArchie Cobbs { 3254b984093SLuigi Rizzo struct ip_fw *rule = NULL; 3262b25acc1SLuigi Rizzo 3272b25acc1SLuigi Rizzo /* Extract info from dummynet tag, ignore others */ 3282b25acc1SLuigi Rizzo for (; m->m_type == MT_TAG; m = m->m_next) 3292b25acc1SLuigi Rizzo if (m->m_flags == PACKET_TAG_DUMMYNET) 3302b25acc1SLuigi Rizzo rule = ((struct dn_pkt *)m)->rule; 3312b25acc1SLuigi Rizzo 332c1d93b05SSam Leffler if (rule == NULL && BDG_ACTIVE(ifp)) { 333c1d93b05SSam Leffler /* 334c1d93b05SSam Leffler * Beware, the bridge code notices the null rcvif and 335c1d93b05SSam Leffler * uses that identify that it's being called from 336c1d93b05SSam Leffler * ether_output as opposd to ether_input. Yech. 337c1d93b05SSam Leffler */ 338fb5fbe46SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 339c1d93b05SSam Leffler m = bdg_forward_ptr(m, ifp); 3402e2de7f2SArchie Cobbs if (m != NULL) 3412e2de7f2SArchie Cobbs m_freem(m); 3421db59ce6SEivind Eklund return (0); 343fb5fbe46SLuigi Rizzo } 3444b984093SLuigi Rizzo if (IPFW_LOADED && ether_ipfw != 0) { 345c1d93b05SSam Leffler if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) { 3464b984093SLuigi Rizzo if (m) { 3474b984093SLuigi Rizzo m_freem(m); 3484b984093SLuigi Rizzo return ENOBUFS; /* pkt dropped */ 3494b984093SLuigi Rizzo } else 3504b984093SLuigi Rizzo return 0; /* consumed e.g. in a pipe */ 3514b984093SLuigi Rizzo } 3524b984093SLuigi Rizzo } 3532b25acc1SLuigi Rizzo 354df8bae1dSRodney W. Grimes /* 355df5e1987SJonathan Lemon * Queue message on interface, update output statistics if 356df5e1987SJonathan Lemon * successful, and start output if interface not yet active. 357df8bae1dSRodney W. Grimes */ 358c1d93b05SSam Leffler return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS); 359df8bae1dSRodney W. Grimes } 360df8bae1dSRodney W. Grimes 361df8bae1dSRodney W. Grimes /* 3624b984093SLuigi Rizzo * ipfw processing for ethernet packets (in and out). 3634b984093SLuigi Rizzo * The second parameter is NULL from ether_demux, and ifp from 3644b984093SLuigi Rizzo * ether_output_frame. This section of code could be used from 3652f8ebbf4SLuigi Rizzo * bridge.c as well as long as we use some extra info 3664b984093SLuigi Rizzo * to distinguish that case from ether_output_frame(); 3674b984093SLuigi Rizzo */ 3684b984093SLuigi Rizzo int 3694b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 370c1d93b05SSam Leffler struct ip_fw **rule, int shared) 3714b984093SLuigi Rizzo { 372c1d93b05SSam Leffler struct ether_header *eh; 373c1d93b05SSam Leffler struct ether_header save_eh; 374c1d93b05SSam Leffler struct mbuf *m; 3754b984093SLuigi Rizzo int i; 3762b25acc1SLuigi Rizzo struct ip_fw_args args; 3774b984093SLuigi Rizzo 37897850a5dSLuigi Rizzo if (*rule != NULL && fw_one_pass) 379015d72e0SLuigi Rizzo return 1; /* dummynet packet, already partially processed */ 3802f8ebbf4SLuigi Rizzo 3814b984093SLuigi Rizzo /* 3822f8ebbf4SLuigi Rizzo * I need some amt of data to be contiguous, and in case others need 3834b984093SLuigi Rizzo * the packet (shared==1) also better be in the first mbuf. 3844b984093SLuigi Rizzo */ 385c1d93b05SSam Leffler m = *m0; 386c1d93b05SSam Leffler i = min( m->m_pkthdr.len, max_protohdr); 387c1d93b05SSam Leffler if ( shared || m->m_len < i) { 388c1d93b05SSam Leffler m = m_pullup(m, i); 389c1d93b05SSam Leffler if (m == NULL) { 390c1d93b05SSam Leffler *m0 = m; 3914b984093SLuigi Rizzo return 0; 3924b984093SLuigi Rizzo } 393c1d93b05SSam Leffler } 394c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 395c1d93b05SSam Leffler save_eh = *eh; /* save copy for restore below */ 396c1d93b05SSam Leffler m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 3974b984093SLuigi Rizzo 398c1d93b05SSam Leffler args.m = m; /* the packet we are looking at */ 3992b25acc1SLuigi Rizzo args.oif = dst; /* destination, if any */ 4002b25acc1SLuigi Rizzo args.divert_rule = 0; /* we do not support divert yet */ 4012b25acc1SLuigi Rizzo args.rule = *rule; /* matching rule to restart */ 4022b25acc1SLuigi Rizzo args.next_hop = NULL; /* we do not support forward yet */ 4032b25acc1SLuigi Rizzo args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 4042b25acc1SLuigi Rizzo i = ip_fw_chk_ptr(&args); 405c1d93b05SSam Leffler m = args.m; 406c1d93b05SSam Leffler if (m != NULL) { 407c1d93b05SSam Leffler /* 408c1d93b05SSam Leffler * Restore Ethernet header, as needed, in case the 409c1d93b05SSam Leffler * mbuf chain was replaced by ipfw. 410c1d93b05SSam Leffler */ 411a163d034SWarner Losh M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 412c1d93b05SSam Leffler if (m == NULL) { 413c1d93b05SSam Leffler *m0 = m; 414c1d93b05SSam Leffler return 0; 415c1d93b05SSam Leffler } 416c1d93b05SSam Leffler if (eh != mtod(m, struct ether_header *)) 417c1d93b05SSam Leffler bcopy(&save_eh, mtod(m, struct ether_header *), 418c1d93b05SSam Leffler ETHER_HDR_LEN); 419c1d93b05SSam Leffler } 420c1d93b05SSam Leffler *m0 = m; 4212b25acc1SLuigi Rizzo *rule = args.rule; 4222b25acc1SLuigi Rizzo 423c1d93b05SSam Leffler if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */ 4244b984093SLuigi Rizzo return 0; 4254b984093SLuigi Rizzo 4264b984093SLuigi Rizzo if (i == 0) /* a PASS rule. */ 4274b984093SLuigi Rizzo return 1; 4284b984093SLuigi Rizzo 4294b984093SLuigi Rizzo if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 4304b984093SLuigi Rizzo /* 4314b984093SLuigi Rizzo * Pass the pkt to dummynet, which consumes it. 4324b984093SLuigi Rizzo * If shared, make a copy and keep the original. 4334b984093SLuigi Rizzo */ 4344b984093SLuigi Rizzo if (shared) { 435a163d034SWarner Losh m = m_copypacket(m, M_DONTWAIT); 4362b25acc1SLuigi Rizzo if (m == NULL) 4374b984093SLuigi Rizzo return 0; 4384b984093SLuigi Rizzo } else { 4392b25acc1SLuigi Rizzo /* 440c1d93b05SSam Leffler * Pass the original to dummynet and 441c1d93b05SSam Leffler * nothing back to the caller 4422b25acc1SLuigi Rizzo */ 443c1d93b05SSam Leffler *m0 = NULL ; 4444b984093SLuigi Rizzo } 4452b25acc1SLuigi Rizzo ip_dn_io_ptr(m, (i & 0xffff), 4462b25acc1SLuigi Rizzo dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 4474b984093SLuigi Rizzo return 0; 4484b984093SLuigi Rizzo } 4494b984093SLuigi Rizzo /* 4502f8ebbf4SLuigi Rizzo * XXX at some point add support for divert/forward actions. 4512f8ebbf4SLuigi Rizzo * If none of the above matches, we have to drop the pkt. 4524b984093SLuigi Rizzo */ 4534b984093SLuigi Rizzo return 0; 4544b984093SLuigi Rizzo } 4554b984093SLuigi Rizzo 4564b984093SLuigi Rizzo /* 457c1d93b05SSam Leffler * Process a received Ethernet packet; the packet is in the 458c1d93b05SSam Leffler * mbuf chain m with the ethernet header at the front. 459df8bae1dSRodney W. Grimes */ 460c1d93b05SSam Leffler static void 461c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m) 462df8bae1dSRodney W. Grimes { 463c1d93b05SSam Leffler struct ether_header *eh; 464c1d93b05SSam Leffler u_short etype; 465df8bae1dSRodney W. Grimes 466c1d93b05SSam Leffler /* 467c1d93b05SSam Leffler * Do consistency checks to verify assumptions 468c1d93b05SSam Leffler * made by code past this point. 469c1d93b05SSam Leffler */ 470c1d93b05SSam Leffler if ((m->m_flags & M_PKTHDR) == 0) { 471c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o packet header\n"); 472c1d93b05SSam Leffler ifp->if_ierrors++; 473c939f1aeSLuigi Rizzo m_freem(m); 474c939f1aeSLuigi Rizzo return; 475c939f1aeSLuigi Rizzo } 476797f247bSMatthew N. Dodd if (m->m_len < ETHER_HDR_LEN) { 477c1d93b05SSam Leffler /* XXX maybe should pullup? */ 478c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o leading ethernet " 479c1d93b05SSam Leffler "header (len %u pkt len %u)\n", 480c1d93b05SSam Leffler m->m_len, m->m_pkthdr.len); 481c1d93b05SSam Leffler ifp->if_ierrors++; 482c1d93b05SSam Leffler m_freem(m); 483c1d93b05SSam Leffler return; 4842e2de7f2SArchie Cobbs } 485c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 486c1d93b05SSam Leffler etype = ntohs(eh->ether_type); 487c1d93b05SSam Leffler if (m->m_pkthdr.len > 488c1d93b05SSam Leffler ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 489c1d93b05SSam Leffler if_printf(ifp, "discard oversize frame " 490c1d93b05SSam Leffler "(ether type %x flags %x len %u > max %lu)\n", 491c1d93b05SSam Leffler etype, m->m_flags, m->m_pkthdr.len, 492c1d93b05SSam Leffler ETHER_MAX_FRAME(ifp, etype, 493c1d93b05SSam Leffler m->m_flags & M_HASFCS)); 494c1d93b05SSam Leffler ifp->if_ierrors++; 495c1d93b05SSam Leffler m_freem(m); 496c1d93b05SSam Leffler return; 497c1d93b05SSam Leffler } 498c1d93b05SSam Leffler if (m->m_pkthdr.rcvif == NULL) { 499c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o interface pointer\n"); 500c1d93b05SSam Leffler ifp->if_ierrors++; 501c1d93b05SSam Leffler m_freem(m); 502c1d93b05SSam Leffler return; 503c1d93b05SSam Leffler } 504c1d93b05SSam Leffler #ifdef DIAGNOSTIC 505c1d93b05SSam Leffler if (m->m_pkthdr.rcvif != ifp) { 5069bf40edeSBrooks Davis if_printf(ifp, "Warning, frame marked as received on %s\n", 5079bf40edeSBrooks Davis m->m_pkthdr.rcvif->if_xname); 508c1d93b05SSam Leffler } 509c1d93b05SSam Leffler #endif 510c1d93b05SSam Leffler 511533d8562SRobert Watson #ifdef MAC 512533d8562SRobert Watson /* 513533d8562SRobert Watson * Tag the mbuf with an appropriate MAC label before any other 514533d8562SRobert Watson * consumers can get to it. 515533d8562SRobert Watson */ 516533d8562SRobert Watson mac_create_mbuf_from_ifnet(ifp, m); 517533d8562SRobert Watson #endif 518533d8562SRobert Watson 519c1d93b05SSam Leffler /* 520c1d93b05SSam Leffler * Give bpf a chance at the packet. 521c1d93b05SSam Leffler */ 522c1d93b05SSam Leffler BPF_MTAP(ifp, m); 5232e2de7f2SArchie Cobbs 524afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) { 525c1d93b05SSam Leffler /* 526c1d93b05SSam Leffler * Interface marked for monitoring; discard packet. 527c1d93b05SSam Leffler */ 528afbe3a0fSPoul-Henning Kamp m_freem(m); 529afbe3a0fSPoul-Henning Kamp return; 530afbe3a0fSPoul-Henning Kamp } 531afbe3a0fSPoul-Henning Kamp 532c1d93b05SSam Leffler /* If the CRC is still on the packet, trim it off. */ 533c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 534c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 535c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 536c1d93b05SSam Leffler } 537c1d93b05SSam Leffler 538c1d93b05SSam Leffler ifp->if_ibytes += m->m_pkthdr.len; 53905463bb5SDavid Greenman 540e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 541e1e1452dSArchie Cobbs if (ng_ether_input_p != NULL) { 542c1d93b05SSam Leffler (*ng_ether_input_p)(ifp, &m); 543e1e1452dSArchie Cobbs if (m == NULL) 544e1e1452dSArchie Cobbs return; 545e1e1452dSArchie Cobbs } 546e1e1452dSArchie Cobbs 5472e2de7f2SArchie Cobbs /* Check for bridging mode */ 5487b109fa4SLuigi Rizzo if (BDG_ACTIVE(ifp) ) { 5492e2de7f2SArchie Cobbs struct ifnet *bif; 5502e2de7f2SArchie Cobbs 551c1d93b05SSam Leffler /* 552c1d93b05SSam Leffler * Check with bridging code to see how the packet 553c1d93b05SSam Leffler * should be handled. Possibilities are: 554c1d93b05SSam Leffler * 555c1d93b05SSam Leffler * BDG_BCAST broadcast 556c1d93b05SSam Leffler * BDG_MCAST multicast 557c1d93b05SSam Leffler * BDG_LOCAL for local address, don't forward 558c1d93b05SSam Leffler * BDG_DROP discard 559c1d93b05SSam Leffler * ifp forward only to specified interface(s) 560c1d93b05SSam Leffler * 561c1d93b05SSam Leffler * Non-local destinations are handled by passing the 562c1d93b05SSam Leffler * packet back to the bridge code. 563c1d93b05SSam Leffler */ 564c1d93b05SSam Leffler bif = bridge_in_ptr(ifp, eh); 565c1d93b05SSam Leffler if (bif == BDG_DROP) { /* discard packet */ 5662e2de7f2SArchie Cobbs m_freem(m); 5672e2de7f2SArchie Cobbs return; 5682e2de7f2SArchie Cobbs } 569c1d93b05SSam Leffler if (bif != BDG_LOCAL) { /* non-local, forward */ 570c1d93b05SSam Leffler m = bdg_forward_ptr(m, bif); 571ddacb30fSBosko Milekic /* 572c1d93b05SSam Leffler * The bridge may consume the packet if it's not 573c1d93b05SSam Leffler * supposed to be passed up or if a problem occurred 574c1d93b05SSam Leffler * while doing its job. This is reflected by it 575c1d93b05SSam Leffler * returning a NULL mbuf pointer. 576ddacb30fSBosko Milekic */ 577507b4b54SLuigi Rizzo if (m == NULL) { 578507b4b54SLuigi Rizzo if (bif == BDG_BCAST || bif == BDG_MCAST) 579c1d93b05SSam Leffler if_printf(ifp, 580c1d93b05SSam Leffler "bridge dropped %s packet\n", 581c1d93b05SSam Leffler bif == BDG_BCAST ? "broadcast" : 582c1d93b05SSam Leffler "multicast"); 583ddacb30fSBosko Milekic return; 584ddacb30fSBosko Milekic } 585c1d93b05SSam Leffler /* 586c1d93b05SSam Leffler * But in some cases the bridge may return the 587c1d93b05SSam Leffler * packet for us to free; sigh. 588c1d93b05SSam Leffler */ 589c1d93b05SSam Leffler if (bif != BDG_BCAST && bif != BDG_MCAST) { 5902e2de7f2SArchie Cobbs m_freem(m); 5912e2de7f2SArchie Cobbs return; 5922e2de7f2SArchie Cobbs } 593c1d93b05SSam Leffler } 594c1d93b05SSam Leffler } 5952e2de7f2SArchie Cobbs 596c1d93b05SSam Leffler ether_demux(ifp, m); 597a6ddbff0SPoul-Henning Kamp /* First chunk of an mbuf contains good entropy */ 59810b1fde0SMark Murray if (harvest.ethernet) 59910b1fde0SMark Murray random_harvest(m, 16, 3, 0, RANDOM_NET); 600e1e1452dSArchie Cobbs } 601e1e1452dSArchie Cobbs 602e1e1452dSArchie Cobbs /* 603e1e1452dSArchie Cobbs * Upper layer processing for a received Ethernet packet. 604e1e1452dSArchie Cobbs */ 605e1e1452dSArchie Cobbs void 606c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m) 607e1e1452dSArchie Cobbs { 608c1d93b05SSam Leffler struct ether_header *eh; 6091cafed39SJonathan Lemon int isr; 610e1e1452dSArchie Cobbs u_short ether_type; 611e1e1452dSArchie Cobbs #if defined(NETATALK) 612098a8c3bSMatthew N. Dodd struct llc *l; 613e1e1452dSArchie Cobbs #endif 6144b984093SLuigi Rizzo struct ip_fw *rule = NULL; 6152b25acc1SLuigi Rizzo 6162b25acc1SLuigi Rizzo /* Extract info from dummynet tag, ignore others */ 6172b25acc1SLuigi Rizzo for (;m->m_type == MT_TAG; m = m->m_next) 6182b25acc1SLuigi Rizzo if (m->m_flags == PACKET_TAG_DUMMYNET) { 6192b25acc1SLuigi Rizzo rule = ((struct dn_pkt *)m)->rule; 6202b25acc1SLuigi Rizzo ifp = m->m_next->m_pkthdr.rcvif; 6214b984093SLuigi Rizzo } 6222b25acc1SLuigi Rizzo 623c1d93b05SSam Leffler KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer")); 624c1d93b05SSam Leffler 625c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 626c1d93b05SSam Leffler 6272b25acc1SLuigi Rizzo if (rule) /* packet was already bridged */ 6282b25acc1SLuigi Rizzo goto post_stats; 6294b984093SLuigi Rizzo 630c6943100SPoul-Henning Kamp if (!(BDG_ACTIVE(ifp))) { 631c6943100SPoul-Henning Kamp /* 632c6943100SPoul-Henning Kamp * Discard packet if upper layers shouldn't see it because it 633c6943100SPoul-Henning Kamp * was unicast to a different Ethernet address. If the driver 634c6943100SPoul-Henning Kamp * is working properly, then this situation can only happen 635c6943100SPoul-Henning Kamp * when the interface is in promiscuous mode. 636c6943100SPoul-Henning Kamp */ 637cb24f323SArchie Cobbs if ((ifp->if_flags & IFF_PROMISC) != 0 638cb24f323SArchie Cobbs && (eh->ether_dhost[0] & 1) == 0 639cb24f323SArchie Cobbs && bcmp(eh->ether_dhost, 640ffb079beSMaxim Sobolev IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0 64125faf49dSMaxim Sobolev && (ifp->if_flags & IFF_PPROMISC) == 0) { 642cb24f323SArchie Cobbs m_freem(m); 643cb24f323SArchie Cobbs return; 644cb24f323SArchie Cobbs } 645c6943100SPoul-Henning Kamp } 646cb24f323SArchie Cobbs 6472e2de7f2SArchie Cobbs /* Discard packet if interface is not up */ 648df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_UP) == 0) { 649df8bae1dSRodney W. Grimes m_freem(m); 650df8bae1dSRodney W. Grimes return; 651df8bae1dSRodney W. Grimes } 652588523faSSam Leffler if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 65364760eb0SWarner Losh if (bcmp(etherbroadcastaddr, eh->ether_dhost, 654df8bae1dSRodney W. Grimes sizeof(etherbroadcastaddr)) == 0) 655df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 6562cc2df49SGarrett Wollman else 657df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 6582cc2df49SGarrett Wollman } 659df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST|M_MCAST)) 660df8bae1dSRodney W. Grimes ifp->if_imcasts++; 661df8bae1dSRodney W. Grimes 6624b984093SLuigi Rizzo post_stats: 6634b984093SLuigi Rizzo if (IPFW_LOADED && ether_ipfw != 0) { 664c1d93b05SSam Leffler if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) { 6654b984093SLuigi Rizzo if (m) 6664b984093SLuigi Rizzo m_freem(m); 6674b984093SLuigi Rizzo return; 6684b984093SLuigi Rizzo } 6694b984093SLuigi Rizzo } 6704b984093SLuigi Rizzo 671c1d93b05SSam Leffler /* 672c1d93b05SSam Leffler * If VLANs are configured on the interface, check to 673c1d93b05SSam Leffler * see if the device performed the decapsulation and 674c1d93b05SSam Leffler * provided us with the tag. 675c1d93b05SSam Leffler */ 676c1d93b05SSam Leffler if (ifp->if_nvlans && 677c1d93b05SSam Leffler m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) { 678c1d93b05SSam Leffler /* 679c1d93b05SSam Leffler * vlan_input() will either recursively call ether_input() 680c1d93b05SSam Leffler * or drop the packet. 681c1d93b05SSam Leffler */ 682c1d93b05SSam Leffler KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!")); 683c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 684c1d93b05SSam Leffler return; 685c1d93b05SSam Leffler } 686c1d93b05SSam Leffler 687307d80beSDavid Greenman ether_type = ntohs(eh->ether_type); 688307d80beSDavid Greenman 689c1d93b05SSam Leffler /* 690c1d93b05SSam Leffler * Handle protocols that expect to have the Ethernet header 691c1d93b05SSam Leffler * (and possibly FCS) intact. 692c1d93b05SSam Leffler */ 693c1d93b05SSam Leffler switch (ether_type) { 694c1d93b05SSam Leffler case ETHERTYPE_VLAN: 695c1d93b05SSam Leffler if (ifp->if_nvlans != 0) { 696c1d93b05SSam Leffler KASSERT(vlan_input_p,("ether_input: VLAN not loaded!")); 697c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 698c1d93b05SSam Leffler } else { 699c1d93b05SSam Leffler ifp->if_noproto++; 700c1d93b05SSam Leffler m_freem(m); 701c1d93b05SSam Leffler } 702c1d93b05SSam Leffler return; 703c1d93b05SSam Leffler } 704c1d93b05SSam Leffler 705c1d93b05SSam Leffler /* Strip off Ethernet header. */ 706797f247bSMatthew N. Dodd m_adj(m, ETHER_HDR_LEN); 707c1d93b05SSam Leffler 708c1d93b05SSam Leffler /* If the CRC is still on the packet, trim it off. */ 709c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 710c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 711c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 712c1d93b05SSam Leffler } 713c1d93b05SSam Leffler 714307d80beSDavid Greenman switch (ether_type) { 715df8bae1dSRodney W. Grimes #ifdef INET 716df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 7171f91d8c5SDavid Greenman if (ipflow_fastforward(m)) 7181f91d8c5SDavid Greenman return; 7191cafed39SJonathan Lemon isr = NETISR_IP; 720df8bae1dSRodney W. Grimes break; 721df8bae1dSRodney W. Grimes 722df8bae1dSRodney W. Grimes case ETHERTYPE_ARP: 72308aadfbbSJonathan Lemon if (ifp->if_flags & IFF_NOARP) { 72408aadfbbSJonathan Lemon /* Discard packet if ARP is disabled on interface */ 72508aadfbbSJonathan Lemon m_freem(m); 72608aadfbbSJonathan Lemon return; 72708aadfbbSJonathan Lemon } 7281cafed39SJonathan Lemon isr = NETISR_ARP; 729df8bae1dSRodney W. Grimes break; 730df8bae1dSRodney W. Grimes #endif 731cc6a66f2SJulian Elischer #ifdef IPX 732cc6a66f2SJulian Elischer case ETHERTYPE_IPX: 7334f93599fSBoris Popov if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 7344f93599fSBoris Popov return; 7351cafed39SJonathan Lemon isr = NETISR_IPX; 736cc6a66f2SJulian Elischer break; 737cc6a66f2SJulian Elischer #endif 73882cd038dSYoshinobu Inoue #ifdef INET6 73982cd038dSYoshinobu Inoue case ETHERTYPE_IPV6: 7401cafed39SJonathan Lemon isr = NETISR_IPV6; 74182cd038dSYoshinobu Inoue break; 74282cd038dSYoshinobu Inoue #endif 743655929bfSJulian Elischer #ifdef NETATALK 744655929bfSJulian Elischer case ETHERTYPE_AT: 7451cafed39SJonathan Lemon isr = NETISR_ATALK1; 746655929bfSJulian Elischer break; 747655929bfSJulian Elischer case ETHERTYPE_AARP: 7481cafed39SJonathan Lemon isr = NETISR_AARP; 7491cafed39SJonathan Lemon break; 7508cdfefbdSPeter Wemm #endif /* NETATALK */ 751df8bae1dSRodney W. Grimes default: 7524f93599fSBoris Popov #ifdef IPX 7534f93599fSBoris Popov if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 7544f93599fSBoris Popov return; 7554f93599fSBoris Popov #endif /* IPX */ 756242c5536SPeter Wemm #if defined(NETATALK) 757307d80beSDavid Greenman if (ether_type > ETHERMTU) 758c1d93b05SSam Leffler goto discard; 759df8bae1dSRodney W. Grimes l = mtod(m, struct llc *); 7601cafed39SJonathan Lemon if (l->llc_dsap == LLC_SNAP_LSAP && 7611cafed39SJonathan Lemon l->llc_ssap == LLC_SNAP_LSAP && 7621cafed39SJonathan Lemon l->llc_control == LLC_UI) { 763655929bfSJulian Elischer if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 764655929bfSJulian Elischer sizeof(at_org_code)) == 0 && 765655929bfSJulian Elischer ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 766797f247bSMatthew N. Dodd m_adj(m, LLC_SNAPFRAMELEN); 7671cafed39SJonathan Lemon isr = NETISR_ATALK2; 768655929bfSJulian Elischer break; 769655929bfSJulian Elischer } 770655929bfSJulian Elischer if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 771655929bfSJulian Elischer sizeof(aarp_org_code)) == 0 && 772655929bfSJulian Elischer ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 773797f247bSMatthew N. Dodd m_adj(m, LLC_SNAPFRAMELEN); 7741cafed39SJonathan Lemon isr = NETISR_AARP; 775655929bfSJulian Elischer break; 776df8bae1dSRodney W. Grimes } 7771cafed39SJonathan Lemon } 778242c5536SPeter Wemm #endif /* NETATALK */ 7791cafed39SJonathan Lemon goto discard; 780df8bae1dSRodney W. Grimes } 7811cafed39SJonathan Lemon netisr_dispatch(isr, m); 782c1d93b05SSam Leffler return; 7831cafed39SJonathan Lemon 784c1d93b05SSam Leffler discard: 785c1d93b05SSam Leffler /* 786c1d93b05SSam Leffler * Packet is to be discarded. If netgraph is present, 787c1d93b05SSam Leffler * hand the packet to it for last chance processing; 788c1d93b05SSam Leffler * otherwise dispose of it. 789c1d93b05SSam Leffler */ 790c1d93b05SSam Leffler if (ng_ether_input_orphan_p != NULL) { 791c1d93b05SSam Leffler /* 792c1d93b05SSam Leffler * Put back the ethernet header so netgraph has a 793c1d93b05SSam Leffler * consistent view of inbound packets. 794c1d93b05SSam Leffler */ 795797f247bSMatthew N. Dodd M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 796c1d93b05SSam Leffler (*ng_ether_input_orphan_p)(ifp, m); 797c1d93b05SSam Leffler return; 798c1d93b05SSam Leffler } 799c1d93b05SSam Leffler m_freem(m); 800c1d93b05SSam Leffler } 801c1d93b05SSam Leffler 802c1d93b05SSam Leffler /* 803c1d93b05SSam Leffler * Convert Ethernet address to printable (loggable) representation. 804c1d93b05SSam Leffler * This routine is for compatibility; it's better to just use 805c1d93b05SSam Leffler * 806c1d93b05SSam Leffler * printf("%6D", <pointer to address>, ":"); 807c1d93b05SSam Leffler * 808c1d93b05SSam Leffler * since there's no static buffer involved. 809c1d93b05SSam Leffler */ 810c1d93b05SSam Leffler char * 811c1d93b05SSam Leffler ether_sprintf(const u_char *ap) 812c1d93b05SSam Leffler { 813c1d93b05SSam Leffler static char etherbuf[18]; 814c1d93b05SSam Leffler snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 815c1d93b05SSam Leffler return (etherbuf); 816df8bae1dSRodney W. Grimes } 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes /* 819df8bae1dSRodney W. Grimes * Perform common duties while attaching to interface list 820df8bae1dSRodney W. Grimes */ 821df8bae1dSRodney W. Grimes void 822c1d93b05SSam Leffler ether_ifattach(struct ifnet *ifp, const u_int8_t *llc) 823df8bae1dSRodney W. Grimes { 824098a8c3bSMatthew N. Dodd struct ifaddr *ifa; 825098a8c3bSMatthew N. Dodd struct sockaddr_dl *sdl; 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes ifp->if_type = IFT_ETHER; 828c1d93b05SSam Leffler ifp->if_addrlen = ETHER_ADDR_LEN; 829c1d93b05SSam Leffler ifp->if_hdrlen = ETHER_HDR_LEN; 830cfeff1b6SJonathan Lemon if_attach(ifp); 831df8bae1dSRodney W. Grimes ifp->if_mtu = ETHERMTU; 832c1d93b05SSam Leffler ifp->if_output = ether_output; 833c1d93b05SSam Leffler ifp->if_input = ether_input; 8341158dfb7SGarrett Wollman ifp->if_resolvemulti = ether_resolvemulti; 835a330e1f1SGary Palmer if (ifp->if_baudrate == 0) 836c1d93b05SSam Leffler ifp->if_baudrate = IF_Mbps(10); /* just a default */ 837322dcb8dSMax Khon ifp->if_broadcastaddr = etherbroadcastaddr; 838c1d93b05SSam Leffler 839f9132cebSJonathan Lemon ifa = ifaddr_byindex(ifp->if_index); 8406e551fb6SDavid E. O'Brien KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 84159562606SGarrett Wollman sdl = (struct sockaddr_dl *)ifa->ifa_addr; 842df8bae1dSRodney W. Grimes sdl->sdl_type = IFT_ETHER; 843df8bae1dSRodney W. Grimes sdl->sdl_alen = ifp->if_addrlen; 844c1d93b05SSam Leffler bcopy(llc, LLADDR(sdl), ifp->if_addrlen); 845c1d93b05SSam Leffler /* 846c1d93b05SSam Leffler * XXX: This doesn't belong here; we do it until 847c1d93b05SSam Leffler * XXX: all drivers are cleaned up 848c1d93b05SSam Leffler */ 849c1d93b05SSam Leffler if (llc != IFP2AC(ifp)->ac_enaddr) 850c1d93b05SSam Leffler bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 851c1d93b05SSam Leffler 852797f247bSMatthew N. Dodd bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 853e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) 854e1e1452dSArchie Cobbs (*ng_ether_attach_p)(ifp); 8557b109fa4SLuigi Rizzo if (BDG_LOADED) 856db69a05dSPaul Saab bdgtakeifaces_ptr(); 857e1e1452dSArchie Cobbs } 858e1e1452dSArchie Cobbs 85921b8ebd9SArchie Cobbs /* 86021b8ebd9SArchie Cobbs * Perform common duties while detaching an Ethernet interface 86121b8ebd9SArchie Cobbs */ 86221b8ebd9SArchie Cobbs void 863c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp) 86421b8ebd9SArchie Cobbs { 86521b8ebd9SArchie Cobbs if (ng_ether_detach_p != NULL) 86621b8ebd9SArchie Cobbs (*ng_ether_detach_p)(ifp); 86721b8ebd9SArchie Cobbs bpfdetach(ifp); 86821b8ebd9SArchie Cobbs if_detach(ifp); 8697b109fa4SLuigi Rizzo if (BDG_LOADED) 870db69a05dSPaul Saab bdgtakeifaces_ptr(); 87121b8ebd9SArchie Cobbs } 87221b8ebd9SArchie Cobbs 873ce02431fSDoug Rabson SYSCTL_DECL(_net_link); 874602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 8754b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 8764b984093SLuigi Rizzo ðer_ipfw,0,"Pass ether pkts through firewall"); 87730106f6aSPoul-Henning Kamp 878fb583156SDavid Greenman int 87972fd1b6aSDag-Erling Smørgrav ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 88030106f6aSPoul-Henning Kamp { 88130106f6aSPoul-Henning Kamp struct ifaddr *ifa = (struct ifaddr *) data; 88230106f6aSPoul-Henning Kamp struct ifreq *ifr = (struct ifreq *) data; 883fb583156SDavid Greenman int error = 0; 88430106f6aSPoul-Henning Kamp 88530106f6aSPoul-Henning Kamp switch (command) { 88630106f6aSPoul-Henning Kamp case SIOCSIFADDR: 88730106f6aSPoul-Henning Kamp ifp->if_flags |= IFF_UP; 88830106f6aSPoul-Henning Kamp 88930106f6aSPoul-Henning Kamp switch (ifa->ifa_addr->sa_family) { 89030106f6aSPoul-Henning Kamp #ifdef INET 89130106f6aSPoul-Henning Kamp case AF_INET: 89230106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); /* before arpwhohas */ 893322dcb8dSMax Khon arp_ifinit(ifp, ifa); 89430106f6aSPoul-Henning Kamp break; 89530106f6aSPoul-Henning Kamp #endif 89630106f6aSPoul-Henning Kamp #ifdef IPX 89730106f6aSPoul-Henning Kamp /* 89830106f6aSPoul-Henning Kamp * XXX - This code is probably wrong 89930106f6aSPoul-Henning Kamp */ 90030106f6aSPoul-Henning Kamp case AF_IPX: 90130106f6aSPoul-Henning Kamp { 902098a8c3bSMatthew N. Dodd struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 9034c8e8c05SJulian Elischer struct arpcom *ac = IFP2AC(ifp); 90430106f6aSPoul-Henning Kamp 90530106f6aSPoul-Henning Kamp if (ipx_nullhost(*ina)) 90630106f6aSPoul-Henning Kamp ina->x_host = 90786101139SPoul-Henning Kamp *(union ipx_host *) 90886101139SPoul-Henning Kamp ac->ac_enaddr; 90930106f6aSPoul-Henning Kamp else { 91030106f6aSPoul-Henning Kamp bcopy((caddr_t) ina->x_host.c_host, 91186101139SPoul-Henning Kamp (caddr_t) ac->ac_enaddr, 91286101139SPoul-Henning Kamp sizeof(ac->ac_enaddr)); 91330106f6aSPoul-Henning Kamp } 91430106f6aSPoul-Henning Kamp 91530106f6aSPoul-Henning Kamp /* 91630106f6aSPoul-Henning Kamp * Set new address 91730106f6aSPoul-Henning Kamp */ 91830106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 91930106f6aSPoul-Henning Kamp break; 92030106f6aSPoul-Henning Kamp } 92130106f6aSPoul-Henning Kamp #endif 92230106f6aSPoul-Henning Kamp default: 92330106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 92430106f6aSPoul-Henning Kamp break; 92530106f6aSPoul-Henning Kamp } 92630106f6aSPoul-Henning Kamp break; 92730106f6aSPoul-Henning Kamp 92830106f6aSPoul-Henning Kamp case SIOCGIFADDR: 92930106f6aSPoul-Henning Kamp { 93030106f6aSPoul-Henning Kamp struct sockaddr *sa; 93130106f6aSPoul-Henning Kamp 93230106f6aSPoul-Henning Kamp sa = (struct sockaddr *) & ifr->ifr_data; 9334c8e8c05SJulian Elischer bcopy(IFP2AC(ifp)->ac_enaddr, 93430106f6aSPoul-Henning Kamp (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 93530106f6aSPoul-Henning Kamp } 93630106f6aSPoul-Henning Kamp break; 937fb583156SDavid Greenman 938fb583156SDavid Greenman case SIOCSIFMTU: 939fb583156SDavid Greenman /* 940fb583156SDavid Greenman * Set the interface MTU. 941fb583156SDavid Greenman */ 942fb583156SDavid Greenman if (ifr->ifr_mtu > ETHERMTU) { 943fb583156SDavid Greenman error = EINVAL; 944fb583156SDavid Greenman } else { 945fb583156SDavid Greenman ifp->if_mtu = ifr->ifr_mtu; 94630106f6aSPoul-Henning Kamp } 947fb583156SDavid Greenman break; 948c1d93b05SSam Leffler default: 949c1d93b05SSam Leffler error = EINVAL; /* XXX netbsd has ENOTTY??? */ 950c1d93b05SSam Leffler break; 951fb583156SDavid Greenman } 952fb583156SDavid Greenman return (error); 95330106f6aSPoul-Henning Kamp } 9541158dfb7SGarrett Wollman 95537c84183SPoul-Henning Kamp static int 95672fd1b6aSDag-Erling Smørgrav ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 95772fd1b6aSDag-Erling Smørgrav struct sockaddr *sa) 9581158dfb7SGarrett Wollman { 9591158dfb7SGarrett Wollman struct sockaddr_dl *sdl; 9601158dfb7SGarrett Wollman struct sockaddr_in *sin; 96182cd038dSYoshinobu Inoue #ifdef INET6 96282cd038dSYoshinobu Inoue struct sockaddr_in6 *sin6; 96382cd038dSYoshinobu Inoue #endif 9641158dfb7SGarrett Wollman u_char *e_addr; 9651158dfb7SGarrett Wollman 9661158dfb7SGarrett Wollman switch(sa->sa_family) { 9671158dfb7SGarrett Wollman case AF_LINK: 9687f33a738SJulian Elischer /* 9697f33a738SJulian Elischer * No mapping needed. Just check that it's a valid MC address. 9707f33a738SJulian Elischer */ 9711158dfb7SGarrett Wollman sdl = (struct sockaddr_dl *)sa; 9721158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 9731158dfb7SGarrett Wollman if ((e_addr[0] & 1) != 1) 9741158dfb7SGarrett Wollman return EADDRNOTAVAIL; 9751158dfb7SGarrett Wollman *llsa = 0; 9761158dfb7SGarrett Wollman return 0; 9771158dfb7SGarrett Wollman 9781158dfb7SGarrett Wollman #ifdef INET 9791158dfb7SGarrett Wollman case AF_INET: 9801158dfb7SGarrett Wollman sin = (struct sockaddr_in *)sa; 9811158dfb7SGarrett Wollman if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 9821158dfb7SGarrett Wollman return EADDRNOTAVAIL; 9831158dfb7SGarrett Wollman MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 984a163d034SWarner Losh M_WAITOK|M_ZERO); 9851158dfb7SGarrett Wollman sdl->sdl_len = sizeof *sdl; 9861158dfb7SGarrett Wollman sdl->sdl_family = AF_LINK; 9871158dfb7SGarrett Wollman sdl->sdl_index = ifp->if_index; 9881158dfb7SGarrett Wollman sdl->sdl_type = IFT_ETHER; 9891158dfb7SGarrett Wollman sdl->sdl_alen = ETHER_ADDR_LEN; 9901158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 9911158dfb7SGarrett Wollman ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 9921158dfb7SGarrett Wollman *llsa = (struct sockaddr *)sdl; 9931158dfb7SGarrett Wollman return 0; 9941158dfb7SGarrett Wollman #endif 99582cd038dSYoshinobu Inoue #ifdef INET6 99682cd038dSYoshinobu Inoue case AF_INET6: 99782cd038dSYoshinobu Inoue sin6 = (struct sockaddr_in6 *)sa; 998595b8a1cSJun-ichiro itojun Hagino if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 999595b8a1cSJun-ichiro itojun Hagino /* 1000595b8a1cSJun-ichiro itojun Hagino * An IP6 address of 0 means listen to all 1001595b8a1cSJun-ichiro itojun Hagino * of the Ethernet multicast address used for IP6. 1002595b8a1cSJun-ichiro itojun Hagino * (This is used for multicast routers.) 1003595b8a1cSJun-ichiro itojun Hagino */ 1004595b8a1cSJun-ichiro itojun Hagino ifp->if_flags |= IFF_ALLMULTI; 1005595b8a1cSJun-ichiro itojun Hagino *llsa = 0; 1006595b8a1cSJun-ichiro itojun Hagino return 0; 1007595b8a1cSJun-ichiro itojun Hagino } 100882cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 100982cd038dSYoshinobu Inoue return EADDRNOTAVAIL; 101082cd038dSYoshinobu Inoue MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1011a163d034SWarner Losh M_WAITOK|M_ZERO); 101282cd038dSYoshinobu Inoue sdl->sdl_len = sizeof *sdl; 101382cd038dSYoshinobu Inoue sdl->sdl_family = AF_LINK; 101482cd038dSYoshinobu Inoue sdl->sdl_index = ifp->if_index; 101582cd038dSYoshinobu Inoue sdl->sdl_type = IFT_ETHER; 101682cd038dSYoshinobu Inoue sdl->sdl_alen = ETHER_ADDR_LEN; 101782cd038dSYoshinobu Inoue e_addr = LLADDR(sdl); 101882cd038dSYoshinobu Inoue ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 101982cd038dSYoshinobu Inoue *llsa = (struct sockaddr *)sdl; 102082cd038dSYoshinobu Inoue return 0; 102182cd038dSYoshinobu Inoue #endif 10221158dfb7SGarrett Wollman 10231158dfb7SGarrett Wollman default: 10241158dfb7SGarrett Wollman /* 10251158dfb7SGarrett Wollman * Well, the text isn't quite right, but it's the name 10261158dfb7SGarrett Wollman * that counts... 10271158dfb7SGarrett Wollman */ 10281158dfb7SGarrett Wollman return EAFNOSUPPORT; 10291158dfb7SGarrett Wollman } 10301158dfb7SGarrett Wollman } 103140811c14SMatthew N. Dodd 103240811c14SMatthew N. Dodd static moduledata_t ether_mod = { 103340811c14SMatthew N. Dodd "ether", 103440811c14SMatthew N. Dodd NULL, 103540811c14SMatthew N. Dodd 0 103640811c14SMatthew N. Dodd }; 103740811c14SMatthew N. Dodd 103840811c14SMatthew N. Dodd DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 103940811c14SMatthew N. Dodd MODULE_VERSION(ether, 1); 1040