/*
* mac80211 - channel management
*/
#include <linux/nl80211.h>
#include <linux/export.h>
#include <linux/rtnetlink.h>
#include <net/cfg80211.h>
#include "ieee80211_i.h"
#include "driver-ops.h"
static int ieee80211_num_chanctx(struct ieee80211_local *local)
{
struct ieee80211_chanctx *ctx;
int num = 0;
lockdep_assert_held(&local->chanctx_mtx);
list_for_each_entry(ctx, &local->chanctx_list, list)
num++;
return num;
}
static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
{
lockdep_assert_held(&local->chanctx_mtx);
return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
}
static const struct cfg80211_chan_def *
ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
struct ieee80211_chanctx *ctx,
const struct cfg80211_chan_def *compat)
{
struct ieee80211_sub_if_data *sdata;
lockdep_assert_held(&local->chanctx_mtx);
list_for_each_entry(sdata, &ctx->reserved_vifs,
reserved_chanctx_list) {
if (!compat)
compat = &sdata->reserved_chandef;
compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
compat);
if (!compat)
break;
}
return compat;
}
static const struct cfg80211_chan_def *
ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
struct ieee80211_chanctx *ctx,
const struct cfg80211_chan_def *compat)
{
struct ieee80211_sub_if_data *sdata;
lockdep_assert_held(&local->chanctx_mtx);
list_for_each_entry(sdata, &ctx->assigned_vifs,
assigned_chanctx_list) {
if (sdata->reserved_chanctx != NULL)
continue;
if (!compat)
compat = &sdata->vif.bss_conf.chandef;
compat = cfg80211_chandef_compatible(
&sdata->vif.bss_conf.chandef, compat);
if (!compat)
break;
}
return compat;
}
static const struct cfg80211_chan_def *
ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
struct ieee80211_chanctx *ctx,
const struct cfg80211_chan_def *compat)
{
lockdep_assert_held(&local->chanctx_mtx);
compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
if (!compat)
return NULL;
compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
if (!compat)
return NULL;
return compat;
}
static bool
ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
struct ieee80211_chanctx *ctx,
const struct cfg80211_chan_def *def)
{
lockdep_assert_held(&local->chanctx_mtx);
if (ieee80211_chanctx_combined_chandef(local, ctx, def))
return true;
if (!list_empty(&ctx->reserved_vifs) &&
ieee80211_chanctx_reserved_chandef(local, ctx, def))
return true;
return false;
}
static struct ieee80211_chanctx *
ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
const struct cfg80211_chan_def *chandef,
enum ieee80211_chanctx_mode mode)
{
struct ieee80211_chanctx *ctx;
lockdep_assert_held(&local->chanctx_mtx);
if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
return NULL;
list_for_each_entry(ctx, &local->chanctx_list, list) {
if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
continue;
if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
chandef))
continue;
return ctx;
}
return NULL;
}
static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
{
switch (sta->bandwidth) {
case IEEE80211_STA_RX_BW_20:
if (sta->ht_cap.<