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