Home | History | Annotate | Line # | Download | only in agr
if_agr.c revision 1.14
      1 /*	$NetBSD: if_agr.c,v 1.14 2007/08/26 23:07:16 dyoung 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.14 2007/08/26 23:07:16 dyoung 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 	struct agr_softc *sc = ifp->if_softc;
    468 	struct agr_port *port = NULL;
    469 	int error = 0;
    470 
    471 	if (ifp_port->if_ioctl == NULL) {
    472 		error = EOPNOTSUPP;
    473 		goto out;
    474 	}
    475 
    476 	if (ifp_port->if_agrprivate) {
    477 		error = EBUSY;
    478 		goto out;
    479 	}
    480 
    481 	if (ifp_port->if_start == agr_start) {
    482 		error = EINVAL;
    483 		goto out;
    484 	}
    485 
    486 	port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
    487 	    M_WAITOK | M_ZERO);
    488 	if (port == NULL) {
    489 		error = ENOMEM;
    490 		goto out;
    491 	}
    492 	port->port_flags = AGRPORT_LARVAL;
    493 
    494 	if (TAILQ_NEXT(TAILQ_FIRST(&ifp_port->if_addrlist), ifa_list) != NULL) {
    495 		error = EBUSY;
    496 		goto out;
    497 	}
    498 
    499 	if (sc->sc_nports == 0) {
    500 		switch (ifp_port->if_type) {
    501 		case IFT_ETHER:
    502 			sc->sc_iftop = &agrether_ops;
    503 			break;
    504 
    505 		default:
    506 			error = EPROTONOSUPPORT; /* XXX */
    507 			goto out;
    508 		}
    509 
    510 		error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
    511 		if (error)
    512 			goto out;
    513 		agrtimer_start(sc);
    514 	} else {
    515 		if (ifp->if_type != ifp_port->if_type) {
    516 			error = EINVAL;
    517 			goto out;
    518 		}
    519 		if (ifp->if_addrlen != ifp_port->if_addrlen) {
    520 			error = EINVAL;
    521 			goto out;
    522 		}
    523 	}
    524 
    525 	memcpy(port->port_origlladdr, CLLADDR(ifp_port->if_sadl),
    526 	    ifp_port->if_addrlen);
    527 
    528 	/*
    529 	 * start to modify ifp_port.
    530 	 */
    531 
    532 	error = (*ifp_port->if_ioctl)(ifp_port, SIOCSIFADDR,
    533 	    (void *)TAILQ_FIRST(&ifp->if_addrlist));
    534 
    535 	if (error) {
    536 		printf("%s: SIOCSIFADDR error %d\n", __func__, error);
    537 		goto cleanup;
    538 	}
    539 	port->port_flags |= AGRPORT_LADDRCHANGED;
    540 
    541 	ifp->if_type = ifp_port->if_type;
    542 	AGR_LOCK(sc);
    543 
    544 	port->port_ifp = ifp_port;
    545 	ifp_port->if_agrprivate = port;
    546 	port->port_agrifp = ifp;
    547 	TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
    548 	sc->sc_nports++;
    549 
    550 	port->port_ioctl = ifp_port->if_ioctl;
    551 	ifp_port->if_ioctl = agr_ioctl_filter;
    552 
    553 	port->port_flags |= AGRPORT_ATTACHED;
    554 
    555 	AGR_UNLOCK(sc);
    556 
    557 	error = (*sc->sc_iftop->iftop_portinit)(sc, port);
    558 	if (error) {
    559 		printf("%s: portinit error %d\n", __func__, error);
    560 		goto cleanup;
    561 	}
    562 
    563 	ifp->if_flags |= IFF_RUNNING;
    564 
    565 	agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
    566 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, true);
    567 	if (error) {
    568 		printf("%s: configmulti error %d\n", __func__, error);
    569 		goto cleanup;
    570 	}
    571 
    572 	AGR_LOCK(sc);
    573 	port->port_flags &= ~AGRPORT_LARVAL;
    574 	AGR_UNLOCK(sc);
    575 out:
    576 	if (error && port) {
    577 		free(port, M_DEVBUF);
    578 	}
    579 	return error;
    580 
    581 cleanup:
    582 	if (agrport_cleanup(sc, port)) {
    583 		printf("%s: error on cleanup\n", __func__);
    584 
    585 		port = NULL; /* XXX */
    586 	}
    587 
    588 	if (sc->sc_nports == 0) {
    589 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    590 		agrtimer_stop(sc);
    591 		(*sc->sc_iftop->iftop_dtor)(sc);
    592 		sc->sc_iftop = NULL;
    593 		agr_reset_iftype(ifp);
    594 	} else {
    595 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    596 	}
    597 
    598 	goto out;
    599 }
    600 
    601 static int
    602 agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
    603 {
    604 	struct agr_softc *sc = ifp->if_softc;
    605 	struct agr_port *port;
    606 	int error = 0;
    607 
    608 	if (ifp_port->if_agrprivate == NULL) {
    609 		error = ENOENT;
    610 		return error;
    611 	}
    612 
    613 	port = ifp_port->if_agrprivate;
    614 	if (port->port_agrifp != ifp) {
    615 		error = EINVAL;
    616 		return error;
    617 	}
    618 
    619 	KASSERT(sc->sc_nports > 0);
    620 
    621 #if 0
    622 	if (sc->sc_nports == 1 &&
    623 	    TAILQ_NEXT(TAILQ_FIRST(&ifp->if_addrlist), ifa_list) != NULL) {
    624 		error = EBUSY;
    625 		return error;
    626 	}
    627 #endif
    628 
    629 	AGR_LOCK(sc);
    630 	port->port_flags |= AGRPORT_DETACHING;
    631 	AGR_UNLOCK(sc);
    632 
    633 	error = (*sc->sc_iftop->iftop_portfini)(sc, port);
    634 	if (error) {
    635 		/* XXX XXX */
    636 		printf("%s: portfini error %d\n", __func__, error);
    637 		goto out;
    638 	}
    639 
    640 	error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, false);
    641 	if (error) {
    642 		/* XXX XXX */
    643 		printf("%s: configmulti_port error %d\n", __func__, error);
    644 		goto out;
    645 	}
    646 
    647 	error = agrport_cleanup(sc, port);
    648 	if (error) {
    649 		/* XXX XXX */
    650 		printf("%s: agrport_cleanup error %d\n", __func__, error);
    651 		goto out;
    652 	}
    653 
    654 	free(port, M_DEVBUF);
    655 
    656 out:
    657 	if (sc->sc_nports == 0) {
    658 		KASSERT(TAILQ_EMPTY(&sc->sc_ports));
    659 		agrtimer_stop(sc);
    660 		(*sc->sc_iftop->iftop_dtor)(sc);
    661 		sc->sc_iftop = NULL;
    662 		/* XXX should purge all addresses? */
    663 		agr_reset_iftype(ifp);
    664 	} else {
    665 		KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
    666 	}
    667 
    668 	return error;
    669 }
    670 
    671 static int
    672 agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
    673 {
    674 	struct ifnet *ifp_port = port->port_ifp;
    675 	int error;
    676 	int result = 0;
    677 
    678 	error = agrport_config_promisc(port, false);
    679 	if (error) {
    680 		printf("%s: config_promisc error %d\n", __func__, error);
    681 		result = error;
    682 	}
    683 
    684 	if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
    685 #if 0
    686 		memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
    687 		    ifp_port->if_addrlen);
    688 		if (ifp_port->if_init != NULL) {
    689 			error = (*ifp_port->if_init)(ifp_port);
    690 		}
    691 #else
    692 		struct sockaddr_dl *sdl;
    693 		struct ifaddr ifa;
    694 		int sdllen;
    695 		int addrlen;
    696 
    697 		addrlen = ifp_port->if_addrlen;
    698 		sdllen = sockaddr_dl_measure(0, addrlen);
    699 		sdl = malloc(sdllen, M_TEMP, M_WAITOK);
    700 		if (sdl == NULL) {
    701 			error = ENOMEM;
    702 		} else {
    703 			sockaddr_dl_init(sdl, 0, ifp_port->if_type, NULL, 0,
    704 			    port->port_origlladdr, addrlen);
    705 			memset(&ifa, 0, sizeof(ifa));
    706 			ifa.ifa_addr = (struct sockaddr *)sdl;
    707 			error = agrport_ioctl(port, SIOCSIFADDR, &ifa);
    708 			free(sdl, M_TEMP);
    709 		}
    710 #endif
    711 		if (error) {
    712 			printf("%s: if_init error %d\n", __func__, error);
    713 			result = error;
    714 		} else {
    715 			port->port_flags &= ~AGRPORT_LADDRCHANGED;
    716 		}
    717 	}
    718 
    719 	AGR_LOCK(sc);
    720 	if ((port->port_flags & AGRPORT_ATTACHED)) {
    721 		ifp_port->if_agrprivate = NULL;
    722 
    723 		TAILQ_REMOVE(&sc->sc_ports, port, port_q);
    724 		sc->sc_nports--;
    725 
    726 		KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
    727 		ifp_port->if_ioctl = port->port_ioctl;
    728 
    729 		port->port_flags &= ~AGRPORT_ATTACHED;
    730 	}
    731 	AGR_UNLOCK(sc);
    732 
    733 	return result;
    734 }
    735 
    736 static int
    737 agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
    738 {
    739 	struct agr_softc *sc = ifp->if_softc;
    740 	int error;
    741 
    742 	error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
    743 	    (cmd == SIOCADDMULTI));
    744 
    745 	return error;
    746 }
    747 
    748 /* XXX an incomplete hack; can't filter ioctls handled ifioctl(). */
    749 static int
    750 agr_ioctl_filter(struct ifnet *ifp, u_long cmd, void *arg)
    751 {
    752 	struct agr_port *port = ifp->if_agrprivate;
    753 	int error;
    754 
    755 	KASSERT(port);
    756 
    757 	switch (cmd) {
    758 	case SIOCGIFADDR:
    759 	case SIOCGIFMEDIA:
    760 	case SIOCSIFFLAGS: /* XXX */
    761 		error = agrport_ioctl(port, cmd, arg);
    762 		break;
    763 	default:
    764 		error = EBUSY;
    765 		break;
    766 	}
    767 	return error;
    768 }
    769 
    770 static int
    771 agrreq_copyin(const void *ubuf, struct agrreq *ar)
    772 {
    773 	int error;
    774 
    775 	error = copyin(ubuf, ar, sizeof(*ar));
    776 	if (error) {
    777 		return error;
    778 	}
    779 
    780 	if (ar->ar_version != AGRREQ_VERSION) {
    781 		return EINVAL;
    782 	}
    783 
    784 	return 0;
    785 }
    786 
    787 static int
    788 agrreq_copyout(void *ubuf, struct agrreq *ar)
    789 {
    790 	int error;
    791 
    792 	KASSERT(ar->ar_version == AGRREQ_VERSION);
    793 
    794 	error = copyout(ar, ubuf, sizeof(*ar));
    795 	if (error) {
    796 		return error;
    797 	}
    798 
    799 	return 0;
    800 }
    801 
    802 static int
    803 agr_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    804 {
    805 	struct agr_softc *sc = ifp->if_softc;
    806 	struct ifreq *ifr = (struct ifreq *)data;
    807 	struct ifaddr *ifa = (struct ifaddr *)data;
    808 	struct sockaddr *sa;
    809 	struct agrreq ar;
    810 	struct proc *p;
    811 	int error = 0;
    812 	int s;
    813 
    814 	agr_ioctl_lock(sc);
    815 
    816 	s = splnet();
    817 
    818 	switch (cmd) {
    819 	case SIOCSIFADDR:
    820 		if (sc->sc_nports == 0) {
    821 			error = EINVAL;
    822 			break;
    823 		}
    824 		ifp->if_flags |= IFF_UP;
    825 		switch (ifa->ifa_addr->sa_family) {
    826 #if defined(INET)
    827 		case AF_INET:
    828 			arp_ifinit(ifp, ifa);
    829 			break;
    830 #endif
    831 		default:
    832 			break;
    833 		}
    834 		break;
    835 
    836 	case SIOCGIFADDR:
    837 		sa = (struct sockaddr *)&ifr->ifr_data;
    838 		memcpy(sa->sa_data, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
    839 		break;
    840 
    841 #if 0 /* notyet */
    842 	case SIOCSIFMTU:
    843 #endif
    844 
    845 	case SIOCSIFFLAGS:
    846 		agr_config_promisc(sc);
    847 		break;
    848 
    849 	case SIOCSETAGR:
    850 		splx(s);
    851 		p = curproc; /* XXX */
    852 		error = kauth_authorize_network(p->p_cred,
    853 		    KAUTH_NETWORK_INTERFACE,
    854 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    855 		    NULL);
    856 		if (!error) {
    857 			error = agrreq_copyin(ifr->ifr_data, &ar);
    858 		}
    859 		if (!error) {
    860 			error = agr_setconfig(ifp, &ar);
    861 		}
    862 		s = splnet();
    863 		break;
    864 
    865 	case SIOCGETAGR:
    866 		splx(s);
    867 		error = agrreq_copyin(ifr->ifr_data, &ar);
    868 		if (!error) {
    869 			error = agr_getconfig(ifp, &ar);
    870 		}
    871 		if (!error) {
    872 			error = agrreq_copyout(ifr->ifr_data, &ar);
    873 		}
    874 		s = splnet();
    875 		break;
    876 
    877 	case SIOCADDMULTI:
    878 	case SIOCDELMULTI:
    879 		if (sc->sc_nports == 0) {
    880 			error = EINVAL;
    881 			break;
    882 		}
    883 		error = agr_ioctl_multi(ifp, cmd, ifr);
    884 		break;
    885 
    886 	default:
    887 		error = EINVAL;
    888 		break;
    889 	}
    890 
    891 	splx(s);
    892 
    893 	agr_ioctl_unlock(sc);
    894 
    895 	return error;
    896 }
    897 
    898 static int
    899 agr_config_promisc(struct agr_softc *sc)
    900 {
    901 	int error;
    902 
    903 	agr_port_foreach(sc, agrport_config_promisc_callback, &error);
    904 
    905 	return error;
    906 }
    907 
    908 static int
    909 agrport_config_promisc_callback(struct agr_port *port, void *arg)
    910 {
    911 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
    912 	int *errorp = arg;
    913 	int error;
    914 	bool promisc;
    915 
    916 	promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
    917 
    918 	error = agrport_config_promisc(port, promisc);
    919 	if (error) {
    920 		*errorp = error;
    921 	}
    922 
    923 	return 0;
    924 }
    925 
    926 static int
    927 agrport_config_promisc(struct agr_port *port, bool promisc)
    928 {
    929 	int error;
    930 
    931 	if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
    932 	    (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
    933 		return 0;
    934 	}
    935 
    936 	error = ifpromisc(port->port_ifp, promisc);
    937 	if (error == 0) {
    938 		if (promisc) {
    939 			port->port_flags |= AGRPORT_PROMISC;
    940 		} else {
    941 			port->port_flags &= ~AGRPORT_PROMISC;
    942 		}
    943 	}
    944 
    945 	return error;
    946 }
    947