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 <machine/stdarg.h>
62 #include <net/if_clone.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_var.h>
66 #include <net/radix.h>
67 #include <net/route.h>
69 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
74 #include <netinet6/in6_var.h>
75 #include <netinet6/in6_ifattach.h>
79 #include <netinet/if_ether.h>
82 #include <netinet/ip_carp.h>
85 #include <security/mac/mac_framework.h>
87 static int slowtimo_started;
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 void if_purgemaddrs(struct ifnet *);
113 static int ifconf(u_long, caddr_t);
114 static void if_freemulti(struct ifmultiaddr *);
115 static void if_grow(void);
116 static void if_init(void *);
117 static void if_check(void *);
118 static void if_qflush(struct ifaltq *);
119 static void if_route(struct ifnet *, int flag, int fam);
120 static int if_setflag(struct ifnet *, int, int, int *, int);
121 static void if_slowtimo(void *);
122 static void if_unroute(struct ifnet *, int flag, int fam);
123 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
124 static int if_rtdel(struct radix_node *, void *);
125 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
126 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
127 static void if_start_deferred(void *context, int pending);
128 static void do_link_state_change(void *, int);
129 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
130 static int if_getgroupmembers(struct ifgroupreq *);
131 static void if_delgroups(struct ifnet *);
134 * XXX: declare here to avoid to include many inet6 related files..
135 * should be more generalized?
137 extern void nd6_setmtu(struct ifnet *);
141 int ifqmaxlen = IFQ_MAXLEN;
142 struct ifnethead ifnet; /* depend on static init XXX */
143 struct ifgrouphead ifg_head;
144 struct mtx ifnet_lock;
145 static if_com_alloc_t *if_com_alloc[256];
146 static if_com_free_t *if_com_free[256];
148 static int if_indexlim = 8;
149 static struct knlist ifklist;
152 * Table of ifnet/cdev by index. Locked with ifnet_lock.
154 static struct ifindex_entry *ifindex_table = NULL;
156 static void filt_netdetach(struct knote *kn);
157 static int filt_netdev(struct knote *kn, long hint);
159 static struct filterops netdev_filtops =
160 { 1, NULL, filt_netdetach, filt_netdev };
163 * System initialization
165 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
166 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
168 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
169 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
170 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
173 ifnet_byindex(u_short idx)
178 ifp = ifindex_table[idx].ife_ifnet;
184 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
187 IFNET_WLOCK_ASSERT();
189 ifindex_table[idx].ife_ifnet = ifp;
193 ifaddr_byindex(u_short idx)
198 ifa = ifnet_byindex(idx)->if_addr;
204 ifdev_byindex(u_short idx)
209 cdev = ifindex_table[idx].ife_dev;
215 ifdev_setbyindex(u_short idx, struct cdev *cdev)
219 ifindex_table[idx].ife_dev = cdev;
223 static d_open_t netopen;
224 static d_close_t netclose;
225 static d_ioctl_t netioctl;
226 static d_kqfilter_t netkqfilter;
228 static struct cdevsw net_cdevsw = {
229 .d_version = D_VERSION,
230 .d_flags = D_NEEDGIANT,
235 .d_kqfilter = netkqfilter,
239 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
245 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
251 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
256 /* only support interface specific ioctls */
257 if (IOCGROUP(cmd) != 'i')
262 * special network device, not interface.
264 if (cmd == SIOCGIFCONF)
265 return (ifconf(cmd, data)); /* XXX remove cmd */
267 if (cmd == SIOCGIFCONF32)
268 return (ifconf(cmd, data)); /* XXX remove cmd */
273 ifp = ifnet_byindex(idx);
277 error = ifhwioctl(cmd, ifp, data, td);
278 if (error == ENOIOCTL)
284 netkqfilter(struct cdev *dev, struct knote *kn)
286 struct knlist *klist;
290 switch (kn->kn_filter) {
292 kn->kn_fop = &netdev_filtops;
302 ifp = ifnet_byindex(idx);
305 klist = &ifp->if_klist;
308 kn->kn_hook = (caddr_t)klist;
310 knlist_add(klist, kn, 0);
316 filt_netdetach(struct knote *kn)
318 struct knlist *klist = (struct knlist *)kn->kn_hook;
320 knlist_remove(klist, kn, 0);
324 filt_netdev(struct knote *kn, long hint)
326 struct knlist *klist = (struct knlist *)kn->kn_hook;
329 * Currently NOTE_EXIT is abused to indicate device detach.
331 if (hint == NOTE_EXIT) {
332 kn->kn_data = NOTE_LINKINV;
333 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
334 knlist_remove_inevent(klist, kn);
338 kn->kn_data = hint; /* current status */
339 if (kn->kn_sfflags & hint)
340 kn->kn_fflags |= hint;
341 return (kn->kn_fflags != 0);
345 * Network interface utility routines.
347 * Routines with ifa_ifwith* names take sockaddr *'s as
353 if_init(void *dummy __unused)
358 TAILQ_INIT(&ifg_head);
359 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
360 if_grow(); /* create initial table */
361 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL,
370 struct ifindex_entry *e;
373 n = if_indexlim * sizeof(*e);
374 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
375 if (ifindex_table != NULL) {
376 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
377 free((caddr_t)ifindex_table, M_IFNET);
384 if_check(void *dummy __unused)
390 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
391 TAILQ_FOREACH(ifp, &ifnet, if_link) {
392 if (ifp->if_snd.ifq_maxlen == 0) {
393 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
394 ifp->if_snd.ifq_maxlen = ifqmaxlen;
396 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
398 "XXX: driver didn't initialize queue mtx\n");
399 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
400 MTX_NETWORK_LOCK, MTX_DEF);
407 * If at least one interface added during boot uses
408 * if_watchdog then start the timer.
410 if (slowtimo_started)
415 * Allocate a struct ifnet and an index for an interface. A layer 2
416 * common structure will also be allocated if an allocation routine is
417 * registered for the passed type.
420 if_alloc(u_char type)
424 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
427 * Try to find an empty slot below if_index. If we fail, take
430 * XXX: should be locked!
432 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
433 if (ifnet_byindex(ifp->if_index) == NULL)
436 /* Catch if_index overflow. */
437 if (ifp->if_index < 1) {
441 if (ifp->if_index > if_index)
442 if_index = ifp->if_index;
443 if (if_index >= if_indexlim)
448 if (if_com_alloc[type] != NULL) {
449 ifp->if_l2com = if_com_alloc[type](type, ifp);
450 if (ifp->if_l2com == NULL) {
456 ifnet_setbyindex(ifp->if_index, ifp);
458 IF_ADDR_LOCK_INIT(ifp);
464 * Free the struct ifnet, the associated index, and the layer 2 common
465 * structure if needed. All the work is done in if_free_type().
467 * Do not add code to this function! Add it to if_free_type().
470 if_free(struct ifnet *ifp)
473 if_free_type(ifp, ifp->if_type);
477 * Do the actual work of freeing a struct ifnet, associated index, and
478 * layer 2 common structure. This version should only be called by
479 * intefaces that switch their type after calling if_alloc().
482 if_free_type(struct ifnet *ifp, u_char type)
485 if (ifp != ifnet_byindex(ifp->if_index)) {
486 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
492 ifnet_setbyindex(ifp->if_index, NULL);
494 /* XXX: should be locked with if_findindex() */
495 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
499 if (if_com_free[type] != NULL)
500 if_com_free[type](ifp->if_l2com, type);
502 IF_ADDR_LOCK_DESTROY(ifp);
507 * Perform generic interface initalization tasks and attach the interface
508 * to the list of "active" interfaces.
511 * - The decision to return void and thus require this function to
512 * succeed is questionable.
513 * - We do more initialization here then is probably a good idea.
514 * Some of this should probably move to if_alloc().
515 * - We should probably do more sanity checking. For instance we don't
516 * do anything to insure if_xname is unique or non-empty.
519 if_attach(struct ifnet *ifp)
521 unsigned socksize, ifasize;
522 int namelen, masklen;
523 struct sockaddr_dl *sdl;
526 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
527 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
530 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
531 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
532 IF_AFDATA_LOCK_INIT(ifp);
533 ifp->if_afdata_initialized = 0;
535 TAILQ_INIT(&ifp->if_addrhead);
536 TAILQ_INIT(&ifp->if_prefixhead);
537 TAILQ_INIT(&ifp->if_multiaddrs);
538 TAILQ_INIT(&ifp->if_groups);
540 if_addgroup(ifp, IFG_ALL);
542 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
543 getmicrotime(&ifp->if_lastchange);
544 ifp->if_data.ifi_epoch = time_uptime;
545 ifp->if_data.ifi_datalen = sizeof(struct if_data);
549 mac_create_ifnet(ifp);
552 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
553 unit2minor(ifp->if_index), UID_ROOT, GID_WHEEL, 0600, "%s/%s",
554 net_cdevsw.d_name, ifp->if_xname));
555 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
556 net_cdevsw.d_name, ifp->if_index);
558 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
561 * create a Link Level name for this device
563 namelen = strlen(ifp->if_xname);
565 * Always save enough space for any possiable name so we can do
566 * a rename in place later.
568 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
569 socksize = masklen + ifp->if_addrlen;
570 if (socksize < sizeof(*sdl))
571 socksize = sizeof(*sdl);
572 socksize = roundup2(socksize, sizeof(long));
573 ifasize = sizeof(*ifa) + 2 * socksize;
574 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
576 sdl = (struct sockaddr_dl *)(ifa + 1);
577 sdl->sdl_len = socksize;
578 sdl->sdl_family = AF_LINK;
579 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
580 sdl->sdl_nlen = namelen;
581 sdl->sdl_index = ifp->if_index;
582 sdl->sdl_type = ifp->if_type;
585 ifa->ifa_rtrequest = link_rtrequest;
586 ifa->ifa_addr = (struct sockaddr *)sdl;
587 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
588 ifa->ifa_netmask = (struct sockaddr *)sdl;
589 sdl->sdl_len = masklen;
591 sdl->sdl_data[--namelen] = 0xff;
593 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
594 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
595 ifp->if_snd.altq_type = 0;
596 ifp->if_snd.altq_disc = NULL;
597 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
598 ifp->if_snd.altq_tbr = NULL;
599 ifp->if_snd.altq_ifp = ifp;
602 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
605 if (domain_init_status >= 2)
606 if_attachdomain1(ifp);
608 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
609 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
611 /* Announce the interface. */
612 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
614 if (ifp->if_watchdog != NULL) {
616 "WARNING: using obsoleted if_watchdog interface\n");
619 * Note that we need if_slowtimo(). If this happens after
620 * boot, then call if_slowtimo() directly.
622 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
625 if (ifp->if_flags & IFF_NEEDSGIANT)
627 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n");
631 if_attachdomain(void *dummy)
637 TAILQ_FOREACH(ifp, &ifnet, if_link)
638 if_attachdomain1(ifp);
641 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
642 if_attachdomain, NULL);
645 if_attachdomain1(struct ifnet *ifp)
653 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
654 * cannot lock ifp->if_afdata initialization, entirely.
656 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
660 if (ifp->if_afdata_initialized >= domain_init_status) {
661 IF_AFDATA_UNLOCK(ifp);
663 printf("if_attachdomain called more than once on %s\n",
667 ifp->if_afdata_initialized = domain_init_status;
668 IF_AFDATA_UNLOCK(ifp);
670 /* address family dependent data region */
671 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
672 for (dp = domains; dp; dp = dp->dom_next) {
673 if (dp->dom_ifattach)
674 ifp->if_afdata[dp->dom_family] =
675 (*dp->dom_ifattach)(ifp);
682 * Remove any unicast or broadcast network addresses from an interface.
685 if_purgeaddrs(struct ifnet *ifp)
687 struct ifaddr *ifa, *next;
689 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
690 if (ifa->ifa_addr->sa_family == AF_LINK)
693 /* XXX: Ugly!! ad hoc just for INET */
694 if (ifa->ifa_addr->sa_family == AF_INET) {
695 struct ifaliasreq ifr;
697 bzero(&ifr, sizeof(ifr));
698 ifr.ifra_addr = *ifa->ifa_addr;
699 if (ifa->ifa_dstaddr)
700 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
701 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
707 if (ifa->ifa_addr->sa_family == AF_INET6) {
709 /* ifp_addrhead is already updated */
713 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
719 * Remove any multicast network addresses from an interface.
722 if_purgemaddrs(struct ifnet *ifp)
724 struct ifmultiaddr *ifma;
725 struct ifmultiaddr *next;
728 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
729 if_delmulti_locked(ifp, ifma, 1);
734 * Detach an interface, removing it from the
735 * list of "active" interfaces.
737 * XXXRW: There are some significant questions about event ordering, and
738 * how to prevent things from starting to use the interface during detach.
741 if_detach(struct ifnet *ifp)
744 struct radix_node_head *rnh;
752 TAILQ_FOREACH(iter, &ifnet, if_link)
754 TAILQ_REMOVE(&ifnet, ifp, if_link);
763 * Remove/wait for pending events.
765 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
768 * Remove routes and flush queues.
773 if (ALTQ_IS_ENABLED(&ifp->if_snd))
774 altq_disable(&ifp->if_snd);
775 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
776 altq_detach(&ifp->if_snd);
787 * Remove all IPv6 kernel structs related to ifp. This should be done
788 * before removing routing entries below, since IPv6 interface direct
789 * routes are expected to be removed by the IPv6-specific kernel API.
790 * Otherwise, the kernel will detect some inconsistency and bark it.
797 * Remove link ifaddr pointer and maybe decrement if_index.
798 * Clean up all addresses.
801 destroy_dev(ifdev_byindex(ifp->if_index));
802 ifdev_setbyindex(ifp->if_index, NULL);
804 /* We can now free link ifaddr. */
805 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
806 ifa = TAILQ_FIRST(&ifp->if_addrhead);
807 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
812 * Delete all remaining routes using this interface
813 * Unfortuneatly the only way to do this is to slog through
814 * the entire routing table looking for routes which point
815 * to this interface...oh well...
817 for (i = 1; i <= AF_MAX; i++) {
819 for (j = 0; j < rt_numfibs; j++) {
820 if ((rnh = rt_tables[j][i]) == NULL)
822 RADIX_NODE_HEAD_LOCK(rnh);
823 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
824 RADIX_NODE_HEAD_UNLOCK(rnh);
828 /* Announce that the interface is gone. */
829 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
830 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
831 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
835 for (dp = domains; dp; dp = dp->dom_next) {
836 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
837 (*dp->dom_ifdetach)(ifp,
838 ifp->if_afdata[dp->dom_family]);
840 IF_AFDATA_UNLOCK(ifp);
843 mac_destroy_ifnet(ifp);
845 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
846 knlist_clear(&ifp->if_klist, 0);
847 knlist_destroy(&ifp->if_klist);
848 mtx_destroy(&ifp->if_snd.ifq_mtx);
849 IF_AFDATA_DESTROY(ifp);
854 * Add a group to an interface
857 if_addgroup(struct ifnet *ifp, const char *groupname)
859 struct ifg_list *ifgl;
860 struct ifg_group *ifg = NULL;
861 struct ifg_member *ifgm;
863 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
864 groupname[strlen(groupname) - 1] <= '9')
868 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
869 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
874 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
875 M_NOWAIT)) == NULL) {
880 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
881 M_TEMP, M_NOWAIT)) == NULL) {
887 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
888 if (!strcmp(ifg->ifg_group, groupname))
892 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
893 M_TEMP, M_NOWAIT)) == NULL) {
899 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
901 TAILQ_INIT(&ifg->ifg_members);
902 EVENTHANDLER_INVOKE(group_attach_event, ifg);
903 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
907 ifgl->ifgl_group = ifg;
908 ifgm->ifgm_ifp = ifp;
911 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
912 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
917 EVENTHANDLER_INVOKE(group_change_event, groupname);
923 * Remove a group from an interface
926 if_delgroup(struct ifnet *ifp, const char *groupname)
928 struct ifg_list *ifgl;
929 struct ifg_member *ifgm;
932 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
933 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
941 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
944 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
945 if (ifgm->ifgm_ifp == ifp)
949 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
953 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
954 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
955 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
956 free(ifgl->ifgl_group, M_TEMP);
962 EVENTHANDLER_INVOKE(group_change_event, groupname);
968 * Remove an interface from all groups
971 if_delgroups(struct ifnet *ifp)
973 struct ifg_list *ifgl;
974 struct ifg_member *ifgm;
975 char groupname[IFNAMSIZ];
978 while (!TAILQ_EMPTY(&ifp->if_groups)) {
979 ifgl = TAILQ_FIRST(&ifp->if_groups);
981 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
984 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
987 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
988 if (ifgm->ifgm_ifp == ifp)
992 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
997 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
998 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
999 EVENTHANDLER_INVOKE(group_detach_event,
1001 free(ifgl->ifgl_group, M_TEMP);
1007 EVENTHANDLER_INVOKE(group_change_event, groupname);
1015 * Stores all groups from an interface in memory pointed
1019 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1022 struct ifg_list *ifgl;
1023 struct ifg_req ifgrq, *ifgp;
1024 struct ifgroupreq *ifgr = data;
1026 if (ifgr->ifgr_len == 0) {
1028 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1029 ifgr->ifgr_len += sizeof(struct ifg_req);
1030 IF_ADDR_UNLOCK(ifp);
1034 len = ifgr->ifgr_len;
1035 ifgp = ifgr->ifgr_groups;
1038 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1039 if (len < sizeof(ifgrq)) {
1040 IF_ADDR_UNLOCK(ifp);
1043 bzero(&ifgrq, sizeof ifgrq);
1044 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1045 sizeof(ifgrq.ifgrq_group));
1046 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1047 IF_ADDR_UNLOCK(ifp);
1050 len -= sizeof(ifgrq);
1053 IF_ADDR_UNLOCK(ifp);
1059 * Stores all members of a group in memory pointed to by data
1062 if_getgroupmembers(struct ifgroupreq *data)
1064 struct ifgroupreq *ifgr = data;
1065 struct ifg_group *ifg;
1066 struct ifg_member *ifgm;
1067 struct ifg_req ifgrq, *ifgp;
1071 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1072 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1079 if (ifgr->ifgr_len == 0) {
1080 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1081 ifgr->ifgr_len += sizeof(ifgrq);
1086 len = ifgr->ifgr_len;
1087 ifgp = ifgr->ifgr_groups;
1088 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1089 if (len < sizeof(ifgrq)) {
1093 bzero(&ifgrq, sizeof ifgrq);
1094 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1095 sizeof(ifgrq.ifgrq_member));
1096 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1100 len -= sizeof(ifgrq);
1109 * Delete Routes for a Network Interface
1111 * Called for each routing entry via the rnh->rnh_walktree() call above
1112 * to delete all route entries referencing a detaching network interface.
1115 * rn pointer to node in the routing table
1116 * arg argument passed to rnh->rnh_walktree() - detaching interface
1120 * errno failed - reason indicated
1124 if_rtdel(struct radix_node *rn, void *arg)
1126 struct rtentry *rt = (struct rtentry *)rn;
1127 struct ifnet *ifp = arg;
1130 if (rt->rt_ifp == ifp) {
1133 * Protect (sorta) against walktree recursion problems
1134 * with cloned routes
1136 if ((rt->rt_flags & RTF_UP) == 0)
1139 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1140 rt_mask(rt), rt->rt_flags,
1141 (struct rtentry **) NULL, rt->rt_fibnum);
1143 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1151 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1152 * structs used to represent other address families, it is necessary
1153 * to perform a different comparison.
1156 #define sa_equal(a1, a2) \
1157 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1159 #define sa_dl_equal(a1, a2) \
1160 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1161 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1162 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1163 LLADDR((struct sockaddr_dl *)(a2)), \
1164 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1167 * Locate an interface based on a complete address.
1171 ifa_ifwithaddr(struct sockaddr *addr)
1177 TAILQ_FOREACH(ifp, &ifnet, if_link)
1178 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1179 if (ifa->ifa_addr->sa_family != addr->sa_family)
1181 if (sa_equal(addr, ifa->ifa_addr))
1183 /* IP6 doesn't have broadcast */
1184 if ((ifp->if_flags & IFF_BROADCAST) &&
1185 ifa->ifa_broadaddr &&
1186 ifa->ifa_broadaddr->sa_len != 0 &&
1187 sa_equal(ifa->ifa_broadaddr, addr))
1197 * Locate an interface based on the broadcast address.
1201 ifa_ifwithbroadaddr(struct sockaddr *addr)
1207 TAILQ_FOREACH(ifp, &ifnet, if_link)
1208 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1209 if (ifa->ifa_addr->sa_family != addr->sa_family)
1211 if ((ifp->if_flags & IFF_BROADCAST) &&
1212 ifa->ifa_broadaddr &&
1213 ifa->ifa_broadaddr->sa_len != 0 &&
1214 sa_equal(ifa->ifa_broadaddr, addr))
1224 * Locate the point to point interface with a given destination address.
1228 ifa_ifwithdstaddr(struct sockaddr *addr)
1234 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1235 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1237 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1238 if (ifa->ifa_addr->sa_family != addr->sa_family)
1240 if (ifa->ifa_dstaddr != NULL &&
1241 sa_equal(addr, ifa->ifa_dstaddr))
1252 * Find an interface on a specific network. If many, choice
1253 * is most specific found.
1256 ifa_ifwithnet(struct sockaddr *addr)
1260 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1261 u_int af = addr->sa_family;
1262 char *addr_data = addr->sa_data, *cplim;
1265 * AF_LINK addresses can be looked up directly by their index number,
1266 * so do that if we can.
1268 if (af == AF_LINK) {
1269 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1270 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1271 return (ifaddr_byindex(sdl->sdl_index));
1275 * Scan though each interface, looking for ones that have
1276 * addresses in this address family.
1279 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1280 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1281 char *cp, *cp2, *cp3;
1283 if (ifa->ifa_addr->sa_family != af)
1285 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1287 * This is a bit broken as it doesn't
1288 * take into account that the remote end may
1289 * be a single node in the network we are
1291 * The trouble is that we don't know the
1292 * netmask for the remote end.
1294 if (ifa->ifa_dstaddr != NULL &&
1295 sa_equal(addr, ifa->ifa_dstaddr))
1299 * if we have a special address handler,
1300 * then use it instead of the generic one.
1302 if (ifa->ifa_claim_addr) {
1303 if ((*ifa->ifa_claim_addr)(ifa, addr))
1309 * Scan all the bits in the ifa's address.
1310 * If a bit dissagrees with what we are
1311 * looking for, mask it with the netmask
1312 * to see if it really matters.
1313 * (A byte at a time)
1315 if (ifa->ifa_netmask == 0)
1318 cp2 = ifa->ifa_addr->sa_data;
1319 cp3 = ifa->ifa_netmask->sa_data;
1320 cplim = ifa->ifa_netmask->sa_len
1321 + (char *)ifa->ifa_netmask;
1323 if ((*cp++ ^ *cp2++) & *cp3++)
1324 goto next; /* next address! */
1326 * If the netmask of what we just found
1327 * is more specific than what we had before
1328 * (if we had one) then remember the new one
1329 * before continuing to search
1330 * for an even better one.
1332 if (ifa_maybe == 0 ||
1333 rn_refines((caddr_t)ifa->ifa_netmask,
1334 (caddr_t)ifa_maybe->ifa_netmask))
1346 * Find an interface address specific to an interface best matching
1350 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1353 char *cp, *cp2, *cp3;
1355 struct ifaddr *ifa_maybe = 0;
1356 u_int af = addr->sa_family;
1360 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1361 if (ifa->ifa_addr->sa_family != af)
1365 if (ifa->ifa_netmask == 0) {
1366 if (sa_equal(addr, ifa->ifa_addr) ||
1367 (ifa->ifa_dstaddr &&
1368 sa_equal(addr, ifa->ifa_dstaddr)))
1372 if (ifp->if_flags & IFF_POINTOPOINT) {
1373 if (sa_equal(addr, ifa->ifa_dstaddr))
1377 cp2 = ifa->ifa_addr->sa_data;
1378 cp3 = ifa->ifa_netmask->sa_data;
1379 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1380 for (; cp3 < cplim; cp3++)
1381 if ((*cp++ ^ *cp2++) & *cp3)
1392 #include <net/route.h>
1395 * Default action when installing a route with a Link Level gateway.
1396 * Lookup an appropriate real ifa to point to.
1397 * This should be moved to /sys/net/link.c eventually.
1400 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1402 struct ifaddr *ifa, *oifa;
1403 struct sockaddr *dst;
1408 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1409 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1411 ifa = ifaof_ifpforaddr(dst, ifp);
1413 IFAREF(ifa); /* XXX */
1417 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1418 ifa->ifa_rtrequest(cmd, rt, info);
1423 * Mark an interface down and notify protocols of
1425 * NOTE: must be called at splnet or eqivalent.
1428 if_unroute(struct ifnet *ifp, int flag, int fam)
1432 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1434 ifp->if_flags &= ~flag;
1435 getmicrotime(&ifp->if_lastchange);
1436 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1437 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1438 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1439 if_qflush(&ifp->if_snd);
1442 carp_carpdev_state(ifp->if_carp);
1448 * Mark an interface up and notify protocols of
1450 * NOTE: must be called at splnet or eqivalent.
1453 if_route(struct ifnet *ifp, int flag, int fam)
1457 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1459 ifp->if_flags |= flag;
1460 getmicrotime(&ifp->if_lastchange);
1461 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1462 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1463 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1466 carp_carpdev_state(ifp->if_carp);
1474 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1475 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1478 * Handle a change in the interface link state. To avoid LORs
1479 * between driver lock and upper layer locks, as well as possible
1480 * recursions, we post event to taskqueue, and all job
1481 * is done in static do_link_state_change().
1484 if_link_state_change(struct ifnet *ifp, int link_state)
1486 /* Return if state hasn't changed. */
1487 if (ifp->if_link_state == link_state)
1490 ifp->if_link_state = link_state;
1492 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1496 do_link_state_change(void *arg, int pending)
1498 struct ifnet *ifp = (struct ifnet *)arg;
1499 int link_state = ifp->if_link_state;
1502 /* Notify that the link state has changed. */
1504 if (link_state == LINK_STATE_UP)
1506 else if (link_state == LINK_STATE_DOWN)
1507 link = NOTE_LINKDOWN;
1509 link = NOTE_LINKINV;
1510 KNOTE_UNLOCKED(&ifp->if_klist, link);
1511 if (ifp->if_vlantrunk != NULL)
1512 (*vlan_link_state_p)(ifp, link);
1514 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1515 IFP2AC(ifp)->ac_netgraph != NULL)
1516 (*ng_ether_link_state_p)(ifp, link_state);
1519 carp_carpdev_state(ifp->if_carp);
1521 if (ifp->if_bridge) {
1522 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1523 (*bstp_linkstate_p)(ifp, link_state);
1526 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1527 (*lagg_linkstate_p)(ifp, link_state);
1530 devctl_notify("IFNET", ifp->if_xname,
1531 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1533 if_printf(ifp, "%d link states coalesced\n", pending);
1534 if (log_link_state_change)
1535 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1536 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1540 * Mark an interface down and notify protocols of
1542 * NOTE: must be called at splnet or eqivalent.
1545 if_down(struct ifnet *ifp)
1548 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1552 * Mark an interface up and notify protocols of
1554 * NOTE: must be called at splnet or eqivalent.
1557 if_up(struct ifnet *ifp)
1560 if_route(ifp, IFF_UP, AF_UNSPEC);
1564 * Flush an interface queue.
1567 if_qflush(struct ifaltq *ifq)
1573 if (ALTQ_IS_ENABLED(ifq))
1577 while ((m = n) != 0) {
1588 * Handle interface watchdog timer routines. Called
1589 * from softclock, we decrement timers (if set) and
1590 * call the appropriate interface routine on expiration.
1592 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1593 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1594 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1597 if_slowtimo(void *arg)
1603 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1604 if (ifp->if_timer == 0 || --ifp->if_timer)
1606 if (ifp->if_watchdog)
1607 (*ifp->if_watchdog)(ifp);
1611 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1615 * Map interface name to
1616 * interface structure pointer.
1619 ifunit(const char *name)
1624 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1625 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1633 * Hardware specific interface ioctls.
1636 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1641 int new_flags, temp_flags;
1642 size_t namelen, onamelen;
1643 char new_name[IFNAMSIZ];
1645 struct sockaddr_dl *sdl;
1647 ifr = (struct ifreq *)data;
1650 ifr->ifr_index = ifp->if_index;
1654 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1655 ifr->ifr_flags = temp_flags & 0xffff;
1656 ifr->ifr_flagshigh = temp_flags >> 16;
1660 ifr->ifr_reqcap = ifp->if_capabilities;
1661 ifr->ifr_curcap = ifp->if_capenable;
1666 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1671 ifr->ifr_metric = ifp->if_metric;
1675 ifr->ifr_mtu = ifp->if_mtu;
1679 ifr->ifr_phys = ifp->if_physical;
1683 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1687 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1688 * check, so we don't need special handling here yet.
1690 new_flags = (ifr->ifr_flags & 0xffff) |
1691 (ifr->ifr_flagshigh << 16);
1692 if (ifp->if_flags & IFF_SMART) {
1693 /* Smart drivers twiddle their own routes */
1694 } else if (ifp->if_flags & IFF_UP &&
1695 (new_flags & IFF_UP) == 0) {
1699 } else if (new_flags & IFF_UP &&
1700 (ifp->if_flags & IFF_UP) == 0) {
1705 /* See if permanently promiscuous mode bit is about to flip */
1706 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1707 if (new_flags & IFF_PPROMISC)
1708 ifp->if_flags |= IFF_PROMISC;
1709 else if (ifp->if_pcount == 0)
1710 ifp->if_flags &= ~IFF_PROMISC;
1711 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1713 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1715 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1716 (new_flags &~ IFF_CANTCHANGE);
1717 if (ifp->if_ioctl) {
1719 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1720 IFF_UNLOCKGIANT(ifp);
1722 getmicrotime(&ifp->if_lastchange);
1726 error = priv_check(td, PRIV_NET_SETIFCAP);
1729 if (ifp->if_ioctl == NULL)
1730 return (EOPNOTSUPP);
1731 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1734 error = (*ifp->if_ioctl)(ifp, cmd, data);
1735 IFF_UNLOCKGIANT(ifp);
1737 getmicrotime(&ifp->if_lastchange);
1742 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1747 error = priv_check(td, PRIV_NET_SETIFNAME);
1750 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1753 if (new_name[0] == '\0')
1755 if (ifunit(new_name) != NULL)
1758 /* Announce the departure of the interface. */
1759 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1760 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1762 log(LOG_INFO, "%s: changing name to '%s'\n",
1763 ifp->if_xname, new_name);
1765 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1768 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1769 namelen = strlen(new_name);
1770 onamelen = sdl->sdl_nlen;
1772 * Move the address if needed. This is safe because we
1773 * allocate space for a name of length IFNAMSIZ when we
1774 * create this in if_attach().
1776 if (namelen != onamelen) {
1777 bcopy(sdl->sdl_data + onamelen,
1778 sdl->sdl_data + namelen, sdl->sdl_alen);
1780 bcopy(new_name, sdl->sdl_data, namelen);
1781 sdl->sdl_nlen = namelen;
1782 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1783 bzero(sdl->sdl_data, onamelen);
1784 while (namelen != 0)
1785 sdl->sdl_data[--namelen] = 0xff;
1788 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1789 /* Announce the return of the interface. */
1790 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1794 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1797 ifp->if_metric = ifr->ifr_metric;
1798 getmicrotime(&ifp->if_lastchange);
1802 error = priv_check(td, PRIV_NET_SETIFPHYS);
1805 if (ifp->if_ioctl == NULL)
1806 return (EOPNOTSUPP);
1808 error = (*ifp->if_ioctl)(ifp, cmd, data);
1809 IFF_UNLOCKGIANT(ifp);
1811 getmicrotime(&ifp->if_lastchange);
1816 u_long oldmtu = ifp->if_mtu;
1818 error = priv_check(td, PRIV_NET_SETIFMTU);
1821 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1823 if (ifp->if_ioctl == NULL)
1824 return (EOPNOTSUPP);
1826 error = (*ifp->if_ioctl)(ifp, cmd, data);
1827 IFF_UNLOCKGIANT(ifp);
1829 getmicrotime(&ifp->if_lastchange);
1833 * If the link MTU changed, do network layer specific procedure.
1835 if (ifp->if_mtu != oldmtu) {
1845 if (cmd == SIOCADDMULTI)
1846 error = priv_check(td, PRIV_NET_ADDMULTI);
1848 error = priv_check(td, PRIV_NET_DELMULTI);
1852 /* Don't allow group membership on non-multicast interfaces. */
1853 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1854 return (EOPNOTSUPP);
1856 /* Don't let users screw up protocols' entries. */
1857 if (ifr->ifr_addr.sa_family != AF_LINK)
1860 if (cmd == SIOCADDMULTI) {
1861 struct ifmultiaddr *ifma;
1864 * Userland is only permitted to join groups once
1865 * via the if_addmulti() KPI, because it cannot hold
1866 * struct ifmultiaddr * between calls. It may also
1867 * lose a race while we check if the membership
1871 ifma = if_findmulti(ifp, &ifr->ifr_addr);
1872 IF_ADDR_UNLOCK(ifp);
1876 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1878 error = if_delmulti(ifp, &ifr->ifr_addr);
1881 getmicrotime(&ifp->if_lastchange);
1884 case SIOCSIFPHYADDR:
1885 case SIOCDIFPHYADDR:
1887 case SIOCSIFPHYADDR_IN6:
1889 case SIOCSLIFPHYADDR:
1891 case SIOCSIFGENERIC:
1892 error = priv_check(td, PRIV_NET_HWIOCTL);
1895 if (ifp->if_ioctl == NULL)
1896 return (EOPNOTSUPP);
1898 error = (*ifp->if_ioctl)(ifp, cmd, data);
1899 IFF_UNLOCKGIANT(ifp);
1901 getmicrotime(&ifp->if_lastchange);
1905 ifs = (struct ifstat *)data;
1906 ifs->ascii[0] = '\0';
1908 case SIOCGIFPSRCADDR:
1909 case SIOCGIFPDSTADDR:
1910 case SIOCGLIFPHYADDR:
1912 case SIOCGIFGENERIC:
1913 if (ifp->if_ioctl == NULL)
1914 return (EOPNOTSUPP);
1916 error = (*ifp->if_ioctl)(ifp, cmd, data);
1917 IFF_UNLOCKGIANT(ifp);
1921 error = priv_check(td, PRIV_NET_SETLLADDR);
1924 error = if_setlladdr(ifp,
1925 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1930 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1932 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1935 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1941 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1947 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1949 error = priv_check(td, PRIV_NET_DELIFGROUP);
1952 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1968 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1981 return (ifconf(cmd, data));
1983 ifr = (struct ifreq *)data;
1988 error = priv_check(td, PRIV_NET_IFCREATE);
1991 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1992 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1994 error = priv_check(td, PRIV_NET_IFDESTROY);
1997 return if_clone_destroy(ifr->ifr_name);
1999 case SIOCIFGCLONERS:
2000 return (if_clone_list((struct if_clonereq *)data));
2002 return (if_getgroupmembers((struct ifgroupreq *)data));
2005 ifp = ifunit(ifr->ifr_name);
2009 error = ifhwioctl(cmd, ifp, data, td);
2010 if (error != ENOIOCTL)
2013 oif_flags = ifp->if_flags;
2014 if (so->so_proto == 0)
2015 return (EOPNOTSUPP);
2017 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2020 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2021 error = (*ifp->if_ioctl)(ifp, cmd, data);
2028 case SIOCSIFDSTADDR:
2030 case SIOCSIFBRDADDR:
2031 case SIOCSIFNETMASK:
2032 #if BYTE_ORDER != BIG_ENDIAN
2033 if (ifr->ifr_addr.sa_family == 0 &&
2034 ifr->ifr_addr.sa_len < 16) {
2035 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2036 ifr->ifr_addr.sa_len = 16;
2039 if (ifr->ifr_addr.sa_len == 0)
2040 ifr->ifr_addr.sa_len = 16;
2048 case OSIOCGIFDSTADDR:
2049 cmd = SIOCGIFDSTADDR;
2052 case OSIOCGIFBRDADDR:
2053 cmd = SIOCGIFBRDADDR;
2056 case OSIOCGIFNETMASK:
2057 cmd = SIOCGIFNETMASK;
2059 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2063 if (error == EOPNOTSUPP && ifp != NULL &&
2064 ifp->if_ioctl != NULL)
2065 error = (*ifp->if_ioctl)(ifp, cmd, data);
2069 case OSIOCGIFDSTADDR:
2070 case OSIOCGIFBRDADDR:
2071 case OSIOCGIFNETMASK:
2072 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2076 #endif /* COMPAT_43 */
2078 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2080 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2081 if (ifp->if_flags & IFF_UP) {
2092 * The code common to handling reference counted flags,
2093 * e.g., in ifpromisc() and if_allmulti().
2094 * The "pflag" argument can specify a permanent mode flag to check,
2095 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2097 * Only to be used on stack-owned flags, not driver-owned flags.
2100 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2104 int oldflags, oldcount;
2106 /* Sanity checks to catch programming errors */
2107 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2108 ("%s: setting driver-owned flag %d", __func__, flag));
2111 KASSERT(*refcount >= 0,
2112 ("%s: increment negative refcount %d for flag %d",
2113 __func__, *refcount, flag));
2115 KASSERT(*refcount > 0,
2116 ("%s: decrement non-positive refcount %d for flag %d",
2117 __func__, *refcount, flag));
2119 /* In case this mode is permanent, just touch refcount */
2120 if (ifp->if_flags & pflag) {
2121 *refcount += onswitch ? 1 : -1;
2125 /* Save ifnet parameters for if_ioctl() may fail */
2126 oldcount = *refcount;
2127 oldflags = ifp->if_flags;
2130 * See if we aren't the only and touching refcount is enough.
2131 * Actually toggle interface flag if we are the first or last.
2136 ifp->if_flags |= flag;
2140 ifp->if_flags &= ~flag;
2143 /* Call down the driver since we've changed interface flags */
2144 if (ifp->if_ioctl == NULL) {
2148 ifr.ifr_flags = ifp->if_flags & 0xffff;
2149 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2151 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2152 IFF_UNLOCKGIANT(ifp);
2155 /* Notify userland that interface flags have changed */
2160 /* Recover after driver error */
2161 *refcount = oldcount;
2162 ifp->if_flags = oldflags;
2167 * Set/clear promiscuous mode on interface ifp based on the truth value
2168 * of pswitch. The calls are reference counted so that only the first
2169 * "on" request actually has an effect, as does the final "off" request.
2170 * Results are undefined if the "off" and "on" requests are not matched.
2173 ifpromisc(struct ifnet *ifp, int pswitch)
2176 int oldflags = ifp->if_flags;
2178 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2179 &ifp->if_pcount, pswitch);
2180 /* If promiscuous mode status has changed, log a message */
2181 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2182 log(LOG_INFO, "%s: promiscuous mode %s\n",
2184 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2189 * Return interface configuration
2190 * of system. List may be used
2191 * in later ioctl's (above) to get
2192 * other information.
2196 ifconf(u_long cmd, caddr_t data)
2198 struct ifconf *ifc = (struct ifconf *)data;
2200 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2201 struct ifconf ifc_swab;
2207 int error, full = 0, valid_len, max_len;
2210 if (cmd == SIOCGIFCONF32) {
2211 ifc_swab.ifc_len = ifc32->ifc_len;
2212 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2216 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2217 max_len = MAXPHYS - 1;
2219 /* Prevent hostile input from being able to crash the system */
2220 if (ifc->ifc_len <= 0)
2224 if (ifc->ifc_len <= max_len) {
2225 max_len = ifc->ifc_len;
2228 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2232 IFNET_RLOCK(); /* could sleep XXX */
2233 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2237 * Zero the ifr_name buffer to make sure we don't
2238 * disclose the contents of the stack.
2240 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2242 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2243 >= sizeof(ifr.ifr_name)) {
2246 return (ENAMETOOLONG);
2250 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2251 struct sockaddr *sa = ifa->ifa_addr;
2253 if (prison_if(curthread->td_ucred, sa) != 0)
2257 if (cmd == OSIOCGIFCONF) {
2258 struct osockaddr *osa =
2259 (struct osockaddr *)&ifr.ifr_addr;
2261 osa->sa_family = sa->sa_family;
2262 sbuf_bcat(sb, &ifr, sizeof(ifr));
2263 max_len += sizeof(ifr);
2266 if (sa->sa_len <= sizeof(*sa)) {
2268 sbuf_bcat(sb, &ifr, sizeof(ifr));
2269 max_len += sizeof(ifr);
2272 offsetof(struct ifreq, ifr_addr));
2273 max_len += offsetof(struct ifreq, ifr_addr);
2274 sbuf_bcat(sb, sa, sa->sa_len);
2275 max_len += sa->sa_len;
2278 if (!sbuf_overflowed(sb))
2279 valid_len = sbuf_len(sb);
2282 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2283 sbuf_bcat(sb, &ifr, sizeof(ifr));
2284 max_len += sizeof(ifr);
2286 if (!sbuf_overflowed(sb))
2287 valid_len = sbuf_len(sb);
2293 * If we didn't allocate enough space (uncommon), try again. If
2294 * we have already allocated as much space as we are allowed,
2295 * return what we've got.
2297 if (valid_len != max_len && !full) {
2302 ifc->ifc_len = valid_len;
2304 if (cmd == SIOCGIFCONF32)
2305 ifc32->ifc_len = valid_len;
2308 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2314 * Just like ifpromisc(), but for all-multicast-reception mode.
2317 if_allmulti(struct ifnet *ifp, int onswitch)
2320 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2323 struct ifmultiaddr *
2324 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2326 struct ifmultiaddr *ifma;
2328 IF_ADDR_LOCK_ASSERT(ifp);
2330 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2331 if (sa->sa_family == AF_LINK) {
2332 if (sa_dl_equal(ifma->ifma_addr, sa))
2335 if (sa_equal(ifma->ifma_addr, sa))
2344 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2345 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2346 * the ifnet multicast address list here, so the caller must do that and
2347 * other setup work (such as notifying the device driver). The reference
2348 * count is initialized to 1.
2350 static struct ifmultiaddr *
2351 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2354 struct ifmultiaddr *ifma;
2355 struct sockaddr *dupsa;
2357 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2362 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2363 if (dupsa == NULL) {
2364 FREE(ifma, M_IFMADDR);
2367 bcopy(sa, dupsa, sa->sa_len);
2368 ifma->ifma_addr = dupsa;
2370 ifma->ifma_ifp = ifp;
2371 ifma->ifma_refcount = 1;
2372 ifma->ifma_protospec = NULL;
2375 ifma->ifma_lladdr = NULL;
2379 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2380 if (dupsa == NULL) {
2381 FREE(ifma->ifma_addr, M_IFMADDR);
2382 FREE(ifma, M_IFMADDR);
2385 bcopy(llsa, dupsa, llsa->sa_len);
2386 ifma->ifma_lladdr = dupsa;
2392 * if_freemulti: free ifmultiaddr structure and possibly attached related
2393 * addresses. The caller is responsible for implementing reference
2394 * counting, notifying the driver, handling routing messages, and releasing
2395 * any dependent link layer state.
2398 if_freemulti(struct ifmultiaddr *ifma)
2401 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2402 ifma->ifma_refcount));
2403 KASSERT(ifma->ifma_protospec == NULL,
2404 ("if_freemulti: protospec not NULL"));
2406 if (ifma->ifma_lladdr != NULL)
2407 FREE(ifma->ifma_lladdr, M_IFMADDR);
2408 FREE(ifma->ifma_addr, M_IFMADDR);
2409 FREE(ifma, M_IFMADDR);
2413 * Register an additional multicast address with a network interface.
2415 * - If the address is already present, bump the reference count on the
2416 * address and return.
2417 * - If the address is not link-layer, look up a link layer address.
2418 * - Allocate address structures for one or both addresses, and attach to the
2419 * multicast address list on the interface. If automatically adding a link
2420 * layer address, the protocol address will own a reference to the link
2421 * layer address, to be freed when it is freed.
2422 * - Notify the network device driver of an addition to the multicast address
2425 * 'sa' points to caller-owned memory with the desired multicast address.
2427 * 'retifma' will be used to return a pointer to the resulting multicast
2428 * address reference, if desired.
2431 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2432 struct ifmultiaddr **retifma)
2434 struct ifmultiaddr *ifma, *ll_ifma;
2435 struct sockaddr *llsa;
2439 * If the address is already present, return a new reference to it;
2440 * otherwise, allocate storage and set up a new address.
2443 ifma = if_findmulti(ifp, sa);
2445 ifma->ifma_refcount++;
2446 if (retifma != NULL)
2448 IF_ADDR_UNLOCK(ifp);
2453 * The address isn't already present; resolve the protocol address
2454 * into a link layer address, and then look that up, bump its
2455 * refcount or allocate an ifma for that also. If 'llsa' was
2456 * returned, we will need to free it later.
2460 if (ifp->if_resolvemulti != NULL) {
2461 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2467 * Allocate the new address. Don't hook it up yet, as we may also
2468 * need to allocate a link layer multicast address.
2470 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2477 * If a link layer address is found, we'll need to see if it's
2478 * already present in the address list, or allocate is as well.
2479 * When this block finishes, the link layer address will be on the
2483 ll_ifma = if_findmulti(ifp, llsa);
2484 if (ll_ifma == NULL) {
2485 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2486 if (ll_ifma == NULL) {
2487 --ifma->ifma_refcount;
2492 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2495 ll_ifma->ifma_refcount++;
2496 ifma->ifma_llifma = ll_ifma;
2500 * We now have a new multicast address, ifma, and possibly a new or
2501 * referenced link layer address. Add the primary address to the
2502 * ifnet address list.
2504 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2506 if (retifma != NULL)
2510 * Must generate the message while holding the lock so that 'ifma'
2511 * pointer is still valid.
2513 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2514 IF_ADDR_UNLOCK(ifp);
2517 * We are certain we have added something, so call down to the
2518 * interface to let them know about it.
2520 if (ifp->if_ioctl != NULL) {
2522 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2523 IFF_UNLOCKGIANT(ifp);
2527 FREE(llsa, M_IFMADDR);
2533 FREE(llsa, M_IFMADDR);
2536 IF_ADDR_UNLOCK(ifp);
2541 * Delete a multicast group membership by network-layer group address.
2543 * Returns ENOENT if the entry could not be found. If ifp no longer
2544 * exists, results are undefined. This entry point should only be used
2545 * from subsystems which do appropriate locking to hold ifp for the
2546 * duration of the call.
2547 * Network-layer protocol domains must use if_delmulti_ifma().
2550 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2552 struct ifmultiaddr *ifma;
2558 TAILQ_FOREACH(oifp, &ifnet, if_link)
2565 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2572 ifma = if_findmulti(ifp, sa);
2574 lastref = if_delmulti_locked(ifp, ifma, 0);
2575 IF_ADDR_UNLOCK(ifp);
2580 if (lastref && ifp->if_ioctl != NULL) {
2582 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2583 IFF_UNLOCKGIANT(ifp);
2590 * Delete a multicast group membership by group membership pointer.
2591 * Network-layer protocol domains must use this routine.
2593 * It is safe to call this routine if the ifp disappeared. Callers should
2594 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be
2598 if_delmulti_ifma(struct ifmultiaddr *ifma)
2603 ifp = ifma->ifma_ifp;
2606 printf("%s: ifma_ifp seems to be detached\n", __func__);
2611 TAILQ_FOREACH(oifp, &ifnet, if_link)
2615 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2622 * If and only if the ifnet instance exists: Acquire the address lock.
2627 lastref = if_delmulti_locked(ifp, ifma, 0);
2631 * If and only if the ifnet instance exists:
2632 * Release the address lock.
2633 * If the group was left: update the hardware hash filter.
2635 IF_ADDR_UNLOCK(ifp);
2636 if (lastref && ifp->if_ioctl != NULL) {
2638 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2639 IFF_UNLOCKGIANT(ifp);
2645 * Perform deletion of network-layer and/or link-layer multicast address.
2647 * Return 0 if the reference count was decremented.
2648 * Return 1 if the final reference was released, indicating that the
2649 * hardware hash filter should be reprogrammed.
2652 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2654 struct ifmultiaddr *ll_ifma;
2656 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2657 KASSERT(ifma->ifma_ifp == ifp,
2658 ("%s: inconsistent ifp %p", __func__, ifp));
2659 IF_ADDR_LOCK_ASSERT(ifp);
2662 ifp = ifma->ifma_ifp;
2665 * If the ifnet is detaching, null out references to ifnet,
2666 * so that upper protocol layers will notice, and not attempt
2667 * to obtain locks for an ifnet which no longer exists. The
2668 * routing socket announcement must happen before the ifnet
2669 * instance is detached from the system.
2673 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2676 * ifp may already be nulled out if we are being reentered
2677 * to delete the ll_ifma.
2680 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2681 ifma->ifma_ifp = NULL;
2685 if (--ifma->ifma_refcount > 0)
2689 * If this ifma is a network-layer ifma, a link-layer ifma may
2690 * have been associated with it. Release it first if so.
2692 ll_ifma = ifma->ifma_llifma;
2693 if (ll_ifma != NULL) {
2694 KASSERT(ifma->ifma_lladdr != NULL,
2695 ("%s: llifma w/o lladdr", __func__));
2697 ll_ifma->ifma_ifp = NULL; /* XXX */
2698 if (--ll_ifma->ifma_refcount == 0) {
2700 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2703 if_freemulti(ll_ifma);
2708 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2713 * The last reference to this instance of struct ifmultiaddr
2714 * was released; the hardware should be notified of this change.
2720 * Set the link layer address on an interface.
2722 * At this time we only support certain types of interfaces,
2723 * and we don't allow the length of the address to change.
2726 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2728 struct sockaddr_dl *sdl;
2735 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2738 if (len != sdl->sdl_alen) /* don't allow length to change */
2740 switch (ifp->if_type) {
2748 case IFT_IEEE8023ADLAG:
2749 bcopy(lladdr, LLADDR(sdl), len);
2755 * If the interface is already up, we need
2756 * to re-init it in order to reprogram its
2759 if ((ifp->if_flags & IFF_UP) != 0) {
2760 if (ifp->if_ioctl) {
2762 ifp->if_flags &= ~IFF_UP;
2763 ifr.ifr_flags = ifp->if_flags & 0xffff;
2764 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2765 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2766 ifp->if_flags |= IFF_UP;
2767 ifr.ifr_flags = ifp->if_flags & 0xffff;
2768 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2769 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2770 IFF_UNLOCKGIANT(ifp);
2774 * Also send gratuitous ARPs to notify other nodes about
2775 * the address change.
2777 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2778 if (ifa->ifa_addr->sa_family == AF_INET)
2779 arp_ifinit(ifp, ifa);
2787 * The name argument must be a pointer to storage which will last as
2788 * long as the interface does. For physical devices, the result of
2789 * device_get_name(dev) is a good choice and for pseudo-devices a
2790 * static string works well.
2793 if_initname(struct ifnet *ifp, const char *name, int unit)
2795 ifp->if_dname = name;
2796 ifp->if_dunit = unit;
2797 if (unit != IF_DUNIT_NONE)
2798 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2800 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2804 if_printf(struct ifnet *ifp, const char * fmt, ...)
2809 retval = printf("%s: ", ifp->if_xname);
2811 retval += vprintf(fmt, ap);
2817 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2818 * be called without Giant. However, we often can't acquire the Giant lock
2819 * at those points; instead, we run it via a task queue that holds Giant via
2820 * if_start_deferred.
2822 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2823 * outstanding if_start_deferred() tasks that will run after the free. This
2824 * probably means waiting in if_detach().
2827 if_start(struct ifnet *ifp)
2830 if (ifp->if_flags & IFF_NEEDSGIANT) {
2831 if (mtx_owned(&Giant))
2832 (*(ifp)->if_start)(ifp);
2834 taskqueue_enqueue(taskqueue_swi_giant,
2835 &ifp->if_starttask);
2837 (*(ifp)->if_start)(ifp);
2841 if_start_deferred(void *context, int pending)
2848 (ifp->if_start)(ifp);
2852 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2857 if (_IF_QFULL(ifq)) {
2864 ifp->if_obytes += m->m_pkthdr.len + adjust;
2865 if (m->m_flags & (M_BCAST|M_MCAST))
2867 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2869 _IF_ENQUEUE(ifq, m);
2871 if (ifp != NULL && !active)
2877 if_register_com_alloc(u_char type,
2878 if_com_alloc_t *a, if_com_free_t *f)
2881 KASSERT(if_com_alloc[type] == NULL,
2882 ("if_register_com_alloc: %d already registered", type));
2883 KASSERT(if_com_free[type] == NULL,
2884 ("if_register_com_alloc: %d free already registered", type));
2886 if_com_alloc[type] = a;
2887 if_com_free[type] = f;
2891 if_deregister_com_alloc(u_char type)
2894 KASSERT(if_com_alloc[type] != NULL,
2895 ("if_deregister_com_alloc: %d not registered", type));
2896 KASSERT(if_com_free[type] != NULL,
2897 ("if_deregister_com_alloc: %d free not registered", type));
2898 if_com_alloc[type] = NULL;
2899 if_com_free[type] = NULL;