Home | History | Annotate | Line # | Download | only in agr
if_agrsubr.c revision 1.5.2.1
      1 /*	$NetBSD: if_agrsubr.c,v 1.5.2.1 2007/08/20 21:27:56 ad 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_agrsubr.c,v 1.5.2.1 2007/08/20 21:27:56 ad 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/systm.h>
     39 #include <sys/types.h>
     40 #include <sys/queue.h>
     41 #include <sys/sockio.h>
     42 
     43 #include <net/if.h>
     44 
     45 #include <net/agr/if_agrvar_impl.h>
     46 #include <net/agr/if_agrsubr.h>
     47 
     48 struct agr_mc_entry {
     49 	TAILQ_ENTRY(agr_mc_entry) ame_q;
     50 	int ame_refcnt;
     51 	struct agr_ifreq ame_ifr; /* XXX waste */
     52 };
     53 
     54 static struct agr_mc_entry *agr_mc_lookup(struct agr_multiaddrs *,
     55     const struct ifreq *);
     56 static int agrport_mc_add_callback(struct agr_port *, void *);
     57 static int agrport_mc_del_callback(struct agr_port *, void *);
     58 static int agrmc_mc_add_callback(struct agr_mc_entry *, void *);
     59 static int agrmc_mc_del_callback(struct agr_mc_entry *, void *);
     60 
     61 static int agr_mc_add(struct agr_multiaddrs *, const struct ifreq *);
     62 static int agr_mc_del(struct agr_multiaddrs *, const struct ifreq *);
     63 
     64 int
     65 agr_mc_purgeall(struct agr_softc *sc, struct agr_multiaddrs *ama)
     66 {
     67 	struct agr_mc_entry *ame;
     68 	int error = 0;
     69 
     70 	while ((ame = TAILQ_FIRST(&ama->ama_addrs)) != NULL) {
     71 		error = agr_port_foreach(sc,
     72 		    agrport_mc_del_callback, &ame->ame_ifr);
     73 		if (error) {
     74 			/* XXX XXX */
     75 			printf("%s: error %d\n", __func__, error);
     76 		}
     77 		TAILQ_REMOVE(&ama->ama_addrs, ame, ame_q);
     78 		free(ame, M_DEVBUF);
     79 	}
     80 
     81 	return error;
     82 }
     83 
     84 int
     85 agr_mc_init(struct agr_softc *sc, struct agr_multiaddrs *ama)
     86 {
     87 
     88 	TAILQ_INIT(&ama->ama_addrs);
     89 
     90 	return 0;
     91 }
     92 
     93 /* ==================== */
     94 
     95 static struct agr_mc_entry *
     96 agr_mc_lookup(struct agr_multiaddrs *ama, const struct ifreq *ifr)
     97 {
     98 	struct agr_mc_entry *ame;
     99 	const struct sockaddr *sa;
    100 	sa = &ifr->ifr_addr;
    101 
    102 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    103 		if (!memcmp(&ame->ame_ifr.ifr_ss, sa, sa->sa_len))
    104 			return ame;
    105 	}
    106 
    107 	return NULL;
    108 }
    109 
    110 int
    111 agr_mc_foreach(struct agr_multiaddrs *ama,
    112     int (*func)(struct agr_mc_entry *, void *), void *arg)
    113 {
    114 	struct agr_mc_entry *ame;
    115 	int error = 0;
    116 
    117 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    118 		error = (*func)(ame, arg);
    119 		if (error) {
    120 			/*
    121 			 * XXX how to recover?
    122 			 * we can try to restore setting, but it can also fail..
    123 			 */
    124 			break;
    125 		}
    126 	}
    127 
    128 	return error;
    129 }
    130 
    131 static int
    132 agr_mc_add(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    133 {
    134 	struct agr_mc_entry *ame;
    135 	const struct sockaddr *sa;
    136 
    137 	ame = agr_mc_lookup(ama, ifr);
    138 	if (ame) {
    139 		ame->ame_refcnt++;
    140 		return 0;
    141 	}
    142 
    143 	ame = malloc(sizeof(*ame), M_DEVBUF, M_NOWAIT | M_ZERO);
    144 	if (ame == NULL)
    145 		return ENOMEM;
    146 
    147 	sa = &ifr->ifr_addr;
    148 	memcpy(&ame->ame_ifr.ifr_ss, sa, sa->sa_len);
    149 	ame->ame_refcnt = 1;
    150 	TAILQ_INSERT_TAIL(&ama->ama_addrs, ame, ame_q);
    151 
    152 	return ENETRESET;
    153 }
    154 
    155 static int
    156 agr_mc_del(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    157 {
    158 	struct agr_mc_entry *ame;
    159 
    160 	ame = agr_mc_lookup(ama, ifr);
    161 	if (ame == NULL)
    162 		return ENOENT;
    163 
    164 	ame->ame_refcnt--;
    165 	if (ame->ame_refcnt > 0)
    166 		return 0;
    167 
    168 	TAILQ_REMOVE(&ama->ama_addrs, ame, ame_q);
    169 	free(ame, M_DEVBUF);
    170 
    171 	return ENETRESET;
    172 }
    173 
    174 /* ==================== */
    175 
    176 int
    177 agr_port_foreach(struct agr_softc *sc,
    178     int (*func)(struct agr_port *, void *), void *arg)
    179 {
    180 	struct agr_port *port;
    181 	int error = 0;
    182 
    183 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    184 		if ((port->port_flags & (AGRPORT_LARVAL | AGRPORT_DETACHING))) {
    185 			continue;
    186 		}
    187 		error = (func)(port, arg);
    188 		if (error) {
    189 			/*
    190 			 * XXX how to recover?
    191 			 * we can try to restore setting, but it can also fail..
    192 			 */
    193 			break;
    194 		}
    195 	}
    196 
    197 	return error;
    198 }
    199 
    200 /* ==================== */
    201 
    202 static int
    203 agrmc_mc_add_callback(struct agr_mc_entry *ame, void *arg)
    204 {
    205 
    206 	return agrport_mc_add_callback(arg, &ame->ame_ifr);
    207 }
    208 
    209 static int
    210 agrmc_mc_del_callback(struct agr_mc_entry *ame, void *arg)
    211 {
    212 
    213 	return agrport_mc_del_callback(arg, &ame->ame_ifr);
    214 }
    215 
    216 int
    217 agr_configmulti_port(struct agr_multiaddrs *ama, struct agr_port *port,
    218     bool add)
    219 {
    220 
    221 	return agr_mc_foreach(ama,
    222 	    add ? agrmc_mc_add_callback : agrmc_mc_del_callback, port);
    223 }
    224 
    225 /* -------------------- */
    226 
    227 static int
    228 agrport_mc_add_callback(struct agr_port *port, void *arg)
    229 {
    230 
    231 	return agrport_ioctl(port, SIOCADDMULTI, arg);
    232 }
    233 
    234 static int
    235 agrport_mc_del_callback(struct agr_port *port, void *arg)
    236 {
    237 
    238 	return agrport_ioctl(port, SIOCDELMULTI, arg);
    239 }
    240 
    241 int
    242 agr_configmulti_ifreq(struct agr_softc *sc, struct agr_multiaddrs *ama,
    243     struct ifreq *ifr, bool add)
    244 {
    245 	int error;
    246 
    247 	if (add)
    248 		error = agr_mc_add(ama, ifr);
    249 	else
    250 		error = agr_mc_del(ama, ifr);
    251 
    252 	if (error != ENETRESET)
    253 		return error;
    254 
    255 	return agr_port_foreach(sc,
    256 	    add ? agrport_mc_add_callback : agrport_mc_del_callback, ifr);
    257 }
    258 
    259 /* ==================== */
    260 
    261 int
    262 agr_port_getmedia(struct agr_port *port, u_int *media, u_int *status)
    263 {
    264 	struct ifmediareq ifmr;
    265 	int error;
    266 
    267 	memset(&ifmr, 0, sizeof(ifmr));
    268 	ifmr.ifm_count = 0;
    269 	error = agrport_ioctl(port, SIOCGIFMEDIA, (void *)&ifmr);
    270 
    271 	if (error == 0) {
    272 		*media = ifmr.ifm_active;
    273 		*status = ifmr.ifm_status;
    274 	}
    275 
    276 	return error;
    277 }
    278