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