if_agr.c revision 1.40 1 /* $NetBSD: if_agr.c,v 1.40 2016/12/15 09:28:06 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.40 2016/12/15 09:28:06 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 /*
259 * Enable vlan hardware assist for the specified port.
260 */
261 static int
262 agr_vlan_add(struct agr_port *port, void *arg)
263 {
264 struct ifnet *ifp = port->port_ifp;
265 struct ethercom *ec_port = (void *)ifp;
266 int error=0;
267
268 if (ec_port->ec_nvlans++ == 0 &&
269 (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
270 struct ifnet *p = port->port_ifp;
271 /*
272 * Enable Tx/Rx of VLAN-sized frames.
273 */
274 ec_port->ec_capenable |= ETHERCAP_VLAN_MTU;
275 if (p->if_flags & IFF_UP) {
276 error = if_flags_set(p, p->if_flags);
277 if (error) {
278 if (ec_port->ec_nvlans-- == 1)
279 ec_port->ec_capenable &=
280 ~ETHERCAP_VLAN_MTU;
281 return (error);
282 }
283 }
284 }
285
286 return error;
287 }
288
289 /*
290 * Disable vlan hardware assist for the specified port.
291 */
292 static int
293 agr_vlan_del(struct agr_port *port, void *arg)
294 {
295 struct ethercom *ec_port = (void *)port->port_ifp;
296
297 /* Disable vlan support */
298 if (ec_port->ec_nvlans-- == 1) {
299 /*
300 * Disable Tx/Rx of VLAN-sized frames.
301 */
302 ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU;
303 if (port->port_ifp->if_flags & IFF_UP) {
304 (void)if_flags_set(port->port_ifp,
305 port->port_ifp->if_flags);
306 }
307 }
308
309 return 0;
310 }
311
312
313 /*
314 * Check for vlan attach/detach.
315 * ec->ec_nvlans is directly modified by the vlan driver.
316 * We keep a local count in sc (sc->sc_nvlans) to detect
317 * when the vlan driver attaches or detaches.
318 * Note the agr interface must be up for this to work.
319 */
320 static void
321 agr_vlan_check(struct ifnet *ifp, struct agr_softc *sc)
322 {
323 struct ethercom *ec = (void *)ifp;
324
325 /* vlans in sync? */
326 if (sc->sc_nvlans == ec->ec_nvlans) {
327 return;
328 }
329
330 if (sc->sc_nvlans == 0) {
331 /* vlan added */
332 agr_port_foreach(sc, agr_vlan_add, NULL);
333 sc->sc_nvlans = ec->ec_nvlans;
334 } else if (ec->ec_nvlans == 0) {
335 /* vlan removed */
336 agr_port_foreach(sc, agr_vlan_del, NULL);
337 sc->sc_nvlans = 0;
338 }
339 }
340
341 static int
342 agr_clone_create(struct if_clone *ifc, int unit)
343 {
344 struct agr_softc *sc;
345 struct ifnet *ifp;
346
347 sc = agr_alloc_softc();
348 TAILQ_INIT(&sc->sc_ports);
349 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
350 mutex_init(&sc->sc_entry_mtx, MUTEX_DEFAULT, IPL_NONE);
351 cv_init(&sc->sc_insc_cv, "agrsoftc");
352 cv_init(&sc->sc_ports_cv, "agrports");
353 agrtimer_init(sc);
354 ifp = &sc->sc_if;
355 snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
356 ifc->ifc_name, unit);
357
358 ifp->if_softc = sc;
359 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
360 ifp->if_start = agr_start;
361 ifp->if_ioctl = agr_ioctl;
362 IFQ_SET_READY(&ifp->if_snd);
363
364 if_attach(ifp);
365
366 agr_reset_iftype(ifp);
367 atomic_inc_uint(&agr_count);
368 return 0;
369 }
370
371 static void
372 agr_reset_iftype(struct ifnet *ifp)
373 {
374
375 ifp->if_type = IFT_OTHER;
376 ifp->if_dlt = DLT_NULL;
377 ifp->if_addrlen = 0;
378 if_alloc_sadl(ifp);
379 }
380
381 static int
382 agr_clone_destroy(struct ifnet *ifp)
383 {
384 struct agr_softc *sc = ifp->if_softc;
385 int error;
386
387 if ((error = agr_pause(sc)) != 0)
388 return error;
389
390 if_detach(ifp);
391 agrtimer_destroy(sc);
392 /* Now that the ifnet has been detached, and our
393 * component ifnets are disconnected, there can be
394 * no new threads in the softc. Wait for every
395 * thread to get out of the softc.
396 */
397 agr_evacuate(sc);
398 mutex_destroy(&sc->sc_lock);
399 mutex_destroy(&sc->sc_entry_mtx);
400 cv_destroy(&sc->sc_insc_cv);
401 cv_destroy(&sc->sc_ports_cv);
402 agr_free_softc(sc);
403
404 atomic_dec_uint(&agr_count);
405 return 0;
406 }
407
408 static struct agr_port *
409 agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
410 {
411
412 return (*sc->sc_iftop->iftop_select_tx_port)(sc, m);
413 }
414
415 #if 0 /* "generic" version */
416 static struct agr_port *
417 agr_select_tx_port(struct agr_softc *sc, struct mbuf *m)
418 {
419 struct agr_port *port;
420 uint32_t hash;
421
422 hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
423 if (sc->sc_nports == 0)
424 return NULL;
425 hash %= sc->sc_nports;
426 port = TAILQ_FIRST(&sc->sc_ports);
427 KASSERT(port != NULL);
428 while (hash--) {
429 port = TAILQ_NEXT(port, port_q);
430 KASSERT(port != NULL);
431 }
432
433 return port;
434 }
435 #endif /* 0 */
436
437 static void
438 agr_start(struct ifnet *ifp)
439 {
440 struct agr_softc *sc = ifp->if_softc;
441 struct mbuf *m;
442
443 AGR_LOCK(sc);
444
445 while (/* CONSTCOND */ 1) {
446 struct agr_port *port;
447
448 IFQ_DEQUEUE(&ifp->if_snd, m);
449 if (m == NULL) {
450 break;
451 }
452 bpf_mtap(ifp, m);
453 port = agr_select_tx_port(sc, m);
454 if (port) {
455 int error;
456
457 error = agr_xmit_frame(port->port_ifp, m);
458 if (error) {
459 ifp->if_oerrors++;
460 } else {
461 ifp->if_opackets++;
462 }
463 } else {
464 m_freem(m);
465 ifp->if_oerrors++;
466 }
467 }
468
469 AGR_UNLOCK(sc);
470
471 ifp->if_flags &= ~IFF_OACTIVE;
472 }
473
474 static int
475 agr_setconfig(struct agr_softc *sc, const struct agrreq *ar)
476 {
477 struct ifnet *ifp = &sc->sc_if;
478 int cmd = ar->ar_cmd;
479 struct ifnet *ifp_port;
480 int error = 0;
481 char ifname[IFNAMSIZ];
482
483 memset(ifname, 0, sizeof(ifname));
484 error = copyin(ar->ar_buf, ifname,
485 MIN(ar->ar_buflen, sizeof(ifname) - 1));
486 if (error) {
487 return error;
488 }
489 ifp_port = ifunit(ifname);
490 if (ifp_port == NULL) {
491 return ENOENT;
492 }
493
494 agr_ports_lock(sc);
495 switch (cmd) {
496 case AGRCMD_ADDPORT:
497 error = agr_addport(ifp, ifp_port);
498 break;
499
500 case AGRCMD_REMPORT:
501 error = agr_remport(ifp, ifp_port);
502 break;
503
504 default:
505 error = EINVAL;
506 break;
507 }
508 agr_ports_unlock(sc);
509
510 return error;
511 }
512
513 static int
514 agr_getportlist(struct agr_softc *sc, struct agrreq *ar)
515 {
516 struct agr_port *port;
517 struct agrportlist apl;
518 struct agrportinfo api;
519 char *cp = ar->ar_buf;
520 size_t bufleft = (cp == NULL) ? 0 : ar->ar_buflen;
521 int error;
522
523 if (cp != NULL) {
524 memset(&apl, 0, sizeof(apl));
525 memset(&api, 0, sizeof(api));
526
527 if (bufleft < sizeof(apl)) {
528 return E2BIG;
529 }
530 apl.apl_nports = sc->sc_nports;
531 error = copyout(&apl, cp, sizeof(apl));
532 if (error) {
533 return error;
534 }
535 cp += sizeof(apl);
536 }
537 bufleft -= sizeof(apl);
538
539 TAILQ_FOREACH(port, &sc->sc_ports, port_q) {
540 if (cp != NULL) {
541 if (bufleft < sizeof(api)) {
542 return E2BIG;
543 }
544 memcpy(api.api_ifname, port->port_ifp->if_xname,
545 sizeof(api.api_ifname));
546 api.api_flags = 0;
547 if (port->port_flags & AGRPORT_COLLECTING) {
548 api.api_flags |= AGRPORTINFO_COLLECTING;
549 }
550 if (port->port_flags & AGRPORT_DISTRIBUTING) {
551 api.api_flags |= AGRPORTINFO_DISTRIBUTING;
552 }
553 error = copyout(&api, cp, sizeof(api));
554 if (error) {
555 return error;
556 }
557 cp += sizeof(api);
558 }
559 bufleft -= sizeof(api);
560 }
561
562 if (cp == NULL) {
563 ar->ar_buflen = -bufleft; /* necessary buffer size */
564 }
565
566 return 0;
567 }
568
569 static int
570 agr_getconfig(struct agr_softc *sc, struct agrreq *ar)
571 {
572 int cmd = ar->ar_cmd;
573 int error;
574
575 (void)agr_ports_enter(sc);
576 switch (cmd) {
577 case AGRCMD_PORTLIST:
578 error = agr_getportlist(sc, ar);
579 break;
580
581 default:
582 error = EINVAL;
583 break;
584 }
585 agr_ports_exit(sc);
586
587 return error;
588 }
589
590 static int
591 agr_addport(struct ifnet *ifp, struct ifnet *ifp_port)
592 {
593 const struct ifaddr *ifa;
594 struct agr_softc *sc = ifp->if_softc;
595 struct agr_port *port = NULL;
596 int error = 0;
597 int s;
598
599 if (ifp_port->if_ioctl == NULL) {
600 error = EOPNOTSUPP;
601 goto out;
602 }
603
604 if (ifp_port->if_agrprivate) {
605 error = EBUSY;
606 goto out;
607 }
608
609 if (ifp_port->if_start == agr_start) {
610 error = EINVAL;
611 goto out;
612 }
613
614 port = malloc(sizeof(*port) + ifp_port->if_addrlen, M_DEVBUF,
615 M_WAITOK | M_ZERO);
616 if (port == NULL) {
617 error = ENOMEM;
618 goto out;
619 }
620 port->port_flags = AGRPORT_LARVAL;
621
622 s = pserialize_read_enter();
623 IFADDR_READER_FOREACH(ifa, ifp_port) {
624 if (ifa->ifa_addr->sa_family != AF_LINK) {
625 pserialize_read_exit(s);
626 error = EBUSY;
627 goto out;
628 }
629 }
630 pserialize_read_exit(s);
631
632 if (sc->sc_nports == 0) {
633 switch (ifp_port->if_type) {
634 case IFT_ETHER:
635 sc->sc_iftop = &agrether_ops;
636 break;
637
638 default:
639 error = EPROTONOSUPPORT; /* XXX */
640 goto out;
641 }
642
643 error = (*sc->sc_iftop->iftop_ctor)(sc, ifp_port);
644 if (error)
645 goto out;
646 agrtimer_start(sc);
647 } else {
648 if (ifp->if_type != ifp_port->if_type) {
649 error = EINVAL;
650 goto out;
651 }
652 if (ifp->if_addrlen != ifp_port->if_addrlen) {
653 error = EINVAL;
654 goto out;
655 }
656 }
657
658 memcpy(port->port_origlladdr, CLLADDR(ifp_port->if_sadl),
659 ifp_port->if_addrlen);
660
661 /*
662 * start to modify ifp_port.
663 */
664
665 /*
666 * XXX this should probably be SIOCALIFADDR but that doesn't
667 * appear to work (ENOTTY). We want to change the mac address
668 * of each port to that of the first port. No need for arps
669 * since there are no inet addresses assigned to the ports.
670 */
671 error = if_addr_init(ifp_port, ifp->if_dl, true);
672
673 if (error) {
674 printf("%s: if_addr_init error %d\n", __func__, error);
675 goto cleanup;
676 }
677 port->port_flags |= AGRPORT_LADDRCHANGED;
678
679 ifp->if_type = ifp_port->if_type;
680 AGR_LOCK(sc);
681
682 port->port_ifp = ifp_port;
683 ifp_port->if_agrprivate = port;
684 port->port_agrifp = ifp;
685 TAILQ_INSERT_TAIL(&sc->sc_ports, port, port_q);
686 sc->sc_nports++;
687
688 port->port_ioctl = ifp_port->if_ioctl;
689 ifp_port->if_ioctl = agr_ioctl_filter;
690
691 port->port_flags |= AGRPORT_ATTACHED;
692
693 AGR_UNLOCK(sc);
694
695 error = (*sc->sc_iftop->iftop_portinit)(sc, port);
696 if (error) {
697 printf("%s: portinit error %d\n", __func__, error);
698 goto cleanup;
699 }
700
701 ifp->if_flags |= IFF_RUNNING;
702
703 agrport_config_promisc(port, (ifp->if_flags & IFF_PROMISC) != 0);
704 error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, true);
705 if (error) {
706 printf("%s: configmulti error %d\n", __func__, error);
707 goto cleanup;
708 }
709
710 AGR_LOCK(sc);
711 port->port_flags &= ~AGRPORT_LARVAL;
712 AGR_UNLOCK(sc);
713 out:
714 if (error && port) {
715 free(port, M_DEVBUF);
716 }
717 return error;
718
719 cleanup:
720 if (agrport_cleanup(sc, port)) {
721 printf("%s: error on cleanup\n", __func__);
722
723 port = NULL; /* XXX */
724 }
725
726 if (sc->sc_nports == 0) {
727 KASSERT(TAILQ_EMPTY(&sc->sc_ports));
728 agrtimer_stop(sc);
729 (*sc->sc_iftop->iftop_dtor)(sc);
730 sc->sc_iftop = NULL;
731 agr_reset_iftype(ifp);
732 } else {
733 KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
734 }
735
736 goto out;
737 }
738
739 static int
740 agr_remport(struct ifnet *ifp, struct ifnet *ifp_port)
741 {
742 struct agr_softc *sc = ifp->if_softc;
743 struct agr_port *port;
744 int error = 0;
745
746 if (ifp_port->if_agrprivate == NULL) {
747 error = ENOENT;
748 return error;
749 }
750
751 port = ifp_port->if_agrprivate;
752 if (port->port_agrifp != ifp) {
753 error = EINVAL;
754 return error;
755 }
756
757 KASSERT(sc->sc_nports > 0);
758
759 AGR_LOCK(sc);
760 port->port_flags |= AGRPORT_DETACHING;
761 AGR_UNLOCK(sc);
762
763 error = (*sc->sc_iftop->iftop_portfini)(sc, port);
764 if (error) {
765 /* XXX XXX */
766 printf("%s: portfini error %d\n", __func__, error);
767 goto out;
768 }
769
770 error = (*sc->sc_iftop->iftop_configmulti_port)(sc, port, false);
771 if (error) {
772 /* XXX XXX */
773 printf("%s: configmulti_port error %d\n", __func__, error);
774 goto out;
775 }
776
777 error = agrport_cleanup(sc, port);
778 if (error) {
779 /* XXX XXX */
780 printf("%s: agrport_cleanup error %d\n", __func__, error);
781 goto out;
782 }
783
784 free(port, M_DEVBUF);
785
786 out:
787 if (sc->sc_nports == 0) {
788 KASSERT(TAILQ_EMPTY(&sc->sc_ports));
789 agrtimer_stop(sc);
790 (*sc->sc_iftop->iftop_dtor)(sc);
791 sc->sc_iftop = NULL;
792 /* XXX should purge all addresses? */
793 agr_reset_iftype(ifp);
794 } else {
795 KASSERT(!TAILQ_EMPTY(&sc->sc_ports));
796 }
797
798 return error;
799 }
800
801 static int
802 agrport_cleanup(struct agr_softc *sc, struct agr_port *port)
803 {
804 struct ifnet *ifp_port = port->port_ifp;
805 int error;
806 int result = 0;
807
808 error = agrport_config_promisc(port, false);
809 if (error) {
810 printf("%s: config_promisc error %d\n", __func__, error);
811 result = error;
812 }
813
814 if ((port->port_flags & AGRPORT_LADDRCHANGED)) {
815 #if 0
816 memcpy(LLADDR(ifp_port->if_sadl), port->port_origlladdr,
817 ifp_port->if_addrlen);
818 if (ifp_port->if_init != NULL) {
819 error = (*ifp_port->if_init)(ifp_port);
820 }
821 #else
822 union {
823 struct sockaddr sa;
824 struct sockaddr_dl sdl;
825 struct sockaddr_storage ss;
826 } u;
827 struct ifaddr ifa;
828
829 sockaddr_dl_init(&u.sdl, sizeof(u.ss),
830 0, ifp_port->if_type, NULL, 0,
831 port->port_origlladdr, ifp_port->if_addrlen);
832 memset(&ifa, 0, sizeof(ifa));
833 ifa.ifa_addr = &u.sa;
834 error = agrport_ioctl(port, SIOCINITIFADDR, &ifa);
835 #endif
836 if (error) {
837 printf("%s: if_init error %d\n", __func__, error);
838 result = error;
839 } else {
840 port->port_flags &= ~AGRPORT_LADDRCHANGED;
841 }
842 }
843
844 AGR_LOCK(sc);
845 if ((port->port_flags & AGRPORT_ATTACHED)) {
846 ifp_port->if_agrprivate = NULL;
847
848 TAILQ_REMOVE(&sc->sc_ports, port, port_q);
849 sc->sc_nports--;
850
851 KASSERT(ifp_port->if_ioctl == agr_ioctl_filter);
852 ifp_port->if_ioctl = port->port_ioctl;
853
854 port->port_flags &= ~AGRPORT_ATTACHED;
855 }
856 AGR_UNLOCK(sc);
857
858 return result;
859 }
860
861 static int
862 agr_ioctl_multi(struct ifnet *ifp, u_long cmd, struct ifreq *ifr)
863 {
864 struct agr_softc *sc = ifp->if_softc;
865 int error;
866
867 error = (*sc->sc_iftop->iftop_configmulti_ifreq)(sc, ifr,
868 (cmd == SIOCADDMULTI));
869
870 return error;
871 }
872
873 /*
874 * XXX an incomplete hack; can't filter ioctls handled ifioctl().
875 *
876 * the intention here is to prevent operations on underlying interfaces
877 * so that their states are not changed in the way that agr(4) doesn't
878 * expect. cf. the BUGS section in the agr(4) manual page.
879 */
880 static int
881 agr_ioctl_filter(struct ifnet *ifp, u_long cmd, void *arg)
882 {
883 struct agr_port *port = ifp->if_agrprivate;
884 int error;
885
886 KASSERT(port);
887
888 switch (cmd) {
889 case SIOCADDMULTI: /* add m'cast addr */
890 case SIOCAIFADDR: /* add/chg IF alias */
891 case SIOCALIFADDR: /* add IF addr */
892 case SIOCDELMULTI: /* del m'cast addr */
893 case SIOCDIFADDR: /* delete IF addr */
894 case SIOCDIFPHYADDR: /* delete gif addrs */
895 case SIOCDLIFADDR: /* delete IF addr */
896 case SIOCINITIFADDR:
897 case SIOCSDRVSPEC: /* set driver-specific parameters */
898 case SIOCSIFADDR: /* set ifnet address */
899 case SIOCSIFBRDADDR: /* set broadcast addr */
900 case SIOCSIFDSTADDR: /* set p-p address */
901 case SIOCSIFGENERIC: /* generic IF set op */
902 case SIOCSIFMEDIA: /* set net media */
903 case SIOCSIFMETRIC: /* set IF metric */
904 case SIOCSIFMTU: /* set ifnet mtu */
905 case SIOCSIFNETMASK: /* set net addr mask */
906 case SIOCSIFPHYADDR: /* set gif addres */
907 case SIOCSLIFPHYADDR: /* set gif addrs */
908 case SIOCSVH: /* set carp param */
909 error = EBUSY;
910 break;
911 case SIOCSIFCAP: /* XXX */
912 case SIOCSIFFLAGS: /* XXX */
913 default:
914 error = agrport_ioctl(port, cmd, arg);
915 break;
916 }
917 return error;
918 }
919
920 static int
921 agrreq_copyin(const void *ubuf, struct agrreq *ar)
922 {
923 int error;
924
925 error = copyin(ubuf, ar, sizeof(*ar));
926 if (error) {
927 return error;
928 }
929
930 if (ar->ar_version != AGRREQ_VERSION) {
931 return EINVAL;
932 }
933
934 return 0;
935 }
936
937 static int
938 agrreq_copyout(void *ubuf, struct agrreq *ar)
939 {
940 int error;
941
942 KASSERT(ar->ar_version == AGRREQ_VERSION);
943
944 error = copyout(ar, ubuf, sizeof(*ar));
945 if (error) {
946 return error;
947 }
948
949 return 0;
950 }
951
952 /* Make sure that if any interrupt handlers are out of the softc. */
953 static void
954 agr_sync(void)
955 {
956 uint64_t h;
957
958 if (!mp_online)
959 return;
960
961 h = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
962 xc_wait(h);
963 }
964
965 static int
966 agr_pause(struct agr_softc *sc)
967 {
968 int error;
969
970 mutex_enter(&sc->sc_entry_mtx);
971 if ((error = sc->sc_noentry) != 0)
972 goto out;
973
974 sc->sc_noentry = EBUSY;
975
976 while (sc->sc_insc != 0)
977 cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
978
979 if (sc->sc_nports == 0) {
980 sc->sc_noentry = ENXIO;
981 } else {
982 sc->sc_noentry = 0;
983 error = EBUSY;
984 }
985 cv_broadcast(&sc->sc_insc_cv);
986 out:
987 mutex_exit(&sc->sc_entry_mtx);
988 return error;
989 }
990
991 static void
992 agr_evacuate(struct agr_softc *sc)
993 {
994 mutex_enter(&sc->sc_entry_mtx);
995 cv_broadcast(&sc->sc_insc_cv);
996 while (sc->sc_insc != 0 || sc->sc_paused != 0)
997 cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
998 mutex_exit(&sc->sc_entry_mtx);
999
1000 agr_sync();
1001 }
1002
1003 static int
1004 agr_enter(struct agr_softc *sc)
1005 {
1006 int error;
1007
1008 mutex_enter(&sc->sc_entry_mtx);
1009 sc->sc_paused++;
1010 while ((error = sc->sc_noentry) == EBUSY)
1011 cv_wait(&sc->sc_insc_cv, &sc->sc_entry_mtx);
1012 sc->sc_paused--;
1013 if (error == 0)
1014 sc->sc_insc++;
1015 mutex_exit(&sc->sc_entry_mtx);
1016
1017 return error;
1018 }
1019
1020 static void
1021 agr_exit(struct agr_softc *sc)
1022 {
1023 mutex_enter(&sc->sc_entry_mtx);
1024 if (--sc->sc_insc == 0)
1025 cv_signal(&sc->sc_insc_cv);
1026 mutex_exit(&sc->sc_entry_mtx);
1027 }
1028
1029 static bool
1030 agr_ports_enter(struct agr_softc *sc)
1031 {
1032 mutex_enter(&sc->sc_entry_mtx);
1033 while (sc->sc_wrports)
1034 cv_wait(&sc->sc_ports_cv, &sc->sc_entry_mtx);
1035 sc->sc_rdports++;
1036 mutex_exit(&sc->sc_entry_mtx);
1037
1038 return true;
1039 }
1040
1041 static void
1042 agr_ports_exit(struct agr_softc *sc)
1043 {
1044 mutex_enter(&sc->sc_entry_mtx);
1045 if (--sc->sc_rdports == 0)
1046 cv_signal(&sc->sc_ports_cv);
1047 mutex_exit(&sc->sc_entry_mtx);
1048 }
1049
1050 static void
1051 agr_ports_lock(struct agr_softc *sc)
1052 {
1053 mutex_enter(&sc->sc_entry_mtx);
1054 while (sc->sc_rdports != 0)
1055 cv_wait(&sc->sc_ports_cv, &sc->sc_entry_mtx);
1056 sc->sc_wrports = true;
1057 mutex_exit(&sc->sc_entry_mtx);
1058 }
1059
1060 static void
1061 agr_ports_unlock(struct agr_softc *sc)
1062 {
1063 mutex_enter(&sc->sc_entry_mtx);
1064 sc->sc_wrports = false;
1065 cv_signal(&sc->sc_ports_cv);
1066 mutex_exit(&sc->sc_entry_mtx);
1067 }
1068
1069 static int
1070 agr_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1071 {
1072 struct agr_softc *sc = ifp->if_softc;
1073 struct ifreq *ifr = (struct ifreq *)data;
1074 struct ifaddr *ifa = (struct ifaddr *)data;
1075 struct agrreq ar;
1076 int error;
1077 bool in_ports = false;
1078 int s;
1079
1080 if ((error = agr_enter(sc)) != 0)
1081 return error;
1082
1083 s = splnet();
1084
1085 switch (cmd) {
1086 case SIOCINITIFADDR:
1087 in_ports = agr_ports_enter(sc);
1088 if (sc->sc_nports == 0) {
1089 error = EINVAL;
1090 break;
1091 }
1092 ifp->if_flags |= IFF_UP;
1093 switch (ifa->ifa_addr->sa_family) {
1094 #if defined(INET)
1095 case AF_INET:
1096 arp_ifinit(ifp, ifa);
1097 break;
1098 #endif
1099 default:
1100 break;
1101 }
1102 break;
1103
1104 #if 0 /* notyet */
1105 case SIOCSIFMTU:
1106 #endif
1107
1108 case SIOCSIFFLAGS:
1109 /*
1110 * Check for a change in vlan status. This ioctl is the
1111 * only way we can tell that a vlan has attached or detached.
1112 * Note the agr interface must be up.
1113 */
1114 agr_vlan_check(ifp, sc);
1115
1116 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1117 break;
1118 agr_config_promisc(sc);
1119 break;
1120
1121 case SIOCSETAGR:
1122 splx(s);
1123 error = kauth_authorize_network(kauth_cred_get(),
1124 KAUTH_NETWORK_INTERFACE,
1125 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1126 NULL);
1127 if (!error) {
1128 error = agrreq_copyin(ifr->ifr_data, &ar);
1129 }
1130 if (!error) {
1131 error = agr_setconfig(sc, &ar);
1132 }
1133 s = splnet();
1134 break;
1135
1136 case SIOCGETAGR:
1137 splx(s);
1138 error = agrreq_copyin(ifr->ifr_data, &ar);
1139 if (!error) {
1140 error = agr_getconfig(sc, &ar);
1141 }
1142 if (!error) {
1143 error = agrreq_copyout(ifr->ifr_data, &ar);
1144 }
1145 s = splnet();
1146 break;
1147
1148 case SIOCADDMULTI:
1149 case SIOCDELMULTI:
1150 in_ports = agr_ports_enter(sc);
1151 if (sc->sc_nports == 0)
1152 error = EINVAL;
1153 else
1154 error = agr_ioctl_multi(ifp, cmd, ifr);
1155 break;
1156
1157 default:
1158 error = ifioctl_common(ifp, cmd, data);
1159 break;
1160 }
1161
1162 if (in_ports)
1163 agr_ports_exit(sc);
1164
1165 splx(s);
1166
1167 agr_exit(sc);
1168
1169 return error;
1170 }
1171
1172 static int
1173 agr_config_promisc(struct agr_softc *sc)
1174 {
1175 int error;
1176
1177 agr_port_foreach(sc, agrport_config_promisc_callback, &error);
1178
1179 return error;
1180 }
1181
1182 static int
1183 agrport_config_promisc_callback(struct agr_port *port, void *arg)
1184 {
1185 struct agr_softc *sc = AGR_SC_FROM_PORT(port);
1186 int *errorp = arg;
1187 int error;
1188 bool promisc;
1189
1190 promisc = (sc->sc_if.if_flags & IFF_PROMISC) != 0;
1191
1192 error = agrport_config_promisc(port, promisc);
1193 if (error) {
1194 *errorp = error;
1195 }
1196
1197 return 0;
1198 }
1199
1200 static int
1201 agrport_config_promisc(struct agr_port *port, bool promisc)
1202 {
1203 int error;
1204
1205 if (( promisc && (port->port_flags & AGRPORT_PROMISC) != 0) ||
1206 (!promisc && (port->port_flags & AGRPORT_PROMISC) == 0)) {
1207 return 0;
1208 }
1209
1210 error = ifpromisc(port->port_ifp, promisc);
1211 if (error == 0) {
1212 if (promisc) {
1213 port->port_flags |= AGRPORT_PROMISC;
1214 } else {
1215 port->port_flags &= ~AGRPORT_PROMISC;
1216 }
1217 }
1218
1219 return error;
1220 }
1221
1222 /*
1223 * Module infrastructure
1224 */
1225 #include <net/if_module.h>
1226
1227 IF_MODULE(MODULE_CLASS_DRIVER, agr, "")
1228