Home | History | Annotate | Line # | Download | only in agr
if_agrsubr.c revision 1.1
      1 /*	$NetBSD: if_agrsubr.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_agrsubr.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/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 ifreq *);
     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 ifreq *);
     61 static int agr_mc_del(struct agr_multiaddrs *, const struct ifreq *);
     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 
     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 ifreq *ifr)
     96 {
     97 	struct agr_mc_entry *ame;
     98 	const struct sockaddr *sa;
     99 	sa = &ifr->ifr_addr;
    100 
    101 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    102 		if (!memcmp(&ame->ame_ifr.ifr_ss, sa, sa->sa_len))
    103 			return ame;
    104 	}
    105 
    106 	return NULL;
    107 }
    108 
    109 int
    110 agr_mc_foreach(struct agr_multiaddrs *ama,
    111     int (*func)(struct agr_mc_entry *, void *), void *arg)
    112 {
    113 	struct agr_mc_entry *ame;
    114 	int error = 0;
    115 
    116 	TAILQ_FOREACH(ame, &ama->ama_addrs, ame_q) {
    117 		error = (*func)(ame, arg);
    118 		if (error) {
    119 			/*
    120 			 * XXX how to recover?
    121 			 * we can try to restore setting, but it can also fail..
    122 			 */
    123 			break;
    124 		}
    125 	}
    126 
    127 	return error;
    128 }
    129 
    130 static int
    131 agr_mc_add(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    132 {
    133 	struct agr_mc_entry *ame;
    134 	const struct sockaddr *sa;
    135 
    136 	ame = agr_mc_lookup(ama, ifr);
    137 	if (ame) {
    138 		ame->ame_refcnt++;
    139 		return 0;
    140 	}
    141 
    142 	ame = malloc(sizeof(*ame), M_DEVBUF, M_NOWAIT | M_ZERO);
    143 	if (ame == NULL)
    144 		return ENOMEM;
    145 
    146 	sa = &ifr->ifr_addr;
    147 	memcpy(&ame->ame_ifr.ifr_ss, sa, sa->sa_len);
    148 	ame->ame_refcnt = 1;
    149 
    150 	return ENETRESET;
    151 }
    152 
    153 static int
    154 agr_mc_del(struct agr_multiaddrs *ama, const struct ifreq *ifr)
    155 {
    156 	struct agr_mc_entry *ame;
    157 
    158 	ame = agr_mc_lookup(ama, ifr);
    159 	if (ame == NULL)
    160 		return ENOENT;
    161 
    162 	ame->ame_refcnt--;
    163 	if (ame->ame_refcnt > 0)
    164 		return 0;
    165 
    166 	free(ame, M_DEVBUF);
    167 
    168 	return ENETRESET;
    169 }
    170 
    171 /* ==================== */
    172 
    173 int
    174 agr_port_foreach(struct agr_softc *sc,
    175     int (*func)(struct agr_port *, void *), void *arg)
    176 {
    177 	struct agr_port *port;
    178 	int error = 0;
    179 
    180 	TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
    181 		if ((port->port_flags & (AGRPORT_LARVAL | AGRPORT_DETACHING))) {
    182 			continue;
    183 		}
    184 		error = (func)(port, arg);
    185 		if (error) {
    186 			/*
    187 			 * XXX how to recover?
    188 			 * we can try to restore setting, but it can also fail..
    189 			 */
    190 			break;
    191 		}
    192 	}
    193 
    194 	return error;
    195 }
    196 
    197 /* ==================== */
    198 
    199 static int
    200 agrmc_mc_add_callback(struct agr_mc_entry *ame, void *arg)
    201 {
    202 
    203 	return agrport_mc_add_callback(arg, &ame->ame_ifr);
    204 }
    205 
    206 static int
    207 agrmc_mc_del_callback(struct agr_mc_entry *ame, void *arg)
    208 {
    209 
    210 	return agrport_mc_del_callback(arg, &ame->ame_ifr);
    211 }
    212 
    213 int
    214 agr_configmulti_port(struct agr_multiaddrs *ama, struct agr_port *port,
    215     boolean_t add)
    216 {
    217 
    218 	return agr_mc_foreach(ama,
    219 	    add ? agrmc_mc_add_callback : agrmc_mc_del_callback, port);
    220 }
    221 
    222 /* -------------------- */
    223 
    224 static int
    225 agrport_mc_add_callback(struct agr_port *port, void *arg)
    226 {
    227 
    228 	return agrport_ioctl(port, SIOCADDMULTI, arg);
    229 }
    230 
    231 static int
    232 agrport_mc_del_callback(struct agr_port *port, void *arg)
    233 {
    234 
    235 	return agrport_ioctl(port, SIOCADDMULTI, arg);
    236 }
    237 
    238 int
    239 agr_configmulti_ifreq(struct agr_softc *sc, struct agr_multiaddrs *ama,
    240     struct ifreq *ifr, boolean_t add)
    241 {
    242 	int error;
    243 
    244 	if (add)
    245 		error = agr_mc_add(ama, ifr);
    246 	else
    247 		error = agr_mc_del(ama, ifr);
    248 
    249 	if (error != ENETRESET)
    250 		return error;
    251 
    252 	return agr_port_foreach(sc,
    253 	    add ? agrport_mc_add_callback : agrport_mc_del_callback, ifr);
    254 }
    255 
    256 /* ==================== */
    257 
    258 int
    259 agr_port_getmedia(struct agr_port *port, u_int *media, u_int *status)
    260 {
    261 	struct ifmediareq ifmr;
    262 	int error;
    263 
    264 	memset(&ifmr, 0, sizeof(ifmr));
    265 	ifmr.ifm_count = 0;
    266 	error = agrport_ioctl(port, SIOCGIFMEDIA, (caddr_t)&ifmr);
    267 
    268 	if (error == 0) {
    269 		*media = ifmr.ifm_active;
    270 		*status = ifmr.ifm_status;
    271 	}
    272 
    273 	return error;
    274 }
    275