/* $NetBSD: if_ethersubr.c,v 1.330 2025/04/23 12:17:05 joe Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
*/
#if NARP == 0
/*
* XXX there should really be a way to issue this warning from within config(8)
*/
#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.
#endif
#if NCARP > 0
if (ifp->if_type == IFT_CARP) {
struct ifaddr *ifa;
int s = pserialize_read_enter();
/* loop back if this is going to the carp interface */
if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
(ifa = ifa_ifwithaddr(dst)) != NULL) {
if (ifa->ifa_ifp == ifp0) {
pserialize_read_exit(s);
return looutput(ifp0, m, dst, rt);
}
}
pserialize_read_exit(s);
ifp = ifp->if_carpdev;
/* ac = (struct arpcom *)ifp; */
if (tha == NULL) {
/* fake with ARPHRD_IEEE1394 */
m_freem(m);
return 0;
}
memcpy(edst, tha, sizeof(edst));
}
ah->ar_hrd = htons(ARPHRD_ETHER);
switch (ntohs(ah->ar_op)) {
case ARPOP_REVREQUEST:
case ARPOP_REVREPLY:
etype = htons(ETHERTYPE_REVARP);
break;
case ARPOP_REQUEST:
case ARPOP_REPLY:
default:
etype = htons(ETHERTYPE_ARP);
}
break;
#endif
#ifdef INET6
case AF_INET6:
if (m->m_flags & M_BCAST) {
memcpy(edst, etherbroadcastaddr, sizeof(edst));
} else if (m->m_flags & M_MCAST) {
ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
edst);
} else {
error = nd6_resolve(ifp0, rt, m, dst, edst,
sizeof(edst));
if (error)
return (error == EWOULDBLOCK) ? 0 : error;
}
etype = htons(ETHERTYPE_IPV6);
break;
#endif
#ifdef NETATALK
case AF_APPLETALK: {
struct ifaddr *ifa;
int s;
KERNEL_LOCK(1, NULL);
if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
/*
* ifaddr is the first thing in at_ifaddr
*/
s = pserialize_read_enter();
ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
if (ifa == NULL) {
pserialize_read_exit(s);
KERNEL_UNLOCK_ONE(NULL);
senderr(EADDRNOTAVAIL);
}
aa = (struct at_ifaddr *)ifa;
/*
* In the phase 2 case, we need to prepend an mbuf for the
* llc header.
*/
if (aa->aa_flags & AFA_PHASE2) {
struct llc llc;
#ifdef MPLS
{
struct m_tag *mtag;
mtag = m_tag_find(m, PACKET_TAG_MPLS);
if (mtag != NULL) {
/* Having the tag itself indicates it's MPLS */
etype = htons(ETHERTYPE_MPLS);
m_tag_delete(m, mtag);
}
}
#endif
if (mcopy)
(void)looutput(ifp, mcopy, dst, rt);
KASSERT((m->m_flags & M_PKTHDR) != 0);
/*
* If no ether type is set, this must be a 802.2 formatted packet.
*/
if (etype == 0)
etype = htons(m->m_pkthdr.len);
/*
* Add local net header. If no space in first mbuf, allocate another.
*/
M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
if (m == NULL)
senderr(ENOBUFS);
#ifdef ALTQ
KERNEL_LOCK(1, NULL);
/*
* If ALTQ is enabled on the parent interface, do
* classification; the queueing discipline might not
* require classification, but might require the
* address family/header pointer in the pktattr.
*/
if (ALTQ_IS_ENABLED(&ifp->if_snd))
altq_etherclassify(&ifp->if_snd, m);
KERNEL_UNLOCK_ONE(NULL);
#endif
return ifq_enqueue(ifp, m);
#ifdef ALTQ
/*
* This routine is a slight hack to allow a packet to be classified
* if the Ethernet headers are present. It will go away when ALTQ's
* classification engine understands link headers.
*
* XXX: We may need to do m_pullups here. First to ensure struct ether_header
* is indeed contiguous, then to read the LLC and so on.
*/
void
altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
{
struct ether_header *eh;
struct mbuf *mtop = m;
uint16_t ether_type;
int hlen, af, hdrsize;
void *hdr;
switch (ether_type) {
case ETHERTYPE_IP:
af = AF_INET;
hdrsize = 20; /* sizeof(struct ip) */
break;
case ETHERTYPE_IPV6:
af = AF_INET6;
hdrsize = 40; /* sizeof(struct ip6_hdr) */
break;
default:
af = AF_UNSPEC;
hdrsize = 0;
break;
}
while (m->m_len <= hlen) {
hlen -= m->m_len;
m = m->m_next;
if (m == NULL)
goto bad;
}
if (m->m_len < (hlen + hdrsize)) {
/*
* protocol header not in a single mbuf.
* We can't cope with this situation right
* now (but it shouldn't ever happen, really, anyhow).
*/
#ifdef DEBUG
printf("altq_etherclassify: headers span multiple mbufs: "
"%d < %d\n", m->m_len, (hlen + hdrsize));
#endif
goto bad;
}
m->m_data += hlen;
m->m_len -= hlen;
hdr = mtod(m, void *);
if (ALTQ_NEEDS_CLASSIFY(ifq)) {
mtop->m_pkthdr.pattr_class =
(*ifq->altq_classify)(ifq->altq_clfier, m, af);
}
mtop->m_pkthdr.pattr_af = af;
mtop->m_pkthdr.pattr_hdr = hdr;
/*
* Determine if the packet is within its size limits. For MPLS the
* header length is variable, so we skip the check.
*/
if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
#ifdef DIAGNOSTIC
mutex_enter(&bigpktpps_lock);
if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
bigpktppslim)) {
printf("%s: discarding oversize frame (len=%d)\n",
ifp->if_xname, m->m_pkthdr.len);
}
mutex_exit(&bigpktpps_lock);
#endif
goto error;
}
if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
/*
* If this is not a simplex interface, drop the packet
* if it came from us.
*/
if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
ETHER_ADDR_LEN) == 0) {
goto drop;
}
/*
* Processing a logical interfaces that are able
* to configure vlan(4).
*/
#if NAGR > 0
if (ifp->if_lagg != NULL &&
__predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
m->m_flags &= ~M_PROMISC;
agr_input(ifp, m);
return;
}
#endif
/*
* VLAN processing.
*
* VLAN provides service delimiting so the frames are
* processed before other handlings. If a VLAN interface
* does not exist to take those frames, they're returned
* to ether_input().
*/
if (vlan_has_tag(m)) {
if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) {
if (etype == ETHERTYPE_VLAN ||
etype == ETHERTYPE_QINQ)
goto drop;
/* XXX we should actually use the prio value? */
m->m_flags &= ~M_VLANTAG;
} else {
#if NVLAN > 0
if (ec->ec_nvlans > 0) {
m = vlan_input(ifp, m);
/* vlan_input() called ether_input() recursively */
if (m == NULL)
return;
}
#endif
/* drop VLAN frames not for this port. */
goto noproto;
}
}
#if NCARP > 0
if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
/*
* Clear M_PROMISC, in case the packet comes from a
* vlan.
*/
m->m_flags &= ~M_PROMISC;
if (carp_input(m, (uint8_t *)&eh->ether_shost,
(uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
return;
}
#endif
/*
* Handle protocols that expect to have the Ethernet header
* (and possibly FCS) intact.
*/
switch (etype) {
#if NPPPOE > 0
case ETHERTYPE_PPPOEDISC:
pppoedisc_input(ifp, m);
return;
case ETHERTYPE_PPPOE:
pppoe_input(ifp, m);
return;
#endif
case ETHERTYPE_SLOWPROTOCOLS: {
uint8_t subtype;
if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
goto error;
/* If the CRC is still on the packet, trim it off. */
if (m->m_flags & M_HASFCS) {
m_adj(m, -ETHER_CRC_LEN);
m->m_flags &= ~M_HASFCS;
}
/* etype represents the size of the payload in this case */
if (etype <= ETHERMTU + sizeof(struct ether_header)) {
KASSERT(ehlen == sizeof(*eh));
#if defined (LLC) || defined (NETATALK)
ether_input_llc(ifp, m, eh);
return;
#else
/* ethertype of 0-1500 is regarded as noproto */
goto noproto;
#endif
}
/* For ARP packets, store the source address so that
* ARP DAD probes can be validated. */
if (etype == ETHERTYPE_ARP) {
struct m_tag *mtag;
IFNET_ASSERT_UNLOCKED(ifp);
/*
* Prevent further calls to ioctl (for example turning off
* promiscuous mode from the bridge code), which eventually can
* call if_init() which can cause panics because the interface
* is in the process of being detached. Return device not configured
* instead.
*/
ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
enxio);
#if 0
/*
* This is for reference. We have a table-driven version
* of the little-endian crc32 generator, which is faster
* than the double-loop.
*/
uint32_t
ether_crc32_le(const uint8_t *buf, size_t len)
{
uint32_t c, crc, carry;
size_t i, j;
crc = 0xffffffffU; /* initial value */
for (i = 0; i < len; i++) {
c = buf[i];
for (j = 0; j < 8; j++) {
carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
crc >>= 1;
c >>= 1;
if (carry)
crc = (crc ^ ETHER_CRC_POLY_LE);
}
}
switch (*cp) {
case ':':
case '-':
case '.':
cp++;
break;
}
}
return ENOBUFS;
}
/*
* Convert a sockaddr into an Ethernet address or range of Ethernet
* addresses.
*/
int
ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
uint8_t addrhi[ETHER_ADDR_LEN])
{
#ifdef INET
const struct sockaddr_in *sin;
#endif
#ifdef INET6
const struct sockaddr_in6 *sin6;
#endif
switch (sa->sa_family) {
case AF_UNSPEC:
memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
break;
#ifdef INET
case AF_INET:
sin = satocsin(sa);
if (sin->sin_addr.s_addr == INADDR_ANY) {
/*
* An IP address of INADDR_ANY means listen to
* or stop listening to all of the Ethernet
* multicast addresses used for IP.
* (This is for the sake of IP multicast routers.)
*/
memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
} else {
ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
}
break;
#endif
#ifdef INET6
case AF_INET6:
sin6 = satocsin6(sa);
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
/*
* An IP6 address of 0 means listen to or stop
* listening to all of the Ethernet multicast
* address used for IP6.
* (This is used for multicast routers.)
*/
memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
} else {
ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
}
break;
#endif
default:
return EAFNOSUPPORT;
}
return 0;
}
/*
* Add an Ethernet multicast address or range of addresses to the list for a
* given interface.
*/
int
ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
{
struct ether_multi *enm, *_enm;
u_char addrlo[ETHER_ADDR_LEN];
u_char addrhi[ETHER_ADDR_LEN];
int error = 0;
/* Allocate out of lock */
enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
/*
* Verify that we have valid Ethernet multicast addresses.
*/
if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
error = EINVAL;
goto out;
}
/*
* See if the address range is already in the list.
*/
_enm = ether_lookup_multi(addrlo, addrhi, ec);
if (_enm != NULL) {
/*
* Found it; just increment the reference count.
*/
++_enm->enm_refcount;
error = 0;
goto out;
}
/*
* Link a new multicast record into the interface's multicast list.
*/
memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
enm->enm_refcount = 1;
LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
ec->ec_multicnt++;
/*
* Return ENETRESET to inform the driver that the list has changed
* and its reception filter should be adjusted accordingly.
*/
error = ENETRESET;
enm = NULL;
/*
* Look up the address in our list.
*/
enm = ether_lookup_multi(addrlo, addrhi, ec);
if (enm == NULL) {
error = ENXIO;
goto error;
}
if (--enm->enm_refcount != 0) {
/*
* Still some claims to this record.
*/
error = 0;
goto error;
}
/*
* No remaining claims to this record; unlink and free it.
*/
LIST_REMOVE(enm, enm_list);
ec->ec_multicnt--;
ETHER_UNLOCK(ec);
kmem_free(enm, sizeof(*enm));
/*
* Return ENETRESET to inform the driver that the list has changed
* and its reception filter should be adjusted accordingly.
*/
return ENETRESET;
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running,
* then stop and disable it.
*/
if_stop(ifp, 1);
break;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
return if_init(ifp);
case IFF_UP | IFF_RUNNING:
error = 0;
if (ec->ec_ifflags_cb != NULL) {
error = (*ec->ec_ifflags_cb)(ec);
if (error == ENETRESET) {
/*
* Reset the interface to pick up
* changes in any other flags that
* affect the hardware state.
*/
return if_init(ifp);
}
} else
error = if_init(ifp);
return error;
case 0:
break;
}
return 0;
}
/*
* Common ioctls for Ethernet interfaces. Note, we must be
* called at splnet().
*/
int
ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct ethercom *ec = (void *)ifp;
struct eccapreq *eccr;
struct ifreq *ifr = (struct ifreq *)data;
struct if_laddrreq *iflr = data;
const struct sockaddr_dl *sdl;
static const uint8_t zero[ETHER_ADDR_LEN];
int error;
if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
return EINVAL;
else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
return error;
else if (ifp->if_flags & IFF_UP) {
/* Make sure the device notices the MTU change. */
return if_init(ifp);
} else
return 0;
}
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
return error;
return ether_ioctl_reinit(ec);
case SIOCGIFFLAGS:
error = ifioctl_common(ifp, cmd, data);
if (error == 0) {
/* Set IFF_ALLMULTI for backcompat */
ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
IFF_ALLMULTI : 0;
}
return error;
case SIOCGETHERCAP:
eccr = (struct eccapreq *)data;
eccr->eccr_capabilities = ec->ec_capabilities;
eccr->eccr_capenable = ec->ec_capenable;
return 0;
case SIOCSETHERCAP:
eccr = (struct eccapreq *)data;
if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
return EINVAL;
if (eccr->eccr_capenable == ec->ec_capenable)
return 0;
#if 0 /* notyet */
ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
| (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
#else
ec->ec_capenable = eccr->eccr_capenable;
#endif
return ether_ioctl_reinit(ec);
case SIOCADDMULTI:
return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
case SIOCDELMULTI:
return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
if (ec->ec_mii != NULL)
return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
cmd);
else if (ec->ec_ifmedia != NULL)
return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
else
return ENOTTY;
break;
case SIOCALIFADDR:
sdl = satocsdl(sstocsa(&iflr->addr));
if (sdl->sdl_family != AF_LINK)
;
else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
return EINVAL;
else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
return EINVAL;
/*FALLTHROUGH*/
default:
return ifioctl_common(ifp, cmd, data);
}
return 0;
}
/*
* Enable/disable passing VLAN packets if the parent interface supports it.
* Return:
* 0: Ok
* -1: Parent interface does not support vlans
* >0: Error
*/
int
ether_enable_vlan_mtu(struct ifnet *ifp)
{
int error;
struct ethercom *ec = (void *)ifp;
/* Parent does not support VLAN's */
if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
return -1;
/*
* Parent supports the VLAN_MTU capability,
* i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
* enable it.
*/
ec->ec_capenable |= ETHERCAP_VLAN_MTU;
/* Interface is down, defer for later */
if ((ifp->if_flags & IFF_UP) == 0)
return 0;
if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
return 0;
if (m->m_len < sizeof(*evl)) {
m = m_pullup(m, sizeof(*evl));
if (m == NULL) {
error = ENOBUFS;
goto out;
}
}
/*
* Transform the Ethernet header into an
* Ethernet header with 802.1Q encapsulation.
*/
memmove(mtod(m, void *),
mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
sizeof(struct ether_header));
evl = mtod(m, struct ether_vlan_header *);
evl->evl_proto = evl->evl_encap_proto;
evl->evl_encap_proto = htons(etype);
evl->evl_tag = htons(tag);
/*
* To cater for VLAN-aware layer 2 ethernet
* switches which may need to strip the tag
* before forwarding the packet, make sure
* the packet+tag is at least 68 bytes long.
* This is necessary because our parent will
* only pad to 64 bytes (ETHER_MIN_LEN) and
* some switches will not pad by themselves
* after deleting a tag.
*/
if (m->m_pkthdr.len < min_data_len) {
m_copyback(m, m->m_pkthdr.len,
min_data_len - m->m_pkthdr.len,
vlan_zero_pad_buff);
}
/*
* Restore the original ethertype. We'll remove
* the encapsulation after we've found the vlan
* interface corresponding to the tag.
*/
evl->evl_encap_proto = evl->evl_proto;
/*
* Remove the encapsulation header and append tag.
* The original header has already been fixed up above.
*/
vlan_set_tag(m, ntohs(evl->evl_tag));
memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
offsetof(struct ether_vlan_header, evl_encap_proto));
m_adj(m, ETHER_VLAN_ENCAP_LEN);
return m;
}
static int
ether_multicast_sysctl(SYSCTLFN_ARGS)
{
struct ether_multi *enm;
struct ifnet *ifp;
struct ethercom *ec;
int error = 0;
size_t written;
struct psref psref;
int bound;
unsigned int multicnt;
struct ether_multi_sysctl *addrs;
int i;
/*
* ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
* is sleepable, while holding it. Copy data to a local buffer first
* with the lock taken and then call sysctl_copyout without holding it.
*/
retry:
multicnt = ec->ec_multicnt;
ETHER_LOCK(ec);
if (multicnt != ec->ec_multicnt) {
/* The number of multicast addresses has changed */
ETHER_UNLOCK(ec);
kmem_free(addrs, sizeof(*addrs) * multicnt);
goto retry;
}