Home | History | Annotate | Line # | Download | only in agr
if_agr.c revision 1.37.2.2
      1 /*	$NetBSD: if_agr.c,v 1.37.2.2 2017/01/07 08:56:50 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2005 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.37.2.2 2017/01/07 08:56:50 pgoyette Exp $");
     31 
     32 #ifdef _KERNEL_OPT
     33 #include "opt_inet.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/callout.h>
     38 #include <sys/malloc.h>
     39 #include <sys/mbuf.h>
     40 #include <sys/systm.h>
     41 #include <sys/types.h>
     42 #include <sys/queue.h>
     43 #include <sys/sockio.h>
     44 #include <sys/proc.h>	/* XXX for curproc */
     45 #include <sys/kauth.h>
     46 #include <sys/xcall.h>
     47 
     48 #include <net/bpf.h>
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_types.h>
     52 #include <net/if_ether.h>
     53 
     54 #if defined(INET)
     55 #include <netinet/in.h>
     56 #include <netinet/if_inarp.h>
     57 #endif
     58 
     59 #include <net/agr/if_agrvar.h>
     60 #include <net/agr/if_agrvar_impl.h>
     61 #include <net/agr/if_agrioctl.h>
     62 #include <net/agr/if_agrsubr.h>
     63 #include <net/agr/if_agrethervar.h>
     64 
     65 #include "ioconf.h"
     66 
     67 static int agr_clone_create(struct if_clone *, int);
     68 static int agr_clone_destroy(struct ifnet *);
     69 static void agr_start(struct ifnet *);
     70 static int agr_setconfig(struct agr_softc *, const struct agrreq *);
     71 static int agr_getconfig(struct agr_softc *, struct agrreq *);
     72 static int agr_getportlist(struct agr_softc *, struct agrreq *);
     73 static int agr_addport(struct ifnet *, struct ifnet *);
     74 static int agr_remport(struct ifnet *, struct ifnet *);
     75 static int agrreq_copyin(const void *, struct agrreq *);
     76 static int agrreq_copyout(void *, struct agrreq *);
     77 static int agr_ioctl(struct ifnet *, u_long, void *);
     78 static struct agr_port *agr_select_tx_port(struct agr_softc *, struct mbuf *);
     79 static int agr_ioctl_filter(struct ifnet *, u_long, void *);
     80 static void agr_reset_iftype(struct ifnet *);
     81 static int agr_config_promisc(struct agr_softc *);
     82 static int agrport_config_promisc_callback(struct agr_port *, void *);
     83 static int agrport_config_promisc(struct agr_port *, bool);
     84 static int agrport_cleanup(struct agr_softc *, struct agr_port *);
     85 
     86 static int agr_enter(struct agr_softc *);
     87 static void agr_exit(struct agr_softc *);
     88 static int agr_pause(struct agr_softc *);
     89 static void agr_evacuate(struct agr_softc *);
     90 static void agr_sync(void);
     91 static void agr_ports_lock(struct agr_softc *);
     92 static void agr_ports_unlock(struct agr_softc *);
     93 static bool agr_ports_enter(struct agr_softc *);
     94 static void agr_ports_exit(struct agr_softc *);
     95 
     96 static struct if_clone agr_cloner =
     97     IF_CLONE_INITIALIZER("agr", agr_clone_create, agr_clone_destroy);
     98 
     99 /*
    100  * EXPORTED FUNCTIONS
    101  */
    102 
    103 /*
    104  * agrattch: device attach routine.
    105  */
    106 
    107 void
    108 agrattach(int count)
    109 {
    110 
    111 	if_clone_attach(&agr_cloner);
    112 }
    113 
    114 /*
    115  * agr_input: frame collector.
    116  */
    117 
    118 void
    119 agr_input(struct ifnet *ifp_port, struct mbuf *m)
    120 {
    121 	struct agr_port *port;
    122 	struct ifnet *ifp;
    123 #if NVLAN > 0
    124 	struct m_tag *mtag;
    125 #endif
    126 
    127 	port = ifp_port->if_agrprivate;
    128 	KASSERT(port);
    129 	ifp = port->port_agrifp;
    130 	if ((port->port_flags & AGRPORT_COLLECTING) == 0) {
    131 		m_freem(m);
    132 		ifp->if_ierrors++;
    133 		return;
    134 	}
    135 
    136 	m_set_rcvif(m, ifp);
    137 
    138 #define DNH_DEBUG
    139 #if NVLAN > 0
    140 	/* got a vlan packet? */
    141 	if ((mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL)) != NULL) {
    142 #ifdef DNH_DEBUG
    143 		printf("%s: vlan tag %d attached\n",
    144 			ifp->if_xname,
    145 			htole16((*(u_int *)(mtag + 1)) & 0xffff));
    146 		printf("%s: vlan input\n", ifp->if_xname);
    147 #endif
    148 		vlan_input(ifp, m);
    149 		return;
    150 #ifdef DNH_DEBUG
    151 	} else {
    152 		struct ethercom *ec = (void *)ifp;
    153 		printf("%s: no vlan tag attached, ec_nvlans=%d\n",
    154 			ifp->if_xname, ec->ec_nvlans);
    155 #endif
    156 	}
    157 #endif
    158 
    159 	if_percpuq_enqueue(ifp->if_percpuq, m);
    160 }
    161 
    162 /*
    163  * EXPORTED AGR-INTERNAL FUNCTIONS
    164  */
    165 
    166 void
    167 agr_lock(struct agr_softc *sc)
    168 {
    169 
    170 	mutex_enter(&sc->sc_lock);
    171 }
    172 
    173 void
    174 agr_unlock(struct agr_softc *sc)
    175 {
    176 
    177 	mutex_exit(&sc->sc_lock);
    178 }
    179 
    180 /*
    181  * agr_xmit_frame: transmit a pre-built frame.
    182  */
    183 
    184 int
    185 agr_xmit_frame(struct ifnet *ifp_port, struct mbuf *m)
    186 {
    187 	int error;
    188 
    189 	struct sockaddr_storage dst0;
    190 	struct sockaddr *dst;
    191 	int hdrlen;
    192 
    193 	/*
    194 	 * trim off link level header and let if_output re-add it.
    195 	 * XXX better to introduce an API to transmit pre-built frames.
    196 	 */
    197 
    198 	hdrlen = ifp_port->if_hdrlen;
    199 	if (m->m_pkthdr.len < hdrlen) {
    200 		m_freem(m);
    201 		return EINVAL;
    202 	}
    203 	memset(&dst0, 0, sizeof(dst0));
    204 	dst = (struct sockaddr *)&dst0;
    205 	dst->sa_family = pseudo_AF_HDRCMPLT;
    206 	dst->sa_len = hdrlen;
    207 	m_copydata(m, 0, hdrlen, &dst->sa_data);
    208 	m_adj(m, hdrlen);
    209 
    210 	error = if_output_lock(ifp_port, ifp_port, m, dst, NULL);
    211 
    212 	return error;
    213 }
    214 
    215 int
    216 agrport_ioctl(struct agr_port *port, u_long cmd, void *arg)
    217 {
    218 	struct ifnet *ifp = port->port_ifp;
    219 
    220 	KASSERT(ifp->if_agrprivate == (void *)port);
    221 	KASSERT(ifp->if_ioctl == agr_ioctl_filter);
    222 
    223 	return (*port->port_ioctl)(ifp, cmd, arg);
    224 }
    225 
    226 /*
    227  * INTERNAL FUNCTIONS
    228  */
    229 
    230 /*
    231  * Enable vlan hardware assist for the specified port.
    232  */
    233 static int
    234 agr_vlan_add(struct agr_port *port, void *arg)
    235 {
    236 	struct ifnet *ifp = port->port_ifp;
    237 	struct ethercom *ec_port = (void *)ifp;
    238 	int error=0;
    239 
    240 	if (ec_port->ec_nvlans++ == 0 &&
    241 	    (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    242 		struct ifnet *p = port->port_ifp;
    243 		/*
    244 		 * Enable Tx/Rx of VLAN-sized frames.
    245 		 */
    246 		ec_port->ec_capenable |= ETHERCAP_VLAN_MTU;
    247 		if (p->if_flags & IFF_UP) {
    248 			error = if_flags_set(p, p->if_flags);
    249 			if (error) {
    250 				if (ec_port->ec_nvlans-- == 1)
    251 					ec_port->ec_capenable &=
    252 					    ~ETHERCAP_VLAN_MTU;
    253 				return (error);
    254 			}
    255 		}
    256 	}
    257 
    258 	return error;
    259 }
    260 
    261 /*
    262  * Disable vlan hardware assist for the specified port.
    263  */
    264 static int
    265 agr_vlan_del(struct agr_port *port, void *arg)
    266 {
    267 	struct ethercom *ec_port = (void *)port->port_ifp;
    268 
    269 	/* Disable vlan support */
    270 	if (ec_port->ec_nvlans-- == 1) {
    271 		/*
    272 		 * Disable Tx/Rx of VLAN-sized frames.
    273 		 */
    274 		ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    275 		if (port->port_ifp->if_flags & IFF_UP) {
    276 			(void)if_flags_set(port->port_ifp,
    277 			    port->port_ifp->if_flags);
    278 		}
    279 	}
    280 
    281 	return 0;
    282 }
    283 
    284 
    285 /*
    286  * Check for vlan attach/detach.
    287  * ec->ec_nvlans is directly modified by the vlan driver.
    288  * We keep a local count in sc (sc->sc_nvlans) to detect
    289  * when the vlan driver attaches or detaches.
    290  * Note the agr interface must be up for this to work.
    291  */
    292 static void
    293 agr_vlan_check(struct ifnet *ifp, struct agr_softc *sc)
    294 {
    295 	struct ethercom *ec = (void *)ifp;
    296 
    297 	/* vlans in sync? */
    298 	if (sc->sc_nvlans == ec->ec_nvlans) {
    299 		return;
    300 	}
    301 
    302 	if (sc->sc_nvlans == 0) {
    303 		/* vlan added */
    304 		agr_port_foreach(sc, agr_vlan_add, NULL);
    305 		sc->sc_nvlans = ec->ec_nvlans;
    306 	} else if (ec->ec_nvlans == 0) {
    307 		/* vlan removed */
    308 		agr_port_foreach(sc, agr_vlan_del, NULL);
    309 		sc->sc_nvlans = 0;
    310 	}
    311 }
    312 
    313 static int
    314 agr_clone_create(struct if_clone *ifc, int unit)
    315 {
    316 	struct agr_softc *sc;
    317 	struct ifnet *ifp;
    318 
    319 	sc = agr_alloc_softc();
    320 	TAILQ_INIT(&sc->sc_ports);
    321 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
    322 	mutex_init(&sc->sc_entry_mtx, MUTEX_DEFAULT, IPL_NONE);
    323 	cv_init(&sc->sc_insc_cv, "agrsoftc");
    324 	cv_init(&sc->sc_ports_cv, "agrports");
    325 	agrtimer_init(sc);
    326 	ifp = &sc->sc_if;
    327 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
    328 	    ifc->ifc_name, unit);
    329 
    330 	ifp->if_softc = sc;
    331 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    332 	ifp->if_start = agr_start;
    333 	ifp->if_ioctl = agr_ioctl;
    334 	IFQ_SET_READY(&ifp->if_snd);
    335 
    336 	if_attach(ifp);
    337 
    338 	agr_reset_iftype(ifp);
    339 
    340 	return 0;
    341 }
    342 
    343 static void
    344 agr_reset_iftype(struct ifnet *ifp)
    345 {
    346 
    347 	ifp->if_type = IFT_OTHER;
    348 	ifp->if_dlt = DLT_NULL;
    349 	ifp->if_addrlen = 0;
    350 	if_alloc_sadl(ifp);
    351 }
    352 
    353 static int
    354 agr_clone_destroy(struct ifnet *ifp)
    355 {
    356 	struct agr_softc *sc = ifp->if_softc;
    357 	int error;
    358 
    359 	if ((error = agr_pause(sc)) != 0)
    360 		return error;
    361 
    362 	if_detach(ifp);
    363 	agrtimer_destroy(sc);
    364 	/* Now that the ifnet has been detached, and our
    365 	 * component ifnets are disconnected, there can be
    366 	 * no new threads in the softc.  Wait for every
    367 	 * thread to get out of the softc.
    368 	 */
    369 	agr_evacuate(sc);
    370 	mutex_destroy(&sc->sc_lock);
    371 	mutex_destroy(&sc->sc_entry_mtx);
    372 	cv_destroy(&sc->sc_insc_cv);
    373 	cv_destroy(&sc->sc_ports_cv);
    374 	agr_free_softc(sc);
    375 
    376 	return 0;
    377 }
    378 
    379 static struct agr_port *
    380 agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    381 {
    382 
    383 	return (*sc->sc_iftop->iftop_select_tx_port)(sc, m);
    384 }
    385 
    386 #if 0 /* "generic" version */
    387 static struct agr_port *
    388 agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    389 {
    390 	struct agr_port *port;
    391 	uint32_t hash;
    392 
    393 	hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
    394 	if (sc->sc_nports == 0)
    395 		return NULL;
    396 	hash %= sc->sc_nports;
    397 	port = TAILQ_FIRST(&sc->sc_ports);
    398 	KASSERT(port != NULL);
    399 	while (hash--) {
    400 		port = TAILQ_NEXT(port, port_q);
    401 		KASSERT(port != NULL);
    402 	}
    403 
    404 	return port;
    405 }
    406 #endif /* 0 */
    407 
    408 static void
    409 agr_start(struct ifnet *ifp)
    410 {
    411 	struct agr_softc *sc = ifp->if_softc;
    412 	struct mbuf *m;
    413 
    414 	AGR_LOCK(sc);
    415 
    416 	while (/* CONSTCOND */ 1) {
    417 		struct agr_port *port;
    418 
    419 		IFQ_DEQUEUE(&ifp->if_snd, m);
    420 		if (m == NULL) {
    421 			break;
    422 		}
    423 		bpf_mtap(ifp, m);
    424 		port = agr_select_tx_port(sc, m);
    425 		if (port) {
    426 			int error;
    427 
    428 			error = agr_xmit_frame(port->port_ifp, m);
    429 			if (error) {
    430 				ifp->if_oerrors++;
    431 			} else {
    432 				ifp->if_opackets++;
    433 			}
    434 		} else {
    435 			m_freem(m);
    436 			ifp->if_oerrors++;
    437 		}
    438 	}
    439 
    440 	AGR_UNLOCK(sc);
    441 
    442 	ifp->if_flags &= ~IFF_OACTIVE;
    443 }
    444 
    445 static int
    446 agr_setconfig(struct agr_softc *sc, const struct agrreq *ar)
    447 {
    448 	struct ifnet *ifp = &sc->sc_if;
    449 	int cmd = ar->ar_cmd;
    450 	struct ifnet *ifp_port;
    451 	int error = 0;
    452 	char ifname[IFNAMSIZ];
    453 
    454 	memset(ifname, 0, sizeof(ifname));
    455 	error = copyin(ar->ar_buf, ifname,
    456 	    MIN(ar->ar_buflen, sizeof(ifname) - 1));
    457 	if (error) {
    458 		return error;
    459 	}
    460 	ifp_port = ifunit(ifname);
    461 	if (ifp_port == NULL) {
    462 		return ENOENT;
    463 	}
    464 
    465 	agr_ports_lock(sc);
    466 	switch (cmd) {
    467 	case AGRCMD_ADDPORT:
    468 		error = agr_addport(ifp, ifp_port);
    469 		break;
    470 
    471 	case AGRCMD_REMPORT:
    472 		error = agr_remport(ifp, ifp_port);
    473 		break;
    474 
    475 	default:
    476 		error = EINVAL;
    477 		break;
    478 	}
    479 	agr_ports_unlock(sc);
    480 
    481 	return error;
    482 }
    483 
    484 static int
    485 agr_getportlist(struct agr_softc *sc, struct agrreq *ar)
    486 {
    487 	struct agr_port *port;
    488 	struct agrportlist apl;
    489 	struct agrportinfo api;
    490 	char *cp = ar->ar_buf;
    491 	size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
    492 	int error;
    493 
    494 	if (cp != NULL) {
    495 		memset(&apl, 0, sizeof(apl));
    496 		memset(&api, 0, sizeof(api));
    497 
    498 		if (bufleft < sizeof(apl)) {
    499 			return E2BIG;
    500 		}
    501 		apl.apl_nports = sc->sc_nports;
    502 		error = copyout(&apl, cp, sizeof(apl));
    503 		if (error) {
    504 			return error;
    505 		}
    506 		cp += sizeof(apl);
    507 	}
    508 	bufleft -= sizeof(apl);
    509 
    510 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    511 		if (cp != NULL) {
    512 			if (bufleft < sizeof(api)) {
    513 				return E2BIG;
    514 			}
    515 			memcpy(api.api_ifname, port->port_ifp->if_xname,
    516 			    sizeof(api.api_ifname));
    517 			api.api_flags = 0;
    518 			if (port->port_flags & AGRPORT_COLLECTING) {
    519 				api.api_flags |= AGRPORTINFO_COLLECTING;
    520 			}
    521 			if (port->port_flags & AGRPORT_DISTRIBUTING) {
    522 				api.api_flags |= AGRPORTINFO_DISTRIBUTING;
    523 			}
    524 			error = copyout(&api, cp, sizeof(api));
    525 			if (error) {
    526 				return error;
    527 			}
    528 			cp += sizeof(api);
    529 		}
    530 		bufleft -= sizeof(api);
    531 	}
    532 
    533 	if (cp == NULL) {
    534 		ar->ar_buflen = -bufleft; /* necessary buffer size */
    535 	}
    536 
    537 	return 0;
    538 }
    539 
    540 static int
    541 agr_getconfig(struct agr_softc *sc, struct agrreq *ar)
    542 {
    543 	int cmd = ar->ar_cmd;
    544 	int error;
    545 
    546 	(void)agr_ports_enter(sc);
    547 	switch (cmd) {
    548 	case AGRCMD_PORTLIST:
    549 		error = agr_getportlist(sc, ar);
    550 		break;
    551 
    552 	default:
    553 		error = EINVAL;
    554 		break;
    555 	}
    556 	agr_ports_exit(sc);
    557 
    558 	return error;
    559 }
    560 
    561 static int
    562 agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
    563 {
    564 	const struct ifaddr *ifa;
    565 	struct agr_softc *sc = ifp->if_softc;
    566 	struct agr_port *port = NULL;
    567 	int error = 0;
    568 	int s;
    569 
    570 	if (ifp_port->if_ioctl == NULL) {
    571 		error = EOPNOTSUPP;
    572 		goto out;
    573 	}
    574 
    575 	if (ifp_port->if_agrprivate) {
    576 		error = EBUSY;
    577 		goto out;
    578 	}
    579 
    580 	if (ifp_port->if_start == agr_start) {
    581 		error = EINVAL;
    582 		goto out;
    583 	}
    584 
    585 	port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
    586 	    M_WAITOK | M_ZERO);
    587 	if (port == NULL) {
    588 		error = ENOMEM;
    589 		goto out;
    590 	}
    591 	port->port_flags = AGRPORT_LARVAL;
    592 
    593 	s = pserialize_read_enter();
    594 	IFADDR_READER_FOREACH(ifa, ifp_port) {
    595 		if (ifa->ifa_addr->sa_family != AF_LINK) {
    596 			pserialize_read_exit(s);
    597 			error = EBUSY;
    598 			goto out;
    599 		}
    600 	}
    601 	pserialize_read_exit(s);
    602 
    603 	if (sc->sc_nports == 0) {
    604 		switch (ifp_port->if_type) {
    605 		case IFT_ETHER:
    606 			sc->sc_iftop = &agrether_ops;
    607 			break;
    608 
    609 		default:
    610 			error = EPROTONOSUPPORT; /* XXX */
    611 			goto out;
    612 		}
    613 
    614 		error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
    615 		if (error)
    616 			goto out;
    617 		agrtimer_start(sc);
    618 	} else {
    619 		if (ifp->if_type != ifp_port->if_type) {
    620 			error = EINVAL;
    621 			goto out;
    622 		}
    623 		if (ifp->if_addrlen != ifp_port->if_addrlen) {
    624 			error = EINVAL;
    625 			goto out;
    626 		}
    627 	}
    628 
    629 	memcpy(port->port_origlladdr, CLLADDR(ifp_port->if_sadl),
    630 	    ifp_port->if_addrlen);
    631 
    632 	/*
    633 	 * start to modify ifp_port.
    634 	 */
    635 
    636 	/*
    637 	 * XXX this should probably be SIOCALIFADDR but that doesn't
    638 	 * appear to work (ENOTTY). We want to change the mac address
    639 	 * of each port to that of the first port. No need for arps
    640 	 * since there are no inet addresses assigned to the ports.
    641 	 */
    642 	error = if_addr_init(ifp_port, ifp->if_dl, true);
    643 
    644 	if (error) {
    645 		printf("%s: if_addr_init error %d\n", __func__, error);
    646 		goto cleanup;
    647 	}
    648 	port->port_flags |= AGRPORT_LADDRCHANGED;
    649 
    650 	ifp->if_type = ifp_port->if_type;
    651 	AGR_LOCK(sc);
    652 
    653 	port->port_ifp = ifp_port;
    654 	ifp_port->if_agrprivate = port;
    655 	port->port_agrifp = ifp;
    656 	TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
    657 	sc->sc_nports++;
    658 
    659 	port->port_ioctl = ifp_port->if_ioctl;
    660 	ifp_port->if_ioctl = agr_ioctl_filter;
    661 
    662 	port->port_flags |= AGRPORT_ATTACHED;
    663 
    664 	AGR_UNLOCK(sc);
    665 
    666 	error = (*sc->sc_iftop->iftop_portinit)(sc, port);
    667 	if (error) {
    668 		printf("%s: portinit error %d\n", __func__, error);
    669 		goto cleanup;
    670 	}
    671 
    672 	ifp->if_flags |= IFF_RUNNING;
    673 
    674 	agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
    675 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, true);
    676 	if (error) {
    677 		printf("%s: configmulti error %d\n", __func__, error);
    678 		goto cleanup;
    679 	}
    680 
    681 	AGR_LOCK(sc);
    682 	port->port_flags &= ~AGRPORT_LARVAL;
    683 	AGR_UNLOCK(sc);
    684 out:
    685 	if (error && port) {
    686 		free(port, M_DEVBUF);
    687 	}
    688 	return error;
    689 
    690 cleanup:
    691 	if (agrport_cleanup(sc, port)) {
    692 		printf("%s: error on cleanup\n", __func__);
    693 
    694 		port = NULL; /* XXX */
    695 	}
    696 
    697 	if (sc->sc_nports == 0) {
    698 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    699 		agrtimer_stop(sc);
    700 		(*sc->sc_iftop->iftop_dtor)(sc);
    701 		sc->sc_iftop = NULL;
    702 		agr_reset_iftype(ifp);
    703 	} else {
    704 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    705 	}
    706 
    707 	goto out;
    708 }
    709 
    710 static int
    711 agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
    712 {
    713 	struct agr_softc *sc = ifp->if_softc;
    714 	struct agr_port *port;
    715 	int error = 0;
    716 
    717 	if (ifp_port->if_agrprivate == NULL) {
    718 		error = ENOENT;
    719 		return error;
    720 	}
    721 
    722 	port = ifp_port->if_agrprivate;
    723 	if (port->port_agrifp != ifp) {
    724 		error = EINVAL;
    725 		return error;
    726 	}
    727 
    728 	KASSERT(sc->sc_nports > 0);
    729 
    730 	AGR_LOCK(sc);
    731 	port->port_flags |= AGRPORT_DETACHING;
    732 	AGR_UNLOCK(sc);
    733 
    734 	error = (*sc->sc_iftop->iftop_portfini)(sc, port);
    735 	if (error) {
    736 		/* XXX XXX */
    737 		printf("%s: portfini error %d\n", __func__, error);
    738 		goto out;
    739 	}
    740 
    741 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, false);
    742 	if (error) {
    743 		/* XXX XXX */
    744 		printf("%s: configmulti_port error %d\n", __func__, error);
    745 		goto out;
    746 	}
    747 
    748 	error = agrport_cleanup(sc, port);
    749 	if (error) {
    750 		/* XXX XXX */
    751 		printf("%s: agrport_cleanup error %d\n", __func__, error);
    752 		goto out;
    753 	}
    754 
    755 	free(port, M_DEVBUF);
    756 
    757 out:
    758 	if (sc->sc_nports == 0) {
    759 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    760 		agrtimer_stop(sc);
    761 		(*sc->sc_iftop->iftop_dtor)(sc);
    762 		sc->sc_iftop = NULL;
    763 		/* XXX should purge all addresses? */
    764 		agr_reset_iftype(ifp);
    765 	} else {
    766 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    767 	}
    768 
    769 	return error;
    770 }
    771 
    772 static int
    773 agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
    774 {
    775 	struct ifnet *ifp_port = port->port_ifp;
    776 	int error;
    777 	int result = 0;
    778 
    779 	error = agrport_config_promisc(port, false);
    780 	if (error) {
    781 		printf("%s: config_promisc error %d\n", __func__, error);
    782 		result = error;
    783 	}
    784 
    785 	if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
    786 #if 0
    787 		memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
    788 		    ifp_port->if_addrlen);
    789 		if (ifp_port->if_init != NULL) {
    790 			error = (*ifp_port->if_init)(ifp_port);
    791 		}
    792 #else
    793 		union {
    794 			struct sockaddr sa;
    795 			struct sockaddr_dl sdl;
    796 			struct sockaddr_storage ss;
    797 		} u;
    798 		struct ifaddr ifa;
    799 
    800 		sockaddr_dl_init(&u.sdl, sizeof(u.ss),
    801 		    0, ifp_port->if_type, NULL, 0,
    802 		    port->port_origlladdr, ifp_port->if_addrlen);
    803 		memset(&ifa, 0, sizeof(ifa));
    804 		ifa.ifa_addr = &u.sa;
    805 		error = agrport_ioctl(port, SIOCINITIFADDR, &ifa);
    806 #endif
    807 		if (error) {
    808 			printf("%s: if_init error %d\n", __func__, error);
    809 			result = error;
    810 		} else {
    811 			port->port_flags &= ~AGRPORT_LADDRCHANGED;
    812 		}
    813 	}
    814 
    815 	AGR_LOCK(sc);
    816 	if ((port->port_flags & AGRPORT_ATTACHED)) {
    817 		ifp_port->if_agrprivate = NULL;
    818 
    819 		TAILQ_REMOVE(&sc->sc_ports, port, port_q);
    820 		sc->sc_nports--;
    821 
    822 		KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
    823 		ifp_port->if_ioctl = port->port_ioctl;
    824 
    825 		port->port_flags &= ~AGRPORT_ATTACHED;
    826 	}
    827 	AGR_UNLOCK(sc);
    828 
    829 	return result;
    830 }
    831 
    832 static int
    833 agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
    834 {
    835 	struct agr_softc *sc = ifp->if_softc;
    836 	int error;
    837 
    838 	error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
    839 	    (cmd == SIOCADDMULTI));
    840 
    841 	return error;
    842 }
    843 
    844 /*
    845  * XXX an incomplete hack; can't filter ioctls handled ifioctl().
    846  *
    847  * the intention here is to prevent operations on underlying interfaces
    848  * so that their states are not changed in the way that agr(4) doesn't
    849  * expect.  cf. the BUGS section in the agr(4) manual page.
    850  */
    851 static int
    852 agr_ioctl_filter(struct ifnet *ifp, u_long cmd, void *arg)
    853 {
    854 	struct agr_port *port = ifp->if_agrprivate;
    855 	int error;
    856 
    857 	KASSERT(port);
    858 
    859 	switch (cmd) {
    860 	case SIOCADDMULTI: /* add m'cast addr */
    861 	case SIOCAIFADDR: /* add/chg IF alias */
    862 	case SIOCALIFADDR: /* add IF addr */
    863 	case SIOCDELMULTI: /* del m'cast addr */
    864 	case SIOCDIFADDR: /* delete IF addr */
    865 	case SIOCDIFPHYADDR: /* delete gif addrs */
    866 	case SIOCDLIFADDR: /* delete IF addr */
    867 	case SIOCINITIFADDR:
    868 	case SIOCSDRVSPEC: /* set driver-specific parameters */
    869 	case SIOCSIFADDR: /* set ifnet address */
    870 	case SIOCSIFBRDADDR: /* set broadcast addr */
    871 	case SIOCSIFDSTADDR: /* set p-p address */
    872 	case SIOCSIFGENERIC: /* generic IF set op */
    873 	case SIOCSIFMEDIA: /* set net media */
    874 	case SIOCSIFMETRIC: /* set IF metric */
    875 	case SIOCSIFMTU: /* set ifnet mtu */
    876 	case SIOCSIFNETMASK: /* set net addr mask */
    877 	case SIOCSIFPHYADDR: /* set gif addres */
    878 	case SIOCSLIFPHYADDR: /* set gif addrs */
    879 	case SIOCSVH: /* set carp param */
    880 		error = EBUSY;
    881 		break;
    882 	case SIOCSIFCAP: /* XXX */
    883 	case SIOCSIFFLAGS: /* XXX */
    884 	default:
    885 		error = agrport_ioctl(port, cmd, arg);
    886 		break;
    887 	}
    888 	return error;
    889 }
    890 
    891 static int
    892 agrreq_copyin(const void *ubuf, struct agrreq *ar)
    893 {
    894 	int error;
    895 
    896 	error = copyin(ubuf, ar, sizeof(*ar));
    897 	if (error) {
    898 		return error;
    899 	}
    900 
    901 	if (ar->ar_version != AGRREQ_VERSION) {
    902 		return EINVAL;
    903 	}
    904 
    905 	return 0;
    906 }
    907 
    908 static int
    909 agrreq_copyout(void *ubuf, struct agrreq *ar)
    910 {
    911 	int error;
    912 
    913 	KASSERT(ar->ar_version == AGRREQ_VERSION);
    914 
    915 	error = copyout(ar, ubuf, sizeof(*ar));
    916 	if (error) {
    917 		return error;
    918 	}
    919 
    920 	return 0;
    921 }
    922 
    923 /* Make sure that if any interrupt handlers are out of the softc. */
    924 static void
    925 agr_sync(void)
    926 {
    927 	uint64_t h;
    928 
    929 	if (!mp_online)
    930 		return;
    931 
    932 	h = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    933 	xc_wait(h);
    934 }
    935 
    936 static int
    937 agr_pause(struct agr_softc *sc)
    938 {
    939 	int error;
    940 
    941 	mutex_enter(&sc->sc_entry_mtx);
    942 	if ((error = sc->sc_noentry) != 0)
    943 		goto out;
    944 
    945 	sc->sc_noentry = EBUSY;
    946 
    947 	while (sc->sc_insc != 0)
    948 		cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
    949 
    950 	if (sc->sc_nports == 0) {
    951 		sc->sc_noentry = ENXIO;
    952 	} else {
    953 		sc->sc_noentry = 0;
    954 		error = EBUSY;
    955 	}
    956 	cv_broadcast(&sc->sc_insc_cv);
    957 out:
    958 	mutex_exit(&sc->sc_entry_mtx);
    959 	return error;
    960 }
    961 
    962 static void
    963 agr_evacuate(struct agr_softc *sc)
    964 {
    965 	mutex_enter(&sc->sc_entry_mtx);
    966 	cv_broadcast(&sc->sc_insc_cv);
    967 	while (sc->sc_insc != 0 || sc->sc_paused != 0)
    968 		cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
    969 	mutex_exit(&sc->sc_entry_mtx);
    970 
    971 	agr_sync();
    972 }
    973 
    974 static int
    975 agr_enter(struct agr_softc *sc)
    976 {
    977 	int error;
    978 
    979 	mutex_enter(&sc->sc_entry_mtx);
    980 	sc->sc_paused++;
    981 	while ((error = sc->sc_noentry) == EBUSY)
    982 		cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
    983 	sc->sc_paused--;
    984 	if (error == 0)
    985 		sc->sc_insc++;
    986 	mutex_exit(&sc->sc_entry_mtx);
    987 
    988 	return error;
    989 }
    990 
    991 static void
    992 agr_exit(struct agr_softc *sc)
    993 {
    994 	mutex_enter(&sc->sc_entry_mtx);
    995 	if (--sc->sc_insc == 0)
    996 		cv_signal(&sc->sc_insc_cv);
    997 	mutex_exit(&sc->sc_entry_mtx);
    998 }
    999 
   1000 static bool
   1001 agr_ports_enter(struct agr_softc *sc)
   1002 {
   1003 	mutex_enter(&sc->sc_entry_mtx);
   1004 	while (sc->sc_wrports)
   1005 		cv_wait(&sc->sc_ports_cv, &sc->sc_entry_mtx);
   1006 	sc->sc_rdports++;
   1007 	mutex_exit(&sc->sc_entry_mtx);
   1008 
   1009 	return true;
   1010 }
   1011 
   1012 static void
   1013 agr_ports_exit(struct agr_softc *sc)
   1014 {
   1015 	mutex_enter(&sc->sc_entry_mtx);
   1016 	if (--sc->sc_rdports == 0)
   1017 		cv_signal(&sc->sc_ports_cv);
   1018 	mutex_exit(&sc->sc_entry_mtx);
   1019 }
   1020 
   1021 static void
   1022 agr_ports_lock(struct agr_softc *sc)
   1023 {
   1024 	mutex_enter(&sc->sc_entry_mtx);
   1025 	while (sc->sc_rdports != 0)
   1026 		cv_wait(&sc->sc_ports_cv, &sc->sc_entry_mtx);
   1027 	sc->sc_wrports = true;
   1028 	mutex_exit(&sc->sc_entry_mtx);
   1029 }
   1030 
   1031 static void
   1032 agr_ports_unlock(struct agr_softc *sc)
   1033 {
   1034 	mutex_enter(&sc->sc_entry_mtx);
   1035 	sc->sc_wrports = false;
   1036 	cv_signal(&sc->sc_ports_cv);
   1037 	mutex_exit(&sc->sc_entry_mtx);
   1038 }
   1039 
   1040 static int
   1041 agr_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
   1042 {
   1043 	struct agr_softc *sc = ifp->if_softc;
   1044 	struct ifreq *ifr = (struct ifreq *)data;
   1045 	struct ifaddr *ifa = (struct ifaddr *)data;
   1046 	struct agrreq ar;
   1047 	int error;
   1048 	bool in_ports = false;
   1049 	int s;
   1050 
   1051 	if ((error = agr_enter(sc)) != 0)
   1052 		return error;
   1053 
   1054 	s = splnet();
   1055 
   1056 	switch (cmd) {
   1057 	case SIOCINITIFADDR:
   1058 		in_ports = agr_ports_enter(sc);
   1059 		if (sc->sc_nports == 0) {
   1060 			error = EINVAL;
   1061 			break;
   1062 		}
   1063 		ifp->if_flags |= IFF_UP;
   1064 		switch (ifa->ifa_addr->sa_family) {
   1065 #if defined(INET)
   1066 		case AF_INET:
   1067 			arp_ifinit(ifp, ifa);
   1068 			break;
   1069 #endif
   1070 		default:
   1071 			break;
   1072 		}
   1073 		break;
   1074 
   1075 #if 0 /* notyet */
   1076 	case SIOCSIFMTU:
   1077 #endif
   1078 
   1079 	case SIOCSIFFLAGS:
   1080 		/*
   1081 		 * Check for a change in vlan status.  This ioctl is the
   1082 		 * only way we can tell that a vlan has attached or detached.
   1083 		 * Note the agr interface must be up.
   1084 		 */
   1085 		agr_vlan_check(ifp, sc);
   1086 
   1087 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1088 			break;
   1089 		agr_config_promisc(sc);
   1090 		break;
   1091 
   1092 	case SIOCSETAGR:
   1093 		splx(s);
   1094 		error = kauth_authorize_network(kauth_cred_get(),
   1095 		    KAUTH_NETWORK_INTERFACE,
   1096 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1097 		    NULL);
   1098 		if (!error) {
   1099 			error = agrreq_copyin(ifr->ifr_data, &ar);
   1100 		}
   1101 		if (!error) {
   1102 			error = agr_setconfig(sc, &ar);
   1103 		}
   1104 		s = splnet();
   1105 		break;
   1106 
   1107 	case SIOCGETAGR:
   1108 		splx(s);
   1109 		error = agrreq_copyin(ifr->ifr_data, &ar);
   1110 		if (!error) {
   1111 			error = agr_getconfig(sc, &ar);
   1112 		}
   1113 		if (!error) {
   1114 			error = agrreq_copyout(ifr->ifr_data, &ar);
   1115 		}
   1116 		s = splnet();
   1117 		break;
   1118 
   1119 	case SIOCADDMULTI:
   1120 	case SIOCDELMULTI:
   1121 		in_ports = agr_ports_enter(sc);
   1122 		if (sc->sc_nports == 0)
   1123 			error = EINVAL;
   1124 		else
   1125 			error = agr_ioctl_multi(ifp, cmd, ifr);
   1126 		break;
   1127 
   1128 	default:
   1129 		error = ifioctl_common(ifp, cmd, data);
   1130 		break;
   1131 	}
   1132 
   1133 	if (in_ports)
   1134 		agr_ports_exit(sc);
   1135 
   1136 	splx(s);
   1137 
   1138 	agr_exit(sc);
   1139 
   1140 	return error;
   1141 }
   1142 
   1143 static int
   1144 agr_config_promisc(struct agr_softc *sc)
   1145 {
   1146 	int error;
   1147 
   1148 	agr_port_foreach(sc, agrport_config_promisc_callback, &error);
   1149 
   1150 	return error;
   1151 }
   1152 
   1153 static int
   1154 agrport_config_promisc_callback(struct agr_port *port, void *arg)
   1155 {
   1156 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
   1157 	int *errorp = arg;
   1158 	int error;
   1159 	bool promisc;
   1160 
   1161 	promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
   1162 
   1163 	error = agrport_config_promisc(port, promisc);
   1164 	if (error) {
   1165 		*errorp = error;
   1166 	}
   1167 
   1168 	return 0;
   1169 }
   1170 
   1171 static int
   1172 agrport_config_promisc(struct agr_port *port, bool promisc)
   1173 {
   1174 	int error;
   1175 
   1176 	if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
   1177 	    (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
   1178 		return 0;
   1179 	}
   1180 
   1181 	error = ifpromisc(port->port_ifp, promisc);
   1182 	if (error == 0) {
   1183 		if (promisc) {
   1184 			port->port_flags |= AGRPORT_PROMISC;
   1185 		} else {
   1186 			port->port_flags &= ~AGRPORT_PROMISC;
   1187 		}
   1188 	}
   1189 
   1190 	return error;
   1191 }
   1192