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