Index: net/if.c =================================================================== RCS file: /cvs/src/sys/net/if.c,v retrieving revision 1.363 diff -u -p -r1.363 if.c --- net/if.c 1 Sep 2015 04:56:55 -0000 1.363 +++ net/if.c 9 Sep 2015 14:59:59 -0000 @@ -80,6 +80,7 @@ #include #include #include +#include #include @@ -260,7 +261,7 @@ if_attachsetup(struct ifnet *ifp) if_addgroup(ifp, IFG_ALL); - ifindex2ifnet[if_index] = ifp; + ifindex2ifnet[if_index] = if_ref(ifp); if (ifp->if_snd.ifq_maxlen == 0) IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); @@ -664,6 +665,7 @@ if_detach(struct ifnet *ifp) rt_ifannouncemsg(ifp, IFAN_DEPARTURE); ifindex2ifnet[ifp->if_index] = NULL; + if_put(ifp); splx(s); } @@ -1231,7 +1233,23 @@ if_get(unsigned int index) if (index < if_indexlim) ifp = ifindex2ifnet[index]; + return (if_ref(ifp)); +} + +struct ifnet * +if_ref(struct ifnet *ifp) +{ + atomic_inc_int(&ifp->if_refs); return (ifp); +} + +void +if_put(struct ifnet *ifp) +{ + if (ifp == NULL) + return; + + atomic_dec_int(&ifp->if_refs); } /* Index: net/if.h =================================================================== RCS file: /cvs/src/sys/net/if.h,v retrieving revision 1.165 diff -u -p -r1.165 if.h --- net/if.h 30 Aug 2015 10:39:16 -0000 1.165 +++ net/if.h 9 Sep 2015 14:59:59 -0000 @@ -462,6 +462,8 @@ int if_delgroup(struct ifnet *, const ch void if_group_routechange(struct sockaddr *, struct sockaddr *); struct ifnet *ifunit(const char *); struct ifnet *if_get(unsigned int); +struct ifnet *if_ref(struct ifnet *); +void if_put(struct ifnet *); void ifnewlladdr(struct ifnet *); void if_congestion(void); int if_congested(void); Index: net/if_var.h =================================================================== RCS file: /cvs/src/sys/net/if_var.h,v retrieving revision 1.34 diff -u -p -r1.34 if_var.h --- net/if_var.h 2 Jul 2015 09:40:02 -0000 1.34 +++ net/if_var.h 9 Sep 2015 14:59:59 -0000 @@ -127,6 +127,7 @@ TAILQ_HEAD(ifnet_head, ifnet); /* the a struct ifnet { /* and the entries */ void *if_softc; /* lower-level data for this if */ + u_int if_refs; TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */ TAILQ_ENTRY(ifnet) if_txlist; /* list of ifnets ready to tx */ TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */ @@ -147,7 +148,7 @@ struct ifnet { /* and the entries */ } if_carp_ptr; #define if_carp if_carp_ptr.carp_s #define if_carpdev if_carp_ptr.carp_d - u_short if_index; /* numeric abbreviation for this if */ + u_int if_index; /* numeric abbreviation for this if */ short if_timer; /* time 'til if_watchdog called */ short if_flags; /* up/down, broadcast, etc. */ int if_xflags; /* extra softnet flags */ @@ -232,7 +233,7 @@ struct ifaddr { */ struct ifmaddr { struct sockaddr *ifma_addr; /* Protocol address */ - unsigned short ifma_ifidx; /* Index of the interface */ + u_int ifma_ifidx; /* Index of the interface */ unsigned int ifma_refcnt; /* Count of references */ TAILQ_ENTRY(ifmaddr) ifma_list; /* Per-interface list */ };