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 136d25f3712SBrian Feldman ether_output(ifp, m, dst, rt0) 137098a8c3bSMatthew N. Dodd struct ifnet *ifp; 138d25f3712SBrian Feldman struct mbuf *m; 139df8bae1dSRodney W. Grimes struct sockaddr *dst; 140df8bae1dSRodney W. Grimes struct rtentry *rt0; 141df8bae1dSRodney W. Grimes { 142df8bae1dSRodney W. Grimes short type; 143e1e1452dSArchie Cobbs int error = 0, hdrcmplt = 0; 144114ae644SMike Smith u_char esrc[6], edst[6]; 145098a8c3bSMatthew N. Dodd struct rtentry *rt; 146098a8c3bSMatthew N. Dodd struct ether_header *eh; 1479961e27dSLuigi Rizzo int loop_copy = 0; 14884dd0fd0SJulian Elischer int hlen; /* link layer header lenght */ 1494c8e8c05SJulian Elischer struct arpcom *ac = IFP2AC(ifp); 150df8bae1dSRodney W. Grimes 15143b29369SRobert Watson #ifdef MAC 15243b29369SRobert Watson error = mac_check_ifnet_transmit(ifp, m); 15343b29369SRobert Watson if (error) 15443b29369SRobert Watson senderr(error); 15543b29369SRobert Watson #endif 15643b29369SRobert Watson 157afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) 158afbe3a0fSPoul-Henning Kamp senderr(ENETDOWN); 159df8bae1dSRodney W. Grimes if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 160df8bae1dSRodney W. Grimes senderr(ENETDOWN); 1617f760c48SMatthew N. Dodd 1627f760c48SMatthew N. Dodd error = rt_check(&rt, &rt0, dst); 1637f760c48SMatthew N. Dodd if (error) 1647f760c48SMatthew N. Dodd goto bad; 1657f760c48SMatthew N. Dodd 16684dd0fd0SJulian Elischer hlen = ETHER_HDR_LEN; 167df8bae1dSRodney W. Grimes switch (dst->sa_family) { 168df8bae1dSRodney W. Grimes #ifdef INET 169df8bae1dSRodney W. Grimes case AF_INET: 170322dcb8dSMax Khon if (!arpresolve(ifp, rt, m, dst, edst, rt0)) 171df8bae1dSRodney W. Grimes return (0); /* if not yet resolved */ 17234bed8b0SDavid Greenman type = htons(ETHERTYPE_IP); 173df8bae1dSRodney W. Grimes break; 174df8bae1dSRodney W. Grimes #endif 17582cd038dSYoshinobu Inoue #ifdef INET6 17682cd038dSYoshinobu Inoue case AF_INET6: 17782cd038dSYoshinobu Inoue if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 178fef5fd23SBosko Milekic /* Something bad happened */ 17982cd038dSYoshinobu Inoue return(0); 18082cd038dSYoshinobu Inoue } 18182cd038dSYoshinobu Inoue type = htons(ETHERTYPE_IPV6); 18282cd038dSYoshinobu Inoue break; 18382cd038dSYoshinobu Inoue #endif 184cc6a66f2SJulian Elischer #ifdef IPX 185cc6a66f2SJulian Elischer case AF_IPX: 1864f93599fSBoris Popov if (ef_outputp) { 1872a7e8eceSBoris Popov error = ef_outputp(ifp, &m, dst, &type, &hlen); 1885accfb8cSBoris Popov if (error) 1895accfb8cSBoris Popov goto bad; 1904f93599fSBoris Popov } else 19134bed8b0SDavid Greenman type = htons(ETHERTYPE_IPX); 192cc6a66f2SJulian Elischer bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 193cc6a66f2SJulian Elischer (caddr_t)edst, sizeof (edst)); 194cc6a66f2SJulian Elischer break; 195cc6a66f2SJulian Elischer #endif 196655929bfSJulian Elischer #ifdef NETATALK 197655929bfSJulian Elischer case AF_APPLETALK: 1981d0eab59SJulian Elischer { 199ed7509acSJulian Elischer struct at_ifaddr *aa; 2001d0eab59SJulian Elischer 201ed7509acSJulian Elischer if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 2021d0eab59SJulian Elischer goto bad; 2031d0eab59SJulian Elischer } 204ed7509acSJulian Elischer if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 2051d0eab59SJulian Elischer return (0); 206655929bfSJulian Elischer /* 207ed7509acSJulian Elischer * In the phase 2 case, need to prepend an mbuf for the llc header. 208655929bfSJulian Elischer * Since we must preserve the value of m, which is passed to us by 209655929bfSJulian Elischer * value, we m_copy() the first mbuf, and use it for our llc header. 210655929bfSJulian Elischer */ 211655929bfSJulian Elischer if ( aa->aa_flags & AFA_PHASE2 ) { 212655929bfSJulian Elischer struct llc llc; 213655929bfSJulian Elischer 214797f247bSMatthew N. Dodd M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT); 215655929bfSJulian Elischer llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 216655929bfSJulian Elischer llc.llc_control = LLC_UI; 217655929bfSJulian Elischer bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 218655929bfSJulian Elischer llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 219797f247bSMatthew N. Dodd bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 22034bed8b0SDavid Greenman type = htons(m->m_pkthdr.len); 221797f247bSMatthew N. Dodd hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN; 222655929bfSJulian Elischer } else { 22334bed8b0SDavid Greenman type = htons(ETHERTYPE_AT); 224655929bfSJulian Elischer } 225655929bfSJulian Elischer break; 226ed7509acSJulian Elischer } 2278cdfefbdSPeter Wemm #endif /* NETATALK */ 228df8bae1dSRodney W. Grimes 229114ae644SMike Smith case pseudo_AF_HDRCMPLT: 230114ae644SMike Smith hdrcmplt = 1; 231114ae644SMike Smith eh = (struct ether_header *)dst->sa_data; 232114ae644SMike Smith (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 233114ae644SMike Smith /* FALLTHROUGH */ 234114ae644SMike Smith 235df8bae1dSRodney W. Grimes case AF_UNSPEC: 2369d3f194dSJulian Elischer loop_copy = -1; /* if this is for us, don't do it */ 237df8bae1dSRodney W. Grimes eh = (struct ether_header *)dst->sa_data; 23894a5d9b6SDavid Greenman (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 239df8bae1dSRodney W. Grimes type = eh->ether_type; 240df8bae1dSRodney W. Grimes break; 241df8bae1dSRodney W. Grimes 242df8bae1dSRodney W. Grimes default: 24329e1b85fSBrooks Davis if_printf(ifp, "can't handle af%d\n", dst->sa_family); 244df8bae1dSRodney W. Grimes senderr(EAFNOSUPPORT); 245df8bae1dSRodney W. Grimes } 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * Add local net header. If no space in first mbuf, 249df8bae1dSRodney W. Grimes * allocate another. 250df8bae1dSRodney W. Grimes */ 251797f247bSMatthew N. Dodd M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 252df8bae1dSRodney W. Grimes if (m == 0) 253df8bae1dSRodney W. Grimes senderr(ENOBUFS); 254df8bae1dSRodney W. Grimes eh = mtod(m, struct ether_header *); 25594a5d9b6SDavid Greenman (void)memcpy(&eh->ether_type, &type, 256df8bae1dSRodney W. Grimes sizeof(eh->ether_type)); 25794a5d9b6SDavid Greenman (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 258114ae644SMike Smith if (hdrcmplt) 259114ae644SMike Smith (void)memcpy(eh->ether_shost, esrc, 260114ae644SMike Smith sizeof(eh->ether_shost)); 261114ae644SMike Smith else 26294a5d9b6SDavid Greenman (void)memcpy(eh->ether_shost, ac->ac_enaddr, 263df8bae1dSRodney W. Grimes sizeof(eh->ether_shost)); 264ed7509acSJulian Elischer 265ed7509acSJulian Elischer /* 266ed7509acSJulian Elischer * If a simplex interface, and the packet is being sent to our 267ed7509acSJulian Elischer * Ethernet address or a broadcast address, loopback a copy. 268ed7509acSJulian Elischer * XXX To make a simplex device behave exactly like a duplex 269ed7509acSJulian Elischer * device, we should copy in the case of sending to our own 270ed7509acSJulian Elischer * ethernet address (thus letting the original actually appear 271ed7509acSJulian Elischer * on the wire). However, we don't do that here for security 272ed7509acSJulian Elischer * reasons and compatibility with the original behavior. 273ed7509acSJulian Elischer */ 2744c8e8c05SJulian Elischer if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 2753cd67511SJesper Skriver int csum_flags = 0; 2763cd67511SJesper Skriver 2773cd67511SJesper Skriver if (m->m_pkthdr.csum_flags & CSUM_IP) 2783cd67511SJesper Skriver csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 2793cd67511SJesper Skriver if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 2803cd67511SJesper Skriver csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 28106f684b0SMatthew N. Dodd 2829d3f194dSJulian Elischer if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 28306f684b0SMatthew N. Dodd struct mbuf *n; 284ed7509acSJulian Elischer 28579db6ff3SArchie Cobbs if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2863cd67511SJesper Skriver n->m_pkthdr.csum_flags |= csum_flags; 2873cd67511SJesper Skriver if (csum_flags & CSUM_DATA_VALID) 2883cd67511SJesper Skriver n->m_pkthdr.csum_data = 0xffff; 28906a429a3SArchie Cobbs (void)if_simloop(ifp, n, dst->sa_family, hlen); 290c1404dc0SArchie Cobbs } else 291c1404dc0SArchie Cobbs ifp->if_iqdrops++; 29206f684b0SMatthew N. Dodd } else if (bcmp(eh->ether_dhost, eh->ether_shost, 29306f684b0SMatthew N. Dodd ETHER_ADDR_LEN) == 0) { 2943cd67511SJesper Skriver m->m_pkthdr.csum_flags |= csum_flags; 2953cd67511SJesper Skriver if (csum_flags & CSUM_DATA_VALID) 2963cd67511SJesper Skriver m->m_pkthdr.csum_data = 0xffff; 29706a429a3SArchie Cobbs (void) if_simloop(ifp, m, dst->sa_family, hlen); 298ed7509acSJulian Elischer return (0); /* XXX */ 299ed7509acSJulian Elischer } 300ed7509acSJulian Elischer } 3012e2de7f2SArchie Cobbs 302e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 303e1e1452dSArchie Cobbs if (ng_ether_output_p != NULL) { 304e1e1452dSArchie Cobbs if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 305e1e1452dSArchie Cobbs bad: if (m != NULL) 306e1e1452dSArchie Cobbs m_freem(m); 307e1e1452dSArchie Cobbs return (error); 308e1e1452dSArchie Cobbs } 309e1e1452dSArchie Cobbs if (m == NULL) 310e1e1452dSArchie Cobbs return (0); 311e1e1452dSArchie Cobbs } 312e1e1452dSArchie Cobbs 313e1e1452dSArchie Cobbs /* Continue with link-layer output */ 314e1e1452dSArchie Cobbs return ether_output_frame(ifp, m); 315e1e1452dSArchie Cobbs } 316e1e1452dSArchie Cobbs 317e1e1452dSArchie Cobbs /* 318e1e1452dSArchie Cobbs * Ethernet link layer output routine to send a raw frame to the device. 319e1e1452dSArchie Cobbs * 320e1e1452dSArchie Cobbs * This assumes that the 14 byte Ethernet header is present and contiguous 321e1e1452dSArchie Cobbs * in the first mbuf (if BRIDGE'ing). 322e1e1452dSArchie Cobbs */ 323e1e1452dSArchie Cobbs int 324c1d93b05SSam Leffler ether_output_frame(struct ifnet *ifp, struct mbuf *m) 325e1e1452dSArchie Cobbs { 3264b984093SLuigi Rizzo struct ip_fw *rule = NULL; 3272b25acc1SLuigi Rizzo 3282b25acc1SLuigi Rizzo /* Extract info from dummynet tag, ignore others */ 3292b25acc1SLuigi Rizzo for (; m->m_type == MT_TAG; m = m->m_next) 3302b25acc1SLuigi Rizzo if (m->m_flags == PACKET_TAG_DUMMYNET) 3312b25acc1SLuigi Rizzo rule = ((struct dn_pkt *)m)->rule; 3322b25acc1SLuigi Rizzo 333c1d93b05SSam Leffler if (rule == NULL && BDG_ACTIVE(ifp)) { 334c1d93b05SSam Leffler /* 335c1d93b05SSam Leffler * Beware, the bridge code notices the null rcvif and 336c1d93b05SSam Leffler * uses that identify that it's being called from 337c1d93b05SSam Leffler * ether_output as opposd to ether_input. Yech. 338c1d93b05SSam Leffler */ 339fb5fbe46SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 340c1d93b05SSam Leffler m = bdg_forward_ptr(m, ifp); 3412e2de7f2SArchie Cobbs if (m != NULL) 3422e2de7f2SArchie Cobbs m_freem(m); 3431db59ce6SEivind Eklund return (0); 344fb5fbe46SLuigi Rizzo } 3454b984093SLuigi Rizzo if (IPFW_LOADED && ether_ipfw != 0) { 346c1d93b05SSam Leffler if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) { 3474b984093SLuigi Rizzo if (m) { 3484b984093SLuigi Rizzo m_freem(m); 3494b984093SLuigi Rizzo return ENOBUFS; /* pkt dropped */ 3504b984093SLuigi Rizzo } else 3514b984093SLuigi Rizzo return 0; /* consumed e.g. in a pipe */ 3524b984093SLuigi Rizzo } 3534b984093SLuigi Rizzo } 3542b25acc1SLuigi Rizzo 355df8bae1dSRodney W. Grimes /* 356df5e1987SJonathan Lemon * Queue message on interface, update output statistics if 357df5e1987SJonathan Lemon * successful, and start output if interface not yet active. 358df8bae1dSRodney W. Grimes */ 359c1d93b05SSam Leffler return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS); 360df8bae1dSRodney W. Grimes } 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes /* 3634b984093SLuigi Rizzo * ipfw processing for ethernet packets (in and out). 3644b984093SLuigi Rizzo * The second parameter is NULL from ether_demux, and ifp from 3654b984093SLuigi Rizzo * ether_output_frame. This section of code could be used from 3662f8ebbf4SLuigi Rizzo * bridge.c as well as long as we use some extra info 3674b984093SLuigi Rizzo * to distinguish that case from ether_output_frame(); 3684b984093SLuigi Rizzo */ 3694b984093SLuigi Rizzo int 3704b984093SLuigi Rizzo ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 371c1d93b05SSam Leffler struct ip_fw **rule, int shared) 3724b984093SLuigi Rizzo { 373c1d93b05SSam Leffler struct ether_header *eh; 374c1d93b05SSam Leffler struct ether_header save_eh; 375c1d93b05SSam Leffler struct mbuf *m; 3764b984093SLuigi Rizzo int i; 3772b25acc1SLuigi Rizzo struct ip_fw_args args; 3784b984093SLuigi Rizzo 37997850a5dSLuigi Rizzo if (*rule != NULL && fw_one_pass) 380015d72e0SLuigi Rizzo return 1; /* dummynet packet, already partially processed */ 3812f8ebbf4SLuigi Rizzo 3824b984093SLuigi Rizzo /* 3832f8ebbf4SLuigi Rizzo * I need some amt of data to be contiguous, and in case others need 3844b984093SLuigi Rizzo * the packet (shared==1) also better be in the first mbuf. 3854b984093SLuigi Rizzo */ 386c1d93b05SSam Leffler m = *m0; 387c1d93b05SSam Leffler i = min( m->m_pkthdr.len, max_protohdr); 388c1d93b05SSam Leffler if ( shared || m->m_len < i) { 389c1d93b05SSam Leffler m = m_pullup(m, i); 390c1d93b05SSam Leffler if (m == NULL) { 391c1d93b05SSam Leffler *m0 = m; 3924b984093SLuigi Rizzo return 0; 3934b984093SLuigi Rizzo } 394c1d93b05SSam Leffler } 395c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 396c1d93b05SSam Leffler save_eh = *eh; /* save copy for restore below */ 397c1d93b05SSam Leffler m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 3984b984093SLuigi Rizzo 399c1d93b05SSam Leffler args.m = m; /* the packet we are looking at */ 4002b25acc1SLuigi Rizzo args.oif = dst; /* destination, if any */ 4012b25acc1SLuigi Rizzo args.divert_rule = 0; /* we do not support divert yet */ 4022b25acc1SLuigi Rizzo args.rule = *rule; /* matching rule to restart */ 4032b25acc1SLuigi Rizzo args.next_hop = NULL; /* we do not support forward yet */ 4042b25acc1SLuigi Rizzo args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 4052b25acc1SLuigi Rizzo i = ip_fw_chk_ptr(&args); 406c1d93b05SSam Leffler m = args.m; 407c1d93b05SSam Leffler if (m != NULL) { 408c1d93b05SSam Leffler /* 409c1d93b05SSam Leffler * Restore Ethernet header, as needed, in case the 410c1d93b05SSam Leffler * mbuf chain was replaced by ipfw. 411c1d93b05SSam Leffler */ 412a163d034SWarner Losh M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 413c1d93b05SSam Leffler if (m == NULL) { 414c1d93b05SSam Leffler *m0 = m; 415c1d93b05SSam Leffler return 0; 416c1d93b05SSam Leffler } 417c1d93b05SSam Leffler if (eh != mtod(m, struct ether_header *)) 418c1d93b05SSam Leffler bcopy(&save_eh, mtod(m, struct ether_header *), 419c1d93b05SSam Leffler ETHER_HDR_LEN); 420c1d93b05SSam Leffler } 421c1d93b05SSam Leffler *m0 = m; 4222b25acc1SLuigi Rizzo *rule = args.rule; 4232b25acc1SLuigi Rizzo 424c1d93b05SSam Leffler if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */ 4254b984093SLuigi Rizzo return 0; 4264b984093SLuigi Rizzo 4274b984093SLuigi Rizzo if (i == 0) /* a PASS rule. */ 4284b984093SLuigi Rizzo return 1; 4294b984093SLuigi Rizzo 4304b984093SLuigi Rizzo if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 4314b984093SLuigi Rizzo /* 4324b984093SLuigi Rizzo * Pass the pkt to dummynet, which consumes it. 4334b984093SLuigi Rizzo * If shared, make a copy and keep the original. 4344b984093SLuigi Rizzo */ 4354b984093SLuigi Rizzo if (shared) { 436a163d034SWarner Losh m = m_copypacket(m, M_DONTWAIT); 4372b25acc1SLuigi Rizzo if (m == NULL) 4384b984093SLuigi Rizzo return 0; 4394b984093SLuigi Rizzo } else { 4402b25acc1SLuigi Rizzo /* 441c1d93b05SSam Leffler * Pass the original to dummynet and 442c1d93b05SSam Leffler * nothing back to the caller 4432b25acc1SLuigi Rizzo */ 444c1d93b05SSam Leffler *m0 = NULL ; 4454b984093SLuigi Rizzo } 4462b25acc1SLuigi Rizzo ip_dn_io_ptr(m, (i & 0xffff), 4472b25acc1SLuigi Rizzo dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 4484b984093SLuigi Rizzo return 0; 4494b984093SLuigi Rizzo } 4504b984093SLuigi Rizzo /* 4512f8ebbf4SLuigi Rizzo * XXX at some point add support for divert/forward actions. 4522f8ebbf4SLuigi Rizzo * If none of the above matches, we have to drop the pkt. 4534b984093SLuigi Rizzo */ 4544b984093SLuigi Rizzo return 0; 4554b984093SLuigi Rizzo } 4564b984093SLuigi Rizzo 4574b984093SLuigi Rizzo /* 458c1d93b05SSam Leffler * Process a received Ethernet packet; the packet is in the 459c1d93b05SSam Leffler * mbuf chain m with the ethernet header at the front. 460df8bae1dSRodney W. Grimes */ 461c1d93b05SSam Leffler static void 462c1d93b05SSam Leffler ether_input(struct ifnet *ifp, struct mbuf *m) 463df8bae1dSRodney W. Grimes { 464c1d93b05SSam Leffler struct ether_header *eh; 465c1d93b05SSam Leffler u_short etype; 466df8bae1dSRodney W. Grimes 467c1d93b05SSam Leffler /* 468c1d93b05SSam Leffler * Do consistency checks to verify assumptions 469c1d93b05SSam Leffler * made by code past this point. 470c1d93b05SSam Leffler */ 471c1d93b05SSam Leffler if ((m->m_flags & M_PKTHDR) == 0) { 472c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o packet header\n"); 473c1d93b05SSam Leffler ifp->if_ierrors++; 474c939f1aeSLuigi Rizzo m_freem(m); 475c939f1aeSLuigi Rizzo return; 476c939f1aeSLuigi Rizzo } 477797f247bSMatthew N. Dodd if (m->m_len < ETHER_HDR_LEN) { 478c1d93b05SSam Leffler /* XXX maybe should pullup? */ 479c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o leading ethernet " 480c1d93b05SSam Leffler "header (len %u pkt len %u)\n", 481c1d93b05SSam Leffler m->m_len, m->m_pkthdr.len); 482c1d93b05SSam Leffler ifp->if_ierrors++; 483c1d93b05SSam Leffler m_freem(m); 484c1d93b05SSam Leffler return; 4852e2de7f2SArchie Cobbs } 486c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 487c1d93b05SSam Leffler etype = ntohs(eh->ether_type); 488c1d93b05SSam Leffler if (m->m_pkthdr.len > 489c1d93b05SSam Leffler ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 490c1d93b05SSam Leffler if_printf(ifp, "discard oversize frame " 491c1d93b05SSam Leffler "(ether type %x flags %x len %u > max %lu)\n", 492c1d93b05SSam Leffler etype, m->m_flags, m->m_pkthdr.len, 493c1d93b05SSam Leffler ETHER_MAX_FRAME(ifp, etype, 494c1d93b05SSam Leffler m->m_flags & M_HASFCS)); 495c1d93b05SSam Leffler ifp->if_ierrors++; 496c1d93b05SSam Leffler m_freem(m); 497c1d93b05SSam Leffler return; 498c1d93b05SSam Leffler } 499c1d93b05SSam Leffler if (m->m_pkthdr.rcvif == NULL) { 500c1d93b05SSam Leffler if_printf(ifp, "discard frame w/o interface pointer\n"); 501c1d93b05SSam Leffler ifp->if_ierrors++; 502c1d93b05SSam Leffler m_freem(m); 503c1d93b05SSam Leffler return; 504c1d93b05SSam Leffler } 505c1d93b05SSam Leffler #ifdef DIAGNOSTIC 506c1d93b05SSam Leffler if (m->m_pkthdr.rcvif != ifp) { 507c1d93b05SSam Leffler if_printf(ifp, "Warning, frame marked as received on %s%u\n", 508c1d93b05SSam Leffler m->m_pkthdr.rcvif->if_name, 509c1d93b05SSam Leffler m->m_pkthdr.rcvif->if_unit); 510c1d93b05SSam Leffler } 511c1d93b05SSam Leffler #endif 512c1d93b05SSam Leffler 513533d8562SRobert Watson #ifdef MAC 514533d8562SRobert Watson /* 515533d8562SRobert Watson * Tag the mbuf with an appropriate MAC label before any other 516533d8562SRobert Watson * consumers can get to it. 517533d8562SRobert Watson */ 518533d8562SRobert Watson mac_create_mbuf_from_ifnet(ifp, m); 519533d8562SRobert Watson #endif 520533d8562SRobert Watson 521c1d93b05SSam Leffler /* 522c1d93b05SSam Leffler * Give bpf a chance at the packet. 523c1d93b05SSam Leffler */ 524c1d93b05SSam Leffler BPF_MTAP(ifp, m); 5252e2de7f2SArchie Cobbs 526afbe3a0fSPoul-Henning Kamp if (ifp->if_flags & IFF_MONITOR) { 527c1d93b05SSam Leffler /* 528c1d93b05SSam Leffler * Interface marked for monitoring; discard packet. 529c1d93b05SSam Leffler */ 530afbe3a0fSPoul-Henning Kamp m_freem(m); 531afbe3a0fSPoul-Henning Kamp return; 532afbe3a0fSPoul-Henning Kamp } 533afbe3a0fSPoul-Henning Kamp 534c1d93b05SSam Leffler /* If the CRC is still on the packet, trim it off. */ 535c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 536c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 537c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 538c1d93b05SSam Leffler } 539c1d93b05SSam Leffler 540c1d93b05SSam Leffler ifp->if_ibytes += m->m_pkthdr.len; 54105463bb5SDavid Greenman 542e1e1452dSArchie Cobbs /* Handle ng_ether(4) processing, if any */ 543e1e1452dSArchie Cobbs if (ng_ether_input_p != NULL) { 544c1d93b05SSam Leffler (*ng_ether_input_p)(ifp, &m); 545e1e1452dSArchie Cobbs if (m == NULL) 546e1e1452dSArchie Cobbs return; 547e1e1452dSArchie Cobbs } 548e1e1452dSArchie Cobbs 5492e2de7f2SArchie Cobbs /* Check for bridging mode */ 5507b109fa4SLuigi Rizzo if (BDG_ACTIVE(ifp) ) { 5512e2de7f2SArchie Cobbs struct ifnet *bif; 5522e2de7f2SArchie Cobbs 553c1d93b05SSam Leffler /* 554c1d93b05SSam Leffler * Check with bridging code to see how the packet 555c1d93b05SSam Leffler * should be handled. Possibilities are: 556c1d93b05SSam Leffler * 557c1d93b05SSam Leffler * BDG_BCAST broadcast 558c1d93b05SSam Leffler * BDG_MCAST multicast 559c1d93b05SSam Leffler * BDG_LOCAL for local address, don't forward 560c1d93b05SSam Leffler * BDG_DROP discard 561c1d93b05SSam Leffler * ifp forward only to specified interface(s) 562c1d93b05SSam Leffler * 563c1d93b05SSam Leffler * Non-local destinations are handled by passing the 564c1d93b05SSam Leffler * packet back to the bridge code. 565c1d93b05SSam Leffler */ 566c1d93b05SSam Leffler bif = bridge_in_ptr(ifp, eh); 567c1d93b05SSam Leffler if (bif == BDG_DROP) { /* discard packet */ 5682e2de7f2SArchie Cobbs m_freem(m); 5692e2de7f2SArchie Cobbs return; 5702e2de7f2SArchie Cobbs } 571c1d93b05SSam Leffler if (bif != BDG_LOCAL) { /* non-local, forward */ 572c1d93b05SSam Leffler m = bdg_forward_ptr(m, bif); 573ddacb30fSBosko Milekic /* 574c1d93b05SSam Leffler * The bridge may consume the packet if it's not 575c1d93b05SSam Leffler * supposed to be passed up or if a problem occurred 576c1d93b05SSam Leffler * while doing its job. This is reflected by it 577c1d93b05SSam Leffler * returning a NULL mbuf pointer. 578ddacb30fSBosko Milekic */ 579507b4b54SLuigi Rizzo if (m == NULL) { 580507b4b54SLuigi Rizzo if (bif == BDG_BCAST || bif == BDG_MCAST) 581c1d93b05SSam Leffler if_printf(ifp, 582c1d93b05SSam Leffler "bridge dropped %s packet\n", 583c1d93b05SSam Leffler bif == BDG_BCAST ? "broadcast" : 584c1d93b05SSam Leffler "multicast"); 585ddacb30fSBosko Milekic return; 586ddacb30fSBosko Milekic } 587c1d93b05SSam Leffler /* 588c1d93b05SSam Leffler * But in some cases the bridge may return the 589c1d93b05SSam Leffler * packet for us to free; sigh. 590c1d93b05SSam Leffler */ 591c1d93b05SSam Leffler if (bif != BDG_BCAST && bif != BDG_MCAST) { 5922e2de7f2SArchie Cobbs m_freem(m); 5932e2de7f2SArchie Cobbs return; 5942e2de7f2SArchie Cobbs } 595c1d93b05SSam Leffler } 596c1d93b05SSam Leffler } 5972e2de7f2SArchie Cobbs 598c1d93b05SSam Leffler ether_demux(ifp, m); 599a6ddbff0SPoul-Henning Kamp /* First chunk of an mbuf contains good entropy */ 60010b1fde0SMark Murray if (harvest.ethernet) 60110b1fde0SMark Murray random_harvest(m, 16, 3, 0, RANDOM_NET); 602e1e1452dSArchie Cobbs } 603e1e1452dSArchie Cobbs 604e1e1452dSArchie Cobbs /* 605e1e1452dSArchie Cobbs * Upper layer processing for a received Ethernet packet. 606e1e1452dSArchie Cobbs */ 607e1e1452dSArchie Cobbs void 608c1d93b05SSam Leffler ether_demux(struct ifnet *ifp, struct mbuf *m) 609e1e1452dSArchie Cobbs { 610c1d93b05SSam Leffler struct ether_header *eh; 6111cafed39SJonathan Lemon int isr; 612e1e1452dSArchie Cobbs u_short ether_type; 613e1e1452dSArchie Cobbs #if defined(NETATALK) 614098a8c3bSMatthew N. Dodd struct llc *l; 615e1e1452dSArchie Cobbs #endif 6164b984093SLuigi Rizzo struct ip_fw *rule = NULL; 6172b25acc1SLuigi Rizzo 6182b25acc1SLuigi Rizzo /* Extract info from dummynet tag, ignore others */ 6192b25acc1SLuigi Rizzo for (;m->m_type == MT_TAG; m = m->m_next) 6202b25acc1SLuigi Rizzo if (m->m_flags == PACKET_TAG_DUMMYNET) { 6212b25acc1SLuigi Rizzo rule = ((struct dn_pkt *)m)->rule; 6222b25acc1SLuigi Rizzo ifp = m->m_next->m_pkthdr.rcvif; 6234b984093SLuigi Rizzo } 6242b25acc1SLuigi Rizzo 625c1d93b05SSam Leffler KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer")); 626c1d93b05SSam Leffler 627c1d93b05SSam Leffler eh = mtod(m, struct ether_header *); 628c1d93b05SSam Leffler 6292b25acc1SLuigi Rizzo if (rule) /* packet was already bridged */ 6302b25acc1SLuigi Rizzo goto post_stats; 6314b984093SLuigi Rizzo 632c6943100SPoul-Henning Kamp if (!(BDG_ACTIVE(ifp))) { 633c6943100SPoul-Henning Kamp /* 634c6943100SPoul-Henning Kamp * Discard packet if upper layers shouldn't see it because it 635c6943100SPoul-Henning Kamp * was unicast to a different Ethernet address. If the driver 636c6943100SPoul-Henning Kamp * is working properly, then this situation can only happen 637c6943100SPoul-Henning Kamp * when the interface is in promiscuous mode. 638c6943100SPoul-Henning Kamp */ 639cb24f323SArchie Cobbs if ((ifp->if_flags & IFF_PROMISC) != 0 640cb24f323SArchie Cobbs && (eh->ether_dhost[0] & 1) == 0 641cb24f323SArchie Cobbs && bcmp(eh->ether_dhost, 642ffb079beSMaxim Sobolev IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0 64325faf49dSMaxim Sobolev && (ifp->if_flags & IFF_PPROMISC) == 0) { 644cb24f323SArchie Cobbs m_freem(m); 645cb24f323SArchie Cobbs return; 646cb24f323SArchie Cobbs } 647c6943100SPoul-Henning Kamp } 648cb24f323SArchie Cobbs 6492e2de7f2SArchie Cobbs /* Discard packet if interface is not up */ 650df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_UP) == 0) { 651df8bae1dSRodney W. Grimes m_freem(m); 652df8bae1dSRodney W. Grimes return; 653df8bae1dSRodney W. Grimes } 654588523faSSam Leffler if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 655df8bae1dSRodney W. Grimes if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 656df8bae1dSRodney W. Grimes sizeof(etherbroadcastaddr)) == 0) 657df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 6582cc2df49SGarrett Wollman else 659df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 6602cc2df49SGarrett Wollman } 661df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST|M_MCAST)) 662df8bae1dSRodney W. Grimes ifp->if_imcasts++; 663df8bae1dSRodney W. Grimes 6644b984093SLuigi Rizzo post_stats: 6654b984093SLuigi Rizzo if (IPFW_LOADED && ether_ipfw != 0) { 666c1d93b05SSam Leffler if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) { 6674b984093SLuigi Rizzo if (m) 6684b984093SLuigi Rizzo m_freem(m); 6694b984093SLuigi Rizzo return; 6704b984093SLuigi Rizzo } 6714b984093SLuigi Rizzo } 6724b984093SLuigi Rizzo 673c1d93b05SSam Leffler /* 674c1d93b05SSam Leffler * If VLANs are configured on the interface, check to 675c1d93b05SSam Leffler * see if the device performed the decapsulation and 676c1d93b05SSam Leffler * provided us with the tag. 677c1d93b05SSam Leffler */ 678c1d93b05SSam Leffler if (ifp->if_nvlans && 679c1d93b05SSam Leffler m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) { 680c1d93b05SSam Leffler /* 681c1d93b05SSam Leffler * vlan_input() will either recursively call ether_input() 682c1d93b05SSam Leffler * or drop the packet. 683c1d93b05SSam Leffler */ 684c1d93b05SSam Leffler KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!")); 685c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 686c1d93b05SSam Leffler return; 687c1d93b05SSam Leffler } 688c1d93b05SSam Leffler 689307d80beSDavid Greenman ether_type = ntohs(eh->ether_type); 690307d80beSDavid Greenman 691c1d93b05SSam Leffler /* 692c1d93b05SSam Leffler * Handle protocols that expect to have the Ethernet header 693c1d93b05SSam Leffler * (and possibly FCS) intact. 694c1d93b05SSam Leffler */ 695c1d93b05SSam Leffler switch (ether_type) { 696c1d93b05SSam Leffler case ETHERTYPE_VLAN: 697c1d93b05SSam Leffler if (ifp->if_nvlans != 0) { 698c1d93b05SSam Leffler KASSERT(vlan_input_p,("ether_input: VLAN not loaded!")); 699c1d93b05SSam Leffler (*vlan_input_p)(ifp, m); 700c1d93b05SSam Leffler } else { 701c1d93b05SSam Leffler ifp->if_noproto++; 702c1d93b05SSam Leffler m_freem(m); 703c1d93b05SSam Leffler } 704c1d93b05SSam Leffler return; 705c1d93b05SSam Leffler } 706c1d93b05SSam Leffler 707c1d93b05SSam Leffler /* Strip off Ethernet header. */ 708797f247bSMatthew N. Dodd m_adj(m, ETHER_HDR_LEN); 709c1d93b05SSam Leffler 710c1d93b05SSam Leffler /* If the CRC is still on the packet, trim it off. */ 711c1d93b05SSam Leffler if (m->m_flags & M_HASFCS) { 712c1d93b05SSam Leffler m_adj(m, -ETHER_CRC_LEN); 713c1d93b05SSam Leffler m->m_flags &= ~M_HASFCS; 714c1d93b05SSam Leffler } 715c1d93b05SSam Leffler 716307d80beSDavid Greenman switch (ether_type) { 717df8bae1dSRodney W. Grimes #ifdef INET 718df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 7191f91d8c5SDavid Greenman if (ipflow_fastforward(m)) 7201f91d8c5SDavid Greenman return; 7211cafed39SJonathan Lemon isr = NETISR_IP; 722df8bae1dSRodney W. Grimes break; 723df8bae1dSRodney W. Grimes 724df8bae1dSRodney W. Grimes case ETHERTYPE_ARP: 72508aadfbbSJonathan Lemon if (ifp->if_flags & IFF_NOARP) { 72608aadfbbSJonathan Lemon /* Discard packet if ARP is disabled on interface */ 72708aadfbbSJonathan Lemon m_freem(m); 72808aadfbbSJonathan Lemon return; 72908aadfbbSJonathan Lemon } 7301cafed39SJonathan Lemon isr = NETISR_ARP; 731df8bae1dSRodney W. Grimes break; 732df8bae1dSRodney W. Grimes #endif 733cc6a66f2SJulian Elischer #ifdef IPX 734cc6a66f2SJulian Elischer case ETHERTYPE_IPX: 7354f93599fSBoris Popov if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 7364f93599fSBoris Popov return; 7371cafed39SJonathan Lemon isr = NETISR_IPX; 738cc6a66f2SJulian Elischer break; 739cc6a66f2SJulian Elischer #endif 74082cd038dSYoshinobu Inoue #ifdef INET6 74182cd038dSYoshinobu Inoue case ETHERTYPE_IPV6: 7421cafed39SJonathan Lemon isr = NETISR_IPV6; 74382cd038dSYoshinobu Inoue break; 74482cd038dSYoshinobu Inoue #endif 745655929bfSJulian Elischer #ifdef NETATALK 746655929bfSJulian Elischer case ETHERTYPE_AT: 7471cafed39SJonathan Lemon isr = NETISR_ATALK1; 748655929bfSJulian Elischer break; 749655929bfSJulian Elischer case ETHERTYPE_AARP: 7501cafed39SJonathan Lemon isr = NETISR_AARP; 7511cafed39SJonathan Lemon break; 7528cdfefbdSPeter Wemm #endif /* NETATALK */ 753df8bae1dSRodney W. Grimes default: 7544f93599fSBoris Popov #ifdef IPX 7554f93599fSBoris Popov if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 7564f93599fSBoris Popov return; 7574f93599fSBoris Popov #endif /* IPX */ 758242c5536SPeter Wemm #if defined(NETATALK) 759307d80beSDavid Greenman if (ether_type > ETHERMTU) 760c1d93b05SSam Leffler goto discard; 761df8bae1dSRodney W. Grimes l = mtod(m, struct llc *); 7621cafed39SJonathan Lemon if (l->llc_dsap == LLC_SNAP_LSAP && 7631cafed39SJonathan Lemon l->llc_ssap == LLC_SNAP_LSAP && 7641cafed39SJonathan Lemon l->llc_control == LLC_UI) { 765655929bfSJulian Elischer if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 766655929bfSJulian Elischer sizeof(at_org_code)) == 0 && 767655929bfSJulian Elischer ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 768797f247bSMatthew N. Dodd m_adj(m, LLC_SNAPFRAMELEN); 7691cafed39SJonathan Lemon isr = NETISR_ATALK2; 770655929bfSJulian Elischer break; 771655929bfSJulian Elischer } 772655929bfSJulian Elischer if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 773655929bfSJulian Elischer sizeof(aarp_org_code)) == 0 && 774655929bfSJulian Elischer ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 775797f247bSMatthew N. Dodd m_adj(m, LLC_SNAPFRAMELEN); 7761cafed39SJonathan Lemon isr = NETISR_AARP; 777655929bfSJulian Elischer break; 778df8bae1dSRodney W. Grimes } 7791cafed39SJonathan Lemon } 780242c5536SPeter Wemm #endif /* NETATALK */ 7811cafed39SJonathan Lemon goto discard; 782df8bae1dSRodney W. Grimes } 7831cafed39SJonathan Lemon netisr_dispatch(isr, m); 784c1d93b05SSam Leffler return; 7851cafed39SJonathan Lemon 786c1d93b05SSam Leffler discard: 787c1d93b05SSam Leffler /* 788c1d93b05SSam Leffler * Packet is to be discarded. If netgraph is present, 789c1d93b05SSam Leffler * hand the packet to it for last chance processing; 790c1d93b05SSam Leffler * otherwise dispose of it. 791c1d93b05SSam Leffler */ 792c1d93b05SSam Leffler if (ng_ether_input_orphan_p != NULL) { 793c1d93b05SSam Leffler /* 794c1d93b05SSam Leffler * Put back the ethernet header so netgraph has a 795c1d93b05SSam Leffler * consistent view of inbound packets. 796c1d93b05SSam Leffler */ 797797f247bSMatthew N. Dodd M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 798c1d93b05SSam Leffler (*ng_ether_input_orphan_p)(ifp, m); 799c1d93b05SSam Leffler return; 800c1d93b05SSam Leffler } 801c1d93b05SSam Leffler m_freem(m); 802c1d93b05SSam Leffler } 803c1d93b05SSam Leffler 804c1d93b05SSam Leffler /* 805c1d93b05SSam Leffler * Convert Ethernet address to printable (loggable) representation. 806c1d93b05SSam Leffler * This routine is for compatibility; it's better to just use 807c1d93b05SSam Leffler * 808c1d93b05SSam Leffler * printf("%6D", <pointer to address>, ":"); 809c1d93b05SSam Leffler * 810c1d93b05SSam Leffler * since there's no static buffer involved. 811c1d93b05SSam Leffler */ 812c1d93b05SSam Leffler char * 813c1d93b05SSam Leffler ether_sprintf(const u_char *ap) 814c1d93b05SSam Leffler { 815c1d93b05SSam Leffler static char etherbuf[18]; 816c1d93b05SSam Leffler snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 817c1d93b05SSam Leffler return (etherbuf); 818df8bae1dSRodney W. Grimes } 819df8bae1dSRodney W. Grimes 820df8bae1dSRodney W. Grimes /* 821df8bae1dSRodney W. Grimes * Perform common duties while attaching to interface list 822df8bae1dSRodney W. Grimes */ 823df8bae1dSRodney W. Grimes void 824c1d93b05SSam Leffler ether_ifattach(struct ifnet *ifp, const u_int8_t *llc) 825df8bae1dSRodney W. Grimes { 826098a8c3bSMatthew N. Dodd struct ifaddr *ifa; 827098a8c3bSMatthew N. Dodd struct sockaddr_dl *sdl; 828df8bae1dSRodney W. Grimes 829df8bae1dSRodney W. Grimes ifp->if_type = IFT_ETHER; 830c1d93b05SSam Leffler ifp->if_addrlen = ETHER_ADDR_LEN; 831c1d93b05SSam Leffler ifp->if_hdrlen = ETHER_HDR_LEN; 832cfeff1b6SJonathan Lemon if_attach(ifp); 833df8bae1dSRodney W. Grimes ifp->if_mtu = ETHERMTU; 834c1d93b05SSam Leffler ifp->if_output = ether_output; 835c1d93b05SSam Leffler ifp->if_input = ether_input; 8361158dfb7SGarrett Wollman ifp->if_resolvemulti = ether_resolvemulti; 837a330e1f1SGary Palmer if (ifp->if_baudrate == 0) 838c1d93b05SSam Leffler ifp->if_baudrate = IF_Mbps(10); /* just a default */ 839322dcb8dSMax Khon ifp->if_broadcastaddr = etherbroadcastaddr; 840c1d93b05SSam Leffler 841f9132cebSJonathan Lemon ifa = ifaddr_byindex(ifp->if_index); 8426e551fb6SDavid E. O'Brien KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 84359562606SGarrett Wollman sdl = (struct sockaddr_dl *)ifa->ifa_addr; 844df8bae1dSRodney W. Grimes sdl->sdl_type = IFT_ETHER; 845df8bae1dSRodney W. Grimes sdl->sdl_alen = ifp->if_addrlen; 846c1d93b05SSam Leffler bcopy(llc, LLADDR(sdl), ifp->if_addrlen); 847c1d93b05SSam Leffler /* 848c1d93b05SSam Leffler * XXX: This doesn't belong here; we do it until 849c1d93b05SSam Leffler * XXX: all drivers are cleaned up 850c1d93b05SSam Leffler */ 851c1d93b05SSam Leffler if (llc != IFP2AC(ifp)->ac_enaddr) 852c1d93b05SSam Leffler bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 853c1d93b05SSam Leffler 854797f247bSMatthew N. Dodd bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 855e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) 856e1e1452dSArchie Cobbs (*ng_ether_attach_p)(ifp); 8577b109fa4SLuigi Rizzo if (BDG_LOADED) 858db69a05dSPaul Saab bdgtakeifaces_ptr(); 859e1e1452dSArchie Cobbs } 860e1e1452dSArchie Cobbs 86121b8ebd9SArchie Cobbs /* 86221b8ebd9SArchie Cobbs * Perform common duties while detaching an Ethernet interface 86321b8ebd9SArchie Cobbs */ 86421b8ebd9SArchie Cobbs void 865c1d93b05SSam Leffler ether_ifdetach(struct ifnet *ifp) 86621b8ebd9SArchie Cobbs { 86721b8ebd9SArchie Cobbs if (ng_ether_detach_p != NULL) 86821b8ebd9SArchie Cobbs (*ng_ether_detach_p)(ifp); 86921b8ebd9SArchie Cobbs bpfdetach(ifp); 87021b8ebd9SArchie Cobbs if_detach(ifp); 8717b109fa4SLuigi Rizzo if (BDG_LOADED) 872db69a05dSPaul Saab bdgtakeifaces_ptr(); 87321b8ebd9SArchie Cobbs } 87421b8ebd9SArchie Cobbs 875ce02431fSDoug Rabson SYSCTL_DECL(_net_link); 876602d513cSGarrett Wollman SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 8774b984093SLuigi Rizzo SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 8784b984093SLuigi Rizzo ðer_ipfw,0,"Pass ether pkts through firewall"); 87930106f6aSPoul-Henning Kamp 880fb583156SDavid Greenman int 881c5a1016bSBruce Evans ether_ioctl(ifp, command, data) 882c5a1016bSBruce Evans struct ifnet *ifp; 883c5a1016bSBruce Evans int command; 884c5a1016bSBruce Evans caddr_t data; 88530106f6aSPoul-Henning Kamp { 88630106f6aSPoul-Henning Kamp struct ifaddr *ifa = (struct ifaddr *) data; 88730106f6aSPoul-Henning Kamp struct ifreq *ifr = (struct ifreq *) data; 888fb583156SDavid Greenman int error = 0; 88930106f6aSPoul-Henning Kamp 89030106f6aSPoul-Henning Kamp switch (command) { 89130106f6aSPoul-Henning Kamp case SIOCSIFADDR: 89230106f6aSPoul-Henning Kamp ifp->if_flags |= IFF_UP; 89330106f6aSPoul-Henning Kamp 89430106f6aSPoul-Henning Kamp switch (ifa->ifa_addr->sa_family) { 89530106f6aSPoul-Henning Kamp #ifdef INET 89630106f6aSPoul-Henning Kamp case AF_INET: 89730106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); /* before arpwhohas */ 898322dcb8dSMax Khon arp_ifinit(ifp, ifa); 89930106f6aSPoul-Henning Kamp break; 90030106f6aSPoul-Henning Kamp #endif 90130106f6aSPoul-Henning Kamp #ifdef IPX 90230106f6aSPoul-Henning Kamp /* 90330106f6aSPoul-Henning Kamp * XXX - This code is probably wrong 90430106f6aSPoul-Henning Kamp */ 90530106f6aSPoul-Henning Kamp case AF_IPX: 90630106f6aSPoul-Henning Kamp { 907098a8c3bSMatthew N. Dodd struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 9084c8e8c05SJulian Elischer struct arpcom *ac = IFP2AC(ifp); 90930106f6aSPoul-Henning Kamp 91030106f6aSPoul-Henning Kamp if (ipx_nullhost(*ina)) 91130106f6aSPoul-Henning Kamp ina->x_host = 91286101139SPoul-Henning Kamp *(union ipx_host *) 91386101139SPoul-Henning Kamp ac->ac_enaddr; 91430106f6aSPoul-Henning Kamp else { 91530106f6aSPoul-Henning Kamp bcopy((caddr_t) ina->x_host.c_host, 91686101139SPoul-Henning Kamp (caddr_t) ac->ac_enaddr, 91786101139SPoul-Henning Kamp sizeof(ac->ac_enaddr)); 91830106f6aSPoul-Henning Kamp } 91930106f6aSPoul-Henning Kamp 92030106f6aSPoul-Henning Kamp /* 92130106f6aSPoul-Henning Kamp * Set new address 92230106f6aSPoul-Henning Kamp */ 92330106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 92430106f6aSPoul-Henning Kamp break; 92530106f6aSPoul-Henning Kamp } 92630106f6aSPoul-Henning Kamp #endif 92730106f6aSPoul-Henning Kamp default: 92830106f6aSPoul-Henning Kamp ifp->if_init(ifp->if_softc); 92930106f6aSPoul-Henning Kamp break; 93030106f6aSPoul-Henning Kamp } 93130106f6aSPoul-Henning Kamp break; 93230106f6aSPoul-Henning Kamp 93330106f6aSPoul-Henning Kamp case SIOCGIFADDR: 93430106f6aSPoul-Henning Kamp { 93530106f6aSPoul-Henning Kamp struct sockaddr *sa; 93630106f6aSPoul-Henning Kamp 93730106f6aSPoul-Henning Kamp sa = (struct sockaddr *) & ifr->ifr_data; 9384c8e8c05SJulian Elischer bcopy(IFP2AC(ifp)->ac_enaddr, 93930106f6aSPoul-Henning Kamp (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 94030106f6aSPoul-Henning Kamp } 94130106f6aSPoul-Henning Kamp break; 942fb583156SDavid Greenman 943fb583156SDavid Greenman case SIOCSIFMTU: 944fb583156SDavid Greenman /* 945fb583156SDavid Greenman * Set the interface MTU. 946fb583156SDavid Greenman */ 947fb583156SDavid Greenman if (ifr->ifr_mtu > ETHERMTU) { 948fb583156SDavid Greenman error = EINVAL; 949fb583156SDavid Greenman } else { 950fb583156SDavid Greenman ifp->if_mtu = ifr->ifr_mtu; 95130106f6aSPoul-Henning Kamp } 952fb583156SDavid Greenman break; 953c1d93b05SSam Leffler default: 954c1d93b05SSam Leffler error = EINVAL; /* XXX netbsd has ENOTTY??? */ 955c1d93b05SSam Leffler break; 956fb583156SDavid Greenman } 957fb583156SDavid Greenman return (error); 95830106f6aSPoul-Henning Kamp } 9591158dfb7SGarrett Wollman 96037c84183SPoul-Henning Kamp static int 9611158dfb7SGarrett Wollman ether_resolvemulti(ifp, llsa, sa) 9621158dfb7SGarrett Wollman struct ifnet *ifp; 9631158dfb7SGarrett Wollman struct sockaddr **llsa; 9641158dfb7SGarrett Wollman struct sockaddr *sa; 9651158dfb7SGarrett Wollman { 9661158dfb7SGarrett Wollman struct sockaddr_dl *sdl; 9671158dfb7SGarrett Wollman struct sockaddr_in *sin; 96882cd038dSYoshinobu Inoue #ifdef INET6 96982cd038dSYoshinobu Inoue struct sockaddr_in6 *sin6; 97082cd038dSYoshinobu Inoue #endif 9711158dfb7SGarrett Wollman u_char *e_addr; 9721158dfb7SGarrett Wollman 9731158dfb7SGarrett Wollman switch(sa->sa_family) { 9741158dfb7SGarrett Wollman case AF_LINK: 9757f33a738SJulian Elischer /* 9767f33a738SJulian Elischer * No mapping needed. Just check that it's a valid MC address. 9777f33a738SJulian Elischer */ 9781158dfb7SGarrett Wollman sdl = (struct sockaddr_dl *)sa; 9791158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 9801158dfb7SGarrett Wollman if ((e_addr[0] & 1) != 1) 9811158dfb7SGarrett Wollman return EADDRNOTAVAIL; 9821158dfb7SGarrett Wollman *llsa = 0; 9831158dfb7SGarrett Wollman return 0; 9841158dfb7SGarrett Wollman 9851158dfb7SGarrett Wollman #ifdef INET 9861158dfb7SGarrett Wollman case AF_INET: 9871158dfb7SGarrett Wollman sin = (struct sockaddr_in *)sa; 9881158dfb7SGarrett Wollman if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 9891158dfb7SGarrett Wollman return EADDRNOTAVAIL; 9901158dfb7SGarrett Wollman MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 991a163d034SWarner Losh M_WAITOK|M_ZERO); 9921158dfb7SGarrett Wollman sdl->sdl_len = sizeof *sdl; 9931158dfb7SGarrett Wollman sdl->sdl_family = AF_LINK; 9941158dfb7SGarrett Wollman sdl->sdl_index = ifp->if_index; 9951158dfb7SGarrett Wollman sdl->sdl_type = IFT_ETHER; 9961158dfb7SGarrett Wollman sdl->sdl_alen = ETHER_ADDR_LEN; 9971158dfb7SGarrett Wollman e_addr = LLADDR(sdl); 9981158dfb7SGarrett Wollman ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 9991158dfb7SGarrett Wollman *llsa = (struct sockaddr *)sdl; 10001158dfb7SGarrett Wollman return 0; 10011158dfb7SGarrett Wollman #endif 100282cd038dSYoshinobu Inoue #ifdef INET6 100382cd038dSYoshinobu Inoue case AF_INET6: 100482cd038dSYoshinobu Inoue sin6 = (struct sockaddr_in6 *)sa; 1005595b8a1cSJun-ichiro itojun Hagino if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1006595b8a1cSJun-ichiro itojun Hagino /* 1007595b8a1cSJun-ichiro itojun Hagino * An IP6 address of 0 means listen to all 1008595b8a1cSJun-ichiro itojun Hagino * of the Ethernet multicast address used for IP6. 1009595b8a1cSJun-ichiro itojun Hagino * (This is used for multicast routers.) 1010595b8a1cSJun-ichiro itojun Hagino */ 1011595b8a1cSJun-ichiro itojun Hagino ifp->if_flags |= IFF_ALLMULTI; 1012595b8a1cSJun-ichiro itojun Hagino *llsa = 0; 1013595b8a1cSJun-ichiro itojun Hagino return 0; 1014595b8a1cSJun-ichiro itojun Hagino } 101582cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 101682cd038dSYoshinobu Inoue return EADDRNOTAVAIL; 101782cd038dSYoshinobu Inoue MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1018a163d034SWarner Losh M_WAITOK|M_ZERO); 101982cd038dSYoshinobu Inoue sdl->sdl_len = sizeof *sdl; 102082cd038dSYoshinobu Inoue sdl->sdl_family = AF_LINK; 102182cd038dSYoshinobu Inoue sdl->sdl_index = ifp->if_index; 102282cd038dSYoshinobu Inoue sdl->sdl_type = IFT_ETHER; 102382cd038dSYoshinobu Inoue sdl->sdl_alen = ETHER_ADDR_LEN; 102482cd038dSYoshinobu Inoue e_addr = LLADDR(sdl); 102582cd038dSYoshinobu Inoue ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 102682cd038dSYoshinobu Inoue *llsa = (struct sockaddr *)sdl; 102782cd038dSYoshinobu Inoue return 0; 102882cd038dSYoshinobu Inoue #endif 10291158dfb7SGarrett Wollman 10301158dfb7SGarrett Wollman default: 10311158dfb7SGarrett Wollman /* 10321158dfb7SGarrett Wollman * Well, the text isn't quite right, but it's the name 10331158dfb7SGarrett Wollman * that counts... 10341158dfb7SGarrett Wollman */ 10351158dfb7SGarrett Wollman return EAFNOSUPPORT; 10361158dfb7SGarrett Wollman } 10371158dfb7SGarrett Wollman } 103840811c14SMatthew N. Dodd 103940811c14SMatthew N. Dodd static moduledata_t ether_mod = { 104040811c14SMatthew N. Dodd "ether", 104140811c14SMatthew N. Dodd NULL, 104240811c14SMatthew N. Dodd 0 104340811c14SMatthew N. Dodd }; 104440811c14SMatthew N. Dodd 104540811c14SMatthew N. Dodd DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 104640811c14SMatthew N. Dodd MODULE_VERSION(ether, 1); 1047