Skip to contentSkip to conversation

mac80211: fix rx monitor filter refcounters

RFC Superseded Christian Lamparter 2 participants 7 messages Sep 28, 2010 – Oct 2, 2010 mac80211

lore
Share & tools
lore
#0 lore raw

RFC 1/1 mac80211: fix rx monitor filter refcounters

This patch fixes a refcounter & commit bug when monitor rx flags are changed by:
iw dev moni set monitor [new flags]

while interface is up.

---
Is there a sane way to do that?

Introduced by: 8cc9a73914 - Jan 31 19:48:23 2008
"mac80211: Use monitor configuration flags"
---

net/mac80211/cfg.c +40 -1
index c981604..c156936 100644
@@ -68,8 +68,47 @@ static int ieee80211_change_iface(struct wiphy *wiphy,
 		 params && params->use_4addr >= 0)
 		sdata->u.mgd.use_4addr = params->use_4addr;
 
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+		u32 changed_flags = sdata->u.mntr_flags ^ *flags;
+
 		sdata->u.mntr_flags = *flags;
+		if (changed_flags & MONITOR_FLAG_FCSFAIL) {
+			if (*flags & MONITOR_FLAG_FCSFAIL)
+				local->fif_fcsfail++;
+			else
+				local->fif_fcsfail--;
+		}
+		if (changed_flags & MONITOR_FLAG_PLCPFAIL) {
+			if (*flags & MONITOR_FLAG_PLCPFAIL)
+				local->fif_plcpfail++;
+			else
+				local->fif_plcpfail--;
+		}
+		if (changed_flags & MONITOR_FLAG_COOK_FRAMES) {
+			if (*flags & MONITOR_FLAG_COOK_FRAMES)
+				local->cooked_mntrs++;
+			else
+				local->cooked_mntrs--;
+		}
+		if (changed_flags & MONITOR_FLAG_OTHER_BSS) {
+			if (*flags & MONITOR_FLAG_OTHER_BSS)
+				local->fif_other_bss++;
+			else
+				local->fif_other_bss--;
+		}
+		if (changed_flags & MONITOR_FLAG_CONTROL) {
+			if (*flags & MONITOR_FLAG_CONTROL) {
+				local->fif_pspoll++;
+				local->fif_control++;
+			} else {
+				local->fif_pspoll--;
+				local->fif_control--;
+			}
+		}
+
+		ieee80211_configure_filter(local);
+	}
 
 	return 0;
 }
maintainer
#1 lore raw
Quoted 8 lines · Christian Lamparter

This patch fixes a refcounter & commit bug when monitor rx flags are changed by:
iw dev moni set monitor [new flags]

while interface is up.

---
Is there a sane way to do that?

Is this not sane enough? Looks OK to me, even if it adds a bit of code.

Quoted 29 lines
net/mac80211/cfg.c +27 -1
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+		u32 changed_flags = sdata->u.mntr_flags ^ *flags;
+
 		sdata->u.mntr_flags = *flags;
