nl80211: add range checks to mesh parameters

Ranges are taken from IEEE 802.11-2012, common sense or current implementation
requirements.

Signed-off-by: Marco Porsch <marco@cozybit.com>
mac80211: update mesh peer link counter during userspace peering

The established peer link count is indicated in mesh beacons and used for
other internal tasks. Previously it was not updated when authenticated peering
is performed in userspace.

Signed-off-by: Marco Porsch <marco@cozybit.com>
mac80211: move add_tim to subfunction

This functions will be used for mesh beacons, too.

Signed-off-by: Marco Porsch <marco@cozybit.com>
{cfg,nl,mac}80211: set beacon interval and DTIM period on mesh join

Move the default mesh beacon interval and DTIM period to cfg80211 and
make it accessible to nl80211. This enables setting both values when joining
an MBSS.
Previously the DTIM parameter was not set by mac80211 so the driver's default
value was used.

Signed-off-by: Marco Porsch <marco@cozybit.com>
{cfg,nl}80211: mesh power mode primitives and userspace access

Add the nl80211_mesh_power_mode enumeration which holds possible values for
the mesh power mode. These modes are unknown, active, light sleep and
deep sleep.

Add power_mode entry to the mesh config structure to hold the user-configured
default mesh power mode. This value will be used for new peer links.

Add the dot11MeshAwakeWindowDuration value to the mesh config. The awake window
is a duration in TU describing how long the STA will stay awake after
transmitting its beacon in PS mode.

Add access routines to:
get/set local link-specific power mode (STA)
get remote STA's link-specific power mode (STA)
get remote STA's non-peer power mode (STA)

get/set default mesh power mode (mesh config)
get/set mesh awake window duration (mesh config)

All config changes may be done at mesh runtime and take effect immediately.

Signed-off-by: Marco Porsch <marco@cozybit.com>
Signed-off-by: Ivan Bezyazychnyy <ivan.bezyazychnyy@gmail.com>
Signed-off-by: Mike Krinkin <krinkin.m.u@gmail.com>
mac80211: mesh power save basics

Add the local_pm field to the sta_info structure to represent
the link-specific power mode at this station towards the remote station.
When a peer link is established, the local link-specific power mode is
set up equal to the user-configurable default power mode.
To avoid frame losses when switching to a lower power state after peering
(and for possible use in a per-link dynamic powersave), the corresponding
function can be called with a delay parameter that sets a per-STA timer
accordingly.
Monitor the status of the local peer-specific power modes for mesh power mode
indication, and for checking if the radio may be set to doze state. Update
the PS status if the peering status of a neighbor changes.
Maintain a mesh power mode  for non-peer mesh STAs. The non-peer mesh power
mode determines when non-peer mesh STA may send Probe Requests and Mesh Peering
Open frames to the mesh STA.
Based on the status of the peer-specific power modes also the non-peer power
mode is set: if there is at least one link in light or deep sleep mode, the
non-peer power mode will be deep sleep mode.
To allow successful peering, set the non-peer power mode to active mode during
peering.
Add a debug define for comfortable debugging output.

Indicate the current power mode in transmitted frames. This differs for
individually-addressed frames and group addressed frames as well as whether
the frames are addressed to peers or non-peers.
Especially indicate a peer-specific mesh power mode transition with an
individually-addressed QoS Null frame to the respective peer.
Indicate the non-peer mesh power mode in frames transmitted to non-peer STA
and in management frames in the Power Management field of the Frame Control
field.
In group addressed Mesh QoS Data frames the Mesh Power Save Level field
in the QoS Control field indicates whether the local STA has any deep
sleep peers.
In beacon (and probe response) frames indicate whether we have any deep sleep
peers, by setting the Mesh Power Save Level field of the Mesh Capability field.
For performance reasons, calls to the function setting the frame flags are
placed in HWMP routing routines, as there the STA pointer is already available.

Add the peer_pm field to the sta_info structure to represent the peer's
link-specific power mode towards the local station. Additionally, the
nonpeer_pm field represents the peer's power mode towards all non-peer
stations.
The peers' link-specific power modes are tracked from the Power Management
field in the Frame Control field and the Mesh Power Save Level field
in the QoS Control field. Peer and non-peer modes are tracked independently
from respective frames. This allows fast reconfiguration after a peering
change.

Add the ps_data structure to ieee80211_if_mesh. This structure contains the TIM
map, a counter how of PS neighbors and the group-addressed frame buffer.
Functions using this structure are modified for mesh mode.

In case of a neighbor changing its power mode or peering status, update the
setting of the WLAN_STA_PS flag. This flag triggers the frame buffering
routines for individually-addressed frames in the tx path used for client mode,
which are re-used here. Also the num_sta_ps counter is updated, which triggers
the buffering and release of group-addressed frames after DTIM beacons.

Until drivers are adapted for mesh power save, do not inform the driver about
the neighbor STA being in PS mode towards us (as sta_ps_start would do).

Announce the awake window value in an IE in beacons if the local STA is in
light or deep sleep mode towards any peer or non-peer. On receipt STA parse
the IE.

Create a TIM IE similarly to AP mode and add it to mesh beacons.

