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"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
62 #include <machine/stdarg.h>
66 #include <net/if_arp.h>
67 #include <net/if_clone.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
75 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/in6_ifattach.h>
85 #include <netinet/if_ether.h>
87 #if defined(INET) || defined(INET6)
89 #include <netinet/ip_carp.h>
93 #include <security/mac/mac_framework.h>
95 struct ifindex_entry {
96 struct ifnet *ife_ifnet;
99 static int slowtimo_started;
101 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
102 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
104 /* Log link state change events */
105 static int log_link_state_change = 1;
107 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
108 &log_link_state_change, 0,
109 "log interface link state change events");
111 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
112 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
113 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
115 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
118 * XXX: Style; these should be sorted alphabetically, and unprototyped
119 * static functions should be prototyped. Currently they are sorted by
122 static void if_attachdomain(void *);
123 static void if_attachdomain1(struct ifnet *);
124 static int ifconf(u_long, caddr_t);
125 static void if_freemulti(struct ifmultiaddr *);
126 static void if_init(void *);
127 static void if_check(void *);
128 static void if_route(struct ifnet *, int flag, int fam);
129 static int if_setflag(struct ifnet *, int, int, int *, int);
130 static void if_slowtimo(void *);
131 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
132 static void if_unroute(struct ifnet *, int flag, int fam);
133 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
134 static int if_rtdel(struct radix_node *, void *);
135 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
136 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
137 static void do_link_state_change(void *, int);
138 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
139 static int if_getgroupmembers(struct ifgroupreq *);
140 static void if_delgroups(struct ifnet *);
141 static void if_attach_internal(struct ifnet *, int);
142 static void if_detach_internal(struct ifnet *, int);
146 * XXX: declare here to avoid to include many inet6 related files..
147 * should be more generalized?
149 extern void nd6_setmtu(struct ifnet *);
152 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
153 VNET_DEFINE(struct ifgrouphead, ifg_head);
154 VNET_DEFINE(int, if_index);
155 static VNET_DEFINE(int, if_indexlim) = 8;
157 /* Table of ifnet by index. Locked with ifnet_lock. */
158 static VNET_DEFINE(struct ifindex_entry *, ifindex_table);
160 #define V_if_indexlim VNET(if_indexlim)
161 #define V_ifindex_table VNET(ifindex_table)
163 int ifqmaxlen = IFQ_MAXLEN;
164 struct rwlock ifnet_lock;
165 static if_com_alloc_t *if_com_alloc[256];
166 static if_com_free_t *if_com_free[256];
169 * System initialization
171 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
173 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
174 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
175 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
178 ifnet_byindex_locked(u_short idx)
181 if (idx > V_if_index)
183 return (V_ifindex_table[idx].ife_ifnet);
187 ifnet_byindex(u_short idx)
192 ifp = ifnet_byindex_locked(idx);
198 ifnet_byindex_ref(u_short idx)
203 ifp = ifnet_byindex_locked(idx);
204 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
214 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
217 IFNET_WLOCK_ASSERT();
219 V_ifindex_table[idx].ife_ifnet = ifp;
223 ifaddr_byindex(u_short idx)
228 ifa = ifnet_byindex_locked(idx)->if_addr;
236 * Network interface utility routines.
238 * Routines with ifa_ifwith* names take sockaddr *'s as
243 vnet_if_init(const void *unused __unused)
246 TAILQ_INIT(&V_ifnet);
247 TAILQ_INIT(&V_ifg_head);
248 if_grow(); /* create initial table */
249 vnet_if_clone_init();
251 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
256 if_init(void *dummy __unused)
262 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
267 vnet_if_uninit(const void *unused __unused)
270 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
271 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
273 free((caddr_t)V_ifindex_table, M_IFNET);
275 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
276 vnet_if_uninit, NULL);
283 struct ifindex_entry *e;
286 n = V_if_indexlim * sizeof(*e);
287 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
288 if (V_ifindex_table != NULL) {
289 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
290 free((caddr_t)V_ifindex_table, M_IFNET);
296 if_check(void *dummy __unused)
300 * If at least one interface added during boot uses
301 * if_watchdog then start the timer.
303 if (slowtimo_started)
308 * Allocate a struct ifnet and an index for an interface. A layer 2
309 * common structure will also be allocated if an allocation routine is
310 * registered for the passed type.
313 if_alloc(u_char type)
317 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
320 * Try to find an empty slot below if_index. If we fail, take
323 * XXX: should be locked!
325 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
326 if (ifnet_byindex(ifp->if_index) == NULL)
329 /* Catch if_index overflow. */
330 if (ifp->if_index < 1) {
334 if (ifp->if_index > V_if_index)
335 V_if_index = ifp->if_index;
336 if (V_if_index >= V_if_indexlim)
340 ifp->if_alloctype = type;
342 if (if_com_alloc[type] != NULL) {
343 ifp->if_l2com = if_com_alloc[type](type, ifp);
344 if (ifp->if_l2com == NULL) {
350 IF_ADDR_LOCK_INIT(ifp);
351 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
352 ifp->if_afdata_initialized = 0;
353 IF_AFDATA_LOCK_INIT(ifp);
354 TAILQ_INIT(&ifp->if_addrhead);
355 TAILQ_INIT(&ifp->if_prefixhead);
356 TAILQ_INIT(&ifp->if_multiaddrs);
357 TAILQ_INIT(&ifp->if_groups);
361 ifq_init(&ifp->if_snd, ifp);
363 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
365 ifnet_setbyindex(ifp->if_index, ifp);
371 * Do the actual work of freeing a struct ifnet, associated index, and layer
372 * 2 common structure. This call is made when the last reference to an
373 * interface is released.
376 if_free_internal(struct ifnet *ifp)
379 KASSERT((ifp->if_flags & IFF_DYING),
380 ("if_free_internal: interface not dying"));
383 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
384 ("%s: freeing unallocated ifnet", ifp->if_xname));
386 ifnet_setbyindex(ifp->if_index, NULL);
387 while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL)
391 if (if_com_free[ifp->if_alloctype] != NULL)
392 if_com_free[ifp->if_alloctype](ifp->if_l2com,
396 mac_ifnet_destroy(ifp);
398 IF_AFDATA_DESTROY(ifp);
399 IF_ADDR_LOCK_DESTROY(ifp);
400 ifq_delete(&ifp->if_snd);
405 * This version should only be called by intefaces that switch their type
406 * after calling if_alloc(). if_free_type() will go away again now that we
407 * have if_alloctype to cache the original allocation type. For now, assert
408 * that they match, since we require that in practice.
411 if_free_type(struct ifnet *ifp, u_char type)
414 KASSERT(ifp->if_alloctype == type,
415 ("if_free_type: type (%d) != alloctype (%d)", type,
418 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
419 if (!refcount_release(&ifp->if_refcount))
421 if_free_internal(ifp);
425 * This is the normal version of if_free(), used by device drivers to free a
426 * detached network interface. The contents of if_free_type() will move into
427 * here when if_free_type() goes away.
430 if_free(struct ifnet *ifp)
433 if_free_type(ifp, ifp->if_alloctype);
437 * Interfaces to keep an ifnet type-stable despite the possibility of the
438 * driver calling if_free(). If there are additional references, we defer
439 * freeing the underlying data structure.
442 if_ref(struct ifnet *ifp)
445 /* We don't assert the ifnet list lock here, but arguably should. */
446 refcount_acquire(&ifp->if_refcount);
450 if_rele(struct ifnet *ifp)
453 if (!refcount_release(&ifp->if_refcount))
455 if_free_internal(ifp);
459 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
462 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
464 if (ifq->ifq_maxlen == 0)
465 ifq->ifq_maxlen = ifqmaxlen;
468 ifq->altq_disc = NULL;
469 ifq->altq_flags &= ALTQF_CANTCHANGE;
470 ifq->altq_tbr = NULL;
475 ifq_delete(struct ifaltq *ifq)
477 mtx_destroy(&ifq->ifq_mtx);
481 * Perform generic interface initalization tasks and attach the interface
482 * to the list of "active" interfaces. If vmove flag is set on entry
483 * to if_attach_internal(), perform only a limited subset of initialization
484 * tasks, given that we are moving from one vnet to another an ifnet which
485 * has already been fully initialized.
488 * - The decision to return void and thus require this function to
489 * succeed is questionable.
490 * - We should probably do more sanity checking. For instance we don't
491 * do anything to insure if_xname is unique or non-empty.
494 if_attach(struct ifnet *ifp)
497 if_attach_internal(ifp, 0);
501 if_attach_internal(struct ifnet *ifp, int vmove)
503 unsigned socksize, ifasize;
504 int namelen, masklen;
505 struct sockaddr_dl *sdl;
508 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
509 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
513 ifp->if_vnet = curvnet;
514 if (ifp->if_home_vnet == NULL)
515 ifp->if_home_vnet = curvnet;
518 if_addgroup(ifp, IFG_ALL);
520 getmicrotime(&ifp->if_lastchange);
521 ifp->if_data.ifi_epoch = time_uptime;
522 ifp->if_data.ifi_datalen = sizeof(struct if_data);
524 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
525 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
526 ("transmit and qflush must both either be set or both be NULL"));
527 if (ifp->if_transmit == NULL) {
528 ifp->if_transmit = if_transmit;
529 ifp->if_qflush = if_qflush;
534 mac_ifnet_create(ifp);
538 * Create a Link Level name for this device.
540 namelen = strlen(ifp->if_xname);
542 * Always save enough space for any possiable name so we
543 * can do a rename in place later.
545 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
546 socksize = masklen + ifp->if_addrlen;
547 if (socksize < sizeof(*sdl))
548 socksize = sizeof(*sdl);
549 socksize = roundup2(socksize, sizeof(long));
550 ifasize = sizeof(*ifa) + 2 * socksize;
551 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
553 sdl = (struct sockaddr_dl *)(ifa + 1);
554 sdl->sdl_len = socksize;
555 sdl->sdl_family = AF_LINK;
556 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
557 sdl->sdl_nlen = namelen;
558 sdl->sdl_index = ifp->if_index;
559 sdl->sdl_type = ifp->if_type;
562 ifa->ifa_rtrequest = link_rtrequest;
563 ifa->ifa_addr = (struct sockaddr *)sdl;
564 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
565 ifa->ifa_netmask = (struct sockaddr *)sdl;
566 sdl->sdl_len = masklen;
568 sdl->sdl_data[--namelen] = 0xff;
569 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
570 /* Reliably crash if used uninitialized. */
571 ifp->if_broadcastaddr = NULL;
575 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
577 curvnet->vnet_ifcnt++;
581 if (domain_init_status >= 2)
582 if_attachdomain1(ifp);
584 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
585 if (IS_DEFAULT_VNET(curvnet))
586 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
588 /* Announce the interface. */
589 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
591 if (!vmove && ifp->if_watchdog != NULL) {
593 "WARNING: using obsoleted if_watchdog interface\n");
596 * Note that we need if_slowtimo(). If this happens after
597 * boot, then call if_slowtimo() directly.
599 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
605 if_attachdomain(void *dummy)
611 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
612 if_attachdomain1(ifp);
615 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
616 if_attachdomain, NULL);
619 if_attachdomain1(struct ifnet *ifp)
627 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
628 * cannot lock ifp->if_afdata initialization, entirely.
630 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
634 if (ifp->if_afdata_initialized >= domain_init_status) {
635 IF_AFDATA_UNLOCK(ifp);
637 printf("if_attachdomain called more than once on %s\n",
641 ifp->if_afdata_initialized = domain_init_status;
642 IF_AFDATA_UNLOCK(ifp);
644 /* address family dependent data region */
645 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
646 for (dp = domains; dp; dp = dp->dom_next) {
647 if (dp->dom_ifattach)
648 ifp->if_afdata[dp->dom_family] =
649 (*dp->dom_ifattach)(ifp);
656 * Remove any unicast or broadcast network addresses from an interface.
659 if_purgeaddrs(struct ifnet *ifp)
661 struct ifaddr *ifa, *next;
663 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
664 if (ifa->ifa_addr->sa_family == AF_LINK)
667 /* XXX: Ugly!! ad hoc just for INET */
668 if (ifa->ifa_addr->sa_family == AF_INET) {
669 struct ifaliasreq ifr;
671 bzero(&ifr, sizeof(ifr));
672 ifr.ifra_addr = *ifa->ifa_addr;
673 if (ifa->ifa_dstaddr)
674 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
675 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
681 if (ifa->ifa_addr->sa_family == AF_INET6) {
683 /* ifp_addrhead is already updated */
687 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
693 * Remove any multicast network addresses from an interface.
696 if_purgemaddrs(struct ifnet *ifp)
698 struct ifmultiaddr *ifma;
699 struct ifmultiaddr *next;
702 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
703 if_delmulti_locked(ifp, ifma, 1);
708 * Detach an interface, removing it from the list of "active" interfaces.
709 * If vmove flag is set on entry to if_detach_internal(), perform only a
710 * limited subset of cleanup tasks, given that we are moving an ifnet from
711 * one vnet to another, where it must be fully operational.
713 * XXXRW: There are some significant questions about event ordering, and
714 * how to prevent things from starting to use the interface during detach.
717 if_detach(struct ifnet *ifp)
720 if_detach_internal(ifp, 0);
724 if_detach_internal(struct ifnet *ifp, int vmove)
727 struct radix_node_head *rnh;
734 TAILQ_FOREACH(iter, &V_ifnet, if_link)
736 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
742 curvnet->vnet_ifcnt--;
747 panic("interface not in it's own ifnet list");
749 return; /* XXX this should panic as well? */
753 * Remove/wait for pending events.
755 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
758 * Remove routes and flush queues.
762 if (ALTQ_IS_ENABLED(&ifp->if_snd))
763 altq_disable(&ifp->if_snd);
764 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
765 altq_detach(&ifp->if_snd);
776 * Remove all IPv6 kernel structs related to ifp. This should be done
777 * before removing routing entries below, since IPv6 interface direct
778 * routes are expected to be removed by the IPv6-specific kernel API.
779 * Otherwise, the kernel will detect some inconsistency and bark it.
787 * Prevent further calls into the device driver via ifnet.
792 * Remove link ifaddr pointer and maybe decrement if_index.
793 * Clean up all addresses.
797 /* We can now free link ifaddr. */
798 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
799 ifa = TAILQ_FIRST(&ifp->if_addrhead);
800 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
806 * Delete all remaining routes using this interface
807 * Unfortuneatly the only way to do this is to slog through
808 * the entire routing table looking for routes which point
809 * to this interface...oh well...
811 for (i = 1; i <= AF_MAX; i++) {
812 for (j = 0; j < rt_numfibs; j++) {
813 rnh = rt_tables_get_rnh(j, i);
816 RADIX_NODE_HEAD_LOCK(rnh);
817 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
818 RADIX_NODE_HEAD_UNLOCK(rnh);
822 /* Announce that the interface is gone. */
823 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
824 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
825 if (IS_DEFAULT_VNET(curvnet))
826 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
830 for (dp = domains; dp; dp = dp->dom_next) {
831 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
832 (*dp->dom_ifdetach)(ifp,
833 ifp->if_afdata[dp->dom_family]);
835 ifp->if_afdata_initialized = 0;
836 IF_AFDATA_UNLOCK(ifp);
841 * if_vmove() performs a limited version of if_detach() in current
842 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
843 * An attempt is made to shrink if_index in current vnet, find an
844 * unused if_index in target vnet and calls if_grow() if necessary,
845 * and finally find an unused if_xname for the target vnet.
848 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
852 * Detach from current vnet, but preserve LLADDR info, do not
853 * mark as dead etc. so that the ifnet can be reattached later.
855 if_detach_internal(ifp, 1);
858 * Unlink the ifnet from ifindex_table[] in current vnet,
859 * and shrink the if_index for that vnet if possible.
862 ifnet_setbyindex(ifp->if_index, NULL);
863 while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL)
868 * Switch to the context of the target vnet.
870 CURVNET_SET_QUIET(new_vnet);
873 * Try to find an empty slot below if_index. If we fail, take
877 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
878 if (ifnet_byindex_locked(ifp->if_index) == NULL)
881 /* Catch if_index overflow. */
882 if (ifp->if_index < 1)
883 panic("if_index overflow");
885 if (ifp->if_index > V_if_index)
886 V_if_index = ifp->if_index;
887 if (V_if_index >= V_if_indexlim)
889 ifnet_setbyindex(ifp->if_index, ifp);
892 if_attach_internal(ifp, 1);
898 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
901 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
906 /* Try to find the prison within our visibility. */
907 sx_slock(&allprison_lock);
908 pr = prison_find_child(td->td_ucred->cr_prison, jid);
909 sx_sunlock(&allprison_lock);
912 prison_hold_locked(pr);
913 mtx_unlock(&pr->pr_mtx);
915 /* Do not try to move the iface from and to the same prison. */
916 if (pr->pr_vnet == ifp->if_vnet) {
921 /* Make sure the named iface does not exists in the dst. prison/vnet. */
922 /* XXX Lock interfaces to avoid races. */
923 CURVNET_SET(pr->pr_vnet);
924 difp = ifunit(ifname);
931 /* Move the interface into the child jail/vnet. */
932 if_vmove(ifp, pr->pr_vnet);
934 /* Report the new if_xname back to the userland. */
935 sprintf(ifname, "%s", ifp->if_xname);
942 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
945 struct vnet *vnet_dst;
948 /* Try to find the prison within our visibility. */
949 sx_slock(&allprison_lock);
950 pr = prison_find_child(td->td_ucred->cr_prison, jid);
951 sx_sunlock(&allprison_lock);
954 prison_hold_locked(pr);
955 mtx_unlock(&pr->pr_mtx);
957 /* Make sure the named iface exists in the source prison/vnet. */
958 CURVNET_SET(pr->pr_vnet);
959 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
966 /* Do not try to move the iface from and to the same prison. */
967 vnet_dst = TD_TO_VNET(td);
968 if (vnet_dst == ifp->if_vnet) {
974 /* Get interface back from child jail/vnet. */
975 if_vmove(ifp, vnet_dst);
978 /* Report the new if_xname back to the userland. */
979 sprintf(ifname, "%s", ifp->if_xname);
987 * Add a group to an interface
990 if_addgroup(struct ifnet *ifp, const char *groupname)
992 struct ifg_list *ifgl;
993 struct ifg_group *ifg = NULL;
994 struct ifg_member *ifgm;
996 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
997 groupname[strlen(groupname) - 1] <= '9')
1001 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1002 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1007 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1008 M_NOWAIT)) == NULL) {
1013 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1014 M_TEMP, M_NOWAIT)) == NULL) {
1020 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1021 if (!strcmp(ifg->ifg_group, groupname))
1025 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1026 M_TEMP, M_NOWAIT)) == NULL) {
1032 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1033 ifg->ifg_refcnt = 0;
1034 TAILQ_INIT(&ifg->ifg_members);
1035 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1036 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1040 ifgl->ifgl_group = ifg;
1041 ifgm->ifgm_ifp = ifp;
1044 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1045 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1046 IF_ADDR_UNLOCK(ifp);
1050 EVENTHANDLER_INVOKE(group_change_event, groupname);
1056 * Remove a group from an interface
1059 if_delgroup(struct ifnet *ifp, const char *groupname)
1061 struct ifg_list *ifgl;
1062 struct ifg_member *ifgm;
1065 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1066 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1074 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1075 IF_ADDR_UNLOCK(ifp);
1077 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1078 if (ifgm->ifgm_ifp == ifp)
1082 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1086 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1087 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1088 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1089 free(ifgl->ifgl_group, M_TEMP);
1095 EVENTHANDLER_INVOKE(group_change_event, groupname);
1101 * Remove an interface from all groups
1104 if_delgroups(struct ifnet *ifp)
1106 struct ifg_list *ifgl;
1107 struct ifg_member *ifgm;
1108 char groupname[IFNAMSIZ];
1111 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1112 ifgl = TAILQ_FIRST(&ifp->if_groups);
1114 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1117 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1118 IF_ADDR_UNLOCK(ifp);
1120 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1121 if (ifgm->ifgm_ifp == ifp)
1125 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1130 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1131 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1132 EVENTHANDLER_INVOKE(group_detach_event,
1134 free(ifgl->ifgl_group, M_TEMP);
1140 EVENTHANDLER_INVOKE(group_change_event, groupname);
1148 * Stores all groups from an interface in memory pointed
1152 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1155 struct ifg_list *ifgl;
1156 struct ifg_req ifgrq, *ifgp;
1157 struct ifgroupreq *ifgr = data;
1159 if (ifgr->ifgr_len == 0) {
1161 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1162 ifgr->ifgr_len += sizeof(struct ifg_req);
1163 IF_ADDR_UNLOCK(ifp);
1167 len = ifgr->ifgr_len;
1168 ifgp = ifgr->ifgr_groups;
1171 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1172 if (len < sizeof(ifgrq)) {
1173 IF_ADDR_UNLOCK(ifp);
1176 bzero(&ifgrq, sizeof ifgrq);
1177 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1178 sizeof(ifgrq.ifgrq_group));
1179 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1180 IF_ADDR_UNLOCK(ifp);
1183 len -= sizeof(ifgrq);
1186 IF_ADDR_UNLOCK(ifp);
1192 * Stores all members of a group in memory pointed to by data
1195 if_getgroupmembers(struct ifgroupreq *data)
1197 struct ifgroupreq *ifgr = data;
1198 struct ifg_group *ifg;
1199 struct ifg_member *ifgm;
1200 struct ifg_req ifgrq, *ifgp;
1204 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1205 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1212 if (ifgr->ifgr_len == 0) {
1213 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1214 ifgr->ifgr_len += sizeof(ifgrq);
1219 len = ifgr->ifgr_len;
1220 ifgp = ifgr->ifgr_groups;
1221 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1222 if (len < sizeof(ifgrq)) {
1226 bzero(&ifgrq, sizeof ifgrq);
1227 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1228 sizeof(ifgrq.ifgrq_member));
1229 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1233 len -= sizeof(ifgrq);
1242 * Delete Routes for a Network Interface
1244 * Called for each routing entry via the rnh->rnh_walktree() call above
1245 * to delete all route entries referencing a detaching network interface.
1248 * rn pointer to node in the routing table
1249 * arg argument passed to rnh->rnh_walktree() - detaching interface
1253 * errno failed - reason indicated
1257 if_rtdel(struct radix_node *rn, void *arg)
1259 struct rtentry *rt = (struct rtentry *)rn;
1260 struct ifnet *ifp = arg;
1263 if (rt->rt_ifp == ifp) {
1266 * Protect (sorta) against walktree recursion problems
1267 * with cloned routes
1269 if ((rt->rt_flags & RTF_UP) == 0)
1272 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1273 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1274 (struct rtentry **) NULL, rt->rt_fibnum);
1276 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1284 * Wrapper functions for struct ifnet address list locking macros. These are
1285 * used by kernel modules to avoid encoding programming interface or binary
1286 * interface assumptions that may be violated when kernel-internal locking
1287 * approaches change.
1290 if_addr_rlock(struct ifnet *ifp)
1297 if_addr_runlock(struct ifnet *ifp)
1300 IF_ADDR_UNLOCK(ifp);
1304 if_maddr_rlock(struct ifnet *ifp)
1311 if_maddr_runlock(struct ifnet *ifp)
1314 IF_ADDR_UNLOCK(ifp);
1318 * Reference count functions for ifaddrs.
1321 ifa_init(struct ifaddr *ifa)
1324 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1325 refcount_init(&ifa->ifa_refcnt, 1);
1329 ifa_ref(struct ifaddr *ifa)
1332 refcount_acquire(&ifa->ifa_refcnt);
1336 ifa_free(struct ifaddr *ifa)
1339 if (refcount_release(&ifa->ifa_refcnt)) {
1340 mtx_destroy(&ifa->ifa_mtx);
1341 free(ifa, M_IFADDR);
1346 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1347 * structs used to represent other address families, it is necessary
1348 * to perform a different comparison.
1351 #define sa_equal(a1, a2) \
1352 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1354 #define sa_dl_equal(a1, a2) \
1355 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1356 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1357 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1358 LLADDR((struct sockaddr_dl *)(a2)), \
1359 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1362 * Locate an interface based on a complete address.
1365 static struct ifaddr *
1366 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1372 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1374 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1375 if (ifa->ifa_addr->sa_family != addr->sa_family)
1377 if (sa_equal(addr, ifa->ifa_addr)) {
1380 IF_ADDR_UNLOCK(ifp);
1383 /* IP6 doesn't have broadcast */
1384 if ((ifp->if_flags & IFF_BROADCAST) &&
1385 ifa->ifa_broadaddr &&
1386 ifa->ifa_broadaddr->sa_len != 0 &&
1387 sa_equal(ifa->ifa_broadaddr, addr)) {
1390 IF_ADDR_UNLOCK(ifp);
1394 IF_ADDR_UNLOCK(ifp);
1403 ifa_ifwithaddr(struct sockaddr *addr)
1406 return (ifa_ifwithaddr_internal(addr, 1));
1410 ifa_ifwithaddr_check(struct sockaddr *addr)
1413 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1417 * Locate an interface based on the broadcast address.
1421 ifa_ifwithbroadaddr(struct sockaddr *addr)
1427 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1429 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1430 if (ifa->ifa_addr->sa_family != addr->sa_family)
1432 if ((ifp->if_flags & IFF_BROADCAST) &&
1433 ifa->ifa_broadaddr &&
1434 ifa->ifa_broadaddr->sa_len != 0 &&
1435 sa_equal(ifa->ifa_broadaddr, addr)) {
1437 IF_ADDR_UNLOCK(ifp);
1441 IF_ADDR_UNLOCK(ifp);
1450 * Locate the point to point interface with a given destination address.
1454 ifa_ifwithdstaddr(struct sockaddr *addr)
1460 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1461 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1464 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1465 if (ifa->ifa_addr->sa_family != addr->sa_family)
1467 if (ifa->ifa_dstaddr != NULL &&
1468 sa_equal(addr, ifa->ifa_dstaddr)) {
1470 IF_ADDR_UNLOCK(ifp);
1474 IF_ADDR_UNLOCK(ifp);
1483 * Find an interface on a specific network. If many, choice
1484 * is most specific found.
1487 ifa_ifwithnet(struct sockaddr *addr)
1491 struct ifaddr *ifa_maybe = NULL;
1492 u_int af = addr->sa_family;
1493 char *addr_data = addr->sa_data, *cplim;
1496 * AF_LINK addresses can be looked up directly by their index number,
1497 * so do that if we can.
1499 if (af == AF_LINK) {
1500 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1501 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1502 return (ifaddr_byindex(sdl->sdl_index));
1506 * Scan though each interface, looking for ones that have addresses
1507 * in this address family. Maintain a reference on ifa_maybe once
1508 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1509 * when we move onto the next interface.
1512 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1514 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1515 char *cp, *cp2, *cp3;
1517 if (ifa->ifa_addr->sa_family != af)
1519 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1521 * This is a bit broken as it doesn't
1522 * take into account that the remote end may
1523 * be a single node in the network we are
1525 * The trouble is that we don't know the
1526 * netmask for the remote end.
1528 if (ifa->ifa_dstaddr != NULL &&
1529 sa_equal(addr, ifa->ifa_dstaddr)) {
1531 IF_ADDR_UNLOCK(ifp);
1536 * if we have a special address handler,
1537 * then use it instead of the generic one.
1539 if (ifa->ifa_claim_addr) {
1540 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1542 IF_ADDR_UNLOCK(ifp);
1549 * Scan all the bits in the ifa's address.
1550 * If a bit dissagrees with what we are
1551 * looking for, mask it with the netmask
1552 * to see if it really matters.
1553 * (A byte at a time)
1555 if (ifa->ifa_netmask == 0)
1558 cp2 = ifa->ifa_addr->sa_data;
1559 cp3 = ifa->ifa_netmask->sa_data;
1560 cplim = ifa->ifa_netmask->sa_len
1561 + (char *)ifa->ifa_netmask;
1563 if ((*cp++ ^ *cp2++) & *cp3++)
1564 goto next; /* next address! */
1566 * If the netmask of what we just found
1567 * is more specific than what we had before
1568 * (if we had one) then remember the new one
1569 * before continuing to search
1570 * for an even better one.
1572 if (ifa_maybe == NULL ||
1573 rn_refines((caddr_t)ifa->ifa_netmask,
1574 (caddr_t)ifa_maybe->ifa_netmask)) {
1575 if (ifa_maybe != NULL)
1576 ifa_free(ifa_maybe);
1582 IF_ADDR_UNLOCK(ifp);
1588 if (ifa_maybe != NULL)
1589 ifa_free(ifa_maybe);
1594 * Find an interface address specific to an interface best matching
1598 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1601 char *cp, *cp2, *cp3;
1603 struct ifaddr *ifa_maybe = NULL;
1604 u_int af = addr->sa_family;
1609 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1610 if (ifa->ifa_addr->sa_family != af)
1612 if (ifa_maybe == NULL)
1614 if (ifa->ifa_netmask == 0) {
1615 if (sa_equal(addr, ifa->ifa_addr) ||
1616 (ifa->ifa_dstaddr &&
1617 sa_equal(addr, ifa->ifa_dstaddr)))
1621 if (ifp->if_flags & IFF_POINTOPOINT) {
1622 if (sa_equal(addr, ifa->ifa_dstaddr))
1626 cp2 = ifa->ifa_addr->sa_data;
1627 cp3 = ifa->ifa_netmask->sa_data;
1628 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1629 for (; cp3 < cplim; cp3++)
1630 if ((*cp++ ^ *cp2++) & *cp3)
1640 IF_ADDR_UNLOCK(ifp);
1644 #include <net/if_llatbl.h>
1647 * Default action when installing a route with a Link Level gateway.
1648 * Lookup an appropriate real ifa to point to.
1649 * This should be moved to /sys/net/link.c eventually.
1652 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1654 struct ifaddr *ifa, *oifa;
1655 struct sockaddr *dst;
1660 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1661 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1663 ifa = ifaof_ifpforaddr(dst, ifp);
1668 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1669 ifa->ifa_rtrequest(cmd, rt, info);
1674 * Mark an interface down and notify protocols of
1676 * NOTE: must be called at splnet or eqivalent.
1679 if_unroute(struct ifnet *ifp, int flag, int fam)
1683 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1685 ifp->if_flags &= ~flag;
1686 getmicrotime(&ifp->if_lastchange);
1687 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1688 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1689 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1690 ifp->if_qflush(ifp);
1692 #if defined(INET) || defined(INET6)
1695 carp_carpdev_state(ifp->if_carp);
1702 * Mark an interface up and notify protocols of
1704 * NOTE: must be called at splnet or eqivalent.
1707 if_route(struct ifnet *ifp, int flag, int fam)
1711 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1713 ifp->if_flags |= flag;
1714 getmicrotime(&ifp->if_lastchange);
1715 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1716 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1717 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1718 #if defined(INET) || defined(INET6)
1721 carp_carpdev_state(ifp->if_carp);
1730 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1731 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1734 * Handle a change in the interface link state. To avoid LORs
1735 * between driver lock and upper layer locks, as well as possible
1736 * recursions, we post event to taskqueue, and all job
1737 * is done in static do_link_state_change().
1740 if_link_state_change(struct ifnet *ifp, int link_state)
1742 /* Return if state hasn't changed. */
1743 if (ifp->if_link_state == link_state)
1746 ifp->if_link_state = link_state;
1748 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1752 do_link_state_change(void *arg, int pending)
1754 struct ifnet *ifp = (struct ifnet *)arg;
1755 int link_state = ifp->if_link_state;
1757 CURVNET_SET(ifp->if_vnet);
1759 /* Notify that the link state has changed. */
1761 if (link_state == LINK_STATE_UP)
1763 else if (link_state == LINK_STATE_DOWN)
1764 link = NOTE_LINKDOWN;
1766 link = NOTE_LINKINV;
1767 if (ifp->if_vlantrunk != NULL)
1768 (*vlan_link_state_p)(ifp, link);
1770 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1771 IFP2AC(ifp)->ac_netgraph != NULL)
1772 (*ng_ether_link_state_p)(ifp, link_state);
1773 #if defined(INET) || defined(INET6)
1776 carp_carpdev_state(ifp->if_carp);
1779 if (ifp->if_bridge) {
1780 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1781 (*bstp_linkstate_p)(ifp, link_state);
1784 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1785 (*lagg_linkstate_p)(ifp, link_state);
1788 if (IS_DEFAULT_VNET(curvnet))
1789 devctl_notify("IFNET", ifp->if_xname,
1790 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1793 if_printf(ifp, "%d link states coalesced\n", pending);
1794 if (log_link_state_change)
1795 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1796 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1801 * Mark an interface down and notify protocols of
1803 * NOTE: must be called at splnet or eqivalent.
1806 if_down(struct ifnet *ifp)
1809 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1813 * Mark an interface up and notify protocols of
1815 * NOTE: must be called at splnet or eqivalent.
1818 if_up(struct ifnet *ifp)
1821 if_route(ifp, IFF_UP, AF_UNSPEC);
1825 * Flush an interface queue.
1828 if_qflush(struct ifnet *ifp)
1836 if (ALTQ_IS_ENABLED(ifq))
1840 while ((m = n) != 0) {
1851 * Handle interface watchdog timer routines. Called
1852 * from softclock, we decrement timers (if set) and
1853 * call the appropriate interface routine on expiration.
1855 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1859 if_slowtimo(void *arg)
1861 VNET_ITERATOR_DECL(vnet_iter);
1865 VNET_LIST_RLOCK_NOSLEEP();
1867 VNET_FOREACH(vnet_iter) {
1868 CURVNET_SET(vnet_iter);
1869 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1870 if (ifp->if_timer == 0 || --ifp->if_timer)
1872 if (ifp->if_watchdog)
1873 (*ifp->if_watchdog)(ifp);
1878 VNET_LIST_RUNLOCK_NOSLEEP();
1880 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1884 * Map interface name to interface structure pointer, with or without
1885 * returning a reference.
1888 ifunit_ref(const char *name)
1893 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1894 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
1895 !(ifp->if_flags & IFF_DYING))
1905 ifunit(const char *name)
1910 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1911 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1919 * Hardware specific interface ioctls.
1922 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1927 int new_flags, temp_flags;
1928 size_t namelen, onamelen;
1929 char new_name[IFNAMSIZ];
1931 struct sockaddr_dl *sdl;
1933 ifr = (struct ifreq *)data;
1936 ifr->ifr_index = ifp->if_index;
1940 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1941 ifr->ifr_flags = temp_flags & 0xffff;
1942 ifr->ifr_flagshigh = temp_flags >> 16;
1946 ifr->ifr_reqcap = ifp->if_capabilities;
1947 ifr->ifr_curcap = ifp->if_capenable;
1952 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
1957 ifr->ifr_metric = ifp->if_metric;
1961 ifr->ifr_mtu = ifp->if_mtu;
1965 ifr->ifr_phys = ifp->if_physical;
1969 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1973 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1974 * check, so we don't need special handling here yet.
1976 new_flags = (ifr->ifr_flags & 0xffff) |
1977 (ifr->ifr_flagshigh << 16);
1978 if (ifp->if_flags & IFF_SMART) {
1979 /* Smart drivers twiddle their own routes */
1980 } else if (ifp->if_flags & IFF_UP &&
1981 (new_flags & IFF_UP) == 0) {
1985 } else if (new_flags & IFF_UP &&
1986 (ifp->if_flags & IFF_UP) == 0) {
1991 /* See if permanently promiscuous mode bit is about to flip */
1992 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1993 if (new_flags & IFF_PPROMISC)
1994 ifp->if_flags |= IFF_PROMISC;
1995 else if (ifp->if_pcount == 0)
1996 ifp->if_flags &= ~IFF_PROMISC;
1997 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1999 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2001 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2002 (new_flags &~ IFF_CANTCHANGE);
2003 if (ifp->if_ioctl) {
2004 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2006 getmicrotime(&ifp->if_lastchange);
2010 error = priv_check(td, PRIV_NET_SETIFCAP);
2013 if (ifp->if_ioctl == NULL)
2014 return (EOPNOTSUPP);
2015 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2017 error = (*ifp->if_ioctl)(ifp, cmd, data);
2019 getmicrotime(&ifp->if_lastchange);
2024 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2029 error = priv_check(td, PRIV_NET_SETIFNAME);
2032 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2035 if (new_name[0] == '\0')
2037 if (ifunit(new_name) != NULL)
2040 /* Announce the departure of the interface. */
2041 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2042 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2044 log(LOG_INFO, "%s: changing name to '%s'\n",
2045 ifp->if_xname, new_name);
2047 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2050 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2051 namelen = strlen(new_name);
2052 onamelen = sdl->sdl_nlen;
2054 * Move the address if needed. This is safe because we
2055 * allocate space for a name of length IFNAMSIZ when we
2056 * create this in if_attach().
2058 if (namelen != onamelen) {
2059 bcopy(sdl->sdl_data + onamelen,
2060 sdl->sdl_data + namelen, sdl->sdl_alen);
2062 bcopy(new_name, sdl->sdl_data, namelen);
2063 sdl->sdl_nlen = namelen;
2064 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2065 bzero(sdl->sdl_data, onamelen);
2066 while (namelen != 0)
2067 sdl->sdl_data[--namelen] = 0xff;
2070 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2071 /* Announce the return of the interface. */
2072 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2077 error = priv_check(td, PRIV_NET_SETIFVNET);
2080 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2085 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2088 ifp->if_metric = ifr->ifr_metric;
2089 getmicrotime(&ifp->if_lastchange);
2093 error = priv_check(td, PRIV_NET_SETIFPHYS);
2096 if (ifp->if_ioctl == NULL)
2097 return (EOPNOTSUPP);
2098 error = (*ifp->if_ioctl)(ifp, cmd, data);
2100 getmicrotime(&ifp->if_lastchange);
2105 u_long oldmtu = ifp->if_mtu;
2107 error = priv_check(td, PRIV_NET_SETIFMTU);
2110 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2112 if (ifp->if_ioctl == NULL)
2113 return (EOPNOTSUPP);
2114 error = (*ifp->if_ioctl)(ifp, cmd, data);
2116 getmicrotime(&ifp->if_lastchange);
2120 * If the link MTU changed, do network layer specific procedure.
2122 if (ifp->if_mtu != oldmtu) {
2132 if (cmd == SIOCADDMULTI)
2133 error = priv_check(td, PRIV_NET_ADDMULTI);
2135 error = priv_check(td, PRIV_NET_DELMULTI);
2139 /* Don't allow group membership on non-multicast interfaces. */
2140 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2141 return (EOPNOTSUPP);
2143 /* Don't let users screw up protocols' entries. */
2144 if (ifr->ifr_addr.sa_family != AF_LINK)
2147 if (cmd == SIOCADDMULTI) {
2148 struct ifmultiaddr *ifma;
2151 * Userland is only permitted to join groups once
2152 * via the if_addmulti() KPI, because it cannot hold
2153 * struct ifmultiaddr * between calls. It may also
2154 * lose a race while we check if the membership
2158 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2159 IF_ADDR_UNLOCK(ifp);
2163 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2165 error = if_delmulti(ifp, &ifr->ifr_addr);
2168 getmicrotime(&ifp->if_lastchange);
2171 case SIOCSIFPHYADDR:
2172 case SIOCDIFPHYADDR:
2174 case SIOCSIFPHYADDR_IN6:
2176 case SIOCSLIFPHYADDR:
2178 case SIOCSIFGENERIC:
2179 error = priv_check(td, PRIV_NET_HWIOCTL);
2182 if (ifp->if_ioctl == NULL)
2183 return (EOPNOTSUPP);
2184 error = (*ifp->if_ioctl)(ifp, cmd, data);
2186 getmicrotime(&ifp->if_lastchange);
2190 ifs = (struct ifstat *)data;
2191 ifs->ascii[0] = '\0';
2193 case SIOCGIFPSRCADDR:
2194 case SIOCGIFPDSTADDR:
2195 case SIOCGLIFPHYADDR:
2197 case SIOCGIFGENERIC:
2198 if (ifp->if_ioctl == NULL)
2199 return (EOPNOTSUPP);
2200 error = (*ifp->if_ioctl)(ifp, cmd, data);
2204 error = priv_check(td, PRIV_NET_SETLLADDR);
2207 error = if_setlladdr(ifp,
2208 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2213 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2215 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2218 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2224 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2230 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2232 error = priv_check(td, PRIV_NET_DELIFGROUP);
2235 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2251 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2264 return (ifconf(cmd, data));
2266 ifr = (struct ifreq *)data;
2271 error = priv_check(td, PRIV_NET_SETIFVNET);
2274 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2278 error = priv_check(td, PRIV_NET_IFCREATE);
2281 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2282 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2284 error = priv_check(td, PRIV_NET_IFDESTROY);
2287 return if_clone_destroy(ifr->ifr_name);
2289 case SIOCIFGCLONERS:
2290 return (if_clone_list((struct if_clonereq *)data));
2292 return (if_getgroupmembers((struct ifgroupreq *)data));
2295 ifp = ifunit_ref(ifr->ifr_name);
2299 error = ifhwioctl(cmd, ifp, data, td);
2300 if (error != ENOIOCTL) {
2305 oif_flags = ifp->if_flags;
2306 if (so->so_proto == NULL) {
2308 return (EOPNOTSUPP);
2311 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2314 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2315 error = (*ifp->if_ioctl)(ifp, cmd, data);
2322 case SIOCSIFDSTADDR:
2324 case SIOCSIFBRDADDR:
2325 case SIOCSIFNETMASK:
2326 #if BYTE_ORDER != BIG_ENDIAN
2327 if (ifr->ifr_addr.sa_family == 0 &&
2328 ifr->ifr_addr.sa_len < 16) {
2329 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2330 ifr->ifr_addr.sa_len = 16;
2333 if (ifr->ifr_addr.sa_len == 0)
2334 ifr->ifr_addr.sa_len = 16;
2342 case OSIOCGIFDSTADDR:
2343 cmd = SIOCGIFDSTADDR;
2346 case OSIOCGIFBRDADDR:
2347 cmd = SIOCGIFBRDADDR;
2350 case OSIOCGIFNETMASK:
2351 cmd = SIOCGIFNETMASK;
2353 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2357 if (error == EOPNOTSUPP && ifp != NULL &&
2358 ifp->if_ioctl != NULL)
2359 error = (*ifp->if_ioctl)(ifp, cmd, data);
2363 case OSIOCGIFDSTADDR:
2364 case OSIOCGIFBRDADDR:
2365 case OSIOCGIFNETMASK:
2366 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2370 #endif /* COMPAT_43 */
2372 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2374 if (ifp->if_flags & IFF_UP) {
2386 * The code common to handling reference counted flags,
2387 * e.g., in ifpromisc() and if_allmulti().
2388 * The "pflag" argument can specify a permanent mode flag to check,
2389 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2391 * Only to be used on stack-owned flags, not driver-owned flags.
2394 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2398 int oldflags, oldcount;
2400 /* Sanity checks to catch programming errors */
2401 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2402 ("%s: setting driver-owned flag %d", __func__, flag));
2405 KASSERT(*refcount >= 0,
2406 ("%s: increment negative refcount %d for flag %d",
2407 __func__, *refcount, flag));
2409 KASSERT(*refcount > 0,
2410 ("%s: decrement non-positive refcount %d for flag %d",
2411 __func__, *refcount, flag));
2413 /* In case this mode is permanent, just touch refcount */
2414 if (ifp->if_flags & pflag) {
2415 *refcount += onswitch ? 1 : -1;
2419 /* Save ifnet parameters for if_ioctl() may fail */
2420 oldcount = *refcount;
2421 oldflags = ifp->if_flags;
2424 * See if we aren't the only and touching refcount is enough.
2425 * Actually toggle interface flag if we are the first or last.
2430 ifp->if_flags |= flag;
2434 ifp->if_flags &= ~flag;
2437 /* Call down the driver since we've changed interface flags */
2438 if (ifp->if_ioctl == NULL) {
2442 ifr.ifr_flags = ifp->if_flags & 0xffff;
2443 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2444 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2447 /* Notify userland that interface flags have changed */
2452 /* Recover after driver error */
2453 *refcount = oldcount;
2454 ifp->if_flags = oldflags;
2459 * Set/clear promiscuous mode on interface ifp based on the truth value
2460 * of pswitch. The calls are reference counted so that only the first
2461 * "on" request actually has an effect, as does the final "off" request.
2462 * Results are undefined if the "off" and "on" requests are not matched.
2465 ifpromisc(struct ifnet *ifp, int pswitch)
2468 int oldflags = ifp->if_flags;
2470 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2471 &ifp->if_pcount, pswitch);
2472 /* If promiscuous mode status has changed, log a message */
2473 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2474 log(LOG_INFO, "%s: promiscuous mode %s\n",
2476 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2481 * Return interface configuration
2482 * of system. List may be used
2483 * in later ioctl's (above) to get
2484 * other information.
2488 ifconf(u_long cmd, caddr_t data)
2490 struct ifconf *ifc = (struct ifconf *)data;
2492 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2493 struct ifconf ifc_swab;
2499 int error, full = 0, valid_len, max_len;
2502 if (cmd == SIOCGIFCONF32) {
2503 ifc_swab.ifc_len = ifc32->ifc_len;
2504 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2508 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2509 max_len = MAXPHYS - 1;
2511 /* Prevent hostile input from being able to crash the system */
2512 if (ifc->ifc_len <= 0)
2516 if (ifc->ifc_len <= max_len) {
2517 max_len = ifc->ifc_len;
2520 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2524 IFNET_RLOCK(); /* could sleep XXX */
2525 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2529 * Zero the ifr_name buffer to make sure we don't
2530 * disclose the contents of the stack.
2532 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2534 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2535 >= sizeof(ifr.ifr_name)) {
2538 return (ENAMETOOLONG);
2543 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2544 struct sockaddr *sa = ifa->ifa_addr;
2546 if (prison_if(curthread->td_ucred, sa) != 0)
2550 if (cmd == OSIOCGIFCONF) {
2551 struct osockaddr *osa =
2552 (struct osockaddr *)&ifr.ifr_addr;
2554 osa->sa_family = sa->sa_family;
2555 sbuf_bcat(sb, &ifr, sizeof(ifr));
2556 max_len += sizeof(ifr);
2559 if (sa->sa_len <= sizeof(*sa)) {
2561 sbuf_bcat(sb, &ifr, sizeof(ifr));
2562 max_len += sizeof(ifr);
2565 offsetof(struct ifreq, ifr_addr));
2566 max_len += offsetof(struct ifreq, ifr_addr);
2567 sbuf_bcat(sb, sa, sa->sa_len);
2568 max_len += sa->sa_len;
2571 if (!sbuf_overflowed(sb))
2572 valid_len = sbuf_len(sb);
2574 IF_ADDR_UNLOCK(ifp);
2576 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2577 sbuf_bcat(sb, &ifr, sizeof(ifr));
2578 max_len += sizeof(ifr);
2580 if (!sbuf_overflowed(sb))
2581 valid_len = sbuf_len(sb);
2587 * If we didn't allocate enough space (uncommon), try again. If
2588 * we have already allocated as much space as we are allowed,
2589 * return what we've got.
2591 if (valid_len != max_len && !full) {
2596 ifc->ifc_len = valid_len;
2598 if (cmd == SIOCGIFCONF32)
2599 ifc32->ifc_len = valid_len;
2602 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2608 * Just like ifpromisc(), but for all-multicast-reception mode.
2611 if_allmulti(struct ifnet *ifp, int onswitch)
2614 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2617 struct ifmultiaddr *
2618 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2620 struct ifmultiaddr *ifma;
2622 IF_ADDR_LOCK_ASSERT(ifp);
2624 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2625 if (sa->sa_family == AF_LINK) {
2626 if (sa_dl_equal(ifma->ifma_addr, sa))
2629 if (sa_equal(ifma->ifma_addr, sa))
2638 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2639 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2640 * the ifnet multicast address list here, so the caller must do that and
2641 * other setup work (such as notifying the device driver). The reference
2642 * count is initialized to 1.
2644 static struct ifmultiaddr *
2645 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2648 struct ifmultiaddr *ifma;
2649 struct sockaddr *dupsa;
2651 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2656 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2657 if (dupsa == NULL) {
2658 free(ifma, M_IFMADDR);
2661 bcopy(sa, dupsa, sa->sa_len);
2662 ifma->ifma_addr = dupsa;
2664 ifma->ifma_ifp = ifp;
2665 ifma->ifma_refcount = 1;
2666 ifma->ifma_protospec = NULL;
2669 ifma->ifma_lladdr = NULL;
2673 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2674 if (dupsa == NULL) {
2675 free(ifma->ifma_addr, M_IFMADDR);
2676 free(ifma, M_IFMADDR);
2679 bcopy(llsa, dupsa, llsa->sa_len);
2680 ifma->ifma_lladdr = dupsa;
2686 * if_freemulti: free ifmultiaddr structure and possibly attached related
2687 * addresses. The caller is responsible for implementing reference
2688 * counting, notifying the driver, handling routing messages, and releasing
2689 * any dependent link layer state.
2692 if_freemulti(struct ifmultiaddr *ifma)
2695 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2696 ifma->ifma_refcount));
2697 KASSERT(ifma->ifma_protospec == NULL,
2698 ("if_freemulti: protospec not NULL"));
2700 if (ifma->ifma_lladdr != NULL)
2701 free(ifma->ifma_lladdr, M_IFMADDR);
2702 free(ifma->ifma_addr, M_IFMADDR);
2703 free(ifma, M_IFMADDR);
2707 * Register an additional multicast address with a network interface.
2709 * - If the address is already present, bump the reference count on the
2710 * address and return.
2711 * - If the address is not link-layer, look up a link layer address.
2712 * - Allocate address structures for one or both addresses, and attach to the
2713 * multicast address list on the interface. If automatically adding a link
2714 * layer address, the protocol address will own a reference to the link
2715 * layer address, to be freed when it is freed.
2716 * - Notify the network device driver of an addition to the multicast address
2719 * 'sa' points to caller-owned memory with the desired multicast address.
2721 * 'retifma' will be used to return a pointer to the resulting multicast
2722 * address reference, if desired.
2725 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2726 struct ifmultiaddr **retifma)
2728 struct ifmultiaddr *ifma, *ll_ifma;
2729 struct sockaddr *llsa;
2733 * If the address is already present, return a new reference to it;
2734 * otherwise, allocate storage and set up a new address.
2737 ifma = if_findmulti(ifp, sa);
2739 ifma->ifma_refcount++;
2740 if (retifma != NULL)
2742 IF_ADDR_UNLOCK(ifp);
2747 * The address isn't already present; resolve the protocol address
2748 * into a link layer address, and then look that up, bump its
2749 * refcount or allocate an ifma for that also. If 'llsa' was
2750 * returned, we will need to free it later.
2754 if (ifp->if_resolvemulti != NULL) {
2755 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2761 * Allocate the new address. Don't hook it up yet, as we may also
2762 * need to allocate a link layer multicast address.
2764 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2771 * If a link layer address is found, we'll need to see if it's
2772 * already present in the address list, or allocate is as well.
2773 * When this block finishes, the link layer address will be on the
2777 ll_ifma = if_findmulti(ifp, llsa);
2778 if (ll_ifma == NULL) {
2779 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2780 if (ll_ifma == NULL) {
2781 --ifma->ifma_refcount;
2786 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2789 ll_ifma->ifma_refcount++;
2790 ifma->ifma_llifma = ll_ifma;
2794 * We now have a new multicast address, ifma, and possibly a new or
2795 * referenced link layer address. Add the primary address to the
2796 * ifnet address list.
2798 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2800 if (retifma != NULL)
2804 * Must generate the message while holding the lock so that 'ifma'
2805 * pointer is still valid.
2807 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2808 IF_ADDR_UNLOCK(ifp);
2811 * We are certain we have added something, so call down to the
2812 * interface to let them know about it.
2814 if (ifp->if_ioctl != NULL) {
2815 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2819 free(llsa, M_IFMADDR);
2825 free(llsa, M_IFMADDR);
2828 IF_ADDR_UNLOCK(ifp);
2833 * Delete a multicast group membership by network-layer group address.
2835 * Returns ENOENT if the entry could not be found. If ifp no longer
2836 * exists, results are undefined. This entry point should only be used
2837 * from subsystems which do appropriate locking to hold ifp for the
2838 * duration of the call.
2839 * Network-layer protocol domains must use if_delmulti_ifma().
2842 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2844 struct ifmultiaddr *ifma;
2850 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2857 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2864 ifma = if_findmulti(ifp, sa);
2866 lastref = if_delmulti_locked(ifp, ifma, 0);
2867 IF_ADDR_UNLOCK(ifp);
2872 if (lastref && ifp->if_ioctl != NULL) {
2873 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2880 * Delete a multicast group membership by group membership pointer.
2881 * Network-layer protocol domains must use this routine.
2883 * It is safe to call this routine if the ifp disappeared.
2886 if_delmulti_ifma(struct ifmultiaddr *ifma)
2891 ifp = ifma->ifma_ifp;
2894 printf("%s: ifma_ifp seems to be detached\n", __func__);
2899 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2903 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2910 * If and only if the ifnet instance exists: Acquire the address lock.
2915 lastref = if_delmulti_locked(ifp, ifma, 0);
2919 * If and only if the ifnet instance exists:
2920 * Release the address lock.
2921 * If the group was left: update the hardware hash filter.
2923 IF_ADDR_UNLOCK(ifp);
2924 if (lastref && ifp->if_ioctl != NULL) {
2925 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2931 * Perform deletion of network-layer and/or link-layer multicast address.
2933 * Return 0 if the reference count was decremented.
2934 * Return 1 if the final reference was released, indicating that the
2935 * hardware hash filter should be reprogrammed.
2938 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2940 struct ifmultiaddr *ll_ifma;
2942 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2943 KASSERT(ifma->ifma_ifp == ifp,
2944 ("%s: inconsistent ifp %p", __func__, ifp));
2945 IF_ADDR_LOCK_ASSERT(ifp);
2948 ifp = ifma->ifma_ifp;
2951 * If the ifnet is detaching, null out references to ifnet,
2952 * so that upper protocol layers will notice, and not attempt
2953 * to obtain locks for an ifnet which no longer exists. The
2954 * routing socket announcement must happen before the ifnet
2955 * instance is detached from the system.
2959 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2962 * ifp may already be nulled out if we are being reentered
2963 * to delete the ll_ifma.
2966 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2967 ifma->ifma_ifp = NULL;
2971 if (--ifma->ifma_refcount > 0)
2975 * If this ifma is a network-layer ifma, a link-layer ifma may
2976 * have been associated with it. Release it first if so.
2978 ll_ifma = ifma->ifma_llifma;
2979 if (ll_ifma != NULL) {
2980 KASSERT(ifma->ifma_lladdr != NULL,
2981 ("%s: llifma w/o lladdr", __func__));
2983 ll_ifma->ifma_ifp = NULL; /* XXX */
2984 if (--ll_ifma->ifma_refcount == 0) {
2986 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2989 if_freemulti(ll_ifma);
2994 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2999 * The last reference to this instance of struct ifmultiaddr
3000 * was released; the hardware should be notified of this change.
3006 * Set the link layer address on an interface.
3008 * At this time we only support certain types of interfaces,
3009 * and we don't allow the length of the address to change.
3012 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3014 struct sockaddr_dl *sdl;
3021 IF_ADDR_UNLOCK(ifp);
3025 IF_ADDR_UNLOCK(ifp);
3026 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3031 if (len != sdl->sdl_alen) { /* don't allow length to change */
3035 switch (ifp->if_type) {
3043 case IFT_IEEE8023ADLAG:
3045 bcopy(lladdr, LLADDR(sdl), len);
3054 * If the interface is already up, we need
3055 * to re-init it in order to reprogram its
3058 if ((ifp->if_flags & IFF_UP) != 0) {
3059 if (ifp->if_ioctl) {
3060 ifp->if_flags &= ~IFF_UP;
3061 ifr.ifr_flags = ifp->if_flags & 0xffff;
3062 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3063 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3064 ifp->if_flags |= IFF_UP;
3065 ifr.ifr_flags = ifp->if_flags & 0xffff;
3066 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3067 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3071 * Also send gratuitous ARPs to notify other nodes about
3072 * the address change.
3074 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3075 if (ifa->ifa_addr->sa_family == AF_INET)
3076 arp_ifinit(ifp, ifa);
3084 * The name argument must be a pointer to storage which will last as
3085 * long as the interface does. For physical devices, the result of
3086 * device_get_name(dev) is a good choice and for pseudo-devices a
3087 * static string works well.
3090 if_initname(struct ifnet *ifp, const char *name, int unit)
3092 ifp->if_dname = name;
3093 ifp->if_dunit = unit;
3094 if (unit != IF_DUNIT_NONE)
3095 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3097 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3101 if_printf(struct ifnet *ifp, const char * fmt, ...)
3106 retval = printf("%s: ", ifp->if_xname);
3108 retval += vprintf(fmt, ap);
3114 if_start(struct ifnet *ifp)
3117 (*(ifp)->if_start)(ifp);
3121 * Backwards compatibility interface for drivers
3122 * that have not implemented it
3125 if_transmit(struct ifnet *ifp, struct mbuf *m)
3129 IFQ_HANDOFF(ifp, m, error);
3134 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3139 if (_IF_QFULL(ifq)) {
3146 ifp->if_obytes += m->m_pkthdr.len + adjust;
3147 if (m->m_flags & (M_BCAST|M_MCAST))
3149 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3151 _IF_ENQUEUE(ifq, m);
3153 if (ifp != NULL && !active)
3154 (*(ifp)->if_start)(ifp);
3159 if_register_com_alloc(u_char type,
3160 if_com_alloc_t *a, if_com_free_t *f)
3163 KASSERT(if_com_alloc[type] == NULL,
3164 ("if_register_com_alloc: %d already registered", type));
3165 KASSERT(if_com_free[type] == NULL,
3166 ("if_register_com_alloc: %d free already registered", type));
3168 if_com_alloc[type] = a;
3169 if_com_free[type] = f;
3173 if_deregister_com_alloc(u_char type)
3176 KASSERT(if_com_alloc[type] != NULL,
3177 ("if_deregister_com_alloc: %d not registered", type));
3178 KASSERT(if_com_free[type] != NULL,
3179 ("if_deregister_com_alloc: %d free not registered", type));
3180 if_com_alloc[type] = NULL;
3181 if_com_free[type] = NULL;