blob: 72d765ef26df7309f5962d9e5265881df34b6e91 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
10#include <linux/slab.h>
11#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
17#include <linux/etherdevice.h>
18#include <net/net_namespace.h>
19#include <net/genetlink.h>
20#include <net/cfg80211.h>
21#include <net/sock.h>
22#include "core.h"
23#include "nl80211.h"
24#include "reg.h"
25
26static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
32static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
37/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
44 .netnsok = true,
45 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
47};
48
49/* internal helper: get rdev and dev */
50static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
53{
54 int ifindex;
55
56 if (!attrs[NL80211_ATTR_IFINDEX])
57 return -EINVAL;
58
59 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
60 *dev = dev_get_by_index(netns, ifindex);
61 if (!*dev)
62 return -ENODEV;
63
64 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
65 if (IS_ERR(*rdev)) {
66 dev_put(*dev);
67 return PTR_ERR(*rdev);
68 }
69
70 return 0;
71}
72
73/* policy for the attributes */
74static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
75 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
77 .len = 20-1 },
78 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
79 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
80 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
81 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
86
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
90
91 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
93
94 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
95 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
120
121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
130
131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
133
134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
209#if defined(CONFIG_AIC8800)
210 #if 0
211 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
212 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
213 [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
214 [NL80211_ATTR_VHT_CAPABILITY] = {
215 .type = NLA_EXACT_LEN_WARN,
216 .len = NL80211_VHT_CAPABILITY_LEN
217 },
218 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
219 [NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
220 [NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
221 [NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
222 NLA_POLICY_RANGE(NLA_U32,
223 NL80211_MESH_POWER_UNKNOWN + 1,
224 NL80211_MESH_POWER_MAX),
225 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
226 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
227 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
228 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
229 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
230 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
231 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
232 .len = NL80211_VHT_CAPABILITY_LEN,
233 },
234 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
235 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
236 .len = IEEE80211_MAX_DATA_LEN },
237 [NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
238 [NL80211_ATTR_MAX_CRIT_PROT_DURATION] = { .type = NLA_U16 },
239 [NL80211_ATTR_PEER_AID] =
240 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
241 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
242 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
243 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
244 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
245 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
246 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
247 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
248 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
249 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
250 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
251 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
252 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
253 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
254 .len = IEEE80211_QOS_MAP_LEN_MAX },
255 [NL80211_ATTR_MAC_HINT] = {
256 .type = NLA_EXACT_LEN_WARN,
257 .len = ETH_ALEN
258 },
259 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
260 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
261 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
262 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
263 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
264 [NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
265 [NL80211_ATTR_USER_PRIO] =
266 NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
267 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
268 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
269 [NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
270 [NL80211_ATTR_MAC_MASK] = {
271 .type = NLA_EXACT_LEN_WARN,
272 .len = ETH_ALEN
273 },
274 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
275 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
276 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
277 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
278 [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
279 [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
280 [NL80211_ATTR_STA_SUPPORT_P2P_PS] =
281 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
282 [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
283 .len = VHT_MUMIMO_GROUPS_DATA_LEN
284 },
285 [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
286 .type = NLA_EXACT_LEN_WARN,
287 .len = ETH_ALEN
288 },
289 [NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
290 [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
291 [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
292 [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
293 .len = FILS_MAX_KEK_LEN },
294 [NL80211_ATTR_FILS_NONCES] = {
295 .type = NLA_EXACT_LEN_WARN,
296 .len = 2 * FILS_NONCE_LEN
297 },
298 [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
299 [NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
300 [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
301 [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
302 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
303 },
304 [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
305 [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
306 .len = FILS_ERP_MAX_USERNAME_LEN },
307 [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
308 .len = FILS_ERP_MAX_REALM_LEN },
309 [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
310 [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
311 .len = FILS_ERP_MAX_RRK_LEN },
312 [NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
313 [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
314 [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
315 #endif
316 [NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
317#endif
318};
319
320/* policy for the key attributes */
321static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
322 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
323 [NL80211_KEY_IDX] = { .type = NLA_U8 },
324 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
325 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
326 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
327 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
328 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
329 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
330};
331
332/* policy for the key default flags */
333static const struct nla_policy
334nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
335 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
336 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
337};
338
339/* policy for WoWLAN attributes */
340static const struct nla_policy
341nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
342 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
343 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
344 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
345 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
346 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
347 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
348 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
349 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
350};
351
352/* policy for GTK rekey offload attributes */
353static const struct nla_policy
354nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
355 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
356 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
357 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
358};
359
360static const struct nla_policy
361nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
362 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
363 .len = IEEE80211_MAX_SSID_LEN },
364};
365
366/* ifidx get helper */
367static int nl80211_get_ifidx(struct netlink_callback *cb)
368{
369 int res;
370
371 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
372 nl80211_fam.attrbuf, nl80211_fam.maxattr,
373 nl80211_policy);
374 if (res)
375 return res;
376
377 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
378 return -EINVAL;
379
380 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
381 if (!res)
382 return -EINVAL;
383 return res;
384}
385
386static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
387 struct netlink_callback *cb,
388 struct cfg80211_registered_device **rdev,
389 struct net_device **dev)
390{
391 int ifidx = cb->args[0];
392 int err;
393
394 if (!ifidx)
395 ifidx = nl80211_get_ifidx(cb);
396 if (ifidx < 0)
397 return ifidx;
398
399 cb->args[0] = ifidx;
400
401 rtnl_lock();
402
403 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
404 if (!*dev) {
405 err = -ENODEV;
406 goto out_rtnl;
407 }
408
409 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
410 if (IS_ERR(*rdev)) {
411 err = PTR_ERR(*rdev);
412 goto out_rtnl;
413 }
414
415 return 0;
416 out_rtnl:
417 rtnl_unlock();
418 return err;
419}
420
421static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
422{
423 cfg80211_unlock_rdev(rdev);
424 rtnl_unlock();
425}
426
427/* IE validation */
428static bool is_valid_ie_attr(const struct nlattr *attr)
429{
430 const u8 *pos;
431 int len;
432
433 if (!attr)
434 return true;
435
436 pos = nla_data(attr);
437 len = nla_len(attr);
438
439 while (len) {
440 u8 elemlen;
441
442 if (len < 2)
443 return false;
444 len -= 2;
445
446 elemlen = pos[1];
447 if (elemlen > len)
448 return false;
449
450 len -= elemlen;
451 pos += 2 + elemlen;
452 }
453
454 return true;
455}
456
457/* message building helper */
458static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
459 int flags, u8 cmd)
460{
461 /* since there is no private header just add the generic one */
462 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
463}
464
465static int nl80211_msg_put_channel(struct sk_buff *msg,
466 struct ieee80211_channel *chan)
467{
468 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
469 chan->center_freq);
470
471 if (chan->flags & IEEE80211_CHAN_DISABLED)
472 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
473 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
474 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
475 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
476 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
477 if (chan->flags & IEEE80211_CHAN_RADAR)
478 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
479
480 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
481 DBM_TO_MBM(chan->max_power));
482
483 return 0;
484
485 nla_put_failure:
486 return -ENOBUFS;
487}
488
489/* netlink command implementations */
490
491struct key_parse {
492 struct key_params p;
493 int idx;
494 int type;
495 bool def, defmgmt;
496 bool def_uni, def_multi;
497};
498
499static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
500{
501 struct nlattr *tb[NL80211_KEY_MAX + 1];
502 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
503 nl80211_key_policy);
504 if (err)
505 return err;
506
507 k->def = !!tb[NL80211_KEY_DEFAULT];
508 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
509
510 if (k->def) {
511 k->def_uni = true;
512 k->def_multi = true;
513 }
514 if (k->defmgmt)
515 k->def_multi = true;
516
517 if (tb[NL80211_KEY_IDX])
518 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
519
520 if (tb[NL80211_KEY_DATA]) {
521 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
522 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
523 }
524
525 if (tb[NL80211_KEY_SEQ]) {
526 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
527 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
528 }
529
530 if (tb[NL80211_KEY_CIPHER])
531 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
532
533 if (tb[NL80211_KEY_TYPE]) {
534 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
535 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
536 return -EINVAL;
537 }
538
539 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
540 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
541 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
542 tb[NL80211_KEY_DEFAULT_TYPES],
543 nl80211_key_default_policy);
544 if (err)
545 return err;
546
547 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
548 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
549 }
550
551 return 0;
552}
553
554static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
555{
556 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
557 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
558 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
559 }
560
561 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
562 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
563 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
564 }
565
566 if (info->attrs[NL80211_ATTR_KEY_IDX])
567 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
568
569 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
570 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
571
572 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
573 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
574
575 if (k->def) {
576 k->def_uni = true;
577 k->def_multi = true;
578 }
579 if (k->defmgmt)
580 k->def_multi = true;
581
582 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
583 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
584 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
585 return -EINVAL;
586 }
587
588 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
589 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
590 int err = nla_parse_nested(
591 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
592 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
593 nl80211_key_default_policy);
594 if (err)
595 return err;
596
597 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
598 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
599 }
600
601 return 0;
602}
603
604static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
605{
606 int err;
607
608 memset(k, 0, sizeof(*k));
609 k->idx = -1;
610 k->type = -1;
611
612 if (info->attrs[NL80211_ATTR_KEY])
613 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
614 else
615 err = nl80211_parse_key_old(info, k);
616
617 if (err)
618 return err;
619
620 if (k->def && k->defmgmt)
621 return -EINVAL;
622
623 if (k->defmgmt) {
624 if (k->def_uni || !k->def_multi)
625 return -EINVAL;
626 }
627
628 if (k->idx != -1) {
629 if (k->defmgmt) {
630 if (k->idx < 4 || k->idx > 5)
631 return -EINVAL;
632 } else if (k->def) {
633 if (k->idx < 0 || k->idx > 3)
634 return -EINVAL;
635 } else {
636 if (k->idx < 0 || k->idx > 5)
637 return -EINVAL;
638 }
639 }
640
641 return 0;
642}
643
644static struct cfg80211_cached_keys *
645nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
646 struct nlattr *keys)
647{
648 struct key_parse parse;
649 struct nlattr *key;
650 struct cfg80211_cached_keys *result;
651 int rem, err, def = 0;
652
653 result = kzalloc(sizeof(*result), GFP_KERNEL);
654 if (!result)
655 return ERR_PTR(-ENOMEM);
656
657 result->def = -1;
658 result->defmgmt = -1;
659
660 nla_for_each_nested(key, keys, rem) {
661 memset(&parse, 0, sizeof(parse));
662 parse.idx = -1;
663
664 err = nl80211_parse_key_new(key, &parse);
665 if (err)
666 goto error;
667 err = -EINVAL;
668 if (!parse.p.key)
669 goto error;
670 if (parse.idx < 0 || parse.idx > 4)
671 goto error;
672 if (parse.def) {
673 if (def)
674 goto error;
675 def = 1;
676 result->def = parse.idx;
677 if (!parse.def_uni || !parse.def_multi)
678 goto error;
679 } else if (parse.defmgmt)
680 goto error;
681 err = cfg80211_validate_key_settings(rdev, &parse.p,
682 parse.idx, false, NULL);
683 if (err)
684 goto error;
685 result->params[parse.idx].cipher = parse.p.cipher;
686 result->params[parse.idx].key_len = parse.p.key_len;
687 result->params[parse.idx].key = result->data[parse.idx];
688 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
689 }
690
691 return result;
692 error:
693 kfree(result);
694 return ERR_PTR(err);
695}
696
697static int nl80211_key_allowed(struct wireless_dev *wdev)
698{
699 ASSERT_WDEV_LOCK(wdev);
700
701 switch (wdev->iftype) {
702 case NL80211_IFTYPE_AP:
703 case NL80211_IFTYPE_AP_VLAN:
704 case NL80211_IFTYPE_P2P_GO:
705 case NL80211_IFTYPE_MESH_POINT:
706 break;
707 case NL80211_IFTYPE_ADHOC:
708 if (!wdev->current_bss)
709 return -ENOLINK;
710 break;
711 case NL80211_IFTYPE_STATION:
712 case NL80211_IFTYPE_P2P_CLIENT:
713 if (wdev->sme_state != CFG80211_SME_CONNECTED)
714 return -ENOLINK;
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 return 0;
721}
722
723static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
724{
725 struct nlattr *nl_modes = nla_nest_start(msg, attr);
726 int i;
727
728 if (!nl_modes)
729 goto nla_put_failure;
730
731 i = 0;
732 while (ifmodes) {
733 if (ifmodes & 1)
734 NLA_PUT_FLAG(msg, i);
735 ifmodes >>= 1;
736 i++;
737 }
738
739 nla_nest_end(msg, nl_modes);
740 return 0;
741
742nla_put_failure:
743 return -ENOBUFS;
744}
745
746static int nl80211_put_iface_combinations(struct wiphy *wiphy,
747 struct sk_buff *msg)
748{
749 struct nlattr *nl_combis;
750 int i, j;
751
752 nl_combis = nla_nest_start(msg,
753 NL80211_ATTR_INTERFACE_COMBINATIONS);
754 if (!nl_combis)
755 goto nla_put_failure;
756
757 for (i = 0; i < wiphy->n_iface_combinations; i++) {
758 const struct ieee80211_iface_combination *c;
759 struct nlattr *nl_combi, *nl_limits;
760
761 c = &wiphy->iface_combinations[i];
762
763 nl_combi = nla_nest_start(msg, i + 1);
764 if (!nl_combi)
765 goto nla_put_failure;
766
767 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
768 if (!nl_limits)
769 goto nla_put_failure;
770
771 for (j = 0; j < c->n_limits; j++) {
772 struct nlattr *nl_limit;
773
774 nl_limit = nla_nest_start(msg, j + 1);
775 if (!nl_limit)
776 goto nla_put_failure;
777 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
778 c->limits[j].max);
779 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
780 c->limits[j].types))
781 goto nla_put_failure;
782 nla_nest_end(msg, nl_limit);
783 }
784
785 nla_nest_end(msg, nl_limits);
786
787 if (c->beacon_int_infra_match)
788 NLA_PUT_FLAG(msg,
789 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
790 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
791 c->num_different_channels);
792 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
793 c->max_interfaces);
794
795 nla_nest_end(msg, nl_combi);
796 }
797
798 nla_nest_end(msg, nl_combis);
799
800 return 0;
801nla_put_failure:
802 return -ENOBUFS;
803}
804
805static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
806 struct cfg80211_registered_device *dev)
807{
808 void *hdr;
809 struct nlattr *nl_bands, *nl_band;
810 struct nlattr *nl_freqs, *nl_freq;
811 struct nlattr *nl_rates, *nl_rate;
812 struct nlattr *nl_cmds;
813 enum ieee80211_band band;
814 struct ieee80211_channel *chan;
815 struct ieee80211_rate *rate;
816 int i;
817 const struct ieee80211_txrx_stypes *mgmt_stypes =
818 dev->wiphy.mgmt_stypes;
819
820 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
821 if (!hdr)
822 return -1;
823
824 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
825 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
826
827 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
828 cfg80211_rdev_list_generation);
829
830 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
831 dev->wiphy.retry_short);
832 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
833 dev->wiphy.retry_long);
834 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
835 dev->wiphy.frag_threshold);
836 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
837 dev->wiphy.rts_threshold);
838 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
839 dev->wiphy.coverage_class);
840 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
841 dev->wiphy.max_scan_ssids);
842 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
843 dev->wiphy.max_sched_scan_ssids);
844 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
845 dev->wiphy.max_scan_ie_len);
846 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
847 dev->wiphy.max_sched_scan_ie_len);
848 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
849 dev->wiphy.max_match_sets);
850
851 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
852 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
853 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
854 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
855 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
856 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
857 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
858 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
859 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
860 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
861 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
862 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
863
864 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
865 sizeof(u32) * dev->wiphy.n_cipher_suites,
866 dev->wiphy.cipher_suites);
867
868 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
869 dev->wiphy.max_num_pmkids);
870
871 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
872 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
873
874 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
875 dev->wiphy.available_antennas_tx);
876 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
877 dev->wiphy.available_antennas_rx);
878
879 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
880 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
881 dev->wiphy.probe_resp_offload);
882
883 if ((dev->wiphy.available_antennas_tx ||
884 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
885 u32 tx_ant = 0, rx_ant = 0;
886 int res;
887 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
888 if (!res) {
889 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
890 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
891 }
892 }
893
894 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
895 dev->wiphy.interface_modes))
896 goto nla_put_failure;
897
898 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
899 if (!nl_bands)
900 goto nla_put_failure;
901
902 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
903 if (!dev->wiphy.bands[band])
904 continue;
905
906 nl_band = nla_nest_start(msg, band);
907 if (!nl_band)
908 goto nla_put_failure;
909
910 /* add HT info */
911 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
912 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
913 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
914 &dev->wiphy.bands[band]->ht_cap.mcs);
915 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
916 dev->wiphy.bands[band]->ht_cap.cap);
917 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
918 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
919 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
920 dev->wiphy.bands[band]->ht_cap.ampdu_density);
921 }
922
923 /* add frequencies */
924 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
925 if (!nl_freqs)
926 goto nla_put_failure;
927
928 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
929 nl_freq = nla_nest_start(msg, i);
930 if (!nl_freq)
931 goto nla_put_failure;
932
933 chan = &dev->wiphy.bands[band]->channels[i];
934
935 if (nl80211_msg_put_channel(msg, chan))
936 goto nla_put_failure;
937
938 nla_nest_end(msg, nl_freq);
939 }
940
941 nla_nest_end(msg, nl_freqs);
942
943 /* add bitrates */
944 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
945 if (!nl_rates)
946 goto nla_put_failure;
947
948 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
949 nl_rate = nla_nest_start(msg, i);
950 if (!nl_rate)
951 goto nla_put_failure;
952
953 rate = &dev->wiphy.bands[band]->bitrates[i];
954 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
955 rate->bitrate);
956 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
957 NLA_PUT_FLAG(msg,
958 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
959
960 nla_nest_end(msg, nl_rate);
961 }
962
963 nla_nest_end(msg, nl_rates);
964
965 nla_nest_end(msg, nl_band);
966 }
967 nla_nest_end(msg, nl_bands);
968
969 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
970 if (!nl_cmds)
971 goto nla_put_failure;
972
973 i = 0;
974#define CMD(op, n) \
975 do { \
976 if (dev->ops->op) { \
977 i++; \
978 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
979 } \
980 } while (0)
981
982 CMD(add_virtual_intf, NEW_INTERFACE);
983 CMD(change_virtual_intf, SET_INTERFACE);
984 CMD(add_key, NEW_KEY);
985 CMD(start_ap, START_AP);
986 CMD(add_station, NEW_STATION);
987 CMD(add_mpath, NEW_MPATH);
988 CMD(update_mesh_config, SET_MESH_CONFIG);
989 CMD(change_bss, SET_BSS);
990 CMD(auth, AUTHENTICATE);
991 CMD(assoc, ASSOCIATE);
992 CMD(deauth, DEAUTHENTICATE);
993 CMD(disassoc, DISASSOCIATE);
994 CMD(join_ibss, JOIN_IBSS);
995 CMD(join_mesh, JOIN_MESH);
996 CMD(set_pmksa, SET_PMKSA);
997 CMD(del_pmksa, DEL_PMKSA);
998 CMD(flush_pmksa, FLUSH_PMKSA);
999 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1000 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1001 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1002 CMD(mgmt_tx, FRAME);
1003 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1004 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1005 i++;
1006 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
1007 }
1008 CMD(set_channel, SET_CHANNEL);
1009 CMD(set_wds_peer, SET_WDS_PEER);
1010 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1011 CMD(tdls_mgmt, TDLS_MGMT);
1012 CMD(tdls_oper, TDLS_OPER);
1013 }
1014 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1015 CMD(sched_scan_start, START_SCHED_SCAN);
1016 CMD(probe_client, PROBE_CLIENT);
1017 CMD(set_noack_map, SET_NOACK_MAP);
1018 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1019 i++;
1020 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
1021 }
1022
1023#ifdef CONFIG_NL80211_TESTMODE
1024 CMD(testmode_cmd, TESTMODE);
1025#endif
1026
1027#undef CMD
1028
1029 if (dev->ops->connect || dev->ops->auth) {
1030 i++;
1031 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
1032 }
1033
1034 if (dev->ops->disconnect || dev->ops->deauth) {
1035 i++;
1036 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
1037 }
1038
1039 nla_nest_end(msg, nl_cmds);
1040
1041 if (dev->ops->remain_on_channel &&
1042 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1043 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1044 dev->wiphy.max_remain_on_channel_duration);
1045
1046 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
1047 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
1048
1049 if (mgmt_stypes) {
1050 u16 stypes;
1051 struct nlattr *nl_ftypes, *nl_ifs;
1052 enum nl80211_iftype ift;
1053
1054 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1055 if (!nl_ifs)
1056 goto nla_put_failure;
1057
1058 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1059 nl_ftypes = nla_nest_start(msg, ift);
1060 if (!nl_ftypes)
1061 goto nla_put_failure;
1062 i = 0;
1063 stypes = mgmt_stypes[ift].tx;
1064 while (stypes) {
1065 if (stypes & 1)
1066 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
1067 (i << 4) | IEEE80211_FTYPE_MGMT);
1068 stypes >>= 1;
1069 i++;
1070 }
1071 nla_nest_end(msg, nl_ftypes);
1072 }
1073
1074 nla_nest_end(msg, nl_ifs);
1075
1076 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1077 if (!nl_ifs)
1078 goto nla_put_failure;
1079
1080 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1081 nl_ftypes = nla_nest_start(msg, ift);
1082 if (!nl_ftypes)
1083 goto nla_put_failure;
1084 i = 0;
1085 stypes = mgmt_stypes[ift].rx;
1086 while (stypes) {
1087 if (stypes & 1)
1088 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
1089 (i << 4) | IEEE80211_FTYPE_MGMT);
1090 stypes >>= 1;
1091 i++;
1092 }
1093 nla_nest_end(msg, nl_ftypes);
1094 }
1095 nla_nest_end(msg, nl_ifs);
1096 }
1097
1098 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1099 struct nlattr *nl_wowlan;
1100
1101 nl_wowlan = nla_nest_start(msg,
1102 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1103 if (!nl_wowlan)
1104 goto nla_put_failure;
1105
1106 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
1107 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
1108 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
1109 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1110 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1111 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
1112 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1113 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1114 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1115 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1116 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1117 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1118 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1119 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1120 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1121 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
1122 if (dev->wiphy.wowlan.n_patterns) {
1123 struct nl80211_wowlan_pattern_support pat = {
1124 .max_patterns = dev->wiphy.wowlan.n_patterns,
1125 .min_pattern_len =
1126 dev->wiphy.wowlan.pattern_min_len,
1127 .max_pattern_len =
1128 dev->wiphy.wowlan.pattern_max_len,
1129 };
1130 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1131 sizeof(pat), &pat);
1132 }
1133
1134 nla_nest_end(msg, nl_wowlan);
1135 }
1136
1137 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1138 dev->wiphy.software_iftypes))
1139 goto nla_put_failure;
1140
1141 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1142 goto nla_put_failure;
1143
1144 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1145 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1146 dev->wiphy.ap_sme_capa);
1147
1148 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1149
1150 if (dev->wiphy.ht_capa_mod_mask)
1151 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1152 sizeof(*dev->wiphy.ht_capa_mod_mask),
1153 dev->wiphy.ht_capa_mod_mask);
1154
1155 return genlmsg_end(msg, hdr);
1156
1157 nla_put_failure:
1158 genlmsg_cancel(msg, hdr);
1159 return -EMSGSIZE;
1160}
1161
1162static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1163{
1164 int idx = 0;
1165 int start = cb->args[0];
1166 struct cfg80211_registered_device *dev;
1167
1168 mutex_lock(&cfg80211_mutex);
1169 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
1170 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1171 continue;
1172 if (++idx <= start)
1173 continue;
1174 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1175 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1176 dev) < 0) {
1177 idx--;
1178 break;
1179 }
1180 }
1181 mutex_unlock(&cfg80211_mutex);
1182
1183 cb->args[0] = idx;
1184
1185 return skb->len;
1186}
1187
1188static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1189{
1190 struct sk_buff *msg;
1191 struct cfg80211_registered_device *dev = info->user_ptr[0];
1192
1193 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1194 if (!msg)
1195 return -ENOMEM;
1196
1197 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1198 nlmsg_free(msg);
1199 return -ENOBUFS;
1200 }
1201
1202 return genlmsg_reply(msg, info);
1203}
1204
1205static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1206 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1207 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1208 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1209 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1210 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1211};
1212
1213static int parse_txq_params(struct nlattr *tb[],
1214 struct ieee80211_txq_params *txq_params)
1215{
1216 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1217 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1218 !tb[NL80211_TXQ_ATTR_AIFS])
1219 return -EINVAL;
1220
1221 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1222 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1223 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1224 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1225 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1226
1227 return 0;
1228}
1229
1230static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1231{
1232 /*
1233 * You can only set the channel explicitly for AP, mesh
1234 * and WDS type interfaces; all others have their channel
1235 * managed via their respective "establish a connection"
1236 * command (connect, join, ...)
1237 *
1238 * Monitors are special as they are normally slaved to
1239 * whatever else is going on, so they behave as though
1240 * you tried setting the wiphy channel itself.
1241 */
1242 return !wdev ||
1243 wdev->iftype == NL80211_IFTYPE_AP ||
1244 wdev->iftype == NL80211_IFTYPE_WDS ||
1245 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1246 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1247 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1248}
1249
1250static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1251 struct wireless_dev *wdev,
1252 struct genl_info *info)
1253{
1254 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1255 u32 freq;
1256 int result;
1257
1258 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1259 return -EINVAL;
1260
1261 if (!nl80211_can_set_dev_channel(wdev))
1262 return -EOPNOTSUPP;
1263
1264 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1265 channel_type = nla_get_u32(info->attrs[
1266 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1267 if (channel_type != NL80211_CHAN_NO_HT &&
1268 channel_type != NL80211_CHAN_HT20 &&
1269 channel_type != NL80211_CHAN_HT40PLUS &&
1270 channel_type != NL80211_CHAN_HT40MINUS)
1271 return -EINVAL;
1272 }
1273
1274 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1275
1276 mutex_lock(&rdev->devlist_mtx);
1277 if (wdev) {
1278 wdev_lock(wdev);
1279 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1280 wdev_unlock(wdev);
1281 } else {
1282 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1283 }
1284 mutex_unlock(&rdev->devlist_mtx);
1285
1286 return result;
1287}
1288
1289static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1290{
1291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1292 struct net_device *netdev = info->user_ptr[1];
1293
1294 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1295}
1296
1297static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1298{
1299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1300 struct net_device *dev = info->user_ptr[1];
1301 struct wireless_dev *wdev = dev->ieee80211_ptr;
1302 const u8 *bssid;
1303
1304 if (!info->attrs[NL80211_ATTR_MAC])
1305 return -EINVAL;
1306
1307 if (netif_running(dev))
1308 return -EBUSY;
1309
1310 if (!rdev->ops->set_wds_peer)
1311 return -EOPNOTSUPP;
1312
1313 if (wdev->iftype != NL80211_IFTYPE_WDS)
1314 return -EOPNOTSUPP;
1315
1316 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1317 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
1318}
1319
1320
1321static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1322{
1323 struct cfg80211_registered_device *rdev;
1324 struct net_device *netdev = NULL;
1325 struct wireless_dev *wdev;
1326 int result = 0, rem_txq_params = 0;
1327 struct nlattr *nl_txq_params;
1328 u32 changed;
1329 u8 retry_short = 0, retry_long = 0;
1330 u32 frag_threshold = 0, rts_threshold = 0;
1331 u8 coverage_class = 0;
1332
1333 /*
1334 * Try to find the wiphy and netdev. Normally this
1335 * function shouldn't need the netdev, but this is
1336 * done for backward compatibility -- previously
1337 * setting the channel was done per wiphy, but now
1338 * it is per netdev. Previous userland like hostapd
1339 * also passed a netdev to set_wiphy, so that it is
1340 * possible to let that go to the right netdev!
1341 */
1342 mutex_lock(&cfg80211_mutex);
1343
1344 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1345 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1346
1347 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1348 if (netdev && netdev->ieee80211_ptr) {
1349 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1350 mutex_lock(&rdev->mtx);
1351 } else
1352 netdev = NULL;
1353 }
1354
1355 if (!netdev) {
1356 rdev = __cfg80211_rdev_from_info(info);
1357 if (IS_ERR(rdev)) {
1358 mutex_unlock(&cfg80211_mutex);
1359 return PTR_ERR(rdev);
1360 }
1361 wdev = NULL;
1362 netdev = NULL;
1363 result = 0;
1364
1365 mutex_lock(&rdev->mtx);
1366 } else if (netif_running(netdev) &&
1367 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1368 wdev = netdev->ieee80211_ptr;
1369 else
1370 wdev = NULL;
1371
1372 /*
1373 * end workaround code, by now the rdev is available
1374 * and locked, and wdev may or may not be NULL.
1375 */
1376
1377 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1378 result = cfg80211_dev_rename(
1379 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1380
1381 mutex_unlock(&cfg80211_mutex);
1382
1383 if (result)
1384 goto bad_res;
1385
1386 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1387 struct ieee80211_txq_params txq_params;
1388 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1389
1390 if (!rdev->ops->set_txq_params) {
1391 result = -EOPNOTSUPP;
1392 goto bad_res;
1393 }
1394
1395 if (!netdev) {
1396 result = -EINVAL;
1397 goto bad_res;
1398 }
1399
1400 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1401 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1402 result = -EINVAL;
1403 goto bad_res;
1404 }
1405
1406 if (!netif_running(netdev)) {
1407 result = -ENETDOWN;
1408 goto bad_res;
1409 }
1410
1411 nla_for_each_nested(nl_txq_params,
1412 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1413 rem_txq_params) {
1414 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1415 nla_data(nl_txq_params),
1416 nla_len(nl_txq_params),
1417 txq_params_policy);
1418 result = parse_txq_params(tb, &txq_params);
1419 if (result)
1420 goto bad_res;
1421
1422 result = rdev->ops->set_txq_params(&rdev->wiphy,
1423 netdev,
1424 &txq_params);
1425 if (result)
1426 goto bad_res;
1427 }
1428 }
1429
1430 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1431 result = __nl80211_set_channel(rdev, wdev, info);
1432 if (result)
1433 goto bad_res;
1434 }
1435
1436 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1437 enum nl80211_tx_power_setting type;
1438 int idx, mbm = 0;
1439
1440 if (!rdev->ops->set_tx_power) {
1441 result = -EOPNOTSUPP;
1442 goto bad_res;
1443 }
1444
1445 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1446 type = nla_get_u32(info->attrs[idx]);
1447
1448 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1449 (type != NL80211_TX_POWER_AUTOMATIC)) {
1450 result = -EINVAL;
1451 goto bad_res;
1452 }
1453
1454 if (type != NL80211_TX_POWER_AUTOMATIC) {
1455 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1456 mbm = nla_get_u32(info->attrs[idx]);
1457 }
1458
1459 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1460 if (result)
1461 goto bad_res;
1462 }
1463
1464 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1465 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1466 u32 tx_ant, rx_ant;
1467 if ((!rdev->wiphy.available_antennas_tx &&
1468 !rdev->wiphy.available_antennas_rx) ||
1469 !rdev->ops->set_antenna) {
1470 result = -EOPNOTSUPP;
1471 goto bad_res;
1472 }
1473
1474 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1475 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1476
1477 /* reject antenna configurations which don't match the
1478 * available antenna masks, except for the "all" mask */
1479 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1480 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1481 result = -EINVAL;
1482 goto bad_res;
1483 }
1484
1485 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1486 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1487
1488 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1489 if (result)
1490 goto bad_res;
1491 }
1492
1493 changed = 0;
1494
1495 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1496 retry_short = nla_get_u8(
1497 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1498 if (retry_short == 0) {
1499 result = -EINVAL;
1500 goto bad_res;
1501 }
1502 changed |= WIPHY_PARAM_RETRY_SHORT;
1503 }
1504
1505 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1506 retry_long = nla_get_u8(
1507 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1508 if (retry_long == 0) {
1509 result = -EINVAL;
1510 goto bad_res;
1511 }
1512 changed |= WIPHY_PARAM_RETRY_LONG;
1513 }
1514
1515 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1516 frag_threshold = nla_get_u32(
1517 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1518 if (frag_threshold < 256) {
1519 result = -EINVAL;
1520 goto bad_res;
1521 }
1522 if (frag_threshold != (u32) -1) {
1523 /*
1524 * Fragments (apart from the last one) are required to
1525 * have even length. Make the fragmentation code
1526 * simpler by stripping LSB should someone try to use
1527 * odd threshold value.
1528 */
1529 frag_threshold &= ~0x1;
1530 }
1531 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1532 }
1533
1534 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1535 rts_threshold = nla_get_u32(
1536 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1537 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1538 }
1539
1540 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1541 coverage_class = nla_get_u8(
1542 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1543 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1544 }
1545
1546 if (changed) {
1547 u8 old_retry_short, old_retry_long;
1548 u32 old_frag_threshold, old_rts_threshold;
1549 u8 old_coverage_class;
1550
1551 if (!rdev->ops->set_wiphy_params) {
1552 result = -EOPNOTSUPP;
1553 goto bad_res;
1554 }
1555
1556 old_retry_short = rdev->wiphy.retry_short;
1557 old_retry_long = rdev->wiphy.retry_long;
1558 old_frag_threshold = rdev->wiphy.frag_threshold;
1559 old_rts_threshold = rdev->wiphy.rts_threshold;
1560 old_coverage_class = rdev->wiphy.coverage_class;
1561
1562 if (changed & WIPHY_PARAM_RETRY_SHORT)
1563 rdev->wiphy.retry_short = retry_short;
1564 if (changed & WIPHY_PARAM_RETRY_LONG)
1565 rdev->wiphy.retry_long = retry_long;
1566 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1567 rdev->wiphy.frag_threshold = frag_threshold;
1568 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1569 rdev->wiphy.rts_threshold = rts_threshold;
1570 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1571 rdev->wiphy.coverage_class = coverage_class;
1572
1573 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1574 if (result) {
1575 rdev->wiphy.retry_short = old_retry_short;
1576 rdev->wiphy.retry_long = old_retry_long;
1577 rdev->wiphy.frag_threshold = old_frag_threshold;
1578 rdev->wiphy.rts_threshold = old_rts_threshold;
1579 rdev->wiphy.coverage_class = old_coverage_class;
1580 }
1581 }
1582
1583 bad_res:
1584 mutex_unlock(&rdev->mtx);
1585 if (netdev)
1586 dev_put(netdev);
1587 return result;
1588}
1589
1590
1591static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1592 struct cfg80211_registered_device *rdev,
1593 struct net_device *dev)
1594{
1595 void *hdr;
1596
1597 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1598 if (!hdr)
1599 return -1;
1600
1601 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1602 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1603 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1604 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1605
1606 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1607 rdev->devlist_generation ^
1608 (cfg80211_rdev_list_generation << 2));
1609
1610 return genlmsg_end(msg, hdr);
1611
1612 nla_put_failure:
1613 genlmsg_cancel(msg, hdr);
1614 return -EMSGSIZE;
1615}
1616
1617static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1618{
1619 int wp_idx = 0;
1620 int if_idx = 0;
1621 int wp_start = cb->args[0];
1622 int if_start = cb->args[1];
1623 struct cfg80211_registered_device *rdev;
1624 struct wireless_dev *wdev;
1625
1626 mutex_lock(&cfg80211_mutex);
1627 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1628 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1629 continue;
1630 if (wp_idx < wp_start) {
1631 wp_idx++;
1632 continue;
1633 }
1634 if_idx = 0;
1635
1636 mutex_lock(&rdev->devlist_mtx);
1637 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1638 if (if_idx < if_start) {
1639 if_idx++;
1640 continue;
1641 }
1642 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1643 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1644 rdev, wdev->netdev) < 0) {
1645 mutex_unlock(&rdev->devlist_mtx);
1646 goto out;
1647 }
1648 if_idx++;
1649 }
1650 mutex_unlock(&rdev->devlist_mtx);
1651
1652 wp_idx++;
1653 }
1654 out:
1655 mutex_unlock(&cfg80211_mutex);
1656
1657 cb->args[0] = wp_idx;
1658 cb->args[1] = if_idx;
1659
1660 return skb->len;
1661}
1662
1663static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1664{
1665 struct sk_buff *msg;
1666 struct cfg80211_registered_device *dev = info->user_ptr[0];
1667 struct net_device *netdev = info->user_ptr[1];
1668
1669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1670 if (!msg)
1671 return -ENOMEM;
1672
1673 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1674 dev, netdev) < 0) {
1675 nlmsg_free(msg);
1676 return -ENOBUFS;
1677 }
1678
1679 return genlmsg_reply(msg, info);
1680}
1681
1682static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1683 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1684 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1685 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1686 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1687 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1688};
1689
1690static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1691{
1692 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1693 int flag;
1694
1695 *mntrflags = 0;
1696
1697 if (!nla)
1698 return -EINVAL;
1699
1700 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1701 nla, mntr_flags_policy))
1702 return -EINVAL;
1703
1704 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1705 if (flags[flag])
1706 *mntrflags |= (1<<flag);
1707
1708 return 0;
1709}
1710
1711static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1712 struct net_device *netdev, u8 use_4addr,
1713 enum nl80211_iftype iftype)
1714{
1715 if (!use_4addr) {
1716 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1717 return -EBUSY;
1718 return 0;
1719 }
1720
1721 switch (iftype) {
1722 case NL80211_IFTYPE_AP_VLAN:
1723 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1724 return 0;
1725 break;
1726 case NL80211_IFTYPE_STATION:
1727 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1728 return 0;
1729 break;
1730 default:
1731 break;
1732 }
1733
1734 return -EOPNOTSUPP;
1735}
1736
1737static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1738{
1739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1740 struct vif_params params;
1741 int err;
1742 enum nl80211_iftype otype, ntype;
1743 struct net_device *dev = info->user_ptr[1];
1744 u32 _flags, *flags = NULL;
1745 bool change = false;
1746
1747 memset(&params, 0, sizeof(params));
1748
1749 otype = ntype = dev->ieee80211_ptr->iftype;
1750
1751 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1752 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1753 if (otype != ntype)
1754 change = true;
1755 if (ntype > NL80211_IFTYPE_MAX)
1756 return -EINVAL;
1757 }
1758
1759 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1760 struct wireless_dev *wdev = dev->ieee80211_ptr;
1761
1762 if (ntype != NL80211_IFTYPE_MESH_POINT)
1763 return -EINVAL;
1764 if (netif_running(dev))
1765 return -EBUSY;
1766
1767 wdev_lock(wdev);
1768 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1769 IEEE80211_MAX_MESH_ID_LEN);
1770 wdev->mesh_id_up_len =
1771 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1772 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1773 wdev->mesh_id_up_len);
1774 wdev_unlock(wdev);
1775 }
1776
1777 if (info->attrs[NL80211_ATTR_4ADDR]) {
1778 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1779 change = true;
1780 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1781 if (err)
1782 return err;
1783 } else {
1784 params.use_4addr = -1;
1785 }
1786
1787 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1788 if (ntype != NL80211_IFTYPE_MONITOR)
1789 return -EINVAL;
1790 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1791 &_flags);
1792 if (err)
1793 return err;
1794
1795 flags = &_flags;
1796 change = true;
1797 }
1798
1799 if (change)
1800 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1801 else
1802 err = 0;
1803
1804 if (!err && params.use_4addr != -1)
1805 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1806
1807 return err;
1808}
1809
1810static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1811{
1812 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1813 struct vif_params params;
1814 struct net_device *dev;
1815 int err;
1816 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1817 u32 flags;
1818
1819 memset(&params, 0, sizeof(params));
1820
1821 if (!info->attrs[NL80211_ATTR_IFNAME])
1822 return -EINVAL;
1823
1824 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1825 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1826 if (type > NL80211_IFTYPE_MAX)
1827 return -EINVAL;
1828 }
1829
1830 if (!rdev->ops->add_virtual_intf ||
1831 !(rdev->wiphy.interface_modes & (1 << type)))
1832 return -EOPNOTSUPP;
1833
1834 if (info->attrs[NL80211_ATTR_4ADDR]) {
1835 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1836 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1837 if (err)
1838 return err;
1839 }
1840
1841 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1842 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1843 &flags);
1844 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
1845 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1846 type, err ? NULL : &flags, &params);
1847 if (IS_ERR(dev))
1848 return PTR_ERR(dev);
1849
1850 if (type == NL80211_IFTYPE_MESH_POINT &&
1851 info->attrs[NL80211_ATTR_MESH_ID]) {
1852 struct wireless_dev *wdev = dev->ieee80211_ptr;
1853
1854 wdev_lock(wdev);
1855 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1856 IEEE80211_MAX_MESH_ID_LEN);
1857 wdev->mesh_id_up_len =
1858 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1859 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1860 wdev->mesh_id_up_len);
1861 wdev_unlock(wdev);
1862 }
1863
1864 return 0;
1865}
1866
1867static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1868{
1869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1870 struct net_device *dev = info->user_ptr[1];
1871
1872 if (!rdev->ops->del_virtual_intf)
1873 return -EOPNOTSUPP;
1874
1875 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1876}
1877
1878static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1879{
1880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1881 struct net_device *dev = info->user_ptr[1];
1882 u16 noack_map;
1883
1884 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1885 return -EINVAL;
1886
1887 if (!rdev->ops->set_noack_map)
1888 return -EOPNOTSUPP;
1889
1890 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1891
1892 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1893}
1894
1895struct get_key_cookie {
1896 struct sk_buff *msg;
1897 int error;
1898 int idx;
1899};
1900
1901static void get_key_callback(void *c, struct key_params *params)
1902{
1903 struct nlattr *key;
1904 struct get_key_cookie *cookie = c;
1905
1906 if (params->key)
1907 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1908 params->key_len, params->key);
1909
1910 if (params->seq)
1911 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1912 params->seq_len, params->seq);
1913
1914 if (params->cipher)
1915 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1916 params->cipher);
1917
1918 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1919 if (!key)
1920 goto nla_put_failure;
1921
1922 if (params->key)
1923 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1924 params->key_len, params->key);
1925
1926 if (params->seq)
1927 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1928 params->seq_len, params->seq);
1929
1930 if (params->cipher)
1931 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1932 params->cipher);
1933
1934 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1935
1936 nla_nest_end(cookie->msg, key);
1937
1938 return;
1939 nla_put_failure:
1940 cookie->error = 1;
1941}
1942
1943static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1944{
1945 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1946 int err;
1947 struct net_device *dev = info->user_ptr[1];
1948 u8 key_idx = 0;
1949 const u8 *mac_addr = NULL;
1950 bool pairwise;
1951 struct get_key_cookie cookie = {
1952 .error = 0,
1953 };
1954 void *hdr;
1955 struct sk_buff *msg;
1956
1957 if (info->attrs[NL80211_ATTR_KEY_IDX])
1958 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1959
1960 if (key_idx > 5)
1961 return -EINVAL;
1962
1963 if (info->attrs[NL80211_ATTR_MAC])
1964 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1965
1966 pairwise = !!mac_addr;
1967 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1968 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1969 if (kt >= NUM_NL80211_KEYTYPES)
1970 return -EINVAL;
1971 if (kt != NL80211_KEYTYPE_GROUP &&
1972 kt != NL80211_KEYTYPE_PAIRWISE)
1973 return -EINVAL;
1974 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1975 }
1976
1977 if (!rdev->ops->get_key)
1978 return -EOPNOTSUPP;
1979
1980 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1981 return -ENOENT;
1982
1983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1984 if (!msg)
1985 return -ENOMEM;
1986
1987 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1988 NL80211_CMD_NEW_KEY);
1989 if (IS_ERR(hdr))
1990 return PTR_ERR(hdr);
1991
1992 cookie.msg = msg;
1993 cookie.idx = key_idx;
1994
1995 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1996 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1997 if (mac_addr)
1998 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1999
2000 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2001 mac_addr, &cookie, get_key_callback);
2002
2003 if (err)
2004 goto free_msg;
2005
2006 if (cookie.error)
2007 goto nla_put_failure;
2008
2009 genlmsg_end(msg, hdr);
2010 return genlmsg_reply(msg, info);
2011
2012 nla_put_failure:
2013 err = -ENOBUFS;
2014 free_msg:
2015 nlmsg_free(msg);
2016 return err;
2017}
2018
2019static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2020{
2021 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2022 struct key_parse key;
2023 int err;
2024 struct net_device *dev = info->user_ptr[1];
2025
2026 err = nl80211_parse_key(info, &key);
2027 if (err)
2028 return err;
2029
2030 if (key.idx < 0)
2031 return -EINVAL;
2032
2033 /* only support setting default key */
2034 if (!key.def && !key.defmgmt)
2035 return -EINVAL;
2036
2037 wdev_lock(dev->ieee80211_ptr);
2038
2039 if (key.def) {
2040 if (!rdev->ops->set_default_key) {
2041 err = -EOPNOTSUPP;
2042 goto out;
2043 }
2044
2045 err = nl80211_key_allowed(dev->ieee80211_ptr);
2046 if (err)
2047 goto out;
2048
2049 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2050 key.def_uni, key.def_multi);
2051
2052 if (err)
2053 goto out;
2054
2055#ifdef CONFIG_CFG80211_WEXT
2056 dev->ieee80211_ptr->wext.default_key = key.idx;
2057#endif
2058 } else {
2059 if (key.def_uni || !key.def_multi) {
2060 err = -EINVAL;
2061 goto out;
2062 }
2063
2064 if (!rdev->ops->set_default_mgmt_key) {
2065 err = -EOPNOTSUPP;
2066 goto out;
2067 }
2068
2069 err = nl80211_key_allowed(dev->ieee80211_ptr);
2070 if (err)
2071 goto out;
2072
2073 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2074 dev, key.idx);
2075 if (err)
2076 goto out;
2077
2078#ifdef CONFIG_CFG80211_WEXT
2079 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2080#endif
2081 }
2082
2083 out:
2084 wdev_unlock(dev->ieee80211_ptr);
2085
2086 return err;
2087}
2088
2089static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2090{
2091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2092 int err;
2093 struct net_device *dev = info->user_ptr[1];
2094 struct key_parse key;
2095 const u8 *mac_addr = NULL;
2096
2097 err = nl80211_parse_key(info, &key);
2098 if (err)
2099 return err;
2100
2101 if (!key.p.key)
2102 return -EINVAL;
2103
2104 if (info->attrs[NL80211_ATTR_MAC])
2105 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2106
2107 if (key.type == -1) {
2108 if (mac_addr)
2109 key.type = NL80211_KEYTYPE_PAIRWISE;
2110 else
2111 key.type = NL80211_KEYTYPE_GROUP;
2112 }
2113
2114 /* for now */
2115 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2116 key.type != NL80211_KEYTYPE_GROUP)
2117 return -EINVAL;
2118
2119 if (!rdev->ops->add_key)
2120 return -EOPNOTSUPP;
2121
2122 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2123 key.type == NL80211_KEYTYPE_PAIRWISE,
2124 mac_addr))
2125 return -EINVAL;
2126
2127 wdev_lock(dev->ieee80211_ptr);
2128 err = nl80211_key_allowed(dev->ieee80211_ptr);
2129 if (!err)
2130 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
2131 key.type == NL80211_KEYTYPE_PAIRWISE,
2132 mac_addr, &key.p);
2133 wdev_unlock(dev->ieee80211_ptr);
2134
2135 return err;
2136}
2137
2138static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2139{
2140 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2141 int err;
2142 struct net_device *dev = info->user_ptr[1];
2143 u8 *mac_addr = NULL;
2144 struct key_parse key;
2145
2146 err = nl80211_parse_key(info, &key);
2147 if (err)
2148 return err;
2149
2150 if (info->attrs[NL80211_ATTR_MAC])
2151 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2152
2153 if (key.type == -1) {
2154 if (mac_addr)
2155 key.type = NL80211_KEYTYPE_PAIRWISE;
2156 else
2157 key.type = NL80211_KEYTYPE_GROUP;
2158 }
2159
2160 /* for now */
2161 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2162 key.type != NL80211_KEYTYPE_GROUP)
2163 return -EINVAL;
2164
2165 if (!rdev->ops->del_key)
2166 return -EOPNOTSUPP;
2167
2168 wdev_lock(dev->ieee80211_ptr);
2169 err = nl80211_key_allowed(dev->ieee80211_ptr);
2170
2171 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
2172 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2173 err = -ENOENT;
2174
2175 if (!err)
2176 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2177 key.type == NL80211_KEYTYPE_PAIRWISE,
2178 mac_addr);
2179
2180#ifdef CONFIG_CFG80211_WEXT
2181 if (!err) {
2182 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2183 dev->ieee80211_ptr->wext.default_key = -1;
2184 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2185 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2186 }
2187#endif
2188 wdev_unlock(dev->ieee80211_ptr);
2189
2190 return err;
2191}
2192
2193static int nl80211_parse_beacon(struct genl_info *info,
2194 struct cfg80211_beacon_data *bcn)
2195{
2196 bool haveinfo = false;
2197
2198 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2199 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2200 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2201 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
2202 return -EINVAL;
2203
2204 memset(bcn, 0, sizeof(*bcn));
2205
2206 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2207 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2208 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2209 if (!bcn->head_len)
2210 return -EINVAL;
2211 haveinfo = true;
2212 }
2213
2214 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2215 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2216 bcn->tail_len =
2217 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2218 haveinfo = true;
2219 }
2220
2221 if (!haveinfo)
2222 return -EINVAL;
2223
2224 if (info->attrs[NL80211_ATTR_IE]) {
2225 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2226 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2227 }
2228
2229 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2230 bcn->proberesp_ies =
2231 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2232 bcn->proberesp_ies_len =
2233 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2234 }
2235
2236 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2237 bcn->assocresp_ies =
2238 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2239 bcn->assocresp_ies_len =
2240 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2241 }
2242
2243 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2244 bcn->probe_resp =
2245 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2246 bcn->probe_resp_len =
2247 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2248 }
2249
2250 return 0;
2251}
2252
2253static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2254{
2255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2256 struct net_device *dev = info->user_ptr[1];
2257 struct wireless_dev *wdev = dev->ieee80211_ptr;
2258 struct cfg80211_ap_settings params;
2259 int err;
2260
2261 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2262 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2263 return -EOPNOTSUPP;
2264
2265 if (!rdev->ops->start_ap)
2266 return -EOPNOTSUPP;
2267
2268 if (wdev->beacon_interval)
2269 return -EALREADY;
2270
2271 memset(&params, 0, sizeof(params));
2272
2273 /* these are required for START_AP */
2274 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2275 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2276 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2277 return -EINVAL;
2278
2279 err = nl80211_parse_beacon(info, &params.beacon);
2280 if (err)
2281 return err;
2282
2283 params.beacon_interval =
2284 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2285 params.dtim_period =
2286 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2287
2288 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2289 if (err)
2290 return err;
2291
2292 /*
2293 * In theory, some of these attributes should be required here
2294 * but since they were not used when the command was originally
2295 * added, keep them optional for old user space programs to let
2296 * them continue to work with drivers that do not need the
2297 * additional information -- drivers must check!
2298 */
2299 if (info->attrs[NL80211_ATTR_SSID]) {
2300 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2301 params.ssid_len =
2302 nla_len(info->attrs[NL80211_ATTR_SSID]);
2303 if (params.ssid_len == 0 ||
2304 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2305 return -EINVAL;
2306 }
2307
2308 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2309 params.hidden_ssid = nla_get_u32(
2310 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2311 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2312 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2313 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2314 return -EINVAL;
2315 }
2316
2317 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2318
2319 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2320 params.auth_type = nla_get_u32(
2321 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2322 if (!nl80211_valid_auth_type(params.auth_type))
2323 return -EINVAL;
2324 } else
2325 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2326
2327 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2328 NL80211_MAX_NR_CIPHER_SUITES);
2329 if (err)
2330 return err;
2331
2332 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2333 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2334 return -EOPNOTSUPP;
2335 params.inactivity_timeout = nla_get_u16(
2336 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2337 }
2338
2339 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2340 if (!err)
2341 wdev->beacon_interval = params.beacon_interval;
2342 return err;
2343}
2344
2345static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2346{
2347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2348 struct net_device *dev = info->user_ptr[1];
2349 struct wireless_dev *wdev = dev->ieee80211_ptr;
2350 struct cfg80211_beacon_data params;
2351 int err;
2352
2353 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2354 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2355 return -EOPNOTSUPP;
2356
2357 if (!rdev->ops->change_beacon)
2358 return -EOPNOTSUPP;
2359
2360 if (!wdev->beacon_interval)
2361 return -EINVAL;
2362
2363 err = nl80211_parse_beacon(info, &params);
2364 if (err)
2365 return err;
2366
2367 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2368}
2369
2370static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
2371{
2372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2373 struct net_device *dev = info->user_ptr[1];
2374 struct wireless_dev *wdev = dev->ieee80211_ptr;
2375 int err;
2376
2377 if (!rdev->ops->stop_ap)
2378 return -EOPNOTSUPP;
2379
2380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2381 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2382 return -EOPNOTSUPP;
2383
2384 if (!wdev->beacon_interval)
2385 return -ENOENT;
2386
2387 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
2388 if (!err)
2389 wdev->beacon_interval = 0;
2390 return err;
2391}
2392
2393static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2394 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2395 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2396 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2397 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2398 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2399 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
2400};
2401
2402static int parse_station_flags(struct genl_info *info,
2403 enum nl80211_iftype iftype,
2404 struct station_parameters *params)
2405{
2406 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2407 struct nlattr *nla;
2408 int flag;
2409
2410 /*
2411 * Try parsing the new attribute first so userspace
2412 * can specify both for older kernels.
2413 */
2414 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2415 if (nla) {
2416 struct nl80211_sta_flag_update *sta_flags;
2417
2418 sta_flags = nla_data(nla);
2419 params->sta_flags_mask = sta_flags->mask;
2420 params->sta_flags_set = sta_flags->set;
2421 if ((params->sta_flags_mask |
2422 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2423 return -EINVAL;
2424 return 0;
2425 }
2426
2427 /* if present, parse the old attribute */
2428
2429 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2430 if (!nla)
2431 return 0;
2432
2433 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2434 nla, sta_flags_policy))
2435 return -EINVAL;
2436
2437 /*
2438 * Only allow certain flags for interface types so that
2439 * other attributes are silently ignored. Remember that
2440 * this is backward compatibility code with old userspace
2441 * and shouldn't be hit in other cases anyway.
2442 */
2443 switch (iftype) {
2444 case NL80211_IFTYPE_AP:
2445 case NL80211_IFTYPE_AP_VLAN:
2446 case NL80211_IFTYPE_P2P_GO:
2447 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2448 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2449 BIT(NL80211_STA_FLAG_WME) |
2450 BIT(NL80211_STA_FLAG_MFP);
2451 break;
2452 case NL80211_IFTYPE_P2P_CLIENT:
2453 case NL80211_IFTYPE_STATION:
2454 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2455 BIT(NL80211_STA_FLAG_TDLS_PEER);
2456 break;
2457 case NL80211_IFTYPE_MESH_POINT:
2458 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2459 BIT(NL80211_STA_FLAG_MFP) |
2460 BIT(NL80211_STA_FLAG_AUTHORIZED);
2461 default:
2462 return -EINVAL;
2463 }
2464
2465 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2466 if (flags[flag])
2467 params->sta_flags_set |= (1<<flag);
2468
2469 return 0;
2470}
2471
2472static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2473 int attr)
2474{
2475 struct nlattr *rate;
2476 u16 bitrate;
2477
2478 rate = nla_nest_start(msg, attr);
2479 if (!rate)
2480 goto nla_put_failure;
2481
2482 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2483 bitrate = cfg80211_calculate_bitrate(info);
2484 if (bitrate > 0)
2485 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2486
2487 if (info->flags & RATE_INFO_FLAGS_MCS)
2488 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2489 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2490 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2491 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2492 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2493
2494 nla_nest_end(msg, rate);
2495 return true;
2496
2497nla_put_failure:
2498 return false;
2499}
2500
2501static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2502 int flags,
2503 struct cfg80211_registered_device *rdev,
2504 struct net_device *dev,
2505 const u8 *mac_addr, struct station_info *sinfo)
2506{
2507 void *hdr;
2508 struct nlattr *sinfoattr, *bss_param;
2509
2510 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2511 if (!hdr)
2512 return -1;
2513
2514 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2515 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2516
2517 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2518
2519 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2520 if (!sinfoattr)
2521 goto nla_put_failure;
2522 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2523 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2524 sinfo->connected_time);
2525 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2526 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2527 sinfo->inactive_time);
2528 if (sinfo->filled & STATION_INFO_RX_BYTES)
2529 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2530 sinfo->rx_bytes);
2531 if (sinfo->filled & STATION_INFO_TX_BYTES)
2532 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2533 sinfo->tx_bytes);
2534 if (sinfo->filled & STATION_INFO_LLID)
2535 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2536 sinfo->llid);
2537 if (sinfo->filled & STATION_INFO_PLID)
2538 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2539 sinfo->plid);
2540 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2541 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2542 sinfo->plink_state);
2543 switch (rdev->wiphy.signal_type) {
2544 case CFG80211_SIGNAL_TYPE_MBM:
2545 if (sinfo->filled & STATION_INFO_SIGNAL)
2546 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2547 sinfo->signal);
2548 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2549 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2550 sinfo->signal_avg);
2551 break;
2552 default:
2553 break;
2554 }
2555 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2556 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2557 NL80211_STA_INFO_TX_BITRATE))
2558 goto nla_put_failure;
2559 }
2560 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2561 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2562 NL80211_STA_INFO_RX_BITRATE))
2563 goto nla_put_failure;
2564 }
2565 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2566 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2567 sinfo->rx_packets);
2568 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2569 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2570 sinfo->tx_packets);
2571 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2572 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2573 sinfo->tx_retries);
2574 if (sinfo->filled & STATION_INFO_TX_FAILED)
2575 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2576 sinfo->tx_failed);
2577 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2578 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2579 sinfo->beacon_loss_count);
2580 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2581 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2582 if (!bss_param)
2583 goto nla_put_failure;
2584
2585 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2586 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2587 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2588 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2589 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2590 NLA_PUT_FLAG(msg,
2591 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2592 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2593 sinfo->bss_param.dtim_period);
2594 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2595 sinfo->bss_param.beacon_interval);
2596
2597 nla_nest_end(msg, bss_param);
2598 }
2599 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2600 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2601 sizeof(struct nl80211_sta_flag_update),
2602 &sinfo->sta_flags);
2603 nla_nest_end(msg, sinfoattr);
2604
2605 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
2606 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2607 sinfo->assoc_req_ies);
2608
2609 return genlmsg_end(msg, hdr);
2610
2611 nla_put_failure:
2612 genlmsg_cancel(msg, hdr);
2613 return -EMSGSIZE;
2614}
2615
2616static int nl80211_dump_station(struct sk_buff *skb,
2617 struct netlink_callback *cb)
2618{
2619 struct station_info sinfo;
2620 struct cfg80211_registered_device *dev;
2621 struct net_device *netdev;
2622 u8 mac_addr[ETH_ALEN];
2623 int sta_idx = cb->args[1];
2624 int err;
2625
2626 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2627 if (err)
2628 return err;
2629
2630 if (!dev->ops->dump_station) {
2631 err = -EOPNOTSUPP;
2632 goto out_err;
2633 }
2634
2635 while (1) {
2636 memset(&sinfo, 0, sizeof(sinfo));
2637 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2638 mac_addr, &sinfo);
2639 if (err == -ENOENT)
2640 break;
2641 if (err)
2642 goto out_err;
2643
2644 if (nl80211_send_station(skb,
2645 NETLINK_CB(cb->skb).pid,
2646 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2647 dev, netdev, mac_addr,
2648 &sinfo) < 0)
2649 goto out;
2650
2651 sta_idx++;
2652 }
2653
2654
2655 out:
2656 cb->args[1] = sta_idx;
2657 err = skb->len;
2658 out_err:
2659 nl80211_finish_netdev_dump(dev);
2660
2661 return err;
2662}
2663
2664static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2665{
2666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2667 struct net_device *dev = info->user_ptr[1];
2668 struct station_info sinfo;
2669 struct sk_buff *msg;
2670 u8 *mac_addr = NULL;
2671 int err;
2672
2673 memset(&sinfo, 0, sizeof(sinfo));
2674
2675 if (!info->attrs[NL80211_ATTR_MAC])
2676 return -EINVAL;
2677
2678 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2679
2680 if (!rdev->ops->get_station)
2681 return -EOPNOTSUPP;
2682
2683 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2684 if (err)
2685 return err;
2686
2687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2688 if (!msg)
2689 return -ENOMEM;
2690
2691 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2692 rdev, dev, mac_addr, &sinfo) < 0) {
2693 nlmsg_free(msg);
2694 return -ENOBUFS;
2695 }
2696
2697 return genlmsg_reply(msg, info);
2698}
2699
2700/*
2701 * Get vlan interface making sure it is running and on the right wiphy.
2702 */
2703static struct net_device *get_vlan(struct genl_info *info,
2704 struct cfg80211_registered_device *rdev)
2705{
2706 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2707 struct net_device *v;
2708 int ret;
2709
2710 if (!vlanattr)
2711 return NULL;
2712
2713 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2714 if (!v)
2715 return ERR_PTR(-ENODEV);
2716
2717 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2718 ret = -EINVAL;
2719 goto error;
2720 }
2721
2722 if (!netif_running(v)) {
2723 ret = -ENETDOWN;
2724 goto error;
2725 }
2726
2727 return v;
2728 error:
2729 dev_put(v);
2730 return ERR_PTR(ret);
2731}
2732
2733static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2734{
2735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2736 int err;
2737 struct net_device *dev = info->user_ptr[1];
2738 struct station_parameters params;
2739 u8 *mac_addr = NULL;
2740
2741 memset(&params, 0, sizeof(params));
2742
2743 params.listen_interval = -1;
2744 params.plink_state = -1;
2745
2746 if (info->attrs[NL80211_ATTR_STA_AID])
2747 return -EINVAL;
2748
2749 if (!info->attrs[NL80211_ATTR_MAC])
2750 return -EINVAL;
2751
2752 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2753
2754 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2755 params.supported_rates =
2756 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2757 params.supported_rates_len =
2758 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2759 }
2760
2761 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2762 params.listen_interval =
2763 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2764
2765 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2766 params.ht_capa =
2767 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2768
2769 if (!rdev->ops->change_station)
2770 return -EOPNOTSUPP;
2771
2772 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2773 return -EINVAL;
2774
2775 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2776 params.plink_action =
2777 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2778
2779 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2780 params.plink_state =
2781 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2782
2783 switch (dev->ieee80211_ptr->iftype) {
2784 case NL80211_IFTYPE_AP:
2785 case NL80211_IFTYPE_AP_VLAN:
2786 case NL80211_IFTYPE_P2P_GO:
2787 /* disallow mesh-specific things */
2788 if (params.plink_action)
2789 return -EINVAL;
2790
2791 /* TDLS can't be set, ... */
2792 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2793 return -EINVAL;
2794 /*
2795 * ... but don't bother the driver with it. This works around
2796 * a hostapd/wpa_supplicant issue -- it always includes the
2797 * TLDS_PEER flag in the mask even for AP mode.
2798 */
2799 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2800
2801 /* accept only the listed bits */
2802 if (params.sta_flags_mask &
2803 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2804 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2805 BIT(NL80211_STA_FLAG_WME) |
2806 BIT(NL80211_STA_FLAG_MFP)))
2807 return -EINVAL;
2808
2809 /* must be last in here for error handling */
2810 params.vlan = get_vlan(info, rdev);
2811 if (IS_ERR(params.vlan))
2812 return PTR_ERR(params.vlan);
2813 break;
2814 case NL80211_IFTYPE_P2P_CLIENT:
2815 case NL80211_IFTYPE_STATION:
2816 /*
2817 * Don't allow userspace to change the TDLS_PEER flag,
2818 * but silently ignore attempts to change it since we
2819 * don't have state here to verify that it doesn't try
2820 * to change the flag.
2821 */
2822 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2823 /* fall through */
2824 case NL80211_IFTYPE_ADHOC:
2825 /* disallow things sta doesn't support */
2826 if (params.plink_action)
2827 return -EINVAL;
2828 if (params.ht_capa)
2829 return -EINVAL;
2830 if (params.listen_interval >= 0)
2831 return -EINVAL;
2832 /* reject any changes other than AUTHORIZED */
2833 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2834 return -EINVAL;
2835 break;
2836 case NL80211_IFTYPE_MESH_POINT:
2837 /* disallow things mesh doesn't support */
2838 if (params.vlan)
2839 return -EINVAL;
2840 if (params.ht_capa)
2841 return -EINVAL;
2842 if (params.listen_interval >= 0)
2843 return -EINVAL;
2844 /*
2845 * No special handling for TDLS here -- the userspace
2846 * mesh code doesn't have this bug.
2847 */
2848 if (params.sta_flags_mask &
2849 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2850 BIT(NL80211_STA_FLAG_MFP) |
2851 BIT(NL80211_STA_FLAG_AUTHORIZED)))
2852 return -EINVAL;
2853 break;
2854 default:
2855 return -EOPNOTSUPP;
2856 }
2857
2858 /* be aware of params.vlan when changing code here */
2859
2860 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2861
2862 if (params.vlan)
2863 dev_put(params.vlan);
2864
2865 return err;
2866}
2867
2868static struct nla_policy
2869nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2870 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2871 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2872};
2873
2874static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2875{
2876 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2877 int err;
2878 struct net_device *dev = info->user_ptr[1];
2879 struct station_parameters params;
2880 u8 *mac_addr = NULL;
2881
2882 memset(&params, 0, sizeof(params));
2883
2884 if (!info->attrs[NL80211_ATTR_MAC])
2885 return -EINVAL;
2886
2887 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2888 return -EINVAL;
2889
2890 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2891 return -EINVAL;
2892
2893 if (!info->attrs[NL80211_ATTR_STA_AID])
2894 return -EINVAL;
2895
2896 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2897 params.supported_rates =
2898 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2899 params.supported_rates_len =
2900 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2901 params.listen_interval =
2902 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2903
2904 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2905 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2906 return -EINVAL;
2907
2908 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2909 params.ht_capa =
2910 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2911
2912 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2913 params.plink_action =
2914 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2915
2916 if (!rdev->ops->add_station)
2917 return -EOPNOTSUPP;
2918
2919 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2920 return -EINVAL;
2921
2922 /* HT requires QoS, but if we don't have that just ignore HT/VHT
2923 * as userspace might just pass through the capabilities from the IEs
2924 * directly, rather than enforcing this restriction and returning an
2925 * error in this case.
2926 */
2927 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
2928 params.ht_capa = NULL;
2929
2930 switch (dev->ieee80211_ptr->iftype) {
2931 case NL80211_IFTYPE_AP:
2932 case NL80211_IFTYPE_AP_VLAN:
2933 case NL80211_IFTYPE_P2P_GO:
2934 /* parse WME attributes if sta is WME capable */
2935 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2936 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2937 info->attrs[NL80211_ATTR_STA_WME]) {
2938 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2939 struct nlattr *nla;
2940
2941 nla = info->attrs[NL80211_ATTR_STA_WME];
2942 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2943 nl80211_sta_wme_policy);
2944 if (err)
2945 return err;
2946
2947 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2948 params.uapsd_queues =
2949 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2950 if (params.uapsd_queues &
2951 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2952 return -EINVAL;
2953
2954 if (tb[NL80211_STA_WME_MAX_SP])
2955 params.max_sp =
2956 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2957
2958 if (params.max_sp &
2959 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2960 return -EINVAL;
2961
2962 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2963 }
2964 /* TDLS peers cannot be added */
2965 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2966 return -EINVAL;
2967 /* but don't bother the driver with it */
2968 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2969
2970 /* must be last in here for error handling */
2971 params.vlan = get_vlan(info, rdev);
2972 if (IS_ERR(params.vlan))
2973 return PTR_ERR(params.vlan);
2974 break;
2975 case NL80211_IFTYPE_MESH_POINT:
2976 /* TDLS peers cannot be added */
2977 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2978 return -EINVAL;
2979 break;
2980 case NL80211_IFTYPE_STATION:
2981 /* Only TDLS peers can be added */
2982 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2983 return -EINVAL;
2984 /* Can only add if TDLS ... */
2985 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2986 return -EOPNOTSUPP;
2987 /* ... with external setup is supported */
2988 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2989 return -EOPNOTSUPP;
2990 break;
2991 default:
2992 return -EOPNOTSUPP;
2993 }
2994
2995 /* be aware of params.vlan when changing code here */
2996
2997 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2998
2999 if (params.vlan)
3000 dev_put(params.vlan);
3001 return err;
3002}
3003
3004static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3005{
3006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3007 struct net_device *dev = info->user_ptr[1];
3008 u8 *mac_addr = NULL;
3009
3010 if (info->attrs[NL80211_ATTR_MAC])
3011 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3012
3013 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3014 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3015 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
3016 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3017 return -EINVAL;
3018
3019 if (!rdev->ops->del_station)
3020 return -EOPNOTSUPP;
3021
3022 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
3023}
3024
3025static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3026 int flags, struct net_device *dev,
3027 u8 *dst, u8 *next_hop,
3028 struct mpath_info *pinfo)
3029{
3030 void *hdr;
3031 struct nlattr *pinfoattr;
3032
3033 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3034 if (!hdr)
3035 return -1;
3036
3037 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3038 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
3039 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
3040
3041 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
3042
3043 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3044 if (!pinfoattr)
3045 goto nla_put_failure;
3046 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
3047 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3048 pinfo->frame_qlen);
3049 if (pinfo->filled & MPATH_INFO_SN)
3050 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
3051 pinfo->sn);
3052 if (pinfo->filled & MPATH_INFO_METRIC)
3053 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
3054 pinfo->metric);
3055 if (pinfo->filled & MPATH_INFO_EXPTIME)
3056 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
3057 pinfo->exptime);
3058 if (pinfo->filled & MPATH_INFO_FLAGS)
3059 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
3060 pinfo->flags);
3061 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
3062 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3063 pinfo->discovery_timeout);
3064 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
3065 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3066 pinfo->discovery_retries);
3067
3068 nla_nest_end(msg, pinfoattr);
3069
3070 return genlmsg_end(msg, hdr);
3071
3072 nla_put_failure:
3073 genlmsg_cancel(msg, hdr);
3074 return -EMSGSIZE;
3075}
3076
3077static int nl80211_dump_mpath(struct sk_buff *skb,
3078 struct netlink_callback *cb)
3079{
3080 struct mpath_info pinfo;
3081 struct cfg80211_registered_device *dev;
3082 struct net_device *netdev;
3083 u8 dst[ETH_ALEN];
3084 u8 next_hop[ETH_ALEN];
3085 int path_idx = cb->args[1];
3086 int err;
3087
3088 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3089 if (err)
3090 return err;
3091
3092 if (!dev->ops->dump_mpath) {
3093 err = -EOPNOTSUPP;
3094 goto out_err;
3095 }
3096
3097 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3098 err = -EOPNOTSUPP;
3099 goto out_err;
3100 }
3101
3102 while (1) {
3103 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3104 dst, next_hop, &pinfo);
3105 if (err == -ENOENT)
3106 break;
3107 if (err)
3108 goto out_err;
3109
3110 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3111 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3112 netdev, dst, next_hop,
3113 &pinfo) < 0)
3114 goto out;
3115
3116 path_idx++;
3117 }
3118
3119
3120 out:
3121 cb->args[1] = path_idx;
3122 err = skb->len;
3123 out_err:
3124 nl80211_finish_netdev_dump(dev);
3125 return err;
3126}
3127
3128static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3129{
3130 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3131 int err;
3132 struct net_device *dev = info->user_ptr[1];
3133 struct mpath_info pinfo;
3134 struct sk_buff *msg;
3135 u8 *dst = NULL;
3136 u8 next_hop[ETH_ALEN];
3137
3138 memset(&pinfo, 0, sizeof(pinfo));
3139
3140 if (!info->attrs[NL80211_ATTR_MAC])
3141 return -EINVAL;
3142
3143 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3144
3145 if (!rdev->ops->get_mpath)
3146 return -EOPNOTSUPP;
3147
3148 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3149 return -EOPNOTSUPP;
3150
3151 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
3152 if (err)
3153 return err;
3154
3155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3156 if (!msg)
3157 return -ENOMEM;
3158
3159 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
3160 dev, dst, next_hop, &pinfo) < 0) {
3161 nlmsg_free(msg);
3162 return -ENOBUFS;
3163 }
3164
3165 return genlmsg_reply(msg, info);
3166}
3167
3168static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3169{
3170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3171 struct net_device *dev = info->user_ptr[1];
3172 u8 *dst = NULL;
3173 u8 *next_hop = NULL;
3174
3175 if (!info->attrs[NL80211_ATTR_MAC])
3176 return -EINVAL;
3177
3178 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3179 return -EINVAL;
3180
3181 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3182 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3183
3184 if (!rdev->ops->change_mpath)
3185 return -EOPNOTSUPP;
3186
3187 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3188 return -EOPNOTSUPP;
3189
3190 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
3191}
3192
3193static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3194{
3195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3196 struct net_device *dev = info->user_ptr[1];
3197 u8 *dst = NULL;
3198 u8 *next_hop = NULL;
3199
3200 if (!info->attrs[NL80211_ATTR_MAC])
3201 return -EINVAL;
3202
3203 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3204 return -EINVAL;
3205
3206 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3207 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3208
3209 if (!rdev->ops->add_mpath)
3210 return -EOPNOTSUPP;
3211
3212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3213 return -EOPNOTSUPP;
3214
3215 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
3216}
3217
3218static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3219{
3220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3221 struct net_device *dev = info->user_ptr[1];
3222 u8 *dst = NULL;
3223
3224 if (info->attrs[NL80211_ATTR_MAC])
3225 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3226
3227 if (!rdev->ops->del_mpath)
3228 return -EOPNOTSUPP;
3229
3230 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
3231}
3232
3233static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3234{
3235 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3236 struct net_device *dev = info->user_ptr[1];
3237 struct bss_parameters params;
3238
3239 memset(&params, 0, sizeof(params));
3240 /* default to not changing parameters */
3241 params.use_cts_prot = -1;
3242 params.use_short_preamble = -1;
3243 params.use_short_slot_time = -1;
3244 params.ap_isolate = -1;
3245 params.ht_opmode = -1;
3246
3247 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3248 params.use_cts_prot =
3249 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3250 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3251 params.use_short_preamble =
3252 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3253 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3254 params.use_short_slot_time =
3255 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
3256 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3257 params.basic_rates =
3258 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3259 params.basic_rates_len =
3260 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3261 }
3262 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3263 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
3264 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3265 params.ht_opmode =
3266 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
3267
3268 if (!rdev->ops->change_bss)
3269 return -EOPNOTSUPP;
3270
3271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3272 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3273 return -EOPNOTSUPP;
3274
3275 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
3276}
3277
3278static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
3279 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3280 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3281 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3282 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3283 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3284 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3285};
3286
3287static int parse_reg_rule(struct nlattr *tb[],
3288 struct ieee80211_reg_rule *reg_rule)
3289{
3290 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3291 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3292
3293 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3294 return -EINVAL;
3295 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3296 return -EINVAL;
3297 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3298 return -EINVAL;
3299 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3300 return -EINVAL;
3301 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3302 return -EINVAL;
3303
3304 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3305
3306 freq_range->start_freq_khz =
3307 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3308 freq_range->end_freq_khz =
3309 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3310 freq_range->max_bandwidth_khz =
3311 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3312
3313 power_rule->max_eirp =
3314 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3315
3316 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3317 power_rule->max_antenna_gain =
3318 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3319
3320 return 0;
3321}
3322
3323static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3324{
3325 int r;
3326 char *data = NULL;
3327
3328 /*
3329 * You should only get this when cfg80211 hasn't yet initialized
3330 * completely when built-in to the kernel right between the time
3331 * window between nl80211_init() and regulatory_init(), if that is
3332 * even possible.
3333 */
3334 mutex_lock(&cfg80211_mutex);
3335 if (unlikely(!cfg80211_regdomain)) {
3336 mutex_unlock(&cfg80211_mutex);
3337 return -EINPROGRESS;
3338 }
3339 mutex_unlock(&cfg80211_mutex);
3340
3341 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3342 return -EINVAL;
3343
3344 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3345
3346 r = regulatory_hint_user(data);
3347
3348 return r;
3349}
3350
3351static int nl80211_get_mesh_config(struct sk_buff *skb,
3352 struct genl_info *info)
3353{
3354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3355 struct net_device *dev = info->user_ptr[1];
3356 struct wireless_dev *wdev = dev->ieee80211_ptr;
3357 struct mesh_config cur_params;
3358 int err = 0;
3359 void *hdr;
3360 struct nlattr *pinfoattr;
3361 struct sk_buff *msg;
3362
3363 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3364 return -EOPNOTSUPP;
3365
3366 if (!rdev->ops->get_mesh_config)
3367 return -EOPNOTSUPP;
3368
3369 wdev_lock(wdev);
3370 /* If not connected, get default parameters */
3371 if (!wdev->mesh_id_len)
3372 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3373 else
3374 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
3375 &cur_params);
3376 wdev_unlock(wdev);
3377
3378 if (err)
3379 return err;
3380
3381 /* Draw up a netlink message to send back */
3382 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3383 if (!msg)
3384 return -ENOMEM;
3385 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3386 NL80211_CMD_GET_MESH_CONFIG);
3387 if (!hdr)
3388 goto out;
3389 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
3390 if (!pinfoattr)
3391 goto nla_put_failure;
3392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3393 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3394 cur_params.dot11MeshRetryTimeout);
3395 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3396 cur_params.dot11MeshConfirmTimeout);
3397 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3398 cur_params.dot11MeshHoldingTimeout);
3399 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3400 cur_params.dot11MeshMaxPeerLinks);
3401 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3402 cur_params.dot11MeshMaxRetries);
3403 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3404 cur_params.dot11MeshTTL);
3405 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3406 cur_params.element_ttl);
3407 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3408 cur_params.auto_open_plinks);
3409 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3410 cur_params.dot11MeshHWMPmaxPREQretries);
3411 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3412 cur_params.path_refresh_time);
3413 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3414 cur_params.min_discovery_timeout);
3415 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3416 cur_params.dot11MeshHWMPactivePathTimeout);
3417 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3418 cur_params.dot11MeshHWMPpreqMinInterval);
3419 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3420 cur_params.dot11MeshHWMPperrMinInterval);
3421 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3422 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
3423 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3424 cur_params.dot11MeshHWMPRootMode);
3425 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3426 cur_params.dot11MeshHWMPRannInterval);
3427 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3428 cur_params.dot11MeshGateAnnouncementProtocol);
3429 NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3430 cur_params.dot11MeshForwarding);
3431 NLA_PUT_U32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3432 cur_params.rssi_threshold);
3433 nla_nest_end(msg, pinfoattr);
3434 genlmsg_end(msg, hdr);
3435 return genlmsg_reply(msg, info);
3436
3437 nla_put_failure:
3438 genlmsg_cancel(msg, hdr);
3439 out:
3440 nlmsg_free(msg);
3441 return -ENOBUFS;
3442}
3443
3444static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
3445 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3446 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3447 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3448 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3449 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3450 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
3451 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
3452 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3453
3454 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3455 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3456 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3457 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3458 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3459 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
3460 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3461 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
3462 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
3463 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
3464 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
3465 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
3466};
3467
3468static const struct nla_policy
3469 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3470 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3471 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
3472 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
3473 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
3474 .len = IEEE80211_MAX_DATA_LEN },
3475 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
3476};
3477
3478static int nl80211_parse_mesh_config(struct genl_info *info,
3479 struct mesh_config *cfg,
3480 u32 *mask_out)
3481{
3482 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
3483 u32 mask = 0;
3484
3485#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3486do {\
3487 if (table[attr_num]) {\
3488 cfg->param = nla_fn(table[attr_num]); \
3489 mask |= (1 << (attr_num - 1)); \
3490 } \
3491} while (0);\
3492
3493
3494 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
3495 return -EINVAL;
3496 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
3497 info->attrs[NL80211_ATTR_MESH_CONFIG],
3498 nl80211_meshconf_params_policy))
3499 return -EINVAL;
3500
3501 /* This makes sure that there aren't more than 32 mesh config
3502 * parameters (otherwise our bitfield scheme would not work.) */
3503 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3504
3505 /* Fill in the params struct */
3506 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3507 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3508 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3509 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3510 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3511 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3512 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3513 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3514 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3515 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3516 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3517 mask, NL80211_MESHCONF_TTL, nla_get_u8);
3518 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3519 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
3520 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3521 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3522 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3523 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3524 nla_get_u8);
3525 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3526 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3527 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3528 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3529 nla_get_u16);
3530 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3531 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3532 nla_get_u32);
3533 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3534 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3535 nla_get_u16);
3536 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3537 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3538 nla_get_u16);
3539 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3540 dot11MeshHWMPnetDiameterTraversalTime,
3541 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3542 nla_get_u16);
3543 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3544 dot11MeshHWMPRootMode, mask,
3545 NL80211_MESHCONF_HWMP_ROOTMODE,
3546 nla_get_u8);
3547 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3548 dot11MeshHWMPRannInterval, mask,
3549 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3550 nla_get_u16);
3551 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3552 dot11MeshGateAnnouncementProtocol, mask,
3553 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3554 nla_get_u8);
3555 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3556 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
3557 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3558 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
3559 if (mask_out)
3560 *mask_out = mask;
3561
3562 return 0;
3563
3564#undef FILL_IN_MESH_PARAM_IF_SET
3565}
3566
3567static int nl80211_parse_mesh_setup(struct genl_info *info,
3568 struct mesh_setup *setup)
3569{
3570 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3571
3572 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3573 return -EINVAL;
3574 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3575 info->attrs[NL80211_ATTR_MESH_SETUP],
3576 nl80211_mesh_setup_params_policy))
3577 return -EINVAL;
3578
3579 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3580 setup->path_sel_proto =
3581 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3582 IEEE80211_PATH_PROTOCOL_VENDOR :
3583 IEEE80211_PATH_PROTOCOL_HWMP;
3584
3585 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3586 setup->path_metric =
3587 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3588 IEEE80211_PATH_METRIC_VENDOR :
3589 IEEE80211_PATH_METRIC_AIRTIME;
3590
3591
3592 if (tb[NL80211_MESH_SETUP_IE]) {
3593 struct nlattr *ieattr =
3594 tb[NL80211_MESH_SETUP_IE];
3595 if (!is_valid_ie_attr(ieattr))
3596 return -EINVAL;
3597 setup->ie = nla_data(ieattr);
3598 setup->ie_len = nla_len(ieattr);
3599 }
3600 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3601 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3602
3603 return 0;
3604}
3605
3606static int nl80211_update_mesh_config(struct sk_buff *skb,
3607 struct genl_info *info)
3608{
3609 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3610 struct net_device *dev = info->user_ptr[1];
3611 struct wireless_dev *wdev = dev->ieee80211_ptr;
3612 struct mesh_config cfg;
3613 u32 mask;
3614 int err;
3615
3616 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3617 return -EOPNOTSUPP;
3618
3619 if (!rdev->ops->update_mesh_config)
3620 return -EOPNOTSUPP;
3621
3622 err = nl80211_parse_mesh_config(info, &cfg, &mask);
3623 if (err)
3624 return err;
3625
3626 wdev_lock(wdev);
3627 if (!wdev->mesh_id_len)
3628 err = -ENOLINK;
3629
3630 if (!err)
3631 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3632 mask, &cfg);
3633
3634 wdev_unlock(wdev);
3635
3636 return err;
3637}
3638
3639static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3640{
3641 struct sk_buff *msg;
3642 void *hdr = NULL;
3643 struct nlattr *nl_reg_rules;
3644 unsigned int i;
3645 int err = -EINVAL;
3646
3647 mutex_lock(&cfg80211_mutex);
3648
3649 if (!cfg80211_regdomain)
3650 goto out;
3651
3652 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3653 if (!msg) {
3654 err = -ENOBUFS;
3655 goto out;
3656 }
3657
3658 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3659 NL80211_CMD_GET_REG);
3660 if (!hdr)
3661 goto put_failure;
3662
3663 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3664 cfg80211_regdomain->alpha2);
3665 if (cfg80211_regdomain->dfs_region)
3666 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3667 cfg80211_regdomain->dfs_region);
3668
3669 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3670 if (!nl_reg_rules)
3671 goto nla_put_failure;
3672
3673 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3674 struct nlattr *nl_reg_rule;
3675 const struct ieee80211_reg_rule *reg_rule;
3676 const struct ieee80211_freq_range *freq_range;
3677 const struct ieee80211_power_rule *power_rule;
3678
3679 reg_rule = &cfg80211_regdomain->reg_rules[i];
3680 freq_range = &reg_rule->freq_range;
3681 power_rule = &reg_rule->power_rule;
3682
3683 nl_reg_rule = nla_nest_start(msg, i);
3684 if (!nl_reg_rule)
3685 goto nla_put_failure;
3686
3687 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3688 reg_rule->flags);
3689 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3690 freq_range->start_freq_khz);
3691 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3692 freq_range->end_freq_khz);
3693 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3694 freq_range->max_bandwidth_khz);
3695 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3696 power_rule->max_antenna_gain);
3697 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3698 power_rule->max_eirp);
3699
3700 nla_nest_end(msg, nl_reg_rule);
3701 }
3702
3703 nla_nest_end(msg, nl_reg_rules);
3704
3705 genlmsg_end(msg, hdr);
3706 err = genlmsg_reply(msg, info);
3707 goto out;
3708
3709nla_put_failure:
3710 genlmsg_cancel(msg, hdr);
3711put_failure:
3712 nlmsg_free(msg);
3713 err = -EMSGSIZE;
3714out:
3715 mutex_unlock(&cfg80211_mutex);
3716 return err;
3717}
3718
3719static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3720{
3721 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3722 struct nlattr *nl_reg_rule;
3723 char *alpha2 = NULL;
3724 int rem_reg_rules = 0, r = 0;
3725 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3726 u8 dfs_region = 0;
3727 struct ieee80211_regdomain *rd = NULL;
3728
3729 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3730 return -EINVAL;
3731
3732 if (!info->attrs[NL80211_ATTR_REG_RULES])
3733 return -EINVAL;
3734
3735 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3736
3737 if (info->attrs[NL80211_ATTR_DFS_REGION])
3738 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3739
3740 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3741 rem_reg_rules) {
3742 num_rules++;
3743 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3744 return -EINVAL;
3745 }
3746
3747 mutex_lock(&cfg80211_mutex);
3748
3749 if (!reg_is_valid_request(alpha2)) {
3750 r = -EINVAL;
3751 goto bad_reg;
3752 }
3753
3754 size_of_regd = sizeof(struct ieee80211_regdomain) +
3755 (num_rules * sizeof(struct ieee80211_reg_rule));
3756
3757 rd = kzalloc(size_of_regd, GFP_KERNEL);
3758 if (!rd) {
3759 r = -ENOMEM;
3760 goto bad_reg;
3761 }
3762
3763 rd->n_reg_rules = num_rules;
3764 rd->alpha2[0] = alpha2[0];
3765 rd->alpha2[1] = alpha2[1];
3766
3767 /*
3768 * Disable DFS master mode if the DFS region was
3769 * not supported or known on this kernel.
3770 */
3771 if (reg_supported_dfs_region(dfs_region))
3772 rd->dfs_region = dfs_region;
3773
3774 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3775 rem_reg_rules) {
3776 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3777 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3778 reg_rule_policy);
3779 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3780 if (r)
3781 goto bad_reg;
3782
3783 rule_idx++;
3784
3785 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3786 r = -EINVAL;
3787 goto bad_reg;
3788 }
3789 }
3790
3791 BUG_ON(rule_idx != num_rules);
3792
3793 r = set_regdom(rd);
3794
3795 mutex_unlock(&cfg80211_mutex);
3796
3797 return r;
3798
3799 bad_reg:
3800 mutex_unlock(&cfg80211_mutex);
3801 kfree(rd);
3802 return r;
3803}
3804
3805static int validate_scan_freqs(struct nlattr *freqs)
3806{
3807 struct nlattr *attr1, *attr2;
3808 int n_channels = 0, tmp1, tmp2;
3809
3810 nla_for_each_nested(attr1, freqs, tmp1) {
3811 n_channels++;
3812 /*
3813 * Some hardware has a limited channel list for
3814 * scanning, and it is pretty much nonsensical
3815 * to scan for a channel twice, so disallow that
3816 * and don't require drivers to check that the
3817 * channel list they get isn't longer than what
3818 * they can scan, as long as they can scan all
3819 * the channels they registered at once.
3820 */
3821 nla_for_each_nested(attr2, freqs, tmp2)
3822 if (attr1 != attr2 &&
3823 nla_get_u32(attr1) == nla_get_u32(attr2))
3824 return 0;
3825 }
3826
3827 return n_channels;
3828}
3829
3830static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3831{
3832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3833 struct net_device *dev = info->user_ptr[1];
3834 struct cfg80211_scan_request *request;
3835 struct nlattr *attr;
3836 struct wiphy *wiphy;
3837 int err, tmp, n_ssids = 0, n_channels, i;
3838 size_t ie_len;
3839
3840 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3841 return -EINVAL;
3842
3843 wiphy = &rdev->wiphy;
3844
3845 if (!rdev->ops->scan)
3846 return -EOPNOTSUPP;
3847
3848 if (rdev->scan_req)
3849 return -EBUSY;
3850
3851 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3852 n_channels = validate_scan_freqs(
3853 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3854 if (!n_channels)
3855 return -EINVAL;
3856 } else {
3857 enum ieee80211_band band;
3858 n_channels = 0;
3859
3860 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3861 if (wiphy->bands[band])
3862 n_channels += wiphy->bands[band]->n_channels;
3863 }
3864
3865 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3866 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3867 n_ssids++;
3868
3869 if (n_ssids > wiphy->max_scan_ssids)
3870 return -EINVAL;
3871
3872 if (info->attrs[NL80211_ATTR_IE])
3873 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3874 else
3875 ie_len = 0;
3876
3877 if (ie_len > wiphy->max_scan_ie_len)
3878 return -EINVAL;
3879
3880 request = kzalloc(sizeof(*request)
3881 + sizeof(*request->ssids) * n_ssids
3882 + sizeof(*request->channels) * n_channels
3883 + ie_len, GFP_KERNEL);
3884 if (!request)
3885 return -ENOMEM;
3886
3887 if (n_ssids)
3888 request->ssids = (void *)&request->channels[n_channels];
3889 request->n_ssids = n_ssids;
3890 if (ie_len) {
3891 if (request->ssids)
3892 request->ie = (void *)(request->ssids + n_ssids);
3893 else
3894 request->ie = (void *)(request->channels + n_channels);
3895 }
3896
3897 i = 0;
3898 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3899 /* user specified, bail out if channel not found */
3900 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3901 struct ieee80211_channel *chan;
3902
3903 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3904
3905 if (!chan) {
3906 err = -EINVAL;
3907 goto out_free;
3908 }
3909
3910 /* ignore disabled channels */
3911 if (chan->flags & IEEE80211_CHAN_DISABLED)
3912 continue;
3913
3914 request->channels[i] = chan;
3915 i++;
3916 }
3917 } else {
3918 enum ieee80211_band band;
3919
3920 /* all channels */
3921 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3922 int j;
3923 if (!wiphy->bands[band])
3924 continue;
3925 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3926 struct ieee80211_channel *chan;
3927
3928 chan = &wiphy->bands[band]->channels[j];
3929
3930 if (chan->flags & IEEE80211_CHAN_DISABLED)
3931 continue;
3932
3933 request->channels[i] = chan;
3934 i++;
3935 }
3936 }
3937 }
3938
3939 if (!i) {
3940 err = -EINVAL;
3941 goto out_free;
3942 }
3943
3944 request->n_channels = i;
3945
3946 i = 0;
3947 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3948 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3949 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3950 err = -EINVAL;
3951 goto out_free;
3952 }
3953 request->ssids[i].ssid_len = nla_len(attr);
3954 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3955 i++;
3956 }
3957 }
3958
3959 if (info->attrs[NL80211_ATTR_IE]) {
3960 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3961 memcpy((void *)request->ie,
3962 nla_data(info->attrs[NL80211_ATTR_IE]),
3963 request->ie_len);
3964 }
3965
3966 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
3967 if (wiphy->bands[i])
3968 request->rates[i] =
3969 (1 << wiphy->bands[i]->n_bitrates) - 1;
3970
3971 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3972 nla_for_each_nested(attr,
3973 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3974 tmp) {
3975 enum ieee80211_band band = nla_type(attr);
3976
3977 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
3978 err = -EINVAL;
3979 goto out_free;
3980 }
3981 err = ieee80211_get_ratemask(wiphy->bands[band],
3982 nla_data(attr),
3983 nla_len(attr),
3984 &request->rates[band]);
3985 if (err)
3986 goto out_free;
3987 }
3988 }
3989
3990 request->no_cck =
3991 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3992
3993 request->dev = dev;
3994 request->wiphy = &rdev->wiphy;
3995
3996 rdev->scan_req = request;
3997 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3998
3999 if (!err) {
4000 nl80211_send_scan_start(rdev, dev);
4001 dev_hold(dev);
4002 } else {
4003 out_free:
4004 rdev->scan_req = NULL;
4005 kfree(request);
4006 }
4007
4008 return err;
4009}
4010
4011static int nl80211_start_sched_scan(struct sk_buff *skb,
4012 struct genl_info *info)
4013{
4014 struct cfg80211_sched_scan_request *request;
4015 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4016 struct net_device *dev = info->user_ptr[1];
4017 struct nlattr *attr;
4018 struct wiphy *wiphy;
4019 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
4020 u32 interval;
4021 enum ieee80211_band band;
4022 size_t ie_len;
4023 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
4024
4025 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4026 !rdev->ops->sched_scan_start)
4027 return -EOPNOTSUPP;
4028
4029 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4030 return -EINVAL;
4031
4032 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4033 return -EINVAL;
4034
4035 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4036 if (interval == 0)
4037 return -EINVAL;
4038
4039 wiphy = &rdev->wiphy;
4040
4041 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4042 n_channels = validate_scan_freqs(
4043 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4044 if (!n_channels)
4045 return -EINVAL;
4046 } else {
4047 n_channels = 0;
4048
4049 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4050 if (wiphy->bands[band])
4051 n_channels += wiphy->bands[band]->n_channels;
4052 }
4053
4054 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4055 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4056 tmp)
4057 n_ssids++;
4058
4059 if (n_ssids > wiphy->max_sched_scan_ssids)
4060 return -EINVAL;
4061
4062 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4063 nla_for_each_nested(attr,
4064 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4065 tmp)
4066 n_match_sets++;
4067
4068 if (n_match_sets > wiphy->max_match_sets)
4069 return -EINVAL;
4070
4071 if (info->attrs[NL80211_ATTR_IE])
4072 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4073 else
4074 ie_len = 0;
4075
4076 if (ie_len > wiphy->max_sched_scan_ie_len)
4077 return -EINVAL;
4078
4079 mutex_lock(&rdev->sched_scan_mtx);
4080
4081 if (rdev->sched_scan_req) {
4082 err = -EINPROGRESS;
4083 goto out;
4084 }
4085
4086 request = kzalloc(sizeof(*request)
4087 + sizeof(*request->ssids) * n_ssids
4088 + sizeof(*request->match_sets) * n_match_sets
4089 + sizeof(*request->channels) * n_channels
4090 + ie_len, GFP_KERNEL);
4091 if (!request) {
4092 err = -ENOMEM;
4093 goto out;
4094 }
4095
4096 if (n_ssids)
4097 request->ssids = (void *)&request->channels[n_channels];
4098 request->n_ssids = n_ssids;
4099 if (ie_len) {
4100 if (request->ssids)
4101 request->ie = (void *)(request->ssids + n_ssids);
4102 else
4103 request->ie = (void *)(request->channels + n_channels);
4104 }
4105
4106 if (n_match_sets) {
4107 if (request->ie)
4108 request->match_sets = (void *)(request->ie + ie_len);
4109 else if (request->ssids)
4110 request->match_sets =
4111 (void *)(request->ssids + n_ssids);
4112 else
4113 request->match_sets =
4114 (void *)(request->channels + n_channels);
4115 }
4116 request->n_match_sets = n_match_sets;
4117
4118 i = 0;
4119 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4120 /* user specified, bail out if channel not found */
4121 nla_for_each_nested(attr,
4122 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4123 tmp) {
4124 struct ieee80211_channel *chan;
4125
4126 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4127
4128 if (!chan) {
4129 err = -EINVAL;
4130 goto out_free;
4131 }
4132
4133 /* ignore disabled channels */
4134 if (chan->flags & IEEE80211_CHAN_DISABLED)
4135 continue;
4136
4137 request->channels[i] = chan;
4138 i++;
4139 }
4140 } else {
4141 /* all channels */
4142 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4143 int j;
4144 if (!wiphy->bands[band])
4145 continue;
4146 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4147 struct ieee80211_channel *chan;
4148
4149 chan = &wiphy->bands[band]->channels[j];
4150
4151 if (chan->flags & IEEE80211_CHAN_DISABLED)
4152 continue;
4153
4154 request->channels[i] = chan;
4155 i++;
4156 }
4157 }
4158 }
4159
4160 if (!i) {
4161 err = -EINVAL;
4162 goto out_free;
4163 }
4164
4165 request->n_channels = i;
4166
4167 i = 0;
4168 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4169 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4170 tmp) {
4171 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
4172 err = -EINVAL;
4173 goto out_free;
4174 }
4175 request->ssids[i].ssid_len = nla_len(attr);
4176 memcpy(request->ssids[i].ssid, nla_data(attr),
4177 nla_len(attr));
4178 i++;
4179 }
4180 }
4181
4182 i = 0;
4183 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4184 nla_for_each_nested(attr,
4185 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4186 tmp) {
4187 struct nlattr *ssid;
4188
4189 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4190 nla_data(attr), nla_len(attr),
4191 nl80211_match_policy);
4192 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4193 if (ssid) {
4194 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4195 err = -EINVAL;
4196 goto out_free;
4197 }
4198 memcpy(request->match_sets[i].ssid.ssid,
4199 nla_data(ssid), nla_len(ssid));
4200 request->match_sets[i].ssid.ssid_len =
4201 nla_len(ssid);
4202 }
4203 i++;
4204 }
4205 }
4206
4207 if (info->attrs[NL80211_ATTR_IE]) {
4208 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4209 memcpy((void *)request->ie,
4210 nla_data(info->attrs[NL80211_ATTR_IE]),
4211 request->ie_len);
4212 }
4213
4214 request->dev = dev;
4215 request->wiphy = &rdev->wiphy;
4216 request->interval = interval;
4217
4218 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4219 if (!err) {
4220 rdev->sched_scan_req = request;
4221 nl80211_send_sched_scan(rdev, dev,
4222 NL80211_CMD_START_SCHED_SCAN);
4223 goto out;
4224 }
4225
4226out_free:
4227 kfree(request);
4228out:
4229 mutex_unlock(&rdev->sched_scan_mtx);
4230 return err;
4231}
4232
4233static int nl80211_stop_sched_scan(struct sk_buff *skb,
4234 struct genl_info *info)
4235{
4236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4237 int err;
4238
4239 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4240 !rdev->ops->sched_scan_stop)
4241 return -EOPNOTSUPP;
4242
4243 mutex_lock(&rdev->sched_scan_mtx);
4244 err = __cfg80211_stop_sched_scan(rdev, false);
4245 mutex_unlock(&rdev->sched_scan_mtx);
4246
4247 return err;
4248}
4249
4250static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4251 u32 seq, int flags,
4252 struct cfg80211_registered_device *rdev,
4253 struct wireless_dev *wdev,
4254 struct cfg80211_internal_bss *intbss)
4255{
4256 struct cfg80211_bss *res = &intbss->pub;
4257 void *hdr;
4258 struct nlattr *bss;
4259
4260 ASSERT_WDEV_LOCK(wdev);
4261
4262 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
4263 NL80211_CMD_NEW_SCAN_RESULTS);
4264 if (!hdr)
4265 return -1;
4266
4267 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4268
4269 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
4270 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
4271
4272 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4273 if (!bss)
4274 goto nla_put_failure;
4275 if (!is_zero_ether_addr(res->bssid))
4276 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4277 if (res->information_elements && res->len_information_elements)
4278 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4279 res->len_information_elements,
4280 res->information_elements);
4281 if (res->beacon_ies && res->len_beacon_ies &&
4282 res->beacon_ies != res->information_elements)
4283 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4284 res->len_beacon_ies, res->beacon_ies);
4285 if (res->tsf)
4286 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4287 if (res->beacon_interval)
4288 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4289 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4290 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
4291 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4292 jiffies_to_msecs(jiffies - intbss->ts));
4293
4294 switch (rdev->wiphy.signal_type) {
4295 case CFG80211_SIGNAL_TYPE_MBM:
4296 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4297 break;
4298 case CFG80211_SIGNAL_TYPE_UNSPEC:
4299 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4300 break;
4301 default:
4302 break;
4303 }
4304
4305 switch (wdev->iftype) {
4306 case NL80211_IFTYPE_P2P_CLIENT:
4307 case NL80211_IFTYPE_STATION:
4308 if (intbss == wdev->current_bss)
4309 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4310 NL80211_BSS_STATUS_ASSOCIATED);
4311 break;
4312 case NL80211_IFTYPE_ADHOC:
4313 if (intbss == wdev->current_bss)
4314 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4315 NL80211_BSS_STATUS_IBSS_JOINED);
4316 break;
4317 default:
4318 break;
4319 }
4320
4321 nla_nest_end(msg, bss);
4322
4323 return genlmsg_end(msg, hdr);
4324
4325 nla_put_failure:
4326 genlmsg_cancel(msg, hdr);
4327 return -EMSGSIZE;
4328}
4329
4330static int nl80211_dump_scan(struct sk_buff *skb,
4331 struct netlink_callback *cb)
4332{
4333 struct cfg80211_registered_device *rdev;
4334 struct net_device *dev;
4335 struct cfg80211_internal_bss *scan;
4336 struct wireless_dev *wdev;
4337 int start = cb->args[1], idx = 0;
4338 int err;
4339
4340 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4341 if (err)
4342 return err;
4343
4344 wdev = dev->ieee80211_ptr;
4345
4346 wdev_lock(wdev);
4347 spin_lock_bh(&rdev->bss_lock);
4348 cfg80211_bss_expire(rdev);
4349
4350 cb->seq = rdev->bss_generation;
4351
4352 list_for_each_entry(scan, &rdev->bss_list, list) {
4353 if (++idx <= start)
4354 continue;
4355 if (nl80211_send_bss(skb, cb,
4356 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4357 rdev, wdev, scan) < 0) {
4358 idx--;
4359 break;
4360 }
4361 }
4362
4363 spin_unlock_bh(&rdev->bss_lock);
4364 wdev_unlock(wdev);
4365
4366 cb->args[1] = idx;
4367 nl80211_finish_netdev_dump(rdev);
4368
4369 return skb->len;
4370}
4371
4372static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4373 int flags, struct net_device *dev,
4374 struct survey_info *survey)
4375{
4376 void *hdr;
4377 struct nlattr *infoattr;
4378
4379 hdr = nl80211hdr_put(msg, pid, seq, flags,
4380 NL80211_CMD_NEW_SURVEY_RESULTS);
4381 if (!hdr)
4382 return -ENOMEM;
4383
4384 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4385
4386 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4387 if (!infoattr)
4388 goto nla_put_failure;
4389
4390 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4391 survey->channel->center_freq);
4392 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4393 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4394 survey->noise);
4395 if (survey->filled & SURVEY_INFO_IN_USE)
4396 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
4397 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4398 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4399 survey->channel_time);
4400 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4401 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4402 survey->channel_time_busy);
4403 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4404 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4405 survey->channel_time_ext_busy);
4406 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4407 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4408 survey->channel_time_rx);
4409 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4410 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4411 survey->channel_time_tx);
4412
4413 nla_nest_end(msg, infoattr);
4414
4415 return genlmsg_end(msg, hdr);
4416
4417 nla_put_failure:
4418 genlmsg_cancel(msg, hdr);
4419 return -EMSGSIZE;
4420}
4421
4422static int nl80211_dump_survey(struct sk_buff *skb,
4423 struct netlink_callback *cb)
4424{
4425 struct survey_info survey;
4426 struct cfg80211_registered_device *dev;
4427 struct net_device *netdev;
4428 int survey_idx = cb->args[1];
4429 int res;
4430
4431 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4432 if (res)
4433 return res;
4434
4435 if (!dev->ops->dump_survey) {
4436 res = -EOPNOTSUPP;
4437 goto out_err;
4438 }
4439
4440 while (1) {
4441 struct ieee80211_channel *chan;
4442
4443 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4444 &survey);
4445 if (res == -ENOENT)
4446 break;
4447 if (res)
4448 goto out_err;
4449
4450 /* Survey without a channel doesn't make sense */
4451 if (!survey.channel) {
4452 res = -EINVAL;
4453 goto out;
4454 }
4455
4456 chan = ieee80211_get_channel(&dev->wiphy,
4457 survey.channel->center_freq);
4458 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4459 survey_idx++;
4460 continue;
4461 }
4462
4463 if (nl80211_send_survey(skb,
4464 NETLINK_CB(cb->skb).pid,
4465 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4466 netdev,
4467 &survey) < 0)
4468 goto out;
4469 survey_idx++;
4470 }
4471
4472 out:
4473 cb->args[1] = survey_idx;
4474 res = skb->len;
4475 out_err:
4476 nl80211_finish_netdev_dump(dev);
4477 return res;
4478}
4479
4480static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4481{
4482 return auth_type <= NL80211_AUTHTYPE_MAX;
4483}
4484
4485static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4486{
4487 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4488 NL80211_WPA_VERSION_2));
4489}
4490
4491static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4492{
4493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
4495 struct ieee80211_channel *chan;
4496 const u8 *bssid, *ssid, *ie = NULL;
4497 int err, ssid_len, ie_len = 0;
4498 enum nl80211_auth_type auth_type;
4499 struct key_parse key;
4500 bool local_state_change;
4501
4502 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4503 return -EINVAL;
4504
4505 if (!info->attrs[NL80211_ATTR_MAC])
4506 return -EINVAL;
4507
4508 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4509 return -EINVAL;
4510
4511 if (!info->attrs[NL80211_ATTR_SSID])
4512 return -EINVAL;
4513
4514 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4515 return -EINVAL;
4516
4517 err = nl80211_parse_key(info, &key);
4518 if (err)
4519 return err;
4520
4521 if (key.idx >= 0) {
4522 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4523 return -EINVAL;
4524 if (!key.p.key || !key.p.key_len)
4525 return -EINVAL;
4526 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4527 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4528 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4529 key.p.key_len != WLAN_KEY_LEN_WEP104))
4530 return -EINVAL;
4531 if (key.idx > 4)
4532 return -EINVAL;
4533 } else {
4534 key.p.key_len = 0;
4535 key.p.key = NULL;
4536 }
4537
4538 if (key.idx >= 0) {
4539 int i;
4540 bool ok = false;
4541 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4542 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4543 ok = true;
4544 break;
4545 }
4546 }
4547 if (!ok)
4548 return -EINVAL;
4549 }
4550
4551 if (!rdev->ops->auth)
4552 return -EOPNOTSUPP;
4553
4554 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4555 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4556 return -EOPNOTSUPP;
4557
4558 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4559 chan = ieee80211_get_channel(&rdev->wiphy,
4560 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4561 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4562 return -EINVAL;
4563
4564 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4565 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4566
4567 if (info->attrs[NL80211_ATTR_IE]) {
4568 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4569 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4570 }
4571
4572 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4573 if (!nl80211_valid_auth_type(auth_type))
4574 return -EINVAL;
4575
4576 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4577
4578 /*
4579 * Since we no longer track auth state, ignore
4580 * requests to only change local state.
4581 */
4582 if (local_state_change)
4583 return 0;
4584
4585 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4586 ssid, ssid_len, ie, ie_len,
4587 key.p.key, key.p.key_len, key.idx);
4588}
4589
4590static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4591 struct genl_info *info,
4592 struct cfg80211_crypto_settings *settings,
4593 int cipher_limit)
4594{
4595 memset(settings, 0, sizeof(*settings));
4596
4597 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4598
4599 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4600 u16 proto;
4601 proto = nla_get_u16(
4602 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4603 settings->control_port_ethertype = cpu_to_be16(proto);
4604 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4605 proto != ETH_P_PAE)
4606 return -EINVAL;
4607 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4608 settings->control_port_no_encrypt = true;
4609 } else
4610 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4611
4612 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4613 void *data;
4614 int len, i;
4615
4616 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4617 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4618 settings->n_ciphers_pairwise = len / sizeof(u32);
4619
4620 if (len % sizeof(u32))
4621 return -EINVAL;
4622
4623 if (settings->n_ciphers_pairwise > cipher_limit)
4624 return -EINVAL;
4625
4626 memcpy(settings->ciphers_pairwise, data, len);
4627
4628 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4629 if (!cfg80211_supported_cipher_suite(
4630 &rdev->wiphy,
4631 settings->ciphers_pairwise[i]))
4632 return -EINVAL;
4633 }
4634
4635 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4636 settings->cipher_group =
4637 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4638 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4639 settings->cipher_group))
4640 return -EINVAL;
4641 }
4642
4643 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4644 settings->wpa_versions =
4645 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4646 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4647 return -EINVAL;
4648 }
4649
4650 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4651 void *data;
4652 int len;
4653
4654 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4655 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4656 settings->n_akm_suites = len / sizeof(u32);
4657
4658 if (len % sizeof(u32))
4659 return -EINVAL;
4660
4661 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4662 return -EINVAL;
4663
4664 memcpy(settings->akm_suites, data, len);
4665 }
4666
4667 return 0;
4668}
4669
4670static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4671{
4672 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4673 struct net_device *dev = info->user_ptr[1];
4674 struct cfg80211_crypto_settings crypto;
4675 struct ieee80211_channel *chan;
4676 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4677 int err, ssid_len, ie_len = 0;
4678 bool use_mfp = false;
4679 u32 flags = 0;
4680 struct ieee80211_ht_cap *ht_capa = NULL;
4681 struct ieee80211_ht_cap *ht_capa_mask = NULL;
4682
4683 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4684 return -EINVAL;
4685
4686 if (!info->attrs[NL80211_ATTR_MAC] ||
4687 !info->attrs[NL80211_ATTR_SSID] ||
4688 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4689 return -EINVAL;
4690
4691 if (!rdev->ops->assoc)
4692 return -EOPNOTSUPP;
4693
4694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4696 return -EOPNOTSUPP;
4697
4698 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4699
4700 chan = ieee80211_get_channel(&rdev->wiphy,
4701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4702 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4703 return -EINVAL;
4704
4705 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4706 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4707
4708 if (info->attrs[NL80211_ATTR_IE]) {
4709 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4710 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4711 }
4712
4713 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4714 enum nl80211_mfp mfp =
4715 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4716 if (mfp == NL80211_MFP_REQUIRED)
4717 use_mfp = true;
4718 else if (mfp != NL80211_MFP_NO)
4719 return -EINVAL;
4720 }
4721
4722 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4723 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4724
4725 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4726 flags |= ASSOC_REQ_DISABLE_HT;
4727
4728 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4729 ht_capa_mask =
4730 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4731
4732 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4733 if (!ht_capa_mask)
4734 return -EINVAL;
4735 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4736 }
4737
4738 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4739 if (!err)
4740 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4741 ssid, ssid_len, ie, ie_len, use_mfp,
4742 &crypto, flags, ht_capa,
4743 ht_capa_mask);
4744
4745 return err;
4746}
4747
4748static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4749{
4750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4751 struct net_device *dev = info->user_ptr[1];
4752 const u8 *ie = NULL, *bssid;
4753 int ie_len = 0;
4754 u16 reason_code;
4755 bool local_state_change;
4756
4757 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4758 return -EINVAL;
4759
4760 if (!info->attrs[NL80211_ATTR_MAC])
4761 return -EINVAL;
4762
4763 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4764 return -EINVAL;
4765
4766 if (!rdev->ops->deauth)
4767 return -EOPNOTSUPP;
4768
4769 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4770 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4771 return -EOPNOTSUPP;
4772
4773 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4774
4775 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4776 if (reason_code == 0) {
4777 /* Reason Code 0 is reserved */
4778 return -EINVAL;
4779 }
4780
4781 if (info->attrs[NL80211_ATTR_IE]) {
4782 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4783 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4784 }
4785
4786 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4787
4788 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4789 local_state_change);
4790}
4791
4792static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4793{
4794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4795 struct net_device *dev = info->user_ptr[1];
4796 const u8 *ie = NULL, *bssid;
4797 int ie_len = 0;
4798 u16 reason_code;
4799 bool local_state_change;
4800
4801 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4802 return -EINVAL;
4803
4804 if (!info->attrs[NL80211_ATTR_MAC])
4805 return -EINVAL;
4806
4807 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4808 return -EINVAL;
4809
4810 if (!rdev->ops->disassoc)
4811 return -EOPNOTSUPP;
4812
4813 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4815 return -EOPNOTSUPP;
4816
4817 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4818
4819 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4820 if (reason_code == 0) {
4821 /* Reason Code 0 is reserved */
4822 return -EINVAL;
4823 }
4824
4825 if (info->attrs[NL80211_ATTR_IE]) {
4826 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4827 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4828 }
4829
4830 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4831
4832 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4833 local_state_change);
4834}
4835
4836static bool
4837nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4838 int mcast_rate[IEEE80211_NUM_BANDS],
4839 int rateval)
4840{
4841 struct wiphy *wiphy = &rdev->wiphy;
4842 bool found = false;
4843 int band, i;
4844
4845 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4846 struct ieee80211_supported_band *sband;
4847
4848 sband = wiphy->bands[band];
4849 if (!sband)
4850 continue;
4851
4852 for (i = 0; i < sband->n_bitrates; i++) {
4853 if (sband->bitrates[i].bitrate == rateval) {
4854 mcast_rate[band] = i + 1;
4855 found = true;
4856 break;
4857 }
4858 }
4859 }
4860
4861 return found;
4862}
4863
4864static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4865{
4866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4867 struct net_device *dev = info->user_ptr[1];
4868 struct cfg80211_ibss_params ibss;
4869 struct wiphy *wiphy;
4870 struct cfg80211_cached_keys *connkeys = NULL;
4871 int err;
4872
4873 memset(&ibss, 0, sizeof(ibss));
4874
4875 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4876 return -EINVAL;
4877
4878 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4879 !info->attrs[NL80211_ATTR_SSID] ||
4880 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4881 return -EINVAL;
4882
4883 ibss.beacon_interval = 100;
4884
4885 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4886 ibss.beacon_interval =
4887 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4888 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4889 return -EINVAL;
4890 }
4891
4892 if (!rdev->ops->join_ibss)
4893 return -EOPNOTSUPP;
4894
4895 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4896 return -EOPNOTSUPP;
4897
4898 wiphy = &rdev->wiphy;
4899
4900 if (info->attrs[NL80211_ATTR_MAC]) {
4901 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4902
4903 if (!is_valid_ether_addr(ibss.bssid))
4904 return -EINVAL;
4905 }
4906 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4907 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4908
4909 if (info->attrs[NL80211_ATTR_IE]) {
4910 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4911 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4912 }
4913
4914 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4915 enum nl80211_channel_type channel_type;
4916
4917 channel_type = nla_get_u32(
4918 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4919 if (channel_type != NL80211_CHAN_NO_HT &&
4920 channel_type != NL80211_CHAN_HT20 &&
4921 channel_type != NL80211_CHAN_HT40MINUS &&
4922 channel_type != NL80211_CHAN_HT40PLUS)
4923 return -EINVAL;
4924
4925 if (channel_type != NL80211_CHAN_NO_HT &&
4926 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4927 return -EINVAL;
4928
4929 ibss.channel_type = channel_type;
4930 } else {
4931 ibss.channel_type = NL80211_CHAN_NO_HT;
4932 }
4933
4934 ibss.channel = rdev_freq_to_chan(rdev,
4935 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4936 ibss.channel_type);
4937 if (!ibss.channel ||
4938 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4939 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4940 return -EINVAL;
4941
4942 /* Both channels should be able to initiate communication */
4943 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4944 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4945 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4946 ibss.channel_type))
4947 return -EINVAL;
4948
4949 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
4950 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4951
4952 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4953 u8 *rates =
4954 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4955 int n_rates =
4956 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4957 struct ieee80211_supported_band *sband =
4958 wiphy->bands[ibss.channel->band];
4959
4960 err = ieee80211_get_ratemask(sband, rates, n_rates,
4961 &ibss.basic_rates);
4962 if (err)
4963 return err;
4964 }
4965
4966 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4967 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4968 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4969 return -EINVAL;
4970
4971 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4972 connkeys = nl80211_parse_connkeys(rdev,
4973 info->attrs[NL80211_ATTR_KEYS]);
4974 if (IS_ERR(connkeys))
4975 return PTR_ERR(connkeys);
4976 }
4977
4978 ibss.control_port =
4979 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4980
4981 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
4982 if (err)
4983 kfree(connkeys);
4984#if defined(CONFIG_AIC8800)
4985 else if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
4986 printk("lemon %s sock_ow\n", __func__);
4987 //dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
4988 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_pid;
4989 }
4990#endif
4991 return err;
4992}
4993
4994static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4995{
4996 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4997 struct net_device *dev = info->user_ptr[1];
4998
4999 if (!rdev->ops->leave_ibss)
5000 return -EOPNOTSUPP;
5001
5002 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5003 return -EOPNOTSUPP;
5004
5005 return cfg80211_leave_ibss(rdev, dev, false);
5006}
5007
5008#ifdef CONFIG_NL80211_TESTMODE
5009static struct genl_multicast_group nl80211_testmode_mcgrp = {
5010 .name = "testmode",
5011};
5012
5013static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5014{
5015 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5016 int err;
5017
5018 if (!info->attrs[NL80211_ATTR_TESTDATA])
5019 return -EINVAL;
5020
5021 err = -EOPNOTSUPP;
5022 if (rdev->ops->testmode_cmd) {
5023 rdev->testmode_info = info;
5024 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5025 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5026 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5027 rdev->testmode_info = NULL;
5028 }
5029
5030 return err;
5031}
5032
5033static int nl80211_testmode_dump(struct sk_buff *skb,
5034 struct netlink_callback *cb)
5035{
5036 struct cfg80211_registered_device *rdev;
5037 int err;
5038 long phy_idx;
5039 void *data = NULL;
5040 int data_len = 0;
5041
5042 if (cb->args[0]) {
5043 /*
5044 * 0 is a valid index, but not valid for args[0],
5045 * so we need to offset by 1.
5046 */
5047 phy_idx = cb->args[0] - 1;
5048 } else {
5049 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5050 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5051 nl80211_policy);
5052 if (err)
5053 return err;
5054 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5055 phy_idx = nla_get_u32(
5056 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5057 } else {
5058 struct net_device *netdev;
5059
5060 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5061 nl80211_fam.attrbuf,
5062 &rdev, &netdev);
5063 if (err)
5064 return err;
5065 dev_put(netdev);
5066 phy_idx = rdev->wiphy_idx;
5067 cfg80211_unlock_rdev(rdev);
5068 }
5069 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5070 cb->args[1] =
5071 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5072 }
5073
5074 if (cb->args[1]) {
5075 data = nla_data((void *)cb->args[1]);
5076 data_len = nla_len((void *)cb->args[1]);
5077 }
5078
5079 mutex_lock(&cfg80211_mutex);
5080 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5081 if (!rdev) {
5082 mutex_unlock(&cfg80211_mutex);
5083 return -ENOENT;
5084 }
5085 cfg80211_lock_rdev(rdev);
5086 mutex_unlock(&cfg80211_mutex);
5087
5088 if (!rdev->ops->testmode_dump) {
5089 err = -EOPNOTSUPP;
5090 goto out_err;
5091 }
5092
5093 while (1) {
5094 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5095 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5096 NL80211_CMD_TESTMODE);
5097 struct nlattr *tmdata;
5098
5099 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
5100 genlmsg_cancel(skb, hdr);
5101 break;
5102 }
5103
5104 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5105 if (!tmdata) {
5106 genlmsg_cancel(skb, hdr);
5107 break;
5108 }
5109 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5110 data, data_len);
5111 nla_nest_end(skb, tmdata);
5112
5113 if (err == -ENOBUFS || err == -ENOENT) {
5114 genlmsg_cancel(skb, hdr);
5115 break;
5116 } else if (err) {
5117 genlmsg_cancel(skb, hdr);
5118 goto out_err;
5119 }
5120
5121 genlmsg_end(skb, hdr);
5122 }
5123
5124 err = skb->len;
5125 /* see above */
5126 cb->args[0] = phy_idx + 1;
5127 out_err:
5128 cfg80211_unlock_rdev(rdev);
5129 return err;
5130}
5131
5132static struct sk_buff *
5133__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5134 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5135{
5136 struct sk_buff *skb;
5137 void *hdr;
5138 struct nlattr *data;
5139
5140 skb = nlmsg_new(approxlen + 100, gfp);
5141 if (!skb)
5142 return NULL;
5143
5144 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5145 if (!hdr) {
5146 kfree_skb(skb);
5147 return NULL;
5148 }
5149
5150 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5151 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5152
5153 ((void **)skb->cb)[0] = rdev;
5154 ((void **)skb->cb)[1] = hdr;
5155 ((void **)skb->cb)[2] = data;
5156
5157 return skb;
5158
5159 nla_put_failure:
5160 kfree_skb(skb);
5161 return NULL;
5162}
5163
5164struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5165 int approxlen)
5166{
5167 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5168
5169 if (WARN_ON(!rdev->testmode_info))
5170 return NULL;
5171
5172 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5173 rdev->testmode_info->snd_pid,
5174 rdev->testmode_info->snd_seq,
5175 GFP_KERNEL);
5176}
5177EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5178
5179int cfg80211_testmode_reply(struct sk_buff *skb)
5180{
5181 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5182 void *hdr = ((void **)skb->cb)[1];
5183 struct nlattr *data = ((void **)skb->cb)[2];
5184
5185 /* clear CB data for netlink core to own from now on */
5186 memset(skb->cb, 0, sizeof(skb->cb));
5187
5188 if (WARN_ON(!rdev->testmode_info)) {
5189 kfree_skb(skb);
5190 return -EINVAL;
5191 }
5192
5193 nla_nest_end(skb, data);
5194 genlmsg_end(skb, hdr);
5195 return genlmsg_reply(skb, rdev->testmode_info);
5196}
5197EXPORT_SYMBOL(cfg80211_testmode_reply);
5198
5199struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5200 int approxlen, gfp_t gfp)
5201{
5202 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5203
5204 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5205}
5206EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5207
5208void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5209{
5210 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5211 void *hdr = ((void **)skb->cb)[1];
5212 struct nlattr *data = ((void **)skb->cb)[2];
5213
5214 /* clear CB data for netlink core to own from now on */
5215 memset(skb->cb, 0, sizeof(skb->cb));
5216
5217 nla_nest_end(skb, data);
5218 genlmsg_end(skb, hdr);
5219 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0,
5220 nl80211_testmode_mcgrp.id, gfp);
5221}
5222EXPORT_SYMBOL(cfg80211_testmode_event);
5223#endif
5224
5225static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5226{
5227 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5228 struct net_device *dev = info->user_ptr[1];
5229 struct cfg80211_connect_params connect;
5230 struct wiphy *wiphy;
5231 struct cfg80211_cached_keys *connkeys = NULL;
5232 int err;
5233
5234 memset(&connect, 0, sizeof(connect));
5235
5236 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5237 return -EINVAL;
5238
5239 if (!info->attrs[NL80211_ATTR_SSID] ||
5240 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5241 return -EINVAL;
5242
5243 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5244 connect.auth_type =
5245 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5246 if (!nl80211_valid_auth_type(connect.auth_type))
5247 return -EINVAL;
5248 } else
5249 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5250
5251 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5252
5253 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
5254 NL80211_MAX_NR_CIPHER_SUITES);
5255 if (err)
5256 return err;
5257
5258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5259 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5260 return -EOPNOTSUPP;
5261
5262 wiphy = &rdev->wiphy;
5263
5264 connect.bg_scan_period = -1;
5265 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5266 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5267 connect.bg_scan_period =
5268 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5269 }
5270
5271 if (info->attrs[NL80211_ATTR_MAC])
5272 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5273 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5274 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5275
5276 if (info->attrs[NL80211_ATTR_IE]) {
5277 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5278 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5279 }
5280#if defined(CONFIG_AIC8800)
5281 if (info->attrs[NL80211_ATTR_USE_MFP]) {
5282 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
5283 if (connect.mfp != NL80211_MFP_REQUIRED &&
5284 connect.mfp != NL80211_MFP_NO) {
5285 return -EINVAL;
5286 }
5287 } else {
5288 connect.mfp = NL80211_MFP_NO;
5289 }
5290#endif
5291 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5292 connect.channel =
5293 ieee80211_get_channel(wiphy,
5294 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5295 if (!connect.channel ||
5296 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5297 return -EINVAL;
5298 }
5299
5300 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5301 connkeys = nl80211_parse_connkeys(rdev,
5302 info->attrs[NL80211_ATTR_KEYS]);
5303 if (IS_ERR(connkeys))
5304 return PTR_ERR(connkeys);
5305 }
5306
5307 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5308 connect.flags |= ASSOC_REQ_DISABLE_HT;
5309
5310 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5311 memcpy(&connect.ht_capa_mask,
5312 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5313 sizeof(connect.ht_capa_mask));
5314
5315 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5316 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5317 return -EINVAL;
5318 memcpy(&connect.ht_capa,
5319 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5320 sizeof(connect.ht_capa));
5321 }
5322#if defined(CONFIG_AIC8800)
5323 if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
5324 if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5325 kzfree(connkeys);
5326 // GENL_SET_ERR_MSG(info,
5327 // "external auth requires connection ownership");
5328 return -EINVAL;
5329 }
5330 connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
5331 }
5332
5333 //wdev_lock(dev->ieee80211_ptr);
5334#endif
5335 err = cfg80211_connect(rdev, dev, &connect, connkeys);
5336 if (err)
5337 kfree(connkeys);
5338#if defined(CONFIG_AIC8800)
5339 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5340 //dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
5341 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_pid;
5342 if (connect.bssid) {
5343 memcpy(dev->ieee80211_ptr->disconnect_bssid,
5344 connect.bssid, ETH_ALEN);
5345 }
5346 else {
5347 memset(dev->ieee80211_ptr->disconnect_bssid,
5348 0, ETH_ALEN);
5349 }
5350 }
5351
5352 //wdev_unlock(dev->ieee80211_ptr);
5353#endif
5354 return err;
5355}
5356
5357static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5358{
5359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5360 struct net_device *dev = info->user_ptr[1];
5361 u16 reason;
5362
5363 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5364 reason = WLAN_REASON_DEAUTH_LEAVING;
5365 else
5366 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5367
5368 if (reason == 0)
5369 return -EINVAL;
5370
5371 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5372 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5373 return -EOPNOTSUPP;
5374
5375 return cfg80211_disconnect(rdev, dev, reason, true);
5376}
5377
5378static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5379{
5380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5381 struct net *net;
5382 int err;
5383 u32 pid;
5384
5385 if (!info->attrs[NL80211_ATTR_PID])
5386 return -EINVAL;
5387
5388 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5389
5390 net = get_net_ns_by_pid(pid);
5391 if (IS_ERR(net))
5392 return PTR_ERR(net);
5393
5394 err = 0;
5395
5396 /* check if anything to do */
5397 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5398 err = cfg80211_switch_netns(rdev, net);
5399
5400 put_net(net);
5401 return err;
5402}
5403
5404static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5405{
5406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5407 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5408 struct cfg80211_pmksa *pmksa) = NULL;
5409 struct net_device *dev = info->user_ptr[1];
5410 struct cfg80211_pmksa pmksa;
5411
5412 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5413
5414 if (!info->attrs[NL80211_ATTR_MAC])
5415 return -EINVAL;
5416
5417 if (!info->attrs[NL80211_ATTR_PMKID])
5418 return -EINVAL;
5419
5420 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5421 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5422
5423 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5424 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5425 return -EOPNOTSUPP;
5426
5427 switch (info->genlhdr->cmd) {
5428 case NL80211_CMD_SET_PMKSA:
5429 rdev_ops = rdev->ops->set_pmksa;
5430 break;
5431 case NL80211_CMD_DEL_PMKSA:
5432 rdev_ops = rdev->ops->del_pmksa;
5433 break;
5434 default:
5435 WARN_ON(1);
5436 break;
5437 }
5438
5439 if (!rdev_ops)
5440 return -EOPNOTSUPP;
5441
5442 return rdev_ops(&rdev->wiphy, dev, &pmksa);
5443}
5444
5445static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5446{
5447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5448 struct net_device *dev = info->user_ptr[1];
5449
5450 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5451 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5452 return -EOPNOTSUPP;
5453
5454 if (!rdev->ops->flush_pmksa)
5455 return -EOPNOTSUPP;
5456
5457 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
5458}
5459
5460static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5461{
5462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5463 struct net_device *dev = info->user_ptr[1];
5464 u8 action_code, dialog_token;
5465 u16 status_code;
5466 u8 *peer;
5467
5468 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5469 !rdev->ops->tdls_mgmt)
5470 return -EOPNOTSUPP;
5471
5472 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5473 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5474 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5475 !info->attrs[NL80211_ATTR_IE] ||
5476 !info->attrs[NL80211_ATTR_MAC])
5477 return -EINVAL;
5478
5479 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5480 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5481 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5482 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5483
5484 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5485 dialog_token, status_code,
5486 nla_data(info->attrs[NL80211_ATTR_IE]),
5487 nla_len(info->attrs[NL80211_ATTR_IE]));
5488}
5489
5490static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5491{
5492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5493 struct net_device *dev = info->user_ptr[1];
5494 enum nl80211_tdls_operation operation;
5495 u8 *peer;
5496
5497 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5498 !rdev->ops->tdls_oper)
5499 return -EOPNOTSUPP;
5500
5501 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5502 !info->attrs[NL80211_ATTR_MAC])
5503 return -EINVAL;
5504
5505 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5506 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5507
5508 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5509}
5510
5511static int nl80211_remain_on_channel(struct sk_buff *skb,
5512 struct genl_info *info)
5513{
5514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5515 struct net_device *dev = info->user_ptr[1];
5516 struct ieee80211_channel *chan;
5517 struct sk_buff *msg;
5518 void *hdr;
5519 u64 cookie;
5520 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5521 u32 freq, duration;
5522 int err;
5523
5524 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5525 !info->attrs[NL80211_ATTR_DURATION])
5526 return -EINVAL;
5527
5528 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5529
5530 /*
5531 * We should be on that channel for at least one jiffie,
5532 * and more than 5 seconds seems excessive.
5533 */
5534 if (!duration || !msecs_to_jiffies(duration) ||
5535 duration > rdev->wiphy.max_remain_on_channel_duration)
5536 return -EINVAL;
5537
5538 if (!rdev->ops->remain_on_channel ||
5539 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
5540 return -EOPNOTSUPP;
5541
5542 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5543 channel_type = nla_get_u32(
5544 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5545 if (channel_type != NL80211_CHAN_NO_HT &&
5546 channel_type != NL80211_CHAN_HT20 &&
5547 channel_type != NL80211_CHAN_HT40PLUS &&
5548 channel_type != NL80211_CHAN_HT40MINUS)
5549 return -EINVAL;
5550 }
5551
5552 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5553 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5554 if (chan == NULL)
5555 return -EINVAL;
5556
5557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5558 if (!msg)
5559 return -ENOMEM;
5560
5561 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5562 NL80211_CMD_REMAIN_ON_CHANNEL);
5563
5564 if (IS_ERR(hdr)) {
5565 err = PTR_ERR(hdr);
5566 goto free_msg;
5567 }
5568
5569 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5570 channel_type, duration, &cookie);
5571
5572 if (err)
5573 goto free_msg;
5574
5575 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5576
5577 genlmsg_end(msg, hdr);
5578
5579 return genlmsg_reply(msg, info);
5580
5581 nla_put_failure:
5582 err = -ENOBUFS;
5583 free_msg:
5584 nlmsg_free(msg);
5585 return err;
5586}
5587
5588static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5589 struct genl_info *info)
5590{
5591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5592 struct net_device *dev = info->user_ptr[1];
5593 u64 cookie;
5594
5595 if (!info->attrs[NL80211_ATTR_COOKIE])
5596 return -EINVAL;
5597
5598 if (!rdev->ops->cancel_remain_on_channel)
5599 return -EOPNOTSUPP;
5600
5601 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5602
5603 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
5604}
5605
5606static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5607 u8 *rates, u8 rates_len)
5608{
5609 u8 i;
5610 u32 mask = 0;
5611
5612 for (i = 0; i < rates_len; i++) {
5613 int rate = (rates[i] & 0x7f) * 5;
5614 int ridx;
5615 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5616 struct ieee80211_rate *srate =
5617 &sband->bitrates[ridx];
5618 if (rate == srate->bitrate) {
5619 mask |= 1 << ridx;
5620 break;
5621 }
5622 }
5623 if (ridx == sband->n_bitrates)
5624 return 0; /* rate not found */
5625 }
5626
5627 return mask;
5628}
5629
5630static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5631 u8 *rates, u8 rates_len,
5632 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5633{
5634 u8 i;
5635
5636 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5637
5638 for (i = 0; i < rates_len; i++) {
5639 int ridx, rbit;
5640
5641 ridx = rates[i] / 8;
5642 rbit = BIT(rates[i] % 8);
5643
5644 /* check validity */
5645 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
5646 return false;
5647
5648 /* check availability */
5649 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5650 mcs[ridx] |= rbit;
5651 else
5652 return false;
5653 }
5654
5655 return true;
5656}
5657
5658static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
5659 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5660 .len = NL80211_MAX_SUPP_RATES },
5661 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5662 .len = NL80211_MAX_SUPP_HT_RATES },
5663};
5664
5665static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5666 struct genl_info *info)
5667{
5668 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5669 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5670 struct cfg80211_bitrate_mask mask;
5671 int rem, i;
5672 struct net_device *dev = info->user_ptr[1];
5673 struct nlattr *tx_rates;
5674 struct ieee80211_supported_band *sband;
5675
5676 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5677 return -EINVAL;
5678
5679 if (!rdev->ops->set_bitrate_mask)
5680 return -EOPNOTSUPP;
5681
5682 memset(&mask, 0, sizeof(mask));
5683 /* Default to all rates enabled */
5684 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5685 sband = rdev->wiphy.bands[i];
5686 mask.control[i].legacy =
5687 sband ? (1 << sband->n_bitrates) - 1 : 0;
5688 if (sband)
5689 memcpy(mask.control[i].mcs,
5690 sband->ht_cap.mcs.rx_mask,
5691 sizeof(mask.control[i].mcs));
5692 else
5693 memset(mask.control[i].mcs, 0,
5694 sizeof(mask.control[i].mcs));
5695 }
5696
5697 /*
5698 * The nested attribute uses enum nl80211_band as the index. This maps
5699 * directly to the enum ieee80211_band values used in cfg80211.
5700 */
5701 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5702 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5703 {
5704 enum ieee80211_band band = nla_type(tx_rates);
5705 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5706 return -EINVAL;
5707 sband = rdev->wiphy.bands[band];
5708 if (sband == NULL)
5709 return -EINVAL;
5710 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5711 nla_len(tx_rates), nl80211_txattr_policy);
5712 if (tb[NL80211_TXRATE_LEGACY]) {
5713 mask.control[band].legacy = rateset_to_mask(
5714 sband,
5715 nla_data(tb[NL80211_TXRATE_LEGACY]),
5716 nla_len(tb[NL80211_TXRATE_LEGACY]));
5717 }
5718 if (tb[NL80211_TXRATE_MCS]) {
5719 if (!ht_rateset_to_mask(
5720 sband,
5721 nla_data(tb[NL80211_TXRATE_MCS]),
5722 nla_len(tb[NL80211_TXRATE_MCS]),
5723 mask.control[band].mcs))
5724 return -EINVAL;
5725 }
5726
5727 if (mask.control[band].legacy == 0) {
5728 /* don't allow empty legacy rates if HT
5729 * is not even supported. */
5730 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5731 return -EINVAL;
5732
5733 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5734 if (mask.control[band].mcs[i])
5735 break;
5736
5737 /* legacy and mcs rates may not be both empty */
5738 if (i == IEEE80211_HT_MCS_MASK_LEN)
5739 return -EINVAL;
5740 }
5741 }
5742
5743 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
5744}
5745
5746static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
5747{
5748 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5749 struct net_device *dev = info->user_ptr[1];
5750 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
5751
5752 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5753 return -EINVAL;
5754
5755 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5756 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
5757
5758 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5759 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5760 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5761 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5762 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5764 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5765 return -EOPNOTSUPP;
5766
5767 /* not much point in registering if we can't reply */
5768 if (!rdev->ops->mgmt_tx)
5769 return -EOPNOTSUPP;
5770
5771 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
5772 frame_type,
5773 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5774 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
5775}
5776
5777static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
5778{
5779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5780 struct net_device *dev = info->user_ptr[1];
5781 struct ieee80211_channel *chan;
5782 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5783 bool channel_type_valid = false;
5784 u32 freq;
5785 int err;
5786 void *hdr = NULL;
5787 u64 cookie;
5788 struct sk_buff *msg = NULL;
5789 unsigned int wait = 0;
5790 bool offchan, no_cck, dont_wait_for_ack;
5791
5792 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
5793
5794 if (!info->attrs[NL80211_ATTR_FRAME] ||
5795 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5796 return -EINVAL;
5797
5798 if (!rdev->ops->mgmt_tx)
5799 return -EOPNOTSUPP;
5800
5801 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5802 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5803 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5804 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5805 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5806 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5807 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5808 return -EOPNOTSUPP;
5809
5810 if (info->attrs[NL80211_ATTR_DURATION]) {
5811 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5812 return -EINVAL;
5813 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5814 }
5815
5816 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5817 channel_type = nla_get_u32(
5818 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5819 if (channel_type != NL80211_CHAN_NO_HT &&
5820 channel_type != NL80211_CHAN_HT20 &&
5821 channel_type != NL80211_CHAN_HT40PLUS &&
5822 channel_type != NL80211_CHAN_HT40MINUS)
5823 return -EINVAL;
5824 channel_type_valid = true;
5825 }
5826
5827 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5828
5829 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5830 return -EINVAL;
5831
5832 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5833
5834 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5835 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5836 if (chan == NULL)
5837 return -EINVAL;
5838
5839 if (!dont_wait_for_ack) {
5840 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5841 if (!msg)
5842 return -ENOMEM;
5843
5844 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5845 NL80211_CMD_FRAME);
5846
5847 if (IS_ERR(hdr)) {
5848 err = PTR_ERR(hdr);
5849 goto free_msg;
5850 }
5851 }
5852
5853 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5854 channel_type_valid, wait,
5855 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5856 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5857 no_cck, dont_wait_for_ack, &cookie);
5858 if (err)
5859 goto free_msg;
5860
5861 if (msg) {
5862 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5863
5864 genlmsg_end(msg, hdr);
5865 return genlmsg_reply(msg, info);
5866 }
5867
5868 return 0;
5869
5870 nla_put_failure:
5871 err = -ENOBUFS;
5872 free_msg:
5873 nlmsg_free(msg);
5874 return err;
5875}
5876
5877static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5878{
5879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5880 struct net_device *dev = info->user_ptr[1];
5881 u64 cookie;
5882
5883 if (!info->attrs[NL80211_ATTR_COOKIE])
5884 return -EINVAL;
5885
5886 if (!rdev->ops->mgmt_tx_cancel_wait)
5887 return -EOPNOTSUPP;
5888
5889 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5890 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5891 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5892 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5893 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5894 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5895 return -EOPNOTSUPP;
5896
5897 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5898
5899 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5900}
5901
5902static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5903{
5904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5905 struct wireless_dev *wdev;
5906 struct net_device *dev = info->user_ptr[1];
5907 u8 ps_state;
5908 bool state;
5909 int err;
5910
5911 if (!info->attrs[NL80211_ATTR_PS_STATE])
5912 return -EINVAL;
5913
5914 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5915
5916 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5917 return -EINVAL;
5918
5919 wdev = dev->ieee80211_ptr;
5920
5921 if (!rdev->ops->set_power_mgmt)
5922 return -EOPNOTSUPP;
5923
5924 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5925
5926 if (state == wdev->ps)
5927 return 0;
5928
5929 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5930 wdev->ps_timeout);
5931 if (!err)
5932 wdev->ps = state;
5933 return err;
5934}
5935
5936static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5937{
5938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5939 enum nl80211_ps_state ps_state;
5940 struct wireless_dev *wdev;
5941 struct net_device *dev = info->user_ptr[1];
5942 struct sk_buff *msg;
5943 void *hdr;
5944 int err;
5945
5946 wdev = dev->ieee80211_ptr;
5947
5948 if (!rdev->ops->set_power_mgmt)
5949 return -EOPNOTSUPP;
5950
5951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5952 if (!msg)
5953 return -ENOMEM;
5954
5955 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5956 NL80211_CMD_GET_POWER_SAVE);
5957 if (!hdr) {
5958 err = -ENOBUFS;
5959 goto free_msg;
5960 }
5961
5962 if (wdev->ps)
5963 ps_state = NL80211_PS_ENABLED;
5964 else
5965 ps_state = NL80211_PS_DISABLED;
5966
5967 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5968
5969 genlmsg_end(msg, hdr);
5970 return genlmsg_reply(msg, info);
5971
5972 nla_put_failure:
5973 err = -ENOBUFS;
5974 free_msg:
5975 nlmsg_free(msg);
5976 return err;
5977}
5978
5979static struct nla_policy
5980nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5981 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5982 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5983 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5984};
5985
5986static int nl80211_set_cqm_rssi(struct genl_info *info,
5987 s32 threshold, u32 hysteresis)
5988{
5989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5990 struct wireless_dev *wdev;
5991 struct net_device *dev = info->user_ptr[1];
5992
5993 if (threshold > 0)
5994 return -EINVAL;
5995
5996 wdev = dev->ieee80211_ptr;
5997
5998 if (!rdev->ops->set_cqm_rssi_config)
5999 return -EOPNOTSUPP;
6000
6001 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6002 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6003 return -EOPNOTSUPP;
6004
6005 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6006 threshold, hysteresis);
6007}
6008
6009static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6010{
6011 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6012 struct nlattr *cqm;
6013 int err;
6014
6015 cqm = info->attrs[NL80211_ATTR_CQM];
6016 if (!cqm) {
6017 err = -EINVAL;
6018 goto out;
6019 }
6020
6021 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6022 nl80211_attr_cqm_policy);
6023 if (err)
6024 goto out;
6025
6026 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6027 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6028 s32 threshold;
6029 u32 hysteresis;
6030 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6031 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6032 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6033 } else
6034 err = -EINVAL;
6035
6036out:
6037 return err;
6038}
6039
6040static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6041{
6042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6043 struct net_device *dev = info->user_ptr[1];
6044 struct mesh_config cfg;
6045 struct mesh_setup setup;
6046 int err;
6047
6048 /* start with default */
6049 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
6050 memcpy(&setup, &default_mesh_setup, sizeof(setup));
6051
6052 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
6053 /* and parse parameters if given */
6054 err = nl80211_parse_mesh_config(info, &cfg, NULL);
6055 if (err)
6056 return err;
6057 }
6058
6059 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6060 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6061 return -EINVAL;
6062
6063 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6064 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6065
6066 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6067 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6068 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6069 return -EINVAL;
6070
6071 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6072 /* parse additional setup parameters if given */
6073 err = nl80211_parse_mesh_setup(info, &setup);
6074 if (err)
6075 return err;
6076 }
6077
6078 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
6079}
6080
6081static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6082{
6083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6084 struct net_device *dev = info->user_ptr[1];
6085
6086 return cfg80211_leave_mesh(rdev, dev);
6087}
6088
6089static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6090{
6091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6092 struct sk_buff *msg;
6093 void *hdr;
6094
6095 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6096 return -EOPNOTSUPP;
6097
6098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6099 if (!msg)
6100 return -ENOMEM;
6101
6102 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6103 NL80211_CMD_GET_WOWLAN);
6104 if (!hdr)
6105 goto nla_put_failure;
6106
6107 if (rdev->wowlan) {
6108 struct nlattr *nl_wowlan;
6109
6110 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6111 if (!nl_wowlan)
6112 goto nla_put_failure;
6113
6114 if (rdev->wowlan->any)
6115 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
6116 if (rdev->wowlan->disconnect)
6117 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
6118 if (rdev->wowlan->magic_pkt)
6119 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
6120 if (rdev->wowlan->gtk_rekey_failure)
6121 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
6122 if (rdev->wowlan->eap_identity_req)
6123 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
6124 if (rdev->wowlan->four_way_handshake)
6125 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
6126 if (rdev->wowlan->rfkill_release)
6127 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
6128 if (rdev->wowlan->n_patterns) {
6129 struct nlattr *nl_pats, *nl_pat;
6130 int i, pat_len;
6131
6132 nl_pats = nla_nest_start(msg,
6133 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6134 if (!nl_pats)
6135 goto nla_put_failure;
6136
6137 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6138 nl_pat = nla_nest_start(msg, i + 1);
6139 if (!nl_pat)
6140 goto nla_put_failure;
6141 pat_len = rdev->wowlan->patterns[i].pattern_len;
6142 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
6143 DIV_ROUND_UP(pat_len, 8),
6144 rdev->wowlan->patterns[i].mask);
6145 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6146 pat_len,
6147 rdev->wowlan->patterns[i].pattern);
6148 nla_nest_end(msg, nl_pat);
6149 }
6150 nla_nest_end(msg, nl_pats);
6151 }
6152
6153 nla_nest_end(msg, nl_wowlan);
6154 }
6155
6156 genlmsg_end(msg, hdr);
6157 return genlmsg_reply(msg, info);
6158
6159nla_put_failure:
6160 nlmsg_free(msg);
6161 return -ENOBUFS;
6162}
6163
6164static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6165{
6166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6167 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6168 struct cfg80211_wowlan no_triggers = {};
6169 struct cfg80211_wowlan new_triggers = {};
6170 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6171 int err, i;
6172
6173 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6174 return -EOPNOTSUPP;
6175
6176 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6177 goto no_triggers;
6178
6179 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6180 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6181 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6182 nl80211_wowlan_policy);
6183 if (err)
6184 return err;
6185
6186 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6187 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6188 return -EINVAL;
6189 new_triggers.any = true;
6190 }
6191
6192 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6193 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6194 return -EINVAL;
6195 new_triggers.disconnect = true;
6196 }
6197
6198 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6199 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6200 return -EINVAL;
6201 new_triggers.magic_pkt = true;
6202 }
6203
6204 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6205 return -EINVAL;
6206
6207 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6208 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6209 return -EINVAL;
6210 new_triggers.gtk_rekey_failure = true;
6211 }
6212
6213 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6214 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6215 return -EINVAL;
6216 new_triggers.eap_identity_req = true;
6217 }
6218
6219 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6220 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6221 return -EINVAL;
6222 new_triggers.four_way_handshake = true;
6223 }
6224
6225 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6226 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6227 return -EINVAL;
6228 new_triggers.rfkill_release = true;
6229 }
6230
6231 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6232 struct nlattr *pat;
6233 int n_patterns = 0;
6234 int rem, pat_len, mask_len;
6235 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6236
6237 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6238 rem)
6239 n_patterns++;
6240 if (n_patterns > wowlan->n_patterns)
6241 return -EINVAL;
6242
6243 new_triggers.patterns = kcalloc(n_patterns,
6244 sizeof(new_triggers.patterns[0]),
6245 GFP_KERNEL);
6246 if (!new_triggers.patterns)
6247 return -ENOMEM;
6248
6249 new_triggers.n_patterns = n_patterns;
6250 i = 0;
6251
6252 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6253 rem) {
6254 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6255 nla_data(pat), nla_len(pat), NULL);
6256 err = -EINVAL;
6257 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6258 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6259 goto error;
6260 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6261 mask_len = DIV_ROUND_UP(pat_len, 8);
6262 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6263 mask_len)
6264 goto error;
6265 if (pat_len > wowlan->pattern_max_len ||
6266 pat_len < wowlan->pattern_min_len)
6267 goto error;
6268
6269 new_triggers.patterns[i].mask =
6270 kmalloc(mask_len + pat_len, GFP_KERNEL);
6271 if (!new_triggers.patterns[i].mask) {
6272 err = -ENOMEM;
6273 goto error;
6274 }
6275 new_triggers.patterns[i].pattern =
6276 new_triggers.patterns[i].mask + mask_len;
6277 memcpy(new_triggers.patterns[i].mask,
6278 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6279 mask_len);
6280 new_triggers.patterns[i].pattern_len = pat_len;
6281 memcpy(new_triggers.patterns[i].pattern,
6282 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6283 pat_len);
6284 i++;
6285 }
6286 }
6287
6288 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6289 struct cfg80211_wowlan *ntrig;
6290 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6291 GFP_KERNEL);
6292 if (!ntrig) {
6293 err = -ENOMEM;
6294 goto error;
6295 }
6296 cfg80211_rdev_free_wowlan(rdev);
6297 rdev->wowlan = ntrig;
6298 } else {
6299 no_triggers:
6300 cfg80211_rdev_free_wowlan(rdev);
6301 rdev->wowlan = NULL;
6302 }
6303
6304 return 0;
6305 error:
6306 for (i = 0; i < new_triggers.n_patterns; i++)
6307 kfree(new_triggers.patterns[i].mask);
6308 kfree(new_triggers.patterns);
6309 return err;
6310}
6311
6312static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6313{
6314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6315 struct net_device *dev = info->user_ptr[1];
6316 struct wireless_dev *wdev = dev->ieee80211_ptr;
6317 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6318 struct cfg80211_gtk_rekey_data rekey_data;
6319 int err;
6320
6321 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6322 return -EINVAL;
6323
6324 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6325 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6326 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6327 nl80211_rekey_policy);
6328 if (err)
6329 return err;
6330
lh758261d2023-07-13 05:52:04 -07006331 if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
6332 !tb[NL80211_REKEY_DATA_KCK]) //CVE-2017-12153(BDSA-2017-1184)
6333 return -EINVAL;
lh9ed821d2023-04-07 01:36:19 -07006334 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6335 return -ERANGE;
6336 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6337 return -ERANGE;
6338 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6339 return -ERANGE;
6340
6341 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6342 NL80211_KEK_LEN);
6343 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6344 NL80211_KCK_LEN);
6345 memcpy(rekey_data.replay_ctr,
6346 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6347 NL80211_REPLAY_CTR_LEN);
6348
6349 wdev_lock(wdev);
6350 if (!wdev->current_bss) {
6351 err = -ENOTCONN;
6352 goto out;
6353 }
6354
6355 if (!rdev->ops->set_rekey_data) {
6356 err = -EOPNOTSUPP;
6357 goto out;
6358 }
6359
6360 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6361 out:
6362 wdev_unlock(wdev);
6363 return err;
6364}
6365
6366static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6367 struct genl_info *info)
6368{
6369 struct net_device *dev = info->user_ptr[1];
6370 struct wireless_dev *wdev = dev->ieee80211_ptr;
6371
6372 if (wdev->iftype != NL80211_IFTYPE_AP &&
6373 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6374 return -EINVAL;
6375
6376 if (wdev->ap_unexpected_nlpid)
6377 return -EBUSY;
6378
6379 wdev->ap_unexpected_nlpid = info->snd_pid;
6380 return 0;
6381}
6382
6383static int nl80211_probe_client(struct sk_buff *skb,
6384 struct genl_info *info)
6385{
6386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6387 struct net_device *dev = info->user_ptr[1];
6388 struct wireless_dev *wdev = dev->ieee80211_ptr;
6389 struct sk_buff *msg;
6390 void *hdr;
6391 const u8 *addr;
6392 u64 cookie;
6393 int err;
6394
6395 if (wdev->iftype != NL80211_IFTYPE_AP &&
6396 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6397 return -EOPNOTSUPP;
6398
6399 if (!info->attrs[NL80211_ATTR_MAC])
6400 return -EINVAL;
6401
6402 if (!rdev->ops->probe_client)
6403 return -EOPNOTSUPP;
6404
6405 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6406 if (!msg)
6407 return -ENOMEM;
6408
6409 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6410 NL80211_CMD_PROBE_CLIENT);
6411
6412 if (IS_ERR(hdr)) {
6413 err = PTR_ERR(hdr);
6414 goto free_msg;
6415 }
6416
6417 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6418
6419 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6420 if (err)
6421 goto free_msg;
6422
6423 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6424
6425 genlmsg_end(msg, hdr);
6426
6427 return genlmsg_reply(msg, info);
6428
6429 nla_put_failure:
6430 err = -ENOBUFS;
6431 free_msg:
6432 nlmsg_free(msg);
6433 return err;
6434}
6435
6436static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6437{
6438 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6439
6440 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6441 return -EOPNOTSUPP;
6442
6443 if (rdev->ap_beacons_nlpid)
6444 return -EBUSY;
6445
6446 rdev->ap_beacons_nlpid = info->snd_pid;
6447
6448 return 0;
6449}
6450#if defined(CONFIG_AIC8800)
6451static inline int
6452rdev_external_auth(struct cfg80211_registered_device *rdev,
6453 struct net_device *dev,
6454 struct cfg80211_external_auth_params *params)
6455{
6456 int ret = -EOPNOTSUPP;
6457
6458 //trace_rdev_external_auth(&rdev->wiphy, dev, params);
6459 if (rdev->ops->external_auth)
6460 ret = rdev->ops->external_auth(&rdev->wiphy, dev, params);
6461 //trace_rdev_return_int(&rdev->wiphy, ret);
6462 return ret;
6463}
6464
6465static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
6466{
6467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6468 struct net_device *dev = info->user_ptr[1];
6469 struct cfg80211_external_auth_params params;
6470
6471 if (!rdev->ops->external_auth)
6472 return -EOPNOTSUPP;
6473
6474 if (!info->attrs[NL80211_ATTR_SSID] &&
6475 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6476 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6477 return -EINVAL;
6478
6479 if (!info->attrs[NL80211_ATTR_BSSID])
6480 return -EINVAL;
6481
6482 if (!info->attrs[NL80211_ATTR_STATUS_CODE])
6483 return -EINVAL;
6484
6485 memset(&params, 0, sizeof(params));
6486
6487 if (info->attrs[NL80211_ATTR_SSID]) {
6488 params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6489 if (params.ssid.ssid_len == 0)
6490 return -EINVAL;
6491 memcpy(params.ssid.ssid,
6492 nla_data(info->attrs[NL80211_ATTR_SSID]),
6493 params.ssid.ssid_len);
6494 }
6495
6496 memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
6497 ETH_ALEN);
6498
6499 params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6500
6501 if (info->attrs[NL80211_ATTR_PMKID])
6502 params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6503
6504 return rdev_external_auth(rdev, dev, &params);
6505}
6506#endif
6507
6508#define NL80211_FLAG_NEED_WIPHY 0x01
6509#define NL80211_FLAG_NEED_NETDEV 0x02
6510#define NL80211_FLAG_NEED_RTNL 0x04
6511#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6512#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6513 NL80211_FLAG_CHECK_NETDEV_UP)
6514
6515static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6516 struct genl_info *info)
6517{
6518 struct cfg80211_registered_device *rdev;
6519 struct net_device *dev;
6520 int err;
6521 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6522
6523 if (rtnl)
6524 rtnl_lock();
6525
6526 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6527 rdev = cfg80211_get_dev_from_info(info);
6528 if (IS_ERR(rdev)) {
6529 if (rtnl)
6530 rtnl_unlock();
6531 return PTR_ERR(rdev);
6532 }
6533 info->user_ptr[0] = rdev;
6534 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6535 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6536 &rdev, &dev);
6537 if (err) {
6538 if (rtnl)
6539 rtnl_unlock();
6540 return err;
6541 }
6542 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6543 !netif_running(dev)) {
6544 cfg80211_unlock_rdev(rdev);
6545 dev_put(dev);
6546 if (rtnl)
6547 rtnl_unlock();
6548 return -ENETDOWN;
6549 }
6550 info->user_ptr[0] = rdev;
6551 info->user_ptr[1] = dev;
6552 }
6553
6554 return 0;
6555}
6556
6557static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6558 struct genl_info *info)
6559{
6560 if (info->user_ptr[0])
6561 cfg80211_unlock_rdev(info->user_ptr[0]);
6562 if (info->user_ptr[1])
6563 dev_put(info->user_ptr[1]);
6564 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6565 rtnl_unlock();
6566}
6567
6568static struct genl_ops nl80211_ops[] = {
6569 {
6570 .cmd = NL80211_CMD_GET_WIPHY,
6571 .doit = nl80211_get_wiphy,
6572 .dumpit = nl80211_dump_wiphy,
6573 .policy = nl80211_policy,
6574 /* can be retrieved by unprivileged users */
6575 .internal_flags = NL80211_FLAG_NEED_WIPHY,
6576 },
6577 {
6578 .cmd = NL80211_CMD_SET_WIPHY,
6579 .doit = nl80211_set_wiphy,
6580 .policy = nl80211_policy,
6581 .flags = GENL_ADMIN_PERM,
6582 .internal_flags = NL80211_FLAG_NEED_RTNL,
6583 },
6584 {
6585 .cmd = NL80211_CMD_GET_INTERFACE,
6586 .doit = nl80211_get_interface,
6587 .dumpit = nl80211_dump_interface,
6588 .policy = nl80211_policy,
6589 /* can be retrieved by unprivileged users */
6590 .internal_flags = NL80211_FLAG_NEED_NETDEV,
6591 },
6592 {
6593 .cmd = NL80211_CMD_SET_INTERFACE,
6594 .doit = nl80211_set_interface,
6595 .policy = nl80211_policy,
6596 .flags = GENL_ADMIN_PERM,
6597 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6598 NL80211_FLAG_NEED_RTNL,
6599 },
6600 {
6601 .cmd = NL80211_CMD_NEW_INTERFACE,
6602 .doit = nl80211_new_interface,
6603 .policy = nl80211_policy,
6604 .flags = GENL_ADMIN_PERM,
6605 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6606 NL80211_FLAG_NEED_RTNL,
6607 },
6608 {
6609 .cmd = NL80211_CMD_DEL_INTERFACE,
6610 .doit = nl80211_del_interface,
6611 .policy = nl80211_policy,
6612 .flags = GENL_ADMIN_PERM,
6613 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6614 NL80211_FLAG_NEED_RTNL,
6615 },
6616 {
6617 .cmd = NL80211_CMD_GET_KEY,
6618 .doit = nl80211_get_key,
6619 .policy = nl80211_policy,
6620 .flags = GENL_ADMIN_PERM,
6621 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6622 NL80211_FLAG_NEED_RTNL,
6623 },
6624 {
6625 .cmd = NL80211_CMD_SET_KEY,
6626 .doit = nl80211_set_key,
6627 .policy = nl80211_policy,
6628 .flags = GENL_ADMIN_PERM,
6629 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6630 NL80211_FLAG_NEED_RTNL,
6631 },
6632 {
6633 .cmd = NL80211_CMD_NEW_KEY,
6634 .doit = nl80211_new_key,
6635 .policy = nl80211_policy,
6636 .flags = GENL_ADMIN_PERM,
6637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6638 NL80211_FLAG_NEED_RTNL,
6639 },
6640 {
6641 .cmd = NL80211_CMD_DEL_KEY,
6642 .doit = nl80211_del_key,
6643 .policy = nl80211_policy,
6644 .flags = GENL_ADMIN_PERM,
6645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6646 NL80211_FLAG_NEED_RTNL,
6647 },
6648 {
6649 .cmd = NL80211_CMD_SET_BEACON,
6650 .policy = nl80211_policy,
6651 .flags = GENL_ADMIN_PERM,
6652 .doit = nl80211_set_beacon,
6653 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6654 NL80211_FLAG_NEED_RTNL,
6655 },
6656 {
6657 .cmd = NL80211_CMD_START_AP,
6658 .policy = nl80211_policy,
6659 .flags = GENL_ADMIN_PERM,
6660 .doit = nl80211_start_ap,
6661 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6662 NL80211_FLAG_NEED_RTNL,
6663 },
6664 {
6665 .cmd = NL80211_CMD_STOP_AP,
6666 .policy = nl80211_policy,
6667 .flags = GENL_ADMIN_PERM,
6668 .doit = nl80211_stop_ap,
6669 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6670 NL80211_FLAG_NEED_RTNL,
6671 },
6672 {
6673 .cmd = NL80211_CMD_GET_STATION,
6674 .doit = nl80211_get_station,
6675 .dumpit = nl80211_dump_station,
6676 .policy = nl80211_policy,
6677 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6678 NL80211_FLAG_NEED_RTNL,
6679 },
6680 {
6681 .cmd = NL80211_CMD_SET_STATION,
6682 .doit = nl80211_set_station,
6683 .policy = nl80211_policy,
6684 .flags = GENL_ADMIN_PERM,
6685 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6686 NL80211_FLAG_NEED_RTNL,
6687 },
6688 {
6689 .cmd = NL80211_CMD_NEW_STATION,
6690 .doit = nl80211_new_station,
6691 .policy = nl80211_policy,
6692 .flags = GENL_ADMIN_PERM,
6693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6694 NL80211_FLAG_NEED_RTNL,
6695 },
6696 {
6697 .cmd = NL80211_CMD_DEL_STATION,
6698 .doit = nl80211_del_station,
6699 .policy = nl80211_policy,
6700 .flags = GENL_ADMIN_PERM,
6701 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6702 NL80211_FLAG_NEED_RTNL,
6703 },
6704 {
6705 .cmd = NL80211_CMD_GET_MPATH,
6706 .doit = nl80211_get_mpath,
6707 .dumpit = nl80211_dump_mpath,
6708 .policy = nl80211_policy,
6709 .flags = GENL_ADMIN_PERM,
6710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6711 NL80211_FLAG_NEED_RTNL,
6712 },
6713 {
6714 .cmd = NL80211_CMD_SET_MPATH,
6715 .doit = nl80211_set_mpath,
6716 .policy = nl80211_policy,
6717 .flags = GENL_ADMIN_PERM,
6718 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6719 NL80211_FLAG_NEED_RTNL,
6720 },
6721 {
6722 .cmd = NL80211_CMD_NEW_MPATH,
6723 .doit = nl80211_new_mpath,
6724 .policy = nl80211_policy,
6725 .flags = GENL_ADMIN_PERM,
6726 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6727 NL80211_FLAG_NEED_RTNL,
6728 },
6729 {
6730 .cmd = NL80211_CMD_DEL_MPATH,
6731 .doit = nl80211_del_mpath,
6732 .policy = nl80211_policy,
6733 .flags = GENL_ADMIN_PERM,
6734 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6735 NL80211_FLAG_NEED_RTNL,
6736 },
6737 {
6738 .cmd = NL80211_CMD_SET_BSS,
6739 .doit = nl80211_set_bss,
6740 .policy = nl80211_policy,
6741 .flags = GENL_ADMIN_PERM,
6742 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6743 NL80211_FLAG_NEED_RTNL,
6744 },
6745 {
6746 .cmd = NL80211_CMD_GET_REG,
6747 .doit = nl80211_get_reg,
6748 .policy = nl80211_policy,
6749 /* can be retrieved by unprivileged users */
6750 },
6751 {
6752 .cmd = NL80211_CMD_SET_REG,
6753 .doit = nl80211_set_reg,
6754 .policy = nl80211_policy,
6755 .flags = GENL_ADMIN_PERM,
6756 },
6757 {
6758 .cmd = NL80211_CMD_REQ_SET_REG,
6759 .doit = nl80211_req_set_reg,
6760 .policy = nl80211_policy,
6761 .flags = GENL_ADMIN_PERM,
6762 },
6763 {
6764 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6765 .doit = nl80211_get_mesh_config,
6766 .policy = nl80211_policy,
6767 /* can be retrieved by unprivileged users */
6768 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6769 NL80211_FLAG_NEED_RTNL,
6770 },
6771 {
6772 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6773 .doit = nl80211_update_mesh_config,
6774 .policy = nl80211_policy,
6775 .flags = GENL_ADMIN_PERM,
6776 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6777 NL80211_FLAG_NEED_RTNL,
6778 },
6779 {
6780 .cmd = NL80211_CMD_TRIGGER_SCAN,
6781 .doit = nl80211_trigger_scan,
6782 .policy = nl80211_policy,
6783 .flags = GENL_ADMIN_PERM,
6784 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6785 NL80211_FLAG_NEED_RTNL,
6786 },
6787 {
6788 .cmd = NL80211_CMD_GET_SCAN,
6789 .policy = nl80211_policy,
6790 .dumpit = nl80211_dump_scan,
6791 },
6792 {
6793 .cmd = NL80211_CMD_START_SCHED_SCAN,
6794 .doit = nl80211_start_sched_scan,
6795 .policy = nl80211_policy,
6796 .flags = GENL_ADMIN_PERM,
6797 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6798 NL80211_FLAG_NEED_RTNL,
6799 },
6800 {
6801 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6802 .doit = nl80211_stop_sched_scan,
6803 .policy = nl80211_policy,
6804 .flags = GENL_ADMIN_PERM,
6805 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6806 NL80211_FLAG_NEED_RTNL,
6807 },
6808 {
6809 .cmd = NL80211_CMD_AUTHENTICATE,
6810 .doit = nl80211_authenticate,
6811 .policy = nl80211_policy,
6812 .flags = GENL_ADMIN_PERM,
6813 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6814 NL80211_FLAG_NEED_RTNL,
6815 },
6816 {
6817 .cmd = NL80211_CMD_ASSOCIATE,
6818 .doit = nl80211_associate,
6819 .policy = nl80211_policy,
6820 .flags = GENL_ADMIN_PERM,
6821 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6822 NL80211_FLAG_NEED_RTNL,
6823 },
6824 {
6825 .cmd = NL80211_CMD_DEAUTHENTICATE,
6826 .doit = nl80211_deauthenticate,
6827 .policy = nl80211_policy,
6828 .flags = GENL_ADMIN_PERM,
6829 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6830 NL80211_FLAG_NEED_RTNL,
6831 },
6832 {
6833 .cmd = NL80211_CMD_DISASSOCIATE,
6834 .doit = nl80211_disassociate,
6835 .policy = nl80211_policy,
6836 .flags = GENL_ADMIN_PERM,
6837 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6838 NL80211_FLAG_NEED_RTNL,
6839 },
6840 {
6841 .cmd = NL80211_CMD_JOIN_IBSS,
6842 .doit = nl80211_join_ibss,
6843 .policy = nl80211_policy,
6844 .flags = GENL_ADMIN_PERM,
6845 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6846 NL80211_FLAG_NEED_RTNL,
6847 },
6848 {
6849 .cmd = NL80211_CMD_LEAVE_IBSS,
6850 .doit = nl80211_leave_ibss,
6851 .policy = nl80211_policy,
6852 .flags = GENL_ADMIN_PERM,
6853 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6854 NL80211_FLAG_NEED_RTNL,
6855 },
6856#ifdef CONFIG_NL80211_TESTMODE
6857 {
6858 .cmd = NL80211_CMD_TESTMODE,
6859 .doit = nl80211_testmode_do,
6860 .dumpit = nl80211_testmode_dump,
6861 .policy = nl80211_policy,
6862 .flags = GENL_ADMIN_PERM,
6863 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6864 NL80211_FLAG_NEED_RTNL,
6865 },
6866#endif
6867 {
6868 .cmd = NL80211_CMD_CONNECT,
6869 .doit = nl80211_connect,
6870 .policy = nl80211_policy,
6871 .flags = GENL_ADMIN_PERM,
6872 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6873 NL80211_FLAG_NEED_RTNL,
6874 },
6875 {
6876 .cmd = NL80211_CMD_DISCONNECT,
6877 .doit = nl80211_disconnect,
6878 .policy = nl80211_policy,
6879 .flags = GENL_ADMIN_PERM,
6880 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6881 NL80211_FLAG_NEED_RTNL,
6882 },
6883 {
6884 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6885 .doit = nl80211_wiphy_netns,
6886 .policy = nl80211_policy,
6887 .flags = GENL_ADMIN_PERM,
6888 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6889 NL80211_FLAG_NEED_RTNL,
6890 },
6891 {
6892 .cmd = NL80211_CMD_GET_SURVEY,
6893 .policy = nl80211_policy,
6894 .dumpit = nl80211_dump_survey,
6895 },
6896 {
6897 .cmd = NL80211_CMD_SET_PMKSA,
6898 .doit = nl80211_setdel_pmksa,
6899 .policy = nl80211_policy,
6900 .flags = GENL_ADMIN_PERM,
6901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6902 NL80211_FLAG_NEED_RTNL,
6903 },
6904 {
6905 .cmd = NL80211_CMD_DEL_PMKSA,
6906 .doit = nl80211_setdel_pmksa,
6907 .policy = nl80211_policy,
6908 .flags = GENL_ADMIN_PERM,
6909 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6910 NL80211_FLAG_NEED_RTNL,
6911 },
6912#if defined(CONFIG_AIC8800)
6913 {
6914 .cmd = NL80211_CMD_EXTERNAL_AUTH,
6915 //.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6916 .doit = nl80211_external_auth,
6917 .flags = GENL_ADMIN_PERM,
6918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
6919 },
6920#endif
6921 {
6922 .cmd = NL80211_CMD_FLUSH_PMKSA,
6923 .doit = nl80211_flush_pmksa,
6924 .policy = nl80211_policy,
6925 .flags = GENL_ADMIN_PERM,
6926 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6927 NL80211_FLAG_NEED_RTNL,
6928 },
6929 {
6930 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6931 .doit = nl80211_remain_on_channel,
6932 .policy = nl80211_policy,
6933 .flags = GENL_ADMIN_PERM,
6934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6935 NL80211_FLAG_NEED_RTNL,
6936 },
6937 {
6938 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6939 .doit = nl80211_cancel_remain_on_channel,
6940 .policy = nl80211_policy,
6941 .flags = GENL_ADMIN_PERM,
6942 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6943 NL80211_FLAG_NEED_RTNL,
6944 },
6945 {
6946 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6947 .doit = nl80211_set_tx_bitrate_mask,
6948 .policy = nl80211_policy,
6949 .flags = GENL_ADMIN_PERM,
6950 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6951 NL80211_FLAG_NEED_RTNL,
6952 },
6953 {
6954 .cmd = NL80211_CMD_REGISTER_FRAME,
6955 .doit = nl80211_register_mgmt,
6956 .policy = nl80211_policy,
6957 .flags = GENL_ADMIN_PERM,
6958 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6959 NL80211_FLAG_NEED_RTNL,
6960 },
6961 {
6962 .cmd = NL80211_CMD_FRAME,
6963 .doit = nl80211_tx_mgmt,
6964 .policy = nl80211_policy,
6965 .flags = GENL_ADMIN_PERM,
6966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6967 NL80211_FLAG_NEED_RTNL,
6968 },
6969 {
6970 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6971 .doit = nl80211_tx_mgmt_cancel_wait,
6972 .policy = nl80211_policy,
6973 .flags = GENL_ADMIN_PERM,
6974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6975 NL80211_FLAG_NEED_RTNL,
6976 },
6977 {
6978 .cmd = NL80211_CMD_SET_POWER_SAVE,
6979 .doit = nl80211_set_power_save,
6980 .policy = nl80211_policy,
6981 .flags = GENL_ADMIN_PERM,
6982 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6983 NL80211_FLAG_NEED_RTNL,
6984 },
6985 {
6986 .cmd = NL80211_CMD_GET_POWER_SAVE,
6987 .doit = nl80211_get_power_save,
6988 .policy = nl80211_policy,
6989 /* can be retrieved by unprivileged users */
6990 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6991 NL80211_FLAG_NEED_RTNL,
6992 },
6993 {
6994 .cmd = NL80211_CMD_SET_CQM,
6995 .doit = nl80211_set_cqm,
6996 .policy = nl80211_policy,
6997 .flags = GENL_ADMIN_PERM,
6998 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6999 NL80211_FLAG_NEED_RTNL,
7000 },
7001 {
7002 .cmd = NL80211_CMD_SET_CHANNEL,
7003 .doit = nl80211_set_channel,
7004 .policy = nl80211_policy,
7005 .flags = GENL_ADMIN_PERM,
7006 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7007 NL80211_FLAG_NEED_RTNL,
7008 },
7009 {
7010 .cmd = NL80211_CMD_SET_WDS_PEER,
7011 .doit = nl80211_set_wds_peer,
7012 .policy = nl80211_policy,
7013 .flags = GENL_ADMIN_PERM,
7014 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7015 NL80211_FLAG_NEED_RTNL,
7016 },
7017 {
7018 .cmd = NL80211_CMD_JOIN_MESH,
7019 .doit = nl80211_join_mesh,
7020 .policy = nl80211_policy,
7021 .flags = GENL_ADMIN_PERM,
7022 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7023 NL80211_FLAG_NEED_RTNL,
7024 },
7025 {
7026 .cmd = NL80211_CMD_LEAVE_MESH,
7027 .doit = nl80211_leave_mesh,
7028 .policy = nl80211_policy,
7029 .flags = GENL_ADMIN_PERM,
7030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7031 NL80211_FLAG_NEED_RTNL,
7032 },
7033 {
7034 .cmd = NL80211_CMD_GET_WOWLAN,
7035 .doit = nl80211_get_wowlan,
7036 .policy = nl80211_policy,
7037 /* can be retrieved by unprivileged users */
7038 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7039 NL80211_FLAG_NEED_RTNL,
7040 },
7041 {
7042 .cmd = NL80211_CMD_SET_WOWLAN,
7043 .doit = nl80211_set_wowlan,
7044 .policy = nl80211_policy,
7045 .flags = GENL_ADMIN_PERM,
7046 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7047 NL80211_FLAG_NEED_RTNL,
7048 },
7049 {
7050 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7051 .doit = nl80211_set_rekey_data,
7052 .policy = nl80211_policy,
7053 .flags = GENL_ADMIN_PERM,
7054 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7055 NL80211_FLAG_NEED_RTNL,
7056 },
7057 {
7058 .cmd = NL80211_CMD_TDLS_MGMT,
7059 .doit = nl80211_tdls_mgmt,
7060 .policy = nl80211_policy,
7061 .flags = GENL_ADMIN_PERM,
7062 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7063 NL80211_FLAG_NEED_RTNL,
7064 },
7065 {
7066 .cmd = NL80211_CMD_TDLS_OPER,
7067 .doit = nl80211_tdls_oper,
7068 .policy = nl80211_policy,
7069 .flags = GENL_ADMIN_PERM,
7070 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7071 NL80211_FLAG_NEED_RTNL,
7072 },
7073 {
7074 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7075 .doit = nl80211_register_unexpected_frame,
7076 .policy = nl80211_policy,
7077 .flags = GENL_ADMIN_PERM,
7078 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7079 NL80211_FLAG_NEED_RTNL,
7080 },
7081 {
7082 .cmd = NL80211_CMD_PROBE_CLIENT,
7083 .doit = nl80211_probe_client,
7084 .policy = nl80211_policy,
7085 .flags = GENL_ADMIN_PERM,
7086 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7087 NL80211_FLAG_NEED_RTNL,
7088 },
7089 {
7090 .cmd = NL80211_CMD_REGISTER_BEACONS,
7091 .doit = nl80211_register_beacons,
7092 .policy = nl80211_policy,
7093 .flags = GENL_ADMIN_PERM,
7094 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7095 NL80211_FLAG_NEED_RTNL,
7096 },
7097 {
7098 .cmd = NL80211_CMD_SET_NOACK_MAP,
7099 .doit = nl80211_set_noack_map,
7100 .policy = nl80211_policy,
7101 .flags = GENL_ADMIN_PERM,
7102 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7103 NL80211_FLAG_NEED_RTNL,
7104 },
7105
7106};
7107
7108static struct genl_multicast_group nl80211_mlme_mcgrp = {
7109 .name = "mlme",
7110};
7111
7112/* multicast groups */
7113static struct genl_multicast_group nl80211_config_mcgrp = {
7114 .name = "config",
7115};
7116static struct genl_multicast_group nl80211_scan_mcgrp = {
7117 .name = "scan",
7118};
7119static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7120 .name = "regulatory",
7121};
7122
7123/* notification functions */
7124
7125void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7126{
7127 struct sk_buff *msg;
7128
7129 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7130 if (!msg)
7131 return;
7132
7133 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7134 nlmsg_free(msg);
7135 return;
7136 }
7137
7138 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7139 nl80211_config_mcgrp.id, GFP_KERNEL);
7140}
7141
7142static int nl80211_add_scan_req(struct sk_buff *msg,
7143 struct cfg80211_registered_device *rdev)
7144{
7145 struct cfg80211_scan_request *req = rdev->scan_req;
7146 struct nlattr *nest;
7147 int i;
7148
7149 ASSERT_RDEV_LOCK(rdev);
7150
7151 if (WARN_ON(!req))
7152 return 0;
7153
7154 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7155 if (!nest)
7156 goto nla_put_failure;
7157 for (i = 0; i < req->n_ssids; i++)
7158 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
7159 nla_nest_end(msg, nest);
7160
7161 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7162 if (!nest)
7163 goto nla_put_failure;
7164 for (i = 0; i < req->n_channels; i++)
7165 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
7166 nla_nest_end(msg, nest);
7167
7168 if (req->ie)
7169 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
7170
7171 return 0;
7172 nla_put_failure:
7173 return -ENOBUFS;
7174}
7175
7176static int nl80211_send_scan_msg(struct sk_buff *msg,
7177 struct cfg80211_registered_device *rdev,
7178 struct net_device *netdev,
7179 u32 pid, u32 seq, int flags,
7180 u32 cmd)
7181{
7182 void *hdr;
7183
7184 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7185 if (!hdr)
7186 return -1;
7187
7188 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7189 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7190
7191 /* ignore errors and send incomplete event anyway */
7192 nl80211_add_scan_req(msg, rdev);
7193
7194 return genlmsg_end(msg, hdr);
7195
7196 nla_put_failure:
7197 genlmsg_cancel(msg, hdr);
7198 return -EMSGSIZE;
7199}
7200
7201static int
7202nl80211_send_sched_scan_msg(struct sk_buff *msg,
7203 struct cfg80211_registered_device *rdev,
7204 struct net_device *netdev,
7205 u32 pid, u32 seq, int flags, u32 cmd)
7206{
7207 void *hdr;
7208
7209 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7210 if (!hdr)
7211 return -1;
7212
7213 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7214 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7215
7216 return genlmsg_end(msg, hdr);
7217
7218 nla_put_failure:
7219 genlmsg_cancel(msg, hdr);
7220 return -EMSGSIZE;
7221}
7222
7223void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7224 struct net_device *netdev)
7225{
7226 struct sk_buff *msg;
7227
7228 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7229 if (!msg)
7230 return;
7231
7232 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7233 NL80211_CMD_TRIGGER_SCAN) < 0) {
7234 nlmsg_free(msg);
7235 return;
7236 }
7237
7238 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7239 nl80211_scan_mcgrp.id, GFP_KERNEL);
7240}
7241
7242void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7243 struct net_device *netdev)
7244{
7245 struct sk_buff *msg;
7246
7247 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7248 if (!msg)
7249 return;
7250
7251 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7252 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
7253 nlmsg_free(msg);
7254 return;
7255 }
7256
7257 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7258 nl80211_scan_mcgrp.id, GFP_KERNEL);
7259}
7260
7261void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7262 struct net_device *netdev)
7263{
7264 struct sk_buff *msg;
7265
7266 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7267 if (!msg)
7268 return;
7269
7270 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7271 NL80211_CMD_SCAN_ABORTED) < 0) {
7272 nlmsg_free(msg);
7273 return;
7274 }
7275
7276 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7277 nl80211_scan_mcgrp.id, GFP_KERNEL);
7278}
7279
7280void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7281 struct net_device *netdev)
7282{
7283 struct sk_buff *msg;
7284
7285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7286 if (!msg)
7287 return;
7288
7289 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7290 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7291 nlmsg_free(msg);
7292 return;
7293 }
7294
7295 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7296 nl80211_scan_mcgrp.id, GFP_KERNEL);
7297}
7298
7299void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7300 struct net_device *netdev, u32 cmd)
7301{
7302 struct sk_buff *msg;
7303
7304 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7305 if (!msg)
7306 return;
7307
7308 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7309 nlmsg_free(msg);
7310 return;
7311 }
7312
7313 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7314 nl80211_scan_mcgrp.id, GFP_KERNEL);
7315}
7316
7317/*
7318 * This can happen on global regulatory changes or device specific settings
7319 * based on custom world regulatory domains.
7320 */
7321void nl80211_send_reg_change_event(struct regulatory_request *request)
7322{
7323 struct sk_buff *msg;
7324 void *hdr;
7325
7326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7327 if (!msg)
7328 return;
7329
7330 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7331 if (!hdr) {
7332 nlmsg_free(msg);
7333 return;
7334 }
7335
7336 /* Userspace can always count this one always being set */
7337 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
7338
7339 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
7340 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7341 NL80211_REGDOM_TYPE_WORLD);
7342 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
7343 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7344 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
7345 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7346 request->intersect)
7347 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7348 NL80211_REGDOM_TYPE_INTERSECTION);
7349 else {
7350 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7351 NL80211_REGDOM_TYPE_COUNTRY);
7352 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
7353 }
7354
7355 if (wiphy_idx_valid(request->wiphy_idx))
7356 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
7357
7358 genlmsg_end(msg, hdr);
7359
7360 rcu_read_lock();
7361 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7362 GFP_ATOMIC);
7363 rcu_read_unlock();
7364
7365 return;
7366
7367nla_put_failure:
7368 genlmsg_cancel(msg, hdr);
7369 nlmsg_free(msg);
7370}
7371
7372static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7373 struct net_device *netdev,
7374 const u8 *buf, size_t len,
7375 enum nl80211_commands cmd, gfp_t gfp)
7376{
7377 struct sk_buff *msg;
7378 void *hdr;
7379
7380 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7381 if (!msg)
7382 return;
7383
7384 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7385 if (!hdr) {
7386 nlmsg_free(msg);
7387 return;
7388 }
7389
7390 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7391 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7392 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7393
7394 genlmsg_end(msg, hdr);
7395
7396 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7397 nl80211_mlme_mcgrp.id, gfp);
7398 return;
7399
7400 nla_put_failure:
7401 genlmsg_cancel(msg, hdr);
7402 nlmsg_free(msg);
7403}
7404
7405void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
7406 struct net_device *netdev, const u8 *buf,
7407 size_t len, gfp_t gfp)
7408{
7409 nl80211_send_mlme_event(rdev, netdev, buf, len,
7410 NL80211_CMD_AUTHENTICATE, gfp);
7411}
7412
7413void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7414 struct net_device *netdev, const u8 *buf,
7415 size_t len, gfp_t gfp)
7416{
7417 nl80211_send_mlme_event(rdev, netdev, buf, len,
7418 NL80211_CMD_ASSOCIATE, gfp);
7419}
7420
7421void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
7422 struct net_device *netdev, const u8 *buf,
7423 size_t len, gfp_t gfp)
7424{
7425 nl80211_send_mlme_event(rdev, netdev, buf, len,
7426 NL80211_CMD_DEAUTHENTICATE, gfp);
7427}
7428
7429void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7430 struct net_device *netdev, const u8 *buf,
7431 size_t len, gfp_t gfp)
7432{
7433 nl80211_send_mlme_event(rdev, netdev, buf, len,
7434 NL80211_CMD_DISASSOCIATE, gfp);
7435}
7436
7437void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7438 struct net_device *netdev, const u8 *buf,
7439 size_t len, gfp_t gfp)
7440{
7441 nl80211_send_mlme_event(rdev, netdev, buf, len,
7442 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7443}
7444
7445void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7446 struct net_device *netdev, const u8 *buf,
7447 size_t len, gfp_t gfp)
7448{
7449 nl80211_send_mlme_event(rdev, netdev, buf, len,
7450 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7451}
7452
7453static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7454 struct net_device *netdev, int cmd,
7455 const u8 *addr, gfp_t gfp)
7456{
7457 struct sk_buff *msg;
7458 void *hdr;
7459
7460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7461 if (!msg)
7462 return;
7463
7464 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7465 if (!hdr) {
7466 nlmsg_free(msg);
7467 return;
7468 }
7469
7470 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7471 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7472 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7473 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7474
7475 genlmsg_end(msg, hdr);
7476
7477 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7478 nl80211_mlme_mcgrp.id, gfp);
7479 return;
7480
7481 nla_put_failure:
7482 genlmsg_cancel(msg, hdr);
7483 nlmsg_free(msg);
7484}
7485
7486void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
7487 struct net_device *netdev, const u8 *addr,
7488 gfp_t gfp)
7489{
7490 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
7491 addr, gfp);
7492}
7493
7494void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
7495 struct net_device *netdev, const u8 *addr,
7496 gfp_t gfp)
7497{
7498 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7499 addr, gfp);
7500}
7501
7502void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7503 struct net_device *netdev, const u8 *bssid,
7504 const u8 *req_ie, size_t req_ie_len,
7505 const u8 *resp_ie, size_t resp_ie_len,
7506 u16 status, gfp_t gfp)
7507{
7508 struct sk_buff *msg;
7509 void *hdr;
7510
7511 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7512 if (!msg)
7513 return;
7514
7515 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7516 if (!hdr) {
7517 nlmsg_free(msg);
7518 return;
7519 }
7520
7521 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7522 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7523 if (bssid)
7524 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7525 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7526 if (req_ie)
7527 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7528 if (resp_ie)
7529 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7530
7531 genlmsg_end(msg, hdr);
7532
7533 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7534 nl80211_mlme_mcgrp.id, gfp);
7535 return;
7536
7537 nla_put_failure:
7538 genlmsg_cancel(msg, hdr);
7539 nlmsg_free(msg);
7540
7541}
7542
7543void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7544 struct net_device *netdev, const u8 *bssid,
7545 const u8 *req_ie, size_t req_ie_len,
7546 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7547{
7548 struct sk_buff *msg;
7549 void *hdr;
7550
7551 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7552 if (!msg)
7553 return;
7554
7555 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7556 if (!hdr) {
7557 nlmsg_free(msg);
7558 return;
7559 }
7560
7561 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7562 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7563 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7564 if (req_ie)
7565 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7566 if (resp_ie)
7567 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7568
7569 genlmsg_end(msg, hdr);
7570
7571 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7572 nl80211_mlme_mcgrp.id, gfp);
7573 return;
7574
7575 nla_put_failure:
7576 genlmsg_cancel(msg, hdr);
7577 nlmsg_free(msg);
7578
7579}
7580
7581void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7582 struct net_device *netdev, u16 reason,
7583 const u8 *ie, size_t ie_len, bool from_ap)
7584{
7585 struct sk_buff *msg;
7586 void *hdr;
7587
7588 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7589 if (!msg)
7590 return;
7591
7592 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7593 if (!hdr) {
7594 nlmsg_free(msg);
7595 return;
7596 }
7597
7598 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7599 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7600 if (from_ap && reason)
7601 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7602 if (from_ap)
7603 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7604 if (ie)
7605 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7606
7607 genlmsg_end(msg, hdr);
7608
7609 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7610 nl80211_mlme_mcgrp.id, GFP_KERNEL);
7611 return;
7612
7613 nla_put_failure:
7614 genlmsg_cancel(msg, hdr);
7615 nlmsg_free(msg);
7616
7617}
7618
7619void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7620 struct net_device *netdev, const u8 *bssid,
7621 gfp_t gfp)
7622{
7623 struct sk_buff *msg;
7624 void *hdr;
7625
7626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7627 if (!msg)
7628 return;
7629
7630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7631 if (!hdr) {
7632 nlmsg_free(msg);
7633 return;
7634 }
7635
7636 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7637 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7638 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7639
7640 genlmsg_end(msg, hdr);
7641
7642 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7643 nl80211_mlme_mcgrp.id, gfp);
7644 return;
7645
7646 nla_put_failure:
7647 genlmsg_cancel(msg, hdr);
7648 nlmsg_free(msg);
7649}
7650
7651void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7652 struct net_device *netdev,
7653 const u8 *macaddr, const u8* ie, u8 ie_len,
7654 gfp_t gfp)
7655{
7656 struct sk_buff *msg;
7657 void *hdr;
7658
7659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7660 if (!msg)
7661 return;
7662
7663 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7664 if (!hdr) {
7665 nlmsg_free(msg);
7666 return;
7667 }
7668
7669 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7671 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7672 if (ie_len && ie)
7673 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7674
7675 genlmsg_end(msg, hdr);
7676
7677 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7678 nl80211_mlme_mcgrp.id, gfp);
7679 return;
7680
7681 nla_put_failure:
7682 genlmsg_cancel(msg, hdr);
7683 nlmsg_free(msg);
7684}
7685
7686void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7687 struct net_device *netdev, const u8 *addr,
7688 enum nl80211_key_type key_type, int key_id,
7689 const u8 *tsc, gfp_t gfp)
7690{
7691 struct sk_buff *msg;
7692 void *hdr;
7693
7694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7695 if (!msg)
7696 return;
7697
7698 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7699 if (!hdr) {
7700 nlmsg_free(msg);
7701 return;
7702 }
7703
7704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7705 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7706 if (addr)
7707 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7708 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
7709 if (key_id != -1)
7710 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
7711 if (tsc)
7712 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7713
7714 genlmsg_end(msg, hdr);
7715
7716 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7717 nl80211_mlme_mcgrp.id, gfp);
7718 return;
7719
7720 nla_put_failure:
7721 genlmsg_cancel(msg, hdr);
7722 nlmsg_free(msg);
7723}
7724
7725void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7726 struct ieee80211_channel *channel_before,
7727 struct ieee80211_channel *channel_after)
7728{
7729 struct sk_buff *msg;
7730 void *hdr;
7731 struct nlattr *nl_freq;
7732
7733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
7734 if (!msg)
7735 return;
7736
7737 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7738 if (!hdr) {
7739 nlmsg_free(msg);
7740 return;
7741 }
7742
7743 /*
7744 * Since we are applying the beacon hint to a wiphy we know its
7745 * wiphy_idx is valid
7746 */
7747 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7748
7749 /* Before */
7750 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7751 if (!nl_freq)
7752 goto nla_put_failure;
7753 if (nl80211_msg_put_channel(msg, channel_before))
7754 goto nla_put_failure;
7755 nla_nest_end(msg, nl_freq);
7756
7757 /* After */
7758 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7759 if (!nl_freq)
7760 goto nla_put_failure;
7761 if (nl80211_msg_put_channel(msg, channel_after))
7762 goto nla_put_failure;
7763 nla_nest_end(msg, nl_freq);
7764
7765 genlmsg_end(msg, hdr);
7766
7767 rcu_read_lock();
7768 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7769 GFP_ATOMIC);
7770 rcu_read_unlock();
7771
7772 return;
7773
7774nla_put_failure:
7775 genlmsg_cancel(msg, hdr);
7776 nlmsg_free(msg);
7777}
7778
7779static void nl80211_send_remain_on_chan_event(
7780 int cmd, struct cfg80211_registered_device *rdev,
7781 struct net_device *netdev, u64 cookie,
7782 struct ieee80211_channel *chan,
7783 enum nl80211_channel_type channel_type,
7784 unsigned int duration, gfp_t gfp)
7785{
7786 struct sk_buff *msg;
7787 void *hdr;
7788
7789 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7790 if (!msg)
7791 return;
7792
7793 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7794 if (!hdr) {
7795 nlmsg_free(msg);
7796 return;
7797 }
7798
7799 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7800 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7801 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7802 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7803 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7804
7805 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7806 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7807
7808 genlmsg_end(msg, hdr);
7809
7810 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7811 nl80211_mlme_mcgrp.id, gfp);
7812 return;
7813
7814 nla_put_failure:
7815 genlmsg_cancel(msg, hdr);
7816 nlmsg_free(msg);
7817}
7818
7819void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7820 struct net_device *netdev, u64 cookie,
7821 struct ieee80211_channel *chan,
7822 enum nl80211_channel_type channel_type,
7823 unsigned int duration, gfp_t gfp)
7824{
7825 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7826 rdev, netdev, cookie, chan,
7827 channel_type, duration, gfp);
7828}
7829
7830void nl80211_send_remain_on_channel_cancel(
7831 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7832 u64 cookie, struct ieee80211_channel *chan,
7833 enum nl80211_channel_type channel_type, gfp_t gfp)
7834{
7835 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7836 rdev, netdev, cookie, chan,
7837 channel_type, 0, gfp);
7838}
7839
7840void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7841 struct net_device *dev, const u8 *mac_addr,
7842 struct station_info *sinfo, gfp_t gfp)
7843{
7844 struct sk_buff *msg;
7845
7846 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7847 if (!msg)
7848 return;
7849
7850 if (nl80211_send_station(msg, 0, 0, 0,
7851 rdev, dev, mac_addr, sinfo) < 0) {
7852 nlmsg_free(msg);
7853 return;
7854 }
7855
7856 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7857 nl80211_mlme_mcgrp.id, gfp);
7858}
7859
7860void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7861 struct net_device *dev, const u8 *mac_addr,
7862 gfp_t gfp)
7863{
7864 struct sk_buff *msg;
7865 void *hdr;
7866
7867 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7868 if (!msg)
7869 return;
7870
7871 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7872 if (!hdr) {
7873 nlmsg_free(msg);
7874 return;
7875 }
7876
7877 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7878 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7879
7880 genlmsg_end(msg, hdr);
7881
7882 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7883 nl80211_mlme_mcgrp.id, gfp);
7884 return;
7885
7886 nla_put_failure:
7887 genlmsg_cancel(msg, hdr);
7888 nlmsg_free(msg);
7889}
7890
7891static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7892 const u8 *addr, gfp_t gfp)
7893{
7894 struct wireless_dev *wdev = dev->ieee80211_ptr;
7895 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7896 struct sk_buff *msg;
7897 void *hdr;
7898 int err;
7899 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7900
7901 if (!nlpid)
7902 return false;
7903
7904 msg = nlmsg_new(100, gfp);
7905 if (!msg)
7906 return true;
7907
7908 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7909 if (!hdr) {
7910 nlmsg_free(msg);
7911 return true;
7912 }
7913
7914 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7915 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7916 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7917
7918 err = genlmsg_end(msg, hdr);
7919 if (err < 0) {
7920 nlmsg_free(msg);
7921 return true;
7922 }
7923
7924 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7925 return true;
7926
7927 nla_put_failure:
7928 genlmsg_cancel(msg, hdr);
7929 nlmsg_free(msg);
7930 return true;
7931}
7932
7933bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7934{
7935 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7936 addr, gfp);
7937}
7938
7939bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7940 const u8 *addr, gfp_t gfp)
7941{
7942 return __nl80211_unexpected_frame(dev,
7943 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7944 addr, gfp);
7945}
7946
7947int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7948 struct net_device *netdev, u32 nlpid,
7949 int freq, int sig_dbm,
7950 const u8 *buf, size_t len, gfp_t gfp)
7951{
7952 struct sk_buff *msg;
7953 void *hdr;
7954
7955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7956 if (!msg)
7957 return -ENOMEM;
7958
7959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7960 if (!hdr) {
7961 nlmsg_free(msg);
7962 return -ENOMEM;
7963 }
7964
7965 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7966 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7967 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7968 if (sig_dbm)
7969 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
7970 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7971
7972 genlmsg_end(msg, hdr);
7973
7974 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7975
7976 nla_put_failure:
7977 genlmsg_cancel(msg, hdr);
7978 nlmsg_free(msg);
7979 return -ENOBUFS;
7980}
7981
7982void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7983 struct net_device *netdev, u64 cookie,
7984 const u8 *buf, size_t len, bool ack,
7985 gfp_t gfp)
7986{
7987 struct sk_buff *msg;
7988 void *hdr;
7989
7990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7991 if (!msg)
7992 return;
7993
7994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
7995 if (!hdr) {
7996 nlmsg_free(msg);
7997 return;
7998 }
7999
8000 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8001 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8002 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
8003 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
8004 if (ack)
8005 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
8006
8007 genlmsg_end(msg, hdr);
8008
8009 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8010 nl80211_mlme_mcgrp.id, gfp);
8011 return;
8012
8013 nla_put_failure:
8014 genlmsg_cancel(msg, hdr);
8015 nlmsg_free(msg);
8016}
8017
8018void
8019nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8020 struct net_device *netdev,
8021 enum nl80211_cqm_rssi_threshold_event rssi_event,
8022 gfp_t gfp)
8023{
8024 struct sk_buff *msg;
8025 struct nlattr *pinfoattr;
8026 void *hdr;
8027
8028 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8029 if (!msg)
8030 return;
8031
8032 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8033 if (!hdr) {
8034 nlmsg_free(msg);
8035 return;
8036 }
8037
8038 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8039 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8040
8041 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8042 if (!pinfoattr)
8043 goto nla_put_failure;
8044
8045 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8046 rssi_event);
8047
8048 nla_nest_end(msg, pinfoattr);
8049
8050 genlmsg_end(msg, hdr);
8051
8052 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8053 nl80211_mlme_mcgrp.id, gfp);
8054 return;
8055
8056 nla_put_failure:
8057 genlmsg_cancel(msg, hdr);
8058 nlmsg_free(msg);
8059}
8060
8061void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8062 struct net_device *netdev, const u8 *bssid,
8063 const u8 *replay_ctr, gfp_t gfp)
8064{
8065 struct sk_buff *msg;
8066 struct nlattr *rekey_attr;
8067 void *hdr;
8068
8069 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8070 if (!msg)
8071 return;
8072
8073 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8074 if (!hdr) {
8075 nlmsg_free(msg);
8076 return;
8077 }
8078
8079 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8080 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8081 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8082
8083 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8084 if (!rekey_attr)
8085 goto nla_put_failure;
8086
8087 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8088 NL80211_REPLAY_CTR_LEN, replay_ctr);
8089
8090 nla_nest_end(msg, rekey_attr);
8091
8092 genlmsg_end(msg, hdr);
8093
8094 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8095 nl80211_mlme_mcgrp.id, gfp);
8096 return;
8097
8098 nla_put_failure:
8099 genlmsg_cancel(msg, hdr);
8100 nlmsg_free(msg);
8101}
8102
8103void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8104 struct net_device *netdev, int index,
8105 const u8 *bssid, bool preauth, gfp_t gfp)
8106{
8107 struct sk_buff *msg;
8108 struct nlattr *attr;
8109 void *hdr;
8110
8111 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8112 if (!msg)
8113 return;
8114
8115 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8116 if (!hdr) {
8117 nlmsg_free(msg);
8118 return;
8119 }
8120
8121 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8123
8124 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8125 if (!attr)
8126 goto nla_put_failure;
8127
8128 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
8129 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
8130 if (preauth)
8131 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
8132
8133 nla_nest_end(msg, attr);
8134
8135 genlmsg_end(msg, hdr);
8136
8137 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8138 nl80211_mlme_mcgrp.id, gfp);
8139 return;
8140
8141 nla_put_failure:
8142 genlmsg_cancel(msg, hdr);
8143 nlmsg_free(msg);
8144}
8145
8146void
8147nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8148 struct net_device *netdev, const u8 *peer,
8149 u32 num_packets, gfp_t gfp)
8150{
8151 struct sk_buff *msg;
8152 struct nlattr *pinfoattr;
8153 void *hdr;
8154
8155 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8156 if (!msg)
8157 return;
8158
8159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8160 if (!hdr) {
8161 nlmsg_free(msg);
8162 return;
8163 }
8164
8165 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8167 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
8168
8169 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8170 if (!pinfoattr)
8171 goto nla_put_failure;
8172
8173 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
8174
8175 nla_nest_end(msg, pinfoattr);
8176
8177 genlmsg_end(msg, hdr);
8178
8179 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8180 nl80211_mlme_mcgrp.id, gfp);
8181 return;
8182
8183 nla_put_failure:
8184 genlmsg_cancel(msg, hdr);
8185 nlmsg_free(msg);
8186}
8187
8188void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8189 u64 cookie, bool acked, gfp_t gfp)
8190{
8191 struct wireless_dev *wdev = dev->ieee80211_ptr;
8192 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8193 struct sk_buff *msg;
8194 void *hdr;
8195 int err;
8196
8197 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8198 if (!msg)
8199 return;
8200
8201 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8202 if (!hdr) {
8203 nlmsg_free(msg);
8204 return;
8205 }
8206
8207 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8208 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
8209 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8210 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
8211 if (acked)
8212 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
8213
8214 err = genlmsg_end(msg, hdr);
8215 if (err < 0) {
8216 nlmsg_free(msg);
8217 return;
8218 }
8219
8220 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8221 nl80211_mlme_mcgrp.id, gfp);
8222 return;
8223
8224 nla_put_failure:
8225 genlmsg_cancel(msg, hdr);
8226 nlmsg_free(msg);
8227}
8228EXPORT_SYMBOL(cfg80211_probe_status);
8229
8230void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8231 const u8 *frame, size_t len,
8232 int freq, int sig_dbm, gfp_t gfp)
8233{
8234 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8235 struct sk_buff *msg;
8236 void *hdr;
8237 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8238
8239 if (!nlpid)
8240 return;
8241
8242 msg = nlmsg_new(len + 100, gfp);
8243 if (!msg)
8244 return;
8245
8246 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8247 if (!hdr) {
8248 nlmsg_free(msg);
8249 return;
8250 }
8251
8252 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8253 if (freq)
8254 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8255 if (sig_dbm)
8256 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
8257 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
8258
8259 genlmsg_end(msg, hdr);
8260
8261 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8262 return;
8263
8264 nla_put_failure:
8265 genlmsg_cancel(msg, hdr);
8266 nlmsg_free(msg);
8267}
8268EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8269
8270#if defined(CONFIG_AIC8800)
8271int cfg80211_external_auth_request(struct net_device *dev,
8272 struct cfg80211_external_auth_params *params,
8273 gfp_t gfp)
8274{
8275 struct wireless_dev *wdev = dev->ieee80211_ptr;
8276 // struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
8277 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8278 struct sk_buff *msg;
8279 void *hdr;
8280
8281 if (!wdev->conn_owner_nlportid) {
8282 return -EINVAL;
8283 }
8284
8285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8286 if (!msg)
8287 return -ENOMEM;
8288
8289 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
8290 if (!hdr)
8291 goto nla_put_failure;
8292
8293 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8294 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8295 nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
8296 nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
8297 params->action) ||
8298 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
8299 nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
8300 params->ssid.ssid))
8301 goto nla_put_failure;
8302
8303 genlmsg_end(msg, hdr);
8304 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
8305 wdev->conn_owner_nlportid);
8306 return 0;
8307
8308 nla_put_failure:
8309 nlmsg_free(msg);
8310 return -ENOBUFS;
8311}
8312EXPORT_SYMBOL(cfg80211_external_auth_request);
8313#endif
8314
8315static int nl80211_netlink_notify(struct notifier_block * nb,
8316 unsigned long state,
8317 void *_notify)
8318{
8319 struct netlink_notify *notify = _notify;
8320 struct cfg80211_registered_device *rdev;
8321 struct wireless_dev *wdev;
8322
8323 if (state != NETLINK_URELEASE)
8324 return NOTIFY_DONE;
8325
8326 rcu_read_lock();
8327
8328 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8329 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
8330 cfg80211_mlme_unregister_socket(wdev, notify->pid);
8331 if (rdev->ap_beacons_nlpid == notify->pid)
8332 rdev->ap_beacons_nlpid = 0;
8333 }
8334
8335 rcu_read_unlock();
8336
8337 return NOTIFY_DONE;
8338}
8339
8340static struct notifier_block nl80211_netlink_notifier = {
8341 .notifier_call = nl80211_netlink_notify,
8342};
8343
8344/* initialisation/exit functions */
8345
8346int nl80211_init(void)
8347{
8348 int err;
8349
8350 err = genl_register_family_with_ops(&nl80211_fam,
8351 nl80211_ops, ARRAY_SIZE(nl80211_ops));
8352 if (err)
8353 return err;
8354
8355 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8356 if (err)
8357 goto err_out;
8358
8359 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8360 if (err)
8361 goto err_out;
8362
8363 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8364 if (err)
8365 goto err_out;
8366
8367 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8368 if (err)
8369 goto err_out;
8370
8371#ifdef CONFIG_NL80211_TESTMODE
8372 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8373 if (err)
8374 goto err_out;
8375#endif
8376
8377 err = netlink_register_notifier(&nl80211_netlink_notifier);
8378 if (err)
8379 goto err_out;
8380
8381 return 0;
8382 err_out:
8383 genl_unregister_family(&nl80211_fam);
8384 return err;
8385}
8386
8387void nl80211_exit(void)
8388{
8389 netlink_unregister_notifier(&nl80211_netlink_notifier);
8390 genl_unregister_family(&nl80211_fam);
8391}