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