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 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
88 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
90 /* Log link state change events */
91 static int log_link_state_change = 1;
93 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
94 &log_link_state_change, 0,
95 "log interface link state change events");
97 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
98 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
100 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
102 static void if_attachdomain(void *);
103 static void if_attachdomain1(struct ifnet *);
104 static int ifconf(u_long, caddr_t);
105 static void if_grow(void);
106 static void if_init(void *);
107 static void if_check(void *);
108 static void if_qflush(struct ifaltq *);
109 static void if_route(struct ifnet *, int flag, int fam);
110 static int if_setflag(struct ifnet *, int, int, int *, int);
111 static void if_slowtimo(void *);
112 static void if_unroute(struct ifnet *, int flag, int fam);
113 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
114 static int if_rtdel(struct radix_node *, void *);
115 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
116 static void if_start_deferred(void *context, int pending);
117 static void do_link_state_change(void *, int);
118 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
119 static int if_getgroupmembers(struct ifgroupreq *);
122 * XXX: declare here to avoid to include many inet6 related files..
123 * should be more generalized?
125 extern void nd6_setmtu(struct ifnet *);
129 struct ifindex_entry *ifindex_table = NULL;
130 int ifqmaxlen = IFQ_MAXLEN;
131 struct ifnethead ifnet; /* depend on static init XXX */
132 struct ifgrouphead ifg_head;
133 struct mtx ifnet_lock;
134 static if_com_alloc_t *if_com_alloc[256];
135 static if_com_free_t *if_com_free[256];
137 static int if_indexlim = 8;
138 static struct knlist ifklist;
140 static void filt_netdetach(struct knote *kn);
141 static int filt_netdev(struct knote *kn, long hint);
143 static struct filterops netdev_filtops =
144 { 1, NULL, filt_netdetach, filt_netdev };
147 * System initialization
149 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
150 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
152 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
153 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
154 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
156 static d_open_t netopen;
157 static d_close_t netclose;
158 static d_ioctl_t netioctl;
159 static d_kqfilter_t netkqfilter;
161 static struct cdevsw net_cdevsw = {
162 .d_version = D_VERSION,
163 .d_flags = D_NEEDGIANT,
168 .d_kqfilter = netkqfilter,
172 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
178 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
184 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
189 /* only support interface specific ioctls */
190 if (IOCGROUP(cmd) != 'i')
195 * special network device, not interface.
197 if (cmd == SIOCGIFCONF)
198 return (ifconf(cmd, data)); /* XXX remove cmd */
200 if (cmd == SIOCGIFCONF32)
201 return (ifconf(cmd, data)); /* XXX remove cmd */
206 ifp = ifnet_byindex(idx);
210 error = ifhwioctl(cmd, ifp, data, td);
211 if (error == ENOIOCTL)
217 netkqfilter(struct cdev *dev, struct knote *kn)
219 struct knlist *klist;
223 switch (kn->kn_filter) {
225 kn->kn_fop = &netdev_filtops;
235 ifp = ifnet_byindex(idx);
238 klist = &ifp->if_klist;
241 kn->kn_hook = (caddr_t)klist;
243 knlist_add(klist, kn, 0);
249 filt_netdetach(struct knote *kn)
251 struct knlist *klist = (struct knlist *)kn->kn_hook;
253 knlist_remove(klist, kn, 0);
257 filt_netdev(struct knote *kn, long hint)
259 struct knlist *klist = (struct knlist *)kn->kn_hook;
262 * Currently NOTE_EXIT is abused to indicate device detach.
264 if (hint == NOTE_EXIT) {
265 kn->kn_data = NOTE_LINKINV;
266 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
267 knlist_remove_inevent(klist, kn);
271 kn->kn_data = hint; /* current status */
272 if (kn->kn_sfflags & hint)
273 kn->kn_fflags |= hint;
274 return (kn->kn_fflags != 0);
278 * Network interface utility routines.
280 * Routines with ifa_ifwith* names take sockaddr *'s as
285 if_init(void *dummy __unused)
290 TAILQ_INIT(&ifg_head);
291 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
292 if_grow(); /* create initial table */
293 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
294 UID_ROOT, GID_WHEEL, 0600, "network");
302 struct ifindex_entry *e;
305 n = if_indexlim * sizeof(*e);
306 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
307 if (ifindex_table != NULL) {
308 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
309 free((caddr_t)ifindex_table, M_IFNET);
316 if_check(void *dummy __unused)
322 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
323 TAILQ_FOREACH(ifp, &ifnet, if_link) {
324 if (ifp->if_snd.ifq_maxlen == 0) {
325 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
326 ifp->if_snd.ifq_maxlen = ifqmaxlen;
328 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
330 "XXX: driver didn't initialize queue mtx\n");
331 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
332 MTX_NETWORK_LOCK, MTX_DEF);
341 * Allocate a struct ifnet and in index for an interface.
344 if_alloc(u_char type)
348 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
351 * Try to find an empty slot below if_index. If we fail, take
354 * XXX: should be locked!
356 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
357 if (ifnet_byindex(ifp->if_index) == NULL)
360 /* Catch if_index overflow. */
361 if (ifp->if_index < 1) {
365 if (ifp->if_index > if_index)
366 if_index = ifp->if_index;
367 if (if_index >= if_indexlim)
369 ifnet_byindex(ifp->if_index) = ifp;
373 if (if_com_alloc[type] != NULL) {
374 ifp->if_l2com = if_com_alloc[type](type, ifp);
375 if (ifp->if_l2com == NULL) {
380 IF_ADDR_LOCK_INIT(ifp);
386 if_free(struct ifnet *ifp)
389 /* Do not add code to this function! Add it to if_free_type(). */
390 if_free_type(ifp, ifp->if_type);
394 if_free_type(struct ifnet *ifp, u_char type)
397 if (ifp != ifnet_byindex(ifp->if_index)) {
398 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
403 IF_ADDR_LOCK_DESTROY(ifp);
405 ifnet_byindex(ifp->if_index) = NULL;
407 /* XXX: should be locked with if_findindex() */
408 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
411 if (if_com_free[type] != NULL)
412 if_com_free[type](ifp->if_l2com, type);
418 * Attach an interface to the
419 * list of "active" interfaces.
422 if_attach(struct ifnet *ifp)
424 unsigned socksize, ifasize;
425 int namelen, masklen;
426 struct sockaddr_dl *sdl;
429 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
430 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
433 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
434 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
435 IF_AFDATA_LOCK_INIT(ifp);
436 ifp->if_afdata_initialized = 0;
439 * The old code would work if the interface passed a pre-existing
440 * chain of ifaddrs to this code. We don't trust our callers to
441 * properly initialize the tailq, however, so we no longer allow
442 * this unlikely case.
444 TAILQ_INIT(&ifp->if_addrhead);
445 TAILQ_INIT(&ifp->if_prefixhead);
446 TAILQ_INIT(&ifp->if_multiaddrs);
447 TAILQ_INIT(&ifp->if_groups);
449 if_addgroup(ifp, IFG_ALL);
451 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
452 getmicrotime(&ifp->if_lastchange);
453 ifp->if_data.ifi_epoch = time_uptime;
454 ifp->if_data.ifi_datalen = sizeof(struct if_data);
458 mac_create_ifnet(ifp);
461 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
462 unit2minor(ifp->if_index),
463 UID_ROOT, GID_WHEEL, 0600, "%s/%s",
464 net_cdevsw.d_name, ifp->if_xname);
465 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
466 net_cdevsw.d_name, ifp->if_index);
468 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
471 * create a Link Level name for this device
473 namelen = strlen(ifp->if_xname);
475 * Always save enough space for any possiable name so we can do
476 * a rename in place later.
478 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
479 socksize = masklen + ifp->if_addrlen;
480 if (socksize < sizeof(*sdl))
481 socksize = sizeof(*sdl);
482 socksize = roundup2(socksize, sizeof(long));
483 ifasize = sizeof(*ifa) + 2 * socksize;
484 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
486 sdl = (struct sockaddr_dl *)(ifa + 1);
487 sdl->sdl_len = socksize;
488 sdl->sdl_family = AF_LINK;
489 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
490 sdl->sdl_nlen = namelen;
491 sdl->sdl_index = ifp->if_index;
492 sdl->sdl_type = ifp->if_type;
495 ifa->ifa_rtrequest = link_rtrequest;
496 ifa->ifa_addr = (struct sockaddr *)sdl;
497 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
498 ifa->ifa_netmask = (struct sockaddr *)sdl;
499 sdl->sdl_len = masklen;
501 sdl->sdl_data[--namelen] = 0xff;
503 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
504 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
505 ifp->if_snd.altq_type = 0;
506 ifp->if_snd.altq_disc = NULL;
507 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
508 ifp->if_snd.altq_tbr = NULL;
509 ifp->if_snd.altq_ifp = ifp;
512 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
515 if (domain_init_status >= 2)
516 if_attachdomain1(ifp);
518 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
519 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
521 /* Announce the interface. */
522 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
524 if (ifp->if_watchdog != NULL)
525 if_printf(ifp, "using obsoleted if_watchdog interface\n");
529 if_attachdomain(void *dummy)
535 TAILQ_FOREACH(ifp, &ifnet, if_link)
536 if_attachdomain1(ifp);
539 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
540 if_attachdomain, NULL);
543 if_attachdomain1(struct ifnet *ifp)
551 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
552 * cannot lock ifp->if_afdata initialization, entirely.
554 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
558 if (ifp->if_afdata_initialized >= domain_init_status) {
559 IF_AFDATA_UNLOCK(ifp);
561 printf("if_attachdomain called more than once on %s\n",
565 ifp->if_afdata_initialized = domain_init_status;
566 IF_AFDATA_UNLOCK(ifp);
568 /* address family dependent data region */
569 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
570 for (dp = domains; dp; dp = dp->dom_next) {
571 if (dp->dom_ifattach)
572 ifp->if_afdata[dp->dom_family] =
573 (*dp->dom_ifattach)(ifp);
580 * Remove any network addresses from an interface.
584 if_purgeaddrs(struct ifnet *ifp)
586 struct ifaddr *ifa, *next;
588 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
589 if (ifa->ifa_addr->sa_family == AF_LINK)
592 /* XXX: Ugly!! ad hoc just for INET */
593 if (ifa->ifa_addr->sa_family == AF_INET) {
594 struct ifaliasreq ifr;
596 bzero(&ifr, sizeof(ifr));
597 ifr.ifra_addr = *ifa->ifa_addr;
598 if (ifa->ifa_dstaddr)
599 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
600 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
606 if (ifa->ifa_addr->sa_family == AF_INET6) {
608 /* ifp_addrhead is already updated */
612 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
618 * Detach an interface, removing it from the
619 * list of "active" interfaces.
621 * XXXRW: There are some significant questions about event ordering, and
622 * how to prevent things from starting to use the interface during detach.
625 if_detach(struct ifnet *ifp)
628 struct radix_node_head *rnh;
636 TAILQ_FOREACH(iter, &ifnet, if_link)
638 TAILQ_REMOVE(&ifnet, ifp, if_link);
647 * Remove/wait for pending events.
649 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
652 * Remove routes and flush queues.
657 if (ALTQ_IS_ENABLED(&ifp->if_snd))
658 altq_disable(&ifp->if_snd);
659 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
660 altq_detach(&ifp->if_snd);
671 * Remove all IPv6 kernel structs related to ifp. This should be done
672 * before removing routing entries below, since IPv6 interface direct
673 * routes are expected to be removed by the IPv6-specific kernel API.
674 * Otherwise, the kernel will detect some inconsistency and bark it.
679 * Remove link ifaddr pointer and maybe decrement if_index.
680 * Clean up all addresses.
683 destroy_dev(ifdev_byindex(ifp->if_index));
684 ifdev_byindex(ifp->if_index) = NULL;
686 /* We can now free link ifaddr. */
687 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
688 ifa = TAILQ_FIRST(&ifp->if_addrhead);
689 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
694 * Delete all remaining routes using this interface
695 * Unfortuneatly the only way to do this is to slog through
696 * the entire routing table looking for routes which point
697 * to this interface...oh well...
699 for (i = 1; i <= AF_MAX; i++) {
700 if ((rnh = rt_tables[i]) == NULL)
702 RADIX_NODE_HEAD_LOCK(rnh);
703 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
704 RADIX_NODE_HEAD_UNLOCK(rnh);
707 /* Announce that the interface is gone. */
708 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
709 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
710 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
713 for (dp = domains; dp; dp = dp->dom_next) {
714 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
715 (*dp->dom_ifdetach)(ifp,
716 ifp->if_afdata[dp->dom_family]);
718 IF_AFDATA_UNLOCK(ifp);
721 mac_destroy_ifnet(ifp);
723 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
724 knlist_clear(&ifp->if_klist, 0);
725 knlist_destroy(&ifp->if_klist);
726 mtx_destroy(&ifp->if_snd.ifq_mtx);
727 IF_AFDATA_DESTROY(ifp);
732 * Add a group to an interface
735 if_addgroup(struct ifnet *ifp, const char *groupname)
737 struct ifg_list *ifgl;
738 struct ifg_group *ifg = NULL;
739 struct ifg_member *ifgm;
741 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
742 groupname[strlen(groupname) - 1] <= '9')
746 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
747 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
752 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
753 M_NOWAIT)) == NULL) {
758 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
759 M_TEMP, M_NOWAIT)) == NULL) {
765 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
766 if (!strcmp(ifg->ifg_group, groupname))
770 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
771 M_TEMP, M_NOWAIT)) == NULL) {
777 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
779 TAILQ_INIT(&ifg->ifg_members);
780 EVENTHANDLER_INVOKE(group_attach_event, ifg);
781 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
785 ifgl->ifgl_group = ifg;
786 ifgm->ifgm_ifp = ifp;
789 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
790 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
795 EVENTHANDLER_INVOKE(group_change_event, groupname);
801 * Remove a group from an interface
804 if_delgroup(struct ifnet *ifp, const char *groupname)
806 struct ifg_list *ifgl;
807 struct ifg_member *ifgm;
810 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
811 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
819 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
822 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
823 if (ifgm->ifgm_ifp == ifp)
827 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
831 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
832 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
833 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
834 free(ifgl->ifgl_group, M_TEMP);
840 EVENTHANDLER_INVOKE(group_change_event, groupname);
846 * Stores all groups from an interface in memory pointed
850 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
853 struct ifg_list *ifgl;
854 struct ifg_req ifgrq, *ifgp;
855 struct ifgroupreq *ifgr = data;
857 if (ifgr->ifgr_len == 0) {
859 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
860 ifgr->ifgr_len += sizeof(struct ifg_req);
865 len = ifgr->ifgr_len;
866 ifgp = ifgr->ifgr_groups;
869 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
870 if (len < sizeof(ifgrq)) {
874 bzero(&ifgrq, sizeof ifgrq);
875 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
876 sizeof(ifgrq.ifgrq_group));
877 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
881 len -= sizeof(ifgrq);
890 * Stores all members of a group in memory pointed to by data
893 if_getgroupmembers(struct ifgroupreq *data)
895 struct ifgroupreq *ifgr = data;
896 struct ifg_group *ifg;
897 struct ifg_member *ifgm;
898 struct ifg_req ifgrq, *ifgp;
902 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
903 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
910 if (ifgr->ifgr_len == 0) {
911 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
912 ifgr->ifgr_len += sizeof(ifgrq);
917 len = ifgr->ifgr_len;
918 ifgp = ifgr->ifgr_groups;
919 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
920 if (len < sizeof(ifgrq)) {
924 bzero(&ifgrq, sizeof ifgrq);
925 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
926 sizeof(ifgrq.ifgrq_member));
927 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
931 len -= sizeof(ifgrq);
940 * Delete Routes for a Network Interface
942 * Called for each routing entry via the rnh->rnh_walktree() call above
943 * to delete all route entries referencing a detaching network interface.
946 * rn pointer to node in the routing table
947 * arg argument passed to rnh->rnh_walktree() - detaching interface
951 * errno failed - reason indicated
955 if_rtdel(struct radix_node *rn, void *arg)
957 struct rtentry *rt = (struct rtentry *)rn;
958 struct ifnet *ifp = arg;
961 if (rt->rt_ifp == ifp) {
964 * Protect (sorta) against walktree recursion problems
967 if ((rt->rt_flags & RTF_UP) == 0)
970 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
971 rt_mask(rt), rt->rt_flags,
972 (struct rtentry **) NULL);
974 log(LOG_WARNING, "if_rtdel: error %d\n", err);
981 #define sa_equal(a1, a2) (bcmp((a1), (a2), ((a1))->sa_len) == 0)
984 * Locate an interface based on a complete address.
988 ifa_ifwithaddr(struct sockaddr *addr)
994 TAILQ_FOREACH(ifp, &ifnet, if_link)
995 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
996 if (ifa->ifa_addr->sa_family != addr->sa_family)
998 if (sa_equal(addr, ifa->ifa_addr))
1000 /* IP6 doesn't have broadcast */
1001 if ((ifp->if_flags & IFF_BROADCAST) &&
1002 ifa->ifa_broadaddr &&
1003 ifa->ifa_broadaddr->sa_len != 0 &&
1004 sa_equal(ifa->ifa_broadaddr, addr))
1014 * Locate an interface based on the broadcast address.
1018 ifa_ifwithbroadaddr(struct sockaddr *addr)
1024 TAILQ_FOREACH(ifp, &ifnet, if_link)
1025 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1026 if (ifa->ifa_addr->sa_family != addr->sa_family)
1028 if ((ifp->if_flags & IFF_BROADCAST) &&
1029 ifa->ifa_broadaddr &&
1030 ifa->ifa_broadaddr->sa_len != 0 &&
1031 sa_equal(ifa->ifa_broadaddr, addr))
1041 * Locate the point to point interface with a given destination address.
1045 ifa_ifwithdstaddr(struct sockaddr *addr)
1051 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1052 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1054 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1055 if (ifa->ifa_addr->sa_family != addr->sa_family)
1057 if (ifa->ifa_dstaddr &&
1058 sa_equal(addr, ifa->ifa_dstaddr))
1069 * Find an interface on a specific network. If many, choice
1070 * is most specific found.
1073 ifa_ifwithnet(struct sockaddr *addr)
1077 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1078 u_int af = addr->sa_family;
1079 char *addr_data = addr->sa_data, *cplim;
1082 * AF_LINK addresses can be looked up directly by their index number,
1083 * so do that if we can.
1085 if (af == AF_LINK) {
1086 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1087 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1088 return (ifaddr_byindex(sdl->sdl_index));
1092 * Scan though each interface, looking for ones that have
1093 * addresses in this address family.
1096 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1097 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1098 char *cp, *cp2, *cp3;
1100 if (ifa->ifa_addr->sa_family != af)
1102 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1104 * This is a bit broken as it doesn't
1105 * take into account that the remote end may
1106 * be a single node in the network we are
1108 * The trouble is that we don't know the
1109 * netmask for the remote end.
1111 if (ifa->ifa_dstaddr != 0 &&
1112 sa_equal(addr, ifa->ifa_dstaddr))
1116 * if we have a special address handler,
1117 * then use it instead of the generic one.
1119 if (ifa->ifa_claim_addr) {
1120 if ((*ifa->ifa_claim_addr)(ifa, addr))
1126 * Scan all the bits in the ifa's address.
1127 * If a bit dissagrees with what we are
1128 * looking for, mask it with the netmask
1129 * to see if it really matters.
1130 * (A byte at a time)
1132 if (ifa->ifa_netmask == 0)
1135 cp2 = ifa->ifa_addr->sa_data;
1136 cp3 = ifa->ifa_netmask->sa_data;
1137 cplim = ifa->ifa_netmask->sa_len
1138 + (char *)ifa->ifa_netmask;
1140 if ((*cp++ ^ *cp2++) & *cp3++)
1141 goto next; /* next address! */
1143 * If the netmask of what we just found
1144 * is more specific than what we had before
1145 * (if we had one) then remember the new one
1146 * before continuing to search
1147 * for an even better one.
1149 if (ifa_maybe == 0 ||
1150 rn_refines((caddr_t)ifa->ifa_netmask,
1151 (caddr_t)ifa_maybe->ifa_netmask))
1163 * Find an interface address specific to an interface best matching
1167 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1170 char *cp, *cp2, *cp3;
1172 struct ifaddr *ifa_maybe = 0;
1173 u_int af = addr->sa_family;
1177 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1178 if (ifa->ifa_addr->sa_family != af)
1182 if (ifa->ifa_netmask == 0) {
1183 if (sa_equal(addr, ifa->ifa_addr) ||
1184 (ifa->ifa_dstaddr &&
1185 sa_equal(addr, ifa->ifa_dstaddr)))
1189 if (ifp->if_flags & IFF_POINTOPOINT) {
1190 if (sa_equal(addr, ifa->ifa_dstaddr))
1194 cp2 = ifa->ifa_addr->sa_data;
1195 cp3 = ifa->ifa_netmask->sa_data;
1196 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1197 for (; cp3 < cplim; cp3++)
1198 if ((*cp++ ^ *cp2++) & *cp3)
1209 #include <net/route.h>
1212 * Default action when installing a route with a Link Level gateway.
1213 * Lookup an appropriate real ifa to point to.
1214 * This should be moved to /sys/net/link.c eventually.
1217 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1219 struct ifaddr *ifa, *oifa;
1220 struct sockaddr *dst;
1225 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1226 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1228 ifa = ifaof_ifpforaddr(dst, ifp);
1230 IFAREF(ifa); /* XXX */
1234 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1235 ifa->ifa_rtrequest(cmd, rt, info);
1240 * Mark an interface down and notify protocols of
1242 * NOTE: must be called at splnet or eqivalent.
1245 if_unroute(struct ifnet *ifp, int flag, int fam)
1249 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1251 ifp->if_flags &= ~flag;
1252 getmicrotime(&ifp->if_lastchange);
1253 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1254 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1255 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1256 if_qflush(&ifp->if_snd);
1259 carp_carpdev_state(ifp->if_carp);
1265 * Mark an interface up and notify protocols of
1267 * NOTE: must be called at splnet or eqivalent.
1270 if_route(struct ifnet *ifp, int flag, int fam)
1274 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1276 ifp->if_flags |= flag;
1277 getmicrotime(&ifp->if_lastchange);
1278 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1279 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1280 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1283 carp_carpdev_state(ifp->if_carp);
1291 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1292 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1295 * Handle a change in the interface link state. To avoid LORs
1296 * between driver lock and upper layer locks, as well as possible
1297 * recursions, we post event to taskqueue, and all job
1298 * is done in static do_link_state_change().
1301 if_link_state_change(struct ifnet *ifp, int link_state)
1303 /* Return if state hasn't changed. */
1304 if (ifp->if_link_state == link_state)
1307 ifp->if_link_state = link_state;
1309 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1313 do_link_state_change(void *arg, int pending)
1315 struct ifnet *ifp = (struct ifnet *)arg;
1316 int link_state = ifp->if_link_state;
1319 /* Notify that the link state has changed. */
1321 if (link_state == LINK_STATE_UP)
1323 else if (link_state == LINK_STATE_DOWN)
1324 link = NOTE_LINKDOWN;
1326 link = NOTE_LINKINV;
1327 KNOTE_UNLOCKED(&ifp->if_klist, link);
1328 if (ifp->if_vlantrunk != NULL)
1329 (*vlan_link_state_p)(ifp, link);
1331 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1332 IFP2AC(ifp)->ac_netgraph != NULL)
1333 (*ng_ether_link_state_p)(ifp, link_state);
1336 carp_carpdev_state(ifp->if_carp);
1338 if (ifp->if_bridge) {
1339 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1340 (*bstp_linkstate_p)(ifp, link_state);
1343 devctl_notify("IFNET", ifp->if_xname,
1344 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1346 if_printf(ifp, "%d link states coalesced\n", pending);
1347 if (log_link_state_change)
1348 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1349 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1353 * Mark an interface down and notify protocols of
1355 * NOTE: must be called at splnet or eqivalent.
1358 if_down(struct ifnet *ifp)
1361 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1365 * Mark an interface up and notify protocols of
1367 * NOTE: must be called at splnet or eqivalent.
1370 if_up(struct ifnet *ifp)
1373 if_route(ifp, IFF_UP, AF_UNSPEC);
1377 * Flush an interface queue.
1380 if_qflush(struct ifaltq *ifq)
1386 if (ALTQ_IS_ENABLED(ifq))
1390 while ((m = n) != 0) {
1401 * Handle interface watchdog timer routines. Called
1402 * from softclock, we decrement timers (if set) and
1403 * call the appropriate interface routine on expiration.
1405 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1406 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1407 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1410 if_slowtimo(void *arg)
1416 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1417 if (ifp->if_timer == 0 || --ifp->if_timer)
1419 if (ifp->if_watchdog)
1420 (*ifp->if_watchdog)(ifp);
1424 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1428 * Map interface name to
1429 * interface structure pointer.
1432 ifunit(const char *name)
1437 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1438 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1446 * Hardware specific interface ioctls.
1449 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1454 int new_flags, temp_flags;
1455 size_t namelen, onamelen;
1456 char new_name[IFNAMSIZ];
1458 struct sockaddr_dl *sdl;
1460 ifr = (struct ifreq *)data;
1463 ifr->ifr_index = ifp->if_index;
1467 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1468 ifr->ifr_flags = temp_flags & 0xffff;
1469 ifr->ifr_flagshigh = temp_flags >> 16;
1473 ifr->ifr_reqcap = ifp->if_capabilities;
1474 ifr->ifr_curcap = ifp->if_capenable;
1479 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1484 ifr->ifr_metric = ifp->if_metric;
1488 ifr->ifr_mtu = ifp->if_mtu;
1492 ifr->ifr_phys = ifp->if_physical;
1496 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1500 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1501 * check, so we don't need special handling here yet.
1503 new_flags = (ifr->ifr_flags & 0xffff) |
1504 (ifr->ifr_flagshigh << 16);
1505 if (ifp->if_flags & IFF_SMART) {
1506 /* Smart drivers twiddle their own routes */
1507 } else if (ifp->if_flags & IFF_UP &&
1508 (new_flags & IFF_UP) == 0) {
1512 } else if (new_flags & IFF_UP &&
1513 (ifp->if_flags & IFF_UP) == 0) {
1518 /* See if permanently promiscuous mode bit is about to flip */
1519 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1520 if (new_flags & IFF_PPROMISC)
1521 ifp->if_flags |= IFF_PROMISC;
1522 else if (ifp->if_pcount == 0)
1523 ifp->if_flags &= ~IFF_PROMISC;
1524 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1526 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1528 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1529 (new_flags &~ IFF_CANTCHANGE);
1530 if (ifp->if_ioctl) {
1532 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1533 IFF_UNLOCKGIANT(ifp);
1535 getmicrotime(&ifp->if_lastchange);
1539 error = priv_check(td, PRIV_NET_SETIFCAP);
1542 if (ifp->if_ioctl == NULL)
1543 return (EOPNOTSUPP);
1544 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1547 error = (*ifp->if_ioctl)(ifp, cmd, data);
1548 IFF_UNLOCKGIANT(ifp);
1550 getmicrotime(&ifp->if_lastchange);
1555 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1560 error = priv_check(td, PRIV_NET_SETIFNAME);
1563 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1566 if (new_name[0] == '\0')
1568 if (ifunit(new_name) != NULL)
1571 /* Announce the departure of the interface. */
1572 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1573 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1575 log(LOG_INFO, "%s: changing name to '%s'\n",
1576 ifp->if_xname, new_name);
1578 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1581 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1582 namelen = strlen(new_name);
1583 onamelen = sdl->sdl_nlen;
1585 * Move the address if needed. This is safe because we
1586 * allocate space for a name of length IFNAMSIZ when we
1587 * create this in if_attach().
1589 if (namelen != onamelen) {
1590 bcopy(sdl->sdl_data + onamelen,
1591 sdl->sdl_data + namelen, sdl->sdl_alen);
1593 bcopy(new_name, sdl->sdl_data, namelen);
1594 sdl->sdl_nlen = namelen;
1595 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1596 bzero(sdl->sdl_data, onamelen);
1597 while (namelen != 0)
1598 sdl->sdl_data[--namelen] = 0xff;
1601 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1602 /* Announce the return of the interface. */
1603 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1607 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1610 ifp->if_metric = ifr->ifr_metric;
1611 getmicrotime(&ifp->if_lastchange);
1615 error = priv_check(td, PRIV_NET_SETIFPHYS);
1618 if (ifp->if_ioctl == NULL)
1619 return (EOPNOTSUPP);
1621 error = (*ifp->if_ioctl)(ifp, cmd, data);
1622 IFF_UNLOCKGIANT(ifp);
1624 getmicrotime(&ifp->if_lastchange);
1629 u_long oldmtu = ifp->if_mtu;
1631 error = priv_check(td, PRIV_NET_SETIFMTU);
1634 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1636 if (ifp->if_ioctl == NULL)
1637 return (EOPNOTSUPP);
1639 error = (*ifp->if_ioctl)(ifp, cmd, data);
1640 IFF_UNLOCKGIANT(ifp);
1642 getmicrotime(&ifp->if_lastchange);
1646 * If the link MTU changed, do network layer specific procedure.
1648 if (ifp->if_mtu != oldmtu) {
1658 if (cmd == SIOCADDMULTI)
1659 error = priv_check(td, PRIV_NET_ADDMULTI);
1661 error = priv_check(td, PRIV_NET_DELMULTI);
1665 /* Don't allow group membership on non-multicast interfaces. */
1666 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1667 return (EOPNOTSUPP);
1669 /* Don't let users screw up protocols' entries. */
1670 if (ifr->ifr_addr.sa_family != AF_LINK)
1673 if (cmd == SIOCADDMULTI) {
1674 struct ifmultiaddr *ifma;
1675 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1677 error = if_delmulti(ifp, &ifr->ifr_addr);
1680 getmicrotime(&ifp->if_lastchange);
1683 case SIOCSIFPHYADDR:
1684 case SIOCDIFPHYADDR:
1686 case SIOCSIFPHYADDR_IN6:
1688 case SIOCSLIFPHYADDR:
1690 case SIOCSIFGENERIC:
1691 error = priv_check(td, PRIV_NET_HWIOCTL);
1694 if (ifp->if_ioctl == NULL)
1695 return (EOPNOTSUPP);
1697 error = (*ifp->if_ioctl)(ifp, cmd, data);
1698 IFF_UNLOCKGIANT(ifp);
1700 getmicrotime(&ifp->if_lastchange);
1704 ifs = (struct ifstat *)data;
1705 ifs->ascii[0] = '\0';
1707 case SIOCGIFPSRCADDR:
1708 case SIOCGIFPDSTADDR:
1709 case SIOCGLIFPHYADDR:
1711 case SIOCGIFGENERIC:
1712 if (ifp->if_ioctl == NULL)
1713 return (EOPNOTSUPP);
1715 error = (*ifp->if_ioctl)(ifp, cmd, data);
1716 IFF_UNLOCKGIANT(ifp);
1720 error = priv_check(td, PRIV_NET_SETLLADDR);
1723 error = if_setlladdr(ifp,
1724 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1729 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1731 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1734 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1740 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1746 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1748 error = priv_check(td, PRIV_NET_DELIFGROUP);
1751 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1767 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1780 return (ifconf(cmd, data));
1782 ifr = (struct ifreq *)data;
1787 error = priv_check(td, PRIV_NET_IFCREATE);
1790 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1791 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1793 error = priv_check(td, PRIV_NET_IFDESTROY);
1796 return if_clone_destroy(ifr->ifr_name);
1798 case SIOCIFGCLONERS:
1799 return (if_clone_list((struct if_clonereq *)data));
1801 return (if_getgroupmembers((struct ifgroupreq *)data));
1804 ifp = ifunit(ifr->ifr_name);
1808 error = ifhwioctl(cmd, ifp, data, td);
1809 if (error != ENOIOCTL)
1812 oif_flags = ifp->if_flags;
1813 if (so->so_proto == 0)
1814 return (EOPNOTSUPP);
1816 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1825 case SIOCSIFDSTADDR:
1827 case SIOCSIFBRDADDR:
1828 case SIOCSIFNETMASK:
1829 #if BYTE_ORDER != BIG_ENDIAN
1830 if (ifr->ifr_addr.sa_family == 0 &&
1831 ifr->ifr_addr.sa_len < 16) {
1832 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1833 ifr->ifr_addr.sa_len = 16;
1836 if (ifr->ifr_addr.sa_len == 0)
1837 ifr->ifr_addr.sa_len = 16;
1845 case OSIOCGIFDSTADDR:
1846 cmd = SIOCGIFDSTADDR;
1849 case OSIOCGIFBRDADDR:
1850 cmd = SIOCGIFBRDADDR;
1853 case OSIOCGIFNETMASK:
1854 cmd = SIOCGIFNETMASK;
1856 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1863 case OSIOCGIFDSTADDR:
1864 case OSIOCGIFBRDADDR:
1865 case OSIOCGIFNETMASK:
1866 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1870 #endif /* COMPAT_43 */
1872 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1874 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1875 if (ifp->if_flags & IFF_UP) {
1886 * The code common to handling reference counted flags,
1887 * e.g., in ifpromisc() and if_allmulti().
1888 * The "pflag" argument can specify a permanent mode flag to check,
1889 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
1891 * Only to be used on stack-owned flags, not driver-owned flags.
1894 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
1898 int oldflags, oldcount;
1900 /* Sanity checks to catch programming errors */
1901 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
1902 ("%s: setting driver-owned flag %d", __func__, flag));
1905 KASSERT(*refcount >= 0,
1906 ("%s: increment negative refcount %d for flag %d",
1907 __func__, *refcount, flag));
1909 KASSERT(*refcount > 0,
1910 ("%s: decrement non-positive refcount %d for flag %d",
1911 __func__, *refcount, flag));
1913 /* In case this mode is permanent, just touch refcount */
1914 if (ifp->if_flags & pflag) {
1915 *refcount += onswitch ? 1 : -1;
1919 /* Save ifnet parameters for if_ioctl() may fail */
1920 oldcount = *refcount;
1921 oldflags = ifp->if_flags;
1924 * See if we aren't the only and touching refcount is enough.
1925 * Actually toggle interface flag if we are the first or last.
1930 ifp->if_flags |= flag;
1934 ifp->if_flags &= ~flag;
1937 /* Call down the driver since we've changed interface flags */
1938 if (ifp->if_ioctl == NULL) {
1942 ifr.ifr_flags = ifp->if_flags & 0xffff;
1943 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1945 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1946 IFF_UNLOCKGIANT(ifp);
1949 /* Notify userland that interface flags have changed */
1954 /* Recover after driver error */
1955 *refcount = oldcount;
1956 ifp->if_flags = oldflags;
1961 * Set/clear promiscuous mode on interface ifp based on the truth value
1962 * of pswitch. The calls are reference counted so that only the first
1963 * "on" request actually has an effect, as does the final "off" request.
1964 * Results are undefined if the "off" and "on" requests are not matched.
1967 ifpromisc(struct ifnet *ifp, int pswitch)
1970 int oldflags = ifp->if_flags;
1972 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
1973 &ifp->if_pcount, pswitch);
1974 /* If promiscuous mode status has changed, log a message */
1975 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
1976 log(LOG_INFO, "%s: promiscuous mode %s\n",
1978 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1983 * Return interface configuration
1984 * of system. List may be used
1985 * in later ioctl's (above) to get
1986 * other information.
1990 ifconf(u_long cmd, caddr_t data)
1992 struct ifconf *ifc = (struct ifconf *)data;
1994 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
1995 struct ifconf ifc_swab;
2001 int error, full = 0, valid_len, max_len;
2004 if (cmd == SIOCGIFCONF32) {
2005 ifc_swab.ifc_len = ifc32->ifc_len;
2006 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2010 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2011 max_len = MAXPHYS - 1;
2013 /* Prevent hostile input from being able to crash the system */
2014 if (ifc->ifc_len <= 0)
2018 if (ifc->ifc_len <= max_len) {
2019 max_len = ifc->ifc_len;
2022 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2026 IFNET_RLOCK(); /* could sleep XXX */
2027 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2031 * Zero the ifr_name buffer to make sure we don't
2032 * disclose the contents of the stack.
2034 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2036 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2037 >= sizeof(ifr.ifr_name)) {
2040 return (ENAMETOOLONG);
2044 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2045 struct sockaddr *sa = ifa->ifa_addr;
2047 if (jailed(curthread->td_ucred) &&
2048 prison_if(curthread->td_ucred, sa))
2052 if (cmd == OSIOCGIFCONF) {
2053 struct osockaddr *osa =
2054 (struct osockaddr *)&ifr.ifr_addr;
2056 osa->sa_family = sa->sa_family;
2057 sbuf_bcat(sb, &ifr, sizeof(ifr));
2058 max_len += sizeof(ifr);
2061 if (sa->sa_len <= sizeof(*sa)) {
2063 sbuf_bcat(sb, &ifr, sizeof(ifr));
2064 max_len += sizeof(ifr);
2067 offsetof(struct ifreq, ifr_addr));
2068 max_len += offsetof(struct ifreq, ifr_addr);
2069 sbuf_bcat(sb, sa, sa->sa_len);
2070 max_len += sa->sa_len;
2073 if (!sbuf_overflowed(sb))
2074 valid_len = sbuf_len(sb);
2077 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2078 sbuf_bcat(sb, &ifr, sizeof(ifr));
2079 max_len += sizeof(ifr);
2081 if (!sbuf_overflowed(sb))
2082 valid_len = sbuf_len(sb);
2088 * If we didn't allocate enough space (uncommon), try again. If
2089 * we have already allocated as much space as we are allowed,
2090 * return what we've got.
2092 if (valid_len != max_len && !full) {
2097 ifc->ifc_len = valid_len;
2099 if (cmd == SIOCGIFCONF32)
2100 ifc32->ifc_len = valid_len;
2103 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2109 * Just like ifpromisc(), but for all-multicast-reception mode.
2112 if_allmulti(struct ifnet *ifp, int onswitch)
2115 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2118 static struct ifmultiaddr *
2119 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2121 struct ifmultiaddr *ifma;
2123 IF_ADDR_LOCK_ASSERT(ifp);
2125 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2126 if (sa_equal(ifma->ifma_addr, sa))
2134 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2135 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2136 * the ifnet multicast address list here, so the caller must do that and
2137 * other setup work (such as notifying the device driver). The reference
2138 * count is initialized to 1.
2140 static struct ifmultiaddr *
2141 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2144 struct ifmultiaddr *ifma;
2145 struct sockaddr *dupsa;
2147 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2152 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2153 if (dupsa == NULL) {
2154 FREE(ifma, M_IFMADDR);
2157 bcopy(sa, dupsa, sa->sa_len);
2158 ifma->ifma_addr = dupsa;
2160 ifma->ifma_ifp = ifp;
2161 ifma->ifma_refcount = 1;
2162 ifma->ifma_protospec = NULL;
2165 ifma->ifma_lladdr = NULL;
2169 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2170 if (dupsa == NULL) {
2171 FREE(ifma->ifma_addr, M_IFMADDR);
2172 FREE(ifma, M_IFMADDR);
2175 bcopy(llsa, dupsa, llsa->sa_len);
2176 ifma->ifma_lladdr = dupsa;
2182 * if_freemulti: free ifmultiaddr structure and possibly attached related
2183 * addresses. The caller is responsible for implementing reference
2184 * counting, notifying the driver, handling routing messages, and releasing
2185 * any dependent link layer state.
2188 if_freemulti(struct ifmultiaddr *ifma)
2191 KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d",
2192 ifma->ifma_refcount));
2193 KASSERT(ifma->ifma_protospec == NULL,
2194 ("if_freemulti: protospec not NULL"));
2196 if (ifma->ifma_lladdr != NULL)
2197 FREE(ifma->ifma_lladdr, M_IFMADDR);
2198 FREE(ifma->ifma_addr, M_IFMADDR);
2199 FREE(ifma, M_IFMADDR);
2203 * Register an additional multicast address with a network interface.
2205 * - If the address is already present, bump the reference count on the
2206 * address and return.
2207 * - If the address is not link-layer, look up a link layer address.
2208 * - Allocate address structures for one or both addresses, and attach to the
2209 * multicast address list on the interface. If automatically adding a link
2210 * layer address, the protocol address will own a reference to the link
2211 * layer address, to be freed when it is freed.
2212 * - Notify the network device driver of an addition to the multicast address
2215 * 'sa' points to caller-owned memory with the desired multicast address.
2217 * 'retifma' will be used to return a pointer to the resulting multicast
2218 * address reference, if desired.
2221 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2222 struct ifmultiaddr **retifma)
2224 struct ifmultiaddr *ifma, *ll_ifma;
2225 struct sockaddr *llsa;
2229 * If the address is already present, return a new reference to it;
2230 * otherwise, allocate storage and set up a new address.
2233 ifma = if_findmulti(ifp, sa);
2235 ifma->ifma_refcount++;
2236 if (retifma != NULL)
2238 IF_ADDR_UNLOCK(ifp);
2243 * The address isn't already present; resolve the protocol address
2244 * into a link layer address, and then look that up, bump its
2245 * refcount or allocate an ifma for that also. If 'llsa' was
2246 * returned, we will need to free it later.
2250 if (ifp->if_resolvemulti != NULL) {
2251 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2257 * Allocate the new address. Don't hook it up yet, as we may also
2258 * need to allocate a link layer multicast address.
2260 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2267 * If a link layer address is found, we'll need to see if it's
2268 * already present in the address list, or allocate is as well.
2269 * When this block finishes, the link layer address will be on the
2273 ll_ifma = if_findmulti(ifp, llsa);
2274 if (ll_ifma == NULL) {
2275 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2276 if (ll_ifma == NULL) {
2281 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2284 ll_ifma->ifma_refcount++;
2288 * We now have a new multicast address, ifma, and possibly a new or
2289 * referenced link layer address. Add the primary address to the
2290 * ifnet address list.
2292 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2294 if (retifma != NULL)
2298 * Must generate the message while holding the lock so that 'ifma'
2299 * pointer is still valid.
2301 * XXXRW: How come we don't announce ll_ifma?
2303 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2304 IF_ADDR_UNLOCK(ifp);
2307 * We are certain we have added something, so call down to the
2308 * interface to let them know about it.
2310 if (ifp->if_ioctl != NULL) {
2312 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2313 IFF_UNLOCKGIANT(ifp);
2317 FREE(llsa, M_IFMADDR);
2323 FREE(llsa, M_IFMADDR);
2326 IF_ADDR_UNLOCK(ifp);
2331 * Remove a reference to a multicast address on this interface. Yell
2332 * if the request does not match an existing membership.
2335 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2337 struct ifmultiaddr *ifma, *ll_ifma;
2340 ifma = if_findmulti(ifp, sa);
2342 IF_ADDR_UNLOCK(ifp);
2346 if (ifma->ifma_refcount > 1) {
2347 ifma->ifma_refcount--;
2348 IF_ADDR_UNLOCK(ifp);
2352 sa = ifma->ifma_lladdr;
2354 ll_ifma = if_findmulti(ifp, sa);
2359 * XXXRW: How come we don't announce ll_ifma?
2361 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2363 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2366 if (ll_ifma != NULL) {
2367 if (ll_ifma->ifma_refcount == 1) {
2368 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link);
2369 if_freemulti(ll_ifma);
2371 ll_ifma->ifma_refcount--;
2373 IF_ADDR_UNLOCK(ifp);
2376 * Make sure the interface driver is notified
2377 * in the case of a link layer mcast group being left.
2379 if (ifp->if_ioctl) {
2381 (void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2382 IFF_UNLOCKGIANT(ifp);
2389 * Set the link layer address on an interface.
2391 * At this time we only support certain types of interfaces,
2392 * and we don't allow the length of the address to change.
2395 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2397 struct sockaddr_dl *sdl;
2404 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2407 if (len != sdl->sdl_alen) /* don't allow length to change */
2409 switch (ifp->if_type) {
2417 bcopy(lladdr, LLADDR(sdl), len);
2423 * If the interface is already up, we need
2424 * to re-init it in order to reprogram its
2427 if ((ifp->if_flags & IFF_UP) != 0) {
2428 if (ifp->if_ioctl) {
2430 ifp->if_flags &= ~IFF_UP;
2431 ifr.ifr_flags = ifp->if_flags & 0xffff;
2432 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2433 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2434 ifp->if_flags |= IFF_UP;
2435 ifr.ifr_flags = ifp->if_flags & 0xffff;
2436 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2437 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2438 IFF_UNLOCKGIANT(ifp);
2442 * Also send gratuitous ARPs to notify other nodes about
2443 * the address change.
2445 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2446 if (ifa->ifa_addr->sa_family == AF_INET)
2447 arp_ifinit(ifp, ifa);
2455 * The name argument must be a pointer to storage which will last as
2456 * long as the interface does. For physical devices, the result of
2457 * device_get_name(dev) is a good choice and for pseudo-devices a
2458 * static string works well.
2461 if_initname(struct ifnet *ifp, const char *name, int unit)
2463 ifp->if_dname = name;
2464 ifp->if_dunit = unit;
2465 if (unit != IF_DUNIT_NONE)
2466 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2468 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2472 if_printf(struct ifnet *ifp, const char * fmt, ...)
2477 retval = printf("%s: ", ifp->if_xname);
2479 retval += vprintf(fmt, ap);
2485 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2486 * be called without Giant. However, we often can't acquire the Giant lock
2487 * at those points; instead, we run it via a task queue that holds Giant via
2488 * if_start_deferred.
2490 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2491 * outstanding if_start_deferred() tasks that will run after the free. This
2492 * probably means waiting in if_detach().
2495 if_start(struct ifnet *ifp)
2500 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
2501 if (mtx_owned(&Giant))
2502 (*(ifp)->if_start)(ifp);
2504 taskqueue_enqueue(taskqueue_swi_giant,
2505 &ifp->if_starttask);
2507 (*(ifp)->if_start)(ifp);
2511 if_start_deferred(void *context, int pending)
2516 * This code must be entered with Giant, and should never run if
2517 * we're not running with debug.mpsafenet.
2519 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
2523 (ifp->if_start)(ifp);
2527 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2532 if (_IF_QFULL(ifq)) {
2539 ifp->if_obytes += m->m_pkthdr.len + adjust;
2540 if (m->m_flags & (M_BCAST|M_MCAST))
2542 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2544 _IF_ENQUEUE(ifq, m);
2546 if (ifp != NULL && !active)
2552 if_register_com_alloc(u_char type,
2553 if_com_alloc_t *a, if_com_free_t *f)
2556 KASSERT(if_com_alloc[type] == NULL,
2557 ("if_register_com_alloc: %d already registered", type));
2558 KASSERT(if_com_free[type] == NULL,
2559 ("if_register_com_alloc: %d free already registered", type));
2561 if_com_alloc[type] = a;
2562 if_com_free[type] = f;
2566 if_deregister_com_alloc(u_char type)
2569 KASSERT(if_com_alloc[type] != NULL,
2570 ("if_deregister_com_alloc: %d not registered", type));
2571 KASSERT(if_com_free[type] != NULL,
2572 ("if_deregister_com_alloc: %d free not registered", type));
2573 if_com_alloc[type] = NULL;
2574 if_com_free[type] = NULL;