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