Home | History | Annotate | Line # | Download | only in agr
if_agr.c revision 1.3.12.1
      1  1.3.12.1  tron /*	$NetBSD: if_agr.c,v 1.3.12.1 2006/05/24 15:50:44 tron 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.3.12.1  tron __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.3.12.1 2006/05/24 15:50:44 tron 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.3.12.1  tron #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.1  yamt static int agr_ioctl(struct ifnet *, u_long, caddr_t);
     77       1.1  yamt static struct agr_port *agr_select_tx_port(struct agr_softc *, struct mbuf *);
     78       1.1  yamt static int agr_ioctl_filter(struct ifnet *, u_long, caddr_t);
     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.1  yamt static int agrport_config_promisc(struct agr_port *, boolean_t);
     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.1  yamt agrport_ioctl(struct agr_port *port, u_long cmd, caddr_t 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.1  yamt 	error = copyin(ar->ar_buf, ifname, MIN(ar->ar_buflen, sizeof(ifname)));
    366       1.1  yamt 	if (error) {
    367       1.1  yamt 		return error;
    368       1.1  yamt 	}
    369       1.1  yamt 	ifp_port = ifunit(ifname);
    370       1.1  yamt 	if (ifp_port == NULL) {
    371       1.1  yamt 		return ENOENT;
    372       1.1  yamt 	}
    373       1.1  yamt 
    374       1.1  yamt 	switch (cmd) {
    375       1.1  yamt 	case AGRCMD_ADDPORT:
    376       1.1  yamt 		error = agr_addport(ifp, ifp_port);
    377       1.1  yamt 		break;
    378       1.1  yamt 
    379       1.1  yamt 	case AGRCMD_REMPORT:
    380       1.1  yamt 		error = agr_remport(ifp, ifp_port);
    381       1.1  yamt 		break;
    382       1.1  yamt 
    383       1.1  yamt 	default:
    384       1.1  yamt 		error = EINVAL;
    385       1.1  yamt 		break;
    386       1.1  yamt 	}
    387       1.1  yamt 
    388       1.1  yamt 	return error;
    389       1.1  yamt }
    390       1.1  yamt 
    391       1.1  yamt static int
    392       1.1  yamt agr_getportlist(struct ifnet *ifp, struct agrreq *ar)
    393       1.1  yamt {
    394       1.1  yamt 	struct agr_softc *sc = ifp->if_softc;
    395       1.1  yamt 	struct agr_port *port;
    396       1.1  yamt 	struct agrportlist apl;
    397       1.1  yamt 	struct agrportinfo api;
    398       1.1  yamt 	char *cp = ar->ar_buf;
    399       1.1  yamt 	size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
    400       1.1  yamt 	int error;
    401       1.1  yamt 
    402       1.1  yamt 	if (cp != NULL) {
    403       1.1  yamt 		memset(&apl, 0, sizeof(apl));
    404       1.1  yamt 		memset(&api, 0, sizeof(api));
    405       1.1  yamt 
    406       1.1  yamt 		if (bufleft < sizeof(apl)) {
    407       1.1  yamt 			return E2BIG;
    408       1.1  yamt 		}
    409       1.1  yamt 		apl.apl_nports = sc->sc_nports;
    410       1.1  yamt 		error = copyout(&apl, cp, sizeof(apl));
    411       1.1  yamt 		if (error) {
    412       1.1  yamt 			return error;
    413       1.1  yamt 		}
    414       1.1  yamt 		cp += sizeof(apl);
    415       1.1  yamt 	}
    416       1.1  yamt 	bufleft -= sizeof(apl);
    417       1.1  yamt 
    418       1.1  yamt 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    419       1.1  yamt 		if (cp != NULL) {
    420       1.1  yamt 			if (bufleft < sizeof(api)) {
    421       1.1  yamt 				return E2BIG;
    422       1.1  yamt 			}
    423       1.1  yamt 			memcpy(api.api_ifname, port->port_ifp->if_xname,
    424       1.1  yamt 			    sizeof(api.api_ifname));
    425       1.1  yamt 			api.api_flags = 0;
    426       1.1  yamt 			if (port->port_flags & AGRPORT_COLLECTING) {
    427       1.1  yamt 				api.api_flags |= AGRPORTINFO_COLLECTING;
    428       1.1  yamt 			}
    429       1.1  yamt 			if (port->port_flags & AGRPORT_DISTRIBUTING) {
    430       1.1  yamt 				api.api_flags |= AGRPORTINFO_DISTRIBUTING;
    431       1.1  yamt 			}
    432       1.1  yamt 			error = copyout(&api, cp, sizeof(api));
    433       1.1  yamt 			if (error) {
    434       1.1  yamt 				return error;
    435       1.1  yamt 			}
    436       1.1  yamt 			cp += sizeof(api);
    437       1.1  yamt 		}
    438       1.1  yamt 		bufleft -= sizeof(api);
    439       1.1  yamt 	}
    440       1.1  yamt 
    441       1.1  yamt 	if (cp == NULL) {
    442       1.1  yamt 		ar->ar_buflen = -bufleft; /* necessary buffer size */
    443       1.1  yamt 	}
    444       1.1  yamt 
    445       1.1  yamt 	return 0;
    446       1.1  yamt }
    447       1.1  yamt 
    448       1.1  yamt static int
    449       1.1  yamt agr_getconfig(struct ifnet *ifp, struct agrreq *ar)
    450       1.1  yamt {
    451       1.1  yamt 	int cmd = ar->ar_cmd;
    452       1.1  yamt 	int error;
    453       1.1  yamt 
    454       1.1  yamt 	switch (cmd) {
    455       1.1  yamt 	case AGRCMD_PORTLIST:
    456       1.1  yamt 		error = agr_getportlist(ifp, ar);
    457       1.1  yamt 		break;
    458       1.1  yamt 
    459       1.1  yamt 	default:
    460       1.1  yamt 		error = EINVAL;
    461       1.1  yamt 		break;
    462       1.1  yamt 	}
    463       1.1  yamt 
    464       1.1  yamt 	return error;
    465       1.1  yamt }
    466       1.1  yamt 
    467       1.1  yamt static int
    468       1.1  yamt agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
    469       1.1  yamt {
    470       1.1  yamt 	struct agr_softc *sc = ifp->if_softc;
    471       1.1  yamt 	struct agr_port *port = NULL;
    472       1.1  yamt 	int error = 0;
    473       1.1  yamt 	int s;
    474       1.1  yamt 
    475       1.1  yamt 	if (ifp_port->if_ioctl == NULL) {
    476       1.1  yamt 		error = EOPNOTSUPP;
    477       1.1  yamt 		goto out;
    478       1.1  yamt 	}
    479       1.1  yamt 
    480       1.1  yamt 	if (ifp_port->if_agrprivate) {
    481       1.1  yamt 		error = EBUSY;
    482       1.1  yamt 		goto out;
    483       1.1  yamt 	}
    484       1.1  yamt 
    485       1.1  yamt 	if (ifp_port->if_start == agr_start) {
    486       1.1  yamt 		error = EINVAL;
    487       1.1  yamt 		goto out;
    488       1.1  yamt 	}
    489       1.1  yamt 
    490       1.1  yamt 	port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
    491       1.1  yamt 	    M_WAITOK | M_ZERO);
    492       1.1  yamt 	if (port == NULL) {
    493       1.1  yamt 		error = ENOMEM;
    494       1.1  yamt 		goto out;
    495       1.1  yamt 	}
    496       1.1  yamt 	port->port_flags = AGRPORT_LARVAL;
    497       1.1  yamt 
    498       1.1  yamt 	if (TAILQ_NEXT(TAILQ_FIRST(&ifp_port->if_addrlist), ifa_list) != NULL) {
    499       1.1  yamt 		error = EBUSY;
    500       1.1  yamt 		goto out;
    501       1.1  yamt 	}
    502       1.1  yamt 
    503       1.1  yamt 	if (sc->sc_nports == 0) {
    504       1.1  yamt 		switch (ifp_port->if_type) {
    505       1.1  yamt 		case IFT_ETHER:
    506       1.1  yamt 			sc->sc_iftop = &agrether_ops;
    507       1.1  yamt 			break;
    508       1.1  yamt 
    509       1.1  yamt 		default:
    510       1.1  yamt 			error = EPROTONOSUPPORT; /* XXX */
    511       1.1  yamt 			goto out;
    512       1.1  yamt 		}
    513       1.1  yamt 
    514       1.1  yamt 		error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
    515       1.1  yamt 		if (error)
    516       1.1  yamt 			goto out;
    517       1.1  yamt 		agrtimer_start(sc);
    518       1.1  yamt 	} else {
    519       1.1  yamt 		if (ifp->if_type != ifp_port->if_type) {
    520       1.1  yamt 			error = EINVAL;
    521       1.1  yamt 			goto out;
    522       1.1  yamt 		}
    523       1.1  yamt 		if (ifp->if_addrlen != ifp_port->if_addrlen) {
    524       1.1  yamt 			error = EINVAL;
    525       1.1  yamt 			goto out;
    526       1.1  yamt 		}
    527       1.1  yamt 	}
    528       1.1  yamt 
    529       1.1  yamt 	memcpy(port->port_origlladdr, LLADDR(ifp_port->if_sadl),
    530       1.1  yamt 	    ifp_port->if_addrlen);
    531       1.1  yamt 
    532       1.1  yamt 	/*
    533       1.1  yamt 	 * start to modify ifp_port.
    534       1.1  yamt 	 */
    535       1.1  yamt 
    536       1.1  yamt 	error = (*ifp_port->if_ioctl)(ifp_port, SIOCSIFADDR,
    537       1.1  yamt 	    (caddr_t)TAILQ_FIRST(&ifp->if_addrlist));
    538       1.1  yamt 
    539       1.1  yamt 	if (error) {
    540       1.1  yamt 		printf("%s: SIOCSIFADDR error %d\n", __func__, error);
    541       1.1  yamt 		goto cleanup;
    542       1.1  yamt 	}
    543       1.1  yamt 	port->port_flags |= AGRPORT_LADDRCHANGED;
    544       1.1  yamt 
    545       1.1  yamt 	ifp->if_type = ifp_port->if_type;
    546       1.1  yamt 	s = AGR_LOCK(sc);
    547       1.1  yamt 
    548       1.1  yamt 	port->port_ifp = ifp_port;
    549       1.1  yamt 	ifp_port->if_agrprivate = port;
    550       1.1  yamt 	port->port_agrifp = ifp;
    551       1.1  yamt 	TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
    552       1.1  yamt 	sc->sc_nports++;
    553       1.1  yamt 
    554       1.1  yamt 	port->port_ioctl = ifp_port->if_ioctl;
    555       1.1  yamt 	ifp_port->if_ioctl = agr_ioctl_filter;
    556       1.1  yamt 
    557       1.1  yamt 	port->port_flags |= AGRPORT_ATTACHED;
    558       1.1  yamt 
    559       1.1  yamt 	AGR_UNLOCK(sc, s);
    560       1.1  yamt 
    561       1.1  yamt 	error = (*sc->sc_iftop->iftop_portinit)(sc, port);
    562       1.1  yamt 	if (error) {
    563       1.1  yamt 		printf("%s: portinit error %d\n", __func__, error);
    564       1.1  yamt 		goto cleanup;
    565       1.1  yamt 	}
    566       1.1  yamt 
    567       1.1  yamt 	ifp->if_flags |= IFF_RUNNING;
    568       1.1  yamt 
    569       1.1  yamt 	agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
    570       1.1  yamt 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, TRUE);
    571       1.1  yamt 	if (error) {
    572       1.1  yamt 		printf("%s: configmulti error %d\n", __func__, error);
    573       1.1  yamt 		goto cleanup;
    574       1.1  yamt 	}
    575       1.1  yamt 
    576       1.1  yamt 	s = AGR_LOCK(sc);
    577       1.1  yamt 	port->port_flags &= ~AGRPORT_LARVAL;
    578       1.1  yamt 	AGR_UNLOCK(sc, s);
    579       1.1  yamt out:
    580       1.1  yamt 	if (error && port) {
    581       1.1  yamt 		free(port, M_DEVBUF);
    582       1.1  yamt 	}
    583       1.1  yamt 	return error;
    584       1.1  yamt 
    585       1.1  yamt cleanup:
    586       1.1  yamt 	if (agrport_cleanup(sc, port)) {
    587       1.1  yamt 		printf("%s: error on cleanup\n", __func__);
    588       1.1  yamt 
    589       1.1  yamt 		port = NULL; /* XXX */
    590       1.1  yamt 	}
    591       1.1  yamt 
    592       1.1  yamt 	if (sc->sc_nports == 0) {
    593       1.1  yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    594       1.1  yamt 		agrtimer_stop(sc);
    595       1.1  yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    596       1.1  yamt 		sc->sc_iftop = NULL;
    597       1.1  yamt 		agr_reset_iftype(ifp);
    598       1.1  yamt 	} else {
    599       1.1  yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    600       1.1  yamt 	}
    601       1.1  yamt 
    602       1.1  yamt 	goto out;
    603       1.1  yamt }
    604       1.1  yamt 
    605       1.1  yamt static int
    606       1.1  yamt agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
    607       1.1  yamt {
    608       1.1  yamt 	struct agr_softc *sc = ifp->if_softc;
    609       1.1  yamt 	struct agr_port *port;
    610       1.1  yamt 	int error = 0;
    611       1.1  yamt 	int s;
    612       1.1  yamt 
    613       1.1  yamt 	if (ifp_port->if_agrprivate == NULL) {
    614       1.1  yamt 		error = ENOENT;
    615       1.1  yamt 		return error;
    616       1.1  yamt 	}
    617       1.1  yamt 
    618       1.1  yamt 	port = ifp_port->if_agrprivate;
    619       1.1  yamt 	if (port->port_agrifp != ifp) {
    620       1.1  yamt 		error = EINVAL;
    621       1.1  yamt 		return error;
    622       1.1  yamt 	}
    623       1.1  yamt 
    624       1.1  yamt 	KASSERT(sc->sc_nports > 0);
    625       1.1  yamt 
    626       1.1  yamt #if 0
    627       1.1  yamt 	if (sc->sc_nports == 1 &&
    628       1.1  yamt 	    TAILQ_NEXT(TAILQ_FIRST(&ifp->if_addrlist), ifa_list) != NULL) {
    629       1.1  yamt 		error = EBUSY;
    630       1.1  yamt 		return error;
    631       1.1  yamt 	}
    632       1.1  yamt #endif
    633       1.1  yamt 
    634       1.1  yamt 	s = AGR_LOCK(sc);
    635       1.1  yamt 	port->port_flags |= AGRPORT_DETACHING;
    636       1.1  yamt 	AGR_UNLOCK(sc, s);
    637       1.1  yamt 
    638       1.1  yamt 	error = (*sc->sc_iftop->iftop_portfini)(sc, port);
    639       1.1  yamt 	if (error) {
    640       1.1  yamt 		/* XXX XXX */
    641       1.1  yamt 		printf("%s: portfini error %d\n", __func__, error);
    642       1.1  yamt 		goto out;
    643       1.1  yamt 	}
    644       1.1  yamt 
    645       1.1  yamt 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, FALSE);
    646       1.1  yamt 	if (error) {
    647       1.1  yamt 		/* XXX XXX */
    648       1.1  yamt 		printf("%s: configmulti_port error %d\n", __func__, error);
    649       1.1  yamt 		goto out;
    650       1.1  yamt 	}
    651       1.1  yamt 
    652       1.1  yamt 	error = agrport_cleanup(sc, port);
    653       1.1  yamt 	if (error) {
    654       1.1  yamt 		/* XXX XXX */
    655       1.1  yamt 		printf("%s: agrport_cleanup error %d\n", __func__, error);
    656       1.1  yamt 		goto out;
    657       1.1  yamt 	}
    658       1.1  yamt 
    659       1.1  yamt 	free(port, M_DEVBUF);
    660       1.1  yamt 
    661       1.1  yamt out:
    662       1.1  yamt 	if (sc->sc_nports == 0) {
    663       1.1  yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    664       1.1  yamt 		agrtimer_stop(sc);
    665       1.1  yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    666       1.1  yamt 		sc->sc_iftop = NULL;
    667       1.1  yamt 		/* XXX should purge all addresses? */
    668       1.1  yamt 		agr_reset_iftype(ifp);
    669       1.1  yamt 	} else {
    670       1.1  yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    671       1.1  yamt 	}
    672       1.1  yamt 
    673       1.1  yamt 	return error;
    674       1.1  yamt }
    675       1.1  yamt 
    676       1.1  yamt static int
    677       1.1  yamt agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
    678       1.1  yamt {
    679       1.1  yamt 	struct ifnet *ifp_port = port->port_ifp;
    680       1.1  yamt 	int error;
    681       1.1  yamt 	int result = 0;
    682       1.1  yamt 	int s;
    683       1.1  yamt 
    684       1.1  yamt 	error = agrport_config_promisc(port, FALSE);
    685       1.1  yamt 	if (error) {
    686       1.1  yamt 		printf("%s: config_promisc error %d\n", __func__, error);
    687       1.1  yamt 		result = error;
    688       1.1  yamt 	}
    689       1.1  yamt 
    690       1.1  yamt 	if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
    691       1.1  yamt #if 0
    692       1.1  yamt 		memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
    693       1.1  yamt 		    ifp_port->if_addrlen);
    694       1.1  yamt 		if (ifp_port->if_init != NULL) {
    695       1.1  yamt 			error = (*ifp_port->if_init)(ifp_port);
    696       1.1  yamt 		}
    697       1.1  yamt #else
    698       1.1  yamt 		struct sockaddr_dl *sdl;
    699       1.1  yamt 		struct ifaddr ifa;
    700       1.1  yamt 		int sdllen;
    701       1.1  yamt 		int addrlen;
    702       1.1  yamt 
    703       1.1  yamt 		addrlen = ifp_port->if_addrlen;
    704       1.1  yamt 		sdllen = sizeof(*sdl) - sizeof(sdl->sdl_data) + addrlen;
    705       1.1  yamt 		sdl = malloc(sdllen, M_TEMP, M_WAITOK);
    706       1.1  yamt 		if (sdl == NULL) {
    707       1.1  yamt 			error = ENOMEM;
    708       1.1  yamt 		} else {
    709       1.1  yamt 			memset(sdl, 0, sdllen);
    710       1.1  yamt 			sdl->sdl_len = sdllen;
    711       1.1  yamt 			sdl->sdl_family = AF_LINK;
    712       1.1  yamt 			sdl->sdl_type = ifp_port->if_type;
    713       1.1  yamt 			sdl->sdl_alen = addrlen;
    714       1.1  yamt 			memcpy(LLADDR(sdl), port->port_origlladdr, addrlen);
    715       1.1  yamt 			memset(&ifa, 0, sizeof(ifa));
    716       1.1  yamt 			ifa.ifa_addr = (struct sockaddr *)sdl;
    717       1.1  yamt 			error = agrport_ioctl(port, SIOCSIFADDR, (caddr_t)&ifa);
    718       1.1  yamt 			free(sdl, M_TEMP);
    719       1.1  yamt 		}
    720       1.1  yamt #endif
    721       1.1  yamt 		if (error) {
    722       1.1  yamt 			printf("%s: if_init error %d\n", __func__, error);
    723       1.1  yamt 			result = error;
    724       1.1  yamt 		} else {
    725       1.1  yamt 			port->port_flags &= ~AGRPORT_LADDRCHANGED;
    726       1.1  yamt 		}
    727       1.1  yamt 	}
    728       1.1  yamt 
    729       1.1  yamt 	s = AGR_LOCK(sc);
    730       1.1  yamt 	if ((port->port_flags & AGRPORT_ATTACHED)) {
    731       1.1  yamt 		ifp_port->if_agrprivate = NULL;
    732       1.1  yamt 
    733       1.1  yamt 		TAILQ_REMOVE(&sc->sc_ports, port, port_q);
    734       1.1  yamt 		sc->sc_nports--;
    735       1.1  yamt 
    736       1.1  yamt 		KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
    737       1.1  yamt 		ifp_port->if_ioctl = port->port_ioctl;
    738       1.1  yamt 
    739       1.1  yamt 		port->port_flags &= ~AGRPORT_ATTACHED;
    740       1.1  yamt 	}
    741       1.1  yamt 	AGR_UNLOCK(sc, s);
    742       1.1  yamt 
    743       1.1  yamt 	return result;
    744       1.1  yamt }
    745       1.1  yamt 
    746       1.1  yamt static int
    747       1.1  yamt agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
    748       1.1  yamt {
    749       1.1  yamt 	struct agr_softc *sc = ifp->if_softc;
    750       1.1  yamt 	int error;
    751       1.1  yamt 
    752       1.1  yamt 	error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
    753       1.1  yamt 	    (cmd == SIOCADDMULTI));
    754       1.1  yamt 
    755       1.1  yamt 	return error;
    756       1.1  yamt }
    757       1.1  yamt 
    758       1.1  yamt /* XXX an incomplete hack; can't filter ioctls handled ifioctl(). */
    759       1.1  yamt static int
    760       1.1  yamt agr_ioctl_filter(struct ifnet *ifp, u_long cmd, caddr_t arg)
    761       1.1  yamt {
    762       1.1  yamt 	struct agr_port *port = ifp->if_agrprivate;
    763       1.1  yamt 	int error;
    764       1.1  yamt 
    765       1.1  yamt 	KASSERT(port);
    766       1.1  yamt 
    767       1.1  yamt 	switch (cmd) {
    768       1.1  yamt 	case SIOCGIFADDR:
    769       1.1  yamt 	case SIOCGIFMEDIA:
    770       1.1  yamt 	case SIOCSIFFLAGS: /* XXX */
    771       1.1  yamt 		error = agrport_ioctl(port, cmd, arg);
    772       1.1  yamt 		break;
    773       1.1  yamt 	default:
    774       1.1  yamt 		error = EBUSY;
    775       1.1  yamt 		break;
    776       1.1  yamt 	}
    777       1.1  yamt 	return error;
    778       1.1  yamt }
    779       1.1  yamt 
    780       1.1  yamt static int
    781       1.1  yamt agrreq_copyin(const void *ubuf, struct agrreq *ar)
    782       1.1  yamt {
    783       1.1  yamt 	int error;
    784       1.1  yamt 
    785       1.1  yamt 	error = copyin(ubuf, ar, sizeof(*ar));
    786       1.1  yamt 	if (error) {
    787       1.1  yamt 		return error;
    788       1.1  yamt 	}
    789       1.1  yamt 
    790       1.1  yamt 	if (ar->ar_version != AGRREQ_VERSION) {
    791       1.1  yamt 		return EINVAL;
    792       1.1  yamt 	}
    793       1.1  yamt 
    794       1.1  yamt 	return 0;
    795       1.1  yamt }
    796       1.1  yamt 
    797       1.1  yamt static int
    798       1.1  yamt agrreq_copyout(void *ubuf, struct agrreq *ar)
    799       1.1  yamt {
    800       1.1  yamt 	int error;
    801       1.1  yamt 
    802       1.1  yamt 	KASSERT(ar->ar_version == AGRREQ_VERSION);
    803       1.1  yamt 
    804       1.1  yamt 	error = copyout(ar, ubuf, sizeof(*ar));
    805       1.1  yamt 	if (error) {
    806       1.1  yamt 		return error;
    807       1.1  yamt 	}
    808       1.1  yamt 
    809       1.1  yamt 	return 0;
    810       1.1  yamt }
    811       1.1  yamt 
    812       1.1  yamt static int
    813       1.1  yamt agr_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    814       1.1  yamt {
    815       1.1  yamt 	struct agr_softc *sc = ifp->if_softc;
    816       1.1  yamt 	struct ifreq *ifr = (struct ifreq *)data;
    817       1.1  yamt 	struct ifaddr *ifa = (struct ifaddr *)data;
    818       1.1  yamt 	struct sockaddr *sa;
    819       1.1  yamt 	struct agrreq ar;
    820       1.1  yamt 	struct proc *p;
    821       1.1  yamt 	int error = 0;
    822       1.1  yamt 	int s;
    823       1.1  yamt 
    824       1.1  yamt 	agr_ioctl_lock(sc);
    825       1.1  yamt 
    826       1.1  yamt 	s = splnet();
    827       1.1  yamt 
    828       1.1  yamt 	switch (cmd) {
    829       1.1  yamt 	case SIOCSIFADDR:
    830       1.1  yamt 		if (sc->sc_nports == 0) {
    831       1.1  yamt 			error = EINVAL;
    832       1.1  yamt 			break;
    833       1.1  yamt 		}
    834       1.1  yamt 		ifp->if_flags |= IFF_UP;
    835       1.1  yamt 		switch (ifa->ifa_addr->sa_family) {
    836       1.1  yamt #if defined(INET)
    837       1.1  yamt 		case AF_INET:
    838       1.1  yamt 			arp_ifinit(ifp, ifa);
    839       1.1  yamt 			break;
    840       1.1  yamt #endif
    841       1.1  yamt 		default:
    842       1.1  yamt 			break;
    843       1.1  yamt 		}
    844       1.1  yamt 		break;
    845       1.1  yamt 
    846       1.1  yamt 	case SIOCGIFADDR:
    847       1.1  yamt 		sa = (struct sockaddr *)&ifr->ifr_data;
    848       1.1  yamt 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ifp->if_addrlen);
    849       1.1  yamt 		break;
    850       1.1  yamt 
    851       1.1  yamt #if 0 /* notyet */
    852       1.1  yamt 	case SIOCSIFMTU:
    853       1.1  yamt #endif
    854       1.1  yamt 
    855       1.1  yamt 	case SIOCSIFFLAGS:
    856       1.1  yamt 		agr_config_promisc(sc);
    857       1.1  yamt 		break;
    858       1.1  yamt 
    859       1.1  yamt 	case SIOCSETAGR:
    860       1.1  yamt 		splx(s);
    861       1.1  yamt 		p = curproc; /* XXX */
    862  1.3.12.1  tron 		error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    863  1.3.12.1  tron 					  &p->p_acflag);
    864       1.1  yamt 		if (!error) {
    865       1.1  yamt 			error = agrreq_copyin(ifr->ifr_data, &ar);
    866       1.1  yamt 		}
    867       1.1  yamt 		if (!error) {
    868       1.1  yamt 			error = agr_setconfig(ifp, &ar);
    869       1.1  yamt 		}
    870       1.1  yamt 		s = splnet();
    871       1.1  yamt 		break;
    872       1.1  yamt 
    873       1.1  yamt 	case SIOCGETAGR:
    874       1.1  yamt 		splx(s);
    875       1.1  yamt 		error = agrreq_copyin(ifr->ifr_data, &ar);
    876       1.1  yamt 		if (!error) {
    877       1.1  yamt 			error = agr_getconfig(ifp, &ar);
    878       1.1  yamt 		}
    879       1.1  yamt 		if (!error) {
    880       1.1  yamt 			error = agrreq_copyout(ifr->ifr_data, &ar);
    881       1.1  yamt 		}
    882       1.1  yamt 		s = splnet();
    883       1.1  yamt 		break;
    884       1.1  yamt 
    885       1.1  yamt 	case SIOCADDMULTI:
    886       1.1  yamt 	case SIOCDELMULTI:
    887       1.1  yamt 		if (sc->sc_nports == 0) {
    888       1.1  yamt 			error = EINVAL;
    889       1.1  yamt 			break;
    890       1.1  yamt 		}
    891       1.1  yamt 		error = agr_ioctl_multi(ifp, cmd, ifr);
    892       1.1  yamt 		break;
    893       1.1  yamt 
    894       1.1  yamt 	default:
    895       1.1  yamt 		error = EINVAL;
    896       1.1  yamt 		break;
    897       1.1  yamt 	}
    898       1.1  yamt 
    899       1.1  yamt 	splx(s);
    900       1.1  yamt 
    901       1.1  yamt 	agr_ioctl_unlock(sc);
    902       1.1  yamt 
    903       1.1  yamt 	return error;
    904       1.1  yamt }
    905       1.1  yamt 
    906       1.1  yamt static int
    907       1.1  yamt agr_config_promisc(struct agr_softc *sc)
    908       1.1  yamt {
    909       1.1  yamt 	int error;
    910       1.1  yamt 
    911       1.1  yamt 	agr_port_foreach(sc, agrport_config_promisc_callback, &error);
    912       1.1  yamt 
    913       1.1  yamt 	return error;
    914       1.1  yamt }
    915       1.1  yamt 
    916       1.1  yamt static int
    917       1.1  yamt agrport_config_promisc_callback(struct agr_port *port, void *arg)
    918       1.1  yamt {
    919       1.1  yamt 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
    920       1.1  yamt 	int *errorp = arg;
    921       1.1  yamt 	int error;
    922       1.1  yamt 	boolean_t promisc;
    923       1.1  yamt 
    924       1.1  yamt 	promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
    925       1.1  yamt 
    926       1.1  yamt 	error = agrport_config_promisc(port, promisc);
    927       1.1  yamt 	if (error) {
    928       1.1  yamt 		*errorp = error;
    929       1.1  yamt 	}
    930       1.1  yamt 
    931       1.1  yamt 	return 0;
    932       1.1  yamt }
    933       1.1  yamt 
    934       1.1  yamt static int
    935       1.1  yamt agrport_config_promisc(struct agr_port *port, boolean_t promisc)
    936       1.1  yamt {
    937       1.1  yamt 	int error;
    938       1.1  yamt 
    939       1.1  yamt 	if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
    940       1.1  yamt 	    (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
    941       1.1  yamt 		return 0;
    942       1.1  yamt 	}
    943       1.1  yamt 
    944       1.1  yamt 	error = ifpromisc(port->port_ifp, promisc);
    945       1.1  yamt 	if (error == 0) {
    946       1.1  yamt 		if (promisc) {
    947       1.1  yamt 			port->port_flags |= AGRPORT_PROMISC;
    948       1.1  yamt 		} else {
    949       1.1  yamt 			port->port_flags &= ~AGRPORT_PROMISC;
    950       1.1  yamt 		}
    951       1.1  yamt 	}
    952       1.1  yamt 
    953       1.1  yamt 	return error;
    954       1.1  yamt }
    955