2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/domain.h>
59 #include <sys/vimage.h>
60 #include <machine/stdarg.h>
63 #include <net/if_arp.h>
64 #include <net/if_clone.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_var.h>
68 #include <net/radix.h>
69 #include <net/route.h>
71 #if defined(INET) || defined(INET6)
73 #include <netinet/in.h>
74 #include <netinet/in_var.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet6/in6_ifattach.h>
81 #include <netinet/if_ether.h>
84 #include <netinet/ip_carp.h>
87 #include <security/mac/mac_framework.h>
89 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
90 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
92 /* Log link state change events */
93 static int log_link_state_change = 1;
95 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
96 &log_link_state_change, 0,
97 "log interface link state change events");
99 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
100 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
101 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
103 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
106 * XXX: Style; these should be sorted alphabetically, and unprototyped
107 * static functions should be prototyped. Currently they are sorted by
110 static void if_attachdomain(void *);
111 static void if_attachdomain1(struct ifnet *);
112 static int ifconf(u_long, caddr_t);
113 static void if_freemulti(struct ifmultiaddr *);
114 static void if_grow(void);
115 static void if_init(void *);
116 static void if_qflush(struct ifaltq *);
117 static void if_route(struct ifnet *, int flag, int fam);
118 static int if_setflag(struct ifnet *, int, int, int *, int);
119 static void if_slowtimo(void *);
120 static void if_unroute(struct ifnet *, int flag, int fam);
121 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
122 static int if_rtdel(struct radix_node *, void *);
123 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
124 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
125 static void if_start_deferred(void *context, int pending);
126 static void do_link_state_change(void *, int);
127 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
128 static int if_getgroupmembers(struct ifgroupreq *);
131 * XXX: declare here to avoid to include many inet6 related files..
132 * should be more generalized?
134 extern void nd6_setmtu(struct ifnet *);
137 #ifdef VIMAGE_GLOBALS
138 struct ifnethead ifnet; /* depend on static init XXX */
139 struct ifgrouphead ifg_head;
141 static int if_indexlim;
142 /* Table of ifnet/cdev by index. Locked with ifnet_lock. */
143 static struct ifindex_entry *ifindex_table;
144 static struct knlist ifklist;
147 int ifqmaxlen = IFQ_MAXLEN;
148 struct mtx ifnet_lock;
149 static if_com_alloc_t *if_com_alloc[256];
150 static if_com_free_t *if_com_free[256];
152 static void filt_netdetach(struct knote *kn);
153 static int filt_netdev(struct knote *kn, long hint);
155 static struct filterops netdev_filtops =
156 { 1, NULL, filt_netdetach, filt_netdev };
159 * System initialization
161 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
162 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL);
164 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
165 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
166 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
169 ifnet_byindex(u_short idx)
171 INIT_VNET_NET(curvnet);
175 ifp = V_ifindex_table[idx].ife_ifnet;
181 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
183 INIT_VNET_NET(curvnet);
185 IFNET_WLOCK_ASSERT();
187 V_ifindex_table[idx].ife_ifnet = ifp;
191 ifaddr_byindex(u_short idx)
193 INIT_VNET_NET(curvnet);
197 ifa = ifnet_byindex(idx)->if_addr;
203 ifdev_byindex(u_short idx)
205 INIT_VNET_NET(curvnet);
209 cdev = V_ifindex_table[idx].ife_dev;
215 ifdev_setbyindex(u_short idx, struct cdev *cdev)
217 INIT_VNET_NET(curvnet);
220 V_ifindex_table[idx].ife_dev = cdev;
224 static d_open_t netopen;
225 static d_close_t netclose;
226 static d_ioctl_t netioctl;
227 static d_kqfilter_t netkqfilter;
229 static struct cdevsw net_cdevsw = {
230 .d_version = D_VERSION,
231 .d_flags = D_NEEDGIANT,
236 .d_kqfilter = netkqfilter,
240 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
246 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
252 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
257 /* only support interface specific ioctls */
258 if (IOCGROUP(cmd) != 'i')
263 * special network device, not interface.
265 if (cmd == SIOCGIFCONF)
266 return (ifconf(cmd, data)); /* XXX remove cmd */
268 if (cmd == SIOCGIFCONF32)
269 return (ifconf(cmd, data)); /* XXX remove cmd */
274 ifp = ifnet_byindex(idx);
278 error = ifhwioctl(cmd, ifp, data, td);
279 if (error == ENOIOCTL)
285 netkqfilter(struct cdev *dev, struct knote *kn)
287 INIT_VNET_NET(curvnet);
288 struct knlist *klist;
292 switch (kn->kn_filter) {
294 kn->kn_fop = &netdev_filtops;
304 ifp = ifnet_byindex(idx);
307 klist = &ifp->if_klist;
310 kn->kn_hook = (caddr_t)klist;
312 knlist_add(klist, kn, 0);
318 filt_netdetach(struct knote *kn)
320 struct knlist *klist = (struct knlist *)kn->kn_hook;
322 knlist_remove(klist, kn, 0);
326 filt_netdev(struct knote *kn, long hint)
328 struct knlist *klist = (struct knlist *)kn->kn_hook;
331 * Currently NOTE_EXIT is abused to indicate device detach.
333 if (hint == NOTE_EXIT) {
334 kn->kn_data = NOTE_LINKINV;
335 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
336 knlist_remove_inevent(klist, kn);
340 kn->kn_data = hint; /* current status */
341 if (kn->kn_sfflags & hint)
342 kn->kn_fflags |= hint;
343 return (kn->kn_fflags != 0);
347 * Network interface utility routines.
349 * Routines with ifa_ifwith* names take sockaddr *'s as
355 if_init(void *dummy __unused)
357 INIT_VNET_NET(curvnet);
360 V_ifindex_table = NULL;
364 TAILQ_INIT(&V_ifnet);
365 TAILQ_INIT(&V_ifg_head);
366 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL);
367 if_grow(); /* create initial table */
368 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL,
376 INIT_VNET_NET(curvnet);
378 struct ifindex_entry *e;
381 n = V_if_indexlim * sizeof(*e);
382 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
383 if (V_ifindex_table != NULL) {
384 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
385 free((caddr_t)V_ifindex_table, M_IFNET);
391 * Allocate a struct ifnet and an index for an interface. A layer 2
392 * common structure will also be allocated if an allocation routine is
393 * registered for the passed type.
396 if_alloc(u_char type)
398 INIT_VNET_NET(curvnet);
401 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
404 * Try to find an empty slot below if_index. If we fail, take
407 * XXX: should be locked!
409 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
410 if (ifnet_byindex(ifp->if_index) == NULL)
413 /* Catch if_index overflow. */
414 if (ifp->if_index < 1) {
418 if (ifp->if_index > V_if_index)
419 V_if_index = ifp->if_index;
420 if (V_if_index >= V_if_indexlim)
425 if (if_com_alloc[type] != NULL) {
426 ifp->if_l2com = if_com_alloc[type](type, ifp);
427 if (ifp->if_l2com == NULL) {
433 ifnet_setbyindex(ifp->if_index, ifp);
435 IF_ADDR_LOCK_INIT(ifp);
441 * Free the struct ifnet, the associated index, and the layer 2 common
442 * structure if needed. All the work is done in if_free_type().
444 * Do not add code to this function! Add it to if_free_type().
447 if_free(struct ifnet *ifp)
450 if_free_type(ifp, ifp->if_type);
454 * Do the actual work of freeing a struct ifnet, associated index, and
455 * layer 2 common structure. This version should only be called by
456 * intefaces that switch their type after calling if_alloc().
459 if_free_type(struct ifnet *ifp, u_char type)
461 INIT_VNET_NET(curvnet); /* ifp->if_vnet can be NULL here ! */
463 if (ifp != ifnet_byindex(ifp->if_index)) {
464 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
470 ifnet_setbyindex(ifp->if_index, NULL);
472 /* XXX: should be locked with if_findindex() */
473 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL)
477 if (if_com_free[type] != NULL)
478 if_com_free[type](ifp->if_l2com, type);
480 IF_ADDR_LOCK_DESTROY(ifp);
485 * Perform generic interface initalization tasks and attach the interface
486 * to the list of "active" interfaces.
489 * - The decision to return void and thus require this function to
490 * succeed is questionable.
491 * - We do more initialization here then is probably a good idea.
492 * Some of this should probably move to if_alloc().
493 * - We should probably do more sanity checking. For instance we don't
494 * do anything to insure if_xname is unique or non-empty.
497 if_attach(struct ifnet *ifp)
499 INIT_VNET_NET(curvnet);
500 unsigned socksize, ifasize;
501 int namelen, masklen;
502 struct sockaddr_dl *sdl;
505 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
506 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
509 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
510 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
511 IF_AFDATA_LOCK_INIT(ifp);
512 ifp->if_afdata_initialized = 0;
514 TAILQ_INIT(&ifp->if_addrhead);
515 TAILQ_INIT(&ifp->if_prefixhead);
516 TAILQ_INIT(&ifp->if_multiaddrs);
517 TAILQ_INIT(&ifp->if_groups);
519 if_addgroup(ifp, IFG_ALL);
521 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
522 getmicrotime(&ifp->if_lastchange);
523 ifp->if_data.ifi_epoch = time_uptime;
524 ifp->if_data.ifi_datalen = sizeof(struct if_data);
528 mac_ifnet_create(ifp);
531 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
532 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s",
533 net_cdevsw.d_name, ifp->if_xname));
534 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
535 net_cdevsw.d_name, ifp->if_index);
537 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
540 * create a Link Level name for this device
542 namelen = strlen(ifp->if_xname);
544 * Always save enough space for any possiable name so we can do
545 * a rename in place later.
547 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
548 socksize = masklen + ifp->if_addrlen;
549 if (socksize < sizeof(*sdl))
550 socksize = sizeof(*sdl);
551 socksize = roundup2(socksize, sizeof(long));
552 ifasize = sizeof(*ifa) + 2 * socksize;
553 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
555 sdl = (struct sockaddr_dl *)(ifa + 1);
556 sdl->sdl_len = socksize;
557 sdl->sdl_family = AF_LINK;
558 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
559 sdl->sdl_nlen = namelen;
560 sdl->sdl_index = ifp->if_index;
561 sdl->sdl_type = ifp->if_type;
564 ifa->ifa_rtrequest = link_rtrequest;
565 ifa->ifa_addr = (struct sockaddr *)sdl;
566 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
567 ifa->ifa_netmask = (struct sockaddr *)sdl;
568 sdl->sdl_len = masklen;
570 sdl->sdl_data[--namelen] = 0xff;
572 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
573 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
576 * XXX: why do we warn about this? We're correcting it and most
577 * drivers just set the value the way we do.
579 if (ifp->if_snd.ifq_maxlen == 0) {
580 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
581 ifp->if_snd.ifq_maxlen = ifqmaxlen;
583 ifp->if_snd.altq_type = 0;
584 ifp->if_snd.altq_disc = NULL;
585 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
586 ifp->if_snd.altq_tbr = NULL;
587 ifp->if_snd.altq_ifp = ifp;
590 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
593 if (domain_init_status >= 2)
594 if_attachdomain1(ifp);
596 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
597 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
599 /* Announce the interface. */
600 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
602 if (ifp->if_watchdog != NULL)
604 "WARNING: using obsoleted if_watchdog interface\n");
605 if (ifp->if_flags & IFF_NEEDSGIANT)
607 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n");
611 if_attachdomain(void *dummy)
613 INIT_VNET_NET(curvnet);
618 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
619 if_attachdomain1(ifp);
622 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
623 if_attachdomain, NULL);
626 if_attachdomain1(struct ifnet *ifp)
634 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
635 * cannot lock ifp->if_afdata initialization, entirely.
637 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
641 if (ifp->if_afdata_initialized >= domain_init_status) {
642 IF_AFDATA_UNLOCK(ifp);
644 printf("if_attachdomain called more than once on %s\n",
648 ifp->if_afdata_initialized = domain_init_status;
649 IF_AFDATA_UNLOCK(ifp);
651 /* address family dependent data region */
652 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
653 for (dp = domains; dp; dp = dp->dom_next) {
654 if (dp->dom_ifattach)
655 ifp->if_afdata[dp->dom_family] =
656 (*dp->dom_ifattach)(ifp);
663 * Remove any unicast or broadcast network addresses from an interface.
666 if_purgeaddrs(struct ifnet *ifp)
668 struct ifaddr *ifa, *next;
670 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
671 if (ifa->ifa_addr->sa_family == AF_LINK)
674 /* XXX: Ugly!! ad hoc just for INET */
675 if (ifa->ifa_addr->sa_family == AF_INET) {
676 struct ifaliasreq ifr;
678 bzero(&ifr, sizeof(ifr));
679 ifr.ifra_addr = *ifa->ifa_addr;
680 if (ifa->ifa_dstaddr)
681 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
682 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
688 if (ifa->ifa_addr->sa_family == AF_INET6) {
690 /* ifp_addrhead is already updated */
694 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
700 * Remove any multicast network addresses from an interface.
703 if_purgemaddrs(struct ifnet *ifp)
705 struct ifmultiaddr *ifma;
706 struct ifmultiaddr *next;
709 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
710 if_delmulti_locked(ifp, ifma, 1);
715 * Detach an interface, removing it from the
716 * list of "active" interfaces.
718 * XXXRW: There are some significant questions about event ordering, and
719 * how to prevent things from starting to use the interface during detach.
722 if_detach(struct ifnet *ifp)
724 INIT_VNET_NET(ifp->if_vnet);
726 struct radix_node_head *rnh;
734 TAILQ_FOREACH(iter, &V_ifnet, if_link)
736 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
745 * Remove/wait for pending events.
747 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
750 * Remove routes and flush queues.
755 if (ALTQ_IS_ENABLED(&ifp->if_snd))
756 altq_disable(&ifp->if_snd);
757 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
758 altq_detach(&ifp->if_snd);
769 * Remove all IPv6 kernel structs related to ifp. This should be done
770 * before removing routing entries below, since IPv6 interface direct
771 * routes are expected to be removed by the IPv6-specific kernel API.
772 * Otherwise, the kernel will detect some inconsistency and bark it.
779 * Remove link ifaddr pointer and maybe decrement if_index.
780 * Clean up all addresses.
783 destroy_dev(ifdev_byindex(ifp->if_index));
784 ifdev_setbyindex(ifp->if_index, NULL);
786 /* We can now free link ifaddr. */
787 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
788 ifa = TAILQ_FIRST(&ifp->if_addrhead);
789 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
794 * Delete all remaining routes using this interface
795 * Unfortuneatly the only way to do this is to slog through
796 * the entire routing table looking for routes which point
797 * to this interface...oh well...
799 for (i = 1; i <= AF_MAX; i++) {
801 for (j = 0; j < rt_numfibs; j++) {
802 if ((rnh = V_rt_tables[j][i]) == NULL)
804 RADIX_NODE_HEAD_LOCK(rnh);
805 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
806 RADIX_NODE_HEAD_UNLOCK(rnh);
810 /* Announce that the interface is gone. */
811 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
812 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
813 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
816 for (dp = domains; dp; dp = dp->dom_next) {
817 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
818 (*dp->dom_ifdetach)(ifp,
819 ifp->if_afdata[dp->dom_family]);
821 IF_AFDATA_UNLOCK(ifp);
824 mac_ifnet_destroy(ifp);
826 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
827 knlist_clear(&ifp->if_klist, 0);
828 knlist_destroy(&ifp->if_klist);
829 mtx_destroy(&ifp->if_snd.ifq_mtx);
830 IF_AFDATA_DESTROY(ifp);
835 * Add a group to an interface
838 if_addgroup(struct ifnet *ifp, const char *groupname)
840 INIT_VNET_NET(ifp->if_vnet);
841 struct ifg_list *ifgl;
842 struct ifg_group *ifg = NULL;
843 struct ifg_member *ifgm;
845 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
846 groupname[strlen(groupname) - 1] <= '9')
850 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
851 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
856 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
857 M_NOWAIT)) == NULL) {
862 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
863 M_TEMP, M_NOWAIT)) == NULL) {
869 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
870 if (!strcmp(ifg->ifg_group, groupname))
874 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
875 M_TEMP, M_NOWAIT)) == NULL) {
881 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
883 TAILQ_INIT(&ifg->ifg_members);
884 EVENTHANDLER_INVOKE(group_attach_event, ifg);
885 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
889 ifgl->ifgl_group = ifg;
890 ifgm->ifgm_ifp = ifp;
893 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
894 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
899 EVENTHANDLER_INVOKE(group_change_event, groupname);
905 * Remove a group from an interface
908 if_delgroup(struct ifnet *ifp, const char *groupname)
910 INIT_VNET_NET(ifp->if_vnet);
911 struct ifg_list *ifgl;
912 struct ifg_member *ifgm;
915 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
916 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
924 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
927 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
928 if (ifgm->ifgm_ifp == ifp)
932 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
936 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
937 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
938 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
939 free(ifgl->ifgl_group, M_TEMP);
945 EVENTHANDLER_INVOKE(group_change_event, groupname);
951 * Stores all groups from an interface in memory pointed
955 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
958 struct ifg_list *ifgl;
959 struct ifg_req ifgrq, *ifgp;
960 struct ifgroupreq *ifgr = data;
962 if (ifgr->ifgr_len == 0) {
964 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
965 ifgr->ifgr_len += sizeof(struct ifg_req);
970 len = ifgr->ifgr_len;
971 ifgp = ifgr->ifgr_groups;
974 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
975 if (len < sizeof(ifgrq)) {
979 bzero(&ifgrq, sizeof ifgrq);
980 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
981 sizeof(ifgrq.ifgrq_group));
982 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
986 len -= sizeof(ifgrq);
995 * Stores all members of a group in memory pointed to by data
998 if_getgroupmembers(struct ifgroupreq *data)
1000 INIT_VNET_NET(curvnet);
1001 struct ifgroupreq *ifgr = data;
1002 struct ifg_group *ifg;
1003 struct ifg_member *ifgm;
1004 struct ifg_req ifgrq, *ifgp;
1008 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1009 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1016 if (ifgr->ifgr_len == 0) {
1017 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1018 ifgr->ifgr_len += sizeof(ifgrq);
1023 len = ifgr->ifgr_len;
1024 ifgp = ifgr->ifgr_groups;
1025 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1026 if (len < sizeof(ifgrq)) {
1030 bzero(&ifgrq, sizeof ifgrq);
1031 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1032 sizeof(ifgrq.ifgrq_member));
1033 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1037 len -= sizeof(ifgrq);
1046 * Delete Routes for a Network Interface
1048 * Called for each routing entry via the rnh->rnh_walktree() call above
1049 * to delete all route entries referencing a detaching network interface.
1052 * rn pointer to node in the routing table
1053 * arg argument passed to rnh->rnh_walktree() - detaching interface
1057 * errno failed - reason indicated
1061 if_rtdel(struct radix_node *rn, void *arg)
1063 struct rtentry *rt = (struct rtentry *)rn;
1064 struct ifnet *ifp = arg;
1067 if (rt->rt_ifp == ifp) {
1070 * Protect (sorta) against walktree recursion problems
1071 * with cloned routes
1073 if ((rt->rt_flags & RTF_UP) == 0)
1076 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1077 rt_mask(rt), rt->rt_flags,
1078 (struct rtentry **) NULL, rt->rt_fibnum);
1080 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1088 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1089 * structs used to represent other address families, it is necessary
1090 * to perform a different comparison.
1093 #define sa_equal(a1, a2) \
1094 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1096 #define sa_dl_equal(a1, a2) \
1097 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1098 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1099 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1100 LLADDR((struct sockaddr_dl *)(a2)), \
1101 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1104 * Locate an interface based on a complete address.
1108 ifa_ifwithaddr(struct sockaddr *addr)
1110 INIT_VNET_NET(curvnet);
1115 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
1116 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1117 if (ifa->ifa_addr->sa_family != addr->sa_family)
1119 if (sa_equal(addr, ifa->ifa_addr))
1121 /* IP6 doesn't have broadcast */
1122 if ((ifp->if_flags & IFF_BROADCAST) &&
1123 ifa->ifa_broadaddr &&
1124 ifa->ifa_broadaddr->sa_len != 0 &&
1125 sa_equal(ifa->ifa_broadaddr, addr))
1135 * Locate an interface based on the broadcast address.
1139 ifa_ifwithbroadaddr(struct sockaddr *addr)
1141 INIT_VNET_NET(curvnet);
1146 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
1147 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1148 if (ifa->ifa_addr->sa_family != addr->sa_family)
1150 if ((ifp->if_flags & IFF_BROADCAST) &&
1151 ifa->ifa_broadaddr &&
1152 ifa->ifa_broadaddr->sa_len != 0 &&
1153 sa_equal(ifa->ifa_broadaddr, addr))
1163 * Locate the point to point interface with a given destination address.
1167 ifa_ifwithdstaddr(struct sockaddr *addr)
1169 INIT_VNET_NET(curvnet);
1174 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1175 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1177 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1178 if (ifa->ifa_addr->sa_family != addr->sa_family)
1180 if (ifa->ifa_dstaddr != NULL &&
1181 sa_equal(addr, ifa->ifa_dstaddr))
1192 * Find an interface on a specific network. If many, choice
1193 * is most specific found.
1196 ifa_ifwithnet(struct sockaddr *addr)
1198 INIT_VNET_NET(curvnet);
1201 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1202 u_int af = addr->sa_family;
1203 char *addr_data = addr->sa_data, *cplim;
1206 * AF_LINK addresses can be looked up directly by their index number,
1207 * so do that if we can.
1209 if (af == AF_LINK) {
1210 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1211 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1212 return (ifaddr_byindex(sdl->sdl_index));
1216 * Scan though each interface, looking for ones that have
1217 * addresses in this address family.
1220 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1221 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1222 char *cp, *cp2, *cp3;
1224 if (ifa->ifa_addr->sa_family != af)
1226 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1228 * This is a bit broken as it doesn't
1229 * take into account that the remote end may
1230 * be a single node in the network we are
1232 * The trouble is that we don't know the
1233 * netmask for the remote end.
1235 if (ifa->ifa_dstaddr != NULL &&
1236 sa_equal(addr, ifa->ifa_dstaddr))
1240 * if we have a special address handler,
1241 * then use it instead of the generic one.
1243 if (ifa->ifa_claim_addr) {
1244 if ((*ifa->ifa_claim_addr)(ifa, addr))
1250 * Scan all the bits in the ifa's address.
1251 * If a bit dissagrees with what we are
1252 * looking for, mask it with the netmask
1253 * to see if it really matters.
1254 * (A byte at a time)
1256 if (ifa->ifa_netmask == 0)
1259 cp2 = ifa->ifa_addr->sa_data;
1260 cp3 = ifa->ifa_netmask->sa_data;
1261 cplim = ifa->ifa_netmask->sa_len
1262 + (char *)ifa->ifa_netmask;
1264 if ((*cp++ ^ *cp2++) & *cp3++)
1265 goto next; /* next address! */
1267 * If the netmask of what we just found
1268 * is more specific than what we had before
1269 * (if we had one) then remember the new one
1270 * before continuing to search
1271 * for an even better one.
1273 if (ifa_maybe == 0 ||
1274 rn_refines((caddr_t)ifa->ifa_netmask,
1275 (caddr_t)ifa_maybe->ifa_netmask))
1287 * Find an interface address specific to an interface best matching
1291 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1294 char *cp, *cp2, *cp3;
1296 struct ifaddr *ifa_maybe = 0;
1297 u_int af = addr->sa_family;
1301 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1302 if (ifa->ifa_addr->sa_family != af)
1306 if (ifa->ifa_netmask == 0) {
1307 if (sa_equal(addr, ifa->ifa_addr) ||
1308 (ifa->ifa_dstaddr &&
1309 sa_equal(addr, ifa->ifa_dstaddr)))
1313 if (ifp->if_flags & IFF_POINTOPOINT) {
1314 if (sa_equal(addr, ifa->ifa_dstaddr))
1318 cp2 = ifa->ifa_addr->sa_data;
1319 cp3 = ifa->ifa_netmask->sa_data;
1320 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1321 for (; cp3 < cplim; cp3++)
1322 if ((*cp++ ^ *cp2++) & *cp3)
1333 #include <net/route.h>
1336 * Default action when installing a route with a Link Level gateway.
1337 * Lookup an appropriate real ifa to point to.
1338 * This should be moved to /sys/net/link.c eventually.
1341 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1343 struct ifaddr *ifa, *oifa;
1344 struct sockaddr *dst;
1349 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1350 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1352 ifa = ifaof_ifpforaddr(dst, ifp);
1354 IFAREF(ifa); /* XXX */
1358 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1359 ifa->ifa_rtrequest(cmd, rt, info);
1364 * Mark an interface down and notify protocols of
1366 * NOTE: must be called at splnet or eqivalent.
1369 if_unroute(struct ifnet *ifp, int flag, int fam)
1373 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1375 ifp->if_flags &= ~flag;
1376 getmicrotime(&ifp->if_lastchange);
1377 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1378 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1379 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1380 if_qflush(&ifp->if_snd);
1383 carp_carpdev_state(ifp->if_carp);
1389 * Mark an interface up and notify protocols of
1391 * NOTE: must be called at splnet or eqivalent.
1394 if_route(struct ifnet *ifp, int flag, int fam)
1398 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1400 ifp->if_flags |= flag;
1401 getmicrotime(&ifp->if_lastchange);
1402 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1403 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1404 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1407 carp_carpdev_state(ifp->if_carp);
1415 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1416 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1419 * Handle a change in the interface link state. To avoid LORs
1420 * between driver lock and upper layer locks, as well as possible
1421 * recursions, we post event to taskqueue, and all job
1422 * is done in static do_link_state_change().
1425 if_link_state_change(struct ifnet *ifp, int link_state)
1427 /* Return if state hasn't changed. */
1428 if (ifp->if_link_state == link_state)
1431 ifp->if_link_state = link_state;
1433 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1437 do_link_state_change(void *arg, int pending)
1439 struct ifnet *ifp = (struct ifnet *)arg;
1440 int link_state = ifp->if_link_state;
1442 CURVNET_SET(ifp->if_vnet);
1444 /* Notify that the link state has changed. */
1446 if (link_state == LINK_STATE_UP)
1448 else if (link_state == LINK_STATE_DOWN)
1449 link = NOTE_LINKDOWN;
1451 link = NOTE_LINKINV;
1452 KNOTE_UNLOCKED(&ifp->if_klist, link);
1453 if (ifp->if_vlantrunk != NULL)
1454 (*vlan_link_state_p)(ifp, link);
1456 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1457 IFP2AC(ifp)->ac_netgraph != NULL)
1458 (*ng_ether_link_state_p)(ifp, link_state);
1461 carp_carpdev_state(ifp->if_carp);
1463 if (ifp->if_bridge) {
1464 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1465 (*bstp_linkstate_p)(ifp, link_state);
1468 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1469 (*lagg_linkstate_p)(ifp, link_state);
1472 devctl_notify("IFNET", ifp->if_xname,
1473 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1475 if_printf(ifp, "%d link states coalesced\n", pending);
1476 if (log_link_state_change)
1477 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1478 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1483 * Mark an interface down and notify protocols of
1485 * NOTE: must be called at splnet or eqivalent.
1488 if_down(struct ifnet *ifp)
1491 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1495 * Mark an interface up and notify protocols of
1497 * NOTE: must be called at splnet or eqivalent.
1500 if_up(struct ifnet *ifp)
1503 if_route(ifp, IFF_UP, AF_UNSPEC);
1507 * Flush an interface queue.
1510 if_qflush(struct ifaltq *ifq)
1516 if (ALTQ_IS_ENABLED(ifq))
1520 while ((m = n) != 0) {
1531 * Handle interface watchdog timer routines. Called
1532 * from softclock, we decrement timers (if set) and
1533 * call the appropriate interface routine on expiration.
1535 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1536 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1537 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1540 if_slowtimo(void *arg)
1542 VNET_ITERATOR_DECL(vnet_iter);
1548 VNET_FOREACH(vnet_iter) {
1549 CURVNET_SET(vnet_iter);
1550 INIT_VNET_NET(vnet_iter);
1551 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1552 if (ifp->if_timer == 0 || --ifp->if_timer)
1554 if (ifp->if_watchdog)
1555 (*ifp->if_watchdog)(ifp);
1559 VNET_LIST_RUNLOCK();
1562 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1566 * Map interface name to
1567 * interface structure pointer.
1570 ifunit(const char *name)
1572 INIT_VNET_NET(curvnet);
1576 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1577 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1585 * Hardware specific interface ioctls.
1588 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1593 int new_flags, temp_flags;
1594 size_t namelen, onamelen;
1595 char new_name[IFNAMSIZ];
1597 struct sockaddr_dl *sdl;
1599 ifr = (struct ifreq *)data;
1602 ifr->ifr_index = ifp->if_index;
1606 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1607 ifr->ifr_flags = temp_flags & 0xffff;
1608 ifr->ifr_flagshigh = temp_flags >> 16;
1612 ifr->ifr_reqcap = ifp->if_capabilities;
1613 ifr->ifr_curcap = ifp->if_capenable;
1618 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
1623 ifr->ifr_metric = ifp->if_metric;
1627 ifr->ifr_mtu = ifp->if_mtu;
1631 ifr->ifr_phys = ifp->if_physical;
1635 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1639 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1640 * check, so we don't need special handling here yet.
1642 new_flags = (ifr->ifr_flags & 0xffff) |
1643 (ifr->ifr_flagshigh << 16);
1644 if (ifp->if_flags & IFF_SMART) {
1645 /* Smart drivers twiddle their own routes */
1646 } else if (ifp->if_flags & IFF_UP &&
1647 (new_flags & IFF_UP) == 0) {
1651 } else if (new_flags & IFF_UP &&
1652 (ifp->if_flags & IFF_UP) == 0) {
1657 /* See if permanently promiscuous mode bit is about to flip */
1658 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1659 if (new_flags & IFF_PPROMISC)
1660 ifp->if_flags |= IFF_PROMISC;
1661 else if (ifp->if_pcount == 0)
1662 ifp->if_flags &= ~IFF_PROMISC;
1663 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1665 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1667 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1668 (new_flags &~ IFF_CANTCHANGE);
1669 if (ifp->if_ioctl) {
1671 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1672 IFF_UNLOCKGIANT(ifp);
1674 getmicrotime(&ifp->if_lastchange);
1678 error = priv_check(td, PRIV_NET_SETIFCAP);
1681 if (ifp->if_ioctl == NULL)
1682 return (EOPNOTSUPP);
1683 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1686 error = (*ifp->if_ioctl)(ifp, cmd, data);
1687 IFF_UNLOCKGIANT(ifp);
1689 getmicrotime(&ifp->if_lastchange);
1694 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
1699 error = priv_check(td, PRIV_NET_SETIFNAME);
1702 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1705 if (new_name[0] == '\0')
1707 if (ifunit(new_name) != NULL)
1710 /* Announce the departure of the interface. */
1711 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1712 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1714 log(LOG_INFO, "%s: changing name to '%s'\n",
1715 ifp->if_xname, new_name);
1717 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1720 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1721 namelen = strlen(new_name);
1722 onamelen = sdl->sdl_nlen;
1724 * Move the address if needed. This is safe because we
1725 * allocate space for a name of length IFNAMSIZ when we
1726 * create this in if_attach().
1728 if (namelen != onamelen) {
1729 bcopy(sdl->sdl_data + onamelen,
1730 sdl->sdl_data + namelen, sdl->sdl_alen);
1732 bcopy(new_name, sdl->sdl_data, namelen);
1733 sdl->sdl_nlen = namelen;
1734 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1735 bzero(sdl->sdl_data, onamelen);
1736 while (namelen != 0)
1737 sdl->sdl_data[--namelen] = 0xff;
1740 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1741 /* Announce the return of the interface. */
1742 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1746 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1749 ifp->if_metric = ifr->ifr_metric;
1750 getmicrotime(&ifp->if_lastchange);
1754 error = priv_check(td, PRIV_NET_SETIFPHYS);
1757 if (ifp->if_ioctl == NULL)
1758 return (EOPNOTSUPP);
1760 error = (*ifp->if_ioctl)(ifp, cmd, data);
1761 IFF_UNLOCKGIANT(ifp);
1763 getmicrotime(&ifp->if_lastchange);
1768 u_long oldmtu = ifp->if_mtu;
1770 error = priv_check(td, PRIV_NET_SETIFMTU);
1773 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1775 if (ifp->if_ioctl == NULL)
1776 return (EOPNOTSUPP);
1778 error = (*ifp->if_ioctl)(ifp, cmd, data);
1779 IFF_UNLOCKGIANT(ifp);
1781 getmicrotime(&ifp->if_lastchange);
1785 * If the link MTU changed, do network layer specific procedure.
1787 if (ifp->if_mtu != oldmtu) {
1797 if (cmd == SIOCADDMULTI)
1798 error = priv_check(td, PRIV_NET_ADDMULTI);
1800 error = priv_check(td, PRIV_NET_DELMULTI);
1804 /* Don't allow group membership on non-multicast interfaces. */
1805 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1806 return (EOPNOTSUPP);
1808 /* Don't let users screw up protocols' entries. */
1809 if (ifr->ifr_addr.sa_family != AF_LINK)
1812 if (cmd == SIOCADDMULTI) {
1813 struct ifmultiaddr *ifma;
1816 * Userland is only permitted to join groups once
1817 * via the if_addmulti() KPI, because it cannot hold
1818 * struct ifmultiaddr * between calls. It may also
1819 * lose a race while we check if the membership
1823 ifma = if_findmulti(ifp, &ifr->ifr_addr);
1824 IF_ADDR_UNLOCK(ifp);
1828 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1830 error = if_delmulti(ifp, &ifr->ifr_addr);
1833 getmicrotime(&ifp->if_lastchange);
1836 case SIOCSIFPHYADDR:
1837 case SIOCDIFPHYADDR:
1839 case SIOCSIFPHYADDR_IN6:
1841 case SIOCSLIFPHYADDR:
1843 case SIOCSIFGENERIC:
1844 error = priv_check(td, PRIV_NET_HWIOCTL);
1847 if (ifp->if_ioctl == NULL)
1848 return (EOPNOTSUPP);
1850 error = (*ifp->if_ioctl)(ifp, cmd, data);
1851 IFF_UNLOCKGIANT(ifp);
1853 getmicrotime(&ifp->if_lastchange);
1857 ifs = (struct ifstat *)data;
1858 ifs->ascii[0] = '\0';
1860 case SIOCGIFPSRCADDR:
1861 case SIOCGIFPDSTADDR:
1862 case SIOCGLIFPHYADDR:
1864 case SIOCGIFGENERIC:
1865 if (ifp->if_ioctl == NULL)
1866 return (EOPNOTSUPP);
1868 error = (*ifp->if_ioctl)(ifp, cmd, data);
1869 IFF_UNLOCKGIANT(ifp);
1873 error = priv_check(td, PRIV_NET_SETLLADDR);
1876 error = if_setlladdr(ifp,
1877 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1882 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1884 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1887 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1893 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1899 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1901 error = priv_check(td, PRIV_NET_DELIFGROUP);
1904 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1920 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1933 return (ifconf(cmd, data));
1935 ifr = (struct ifreq *)data;
1940 error = priv_check(td, PRIV_NET_IFCREATE);
1943 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1944 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1946 error = priv_check(td, PRIV_NET_IFDESTROY);
1949 return if_clone_destroy(ifr->ifr_name);
1951 case SIOCIFGCLONERS:
1952 return (if_clone_list((struct if_clonereq *)data));
1954 return (if_getgroupmembers((struct ifgroupreq *)data));
1957 ifp = ifunit(ifr->ifr_name);
1961 error = ifhwioctl(cmd, ifp, data, td);
1962 if (error != ENOIOCTL)
1965 oif_flags = ifp->if_flags;
1966 if (so->so_proto == 0)
1967 return (EOPNOTSUPP);
1969 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1978 case SIOCSIFDSTADDR:
1980 case SIOCSIFBRDADDR:
1981 case SIOCSIFNETMASK:
1982 #if BYTE_ORDER != BIG_ENDIAN
1983 if (ifr->ifr_addr.sa_family == 0 &&
1984 ifr->ifr_addr.sa_len < 16) {
1985 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1986 ifr->ifr_addr.sa_len = 16;
1989 if (ifr->ifr_addr.sa_len == 0)
1990 ifr->ifr_addr.sa_len = 16;
1998 case OSIOCGIFDSTADDR:
1999 cmd = SIOCGIFDSTADDR;
2002 case OSIOCGIFBRDADDR:
2003 cmd = SIOCGIFBRDADDR;
2006 case OSIOCGIFNETMASK:
2007 cmd = SIOCGIFNETMASK;
2009 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2016 case OSIOCGIFDSTADDR:
2017 case OSIOCGIFBRDADDR:
2018 case OSIOCGIFNETMASK:
2019 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2023 #endif /* COMPAT_43 */
2025 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2027 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2028 if (ifp->if_flags & IFF_UP) {
2039 * The code common to handling reference counted flags,
2040 * e.g., in ifpromisc() and if_allmulti().
2041 * The "pflag" argument can specify a permanent mode flag to check,
2042 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2044 * Only to be used on stack-owned flags, not driver-owned flags.
2047 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2051 int oldflags, oldcount;
2053 /* Sanity checks to catch programming errors */
2054 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2055 ("%s: setting driver-owned flag %d", __func__, flag));
2058 KASSERT(*refcount >= 0,
2059 ("%s: increment negative refcount %d for flag %d",
2060 __func__, *refcount, flag));
2062 KASSERT(*refcount > 0,
2063 ("%s: decrement non-positive refcount %d for flag %d",
2064 __func__, *refcount, flag));
2066 /* In case this mode is permanent, just touch refcount */
2067 if (ifp->if_flags & pflag) {
2068 *refcount += onswitch ? 1 : -1;
2072 /* Save ifnet parameters for if_ioctl() may fail */
2073 oldcount = *refcount;
2074 oldflags = ifp->if_flags;
2077 * See if we aren't the only and touching refcount is enough.
2078 * Actually toggle interface flag if we are the first or last.
2083 ifp->if_flags |= flag;
2087 ifp->if_flags &= ~flag;
2090 /* Call down the driver since we've changed interface flags */
2091 if (ifp->if_ioctl == NULL) {
2095 ifr.ifr_flags = ifp->if_flags & 0xffff;
2096 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2098 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2099 IFF_UNLOCKGIANT(ifp);
2102 /* Notify userland that interface flags have changed */
2107 /* Recover after driver error */
2108 *refcount = oldcount;
2109 ifp->if_flags = oldflags;
2114 * Set/clear promiscuous mode on interface ifp based on the truth value
2115 * of pswitch. The calls are reference counted so that only the first
2116 * "on" request actually has an effect, as does the final "off" request.
2117 * Results are undefined if the "off" and "on" requests are not matched.
2120 ifpromisc(struct ifnet *ifp, int pswitch)
2123 int oldflags = ifp->if_flags;
2125 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2126 &ifp->if_pcount, pswitch);
2127 /* If promiscuous mode status has changed, log a message */
2128 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2129 log(LOG_INFO, "%s: promiscuous mode %s\n",
2131 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2136 * Return interface configuration
2137 * of system. List may be used
2138 * in later ioctl's (above) to get
2139 * other information.
2143 ifconf(u_long cmd, caddr_t data)
2145 INIT_VNET_NET(curvnet);
2146 struct ifconf *ifc = (struct ifconf *)data;
2148 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2149 struct ifconf ifc_swab;
2155 int error, full = 0, valid_len, max_len;
2158 if (cmd == SIOCGIFCONF32) {
2159 ifc_swab.ifc_len = ifc32->ifc_len;
2160 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2164 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2165 max_len = MAXPHYS - 1;
2167 /* Prevent hostile input from being able to crash the system */
2168 if (ifc->ifc_len <= 0)
2172 if (ifc->ifc_len <= max_len) {
2173 max_len = ifc->ifc_len;
2176 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2180 IFNET_RLOCK(); /* could sleep XXX */
2181 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2185 * Zero the ifr_name buffer to make sure we don't
2186 * disclose the contents of the stack.
2188 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2190 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2191 >= sizeof(ifr.ifr_name)) {
2194 return (ENAMETOOLONG);
2198 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2199 struct sockaddr *sa = ifa->ifa_addr;
2201 if (jailed(curthread->td_ucred) &&
2202 prison_if(curthread->td_ucred, sa))
2206 if (cmd == OSIOCGIFCONF) {
2207 struct osockaddr *osa =
2208 (struct osockaddr *)&ifr.ifr_addr;
2210 osa->sa_family = sa->sa_family;
2211 sbuf_bcat(sb, &ifr, sizeof(ifr));
2212 max_len += sizeof(ifr);
2215 if (sa->sa_len <= sizeof(*sa)) {
2217 sbuf_bcat(sb, &ifr, sizeof(ifr));
2218 max_len += sizeof(ifr);
2221 offsetof(struct ifreq, ifr_addr));
2222 max_len += offsetof(struct ifreq, ifr_addr);
2223 sbuf_bcat(sb, sa, sa->sa_len);
2224 max_len += sa->sa_len;
2227 if (!sbuf_overflowed(sb))
2228 valid_len = sbuf_len(sb);
2231 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2232 sbuf_bcat(sb, &ifr, sizeof(ifr));
2233 max_len += sizeof(ifr);
2235 if (!sbuf_overflowed(sb))
2236 valid_len = sbuf_len(sb);
2242 * If we didn't allocate enough space (uncommon), try again. If
2243 * we have already allocated as much space as we are allowed,
2244 * return what we've got.
2246 if (valid_len != max_len && !full) {
2251 ifc->ifc_len = valid_len;
2253 if (cmd == SIOCGIFCONF32)
2254 ifc32->ifc_len = valid_len;
2257 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2263 * Just like ifpromisc(), but for all-multicast-reception mode.
2266 if_allmulti(struct ifnet *ifp, int onswitch)
2269 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2272 struct ifmultiaddr *
2273 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2275 struct ifmultiaddr *ifma;
2277 IF_ADDR_LOCK_ASSERT(ifp);
2279 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2280 if (sa->sa_family == AF_LINK) {
2281 if (sa_dl_equal(ifma->ifma_addr, sa))
2284 if (sa_equal(ifma->ifma_addr, sa))
2293 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2294 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2295 * the ifnet multicast address list here, so the caller must do that and
2296 * other setup work (such as notifying the device driver). The reference
2297 * count is initialized to 1.
2299 static struct ifmultiaddr *
2300 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2303 struct ifmultiaddr *ifma;
2304 struct sockaddr *dupsa;
2306 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2311 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2312 if (dupsa == NULL) {
2313 free(ifma, M_IFMADDR);
2316 bcopy(sa, dupsa, sa->sa_len);
2317 ifma->ifma_addr = dupsa;
2319 ifma->ifma_ifp = ifp;
2320 ifma->ifma_refcount = 1;
2321 ifma->ifma_protospec = NULL;
2324 ifma->ifma_lladdr = NULL;
2328 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2329 if (dupsa == NULL) {
2330 free(ifma->ifma_addr, M_IFMADDR);
2331 free(ifma, M_IFMADDR);
2334 bcopy(llsa, dupsa, llsa->sa_len);
2335 ifma->ifma_lladdr = dupsa;
2341 * if_freemulti: free ifmultiaddr structure and possibly attached related
2342 * addresses. The caller is responsible for implementing reference
2343 * counting, notifying the driver, handling routing messages, and releasing
2344 * any dependent link layer state.
2347 if_freemulti(struct ifmultiaddr *ifma)
2350 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2351 ifma->ifma_refcount));
2352 KASSERT(ifma->ifma_protospec == NULL,
2353 ("if_freemulti: protospec not NULL"));
2355 if (ifma->ifma_lladdr != NULL)
2356 free(ifma->ifma_lladdr, M_IFMADDR);
2357 free(ifma->ifma_addr, M_IFMADDR);
2358 free(ifma, M_IFMADDR);
2362 * Register an additional multicast address with a network interface.
2364 * - If the address is already present, bump the reference count on the
2365 * address and return.
2366 * - If the address is not link-layer, look up a link layer address.
2367 * - Allocate address structures for one or both addresses, and attach to the
2368 * multicast address list on the interface. If automatically adding a link
2369 * layer address, the protocol address will own a reference to the link
2370 * layer address, to be freed when it is freed.
2371 * - Notify the network device driver of an addition to the multicast address
2374 * 'sa' points to caller-owned memory with the desired multicast address.
2376 * 'retifma' will be used to return a pointer to the resulting multicast
2377 * address reference, if desired.
2380 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2381 struct ifmultiaddr **retifma)
2383 struct ifmultiaddr *ifma, *ll_ifma;
2384 struct sockaddr *llsa;
2388 * If the address is already present, return a new reference to it;
2389 * otherwise, allocate storage and set up a new address.
2392 ifma = if_findmulti(ifp, sa);
2394 ifma->ifma_refcount++;
2395 if (retifma != NULL)
2397 IF_ADDR_UNLOCK(ifp);
2402 * The address isn't already present; resolve the protocol address
2403 * into a link layer address, and then look that up, bump its
2404 * refcount or allocate an ifma for that also. If 'llsa' was
2405 * returned, we will need to free it later.
2409 if (ifp->if_resolvemulti != NULL) {
2410 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2416 * Allocate the new address. Don't hook it up yet, as we may also
2417 * need to allocate a link layer multicast address.
2419 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2426 * If a link layer address is found, we'll need to see if it's
2427 * already present in the address list, or allocate is as well.
2428 * When this block finishes, the link layer address will be on the
2432 ll_ifma = if_findmulti(ifp, llsa);
2433 if (ll_ifma == NULL) {
2434 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2435 if (ll_ifma == NULL) {
2436 --ifma->ifma_refcount;
2441 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2444 ll_ifma->ifma_refcount++;
2445 ifma->ifma_llifma = ll_ifma;
2449 * We now have a new multicast address, ifma, and possibly a new or
2450 * referenced link layer address. Add the primary address to the
2451 * ifnet address list.
2453 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2455 if (retifma != NULL)
2459 * Must generate the message while holding the lock so that 'ifma'
2460 * pointer is still valid.
2462 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2463 IF_ADDR_UNLOCK(ifp);
2466 * We are certain we have added something, so call down to the
2467 * interface to let them know about it.
2469 if (ifp->if_ioctl != NULL) {
2471 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2472 IFF_UNLOCKGIANT(ifp);
2476 free(llsa, M_IFMADDR);
2482 free(llsa, M_IFMADDR);
2485 IF_ADDR_UNLOCK(ifp);
2490 * Delete a multicast group membership by network-layer group address.
2492 * Returns ENOENT if the entry could not be found. If ifp no longer
2493 * exists, results are undefined. This entry point should only be used
2494 * from subsystems which do appropriate locking to hold ifp for the
2495 * duration of the call.
2496 * Network-layer protocol domains must use if_delmulti_ifma().
2499 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2501 struct ifmultiaddr *ifma;
2505 INIT_VNET_NET(ifp->if_vnet);
2508 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2515 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2522 ifma = if_findmulti(ifp, sa);
2524 lastref = if_delmulti_locked(ifp, ifma, 0);
2525 IF_ADDR_UNLOCK(ifp);
2530 if (lastref && ifp->if_ioctl != NULL) {
2532 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2533 IFF_UNLOCKGIANT(ifp);
2540 * Delete a multicast group membership by group membership pointer.
2541 * Network-layer protocol domains must use this routine.
2543 * It is safe to call this routine if the ifp disappeared. Callers should
2544 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be
2548 if_delmulti_ifma(struct ifmultiaddr *ifma)
2551 INIT_VNET_NET(curvnet);
2556 ifp = ifma->ifma_ifp;
2559 printf("%s: ifma_ifp seems to be detached\n", __func__);
2564 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2568 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2575 * If and only if the ifnet instance exists: Acquire the address lock.
2580 lastref = if_delmulti_locked(ifp, ifma, 0);
2584 * If and only if the ifnet instance exists:
2585 * Release the address lock.
2586 * If the group was left: update the hardware hash filter.
2588 IF_ADDR_UNLOCK(ifp);
2589 if (lastref && ifp->if_ioctl != NULL) {
2591 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2592 IFF_UNLOCKGIANT(ifp);
2598 * Perform deletion of network-layer and/or link-layer multicast address.
2600 * Return 0 if the reference count was decremented.
2601 * Return 1 if the final reference was released, indicating that the
2602 * hardware hash filter should be reprogrammed.
2605 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2607 struct ifmultiaddr *ll_ifma;
2609 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2610 KASSERT(ifma->ifma_ifp == ifp,
2611 ("%s: inconsistent ifp %p", __func__, ifp));
2612 IF_ADDR_LOCK_ASSERT(ifp);
2615 ifp = ifma->ifma_ifp;
2618 * If the ifnet is detaching, null out references to ifnet,
2619 * so that upper protocol layers will notice, and not attempt
2620 * to obtain locks for an ifnet which no longer exists. The
2621 * routing socket announcement must happen before the ifnet
2622 * instance is detached from the system.
2626 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2629 * ifp may already be nulled out if we are being reentered
2630 * to delete the ll_ifma.
2633 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2634 ifma->ifma_ifp = NULL;
2638 if (--ifma->ifma_refcount > 0)
2642 * If this ifma is a network-layer ifma, a link-layer ifma may
2643 * have been associated with it. Release it first if so.
2645 ll_ifma = ifma->ifma_llifma;
2646 if (ll_ifma != NULL) {
2647 KASSERT(ifma->ifma_lladdr != NULL,
2648 ("%s: llifma w/o lladdr", __func__));
2650 ll_ifma->ifma_ifp = NULL; /* XXX */
2651 if (--ll_ifma->ifma_refcount == 0) {
2653 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2656 if_freemulti(ll_ifma);
2661 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2666 * The last reference to this instance of struct ifmultiaddr
2667 * was released; the hardware should be notified of this change.
2673 * Set the link layer address on an interface.
2675 * At this time we only support certain types of interfaces,
2676 * and we don't allow the length of the address to change.
2679 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2681 struct sockaddr_dl *sdl;
2688 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2691 if (len != sdl->sdl_alen) /* don't allow length to change */
2693 switch (ifp->if_type) {
2701 case IFT_IEEE8023ADLAG:
2702 bcopy(lladdr, LLADDR(sdl), len);
2708 * If the interface is already up, we need
2709 * to re-init it in order to reprogram its
2712 if ((ifp->if_flags & IFF_UP) != 0) {
2713 if (ifp->if_ioctl) {
2715 ifp->if_flags &= ~IFF_UP;
2716 ifr.ifr_flags = ifp->if_flags & 0xffff;
2717 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2718 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2719 ifp->if_flags |= IFF_UP;
2720 ifr.ifr_flags = ifp->if_flags & 0xffff;
2721 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2722 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2723 IFF_UNLOCKGIANT(ifp);
2727 * Also send gratuitous ARPs to notify other nodes about
2728 * the address change.
2730 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2731 if (ifa->ifa_addr->sa_family == AF_INET)
2732 arp_ifinit(ifp, ifa);
2740 * The name argument must be a pointer to storage which will last as
2741 * long as the interface does. For physical devices, the result of
2742 * device_get_name(dev) is a good choice and for pseudo-devices a
2743 * static string works well.
2746 if_initname(struct ifnet *ifp, const char *name, int unit)
2748 ifp->if_dname = name;
2749 ifp->if_dunit = unit;
2750 if (unit != IF_DUNIT_NONE)
2751 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2753 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2757 if_printf(struct ifnet *ifp, const char * fmt, ...)
2762 retval = printf("%s: ", ifp->if_xname);
2764 retval += vprintf(fmt, ap);
2770 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2771 * be called without Giant. However, we often can't acquire the Giant lock
2772 * at those points; instead, we run it via a task queue that holds Giant via
2773 * if_start_deferred.
2775 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2776 * outstanding if_start_deferred() tasks that will run after the free. This
2777 * probably means waiting in if_detach().
2780 if_start(struct ifnet *ifp)
2783 if (ifp->if_flags & IFF_NEEDSGIANT) {
2784 if (mtx_owned(&Giant))
2785 (*(ifp)->if_start)(ifp);
2787 taskqueue_enqueue(taskqueue_swi_giant,
2788 &ifp->if_starttask);
2790 (*(ifp)->if_start)(ifp);
2794 if_start_deferred(void *context, int pending)
2801 (ifp->if_start)(ifp);
2805 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2810 if (_IF_QFULL(ifq)) {
2817 ifp->if_obytes += m->m_pkthdr.len + adjust;
2818 if (m->m_flags & (M_BCAST|M_MCAST))
2820 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2822 _IF_ENQUEUE(ifq, m);
2824 if (ifp != NULL && !active)
2830 if_register_com_alloc(u_char type,
2831 if_com_alloc_t *a, if_com_free_t *f)
2834 KASSERT(if_com_alloc[type] == NULL,
2835 ("if_register_com_alloc: %d already registered", type));
2836 KASSERT(if_com_free[type] == NULL,
2837 ("if_register_com_alloc: %d free already registered", type));
2839 if_com_alloc[type] = a;
2840 if_com_free[type] = f;
2844 if_deregister_com_alloc(u_char type)
2847 KASSERT(if_com_alloc[type] != NULL,
2848 ("if_deregister_com_alloc: %d not registered", type));
2849 KASSERT(if_com_free[type] != NULL,
2850 ("if_deregister_com_alloc: %d free not registered", type));
2851 if_com_alloc[type] = NULL;
2852 if_com_free[type] = NULL;