if_agr.c revision 1.1 1 1.1 yamt /* $NetBSD: if_agr.c,v 1.1 2005/03/18 11:11:50 yamt Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2005 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt #include <sys/cdefs.h>
30 1.1 yamt __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.1 2005/03/18 11:11:50 yamt Exp $");
31 1.1 yamt
32 1.1 yamt #include "bpfilter.h"
33 1.1 yamt #include "opt_inet.h"
34 1.1 yamt
35 1.1 yamt #include <sys/param.h>
36 1.1 yamt #include <sys/malloc.h>
37 1.1 yamt #include <sys/mbuf.h>
38 1.1 yamt #include <sys/systm.h>
39 1.1 yamt #include <sys/types.h>
40 1.1 yamt #include <sys/queue.h>
41 1.1 yamt #include <sys/sockio.h>
42 1.1 yamt #include <sys/proc.h> /* XXX for curproc */
43 1.1 yamt
44 1.1 yamt #if NBPFILTER > 0
45 1.1 yamt #include <net/bpf.h>
46 1.1 yamt #endif
47 1.1 yamt #include <net/if.h>
48 1.1 yamt #include <net/if_dl.h>
49 1.1 yamt #include <net/if_types.h>
50 1.1 yamt
51 1.1 yamt #if defined(INET)
52 1.1 yamt #include <netinet/in.h>
53 1.1 yamt #include <netinet/if_inarp.h>
54 1.1 yamt #endif
55 1.1 yamt
56 1.1 yamt #include <net/agr/if_agrvar.h>
57 1.1 yamt #include <net/agr/if_agrvar_impl.h>
58 1.1 yamt #include <net/agr/if_agrioctl.h>
59 1.1 yamt #include <net/agr/if_agrsubr.h>
60 1.1 yamt #include <net/agr/if_agrethervar.h>
61 1.1 yamt
62 1.1 yamt void agrattach(int);
63 1.1 yamt
64 1.1 yamt static int agr_clone_create(struct if_clone *, int);
65 1.1 yamt static int agr_clone_destroy(struct ifnet *);
66 1.1 yamt static void agr_start(struct ifnet *);
67 1.1 yamt static int agr_setconfig(struct ifnet *, const struct agrreq *);
68 1.1 yamt static int agr_getconfig(struct ifnet *, struct agrreq *);
69 1.1 yamt static int agr_getportlist(struct ifnet *, struct agrreq *);
70 1.1 yamt static int agr_addport(struct ifnet *, struct ifnet *);
71 1.1 yamt static int agr_remport(struct ifnet *, struct ifnet *);
72 1.1 yamt static int agrreq_copyin(const void *, struct agrreq *);
73 1.1 yamt static int agrreq_copyout(void *, struct agrreq *);
74 1.1 yamt static int agr_ioctl(struct ifnet *, u_long, caddr_t);
75 1.1 yamt static struct agr_port *agr_select_tx_port(struct agr_softc *, struct mbuf *);
76 1.1 yamt static int agr_ioctl_filter(struct ifnet *, u_long, caddr_t);
77 1.1 yamt static void agr_reset_iftype(struct ifnet *);
78 1.1 yamt static int agr_config_promisc(struct agr_softc *);
79 1.1 yamt static int agrport_config_promisc_callback(struct agr_port *, void *);
80 1.1 yamt static int agrport_config_promisc(struct agr_port *, boolean_t);
81 1.1 yamt static int agrport_cleanup(struct agr_softc *, struct agr_port *);
82 1.1 yamt
83 1.1 yamt static struct if_clone agr_cloner =
84 1.1 yamt IF_CLONE_INITIALIZER("agr", agr_clone_create, agr_clone_destroy);
85 1.1 yamt
86 1.1 yamt /*
87 1.1 yamt * EXPORTED FUNCTIONS
88 1.1 yamt */
89 1.1 yamt
90 1.1 yamt /*
91 1.1 yamt * agrattch: device attach routine.
92 1.1 yamt */
93 1.1 yamt
94 1.1 yamt void
95 1.1 yamt agrattach(int count)
96 1.1 yamt {
97 1.1 yamt
98 1.1 yamt if_clone_attach(&agr_cloner);
99 1.1 yamt }
100 1.1 yamt
101 1.1 yamt /*
102 1.1 yamt * agr_input: frame collector.
103 1.1 yamt */
104 1.1 yamt
105 1.1 yamt void
106 1.1 yamt agr_input(struct ifnet *ifp_port, struct mbuf *m)
107 1.1 yamt {
108 1.1 yamt struct agr_port *port;
109 1.1 yamt struct ifnet *ifp;
110 1.1 yamt
111 1.1 yamt port = ifp_port->if_agrprivate;
112 1.1 yamt KASSERT(port);
113 1.1 yamt ifp = port->port_agrifp;
114 1.1 yamt if ((port->port_flags & AGRPORT_COLLECTING) == 0) {
115 1.1 yamt m_freem(m);
116 1.1 yamt ifp->if_ierrors++;
117 1.1 yamt return;
118 1.1 yamt }
119 1.1 yamt
120 1.1 yamt ifp->if_ipackets++;
121 1.1 yamt m->m_pkthdr.rcvif = ifp;
122 1.1 yamt
123 1.1 yamt #if NBPFILTER > 0
124 1.1 yamt if (ifp->if_bpf) {
125 1.1 yamt bpf_mtap(ifp->if_bpf, m);
126 1.1 yamt }
127 1.1 yamt #endif
128 1.1 yamt
129 1.1 yamt (*ifp->if_input)(ifp, m);
130 1.1 yamt }
131 1.1 yamt
132 1.1 yamt /*
133 1.1 yamt * EXPORTED AGR-INTERNAL FUNCTIONS
134 1.1 yamt */
135 1.1 yamt
136 1.1 yamt int
137 1.1 yamt agr_lock(struct agr_softc *sc)
138 1.1 yamt {
139 1.1 yamt int s;
140 1.1 yamt
141 1.1 yamt s = splnet();
142 1.1 yamt simple_lock(&sc->sc_lock);
143 1.1 yamt
144 1.1 yamt return s;
145 1.1 yamt }
146 1.1 yamt
147 1.1 yamt void
148 1.1 yamt agr_unlock(struct agr_softc *sc, int savedipl)
149 1.1 yamt {
150 1.1 yamt
151 1.1 yamt simple_unlock(&sc->sc_lock);
152 1.1 yamt splx(savedipl);
153 1.1 yamt }
154 1.1 yamt
155 1.1 yamt void
156 1.1 yamt agr_ioctl_lock(struct agr_softc *sc)
157 1.1 yamt {
158 1.1 yamt
159 1.1 yamt lockmgr(&sc->sc_ioctl_lock, LK_EXCLUSIVE, NULL);
160 1.1 yamt }
161 1.1 yamt
162 1.1 yamt void
163 1.1 yamt agr_ioctl_unlock(struct agr_softc *sc)
164 1.1 yamt {
165 1.1 yamt
166 1.1 yamt lockmgr(&sc->sc_ioctl_lock, LK_RELEASE, NULL);
167 1.1 yamt }
168 1.1 yamt
169 1.1 yamt /*
170 1.1 yamt * agr_xmit_frame: transmit a pre-built frame.
171 1.1 yamt */
172 1.1 yamt
173 1.1 yamt int
174 1.1 yamt agr_xmit_frame(struct ifnet *ifp_port, struct mbuf *m)
175 1.1 yamt {
176 1.1 yamt int error;
177 1.1 yamt
178 1.1 yamt struct sockaddr_storage dst0;
179 1.1 yamt struct sockaddr *dst;
180 1.1 yamt int hdrlen;
181 1.1 yamt
182 1.1 yamt /*
183 1.1 yamt * trim off link level header and let if_output re-add it.
184 1.1 yamt * XXX better to introduce an API to transmit pre-built frames.
185 1.1 yamt */
186 1.1 yamt
187 1.1 yamt hdrlen = ifp_port->if_hdrlen;
188 1.1 yamt if (m->m_pkthdr.len < hdrlen) {
189 1.1 yamt m_freem(m);
190 1.1 yamt return EINVAL;
191 1.1 yamt }
192 1.1 yamt memset(&dst0, 0, sizeof(dst0));
193 1.1 yamt dst = (struct sockaddr *)&dst0;
194 1.1 yamt dst->sa_family = pseudo_AF_HDRCMPLT;
195 1.1 yamt dst->sa_len = hdrlen;
196 1.1 yamt m_copydata(m, 0, hdrlen, &dst->sa_data);
197 1.1 yamt m_adj(m, hdrlen);
198 1.1 yamt
199 1.1 yamt error = (*ifp_port->if_output)(ifp_port, m, dst, NULL);
200 1.1 yamt
201 1.1 yamt return error;
202 1.1 yamt }
203 1.1 yamt
204 1.1 yamt int
205 1.1 yamt agrport_ioctl(struct agr_port *port, u_long cmd, caddr_t arg)
206 1.1 yamt {
207 1.1 yamt struct ifnet *ifp = port->port_ifp;
208 1.1 yamt
209 1.1 yamt KASSERT(ifp->if_agrprivate == (void *)port);
210 1.1 yamt KASSERT(ifp->if_ioctl == agr_ioctl_filter);
211 1.1 yamt
212 1.1 yamt return (*port->port_ioctl)(ifp, cmd, arg);
213 1.1 yamt }
214 1.1 yamt
215 1.1 yamt /*
216 1.1 yamt * INTERNAL FUNCTIONS
217 1.1 yamt */
218 1.1 yamt
219 1.1 yamt static int
220 1.1 yamt agr_clone_create(struct if_clone *ifc, int unit)
221 1.1 yamt {
222 1.1 yamt struct agr_softc *sc;
223 1.1 yamt struct ifnet *ifp;
224 1.1 yamt
225 1.1 yamt sc = agr_alloc_softc();
226 1.1 yamt TAILQ_INIT(&sc->sc_ports);
227 1.1 yamt lockinit(&sc->sc_ioctl_lock, PSOCK, "agrioctl", 0, 0);
228 1.1 yamt simple_lock_init(&sc->sc_lock);
229 1.1 yamt agrtimer_init(sc);
230 1.1 yamt ifp = &sc->sc_if;
231 1.1 yamt snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
232 1.1 yamt ifc->ifc_name, unit);
233 1.1 yamt
234 1.1 yamt ifp->if_softc = sc;
235 1.1 yamt ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
236 1.1 yamt ifp->if_start = agr_start;
237 1.1 yamt ifp->if_ioctl = agr_ioctl;
238 1.1 yamt IFQ_SET_READY(&ifp->if_snd);
239 1.1 yamt
240 1.1 yamt if_attach(ifp);
241 1.1 yamt
242 1.1 yamt agr_reset_iftype(ifp);
243 1.1 yamt
244 1.1 yamt return 0;
245 1.1 yamt }
246 1.1 yamt
247 1.1 yamt static void
248 1.1 yamt agr_reset_iftype(struct ifnet *ifp)
249 1.1 yamt {
250 1.1 yamt
251 1.1 yamt ifp->if_type = IFT_OTHER;
252 1.1 yamt ifp->if_dlt = DLT_NULL;
253 1.1 yamt ifp->if_addrlen = 0;
254 1.1 yamt if_alloc_sadl(ifp);
255 1.1 yamt }
256 1.1 yamt
257 1.1 yamt static int
258 1.1 yamt agr_clone_destroy(struct ifnet *ifp)
259 1.1 yamt {
260 1.1 yamt struct agr_softc *sc = ifp->if_softc;
261 1.1 yamt int error;
262 1.1 yamt int s;
263 1.1 yamt
264 1.1 yamt agr_ioctl_lock(sc);
265 1.1 yamt
266 1.1 yamt s = AGR_LOCK(sc);
267 1.1 yamt if (sc->sc_nports > 0) {
268 1.1 yamt error = EBUSY;
269 1.1 yamt } else {
270 1.1 yamt error = 0;
271 1.1 yamt }
272 1.1 yamt AGR_UNLOCK(sc, s);
273 1.1 yamt
274 1.1 yamt agr_ioctl_unlock(sc);
275 1.1 yamt
276 1.1 yamt if (error == 0) {
277 1.1 yamt if_detach(ifp);
278 1.1 yamt agr_free_softc(sc);
279 1.1 yamt }
280 1.1 yamt
281 1.1 yamt return error;
282 1.1 yamt }
283 1.1 yamt
284 1.1 yamt static struct agr_port *
285 1.1 yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
286 1.1 yamt {
287 1.1 yamt
288 1.1 yamt return (*sc->sc_iftop->iftop_select_tx_port)(sc, m);
289 1.1 yamt }
290 1.1 yamt
291 1.1 yamt #if 0 /* "generic" version */
292 1.1 yamt static struct agr_port *
293 1.1 yamt agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
294 1.1 yamt {
295 1.1 yamt struct agr_port *port;
296 1.1 yamt uint32_t hash;
297 1.1 yamt
298 1.1 yamt hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
299 1.1 yamt if (sc->sc_nports == 0)
300 1.1 yamt return NULL;
301 1.1 yamt hash %= sc->sc_nports;
302 1.1 yamt port = TAILQ_FIRST(&sc->sc_ports);
303 1.1 yamt KASSERT(port != NULL);
304 1.1 yamt while (hash--) {
305 1.1 yamt port = TAILQ_NEXT(port, port_q);
306 1.1 yamt KASSERT(port != NULL);
307 1.1 yamt }
308 1.1 yamt
309 1.1 yamt return port;
310 1.1 yamt }
311 1.1 yamt #endif /* 0 */
312 1.1 yamt
313 1.1 yamt static void
314 1.1 yamt agr_start(struct ifnet *ifp)
315 1.1 yamt {
316 1.1 yamt struct agr_softc *sc = ifp->if_softc;
317 1.1 yamt struct mbuf *m;
318 1.1 yamt int s;
319 1.1 yamt
320 1.1 yamt s = AGR_LOCK(sc);
321 1.1 yamt
322 1.1 yamt while (/* CONSTCOND */ 1) {
323 1.1 yamt struct agr_port *port;
324 1.1 yamt
325 1.1 yamt IFQ_DEQUEUE(&ifp->if_snd, m);
326 1.1 yamt if (m == NULL) {
327 1.1 yamt break;
328 1.1 yamt }
329 1.1 yamt #if NBPFILTER > 0
330 1.1 yamt if (ifp->if_bpf) {
331 1.1 yamt bpf_mtap(ifp->if_bpf, m);
332 1.1 yamt }
333 1.1 yamt #endif
334 1.1 yamt port = agr_select_tx_port(sc, m);
335 1.1 yamt if (port) {
336 1.1 yamt int error;
337 1.1 yamt
338 1.1 yamt error = agr_xmit_frame(port->port_ifp, m);
339 1.1 yamt if (error) {
340 1.1 yamt ifp->if_oerrors++;
341 1.1 yamt } else {
342 1.1 yamt ifp->if_opackets++;
343 1.1 yamt }
344 1.1 yamt } else {
345 1.1 yamt m_freem(m);
346 1.1 yamt ifp->if_oerrors++;
347 1.1 yamt }
348 1.1 yamt }
349 1.1 yamt
350 1.1 yamt AGR_UNLOCK(sc, s);
351 1.1 yamt
352 1.1 yamt ifp->if_flags &= ~IFF_OACTIVE;
353 1.1 yamt }
354 1.1 yamt
355 1.1 yamt static int
356 1.1 yamt agr_setconfig(struct ifnet *ifp, const struct agrreq *ar)
357 1.1 yamt {
358 1.1 yamt int cmd = ar->ar_cmd;
359 1.1 yamt struct ifnet *ifp_port;
360 1.1 yamt int error = 0;
361 1.1 yamt char ifname[IFNAMSIZ];
362 1.1 yamt
363 1.1 yamt error = copyin(ar->ar_buf, ifname, MIN(ar->ar_buflen, sizeof(ifname)));
364 1.1 yamt if (error) {
365 1.1 yamt return error;
366 1.1 yamt }
367 1.1 yamt ifp_port = ifunit(ifname);
368 1.1 yamt if (ifp_port == NULL) {
369 1.1 yamt return ENOENT;
370 1.1 yamt }
371 1.1 yamt
372 1.1 yamt switch (cmd) {
373 1.1 yamt case AGRCMD_ADDPORT:
374 1.1 yamt error = agr_addport(ifp, ifp_port);
375 1.1 yamt break;
376 1.1 yamt
377 1.1 yamt case AGRCMD_REMPORT:
378 1.1 yamt error = agr_remport(ifp, ifp_port);
379 1.1 yamt break;
380 1.1 yamt
381 1.1 yamt default:
382 1.1 yamt error = EINVAL;
383 1.1 yamt break;
384 1.1 yamt }
385 1.1 yamt
386 1.1 yamt return error;
387 1.1 yamt }
388 1.1 yamt
389 1.1 yamt static int
390 1.1 yamt agr_getportlist(struct ifnet *ifp, struct agrreq *ar)
391 1.1 yamt {
392 1.1 yamt struct agr_softc *sc = ifp->if_softc;
393 1.1 yamt struct agr_port *port;
394 1.1 yamt struct agrportlist apl;
395 1.1 yamt struct agrportinfo api;
396 1.1 yamt char *cp = ar->ar_buf;
397 1.1 yamt size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
398 1.1 yamt int error;
399 1.1 yamt
400 1.1 yamt if (cp != NULL) {
401 1.1 yamt memset(&apl, 0, sizeof(apl));
402 1.1 yamt memset(&api, 0, sizeof(api));
403 1.1 yamt
404 1.1 yamt if (bufleft < sizeof(apl)) {
405 1.1 yamt return E2BIG;
406 1.1 yamt }
407 1.1 yamt apl.apl_nports = sc->sc_nports;
408 1.1 yamt error = copyout(&apl, cp, sizeof(apl));
409 1.1 yamt if (error) {
410 1.1 yamt return error;
411 1.1 yamt }
412 1.1 yamt cp += sizeof(apl);
413 1.1 yamt }
414 1.1 yamt bufleft -= sizeof(apl);
415 1.1 yamt
416 1.1 yamt TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
417 1.1 yamt if (cp != NULL) {
418 1.1 yamt if (bufleft < sizeof(api)) {
419 1.1 yamt return E2BIG;
420 1.1 yamt }
421 1.1 yamt memcpy(api.api_ifname, port->port_ifp->if_xname,
422 1.1 yamt sizeof(api.api_ifname));
423 1.1 yamt api.api_flags = 0;
424 1.1 yamt if (port->port_flags & AGRPORT_COLLECTING) {
425 1.1 yamt api.api_flags |= AGRPORTINFO_COLLECTING;
426 1.1 yamt }
427 1.1 yamt if (port->port_flags & AGRPORT_DISTRIBUTING) {
428 1.1 yamt api.api_flags |= AGRPORTINFO_DISTRIBUTING;
429 1.1 yamt }
430 1.1 yamt error = copyout(&api, cp, sizeof(api));
431 1.1 yamt if (error) {
432 1.1 yamt return error;
433 1.1 yamt }
434 1.1 yamt cp += sizeof(api);
435 1.1 yamt }
436 1.1 yamt bufleft -= sizeof(api);
437 1.1 yamt }
438 1.1 yamt
439 1.1 yamt if (cp == NULL) {
440 1.1 yamt ar->ar_buflen = -bufleft; /* necessary buffer size */
441 1.1 yamt }
442 1.1 yamt
443 1.1 yamt return 0;
444 1.1 yamt }
445 1.1 yamt
446 1.1 yamt static int
447 1.1 yamt agr_getconfig(struct ifnet *ifp, struct agrreq *ar)
448 1.1 yamt {
449 1.1 yamt int cmd = ar->ar_cmd;
450 1.1 yamt int error;
451 1.1 yamt
452 1.1 yamt switch (cmd) {
453 1.1 yamt case AGRCMD_PORTLIST:
454 1.1 yamt error = agr_getportlist(ifp, ar);
455 1.1 yamt break;
456 1.1 yamt
457 1.1 yamt default:
458 1.1 yamt error = EINVAL;
459 1.1 yamt break;
460 1.1 yamt }
461 1.1 yamt
462 1.1 yamt return error;
463 1.1 yamt }
464 1.1 yamt
465 1.1 yamt static int
466 1.1 yamt agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
467 1.1 yamt {
468 1.1 yamt struct agr_softc *sc = ifp->if_softc;
469 1.1 yamt struct agr_port *port = NULL;
470 1.1 yamt int error = 0;
471 1.1 yamt int s;
472 1.1 yamt
473 1.1 yamt if (ifp_port->if_ioctl == NULL) {
474 1.1 yamt error = EOPNOTSUPP;
475 1.1 yamt goto out;
476 1.1 yamt }
477 1.1 yamt
478 1.1 yamt if (ifp_port->if_agrprivate) {
479 1.1 yamt error = EBUSY;
480 1.1 yamt goto out;
481 1.1 yamt }
482 1.1 yamt
483 1.1 yamt if (ifp_port->if_start == agr_start) {
484 1.1 yamt error = EINVAL;
485 1.1 yamt goto out;
486 1.1 yamt }
487 1.1 yamt
488 1.1 yamt port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
489 1.1 yamt M_WAITOK | M_ZERO);
490 1.1 yamt if (port == NULL) {
491 1.1 yamt error = ENOMEM;
492 1.1 yamt goto out;
493 1.1 yamt }
494 1.1 yamt port->port_flags = AGRPORT_LARVAL;
495 1.1 yamt
496 1.1 yamt if (TAILQ_NEXT(TAILQ_FIRST(&ifp_port->if_addrlist), ifa_list) != NULL) {
497 1.1 yamt error = EBUSY;
498 1.1 yamt goto out;
499 1.1 yamt }
500 1.1 yamt
501 1.1 yamt if (sc->sc_nports == 0) {
502 1.1 yamt switch (ifp_port->if_type) {
503 1.1 yamt case IFT_ETHER:
504 1.1 yamt sc->sc_iftop = &agrether_ops;
505 1.1 yamt break;
506 1.1 yamt
507 1.1 yamt default:
508 1.1 yamt error = EPROTONOSUPPORT; /* XXX */
509 1.1 yamt goto out;
510 1.1 yamt }
511 1.1 yamt
512 1.1 yamt error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
513 1.1 yamt if (error)
514 1.1 yamt goto out;
515 1.1 yamt agrtimer_start(sc);
516 1.1 yamt } else {
517 1.1 yamt if (ifp->if_type != ifp_port->if_type) {
518 1.1 yamt error = EINVAL;
519 1.1 yamt goto out;
520 1.1 yamt }
521 1.1 yamt if (ifp->if_addrlen != ifp_port->if_addrlen) {
522 1.1 yamt error = EINVAL;
523 1.1 yamt goto out;
524 1.1 yamt }
525 1.1 yamt }
526 1.1 yamt
527 1.1 yamt memcpy(port->port_origlladdr, LLADDR(ifp_port->if_sadl),
528 1.1 yamt ifp_port->if_addrlen);
529 1.1 yamt
530 1.1 yamt /*
531 1.1 yamt * start to modify ifp_port.
532 1.1 yamt */
533 1.1 yamt
534 1.1 yamt error = (*ifp_port->if_ioctl)(ifp_port, SIOCSIFADDR,
535 1.1 yamt (caddr_t)TAILQ_FIRST(&ifp->if_addrlist));
536 1.1 yamt
537 1.1 yamt if (error) {
538 1.1 yamt printf("%s: SIOCSIFADDR error %d\n", __func__, error);
539 1.1 yamt goto cleanup;
540 1.1 yamt }
541 1.1 yamt port->port_flags |= AGRPORT_LADDRCHANGED;
542 1.1 yamt
543 1.1 yamt ifp->if_type = ifp_port->if_type;
544 1.1 yamt s = AGR_LOCK(sc);
545 1.1 yamt
546 1.1 yamt port->port_ifp = ifp_port;
547 1.1 yamt ifp_port->if_agrprivate = port;
548 1.1 yamt port->port_agrifp = ifp;
549 1.1 yamt TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
550 1.1 yamt sc->sc_nports++;
551 1.1 yamt
552 1.1 yamt port->port_ioctl = ifp_port->if_ioctl;
553 1.1 yamt ifp_port->if_ioctl = agr_ioctl_filter;
554 1.1 yamt
555 1.1 yamt port->port_flags |= AGRPORT_ATTACHED;
556 1.1 yamt
557 1.1 yamt AGR_UNLOCK(sc, s);
558 1.1 yamt
559 1.1 yamt error = (*sc->sc_iftop->iftop_portinit)(sc, port);
560 1.1 yamt if (error) {
561 1.1 yamt printf("%s: portinit error %d\n", __func__, error);
562 1.1 yamt goto cleanup;
563 1.1 yamt }
564 1.1 yamt
565 1.1 yamt ifp->if_flags |= IFF_RUNNING;
566 1.1 yamt
567 1.1 yamt agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
568 1.1 yamt error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, TRUE);
569 1.1 yamt if (error) {
570 1.1 yamt printf("%s: configmulti error %d\n", __func__, error);
571 1.1 yamt goto cleanup;
572 1.1 yamt }
573 1.1 yamt
574 1.1 yamt s = AGR_LOCK(sc);
575 1.1 yamt port->port_flags &= ~AGRPORT_LARVAL;
576 1.1 yamt AGR_UNLOCK(sc, s);
577 1.1 yamt out:
578 1.1 yamt if (error && port) {
579 1.1 yamt free(port, M_DEVBUF);
580 1.1 yamt }
581 1.1 yamt return error;
582 1.1 yamt
583 1.1 yamt cleanup:
584 1.1 yamt if (agrport_cleanup(sc, port)) {
585 1.1 yamt printf("%s: error on cleanup\n", __func__);
586 1.1 yamt
587 1.1 yamt port = NULL; /* XXX */
588 1.1 yamt }
589 1.1 yamt
590 1.1 yamt if (sc->sc_nports == 0) {
591 1.1 yamt KASSERT(TAILQ_EMPTY(&sc->sc_ports));
592 1.1 yamt agrtimer_stop(sc);
593 1.1 yamt (*sc->sc_iftop->iftop_dtor)(sc);
594 1.1 yamt sc->sc_iftop = NULL;
595 1.1 yamt agr_reset_iftype(ifp);
596 1.1 yamt } else {
597 1.1 yamt KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
598 1.1 yamt }
599 1.1 yamt
600 1.1 yamt goto out;
601 1.1 yamt }
602 1.1 yamt
603 1.1 yamt static int
604 1.1 yamt agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
605 1.1 yamt {
606 1.1 yamt struct agr_softc *sc = ifp->if_softc;
607 1.1 yamt struct agr_port *port;
608 1.1 yamt int error = 0;
609 1.1 yamt int s;
610 1.1 yamt
611 1.1 yamt if (ifp_port->if_agrprivate == NULL) {
612 1.1 yamt error = ENOENT;
613 1.1 yamt return error;
614 1.1 yamt }
615 1.1 yamt
616 1.1 yamt port = ifp_port->if_agrprivate;
617 1.1 yamt if (port->port_agrifp != ifp) {
618 1.1 yamt error = EINVAL;
619 1.1 yamt return error;
620 1.1 yamt }
621 1.1 yamt
622 1.1 yamt KASSERT(sc->sc_nports > 0);
623 1.1 yamt
624 1.1 yamt #if 0
625 1.1 yamt if (sc->sc_nports == 1 &&
626 1.1 yamt TAILQ_NEXT(TAILQ_FIRST(&ifp->if_addrlist), ifa_list) != NULL) {
627 1.1 yamt error = EBUSY;
628 1.1 yamt return error;
629 1.1 yamt }
630 1.1 yamt #endif
631 1.1 yamt
632 1.1 yamt s = AGR_LOCK(sc);
633 1.1 yamt port->port_flags |= AGRPORT_DETACHING;
634 1.1 yamt AGR_UNLOCK(sc, s);
635 1.1 yamt
636 1.1 yamt error = (*sc->sc_iftop->iftop_portfini)(sc, port);
637 1.1 yamt if (error) {
638 1.1 yamt /* XXX XXX */
639 1.1 yamt printf("%s: portfini error %d\n", __func__, error);
640 1.1 yamt goto out;
641 1.1 yamt }
642 1.1 yamt
643 1.1 yamt error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, FALSE);
644 1.1 yamt if (error) {
645 1.1 yamt /* XXX XXX */
646 1.1 yamt printf("%s: configmulti_port error %d\n", __func__, error);
647 1.1 yamt goto out;
648 1.1 yamt }
649 1.1 yamt
650 1.1 yamt error = agrport_cleanup(sc, port);
651 1.1 yamt if (error) {
652 1.1 yamt /* XXX XXX */
653 1.1 yamt printf("%s: agrport_cleanup error %d\n", __func__, error);
654 1.1 yamt goto out;
655 1.1 yamt }
656 1.1 yamt
657 1.1 yamt free(port, M_DEVBUF);
658 1.1 yamt
659 1.1 yamt out:
660 1.1 yamt if (sc->sc_nports == 0) {
661 1.1 yamt KASSERT(TAILQ_EMPTY(&sc->sc_ports));
662 1.1 yamt agrtimer_stop(sc);
663 1.1 yamt (*sc->sc_iftop->iftop_dtor)(sc);
664 1.1 yamt sc->sc_iftop = NULL;
665 1.1 yamt /* XXX should purge all addresses? */
666 1.1 yamt agr_reset_iftype(ifp);
667 1.1 yamt } else {
668 1.1 yamt KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
669 1.1 yamt }
670 1.1 yamt
671 1.1 yamt return error;
672 1.1 yamt }
673 1.1 yamt
674 1.1 yamt static int
675 1.1 yamt agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
676 1.1 yamt {
677 1.1 yamt struct ifnet *ifp_port = port->port_ifp;
678 1.1 yamt int error;
679 1.1 yamt int result = 0;
680 1.1 yamt int s;
681 1.1 yamt
682 1.1 yamt error = agrport_config_promisc(port, FALSE);
683 1.1 yamt if (error) {
684 1.1 yamt printf("%s: config_promisc error %d\n", __func__, error);
685 1.1 yamt result = error;
686 1.1 yamt }
687 1.1 yamt
688 1.1 yamt if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
689 1.1 yamt #if 0
690 1.1 yamt memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
691 1.1 yamt ifp_port->if_addrlen);
692 1.1 yamt if (ifp_port->if_init != NULL) {
693 1.1 yamt error = (*ifp_port->if_init)(ifp_port);
694 1.1 yamt }
695 1.1 yamt #else
696 1.1 yamt struct sockaddr_dl *sdl;
697 1.1 yamt struct ifaddr ifa;
698 1.1 yamt int sdllen;
699 1.1 yamt int addrlen;
700 1.1 yamt
701 1.1 yamt addrlen = ifp_port->if_addrlen;
702 1.1 yamt sdllen = sizeof(*sdl) - sizeof(sdl->sdl_data) + addrlen;
703 1.1 yamt sdl = malloc(sdllen, M_TEMP, M_WAITOK);
704 1.1 yamt if (sdl == NULL) {
705 1.1 yamt error = ENOMEM;
706 1.1 yamt } else {
707 1.1 yamt memset(sdl, 0, sdllen);
708 1.1 yamt sdl->sdl_len = sdllen;
709 1.1 yamt sdl->sdl_family = AF_LINK;
710 1.1 yamt sdl->sdl_type = ifp_port->if_type;
711 1.1 yamt sdl->sdl_alen = addrlen;
712 1.1 yamt memcpy(LLADDR(sdl), port->port_origlladdr, addrlen);
713 1.1 yamt memset(&ifa, 0, sizeof(ifa));
714 1.1 yamt ifa.ifa_addr = (struct sockaddr *)sdl;
715 1.1 yamt error = agrport_ioctl(port, SIOCSIFADDR, (caddr_t)&ifa);
716 1.1 yamt free(sdl, M_TEMP);
717 1.1 yamt }
718 1.1 yamt #endif
719 1.1 yamt if (error) {
720 1.1 yamt printf("%s: if_init error %d\n", __func__, error);
721 1.1 yamt result = error;
722 1.1 yamt } else {
723 1.1 yamt port->port_flags &= ~AGRPORT_LADDRCHANGED;
724 1.1 yamt }
725 1.1 yamt }
726 1.1 yamt
727 1.1 yamt s = AGR_LOCK(sc);
728 1.1 yamt if ((port->port_flags & AGRPORT_ATTACHED)) {
729 1.1 yamt ifp_port->if_agrprivate = NULL;
730 1.1 yamt
731 1.1 yamt TAILQ_REMOVE(&sc->sc_ports, port, port_q);
732 1.1 yamt sc->sc_nports--;
733 1.1 yamt
734 1.1 yamt KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
735 1.1 yamt ifp_port->if_ioctl = port->port_ioctl;
736 1.1 yamt
737 1.1 yamt port->port_flags &= ~AGRPORT_ATTACHED;
738 1.1 yamt }
739 1.1 yamt AGR_UNLOCK(sc, s);
740 1.1 yamt
741 1.1 yamt return result;
742 1.1 yamt }
743 1.1 yamt
744 1.1 yamt static int
745 1.1 yamt agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
746 1.1 yamt {
747 1.1 yamt struct agr_softc *sc = ifp->if_softc;
748 1.1 yamt int error;
749 1.1 yamt
750 1.1 yamt error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
751 1.1 yamt (cmd == SIOCADDMULTI));
752 1.1 yamt
753 1.1 yamt return error;
754 1.1 yamt }
755 1.1 yamt
756 1.1 yamt /* XXX an incomplete hack; can't filter ioctls handled ifioctl(). */
757 1.1 yamt static int
758 1.1 yamt agr_ioctl_filter(struct ifnet *ifp, u_long cmd, caddr_t arg)
759 1.1 yamt {
760 1.1 yamt struct agr_port *port = ifp->if_agrprivate;
761 1.1 yamt int error;
762 1.1 yamt
763 1.1 yamt KASSERT(port);
764 1.1 yamt
765 1.1 yamt switch (cmd) {
766 1.1 yamt case SIOCGIFADDR:
767 1.1 yamt case SIOCGIFMEDIA:
768 1.1 yamt case SIOCSIFFLAGS: /* XXX */
769 1.1 yamt error = agrport_ioctl(port, cmd, arg);
770 1.1 yamt break;
771 1.1 yamt default:
772 1.1 yamt error = EBUSY;
773 1.1 yamt break;
774 1.1 yamt }
775 1.1 yamt return error;
776 1.1 yamt }
777 1.1 yamt
778 1.1 yamt static int
779 1.1 yamt agrreq_copyin(const void *ubuf, struct agrreq *ar)
780 1.1 yamt {
781 1.1 yamt int error;
782 1.1 yamt
783 1.1 yamt error = copyin(ubuf, ar, sizeof(*ar));
784 1.1 yamt if (error) {
785 1.1 yamt return error;
786 1.1 yamt }
787 1.1 yamt
788 1.1 yamt if (ar->ar_version != AGRREQ_VERSION) {
789 1.1 yamt return EINVAL;
790 1.1 yamt }
791 1.1 yamt
792 1.1 yamt return 0;
793 1.1 yamt }
794 1.1 yamt
795 1.1 yamt static int
796 1.1 yamt agrreq_copyout(void *ubuf, struct agrreq *ar)
797 1.1 yamt {
798 1.1 yamt int error;
799 1.1 yamt
800 1.1 yamt KASSERT(ar->ar_version == AGRREQ_VERSION);
801 1.1 yamt
802 1.1 yamt error = copyout(ar, ubuf, sizeof(*ar));
803 1.1 yamt if (error) {
804 1.1 yamt return error;
805 1.1 yamt }
806 1.1 yamt
807 1.1 yamt return 0;
808 1.1 yamt }
809 1.1 yamt
810 1.1 yamt static int
811 1.1 yamt agr_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
812 1.1 yamt {
813 1.1 yamt struct agr_softc *sc = ifp->if_softc;
814 1.1 yamt struct ifreq *ifr = (struct ifreq *)data;
815 1.1 yamt struct ifaddr *ifa = (struct ifaddr *)data;
816 1.1 yamt struct sockaddr *sa;
817 1.1 yamt struct agrreq ar;
818 1.1 yamt struct proc *p;
819 1.1 yamt int error = 0;
820 1.1 yamt int s;
821 1.1 yamt
822 1.1 yamt agr_ioctl_lock(sc);
823 1.1 yamt
824 1.1 yamt s = splnet();
825 1.1 yamt
826 1.1 yamt switch (cmd) {
827 1.1 yamt case SIOCSIFADDR:
828 1.1 yamt if (sc->sc_nports == 0) {
829 1.1 yamt error = EINVAL;
830 1.1 yamt break;
831 1.1 yamt }
832 1.1 yamt ifp->if_flags |= IFF_UP;
833 1.1 yamt switch (ifa->ifa_addr->sa_family) {
834 1.1 yamt #if defined(INET)
835 1.1 yamt case AF_INET:
836 1.1 yamt arp_ifinit(ifp, ifa);
837 1.1 yamt break;
838 1.1 yamt #endif
839 1.1 yamt default:
840 1.1 yamt break;
841 1.1 yamt }
842 1.1 yamt break;
843 1.1 yamt
844 1.1 yamt case SIOCGIFADDR:
845 1.1 yamt sa = (struct sockaddr *)&ifr->ifr_data;
846 1.1 yamt memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ifp->if_addrlen);
847 1.1 yamt break;
848 1.1 yamt
849 1.1 yamt #if 0 /* notyet */
850 1.1 yamt case SIOCSIFMTU:
851 1.1 yamt #endif
852 1.1 yamt
853 1.1 yamt case SIOCSIFFLAGS:
854 1.1 yamt agr_config_promisc(sc);
855 1.1 yamt break;
856 1.1 yamt
857 1.1 yamt case SIOCSETAGR:
858 1.1 yamt splx(s);
859 1.1 yamt p = curproc; /* XXX */
860 1.1 yamt error = suser(p->p_ucred, &p->p_acflag);
861 1.1 yamt if (!error) {
862 1.1 yamt error = agrreq_copyin(ifr->ifr_data, &ar);
863 1.1 yamt }
864 1.1 yamt if (!error) {
865 1.1 yamt error = agr_setconfig(ifp, &ar);
866 1.1 yamt }
867 1.1 yamt s = splnet();
868 1.1 yamt break;
869 1.1 yamt
870 1.1 yamt case SIOCGETAGR:
871 1.1 yamt splx(s);
872 1.1 yamt error = agrreq_copyin(ifr->ifr_data, &ar);
873 1.1 yamt if (!error) {
874 1.1 yamt error = agr_getconfig(ifp, &ar);
875 1.1 yamt }
876 1.1 yamt if (!error) {
877 1.1 yamt error = agrreq_copyout(ifr->ifr_data, &ar);
878 1.1 yamt }
879 1.1 yamt s = splnet();
880 1.1 yamt break;
881 1.1 yamt
882 1.1 yamt case SIOCADDMULTI:
883 1.1 yamt case SIOCDELMULTI:
884 1.1 yamt if (sc->sc_nports == 0) {
885 1.1 yamt error = EINVAL;
886 1.1 yamt break;
887 1.1 yamt }
888 1.1 yamt error = agr_ioctl_multi(ifp, cmd, ifr);
889 1.1 yamt break;
890 1.1 yamt
891 1.1 yamt default:
892 1.1 yamt error = EINVAL;
893 1.1 yamt break;
894 1.1 yamt }
895 1.1 yamt
896 1.1 yamt splx(s);
897 1.1 yamt
898 1.1 yamt agr_ioctl_unlock(sc);
899 1.1 yamt
900 1.1 yamt return error;
901 1.1 yamt }
902 1.1 yamt
903 1.1 yamt static int
904 1.1 yamt agr_config_promisc(struct agr_softc *sc)
905 1.1 yamt {
906 1.1 yamt int error;
907 1.1 yamt
908 1.1 yamt agr_port_foreach(sc, agrport_config_promisc_callback, &error);
909 1.1 yamt
910 1.1 yamt return error;
911 1.1 yamt }
912 1.1 yamt
913 1.1 yamt static int
914 1.1 yamt agrport_config_promisc_callback(struct agr_port *port, void *arg)
915 1.1 yamt {
916 1.1 yamt struct agr_softc *sc = AGR_SC_FROM_PORT(port);
917 1.1 yamt int *errorp = arg;
918 1.1 yamt int error;
919 1.1 yamt boolean_t promisc;
920 1.1 yamt
921 1.1 yamt promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
922 1.1 yamt
923 1.1 yamt error = agrport_config_promisc(port, promisc);
924 1.1 yamt if (error) {
925 1.1 yamt *errorp = error;
926 1.1 yamt }
927 1.1 yamt
928 1.1 yamt return 0;
929 1.1 yamt }
930 1.1 yamt
931 1.1 yamt static int
932 1.1 yamt agrport_config_promisc(struct agr_port *port, boolean_t promisc)
933 1.1 yamt {
934 1.1 yamt int error;
935 1.1 yamt
936 1.1 yamt if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
937 1.1 yamt (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
938 1.1 yamt return 0;
939 1.1 yamt }
940 1.1 yamt
941 1.1 yamt error = ifpromisc(port->port_ifp, promisc);
942 1.1 yamt if (error == 0) {
943 1.1 yamt if (promisc) {
944 1.1 yamt port->port_flags |= AGRPORT_PROMISC;
945 1.1 yamt } else {
946 1.1 yamt port->port_flags &= ~AGRPORT_PROMISC;
947 1.1 yamt }
948 1.1 yamt }
949 1.1 yamt
950 1.1 yamt return error;
951 1.1 yamt }
952