Home | History | Annotate | Line # | Download | only in agr
if_agr.c revision 1.21.12.1
      1  1.21.12.1      matt /*	$NetBSD: if_agr.c,v 1.21.12.1 2010/04/21 00:28:21 matt 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.21.12.1      matt __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.21.12.1 2010/04/21 00:28:21 matt 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.21.12.1      matt #include <net/if_ether.h>
     53        1.1      yamt 
     54        1.1      yamt #if defined(INET)
     55        1.1      yamt #include <netinet/in.h>
     56        1.1      yamt #include <netinet/if_inarp.h>
     57        1.1      yamt #endif
     58        1.1      yamt 
     59        1.1      yamt #include <net/agr/if_agrvar.h>
     60        1.1      yamt #include <net/agr/if_agrvar_impl.h>
     61        1.1      yamt #include <net/agr/if_agrioctl.h>
     62        1.1      yamt #include <net/agr/if_agrsubr.h>
     63        1.1      yamt #include <net/agr/if_agrethervar.h>
     64        1.1      yamt 
     65  1.21.12.1      matt #include "vlan.h"
     66  1.21.12.1      matt #if NVLAN > 0
     67  1.21.12.1      matt #include <net/if_vlanvar.h>
     68  1.21.12.1      matt #endif
     69  1.21.12.1      matt 
     70        1.1      yamt void agrattach(int);
     71        1.1      yamt 
     72        1.1      yamt static int agr_clone_create(struct if_clone *, int);
     73        1.1      yamt static int agr_clone_destroy(struct ifnet *);
     74        1.1      yamt static void agr_start(struct ifnet *);
     75        1.1      yamt static int agr_setconfig(struct ifnet *, const struct agrreq *);
     76        1.1      yamt static int agr_getconfig(struct ifnet *, struct agrreq *);
     77        1.1      yamt static int agr_getportlist(struct ifnet *, struct agrreq *);
     78        1.1      yamt static int agr_addport(struct ifnet *, struct ifnet *);
     79        1.1      yamt static int agr_remport(struct ifnet *, struct ifnet *);
     80        1.1      yamt static int agrreq_copyin(const void *, struct agrreq *);
     81        1.1      yamt static int agrreq_copyout(void *, struct agrreq *);
     82       1.11  christos static int agr_ioctl(struct ifnet *, u_long, void *);
     83        1.1      yamt static struct agr_port *agr_select_tx_port(struct agr_softc *, struct mbuf *);
     84       1.11  christos static int agr_ioctl_filter(struct ifnet *, u_long, void *);
     85        1.1      yamt static void agr_reset_iftype(struct ifnet *);
     86        1.1      yamt static int agr_config_promisc(struct agr_softc *);
     87        1.1      yamt static int agrport_config_promisc_callback(struct agr_port *, void *);
     88        1.9   thorpej static int agrport_config_promisc(struct agr_port *, bool);
     89        1.1      yamt static int agrport_cleanup(struct agr_softc *, struct agr_port *);
     90  1.21.12.1      matt static int agr_vlan_add(struct agr_port *, void *);
     91  1.21.12.1      matt static int agr_vlan_del(struct agr_port *, void *);
     92  1.21.12.1      matt static void agr_vlan_check(struct ifnet *, struct agr_softc *);
     93        1.1      yamt 
     94        1.1      yamt static struct if_clone agr_cloner =
     95        1.1      yamt     IF_CLONE_INITIALIZER("agr", agr_clone_create, agr_clone_destroy);
     96        1.1      yamt 
     97        1.1      yamt /*
     98        1.1      yamt  * EXPORTED FUNCTIONS
     99        1.1      yamt  */
    100        1.1      yamt 
    101        1.1      yamt /*
    102        1.1      yamt  * agrattch: device attach routine.
    103        1.1      yamt  */
    104        1.1      yamt 
    105        1.1      yamt void
    106        1.1      yamt agrattach(int count)
    107        1.1      yamt {
    108        1.1      yamt 
    109        1.1      yamt 	if_clone_attach(&agr_cloner);
    110        1.1      yamt }
    111        1.1      yamt 
    112        1.1      yamt /*
    113        1.1      yamt  * agr_input: frame collector.
    114        1.1      yamt  */
    115        1.1      yamt 
    116        1.1      yamt void
    117        1.1      yamt agr_input(struct ifnet *ifp_port, struct mbuf *m)
    118        1.1      yamt {
    119        1.1      yamt 	struct agr_port *port;
    120        1.1      yamt 	struct ifnet *ifp;
    121  1.21.12.1      matt #if NVLAN > 0
    122  1.21.12.1      matt 	struct m_tag *mtag;
    123  1.21.12.1      matt #endif
    124        1.1      yamt 
    125        1.1      yamt 	port = ifp_port->if_agrprivate;
    126        1.1      yamt 	KASSERT(port);
    127        1.1      yamt 	ifp = port->port_agrifp;
    128        1.1      yamt 	if ((port->port_flags & AGRPORT_COLLECTING) == 0) {
    129        1.1      yamt 		m_freem(m);
    130        1.1      yamt 		ifp->if_ierrors++;
    131        1.1      yamt 		return;
    132        1.1      yamt 	}
    133        1.1      yamt 
    134        1.1      yamt 	ifp->if_ipackets++;
    135        1.1      yamt 	m->m_pkthdr.rcvif = ifp;
    136        1.1      yamt 
    137        1.1      yamt #if NBPFILTER > 0
    138        1.1      yamt 	if (ifp->if_bpf) {
    139        1.1      yamt 		bpf_mtap(ifp->if_bpf, m);
    140        1.1      yamt 	}
    141        1.1      yamt #endif
    142        1.1      yamt 
    143  1.21.12.1      matt #if NVLAN > 0
    144  1.21.12.1      matt 	/* got a vlan packet? */
    145  1.21.12.1      matt 	if ((mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL)) != NULL) {
    146  1.21.12.1      matt 	    	/* vlan_input will call ether_input */
    147  1.21.12.1      matt 		vlan_input(ifp, m);
    148  1.21.12.1      matt 		return;
    149  1.21.12.1      matt 	}
    150  1.21.12.1      matt #endif
    151  1.21.12.1      matt 
    152        1.1      yamt 	(*ifp->if_input)(ifp, m);
    153        1.1      yamt }
    154        1.1      yamt 
    155        1.1      yamt /*
    156        1.1      yamt  * EXPORTED AGR-INTERNAL FUNCTIONS
    157        1.1      yamt  */
    158        1.1      yamt 
    159       1.12      yamt void
    160        1.1      yamt agr_lock(struct agr_softc *sc)
    161        1.1      yamt {
    162        1.1      yamt 
    163       1.12      yamt 	mutex_enter(&sc->sc_lock);
    164        1.1      yamt }
    165        1.1      yamt 
    166        1.1      yamt void
    167       1.12      yamt agr_unlock(struct agr_softc *sc)
    168        1.1      yamt {
    169        1.1      yamt 
    170       1.12      yamt 	mutex_exit(&sc->sc_lock);
    171        1.1      yamt }
    172        1.1      yamt 
    173        1.1      yamt void
    174        1.1      yamt agr_ioctl_lock(struct agr_softc *sc)
    175        1.1      yamt {
    176        1.1      yamt 
    177       1.12      yamt 	mutex_enter(&sc->sc_ioctl_lock);
    178        1.1      yamt }
    179        1.1      yamt 
    180        1.1      yamt void
    181        1.1      yamt agr_ioctl_unlock(struct agr_softc *sc)
    182        1.1      yamt {
    183        1.1      yamt 
    184       1.12      yamt 	mutex_exit(&sc->sc_ioctl_lock);
    185        1.1      yamt }
    186        1.1      yamt 
    187        1.1      yamt /*
    188        1.1      yamt  * agr_xmit_frame: transmit a pre-built frame.
    189        1.1      yamt  */
    190        1.1      yamt 
    191        1.1      yamt int
    192        1.1      yamt agr_xmit_frame(struct ifnet *ifp_port, struct mbuf *m)
    193        1.1      yamt {
    194        1.1      yamt 	int error;
    195        1.1      yamt 
    196        1.1      yamt 	struct sockaddr_storage dst0;
    197        1.1      yamt 	struct sockaddr *dst;
    198        1.1      yamt 	int hdrlen;
    199        1.1      yamt 
    200        1.1      yamt 	/*
    201        1.1      yamt 	 * trim off link level header and let if_output re-add it.
    202        1.1      yamt 	 * XXX better to introduce an API to transmit pre-built frames.
    203        1.1      yamt 	 */
    204        1.1      yamt 
    205        1.1      yamt 	hdrlen = ifp_port->if_hdrlen;
    206        1.1      yamt 	if (m->m_pkthdr.len < hdrlen) {
    207        1.1      yamt 		m_freem(m);
    208        1.1      yamt 		return EINVAL;
    209        1.1      yamt 	}
    210        1.1      yamt 	memset(&dst0, 0, sizeof(dst0));
    211        1.1      yamt 	dst = (struct sockaddr *)&dst0;
    212        1.1      yamt 	dst->sa_family = pseudo_AF_HDRCMPLT;
    213        1.1      yamt 	dst->sa_len = hdrlen;
    214        1.1      yamt 	m_copydata(m, 0, hdrlen, &dst->sa_data);
    215        1.1      yamt 	m_adj(m, hdrlen);
    216        1.1      yamt 
    217        1.1      yamt 	error = (*ifp_port->if_output)(ifp_port, m, dst, NULL);
    218        1.1      yamt 
    219        1.1      yamt 	return error;
    220        1.1      yamt }
    221        1.1      yamt 
    222        1.1      yamt int
    223       1.11  christos agrport_ioctl(struct agr_port *port, u_long cmd, void *arg)
    224        1.1      yamt {
    225        1.1      yamt 	struct ifnet *ifp = port->port_ifp;
    226        1.1      yamt 
    227        1.1      yamt 	KASSERT(ifp->if_agrprivate == (void *)port);
    228        1.1      yamt 	KASSERT(ifp->if_ioctl == agr_ioctl_filter);
    229        1.1      yamt 
    230        1.1      yamt 	return (*port->port_ioctl)(ifp, cmd, arg);
    231        1.1      yamt }
    232        1.1      yamt 
    233        1.1      yamt /*
    234        1.1      yamt  * INTERNAL FUNCTIONS
    235        1.1      yamt  */
    236        1.1      yamt 
    237  1.21.12.1      matt /*
    238  1.21.12.1      matt  * Enable vlan hardware assist for the specified port.
    239  1.21.12.1      matt  */
    240  1.21.12.1      matt static int
    241  1.21.12.1      matt agr_vlan_add(struct agr_port *port, void *arg)
    242  1.21.12.1      matt {
    243  1.21.12.1      matt 	struct ifnet *ifp = port->port_ifp;
    244  1.21.12.1      matt 	struct ethercom *ec_port = (void *)ifp;
    245  1.21.12.1      matt 	struct ifreq ifr;
    246  1.21.12.1      matt 	int error=0;
    247  1.21.12.1      matt 
    248  1.21.12.1      matt 	if (ec_port->ec_nvlans++ == 0 &&
    249  1.21.12.1      matt 	    (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    250  1.21.12.1      matt 		struct ifnet *p = port->port_ifp;
    251  1.21.12.1      matt 		/*
    252  1.21.12.1      matt 		 * Enable Tx/Rx of VLAN-sized frames.
    253  1.21.12.1      matt 		 */
    254  1.21.12.1      matt 		ec_port->ec_capenable |= ETHERCAP_VLAN_MTU;
    255  1.21.12.1      matt 		if (p->if_flags & IFF_UP) {
    256  1.21.12.1      matt 			ifr.ifr_flags = p->if_flags;
    257  1.21.12.1      matt 			error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    258  1.21.12.1      matt 			    (void *) &ifr);
    259  1.21.12.1      matt 			if (error) {
    260  1.21.12.1      matt 				if (ec_port->ec_nvlans-- == 1)
    261  1.21.12.1      matt 					ec_port->ec_capenable &=
    262  1.21.12.1      matt 					    ~ETHERCAP_VLAN_MTU;
    263  1.21.12.1      matt 				return (error);
    264  1.21.12.1      matt 			}
    265  1.21.12.1      matt 		}
    266  1.21.12.1      matt 	}
    267  1.21.12.1      matt 
    268  1.21.12.1      matt 	return error;
    269  1.21.12.1      matt }
    270  1.21.12.1      matt 
    271  1.21.12.1      matt /*
    272  1.21.12.1      matt  * Disable vlan hardware assist for the specified port.
    273  1.21.12.1      matt  */
    274  1.21.12.1      matt static int
    275  1.21.12.1      matt agr_vlan_del(struct agr_port *port, void *arg)
    276  1.21.12.1      matt {
    277  1.21.12.1      matt 	struct ethercom *ec_port = (void *)port->port_ifp;
    278  1.21.12.1      matt 	struct ifreq ifr;
    279  1.21.12.1      matt 
    280  1.21.12.1      matt 	/* Disable vlan support */
    281  1.21.12.1      matt 	if (ec_port->ec_nvlans-- == 1) {
    282  1.21.12.1      matt 		/*
    283  1.21.12.1      matt 		 * Disable Tx/Rx of VLAN-sized frames.
    284  1.21.12.1      matt 		 */
    285  1.21.12.1      matt 		ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    286  1.21.12.1      matt 		if (port->port_ifp->if_flags & IFF_UP) {
    287  1.21.12.1      matt 			ifr.ifr_flags = port->port_ifp->if_flags;
    288  1.21.12.1      matt 			(void) (*port->port_ifp->if_ioctl)(port->port_ifp,
    289  1.21.12.1      matt 			    SIOCSIFFLAGS, (void *) &ifr);
    290  1.21.12.1      matt 		}
    291  1.21.12.1      matt 	}
    292  1.21.12.1      matt 
    293  1.21.12.1      matt 	return 0;
    294  1.21.12.1      matt }
    295  1.21.12.1      matt 
    296  1.21.12.1      matt 
    297  1.21.12.1      matt /*
    298  1.21.12.1      matt  * Check for vlan attach/detach.
    299  1.21.12.1      matt  * ec->ec_nvlans is directly modified by the vlan driver.
    300  1.21.12.1      matt  * We keep a local count in sc (sc->sc_nvlans) to detect
    301  1.21.12.1      matt  * when the vlan driver attaches or detaches.
    302  1.21.12.1      matt  * Note the agr interface must be up for this to work.
    303  1.21.12.1      matt  */
    304  1.21.12.1      matt static void
    305  1.21.12.1      matt agr_vlan_check(struct ifnet *ifp, struct agr_softc *sc)
    306  1.21.12.1      matt {
    307  1.21.12.1      matt 	struct ethercom *ec = (void *)ifp;
    308  1.21.12.1      matt 	int error;
    309  1.21.12.1      matt 
    310  1.21.12.1      matt 	/* vlans in sync? */
    311  1.21.12.1      matt 	if (sc->sc_nvlans == ec->ec_nvlans) {
    312  1.21.12.1      matt 		return;
    313  1.21.12.1      matt 	}
    314  1.21.12.1      matt 
    315  1.21.12.1      matt 	if (sc->sc_nvlans == 0) {
    316  1.21.12.1      matt 		/* vlan added */
    317  1.21.12.1      matt 		error = agr_port_foreach(sc, agr_vlan_add, NULL);
    318  1.21.12.1      matt 		sc->sc_nvlans = ec->ec_nvlans;
    319  1.21.12.1      matt 	} else if (ec->ec_nvlans == 0) {
    320  1.21.12.1      matt 		/* vlan removed */
    321  1.21.12.1      matt 		error = agr_port_foreach(sc, agr_vlan_del, NULL);
    322  1.21.12.1      matt 		sc->sc_nvlans = 0;
    323  1.21.12.1      matt 	}
    324  1.21.12.1      matt }
    325  1.21.12.1      matt 
    326  1.21.12.1      matt 
    327        1.1      yamt static int
    328        1.1      yamt agr_clone_create(struct if_clone *ifc, int unit)
    329        1.1      yamt {
    330        1.1      yamt 	struct agr_softc *sc;
    331        1.1      yamt 	struct ifnet *ifp;
    332        1.1      yamt 
    333        1.1      yamt 	sc = agr_alloc_softc();
    334        1.1      yamt 	TAILQ_INIT(&sc->sc_ports);
    335       1.12      yamt 	mutex_init(&sc->sc_ioctl_lock, MUTEX_DRIVER, IPL_NONE);
    336       1.12      yamt 	mutex_init(&sc->sc_lock, MUTEX_DRIVER, IPL_NET);
    337        1.1      yamt 	agrtimer_init(sc);
    338        1.1      yamt 	ifp = &sc->sc_if;
    339        1.1      yamt 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
    340        1.1      yamt 	    ifc->ifc_name, unit);
    341        1.1      yamt 
    342        1.1      yamt 	ifp->if_softc = sc;
    343        1.1      yamt 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    344        1.1      yamt 	ifp->if_start = agr_start;
    345        1.1      yamt 	ifp->if_ioctl = agr_ioctl;
    346        1.1      yamt 	IFQ_SET_READY(&ifp->if_snd);
    347        1.1      yamt 
    348        1.1      yamt 	if_attach(ifp);
    349        1.1      yamt 
    350        1.1      yamt 	agr_reset_iftype(ifp);
    351        1.1      yamt 
    352        1.1      yamt 	return 0;
    353        1.1      yamt }
    354        1.1      yamt 
    355        1.1      yamt static void
    356        1.1      yamt agr_reset_iftype(struct ifnet *ifp)
    357        1.1      yamt {
    358        1.1      yamt 
    359        1.1      yamt 	ifp->if_type = IFT_OTHER;
    360        1.1      yamt 	ifp->if_dlt = DLT_NULL;
    361        1.1      yamt 	ifp->if_addrlen = 0;
    362        1.1      yamt 	if_alloc_sadl(ifp);
    363        1.1      yamt }
    364        1.1      yamt 
    365        1.1      yamt static int
    366        1.1      yamt agr_clone_destroy(struct ifnet *ifp)
    367        1.1      yamt {
    368        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    369        1.1      yamt 	int error;
    370        1.1      yamt 
    371        1.1      yamt 	agr_ioctl_lock(sc);
    372        1.1      yamt 
    373       1.12      yamt 	AGR_LOCK(sc);
    374        1.1      yamt 	if (sc->sc_nports > 0) {
    375        1.1      yamt 		error = EBUSY;
    376        1.1      yamt 	} else {
    377        1.1      yamt 		error = 0;
    378        1.1      yamt 	}
    379       1.12      yamt 	AGR_UNLOCK(sc);
    380        1.1      yamt 
    381        1.1      yamt 	agr_ioctl_unlock(sc);
    382        1.1      yamt 
    383        1.1      yamt 	if (error == 0) {
    384        1.1      yamt 		if_detach(ifp);
    385       1.12      yamt 		mutex_destroy(&sc->sc_ioctl_lock);
    386       1.12      yamt 		mutex_destroy(&sc->sc_lock);
    387        1.1      yamt 		agr_free_softc(sc);
    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 struct agr_port *
    394        1.1      yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    395        1.1      yamt {
    396        1.1      yamt 
    397        1.1      yamt 	return (*sc->sc_iftop->iftop_select_tx_port)(sc, m);
    398        1.1      yamt }
    399        1.1      yamt 
    400        1.1      yamt #if 0 /* "generic" version */
    401        1.1      yamt static struct agr_port *
    402        1.1      yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    403        1.1      yamt {
    404        1.1      yamt 	struct agr_port *port;
    405        1.1      yamt 	uint32_t hash;
    406        1.1      yamt 
    407        1.1      yamt 	hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
    408        1.1      yamt 	if (sc->sc_nports == 0)
    409        1.1      yamt 		return NULL;
    410        1.1      yamt 	hash %= sc->sc_nports;
    411        1.1      yamt 	port = TAILQ_FIRST(&sc->sc_ports);
    412        1.1      yamt 	KASSERT(port != NULL);
    413        1.1      yamt 	while (hash--) {
    414        1.1      yamt 		port = TAILQ_NEXT(port, port_q);
    415        1.1      yamt 		KASSERT(port != NULL);
    416        1.1      yamt 	}
    417        1.1      yamt 
    418        1.1      yamt 	return port;
    419        1.1      yamt }
    420        1.1      yamt #endif /* 0 */
    421        1.1      yamt 
    422        1.1      yamt static void
    423        1.1      yamt agr_start(struct ifnet *ifp)
    424        1.1      yamt {
    425        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    426        1.1      yamt 	struct mbuf *m;
    427        1.1      yamt 
    428       1.12      yamt 	AGR_LOCK(sc);
    429        1.1      yamt 
    430        1.1      yamt 	while (/* CONSTCOND */ 1) {
    431        1.1      yamt 		struct agr_port *port;
    432        1.1      yamt 
    433        1.1      yamt 		IFQ_DEQUEUE(&ifp->if_snd, m);
    434        1.1      yamt 		if (m == NULL) {
    435        1.1      yamt 			break;
    436        1.1      yamt 		}
    437        1.1      yamt #if NBPFILTER > 0
    438        1.1      yamt 		if (ifp->if_bpf) {
    439        1.1      yamt 			bpf_mtap(ifp->if_bpf, m);
    440        1.1      yamt 		}
    441        1.1      yamt #endif
    442        1.1      yamt 		port = agr_select_tx_port(sc, m);
    443        1.1      yamt 		if (port) {
    444        1.1      yamt 			int error;
    445        1.1      yamt 
    446        1.1      yamt 			error = agr_xmit_frame(port->port_ifp, m);
    447        1.1      yamt 			if (error) {
    448        1.1      yamt 				ifp->if_oerrors++;
    449        1.1      yamt 			} else {
    450        1.1      yamt 				ifp->if_opackets++;
    451        1.1      yamt 			}
    452        1.1      yamt 		} else {
    453        1.1      yamt 			m_freem(m);
    454        1.1      yamt 			ifp->if_oerrors++;
    455        1.1      yamt 		}
    456        1.1      yamt 	}
    457        1.1      yamt 
    458       1.12      yamt 	AGR_UNLOCK(sc);
    459        1.1      yamt 
    460        1.1      yamt 	ifp->if_flags &= ~IFF_OACTIVE;
    461        1.1      yamt }
    462        1.1      yamt 
    463        1.1      yamt static int
    464        1.1      yamt agr_setconfig(struct ifnet *ifp, const struct agrreq *ar)
    465        1.1      yamt {
    466        1.1      yamt 	int cmd = ar->ar_cmd;
    467        1.1      yamt 	struct ifnet *ifp_port;
    468        1.1      yamt 	int error = 0;
    469        1.1      yamt 	char ifname[IFNAMSIZ];
    470        1.1      yamt 
    471        1.8      yamt 	memset(ifname, 0, sizeof(ifname));
    472        1.8      yamt 	error = copyin(ar->ar_buf, ifname,
    473        1.8      yamt 	    MIN(ar->ar_buflen, sizeof(ifname) - 1));
    474        1.1      yamt 	if (error) {
    475        1.1      yamt 		return error;
    476        1.1      yamt 	}
    477        1.1      yamt 	ifp_port = ifunit(ifname);
    478        1.1      yamt 	if (ifp_port == NULL) {
    479        1.1      yamt 		return ENOENT;
    480        1.1      yamt 	}
    481        1.1      yamt 
    482        1.1      yamt 	switch (cmd) {
    483        1.1      yamt 	case AGRCMD_ADDPORT:
    484        1.1      yamt 		error = agr_addport(ifp, ifp_port);
    485        1.1      yamt 		break;
    486        1.1      yamt 
    487        1.1      yamt 	case AGRCMD_REMPORT:
    488        1.1      yamt 		error = agr_remport(ifp, ifp_port);
    489        1.1      yamt 		break;
    490        1.1      yamt 
    491        1.1      yamt 	default:
    492        1.1      yamt 		error = EINVAL;
    493        1.1      yamt 		break;
    494        1.1      yamt 	}
    495        1.1      yamt 
    496        1.1      yamt 	return error;
    497        1.1      yamt }
    498        1.1      yamt 
    499        1.1      yamt static int
    500        1.1      yamt agr_getportlist(struct ifnet *ifp, struct agrreq *ar)
    501        1.1      yamt {
    502        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    503        1.1      yamt 	struct agr_port *port;
    504        1.1      yamt 	struct agrportlist apl;
    505        1.1      yamt 	struct agrportinfo api;
    506        1.1      yamt 	char *cp = ar->ar_buf;
    507        1.1      yamt 	size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
    508        1.1      yamt 	int error;
    509        1.1      yamt 
    510        1.1      yamt 	if (cp != NULL) {
    511        1.1      yamt 		memset(&apl, 0, sizeof(apl));
    512        1.1      yamt 		memset(&api, 0, sizeof(api));
    513        1.1      yamt 
    514        1.1      yamt 		if (bufleft < sizeof(apl)) {
    515        1.1      yamt 			return E2BIG;
    516        1.1      yamt 		}
    517        1.1      yamt 		apl.apl_nports = sc->sc_nports;
    518        1.1      yamt 		error = copyout(&apl, cp, sizeof(apl));
    519        1.1      yamt 		if (error) {
    520        1.1      yamt 			return error;
    521        1.1      yamt 		}
    522        1.1      yamt 		cp += sizeof(apl);
    523        1.1      yamt 	}
    524        1.1      yamt 	bufleft -= sizeof(apl);
    525        1.1      yamt 
    526        1.1      yamt 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    527        1.1      yamt 		if (cp != NULL) {
    528        1.1      yamt 			if (bufleft < sizeof(api)) {
    529        1.1      yamt 				return E2BIG;
    530        1.1      yamt 			}
    531        1.1      yamt 			memcpy(api.api_ifname, port->port_ifp->if_xname,
    532        1.1      yamt 			    sizeof(api.api_ifname));
    533        1.1      yamt 			api.api_flags = 0;
    534        1.1      yamt 			if (port->port_flags & AGRPORT_COLLECTING) {
    535        1.1      yamt 				api.api_flags |= AGRPORTINFO_COLLECTING;
    536        1.1      yamt 			}
    537        1.1      yamt 			if (port->port_flags & AGRPORT_DISTRIBUTING) {
    538        1.1      yamt 				api.api_flags |= AGRPORTINFO_DISTRIBUTING;
    539        1.1      yamt 			}
    540        1.1      yamt 			error = copyout(&api, cp, sizeof(api));
    541        1.1      yamt 			if (error) {
    542        1.1      yamt 				return error;
    543        1.1      yamt 			}
    544        1.1      yamt 			cp += sizeof(api);
    545        1.1      yamt 		}
    546        1.1      yamt 		bufleft -= sizeof(api);
    547        1.1      yamt 	}
    548        1.1      yamt 
    549        1.1      yamt 	if (cp == NULL) {
    550        1.1      yamt 		ar->ar_buflen = -bufleft; /* necessary buffer size */
    551        1.1      yamt 	}
    552        1.1      yamt 
    553        1.1      yamt 	return 0;
    554        1.1      yamt }
    555        1.1      yamt 
    556        1.1      yamt static int
    557        1.1      yamt agr_getconfig(struct ifnet *ifp, struct agrreq *ar)
    558        1.1      yamt {
    559        1.1      yamt 	int cmd = ar->ar_cmd;
    560        1.1      yamt 	int error;
    561        1.1      yamt 
    562        1.1      yamt 	switch (cmd) {
    563        1.1      yamt 	case AGRCMD_PORTLIST:
    564        1.1      yamt 		error = agr_getportlist(ifp, ar);
    565        1.1      yamt 		break;
    566        1.1      yamt 
    567        1.1      yamt 	default:
    568        1.1      yamt 		error = EINVAL;
    569        1.1      yamt 		break;
    570        1.1      yamt 	}
    571        1.1      yamt 
    572        1.1      yamt 	return error;
    573        1.1      yamt }
    574        1.1      yamt 
    575        1.1      yamt static int
    576        1.1      yamt agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
    577        1.1      yamt {
    578       1.20    dyoung 	const struct ifaddr *ifa;
    579        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    580        1.1      yamt 	struct agr_port *port = NULL;
    581        1.1      yamt 	int error = 0;
    582        1.1      yamt 
    583        1.1      yamt 	if (ifp_port->if_ioctl == NULL) {
    584        1.1      yamt 		error = EOPNOTSUPP;
    585        1.1      yamt 		goto out;
    586        1.1      yamt 	}
    587        1.1      yamt 
    588        1.1      yamt 	if (ifp_port->if_agrprivate) {
    589        1.1      yamt 		error = EBUSY;
    590        1.1      yamt 		goto out;
    591        1.1      yamt 	}
    592        1.1      yamt 
    593        1.1      yamt 	if (ifp_port->if_start == agr_start) {
    594        1.1      yamt 		error = EINVAL;
    595        1.1      yamt 		goto out;
    596        1.1      yamt 	}
    597        1.1      yamt 
    598        1.1      yamt 	port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
    599        1.1      yamt 	    M_WAITOK | M_ZERO);
    600        1.1      yamt 	if (port == NULL) {
    601        1.1      yamt 		error = ENOMEM;
    602        1.1      yamt 		goto out;
    603        1.1      yamt 	}
    604        1.1      yamt 	port->port_flags = AGRPORT_LARVAL;
    605        1.1      yamt 
    606       1.20    dyoung 	IFADDR_FOREACH(ifa, ifp_port) {
    607       1.20    dyoung 		if (ifa->ifa_addr->sa_family != AF_LINK) {
    608       1.20    dyoung 			error = EBUSY;
    609       1.20    dyoung 			goto out;
    610       1.20    dyoung 		}
    611        1.1      yamt 	}
    612        1.1      yamt 
    613        1.1      yamt 	if (sc->sc_nports == 0) {
    614        1.1      yamt 		switch (ifp_port->if_type) {
    615        1.1      yamt 		case IFT_ETHER:
    616        1.1      yamt 			sc->sc_iftop = &agrether_ops;
    617        1.1      yamt 			break;
    618        1.1      yamt 
    619        1.1      yamt 		default:
    620        1.1      yamt 			error = EPROTONOSUPPORT; /* XXX */
    621        1.1      yamt 			goto out;
    622        1.1      yamt 		}
    623        1.1      yamt 
    624        1.1      yamt 		error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
    625        1.1      yamt 		if (error)
    626        1.1      yamt 			goto out;
    627        1.1      yamt 		agrtimer_start(sc);
    628        1.1      yamt 	} else {
    629        1.1      yamt 		if (ifp->if_type != ifp_port->if_type) {
    630        1.1      yamt 			error = EINVAL;
    631        1.1      yamt 			goto out;
    632        1.1      yamt 		}
    633        1.1      yamt 		if (ifp->if_addrlen != ifp_port->if_addrlen) {
    634        1.1      yamt 			error = EINVAL;
    635        1.1      yamt 			goto out;
    636        1.1      yamt 		}
    637        1.1      yamt 	}
    638        1.1      yamt 
    639       1.14    dyoung 	memcpy(port->port_origlladdr, CLLADDR(ifp_port->if_sadl),
    640        1.1      yamt 	    ifp_port->if_addrlen);
    641        1.1      yamt 
    642        1.1      yamt 	/*
    643        1.1      yamt 	 * start to modify ifp_port.
    644        1.1      yamt 	 */
    645        1.1      yamt 
    646        1.1      yamt 	error = (*ifp_port->if_ioctl)(ifp_port, SIOCSIFADDR,
    647       1.20    dyoung 	    (void *)ifp->if_dl);
    648        1.1      yamt 
    649        1.1      yamt 	if (error) {
    650        1.1      yamt 		printf("%s: SIOCSIFADDR error %d\n", __func__, error);
    651        1.1      yamt 		goto cleanup;
    652        1.1      yamt 	}
    653        1.1      yamt 	port->port_flags |= AGRPORT_LADDRCHANGED;
    654        1.1      yamt 
    655        1.1      yamt 	ifp->if_type = ifp_port->if_type;
    656       1.12      yamt 	AGR_LOCK(sc);
    657        1.1      yamt 
    658        1.1      yamt 	port->port_ifp = ifp_port;
    659        1.1      yamt 	ifp_port->if_agrprivate = port;
    660        1.1      yamt 	port->port_agrifp = ifp;
    661        1.1      yamt 	TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
    662        1.1      yamt 	sc->sc_nports++;
    663        1.1      yamt 
    664        1.1      yamt 	port->port_ioctl = ifp_port->if_ioctl;
    665        1.1      yamt 	ifp_port->if_ioctl = agr_ioctl_filter;
    666        1.1      yamt 
    667        1.1      yamt 	port->port_flags |= AGRPORT_ATTACHED;
    668        1.1      yamt 
    669       1.12      yamt 	AGR_UNLOCK(sc);
    670        1.1      yamt 
    671        1.1      yamt 	error = (*sc->sc_iftop->iftop_portinit)(sc, port);
    672        1.1      yamt 	if (error) {
    673        1.1      yamt 		printf("%s: portinit error %d\n", __func__, error);
    674        1.1      yamt 		goto cleanup;
    675        1.1      yamt 	}
    676        1.1      yamt 
    677        1.1      yamt 	ifp->if_flags |= IFF_RUNNING;
    678        1.1      yamt 
    679        1.1      yamt 	agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
    680       1.10   thorpej 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, true);
    681        1.1      yamt 	if (error) {
    682        1.1      yamt 		printf("%s: configmulti error %d\n", __func__, error);
    683        1.1      yamt 		goto cleanup;
    684        1.1      yamt 	}
    685        1.1      yamt 
    686       1.12      yamt 	AGR_LOCK(sc);
    687        1.1      yamt 	port->port_flags &= ~AGRPORT_LARVAL;
    688       1.12      yamt 	AGR_UNLOCK(sc);
    689        1.1      yamt out:
    690        1.1      yamt 	if (error && port) {
    691        1.1      yamt 		free(port, M_DEVBUF);
    692        1.1      yamt 	}
    693        1.1      yamt 	return error;
    694        1.1      yamt 
    695        1.1      yamt cleanup:
    696        1.1      yamt 	if (agrport_cleanup(sc, port)) {
    697        1.1      yamt 		printf("%s: error on cleanup\n", __func__);
    698        1.1      yamt 
    699        1.1      yamt 		port = NULL; /* XXX */
    700        1.1      yamt 	}
    701        1.1      yamt 
    702        1.1      yamt 	if (sc->sc_nports == 0) {
    703        1.1      yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    704        1.1      yamt 		agrtimer_stop(sc);
    705        1.1      yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    706        1.1      yamt 		sc->sc_iftop = NULL;
    707        1.1      yamt 		agr_reset_iftype(ifp);
    708        1.1      yamt 	} else {
    709        1.1      yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    710        1.1      yamt 	}
    711        1.1      yamt 
    712        1.1      yamt 	goto out;
    713        1.1      yamt }
    714        1.1      yamt 
    715        1.1      yamt static int
    716        1.1      yamt agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
    717        1.1      yamt {
    718        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    719        1.1      yamt 	struct agr_port *port;
    720        1.1      yamt 	int error = 0;
    721        1.1      yamt 
    722        1.1      yamt 	if (ifp_port->if_agrprivate == NULL) {
    723        1.1      yamt 		error = ENOENT;
    724        1.1      yamt 		return error;
    725        1.1      yamt 	}
    726        1.1      yamt 
    727        1.1      yamt 	port = ifp_port->if_agrprivate;
    728        1.1      yamt 	if (port->port_agrifp != ifp) {
    729        1.1      yamt 		error = EINVAL;
    730        1.1      yamt 		return error;
    731        1.1      yamt 	}
    732        1.1      yamt 
    733        1.1      yamt 	KASSERT(sc->sc_nports > 0);
    734        1.1      yamt 
    735       1.12      yamt 	AGR_LOCK(sc);
    736        1.1      yamt 	port->port_flags |= AGRPORT_DETACHING;
    737       1.12      yamt 	AGR_UNLOCK(sc);
    738        1.1      yamt 
    739        1.1      yamt 	error = (*sc->sc_iftop->iftop_portfini)(sc, port);
    740        1.1      yamt 	if (error) {
    741        1.1      yamt 		/* XXX XXX */
    742        1.1      yamt 		printf("%s: portfini error %d\n", __func__, error);
    743        1.1      yamt 		goto out;
    744        1.1      yamt 	}
    745        1.1      yamt 
    746       1.10   thorpej 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, false);
    747        1.1      yamt 	if (error) {
    748        1.1      yamt 		/* XXX XXX */
    749        1.1      yamt 		printf("%s: configmulti_port error %d\n", __func__, error);
    750        1.1      yamt 		goto out;
    751        1.1      yamt 	}
    752        1.1      yamt 
    753        1.1      yamt 	error = agrport_cleanup(sc, port);
    754        1.1      yamt 	if (error) {
    755        1.1      yamt 		/* XXX XXX */
    756        1.1      yamt 		printf("%s: agrport_cleanup error %d\n", __func__, error);
    757        1.1      yamt 		goto out;
    758        1.1      yamt 	}
    759        1.1      yamt 
    760        1.1      yamt 	free(port, M_DEVBUF);
    761        1.1      yamt 
    762        1.1      yamt out:
    763        1.1      yamt 	if (sc->sc_nports == 0) {
    764        1.1      yamt 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    765        1.1      yamt 		agrtimer_stop(sc);
    766        1.1      yamt 		(*sc->sc_iftop->iftop_dtor)(sc);
    767        1.1      yamt 		sc->sc_iftop = NULL;
    768        1.1      yamt 		/* XXX should purge all addresses? */
    769        1.1      yamt 		agr_reset_iftype(ifp);
    770        1.1      yamt 	} else {
    771        1.1      yamt 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    772        1.1      yamt 	}
    773        1.1      yamt 
    774        1.1      yamt 	return error;
    775        1.1      yamt }
    776        1.1      yamt 
    777        1.1      yamt static int
    778        1.1      yamt agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
    779        1.1      yamt {
    780        1.1      yamt 	struct ifnet *ifp_port = port->port_ifp;
    781        1.1      yamt 	int error;
    782        1.1      yamt 	int result = 0;
    783        1.1      yamt 
    784       1.10   thorpej 	error = agrport_config_promisc(port, false);
    785        1.1      yamt 	if (error) {
    786        1.1      yamt 		printf("%s: config_promisc error %d\n", __func__, error);
    787        1.1      yamt 		result = error;
    788        1.1      yamt 	}
    789        1.1      yamt 
    790        1.1      yamt 	if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
    791        1.1      yamt #if 0
    792        1.1      yamt 		memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
    793        1.1      yamt 		    ifp_port->if_addrlen);
    794        1.1      yamt 		if (ifp_port->if_init != NULL) {
    795        1.1      yamt 			error = (*ifp_port->if_init)(ifp_port);
    796        1.1      yamt 		}
    797        1.1      yamt #else
    798       1.16    dyoung 		union {
    799       1.16    dyoung 			struct sockaddr sa;
    800       1.16    dyoung 			struct sockaddr_dl sdl;
    801       1.16    dyoung 			struct sockaddr_storage ss;
    802       1.16    dyoung 		} u;
    803        1.1      yamt 		struct ifaddr ifa;
    804        1.1      yamt 
    805       1.16    dyoung 		sockaddr_dl_init(&u.sdl, sizeof(u.ss),
    806       1.16    dyoung 		    0, ifp_port->if_type, NULL, 0,
    807       1.16    dyoung 		    port->port_origlladdr, ifp_port->if_addrlen);
    808       1.16    dyoung 		memset(&ifa, 0, sizeof(ifa));
    809       1.16    dyoung 		ifa.ifa_addr = &u.sa;
    810       1.16    dyoung 		error = agrport_ioctl(port, SIOCSIFADDR, &ifa);
    811        1.1      yamt #endif
    812        1.1      yamt 		if (error) {
    813        1.1      yamt 			printf("%s: if_init error %d\n", __func__, error);
    814        1.1      yamt 			result = error;
    815        1.1      yamt 		} else {
    816        1.1      yamt 			port->port_flags &= ~AGRPORT_LADDRCHANGED;
    817        1.1      yamt 		}
    818        1.1      yamt 	}
    819        1.1      yamt 
    820       1.12      yamt 	AGR_LOCK(sc);
    821        1.1      yamt 	if ((port->port_flags & AGRPORT_ATTACHED)) {
    822        1.1      yamt 		ifp_port->if_agrprivate = NULL;
    823        1.1      yamt 
    824        1.1      yamt 		TAILQ_REMOVE(&sc->sc_ports, port, port_q);
    825        1.1      yamt 		sc->sc_nports--;
    826        1.1      yamt 
    827        1.1      yamt 		KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
    828        1.1      yamt 		ifp_port->if_ioctl = port->port_ioctl;
    829        1.1      yamt 
    830        1.1      yamt 		port->port_flags &= ~AGRPORT_ATTACHED;
    831        1.1      yamt 	}
    832       1.12      yamt 	AGR_UNLOCK(sc);
    833        1.1      yamt 
    834        1.1      yamt 	return result;
    835        1.1      yamt }
    836        1.1      yamt 
    837        1.1      yamt static int
    838        1.1      yamt agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
    839        1.1      yamt {
    840        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    841        1.1      yamt 	int error;
    842        1.1      yamt 
    843        1.1      yamt 	error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
    844        1.1      yamt 	    (cmd == SIOCADDMULTI));
    845        1.1      yamt 
    846        1.1      yamt 	return error;
    847        1.1      yamt }
    848        1.1      yamt 
    849       1.21      yamt /*
    850       1.21      yamt  * XXX an incomplete hack; can't filter ioctls handled ifioctl().
    851       1.21      yamt  *
    852       1.21      yamt  * the intention here is to prevent operations on underlying interfaces
    853       1.21      yamt  * so that their states are not changed in the way that agr(4) doesn't
    854       1.21      yamt  * expect.  cf. the BUGS section in the agr(4) manual page.
    855       1.21      yamt  */
    856        1.1      yamt static int
    857       1.11  christos agr_ioctl_filter(struct ifnet *ifp, u_long cmd, void *arg)
    858        1.1      yamt {
    859        1.1      yamt 	struct agr_port *port = ifp->if_agrprivate;
    860        1.1      yamt 	int error;
    861        1.1      yamt 
    862        1.1      yamt 	KASSERT(port);
    863        1.1      yamt 
    864        1.1      yamt 	switch (cmd) {
    865        1.1      yamt 	case SIOCGIFADDR:
    866        1.1      yamt 	case SIOCGIFMEDIA:
    867        1.1      yamt 	case SIOCSIFFLAGS: /* XXX */
    868        1.1      yamt 		error = agrport_ioctl(port, cmd, arg);
    869        1.1      yamt 		break;
    870        1.1      yamt 	default:
    871        1.1      yamt 		error = EBUSY;
    872        1.1      yamt 		break;
    873        1.1      yamt 	}
    874        1.1      yamt 	return error;
    875        1.1      yamt }
    876        1.1      yamt 
    877        1.1      yamt static int
    878        1.1      yamt agrreq_copyin(const void *ubuf, struct agrreq *ar)
    879        1.1      yamt {
    880        1.1      yamt 	int error;
    881        1.1      yamt 
    882        1.1      yamt 	error = copyin(ubuf, ar, sizeof(*ar));
    883        1.1      yamt 	if (error) {
    884        1.1      yamt 		return error;
    885        1.1      yamt 	}
    886        1.1      yamt 
    887        1.1      yamt 	if (ar->ar_version != AGRREQ_VERSION) {
    888        1.1      yamt 		return EINVAL;
    889        1.1      yamt 	}
    890        1.1      yamt 
    891        1.1      yamt 	return 0;
    892        1.1      yamt }
    893        1.1      yamt 
    894        1.1      yamt static int
    895        1.1      yamt agrreq_copyout(void *ubuf, struct agrreq *ar)
    896        1.1      yamt {
    897        1.1      yamt 	int error;
    898        1.1      yamt 
    899        1.1      yamt 	KASSERT(ar->ar_version == AGRREQ_VERSION);
    900        1.1      yamt 
    901        1.1      yamt 	error = copyout(ar, ubuf, sizeof(*ar));
    902        1.1      yamt 	if (error) {
    903        1.1      yamt 		return error;
    904        1.1      yamt 	}
    905        1.1      yamt 
    906        1.1      yamt 	return 0;
    907        1.1      yamt }
    908        1.1      yamt 
    909        1.1      yamt static int
    910       1.11  christos agr_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    911        1.1      yamt {
    912        1.1      yamt 	struct agr_softc *sc = ifp->if_softc;
    913        1.1      yamt 	struct ifreq *ifr = (struct ifreq *)data;
    914        1.1      yamt 	struct ifaddr *ifa = (struct ifaddr *)data;
    915  1.21.12.1      matt 	struct ifcapreq *ifcr;
    916        1.1      yamt 	struct sockaddr *sa;
    917        1.1      yamt 	struct agrreq ar;
    918        1.1      yamt 	int error = 0;
    919        1.1      yamt 	int s;
    920        1.1      yamt 
    921        1.1      yamt 	agr_ioctl_lock(sc);
    922        1.1      yamt 
    923        1.1      yamt 	s = splnet();
    924        1.1      yamt 
    925        1.1      yamt 	switch (cmd) {
    926        1.1      yamt 	case SIOCSIFADDR:
    927        1.1      yamt 		if (sc->sc_nports == 0) {
    928        1.1      yamt 			error = EINVAL;
    929        1.1      yamt 			break;
    930        1.1      yamt 		}
    931        1.1      yamt 		ifp->if_flags |= IFF_UP;
    932        1.1      yamt 		switch (ifa->ifa_addr->sa_family) {
    933        1.1      yamt #if defined(INET)
    934        1.1      yamt 		case AF_INET:
    935        1.1      yamt 			arp_ifinit(ifp, ifa);
    936        1.1      yamt 			break;
    937        1.1      yamt #endif
    938        1.1      yamt 		default:
    939        1.1      yamt 			break;
    940        1.1      yamt 		}
    941        1.1      yamt 		break;
    942        1.1      yamt 
    943        1.1      yamt 	case SIOCGIFADDR:
    944        1.1      yamt 		sa = (struct sockaddr *)&ifr->ifr_data;
    945       1.14    dyoung 		memcpy(sa->sa_data, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
    946        1.1      yamt 		break;
    947        1.1      yamt 
    948        1.1      yamt #if 0 /* notyet */
    949        1.1      yamt 	case SIOCSIFMTU:
    950        1.1      yamt #endif
    951        1.1      yamt 
    952        1.1      yamt 	case SIOCSIFFLAGS:
    953        1.1      yamt 		agr_config_promisc(sc);
    954  1.21.12.1      matt 
    955  1.21.12.1      matt 		/* Check for a change in vlan status.  This ioctl is the
    956  1.21.12.1      matt 		 * only way we can tell that a vlan has attached or detached.
    957  1.21.12.1      matt 		 * Note the agr interface must be up.
    958  1.21.12.1      matt 		 */
    959  1.21.12.1      matt 		agr_vlan_check(ifp, sc);
    960  1.21.12.1      matt 		break;
    961  1.21.12.1      matt 
    962  1.21.12.1      matt 	case SIOCSIFCAP:
    963  1.21.12.1      matt 		ifcr = data;
    964  1.21.12.1      matt 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    965  1.21.12.1      matt 			error = 0;
    966        1.1      yamt 		break;
    967        1.1      yamt 
    968        1.1      yamt 	case SIOCSETAGR:
    969        1.1      yamt 		splx(s);
    970       1.18      elad 		error = kauth_authorize_network(kauth_cred_get(),
    971        1.7      elad 		    KAUTH_NETWORK_INTERFACE,
    972        1.7      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    973        1.7      elad 		    NULL);
    974        1.1      yamt 		if (!error) {
    975        1.1      yamt 			error = agrreq_copyin(ifr->ifr_data, &ar);
    976        1.1      yamt 		}
    977        1.1      yamt 		if (!error) {
    978        1.1      yamt 			error = agr_setconfig(ifp, &ar);
    979        1.1      yamt 		}
    980        1.1      yamt 		s = splnet();
    981        1.1      yamt 		break;
    982        1.1      yamt 
    983        1.1      yamt 	case SIOCGETAGR:
    984        1.1      yamt 		splx(s);
    985        1.1      yamt 		error = agrreq_copyin(ifr->ifr_data, &ar);
    986        1.1      yamt 		if (!error) {
    987        1.1      yamt 			error = agr_getconfig(ifp, &ar);
    988        1.1      yamt 		}
    989        1.1      yamt 		if (!error) {
    990        1.1      yamt 			error = agrreq_copyout(ifr->ifr_data, &ar);
    991        1.1      yamt 		}
    992        1.1      yamt 		s = splnet();
    993        1.1      yamt 		break;
    994        1.1      yamt 
    995        1.1      yamt 	case SIOCADDMULTI:
    996        1.1      yamt 	case SIOCDELMULTI:
    997        1.1      yamt 		if (sc->sc_nports == 0) {
    998        1.1      yamt 			error = EINVAL;
    999        1.1      yamt 			break;
   1000        1.1      yamt 		}
   1001        1.1      yamt 		error = agr_ioctl_multi(ifp, cmd, ifr);
   1002        1.1      yamt 		break;
   1003        1.1      yamt 
   1004        1.1      yamt 	default:
   1005        1.1      yamt 		error = EINVAL;
   1006        1.1      yamt 		break;
   1007        1.1      yamt 	}
   1008        1.1      yamt 
   1009        1.1      yamt 	splx(s);
   1010        1.1      yamt 
   1011        1.1      yamt 	agr_ioctl_unlock(sc);
   1012        1.1      yamt 
   1013        1.1      yamt 	return error;
   1014        1.1      yamt }
   1015        1.1      yamt 
   1016        1.1      yamt static int
   1017        1.1      yamt agr_config_promisc(struct agr_softc *sc)
   1018        1.1      yamt {
   1019        1.1      yamt 	int error;
   1020        1.1      yamt 
   1021        1.1      yamt 	agr_port_foreach(sc, agrport_config_promisc_callback, &error);
   1022        1.1      yamt 
   1023        1.1      yamt 	return error;
   1024        1.1      yamt }
   1025        1.1      yamt 
   1026        1.1      yamt static int
   1027        1.1      yamt agrport_config_promisc_callback(struct agr_port *port, void *arg)
   1028        1.1      yamt {
   1029        1.1      yamt 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
   1030        1.1      yamt 	int *errorp = arg;
   1031        1.1      yamt 	int error;
   1032        1.9   thorpej 	bool promisc;
   1033        1.1      yamt 
   1034        1.1      yamt 	promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
   1035        1.1      yamt 
   1036        1.1      yamt 	error = agrport_config_promisc(port, promisc);
   1037        1.1      yamt 	if (error) {
   1038        1.1      yamt 		*errorp = error;
   1039        1.1      yamt 	}
   1040        1.1      yamt 
   1041        1.1      yamt 	return 0;
   1042        1.1      yamt }
   1043        1.1      yamt 
   1044        1.1      yamt static int
   1045        1.9   thorpej agrport_config_promisc(struct agr_port *port, bool promisc)
   1046        1.1      yamt {
   1047        1.1      yamt 	int error;
   1048        1.1      yamt 
   1049        1.1      yamt 	if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
   1050        1.1      yamt 	    (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
   1051        1.1      yamt 		return 0;
   1052        1.1      yamt 	}
   1053        1.1      yamt 
   1054        1.1      yamt 	error = ifpromisc(port->port_ifp, promisc);
   1055        1.1      yamt 	if (error == 0) {
   1056        1.1      yamt 		if (promisc) {
   1057        1.1      yamt 			port->port_flags |= AGRPORT_PROMISC;
   1058        1.1      yamt 		} else {
   1059        1.1      yamt 			port->port_flags &= ~AGRPORT_PROMISC;
   1060        1.1      yamt 		}
   1061        1.1      yamt 	}
   1062        1.1      yamt 
   1063        1.1      yamt 	return error;
   1064        1.1      yamt }
   1065