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