Author: avos
Date: Sun Jan 20 13:39:18 2019
New Revision: 343213
URL: https://svnweb.freebsd.org/changeset/base/343213

Log:
  net80211: resolve ioctl <-> detach race for ieee80211com structure
  
  Since r287197 ieee80211com is a part of drivers softc; as a result,
  after detach all pointers to it (iv_ic, ni_ic) are invalid. Most
  possible users (tasks, interrupt handlers) are blocked / removed
  when device is stopped; however, ioctl handlers were not tracked
  and may crash if ieee80211com structure is accessed.
  
  Since ieee80211com pointer access from ieee80211vap structure is not
  protected by lock (constant after interface creation) and used in
  many other places just use reference counting for ioctl handlers;
  on detach set 'detached' flag and wait until reference counter goes to 0.
  
  For HEAD ieee80211vap size was changed (__FreeBSD_version bumped);
  however, in stable branches I'm going to split / reuse the last
  iv_spare field for KBI stability.
  
  Tested with:
   - rsu(4), SIOCSIFCAP (-rxcsum) ioctl;
   - rtwn_pci(4), SIOCG80211 / IEEE80211_IOC_HTPROTMODE ioctl.
  
  MFC after:    1 week

Modified:
  head/sys/net80211/ieee80211.c
  head/sys/net80211/ieee80211_freebsd.c
  head/sys/net80211/ieee80211_freebsd.h
  head/sys/net80211/ieee80211_ioctl.c
  head/sys/net80211/ieee80211_var.h
  head/sys/sys/param.h

Modified: head/sys/net80211/ieee80211.c
==============================================================================
--- head/sys/net80211/ieee80211.c       Sun Jan 20 13:16:36 2019        
(r343212)
+++ head/sys/net80211/ieee80211.c       Sun Jan 20 13:39:18 2019        
(r343213)
@@ -405,8 +405,10 @@ ieee80211_ifdetach(struct ieee80211com *ic)
         * The VAP is responsible for setting and clearing
         * the VIMAGE context.
         */
-       while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
+       while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) {
+               ieee80211_com_vdetach(vap);
                ieee80211_vap_destroy(vap);
+       }
        ieee80211_waitfor_parent(ic);
 
        ieee80211_sysctl_detach(ic);

Modified: head/sys/net80211/ieee80211_freebsd.c
==============================================================================
--- head/sys/net80211/ieee80211_freebsd.c       Sun Jan 20 13:16:36 2019        
(r343212)
+++ head/sys/net80211/ieee80211_freebsd.c       Sun Jan 20 13:39:18 2019        
(r343213)
@@ -307,6 +307,55 @@ ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
        }
 }
 
