Home | History | Annotate | Line # | Download | only in agr
if_agr.c revision 1.11
      1  1.11  christos /*	$NetBSD: if_agr.c,v 1.11 2007/03/04 06:03:18 christos Exp $	*/
      2   1.1      yamt 
      3   1.1      yamt /*-
      4   1.1      yamt  * Copyright (c)2005 YAMAMOTO Takashi,
      5   1.1      yamt  * All rights reserved.
      6   1.1      yamt  *
      7   1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8   1.1      yamt  * modification, are permitted provided that the following conditions
      9   1.1      yamt  * are met:
     10   1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11   1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12   1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15   1.1      yamt  *
     16   1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1      yamt  * SUCH DAMAGE.
     27   1.1      yamt  */
     28   1.1      yamt 
     29   1.1      yamt #include <sys/cdefs.h>
     30  1.11  christos __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.11 2007/03/04 06:03:18 christos Exp $");
     31   1.1      yamt 
     32   1.1      yamt #include "bpfilter.h"
     33   1.1      yamt #include "opt_inet.h"
     34   1.1      yamt 
     35   1.1      yamt #include <sys/param.h>
     36   1.2      yamt #include <sys/callout.h>
     37   1.1      yamt #include <sys/malloc.h>
     38   1.1      yamt #include <sys/mbuf.h>
     39   1.1      yamt #include <sys/systm.h>
     40   1.1      yamt #include <sys/types.h>
     41   1.1      yamt #include <sys/queue.h>
     42   1.1      yamt #include <sys/sockio.h>
     43   1.1      yamt #include <sys/proc.h>	/* XXX for curproc */
     44   1.5      yamt #include <sys/kauth.h>
     45   1.1      yamt 
     46   1.1      yamt #if NBPFILTER > 0
     47   1.1      yamt #include <net/bpf.h>
     48   1.1      yamt #endif
     49   1.1      yamt #include <net/if.h>
     50   1.1      yamt #include <net/if_dl.h>
     51   1.1      yamt #include <net/if_types.h>
     52   1.1      yamt 
     53   1.1      yamt #if defined(INET)
     54   1.1      yamt #include <netinet/in.h>
     55   1.1      yamt #include <netinet/if_inarp.h>
     56   1.1      yamt #endif
     57   1.1      yamt 
     58   1.1      yamt #include <net/agr/if_agrvar.h>
     59   1.1      yamt #include <net/agr/if_agrvar_impl.h>
     60   1.1      yamt #include <net/agr/if_agrioctl.h>
     61   1.1      yamt #include <net/agr/if_agrsubr.h>
     62   1.1      yamt #include <net/agr/if_agrethervar.h>
     63   1.1      yamt 
     64   1.1      yamt void agrattach(int);
     65   1.1      yamt 
     66   1.1      yamt static int agr_clone_create(struct if_clone *, int);
     67   1.1      yamt static int agr_clone_destroy(struct ifnet *);
     68   1.1      yamt static void agr_start(struct ifnet *);
     69   1.1      yamt static int agr_setconfig(struct ifnet *, const struct agrreq *);
     70   1.1      yamt static int agr_getconfig(struct ifnet *, struct agrreq *);
     71   1.1      yamt static int agr_getportlist(struct ifnet *, struct agrreq *);
     72   1.1      yamt static int agr_addport(struct ifnet *, struct ifnet *);
     73   1.1      yamt static int agr_remport(struct ifnet *, struct ifnet *);
     74   1.1      yamt static int agrreq_copyin(const void *, struct agrreq *);
     75   1.1      yamt static int agrreq_copyout(void *, struct agrreq *);
     76  1.11  christos static int agr_ioctl(struct ifnet *, u_long, void *);
     77   1.1      yamt static struct agr_port *agr_select_tx_port(struct agr_softc *, struct mbuf *);
     78  1.11  christos static int agr_ioctl_filter(struct ifnet *, u_long, void *);
     79   1.1      yamt static void agr_reset_iftype(struct ifnet *);
     80   1.1      yamt static int agr_config_promisc(struct agr_softc *);
     81   1.1      yamt static int agrport_config_promisc_callback(struct agr_port *, void *);
     82   1.9   thorpej static int agrport_config_promisc(struct agr_port *, bool);
     83   1.1      yamt static int agrport_cleanup(struct agr_softc *, struct agr_port *);
     84   1.1      yamt 
     85   1.1      yamt static struct if_clone agr_cloner =
     86   1.1      yamt     IF_CLONE_INITIALIZER("agr", agr_clone_create, agr_clone_destroy);
     87   1.1      yamt 
     88   1.1      yamt /*
     89   1.1      yamt  * EXPORTED FUNCTIONS
     90   1.1      yamt  */
     91   1.1      yamt 
     92   1.1      yamt /*
     93   1.1      yamt  * agrattch: device attach routine.
     94   1.1      yamt  */
     95   1.1      yamt 
     96   1.1      yamt void
     97   1.1      yamt agrattach(int count)
     98   1.1      yamt {
     99   1.1      yamt 
    100   1.1      yamt 	if_clone_attach(&agr_cloner);
    101   1.1      yamt }
    102   1.1      yamt 
    103   1.1      yamt /*
    104   1.1      yamt  * agr_input: frame collector.
    105   1.1      yamt  */
    106   1.1      yamt 
    107   1.1      yamt void
    108   1.1      yamt agr_input(struct ifnet *ifp_port, struct mbuf *m)
    109   1.1      yamt {
    110   1.1      yamt 	struct agr_port *port;
    111   1.1      yamt 	struct ifnet *ifp;
    112   1.1      yamt 
    113   1.1      yamt 	port = ifp_port->if_agrprivate;
    114   1.1      yamt 	KASSERT(port);
    115   1.1      yamt 	ifp = port->port_agrifp;
    116   1.1      yamt 	if ((port->port_flags & AGRPORT_COLLECTING) == 0) {
    117   1.1      yamt 		m_freem(m);
    118   1.1      yamt 		ifp->if_ierrors++;
    119   1.1      yamt 		return;
    120   1.1      yamt 	}
    121   1.1      yamt 
    122   1.1      yamt 	ifp->if_ipackets++;
    123   1.1      yamt 	m->m_pkthdr.rcvif = ifp;
    124   1.1      yamt 
    125   1.1      yamt #if NBPFILTER > 0
    126   1.1      yamt 	if (ifp->if_bpf) {
    127   1.1      yamt 		bpf_mtap(ifp->if_bpf, m);
    128   1.1      yamt 	}
    129   1.1      yamt #endif
    130   1.1      yamt 
    131   1.1      yamt 	(*ifp->if_input)(ifp, m);
    132   1.1      yamt }
    133   1.1      yamt 
    134   1.1      yamt /*
    135   1.1      yamt  * EXPORTED AGR-INTERNAL FUNCTIONS
    136   1.1      yamt  */
    137   1.1      yamt 
    138   1.1      yamt int
    139   1.1      yamt agr_lock(struct agr_softc *sc)
    140   1.1      yamt {
    141   1.1      yamt 	int s;
    142   1.1      yamt 
    143   1.1      yamt 	s = splnet();
    144   1.1      yamt 	simple_lock(&sc->sc_lock);
    145   1.1      yamt 
    146   1.1      yamt 	return s;
    147   1.1      yamt }
    148   1.1      yamt 
    149   1.1      yamt void
    150   1.1      yamt agr_unlock(struct agr_softc *sc, int savedipl)
    151   1.1      yamt {
    152   1.1      yamt 
    153   1.1      yamt 	simple_unlock(&sc->sc_lock);
    154   1.1      yamt 	splx(savedipl);
    155   1.1      yamt }
    156   1.1      yamt 
    157   1.1      yamt void
    158   1.1      yamt agr_ioctl_lock(struct agr_softc *sc)
    159   1.1      yamt {
    160   1.1      yamt 
    161   1.1      yamt 	lockmgr(&sc->sc_ioctl_lock, LK_EXCLUSIVE, NULL);
    162   1.1      yamt }
    163   1.1      yamt 
    164   1.1      yamt void
    165   1.1      yamt agr_ioctl_unlock(struct agr_softc *sc)
    166   1.1      yamt {
    167   1.1      yamt 
    168   1.1      yamt 	lockmgr(&sc->sc_ioctl_lock, LK_RELEASE, NULL);
    169   1.1      yamt }
    170   1.1      yamt 
    171   1.1      yamt /*
    172   1.1      yamt  * agr_xmit_frame: transmit a pre-built frame.
    173   1.1      yamt  */
    174   1.1      yamt 
    175   1.1      yamt int
    176   1.1      yamt agr_xmit_frame(struct ifnet *ifp_port, struct mbuf *m)
    177   1.1      yamt {
    178   1.1      yamt 	int error;
    179   1.1      yamt 
    180   1.1      yamt 	struct sockaddr_storage dst0;
    181   1.1      yamt 	struct sockaddr *dst;
    182   1.1      yamt 	int hdrlen;
    183   1.1      yamt 
    184   1.1      yamt 	/*
    185   1.1      yamt 	 * trim off link level header and let if_output re-add it.
    186   1.1      yamt 	 * XXX better to introduce an API to transmit pre-built frames.
    187   1.1      yamt 	 */
    188   1.1      yamt 
    189   1.1      yamt 	hdrlen = ifp_port->if_hdrlen;
    190   1.1      yamt 	if (m->m_pkthdr.len < hdrlen) {
    191   1.1      yamt 		m_freem(m);
    192   1.1      yamt 		return EINVAL;
    193   1.1      yamt 	}
    194   1.1      yamt 	memset(&dst0, 0, sizeof(dst0));
    195   1.1      yamt 	dst = (struct sockaddr *)&dst0;
    196   1.1      yamt 	dst->sa_family = pseudo_AF_HDRCMPLT;
    197   1.1      yamt 	dst->sa_len = hdrlen;
    198   1.1      yamt 	m_copydata(m, 0, hdrlen, &dst->sa_data);
    199   1.1      yamt 	m_adj(m, hdrlen);
    200   1.1      yamt 
    201   1.1      yamt 	error = (*ifp_port->if_output)(ifp_port, m, dst, NULL);
    202   1.1      yamt 
    203   1.1      yamt 	return error;
    204   1.1      yamt }
    205   1.1      yamt 
    206   1.1      yamt int
    207  1.11  christos agrport_ioctl(struct agr_port *port, u_long cmd, void *arg)
    208   1.1      yamt {
    209   1.1      yamt 	struct ifnet *ifp = port->port_ifp;
    210   1.1      yamt 
    211   1.1      yamt 	KASSERT(ifp->if_agrprivate == (void *)port);
    212   1.1      yamt 	KASSERT(ifp->if_ioctl == agr_ioctl_filter);
    213   1.1      yamt 
    214   1.1      yamt 	return (*port->port_ioctl)(ifp, cmd, arg);
    215   1.1      yamt }
    216   1.1      yamt 
    217   1.1      yamt /*
    218   1.1      yamt  * INTERNAL FUNCTIONS
    219   1.1      yamt  */
    220   1.1      yamt 
    221   1.1      yamt static int
    222   1.1      yamt agr_clone_create(struct if_clone *ifc, int unit)
    223   1.1      yamt {
    224   1.1      yamt 	struct agr_softc *sc;
    225   1.1      yamt 	struct ifnet *ifp;
    226   1.1      yamt 
    227   1.1      yamt 	sc = agr_alloc_softc();
    228   1.1      yamt 	TAILQ_INIT(&sc->sc_ports);
    229   1.1      yamt 	lockinit(&sc->sc_ioctl_lock, PSOCK, "agrioctl", 0, 0);
    230   1.1      yamt 	simple_lock_init(&sc->sc_lock);
    231   1.1      yamt 	agrtimer_init(sc);
    232   1.1      yamt 	ifp = &sc->sc_if;
    233   1.1      yamt 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
    234   1.1      yamt 	    ifc->ifc_name, unit);
    235   1.1      yamt 
    236   1.1      yamt 	ifp->if_softc = sc;
    237   1.1      yamt 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    238   1.1      yamt 	ifp->if_start = agr_start;
    239   1.1      yamt 	ifp->if_ioctl = agr_ioctl;
    240   1.1      yamt 	IFQ_SET_READY(&ifp->if_snd);
    241   1.1      yamt 
    242   1.1      yamt 	if_attach(ifp);
    243   1.1      yamt 
    244   1.1      yamt 	agr_reset_iftype(ifp);
    245   1.1      yamt 
    246   1.1      yamt 	return 0;
    247   1.1      yamt }
    248   1.1      yamt 
    249   1.1      yamt static void
    250   1.1      yamt agr_reset_iftype(struct ifnet *ifp)
    251   1.1      yamt {
    252   1.1      yamt 
    253   1.1      yamt 	ifp->if_type = IFT_OTHER;
    254   1.1      yamt 	ifp->if_dlt = DLT_NULL;
    255   1.1      yamt 	ifp->if_addrlen = 0;
    256   1.1      yamt 	if_alloc_sadl(ifp);
    257   1.1      yamt }
    258   1.1      yamt 
    259   1.1      yamt static int
    260   1.1      yamt agr_clone_destroy(struct ifnet *ifp)
    261   1.1      yamt {
    262   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    263   1.1      yamt 	int error;
    264   1.1      yamt 	int s;
    265   1.1      yamt 
    266   1.1      yamt 	agr_ioctl_lock(sc);
    267   1.1      yamt 
    268   1.1      yamt 	s = AGR_LOCK(sc);
    269   1.1      yamt 	if (sc->sc_nports > 0) {
    270   1.1      yamt 		error = EBUSY;
    271   1.1      yamt 	} else {
    272   1.1      yamt 		error = 0;
    273   1.1      yamt 	}
    274   1.1      yamt 	AGR_UNLOCK(sc, s);
    275   1.1      yamt 
    276   1.1      yamt 	agr_ioctl_unlock(sc);
    277   1.1      yamt 
    278   1.1      yamt 	if (error == 0) {
    279   1.1      yamt 		if_detach(ifp);
    280   1.1      yamt 		agr_free_softc(sc);
    281   1.1      yamt 	}
    282   1.1      yamt 
    283   1.1      yamt 	return error;
    284   1.1      yamt }
    285   1.1      yamt 
    286   1.1      yamt static struct agr_port *
    287   1.1      yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    288   1.1      yamt {
    289   1.1      yamt 
    290   1.1      yamt 	return (*sc->sc_iftop->iftop_select_tx_port)(sc, m);
    291   1.1      yamt }
    292   1.1      yamt 
    293   1.1      yamt #if 0 /* "generic" version */
    294   1.1      yamt static struct agr_port *
    295   1.1      yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    296   1.1      yamt {
    297   1.1      yamt 	struct agr_port *port;
    298   1.1      yamt 	uint32_t hash;
    299   1.1      yamt 
    300   1.1      yamt 	hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
    301   1.1      yamt 	if (sc->sc_nports == 0)
    302   1.1      yamt 		return NULL;
    303   1.1      yamt 	hash %= sc->sc_nports;
    304   1.1      yamt 	port = TAILQ_FIRST(&sc->sc_ports);
    305   1.1      yamt 	KASSERT(port != NULL);
    306   1.1      yamt 	while (hash--) {
    307   1.1      yamt 		port = TAILQ_NEXT(port, port_q);
    308   1.1      yamt 		KASSERT(port != NULL);
    309   1.1      yamt 	}
    310   1.1      yamt 
    311   1.1      yamt 	return port;
    312   1.1      yamt }
    313   1.1      yamt #endif /* 0 */
    314   1.1      yamt 
    315   1.1      yamt static void
    316   1.1      yamt agr_start(struct ifnet *ifp)
    317   1.1      yamt {
    318   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    319   1.1      yamt 	struct mbuf *m;
    320   1.1      yamt 	int s;
    321   1.1      yamt 
    322   1.1      yamt 	s = AGR_LOCK(sc);
    323   1.1      yamt 
    324   1.1      yamt 	while (/* CONSTCOND */ 1) {
    325   1.1      yamt 		struct agr_port *port;
    326   1.1      yamt 
    327   1.1      yamt 		IFQ_DEQUEUE(&ifp->if_snd, m);
    328   1.1      yamt 		if (m == NULL) {
    329   1.1      yamt 			break;
    330   1.1      yamt 		}
    331   1.1      yamt #if NBPFILTER > 0
    332   1.1      yamt 		if (ifp->if_bpf) {
    333   1.1      yamt 			bpf_mtap(ifp->if_bpf, m);
    334   1.1      yamt 		}
    335   1.1      yamt #endif
    336   1.1      yamt 		port = agr_select_tx_port(sc, m);
    337   1.1      yamt 		if (port) {
    338   1.1      yamt 			int error;
    339   1.1      yamt 
    340   1.1      yamt 			error = agr_xmit_frame(port->port_ifp, m);
    341   1.1      yamt 			if (error) {
    342   1.1      yamt 				ifp->if_oerrors++;
    343   1.1      yamt 			} else {
    344   1.1      yamt 				ifp->if_opackets++;
    345   1.1      yamt 			}
    346   1.1      yamt 		} else {
    347   1.1      yamt 			m_freem(m);
    348   1.1      yamt 			ifp->if_oerrors++;
    349   1.1      yamt 		}
    350   1.1      yamt 	}
    351   1.1      yamt 
    352   1.1      yamt 	AGR_UNLOCK(sc, s);
    353   1.1      yamt 
    354   1.1      yamt 	ifp->if_flags &= ~IFF_OACTIVE;
    355   1.1      yamt }
    356   1.1      yamt 
    357   1.1      yamt static int
    358   1.1      yamt agr_setconfig(struct ifnet *ifp, const struct agrreq *ar)
    359   1.1      yamt {
    360   1.1      yamt 	int cmd = ar->ar_cmd;
    361   1.1      yamt 	struct ifnet *ifp_port;
    362   1.1      yamt 	int error = 0;
    363   1.1      yamt 	char ifname[IFNAMSIZ];
    364   1.1      yamt 
    365   1.8      yamt 	memset(ifname, 0, sizeof(ifname));
    366   1.8      yamt 	error = copyin(ar->ar_buf, ifname,
    367   1.8      yamt 	    MIN(ar->ar_buflen, sizeof(ifname) - 1));
    368   1.1      yamt 	if (error) {
    369   1.1      yamt 		return error;
    370   1.1      yamt 	}
    371   1.1      yamt 	ifp_port = ifunit(ifname);
    372   1.1      yamt 	if (ifp_port == NULL) {
    373   1.1      yamt 		return ENOENT;
    374   1.1      yamt 	}
    375   1.1      yamt 
    376   1.1      yamt 	switch (cmd) {
    377   1.1      yamt 	case AGRCMD_ADDPORT:
    378   1.1      yamt 		error = agr_addport(ifp, ifp_port);
    379   1.1      yamt 		break;
    380   1.1      yamt 
    381   1.1      yamt 	case AGRCMD_REMPORT:
    382   1.1      yamt 		error = agr_remport(ifp, ifp_port);
    383   1.1      yamt 		break;
    384   1.1      yamt 
    385   1.1      yamt 	default:
    386   1.1      yamt 		error = EINVAL;
    387   1.1      yamt 		break;
    388   1.1      yamt 	}
    389   1.1      yamt 
    390   1.1      yamt 	return error;
    391   1.1      yamt }
    392   1.1      yamt 
    393   1.1      yamt static int
    394   1.1      yamt agr_getportlist(struct ifnet *ifp, struct agrreq *ar)
    395   1.1      yamt {
    396   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    397   1.1      yamt 	struct agr_port *port;
    398   1.1      yamt 	struct agrportlist apl;
    399   1.1      yamt 	struct agrportinfo api;
    400   1.1      yamt 	char *cp = ar->ar_buf;
    401   1.1      yamt 	size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
    402   1.1      yamt 	int error;
    403   1.1      yamt 
    404   1.1      yamt 	if (cp != NULL) {
    405   1.1      yamt 		memset(&apl, 0, sizeof(apl));
    406   1.1      yamt 		memset(&api, 0, sizeof(api));
    407   1.1      yamt 
    408   1.1      yamt 		if (bufleft < sizeof(apl)) {
    409   1.1      yamt 			return E2BIG;
    410   1.1      yamt 		}
    411   1.1      yamt 		apl.apl_nports = sc->sc_nports;
    412   1.1      yamt 		error = copyout(&apl, cp, sizeof(apl));
    413   1.1      yamt 		if (error) {
    414   1.1      yamt 			return error;
    415   1.1      yamt 		}
    416   1.1      yamt 		cp += sizeof(apl);
    417   1.1      yamt 	}
    418   1.1      yamt 	bufleft -= sizeof(apl);
    419   1.1      yamt 
    420   1.1      yamt 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    421   1.1      yamt 		if (cp != NULL) {
    422   1.1      yamt 			if (bufleft < sizeof(api)) {
    423   1.1      yamt 				return E2BIG;
    424   1.1      yamt 			}
    425   1.1      yamt 			memcpy(api.api_ifname, port->port_ifp->if_xname,
    426   1.1      yamt 			    sizeof(api.api_ifname));
    427   1.1      yamt 			api.api_flags = 0;
    428   1.1      yamt 			if (port->port_flags & AGRPORT_COLLECTING) {
    429   1.1      yamt 				api.api_flags |= AGRPORTINFO_COLLECTING;
    430   1.1      yamt 			}
    431   1.1      yamt 			if (port->port_flags & AGRPORT_DISTRIBUTING) {
    432   1.1      yamt 				api.api_flags |= AGRPORTINFO_DISTRIBUTING;
    433   1.1      yamt 			}
    434   1.1      yamt 			error = copyout(&api, cp, sizeof(api));
    435   1.1      yamt 			if (error) {
    436   1.1      yamt 				return error;
    437   1.1      yamt 			}
    438   1.1      yamt 			cp += sizeof(api);
    439   1.1      yamt 		}
    440   1.1      yamt 		bufleft -= sizeof(api);
    441   1.1      yamt 	}
    442   1.1      yamt 
    443   1.1      yamt 	if (cp == NULL) {
    444   1.1      yamt 		ar->ar_buflen = -bufleft; /* necessary buffer size */
    445   1.1      yamt 	}
    446   1.1      yamt 
    447   1.1      yamt 	return 0;
    448   1.1      yamt }
    449   1.1      yamt 
    450   1.1      yamt static int
    451   1.1      yamt agr_getconfig(struct ifnet *ifp, struct agrreq *ar)
    452   1.1      yamt {
    453   1.1      yamt 	int cmd = ar->ar_cmd;
    454   1.1      yamt 	int error;
    455   1.1      yamt 
    456   1.1      yamt 	switch (cmd) {
    457   1.1      yamt 	case AGRCMD_PORTLIST:
    458   1.1      yamt 		error = agr_getportlist(ifp, ar);
    459   1.1      yamt 		break;
    460   1.1      yamt 
    461   1.1      yamt 	default:
    462   1.1      yamt 		error = EINVAL;
    463   1.1      yamt 		break;
    464   1.1      yamt 	}
    465   1.1      yamt 
    466   1.1      yamt 	return error;
    467   1.1      yamt }
    468   1.1      yamt 
    469   1.1      yamt static int
    470   1.1      yamt agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
    471   1.1      yamt {
    472   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    473   1.1      yamt 	struct agr_port *port = NULL;
    474   1.1      yamt 	int error = 0;
    475   1.1      yamt 	int s;
    476   1.1      yamt 
    477   1.1      yamt 	if (ifp_port->if_ioctl == NULL) {
    478   1.1      yamt 		error = EOPNOTSUPP;
    479   1.1      yamt 		goto out;
    480   1.1      yamt 	}
    481   1.1      yamt 
    482   1.1      yamt 	if (ifp_port->if_agrprivate) {
    483   1.1      yamt 		error = EBUSY;
    484   1.1      yamt 		goto out;
    485   1.1      yamt 	}
    486   1.1      yamt 
    487   1.1      yamt 	if (ifp_port->if_start == agr_start) {
    488   1.1      yamt 		error = EINVAL;
    489   1.1      yamt 		goto out;
    490   1.1      yamt 	}
    491   1.1      yamt 
    492   1.1      yamt 	port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
    493   1.1      yamt 	    M_WAITOK | M_ZERO);
    494   1.1      yamt 	if (port == NULL) {
    495   1.1      yamt 		error = ENOMEM;
    496   1.1      yamt 		goto out;
    497   1.1      yamt 	}
    498   1.1      yamt 	port->port_flags = AGRPORT_LARVAL;
    499   1.1      yamt 
    500   1.1      yamt 	if (TAILQ_NEXT(TAILQ_FIRST(&ifp_port->if_addrlist), ifa_list) != NULL) {
    501   1.1      yamt 		error = EBUSY;
    502   1.1      yamt 		goto out;
    503   1.1      yamt 	}
    504   1.1      yamt 
    505   1.1      yamt 	if (sc->sc_nports == 0) {
    506   1.1      yamt 		switch (ifp_port->if_type) {
    507   1.1      yamt 		case IFT_ETHER:
    508   1.1      yamt 			sc->sc_iftop = &agrether_ops;
    509   1.1      yamt 			break;
    510   1.1      yamt 
    511   1.1      yamt 		default:
    512   1.1      yamt 			error = EPROTONOSUPPORT; /* XXX */
    513   1.1      yamt 			goto out;
    514   1.1      yamt 		}
    515   1.1      yamt 
    516   1.1      yamt 		error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
    517   1.1      yamt 		if (error)
    518   1.1      yamt 			goto out;
    519   1.1      yamt 		agrtimer_start(sc);
    520   1.1      yamt 	} else {
    521   1.1      yamt 		if (ifp->if_type != ifp_port->if_type) {
    522   1.1      yamt 			error = EINVAL;
    523   1.1      yamt 			goto out;
    524   1.1      yamt 		}
    525   1.1      yamt 		if (ifp->if_addrlen != ifp_port->if_addrlen) {
    526   1.1      yamt 			error = EINVAL;
    527   1.1      yamt 			goto out;
    528   1.1      yamt 		}
    529   1.1      yamt 	}
    530   1.1      yamt 
    531   1.1      yamt 	memcpy(port->port_origlladdr, LLADDR(ifp_port->if_sadl),
    532   1.1      yamt 	    ifp_port->if_addrlen);
    533   1.1      yamt 
    534   1.1      yamt 	/*
    535   1.1      yamt 	 * start to modify ifp_port.
    536   1.1      yamt 	 */
    537   1.1      yamt 
    538   1.1      yamt 	error = (*ifp_port->if_ioctl)(ifp_port, SIOCSIFADDR,
    539  1.11  christos 	    (void *)TAILQ_FIRST(&ifp->if_addrlist));
    540   1.1      yamt 
    541   1.1      yamt 	if (error) {
    542   1.1      yamt 		printf("%s: SIOCSIFADDR error %d\n", __func__, error);
    543   1.1      yamt 		goto cleanup;
    544   1.1      yamt 	}
    545   1.1      yamt 	port->port_flags |= AGRPORT_LADDRCHANGED;
    546   1.1      yamt 
    547   1.1      yamt 	ifp->if_type = ifp_port->if_type;
    548   1.1      yamt 	s = AGR_LOCK(sc);
    549   1.1      yamt 
    550   1.1      yamt 	port->port_ifp = ifp_port;
    551   1.1      yamt 	ifp_port->if_agrprivate = port;
    552   1.1      yamt 	port->port_agrifp = ifp;
    553   1.1      yamt 	TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
    554   1.1      yamt 	sc->sc_nports++;
    555   1.1      yamt 
    556   1.1      yamt 	port->port_ioctl = ifp_port->if_ioctl;
    557   1.1      yamt 	ifp_port->if_ioctl = agr_ioctl_filter;
    558   1.1      yamt 
    559   1.1      yamt 	port->port_flags |= AGRPORT_ATTACHED;
    560   1.1      yamt 
    561   1.1      yamt 	AGR_UNLOCK(sc, s);
    562   1.1      yamt 
    563   1.1      yamt 	error = (*sc->sc_iftop->iftop_portinit)(sc, port);
    564   1.1      yamt 	if (error) {
    565   1.1      yamt 		printf("%s: portinit error %d\n", __func__, error);
    566   1.1      yamt 		goto cleanup;
    567   1.1      yamt 	}
    568   1.1      yamt 
    569   1.1      yamt 	ifp->if_flags |= IFF_RUNNING;
    570   1.1      yamt 
    571   1.1      yamt 	agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
    572  1.10   thorpej 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, true);
    573   1.1      yamt 	if (error) {
    574   1.1      yamt 		printf("%s: configmulti error %d\n", __func__, error);
    575   1.1      yamt 		goto cleanup;
    576   1.1      yamt 	}
    577   1.1      yamt 
    578   1.1      yamt 	s = AGR_LOCK(sc);
    579   1.1      yamt 	port->port_flags &= ~AGRPORT_LARVAL;
    580   1.1      yamt 	AGR_UNLOCK(sc, s);
    581   1.1      yamt out:
    582   1.1      yamt 	if (error && port) {
    583   1.1      yamt 		free(port, M_DEVBUF);
    584   1.1      yamt 	}
    585   1.1      yamt 	return error;
    586   1.1      yamt 
    587   1.1      yamt cleanup:
    588   1.1      yamt 	if (agrport_cleanup(sc, port)) {
    589   1.1      yamt 		printf("%s: error on cleanup\n", __func__);
    590   1.1      yamt 
    591   1.1      yamt 		port = NULL; /* XXX */
    592   1.1      yamt 	}
    593   1.1      yamt 
    594   1.1      yamt 	if (sc->sc_nports == 0) {
    595   1.1      yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    596   1.1      yamt 		agrtimer_stop(sc);
    597   1.1      yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    598   1.1      yamt 		sc->sc_iftop = NULL;
    599   1.1      yamt 		agr_reset_iftype(ifp);
    600   1.1      yamt 	} else {
    601   1.1      yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    602   1.1      yamt 	}
    603   1.1      yamt 
    604   1.1      yamt 	goto out;
    605   1.1      yamt }
    606   1.1      yamt 
    607   1.1      yamt static int
    608   1.1      yamt agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
    609   1.1      yamt {
    610   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    611   1.1      yamt 	struct agr_port *port;
    612   1.1      yamt 	int error = 0;
    613   1.1      yamt 	int s;
    614   1.1      yamt 
    615   1.1      yamt 	if (ifp_port->if_agrprivate == NULL) {
    616   1.1      yamt 		error = ENOENT;
    617   1.1      yamt 		return error;
    618   1.1      yamt 	}
    619   1.1      yamt 
    620   1.1      yamt 	port = ifp_port->if_agrprivate;
    621   1.1      yamt 	if (port->port_agrifp != ifp) {
    622   1.1      yamt 		error = EINVAL;
    623   1.1      yamt 		return error;
    624   1.1      yamt 	}
    625   1.1      yamt 
    626   1.1      yamt 	KASSERT(sc->sc_nports > 0);
    627   1.1      yamt 
    628   1.1      yamt #if 0
    629   1.1      yamt 	if (sc->sc_nports == 1 &&
    630   1.1      yamt 	    TAILQ_NEXT(TAILQ_FIRST(&ifp->if_addrlist), ifa_list) != NULL) {
    631   1.1      yamt 		error = EBUSY;
    632   1.1      yamt 		return error;
    633   1.1      yamt 	}
    634   1.1      yamt #endif
    635   1.1      yamt 
    636   1.1      yamt 	s = AGR_LOCK(sc);
    637   1.1      yamt 	port->port_flags |= AGRPORT_DETACHING;
    638   1.1      yamt 	AGR_UNLOCK(sc, s);
    639   1.1      yamt 
    640   1.1      yamt 	error = (*sc->sc_iftop->iftop_portfini)(sc, port);
    641   1.1      yamt 	if (error) {
    642   1.1      yamt 		/* XXX XXX */
    643   1.1      yamt 		printf("%s: portfini error %d\n", __func__, error);
    644   1.1      yamt 		goto out;
    645   1.1      yamt 	}
    646   1.1      yamt 
    647  1.10   thorpej 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, false);
    648   1.1      yamt 	if (error) {
    649   1.1      yamt 		/* XXX XXX */
    650   1.1      yamt 		printf("%s: configmulti_port error %d\n", __func__, error);
    651   1.1      yamt 		goto out;
    652   1.1      yamt 	}
    653   1.1      yamt 
    654   1.1      yamt 	error = agrport_cleanup(sc, port);
    655   1.1      yamt 	if (error) {
    656   1.1      yamt 		/* XXX XXX */
    657   1.1      yamt 		printf("%s: agrport_cleanup error %d\n", __func__, error);
    658   1.1      yamt 		goto out;
    659   1.1      yamt 	}
    660   1.1      yamt 
    661   1.1      yamt 	free(port, M_DEVBUF);
    662   1.1      yamt 
    663   1.1      yamt out:
    664   1.1      yamt 	if (sc->sc_nports == 0) {
    665   1.1      yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    666   1.1      yamt 		agrtimer_stop(sc);
    667   1.1      yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    668   1.1      yamt 		sc->sc_iftop = NULL;
    669   1.1      yamt 		/* XXX should purge all addresses? */
    670   1.1      yamt 		agr_reset_iftype(ifp);
    671   1.1      yamt 	} else {
    672   1.1      yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    673   1.1      yamt 	}
    674   1.1      yamt 
    675   1.1      yamt 	return error;
    676   1.1      yamt }
    677   1.1      yamt 
    678   1.1      yamt static int
    679   1.1      yamt agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
    680   1.1      yamt {
    681   1.1      yamt 	struct ifnet *ifp_port = port->port_ifp;
    682   1.1      yamt 	int error;
    683   1.1      yamt 	int result = 0;
    684   1.1      yamt 	int s;
    685   1.1      yamt 
    686  1.10   thorpej 	error = agrport_config_promisc(port, false);
    687   1.1      yamt 	if (error) {
    688   1.1      yamt 		printf("%s: config_promisc error %d\n", __func__, error);
    689   1.1      yamt 		result = error;
    690   1.1      yamt 	}
    691   1.1      yamt 
    692   1.1      yamt 	if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
    693   1.1      yamt #if 0
    694   1.1      yamt 		memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
    695   1.1      yamt 		    ifp_port->if_addrlen);
    696   1.1      yamt 		if (ifp_port->if_init != NULL) {
    697   1.1      yamt 			error = (*ifp_port->if_init)(ifp_port);
    698   1.1      yamt 		}
    699   1.1      yamt #else
    700   1.1      yamt 		struct sockaddr_dl *sdl;
    701   1.1      yamt 		struct ifaddr ifa;
    702   1.1      yamt 		int sdllen;
    703   1.1      yamt 		int addrlen;
    704   1.1      yamt 
    705   1.1      yamt 		addrlen = ifp_port->if_addrlen;
    706   1.1      yamt 		sdllen = sizeof(*sdl) - sizeof(sdl->sdl_data) + addrlen;
    707   1.1      yamt 		sdl = malloc(sdllen, M_TEMP, M_WAITOK);
    708   1.1      yamt 		if (sdl == NULL) {
    709   1.1      yamt 			error = ENOMEM;
    710   1.1      yamt 		} else {
    711   1.1      yamt 			memset(sdl, 0, sdllen);
    712   1.1      yamt 			sdl->sdl_len = sdllen;
    713   1.1      yamt 			sdl->sdl_family = AF_LINK;
    714   1.1      yamt 			sdl->sdl_type = ifp_port->if_type;
    715   1.1      yamt 			sdl->sdl_alen = addrlen;
    716   1.1      yamt 			memcpy(LLADDR(sdl), port->port_origlladdr, addrlen);
    717   1.1      yamt 			memset(&ifa, 0, sizeof(ifa));
    718   1.1      yamt 			ifa.ifa_addr = (struct sockaddr *)sdl;
    719  1.11  christos 			error = agrport_ioctl(port, SIOCSIFADDR, (void *)&ifa);
    720   1.1      yamt 			free(sdl, M_TEMP);
    721   1.1      yamt 		}
    722   1.1      yamt #endif
    723   1.1      yamt 		if (error) {
    724   1.1      yamt 			printf("%s: if_init error %d\n", __func__, error);
    725   1.1      yamt 			result = error;
    726   1.1      yamt 		} else {
    727   1.1      yamt 			port->port_flags &= ~AGRPORT_LADDRCHANGED;
    728   1.1      yamt 		}
    729   1.1      yamt 	}
    730   1.1      yamt 
    731   1.1      yamt 	s = AGR_LOCK(sc);
    732   1.1      yamt 	if ((port->port_flags & AGRPORT_ATTACHED)) {
    733   1.1      yamt 		ifp_port->if_agrprivate = NULL;
    734   1.1      yamt 
    735   1.1      yamt 		TAILQ_REMOVE(&sc->sc_ports, port, port_q);
    736   1.1      yamt 		sc->sc_nports--;
    737   1.1      yamt 
    738   1.1      yamt 		KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
    739   1.1      yamt 		ifp_port->if_ioctl = port->port_ioctl;
    740   1.1      yamt 
    741   1.1      yamt 		port->port_flags &= ~AGRPORT_ATTACHED;
    742   1.1      yamt 	}
    743   1.1      yamt 	AGR_UNLOCK(sc, s);
    744   1.1      yamt 
    745   1.1      yamt 	return result;
    746   1.1      yamt }
    747   1.1      yamt 
    748   1.1      yamt static int
    749   1.1      yamt agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
    750   1.1      yamt {
    751   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    752   1.1      yamt 	int error;
    753   1.1      yamt 
    754   1.1      yamt 	error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
    755   1.1      yamt 	    (cmd == SIOCADDMULTI));
    756   1.1      yamt 
    757   1.1      yamt 	return error;
    758   1.1      yamt }
    759   1.1      yamt 
    760   1.1      yamt /* XXX an incomplete hack; can't filter ioctls handled ifioctl(). */
    761   1.1      yamt static int
    762  1.11  christos agr_ioctl_filter(struct ifnet *ifp, u_long cmd, void *arg)
    763   1.1      yamt {
    764   1.1      yamt 	struct agr_port *port = ifp->if_agrprivate;
    765   1.1      yamt 	int error;
    766   1.1      yamt 
    767   1.1      yamt 	KASSERT(port);
    768   1.1      yamt 
    769   1.1      yamt 	switch (cmd) {
    770   1.1      yamt 	case SIOCGIFADDR:
    771   1.1      yamt 	case SIOCGIFMEDIA:
    772   1.1      yamt 	case SIOCSIFFLAGS: /* XXX */
    773   1.1      yamt 		error = agrport_ioctl(port, cmd, arg);
    774   1.1      yamt 		break;
    775   1.1      yamt 	default:
    776   1.1      yamt 		error = EBUSY;
    777   1.1      yamt 		break;
    778   1.1      yamt 	}
    779   1.1      yamt 	return error;
    780   1.1      yamt }
    781   1.1      yamt 
    782   1.1      yamt static int
    783   1.1      yamt agrreq_copyin(const void *ubuf, struct agrreq *ar)
    784   1.1      yamt {
    785   1.1      yamt 	int error;
    786   1.1      yamt 
    787   1.1      yamt 	error = copyin(ubuf, ar, sizeof(*ar));
    788   1.1      yamt 	if (error) {
    789   1.1      yamt 		return error;
    790   1.1      yamt 	}
    791   1.1      yamt 
    792   1.1      yamt 	if (ar->ar_version != AGRREQ_VERSION) {
    793   1.1      yamt 		return EINVAL;
    794   1.1      yamt 	}
    795   1.1      yamt 
    796   1.1      yamt 	return 0;
    797   1.1      yamt }
    798   1.1      yamt 
    799   1.1      yamt static int
    800   1.1      yamt agrreq_copyout(void *ubuf, struct agrreq *ar)
    801   1.1      yamt {
    802   1.1      yamt 	int error;
    803   1.1      yamt 
    804   1.1      yamt 	KASSERT(ar->ar_version == AGRREQ_VERSION);
    805   1.1      yamt 
    806   1.1      yamt 	error = copyout(ar, ubuf, sizeof(*ar));
    807   1.1      yamt 	if (error) {
    808   1.1      yamt 		return error;
    809   1.1      yamt 	}
    810   1.1      yamt 
    811   1.1      yamt 	return 0;
    812   1.1      yamt }
    813   1.1      yamt 
    814   1.1      yamt static int
    815  1.11  christos agr_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    816   1.1      yamt {
    817   1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    818   1.1      yamt 	struct ifreq *ifr = (struct ifreq *)data;
    819   1.1      yamt 	struct ifaddr *ifa = (struct ifaddr *)data;
    820   1.1      yamt 	struct sockaddr *sa;
    821   1.1      yamt 	struct agrreq ar;
    822   1.1      yamt 	struct proc *p;
    823   1.1      yamt 	int error = 0;
    824   1.1      yamt 	int s;
    825   1.1      yamt 
    826   1.1      yamt 	agr_ioctl_lock(sc);
    827   1.1      yamt 
    828   1.1      yamt 	s = splnet();
    829   1.1      yamt 
    830   1.1      yamt 	switch (cmd) {
    831   1.1      yamt 	case SIOCSIFADDR:
    832   1.1      yamt 		if (sc->sc_nports == 0) {
    833   1.1      yamt 			error = EINVAL;
    834   1.1      yamt 			break;
    835   1.1      yamt 		}
    836   1.1      yamt 		ifp->if_flags |= IFF_UP;
    837   1.1      yamt 		switch (ifa->ifa_addr->sa_family) {
    838   1.1      yamt #if defined(INET)
    839   1.1      yamt 		case AF_INET:
    840   1.1      yamt 			arp_ifinit(ifp, ifa);
    841   1.1      yamt 			break;
    842   1.1      yamt #endif
    843   1.1      yamt 		default:
    844   1.1      yamt 			break;
    845   1.1      yamt 		}
    846   1.1      yamt 		break;
    847   1.1      yamt 
    848   1.1      yamt 	case SIOCGIFADDR:
    849   1.1      yamt 		sa = (struct sockaddr *)&ifr->ifr_data;
    850   1.1      yamt 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ifp->if_addrlen);
    851   1.1      yamt 		break;
    852   1.1      yamt 
    853   1.1      yamt #if 0 /* notyet */
    854   1.1      yamt 	case SIOCSIFMTU:
    855   1.1      yamt #endif
    856   1.1      yamt 
    857   1.1      yamt 	case SIOCSIFFLAGS:
    858   1.1      yamt 		agr_config_promisc(sc);
    859   1.1      yamt 		break;
    860   1.1      yamt 
    861   1.1      yamt 	case SIOCSETAGR:
    862   1.1      yamt 		splx(s);
    863   1.1      yamt 		p = curproc; /* XXX */
    864   1.7      elad 		error = kauth_authorize_network(p->p_cred,
    865   1.7      elad 		    KAUTH_NETWORK_INTERFACE,
    866   1.7      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    867   1.7      elad 		    NULL);
    868   1.1      yamt 		if (!error) {
    869   1.1      yamt 			error = agrreq_copyin(ifr->ifr_data, &ar);
    870   1.1      yamt 		}
    871   1.1      yamt 		if (!error) {
    872   1.1      yamt 			error = agr_setconfig(ifp, &ar);
    873   1.1      yamt 		}
    874   1.1      yamt 		s = splnet();
    875   1.1      yamt 		break;
    876   1.1      yamt 
    877   1.1      yamt 	case SIOCGETAGR:
    878   1.1      yamt 		splx(s);
    879   1.1      yamt 		error = agrreq_copyin(ifr->ifr_data, &ar);
    880   1.1      yamt 		if (!error) {
    881   1.1      yamt 			error = agr_getconfig(ifp, &ar);
    882   1.1      yamt 		}
    883   1.1      yamt 		if (!error) {
    884   1.1      yamt 			error = agrreq_copyout(ifr->ifr_data, &ar);
    885   1.1      yamt 		}
    886   1.1      yamt 		s = splnet();
    887   1.1      yamt 		break;
    888   1.1      yamt 
    889   1.1      yamt 	case SIOCADDMULTI:
    890   1.1      yamt 	case SIOCDELMULTI:
    891   1.1      yamt 		if (sc->sc_nports == 0) {
    892   1.1      yamt 			error = EINVAL;
    893   1.1      yamt 			break;
    894   1.1      yamt 		}
    895   1.1      yamt 		error = agr_ioctl_multi(ifp, cmd, ifr);
    896   1.1      yamt 		break;
    897   1.1      yamt 
    898   1.1      yamt 	default:
    899   1.1      yamt 		error = EINVAL;
    900   1.1      yamt 		break;
    901   1.1      yamt 	}
    902   1.1      yamt 
    903   1.1      yamt 	splx(s);
    904   1.1      yamt 
    905   1.1      yamt 	agr_ioctl_unlock(sc);
    906   1.1      yamt 
    907   1.1      yamt 	return error;
    908   1.1      yamt }
    909   1.1      yamt 
    910   1.1      yamt static int
    911   1.1      yamt agr_config_promisc(struct agr_softc *sc)
    912   1.1      yamt {
    913   1.1      yamt 	int error;
    914   1.1      yamt 
    915   1.1      yamt 	agr_port_foreach(sc, agrport_config_promisc_callback, &error);
    916   1.1      yamt 
    917   1.1      yamt 	return error;
    918   1.1      yamt }
    919   1.1      yamt 
    920   1.1      yamt static int
    921   1.1      yamt agrport_config_promisc_callback(struct agr_port *port, void *arg)
    922   1.1      yamt {
    923   1.1      yamt 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
    924   1.1      yamt 	int *errorp = arg;
    925   1.1      yamt 	int error;
    926   1.9   thorpej 	bool promisc;
    927   1.1      yamt 
    928   1.1      yamt 	promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
    929   1.1      yamt 
    930   1.1      yamt 	error = agrport_config_promisc(port, promisc);
    931   1.1      yamt 	if (error) {
    932   1.1      yamt 		*errorp = error;
    933   1.1      yamt 	}
    934   1.1      yamt 
    935   1.1      yamt 	return 0;
    936   1.1      yamt }
    937   1.1      yamt 
    938   1.1      yamt static int
    939   1.9   thorpej agrport_config_promisc(struct agr_port *port, bool promisc)
    940   1.1      yamt {
    941   1.1      yamt 	int error;
    942   1.1      yamt 
    943   1.1      yamt 	if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
    944   1.1      yamt 	    (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
    945   1.1      yamt 		return 0;
    946   1.1      yamt 	}
    947   1.1      yamt 
    948   1.1      yamt 	error = ifpromisc(port->port_ifp, promisc);
    949   1.1      yamt 	if (error == 0) {
    950   1.1      yamt 		if (promisc) {
    951   1.1      yamt 			port->port_flags |= AGRPORT_PROMISC;
    952   1.1      yamt 		} else {
    953   1.1      yamt 			port->port_flags &= ~AGRPORT_PROMISC;
    954   1.1      yamt 		}
    955   1.1      yamt 	}
    956   1.1      yamt 
    957   1.1      yamt 	return error;
    958   1.1      yamt }
    959