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