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