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>
{nl,cfg,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.

NOTE: In a mesh with links in PS mode it is not advised to use the default
parameters 1000:4, as these cause excessive buffering delays that exceed
various timeouts (e.g. HWMP path request).

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
cfg80211: mesh power mode primitives

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 the station parameters and flags for future userspace access.

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: move add_tim to subfunction

This functions will be used for mesh beacons, too.

Signed-off-by: Marco Porsch <marco@cozybit.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>
nl80211: allow userspace to access mesh power mode parameters

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>
auth PS peering wip

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

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

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
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: 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: wip

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

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

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
{nl,cfg,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: fix for mesh sync to indicate TBTT adjustment

Currently the mesh sync code checks, whether peers indicate TBTT adjustment,
but it never sets the corresponding flag itself.

By setting ifmsh->tbtt_adjusting to true, it will set the corresponding field
in the mesh configuration IE of own beacons.
This indication will be set in the current beacon. The TBTT adjustment will be
performed afterwards, affecting the next beacon. Thus, the first beacon with
stable TBTT will not indicate adjustment anymore and peers will continue
tracking the new offset.

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
cfg80211: fix channel error on mesh join

Fix an error on mesh join when no channel has been explicitly set beforehand.

Previously a WARN_ON was hit in cfg80211_reg_can_beacon because center_freq1
was not set.

Also fix double semicolon.

Signed-off-by: Marco Porsch <marco.porsch@etit.tu-chemnitz.de>
mac80211: do not drop mesh Peering Open frames from unknown STA

These are valid mesh peering frames. The STA entry will then be created in
mesh_peer_init.
The problem was previously worked around, since both STA receive the beacons
of one another, which create the STA entry. This may not be the case when
either node is in PS mode.

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>