+		if (changed_flags & MONITOR_FLAG_FCSFAIL) {
+			if (*flags & MONITOR_FLAG_FCSFAIL)
+				local->fif_fcsfail++;
+			else
+				local->fif_fcsfail--;
+		}
+		if (changed_flags & MONITOR_FLAG_PLCPFAIL) {
+			if (*flags & MONITOR_FLAG_PLCPFAIL)
+				local->fif_plcpfail++;
+			else
+				local->fif_plcpfail--;
+		}
+		if (changed_flags & MONITOR_FLAG_COOK_FRAMES) {
+			if (*flags & MONITOR_FLAG_COOK_FRAMES)
+				local->cooked_mntrs++;
+			else
+				local->cooked_mntrs--;
+		}
+		if (changed_flags & MONITOR_FLAG_OTHER_BSS) {
+			if (*flags & MONITOR_FLAG_OTHER_BSS)
+				local->fif_other_bss++;
+			else
+				local->fif_other_bss--;

net/mac80211/cfg.c static int ieee80211_change_iface(struct wiphy *wiphy, · line 108

Quoted 10 lines
net/mac80211/cfg.c +10 -0
+		}
+		if (changed_flags & MONITOR_FLAG_CONTROL) {
+			if (*flags & MONITOR_FLAG_CONTROL) {
+				local->fif_pspoll++;
+				local->fif_control++;
+			} else {
+				local->fif_pspoll--;
+				local->fif_control--;
+			}
+		}

Although, come to think of it, one could do something like this:

static void adjust_flags(local, flags, offset)
{

#define ADJUST(_flg, _fif)	do { 		\
	if (flags & MONITOR_FLAG_#_flg)		\
		local->fif_#_fif += offset;	\
	} while (0)

	ADJUST(FCSFAIL, fcsfail);
	ADJUST(PLCPFAIL, plcpfail);
	ADJUST(CONTROL, control);
	ADJUST(CONTROL, pspoll);
	ADJUST(OTHER_BSS, other_bss);

#undef ADJUST
}

and then we can have four callers of this function.

Two here:
adjust_flags(local, sdata->u.mntr_flags, -1);
adjust_flags(local, *flags, 1)
sdata->u.mntr_flags = *flags;

and the same two in ieee80211_do_open / ieee80211_do_stop.

johannes

#2 lore raw

RFC v2 1/1 mac80211: fix rx monitor filter refcounters

Quoted 5 lines · On Wednesday 29 September 2010 09:10:18 Johannes Berg wrote:
Quoted 4 lines · Christian Lamparter

This patch fixes a refcounter & commit bug when monitor rx flags are changed by:
iw dev moni set monitor [new flags]

Quoted 7 lines
Quoted 4 lines

while interface is up.

---
Is there a sane way to do that?

Is this not sane enough? Looks OK to me, even if it adds a bit of code.

It's about MONITOR_FLAG_COOK_FRAMES. This flag gives me headaches. I wish we could make this flag "const" and don't allow it be changed by iw dev wlanX set monitor. Another alternative would be to move the "sdata->u.mntr_flag -> fif_* processing" into ieee80211_configure_filter.

or:
---

net/mac80211/cfg.c +53 -1
index c981604..1ffe266 100644
@@ -68,9 +68,61 @@ static int ieee80211_change_iface(struct wiphy *wiphy,
 		 params && params->use_4addr >= 0)
 		sdata->u.mgd.use_4addr = params->use_4addr;
 
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+		u32 changed_flags;
+		u32 old_flags;
+		u32 hw_reconf_flags = 0;
+
+		old_flags = sdata->u.mntr_flags;
+		changed_flags = old_flags ^ *flags;
+		if (!(old_flags & MONITOR_FLAG_COOK_FRAMES))
+			ieee80211_adjust_monitor_flags(sdata, -1);
+
+		if (changed_flags & MONITOR_FLAG_COOK_FRAMES) {
+			if (*flags & MONITOR_FLAG_COOK_FRAMES) {
+				local->cooked_mntrs++;
+				local->monitors--;
+			} else {
+				local->monitors++;
+				local->cooked_mntrs--;
+
+				changed_flags |=
+					old_flags & ~MONITOR_FLAG_COOK_FRAMES;
+			}
+
+			switch (local->monitors) {
+			case 0:
+				local->hw.conf.flags &=
+					~IEEE80211_CONF_MONITOR;
+				hw_reconf_flags |=
+					IEEE80211_CONF_CHANGE_MONITOR;
+				break;
+
+			case 1:
+				local->hw.conf.flags |=
+					IEEE80211_CONF_MONITOR;
+				hw_reconf_flags |=
+					 IEEE80211_CONF_CHANGE_MONITOR;
+				break;
+
+			default:
+				break;
+			}
+		}
+
 		sdata->u.mntr_flags = *flags;
 
+		if (!(*flags & MONITOR_FLAG_COOK_FRAMES))
+			ieee80211_adjust_monitor_flags(sdata, 1);
+
+		if (changed_flags)
+			ieee80211_configure_filter(local);
+
+		if (hw_reconf_flags)
+			ieee80211_hw_config(local, hw_reconf_flags);
+	}
+
 	return 0;
 }
 
