Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.319
      1  1.319   thorpej /*	$NetBSD: if_ethersubr.c,v 1.319 2022/09/03 02:24:59 thorpej Exp $	*/
      2   1.44    itojun 
      3   1.44    itojun /*
      4   1.44    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.44    itojun  * All rights reserved.
      6  1.120     perry  *
      7   1.44    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.44    itojun  * modification, are permitted provided that the following conditions
      9   1.44    itojun  * are met:
     10   1.44    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.44    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.44    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.44    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.44    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.44    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.44    itojun  *    may be used to endorse or promote products derived from this software
     17   1.44    itojun  *    without specific prior written permission.
     18  1.120     perry  *
     19   1.44    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.44    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.44    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.44    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.44    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.44    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.44    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.44    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.44    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.44    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.44    itojun  * SUCH DAMAGE.
     30   1.44    itojun  */
     31    1.9       cgd 
     32    1.1       cgd /*
     33    1.8   mycroft  * Copyright (c) 1982, 1989, 1993
     34    1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     35    1.1       cgd  *
     36    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37    1.1       cgd  * modification, are permitted provided that the following conditions
     38    1.1       cgd  * are met:
     39    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44  1.113       agc  * 3. Neither the name of the University nor the names of its contributors
     45    1.1       cgd  *    may be used to endorse or promote products derived from this software
     46    1.1       cgd  *    without specific prior written permission.
     47    1.1       cgd  *
     48    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58    1.1       cgd  * SUCH DAMAGE.
     59    1.1       cgd  *
     60   1.27      fvdl  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
     61    1.1       cgd  */
     62   1.90     lukem 
     63   1.90     lukem #include <sys/cdefs.h>
     64  1.319   thorpej __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.319 2022/09/03 02:24:59 thorpej Exp $");
     65    1.1       cgd 
     66  1.212     pooka #ifdef _KERNEL_OPT
     67   1.33  jonathan #include "opt_inet.h"
     68   1.33  jonathan #include "opt_atalk.h"
     69  1.112    martin #include "opt_mbuftrace.h"
     70  1.182    kefren #include "opt_mpls.h"
     71   1.30      matt #include "opt_gateway.h"
     72  1.135     pavel #include "opt_pppoe.h"
     73  1.206     ozaki #include "opt_net_mpsafe.h"
     74  1.212     pooka #endif
     75  1.212     pooka 
     76   1.59   thorpej #include "vlan.h"
     77   1.81    martin #include "pppoe.h"
     78   1.78   thorpej #include "bridge.h"
     79   1.93    martin #include "arp.h"
     80  1.121      yamt #include "agr.h"
     81   1.30      matt 
     82  1.200     joerg #include <sys/sysctl.h>
     83    1.4   mycroft #include <sys/mbuf.h>
     84  1.211     ozaki #include <sys/mutex.h>
     85    1.4   mycroft #include <sys/ioctl.h>
     86    1.4   mycroft #include <sys/errno.h>
     87  1.158    dyoung #include <sys/device.h>
     88  1.284  riastrad #include <sys/entropy.h>
     89  1.207  riastrad #include <sys/rndsource.h>
     90  1.216     ozaki #include <sys/cpu.h>
     91  1.231     ozaki #include <sys/kmem.h>
     92  1.297  yamaguch #include <sys/hook.h>
     93    1.8   mycroft 
     94    1.4   mycroft #include <net/if.h>
     95    1.4   mycroft #include <net/netisr.h>
     96    1.4   mycroft #include <net/route.h>
     97    1.4   mycroft #include <net/if_llc.h>
     98    1.4   mycroft #include <net/if_dl.h>
     99    1.8   mycroft #include <net/if_types.h>
    100  1.211     ozaki #include <net/pktqueue.h>
    101   1.93    martin 
    102  1.158    dyoung #include <net/if_media.h>
    103  1.158    dyoung #include <dev/mii/mii.h>
    104  1.158    dyoung #include <dev/mii/miivar.h>
    105  1.158    dyoung 
    106   1.93    martin #if NARP == 0
    107   1.93    martin /*
    108  1.102      jmmv  * XXX there should really be a way to issue this warning from within config(8)
    109   1.93    martin  */
    110  1.111    martin #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
    111   1.93    martin #endif
    112    1.1       cgd 
    113   1.69   thorpej #include <net/bpf.h>
    114   1.69   thorpej 
    115   1.22        is #include <net/if_ether.h>
    116   1.59   thorpej #include <net/if_vlanvar.h>
    117   1.22        is 
    118   1.81    martin #if NPPPOE > 0
    119   1.81    martin #include <net/if_pppoe.h>
    120   1.81    martin #endif
    121   1.81    martin 
    122  1.121      yamt #if NAGR > 0
    123  1.306  yamaguch #include <net/ether_slowprotocols.h>
    124  1.121      yamt #include <net/agr/ieee8023ad.h>
    125  1.121      yamt #include <net/agr/if_agrvar.h>
    126  1.121      yamt #endif
    127  1.121      yamt 
    128   1.78   thorpej #if NBRIDGE > 0
    129   1.78   thorpej #include <net/if_bridgevar.h>
    130   1.78   thorpej #endif
    131   1.78   thorpej 
    132   1.15      phil #include <netinet/in.h>
    133    1.1       cgd #ifdef INET
    134    1.4   mycroft #include <netinet/in_var.h>
    135    1.1       cgd #endif
    136   1.22        is #include <netinet/if_inarp.h>
    137    1.1       cgd 
    138   1.44    itojun #ifdef INET6
    139   1.44    itojun #ifndef INET
    140   1.44    itojun #include <netinet/in.h>
    141   1.44    itojun #endif
    142   1.44    itojun #include <netinet6/in6_var.h>
    143   1.44    itojun #include <netinet6/nd6.h>
    144   1.44    itojun #endif
    145   1.44    itojun 
    146  1.133  liamjfoy #include "carp.h"
    147  1.133  liamjfoy #if NCARP > 0
    148  1.133  liamjfoy #include <netinet/ip_carp.h>
    149  1.133  liamjfoy #endif
    150  1.133  liamjfoy 
    151   1.23  christos #ifdef NETATALK
    152   1.23  christos #include <netatalk/at.h>
    153   1.23  christos #include <netatalk/at_var.h>
    154   1.23  christos #include <netatalk/at_extern.h>
    155   1.23  christos 
    156   1.23  christos #define llc_snap_org_code llc_un.type_snap.org_code
    157   1.23  christos #define llc_snap_ether_type llc_un.type_snap.ether_type
    158   1.23  christos 
    159   1.23  christos extern u_char	at_org_code[3];
    160   1.23  christos extern u_char	aarp_org_code[3];
    161   1.23  christos #endif /* NETATALK */
    162   1.23  christos 
    163  1.182    kefren #ifdef MPLS
    164  1.182    kefren #include <netmpls/mpls.h>
    165  1.182    kefren #include <netmpls/mpls_var.h>
    166  1.182    kefren #endif
    167  1.182    kefren 
    168  1.292       roy CTASSERT(sizeof(struct ether_addr) == 6);
    169  1.292       roy CTASSERT(sizeof(struct ether_header) == 14);
    170  1.292       roy 
    171  1.278   msaitoh #ifdef DIAGNOSTIC
    172  1.123      matt static struct timeval bigpktppslim_last;
    173  1.123      matt static int bigpktppslim = 2;	/* XXX */
    174  1.123      matt static int bigpktpps_count;
    175  1.203     ozaki static kmutex_t bigpktpps_lock __cacheline_aligned;
    176  1.278   msaitoh #endif
    177  1.123      matt 
    178  1.118      yamt const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
    179  1.118      yamt     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    180  1.121      yamt const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
    181  1.121      yamt     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
    182    1.8   mycroft #define senderr(e) { error = (e); goto bad;}
    183    1.1       cgd 
    184  1.301  knakahar static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
    185  1.301  knakahar 
    186  1.261      maxv static int ether_output(struct ifnet *, struct mbuf *,
    187  1.261      maxv     const struct sockaddr *, const struct rtentry *);
    188   1.42   thorpej 
    189    1.1       cgd /*
    190    1.1       cgd  * Ethernet output routine.
    191    1.1       cgd  * Encapsulate a packet of type family for the local net.
    192   1.22        is  * Assumes that ifp is actually pointer to ethercom structure.
    193    1.1       cgd  */
    194   1.42   thorpej static int
    195  1.178    dyoung ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
    196  1.256      maxv     const struct sockaddr * const dst, const struct rtentry *rt)
    197    1.1       cgd {
    198  1.256      maxv 	uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
    199  1.162      matt 	uint16_t etype = 0;
    200  1.122  christos 	int error = 0, hdrcmplt = 0;
    201   1.29       mrg 	struct mbuf *m = m0;
    202  1.151    dyoung 	struct mbuf *mcopy = NULL;
    203   1.29       mrg 	struct ether_header *eh;
    204  1.133  liamjfoy 	struct ifnet *ifp = ifp0;
    205   1.24  christos #ifdef INET
    206   1.22        is 	struct arphdr *ah;
    207  1.260      maxv #endif
    208   1.23  christos #ifdef NETATALK
    209   1.23  christos 	struct at_ifaddr *aa;
    210  1.260      maxv #endif
    211    1.1       cgd 
    212  1.104      matt #ifdef MBUFTRACE
    213  1.116  jonathan 	m_claimm(m, ifp->if_mowner);
    214  1.104      matt #endif
    215  1.133  liamjfoy 
    216  1.133  liamjfoy #if NCARP > 0
    217  1.133  liamjfoy 	if (ifp->if_type == IFT_CARP) {
    218  1.133  liamjfoy 		struct ifaddr *ifa;
    219  1.227     ozaki 		int s = pserialize_read_enter();
    220  1.133  liamjfoy 
    221  1.133  liamjfoy 		/* loop back if this is going to the carp interface */
    222  1.133  liamjfoy 		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
    223  1.227     ozaki 		    (ifa = ifa_ifwithaddr(dst)) != NULL) {
    224  1.227     ozaki 			if (ifa->ifa_ifp == ifp0) {
    225  1.227     ozaki 				pserialize_read_exit(s);
    226  1.227     ozaki 				return looutput(ifp0, m, dst, rt);
    227  1.227     ozaki 			}
    228  1.227     ozaki 		}
    229  1.227     ozaki 		pserialize_read_exit(s);
    230  1.133  liamjfoy 
    231  1.133  liamjfoy 		ifp = ifp->if_carpdev;
    232  1.133  liamjfoy 		/* ac = (struct arpcom *)ifp; */
    233  1.133  liamjfoy 
    234  1.247   msaitoh 		if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
    235  1.247   msaitoh 		    (IFF_UP | IFF_RUNNING))
    236  1.133  liamjfoy 			senderr(ENETDOWN);
    237  1.133  liamjfoy 	}
    238  1.260      maxv #endif
    239  1.133  liamjfoy 
    240  1.247   msaitoh 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
    241    1.8   mycroft 		senderr(ENETDOWN);
    242   1.72   thorpej 
    243    1.1       cgd 	switch (dst->sa_family) {
    244    1.1       cgd 
    245    1.1       cgd #ifdef INET
    246    1.1       cgd 	case AF_INET:
    247  1.261      maxv 		if (m->m_flags & M_BCAST) {
    248  1.256      maxv 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    249  1.261      maxv 		} else if (m->m_flags & M_MCAST) {
    250  1.145    dyoung 			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
    251  1.261      maxv 		} else {
    252  1.281    kardel 			error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
    253  1.260      maxv 			if (error)
    254  1.260      maxv 				return (error == EWOULDBLOCK) ? 0 : error;
    255  1.224  knakahar 		}
    256    1.3   hpeyerl 		/* If broadcasting on a simplex interface, loopback a copy */
    257    1.3   hpeyerl 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    258  1.266      maxv 			mcopy = m_copypacket(m, M_DONTWAIT);
    259   1.17   mycroft 		etype = htons(ETHERTYPE_IP);
    260    1.8   mycroft 		break;
    261   1.22        is 
    262   1.22        is 	case AF_ARP:
    263   1.22        is 		ah = mtod(m, struct arphdr *);
    264  1.261      maxv 		if (m->m_flags & M_BCAST) {
    265  1.256      maxv 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    266  1.261      maxv 		} else {
    267  1.147  christos 			void *tha = ar_tha(ah);
    268  1.131       mrg 
    269  1.173  christos 			if (tha == NULL) {
    270  1.258      maxv 				/* fake with ARPHRD_IEEE1394 */
    271  1.236      maxv 				m_freem(m);
    272  1.173  christos 				return 0;
    273  1.173  christos 			}
    274  1.146    dyoung 			memcpy(edst, tha, sizeof(edst));
    275  1.131       mrg 		}
    276  1.120     perry 
    277   1.22        is 		ah->ar_hrd = htons(ARPHRD_ETHER);
    278   1.22        is 
    279  1.107    itojun 		switch (ntohs(ah->ar_op)) {
    280   1.22        is 		case ARPOP_REVREQUEST:
    281   1.22        is 		case ARPOP_REVREPLY:
    282   1.22        is 			etype = htons(ETHERTYPE_REVARP);
    283   1.22        is 			break;
    284   1.22        is 
    285   1.22        is 		case ARPOP_REQUEST:
    286   1.22        is 		case ARPOP_REPLY:
    287   1.22        is 		default:
    288   1.22        is 			etype = htons(ETHERTYPE_ARP);
    289   1.22        is 		}
    290   1.22        is 		break;
    291    1.1       cgd #endif
    292  1.256      maxv 
    293   1.44    itojun #ifdef INET6
    294   1.44    itojun 	case AF_INET6:
    295  1.261      maxv 		if (m->m_flags & M_BCAST) {
    296  1.256      maxv 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    297  1.261      maxv 		} else if (m->m_flags & M_MCAST) {
    298  1.238     ozaki 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
    299  1.238     ozaki 			    edst);
    300  1.238     ozaki 		} else {
    301  1.281    kardel 			error = nd6_resolve(ifp0, rt, m, dst, edst,
    302  1.238     ozaki 			    sizeof(edst));
    303  1.261      maxv 			if (error)
    304  1.261      maxv 				return (error == EWOULDBLOCK) ? 0 : error;
    305   1.51    itojun 		}
    306   1.44    itojun 		etype = htons(ETHERTYPE_IPV6);
    307   1.44    itojun 		break;
    308   1.44    itojun #endif
    309  1.256      maxv 
    310   1.23  christos #ifdef NETATALK
    311  1.250      maxv 	case AF_APPLETALK: {
    312  1.227     ozaki 		struct ifaddr *ifa;
    313  1.227     ozaki 		int s;
    314  1.227     ozaki 
    315  1.224  knakahar 		KERNEL_LOCK(1, NULL);
    316  1.256      maxv 
    317  1.226       rjs 		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
    318  1.224  knakahar 			KERNEL_UNLOCK_ONE(NULL);
    319  1.261      maxv 			return 0;
    320   1.23  christos 		}
    321  1.256      maxv 
    322   1.23  christos 		/*
    323   1.23  christos 		 * ifaddr is the first thing in at_ifaddr
    324   1.23  christos 		 */
    325  1.227     ozaki 		s = pserialize_read_enter();
    326  1.227     ozaki 		ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
    327  1.227     ozaki 		if (ifa == NULL) {
    328  1.227     ozaki 			pserialize_read_exit(s);
    329  1.227     ozaki 			KERNEL_UNLOCK_ONE(NULL);
    330  1.260      maxv 			senderr(EADDRNOTAVAIL);
    331  1.224  knakahar 		}
    332  1.227     ozaki 		aa = (struct at_ifaddr *)ifa;
    333  1.120     perry 
    334   1.23  christos 		/*
    335   1.23  christos 		 * In the phase 2 case, we need to prepend an mbuf for the
    336  1.265      maxv 		 * llc header.
    337   1.23  christos 		 */
    338   1.23  christos 		if (aa->aa_flags & AFA_PHASE2) {
    339   1.23  christos 			struct llc llc;
    340   1.23  christos 
    341   1.43    bouyer 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    342  1.256      maxv 			if (m == NULL) {
    343  1.260      maxv 				pserialize_read_exit(s);
    344  1.256      maxv 				KERNEL_UNLOCK_ONE(NULL);
    345  1.256      maxv 				senderr(ENOBUFS);
    346  1.256      maxv 			}
    347  1.256      maxv 
    348   1.23  christos 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
    349   1.23  christos 			llc.llc_control = LLC_UI;
    350  1.146    dyoung 			memcpy(llc.llc_snap_org_code, at_org_code,
    351   1.23  christos 			    sizeof(llc.llc_snap_org_code));
    352   1.38       kim 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
    353  1.147  christos 			memcpy(mtod(m, void *), &llc, sizeof(struct llc));
    354   1.23  christos 		} else {
    355   1.38       kim 			etype = htons(ETHERTYPE_ATALK);
    356   1.23  christos 		}
    357  1.227     ozaki 		pserialize_read_exit(s);
    358  1.224  knakahar 		KERNEL_UNLOCK_ONE(NULL);
    359   1.23  christos 		break;
    360  1.250      maxv 	}
    361   1.23  christos #endif /* NETATALK */
    362  1.256      maxv 
    363   1.31   thorpej 	case pseudo_AF_HDRCMPLT:
    364   1.31   thorpej 		hdrcmplt = 1;
    365  1.146    dyoung 		memcpy(esrc,
    366  1.146    dyoung 		    ((const struct ether_header *)dst->sa_data)->ether_shost,
    367  1.146    dyoung 		    sizeof(esrc));
    368   1.31   thorpej 		/* FALLTHROUGH */
    369   1.31   thorpej 
    370    1.1       cgd 	case AF_UNSPEC:
    371  1.250      maxv 		memcpy(edst,
    372  1.146    dyoung 		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
    373  1.146    dyoung 		    sizeof(edst));
    374    1.8   mycroft 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    375  1.145    dyoung 		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
    376    1.8   mycroft 		break;
    377    1.1       cgd 
    378    1.1       cgd 	default:
    379   1.21  christos 		printf("%s: can't handle af%d\n", ifp->if_xname,
    380  1.261      maxv 		    dst->sa_family);
    381    1.8   mycroft 		senderr(EAFNOSUPPORT);
    382    1.1       cgd 	}
    383    1.1       cgd 
    384  1.182    kefren #ifdef MPLS
    385  1.210     ozaki 	{
    386  1.210     ozaki 		struct m_tag *mtag;
    387  1.271      maxv 		mtag = m_tag_find(m, PACKET_TAG_MPLS);
    388  1.210     ozaki 		if (mtag != NULL) {
    389  1.210     ozaki 			/* Having the tag itself indicates it's MPLS */
    390  1.184    dyoung 			etype = htons(ETHERTYPE_MPLS);
    391  1.210     ozaki 			m_tag_delete(m, mtag);
    392  1.210     ozaki 		}
    393  1.184    dyoung 	}
    394  1.182    kefren #endif
    395  1.182    kefren 
    396    1.1       cgd 	if (mcopy)
    397  1.145    dyoung 		(void)looutput(ifp, mcopy, dst, rt);
    398   1.16   mycroft 
    399  1.256      maxv 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    400  1.256      maxv 
    401  1.256      maxv 	/*
    402  1.256      maxv 	 * If no ether type is set, this must be a 802.2 formatted packet.
    403   1.50      matt 	 */
    404   1.50      matt 	if (etype == 0)
    405   1.50      matt 		etype = htons(m->m_pkthdr.len);
    406  1.256      maxv 
    407    1.1       cgd 	/*
    408  1.256      maxv 	 * Add local net header. If no space in first mbuf, allocate another.
    409    1.1       cgd 	 */
    410  1.256      maxv 	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
    411  1.256      maxv 	if (m == NULL)
    412    1.8   mycroft 		senderr(ENOBUFS);
    413  1.256      maxv 
    414    1.1       cgd 	eh = mtod(m, struct ether_header *);
    415   1.96   thorpej 	/* Note: etype is already in network byte order. */
    416  1.256      maxv 	memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
    417  1.250      maxv 	memcpy(eh->ether_dhost, edst, sizeof(edst));
    418  1.261      maxv 	if (hdrcmplt) {
    419  1.146    dyoung 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
    420  1.261      maxv 	} else {
    421  1.153    dyoung 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
    422   1.31   thorpej 		    sizeof(eh->ether_shost));
    423  1.261      maxv 	}
    424   1.77   thorpej 
    425  1.133  liamjfoy #if NCARP > 0
    426  1.133  liamjfoy 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
    427  1.153    dyoung 	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
    428  1.133  liamjfoy 		    sizeof(eh->ether_shost));
    429  1.133  liamjfoy 	}
    430  1.256      maxv #endif
    431  1.133  liamjfoy 
    432  1.195     rmind 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
    433  1.261      maxv 		return error;
    434   1.77   thorpej 	if (m == NULL)
    435  1.261      maxv 		return 0;
    436   1.77   thorpej 
    437   1.78   thorpej #if NBRIDGE > 0
    438   1.78   thorpej 	/*
    439   1.78   thorpej 	 * Bridges require special output handling.
    440   1.78   thorpej 	 */
    441   1.78   thorpej 	if (ifp->if_bridge)
    442  1.256      maxv 		return bridge_output(ifp, m, NULL, NULL);
    443   1.78   thorpej #endif
    444   1.78   thorpej 
    445  1.133  liamjfoy #if NCARP > 0
    446  1.133  liamjfoy 	if (ifp != ifp0)
    447  1.282   thorpej 		if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
    448  1.256      maxv #endif
    449  1.133  liamjfoy 
    450   1.77   thorpej #ifdef ALTQ
    451  1.224  knakahar 	KERNEL_LOCK(1, NULL);
    452   1.77   thorpej 	/*
    453   1.77   thorpej 	 * If ALTQ is enabled on the parent interface, do
    454   1.77   thorpej 	 * classification; the queueing discipline might not
    455   1.77   thorpej 	 * require classification, but might require the
    456   1.77   thorpej 	 * address family/header pointer in the pktattr.
    457   1.77   thorpej 	 */
    458   1.77   thorpej 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    459  1.220  knakahar 		altq_etherclassify(&ifp->if_snd, m);
    460  1.224  knakahar 	KERNEL_UNLOCK_ONE(NULL);
    461   1.77   thorpej #endif
    462  1.221  knakahar 	return ifq_enqueue(ifp, m);
    463    1.1       cgd 
    464    1.1       cgd bad:
    465  1.288     ozaki 	if_statinc(ifp, if_oerrors);
    466    1.1       cgd 	if (m)
    467    1.1       cgd 		m_freem(m);
    468  1.261      maxv 	return error;
    469    1.1       cgd }
    470   1.76   thorpej 
    471   1.76   thorpej #ifdef ALTQ
    472   1.76   thorpej /*
    473   1.76   thorpej  * This routine is a slight hack to allow a packet to be classified
    474   1.76   thorpej  * if the Ethernet headers are present.  It will go away when ALTQ's
    475   1.76   thorpej  * classification engine understands link headers.
    476  1.261      maxv  *
    477  1.261      maxv  * XXX: We may need to do m_pullups here. First to ensure struct ether_header
    478  1.261      maxv  * is indeed contiguous, then to read the LLC and so on.
    479   1.76   thorpej  */
    480   1.76   thorpej void
    481  1.220  knakahar altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
    482   1.76   thorpej {
    483   1.76   thorpej 	struct ether_header *eh;
    484  1.255      maxv 	struct mbuf *mtop = m;
    485  1.162      matt 	uint16_t ether_type;
    486   1.76   thorpej 	int hlen, af, hdrsize;
    487  1.147  christos 	void *hdr;
    488   1.76   thorpej 
    489  1.255      maxv 	KASSERT((mtop->m_flags & M_PKTHDR) != 0);
    490  1.255      maxv 
    491   1.76   thorpej 	hlen = ETHER_HDR_LEN;
    492   1.76   thorpej 	eh = mtod(m, struct ether_header *);
    493   1.76   thorpej 
    494   1.76   thorpej 	ether_type = htons(eh->ether_type);
    495   1.76   thorpej 
    496   1.76   thorpej 	if (ether_type < ETHERMTU) {
    497   1.76   thorpej 		/* LLC/SNAP */
    498   1.76   thorpej 		struct llc *llc = (struct llc *)(eh + 1);
    499   1.76   thorpej 		hlen += 8;
    500   1.76   thorpej 
    501   1.76   thorpej 		if (m->m_len < hlen ||
    502   1.76   thorpej 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    503   1.76   thorpej 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    504   1.76   thorpej 		    llc->llc_control != LLC_UI) {
    505   1.76   thorpej 			/* Not SNAP. */
    506   1.76   thorpej 			goto bad;
    507   1.76   thorpej 		}
    508   1.76   thorpej 
    509   1.76   thorpej 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    510   1.76   thorpej 	}
    511   1.76   thorpej 
    512   1.76   thorpej 	switch (ether_type) {
    513   1.76   thorpej 	case ETHERTYPE_IP:
    514   1.76   thorpej 		af = AF_INET;
    515   1.76   thorpej 		hdrsize = 20;		/* sizeof(struct ip) */
    516   1.76   thorpej 		break;
    517   1.76   thorpej 
    518   1.76   thorpej 	case ETHERTYPE_IPV6:
    519   1.76   thorpej 		af = AF_INET6;
    520   1.76   thorpej 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    521   1.76   thorpej 		break;
    522   1.76   thorpej 
    523   1.76   thorpej 	default:
    524   1.76   thorpej 		af = AF_UNSPEC;
    525   1.76   thorpej 		hdrsize = 0;
    526   1.76   thorpej 		break;
    527   1.76   thorpej 	}
    528   1.76   thorpej 
    529   1.95    itojun 	while (m->m_len <= hlen) {
    530   1.95    itojun 		hlen -= m->m_len;
    531   1.95    itojun 		m = m->m_next;
    532  1.255      maxv 		if (m == NULL)
    533  1.255      maxv 			goto bad;
    534   1.95    itojun 	}
    535  1.255      maxv 
    536   1.76   thorpej 	if (m->m_len < (hlen + hdrsize)) {
    537   1.76   thorpej 		/*
    538   1.95    itojun 		 * protocol header not in a single mbuf.
    539   1.95    itojun 		 * We can't cope with this situation right
    540   1.76   thorpej 		 * now (but it shouldn't ever happen, really, anyhow).
    541   1.76   thorpej 		 */
    542   1.92    itojun #ifdef DEBUG
    543   1.78   thorpej 		printf("altq_etherclassify: headers span multiple mbufs: "
    544   1.78   thorpej 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
    545   1.92    itojun #endif
    546   1.76   thorpej 		goto bad;
    547   1.76   thorpej 	}
    548   1.76   thorpej 
    549   1.76   thorpej 	m->m_data += hlen;
    550   1.76   thorpej 	m->m_len -= hlen;
    551   1.76   thorpej 
    552  1.147  christos 	hdr = mtod(m, void *);
    553   1.76   thorpej 
    554  1.255      maxv 	if (ALTQ_NEEDS_CLASSIFY(ifq)) {
    555  1.255      maxv 		mtop->m_pkthdr.pattr_class =
    556   1.76   thorpej 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    557  1.255      maxv 	}
    558  1.255      maxv 	mtop->m_pkthdr.pattr_af = af;
    559  1.255      maxv 	mtop->m_pkthdr.pattr_hdr = hdr;
    560   1.76   thorpej 
    561   1.76   thorpej 	m->m_data -= hlen;
    562   1.76   thorpej 	m->m_len += hlen;
    563   1.76   thorpej 
    564   1.76   thorpej 	return;
    565   1.76   thorpej 
    566  1.250      maxv bad:
    567  1.255      maxv 	mtop->m_pkthdr.pattr_class = NULL;
    568  1.255      maxv 	mtop->m_pkthdr.pattr_hdr = NULL;
    569  1.255      maxv 	mtop->m_pkthdr.pattr_af = AF_UNSPEC;
    570   1.76   thorpej }
    571   1.76   thorpej #endif /* ALTQ */
    572    1.1       cgd 
    573  1.286     ozaki #if defined (LLC) || defined (NETATALK)
    574  1.286     ozaki static void
    575  1.286     ozaki ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
    576  1.286     ozaki {
    577  1.318   thorpej 	pktqueue_t *pktq = NULL;
    578  1.286     ozaki 	struct llc *l;
    579  1.286     ozaki 
    580  1.286     ozaki 	if (m->m_len < sizeof(*eh) + sizeof(struct llc))
    581  1.305   msaitoh 		goto error;
    582  1.286     ozaki 
    583  1.286     ozaki 	l = (struct llc *)(eh+1);
    584  1.286     ozaki 	switch (l->llc_dsap) {
    585  1.286     ozaki #ifdef NETATALK
    586  1.286     ozaki 	case LLC_SNAP_LSAP:
    587  1.286     ozaki 		switch (l->llc_control) {
    588  1.286     ozaki 		case LLC_UI:
    589  1.286     ozaki 			if (l->llc_ssap != LLC_SNAP_LSAP)
    590  1.305   msaitoh 				goto error;
    591  1.286     ozaki 
    592  1.286     ozaki 			if (memcmp(&(l->llc_snap_org_code)[0],
    593  1.286     ozaki 			    at_org_code, sizeof(at_org_code)) == 0 &&
    594  1.286     ozaki 			    ntohs(l->llc_snap_ether_type) ==
    595  1.286     ozaki 			    ETHERTYPE_ATALK) {
    596  1.318   thorpej 				pktq = at_pktq2;
    597  1.286     ozaki 				m_adj(m, sizeof(struct ether_header)
    598  1.286     ozaki 				    + sizeof(struct llc));
    599  1.286     ozaki 				break;
    600  1.286     ozaki 			}
    601  1.286     ozaki 
    602  1.286     ozaki 			if (memcmp(&(l->llc_snap_org_code)[0],
    603  1.286     ozaki 			    aarp_org_code,
    604  1.286     ozaki 			    sizeof(aarp_org_code)) == 0 &&
    605  1.286     ozaki 			    ntohs(l->llc_snap_ether_type) ==
    606  1.286     ozaki 			    ETHERTYPE_AARP) {
    607  1.286     ozaki 				m_adj(m, sizeof(struct ether_header)
    608  1.286     ozaki 				    + sizeof(struct llc));
    609  1.286     ozaki 				aarpinput(ifp, m); /* XXX queue? */
    610  1.286     ozaki 				return;
    611  1.286     ozaki 			}
    612  1.286     ozaki 
    613  1.286     ozaki 		default:
    614  1.305   msaitoh 			goto error;
    615  1.286     ozaki 		}
    616  1.286     ozaki 		break;
    617  1.286     ozaki #endif
    618  1.286     ozaki 	default:
    619  1.305   msaitoh 		goto noproto;
    620  1.286     ozaki 	}
    621  1.286     ozaki 
    622  1.318   thorpej 	KASSERT(pktq != NULL);
    623  1.318   thorpej 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    624  1.318   thorpej 		m_freem(m);
    625  1.318   thorpej 	}
    626  1.286     ozaki 	return;
    627  1.286     ozaki 
    628  1.305   msaitoh noproto:
    629  1.305   msaitoh 	m_freem(m);
    630  1.305   msaitoh 	if_statinc(ifp, if_noproto);
    631  1.305   msaitoh 	return;
    632  1.305   msaitoh error:
    633  1.286     ozaki 	m_freem(m);
    634  1.305   msaitoh 	if_statinc(ifp, if_ierrors);
    635  1.286     ozaki 	return;
    636  1.286     ozaki }
    637  1.286     ozaki #endif /* defined (LLC) || defined (NETATALK) */
    638  1.286     ozaki 
    639    1.1       cgd /*
    640    1.1       cgd  * Process a received Ethernet packet;
    641   1.42   thorpej  * the packet is in the mbuf chain m with
    642   1.42   thorpej  * the ether header.
    643    1.1       cgd  */
    644  1.133  liamjfoy void
    645   1.58      matt ether_input(struct ifnet *ifp, struct mbuf *m)
    646    1.1       cgd {
    647  1.315    martin #if NVLAN > 0 || defined(MBUFTRACE)
    648   1.91   thorpej 	struct ethercom *ec = (struct ethercom *) ifp;
    649  1.315    martin #endif
    650  1.199     rmind 	pktqueue_t *pktq = NULL;
    651  1.162      matt 	uint16_t etype;
    652   1.42   thorpej 	struct ether_header *eh;
    653  1.187      matt 	size_t ehlen;
    654  1.204       tls 	static int earlypkts;
    655    1.1       cgd 
    656  1.316   thorpej 	/* No RPS for not-IP. */
    657  1.316   thorpej 	pktq_rps_hash_func_t rps_hash = NULL;
    658  1.316   thorpej 
    659  1.216     ozaki 	KASSERT(!cpu_intr_p());
    660  1.253      maxv 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    661  1.216     ozaki 
    662  1.287     ozaki 	if ((ifp->if_flags & IFF_UP) == 0)
    663  1.287     ozaki 		goto drop;
    664  1.290       roy 
    665  1.291       roy #ifdef MBUFTRACE
    666  1.291       roy 	m_claimm(m, &ec->ec_rx_mowner);
    667  1.291       roy #endif
    668  1.291       roy 
    669  1.292       roy 	if (__predict_false(m->m_len < sizeof(*eh))) {
    670  1.303  christos 		if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
    671  1.303  christos 			if_statinc(ifp, if_ierrors);
    672  1.303  christos 			return;
    673  1.303  christos 		}
    674  1.267      maxv 	}
    675   1.42   thorpej 
    676   1.42   thorpej 	eh = mtod(m, struct ether_header *);
    677   1.63   thorpej 	etype = ntohs(eh->ether_type);
    678  1.187      matt 	ehlen = sizeof(*eh);
    679   1.63   thorpej 
    680  1.284  riastrad 	if (__predict_false(earlypkts < 100 ||
    681  1.284  riastrad 		entropy_epoch() == (unsigned)-1)) {
    682  1.204       tls 		rnd_add_data(NULL, eh, ehlen, 0);
    683  1.204       tls 		earlypkts++;
    684  1.204       tls 	}
    685  1.204       tls 
    686   1.63   thorpej 	/*
    687  1.258      maxv 	 * Determine if the packet is within its size limits. For MPLS the
    688  1.258      maxv 	 * header length is variable, so we skip the check.
    689   1.63   thorpej 	 */
    690  1.182    kefren 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
    691   1.83   thorpej 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
    692  1.278   msaitoh #ifdef DIAGNOSTIC
    693  1.203     ozaki 		mutex_enter(&bigpktpps_lock);
    694  1.123      matt 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
    695  1.261      maxv 		    bigpktppslim)) {
    696  1.123      matt 			printf("%s: discarding oversize frame (len=%d)\n",
    697  1.123      matt 			    ifp->if_xname, m->m_pkthdr.len);
    698  1.123      matt 		}
    699  1.203     ozaki 		mutex_exit(&bigpktpps_lock);
    700  1.278   msaitoh #endif
    701  1.305   msaitoh 		goto error;
    702   1.63   thorpej 	}
    703   1.77   thorpej 
    704   1.84   thorpej 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    705   1.84   thorpej 		/*
    706   1.84   thorpej 		 * If this is not a simplex interface, drop the packet
    707   1.84   thorpej 		 * if it came from us.
    708   1.84   thorpej 		 */
    709   1.84   thorpej 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
    710  1.153    dyoung 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
    711   1.84   thorpej 		    ETHER_ADDR_LEN) == 0) {
    712  1.287     ozaki 			goto drop;
    713   1.84   thorpej 		}
    714   1.84   thorpej 
    715   1.84   thorpej 		if (memcmp(etherbroadcastaddr,
    716   1.84   thorpej 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    717   1.84   thorpej 			m->m_flags |= M_BCAST;
    718   1.84   thorpej 		else
    719   1.84   thorpej 			m->m_flags |= M_MCAST;
    720  1.282   thorpej 		if_statinc(ifp, if_imcasts);
    721   1.84   thorpej 	}
    722   1.84   thorpej 
    723   1.79   thorpej 	/* If the CRC is still on the packet, trim it off. */
    724   1.79   thorpej 	if (m->m_flags & M_HASFCS) {
    725   1.79   thorpej 		m_adj(m, -ETHER_CRC_LEN);
    726   1.79   thorpej 		m->m_flags &= ~M_HASFCS;
    727   1.79   thorpej 	}
    728   1.79   thorpej 
    729  1.282   thorpej 	if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
    730   1.78   thorpej 
    731  1.313  yamaguch 	if (!vlan_has_tag(m) && etype == ETHERTYPE_VLAN) {
    732  1.313  yamaguch 		m = ether_strip_vlantag(m);
    733  1.313  yamaguch 		if (m == NULL) {
    734  1.313  yamaguch 			if_statinc(ifp, if_ierrors);
    735  1.313  yamaguch 			return;
    736  1.313  yamaguch 		}
    737  1.313  yamaguch 
    738  1.313  yamaguch 		eh = mtod(m, struct ether_header *);
    739  1.313  yamaguch 		etype = ntohs(eh->ether_type);
    740  1.313  yamaguch 		ehlen = sizeof(*eh);
    741  1.313  yamaguch 	}
    742  1.313  yamaguch 
    743  1.247   msaitoh 	if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
    744  1.201     ozaki 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
    745  1.201     ozaki 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    746  1.261      maxv 	     ETHER_ADDR_LEN) != 0) {
    747  1.201     ozaki 		m->m_flags |= M_PROMISC;
    748   1.70    bouyer 	}
    749   1.78   thorpej 
    750  1.106    bouyer 	if ((m->m_flags & M_PROMISC) == 0) {
    751  1.195     rmind 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    752  1.106    bouyer 			return;
    753  1.106    bouyer 		if (m == NULL)
    754  1.106    bouyer 			return;
    755   1.78   thorpej 
    756  1.106    bouyer 		eh = mtod(m, struct ether_header *);
    757  1.106    bouyer 		etype = ntohs(eh->ether_type);
    758  1.106    bouyer 	}
    759   1.70    bouyer 
    760  1.312  yamaguch 	/*
    761  1.312  yamaguch 	 * Processing a logical interfaces that are able
    762  1.312  yamaguch 	 * to configure vlan(4).
    763  1.312  yamaguch 	*/
    764  1.172    darran #if NAGR > 0
    765  1.311  yamaguch 	if (ifp->if_lagg != NULL &&
    766  1.172    darran 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    767  1.172    darran 		m->m_flags &= ~M_PROMISC;
    768  1.172    darran 		agr_input(ifp, m);
    769  1.172    darran 		return;
    770  1.172    darran 	}
    771  1.253      maxv #endif
    772  1.172    darran 
    773   1.91   thorpej 	/*
    774  1.312  yamaguch 	 * VLAN processing.
    775  1.312  yamaguch 	 *
    776  1.312  yamaguch 	 * VLAN provides service delimiting so the frames are
    777  1.312  yamaguch 	 * processed before other handlings. If a VLAN interface
    778  1.312  yamaguch 	 * does not exist to take those frames, they're returned
    779  1.312  yamaguch 	 * to ether_input().
    780   1.91   thorpej 	 */
    781    1.1       cgd 
    782  1.313  yamaguch 	if (vlan_has_tag(m)) {
    783  1.313  yamaguch 		if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) {
    784  1.312  yamaguch 			if (etype == ETHERTYPE_VLAN ||
    785  1.312  yamaguch 			     etype == ETHERTYPE_QINQ)
    786  1.312  yamaguch 				goto drop;
    787  1.312  yamaguch 
    788  1.312  yamaguch 			/* XXX we should actually use the prio value? */
    789  1.312  yamaguch 			m->m_flags &= ~M_VLANTAG;
    790  1.312  yamaguch 		} else {
    791   1.59   thorpej #if NVLAN > 0
    792  1.312  yamaguch 			if (ec->ec_nvlans > 0) {
    793  1.312  yamaguch 				m = vlan_input(ifp, m);
    794  1.312  yamaguch 
    795  1.312  yamaguch 				/* vlan_input() called ether_input() recursively */
    796  1.312  yamaguch 				if (m == NULL)
    797  1.312  yamaguch 					return;
    798  1.312  yamaguch 			}
    799  1.312  yamaguch #endif
    800  1.312  yamaguch 			/* drop VLAN frames not for this port. */
    801  1.312  yamaguch 			goto noproto;
    802  1.312  yamaguch 		}
    803  1.312  yamaguch 	}
    804  1.312  yamaguch 
    805  1.312  yamaguch #if NCARP > 0
    806  1.312  yamaguch 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    807   1.59   thorpej 		/*
    808  1.312  yamaguch 		 * Clear M_PROMISC, in case the packet comes from a
    809  1.312  yamaguch 		 * vlan.
    810   1.59   thorpej 		 */
    811  1.312  yamaguch 		m->m_flags &= ~M_PROMISC;
    812  1.312  yamaguch 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
    813  1.312  yamaguch 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    814  1.287     ozaki 			return;
    815  1.312  yamaguch 	}
    816  1.253      maxv #endif
    817  1.253      maxv 
    818  1.312  yamaguch 	/*
    819  1.312  yamaguch 	 * Handle protocols that expect to have the Ethernet header
    820  1.312  yamaguch 	 * (and possibly FCS) intact.
    821  1.312  yamaguch 	 */
    822  1.312  yamaguch 	switch (etype) {
    823   1.81    martin #if NPPPOE > 0
    824   1.81    martin 	case ETHERTYPE_PPPOEDISC:
    825  1.218     ozaki 		pppoedisc_input(ifp, m);
    826  1.218     ozaki 		return;
    827  1.253      maxv 
    828   1.81    martin 	case ETHERTYPE_PPPOE:
    829  1.218     ozaki 		pppoe_input(ifp, m);
    830   1.81    martin 		return;
    831  1.253      maxv #endif
    832  1.253      maxv 
    833  1.121      yamt 	case ETHERTYPE_SLOWPROTOCOLS: {
    834  1.121      yamt 		uint8_t subtype;
    835  1.121      yamt 
    836  1.287     ozaki 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
    837  1.305   msaitoh 			goto error;
    838  1.253      maxv 
    839  1.121      yamt 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    840  1.121      yamt 		switch (subtype) {
    841  1.121      yamt #if NAGR > 0
    842  1.121      yamt 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    843  1.311  yamaguch 			if (ifp->if_lagg != NULL) {
    844  1.121      yamt 				ieee8023ad_lacp_input(ifp, m);
    845  1.121      yamt 				return;
    846  1.121      yamt 			}
    847  1.121      yamt 			break;
    848  1.121      yamt 
    849  1.121      yamt 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    850  1.311  yamaguch 			if (ifp->if_lagg != NULL) {
    851  1.121      yamt 				ieee8023ad_marker_input(ifp, m);
    852  1.121      yamt 				return;
    853  1.121      yamt 			}
    854  1.121      yamt 			break;
    855  1.261      maxv #endif
    856  1.253      maxv 
    857  1.121      yamt 		default:
    858  1.121      yamt 			if (subtype == 0 || subtype > 10) {
    859  1.121      yamt 				/* illegal value */
    860  1.303  christos 				goto error;
    861  1.121      yamt 			}
    862  1.121      yamt 			/* unknown subtype */
    863  1.121      yamt 			break;
    864  1.121      yamt 		}
    865  1.121      yamt 	}
    866  1.273       mrg 	/* FALLTHROUGH */
    867   1.59   thorpej 	default:
    868  1.287     ozaki 		if (m->m_flags & M_PROMISC)
    869  1.287     ozaki 			goto drop;
    870   1.59   thorpej 	}
    871   1.42   thorpej 
    872   1.45   thorpej 	/* If the CRC is still on the packet, trim it off. */
    873   1.79   thorpej 	if (m->m_flags & M_HASFCS) {
    874   1.45   thorpej 		m_adj(m, -ETHER_CRC_LEN);
    875   1.79   thorpej 		m->m_flags &= ~M_HASFCS;
    876   1.79   thorpej 	}
    877   1.42   thorpej 
    878  1.286     ozaki 	/* etype represents the size of the payload in this case */
    879  1.286     ozaki 	if (etype <= ETHERMTU + sizeof(struct ether_header)) {
    880  1.286     ozaki 		KASSERT(ehlen == sizeof(*eh));
    881  1.286     ozaki #if defined (LLC) || defined (NETATALK)
    882  1.286     ozaki 		ether_input_llc(ifp, m, eh);
    883  1.286     ozaki 		return;
    884  1.286     ozaki #else
    885  1.307   msaitoh 		/* ethertype of 0-1500 is regarded as noproto */
    886  1.305   msaitoh 		goto noproto;
    887  1.286     ozaki #endif
    888  1.286     ozaki 	}
    889  1.139        is 
    890  1.286     ozaki 	/* Strip off the Ethernet header. */
    891  1.286     ozaki 	m_adj(m, ehlen);
    892  1.286     ozaki 
    893  1.286     ozaki 	switch (etype) {
    894    1.1       cgd #ifdef INET
    895  1.286     ozaki 	case ETHERTYPE_IP:
    896   1.30      matt #ifdef GATEWAY
    897  1.286     ozaki 		if (ipflow_fastforward(m))
    898  1.286     ozaki 			return;
    899   1.30      matt #endif
    900  1.286     ozaki 		pktq = ip_pktq;
    901  1.316   thorpej 		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
    902  1.286     ozaki 		break;
    903    1.1       cgd 
    904  1.286     ozaki 	case ETHERTYPE_ARP:
    905  1.317   thorpej 		pktq = arp_pktq;
    906  1.286     ozaki 		break;
    907    1.7     glass 
    908  1.286     ozaki 	case ETHERTYPE_REVARP:
    909  1.286     ozaki 		revarpinput(m);	/* XXX queue? */
    910  1.286     ozaki 		return;
    911    1.1       cgd #endif
    912  1.253      maxv 
    913   1.44    itojun #ifdef INET6
    914  1.286     ozaki 	case ETHERTYPE_IPV6:
    915  1.287     ozaki 		if (__predict_false(!in6_present))
    916  1.305   msaitoh 			goto noproto;
    917  1.250      maxv #ifdef GATEWAY
    918  1.286     ozaki 		if (ip6flow_fastforward(&m))
    919  1.286     ozaki 			return;
    920  1.148  liamjfoy #endif
    921  1.286     ozaki 		pktq = ip6_pktq;
    922  1.316   thorpej 		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
    923  1.286     ozaki 		break;
    924   1.44    itojun #endif
    925  1.253      maxv 
    926   1.23  christos #ifdef NETATALK
    927  1.286     ozaki 	case ETHERTYPE_ATALK:
    928  1.318   thorpej 		pktq = at_pktq1;
    929  1.286     ozaki 		break;
    930  1.253      maxv 
    931  1.286     ozaki 	case ETHERTYPE_AARP:
    932  1.286     ozaki 		aarpinput(ifp, m); /* XXX queue? */
    933  1.286     ozaki 		return;
    934  1.253      maxv #endif
    935  1.253      maxv 
    936  1.182    kefren #ifdef MPLS
    937  1.286     ozaki 	case ETHERTYPE_MPLS:
    938  1.319   thorpej 		pktq = mpls_pktq;
    939  1.286     ozaki 		break;
    940  1.182    kefren #endif
    941  1.253      maxv 
    942  1.286     ozaki 	default:
    943  1.305   msaitoh 		goto noproto;
    944    1.1       cgd 	}
    945    1.1       cgd 
    946  1.319   thorpej 	KASSERT(pktq != NULL);
    947  1.319   thorpej 	const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0;
    948  1.319   thorpej 	if (__predict_false(!pktq_enqueue(pktq, m, h))) {
    949  1.319   thorpej 		m_freem(m);
    950  1.199     rmind 	}
    951  1.287     ozaki 	return;
    952  1.287     ozaki 
    953  1.287     ozaki drop:
    954  1.287     ozaki 	m_freem(m);
    955  1.305   msaitoh 	if_statinc(ifp, if_iqdrops);
    956  1.305   msaitoh 	return;
    957  1.305   msaitoh noproto:
    958  1.305   msaitoh 	m_freem(m);
    959  1.305   msaitoh 	if_statinc(ifp, if_noproto);
    960  1.303  christos 	return;
    961  1.303  christos error:
    962  1.303  christos 	m_freem(m);
    963  1.305   msaitoh 	if_statinc(ifp, if_ierrors);
    964  1.303  christos 	return;
    965    1.1       cgd }
    966    1.1       cgd 
    967  1.314  yamaguch static void
    968  1.314  yamaguch ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
    969  1.314  yamaguch {
    970  1.314  yamaguch 	struct ether_vlan_header evl;
    971  1.314  yamaguch 	struct m_hdr mh, md;
    972  1.314  yamaguch 
    973  1.314  yamaguch 	KASSERT(bp != NULL);
    974  1.314  yamaguch 
    975  1.314  yamaguch 	if (!vlan_has_tag(m)) {
    976  1.314  yamaguch 		bpf_mtap3(bp, m, direction);
    977  1.314  yamaguch 		return;
    978  1.314  yamaguch 	}
    979  1.314  yamaguch 
    980  1.314  yamaguch 	memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN);
    981  1.314  yamaguch 	evl.evl_proto = evl.evl_encap_proto;
    982  1.314  yamaguch 	evl.evl_encap_proto = htons(ETHERTYPE_VLAN);
    983  1.314  yamaguch 	evl.evl_tag = htons(vlan_get_tag(m));
    984  1.314  yamaguch 
    985  1.314  yamaguch 	md.mh_flags = 0;
    986  1.314  yamaguch 	md.mh_data = m->m_data + ETHER_HDR_LEN;
    987  1.314  yamaguch 	md.mh_len = m->m_len - ETHER_HDR_LEN;
    988  1.314  yamaguch 	md.mh_next = m->m_next;
    989  1.314  yamaguch 
    990  1.314  yamaguch 	mh.mh_flags = 0;
    991  1.314  yamaguch 	mh.mh_data = (char *)&evl;
    992  1.314  yamaguch 	mh.mh_len = sizeof(evl);
    993  1.314  yamaguch 	mh.mh_next = (struct mbuf *)&md;
    994  1.314  yamaguch 
    995  1.314  yamaguch 	bpf_mtap3(bp, (struct mbuf *)&mh, direction);
    996  1.314  yamaguch }
    997  1.314  yamaguch 
    998    1.1       cgd /*
    999    1.1       cgd  * Convert Ethernet address to printable (loggable) representation.
   1000    1.1       cgd  */
   1001    1.1       cgd char *
   1002   1.58      matt ether_sprintf(const u_char *ap)
   1003    1.1       cgd {
   1004  1.129  christos 	static char etherbuf[3 * ETHER_ADDR_LEN];
   1005  1.129  christos 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
   1006  1.129  christos }
   1007  1.129  christos 
   1008  1.129  christos char *
   1009  1.129  christos ether_snprintf(char *buf, size_t len, const u_char *ap)
   1010  1.129  christos {
   1011  1.129  christos 	char *cp = buf;
   1012  1.129  christos 	size_t i;
   1013    1.1       cgd 
   1014  1.129  christos 	for (i = 0; i < len / 3; i++) {
   1015  1.124  christos 		*cp++ = hexdigits[*ap >> 4];
   1016  1.124  christos 		*cp++ = hexdigits[*ap++ & 0xf];
   1017    1.1       cgd 		*cp++ = ':';
   1018    1.1       cgd 	}
   1019  1.129  christos 	*--cp = '\0';
   1020  1.129  christos 	return buf;
   1021    1.1       cgd }
   1022    1.8   mycroft 
   1023    1.8   mycroft /*
   1024    1.8   mycroft  * Perform common duties while attaching to interface list
   1025    1.8   mycroft  */
   1026    1.8   mycroft void
   1027  1.162      matt ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
   1028    1.8   mycroft {
   1029  1.104      matt 	struct ethercom *ec = (struct ethercom *)ifp;
   1030  1.297  yamaguch 	char xnamebuf[HOOKNAMSIZ];
   1031    1.8   mycroft 
   1032    1.8   mycroft 	ifp->if_type = IFT_ETHER;
   1033   1.94     enami 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1034   1.73   thorpej 	ifp->if_dlt = DLT_EN10MB;
   1035    1.8   mycroft 	ifp->if_mtu = ETHERMTU;
   1036   1.12   mycroft 	ifp->if_output = ether_output;
   1037  1.216     ozaki 	ifp->_if_input = ether_input;
   1038  1.314  yamaguch 	ifp->if_bpf_mtap = ether_bpf_mtap;
   1039   1.54   thorpej 	if (ifp->if_baudrate == 0)
   1040   1.54   thorpej 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1041   1.75   thorpej 
   1042  1.230     ozaki 	if (lla != NULL)
   1043  1.230     ozaki 		if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1044   1.75   thorpej 
   1045  1.104      matt 	LIST_INIT(&ec->ec_multiaddrs);
   1046  1.276   msaitoh 	SIMPLEQ_INIT(&ec->ec_vids);
   1047  1.233     ozaki 	ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   1048  1.274     ozaki 	ec->ec_flags = 0;
   1049   1.26        is 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1050  1.177     joerg 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1051  1.297  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf),
   1052  1.297  yamaguch 	    "%s-ether_ifdetachhooks", ifp->if_xname);
   1053  1.297  yamaguch 	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
   1054  1.104      matt #ifdef MBUFTRACE
   1055  1.283   thorpej 	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
   1056  1.283   thorpej 	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
   1057  1.104      matt 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1058  1.104      matt 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1059  1.104      matt 	ifp->if_mowner = &ec->ec_tx_mowner;
   1060  1.104      matt #endif
   1061   1.52   thorpej }
   1062   1.52   thorpej 
   1063   1.52   thorpej void
   1064   1.58      matt ether_ifdetach(struct ifnet *ifp)
   1065   1.52   thorpej {
   1066   1.63   thorpej 	struct ethercom *ec = (void *) ifp;
   1067   1.63   thorpej 	struct ether_multi *enm;
   1068   1.69   thorpej 
   1069  1.269     ozaki 	IFNET_ASSERT_UNLOCKED(ifp);
   1070  1.190  christos 	/*
   1071  1.190  christos 	 * Prevent further calls to ioctl (for example turning off
   1072  1.190  christos 	 * promiscuous mode from the bridge code), which eventually can
   1073  1.190  christos 	 * call if_init() which can cause panics because the interface
   1074  1.190  christos 	 * is in the process of being detached. Return device not configured
   1075  1.190  christos 	 * instead.
   1076  1.190  christos 	 */
   1077  1.280  christos 	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
   1078  1.280  christos 	    enxio);
   1079  1.190  christos 
   1080  1.297  yamaguch 	simplehook_dohooks(ec->ec_ifdetach_hooks);
   1081  1.297  yamaguch 	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
   1082  1.297  yamaguch 	simplehook_destroy(ec->ec_ifdetach_hooks);
   1083  1.297  yamaguch 
   1084  1.177     joerg 	bpf_detach(ifp);
   1085   1.63   thorpej 
   1086  1.245   msaitoh 	ETHER_LOCK(ec);
   1087  1.276   msaitoh 	KASSERT(ec->ec_nvlans == 0);
   1088   1.63   thorpej 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1089   1.63   thorpej 		LIST_REMOVE(enm, enm_list);
   1090  1.248     ozaki 		kmem_free(enm, sizeof(*enm));
   1091   1.63   thorpej 		ec->ec_multicnt--;
   1092   1.63   thorpej 	}
   1093  1.245   msaitoh 	ETHER_UNLOCK(ec);
   1094   1.52   thorpej 
   1095  1.252      maxv 	mutex_obj_free(ec->ec_lock);
   1096  1.253      maxv 	ec->ec_lock = NULL;
   1097  1.231     ozaki 
   1098  1.189       chs 	ifp->if_mowner = NULL;
   1099  1.104      matt 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1100  1.104      matt 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1101   1.53   thorpej }
   1102   1.53   thorpej 
   1103  1.297  yamaguch void *
   1104  1.297  yamaguch ether_ifdetachhook_establish(struct ifnet *ifp,
   1105  1.297  yamaguch     void (*fn)(void *), void *arg)
   1106  1.297  yamaguch {
   1107  1.297  yamaguch 	struct ethercom *ec;
   1108  1.297  yamaguch 	khook_t *hk;
   1109  1.297  yamaguch 
   1110  1.297  yamaguch 	if (ifp->if_type != IFT_ETHER)
   1111  1.297  yamaguch 		return NULL;
   1112  1.297  yamaguch 
   1113  1.297  yamaguch 	ec = (struct ethercom *)ifp;
   1114  1.297  yamaguch 	hk = simplehook_establish(ec->ec_ifdetach_hooks,
   1115  1.297  yamaguch 	    fn, arg);
   1116  1.297  yamaguch 
   1117  1.297  yamaguch 	return (void *)hk;
   1118  1.297  yamaguch }
   1119  1.297  yamaguch 
   1120  1.297  yamaguch void
   1121  1.297  yamaguch ether_ifdetachhook_disestablish(struct ifnet *ifp,
   1122  1.297  yamaguch     void *vhook, kmutex_t *lock)
   1123  1.297  yamaguch {
   1124  1.297  yamaguch 	struct ethercom *ec;
   1125  1.297  yamaguch 
   1126  1.297  yamaguch 	if (vhook == NULL)
   1127  1.297  yamaguch 		return;
   1128  1.297  yamaguch 
   1129  1.297  yamaguch 	ec = (struct ethercom *)ifp;
   1130  1.297  yamaguch 	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
   1131  1.297  yamaguch }
   1132  1.297  yamaguch 
   1133   1.56   thorpej #if 0
   1134   1.56   thorpej /*
   1135   1.56   thorpej  * This is for reference.  We have a table-driven version
   1136   1.56   thorpej  * of the little-endian crc32 generator, which is faster
   1137   1.56   thorpej  * than the double-loop.
   1138   1.56   thorpej  */
   1139  1.162      matt uint32_t
   1140  1.162      matt ether_crc32_le(const uint8_t *buf, size_t len)
   1141   1.53   thorpej {
   1142  1.162      matt 	uint32_t c, crc, carry;
   1143   1.53   thorpej 	size_t i, j;
   1144   1.53   thorpej 
   1145   1.53   thorpej 	crc = 0xffffffffU;	/* initial value */
   1146   1.53   thorpej 
   1147   1.53   thorpej 	for (i = 0; i < len; i++) {
   1148   1.53   thorpej 		c = buf[i];
   1149   1.53   thorpej 		for (j = 0; j < 8; j++) {
   1150   1.53   thorpej 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1151   1.53   thorpej 			crc >>= 1;
   1152   1.53   thorpej 			c >>= 1;
   1153   1.53   thorpej 			if (carry)
   1154   1.56   thorpej 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1155   1.53   thorpej 		}
   1156   1.53   thorpej 	}
   1157   1.53   thorpej 
   1158   1.53   thorpej 	return (crc);
   1159   1.53   thorpej }
   1160   1.56   thorpej #else
   1161  1.162      matt uint32_t
   1162  1.162      matt ether_crc32_le(const uint8_t *buf, size_t len)
   1163   1.56   thorpej {
   1164  1.162      matt 	static const uint32_t crctab[] = {
   1165   1.56   thorpej 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1166   1.56   thorpej 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1167   1.56   thorpej 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1168   1.56   thorpej 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1169   1.56   thorpej 	};
   1170  1.162      matt 	uint32_t crc;
   1171   1.98   thorpej 	size_t i;
   1172   1.56   thorpej 
   1173   1.56   thorpej 	crc = 0xffffffffU;	/* initial value */
   1174   1.56   thorpej 
   1175   1.56   thorpej 	for (i = 0; i < len; i++) {
   1176   1.56   thorpej 		crc ^= buf[i];
   1177   1.56   thorpej 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1178   1.56   thorpej 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1179   1.56   thorpej 	}
   1180   1.56   thorpej 
   1181   1.56   thorpej 	return (crc);
   1182   1.56   thorpej }
   1183   1.56   thorpej #endif
   1184   1.53   thorpej 
   1185  1.162      matt uint32_t
   1186  1.162      matt ether_crc32_be(const uint8_t *buf, size_t len)
   1187   1.53   thorpej {
   1188  1.162      matt 	uint32_t c, crc, carry;
   1189   1.53   thorpej 	size_t i, j;
   1190   1.53   thorpej 
   1191   1.53   thorpej 	crc = 0xffffffffU;	/* initial value */
   1192   1.53   thorpej 
   1193   1.53   thorpej 	for (i = 0; i < len; i++) {
   1194   1.53   thorpej 		c = buf[i];
   1195   1.53   thorpej 		for (j = 0; j < 8; j++) {
   1196   1.53   thorpej 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1197   1.53   thorpej 			crc <<= 1;
   1198   1.53   thorpej 			c >>= 1;
   1199   1.53   thorpej 			if (carry)
   1200   1.53   thorpej 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1201   1.53   thorpej 		}
   1202   1.53   thorpej 	}
   1203   1.53   thorpej 
   1204   1.53   thorpej 	return (crc);
   1205    1.8   mycroft }
   1206    1.8   mycroft 
   1207   1.48        is #ifdef INET
   1208  1.118      yamt const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1209  1.118      yamt     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1210  1.118      yamt const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1211  1.118      yamt     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1212   1.48        is #endif
   1213   1.44    itojun #ifdef INET6
   1214  1.118      yamt const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1215  1.118      yamt     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1216  1.118      yamt const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1217  1.118      yamt     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1218   1.44    itojun #endif
   1219   1.60     enami 
   1220    1.3   hpeyerl /*
   1221  1.138    rpaulo  * ether_aton implementation, not using a static buffer.
   1222  1.138    rpaulo  */
   1223  1.138    rpaulo int
   1224  1.180  christos ether_aton_r(u_char *dest, size_t len, const char *str)
   1225  1.138    rpaulo {
   1226  1.250      maxv 	const u_char *cp = (const void *)str;
   1227  1.180  christos 	u_char *ep;
   1228  1.180  christos 
   1229  1.185   tsutsui #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1230  1.180  christos 
   1231  1.180  christos 	if (len < ETHER_ADDR_LEN)
   1232  1.180  christos 		return ENOSPC;
   1233  1.180  christos 
   1234  1.180  christos 	ep = dest + ETHER_ADDR_LEN;
   1235  1.250      maxv 
   1236  1.180  christos 	while (*cp) {
   1237  1.250      maxv 		if (!isxdigit(*cp))
   1238  1.250      maxv 			return EINVAL;
   1239  1.252      maxv 
   1240  1.180  christos 		*dest = atox(*cp);
   1241  1.180  christos 		cp++;
   1242  1.250      maxv 		if (isxdigit(*cp)) {
   1243  1.250      maxv 			*dest = (*dest << 4) | atox(*cp);
   1244  1.180  christos 			cp++;
   1245  1.250      maxv 		}
   1246  1.252      maxv 		dest++;
   1247  1.252      maxv 
   1248  1.180  christos 		if (dest == ep)
   1249  1.252      maxv 			return (*cp == '\0') ? 0 : ENAMETOOLONG;
   1250  1.252      maxv 
   1251  1.180  christos 		switch (*cp) {
   1252  1.180  christos 		case ':':
   1253  1.180  christos 		case '-':
   1254  1.180  christos 		case '.':
   1255  1.179  jakllsch 			cp++;
   1256  1.180  christos 			break;
   1257  1.179  jakllsch 		}
   1258  1.250      maxv 	}
   1259  1.180  christos 	return ENOBUFS;
   1260  1.138    rpaulo }
   1261  1.138    rpaulo 
   1262  1.138    rpaulo /*
   1263   1.60     enami  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1264   1.60     enami  * addresses.
   1265    1.3   hpeyerl  */
   1266    1.3   hpeyerl int
   1267  1.162      matt ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1268  1.162      matt     uint8_t addrhi[ETHER_ADDR_LEN])
   1269    1.3   hpeyerl {
   1270   1.24  christos #ifdef INET
   1271  1.155    dyoung 	const struct sockaddr_in *sin;
   1272  1.261      maxv #endif
   1273   1.44    itojun #ifdef INET6
   1274  1.155    dyoung 	const struct sockaddr_in6 *sin6;
   1275  1.261      maxv #endif
   1276    1.3   hpeyerl 
   1277   1.60     enami 	switch (sa->sa_family) {
   1278    1.3   hpeyerl 
   1279    1.3   hpeyerl 	case AF_UNSPEC:
   1280  1.146    dyoung 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1281  1.146    dyoung 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1282    1.3   hpeyerl 		break;
   1283    1.3   hpeyerl 
   1284    1.3   hpeyerl #ifdef INET
   1285    1.3   hpeyerl 	case AF_INET:
   1286  1.155    dyoung 		sin = satocsin(sa);
   1287    1.3   hpeyerl 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1288    1.3   hpeyerl 			/*
   1289   1.60     enami 			 * An IP address of INADDR_ANY means listen to
   1290   1.60     enami 			 * or stop listening to all of the Ethernet
   1291   1.60     enami 			 * multicast addresses used for IP.
   1292    1.3   hpeyerl 			 * (This is for the sake of IP multicast routers.)
   1293    1.3   hpeyerl 			 */
   1294  1.146    dyoung 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1295  1.146    dyoung 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1296  1.252      maxv 		} else {
   1297    1.3   hpeyerl 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1298  1.146    dyoung 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1299    1.3   hpeyerl 		}
   1300    1.3   hpeyerl 		break;
   1301    1.3   hpeyerl #endif
   1302   1.44    itojun #ifdef INET6
   1303   1.44    itojun 	case AF_INET6:
   1304  1.155    dyoung 		sin6 = satocsin6(sa);
   1305   1.47    itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1306   1.44    itojun 			/*
   1307   1.60     enami 			 * An IP6 address of 0 means listen to or stop
   1308   1.60     enami 			 * listening to all of the Ethernet multicast
   1309   1.60     enami 			 * address used for IP6.
   1310   1.44    itojun 			 * (This is used for multicast routers.)
   1311   1.44    itojun 			 */
   1312  1.146    dyoung 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1313  1.146    dyoung 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1314   1.44    itojun 		} else {
   1315   1.44    itojun 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1316  1.146    dyoung 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1317   1.44    itojun 		}
   1318   1.44    itojun 		break;
   1319   1.44    itojun #endif
   1320    1.3   hpeyerl 
   1321    1.3   hpeyerl 	default:
   1322  1.146    dyoung 		return EAFNOSUPPORT;
   1323   1.60     enami 	}
   1324  1.146    dyoung 	return 0;
   1325   1.60     enami }
   1326   1.60     enami 
   1327   1.60     enami /*
   1328   1.60     enami  * Add an Ethernet multicast address or range of addresses to the list for a
   1329   1.60     enami  * given interface.
   1330   1.60     enami  */
   1331   1.60     enami int
   1332  1.155    dyoung ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1333   1.60     enami {
   1334  1.231     ozaki 	struct ether_multi *enm, *_enm;
   1335   1.60     enami 	u_char addrlo[ETHER_ADDR_LEN];
   1336   1.60     enami 	u_char addrhi[ETHER_ADDR_LEN];
   1337  1.237     skrll 	int error = 0;
   1338  1.231     ozaki 
   1339  1.231     ozaki 	/* Allocate out of lock */
   1340  1.248     ozaki 	enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
   1341   1.60     enami 
   1342  1.245   msaitoh 	ETHER_LOCK(ec);
   1343  1.155    dyoung 	error = ether_multiaddr(sa, addrlo, addrhi);
   1344  1.231     ozaki 	if (error != 0)
   1345  1.231     ozaki 		goto out;
   1346    1.3   hpeyerl 
   1347    1.3   hpeyerl 	/*
   1348    1.3   hpeyerl 	 * Verify that we have valid Ethernet multicast addresses.
   1349    1.3   hpeyerl 	 */
   1350  1.186      yamt 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1351  1.231     ozaki 		error = EINVAL;
   1352  1.231     ozaki 		goto out;
   1353    1.3   hpeyerl 	}
   1354  1.252      maxv 
   1355    1.3   hpeyerl 	/*
   1356    1.3   hpeyerl 	 * See if the address range is already in the list.
   1357    1.3   hpeyerl 	 */
   1358  1.270  yamaguch 	_enm = ether_lookup_multi(addrlo, addrhi, ec);
   1359  1.231     ozaki 	if (_enm != NULL) {
   1360    1.3   hpeyerl 		/*
   1361    1.3   hpeyerl 		 * Found it; just increment the reference count.
   1362    1.3   hpeyerl 		 */
   1363  1.231     ozaki 		++_enm->enm_refcount;
   1364  1.231     ozaki 		error = 0;
   1365  1.231     ozaki 		goto out;
   1366    1.3   hpeyerl 	}
   1367  1.252      maxv 
   1368    1.3   hpeyerl 	/*
   1369  1.239     ozaki 	 * Link a new multicast record into the interface's multicast list.
   1370    1.3   hpeyerl 	 */
   1371  1.252      maxv 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
   1372  1.252      maxv 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
   1373    1.3   hpeyerl 	enm->enm_refcount = 1;
   1374   1.22        is 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1375   1.22        is 	ec->ec_multicnt++;
   1376  1.252      maxv 
   1377    1.3   hpeyerl 	/*
   1378    1.3   hpeyerl 	 * Return ENETRESET to inform the driver that the list has changed
   1379    1.3   hpeyerl 	 * and its reception filter should be adjusted accordingly.
   1380    1.3   hpeyerl 	 */
   1381  1.231     ozaki 	error = ENETRESET;
   1382  1.231     ozaki 	enm = NULL;
   1383  1.252      maxv 
   1384  1.231     ozaki out:
   1385  1.245   msaitoh 	ETHER_UNLOCK(ec);
   1386  1.231     ozaki 	if (enm != NULL)
   1387  1.248     ozaki 		kmem_free(enm, sizeof(*enm));
   1388  1.231     ozaki 	return error;
   1389    1.3   hpeyerl }
   1390    1.3   hpeyerl 
   1391    1.3   hpeyerl /*
   1392    1.3   hpeyerl  * Delete a multicast address record.
   1393    1.3   hpeyerl  */
   1394    1.3   hpeyerl int
   1395  1.155    dyoung ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1396    1.3   hpeyerl {
   1397   1.29       mrg 	struct ether_multi *enm;
   1398   1.60     enami 	u_char addrlo[ETHER_ADDR_LEN];
   1399   1.60     enami 	u_char addrhi[ETHER_ADDR_LEN];
   1400  1.237     skrll 	int error;
   1401    1.3   hpeyerl 
   1402  1.245   msaitoh 	ETHER_LOCK(ec);
   1403  1.155    dyoung 	error = ether_multiaddr(sa, addrlo, addrhi);
   1404  1.231     ozaki 	if (error != 0)
   1405  1.231     ozaki 		goto error;
   1406    1.3   hpeyerl 
   1407    1.3   hpeyerl 	/*
   1408  1.252      maxv 	 * Look up the address in our list.
   1409    1.3   hpeyerl 	 */
   1410  1.270  yamaguch 	enm = ether_lookup_multi(addrlo, addrhi, ec);
   1411    1.3   hpeyerl 	if (enm == NULL) {
   1412  1.231     ozaki 		error = ENXIO;
   1413  1.231     ozaki 		goto error;
   1414    1.3   hpeyerl 	}
   1415    1.3   hpeyerl 	if (--enm->enm_refcount != 0) {
   1416    1.3   hpeyerl 		/*
   1417    1.3   hpeyerl 		 * Still some claims to this record.
   1418    1.3   hpeyerl 		 */
   1419  1.231     ozaki 		error = 0;
   1420  1.231     ozaki 		goto error;
   1421    1.3   hpeyerl 	}
   1422  1.252      maxv 
   1423    1.3   hpeyerl 	/*
   1424    1.3   hpeyerl 	 * No remaining claims to this record; unlink and free it.
   1425    1.3   hpeyerl 	 */
   1426   1.13   mycroft 	LIST_REMOVE(enm, enm_list);
   1427   1.22        is 	ec->ec_multicnt--;
   1428  1.245   msaitoh 	ETHER_UNLOCK(ec);
   1429  1.252      maxv 	kmem_free(enm, sizeof(*enm));
   1430  1.231     ozaki 
   1431    1.3   hpeyerl 	/*
   1432    1.3   hpeyerl 	 * Return ENETRESET to inform the driver that the list has changed
   1433    1.3   hpeyerl 	 * and its reception filter should be adjusted accordingly.
   1434    1.3   hpeyerl 	 */
   1435  1.231     ozaki 	return ENETRESET;
   1436  1.252      maxv 
   1437  1.231     ozaki error:
   1438  1.245   msaitoh 	ETHER_UNLOCK(ec);
   1439  1.231     ozaki 	return error;
   1440   1.66   thorpej }
   1441   1.66   thorpej 
   1442  1.170    dyoung void
   1443  1.170    dyoung ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1444  1.170    dyoung {
   1445  1.170    dyoung 	ec->ec_ifflags_cb = cb;
   1446  1.170    dyoung }
   1447  1.170    dyoung 
   1448  1.276   msaitoh void
   1449  1.276   msaitoh ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
   1450  1.276   msaitoh {
   1451  1.276   msaitoh 
   1452  1.276   msaitoh 	ec->ec_vlan_cb = cb;
   1453  1.276   msaitoh }
   1454  1.276   msaitoh 
   1455  1.272   msaitoh static int
   1456  1.272   msaitoh ether_ioctl_reinit(struct ethercom *ec)
   1457  1.272   msaitoh {
   1458  1.272   msaitoh 	struct ifnet *ifp = &ec->ec_if;
   1459  1.272   msaitoh 	int error;
   1460  1.272   msaitoh 
   1461  1.310  riastrad 	KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
   1462  1.310  riastrad 
   1463  1.272   msaitoh 	switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1464  1.272   msaitoh 	case IFF_RUNNING:
   1465  1.272   msaitoh 		/*
   1466  1.272   msaitoh 		 * If interface is marked down and it is running,
   1467  1.272   msaitoh 		 * then stop and disable it.
   1468  1.272   msaitoh 		 */
   1469  1.308  riastrad 		if_stop(ifp, 1);
   1470  1.272   msaitoh 		break;
   1471  1.272   msaitoh 	case IFF_UP:
   1472  1.272   msaitoh 		/*
   1473  1.272   msaitoh 		 * If interface is marked up and it is stopped, then
   1474  1.272   msaitoh 		 * start it.
   1475  1.272   msaitoh 		 */
   1476  1.309  riastrad 		return if_init(ifp);
   1477  1.272   msaitoh 	case IFF_UP | IFF_RUNNING:
   1478  1.272   msaitoh 		error = 0;
   1479  1.272   msaitoh 		if (ec->ec_ifflags_cb != NULL) {
   1480  1.272   msaitoh 			error = (*ec->ec_ifflags_cb)(ec);
   1481  1.272   msaitoh 			if (error == ENETRESET) {
   1482  1.272   msaitoh 				/*
   1483  1.272   msaitoh 				 * Reset the interface to pick up
   1484  1.272   msaitoh 				 * changes in any other flags that
   1485  1.272   msaitoh 				 * affect the hardware state.
   1486  1.272   msaitoh 				 */
   1487  1.309  riastrad 				return if_init(ifp);
   1488  1.272   msaitoh 			}
   1489  1.272   msaitoh 		} else
   1490  1.309  riastrad 			error = if_init(ifp);
   1491  1.272   msaitoh 		return error;
   1492  1.272   msaitoh 	case 0:
   1493  1.272   msaitoh 		break;
   1494  1.272   msaitoh 	}
   1495  1.272   msaitoh 
   1496  1.272   msaitoh 	return 0;
   1497  1.272   msaitoh }
   1498  1.272   msaitoh 
   1499   1.66   thorpej /*
   1500   1.66   thorpej  * Common ioctls for Ethernet interfaces.  Note, we must be
   1501   1.66   thorpej  * called at splnet().
   1502   1.66   thorpej  */
   1503   1.66   thorpej int
   1504  1.147  christos ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1505   1.66   thorpej {
   1506  1.272   msaitoh 	struct ethercom *ec = (void *)ifp;
   1507  1.193   msaitoh 	struct eccapreq *eccr;
   1508   1.66   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
   1509  1.170    dyoung 	struct if_laddrreq *iflr = data;
   1510  1.170    dyoung 	const struct sockaddr_dl *sdl;
   1511  1.170    dyoung 	static const uint8_t zero[ETHER_ADDR_LEN];
   1512  1.169    dyoung 	int error;
   1513   1.66   thorpej 
   1514   1.66   thorpej 	switch (cmd) {
   1515  1.170    dyoung 	case SIOCINITIFADDR:
   1516  1.191      matt 	    {
   1517  1.191      matt 		struct ifaddr *ifa = (struct ifaddr *)data;
   1518  1.191      matt 		if (ifa->ifa_addr->sa_family != AF_LINK
   1519  1.247   msaitoh 		    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   1520  1.247   msaitoh 		       (IFF_UP | IFF_RUNNING)) {
   1521  1.170    dyoung 			ifp->if_flags |= IFF_UP;
   1522  1.309  riastrad 			if ((error = if_init(ifp)) != 0)
   1523  1.170    dyoung 				return error;
   1524  1.170    dyoung 		}
   1525   1.66   thorpej #ifdef INET
   1526  1.191      matt 		if (ifa->ifa_addr->sa_family == AF_INET)
   1527  1.191      matt 			arp_ifinit(ifp, ifa);
   1528  1.252      maxv #endif
   1529  1.169    dyoung 		return 0;
   1530  1.191      matt 	    }
   1531   1.66   thorpej 
   1532   1.66   thorpej 	case SIOCSIFMTU:
   1533   1.82   thorpej 	    {
   1534   1.82   thorpej 		int maxmtu;
   1535   1.82   thorpej 
   1536   1.82   thorpej 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1537   1.82   thorpej 			maxmtu = ETHERMTU_JUMBO;
   1538   1.82   thorpej 		else
   1539   1.82   thorpej 			maxmtu = ETHERMTU;
   1540   1.82   thorpej 
   1541   1.82   thorpej 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1542  1.169    dyoung 			return EINVAL;
   1543  1.169    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1544  1.169    dyoung 			return error;
   1545  1.169    dyoung 		else if (ifp->if_flags & IFF_UP) {
   1546   1.88   thorpej 			/* Make sure the device notices the MTU change. */
   1547  1.309  riastrad 			return if_init(ifp);
   1548  1.169    dyoung 		} else
   1549  1.169    dyoung 			return 0;
   1550   1.82   thorpej 	    }
   1551   1.66   thorpej 
   1552   1.66   thorpej 	case SIOCSIFFLAGS:
   1553  1.170    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1554  1.170    dyoung 			return error;
   1555  1.272   msaitoh 		return ether_ioctl_reinit(ec);
   1556  1.274     ozaki 	case SIOCGIFFLAGS:
   1557  1.274     ozaki 		error = ifioctl_common(ifp, cmd, data);
   1558  1.274     ozaki 		if (error == 0) {
   1559  1.274     ozaki 			/* Set IFF_ALLMULTI for backcompat */
   1560  1.274     ozaki 			ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
   1561  1.274     ozaki 			    IFF_ALLMULTI : 0;
   1562  1.274     ozaki 		}
   1563  1.274     ozaki 		return error;
   1564  1.193   msaitoh 	case SIOCGETHERCAP:
   1565  1.193   msaitoh 		eccr = (struct eccapreq *)data;
   1566  1.193   msaitoh 		eccr->eccr_capabilities = ec->ec_capabilities;
   1567  1.193   msaitoh 		eccr->eccr_capenable = ec->ec_capenable;
   1568  1.193   msaitoh 		return 0;
   1569  1.272   msaitoh 	case SIOCSETHERCAP:
   1570  1.272   msaitoh 		eccr = (struct eccapreq *)data;
   1571  1.272   msaitoh 		if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
   1572  1.272   msaitoh 			return EINVAL;
   1573  1.272   msaitoh 		if (eccr->eccr_capenable == ec->ec_capenable)
   1574  1.272   msaitoh 			return 0;
   1575  1.272   msaitoh #if 0 /* notyet */
   1576  1.272   msaitoh 		ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
   1577  1.272   msaitoh 		    | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
   1578  1.272   msaitoh #else
   1579  1.272   msaitoh 		ec->ec_capenable = eccr->eccr_capenable;
   1580  1.272   msaitoh #endif
   1581  1.272   msaitoh 		return ether_ioctl_reinit(ec);
   1582   1.66   thorpej 	case SIOCADDMULTI:
   1583  1.169    dyoung 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1584   1.66   thorpej 	case SIOCDELMULTI:
   1585  1.169    dyoung 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1586  1.160    dyoung 	case SIOCSIFMEDIA:
   1587  1.160    dyoung 	case SIOCGIFMEDIA:
   1588  1.275   msaitoh 		if (ec->ec_mii != NULL)
   1589  1.275   msaitoh 			return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
   1590  1.275   msaitoh 			    cmd);
   1591  1.275   msaitoh 		else if (ec->ec_ifmedia != NULL)
   1592  1.275   msaitoh 			return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
   1593  1.275   msaitoh 		else
   1594  1.169    dyoung 			return ENOTTY;
   1595  1.275   msaitoh 		break;
   1596  1.170    dyoung 	case SIOCALIFADDR:
   1597  1.170    dyoung 		sdl = satocsdl(sstocsa(&iflr->addr));
   1598  1.170    dyoung 		if (sdl->sdl_family != AF_LINK)
   1599  1.170    dyoung 			;
   1600  1.170    dyoung 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1601  1.170    dyoung 			return EINVAL;
   1602  1.170    dyoung 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1603  1.170    dyoung 			return EINVAL;
   1604  1.170    dyoung 		/*FALLTHROUGH*/
   1605  1.170    dyoung 	default:
   1606  1.161    dyoung 		return ifioctl_common(ifp, cmd, data);
   1607   1.66   thorpej 	}
   1608  1.169    dyoung 	return 0;
   1609    1.3   hpeyerl }
   1610  1.200     joerg 
   1611  1.215  christos /*
   1612  1.215  christos  * Enable/disable passing VLAN packets if the parent interface supports it.
   1613  1.215  christos  * Return:
   1614  1.215  christos  * 	 0: Ok
   1615  1.215  christos  *	-1: Parent interface does not support vlans
   1616  1.215  christos  *	>0: Error
   1617  1.215  christos  */
   1618  1.215  christos int
   1619  1.215  christos ether_enable_vlan_mtu(struct ifnet *ifp)
   1620  1.215  christos {
   1621  1.215  christos 	int error;
   1622  1.215  christos 	struct ethercom *ec = (void *)ifp;
   1623  1.215  christos 
   1624  1.215  christos 	/* Parent does not support VLAN's */
   1625  1.215  christos 	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
   1626  1.215  christos 		return -1;
   1627  1.215  christos 
   1628  1.215  christos 	/*
   1629  1.215  christos 	 * Parent supports the VLAN_MTU capability,
   1630  1.215  christos 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
   1631  1.215  christos 	 * enable it.
   1632  1.215  christos 	 */
   1633  1.215  christos 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1634  1.215  christos 
   1635  1.215  christos 	/* Interface is down, defer for later */
   1636  1.215  christos 	if ((ifp->if_flags & IFF_UP) == 0)
   1637  1.215  christos 		return 0;
   1638  1.215  christos 
   1639  1.215  christos 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1640  1.215  christos 		return 0;
   1641  1.215  christos 
   1642  1.215  christos 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1643  1.215  christos 	return error;
   1644  1.215  christos }
   1645  1.215  christos 
   1646  1.215  christos int
   1647  1.215  christos ether_disable_vlan_mtu(struct ifnet *ifp)
   1648  1.215  christos {
   1649  1.215  christos 	int error;
   1650  1.215  christos 	struct ethercom *ec = (void *)ifp;
   1651  1.215  christos 
   1652  1.215  christos 	/* We still have VLAN's, defer for later */
   1653  1.215  christos 	if (ec->ec_nvlans != 0)
   1654  1.215  christos 		return 0;
   1655  1.215  christos 
   1656  1.215  christos 	/* Parent does not support VLAB's, nothing to do. */
   1657  1.215  christos 	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
   1658  1.215  christos 		return -1;
   1659  1.215  christos 
   1660  1.215  christos 	/*
   1661  1.215  christos 	 * Disable Tx/Rx of VLAN-sized frames.
   1662  1.215  christos 	 */
   1663  1.215  christos 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1664  1.250      maxv 
   1665  1.215  christos 	/* Interface is down, defer for later */
   1666  1.215  christos 	if ((ifp->if_flags & IFF_UP) == 0)
   1667  1.215  christos 		return 0;
   1668  1.215  christos 
   1669  1.215  christos 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1670  1.215  christos 		return 0;
   1671  1.215  christos 
   1672  1.215  christos 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1673  1.215  christos 	return error;
   1674  1.215  christos }
   1675  1.215  christos 
   1676  1.304  yamaguch /*
   1677  1.304  yamaguch  * Add and delete VLAN TAG
   1678  1.304  yamaguch  */
   1679  1.304  yamaguch int
   1680  1.304  yamaguch ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
   1681  1.304  yamaguch {
   1682  1.304  yamaguch 	struct ethercom *ec = (void *)ifp;
   1683  1.304  yamaguch 	struct vlanid_list *vidp;
   1684  1.304  yamaguch 	bool vlanmtu_enabled;
   1685  1.304  yamaguch 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1686  1.304  yamaguch 	int error;
   1687  1.304  yamaguch 
   1688  1.304  yamaguch 	vlanmtu_enabled = false;
   1689  1.304  yamaguch 
   1690  1.304  yamaguch 	/* Add a vid to the list */
   1691  1.304  yamaguch 	vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
   1692  1.304  yamaguch 	vidp->vid = vid;
   1693  1.304  yamaguch 
   1694  1.304  yamaguch 	ETHER_LOCK(ec);
   1695  1.304  yamaguch 	ec->ec_nvlans++;
   1696  1.304  yamaguch 	SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
   1697  1.304  yamaguch 	ETHER_UNLOCK(ec);
   1698  1.304  yamaguch 
   1699  1.304  yamaguch 	if (ec->ec_nvlans == 1) {
   1700  1.304  yamaguch 		IFNET_LOCK(ifp);
   1701  1.304  yamaguch 		error = ether_enable_vlan_mtu(ifp);
   1702  1.304  yamaguch 		IFNET_UNLOCK(ifp);
   1703  1.304  yamaguch 
   1704  1.304  yamaguch 		if (error == 0) {
   1705  1.304  yamaguch 			vlanmtu_enabled = true;
   1706  1.304  yamaguch 		} else if (error != -1) {
   1707  1.304  yamaguch 			goto fail;
   1708  1.304  yamaguch 		}
   1709  1.304  yamaguch 	}
   1710  1.304  yamaguch 
   1711  1.304  yamaguch 	if (ec->ec_vlan_cb != NULL) {
   1712  1.304  yamaguch 		error = (*ec->ec_vlan_cb)(ec, vid, true);
   1713  1.304  yamaguch 		if (error != 0)
   1714  1.304  yamaguch 			goto fail;
   1715  1.304  yamaguch 	}
   1716  1.304  yamaguch 
   1717  1.304  yamaguch 	if (vlanmtu_status != NULL)
   1718  1.304  yamaguch 		*vlanmtu_status = vlanmtu_enabled;
   1719  1.304  yamaguch 
   1720  1.304  yamaguch 	return 0;
   1721  1.304  yamaguch fail:
   1722  1.304  yamaguch 	ETHER_LOCK(ec);
   1723  1.304  yamaguch 	ec->ec_nvlans--;
   1724  1.304  yamaguch 	SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
   1725  1.304  yamaguch 	ETHER_UNLOCK(ec);
   1726  1.304  yamaguch 
   1727  1.304  yamaguch 	if (vlanmtu_enabled) {
   1728  1.304  yamaguch 		IFNET_LOCK(ifp);
   1729  1.304  yamaguch 		(void)ether_disable_vlan_mtu(ifp);
   1730  1.304  yamaguch 		IFNET_UNLOCK(ifp);
   1731  1.304  yamaguch 	}
   1732  1.304  yamaguch 
   1733  1.304  yamaguch 	kmem_free(vidp, sizeof(*vidp));
   1734  1.304  yamaguch 
   1735  1.304  yamaguch 	return error;
   1736  1.304  yamaguch }
   1737  1.304  yamaguch 
   1738  1.304  yamaguch int
   1739  1.304  yamaguch ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
   1740  1.304  yamaguch {
   1741  1.304  yamaguch 	struct ethercom *ec = (void *)ifp;
   1742  1.304  yamaguch 	struct vlanid_list *vidp;
   1743  1.304  yamaguch 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1744  1.304  yamaguch 
   1745  1.304  yamaguch 	ETHER_LOCK(ec);
   1746  1.304  yamaguch 	SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
   1747  1.304  yamaguch 		if (vidp->vid == vid) {
   1748  1.304  yamaguch 			SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
   1749  1.304  yamaguch 			    vlanid_list, vid_list);
   1750  1.304  yamaguch 			ec->ec_nvlans--;
   1751  1.304  yamaguch 			break;
   1752  1.304  yamaguch 		}
   1753  1.304  yamaguch 	}
   1754  1.304  yamaguch 	ETHER_UNLOCK(ec);
   1755  1.304  yamaguch 
   1756  1.304  yamaguch 	if (vidp == NULL)
   1757  1.304  yamaguch 		return ENOENT;
   1758  1.304  yamaguch 
   1759  1.304  yamaguch 	if (ec->ec_vlan_cb != NULL) {
   1760  1.304  yamaguch 		(void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
   1761  1.304  yamaguch 	}
   1762  1.304  yamaguch 
   1763  1.304  yamaguch 	if (ec->ec_nvlans == 0) {
   1764  1.304  yamaguch 		IFNET_LOCK(ifp);
   1765  1.304  yamaguch 		(void)ether_disable_vlan_mtu(ifp);
   1766  1.304  yamaguch 		IFNET_UNLOCK(ifp);
   1767  1.304  yamaguch 	}
   1768  1.304  yamaguch 
   1769  1.304  yamaguch 	kmem_free(vidp, sizeof(*vidp));
   1770  1.304  yamaguch 
   1771  1.304  yamaguch 	return 0;
   1772  1.304  yamaguch }
   1773  1.304  yamaguch 
   1774  1.313  yamaguch int
   1775  1.313  yamaguch ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag)
   1776  1.313  yamaguch {
   1777  1.313  yamaguch 	static const size_t min_data_len =
   1778  1.313  yamaguch 	    ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
   1779  1.313  yamaguch 	/* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
   1780  1.313  yamaguch 	static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 };
   1781  1.313  yamaguch 
   1782  1.313  yamaguch 	struct ether_vlan_header *evl;
   1783  1.313  yamaguch 	struct mbuf *m = *mp;
   1784  1.313  yamaguch 	int error;
   1785  1.313  yamaguch 
   1786  1.313  yamaguch 	error = 0;
   1787  1.313  yamaguch 
   1788  1.313  yamaguch 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
   1789  1.313  yamaguch 	if (m == NULL) {
   1790  1.313  yamaguch 		error = ENOBUFS;
   1791  1.313  yamaguch 		goto out;
   1792  1.313  yamaguch 	}
   1793  1.313  yamaguch 
   1794  1.313  yamaguch 	if (m->m_len < sizeof(*evl)) {
   1795  1.313  yamaguch 		m = m_pullup(m, sizeof(*evl));
   1796  1.313  yamaguch 		if (m == NULL) {
   1797  1.313  yamaguch 			error = ENOBUFS;
   1798  1.313  yamaguch 			goto out;
   1799  1.313  yamaguch 		}
   1800  1.313  yamaguch 	}
   1801  1.313  yamaguch 
   1802  1.313  yamaguch 	/*
   1803  1.313  yamaguch 	 * Transform the Ethernet header into an
   1804  1.313  yamaguch 	 * Ethernet header with 802.1Q encapsulation.
   1805  1.313  yamaguch 	 */
   1806  1.313  yamaguch 	memmove(mtod(m, void *),
   1807  1.313  yamaguch 	    mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
   1808  1.313  yamaguch 	    sizeof(struct ether_header));
   1809  1.313  yamaguch 	evl = mtod(m, struct ether_vlan_header *);
   1810  1.313  yamaguch 	evl->evl_proto = evl->evl_encap_proto;
   1811  1.313  yamaguch 	evl->evl_encap_proto = htons(etype);
   1812  1.313  yamaguch 	evl->evl_tag = htons(tag);
   1813  1.313  yamaguch 
   1814  1.313  yamaguch 	/*
   1815  1.313  yamaguch 	 * To cater for VLAN-aware layer 2 ethernet
   1816  1.313  yamaguch 	 * switches which may need to strip the tag
   1817  1.313  yamaguch 	 * before forwarding the packet, make sure
   1818  1.313  yamaguch 	 * the packet+tag is at least 68 bytes long.
   1819  1.313  yamaguch 	 * This is necessary because our parent will
   1820  1.313  yamaguch 	 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1821  1.313  yamaguch 	 * some switches will not pad by themselves
   1822  1.313  yamaguch 	 * after deleting a tag.
   1823  1.313  yamaguch 	 */
   1824  1.313  yamaguch 	if (m->m_pkthdr.len < min_data_len) {
   1825  1.313  yamaguch 		m_copyback(m, m->m_pkthdr.len,
   1826  1.313  yamaguch 		    min_data_len - m->m_pkthdr.len,
   1827  1.313  yamaguch 		    vlan_zero_pad_buff);
   1828  1.313  yamaguch 	}
   1829  1.313  yamaguch 
   1830  1.313  yamaguch 	m->m_flags &= ~M_VLANTAG;
   1831  1.313  yamaguch 
   1832  1.313  yamaguch out:
   1833  1.313  yamaguch 	*mp = m;
   1834  1.313  yamaguch 	return error;
   1835  1.313  yamaguch }
   1836  1.313  yamaguch 
   1837  1.313  yamaguch struct mbuf *
   1838  1.313  yamaguch ether_strip_vlantag(struct mbuf *m)
   1839  1.313  yamaguch {
   1840  1.313  yamaguch 	struct ether_vlan_header *evl;
   1841  1.313  yamaguch 
   1842  1.313  yamaguch 	if (m->m_len < sizeof(*evl) &&
   1843  1.313  yamaguch 	    (m = m_pullup(m, sizeof(*evl))) == NULL) {
   1844  1.313  yamaguch 		return NULL;
   1845  1.313  yamaguch 	}
   1846  1.313  yamaguch 
   1847  1.313  yamaguch 	if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) {
   1848  1.313  yamaguch 		m_freem(m);
   1849  1.313  yamaguch 		return NULL;
   1850  1.313  yamaguch 	}
   1851  1.313  yamaguch 
   1852  1.313  yamaguch 	evl = mtod(m, struct ether_vlan_header *);
   1853  1.313  yamaguch 	KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
   1854  1.313  yamaguch 
   1855  1.313  yamaguch 	vlan_set_tag(m, ntohs(evl->evl_tag));
   1856  1.313  yamaguch 
   1857  1.313  yamaguch 	/*
   1858  1.313  yamaguch 	 * Restore the original ethertype.  We'll remove
   1859  1.313  yamaguch 	 * the encapsulation after we've found the vlan
   1860  1.313  yamaguch 	 * interface corresponding to the tag.
   1861  1.313  yamaguch 	 */
   1862  1.313  yamaguch 	evl->evl_encap_proto = evl->evl_proto;
   1863  1.313  yamaguch 
   1864  1.313  yamaguch 	/*
   1865  1.313  yamaguch 	 * Remove the encapsulation header and append tag.
   1866  1.313  yamaguch 	 * The original header has already been fixed up above.
   1867  1.313  yamaguch 	 */
   1868  1.313  yamaguch 	vlan_set_tag(m, ntohs(evl->evl_tag));
   1869  1.313  yamaguch 	memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
   1870  1.313  yamaguch 	    offsetof(struct ether_vlan_header, evl_encap_proto));
   1871  1.313  yamaguch 	m_adj(m, ETHER_VLAN_ENCAP_LEN);
   1872  1.313  yamaguch 
   1873  1.313  yamaguch 	return m;
   1874  1.313  yamaguch }
   1875  1.313  yamaguch 
   1876  1.200     joerg static int
   1877  1.200     joerg ether_multicast_sysctl(SYSCTLFN_ARGS)
   1878  1.200     joerg {
   1879  1.200     joerg 	struct ether_multi *enm;
   1880  1.200     joerg 	struct ifnet *ifp;
   1881  1.200     joerg 	struct ethercom *ec;
   1882  1.223     ozaki 	int error = 0;
   1883  1.200     joerg 	size_t written;
   1884  1.223     ozaki 	struct psref psref;
   1885  1.237     skrll 	int bound;
   1886  1.233     ozaki 	unsigned int multicnt;
   1887  1.233     ozaki 	struct ether_multi_sysctl *addrs;
   1888  1.233     ozaki 	int i;
   1889  1.200     joerg 
   1890  1.200     joerg 	if (namelen != 1)
   1891  1.200     joerg 		return EINVAL;
   1892  1.200     joerg 
   1893  1.223     ozaki 	bound = curlwp_bind();
   1894  1.223     ozaki 	ifp = if_get_byindex(name[0], &psref);
   1895  1.223     ozaki 	if (ifp == NULL) {
   1896  1.223     ozaki 		error = ENODEV;
   1897  1.223     ozaki 		goto out;
   1898  1.223     ozaki 	}
   1899  1.200     joerg 	if (ifp->if_type != IFT_ETHER) {
   1900  1.223     ozaki 		if_put(ifp, &psref);
   1901  1.200     joerg 		*oldlenp = 0;
   1902  1.223     ozaki 		goto out;
   1903  1.200     joerg 	}
   1904  1.200     joerg 	ec = (struct ethercom *)ifp;
   1905  1.200     joerg 
   1906  1.200     joerg 	if (oldp == NULL) {
   1907  1.223     ozaki 		if_put(ifp, &psref);
   1908  1.233     ozaki 		*oldlenp = ec->ec_multicnt * sizeof(*addrs);
   1909  1.223     ozaki 		goto out;
   1910  1.200     joerg 	}
   1911  1.200     joerg 
   1912  1.233     ozaki 	/*
   1913  1.233     ozaki 	 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
   1914  1.251      maxv 	 * is sleepable, while holding it. Copy data to a local buffer first
   1915  1.251      maxv 	 * with the lock taken and then call sysctl_copyout without holding it.
   1916  1.233     ozaki 	 */
   1917  1.233     ozaki retry:
   1918  1.233     ozaki 	multicnt = ec->ec_multicnt;
   1919  1.251      maxv 
   1920  1.251      maxv 	if (multicnt == 0) {
   1921  1.251      maxv 		if_put(ifp, &psref);
   1922  1.251      maxv 		*oldlenp = 0;
   1923  1.251      maxv 		goto out;
   1924  1.251      maxv 	}
   1925  1.251      maxv 
   1926  1.252      maxv 	addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
   1927  1.200     joerg 
   1928  1.245   msaitoh 	ETHER_LOCK(ec);
   1929  1.251      maxv 	if (multicnt != ec->ec_multicnt) {
   1930  1.251      maxv 		/* The number of multicast addresses has changed */
   1931  1.245   msaitoh 		ETHER_UNLOCK(ec);
   1932  1.233     ozaki 		kmem_free(addrs, sizeof(*addrs) * multicnt);
   1933  1.233     ozaki 		goto retry;
   1934  1.233     ozaki 	}
   1935  1.233     ozaki 
   1936  1.233     ozaki 	i = 0;
   1937  1.200     joerg 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
   1938  1.233     ozaki 		struct ether_multi_sysctl *addr = &addrs[i];
   1939  1.233     ozaki 		addr->enm_refcount = enm->enm_refcount;
   1940  1.233     ozaki 		memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
   1941  1.233     ozaki 		memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
   1942  1.233     ozaki 		i++;
   1943  1.233     ozaki 	}
   1944  1.245   msaitoh 	ETHER_UNLOCK(ec);
   1945  1.233     ozaki 
   1946  1.233     ozaki 	error = 0;
   1947  1.233     ozaki 	written = 0;
   1948  1.233     ozaki 	for (i = 0; i < multicnt; i++) {
   1949  1.233     ozaki 		struct ether_multi_sysctl *addr = &addrs[i];
   1950  1.233     ozaki 
   1951  1.233     ozaki 		if (written + sizeof(*addr) > *oldlenp)
   1952  1.200     joerg 			break;
   1953  1.233     ozaki 		error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
   1954  1.200     joerg 		if (error)
   1955  1.200     joerg 			break;
   1956  1.233     ozaki 		written += sizeof(*addr);
   1957  1.233     ozaki 		oldp = (char *)oldp + sizeof(*addr);
   1958  1.200     joerg 	}
   1959  1.233     ozaki 	kmem_free(addrs, sizeof(*addrs) * multicnt);
   1960  1.233     ozaki 
   1961  1.223     ozaki 	if_put(ifp, &psref);
   1962  1.200     joerg 
   1963  1.200     joerg 	*oldlenp = written;
   1964  1.223     ozaki out:
   1965  1.223     ozaki 	curlwp_bindx(bound);
   1966  1.200     joerg 	return error;
   1967  1.200     joerg }
   1968  1.200     joerg 
   1969  1.234     ozaki static void
   1970  1.234     ozaki ether_sysctl_setup(struct sysctllog **clog)
   1971  1.200     joerg {
   1972  1.200     joerg 	const struct sysctlnode *rnode = NULL;
   1973  1.200     joerg 
   1974  1.200     joerg 	sysctl_createv(clog, 0, NULL, &rnode,
   1975  1.200     joerg 		       CTLFLAG_PERMANENT,
   1976  1.200     joerg 		       CTLTYPE_NODE, "ether",
   1977  1.200     joerg 		       SYSCTL_DESCR("Ethernet-specific information"),
   1978  1.200     joerg 		       NULL, 0, NULL, 0,
   1979  1.200     joerg 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1980  1.200     joerg 
   1981  1.200     joerg 	sysctl_createv(clog, 0, &rnode, NULL,
   1982  1.200     joerg 		       CTLFLAG_PERMANENT,
   1983  1.200     joerg 		       CTLTYPE_NODE, "multicast",
   1984  1.200     joerg 		       SYSCTL_DESCR("multicast addresses"),
   1985  1.200     joerg 		       ether_multicast_sysctl, 0, NULL, 0,
   1986  1.200     joerg 		       CTL_CREATE, CTL_EOL);
   1987  1.301  knakahar 
   1988  1.301  knakahar 	sysctl_createv(clog, 0, &rnode, NULL,
   1989  1.301  knakahar 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1990  1.301  knakahar 		       CTLTYPE_STRING, "rps_hash",
   1991  1.301  knakahar 		       SYSCTL_DESCR("Interface rps hash function control"),
   1992  1.301  knakahar 		       sysctl_pktq_rps_hash_handler, 0, (void *)&ether_pktq_rps_hash_p,
   1993  1.301  knakahar 		       PKTQ_RPS_HASH_NAME_LEN,
   1994  1.301  knakahar 		       CTL_CREATE, CTL_EOL);
   1995  1.200     joerg }
   1996  1.203     ozaki 
   1997  1.203     ozaki void
   1998  1.203     ozaki etherinit(void)
   1999  1.203     ozaki {
   2000  1.234     ozaki 
   2001  1.278   msaitoh #ifdef DIAGNOSTIC
   2002  1.203     ozaki 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
   2003  1.278   msaitoh #endif
   2004  1.301  knakahar 	ether_pktq_rps_hash_p = pktq_rps_hash_default;
   2005  1.234     ozaki 	ether_sysctl_setup(NULL);
   2006  1.203     ozaki }
   2007