React to neighbor's Awake Window and TIM IE. When receiving a neighbor beacon,
check if the neighbor indicates buffered frames and if it announces an awake
window duration.

In case the neighbor is an established peer, frames may be released in a
mesh Peer Service Period. MPSPs start and end with a MPSP trigger frame. The
setting of RSPI and EOSP frame flags determine if a MPSP starts or ends and
which STA is the owner, recipient or both during the MPSP.
The indication of buffered frames at the peer and the status of the local
buffers determine which type of MPSP is started when a peer beacon is
received. The buffered frames are transmitted only after the MPSP trigger frame
has been acknowledged.
To end a MPSP set the EOSP flag in the last QoS Data frame. In case the last
frame is not a QoS data frame, append a QoS Null frame after the last frame.

Count the number of MPSPs the local STA is owner and/or recipient in. This will
be used to disallow doze state during ongoing MPSPs.

Currently, in HT channels MPSPs behave imperfectly and show large delay spikes
and frame losses.

Authenticated mesh peering is currently not working when either node is
configured to be in power save mode.

Signed-off-by: Marco Porsch <marco@cozybit.com>
Signed-off-by: Ivan Bezyazychnyy <ivan.bezyazychnyy@gmail.com>
Signed-off-by: Mike Krinkin <krinkin.m.u@gmail.com>
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
mac80211: move mesh sync beacon handler into neighbour_update

Move the beacon handler into neighbour_update where the STA entry is already
available. This avoids additional rcu_locks and simplifies the handler.
Rename the handler to reflect its purpose better.
The repositioning will also benefit mesh PS which uses the up-to-date T_offset
value.

Signed-off-by: Marco Porsch <marco@cozybit.com>
mac80211: mesh power save doze scheduling

Configure the HW for PS mode if the local mesh PS parameters allow it.

Expose a callback ieee80211_mps_init for drivers to register
mesh powersave ops:
- hw_doze - put the radio to sleep now
- hw_wakeup - wake the radio up for frame RX
These ops may be extended in the future to allow drivers/HW to implement
mesh PS themselves. (The current design goal was to concentrate mesh PS
routines in mac80211 to keep driver modifications minimal.

Track the timing information of beacons from peers we are in PS mode towards.
Set a per-STA hrtimer which will trigger a wakeup right before the peer's next
TBTT.
Also use the same hrtimer to go to sleep mode after not receiving a beacon in a
defined time margin. In this case calculate the next TBTT and increase the
margin.

For mesh Awake Windows wakeup on SWBA (beacon_get_tim) and start a timer which
triggers a hw_doze call on expiry.

Signed-off-by: Marco Porsch <marco@cozybit.com>
ath9k: mesh powersave support

Register mesh PS ops on interface add and de-register on removal.

When enabling PS do not enable the hardware wakeup scheduling interrupt.

React to mac80211 doze/wakeup calls.
Add a PS status flag PS_MAC80211_CTL to store last PS command from mac80211.

Signed-off-by: Marco Porsch <marco@cozybit.com>
mac80211: auth PS peering wip
ath9k_htc: Add support for NL80211_IFTYPE_MESH_POINT interfaces

More specifically, enable AP-style beaconing on mesh ifaces, honor
FIF_OTHER_BSS filter and change the hw capabilities to reflect mesh
support.

Also enable IEEE80211_HW_SUPPORTS_PER_STA_GTK, IEEE80211_HW_MFP_CAPABLE
and WIPHY_FLAG_IBSS_RSN.  Probably these should depend on the
capabilities of the hardware but I don't know which hardware supports
what.  But these are required for secured mesh and work fine on our
TL-WN821N

Signed-off-by: Javier Cardona <javier@cozybit.com>
ath9k_htc: mesh powersave support

Register mesh_ps_ops on mesh interface add/remove.
React to hw_doze/hw_wakeup calls from mac80211. Before going to sleep request
mac80211's status with mps_hw_doze_allow callback.
On beacon SWBA the hardware is already in Awake state, so update the drivers
state variable accordingly.

Signed-off-by: Marco Porsch <marco@cozybit.com>
carl9170: mesh powersave support

Register mesh PS ops on interface add and de-register on removal.

React to mac80211's mps_doze/mps_wakeup calls and request mac80211's status
before going to sleep state.

The device is already in Awake state after SWBA. Update the driver state
variable accordingly.

Signed-off-by: Marco Porsch <marco@cozybit.com>
mac80211_hwsim: fix for BUG in add_timer with multiple phys

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
mac80211_hwsim: wip

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
ath9k_htc: wip
printk, comments, wip

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
{cfg,nl,mac}80211: make peer inactivity limit accessible to userspace

For mesh power save it is useful to remove inactive peers whose beacons we
should not track anymore.
This patch allows modifiying the corresponding peer inactivity limit, to expire
peers that we have not received frames or beacons from recently. It is checked
in the mesh housekeeping routine (currently every 60 HZ).

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
mac80211_hwsim: do not modify TBTT if beacon is already enabled

If the beacon is already enabled, do not modify the beacon timer. This causes
a hard TBTT adjustment and may cause mischief for powersave or synchronization.

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
fix mac80211: mesh power save basics