blob: 0bfc914ddd1504d16a2ebf8ad74655d6deb60e53 [file] [log] [blame]
Johannes Bergf444de02010-05-05 15:25:02 +02001/*
2 * mac80211 - channel management
3 */
4
Johannes Berg0aaffa92010-05-05 15:28:27 +02005#include <linux/nl80211.h>
Paul Stewart3117bbdb2012-03-13 07:46:18 -07006#include <net/cfg80211.h>
Johannes Bergf444de02010-05-05 15:25:02 +02007#include "ieee80211_i.h"
8
Johannes Berg368a07d2010-05-28 14:26:23 +02009static enum ieee80211_chan_mode
Johannes Bergf444de02010-05-05 15:25:02 +020010__ieee80211_get_channel_mode(struct ieee80211_local *local,
11 struct ieee80211_sub_if_data *ignore)
12{
13 struct ieee80211_sub_if_data *sdata;
14
Johannes Berg46a5eba2010-09-15 13:28:15 +020015 lockdep_assert_held(&local->iflist_mtx);
Johannes Bergf444de02010-05-05 15:25:02 +020016
17 list_for_each_entry(sdata, &local->interfaces, list) {
18 if (sdata == ignore)
19 continue;
20
21 if (!ieee80211_sdata_running(sdata))
22 continue;
23
Johannes Berge9980e62012-01-09 13:57:36 +010024 switch (sdata->vif.type) {
25 case NL80211_IFTYPE_MONITOR:
Johannes Bergf444de02010-05-05 15:25:02 +020026 continue;
Johannes Berge9980e62012-01-09 13:57:36 +010027 case NL80211_IFTYPE_STATION:
28 if (!sdata->u.mgd.associated)
29 continue;
30 break;
31 case NL80211_IFTYPE_ADHOC:
Johannes Bergf444de02010-05-05 15:25:02 +020032 if (!sdata->u.ibss.ssid_len)
33 continue;
34 if (!sdata->u.ibss.fixed_channel)
35 return CHAN_MODE_HOPPING;
Johannes Berge9980e62012-01-09 13:57:36 +010036 break;
37 case NL80211_IFTYPE_AP_VLAN:
38 /* will also have _AP interface */
Johannes Bergf444de02010-05-05 15:25:02 +020039 continue;
Johannes Berge9980e62012-01-09 13:57:36 +010040 case NL80211_IFTYPE_AP:
41 if (!sdata->u.ap.beacon)
42 continue;
43 break;
Thomas Pedersenbe0f4232012-05-13 22:24:08 -070044 case NL80211_IFTYPE_MESH_POINT:
45 if (!sdata->wdev.mesh_id_len)
46 continue;
47 break;
Johannes Berge9980e62012-01-09 13:57:36 +010048 default:
49 break;
50 }
Johannes Bergf444de02010-05-05 15:25:02 +020051
52 return CHAN_MODE_FIXED;
53 }
54
55 return CHAN_MODE_UNDEFINED;
56}
57
58enum ieee80211_chan_mode
59ieee80211_get_channel_mode(struct ieee80211_local *local,
60 struct ieee80211_sub_if_data *ignore)
61{
62 enum ieee80211_chan_mode mode;
63
64 mutex_lock(&local->iflist_mtx);
65 mode = __ieee80211_get_channel_mode(local, ignore);
66 mutex_unlock(&local->iflist_mtx);
67
68 return mode;
69}
Johannes Berg0aaffa92010-05-05 15:28:27 +020070
Michal Kazior23a85b452012-06-26 14:37:21 +020071static enum nl80211_channel_type
72ieee80211_get_superchan(struct ieee80211_local *local,
73 struct ieee80211_sub_if_data *sdata)
Johannes Berg0aaffa92010-05-05 15:28:27 +020074{
Johannes Berg0aaffa92010-05-05 15:28:27 +020075 enum nl80211_channel_type superchan = NL80211_CHAN_NO_HT;
Michal Kazior23a85b452012-06-26 14:37:21 +020076 struct ieee80211_sub_if_data *tmp;
Johannes Berg0aaffa92010-05-05 15:28:27 +020077
78 mutex_lock(&local->iflist_mtx);
Johannes Berg0aaffa92010-05-05 15:28:27 +020079 list_for_each_entry(tmp, &local->interfaces, list) {
80 if (tmp == sdata)
81 continue;
82
83 if (!ieee80211_sdata_running(tmp))
84 continue;
85
86 switch (tmp->vif.bss_conf.channel_type) {
87 case NL80211_CHAN_NO_HT:
88 case NL80211_CHAN_HT20:
Felix Fietkau9db372f2011-03-11 21:45:51 +010089 if (superchan > tmp->vif.bss_conf.channel_type)
90 break;
91
Johannes Berg0aaffa92010-05-05 15:28:27 +020092 superchan = tmp->vif.bss_conf.channel_type;
93 break;
94 case NL80211_CHAN_HT40PLUS:
95 WARN_ON(superchan == NL80211_CHAN_HT40MINUS);
96 superchan = NL80211_CHAN_HT40PLUS;
97 break;
98 case NL80211_CHAN_HT40MINUS:
99 WARN_ON(superchan == NL80211_CHAN_HT40PLUS);
100 superchan = NL80211_CHAN_HT40MINUS;
101 break;
102 }
103 }
Michal Kazior23a85b452012-06-26 14:37:21 +0200104 mutex_unlock(&local->iflist_mtx);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200105
Michal Kazior23a85b452012-06-26 14:37:21 +0200106 return superchan;
107}
108
109static bool
110ieee80211_channel_types_are_compatible(enum nl80211_channel_type chantype1,
111 enum nl80211_channel_type chantype2,
112 enum nl80211_channel_type *compat)
113{
114 /*
115 * start out with chantype1 being the result,
116 * overwriting later if needed
117 */
118 if (compat)
119 *compat = chantype1;
120
121 switch (chantype1) {
Johannes Berg0aaffa92010-05-05 15:28:27 +0200122 case NL80211_CHAN_NO_HT:
Michal Kazior23a85b452012-06-26 14:37:21 +0200123 if (compat)
124 *compat = chantype2;
125 break;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200126 case NL80211_CHAN_HT20:
127 /*
128 * allow any change that doesn't go to no-HT
129 * (if it already is no-HT no change is needed)
130 */
Michal Kazior23a85b452012-06-26 14:37:21 +0200131 if (chantype2 == NL80211_CHAN_NO_HT)
Johannes Berg0aaffa92010-05-05 15:28:27 +0200132 break;
Michal Kazior23a85b452012-06-26 14:37:21 +0200133 if (compat)
134 *compat = chantype2;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200135 break;
136 case NL80211_CHAN_HT40PLUS:
137 case NL80211_CHAN_HT40MINUS:
138 /* allow smaller bandwidth and same */
Michal Kazior23a85b452012-06-26 14:37:21 +0200139 if (chantype2 == NL80211_CHAN_NO_HT)
Johannes Berg0aaffa92010-05-05 15:28:27 +0200140 break;
Michal Kazior23a85b452012-06-26 14:37:21 +0200141 if (chantype2 == NL80211_CHAN_HT20)
Johannes Berg0aaffa92010-05-05 15:28:27 +0200142 break;
Michal Kazior23a85b452012-06-26 14:37:21 +0200143 if (chantype2 == chantype1)
Johannes Berg0aaffa92010-05-05 15:28:27 +0200144 break;
Michal Kazior23a85b452012-06-26 14:37:21 +0200145 return false;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200146 }
147
Michal Kazior23a85b452012-06-26 14:37:21 +0200148 return true;
149}
150
151bool ieee80211_set_channel_type(struct ieee80211_local *local,
152 struct ieee80211_sub_if_data *sdata,
153 enum nl80211_channel_type chantype)
154{
155 enum nl80211_channel_type superchan;
156 enum nl80211_channel_type compatchan;
157
158 superchan = ieee80211_get_superchan(local, sdata);
159 if (!ieee80211_channel_types_are_compatible(superchan, chantype,
160 &compatchan))
161 return false;
162
163 local->_oper_channel_type = compatchan;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200164
165 if (sdata)
166 sdata->vif.bss_conf.channel_type = chantype;
167
Michal Kazior23a85b452012-06-26 14:37:21 +0200168 return true;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200169
Johannes Berg0aaffa92010-05-05 15:28:27 +0200170}