blob: 7a742594916e177461a5374693e78c07bcbce09b [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 Berg7fee4772012-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 Berg7fee4772012-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 Berg7fee4772012-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 Berg7fee4772012-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 Berg7fee4772012-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 Malinen36aedc902008-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 Malinendc6382c2009-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 Berg2e161f72010-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 },
Johannes Berg55682962007-09-20 13:09:35 -0400385};
386
Johannes Berge31b8212010-10-05 19:39:30 +0200387/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000388static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200389 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200390 [NL80211_KEY_IDX] = { .type = NLA_U8 },
391 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200392 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
394 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200395 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100396 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
397};
398
399/* policy for the key default flags */
400static const struct nla_policy
401nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
402 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
403 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200404};
405
Johannes Bergff1b6e62011-05-04 15:37:28 +0200406/* policy for WoWLAN attributes */
407static const struct nla_policy
408nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
409 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200413 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
416 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100417 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
418};
419
420static const struct nla_policy
421nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
422 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
424 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
425 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
426 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
428 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
429 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
430 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
432 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
433 },
434 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
435 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
436 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200437};
438
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700439/* policy for coalesce rule attributes */
440static const struct nla_policy
441nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
442 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
443 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
444 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
445};
446
Johannes Berge5497d72011-07-05 16:35:40 +0200447/* policy for GTK rekey offload attributes */
448static const struct nla_policy
449nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
450 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
451 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
452 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
453};
454
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300455static const struct nla_policy
456nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200457 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300458 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700459 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300460};
461
Johannes Berg97990a02013-04-19 01:02:55 +0200462static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100466{
Johannes Berg67748892010-10-04 21:14:06 +0200467 int err;
468
Johannes Berg67748892010-10-04 21:14:06 +0200469 rtnl_lock();
470
Johannes Berg97990a02013-04-19 01:02:55 +0200471 if (!cb->args[0]) {
472 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
473 nl80211_fam.attrbuf, nl80211_fam.maxattr,
474 nl80211_policy);
475 if (err)
476 goto out_unlock;
477
478 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
479 nl80211_fam.attrbuf);
480 if (IS_ERR(*wdev)) {
481 err = PTR_ERR(*wdev);
482 goto out_unlock;
483 }
484 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200485 /* 0 is the first index - add 1 to parse only once */
486 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200487 cb->args[1] = (*wdev)->identifier;
488 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200489 /* subtract the 1 again here */
490 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200491 struct wireless_dev *tmp;
492
493 if (!wiphy) {
494 err = -ENODEV;
495 goto out_unlock;
496 }
497 *rdev = wiphy_to_dev(wiphy);
498 *wdev = NULL;
499
Johannes Berg97990a02013-04-19 01:02:55 +0200500 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
501 if (tmp->identifier == cb->args[1]) {
502 *wdev = tmp;
503 break;
504 }
505 }
Johannes Berg97990a02013-04-19 01:02:55 +0200506
507 if (!*wdev) {
508 err = -ENODEV;
509 goto out_unlock;
510 }
Johannes Berg67748892010-10-04 21:14:06 +0200511 }
512
Johannes Berg67748892010-10-04 21:14:06 +0200513 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200514 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200515 rtnl_unlock();
516 return err;
517}
518
Johannes Berg97990a02013-04-19 01:02:55 +0200519static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200520{
Johannes Berg67748892010-10-04 21:14:06 +0200521 rtnl_unlock();
522}
523
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100524/* IE validation */
525static bool is_valid_ie_attr(const struct nlattr *attr)
526{
527 const u8 *pos;
528 int len;
529
530 if (!attr)
531 return true;
532
533 pos = nla_data(attr);
534 len = nla_len(attr);
535
536 while (len) {
537 u8 elemlen;
538
539 if (len < 2)
540 return false;
541 len -= 2;
542
543 elemlen = pos[1];
544 if (elemlen > len)
545 return false;
546
547 len -= elemlen;
548 pos += 2 + elemlen;
549 }
550
551 return true;
552}
553
Johannes Berg55682962007-09-20 13:09:35 -0400554/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000555static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400556 int flags, u8 cmd)
557{
558 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000559 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400560}
561
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400562static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100563 struct ieee80211_channel *chan,
564 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565{
David S. Miller9360ffd2012-03-29 04:41:26 -0400566 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
567 chan->center_freq))
568 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400569
David S. Miller9360ffd2012-03-29 04:41:26 -0400570 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
572 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200573 if (chan->flags & IEEE80211_CHAN_NO_IR) {
574 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
575 goto nla_put_failure;
576 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
577 goto nla_put_failure;
578 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100579 if (chan->flags & IEEE80211_CHAN_RADAR) {
580 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
581 goto nla_put_failure;
582 if (large) {
583 u32 time;
584
585 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
586
587 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
588 chan->dfs_state))
589 goto nla_put_failure;
590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
591 time))
592 goto nla_put_failure;
593 }
594 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400595
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100596 if (large) {
597 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
598 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
599 goto nla_put_failure;
600 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
602 goto nla_put_failure;
603 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
604 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
605 goto nla_put_failure;
606 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
607 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
608 goto nla_put_failure;
609 }
610
David S. Miller9360ffd2012-03-29 04:41:26 -0400611 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
612 DBM_TO_MBM(chan->max_power)))
613 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400614
615 return 0;
616
617 nla_put_failure:
618 return -ENOBUFS;
619}
620
Johannes Berg55682962007-09-20 13:09:35 -0400621/* netlink command implementations */
622
Johannes Bergb9454e82009-07-08 13:29:08 +0200623struct key_parse {
624 struct key_params p;
625 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200626 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200627 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100628 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200629};
630
631static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
632{
633 struct nlattr *tb[NL80211_KEY_MAX + 1];
634 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
635 nl80211_key_policy);
636 if (err)
637 return err;
638
639 k->def = !!tb[NL80211_KEY_DEFAULT];
640 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
641
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100642 if (k->def) {
643 k->def_uni = true;
644 k->def_multi = true;
645 }
646 if (k->defmgmt)
647 k->def_multi = true;
648
Johannes Bergb9454e82009-07-08 13:29:08 +0200649 if (tb[NL80211_KEY_IDX])
650 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
651
652 if (tb[NL80211_KEY_DATA]) {
653 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
654 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
655 }
656
657 if (tb[NL80211_KEY_SEQ]) {
658 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
659 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
660 }
661
662 if (tb[NL80211_KEY_CIPHER])
663 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
664
Johannes Berge31b8212010-10-05 19:39:30 +0200665 if (tb[NL80211_KEY_TYPE]) {
666 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
667 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
668 return -EINVAL;
669 }
670
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100671 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
672 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100673 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
674 tb[NL80211_KEY_DEFAULT_TYPES],
675 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100676 if (err)
677 return err;
678
679 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
680 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 return 0;
684}
685
686static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
687{
688 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
689 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
690 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
691 }
692
693 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
694 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
695 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
696 }
697
698 if (info->attrs[NL80211_ATTR_KEY_IDX])
699 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
700
701 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
702 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
703
704 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
705 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
706
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100707 if (k->def) {
708 k->def_uni = true;
709 k->def_multi = true;
710 }
711 if (k->defmgmt)
712 k->def_multi = true;
713
Johannes Berge31b8212010-10-05 19:39:30 +0200714 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
715 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
716 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
717 return -EINVAL;
718 }
719
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100720 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
721 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
722 int err = nla_parse_nested(
723 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
724 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
725 nl80211_key_default_policy);
726 if (err)
727 return err;
728
729 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
730 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
731 }
732
Johannes Bergb9454e82009-07-08 13:29:08 +0200733 return 0;
734}
735
736static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
737{
738 int err;
739
740 memset(k, 0, sizeof(*k));
741 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200742 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200743
744 if (info->attrs[NL80211_ATTR_KEY])
745 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
746 else
747 err = nl80211_parse_key_old(info, k);
748
749 if (err)
750 return err;
751
752 if (k->def && k->defmgmt)
753 return -EINVAL;
754
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100755 if (k->defmgmt) {
756 if (k->def_uni || !k->def_multi)
757 return -EINVAL;
758 }
759
Johannes Bergb9454e82009-07-08 13:29:08 +0200760 if (k->idx != -1) {
761 if (k->defmgmt) {
762 if (k->idx < 4 || k->idx > 5)
763 return -EINVAL;
764 } else if (k->def) {
765 if (k->idx < 0 || k->idx > 3)
766 return -EINVAL;
767 } else {
768 if (k->idx < 0 || k->idx > 5)
769 return -EINVAL;
770 }
771 }
772
773 return 0;
774}
775
Johannes Bergfffd0932009-07-08 14:22:54 +0200776static struct cfg80211_cached_keys *
777nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530778 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200779{
780 struct key_parse parse;
781 struct nlattr *key;
782 struct cfg80211_cached_keys *result;
783 int rem, err, def = 0;
784
785 result = kzalloc(sizeof(*result), GFP_KERNEL);
786 if (!result)
787 return ERR_PTR(-ENOMEM);
788
789 result->def = -1;
790 result->defmgmt = -1;
791
792 nla_for_each_nested(key, keys, rem) {
793 memset(&parse, 0, sizeof(parse));
794 parse.idx = -1;
795
796 err = nl80211_parse_key_new(key, &parse);
797 if (err)
798 goto error;
799 err = -EINVAL;
800 if (!parse.p.key)
801 goto error;
802 if (parse.idx < 0 || parse.idx > 4)
803 goto error;
804 if (parse.def) {
805 if (def)
806 goto error;
807 def = 1;
808 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100809 if (!parse.def_uni || !parse.def_multi)
810 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200811 } else if (parse.defmgmt)
812 goto error;
813 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200814 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200815 if (err)
816 goto error;
817 result->params[parse.idx].cipher = parse.p.cipher;
818 result->params[parse.idx].key_len = parse.p.key_len;
819 result->params[parse.idx].key = result->data[parse.idx];
820 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530821
822 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
823 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
824 if (no_ht)
825 *no_ht = true;
826 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200827 }
828
829 return result;
830 error:
831 kfree(result);
832 return ERR_PTR(err);
833}
834
835static int nl80211_key_allowed(struct wireless_dev *wdev)
836{
837 ASSERT_WDEV_LOCK(wdev);
838
Johannes Bergfffd0932009-07-08 14:22:54 +0200839 switch (wdev->iftype) {
840 case NL80211_IFTYPE_AP:
841 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200842 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700843 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200844 break;
845 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200846 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200847 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200848 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 return -ENOLINK;
850 break;
851 default:
852 return -EINVAL;
853 }
854
855 return 0;
856}
857
Johannes Berg7527a782011-05-13 10:58:57 +0200858static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
859{
860 struct nlattr *nl_modes = nla_nest_start(msg, attr);
861 int i;
862
863 if (!nl_modes)
864 goto nla_put_failure;
865
866 i = 0;
867 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400868 if ((ifmodes & 1) && nla_put_flag(msg, i))
869 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200870 ifmodes >>= 1;
871 i++;
872 }
873
874 nla_nest_end(msg, nl_modes);
875 return 0;
876
877nla_put_failure:
878 return -ENOBUFS;
879}
880
881static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100882 struct sk_buff *msg,
883 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200884{
885 struct nlattr *nl_combis;
886 int i, j;
887
888 nl_combis = nla_nest_start(msg,
889 NL80211_ATTR_INTERFACE_COMBINATIONS);
890 if (!nl_combis)
891 goto nla_put_failure;
892
893 for (i = 0; i < wiphy->n_iface_combinations; i++) {
894 const struct ieee80211_iface_combination *c;
895 struct nlattr *nl_combi, *nl_limits;
896
897 c = &wiphy->iface_combinations[i];
898
899 nl_combi = nla_nest_start(msg, i + 1);
900 if (!nl_combi)
901 goto nla_put_failure;
902
903 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
904 if (!nl_limits)
905 goto nla_put_failure;
906
907 for (j = 0; j < c->n_limits; j++) {
908 struct nlattr *nl_limit;
909
910 nl_limit = nla_nest_start(msg, j + 1);
911 if (!nl_limit)
912 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400913 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
914 c->limits[j].max))
915 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200916 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
917 c->limits[j].types))
918 goto nla_put_failure;
919 nla_nest_end(msg, nl_limit);
920 }
921
922 nla_nest_end(msg, nl_limits);
923
David S. Miller9360ffd2012-03-29 04:41:26 -0400924 if (c->beacon_int_infra_match &&
925 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
926 goto nla_put_failure;
927 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
928 c->num_different_channels) ||
929 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
930 c->max_interfaces))
931 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100932 if (large &&
933 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
934 c->radar_detect_widths))
935 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200936
937 nla_nest_end(msg, nl_combi);
938 }
939
940 nla_nest_end(msg, nl_combis);
941
942 return 0;
943nla_put_failure:
944 return -ENOBUFS;
945}
946
Johannes Berg3713b4e2013-02-14 16:19:38 +0100947#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100948static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
949 struct sk_buff *msg)
950{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200951 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100952 struct nlattr *nl_tcp;
953
954 if (!tcp)
955 return 0;
956
957 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
958 if (!nl_tcp)
959 return -ENOBUFS;
960
961 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
962 tcp->data_payload_max))
963 return -ENOBUFS;
964
965 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
966 tcp->data_payload_max))
967 return -ENOBUFS;
968
969 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
970 return -ENOBUFS;
971
972 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
973 sizeof(*tcp->tok), tcp->tok))
974 return -ENOBUFS;
975
976 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
977 tcp->data_interval_max))
978 return -ENOBUFS;
979
980 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
981 tcp->wake_payload_max))
982 return -ENOBUFS;
983
984 nla_nest_end(msg, nl_tcp);
985 return 0;
986}
987
Johannes Berg3713b4e2013-02-14 16:19:38 +0100988static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100989 struct cfg80211_registered_device *dev,
990 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100991{
992 struct nlattr *nl_wowlan;
993
Johannes Berg964dc9e2013-06-03 17:25:34 +0200994 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100995 return 0;
996
997 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
998 if (!nl_wowlan)
999 return -ENOBUFS;
1000
Johannes Berg964dc9e2013-06-03 17:25:34 +02001001 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001002 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001003 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001005 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001006 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001007 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001008 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001009 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001010 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001011 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001012 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001013 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001014 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001015 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001016 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1017 return -ENOBUFS;
1018
Johannes Berg964dc9e2013-06-03 17:25:34 +02001019 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001020 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 .max_patterns = dev->wiphy.wowlan->n_patterns,
1022 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1023 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1024 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001025 };
1026
1027 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1028 sizeof(pat), &pat))
1029 return -ENOBUFS;
1030 }
1031
Johannes Bergb56cf722013-02-20 01:02:38 +01001032 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1033 return -ENOBUFS;
1034
Johannes Berg3713b4e2013-02-14 16:19:38 +01001035 nla_nest_end(msg, nl_wowlan);
1036
1037 return 0;
1038}
1039#endif
1040
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001041static int nl80211_send_coalesce(struct sk_buff *msg,
1042 struct cfg80211_registered_device *dev)
1043{
1044 struct nl80211_coalesce_rule_support rule;
1045
1046 if (!dev->wiphy.coalesce)
1047 return 0;
1048
1049 rule.max_rules = dev->wiphy.coalesce->n_rules;
1050 rule.max_delay = dev->wiphy.coalesce->max_delay;
1051 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1052 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1053 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1054 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1055
1056 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1057 return -ENOBUFS;
1058
1059 return 0;
1060}
1061
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1063 struct ieee80211_supported_band *sband)
1064{
1065 struct nlattr *nl_rates, *nl_rate;
1066 struct ieee80211_rate *rate;
1067 int i;
1068
1069 /* add HT info */
1070 if (sband->ht_cap.ht_supported &&
1071 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1072 sizeof(sband->ht_cap.mcs),
1073 &sband->ht_cap.mcs) ||
1074 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1075 sband->ht_cap.cap) ||
1076 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1077 sband->ht_cap.ampdu_factor) ||
1078 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1079 sband->ht_cap.ampdu_density)))
1080 return -ENOBUFS;
1081
1082 /* add VHT info */
1083 if (sband->vht_cap.vht_supported &&
1084 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1085 sizeof(sband->vht_cap.vht_mcs),
1086 &sband->vht_cap.vht_mcs) ||
1087 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1088 sband->vht_cap.cap)))
1089 return -ENOBUFS;
1090
1091 /* add bitrates */
1092 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1093 if (!nl_rates)
1094 return -ENOBUFS;
1095
1096 for (i = 0; i < sband->n_bitrates; i++) {
1097 nl_rate = nla_nest_start(msg, i);
1098 if (!nl_rate)
1099 return -ENOBUFS;
1100
1101 rate = &sband->bitrates[i];
1102 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1103 rate->bitrate))
1104 return -ENOBUFS;
1105 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1106 nla_put_flag(msg,
1107 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1108 return -ENOBUFS;
1109
1110 nla_nest_end(msg, nl_rate);
1111 }
1112
1113 nla_nest_end(msg, nl_rates);
1114
1115 return 0;
1116}
1117
1118static int
1119nl80211_send_mgmt_stypes(struct sk_buff *msg,
1120 const struct ieee80211_txrx_stypes *mgmt_stypes)
1121{
1122 u16 stypes;
1123 struct nlattr *nl_ftypes, *nl_ifs;
1124 enum nl80211_iftype ift;
1125 int i;
1126
1127 if (!mgmt_stypes)
1128 return 0;
1129
1130 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1131 if (!nl_ifs)
1132 return -ENOBUFS;
1133
1134 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1135 nl_ftypes = nla_nest_start(msg, ift);
1136 if (!nl_ftypes)
1137 return -ENOBUFS;
1138 i = 0;
1139 stypes = mgmt_stypes[ift].tx;
1140 while (stypes) {
1141 if ((stypes & 1) &&
1142 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1143 (i << 4) | IEEE80211_FTYPE_MGMT))
1144 return -ENOBUFS;
1145 stypes >>= 1;
1146 i++;
1147 }
1148 nla_nest_end(msg, nl_ftypes);
1149 }
1150
1151 nla_nest_end(msg, nl_ifs);
1152
1153 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1154 if (!nl_ifs)
1155 return -ENOBUFS;
1156
1157 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1158 nl_ftypes = nla_nest_start(msg, ift);
1159 if (!nl_ftypes)
1160 return -ENOBUFS;
1161 i = 0;
1162 stypes = mgmt_stypes[ift].rx;
1163 while (stypes) {
1164 if ((stypes & 1) &&
1165 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1166 (i << 4) | IEEE80211_FTYPE_MGMT))
1167 return -ENOBUFS;
1168 stypes >>= 1;
1169 i++;
1170 }
1171 nla_nest_end(msg, nl_ftypes);
1172 }
1173 nla_nest_end(msg, nl_ifs);
1174
1175 return 0;
1176}
1177
Johannes Berg86e8cf92013-06-19 10:57:22 +02001178struct nl80211_dump_wiphy_state {
1179 s64 filter_wiphy;
1180 long start;
1181 long split_start, band_start, chan_start;
1182 bool split;
1183};
1184
Johannes Berg3713b4e2013-02-14 16:19:38 +01001185static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1186 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001187 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001188{
1189 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001190 struct nlattr *nl_bands, *nl_band;
1191 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001192 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001193 enum ieee80211_band band;
1194 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001195 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001196 const struct ieee80211_txrx_stypes *mgmt_stypes =
1197 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001198 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001199
Eric W. Biederman15e47302012-09-07 20:12:54 +00001200 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001201 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001202 return -ENOBUFS;
1203
Johannes Berg86e8cf92013-06-19 10:57:22 +02001204 if (WARN_ON(!state))
1205 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001206
David S. Miller9360ffd2012-03-29 04:41:26 -04001207 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001208 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1209 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001210 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001211 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001212 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001213
Johannes Berg86e8cf92013-06-19 10:57:22 +02001214 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001215 case 0:
1216 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1217 dev->wiphy.retry_short) ||
1218 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1219 dev->wiphy.retry_long) ||
1220 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1221 dev->wiphy.frag_threshold) ||
1222 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1223 dev->wiphy.rts_threshold) ||
1224 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1225 dev->wiphy.coverage_class) ||
1226 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1227 dev->wiphy.max_scan_ssids) ||
1228 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1229 dev->wiphy.max_sched_scan_ssids) ||
1230 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1231 dev->wiphy.max_scan_ie_len) ||
1232 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1233 dev->wiphy.max_sched_scan_ie_len) ||
1234 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1235 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001236 goto nla_put_failure;
1237
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1239 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1240 goto nla_put_failure;
1241 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1242 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1243 goto nla_put_failure;
1244 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1245 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1246 goto nla_put_failure;
1247 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1248 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1249 goto nla_put_failure;
1250 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1251 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1252 goto nla_put_failure;
1253 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1254 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001255 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001256 state->split_start++;
1257 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001258 break;
1259 case 1:
1260 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1261 sizeof(u32) * dev->wiphy.n_cipher_suites,
1262 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001263 goto nla_put_failure;
1264
Johannes Berg3713b4e2013-02-14 16:19:38 +01001265 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1266 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001267 goto nla_put_failure;
1268
Johannes Berg3713b4e2013-02-14 16:19:38 +01001269 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1270 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1271 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001272
Johannes Berg3713b4e2013-02-14 16:19:38 +01001273 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1274 dev->wiphy.available_antennas_tx) ||
1275 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1276 dev->wiphy.available_antennas_rx))
1277 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001278
Johannes Berg3713b4e2013-02-14 16:19:38 +01001279 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1280 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1281 dev->wiphy.probe_resp_offload))
1282 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001283
Johannes Berg3713b4e2013-02-14 16:19:38 +01001284 if ((dev->wiphy.available_antennas_tx ||
1285 dev->wiphy.available_antennas_rx) &&
1286 dev->ops->get_antenna) {
1287 u32 tx_ant = 0, rx_ant = 0;
1288 int res;
1289 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1290 if (!res) {
1291 if (nla_put_u32(msg,
1292 NL80211_ATTR_WIPHY_ANTENNA_TX,
1293 tx_ant) ||
1294 nla_put_u32(msg,
1295 NL80211_ATTR_WIPHY_ANTENNA_RX,
1296 rx_ant))
1297 goto nla_put_failure;
1298 }
Johannes Bergee688b002008-01-24 19:38:39 +01001299 }
1300
Johannes Berg86e8cf92013-06-19 10:57:22 +02001301 state->split_start++;
1302 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001303 break;
1304 case 2:
1305 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1306 dev->wiphy.interface_modes))
1307 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001308 state->split_start++;
1309 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001310 break;
1311 case 3:
1312 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1313 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001314 goto nla_put_failure;
1315
Johannes Berg86e8cf92013-06-19 10:57:22 +02001316 for (band = state->band_start;
1317 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 struct ieee80211_supported_band *sband;
1319
1320 sband = dev->wiphy.bands[band];
1321
1322 if (!sband)
1323 continue;
1324
1325 nl_band = nla_nest_start(msg, band);
1326 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001327 goto nla_put_failure;
1328
Johannes Berg86e8cf92013-06-19 10:57:22 +02001329 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 case 0:
1331 if (nl80211_send_band_rateinfo(msg, sband))
1332 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001333 state->chan_start++;
1334 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001335 break;
1336 default:
1337 /* add frequencies */
1338 nl_freqs = nla_nest_start(
1339 msg, NL80211_BAND_ATTR_FREQS);
1340 if (!nl_freqs)
1341 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001342
Johannes Berg86e8cf92013-06-19 10:57:22 +02001343 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001344 i < sband->n_channels;
1345 i++) {
1346 nl_freq = nla_nest_start(msg, i);
1347 if (!nl_freq)
1348 goto nla_put_failure;
1349
1350 chan = &sband->channels[i];
1351
Johannes Berg86e8cf92013-06-19 10:57:22 +02001352 if (nl80211_msg_put_channel(
1353 msg, chan,
1354 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001355 goto nla_put_failure;
1356
1357 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001358 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001359 break;
1360 }
1361 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001362 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001363 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001364 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001365 nla_nest_end(msg, nl_freqs);
1366 }
1367
1368 nla_nest_end(msg, nl_band);
1369
Johannes Berg86e8cf92013-06-19 10:57:22 +02001370 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001372 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001373 band--;
1374 break;
1375 }
Johannes Bergee688b002008-01-24 19:38:39 +01001376 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001377 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001378
Johannes Berg3713b4e2013-02-14 16:19:38 +01001379 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001381 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001382 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001383
Johannes Berg3713b4e2013-02-14 16:19:38 +01001384 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001385 if (state->band_start == 0 && state->chan_start == 0)
1386 state->split_start++;
1387 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001388 break;
1389 case 4:
1390 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1391 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001392 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001393
1394 i = 0;
1395#define CMD(op, n) \
1396 do { \
1397 if (dev->ops->op) { \
1398 i++; \
1399 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1400 goto nla_put_failure; \
1401 } \
1402 } while (0)
1403
1404 CMD(add_virtual_intf, NEW_INTERFACE);
1405 CMD(change_virtual_intf, SET_INTERFACE);
1406 CMD(add_key, NEW_KEY);
1407 CMD(start_ap, START_AP);
1408 CMD(add_station, NEW_STATION);
1409 CMD(add_mpath, NEW_MPATH);
1410 CMD(update_mesh_config, SET_MESH_CONFIG);
1411 CMD(change_bss, SET_BSS);
1412 CMD(auth, AUTHENTICATE);
1413 CMD(assoc, ASSOCIATE);
1414 CMD(deauth, DEAUTHENTICATE);
1415 CMD(disassoc, DISASSOCIATE);
1416 CMD(join_ibss, JOIN_IBSS);
1417 CMD(join_mesh, JOIN_MESH);
1418 CMD(set_pmksa, SET_PMKSA);
1419 CMD(del_pmksa, DEL_PMKSA);
1420 CMD(flush_pmksa, FLUSH_PMKSA);
1421 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1422 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1423 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1424 CMD(mgmt_tx, FRAME);
1425 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1426 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1427 i++;
1428 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1429 goto nla_put_failure;
1430 }
1431 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1432 dev->ops->join_mesh) {
1433 i++;
1434 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1435 goto nla_put_failure;
1436 }
1437 CMD(set_wds_peer, SET_WDS_PEER);
1438 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1439 CMD(tdls_mgmt, TDLS_MGMT);
1440 CMD(tdls_oper, TDLS_OPER);
1441 }
1442 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1443 CMD(sched_scan_start, START_SCHED_SCAN);
1444 CMD(probe_client, PROBE_CLIENT);
1445 CMD(set_noack_map, SET_NOACK_MAP);
1446 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1447 i++;
1448 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1449 goto nla_put_failure;
1450 }
1451 CMD(start_p2p_device, START_P2P_DEVICE);
1452 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001453 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001454 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1455 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001456 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1457 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001458 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001459 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001460
Kalle Valo4745fc02011-11-17 19:06:10 +02001461#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001463#endif
1464
Johannes Berg8fdc6212009-03-14 09:34:01 +01001465#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001466
Johannes Berg3713b4e2013-02-14 16:19:38 +01001467 if (dev->ops->connect || dev->ops->auth) {
1468 i++;
1469 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001470 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001471 }
1472
Johannes Berg3713b4e2013-02-14 16:19:38 +01001473 if (dev->ops->disconnect || dev->ops->deauth) {
1474 i++;
1475 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1476 goto nla_put_failure;
1477 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001478
Johannes Berg3713b4e2013-02-14 16:19:38 +01001479 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001480 state->split_start++;
1481 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001482 break;
1483 case 5:
1484 if (dev->ops->remain_on_channel &&
1485 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1486 nla_put_u32(msg,
1487 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1488 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001489 goto nla_put_failure;
1490
Johannes Berg3713b4e2013-02-14 16:19:38 +01001491 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1492 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1493 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001494
Johannes Berg3713b4e2013-02-14 16:19:38 +01001495 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1496 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001497 state->split_start++;
1498 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001499 break;
1500 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001501#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001502 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001504 state->split_start++;
1505 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001506 break;
1507#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001508 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509#endif
1510 case 7:
1511 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1512 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001513 goto nla_put_failure;
1514
Johannes Berg86e8cf92013-06-19 10:57:22 +02001515 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1516 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001517 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001518
Johannes Berg86e8cf92013-06-19 10:57:22 +02001519 state->split_start++;
1520 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001521 break;
1522 case 8:
1523 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1524 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1525 dev->wiphy.ap_sme_capa))
1526 goto nla_put_failure;
1527
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001528 features = dev->wiphy.features;
1529 /*
1530 * We can only add the per-channel limit information if the
1531 * dump is split, otherwise it makes it too big. Therefore
1532 * only advertise it in that case.
1533 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001534 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001535 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1536 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001537 goto nla_put_failure;
1538
1539 if (dev->wiphy.ht_capa_mod_mask &&
1540 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1541 sizeof(*dev->wiphy.ht_capa_mod_mask),
1542 dev->wiphy.ht_capa_mod_mask))
1543 goto nla_put_failure;
1544
1545 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1546 dev->wiphy.max_acl_mac_addrs &&
1547 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1548 dev->wiphy.max_acl_mac_addrs))
1549 goto nla_put_failure;
1550
1551 /*
1552 * Any information below this point is only available to
1553 * applications that can deal with it being split. This
1554 * helps ensure that newly added capabilities don't break
1555 * older tools by overrunning their buffers.
1556 *
1557 * We still increment split_start so that in the split
1558 * case we'll continue with more data in the next round,
1559 * but break unconditionally so unsplit data stops here.
1560 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001561 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001562 break;
1563 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001564 if (dev->wiphy.extended_capabilities &&
1565 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1566 dev->wiphy.extended_capabilities_len,
1567 dev->wiphy.extended_capabilities) ||
1568 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1569 dev->wiphy.extended_capabilities_len,
1570 dev->wiphy.extended_capabilities_mask)))
1571 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001572
Johannes Bergee2aca32013-02-21 17:36:01 +01001573 if (dev->wiphy.vht_capa_mod_mask &&
1574 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1575 sizeof(*dev->wiphy.vht_capa_mod_mask),
1576 dev->wiphy.vht_capa_mod_mask))
1577 goto nla_put_failure;
1578
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001579 state->split_start++;
1580 break;
1581 case 10:
1582 if (nl80211_send_coalesce(msg, dev))
1583 goto nla_put_failure;
1584
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001585 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1586 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1587 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1588 goto nla_put_failure;
Johannes Bergad7e7182013-11-13 13:37:47 +01001589 state->split_start++;
1590 break;
1591 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001592 if (dev->wiphy.n_vendor_commands) {
1593 const struct nl80211_vendor_cmd_info *info;
1594 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001595
Johannes Berg567ffc32013-12-18 14:43:31 +01001596 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1597 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001598 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001599
1600 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1601 info = &dev->wiphy.vendor_commands[i].info;
1602 if (nla_put(msg, i + 1, sizeof(*info), info))
1603 goto nla_put_failure;
1604 }
1605 nla_nest_end(msg, nested);
1606 }
1607
1608 if (dev->wiphy.n_vendor_events) {
1609 const struct nl80211_vendor_cmd_info *info;
1610 struct nlattr *nested;
1611
1612 nested = nla_nest_start(msg,
1613 NL80211_ATTR_VENDOR_EVENTS);
1614 if (!nested)
1615 goto nla_put_failure;
1616
1617 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1618 info = &dev->wiphy.vendor_events[i];
1619 if (nla_put(msg, i + 1, sizeof(*info), info))
1620 goto nla_put_failure;
1621 }
1622 nla_nest_end(msg, nested);
1623 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001624
Johannes Berg3713b4e2013-02-14 16:19:38 +01001625 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001626 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001627 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001628 }
Johannes Berg55682962007-09-20 13:09:35 -04001629 return genlmsg_end(msg, hdr);
1630
1631 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001632 genlmsg_cancel(msg, hdr);
1633 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001634}
1635
Johannes Berg86e8cf92013-06-19 10:57:22 +02001636static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1637 struct netlink_callback *cb,
1638 struct nl80211_dump_wiphy_state *state)
1639{
1640 struct nlattr **tb = nl80211_fam.attrbuf;
1641 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1642 tb, nl80211_fam.maxattr, nl80211_policy);
1643 /* ignore parse errors for backward compatibility */
1644 if (ret)
1645 return 0;
1646
1647 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1648 if (tb[NL80211_ATTR_WIPHY])
1649 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1650 if (tb[NL80211_ATTR_WDEV])
1651 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1652 if (tb[NL80211_ATTR_IFINDEX]) {
1653 struct net_device *netdev;
1654 struct cfg80211_registered_device *rdev;
1655 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1656
Ying Xue7f2b8562014-01-15 10:23:45 +08001657 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001658 if (!netdev)
1659 return -ENODEV;
1660 if (netdev->ieee80211_ptr) {
1661 rdev = wiphy_to_dev(
1662 netdev->ieee80211_ptr->wiphy);
1663 state->filter_wiphy = rdev->wiphy_idx;
1664 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001665 }
1666
1667 return 0;
1668}
1669
Johannes Berg55682962007-09-20 13:09:35 -04001670static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1671{
Johannes Berg645e77d2013-03-01 14:03:49 +01001672 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001673 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001674 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001675
Johannes Berg5fe231e2013-05-08 21:45:15 +02001676 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001677 if (!state) {
1678 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001679 if (!state) {
1680 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001681 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001682 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001683 state->filter_wiphy = -1;
1684 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1685 if (ret) {
1686 kfree(state);
1687 rtnl_unlock();
1688 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001689 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001690 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001691 }
1692
Johannes Berg79c97e92009-07-07 03:56:12 +02001693 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001694 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1695 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001696 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001697 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001698 if (state->filter_wiphy != -1 &&
1699 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001700 continue;
1701 /* attempt to fit multiple wiphy data chunks into the skb */
1702 do {
1703 ret = nl80211_send_wiphy(dev, skb,
1704 NETLINK_CB(cb->skb).portid,
1705 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001706 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001707 if (ret < 0) {
1708 /*
1709 * If sending the wiphy data didn't fit (ENOBUFS
1710 * or EMSGSIZE returned), this SKB is still
1711 * empty (so it's not too big because another
1712 * wiphy dataset is already in the skb) and
1713 * we've not tried to adjust the dump allocation
1714 * yet ... then adjust the alloc size to be
1715 * bigger, and return 1 but with the empty skb.
1716 * This results in an empty message being RX'ed
1717 * in userspace, but that is ignored.
1718 *
1719 * We can then retry with the larger buffer.
1720 */
1721 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1722 !skb->len &&
1723 cb->min_dump_alloc < 4096) {
1724 cb->min_dump_alloc = 4096;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001725 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001726 return 1;
1727 }
1728 idx--;
1729 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001730 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001731 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001732 break;
Johannes Berg55682962007-09-20 13:09:35 -04001733 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001734 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001735
Johannes Berg86e8cf92013-06-19 10:57:22 +02001736 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001737
1738 return skb->len;
1739}
1740
Johannes Berg86e8cf92013-06-19 10:57:22 +02001741static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1742{
1743 kfree((void *)cb->args[0]);
1744 return 0;
1745}
1746
Johannes Berg55682962007-09-20 13:09:35 -04001747static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1748{
1749 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001750 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001751 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001752
Johannes Berg645e77d2013-03-01 14:03:49 +01001753 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001754 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001755 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001756
Johannes Berg3713b4e2013-02-14 16:19:38 +01001757 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001758 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001759 nlmsg_free(msg);
1760 return -ENOBUFS;
1761 }
Johannes Berg55682962007-09-20 13:09:35 -04001762
Johannes Berg134e6372009-07-10 09:51:34 +00001763 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001764}
1765
Jouni Malinen31888482008-10-30 16:59:24 +02001766static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1767 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1768 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1769 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1770 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1771 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1772};
1773
1774static int parse_txq_params(struct nlattr *tb[],
1775 struct ieee80211_txq_params *txq_params)
1776{
Johannes Berga3304b02012-03-28 11:04:24 +02001777 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001778 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1779 !tb[NL80211_TXQ_ATTR_AIFS])
1780 return -EINVAL;
1781
Johannes Berga3304b02012-03-28 11:04:24 +02001782 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001783 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1784 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1785 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1786 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1787
Johannes Berga3304b02012-03-28 11:04:24 +02001788 if (txq_params->ac >= NL80211_NUM_ACS)
1789 return -EINVAL;
1790
Jouni Malinen31888482008-10-30 16:59:24 +02001791 return 0;
1792}
1793
Johannes Bergf444de02010-05-05 15:25:02 +02001794static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1795{
1796 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001797 * You can only set the channel explicitly for WDS interfaces,
1798 * all others have their channel managed via their respective
1799 * "establish a connection" command (connect, join, ...)
1800 *
1801 * For AP/GO and mesh mode, the channel can be set with the
1802 * channel userspace API, but is only stored and passed to the
1803 * low-level driver when the AP starts or the mesh is joined.
1804 * This is for backward compatibility, userspace can also give
1805 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001806 *
1807 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001808 * whatever else is going on, so they have their own special
1809 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001810 */
1811 return !wdev ||
1812 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001813 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001814 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1815 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001816}
1817
Johannes Berg683b6d32012-11-08 21:25:48 +01001818static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1819 struct genl_info *info,
1820 struct cfg80211_chan_def *chandef)
1821{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301822 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001823
1824 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1825 return -EINVAL;
1826
1827 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1828
1829 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001830 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1831 chandef->center_freq1 = control_freq;
1832 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001833
1834 /* Primary channel not allowed */
1835 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1836 return -EINVAL;
1837
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001838 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1839 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001840
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001841 chantype = nla_get_u32(
1842 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1843
1844 switch (chantype) {
1845 case NL80211_CHAN_NO_HT:
1846 case NL80211_CHAN_HT20:
1847 case NL80211_CHAN_HT40PLUS:
1848 case NL80211_CHAN_HT40MINUS:
1849 cfg80211_chandef_create(chandef, chandef->chan,
1850 chantype);
1851 break;
1852 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001853 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001854 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001855 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1856 chandef->width =
1857 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1858 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1859 chandef->center_freq1 =
1860 nla_get_u32(
1861 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1862 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1863 chandef->center_freq2 =
1864 nla_get_u32(
1865 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1866 }
1867
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001868 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001869 return -EINVAL;
1870
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001871 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1872 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001873 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001874
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001875 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1876 chandef->width == NL80211_CHAN_WIDTH_10) &&
1877 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1878 return -EINVAL;
1879
Johannes Berg683b6d32012-11-08 21:25:48 +01001880 return 0;
1881}
1882
Johannes Bergf444de02010-05-05 15:25:02 +02001883static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1884 struct wireless_dev *wdev,
1885 struct genl_info *info)
1886{
Johannes Berg683b6d32012-11-08 21:25:48 +01001887 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001888 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001889 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1890
1891 if (wdev)
1892 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001893
Johannes Bergf444de02010-05-05 15:25:02 +02001894 if (!nl80211_can_set_dev_channel(wdev))
1895 return -EOPNOTSUPP;
1896
Johannes Berg683b6d32012-11-08 21:25:48 +01001897 result = nl80211_parse_chandef(rdev, info, &chandef);
1898 if (result)
1899 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001900
Johannes Berge8c9bd52012-06-06 08:18:22 +02001901 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001902 case NL80211_IFTYPE_AP:
1903 case NL80211_IFTYPE_P2P_GO:
1904 if (wdev->beacon_interval) {
1905 result = -EBUSY;
1906 break;
1907 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001908 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001909 result = -EINVAL;
1910 break;
1911 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001912 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001913 result = 0;
1914 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001915 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001916 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001917 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001918 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001919 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001920 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001921 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001922 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001923 }
Johannes Bergf444de02010-05-05 15:25:02 +02001924
1925 return result;
1926}
1927
1928static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1929{
Johannes Berg4c476992010-10-04 21:36:35 +02001930 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1931 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001932
Johannes Berg4c476992010-10-04 21:36:35 +02001933 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001934}
1935
Bill Jordane8347eb2010-10-01 13:54:28 -04001936static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1937{
Johannes Berg43b19952010-10-07 13:10:30 +02001938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1939 struct net_device *dev = info->user_ptr[1];
1940 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001941 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001942
1943 if (!info->attrs[NL80211_ATTR_MAC])
1944 return -EINVAL;
1945
Johannes Berg43b19952010-10-07 13:10:30 +02001946 if (netif_running(dev))
1947 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001948
Johannes Berg43b19952010-10-07 13:10:30 +02001949 if (!rdev->ops->set_wds_peer)
1950 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001951
Johannes Berg43b19952010-10-07 13:10:30 +02001952 if (wdev->iftype != NL80211_IFTYPE_WDS)
1953 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001954
1955 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001956 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001957}
1958
1959
Johannes Berg55682962007-09-20 13:09:35 -04001960static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1961{
1962 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001963 struct net_device *netdev = NULL;
1964 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001965 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001966 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001967 u32 changed;
1968 u8 retry_short = 0, retry_long = 0;
1969 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001970 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001971
Johannes Berg5fe231e2013-05-08 21:45:15 +02001972 ASSERT_RTNL();
1973
Johannes Bergf444de02010-05-05 15:25:02 +02001974 /*
1975 * Try to find the wiphy and netdev. Normally this
1976 * function shouldn't need the netdev, but this is
1977 * done for backward compatibility -- previously
1978 * setting the channel was done per wiphy, but now
1979 * it is per netdev. Previous userland like hostapd
1980 * also passed a netdev to set_wiphy, so that it is
1981 * possible to let that go to the right netdev!
1982 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001983
Johannes Bergf444de02010-05-05 15:25:02 +02001984 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1985 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1986
Ying Xue7f2b8562014-01-15 10:23:45 +08001987 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001988 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02001989 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001990 else
Johannes Bergf444de02010-05-05 15:25:02 +02001991 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001992 }
1993
Johannes Bergf444de02010-05-05 15:25:02 +02001994 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001995 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1996 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001997 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02001998 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001999 wdev = NULL;
2000 netdev = NULL;
2001 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002002 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002003 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002004
2005 /*
2006 * end workaround code, by now the rdev is available
2007 * and locked, and wdev may or may not be NULL.
2008 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002009
2010 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002011 result = cfg80211_dev_rename(
2012 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002013
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002014 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002015 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002016
Jouni Malinen31888482008-10-30 16:59:24 +02002017 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2018 struct ieee80211_txq_params txq_params;
2019 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2020
Ying Xue7f2b8562014-01-15 10:23:45 +08002021 if (!rdev->ops->set_txq_params)
2022 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002023
Ying Xue7f2b8562014-01-15 10:23:45 +08002024 if (!netdev)
2025 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002026
Johannes Berg133a3ff2011-11-03 14:50:13 +01002027 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002028 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2029 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002030
Ying Xue7f2b8562014-01-15 10:23:45 +08002031 if (!netif_running(netdev))
2032 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002033
Jouni Malinen31888482008-10-30 16:59:24 +02002034 nla_for_each_nested(nl_txq_params,
2035 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2036 rem_txq_params) {
2037 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2038 nla_data(nl_txq_params),
2039 nla_len(nl_txq_params),
2040 txq_params_policy);
2041 result = parse_txq_params(tb, &txq_params);
2042 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002043 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002044
Hila Gonene35e4d22012-06-27 17:19:42 +03002045 result = rdev_set_txq_params(rdev, netdev,
2046 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002047 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002048 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002049 }
2050 }
2051
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002052 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002053 result = __nl80211_set_channel(rdev,
2054 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2055 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002056 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002057 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002058 }
2059
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002060 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002061 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002062 enum nl80211_tx_power_setting type;
2063 int idx, mbm = 0;
2064
Johannes Bergc8442112012-10-24 10:17:18 +02002065 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2066 txp_wdev = NULL;
2067
Ying Xue7f2b8562014-01-15 10:23:45 +08002068 if (!rdev->ops->set_tx_power)
2069 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002070
2071 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2072 type = nla_get_u32(info->attrs[idx]);
2073
2074 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002075 (type != NL80211_TX_POWER_AUTOMATIC))
2076 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002077
2078 if (type != NL80211_TX_POWER_AUTOMATIC) {
2079 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2080 mbm = nla_get_u32(info->attrs[idx]);
2081 }
2082
Johannes Bergc8442112012-10-24 10:17:18 +02002083 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002084 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002085 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002086 }
2087
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002088 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2089 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2090 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002091 if ((!rdev->wiphy.available_antennas_tx &&
2092 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 !rdev->ops->set_antenna)
2094 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002095
2096 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2097 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2098
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002099 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002100 * available antenna masks, except for the "all" mask */
2101 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002102 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2103 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002104
Bruno Randolf7f531e02010-12-16 11:30:22 +09002105 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2106 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002107
Hila Gonene35e4d22012-06-27 17:19:42 +03002108 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002109 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002110 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002111 }
2112
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002113 changed = 0;
2114
2115 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2116 retry_short = nla_get_u8(
2117 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002118 if (retry_short == 0)
2119 return -EINVAL;
2120
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002121 changed |= WIPHY_PARAM_RETRY_SHORT;
2122 }
2123
2124 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2125 retry_long = nla_get_u8(
2126 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002127 if (retry_long == 0)
2128 return -EINVAL;
2129
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002130 changed |= WIPHY_PARAM_RETRY_LONG;
2131 }
2132
2133 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2134 frag_threshold = nla_get_u32(
2135 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002136 if (frag_threshold < 256)
2137 return -EINVAL;
2138
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002139 if (frag_threshold != (u32) -1) {
2140 /*
2141 * Fragments (apart from the last one) are required to
2142 * have even length. Make the fragmentation code
2143 * simpler by stripping LSB should someone try to use
2144 * odd threshold value.
2145 */
2146 frag_threshold &= ~0x1;
2147 }
2148 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2149 }
2150
2151 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2152 rts_threshold = nla_get_u32(
2153 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2154 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2155 }
2156
Lukáš Turek81077e82009-12-21 22:50:47 +01002157 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2158 coverage_class = nla_get_u8(
2159 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2160 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2161 }
2162
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002163 if (changed) {
2164 u8 old_retry_short, old_retry_long;
2165 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002166 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002167
Ying Xue7f2b8562014-01-15 10:23:45 +08002168 if (!rdev->ops->set_wiphy_params)
2169 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002170
2171 old_retry_short = rdev->wiphy.retry_short;
2172 old_retry_long = rdev->wiphy.retry_long;
2173 old_frag_threshold = rdev->wiphy.frag_threshold;
2174 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002175 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002176
2177 if (changed & WIPHY_PARAM_RETRY_SHORT)
2178 rdev->wiphy.retry_short = retry_short;
2179 if (changed & WIPHY_PARAM_RETRY_LONG)
2180 rdev->wiphy.retry_long = retry_long;
2181 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2182 rdev->wiphy.frag_threshold = frag_threshold;
2183 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2184 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002185 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2186 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002187
Hila Gonene35e4d22012-06-27 17:19:42 +03002188 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002189 if (result) {
2190 rdev->wiphy.retry_short = old_retry_short;
2191 rdev->wiphy.retry_long = old_retry_long;
2192 rdev->wiphy.frag_threshold = old_frag_threshold;
2193 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002194 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002195 }
2196 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002197 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002198}
2199
Johannes Berg71bbc992012-06-15 15:30:18 +02002200static inline u64 wdev_id(struct wireless_dev *wdev)
2201{
2202 return (u64)wdev->identifier |
2203 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2204}
Johannes Berg55682962007-09-20 13:09:35 -04002205
Johannes Berg683b6d32012-11-08 21:25:48 +01002206static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002207 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002208{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002209 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002210
Johannes Berg683b6d32012-11-08 21:25:48 +01002211 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2212 chandef->chan->center_freq))
2213 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002214 switch (chandef->width) {
2215 case NL80211_CHAN_WIDTH_20_NOHT:
2216 case NL80211_CHAN_WIDTH_20:
2217 case NL80211_CHAN_WIDTH_40:
2218 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2219 cfg80211_get_chandef_type(chandef)))
2220 return -ENOBUFS;
2221 break;
2222 default:
2223 break;
2224 }
2225 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2226 return -ENOBUFS;
2227 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2228 return -ENOBUFS;
2229 if (chandef->center_freq2 &&
2230 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002231 return -ENOBUFS;
2232 return 0;
2233}
2234
Eric W. Biederman15e47302012-09-07 20:12:54 +00002235static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002236 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002237 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002238{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002239 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002240 void *hdr;
2241
Eric W. Biederman15e47302012-09-07 20:12:54 +00002242 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002243 if (!hdr)
2244 return -1;
2245
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002246 if (dev &&
2247 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002248 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002249 goto nla_put_failure;
2250
2251 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2252 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002253 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002254 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002255 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2256 rdev->devlist_generation ^
2257 (cfg80211_rdev_list_generation << 2)))
2258 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002259
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002260 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002261 int ret;
2262 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002263
Johannes Berg683b6d32012-11-08 21:25:48 +01002264 ret = rdev_get_channel(rdev, wdev, &chandef);
2265 if (ret == 0) {
2266 if (nl80211_send_chandef(msg, &chandef))
2267 goto nla_put_failure;
2268 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002269 }
2270
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002271 if (wdev->ssid_len) {
2272 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2273 goto nla_put_failure;
2274 }
2275
Johannes Berg55682962007-09-20 13:09:35 -04002276 return genlmsg_end(msg, hdr);
2277
2278 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002279 genlmsg_cancel(msg, hdr);
2280 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002281}
2282
2283static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2284{
2285 int wp_idx = 0;
2286 int if_idx = 0;
2287 int wp_start = cb->args[0];
2288 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002289 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002290 struct wireless_dev *wdev;
2291
Johannes Berg5fe231e2013-05-08 21:45:15 +02002292 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002293 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2294 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002295 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002296 if (wp_idx < wp_start) {
2297 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002298 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002299 }
Johannes Berg55682962007-09-20 13:09:35 -04002300 if_idx = 0;
2301
Johannes Berg89a54e42012-06-15 14:33:17 +02002302 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002303 if (if_idx < if_start) {
2304 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002305 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002306 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002307 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002308 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002309 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002310 goto out;
2311 }
2312 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002313 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002314
2315 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002316 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002317 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002318 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002319
2320 cb->args[0] = wp_idx;
2321 cb->args[1] = if_idx;
2322
2323 return skb->len;
2324}
2325
2326static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2327{
2328 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002329 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002330 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002331
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002332 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002333 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002334 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002335
Eric W. Biederman15e47302012-09-07 20:12:54 +00002336 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002337 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002338 nlmsg_free(msg);
2339 return -ENOBUFS;
2340 }
Johannes Berg55682962007-09-20 13:09:35 -04002341
Johannes Berg134e6372009-07-10 09:51:34 +00002342 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002343}
2344
Michael Wu66f7ac52008-01-31 19:48:22 +01002345static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2346 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2347 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2348 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2349 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2350 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002351 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002352};
2353
2354static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2355{
2356 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2357 int flag;
2358
2359 *mntrflags = 0;
2360
2361 if (!nla)
2362 return -EINVAL;
2363
2364 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2365 nla, mntr_flags_policy))
2366 return -EINVAL;
2367
2368 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2369 if (flags[flag])
2370 *mntrflags |= (1<<flag);
2371
2372 return 0;
2373}
2374
Johannes Berg9bc383d2009-11-19 11:55:19 +01002375static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002376 struct net_device *netdev, u8 use_4addr,
2377 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002378{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002379 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002380 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002381 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002382 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002383 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002384
2385 switch (iftype) {
2386 case NL80211_IFTYPE_AP_VLAN:
2387 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2388 return 0;
2389 break;
2390 case NL80211_IFTYPE_STATION:
2391 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2392 return 0;
2393 break;
2394 default:
2395 break;
2396 }
2397
2398 return -EOPNOTSUPP;
2399}
2400
Johannes Berg55682962007-09-20 13:09:35 -04002401static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2402{
Johannes Berg4c476992010-10-04 21:36:35 +02002403 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002405 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002406 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002407 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002408 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002409 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002410
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002411 memset(&params, 0, sizeof(params));
2412
Johannes Berg04a773a2009-04-19 21:24:32 +02002413 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002414
Johannes Berg723b0382008-09-16 20:22:09 +02002415 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002416 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002417 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002418 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002419 if (ntype > NL80211_IFTYPE_MAX)
2420 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002421 }
2422
Johannes Berg92ffe052008-09-16 20:39:36 +02002423 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002424 struct wireless_dev *wdev = dev->ieee80211_ptr;
2425
Johannes Berg4c476992010-10-04 21:36:35 +02002426 if (ntype != NL80211_IFTYPE_MESH_POINT)
2427 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002428 if (netif_running(dev))
2429 return -EBUSY;
2430
2431 wdev_lock(wdev);
2432 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2433 IEEE80211_MAX_MESH_ID_LEN);
2434 wdev->mesh_id_up_len =
2435 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2436 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2437 wdev->mesh_id_up_len);
2438 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002439 }
2440
Felix Fietkau8b787642009-11-10 18:53:10 +01002441 if (info->attrs[NL80211_ATTR_4ADDR]) {
2442 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2443 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002444 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002445 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002446 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002447 } else {
2448 params.use_4addr = -1;
2449 }
2450
Johannes Berg92ffe052008-09-16 20:39:36 +02002451 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002452 if (ntype != NL80211_IFTYPE_MONITOR)
2453 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002454 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2455 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002456 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002457 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002458
2459 flags = &_flags;
2460 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002461 }
Johannes Berg3b858752009-03-12 09:55:09 +01002462
Luciano Coelho18003292013-08-29 13:26:57 +03002463 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002464 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2465 return -EOPNOTSUPP;
2466
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002467 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002468 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002469 else
2470 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002471
Johannes Berg9bc383d2009-11-19 11:55:19 +01002472 if (!err && params.use_4addr != -1)
2473 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2474
Johannes Berg55682962007-09-20 13:09:35 -04002475 return err;
2476}
2477
2478static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2479{
Johannes Berg4c476992010-10-04 21:36:35 +02002480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002481 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002482 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002483 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002484 int err;
2485 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002486 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002487
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002488 memset(&params, 0, sizeof(params));
2489
Johannes Berg55682962007-09-20 13:09:35 -04002490 if (!info->attrs[NL80211_ATTR_IFNAME])
2491 return -EINVAL;
2492
2493 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2494 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2495 if (type > NL80211_IFTYPE_MAX)
2496 return -EINVAL;
2497 }
2498
Johannes Berg79c97e92009-07-07 03:56:12 +02002499 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002500 !(rdev->wiphy.interface_modes & (1 << type)))
2501 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002502
Arend van Spriel1c18f142013-01-08 10:17:27 +01002503 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2504 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2505 ETH_ALEN);
2506 if (!is_valid_ether_addr(params.macaddr))
2507 return -EADDRNOTAVAIL;
2508 }
2509
Johannes Berg9bc383d2009-11-19 11:55:19 +01002510 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002511 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002512 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002513 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002514 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002515 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002516
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2518 if (!msg)
2519 return -ENOMEM;
2520
Michael Wu66f7ac52008-01-31 19:48:22 +01002521 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2522 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2523 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002524
Luciano Coelho18003292013-08-29 13:26:57 +03002525 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002526 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2527 return -EOPNOTSUPP;
2528
Hila Gonene35e4d22012-06-27 17:19:42 +03002529 wdev = rdev_add_virtual_intf(rdev,
2530 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2531 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002532 if (IS_ERR(wdev)) {
2533 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002534 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002535 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002536
Johannes Berg98104fde2012-06-16 00:19:54 +02002537 switch (type) {
2538 case NL80211_IFTYPE_MESH_POINT:
2539 if (!info->attrs[NL80211_ATTR_MESH_ID])
2540 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002541 wdev_lock(wdev);
2542 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2543 IEEE80211_MAX_MESH_ID_LEN);
2544 wdev->mesh_id_up_len =
2545 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2546 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2547 wdev->mesh_id_up_len);
2548 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002549 break;
2550 case NL80211_IFTYPE_P2P_DEVICE:
2551 /*
2552 * P2P Device doesn't have a netdev, so doesn't go
2553 * through the netdev notifier and must be added here
2554 */
2555 mutex_init(&wdev->mtx);
2556 INIT_LIST_HEAD(&wdev->event_list);
2557 spin_lock_init(&wdev->event_lock);
2558 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2559 spin_lock_init(&wdev->mgmt_registrations_lock);
2560
Johannes Berg98104fde2012-06-16 00:19:54 +02002561 wdev->identifier = ++rdev->wdev_id;
2562 list_add_rcu(&wdev->list, &rdev->wdev_list);
2563 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002564 break;
2565 default:
2566 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002567 }
2568
Eric W. Biederman15e47302012-09-07 20:12:54 +00002569 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002570 rdev, wdev) < 0) {
2571 nlmsg_free(msg);
2572 return -ENOBUFS;
2573 }
2574
2575 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002576}
2577
2578static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2579{
Johannes Berg4c476992010-10-04 21:36:35 +02002580 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002581 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002582
Johannes Berg4c476992010-10-04 21:36:35 +02002583 if (!rdev->ops->del_virtual_intf)
2584 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002585
Johannes Berg84efbb82012-06-16 00:00:26 +02002586 /*
2587 * If we remove a wireless device without a netdev then clear
2588 * user_ptr[1] so that nl80211_post_doit won't dereference it
2589 * to check if it needs to do dev_put(). Otherwise it crashes
2590 * since the wdev has been freed, unlike with a netdev where
2591 * we need the dev_put() for the netdev to really be freed.
2592 */
2593 if (!wdev->netdev)
2594 info->user_ptr[1] = NULL;
2595
Hila Gonene35e4d22012-06-27 17:19:42 +03002596 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002597}
2598
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002599static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2600{
2601 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2602 struct net_device *dev = info->user_ptr[1];
2603 u16 noack_map;
2604
2605 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2606 return -EINVAL;
2607
2608 if (!rdev->ops->set_noack_map)
2609 return -EOPNOTSUPP;
2610
2611 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2612
Hila Gonene35e4d22012-06-27 17:19:42 +03002613 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002614}
2615
Johannes Berg41ade002007-12-19 02:03:29 +01002616struct get_key_cookie {
2617 struct sk_buff *msg;
2618 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002619 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002620};
2621
2622static void get_key_callback(void *c, struct key_params *params)
2623{
Johannes Bergb9454e82009-07-08 13:29:08 +02002624 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002625 struct get_key_cookie *cookie = c;
2626
David S. Miller9360ffd2012-03-29 04:41:26 -04002627 if ((params->key &&
2628 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2629 params->key_len, params->key)) ||
2630 (params->seq &&
2631 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2632 params->seq_len, params->seq)) ||
2633 (params->cipher &&
2634 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2635 params->cipher)))
2636 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002637
Johannes Bergb9454e82009-07-08 13:29:08 +02002638 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2639 if (!key)
2640 goto nla_put_failure;
2641
David S. Miller9360ffd2012-03-29 04:41:26 -04002642 if ((params->key &&
2643 nla_put(cookie->msg, NL80211_KEY_DATA,
2644 params->key_len, params->key)) ||
2645 (params->seq &&
2646 nla_put(cookie->msg, NL80211_KEY_SEQ,
2647 params->seq_len, params->seq)) ||
2648 (params->cipher &&
2649 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2650 params->cipher)))
2651 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002652
David S. Miller9360ffd2012-03-29 04:41:26 -04002653 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2654 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002655
2656 nla_nest_end(cookie->msg, key);
2657
Johannes Berg41ade002007-12-19 02:03:29 +01002658 return;
2659 nla_put_failure:
2660 cookie->error = 1;
2661}
2662
2663static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2664{
Johannes Berg4c476992010-10-04 21:36:35 +02002665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002666 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002667 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002668 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002669 const u8 *mac_addr = NULL;
2670 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002671 struct get_key_cookie cookie = {
2672 .error = 0,
2673 };
2674 void *hdr;
2675 struct sk_buff *msg;
2676
2677 if (info->attrs[NL80211_ATTR_KEY_IDX])
2678 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2679
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002680 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002681 return -EINVAL;
2682
2683 if (info->attrs[NL80211_ATTR_MAC])
2684 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2685
Johannes Berge31b8212010-10-05 19:39:30 +02002686 pairwise = !!mac_addr;
2687 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2688 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2689 if (kt >= NUM_NL80211_KEYTYPES)
2690 return -EINVAL;
2691 if (kt != NL80211_KEYTYPE_GROUP &&
2692 kt != NL80211_KEYTYPE_PAIRWISE)
2693 return -EINVAL;
2694 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2695 }
2696
Johannes Berg4c476992010-10-04 21:36:35 +02002697 if (!rdev->ops->get_key)
2698 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002699
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002701 if (!msg)
2702 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002703
Eric W. Biederman15e47302012-09-07 20:12:54 +00002704 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002705 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002706 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002707 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002708
2709 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002710 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002711
David S. Miller9360ffd2012-03-29 04:41:26 -04002712 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2713 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2714 goto nla_put_failure;
2715 if (mac_addr &&
2716 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2717 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002718
Johannes Berge31b8212010-10-05 19:39:30 +02002719 if (pairwise && mac_addr &&
2720 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2721 return -ENOENT;
2722
Hila Gonene35e4d22012-06-27 17:19:42 +03002723 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2724 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002725
2726 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002727 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002728
2729 if (cookie.error)
2730 goto nla_put_failure;
2731
2732 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002733 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002734
2735 nla_put_failure:
2736 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002737 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002738 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002739 return err;
2740}
2741
2742static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2743{
Johannes Berg4c476992010-10-04 21:36:35 +02002744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002745 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002746 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002747 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002748
Johannes Bergb9454e82009-07-08 13:29:08 +02002749 err = nl80211_parse_key(info, &key);
2750 if (err)
2751 return err;
2752
2753 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002754 return -EINVAL;
2755
Johannes Bergb9454e82009-07-08 13:29:08 +02002756 /* only support setting default key */
2757 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002758 return -EINVAL;
2759
Johannes Bergfffd0932009-07-08 14:22:54 +02002760 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002761
2762 if (key.def) {
2763 if (!rdev->ops->set_default_key) {
2764 err = -EOPNOTSUPP;
2765 goto out;
2766 }
2767
2768 err = nl80211_key_allowed(dev->ieee80211_ptr);
2769 if (err)
2770 goto out;
2771
Hila Gonene35e4d22012-06-27 17:19:42 +03002772 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002773 key.def_uni, key.def_multi);
2774
2775 if (err)
2776 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002777
Johannes Berg3d23e342009-09-29 23:27:28 +02002778#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002779 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002780#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002781 } else {
2782 if (key.def_uni || !key.def_multi) {
2783 err = -EINVAL;
2784 goto out;
2785 }
2786
2787 if (!rdev->ops->set_default_mgmt_key) {
2788 err = -EOPNOTSUPP;
2789 goto out;
2790 }
2791
2792 err = nl80211_key_allowed(dev->ieee80211_ptr);
2793 if (err)
2794 goto out;
2795
Hila Gonene35e4d22012-06-27 17:19:42 +03002796 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002797 if (err)
2798 goto out;
2799
2800#ifdef CONFIG_CFG80211_WEXT
2801 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2802#endif
2803 }
2804
2805 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002806 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002807
Johannes Berg41ade002007-12-19 02:03:29 +01002808 return err;
2809}
2810
2811static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2812{
Johannes Berg4c476992010-10-04 21:36:35 +02002813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002814 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002815 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002816 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002817 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002818
Johannes Bergb9454e82009-07-08 13:29:08 +02002819 err = nl80211_parse_key(info, &key);
2820 if (err)
2821 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002822
Johannes Bergb9454e82009-07-08 13:29:08 +02002823 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002824 return -EINVAL;
2825
Johannes Berg41ade002007-12-19 02:03:29 +01002826 if (info->attrs[NL80211_ATTR_MAC])
2827 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2828
Johannes Berge31b8212010-10-05 19:39:30 +02002829 if (key.type == -1) {
2830 if (mac_addr)
2831 key.type = NL80211_KEYTYPE_PAIRWISE;
2832 else
2833 key.type = NL80211_KEYTYPE_GROUP;
2834 }
2835
2836 /* for now */
2837 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2838 key.type != NL80211_KEYTYPE_GROUP)
2839 return -EINVAL;
2840
Johannes Berg4c476992010-10-04 21:36:35 +02002841 if (!rdev->ops->add_key)
2842 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002843
Johannes Berge31b8212010-10-05 19:39:30 +02002844 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2845 key.type == NL80211_KEYTYPE_PAIRWISE,
2846 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002847 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002848
2849 wdev_lock(dev->ieee80211_ptr);
2850 err = nl80211_key_allowed(dev->ieee80211_ptr);
2851 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002852 err = rdev_add_key(rdev, dev, key.idx,
2853 key.type == NL80211_KEYTYPE_PAIRWISE,
2854 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002855 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002856
Johannes Berg41ade002007-12-19 02:03:29 +01002857 return err;
2858}
2859
2860static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2861{
Johannes Berg4c476992010-10-04 21:36:35 +02002862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002863 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002864 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002865 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002866 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002867
Johannes Bergb9454e82009-07-08 13:29:08 +02002868 err = nl80211_parse_key(info, &key);
2869 if (err)
2870 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002871
2872 if (info->attrs[NL80211_ATTR_MAC])
2873 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2874
Johannes Berge31b8212010-10-05 19:39:30 +02002875 if (key.type == -1) {
2876 if (mac_addr)
2877 key.type = NL80211_KEYTYPE_PAIRWISE;
2878 else
2879 key.type = NL80211_KEYTYPE_GROUP;
2880 }
2881
2882 /* for now */
2883 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2884 key.type != NL80211_KEYTYPE_GROUP)
2885 return -EINVAL;
2886
Johannes Berg4c476992010-10-04 21:36:35 +02002887 if (!rdev->ops->del_key)
2888 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002889
Johannes Bergfffd0932009-07-08 14:22:54 +02002890 wdev_lock(dev->ieee80211_ptr);
2891 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002892
2893 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2894 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2895 err = -ENOENT;
2896
Johannes Bergfffd0932009-07-08 14:22:54 +02002897 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002898 err = rdev_del_key(rdev, dev, key.idx,
2899 key.type == NL80211_KEYTYPE_PAIRWISE,
2900 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002901
Johannes Berg3d23e342009-09-29 23:27:28 +02002902#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002903 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002904 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002905 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002906 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002907 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2908 }
2909#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002910 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002911
Johannes Berg41ade002007-12-19 02:03:29 +01002912 return err;
2913}
2914
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302915/* This function returns an error or the number of nested attributes */
2916static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2917{
2918 struct nlattr *attr;
2919 int n_entries = 0, tmp;
2920
2921 nla_for_each_nested(attr, nl_attr, tmp) {
2922 if (nla_len(attr) != ETH_ALEN)
2923 return -EINVAL;
2924
2925 n_entries++;
2926 }
2927
2928 return n_entries;
2929}
2930
2931/*
2932 * This function parses ACL information and allocates memory for ACL data.
2933 * On successful return, the calling function is responsible to free the
2934 * ACL buffer returned by this function.
2935 */
2936static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2937 struct genl_info *info)
2938{
2939 enum nl80211_acl_policy acl_policy;
2940 struct nlattr *attr;
2941 struct cfg80211_acl_data *acl;
2942 int i = 0, n_entries, tmp;
2943
2944 if (!wiphy->max_acl_mac_addrs)
2945 return ERR_PTR(-EOPNOTSUPP);
2946
2947 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2948 return ERR_PTR(-EINVAL);
2949
2950 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2951 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2952 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2953 return ERR_PTR(-EINVAL);
2954
2955 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2956 return ERR_PTR(-EINVAL);
2957
2958 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2959 if (n_entries < 0)
2960 return ERR_PTR(n_entries);
2961
2962 if (n_entries > wiphy->max_acl_mac_addrs)
2963 return ERR_PTR(-ENOTSUPP);
2964
2965 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2966 GFP_KERNEL);
2967 if (!acl)
2968 return ERR_PTR(-ENOMEM);
2969
2970 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2971 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2972 i++;
2973 }
2974
2975 acl->n_acl_entries = n_entries;
2976 acl->acl_policy = acl_policy;
2977
2978 return acl;
2979}
2980
2981static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2982{
2983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2984 struct net_device *dev = info->user_ptr[1];
2985 struct cfg80211_acl_data *acl;
2986 int err;
2987
2988 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2989 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2990 return -EOPNOTSUPP;
2991
2992 if (!dev->ieee80211_ptr->beacon_interval)
2993 return -EINVAL;
2994
2995 acl = parse_acl_data(&rdev->wiphy, info);
2996 if (IS_ERR(acl))
2997 return PTR_ERR(acl);
2998
2999 err = rdev_set_mac_acl(rdev, dev, acl);
3000
3001 kfree(acl);
3002
3003 return err;
3004}
3005
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003006static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003007 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003008{
Johannes Berg88600202012-02-13 15:17:18 +01003009 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003010
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003011 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3012 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3013 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3014 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003015 return -EINVAL;
3016
Johannes Berg88600202012-02-13 15:17:18 +01003017 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003018
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003019 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3020 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3021 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003022 if (!bcn->head_len)
3023 return -EINVAL;
3024 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003025 }
3026
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003027 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3028 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3029 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003030 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003031 }
3032
Johannes Berg4c476992010-10-04 21:36:35 +02003033 if (!haveinfo)
3034 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003035
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003036 if (attrs[NL80211_ATTR_IE]) {
3037 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3038 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003039 }
3040
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003041 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003042 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003043 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003044 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003045 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003046 }
3047
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003048 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003049 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003050 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003051 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003052 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003053 }
3054
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003055 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3056 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3057 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003058 }
3059
Johannes Berg88600202012-02-13 15:17:18 +01003060 return 0;
3061}
3062
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003063static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3064 struct cfg80211_ap_settings *params)
3065{
3066 struct wireless_dev *wdev;
3067 bool ret = false;
3068
Johannes Berg89a54e42012-06-15 14:33:17 +02003069 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003070 if (wdev->iftype != NL80211_IFTYPE_AP &&
3071 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3072 continue;
3073
Johannes Berg683b6d32012-11-08 21:25:48 +01003074 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003075 continue;
3076
Johannes Berg683b6d32012-11-08 21:25:48 +01003077 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003078 ret = true;
3079 break;
3080 }
3081
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003082 return ret;
3083}
3084
Jouni Malinene39e5b52012-09-30 19:29:39 +03003085static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3086 enum nl80211_auth_type auth_type,
3087 enum nl80211_commands cmd)
3088{
3089 if (auth_type > NL80211_AUTHTYPE_MAX)
3090 return false;
3091
3092 switch (cmd) {
3093 case NL80211_CMD_AUTHENTICATE:
3094 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3095 auth_type == NL80211_AUTHTYPE_SAE)
3096 return false;
3097 return true;
3098 case NL80211_CMD_CONNECT:
3099 case NL80211_CMD_START_AP:
3100 /* SAE not supported yet */
3101 if (auth_type == NL80211_AUTHTYPE_SAE)
3102 return false;
3103 return true;
3104 default:
3105 return false;
3106 }
3107}
3108
Johannes Berg88600202012-02-13 15:17:18 +01003109static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3110{
3111 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3112 struct net_device *dev = info->user_ptr[1];
3113 struct wireless_dev *wdev = dev->ieee80211_ptr;
3114 struct cfg80211_ap_settings params;
3115 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003116 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003117
3118 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3119 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3120 return -EOPNOTSUPP;
3121
3122 if (!rdev->ops->start_ap)
3123 return -EOPNOTSUPP;
3124
3125 if (wdev->beacon_interval)
3126 return -EALREADY;
3127
3128 memset(&params, 0, sizeof(params));
3129
3130 /* these are required for START_AP */
3131 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3132 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3133 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3134 return -EINVAL;
3135
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003136 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003137 if (err)
3138 return err;
3139
3140 params.beacon_interval =
3141 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3142 params.dtim_period =
3143 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3144
3145 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3146 if (err)
3147 return err;
3148
3149 /*
3150 * In theory, some of these attributes should be required here
3151 * but since they were not used when the command was originally
3152 * added, keep them optional for old user space programs to let
3153 * them continue to work with drivers that do not need the
3154 * additional information -- drivers must check!
3155 */
3156 if (info->attrs[NL80211_ATTR_SSID]) {
3157 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3158 params.ssid_len =
3159 nla_len(info->attrs[NL80211_ATTR_SSID]);
3160 if (params.ssid_len == 0 ||
3161 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3162 return -EINVAL;
3163 }
3164
3165 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3166 params.hidden_ssid = nla_get_u32(
3167 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3168 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3169 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3170 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3171 return -EINVAL;
3172 }
3173
3174 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3175
3176 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3177 params.auth_type = nla_get_u32(
3178 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003179 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3180 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003181 return -EINVAL;
3182 } else
3183 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3184
3185 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3186 NL80211_MAX_NR_CIPHER_SUITES);
3187 if (err)
3188 return err;
3189
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303190 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3191 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3192 return -EOPNOTSUPP;
3193 params.inactivity_timeout = nla_get_u16(
3194 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3195 }
3196
Johannes Berg53cabad2012-11-14 15:17:28 +01003197 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3198 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3199 return -EINVAL;
3200 params.p2p_ctwindow =
3201 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3202 if (params.p2p_ctwindow > 127)
3203 return -EINVAL;
3204 if (params.p2p_ctwindow != 0 &&
3205 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3206 return -EINVAL;
3207 }
3208
3209 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3210 u8 tmp;
3211
3212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3213 return -EINVAL;
3214 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3215 if (tmp > 1)
3216 return -EINVAL;
3217 params.p2p_opp_ps = tmp;
3218 if (params.p2p_opp_ps != 0 &&
3219 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3220 return -EINVAL;
3221 }
3222
Johannes Bergaa430da2012-05-16 23:50:18 +02003223 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003224 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3225 if (err)
3226 return err;
3227 } else if (wdev->preset_chandef.chan) {
3228 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003229 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003230 return -EINVAL;
3231
Johannes Berg683b6d32012-11-08 21:25:48 +01003232 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003233 return -EINVAL;
3234
Simon Wunderlich04f39042013-02-08 18:16:19 +01003235 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3236 if (err < 0)
3237 return err;
3238 if (err) {
3239 radar_detect_width = BIT(params.chandef.width);
3240 params.radar_required = true;
3241 }
3242
Simon Wunderlich04f39042013-02-08 18:16:19 +01003243 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3244 params.chandef.chan,
3245 CHAN_MODE_SHARED,
3246 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003247 if (err)
3248 return err;
3249
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303250 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3251 params.acl = parse_acl_data(&rdev->wiphy, info);
3252 if (IS_ERR(params.acl))
3253 return PTR_ERR(params.acl);
3254 }
3255
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003256 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003257 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003258 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003259 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003260 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003261 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003262 wdev->ssid_len = params.ssid_len;
3263 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003264 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003265 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303266
3267 kfree(params.acl);
3268
Johannes Berg56d18932011-05-09 18:41:15 +02003269 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003270}
3271
Johannes Berg88600202012-02-13 15:17:18 +01003272static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3273{
3274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3275 struct net_device *dev = info->user_ptr[1];
3276 struct wireless_dev *wdev = dev->ieee80211_ptr;
3277 struct cfg80211_beacon_data params;
3278 int err;
3279
3280 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3282 return -EOPNOTSUPP;
3283
3284 if (!rdev->ops->change_beacon)
3285 return -EOPNOTSUPP;
3286
3287 if (!wdev->beacon_interval)
3288 return -EINVAL;
3289
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003290 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003291 if (err)
3292 return err;
3293
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003294 wdev_lock(wdev);
3295 err = rdev_change_beacon(rdev, dev, &params);
3296 wdev_unlock(wdev);
3297
3298 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003299}
3300
3301static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003302{
Johannes Berg4c476992010-10-04 21:36:35 +02003303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3304 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003305
Michal Kazior60771782012-06-29 12:46:56 +02003306 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003307}
3308
Johannes Berg5727ef12007-12-19 02:03:34 +01003309static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3310 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3311 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3312 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003313 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003314 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003315 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003316};
3317
Johannes Bergeccb8e82009-05-11 21:57:56 +03003318static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003319 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003320 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003321{
3322 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003323 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003324 int flag;
3325
Johannes Bergeccb8e82009-05-11 21:57:56 +03003326 /*
3327 * Try parsing the new attribute first so userspace
3328 * can specify both for older kernels.
3329 */
3330 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3331 if (nla) {
3332 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003333
Johannes Bergeccb8e82009-05-11 21:57:56 +03003334 sta_flags = nla_data(nla);
3335 params->sta_flags_mask = sta_flags->mask;
3336 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003337 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003338 if ((params->sta_flags_mask |
3339 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3340 return -EINVAL;
3341 return 0;
3342 }
3343
3344 /* if present, parse the old attribute */
3345
3346 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003347 if (!nla)
3348 return 0;
3349
3350 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3351 nla, sta_flags_policy))
3352 return -EINVAL;
3353
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003354 /*
3355 * Only allow certain flags for interface types so that
3356 * other attributes are silently ignored. Remember that
3357 * this is backward compatibility code with old userspace
3358 * and shouldn't be hit in other cases anyway.
3359 */
3360 switch (iftype) {
3361 case NL80211_IFTYPE_AP:
3362 case NL80211_IFTYPE_AP_VLAN:
3363 case NL80211_IFTYPE_P2P_GO:
3364 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3365 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3366 BIT(NL80211_STA_FLAG_WME) |
3367 BIT(NL80211_STA_FLAG_MFP);
3368 break;
3369 case NL80211_IFTYPE_P2P_CLIENT:
3370 case NL80211_IFTYPE_STATION:
3371 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3372 BIT(NL80211_STA_FLAG_TDLS_PEER);
3373 break;
3374 case NL80211_IFTYPE_MESH_POINT:
3375 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3376 BIT(NL80211_STA_FLAG_MFP) |
3377 BIT(NL80211_STA_FLAG_AUTHORIZED);
3378 default:
3379 return -EINVAL;
3380 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003381
Johannes Berg3383b5a2012-05-10 20:14:43 +02003382 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3383 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003384 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003385
Johannes Berg3383b5a2012-05-10 20:14:43 +02003386 /* no longer support new API additions in old API */
3387 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3388 return -EINVAL;
3389 }
3390 }
3391
Johannes Berg5727ef12007-12-19 02:03:34 +01003392 return 0;
3393}
3394
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003395static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3396 int attr)
3397{
3398 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003399 u32 bitrate;
3400 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003401
3402 rate = nla_nest_start(msg, attr);
3403 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003404 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003405
3406 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3407 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003408 /* report 16-bit bitrate only if we can */
3409 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003410 if (bitrate > 0 &&
3411 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3412 return false;
3413 if (bitrate_compat > 0 &&
3414 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3415 return false;
3416
3417 if (info->flags & RATE_INFO_FLAGS_MCS) {
3418 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3419 return false;
3420 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3421 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3422 return false;
3423 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3424 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3425 return false;
3426 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3427 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3428 return false;
3429 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3430 return false;
3431 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3432 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3433 return false;
3434 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3435 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3436 return false;
3437 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3438 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3439 return false;
3440 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3441 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3442 return false;
3443 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3444 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3445 return false;
3446 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003447
3448 nla_nest_end(msg, rate);
3449 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003450}
3451
Felix Fietkau119363c2013-04-22 16:29:30 +02003452static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3453 int id)
3454{
3455 void *attr;
3456 int i = 0;
3457
3458 if (!mask)
3459 return true;
3460
3461 attr = nla_nest_start(msg, id);
3462 if (!attr)
3463 return false;
3464
3465 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3466 if (!(mask & BIT(i)))
3467 continue;
3468
3469 if (nla_put_u8(msg, i, signal[i]))
3470 return false;
3471 }
3472
3473 nla_nest_end(msg, attr);
3474
3475 return true;
3476}
3477
Eric W. Biederman15e47302012-09-07 20:12:54 +00003478static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003479 int flags,
3480 struct cfg80211_registered_device *rdev,
3481 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003482 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003483{
3484 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003485 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003486
Eric W. Biederman15e47302012-09-07 20:12:54 +00003487 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003488 if (!hdr)
3489 return -1;
3490
David S. Miller9360ffd2012-03-29 04:41:26 -04003491 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3492 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3493 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3494 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003495
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003496 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3497 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003498 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003499 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3500 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3501 sinfo->connected_time))
3502 goto nla_put_failure;
3503 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3504 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3505 sinfo->inactive_time))
3506 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003507 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3508 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003509 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003510 (u32)sinfo->rx_bytes))
3511 goto nla_put_failure;
3512 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003513 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003514 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3515 (u32)sinfo->tx_bytes))
3516 goto nla_put_failure;
3517 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3518 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003519 sinfo->rx_bytes))
3520 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003521 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3522 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003523 sinfo->tx_bytes))
3524 goto nla_put_failure;
3525 if ((sinfo->filled & STATION_INFO_LLID) &&
3526 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3527 goto nla_put_failure;
3528 if ((sinfo->filled & STATION_INFO_PLID) &&
3529 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3530 goto nla_put_failure;
3531 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3532 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3533 sinfo->plink_state))
3534 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003535 switch (rdev->wiphy.signal_type) {
3536 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003537 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3538 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3539 sinfo->signal))
3540 goto nla_put_failure;
3541 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3542 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3543 sinfo->signal_avg))
3544 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003545 break;
3546 default:
3547 break;
3548 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003549 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3550 if (!nl80211_put_signal(msg, sinfo->chains,
3551 sinfo->chain_signal,
3552 NL80211_STA_INFO_CHAIN_SIGNAL))
3553 goto nla_put_failure;
3554 }
3555 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3556 if (!nl80211_put_signal(msg, sinfo->chains,
3557 sinfo->chain_signal_avg,
3558 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3559 goto nla_put_failure;
3560 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003561 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003562 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3563 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003564 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003565 }
3566 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3567 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3568 NL80211_STA_INFO_RX_BITRATE))
3569 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003570 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003571 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3572 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3573 sinfo->rx_packets))
3574 goto nla_put_failure;
3575 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3576 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3577 sinfo->tx_packets))
3578 goto nla_put_failure;
3579 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3580 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3581 sinfo->tx_retries))
3582 goto nla_put_failure;
3583 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3584 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3585 sinfo->tx_failed))
3586 goto nla_put_failure;
3587 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3588 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3589 sinfo->beacon_loss_count))
3590 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003591 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3592 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3593 sinfo->local_pm))
3594 goto nla_put_failure;
3595 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3596 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3597 sinfo->peer_pm))
3598 goto nla_put_failure;
3599 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3600 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3601 sinfo->nonpeer_pm))
3602 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003603 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3604 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3605 if (!bss_param)
3606 goto nla_put_failure;
3607
David S. Miller9360ffd2012-03-29 04:41:26 -04003608 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3609 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3610 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3611 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3612 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3613 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3614 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3615 sinfo->bss_param.dtim_period) ||
3616 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3617 sinfo->bss_param.beacon_interval))
3618 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003619
3620 nla_nest_end(msg, bss_param);
3621 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003622 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3623 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3624 sizeof(struct nl80211_sta_flag_update),
3625 &sinfo->sta_flags))
3626 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003627 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3628 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3629 sinfo->t_offset))
3630 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003631 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003632
David S. Miller9360ffd2012-03-29 04:41:26 -04003633 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3634 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3635 sinfo->assoc_req_ies))
3636 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003637
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003638 return genlmsg_end(msg, hdr);
3639
3640 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003641 genlmsg_cancel(msg, hdr);
3642 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003643}
3644
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003645static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003646 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003647{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003648 struct station_info sinfo;
3649 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003650 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003651 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003652 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003653 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003654
Johannes Berg97990a02013-04-19 01:02:55 +02003655 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003656 if (err)
3657 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003658
Johannes Berg97990a02013-04-19 01:02:55 +02003659 if (!wdev->netdev) {
3660 err = -EINVAL;
3661 goto out_err;
3662 }
3663
Johannes Bergbba95fe2008-07-29 13:22:51 +02003664 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003665 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003666 goto out_err;
3667 }
3668
Johannes Bergbba95fe2008-07-29 13:22:51 +02003669 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003670 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003671 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003672 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003673 if (err == -ENOENT)
3674 break;
3675 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003676 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003677
3678 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003679 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003680 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003681 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003682 &sinfo) < 0)
3683 goto out;
3684
3685 sta_idx++;
3686 }
3687
3688
3689 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003690 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003691 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003692 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003693 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003694
3695 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003696}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003697
Johannes Berg5727ef12007-12-19 02:03:34 +01003698static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3699{
Johannes Berg4c476992010-10-04 21:36:35 +02003700 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3701 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003702 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003703 struct sk_buff *msg;
3704 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003705 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003706
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003707 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003708
3709 if (!info->attrs[NL80211_ATTR_MAC])
3710 return -EINVAL;
3711
3712 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3713
Johannes Berg4c476992010-10-04 21:36:35 +02003714 if (!rdev->ops->get_station)
3715 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003716
Hila Gonene35e4d22012-06-27 17:19:42 +03003717 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003718 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003719 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003720
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003722 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003723 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003724
Eric W. Biederman15e47302012-09-07 20:12:54 +00003725 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003726 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003727 nlmsg_free(msg);
3728 return -ENOBUFS;
3729 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003730
Johannes Berg4c476992010-10-04 21:36:35 +02003731 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003732}
3733
Johannes Berg77ee7c82013-02-15 00:48:33 +01003734int cfg80211_check_station_change(struct wiphy *wiphy,
3735 struct station_parameters *params,
3736 enum cfg80211_station_type statype)
3737{
3738 if (params->listen_interval != -1)
3739 return -EINVAL;
3740 if (params->aid)
3741 return -EINVAL;
3742
3743 /* When you run into this, adjust the code below for the new flag */
3744 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3745
3746 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003747 case CFG80211_STA_MESH_PEER_KERNEL:
3748 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003749 /*
3750 * No ignoring the TDLS flag here -- the userspace mesh
3751 * code doesn't have the bug of including TDLS in the
3752 * mask everywhere.
3753 */
3754 if (params->sta_flags_mask &
3755 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3756 BIT(NL80211_STA_FLAG_MFP) |
3757 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3758 return -EINVAL;
3759 break;
3760 case CFG80211_STA_TDLS_PEER_SETUP:
3761 case CFG80211_STA_TDLS_PEER_ACTIVE:
3762 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3763 return -EINVAL;
3764 /* ignore since it can't change */
3765 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3766 break;
3767 default:
3768 /* disallow mesh-specific things */
3769 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3770 return -EINVAL;
3771 if (params->local_pm)
3772 return -EINVAL;
3773 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3774 return -EINVAL;
3775 }
3776
3777 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3778 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3779 /* TDLS can't be set, ... */
3780 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3781 return -EINVAL;
3782 /*
3783 * ... but don't bother the driver with it. This works around
3784 * a hostapd/wpa_supplicant issue -- it always includes the
3785 * TLDS_PEER flag in the mask even for AP mode.
3786 */
3787 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3788 }
3789
3790 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3791 /* reject other things that can't change */
3792 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3793 return -EINVAL;
3794 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3795 return -EINVAL;
3796 if (params->supported_rates)
3797 return -EINVAL;
3798 if (params->ext_capab || params->ht_capa || params->vht_capa)
3799 return -EINVAL;
3800 }
3801
3802 if (statype != CFG80211_STA_AP_CLIENT) {
3803 if (params->vlan)
3804 return -EINVAL;
3805 }
3806
3807 switch (statype) {
3808 case CFG80211_STA_AP_MLME_CLIENT:
3809 /* Use this only for authorizing/unauthorizing a station */
3810 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3811 return -EOPNOTSUPP;
3812 break;
3813 case CFG80211_STA_AP_CLIENT:
3814 /* accept only the listed bits */
3815 if (params->sta_flags_mask &
3816 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3817 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3818 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3819 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3820 BIT(NL80211_STA_FLAG_WME) |
3821 BIT(NL80211_STA_FLAG_MFP)))
3822 return -EINVAL;
3823
3824 /* but authenticated/associated only if driver handles it */
3825 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3826 params->sta_flags_mask &
3827 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3828 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3829 return -EINVAL;
3830 break;
3831 case CFG80211_STA_IBSS:
3832 case CFG80211_STA_AP_STA:
3833 /* reject any changes other than AUTHORIZED */
3834 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3835 return -EINVAL;
3836 break;
3837 case CFG80211_STA_TDLS_PEER_SETUP:
3838 /* reject any changes other than AUTHORIZED or WME */
3839 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3840 BIT(NL80211_STA_FLAG_WME)))
3841 return -EINVAL;
3842 /* force (at least) rates when authorizing */
3843 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3844 !params->supported_rates)
3845 return -EINVAL;
3846 break;
3847 case CFG80211_STA_TDLS_PEER_ACTIVE:
3848 /* reject any changes */
3849 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003850 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003851 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3852 return -EINVAL;
3853 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003854 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003855 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3856 return -EINVAL;
3857 break;
3858 }
3859
3860 return 0;
3861}
3862EXPORT_SYMBOL(cfg80211_check_station_change);
3863
Johannes Berg5727ef12007-12-19 02:03:34 +01003864/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003865 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003866 */
Johannes Berg80b99892011-11-18 16:23:01 +01003867static struct net_device *get_vlan(struct genl_info *info,
3868 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003869{
Johannes Berg463d0182009-07-14 00:33:35 +02003870 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003871 struct net_device *v;
3872 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003873
Johannes Berg80b99892011-11-18 16:23:01 +01003874 if (!vlanattr)
3875 return NULL;
3876
3877 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3878 if (!v)
3879 return ERR_PTR(-ENODEV);
3880
3881 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3882 ret = -EINVAL;
3883 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003884 }
Johannes Berg80b99892011-11-18 16:23:01 +01003885
Johannes Berg77ee7c82013-02-15 00:48:33 +01003886 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3887 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3888 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3889 ret = -EINVAL;
3890 goto error;
3891 }
3892
Johannes Berg80b99892011-11-18 16:23:01 +01003893 if (!netif_running(v)) {
3894 ret = -ENETDOWN;
3895 goto error;
3896 }
3897
3898 return v;
3899 error:
3900 dev_put(v);
3901 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003902}
3903
Jouni Malinendf881292013-02-14 21:10:54 +02003904static struct nla_policy
3905nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3906 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3907 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3908};
3909
Johannes Bergff276692013-02-15 00:09:01 +01003910static int nl80211_parse_sta_wme(struct genl_info *info,
3911 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003912{
Jouni Malinendf881292013-02-14 21:10:54 +02003913 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3914 struct nlattr *nla;
3915 int err;
3916
Jouni Malinendf881292013-02-14 21:10:54 +02003917 /* parse WME attributes if present */
3918 if (!info->attrs[NL80211_ATTR_STA_WME])
3919 return 0;
3920
3921 nla = info->attrs[NL80211_ATTR_STA_WME];
3922 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3923 nl80211_sta_wme_policy);
3924 if (err)
3925 return err;
3926
3927 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3928 params->uapsd_queues = nla_get_u8(
3929 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3930 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3931 return -EINVAL;
3932
3933 if (tb[NL80211_STA_WME_MAX_SP])
3934 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3935
3936 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3937 return -EINVAL;
3938
3939 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3940
3941 return 0;
3942}
3943
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303944static int nl80211_parse_sta_channel_info(struct genl_info *info,
3945 struct station_parameters *params)
3946{
3947 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3948 params->supported_channels =
3949 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3950 params->supported_channels_len =
3951 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3952 /*
3953 * Need to include at least one (first channel, number of
3954 * channels) tuple for each subband, and must have proper
3955 * tuples for the rest of the data as well.
3956 */
3957 if (params->supported_channels_len < 2)
3958 return -EINVAL;
3959 if (params->supported_channels_len % 2)
3960 return -EINVAL;
3961 }
3962
3963 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3964 params->supported_oper_classes =
3965 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3966 params->supported_oper_classes_len =
3967 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3968 /*
3969 * The value of the Length field of the Supported Operating
3970 * Classes element is between 2 and 253.
3971 */
3972 if (params->supported_oper_classes_len < 2 ||
3973 params->supported_oper_classes_len > 253)
3974 return -EINVAL;
3975 }
3976 return 0;
3977}
3978
Johannes Bergff276692013-02-15 00:09:01 +01003979static int nl80211_set_station_tdls(struct genl_info *info,
3980 struct station_parameters *params)
3981{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303982 int err;
Johannes Bergff276692013-02-15 00:09:01 +01003983 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003984 if (info->attrs[NL80211_ATTR_PEER_AID])
3985 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003986 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3987 params->ht_capa =
3988 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3989 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3990 params->vht_capa =
3991 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3992
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303993 err = nl80211_parse_sta_channel_info(info, params);
3994 if (err)
3995 return err;
3996
Johannes Bergff276692013-02-15 00:09:01 +01003997 return nl80211_parse_sta_wme(info, params);
3998}
3999
Johannes Berg5727ef12007-12-19 02:03:34 +01004000static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4001{
Johannes Berg4c476992010-10-04 21:36:35 +02004002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004003 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004004 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004005 u8 *mac_addr;
4006 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004007
4008 memset(&params, 0, sizeof(params));
4009
4010 params.listen_interval = -1;
4011
Johannes Berg77ee7c82013-02-15 00:48:33 +01004012 if (!rdev->ops->change_station)
4013 return -EOPNOTSUPP;
4014
Johannes Berg5727ef12007-12-19 02:03:34 +01004015 if (info->attrs[NL80211_ATTR_STA_AID])
4016 return -EINVAL;
4017
4018 if (!info->attrs[NL80211_ATTR_MAC])
4019 return -EINVAL;
4020
4021 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4022
4023 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4024 params.supported_rates =
4025 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4026 params.supported_rates_len =
4027 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4028 }
4029
Jouni Malinen9d62a982013-02-14 21:10:13 +02004030 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4031 params.capability =
4032 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4033 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4034 }
4035
4036 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4037 params.ext_capab =
4038 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4039 params.ext_capab_len =
4040 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4041 }
4042
Jouni Malinendf881292013-02-14 21:10:54 +02004043 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004044 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004045
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004046 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004047 return -EINVAL;
4048
Johannes Bergf8bacc22013-02-14 23:27:01 +01004049 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004050 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004051 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4052 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4053 return -EINVAL;
4054 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004055
Johannes Bergf8bacc22013-02-14 23:27:01 +01004056 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004057 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004058 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4059 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4060 return -EINVAL;
4061 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4062 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004063
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004064 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4065 enum nl80211_mesh_power_mode pm = nla_get_u32(
4066 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4067
4068 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4069 pm > NL80211_MESH_POWER_MAX)
4070 return -EINVAL;
4071
4072 params.local_pm = pm;
4073 }
4074
Johannes Berg77ee7c82013-02-15 00:48:33 +01004075 /* Include parameters for TDLS peer (will check later) */
4076 err = nl80211_set_station_tdls(info, &params);
4077 if (err)
4078 return err;
4079
4080 params.vlan = get_vlan(info, rdev);
4081 if (IS_ERR(params.vlan))
4082 return PTR_ERR(params.vlan);
4083
Johannes Berga97f4422009-06-18 17:23:43 +02004084 switch (dev->ieee80211_ptr->iftype) {
4085 case NL80211_IFTYPE_AP:
4086 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004087 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004088 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004089 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004090 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004091 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004092 break;
4093 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004094 err = -EOPNOTSUPP;
4095 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004096 }
4097
Johannes Berg77ee7c82013-02-15 00:48:33 +01004098 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004099 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004100
Johannes Berg77ee7c82013-02-15 00:48:33 +01004101 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004102 if (params.vlan)
4103 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004104
Johannes Berg5727ef12007-12-19 02:03:34 +01004105 return err;
4106}
4107
4108static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4109{
Johannes Berg4c476992010-10-04 21:36:35 +02004110 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004111 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004112 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004113 struct station_parameters params;
4114 u8 *mac_addr = NULL;
4115
4116 memset(&params, 0, sizeof(params));
4117
Johannes Berg984c3112013-02-14 23:43:25 +01004118 if (!rdev->ops->add_station)
4119 return -EOPNOTSUPP;
4120
Johannes Berg5727ef12007-12-19 02:03:34 +01004121 if (!info->attrs[NL80211_ATTR_MAC])
4122 return -EINVAL;
4123
Johannes Berg5727ef12007-12-19 02:03:34 +01004124 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4125 return -EINVAL;
4126
4127 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4128 return -EINVAL;
4129
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004130 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4131 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004132 return -EINVAL;
4133
Johannes Berg5727ef12007-12-19 02:03:34 +01004134 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4135 params.supported_rates =
4136 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4137 params.supported_rates_len =
4138 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4139 params.listen_interval =
4140 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004141
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004142 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004143 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004144 else
4145 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004146 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4147 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004148
Jouni Malinen9d62a982013-02-14 21:10:13 +02004149 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4150 params.capability =
4151 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4152 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4153 }
4154
4155 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4156 params.ext_capab =
4157 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4158 params.ext_capab_len =
4159 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4160 }
4161
Jouni Malinen36aedc902008-08-25 11:58:58 +03004162 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4163 params.ht_capa =
4164 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004165
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004166 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4167 params.vht_capa =
4168 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4169
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004170 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4171 params.opmode_notif_used = true;
4172 params.opmode_notif =
4173 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4174 }
4175
Johannes Bergf8bacc22013-02-14 23:27:01 +01004176 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004177 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004178 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4179 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4180 return -EINVAL;
4181 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004182
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304183 err = nl80211_parse_sta_channel_info(info, &params);
4184 if (err)
4185 return err;
4186
Johannes Bergff276692013-02-15 00:09:01 +01004187 err = nl80211_parse_sta_wme(info, &params);
4188 if (err)
4189 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004190
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004191 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004192 return -EINVAL;
4193
Johannes Berg77ee7c82013-02-15 00:48:33 +01004194 /* When you run into this, adjust the code below for the new flag */
4195 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4196
Johannes Bergbdd90d52011-12-14 12:20:27 +01004197 switch (dev->ieee80211_ptr->iftype) {
4198 case NL80211_IFTYPE_AP:
4199 case NL80211_IFTYPE_AP_VLAN:
4200 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004201 /* ignore WME attributes if iface/sta is not capable */
4202 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4203 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4204 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004205
Johannes Bergbdd90d52011-12-14 12:20:27 +01004206 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004207 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4208 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004209 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004210 /* but don't bother the driver with it */
4211 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004212
Johannes Bergd582cff2012-10-26 17:53:44 +02004213 /* allow authenticated/associated only if driver handles it */
4214 if (!(rdev->wiphy.features &
4215 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4216 params.sta_flags_mask &
4217 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4218 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4219 return -EINVAL;
4220
Johannes Bergbdd90d52011-12-14 12:20:27 +01004221 /* must be last in here for error handling */
4222 params.vlan = get_vlan(info, rdev);
4223 if (IS_ERR(params.vlan))
4224 return PTR_ERR(params.vlan);
4225 break;
4226 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004227 /* ignore uAPSD data */
4228 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4229
Johannes Bergd582cff2012-10-26 17:53:44 +02004230 /* associated is disallowed */
4231 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4232 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004233 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004234 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4235 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004236 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004237 break;
4238 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004239 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004240 /* ignore uAPSD data */
4241 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4242
Johannes Berg77ee7c82013-02-15 00:48:33 +01004243 /* these are disallowed */
4244 if (params.sta_flags_mask &
4245 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4246 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004247 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004248 /* Only TDLS peers can be added */
4249 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4250 return -EINVAL;
4251 /* Can only add if TDLS ... */
4252 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4253 return -EOPNOTSUPP;
4254 /* ... with external setup is supported */
4255 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4256 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004257 /*
4258 * Older wpa_supplicant versions always mark the TDLS peer
4259 * as authorized, but it shouldn't yet be.
4260 */
4261 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004262 break;
4263 default:
4264 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004265 }
4266
Johannes Bergbdd90d52011-12-14 12:20:27 +01004267 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004268
Hila Gonene35e4d22012-06-27 17:19:42 +03004269 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004270
Johannes Berg5727ef12007-12-19 02:03:34 +01004271 if (params.vlan)
4272 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004273 return err;
4274}
4275
4276static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4277{
Johannes Berg4c476992010-10-04 21:36:35 +02004278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4279 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004280 u8 *mac_addr = NULL;
4281
4282 if (info->attrs[NL80211_ATTR_MAC])
4283 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4284
Johannes Berge80cf852009-05-11 14:43:13 +02004285 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004286 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004287 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004288 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4289 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004290
Johannes Berg4c476992010-10-04 21:36:35 +02004291 if (!rdev->ops->del_station)
4292 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004293
Hila Gonene35e4d22012-06-27 17:19:42 +03004294 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004295}
4296
Eric W. Biederman15e47302012-09-07 20:12:54 +00004297static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004298 int flags, struct net_device *dev,
4299 u8 *dst, u8 *next_hop,
4300 struct mpath_info *pinfo)
4301{
4302 void *hdr;
4303 struct nlattr *pinfoattr;
4304
Eric W. Biederman15e47302012-09-07 20:12:54 +00004305 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004306 if (!hdr)
4307 return -1;
4308
David S. Miller9360ffd2012-03-29 04:41:26 -04004309 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4310 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4311 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4312 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4313 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004314
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004315 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4316 if (!pinfoattr)
4317 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004318 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4319 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4320 pinfo->frame_qlen))
4321 goto nla_put_failure;
4322 if (((pinfo->filled & MPATH_INFO_SN) &&
4323 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4324 ((pinfo->filled & MPATH_INFO_METRIC) &&
4325 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4326 pinfo->metric)) ||
4327 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4328 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4329 pinfo->exptime)) ||
4330 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4331 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4332 pinfo->flags)) ||
4333 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4334 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4335 pinfo->discovery_timeout)) ||
4336 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4337 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4338 pinfo->discovery_retries)))
4339 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340
4341 nla_nest_end(msg, pinfoattr);
4342
4343 return genlmsg_end(msg, hdr);
4344
4345 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004346 genlmsg_cancel(msg, hdr);
4347 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004348}
4349
4350static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004351 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004352{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004353 struct mpath_info pinfo;
4354 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004355 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004356 u8 dst[ETH_ALEN];
4357 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004358 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004359 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004360
Johannes Berg97990a02013-04-19 01:02:55 +02004361 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004362 if (err)
4363 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004364
4365 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004366 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004367 goto out_err;
4368 }
4369
Johannes Berg97990a02013-04-19 01:02:55 +02004370 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004371 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004372 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004373 }
4374
Johannes Bergbba95fe2008-07-29 13:22:51 +02004375 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004376 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4377 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004378 if (err == -ENOENT)
4379 break;
4380 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004381 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004382
Eric W. Biederman15e47302012-09-07 20:12:54 +00004383 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004384 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004385 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004386 &pinfo) < 0)
4387 goto out;
4388
4389 path_idx++;
4390 }
4391
4392
4393 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004394 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004395 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004396 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004397 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004398 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004399}
4400
4401static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4402{
Johannes Berg4c476992010-10-04 21:36:35 +02004403 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004404 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004405 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004406 struct mpath_info pinfo;
4407 struct sk_buff *msg;
4408 u8 *dst = NULL;
4409 u8 next_hop[ETH_ALEN];
4410
4411 memset(&pinfo, 0, sizeof(pinfo));
4412
4413 if (!info->attrs[NL80211_ATTR_MAC])
4414 return -EINVAL;
4415
4416 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4417
Johannes Berg4c476992010-10-04 21:36:35 +02004418 if (!rdev->ops->get_mpath)
4419 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004420
Johannes Berg4c476992010-10-04 21:36:35 +02004421 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4422 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004423
Hila Gonene35e4d22012-06-27 17:19:42 +03004424 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004425 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004426 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004427
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004429 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004430 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004431
Eric W. Biederman15e47302012-09-07 20:12:54 +00004432 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004433 dev, dst, next_hop, &pinfo) < 0) {
4434 nlmsg_free(msg);
4435 return -ENOBUFS;
4436 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004437
Johannes Berg4c476992010-10-04 21:36:35 +02004438 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004439}
4440
4441static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4442{
Johannes Berg4c476992010-10-04 21:36:35 +02004443 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4444 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004445 u8 *dst = NULL;
4446 u8 *next_hop = NULL;
4447
4448 if (!info->attrs[NL80211_ATTR_MAC])
4449 return -EINVAL;
4450
4451 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4452 return -EINVAL;
4453
4454 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4455 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4456
Johannes Berg4c476992010-10-04 21:36:35 +02004457 if (!rdev->ops->change_mpath)
4458 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004459
Johannes Berg4c476992010-10-04 21:36:35 +02004460 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4461 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004462
Hila Gonene35e4d22012-06-27 17:19:42 +03004463 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004464}
Johannes Berg4c476992010-10-04 21:36:35 +02004465
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004466static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4467{
Johannes Berg4c476992010-10-04 21:36:35 +02004468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4469 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470 u8 *dst = NULL;
4471 u8 *next_hop = NULL;
4472
4473 if (!info->attrs[NL80211_ATTR_MAC])
4474 return -EINVAL;
4475
4476 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4477 return -EINVAL;
4478
4479 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4480 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4481
Johannes Berg4c476992010-10-04 21:36:35 +02004482 if (!rdev->ops->add_mpath)
4483 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004484
Johannes Berg4c476992010-10-04 21:36:35 +02004485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4486 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487
Hila Gonene35e4d22012-06-27 17:19:42 +03004488 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004489}
4490
4491static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495 u8 *dst = NULL;
4496
4497 if (info->attrs[NL80211_ATTR_MAC])
4498 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4499
Johannes Berg4c476992010-10-04 21:36:35 +02004500 if (!rdev->ops->del_mpath)
4501 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004502
Hila Gonene35e4d22012-06-27 17:19:42 +03004503 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004504}
4505
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004506static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4507{
Johannes Berg4c476992010-10-04 21:36:35 +02004508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4509 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004510 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004511 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004512 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004513
4514 memset(&params, 0, sizeof(params));
4515 /* default to not changing parameters */
4516 params.use_cts_prot = -1;
4517 params.use_short_preamble = -1;
4518 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004519 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004520 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004521 params.p2p_ctwindow = -1;
4522 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004523
4524 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4525 params.use_cts_prot =
4526 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4527 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4528 params.use_short_preamble =
4529 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4530 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4531 params.use_short_slot_time =
4532 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004533 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4534 params.basic_rates =
4535 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4536 params.basic_rates_len =
4537 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4538 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004539 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4540 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004541 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4542 params.ht_opmode =
4543 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004544
Johannes Berg53cabad2012-11-14 15:17:28 +01004545 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4546 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4547 return -EINVAL;
4548 params.p2p_ctwindow =
4549 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4550 if (params.p2p_ctwindow < 0)
4551 return -EINVAL;
4552 if (params.p2p_ctwindow != 0 &&
4553 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4554 return -EINVAL;
4555 }
4556
4557 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4558 u8 tmp;
4559
4560 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4561 return -EINVAL;
4562 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4563 if (tmp > 1)
4564 return -EINVAL;
4565 params.p2p_opp_ps = tmp;
4566 if (params.p2p_opp_ps &&
4567 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4568 return -EINVAL;
4569 }
4570
Johannes Berg4c476992010-10-04 21:36:35 +02004571 if (!rdev->ops->change_bss)
4572 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004573
Johannes Berg074ac8d2010-09-16 14:58:22 +02004574 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004575 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4576 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004577
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004578 wdev_lock(wdev);
4579 err = rdev_change_bss(rdev, dev, &params);
4580 wdev_unlock(wdev);
4581
4582 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004583}
4584
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004585static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004586 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4587 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4588 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4589 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4590 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4591 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4592};
4593
4594static int parse_reg_rule(struct nlattr *tb[],
4595 struct ieee80211_reg_rule *reg_rule)
4596{
4597 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4598 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4599
4600 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4601 return -EINVAL;
4602 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4603 return -EINVAL;
4604 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4605 return -EINVAL;
4606 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4607 return -EINVAL;
4608 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4609 return -EINVAL;
4610
4611 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4612
4613 freq_range->start_freq_khz =
4614 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4615 freq_range->end_freq_khz =
4616 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4617 freq_range->max_bandwidth_khz =
4618 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4619
4620 power_rule->max_eirp =
4621 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4622
4623 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4624 power_rule->max_antenna_gain =
4625 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4626
4627 return 0;
4628}
4629
4630static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4631{
4632 int r;
4633 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004634 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004635
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004636 /*
4637 * You should only get this when cfg80211 hasn't yet initialized
4638 * completely when built-in to the kernel right between the time
4639 * window between nl80211_init() and regulatory_init(), if that is
4640 * even possible.
4641 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004642 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004643 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004644
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004645 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4646 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004647
4648 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4649
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004650 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4651 user_reg_hint_type =
4652 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4653 else
4654 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4655
4656 switch (user_reg_hint_type) {
4657 case NL80211_USER_REG_HINT_USER:
4658 case NL80211_USER_REG_HINT_CELL_BASE:
4659 break;
4660 default:
4661 return -EINVAL;
4662 }
4663
4664 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004665
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004666 return r;
4667}
4668
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004669static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004670 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004671{
Johannes Berg4c476992010-10-04 21:36:35 +02004672 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004673 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004674 struct wireless_dev *wdev = dev->ieee80211_ptr;
4675 struct mesh_config cur_params;
4676 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004677 void *hdr;
4678 struct nlattr *pinfoattr;
4679 struct sk_buff *msg;
4680
Johannes Berg29cbe682010-12-03 09:20:44 +01004681 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4682 return -EOPNOTSUPP;
4683
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004684 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004685 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004686
Johannes Berg29cbe682010-12-03 09:20:44 +01004687 wdev_lock(wdev);
4688 /* If not connected, get default parameters */
4689 if (!wdev->mesh_id_len)
4690 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4691 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004692 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004693 wdev_unlock(wdev);
4694
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004695 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004696 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004697
4698 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004700 if (!msg)
4701 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004702 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004703 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004704 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004705 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004706 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004707 if (!pinfoattr)
4708 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004709 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4710 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4711 cur_params.dot11MeshRetryTimeout) ||
4712 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4713 cur_params.dot11MeshConfirmTimeout) ||
4714 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4715 cur_params.dot11MeshHoldingTimeout) ||
4716 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4717 cur_params.dot11MeshMaxPeerLinks) ||
4718 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4719 cur_params.dot11MeshMaxRetries) ||
4720 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4721 cur_params.dot11MeshTTL) ||
4722 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4723 cur_params.element_ttl) ||
4724 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4725 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004726 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4727 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004728 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4729 cur_params.dot11MeshHWMPmaxPREQretries) ||
4730 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4731 cur_params.path_refresh_time) ||
4732 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4733 cur_params.min_discovery_timeout) ||
4734 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4735 cur_params.dot11MeshHWMPactivePathTimeout) ||
4736 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4737 cur_params.dot11MeshHWMPpreqMinInterval) ||
4738 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4739 cur_params.dot11MeshHWMPperrMinInterval) ||
4740 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4741 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4742 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4743 cur_params.dot11MeshHWMPRootMode) ||
4744 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4745 cur_params.dot11MeshHWMPRannInterval) ||
4746 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4747 cur_params.dot11MeshGateAnnouncementProtocol) ||
4748 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4749 cur_params.dot11MeshForwarding) ||
4750 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004751 cur_params.rssi_threshold) ||
4752 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004753 cur_params.ht_opmode) ||
4754 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4755 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4756 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004757 cur_params.dot11MeshHWMProotInterval) ||
4758 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004759 cur_params.dot11MeshHWMPconfirmationInterval) ||
4760 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4761 cur_params.power_mode) ||
4762 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004763 cur_params.dot11MeshAwakeWindowDuration) ||
4764 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4765 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004766 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004767 nla_nest_end(msg, pinfoattr);
4768 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004769 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004770
Johannes Berg3b858752009-03-12 09:55:09 +01004771 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004772 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004773 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004774 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004775 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004776}
4777
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004778static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004779 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4780 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4781 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4782 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4783 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4784 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004785 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004786 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004787 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004788 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4789 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4790 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4791 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4792 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004793 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004794 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004795 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004796 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004797 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004798 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004799 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4800 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004801 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4802 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004803 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004804 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4805 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004806 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004807};
4808
Javier Cardonac80d5452010-12-16 17:37:49 -08004809static const struct nla_policy
4810 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004811 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004812 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4813 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004814 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004815 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004816 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004817 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004818 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004819 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004820};
4821
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004822static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004823 struct mesh_config *cfg,
4824 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004825{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004826 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004828
Marco Porschea54fba2013-01-07 16:04:48 +01004829#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4830do { \
4831 if (tb[attr]) { \
4832 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4833 return -EINVAL; \
4834 cfg->param = fn(tb[attr]); \
4835 mask |= (1 << (attr - 1)); \
4836 } \
4837} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004838
4839
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004840 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004841 return -EINVAL;
4842 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004843 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004844 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004845 return -EINVAL;
4846
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004847 /* This makes sure that there aren't more than 32 mesh config
4848 * parameters (otherwise our bitfield scheme would not work.) */
4849 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4850
4851 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004852 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004853 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4854 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004855 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004856 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4857 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004858 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004859 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4860 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004861 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004862 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4863 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004864 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004865 mask, NL80211_MESHCONF_MAX_RETRIES,
4866 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004867 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004868 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004869 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004870 mask, NL80211_MESHCONF_ELEMENT_TTL,
4871 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004872 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004873 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4874 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004875 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4876 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004877 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4878 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004880 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4881 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004883 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4884 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004886 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4887 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4889 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004890 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4891 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004893 1, 65535, mask,
4894 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004895 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004896 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004897 1, 65535, mask,
4898 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004899 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004901 dot11MeshHWMPnetDiameterTraversalTime,
4902 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004903 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4904 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004905 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4906 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4907 nla_get_u8);
4908 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4909 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004910 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004912 dot11MeshGateAnnouncementProtocol, 0, 1,
4913 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004914 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004916 mask, NL80211_MESHCONF_FORWARDING,
4917 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004918 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004919 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004920 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004922 mask, NL80211_MESHCONF_HT_OPMODE,
4923 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004925 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004926 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4927 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004928 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004929 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4930 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004931 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004932 dot11MeshHWMPconfirmationInterval,
4933 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004934 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4935 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4937 NL80211_MESH_POWER_ACTIVE,
4938 NL80211_MESH_POWER_MAX,
4939 mask, NL80211_MESHCONF_POWER_MODE,
4940 nla_get_u32);
4941 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4942 0, 65535, mask,
4943 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004944 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4945 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4946 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004947 if (mask_out)
4948 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004949
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004950 return 0;
4951
4952#undef FILL_IN_MESH_PARAM_IF_SET
4953}
4954
Javier Cardonac80d5452010-12-16 17:37:49 -08004955static int nl80211_parse_mesh_setup(struct genl_info *info,
4956 struct mesh_setup *setup)
4957{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004958 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004959 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4960
4961 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4962 return -EINVAL;
4963 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4964 info->attrs[NL80211_ATTR_MESH_SETUP],
4965 nl80211_mesh_setup_params_policy))
4966 return -EINVAL;
4967
Javier Cardonad299a1f2012-03-31 11:31:33 -07004968 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4969 setup->sync_method =
4970 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4971 IEEE80211_SYNC_METHOD_VENDOR :
4972 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4973
Javier Cardonac80d5452010-12-16 17:37:49 -08004974 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4975 setup->path_sel_proto =
4976 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4977 IEEE80211_PATH_PROTOCOL_VENDOR :
4978 IEEE80211_PATH_PROTOCOL_HWMP;
4979
4980 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4981 setup->path_metric =
4982 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4983 IEEE80211_PATH_METRIC_VENDOR :
4984 IEEE80211_PATH_METRIC_AIRTIME;
4985
Javier Cardona581a8b02011-04-07 15:08:27 -07004986
4987 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004988 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004989 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004990 if (!is_valid_ie_attr(ieattr))
4991 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004992 setup->ie = nla_data(ieattr);
4993 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004994 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004995 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4996 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4997 return -EINVAL;
4998 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004999 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5000 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005001 if (setup->is_secure)
5002 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005003
Colleen Twitty6e16d902013-05-08 11:45:59 -07005004 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5005 if (!setup->user_mpm)
5006 return -EINVAL;
5007 setup->auth_id =
5008 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5009 }
5010
Javier Cardonac80d5452010-12-16 17:37:49 -08005011 return 0;
5012}
5013
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005014static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005015 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005016{
5017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5018 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005019 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005020 struct mesh_config cfg;
5021 u32 mask;
5022 int err;
5023
Johannes Berg29cbe682010-12-03 09:20:44 +01005024 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5025 return -EOPNOTSUPP;
5026
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005027 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005028 return -EOPNOTSUPP;
5029
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005030 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005031 if (err)
5032 return err;
5033
Johannes Berg29cbe682010-12-03 09:20:44 +01005034 wdev_lock(wdev);
5035 if (!wdev->mesh_id_len)
5036 err = -ENOLINK;
5037
5038 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005039 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005040
5041 wdev_unlock(wdev);
5042
5043 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005044}
5045
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005046static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5047{
Johannes Berg458f4f92012-12-06 15:47:38 +01005048 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005049 struct sk_buff *msg;
5050 void *hdr = NULL;
5051 struct nlattr *nl_reg_rules;
5052 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005053
5054 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005055 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005056
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005058 if (!msg)
5059 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005060
Eric W. Biederman15e47302012-09-07 20:12:54 +00005061 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005062 NL80211_CMD_GET_REG);
5063 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005064 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005065
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005066 if (reg_last_request_cell_base() &&
5067 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5068 NL80211_USER_REG_HINT_CELL_BASE))
5069 goto nla_put_failure;
5070
Johannes Berg458f4f92012-12-06 15:47:38 +01005071 rcu_read_lock();
5072 regdom = rcu_dereference(cfg80211_regdomain);
5073
5074 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5075 (regdom->dfs_region &&
5076 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5077 goto nla_put_failure_rcu;
5078
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005079 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5080 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005081 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005082
Johannes Berg458f4f92012-12-06 15:47:38 +01005083 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005084 struct nlattr *nl_reg_rule;
5085 const struct ieee80211_reg_rule *reg_rule;
5086 const struct ieee80211_freq_range *freq_range;
5087 const struct ieee80211_power_rule *power_rule;
5088
Johannes Berg458f4f92012-12-06 15:47:38 +01005089 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005090 freq_range = &reg_rule->freq_range;
5091 power_rule = &reg_rule->power_rule;
5092
5093 nl_reg_rule = nla_nest_start(msg, i);
5094 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005095 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005096
David S. Miller9360ffd2012-03-29 04:41:26 -04005097 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5098 reg_rule->flags) ||
5099 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5100 freq_range->start_freq_khz) ||
5101 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5102 freq_range->end_freq_khz) ||
5103 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5104 freq_range->max_bandwidth_khz) ||
5105 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5106 power_rule->max_antenna_gain) ||
5107 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5108 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005109 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005110
5111 nla_nest_end(msg, nl_reg_rule);
5112 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005113 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005114
5115 nla_nest_end(msg, nl_reg_rules);
5116
5117 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005118 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005119
Johannes Berg458f4f92012-12-06 15:47:38 +01005120nla_put_failure_rcu:
5121 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005122nla_put_failure:
5123 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005124put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005125 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005126 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005127}
5128
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005129static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5130{
5131 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5132 struct nlattr *nl_reg_rule;
5133 char *alpha2 = NULL;
5134 int rem_reg_rules = 0, r = 0;
5135 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005136 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005137 struct ieee80211_regdomain *rd = NULL;
5138
5139 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5140 return -EINVAL;
5141
5142 if (!info->attrs[NL80211_ATTR_REG_RULES])
5143 return -EINVAL;
5144
5145 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5146
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005147 if (info->attrs[NL80211_ATTR_DFS_REGION])
5148 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5149
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005150 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005151 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005152 num_rules++;
5153 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005154 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005155 }
5156
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005157 if (!reg_is_valid_request(alpha2))
5158 return -EINVAL;
5159
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005160 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005161 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005162
5163 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005164 if (!rd)
5165 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005166
5167 rd->n_reg_rules = num_rules;
5168 rd->alpha2[0] = alpha2[0];
5169 rd->alpha2[1] = alpha2[1];
5170
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005171 /*
5172 * Disable DFS master mode if the DFS region was
5173 * not supported or known on this kernel.
5174 */
5175 if (reg_supported_dfs_region(dfs_region))
5176 rd->dfs_region = dfs_region;
5177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005178 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005179 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005180 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005181 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5182 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005183 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5184 if (r)
5185 goto bad_reg;
5186
5187 rule_idx++;
5188
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005189 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5190 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005191 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005192 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005193 }
5194
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005195 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005196 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005197 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005198
Johannes Bergd2372b32008-10-24 20:32:20 +02005199 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005200 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005201 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005202}
5203
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005204static int validate_scan_freqs(struct nlattr *freqs)
5205{
5206 struct nlattr *attr1, *attr2;
5207 int n_channels = 0, tmp1, tmp2;
5208
5209 nla_for_each_nested(attr1, freqs, tmp1) {
5210 n_channels++;
5211 /*
5212 * Some hardware has a limited channel list for
5213 * scanning, and it is pretty much nonsensical
5214 * to scan for a channel twice, so disallow that
5215 * and don't require drivers to check that the
5216 * channel list they get isn't longer than what
5217 * they can scan, as long as they can scan all
5218 * the channels they registered at once.
5219 */
5220 nla_for_each_nested(attr2, freqs, tmp2)
5221 if (attr1 != attr2 &&
5222 nla_get_u32(attr1) == nla_get_u32(attr2))
5223 return 0;
5224 }
5225
5226 return n_channels;
5227}
5228
Johannes Berg2a519312009-02-10 21:25:55 +01005229static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5230{
Johannes Berg4c476992010-10-04 21:36:35 +02005231 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005232 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005233 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005234 struct nlattr *attr;
5235 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005236 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005237 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005238
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005239 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5240 return -EINVAL;
5241
Johannes Berg79c97e92009-07-07 03:56:12 +02005242 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005243
Johannes Berg4c476992010-10-04 21:36:35 +02005244 if (!rdev->ops->scan)
5245 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005246
Johannes Bergf9f47522013-03-19 15:04:07 +01005247 if (rdev->scan_req) {
5248 err = -EBUSY;
5249 goto unlock;
5250 }
Johannes Berg2a519312009-02-10 21:25:55 +01005251
5252 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005253 n_channels = validate_scan_freqs(
5254 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005255 if (!n_channels) {
5256 err = -EINVAL;
5257 goto unlock;
5258 }
Johannes Berg2a519312009-02-10 21:25:55 +01005259 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005260 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005261 }
5262
5263 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5264 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5265 n_ssids++;
5266
Johannes Bergf9f47522013-03-19 15:04:07 +01005267 if (n_ssids > wiphy->max_scan_ssids) {
5268 err = -EINVAL;
5269 goto unlock;
5270 }
Johannes Berg2a519312009-02-10 21:25:55 +01005271
Jouni Malinen70692ad2009-02-16 19:39:13 +02005272 if (info->attrs[NL80211_ATTR_IE])
5273 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5274 else
5275 ie_len = 0;
5276
Johannes Bergf9f47522013-03-19 15:04:07 +01005277 if (ie_len > wiphy->max_scan_ie_len) {
5278 err = -EINVAL;
5279 goto unlock;
5280 }
Johannes Berg18a83652009-03-31 12:12:05 +02005281
Johannes Berg2a519312009-02-10 21:25:55 +01005282 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005283 + sizeof(*request->ssids) * n_ssids
5284 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005285 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005286 if (!request) {
5287 err = -ENOMEM;
5288 goto unlock;
5289 }
Johannes Berg2a519312009-02-10 21:25:55 +01005290
Johannes Berg2a519312009-02-10 21:25:55 +01005291 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005292 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005293 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005294 if (ie_len) {
5295 if (request->ssids)
5296 request->ie = (void *)(request->ssids + n_ssids);
5297 else
5298 request->ie = (void *)(request->channels + n_channels);
5299 }
Johannes Berg2a519312009-02-10 21:25:55 +01005300
Johannes Berg584991d2009-11-02 13:32:03 +01005301 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005302 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5303 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005304 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005305 struct ieee80211_channel *chan;
5306
5307 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5308
5309 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005310 err = -EINVAL;
5311 goto out_free;
5312 }
Johannes Berg584991d2009-11-02 13:32:03 +01005313
5314 /* ignore disabled channels */
5315 if (chan->flags & IEEE80211_CHAN_DISABLED)
5316 continue;
5317
5318 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005319 i++;
5320 }
5321 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005322 enum ieee80211_band band;
5323
Johannes Berg2a519312009-02-10 21:25:55 +01005324 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005325 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5326 int j;
5327 if (!wiphy->bands[band])
5328 continue;
5329 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005330 struct ieee80211_channel *chan;
5331
5332 chan = &wiphy->bands[band]->channels[j];
5333
5334 if (chan->flags & IEEE80211_CHAN_DISABLED)
5335 continue;
5336
5337 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005338 i++;
5339 }
5340 }
5341 }
5342
Johannes Berg584991d2009-11-02 13:32:03 +01005343 if (!i) {
5344 err = -EINVAL;
5345 goto out_free;
5346 }
5347
5348 request->n_channels = i;
5349
Johannes Berg2a519312009-02-10 21:25:55 +01005350 i = 0;
5351 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5352 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005353 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005354 err = -EINVAL;
5355 goto out_free;
5356 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005357 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005358 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005359 i++;
5360 }
5361 }
5362
Jouni Malinen70692ad2009-02-16 19:39:13 +02005363 if (info->attrs[NL80211_ATTR_IE]) {
5364 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005365 memcpy((void *)request->ie,
5366 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005367 request->ie_len);
5368 }
5369
Johannes Berg34850ab2011-07-18 18:08:35 +02005370 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005371 if (wiphy->bands[i])
5372 request->rates[i] =
5373 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005374
5375 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5376 nla_for_each_nested(attr,
5377 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5378 tmp) {
5379 enum ieee80211_band band = nla_type(attr);
5380
Dan Carpenter84404622011-07-29 11:52:18 +03005381 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005382 err = -EINVAL;
5383 goto out_free;
5384 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005385
5386 if (!wiphy->bands[band])
5387 continue;
5388
Johannes Berg34850ab2011-07-18 18:08:35 +02005389 err = ieee80211_get_ratemask(wiphy->bands[band],
5390 nla_data(attr),
5391 nla_len(attr),
5392 &request->rates[band]);
5393 if (err)
5394 goto out_free;
5395 }
5396 }
5397
Sam Leffler46856bb2012-10-11 21:03:32 -07005398 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005399 request->flags = nla_get_u32(
5400 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005401 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5402 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005403 err = -EOPNOTSUPP;
5404 goto out_free;
5405 }
5406 }
Sam Lefflered4737712012-10-11 21:03:31 -07005407
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305408 request->no_cck =
5409 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5410
Johannes Bergfd014282012-06-18 19:17:03 +02005411 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005412 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005413 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005414
Johannes Berg79c97e92009-07-07 03:56:12 +02005415 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005416 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005417
Johannes Berg463d0182009-07-14 00:33:35 +02005418 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005419 nl80211_send_scan_start(rdev, wdev);
5420 if (wdev->netdev)
5421 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005422 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005423 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005424 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005425 kfree(request);
5426 }
Johannes Berg3b858752009-03-12 09:55:09 +01005427
Johannes Bergf9f47522013-03-19 15:04:07 +01005428 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005429 return err;
5430}
5431
Luciano Coelho807f8a82011-05-11 17:09:35 +03005432static int nl80211_start_sched_scan(struct sk_buff *skb,
5433 struct genl_info *info)
5434{
5435 struct cfg80211_sched_scan_request *request;
5436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5437 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005438 struct nlattr *attr;
5439 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005440 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005441 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005442 enum ieee80211_band band;
5443 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005444 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005445
5446 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5447 !rdev->ops->sched_scan_start)
5448 return -EOPNOTSUPP;
5449
5450 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5451 return -EINVAL;
5452
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005453 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5454 return -EINVAL;
5455
5456 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5457 if (interval == 0)
5458 return -EINVAL;
5459
Luciano Coelho807f8a82011-05-11 17:09:35 +03005460 wiphy = &rdev->wiphy;
5461
5462 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5463 n_channels = validate_scan_freqs(
5464 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5465 if (!n_channels)
5466 return -EINVAL;
5467 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005468 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005469 }
5470
5471 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5472 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5473 tmp)
5474 n_ssids++;
5475
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005476 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005477 return -EINVAL;
5478
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005479 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5480 nla_for_each_nested(attr,
5481 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5482 tmp)
5483 n_match_sets++;
5484
5485 if (n_match_sets > wiphy->max_match_sets)
5486 return -EINVAL;
5487
Luciano Coelho807f8a82011-05-11 17:09:35 +03005488 if (info->attrs[NL80211_ATTR_IE])
5489 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5490 else
5491 ie_len = 0;
5492
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005493 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005494 return -EINVAL;
5495
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005496 if (rdev->sched_scan_req) {
5497 err = -EINPROGRESS;
5498 goto out;
5499 }
5500
Luciano Coelho807f8a82011-05-11 17:09:35 +03005501 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005502 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005503 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005504 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005505 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005506 if (!request) {
5507 err = -ENOMEM;
5508 goto out;
5509 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005510
5511 if (n_ssids)
5512 request->ssids = (void *)&request->channels[n_channels];
5513 request->n_ssids = n_ssids;
5514 if (ie_len) {
5515 if (request->ssids)
5516 request->ie = (void *)(request->ssids + n_ssids);
5517 else
5518 request->ie = (void *)(request->channels + n_channels);
5519 }
5520
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005521 if (n_match_sets) {
5522 if (request->ie)
5523 request->match_sets = (void *)(request->ie + ie_len);
5524 else if (request->ssids)
5525 request->match_sets =
5526 (void *)(request->ssids + n_ssids);
5527 else
5528 request->match_sets =
5529 (void *)(request->channels + n_channels);
5530 }
5531 request->n_match_sets = n_match_sets;
5532
Luciano Coelho807f8a82011-05-11 17:09:35 +03005533 i = 0;
5534 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5535 /* user specified, bail out if channel not found */
5536 nla_for_each_nested(attr,
5537 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5538 tmp) {
5539 struct ieee80211_channel *chan;
5540
5541 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5542
5543 if (!chan) {
5544 err = -EINVAL;
5545 goto out_free;
5546 }
5547
5548 /* ignore disabled channels */
5549 if (chan->flags & IEEE80211_CHAN_DISABLED)
5550 continue;
5551
5552 request->channels[i] = chan;
5553 i++;
5554 }
5555 } else {
5556 /* all channels */
5557 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5558 int j;
5559 if (!wiphy->bands[band])
5560 continue;
5561 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5562 struct ieee80211_channel *chan;
5563
5564 chan = &wiphy->bands[band]->channels[j];
5565
5566 if (chan->flags & IEEE80211_CHAN_DISABLED)
5567 continue;
5568
5569 request->channels[i] = chan;
5570 i++;
5571 }
5572 }
5573 }
5574
5575 if (!i) {
5576 err = -EINVAL;
5577 goto out_free;
5578 }
5579
5580 request->n_channels = i;
5581
5582 i = 0;
5583 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5584 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5585 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005586 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005587 err = -EINVAL;
5588 goto out_free;
5589 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005590 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005591 memcpy(request->ssids[i].ssid, nla_data(attr),
5592 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005593 i++;
5594 }
5595 }
5596
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005597 i = 0;
5598 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5599 nla_for_each_nested(attr,
5600 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5601 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005602 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005603
5604 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5605 nla_data(attr), nla_len(attr),
5606 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005607 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005608 if (ssid) {
5609 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5610 err = -EINVAL;
5611 goto out_free;
5612 }
5613 memcpy(request->match_sets[i].ssid.ssid,
5614 nla_data(ssid), nla_len(ssid));
5615 request->match_sets[i].ssid.ssid_len =
5616 nla_len(ssid);
5617 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005618 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5619 if (rssi)
5620 request->rssi_thold = nla_get_u32(rssi);
5621 else
5622 request->rssi_thold =
5623 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005624 i++;
5625 }
5626 }
5627
Luciano Coelho807f8a82011-05-11 17:09:35 +03005628 if (info->attrs[NL80211_ATTR_IE]) {
5629 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5630 memcpy((void *)request->ie,
5631 nla_data(info->attrs[NL80211_ATTR_IE]),
5632 request->ie_len);
5633 }
5634
Sam Leffler46856bb2012-10-11 21:03:32 -07005635 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005636 request->flags = nla_get_u32(
5637 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005638 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5639 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005640 err = -EOPNOTSUPP;
5641 goto out_free;
5642 }
5643 }
Sam Lefflered4737712012-10-11 21:03:31 -07005644
Luciano Coelho807f8a82011-05-11 17:09:35 +03005645 request->dev = dev;
5646 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005647 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005648 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005649
Hila Gonene35e4d22012-06-27 17:19:42 +03005650 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005651 if (!err) {
5652 rdev->sched_scan_req = request;
5653 nl80211_send_sched_scan(rdev, dev,
5654 NL80211_CMD_START_SCHED_SCAN);
5655 goto out;
5656 }
5657
5658out_free:
5659 kfree(request);
5660out:
5661 return err;
5662}
5663
5664static int nl80211_stop_sched_scan(struct sk_buff *skb,
5665 struct genl_info *info)
5666{
5667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5668
5669 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5670 !rdev->ops->sched_scan_stop)
5671 return -EOPNOTSUPP;
5672
Johannes Berg5fe231e2013-05-08 21:45:15 +02005673 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005674}
5675
Simon Wunderlich04f39042013-02-08 18:16:19 +01005676static int nl80211_start_radar_detection(struct sk_buff *skb,
5677 struct genl_info *info)
5678{
5679 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5680 struct net_device *dev = info->user_ptr[1];
5681 struct wireless_dev *wdev = dev->ieee80211_ptr;
5682 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005683 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005684 int err;
5685
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005686 dfs_region = reg_get_dfs_region(wdev->wiphy);
5687 if (dfs_region == NL80211_DFS_UNSET)
5688 return -EINVAL;
5689
Simon Wunderlich04f39042013-02-08 18:16:19 +01005690 err = nl80211_parse_chandef(rdev, info, &chandef);
5691 if (err)
5692 return err;
5693
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005694 if (netif_carrier_ok(dev))
5695 return -EBUSY;
5696
Simon Wunderlich04f39042013-02-08 18:16:19 +01005697 if (wdev->cac_started)
5698 return -EBUSY;
5699
5700 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5701 if (err < 0)
5702 return err;
5703
5704 if (err == 0)
5705 return -EINVAL;
5706
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005707 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005708 return -EINVAL;
5709
5710 if (!rdev->ops->start_radar_detection)
5711 return -EOPNOTSUPP;
5712
Simon Wunderlich04f39042013-02-08 18:16:19 +01005713 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5714 chandef.chan, CHAN_MODE_SHARED,
5715 BIT(chandef.width));
5716 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005717 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005718
5719 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5720 if (!err) {
5721 wdev->channel = chandef.chan;
5722 wdev->cac_started = true;
5723 wdev->cac_start_time = jiffies;
5724 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005725 return err;
5726}
5727
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005728static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5729{
5730 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5731 struct net_device *dev = info->user_ptr[1];
5732 struct wireless_dev *wdev = dev->ieee80211_ptr;
5733 struct cfg80211_csa_settings params;
5734 /* csa_attrs is defined static to avoid waste of stack size - this
5735 * function is called under RTNL lock, so this should not be a problem.
5736 */
5737 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5738 u8 radar_detect_width = 0;
5739 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005740 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005741
5742 if (!rdev->ops->channel_switch ||
5743 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5744 return -EOPNOTSUPP;
5745
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005746 switch (dev->ieee80211_ptr->iftype) {
5747 case NL80211_IFTYPE_AP:
5748 case NL80211_IFTYPE_P2P_GO:
5749 need_new_beacon = true;
5750
5751 /* useless if AP is not running */
5752 if (!wdev->beacon_interval)
5753 return -EINVAL;
5754 break;
5755 case NL80211_IFTYPE_ADHOC:
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005756 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005757 break;
5758 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005759 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005760 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005761
5762 memset(&params, 0, sizeof(params));
5763
5764 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5765 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5766 return -EINVAL;
5767
5768 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005769 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005770 return -EINVAL;
5771
5772 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5773
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005774 if (!need_new_beacon)
5775 goto skip_beacons;
5776
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005777 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5778 if (err)
5779 return err;
5780
5781 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5782 info->attrs[NL80211_ATTR_CSA_IES],
5783 nl80211_policy);
5784 if (err)
5785 return err;
5786
5787 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5788 if (err)
5789 return err;
5790
5791 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5792 return -EINVAL;
5793
5794 params.counter_offset_beacon =
5795 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5796 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5797 return -EINVAL;
5798
5799 /* sanity check - counters should be the same */
5800 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5801 params.count)
5802 return -EINVAL;
5803
5804 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5805 params.counter_offset_presp =
5806 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5807 if (params.counter_offset_presp >=
5808 params.beacon_csa.probe_resp_len)
5809 return -EINVAL;
5810
5811 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5812 params.count)
5813 return -EINVAL;
5814 }
5815
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005816skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005817 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5818 if (err)
5819 return err;
5820
5821 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5822 return -EINVAL;
5823
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005824 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005825 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5826 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005827 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5828 &params.chandef);
5829 if (err < 0) {
5830 return err;
5831 } else if (err) {
5832 radar_detect_width = BIT(params.chandef.width);
5833 params.radar_required = true;
5834 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005835 }
5836
5837 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5838 params.chandef.chan,
5839 CHAN_MODE_SHARED,
5840 radar_detect_width);
5841 if (err)
5842 return err;
5843
5844 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5845 params.block_tx = true;
5846
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005847 wdev_lock(wdev);
5848 err = rdev_channel_switch(rdev, dev, &params);
5849 wdev_unlock(wdev);
5850
5851 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005852}
5853
Johannes Berg9720bb32011-06-21 09:45:33 +02005854static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5855 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005856 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005857 struct wireless_dev *wdev,
5858 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005859{
Johannes Berg48ab9052009-07-10 18:42:31 +02005860 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005861 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005862 void *hdr;
5863 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005864 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005865
5866 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005867
Eric W. Biederman15e47302012-09-07 20:12:54 +00005868 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005869 NL80211_CMD_NEW_SCAN_RESULTS);
5870 if (!hdr)
5871 return -1;
5872
Johannes Berg9720bb32011-06-21 09:45:33 +02005873 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5874
Johannes Berg97990a02013-04-19 01:02:55 +02005875 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5876 goto nla_put_failure;
5877 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005878 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5879 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005880 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5881 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005882
5883 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5884 if (!bss)
5885 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005886 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005887 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005888 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005889
5890 rcu_read_lock();
5891 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005892 if (ies) {
5893 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5894 goto fail_unlock_rcu;
5895 tsf = true;
5896 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5897 ies->len, ies->data))
5898 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005899 }
5900 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005901 if (ies) {
5902 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5903 goto fail_unlock_rcu;
5904 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5905 ies->len, ies->data))
5906 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005907 }
5908 rcu_read_unlock();
5909
David S. Miller9360ffd2012-03-29 04:41:26 -04005910 if (res->beacon_interval &&
5911 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5912 goto nla_put_failure;
5913 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5914 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02005915 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005916 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5917 jiffies_to_msecs(jiffies - intbss->ts)))
5918 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005919
Johannes Berg77965c92009-02-18 18:45:06 +01005920 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005921 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005922 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5923 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005924 break;
5925 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005926 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5927 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005928 break;
5929 default:
5930 break;
5931 }
5932
Johannes Berg48ab9052009-07-10 18:42:31 +02005933 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005934 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005935 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005936 if (intbss == wdev->current_bss &&
5937 nla_put_u32(msg, NL80211_BSS_STATUS,
5938 NL80211_BSS_STATUS_ASSOCIATED))
5939 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005940 break;
5941 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005942 if (intbss == wdev->current_bss &&
5943 nla_put_u32(msg, NL80211_BSS_STATUS,
5944 NL80211_BSS_STATUS_IBSS_JOINED))
5945 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005946 break;
5947 default:
5948 break;
5949 }
5950
Johannes Berg2a519312009-02-10 21:25:55 +01005951 nla_nest_end(msg, bss);
5952
5953 return genlmsg_end(msg, hdr);
5954
Johannes Berg8cef2c92013-02-05 16:54:31 +01005955 fail_unlock_rcu:
5956 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005957 nla_put_failure:
5958 genlmsg_cancel(msg, hdr);
5959 return -EMSGSIZE;
5960}
5961
Johannes Berg97990a02013-04-19 01:02:55 +02005962static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005963{
Johannes Berg48ab9052009-07-10 18:42:31 +02005964 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005965 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005966 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005967 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005968 int err;
5969
Johannes Berg97990a02013-04-19 01:02:55 +02005970 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005971 if (err)
5972 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005973
Johannes Berg48ab9052009-07-10 18:42:31 +02005974 wdev_lock(wdev);
5975 spin_lock_bh(&rdev->bss_lock);
5976 cfg80211_bss_expire(rdev);
5977
Johannes Berg9720bb32011-06-21 09:45:33 +02005978 cb->seq = rdev->bss_generation;
5979
Johannes Berg48ab9052009-07-10 18:42:31 +02005980 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005981 if (++idx <= start)
5982 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005983 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005984 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005985 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005986 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005987 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005988 }
5989 }
5990
Johannes Berg48ab9052009-07-10 18:42:31 +02005991 spin_unlock_bh(&rdev->bss_lock);
5992 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005993
Johannes Berg97990a02013-04-19 01:02:55 +02005994 cb->args[2] = idx;
5995 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005996
Johannes Berg67748892010-10-04 21:14:06 +02005997 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005998}
5999
Eric W. Biederman15e47302012-09-07 20:12:54 +00006000static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006001 int flags, struct net_device *dev,
6002 struct survey_info *survey)
6003{
6004 void *hdr;
6005 struct nlattr *infoattr;
6006
Eric W. Biederman15e47302012-09-07 20:12:54 +00006007 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006008 NL80211_CMD_NEW_SURVEY_RESULTS);
6009 if (!hdr)
6010 return -ENOMEM;
6011
David S. Miller9360ffd2012-03-29 04:41:26 -04006012 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6013 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006014
6015 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6016 if (!infoattr)
6017 goto nla_put_failure;
6018
David S. Miller9360ffd2012-03-29 04:41:26 -04006019 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6020 survey->channel->center_freq))
6021 goto nla_put_failure;
6022
6023 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6024 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6025 goto nla_put_failure;
6026 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6027 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6028 goto nla_put_failure;
6029 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6030 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6031 survey->channel_time))
6032 goto nla_put_failure;
6033 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6034 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6035 survey->channel_time_busy))
6036 goto nla_put_failure;
6037 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6038 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6039 survey->channel_time_ext_busy))
6040 goto nla_put_failure;
6041 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6042 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6043 survey->channel_time_rx))
6044 goto nla_put_failure;
6045 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6046 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6047 survey->channel_time_tx))
6048 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006049
6050 nla_nest_end(msg, infoattr);
6051
6052 return genlmsg_end(msg, hdr);
6053
6054 nla_put_failure:
6055 genlmsg_cancel(msg, hdr);
6056 return -EMSGSIZE;
6057}
6058
6059static int nl80211_dump_survey(struct sk_buff *skb,
6060 struct netlink_callback *cb)
6061{
6062 struct survey_info survey;
6063 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006064 struct wireless_dev *wdev;
6065 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006066 int res;
6067
Johannes Berg97990a02013-04-19 01:02:55 +02006068 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006069 if (res)
6070 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006071
Johannes Berg97990a02013-04-19 01:02:55 +02006072 if (!wdev->netdev) {
6073 res = -EINVAL;
6074 goto out_err;
6075 }
6076
Holger Schurig61fa7132009-11-11 12:25:40 +01006077 if (!dev->ops->dump_survey) {
6078 res = -EOPNOTSUPP;
6079 goto out_err;
6080 }
6081
6082 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006083 struct ieee80211_channel *chan;
6084
Johannes Berg97990a02013-04-19 01:02:55 +02006085 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006086 if (res == -ENOENT)
6087 break;
6088 if (res)
6089 goto out_err;
6090
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006091 /* Survey without a channel doesn't make sense */
6092 if (!survey.channel) {
6093 res = -EINVAL;
6094 goto out;
6095 }
6096
6097 chan = ieee80211_get_channel(&dev->wiphy,
6098 survey.channel->center_freq);
6099 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6100 survey_idx++;
6101 continue;
6102 }
6103
Holger Schurig61fa7132009-11-11 12:25:40 +01006104 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006105 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006106 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006107 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006108 goto out;
6109 survey_idx++;
6110 }
6111
6112 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006113 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006114 res = skb->len;
6115 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006116 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006117 return res;
6118}
6119
Samuel Ortizb23aa672009-07-01 21:26:54 +02006120static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6121{
6122 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6123 NL80211_WPA_VERSION_2));
6124}
6125
Jouni Malinen636a5d32009-03-19 13:39:22 +02006126static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6127{
Johannes Berg4c476992010-10-04 21:36:35 +02006128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6129 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006130 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006131 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6132 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006133 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006134 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006135 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006136
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006137 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6138 return -EINVAL;
6139
6140 if (!info->attrs[NL80211_ATTR_MAC])
6141 return -EINVAL;
6142
Jouni Malinen17780922009-03-27 20:52:47 +02006143 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6144 return -EINVAL;
6145
Johannes Berg19957bb2009-07-02 17:20:43 +02006146 if (!info->attrs[NL80211_ATTR_SSID])
6147 return -EINVAL;
6148
6149 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6150 return -EINVAL;
6151
Johannes Bergfffd0932009-07-08 14:22:54 +02006152 err = nl80211_parse_key(info, &key);
6153 if (err)
6154 return err;
6155
6156 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006157 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6158 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006159 if (!key.p.key || !key.p.key_len)
6160 return -EINVAL;
6161 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6162 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6163 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6164 key.p.key_len != WLAN_KEY_LEN_WEP104))
6165 return -EINVAL;
6166 if (key.idx > 4)
6167 return -EINVAL;
6168 } else {
6169 key.p.key_len = 0;
6170 key.p.key = NULL;
6171 }
6172
Johannes Bergafea0b72010-08-10 09:46:42 +02006173 if (key.idx >= 0) {
6174 int i;
6175 bool ok = false;
6176 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6177 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6178 ok = true;
6179 break;
6180 }
6181 }
Johannes Berg4c476992010-10-04 21:36:35 +02006182 if (!ok)
6183 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006184 }
6185
Johannes Berg4c476992010-10-04 21:36:35 +02006186 if (!rdev->ops->auth)
6187 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006188
Johannes Berg074ac8d2010-09-16 14:58:22 +02006189 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006190 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6191 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006192
Johannes Berg19957bb2009-07-02 17:20:43 +02006193 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02006194 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02006195 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006196 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6197 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006198
Johannes Berg19957bb2009-07-02 17:20:43 +02006199 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6200 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6201
6202 if (info->attrs[NL80211_ATTR_IE]) {
6203 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6204 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6205 }
6206
6207 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006208 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006209 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006210
Jouni Malinene39e5b52012-09-30 19:29:39 +03006211 if (auth_type == NL80211_AUTHTYPE_SAE &&
6212 !info->attrs[NL80211_ATTR_SAE_DATA])
6213 return -EINVAL;
6214
6215 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6216 if (auth_type != NL80211_AUTHTYPE_SAE)
6217 return -EINVAL;
6218 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6219 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6220 /* need to include at least Auth Transaction and Status Code */
6221 if (sae_data_len < 4)
6222 return -EINVAL;
6223 }
6224
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006225 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6226
Johannes Berg95de8172012-01-20 13:55:25 +01006227 /*
6228 * Since we no longer track auth state, ignore
6229 * requests to only change local state.
6230 */
6231 if (local_state_change)
6232 return 0;
6233
Johannes Berg91bf9b22013-05-15 17:44:01 +02006234 wdev_lock(dev->ieee80211_ptr);
6235 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6236 ssid, ssid_len, ie, ie_len,
6237 key.p.key, key.p.key_len, key.idx,
6238 sae_data, sae_data_len);
6239 wdev_unlock(dev->ieee80211_ptr);
6240 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006241}
6242
Johannes Bergc0692b82010-08-27 14:26:53 +03006243static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6244 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006245 struct cfg80211_crypto_settings *settings,
6246 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006247{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006248 memset(settings, 0, sizeof(*settings));
6249
Samuel Ortizb23aa672009-07-01 21:26:54 +02006250 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6251
Johannes Bergc0692b82010-08-27 14:26:53 +03006252 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6253 u16 proto;
6254 proto = nla_get_u16(
6255 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6256 settings->control_port_ethertype = cpu_to_be16(proto);
6257 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6258 proto != ETH_P_PAE)
6259 return -EINVAL;
6260 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6261 settings->control_port_no_encrypt = true;
6262 } else
6263 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6264
Samuel Ortizb23aa672009-07-01 21:26:54 +02006265 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6266 void *data;
6267 int len, i;
6268
6269 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6270 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6271 settings->n_ciphers_pairwise = len / sizeof(u32);
6272
6273 if (len % sizeof(u32))
6274 return -EINVAL;
6275
Johannes Berg3dc27d22009-07-02 21:36:37 +02006276 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006277 return -EINVAL;
6278
6279 memcpy(settings->ciphers_pairwise, data, len);
6280
6281 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006282 if (!cfg80211_supported_cipher_suite(
6283 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006284 settings->ciphers_pairwise[i]))
6285 return -EINVAL;
6286 }
6287
6288 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6289 settings->cipher_group =
6290 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006291 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6292 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006293 return -EINVAL;
6294 }
6295
6296 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6297 settings->wpa_versions =
6298 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6299 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6300 return -EINVAL;
6301 }
6302
6303 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6304 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006305 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006306
6307 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6308 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6309 settings->n_akm_suites = len / sizeof(u32);
6310
6311 if (len % sizeof(u32))
6312 return -EINVAL;
6313
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006314 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6315 return -EINVAL;
6316
Samuel Ortizb23aa672009-07-01 21:26:54 +02006317 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006318 }
6319
6320 return 0;
6321}
6322
Jouni Malinen636a5d32009-03-19 13:39:22 +02006323static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6324{
Johannes Berg4c476992010-10-04 21:36:35 +02006325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6326 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006327 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006328 struct cfg80211_assoc_request req = {};
6329 const u8 *bssid, *ssid;
6330 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006331
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006332 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6333 return -EINVAL;
6334
6335 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006336 !info->attrs[NL80211_ATTR_SSID] ||
6337 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006338 return -EINVAL;
6339
Johannes Berg4c476992010-10-04 21:36:35 +02006340 if (!rdev->ops->assoc)
6341 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006342
Johannes Berg074ac8d2010-09-16 14:58:22 +02006343 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006344 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6345 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006346
Johannes Berg19957bb2009-07-02 17:20:43 +02006347 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006348
Johannes Berg19957bb2009-07-02 17:20:43 +02006349 chan = ieee80211_get_channel(&rdev->wiphy,
6350 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006351 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6352 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006353
Johannes Berg19957bb2009-07-02 17:20:43 +02006354 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6355 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006356
6357 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006358 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6359 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006360 }
6361
Jouni Malinendc6382c2009-05-06 22:09:37 +03006362 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006363 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006364 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006365 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006366 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006367 else if (mfp != NL80211_MFP_NO)
6368 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006369 }
6370
Johannes Berg3e5d7642009-07-07 14:37:26 +02006371 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006372 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006373
Ben Greear7e7c8922011-11-18 11:31:59 -08006374 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006375 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006376
6377 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006378 memcpy(&req.ht_capa_mask,
6379 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6380 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006381
6382 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006383 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006384 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006385 memcpy(&req.ht_capa,
6386 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6387 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006388 }
6389
Johannes Bergee2aca32013-02-21 17:36:01 +01006390 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006391 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006392
6393 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006394 memcpy(&req.vht_capa_mask,
6395 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6396 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006397
6398 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006399 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006400 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006401 memcpy(&req.vht_capa,
6402 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6403 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006404 }
6405
Johannes Bergf62fab72013-02-21 20:09:09 +01006406 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006407 if (!err) {
6408 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006409 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6410 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006411 wdev_unlock(dev->ieee80211_ptr);
6412 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006413
Jouni Malinen636a5d32009-03-19 13:39:22 +02006414 return err;
6415}
6416
6417static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6418{
Johannes Berg4c476992010-10-04 21:36:35 +02006419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6420 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006421 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006422 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006423 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006424 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006425
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006426 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6427 return -EINVAL;
6428
6429 if (!info->attrs[NL80211_ATTR_MAC])
6430 return -EINVAL;
6431
6432 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6433 return -EINVAL;
6434
Johannes Berg4c476992010-10-04 21:36:35 +02006435 if (!rdev->ops->deauth)
6436 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006437
Johannes Berg074ac8d2010-09-16 14:58:22 +02006438 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006439 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6440 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006441
Johannes Berg19957bb2009-07-02 17:20:43 +02006442 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006443
Johannes Berg19957bb2009-07-02 17:20:43 +02006444 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6445 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006446 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006447 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006448 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006449
6450 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006451 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6452 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006453 }
6454
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006455 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6456
Johannes Berg91bf9b22013-05-15 17:44:01 +02006457 wdev_lock(dev->ieee80211_ptr);
6458 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6459 local_state_change);
6460 wdev_unlock(dev->ieee80211_ptr);
6461 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006462}
6463
6464static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6465{
Johannes Berg4c476992010-10-04 21:36:35 +02006466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6467 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006468 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006469 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006470 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006471 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006472
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006473 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6474 return -EINVAL;
6475
6476 if (!info->attrs[NL80211_ATTR_MAC])
6477 return -EINVAL;
6478
6479 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6480 return -EINVAL;
6481
Johannes Berg4c476992010-10-04 21:36:35 +02006482 if (!rdev->ops->disassoc)
6483 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006484
Johannes Berg074ac8d2010-09-16 14:58:22 +02006485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006486 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6487 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006488
Johannes Berg19957bb2009-07-02 17:20:43 +02006489 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006490
Johannes Berg19957bb2009-07-02 17:20:43 +02006491 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6492 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006493 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006494 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006495 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006496
6497 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006498 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6499 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006500 }
6501
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006502 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6503
Johannes Berg91bf9b22013-05-15 17:44:01 +02006504 wdev_lock(dev->ieee80211_ptr);
6505 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6506 local_state_change);
6507 wdev_unlock(dev->ieee80211_ptr);
6508 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006509}
6510
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006511static bool
6512nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6513 int mcast_rate[IEEE80211_NUM_BANDS],
6514 int rateval)
6515{
6516 struct wiphy *wiphy = &rdev->wiphy;
6517 bool found = false;
6518 int band, i;
6519
6520 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6521 struct ieee80211_supported_band *sband;
6522
6523 sband = wiphy->bands[band];
6524 if (!sband)
6525 continue;
6526
6527 for (i = 0; i < sband->n_bitrates; i++) {
6528 if (sband->bitrates[i].bitrate == rateval) {
6529 mcast_rate[band] = i + 1;
6530 found = true;
6531 break;
6532 }
6533 }
6534 }
6535
6536 return found;
6537}
6538
Johannes Berg04a773a2009-04-19 21:24:32 +02006539static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6540{
Johannes Berg4c476992010-10-04 21:36:35 +02006541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6542 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006543 struct cfg80211_ibss_params ibss;
6544 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006545 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006546 int err;
6547
Johannes Berg8e30bc52009-04-22 17:45:38 +02006548 memset(&ibss, 0, sizeof(ibss));
6549
Johannes Berg04a773a2009-04-19 21:24:32 +02006550 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6551 return -EINVAL;
6552
Johannes Berg683b6d32012-11-08 21:25:48 +01006553 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006554 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6555 return -EINVAL;
6556
Johannes Berg8e30bc52009-04-22 17:45:38 +02006557 ibss.beacon_interval = 100;
6558
6559 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6560 ibss.beacon_interval =
6561 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6562 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6563 return -EINVAL;
6564 }
6565
Johannes Berg4c476992010-10-04 21:36:35 +02006566 if (!rdev->ops->join_ibss)
6567 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006568
Johannes Berg4c476992010-10-04 21:36:35 +02006569 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6570 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006571
Johannes Berg79c97e92009-07-07 03:56:12 +02006572 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006573
Johannes Berg39193492011-09-16 13:45:25 +02006574 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006575 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006576
6577 if (!is_valid_ether_addr(ibss.bssid))
6578 return -EINVAL;
6579 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006580 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6581 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6582
6583 if (info->attrs[NL80211_ATTR_IE]) {
6584 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6585 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6586 }
6587
Johannes Berg683b6d32012-11-08 21:25:48 +01006588 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6589 if (err)
6590 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006591
Johannes Berg683b6d32012-11-08 21:25:48 +01006592 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006593 return -EINVAL;
6594
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006595 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006596 case NL80211_CHAN_WIDTH_5:
6597 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006598 case NL80211_CHAN_WIDTH_20_NOHT:
6599 break;
6600 case NL80211_CHAN_WIDTH_20:
6601 case NL80211_CHAN_WIDTH_40:
6602 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6603 break;
6604 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006605 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006606 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006607
Johannes Berg04a773a2009-04-19 21:24:32 +02006608 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006609 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006610
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006611 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6612 u8 *rates =
6613 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6614 int n_rates =
6615 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6616 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006617 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006618
Johannes Berg34850ab2011-07-18 18:08:35 +02006619 err = ieee80211_get_ratemask(sband, rates, n_rates,
6620 &ibss.basic_rates);
6621 if (err)
6622 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006623 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006624
Simon Wunderlich803768f2013-06-28 10:39:58 +02006625 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6626 memcpy(&ibss.ht_capa_mask,
6627 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6628 sizeof(ibss.ht_capa_mask));
6629
6630 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6631 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6632 return -EINVAL;
6633 memcpy(&ibss.ht_capa,
6634 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6635 sizeof(ibss.ht_capa));
6636 }
6637
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006638 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6639 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6640 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6641 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006642
Johannes Berg4c476992010-10-04 21:36:35 +02006643 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306644 bool no_ht = false;
6645
Johannes Berg4c476992010-10-04 21:36:35 +02006646 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306647 info->attrs[NL80211_ATTR_KEYS],
6648 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006649 if (IS_ERR(connkeys))
6650 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306651
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006652 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6653 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306654 kfree(connkeys);
6655 return -EINVAL;
6656 }
Johannes Berg4c476992010-10-04 21:36:35 +02006657 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006658
Antonio Quartulli267335d2012-01-31 20:25:47 +01006659 ibss.control_port =
6660 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6661
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006662 ibss.userspace_handles_dfs =
6663 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6664
Johannes Berg4c476992010-10-04 21:36:35 +02006665 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006666 if (err)
6667 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006668 return err;
6669}
6670
6671static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6672{
Johannes Berg4c476992010-10-04 21:36:35 +02006673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6674 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006675
Johannes Berg4c476992010-10-04 21:36:35 +02006676 if (!rdev->ops->leave_ibss)
6677 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006678
Johannes Berg4c476992010-10-04 21:36:35 +02006679 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6680 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006681
Johannes Berg4c476992010-10-04 21:36:35 +02006682 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006683}
6684
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006685static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6686{
6687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6688 struct net_device *dev = info->user_ptr[1];
6689 int mcast_rate[IEEE80211_NUM_BANDS];
6690 u32 nla_rate;
6691 int err;
6692
6693 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6694 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6695 return -EOPNOTSUPP;
6696
6697 if (!rdev->ops->set_mcast_rate)
6698 return -EOPNOTSUPP;
6699
6700 memset(mcast_rate, 0, sizeof(mcast_rate));
6701
6702 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6703 return -EINVAL;
6704
6705 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6706 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6707 return -EINVAL;
6708
6709 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6710
6711 return err;
6712}
6713
Johannes Bergad7e7182013-11-13 13:37:47 +01006714static struct sk_buff *
6715__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6716 int approxlen, u32 portid, u32 seq,
6717 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006718 enum nl80211_attrs attr,
6719 const struct nl80211_vendor_cmd_info *info,
6720 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006721{
6722 struct sk_buff *skb;
6723 void *hdr;
6724 struct nlattr *data;
6725
6726 skb = nlmsg_new(approxlen + 100, gfp);
6727 if (!skb)
6728 return NULL;
6729
6730 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6731 if (!hdr) {
6732 kfree_skb(skb);
6733 return NULL;
6734 }
6735
6736 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6737 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006738
6739 if (info) {
6740 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6741 info->vendor_id))
6742 goto nla_put_failure;
6743 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6744 info->subcmd))
6745 goto nla_put_failure;
6746 }
6747
Johannes Bergad7e7182013-11-13 13:37:47 +01006748 data = nla_nest_start(skb, attr);
6749
6750 ((void **)skb->cb)[0] = rdev;
6751 ((void **)skb->cb)[1] = hdr;
6752 ((void **)skb->cb)[2] = data;
6753
6754 return skb;
6755
6756 nla_put_failure:
6757 kfree_skb(skb);
6758 return NULL;
6759}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006760
Johannes Berge03ad6e2014-01-01 17:22:30 +01006761struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6762 enum nl80211_commands cmd,
6763 enum nl80211_attrs attr,
6764 int vendor_event_idx,
6765 int approxlen, gfp_t gfp)
6766{
6767 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6768 const struct nl80211_vendor_cmd_info *info;
6769
6770 switch (cmd) {
6771 case NL80211_CMD_TESTMODE:
6772 if (WARN_ON(vendor_event_idx != -1))
6773 return NULL;
6774 info = NULL;
6775 break;
6776 case NL80211_CMD_VENDOR:
6777 if (WARN_ON(vendor_event_idx < 0 ||
6778 vendor_event_idx >= wiphy->n_vendor_events))
6779 return NULL;
6780 info = &wiphy->vendor_events[vendor_event_idx];
6781 break;
6782 default:
6783 WARN_ON(1);
6784 return NULL;
6785 }
6786
6787 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6788 cmd, attr, info, gfp);
6789}
6790EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6791
6792void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6793{
6794 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6795 void *hdr = ((void **)skb->cb)[1];
6796 struct nlattr *data = ((void **)skb->cb)[2];
6797 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6798
6799 nla_nest_end(skb, data);
6800 genlmsg_end(skb, hdr);
6801
6802 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6803 mcgrp = NL80211_MCGRP_VENDOR;
6804
6805 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6806 mcgrp, gfp);
6807}
6808EXPORT_SYMBOL(__cfg80211_send_event_skb);
6809
Johannes Bergaff89a92009-07-01 21:26:51 +02006810#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006811static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6812{
Johannes Berg4c476992010-10-04 21:36:35 +02006813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006814 struct wireless_dev *wdev =
6815 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006816 int err;
6817
David Spinadelfc73f112013-07-31 18:04:15 +03006818 if (!rdev->ops->testmode_cmd)
6819 return -EOPNOTSUPP;
6820
6821 if (IS_ERR(wdev)) {
6822 err = PTR_ERR(wdev);
6823 if (err != -EINVAL)
6824 return err;
6825 wdev = NULL;
6826 } else if (wdev->wiphy != &rdev->wiphy) {
6827 return -EINVAL;
6828 }
6829
Johannes Bergaff89a92009-07-01 21:26:51 +02006830 if (!info->attrs[NL80211_ATTR_TESTDATA])
6831 return -EINVAL;
6832
Johannes Bergad7e7182013-11-13 13:37:47 +01006833 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006834 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006835 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6836 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006837 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006838
Johannes Bergaff89a92009-07-01 21:26:51 +02006839 return err;
6840}
6841
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006842static int nl80211_testmode_dump(struct sk_buff *skb,
6843 struct netlink_callback *cb)
6844{
Johannes Berg00918d32011-12-13 17:22:05 +01006845 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006846 int err;
6847 long phy_idx;
6848 void *data = NULL;
6849 int data_len = 0;
6850
Johannes Berg5fe231e2013-05-08 21:45:15 +02006851 rtnl_lock();
6852
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006853 if (cb->args[0]) {
6854 /*
6855 * 0 is a valid index, but not valid for args[0],
6856 * so we need to offset by 1.
6857 */
6858 phy_idx = cb->args[0] - 1;
6859 } else {
6860 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6861 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6862 nl80211_policy);
6863 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006864 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006865
Johannes Berg2bd7e352012-06-15 14:23:16 +02006866 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6867 nl80211_fam.attrbuf);
6868 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006869 err = PTR_ERR(rdev);
6870 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006871 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006872 phy_idx = rdev->wiphy_idx;
6873 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006874
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006875 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6876 cb->args[1] =
6877 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6878 }
6879
6880 if (cb->args[1]) {
6881 data = nla_data((void *)cb->args[1]);
6882 data_len = nla_len((void *)cb->args[1]);
6883 }
6884
Johannes Berg00918d32011-12-13 17:22:05 +01006885 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6886 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006887 err = -ENOENT;
6888 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006889 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006890
Johannes Berg00918d32011-12-13 17:22:05 +01006891 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006892 err = -EOPNOTSUPP;
6893 goto out_err;
6894 }
6895
6896 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006897 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006898 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6899 NL80211_CMD_TESTMODE);
6900 struct nlattr *tmdata;
6901
Dan Carpentercb35fba2013-08-14 14:50:01 +03006902 if (!hdr)
6903 break;
6904
David S. Miller9360ffd2012-03-29 04:41:26 -04006905 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006906 genlmsg_cancel(skb, hdr);
6907 break;
6908 }
6909
6910 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6911 if (!tmdata) {
6912 genlmsg_cancel(skb, hdr);
6913 break;
6914 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006915 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006916 nla_nest_end(skb, tmdata);
6917
6918 if (err == -ENOBUFS || err == -ENOENT) {
6919 genlmsg_cancel(skb, hdr);
6920 break;
6921 } else if (err) {
6922 genlmsg_cancel(skb, hdr);
6923 goto out_err;
6924 }
6925
6926 genlmsg_end(skb, hdr);
6927 }
6928
6929 err = skb->len;
6930 /* see above */
6931 cb->args[0] = phy_idx + 1;
6932 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02006933 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006934 return err;
6935}
Johannes Bergaff89a92009-07-01 21:26:51 +02006936#endif
6937
Samuel Ortizb23aa672009-07-01 21:26:54 +02006938static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6939{
Johannes Berg4c476992010-10-04 21:36:35 +02006940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6941 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006942 struct cfg80211_connect_params connect;
6943 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006944 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006945 int err;
6946
6947 memset(&connect, 0, sizeof(connect));
6948
6949 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6950 return -EINVAL;
6951
6952 if (!info->attrs[NL80211_ATTR_SSID] ||
6953 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6954 return -EINVAL;
6955
6956 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6957 connect.auth_type =
6958 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006959 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6960 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006961 return -EINVAL;
6962 } else
6963 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6964
6965 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6966
Johannes Bergc0692b82010-08-27 14:26:53 +03006967 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006968 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006969 if (err)
6970 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006971
Johannes Berg074ac8d2010-09-16 14:58:22 +02006972 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006973 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6974 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006975
Johannes Berg79c97e92009-07-07 03:56:12 +02006976 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006977
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306978 connect.bg_scan_period = -1;
6979 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6980 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6981 connect.bg_scan_period =
6982 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6983 }
6984
Samuel Ortizb23aa672009-07-01 21:26:54 +02006985 if (info->attrs[NL80211_ATTR_MAC])
6986 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6987 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6988 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6989
6990 if (info->attrs[NL80211_ATTR_IE]) {
6991 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6992 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6993 }
6994
Jouni Malinencee00a92013-01-15 17:15:57 +02006995 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6996 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6997 if (connect.mfp != NL80211_MFP_REQUIRED &&
6998 connect.mfp != NL80211_MFP_NO)
6999 return -EINVAL;
7000 } else {
7001 connect.mfp = NL80211_MFP_NO;
7002 }
7003
Samuel Ortizb23aa672009-07-01 21:26:54 +02007004 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7005 connect.channel =
7006 ieee80211_get_channel(wiphy,
7007 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
7008 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02007009 connect.channel->flags & IEEE80211_CHAN_DISABLED)
7010 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007011 }
7012
Johannes Bergfffd0932009-07-08 14:22:54 +02007013 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7014 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307015 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007016 if (IS_ERR(connkeys))
7017 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007018 }
7019
Ben Greear7e7c8922011-11-18 11:31:59 -08007020 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7021 connect.flags |= ASSOC_REQ_DISABLE_HT;
7022
7023 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7024 memcpy(&connect.ht_capa_mask,
7025 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7026 sizeof(connect.ht_capa_mask));
7027
7028 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007029 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7030 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007031 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007032 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007033 memcpy(&connect.ht_capa,
7034 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7035 sizeof(connect.ht_capa));
7036 }
7037
Johannes Bergee2aca32013-02-21 17:36:01 +01007038 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7039 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7040
7041 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7042 memcpy(&connect.vht_capa_mask,
7043 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7044 sizeof(connect.vht_capa_mask));
7045
7046 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7047 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7048 kfree(connkeys);
7049 return -EINVAL;
7050 }
7051 memcpy(&connect.vht_capa,
7052 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7053 sizeof(connect.vht_capa));
7054 }
7055
Johannes Berg83739b02013-05-15 17:44:01 +02007056 wdev_lock(dev->ieee80211_ptr);
7057 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7058 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007059 if (err)
7060 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007061 return err;
7062}
7063
7064static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7065{
Johannes Berg4c476992010-10-04 21:36:35 +02007066 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7067 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007068 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007069 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007070
7071 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7072 reason = WLAN_REASON_DEAUTH_LEAVING;
7073 else
7074 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7075
7076 if (reason == 0)
7077 return -EINVAL;
7078
Johannes Berg074ac8d2010-09-16 14:58:22 +02007079 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007080 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7081 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007082
Johannes Berg83739b02013-05-15 17:44:01 +02007083 wdev_lock(dev->ieee80211_ptr);
7084 ret = cfg80211_disconnect(rdev, dev, reason, true);
7085 wdev_unlock(dev->ieee80211_ptr);
7086 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007087}
7088
Johannes Berg463d0182009-07-14 00:33:35 +02007089static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7090{
Johannes Berg4c476992010-10-04 21:36:35 +02007091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007092 struct net *net;
7093 int err;
7094 u32 pid;
7095
7096 if (!info->attrs[NL80211_ATTR_PID])
7097 return -EINVAL;
7098
7099 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7100
Johannes Berg463d0182009-07-14 00:33:35 +02007101 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007102 if (IS_ERR(net))
7103 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007104
7105 err = 0;
7106
7107 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007108 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7109 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007110
Johannes Berg463d0182009-07-14 00:33:35 +02007111 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007112 return err;
7113}
7114
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007115static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7116{
Johannes Berg4c476992010-10-04 21:36:35 +02007117 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007118 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7119 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007120 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007121 struct cfg80211_pmksa pmksa;
7122
7123 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7124
7125 if (!info->attrs[NL80211_ATTR_MAC])
7126 return -EINVAL;
7127
7128 if (!info->attrs[NL80211_ATTR_PMKID])
7129 return -EINVAL;
7130
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007131 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7132 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7133
Johannes Berg074ac8d2010-09-16 14:58:22 +02007134 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007135 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7136 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007137
7138 switch (info->genlhdr->cmd) {
7139 case NL80211_CMD_SET_PMKSA:
7140 rdev_ops = rdev->ops->set_pmksa;
7141 break;
7142 case NL80211_CMD_DEL_PMKSA:
7143 rdev_ops = rdev->ops->del_pmksa;
7144 break;
7145 default:
7146 WARN_ON(1);
7147 break;
7148 }
7149
Johannes Berg4c476992010-10-04 21:36:35 +02007150 if (!rdev_ops)
7151 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007152
Johannes Berg4c476992010-10-04 21:36:35 +02007153 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007154}
7155
7156static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7157{
Johannes Berg4c476992010-10-04 21:36:35 +02007158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7159 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007160
Johannes Berg074ac8d2010-09-16 14:58:22 +02007161 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007162 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7163 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007164
Johannes Berg4c476992010-10-04 21:36:35 +02007165 if (!rdev->ops->flush_pmksa)
7166 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007167
Hila Gonene35e4d22012-06-27 17:19:42 +03007168 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007169}
7170
Arik Nemtsov109086c2011-09-28 14:12:50 +03007171static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7172{
7173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7174 struct net_device *dev = info->user_ptr[1];
7175 u8 action_code, dialog_token;
7176 u16 status_code;
7177 u8 *peer;
7178
7179 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7180 !rdev->ops->tdls_mgmt)
7181 return -EOPNOTSUPP;
7182
7183 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7184 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7185 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7186 !info->attrs[NL80211_ATTR_IE] ||
7187 !info->attrs[NL80211_ATTR_MAC])
7188 return -EINVAL;
7189
7190 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7191 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7192 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7193 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
7194
Hila Gonene35e4d22012-06-27 17:19:42 +03007195 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
7196 dialog_token, status_code,
7197 nla_data(info->attrs[NL80211_ATTR_IE]),
7198 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007199}
7200
7201static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7202{
7203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7204 struct net_device *dev = info->user_ptr[1];
7205 enum nl80211_tdls_operation operation;
7206 u8 *peer;
7207
7208 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7209 !rdev->ops->tdls_oper)
7210 return -EOPNOTSUPP;
7211
7212 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7213 !info->attrs[NL80211_ATTR_MAC])
7214 return -EINVAL;
7215
7216 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7217 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7218
Hila Gonene35e4d22012-06-27 17:19:42 +03007219 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007220}
7221
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007222static int nl80211_remain_on_channel(struct sk_buff *skb,
7223 struct genl_info *info)
7224{
Johannes Berg4c476992010-10-04 21:36:35 +02007225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007226 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007227 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007228 struct sk_buff *msg;
7229 void *hdr;
7230 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007231 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007232 int err;
7233
7234 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7235 !info->attrs[NL80211_ATTR_DURATION])
7236 return -EINVAL;
7237
7238 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7239
Johannes Berg7c4ef712011-11-18 15:33:48 +01007240 if (!rdev->ops->remain_on_channel ||
7241 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007242 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007243
Johannes Bergebf348f2012-06-01 12:50:54 +02007244 /*
7245 * We should be on that channel for at least a minimum amount of
7246 * time (10ms) but no longer than the driver supports.
7247 */
7248 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7249 duration > rdev->wiphy.max_remain_on_channel_duration)
7250 return -EINVAL;
7251
Johannes Berg683b6d32012-11-08 21:25:48 +01007252 err = nl80211_parse_chandef(rdev, info, &chandef);
7253 if (err)
7254 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007255
7256 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007257 if (!msg)
7258 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007259
Eric W. Biederman15e47302012-09-07 20:12:54 +00007260 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007261 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007262 if (!hdr) {
7263 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007264 goto free_msg;
7265 }
7266
Johannes Berg683b6d32012-11-08 21:25:48 +01007267 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7268 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007269
7270 if (err)
7271 goto free_msg;
7272
David S. Miller9360ffd2012-03-29 04:41:26 -04007273 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7274 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007275
7276 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007277
7278 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007279
7280 nla_put_failure:
7281 err = -ENOBUFS;
7282 free_msg:
7283 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007284 return err;
7285}
7286
7287static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7288 struct genl_info *info)
7289{
Johannes Berg4c476992010-10-04 21:36:35 +02007290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007291 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007292 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007293
7294 if (!info->attrs[NL80211_ATTR_COOKIE])
7295 return -EINVAL;
7296
Johannes Berg4c476992010-10-04 21:36:35 +02007297 if (!rdev->ops->cancel_remain_on_channel)
7298 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007299
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007300 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7301
Hila Gonene35e4d22012-06-27 17:19:42 +03007302 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007303}
7304
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007305static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7306 u8 *rates, u8 rates_len)
7307{
7308 u8 i;
7309 u32 mask = 0;
7310
7311 for (i = 0; i < rates_len; i++) {
7312 int rate = (rates[i] & 0x7f) * 5;
7313 int ridx;
7314 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7315 struct ieee80211_rate *srate =
7316 &sband->bitrates[ridx];
7317 if (rate == srate->bitrate) {
7318 mask |= 1 << ridx;
7319 break;
7320 }
7321 }
7322 if (ridx == sband->n_bitrates)
7323 return 0; /* rate not found */
7324 }
7325
7326 return mask;
7327}
7328
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007329static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7330 u8 *rates, u8 rates_len,
7331 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7332{
7333 u8 i;
7334
7335 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7336
7337 for (i = 0; i < rates_len; i++) {
7338 int ridx, rbit;
7339
7340 ridx = rates[i] / 8;
7341 rbit = BIT(rates[i] % 8);
7342
7343 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007344 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007345 return false;
7346
7347 /* check availability */
7348 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7349 mcs[ridx] |= rbit;
7350 else
7351 return false;
7352 }
7353
7354 return true;
7355}
7356
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007357static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7358{
7359 u16 mcs_mask = 0;
7360
7361 switch (vht_mcs_map) {
7362 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7363 break;
7364 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7365 mcs_mask = 0x00FF;
7366 break;
7367 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7368 mcs_mask = 0x01FF;
7369 break;
7370 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7371 mcs_mask = 0x03FF;
7372 break;
7373 default:
7374 break;
7375 }
7376
7377 return mcs_mask;
7378}
7379
7380static void vht_build_mcs_mask(u16 vht_mcs_map,
7381 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7382{
7383 u8 nss;
7384
7385 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7386 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7387 vht_mcs_map >>= 2;
7388 }
7389}
7390
7391static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7392 struct nl80211_txrate_vht *txrate,
7393 u16 mcs[NL80211_VHT_NSS_MAX])
7394{
7395 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7396 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7397 u8 i;
7398
7399 if (!sband->vht_cap.vht_supported)
7400 return false;
7401
7402 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7403
7404 /* Build vht_mcs_mask from VHT capabilities */
7405 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7406
7407 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7408 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7409 mcs[i] = txrate->mcs[i];
7410 else
7411 return false;
7412 }
7413
7414 return true;
7415}
7416
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007417static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007418 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7419 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007420 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7421 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007422 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007423};
7424
7425static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7426 struct genl_info *info)
7427{
7428 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007430 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007431 int rem, i;
7432 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007433 struct nlattr *tx_rates;
7434 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007435 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007436
Johannes Berg4c476992010-10-04 21:36:35 +02007437 if (!rdev->ops->set_bitrate_mask)
7438 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007439
7440 memset(&mask, 0, sizeof(mask));
7441 /* Default to all rates enabled */
7442 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7443 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007444
7445 if (!sband)
7446 continue;
7447
7448 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007449 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007450 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007451 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007452
7453 if (!sband->vht_cap.vht_supported)
7454 continue;
7455
7456 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7457 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007458 }
7459
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007460 /* if no rates are given set it back to the defaults */
7461 if (!info->attrs[NL80211_ATTR_TX_RATES])
7462 goto out;
7463
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007464 /*
7465 * The nested attribute uses enum nl80211_band as the index. This maps
7466 * directly to the enum ieee80211_band values used in cfg80211.
7467 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007468 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007469 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7470 {
7471 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007472 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7473 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007474 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007475 if (sband == NULL)
7476 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007477 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7478 nla_len(tx_rates), nl80211_txattr_policy);
7479 if (tb[NL80211_TXRATE_LEGACY]) {
7480 mask.control[band].legacy = rateset_to_mask(
7481 sband,
7482 nla_data(tb[NL80211_TXRATE_LEGACY]),
7483 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307484 if ((mask.control[band].legacy == 0) &&
7485 nla_len(tb[NL80211_TXRATE_LEGACY]))
7486 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007487 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007488 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007489 if (!ht_rateset_to_mask(
7490 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007491 nla_data(tb[NL80211_TXRATE_HT]),
7492 nla_len(tb[NL80211_TXRATE_HT]),
7493 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007494 return -EINVAL;
7495 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007496 if (tb[NL80211_TXRATE_VHT]) {
7497 if (!vht_set_mcs_mask(
7498 sband,
7499 nla_data(tb[NL80211_TXRATE_VHT]),
7500 mask.control[band].vht_mcs))
7501 return -EINVAL;
7502 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007503
7504 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007505 /* don't allow empty legacy rates if HT or VHT
7506 * are not even supported.
7507 */
7508 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7509 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007510 return -EINVAL;
7511
7512 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007513 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007514 goto out;
7515
7516 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7517 if (mask.control[band].vht_mcs[i])
7518 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007519
7520 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007521 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007522 }
7523 }
7524
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007525out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007526 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007527}
7528
Johannes Berg2e161f72010-08-12 15:38:38 +02007529static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007530{
Johannes Berg4c476992010-10-04 21:36:35 +02007531 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007532 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007533 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007534
7535 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7536 return -EINVAL;
7537
Johannes Berg2e161f72010-08-12 15:38:38 +02007538 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7539 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007540
Johannes Berg71bbc992012-06-15 15:30:18 +02007541 switch (wdev->iftype) {
7542 case NL80211_IFTYPE_STATION:
7543 case NL80211_IFTYPE_ADHOC:
7544 case NL80211_IFTYPE_P2P_CLIENT:
7545 case NL80211_IFTYPE_AP:
7546 case NL80211_IFTYPE_AP_VLAN:
7547 case NL80211_IFTYPE_MESH_POINT:
7548 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007549 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007550 break;
7551 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007552 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007553 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007554
7555 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007556 if (!rdev->ops->mgmt_tx)
7557 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007558
Eric W. Biederman15e47302012-09-07 20:12:54 +00007559 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007560 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7561 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007562}
7563
Johannes Berg2e161f72010-08-12 15:38:38 +02007564static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007565{
Johannes Berg4c476992010-10-04 21:36:35 +02007566 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007567 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007568 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007569 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007570 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007571 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007572 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007573 struct cfg80211_mgmt_tx_params params = {
7574 .dont_wait_for_ack =
7575 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7576 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007577
Johannes Berg683b6d32012-11-08 21:25:48 +01007578 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007579 return -EINVAL;
7580
Johannes Berg4c476992010-10-04 21:36:35 +02007581 if (!rdev->ops->mgmt_tx)
7582 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007583
Johannes Berg71bbc992012-06-15 15:30:18 +02007584 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007585 case NL80211_IFTYPE_P2P_DEVICE:
7586 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7587 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007588 case NL80211_IFTYPE_STATION:
7589 case NL80211_IFTYPE_ADHOC:
7590 case NL80211_IFTYPE_P2P_CLIENT:
7591 case NL80211_IFTYPE_AP:
7592 case NL80211_IFTYPE_AP_VLAN:
7593 case NL80211_IFTYPE_MESH_POINT:
7594 case NL80211_IFTYPE_P2P_GO:
7595 break;
7596 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007597 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007598 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007599
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007600 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007601 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007602 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007603 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007604
7605 /*
7606 * We should wait on the channel for at least a minimum amount
7607 * of time (10ms) but no longer than the driver supports.
7608 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007609 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7610 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007611 return -EINVAL;
7612
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007613 }
7614
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007615 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007616
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007617 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007618 return -EINVAL;
7619
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007620 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307621
Antonio Quartulliea141b752013-06-11 14:20:03 +02007622 /* get the channel if any has been specified, otherwise pass NULL to
7623 * the driver. The latter will use the current one
7624 */
7625 chandef.chan = NULL;
7626 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7627 err = nl80211_parse_chandef(rdev, info, &chandef);
7628 if (err)
7629 return err;
7630 }
7631
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007632 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007633 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007634
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007635 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007636 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7637 if (!msg)
7638 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007639
Eric W. Biederman15e47302012-09-07 20:12:54 +00007640 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007641 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007642 if (!hdr) {
7643 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007644 goto free_msg;
7645 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007646 }
Johannes Berge247bd902011-11-04 11:18:21 +01007647
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007648 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7649 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7650 params.chan = chandef.chan;
7651 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007652 if (err)
7653 goto free_msg;
7654
Johannes Berge247bd902011-11-04 11:18:21 +01007655 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007656 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7657 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007658
Johannes Berge247bd902011-11-04 11:18:21 +01007659 genlmsg_end(msg, hdr);
7660 return genlmsg_reply(msg, info);
7661 }
7662
7663 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007664
7665 nla_put_failure:
7666 err = -ENOBUFS;
7667 free_msg:
7668 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007669 return err;
7670}
7671
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007672static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7673{
7674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007675 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007676 u64 cookie;
7677
7678 if (!info->attrs[NL80211_ATTR_COOKIE])
7679 return -EINVAL;
7680
7681 if (!rdev->ops->mgmt_tx_cancel_wait)
7682 return -EOPNOTSUPP;
7683
Johannes Berg71bbc992012-06-15 15:30:18 +02007684 switch (wdev->iftype) {
7685 case NL80211_IFTYPE_STATION:
7686 case NL80211_IFTYPE_ADHOC:
7687 case NL80211_IFTYPE_P2P_CLIENT:
7688 case NL80211_IFTYPE_AP:
7689 case NL80211_IFTYPE_AP_VLAN:
7690 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007691 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007692 break;
7693 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007694 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007695 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007696
7697 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7698
Hila Gonene35e4d22012-06-27 17:19:42 +03007699 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007700}
7701
Kalle Valoffb9eb32010-02-17 17:58:10 +02007702static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7703{
Johannes Berg4c476992010-10-04 21:36:35 +02007704 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007705 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007706 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007707 u8 ps_state;
7708 bool state;
7709 int err;
7710
Johannes Berg4c476992010-10-04 21:36:35 +02007711 if (!info->attrs[NL80211_ATTR_PS_STATE])
7712 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007713
7714 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7715
Johannes Berg4c476992010-10-04 21:36:35 +02007716 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7717 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007718
7719 wdev = dev->ieee80211_ptr;
7720
Johannes Berg4c476992010-10-04 21:36:35 +02007721 if (!rdev->ops->set_power_mgmt)
7722 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007723
7724 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7725
7726 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007727 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007728
Hila Gonene35e4d22012-06-27 17:19:42 +03007729 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007730 if (!err)
7731 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007732 return err;
7733}
7734
7735static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7736{
Johannes Berg4c476992010-10-04 21:36:35 +02007737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007738 enum nl80211_ps_state ps_state;
7739 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007740 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007741 struct sk_buff *msg;
7742 void *hdr;
7743 int err;
7744
Kalle Valoffb9eb32010-02-17 17:58:10 +02007745 wdev = dev->ieee80211_ptr;
7746
Johannes Berg4c476992010-10-04 21:36:35 +02007747 if (!rdev->ops->set_power_mgmt)
7748 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007749
7750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007751 if (!msg)
7752 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007753
Eric W. Biederman15e47302012-09-07 20:12:54 +00007754 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007755 NL80211_CMD_GET_POWER_SAVE);
7756 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007757 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007758 goto free_msg;
7759 }
7760
7761 if (wdev->ps)
7762 ps_state = NL80211_PS_ENABLED;
7763 else
7764 ps_state = NL80211_PS_DISABLED;
7765
David S. Miller9360ffd2012-03-29 04:41:26 -04007766 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7767 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007768
7769 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007770 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007771
Johannes Berg4c476992010-10-04 21:36:35 +02007772 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007773 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007774 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007775 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007776 return err;
7777}
7778
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007779static struct nla_policy
7780nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7781 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7782 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7783 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007784 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7785 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7786 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007787};
7788
Thomas Pedersen84f10702012-07-12 16:17:33 -07007789static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007790 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007791{
7792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007793 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007794 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007795
Johannes Bergd9d8b012012-11-26 12:51:52 +01007796 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007797 return -EINVAL;
7798
Thomas Pedersen84f10702012-07-12 16:17:33 -07007799 if (!rdev->ops->set_cqm_txe_config)
7800 return -EOPNOTSUPP;
7801
7802 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7803 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7804 return -EOPNOTSUPP;
7805
Hila Gonene35e4d22012-06-27 17:19:42 +03007806 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007807}
7808
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007809static int nl80211_set_cqm_rssi(struct genl_info *info,
7810 s32 threshold, u32 hysteresis)
7811{
Johannes Berg4c476992010-10-04 21:36:35 +02007812 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007813 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007814 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007815
7816 if (threshold > 0)
7817 return -EINVAL;
7818
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007819 /* disabling - hysteresis should also be zero then */
7820 if (threshold == 0)
7821 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007822
Johannes Berg4c476992010-10-04 21:36:35 +02007823 if (!rdev->ops->set_cqm_rssi_config)
7824 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007825
Johannes Berg074ac8d2010-09-16 14:58:22 +02007826 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007827 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7828 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007829
Hila Gonene35e4d22012-06-27 17:19:42 +03007830 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007831}
7832
7833static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7834{
7835 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7836 struct nlattr *cqm;
7837 int err;
7838
7839 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007840 if (!cqm)
7841 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007842
7843 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7844 nl80211_attr_cqm_policy);
7845 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007846 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007847
7848 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7849 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007850 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7851 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007852
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007853 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7854 }
7855
7856 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7857 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7858 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7859 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7860 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7861 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7862
7863 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7864 }
7865
7866 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007867}
7868
Johannes Berg29cbe682010-12-03 09:20:44 +01007869static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7870{
7871 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7872 struct net_device *dev = info->user_ptr[1];
7873 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007874 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007875 int err;
7876
7877 /* start with default */
7878 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007879 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007880
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007881 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007882 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007883 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007884 if (err)
7885 return err;
7886 }
7887
7888 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7889 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7890 return -EINVAL;
7891
Javier Cardonac80d5452010-12-16 17:37:49 -08007892 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7893 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7894
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007895 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7896 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7897 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7898 return -EINVAL;
7899
Marco Porsch9bdbf042013-01-07 16:04:51 +01007900 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7901 setup.beacon_interval =
7902 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7903 if (setup.beacon_interval < 10 ||
7904 setup.beacon_interval > 10000)
7905 return -EINVAL;
7906 }
7907
7908 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7909 setup.dtim_period =
7910 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7911 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7912 return -EINVAL;
7913 }
7914
Javier Cardonac80d5452010-12-16 17:37:49 -08007915 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7916 /* parse additional setup parameters if given */
7917 err = nl80211_parse_mesh_setup(info, &setup);
7918 if (err)
7919 return err;
7920 }
7921
Thomas Pedersend37bb182013-03-04 13:06:13 -08007922 if (setup.user_mpm)
7923 cfg.auto_open_plinks = false;
7924
Johannes Bergcc1d2802012-05-16 23:50:20 +02007925 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007926 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7927 if (err)
7928 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007929 } else {
7930 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007931 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007932 }
7933
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07007934 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7935 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7936 int n_rates =
7937 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7938 struct ieee80211_supported_band *sband;
7939
7940 if (!setup.chandef.chan)
7941 return -EINVAL;
7942
7943 sband = rdev->wiphy.bands[setup.chandef.chan->band];
7944
7945 err = ieee80211_get_ratemask(sband, rates, n_rates,
7946 &setup.basic_rates);
7947 if (err)
7948 return err;
7949 }
7950
Javier Cardonac80d5452010-12-16 17:37:49 -08007951 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007952}
7953
7954static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7955{
7956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7957 struct net_device *dev = info->user_ptr[1];
7958
7959 return cfg80211_leave_mesh(rdev, dev);
7960}
7961
Johannes Bergdfb89c52012-06-27 09:23:48 +02007962#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007963static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7964 struct cfg80211_registered_device *rdev)
7965{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007966 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007967 struct nlattr *nl_pats, *nl_pat;
7968 int i, pat_len;
7969
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007970 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007971 return 0;
7972
7973 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7974 if (!nl_pats)
7975 return -ENOBUFS;
7976
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007977 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007978 nl_pat = nla_nest_start(msg, i + 1);
7979 if (!nl_pat)
7980 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007981 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007982 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007983 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007984 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
7985 wowlan->patterns[i].pattern) ||
7986 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007987 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007988 return -ENOBUFS;
7989 nla_nest_end(msg, nl_pat);
7990 }
7991 nla_nest_end(msg, nl_pats);
7992
7993 return 0;
7994}
7995
Johannes Berg2a0e0472013-01-23 22:57:40 +01007996static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7997 struct cfg80211_wowlan_tcp *tcp)
7998{
7999 struct nlattr *nl_tcp;
8000
8001 if (!tcp)
8002 return 0;
8003
8004 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8005 if (!nl_tcp)
8006 return -ENOBUFS;
8007
8008 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8009 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8010 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8011 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8012 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8013 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8014 tcp->payload_len, tcp->payload) ||
8015 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8016 tcp->data_interval) ||
8017 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8018 tcp->wake_len, tcp->wake_data) ||
8019 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8020 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8021 return -ENOBUFS;
8022
8023 if (tcp->payload_seq.len &&
8024 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8025 sizeof(tcp->payload_seq), &tcp->payload_seq))
8026 return -ENOBUFS;
8027
8028 if (tcp->payload_tok.len &&
8029 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8030 sizeof(tcp->payload_tok) + tcp->tokens_size,
8031 &tcp->payload_tok))
8032 return -ENOBUFS;
8033
Johannes Berge248ad32013-05-16 10:24:28 +02008034 nla_nest_end(msg, nl_tcp);
8035
Johannes Berg2a0e0472013-01-23 22:57:40 +01008036 return 0;
8037}
8038
Johannes Bergff1b6e62011-05-04 15:37:28 +02008039static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8040{
8041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8042 struct sk_buff *msg;
8043 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008044 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008045
Johannes Berg964dc9e2013-06-03 17:25:34 +02008046 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008047 return -EOPNOTSUPP;
8048
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008049 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008050 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008051 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8052 rdev->wiphy.wowlan_config->tcp->payload_len +
8053 rdev->wiphy.wowlan_config->tcp->wake_len +
8054 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008055 }
8056
8057 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008058 if (!msg)
8059 return -ENOMEM;
8060
Eric W. Biederman15e47302012-09-07 20:12:54 +00008061 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008062 NL80211_CMD_GET_WOWLAN);
8063 if (!hdr)
8064 goto nla_put_failure;
8065
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008066 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008067 struct nlattr *nl_wowlan;
8068
8069 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8070 if (!nl_wowlan)
8071 goto nla_put_failure;
8072
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008073 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008074 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008075 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008076 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008077 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008078 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008079 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008080 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008081 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008082 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008083 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008084 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008085 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008086 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8087 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008088
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008089 if (nl80211_send_wowlan_patterns(msg, rdev))
8090 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008091
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008092 if (nl80211_send_wowlan_tcp(msg,
8093 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008094 goto nla_put_failure;
8095
Johannes Bergff1b6e62011-05-04 15:37:28 +02008096 nla_nest_end(msg, nl_wowlan);
8097 }
8098
8099 genlmsg_end(msg, hdr);
8100 return genlmsg_reply(msg, info);
8101
8102nla_put_failure:
8103 nlmsg_free(msg);
8104 return -ENOBUFS;
8105}
8106
Johannes Berg2a0e0472013-01-23 22:57:40 +01008107static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8108 struct nlattr *attr,
8109 struct cfg80211_wowlan *trig)
8110{
8111 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8112 struct cfg80211_wowlan_tcp *cfg;
8113 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8114 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8115 u32 size;
8116 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8117 int err, port;
8118
Johannes Berg964dc9e2013-06-03 17:25:34 +02008119 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008120 return -EINVAL;
8121
8122 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8123 nla_data(attr), nla_len(attr),
8124 nl80211_wowlan_tcp_policy);
8125 if (err)
8126 return err;
8127
8128 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8129 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8130 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8131 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8132 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8133 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8134 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8135 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8136 return -EINVAL;
8137
8138 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008139 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008140 return -EINVAL;
8141
8142 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008143 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008144 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008145 return -EINVAL;
8146
8147 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008148 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008149 return -EINVAL;
8150
8151 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8152 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8153 return -EINVAL;
8154
8155 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8156 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8157
8158 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8159 tokens_size = tokln - sizeof(*tok);
8160
8161 if (!tok->len || tokens_size % tok->len)
8162 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008163 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008164 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008165 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008166 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008167 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008168 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008169 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008170 return -EINVAL;
8171 if (tok->offset + tok->len > data_size)
8172 return -EINVAL;
8173 }
8174
8175 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8176 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008177 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008178 return -EINVAL;
8179 if (seq->len == 0 || seq->len > 4)
8180 return -EINVAL;
8181 if (seq->len + seq->offset > data_size)
8182 return -EINVAL;
8183 }
8184
8185 size = sizeof(*cfg);
8186 size += data_size;
8187 size += wake_size + wake_mask_size;
8188 size += tokens_size;
8189
8190 cfg = kzalloc(size, GFP_KERNEL);
8191 if (!cfg)
8192 return -ENOMEM;
8193 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8194 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8195 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8196 ETH_ALEN);
8197 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8198 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8199 else
8200 port = 0;
8201#ifdef CONFIG_INET
8202 /* allocate a socket and port for it and use it */
8203 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8204 IPPROTO_TCP, &cfg->sock, 1);
8205 if (err) {
8206 kfree(cfg);
8207 return err;
8208 }
8209 if (inet_csk_get_port(cfg->sock->sk, port)) {
8210 sock_release(cfg->sock);
8211 kfree(cfg);
8212 return -EADDRINUSE;
8213 }
8214 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8215#else
8216 if (!port) {
8217 kfree(cfg);
8218 return -EINVAL;
8219 }
8220 cfg->src_port = port;
8221#endif
8222
8223 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8224 cfg->payload_len = data_size;
8225 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8226 memcpy((void *)cfg->payload,
8227 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8228 data_size);
8229 if (seq)
8230 cfg->payload_seq = *seq;
8231 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8232 cfg->wake_len = wake_size;
8233 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8234 memcpy((void *)cfg->wake_data,
8235 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8236 wake_size);
8237 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8238 data_size + wake_size;
8239 memcpy((void *)cfg->wake_mask,
8240 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8241 wake_mask_size);
8242 if (tok) {
8243 cfg->tokens_size = tokens_size;
8244 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8245 }
8246
8247 trig->tcp = cfg;
8248
8249 return 0;
8250}
8251
Johannes Bergff1b6e62011-05-04 15:37:28 +02008252static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8253{
8254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8255 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008256 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008257 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008258 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008259 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008260 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008261
Johannes Berg964dc9e2013-06-03 17:25:34 +02008262 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008263 return -EOPNOTSUPP;
8264
Johannes Bergae33bd82012-07-12 16:25:02 +02008265 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8266 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008267 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008268 goto set_wakeup;
8269 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008270
8271 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8272 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8273 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8274 nl80211_wowlan_policy);
8275 if (err)
8276 return err;
8277
8278 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8279 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8280 return -EINVAL;
8281 new_triggers.any = true;
8282 }
8283
8284 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8285 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8286 return -EINVAL;
8287 new_triggers.disconnect = true;
8288 }
8289
8290 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8291 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8292 return -EINVAL;
8293 new_triggers.magic_pkt = true;
8294 }
8295
Johannes Berg77dbbb12011-07-13 10:48:55 +02008296 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8297 return -EINVAL;
8298
8299 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8300 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8301 return -EINVAL;
8302 new_triggers.gtk_rekey_failure = true;
8303 }
8304
8305 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8306 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8307 return -EINVAL;
8308 new_triggers.eap_identity_req = true;
8309 }
8310
8311 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8312 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8313 return -EINVAL;
8314 new_triggers.four_way_handshake = true;
8315 }
8316
8317 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8318 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8319 return -EINVAL;
8320 new_triggers.rfkill_release = true;
8321 }
8322
Johannes Bergff1b6e62011-05-04 15:37:28 +02008323 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8324 struct nlattr *pat;
8325 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008326 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008327 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008328
8329 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8330 rem)
8331 n_patterns++;
8332 if (n_patterns > wowlan->n_patterns)
8333 return -EINVAL;
8334
8335 new_triggers.patterns = kcalloc(n_patterns,
8336 sizeof(new_triggers.patterns[0]),
8337 GFP_KERNEL);
8338 if (!new_triggers.patterns)
8339 return -ENOMEM;
8340
8341 new_triggers.n_patterns = n_patterns;
8342 i = 0;
8343
8344 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8345 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008346 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8347 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008348 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008349 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8350 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008351 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008352 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008353 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008354 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008355 goto error;
8356 if (pat_len > wowlan->pattern_max_len ||
8357 pat_len < wowlan->pattern_min_len)
8358 goto error;
8359
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008360 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008361 pkt_offset = 0;
8362 else
8363 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008364 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008365 if (pkt_offset > wowlan->max_pkt_offset)
8366 goto error;
8367 new_triggers.patterns[i].pkt_offset = pkt_offset;
8368
Johannes Bergff1b6e62011-05-04 15:37:28 +02008369 new_triggers.patterns[i].mask =
8370 kmalloc(mask_len + pat_len, GFP_KERNEL);
8371 if (!new_triggers.patterns[i].mask) {
8372 err = -ENOMEM;
8373 goto error;
8374 }
8375 new_triggers.patterns[i].pattern =
8376 new_triggers.patterns[i].mask + mask_len;
8377 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008378 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008379 mask_len);
8380 new_triggers.patterns[i].pattern_len = pat_len;
8381 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008382 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008383 pat_len);
8384 i++;
8385 }
8386 }
8387
Johannes Berg2a0e0472013-01-23 22:57:40 +01008388 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8389 err = nl80211_parse_wowlan_tcp(
8390 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8391 &new_triggers);
8392 if (err)
8393 goto error;
8394 }
8395
Johannes Bergae33bd82012-07-12 16:25:02 +02008396 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8397 if (!ntrig) {
8398 err = -ENOMEM;
8399 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008400 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008401 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008402 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008403
Johannes Bergae33bd82012-07-12 16:25:02 +02008404 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008405 if (rdev->ops->set_wakeup &&
8406 prev_enabled != !!rdev->wiphy.wowlan_config)
8407 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008408
Johannes Bergff1b6e62011-05-04 15:37:28 +02008409 return 0;
8410 error:
8411 for (i = 0; i < new_triggers.n_patterns; i++)
8412 kfree(new_triggers.patterns[i].mask);
8413 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008414 if (new_triggers.tcp && new_triggers.tcp->sock)
8415 sock_release(new_triggers.tcp->sock);
8416 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008417 return err;
8418}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008419#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008420
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008421static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8422 struct cfg80211_registered_device *rdev)
8423{
8424 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8425 int i, j, pat_len;
8426 struct cfg80211_coalesce_rules *rule;
8427
8428 if (!rdev->coalesce->n_rules)
8429 return 0;
8430
8431 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8432 if (!nl_rules)
8433 return -ENOBUFS;
8434
8435 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8436 nl_rule = nla_nest_start(msg, i + 1);
8437 if (!nl_rule)
8438 return -ENOBUFS;
8439
8440 rule = &rdev->coalesce->rules[i];
8441 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8442 rule->delay))
8443 return -ENOBUFS;
8444
8445 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8446 rule->condition))
8447 return -ENOBUFS;
8448
8449 nl_pats = nla_nest_start(msg,
8450 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8451 if (!nl_pats)
8452 return -ENOBUFS;
8453
8454 for (j = 0; j < rule->n_patterns; j++) {
8455 nl_pat = nla_nest_start(msg, j + 1);
8456 if (!nl_pat)
8457 return -ENOBUFS;
8458 pat_len = rule->patterns[j].pattern_len;
8459 if (nla_put(msg, NL80211_PKTPAT_MASK,
8460 DIV_ROUND_UP(pat_len, 8),
8461 rule->patterns[j].mask) ||
8462 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8463 rule->patterns[j].pattern) ||
8464 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8465 rule->patterns[j].pkt_offset))
8466 return -ENOBUFS;
8467 nla_nest_end(msg, nl_pat);
8468 }
8469 nla_nest_end(msg, nl_pats);
8470 nla_nest_end(msg, nl_rule);
8471 }
8472 nla_nest_end(msg, nl_rules);
8473
8474 return 0;
8475}
8476
8477static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8478{
8479 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8480 struct sk_buff *msg;
8481 void *hdr;
8482
8483 if (!rdev->wiphy.coalesce)
8484 return -EOPNOTSUPP;
8485
8486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8487 if (!msg)
8488 return -ENOMEM;
8489
8490 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8491 NL80211_CMD_GET_COALESCE);
8492 if (!hdr)
8493 goto nla_put_failure;
8494
8495 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8496 goto nla_put_failure;
8497
8498 genlmsg_end(msg, hdr);
8499 return genlmsg_reply(msg, info);
8500
8501nla_put_failure:
8502 nlmsg_free(msg);
8503 return -ENOBUFS;
8504}
8505
8506void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8507{
8508 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8509 int i, j;
8510 struct cfg80211_coalesce_rules *rule;
8511
8512 if (!coalesce)
8513 return;
8514
8515 for (i = 0; i < coalesce->n_rules; i++) {
8516 rule = &coalesce->rules[i];
8517 for (j = 0; j < rule->n_patterns; j++)
8518 kfree(rule->patterns[j].mask);
8519 kfree(rule->patterns);
8520 }
8521 kfree(coalesce->rules);
8522 kfree(coalesce);
8523 rdev->coalesce = NULL;
8524}
8525
8526static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8527 struct nlattr *rule,
8528 struct cfg80211_coalesce_rules *new_rule)
8529{
8530 int err, i;
8531 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8532 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8533 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8534 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8535
8536 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8537 nla_len(rule), nl80211_coalesce_policy);
8538 if (err)
8539 return err;
8540
8541 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8542 new_rule->delay =
8543 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8544 if (new_rule->delay > coalesce->max_delay)
8545 return -EINVAL;
8546
8547 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8548 new_rule->condition =
8549 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8550 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8551 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8552 return -EINVAL;
8553
8554 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8555 return -EINVAL;
8556
8557 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8558 rem)
8559 n_patterns++;
8560 if (n_patterns > coalesce->n_patterns)
8561 return -EINVAL;
8562
8563 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8564 GFP_KERNEL);
8565 if (!new_rule->patterns)
8566 return -ENOMEM;
8567
8568 new_rule->n_patterns = n_patterns;
8569 i = 0;
8570
8571 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8572 rem) {
8573 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8574 nla_len(pat), NULL);
8575 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8576 !pat_tb[NL80211_PKTPAT_PATTERN])
8577 return -EINVAL;
8578 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8579 mask_len = DIV_ROUND_UP(pat_len, 8);
8580 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8581 return -EINVAL;
8582 if (pat_len > coalesce->pattern_max_len ||
8583 pat_len < coalesce->pattern_min_len)
8584 return -EINVAL;
8585
8586 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8587 pkt_offset = 0;
8588 else
8589 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8590 if (pkt_offset > coalesce->max_pkt_offset)
8591 return -EINVAL;
8592 new_rule->patterns[i].pkt_offset = pkt_offset;
8593
8594 new_rule->patterns[i].mask =
8595 kmalloc(mask_len + pat_len, GFP_KERNEL);
8596 if (!new_rule->patterns[i].mask)
8597 return -ENOMEM;
8598 new_rule->patterns[i].pattern =
8599 new_rule->patterns[i].mask + mask_len;
8600 memcpy(new_rule->patterns[i].mask,
8601 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8602 new_rule->patterns[i].pattern_len = pat_len;
8603 memcpy(new_rule->patterns[i].pattern,
8604 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8605 i++;
8606 }
8607
8608 return 0;
8609}
8610
8611static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8612{
8613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8614 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8615 struct cfg80211_coalesce new_coalesce = {};
8616 struct cfg80211_coalesce *n_coalesce;
8617 int err, rem_rule, n_rules = 0, i, j;
8618 struct nlattr *rule;
8619 struct cfg80211_coalesce_rules *tmp_rule;
8620
8621 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8622 return -EOPNOTSUPP;
8623
8624 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8625 cfg80211_rdev_free_coalesce(rdev);
8626 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8627 return 0;
8628 }
8629
8630 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8631 rem_rule)
8632 n_rules++;
8633 if (n_rules > coalesce->n_rules)
8634 return -EINVAL;
8635
8636 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8637 GFP_KERNEL);
8638 if (!new_coalesce.rules)
8639 return -ENOMEM;
8640
8641 new_coalesce.n_rules = n_rules;
8642 i = 0;
8643
8644 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8645 rem_rule) {
8646 err = nl80211_parse_coalesce_rule(rdev, rule,
8647 &new_coalesce.rules[i]);
8648 if (err)
8649 goto error;
8650
8651 i++;
8652 }
8653
8654 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8655 if (err)
8656 goto error;
8657
8658 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8659 if (!n_coalesce) {
8660 err = -ENOMEM;
8661 goto error;
8662 }
8663 cfg80211_rdev_free_coalesce(rdev);
8664 rdev->coalesce = n_coalesce;
8665
8666 return 0;
8667error:
8668 for (i = 0; i < new_coalesce.n_rules; i++) {
8669 tmp_rule = &new_coalesce.rules[i];
8670 for (j = 0; j < tmp_rule->n_patterns; j++)
8671 kfree(tmp_rule->patterns[j].mask);
8672 kfree(tmp_rule->patterns);
8673 }
8674 kfree(new_coalesce.rules);
8675
8676 return err;
8677}
8678
Johannes Berge5497d72011-07-05 16:35:40 +02008679static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8680{
8681 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8682 struct net_device *dev = info->user_ptr[1];
8683 struct wireless_dev *wdev = dev->ieee80211_ptr;
8684 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8685 struct cfg80211_gtk_rekey_data rekey_data;
8686 int err;
8687
8688 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8689 return -EINVAL;
8690
8691 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8692 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8693 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8694 nl80211_rekey_policy);
8695 if (err)
8696 return err;
8697
8698 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8699 return -ERANGE;
8700 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8701 return -ERANGE;
8702 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8703 return -ERANGE;
8704
8705 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8706 NL80211_KEK_LEN);
8707 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8708 NL80211_KCK_LEN);
8709 memcpy(rekey_data.replay_ctr,
8710 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8711 NL80211_REPLAY_CTR_LEN);
8712
8713 wdev_lock(wdev);
8714 if (!wdev->current_bss) {
8715 err = -ENOTCONN;
8716 goto out;
8717 }
8718
8719 if (!rdev->ops->set_rekey_data) {
8720 err = -EOPNOTSUPP;
8721 goto out;
8722 }
8723
Hila Gonene35e4d22012-06-27 17:19:42 +03008724 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008725 out:
8726 wdev_unlock(wdev);
8727 return err;
8728}
8729
Johannes Berg28946da2011-11-04 11:18:12 +01008730static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8731 struct genl_info *info)
8732{
8733 struct net_device *dev = info->user_ptr[1];
8734 struct wireless_dev *wdev = dev->ieee80211_ptr;
8735
8736 if (wdev->iftype != NL80211_IFTYPE_AP &&
8737 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8738 return -EINVAL;
8739
Eric W. Biederman15e47302012-09-07 20:12:54 +00008740 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008741 return -EBUSY;
8742
Eric W. Biederman15e47302012-09-07 20:12:54 +00008743 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008744 return 0;
8745}
8746
Johannes Berg7f6cf312011-11-04 11:18:15 +01008747static int nl80211_probe_client(struct sk_buff *skb,
8748 struct genl_info *info)
8749{
8750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8751 struct net_device *dev = info->user_ptr[1];
8752 struct wireless_dev *wdev = dev->ieee80211_ptr;
8753 struct sk_buff *msg;
8754 void *hdr;
8755 const u8 *addr;
8756 u64 cookie;
8757 int err;
8758
8759 if (wdev->iftype != NL80211_IFTYPE_AP &&
8760 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8761 return -EOPNOTSUPP;
8762
8763 if (!info->attrs[NL80211_ATTR_MAC])
8764 return -EINVAL;
8765
8766 if (!rdev->ops->probe_client)
8767 return -EOPNOTSUPP;
8768
8769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8770 if (!msg)
8771 return -ENOMEM;
8772
Eric W. Biederman15e47302012-09-07 20:12:54 +00008773 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008774 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008775 if (!hdr) {
8776 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008777 goto free_msg;
8778 }
8779
8780 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8781
Hila Gonene35e4d22012-06-27 17:19:42 +03008782 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008783 if (err)
8784 goto free_msg;
8785
David S. Miller9360ffd2012-03-29 04:41:26 -04008786 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8787 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008788
8789 genlmsg_end(msg, hdr);
8790
8791 return genlmsg_reply(msg, info);
8792
8793 nla_put_failure:
8794 err = -ENOBUFS;
8795 free_msg:
8796 nlmsg_free(msg);
8797 return err;
8798}
8799
Johannes Berg5e7602302011-11-04 11:18:17 +01008800static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8801{
8802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008803 struct cfg80211_beacon_registration *reg, *nreg;
8804 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008805
8806 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8807 return -EOPNOTSUPP;
8808
Ben Greear37c73b52012-10-26 14:49:25 -07008809 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8810 if (!nreg)
8811 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008812
Ben Greear37c73b52012-10-26 14:49:25 -07008813 /* First, check if already registered. */
8814 spin_lock_bh(&rdev->beacon_registrations_lock);
8815 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8816 if (reg->nlportid == info->snd_portid) {
8817 rv = -EALREADY;
8818 goto out_err;
8819 }
8820 }
8821 /* Add it to the list */
8822 nreg->nlportid = info->snd_portid;
8823 list_add(&nreg->list, &rdev->beacon_registrations);
8824
8825 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008826
8827 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008828out_err:
8829 spin_unlock_bh(&rdev->beacon_registrations_lock);
8830 kfree(nreg);
8831 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008832}
8833
Johannes Berg98104fde2012-06-16 00:19:54 +02008834static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8835{
8836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8837 struct wireless_dev *wdev = info->user_ptr[1];
8838 int err;
8839
8840 if (!rdev->ops->start_p2p_device)
8841 return -EOPNOTSUPP;
8842
8843 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8844 return -EOPNOTSUPP;
8845
8846 if (wdev->p2p_started)
8847 return 0;
8848
Johannes Berg98104fde2012-06-16 00:19:54 +02008849 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008850 if (err)
8851 return err;
8852
Johannes Bergeeb126e2012-10-23 15:16:50 +02008853 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008854 if (err)
8855 return err;
8856
8857 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008858 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008859
8860 return 0;
8861}
8862
8863static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8864{
8865 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8866 struct wireless_dev *wdev = info->user_ptr[1];
8867
8868 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8869 return -EOPNOTSUPP;
8870
8871 if (!rdev->ops->stop_p2p_device)
8872 return -EOPNOTSUPP;
8873
Johannes Bergf9f47522013-03-19 15:04:07 +01008874 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008875
8876 return 0;
8877}
8878
Johannes Berg3713b4e2013-02-14 16:19:38 +01008879static int nl80211_get_protocol_features(struct sk_buff *skb,
8880 struct genl_info *info)
8881{
8882 void *hdr;
8883 struct sk_buff *msg;
8884
8885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8886 if (!msg)
8887 return -ENOMEM;
8888
8889 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8890 NL80211_CMD_GET_PROTOCOL_FEATURES);
8891 if (!hdr)
8892 goto nla_put_failure;
8893
8894 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8895 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8896 goto nla_put_failure;
8897
8898 genlmsg_end(msg, hdr);
8899 return genlmsg_reply(msg, info);
8900
8901 nla_put_failure:
8902 kfree_skb(msg);
8903 return -ENOBUFS;
8904}
8905
Jouni Malinen355199e2013-02-27 17:14:27 +02008906static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8907{
8908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8909 struct cfg80211_update_ft_ies_params ft_params;
8910 struct net_device *dev = info->user_ptr[1];
8911
8912 if (!rdev->ops->update_ft_ies)
8913 return -EOPNOTSUPP;
8914
8915 if (!info->attrs[NL80211_ATTR_MDID] ||
8916 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8917 return -EINVAL;
8918
8919 memset(&ft_params, 0, sizeof(ft_params));
8920 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8921 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8922 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8923
8924 return rdev_update_ft_ies(rdev, dev, &ft_params);
8925}
8926
Arend van Spriel5de17982013-04-18 15:49:00 +02008927static int nl80211_crit_protocol_start(struct sk_buff *skb,
8928 struct genl_info *info)
8929{
8930 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8931 struct wireless_dev *wdev = info->user_ptr[1];
8932 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8933 u16 duration;
8934 int ret;
8935
8936 if (!rdev->ops->crit_proto_start)
8937 return -EOPNOTSUPP;
8938
8939 if (WARN_ON(!rdev->ops->crit_proto_stop))
8940 return -EINVAL;
8941
8942 if (rdev->crit_proto_nlportid)
8943 return -EBUSY;
8944
8945 /* determine protocol if provided */
8946 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8947 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8948
8949 if (proto >= NUM_NL80211_CRIT_PROTO)
8950 return -EINVAL;
8951
8952 /* timeout must be provided */
8953 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8954 return -EINVAL;
8955
8956 duration =
8957 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8958
8959 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8960 return -ERANGE;
8961
8962 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8963 if (!ret)
8964 rdev->crit_proto_nlportid = info->snd_portid;
8965
8966 return ret;
8967}
8968
8969static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8970 struct genl_info *info)
8971{
8972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8973 struct wireless_dev *wdev = info->user_ptr[1];
8974
8975 if (!rdev->ops->crit_proto_stop)
8976 return -EOPNOTSUPP;
8977
8978 if (rdev->crit_proto_nlportid) {
8979 rdev->crit_proto_nlportid = 0;
8980 rdev_crit_proto_stop(rdev, wdev);
8981 }
8982 return 0;
8983}
8984
Johannes Bergad7e7182013-11-13 13:37:47 +01008985static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
8986{
8987 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8988 struct wireless_dev *wdev =
8989 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8990 int i, err;
8991 u32 vid, subcmd;
8992
8993 if (!rdev->wiphy.vendor_commands)
8994 return -EOPNOTSUPP;
8995
8996 if (IS_ERR(wdev)) {
8997 err = PTR_ERR(wdev);
8998 if (err != -EINVAL)
8999 return err;
9000 wdev = NULL;
9001 } else if (wdev->wiphy != &rdev->wiphy) {
9002 return -EINVAL;
9003 }
9004
9005 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9006 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9007 return -EINVAL;
9008
9009 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9010 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9011 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9012 const struct wiphy_vendor_command *vcmd;
9013 void *data = NULL;
9014 int len = 0;
9015
9016 vcmd = &rdev->wiphy.vendor_commands[i];
9017
9018 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9019 continue;
9020
9021 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9022 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9023 if (!wdev)
9024 return -EINVAL;
9025 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9026 !wdev->netdev)
9027 return -EINVAL;
9028
9029 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9030 if (wdev->netdev &&
9031 !netif_running(wdev->netdev))
9032 return -ENETDOWN;
9033 if (!wdev->netdev && !wdev->p2p_started)
9034 return -ENETDOWN;
9035 }
9036 } else {
9037 wdev = NULL;
9038 }
9039
9040 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9041 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9042 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9043 }
9044
9045 rdev->cur_cmd_info = info;
9046 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9047 data, len);
9048 rdev->cur_cmd_info = NULL;
9049 return err;
9050 }
9051
9052 return -EOPNOTSUPP;
9053}
9054
9055struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9056 enum nl80211_commands cmd,
9057 enum nl80211_attrs attr,
9058 int approxlen)
9059{
9060 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9061
9062 if (WARN_ON(!rdev->cur_cmd_info))
9063 return NULL;
9064
9065 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9066 rdev->cur_cmd_info->snd_portid,
9067 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009068 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009069}
9070EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9071
9072int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9073{
9074 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9075 void *hdr = ((void **)skb->cb)[1];
9076 struct nlattr *data = ((void **)skb->cb)[2];
9077
9078 if (WARN_ON(!rdev->cur_cmd_info)) {
9079 kfree_skb(skb);
9080 return -EINVAL;
9081 }
9082
9083 nla_nest_end(skb, data);
9084 genlmsg_end(skb, hdr);
9085 return genlmsg_reply(skb, rdev->cur_cmd_info);
9086}
9087EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9088
9089
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009090static int nl80211_set_qos_map(struct sk_buff *skb,
9091 struct genl_info *info)
9092{
9093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9094 struct cfg80211_qos_map *qos_map = NULL;
9095 struct net_device *dev = info->user_ptr[1];
9096 u8 *pos, len, num_des, des_len, des;
9097 int ret;
9098
9099 if (!rdev->ops->set_qos_map)
9100 return -EOPNOTSUPP;
9101
9102 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9103 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9104 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9105
9106 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9107 len > IEEE80211_QOS_MAP_LEN_MAX)
9108 return -EINVAL;
9109
9110 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9111 if (!qos_map)
9112 return -ENOMEM;
9113
9114 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9115 if (num_des) {
9116 des_len = num_des *
9117 sizeof(struct cfg80211_dscp_exception);
9118 memcpy(qos_map->dscp_exception, pos, des_len);
9119 qos_map->num_des = num_des;
9120 for (des = 0; des < num_des; des++) {
9121 if (qos_map->dscp_exception[des].up > 7) {
9122 kfree(qos_map);
9123 return -EINVAL;
9124 }
9125 }
9126 pos += des_len;
9127 }
9128 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9129 }
9130
9131 wdev_lock(dev->ieee80211_ptr);
9132 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9133 if (!ret)
9134 ret = rdev_set_qos_map(rdev, dev, qos_map);
9135 wdev_unlock(dev->ieee80211_ptr);
9136
9137 kfree(qos_map);
9138 return ret;
9139}
9140
Johannes Berg4c476992010-10-04 21:36:35 +02009141#define NL80211_FLAG_NEED_WIPHY 0x01
9142#define NL80211_FLAG_NEED_NETDEV 0x02
9143#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009144#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9145#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9146 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009147#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009148/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009149#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9150 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009151
Johannes Bergf84f7712013-11-14 17:14:45 +01009152static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009153 struct genl_info *info)
9154{
9155 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009156 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009157 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009158 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9159
9160 if (rtnl)
9161 rtnl_lock();
9162
9163 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009164 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009165 if (IS_ERR(rdev)) {
9166 if (rtnl)
9167 rtnl_unlock();
9168 return PTR_ERR(rdev);
9169 }
9170 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009171 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9172 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009173 ASSERT_RTNL();
9174
Johannes Berg89a54e42012-06-15 14:33:17 +02009175 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9176 info->attrs);
9177 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009178 if (rtnl)
9179 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009180 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009181 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009182
Johannes Berg89a54e42012-06-15 14:33:17 +02009183 dev = wdev->netdev;
9184 rdev = wiphy_to_dev(wdev->wiphy);
9185
Johannes Berg1bf614e2012-06-15 15:23:36 +02009186 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9187 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009188 if (rtnl)
9189 rtnl_unlock();
9190 return -EINVAL;
9191 }
9192
9193 info->user_ptr[1] = dev;
9194 } else {
9195 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009196 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009197
Johannes Berg1bf614e2012-06-15 15:23:36 +02009198 if (dev) {
9199 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9200 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009201 if (rtnl)
9202 rtnl_unlock();
9203 return -ENETDOWN;
9204 }
9205
9206 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009207 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9208 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009209 if (rtnl)
9210 rtnl_unlock();
9211 return -ENETDOWN;
9212 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009213 }
9214
Johannes Berg4c476992010-10-04 21:36:35 +02009215 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009216 }
9217
9218 return 0;
9219}
9220
Johannes Bergf84f7712013-11-14 17:14:45 +01009221static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009222 struct genl_info *info)
9223{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009224 if (info->user_ptr[1]) {
9225 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9226 struct wireless_dev *wdev = info->user_ptr[1];
9227
9228 if (wdev->netdev)
9229 dev_put(wdev->netdev);
9230 } else {
9231 dev_put(info->user_ptr[1]);
9232 }
9233 }
Johannes Berg4c476992010-10-04 21:36:35 +02009234 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9235 rtnl_unlock();
9236}
9237
Johannes Berg4534de82013-11-14 17:14:46 +01009238static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009239 {
9240 .cmd = NL80211_CMD_GET_WIPHY,
9241 .doit = nl80211_get_wiphy,
9242 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009243 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009244 .policy = nl80211_policy,
9245 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009246 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9247 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009248 },
9249 {
9250 .cmd = NL80211_CMD_SET_WIPHY,
9251 .doit = nl80211_set_wiphy,
9252 .policy = nl80211_policy,
9253 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009254 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009255 },
9256 {
9257 .cmd = NL80211_CMD_GET_INTERFACE,
9258 .doit = nl80211_get_interface,
9259 .dumpit = nl80211_dump_interface,
9260 .policy = nl80211_policy,
9261 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009262 .internal_flags = NL80211_FLAG_NEED_WDEV |
9263 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009264 },
9265 {
9266 .cmd = NL80211_CMD_SET_INTERFACE,
9267 .doit = nl80211_set_interface,
9268 .policy = nl80211_policy,
9269 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009270 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9271 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009272 },
9273 {
9274 .cmd = NL80211_CMD_NEW_INTERFACE,
9275 .doit = nl80211_new_interface,
9276 .policy = nl80211_policy,
9277 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009278 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9279 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009280 },
9281 {
9282 .cmd = NL80211_CMD_DEL_INTERFACE,
9283 .doit = nl80211_del_interface,
9284 .policy = nl80211_policy,
9285 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009286 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009287 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009288 },
Johannes Berg41ade002007-12-19 02:03:29 +01009289 {
9290 .cmd = NL80211_CMD_GET_KEY,
9291 .doit = nl80211_get_key,
9292 .policy = nl80211_policy,
9293 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009295 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009296 },
9297 {
9298 .cmd = NL80211_CMD_SET_KEY,
9299 .doit = nl80211_set_key,
9300 .policy = nl80211_policy,
9301 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009303 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009304 },
9305 {
9306 .cmd = NL80211_CMD_NEW_KEY,
9307 .doit = nl80211_new_key,
9308 .policy = nl80211_policy,
9309 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009311 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009312 },
9313 {
9314 .cmd = NL80211_CMD_DEL_KEY,
9315 .doit = nl80211_del_key,
9316 .policy = nl80211_policy,
9317 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009318 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009319 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009320 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009321 {
9322 .cmd = NL80211_CMD_SET_BEACON,
9323 .policy = nl80211_policy,
9324 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009325 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009326 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009327 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009328 },
9329 {
Johannes Berg88600202012-02-13 15:17:18 +01009330 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009331 .policy = nl80211_policy,
9332 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009333 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009334 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009335 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009336 },
9337 {
Johannes Berg88600202012-02-13 15:17:18 +01009338 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009339 .policy = nl80211_policy,
9340 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009341 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009343 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009344 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009345 {
9346 .cmd = NL80211_CMD_GET_STATION,
9347 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009348 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009349 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009350 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9351 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009352 },
9353 {
9354 .cmd = NL80211_CMD_SET_STATION,
9355 .doit = nl80211_set_station,
9356 .policy = nl80211_policy,
9357 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009358 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009359 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009360 },
9361 {
9362 .cmd = NL80211_CMD_NEW_STATION,
9363 .doit = nl80211_new_station,
9364 .policy = nl80211_policy,
9365 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009366 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009367 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009368 },
9369 {
9370 .cmd = NL80211_CMD_DEL_STATION,
9371 .doit = nl80211_del_station,
9372 .policy = nl80211_policy,
9373 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009374 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009375 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009376 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009377 {
9378 .cmd = NL80211_CMD_GET_MPATH,
9379 .doit = nl80211_get_mpath,
9380 .dumpit = nl80211_dump_mpath,
9381 .policy = nl80211_policy,
9382 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009383 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009384 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009385 },
9386 {
9387 .cmd = NL80211_CMD_SET_MPATH,
9388 .doit = nl80211_set_mpath,
9389 .policy = nl80211_policy,
9390 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009392 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009393 },
9394 {
9395 .cmd = NL80211_CMD_NEW_MPATH,
9396 .doit = nl80211_new_mpath,
9397 .policy = nl80211_policy,
9398 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009399 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009400 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009401 },
9402 {
9403 .cmd = NL80211_CMD_DEL_MPATH,
9404 .doit = nl80211_del_mpath,
9405 .policy = nl80211_policy,
9406 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009407 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009408 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009409 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009410 {
9411 .cmd = NL80211_CMD_SET_BSS,
9412 .doit = nl80211_set_bss,
9413 .policy = nl80211_policy,
9414 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009416 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009417 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009418 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009419 .cmd = NL80211_CMD_GET_REG,
9420 .doit = nl80211_get_reg,
9421 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009422 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009423 /* can be retrieved by unprivileged users */
9424 },
9425 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009426 .cmd = NL80211_CMD_SET_REG,
9427 .doit = nl80211_set_reg,
9428 .policy = nl80211_policy,
9429 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009430 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009431 },
9432 {
9433 .cmd = NL80211_CMD_REQ_SET_REG,
9434 .doit = nl80211_req_set_reg,
9435 .policy = nl80211_policy,
9436 .flags = GENL_ADMIN_PERM,
9437 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009438 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009439 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9440 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009441 .policy = nl80211_policy,
9442 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009443 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009444 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009445 },
9446 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009447 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9448 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009449 .policy = nl80211_policy,
9450 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009451 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009452 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009453 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009454 {
Johannes Berg2a519312009-02-10 21:25:55 +01009455 .cmd = NL80211_CMD_TRIGGER_SCAN,
9456 .doit = nl80211_trigger_scan,
9457 .policy = nl80211_policy,
9458 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009459 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009460 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009461 },
9462 {
9463 .cmd = NL80211_CMD_GET_SCAN,
9464 .policy = nl80211_policy,
9465 .dumpit = nl80211_dump_scan,
9466 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009467 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009468 .cmd = NL80211_CMD_START_SCHED_SCAN,
9469 .doit = nl80211_start_sched_scan,
9470 .policy = nl80211_policy,
9471 .flags = GENL_ADMIN_PERM,
9472 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9473 NL80211_FLAG_NEED_RTNL,
9474 },
9475 {
9476 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9477 .doit = nl80211_stop_sched_scan,
9478 .policy = nl80211_policy,
9479 .flags = GENL_ADMIN_PERM,
9480 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9481 NL80211_FLAG_NEED_RTNL,
9482 },
9483 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009484 .cmd = NL80211_CMD_AUTHENTICATE,
9485 .doit = nl80211_authenticate,
9486 .policy = nl80211_policy,
9487 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009488 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009489 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009490 },
9491 {
9492 .cmd = NL80211_CMD_ASSOCIATE,
9493 .doit = nl80211_associate,
9494 .policy = nl80211_policy,
9495 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009496 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009497 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009498 },
9499 {
9500 .cmd = NL80211_CMD_DEAUTHENTICATE,
9501 .doit = nl80211_deauthenticate,
9502 .policy = nl80211_policy,
9503 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009504 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009505 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009506 },
9507 {
9508 .cmd = NL80211_CMD_DISASSOCIATE,
9509 .doit = nl80211_disassociate,
9510 .policy = nl80211_policy,
9511 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009513 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009514 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009515 {
9516 .cmd = NL80211_CMD_JOIN_IBSS,
9517 .doit = nl80211_join_ibss,
9518 .policy = nl80211_policy,
9519 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009521 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009522 },
9523 {
9524 .cmd = NL80211_CMD_LEAVE_IBSS,
9525 .doit = nl80211_leave_ibss,
9526 .policy = nl80211_policy,
9527 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009528 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009529 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009530 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009531#ifdef CONFIG_NL80211_TESTMODE
9532 {
9533 .cmd = NL80211_CMD_TESTMODE,
9534 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009535 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009536 .policy = nl80211_policy,
9537 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009538 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9539 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009540 },
9541#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009542 {
9543 .cmd = NL80211_CMD_CONNECT,
9544 .doit = nl80211_connect,
9545 .policy = nl80211_policy,
9546 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009547 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009548 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009549 },
9550 {
9551 .cmd = NL80211_CMD_DISCONNECT,
9552 .doit = nl80211_disconnect,
9553 .policy = nl80211_policy,
9554 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009555 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009556 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009557 },
Johannes Berg463d0182009-07-14 00:33:35 +02009558 {
9559 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9560 .doit = nl80211_wiphy_netns,
9561 .policy = nl80211_policy,
9562 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009563 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9564 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009565 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009566 {
9567 .cmd = NL80211_CMD_GET_SURVEY,
9568 .policy = nl80211_policy,
9569 .dumpit = nl80211_dump_survey,
9570 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009571 {
9572 .cmd = NL80211_CMD_SET_PMKSA,
9573 .doit = nl80211_setdel_pmksa,
9574 .policy = nl80211_policy,
9575 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009576 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009577 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009578 },
9579 {
9580 .cmd = NL80211_CMD_DEL_PMKSA,
9581 .doit = nl80211_setdel_pmksa,
9582 .policy = nl80211_policy,
9583 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009584 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009585 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009586 },
9587 {
9588 .cmd = NL80211_CMD_FLUSH_PMKSA,
9589 .doit = nl80211_flush_pmksa,
9590 .policy = nl80211_policy,
9591 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009592 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009593 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009594 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009595 {
9596 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9597 .doit = nl80211_remain_on_channel,
9598 .policy = nl80211_policy,
9599 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009600 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009601 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009602 },
9603 {
9604 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9605 .doit = nl80211_cancel_remain_on_channel,
9606 .policy = nl80211_policy,
9607 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009608 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009609 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009610 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009611 {
9612 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9613 .doit = nl80211_set_tx_bitrate_mask,
9614 .policy = nl80211_policy,
9615 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009616 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9617 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009618 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009619 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009620 .cmd = NL80211_CMD_REGISTER_FRAME,
9621 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009622 .policy = nl80211_policy,
9623 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009624 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009625 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009626 },
9627 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009628 .cmd = NL80211_CMD_FRAME,
9629 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009630 .policy = nl80211_policy,
9631 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009632 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009633 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009634 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009635 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009636 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9637 .doit = nl80211_tx_mgmt_cancel_wait,
9638 .policy = nl80211_policy,
9639 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009640 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009641 NL80211_FLAG_NEED_RTNL,
9642 },
9643 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009644 .cmd = NL80211_CMD_SET_POWER_SAVE,
9645 .doit = nl80211_set_power_save,
9646 .policy = nl80211_policy,
9647 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009648 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9649 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009650 },
9651 {
9652 .cmd = NL80211_CMD_GET_POWER_SAVE,
9653 .doit = nl80211_get_power_save,
9654 .policy = nl80211_policy,
9655 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009656 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9657 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009658 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009659 {
9660 .cmd = NL80211_CMD_SET_CQM,
9661 .doit = nl80211_set_cqm,
9662 .policy = nl80211_policy,
9663 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009664 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9665 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009666 },
Johannes Bergf444de02010-05-05 15:25:02 +02009667 {
9668 .cmd = NL80211_CMD_SET_CHANNEL,
9669 .doit = nl80211_set_channel,
9670 .policy = nl80211_policy,
9671 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009672 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9673 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009674 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009675 {
9676 .cmd = NL80211_CMD_SET_WDS_PEER,
9677 .doit = nl80211_set_wds_peer,
9678 .policy = nl80211_policy,
9679 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009680 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9681 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009682 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009683 {
9684 .cmd = NL80211_CMD_JOIN_MESH,
9685 .doit = nl80211_join_mesh,
9686 .policy = nl80211_policy,
9687 .flags = GENL_ADMIN_PERM,
9688 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9689 NL80211_FLAG_NEED_RTNL,
9690 },
9691 {
9692 .cmd = NL80211_CMD_LEAVE_MESH,
9693 .doit = nl80211_leave_mesh,
9694 .policy = nl80211_policy,
9695 .flags = GENL_ADMIN_PERM,
9696 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9697 NL80211_FLAG_NEED_RTNL,
9698 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009699#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009700 {
9701 .cmd = NL80211_CMD_GET_WOWLAN,
9702 .doit = nl80211_get_wowlan,
9703 .policy = nl80211_policy,
9704 /* can be retrieved by unprivileged users */
9705 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9706 NL80211_FLAG_NEED_RTNL,
9707 },
9708 {
9709 .cmd = NL80211_CMD_SET_WOWLAN,
9710 .doit = nl80211_set_wowlan,
9711 .policy = nl80211_policy,
9712 .flags = GENL_ADMIN_PERM,
9713 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9714 NL80211_FLAG_NEED_RTNL,
9715 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009716#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009717 {
9718 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9719 .doit = nl80211_set_rekey_data,
9720 .policy = nl80211_policy,
9721 .flags = GENL_ADMIN_PERM,
9722 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9723 NL80211_FLAG_NEED_RTNL,
9724 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009725 {
9726 .cmd = NL80211_CMD_TDLS_MGMT,
9727 .doit = nl80211_tdls_mgmt,
9728 .policy = nl80211_policy,
9729 .flags = GENL_ADMIN_PERM,
9730 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9731 NL80211_FLAG_NEED_RTNL,
9732 },
9733 {
9734 .cmd = NL80211_CMD_TDLS_OPER,
9735 .doit = nl80211_tdls_oper,
9736 .policy = nl80211_policy,
9737 .flags = GENL_ADMIN_PERM,
9738 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9739 NL80211_FLAG_NEED_RTNL,
9740 },
Johannes Berg28946da2011-11-04 11:18:12 +01009741 {
9742 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9743 .doit = nl80211_register_unexpected_frame,
9744 .policy = nl80211_policy,
9745 .flags = GENL_ADMIN_PERM,
9746 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9747 NL80211_FLAG_NEED_RTNL,
9748 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009749 {
9750 .cmd = NL80211_CMD_PROBE_CLIENT,
9751 .doit = nl80211_probe_client,
9752 .policy = nl80211_policy,
9753 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009754 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009755 NL80211_FLAG_NEED_RTNL,
9756 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009757 {
9758 .cmd = NL80211_CMD_REGISTER_BEACONS,
9759 .doit = nl80211_register_beacons,
9760 .policy = nl80211_policy,
9761 .flags = GENL_ADMIN_PERM,
9762 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9763 NL80211_FLAG_NEED_RTNL,
9764 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009765 {
9766 .cmd = NL80211_CMD_SET_NOACK_MAP,
9767 .doit = nl80211_set_noack_map,
9768 .policy = nl80211_policy,
9769 .flags = GENL_ADMIN_PERM,
9770 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9771 NL80211_FLAG_NEED_RTNL,
9772 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009773 {
9774 .cmd = NL80211_CMD_START_P2P_DEVICE,
9775 .doit = nl80211_start_p2p_device,
9776 .policy = nl80211_policy,
9777 .flags = GENL_ADMIN_PERM,
9778 .internal_flags = NL80211_FLAG_NEED_WDEV |
9779 NL80211_FLAG_NEED_RTNL,
9780 },
9781 {
9782 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9783 .doit = nl80211_stop_p2p_device,
9784 .policy = nl80211_policy,
9785 .flags = GENL_ADMIN_PERM,
9786 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9787 NL80211_FLAG_NEED_RTNL,
9788 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009789 {
9790 .cmd = NL80211_CMD_SET_MCAST_RATE,
9791 .doit = nl80211_set_mcast_rate,
9792 .policy = nl80211_policy,
9793 .flags = GENL_ADMIN_PERM,
9794 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9795 NL80211_FLAG_NEED_RTNL,
9796 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309797 {
9798 .cmd = NL80211_CMD_SET_MAC_ACL,
9799 .doit = nl80211_set_mac_acl,
9800 .policy = nl80211_policy,
9801 .flags = GENL_ADMIN_PERM,
9802 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9803 NL80211_FLAG_NEED_RTNL,
9804 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009805 {
9806 .cmd = NL80211_CMD_RADAR_DETECT,
9807 .doit = nl80211_start_radar_detection,
9808 .policy = nl80211_policy,
9809 .flags = GENL_ADMIN_PERM,
9810 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9811 NL80211_FLAG_NEED_RTNL,
9812 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009813 {
9814 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9815 .doit = nl80211_get_protocol_features,
9816 .policy = nl80211_policy,
9817 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009818 {
9819 .cmd = NL80211_CMD_UPDATE_FT_IES,
9820 .doit = nl80211_update_ft_ies,
9821 .policy = nl80211_policy,
9822 .flags = GENL_ADMIN_PERM,
9823 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9824 NL80211_FLAG_NEED_RTNL,
9825 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009826 {
9827 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9828 .doit = nl80211_crit_protocol_start,
9829 .policy = nl80211_policy,
9830 .flags = GENL_ADMIN_PERM,
9831 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9832 NL80211_FLAG_NEED_RTNL,
9833 },
9834 {
9835 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9836 .doit = nl80211_crit_protocol_stop,
9837 .policy = nl80211_policy,
9838 .flags = GENL_ADMIN_PERM,
9839 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9840 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009841 },
9842 {
9843 .cmd = NL80211_CMD_GET_COALESCE,
9844 .doit = nl80211_get_coalesce,
9845 .policy = nl80211_policy,
9846 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9847 NL80211_FLAG_NEED_RTNL,
9848 },
9849 {
9850 .cmd = NL80211_CMD_SET_COALESCE,
9851 .doit = nl80211_set_coalesce,
9852 .policy = nl80211_policy,
9853 .flags = GENL_ADMIN_PERM,
9854 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9855 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009856 },
9857 {
9858 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9859 .doit = nl80211_channel_switch,
9860 .policy = nl80211_policy,
9861 .flags = GENL_ADMIN_PERM,
9862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9863 NL80211_FLAG_NEED_RTNL,
9864 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009865 {
9866 .cmd = NL80211_CMD_VENDOR,
9867 .doit = nl80211_vendor_cmd,
9868 .policy = nl80211_policy,
9869 .flags = GENL_ADMIN_PERM,
9870 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9871 NL80211_FLAG_NEED_RTNL,
9872 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009873 {
9874 .cmd = NL80211_CMD_SET_QOS_MAP,
9875 .doit = nl80211_set_qos_map,
9876 .policy = nl80211_policy,
9877 .flags = GENL_ADMIN_PERM,
9878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9879 NL80211_FLAG_NEED_RTNL,
9880 },
Johannes Berg55682962007-09-20 13:09:35 -04009881};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009882
Johannes Berg55682962007-09-20 13:09:35 -04009883/* notification functions */
9884
9885void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9886{
9887 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +02009888 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04009889
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009890 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009891 if (!msg)
9892 return;
9893
Johannes Berg86e8cf92013-06-19 10:57:22 +02009894 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009895 nlmsg_free(msg);
9896 return;
9897 }
9898
Johannes Berg68eb5502013-11-19 15:19:38 +01009899 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01009900 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009901}
9902
Johannes Berg362a4152009-05-24 16:43:15 +02009903static int nl80211_add_scan_req(struct sk_buff *msg,
9904 struct cfg80211_registered_device *rdev)
9905{
9906 struct cfg80211_scan_request *req = rdev->scan_req;
9907 struct nlattr *nest;
9908 int i;
9909
9910 if (WARN_ON(!req))
9911 return 0;
9912
9913 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9914 if (!nest)
9915 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009916 for (i = 0; i < req->n_ssids; i++) {
9917 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9918 goto nla_put_failure;
9919 }
Johannes Berg362a4152009-05-24 16:43:15 +02009920 nla_nest_end(msg, nest);
9921
9922 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9923 if (!nest)
9924 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009925 for (i = 0; i < req->n_channels; i++) {
9926 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9927 goto nla_put_failure;
9928 }
Johannes Berg362a4152009-05-24 16:43:15 +02009929 nla_nest_end(msg, nest);
9930
David S. Miller9360ffd2012-03-29 04:41:26 -04009931 if (req->ie &&
9932 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9933 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009934
Johannes Bergae917c92013-10-25 11:05:22 +02009935 if (req->flags &&
9936 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
9937 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -07009938
Johannes Berg362a4152009-05-24 16:43:15 +02009939 return 0;
9940 nla_put_failure:
9941 return -ENOBUFS;
9942}
9943
Johannes Berga538e2d2009-06-16 19:56:42 +02009944static int nl80211_send_scan_msg(struct sk_buff *msg,
9945 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009946 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009947 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009948 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009949{
9950 void *hdr;
9951
Eric W. Biederman15e47302012-09-07 20:12:54 +00009952 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009953 if (!hdr)
9954 return -1;
9955
David S. Miller9360ffd2012-03-29 04:41:26 -04009956 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009957 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9958 wdev->netdev->ifindex)) ||
9959 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009960 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009961
Johannes Berg362a4152009-05-24 16:43:15 +02009962 /* ignore errors and send incomplete event anyway */
9963 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009964
9965 return genlmsg_end(msg, hdr);
9966
9967 nla_put_failure:
9968 genlmsg_cancel(msg, hdr);
9969 return -EMSGSIZE;
9970}
9971
Luciano Coelho807f8a82011-05-11 17:09:35 +03009972static int
9973nl80211_send_sched_scan_msg(struct sk_buff *msg,
9974 struct cfg80211_registered_device *rdev,
9975 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009976 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009977{
9978 void *hdr;
9979
Eric W. Biederman15e47302012-09-07 20:12:54 +00009980 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009981 if (!hdr)
9982 return -1;
9983
David S. Miller9360ffd2012-03-29 04:41:26 -04009984 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9985 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9986 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009987
9988 return genlmsg_end(msg, hdr);
9989
9990 nla_put_failure:
9991 genlmsg_cancel(msg, hdr);
9992 return -EMSGSIZE;
9993}
9994
Johannes Berga538e2d2009-06-16 19:56:42 +02009995void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009996 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009997{
9998 struct sk_buff *msg;
9999
Thomas Graf58050fc2012-06-28 03:57:45 +000010000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010001 if (!msg)
10002 return;
10003
Johannes Bergfd014282012-06-18 19:17:03 +020010004 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010005 NL80211_CMD_TRIGGER_SCAN) < 0) {
10006 nlmsg_free(msg);
10007 return;
10008 }
10009
Johannes Berg68eb5502013-11-19 15:19:38 +010010010 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010011 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010012}
10013
Johannes Berg2a519312009-02-10 21:25:55 +010010014void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010015 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +010010016{
10017 struct sk_buff *msg;
10018
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010019 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010020 if (!msg)
10021 return;
10022
Johannes Bergfd014282012-06-18 19:17:03 +020010023 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010024 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010025 nlmsg_free(msg);
10026 return;
10027 }
10028
Johannes Berg68eb5502013-11-19 15:19:38 +010010029 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010030 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010031}
10032
10033void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010034 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +010010035{
10036 struct sk_buff *msg;
10037
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010038 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010039 if (!msg)
10040 return;
10041
Johannes Bergfd014282012-06-18 19:17:03 +020010042 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010043 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010044 nlmsg_free(msg);
10045 return;
10046 }
10047
Johannes Berg68eb5502013-11-19 15:19:38 +010010048 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010049 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010050}
10051
Luciano Coelho807f8a82011-05-11 17:09:35 +030010052void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10053 struct net_device *netdev)
10054{
10055 struct sk_buff *msg;
10056
10057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10058 if (!msg)
10059 return;
10060
10061 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10062 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10063 nlmsg_free(msg);
10064 return;
10065 }
10066
Johannes Berg68eb5502013-11-19 15:19:38 +010010067 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010068 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010069}
10070
10071void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10072 struct net_device *netdev, u32 cmd)
10073{
10074 struct sk_buff *msg;
10075
Thomas Graf58050fc2012-06-28 03:57:45 +000010076 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010077 if (!msg)
10078 return;
10079
10080 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10081 nlmsg_free(msg);
10082 return;
10083 }
10084
Johannes Berg68eb5502013-11-19 15:19:38 +010010085 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010086 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010087}
10088
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010089/*
10090 * This can happen on global regulatory changes or device specific settings
10091 * based on custom world regulatory domains.
10092 */
10093void nl80211_send_reg_change_event(struct regulatory_request *request)
10094{
10095 struct sk_buff *msg;
10096 void *hdr;
10097
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010099 if (!msg)
10100 return;
10101
10102 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10103 if (!hdr) {
10104 nlmsg_free(msg);
10105 return;
10106 }
10107
10108 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010109 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10110 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010111
David S. Miller9360ffd2012-03-29 04:41:26 -040010112 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10113 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10114 NL80211_REGDOM_TYPE_WORLD))
10115 goto nla_put_failure;
10116 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10117 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10118 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10119 goto nla_put_failure;
10120 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10121 request->intersect) {
10122 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10123 NL80211_REGDOM_TYPE_INTERSECTION))
10124 goto nla_put_failure;
10125 } else {
10126 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10127 NL80211_REGDOM_TYPE_COUNTRY) ||
10128 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10129 request->alpha2))
10130 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010131 }
10132
Johannes Bergf4173762012-12-03 18:23:37 +010010133 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010134 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10135 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010136
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010137 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010138
Johannes Bergbc43b282009-07-25 10:54:13 +020010139 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010140 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010141 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010142 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010143
10144 return;
10145
10146nla_put_failure:
10147 genlmsg_cancel(msg, hdr);
10148 nlmsg_free(msg);
10149}
10150
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010151static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10152 struct net_device *netdev,
10153 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010154 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010155{
10156 struct sk_buff *msg;
10157 void *hdr;
10158
Johannes Berge6d6e342009-07-01 21:26:47 +020010159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010160 if (!msg)
10161 return;
10162
10163 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10164 if (!hdr) {
10165 nlmsg_free(msg);
10166 return;
10167 }
10168
David S. Miller9360ffd2012-03-29 04:41:26 -040010169 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10170 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10171 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10172 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010173
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010174 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010175
Johannes Berg68eb5502013-11-19 15:19:38 +010010176 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010177 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010178 return;
10179
10180 nla_put_failure:
10181 genlmsg_cancel(msg, hdr);
10182 nlmsg_free(msg);
10183}
10184
10185void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010186 struct net_device *netdev, const u8 *buf,
10187 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010188{
10189 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010190 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010191}
10192
10193void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10194 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010195 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010196{
Johannes Berge6d6e342009-07-01 21:26:47 +020010197 nl80211_send_mlme_event(rdev, netdev, buf, len,
10198 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010199}
10200
Jouni Malinen53b46b82009-03-27 20:53:56 +020010201void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010202 struct net_device *netdev, const u8 *buf,
10203 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010204{
10205 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010206 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010207}
10208
Jouni Malinen53b46b82009-03-27 20:53:56 +020010209void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10210 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010211 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010212{
10213 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010214 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010215}
10216
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010217void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10218 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010219{
Johannes Berg947add32013-02-22 22:05:20 +010010220 struct wireless_dev *wdev = dev->ieee80211_ptr;
10221 struct wiphy *wiphy = wdev->wiphy;
10222 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010223 const struct ieee80211_mgmt *mgmt = (void *)buf;
10224 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010225
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010226 if (WARN_ON(len < 2))
10227 return;
10228
10229 if (ieee80211_is_deauth(mgmt->frame_control))
10230 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10231 else
10232 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10233
10234 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10235 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010236}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010237EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010238
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010239static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10240 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010241 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010242{
10243 struct sk_buff *msg;
10244 void *hdr;
10245
Johannes Berge6d6e342009-07-01 21:26:47 +020010246 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010247 if (!msg)
10248 return;
10249
10250 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10251 if (!hdr) {
10252 nlmsg_free(msg);
10253 return;
10254 }
10255
David S. Miller9360ffd2012-03-29 04:41:26 -040010256 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10257 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10258 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10259 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10260 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010261
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010262 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010263
Johannes Berg68eb5502013-11-19 15:19:38 +010010264 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010265 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010266 return;
10267
10268 nla_put_failure:
10269 genlmsg_cancel(msg, hdr);
10270 nlmsg_free(msg);
10271}
10272
10273void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010274 struct net_device *netdev, const u8 *addr,
10275 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010276{
10277 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010278 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010279}
10280
10281void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010282 struct net_device *netdev, const u8 *addr,
10283 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010284{
Johannes Berge6d6e342009-07-01 21:26:47 +020010285 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10286 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010287}
10288
Samuel Ortizb23aa672009-07-01 21:26:54 +020010289void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10290 struct net_device *netdev, const u8 *bssid,
10291 const u8 *req_ie, size_t req_ie_len,
10292 const u8 *resp_ie, size_t resp_ie_len,
10293 u16 status, gfp_t gfp)
10294{
10295 struct sk_buff *msg;
10296 void *hdr;
10297
Thomas Graf58050fc2012-06-28 03:57:45 +000010298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010299 if (!msg)
10300 return;
10301
10302 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10303 if (!hdr) {
10304 nlmsg_free(msg);
10305 return;
10306 }
10307
David S. Miller9360ffd2012-03-29 04:41:26 -040010308 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10309 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10310 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10311 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10312 (req_ie &&
10313 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10314 (resp_ie &&
10315 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10316 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010317
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010318 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010319
Johannes Berg68eb5502013-11-19 15:19:38 +010010320 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010321 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010322 return;
10323
10324 nla_put_failure:
10325 genlmsg_cancel(msg, hdr);
10326 nlmsg_free(msg);
10327
10328}
10329
10330void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10331 struct net_device *netdev, const u8 *bssid,
10332 const u8 *req_ie, size_t req_ie_len,
10333 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10334{
10335 struct sk_buff *msg;
10336 void *hdr;
10337
Thomas Graf58050fc2012-06-28 03:57:45 +000010338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010339 if (!msg)
10340 return;
10341
10342 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10343 if (!hdr) {
10344 nlmsg_free(msg);
10345 return;
10346 }
10347
David S. Miller9360ffd2012-03-29 04:41:26 -040010348 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10349 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10350 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10351 (req_ie &&
10352 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10353 (resp_ie &&
10354 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10355 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010356
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010357 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010358
Johannes Berg68eb5502013-11-19 15:19:38 +010010359 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010360 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010361 return;
10362
10363 nla_put_failure:
10364 genlmsg_cancel(msg, hdr);
10365 nlmsg_free(msg);
10366
10367}
10368
10369void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10370 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010371 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010372{
10373 struct sk_buff *msg;
10374 void *hdr;
10375
Thomas Graf58050fc2012-06-28 03:57:45 +000010376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010377 if (!msg)
10378 return;
10379
10380 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10381 if (!hdr) {
10382 nlmsg_free(msg);
10383 return;
10384 }
10385
David S. Miller9360ffd2012-03-29 04:41:26 -040010386 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10387 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10388 (from_ap && reason &&
10389 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10390 (from_ap &&
10391 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10392 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10393 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010394
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010395 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010396
Johannes Berg68eb5502013-11-19 15:19:38 +010010397 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010398 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010399 return;
10400
10401 nla_put_failure:
10402 genlmsg_cancel(msg, hdr);
10403 nlmsg_free(msg);
10404
10405}
10406
Johannes Berg04a773a2009-04-19 21:24:32 +020010407void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10408 struct net_device *netdev, const u8 *bssid,
10409 gfp_t gfp)
10410{
10411 struct sk_buff *msg;
10412 void *hdr;
10413
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010415 if (!msg)
10416 return;
10417
10418 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10419 if (!hdr) {
10420 nlmsg_free(msg);
10421 return;
10422 }
10423
David S. Miller9360ffd2012-03-29 04:41:26 -040010424 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10425 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10426 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10427 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010428
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010429 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010430
Johannes Berg68eb5502013-11-19 15:19:38 +010010431 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010432 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010433 return;
10434
10435 nla_put_failure:
10436 genlmsg_cancel(msg, hdr);
10437 nlmsg_free(msg);
10438}
10439
Johannes Berg947add32013-02-22 22:05:20 +010010440void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10441 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010442{
Johannes Berg947add32013-02-22 22:05:20 +010010443 struct wireless_dev *wdev = dev->ieee80211_ptr;
10444 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010445 struct sk_buff *msg;
10446 void *hdr;
10447
Johannes Berg947add32013-02-22 22:05:20 +010010448 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10449 return;
10450
10451 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10452
Javier Cardonac93b5e72011-04-07 15:08:34 -070010453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10454 if (!msg)
10455 return;
10456
10457 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10458 if (!hdr) {
10459 nlmsg_free(msg);
10460 return;
10461 }
10462
David S. Miller9360ffd2012-03-29 04:41:26 -040010463 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010464 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10465 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010466 (ie_len && ie &&
10467 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10468 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010469
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010470 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010471
Johannes Berg68eb5502013-11-19 15:19:38 +010010472 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010473 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010474 return;
10475
10476 nla_put_failure:
10477 genlmsg_cancel(msg, hdr);
10478 nlmsg_free(msg);
10479}
Johannes Berg947add32013-02-22 22:05:20 +010010480EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010481
Jouni Malinena3b8b052009-03-27 21:59:49 +020010482void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10483 struct net_device *netdev, const u8 *addr,
10484 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010485 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010486{
10487 struct sk_buff *msg;
10488 void *hdr;
10489
Johannes Berge6d6e342009-07-01 21:26:47 +020010490 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010491 if (!msg)
10492 return;
10493
10494 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10495 if (!hdr) {
10496 nlmsg_free(msg);
10497 return;
10498 }
10499
David S. Miller9360ffd2012-03-29 04:41:26 -040010500 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10501 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10502 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10503 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10504 (key_id != -1 &&
10505 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10506 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10507 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010508
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010509 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010510
Johannes Berg68eb5502013-11-19 15:19:38 +010010511 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010512 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010513 return;
10514
10515 nla_put_failure:
10516 genlmsg_cancel(msg, hdr);
10517 nlmsg_free(msg);
10518}
10519
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010520void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10521 struct ieee80211_channel *channel_before,
10522 struct ieee80211_channel *channel_after)
10523{
10524 struct sk_buff *msg;
10525 void *hdr;
10526 struct nlattr *nl_freq;
10527
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010529 if (!msg)
10530 return;
10531
10532 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10533 if (!hdr) {
10534 nlmsg_free(msg);
10535 return;
10536 }
10537
10538 /*
10539 * Since we are applying the beacon hint to a wiphy we know its
10540 * wiphy_idx is valid
10541 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010542 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10543 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010544
10545 /* Before */
10546 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10547 if (!nl_freq)
10548 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010549 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010550 goto nla_put_failure;
10551 nla_nest_end(msg, nl_freq);
10552
10553 /* After */
10554 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10555 if (!nl_freq)
10556 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010557 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010558 goto nla_put_failure;
10559 nla_nest_end(msg, nl_freq);
10560
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010561 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010562
Johannes Berg463d0182009-07-14 00:33:35 +020010563 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010564 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010565 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010566 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010567
10568 return;
10569
10570nla_put_failure:
10571 genlmsg_cancel(msg, hdr);
10572 nlmsg_free(msg);
10573}
10574
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010575static void nl80211_send_remain_on_chan_event(
10576 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010577 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010578 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010579 unsigned int duration, gfp_t gfp)
10580{
10581 struct sk_buff *msg;
10582 void *hdr;
10583
10584 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10585 if (!msg)
10586 return;
10587
10588 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10589 if (!hdr) {
10590 nlmsg_free(msg);
10591 return;
10592 }
10593
David S. Miller9360ffd2012-03-29 04:41:26 -040010594 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010595 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10596 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010597 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010598 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010599 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10600 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010601 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10602 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010603
David S. Miller9360ffd2012-03-29 04:41:26 -040010604 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10605 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10606 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010607
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010608 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010609
Johannes Berg68eb5502013-11-19 15:19:38 +010010610 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010611 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010612 return;
10613
10614 nla_put_failure:
10615 genlmsg_cancel(msg, hdr);
10616 nlmsg_free(msg);
10617}
10618
Johannes Berg947add32013-02-22 22:05:20 +010010619void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10620 struct ieee80211_channel *chan,
10621 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010622{
Johannes Berg947add32013-02-22 22:05:20 +010010623 struct wiphy *wiphy = wdev->wiphy;
10624 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10625
10626 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010627 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010628 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010629 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010630}
Johannes Berg947add32013-02-22 22:05:20 +010010631EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010632
Johannes Berg947add32013-02-22 22:05:20 +010010633void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10634 struct ieee80211_channel *chan,
10635 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010636{
Johannes Berg947add32013-02-22 22:05:20 +010010637 struct wiphy *wiphy = wdev->wiphy;
10638 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10639
10640 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010641 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010642 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010643}
Johannes Berg947add32013-02-22 22:05:20 +010010644EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010645
Johannes Berg947add32013-02-22 22:05:20 +010010646void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10647 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010648{
Johannes Berg947add32013-02-22 22:05:20 +010010649 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10650 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010651 struct sk_buff *msg;
10652
Johannes Berg947add32013-02-22 22:05:20 +010010653 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10654
Thomas Graf58050fc2012-06-28 03:57:45 +000010655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010656 if (!msg)
10657 return;
10658
John W. Linville66266b32012-03-15 13:25:41 -040010659 if (nl80211_send_station(msg, 0, 0, 0,
10660 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010661 nlmsg_free(msg);
10662 return;
10663 }
10664
Johannes Berg68eb5502013-11-19 15:19:38 +010010665 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010666 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010667}
Johannes Berg947add32013-02-22 22:05:20 +010010668EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010669
Johannes Berg947add32013-02-22 22:05:20 +010010670void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010671{
Johannes Berg947add32013-02-22 22:05:20 +010010672 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10673 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010674 struct sk_buff *msg;
10675 void *hdr;
10676
Johannes Berg947add32013-02-22 22:05:20 +010010677 trace_cfg80211_del_sta(dev, mac_addr);
10678
Thomas Graf58050fc2012-06-28 03:57:45 +000010679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010680 if (!msg)
10681 return;
10682
10683 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10684 if (!hdr) {
10685 nlmsg_free(msg);
10686 return;
10687 }
10688
David S. Miller9360ffd2012-03-29 04:41:26 -040010689 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10690 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10691 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010692
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010693 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010694
Johannes Berg68eb5502013-11-19 15:19:38 +010010695 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010696 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010697 return;
10698
10699 nla_put_failure:
10700 genlmsg_cancel(msg, hdr);
10701 nlmsg_free(msg);
10702}
Johannes Berg947add32013-02-22 22:05:20 +010010703EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010704
Johannes Berg947add32013-02-22 22:05:20 +010010705void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10706 enum nl80211_connect_failed_reason reason,
10707 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010708{
Johannes Berg947add32013-02-22 22:05:20 +010010709 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10710 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010711 struct sk_buff *msg;
10712 void *hdr;
10713
10714 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10715 if (!msg)
10716 return;
10717
10718 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10719 if (!hdr) {
10720 nlmsg_free(msg);
10721 return;
10722 }
10723
10724 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10725 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10726 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10727 goto nla_put_failure;
10728
10729 genlmsg_end(msg, hdr);
10730
Johannes Berg68eb5502013-11-19 15:19:38 +010010731 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010732 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010733 return;
10734
10735 nla_put_failure:
10736 genlmsg_cancel(msg, hdr);
10737 nlmsg_free(msg);
10738}
Johannes Berg947add32013-02-22 22:05:20 +010010739EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010740
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010741static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10742 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010743{
10744 struct wireless_dev *wdev = dev->ieee80211_ptr;
10745 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10746 struct sk_buff *msg;
10747 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010748 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010749
Eric W. Biederman15e47302012-09-07 20:12:54 +000010750 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010751 return false;
10752
10753 msg = nlmsg_new(100, gfp);
10754 if (!msg)
10755 return true;
10756
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010757 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010758 if (!hdr) {
10759 nlmsg_free(msg);
10760 return true;
10761 }
10762
David S. Miller9360ffd2012-03-29 04:41:26 -040010763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10764 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10765 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10766 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010767
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010768 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010769 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010770 return true;
10771
10772 nla_put_failure:
10773 genlmsg_cancel(msg, hdr);
10774 nlmsg_free(msg);
10775 return true;
10776}
10777
Johannes Berg947add32013-02-22 22:05:20 +010010778bool cfg80211_rx_spurious_frame(struct net_device *dev,
10779 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010780{
Johannes Berg947add32013-02-22 22:05:20 +010010781 struct wireless_dev *wdev = dev->ieee80211_ptr;
10782 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010783
Johannes Berg947add32013-02-22 22:05:20 +010010784 trace_cfg80211_rx_spurious_frame(dev, addr);
10785
10786 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10787 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10788 trace_cfg80211_return_bool(false);
10789 return false;
10790 }
10791 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10792 addr, gfp);
10793 trace_cfg80211_return_bool(ret);
10794 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010795}
Johannes Berg947add32013-02-22 22:05:20 +010010796EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10797
10798bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10799 const u8 *addr, gfp_t gfp)
10800{
10801 struct wireless_dev *wdev = dev->ieee80211_ptr;
10802 bool ret;
10803
10804 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10805
10806 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10807 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10808 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10809 trace_cfg80211_return_bool(false);
10810 return false;
10811 }
10812 ret = __nl80211_unexpected_frame(dev,
10813 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10814 addr, gfp);
10815 trace_cfg80211_return_bool(ret);
10816 return ret;
10817}
10818EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010819
Johannes Berg2e161f72010-08-12 15:38:38 +020010820int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010821 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010822 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010823 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010824{
Johannes Berg71bbc992012-06-15 15:30:18 +020010825 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010826 struct sk_buff *msg;
10827 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010828
10829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10830 if (!msg)
10831 return -ENOMEM;
10832
Johannes Berg2e161f72010-08-12 15:38:38 +020010833 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010834 if (!hdr) {
10835 nlmsg_free(msg);
10836 return -ENOMEM;
10837 }
10838
David S. Miller9360ffd2012-03-29 04:41:26 -040010839 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010840 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10841 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010842 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010843 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10844 (sig_dbm &&
10845 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010846 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10847 (flags &&
10848 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010849 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010850
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010851 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010852
Eric W. Biederman15e47302012-09-07 20:12:54 +000010853 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010854
10855 nla_put_failure:
10856 genlmsg_cancel(msg, hdr);
10857 nlmsg_free(msg);
10858 return -ENOBUFS;
10859}
10860
Johannes Berg947add32013-02-22 22:05:20 +010010861void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10862 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010863{
Johannes Berg947add32013-02-22 22:05:20 +010010864 struct wiphy *wiphy = wdev->wiphy;
10865 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010866 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010867 struct sk_buff *msg;
10868 void *hdr;
10869
Johannes Berg947add32013-02-22 22:05:20 +010010870 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10871
Jouni Malinen026331c2010-02-15 12:53:10 +020010872 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10873 if (!msg)
10874 return;
10875
Johannes Berg2e161f72010-08-12 15:38:38 +020010876 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010877 if (!hdr) {
10878 nlmsg_free(msg);
10879 return;
10880 }
10881
David S. Miller9360ffd2012-03-29 04:41:26 -040010882 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010883 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10884 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010885 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010886 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10887 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10888 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10889 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010890
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010891 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010892
Johannes Berg68eb5502013-11-19 15:19:38 +010010893 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010894 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010895 return;
10896
10897 nla_put_failure:
10898 genlmsg_cancel(msg, hdr);
10899 nlmsg_free(msg);
10900}
Johannes Berg947add32013-02-22 22:05:20 +010010901EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010902
Johannes Berg947add32013-02-22 22:05:20 +010010903void cfg80211_cqm_rssi_notify(struct net_device *dev,
10904 enum nl80211_cqm_rssi_threshold_event rssi_event,
10905 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010906{
Johannes Berg947add32013-02-22 22:05:20 +010010907 struct wireless_dev *wdev = dev->ieee80211_ptr;
10908 struct wiphy *wiphy = wdev->wiphy;
10909 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010910 struct sk_buff *msg;
10911 struct nlattr *pinfoattr;
10912 void *hdr;
10913
Johannes Berg947add32013-02-22 22:05:20 +010010914 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10915
Thomas Graf58050fc2012-06-28 03:57:45 +000010916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010917 if (!msg)
10918 return;
10919
10920 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10921 if (!hdr) {
10922 nlmsg_free(msg);
10923 return;
10924 }
10925
David S. Miller9360ffd2012-03-29 04:41:26 -040010926 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010927 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010928 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010929
10930 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10931 if (!pinfoattr)
10932 goto nla_put_failure;
10933
David S. Miller9360ffd2012-03-29 04:41:26 -040010934 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10935 rssi_event))
10936 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010937
10938 nla_nest_end(msg, pinfoattr);
10939
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010940 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010941
Johannes Berg68eb5502013-11-19 15:19:38 +010010942 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010943 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010944 return;
10945
10946 nla_put_failure:
10947 genlmsg_cancel(msg, hdr);
10948 nlmsg_free(msg);
10949}
Johannes Berg947add32013-02-22 22:05:20 +010010950EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010951
Johannes Berg947add32013-02-22 22:05:20 +010010952static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10953 struct net_device *netdev, const u8 *bssid,
10954 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010955{
10956 struct sk_buff *msg;
10957 struct nlattr *rekey_attr;
10958 void *hdr;
10959
Thomas Graf58050fc2012-06-28 03:57:45 +000010960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010961 if (!msg)
10962 return;
10963
10964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10965 if (!hdr) {
10966 nlmsg_free(msg);
10967 return;
10968 }
10969
David S. Miller9360ffd2012-03-29 04:41:26 -040010970 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10971 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10972 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10973 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010974
10975 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10976 if (!rekey_attr)
10977 goto nla_put_failure;
10978
David S. Miller9360ffd2012-03-29 04:41:26 -040010979 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10980 NL80211_REPLAY_CTR_LEN, replay_ctr))
10981 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010982
10983 nla_nest_end(msg, rekey_attr);
10984
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010985 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010986
Johannes Berg68eb5502013-11-19 15:19:38 +010010987 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010988 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010989 return;
10990
10991 nla_put_failure:
10992 genlmsg_cancel(msg, hdr);
10993 nlmsg_free(msg);
10994}
10995
Johannes Berg947add32013-02-22 22:05:20 +010010996void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10997 const u8 *replay_ctr, gfp_t gfp)
10998{
10999 struct wireless_dev *wdev = dev->ieee80211_ptr;
11000 struct wiphy *wiphy = wdev->wiphy;
11001 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11002
11003 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11004 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11005}
11006EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11007
11008static void
11009nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11010 struct net_device *netdev, int index,
11011 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011012{
11013 struct sk_buff *msg;
11014 struct nlattr *attr;
11015 void *hdr;
11016
Thomas Graf58050fc2012-06-28 03:57:45 +000011017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011018 if (!msg)
11019 return;
11020
11021 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11022 if (!hdr) {
11023 nlmsg_free(msg);
11024 return;
11025 }
11026
David S. Miller9360ffd2012-03-29 04:41:26 -040011027 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11028 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11029 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011030
11031 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11032 if (!attr)
11033 goto nla_put_failure;
11034
David S. Miller9360ffd2012-03-29 04:41:26 -040011035 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11036 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11037 (preauth &&
11038 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11039 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011040
11041 nla_nest_end(msg, attr);
11042
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011043 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011044
Johannes Berg68eb5502013-11-19 15:19:38 +010011045 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011046 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011047 return;
11048
11049 nla_put_failure:
11050 genlmsg_cancel(msg, hdr);
11051 nlmsg_free(msg);
11052}
11053
Johannes Berg947add32013-02-22 22:05:20 +010011054void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11055 const u8 *bssid, bool preauth, gfp_t gfp)
11056{
11057 struct wireless_dev *wdev = dev->ieee80211_ptr;
11058 struct wiphy *wiphy = wdev->wiphy;
11059 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11060
11061 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11062 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11063}
11064EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11065
11066static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11067 struct net_device *netdev,
11068 struct cfg80211_chan_def *chandef,
11069 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011070{
11071 struct sk_buff *msg;
11072 void *hdr;
11073
Thomas Graf58050fc2012-06-28 03:57:45 +000011074 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011075 if (!msg)
11076 return;
11077
11078 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11079 if (!hdr) {
11080 nlmsg_free(msg);
11081 return;
11082 }
11083
Johannes Berg683b6d32012-11-08 21:25:48 +010011084 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11085 goto nla_put_failure;
11086
11087 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011088 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011089
11090 genlmsg_end(msg, hdr);
11091
Johannes Berg68eb5502013-11-19 15:19:38 +010011092 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011093 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011094 return;
11095
11096 nla_put_failure:
11097 genlmsg_cancel(msg, hdr);
11098 nlmsg_free(msg);
11099}
11100
Johannes Berg947add32013-02-22 22:05:20 +010011101void cfg80211_ch_switch_notify(struct net_device *dev,
11102 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011103{
Johannes Berg947add32013-02-22 22:05:20 +010011104 struct wireless_dev *wdev = dev->ieee80211_ptr;
11105 struct wiphy *wiphy = wdev->wiphy;
11106 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11107
Simon Wunderliche487eae2013-11-21 18:19:51 +010011108 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011109
Simon Wunderliche487eae2013-11-21 18:19:51 +010011110 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011111
11112 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011113 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011114 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11115 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011116 return;
Johannes Berg947add32013-02-22 22:05:20 +010011117
11118 wdev->channel = chandef->chan;
11119 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011120}
11121EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11122
11123void cfg80211_cqm_txe_notify(struct net_device *dev,
11124 const u8 *peer, u32 num_packets,
11125 u32 rate, u32 intvl, gfp_t gfp)
11126{
11127 struct wireless_dev *wdev = dev->ieee80211_ptr;
11128 struct wiphy *wiphy = wdev->wiphy;
11129 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011130 struct sk_buff *msg;
11131 struct nlattr *pinfoattr;
11132 void *hdr;
11133
11134 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11135 if (!msg)
11136 return;
11137
11138 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11139 if (!hdr) {
11140 nlmsg_free(msg);
11141 return;
11142 }
11143
11144 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011145 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011146 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11147 goto nla_put_failure;
11148
11149 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11150 if (!pinfoattr)
11151 goto nla_put_failure;
11152
11153 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11154 goto nla_put_failure;
11155
11156 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11157 goto nla_put_failure;
11158
11159 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11160 goto nla_put_failure;
11161
11162 nla_nest_end(msg, pinfoattr);
11163
11164 genlmsg_end(msg, hdr);
11165
Johannes Berg68eb5502013-11-19 15:19:38 +010011166 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011167 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011168 return;
11169
11170 nla_put_failure:
11171 genlmsg_cancel(msg, hdr);
11172 nlmsg_free(msg);
11173}
Johannes Berg947add32013-02-22 22:05:20 +010011174EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011175
11176void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011177nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011178 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011179 enum nl80211_radar_event event,
11180 struct net_device *netdev, gfp_t gfp)
11181{
11182 struct sk_buff *msg;
11183 void *hdr;
11184
11185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11186 if (!msg)
11187 return;
11188
11189 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11190 if (!hdr) {
11191 nlmsg_free(msg);
11192 return;
11193 }
11194
11195 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11196 goto nla_put_failure;
11197
11198 /* NOP and radar events don't need a netdev parameter */
11199 if (netdev) {
11200 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11201
11202 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11203 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11204 goto nla_put_failure;
11205 }
11206
11207 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11208 goto nla_put_failure;
11209
11210 if (nl80211_send_chandef(msg, chandef))
11211 goto nla_put_failure;
11212
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011213 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011214
Johannes Berg68eb5502013-11-19 15:19:38 +010011215 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011216 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011217 return;
11218
11219 nla_put_failure:
11220 genlmsg_cancel(msg, hdr);
11221 nlmsg_free(msg);
11222}
11223
Johannes Berg947add32013-02-22 22:05:20 +010011224void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11225 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011226{
Johannes Berg947add32013-02-22 22:05:20 +010011227 struct wireless_dev *wdev = dev->ieee80211_ptr;
11228 struct wiphy *wiphy = wdev->wiphy;
11229 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011230 struct sk_buff *msg;
11231 struct nlattr *pinfoattr;
11232 void *hdr;
11233
Johannes Berg947add32013-02-22 22:05:20 +010011234 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11235
Thomas Graf58050fc2012-06-28 03:57:45 +000011236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011237 if (!msg)
11238 return;
11239
11240 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11241 if (!hdr) {
11242 nlmsg_free(msg);
11243 return;
11244 }
11245
David S. Miller9360ffd2012-03-29 04:41:26 -040011246 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011247 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011248 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11249 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011250
11251 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11252 if (!pinfoattr)
11253 goto nla_put_failure;
11254
David S. Miller9360ffd2012-03-29 04:41:26 -040011255 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11256 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011257
11258 nla_nest_end(msg, pinfoattr);
11259
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011260 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011261
Johannes Berg68eb5502013-11-19 15:19:38 +010011262 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011263 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011264 return;
11265
11266 nla_put_failure:
11267 genlmsg_cancel(msg, hdr);
11268 nlmsg_free(msg);
11269}
Johannes Berg947add32013-02-22 22:05:20 +010011270EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011271
Johannes Berg7f6cf312011-11-04 11:18:15 +010011272void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11273 u64 cookie, bool acked, gfp_t gfp)
11274{
11275 struct wireless_dev *wdev = dev->ieee80211_ptr;
11276 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11277 struct sk_buff *msg;
11278 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011279
Beni Lev4ee3e062012-08-27 12:49:39 +030011280 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11281
Thomas Graf58050fc2012-06-28 03:57:45 +000011282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011283
Johannes Berg7f6cf312011-11-04 11:18:15 +010011284 if (!msg)
11285 return;
11286
11287 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11288 if (!hdr) {
11289 nlmsg_free(msg);
11290 return;
11291 }
11292
David S. Miller9360ffd2012-03-29 04:41:26 -040011293 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11294 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11295 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11296 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11297 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11298 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011299
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011300 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011301
Johannes Berg68eb5502013-11-19 15:19:38 +010011302 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011303 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011304 return;
11305
11306 nla_put_failure:
11307 genlmsg_cancel(msg, hdr);
11308 nlmsg_free(msg);
11309}
11310EXPORT_SYMBOL(cfg80211_probe_status);
11311
Johannes Berg5e7602302011-11-04 11:18:17 +010011312void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11313 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011314 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011315{
11316 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11317 struct sk_buff *msg;
11318 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011319 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011320
Beni Lev4ee3e062012-08-27 12:49:39 +030011321 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11322
Ben Greear37c73b52012-10-26 14:49:25 -070011323 spin_lock_bh(&rdev->beacon_registrations_lock);
11324 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11325 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11326 if (!msg) {
11327 spin_unlock_bh(&rdev->beacon_registrations_lock);
11328 return;
11329 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011330
Ben Greear37c73b52012-10-26 14:49:25 -070011331 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11332 if (!hdr)
11333 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011334
Ben Greear37c73b52012-10-26 14:49:25 -070011335 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11336 (freq &&
11337 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11338 (sig_dbm &&
11339 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11340 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11341 goto nla_put_failure;
11342
11343 genlmsg_end(msg, hdr);
11344
11345 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011346 }
Ben Greear37c73b52012-10-26 14:49:25 -070011347 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011348 return;
11349
11350 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011351 spin_unlock_bh(&rdev->beacon_registrations_lock);
11352 if (hdr)
11353 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011354 nlmsg_free(msg);
11355}
11356EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11357
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011358#ifdef CONFIG_PM
11359void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11360 struct cfg80211_wowlan_wakeup *wakeup,
11361 gfp_t gfp)
11362{
11363 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11364 struct sk_buff *msg;
11365 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011366 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011367
11368 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11369
11370 if (wakeup)
11371 size += wakeup->packet_present_len;
11372
11373 msg = nlmsg_new(size, gfp);
11374 if (!msg)
11375 return;
11376
11377 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11378 if (!hdr)
11379 goto free_msg;
11380
11381 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11382 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11383 goto free_msg;
11384
11385 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11386 wdev->netdev->ifindex))
11387 goto free_msg;
11388
11389 if (wakeup) {
11390 struct nlattr *reasons;
11391
11392 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011393 if (!reasons)
11394 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011395
11396 if (wakeup->disconnect &&
11397 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11398 goto free_msg;
11399 if (wakeup->magic_pkt &&
11400 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11401 goto free_msg;
11402 if (wakeup->gtk_rekey_failure &&
11403 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11404 goto free_msg;
11405 if (wakeup->eap_identity_req &&
11406 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11407 goto free_msg;
11408 if (wakeup->four_way_handshake &&
11409 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11410 goto free_msg;
11411 if (wakeup->rfkill_release &&
11412 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11413 goto free_msg;
11414
11415 if (wakeup->pattern_idx >= 0 &&
11416 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11417 wakeup->pattern_idx))
11418 goto free_msg;
11419
Johannes Bergae917c92013-10-25 11:05:22 +020011420 if (wakeup->tcp_match &&
11421 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11422 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011423
Johannes Bergae917c92013-10-25 11:05:22 +020011424 if (wakeup->tcp_connlost &&
11425 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11426 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011427
Johannes Bergae917c92013-10-25 11:05:22 +020011428 if (wakeup->tcp_nomoretokens &&
11429 nla_put_flag(msg,
11430 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11431 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011432
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011433 if (wakeup->packet) {
11434 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11435 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11436
11437 if (!wakeup->packet_80211) {
11438 pkt_attr =
11439 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11440 len_attr =
11441 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11442 }
11443
11444 if (wakeup->packet_len &&
11445 nla_put_u32(msg, len_attr, wakeup->packet_len))
11446 goto free_msg;
11447
11448 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11449 wakeup->packet))
11450 goto free_msg;
11451 }
11452
11453 nla_nest_end(msg, reasons);
11454 }
11455
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011456 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011457
Johannes Berg68eb5502013-11-19 15:19:38 +010011458 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011459 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011460 return;
11461
11462 free_msg:
11463 nlmsg_free(msg);
11464}
11465EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11466#endif
11467
Jouni Malinen3475b092012-11-16 22:49:57 +020011468void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11469 enum nl80211_tdls_operation oper,
11470 u16 reason_code, gfp_t gfp)
11471{
11472 struct wireless_dev *wdev = dev->ieee80211_ptr;
11473 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11474 struct sk_buff *msg;
11475 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011476
11477 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11478 reason_code);
11479
11480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11481 if (!msg)
11482 return;
11483
11484 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11485 if (!hdr) {
11486 nlmsg_free(msg);
11487 return;
11488 }
11489
11490 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11491 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11492 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11493 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11494 (reason_code > 0 &&
11495 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11496 goto nla_put_failure;
11497
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011498 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011499
Johannes Berg68eb5502013-11-19 15:19:38 +010011500 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011501 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011502 return;
11503
11504 nla_put_failure:
11505 genlmsg_cancel(msg, hdr);
11506 nlmsg_free(msg);
11507}
11508EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11509
Jouni Malinen026331c2010-02-15 12:53:10 +020011510static int nl80211_netlink_notify(struct notifier_block * nb,
11511 unsigned long state,
11512 void *_notify)
11513{
11514 struct netlink_notify *notify = _notify;
11515 struct cfg80211_registered_device *rdev;
11516 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011517 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011518
11519 if (state != NETLINK_URELEASE)
11520 return NOTIFY_DONE;
11521
11522 rcu_read_lock();
11523
Johannes Berg5e7602302011-11-04 11:18:17 +010011524 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011525 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011526 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011527
11528 spin_lock_bh(&rdev->beacon_registrations_lock);
11529 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11530 list) {
11531 if (reg->nlportid == notify->portid) {
11532 list_del(&reg->list);
11533 kfree(reg);
11534 break;
11535 }
11536 }
11537 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011538 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011539
11540 rcu_read_unlock();
11541
11542 return NOTIFY_DONE;
11543}
11544
11545static struct notifier_block nl80211_netlink_notifier = {
11546 .notifier_call = nl80211_netlink_notify,
11547};
11548
Jouni Malinen355199e2013-02-27 17:14:27 +020011549void cfg80211_ft_event(struct net_device *netdev,
11550 struct cfg80211_ft_event_params *ft_event)
11551{
11552 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11553 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11554 struct sk_buff *msg;
11555 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011556
11557 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11558
11559 if (!ft_event->target_ap)
11560 return;
11561
11562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11563 if (!msg)
11564 return;
11565
11566 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011567 if (!hdr)
11568 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011569
Johannes Bergae917c92013-10-25 11:05:22 +020011570 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11571 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11572 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11573 goto out;
11574
11575 if (ft_event->ies &&
11576 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11577 goto out;
11578 if (ft_event->ric_ies &&
11579 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11580 ft_event->ric_ies))
11581 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011582
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011583 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011584
Johannes Berg68eb5502013-11-19 15:19:38 +010011585 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011586 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011587 return;
11588 out:
11589 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011590}
11591EXPORT_SYMBOL(cfg80211_ft_event);
11592
Arend van Spriel5de17982013-04-18 15:49:00 +020011593void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11594{
11595 struct cfg80211_registered_device *rdev;
11596 struct sk_buff *msg;
11597 void *hdr;
11598 u32 nlportid;
11599
11600 rdev = wiphy_to_dev(wdev->wiphy);
11601 if (!rdev->crit_proto_nlportid)
11602 return;
11603
11604 nlportid = rdev->crit_proto_nlportid;
11605 rdev->crit_proto_nlportid = 0;
11606
11607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11608 if (!msg)
11609 return;
11610
11611 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11612 if (!hdr)
11613 goto nla_put_failure;
11614
11615 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11616 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11617 goto nla_put_failure;
11618
11619 genlmsg_end(msg, hdr);
11620
11621 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11622 return;
11623
11624 nla_put_failure:
11625 if (hdr)
11626 genlmsg_cancel(msg, hdr);
11627 nlmsg_free(msg);
11628
11629}
11630EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11631
Johannes Berg55682962007-09-20 13:09:35 -040011632/* initialisation/exit functions */
11633
11634int nl80211_init(void)
11635{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011636 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011637
Johannes Berg2a94fe42013-11-19 15:19:39 +010011638 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11639 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011640 if (err)
11641 return err;
11642
Jouni Malinen026331c2010-02-15 12:53:10 +020011643 err = netlink_register_notifier(&nl80211_netlink_notifier);
11644 if (err)
11645 goto err_out;
11646
Johannes Berg55682962007-09-20 13:09:35 -040011647 return 0;
11648 err_out:
11649 genl_unregister_family(&nl80211_fam);
11650 return err;
11651}
11652
11653void nl80211_exit(void)
11654{
Jouni Malinen026331c2010-02-15 12:53:10 +020011655 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011656 genl_unregister_family(&nl80211_fam);
11657}