summaryrefslogtreecommitdiffstats
path: root/sys/net/if_var.h
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2009-04-21 22:43:32 +0000
committerrwatson <rwatson@FreeBSD.org>2009-04-21 22:43:32 +0000
commit6b19bec016f3ac4388638851255a7bec97b3a4ce (patch)
tree65917221387f491de45abd3774de9e0285405c38 /sys/net/if_var.h
parent08708b99cb0019857302bee2d0c9290bb75800c2 (diff)
downloadFreeBSD-src-6b19bec016f3ac4388638851255a7bec97b3a4ce.zip
FreeBSD-src-6b19bec016f3ac4388638851255a7bec97b3a4ce.tar.gz
Start to address a number of races relating to use of ifnet pointers
after the corresponding interface has been destroyed: (1) Add an ifnet refcount, ifp->if_refcount. Initialize it to 1 in if_alloc(), and modify if_free_type() to decrement and check the refcount. (2) Add new if_ref() and if_rele() interfaces to allow kernel code walking global interface lists to release IFNET_[RW]LOCK() yet keep the ifnet stable. Currently, if_rele() is a no-op wrapper around if_free(), but this may change in the future. (3) Add new ifnet field, if_alloctype, which caches the type passed to if_alloc(), but unlike if_type, won't be changed by drivers. This allows asynchronous free's of the interface after the driver has released it to still use the right type. Use that instead of the type passed to if_free_type(), but assert that they are the same (might have to rethink this if that doesn't work out). (4) Add a new ifnet_byindex_ref(), which looks up an interface by index and returns a reference rather than a pointer to it. (5) Fix if_alloc() to fully initialize the if_addr_mtx before hooking up the ifnet to global lists. (6) Modify sysctls in if_mib.c to use ifnet_byindex_ref() and release the ifnet when done. When this change is MFC'd, it will need to replace if_ispare fields rather than adding new fields in order to avoid breaking the binary interface. Once this change is MFC'd, if_free_type() should be removed, as its 'type' argument is now optional. This refcount is not appropriate for counting mbuf pkthdr references, and also not for counting entry into the device driver via ifnet function pointers. An rmlock may be appropriate for the latter. Rather, this is about ensuring data structure stability when reaching an ifnet via global ifnet lists and tables followed by copy in or out of userspace. MFC after: 3 weeks Reported by: mdtancsa Reviewed by: brooks
Diffstat (limited to 'sys/net/if_var.h')
-rw-r--r--sys/net/if_var.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/sys/net/if_var.h b/sys/net/if_var.h
index 4b7447d..26b2747 100644
--- a/sys/net/if_var.h
+++ b/sys/net/if_var.h
@@ -121,6 +121,7 @@ struct ifnet {
char if_xname[IFNAMSIZ]; /* external name (name + unit) */
const char *if_dname; /* driver name */
int if_dunit; /* unit or IF_DUNIT_NONE */
+ u_int if_refcount; /* reference count */
struct ifaddrhead if_addrhead; /* linked list of addresses per if */
/*
* if_addrhead is the list of all addresses associated to
@@ -190,12 +191,14 @@ struct ifnet {
/* protected by if_addr_mtx */
void *if_pf_kif;
void *if_lagg; /* lagg glue */
+ u_char if_alloctype; /* if_type at time of allocation */
/*
* Spare fields are added so that we can modify sensitive data
* structures without changing the kernel binary interface, and must
* be used with care where binary compatibility is required.
*/
+ char if_cspare[3];
void *if_pspare[8];
int if_ispare[4];
};
@@ -721,7 +724,13 @@ struct ifindex_entry {
struct cdev *ife_dev;
};
+/*
+ * Look up an ifnet given its index; the _ref variant also acquires a
+ * reference that must be freed using if_rele(). It is almost always a bug
+ * to call ifnet_byindex() instead if ifnet_byindex_ref().
+ */
struct ifnet *ifnet_byindex(u_short idx);
+struct ifnet *ifnet_byindex_ref(u_short idx);
/*
* Given the index, ifaddr_byindex() returns the one and only
@@ -758,6 +767,8 @@ void if_initname(struct ifnet *, const char *, int);
void if_link_state_change(struct ifnet *, int);
int if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
void if_qflush(struct ifnet *);
+void if_ref(struct ifnet *);
+void if_rele(struct ifnet *);
int if_setlladdr(struct ifnet *, const u_char *, int);
void if_up(struct ifnet *);
/*void ifinit(void);*/ /* declared in systm.h for main() */
OpenPOWER on IntegriCloud