+#define        MS(_v, _f)      (((_v) & _f##_M) >> _f##_S)
+int
+ieee80211_com_vincref(struct ieee80211vap *vap)
+{
+       uint32_t ostate;
+
+       ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
+
+       if (ostate & IEEE80211_COM_DETACHED) {
+               atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
+               return (ENETDOWN);
+       }
+
+       if (MS(ostate, IEEE80211_COM_REF) == IEEE80211_COM_REF_MAX) {
+               atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
+               return (EOVERFLOW);
+       }
+
+       return (0);
+}
+
+void
+ieee80211_com_vdecref(struct ieee80211vap *vap)
+{
+       uint32_t ostate;
+
+       ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
+
+       KASSERT(MS(ostate, IEEE80211_COM_REF) != 0,
+           ("com reference counter underflow"));
+
+       (void) ostate;
+}
+
+void
+ieee80211_com_vdetach(struct ieee80211vap *vap)
+{
+       int sleep_time;
+
+       sleep_time = msecs_to_ticks(250);
+       if (sleep_time == 0)
+               sleep_time = 1;
+
+       atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED);
+       while (MS(atomic_load_32(&vap->iv_com_state), IEEE80211_COM_REF) != 0)
+               pause("comref", sleep_time);
+}
+#undef MS
+
 int
 ieee80211_node_dectestref(struct ieee80211_node *ni)
 {

Modified: head/sys/net80211/ieee80211_freebsd.h
==============================================================================
--- head/sys/net80211/ieee80211_freebsd.h       Sun Jan 20 13:16:36 2019        
(r343212)
+++ head/sys/net80211/ieee80211_freebsd.h       Sun Jan 20 13:39:18 2019        
(r343213)
@@ -224,6 +224,11 @@ typedef struct mtx ieee80211_rt_lock_t;
  */
 #include <machine/atomic.h>
 
+struct ieee80211vap;
+int    ieee80211_com_vincref(struct ieee80211vap *);
+void   ieee80211_com_vdecref(struct ieee80211vap *);
+void   ieee80211_com_vdetach(struct ieee80211vap *);
+
 #define ieee80211_node_initref(_ni) \
        do { ((_ni)->ni_refcnt = 1); } while (0)
 #define ieee80211_node_incref(_ni) \
@@ -235,7 +240,6 @@ int ieee80211_node_dectestref(struct ieee80211_node *n
 #define        ieee80211_node_refcnt(_ni)      (_ni)->ni_refcnt
 
 struct ifqueue;
-struct ieee80211vap;
 void   ieee80211_drain_ifq(struct ifqueue *);
 void   ieee80211_flush_ifq(struct ifqueue *, struct ieee80211vap *);
 

Modified: head/sys/net80211/ieee80211_ioctl.c
==============================================================================
--- head/sys/net80211/ieee80211_ioctl.c Sun Jan 20 13:16:36 2019        
(r343212)
+++ head/sys/net80211/ieee80211_ioctl.c Sun Jan 20 13:39:18 2019        
(r343213)
@@ -3480,10 +3480,14 @@ ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t
 {
        struct ieee80211vap *vap = ifp->if_softc;
        struct ieee80211com *ic = vap->iv_ic;
-       int error = 0, wait = 0;
+       int error = 0, wait = 0, ic_used;
        struct ifreq *ifr;
        struct ifaddr *ifa;                     /* XXX */
 
+       ic_used = (cmd != SIOCSIFMTU && cmd != SIOCG80211STATS);
+       if (ic_used && (error = ieee80211_com_vincref(vap)) != 0)
+               return (error);
+
        switch (cmd) {
        case SIOCSIFFLAGS:
                IEEE80211_LOCK(ic);
@@ -3620,5 +3624,9 @@ ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t
                error = ether_ioctl(ifp, cmd, data);
                break;
        }
+
+       if (ic_used)
+               ieee80211_com_vdecref(vap);
+
        return (error);
 }

Modified: head/sys/net80211/ieee80211_var.h
==============================================================================
--- head/sys/net80211/ieee80211_var.h   Sun Jan 20 13:16:36 2019        
(r343212)
+++ head/sys/net80211/ieee80211_var.h   Sun Jan 20 13:39:18 2019        
(r343213)
@@ -400,6 +400,7 @@ struct ieee80211vap {
        uint32_t                iv_caps;        /* capabilities */
        uint32_t                iv_htcaps;      /* HT capabilities */
        uint32_t                iv_htextcaps;   /* HT extended capabilities */
+       uint32_t                iv_com_state;   /* com usage / detached flag */
        enum ieee80211_opmode   iv_opmode;      /* operation mode */
        enum ieee80211_state    iv_state;       /* state machine state */
        enum ieee80211_state    iv_nstate;      /* pending state */
@@ -684,6 +685,12 @@ MALLOC_DECLARE(M_80211_VAP);
 #define        IEEE80211_FVHT_USEVHT160        0x000000010     /* CONF: Use 
VHT160 */
 #define        IEEE80211_VFHT_BITS \
        "\20\1VHT\2VHT40\3VHT80\4VHT80P80\5VHT160"
+
+#define        IEEE80211_COM_DETACHED  0x00000001      /* ieee80211_ifdetach 
called */
+#define        IEEE80211_COM_REF_ADD   0x00000002      /* add / remove 
reference */
+#define        IEEE80211_COM_REF_M     0xfffffffe      /* reference counter 
bits */
+#define        IEEE80211_COM_REF_S     1
+#define        IEEE80211_COM_REF_MAX   (IEEE80211_COM_REF_M >> 
IEEE80211_COM_REF_S)
 
 int    ic_printf(struct ieee80211com *, const char *, ...) __printflike(2, 3);
 void   ieee80211_ifattach(struct ieee80211com *);

Modified: head/sys/sys/param.h
==============================================================================
--- head/sys/sys/param.h        Sun Jan 20 13:16:36 2019        (r343212)
+++ head/sys/sys/param.h        Sun Jan 20 13:39:18 2019        (r343213)
@@ -60,7 +60,7 @@
  *             in the range 5 to 9.
  */
 #undef __FreeBSD_version
-#define __FreeBSD_version 1300008      /* Master, propagated to newvers */
+#define __FreeBSD_version 1300009      /* Master, propagated to newvers */
 
 /*
  * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to