blob: 4fe2e6e2bc7635daef9aee5752ed97eafb29012a [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) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001722 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001723 cb->min_dump_alloc < 4096) {
1724 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001725 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001726 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001727 return 1;
1728 }
1729 idx--;
1730 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001731 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001732 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001733 break;
Johannes Berg55682962007-09-20 13:09:35 -04001734 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001735 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001736
Johannes Berg86e8cf92013-06-19 10:57:22 +02001737 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001738
1739 return skb->len;
1740}
1741
Johannes Berg86e8cf92013-06-19 10:57:22 +02001742static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1743{
1744 kfree((void *)cb->args[0]);
1745 return 0;
1746}
1747
Johannes Berg55682962007-09-20 13:09:35 -04001748static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1749{
1750 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001751 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001752 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001753
Johannes Berg645e77d2013-03-01 14:03:49 +01001754 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001755 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001756 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001757
Johannes Berg3713b4e2013-02-14 16:19:38 +01001758 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001759 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001760 nlmsg_free(msg);
1761 return -ENOBUFS;
1762 }
Johannes Berg55682962007-09-20 13:09:35 -04001763
Johannes Berg134e6372009-07-10 09:51:34 +00001764 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001765}
1766
Jouni Malinen31888482008-10-30 16:59:24 +02001767static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1768 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1769 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1770 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1771 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1772 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1773};
1774
1775static int parse_txq_params(struct nlattr *tb[],
1776 struct ieee80211_txq_params *txq_params)
1777{
Johannes Berga3304b02012-03-28 11:04:24 +02001778 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001779 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1780 !tb[NL80211_TXQ_ATTR_AIFS])
1781 return -EINVAL;
1782
Johannes Berga3304b02012-03-28 11:04:24 +02001783 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001784 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1785 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1786 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1787 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1788
Johannes Berga3304b02012-03-28 11:04:24 +02001789 if (txq_params->ac >= NL80211_NUM_ACS)
1790 return -EINVAL;
1791
Jouni Malinen31888482008-10-30 16:59:24 +02001792 return 0;
1793}
1794
Johannes Bergf444de02010-05-05 15:25:02 +02001795static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1796{
1797 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001798 * You can only set the channel explicitly for WDS interfaces,
1799 * all others have their channel managed via their respective
1800 * "establish a connection" command (connect, join, ...)
1801 *
1802 * For AP/GO and mesh mode, the channel can be set with the
1803 * channel userspace API, but is only stored and passed to the
1804 * low-level driver when the AP starts or the mesh is joined.
1805 * This is for backward compatibility, userspace can also give
1806 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001807 *
1808 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001809 * whatever else is going on, so they have their own special
1810 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001811 */
1812 return !wdev ||
1813 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001814 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001815 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1816 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001817}
1818
Johannes Berg683b6d32012-11-08 21:25:48 +01001819static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1820 struct genl_info *info,
1821 struct cfg80211_chan_def *chandef)
1822{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301823 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001824
1825 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1826 return -EINVAL;
1827
1828 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1829
1830 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001831 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1832 chandef->center_freq1 = control_freq;
1833 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001834
1835 /* Primary channel not allowed */
1836 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1837 return -EINVAL;
1838
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001839 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1840 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001841
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001842 chantype = nla_get_u32(
1843 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1844
1845 switch (chantype) {
1846 case NL80211_CHAN_NO_HT:
1847 case NL80211_CHAN_HT20:
1848 case NL80211_CHAN_HT40PLUS:
1849 case NL80211_CHAN_HT40MINUS:
1850 cfg80211_chandef_create(chandef, chandef->chan,
1851 chantype);
1852 break;
1853 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001854 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001855 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001856 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1857 chandef->width =
1858 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1859 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1860 chandef->center_freq1 =
1861 nla_get_u32(
1862 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1863 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1864 chandef->center_freq2 =
1865 nla_get_u32(
1866 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1867 }
1868
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001869 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001870 return -EINVAL;
1871
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001872 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1873 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001874 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001875
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001876 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1877 chandef->width == NL80211_CHAN_WIDTH_10) &&
1878 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1879 return -EINVAL;
1880
Johannes Berg683b6d32012-11-08 21:25:48 +01001881 return 0;
1882}
1883
Johannes Bergf444de02010-05-05 15:25:02 +02001884static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1885 struct wireless_dev *wdev,
1886 struct genl_info *info)
1887{
Johannes Berg683b6d32012-11-08 21:25:48 +01001888 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001889 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001890 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1891
1892 if (wdev)
1893 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001894
Johannes Bergf444de02010-05-05 15:25:02 +02001895 if (!nl80211_can_set_dev_channel(wdev))
1896 return -EOPNOTSUPP;
1897
Johannes Berg683b6d32012-11-08 21:25:48 +01001898 result = nl80211_parse_chandef(rdev, info, &chandef);
1899 if (result)
1900 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001901
Johannes Berge8c9bd52012-06-06 08:18:22 +02001902 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001903 case NL80211_IFTYPE_AP:
1904 case NL80211_IFTYPE_P2P_GO:
1905 if (wdev->beacon_interval) {
1906 result = -EBUSY;
1907 break;
1908 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001909 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001910 result = -EINVAL;
1911 break;
1912 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001913 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001914 result = 0;
1915 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001916 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001917 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001918 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001919 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001920 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001921 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001922 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001923 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001924 }
Johannes Bergf444de02010-05-05 15:25:02 +02001925
1926 return result;
1927}
1928
1929static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1930{
Johannes Berg4c476992010-10-04 21:36:35 +02001931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1932 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001933
Johannes Berg4c476992010-10-04 21:36:35 +02001934 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001935}
1936
Bill Jordane8347eb2010-10-01 13:54:28 -04001937static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1938{
Johannes Berg43b19952010-10-07 13:10:30 +02001939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1940 struct net_device *dev = info->user_ptr[1];
1941 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001942 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001943
1944 if (!info->attrs[NL80211_ATTR_MAC])
1945 return -EINVAL;
1946
Johannes Berg43b19952010-10-07 13:10:30 +02001947 if (netif_running(dev))
1948 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001949
Johannes Berg43b19952010-10-07 13:10:30 +02001950 if (!rdev->ops->set_wds_peer)
1951 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001952
Johannes Berg43b19952010-10-07 13:10:30 +02001953 if (wdev->iftype != NL80211_IFTYPE_WDS)
1954 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001955
1956 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001957 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001958}
1959
1960
Johannes Berg55682962007-09-20 13:09:35 -04001961static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1962{
1963 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001964 struct net_device *netdev = NULL;
1965 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001966 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001967 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001968 u32 changed;
1969 u8 retry_short = 0, retry_long = 0;
1970 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001971 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001972
Johannes Berg5fe231e2013-05-08 21:45:15 +02001973 ASSERT_RTNL();
1974
Johannes Bergf444de02010-05-05 15:25:02 +02001975 /*
1976 * Try to find the wiphy and netdev. Normally this
1977 * function shouldn't need the netdev, but this is
1978 * done for backward compatibility -- previously
1979 * setting the channel was done per wiphy, but now
1980 * it is per netdev. Previous userland like hostapd
1981 * also passed a netdev to set_wiphy, so that it is
1982 * possible to let that go to the right netdev!
1983 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001984
Johannes Bergf444de02010-05-05 15:25:02 +02001985 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1986 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1987
Ying Xue7f2b8562014-01-15 10:23:45 +08001988 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001989 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02001990 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001991 else
Johannes Bergf444de02010-05-05 15:25:02 +02001992 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001993 }
1994
Johannes Bergf444de02010-05-05 15:25:02 +02001995 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001996 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1997 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001998 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02001999 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002000 wdev = NULL;
2001 netdev = NULL;
2002 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002003 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002004 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002005
2006 /*
2007 * end workaround code, by now the rdev is available
2008 * and locked, and wdev may or may not be NULL.
2009 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002010
2011 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002012 result = cfg80211_dev_rename(
2013 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002014
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002015 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002016 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002017
Jouni Malinen31888482008-10-30 16:59:24 +02002018 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2019 struct ieee80211_txq_params txq_params;
2020 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2021
Ying Xue7f2b8562014-01-15 10:23:45 +08002022 if (!rdev->ops->set_txq_params)
2023 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002024
Ying Xue7f2b8562014-01-15 10:23:45 +08002025 if (!netdev)
2026 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002027
Johannes Berg133a3ff2011-11-03 14:50:13 +01002028 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002029 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2030 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002031
Ying Xue7f2b8562014-01-15 10:23:45 +08002032 if (!netif_running(netdev))
2033 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002034
Jouni Malinen31888482008-10-30 16:59:24 +02002035 nla_for_each_nested(nl_txq_params,
2036 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2037 rem_txq_params) {
2038 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2039 nla_data(nl_txq_params),
2040 nla_len(nl_txq_params),
2041 txq_params_policy);
2042 result = parse_txq_params(tb, &txq_params);
2043 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002044 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002045
Hila Gonene35e4d22012-06-27 17:19:42 +03002046 result = rdev_set_txq_params(rdev, netdev,
2047 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002048 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002049 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002050 }
2051 }
2052
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002053 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002054 result = __nl80211_set_channel(rdev,
2055 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2056 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002057 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002058 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002059 }
2060
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002061 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002062 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002063 enum nl80211_tx_power_setting type;
2064 int idx, mbm = 0;
2065
Johannes Bergc8442112012-10-24 10:17:18 +02002066 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2067 txp_wdev = NULL;
2068
Ying Xue7f2b8562014-01-15 10:23:45 +08002069 if (!rdev->ops->set_tx_power)
2070 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002071
2072 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2073 type = nla_get_u32(info->attrs[idx]);
2074
2075 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002076 (type != NL80211_TX_POWER_AUTOMATIC))
2077 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002078
2079 if (type != NL80211_TX_POWER_AUTOMATIC) {
2080 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2081 mbm = nla_get_u32(info->attrs[idx]);
2082 }
2083
Johannes Bergc8442112012-10-24 10:17:18 +02002084 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002085 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002086 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002087 }
2088
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002089 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2090 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2091 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002092 if ((!rdev->wiphy.available_antennas_tx &&
2093 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002094 !rdev->ops->set_antenna)
2095 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002096
2097 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2098 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2099
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002100 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002101 * available antenna masks, except for the "all" mask */
2102 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002103 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2104 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002105
Bruno Randolf7f531e02010-12-16 11:30:22 +09002106 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2107 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002108
Hila Gonene35e4d22012-06-27 17:19:42 +03002109 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002110 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002111 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002112 }
2113
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002114 changed = 0;
2115
2116 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2117 retry_short = nla_get_u8(
2118 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002119 if (retry_short == 0)
2120 return -EINVAL;
2121
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002122 changed |= WIPHY_PARAM_RETRY_SHORT;
2123 }
2124
2125 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2126 retry_long = nla_get_u8(
2127 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002128 if (retry_long == 0)
2129 return -EINVAL;
2130
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002131 changed |= WIPHY_PARAM_RETRY_LONG;
2132 }
2133
2134 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2135 frag_threshold = nla_get_u32(
2136 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002137 if (frag_threshold < 256)
2138 return -EINVAL;
2139
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002140 if (frag_threshold != (u32) -1) {
2141 /*
2142 * Fragments (apart from the last one) are required to
2143 * have even length. Make the fragmentation code
2144 * simpler by stripping LSB should someone try to use
2145 * odd threshold value.
2146 */
2147 frag_threshold &= ~0x1;
2148 }
2149 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2150 }
2151
2152 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2153 rts_threshold = nla_get_u32(
2154 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2155 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2156 }
2157
Lukáš Turek81077e82009-12-21 22:50:47 +01002158 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2159 coverage_class = nla_get_u8(
2160 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2161 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2162 }
2163
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002164 if (changed) {
2165 u8 old_retry_short, old_retry_long;
2166 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002167 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002168
Ying Xue7f2b8562014-01-15 10:23:45 +08002169 if (!rdev->ops->set_wiphy_params)
2170 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002171
2172 old_retry_short = rdev->wiphy.retry_short;
2173 old_retry_long = rdev->wiphy.retry_long;
2174 old_frag_threshold = rdev->wiphy.frag_threshold;
2175 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002176 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002177
2178 if (changed & WIPHY_PARAM_RETRY_SHORT)
2179 rdev->wiphy.retry_short = retry_short;
2180 if (changed & WIPHY_PARAM_RETRY_LONG)
2181 rdev->wiphy.retry_long = retry_long;
2182 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2183 rdev->wiphy.frag_threshold = frag_threshold;
2184 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2185 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002186 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2187 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002188
Hila Gonene35e4d22012-06-27 17:19:42 +03002189 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002190 if (result) {
2191 rdev->wiphy.retry_short = old_retry_short;
2192 rdev->wiphy.retry_long = old_retry_long;
2193 rdev->wiphy.frag_threshold = old_frag_threshold;
2194 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002195 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002196 }
2197 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002198 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002199}
2200
Johannes Berg71bbc992012-06-15 15:30:18 +02002201static inline u64 wdev_id(struct wireless_dev *wdev)
2202{
2203 return (u64)wdev->identifier |
2204 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2205}
Johannes Berg55682962007-09-20 13:09:35 -04002206
Johannes Berg683b6d32012-11-08 21:25:48 +01002207static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002208 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002209{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002210 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002211
Johannes Berg683b6d32012-11-08 21:25:48 +01002212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2213 chandef->chan->center_freq))
2214 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002215 switch (chandef->width) {
2216 case NL80211_CHAN_WIDTH_20_NOHT:
2217 case NL80211_CHAN_WIDTH_20:
2218 case NL80211_CHAN_WIDTH_40:
2219 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2220 cfg80211_get_chandef_type(chandef)))
2221 return -ENOBUFS;
2222 break;
2223 default:
2224 break;
2225 }
2226 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2227 return -ENOBUFS;
2228 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2229 return -ENOBUFS;
2230 if (chandef->center_freq2 &&
2231 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002232 return -ENOBUFS;
2233 return 0;
2234}
2235
Eric W. Biederman15e47302012-09-07 20:12:54 +00002236static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002237 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002238 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002239{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002240 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002241 void *hdr;
2242
Eric W. Biederman15e47302012-09-07 20:12:54 +00002243 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002244 if (!hdr)
2245 return -1;
2246
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002247 if (dev &&
2248 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002249 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002250 goto nla_put_failure;
2251
2252 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2253 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002254 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002255 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002256 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2257 rdev->devlist_generation ^
2258 (cfg80211_rdev_list_generation << 2)))
2259 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002260
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002261 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002262 int ret;
2263 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002264
Johannes Berg683b6d32012-11-08 21:25:48 +01002265 ret = rdev_get_channel(rdev, wdev, &chandef);
2266 if (ret == 0) {
2267 if (nl80211_send_chandef(msg, &chandef))
2268 goto nla_put_failure;
2269 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002270 }
2271
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002272 if (wdev->ssid_len) {
2273 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2274 goto nla_put_failure;
2275 }
2276
Johannes Berg55682962007-09-20 13:09:35 -04002277 return genlmsg_end(msg, hdr);
2278
2279 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002280 genlmsg_cancel(msg, hdr);
2281 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002282}
2283
2284static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2285{
2286 int wp_idx = 0;
2287 int if_idx = 0;
2288 int wp_start = cb->args[0];
2289 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002290 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002291 struct wireless_dev *wdev;
2292
Johannes Berg5fe231e2013-05-08 21:45:15 +02002293 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002294 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2295 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002296 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002297 if (wp_idx < wp_start) {
2298 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002299 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002300 }
Johannes Berg55682962007-09-20 13:09:35 -04002301 if_idx = 0;
2302
Johannes Berg89a54e42012-06-15 14:33:17 +02002303 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002304 if (if_idx < if_start) {
2305 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002306 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002307 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002308 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002309 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002310 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002311 goto out;
2312 }
2313 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002314 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002315
2316 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002317 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002318 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002319 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002320
2321 cb->args[0] = wp_idx;
2322 cb->args[1] = if_idx;
2323
2324 return skb->len;
2325}
2326
2327static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2328{
2329 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002330 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002331 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002332
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002334 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002335 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002336
Eric W. Biederman15e47302012-09-07 20:12:54 +00002337 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002338 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002339 nlmsg_free(msg);
2340 return -ENOBUFS;
2341 }
Johannes Berg55682962007-09-20 13:09:35 -04002342
Johannes Berg134e6372009-07-10 09:51:34 +00002343 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002344}
2345
Michael Wu66f7ac52008-01-31 19:48:22 +01002346static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2347 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2348 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2349 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2350 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2351 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002352 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002353};
2354
2355static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2356{
2357 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2358 int flag;
2359
2360 *mntrflags = 0;
2361
2362 if (!nla)
2363 return -EINVAL;
2364
2365 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2366 nla, mntr_flags_policy))
2367 return -EINVAL;
2368
2369 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2370 if (flags[flag])
2371 *mntrflags |= (1<<flag);
2372
2373 return 0;
2374}
2375
Johannes Berg9bc383d2009-11-19 11:55:19 +01002376static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002377 struct net_device *netdev, u8 use_4addr,
2378 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002379{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002380 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002381 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002382 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002383 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002384 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002385
2386 switch (iftype) {
2387 case NL80211_IFTYPE_AP_VLAN:
2388 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2389 return 0;
2390 break;
2391 case NL80211_IFTYPE_STATION:
2392 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2393 return 0;
2394 break;
2395 default:
2396 break;
2397 }
2398
2399 return -EOPNOTSUPP;
2400}
2401
Johannes Berg55682962007-09-20 13:09:35 -04002402static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2403{
Johannes Berg4c476992010-10-04 21:36:35 +02002404 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002405 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002406 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002407 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002408 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002409 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002410 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002411
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412 memset(&params, 0, sizeof(params));
2413
Johannes Berg04a773a2009-04-19 21:24:32 +02002414 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002415
Johannes Berg723b0382008-09-16 20:22:09 +02002416 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002417 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002418 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002419 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002420 if (ntype > NL80211_IFTYPE_MAX)
2421 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002422 }
2423
Johannes Berg92ffe052008-09-16 20:39:36 +02002424 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002425 struct wireless_dev *wdev = dev->ieee80211_ptr;
2426
Johannes Berg4c476992010-10-04 21:36:35 +02002427 if (ntype != NL80211_IFTYPE_MESH_POINT)
2428 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002429 if (netif_running(dev))
2430 return -EBUSY;
2431
2432 wdev_lock(wdev);
2433 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2434 IEEE80211_MAX_MESH_ID_LEN);
2435 wdev->mesh_id_up_len =
2436 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2437 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2438 wdev->mesh_id_up_len);
2439 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002440 }
2441
Felix Fietkau8b787642009-11-10 18:53:10 +01002442 if (info->attrs[NL80211_ATTR_4ADDR]) {
2443 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2444 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002445 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002446 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002447 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002448 } else {
2449 params.use_4addr = -1;
2450 }
2451
Johannes Berg92ffe052008-09-16 20:39:36 +02002452 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002453 if (ntype != NL80211_IFTYPE_MONITOR)
2454 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002455 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2456 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002457 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002458 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002459
2460 flags = &_flags;
2461 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002462 }
Johannes Berg3b858752009-03-12 09:55:09 +01002463
Luciano Coelho18003292013-08-29 13:26:57 +03002464 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002465 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2466 return -EOPNOTSUPP;
2467
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002468 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002469 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002470 else
2471 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002472
Johannes Berg9bc383d2009-11-19 11:55:19 +01002473 if (!err && params.use_4addr != -1)
2474 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2475
Johannes Berg55682962007-09-20 13:09:35 -04002476 return err;
2477}
2478
2479static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2480{
Johannes Berg4c476992010-10-04 21:36:35 +02002481 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002482 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002483 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002484 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002485 int err;
2486 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002487 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002488
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002489 memset(&params, 0, sizeof(params));
2490
Johannes Berg55682962007-09-20 13:09:35 -04002491 if (!info->attrs[NL80211_ATTR_IFNAME])
2492 return -EINVAL;
2493
2494 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2495 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2496 if (type > NL80211_IFTYPE_MAX)
2497 return -EINVAL;
2498 }
2499
Johannes Berg79c97e92009-07-07 03:56:12 +02002500 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002501 !(rdev->wiphy.interface_modes & (1 << type)))
2502 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002503
Arend van Spriel1c18f142013-01-08 10:17:27 +01002504 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2505 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2506 ETH_ALEN);
2507 if (!is_valid_ether_addr(params.macaddr))
2508 return -EADDRNOTAVAIL;
2509 }
2510
Johannes Berg9bc383d2009-11-19 11:55:19 +01002511 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002512 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002513 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002514 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002515 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002516 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002517
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2519 if (!msg)
2520 return -ENOMEM;
2521
Michael Wu66f7ac52008-01-31 19:48:22 +01002522 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2523 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2524 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002525
Luciano Coelho18003292013-08-29 13:26:57 +03002526 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002527 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2528 return -EOPNOTSUPP;
2529
Hila Gonene35e4d22012-06-27 17:19:42 +03002530 wdev = rdev_add_virtual_intf(rdev,
2531 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2532 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002533 if (IS_ERR(wdev)) {
2534 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002535 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002536 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002537
Johannes Berg98104fde2012-06-16 00:19:54 +02002538 switch (type) {
2539 case NL80211_IFTYPE_MESH_POINT:
2540 if (!info->attrs[NL80211_ATTR_MESH_ID])
2541 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002542 wdev_lock(wdev);
2543 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2544 IEEE80211_MAX_MESH_ID_LEN);
2545 wdev->mesh_id_up_len =
2546 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2547 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2548 wdev->mesh_id_up_len);
2549 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002550 break;
2551 case NL80211_IFTYPE_P2P_DEVICE:
2552 /*
2553 * P2P Device doesn't have a netdev, so doesn't go
2554 * through the netdev notifier and must be added here
2555 */
2556 mutex_init(&wdev->mtx);
2557 INIT_LIST_HEAD(&wdev->event_list);
2558 spin_lock_init(&wdev->event_lock);
2559 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2560 spin_lock_init(&wdev->mgmt_registrations_lock);
2561
Johannes Berg98104fde2012-06-16 00:19:54 +02002562 wdev->identifier = ++rdev->wdev_id;
2563 list_add_rcu(&wdev->list, &rdev->wdev_list);
2564 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002565 break;
2566 default:
2567 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002568 }
2569
Eric W. Biederman15e47302012-09-07 20:12:54 +00002570 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002571 rdev, wdev) < 0) {
2572 nlmsg_free(msg);
2573 return -ENOBUFS;
2574 }
2575
2576 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002577}
2578
2579static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2580{
Johannes Berg4c476992010-10-04 21:36:35 +02002581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002582 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002583
Johannes Berg4c476992010-10-04 21:36:35 +02002584 if (!rdev->ops->del_virtual_intf)
2585 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002586
Johannes Berg84efbb82012-06-16 00:00:26 +02002587 /*
2588 * If we remove a wireless device without a netdev then clear
2589 * user_ptr[1] so that nl80211_post_doit won't dereference it
2590 * to check if it needs to do dev_put(). Otherwise it crashes
2591 * since the wdev has been freed, unlike with a netdev where
2592 * we need the dev_put() for the netdev to really be freed.
2593 */
2594 if (!wdev->netdev)
2595 info->user_ptr[1] = NULL;
2596
Hila Gonene35e4d22012-06-27 17:19:42 +03002597 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002598}
2599
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002600static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2601{
2602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2603 struct net_device *dev = info->user_ptr[1];
2604 u16 noack_map;
2605
2606 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2607 return -EINVAL;
2608
2609 if (!rdev->ops->set_noack_map)
2610 return -EOPNOTSUPP;
2611
2612 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2613
Hila Gonene35e4d22012-06-27 17:19:42 +03002614 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002615}
2616
Johannes Berg41ade002007-12-19 02:03:29 +01002617struct get_key_cookie {
2618 struct sk_buff *msg;
2619 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002620 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002621};
2622
2623static void get_key_callback(void *c, struct key_params *params)
2624{
Johannes Bergb9454e82009-07-08 13:29:08 +02002625 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002626 struct get_key_cookie *cookie = c;
2627
David S. Miller9360ffd2012-03-29 04:41:26 -04002628 if ((params->key &&
2629 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2630 params->key_len, params->key)) ||
2631 (params->seq &&
2632 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2633 params->seq_len, params->seq)) ||
2634 (params->cipher &&
2635 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2636 params->cipher)))
2637 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002638
Johannes Bergb9454e82009-07-08 13:29:08 +02002639 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2640 if (!key)
2641 goto nla_put_failure;
2642
David S. Miller9360ffd2012-03-29 04:41:26 -04002643 if ((params->key &&
2644 nla_put(cookie->msg, NL80211_KEY_DATA,
2645 params->key_len, params->key)) ||
2646 (params->seq &&
2647 nla_put(cookie->msg, NL80211_KEY_SEQ,
2648 params->seq_len, params->seq)) ||
2649 (params->cipher &&
2650 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2651 params->cipher)))
2652 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002653
David S. Miller9360ffd2012-03-29 04:41:26 -04002654 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2655 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002656
2657 nla_nest_end(cookie->msg, key);
2658
Johannes Berg41ade002007-12-19 02:03:29 +01002659 return;
2660 nla_put_failure:
2661 cookie->error = 1;
2662}
2663
2664static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2665{
Johannes Berg4c476992010-10-04 21:36:35 +02002666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002667 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002668 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002669 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002670 const u8 *mac_addr = NULL;
2671 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002672 struct get_key_cookie cookie = {
2673 .error = 0,
2674 };
2675 void *hdr;
2676 struct sk_buff *msg;
2677
2678 if (info->attrs[NL80211_ATTR_KEY_IDX])
2679 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2680
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002681 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002682 return -EINVAL;
2683
2684 if (info->attrs[NL80211_ATTR_MAC])
2685 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2686
Johannes Berge31b8212010-10-05 19:39:30 +02002687 pairwise = !!mac_addr;
2688 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2689 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2690 if (kt >= NUM_NL80211_KEYTYPES)
2691 return -EINVAL;
2692 if (kt != NL80211_KEYTYPE_GROUP &&
2693 kt != NL80211_KEYTYPE_PAIRWISE)
2694 return -EINVAL;
2695 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2696 }
2697
Johannes Berg4c476992010-10-04 21:36:35 +02002698 if (!rdev->ops->get_key)
2699 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002700
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002701 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002702 if (!msg)
2703 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002704
Eric W. Biederman15e47302012-09-07 20:12:54 +00002705 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002706 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002707 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002708 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002709
2710 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002711 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002712
David S. Miller9360ffd2012-03-29 04:41:26 -04002713 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2714 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2715 goto nla_put_failure;
2716 if (mac_addr &&
2717 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2718 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002719
Johannes Berge31b8212010-10-05 19:39:30 +02002720 if (pairwise && mac_addr &&
2721 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2722 return -ENOENT;
2723
Hila Gonene35e4d22012-06-27 17:19:42 +03002724 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2725 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002726
2727 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002728 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002729
2730 if (cookie.error)
2731 goto nla_put_failure;
2732
2733 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002734 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002735
2736 nla_put_failure:
2737 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002738 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002739 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002740 return err;
2741}
2742
2743static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2744{
Johannes Berg4c476992010-10-04 21:36:35 +02002745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002746 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002747 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002748 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002749
Johannes Bergb9454e82009-07-08 13:29:08 +02002750 err = nl80211_parse_key(info, &key);
2751 if (err)
2752 return err;
2753
2754 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002755 return -EINVAL;
2756
Johannes Bergb9454e82009-07-08 13:29:08 +02002757 /* only support setting default key */
2758 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002759 return -EINVAL;
2760
Johannes Bergfffd0932009-07-08 14:22:54 +02002761 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002762
2763 if (key.def) {
2764 if (!rdev->ops->set_default_key) {
2765 err = -EOPNOTSUPP;
2766 goto out;
2767 }
2768
2769 err = nl80211_key_allowed(dev->ieee80211_ptr);
2770 if (err)
2771 goto out;
2772
Hila Gonene35e4d22012-06-27 17:19:42 +03002773 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002774 key.def_uni, key.def_multi);
2775
2776 if (err)
2777 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002778
Johannes Berg3d23e342009-09-29 23:27:28 +02002779#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002780 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002781#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002782 } else {
2783 if (key.def_uni || !key.def_multi) {
2784 err = -EINVAL;
2785 goto out;
2786 }
2787
2788 if (!rdev->ops->set_default_mgmt_key) {
2789 err = -EOPNOTSUPP;
2790 goto out;
2791 }
2792
2793 err = nl80211_key_allowed(dev->ieee80211_ptr);
2794 if (err)
2795 goto out;
2796
Hila Gonene35e4d22012-06-27 17:19:42 +03002797 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002798 if (err)
2799 goto out;
2800
2801#ifdef CONFIG_CFG80211_WEXT
2802 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2803#endif
2804 }
2805
2806 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002807 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002808
Johannes Berg41ade002007-12-19 02:03:29 +01002809 return err;
2810}
2811
2812static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2813{
Johannes Berg4c476992010-10-04 21:36:35 +02002814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002815 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002816 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002817 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002818 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002819
Johannes Bergb9454e82009-07-08 13:29:08 +02002820 err = nl80211_parse_key(info, &key);
2821 if (err)
2822 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002823
Johannes Bergb9454e82009-07-08 13:29:08 +02002824 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002825 return -EINVAL;
2826
Johannes Berg41ade002007-12-19 02:03:29 +01002827 if (info->attrs[NL80211_ATTR_MAC])
2828 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2829
Johannes Berge31b8212010-10-05 19:39:30 +02002830 if (key.type == -1) {
2831 if (mac_addr)
2832 key.type = NL80211_KEYTYPE_PAIRWISE;
2833 else
2834 key.type = NL80211_KEYTYPE_GROUP;
2835 }
2836
2837 /* for now */
2838 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2839 key.type != NL80211_KEYTYPE_GROUP)
2840 return -EINVAL;
2841
Johannes Berg4c476992010-10-04 21:36:35 +02002842 if (!rdev->ops->add_key)
2843 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002844
Johannes Berge31b8212010-10-05 19:39:30 +02002845 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2846 key.type == NL80211_KEYTYPE_PAIRWISE,
2847 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002848 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002849
2850 wdev_lock(dev->ieee80211_ptr);
2851 err = nl80211_key_allowed(dev->ieee80211_ptr);
2852 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002853 err = rdev_add_key(rdev, dev, key.idx,
2854 key.type == NL80211_KEYTYPE_PAIRWISE,
2855 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002856 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002857
Johannes Berg41ade002007-12-19 02:03:29 +01002858 return err;
2859}
2860
2861static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2862{
Johannes Berg4c476992010-10-04 21:36:35 +02002863 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002864 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002865 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002866 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002867 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002868
Johannes Bergb9454e82009-07-08 13:29:08 +02002869 err = nl80211_parse_key(info, &key);
2870 if (err)
2871 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002872
2873 if (info->attrs[NL80211_ATTR_MAC])
2874 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2875
Johannes Berge31b8212010-10-05 19:39:30 +02002876 if (key.type == -1) {
2877 if (mac_addr)
2878 key.type = NL80211_KEYTYPE_PAIRWISE;
2879 else
2880 key.type = NL80211_KEYTYPE_GROUP;
2881 }
2882
2883 /* for now */
2884 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2885 key.type != NL80211_KEYTYPE_GROUP)
2886 return -EINVAL;
2887
Johannes Berg4c476992010-10-04 21:36:35 +02002888 if (!rdev->ops->del_key)
2889 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002890
Johannes Bergfffd0932009-07-08 14:22:54 +02002891 wdev_lock(dev->ieee80211_ptr);
2892 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002893
2894 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2895 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2896 err = -ENOENT;
2897
Johannes Bergfffd0932009-07-08 14:22:54 +02002898 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002899 err = rdev_del_key(rdev, dev, key.idx,
2900 key.type == NL80211_KEYTYPE_PAIRWISE,
2901 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002902
Johannes Berg3d23e342009-09-29 23:27:28 +02002903#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002904 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002905 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002906 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002907 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002908 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2909 }
2910#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002911 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002912
Johannes Berg41ade002007-12-19 02:03:29 +01002913 return err;
2914}
2915
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302916/* This function returns an error or the number of nested attributes */
2917static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2918{
2919 struct nlattr *attr;
2920 int n_entries = 0, tmp;
2921
2922 nla_for_each_nested(attr, nl_attr, tmp) {
2923 if (nla_len(attr) != ETH_ALEN)
2924 return -EINVAL;
2925
2926 n_entries++;
2927 }
2928
2929 return n_entries;
2930}
2931
2932/*
2933 * This function parses ACL information and allocates memory for ACL data.
2934 * On successful return, the calling function is responsible to free the
2935 * ACL buffer returned by this function.
2936 */
2937static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2938 struct genl_info *info)
2939{
2940 enum nl80211_acl_policy acl_policy;
2941 struct nlattr *attr;
2942 struct cfg80211_acl_data *acl;
2943 int i = 0, n_entries, tmp;
2944
2945 if (!wiphy->max_acl_mac_addrs)
2946 return ERR_PTR(-EOPNOTSUPP);
2947
2948 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2949 return ERR_PTR(-EINVAL);
2950
2951 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2952 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2953 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2954 return ERR_PTR(-EINVAL);
2955
2956 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2957 return ERR_PTR(-EINVAL);
2958
2959 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2960 if (n_entries < 0)
2961 return ERR_PTR(n_entries);
2962
2963 if (n_entries > wiphy->max_acl_mac_addrs)
2964 return ERR_PTR(-ENOTSUPP);
2965
2966 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2967 GFP_KERNEL);
2968 if (!acl)
2969 return ERR_PTR(-ENOMEM);
2970
2971 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2972 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2973 i++;
2974 }
2975
2976 acl->n_acl_entries = n_entries;
2977 acl->acl_policy = acl_policy;
2978
2979 return acl;
2980}
2981
2982static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2983{
2984 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2985 struct net_device *dev = info->user_ptr[1];
2986 struct cfg80211_acl_data *acl;
2987 int err;
2988
2989 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2990 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2991 return -EOPNOTSUPP;
2992
2993 if (!dev->ieee80211_ptr->beacon_interval)
2994 return -EINVAL;
2995
2996 acl = parse_acl_data(&rdev->wiphy, info);
2997 if (IS_ERR(acl))
2998 return PTR_ERR(acl);
2999
3000 err = rdev_set_mac_acl(rdev, dev, acl);
3001
3002 kfree(acl);
3003
3004 return err;
3005}
3006
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003007static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003008 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003009{
Johannes Berg88600202012-02-13 15:17:18 +01003010 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003011
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003012 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3013 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3014 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3015 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003016 return -EINVAL;
3017
Johannes Berg88600202012-02-13 15:17:18 +01003018 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003019
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003020 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3021 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3022 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003023 if (!bcn->head_len)
3024 return -EINVAL;
3025 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003026 }
3027
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003028 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3029 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3030 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003031 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003032 }
3033
Johannes Berg4c476992010-10-04 21:36:35 +02003034 if (!haveinfo)
3035 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003036
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003037 if (attrs[NL80211_ATTR_IE]) {
3038 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3039 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003040 }
3041
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003042 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003043 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003044 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003045 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003046 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003047 }
3048
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003049 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003050 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003051 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003052 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003053 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003054 }
3055
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003056 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3057 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3058 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003059 }
3060
Johannes Berg88600202012-02-13 15:17:18 +01003061 return 0;
3062}
3063
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003064static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3065 struct cfg80211_ap_settings *params)
3066{
3067 struct wireless_dev *wdev;
3068 bool ret = false;
3069
Johannes Berg89a54e42012-06-15 14:33:17 +02003070 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003071 if (wdev->iftype != NL80211_IFTYPE_AP &&
3072 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3073 continue;
3074
Johannes Berg683b6d32012-11-08 21:25:48 +01003075 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003076 continue;
3077
Johannes Berg683b6d32012-11-08 21:25:48 +01003078 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003079 ret = true;
3080 break;
3081 }
3082
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003083 return ret;
3084}
3085
Jouni Malinene39e5b52012-09-30 19:29:39 +03003086static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3087 enum nl80211_auth_type auth_type,
3088 enum nl80211_commands cmd)
3089{
3090 if (auth_type > NL80211_AUTHTYPE_MAX)
3091 return false;
3092
3093 switch (cmd) {
3094 case NL80211_CMD_AUTHENTICATE:
3095 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3096 auth_type == NL80211_AUTHTYPE_SAE)
3097 return false;
3098 return true;
3099 case NL80211_CMD_CONNECT:
3100 case NL80211_CMD_START_AP:
3101 /* SAE not supported yet */
3102 if (auth_type == NL80211_AUTHTYPE_SAE)
3103 return false;
3104 return true;
3105 default:
3106 return false;
3107 }
3108}
3109
Johannes Berg88600202012-02-13 15:17:18 +01003110static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3111{
3112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3113 struct net_device *dev = info->user_ptr[1];
3114 struct wireless_dev *wdev = dev->ieee80211_ptr;
3115 struct cfg80211_ap_settings params;
3116 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003117 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003118
3119 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3120 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3121 return -EOPNOTSUPP;
3122
3123 if (!rdev->ops->start_ap)
3124 return -EOPNOTSUPP;
3125
3126 if (wdev->beacon_interval)
3127 return -EALREADY;
3128
3129 memset(&params, 0, sizeof(params));
3130
3131 /* these are required for START_AP */
3132 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3133 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3134 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3135 return -EINVAL;
3136
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003137 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003138 if (err)
3139 return err;
3140
3141 params.beacon_interval =
3142 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3143 params.dtim_period =
3144 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3145
3146 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3147 if (err)
3148 return err;
3149
3150 /*
3151 * In theory, some of these attributes should be required here
3152 * but since they were not used when the command was originally
3153 * added, keep them optional for old user space programs to let
3154 * them continue to work with drivers that do not need the
3155 * additional information -- drivers must check!
3156 */
3157 if (info->attrs[NL80211_ATTR_SSID]) {
3158 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3159 params.ssid_len =
3160 nla_len(info->attrs[NL80211_ATTR_SSID]);
3161 if (params.ssid_len == 0 ||
3162 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3163 return -EINVAL;
3164 }
3165
3166 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3167 params.hidden_ssid = nla_get_u32(
3168 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3169 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3170 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3171 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3172 return -EINVAL;
3173 }
3174
3175 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3176
3177 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3178 params.auth_type = nla_get_u32(
3179 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003180 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3181 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003182 return -EINVAL;
3183 } else
3184 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3185
3186 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3187 NL80211_MAX_NR_CIPHER_SUITES);
3188 if (err)
3189 return err;
3190
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303191 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3192 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3193 return -EOPNOTSUPP;
3194 params.inactivity_timeout = nla_get_u16(
3195 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3196 }
3197
Johannes Berg53cabad2012-11-14 15:17:28 +01003198 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3199 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3200 return -EINVAL;
3201 params.p2p_ctwindow =
3202 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3203 if (params.p2p_ctwindow > 127)
3204 return -EINVAL;
3205 if (params.p2p_ctwindow != 0 &&
3206 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3207 return -EINVAL;
3208 }
3209
3210 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3211 u8 tmp;
3212
3213 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3214 return -EINVAL;
3215 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3216 if (tmp > 1)
3217 return -EINVAL;
3218 params.p2p_opp_ps = tmp;
3219 if (params.p2p_opp_ps != 0 &&
3220 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3221 return -EINVAL;
3222 }
3223
Johannes Bergaa430da2012-05-16 23:50:18 +02003224 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003225 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3226 if (err)
3227 return err;
3228 } else if (wdev->preset_chandef.chan) {
3229 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003230 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003231 return -EINVAL;
3232
Johannes Berg683b6d32012-11-08 21:25:48 +01003233 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003234 return -EINVAL;
3235
Simon Wunderlich04f39042013-02-08 18:16:19 +01003236 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3237 if (err < 0)
3238 return err;
3239 if (err) {
3240 radar_detect_width = BIT(params.chandef.width);
3241 params.radar_required = true;
3242 }
3243
Simon Wunderlich04f39042013-02-08 18:16:19 +01003244 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3245 params.chandef.chan,
3246 CHAN_MODE_SHARED,
3247 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003248 if (err)
3249 return err;
3250
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303251 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3252 params.acl = parse_acl_data(&rdev->wiphy, info);
3253 if (IS_ERR(params.acl))
3254 return PTR_ERR(params.acl);
3255 }
3256
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003257 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003258 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003259 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003260 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003261 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003262 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003263 wdev->ssid_len = params.ssid_len;
3264 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003265 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003266 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303267
3268 kfree(params.acl);
3269
Johannes Berg56d18932011-05-09 18:41:15 +02003270 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003271}
3272
Johannes Berg88600202012-02-13 15:17:18 +01003273static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3274{
3275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3276 struct net_device *dev = info->user_ptr[1];
3277 struct wireless_dev *wdev = dev->ieee80211_ptr;
3278 struct cfg80211_beacon_data params;
3279 int err;
3280
3281 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3282 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3283 return -EOPNOTSUPP;
3284
3285 if (!rdev->ops->change_beacon)
3286 return -EOPNOTSUPP;
3287
3288 if (!wdev->beacon_interval)
3289 return -EINVAL;
3290
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003291 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003292 if (err)
3293 return err;
3294
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003295 wdev_lock(wdev);
3296 err = rdev_change_beacon(rdev, dev, &params);
3297 wdev_unlock(wdev);
3298
3299 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003300}
3301
3302static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003303{
Johannes Berg4c476992010-10-04 21:36:35 +02003304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3305 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003306
Michal Kazior60771782012-06-29 12:46:56 +02003307 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003308}
3309
Johannes Berg5727ef12007-12-19 02:03:34 +01003310static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3311 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3312 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3313 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003314 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003315 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003316 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003317};
3318
Johannes Bergeccb8e82009-05-11 21:57:56 +03003319static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003320 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003321 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003322{
3323 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003324 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003325 int flag;
3326
Johannes Bergeccb8e82009-05-11 21:57:56 +03003327 /*
3328 * Try parsing the new attribute first so userspace
3329 * can specify both for older kernels.
3330 */
3331 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3332 if (nla) {
3333 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003334
Johannes Bergeccb8e82009-05-11 21:57:56 +03003335 sta_flags = nla_data(nla);
3336 params->sta_flags_mask = sta_flags->mask;
3337 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003338 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003339 if ((params->sta_flags_mask |
3340 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3341 return -EINVAL;
3342 return 0;
3343 }
3344
3345 /* if present, parse the old attribute */
3346
3347 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003348 if (!nla)
3349 return 0;
3350
3351 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3352 nla, sta_flags_policy))
3353 return -EINVAL;
3354
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003355 /*
3356 * Only allow certain flags for interface types so that
3357 * other attributes are silently ignored. Remember that
3358 * this is backward compatibility code with old userspace
3359 * and shouldn't be hit in other cases anyway.
3360 */
3361 switch (iftype) {
3362 case NL80211_IFTYPE_AP:
3363 case NL80211_IFTYPE_AP_VLAN:
3364 case NL80211_IFTYPE_P2P_GO:
3365 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3366 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3367 BIT(NL80211_STA_FLAG_WME) |
3368 BIT(NL80211_STA_FLAG_MFP);
3369 break;
3370 case NL80211_IFTYPE_P2P_CLIENT:
3371 case NL80211_IFTYPE_STATION:
3372 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3373 BIT(NL80211_STA_FLAG_TDLS_PEER);
3374 break;
3375 case NL80211_IFTYPE_MESH_POINT:
3376 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3377 BIT(NL80211_STA_FLAG_MFP) |
3378 BIT(NL80211_STA_FLAG_AUTHORIZED);
3379 default:
3380 return -EINVAL;
3381 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003382
Johannes Berg3383b5a2012-05-10 20:14:43 +02003383 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3384 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003385 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003386
Johannes Berg3383b5a2012-05-10 20:14:43 +02003387 /* no longer support new API additions in old API */
3388 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3389 return -EINVAL;
3390 }
3391 }
3392
Johannes Berg5727ef12007-12-19 02:03:34 +01003393 return 0;
3394}
3395
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003396static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3397 int attr)
3398{
3399 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003400 u32 bitrate;
3401 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003402
3403 rate = nla_nest_start(msg, attr);
3404 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003405 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003406
3407 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3408 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003409 /* report 16-bit bitrate only if we can */
3410 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003411 if (bitrate > 0 &&
3412 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3413 return false;
3414 if (bitrate_compat > 0 &&
3415 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3416 return false;
3417
3418 if (info->flags & RATE_INFO_FLAGS_MCS) {
3419 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3420 return false;
3421 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3422 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3423 return false;
3424 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3425 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3426 return false;
3427 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3428 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3429 return false;
3430 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3431 return false;
3432 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3433 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3434 return false;
3435 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3436 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3437 return false;
3438 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3439 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3440 return false;
3441 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3442 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3443 return false;
3444 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3445 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3446 return false;
3447 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003448
3449 nla_nest_end(msg, rate);
3450 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003451}
3452
Felix Fietkau119363c2013-04-22 16:29:30 +02003453static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3454 int id)
3455{
3456 void *attr;
3457 int i = 0;
3458
3459 if (!mask)
3460 return true;
3461
3462 attr = nla_nest_start(msg, id);
3463 if (!attr)
3464 return false;
3465
3466 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3467 if (!(mask & BIT(i)))
3468 continue;
3469
3470 if (nla_put_u8(msg, i, signal[i]))
3471 return false;
3472 }
3473
3474 nla_nest_end(msg, attr);
3475
3476 return true;
3477}
3478
Eric W. Biederman15e47302012-09-07 20:12:54 +00003479static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003480 int flags,
3481 struct cfg80211_registered_device *rdev,
3482 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003483 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003484{
3485 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003486 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003487
Eric W. Biederman15e47302012-09-07 20:12:54 +00003488 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003489 if (!hdr)
3490 return -1;
3491
David S. Miller9360ffd2012-03-29 04:41:26 -04003492 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3493 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3494 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3495 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003496
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003497 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3498 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003499 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003500 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3501 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3502 sinfo->connected_time))
3503 goto nla_put_failure;
3504 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3505 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3506 sinfo->inactive_time))
3507 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003508 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3509 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003510 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003511 (u32)sinfo->rx_bytes))
3512 goto nla_put_failure;
3513 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003514 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003515 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3516 (u32)sinfo->tx_bytes))
3517 goto nla_put_failure;
3518 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3519 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003520 sinfo->rx_bytes))
3521 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003522 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3523 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003524 sinfo->tx_bytes))
3525 goto nla_put_failure;
3526 if ((sinfo->filled & STATION_INFO_LLID) &&
3527 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3528 goto nla_put_failure;
3529 if ((sinfo->filled & STATION_INFO_PLID) &&
3530 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3531 goto nla_put_failure;
3532 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3533 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3534 sinfo->plink_state))
3535 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003536 switch (rdev->wiphy.signal_type) {
3537 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003538 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3539 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3540 sinfo->signal))
3541 goto nla_put_failure;
3542 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3543 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3544 sinfo->signal_avg))
3545 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003546 break;
3547 default:
3548 break;
3549 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003550 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3551 if (!nl80211_put_signal(msg, sinfo->chains,
3552 sinfo->chain_signal,
3553 NL80211_STA_INFO_CHAIN_SIGNAL))
3554 goto nla_put_failure;
3555 }
3556 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3557 if (!nl80211_put_signal(msg, sinfo->chains,
3558 sinfo->chain_signal_avg,
3559 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3560 goto nla_put_failure;
3561 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003562 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003563 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3564 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003565 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003566 }
3567 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3568 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3569 NL80211_STA_INFO_RX_BITRATE))
3570 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003571 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003572 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3573 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3574 sinfo->rx_packets))
3575 goto nla_put_failure;
3576 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3577 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3578 sinfo->tx_packets))
3579 goto nla_put_failure;
3580 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3581 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3582 sinfo->tx_retries))
3583 goto nla_put_failure;
3584 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3585 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3586 sinfo->tx_failed))
3587 goto nla_put_failure;
3588 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3589 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3590 sinfo->beacon_loss_count))
3591 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003592 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3593 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3594 sinfo->local_pm))
3595 goto nla_put_failure;
3596 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3597 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3598 sinfo->peer_pm))
3599 goto nla_put_failure;
3600 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3601 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3602 sinfo->nonpeer_pm))
3603 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003604 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3605 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3606 if (!bss_param)
3607 goto nla_put_failure;
3608
David S. Miller9360ffd2012-03-29 04:41:26 -04003609 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3610 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3611 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3612 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3613 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3614 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3615 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3616 sinfo->bss_param.dtim_period) ||
3617 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3618 sinfo->bss_param.beacon_interval))
3619 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003620
3621 nla_nest_end(msg, bss_param);
3622 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003623 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3624 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3625 sizeof(struct nl80211_sta_flag_update),
3626 &sinfo->sta_flags))
3627 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003628 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3629 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3630 sinfo->t_offset))
3631 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003632 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003633
David S. Miller9360ffd2012-03-29 04:41:26 -04003634 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3635 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3636 sinfo->assoc_req_ies))
3637 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003638
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003639 return genlmsg_end(msg, hdr);
3640
3641 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003642 genlmsg_cancel(msg, hdr);
3643 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003644}
3645
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003646static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003647 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003648{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003649 struct station_info sinfo;
3650 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003651 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003652 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003653 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003654 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003655
Johannes Berg97990a02013-04-19 01:02:55 +02003656 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003657 if (err)
3658 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003659
Johannes Berg97990a02013-04-19 01:02:55 +02003660 if (!wdev->netdev) {
3661 err = -EINVAL;
3662 goto out_err;
3663 }
3664
Johannes Bergbba95fe2008-07-29 13:22:51 +02003665 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003666 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003667 goto out_err;
3668 }
3669
Johannes Bergbba95fe2008-07-29 13:22:51 +02003670 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003671 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003672 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003673 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003674 if (err == -ENOENT)
3675 break;
3676 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003677 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003678
3679 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003680 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003681 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003682 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003683 &sinfo) < 0)
3684 goto out;
3685
3686 sta_idx++;
3687 }
3688
3689
3690 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003691 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003692 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003693 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003694 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003695
3696 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003697}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003698
Johannes Berg5727ef12007-12-19 02:03:34 +01003699static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3700{
Johannes Berg4c476992010-10-04 21:36:35 +02003701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3702 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003703 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003704 struct sk_buff *msg;
3705 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003706 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003707
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003708 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003709
3710 if (!info->attrs[NL80211_ATTR_MAC])
3711 return -EINVAL;
3712
3713 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3714
Johannes Berg4c476992010-10-04 21:36:35 +02003715 if (!rdev->ops->get_station)
3716 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003717
Hila Gonene35e4d22012-06-27 17:19:42 +03003718 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003719 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003720 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003721
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003723 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003724 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003725
Eric W. Biederman15e47302012-09-07 20:12:54 +00003726 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003727 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003728 nlmsg_free(msg);
3729 return -ENOBUFS;
3730 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003731
Johannes Berg4c476992010-10-04 21:36:35 +02003732 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003733}
3734
Johannes Berg77ee7c82013-02-15 00:48:33 +01003735int cfg80211_check_station_change(struct wiphy *wiphy,
3736 struct station_parameters *params,
3737 enum cfg80211_station_type statype)
3738{
3739 if (params->listen_interval != -1)
3740 return -EINVAL;
3741 if (params->aid)
3742 return -EINVAL;
3743
3744 /* When you run into this, adjust the code below for the new flag */
3745 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3746
3747 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003748 case CFG80211_STA_MESH_PEER_KERNEL:
3749 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003750 /*
3751 * No ignoring the TDLS flag here -- the userspace mesh
3752 * code doesn't have the bug of including TDLS in the
3753 * mask everywhere.
3754 */
3755 if (params->sta_flags_mask &
3756 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3757 BIT(NL80211_STA_FLAG_MFP) |
3758 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3759 return -EINVAL;
3760 break;
3761 case CFG80211_STA_TDLS_PEER_SETUP:
3762 case CFG80211_STA_TDLS_PEER_ACTIVE:
3763 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3764 return -EINVAL;
3765 /* ignore since it can't change */
3766 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3767 break;
3768 default:
3769 /* disallow mesh-specific things */
3770 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3771 return -EINVAL;
3772 if (params->local_pm)
3773 return -EINVAL;
3774 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3775 return -EINVAL;
3776 }
3777
3778 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3779 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3780 /* TDLS can't be set, ... */
3781 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3782 return -EINVAL;
3783 /*
3784 * ... but don't bother the driver with it. This works around
3785 * a hostapd/wpa_supplicant issue -- it always includes the
3786 * TLDS_PEER flag in the mask even for AP mode.
3787 */
3788 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3789 }
3790
3791 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3792 /* reject other things that can't change */
3793 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3794 return -EINVAL;
3795 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3796 return -EINVAL;
3797 if (params->supported_rates)
3798 return -EINVAL;
3799 if (params->ext_capab || params->ht_capa || params->vht_capa)
3800 return -EINVAL;
3801 }
3802
3803 if (statype != CFG80211_STA_AP_CLIENT) {
3804 if (params->vlan)
3805 return -EINVAL;
3806 }
3807
3808 switch (statype) {
3809 case CFG80211_STA_AP_MLME_CLIENT:
3810 /* Use this only for authorizing/unauthorizing a station */
3811 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3812 return -EOPNOTSUPP;
3813 break;
3814 case CFG80211_STA_AP_CLIENT:
3815 /* accept only the listed bits */
3816 if (params->sta_flags_mask &
3817 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3818 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3819 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3820 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3821 BIT(NL80211_STA_FLAG_WME) |
3822 BIT(NL80211_STA_FLAG_MFP)))
3823 return -EINVAL;
3824
3825 /* but authenticated/associated only if driver handles it */
3826 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3827 params->sta_flags_mask &
3828 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3829 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3830 return -EINVAL;
3831 break;
3832 case CFG80211_STA_IBSS:
3833 case CFG80211_STA_AP_STA:
3834 /* reject any changes other than AUTHORIZED */
3835 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3836 return -EINVAL;
3837 break;
3838 case CFG80211_STA_TDLS_PEER_SETUP:
3839 /* reject any changes other than AUTHORIZED or WME */
3840 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3841 BIT(NL80211_STA_FLAG_WME)))
3842 return -EINVAL;
3843 /* force (at least) rates when authorizing */
3844 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3845 !params->supported_rates)
3846 return -EINVAL;
3847 break;
3848 case CFG80211_STA_TDLS_PEER_ACTIVE:
3849 /* reject any changes */
3850 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003851 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003852 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3853 return -EINVAL;
3854 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003855 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003856 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3857 return -EINVAL;
3858 break;
3859 }
3860
3861 return 0;
3862}
3863EXPORT_SYMBOL(cfg80211_check_station_change);
3864
Johannes Berg5727ef12007-12-19 02:03:34 +01003865/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003866 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003867 */
Johannes Berg80b99892011-11-18 16:23:01 +01003868static struct net_device *get_vlan(struct genl_info *info,
3869 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003870{
Johannes Berg463d0182009-07-14 00:33:35 +02003871 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003872 struct net_device *v;
3873 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003874
Johannes Berg80b99892011-11-18 16:23:01 +01003875 if (!vlanattr)
3876 return NULL;
3877
3878 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3879 if (!v)
3880 return ERR_PTR(-ENODEV);
3881
3882 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3883 ret = -EINVAL;
3884 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003885 }
Johannes Berg80b99892011-11-18 16:23:01 +01003886
Johannes Berg77ee7c82013-02-15 00:48:33 +01003887 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3888 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3889 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3890 ret = -EINVAL;
3891 goto error;
3892 }
3893
Johannes Berg80b99892011-11-18 16:23:01 +01003894 if (!netif_running(v)) {
3895 ret = -ENETDOWN;
3896 goto error;
3897 }
3898
3899 return v;
3900 error:
3901 dev_put(v);
3902 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003903}
3904
Jouni Malinendf881292013-02-14 21:10:54 +02003905static struct nla_policy
3906nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3907 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3908 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3909};
3910
Johannes Bergff276692013-02-15 00:09:01 +01003911static int nl80211_parse_sta_wme(struct genl_info *info,
3912 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003913{
Jouni Malinendf881292013-02-14 21:10:54 +02003914 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3915 struct nlattr *nla;
3916 int err;
3917
Jouni Malinendf881292013-02-14 21:10:54 +02003918 /* parse WME attributes if present */
3919 if (!info->attrs[NL80211_ATTR_STA_WME])
3920 return 0;
3921
3922 nla = info->attrs[NL80211_ATTR_STA_WME];
3923 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3924 nl80211_sta_wme_policy);
3925 if (err)
3926 return err;
3927
3928 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3929 params->uapsd_queues = nla_get_u8(
3930 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3931 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3932 return -EINVAL;
3933
3934 if (tb[NL80211_STA_WME_MAX_SP])
3935 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3936
3937 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3938 return -EINVAL;
3939
3940 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3941
3942 return 0;
3943}
3944
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303945static int nl80211_parse_sta_channel_info(struct genl_info *info,
3946 struct station_parameters *params)
3947{
3948 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3949 params->supported_channels =
3950 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3951 params->supported_channels_len =
3952 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3953 /*
3954 * Need to include at least one (first channel, number of
3955 * channels) tuple for each subband, and must have proper
3956 * tuples for the rest of the data as well.
3957 */
3958 if (params->supported_channels_len < 2)
3959 return -EINVAL;
3960 if (params->supported_channels_len % 2)
3961 return -EINVAL;
3962 }
3963
3964 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3965 params->supported_oper_classes =
3966 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3967 params->supported_oper_classes_len =
3968 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3969 /*
3970 * The value of the Length field of the Supported Operating
3971 * Classes element is between 2 and 253.
3972 */
3973 if (params->supported_oper_classes_len < 2 ||
3974 params->supported_oper_classes_len > 253)
3975 return -EINVAL;
3976 }
3977 return 0;
3978}
3979
Johannes Bergff276692013-02-15 00:09:01 +01003980static int nl80211_set_station_tdls(struct genl_info *info,
3981 struct station_parameters *params)
3982{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303983 int err;
Johannes Bergff276692013-02-15 00:09:01 +01003984 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003985 if (info->attrs[NL80211_ATTR_PEER_AID])
3986 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003987 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3988 params->ht_capa =
3989 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3990 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3991 params->vht_capa =
3992 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3993
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303994 err = nl80211_parse_sta_channel_info(info, params);
3995 if (err)
3996 return err;
3997
Johannes Bergff276692013-02-15 00:09:01 +01003998 return nl80211_parse_sta_wme(info, params);
3999}
4000
Johannes Berg5727ef12007-12-19 02:03:34 +01004001static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4002{
Johannes Berg4c476992010-10-04 21:36:35 +02004003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004004 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004005 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004006 u8 *mac_addr;
4007 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004008
4009 memset(&params, 0, sizeof(params));
4010
4011 params.listen_interval = -1;
4012
Johannes Berg77ee7c82013-02-15 00:48:33 +01004013 if (!rdev->ops->change_station)
4014 return -EOPNOTSUPP;
4015
Johannes Berg5727ef12007-12-19 02:03:34 +01004016 if (info->attrs[NL80211_ATTR_STA_AID])
4017 return -EINVAL;
4018
4019 if (!info->attrs[NL80211_ATTR_MAC])
4020 return -EINVAL;
4021
4022 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4023
4024 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4025 params.supported_rates =
4026 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4027 params.supported_rates_len =
4028 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4029 }
4030
Jouni Malinen9d62a982013-02-14 21:10:13 +02004031 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4032 params.capability =
4033 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4034 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4035 }
4036
4037 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4038 params.ext_capab =
4039 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4040 params.ext_capab_len =
4041 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4042 }
4043
Jouni Malinendf881292013-02-14 21:10:54 +02004044 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004045 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004046
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004047 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004048 return -EINVAL;
4049
Johannes Bergf8bacc22013-02-14 23:27:01 +01004050 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004051 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004052 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4053 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4054 return -EINVAL;
4055 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004056
Johannes Bergf8bacc22013-02-14 23:27:01 +01004057 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004058 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004059 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4060 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4061 return -EINVAL;
4062 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4063 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004064
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004065 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4066 enum nl80211_mesh_power_mode pm = nla_get_u32(
4067 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4068
4069 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4070 pm > NL80211_MESH_POWER_MAX)
4071 return -EINVAL;
4072
4073 params.local_pm = pm;
4074 }
4075
Johannes Berg77ee7c82013-02-15 00:48:33 +01004076 /* Include parameters for TDLS peer (will check later) */
4077 err = nl80211_set_station_tdls(info, &params);
4078 if (err)
4079 return err;
4080
4081 params.vlan = get_vlan(info, rdev);
4082 if (IS_ERR(params.vlan))
4083 return PTR_ERR(params.vlan);
4084
Johannes Berga97f4422009-06-18 17:23:43 +02004085 switch (dev->ieee80211_ptr->iftype) {
4086 case NL80211_IFTYPE_AP:
4087 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004088 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004089 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004090 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004091 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004092 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004093 break;
4094 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004095 err = -EOPNOTSUPP;
4096 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004097 }
4098
Johannes Berg77ee7c82013-02-15 00:48:33 +01004099 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004100 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004101
Johannes Berg77ee7c82013-02-15 00:48:33 +01004102 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004103 if (params.vlan)
4104 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004105
Johannes Berg5727ef12007-12-19 02:03:34 +01004106 return err;
4107}
4108
4109static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4110{
Johannes Berg4c476992010-10-04 21:36:35 +02004111 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004112 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004113 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004114 struct station_parameters params;
4115 u8 *mac_addr = NULL;
4116
4117 memset(&params, 0, sizeof(params));
4118
Johannes Berg984c3112013-02-14 23:43:25 +01004119 if (!rdev->ops->add_station)
4120 return -EOPNOTSUPP;
4121
Johannes Berg5727ef12007-12-19 02:03:34 +01004122 if (!info->attrs[NL80211_ATTR_MAC])
4123 return -EINVAL;
4124
Johannes Berg5727ef12007-12-19 02:03:34 +01004125 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4126 return -EINVAL;
4127
4128 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4129 return -EINVAL;
4130
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004131 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4132 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004133 return -EINVAL;
4134
Johannes Berg5727ef12007-12-19 02:03:34 +01004135 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4136 params.supported_rates =
4137 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4138 params.supported_rates_len =
4139 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4140 params.listen_interval =
4141 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004142
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004143 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004144 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004145 else
4146 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004147 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4148 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004149
Jouni Malinen9d62a982013-02-14 21:10:13 +02004150 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4151 params.capability =
4152 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4153 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4154 }
4155
4156 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4157 params.ext_capab =
4158 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4159 params.ext_capab_len =
4160 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4161 }
4162
Jouni Malinen36aedc902008-08-25 11:58:58 +03004163 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4164 params.ht_capa =
4165 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004166
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004167 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4168 params.vht_capa =
4169 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4170
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004171 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4172 params.opmode_notif_used = true;
4173 params.opmode_notif =
4174 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4175 }
4176
Johannes Bergf8bacc22013-02-14 23:27:01 +01004177 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004178 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004179 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4180 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4181 return -EINVAL;
4182 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004183
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304184 err = nl80211_parse_sta_channel_info(info, &params);
4185 if (err)
4186 return err;
4187
Johannes Bergff276692013-02-15 00:09:01 +01004188 err = nl80211_parse_sta_wme(info, &params);
4189 if (err)
4190 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004191
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004192 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004193 return -EINVAL;
4194
Johannes Berg77ee7c82013-02-15 00:48:33 +01004195 /* When you run into this, adjust the code below for the new flag */
4196 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4197
Johannes Bergbdd90d52011-12-14 12:20:27 +01004198 switch (dev->ieee80211_ptr->iftype) {
4199 case NL80211_IFTYPE_AP:
4200 case NL80211_IFTYPE_AP_VLAN:
4201 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004202 /* ignore WME attributes if iface/sta is not capable */
4203 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4204 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4205 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004206
Johannes Bergbdd90d52011-12-14 12:20:27 +01004207 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004208 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4209 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004210 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004211 /* but don't bother the driver with it */
4212 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004213
Johannes Bergd582cff2012-10-26 17:53:44 +02004214 /* allow authenticated/associated only if driver handles it */
4215 if (!(rdev->wiphy.features &
4216 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4217 params.sta_flags_mask &
4218 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4219 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4220 return -EINVAL;
4221
Johannes Bergbdd90d52011-12-14 12:20:27 +01004222 /* must be last in here for error handling */
4223 params.vlan = get_vlan(info, rdev);
4224 if (IS_ERR(params.vlan))
4225 return PTR_ERR(params.vlan);
4226 break;
4227 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004228 /* ignore uAPSD data */
4229 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4230
Johannes Bergd582cff2012-10-26 17:53:44 +02004231 /* associated is disallowed */
4232 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4233 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004234 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004235 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4236 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004237 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004238 break;
4239 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004240 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004241 /* ignore uAPSD data */
4242 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4243
Johannes Berg77ee7c82013-02-15 00:48:33 +01004244 /* these are disallowed */
4245 if (params.sta_flags_mask &
4246 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4247 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004248 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004249 /* Only TDLS peers can be added */
4250 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4251 return -EINVAL;
4252 /* Can only add if TDLS ... */
4253 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4254 return -EOPNOTSUPP;
4255 /* ... with external setup is supported */
4256 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4257 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004258 /*
4259 * Older wpa_supplicant versions always mark the TDLS peer
4260 * as authorized, but it shouldn't yet be.
4261 */
4262 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004263 break;
4264 default:
4265 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004266 }
4267
Johannes Bergbdd90d52011-12-14 12:20:27 +01004268 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004269
Hila Gonene35e4d22012-06-27 17:19:42 +03004270 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004271
Johannes Berg5727ef12007-12-19 02:03:34 +01004272 if (params.vlan)
4273 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004274 return err;
4275}
4276
4277static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4278{
Johannes Berg4c476992010-10-04 21:36:35 +02004279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4280 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004281 u8 *mac_addr = NULL;
4282
4283 if (info->attrs[NL80211_ATTR_MAC])
4284 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4285
Johannes Berge80cf852009-05-11 14:43:13 +02004286 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004287 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004288 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004289 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4290 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004291
Johannes Berg4c476992010-10-04 21:36:35 +02004292 if (!rdev->ops->del_station)
4293 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004294
Hila Gonene35e4d22012-06-27 17:19:42 +03004295 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004296}
4297
Eric W. Biederman15e47302012-09-07 20:12:54 +00004298static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004299 int flags, struct net_device *dev,
4300 u8 *dst, u8 *next_hop,
4301 struct mpath_info *pinfo)
4302{
4303 void *hdr;
4304 struct nlattr *pinfoattr;
4305
Eric W. Biederman15e47302012-09-07 20:12:54 +00004306 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004307 if (!hdr)
4308 return -1;
4309
David S. Miller9360ffd2012-03-29 04:41:26 -04004310 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4311 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4312 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4313 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4314 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004315
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004316 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4317 if (!pinfoattr)
4318 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004319 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4320 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4321 pinfo->frame_qlen))
4322 goto nla_put_failure;
4323 if (((pinfo->filled & MPATH_INFO_SN) &&
4324 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4325 ((pinfo->filled & MPATH_INFO_METRIC) &&
4326 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4327 pinfo->metric)) ||
4328 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4329 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4330 pinfo->exptime)) ||
4331 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4332 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4333 pinfo->flags)) ||
4334 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4335 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4336 pinfo->discovery_timeout)) ||
4337 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4338 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4339 pinfo->discovery_retries)))
4340 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004341
4342 nla_nest_end(msg, pinfoattr);
4343
4344 return genlmsg_end(msg, hdr);
4345
4346 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004347 genlmsg_cancel(msg, hdr);
4348 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004349}
4350
4351static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004352 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004353{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004354 struct mpath_info pinfo;
4355 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004356 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004357 u8 dst[ETH_ALEN];
4358 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004359 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004360 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004361
Johannes Berg97990a02013-04-19 01:02:55 +02004362 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004363 if (err)
4364 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004365
4366 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004367 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004368 goto out_err;
4369 }
4370
Johannes Berg97990a02013-04-19 01:02:55 +02004371 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004372 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004373 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004374 }
4375
Johannes Bergbba95fe2008-07-29 13:22:51 +02004376 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004377 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4378 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004379 if (err == -ENOENT)
4380 break;
4381 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004382 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004383
Eric W. Biederman15e47302012-09-07 20:12:54 +00004384 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004385 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004386 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004387 &pinfo) < 0)
4388 goto out;
4389
4390 path_idx++;
4391 }
4392
4393
4394 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004395 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004396 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004397 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004398 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004399 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004400}
4401
4402static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4403{
Johannes Berg4c476992010-10-04 21:36:35 +02004404 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004405 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004406 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004407 struct mpath_info pinfo;
4408 struct sk_buff *msg;
4409 u8 *dst = NULL;
4410 u8 next_hop[ETH_ALEN];
4411
4412 memset(&pinfo, 0, sizeof(pinfo));
4413
4414 if (!info->attrs[NL80211_ATTR_MAC])
4415 return -EINVAL;
4416
4417 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4418
Johannes Berg4c476992010-10-04 21:36:35 +02004419 if (!rdev->ops->get_mpath)
4420 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004421
Johannes Berg4c476992010-10-04 21:36:35 +02004422 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4423 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004424
Hila Gonene35e4d22012-06-27 17:19:42 +03004425 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004426 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004427 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004428
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004430 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004431 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004432
Eric W. Biederman15e47302012-09-07 20:12:54 +00004433 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004434 dev, dst, next_hop, &pinfo) < 0) {
4435 nlmsg_free(msg);
4436 return -ENOBUFS;
4437 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004438
Johannes Berg4c476992010-10-04 21:36:35 +02004439 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004440}
4441
4442static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4443{
Johannes Berg4c476992010-10-04 21:36:35 +02004444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4445 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004446 u8 *dst = NULL;
4447 u8 *next_hop = NULL;
4448
4449 if (!info->attrs[NL80211_ATTR_MAC])
4450 return -EINVAL;
4451
4452 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4453 return -EINVAL;
4454
4455 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4456 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4457
Johannes Berg4c476992010-10-04 21:36:35 +02004458 if (!rdev->ops->change_mpath)
4459 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004460
Johannes Berg4c476992010-10-04 21:36:35 +02004461 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4462 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004463
Hila Gonene35e4d22012-06-27 17:19:42 +03004464 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004465}
Johannes Berg4c476992010-10-04 21:36:35 +02004466
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004467static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4468{
Johannes Berg4c476992010-10-04 21:36:35 +02004469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4470 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004471 u8 *dst = NULL;
4472 u8 *next_hop = NULL;
4473
4474 if (!info->attrs[NL80211_ATTR_MAC])
4475 return -EINVAL;
4476
4477 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4478 return -EINVAL;
4479
4480 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4481 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4482
Johannes Berg4c476992010-10-04 21:36:35 +02004483 if (!rdev->ops->add_mpath)
4484 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004485
Johannes Berg4c476992010-10-04 21:36:35 +02004486 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4487 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004488
Hila Gonene35e4d22012-06-27 17:19:42 +03004489 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004490}
4491
4492static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4493{
Johannes Berg4c476992010-10-04 21:36:35 +02004494 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4495 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004496 u8 *dst = NULL;
4497
4498 if (info->attrs[NL80211_ATTR_MAC])
4499 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4500
Johannes Berg4c476992010-10-04 21:36:35 +02004501 if (!rdev->ops->del_mpath)
4502 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004503
Hila Gonene35e4d22012-06-27 17:19:42 +03004504 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004505}
4506
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004507static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4508{
Johannes Berg4c476992010-10-04 21:36:35 +02004509 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4510 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004511 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004512 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004513 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004514
4515 memset(&params, 0, sizeof(params));
4516 /* default to not changing parameters */
4517 params.use_cts_prot = -1;
4518 params.use_short_preamble = -1;
4519 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004520 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004521 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004522 params.p2p_ctwindow = -1;
4523 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004524
4525 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4526 params.use_cts_prot =
4527 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4528 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4529 params.use_short_preamble =
4530 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4531 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4532 params.use_short_slot_time =
4533 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004534 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4535 params.basic_rates =
4536 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4537 params.basic_rates_len =
4538 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4539 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004540 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4541 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004542 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4543 params.ht_opmode =
4544 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004545
Johannes Berg53cabad2012-11-14 15:17:28 +01004546 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4547 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4548 return -EINVAL;
4549 params.p2p_ctwindow =
4550 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4551 if (params.p2p_ctwindow < 0)
4552 return -EINVAL;
4553 if (params.p2p_ctwindow != 0 &&
4554 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4555 return -EINVAL;
4556 }
4557
4558 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4559 u8 tmp;
4560
4561 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4562 return -EINVAL;
4563 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4564 if (tmp > 1)
4565 return -EINVAL;
4566 params.p2p_opp_ps = tmp;
4567 if (params.p2p_opp_ps &&
4568 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4569 return -EINVAL;
4570 }
4571
Johannes Berg4c476992010-10-04 21:36:35 +02004572 if (!rdev->ops->change_bss)
4573 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004574
Johannes Berg074ac8d2010-09-16 14:58:22 +02004575 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004576 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4577 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004578
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004579 wdev_lock(wdev);
4580 err = rdev_change_bss(rdev, dev, &params);
4581 wdev_unlock(wdev);
4582
4583 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004584}
4585
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004586static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004587 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4588 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4589 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4590 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4591 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4592 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4593};
4594
4595static int parse_reg_rule(struct nlattr *tb[],
4596 struct ieee80211_reg_rule *reg_rule)
4597{
4598 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4599 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4600
4601 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4602 return -EINVAL;
4603 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4604 return -EINVAL;
4605 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4606 return -EINVAL;
4607 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4608 return -EINVAL;
4609 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4610 return -EINVAL;
4611
4612 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4613
4614 freq_range->start_freq_khz =
4615 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4616 freq_range->end_freq_khz =
4617 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4618 freq_range->max_bandwidth_khz =
4619 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4620
4621 power_rule->max_eirp =
4622 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4623
4624 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4625 power_rule->max_antenna_gain =
4626 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4627
4628 return 0;
4629}
4630
4631static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4632{
4633 int r;
4634 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004635 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004636
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004637 /*
4638 * You should only get this when cfg80211 hasn't yet initialized
4639 * completely when built-in to the kernel right between the time
4640 * window between nl80211_init() and regulatory_init(), if that is
4641 * even possible.
4642 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004643 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004644 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004645
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004646 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4647 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004648
4649 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4650
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004651 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4652 user_reg_hint_type =
4653 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4654 else
4655 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4656
4657 switch (user_reg_hint_type) {
4658 case NL80211_USER_REG_HINT_USER:
4659 case NL80211_USER_REG_HINT_CELL_BASE:
4660 break;
4661 default:
4662 return -EINVAL;
4663 }
4664
4665 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004666
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004667 return r;
4668}
4669
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004670static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004671 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004672{
Johannes Berg4c476992010-10-04 21:36:35 +02004673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004674 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004675 struct wireless_dev *wdev = dev->ieee80211_ptr;
4676 struct mesh_config cur_params;
4677 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004678 void *hdr;
4679 struct nlattr *pinfoattr;
4680 struct sk_buff *msg;
4681
Johannes Berg29cbe682010-12-03 09:20:44 +01004682 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4683 return -EOPNOTSUPP;
4684
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004685 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004686 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004687
Johannes Berg29cbe682010-12-03 09:20:44 +01004688 wdev_lock(wdev);
4689 /* If not connected, get default parameters */
4690 if (!wdev->mesh_id_len)
4691 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4692 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004693 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004694 wdev_unlock(wdev);
4695
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004696 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004697 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004698
4699 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004701 if (!msg)
4702 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004703 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004704 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004705 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004706 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004707 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004708 if (!pinfoattr)
4709 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004710 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4711 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4712 cur_params.dot11MeshRetryTimeout) ||
4713 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4714 cur_params.dot11MeshConfirmTimeout) ||
4715 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4716 cur_params.dot11MeshHoldingTimeout) ||
4717 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4718 cur_params.dot11MeshMaxPeerLinks) ||
4719 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4720 cur_params.dot11MeshMaxRetries) ||
4721 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4722 cur_params.dot11MeshTTL) ||
4723 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4724 cur_params.element_ttl) ||
4725 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4726 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004727 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4728 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004729 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4730 cur_params.dot11MeshHWMPmaxPREQretries) ||
4731 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4732 cur_params.path_refresh_time) ||
4733 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4734 cur_params.min_discovery_timeout) ||
4735 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4736 cur_params.dot11MeshHWMPactivePathTimeout) ||
4737 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4738 cur_params.dot11MeshHWMPpreqMinInterval) ||
4739 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4740 cur_params.dot11MeshHWMPperrMinInterval) ||
4741 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4742 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4743 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4744 cur_params.dot11MeshHWMPRootMode) ||
4745 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4746 cur_params.dot11MeshHWMPRannInterval) ||
4747 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4748 cur_params.dot11MeshGateAnnouncementProtocol) ||
4749 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4750 cur_params.dot11MeshForwarding) ||
4751 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004752 cur_params.rssi_threshold) ||
4753 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004754 cur_params.ht_opmode) ||
4755 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4756 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4757 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004758 cur_params.dot11MeshHWMProotInterval) ||
4759 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004760 cur_params.dot11MeshHWMPconfirmationInterval) ||
4761 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4762 cur_params.power_mode) ||
4763 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004764 cur_params.dot11MeshAwakeWindowDuration) ||
4765 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4766 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004767 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004768 nla_nest_end(msg, pinfoattr);
4769 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004770 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004771
Johannes Berg3b858752009-03-12 09:55:09 +01004772 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004773 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004774 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004775 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004776 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004777}
4778
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004779static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004780 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4781 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4782 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4783 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4784 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4785 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004786 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004787 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004788 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004789 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4790 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4791 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4792 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4793 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004794 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004795 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004796 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004797 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004798 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004799 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004800 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4801 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004802 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4803 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004804 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004805 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4806 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004807 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004808};
4809
Javier Cardonac80d5452010-12-16 17:37:49 -08004810static const struct nla_policy
4811 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004812 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004813 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4814 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004815 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004816 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004817 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004818 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004819 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004820 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004821};
4822
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004823static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004824 struct mesh_config *cfg,
4825 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004826{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004827 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004828 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004829
Marco Porschea54fba2013-01-07 16:04:48 +01004830#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4831do { \
4832 if (tb[attr]) { \
4833 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4834 return -EINVAL; \
4835 cfg->param = fn(tb[attr]); \
4836 mask |= (1 << (attr - 1)); \
4837 } \
4838} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004839
4840
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004841 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004842 return -EINVAL;
4843 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004844 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004845 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004846 return -EINVAL;
4847
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004848 /* This makes sure that there aren't more than 32 mesh config
4849 * parameters (otherwise our bitfield scheme would not work.) */
4850 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4851
4852 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004853 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004854 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4855 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004856 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004857 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4858 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004859 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004860 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4861 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004863 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4864 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004866 mask, NL80211_MESHCONF_MAX_RETRIES,
4867 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004869 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004870 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004871 mask, NL80211_MESHCONF_ELEMENT_TTL,
4872 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004873 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004874 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4875 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004876 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4877 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004878 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4879 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004880 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004881 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4882 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004884 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4885 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004886 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004887 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4888 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004889 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4890 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004891 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4892 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004893 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004894 1, 65535, mask,
4895 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004896 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004898 1, 65535, mask,
4899 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004900 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004902 dot11MeshHWMPnetDiameterTraversalTime,
4903 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004904 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4905 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4907 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4908 nla_get_u8);
4909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4910 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004911 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004912 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004913 dot11MeshGateAnnouncementProtocol, 0, 1,
4914 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004915 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004917 mask, NL80211_MESHCONF_FORWARDING,
4918 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004921 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004923 mask, NL80211_MESHCONF_HT_OPMODE,
4924 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004926 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004927 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4928 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004930 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4931 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004933 dot11MeshHWMPconfirmationInterval,
4934 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004935 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4936 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004937 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4938 NL80211_MESH_POWER_ACTIVE,
4939 NL80211_MESH_POWER_MAX,
4940 mask, NL80211_MESHCONF_POWER_MODE,
4941 nla_get_u32);
4942 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4943 0, 65535, mask,
4944 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004945 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4946 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4947 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004948 if (mask_out)
4949 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004950
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004951 return 0;
4952
4953#undef FILL_IN_MESH_PARAM_IF_SET
4954}
4955
Javier Cardonac80d5452010-12-16 17:37:49 -08004956static int nl80211_parse_mesh_setup(struct genl_info *info,
4957 struct mesh_setup *setup)
4958{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004959 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004960 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4961
4962 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4963 return -EINVAL;
4964 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4965 info->attrs[NL80211_ATTR_MESH_SETUP],
4966 nl80211_mesh_setup_params_policy))
4967 return -EINVAL;
4968
Javier Cardonad299a1f2012-03-31 11:31:33 -07004969 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4970 setup->sync_method =
4971 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4972 IEEE80211_SYNC_METHOD_VENDOR :
4973 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4974
Javier Cardonac80d5452010-12-16 17:37:49 -08004975 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4976 setup->path_sel_proto =
4977 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4978 IEEE80211_PATH_PROTOCOL_VENDOR :
4979 IEEE80211_PATH_PROTOCOL_HWMP;
4980
4981 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4982 setup->path_metric =
4983 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4984 IEEE80211_PATH_METRIC_VENDOR :
4985 IEEE80211_PATH_METRIC_AIRTIME;
4986
Javier Cardona581a8b02011-04-07 15:08:27 -07004987
4988 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004989 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004990 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004991 if (!is_valid_ie_attr(ieattr))
4992 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004993 setup->ie = nla_data(ieattr);
4994 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004995 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004996 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4997 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4998 return -EINVAL;
4999 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005000 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5001 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005002 if (setup->is_secure)
5003 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005004
Colleen Twitty6e16d902013-05-08 11:45:59 -07005005 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5006 if (!setup->user_mpm)
5007 return -EINVAL;
5008 setup->auth_id =
5009 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5010 }
5011
Javier Cardonac80d5452010-12-16 17:37:49 -08005012 return 0;
5013}
5014
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005015static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005016 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005017{
5018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5019 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005020 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005021 struct mesh_config cfg;
5022 u32 mask;
5023 int err;
5024
Johannes Berg29cbe682010-12-03 09:20:44 +01005025 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5026 return -EOPNOTSUPP;
5027
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005028 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005029 return -EOPNOTSUPP;
5030
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005031 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005032 if (err)
5033 return err;
5034
Johannes Berg29cbe682010-12-03 09:20:44 +01005035 wdev_lock(wdev);
5036 if (!wdev->mesh_id_len)
5037 err = -ENOLINK;
5038
5039 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005040 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005041
5042 wdev_unlock(wdev);
5043
5044 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005045}
5046
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005047static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5048{
Johannes Berg458f4f92012-12-06 15:47:38 +01005049 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005050 struct sk_buff *msg;
5051 void *hdr = NULL;
5052 struct nlattr *nl_reg_rules;
5053 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005054
5055 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005056 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005057
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005059 if (!msg)
5060 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005061
Eric W. Biederman15e47302012-09-07 20:12:54 +00005062 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005063 NL80211_CMD_GET_REG);
5064 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005065 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005066
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005067 if (reg_last_request_cell_base() &&
5068 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5069 NL80211_USER_REG_HINT_CELL_BASE))
5070 goto nla_put_failure;
5071
Johannes Berg458f4f92012-12-06 15:47:38 +01005072 rcu_read_lock();
5073 regdom = rcu_dereference(cfg80211_regdomain);
5074
5075 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5076 (regdom->dfs_region &&
5077 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5078 goto nla_put_failure_rcu;
5079
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005080 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5081 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005082 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005083
Johannes Berg458f4f92012-12-06 15:47:38 +01005084 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005085 struct nlattr *nl_reg_rule;
5086 const struct ieee80211_reg_rule *reg_rule;
5087 const struct ieee80211_freq_range *freq_range;
5088 const struct ieee80211_power_rule *power_rule;
5089
Johannes Berg458f4f92012-12-06 15:47:38 +01005090 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005091 freq_range = &reg_rule->freq_range;
5092 power_rule = &reg_rule->power_rule;
5093
5094 nl_reg_rule = nla_nest_start(msg, i);
5095 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005097
David S. Miller9360ffd2012-03-29 04:41:26 -04005098 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5099 reg_rule->flags) ||
5100 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5101 freq_range->start_freq_khz) ||
5102 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5103 freq_range->end_freq_khz) ||
5104 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5105 freq_range->max_bandwidth_khz) ||
5106 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5107 power_rule->max_antenna_gain) ||
5108 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5109 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005110 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005111
5112 nla_nest_end(msg, nl_reg_rule);
5113 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005114 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005115
5116 nla_nest_end(msg, nl_reg_rules);
5117
5118 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005119 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005120
Johannes Berg458f4f92012-12-06 15:47:38 +01005121nla_put_failure_rcu:
5122 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005123nla_put_failure:
5124 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005125put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005126 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005127 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005128}
5129
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005130static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5131{
5132 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5133 struct nlattr *nl_reg_rule;
5134 char *alpha2 = NULL;
5135 int rem_reg_rules = 0, r = 0;
5136 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005137 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005138 struct ieee80211_regdomain *rd = NULL;
5139
5140 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5141 return -EINVAL;
5142
5143 if (!info->attrs[NL80211_ATTR_REG_RULES])
5144 return -EINVAL;
5145
5146 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5147
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005148 if (info->attrs[NL80211_ATTR_DFS_REGION])
5149 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5150
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005151 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005152 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005153 num_rules++;
5154 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005155 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005156 }
5157
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005158 if (!reg_is_valid_request(alpha2))
5159 return -EINVAL;
5160
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005161 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005162 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005163
5164 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005165 if (!rd)
5166 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005167
5168 rd->n_reg_rules = num_rules;
5169 rd->alpha2[0] = alpha2[0];
5170 rd->alpha2[1] = alpha2[1];
5171
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005172 /*
5173 * Disable DFS master mode if the DFS region was
5174 * not supported or known on this kernel.
5175 */
5176 if (reg_supported_dfs_region(dfs_region))
5177 rd->dfs_region = dfs_region;
5178
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005179 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005180 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005182 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5183 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005184 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5185 if (r)
5186 goto bad_reg;
5187
5188 rule_idx++;
5189
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005190 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5191 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005192 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005193 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005194 }
5195
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005196 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005197 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005198 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005199
Johannes Bergd2372b32008-10-24 20:32:20 +02005200 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005201 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005202 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005203}
5204
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005205static int validate_scan_freqs(struct nlattr *freqs)
5206{
5207 struct nlattr *attr1, *attr2;
5208 int n_channels = 0, tmp1, tmp2;
5209
5210 nla_for_each_nested(attr1, freqs, tmp1) {
5211 n_channels++;
5212 /*
5213 * Some hardware has a limited channel list for
5214 * scanning, and it is pretty much nonsensical
5215 * to scan for a channel twice, so disallow that
5216 * and don't require drivers to check that the
5217 * channel list they get isn't longer than what
5218 * they can scan, as long as they can scan all
5219 * the channels they registered at once.
5220 */
5221 nla_for_each_nested(attr2, freqs, tmp2)
5222 if (attr1 != attr2 &&
5223 nla_get_u32(attr1) == nla_get_u32(attr2))
5224 return 0;
5225 }
5226
5227 return n_channels;
5228}
5229
Johannes Berg2a519312009-02-10 21:25:55 +01005230static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5231{
Johannes Berg4c476992010-10-04 21:36:35 +02005232 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005233 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005234 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005235 struct nlattr *attr;
5236 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005237 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005238 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005239
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005240 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5241 return -EINVAL;
5242
Johannes Berg79c97e92009-07-07 03:56:12 +02005243 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005244
Johannes Berg4c476992010-10-04 21:36:35 +02005245 if (!rdev->ops->scan)
5246 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005247
Johannes Bergf9d15d12014-01-22 11:14:19 +02005248 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005249 err = -EBUSY;
5250 goto unlock;
5251 }
Johannes Berg2a519312009-02-10 21:25:55 +01005252
5253 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005254 n_channels = validate_scan_freqs(
5255 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005256 if (!n_channels) {
5257 err = -EINVAL;
5258 goto unlock;
5259 }
Johannes Berg2a519312009-02-10 21:25:55 +01005260 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005261 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005262 }
5263
5264 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5265 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5266 n_ssids++;
5267
Johannes Bergf9f47522013-03-19 15:04:07 +01005268 if (n_ssids > wiphy->max_scan_ssids) {
5269 err = -EINVAL;
5270 goto unlock;
5271 }
Johannes Berg2a519312009-02-10 21:25:55 +01005272
Jouni Malinen70692ad2009-02-16 19:39:13 +02005273 if (info->attrs[NL80211_ATTR_IE])
5274 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5275 else
5276 ie_len = 0;
5277
Johannes Bergf9f47522013-03-19 15:04:07 +01005278 if (ie_len > wiphy->max_scan_ie_len) {
5279 err = -EINVAL;
5280 goto unlock;
5281 }
Johannes Berg18a83652009-03-31 12:12:05 +02005282
Johannes Berg2a519312009-02-10 21:25:55 +01005283 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005284 + sizeof(*request->ssids) * n_ssids
5285 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005286 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005287 if (!request) {
5288 err = -ENOMEM;
5289 goto unlock;
5290 }
Johannes Berg2a519312009-02-10 21:25:55 +01005291
Johannes Berg2a519312009-02-10 21:25:55 +01005292 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005293 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005294 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005295 if (ie_len) {
5296 if (request->ssids)
5297 request->ie = (void *)(request->ssids + n_ssids);
5298 else
5299 request->ie = (void *)(request->channels + n_channels);
5300 }
Johannes Berg2a519312009-02-10 21:25:55 +01005301
Johannes Berg584991d2009-11-02 13:32:03 +01005302 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005303 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5304 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005305 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005306 struct ieee80211_channel *chan;
5307
5308 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5309
5310 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005311 err = -EINVAL;
5312 goto out_free;
5313 }
Johannes Berg584991d2009-11-02 13:32:03 +01005314
5315 /* ignore disabled channels */
5316 if (chan->flags & IEEE80211_CHAN_DISABLED)
5317 continue;
5318
5319 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005320 i++;
5321 }
5322 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005323 enum ieee80211_band band;
5324
Johannes Berg2a519312009-02-10 21:25:55 +01005325 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005326 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5327 int j;
5328 if (!wiphy->bands[band])
5329 continue;
5330 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005331 struct ieee80211_channel *chan;
5332
5333 chan = &wiphy->bands[band]->channels[j];
5334
5335 if (chan->flags & IEEE80211_CHAN_DISABLED)
5336 continue;
5337
5338 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005339 i++;
5340 }
5341 }
5342 }
5343
Johannes Berg584991d2009-11-02 13:32:03 +01005344 if (!i) {
5345 err = -EINVAL;
5346 goto out_free;
5347 }
5348
5349 request->n_channels = i;
5350
Johannes Berg2a519312009-02-10 21:25:55 +01005351 i = 0;
5352 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5353 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005354 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005355 err = -EINVAL;
5356 goto out_free;
5357 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005358 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005359 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005360 i++;
5361 }
5362 }
5363
Jouni Malinen70692ad2009-02-16 19:39:13 +02005364 if (info->attrs[NL80211_ATTR_IE]) {
5365 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005366 memcpy((void *)request->ie,
5367 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005368 request->ie_len);
5369 }
5370
Johannes Berg34850ab2011-07-18 18:08:35 +02005371 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005372 if (wiphy->bands[i])
5373 request->rates[i] =
5374 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005375
5376 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5377 nla_for_each_nested(attr,
5378 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5379 tmp) {
5380 enum ieee80211_band band = nla_type(attr);
5381
Dan Carpenter84404622011-07-29 11:52:18 +03005382 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005383 err = -EINVAL;
5384 goto out_free;
5385 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005386
5387 if (!wiphy->bands[band])
5388 continue;
5389
Johannes Berg34850ab2011-07-18 18:08:35 +02005390 err = ieee80211_get_ratemask(wiphy->bands[band],
5391 nla_data(attr),
5392 nla_len(attr),
5393 &request->rates[band]);
5394 if (err)
5395 goto out_free;
5396 }
5397 }
5398
Sam Leffler46856bb2012-10-11 21:03:32 -07005399 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005400 request->flags = nla_get_u32(
5401 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005402 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5403 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005404 err = -EOPNOTSUPP;
5405 goto out_free;
5406 }
5407 }
Sam Lefflered4737712012-10-11 21:03:31 -07005408
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305409 request->no_cck =
5410 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5411
Johannes Bergfd014282012-06-18 19:17:03 +02005412 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005413 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005414 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005415
Johannes Berg79c97e92009-07-07 03:56:12 +02005416 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005417 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005418
Johannes Berg463d0182009-07-14 00:33:35 +02005419 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005420 nl80211_send_scan_start(rdev, wdev);
5421 if (wdev->netdev)
5422 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005423 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005424 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005425 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005426 kfree(request);
5427 }
Johannes Berg3b858752009-03-12 09:55:09 +01005428
Johannes Bergf9f47522013-03-19 15:04:07 +01005429 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005430 return err;
5431}
5432
Luciano Coelho807f8a82011-05-11 17:09:35 +03005433static int nl80211_start_sched_scan(struct sk_buff *skb,
5434 struct genl_info *info)
5435{
5436 struct cfg80211_sched_scan_request *request;
5437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5438 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005439 struct nlattr *attr;
5440 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005441 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005442 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005443 enum ieee80211_band band;
5444 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005445 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005446
5447 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5448 !rdev->ops->sched_scan_start)
5449 return -EOPNOTSUPP;
5450
5451 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5452 return -EINVAL;
5453
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005454 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5455 return -EINVAL;
5456
5457 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5458 if (interval == 0)
5459 return -EINVAL;
5460
Luciano Coelho807f8a82011-05-11 17:09:35 +03005461 wiphy = &rdev->wiphy;
5462
5463 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5464 n_channels = validate_scan_freqs(
5465 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5466 if (!n_channels)
5467 return -EINVAL;
5468 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005469 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005470 }
5471
5472 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5473 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5474 tmp)
5475 n_ssids++;
5476
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005477 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005478 return -EINVAL;
5479
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005480 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5481 nla_for_each_nested(attr,
5482 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5483 tmp)
5484 n_match_sets++;
5485
5486 if (n_match_sets > wiphy->max_match_sets)
5487 return -EINVAL;
5488
Luciano Coelho807f8a82011-05-11 17:09:35 +03005489 if (info->attrs[NL80211_ATTR_IE])
5490 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5491 else
5492 ie_len = 0;
5493
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005494 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005495 return -EINVAL;
5496
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005497 if (rdev->sched_scan_req) {
5498 err = -EINPROGRESS;
5499 goto out;
5500 }
5501
Luciano Coelho807f8a82011-05-11 17:09:35 +03005502 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005503 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005504 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005505 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005506 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005507 if (!request) {
5508 err = -ENOMEM;
5509 goto out;
5510 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005511
5512 if (n_ssids)
5513 request->ssids = (void *)&request->channels[n_channels];
5514 request->n_ssids = n_ssids;
5515 if (ie_len) {
5516 if (request->ssids)
5517 request->ie = (void *)(request->ssids + n_ssids);
5518 else
5519 request->ie = (void *)(request->channels + n_channels);
5520 }
5521
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005522 if (n_match_sets) {
5523 if (request->ie)
5524 request->match_sets = (void *)(request->ie + ie_len);
5525 else if (request->ssids)
5526 request->match_sets =
5527 (void *)(request->ssids + n_ssids);
5528 else
5529 request->match_sets =
5530 (void *)(request->channels + n_channels);
5531 }
5532 request->n_match_sets = n_match_sets;
5533
Luciano Coelho807f8a82011-05-11 17:09:35 +03005534 i = 0;
5535 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5536 /* user specified, bail out if channel not found */
5537 nla_for_each_nested(attr,
5538 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5539 tmp) {
5540 struct ieee80211_channel *chan;
5541
5542 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5543
5544 if (!chan) {
5545 err = -EINVAL;
5546 goto out_free;
5547 }
5548
5549 /* ignore disabled channels */
5550 if (chan->flags & IEEE80211_CHAN_DISABLED)
5551 continue;
5552
5553 request->channels[i] = chan;
5554 i++;
5555 }
5556 } else {
5557 /* all channels */
5558 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5559 int j;
5560 if (!wiphy->bands[band])
5561 continue;
5562 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5563 struct ieee80211_channel *chan;
5564
5565 chan = &wiphy->bands[band]->channels[j];
5566
5567 if (chan->flags & IEEE80211_CHAN_DISABLED)
5568 continue;
5569
5570 request->channels[i] = chan;
5571 i++;
5572 }
5573 }
5574 }
5575
5576 if (!i) {
5577 err = -EINVAL;
5578 goto out_free;
5579 }
5580
5581 request->n_channels = i;
5582
5583 i = 0;
5584 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5585 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5586 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005587 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005588 err = -EINVAL;
5589 goto out_free;
5590 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005591 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005592 memcpy(request->ssids[i].ssid, nla_data(attr),
5593 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005594 i++;
5595 }
5596 }
5597
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005598 i = 0;
5599 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5600 nla_for_each_nested(attr,
5601 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5602 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005603 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005604
5605 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5606 nla_data(attr), nla_len(attr),
5607 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005608 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005609 if (ssid) {
5610 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5611 err = -EINVAL;
5612 goto out_free;
5613 }
5614 memcpy(request->match_sets[i].ssid.ssid,
5615 nla_data(ssid), nla_len(ssid));
5616 request->match_sets[i].ssid.ssid_len =
5617 nla_len(ssid);
5618 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005619 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5620 if (rssi)
5621 request->rssi_thold = nla_get_u32(rssi);
5622 else
5623 request->rssi_thold =
5624 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005625 i++;
5626 }
5627 }
5628
Luciano Coelho807f8a82011-05-11 17:09:35 +03005629 if (info->attrs[NL80211_ATTR_IE]) {
5630 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5631 memcpy((void *)request->ie,
5632 nla_data(info->attrs[NL80211_ATTR_IE]),
5633 request->ie_len);
5634 }
5635
Sam Leffler46856bb2012-10-11 21:03:32 -07005636 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005637 request->flags = nla_get_u32(
5638 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005639 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5640 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005641 err = -EOPNOTSUPP;
5642 goto out_free;
5643 }
5644 }
Sam Lefflered4737712012-10-11 21:03:31 -07005645
Luciano Coelho807f8a82011-05-11 17:09:35 +03005646 request->dev = dev;
5647 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005648 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005649 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005650
Hila Gonene35e4d22012-06-27 17:19:42 +03005651 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005652 if (!err) {
5653 rdev->sched_scan_req = request;
5654 nl80211_send_sched_scan(rdev, dev,
5655 NL80211_CMD_START_SCHED_SCAN);
5656 goto out;
5657 }
5658
5659out_free:
5660 kfree(request);
5661out:
5662 return err;
5663}
5664
5665static int nl80211_stop_sched_scan(struct sk_buff *skb,
5666 struct genl_info *info)
5667{
5668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5669
5670 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5671 !rdev->ops->sched_scan_stop)
5672 return -EOPNOTSUPP;
5673
Johannes Berg5fe231e2013-05-08 21:45:15 +02005674 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005675}
5676
Simon Wunderlich04f39042013-02-08 18:16:19 +01005677static int nl80211_start_radar_detection(struct sk_buff *skb,
5678 struct genl_info *info)
5679{
5680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5681 struct net_device *dev = info->user_ptr[1];
5682 struct wireless_dev *wdev = dev->ieee80211_ptr;
5683 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005684 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005685 int err;
5686
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005687 dfs_region = reg_get_dfs_region(wdev->wiphy);
5688 if (dfs_region == NL80211_DFS_UNSET)
5689 return -EINVAL;
5690
Simon Wunderlich04f39042013-02-08 18:16:19 +01005691 err = nl80211_parse_chandef(rdev, info, &chandef);
5692 if (err)
5693 return err;
5694
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005695 if (netif_carrier_ok(dev))
5696 return -EBUSY;
5697
Simon Wunderlich04f39042013-02-08 18:16:19 +01005698 if (wdev->cac_started)
5699 return -EBUSY;
5700
5701 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5702 if (err < 0)
5703 return err;
5704
5705 if (err == 0)
5706 return -EINVAL;
5707
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005708 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005709 return -EINVAL;
5710
5711 if (!rdev->ops->start_radar_detection)
5712 return -EOPNOTSUPP;
5713
Simon Wunderlich04f39042013-02-08 18:16:19 +01005714 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5715 chandef.chan, CHAN_MODE_SHARED,
5716 BIT(chandef.width));
5717 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005718 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005719
5720 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5721 if (!err) {
5722 wdev->channel = chandef.chan;
5723 wdev->cac_started = true;
5724 wdev->cac_start_time = jiffies;
5725 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005726 return err;
5727}
5728
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005729static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5730{
5731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5732 struct net_device *dev = info->user_ptr[1];
5733 struct wireless_dev *wdev = dev->ieee80211_ptr;
5734 struct cfg80211_csa_settings params;
5735 /* csa_attrs is defined static to avoid waste of stack size - this
5736 * function is called under RTNL lock, so this should not be a problem.
5737 */
5738 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5739 u8 radar_detect_width = 0;
5740 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005741 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005742
5743 if (!rdev->ops->channel_switch ||
5744 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5745 return -EOPNOTSUPP;
5746
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005747 switch (dev->ieee80211_ptr->iftype) {
5748 case NL80211_IFTYPE_AP:
5749 case NL80211_IFTYPE_P2P_GO:
5750 need_new_beacon = true;
5751
5752 /* useless if AP is not running */
5753 if (!wdev->beacon_interval)
5754 return -EINVAL;
5755 break;
5756 case NL80211_IFTYPE_ADHOC:
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005757 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005758 break;
5759 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005760 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005761 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005762
5763 memset(&params, 0, sizeof(params));
5764
5765 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5766 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5767 return -EINVAL;
5768
5769 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005770 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005771 return -EINVAL;
5772
5773 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5774
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005775 if (!need_new_beacon)
5776 goto skip_beacons;
5777
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005778 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5779 if (err)
5780 return err;
5781
5782 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5783 info->attrs[NL80211_ATTR_CSA_IES],
5784 nl80211_policy);
5785 if (err)
5786 return err;
5787
5788 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5789 if (err)
5790 return err;
5791
5792 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5793 return -EINVAL;
5794
5795 params.counter_offset_beacon =
5796 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5797 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5798 return -EINVAL;
5799
5800 /* sanity check - counters should be the same */
5801 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5802 params.count)
5803 return -EINVAL;
5804
5805 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5806 params.counter_offset_presp =
5807 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5808 if (params.counter_offset_presp >=
5809 params.beacon_csa.probe_resp_len)
5810 return -EINVAL;
5811
5812 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5813 params.count)
5814 return -EINVAL;
5815 }
5816
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005817skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005818 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5819 if (err)
5820 return err;
5821
5822 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5823 return -EINVAL;
5824
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005825 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005826 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5827 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005828 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5829 &params.chandef);
5830 if (err < 0) {
5831 return err;
5832 } else if (err) {
5833 radar_detect_width = BIT(params.chandef.width);
5834 params.radar_required = true;
5835 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005836 }
5837
5838 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5839 params.chandef.chan,
5840 CHAN_MODE_SHARED,
5841 radar_detect_width);
5842 if (err)
5843 return err;
5844
5845 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5846 params.block_tx = true;
5847
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005848 wdev_lock(wdev);
5849 err = rdev_channel_switch(rdev, dev, &params);
5850 wdev_unlock(wdev);
5851
5852 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005853}
5854
Johannes Berg9720bb32011-06-21 09:45:33 +02005855static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5856 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005857 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005858 struct wireless_dev *wdev,
5859 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005860{
Johannes Berg48ab9052009-07-10 18:42:31 +02005861 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005862 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005863 void *hdr;
5864 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005865 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005866
5867 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005868
Eric W. Biederman15e47302012-09-07 20:12:54 +00005869 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005870 NL80211_CMD_NEW_SCAN_RESULTS);
5871 if (!hdr)
5872 return -1;
5873
Johannes Berg9720bb32011-06-21 09:45:33 +02005874 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5875
Johannes Berg97990a02013-04-19 01:02:55 +02005876 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5877 goto nla_put_failure;
5878 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005879 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5880 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005881 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5882 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005883
5884 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5885 if (!bss)
5886 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005887 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005888 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005889 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005890
5891 rcu_read_lock();
5892 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005893 if (ies) {
5894 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5895 goto fail_unlock_rcu;
5896 tsf = true;
5897 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5898 ies->len, ies->data))
5899 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005900 }
5901 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005902 if (ies) {
5903 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5904 goto fail_unlock_rcu;
5905 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5906 ies->len, ies->data))
5907 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005908 }
5909 rcu_read_unlock();
5910
David S. Miller9360ffd2012-03-29 04:41:26 -04005911 if (res->beacon_interval &&
5912 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5913 goto nla_put_failure;
5914 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5915 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02005916 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005917 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5918 jiffies_to_msecs(jiffies - intbss->ts)))
5919 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005920
Johannes Berg77965c92009-02-18 18:45:06 +01005921 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005922 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005923 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5924 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005925 break;
5926 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005927 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5928 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005929 break;
5930 default:
5931 break;
5932 }
5933
Johannes Berg48ab9052009-07-10 18:42:31 +02005934 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005935 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005936 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005937 if (intbss == wdev->current_bss &&
5938 nla_put_u32(msg, NL80211_BSS_STATUS,
5939 NL80211_BSS_STATUS_ASSOCIATED))
5940 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005941 break;
5942 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005943 if (intbss == wdev->current_bss &&
5944 nla_put_u32(msg, NL80211_BSS_STATUS,
5945 NL80211_BSS_STATUS_IBSS_JOINED))
5946 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005947 break;
5948 default:
5949 break;
5950 }
5951
Johannes Berg2a519312009-02-10 21:25:55 +01005952 nla_nest_end(msg, bss);
5953
5954 return genlmsg_end(msg, hdr);
5955
Johannes Berg8cef2c92013-02-05 16:54:31 +01005956 fail_unlock_rcu:
5957 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005958 nla_put_failure:
5959 genlmsg_cancel(msg, hdr);
5960 return -EMSGSIZE;
5961}
5962
Johannes Berg97990a02013-04-19 01:02:55 +02005963static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005964{
Johannes Berg48ab9052009-07-10 18:42:31 +02005965 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005966 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005967 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005968 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005969 int err;
5970
Johannes Berg97990a02013-04-19 01:02:55 +02005971 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005972 if (err)
5973 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005974
Johannes Berg48ab9052009-07-10 18:42:31 +02005975 wdev_lock(wdev);
5976 spin_lock_bh(&rdev->bss_lock);
5977 cfg80211_bss_expire(rdev);
5978
Johannes Berg9720bb32011-06-21 09:45:33 +02005979 cb->seq = rdev->bss_generation;
5980
Johannes Berg48ab9052009-07-10 18:42:31 +02005981 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005982 if (++idx <= start)
5983 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005984 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005985 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005986 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005987 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005988 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005989 }
5990 }
5991
Johannes Berg48ab9052009-07-10 18:42:31 +02005992 spin_unlock_bh(&rdev->bss_lock);
5993 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005994
Johannes Berg97990a02013-04-19 01:02:55 +02005995 cb->args[2] = idx;
5996 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005997
Johannes Berg67748892010-10-04 21:14:06 +02005998 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005999}
6000
Eric W. Biederman15e47302012-09-07 20:12:54 +00006001static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006002 int flags, struct net_device *dev,
6003 struct survey_info *survey)
6004{
6005 void *hdr;
6006 struct nlattr *infoattr;
6007
Eric W. Biederman15e47302012-09-07 20:12:54 +00006008 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006009 NL80211_CMD_NEW_SURVEY_RESULTS);
6010 if (!hdr)
6011 return -ENOMEM;
6012
David S. Miller9360ffd2012-03-29 04:41:26 -04006013 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6014 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006015
6016 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6017 if (!infoattr)
6018 goto nla_put_failure;
6019
David S. Miller9360ffd2012-03-29 04:41:26 -04006020 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6021 survey->channel->center_freq))
6022 goto nla_put_failure;
6023
6024 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6025 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6026 goto nla_put_failure;
6027 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6028 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6029 goto nla_put_failure;
6030 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6031 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6032 survey->channel_time))
6033 goto nla_put_failure;
6034 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6035 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6036 survey->channel_time_busy))
6037 goto nla_put_failure;
6038 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6039 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6040 survey->channel_time_ext_busy))
6041 goto nla_put_failure;
6042 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6043 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6044 survey->channel_time_rx))
6045 goto nla_put_failure;
6046 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6047 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6048 survey->channel_time_tx))
6049 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006050
6051 nla_nest_end(msg, infoattr);
6052
6053 return genlmsg_end(msg, hdr);
6054
6055 nla_put_failure:
6056 genlmsg_cancel(msg, hdr);
6057 return -EMSGSIZE;
6058}
6059
6060static int nl80211_dump_survey(struct sk_buff *skb,
6061 struct netlink_callback *cb)
6062{
6063 struct survey_info survey;
6064 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006065 struct wireless_dev *wdev;
6066 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006067 int res;
6068
Johannes Berg97990a02013-04-19 01:02:55 +02006069 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006070 if (res)
6071 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006072
Johannes Berg97990a02013-04-19 01:02:55 +02006073 if (!wdev->netdev) {
6074 res = -EINVAL;
6075 goto out_err;
6076 }
6077
Holger Schurig61fa7132009-11-11 12:25:40 +01006078 if (!dev->ops->dump_survey) {
6079 res = -EOPNOTSUPP;
6080 goto out_err;
6081 }
6082
6083 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006084 struct ieee80211_channel *chan;
6085
Johannes Berg97990a02013-04-19 01:02:55 +02006086 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006087 if (res == -ENOENT)
6088 break;
6089 if (res)
6090 goto out_err;
6091
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006092 /* Survey without a channel doesn't make sense */
6093 if (!survey.channel) {
6094 res = -EINVAL;
6095 goto out;
6096 }
6097
6098 chan = ieee80211_get_channel(&dev->wiphy,
6099 survey.channel->center_freq);
6100 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6101 survey_idx++;
6102 continue;
6103 }
6104
Holger Schurig61fa7132009-11-11 12:25:40 +01006105 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006106 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006107 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006108 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006109 goto out;
6110 survey_idx++;
6111 }
6112
6113 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006114 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006115 res = skb->len;
6116 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006117 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006118 return res;
6119}
6120
Samuel Ortizb23aa672009-07-01 21:26:54 +02006121static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6122{
6123 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6124 NL80211_WPA_VERSION_2));
6125}
6126
Jouni Malinen636a5d32009-03-19 13:39:22 +02006127static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6128{
Johannes Berg4c476992010-10-04 21:36:35 +02006129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6130 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006131 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006132 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6133 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006134 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006135 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006136 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006137
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006138 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6139 return -EINVAL;
6140
6141 if (!info->attrs[NL80211_ATTR_MAC])
6142 return -EINVAL;
6143
Jouni Malinen17780922009-03-27 20:52:47 +02006144 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6145 return -EINVAL;
6146
Johannes Berg19957bb2009-07-02 17:20:43 +02006147 if (!info->attrs[NL80211_ATTR_SSID])
6148 return -EINVAL;
6149
6150 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6151 return -EINVAL;
6152
Johannes Bergfffd0932009-07-08 14:22:54 +02006153 err = nl80211_parse_key(info, &key);
6154 if (err)
6155 return err;
6156
6157 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006158 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6159 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006160 if (!key.p.key || !key.p.key_len)
6161 return -EINVAL;
6162 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6163 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6164 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6165 key.p.key_len != WLAN_KEY_LEN_WEP104))
6166 return -EINVAL;
6167 if (key.idx > 4)
6168 return -EINVAL;
6169 } else {
6170 key.p.key_len = 0;
6171 key.p.key = NULL;
6172 }
6173
Johannes Bergafea0b72010-08-10 09:46:42 +02006174 if (key.idx >= 0) {
6175 int i;
6176 bool ok = false;
6177 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6178 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6179 ok = true;
6180 break;
6181 }
6182 }
Johannes Berg4c476992010-10-04 21:36:35 +02006183 if (!ok)
6184 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006185 }
6186
Johannes Berg4c476992010-10-04 21:36:35 +02006187 if (!rdev->ops->auth)
6188 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006189
Johannes Berg074ac8d2010-09-16 14:58:22 +02006190 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006191 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6192 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006193
Johannes Berg19957bb2009-07-02 17:20:43 +02006194 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02006195 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02006196 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006197 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6198 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006199
Johannes Berg19957bb2009-07-02 17:20:43 +02006200 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6201 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6202
6203 if (info->attrs[NL80211_ATTR_IE]) {
6204 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6205 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6206 }
6207
6208 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006209 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006210 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006211
Jouni Malinene39e5b52012-09-30 19:29:39 +03006212 if (auth_type == NL80211_AUTHTYPE_SAE &&
6213 !info->attrs[NL80211_ATTR_SAE_DATA])
6214 return -EINVAL;
6215
6216 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6217 if (auth_type != NL80211_AUTHTYPE_SAE)
6218 return -EINVAL;
6219 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6220 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6221 /* need to include at least Auth Transaction and Status Code */
6222 if (sae_data_len < 4)
6223 return -EINVAL;
6224 }
6225
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006226 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6227
Johannes Berg95de8172012-01-20 13:55:25 +01006228 /*
6229 * Since we no longer track auth state, ignore
6230 * requests to only change local state.
6231 */
6232 if (local_state_change)
6233 return 0;
6234
Johannes Berg91bf9b22013-05-15 17:44:01 +02006235 wdev_lock(dev->ieee80211_ptr);
6236 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6237 ssid, ssid_len, ie, ie_len,
6238 key.p.key, key.p.key_len, key.idx,
6239 sae_data, sae_data_len);
6240 wdev_unlock(dev->ieee80211_ptr);
6241 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006242}
6243
Johannes Bergc0692b82010-08-27 14:26:53 +03006244static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6245 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006246 struct cfg80211_crypto_settings *settings,
6247 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006248{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006249 memset(settings, 0, sizeof(*settings));
6250
Samuel Ortizb23aa672009-07-01 21:26:54 +02006251 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6252
Johannes Bergc0692b82010-08-27 14:26:53 +03006253 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6254 u16 proto;
6255 proto = nla_get_u16(
6256 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6257 settings->control_port_ethertype = cpu_to_be16(proto);
6258 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6259 proto != ETH_P_PAE)
6260 return -EINVAL;
6261 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6262 settings->control_port_no_encrypt = true;
6263 } else
6264 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6265
Samuel Ortizb23aa672009-07-01 21:26:54 +02006266 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6267 void *data;
6268 int len, i;
6269
6270 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6271 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6272 settings->n_ciphers_pairwise = len / sizeof(u32);
6273
6274 if (len % sizeof(u32))
6275 return -EINVAL;
6276
Johannes Berg3dc27d22009-07-02 21:36:37 +02006277 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006278 return -EINVAL;
6279
6280 memcpy(settings->ciphers_pairwise, data, len);
6281
6282 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006283 if (!cfg80211_supported_cipher_suite(
6284 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006285 settings->ciphers_pairwise[i]))
6286 return -EINVAL;
6287 }
6288
6289 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6290 settings->cipher_group =
6291 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006292 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6293 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006294 return -EINVAL;
6295 }
6296
6297 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6298 settings->wpa_versions =
6299 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6300 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6301 return -EINVAL;
6302 }
6303
6304 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6305 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006306 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006307
6308 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6309 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6310 settings->n_akm_suites = len / sizeof(u32);
6311
6312 if (len % sizeof(u32))
6313 return -EINVAL;
6314
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006315 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6316 return -EINVAL;
6317
Samuel Ortizb23aa672009-07-01 21:26:54 +02006318 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006319 }
6320
6321 return 0;
6322}
6323
Jouni Malinen636a5d32009-03-19 13:39:22 +02006324static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6325{
Johannes Berg4c476992010-10-04 21:36:35 +02006326 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6327 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006328 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006329 struct cfg80211_assoc_request req = {};
6330 const u8 *bssid, *ssid;
6331 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006332
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006333 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6334 return -EINVAL;
6335
6336 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006337 !info->attrs[NL80211_ATTR_SSID] ||
6338 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006339 return -EINVAL;
6340
Johannes Berg4c476992010-10-04 21:36:35 +02006341 if (!rdev->ops->assoc)
6342 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006343
Johannes Berg074ac8d2010-09-16 14:58:22 +02006344 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006345 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6346 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006347
Johannes Berg19957bb2009-07-02 17:20:43 +02006348 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006349
Johannes Berg19957bb2009-07-02 17:20:43 +02006350 chan = ieee80211_get_channel(&rdev->wiphy,
6351 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006352 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6353 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006354
Johannes Berg19957bb2009-07-02 17:20:43 +02006355 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6356 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006357
6358 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006359 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6360 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006361 }
6362
Jouni Malinendc6382c2009-05-06 22:09:37 +03006363 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006364 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006365 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006366 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006367 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006368 else if (mfp != NL80211_MFP_NO)
6369 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006370 }
6371
Johannes Berg3e5d7642009-07-07 14:37:26 +02006372 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006373 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006374
Ben Greear7e7c8922011-11-18 11:31:59 -08006375 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006376 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006377
6378 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006379 memcpy(&req.ht_capa_mask,
6380 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6381 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006382
6383 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006384 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006385 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006386 memcpy(&req.ht_capa,
6387 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6388 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006389 }
6390
Johannes Bergee2aca32013-02-21 17:36:01 +01006391 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006392 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006393
6394 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006395 memcpy(&req.vht_capa_mask,
6396 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6397 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006398
6399 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006400 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006401 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006402 memcpy(&req.vht_capa,
6403 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6404 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006405 }
6406
Johannes Bergf62fab72013-02-21 20:09:09 +01006407 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006408 if (!err) {
6409 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006410 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6411 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006412 wdev_unlock(dev->ieee80211_ptr);
6413 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006414
Jouni Malinen636a5d32009-03-19 13:39:22 +02006415 return err;
6416}
6417
6418static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6419{
Johannes Berg4c476992010-10-04 21:36:35 +02006420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6421 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006422 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006423 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006424 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006425 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006426
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006427 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6428 return -EINVAL;
6429
6430 if (!info->attrs[NL80211_ATTR_MAC])
6431 return -EINVAL;
6432
6433 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6434 return -EINVAL;
6435
Johannes Berg4c476992010-10-04 21:36:35 +02006436 if (!rdev->ops->deauth)
6437 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006438
Johannes Berg074ac8d2010-09-16 14:58:22 +02006439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006440 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6441 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006442
Johannes Berg19957bb2009-07-02 17:20:43 +02006443 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006444
Johannes Berg19957bb2009-07-02 17:20:43 +02006445 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6446 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006447 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006448 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006449 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006450
6451 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006452 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6453 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006454 }
6455
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006456 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6457
Johannes Berg91bf9b22013-05-15 17:44:01 +02006458 wdev_lock(dev->ieee80211_ptr);
6459 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6460 local_state_change);
6461 wdev_unlock(dev->ieee80211_ptr);
6462 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006463}
6464
6465static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6466{
Johannes Berg4c476992010-10-04 21:36:35 +02006467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6468 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006469 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006470 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006471 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006472 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006473
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006474 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6475 return -EINVAL;
6476
6477 if (!info->attrs[NL80211_ATTR_MAC])
6478 return -EINVAL;
6479
6480 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6481 return -EINVAL;
6482
Johannes Berg4c476992010-10-04 21:36:35 +02006483 if (!rdev->ops->disassoc)
6484 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006485
Johannes Berg074ac8d2010-09-16 14:58:22 +02006486 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006487 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6488 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006489
Johannes Berg19957bb2009-07-02 17:20:43 +02006490 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006491
Johannes Berg19957bb2009-07-02 17:20:43 +02006492 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6493 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006494 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006495 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006496 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006497
6498 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006499 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6500 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006501 }
6502
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006503 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6504
Johannes Berg91bf9b22013-05-15 17:44:01 +02006505 wdev_lock(dev->ieee80211_ptr);
6506 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6507 local_state_change);
6508 wdev_unlock(dev->ieee80211_ptr);
6509 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006510}
6511
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006512static bool
6513nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6514 int mcast_rate[IEEE80211_NUM_BANDS],
6515 int rateval)
6516{
6517 struct wiphy *wiphy = &rdev->wiphy;
6518 bool found = false;
6519 int band, i;
6520
6521 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6522 struct ieee80211_supported_band *sband;
6523
6524 sband = wiphy->bands[band];
6525 if (!sband)
6526 continue;
6527
6528 for (i = 0; i < sband->n_bitrates; i++) {
6529 if (sband->bitrates[i].bitrate == rateval) {
6530 mcast_rate[band] = i + 1;
6531 found = true;
6532 break;
6533 }
6534 }
6535 }
6536
6537 return found;
6538}
6539
Johannes Berg04a773a2009-04-19 21:24:32 +02006540static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6541{
Johannes Berg4c476992010-10-04 21:36:35 +02006542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6543 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006544 struct cfg80211_ibss_params ibss;
6545 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006546 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006547 int err;
6548
Johannes Berg8e30bc52009-04-22 17:45:38 +02006549 memset(&ibss, 0, sizeof(ibss));
6550
Johannes Berg04a773a2009-04-19 21:24:32 +02006551 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6552 return -EINVAL;
6553
Johannes Berg683b6d32012-11-08 21:25:48 +01006554 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006555 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6556 return -EINVAL;
6557
Johannes Berg8e30bc52009-04-22 17:45:38 +02006558 ibss.beacon_interval = 100;
6559
6560 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6561 ibss.beacon_interval =
6562 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6563 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6564 return -EINVAL;
6565 }
6566
Johannes Berg4c476992010-10-04 21:36:35 +02006567 if (!rdev->ops->join_ibss)
6568 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006569
Johannes Berg4c476992010-10-04 21:36:35 +02006570 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6571 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006572
Johannes Berg79c97e92009-07-07 03:56:12 +02006573 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006574
Johannes Berg39193492011-09-16 13:45:25 +02006575 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006576 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006577
6578 if (!is_valid_ether_addr(ibss.bssid))
6579 return -EINVAL;
6580 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006581 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6582 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6583
6584 if (info->attrs[NL80211_ATTR_IE]) {
6585 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6586 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6587 }
6588
Johannes Berg683b6d32012-11-08 21:25:48 +01006589 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6590 if (err)
6591 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006592
Johannes Berg683b6d32012-11-08 21:25:48 +01006593 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006594 return -EINVAL;
6595
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006596 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006597 case NL80211_CHAN_WIDTH_5:
6598 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006599 case NL80211_CHAN_WIDTH_20_NOHT:
6600 break;
6601 case NL80211_CHAN_WIDTH_20:
6602 case NL80211_CHAN_WIDTH_40:
6603 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6604 break;
6605 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006606 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006607 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006608
Johannes Berg04a773a2009-04-19 21:24:32 +02006609 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006610 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006611
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006612 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6613 u8 *rates =
6614 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6615 int n_rates =
6616 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6617 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006618 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006619
Johannes Berg34850ab2011-07-18 18:08:35 +02006620 err = ieee80211_get_ratemask(sband, rates, n_rates,
6621 &ibss.basic_rates);
6622 if (err)
6623 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006624 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006625
Simon Wunderlich803768f2013-06-28 10:39:58 +02006626 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6627 memcpy(&ibss.ht_capa_mask,
6628 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6629 sizeof(ibss.ht_capa_mask));
6630
6631 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6632 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6633 return -EINVAL;
6634 memcpy(&ibss.ht_capa,
6635 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6636 sizeof(ibss.ht_capa));
6637 }
6638
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006639 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6640 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6641 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6642 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006643
Johannes Berg4c476992010-10-04 21:36:35 +02006644 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306645 bool no_ht = false;
6646
Johannes Berg4c476992010-10-04 21:36:35 +02006647 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306648 info->attrs[NL80211_ATTR_KEYS],
6649 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006650 if (IS_ERR(connkeys))
6651 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306652
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006653 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6654 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306655 kfree(connkeys);
6656 return -EINVAL;
6657 }
Johannes Berg4c476992010-10-04 21:36:35 +02006658 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006659
Antonio Quartulli267335d2012-01-31 20:25:47 +01006660 ibss.control_port =
6661 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6662
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006663 ibss.userspace_handles_dfs =
6664 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6665
Johannes Berg4c476992010-10-04 21:36:35 +02006666 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006667 if (err)
6668 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006669 return err;
6670}
6671
6672static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6673{
Johannes Berg4c476992010-10-04 21:36:35 +02006674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6675 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006676
Johannes Berg4c476992010-10-04 21:36:35 +02006677 if (!rdev->ops->leave_ibss)
6678 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006679
Johannes Berg4c476992010-10-04 21:36:35 +02006680 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6681 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006682
Johannes Berg4c476992010-10-04 21:36:35 +02006683 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006684}
6685
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006686static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6687{
6688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6689 struct net_device *dev = info->user_ptr[1];
6690 int mcast_rate[IEEE80211_NUM_BANDS];
6691 u32 nla_rate;
6692 int err;
6693
6694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6696 return -EOPNOTSUPP;
6697
6698 if (!rdev->ops->set_mcast_rate)
6699 return -EOPNOTSUPP;
6700
6701 memset(mcast_rate, 0, sizeof(mcast_rate));
6702
6703 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6704 return -EINVAL;
6705
6706 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6707 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6708 return -EINVAL;
6709
6710 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6711
6712 return err;
6713}
6714
Johannes Bergad7e7182013-11-13 13:37:47 +01006715static struct sk_buff *
6716__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6717 int approxlen, u32 portid, u32 seq,
6718 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006719 enum nl80211_attrs attr,
6720 const struct nl80211_vendor_cmd_info *info,
6721 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006722{
6723 struct sk_buff *skb;
6724 void *hdr;
6725 struct nlattr *data;
6726
6727 skb = nlmsg_new(approxlen + 100, gfp);
6728 if (!skb)
6729 return NULL;
6730
6731 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6732 if (!hdr) {
6733 kfree_skb(skb);
6734 return NULL;
6735 }
6736
6737 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6738 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006739
6740 if (info) {
6741 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6742 info->vendor_id))
6743 goto nla_put_failure;
6744 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6745 info->subcmd))
6746 goto nla_put_failure;
6747 }
6748
Johannes Bergad7e7182013-11-13 13:37:47 +01006749 data = nla_nest_start(skb, attr);
6750
6751 ((void **)skb->cb)[0] = rdev;
6752 ((void **)skb->cb)[1] = hdr;
6753 ((void **)skb->cb)[2] = data;
6754
6755 return skb;
6756
6757 nla_put_failure:
6758 kfree_skb(skb);
6759 return NULL;
6760}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006761
Johannes Berge03ad6e2014-01-01 17:22:30 +01006762struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6763 enum nl80211_commands cmd,
6764 enum nl80211_attrs attr,
6765 int vendor_event_idx,
6766 int approxlen, gfp_t gfp)
6767{
6768 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6769 const struct nl80211_vendor_cmd_info *info;
6770
6771 switch (cmd) {
6772 case NL80211_CMD_TESTMODE:
6773 if (WARN_ON(vendor_event_idx != -1))
6774 return NULL;
6775 info = NULL;
6776 break;
6777 case NL80211_CMD_VENDOR:
6778 if (WARN_ON(vendor_event_idx < 0 ||
6779 vendor_event_idx >= wiphy->n_vendor_events))
6780 return NULL;
6781 info = &wiphy->vendor_events[vendor_event_idx];
6782 break;
6783 default:
6784 WARN_ON(1);
6785 return NULL;
6786 }
6787
6788 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6789 cmd, attr, info, gfp);
6790}
6791EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6792
6793void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6794{
6795 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6796 void *hdr = ((void **)skb->cb)[1];
6797 struct nlattr *data = ((void **)skb->cb)[2];
6798 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6799
6800 nla_nest_end(skb, data);
6801 genlmsg_end(skb, hdr);
6802
6803 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6804 mcgrp = NL80211_MCGRP_VENDOR;
6805
6806 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6807 mcgrp, gfp);
6808}
6809EXPORT_SYMBOL(__cfg80211_send_event_skb);
6810
Johannes Bergaff89a92009-07-01 21:26:51 +02006811#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006812static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6813{
Johannes Berg4c476992010-10-04 21:36:35 +02006814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006815 struct wireless_dev *wdev =
6816 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006817 int err;
6818
David Spinadelfc73f112013-07-31 18:04:15 +03006819 if (!rdev->ops->testmode_cmd)
6820 return -EOPNOTSUPP;
6821
6822 if (IS_ERR(wdev)) {
6823 err = PTR_ERR(wdev);
6824 if (err != -EINVAL)
6825 return err;
6826 wdev = NULL;
6827 } else if (wdev->wiphy != &rdev->wiphy) {
6828 return -EINVAL;
6829 }
6830
Johannes Bergaff89a92009-07-01 21:26:51 +02006831 if (!info->attrs[NL80211_ATTR_TESTDATA])
6832 return -EINVAL;
6833
Johannes Bergad7e7182013-11-13 13:37:47 +01006834 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006835 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006836 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6837 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006838 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006839
Johannes Bergaff89a92009-07-01 21:26:51 +02006840 return err;
6841}
6842
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006843static int nl80211_testmode_dump(struct sk_buff *skb,
6844 struct netlink_callback *cb)
6845{
Johannes Berg00918d32011-12-13 17:22:05 +01006846 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006847 int err;
6848 long phy_idx;
6849 void *data = NULL;
6850 int data_len = 0;
6851
Johannes Berg5fe231e2013-05-08 21:45:15 +02006852 rtnl_lock();
6853
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006854 if (cb->args[0]) {
6855 /*
6856 * 0 is a valid index, but not valid for args[0],
6857 * so we need to offset by 1.
6858 */
6859 phy_idx = cb->args[0] - 1;
6860 } else {
6861 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6862 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6863 nl80211_policy);
6864 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006865 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006866
Johannes Berg2bd7e352012-06-15 14:23:16 +02006867 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6868 nl80211_fam.attrbuf);
6869 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006870 err = PTR_ERR(rdev);
6871 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006872 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006873 phy_idx = rdev->wiphy_idx;
6874 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006875
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006876 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6877 cb->args[1] =
6878 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6879 }
6880
6881 if (cb->args[1]) {
6882 data = nla_data((void *)cb->args[1]);
6883 data_len = nla_len((void *)cb->args[1]);
6884 }
6885
Johannes Berg00918d32011-12-13 17:22:05 +01006886 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6887 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006888 err = -ENOENT;
6889 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006890 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006891
Johannes Berg00918d32011-12-13 17:22:05 +01006892 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006893 err = -EOPNOTSUPP;
6894 goto out_err;
6895 }
6896
6897 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006898 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006899 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6900 NL80211_CMD_TESTMODE);
6901 struct nlattr *tmdata;
6902
Dan Carpentercb35fba2013-08-14 14:50:01 +03006903 if (!hdr)
6904 break;
6905
David S. Miller9360ffd2012-03-29 04:41:26 -04006906 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006907 genlmsg_cancel(skb, hdr);
6908 break;
6909 }
6910
6911 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6912 if (!tmdata) {
6913 genlmsg_cancel(skb, hdr);
6914 break;
6915 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006916 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006917 nla_nest_end(skb, tmdata);
6918
6919 if (err == -ENOBUFS || err == -ENOENT) {
6920 genlmsg_cancel(skb, hdr);
6921 break;
6922 } else if (err) {
6923 genlmsg_cancel(skb, hdr);
6924 goto out_err;
6925 }
6926
6927 genlmsg_end(skb, hdr);
6928 }
6929
6930 err = skb->len;
6931 /* see above */
6932 cb->args[0] = phy_idx + 1;
6933 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02006934 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006935 return err;
6936}
Johannes Bergaff89a92009-07-01 21:26:51 +02006937#endif
6938
Samuel Ortizb23aa672009-07-01 21:26:54 +02006939static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6940{
Johannes Berg4c476992010-10-04 21:36:35 +02006941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6942 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006943 struct cfg80211_connect_params connect;
6944 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006945 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006946 int err;
6947
6948 memset(&connect, 0, sizeof(connect));
6949
6950 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6951 return -EINVAL;
6952
6953 if (!info->attrs[NL80211_ATTR_SSID] ||
6954 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6955 return -EINVAL;
6956
6957 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6958 connect.auth_type =
6959 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006960 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6961 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006962 return -EINVAL;
6963 } else
6964 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6965
6966 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6967
Johannes Bergc0692b82010-08-27 14:26:53 +03006968 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006969 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006970 if (err)
6971 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006972
Johannes Berg074ac8d2010-09-16 14:58:22 +02006973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6975 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006976
Johannes Berg79c97e92009-07-07 03:56:12 +02006977 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006978
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306979 connect.bg_scan_period = -1;
6980 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6981 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6982 connect.bg_scan_period =
6983 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6984 }
6985
Samuel Ortizb23aa672009-07-01 21:26:54 +02006986 if (info->attrs[NL80211_ATTR_MAC])
6987 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6988 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6989 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6990
6991 if (info->attrs[NL80211_ATTR_IE]) {
6992 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6993 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6994 }
6995
Jouni Malinencee00a92013-01-15 17:15:57 +02006996 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6997 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6998 if (connect.mfp != NL80211_MFP_REQUIRED &&
6999 connect.mfp != NL80211_MFP_NO)
7000 return -EINVAL;
7001 } else {
7002 connect.mfp = NL80211_MFP_NO;
7003 }
7004
Samuel Ortizb23aa672009-07-01 21:26:54 +02007005 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7006 connect.channel =
7007 ieee80211_get_channel(wiphy,
7008 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
7009 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02007010 connect.channel->flags & IEEE80211_CHAN_DISABLED)
7011 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007012 }
7013
Johannes Bergfffd0932009-07-08 14:22:54 +02007014 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7015 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307016 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007017 if (IS_ERR(connkeys))
7018 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007019 }
7020
Ben Greear7e7c8922011-11-18 11:31:59 -08007021 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7022 connect.flags |= ASSOC_REQ_DISABLE_HT;
7023
7024 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7025 memcpy(&connect.ht_capa_mask,
7026 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7027 sizeof(connect.ht_capa_mask));
7028
7029 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007030 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7031 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007032 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007033 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007034 memcpy(&connect.ht_capa,
7035 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7036 sizeof(connect.ht_capa));
7037 }
7038
Johannes Bergee2aca32013-02-21 17:36:01 +01007039 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7040 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7041
7042 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7043 memcpy(&connect.vht_capa_mask,
7044 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7045 sizeof(connect.vht_capa_mask));
7046
7047 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7048 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7049 kfree(connkeys);
7050 return -EINVAL;
7051 }
7052 memcpy(&connect.vht_capa,
7053 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7054 sizeof(connect.vht_capa));
7055 }
7056
Johannes Berg83739b02013-05-15 17:44:01 +02007057 wdev_lock(dev->ieee80211_ptr);
7058 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7059 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007060 if (err)
7061 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007062 return err;
7063}
7064
7065static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7066{
Johannes Berg4c476992010-10-04 21:36:35 +02007067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7068 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007069 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007070 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007071
7072 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7073 reason = WLAN_REASON_DEAUTH_LEAVING;
7074 else
7075 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7076
7077 if (reason == 0)
7078 return -EINVAL;
7079
Johannes Berg074ac8d2010-09-16 14:58:22 +02007080 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007081 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7082 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007083
Johannes Berg83739b02013-05-15 17:44:01 +02007084 wdev_lock(dev->ieee80211_ptr);
7085 ret = cfg80211_disconnect(rdev, dev, reason, true);
7086 wdev_unlock(dev->ieee80211_ptr);
7087 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007088}
7089
Johannes Berg463d0182009-07-14 00:33:35 +02007090static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7091{
Johannes Berg4c476992010-10-04 21:36:35 +02007092 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007093 struct net *net;
7094 int err;
7095 u32 pid;
7096
7097 if (!info->attrs[NL80211_ATTR_PID])
7098 return -EINVAL;
7099
7100 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7101
Johannes Berg463d0182009-07-14 00:33:35 +02007102 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007103 if (IS_ERR(net))
7104 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007105
7106 err = 0;
7107
7108 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007109 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7110 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007111
Johannes Berg463d0182009-07-14 00:33:35 +02007112 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007113 return err;
7114}
7115
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007116static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7117{
Johannes Berg4c476992010-10-04 21:36:35 +02007118 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007119 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7120 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007121 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007122 struct cfg80211_pmksa pmksa;
7123
7124 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7125
7126 if (!info->attrs[NL80211_ATTR_MAC])
7127 return -EINVAL;
7128
7129 if (!info->attrs[NL80211_ATTR_PMKID])
7130 return -EINVAL;
7131
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007132 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7133 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7134
Johannes Berg074ac8d2010-09-16 14:58:22 +02007135 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007136 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7137 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007138
7139 switch (info->genlhdr->cmd) {
7140 case NL80211_CMD_SET_PMKSA:
7141 rdev_ops = rdev->ops->set_pmksa;
7142 break;
7143 case NL80211_CMD_DEL_PMKSA:
7144 rdev_ops = rdev->ops->del_pmksa;
7145 break;
7146 default:
7147 WARN_ON(1);
7148 break;
7149 }
7150
Johannes Berg4c476992010-10-04 21:36:35 +02007151 if (!rdev_ops)
7152 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007153
Johannes Berg4c476992010-10-04 21:36:35 +02007154 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007155}
7156
7157static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7158{
Johannes Berg4c476992010-10-04 21:36:35 +02007159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7160 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007161
Johannes Berg074ac8d2010-09-16 14:58:22 +02007162 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007163 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7164 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007165
Johannes Berg4c476992010-10-04 21:36:35 +02007166 if (!rdev->ops->flush_pmksa)
7167 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007168
Hila Gonene35e4d22012-06-27 17:19:42 +03007169 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007170}
7171
Arik Nemtsov109086c2011-09-28 14:12:50 +03007172static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7173{
7174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7175 struct net_device *dev = info->user_ptr[1];
7176 u8 action_code, dialog_token;
7177 u16 status_code;
7178 u8 *peer;
7179
7180 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7181 !rdev->ops->tdls_mgmt)
7182 return -EOPNOTSUPP;
7183
7184 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7185 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7186 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7187 !info->attrs[NL80211_ATTR_IE] ||
7188 !info->attrs[NL80211_ATTR_MAC])
7189 return -EINVAL;
7190
7191 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7192 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7193 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7194 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
7195
Hila Gonene35e4d22012-06-27 17:19:42 +03007196 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
7197 dialog_token, status_code,
7198 nla_data(info->attrs[NL80211_ATTR_IE]),
7199 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007200}
7201
7202static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7203{
7204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7205 struct net_device *dev = info->user_ptr[1];
7206 enum nl80211_tdls_operation operation;
7207 u8 *peer;
7208
7209 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7210 !rdev->ops->tdls_oper)
7211 return -EOPNOTSUPP;
7212
7213 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7214 !info->attrs[NL80211_ATTR_MAC])
7215 return -EINVAL;
7216
7217 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7218 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7219
Hila Gonene35e4d22012-06-27 17:19:42 +03007220 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007221}
7222
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007223static int nl80211_remain_on_channel(struct sk_buff *skb,
7224 struct genl_info *info)
7225{
Johannes Berg4c476992010-10-04 21:36:35 +02007226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007227 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007228 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007229 struct sk_buff *msg;
7230 void *hdr;
7231 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007232 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007233 int err;
7234
7235 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7236 !info->attrs[NL80211_ATTR_DURATION])
7237 return -EINVAL;
7238
7239 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7240
Johannes Berg7c4ef712011-11-18 15:33:48 +01007241 if (!rdev->ops->remain_on_channel ||
7242 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007243 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007244
Johannes Bergebf348f2012-06-01 12:50:54 +02007245 /*
7246 * We should be on that channel for at least a minimum amount of
7247 * time (10ms) but no longer than the driver supports.
7248 */
7249 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7250 duration > rdev->wiphy.max_remain_on_channel_duration)
7251 return -EINVAL;
7252
Johannes Berg683b6d32012-11-08 21:25:48 +01007253 err = nl80211_parse_chandef(rdev, info, &chandef);
7254 if (err)
7255 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007256
7257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007258 if (!msg)
7259 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007260
Eric W. Biederman15e47302012-09-07 20:12:54 +00007261 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007262 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007263 if (!hdr) {
7264 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007265 goto free_msg;
7266 }
7267
Johannes Berg683b6d32012-11-08 21:25:48 +01007268 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7269 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007270
7271 if (err)
7272 goto free_msg;
7273
David S. Miller9360ffd2012-03-29 04:41:26 -04007274 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7275 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007276
7277 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007278
7279 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007280
7281 nla_put_failure:
7282 err = -ENOBUFS;
7283 free_msg:
7284 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007285 return err;
7286}
7287
7288static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7289 struct genl_info *info)
7290{
Johannes Berg4c476992010-10-04 21:36:35 +02007291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007292 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007293 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007294
7295 if (!info->attrs[NL80211_ATTR_COOKIE])
7296 return -EINVAL;
7297
Johannes Berg4c476992010-10-04 21:36:35 +02007298 if (!rdev->ops->cancel_remain_on_channel)
7299 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007300
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007301 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7302
Hila Gonene35e4d22012-06-27 17:19:42 +03007303 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007304}
7305
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007306static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7307 u8 *rates, u8 rates_len)
7308{
7309 u8 i;
7310 u32 mask = 0;
7311
7312 for (i = 0; i < rates_len; i++) {
7313 int rate = (rates[i] & 0x7f) * 5;
7314 int ridx;
7315 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7316 struct ieee80211_rate *srate =
7317 &sband->bitrates[ridx];
7318 if (rate == srate->bitrate) {
7319 mask |= 1 << ridx;
7320 break;
7321 }
7322 }
7323 if (ridx == sband->n_bitrates)
7324 return 0; /* rate not found */
7325 }
7326
7327 return mask;
7328}
7329
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007330static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7331 u8 *rates, u8 rates_len,
7332 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7333{
7334 u8 i;
7335
7336 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7337
7338 for (i = 0; i < rates_len; i++) {
7339 int ridx, rbit;
7340
7341 ridx = rates[i] / 8;
7342 rbit = BIT(rates[i] % 8);
7343
7344 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007345 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007346 return false;
7347
7348 /* check availability */
7349 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7350 mcs[ridx] |= rbit;
7351 else
7352 return false;
7353 }
7354
7355 return true;
7356}
7357
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007358static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7359{
7360 u16 mcs_mask = 0;
7361
7362 switch (vht_mcs_map) {
7363 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7364 break;
7365 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7366 mcs_mask = 0x00FF;
7367 break;
7368 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7369 mcs_mask = 0x01FF;
7370 break;
7371 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7372 mcs_mask = 0x03FF;
7373 break;
7374 default:
7375 break;
7376 }
7377
7378 return mcs_mask;
7379}
7380
7381static void vht_build_mcs_mask(u16 vht_mcs_map,
7382 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7383{
7384 u8 nss;
7385
7386 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7387 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7388 vht_mcs_map >>= 2;
7389 }
7390}
7391
7392static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7393 struct nl80211_txrate_vht *txrate,
7394 u16 mcs[NL80211_VHT_NSS_MAX])
7395{
7396 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7397 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7398 u8 i;
7399
7400 if (!sband->vht_cap.vht_supported)
7401 return false;
7402
7403 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7404
7405 /* Build vht_mcs_mask from VHT capabilities */
7406 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7407
7408 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7409 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7410 mcs[i] = txrate->mcs[i];
7411 else
7412 return false;
7413 }
7414
7415 return true;
7416}
7417
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007418static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007419 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7420 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007421 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7422 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007423 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007424};
7425
7426static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7427 struct genl_info *info)
7428{
7429 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007430 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007431 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007432 int rem, i;
7433 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007434 struct nlattr *tx_rates;
7435 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007436 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007437
Johannes Berg4c476992010-10-04 21:36:35 +02007438 if (!rdev->ops->set_bitrate_mask)
7439 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007440
7441 memset(&mask, 0, sizeof(mask));
7442 /* Default to all rates enabled */
7443 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7444 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007445
7446 if (!sband)
7447 continue;
7448
7449 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007450 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007451 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007452 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007453
7454 if (!sband->vht_cap.vht_supported)
7455 continue;
7456
7457 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7458 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007459 }
7460
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007461 /* if no rates are given set it back to the defaults */
7462 if (!info->attrs[NL80211_ATTR_TX_RATES])
7463 goto out;
7464
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007465 /*
7466 * The nested attribute uses enum nl80211_band as the index. This maps
7467 * directly to the enum ieee80211_band values used in cfg80211.
7468 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007469 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007470 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7471 {
7472 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007473 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7474 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007475 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007476 if (sband == NULL)
7477 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007478 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7479 nla_len(tx_rates), nl80211_txattr_policy);
7480 if (tb[NL80211_TXRATE_LEGACY]) {
7481 mask.control[band].legacy = rateset_to_mask(
7482 sband,
7483 nla_data(tb[NL80211_TXRATE_LEGACY]),
7484 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307485 if ((mask.control[band].legacy == 0) &&
7486 nla_len(tb[NL80211_TXRATE_LEGACY]))
7487 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007488 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007489 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007490 if (!ht_rateset_to_mask(
7491 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007492 nla_data(tb[NL80211_TXRATE_HT]),
7493 nla_len(tb[NL80211_TXRATE_HT]),
7494 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007495 return -EINVAL;
7496 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007497 if (tb[NL80211_TXRATE_VHT]) {
7498 if (!vht_set_mcs_mask(
7499 sband,
7500 nla_data(tb[NL80211_TXRATE_VHT]),
7501 mask.control[band].vht_mcs))
7502 return -EINVAL;
7503 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007504
7505 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007506 /* don't allow empty legacy rates if HT or VHT
7507 * are not even supported.
7508 */
7509 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7510 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007511 return -EINVAL;
7512
7513 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007514 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007515 goto out;
7516
7517 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7518 if (mask.control[band].vht_mcs[i])
7519 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007520
7521 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007522 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007523 }
7524 }
7525
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007526out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007527 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007528}
7529
Johannes Berg2e161f72010-08-12 15:38:38 +02007530static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007531{
Johannes Berg4c476992010-10-04 21:36:35 +02007532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007533 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007534 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007535
7536 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7537 return -EINVAL;
7538
Johannes Berg2e161f72010-08-12 15:38:38 +02007539 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7540 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007541
Johannes Berg71bbc992012-06-15 15:30:18 +02007542 switch (wdev->iftype) {
7543 case NL80211_IFTYPE_STATION:
7544 case NL80211_IFTYPE_ADHOC:
7545 case NL80211_IFTYPE_P2P_CLIENT:
7546 case NL80211_IFTYPE_AP:
7547 case NL80211_IFTYPE_AP_VLAN:
7548 case NL80211_IFTYPE_MESH_POINT:
7549 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007550 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007551 break;
7552 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007553 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007554 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007555
7556 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007557 if (!rdev->ops->mgmt_tx)
7558 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007559
Eric W. Biederman15e47302012-09-07 20:12:54 +00007560 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007561 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7562 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007563}
7564
Johannes Berg2e161f72010-08-12 15:38:38 +02007565static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007566{
Johannes Berg4c476992010-10-04 21:36:35 +02007567 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007568 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007569 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007570 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007571 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007572 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007573 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007574 struct cfg80211_mgmt_tx_params params = {
7575 .dont_wait_for_ack =
7576 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7577 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007578
Johannes Berg683b6d32012-11-08 21:25:48 +01007579 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007580 return -EINVAL;
7581
Johannes Berg4c476992010-10-04 21:36:35 +02007582 if (!rdev->ops->mgmt_tx)
7583 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007584
Johannes Berg71bbc992012-06-15 15:30:18 +02007585 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007586 case NL80211_IFTYPE_P2P_DEVICE:
7587 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7588 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007589 case NL80211_IFTYPE_STATION:
7590 case NL80211_IFTYPE_ADHOC:
7591 case NL80211_IFTYPE_P2P_CLIENT:
7592 case NL80211_IFTYPE_AP:
7593 case NL80211_IFTYPE_AP_VLAN:
7594 case NL80211_IFTYPE_MESH_POINT:
7595 case NL80211_IFTYPE_P2P_GO:
7596 break;
7597 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007598 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007599 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007600
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007601 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007602 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007603 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007604 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007605
7606 /*
7607 * We should wait on the channel for at least a minimum amount
7608 * of time (10ms) but no longer than the driver supports.
7609 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007610 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7611 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007612 return -EINVAL;
7613
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007614 }
7615
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007616 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007617
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007618 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007619 return -EINVAL;
7620
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007621 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307622
Antonio Quartulliea141b752013-06-11 14:20:03 +02007623 /* get the channel if any has been specified, otherwise pass NULL to
7624 * the driver. The latter will use the current one
7625 */
7626 chandef.chan = NULL;
7627 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7628 err = nl80211_parse_chandef(rdev, info, &chandef);
7629 if (err)
7630 return err;
7631 }
7632
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007633 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007634 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007635
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007636 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7638 if (!msg)
7639 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007640
Eric W. Biederman15e47302012-09-07 20:12:54 +00007641 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007642 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007643 if (!hdr) {
7644 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007645 goto free_msg;
7646 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007647 }
Johannes Berge247bd902011-11-04 11:18:21 +01007648
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007649 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7650 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7651 params.chan = chandef.chan;
7652 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007653 if (err)
7654 goto free_msg;
7655
Johannes Berge247bd902011-11-04 11:18:21 +01007656 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007657 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7658 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007659
Johannes Berge247bd902011-11-04 11:18:21 +01007660 genlmsg_end(msg, hdr);
7661 return genlmsg_reply(msg, info);
7662 }
7663
7664 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007665
7666 nla_put_failure:
7667 err = -ENOBUFS;
7668 free_msg:
7669 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007670 return err;
7671}
7672
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007673static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7674{
7675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007676 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007677 u64 cookie;
7678
7679 if (!info->attrs[NL80211_ATTR_COOKIE])
7680 return -EINVAL;
7681
7682 if (!rdev->ops->mgmt_tx_cancel_wait)
7683 return -EOPNOTSUPP;
7684
Johannes Berg71bbc992012-06-15 15:30:18 +02007685 switch (wdev->iftype) {
7686 case NL80211_IFTYPE_STATION:
7687 case NL80211_IFTYPE_ADHOC:
7688 case NL80211_IFTYPE_P2P_CLIENT:
7689 case NL80211_IFTYPE_AP:
7690 case NL80211_IFTYPE_AP_VLAN:
7691 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007692 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007693 break;
7694 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007695 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007696 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007697
7698 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7699
Hila Gonene35e4d22012-06-27 17:19:42 +03007700 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007701}
7702
Kalle Valoffb9eb32010-02-17 17:58:10 +02007703static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7704{
Johannes Berg4c476992010-10-04 21:36:35 +02007705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007706 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007707 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007708 u8 ps_state;
7709 bool state;
7710 int err;
7711
Johannes Berg4c476992010-10-04 21:36:35 +02007712 if (!info->attrs[NL80211_ATTR_PS_STATE])
7713 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007714
7715 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7716
Johannes Berg4c476992010-10-04 21:36:35 +02007717 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7718 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007719
7720 wdev = dev->ieee80211_ptr;
7721
Johannes Berg4c476992010-10-04 21:36:35 +02007722 if (!rdev->ops->set_power_mgmt)
7723 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007724
7725 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7726
7727 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007728 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007729
Hila Gonene35e4d22012-06-27 17:19:42 +03007730 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007731 if (!err)
7732 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007733 return err;
7734}
7735
7736static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7737{
Johannes Berg4c476992010-10-04 21:36:35 +02007738 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007739 enum nl80211_ps_state ps_state;
7740 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007741 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007742 struct sk_buff *msg;
7743 void *hdr;
7744 int err;
7745
Kalle Valoffb9eb32010-02-17 17:58:10 +02007746 wdev = dev->ieee80211_ptr;
7747
Johannes Berg4c476992010-10-04 21:36:35 +02007748 if (!rdev->ops->set_power_mgmt)
7749 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007750
7751 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007752 if (!msg)
7753 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007754
Eric W. Biederman15e47302012-09-07 20:12:54 +00007755 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007756 NL80211_CMD_GET_POWER_SAVE);
7757 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007758 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007759 goto free_msg;
7760 }
7761
7762 if (wdev->ps)
7763 ps_state = NL80211_PS_ENABLED;
7764 else
7765 ps_state = NL80211_PS_DISABLED;
7766
David S. Miller9360ffd2012-03-29 04:41:26 -04007767 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7768 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007769
7770 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007771 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007772
Johannes Berg4c476992010-10-04 21:36:35 +02007773 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007774 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007775 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007776 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007777 return err;
7778}
7779
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007780static struct nla_policy
7781nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7782 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7783 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7784 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007785 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7786 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7787 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007788};
7789
Thomas Pedersen84f10702012-07-12 16:17:33 -07007790static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007791 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007792{
7793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007794 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007795 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007796
Johannes Bergd9d8b012012-11-26 12:51:52 +01007797 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007798 return -EINVAL;
7799
Thomas Pedersen84f10702012-07-12 16:17:33 -07007800 if (!rdev->ops->set_cqm_txe_config)
7801 return -EOPNOTSUPP;
7802
7803 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7804 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7805 return -EOPNOTSUPP;
7806
Hila Gonene35e4d22012-06-27 17:19:42 +03007807 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007808}
7809
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007810static int nl80211_set_cqm_rssi(struct genl_info *info,
7811 s32 threshold, u32 hysteresis)
7812{
Johannes Berg4c476992010-10-04 21:36:35 +02007813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007814 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007815 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007816
7817 if (threshold > 0)
7818 return -EINVAL;
7819
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007820 /* disabling - hysteresis should also be zero then */
7821 if (threshold == 0)
7822 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007823
Johannes Berg4c476992010-10-04 21:36:35 +02007824 if (!rdev->ops->set_cqm_rssi_config)
7825 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007826
Johannes Berg074ac8d2010-09-16 14:58:22 +02007827 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007828 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7829 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007830
Hila Gonene35e4d22012-06-27 17:19:42 +03007831 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007832}
7833
7834static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7835{
7836 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7837 struct nlattr *cqm;
7838 int err;
7839
7840 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007841 if (!cqm)
7842 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007843
7844 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7845 nl80211_attr_cqm_policy);
7846 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007847 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007848
7849 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7850 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007851 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7852 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007853
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007854 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7855 }
7856
7857 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7858 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7859 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7860 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7861 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7862 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7863
7864 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7865 }
7866
7867 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007868}
7869
Johannes Berg29cbe682010-12-03 09:20:44 +01007870static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7871{
7872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7873 struct net_device *dev = info->user_ptr[1];
7874 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007875 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007876 int err;
7877
7878 /* start with default */
7879 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007880 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007881
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007882 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007883 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007884 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007885 if (err)
7886 return err;
7887 }
7888
7889 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7890 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7891 return -EINVAL;
7892
Javier Cardonac80d5452010-12-16 17:37:49 -08007893 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7894 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7895
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007896 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7897 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7898 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7899 return -EINVAL;
7900
Marco Porsch9bdbf042013-01-07 16:04:51 +01007901 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7902 setup.beacon_interval =
7903 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7904 if (setup.beacon_interval < 10 ||
7905 setup.beacon_interval > 10000)
7906 return -EINVAL;
7907 }
7908
7909 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7910 setup.dtim_period =
7911 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7912 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7913 return -EINVAL;
7914 }
7915
Javier Cardonac80d5452010-12-16 17:37:49 -08007916 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7917 /* parse additional setup parameters if given */
7918 err = nl80211_parse_mesh_setup(info, &setup);
7919 if (err)
7920 return err;
7921 }
7922
Thomas Pedersend37bb182013-03-04 13:06:13 -08007923 if (setup.user_mpm)
7924 cfg.auto_open_plinks = false;
7925
Johannes Bergcc1d2802012-05-16 23:50:20 +02007926 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007927 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7928 if (err)
7929 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007930 } else {
7931 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007932 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007933 }
7934
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07007935 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7936 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7937 int n_rates =
7938 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7939 struct ieee80211_supported_band *sband;
7940
7941 if (!setup.chandef.chan)
7942 return -EINVAL;
7943
7944 sband = rdev->wiphy.bands[setup.chandef.chan->band];
7945
7946 err = ieee80211_get_ratemask(sband, rates, n_rates,
7947 &setup.basic_rates);
7948 if (err)
7949 return err;
7950 }
7951
Javier Cardonac80d5452010-12-16 17:37:49 -08007952 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007953}
7954
7955static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7956{
7957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7958 struct net_device *dev = info->user_ptr[1];
7959
7960 return cfg80211_leave_mesh(rdev, dev);
7961}
7962
Johannes Bergdfb89c52012-06-27 09:23:48 +02007963#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007964static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7965 struct cfg80211_registered_device *rdev)
7966{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007967 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007968 struct nlattr *nl_pats, *nl_pat;
7969 int i, pat_len;
7970
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007971 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007972 return 0;
7973
7974 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7975 if (!nl_pats)
7976 return -ENOBUFS;
7977
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007978 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007979 nl_pat = nla_nest_start(msg, i + 1);
7980 if (!nl_pat)
7981 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007982 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007983 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007984 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007985 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
7986 wowlan->patterns[i].pattern) ||
7987 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007988 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007989 return -ENOBUFS;
7990 nla_nest_end(msg, nl_pat);
7991 }
7992 nla_nest_end(msg, nl_pats);
7993
7994 return 0;
7995}
7996
Johannes Berg2a0e0472013-01-23 22:57:40 +01007997static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7998 struct cfg80211_wowlan_tcp *tcp)
7999{
8000 struct nlattr *nl_tcp;
8001
8002 if (!tcp)
8003 return 0;
8004
8005 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8006 if (!nl_tcp)
8007 return -ENOBUFS;
8008
8009 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8010 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8011 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8012 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8013 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8014 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8015 tcp->payload_len, tcp->payload) ||
8016 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8017 tcp->data_interval) ||
8018 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8019 tcp->wake_len, tcp->wake_data) ||
8020 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8021 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8022 return -ENOBUFS;
8023
8024 if (tcp->payload_seq.len &&
8025 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8026 sizeof(tcp->payload_seq), &tcp->payload_seq))
8027 return -ENOBUFS;
8028
8029 if (tcp->payload_tok.len &&
8030 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8031 sizeof(tcp->payload_tok) + tcp->tokens_size,
8032 &tcp->payload_tok))
8033 return -ENOBUFS;
8034
Johannes Berge248ad32013-05-16 10:24:28 +02008035 nla_nest_end(msg, nl_tcp);
8036
Johannes Berg2a0e0472013-01-23 22:57:40 +01008037 return 0;
8038}
8039
Johannes Bergff1b6e62011-05-04 15:37:28 +02008040static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8041{
8042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8043 struct sk_buff *msg;
8044 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008045 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008046
Johannes Berg964dc9e2013-06-03 17:25:34 +02008047 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008048 return -EOPNOTSUPP;
8049
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008050 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008051 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008052 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8053 rdev->wiphy.wowlan_config->tcp->payload_len +
8054 rdev->wiphy.wowlan_config->tcp->wake_len +
8055 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008056 }
8057
8058 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008059 if (!msg)
8060 return -ENOMEM;
8061
Eric W. Biederman15e47302012-09-07 20:12:54 +00008062 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008063 NL80211_CMD_GET_WOWLAN);
8064 if (!hdr)
8065 goto nla_put_failure;
8066
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008067 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008068 struct nlattr *nl_wowlan;
8069
8070 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8071 if (!nl_wowlan)
8072 goto nla_put_failure;
8073
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008074 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008075 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008076 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008077 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008078 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008079 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008080 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008081 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008082 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008083 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008084 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008085 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008086 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008087 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8088 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008089
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008090 if (nl80211_send_wowlan_patterns(msg, rdev))
8091 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008092
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008093 if (nl80211_send_wowlan_tcp(msg,
8094 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008095 goto nla_put_failure;
8096
Johannes Bergff1b6e62011-05-04 15:37:28 +02008097 nla_nest_end(msg, nl_wowlan);
8098 }
8099
8100 genlmsg_end(msg, hdr);
8101 return genlmsg_reply(msg, info);
8102
8103nla_put_failure:
8104 nlmsg_free(msg);
8105 return -ENOBUFS;
8106}
8107
Johannes Berg2a0e0472013-01-23 22:57:40 +01008108static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8109 struct nlattr *attr,
8110 struct cfg80211_wowlan *trig)
8111{
8112 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8113 struct cfg80211_wowlan_tcp *cfg;
8114 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8115 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8116 u32 size;
8117 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8118 int err, port;
8119
Johannes Berg964dc9e2013-06-03 17:25:34 +02008120 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008121 return -EINVAL;
8122
8123 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8124 nla_data(attr), nla_len(attr),
8125 nl80211_wowlan_tcp_policy);
8126 if (err)
8127 return err;
8128
8129 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8130 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8131 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8132 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8133 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8134 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8135 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8136 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8137 return -EINVAL;
8138
8139 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008140 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008141 return -EINVAL;
8142
8143 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008144 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008145 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008146 return -EINVAL;
8147
8148 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008149 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008150 return -EINVAL;
8151
8152 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8153 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8154 return -EINVAL;
8155
8156 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8157 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8158
8159 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8160 tokens_size = tokln - sizeof(*tok);
8161
8162 if (!tok->len || tokens_size % tok->len)
8163 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008164 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008165 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008166 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008167 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008168 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008169 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008170 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008171 return -EINVAL;
8172 if (tok->offset + tok->len > data_size)
8173 return -EINVAL;
8174 }
8175
8176 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8177 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008178 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008179 return -EINVAL;
8180 if (seq->len == 0 || seq->len > 4)
8181 return -EINVAL;
8182 if (seq->len + seq->offset > data_size)
8183 return -EINVAL;
8184 }
8185
8186 size = sizeof(*cfg);
8187 size += data_size;
8188 size += wake_size + wake_mask_size;
8189 size += tokens_size;
8190
8191 cfg = kzalloc(size, GFP_KERNEL);
8192 if (!cfg)
8193 return -ENOMEM;
8194 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8195 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8196 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8197 ETH_ALEN);
8198 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8199 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8200 else
8201 port = 0;
8202#ifdef CONFIG_INET
8203 /* allocate a socket and port for it and use it */
8204 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8205 IPPROTO_TCP, &cfg->sock, 1);
8206 if (err) {
8207 kfree(cfg);
8208 return err;
8209 }
8210 if (inet_csk_get_port(cfg->sock->sk, port)) {
8211 sock_release(cfg->sock);
8212 kfree(cfg);
8213 return -EADDRINUSE;
8214 }
8215 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8216#else
8217 if (!port) {
8218 kfree(cfg);
8219 return -EINVAL;
8220 }
8221 cfg->src_port = port;
8222#endif
8223
8224 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8225 cfg->payload_len = data_size;
8226 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8227 memcpy((void *)cfg->payload,
8228 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8229 data_size);
8230 if (seq)
8231 cfg->payload_seq = *seq;
8232 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8233 cfg->wake_len = wake_size;
8234 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8235 memcpy((void *)cfg->wake_data,
8236 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8237 wake_size);
8238 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8239 data_size + wake_size;
8240 memcpy((void *)cfg->wake_mask,
8241 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8242 wake_mask_size);
8243 if (tok) {
8244 cfg->tokens_size = tokens_size;
8245 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8246 }
8247
8248 trig->tcp = cfg;
8249
8250 return 0;
8251}
8252
Johannes Bergff1b6e62011-05-04 15:37:28 +02008253static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8254{
8255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8256 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008257 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008258 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008259 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008260 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008261 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008262
Johannes Berg964dc9e2013-06-03 17:25:34 +02008263 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008264 return -EOPNOTSUPP;
8265
Johannes Bergae33bd82012-07-12 16:25:02 +02008266 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8267 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008268 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008269 goto set_wakeup;
8270 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008271
8272 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8273 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8274 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8275 nl80211_wowlan_policy);
8276 if (err)
8277 return err;
8278
8279 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8280 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8281 return -EINVAL;
8282 new_triggers.any = true;
8283 }
8284
8285 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8286 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8287 return -EINVAL;
8288 new_triggers.disconnect = true;
8289 }
8290
8291 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8292 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8293 return -EINVAL;
8294 new_triggers.magic_pkt = true;
8295 }
8296
Johannes Berg77dbbb12011-07-13 10:48:55 +02008297 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8298 return -EINVAL;
8299
8300 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8301 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8302 return -EINVAL;
8303 new_triggers.gtk_rekey_failure = true;
8304 }
8305
8306 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8307 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8308 return -EINVAL;
8309 new_triggers.eap_identity_req = true;
8310 }
8311
8312 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8313 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8314 return -EINVAL;
8315 new_triggers.four_way_handshake = true;
8316 }
8317
8318 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8319 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8320 return -EINVAL;
8321 new_triggers.rfkill_release = true;
8322 }
8323
Johannes Bergff1b6e62011-05-04 15:37:28 +02008324 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8325 struct nlattr *pat;
8326 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008327 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008328 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008329
8330 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8331 rem)
8332 n_patterns++;
8333 if (n_patterns > wowlan->n_patterns)
8334 return -EINVAL;
8335
8336 new_triggers.patterns = kcalloc(n_patterns,
8337 sizeof(new_triggers.patterns[0]),
8338 GFP_KERNEL);
8339 if (!new_triggers.patterns)
8340 return -ENOMEM;
8341
8342 new_triggers.n_patterns = n_patterns;
8343 i = 0;
8344
8345 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8346 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008347 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8348 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008349 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008350 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8351 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008352 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008353 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008354 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008355 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008356 goto error;
8357 if (pat_len > wowlan->pattern_max_len ||
8358 pat_len < wowlan->pattern_min_len)
8359 goto error;
8360
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008361 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008362 pkt_offset = 0;
8363 else
8364 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008365 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008366 if (pkt_offset > wowlan->max_pkt_offset)
8367 goto error;
8368 new_triggers.patterns[i].pkt_offset = pkt_offset;
8369
Johannes Bergff1b6e62011-05-04 15:37:28 +02008370 new_triggers.patterns[i].mask =
8371 kmalloc(mask_len + pat_len, GFP_KERNEL);
8372 if (!new_triggers.patterns[i].mask) {
8373 err = -ENOMEM;
8374 goto error;
8375 }
8376 new_triggers.patterns[i].pattern =
8377 new_triggers.patterns[i].mask + mask_len;
8378 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008379 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008380 mask_len);
8381 new_triggers.patterns[i].pattern_len = pat_len;
8382 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008383 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008384 pat_len);
8385 i++;
8386 }
8387 }
8388
Johannes Berg2a0e0472013-01-23 22:57:40 +01008389 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8390 err = nl80211_parse_wowlan_tcp(
8391 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8392 &new_triggers);
8393 if (err)
8394 goto error;
8395 }
8396
Johannes Bergae33bd82012-07-12 16:25:02 +02008397 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8398 if (!ntrig) {
8399 err = -ENOMEM;
8400 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008401 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008402 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008403 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008404
Johannes Bergae33bd82012-07-12 16:25:02 +02008405 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008406 if (rdev->ops->set_wakeup &&
8407 prev_enabled != !!rdev->wiphy.wowlan_config)
8408 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008409
Johannes Bergff1b6e62011-05-04 15:37:28 +02008410 return 0;
8411 error:
8412 for (i = 0; i < new_triggers.n_patterns; i++)
8413 kfree(new_triggers.patterns[i].mask);
8414 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008415 if (new_triggers.tcp && new_triggers.tcp->sock)
8416 sock_release(new_triggers.tcp->sock);
8417 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008418 return err;
8419}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008420#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008421
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008422static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8423 struct cfg80211_registered_device *rdev)
8424{
8425 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8426 int i, j, pat_len;
8427 struct cfg80211_coalesce_rules *rule;
8428
8429 if (!rdev->coalesce->n_rules)
8430 return 0;
8431
8432 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8433 if (!nl_rules)
8434 return -ENOBUFS;
8435
8436 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8437 nl_rule = nla_nest_start(msg, i + 1);
8438 if (!nl_rule)
8439 return -ENOBUFS;
8440
8441 rule = &rdev->coalesce->rules[i];
8442 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8443 rule->delay))
8444 return -ENOBUFS;
8445
8446 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8447 rule->condition))
8448 return -ENOBUFS;
8449
8450 nl_pats = nla_nest_start(msg,
8451 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8452 if (!nl_pats)
8453 return -ENOBUFS;
8454
8455 for (j = 0; j < rule->n_patterns; j++) {
8456 nl_pat = nla_nest_start(msg, j + 1);
8457 if (!nl_pat)
8458 return -ENOBUFS;
8459 pat_len = rule->patterns[j].pattern_len;
8460 if (nla_put(msg, NL80211_PKTPAT_MASK,
8461 DIV_ROUND_UP(pat_len, 8),
8462 rule->patterns[j].mask) ||
8463 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8464 rule->patterns[j].pattern) ||
8465 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8466 rule->patterns[j].pkt_offset))
8467 return -ENOBUFS;
8468 nla_nest_end(msg, nl_pat);
8469 }
8470 nla_nest_end(msg, nl_pats);
8471 nla_nest_end(msg, nl_rule);
8472 }
8473 nla_nest_end(msg, nl_rules);
8474
8475 return 0;
8476}
8477
8478static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8479{
8480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8481 struct sk_buff *msg;
8482 void *hdr;
8483
8484 if (!rdev->wiphy.coalesce)
8485 return -EOPNOTSUPP;
8486
8487 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8488 if (!msg)
8489 return -ENOMEM;
8490
8491 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8492 NL80211_CMD_GET_COALESCE);
8493 if (!hdr)
8494 goto nla_put_failure;
8495
8496 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8497 goto nla_put_failure;
8498
8499 genlmsg_end(msg, hdr);
8500 return genlmsg_reply(msg, info);
8501
8502nla_put_failure:
8503 nlmsg_free(msg);
8504 return -ENOBUFS;
8505}
8506
8507void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8508{
8509 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8510 int i, j;
8511 struct cfg80211_coalesce_rules *rule;
8512
8513 if (!coalesce)
8514 return;
8515
8516 for (i = 0; i < coalesce->n_rules; i++) {
8517 rule = &coalesce->rules[i];
8518 for (j = 0; j < rule->n_patterns; j++)
8519 kfree(rule->patterns[j].mask);
8520 kfree(rule->patterns);
8521 }
8522 kfree(coalesce->rules);
8523 kfree(coalesce);
8524 rdev->coalesce = NULL;
8525}
8526
8527static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8528 struct nlattr *rule,
8529 struct cfg80211_coalesce_rules *new_rule)
8530{
8531 int err, i;
8532 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8533 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8534 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8535 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8536
8537 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8538 nla_len(rule), nl80211_coalesce_policy);
8539 if (err)
8540 return err;
8541
8542 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8543 new_rule->delay =
8544 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8545 if (new_rule->delay > coalesce->max_delay)
8546 return -EINVAL;
8547
8548 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8549 new_rule->condition =
8550 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8551 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8552 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8553 return -EINVAL;
8554
8555 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8556 return -EINVAL;
8557
8558 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8559 rem)
8560 n_patterns++;
8561 if (n_patterns > coalesce->n_patterns)
8562 return -EINVAL;
8563
8564 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8565 GFP_KERNEL);
8566 if (!new_rule->patterns)
8567 return -ENOMEM;
8568
8569 new_rule->n_patterns = n_patterns;
8570 i = 0;
8571
8572 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8573 rem) {
8574 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8575 nla_len(pat), NULL);
8576 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8577 !pat_tb[NL80211_PKTPAT_PATTERN])
8578 return -EINVAL;
8579 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8580 mask_len = DIV_ROUND_UP(pat_len, 8);
8581 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8582 return -EINVAL;
8583 if (pat_len > coalesce->pattern_max_len ||
8584 pat_len < coalesce->pattern_min_len)
8585 return -EINVAL;
8586
8587 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8588 pkt_offset = 0;
8589 else
8590 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8591 if (pkt_offset > coalesce->max_pkt_offset)
8592 return -EINVAL;
8593 new_rule->patterns[i].pkt_offset = pkt_offset;
8594
8595 new_rule->patterns[i].mask =
8596 kmalloc(mask_len + pat_len, GFP_KERNEL);
8597 if (!new_rule->patterns[i].mask)
8598 return -ENOMEM;
8599 new_rule->patterns[i].pattern =
8600 new_rule->patterns[i].mask + mask_len;
8601 memcpy(new_rule->patterns[i].mask,
8602 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8603 new_rule->patterns[i].pattern_len = pat_len;
8604 memcpy(new_rule->patterns[i].pattern,
8605 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8606 i++;
8607 }
8608
8609 return 0;
8610}
8611
8612static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8613{
8614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8615 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8616 struct cfg80211_coalesce new_coalesce = {};
8617 struct cfg80211_coalesce *n_coalesce;
8618 int err, rem_rule, n_rules = 0, i, j;
8619 struct nlattr *rule;
8620 struct cfg80211_coalesce_rules *tmp_rule;
8621
8622 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8623 return -EOPNOTSUPP;
8624
8625 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8626 cfg80211_rdev_free_coalesce(rdev);
8627 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8628 return 0;
8629 }
8630
8631 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8632 rem_rule)
8633 n_rules++;
8634 if (n_rules > coalesce->n_rules)
8635 return -EINVAL;
8636
8637 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8638 GFP_KERNEL);
8639 if (!new_coalesce.rules)
8640 return -ENOMEM;
8641
8642 new_coalesce.n_rules = n_rules;
8643 i = 0;
8644
8645 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8646 rem_rule) {
8647 err = nl80211_parse_coalesce_rule(rdev, rule,
8648 &new_coalesce.rules[i]);
8649 if (err)
8650 goto error;
8651
8652 i++;
8653 }
8654
8655 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8656 if (err)
8657 goto error;
8658
8659 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8660 if (!n_coalesce) {
8661 err = -ENOMEM;
8662 goto error;
8663 }
8664 cfg80211_rdev_free_coalesce(rdev);
8665 rdev->coalesce = n_coalesce;
8666
8667 return 0;
8668error:
8669 for (i = 0; i < new_coalesce.n_rules; i++) {
8670 tmp_rule = &new_coalesce.rules[i];
8671 for (j = 0; j < tmp_rule->n_patterns; j++)
8672 kfree(tmp_rule->patterns[j].mask);
8673 kfree(tmp_rule->patterns);
8674 }
8675 kfree(new_coalesce.rules);
8676
8677 return err;
8678}
8679
Johannes Berge5497d72011-07-05 16:35:40 +02008680static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8681{
8682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8683 struct net_device *dev = info->user_ptr[1];
8684 struct wireless_dev *wdev = dev->ieee80211_ptr;
8685 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8686 struct cfg80211_gtk_rekey_data rekey_data;
8687 int err;
8688
8689 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8690 return -EINVAL;
8691
8692 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8693 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8694 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8695 nl80211_rekey_policy);
8696 if (err)
8697 return err;
8698
8699 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8700 return -ERANGE;
8701 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8702 return -ERANGE;
8703 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8704 return -ERANGE;
8705
8706 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8707 NL80211_KEK_LEN);
8708 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8709 NL80211_KCK_LEN);
8710 memcpy(rekey_data.replay_ctr,
8711 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8712 NL80211_REPLAY_CTR_LEN);
8713
8714 wdev_lock(wdev);
8715 if (!wdev->current_bss) {
8716 err = -ENOTCONN;
8717 goto out;
8718 }
8719
8720 if (!rdev->ops->set_rekey_data) {
8721 err = -EOPNOTSUPP;
8722 goto out;
8723 }
8724
Hila Gonene35e4d22012-06-27 17:19:42 +03008725 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008726 out:
8727 wdev_unlock(wdev);
8728 return err;
8729}
8730
Johannes Berg28946da2011-11-04 11:18:12 +01008731static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8732 struct genl_info *info)
8733{
8734 struct net_device *dev = info->user_ptr[1];
8735 struct wireless_dev *wdev = dev->ieee80211_ptr;
8736
8737 if (wdev->iftype != NL80211_IFTYPE_AP &&
8738 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8739 return -EINVAL;
8740
Eric W. Biederman15e47302012-09-07 20:12:54 +00008741 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008742 return -EBUSY;
8743
Eric W. Biederman15e47302012-09-07 20:12:54 +00008744 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008745 return 0;
8746}
8747
Johannes Berg7f6cf312011-11-04 11:18:15 +01008748static int nl80211_probe_client(struct sk_buff *skb,
8749 struct genl_info *info)
8750{
8751 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8752 struct net_device *dev = info->user_ptr[1];
8753 struct wireless_dev *wdev = dev->ieee80211_ptr;
8754 struct sk_buff *msg;
8755 void *hdr;
8756 const u8 *addr;
8757 u64 cookie;
8758 int err;
8759
8760 if (wdev->iftype != NL80211_IFTYPE_AP &&
8761 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8762 return -EOPNOTSUPP;
8763
8764 if (!info->attrs[NL80211_ATTR_MAC])
8765 return -EINVAL;
8766
8767 if (!rdev->ops->probe_client)
8768 return -EOPNOTSUPP;
8769
8770 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8771 if (!msg)
8772 return -ENOMEM;
8773
Eric W. Biederman15e47302012-09-07 20:12:54 +00008774 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008775 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008776 if (!hdr) {
8777 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008778 goto free_msg;
8779 }
8780
8781 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8782
Hila Gonene35e4d22012-06-27 17:19:42 +03008783 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008784 if (err)
8785 goto free_msg;
8786
David S. Miller9360ffd2012-03-29 04:41:26 -04008787 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8788 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008789
8790 genlmsg_end(msg, hdr);
8791
8792 return genlmsg_reply(msg, info);
8793
8794 nla_put_failure:
8795 err = -ENOBUFS;
8796 free_msg:
8797 nlmsg_free(msg);
8798 return err;
8799}
8800
Johannes Berg5e760232011-11-04 11:18:17 +01008801static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8802{
8803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008804 struct cfg80211_beacon_registration *reg, *nreg;
8805 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008806
8807 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8808 return -EOPNOTSUPP;
8809
Ben Greear37c73b52012-10-26 14:49:25 -07008810 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8811 if (!nreg)
8812 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008813
Ben Greear37c73b52012-10-26 14:49:25 -07008814 /* First, check if already registered. */
8815 spin_lock_bh(&rdev->beacon_registrations_lock);
8816 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8817 if (reg->nlportid == info->snd_portid) {
8818 rv = -EALREADY;
8819 goto out_err;
8820 }
8821 }
8822 /* Add it to the list */
8823 nreg->nlportid = info->snd_portid;
8824 list_add(&nreg->list, &rdev->beacon_registrations);
8825
8826 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008827
8828 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008829out_err:
8830 spin_unlock_bh(&rdev->beacon_registrations_lock);
8831 kfree(nreg);
8832 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008833}
8834
Johannes Berg98104fde2012-06-16 00:19:54 +02008835static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8836{
8837 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8838 struct wireless_dev *wdev = info->user_ptr[1];
8839 int err;
8840
8841 if (!rdev->ops->start_p2p_device)
8842 return -EOPNOTSUPP;
8843
8844 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8845 return -EOPNOTSUPP;
8846
8847 if (wdev->p2p_started)
8848 return 0;
8849
Johannes Berg98104fde2012-06-16 00:19:54 +02008850 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008851 if (err)
8852 return err;
8853
Johannes Bergeeb126e2012-10-23 15:16:50 +02008854 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008855 if (err)
8856 return err;
8857
8858 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008859 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008860
8861 return 0;
8862}
8863
8864static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8865{
8866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8867 struct wireless_dev *wdev = info->user_ptr[1];
8868
8869 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8870 return -EOPNOTSUPP;
8871
8872 if (!rdev->ops->stop_p2p_device)
8873 return -EOPNOTSUPP;
8874
Johannes Bergf9f47522013-03-19 15:04:07 +01008875 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008876
8877 return 0;
8878}
8879
Johannes Berg3713b4e2013-02-14 16:19:38 +01008880static int nl80211_get_protocol_features(struct sk_buff *skb,
8881 struct genl_info *info)
8882{
8883 void *hdr;
8884 struct sk_buff *msg;
8885
8886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8887 if (!msg)
8888 return -ENOMEM;
8889
8890 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8891 NL80211_CMD_GET_PROTOCOL_FEATURES);
8892 if (!hdr)
8893 goto nla_put_failure;
8894
8895 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8896 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8897 goto nla_put_failure;
8898
8899 genlmsg_end(msg, hdr);
8900 return genlmsg_reply(msg, info);
8901
8902 nla_put_failure:
8903 kfree_skb(msg);
8904 return -ENOBUFS;
8905}
8906
Jouni Malinen355199e2013-02-27 17:14:27 +02008907static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8908{
8909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8910 struct cfg80211_update_ft_ies_params ft_params;
8911 struct net_device *dev = info->user_ptr[1];
8912
8913 if (!rdev->ops->update_ft_ies)
8914 return -EOPNOTSUPP;
8915
8916 if (!info->attrs[NL80211_ATTR_MDID] ||
8917 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8918 return -EINVAL;
8919
8920 memset(&ft_params, 0, sizeof(ft_params));
8921 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8922 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8923 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8924
8925 return rdev_update_ft_ies(rdev, dev, &ft_params);
8926}
8927
Arend van Spriel5de17982013-04-18 15:49:00 +02008928static int nl80211_crit_protocol_start(struct sk_buff *skb,
8929 struct genl_info *info)
8930{
8931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8932 struct wireless_dev *wdev = info->user_ptr[1];
8933 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8934 u16 duration;
8935 int ret;
8936
8937 if (!rdev->ops->crit_proto_start)
8938 return -EOPNOTSUPP;
8939
8940 if (WARN_ON(!rdev->ops->crit_proto_stop))
8941 return -EINVAL;
8942
8943 if (rdev->crit_proto_nlportid)
8944 return -EBUSY;
8945
8946 /* determine protocol if provided */
8947 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8948 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8949
8950 if (proto >= NUM_NL80211_CRIT_PROTO)
8951 return -EINVAL;
8952
8953 /* timeout must be provided */
8954 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8955 return -EINVAL;
8956
8957 duration =
8958 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8959
8960 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8961 return -ERANGE;
8962
8963 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8964 if (!ret)
8965 rdev->crit_proto_nlportid = info->snd_portid;
8966
8967 return ret;
8968}
8969
8970static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8971 struct genl_info *info)
8972{
8973 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8974 struct wireless_dev *wdev = info->user_ptr[1];
8975
8976 if (!rdev->ops->crit_proto_stop)
8977 return -EOPNOTSUPP;
8978
8979 if (rdev->crit_proto_nlportid) {
8980 rdev->crit_proto_nlportid = 0;
8981 rdev_crit_proto_stop(rdev, wdev);
8982 }
8983 return 0;
8984}
8985
Johannes Bergad7e7182013-11-13 13:37:47 +01008986static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
8987{
8988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8989 struct wireless_dev *wdev =
8990 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8991 int i, err;
8992 u32 vid, subcmd;
8993
8994 if (!rdev->wiphy.vendor_commands)
8995 return -EOPNOTSUPP;
8996
8997 if (IS_ERR(wdev)) {
8998 err = PTR_ERR(wdev);
8999 if (err != -EINVAL)
9000 return err;
9001 wdev = NULL;
9002 } else if (wdev->wiphy != &rdev->wiphy) {
9003 return -EINVAL;
9004 }
9005
9006 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9007 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9008 return -EINVAL;
9009
9010 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9011 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9012 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9013 const struct wiphy_vendor_command *vcmd;
9014 void *data = NULL;
9015 int len = 0;
9016
9017 vcmd = &rdev->wiphy.vendor_commands[i];
9018
9019 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9020 continue;
9021
9022 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9023 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9024 if (!wdev)
9025 return -EINVAL;
9026 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9027 !wdev->netdev)
9028 return -EINVAL;
9029
9030 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9031 if (wdev->netdev &&
9032 !netif_running(wdev->netdev))
9033 return -ENETDOWN;
9034 if (!wdev->netdev && !wdev->p2p_started)
9035 return -ENETDOWN;
9036 }
9037 } else {
9038 wdev = NULL;
9039 }
9040
9041 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9042 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9043 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9044 }
9045
9046 rdev->cur_cmd_info = info;
9047 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9048 data, len);
9049 rdev->cur_cmd_info = NULL;
9050 return err;
9051 }
9052
9053 return -EOPNOTSUPP;
9054}
9055
9056struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9057 enum nl80211_commands cmd,
9058 enum nl80211_attrs attr,
9059 int approxlen)
9060{
9061 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9062
9063 if (WARN_ON(!rdev->cur_cmd_info))
9064 return NULL;
9065
9066 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9067 rdev->cur_cmd_info->snd_portid,
9068 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009069 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009070}
9071EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9072
9073int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9074{
9075 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9076 void *hdr = ((void **)skb->cb)[1];
9077 struct nlattr *data = ((void **)skb->cb)[2];
9078
9079 if (WARN_ON(!rdev->cur_cmd_info)) {
9080 kfree_skb(skb);
9081 return -EINVAL;
9082 }
9083
9084 nla_nest_end(skb, data);
9085 genlmsg_end(skb, hdr);
9086 return genlmsg_reply(skb, rdev->cur_cmd_info);
9087}
9088EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9089
9090
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009091static int nl80211_set_qos_map(struct sk_buff *skb,
9092 struct genl_info *info)
9093{
9094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9095 struct cfg80211_qos_map *qos_map = NULL;
9096 struct net_device *dev = info->user_ptr[1];
9097 u8 *pos, len, num_des, des_len, des;
9098 int ret;
9099
9100 if (!rdev->ops->set_qos_map)
9101 return -EOPNOTSUPP;
9102
9103 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9104 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9105 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9106
9107 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9108 len > IEEE80211_QOS_MAP_LEN_MAX)
9109 return -EINVAL;
9110
9111 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9112 if (!qos_map)
9113 return -ENOMEM;
9114
9115 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9116 if (num_des) {
9117 des_len = num_des *
9118 sizeof(struct cfg80211_dscp_exception);
9119 memcpy(qos_map->dscp_exception, pos, des_len);
9120 qos_map->num_des = num_des;
9121 for (des = 0; des < num_des; des++) {
9122 if (qos_map->dscp_exception[des].up > 7) {
9123 kfree(qos_map);
9124 return -EINVAL;
9125 }
9126 }
9127 pos += des_len;
9128 }
9129 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9130 }
9131
9132 wdev_lock(dev->ieee80211_ptr);
9133 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9134 if (!ret)
9135 ret = rdev_set_qos_map(rdev, dev, qos_map);
9136 wdev_unlock(dev->ieee80211_ptr);
9137
9138 kfree(qos_map);
9139 return ret;
9140}
9141
Johannes Berg4c476992010-10-04 21:36:35 +02009142#define NL80211_FLAG_NEED_WIPHY 0x01
9143#define NL80211_FLAG_NEED_NETDEV 0x02
9144#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009145#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9146#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9147 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009148#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009149/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009150#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9151 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009152
Johannes Bergf84f7712013-11-14 17:14:45 +01009153static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009154 struct genl_info *info)
9155{
9156 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009157 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009158 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009159 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9160
9161 if (rtnl)
9162 rtnl_lock();
9163
9164 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009165 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009166 if (IS_ERR(rdev)) {
9167 if (rtnl)
9168 rtnl_unlock();
9169 return PTR_ERR(rdev);
9170 }
9171 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009172 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9173 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009174 ASSERT_RTNL();
9175
Johannes Berg89a54e42012-06-15 14:33:17 +02009176 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9177 info->attrs);
9178 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009179 if (rtnl)
9180 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009181 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009182 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009183
Johannes Berg89a54e42012-06-15 14:33:17 +02009184 dev = wdev->netdev;
9185 rdev = wiphy_to_dev(wdev->wiphy);
9186
Johannes Berg1bf614e2012-06-15 15:23:36 +02009187 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9188 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009189 if (rtnl)
9190 rtnl_unlock();
9191 return -EINVAL;
9192 }
9193
9194 info->user_ptr[1] = dev;
9195 } else {
9196 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009197 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009198
Johannes Berg1bf614e2012-06-15 15:23:36 +02009199 if (dev) {
9200 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9201 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009202 if (rtnl)
9203 rtnl_unlock();
9204 return -ENETDOWN;
9205 }
9206
9207 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009208 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9209 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009210 if (rtnl)
9211 rtnl_unlock();
9212 return -ENETDOWN;
9213 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009214 }
9215
Johannes Berg4c476992010-10-04 21:36:35 +02009216 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009217 }
9218
9219 return 0;
9220}
9221
Johannes Bergf84f7712013-11-14 17:14:45 +01009222static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009223 struct genl_info *info)
9224{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009225 if (info->user_ptr[1]) {
9226 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9227 struct wireless_dev *wdev = info->user_ptr[1];
9228
9229 if (wdev->netdev)
9230 dev_put(wdev->netdev);
9231 } else {
9232 dev_put(info->user_ptr[1]);
9233 }
9234 }
Johannes Berg4c476992010-10-04 21:36:35 +02009235 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9236 rtnl_unlock();
9237}
9238
Johannes Berg4534de82013-11-14 17:14:46 +01009239static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009240 {
9241 .cmd = NL80211_CMD_GET_WIPHY,
9242 .doit = nl80211_get_wiphy,
9243 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009244 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009245 .policy = nl80211_policy,
9246 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009247 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9248 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009249 },
9250 {
9251 .cmd = NL80211_CMD_SET_WIPHY,
9252 .doit = nl80211_set_wiphy,
9253 .policy = nl80211_policy,
9254 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009255 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009256 },
9257 {
9258 .cmd = NL80211_CMD_GET_INTERFACE,
9259 .doit = nl80211_get_interface,
9260 .dumpit = nl80211_dump_interface,
9261 .policy = nl80211_policy,
9262 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009263 .internal_flags = NL80211_FLAG_NEED_WDEV |
9264 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009265 },
9266 {
9267 .cmd = NL80211_CMD_SET_INTERFACE,
9268 .doit = nl80211_set_interface,
9269 .policy = nl80211_policy,
9270 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009271 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9272 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009273 },
9274 {
9275 .cmd = NL80211_CMD_NEW_INTERFACE,
9276 .doit = nl80211_new_interface,
9277 .policy = nl80211_policy,
9278 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009279 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9280 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009281 },
9282 {
9283 .cmd = NL80211_CMD_DEL_INTERFACE,
9284 .doit = nl80211_del_interface,
9285 .policy = nl80211_policy,
9286 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009287 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009288 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009289 },
Johannes Berg41ade002007-12-19 02:03:29 +01009290 {
9291 .cmd = NL80211_CMD_GET_KEY,
9292 .doit = nl80211_get_key,
9293 .policy = nl80211_policy,
9294 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009295 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009296 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009297 },
9298 {
9299 .cmd = NL80211_CMD_SET_KEY,
9300 .doit = nl80211_set_key,
9301 .policy = nl80211_policy,
9302 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009303 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009304 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009305 },
9306 {
9307 .cmd = NL80211_CMD_NEW_KEY,
9308 .doit = nl80211_new_key,
9309 .policy = nl80211_policy,
9310 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009312 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009313 },
9314 {
9315 .cmd = NL80211_CMD_DEL_KEY,
9316 .doit = nl80211_del_key,
9317 .policy = nl80211_policy,
9318 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009319 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009320 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009321 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009322 {
9323 .cmd = NL80211_CMD_SET_BEACON,
9324 .policy = nl80211_policy,
9325 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009326 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009328 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009329 },
9330 {
Johannes Berg88600202012-02-13 15:17:18 +01009331 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009332 .policy = nl80211_policy,
9333 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009334 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009336 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009337 },
9338 {
Johannes Berg88600202012-02-13 15:17:18 +01009339 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009340 .policy = nl80211_policy,
9341 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009342 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009343 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009344 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009345 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009346 {
9347 .cmd = NL80211_CMD_GET_STATION,
9348 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009349 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009350 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009351 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9352 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009353 },
9354 {
9355 .cmd = NL80211_CMD_SET_STATION,
9356 .doit = nl80211_set_station,
9357 .policy = nl80211_policy,
9358 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009359 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009360 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009361 },
9362 {
9363 .cmd = NL80211_CMD_NEW_STATION,
9364 .doit = nl80211_new_station,
9365 .policy = nl80211_policy,
9366 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009368 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009369 },
9370 {
9371 .cmd = NL80211_CMD_DEL_STATION,
9372 .doit = nl80211_del_station,
9373 .policy = nl80211_policy,
9374 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009375 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009376 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009377 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009378 {
9379 .cmd = NL80211_CMD_GET_MPATH,
9380 .doit = nl80211_get_mpath,
9381 .dumpit = nl80211_dump_mpath,
9382 .policy = nl80211_policy,
9383 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009384 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009385 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009386 },
9387 {
9388 .cmd = NL80211_CMD_SET_MPATH,
9389 .doit = nl80211_set_mpath,
9390 .policy = nl80211_policy,
9391 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009392 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009393 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009394 },
9395 {
9396 .cmd = NL80211_CMD_NEW_MPATH,
9397 .doit = nl80211_new_mpath,
9398 .policy = nl80211_policy,
9399 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009400 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009401 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009402 },
9403 {
9404 .cmd = NL80211_CMD_DEL_MPATH,
9405 .doit = nl80211_del_mpath,
9406 .policy = nl80211_policy,
9407 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009408 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009409 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009410 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009411 {
9412 .cmd = NL80211_CMD_SET_BSS,
9413 .doit = nl80211_set_bss,
9414 .policy = nl80211_policy,
9415 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009416 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009417 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009418 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009419 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009420 .cmd = NL80211_CMD_GET_REG,
9421 .doit = nl80211_get_reg,
9422 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009423 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009424 /* can be retrieved by unprivileged users */
9425 },
9426 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009427 .cmd = NL80211_CMD_SET_REG,
9428 .doit = nl80211_set_reg,
9429 .policy = nl80211_policy,
9430 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009431 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009432 },
9433 {
9434 .cmd = NL80211_CMD_REQ_SET_REG,
9435 .doit = nl80211_req_set_reg,
9436 .policy = nl80211_policy,
9437 .flags = GENL_ADMIN_PERM,
9438 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009439 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009440 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9441 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009442 .policy = nl80211_policy,
9443 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009444 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009445 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009446 },
9447 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009448 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9449 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009450 .policy = nl80211_policy,
9451 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009453 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009454 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009455 {
Johannes Berg2a519312009-02-10 21:25:55 +01009456 .cmd = NL80211_CMD_TRIGGER_SCAN,
9457 .doit = nl80211_trigger_scan,
9458 .policy = nl80211_policy,
9459 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009460 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009461 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009462 },
9463 {
9464 .cmd = NL80211_CMD_GET_SCAN,
9465 .policy = nl80211_policy,
9466 .dumpit = nl80211_dump_scan,
9467 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009468 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009469 .cmd = NL80211_CMD_START_SCHED_SCAN,
9470 .doit = nl80211_start_sched_scan,
9471 .policy = nl80211_policy,
9472 .flags = GENL_ADMIN_PERM,
9473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9474 NL80211_FLAG_NEED_RTNL,
9475 },
9476 {
9477 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9478 .doit = nl80211_stop_sched_scan,
9479 .policy = nl80211_policy,
9480 .flags = GENL_ADMIN_PERM,
9481 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9482 NL80211_FLAG_NEED_RTNL,
9483 },
9484 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009485 .cmd = NL80211_CMD_AUTHENTICATE,
9486 .doit = nl80211_authenticate,
9487 .policy = nl80211_policy,
9488 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009489 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009490 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009491 },
9492 {
9493 .cmd = NL80211_CMD_ASSOCIATE,
9494 .doit = nl80211_associate,
9495 .policy = nl80211_policy,
9496 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009497 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009498 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009499 },
9500 {
9501 .cmd = NL80211_CMD_DEAUTHENTICATE,
9502 .doit = nl80211_deauthenticate,
9503 .policy = nl80211_policy,
9504 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009505 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009506 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009507 },
9508 {
9509 .cmd = NL80211_CMD_DISASSOCIATE,
9510 .doit = nl80211_disassociate,
9511 .policy = nl80211_policy,
9512 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009513 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009514 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009515 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009516 {
9517 .cmd = NL80211_CMD_JOIN_IBSS,
9518 .doit = nl80211_join_ibss,
9519 .policy = nl80211_policy,
9520 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009522 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009523 },
9524 {
9525 .cmd = NL80211_CMD_LEAVE_IBSS,
9526 .doit = nl80211_leave_ibss,
9527 .policy = nl80211_policy,
9528 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009530 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009531 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009532#ifdef CONFIG_NL80211_TESTMODE
9533 {
9534 .cmd = NL80211_CMD_TESTMODE,
9535 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009536 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009537 .policy = nl80211_policy,
9538 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009539 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9540 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009541 },
9542#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009543 {
9544 .cmd = NL80211_CMD_CONNECT,
9545 .doit = nl80211_connect,
9546 .policy = nl80211_policy,
9547 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009548 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009549 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009550 },
9551 {
9552 .cmd = NL80211_CMD_DISCONNECT,
9553 .doit = nl80211_disconnect,
9554 .policy = nl80211_policy,
9555 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009556 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009557 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009558 },
Johannes Berg463d0182009-07-14 00:33:35 +02009559 {
9560 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9561 .doit = nl80211_wiphy_netns,
9562 .policy = nl80211_policy,
9563 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009564 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9565 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009566 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009567 {
9568 .cmd = NL80211_CMD_GET_SURVEY,
9569 .policy = nl80211_policy,
9570 .dumpit = nl80211_dump_survey,
9571 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009572 {
9573 .cmd = NL80211_CMD_SET_PMKSA,
9574 .doit = nl80211_setdel_pmksa,
9575 .policy = nl80211_policy,
9576 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009578 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009579 },
9580 {
9581 .cmd = NL80211_CMD_DEL_PMKSA,
9582 .doit = nl80211_setdel_pmksa,
9583 .policy = nl80211_policy,
9584 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009585 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009586 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009587 },
9588 {
9589 .cmd = NL80211_CMD_FLUSH_PMKSA,
9590 .doit = nl80211_flush_pmksa,
9591 .policy = nl80211_policy,
9592 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009593 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009594 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009595 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009596 {
9597 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9598 .doit = nl80211_remain_on_channel,
9599 .policy = nl80211_policy,
9600 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009601 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009602 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009603 },
9604 {
9605 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9606 .doit = nl80211_cancel_remain_on_channel,
9607 .policy = nl80211_policy,
9608 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009609 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009610 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009611 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009612 {
9613 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9614 .doit = nl80211_set_tx_bitrate_mask,
9615 .policy = nl80211_policy,
9616 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9618 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009619 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009620 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009621 .cmd = NL80211_CMD_REGISTER_FRAME,
9622 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009623 .policy = nl80211_policy,
9624 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009625 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009626 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009627 },
9628 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009629 .cmd = NL80211_CMD_FRAME,
9630 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009631 .policy = nl80211_policy,
9632 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009633 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009634 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009635 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009636 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009637 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9638 .doit = nl80211_tx_mgmt_cancel_wait,
9639 .policy = nl80211_policy,
9640 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009641 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009642 NL80211_FLAG_NEED_RTNL,
9643 },
9644 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009645 .cmd = NL80211_CMD_SET_POWER_SAVE,
9646 .doit = nl80211_set_power_save,
9647 .policy = nl80211_policy,
9648 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009649 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9650 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009651 },
9652 {
9653 .cmd = NL80211_CMD_GET_POWER_SAVE,
9654 .doit = nl80211_get_power_save,
9655 .policy = nl80211_policy,
9656 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009657 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9658 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009659 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009660 {
9661 .cmd = NL80211_CMD_SET_CQM,
9662 .doit = nl80211_set_cqm,
9663 .policy = nl80211_policy,
9664 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009665 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9666 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009667 },
Johannes Bergf444de02010-05-05 15:25:02 +02009668 {
9669 .cmd = NL80211_CMD_SET_CHANNEL,
9670 .doit = nl80211_set_channel,
9671 .policy = nl80211_policy,
9672 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009673 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9674 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009675 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009676 {
9677 .cmd = NL80211_CMD_SET_WDS_PEER,
9678 .doit = nl80211_set_wds_peer,
9679 .policy = nl80211_policy,
9680 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009681 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9682 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009683 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009684 {
9685 .cmd = NL80211_CMD_JOIN_MESH,
9686 .doit = nl80211_join_mesh,
9687 .policy = nl80211_policy,
9688 .flags = GENL_ADMIN_PERM,
9689 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9690 NL80211_FLAG_NEED_RTNL,
9691 },
9692 {
9693 .cmd = NL80211_CMD_LEAVE_MESH,
9694 .doit = nl80211_leave_mesh,
9695 .policy = nl80211_policy,
9696 .flags = GENL_ADMIN_PERM,
9697 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9698 NL80211_FLAG_NEED_RTNL,
9699 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009700#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009701 {
9702 .cmd = NL80211_CMD_GET_WOWLAN,
9703 .doit = nl80211_get_wowlan,
9704 .policy = nl80211_policy,
9705 /* can be retrieved by unprivileged users */
9706 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9707 NL80211_FLAG_NEED_RTNL,
9708 },
9709 {
9710 .cmd = NL80211_CMD_SET_WOWLAN,
9711 .doit = nl80211_set_wowlan,
9712 .policy = nl80211_policy,
9713 .flags = GENL_ADMIN_PERM,
9714 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9715 NL80211_FLAG_NEED_RTNL,
9716 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009717#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009718 {
9719 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9720 .doit = nl80211_set_rekey_data,
9721 .policy = nl80211_policy,
9722 .flags = GENL_ADMIN_PERM,
9723 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9724 NL80211_FLAG_NEED_RTNL,
9725 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009726 {
9727 .cmd = NL80211_CMD_TDLS_MGMT,
9728 .doit = nl80211_tdls_mgmt,
9729 .policy = nl80211_policy,
9730 .flags = GENL_ADMIN_PERM,
9731 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9732 NL80211_FLAG_NEED_RTNL,
9733 },
9734 {
9735 .cmd = NL80211_CMD_TDLS_OPER,
9736 .doit = nl80211_tdls_oper,
9737 .policy = nl80211_policy,
9738 .flags = GENL_ADMIN_PERM,
9739 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9740 NL80211_FLAG_NEED_RTNL,
9741 },
Johannes Berg28946da2011-11-04 11:18:12 +01009742 {
9743 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9744 .doit = nl80211_register_unexpected_frame,
9745 .policy = nl80211_policy,
9746 .flags = GENL_ADMIN_PERM,
9747 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9748 NL80211_FLAG_NEED_RTNL,
9749 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009750 {
9751 .cmd = NL80211_CMD_PROBE_CLIENT,
9752 .doit = nl80211_probe_client,
9753 .policy = nl80211_policy,
9754 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009755 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009756 NL80211_FLAG_NEED_RTNL,
9757 },
Johannes Berg5e760232011-11-04 11:18:17 +01009758 {
9759 .cmd = NL80211_CMD_REGISTER_BEACONS,
9760 .doit = nl80211_register_beacons,
9761 .policy = nl80211_policy,
9762 .flags = GENL_ADMIN_PERM,
9763 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9764 NL80211_FLAG_NEED_RTNL,
9765 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009766 {
9767 .cmd = NL80211_CMD_SET_NOACK_MAP,
9768 .doit = nl80211_set_noack_map,
9769 .policy = nl80211_policy,
9770 .flags = GENL_ADMIN_PERM,
9771 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9772 NL80211_FLAG_NEED_RTNL,
9773 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009774 {
9775 .cmd = NL80211_CMD_START_P2P_DEVICE,
9776 .doit = nl80211_start_p2p_device,
9777 .policy = nl80211_policy,
9778 .flags = GENL_ADMIN_PERM,
9779 .internal_flags = NL80211_FLAG_NEED_WDEV |
9780 NL80211_FLAG_NEED_RTNL,
9781 },
9782 {
9783 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9784 .doit = nl80211_stop_p2p_device,
9785 .policy = nl80211_policy,
9786 .flags = GENL_ADMIN_PERM,
9787 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9788 NL80211_FLAG_NEED_RTNL,
9789 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009790 {
9791 .cmd = NL80211_CMD_SET_MCAST_RATE,
9792 .doit = nl80211_set_mcast_rate,
9793 .policy = nl80211_policy,
9794 .flags = GENL_ADMIN_PERM,
9795 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9796 NL80211_FLAG_NEED_RTNL,
9797 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309798 {
9799 .cmd = NL80211_CMD_SET_MAC_ACL,
9800 .doit = nl80211_set_mac_acl,
9801 .policy = nl80211_policy,
9802 .flags = GENL_ADMIN_PERM,
9803 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9804 NL80211_FLAG_NEED_RTNL,
9805 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009806 {
9807 .cmd = NL80211_CMD_RADAR_DETECT,
9808 .doit = nl80211_start_radar_detection,
9809 .policy = nl80211_policy,
9810 .flags = GENL_ADMIN_PERM,
9811 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9812 NL80211_FLAG_NEED_RTNL,
9813 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009814 {
9815 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9816 .doit = nl80211_get_protocol_features,
9817 .policy = nl80211_policy,
9818 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009819 {
9820 .cmd = NL80211_CMD_UPDATE_FT_IES,
9821 .doit = nl80211_update_ft_ies,
9822 .policy = nl80211_policy,
9823 .flags = GENL_ADMIN_PERM,
9824 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9825 NL80211_FLAG_NEED_RTNL,
9826 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009827 {
9828 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9829 .doit = nl80211_crit_protocol_start,
9830 .policy = nl80211_policy,
9831 .flags = GENL_ADMIN_PERM,
9832 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9833 NL80211_FLAG_NEED_RTNL,
9834 },
9835 {
9836 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9837 .doit = nl80211_crit_protocol_stop,
9838 .policy = nl80211_policy,
9839 .flags = GENL_ADMIN_PERM,
9840 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9841 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009842 },
9843 {
9844 .cmd = NL80211_CMD_GET_COALESCE,
9845 .doit = nl80211_get_coalesce,
9846 .policy = nl80211_policy,
9847 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9848 NL80211_FLAG_NEED_RTNL,
9849 },
9850 {
9851 .cmd = NL80211_CMD_SET_COALESCE,
9852 .doit = nl80211_set_coalesce,
9853 .policy = nl80211_policy,
9854 .flags = GENL_ADMIN_PERM,
9855 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9856 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009857 },
9858 {
9859 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9860 .doit = nl80211_channel_switch,
9861 .policy = nl80211_policy,
9862 .flags = GENL_ADMIN_PERM,
9863 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9864 NL80211_FLAG_NEED_RTNL,
9865 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009866 {
9867 .cmd = NL80211_CMD_VENDOR,
9868 .doit = nl80211_vendor_cmd,
9869 .policy = nl80211_policy,
9870 .flags = GENL_ADMIN_PERM,
9871 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9872 NL80211_FLAG_NEED_RTNL,
9873 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009874 {
9875 .cmd = NL80211_CMD_SET_QOS_MAP,
9876 .doit = nl80211_set_qos_map,
9877 .policy = nl80211_policy,
9878 .flags = GENL_ADMIN_PERM,
9879 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9880 NL80211_FLAG_NEED_RTNL,
9881 },
Johannes Berg55682962007-09-20 13:09:35 -04009882};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009883
Johannes Berg55682962007-09-20 13:09:35 -04009884/* notification functions */
9885
9886void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9887{
9888 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +02009889 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04009890
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009891 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009892 if (!msg)
9893 return;
9894
Johannes Berg86e8cf92013-06-19 10:57:22 +02009895 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009896 nlmsg_free(msg);
9897 return;
9898 }
9899
Johannes Berg68eb5502013-11-19 15:19:38 +01009900 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01009901 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009902}
9903
Johannes Berg362a4152009-05-24 16:43:15 +02009904static int nl80211_add_scan_req(struct sk_buff *msg,
9905 struct cfg80211_registered_device *rdev)
9906{
9907 struct cfg80211_scan_request *req = rdev->scan_req;
9908 struct nlattr *nest;
9909 int i;
9910
9911 if (WARN_ON(!req))
9912 return 0;
9913
9914 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9915 if (!nest)
9916 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009917 for (i = 0; i < req->n_ssids; i++) {
9918 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9919 goto nla_put_failure;
9920 }
Johannes Berg362a4152009-05-24 16:43:15 +02009921 nla_nest_end(msg, nest);
9922
9923 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9924 if (!nest)
9925 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009926 for (i = 0; i < req->n_channels; i++) {
9927 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9928 goto nla_put_failure;
9929 }
Johannes Berg362a4152009-05-24 16:43:15 +02009930 nla_nest_end(msg, nest);
9931
David S. Miller9360ffd2012-03-29 04:41:26 -04009932 if (req->ie &&
9933 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9934 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009935
Johannes Bergae917c92013-10-25 11:05:22 +02009936 if (req->flags &&
9937 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
9938 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -07009939
Johannes Berg362a4152009-05-24 16:43:15 +02009940 return 0;
9941 nla_put_failure:
9942 return -ENOBUFS;
9943}
9944
Johannes Berga538e2d2009-06-16 19:56:42 +02009945static int nl80211_send_scan_msg(struct sk_buff *msg,
9946 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009947 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009948 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009949 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009950{
9951 void *hdr;
9952
Eric W. Biederman15e47302012-09-07 20:12:54 +00009953 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009954 if (!hdr)
9955 return -1;
9956
David S. Miller9360ffd2012-03-29 04:41:26 -04009957 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009958 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9959 wdev->netdev->ifindex)) ||
9960 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009961 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009962
Johannes Berg362a4152009-05-24 16:43:15 +02009963 /* ignore errors and send incomplete event anyway */
9964 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009965
9966 return genlmsg_end(msg, hdr);
9967
9968 nla_put_failure:
9969 genlmsg_cancel(msg, hdr);
9970 return -EMSGSIZE;
9971}
9972
Luciano Coelho807f8a82011-05-11 17:09:35 +03009973static int
9974nl80211_send_sched_scan_msg(struct sk_buff *msg,
9975 struct cfg80211_registered_device *rdev,
9976 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009977 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009978{
9979 void *hdr;
9980
Eric W. Biederman15e47302012-09-07 20:12:54 +00009981 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009982 if (!hdr)
9983 return -1;
9984
David S. Miller9360ffd2012-03-29 04:41:26 -04009985 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9986 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9987 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009988
9989 return genlmsg_end(msg, hdr);
9990
9991 nla_put_failure:
9992 genlmsg_cancel(msg, hdr);
9993 return -EMSGSIZE;
9994}
9995
Johannes Berga538e2d2009-06-16 19:56:42 +02009996void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009997 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009998{
9999 struct sk_buff *msg;
10000
Thomas Graf58050fc2012-06-28 03:57:45 +000010001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010002 if (!msg)
10003 return;
10004
Johannes Bergfd014282012-06-18 19:17:03 +020010005 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010006 NL80211_CMD_TRIGGER_SCAN) < 0) {
10007 nlmsg_free(msg);
10008 return;
10009 }
10010
Johannes Berg68eb5502013-11-19 15:19:38 +010010011 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010012 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010013}
10014
Johannes Bergf9d15d12014-01-22 11:14:19 +020010015struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10016 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010017{
10018 struct sk_buff *msg;
10019
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010021 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010022 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010023
Johannes Bergfd014282012-06-18 19:17:03 +020010024 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010025 aborted ? NL80211_CMD_SCAN_ABORTED :
10026 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010027 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010028 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010029 }
10030
Johannes Bergf9d15d12014-01-22 11:14:19 +020010031 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010032}
10033
Johannes Bergf9d15d12014-01-22 11:14:19 +020010034void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10035 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010036{
Johannes Berg2a519312009-02-10 21:25:55 +010010037 if (!msg)
10038 return;
10039
Johannes Berg68eb5502013-11-19 15:19:38 +010010040 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010041 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010042}
10043
Luciano Coelho807f8a82011-05-11 17:09:35 +030010044void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10045 struct net_device *netdev)
10046{
10047 struct sk_buff *msg;
10048
10049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10050 if (!msg)
10051 return;
10052
10053 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10054 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10055 nlmsg_free(msg);
10056 return;
10057 }
10058
Johannes Berg68eb5502013-11-19 15:19:38 +010010059 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010060 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010061}
10062
10063void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10064 struct net_device *netdev, u32 cmd)
10065{
10066 struct sk_buff *msg;
10067
Thomas Graf58050fc2012-06-28 03:57:45 +000010068 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010069 if (!msg)
10070 return;
10071
10072 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10073 nlmsg_free(msg);
10074 return;
10075 }
10076
Johannes Berg68eb5502013-11-19 15:19:38 +010010077 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010078 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010079}
10080
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010081/*
10082 * This can happen on global regulatory changes or device specific settings
10083 * based on custom world regulatory domains.
10084 */
10085void nl80211_send_reg_change_event(struct regulatory_request *request)
10086{
10087 struct sk_buff *msg;
10088 void *hdr;
10089
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010090 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010091 if (!msg)
10092 return;
10093
10094 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10095 if (!hdr) {
10096 nlmsg_free(msg);
10097 return;
10098 }
10099
10100 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010101 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10102 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010103
David S. Miller9360ffd2012-03-29 04:41:26 -040010104 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10105 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10106 NL80211_REGDOM_TYPE_WORLD))
10107 goto nla_put_failure;
10108 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10109 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10110 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10111 goto nla_put_failure;
10112 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10113 request->intersect) {
10114 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10115 NL80211_REGDOM_TYPE_INTERSECTION))
10116 goto nla_put_failure;
10117 } else {
10118 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10119 NL80211_REGDOM_TYPE_COUNTRY) ||
10120 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10121 request->alpha2))
10122 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010123 }
10124
Johannes Bergf4173762012-12-03 18:23:37 +010010125 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010126 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10127 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010128
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010129 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010130
Johannes Bergbc43b282009-07-25 10:54:13 +020010131 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010132 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010133 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010134 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010135
10136 return;
10137
10138nla_put_failure:
10139 genlmsg_cancel(msg, hdr);
10140 nlmsg_free(msg);
10141}
10142
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010143static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10144 struct net_device *netdev,
10145 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010146 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010147{
10148 struct sk_buff *msg;
10149 void *hdr;
10150
Johannes Berge6d6e342009-07-01 21:26:47 +020010151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010152 if (!msg)
10153 return;
10154
10155 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10156 if (!hdr) {
10157 nlmsg_free(msg);
10158 return;
10159 }
10160
David S. Miller9360ffd2012-03-29 04:41:26 -040010161 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10162 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10163 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10164 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010165
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010166 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010167
Johannes Berg68eb5502013-11-19 15:19:38 +010010168 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010169 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010170 return;
10171
10172 nla_put_failure:
10173 genlmsg_cancel(msg, hdr);
10174 nlmsg_free(msg);
10175}
10176
10177void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010178 struct net_device *netdev, const u8 *buf,
10179 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010180{
10181 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010182 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010183}
10184
10185void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10186 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010187 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010188{
Johannes Berge6d6e342009-07-01 21:26:47 +020010189 nl80211_send_mlme_event(rdev, netdev, buf, len,
10190 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010191}
10192
Jouni Malinen53b46b82009-03-27 20:53:56 +020010193void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010194 struct net_device *netdev, const u8 *buf,
10195 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010196{
10197 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010198 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010199}
10200
Jouni Malinen53b46b82009-03-27 20:53:56 +020010201void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10202 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010203 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_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010207}
10208
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010209void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10210 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010211{
Johannes Berg947add32013-02-22 22:05:20 +010010212 struct wireless_dev *wdev = dev->ieee80211_ptr;
10213 struct wiphy *wiphy = wdev->wiphy;
10214 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010215 const struct ieee80211_mgmt *mgmt = (void *)buf;
10216 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010217
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010218 if (WARN_ON(len < 2))
10219 return;
10220
10221 if (ieee80211_is_deauth(mgmt->frame_control))
10222 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10223 else
10224 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10225
10226 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10227 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010228}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010229EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010230
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010231static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10232 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010233 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010234{
10235 struct sk_buff *msg;
10236 void *hdr;
10237
Johannes Berge6d6e342009-07-01 21:26:47 +020010238 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010239 if (!msg)
10240 return;
10241
10242 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10243 if (!hdr) {
10244 nlmsg_free(msg);
10245 return;
10246 }
10247
David S. Miller9360ffd2012-03-29 04:41:26 -040010248 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10249 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10250 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10251 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10252 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010253
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010254 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010255
Johannes Berg68eb5502013-11-19 15:19:38 +010010256 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010257 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010258 return;
10259
10260 nla_put_failure:
10261 genlmsg_cancel(msg, hdr);
10262 nlmsg_free(msg);
10263}
10264
10265void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010266 struct net_device *netdev, const u8 *addr,
10267 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010268{
10269 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010270 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010271}
10272
10273void nl80211_send_assoc_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{
Johannes Berge6d6e342009-07-01 21:26:47 +020010277 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10278 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010279}
10280
Samuel Ortizb23aa672009-07-01 21:26:54 +020010281void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10282 struct net_device *netdev, const u8 *bssid,
10283 const u8 *req_ie, size_t req_ie_len,
10284 const u8 *resp_ie, size_t resp_ie_len,
10285 u16 status, gfp_t gfp)
10286{
10287 struct sk_buff *msg;
10288 void *hdr;
10289
Thomas Graf58050fc2012-06-28 03:57:45 +000010290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010291 if (!msg)
10292 return;
10293
10294 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10295 if (!hdr) {
10296 nlmsg_free(msg);
10297 return;
10298 }
10299
David S. Miller9360ffd2012-03-29 04:41:26 -040010300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10301 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10302 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10303 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10304 (req_ie &&
10305 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10306 (resp_ie &&
10307 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10308 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010309
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010310 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010311
Johannes Berg68eb5502013-11-19 15:19:38 +010010312 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010313 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010314 return;
10315
10316 nla_put_failure:
10317 genlmsg_cancel(msg, hdr);
10318 nlmsg_free(msg);
10319
10320}
10321
10322void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10323 struct net_device *netdev, const u8 *bssid,
10324 const u8 *req_ie, size_t req_ie_len,
10325 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10326{
10327 struct sk_buff *msg;
10328 void *hdr;
10329
Thomas Graf58050fc2012-06-28 03:57:45 +000010330 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010331 if (!msg)
10332 return;
10333
10334 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10335 if (!hdr) {
10336 nlmsg_free(msg);
10337 return;
10338 }
10339
David S. Miller9360ffd2012-03-29 04:41:26 -040010340 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10341 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10342 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10343 (req_ie &&
10344 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10345 (resp_ie &&
10346 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10347 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010348
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010349 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010350
Johannes Berg68eb5502013-11-19 15:19:38 +010010351 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010352 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010353 return;
10354
10355 nla_put_failure:
10356 genlmsg_cancel(msg, hdr);
10357 nlmsg_free(msg);
10358
10359}
10360
10361void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10362 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +020010363 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010364{
10365 struct sk_buff *msg;
10366 void *hdr;
10367
Thomas Graf58050fc2012-06-28 03:57:45 +000010368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010369 if (!msg)
10370 return;
10371
10372 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10373 if (!hdr) {
10374 nlmsg_free(msg);
10375 return;
10376 }
10377
David S. Miller9360ffd2012-03-29 04:41:26 -040010378 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10379 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10380 (from_ap && reason &&
10381 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10382 (from_ap &&
10383 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10384 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10385 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010386
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010387 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010388
Johannes Berg68eb5502013-11-19 15:19:38 +010010389 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010390 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010391 return;
10392
10393 nla_put_failure:
10394 genlmsg_cancel(msg, hdr);
10395 nlmsg_free(msg);
10396
10397}
10398
Johannes Berg04a773a2009-04-19 21:24:32 +020010399void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10400 struct net_device *netdev, const u8 *bssid,
10401 gfp_t gfp)
10402{
10403 struct sk_buff *msg;
10404 void *hdr;
10405
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010407 if (!msg)
10408 return;
10409
10410 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10411 if (!hdr) {
10412 nlmsg_free(msg);
10413 return;
10414 }
10415
David S. Miller9360ffd2012-03-29 04:41:26 -040010416 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10417 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10418 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10419 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010420
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010421 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010422
Johannes Berg68eb5502013-11-19 15:19:38 +010010423 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010424 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010425 return;
10426
10427 nla_put_failure:
10428 genlmsg_cancel(msg, hdr);
10429 nlmsg_free(msg);
10430}
10431
Johannes Berg947add32013-02-22 22:05:20 +010010432void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10433 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010434{
Johannes Berg947add32013-02-22 22:05:20 +010010435 struct wireless_dev *wdev = dev->ieee80211_ptr;
10436 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010437 struct sk_buff *msg;
10438 void *hdr;
10439
Johannes Berg947add32013-02-22 22:05:20 +010010440 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10441 return;
10442
10443 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10444
Javier Cardonac93b5e72011-04-07 15:08:34 -070010445 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10446 if (!msg)
10447 return;
10448
10449 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10450 if (!hdr) {
10451 nlmsg_free(msg);
10452 return;
10453 }
10454
David S. Miller9360ffd2012-03-29 04:41:26 -040010455 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010456 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10457 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010458 (ie_len && ie &&
10459 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10460 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010461
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010462 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010463
Johannes Berg68eb5502013-11-19 15:19:38 +010010464 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010465 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010466 return;
10467
10468 nla_put_failure:
10469 genlmsg_cancel(msg, hdr);
10470 nlmsg_free(msg);
10471}
Johannes Berg947add32013-02-22 22:05:20 +010010472EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010473
Jouni Malinena3b8b052009-03-27 21:59:49 +020010474void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10475 struct net_device *netdev, const u8 *addr,
10476 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010477 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010478{
10479 struct sk_buff *msg;
10480 void *hdr;
10481
Johannes Berge6d6e342009-07-01 21:26:47 +020010482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010483 if (!msg)
10484 return;
10485
10486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10487 if (!hdr) {
10488 nlmsg_free(msg);
10489 return;
10490 }
10491
David S. Miller9360ffd2012-03-29 04:41:26 -040010492 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10493 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10494 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10495 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10496 (key_id != -1 &&
10497 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10498 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10499 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010500
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010501 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010502
Johannes Berg68eb5502013-11-19 15:19:38 +010010503 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010504 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010505 return;
10506
10507 nla_put_failure:
10508 genlmsg_cancel(msg, hdr);
10509 nlmsg_free(msg);
10510}
10511
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010512void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10513 struct ieee80211_channel *channel_before,
10514 struct ieee80211_channel *channel_after)
10515{
10516 struct sk_buff *msg;
10517 void *hdr;
10518 struct nlattr *nl_freq;
10519
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010520 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010521 if (!msg)
10522 return;
10523
10524 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10525 if (!hdr) {
10526 nlmsg_free(msg);
10527 return;
10528 }
10529
10530 /*
10531 * Since we are applying the beacon hint to a wiphy we know its
10532 * wiphy_idx is valid
10533 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010534 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10535 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010536
10537 /* Before */
10538 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10539 if (!nl_freq)
10540 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010541 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010542 goto nla_put_failure;
10543 nla_nest_end(msg, nl_freq);
10544
10545 /* After */
10546 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10547 if (!nl_freq)
10548 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010549 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010550 goto nla_put_failure;
10551 nla_nest_end(msg, nl_freq);
10552
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010553 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010554
Johannes Berg463d0182009-07-14 00:33:35 +020010555 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010556 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010557 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010558 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010559
10560 return;
10561
10562nla_put_failure:
10563 genlmsg_cancel(msg, hdr);
10564 nlmsg_free(msg);
10565}
10566
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010567static void nl80211_send_remain_on_chan_event(
10568 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010569 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010570 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010571 unsigned int duration, gfp_t gfp)
10572{
10573 struct sk_buff *msg;
10574 void *hdr;
10575
10576 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10577 if (!msg)
10578 return;
10579
10580 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10581 if (!hdr) {
10582 nlmsg_free(msg);
10583 return;
10584 }
10585
David S. Miller9360ffd2012-03-29 04:41:26 -040010586 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010587 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10588 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010589 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010590 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010591 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10592 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010593 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10594 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010595
David S. Miller9360ffd2012-03-29 04:41:26 -040010596 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10597 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10598 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010599
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010600 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010601
Johannes Berg68eb5502013-11-19 15:19:38 +010010602 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010603 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010604 return;
10605
10606 nla_put_failure:
10607 genlmsg_cancel(msg, hdr);
10608 nlmsg_free(msg);
10609}
10610
Johannes Berg947add32013-02-22 22:05:20 +010010611void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10612 struct ieee80211_channel *chan,
10613 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010614{
Johannes Berg947add32013-02-22 22:05:20 +010010615 struct wiphy *wiphy = wdev->wiphy;
10616 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10617
10618 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010619 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010620 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010621 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010622}
Johannes Berg947add32013-02-22 22:05:20 +010010623EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010624
Johannes Berg947add32013-02-22 22:05:20 +010010625void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10626 struct ieee80211_channel *chan,
10627 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010628{
Johannes Berg947add32013-02-22 22:05:20 +010010629 struct wiphy *wiphy = wdev->wiphy;
10630 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10631
10632 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010633 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010634 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010635}
Johannes Berg947add32013-02-22 22:05:20 +010010636EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010637
Johannes Berg947add32013-02-22 22:05:20 +010010638void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10639 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010640{
Johannes Berg947add32013-02-22 22:05:20 +010010641 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10642 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010643 struct sk_buff *msg;
10644
Johannes Berg947add32013-02-22 22:05:20 +010010645 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10646
Thomas Graf58050fc2012-06-28 03:57:45 +000010647 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010648 if (!msg)
10649 return;
10650
John W. Linville66266b32012-03-15 13:25:41 -040010651 if (nl80211_send_station(msg, 0, 0, 0,
10652 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010653 nlmsg_free(msg);
10654 return;
10655 }
10656
Johannes Berg68eb5502013-11-19 15:19:38 +010010657 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010658 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010659}
Johannes Berg947add32013-02-22 22:05:20 +010010660EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010661
Johannes Berg947add32013-02-22 22:05:20 +010010662void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010663{
Johannes Berg947add32013-02-22 22:05:20 +010010664 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10665 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010666 struct sk_buff *msg;
10667 void *hdr;
10668
Johannes Berg947add32013-02-22 22:05:20 +010010669 trace_cfg80211_del_sta(dev, mac_addr);
10670
Thomas Graf58050fc2012-06-28 03:57:45 +000010671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010672 if (!msg)
10673 return;
10674
10675 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10676 if (!hdr) {
10677 nlmsg_free(msg);
10678 return;
10679 }
10680
David S. Miller9360ffd2012-03-29 04:41:26 -040010681 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10682 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10683 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010684
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010685 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010686
Johannes Berg68eb5502013-11-19 15:19:38 +010010687 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010688 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010689 return;
10690
10691 nla_put_failure:
10692 genlmsg_cancel(msg, hdr);
10693 nlmsg_free(msg);
10694}
Johannes Berg947add32013-02-22 22:05:20 +010010695EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010696
Johannes Berg947add32013-02-22 22:05:20 +010010697void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10698 enum nl80211_connect_failed_reason reason,
10699 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010700{
Johannes Berg947add32013-02-22 22:05:20 +010010701 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10702 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010703 struct sk_buff *msg;
10704 void *hdr;
10705
10706 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10707 if (!msg)
10708 return;
10709
10710 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10711 if (!hdr) {
10712 nlmsg_free(msg);
10713 return;
10714 }
10715
10716 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10717 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10718 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10719 goto nla_put_failure;
10720
10721 genlmsg_end(msg, hdr);
10722
Johannes Berg68eb5502013-11-19 15:19:38 +010010723 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010724 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010725 return;
10726
10727 nla_put_failure:
10728 genlmsg_cancel(msg, hdr);
10729 nlmsg_free(msg);
10730}
Johannes Berg947add32013-02-22 22:05:20 +010010731EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010732
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010733static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10734 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010735{
10736 struct wireless_dev *wdev = dev->ieee80211_ptr;
10737 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10738 struct sk_buff *msg;
10739 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010740 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010741
Eric W. Biederman15e47302012-09-07 20:12:54 +000010742 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010743 return false;
10744
10745 msg = nlmsg_new(100, gfp);
10746 if (!msg)
10747 return true;
10748
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010749 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010750 if (!hdr) {
10751 nlmsg_free(msg);
10752 return true;
10753 }
10754
David S. Miller9360ffd2012-03-29 04:41:26 -040010755 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10756 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10757 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10758 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010759
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010760 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010761 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010762 return true;
10763
10764 nla_put_failure:
10765 genlmsg_cancel(msg, hdr);
10766 nlmsg_free(msg);
10767 return true;
10768}
10769
Johannes Berg947add32013-02-22 22:05:20 +010010770bool cfg80211_rx_spurious_frame(struct net_device *dev,
10771 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010772{
Johannes Berg947add32013-02-22 22:05:20 +010010773 struct wireless_dev *wdev = dev->ieee80211_ptr;
10774 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010775
Johannes Berg947add32013-02-22 22:05:20 +010010776 trace_cfg80211_rx_spurious_frame(dev, addr);
10777
10778 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10779 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10780 trace_cfg80211_return_bool(false);
10781 return false;
10782 }
10783 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10784 addr, gfp);
10785 trace_cfg80211_return_bool(ret);
10786 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010787}
Johannes Berg947add32013-02-22 22:05:20 +010010788EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10789
10790bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10791 const u8 *addr, gfp_t gfp)
10792{
10793 struct wireless_dev *wdev = dev->ieee80211_ptr;
10794 bool ret;
10795
10796 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10797
10798 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10799 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10800 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10801 trace_cfg80211_return_bool(false);
10802 return false;
10803 }
10804 ret = __nl80211_unexpected_frame(dev,
10805 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10806 addr, gfp);
10807 trace_cfg80211_return_bool(ret);
10808 return ret;
10809}
10810EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010811
Johannes Berg2e161f72010-08-12 15:38:38 +020010812int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010813 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010814 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010815 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010816{
Johannes Berg71bbc992012-06-15 15:30:18 +020010817 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010818 struct sk_buff *msg;
10819 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010820
10821 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10822 if (!msg)
10823 return -ENOMEM;
10824
Johannes Berg2e161f72010-08-12 15:38:38 +020010825 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010826 if (!hdr) {
10827 nlmsg_free(msg);
10828 return -ENOMEM;
10829 }
10830
David S. Miller9360ffd2012-03-29 04:41:26 -040010831 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010832 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10833 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010834 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010835 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10836 (sig_dbm &&
10837 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010838 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10839 (flags &&
10840 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010841 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010842
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010843 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010844
Eric W. Biederman15e47302012-09-07 20:12:54 +000010845 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010846
10847 nla_put_failure:
10848 genlmsg_cancel(msg, hdr);
10849 nlmsg_free(msg);
10850 return -ENOBUFS;
10851}
10852
Johannes Berg947add32013-02-22 22:05:20 +010010853void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10854 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010855{
Johannes Berg947add32013-02-22 22:05:20 +010010856 struct wiphy *wiphy = wdev->wiphy;
10857 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010858 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010859 struct sk_buff *msg;
10860 void *hdr;
10861
Johannes Berg947add32013-02-22 22:05:20 +010010862 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10863
Jouni Malinen026331c2010-02-15 12:53:10 +020010864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10865 if (!msg)
10866 return;
10867
Johannes Berg2e161f72010-08-12 15:38:38 +020010868 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010869 if (!hdr) {
10870 nlmsg_free(msg);
10871 return;
10872 }
10873
David S. Miller9360ffd2012-03-29 04:41:26 -040010874 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010875 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10876 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010877 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010878 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10879 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10880 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10881 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010882
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010883 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010884
Johannes Berg68eb5502013-11-19 15:19:38 +010010885 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010886 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010887 return;
10888
10889 nla_put_failure:
10890 genlmsg_cancel(msg, hdr);
10891 nlmsg_free(msg);
10892}
Johannes Berg947add32013-02-22 22:05:20 +010010893EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010894
Johannes Berg947add32013-02-22 22:05:20 +010010895void cfg80211_cqm_rssi_notify(struct net_device *dev,
10896 enum nl80211_cqm_rssi_threshold_event rssi_event,
10897 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010898{
Johannes Berg947add32013-02-22 22:05:20 +010010899 struct wireless_dev *wdev = dev->ieee80211_ptr;
10900 struct wiphy *wiphy = wdev->wiphy;
10901 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010902 struct sk_buff *msg;
10903 struct nlattr *pinfoattr;
10904 void *hdr;
10905
Johannes Berg947add32013-02-22 22:05:20 +010010906 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10907
Thomas Graf58050fc2012-06-28 03:57:45 +000010908 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010909 if (!msg)
10910 return;
10911
10912 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10913 if (!hdr) {
10914 nlmsg_free(msg);
10915 return;
10916 }
10917
David S. Miller9360ffd2012-03-29 04:41:26 -040010918 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010919 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010920 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010921
10922 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10923 if (!pinfoattr)
10924 goto nla_put_failure;
10925
David S. Miller9360ffd2012-03-29 04:41:26 -040010926 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10927 rssi_event))
10928 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010929
10930 nla_nest_end(msg, pinfoattr);
10931
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010932 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010933
Johannes Berg68eb5502013-11-19 15:19:38 +010010934 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010935 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010936 return;
10937
10938 nla_put_failure:
10939 genlmsg_cancel(msg, hdr);
10940 nlmsg_free(msg);
10941}
Johannes Berg947add32013-02-22 22:05:20 +010010942EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010943
Johannes Berg947add32013-02-22 22:05:20 +010010944static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10945 struct net_device *netdev, const u8 *bssid,
10946 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010947{
10948 struct sk_buff *msg;
10949 struct nlattr *rekey_attr;
10950 void *hdr;
10951
Thomas Graf58050fc2012-06-28 03:57:45 +000010952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010953 if (!msg)
10954 return;
10955
10956 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10957 if (!hdr) {
10958 nlmsg_free(msg);
10959 return;
10960 }
10961
David S. Miller9360ffd2012-03-29 04:41:26 -040010962 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10963 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10964 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10965 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010966
10967 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10968 if (!rekey_attr)
10969 goto nla_put_failure;
10970
David S. Miller9360ffd2012-03-29 04:41:26 -040010971 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10972 NL80211_REPLAY_CTR_LEN, replay_ctr))
10973 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010974
10975 nla_nest_end(msg, rekey_attr);
10976
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010977 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010978
Johannes Berg68eb5502013-11-19 15:19:38 +010010979 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010980 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010981 return;
10982
10983 nla_put_failure:
10984 genlmsg_cancel(msg, hdr);
10985 nlmsg_free(msg);
10986}
10987
Johannes Berg947add32013-02-22 22:05:20 +010010988void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10989 const u8 *replay_ctr, gfp_t gfp)
10990{
10991 struct wireless_dev *wdev = dev->ieee80211_ptr;
10992 struct wiphy *wiphy = wdev->wiphy;
10993 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10994
10995 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10996 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10997}
10998EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10999
11000static void
11001nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11002 struct net_device *netdev, int index,
11003 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011004{
11005 struct sk_buff *msg;
11006 struct nlattr *attr;
11007 void *hdr;
11008
Thomas Graf58050fc2012-06-28 03:57:45 +000011009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011010 if (!msg)
11011 return;
11012
11013 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11014 if (!hdr) {
11015 nlmsg_free(msg);
11016 return;
11017 }
11018
David S. Miller9360ffd2012-03-29 04:41:26 -040011019 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11020 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11021 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011022
11023 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11024 if (!attr)
11025 goto nla_put_failure;
11026
David S. Miller9360ffd2012-03-29 04:41:26 -040011027 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11028 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11029 (preauth &&
11030 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11031 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011032
11033 nla_nest_end(msg, attr);
11034
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011035 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011036
Johannes Berg68eb5502013-11-19 15:19:38 +010011037 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011038 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011039 return;
11040
11041 nla_put_failure:
11042 genlmsg_cancel(msg, hdr);
11043 nlmsg_free(msg);
11044}
11045
Johannes Berg947add32013-02-22 22:05:20 +010011046void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11047 const u8 *bssid, bool preauth, gfp_t gfp)
11048{
11049 struct wireless_dev *wdev = dev->ieee80211_ptr;
11050 struct wiphy *wiphy = wdev->wiphy;
11051 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11052
11053 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11054 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11055}
11056EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11057
11058static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11059 struct net_device *netdev,
11060 struct cfg80211_chan_def *chandef,
11061 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011062{
11063 struct sk_buff *msg;
11064 void *hdr;
11065
Thomas Graf58050fc2012-06-28 03:57:45 +000011066 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011067 if (!msg)
11068 return;
11069
11070 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11071 if (!hdr) {
11072 nlmsg_free(msg);
11073 return;
11074 }
11075
Johannes Berg683b6d32012-11-08 21:25:48 +010011076 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11077 goto nla_put_failure;
11078
11079 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011080 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011081
11082 genlmsg_end(msg, hdr);
11083
Johannes Berg68eb5502013-11-19 15:19:38 +010011084 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011085 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011086 return;
11087
11088 nla_put_failure:
11089 genlmsg_cancel(msg, hdr);
11090 nlmsg_free(msg);
11091}
11092
Johannes Berg947add32013-02-22 22:05:20 +010011093void cfg80211_ch_switch_notify(struct net_device *dev,
11094 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011095{
Johannes Berg947add32013-02-22 22:05:20 +010011096 struct wireless_dev *wdev = dev->ieee80211_ptr;
11097 struct wiphy *wiphy = wdev->wiphy;
11098 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11099
Simon Wunderliche487eae2013-11-21 18:19:51 +010011100 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011101
Simon Wunderliche487eae2013-11-21 18:19:51 +010011102 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011103
11104 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011105 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011106 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11107 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011108 return;
Johannes Berg947add32013-02-22 22:05:20 +010011109
11110 wdev->channel = chandef->chan;
11111 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011112}
11113EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11114
11115void cfg80211_cqm_txe_notify(struct net_device *dev,
11116 const u8 *peer, u32 num_packets,
11117 u32 rate, u32 intvl, gfp_t gfp)
11118{
11119 struct wireless_dev *wdev = dev->ieee80211_ptr;
11120 struct wiphy *wiphy = wdev->wiphy;
11121 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011122 struct sk_buff *msg;
11123 struct nlattr *pinfoattr;
11124 void *hdr;
11125
11126 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11127 if (!msg)
11128 return;
11129
11130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11131 if (!hdr) {
11132 nlmsg_free(msg);
11133 return;
11134 }
11135
11136 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011137 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011138 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11139 goto nla_put_failure;
11140
11141 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11142 if (!pinfoattr)
11143 goto nla_put_failure;
11144
11145 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11146 goto nla_put_failure;
11147
11148 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11149 goto nla_put_failure;
11150
11151 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11152 goto nla_put_failure;
11153
11154 nla_nest_end(msg, pinfoattr);
11155
11156 genlmsg_end(msg, hdr);
11157
Johannes Berg68eb5502013-11-19 15:19:38 +010011158 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011159 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011160 return;
11161
11162 nla_put_failure:
11163 genlmsg_cancel(msg, hdr);
11164 nlmsg_free(msg);
11165}
Johannes Berg947add32013-02-22 22:05:20 +010011166EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011167
11168void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011169nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011170 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011171 enum nl80211_radar_event event,
11172 struct net_device *netdev, gfp_t gfp)
11173{
11174 struct sk_buff *msg;
11175 void *hdr;
11176
11177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11178 if (!msg)
11179 return;
11180
11181 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11182 if (!hdr) {
11183 nlmsg_free(msg);
11184 return;
11185 }
11186
11187 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11188 goto nla_put_failure;
11189
11190 /* NOP and radar events don't need a netdev parameter */
11191 if (netdev) {
11192 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11193
11194 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11195 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11196 goto nla_put_failure;
11197 }
11198
11199 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11200 goto nla_put_failure;
11201
11202 if (nl80211_send_chandef(msg, chandef))
11203 goto nla_put_failure;
11204
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011205 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011206
Johannes Berg68eb5502013-11-19 15:19:38 +010011207 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011208 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011209 return;
11210
11211 nla_put_failure:
11212 genlmsg_cancel(msg, hdr);
11213 nlmsg_free(msg);
11214}
11215
Johannes Berg947add32013-02-22 22:05:20 +010011216void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11217 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011218{
Johannes Berg947add32013-02-22 22:05:20 +010011219 struct wireless_dev *wdev = dev->ieee80211_ptr;
11220 struct wiphy *wiphy = wdev->wiphy;
11221 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011222 struct sk_buff *msg;
11223 struct nlattr *pinfoattr;
11224 void *hdr;
11225
Johannes Berg947add32013-02-22 22:05:20 +010011226 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11227
Thomas Graf58050fc2012-06-28 03:57:45 +000011228 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011229 if (!msg)
11230 return;
11231
11232 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11233 if (!hdr) {
11234 nlmsg_free(msg);
11235 return;
11236 }
11237
David S. Miller9360ffd2012-03-29 04:41:26 -040011238 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011239 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011240 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11241 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011242
11243 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11244 if (!pinfoattr)
11245 goto nla_put_failure;
11246
David S. Miller9360ffd2012-03-29 04:41:26 -040011247 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11248 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011249
11250 nla_nest_end(msg, pinfoattr);
11251
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011252 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011253
Johannes Berg68eb5502013-11-19 15:19:38 +010011254 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011255 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011256 return;
11257
11258 nla_put_failure:
11259 genlmsg_cancel(msg, hdr);
11260 nlmsg_free(msg);
11261}
Johannes Berg947add32013-02-22 22:05:20 +010011262EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011263
Johannes Berg7f6cf312011-11-04 11:18:15 +010011264void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11265 u64 cookie, bool acked, gfp_t gfp)
11266{
11267 struct wireless_dev *wdev = dev->ieee80211_ptr;
11268 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11269 struct sk_buff *msg;
11270 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011271
Beni Lev4ee3e062012-08-27 12:49:39 +030011272 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11273
Thomas Graf58050fc2012-06-28 03:57:45 +000011274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011275
Johannes Berg7f6cf312011-11-04 11:18:15 +010011276 if (!msg)
11277 return;
11278
11279 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11280 if (!hdr) {
11281 nlmsg_free(msg);
11282 return;
11283 }
11284
David S. Miller9360ffd2012-03-29 04:41:26 -040011285 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11286 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11287 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11288 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11289 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11290 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011291
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011292 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011293
Johannes Berg68eb5502013-11-19 15:19:38 +010011294 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011295 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011296 return;
11297
11298 nla_put_failure:
11299 genlmsg_cancel(msg, hdr);
11300 nlmsg_free(msg);
11301}
11302EXPORT_SYMBOL(cfg80211_probe_status);
11303
Johannes Berg5e760232011-11-04 11:18:17 +010011304void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11305 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011306 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011307{
11308 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11309 struct sk_buff *msg;
11310 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011311 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011312
Beni Lev4ee3e062012-08-27 12:49:39 +030011313 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11314
Ben Greear37c73b52012-10-26 14:49:25 -070011315 spin_lock_bh(&rdev->beacon_registrations_lock);
11316 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11317 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11318 if (!msg) {
11319 spin_unlock_bh(&rdev->beacon_registrations_lock);
11320 return;
11321 }
Johannes Berg5e760232011-11-04 11:18:17 +010011322
Ben Greear37c73b52012-10-26 14:49:25 -070011323 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11324 if (!hdr)
11325 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011326
Ben Greear37c73b52012-10-26 14:49:25 -070011327 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11328 (freq &&
11329 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11330 (sig_dbm &&
11331 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11332 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11333 goto nla_put_failure;
11334
11335 genlmsg_end(msg, hdr);
11336
11337 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011338 }
Ben Greear37c73b52012-10-26 14:49:25 -070011339 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011340 return;
11341
11342 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011343 spin_unlock_bh(&rdev->beacon_registrations_lock);
11344 if (hdr)
11345 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011346 nlmsg_free(msg);
11347}
11348EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11349
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011350#ifdef CONFIG_PM
11351void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11352 struct cfg80211_wowlan_wakeup *wakeup,
11353 gfp_t gfp)
11354{
11355 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11356 struct sk_buff *msg;
11357 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011358 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011359
11360 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11361
11362 if (wakeup)
11363 size += wakeup->packet_present_len;
11364
11365 msg = nlmsg_new(size, gfp);
11366 if (!msg)
11367 return;
11368
11369 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11370 if (!hdr)
11371 goto free_msg;
11372
11373 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11374 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11375 goto free_msg;
11376
11377 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11378 wdev->netdev->ifindex))
11379 goto free_msg;
11380
11381 if (wakeup) {
11382 struct nlattr *reasons;
11383
11384 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011385 if (!reasons)
11386 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011387
11388 if (wakeup->disconnect &&
11389 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11390 goto free_msg;
11391 if (wakeup->magic_pkt &&
11392 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11393 goto free_msg;
11394 if (wakeup->gtk_rekey_failure &&
11395 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11396 goto free_msg;
11397 if (wakeup->eap_identity_req &&
11398 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11399 goto free_msg;
11400 if (wakeup->four_way_handshake &&
11401 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11402 goto free_msg;
11403 if (wakeup->rfkill_release &&
11404 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11405 goto free_msg;
11406
11407 if (wakeup->pattern_idx >= 0 &&
11408 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11409 wakeup->pattern_idx))
11410 goto free_msg;
11411
Johannes Bergae917c92013-10-25 11:05:22 +020011412 if (wakeup->tcp_match &&
11413 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11414 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011415
Johannes Bergae917c92013-10-25 11:05:22 +020011416 if (wakeup->tcp_connlost &&
11417 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11418 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011419
Johannes Bergae917c92013-10-25 11:05:22 +020011420 if (wakeup->tcp_nomoretokens &&
11421 nla_put_flag(msg,
11422 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11423 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011424
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011425 if (wakeup->packet) {
11426 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11427 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11428
11429 if (!wakeup->packet_80211) {
11430 pkt_attr =
11431 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11432 len_attr =
11433 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11434 }
11435
11436 if (wakeup->packet_len &&
11437 nla_put_u32(msg, len_attr, wakeup->packet_len))
11438 goto free_msg;
11439
11440 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11441 wakeup->packet))
11442 goto free_msg;
11443 }
11444
11445 nla_nest_end(msg, reasons);
11446 }
11447
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011448 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011449
Johannes Berg68eb5502013-11-19 15:19:38 +010011450 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011451 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011452 return;
11453
11454 free_msg:
11455 nlmsg_free(msg);
11456}
11457EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11458#endif
11459
Jouni Malinen3475b092012-11-16 22:49:57 +020011460void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11461 enum nl80211_tdls_operation oper,
11462 u16 reason_code, gfp_t gfp)
11463{
11464 struct wireless_dev *wdev = dev->ieee80211_ptr;
11465 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11466 struct sk_buff *msg;
11467 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011468
11469 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11470 reason_code);
11471
11472 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11473 if (!msg)
11474 return;
11475
11476 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11477 if (!hdr) {
11478 nlmsg_free(msg);
11479 return;
11480 }
11481
11482 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11483 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11484 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11485 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11486 (reason_code > 0 &&
11487 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11488 goto nla_put_failure;
11489
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011490 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011491
Johannes Berg68eb5502013-11-19 15:19:38 +010011492 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011493 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011494 return;
11495
11496 nla_put_failure:
11497 genlmsg_cancel(msg, hdr);
11498 nlmsg_free(msg);
11499}
11500EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11501
Jouni Malinen026331c2010-02-15 12:53:10 +020011502static int nl80211_netlink_notify(struct notifier_block * nb,
11503 unsigned long state,
11504 void *_notify)
11505{
11506 struct netlink_notify *notify = _notify;
11507 struct cfg80211_registered_device *rdev;
11508 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011509 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011510
11511 if (state != NETLINK_URELEASE)
11512 return NOTIFY_DONE;
11513
11514 rcu_read_lock();
11515
Johannes Berg5e760232011-11-04 11:18:17 +010011516 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011517 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011518 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011519
11520 spin_lock_bh(&rdev->beacon_registrations_lock);
11521 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11522 list) {
11523 if (reg->nlportid == notify->portid) {
11524 list_del(&reg->list);
11525 kfree(reg);
11526 break;
11527 }
11528 }
11529 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011530 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011531
11532 rcu_read_unlock();
11533
11534 return NOTIFY_DONE;
11535}
11536
11537static struct notifier_block nl80211_netlink_notifier = {
11538 .notifier_call = nl80211_netlink_notify,
11539};
11540
Jouni Malinen355199e2013-02-27 17:14:27 +020011541void cfg80211_ft_event(struct net_device *netdev,
11542 struct cfg80211_ft_event_params *ft_event)
11543{
11544 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11545 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11546 struct sk_buff *msg;
11547 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011548
11549 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11550
11551 if (!ft_event->target_ap)
11552 return;
11553
11554 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11555 if (!msg)
11556 return;
11557
11558 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011559 if (!hdr)
11560 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011561
Johannes Bergae917c92013-10-25 11:05:22 +020011562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11563 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11565 goto out;
11566
11567 if (ft_event->ies &&
11568 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11569 goto out;
11570 if (ft_event->ric_ies &&
11571 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11572 ft_event->ric_ies))
11573 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011574
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011575 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011576
Johannes Berg68eb5502013-11-19 15:19:38 +010011577 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011578 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011579 return;
11580 out:
11581 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011582}
11583EXPORT_SYMBOL(cfg80211_ft_event);
11584
Arend van Spriel5de17982013-04-18 15:49:00 +020011585void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11586{
11587 struct cfg80211_registered_device *rdev;
11588 struct sk_buff *msg;
11589 void *hdr;
11590 u32 nlportid;
11591
11592 rdev = wiphy_to_dev(wdev->wiphy);
11593 if (!rdev->crit_proto_nlportid)
11594 return;
11595
11596 nlportid = rdev->crit_proto_nlportid;
11597 rdev->crit_proto_nlportid = 0;
11598
11599 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11600 if (!msg)
11601 return;
11602
11603 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11604 if (!hdr)
11605 goto nla_put_failure;
11606
11607 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11608 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11609 goto nla_put_failure;
11610
11611 genlmsg_end(msg, hdr);
11612
11613 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11614 return;
11615
11616 nla_put_failure:
11617 if (hdr)
11618 genlmsg_cancel(msg, hdr);
11619 nlmsg_free(msg);
11620
11621}
11622EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11623
Johannes Berg55682962007-09-20 13:09:35 -040011624/* initialisation/exit functions */
11625
11626int nl80211_init(void)
11627{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011628 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011629
Johannes Berg2a94fe42013-11-19 15:19:39 +010011630 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11631 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011632 if (err)
11633 return err;
11634
Jouni Malinen026331c2010-02-15 12:53:10 +020011635 err = netlink_register_notifier(&nl80211_netlink_notifier);
11636 if (err)
11637 goto err_out;
11638
Johannes Berg55682962007-09-20 13:09:35 -040011639 return 0;
11640 err_out:
11641 genl_unregister_family(&nl80211_fam);
11642 return err;
11643}
11644
11645void nl80211_exit(void)
11646{
Jouni Malinen026331c2010-02-15 12:53:10 +020011647 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011648 genl_unregister_family(&nl80211_fam);
11649}