blob: b8d81e41b0f7ca3163a6ed9801deac073896a130 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#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>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Bergf84f7712013-11-14 17:14:45 +010033static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020034 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010035static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020036 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg2a94fe42013-11-19 15:19:39 +010050/* multicast groups */
51enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010056 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010057 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
58};
59
60static const struct genl_multicast_group nl80211_mcgrps[] = {
61 [NL80211_MCGRP_CONFIG] = { .name = "config", },
62 [NL80211_MCGRP_SCAN] = { .name = "scan", },
63 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
64 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010065 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010066#ifdef CONFIG_NL80211_TESTMODE
67 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
68#endif
69};
70
Johannes Berg89a54e42012-06-15 14:33:17 +020071/* returns ERR_PTR values */
72static struct wireless_dev *
73__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040074{
Johannes Berg89a54e42012-06-15 14:33:17 +020075 struct cfg80211_registered_device *rdev;
76 struct wireless_dev *result = NULL;
77 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
78 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
79 u64 wdev_id;
80 int wiphy_idx = -1;
81 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040082
Johannes Berg5fe231e2013-05-08 21:45:15 +020083 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040084
Johannes Berg89a54e42012-06-15 14:33:17 +020085 if (!have_ifidx && !have_wdev_id)
86 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040087
Johannes Berg89a54e42012-06-15 14:33:17 +020088 if (have_ifidx)
89 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
90 if (have_wdev_id) {
91 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
92 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040093 }
94
Johannes Berg89a54e42012-06-15 14:33:17 +020095 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
96 struct wireless_dev *wdev;
97
98 if (wiphy_net(&rdev->wiphy) != netns)
99 continue;
100
101 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
102 continue;
103
Johannes Berg89a54e42012-06-15 14:33:17 +0200104 list_for_each_entry(wdev, &rdev->wdev_list, list) {
105 if (have_ifidx && wdev->netdev &&
106 wdev->netdev->ifindex == ifidx) {
107 result = wdev;
108 break;
109 }
110 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
111 result = wdev;
112 break;
113 }
114 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200115
116 if (result)
117 break;
118 }
119
120 if (result)
121 return result;
122 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400123}
124
Johannes Berga9455402012-06-15 13:32:49 +0200125static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200126__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200127{
Johannes Berg7fee47782012-06-15 14:09:58 +0200128 struct cfg80211_registered_device *rdev = NULL, *tmp;
129 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200130
Johannes Berg5fe231e2013-05-08 21:45:15 +0200131 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200132
Johannes Berg878d9ec2012-06-15 14:18:32 +0200133 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200134 !attrs[NL80211_ATTR_IFINDEX] &&
135 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee47782012-06-15 14:09:58 +0200136 return ERR_PTR(-EINVAL);
137
Johannes Berg878d9ec2012-06-15 14:18:32 +0200138 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee47782012-06-15 14:09:58 +0200139 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200140 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200141
Johannes Berg89a54e42012-06-15 14:33:17 +0200142 if (attrs[NL80211_ATTR_WDEV]) {
143 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
144 struct wireless_dev *wdev;
145 bool found = false;
146
147 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
148 if (tmp) {
149 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200150 list_for_each_entry(wdev, &tmp->wdev_list, list) {
151 if (wdev->identifier != (u32)wdev_id)
152 continue;
153 found = true;
154 break;
155 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200156
157 if (!found)
158 tmp = NULL;
159
160 if (rdev && tmp != rdev)
161 return ERR_PTR(-EINVAL);
162 rdev = tmp;
163 }
164 }
165
Johannes Berg878d9ec2012-06-15 14:18:32 +0200166 if (attrs[NL80211_ATTR_IFINDEX]) {
167 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800168 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee47782012-06-15 14:09:58 +0200169 if (netdev) {
170 if (netdev->ieee80211_ptr)
171 tmp = wiphy_to_dev(
172 netdev->ieee80211_ptr->wiphy);
173 else
174 tmp = NULL;
175
Johannes Berg7fee47782012-06-15 14:09:58 +0200176 /* not wireless device -- return error */
177 if (!tmp)
178 return ERR_PTR(-EINVAL);
179
180 /* mismatch -- return error */
181 if (rdev && tmp != rdev)
182 return ERR_PTR(-EINVAL);
183
184 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200185 }
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
187
Johannes Berg4f7eff12012-06-15 14:14:22 +0200188 if (!rdev)
189 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200190
Johannes Berg4f7eff12012-06-15 14:14:22 +0200191 if (netns != wiphy_net(&rdev->wiphy))
192 return ERR_PTR(-ENODEV);
193
194 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200195}
196
197/*
198 * This function returns a pointer to the driver
199 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200200 *
201 * The result of this can be a PTR_ERR and hence must
202 * be checked with IS_ERR() for errors.
203 */
204static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200205cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200206{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200207 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208}
209
Johannes Berg55682962007-09-20 13:09:35 -0400210/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000211static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400212 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
213 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700214 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200215 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100216
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200217 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530218 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100219 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
220 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
222
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200223 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
224 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100227 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400228
229 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
230 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
231 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100232
Eliad Pellere007b852011-11-24 18:13:56 +0200233 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
234 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100235
Johannes Bergb9454e82009-07-08 13:29:08 +0200236 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100237 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
238 .len = WLAN_MAX_KEY_LEN },
239 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
240 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
241 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200242 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200243 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100244
245 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
246 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
247 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_DATA_LEN },
249 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100251 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
252 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
253 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
255 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100256 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100257 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200258 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100259 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800260 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300262
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
264 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
265
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300266 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
267 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
268 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200269 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
270 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100271 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300272
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800273 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700274 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700275
Johannes Berg6c739412011-11-03 09:27:01 +0100276 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200277
278 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
279 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
280 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100281 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
282 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200283
284 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_SSID_LEN },
286 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
287 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300289 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
293 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f782010-08-12 15:38:38 +0200316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
347 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
366 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg78f22b62014-03-24 17:57:27 +0100388 [NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400389};
390
Johannes Berge31b8212010-10-05 19:39:30 +0200391/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000392static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200393 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 [NL80211_KEY_IDX] = { .type = NLA_U8 },
395 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200396 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200397 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
398 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200399 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100400 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
401};
402
403/* policy for the key default flags */
404static const struct nla_policy
405nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
406 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
407 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200408};
409
Johannes Bergff1b6e62011-05-04 15:37:28 +0200410/* policy for WoWLAN attributes */
411static const struct nla_policy
412nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
413 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
416 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200417 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
420 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100421 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
422};
423
424static const struct nla_policy
425nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
426 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
428 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
429 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
433 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
434 },
435 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
436 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
437 },
438 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
439 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
440 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200441};
442
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -0700443/* policy for coalesce rule attributes */
444static const struct nla_policy
445nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
446 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
448 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
449};
450
Johannes Berge5497d72011-07-05 16:35:40 +0200451/* policy for GTK rekey offload attributes */
452static const struct nla_policy
453nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
454 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
455 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
456 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
457};
458
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300459static const struct nla_policy
460nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200461 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300462 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700463 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300464};
465
Johannes Berg97990a02013-04-19 01:02:55 +0200466static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
467 struct netlink_callback *cb,
468 struct cfg80211_registered_device **rdev,
469 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100470{
Johannes Berg67748892010-10-04 21:14:06 +0200471 int err;
472
Johannes Berg67748892010-10-04 21:14:06 +0200473 rtnl_lock();
474
Johannes Berg97990a02013-04-19 01:02:55 +0200475 if (!cb->args[0]) {
476 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
477 nl80211_fam.attrbuf, nl80211_fam.maxattr,
478 nl80211_policy);
479 if (err)
480 goto out_unlock;
481
482 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
483 nl80211_fam.attrbuf);
484 if (IS_ERR(*wdev)) {
485 err = PTR_ERR(*wdev);
486 goto out_unlock;
487 }
488 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200489 /* 0 is the first index - add 1 to parse only once */
490 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200491 cb->args[1] = (*wdev)->identifier;
492 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200493 /* subtract the 1 again here */
494 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200495 struct wireless_dev *tmp;
496
497 if (!wiphy) {
498 err = -ENODEV;
499 goto out_unlock;
500 }
501 *rdev = wiphy_to_dev(wiphy);
502 *wdev = NULL;
503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
505 if (tmp->identifier == cb->args[1]) {
506 *wdev = tmp;
507 break;
508 }
509 }
Johannes Berg97990a02013-04-19 01:02:55 +0200510
511 if (!*wdev) {
512 err = -ENODEV;
513 goto out_unlock;
514 }
Johannes Berg67748892010-10-04 21:14:06 +0200515 }
516
Johannes Berg67748892010-10-04 21:14:06 +0200517 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200518 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200519 rtnl_unlock();
520 return err;
521}
522
Johannes Berg97990a02013-04-19 01:02:55 +0200523static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200524{
Johannes Berg67748892010-10-04 21:14:06 +0200525 rtnl_unlock();
526}
527
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100528/* IE validation */
529static bool is_valid_ie_attr(const struct nlattr *attr)
530{
531 const u8 *pos;
532 int len;
533
534 if (!attr)
535 return true;
536
537 pos = nla_data(attr);
538 len = nla_len(attr);
539
540 while (len) {
541 u8 elemlen;
542
543 if (len < 2)
544 return false;
545 len -= 2;
546
547 elemlen = pos[1];
548 if (elemlen > len)
549 return false;
550
551 len -= elemlen;
552 pos += 2 + elemlen;
553 }
554
555 return true;
556}
557
Johannes Berg55682962007-09-20 13:09:35 -0400558/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000559static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400560 int flags, u8 cmd)
561{
562 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000563 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400564}
565
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400566static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100567 struct ieee80211_channel *chan,
568 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400569{
David S. Miller9360ffd2012-03-29 04:41:26 -0400570 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
571 chan->center_freq))
572 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400573
David S. Miller9360ffd2012-03-29 04:41:26 -0400574 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
575 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
576 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200577 if (chan->flags & IEEE80211_CHAN_NO_IR) {
578 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
579 goto nla_put_failure;
580 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
581 goto nla_put_failure;
582 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100583 if (chan->flags & IEEE80211_CHAN_RADAR) {
584 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
585 goto nla_put_failure;
586 if (large) {
587 u32 time;
588
589 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
590
591 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
592 chan->dfs_state))
593 goto nla_put_failure;
594 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
595 time))
596 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100597 if (nla_put_u32(msg,
598 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
599 chan->dfs_cac_ms))
600 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100601 }
602 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400603
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100604 if (large) {
605 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
606 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
607 goto nla_put_failure;
608 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
609 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
610 goto nla_put_failure;
611 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
612 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
613 goto nla_put_failure;
614 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
615 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
616 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200617 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
618 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
619 goto nla_put_failure;
620 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
621 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
622 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100623 }
624
David S. Miller9360ffd2012-03-29 04:41:26 -0400625 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
626 DBM_TO_MBM(chan->max_power)))
627 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400628
629 return 0;
630
631 nla_put_failure:
632 return -ENOBUFS;
633}
634
Johannes Berg55682962007-09-20 13:09:35 -0400635/* netlink command implementations */
636
Johannes Bergb9454e82009-07-08 13:29:08 +0200637struct key_parse {
638 struct key_params p;
639 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200640 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200641 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100642 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200643};
644
645static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
646{
647 struct nlattr *tb[NL80211_KEY_MAX + 1];
648 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
649 nl80211_key_policy);
650 if (err)
651 return err;
652
653 k->def = !!tb[NL80211_KEY_DEFAULT];
654 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
655
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100656 if (k->def) {
657 k->def_uni = true;
658 k->def_multi = true;
659 }
660 if (k->defmgmt)
661 k->def_multi = true;
662
Johannes Bergb9454e82009-07-08 13:29:08 +0200663 if (tb[NL80211_KEY_IDX])
664 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
665
666 if (tb[NL80211_KEY_DATA]) {
667 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
668 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
669 }
670
671 if (tb[NL80211_KEY_SEQ]) {
672 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
673 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
674 }
675
676 if (tb[NL80211_KEY_CIPHER])
677 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
678
Johannes Berge31b8212010-10-05 19:39:30 +0200679 if (tb[NL80211_KEY_TYPE]) {
680 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
681 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
682 return -EINVAL;
683 }
684
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100685 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
686 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100687 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
688 tb[NL80211_KEY_DEFAULT_TYPES],
689 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100690 if (err)
691 return err;
692
693 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
694 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
695 }
696
Johannes Bergb9454e82009-07-08 13:29:08 +0200697 return 0;
698}
699
700static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
701{
702 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
703 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
704 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
705 }
706
707 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
708 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
709 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
710 }
711
712 if (info->attrs[NL80211_ATTR_KEY_IDX])
713 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
714
715 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
716 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
717
718 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
719 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
720
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100721 if (k->def) {
722 k->def_uni = true;
723 k->def_multi = true;
724 }
725 if (k->defmgmt)
726 k->def_multi = true;
727
Johannes Berge31b8212010-10-05 19:39:30 +0200728 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
729 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
730 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
731 return -EINVAL;
732 }
733
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
735 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
736 int err = nla_parse_nested(
737 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
738 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
739 nl80211_key_default_policy);
740 if (err)
741 return err;
742
743 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
744 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
745 }
746
Johannes Bergb9454e82009-07-08 13:29:08 +0200747 return 0;
748}
749
750static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
751{
752 int err;
753
754 memset(k, 0, sizeof(*k));
755 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200756 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200757
758 if (info->attrs[NL80211_ATTR_KEY])
759 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
760 else
761 err = nl80211_parse_key_old(info, k);
762
763 if (err)
764 return err;
765
766 if (k->def && k->defmgmt)
767 return -EINVAL;
768
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100769 if (k->defmgmt) {
770 if (k->def_uni || !k->def_multi)
771 return -EINVAL;
772 }
773
Johannes Bergb9454e82009-07-08 13:29:08 +0200774 if (k->idx != -1) {
775 if (k->defmgmt) {
776 if (k->idx < 4 || k->idx > 5)
777 return -EINVAL;
778 } else if (k->def) {
779 if (k->idx < 0 || k->idx > 3)
780 return -EINVAL;
781 } else {
782 if (k->idx < 0 || k->idx > 5)
783 return -EINVAL;
784 }
785 }
786
787 return 0;
788}
789
Johannes Bergfffd0932009-07-08 14:22:54 +0200790static struct cfg80211_cached_keys *
791nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530792 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200793{
794 struct key_parse parse;
795 struct nlattr *key;
796 struct cfg80211_cached_keys *result;
797 int rem, err, def = 0;
798
799 result = kzalloc(sizeof(*result), GFP_KERNEL);
800 if (!result)
801 return ERR_PTR(-ENOMEM);
802
803 result->def = -1;
804 result->defmgmt = -1;
805
806 nla_for_each_nested(key, keys, rem) {
807 memset(&parse, 0, sizeof(parse));
808 parse.idx = -1;
809
810 err = nl80211_parse_key_new(key, &parse);
811 if (err)
812 goto error;
813 err = -EINVAL;
814 if (!parse.p.key)
815 goto error;
816 if (parse.idx < 0 || parse.idx > 4)
817 goto error;
818 if (parse.def) {
819 if (def)
820 goto error;
821 def = 1;
822 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100823 if (!parse.def_uni || !parse.def_multi)
824 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 } else if (parse.defmgmt)
826 goto error;
827 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200828 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 if (err)
830 goto error;
831 result->params[parse.idx].cipher = parse.p.cipher;
832 result->params[parse.idx].key_len = parse.p.key_len;
833 result->params[parse.idx].key = result->data[parse.idx];
834 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530835
836 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
837 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
838 if (no_ht)
839 *no_ht = true;
840 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200841 }
842
843 return result;
844 error:
845 kfree(result);
846 return ERR_PTR(err);
847}
848
849static int nl80211_key_allowed(struct wireless_dev *wdev)
850{
851 ASSERT_WDEV_LOCK(wdev);
852
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 switch (wdev->iftype) {
854 case NL80211_IFTYPE_AP:
855 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200856 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700857 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200858 break;
859 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200860 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200861 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200862 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200863 return -ENOLINK;
864 break;
865 default:
866 return -EINVAL;
867 }
868
869 return 0;
870}
871
Jouni Malinen664834d2014-01-15 00:01:44 +0200872static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
873 struct nlattr *tb)
874{
875 struct ieee80211_channel *chan;
876
877 if (tb == NULL)
878 return NULL;
879 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
880 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
881 return NULL;
882 return chan;
883}
884
Johannes Berg7527a782011-05-13 10:58:57 +0200885static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
886{
887 struct nlattr *nl_modes = nla_nest_start(msg, attr);
888 int i;
889
890 if (!nl_modes)
891 goto nla_put_failure;
892
893 i = 0;
894 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if ((ifmodes & 1) && nla_put_flag(msg, i))
896 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200897 ifmodes >>= 1;
898 i++;
899 }
900
901 nla_nest_end(msg, nl_modes);
902 return 0;
903
904nla_put_failure:
905 return -ENOBUFS;
906}
907
908static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100909 struct sk_buff *msg,
910 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200911{
912 struct nlattr *nl_combis;
913 int i, j;
914
915 nl_combis = nla_nest_start(msg,
916 NL80211_ATTR_INTERFACE_COMBINATIONS);
917 if (!nl_combis)
918 goto nla_put_failure;
919
920 for (i = 0; i < wiphy->n_iface_combinations; i++) {
921 const struct ieee80211_iface_combination *c;
922 struct nlattr *nl_combi, *nl_limits;
923
924 c = &wiphy->iface_combinations[i];
925
926 nl_combi = nla_nest_start(msg, i + 1);
927 if (!nl_combi)
928 goto nla_put_failure;
929
930 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
931 if (!nl_limits)
932 goto nla_put_failure;
933
934 for (j = 0; j < c->n_limits; j++) {
935 struct nlattr *nl_limit;
936
937 nl_limit = nla_nest_start(msg, j + 1);
938 if (!nl_limit)
939 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
941 c->limits[j].max))
942 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200943 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
944 c->limits[j].types))
945 goto nla_put_failure;
946 nla_nest_end(msg, nl_limit);
947 }
948
949 nla_nest_end(msg, nl_limits);
950
David S. Miller9360ffd2012-03-29 04:41:26 -0400951 if (c->beacon_int_infra_match &&
952 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
953 goto nla_put_failure;
954 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
955 c->num_different_channels) ||
956 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
957 c->max_interfaces))
958 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100959 if (large &&
960 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
961 c->radar_detect_widths))
962 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200963
964 nla_nest_end(msg, nl_combi);
965 }
966
967 nla_nest_end(msg, nl_combis);
968
969 return 0;
970nla_put_failure:
971 return -ENOBUFS;
972}
973
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100975static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
976 struct sk_buff *msg)
977{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200978 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100979 struct nlattr *nl_tcp;
980
981 if (!tcp)
982 return 0;
983
984 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
985 if (!nl_tcp)
986 return -ENOBUFS;
987
988 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
989 tcp->data_payload_max))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
993 tcp->data_payload_max))
994 return -ENOBUFS;
995
996 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
997 return -ENOBUFS;
998
999 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1000 sizeof(*tcp->tok), tcp->tok))
1001 return -ENOBUFS;
1002
1003 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1004 tcp->data_interval_max))
1005 return -ENOBUFS;
1006
1007 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1008 tcp->wake_payload_max))
1009 return -ENOBUFS;
1010
1011 nla_nest_end(msg, nl_tcp);
1012 return 0;
1013}
1014
Johannes Berg3713b4e2013-02-14 16:19:38 +01001015static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001016 struct cfg80211_registered_device *dev,
1017 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018{
1019 struct nlattr *nl_wowlan;
1020
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 return 0;
1023
1024 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1025 if (!nl_wowlan)
1026 return -ENOBUFS;
1027
Johannes Berg964dc9e2013-06-03 17:25:34 +02001028 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001029 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001030 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001031 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001032 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001033 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001034 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001035 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001036 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001037 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001038 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001039 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001040 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001042 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001043 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1044 return -ENOBUFS;
1045
Johannes Berg964dc9e2013-06-03 17:25:34 +02001046 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001047 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001048 .max_patterns = dev->wiphy.wowlan->n_patterns,
1049 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1050 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1051 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001052 };
1053
1054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 return -ENOBUFS;
1057 }
1058
Johannes Bergb56cf722013-02-20 01:02:38 +01001059 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1060 return -ENOBUFS;
1061
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062 nla_nest_end(msg, nl_wowlan);
1063
1064 return 0;
1065}
1066#endif
1067
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001068static int nl80211_send_coalesce(struct sk_buff *msg,
1069 struct cfg80211_registered_device *dev)
1070{
1071 struct nl80211_coalesce_rule_support rule;
1072
1073 if (!dev->wiphy.coalesce)
1074 return 0;
1075
1076 rule.max_rules = dev->wiphy.coalesce->n_rules;
1077 rule.max_delay = dev->wiphy.coalesce->max_delay;
1078 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1079 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1080 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1081 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1082
1083 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1084 return -ENOBUFS;
1085
1086 return 0;
1087}
1088
Johannes Berg3713b4e2013-02-14 16:19:38 +01001089static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1090 struct ieee80211_supported_band *sband)
1091{
1092 struct nlattr *nl_rates, *nl_rate;
1093 struct ieee80211_rate *rate;
1094 int i;
1095
1096 /* add HT info */
1097 if (sband->ht_cap.ht_supported &&
1098 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1099 sizeof(sband->ht_cap.mcs),
1100 &sband->ht_cap.mcs) ||
1101 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1102 sband->ht_cap.cap) ||
1103 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1104 sband->ht_cap.ampdu_factor) ||
1105 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1106 sband->ht_cap.ampdu_density)))
1107 return -ENOBUFS;
1108
1109 /* add VHT info */
1110 if (sband->vht_cap.vht_supported &&
1111 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1112 sizeof(sband->vht_cap.vht_mcs),
1113 &sband->vht_cap.vht_mcs) ||
1114 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1115 sband->vht_cap.cap)))
1116 return -ENOBUFS;
1117
1118 /* add bitrates */
1119 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1120 if (!nl_rates)
1121 return -ENOBUFS;
1122
1123 for (i = 0; i < sband->n_bitrates; i++) {
1124 nl_rate = nla_nest_start(msg, i);
1125 if (!nl_rate)
1126 return -ENOBUFS;
1127
1128 rate = &sband->bitrates[i];
1129 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1130 rate->bitrate))
1131 return -ENOBUFS;
1132 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1133 nla_put_flag(msg,
1134 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1135 return -ENOBUFS;
1136
1137 nla_nest_end(msg, nl_rate);
1138 }
1139
1140 nla_nest_end(msg, nl_rates);
1141
1142 return 0;
1143}
1144
1145static int
1146nl80211_send_mgmt_stypes(struct sk_buff *msg,
1147 const struct ieee80211_txrx_stypes *mgmt_stypes)
1148{
1149 u16 stypes;
1150 struct nlattr *nl_ftypes, *nl_ifs;
1151 enum nl80211_iftype ift;
1152 int i;
1153
1154 if (!mgmt_stypes)
1155 return 0;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 return -ENOBUFS;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 return -ENOBUFS;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
1168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 return -ENOBUFS;
1172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
1178 nla_nest_end(msg, nl_ifs);
1179
1180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 return -ENOBUFS;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 return -ENOBUFS;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
1191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 return -ENOBUFS;
1195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201
1202 return 0;
1203}
1204
Johannes Berg86e8cf92013-06-19 10:57:22 +02001205struct nl80211_dump_wiphy_state {
1206 s64 filter_wiphy;
1207 long start;
1208 long split_start, band_start, chan_start;
1209 bool split;
1210};
1211
Johannes Berg3713b4e2013-02-14 16:19:38 +01001212static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1213 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001214 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001215{
1216 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001217 struct nlattr *nl_bands, *nl_band;
1218 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001219 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001220 enum ieee80211_band band;
1221 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001222 int i;
Johannes Berg2e161f782010-08-12 15:38:38 +02001223 const struct ieee80211_txrx_stypes *mgmt_stypes =
1224 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001225 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001226
Eric W. Biederman15e47302012-09-07 20:12:54 +00001227 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001228 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001229 return -ENOBUFS;
1230
Johannes Berg86e8cf92013-06-19 10:57:22 +02001231 if (WARN_ON(!state))
1232 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001235 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1236 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001240
Johannes Berg86e8cf92013-06-19 10:57:22 +02001241 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001242 case 0:
1243 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1244 dev->wiphy.retry_short) ||
1245 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1246 dev->wiphy.retry_long) ||
1247 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1248 dev->wiphy.frag_threshold) ||
1249 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1250 dev->wiphy.rts_threshold) ||
1251 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1252 dev->wiphy.coverage_class) ||
1253 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1254 dev->wiphy.max_scan_ssids) ||
1255 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1256 dev->wiphy.max_sched_scan_ssids) ||
1257 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1258 dev->wiphy.max_scan_ie_len) ||
1259 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1260 dev->wiphy.max_sched_scan_ie_len) ||
1261 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1262 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001263 goto nla_put_failure;
1264
Johannes Berg3713b4e2013-02-14 16:19:38 +01001265 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1266 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1267 goto nla_put_failure;
1268 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1269 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1270 goto nla_put_failure;
1271 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1272 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1273 goto nla_put_failure;
1274 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1275 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1276 goto nla_put_failure;
1277 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1278 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1279 goto nla_put_failure;
1280 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1281 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001282 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001283 state->split_start++;
1284 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 break;
1286 case 1:
1287 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1288 sizeof(u32) * dev->wiphy.n_cipher_suites,
1289 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1293 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001294 goto nla_put_failure;
1295
Johannes Berg3713b4e2013-02-14 16:19:38 +01001296 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1297 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1298 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1301 dev->wiphy.available_antennas_tx) ||
1302 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1303 dev->wiphy.available_antennas_rx))
1304 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001305
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1307 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1308 dev->wiphy.probe_resp_offload))
1309 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001310
Johannes Berg3713b4e2013-02-14 16:19:38 +01001311 if ((dev->wiphy.available_antennas_tx ||
1312 dev->wiphy.available_antennas_rx) &&
1313 dev->ops->get_antenna) {
1314 u32 tx_ant = 0, rx_ant = 0;
1315 int res;
1316 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1317 if (!res) {
1318 if (nla_put_u32(msg,
1319 NL80211_ATTR_WIPHY_ANTENNA_TX,
1320 tx_ant) ||
1321 nla_put_u32(msg,
1322 NL80211_ATTR_WIPHY_ANTENNA_RX,
1323 rx_ant))
1324 goto nla_put_failure;
1325 }
Johannes Bergee688b002008-01-24 19:38:39 +01001326 }
1327
Johannes Berg86e8cf92013-06-19 10:57:22 +02001328 state->split_start++;
1329 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 break;
1331 case 2:
1332 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1333 dev->wiphy.interface_modes))
1334 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001335 state->split_start++;
1336 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001337 break;
1338 case 3:
1339 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1340 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001341 goto nla_put_failure;
1342
Johannes Berg86e8cf92013-06-19 10:57:22 +02001343 for (band = state->band_start;
1344 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001345 struct ieee80211_supported_band *sband;
1346
1347 sband = dev->wiphy.bands[band];
1348
1349 if (!sband)
1350 continue;
1351
1352 nl_band = nla_nest_start(msg, band);
1353 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001354 goto nla_put_failure;
1355
Johannes Berg86e8cf92013-06-19 10:57:22 +02001356 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001357 case 0:
1358 if (nl80211_send_band_rateinfo(msg, sband))
1359 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001360 state->chan_start++;
1361 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001362 break;
1363 default:
1364 /* add frequencies */
1365 nl_freqs = nla_nest_start(
1366 msg, NL80211_BAND_ATTR_FREQS);
1367 if (!nl_freqs)
1368 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001369
Johannes Berg86e8cf92013-06-19 10:57:22 +02001370 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 i < sband->n_channels;
1372 i++) {
1373 nl_freq = nla_nest_start(msg, i);
1374 if (!nl_freq)
1375 goto nla_put_failure;
1376
1377 chan = &sband->channels[i];
1378
Johannes Berg86e8cf92013-06-19 10:57:22 +02001379 if (nl80211_msg_put_channel(
1380 msg, chan,
1381 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001382 goto nla_put_failure;
1383
1384 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001385 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001386 break;
1387 }
1388 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001389 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001390 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001391 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001392 nla_nest_end(msg, nl_freqs);
1393 }
1394
1395 nla_nest_end(msg, nl_band);
1396
Johannes Berg86e8cf92013-06-19 10:57:22 +02001397 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001398 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001399 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 band--;
1401 break;
1402 }
Johannes Bergee688b002008-01-24 19:38:39 +01001403 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001405
Johannes Berg3713b4e2013-02-14 16:19:38 +01001406 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001407 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001409 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001410
Johannes Berg3713b4e2013-02-14 16:19:38 +01001411 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001412 if (state->band_start == 0 && state->chan_start == 0)
1413 state->split_start++;
1414 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001415 break;
1416 case 4:
1417 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1418 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001419 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001420
1421 i = 0;
1422#define CMD(op, n) \
1423 do { \
1424 if (dev->ops->op) { \
1425 i++; \
1426 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1427 goto nla_put_failure; \
1428 } \
1429 } while (0)
1430
1431 CMD(add_virtual_intf, NEW_INTERFACE);
1432 CMD(change_virtual_intf, SET_INTERFACE);
1433 CMD(add_key, NEW_KEY);
1434 CMD(start_ap, START_AP);
1435 CMD(add_station, NEW_STATION);
1436 CMD(add_mpath, NEW_MPATH);
1437 CMD(update_mesh_config, SET_MESH_CONFIG);
1438 CMD(change_bss, SET_BSS);
1439 CMD(auth, AUTHENTICATE);
1440 CMD(assoc, ASSOCIATE);
1441 CMD(deauth, DEAUTHENTICATE);
1442 CMD(disassoc, DISASSOCIATE);
1443 CMD(join_ibss, JOIN_IBSS);
1444 CMD(join_mesh, JOIN_MESH);
1445 CMD(set_pmksa, SET_PMKSA);
1446 CMD(del_pmksa, DEL_PMKSA);
1447 CMD(flush_pmksa, FLUSH_PMKSA);
1448 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1449 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1450 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1451 CMD(mgmt_tx, FRAME);
1452 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1453 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1454 i++;
1455 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1456 goto nla_put_failure;
1457 }
1458 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1459 dev->ops->join_mesh) {
1460 i++;
1461 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1462 goto nla_put_failure;
1463 }
1464 CMD(set_wds_peer, SET_WDS_PEER);
1465 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1466 CMD(tdls_mgmt, TDLS_MGMT);
1467 CMD(tdls_oper, TDLS_OPER);
1468 }
1469 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1470 CMD(sched_scan_start, START_SCHED_SCAN);
1471 CMD(probe_client, PROBE_CLIENT);
1472 CMD(set_noack_map, SET_NOACK_MAP);
1473 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1474 i++;
1475 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1476 goto nla_put_failure;
1477 }
1478 CMD(start_p2p_device, START_P2P_DEVICE);
1479 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001480 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001481 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1482 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001483 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1484 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001485 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001486 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001487
Kalle Valo4745fc02011-11-17 19:06:10 +02001488#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001490#endif
1491
Johannes Berg8fdc6212009-03-14 09:34:01 +01001492#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001493
Johannes Berg3713b4e2013-02-14 16:19:38 +01001494 if (dev->ops->connect || dev->ops->auth) {
1495 i++;
1496 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f782010-08-12 15:38:38 +02001497 goto nla_put_failure;
Johannes Berg2e161f782010-08-12 15:38:38 +02001498 }
1499
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 if (dev->ops->disconnect || dev->ops->deauth) {
1501 i++;
1502 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1503 goto nla_put_failure;
1504 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001505
Johannes Berg3713b4e2013-02-14 16:19:38 +01001506 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001507 state->split_start++;
1508 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509 break;
1510 case 5:
1511 if (dev->ops->remain_on_channel &&
1512 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1513 nla_put_u32(msg,
1514 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1515 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f782010-08-12 15:38:38 +02001516 goto nla_put_failure;
1517
Johannes Berg3713b4e2013-02-14 16:19:38 +01001518 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1519 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1520 goto nla_put_failure;
Johannes Berg2e161f782010-08-12 15:38:38 +02001521
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1523 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
1525 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001526 break;
1527 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001528#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001529 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001530 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 state->split_start++;
1532 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 break;
1534#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001536#endif
1537 case 7:
1538 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1539 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001540 goto nla_put_failure;
1541
Johannes Berg86e8cf92013-06-19 10:57:22 +02001542 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1543 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001544 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001545
Johannes Berg86e8cf92013-06-19 10:57:22 +02001546 state->split_start++;
1547 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 break;
1549 case 8:
1550 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1551 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1552 dev->wiphy.ap_sme_capa))
1553 goto nla_put_failure;
1554
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001555 features = dev->wiphy.features;
1556 /*
1557 * We can only add the per-channel limit information if the
1558 * dump is split, otherwise it makes it too big. Therefore
1559 * only advertise it in that case.
1560 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001561 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001562 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1563 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001564 goto nla_put_failure;
1565
1566 if (dev->wiphy.ht_capa_mod_mask &&
1567 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1568 sizeof(*dev->wiphy.ht_capa_mod_mask),
1569 dev->wiphy.ht_capa_mod_mask))
1570 goto nla_put_failure;
1571
1572 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1573 dev->wiphy.max_acl_mac_addrs &&
1574 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1575 dev->wiphy.max_acl_mac_addrs))
1576 goto nla_put_failure;
1577
1578 /*
1579 * Any information below this point is only available to
1580 * applications that can deal with it being split. This
1581 * helps ensure that newly added capabilities don't break
1582 * older tools by overrunning their buffers.
1583 *
1584 * We still increment split_start so that in the split
1585 * case we'll continue with more data in the next round,
1586 * but break unconditionally so unsplit data stops here.
1587 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001588 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001589 break;
1590 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001591 if (dev->wiphy.extended_capabilities &&
1592 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1593 dev->wiphy.extended_capabilities_len,
1594 dev->wiphy.extended_capabilities) ||
1595 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1596 dev->wiphy.extended_capabilities_len,
1597 dev->wiphy.extended_capabilities_mask)))
1598 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001599
Johannes Bergee2aca32013-02-21 17:36:01 +01001600 if (dev->wiphy.vht_capa_mod_mask &&
1601 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1602 sizeof(*dev->wiphy.vht_capa_mod_mask),
1603 dev->wiphy.vht_capa_mod_mask))
1604 goto nla_put_failure;
1605
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001606 state->split_start++;
1607 break;
1608 case 10:
1609 if (nl80211_send_coalesce(msg, dev))
1610 goto nla_put_failure;
1611
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001612 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1613 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1614 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1615 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001616
1617 if (dev->wiphy.max_ap_assoc_sta &&
1618 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1619 dev->wiphy.max_ap_assoc_sta))
1620 goto nla_put_failure;
1621
Johannes Bergad7e7182013-11-13 13:37:47 +01001622 state->split_start++;
1623 break;
1624 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001625 if (dev->wiphy.n_vendor_commands) {
1626 const struct nl80211_vendor_cmd_info *info;
1627 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001628
Johannes Berg567ffc32013-12-18 14:43:31 +01001629 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1630 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001631 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001632
1633 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1634 info = &dev->wiphy.vendor_commands[i].info;
1635 if (nla_put(msg, i + 1, sizeof(*info), info))
1636 goto nla_put_failure;
1637 }
1638 nla_nest_end(msg, nested);
1639 }
1640
1641 if (dev->wiphy.n_vendor_events) {
1642 const struct nl80211_vendor_cmd_info *info;
1643 struct nlattr *nested;
1644
1645 nested = nla_nest_start(msg,
1646 NL80211_ATTR_VENDOR_EVENTS);
1647 if (!nested)
1648 goto nla_put_failure;
1649
1650 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1651 info = &dev->wiphy.vendor_events[i];
1652 if (nla_put(msg, i + 1, sizeof(*info), info))
1653 goto nla_put_failure;
1654 }
1655 nla_nest_end(msg, nested);
1656 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001657
Johannes Berg3713b4e2013-02-14 16:19:38 +01001658 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001659 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001660 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001661 }
Johannes Berg55682962007-09-20 13:09:35 -04001662 return genlmsg_end(msg, hdr);
1663
1664 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001665 genlmsg_cancel(msg, hdr);
1666 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001667}
1668
Johannes Berg86e8cf92013-06-19 10:57:22 +02001669static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1670 struct netlink_callback *cb,
1671 struct nl80211_dump_wiphy_state *state)
1672{
1673 struct nlattr **tb = nl80211_fam.attrbuf;
1674 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1675 tb, nl80211_fam.maxattr, nl80211_policy);
1676 /* ignore parse errors for backward compatibility */
1677 if (ret)
1678 return 0;
1679
1680 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1681 if (tb[NL80211_ATTR_WIPHY])
1682 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1683 if (tb[NL80211_ATTR_WDEV])
1684 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1685 if (tb[NL80211_ATTR_IFINDEX]) {
1686 struct net_device *netdev;
1687 struct cfg80211_registered_device *rdev;
1688 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1689
Ying Xue7f2b8562014-01-15 10:23:45 +08001690 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001691 if (!netdev)
1692 return -ENODEV;
1693 if (netdev->ieee80211_ptr) {
1694 rdev = wiphy_to_dev(
1695 netdev->ieee80211_ptr->wiphy);
1696 state->filter_wiphy = rdev->wiphy_idx;
1697 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001698 }
1699
1700 return 0;
1701}
1702
Johannes Berg55682962007-09-20 13:09:35 -04001703static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1704{
Johannes Berg645e77d2013-03-01 14:03:49 +01001705 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001706 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001707 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001708
Johannes Berg5fe231e2013-05-08 21:45:15 +02001709 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001710 if (!state) {
1711 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001712 if (!state) {
1713 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001714 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 state->filter_wiphy = -1;
1717 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1718 if (ret) {
1719 kfree(state);
1720 rtnl_unlock();
1721 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001723 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001724 }
1725
Johannes Berg79c97e92009-07-07 03:56:12 +02001726 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001727 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1728 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001729 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001730 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001731 if (state->filter_wiphy != -1 &&
1732 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001733 continue;
1734 /* attempt to fit multiple wiphy data chunks into the skb */
1735 do {
1736 ret = nl80211_send_wiphy(dev, skb,
1737 NETLINK_CB(cb->skb).portid,
1738 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001739 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001740 if (ret < 0) {
1741 /*
1742 * If sending the wiphy data didn't fit (ENOBUFS
1743 * or EMSGSIZE returned), this SKB is still
1744 * empty (so it's not too big because another
1745 * wiphy dataset is already in the skb) and
1746 * we've not tried to adjust the dump allocation
1747 * yet ... then adjust the alloc size to be
1748 * bigger, and return 1 but with the empty skb.
1749 * This results in an empty message being RX'ed
1750 * in userspace, but that is ignored.
1751 *
1752 * We can then retry with the larger buffer.
1753 */
1754 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001755 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001756 cb->min_dump_alloc < 4096) {
1757 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001758 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001759 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001760 return 1;
1761 }
1762 idx--;
1763 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001764 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001765 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001766 break;
Johannes Berg55682962007-09-20 13:09:35 -04001767 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001768 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001769
Johannes Berg86e8cf92013-06-19 10:57:22 +02001770 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001771
1772 return skb->len;
1773}
1774
Johannes Berg86e8cf92013-06-19 10:57:22 +02001775static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1776{
1777 kfree((void *)cb->args[0]);
1778 return 0;
1779}
1780
Johannes Berg55682962007-09-20 13:09:35 -04001781static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1782{
1783 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001784 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001785 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001786
Johannes Berg645e77d2013-03-01 14:03:49 +01001787 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001788 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001789 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001790
Johannes Berg3713b4e2013-02-14 16:19:38 +01001791 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001792 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001793 nlmsg_free(msg);
1794 return -ENOBUFS;
1795 }
Johannes Berg55682962007-09-20 13:09:35 -04001796
Johannes Berg134e6372009-07-10 09:51:34 +00001797 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001798}
1799
Jouni Malinen31888482008-10-30 16:59:24 +02001800static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1801 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1802 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1803 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1804 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1805 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1806};
1807
1808static int parse_txq_params(struct nlattr *tb[],
1809 struct ieee80211_txq_params *txq_params)
1810{
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001812 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1813 !tb[NL80211_TXQ_ATTR_AIFS])
1814 return -EINVAL;
1815
Johannes Berga3304b02012-03-28 11:04:24 +02001816 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001817 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1818 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1819 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1820 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1821
Johannes Berga3304b02012-03-28 11:04:24 +02001822 if (txq_params->ac >= NL80211_NUM_ACS)
1823 return -EINVAL;
1824
Jouni Malinen31888482008-10-30 16:59:24 +02001825 return 0;
1826}
1827
Johannes Bergf444de02010-05-05 15:25:02 +02001828static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1829{
1830 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001831 * You can only set the channel explicitly for WDS interfaces,
1832 * all others have their channel managed via their respective
1833 * "establish a connection" command (connect, join, ...)
1834 *
1835 * For AP/GO and mesh mode, the channel can be set with the
1836 * channel userspace API, but is only stored and passed to the
1837 * low-level driver when the AP starts or the mesh is joined.
1838 * This is for backward compatibility, userspace can also give
1839 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001840 *
1841 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001842 * whatever else is going on, so they have their own special
1843 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001844 */
1845 return !wdev ||
1846 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001847 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001848 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1849 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001850}
1851
Johannes Berg683b6d32012-11-08 21:25:48 +01001852static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1853 struct genl_info *info,
1854 struct cfg80211_chan_def *chandef)
1855{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301856 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001857
1858 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1859 return -EINVAL;
1860
1861 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1862
1863 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1865 chandef->center_freq1 = control_freq;
1866 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001867
1868 /* Primary channel not allowed */
1869 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1870 return -EINVAL;
1871
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001872 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1873 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001874
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001875 chantype = nla_get_u32(
1876 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1877
1878 switch (chantype) {
1879 case NL80211_CHAN_NO_HT:
1880 case NL80211_CHAN_HT20:
1881 case NL80211_CHAN_HT40PLUS:
1882 case NL80211_CHAN_HT40MINUS:
1883 cfg80211_chandef_create(chandef, chandef->chan,
1884 chantype);
1885 break;
1886 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001887 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001888 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001889 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1890 chandef->width =
1891 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1892 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1893 chandef->center_freq1 =
1894 nla_get_u32(
1895 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1896 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1897 chandef->center_freq2 =
1898 nla_get_u32(
1899 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1900 }
1901
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001902 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001903 return -EINVAL;
1904
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001905 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1906 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001907 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001908
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001909 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1910 chandef->width == NL80211_CHAN_WIDTH_10) &&
1911 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1912 return -EINVAL;
1913
Johannes Berg683b6d32012-11-08 21:25:48 +01001914 return 0;
1915}
1916
Johannes Bergf444de02010-05-05 15:25:02 +02001917static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1918 struct wireless_dev *wdev,
1919 struct genl_info *info)
1920{
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001922 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001923 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1924
1925 if (wdev)
1926 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001927
Johannes Bergf444de02010-05-05 15:25:02 +02001928 if (!nl80211_can_set_dev_channel(wdev))
1929 return -EOPNOTSUPP;
1930
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 result = nl80211_parse_chandef(rdev, info, &chandef);
1932 if (result)
1933 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001934
Johannes Berge8c9bd52012-06-06 08:18:22 +02001935 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 case NL80211_IFTYPE_AP:
1937 case NL80211_IFTYPE_P2P_GO:
1938 if (wdev->beacon_interval) {
1939 result = -EBUSY;
1940 break;
1941 }
Ilan Peer174e0cd2014-02-23 09:13:01 +02001942 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001943 result = -EINVAL;
1944 break;
1945 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001946 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001947 result = 0;
1948 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001949 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001950 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001951 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001952 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001953 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001954 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001955 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001956 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001957 }
Johannes Bergf444de02010-05-05 15:25:02 +02001958
1959 return result;
1960}
1961
1962static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1963{
Johannes Berg4c476992010-10-04 21:36:35 +02001964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1965 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001966
Johannes Berg4c476992010-10-04 21:36:35 +02001967 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001968}
1969
Bill Jordane8347eb2010-10-01 13:54:28 -04001970static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1971{
Johannes Berg43b19952010-10-07 13:10:30 +02001972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1973 struct net_device *dev = info->user_ptr[1];
1974 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001975 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001976
1977 if (!info->attrs[NL80211_ATTR_MAC])
1978 return -EINVAL;
1979
Johannes Berg43b19952010-10-07 13:10:30 +02001980 if (netif_running(dev))
1981 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001982
Johannes Berg43b19952010-10-07 13:10:30 +02001983 if (!rdev->ops->set_wds_peer)
1984 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001985
Johannes Berg43b19952010-10-07 13:10:30 +02001986 if (wdev->iftype != NL80211_IFTYPE_WDS)
1987 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001988
1989 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001990 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001991}
1992
1993
Johannes Berg55682962007-09-20 13:09:35 -04001994static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1995{
1996 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001997 struct net_device *netdev = NULL;
1998 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001999 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002000 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002001 u32 changed;
2002 u8 retry_short = 0, retry_long = 0;
2003 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002004 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002005
Johannes Berg5fe231e2013-05-08 21:45:15 +02002006 ASSERT_RTNL();
2007
Johannes Bergf444de02010-05-05 15:25:02 +02002008 /*
2009 * Try to find the wiphy and netdev. Normally this
2010 * function shouldn't need the netdev, but this is
2011 * done for backward compatibility -- previously
2012 * setting the channel was done per wiphy, but now
2013 * it is per netdev. Previous userland like hostapd
2014 * also passed a netdev to set_wiphy, so that it is
2015 * possible to let that go to the right netdev!
2016 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002017
Johannes Bergf444de02010-05-05 15:25:02 +02002018 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2019 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2020
Ying Xue7f2b8562014-01-15 10:23:45 +08002021 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002022 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002023 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002024 else
Johannes Bergf444de02010-05-05 15:25:02 +02002025 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002026 }
2027
Johannes Bergf444de02010-05-05 15:25:02 +02002028 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002029 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2030 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002031 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002032 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002033 wdev = NULL;
2034 netdev = NULL;
2035 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002036 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002037 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002038
2039 /*
2040 * end workaround code, by now the rdev is available
2041 * and locked, and wdev may or may not be NULL.
2042 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002043
2044 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002045 result = cfg80211_dev_rename(
2046 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002047
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002048 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002049 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Jouni Malinen31888482008-10-30 16:59:24 +02002051 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2052 struct ieee80211_txq_params txq_params;
2053 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2054
Ying Xue7f2b8562014-01-15 10:23:45 +08002055 if (!rdev->ops->set_txq_params)
2056 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002057
Ying Xue7f2b8562014-01-15 10:23:45 +08002058 if (!netdev)
2059 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002060
Johannes Berg133a3ff2011-11-03 14:50:13 +01002061 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002062 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2063 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002064
Ying Xue7f2b8562014-01-15 10:23:45 +08002065 if (!netif_running(netdev))
2066 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002067
Jouni Malinen31888482008-10-30 16:59:24 +02002068 nla_for_each_nested(nl_txq_params,
2069 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2070 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002071 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2072 nla_data(nl_txq_params),
2073 nla_len(nl_txq_params),
2074 txq_params_policy);
2075 if (result)
2076 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002077 result = parse_txq_params(tb, &txq_params);
2078 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002079 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002080
Hila Gonene35e4d22012-06-27 17:19:42 +03002081 result = rdev_set_txq_params(rdev, netdev,
2082 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002083 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002084 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002085 }
2086 }
2087
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002088 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002089 result = __nl80211_set_channel(rdev,
2090 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2091 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002092 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002094 }
2095
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002096 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002097 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002098 enum nl80211_tx_power_setting type;
2099 int idx, mbm = 0;
2100
Johannes Bergc8442112012-10-24 10:17:18 +02002101 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2102 txp_wdev = NULL;
2103
Ying Xue7f2b8562014-01-15 10:23:45 +08002104 if (!rdev->ops->set_tx_power)
2105 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002106
2107 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2108 type = nla_get_u32(info->attrs[idx]);
2109
2110 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002111 (type != NL80211_TX_POWER_AUTOMATIC))
2112 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002113
2114 if (type != NL80211_TX_POWER_AUTOMATIC) {
2115 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2116 mbm = nla_get_u32(info->attrs[idx]);
2117 }
2118
Johannes Bergc8442112012-10-24 10:17:18 +02002119 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002120 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002121 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002122 }
2123
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002124 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2125 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2126 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002127 if ((!rdev->wiphy.available_antennas_tx &&
2128 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002129 !rdev->ops->set_antenna)
2130 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002131
2132 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2133 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2134
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002135 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002136 * available antenna masks, except for the "all" mask */
2137 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002138 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2139 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002140
Bruno Randolf7f531e02010-12-16 11:30:22 +09002141 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2142 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002143
Hila Gonene35e4d22012-06-27 17:19:42 +03002144 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002145 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002146 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002147 }
2148
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002149 changed = 0;
2150
2151 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2152 retry_short = nla_get_u8(
2153 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002154 if (retry_short == 0)
2155 return -EINVAL;
2156
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002157 changed |= WIPHY_PARAM_RETRY_SHORT;
2158 }
2159
2160 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2161 retry_long = nla_get_u8(
2162 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002163 if (retry_long == 0)
2164 return -EINVAL;
2165
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002166 changed |= WIPHY_PARAM_RETRY_LONG;
2167 }
2168
2169 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2170 frag_threshold = nla_get_u32(
2171 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002172 if (frag_threshold < 256)
2173 return -EINVAL;
2174
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002175 if (frag_threshold != (u32) -1) {
2176 /*
2177 * Fragments (apart from the last one) are required to
2178 * have even length. Make the fragmentation code
2179 * simpler by stripping LSB should someone try to use
2180 * odd threshold value.
2181 */
2182 frag_threshold &= ~0x1;
2183 }
2184 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2185 }
2186
2187 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2188 rts_threshold = nla_get_u32(
2189 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2190 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2191 }
2192
Lukáš Turek81077e82009-12-21 22:50:47 +01002193 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2194 coverage_class = nla_get_u8(
2195 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2196 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2197 }
2198
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002199 if (changed) {
2200 u8 old_retry_short, old_retry_long;
2201 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002202 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002203
Ying Xue7f2b8562014-01-15 10:23:45 +08002204 if (!rdev->ops->set_wiphy_params)
2205 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002206
2207 old_retry_short = rdev->wiphy.retry_short;
2208 old_retry_long = rdev->wiphy.retry_long;
2209 old_frag_threshold = rdev->wiphy.frag_threshold;
2210 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002211 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
2213 if (changed & WIPHY_PARAM_RETRY_SHORT)
2214 rdev->wiphy.retry_short = retry_short;
2215 if (changed & WIPHY_PARAM_RETRY_LONG)
2216 rdev->wiphy.retry_long = retry_long;
2217 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2218 rdev->wiphy.frag_threshold = frag_threshold;
2219 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2220 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002221 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2222 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002223
Hila Gonene35e4d22012-06-27 17:19:42 +03002224 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002225 if (result) {
2226 rdev->wiphy.retry_short = old_retry_short;
2227 rdev->wiphy.retry_long = old_retry_long;
2228 rdev->wiphy.frag_threshold = old_frag_threshold;
2229 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002230 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002231 }
2232 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002233 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002234}
2235
Johannes Berg71bbc992012-06-15 15:30:18 +02002236static inline u64 wdev_id(struct wireless_dev *wdev)
2237{
2238 return (u64)wdev->identifier |
2239 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2240}
Johannes Berg55682962007-09-20 13:09:35 -04002241
Johannes Berg683b6d32012-11-08 21:25:48 +01002242static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002243 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002244{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002245 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002246
Johannes Berg683b6d32012-11-08 21:25:48 +01002247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2248 chandef->chan->center_freq))
2249 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002250 switch (chandef->width) {
2251 case NL80211_CHAN_WIDTH_20_NOHT:
2252 case NL80211_CHAN_WIDTH_20:
2253 case NL80211_CHAN_WIDTH_40:
2254 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2255 cfg80211_get_chandef_type(chandef)))
2256 return -ENOBUFS;
2257 break;
2258 default:
2259 break;
2260 }
2261 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2262 return -ENOBUFS;
2263 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2264 return -ENOBUFS;
2265 if (chandef->center_freq2 &&
2266 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002267 return -ENOBUFS;
2268 return 0;
2269}
2270
Eric W. Biederman15e47302012-09-07 20:12:54 +00002271static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002272 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002273 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002274{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002275 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002276 void *hdr;
2277
Eric W. Biederman15e47302012-09-07 20:12:54 +00002278 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002279 if (!hdr)
2280 return -1;
2281
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002282 if (dev &&
2283 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002284 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002285 goto nla_put_failure;
2286
2287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2288 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002289 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002290 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002291 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2292 rdev->devlist_generation ^
2293 (cfg80211_rdev_list_generation << 2)))
2294 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002295
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002296 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002297 int ret;
2298 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002299
Johannes Berg683b6d32012-11-08 21:25:48 +01002300 ret = rdev_get_channel(rdev, wdev, &chandef);
2301 if (ret == 0) {
2302 if (nl80211_send_chandef(msg, &chandef))
2303 goto nla_put_failure;
2304 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002305 }
2306
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002307 if (wdev->ssid_len) {
2308 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2309 goto nla_put_failure;
2310 }
2311
Johannes Berg55682962007-09-20 13:09:35 -04002312 return genlmsg_end(msg, hdr);
2313
2314 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002315 genlmsg_cancel(msg, hdr);
2316 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002317}
2318
2319static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2320{
2321 int wp_idx = 0;
2322 int if_idx = 0;
2323 int wp_start = cb->args[0];
2324 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002325 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002326 struct wireless_dev *wdev;
2327
Johannes Berg5fe231e2013-05-08 21:45:15 +02002328 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002329 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2330 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002331 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002332 if (wp_idx < wp_start) {
2333 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002334 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 }
Johannes Berg55682962007-09-20 13:09:35 -04002336 if_idx = 0;
2337
Johannes Berg89a54e42012-06-15 14:33:17 +02002338 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339 if (if_idx < if_start) {
2340 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002343 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002344 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002345 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002346 goto out;
2347 }
2348 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002349 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002350
2351 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002352 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002354 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002355
2356 cb->args[0] = wp_idx;
2357 cb->args[1] = if_idx;
2358
2359 return skb->len;
2360}
2361
2362static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2363{
2364 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002365 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002366 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002367
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002369 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002370 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002371
Eric W. Biederman15e47302012-09-07 20:12:54 +00002372 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002373 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002374 nlmsg_free(msg);
2375 return -ENOBUFS;
2376 }
Johannes Berg55682962007-09-20 13:09:35 -04002377
Johannes Berg134e6372009-07-10 09:51:34 +00002378 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002379}
2380
Michael Wu66f7ac52008-01-31 19:48:22 +01002381static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2382 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2383 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2384 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2385 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2386 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002387 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002388};
2389
2390static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2391{
2392 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2393 int flag;
2394
2395 *mntrflags = 0;
2396
2397 if (!nla)
2398 return -EINVAL;
2399
2400 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2401 nla, mntr_flags_policy))
2402 return -EINVAL;
2403
2404 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2405 if (flags[flag])
2406 *mntrflags |= (1<<flag);
2407
2408 return 0;
2409}
2410
Johannes Berg9bc383d2009-11-19 11:55:19 +01002411static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002412 struct net_device *netdev, u8 use_4addr,
2413 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002414{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002415 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002416 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002417 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002418 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002419 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002420
2421 switch (iftype) {
2422 case NL80211_IFTYPE_AP_VLAN:
2423 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2424 return 0;
2425 break;
2426 case NL80211_IFTYPE_STATION:
2427 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2428 return 0;
2429 break;
2430 default:
2431 break;
2432 }
2433
2434 return -EOPNOTSUPP;
2435}
2436
Johannes Berg55682962007-09-20 13:09:35 -04002437static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2438{
Johannes Berg4c476992010-10-04 21:36:35 +02002439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002440 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002441 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002443 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002444 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002445 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002446
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002447 memset(&params, 0, sizeof(params));
2448
Johannes Berg04a773a2009-04-19 21:24:32 +02002449 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002450
Johannes Berg723b0382008-09-16 20:22:09 +02002451 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002452 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002453 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002454 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002455 if (ntype > NL80211_IFTYPE_MAX)
2456 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002457 }
2458
Johannes Berg92ffe052008-09-16 20:39:36 +02002459 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002460 struct wireless_dev *wdev = dev->ieee80211_ptr;
2461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 if (ntype != NL80211_IFTYPE_MESH_POINT)
2463 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002464 if (netif_running(dev))
2465 return -EBUSY;
2466
2467 wdev_lock(wdev);
2468 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2469 IEEE80211_MAX_MESH_ID_LEN);
2470 wdev->mesh_id_up_len =
2471 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2472 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2473 wdev->mesh_id_up_len);
2474 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 }
2476
Felix Fietkau8b787642009-11-10 18:53:10 +01002477 if (info->attrs[NL80211_ATTR_4ADDR]) {
2478 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2479 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002480 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002483 } else {
2484 params.use_4addr = -1;
2485 }
2486
Johannes Berg92ffe052008-09-16 20:39:36 +02002487 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002488 if (ntype != NL80211_IFTYPE_MONITOR)
2489 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002490 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2491 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002493 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494
2495 flags = &_flags;
2496 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002497 }
Johannes Berg3b858752009-03-12 09:55:09 +01002498
Luciano Coelho18003292013-08-29 13:26:57 +03002499 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002500 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2501 return -EOPNOTSUPP;
2502
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002503 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002504 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002505 else
2506 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002507
Johannes Berg9bc383d2009-11-19 11:55:19 +01002508 if (!err && params.use_4addr != -1)
2509 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2510
Johannes Berg55682962007-09-20 13:09:35 -04002511 return err;
2512}
2513
2514static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2515{
Johannes Berg4c476992010-10-04 21:36:35 +02002516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002518 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002519 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002520 int err;
2521 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002522 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002523
Johannes Berg78f22b62014-03-24 17:57:27 +01002524 /* to avoid failing a new interface creation due to pending removal */
2525 cfg80211_destroy_ifaces(rdev);
2526
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002527 memset(&params, 0, sizeof(params));
2528
Johannes Berg55682962007-09-20 13:09:35 -04002529 if (!info->attrs[NL80211_ATTR_IFNAME])
2530 return -EINVAL;
2531
2532 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2533 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2534 if (type > NL80211_IFTYPE_MAX)
2535 return -EINVAL;
2536 }
2537
Johannes Berg79c97e92009-07-07 03:56:12 +02002538 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002539 !(rdev->wiphy.interface_modes & (1 << type)))
2540 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002541
Arend van Spriel1c18f142013-01-08 10:17:27 +01002542 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2543 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2544 ETH_ALEN);
2545 if (!is_valid_ether_addr(params.macaddr))
2546 return -EADDRNOTAVAIL;
2547 }
2548
Johannes Berg9bc383d2009-11-19 11:55:19 +01002549 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002550 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002551 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002552 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002553 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002554 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002555
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2557 if (!msg)
2558 return -ENOMEM;
2559
Michael Wu66f7ac52008-01-31 19:48:22 +01002560 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2561 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2562 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002563
Luciano Coelho18003292013-08-29 13:26:57 +03002564 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002565 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2566 return -EOPNOTSUPP;
2567
Hila Gonene35e4d22012-06-27 17:19:42 +03002568 wdev = rdev_add_virtual_intf(rdev,
2569 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2570 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002571 if (IS_ERR(wdev)) {
2572 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002573 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002574 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575
Johannes Berg78f22b62014-03-24 17:57:27 +01002576 if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
2577 wdev->owner_nlportid = info->snd_portid;
2578
Johannes Berg98104fde2012-06-16 00:19:54 +02002579 switch (type) {
2580 case NL80211_IFTYPE_MESH_POINT:
2581 if (!info->attrs[NL80211_ATTR_MESH_ID])
2582 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002583 wdev_lock(wdev);
2584 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2585 IEEE80211_MAX_MESH_ID_LEN);
2586 wdev->mesh_id_up_len =
2587 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2588 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2589 wdev->mesh_id_up_len);
2590 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002591 break;
2592 case NL80211_IFTYPE_P2P_DEVICE:
2593 /*
2594 * P2P Device doesn't have a netdev, so doesn't go
2595 * through the netdev notifier and must be added here
2596 */
2597 mutex_init(&wdev->mtx);
2598 INIT_LIST_HEAD(&wdev->event_list);
2599 spin_lock_init(&wdev->event_lock);
2600 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2601 spin_lock_init(&wdev->mgmt_registrations_lock);
2602
Johannes Berg98104fde2012-06-16 00:19:54 +02002603 wdev->identifier = ++rdev->wdev_id;
2604 list_add_rcu(&wdev->list, &rdev->wdev_list);
2605 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002606 break;
2607 default:
2608 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002609 }
2610
Eric W. Biederman15e47302012-09-07 20:12:54 +00002611 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002612 rdev, wdev) < 0) {
2613 nlmsg_free(msg);
2614 return -ENOBUFS;
2615 }
2616
2617 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002618}
2619
2620static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2621{
Johannes Berg4c476992010-10-04 21:36:35 +02002622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002623 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002624
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (!rdev->ops->del_virtual_intf)
2626 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002627
Johannes Berg84efbb82012-06-16 00:00:26 +02002628 /*
2629 * If we remove a wireless device without a netdev then clear
2630 * user_ptr[1] so that nl80211_post_doit won't dereference it
2631 * to check if it needs to do dev_put(). Otherwise it crashes
2632 * since the wdev has been freed, unlike with a netdev where
2633 * we need the dev_put() for the netdev to really be freed.
2634 */
2635 if (!wdev->netdev)
2636 info->user_ptr[1] = NULL;
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002639}
2640
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002641static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2642{
2643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2644 struct net_device *dev = info->user_ptr[1];
2645 u16 noack_map;
2646
2647 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2648 return -EINVAL;
2649
2650 if (!rdev->ops->set_noack_map)
2651 return -EOPNOTSUPP;
2652
2653 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2654
Hila Gonene35e4d22012-06-27 17:19:42 +03002655 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002656}
2657
Johannes Berg41ade002007-12-19 02:03:29 +01002658struct get_key_cookie {
2659 struct sk_buff *msg;
2660 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002661 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002662};
2663
2664static void get_key_callback(void *c, struct key_params *params)
2665{
Johannes Bergb9454e82009-07-08 13:29:08 +02002666 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002667 struct get_key_cookie *cookie = c;
2668
David S. Miller9360ffd2012-03-29 04:41:26 -04002669 if ((params->key &&
2670 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2671 params->key_len, params->key)) ||
2672 (params->seq &&
2673 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2674 params->seq_len, params->seq)) ||
2675 (params->cipher &&
2676 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2677 params->cipher)))
2678 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002679
Johannes Bergb9454e82009-07-08 13:29:08 +02002680 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2681 if (!key)
2682 goto nla_put_failure;
2683
David S. Miller9360ffd2012-03-29 04:41:26 -04002684 if ((params->key &&
2685 nla_put(cookie->msg, NL80211_KEY_DATA,
2686 params->key_len, params->key)) ||
2687 (params->seq &&
2688 nla_put(cookie->msg, NL80211_KEY_SEQ,
2689 params->seq_len, params->seq)) ||
2690 (params->cipher &&
2691 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2692 params->cipher)))
2693 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002694
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2696 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002697
2698 nla_nest_end(cookie->msg, key);
2699
Johannes Berg41ade002007-12-19 02:03:29 +01002700 return;
2701 nla_put_failure:
2702 cookie->error = 1;
2703}
2704
2705static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2706{
Johannes Berg4c476992010-10-04 21:36:35 +02002707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002708 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002709 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002710 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002711 const u8 *mac_addr = NULL;
2712 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002713 struct get_key_cookie cookie = {
2714 .error = 0,
2715 };
2716 void *hdr;
2717 struct sk_buff *msg;
2718
2719 if (info->attrs[NL80211_ATTR_KEY_IDX])
2720 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2721
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002722 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002723 return -EINVAL;
2724
2725 if (info->attrs[NL80211_ATTR_MAC])
2726 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2727
Johannes Berge31b8212010-10-05 19:39:30 +02002728 pairwise = !!mac_addr;
2729 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2730 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2731 if (kt >= NUM_NL80211_KEYTYPES)
2732 return -EINVAL;
2733 if (kt != NL80211_KEYTYPE_GROUP &&
2734 kt != NL80211_KEYTYPE_PAIRWISE)
2735 return -EINVAL;
2736 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2737 }
2738
Johannes Berg4c476992010-10-04 21:36:35 +02002739 if (!rdev->ops->get_key)
2740 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002743 if (!msg)
2744 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Eric W. Biederman15e47302012-09-07 20:12:54 +00002746 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002747 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002748 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002749 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002752 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
David S. Miller9360ffd2012-03-29 04:41:26 -04002754 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2755 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2756 goto nla_put_failure;
2757 if (mac_addr &&
2758 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2759 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002760
Johannes Berge31b8212010-10-05 19:39:30 +02002761 if (pairwise && mac_addr &&
2762 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2763 return -ENOENT;
2764
Hila Gonene35e4d22012-06-27 17:19:42 +03002765 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2766 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002767
2768 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002769 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002770
2771 if (cookie.error)
2772 goto nla_put_failure;
2773
2774 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002775 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002776
2777 nla_put_failure:
2778 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002779 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002780 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002781 return err;
2782}
2783
2784static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2785{
Johannes Berg4c476992010-10-04 21:36:35 +02002786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002787 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002788 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002789 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Bergb9454e82009-07-08 13:29:08 +02002791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
2794
2795 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002796 return -EINVAL;
2797
Johannes Bergb9454e82009-07-08 13:29:08 +02002798 /* only support setting default key */
2799 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002800 return -EINVAL;
2801
Johannes Bergfffd0932009-07-08 14:22:54 +02002802 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002803
2804 if (key.def) {
2805 if (!rdev->ops->set_default_key) {
2806 err = -EOPNOTSUPP;
2807 goto out;
2808 }
2809
2810 err = nl80211_key_allowed(dev->ieee80211_ptr);
2811 if (err)
2812 goto out;
2813
Hila Gonene35e4d22012-06-27 17:19:42 +03002814 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002815 key.def_uni, key.def_multi);
2816
2817 if (err)
2818 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002819
Johannes Berg3d23e342009-09-29 23:27:28 +02002820#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002821 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002822#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002823 } else {
2824 if (key.def_uni || !key.def_multi) {
2825 err = -EINVAL;
2826 goto out;
2827 }
2828
2829 if (!rdev->ops->set_default_mgmt_key) {
2830 err = -EOPNOTSUPP;
2831 goto out;
2832 }
2833
2834 err = nl80211_key_allowed(dev->ieee80211_ptr);
2835 if (err)
2836 goto out;
2837
Hila Gonene35e4d22012-06-27 17:19:42 +03002838 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002839 if (err)
2840 goto out;
2841
2842#ifdef CONFIG_CFG80211_WEXT
2843 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2844#endif
2845 }
2846
2847 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002848 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002849
Johannes Berg41ade002007-12-19 02:03:29 +01002850 return err;
2851}
2852
2853static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2854{
Johannes Berg4c476992010-10-04 21:36:35 +02002855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002856 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002857 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002858 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002859 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002860
Johannes Bergb9454e82009-07-08 13:29:08 +02002861 err = nl80211_parse_key(info, &key);
2862 if (err)
2863 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002864
Johannes Bergb9454e82009-07-08 13:29:08 +02002865 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002866 return -EINVAL;
2867
Johannes Berg41ade002007-12-19 02:03:29 +01002868 if (info->attrs[NL80211_ATTR_MAC])
2869 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2870
Johannes Berge31b8212010-10-05 19:39:30 +02002871 if (key.type == -1) {
2872 if (mac_addr)
2873 key.type = NL80211_KEYTYPE_PAIRWISE;
2874 else
2875 key.type = NL80211_KEYTYPE_GROUP;
2876 }
2877
2878 /* for now */
2879 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2880 key.type != NL80211_KEYTYPE_GROUP)
2881 return -EINVAL;
2882
Johannes Berg4c476992010-10-04 21:36:35 +02002883 if (!rdev->ops->add_key)
2884 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002885
Johannes Berge31b8212010-10-05 19:39:30 +02002886 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2887 key.type == NL80211_KEYTYPE_PAIRWISE,
2888 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002889 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002890
2891 wdev_lock(dev->ieee80211_ptr);
2892 err = nl80211_key_allowed(dev->ieee80211_ptr);
2893 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002894 err = rdev_add_key(rdev, dev, key.idx,
2895 key.type == NL80211_KEYTYPE_PAIRWISE,
2896 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002897 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002898
Johannes Berg41ade002007-12-19 02:03:29 +01002899 return err;
2900}
2901
2902static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2903{
Johannes Berg4c476992010-10-04 21:36:35 +02002904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002905 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002906 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002907 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002908 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002909
Johannes Bergb9454e82009-07-08 13:29:08 +02002910 err = nl80211_parse_key(info, &key);
2911 if (err)
2912 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002913
2914 if (info->attrs[NL80211_ATTR_MAC])
2915 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2916
Johannes Berge31b8212010-10-05 19:39:30 +02002917 if (key.type == -1) {
2918 if (mac_addr)
2919 key.type = NL80211_KEYTYPE_PAIRWISE;
2920 else
2921 key.type = NL80211_KEYTYPE_GROUP;
2922 }
2923
2924 /* for now */
2925 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2926 key.type != NL80211_KEYTYPE_GROUP)
2927 return -EINVAL;
2928
Johannes Berg4c476992010-10-04 21:36:35 +02002929 if (!rdev->ops->del_key)
2930 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002931
Johannes Bergfffd0932009-07-08 14:22:54 +02002932 wdev_lock(dev->ieee80211_ptr);
2933 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002934
2935 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2936 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2937 err = -ENOENT;
2938
Johannes Bergfffd0932009-07-08 14:22:54 +02002939 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002940 err = rdev_del_key(rdev, dev, key.idx,
2941 key.type == NL80211_KEYTYPE_PAIRWISE,
2942 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002943
Johannes Berg3d23e342009-09-29 23:27:28 +02002944#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002945 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002946 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002947 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002948 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002949 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2950 }
2951#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002952 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002953
Johannes Berg41ade002007-12-19 02:03:29 +01002954 return err;
2955}
2956
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302957/* This function returns an error or the number of nested attributes */
2958static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2959{
2960 struct nlattr *attr;
2961 int n_entries = 0, tmp;
2962
2963 nla_for_each_nested(attr, nl_attr, tmp) {
2964 if (nla_len(attr) != ETH_ALEN)
2965 return -EINVAL;
2966
2967 n_entries++;
2968 }
2969
2970 return n_entries;
2971}
2972
2973/*
2974 * This function parses ACL information and allocates memory for ACL data.
2975 * On successful return, the calling function is responsible to free the
2976 * ACL buffer returned by this function.
2977 */
2978static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2979 struct genl_info *info)
2980{
2981 enum nl80211_acl_policy acl_policy;
2982 struct nlattr *attr;
2983 struct cfg80211_acl_data *acl;
2984 int i = 0, n_entries, tmp;
2985
2986 if (!wiphy->max_acl_mac_addrs)
2987 return ERR_PTR(-EOPNOTSUPP);
2988
2989 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2990 return ERR_PTR(-EINVAL);
2991
2992 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2993 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2994 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2995 return ERR_PTR(-EINVAL);
2996
2997 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2998 return ERR_PTR(-EINVAL);
2999
3000 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3001 if (n_entries < 0)
3002 return ERR_PTR(n_entries);
3003
3004 if (n_entries > wiphy->max_acl_mac_addrs)
3005 return ERR_PTR(-ENOTSUPP);
3006
3007 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3008 GFP_KERNEL);
3009 if (!acl)
3010 return ERR_PTR(-ENOMEM);
3011
3012 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3013 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3014 i++;
3015 }
3016
3017 acl->n_acl_entries = n_entries;
3018 acl->acl_policy = acl_policy;
3019
3020 return acl;
3021}
3022
3023static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3024{
3025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3026 struct net_device *dev = info->user_ptr[1];
3027 struct cfg80211_acl_data *acl;
3028 int err;
3029
3030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3031 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3032 return -EOPNOTSUPP;
3033
3034 if (!dev->ieee80211_ptr->beacon_interval)
3035 return -EINVAL;
3036
3037 acl = parse_acl_data(&rdev->wiphy, info);
3038 if (IS_ERR(acl))
3039 return PTR_ERR(acl);
3040
3041 err = rdev_set_mac_acl(rdev, dev, acl);
3042
3043 kfree(acl);
3044
3045 return err;
3046}
3047
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003048static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003049 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050{
Johannes Berg88600202012-02-13 15:17:18 +01003051 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003052
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003053 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3054 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3055 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3056 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003057 return -EINVAL;
3058
Johannes Berg88600202012-02-13 15:17:18 +01003059 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3062 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3063 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003064 if (!bcn->head_len)
3065 return -EINVAL;
3066 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003067 }
3068
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003069 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3070 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3071 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003072 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003073 }
3074
Johannes Berg4c476992010-10-04 21:36:35 +02003075 if (!haveinfo)
3076 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003077
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003078 if (attrs[NL80211_ATTR_IE]) {
3079 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3080 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003081 }
3082
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003083 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003084 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003085 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003086 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003087 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003088 }
3089
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003090 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003091 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003092 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003093 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003094 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003095 }
3096
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003097 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3098 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3099 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003100 }
3101
Johannes Berg88600202012-02-13 15:17:18 +01003102 return 0;
3103}
3104
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003105static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3106 struct cfg80211_ap_settings *params)
3107{
3108 struct wireless_dev *wdev;
3109 bool ret = false;
3110
Johannes Berg89a54e42012-06-15 14:33:17 +02003111 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003112 if (wdev->iftype != NL80211_IFTYPE_AP &&
3113 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3114 continue;
3115
Johannes Berg683b6d32012-11-08 21:25:48 +01003116 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003117 continue;
3118
Johannes Berg683b6d32012-11-08 21:25:48 +01003119 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003120 ret = true;
3121 break;
3122 }
3123
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003124 return ret;
3125}
3126
Jouni Malinene39e5b52012-09-30 19:29:39 +03003127static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3128 enum nl80211_auth_type auth_type,
3129 enum nl80211_commands cmd)
3130{
3131 if (auth_type > NL80211_AUTHTYPE_MAX)
3132 return false;
3133
3134 switch (cmd) {
3135 case NL80211_CMD_AUTHENTICATE:
3136 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3137 auth_type == NL80211_AUTHTYPE_SAE)
3138 return false;
3139 return true;
3140 case NL80211_CMD_CONNECT:
3141 case NL80211_CMD_START_AP:
3142 /* SAE not supported yet */
3143 if (auth_type == NL80211_AUTHTYPE_SAE)
3144 return false;
3145 return true;
3146 default:
3147 return false;
3148 }
3149}
3150
Johannes Berg88600202012-02-13 15:17:18 +01003151static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3152{
3153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3154 struct net_device *dev = info->user_ptr[1];
3155 struct wireless_dev *wdev = dev->ieee80211_ptr;
3156 struct cfg80211_ap_settings params;
3157 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003158 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003159
3160 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3162 return -EOPNOTSUPP;
3163
3164 if (!rdev->ops->start_ap)
3165 return -EOPNOTSUPP;
3166
3167 if (wdev->beacon_interval)
3168 return -EALREADY;
3169
3170 memset(&params, 0, sizeof(params));
3171
3172 /* these are required for START_AP */
3173 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3174 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3175 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3176 return -EINVAL;
3177
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003178 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003179 if (err)
3180 return err;
3181
3182 params.beacon_interval =
3183 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3184 params.dtim_period =
3185 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3186
3187 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3188 if (err)
3189 return err;
3190
3191 /*
3192 * In theory, some of these attributes should be required here
3193 * but since they were not used when the command was originally
3194 * added, keep them optional for old user space programs to let
3195 * them continue to work with drivers that do not need the
3196 * additional information -- drivers must check!
3197 */
3198 if (info->attrs[NL80211_ATTR_SSID]) {
3199 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3200 params.ssid_len =
3201 nla_len(info->attrs[NL80211_ATTR_SSID]);
3202 if (params.ssid_len == 0 ||
3203 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3204 return -EINVAL;
3205 }
3206
3207 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3208 params.hidden_ssid = nla_get_u32(
3209 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3210 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3211 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3212 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3213 return -EINVAL;
3214 }
3215
3216 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3217
3218 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3219 params.auth_type = nla_get_u32(
3220 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003221 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3222 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003223 return -EINVAL;
3224 } else
3225 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3226
3227 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3228 NL80211_MAX_NR_CIPHER_SUITES);
3229 if (err)
3230 return err;
3231
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303232 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3233 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3234 return -EOPNOTSUPP;
3235 params.inactivity_timeout = nla_get_u16(
3236 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3237 }
3238
Johannes Berg53cabad2012-11-14 15:17:28 +01003239 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3241 return -EINVAL;
3242 params.p2p_ctwindow =
3243 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3244 if (params.p2p_ctwindow > 127)
3245 return -EINVAL;
3246 if (params.p2p_ctwindow != 0 &&
3247 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3248 return -EINVAL;
3249 }
3250
3251 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3252 u8 tmp;
3253
3254 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3255 return -EINVAL;
3256 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3257 if (tmp > 1)
3258 return -EINVAL;
3259 params.p2p_opp_ps = tmp;
3260 if (params.p2p_opp_ps != 0 &&
3261 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3262 return -EINVAL;
3263 }
3264
Johannes Bergaa430da2012-05-16 23:50:18 +02003265 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003266 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3267 if (err)
3268 return err;
3269 } else if (wdev->preset_chandef.chan) {
3270 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003271 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003272 return -EINVAL;
3273
Ilan Peer174e0cd2014-02-23 09:13:01 +02003274 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3275 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02003276 return -EINVAL;
3277
Simon Wunderlich04f39042013-02-08 18:16:19 +01003278 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3279 if (err < 0)
3280 return err;
3281 if (err) {
3282 radar_detect_width = BIT(params.chandef.width);
3283 params.radar_required = true;
3284 }
3285
Simon Wunderlich04f39042013-02-08 18:16:19 +01003286 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3287 params.chandef.chan,
3288 CHAN_MODE_SHARED,
3289 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003290 if (err)
3291 return err;
3292
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303293 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3294 params.acl = parse_acl_data(&rdev->wiphy, info);
3295 if (IS_ERR(params.acl))
3296 return PTR_ERR(params.acl);
3297 }
3298
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003299 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003300 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003301 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003302 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003303 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003304 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003305 wdev->ssid_len = params.ssid_len;
3306 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003307 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003308 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303309
3310 kfree(params.acl);
3311
Johannes Berg56d18932011-05-09 18:41:15 +02003312 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003313}
3314
Johannes Berg88600202012-02-13 15:17:18 +01003315static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3316{
3317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3318 struct net_device *dev = info->user_ptr[1];
3319 struct wireless_dev *wdev = dev->ieee80211_ptr;
3320 struct cfg80211_beacon_data params;
3321 int err;
3322
3323 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3324 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3325 return -EOPNOTSUPP;
3326
3327 if (!rdev->ops->change_beacon)
3328 return -EOPNOTSUPP;
3329
3330 if (!wdev->beacon_interval)
3331 return -EINVAL;
3332
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003333 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003334 if (err)
3335 return err;
3336
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003337 wdev_lock(wdev);
3338 err = rdev_change_beacon(rdev, dev, &params);
3339 wdev_unlock(wdev);
3340
3341 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003342}
3343
3344static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003345{
Johannes Berg4c476992010-10-04 21:36:35 +02003346 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3347 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003348
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003349 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003350}
3351
Johannes Berg5727ef12007-12-19 02:03:34 +01003352static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3353 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3354 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3355 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003356 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003357 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003358 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003359};
3360
Johannes Bergeccb8e82009-05-11 21:57:56 +03003361static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003362 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003364{
3365 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003366 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003367 int flag;
3368
Johannes Bergeccb8e82009-05-11 21:57:56 +03003369 /*
3370 * Try parsing the new attribute first so userspace
3371 * can specify both for older kernels.
3372 */
3373 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3374 if (nla) {
3375 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003376
Johannes Bergeccb8e82009-05-11 21:57:56 +03003377 sta_flags = nla_data(nla);
3378 params->sta_flags_mask = sta_flags->mask;
3379 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003380 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003381 if ((params->sta_flags_mask |
3382 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3383 return -EINVAL;
3384 return 0;
3385 }
3386
3387 /* if present, parse the old attribute */
3388
3389 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003390 if (!nla)
3391 return 0;
3392
3393 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3394 nla, sta_flags_policy))
3395 return -EINVAL;
3396
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003397 /*
3398 * Only allow certain flags for interface types so that
3399 * other attributes are silently ignored. Remember that
3400 * this is backward compatibility code with old userspace
3401 * and shouldn't be hit in other cases anyway.
3402 */
3403 switch (iftype) {
3404 case NL80211_IFTYPE_AP:
3405 case NL80211_IFTYPE_AP_VLAN:
3406 case NL80211_IFTYPE_P2P_GO:
3407 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3408 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3409 BIT(NL80211_STA_FLAG_WME) |
3410 BIT(NL80211_STA_FLAG_MFP);
3411 break;
3412 case NL80211_IFTYPE_P2P_CLIENT:
3413 case NL80211_IFTYPE_STATION:
3414 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3415 BIT(NL80211_STA_FLAG_TDLS_PEER);
3416 break;
3417 case NL80211_IFTYPE_MESH_POINT:
3418 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3419 BIT(NL80211_STA_FLAG_MFP) |
3420 BIT(NL80211_STA_FLAG_AUTHORIZED);
3421 default:
3422 return -EINVAL;
3423 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003424
Johannes Berg3383b5a2012-05-10 20:14:43 +02003425 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3426 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003427 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003428
Johannes Berg3383b5a2012-05-10 20:14:43 +02003429 /* no longer support new API additions in old API */
3430 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3431 return -EINVAL;
3432 }
3433 }
3434
Johannes Berg5727ef12007-12-19 02:03:34 +01003435 return 0;
3436}
3437
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003438static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3439 int attr)
3440{
3441 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003442 u32 bitrate;
3443 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003444
3445 rate = nla_nest_start(msg, attr);
3446 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003447 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003448
3449 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3450 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003451 /* report 16-bit bitrate only if we can */
3452 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003453 if (bitrate > 0 &&
3454 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3455 return false;
3456 if (bitrate_compat > 0 &&
3457 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3458 return false;
3459
3460 if (info->flags & RATE_INFO_FLAGS_MCS) {
3461 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3462 return false;
3463 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3464 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3465 return false;
3466 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3467 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3468 return false;
3469 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3470 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3471 return false;
3472 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3473 return false;
3474 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3475 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3476 return false;
3477 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3478 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3479 return false;
3480 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3481 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3482 return false;
3483 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3484 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3485 return false;
3486 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3487 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3488 return false;
3489 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003490
3491 nla_nest_end(msg, rate);
3492 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003493}
3494
Felix Fietkau119363c2013-04-22 16:29:30 +02003495static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3496 int id)
3497{
3498 void *attr;
3499 int i = 0;
3500
3501 if (!mask)
3502 return true;
3503
3504 attr = nla_nest_start(msg, id);
3505 if (!attr)
3506 return false;
3507
3508 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3509 if (!(mask & BIT(i)))
3510 continue;
3511
3512 if (nla_put_u8(msg, i, signal[i]))
3513 return false;
3514 }
3515
3516 nla_nest_end(msg, attr);
3517
3518 return true;
3519}
3520
Eric W. Biederman15e47302012-09-07 20:12:54 +00003521static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003522 int flags,
3523 struct cfg80211_registered_device *rdev,
3524 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003525 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003526{
3527 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003528 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003529
Eric W. Biederman15e47302012-09-07 20:12:54 +00003530 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003531 if (!hdr)
3532 return -1;
3533
David S. Miller9360ffd2012-03-29 04:41:26 -04003534 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3535 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3536 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3537 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003538
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3540 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003541 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003542 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3543 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3544 sinfo->connected_time))
3545 goto nla_put_failure;
3546 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3547 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3548 sinfo->inactive_time))
3549 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003550 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3551 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003552 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003553 (u32)sinfo->rx_bytes))
3554 goto nla_put_failure;
3555 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003556 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003557 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3558 (u32)sinfo->tx_bytes))
3559 goto nla_put_failure;
3560 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3561 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003562 sinfo->rx_bytes))
3563 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003564 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3565 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003566 sinfo->tx_bytes))
3567 goto nla_put_failure;
3568 if ((sinfo->filled & STATION_INFO_LLID) &&
3569 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3570 goto nla_put_failure;
3571 if ((sinfo->filled & STATION_INFO_PLID) &&
3572 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3573 goto nla_put_failure;
3574 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3575 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3576 sinfo->plink_state))
3577 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003578 switch (rdev->wiphy.signal_type) {
3579 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003580 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3581 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3582 sinfo->signal))
3583 goto nla_put_failure;
3584 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3585 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3586 sinfo->signal_avg))
3587 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003588 break;
3589 default:
3590 break;
3591 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003592 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3593 if (!nl80211_put_signal(msg, sinfo->chains,
3594 sinfo->chain_signal,
3595 NL80211_STA_INFO_CHAIN_SIGNAL))
3596 goto nla_put_failure;
3597 }
3598 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3599 if (!nl80211_put_signal(msg, sinfo->chains,
3600 sinfo->chain_signal_avg,
3601 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3602 goto nla_put_failure;
3603 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003604 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003605 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3606 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003607 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003608 }
3609 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3610 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3611 NL80211_STA_INFO_RX_BITRATE))
3612 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003613 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003614 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3615 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3616 sinfo->rx_packets))
3617 goto nla_put_failure;
3618 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3619 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3620 sinfo->tx_packets))
3621 goto nla_put_failure;
3622 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3623 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3624 sinfo->tx_retries))
3625 goto nla_put_failure;
3626 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3627 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3628 sinfo->tx_failed))
3629 goto nla_put_failure;
3630 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3631 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3632 sinfo->beacon_loss_count))
3633 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003634 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3635 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3636 sinfo->local_pm))
3637 goto nla_put_failure;
3638 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3639 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3640 sinfo->peer_pm))
3641 goto nla_put_failure;
3642 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3643 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3644 sinfo->nonpeer_pm))
3645 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003646 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3647 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3648 if (!bss_param)
3649 goto nla_put_failure;
3650
David S. Miller9360ffd2012-03-29 04:41:26 -04003651 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3652 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3653 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3654 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3655 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3656 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3657 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3658 sinfo->bss_param.dtim_period) ||
3659 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3660 sinfo->bss_param.beacon_interval))
3661 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003662
3663 nla_nest_end(msg, bss_param);
3664 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003665 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3666 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3667 sizeof(struct nl80211_sta_flag_update),
3668 &sinfo->sta_flags))
3669 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003670 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3671 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3672 sinfo->t_offset))
3673 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003674 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003675
David S. Miller9360ffd2012-03-29 04:41:26 -04003676 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3677 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3678 sinfo->assoc_req_ies))
3679 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003680
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003681 return genlmsg_end(msg, hdr);
3682
3683 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003684 genlmsg_cancel(msg, hdr);
3685 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003686}
3687
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003688static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003689 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003690{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003691 struct station_info sinfo;
3692 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003693 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003694 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003695 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003696 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003697
Johannes Berg97990a02013-04-19 01:02:55 +02003698 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003699 if (err)
3700 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003701
Johannes Berg97990a02013-04-19 01:02:55 +02003702 if (!wdev->netdev) {
3703 err = -EINVAL;
3704 goto out_err;
3705 }
3706
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003708 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003709 goto out_err;
3710 }
3711
Johannes Bergbba95fe2008-07-29 13:22:51 +02003712 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003713 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003714 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003715 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003716 if (err == -ENOENT)
3717 break;
3718 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003719 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003720
3721 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003722 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003723 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003724 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003725 &sinfo) < 0)
3726 goto out;
3727
3728 sta_idx++;
3729 }
3730
3731
3732 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003733 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003734 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003735 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003736 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003737
3738 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003739}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003740
Johannes Berg5727ef12007-12-19 02:03:34 +01003741static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3742{
Johannes Berg4c476992010-10-04 21:36:35 +02003743 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3744 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003745 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003746 struct sk_buff *msg;
3747 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003748 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003750 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003751
3752 if (!info->attrs[NL80211_ATTR_MAC])
3753 return -EINVAL;
3754
3755 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3756
Johannes Berg4c476992010-10-04 21:36:35 +02003757 if (!rdev->ops->get_station)
3758 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003759
Hila Gonene35e4d22012-06-27 17:19:42 +03003760 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003761 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003762 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003763
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003764 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003765 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003766 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003767
Eric W. Biederman15e47302012-09-07 20:12:54 +00003768 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003769 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003770 nlmsg_free(msg);
3771 return -ENOBUFS;
3772 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003773
Johannes Berg4c476992010-10-04 21:36:35 +02003774 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003775}
3776
Johannes Berg77ee7c82013-02-15 00:48:33 +01003777int cfg80211_check_station_change(struct wiphy *wiphy,
3778 struct station_parameters *params,
3779 enum cfg80211_station_type statype)
3780{
3781 if (params->listen_interval != -1)
3782 return -EINVAL;
3783 if (params->aid)
3784 return -EINVAL;
3785
3786 /* When you run into this, adjust the code below for the new flag */
3787 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3788
3789 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003790 case CFG80211_STA_MESH_PEER_KERNEL:
3791 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003792 /*
3793 * No ignoring the TDLS flag here -- the userspace mesh
3794 * code doesn't have the bug of including TDLS in the
3795 * mask everywhere.
3796 */
3797 if (params->sta_flags_mask &
3798 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3799 BIT(NL80211_STA_FLAG_MFP) |
3800 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3801 return -EINVAL;
3802 break;
3803 case CFG80211_STA_TDLS_PEER_SETUP:
3804 case CFG80211_STA_TDLS_PEER_ACTIVE:
3805 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3806 return -EINVAL;
3807 /* ignore since it can't change */
3808 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3809 break;
3810 default:
3811 /* disallow mesh-specific things */
3812 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3813 return -EINVAL;
3814 if (params->local_pm)
3815 return -EINVAL;
3816 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3817 return -EINVAL;
3818 }
3819
3820 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3821 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3822 /* TDLS can't be set, ... */
3823 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3824 return -EINVAL;
3825 /*
3826 * ... but don't bother the driver with it. This works around
3827 * a hostapd/wpa_supplicant issue -- it always includes the
3828 * TLDS_PEER flag in the mask even for AP mode.
3829 */
3830 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3831 }
3832
3833 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3834 /* reject other things that can't change */
3835 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3836 return -EINVAL;
3837 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3838 return -EINVAL;
3839 if (params->supported_rates)
3840 return -EINVAL;
3841 if (params->ext_capab || params->ht_capa || params->vht_capa)
3842 return -EINVAL;
3843 }
3844
3845 if (statype != CFG80211_STA_AP_CLIENT) {
3846 if (params->vlan)
3847 return -EINVAL;
3848 }
3849
3850 switch (statype) {
3851 case CFG80211_STA_AP_MLME_CLIENT:
3852 /* Use this only for authorizing/unauthorizing a station */
3853 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3854 return -EOPNOTSUPP;
3855 break;
3856 case CFG80211_STA_AP_CLIENT:
3857 /* accept only the listed bits */
3858 if (params->sta_flags_mask &
3859 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3860 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3861 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3862 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3863 BIT(NL80211_STA_FLAG_WME) |
3864 BIT(NL80211_STA_FLAG_MFP)))
3865 return -EINVAL;
3866
3867 /* but authenticated/associated only if driver handles it */
3868 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3869 params->sta_flags_mask &
3870 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3871 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3872 return -EINVAL;
3873 break;
3874 case CFG80211_STA_IBSS:
3875 case CFG80211_STA_AP_STA:
3876 /* reject any changes other than AUTHORIZED */
3877 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3878 return -EINVAL;
3879 break;
3880 case CFG80211_STA_TDLS_PEER_SETUP:
3881 /* reject any changes other than AUTHORIZED or WME */
3882 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3883 BIT(NL80211_STA_FLAG_WME)))
3884 return -EINVAL;
3885 /* force (at least) rates when authorizing */
3886 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3887 !params->supported_rates)
3888 return -EINVAL;
3889 break;
3890 case CFG80211_STA_TDLS_PEER_ACTIVE:
3891 /* reject any changes */
3892 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003893 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003894 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3895 return -EINVAL;
3896 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003897 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003898 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3899 return -EINVAL;
3900 break;
3901 }
3902
3903 return 0;
3904}
3905EXPORT_SYMBOL(cfg80211_check_station_change);
3906
Johannes Berg5727ef12007-12-19 02:03:34 +01003907/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003908 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003909 */
Johannes Berg80b99892011-11-18 16:23:01 +01003910static struct net_device *get_vlan(struct genl_info *info,
3911 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003912{
Johannes Berg463d0182009-07-14 00:33:35 +02003913 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003914 struct net_device *v;
3915 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003916
Johannes Berg80b99892011-11-18 16:23:01 +01003917 if (!vlanattr)
3918 return NULL;
3919
3920 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3921 if (!v)
3922 return ERR_PTR(-ENODEV);
3923
3924 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3925 ret = -EINVAL;
3926 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003927 }
Johannes Berg80b99892011-11-18 16:23:01 +01003928
Johannes Berg77ee7c82013-02-15 00:48:33 +01003929 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3930 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3931 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3932 ret = -EINVAL;
3933 goto error;
3934 }
3935
Johannes Berg80b99892011-11-18 16:23:01 +01003936 if (!netif_running(v)) {
3937 ret = -ENETDOWN;
3938 goto error;
3939 }
3940
3941 return v;
3942 error:
3943 dev_put(v);
3944 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003945}
3946
Johannes Berg94e860f2014-01-20 23:58:15 +01003947static const struct nla_policy
3948nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003949 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3950 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3951};
3952
Johannes Bergff276692013-02-15 00:09:01 +01003953static int nl80211_parse_sta_wme(struct genl_info *info,
3954 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003955{
Jouni Malinendf881292013-02-14 21:10:54 +02003956 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3957 struct nlattr *nla;
3958 int err;
3959
Jouni Malinendf881292013-02-14 21:10:54 +02003960 /* parse WME attributes if present */
3961 if (!info->attrs[NL80211_ATTR_STA_WME])
3962 return 0;
3963
3964 nla = info->attrs[NL80211_ATTR_STA_WME];
3965 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3966 nl80211_sta_wme_policy);
3967 if (err)
3968 return err;
3969
3970 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3971 params->uapsd_queues = nla_get_u8(
3972 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3973 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3974 return -EINVAL;
3975
3976 if (tb[NL80211_STA_WME_MAX_SP])
3977 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3978
3979 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3980 return -EINVAL;
3981
3982 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3983
3984 return 0;
3985}
3986
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303987static int nl80211_parse_sta_channel_info(struct genl_info *info,
3988 struct station_parameters *params)
3989{
3990 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3991 params->supported_channels =
3992 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3993 params->supported_channels_len =
3994 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3995 /*
3996 * Need to include at least one (first channel, number of
3997 * channels) tuple for each subband, and must have proper
3998 * tuples for the rest of the data as well.
3999 */
4000 if (params->supported_channels_len < 2)
4001 return -EINVAL;
4002 if (params->supported_channels_len % 2)
4003 return -EINVAL;
4004 }
4005
4006 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4007 params->supported_oper_classes =
4008 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4009 params->supported_oper_classes_len =
4010 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4011 /*
4012 * The value of the Length field of the Supported Operating
4013 * Classes element is between 2 and 253.
4014 */
4015 if (params->supported_oper_classes_len < 2 ||
4016 params->supported_oper_classes_len > 253)
4017 return -EINVAL;
4018 }
4019 return 0;
4020}
4021
Johannes Bergff276692013-02-15 00:09:01 +01004022static int nl80211_set_station_tdls(struct genl_info *info,
4023 struct station_parameters *params)
4024{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304025 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004026 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004027 if (info->attrs[NL80211_ATTR_PEER_AID])
4028 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004029 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4030 params->ht_capa =
4031 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4032 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4033 params->vht_capa =
4034 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4035
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304036 err = nl80211_parse_sta_channel_info(info, params);
4037 if (err)
4038 return err;
4039
Johannes Bergff276692013-02-15 00:09:01 +01004040 return nl80211_parse_sta_wme(info, params);
4041}
4042
Johannes Berg5727ef12007-12-19 02:03:34 +01004043static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4044{
Johannes Berg4c476992010-10-04 21:36:35 +02004045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004046 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004047 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004048 u8 *mac_addr;
4049 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004050
4051 memset(&params, 0, sizeof(params));
4052
4053 params.listen_interval = -1;
4054
Johannes Berg77ee7c82013-02-15 00:48:33 +01004055 if (!rdev->ops->change_station)
4056 return -EOPNOTSUPP;
4057
Johannes Berg5727ef12007-12-19 02:03:34 +01004058 if (info->attrs[NL80211_ATTR_STA_AID])
4059 return -EINVAL;
4060
4061 if (!info->attrs[NL80211_ATTR_MAC])
4062 return -EINVAL;
4063
4064 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4065
4066 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4067 params.supported_rates =
4068 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4069 params.supported_rates_len =
4070 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4071 }
4072
Jouni Malinen9d62a982013-02-14 21:10:13 +02004073 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4074 params.capability =
4075 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4076 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4077 }
4078
4079 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4080 params.ext_capab =
4081 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4082 params.ext_capab_len =
4083 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4084 }
4085
Jouni Malinendf881292013-02-14 21:10:54 +02004086 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004087 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004088
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004089 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004090 return -EINVAL;
4091
Johannes Bergf8bacc22013-02-14 23:27:01 +01004092 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004093 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004094 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4095 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4096 return -EINVAL;
4097 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004098
Johannes Bergf8bacc22013-02-14 23:27:01 +01004099 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004100 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004101 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4102 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4103 return -EINVAL;
4104 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4105 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004106
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004107 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4108 enum nl80211_mesh_power_mode pm = nla_get_u32(
4109 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4110
4111 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4112 pm > NL80211_MESH_POWER_MAX)
4113 return -EINVAL;
4114
4115 params.local_pm = pm;
4116 }
4117
Johannes Berg77ee7c82013-02-15 00:48:33 +01004118 /* Include parameters for TDLS peer (will check later) */
4119 err = nl80211_set_station_tdls(info, &params);
4120 if (err)
4121 return err;
4122
4123 params.vlan = get_vlan(info, rdev);
4124 if (IS_ERR(params.vlan))
4125 return PTR_ERR(params.vlan);
4126
Johannes Berga97f4422009-06-18 17:23:43 +02004127 switch (dev->ieee80211_ptr->iftype) {
4128 case NL80211_IFTYPE_AP:
4129 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004130 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004131 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004132 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004133 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004134 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004135 break;
4136 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004137 err = -EOPNOTSUPP;
4138 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004139 }
4140
Johannes Berg77ee7c82013-02-15 00:48:33 +01004141 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004142 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004143
Johannes Berg77ee7c82013-02-15 00:48:33 +01004144 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004145 if (params.vlan)
4146 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004147
Johannes Berg5727ef12007-12-19 02:03:34 +01004148 return err;
4149}
4150
4151static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4152{
Johannes Berg4c476992010-10-04 21:36:35 +02004153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004154 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004155 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004156 struct station_parameters params;
4157 u8 *mac_addr = NULL;
4158
4159 memset(&params, 0, sizeof(params));
4160
Johannes Berg984c3112013-02-14 23:43:25 +01004161 if (!rdev->ops->add_station)
4162 return -EOPNOTSUPP;
4163
Johannes Berg5727ef12007-12-19 02:03:34 +01004164 if (!info->attrs[NL80211_ATTR_MAC])
4165 return -EINVAL;
4166
Johannes Berg5727ef12007-12-19 02:03:34 +01004167 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4168 return -EINVAL;
4169
4170 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4171 return -EINVAL;
4172
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004173 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4174 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004175 return -EINVAL;
4176
Johannes Berg5727ef12007-12-19 02:03:34 +01004177 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4178 params.supported_rates =
4179 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4180 params.supported_rates_len =
4181 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4182 params.listen_interval =
4183 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004184
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004185 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004186 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004187 else
4188 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004189 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4190 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004191
Jouni Malinen9d62a982013-02-14 21:10:13 +02004192 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4193 params.capability =
4194 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4195 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4196 }
4197
4198 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4199 params.ext_capab =
4200 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4201 params.ext_capab_len =
4202 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4203 }
4204
Jouni Malinen36aedc92008-08-25 11:58:58 +03004205 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4206 params.ht_capa =
4207 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004208
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004209 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4210 params.vht_capa =
4211 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4212
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004213 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4214 params.opmode_notif_used = true;
4215 params.opmode_notif =
4216 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4217 }
4218
Johannes Bergf8bacc22013-02-14 23:27:01 +01004219 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004220 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004221 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4222 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4223 return -EINVAL;
4224 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004225
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304226 err = nl80211_parse_sta_channel_info(info, &params);
4227 if (err)
4228 return err;
4229
Johannes Bergff276692013-02-15 00:09:01 +01004230 err = nl80211_parse_sta_wme(info, &params);
4231 if (err)
4232 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004233
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004234 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004235 return -EINVAL;
4236
Johannes Berg77ee7c82013-02-15 00:48:33 +01004237 /* When you run into this, adjust the code below for the new flag */
4238 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4239
Johannes Bergbdd90d52011-12-14 12:20:27 +01004240 switch (dev->ieee80211_ptr->iftype) {
4241 case NL80211_IFTYPE_AP:
4242 case NL80211_IFTYPE_AP_VLAN:
4243 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004244 /* ignore WME attributes if iface/sta is not capable */
4245 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4246 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4247 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004248
Johannes Bergbdd90d52011-12-14 12:20:27 +01004249 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004250 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4251 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004252 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004253 /* but don't bother the driver with it */
4254 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004255
Johannes Bergd582cff2012-10-26 17:53:44 +02004256 /* allow authenticated/associated only if driver handles it */
4257 if (!(rdev->wiphy.features &
4258 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4259 params.sta_flags_mask &
4260 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4261 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4262 return -EINVAL;
4263
Johannes Bergbdd90d52011-12-14 12:20:27 +01004264 /* must be last in here for error handling */
4265 params.vlan = get_vlan(info, rdev);
4266 if (IS_ERR(params.vlan))
4267 return PTR_ERR(params.vlan);
4268 break;
4269 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004270 /* ignore uAPSD data */
4271 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4272
Johannes Bergd582cff2012-10-26 17:53:44 +02004273 /* associated is disallowed */
4274 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4275 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004276 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004277 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4278 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004279 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004280 break;
4281 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004282 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004283 /* ignore uAPSD data */
4284 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4285
Johannes Berg77ee7c82013-02-15 00:48:33 +01004286 /* these are disallowed */
4287 if (params.sta_flags_mask &
4288 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4289 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004290 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004291 /* Only TDLS peers can be added */
4292 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4293 return -EINVAL;
4294 /* Can only add if TDLS ... */
4295 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4296 return -EOPNOTSUPP;
4297 /* ... with external setup is supported */
4298 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4299 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004300 /*
4301 * Older wpa_supplicant versions always mark the TDLS peer
4302 * as authorized, but it shouldn't yet be.
4303 */
4304 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004305 break;
4306 default:
4307 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004308 }
4309
Johannes Bergbdd90d52011-12-14 12:20:27 +01004310 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004311
Hila Gonene35e4d22012-06-27 17:19:42 +03004312 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004313
Johannes Berg5727ef12007-12-19 02:03:34 +01004314 if (params.vlan)
4315 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004316 return err;
4317}
4318
4319static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4320{
Johannes Berg4c476992010-10-04 21:36:35 +02004321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4322 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004323 u8 *mac_addr = NULL;
4324
4325 if (info->attrs[NL80211_ATTR_MAC])
4326 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4327
Johannes Berge80cf852009-05-11 14:43:13 +02004328 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004330 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004331 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4332 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004333
Johannes Berg4c476992010-10-04 21:36:35 +02004334 if (!rdev->ops->del_station)
4335 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004336
Hila Gonene35e4d22012-06-27 17:19:42 +03004337 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004338}
4339
Eric W. Biederman15e47302012-09-07 20:12:54 +00004340static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004341 int flags, struct net_device *dev,
4342 u8 *dst, u8 *next_hop,
4343 struct mpath_info *pinfo)
4344{
4345 void *hdr;
4346 struct nlattr *pinfoattr;
4347
Eric W. Biederman15e47302012-09-07 20:12:54 +00004348 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004349 if (!hdr)
4350 return -1;
4351
David S. Miller9360ffd2012-03-29 04:41:26 -04004352 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4353 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4354 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4355 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4356 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004357
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004358 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4359 if (!pinfoattr)
4360 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004361 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4362 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4363 pinfo->frame_qlen))
4364 goto nla_put_failure;
4365 if (((pinfo->filled & MPATH_INFO_SN) &&
4366 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4367 ((pinfo->filled & MPATH_INFO_METRIC) &&
4368 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4369 pinfo->metric)) ||
4370 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4371 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4372 pinfo->exptime)) ||
4373 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4374 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4375 pinfo->flags)) ||
4376 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4377 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4378 pinfo->discovery_timeout)) ||
4379 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4380 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4381 pinfo->discovery_retries)))
4382 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004383
4384 nla_nest_end(msg, pinfoattr);
4385
4386 return genlmsg_end(msg, hdr);
4387
4388 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004389 genlmsg_cancel(msg, hdr);
4390 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004391}
4392
4393static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004394 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004395{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004396 struct mpath_info pinfo;
4397 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004398 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004399 u8 dst[ETH_ALEN];
4400 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004401 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004402 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004403
Johannes Berg97990a02013-04-19 01:02:55 +02004404 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004405 if (err)
4406 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407
4408 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004409 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004410 goto out_err;
4411 }
4412
Johannes Berg97990a02013-04-19 01:02:55 +02004413 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004414 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004415 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004416 }
4417
Johannes Bergbba95fe2008-07-29 13:22:51 +02004418 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004419 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4420 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 if (err == -ENOENT)
4422 break;
4423 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004424 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004425
Eric W. Biederman15e47302012-09-07 20:12:54 +00004426 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004427 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004428 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004429 &pinfo) < 0)
4430 goto out;
4431
4432 path_idx++;
4433 }
4434
4435
4436 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004437 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004438 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004439 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004440 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004441 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004442}
4443
4444static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4445{
Johannes Berg4c476992010-10-04 21:36:35 +02004446 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004447 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004448 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004449 struct mpath_info pinfo;
4450 struct sk_buff *msg;
4451 u8 *dst = NULL;
4452 u8 next_hop[ETH_ALEN];
4453
4454 memset(&pinfo, 0, sizeof(pinfo));
4455
4456 if (!info->attrs[NL80211_ATTR_MAC])
4457 return -EINVAL;
4458
4459 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4460
Johannes Berg4c476992010-10-04 21:36:35 +02004461 if (!rdev->ops->get_mpath)
4462 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004463
Johannes Berg4c476992010-10-04 21:36:35 +02004464 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4465 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004466
Hila Gonene35e4d22012-06-27 17:19:42 +03004467 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004468 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004469 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004472 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004473 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004474
Eric W. Biederman15e47302012-09-07 20:12:54 +00004475 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004476 dev, dst, next_hop, &pinfo) < 0) {
4477 nlmsg_free(msg);
4478 return -ENOBUFS;
4479 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004480
Johannes Berg4c476992010-10-04 21:36:35 +02004481 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004482}
4483
4484static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4485{
Johannes Berg4c476992010-10-04 21:36:35 +02004486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4487 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004488 u8 *dst = NULL;
4489 u8 *next_hop = NULL;
4490
4491 if (!info->attrs[NL80211_ATTR_MAC])
4492 return -EINVAL;
4493
4494 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4495 return -EINVAL;
4496
4497 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4498 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4499
Johannes Berg4c476992010-10-04 21:36:35 +02004500 if (!rdev->ops->change_mpath)
4501 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004502
Johannes Berg4c476992010-10-04 21:36:35 +02004503 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4504 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004505
Hila Gonene35e4d22012-06-27 17:19:42 +03004506 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004507}
Johannes Berg4c476992010-10-04 21:36:35 +02004508
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004509static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4510{
Johannes Berg4c476992010-10-04 21:36:35 +02004511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4512 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004513 u8 *dst = NULL;
4514 u8 *next_hop = NULL;
4515
4516 if (!info->attrs[NL80211_ATTR_MAC])
4517 return -EINVAL;
4518
4519 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4520 return -EINVAL;
4521
4522 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4523 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!rdev->ops->add_mpath)
4526 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004527
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4529 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004530
Hila Gonene35e4d22012-06-27 17:19:42 +03004531 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004532}
4533
4534static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4535{
Johannes Berg4c476992010-10-04 21:36:35 +02004536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4537 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004538 u8 *dst = NULL;
4539
4540 if (info->attrs[NL80211_ATTR_MAC])
4541 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4542
Johannes Berg4c476992010-10-04 21:36:35 +02004543 if (!rdev->ops->del_mpath)
4544 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004545
Hila Gonene35e4d22012-06-27 17:19:42 +03004546 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004547}
4548
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004549static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4550{
Johannes Berg4c476992010-10-04 21:36:35 +02004551 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4552 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004553 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004554 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004555 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004556
4557 memset(&params, 0, sizeof(params));
4558 /* default to not changing parameters */
4559 params.use_cts_prot = -1;
4560 params.use_short_preamble = -1;
4561 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004562 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004563 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004564 params.p2p_ctwindow = -1;
4565 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004566
4567 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4568 params.use_cts_prot =
4569 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4570 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4571 params.use_short_preamble =
4572 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4573 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4574 params.use_short_slot_time =
4575 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004576 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4577 params.basic_rates =
4578 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4579 params.basic_rates_len =
4580 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4581 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004582 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4583 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004584 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4585 params.ht_opmode =
4586 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004587
Johannes Berg53cabad2012-11-14 15:17:28 +01004588 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4589 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4590 return -EINVAL;
4591 params.p2p_ctwindow =
4592 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4593 if (params.p2p_ctwindow < 0)
4594 return -EINVAL;
4595 if (params.p2p_ctwindow != 0 &&
4596 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4597 return -EINVAL;
4598 }
4599
4600 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4601 u8 tmp;
4602
4603 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4604 return -EINVAL;
4605 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4606 if (tmp > 1)
4607 return -EINVAL;
4608 params.p2p_opp_ps = tmp;
4609 if (params.p2p_opp_ps &&
4610 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4611 return -EINVAL;
4612 }
4613
Johannes Berg4c476992010-10-04 21:36:35 +02004614 if (!rdev->ops->change_bss)
4615 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004616
Johannes Berg074ac8d2010-09-16 14:58:22 +02004617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4619 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004620
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004621 wdev_lock(wdev);
4622 err = rdev_change_bss(rdev, dev, &params);
4623 wdev_unlock(wdev);
4624
4625 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004626}
4627
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004628static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004629 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4630 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4631 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4632 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4633 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4634 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004635 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004636};
4637
4638static int parse_reg_rule(struct nlattr *tb[],
4639 struct ieee80211_reg_rule *reg_rule)
4640{
4641 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4642 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4643
4644 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4645 return -EINVAL;
4646 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4647 return -EINVAL;
4648 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4649 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004650 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4651 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004652 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4653 return -EINVAL;
4654
4655 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4656
4657 freq_range->start_freq_khz =
4658 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4659 freq_range->end_freq_khz =
4660 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004661 freq_range->max_bandwidth_khz =
4662 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004663
4664 power_rule->max_eirp =
4665 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4666
4667 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4668 power_rule->max_antenna_gain =
4669 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4670
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004671 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4672 reg_rule->dfs_cac_ms =
4673 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4674
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004675 return 0;
4676}
4677
4678static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4679{
4680 int r;
4681 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004682 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004683
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004684 /*
4685 * You should only get this when cfg80211 hasn't yet initialized
4686 * completely when built-in to the kernel right between the time
4687 * window between nl80211_init() and regulatory_init(), if that is
4688 * even possible.
4689 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004690 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004691 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004692
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004693 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4694 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004695
4696 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4697
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004698 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4699 user_reg_hint_type =
4700 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4701 else
4702 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4703
4704 switch (user_reg_hint_type) {
4705 case NL80211_USER_REG_HINT_USER:
4706 case NL80211_USER_REG_HINT_CELL_BASE:
4707 break;
4708 default:
4709 return -EINVAL;
4710 }
4711
4712 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004713
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004714 return r;
4715}
4716
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004717static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004718 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004719{
Johannes Berg4c476992010-10-04 21:36:35 +02004720 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004721 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004722 struct wireless_dev *wdev = dev->ieee80211_ptr;
4723 struct mesh_config cur_params;
4724 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004725 void *hdr;
4726 struct nlattr *pinfoattr;
4727 struct sk_buff *msg;
4728
Johannes Berg29cbe682010-12-03 09:20:44 +01004729 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4730 return -EOPNOTSUPP;
4731
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004732 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004733 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004734
Johannes Berg29cbe682010-12-03 09:20:44 +01004735 wdev_lock(wdev);
4736 /* If not connected, get default parameters */
4737 if (!wdev->mesh_id_len)
4738 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4739 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004740 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004741 wdev_unlock(wdev);
4742
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004743 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004744 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004745
4746 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004748 if (!msg)
4749 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004750 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004751 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004752 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004753 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004754 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004755 if (!pinfoattr)
4756 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004757 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4758 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4759 cur_params.dot11MeshRetryTimeout) ||
4760 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4761 cur_params.dot11MeshConfirmTimeout) ||
4762 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4763 cur_params.dot11MeshHoldingTimeout) ||
4764 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4765 cur_params.dot11MeshMaxPeerLinks) ||
4766 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4767 cur_params.dot11MeshMaxRetries) ||
4768 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4769 cur_params.dot11MeshTTL) ||
4770 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4771 cur_params.element_ttl) ||
4772 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4773 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004774 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4775 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004776 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4777 cur_params.dot11MeshHWMPmaxPREQretries) ||
4778 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4779 cur_params.path_refresh_time) ||
4780 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4781 cur_params.min_discovery_timeout) ||
4782 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4783 cur_params.dot11MeshHWMPactivePathTimeout) ||
4784 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4785 cur_params.dot11MeshHWMPpreqMinInterval) ||
4786 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4787 cur_params.dot11MeshHWMPperrMinInterval) ||
4788 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4789 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4790 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4791 cur_params.dot11MeshHWMPRootMode) ||
4792 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4793 cur_params.dot11MeshHWMPRannInterval) ||
4794 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4795 cur_params.dot11MeshGateAnnouncementProtocol) ||
4796 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4797 cur_params.dot11MeshForwarding) ||
4798 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004799 cur_params.rssi_threshold) ||
4800 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004801 cur_params.ht_opmode) ||
4802 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4803 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4804 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004805 cur_params.dot11MeshHWMProotInterval) ||
4806 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004807 cur_params.dot11MeshHWMPconfirmationInterval) ||
4808 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4809 cur_params.power_mode) ||
4810 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004811 cur_params.dot11MeshAwakeWindowDuration) ||
4812 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4813 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004814 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004815 nla_nest_end(msg, pinfoattr);
4816 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004817 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004818
Johannes Berg3b858752009-03-12 09:55:09 +01004819 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004820 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004821 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004822 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004823 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004824}
4825
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004826static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004827 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4828 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4829 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4830 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4831 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4832 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004833 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004834 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004835 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004836 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4837 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4838 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4839 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4840 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004841 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004842 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004843 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004844 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004845 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004846 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004847 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4848 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004849 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4850 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004851 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004852 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4853 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004854 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004855};
4856
Javier Cardonac80d5452010-12-16 17:37:49 -08004857static const struct nla_policy
4858 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004859 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004860 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4861 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004862 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004863 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004864 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004865 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004866 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004867 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004868};
4869
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004870static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004871 struct mesh_config *cfg,
4872 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004873{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004874 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004875 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004876
Marco Porschea54fba2013-01-07 16:04:48 +01004877#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4878do { \
4879 if (tb[attr]) { \
4880 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4881 return -EINVAL; \
4882 cfg->param = fn(tb[attr]); \
4883 mask |= (1 << (attr - 1)); \
4884 } \
4885} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004886
4887
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004888 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004889 return -EINVAL;
4890 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004891 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004892 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004893 return -EINVAL;
4894
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004895 /* This makes sure that there aren't more than 32 mesh config
4896 * parameters (otherwise our bitfield scheme would not work.) */
4897 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4898
4899 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004901 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4902 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004904 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4905 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004907 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4908 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004910 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4911 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004912 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004913 mask, NL80211_MESHCONF_MAX_RETRIES,
4914 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004916 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004918 mask, NL80211_MESHCONF_ELEMENT_TTL,
4919 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004920 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004921 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4922 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4924 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004925 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4926 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004928 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4929 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004931 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4932 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004933 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004934 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4935 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4937 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004938 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4939 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004941 1, 65535, mask,
4942 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004943 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004944 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004945 1, 65535, mask,
4946 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004947 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004948 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004949 dot11MeshHWMPnetDiameterTraversalTime,
4950 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004951 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4952 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4954 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4955 nla_get_u8);
4956 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4957 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004958 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004959 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004960 dot11MeshGateAnnouncementProtocol, 0, 1,
4961 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004962 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004963 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004964 mask, NL80211_MESHCONF_FORWARDING,
4965 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004966 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004967 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004968 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004969 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004970 mask, NL80211_MESHCONF_HT_OPMODE,
4971 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004972 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004973 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004974 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4975 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004976 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004977 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4978 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004979 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004980 dot11MeshHWMPconfirmationInterval,
4981 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004982 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4983 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004984 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4985 NL80211_MESH_POWER_ACTIVE,
4986 NL80211_MESH_POWER_MAX,
4987 mask, NL80211_MESHCONF_POWER_MODE,
4988 nla_get_u32);
4989 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4990 0, 65535, mask,
4991 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004992 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4993 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4994 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004995 if (mask_out)
4996 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004997
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004998 return 0;
4999
5000#undef FILL_IN_MESH_PARAM_IF_SET
5001}
5002
Javier Cardonac80d5452010-12-16 17:37:49 -08005003static int nl80211_parse_mesh_setup(struct genl_info *info,
5004 struct mesh_setup *setup)
5005{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005007 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5008
5009 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5010 return -EINVAL;
5011 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5012 info->attrs[NL80211_ATTR_MESH_SETUP],
5013 nl80211_mesh_setup_params_policy))
5014 return -EINVAL;
5015
Javier Cardonad299a1f2012-03-31 11:31:33 -07005016 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5017 setup->sync_method =
5018 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5019 IEEE80211_SYNC_METHOD_VENDOR :
5020 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5021
Javier Cardonac80d5452010-12-16 17:37:49 -08005022 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5023 setup->path_sel_proto =
5024 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5025 IEEE80211_PATH_PROTOCOL_VENDOR :
5026 IEEE80211_PATH_PROTOCOL_HWMP;
5027
5028 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5029 setup->path_metric =
5030 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5031 IEEE80211_PATH_METRIC_VENDOR :
5032 IEEE80211_PATH_METRIC_AIRTIME;
5033
Javier Cardona581a8b02011-04-07 15:08:27 -07005034
5035 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005036 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005037 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005038 if (!is_valid_ie_attr(ieattr))
5039 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005040 setup->ie = nla_data(ieattr);
5041 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005042 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005043 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5044 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5045 return -EINVAL;
5046 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005047 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5048 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005049 if (setup->is_secure)
5050 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005051
Colleen Twitty6e16d902013-05-08 11:45:59 -07005052 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5053 if (!setup->user_mpm)
5054 return -EINVAL;
5055 setup->auth_id =
5056 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5057 }
5058
Javier Cardonac80d5452010-12-16 17:37:49 -08005059 return 0;
5060}
5061
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005062static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005063 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005064{
5065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5066 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005067 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005068 struct mesh_config cfg;
5069 u32 mask;
5070 int err;
5071
Johannes Berg29cbe682010-12-03 09:20:44 +01005072 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5073 return -EOPNOTSUPP;
5074
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005075 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005076 return -EOPNOTSUPP;
5077
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005078 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005079 if (err)
5080 return err;
5081
Johannes Berg29cbe682010-12-03 09:20:44 +01005082 wdev_lock(wdev);
5083 if (!wdev->mesh_id_len)
5084 err = -ENOLINK;
5085
5086 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005087 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005088
5089 wdev_unlock(wdev);
5090
5091 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005092}
5093
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005094static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5095{
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005097 struct sk_buff *msg;
5098 void *hdr = NULL;
5099 struct nlattr *nl_reg_rules;
5100 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005101
5102 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005103 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005104
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005105 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005106 if (!msg)
5107 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005108
Eric W. Biederman15e47302012-09-07 20:12:54 +00005109 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005110 NL80211_CMD_GET_REG);
5111 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005112 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005113
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005114 if (reg_last_request_cell_base() &&
5115 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5116 NL80211_USER_REG_HINT_CELL_BASE))
5117 goto nla_put_failure;
5118
Johannes Berg458f4f92012-12-06 15:47:38 +01005119 rcu_read_lock();
5120 regdom = rcu_dereference(cfg80211_regdomain);
5121
5122 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5123 (regdom->dfs_region &&
5124 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5125 goto nla_put_failure_rcu;
5126
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005127 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5128 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005129 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005130
Johannes Berg458f4f92012-12-06 15:47:38 +01005131 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005132 struct nlattr *nl_reg_rule;
5133 const struct ieee80211_reg_rule *reg_rule;
5134 const struct ieee80211_freq_range *freq_range;
5135 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005136 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005137
Johannes Berg458f4f92012-12-06 15:47:38 +01005138 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005139 freq_range = &reg_rule->freq_range;
5140 power_rule = &reg_rule->power_rule;
5141
5142 nl_reg_rule = nla_nest_start(msg, i);
5143 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005144 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005145
Janusz Dziedzic97524822014-01-30 09:52:20 +01005146 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5147 if (!max_bandwidth_khz)
5148 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5149 reg_rule);
5150
David S. Miller9360ffd2012-03-29 04:41:26 -04005151 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5152 reg_rule->flags) ||
5153 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5154 freq_range->start_freq_khz) ||
5155 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5156 freq_range->end_freq_khz) ||
5157 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005158 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005159 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5160 power_rule->max_antenna_gain) ||
5161 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005162 power_rule->max_eirp) ||
5163 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5164 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005165 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005166
5167 nla_nest_end(msg, nl_reg_rule);
5168 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005169 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005170
5171 nla_nest_end(msg, nl_reg_rules);
5172
5173 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005174 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005175
Johannes Berg458f4f92012-12-06 15:47:38 +01005176nla_put_failure_rcu:
5177 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005178nla_put_failure:
5179 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005180put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005181 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005182 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005183}
5184
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005185static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5186{
5187 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5188 struct nlattr *nl_reg_rule;
5189 char *alpha2 = NULL;
5190 int rem_reg_rules = 0, r = 0;
5191 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005192 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005193 struct ieee80211_regdomain *rd = NULL;
5194
5195 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5196 return -EINVAL;
5197
5198 if (!info->attrs[NL80211_ATTR_REG_RULES])
5199 return -EINVAL;
5200
5201 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5202
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005203 if (info->attrs[NL80211_ATTR_DFS_REGION])
5204 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5205
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005206 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005207 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005208 num_rules++;
5209 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005210 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005211 }
5212
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005213 if (!reg_is_valid_request(alpha2))
5214 return -EINVAL;
5215
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005216 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005217 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005218
5219 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005220 if (!rd)
5221 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005222
5223 rd->n_reg_rules = num_rules;
5224 rd->alpha2[0] = alpha2[0];
5225 rd->alpha2[1] = alpha2[1];
5226
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005227 /*
5228 * Disable DFS master mode if the DFS region was
5229 * not supported or known on this kernel.
5230 */
5231 if (reg_supported_dfs_region(dfs_region))
5232 rd->dfs_region = dfs_region;
5233
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005234 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005235 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005236 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5237 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5238 reg_rule_policy);
5239 if (r)
5240 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005241 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5242 if (r)
5243 goto bad_reg;
5244
5245 rule_idx++;
5246
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005247 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5248 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005249 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005250 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005251 }
5252
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005253 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005254 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005255 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005256
Johannes Bergd2372b32008-10-24 20:32:20 +02005257 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005258 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005259 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005260}
5261
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005262static int validate_scan_freqs(struct nlattr *freqs)
5263{
5264 struct nlattr *attr1, *attr2;
5265 int n_channels = 0, tmp1, tmp2;
5266
5267 nla_for_each_nested(attr1, freqs, tmp1) {
5268 n_channels++;
5269 /*
5270 * Some hardware has a limited channel list for
5271 * scanning, and it is pretty much nonsensical
5272 * to scan for a channel twice, so disallow that
5273 * and don't require drivers to check that the
5274 * channel list they get isn't longer than what
5275 * they can scan, as long as they can scan all
5276 * the channels they registered at once.
5277 */
5278 nla_for_each_nested(attr2, freqs, tmp2)
5279 if (attr1 != attr2 &&
5280 nla_get_u32(attr1) == nla_get_u32(attr2))
5281 return 0;
5282 }
5283
5284 return n_channels;
5285}
5286
Johannes Berg2a519312009-02-10 21:25:55 +01005287static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5288{
Johannes Berg4c476992010-10-04 21:36:35 +02005289 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005290 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005291 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005292 struct nlattr *attr;
5293 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005294 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005295 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005296
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005297 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5298 return -EINVAL;
5299
Johannes Berg79c97e92009-07-07 03:56:12 +02005300 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005301
Johannes Berg4c476992010-10-04 21:36:35 +02005302 if (!rdev->ops->scan)
5303 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005304
Johannes Bergf9d15d12014-01-22 11:14:19 +02005305 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005306 err = -EBUSY;
5307 goto unlock;
5308 }
Johannes Berg2a519312009-02-10 21:25:55 +01005309
5310 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005311 n_channels = validate_scan_freqs(
5312 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005313 if (!n_channels) {
5314 err = -EINVAL;
5315 goto unlock;
5316 }
Johannes Berg2a519312009-02-10 21:25:55 +01005317 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005318 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005319 }
5320
5321 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5322 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5323 n_ssids++;
5324
Johannes Bergf9f47522013-03-19 15:04:07 +01005325 if (n_ssids > wiphy->max_scan_ssids) {
5326 err = -EINVAL;
5327 goto unlock;
5328 }
Johannes Berg2a519312009-02-10 21:25:55 +01005329
Jouni Malinen70692ad2009-02-16 19:39:13 +02005330 if (info->attrs[NL80211_ATTR_IE])
5331 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5332 else
5333 ie_len = 0;
5334
Johannes Bergf9f47522013-03-19 15:04:07 +01005335 if (ie_len > wiphy->max_scan_ie_len) {
5336 err = -EINVAL;
5337 goto unlock;
5338 }
Johannes Berg18a83652009-03-31 12:12:05 +02005339
Johannes Berg2a519312009-02-10 21:25:55 +01005340 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005341 + sizeof(*request->ssids) * n_ssids
5342 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005343 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005344 if (!request) {
5345 err = -ENOMEM;
5346 goto unlock;
5347 }
Johannes Berg2a519312009-02-10 21:25:55 +01005348
Johannes Berg2a519312009-02-10 21:25:55 +01005349 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005350 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005351 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005352 if (ie_len) {
5353 if (request->ssids)
5354 request->ie = (void *)(request->ssids + n_ssids);
5355 else
5356 request->ie = (void *)(request->channels + n_channels);
5357 }
Johannes Berg2a519312009-02-10 21:25:55 +01005358
Johannes Berg584991d2009-11-02 13:32:03 +01005359 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005360 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5361 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005362 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005363 struct ieee80211_channel *chan;
5364
5365 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5366
5367 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005368 err = -EINVAL;
5369 goto out_free;
5370 }
Johannes Berg584991d2009-11-02 13:32:03 +01005371
5372 /* ignore disabled channels */
5373 if (chan->flags & IEEE80211_CHAN_DISABLED)
5374 continue;
5375
5376 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005377 i++;
5378 }
5379 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005380 enum ieee80211_band band;
5381
Johannes Berg2a519312009-02-10 21:25:55 +01005382 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005383 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5384 int j;
5385 if (!wiphy->bands[band])
5386 continue;
5387 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005388 struct ieee80211_channel *chan;
5389
5390 chan = &wiphy->bands[band]->channels[j];
5391
5392 if (chan->flags & IEEE80211_CHAN_DISABLED)
5393 continue;
5394
5395 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005396 i++;
5397 }
5398 }
5399 }
5400
Johannes Berg584991d2009-11-02 13:32:03 +01005401 if (!i) {
5402 err = -EINVAL;
5403 goto out_free;
5404 }
5405
5406 request->n_channels = i;
5407
Johannes Berg2a519312009-02-10 21:25:55 +01005408 i = 0;
5409 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5410 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005411 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005412 err = -EINVAL;
5413 goto out_free;
5414 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005415 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005416 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005417 i++;
5418 }
5419 }
5420
Jouni Malinen70692ad2009-02-16 19:39:13 +02005421 if (info->attrs[NL80211_ATTR_IE]) {
5422 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a54b2009-04-01 11:58:36 +02005423 memcpy((void *)request->ie,
5424 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005425 request->ie_len);
5426 }
5427
Johannes Berg34850ab2011-07-18 18:08:35 +02005428 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005429 if (wiphy->bands[i])
5430 request->rates[i] =
5431 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005432
5433 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5434 nla_for_each_nested(attr,
5435 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5436 tmp) {
5437 enum ieee80211_band band = nla_type(attr);
5438
Dan Carpenter84404622011-07-29 11:52:18 +03005439 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005440 err = -EINVAL;
5441 goto out_free;
5442 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005443
5444 if (!wiphy->bands[band])
5445 continue;
5446
Johannes Berg34850ab2011-07-18 18:08:35 +02005447 err = ieee80211_get_ratemask(wiphy->bands[band],
5448 nla_data(attr),
5449 nla_len(attr),
5450 &request->rates[band]);
5451 if (err)
5452 goto out_free;
5453 }
5454 }
5455
Sam Leffler46856bb2012-10-11 21:03:32 -07005456 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005457 request->flags = nla_get_u32(
5458 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005459 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5460 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005461 err = -EOPNOTSUPP;
5462 goto out_free;
5463 }
5464 }
Sam Lefflered4737712012-10-11 21:03:31 -07005465
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305466 request->no_cck =
5467 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5468
Johannes Bergfd014282012-06-18 19:17:03 +02005469 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005470 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005471 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005472
Johannes Berg79c97e92009-07-07 03:56:12 +02005473 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005474 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005475
Johannes Berg463d0182009-07-14 00:33:35 +02005476 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005477 nl80211_send_scan_start(rdev, wdev);
5478 if (wdev->netdev)
5479 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005480 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005481 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005482 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005483 kfree(request);
5484 }
Johannes Berg3b858752009-03-12 09:55:09 +01005485
Johannes Bergf9f47522013-03-19 15:04:07 +01005486 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005487 return err;
5488}
5489
Luciano Coelho807f8a82011-05-11 17:09:35 +03005490static int nl80211_start_sched_scan(struct sk_buff *skb,
5491 struct genl_info *info)
5492{
5493 struct cfg80211_sched_scan_request *request;
5494 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5495 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005496 struct nlattr *attr;
5497 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005498 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005499 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005500 enum ieee80211_band band;
5501 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005502 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005503 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005504
5505 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5506 !rdev->ops->sched_scan_start)
5507 return -EOPNOTSUPP;
5508
5509 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5510 return -EINVAL;
5511
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005512 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5513 return -EINVAL;
5514
5515 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5516 if (interval == 0)
5517 return -EINVAL;
5518
Luciano Coelho807f8a82011-05-11 17:09:35 +03005519 wiphy = &rdev->wiphy;
5520
5521 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5522 n_channels = validate_scan_freqs(
5523 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5524 if (!n_channels)
5525 return -EINVAL;
5526 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005527 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005528 }
5529
5530 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5531 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5532 tmp)
5533 n_ssids++;
5534
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005535 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005536 return -EINVAL;
5537
Johannes Bergea73cbc2014-01-24 10:53:53 +01005538 /*
5539 * First, count the number of 'real' matchsets. Due to an issue with
5540 * the old implementation, matchsets containing only the RSSI attribute
5541 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5542 * RSSI for all matchsets, rather than their own matchset for reporting
5543 * all APs with a strong RSSI. This is needed to be compatible with
5544 * older userspace that treated a matchset with only the RSSI as the
5545 * global RSSI for all other matchsets - if there are other matchsets.
5546 */
5547 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005548 nla_for_each_nested(attr,
5549 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005550 tmp) {
5551 struct nlattr *rssi;
5552
5553 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5554 nla_data(attr), nla_len(attr),
5555 nl80211_match_policy);
5556 if (err)
5557 return err;
5558 /* add other standalone attributes here */
5559 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5560 n_match_sets++;
5561 continue;
5562 }
5563 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5564 if (rssi)
5565 default_match_rssi = nla_get_s32(rssi);
5566 }
5567 }
5568
5569 /* However, if there's no other matchset, add the RSSI one */
5570 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5571 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005572
5573 if (n_match_sets > wiphy->max_match_sets)
5574 return -EINVAL;
5575
Luciano Coelho807f8a82011-05-11 17:09:35 +03005576 if (info->attrs[NL80211_ATTR_IE])
5577 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5578 else
5579 ie_len = 0;
5580
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005581 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005582 return -EINVAL;
5583
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005584 if (rdev->sched_scan_req) {
5585 err = -EINPROGRESS;
5586 goto out;
5587 }
5588
Luciano Coelho807f8a82011-05-11 17:09:35 +03005589 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005590 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005591 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005592 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005593 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005594 if (!request) {
5595 err = -ENOMEM;
5596 goto out;
5597 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005598
5599 if (n_ssids)
5600 request->ssids = (void *)&request->channels[n_channels];
5601 request->n_ssids = n_ssids;
5602 if (ie_len) {
5603 if (request->ssids)
5604 request->ie = (void *)(request->ssids + n_ssids);
5605 else
5606 request->ie = (void *)(request->channels + n_channels);
5607 }
5608
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005609 if (n_match_sets) {
5610 if (request->ie)
5611 request->match_sets = (void *)(request->ie + ie_len);
5612 else if (request->ssids)
5613 request->match_sets =
5614 (void *)(request->ssids + n_ssids);
5615 else
5616 request->match_sets =
5617 (void *)(request->channels + n_channels);
5618 }
5619 request->n_match_sets = n_match_sets;
5620
Luciano Coelho807f8a82011-05-11 17:09:35 +03005621 i = 0;
5622 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5623 /* user specified, bail out if channel not found */
5624 nla_for_each_nested(attr,
5625 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5626 tmp) {
5627 struct ieee80211_channel *chan;
5628
5629 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5630
5631 if (!chan) {
5632 err = -EINVAL;
5633 goto out_free;
5634 }
5635
5636 /* ignore disabled channels */
5637 if (chan->flags & IEEE80211_CHAN_DISABLED)
5638 continue;
5639
5640 request->channels[i] = chan;
5641 i++;
5642 }
5643 } else {
5644 /* all channels */
5645 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5646 int j;
5647 if (!wiphy->bands[band])
5648 continue;
5649 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5650 struct ieee80211_channel *chan;
5651
5652 chan = &wiphy->bands[band]->channels[j];
5653
5654 if (chan->flags & IEEE80211_CHAN_DISABLED)
5655 continue;
5656
5657 request->channels[i] = chan;
5658 i++;
5659 }
5660 }
5661 }
5662
5663 if (!i) {
5664 err = -EINVAL;
5665 goto out_free;
5666 }
5667
5668 request->n_channels = i;
5669
5670 i = 0;
5671 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5672 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5673 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005674 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005675 err = -EINVAL;
5676 goto out_free;
5677 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005678 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005679 memcpy(request->ssids[i].ssid, nla_data(attr),
5680 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005681 i++;
5682 }
5683 }
5684
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005685 i = 0;
5686 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5687 nla_for_each_nested(attr,
5688 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5689 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005690 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005691
Johannes Bergae811e22014-01-24 10:17:47 +01005692 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5693 nla_data(attr), nla_len(attr),
5694 nl80211_match_policy);
5695 if (err)
5696 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005697 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005698 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005699 if (WARN_ON(i >= n_match_sets)) {
5700 /* this indicates a programming error,
5701 * the loop above should have verified
5702 * things properly
5703 */
5704 err = -EINVAL;
5705 goto out_free;
5706 }
5707
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005708 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5709 err = -EINVAL;
5710 goto out_free;
5711 }
5712 memcpy(request->match_sets[i].ssid.ssid,
5713 nla_data(ssid), nla_len(ssid));
5714 request->match_sets[i].ssid.ssid_len =
5715 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005716 /* special attribute - old implemenation w/a */
5717 request->match_sets[i].rssi_thold =
5718 default_match_rssi;
5719 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5720 if (rssi)
5721 request->match_sets[i].rssi_thold =
5722 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005723 }
5724 i++;
5725 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005726
5727 /* there was no other matchset, so the RSSI one is alone */
5728 if (i == 0)
5729 request->match_sets[0].rssi_thold = default_match_rssi;
5730
5731 request->min_rssi_thold = INT_MAX;
5732 for (i = 0; i < n_match_sets; i++)
5733 request->min_rssi_thold =
5734 min(request->match_sets[i].rssi_thold,
5735 request->min_rssi_thold);
5736 } else {
5737 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005738 }
5739
Johannes Berg9900e482014-02-04 21:01:25 +01005740 if (ie_len) {
5741 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005742 memcpy((void *)request->ie,
5743 nla_data(info->attrs[NL80211_ATTR_IE]),
5744 request->ie_len);
5745 }
5746
Sam Leffler46856bb2012-10-11 21:03:32 -07005747 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005748 request->flags = nla_get_u32(
5749 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005750 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5751 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005752 err = -EOPNOTSUPP;
5753 goto out_free;
5754 }
5755 }
Sam Lefflered4737712012-10-11 21:03:31 -07005756
Luciano Coelho807f8a82011-05-11 17:09:35 +03005757 request->dev = dev;
5758 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005759 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005760 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005761
Hila Gonene35e4d22012-06-27 17:19:42 +03005762 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005763 if (!err) {
5764 rdev->sched_scan_req = request;
5765 nl80211_send_sched_scan(rdev, dev,
5766 NL80211_CMD_START_SCHED_SCAN);
5767 goto out;
5768 }
5769
5770out_free:
5771 kfree(request);
5772out:
5773 return err;
5774}
5775
5776static int nl80211_stop_sched_scan(struct sk_buff *skb,
5777 struct genl_info *info)
5778{
5779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5780
5781 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5782 !rdev->ops->sched_scan_stop)
5783 return -EOPNOTSUPP;
5784
Johannes Berg5fe231e2013-05-08 21:45:15 +02005785 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005786}
5787
Simon Wunderlich04f39042013-02-08 18:16:19 +01005788static int nl80211_start_radar_detection(struct sk_buff *skb,
5789 struct genl_info *info)
5790{
5791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5792 struct net_device *dev = info->user_ptr[1];
5793 struct wireless_dev *wdev = dev->ieee80211_ptr;
5794 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005795 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005796 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005797 int err;
5798
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005799 dfs_region = reg_get_dfs_region(wdev->wiphy);
5800 if (dfs_region == NL80211_DFS_UNSET)
5801 return -EINVAL;
5802
Simon Wunderlich04f39042013-02-08 18:16:19 +01005803 err = nl80211_parse_chandef(rdev, info, &chandef);
5804 if (err)
5805 return err;
5806
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005807 if (netif_carrier_ok(dev))
5808 return -EBUSY;
5809
Simon Wunderlich04f39042013-02-08 18:16:19 +01005810 if (wdev->cac_started)
5811 return -EBUSY;
5812
5813 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5814 if (err < 0)
5815 return err;
5816
5817 if (err == 0)
5818 return -EINVAL;
5819
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005820 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005821 return -EINVAL;
5822
5823 if (!rdev->ops->start_radar_detection)
5824 return -EOPNOTSUPP;
5825
Simon Wunderlich04f39042013-02-08 18:16:19 +01005826 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5827 chandef.chan, CHAN_MODE_SHARED,
5828 BIT(chandef.width));
5829 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005830 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005831
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005832 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5833 if (WARN_ON(!cac_time_ms))
5834 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5835
5836 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5837 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005838 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005839 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005840 wdev->cac_started = true;
5841 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005842 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005843 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005844 return err;
5845}
5846
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005847static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5848{
5849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5850 struct net_device *dev = info->user_ptr[1];
5851 struct wireless_dev *wdev = dev->ieee80211_ptr;
5852 struct cfg80211_csa_settings params;
5853 /* csa_attrs is defined static to avoid waste of stack size - this
5854 * function is called under RTNL lock, so this should not be a problem.
5855 */
5856 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5857 u8 radar_detect_width = 0;
5858 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005859 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005860
5861 if (!rdev->ops->channel_switch ||
5862 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5863 return -EOPNOTSUPP;
5864
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005865 switch (dev->ieee80211_ptr->iftype) {
5866 case NL80211_IFTYPE_AP:
5867 case NL80211_IFTYPE_P2P_GO:
5868 need_new_beacon = true;
5869
5870 /* useless if AP is not running */
5871 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005872 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005873 break;
5874 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005875 if (!wdev->ssid_len)
5876 return -ENOTCONN;
5877 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005878 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005879 if (!wdev->mesh_id_len)
5880 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005881 break;
5882 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005883 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005884 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005885
5886 memset(&params, 0, sizeof(params));
5887
5888 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5889 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5890 return -EINVAL;
5891
5892 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005893 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005894 return -EINVAL;
5895
5896 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5897
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005898 if (!need_new_beacon)
5899 goto skip_beacons;
5900
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005901 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5902 if (err)
5903 return err;
5904
5905 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5906 info->attrs[NL80211_ATTR_CSA_IES],
5907 nl80211_policy);
5908 if (err)
5909 return err;
5910
5911 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5912 if (err)
5913 return err;
5914
5915 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5916 return -EINVAL;
5917
5918 params.counter_offset_beacon =
5919 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5920 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5921 return -EINVAL;
5922
5923 /* sanity check - counters should be the same */
5924 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5925 params.count)
5926 return -EINVAL;
5927
5928 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5929 params.counter_offset_presp =
5930 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5931 if (params.counter_offset_presp >=
5932 params.beacon_csa.probe_resp_len)
5933 return -EINVAL;
5934
5935 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5936 params.count)
5937 return -EINVAL;
5938 }
5939
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005940skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005941 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5942 if (err)
5943 return err;
5944
Ilan Peer174e0cd2014-02-23 09:13:01 +02005945 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
5946 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005947 return -EINVAL;
5948
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005949 switch (dev->ieee80211_ptr->iftype) {
5950 case NL80211_IFTYPE_AP:
5951 case NL80211_IFTYPE_P2P_GO:
5952 case NL80211_IFTYPE_ADHOC:
5953 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005954 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5955 &params.chandef);
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005956 if (err < 0)
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005957 return err;
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005958 if (err) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005959 radar_detect_width = BIT(params.chandef.width);
5960 params.radar_required = true;
5961 }
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005962 break;
5963 default:
5964 break;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005965 }
5966
5967 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5968 params.chandef.chan,
5969 CHAN_MODE_SHARED,
5970 radar_detect_width);
5971 if (err)
5972 return err;
5973
5974 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5975 params.block_tx = true;
5976
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005977 wdev_lock(wdev);
5978 err = rdev_channel_switch(rdev, dev, &params);
5979 wdev_unlock(wdev);
5980
5981 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005982}
5983
Johannes Berg9720bb32011-06-21 09:45:33 +02005984static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5985 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005986 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005987 struct wireless_dev *wdev,
5988 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005989{
Johannes Berg48ab9052009-07-10 18:42:31 +02005990 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005991 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005992 void *hdr;
5993 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005994 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005995
5996 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005997
Eric W. Biederman15e47302012-09-07 20:12:54 +00005998 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005999 NL80211_CMD_NEW_SCAN_RESULTS);
6000 if (!hdr)
6001 return -1;
6002
Johannes Berg9720bb32011-06-21 09:45:33 +02006003 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6004
Johannes Berg97990a02013-04-19 01:02:55 +02006005 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6006 goto nla_put_failure;
6007 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006008 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6009 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006010 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6011 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006012
6013 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6014 if (!bss)
6015 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006016 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006017 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006018 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006019
6020 rcu_read_lock();
6021 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006022 if (ies) {
6023 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6024 goto fail_unlock_rcu;
6025 tsf = true;
6026 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6027 ies->len, ies->data))
6028 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006029 }
6030 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006031 if (ies) {
6032 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6033 goto fail_unlock_rcu;
6034 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6035 ies->len, ies->data))
6036 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006037 }
6038 rcu_read_unlock();
6039
David S. Miller9360ffd2012-03-29 04:41:26 -04006040 if (res->beacon_interval &&
6041 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6042 goto nla_put_failure;
6043 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6044 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006045 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006046 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6047 jiffies_to_msecs(jiffies - intbss->ts)))
6048 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006049
Johannes Berg77965c972009-02-18 18:45:06 +01006050 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006051 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006052 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6053 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006054 break;
6055 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006056 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6057 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006058 break;
6059 default:
6060 break;
6061 }
6062
Johannes Berg48ab9052009-07-10 18:42:31 +02006063 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006064 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006065 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006066 if (intbss == wdev->current_bss &&
6067 nla_put_u32(msg, NL80211_BSS_STATUS,
6068 NL80211_BSS_STATUS_ASSOCIATED))
6069 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006070 break;
6071 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006072 if (intbss == wdev->current_bss &&
6073 nla_put_u32(msg, NL80211_BSS_STATUS,
6074 NL80211_BSS_STATUS_IBSS_JOINED))
6075 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006076 break;
6077 default:
6078 break;
6079 }
6080
Johannes Berg2a519312009-02-10 21:25:55 +01006081 nla_nest_end(msg, bss);
6082
6083 return genlmsg_end(msg, hdr);
6084
Johannes Berg8cef2c92013-02-05 16:54:31 +01006085 fail_unlock_rcu:
6086 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006087 nla_put_failure:
6088 genlmsg_cancel(msg, hdr);
6089 return -EMSGSIZE;
6090}
6091
Johannes Berg97990a02013-04-19 01:02:55 +02006092static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006093{
Johannes Berg48ab9052009-07-10 18:42:31 +02006094 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006095 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006096 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006097 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006098 int err;
6099
Johannes Berg97990a02013-04-19 01:02:55 +02006100 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006101 if (err)
6102 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006103
Johannes Berg48ab9052009-07-10 18:42:31 +02006104 wdev_lock(wdev);
6105 spin_lock_bh(&rdev->bss_lock);
6106 cfg80211_bss_expire(rdev);
6107
Johannes Berg9720bb32011-06-21 09:45:33 +02006108 cb->seq = rdev->bss_generation;
6109
Johannes Berg48ab9052009-07-10 18:42:31 +02006110 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006111 if (++idx <= start)
6112 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006113 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006114 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006115 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006116 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006117 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006118 }
6119 }
6120
Johannes Berg48ab9052009-07-10 18:42:31 +02006121 spin_unlock_bh(&rdev->bss_lock);
6122 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006123
Johannes Berg97990a02013-04-19 01:02:55 +02006124 cb->args[2] = idx;
6125 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006126
Johannes Berg67748892010-10-04 21:14:06 +02006127 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006128}
6129
Eric W. Biederman15e47302012-09-07 20:12:54 +00006130static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006131 int flags, struct net_device *dev,
6132 struct survey_info *survey)
6133{
6134 void *hdr;
6135 struct nlattr *infoattr;
6136
Eric W. Biederman15e47302012-09-07 20:12:54 +00006137 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006138 NL80211_CMD_NEW_SURVEY_RESULTS);
6139 if (!hdr)
6140 return -ENOMEM;
6141
David S. Miller9360ffd2012-03-29 04:41:26 -04006142 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6143 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006144
6145 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6146 if (!infoattr)
6147 goto nla_put_failure;
6148
David S. Miller9360ffd2012-03-29 04:41:26 -04006149 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6150 survey->channel->center_freq))
6151 goto nla_put_failure;
6152
6153 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6154 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6155 goto nla_put_failure;
6156 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6157 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6158 goto nla_put_failure;
6159 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6160 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6161 survey->channel_time))
6162 goto nla_put_failure;
6163 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6164 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6165 survey->channel_time_busy))
6166 goto nla_put_failure;
6167 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6168 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6169 survey->channel_time_ext_busy))
6170 goto nla_put_failure;
6171 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6172 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6173 survey->channel_time_rx))
6174 goto nla_put_failure;
6175 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6176 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6177 survey->channel_time_tx))
6178 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006179
6180 nla_nest_end(msg, infoattr);
6181
6182 return genlmsg_end(msg, hdr);
6183
6184 nla_put_failure:
6185 genlmsg_cancel(msg, hdr);
6186 return -EMSGSIZE;
6187}
6188
6189static int nl80211_dump_survey(struct sk_buff *skb,
6190 struct netlink_callback *cb)
6191{
6192 struct survey_info survey;
6193 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006194 struct wireless_dev *wdev;
6195 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006196 int res;
6197
Johannes Berg97990a02013-04-19 01:02:55 +02006198 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006199 if (res)
6200 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006201
Johannes Berg97990a02013-04-19 01:02:55 +02006202 if (!wdev->netdev) {
6203 res = -EINVAL;
6204 goto out_err;
6205 }
6206
Holger Schurig61fa7132009-11-11 12:25:40 +01006207 if (!dev->ops->dump_survey) {
6208 res = -EOPNOTSUPP;
6209 goto out_err;
6210 }
6211
6212 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006213 struct ieee80211_channel *chan;
6214
Johannes Berg97990a02013-04-19 01:02:55 +02006215 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006216 if (res == -ENOENT)
6217 break;
6218 if (res)
6219 goto out_err;
6220
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006221 /* Survey without a channel doesn't make sense */
6222 if (!survey.channel) {
6223 res = -EINVAL;
6224 goto out;
6225 }
6226
6227 chan = ieee80211_get_channel(&dev->wiphy,
6228 survey.channel->center_freq);
6229 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6230 survey_idx++;
6231 continue;
6232 }
6233
Holger Schurig61fa7132009-11-11 12:25:40 +01006234 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006235 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006236 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006237 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006238 goto out;
6239 survey_idx++;
6240 }
6241
6242 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006243 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006244 res = skb->len;
6245 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006246 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006247 return res;
6248}
6249
Samuel Ortizb23aa672009-07-01 21:26:54 +02006250static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6251{
6252 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6253 NL80211_WPA_VERSION_2));
6254}
6255
Jouni Malinen636a5d32009-03-19 13:39:22 +02006256static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6257{
Johannes Berg4c476992010-10-04 21:36:35 +02006258 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6259 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006260 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006261 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6262 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006263 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006264 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006265 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006266
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006267 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6268 return -EINVAL;
6269
6270 if (!info->attrs[NL80211_ATTR_MAC])
6271 return -EINVAL;
6272
Jouni Malinen17780922009-03-27 20:52:47 +02006273 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6274 return -EINVAL;
6275
Johannes Berg19957bb2009-07-02 17:20:43 +02006276 if (!info->attrs[NL80211_ATTR_SSID])
6277 return -EINVAL;
6278
6279 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6280 return -EINVAL;
6281
Johannes Bergfffd0932009-07-08 14:22:54 +02006282 err = nl80211_parse_key(info, &key);
6283 if (err)
6284 return err;
6285
6286 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006287 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6288 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006289 if (!key.p.key || !key.p.key_len)
6290 return -EINVAL;
6291 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6292 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6293 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6294 key.p.key_len != WLAN_KEY_LEN_WEP104))
6295 return -EINVAL;
6296 if (key.idx > 4)
6297 return -EINVAL;
6298 } else {
6299 key.p.key_len = 0;
6300 key.p.key = NULL;
6301 }
6302
Johannes Bergafea0b72010-08-10 09:46:42 +02006303 if (key.idx >= 0) {
6304 int i;
6305 bool ok = false;
6306 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6307 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6308 ok = true;
6309 break;
6310 }
6311 }
Johannes Berg4c476992010-10-04 21:36:35 +02006312 if (!ok)
6313 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006314 }
6315
Johannes Berg4c476992010-10-04 21:36:35 +02006316 if (!rdev->ops->auth)
6317 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006318
Johannes Berg074ac8d2010-09-16 14:58:22 +02006319 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006320 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6321 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006322
Johannes Berg19957bb2009-07-02 17:20:43 +02006323 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006324 chan = nl80211_get_valid_chan(&rdev->wiphy,
6325 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6326 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006327 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006328
Johannes Berg19957bb2009-07-02 17:20:43 +02006329 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6330 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6331
6332 if (info->attrs[NL80211_ATTR_IE]) {
6333 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6334 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6335 }
6336
6337 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006338 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006339 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006340
Jouni Malinene39e5b52012-09-30 19:29:39 +03006341 if (auth_type == NL80211_AUTHTYPE_SAE &&
6342 !info->attrs[NL80211_ATTR_SAE_DATA])
6343 return -EINVAL;
6344
6345 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6346 if (auth_type != NL80211_AUTHTYPE_SAE)
6347 return -EINVAL;
6348 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6349 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6350 /* need to include at least Auth Transaction and Status Code */
6351 if (sae_data_len < 4)
6352 return -EINVAL;
6353 }
6354
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006355 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6356
Johannes Berg95de8172012-01-20 13:55:25 +01006357 /*
6358 * Since we no longer track auth state, ignore
6359 * requests to only change local state.
6360 */
6361 if (local_state_change)
6362 return 0;
6363
Johannes Berg91bf9b22013-05-15 17:44:01 +02006364 wdev_lock(dev->ieee80211_ptr);
6365 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6366 ssid, ssid_len, ie, ie_len,
6367 key.p.key, key.p.key_len, key.idx,
6368 sae_data, sae_data_len);
6369 wdev_unlock(dev->ieee80211_ptr);
6370 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006371}
6372
Johannes Bergc0692b82010-08-27 14:26:53 +03006373static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6374 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006375 struct cfg80211_crypto_settings *settings,
6376 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006377{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006378 memset(settings, 0, sizeof(*settings));
6379
Samuel Ortizb23aa672009-07-01 21:26:54 +02006380 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6381
Johannes Bergc0692b82010-08-27 14:26:53 +03006382 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6383 u16 proto;
6384 proto = nla_get_u16(
6385 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6386 settings->control_port_ethertype = cpu_to_be16(proto);
6387 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6388 proto != ETH_P_PAE)
6389 return -EINVAL;
6390 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6391 settings->control_port_no_encrypt = true;
6392 } else
6393 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6394
Samuel Ortizb23aa672009-07-01 21:26:54 +02006395 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6396 void *data;
6397 int len, i;
6398
6399 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6400 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6401 settings->n_ciphers_pairwise = len / sizeof(u32);
6402
6403 if (len % sizeof(u32))
6404 return -EINVAL;
6405
Johannes Berg3dc27d22009-07-02 21:36:37 +02006406 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006407 return -EINVAL;
6408
6409 memcpy(settings->ciphers_pairwise, data, len);
6410
6411 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006412 if (!cfg80211_supported_cipher_suite(
6413 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006414 settings->ciphers_pairwise[i]))
6415 return -EINVAL;
6416 }
6417
6418 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6419 settings->cipher_group =
6420 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006421 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6422 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006423 return -EINVAL;
6424 }
6425
6426 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6427 settings->wpa_versions =
6428 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6429 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6430 return -EINVAL;
6431 }
6432
6433 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6434 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006435 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006436
6437 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6438 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6439 settings->n_akm_suites = len / sizeof(u32);
6440
6441 if (len % sizeof(u32))
6442 return -EINVAL;
6443
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006444 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6445 return -EINVAL;
6446
Samuel Ortizb23aa672009-07-01 21:26:54 +02006447 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006448 }
6449
6450 return 0;
6451}
6452
Jouni Malinen636a5d32009-03-19 13:39:22 +02006453static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6454{
Johannes Berg4c476992010-10-04 21:36:35 +02006455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6456 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006457 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006458 struct cfg80211_assoc_request req = {};
6459 const u8 *bssid, *ssid;
6460 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006461
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006462 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6463 return -EINVAL;
6464
6465 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006466 !info->attrs[NL80211_ATTR_SSID] ||
6467 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006468 return -EINVAL;
6469
Johannes Berg4c476992010-10-04 21:36:35 +02006470 if (!rdev->ops->assoc)
6471 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006472
Johannes Berg074ac8d2010-09-16 14:58:22 +02006473 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006474 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6475 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006476
Johannes Berg19957bb2009-07-02 17:20:43 +02006477 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006478
Jouni Malinen664834d2014-01-15 00:01:44 +02006479 chan = nl80211_get_valid_chan(&rdev->wiphy,
6480 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6481 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006482 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006483
Johannes Berg19957bb2009-07-02 17:20:43 +02006484 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6485 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006486
6487 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006488 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6489 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006490 }
6491
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006492 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006493 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006494 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006495 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006496 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006497 else if (mfp != NL80211_MFP_NO)
6498 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006499 }
6500
Johannes Berg3e5d7642009-07-07 14:37:26 +02006501 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006502 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006503
Ben Greear7e7c8922011-11-18 11:31:59 -08006504 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006505 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006506
6507 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006508 memcpy(&req.ht_capa_mask,
6509 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6510 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006511
6512 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006513 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006514 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006515 memcpy(&req.ht_capa,
6516 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6517 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006518 }
6519
Johannes Bergee2aca32013-02-21 17:36:01 +01006520 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006521 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006522
6523 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006524 memcpy(&req.vht_capa_mask,
6525 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6526 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006527
6528 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006529 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006530 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006531 memcpy(&req.vht_capa,
6532 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6533 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006534 }
6535
Johannes Bergf62fab72013-02-21 20:09:09 +01006536 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006537 if (!err) {
6538 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006539 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6540 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006541 wdev_unlock(dev->ieee80211_ptr);
6542 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006543
Jouni Malinen636a5d32009-03-19 13:39:22 +02006544 return err;
6545}
6546
6547static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6548{
Johannes Berg4c476992010-10-04 21:36:35 +02006549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6550 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006551 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006552 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006553 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006554 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006555
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006556 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6557 return -EINVAL;
6558
6559 if (!info->attrs[NL80211_ATTR_MAC])
6560 return -EINVAL;
6561
6562 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6563 return -EINVAL;
6564
Johannes Berg4c476992010-10-04 21:36:35 +02006565 if (!rdev->ops->deauth)
6566 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006567
Johannes Berg074ac8d2010-09-16 14:58:22 +02006568 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006569 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6570 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006571
Johannes Berg19957bb2009-07-02 17:20:43 +02006572 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006573
Johannes Berg19957bb2009-07-02 17:20:43 +02006574 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6575 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006576 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006577 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006578 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006579
6580 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006581 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6582 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006583 }
6584
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006585 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6586
Johannes Berg91bf9b22013-05-15 17:44:01 +02006587 wdev_lock(dev->ieee80211_ptr);
6588 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6589 local_state_change);
6590 wdev_unlock(dev->ieee80211_ptr);
6591 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006592}
6593
6594static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6595{
Johannes Berg4c476992010-10-04 21:36:35 +02006596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6597 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006598 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006599 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006600 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006601 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006602
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006603 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6604 return -EINVAL;
6605
6606 if (!info->attrs[NL80211_ATTR_MAC])
6607 return -EINVAL;
6608
6609 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6610 return -EINVAL;
6611
Johannes Berg4c476992010-10-04 21:36:35 +02006612 if (!rdev->ops->disassoc)
6613 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006614
Johannes Berg074ac8d2010-09-16 14:58:22 +02006615 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006616 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6617 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006618
Johannes Berg19957bb2009-07-02 17:20:43 +02006619 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006620
Johannes Berg19957bb2009-07-02 17:20:43 +02006621 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6622 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006623 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006624 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006625 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006626
6627 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006628 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6629 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006630 }
6631
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006632 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6633
Johannes Berg91bf9b22013-05-15 17:44:01 +02006634 wdev_lock(dev->ieee80211_ptr);
6635 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6636 local_state_change);
6637 wdev_unlock(dev->ieee80211_ptr);
6638 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006639}
6640
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006641static bool
6642nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6643 int mcast_rate[IEEE80211_NUM_BANDS],
6644 int rateval)
6645{
6646 struct wiphy *wiphy = &rdev->wiphy;
6647 bool found = false;
6648 int band, i;
6649
6650 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6651 struct ieee80211_supported_band *sband;
6652
6653 sband = wiphy->bands[band];
6654 if (!sband)
6655 continue;
6656
6657 for (i = 0; i < sband->n_bitrates; i++) {
6658 if (sband->bitrates[i].bitrate == rateval) {
6659 mcast_rate[band] = i + 1;
6660 found = true;
6661 break;
6662 }
6663 }
6664 }
6665
6666 return found;
6667}
6668
Johannes Berg04a773a2009-04-19 21:24:32 +02006669static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6670{
Johannes Berg4c476992010-10-04 21:36:35 +02006671 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6672 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006673 struct cfg80211_ibss_params ibss;
6674 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006675 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006676 int err;
6677
Johannes Berg8e30bc52009-04-22 17:45:38 +02006678 memset(&ibss, 0, sizeof(ibss));
6679
Johannes Berg04a773a2009-04-19 21:24:32 +02006680 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6681 return -EINVAL;
6682
Johannes Berg683b6d32012-11-08 21:25:48 +01006683 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006684 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6685 return -EINVAL;
6686
Johannes Berg8e30bc52009-04-22 17:45:38 +02006687 ibss.beacon_interval = 100;
6688
6689 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6690 ibss.beacon_interval =
6691 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6692 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6693 return -EINVAL;
6694 }
6695
Johannes Berg4c476992010-10-04 21:36:35 +02006696 if (!rdev->ops->join_ibss)
6697 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006698
Johannes Berg4c476992010-10-04 21:36:35 +02006699 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6700 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006701
Johannes Berg79c97e92009-07-07 03:56:12 +02006702 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006703
Johannes Berg39193492011-09-16 13:45:25 +02006704 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006705 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006706
6707 if (!is_valid_ether_addr(ibss.bssid))
6708 return -EINVAL;
6709 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006710 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6711 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6712
6713 if (info->attrs[NL80211_ATTR_IE]) {
6714 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6715 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6716 }
6717
Johannes Berg683b6d32012-11-08 21:25:48 +01006718 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6719 if (err)
6720 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006721
Ilan Peer174e0cd2014-02-23 09:13:01 +02006722 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
6723 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006724 return -EINVAL;
6725
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006726 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006727 case NL80211_CHAN_WIDTH_5:
6728 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006729 case NL80211_CHAN_WIDTH_20_NOHT:
6730 break;
6731 case NL80211_CHAN_WIDTH_20:
6732 case NL80211_CHAN_WIDTH_40:
6733 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6734 break;
6735 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006736 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006737 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006738
Johannes Berg04a773a2009-04-19 21:24:32 +02006739 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006740 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006741
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006742 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6743 u8 *rates =
6744 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6745 int n_rates =
6746 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6747 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006748 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006749
Johannes Berg34850ab2011-07-18 18:08:35 +02006750 err = ieee80211_get_ratemask(sband, rates, n_rates,
6751 &ibss.basic_rates);
6752 if (err)
6753 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006754 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006755
Simon Wunderlich803768f2013-06-28 10:39:58 +02006756 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6757 memcpy(&ibss.ht_capa_mask,
6758 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6759 sizeof(ibss.ht_capa_mask));
6760
6761 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6762 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6763 return -EINVAL;
6764 memcpy(&ibss.ht_capa,
6765 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6766 sizeof(ibss.ht_capa));
6767 }
6768
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006769 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6770 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6771 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6772 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006773
Johannes Berg4c476992010-10-04 21:36:35 +02006774 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306775 bool no_ht = false;
6776
Johannes Berg4c476992010-10-04 21:36:35 +02006777 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306778 info->attrs[NL80211_ATTR_KEYS],
6779 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006780 if (IS_ERR(connkeys))
6781 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306782
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006783 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6784 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306785 kfree(connkeys);
6786 return -EINVAL;
6787 }
Johannes Berg4c476992010-10-04 21:36:35 +02006788 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006789
Antonio Quartulli267335d2012-01-31 20:25:47 +01006790 ibss.control_port =
6791 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6792
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006793 ibss.userspace_handles_dfs =
6794 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6795
Johannes Berg4c476992010-10-04 21:36:35 +02006796 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006797 if (err)
6798 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006799 return err;
6800}
6801
6802static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6803{
Johannes Berg4c476992010-10-04 21:36:35 +02006804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6805 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006806
Johannes Berg4c476992010-10-04 21:36:35 +02006807 if (!rdev->ops->leave_ibss)
6808 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006809
Johannes Berg4c476992010-10-04 21:36:35 +02006810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6811 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006812
Johannes Berg4c476992010-10-04 21:36:35 +02006813 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006814}
6815
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006816static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6817{
6818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6819 struct net_device *dev = info->user_ptr[1];
6820 int mcast_rate[IEEE80211_NUM_BANDS];
6821 u32 nla_rate;
6822 int err;
6823
6824 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6825 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6826 return -EOPNOTSUPP;
6827
6828 if (!rdev->ops->set_mcast_rate)
6829 return -EOPNOTSUPP;
6830
6831 memset(mcast_rate, 0, sizeof(mcast_rate));
6832
6833 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6834 return -EINVAL;
6835
6836 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6837 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6838 return -EINVAL;
6839
6840 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6841
6842 return err;
6843}
6844
Johannes Bergad7e7182013-11-13 13:37:47 +01006845static struct sk_buff *
6846__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6847 int approxlen, u32 portid, u32 seq,
6848 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006849 enum nl80211_attrs attr,
6850 const struct nl80211_vendor_cmd_info *info,
6851 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006852{
6853 struct sk_buff *skb;
6854 void *hdr;
6855 struct nlattr *data;
6856
6857 skb = nlmsg_new(approxlen + 100, gfp);
6858 if (!skb)
6859 return NULL;
6860
6861 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6862 if (!hdr) {
6863 kfree_skb(skb);
6864 return NULL;
6865 }
6866
6867 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6868 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006869
6870 if (info) {
6871 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6872 info->vendor_id))
6873 goto nla_put_failure;
6874 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6875 info->subcmd))
6876 goto nla_put_failure;
6877 }
6878
Johannes Bergad7e7182013-11-13 13:37:47 +01006879 data = nla_nest_start(skb, attr);
6880
6881 ((void **)skb->cb)[0] = rdev;
6882 ((void **)skb->cb)[1] = hdr;
6883 ((void **)skb->cb)[2] = data;
6884
6885 return skb;
6886
6887 nla_put_failure:
6888 kfree_skb(skb);
6889 return NULL;
6890}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006891
Johannes Berge03ad6e2014-01-01 17:22:30 +01006892struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6893 enum nl80211_commands cmd,
6894 enum nl80211_attrs attr,
6895 int vendor_event_idx,
6896 int approxlen, gfp_t gfp)
6897{
6898 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6899 const struct nl80211_vendor_cmd_info *info;
6900
6901 switch (cmd) {
6902 case NL80211_CMD_TESTMODE:
6903 if (WARN_ON(vendor_event_idx != -1))
6904 return NULL;
6905 info = NULL;
6906 break;
6907 case NL80211_CMD_VENDOR:
6908 if (WARN_ON(vendor_event_idx < 0 ||
6909 vendor_event_idx >= wiphy->n_vendor_events))
6910 return NULL;
6911 info = &wiphy->vendor_events[vendor_event_idx];
6912 break;
6913 default:
6914 WARN_ON(1);
6915 return NULL;
6916 }
6917
6918 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6919 cmd, attr, info, gfp);
6920}
6921EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6922
6923void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6924{
6925 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6926 void *hdr = ((void **)skb->cb)[1];
6927 struct nlattr *data = ((void **)skb->cb)[2];
6928 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6929
6930 nla_nest_end(skb, data);
6931 genlmsg_end(skb, hdr);
6932
6933 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6934 mcgrp = NL80211_MCGRP_VENDOR;
6935
6936 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6937 mcgrp, gfp);
6938}
6939EXPORT_SYMBOL(__cfg80211_send_event_skb);
6940
Johannes Bergaff89a92009-07-01 21:26:51 +02006941#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006942static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6943{
Johannes Berg4c476992010-10-04 21:36:35 +02006944 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006945 struct wireless_dev *wdev =
6946 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006947 int err;
6948
David Spinadelfc73f112013-07-31 18:04:15 +03006949 if (!rdev->ops->testmode_cmd)
6950 return -EOPNOTSUPP;
6951
6952 if (IS_ERR(wdev)) {
6953 err = PTR_ERR(wdev);
6954 if (err != -EINVAL)
6955 return err;
6956 wdev = NULL;
6957 } else if (wdev->wiphy != &rdev->wiphy) {
6958 return -EINVAL;
6959 }
6960
Johannes Bergaff89a92009-07-01 21:26:51 +02006961 if (!info->attrs[NL80211_ATTR_TESTDATA])
6962 return -EINVAL;
6963
Johannes Bergad7e7182013-11-13 13:37:47 +01006964 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006965 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006966 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6967 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006968 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006969
Johannes Bergaff89a92009-07-01 21:26:51 +02006970 return err;
6971}
6972
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006973static int nl80211_testmode_dump(struct sk_buff *skb,
6974 struct netlink_callback *cb)
6975{
Johannes Berg00918d32011-12-13 17:22:05 +01006976 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006977 int err;
6978 long phy_idx;
6979 void *data = NULL;
6980 int data_len = 0;
6981
Johannes Berg5fe231e2013-05-08 21:45:15 +02006982 rtnl_lock();
6983
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006984 if (cb->args[0]) {
6985 /*
6986 * 0 is a valid index, but not valid for args[0],
6987 * so we need to offset by 1.
6988 */
6989 phy_idx = cb->args[0] - 1;
6990 } else {
6991 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6992 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6993 nl80211_policy);
6994 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006995 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006996
Johannes Berg2bd7e352012-06-15 14:23:16 +02006997 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6998 nl80211_fam.attrbuf);
6999 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007000 err = PTR_ERR(rdev);
7001 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01007002 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02007003 phy_idx = rdev->wiphy_idx;
7004 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02007005
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007006 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7007 cb->args[1] =
7008 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7009 }
7010
7011 if (cb->args[1]) {
7012 data = nla_data((void *)cb->args[1]);
7013 data_len = nla_len((void *)cb->args[1]);
7014 }
7015
Johannes Berg00918d32011-12-13 17:22:05 +01007016 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7017 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007018 err = -ENOENT;
7019 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007020 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007021
Johannes Berg00918d32011-12-13 17:22:05 +01007022 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007023 err = -EOPNOTSUPP;
7024 goto out_err;
7025 }
7026
7027 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007028 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007029 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7030 NL80211_CMD_TESTMODE);
7031 struct nlattr *tmdata;
7032
Dan Carpentercb35fba2013-08-14 14:50:01 +03007033 if (!hdr)
7034 break;
7035
David S. Miller9360ffd2012-03-29 04:41:26 -04007036 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007037 genlmsg_cancel(skb, hdr);
7038 break;
7039 }
7040
7041 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7042 if (!tmdata) {
7043 genlmsg_cancel(skb, hdr);
7044 break;
7045 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007046 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007047 nla_nest_end(skb, tmdata);
7048
7049 if (err == -ENOBUFS || err == -ENOENT) {
7050 genlmsg_cancel(skb, hdr);
7051 break;
7052 } else if (err) {
7053 genlmsg_cancel(skb, hdr);
7054 goto out_err;
7055 }
7056
7057 genlmsg_end(skb, hdr);
7058 }
7059
7060 err = skb->len;
7061 /* see above */
7062 cb->args[0] = phy_idx + 1;
7063 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007064 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007065 return err;
7066}
Johannes Bergaff89a92009-07-01 21:26:51 +02007067#endif
7068
Samuel Ortizb23aa672009-07-01 21:26:54 +02007069static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7070{
Johannes Berg4c476992010-10-04 21:36:35 +02007071 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7072 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007073 struct cfg80211_connect_params connect;
7074 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007075 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007076 int err;
7077
7078 memset(&connect, 0, sizeof(connect));
7079
7080 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7081 return -EINVAL;
7082
7083 if (!info->attrs[NL80211_ATTR_SSID] ||
7084 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7085 return -EINVAL;
7086
7087 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7088 connect.auth_type =
7089 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007090 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7091 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007092 return -EINVAL;
7093 } else
7094 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7095
7096 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7097
Johannes Bergc0692b82010-08-27 14:26:53 +03007098 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007099 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007100 if (err)
7101 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007102
Johannes Berg074ac8d2010-09-16 14:58:22 +02007103 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007104 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7105 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007106
Johannes Berg79c97e92009-07-07 03:56:12 +02007107 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007108
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307109 connect.bg_scan_period = -1;
7110 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7111 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7112 connect.bg_scan_period =
7113 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7114 }
7115
Samuel Ortizb23aa672009-07-01 21:26:54 +02007116 if (info->attrs[NL80211_ATTR_MAC])
7117 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007118 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7119 connect.bssid_hint =
7120 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007121 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7122 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7123
7124 if (info->attrs[NL80211_ATTR_IE]) {
7125 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7126 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7127 }
7128
Jouni Malinencee00a92013-01-15 17:15:57 +02007129 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7130 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7131 if (connect.mfp != NL80211_MFP_REQUIRED &&
7132 connect.mfp != NL80211_MFP_NO)
7133 return -EINVAL;
7134 } else {
7135 connect.mfp = NL80211_MFP_NO;
7136 }
7137
Samuel Ortizb23aa672009-07-01 21:26:54 +02007138 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007139 connect.channel = nl80211_get_valid_chan(
7140 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7141 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007142 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007143 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007144 connect.channel_hint = nl80211_get_valid_chan(
7145 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7146 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007147 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007148 }
7149
Johannes Bergfffd0932009-07-08 14:22:54 +02007150 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7151 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307152 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007153 if (IS_ERR(connkeys))
7154 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007155 }
7156
Ben Greear7e7c8922011-11-18 11:31:59 -08007157 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7158 connect.flags |= ASSOC_REQ_DISABLE_HT;
7159
7160 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7161 memcpy(&connect.ht_capa_mask,
7162 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7163 sizeof(connect.ht_capa_mask));
7164
7165 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007166 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7167 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007168 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007169 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007170 memcpy(&connect.ht_capa,
7171 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7172 sizeof(connect.ht_capa));
7173 }
7174
Johannes Bergee2aca32013-02-21 17:36:01 +01007175 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7176 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7177
7178 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7179 memcpy(&connect.vht_capa_mask,
7180 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7181 sizeof(connect.vht_capa_mask));
7182
7183 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7184 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7185 kfree(connkeys);
7186 return -EINVAL;
7187 }
7188 memcpy(&connect.vht_capa,
7189 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7190 sizeof(connect.vht_capa));
7191 }
7192
Johannes Berg83739b02013-05-15 17:44:01 +02007193 wdev_lock(dev->ieee80211_ptr);
7194 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7195 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007196 if (err)
7197 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007198 return err;
7199}
7200
7201static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7202{
Johannes Berg4c476992010-10-04 21:36:35 +02007203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7204 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007205 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007206 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007207
7208 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7209 reason = WLAN_REASON_DEAUTH_LEAVING;
7210 else
7211 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7212
7213 if (reason == 0)
7214 return -EINVAL;
7215
Johannes Berg074ac8d2010-09-16 14:58:22 +02007216 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007217 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7218 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007219
Johannes Berg83739b02013-05-15 17:44:01 +02007220 wdev_lock(dev->ieee80211_ptr);
7221 ret = cfg80211_disconnect(rdev, dev, reason, true);
7222 wdev_unlock(dev->ieee80211_ptr);
7223 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007224}
7225
Johannes Berg463d0182009-07-14 00:33:35 +02007226static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7227{
Johannes Berg4c476992010-10-04 21:36:35 +02007228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007229 struct net *net;
7230 int err;
7231 u32 pid;
7232
7233 if (!info->attrs[NL80211_ATTR_PID])
7234 return -EINVAL;
7235
7236 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7237
Johannes Berg463d0182009-07-14 00:33:35 +02007238 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007239 if (IS_ERR(net))
7240 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007241
7242 err = 0;
7243
7244 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007245 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7246 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007247
Johannes Berg463d0182009-07-14 00:33:35 +02007248 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007249 return err;
7250}
7251
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007252static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7253{
Johannes Berg4c476992010-10-04 21:36:35 +02007254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007255 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7256 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007257 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007258 struct cfg80211_pmksa pmksa;
7259
7260 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7261
7262 if (!info->attrs[NL80211_ATTR_MAC])
7263 return -EINVAL;
7264
7265 if (!info->attrs[NL80211_ATTR_PMKID])
7266 return -EINVAL;
7267
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007268 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7269 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7270
Johannes Berg074ac8d2010-09-16 14:58:22 +02007271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007272 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7273 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007274
7275 switch (info->genlhdr->cmd) {
7276 case NL80211_CMD_SET_PMKSA:
7277 rdev_ops = rdev->ops->set_pmksa;
7278 break;
7279 case NL80211_CMD_DEL_PMKSA:
7280 rdev_ops = rdev->ops->del_pmksa;
7281 break;
7282 default:
7283 WARN_ON(1);
7284 break;
7285 }
7286
Johannes Berg4c476992010-10-04 21:36:35 +02007287 if (!rdev_ops)
7288 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007289
Johannes Berg4c476992010-10-04 21:36:35 +02007290 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007291}
7292
7293static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7294{
Johannes Berg4c476992010-10-04 21:36:35 +02007295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7296 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007297
Johannes Berg074ac8d2010-09-16 14:58:22 +02007298 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007299 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7300 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007301
Johannes Berg4c476992010-10-04 21:36:35 +02007302 if (!rdev->ops->flush_pmksa)
7303 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007304
Hila Gonene35e4d22012-06-27 17:19:42 +03007305 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007306}
7307
Arik Nemtsov109086c2011-09-28 14:12:50 +03007308static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7309{
7310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7311 struct net_device *dev = info->user_ptr[1];
7312 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307313 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007314 u16 status_code;
7315 u8 *peer;
7316
7317 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7318 !rdev->ops->tdls_mgmt)
7319 return -EOPNOTSUPP;
7320
7321 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7322 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7323 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7324 !info->attrs[NL80211_ATTR_IE] ||
7325 !info->attrs[NL80211_ATTR_MAC])
7326 return -EINVAL;
7327
7328 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7329 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7330 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7331 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307332 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7333 peer_capability =
7334 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007335
Hila Gonene35e4d22012-06-27 17:19:42 +03007336 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307337 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007338 nla_data(info->attrs[NL80211_ATTR_IE]),
7339 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007340}
7341
7342static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7343{
7344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7345 struct net_device *dev = info->user_ptr[1];
7346 enum nl80211_tdls_operation operation;
7347 u8 *peer;
7348
7349 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7350 !rdev->ops->tdls_oper)
7351 return -EOPNOTSUPP;
7352
7353 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7354 !info->attrs[NL80211_ATTR_MAC])
7355 return -EINVAL;
7356
7357 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7358 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7359
Hila Gonene35e4d22012-06-27 17:19:42 +03007360 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007361}
7362
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007363static int nl80211_remain_on_channel(struct sk_buff *skb,
7364 struct genl_info *info)
7365{
Johannes Berg4c476992010-10-04 21:36:35 +02007366 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007367 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007368 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007369 struct sk_buff *msg;
7370 void *hdr;
7371 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007372 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007373 int err;
7374
7375 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7376 !info->attrs[NL80211_ATTR_DURATION])
7377 return -EINVAL;
7378
7379 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7380
Johannes Berg7c4ef712011-11-18 15:33:48 +01007381 if (!rdev->ops->remain_on_channel ||
7382 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007383 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007384
Johannes Bergebf348f2012-06-01 12:50:54 +02007385 /*
7386 * We should be on that channel for at least a minimum amount of
7387 * time (10ms) but no longer than the driver supports.
7388 */
7389 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7390 duration > rdev->wiphy.max_remain_on_channel_duration)
7391 return -EINVAL;
7392
Johannes Berg683b6d32012-11-08 21:25:48 +01007393 err = nl80211_parse_chandef(rdev, info, &chandef);
7394 if (err)
7395 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007396
7397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007398 if (!msg)
7399 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007400
Eric W. Biederman15e47302012-09-07 20:12:54 +00007401 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007402 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007403 if (!hdr) {
7404 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007405 goto free_msg;
7406 }
7407
Johannes Berg683b6d32012-11-08 21:25:48 +01007408 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7409 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007410
7411 if (err)
7412 goto free_msg;
7413
David S. Miller9360ffd2012-03-29 04:41:26 -04007414 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7415 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007416
7417 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007418
7419 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007420
7421 nla_put_failure:
7422 err = -ENOBUFS;
7423 free_msg:
7424 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007425 return err;
7426}
7427
7428static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7429 struct genl_info *info)
7430{
Johannes Berg4c476992010-10-04 21:36:35 +02007431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007432 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007433 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007434
7435 if (!info->attrs[NL80211_ATTR_COOKIE])
7436 return -EINVAL;
7437
Johannes Berg4c476992010-10-04 21:36:35 +02007438 if (!rdev->ops->cancel_remain_on_channel)
7439 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007440
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007441 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7442
Hila Gonene35e4d22012-06-27 17:19:42 +03007443 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007444}
7445
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007446static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7447 u8 *rates, u8 rates_len)
7448{
7449 u8 i;
7450 u32 mask = 0;
7451
7452 for (i = 0; i < rates_len; i++) {
7453 int rate = (rates[i] & 0x7f) * 5;
7454 int ridx;
7455 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7456 struct ieee80211_rate *srate =
7457 &sband->bitrates[ridx];
7458 if (rate == srate->bitrate) {
7459 mask |= 1 << ridx;
7460 break;
7461 }
7462 }
7463 if (ridx == sband->n_bitrates)
7464 return 0; /* rate not found */
7465 }
7466
7467 return mask;
7468}
7469
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007470static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7471 u8 *rates, u8 rates_len,
7472 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7473{
7474 u8 i;
7475
7476 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7477
7478 for (i = 0; i < rates_len; i++) {
7479 int ridx, rbit;
7480
7481 ridx = rates[i] / 8;
7482 rbit = BIT(rates[i] % 8);
7483
7484 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007485 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007486 return false;
7487
7488 /* check availability */
7489 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7490 mcs[ridx] |= rbit;
7491 else
7492 return false;
7493 }
7494
7495 return true;
7496}
7497
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007498static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7499{
7500 u16 mcs_mask = 0;
7501
7502 switch (vht_mcs_map) {
7503 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7504 break;
7505 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7506 mcs_mask = 0x00FF;
7507 break;
7508 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7509 mcs_mask = 0x01FF;
7510 break;
7511 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7512 mcs_mask = 0x03FF;
7513 break;
7514 default:
7515 break;
7516 }
7517
7518 return mcs_mask;
7519}
7520
7521static void vht_build_mcs_mask(u16 vht_mcs_map,
7522 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7523{
7524 u8 nss;
7525
7526 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7527 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7528 vht_mcs_map >>= 2;
7529 }
7530}
7531
7532static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7533 struct nl80211_txrate_vht *txrate,
7534 u16 mcs[NL80211_VHT_NSS_MAX])
7535{
7536 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7537 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7538 u8 i;
7539
7540 if (!sband->vht_cap.vht_supported)
7541 return false;
7542
7543 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7544
7545 /* Build vht_mcs_mask from VHT capabilities */
7546 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7547
7548 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7549 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7550 mcs[i] = txrate->mcs[i];
7551 else
7552 return false;
7553 }
7554
7555 return true;
7556}
7557
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007558static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007559 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7560 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007561 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7562 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007563 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007564 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007565};
7566
7567static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7568 struct genl_info *info)
7569{
7570 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007571 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007572 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007573 int rem, i;
7574 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007575 struct nlattr *tx_rates;
7576 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007577 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007578
Johannes Berg4c476992010-10-04 21:36:35 +02007579 if (!rdev->ops->set_bitrate_mask)
7580 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007581
7582 memset(&mask, 0, sizeof(mask));
7583 /* Default to all rates enabled */
7584 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7585 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007586
7587 if (!sband)
7588 continue;
7589
7590 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007591 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007592 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007593 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007594
7595 if (!sband->vht_cap.vht_supported)
7596 continue;
7597
7598 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7599 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007600 }
7601
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007602 /* if no rates are given set it back to the defaults */
7603 if (!info->attrs[NL80211_ATTR_TX_RATES])
7604 goto out;
7605
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007606 /*
7607 * The nested attribute uses enum nl80211_band as the index. This maps
7608 * directly to the enum ieee80211_band values used in cfg80211.
7609 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007610 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007611 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007612 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007613 int err;
7614
Johannes Berg4c476992010-10-04 21:36:35 +02007615 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7616 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007617 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007618 if (sband == NULL)
7619 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007620 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7621 nla_len(tx_rates), nl80211_txattr_policy);
7622 if (err)
7623 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007624 if (tb[NL80211_TXRATE_LEGACY]) {
7625 mask.control[band].legacy = rateset_to_mask(
7626 sband,
7627 nla_data(tb[NL80211_TXRATE_LEGACY]),
7628 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307629 if ((mask.control[band].legacy == 0) &&
7630 nla_len(tb[NL80211_TXRATE_LEGACY]))
7631 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007632 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007633 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007634 if (!ht_rateset_to_mask(
7635 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007636 nla_data(tb[NL80211_TXRATE_HT]),
7637 nla_len(tb[NL80211_TXRATE_HT]),
7638 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007639 return -EINVAL;
7640 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007641 if (tb[NL80211_TXRATE_VHT]) {
7642 if (!vht_set_mcs_mask(
7643 sband,
7644 nla_data(tb[NL80211_TXRATE_VHT]),
7645 mask.control[band].vht_mcs))
7646 return -EINVAL;
7647 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007648 if (tb[NL80211_TXRATE_GI]) {
7649 mask.control[band].gi =
7650 nla_get_u8(tb[NL80211_TXRATE_GI]);
7651 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7652 return -EINVAL;
7653 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007654
7655 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007656 /* don't allow empty legacy rates if HT or VHT
7657 * are not even supported.
7658 */
7659 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7660 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007661 return -EINVAL;
7662
7663 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007664 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007665 goto out;
7666
7667 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7668 if (mask.control[band].vht_mcs[i])
7669 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007670
7671 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007672 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007673 }
7674 }
7675
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007676out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007677 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007678}
7679
Johannes Berg2e161f782010-08-12 15:38:38 +02007680static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007681{
Johannes Berg4c476992010-10-04 21:36:35 +02007682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007683 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f782010-08-12 15:38:38 +02007684 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007685
7686 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7687 return -EINVAL;
7688
Johannes Berg2e161f782010-08-12 15:38:38 +02007689 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7690 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007691
Johannes Berg71bbc992012-06-15 15:30:18 +02007692 switch (wdev->iftype) {
7693 case NL80211_IFTYPE_STATION:
7694 case NL80211_IFTYPE_ADHOC:
7695 case NL80211_IFTYPE_P2P_CLIENT:
7696 case NL80211_IFTYPE_AP:
7697 case NL80211_IFTYPE_AP_VLAN:
7698 case NL80211_IFTYPE_MESH_POINT:
7699 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007700 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007701 break;
7702 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007703 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007704 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007705
7706 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007707 if (!rdev->ops->mgmt_tx)
7708 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007709
Eric W. Biederman15e47302012-09-07 20:12:54 +00007710 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007711 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7712 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007713}
7714
Johannes Berg2e161f782010-08-12 15:38:38 +02007715static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007716{
Johannes Berg4c476992010-10-04 21:36:35 +02007717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007718 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007719 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007720 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007721 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007722 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007723 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007724 struct cfg80211_mgmt_tx_params params = {
7725 .dont_wait_for_ack =
7726 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7727 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007728
Johannes Berg683b6d32012-11-08 21:25:48 +01007729 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007730 return -EINVAL;
7731
Johannes Berg4c476992010-10-04 21:36:35 +02007732 if (!rdev->ops->mgmt_tx)
7733 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007734
Johannes Berg71bbc992012-06-15 15:30:18 +02007735 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007736 case NL80211_IFTYPE_P2P_DEVICE:
7737 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7738 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007739 case NL80211_IFTYPE_STATION:
7740 case NL80211_IFTYPE_ADHOC:
7741 case NL80211_IFTYPE_P2P_CLIENT:
7742 case NL80211_IFTYPE_AP:
7743 case NL80211_IFTYPE_AP_VLAN:
7744 case NL80211_IFTYPE_MESH_POINT:
7745 case NL80211_IFTYPE_P2P_GO:
7746 break;
7747 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007748 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007749 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007750
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007751 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007752 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007753 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007754 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007755
7756 /*
7757 * We should wait on the channel for at least a minimum amount
7758 * of time (10ms) but no longer than the driver supports.
7759 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007760 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7761 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007762 return -EINVAL;
7763
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007764 }
7765
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007766 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007767
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007768 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007769 return -EINVAL;
7770
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007771 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307772
Antonio Quartulliea141b752013-06-11 14:20:03 +02007773 /* get the channel if any has been specified, otherwise pass NULL to
7774 * the driver. The latter will use the current one
7775 */
7776 chandef.chan = NULL;
7777 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7778 err = nl80211_parse_chandef(rdev, info, &chandef);
7779 if (err)
7780 return err;
7781 }
7782
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007783 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007784 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007785
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007786 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007787 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7788 if (!msg)
7789 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007790
Eric W. Biederman15e47302012-09-07 20:12:54 +00007791 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007792 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007793 if (!hdr) {
7794 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007795 goto free_msg;
7796 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007797 }
Johannes Berge247bd902011-11-04 11:18:21 +01007798
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007799 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7800 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7801 params.chan = chandef.chan;
7802 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007803 if (err)
7804 goto free_msg;
7805
Johannes Berge247bd902011-11-04 11:18:21 +01007806 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007807 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7808 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007809
Johannes Berge247bd902011-11-04 11:18:21 +01007810 genlmsg_end(msg, hdr);
7811 return genlmsg_reply(msg, info);
7812 }
7813
7814 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007815
7816 nla_put_failure:
7817 err = -ENOBUFS;
7818 free_msg:
7819 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007820 return err;
7821}
7822
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007823static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7824{
7825 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007826 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007827 u64 cookie;
7828
7829 if (!info->attrs[NL80211_ATTR_COOKIE])
7830 return -EINVAL;
7831
7832 if (!rdev->ops->mgmt_tx_cancel_wait)
7833 return -EOPNOTSUPP;
7834
Johannes Berg71bbc992012-06-15 15:30:18 +02007835 switch (wdev->iftype) {
7836 case NL80211_IFTYPE_STATION:
7837 case NL80211_IFTYPE_ADHOC:
7838 case NL80211_IFTYPE_P2P_CLIENT:
7839 case NL80211_IFTYPE_AP:
7840 case NL80211_IFTYPE_AP_VLAN:
7841 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007842 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007843 break;
7844 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007845 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007846 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007847
7848 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7849
Hila Gonene35e4d22012-06-27 17:19:42 +03007850 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007851}
7852
Kalle Valoffb9eb32010-02-17 17:58:10 +02007853static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7854{
Johannes Berg4c476992010-10-04 21:36:35 +02007855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007856 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007857 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007858 u8 ps_state;
7859 bool state;
7860 int err;
7861
Johannes Berg4c476992010-10-04 21:36:35 +02007862 if (!info->attrs[NL80211_ATTR_PS_STATE])
7863 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007864
7865 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7866
Johannes Berg4c476992010-10-04 21:36:35 +02007867 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7868 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007869
7870 wdev = dev->ieee80211_ptr;
7871
Johannes Berg4c476992010-10-04 21:36:35 +02007872 if (!rdev->ops->set_power_mgmt)
7873 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007874
7875 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7876
7877 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007878 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007879
Hila Gonene35e4d22012-06-27 17:19:42 +03007880 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007881 if (!err)
7882 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007883 return err;
7884}
7885
7886static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7887{
Johannes Berg4c476992010-10-04 21:36:35 +02007888 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007889 enum nl80211_ps_state ps_state;
7890 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007891 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007892 struct sk_buff *msg;
7893 void *hdr;
7894 int err;
7895
Kalle Valoffb9eb32010-02-17 17:58:10 +02007896 wdev = dev->ieee80211_ptr;
7897
Johannes Berg4c476992010-10-04 21:36:35 +02007898 if (!rdev->ops->set_power_mgmt)
7899 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007900
7901 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007902 if (!msg)
7903 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007904
Eric W. Biederman15e47302012-09-07 20:12:54 +00007905 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007906 NL80211_CMD_GET_POWER_SAVE);
7907 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007908 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007909 goto free_msg;
7910 }
7911
7912 if (wdev->ps)
7913 ps_state = NL80211_PS_ENABLED;
7914 else
7915 ps_state = NL80211_PS_DISABLED;
7916
David S. Miller9360ffd2012-03-29 04:41:26 -04007917 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7918 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007919
7920 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007921 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007922
Johannes Berg4c476992010-10-04 21:36:35 +02007923 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007924 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007925 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007926 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007927 return err;
7928}
7929
Johannes Berg94e860f2014-01-20 23:58:15 +01007930static const struct nla_policy
7931nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007932 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7933 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7934 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007935 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7936 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7937 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007938};
7939
Thomas Pedersen84f10702012-07-12 16:17:33 -07007940static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007941 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007942{
7943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007944 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007945 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007946
Johannes Bergd9d8b012012-11-26 12:51:52 +01007947 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007948 return -EINVAL;
7949
Thomas Pedersen84f10702012-07-12 16:17:33 -07007950 if (!rdev->ops->set_cqm_txe_config)
7951 return -EOPNOTSUPP;
7952
7953 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7954 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7955 return -EOPNOTSUPP;
7956
Hila Gonene35e4d22012-06-27 17:19:42 +03007957 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007958}
7959
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007960static int nl80211_set_cqm_rssi(struct genl_info *info,
7961 s32 threshold, u32 hysteresis)
7962{
Johannes Berg4c476992010-10-04 21:36:35 +02007963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007964 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007965 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007966
7967 if (threshold > 0)
7968 return -EINVAL;
7969
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007970 /* disabling - hysteresis should also be zero then */
7971 if (threshold == 0)
7972 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007973
Johannes Berg4c476992010-10-04 21:36:35 +02007974 if (!rdev->ops->set_cqm_rssi_config)
7975 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007976
Johannes Berg074ac8d2010-09-16 14:58:22 +02007977 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007978 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7979 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007980
Hila Gonene35e4d22012-06-27 17:19:42 +03007981 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007982}
7983
7984static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7985{
7986 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7987 struct nlattr *cqm;
7988 int err;
7989
7990 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007991 if (!cqm)
7992 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007993
7994 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7995 nl80211_attr_cqm_policy);
7996 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007997 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007998
7999 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8000 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008001 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8002 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008003
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008004 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8005 }
8006
8007 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8008 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8009 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8010 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8011 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8012 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8013
8014 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8015 }
8016
8017 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008018}
8019
Johannes Berg29cbe682010-12-03 09:20:44 +01008020static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8021{
8022 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8023 struct net_device *dev = info->user_ptr[1];
8024 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008025 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008026 int err;
8027
8028 /* start with default */
8029 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008030 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008031
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008032 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008033 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008034 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008035 if (err)
8036 return err;
8037 }
8038
8039 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8040 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8041 return -EINVAL;
8042
Javier Cardonac80d5452010-12-16 17:37:49 -08008043 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8044 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8045
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008046 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8047 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8048 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8049 return -EINVAL;
8050
Marco Porsch9bdbf042013-01-07 16:04:51 +01008051 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8052 setup.beacon_interval =
8053 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8054 if (setup.beacon_interval < 10 ||
8055 setup.beacon_interval > 10000)
8056 return -EINVAL;
8057 }
8058
8059 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8060 setup.dtim_period =
8061 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8062 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8063 return -EINVAL;
8064 }
8065
Javier Cardonac80d5452010-12-16 17:37:49 -08008066 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8067 /* parse additional setup parameters if given */
8068 err = nl80211_parse_mesh_setup(info, &setup);
8069 if (err)
8070 return err;
8071 }
8072
Thomas Pedersend37bb182013-03-04 13:06:13 -08008073 if (setup.user_mpm)
8074 cfg.auto_open_plinks = false;
8075
Johannes Bergcc1d2802012-05-16 23:50:20 +02008076 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008077 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8078 if (err)
8079 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008080 } else {
8081 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008082 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008083 }
8084
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008085 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8086 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8087 int n_rates =
8088 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8089 struct ieee80211_supported_band *sband;
8090
8091 if (!setup.chandef.chan)
8092 return -EINVAL;
8093
8094 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8095
8096 err = ieee80211_get_ratemask(sband, rates, n_rates,
8097 &setup.basic_rates);
8098 if (err)
8099 return err;
8100 }
8101
Javier Cardonac80d5452010-12-16 17:37:49 -08008102 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008103}
8104
8105static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8106{
8107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8108 struct net_device *dev = info->user_ptr[1];
8109
8110 return cfg80211_leave_mesh(rdev, dev);
8111}
8112
Johannes Bergdfb89c52012-06-27 09:23:48 +02008113#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008114static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8115 struct cfg80211_registered_device *rdev)
8116{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008117 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008118 struct nlattr *nl_pats, *nl_pat;
8119 int i, pat_len;
8120
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008121 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008122 return 0;
8123
8124 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8125 if (!nl_pats)
8126 return -ENOBUFS;
8127
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008128 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008129 nl_pat = nla_nest_start(msg, i + 1);
8130 if (!nl_pat)
8131 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008132 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008133 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008134 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008135 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8136 wowlan->patterns[i].pattern) ||
8137 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008138 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008139 return -ENOBUFS;
8140 nla_nest_end(msg, nl_pat);
8141 }
8142 nla_nest_end(msg, nl_pats);
8143
8144 return 0;
8145}
8146
Johannes Berg2a0e0472013-01-23 22:57:40 +01008147static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8148 struct cfg80211_wowlan_tcp *tcp)
8149{
8150 struct nlattr *nl_tcp;
8151
8152 if (!tcp)
8153 return 0;
8154
8155 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8156 if (!nl_tcp)
8157 return -ENOBUFS;
8158
8159 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8160 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8161 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8162 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8163 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8164 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8165 tcp->payload_len, tcp->payload) ||
8166 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8167 tcp->data_interval) ||
8168 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8169 tcp->wake_len, tcp->wake_data) ||
8170 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8171 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8172 return -ENOBUFS;
8173
8174 if (tcp->payload_seq.len &&
8175 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8176 sizeof(tcp->payload_seq), &tcp->payload_seq))
8177 return -ENOBUFS;
8178
8179 if (tcp->payload_tok.len &&
8180 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8181 sizeof(tcp->payload_tok) + tcp->tokens_size,
8182 &tcp->payload_tok))
8183 return -ENOBUFS;
8184
Johannes Berge248ad32013-05-16 10:24:28 +02008185 nla_nest_end(msg, nl_tcp);
8186
Johannes Berg2a0e0472013-01-23 22:57:40 +01008187 return 0;
8188}
8189
Johannes Bergff1b6e62011-05-04 15:37:28 +02008190static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8191{
8192 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8193 struct sk_buff *msg;
8194 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008195 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008196
Johannes Berg964dc9e2013-06-03 17:25:34 +02008197 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008198 return -EOPNOTSUPP;
8199
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008200 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008201 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008202 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8203 rdev->wiphy.wowlan_config->tcp->payload_len +
8204 rdev->wiphy.wowlan_config->tcp->wake_len +
8205 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008206 }
8207
8208 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008209 if (!msg)
8210 return -ENOMEM;
8211
Eric W. Biederman15e47302012-09-07 20:12:54 +00008212 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008213 NL80211_CMD_GET_WOWLAN);
8214 if (!hdr)
8215 goto nla_put_failure;
8216
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008217 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008218 struct nlattr *nl_wowlan;
8219
8220 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8221 if (!nl_wowlan)
8222 goto nla_put_failure;
8223
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008224 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008226 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008228 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008230 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008232 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008233 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008234 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008235 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008236 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008237 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8238 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008239
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008240 if (nl80211_send_wowlan_patterns(msg, rdev))
8241 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008242
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008243 if (nl80211_send_wowlan_tcp(msg,
8244 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008245 goto nla_put_failure;
8246
Johannes Bergff1b6e62011-05-04 15:37:28 +02008247 nla_nest_end(msg, nl_wowlan);
8248 }
8249
8250 genlmsg_end(msg, hdr);
8251 return genlmsg_reply(msg, info);
8252
8253nla_put_failure:
8254 nlmsg_free(msg);
8255 return -ENOBUFS;
8256}
8257
Johannes Berg2a0e0472013-01-23 22:57:40 +01008258static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8259 struct nlattr *attr,
8260 struct cfg80211_wowlan *trig)
8261{
8262 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8263 struct cfg80211_wowlan_tcp *cfg;
8264 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8265 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8266 u32 size;
8267 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8268 int err, port;
8269
Johannes Berg964dc9e2013-06-03 17:25:34 +02008270 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008271 return -EINVAL;
8272
8273 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8274 nla_data(attr), nla_len(attr),
8275 nl80211_wowlan_tcp_policy);
8276 if (err)
8277 return err;
8278
8279 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8280 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8281 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8282 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8283 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8284 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8285 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8286 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8287 return -EINVAL;
8288
8289 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008290 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008291 return -EINVAL;
8292
8293 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008294 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008295 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008296 return -EINVAL;
8297
8298 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008299 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008300 return -EINVAL;
8301
8302 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8303 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8304 return -EINVAL;
8305
8306 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8307 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8308
8309 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8310 tokens_size = tokln - sizeof(*tok);
8311
8312 if (!tok->len || tokens_size % tok->len)
8313 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008314 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008315 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008316 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008317 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008318 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008319 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008320 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008321 return -EINVAL;
8322 if (tok->offset + tok->len > data_size)
8323 return -EINVAL;
8324 }
8325
8326 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8327 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008328 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008329 return -EINVAL;
8330 if (seq->len == 0 || seq->len > 4)
8331 return -EINVAL;
8332 if (seq->len + seq->offset > data_size)
8333 return -EINVAL;
8334 }
8335
8336 size = sizeof(*cfg);
8337 size += data_size;
8338 size += wake_size + wake_mask_size;
8339 size += tokens_size;
8340
8341 cfg = kzalloc(size, GFP_KERNEL);
8342 if (!cfg)
8343 return -ENOMEM;
8344 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8345 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8346 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8347 ETH_ALEN);
8348 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8349 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8350 else
8351 port = 0;
8352#ifdef CONFIG_INET
8353 /* allocate a socket and port for it and use it */
8354 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8355 IPPROTO_TCP, &cfg->sock, 1);
8356 if (err) {
8357 kfree(cfg);
8358 return err;
8359 }
8360 if (inet_csk_get_port(cfg->sock->sk, port)) {
8361 sock_release(cfg->sock);
8362 kfree(cfg);
8363 return -EADDRINUSE;
8364 }
8365 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8366#else
8367 if (!port) {
8368 kfree(cfg);
8369 return -EINVAL;
8370 }
8371 cfg->src_port = port;
8372#endif
8373
8374 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8375 cfg->payload_len = data_size;
8376 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8377 memcpy((void *)cfg->payload,
8378 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8379 data_size);
8380 if (seq)
8381 cfg->payload_seq = *seq;
8382 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8383 cfg->wake_len = wake_size;
8384 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8385 memcpy((void *)cfg->wake_data,
8386 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8387 wake_size);
8388 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8389 data_size + wake_size;
8390 memcpy((void *)cfg->wake_mask,
8391 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8392 wake_mask_size);
8393 if (tok) {
8394 cfg->tokens_size = tokens_size;
8395 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8396 }
8397
8398 trig->tcp = cfg;
8399
8400 return 0;
8401}
8402
Johannes Bergff1b6e62011-05-04 15:37:28 +02008403static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8404{
8405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8406 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008407 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008408 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008409 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008410 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008411 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008412
Johannes Berg964dc9e2013-06-03 17:25:34 +02008413 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008414 return -EOPNOTSUPP;
8415
Johannes Bergae33bd82012-07-12 16:25:02 +02008416 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8417 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008418 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008419 goto set_wakeup;
8420 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008421
8422 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8423 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8424 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8425 nl80211_wowlan_policy);
8426 if (err)
8427 return err;
8428
8429 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8430 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8431 return -EINVAL;
8432 new_triggers.any = true;
8433 }
8434
8435 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8436 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8437 return -EINVAL;
8438 new_triggers.disconnect = true;
8439 }
8440
8441 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8442 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8443 return -EINVAL;
8444 new_triggers.magic_pkt = true;
8445 }
8446
Johannes Berg77dbbb12011-07-13 10:48:55 +02008447 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8448 return -EINVAL;
8449
8450 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8451 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8452 return -EINVAL;
8453 new_triggers.gtk_rekey_failure = true;
8454 }
8455
8456 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8457 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8458 return -EINVAL;
8459 new_triggers.eap_identity_req = true;
8460 }
8461
8462 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8463 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8464 return -EINVAL;
8465 new_triggers.four_way_handshake = true;
8466 }
8467
8468 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8469 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8470 return -EINVAL;
8471 new_triggers.rfkill_release = true;
8472 }
8473
Johannes Bergff1b6e62011-05-04 15:37:28 +02008474 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8475 struct nlattr *pat;
8476 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008477 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008478 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008479
8480 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8481 rem)
8482 n_patterns++;
8483 if (n_patterns > wowlan->n_patterns)
8484 return -EINVAL;
8485
8486 new_triggers.patterns = kcalloc(n_patterns,
8487 sizeof(new_triggers.patterns[0]),
8488 GFP_KERNEL);
8489 if (!new_triggers.patterns)
8490 return -ENOMEM;
8491
8492 new_triggers.n_patterns = n_patterns;
8493 i = 0;
8494
8495 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8496 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008497 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8498 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008499 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008500 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8501 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008502 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008503 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008504 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008505 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008506 goto error;
8507 if (pat_len > wowlan->pattern_max_len ||
8508 pat_len < wowlan->pattern_min_len)
8509 goto error;
8510
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008511 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008512 pkt_offset = 0;
8513 else
8514 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008515 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008516 if (pkt_offset > wowlan->max_pkt_offset)
8517 goto error;
8518 new_triggers.patterns[i].pkt_offset = pkt_offset;
8519
Johannes Bergff1b6e62011-05-04 15:37:28 +02008520 new_triggers.patterns[i].mask =
8521 kmalloc(mask_len + pat_len, GFP_KERNEL);
8522 if (!new_triggers.patterns[i].mask) {
8523 err = -ENOMEM;
8524 goto error;
8525 }
8526 new_triggers.patterns[i].pattern =
8527 new_triggers.patterns[i].mask + mask_len;
8528 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008529 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008530 mask_len);
8531 new_triggers.patterns[i].pattern_len = pat_len;
8532 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008533 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008534 pat_len);
8535 i++;
8536 }
8537 }
8538
Johannes Berg2a0e0472013-01-23 22:57:40 +01008539 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8540 err = nl80211_parse_wowlan_tcp(
8541 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8542 &new_triggers);
8543 if (err)
8544 goto error;
8545 }
8546
Johannes Bergae33bd82012-07-12 16:25:02 +02008547 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8548 if (!ntrig) {
8549 err = -ENOMEM;
8550 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008551 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008552 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008553 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008554
Johannes Bergae33bd82012-07-12 16:25:02 +02008555 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008556 if (rdev->ops->set_wakeup &&
8557 prev_enabled != !!rdev->wiphy.wowlan_config)
8558 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008559
Johannes Bergff1b6e62011-05-04 15:37:28 +02008560 return 0;
8561 error:
8562 for (i = 0; i < new_triggers.n_patterns; i++)
8563 kfree(new_triggers.patterns[i].mask);
8564 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008565 if (new_triggers.tcp && new_triggers.tcp->sock)
8566 sock_release(new_triggers.tcp->sock);
8567 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008568 return err;
8569}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008570#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008571
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07008572static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8573 struct cfg80211_registered_device *rdev)
8574{
8575 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8576 int i, j, pat_len;
8577 struct cfg80211_coalesce_rules *rule;
8578
8579 if (!rdev->coalesce->n_rules)
8580 return 0;
8581
8582 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8583 if (!nl_rules)
8584 return -ENOBUFS;
8585
8586 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8587 nl_rule = nla_nest_start(msg, i + 1);
8588 if (!nl_rule)
8589 return -ENOBUFS;
8590
8591 rule = &rdev->coalesce->rules[i];
8592 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8593 rule->delay))
8594 return -ENOBUFS;
8595
8596 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8597 rule->condition))
8598 return -ENOBUFS;
8599
8600 nl_pats = nla_nest_start(msg,
8601 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8602 if (!nl_pats)
8603 return -ENOBUFS;
8604
8605 for (j = 0; j < rule->n_patterns; j++) {
8606 nl_pat = nla_nest_start(msg, j + 1);
8607 if (!nl_pat)
8608 return -ENOBUFS;
8609 pat_len = rule->patterns[j].pattern_len;
8610 if (nla_put(msg, NL80211_PKTPAT_MASK,
8611 DIV_ROUND_UP(pat_len, 8),
8612 rule->patterns[j].mask) ||
8613 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8614 rule->patterns[j].pattern) ||
8615 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8616 rule->patterns[j].pkt_offset))
8617 return -ENOBUFS;
8618 nla_nest_end(msg, nl_pat);
8619 }
8620 nla_nest_end(msg, nl_pats);
8621 nla_nest_end(msg, nl_rule);
8622 }
8623 nla_nest_end(msg, nl_rules);
8624
8625 return 0;
8626}
8627
8628static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8629{
8630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8631 struct sk_buff *msg;
8632 void *hdr;
8633
8634 if (!rdev->wiphy.coalesce)
8635 return -EOPNOTSUPP;
8636
8637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8638 if (!msg)
8639 return -ENOMEM;
8640
8641 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8642 NL80211_CMD_GET_COALESCE);
8643 if (!hdr)
8644 goto nla_put_failure;
8645
8646 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8647 goto nla_put_failure;
8648
8649 genlmsg_end(msg, hdr);
8650 return genlmsg_reply(msg, info);
8651
8652nla_put_failure:
8653 nlmsg_free(msg);
8654 return -ENOBUFS;
8655}
8656
8657void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8658{
8659 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8660 int i, j;
8661 struct cfg80211_coalesce_rules *rule;
8662
8663 if (!coalesce)
8664 return;
8665
8666 for (i = 0; i < coalesce->n_rules; i++) {
8667 rule = &coalesce->rules[i];
8668 for (j = 0; j < rule->n_patterns; j++)
8669 kfree(rule->patterns[j].mask);
8670 kfree(rule->patterns);
8671 }
8672 kfree(coalesce->rules);
8673 kfree(coalesce);
8674 rdev->coalesce = NULL;
8675}
8676
8677static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8678 struct nlattr *rule,
8679 struct cfg80211_coalesce_rules *new_rule)
8680{
8681 int err, i;
8682 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8683 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8684 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8685 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8686
8687 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8688 nla_len(rule), nl80211_coalesce_policy);
8689 if (err)
8690 return err;
8691
8692 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8693 new_rule->delay =
8694 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8695 if (new_rule->delay > coalesce->max_delay)
8696 return -EINVAL;
8697
8698 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8699 new_rule->condition =
8700 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8701 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8702 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8703 return -EINVAL;
8704
8705 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8706 return -EINVAL;
8707
8708 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8709 rem)
8710 n_patterns++;
8711 if (n_patterns > coalesce->n_patterns)
8712 return -EINVAL;
8713
8714 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8715 GFP_KERNEL);
8716 if (!new_rule->patterns)
8717 return -ENOMEM;
8718
8719 new_rule->n_patterns = n_patterns;
8720 i = 0;
8721
8722 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8723 rem) {
8724 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8725 nla_len(pat), NULL);
8726 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8727 !pat_tb[NL80211_PKTPAT_PATTERN])
8728 return -EINVAL;
8729 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8730 mask_len = DIV_ROUND_UP(pat_len, 8);
8731 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8732 return -EINVAL;
8733 if (pat_len > coalesce->pattern_max_len ||
8734 pat_len < coalesce->pattern_min_len)
8735 return -EINVAL;
8736
8737 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8738 pkt_offset = 0;
8739 else
8740 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8741 if (pkt_offset > coalesce->max_pkt_offset)
8742 return -EINVAL;
8743 new_rule->patterns[i].pkt_offset = pkt_offset;
8744
8745 new_rule->patterns[i].mask =
8746 kmalloc(mask_len + pat_len, GFP_KERNEL);
8747 if (!new_rule->patterns[i].mask)
8748 return -ENOMEM;
8749 new_rule->patterns[i].pattern =
8750 new_rule->patterns[i].mask + mask_len;
8751 memcpy(new_rule->patterns[i].mask,
8752 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8753 new_rule->patterns[i].pattern_len = pat_len;
8754 memcpy(new_rule->patterns[i].pattern,
8755 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8756 i++;
8757 }
8758
8759 return 0;
8760}
8761
8762static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8763{
8764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8765 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8766 struct cfg80211_coalesce new_coalesce = {};
8767 struct cfg80211_coalesce *n_coalesce;
8768 int err, rem_rule, n_rules = 0, i, j;
8769 struct nlattr *rule;
8770 struct cfg80211_coalesce_rules *tmp_rule;
8771
8772 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8773 return -EOPNOTSUPP;
8774
8775 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8776 cfg80211_rdev_free_coalesce(rdev);
8777 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8778 return 0;
8779 }
8780
8781 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8782 rem_rule)
8783 n_rules++;
8784 if (n_rules > coalesce->n_rules)
8785 return -EINVAL;
8786
8787 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8788 GFP_KERNEL);
8789 if (!new_coalesce.rules)
8790 return -ENOMEM;
8791
8792 new_coalesce.n_rules = n_rules;
8793 i = 0;
8794
8795 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8796 rem_rule) {
8797 err = nl80211_parse_coalesce_rule(rdev, rule,
8798 &new_coalesce.rules[i]);
8799 if (err)
8800 goto error;
8801
8802 i++;
8803 }
8804
8805 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8806 if (err)
8807 goto error;
8808
8809 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8810 if (!n_coalesce) {
8811 err = -ENOMEM;
8812 goto error;
8813 }
8814 cfg80211_rdev_free_coalesce(rdev);
8815 rdev->coalesce = n_coalesce;
8816
8817 return 0;
8818error:
8819 for (i = 0; i < new_coalesce.n_rules; i++) {
8820 tmp_rule = &new_coalesce.rules[i];
8821 for (j = 0; j < tmp_rule->n_patterns; j++)
8822 kfree(tmp_rule->patterns[j].mask);
8823 kfree(tmp_rule->patterns);
8824 }
8825 kfree(new_coalesce.rules);
8826
8827 return err;
8828}
8829
Johannes Berge5497d72011-07-05 16:35:40 +02008830static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8831{
8832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8833 struct net_device *dev = info->user_ptr[1];
8834 struct wireless_dev *wdev = dev->ieee80211_ptr;
8835 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8836 struct cfg80211_gtk_rekey_data rekey_data;
8837 int err;
8838
8839 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8840 return -EINVAL;
8841
8842 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8843 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8844 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8845 nl80211_rekey_policy);
8846 if (err)
8847 return err;
8848
8849 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8850 return -ERANGE;
8851 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8852 return -ERANGE;
8853 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8854 return -ERANGE;
8855
8856 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8857 NL80211_KEK_LEN);
8858 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8859 NL80211_KCK_LEN);
8860 memcpy(rekey_data.replay_ctr,
8861 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8862 NL80211_REPLAY_CTR_LEN);
8863
8864 wdev_lock(wdev);
8865 if (!wdev->current_bss) {
8866 err = -ENOTCONN;
8867 goto out;
8868 }
8869
8870 if (!rdev->ops->set_rekey_data) {
8871 err = -EOPNOTSUPP;
8872 goto out;
8873 }
8874
Hila Gonene35e4d22012-06-27 17:19:42 +03008875 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008876 out:
8877 wdev_unlock(wdev);
8878 return err;
8879}
8880
Johannes Berg28946da2011-11-04 11:18:12 +01008881static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8882 struct genl_info *info)
8883{
8884 struct net_device *dev = info->user_ptr[1];
8885 struct wireless_dev *wdev = dev->ieee80211_ptr;
8886
8887 if (wdev->iftype != NL80211_IFTYPE_AP &&
8888 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8889 return -EINVAL;
8890
Eric W. Biederman15e47302012-09-07 20:12:54 +00008891 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008892 return -EBUSY;
8893
Eric W. Biederman15e47302012-09-07 20:12:54 +00008894 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008895 return 0;
8896}
8897
Johannes Berg7f6cf312011-11-04 11:18:15 +01008898static int nl80211_probe_client(struct sk_buff *skb,
8899 struct genl_info *info)
8900{
8901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8902 struct net_device *dev = info->user_ptr[1];
8903 struct wireless_dev *wdev = dev->ieee80211_ptr;
8904 struct sk_buff *msg;
8905 void *hdr;
8906 const u8 *addr;
8907 u64 cookie;
8908 int err;
8909
8910 if (wdev->iftype != NL80211_IFTYPE_AP &&
8911 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8912 return -EOPNOTSUPP;
8913
8914 if (!info->attrs[NL80211_ATTR_MAC])
8915 return -EINVAL;
8916
8917 if (!rdev->ops->probe_client)
8918 return -EOPNOTSUPP;
8919
8920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8921 if (!msg)
8922 return -ENOMEM;
8923
Eric W. Biederman15e47302012-09-07 20:12:54 +00008924 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008925 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008926 if (!hdr) {
8927 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008928 goto free_msg;
8929 }
8930
8931 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8932
Hila Gonene35e4d22012-06-27 17:19:42 +03008933 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008934 if (err)
8935 goto free_msg;
8936
David S. Miller9360ffd2012-03-29 04:41:26 -04008937 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8938 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008939
8940 genlmsg_end(msg, hdr);
8941
8942 return genlmsg_reply(msg, info);
8943
8944 nla_put_failure:
8945 err = -ENOBUFS;
8946 free_msg:
8947 nlmsg_free(msg);
8948 return err;
8949}
8950
Johannes Berg5e760232011-11-04 11:18:17 +01008951static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8952{
8953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008954 struct cfg80211_beacon_registration *reg, *nreg;
8955 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008956
8957 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8958 return -EOPNOTSUPP;
8959
Ben Greear37c73b52012-10-26 14:49:25 -07008960 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8961 if (!nreg)
8962 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008963
Ben Greear37c73b52012-10-26 14:49:25 -07008964 /* First, check if already registered. */
8965 spin_lock_bh(&rdev->beacon_registrations_lock);
8966 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8967 if (reg->nlportid == info->snd_portid) {
8968 rv = -EALREADY;
8969 goto out_err;
8970 }
8971 }
8972 /* Add it to the list */
8973 nreg->nlportid = info->snd_portid;
8974 list_add(&nreg->list, &rdev->beacon_registrations);
8975
8976 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008977
8978 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008979out_err:
8980 spin_unlock_bh(&rdev->beacon_registrations_lock);
8981 kfree(nreg);
8982 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008983}
8984
Johannes Berg98104fde2012-06-16 00:19:54 +02008985static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8986{
8987 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8988 struct wireless_dev *wdev = info->user_ptr[1];
8989 int err;
8990
8991 if (!rdev->ops->start_p2p_device)
8992 return -EOPNOTSUPP;
8993
8994 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8995 return -EOPNOTSUPP;
8996
8997 if (wdev->p2p_started)
8998 return 0;
8999
Johannes Berg98104fde2012-06-16 00:19:54 +02009000 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02009001 if (err)
9002 return err;
9003
Johannes Bergeeb126e2012-10-23 15:16:50 +02009004 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009005 if (err)
9006 return err;
9007
9008 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009009 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009010
9011 return 0;
9012}
9013
9014static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9015{
9016 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9017 struct wireless_dev *wdev = info->user_ptr[1];
9018
9019 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9020 return -EOPNOTSUPP;
9021
9022 if (!rdev->ops->stop_p2p_device)
9023 return -EOPNOTSUPP;
9024
Johannes Bergf9f47522013-03-19 15:04:07 +01009025 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009026
9027 return 0;
9028}
9029
Johannes Berg3713b4e2013-02-14 16:19:38 +01009030static int nl80211_get_protocol_features(struct sk_buff *skb,
9031 struct genl_info *info)
9032{
9033 void *hdr;
9034 struct sk_buff *msg;
9035
9036 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9037 if (!msg)
9038 return -ENOMEM;
9039
9040 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9041 NL80211_CMD_GET_PROTOCOL_FEATURES);
9042 if (!hdr)
9043 goto nla_put_failure;
9044
9045 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9046 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9047 goto nla_put_failure;
9048
9049 genlmsg_end(msg, hdr);
9050 return genlmsg_reply(msg, info);
9051
9052 nla_put_failure:
9053 kfree_skb(msg);
9054 return -ENOBUFS;
9055}
9056
Jouni Malinen355199e2013-02-27 17:14:27 +02009057static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9058{
9059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9060 struct cfg80211_update_ft_ies_params ft_params;
9061 struct net_device *dev = info->user_ptr[1];
9062
9063 if (!rdev->ops->update_ft_ies)
9064 return -EOPNOTSUPP;
9065
9066 if (!info->attrs[NL80211_ATTR_MDID] ||
9067 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9068 return -EINVAL;
9069
9070 memset(&ft_params, 0, sizeof(ft_params));
9071 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9072 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9073 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9074
9075 return rdev_update_ft_ies(rdev, dev, &ft_params);
9076}
9077
Arend van Spriel5de17982013-04-18 15:49:00 +02009078static int nl80211_crit_protocol_start(struct sk_buff *skb,
9079 struct genl_info *info)
9080{
9081 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9082 struct wireless_dev *wdev = info->user_ptr[1];
9083 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9084 u16 duration;
9085 int ret;
9086
9087 if (!rdev->ops->crit_proto_start)
9088 return -EOPNOTSUPP;
9089
9090 if (WARN_ON(!rdev->ops->crit_proto_stop))
9091 return -EINVAL;
9092
9093 if (rdev->crit_proto_nlportid)
9094 return -EBUSY;
9095
9096 /* determine protocol if provided */
9097 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9098 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9099
9100 if (proto >= NUM_NL80211_CRIT_PROTO)
9101 return -EINVAL;
9102
9103 /* timeout must be provided */
9104 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9105 return -EINVAL;
9106
9107 duration =
9108 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9109
9110 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9111 return -ERANGE;
9112
9113 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9114 if (!ret)
9115 rdev->crit_proto_nlportid = info->snd_portid;
9116
9117 return ret;
9118}
9119
9120static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9121 struct genl_info *info)
9122{
9123 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9124 struct wireless_dev *wdev = info->user_ptr[1];
9125
9126 if (!rdev->ops->crit_proto_stop)
9127 return -EOPNOTSUPP;
9128
9129 if (rdev->crit_proto_nlportid) {
9130 rdev->crit_proto_nlportid = 0;
9131 rdev_crit_proto_stop(rdev, wdev);
9132 }
9133 return 0;
9134}
9135
Johannes Bergad7e7182013-11-13 13:37:47 +01009136static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9137{
9138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9139 struct wireless_dev *wdev =
9140 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9141 int i, err;
9142 u32 vid, subcmd;
9143
9144 if (!rdev->wiphy.vendor_commands)
9145 return -EOPNOTSUPP;
9146
9147 if (IS_ERR(wdev)) {
9148 err = PTR_ERR(wdev);
9149 if (err != -EINVAL)
9150 return err;
9151 wdev = NULL;
9152 } else if (wdev->wiphy != &rdev->wiphy) {
9153 return -EINVAL;
9154 }
9155
9156 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9157 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9158 return -EINVAL;
9159
9160 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9161 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9162 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9163 const struct wiphy_vendor_command *vcmd;
9164 void *data = NULL;
9165 int len = 0;
9166
9167 vcmd = &rdev->wiphy.vendor_commands[i];
9168
9169 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9170 continue;
9171
9172 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9173 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9174 if (!wdev)
9175 return -EINVAL;
9176 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9177 !wdev->netdev)
9178 return -EINVAL;
9179
9180 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9181 if (wdev->netdev &&
9182 !netif_running(wdev->netdev))
9183 return -ENETDOWN;
9184 if (!wdev->netdev && !wdev->p2p_started)
9185 return -ENETDOWN;
9186 }
9187 } else {
9188 wdev = NULL;
9189 }
9190
9191 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9192 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9193 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9194 }
9195
9196 rdev->cur_cmd_info = info;
9197 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9198 data, len);
9199 rdev->cur_cmd_info = NULL;
9200 return err;
9201 }
9202
9203 return -EOPNOTSUPP;
9204}
9205
9206struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9207 enum nl80211_commands cmd,
9208 enum nl80211_attrs attr,
9209 int approxlen)
9210{
9211 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9212
9213 if (WARN_ON(!rdev->cur_cmd_info))
9214 return NULL;
9215
9216 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9217 rdev->cur_cmd_info->snd_portid,
9218 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009219 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009220}
9221EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9222
9223int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9224{
9225 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9226 void *hdr = ((void **)skb->cb)[1];
9227 struct nlattr *data = ((void **)skb->cb)[2];
9228
9229 if (WARN_ON(!rdev->cur_cmd_info)) {
9230 kfree_skb(skb);
9231 return -EINVAL;
9232 }
9233
9234 nla_nest_end(skb, data);
9235 genlmsg_end(skb, hdr);
9236 return genlmsg_reply(skb, rdev->cur_cmd_info);
9237}
9238EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9239
9240
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009241static int nl80211_set_qos_map(struct sk_buff *skb,
9242 struct genl_info *info)
9243{
9244 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9245 struct cfg80211_qos_map *qos_map = NULL;
9246 struct net_device *dev = info->user_ptr[1];
9247 u8 *pos, len, num_des, des_len, des;
9248 int ret;
9249
9250 if (!rdev->ops->set_qos_map)
9251 return -EOPNOTSUPP;
9252
9253 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9254 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9255 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9256
9257 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9258 len > IEEE80211_QOS_MAP_LEN_MAX)
9259 return -EINVAL;
9260
9261 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9262 if (!qos_map)
9263 return -ENOMEM;
9264
9265 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9266 if (num_des) {
9267 des_len = num_des *
9268 sizeof(struct cfg80211_dscp_exception);
9269 memcpy(qos_map->dscp_exception, pos, des_len);
9270 qos_map->num_des = num_des;
9271 for (des = 0; des < num_des; des++) {
9272 if (qos_map->dscp_exception[des].up > 7) {
9273 kfree(qos_map);
9274 return -EINVAL;
9275 }
9276 }
9277 pos += des_len;
9278 }
9279 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9280 }
9281
9282 wdev_lock(dev->ieee80211_ptr);
9283 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9284 if (!ret)
9285 ret = rdev_set_qos_map(rdev, dev, qos_map);
9286 wdev_unlock(dev->ieee80211_ptr);
9287
9288 kfree(qos_map);
9289 return ret;
9290}
9291
Johannes Berg4c476992010-10-04 21:36:35 +02009292#define NL80211_FLAG_NEED_WIPHY 0x01
9293#define NL80211_FLAG_NEED_NETDEV 0x02
9294#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009295#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9296#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9297 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009298#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009299/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009300#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9301 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009302
Johannes Bergf84f7712013-11-14 17:14:45 +01009303static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009304 struct genl_info *info)
9305{
9306 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009307 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009308 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009309 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9310
9311 if (rtnl)
9312 rtnl_lock();
9313
9314 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009315 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009316 if (IS_ERR(rdev)) {
9317 if (rtnl)
9318 rtnl_unlock();
9319 return PTR_ERR(rdev);
9320 }
9321 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009322 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9323 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009324 ASSERT_RTNL();
9325
Johannes Berg89a54e42012-06-15 14:33:17 +02009326 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9327 info->attrs);
9328 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009329 if (rtnl)
9330 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009331 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009332 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009333
Johannes Berg89a54e42012-06-15 14:33:17 +02009334 dev = wdev->netdev;
9335 rdev = wiphy_to_dev(wdev->wiphy);
9336
Johannes Berg1bf614e2012-06-15 15:23:36 +02009337 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9338 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009339 if (rtnl)
9340 rtnl_unlock();
9341 return -EINVAL;
9342 }
9343
9344 info->user_ptr[1] = dev;
9345 } else {
9346 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009347 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009348
Johannes Berg1bf614e2012-06-15 15:23:36 +02009349 if (dev) {
9350 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9351 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009352 if (rtnl)
9353 rtnl_unlock();
9354 return -ENETDOWN;
9355 }
9356
9357 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009358 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9359 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009360 if (rtnl)
9361 rtnl_unlock();
9362 return -ENETDOWN;
9363 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009364 }
9365
Johannes Berg4c476992010-10-04 21:36:35 +02009366 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009367 }
9368
9369 return 0;
9370}
9371
Johannes Bergf84f7712013-11-14 17:14:45 +01009372static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009373 struct genl_info *info)
9374{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009375 if (info->user_ptr[1]) {
9376 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9377 struct wireless_dev *wdev = info->user_ptr[1];
9378
9379 if (wdev->netdev)
9380 dev_put(wdev->netdev);
9381 } else {
9382 dev_put(info->user_ptr[1]);
9383 }
9384 }
Johannes Berg4c476992010-10-04 21:36:35 +02009385 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9386 rtnl_unlock();
9387}
9388
Johannes Berg4534de82013-11-14 17:14:46 +01009389static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009390 {
9391 .cmd = NL80211_CMD_GET_WIPHY,
9392 .doit = nl80211_get_wiphy,
9393 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009394 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009395 .policy = nl80211_policy,
9396 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009397 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9398 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009399 },
9400 {
9401 .cmd = NL80211_CMD_SET_WIPHY,
9402 .doit = nl80211_set_wiphy,
9403 .policy = nl80211_policy,
9404 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009405 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009406 },
9407 {
9408 .cmd = NL80211_CMD_GET_INTERFACE,
9409 .doit = nl80211_get_interface,
9410 .dumpit = nl80211_dump_interface,
9411 .policy = nl80211_policy,
9412 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009413 .internal_flags = NL80211_FLAG_NEED_WDEV |
9414 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009415 },
9416 {
9417 .cmd = NL80211_CMD_SET_INTERFACE,
9418 .doit = nl80211_set_interface,
9419 .policy = nl80211_policy,
9420 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009421 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9422 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009423 },
9424 {
9425 .cmd = NL80211_CMD_NEW_INTERFACE,
9426 .doit = nl80211_new_interface,
9427 .policy = nl80211_policy,
9428 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009429 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9430 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009431 },
9432 {
9433 .cmd = NL80211_CMD_DEL_INTERFACE,
9434 .doit = nl80211_del_interface,
9435 .policy = nl80211_policy,
9436 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009437 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009438 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009439 },
Johannes Berg41ade002007-12-19 02:03:29 +01009440 {
9441 .cmd = NL80211_CMD_GET_KEY,
9442 .doit = nl80211_get_key,
9443 .policy = nl80211_policy,
9444 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009445 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009446 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009447 },
9448 {
9449 .cmd = NL80211_CMD_SET_KEY,
9450 .doit = nl80211_set_key,
9451 .policy = nl80211_policy,
9452 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009453 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009454 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009455 },
9456 {
9457 .cmd = NL80211_CMD_NEW_KEY,
9458 .doit = nl80211_new_key,
9459 .policy = nl80211_policy,
9460 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009461 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009462 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009463 },
9464 {
9465 .cmd = NL80211_CMD_DEL_KEY,
9466 .doit = nl80211_del_key,
9467 .policy = nl80211_policy,
9468 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009470 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009471 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009472 {
9473 .cmd = NL80211_CMD_SET_BEACON,
9474 .policy = nl80211_policy,
9475 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009476 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009478 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009479 },
9480 {
Johannes Berg88600202012-02-13 15:17:18 +01009481 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009482 .policy = nl80211_policy,
9483 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009484 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009486 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009487 },
9488 {
Johannes Berg88600202012-02-13 15:17:18 +01009489 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009490 .policy = nl80211_policy,
9491 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009492 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009494 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009495 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009496 {
9497 .cmd = NL80211_CMD_GET_STATION,
9498 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009499 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009500 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009501 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9502 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009503 },
9504 {
9505 .cmd = NL80211_CMD_SET_STATION,
9506 .doit = nl80211_set_station,
9507 .policy = nl80211_policy,
9508 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009510 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009511 },
9512 {
9513 .cmd = NL80211_CMD_NEW_STATION,
9514 .doit = nl80211_new_station,
9515 .policy = nl80211_policy,
9516 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009518 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009519 },
9520 {
9521 .cmd = NL80211_CMD_DEL_STATION,
9522 .doit = nl80211_del_station,
9523 .policy = nl80211_policy,
9524 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009526 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009527 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009528 {
9529 .cmd = NL80211_CMD_GET_MPATH,
9530 .doit = nl80211_get_mpath,
9531 .dumpit = nl80211_dump_mpath,
9532 .policy = nl80211_policy,
9533 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009535 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009536 },
9537 {
9538 .cmd = NL80211_CMD_SET_MPATH,
9539 .doit = nl80211_set_mpath,
9540 .policy = nl80211_policy,
9541 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009543 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009544 },
9545 {
9546 .cmd = NL80211_CMD_NEW_MPATH,
9547 .doit = nl80211_new_mpath,
9548 .policy = nl80211_policy,
9549 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009551 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009552 },
9553 {
9554 .cmd = NL80211_CMD_DEL_MPATH,
9555 .doit = nl80211_del_mpath,
9556 .policy = nl80211_policy,
9557 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009559 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009560 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009561 {
9562 .cmd = NL80211_CMD_SET_BSS,
9563 .doit = nl80211_set_bss,
9564 .policy = nl80211_policy,
9565 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009567 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009568 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009569 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009570 .cmd = NL80211_CMD_GET_REG,
9571 .doit = nl80211_get_reg,
9572 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009573 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009574 /* can be retrieved by unprivileged users */
9575 },
9576 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009577 .cmd = NL80211_CMD_SET_REG,
9578 .doit = nl80211_set_reg,
9579 .policy = nl80211_policy,
9580 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009581 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009582 },
9583 {
9584 .cmd = NL80211_CMD_REQ_SET_REG,
9585 .doit = nl80211_req_set_reg,
9586 .policy = nl80211_policy,
9587 .flags = GENL_ADMIN_PERM,
9588 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009589 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009590 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9591 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009592 .policy = nl80211_policy,
9593 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009594 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009595 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009596 },
9597 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009598 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9599 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009600 .policy = nl80211_policy,
9601 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009603 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009604 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009605 {
Johannes Berg2a519312009-02-10 21:25:55 +01009606 .cmd = NL80211_CMD_TRIGGER_SCAN,
9607 .doit = nl80211_trigger_scan,
9608 .policy = nl80211_policy,
9609 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009610 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009611 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009612 },
9613 {
9614 .cmd = NL80211_CMD_GET_SCAN,
9615 .policy = nl80211_policy,
9616 .dumpit = nl80211_dump_scan,
9617 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009618 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009619 .cmd = NL80211_CMD_START_SCHED_SCAN,
9620 .doit = nl80211_start_sched_scan,
9621 .policy = nl80211_policy,
9622 .flags = GENL_ADMIN_PERM,
9623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9624 NL80211_FLAG_NEED_RTNL,
9625 },
9626 {
9627 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9628 .doit = nl80211_stop_sched_scan,
9629 .policy = nl80211_policy,
9630 .flags = GENL_ADMIN_PERM,
9631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9632 NL80211_FLAG_NEED_RTNL,
9633 },
9634 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009635 .cmd = NL80211_CMD_AUTHENTICATE,
9636 .doit = nl80211_authenticate,
9637 .policy = nl80211_policy,
9638 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009639 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009640 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009641 },
9642 {
9643 .cmd = NL80211_CMD_ASSOCIATE,
9644 .doit = nl80211_associate,
9645 .policy = nl80211_policy,
9646 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009648 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009649 },
9650 {
9651 .cmd = NL80211_CMD_DEAUTHENTICATE,
9652 .doit = nl80211_deauthenticate,
9653 .policy = nl80211_policy,
9654 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009655 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009656 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009657 },
9658 {
9659 .cmd = NL80211_CMD_DISASSOCIATE,
9660 .doit = nl80211_disassociate,
9661 .policy = nl80211_policy,
9662 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009663 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009664 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009665 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009666 {
9667 .cmd = NL80211_CMD_JOIN_IBSS,
9668 .doit = nl80211_join_ibss,
9669 .policy = nl80211_policy,
9670 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009671 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009672 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009673 },
9674 {
9675 .cmd = NL80211_CMD_LEAVE_IBSS,
9676 .doit = nl80211_leave_ibss,
9677 .policy = nl80211_policy,
9678 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009679 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009680 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009681 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009682#ifdef CONFIG_NL80211_TESTMODE
9683 {
9684 .cmd = NL80211_CMD_TESTMODE,
9685 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009686 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009687 .policy = nl80211_policy,
9688 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009689 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9690 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009691 },
9692#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009693 {
9694 .cmd = NL80211_CMD_CONNECT,
9695 .doit = nl80211_connect,
9696 .policy = nl80211_policy,
9697 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009698 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009699 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009700 },
9701 {
9702 .cmd = NL80211_CMD_DISCONNECT,
9703 .doit = nl80211_disconnect,
9704 .policy = nl80211_policy,
9705 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009706 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009707 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009708 },
Johannes Berg463d0182009-07-14 00:33:35 +02009709 {
9710 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9711 .doit = nl80211_wiphy_netns,
9712 .policy = nl80211_policy,
9713 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009714 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9715 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009716 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009717 {
9718 .cmd = NL80211_CMD_GET_SURVEY,
9719 .policy = nl80211_policy,
9720 .dumpit = nl80211_dump_survey,
9721 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009722 {
9723 .cmd = NL80211_CMD_SET_PMKSA,
9724 .doit = nl80211_setdel_pmksa,
9725 .policy = nl80211_policy,
9726 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009727 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009728 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009729 },
9730 {
9731 .cmd = NL80211_CMD_DEL_PMKSA,
9732 .doit = nl80211_setdel_pmksa,
9733 .policy = nl80211_policy,
9734 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009735 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009736 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009737 },
9738 {
9739 .cmd = NL80211_CMD_FLUSH_PMKSA,
9740 .doit = nl80211_flush_pmksa,
9741 .policy = nl80211_policy,
9742 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009743 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009744 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009745 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009746 {
9747 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9748 .doit = nl80211_remain_on_channel,
9749 .policy = nl80211_policy,
9750 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009751 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009752 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009753 },
9754 {
9755 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9756 .doit = nl80211_cancel_remain_on_channel,
9757 .policy = nl80211_policy,
9758 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009759 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009760 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009761 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009762 {
9763 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9764 .doit = nl80211_set_tx_bitrate_mask,
9765 .policy = nl80211_policy,
9766 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9768 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009769 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009770 {
Johannes Berg2e161f782010-08-12 15:38:38 +02009771 .cmd = NL80211_CMD_REGISTER_FRAME,
9772 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009773 .policy = nl80211_policy,
9774 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009775 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009776 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009777 },
9778 {
Johannes Berg2e161f782010-08-12 15:38:38 +02009779 .cmd = NL80211_CMD_FRAME,
9780 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009781 .policy = nl80211_policy,
9782 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009783 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009784 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009785 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009786 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009787 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9788 .doit = nl80211_tx_mgmt_cancel_wait,
9789 .policy = nl80211_policy,
9790 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009791 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009792 NL80211_FLAG_NEED_RTNL,
9793 },
9794 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009795 .cmd = NL80211_CMD_SET_POWER_SAVE,
9796 .doit = nl80211_set_power_save,
9797 .policy = nl80211_policy,
9798 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009799 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9800 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009801 },
9802 {
9803 .cmd = NL80211_CMD_GET_POWER_SAVE,
9804 .doit = nl80211_get_power_save,
9805 .policy = nl80211_policy,
9806 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009807 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9808 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009809 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009810 {
9811 .cmd = NL80211_CMD_SET_CQM,
9812 .doit = nl80211_set_cqm,
9813 .policy = nl80211_policy,
9814 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009815 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9816 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009817 },
Johannes Bergf444de02010-05-05 15:25:02 +02009818 {
9819 .cmd = NL80211_CMD_SET_CHANNEL,
9820 .doit = nl80211_set_channel,
9821 .policy = nl80211_policy,
9822 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009823 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9824 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009825 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009826 {
9827 .cmd = NL80211_CMD_SET_WDS_PEER,
9828 .doit = nl80211_set_wds_peer,
9829 .policy = nl80211_policy,
9830 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009831 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9832 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009833 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009834 {
9835 .cmd = NL80211_CMD_JOIN_MESH,
9836 .doit = nl80211_join_mesh,
9837 .policy = nl80211_policy,
9838 .flags = GENL_ADMIN_PERM,
9839 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9840 NL80211_FLAG_NEED_RTNL,
9841 },
9842 {
9843 .cmd = NL80211_CMD_LEAVE_MESH,
9844 .doit = nl80211_leave_mesh,
9845 .policy = nl80211_policy,
9846 .flags = GENL_ADMIN_PERM,
9847 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9848 NL80211_FLAG_NEED_RTNL,
9849 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009850#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009851 {
9852 .cmd = NL80211_CMD_GET_WOWLAN,
9853 .doit = nl80211_get_wowlan,
9854 .policy = nl80211_policy,
9855 /* can be retrieved by unprivileged users */
9856 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9857 NL80211_FLAG_NEED_RTNL,
9858 },
9859 {
9860 .cmd = NL80211_CMD_SET_WOWLAN,
9861 .doit = nl80211_set_wowlan,
9862 .policy = nl80211_policy,
9863 .flags = GENL_ADMIN_PERM,
9864 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9865 NL80211_FLAG_NEED_RTNL,
9866 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009867#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009868 {
9869 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9870 .doit = nl80211_set_rekey_data,
9871 .policy = nl80211_policy,
9872 .flags = GENL_ADMIN_PERM,
9873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9874 NL80211_FLAG_NEED_RTNL,
9875 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009876 {
9877 .cmd = NL80211_CMD_TDLS_MGMT,
9878 .doit = nl80211_tdls_mgmt,
9879 .policy = nl80211_policy,
9880 .flags = GENL_ADMIN_PERM,
9881 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9882 NL80211_FLAG_NEED_RTNL,
9883 },
9884 {
9885 .cmd = NL80211_CMD_TDLS_OPER,
9886 .doit = nl80211_tdls_oper,
9887 .policy = nl80211_policy,
9888 .flags = GENL_ADMIN_PERM,
9889 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9890 NL80211_FLAG_NEED_RTNL,
9891 },
Johannes Berg28946da2011-11-04 11:18:12 +01009892 {
9893 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9894 .doit = nl80211_register_unexpected_frame,
9895 .policy = nl80211_policy,
9896 .flags = GENL_ADMIN_PERM,
9897 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9898 NL80211_FLAG_NEED_RTNL,
9899 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009900 {
9901 .cmd = NL80211_CMD_PROBE_CLIENT,
9902 .doit = nl80211_probe_client,
9903 .policy = nl80211_policy,
9904 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009905 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009906 NL80211_FLAG_NEED_RTNL,
9907 },
Johannes Berg5e760232011-11-04 11:18:17 +01009908 {
9909 .cmd = NL80211_CMD_REGISTER_BEACONS,
9910 .doit = nl80211_register_beacons,
9911 .policy = nl80211_policy,
9912 .flags = GENL_ADMIN_PERM,
9913 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9914 NL80211_FLAG_NEED_RTNL,
9915 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009916 {
9917 .cmd = NL80211_CMD_SET_NOACK_MAP,
9918 .doit = nl80211_set_noack_map,
9919 .policy = nl80211_policy,
9920 .flags = GENL_ADMIN_PERM,
9921 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9922 NL80211_FLAG_NEED_RTNL,
9923 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009924 {
9925 .cmd = NL80211_CMD_START_P2P_DEVICE,
9926 .doit = nl80211_start_p2p_device,
9927 .policy = nl80211_policy,
9928 .flags = GENL_ADMIN_PERM,
9929 .internal_flags = NL80211_FLAG_NEED_WDEV |
9930 NL80211_FLAG_NEED_RTNL,
9931 },
9932 {
9933 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9934 .doit = nl80211_stop_p2p_device,
9935 .policy = nl80211_policy,
9936 .flags = GENL_ADMIN_PERM,
9937 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9938 NL80211_FLAG_NEED_RTNL,
9939 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009940 {
9941 .cmd = NL80211_CMD_SET_MCAST_RATE,
9942 .doit = nl80211_set_mcast_rate,
9943 .policy = nl80211_policy,
9944 .flags = GENL_ADMIN_PERM,
9945 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9946 NL80211_FLAG_NEED_RTNL,
9947 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309948 {
9949 .cmd = NL80211_CMD_SET_MAC_ACL,
9950 .doit = nl80211_set_mac_acl,
9951 .policy = nl80211_policy,
9952 .flags = GENL_ADMIN_PERM,
9953 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9954 NL80211_FLAG_NEED_RTNL,
9955 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009956 {
9957 .cmd = NL80211_CMD_RADAR_DETECT,
9958 .doit = nl80211_start_radar_detection,
9959 .policy = nl80211_policy,
9960 .flags = GENL_ADMIN_PERM,
9961 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9962 NL80211_FLAG_NEED_RTNL,
9963 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009964 {
9965 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9966 .doit = nl80211_get_protocol_features,
9967 .policy = nl80211_policy,
9968 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009969 {
9970 .cmd = NL80211_CMD_UPDATE_FT_IES,
9971 .doit = nl80211_update_ft_ies,
9972 .policy = nl80211_policy,
9973 .flags = GENL_ADMIN_PERM,
9974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9975 NL80211_FLAG_NEED_RTNL,
9976 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009977 {
9978 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9979 .doit = nl80211_crit_protocol_start,
9980 .policy = nl80211_policy,
9981 .flags = GENL_ADMIN_PERM,
9982 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9983 NL80211_FLAG_NEED_RTNL,
9984 },
9985 {
9986 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9987 .doit = nl80211_crit_protocol_stop,
9988 .policy = nl80211_policy,
9989 .flags = GENL_ADMIN_PERM,
9990 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9991 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07009992 },
9993 {
9994 .cmd = NL80211_CMD_GET_COALESCE,
9995 .doit = nl80211_get_coalesce,
9996 .policy = nl80211_policy,
9997 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9998 NL80211_FLAG_NEED_RTNL,
9999 },
10000 {
10001 .cmd = NL80211_CMD_SET_COALESCE,
10002 .doit = nl80211_set_coalesce,
10003 .policy = nl80211_policy,
10004 .flags = GENL_ADMIN_PERM,
10005 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10006 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010007 },
10008 {
10009 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10010 .doit = nl80211_channel_switch,
10011 .policy = nl80211_policy,
10012 .flags = GENL_ADMIN_PERM,
10013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10014 NL80211_FLAG_NEED_RTNL,
10015 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010016 {
10017 .cmd = NL80211_CMD_VENDOR,
10018 .doit = nl80211_vendor_cmd,
10019 .policy = nl80211_policy,
10020 .flags = GENL_ADMIN_PERM,
10021 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10022 NL80211_FLAG_NEED_RTNL,
10023 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010024 {
10025 .cmd = NL80211_CMD_SET_QOS_MAP,
10026 .doit = nl80211_set_qos_map,
10027 .policy = nl80211_policy,
10028 .flags = GENL_ADMIN_PERM,
10029 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10030 NL80211_FLAG_NEED_RTNL,
10031 },
Johannes Berg55682962007-09-20 13:09:35 -040010032};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010033
Johannes Berg55682962007-09-20 13:09:35 -040010034/* notification functions */
10035
10036void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10037{
10038 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010039 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010040
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010041 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010042 if (!msg)
10043 return;
10044
Johannes Berg86e8cf92013-06-19 10:57:22 +020010045 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010046 nlmsg_free(msg);
10047 return;
10048 }
10049
Johannes Berg68eb5502013-11-19 15:19:38 +010010050 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010051 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010052}
10053
Johannes Berg362a4152009-05-24 16:43:15 +020010054static int nl80211_add_scan_req(struct sk_buff *msg,
10055 struct cfg80211_registered_device *rdev)
10056{
10057 struct cfg80211_scan_request *req = rdev->scan_req;
10058 struct nlattr *nest;
10059 int i;
10060
10061 if (WARN_ON(!req))
10062 return 0;
10063
10064 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10065 if (!nest)
10066 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010067 for (i = 0; i < req->n_ssids; i++) {
10068 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10069 goto nla_put_failure;
10070 }
Johannes Berg362a4152009-05-24 16:43:15 +020010071 nla_nest_end(msg, nest);
10072
10073 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10074 if (!nest)
10075 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010076 for (i = 0; i < req->n_channels; i++) {
10077 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10078 goto nla_put_failure;
10079 }
Johannes Berg362a4152009-05-24 16:43:15 +020010080 nla_nest_end(msg, nest);
10081
David S. Miller9360ffd2012-03-29 04:41:26 -040010082 if (req->ie &&
10083 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10084 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010085
Johannes Bergae917c92013-10-25 11:05:22 +020010086 if (req->flags &&
10087 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10088 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010089
Johannes Berg362a4152009-05-24 16:43:15 +020010090 return 0;
10091 nla_put_failure:
10092 return -ENOBUFS;
10093}
10094
Johannes Berga538e2d2009-06-16 19:56:42 +020010095static int nl80211_send_scan_msg(struct sk_buff *msg,
10096 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010097 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010098 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010099 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010100{
10101 void *hdr;
10102
Eric W. Biederman15e47302012-09-07 20:12:54 +000010103 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010104 if (!hdr)
10105 return -1;
10106
David S. Miller9360ffd2012-03-29 04:41:26 -040010107 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010108 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10109 wdev->netdev->ifindex)) ||
10110 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010111 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010112
Johannes Berg362a4152009-05-24 16:43:15 +020010113 /* ignore errors and send incomplete event anyway */
10114 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010115
10116 return genlmsg_end(msg, hdr);
10117
10118 nla_put_failure:
10119 genlmsg_cancel(msg, hdr);
10120 return -EMSGSIZE;
10121}
10122
Luciano Coelho807f8a82011-05-11 17:09:35 +030010123static int
10124nl80211_send_sched_scan_msg(struct sk_buff *msg,
10125 struct cfg80211_registered_device *rdev,
10126 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010127 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010128{
10129 void *hdr;
10130
Eric W. Biederman15e47302012-09-07 20:12:54 +000010131 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010132 if (!hdr)
10133 return -1;
10134
David S. Miller9360ffd2012-03-29 04:41:26 -040010135 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10136 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10137 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010138
10139 return genlmsg_end(msg, hdr);
10140
10141 nla_put_failure:
10142 genlmsg_cancel(msg, hdr);
10143 return -EMSGSIZE;
10144}
10145
Johannes Berga538e2d2009-06-16 19:56:42 +020010146void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010147 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010148{
10149 struct sk_buff *msg;
10150
Thomas Graf58050fc2012-06-28 03:57:45 +000010151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010152 if (!msg)
10153 return;
10154
Johannes Bergfd014282012-06-18 19:17:03 +020010155 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010156 NL80211_CMD_TRIGGER_SCAN) < 0) {
10157 nlmsg_free(msg);
10158 return;
10159 }
10160
Johannes Berg68eb5502013-11-19 15:19:38 +010010161 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010162 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010163}
10164
Johannes Bergf9d15d12014-01-22 11:14:19 +020010165struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10166 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010167{
10168 struct sk_buff *msg;
10169
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010171 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010172 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010173
Johannes Bergfd014282012-06-18 19:17:03 +020010174 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010175 aborted ? NL80211_CMD_SCAN_ABORTED :
10176 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010177 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010178 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010179 }
10180
Johannes Bergf9d15d12014-01-22 11:14:19 +020010181 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010182}
10183
Johannes Bergf9d15d12014-01-22 11:14:19 +020010184void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10185 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010186{
Johannes Berg2a519312009-02-10 21:25:55 +010010187 if (!msg)
10188 return;
10189
Johannes Berg68eb5502013-11-19 15:19:38 +010010190 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010191 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010192}
10193
Luciano Coelho807f8a82011-05-11 17:09:35 +030010194void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10195 struct net_device *netdev)
10196{
10197 struct sk_buff *msg;
10198
10199 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10200 if (!msg)
10201 return;
10202
10203 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10204 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10205 nlmsg_free(msg);
10206 return;
10207 }
10208
Johannes Berg68eb5502013-11-19 15:19:38 +010010209 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010210 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010211}
10212
10213void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10214 struct net_device *netdev, u32 cmd)
10215{
10216 struct sk_buff *msg;
10217
Thomas Graf58050fc2012-06-28 03:57:45 +000010218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010219 if (!msg)
10220 return;
10221
10222 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10223 nlmsg_free(msg);
10224 return;
10225 }
10226
Johannes Berg68eb5502013-11-19 15:19:38 +010010227 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010228 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010229}
10230
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010231/*
10232 * This can happen on global regulatory changes or device specific settings
10233 * based on custom world regulatory domains.
10234 */
10235void nl80211_send_reg_change_event(struct regulatory_request *request)
10236{
10237 struct sk_buff *msg;
10238 void *hdr;
10239
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010241 if (!msg)
10242 return;
10243
10244 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10245 if (!hdr) {
10246 nlmsg_free(msg);
10247 return;
10248 }
10249
10250 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010251 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10252 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010253
David S. Miller9360ffd2012-03-29 04:41:26 -040010254 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10255 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10256 NL80211_REGDOM_TYPE_WORLD))
10257 goto nla_put_failure;
10258 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10259 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10260 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10261 goto nla_put_failure;
10262 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10263 request->intersect) {
10264 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10265 NL80211_REGDOM_TYPE_INTERSECTION))
10266 goto nla_put_failure;
10267 } else {
10268 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10269 NL80211_REGDOM_TYPE_COUNTRY) ||
10270 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10271 request->alpha2))
10272 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010273 }
10274
Johannes Bergf4173762012-12-03 18:23:37 +010010275 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010276 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10277 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010278
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010279 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010280
Johannes Bergbc43b282009-07-25 10:54:13 +020010281 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010282 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010283 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010284 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010285
10286 return;
10287
10288nla_put_failure:
10289 genlmsg_cancel(msg, hdr);
10290 nlmsg_free(msg);
10291}
10292
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010293static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10294 struct net_device *netdev,
10295 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010296 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010297{
10298 struct sk_buff *msg;
10299 void *hdr;
10300
Johannes Berge6d6e342009-07-01 21:26:47 +020010301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010302 if (!msg)
10303 return;
10304
10305 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10306 if (!hdr) {
10307 nlmsg_free(msg);
10308 return;
10309 }
10310
David S. Miller9360ffd2012-03-29 04:41:26 -040010311 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10312 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10313 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10314 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010315
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010316 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010317
Johannes Berg68eb5502013-11-19 15:19:38 +010010318 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010319 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010320 return;
10321
10322 nla_put_failure:
10323 genlmsg_cancel(msg, hdr);
10324 nlmsg_free(msg);
10325}
10326
10327void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010328 struct net_device *netdev, const u8 *buf,
10329 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010330{
10331 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010332 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010333}
10334
10335void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10336 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010337 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010338{
Johannes Berge6d6e342009-07-01 21:26:47 +020010339 nl80211_send_mlme_event(rdev, netdev, buf, len,
10340 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010341}
10342
Jouni Malinen53b46b82009-03-27 20:53:56 +020010343void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010344 struct net_device *netdev, const u8 *buf,
10345 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010346{
10347 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010348 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010349}
10350
Jouni Malinen53b46b82009-03-27 20:53:56 +020010351void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10352 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010353 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010354{
10355 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010356 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010357}
10358
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010359void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10360 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010361{
Johannes Berg947add32013-02-22 22:05:20 +010010362 struct wireless_dev *wdev = dev->ieee80211_ptr;
10363 struct wiphy *wiphy = wdev->wiphy;
10364 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010365 const struct ieee80211_mgmt *mgmt = (void *)buf;
10366 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010367
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010368 if (WARN_ON(len < 2))
10369 return;
10370
10371 if (ieee80211_is_deauth(mgmt->frame_control))
10372 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10373 else
10374 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10375
10376 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10377 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010378}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010379EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010380
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010381static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10382 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010383 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010384{
10385 struct sk_buff *msg;
10386 void *hdr;
10387
Johannes Berge6d6e342009-07-01 21:26:47 +020010388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010389 if (!msg)
10390 return;
10391
10392 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10393 if (!hdr) {
10394 nlmsg_free(msg);
10395 return;
10396 }
10397
David S. Miller9360ffd2012-03-29 04:41:26 -040010398 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10399 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10400 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10401 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10402 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010403
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010404 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010405
Johannes Berg68eb5502013-11-19 15:19:38 +010010406 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010407 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010408 return;
10409
10410 nla_put_failure:
10411 genlmsg_cancel(msg, hdr);
10412 nlmsg_free(msg);
10413}
10414
10415void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010416 struct net_device *netdev, const u8 *addr,
10417 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010418{
10419 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010420 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010421}
10422
10423void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010424 struct net_device *netdev, const u8 *addr,
10425 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010426{
Johannes Berge6d6e342009-07-01 21:26:47 +020010427 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10428 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010429}
10430
Samuel Ortizb23aa672009-07-01 21:26:54 +020010431void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10432 struct net_device *netdev, const u8 *bssid,
10433 const u8 *req_ie, size_t req_ie_len,
10434 const u8 *resp_ie, size_t resp_ie_len,
10435 u16 status, gfp_t gfp)
10436{
10437 struct sk_buff *msg;
10438 void *hdr;
10439
Thomas Graf58050fc2012-06-28 03:57:45 +000010440 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010441 if (!msg)
10442 return;
10443
10444 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10445 if (!hdr) {
10446 nlmsg_free(msg);
10447 return;
10448 }
10449
David S. Miller9360ffd2012-03-29 04:41:26 -040010450 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10451 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10452 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10453 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10454 (req_ie &&
10455 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10456 (resp_ie &&
10457 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10458 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010459
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010460 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010461
Johannes Berg68eb5502013-11-19 15:19:38 +010010462 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010463 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010464 return;
10465
10466 nla_put_failure:
10467 genlmsg_cancel(msg, hdr);
10468 nlmsg_free(msg);
10469
10470}
10471
10472void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10473 struct net_device *netdev, const u8 *bssid,
10474 const u8 *req_ie, size_t req_ie_len,
10475 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10476{
10477 struct sk_buff *msg;
10478 void *hdr;
10479
Thomas Graf58050fc2012-06-28 03:57:45 +000010480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010481 if (!msg)
10482 return;
10483
10484 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10485 if (!hdr) {
10486 nlmsg_free(msg);
10487 return;
10488 }
10489
David S. Miller9360ffd2012-03-29 04:41:26 -040010490 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10491 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10492 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10493 (req_ie &&
10494 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10495 (resp_ie &&
10496 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10497 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010498
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010499 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010500
Johannes Berg68eb5502013-11-19 15:19:38 +010010501 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010502 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010503 return;
10504
10505 nla_put_failure:
10506 genlmsg_cancel(msg, hdr);
10507 nlmsg_free(msg);
10508
10509}
10510
10511void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10512 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +020010513 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010514{
10515 struct sk_buff *msg;
10516 void *hdr;
10517
Thomas Graf58050fc2012-06-28 03:57:45 +000010518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010519 if (!msg)
10520 return;
10521
10522 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10523 if (!hdr) {
10524 nlmsg_free(msg);
10525 return;
10526 }
10527
David S. Miller9360ffd2012-03-29 04:41:26 -040010528 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10529 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10530 (from_ap && reason &&
10531 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10532 (from_ap &&
10533 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10534 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10535 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010536
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010537 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010538
Johannes Berg68eb5502013-11-19 15:19:38 +010010539 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010540 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010541 return;
10542
10543 nla_put_failure:
10544 genlmsg_cancel(msg, hdr);
10545 nlmsg_free(msg);
10546
10547}
10548
Johannes Berg04a773a2009-04-19 21:24:32 +020010549void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10550 struct net_device *netdev, const u8 *bssid,
10551 gfp_t gfp)
10552{
10553 struct sk_buff *msg;
10554 void *hdr;
10555
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010557 if (!msg)
10558 return;
10559
10560 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10561 if (!hdr) {
10562 nlmsg_free(msg);
10563 return;
10564 }
10565
David S. Miller9360ffd2012-03-29 04:41:26 -040010566 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10567 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10568 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10569 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010570
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010571 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010572
Johannes Berg68eb5502013-11-19 15:19:38 +010010573 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010574 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010575 return;
10576
10577 nla_put_failure:
10578 genlmsg_cancel(msg, hdr);
10579 nlmsg_free(msg);
10580}
10581
Johannes Berg947add32013-02-22 22:05:20 +010010582void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10583 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010584{
Johannes Berg947add32013-02-22 22:05:20 +010010585 struct wireless_dev *wdev = dev->ieee80211_ptr;
10586 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010587 struct sk_buff *msg;
10588 void *hdr;
10589
Johannes Berg947add32013-02-22 22:05:20 +010010590 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10591 return;
10592
10593 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10594
Javier Cardonac93b5e72011-04-07 15:08:34 -070010595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10596 if (!msg)
10597 return;
10598
10599 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10600 if (!hdr) {
10601 nlmsg_free(msg);
10602 return;
10603 }
10604
David S. Miller9360ffd2012-03-29 04:41:26 -040010605 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010606 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10607 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010608 (ie_len && ie &&
10609 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10610 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010611
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010612 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010613
Johannes Berg68eb5502013-11-19 15:19:38 +010010614 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010615 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010616 return;
10617
10618 nla_put_failure:
10619 genlmsg_cancel(msg, hdr);
10620 nlmsg_free(msg);
10621}
Johannes Berg947add32013-02-22 22:05:20 +010010622EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010623
Jouni Malinena3b8b052009-03-27 21:59:49 +020010624void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10625 struct net_device *netdev, const u8 *addr,
10626 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010627 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010628{
10629 struct sk_buff *msg;
10630 void *hdr;
10631
Johannes Berge6d6e342009-07-01 21:26:47 +020010632 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010633 if (!msg)
10634 return;
10635
10636 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10637 if (!hdr) {
10638 nlmsg_free(msg);
10639 return;
10640 }
10641
David S. Miller9360ffd2012-03-29 04:41:26 -040010642 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10643 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10644 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10645 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10646 (key_id != -1 &&
10647 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10648 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10649 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010650
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010651 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010652
Johannes Berg68eb5502013-11-19 15:19:38 +010010653 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010654 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010655 return;
10656
10657 nla_put_failure:
10658 genlmsg_cancel(msg, hdr);
10659 nlmsg_free(msg);
10660}
10661
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010662void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10663 struct ieee80211_channel *channel_before,
10664 struct ieee80211_channel *channel_after)
10665{
10666 struct sk_buff *msg;
10667 void *hdr;
10668 struct nlattr *nl_freq;
10669
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010670 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010671 if (!msg)
10672 return;
10673
10674 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10675 if (!hdr) {
10676 nlmsg_free(msg);
10677 return;
10678 }
10679
10680 /*
10681 * Since we are applying the beacon hint to a wiphy we know its
10682 * wiphy_idx is valid
10683 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010684 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10685 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010686
10687 /* Before */
10688 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10689 if (!nl_freq)
10690 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010691 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010692 goto nla_put_failure;
10693 nla_nest_end(msg, nl_freq);
10694
10695 /* After */
10696 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10697 if (!nl_freq)
10698 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010699 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010700 goto nla_put_failure;
10701 nla_nest_end(msg, nl_freq);
10702
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010703 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010704
Johannes Berg463d0182009-07-14 00:33:35 +020010705 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010706 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010707 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010708 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010709
10710 return;
10711
10712nla_put_failure:
10713 genlmsg_cancel(msg, hdr);
10714 nlmsg_free(msg);
10715}
10716
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010717static void nl80211_send_remain_on_chan_event(
10718 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010719 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010720 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010721 unsigned int duration, gfp_t gfp)
10722{
10723 struct sk_buff *msg;
10724 void *hdr;
10725
10726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10727 if (!msg)
10728 return;
10729
10730 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10731 if (!hdr) {
10732 nlmsg_free(msg);
10733 return;
10734 }
10735
David S. Miller9360ffd2012-03-29 04:41:26 -040010736 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010737 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10738 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010739 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010740 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010741 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10742 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010743 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10744 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010745
David S. Miller9360ffd2012-03-29 04:41:26 -040010746 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10747 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10748 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010749
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010750 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010751
Johannes Berg68eb5502013-11-19 15:19:38 +010010752 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010753 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010754 return;
10755
10756 nla_put_failure:
10757 genlmsg_cancel(msg, hdr);
10758 nlmsg_free(msg);
10759}
10760
Johannes Berg947add32013-02-22 22:05:20 +010010761void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10762 struct ieee80211_channel *chan,
10763 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010764{
Johannes Berg947add32013-02-22 22:05:20 +010010765 struct wiphy *wiphy = wdev->wiphy;
10766 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10767
10768 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010769 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010770 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010771 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010772}
Johannes Berg947add32013-02-22 22:05:20 +010010773EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010774
Johannes Berg947add32013-02-22 22:05:20 +010010775void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10776 struct ieee80211_channel *chan,
10777 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010778{
Johannes Berg947add32013-02-22 22:05:20 +010010779 struct wiphy *wiphy = wdev->wiphy;
10780 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10781
10782 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010783 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010784 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010785}
Johannes Berg947add32013-02-22 22:05:20 +010010786EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010787
Johannes Berg947add32013-02-22 22:05:20 +010010788void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10789 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010790{
Johannes Berg947add32013-02-22 22:05:20 +010010791 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10792 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010793 struct sk_buff *msg;
10794
Johannes Berg947add32013-02-22 22:05:20 +010010795 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10796
Thomas Graf58050fc2012-06-28 03:57:45 +000010797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010798 if (!msg)
10799 return;
10800
John W. Linville66266b32012-03-15 13:25:41 -040010801 if (nl80211_send_station(msg, 0, 0, 0,
10802 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010803 nlmsg_free(msg);
10804 return;
10805 }
10806
Johannes Berg68eb5502013-11-19 15:19:38 +010010807 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010808 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010809}
Johannes Berg947add32013-02-22 22:05:20 +010010810EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010811
Johannes Berg947add32013-02-22 22:05:20 +010010812void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010813{
Johannes Berg947add32013-02-22 22:05:20 +010010814 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10815 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010816 struct sk_buff *msg;
10817 void *hdr;
10818
Johannes Berg947add32013-02-22 22:05:20 +010010819 trace_cfg80211_del_sta(dev, mac_addr);
10820
Thomas Graf58050fc2012-06-28 03:57:45 +000010821 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010822 if (!msg)
10823 return;
10824
10825 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10826 if (!hdr) {
10827 nlmsg_free(msg);
10828 return;
10829 }
10830
David S. Miller9360ffd2012-03-29 04:41:26 -040010831 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10832 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10833 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010834
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010835 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010836
Johannes Berg68eb5502013-11-19 15:19:38 +010010837 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010838 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010839 return;
10840
10841 nla_put_failure:
10842 genlmsg_cancel(msg, hdr);
10843 nlmsg_free(msg);
10844}
Johannes Berg947add32013-02-22 22:05:20 +010010845EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010846
Johannes Berg947add32013-02-22 22:05:20 +010010847void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10848 enum nl80211_connect_failed_reason reason,
10849 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010850{
Johannes Berg947add32013-02-22 22:05:20 +010010851 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10852 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010853 struct sk_buff *msg;
10854 void *hdr;
10855
10856 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10857 if (!msg)
10858 return;
10859
10860 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10861 if (!hdr) {
10862 nlmsg_free(msg);
10863 return;
10864 }
10865
10866 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10867 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10868 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10869 goto nla_put_failure;
10870
10871 genlmsg_end(msg, hdr);
10872
Johannes Berg68eb5502013-11-19 15:19:38 +010010873 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010874 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010875 return;
10876
10877 nla_put_failure:
10878 genlmsg_cancel(msg, hdr);
10879 nlmsg_free(msg);
10880}
Johannes Berg947add32013-02-22 22:05:20 +010010881EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010882
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010883static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10884 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010885{
10886 struct wireless_dev *wdev = dev->ieee80211_ptr;
10887 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10888 struct sk_buff *msg;
10889 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010890 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010891
Eric W. Biederman15e47302012-09-07 20:12:54 +000010892 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010893 return false;
10894
10895 msg = nlmsg_new(100, gfp);
10896 if (!msg)
10897 return true;
10898
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010899 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010900 if (!hdr) {
10901 nlmsg_free(msg);
10902 return true;
10903 }
10904
David S. Miller9360ffd2012-03-29 04:41:26 -040010905 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10906 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10907 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10908 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010909
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010910 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010911 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010912 return true;
10913
10914 nla_put_failure:
10915 genlmsg_cancel(msg, hdr);
10916 nlmsg_free(msg);
10917 return true;
10918}
10919
Johannes Berg947add32013-02-22 22:05:20 +010010920bool cfg80211_rx_spurious_frame(struct net_device *dev,
10921 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010922{
Johannes Berg947add32013-02-22 22:05:20 +010010923 struct wireless_dev *wdev = dev->ieee80211_ptr;
10924 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010925
Johannes Berg947add32013-02-22 22:05:20 +010010926 trace_cfg80211_rx_spurious_frame(dev, addr);
10927
10928 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10929 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10930 trace_cfg80211_return_bool(false);
10931 return false;
10932 }
10933 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10934 addr, gfp);
10935 trace_cfg80211_return_bool(ret);
10936 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010937}
Johannes Berg947add32013-02-22 22:05:20 +010010938EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10939
10940bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10941 const u8 *addr, gfp_t gfp)
10942{
10943 struct wireless_dev *wdev = dev->ieee80211_ptr;
10944 bool ret;
10945
10946 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10947
10948 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10949 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10950 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10951 trace_cfg80211_return_bool(false);
10952 return false;
10953 }
10954 ret = __nl80211_unexpected_frame(dev,
10955 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10956 addr, gfp);
10957 trace_cfg80211_return_bool(ret);
10958 return ret;
10959}
10960EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010961
Johannes Berg2e161f782010-08-12 15:38:38 +020010962int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010963 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010964 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010965 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010966{
Johannes Berg71bbc992012-06-15 15:30:18 +020010967 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010968 struct sk_buff *msg;
10969 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010970
10971 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10972 if (!msg)
10973 return -ENOMEM;
10974
Johannes Berg2e161f782010-08-12 15:38:38 +020010975 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010976 if (!hdr) {
10977 nlmsg_free(msg);
10978 return -ENOMEM;
10979 }
10980
David S. Miller9360ffd2012-03-29 04:41:26 -040010981 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010982 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10983 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010984 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010985 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10986 (sig_dbm &&
10987 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010988 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10989 (flags &&
10990 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010991 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010992
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010993 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010994
Eric W. Biederman15e47302012-09-07 20:12:54 +000010995 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010996
10997 nla_put_failure:
10998 genlmsg_cancel(msg, hdr);
10999 nlmsg_free(msg);
11000 return -ENOBUFS;
11001}
11002
Johannes Berg947add32013-02-22 22:05:20 +010011003void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11004 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011005{
Johannes Berg947add32013-02-22 22:05:20 +010011006 struct wiphy *wiphy = wdev->wiphy;
11007 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011008 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011009 struct sk_buff *msg;
11010 void *hdr;
11011
Johannes Berg947add32013-02-22 22:05:20 +010011012 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11013
Jouni Malinen026331c2010-02-15 12:53:10 +020011014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11015 if (!msg)
11016 return;
11017
Johannes Berg2e161f782010-08-12 15:38:38 +020011018 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011019 if (!hdr) {
11020 nlmsg_free(msg);
11021 return;
11022 }
11023
David S. Miller9360ffd2012-03-29 04:41:26 -040011024 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011025 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11026 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011027 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011028 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11029 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11030 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11031 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011032
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011033 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011034
Johannes Berg68eb5502013-11-19 15:19:38 +010011035 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011036 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011037 return;
11038
11039 nla_put_failure:
11040 genlmsg_cancel(msg, hdr);
11041 nlmsg_free(msg);
11042}
Johannes Berg947add32013-02-22 22:05:20 +010011043EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011044
Johannes Berg947add32013-02-22 22:05:20 +010011045void cfg80211_cqm_rssi_notify(struct net_device *dev,
11046 enum nl80211_cqm_rssi_threshold_event rssi_event,
11047 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011048{
Johannes Berg947add32013-02-22 22:05:20 +010011049 struct wireless_dev *wdev = dev->ieee80211_ptr;
11050 struct wiphy *wiphy = wdev->wiphy;
11051 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011052 struct sk_buff *msg;
11053 struct nlattr *pinfoattr;
11054 void *hdr;
11055
Johannes Berg947add32013-02-22 22:05:20 +010011056 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11057
Thomas Graf58050fc2012-06-28 03:57:45 +000011058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011059 if (!msg)
11060 return;
11061
11062 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11063 if (!hdr) {
11064 nlmsg_free(msg);
11065 return;
11066 }
11067
David S. Miller9360ffd2012-03-29 04:41:26 -040011068 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011069 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011070 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011071
11072 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11073 if (!pinfoattr)
11074 goto nla_put_failure;
11075
David S. Miller9360ffd2012-03-29 04:41:26 -040011076 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11077 rssi_event))
11078 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011079
11080 nla_nest_end(msg, pinfoattr);
11081
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011082 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011083
Johannes Berg68eb5502013-11-19 15:19:38 +010011084 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011085 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011086 return;
11087
11088 nla_put_failure:
11089 genlmsg_cancel(msg, hdr);
11090 nlmsg_free(msg);
11091}
Johannes Berg947add32013-02-22 22:05:20 +010011092EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011093
Johannes Berg947add32013-02-22 22:05:20 +010011094static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11095 struct net_device *netdev, const u8 *bssid,
11096 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011097{
11098 struct sk_buff *msg;
11099 struct nlattr *rekey_attr;
11100 void *hdr;
11101
Thomas Graf58050fc2012-06-28 03:57:45 +000011102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011103 if (!msg)
11104 return;
11105
11106 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11107 if (!hdr) {
11108 nlmsg_free(msg);
11109 return;
11110 }
11111
David S. Miller9360ffd2012-03-29 04:41:26 -040011112 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11113 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11114 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11115 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011116
11117 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11118 if (!rekey_attr)
11119 goto nla_put_failure;
11120
David S. Miller9360ffd2012-03-29 04:41:26 -040011121 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11122 NL80211_REPLAY_CTR_LEN, replay_ctr))
11123 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011124
11125 nla_nest_end(msg, rekey_attr);
11126
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011127 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011128
Johannes Berg68eb5502013-11-19 15:19:38 +010011129 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011130 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011131 return;
11132
11133 nla_put_failure:
11134 genlmsg_cancel(msg, hdr);
11135 nlmsg_free(msg);
11136}
11137
Johannes Berg947add32013-02-22 22:05:20 +010011138void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11139 const u8 *replay_ctr, gfp_t gfp)
11140{
11141 struct wireless_dev *wdev = dev->ieee80211_ptr;
11142 struct wiphy *wiphy = wdev->wiphy;
11143 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11144
11145 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11146 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11147}
11148EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11149
11150static void
11151nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11152 struct net_device *netdev, int index,
11153 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011154{
11155 struct sk_buff *msg;
11156 struct nlattr *attr;
11157 void *hdr;
11158
Thomas Graf58050fc2012-06-28 03:57:45 +000011159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011160 if (!msg)
11161 return;
11162
11163 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11164 if (!hdr) {
11165 nlmsg_free(msg);
11166 return;
11167 }
11168
David S. Miller9360ffd2012-03-29 04:41:26 -040011169 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11170 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11171 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011172
11173 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11174 if (!attr)
11175 goto nla_put_failure;
11176
David S. Miller9360ffd2012-03-29 04:41:26 -040011177 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11178 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11179 (preauth &&
11180 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11181 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011182
11183 nla_nest_end(msg, attr);
11184
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011185 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011186
Johannes Berg68eb5502013-11-19 15:19:38 +010011187 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011188 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011189 return;
11190
11191 nla_put_failure:
11192 genlmsg_cancel(msg, hdr);
11193 nlmsg_free(msg);
11194}
11195
Johannes Berg947add32013-02-22 22:05:20 +010011196void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11197 const u8 *bssid, bool preauth, gfp_t gfp)
11198{
11199 struct wireless_dev *wdev = dev->ieee80211_ptr;
11200 struct wiphy *wiphy = wdev->wiphy;
11201 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11202
11203 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11204 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11205}
11206EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11207
11208static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11209 struct net_device *netdev,
11210 struct cfg80211_chan_def *chandef,
11211 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011212{
11213 struct sk_buff *msg;
11214 void *hdr;
11215
Thomas Graf58050fc2012-06-28 03:57:45 +000011216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011217 if (!msg)
11218 return;
11219
11220 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11221 if (!hdr) {
11222 nlmsg_free(msg);
11223 return;
11224 }
11225
Johannes Berg683b6d32012-11-08 21:25:48 +010011226 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11227 goto nla_put_failure;
11228
11229 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011230 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011231
11232 genlmsg_end(msg, hdr);
11233
Johannes Berg68eb5502013-11-19 15:19:38 +010011234 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011235 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011236 return;
11237
11238 nla_put_failure:
11239 genlmsg_cancel(msg, hdr);
11240 nlmsg_free(msg);
11241}
11242
Johannes Berg947add32013-02-22 22:05:20 +010011243void cfg80211_ch_switch_notify(struct net_device *dev,
11244 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011245{
Johannes Berg947add32013-02-22 22:05:20 +010011246 struct wireless_dev *wdev = dev->ieee80211_ptr;
11247 struct wiphy *wiphy = wdev->wiphy;
11248 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11249
Simon Wunderliche487eae2013-11-21 18:19:51 +010011250 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011251
Simon Wunderliche487eae2013-11-21 18:19:51 +010011252 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011253
11254 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011255 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011256 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11257 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011258 return;
Johannes Berg947add32013-02-22 22:05:20 +010011259
Michal Kazior9e0e2962014-01-29 14:22:27 +010011260 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011261 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011262 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011263}
11264EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11265
11266void cfg80211_cqm_txe_notify(struct net_device *dev,
11267 const u8 *peer, u32 num_packets,
11268 u32 rate, u32 intvl, gfp_t gfp)
11269{
11270 struct wireless_dev *wdev = dev->ieee80211_ptr;
11271 struct wiphy *wiphy = wdev->wiphy;
11272 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011273 struct sk_buff *msg;
11274 struct nlattr *pinfoattr;
11275 void *hdr;
11276
11277 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11278 if (!msg)
11279 return;
11280
11281 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11282 if (!hdr) {
11283 nlmsg_free(msg);
11284 return;
11285 }
11286
11287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011288 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011289 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11290 goto nla_put_failure;
11291
11292 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11293 if (!pinfoattr)
11294 goto nla_put_failure;
11295
11296 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11297 goto nla_put_failure;
11298
11299 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11300 goto nla_put_failure;
11301
11302 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11303 goto nla_put_failure;
11304
11305 nla_nest_end(msg, pinfoattr);
11306
11307 genlmsg_end(msg, hdr);
11308
Johannes Berg68eb5502013-11-19 15:19:38 +010011309 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011310 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011311 return;
11312
11313 nla_put_failure:
11314 genlmsg_cancel(msg, hdr);
11315 nlmsg_free(msg);
11316}
Johannes Berg947add32013-02-22 22:05:20 +010011317EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011318
11319void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011320nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011321 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011322 enum nl80211_radar_event event,
11323 struct net_device *netdev, gfp_t gfp)
11324{
11325 struct sk_buff *msg;
11326 void *hdr;
11327
11328 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11329 if (!msg)
11330 return;
11331
11332 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11333 if (!hdr) {
11334 nlmsg_free(msg);
11335 return;
11336 }
11337
11338 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11339 goto nla_put_failure;
11340
11341 /* NOP and radar events don't need a netdev parameter */
11342 if (netdev) {
11343 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11344
11345 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11346 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11347 goto nla_put_failure;
11348 }
11349
11350 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11351 goto nla_put_failure;
11352
11353 if (nl80211_send_chandef(msg, chandef))
11354 goto nla_put_failure;
11355
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011356 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011357
Johannes Berg68eb5502013-11-19 15:19:38 +010011358 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011359 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011360 return;
11361
11362 nla_put_failure:
11363 genlmsg_cancel(msg, hdr);
11364 nlmsg_free(msg);
11365}
11366
Johannes Berg947add32013-02-22 22:05:20 +010011367void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11368 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011369{
Johannes Berg947add32013-02-22 22:05:20 +010011370 struct wireless_dev *wdev = dev->ieee80211_ptr;
11371 struct wiphy *wiphy = wdev->wiphy;
11372 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011373 struct sk_buff *msg;
11374 struct nlattr *pinfoattr;
11375 void *hdr;
11376
Johannes Berg947add32013-02-22 22:05:20 +010011377 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11378
Thomas Graf58050fc2012-06-28 03:57:45 +000011379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011380 if (!msg)
11381 return;
11382
11383 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11384 if (!hdr) {
11385 nlmsg_free(msg);
11386 return;
11387 }
11388
David S. Miller9360ffd2012-03-29 04:41:26 -040011389 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011390 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011391 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11392 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011393
11394 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11395 if (!pinfoattr)
11396 goto nla_put_failure;
11397
David S. Miller9360ffd2012-03-29 04:41:26 -040011398 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11399 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011400
11401 nla_nest_end(msg, pinfoattr);
11402
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011403 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011404
Johannes Berg68eb5502013-11-19 15:19:38 +010011405 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011406 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011407 return;
11408
11409 nla_put_failure:
11410 genlmsg_cancel(msg, hdr);
11411 nlmsg_free(msg);
11412}
Johannes Berg947add32013-02-22 22:05:20 +010011413EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011414
Johannes Berg7f6cf312011-11-04 11:18:15 +010011415void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11416 u64 cookie, bool acked, gfp_t gfp)
11417{
11418 struct wireless_dev *wdev = dev->ieee80211_ptr;
11419 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11420 struct sk_buff *msg;
11421 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011422
Beni Lev4ee3e062012-08-27 12:49:39 +030011423 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11424
Thomas Graf58050fc2012-06-28 03:57:45 +000011425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011426
Johannes Berg7f6cf312011-11-04 11:18:15 +010011427 if (!msg)
11428 return;
11429
11430 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11431 if (!hdr) {
11432 nlmsg_free(msg);
11433 return;
11434 }
11435
David S. Miller9360ffd2012-03-29 04:41:26 -040011436 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11437 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11438 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11439 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11440 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11441 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011442
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011443 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011444
Johannes Berg68eb5502013-11-19 15:19:38 +010011445 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011446 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011447 return;
11448
11449 nla_put_failure:
11450 genlmsg_cancel(msg, hdr);
11451 nlmsg_free(msg);
11452}
11453EXPORT_SYMBOL(cfg80211_probe_status);
11454
Johannes Berg5e760232011-11-04 11:18:17 +010011455void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11456 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011457 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011458{
11459 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11460 struct sk_buff *msg;
11461 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011462 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011463
Beni Lev4ee3e062012-08-27 12:49:39 +030011464 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11465
Ben Greear37c73b52012-10-26 14:49:25 -070011466 spin_lock_bh(&rdev->beacon_registrations_lock);
11467 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11468 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11469 if (!msg) {
11470 spin_unlock_bh(&rdev->beacon_registrations_lock);
11471 return;
11472 }
Johannes Berg5e760232011-11-04 11:18:17 +010011473
Ben Greear37c73b52012-10-26 14:49:25 -070011474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11475 if (!hdr)
11476 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011477
Ben Greear37c73b52012-10-26 14:49:25 -070011478 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11479 (freq &&
11480 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11481 (sig_dbm &&
11482 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11483 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11484 goto nla_put_failure;
11485
11486 genlmsg_end(msg, hdr);
11487
11488 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011489 }
Ben Greear37c73b52012-10-26 14:49:25 -070011490 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011491 return;
11492
11493 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011494 spin_unlock_bh(&rdev->beacon_registrations_lock);
11495 if (hdr)
11496 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011497 nlmsg_free(msg);
11498}
11499EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11500
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011501#ifdef CONFIG_PM
11502void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11503 struct cfg80211_wowlan_wakeup *wakeup,
11504 gfp_t gfp)
11505{
11506 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11507 struct sk_buff *msg;
11508 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011509 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011510
11511 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11512
11513 if (wakeup)
11514 size += wakeup->packet_present_len;
11515
11516 msg = nlmsg_new(size, gfp);
11517 if (!msg)
11518 return;
11519
11520 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11521 if (!hdr)
11522 goto free_msg;
11523
11524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11525 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11526 goto free_msg;
11527
11528 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11529 wdev->netdev->ifindex))
11530 goto free_msg;
11531
11532 if (wakeup) {
11533 struct nlattr *reasons;
11534
11535 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011536 if (!reasons)
11537 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011538
11539 if (wakeup->disconnect &&
11540 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11541 goto free_msg;
11542 if (wakeup->magic_pkt &&
11543 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11544 goto free_msg;
11545 if (wakeup->gtk_rekey_failure &&
11546 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11547 goto free_msg;
11548 if (wakeup->eap_identity_req &&
11549 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11550 goto free_msg;
11551 if (wakeup->four_way_handshake &&
11552 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11553 goto free_msg;
11554 if (wakeup->rfkill_release &&
11555 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11556 goto free_msg;
11557
11558 if (wakeup->pattern_idx >= 0 &&
11559 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11560 wakeup->pattern_idx))
11561 goto free_msg;
11562
Johannes Bergae917c92013-10-25 11:05:22 +020011563 if (wakeup->tcp_match &&
11564 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11565 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011566
Johannes Bergae917c92013-10-25 11:05:22 +020011567 if (wakeup->tcp_connlost &&
11568 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11569 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011570
Johannes Bergae917c92013-10-25 11:05:22 +020011571 if (wakeup->tcp_nomoretokens &&
11572 nla_put_flag(msg,
11573 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11574 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011575
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011576 if (wakeup->packet) {
11577 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11578 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11579
11580 if (!wakeup->packet_80211) {
11581 pkt_attr =
11582 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11583 len_attr =
11584 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11585 }
11586
11587 if (wakeup->packet_len &&
11588 nla_put_u32(msg, len_attr, wakeup->packet_len))
11589 goto free_msg;
11590
11591 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11592 wakeup->packet))
11593 goto free_msg;
11594 }
11595
11596 nla_nest_end(msg, reasons);
11597 }
11598
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011599 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011600
Johannes Berg68eb5502013-11-19 15:19:38 +010011601 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011602 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011603 return;
11604
11605 free_msg:
11606 nlmsg_free(msg);
11607}
11608EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11609#endif
11610
Jouni Malinen3475b092012-11-16 22:49:57 +020011611void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11612 enum nl80211_tdls_operation oper,
11613 u16 reason_code, gfp_t gfp)
11614{
11615 struct wireless_dev *wdev = dev->ieee80211_ptr;
11616 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11617 struct sk_buff *msg;
11618 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011619
11620 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11621 reason_code);
11622
11623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11624 if (!msg)
11625 return;
11626
11627 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11628 if (!hdr) {
11629 nlmsg_free(msg);
11630 return;
11631 }
11632
11633 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11634 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11635 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11636 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11637 (reason_code > 0 &&
11638 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11639 goto nla_put_failure;
11640
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011641 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011642
Johannes Berg68eb5502013-11-19 15:19:38 +010011643 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011644 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011645 return;
11646
11647 nla_put_failure:
11648 genlmsg_cancel(msg, hdr);
11649 nlmsg_free(msg);
11650}
11651EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11652
Jouni Malinen026331c2010-02-15 12:53:10 +020011653static int nl80211_netlink_notify(struct notifier_block * nb,
11654 unsigned long state,
11655 void *_notify)
11656{
11657 struct netlink_notify *notify = _notify;
11658 struct cfg80211_registered_device *rdev;
11659 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011660 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011661
11662 if (state != NETLINK_URELEASE)
11663 return NOTIFY_DONE;
11664
11665 rcu_read_lock();
11666
Johannes Berg5e760232011-11-04 11:18:17 +010011667 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010011668 bool schedule_destroy_work = false;
11669
11670 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000011671 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011672
Johannes Berg78f22b62014-03-24 17:57:27 +010011673 if (wdev->owner_nlportid == notify->portid)
11674 schedule_destroy_work = true;
11675 }
11676
Ben Greear37c73b52012-10-26 14:49:25 -070011677 spin_lock_bh(&rdev->beacon_registrations_lock);
11678 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11679 list) {
11680 if (reg->nlportid == notify->portid) {
11681 list_del(&reg->list);
11682 kfree(reg);
11683 break;
11684 }
11685 }
11686 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010011687
11688 if (schedule_destroy_work) {
11689 struct cfg80211_iface_destroy *destroy;
11690
11691 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
11692 if (destroy) {
11693 destroy->nlportid = notify->portid;
11694 spin_lock(&rdev->destroy_list_lock);
11695 list_add(&destroy->list, &rdev->destroy_list);
11696 spin_unlock(&rdev->destroy_list_lock);
11697 schedule_work(&rdev->destroy_work);
11698 }
11699 }
Johannes Berg5e760232011-11-04 11:18:17 +010011700 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011701
11702 rcu_read_unlock();
11703
11704 return NOTIFY_DONE;
11705}
11706
11707static struct notifier_block nl80211_netlink_notifier = {
11708 .notifier_call = nl80211_netlink_notify,
11709};
11710
Jouni Malinen355199e2013-02-27 17:14:27 +020011711void cfg80211_ft_event(struct net_device *netdev,
11712 struct cfg80211_ft_event_params *ft_event)
11713{
11714 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11715 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11716 struct sk_buff *msg;
11717 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011718
11719 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11720
11721 if (!ft_event->target_ap)
11722 return;
11723
11724 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11725 if (!msg)
11726 return;
11727
11728 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011729 if (!hdr)
11730 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011731
Johannes Bergae917c92013-10-25 11:05:22 +020011732 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11733 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11734 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11735 goto out;
11736
11737 if (ft_event->ies &&
11738 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11739 goto out;
11740 if (ft_event->ric_ies &&
11741 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11742 ft_event->ric_ies))
11743 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011744
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011745 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011746
Johannes Berg68eb5502013-11-19 15:19:38 +010011747 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011748 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011749 return;
11750 out:
11751 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011752}
11753EXPORT_SYMBOL(cfg80211_ft_event);
11754
Arend van Spriel5de17982013-04-18 15:49:00 +020011755void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11756{
11757 struct cfg80211_registered_device *rdev;
11758 struct sk_buff *msg;
11759 void *hdr;
11760 u32 nlportid;
11761
11762 rdev = wiphy_to_dev(wdev->wiphy);
11763 if (!rdev->crit_proto_nlportid)
11764 return;
11765
11766 nlportid = rdev->crit_proto_nlportid;
11767 rdev->crit_proto_nlportid = 0;
11768
11769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11770 if (!msg)
11771 return;
11772
11773 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11774 if (!hdr)
11775 goto nla_put_failure;
11776
11777 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11778 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11779 goto nla_put_failure;
11780
11781 genlmsg_end(msg, hdr);
11782
11783 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11784 return;
11785
11786 nla_put_failure:
11787 if (hdr)
11788 genlmsg_cancel(msg, hdr);
11789 nlmsg_free(msg);
11790
11791}
11792EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11793
Johannes Berg348baf02014-01-24 14:06:29 +010011794void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11795{
11796 struct wiphy *wiphy = wdev->wiphy;
11797 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11798 struct sk_buff *msg;
11799 void *hdr;
11800
11801 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11802 if (!msg)
11803 return;
11804
11805 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11806 if (!hdr)
11807 goto out;
11808
11809 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11810 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11811 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11812 goto out;
11813
11814 genlmsg_end(msg, hdr);
11815
11816 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11817 NL80211_MCGRP_MLME, GFP_KERNEL);
11818 return;
11819 out:
11820 nlmsg_free(msg);
11821}
11822
Johannes Berg55682962007-09-20 13:09:35 -040011823/* initialisation/exit functions */
11824
11825int nl80211_init(void)
11826{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011827 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011828
Johannes Berg2a94fe42013-11-19 15:19:39 +010011829 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11830 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011831 if (err)
11832 return err;
11833
Jouni Malinen026331c2010-02-15 12:53:10 +020011834 err = netlink_register_notifier(&nl80211_netlink_notifier);
11835 if (err)
11836 goto err_out;
11837
Johannes Berg55682962007-09-20 13:09:35 -040011838 return 0;
11839 err_out:
11840 genl_unregister_family(&nl80211_fam);
11841 return err;
11842}
11843
11844void nl80211_exit(void)
11845{
Jouni Malinen026331c2010-02-15 12:53:10 +020011846 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011847 genl_unregister_family(&nl80211_fam);
11848}