Home | History | Annotate | Line # | Download | only in agr
if_agrsubr.c revision 1.5.10.1
      1  1.5.10.1     skrll /*	$NetBSD: if_agrsubr.c,v 1.5.10.1 2007/08/15 13:49:42 skrll Exp $	*/
      2       1.1      yamt 
      3       1.1      yamt /*-
      4       1.1      yamt  * Copyright (c)2005 YAMAMOTO Takashi,
      5       1.1      yamt  * All rights reserved.
      6       1.1      yamt  *
      7       1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8       1.1      yamt  * modification, are permitted provided that the following conditions
      9       1.1      yamt  * are met:
     10       1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11       1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12       1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15       1.1      yamt  *
     16       1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1      yamt  * SUCH DAMAGE.
     27       1.1      yamt  */
     28       1.1      yamt 
     29       1.1      yamt #include <sys/cdefs.h>
     30  1.5.10.1     skrll __KERNEL_RCSID(0, "$NetBSD: if_agrsubr.c,v 1.5.10.1 2007/08/15 13:49:42 skrll Exp $");
     31       1.1      yamt 
     32       1.1      yamt #include "bpfilter.h"
     33       1.1      yamt #include "opt_inet.h"
     34       1.1      yamt 
     35       1.1      yamt #include <sys/param.h>
     36       1.2      yamt #include <sys/callout.h>
     37       1.1      yamt #include <sys/malloc.h>
     38       1.1      yamt #include <sys/systm.h>
     39       1.1      yamt #include <sys/types.h>
     40       1.1      yamt #include <sys/queue.h>
     41       1.1      yamt #include <sys/sockio.h>
     42       1.1      yamt 
     43       1.1      yamt #include <net/if.h>
     44       1.1      yamt 
     45       1.1      yamt #include <net/agr/if_agrvar_impl.h>
     46       1.1      yamt #include <net/agr/if_agrsubr.h>
     47       1.1      yamt 
     48       1.1      yamt struct agr_mc_entry {
     49       1.1      yamt 	TAILQ_ENTRY(agr_mc_entry) ame_q;
     50       1.1      yamt 	int ame_refcnt;
     51       1.1      yamt 	struct agr_ifreq ame_ifr; /* XXX waste */
     52       1.1      yamt };
     53       1.1      yamt 
     54       1.1      yamt static struct agr_mc_entry *agr_mc_lookup(struct agr_multiaddrs *,
     55       1.1      yamt     const struct ifreq *);
     56       1.1      yamt static int agrport_mc_add_callback(struct agr_port *, void *);
     57       1.1      yamt static int agrport_mc_del_callback(struct agr_port *, void *);
     58       1.1      yamt static int agrmc_mc_add_callback(struct agr_mc_entry *, void *);
     59       1.1      yamt static int agrmc_mc_del_callback(struct agr_mc_entry *, void *);
     60       1.1      yamt 
     61       1.1      yamt static int agr_mc_add(struct agr_multiaddrs *, const struct ifreq *);
     62       1.1      yamt static int agr_mc_del(struct agr_multiaddrs *, const struct ifreq *);
     63       1.1      yamt 
     64       1.1      yamt int
     65       1.1      yamt agr_mc_purgeall(struct agr_softc *sc, struct agr_multiaddrs *ama)
     66       1.1      yamt {
     67       1.1      yamt 	struct agr_mc_entry *ame;
     68       1.1      yamt 	int error = 0;
     69       1.1      yamt 
     70       1.1      yamt 	while ((ame = TAILQ_FIRST(&ama->ama_addrs)) != NULL) {
     71       1.1      yamt 		error = agr_port_foreach(sc,
     72       1.1      yamt 		    agrport_mc_del_callback, &ame->ame_ifr);
     73       1.1      yamt 		if (error) {
     74       1.1      yamt 			/* XXX XXX */
     75       1.1      yamt 			printf("%s: error %d\n", __func__, error);
     76       1.1      yamt 		}
     77  1.5.10.1     skrll 		TAILQ_REMOVE(&ama->ama_addrs, ame, ame_q);
     78       1.1      yamt 		free(ame, M_DEVBUF);
     79       1.1      yamt 	}
     80       1.1      yamt 
     81       1.1      yamt 	return error;
     82       1.1      yamt }
     83       1.1      yamt 
     84       1.1      yamt int
     85       1.1      yamt agr_mc_init(struct agr_softc *sc, struct agr_multiaddrs *ama)
     86       1.1      yamt {
     87       1.1      yamt 
     88       1.1      yamt 	TAILQ_INIT(&ama->ama_addrs);
     89       1.1      yamt 
     90       1.1      yamt 	return 0;
     91       1.1      yamt }
     92       1.1      yamt 
     93       1.1      yamt /* ==================== */
     94       1.1      yamt 
     95       1.1      yamt static struct agr_mc_entry *
     96       1.1      yamt agr_mc_lookup(struct agr_multiaddrs *ama, const struct ifreq *ifr)
     97       1.1      yamt {
     98       1.1      yamt 	struct agr_mc_entry *ame;
     99       1.1      yamt 	const struct sockaddr *sa;
    100       1.1      yamt 	sa = &ifr->ifr_addr;
    101       1.1      yamt 
    102       1.1      yamt 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    103       1.1      yamt 		if (!memcmp(&ame->ame_ifr.ifr_ss, sa, sa->sa_len))
    104       1.1      yamt 			return ame;
    105       1.1      yamt 	}
    106       1.1      yamt 
    107       1.1      yamt 	return NULL;
    108       1.1      yamt }
    109       1.1      yamt 
    110       1.1      yamt int
    111       1.1      yamt agr_mc_foreach(struct agr_multiaddrs *ama,
    112       1.1      yamt     int (*func)(struct agr_mc_entry *, void *), void *arg)
    113       1.1      yamt {
    114       1.1      yamt 	struct agr_mc_entry *ame;
    115       1.1      yamt 	int error = 0;
    116       1.1      yamt 
    117       1.1      yamt 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    118       1.1      yamt 		error = (*func)(ame, arg);
    119       1.1      yamt 		if (error) {
    120       1.1      yamt 			/*
    121       1.1      yamt 			 * XXX how to recover?
    122       1.1      yamt 			 * we can try to restore setting, but it can also fail..
    123       1.1      yamt 			 */
    124       1.1      yamt 			break;
    125       1.1      yamt 		}
    126       1.1      yamt 	}
    127       1.1      yamt 
    128       1.1      yamt 	return error;
    129       1.1      yamt }
    130       1.1      yamt 
    131       1.1      yamt static int
    132       1.1      yamt agr_mc_add(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    133       1.1      yamt {
    134       1.1      yamt 	struct agr_mc_entry *ame;
    135       1.1      yamt 	const struct sockaddr *sa;
    136       1.1      yamt 
    137       1.1      yamt 	ame = agr_mc_lookup(ama, ifr);
    138       1.1      yamt 	if (ame) {
    139       1.1      yamt 		ame->ame_refcnt++;
    140       1.1      yamt 		return 0;
    141       1.1      yamt 	}
    142       1.1      yamt 
    143       1.1      yamt 	ame = malloc(sizeof(*ame), M_DEVBUF, M_NOWAIT | M_ZERO);
    144       1.1      yamt 	if (ame == NULL)
    145       1.1      yamt 		return ENOMEM;
    146       1.1      yamt 
    147       1.1      yamt 	sa = &ifr->ifr_addr;
    148       1.1      yamt 	memcpy(&ame->ame_ifr.ifr_ss, sa, sa->sa_len);
    149       1.1      yamt 	ame->ame_refcnt = 1;
    150  1.5.10.1     skrll 	TAILQ_INSERT_TAIL(&ama->ama_addrs, ame, ame_q);
    151       1.1      yamt 
    152       1.1      yamt 	return ENETRESET;
    153       1.1      yamt }
    154       1.1      yamt 
    155       1.1      yamt static int
    156       1.1      yamt agr_mc_del(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    157       1.1      yamt {
    158       1.1      yamt 	struct agr_mc_entry *ame;
    159       1.1      yamt 
    160       1.1      yamt 	ame = agr_mc_lookup(ama, ifr);
    161       1.1      yamt 	if (ame == NULL)
    162       1.1      yamt 		return ENOENT;
    163       1.1      yamt 
    164       1.1      yamt 	ame->ame_refcnt--;
    165       1.1      yamt 	if (ame->ame_refcnt > 0)
    166       1.1      yamt 		return 0;
    167       1.1      yamt 
    168  1.5.10.1     skrll 	TAILQ_REMOVE(&ama->ama_addrs, ame, ame_q);
    169       1.1      yamt 	free(ame, M_DEVBUF);
    170       1.1      yamt 
    171       1.1      yamt 	return ENETRESET;
    172       1.1      yamt }
    173       1.1      yamt 
    174       1.1      yamt /* ==================== */
    175       1.1      yamt 
    176       1.1      yamt int
    177       1.1      yamt agr_port_foreach(struct agr_softc *sc,
    178       1.1      yamt     int (*func)(struct agr_port *, void *), void *arg)
    179       1.1      yamt {
    180       1.1      yamt 	struct agr_port *port;
    181       1.1      yamt 	int error = 0;
    182       1.1      yamt 
    183       1.1      yamt 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    184       1.1      yamt 		if ((port->port_flags & (AGRPORT_LARVAL | AGRPORT_DETACHING))) {
    185       1.1      yamt 			continue;
    186       1.1      yamt 		}
    187       1.1      yamt 		error = (func)(port, arg);
    188       1.1      yamt 		if (error) {
    189       1.1      yamt 			/*
    190       1.1      yamt 			 * XXX how to recover?
    191       1.1      yamt 			 * we can try to restore setting, but it can also fail..
    192       1.1      yamt 			 */
    193       1.1      yamt 			break;
    194       1.1      yamt 		}
    195       1.1      yamt 	}
    196       1.1      yamt 
    197       1.1      yamt 	return error;
    198       1.1      yamt }
    199       1.1      yamt 
    200       1.1      yamt /* ==================== */
    201       1.1      yamt 
    202       1.1      yamt static int
    203       1.1      yamt agrmc_mc_add_callback(struct agr_mc_entry *ame, void *arg)
    204       1.1      yamt {
    205       1.1      yamt 
    206       1.1      yamt 	return agrport_mc_add_callback(arg, &ame->ame_ifr);
    207       1.1      yamt }
    208       1.1      yamt 
    209       1.1      yamt static int
    210       1.1      yamt agrmc_mc_del_callback(struct agr_mc_entry *ame, void *arg)
    211       1.1      yamt {
    212       1.1      yamt 
    213       1.1      yamt 	return agrport_mc_del_callback(arg, &ame->ame_ifr);
    214       1.1      yamt }
    215       1.1      yamt 
    216       1.1      yamt int
    217       1.1      yamt agr_configmulti_port(struct agr_multiaddrs *ama, struct agr_port *port,
    218       1.4   thorpej     bool add)
    219       1.1      yamt {
    220       1.1      yamt 
    221       1.1      yamt 	return agr_mc_foreach(ama,
    222       1.1      yamt 	    add ? agrmc_mc_add_callback : agrmc_mc_del_callback, port);
    223       1.1      yamt }
    224       1.1      yamt 
    225       1.1      yamt /* -------------------- */
    226       1.1      yamt 
    227       1.1      yamt static int
    228       1.1      yamt agrport_mc_add_callback(struct agr_port *port, void *arg)
    229       1.1      yamt {
    230       1.1      yamt 
    231       1.1      yamt 	return agrport_ioctl(port, SIOCADDMULTI, arg);
    232       1.1      yamt }
    233       1.1      yamt 
    234       1.1      yamt static int
    235       1.1      yamt agrport_mc_del_callback(struct agr_port *port, void *arg)
    236       1.1      yamt {
    237       1.1      yamt 
    238  1.5.10.1     skrll 	return agrport_ioctl(port, SIOCDELMULTI, arg);
    239       1.1      yamt }
    240       1.1      yamt 
    241       1.1      yamt int
    242       1.1      yamt agr_configmulti_ifreq(struct agr_softc *sc, struct agr_multiaddrs *ama,
    243       1.4   thorpej     struct ifreq *ifr, bool add)
    244       1.1      yamt {
    245       1.1      yamt 	int error;
    246       1.1      yamt 
    247       1.1      yamt 	if (add)
    248       1.1      yamt 		error = agr_mc_add(ama, ifr);
    249       1.1      yamt 	else
    250       1.1      yamt 		error = agr_mc_del(ama, ifr);
    251       1.1      yamt 
    252       1.1      yamt 	if (error != ENETRESET)
    253       1.1      yamt 		return error;
    254       1.1      yamt 
    255       1.1      yamt 	return agr_port_foreach(sc,
    256       1.1      yamt 	    add ? agrport_mc_add_callback : agrport_mc_del_callback, ifr);
    257       1.1      yamt }
    258       1.1      yamt 
    259       1.1      yamt /* ==================== */
    260       1.1      yamt 
    261       1.1      yamt int
    262       1.1      yamt agr_port_getmedia(struct agr_port *port, u_int *media, u_int *status)
    263       1.1      yamt {
    264       1.1      yamt 	struct ifmediareq ifmr;
    265       1.1      yamt 	int error;
    266       1.1      yamt 
    267       1.1      yamt 	memset(&ifmr, 0, sizeof(ifmr));
    268       1.1      yamt 	ifmr.ifm_count = 0;
    269       1.5  christos 	error = agrport_ioctl(port, SIOCGIFMEDIA, (void *)&ifmr);
    270       1.1      yamt 
    271       1.1      yamt 	if (error == 0) {
    272       1.1      yamt 		*media = ifmr.ifm_active;
    273       1.1      yamt 		*status = ifmr.ifm_status;
    274       1.1      yamt 	}
    275       1.1      yamt 
    276       1.1      yamt 	return error;
    277       1.1      yamt }
    278