net/mac80211/ieee80211_i.h +2 -0
index 945fbf2..f6a6d78 100644
@@ -1132,6 +1132,8 @@ void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata);
 void ieee80211_remove_interfaces(struct ieee80211_local *local);
 u32 __ieee80211_recalc_idle(struct ieee80211_local *local);
 void ieee80211_recalc_idle(struct ieee80211_local *local);
+void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
+				    const int offset);
 
 static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data *sdata)
 {
net/mac80211/iface.c +22 -22

62 lines not on this page · raw message

maintainer
#3 lore raw
Quoted 8 lines · Christian Lamparter
Quoted 4 lines
Quoted 1 line

Is there a sane way to do that?

Is this not sane enough? Looks OK to me, even if it adds a bit of code.

It's about MONITOR_FLAG_COOK_FRAMES. This flag gives me headaches. I wish we could make this flag "const" and don't allow it be changed by iw dev wlanX set monitor.

Ah. Then you can only specify it when creating an interface, right? Fine with me, really.

Quoted 20 lines

Another alternative would be to move
the "sdata->u.mntr_flag -> fif_* processing" into
ieee80211_configure_filter.

or:
---

net/mac80211/cfg.c +5 -1
index c981604..1ffe266 100644
@@ -68,9 +68,61 @@ static int ieee80211_change_iface(struct wiphy *wiphy,
 		 params && params->use_4addr >= 0)
 		sdata->u.mgd.use_4addr = params->use_4addr;
 
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+		u32 changed_flags;
+		u32 old_flags;
+		u32 hw_reconf_flags = 0;

net/mac80211/cfg.c static int ieee80211_change_iface(struct wiphy *wiphy, · line 85

Quoted 10 lines
net/mac80211/cfg.c +10 -0
+
+		old_flags = sdata->u.mntr_flags;
+		changed_flags = old_flags ^ *flags;
+		if (!(old_flags & MONITOR_FLAG_COOK_FRAMES))
+			ieee80211_adjust_monitor_flags(sdata, -1);
+
+		if (changed_flags & MONITOR_FLAG_COOK_FRAMES) {
+			if (*flags & MONITOR_FLAG_COOK_FRAMES) {
+				local->cooked_mntrs++;
+				local->monitors--;

Yeah, that's kinda ugly. I'm fine with not allowing the cook flag to change, seems like a pretty special case anyway. Or, we can allow it to change, but only while the interface is down, right?

johannes

#4 lore raw

PATCH 1/1 mac80211: fix rx monitor filter refcounters

This patch fixes a refcounter & commit bug when monitor rx flags are changed by:
iw dev moni set monitor [new flags]

while interface is up.

---

Quoted 9 lines · On Wednesday 29 September 2010 22:00:39 Johannes wrote:
Quoted 3 lines · Christian

It's about MONITOR_FLAG_COOK_FRAMES. This flag gives me headaches. I wish we could make this flag "const" and don't allow it be changed by iw dev wlanX set monitor.

I'm fine with not allowing the cook flag to change, seems like a pretty special case anyway. Or, we can allow it to change, but only while the interface is down, right?

jup, works:

command failed: Function not implemented (-38), or should we use a different error code like -EBUSY/-EOPNOTSUPP?
---

net/mac80211/cfg.c +30 -2
index c981604..554166b 100644
@@ -68,8 +68,36 @@ static int ieee80211_change_iface(struct wiphy *wiphy,
 		 params && params->use_4addr >= 0)
 		sdata->u.mgd.use_4addr = params->use_4addr;
 
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
-		sdata->u.mntr_flags = *flags;
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+
+		if (test_bit(SDATA_STATE_RUNNING, &sdata->state)) {
+			/*
+			 * Prohibit MONITOR_FLAG_COOK_FRAMES to be
+			 * changed while the interface is up.
+			 * Else we would need to add a lot of cruft
+			 * to update everything:
+			 *	cooked_mntrs, monitor and all fif_* counters
+			 *	reconfigure hardware
+			 */
+			if ((*flags & MONITOR_FLAG_COOK_FRAMES) !=
+			    (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
+				return -ENOSYS;
+
+			ieee80211_adjust_monitor_flags(sdata, -1);
+			sdata->u.mntr_flags = *flags;
+			ieee80211_adjust_monitor_flags(sdata, 1);
+
+			ieee80211_configure_filter(local);
+		} else {
+			/*
+			 * Because the interface is down, ieee80211_do_stop
+			 * and ieee80211_do_open take care of "everything"
+			 * mentioned in the comment above.
+			 */
+			sdata->u.mntr_flags = *flags;
+		}
+	}
 
 	return 0;
 }
net/mac80211/ieee80211_i.h +2 -0
index 945fbf2..f6a6d78 100644
@@ -1132,6 +1132,8 @@ void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata);
 void ieee80211_remove_interfaces(struct ieee80211_local *local);
 u32 __ieee80211_recalc_idle(struct ieee80211_local *local);
 void ieee80211_recalc_idle(struct ieee80211_local *local);
+void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
+				    const int offset);
 
 static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data *sdata)
 {
net/mac80211/iface.c +22 -22

62 lines not on this page · raw message

maintainer
#5 lore raw
Quoted 8 lines
Quoted 4 lines

I'm fine with not allowing the cook flag to change, seems like a pretty special case anyway. Or, we can allow it to change, but only while the interface is down, right?

jup, works:

command failed: Function not implemented (-38), or should we use a different error code like -EBUSY/-EOPNOTSUPP?

I think typically I'd use ebusy, and I think it'd be more descriptive here?

Quoted 1 line

+        if (test_bit(SDATA_STATE_RUNNING, &sdata->state)) {

There's an inline for this, I think?

Quoted 1 line

  static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data

heh, by accident it's even in your patch :)

ACK with those two changes.

Johannes

#6 lore raw

PATCH v2 1/1 mac80211: fix rx monitor filter refcounters

This patch fixes an refcounting bug. Previously it was possible to corrupt the per-device recv. filter and monitor management counters when:
iw dev wlanX set monitor [new flags]
was issued on an active monitor interface.

---

Quoted 7 lines · On Saturday 02 October 2010 10:46:27 Johannes Berg wrote:
Quoted 1 line

+ if (test_bit(SDATA_STATE_RUNNING, &sdata->state)) {

There's an inline for this, I think?

Quoted 1 line

static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data

heh, by accident it's even in your patch :)

but what an accident!
---

net/mac80211/cfg.c +30 -2
index c981604..9e63fc2 100644
@@ -68,8 +68,36 @@ static int ieee80211_change_iface(struct wiphy *wiphy,
 		 params && params->use_4addr >= 0)
 		sdata->u.mgd.use_4addr = params->use_4addr;
 
-	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags)
-		sdata->u.mntr_flags = *flags;
+	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
+		struct ieee80211_local *local = sdata->local;
+
+		if (ieee80211_sdata_running(sdata)) {
+			/*
+			 * Prohibit MONITOR_FLAG_COOK_FRAMES to be
+			 * changed while the interface is up.
+			 * Else we would need to add a lot of cruft
+			 * to update everything:
+			 *	cooked_mntrs, monitor and all fif_* counters
+			 *	reconfigure hardware
+			 */
+			if ((*flags & MONITOR_FLAG_COOK_FRAMES) !=
+			    (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
+				return -EBUSY;
+
+			ieee80211_adjust_monitor_flags(sdata, -1);
+			sdata->u.mntr_flags = *flags;
+			ieee80211_adjust_monitor_flags(sdata, 1);
+
+			ieee80211_configure_filter(local);
+		} else {
+			/*
+			 * Because the interface is down, ieee80211_do_stop
+			 * and ieee80211_do_open take care of "everything"
+			 * mentioned in the comment above.
+			 */
+			sdata->u.mntr_flags = *flags;
+		}
+	}
 
 	return 0;
 }
net/mac80211/ieee80211_i.h +2 -0
index 945fbf2..f6a6d78 100644
@@ -1132,6 +1132,8 @@ void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata);
 void ieee80211_remove_interfaces(struct ieee80211_local *local);
 u32 __ieee80211_recalc_idle(struct ieee80211_local *local);
 void ieee80211_recalc_idle(struct ieee80211_local *local);
+void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
+				    const int offset);
 
 static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data *sdata)
 {
net/mac80211/iface.c +22 -22

62 lines not on this page · raw message