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