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