Home | History | Annotate | Line # | Download | only in agr
      1 /*	$NetBSD: if_agrsubr.c,v 1.13 2019/11/10 21:16:38 chs 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.13 2019/11/10 21:16:38 chs Exp $");
     31 
     32 #ifdef _KERNEL_OPT
     33 #include "opt_inet.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/callout.h>
     38 #include <sys/malloc.h>
     39 #include <sys/systm.h>
     40 #include <sys/types.h>
     41 #include <sys/queue.h>
     42 #include <sys/sockio.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 
     47 #include <net/agr/if_agrvar_impl.h>
     48 #include <net/agr/if_agrsubr.h>
     49 
     50 struct agr_mc_entry {
     51 	TAILQ_ENTRY(agr_mc_entry) ame_q;
     52 	int ame_refcnt;
     53 	struct agr_ifreq ame_ifr; /* XXX waste */
     54 };
     55 
     56 static struct agr_mc_entry *agr_mc_lookup(struct agr_multiaddrs *,
     57     const struct sockaddr *);
     58 static int agrport_mc_add_callback(struct agr_port *, void *);
     59 static int agrport_mc_del_callback(struct agr_port *, void *);
     60 static int agrmc_mc_add_callback(struct agr_mc_entry *, void *);
     61 static int agrmc_mc_del_callback(struct agr_mc_entry *, void *);
     62 
     63 static int agr_mc_add(struct agr_multiaddrs *, const struct sockaddr *);
     64 static int agr_mc_del(struct agr_multiaddrs *, const struct sockaddr *);
     65 
     66 int
     67 agr_mc_purgeall(struct agr_softc *sc, struct agr_multiaddrs *ama)
     68 {
     69 	struct agr_mc_entry *ame;
     70 	int error = 0;
     71 
     72 	while ((ame = TAILQ_FIRST(&ama->ama_addrs)) != NULL) {
     73 		error = agr_port_foreach(sc,
     74 		    agrport_mc_del_callback, &ame->ame_ifr);
     75 		if (error) {
     76 			/* XXX XXX */
     77 			printf("%s: error %d\n", __func__, error);
     78 		}
     79 		TAILQ_REMOVE(&ama->ama_addrs, ame, ame_q);
     80 		free(ame, M_DEVBUF);
     81 	}
     82 
     83 	return error;
     84 }
     85 
     86 int
     87 agr_mc_init(struct agr_softc *sc, struct agr_multiaddrs *ama)
     88 {
     89 
     90 	TAILQ_INIT(&ama->ama_addrs);
     91 
     92 	return 0;
     93 }
     94 
     95 /* ==================== */
     96 
     97 static struct agr_mc_entry *
     98 agr_mc_lookup(struct agr_multiaddrs *ama, const struct sockaddr *sa)
     99 {
    100 	struct agr_mc_entry *ame;
    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 sockaddr *sa)
    133 {
    134 	struct agr_mc_entry *ame;
    135 
    136 	ame = agr_mc_lookup(ama, sa);
    137 	if (ame) {
    138 		ame->ame_refcnt++;
    139 		return 0;
    140 	}
    141 
    142 	ame = malloc(sizeof(*ame), M_DEVBUF, M_WAITOK | M_ZERO);
    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 
    274 /* ==================== */
    275 
    276 /*
    277  * Enable vlan hardware assist for the specified port.
    278  */
    279 int
    280 agr_vlan_add(struct agr_port *port, void *arg)
    281 {
    282 	struct ifnet *ifp = port->port_ifp;
    283 	struct ethercom *ec_port = (void *)ifp;
    284 	int error=0;
    285 
    286 	if (ec_port->ec_nvlans++ == 0 &&
    287 	    (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    288 		struct ifnet *p = port->port_ifp;
    289 		/*
    290 		 * Enable Tx/Rx of VLAN-sized frames.
    291 		 */
    292 		ec_port->ec_capenable |= ETHERCAP_VLAN_MTU;
    293 		if (p->if_flags & IFF_UP) {
    294 			IFNET_LOCK(p);
    295 			error = if_flags_set(p, p->if_flags);
    296 			IFNET_UNLOCK(p);
    297 			if (error) {
    298 				if (ec_port->ec_nvlans-- == 1)
    299 					ec_port->ec_capenable &=
    300 					    ~ETHERCAP_VLAN_MTU;
    301 				return (error);
    302 			}
    303 		}
    304 	}
    305 
    306 	return error;
    307 }
    308 
    309 /*
    310  * Disable vlan hardware assist for the specified port.
    311  */
    312 int
    313 agr_vlan_del(struct agr_port *port, void *arg)
    314 {
    315 	struct ethercom *ec_port = (void *)port->port_ifp;
    316 	bool *force_zero = (bool *)arg;
    317 
    318 	KASSERT(force_zero != NULL);
    319 
    320 	/* Disable vlan support */
    321 	if ((*force_zero && ec_port->ec_nvlans > 0) ||
    322 	    ec_port->ec_nvlans-- == 1) {
    323 		struct ifnet *p = port->port_ifp;
    324 		if (*force_zero)
    325 			ec_port->ec_nvlans = 0;
    326 		/*
    327 		 * Disable Tx/Rx of VLAN-sized frames.
    328 		 */
    329 		ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    330 		if (p->if_flags & IFF_UP) {
    331 			IFNET_LOCK(p);
    332 			(void)if_flags_set(p, p->if_flags);
    333 			IFNET_UNLOCK(p);
    334 		}
    335 	}
    336 
    337 	return 0;
    338 }
    339